split instructions

This commit is contained in:
Mark Canlas 2022-12-06 17:55:51 -05:00
parent 5370ae45ee
commit def9668d82
4 changed files with 44 additions and 20 deletions

View File

@ -71,7 +71,16 @@ object AsmBlock:
case class Intent(label: Option[String], instructions: List[Intent.Instruction])
object Intent:
case class Instruction(code: String, comment: Option[String])
sealed trait Instruction:
def length: Int
object Instruction:
case class One(code: String, operand: String, comment: Option[String]) extends Instruction:
def length: Int =
operand.length + 4
case class Zero(code: String, comment: Option[String]) extends Instruction:
def length: Int =
0
def toLines(x: Intent): List[String] =
val comment =
@ -80,19 +89,31 @@ object AsmBlock:
val maximumLength =
x
.instructions
.map(_.code.length)
.map(_.length)
.max
val instructions =
x
.instructions
.map { i =>
i.comment match
case Some(c) =>
String.format(s"%-${maximumLength}s", i.code) + " " + toComment(c)
.map {
case Instruction.Zero(code, oComment) =>
oComment match
case Some(c) =>
String.format(s"%-${maximumLength}s", code) + " " + toComment(c)
case None =>
i.code
case None =>
code
case Instruction.One(code, operand, oComment) =>
val leftSlug =
code + " " + operand
oComment match
case Some(c) =>
String.format(s"%-${maximumLength}s", leftSlug) + " " + toComment(c)
case None =>
leftSlug
}
.map(withIndent)

View File

@ -6,7 +6,7 @@ import com.htmlism.firepower.core.AsmBlock._
case class Subroutine(name: String, intents: List[Intent]):
def call: Intent.Instruction =
Intent.Instruction(s"jsr $name", None)
Intent.Instruction.One("jsr", name, None)
def attach: NamedCodeBlock =
NamedCodeBlock(name, "this is a named block".some, intents)

View File

@ -17,15 +17,15 @@ object AnnotatedSnake:
AsmBlock.Intent(
None,
List(
AsmBlock.Intent.Instruction("lda $00", None),
AsmBlock.Intent.Instruction("lda $01", "instruction comment".some)
AsmBlock.Intent.Instruction.One("lda", "$00", None),
AsmBlock.Intent.Instruction.One("lda", "$01", "instruction comment".some)
)
),
AsmBlock.Intent(
"this block has some preamble".some,
List(
AsmBlock.Intent.Instruction("lda $00", None),
AsmBlock.Intent.Instruction("lda $01", "instruction comment".some)
AsmBlock.Intent.Instruction.One("lda", "$00", None),
AsmBlock.Intent.Instruction.One("lda", "$01", "instruction comment".some)
)
)
)
@ -37,15 +37,15 @@ object AnnotatedSnake:
AsmBlock.Intent(
None,
List(
AsmBlock.Intent.Instruction("lda $00", None),
AsmBlock.Intent.Instruction("lda $01", "instruction comment".some)
AsmBlock.Intent.Instruction.One("lda", "$00", None),
AsmBlock.Intent.Instruction.One("lda", "$01", "instruction comment".some)
)
),
AsmBlock.Intent(
"this block has some preamble".some,
List(
AsmBlock.Intent.Instruction("lda $00", None),
AsmBlock.Intent.Instruction("lda $01", "instruction comment".some)
AsmBlock.Intent.Instruction.One("lda", "$00", None),
AsmBlock.Intent.Instruction.One("lda", "$01", "instruction comment".some)
)
)
)

View File

@ -77,11 +77,14 @@ object PrintThree:
List(
AsmBlock
.Intent
.Instruction(instruction("LDA", opts.instructionCase) + " " + argument, s"a = ${mv.src.toComment}".some),
.Instruction
.One(instruction("LDA", opts.instructionCase), argument, s"a = ${mv.src.toComment}".some),
AsmBlock
.Intent
.Instruction(
instruction("STA", opts.instructionCase) + " " + argumentTwo,
.Instruction
.One(
instruction("STA", opts.instructionCase),
argumentTwo,
s"${mv.dest.toComment} = a".some
)
)