merge instructions back

This commit is contained in:
Mark Canlas 2022-12-07 07:59:59 -05:00
parent d107d76f58
commit c354ec8023
4 changed files with 26 additions and 39 deletions

View File

@ -1,5 +1,7 @@
package com.htmlism.firepower.core package com.htmlism.firepower.core
import cats.syntax.all._
sealed trait AsmBlock sealed trait AsmBlock
object AsmBlock: object AsmBlock:
@ -94,16 +96,13 @@ object AsmBlock:
case class Intent(label: Option[String], instructions: List[Intent.Instruction]) case class Intent(label: Option[String], instructions: List[Intent.Instruction])
object Intent: object Intent:
sealed trait Instruction: case class Instruction(code: String, operand: Option[String], comment: Option[String]):
def length: Int def length: Int =
(code + " " + operand.getOrElse("")).length
object Instruction: object Instruction:
case class One(code: String, operand: String, comment: Option[String]) extends Instruction: def one(code: String, operand: String, comment: Option[String]): Instruction =
def length: Int = Instruction(code, operand.some, comment)
operand.length + 4
case class Zero(code: String, comment: Option[String]) extends Instruction:
def length: Int =
0
def toLines(opts: AssemblerOptions.InstructionCase)(x: Intent): List[String] = def toLines(opts: AssemblerOptions.InstructionCase)(x: Intent): List[String] =
val comment = val comment =
@ -118,28 +117,16 @@ object AsmBlock:
val instructions = val instructions =
x x
.instructions .instructions
.map { .map { i =>
case Instruction.Zero(code, oComment) => val leftSlug =
val codeUpperLower = instruction(i.code, opts) + i.operand.map(" " + _).getOrElse("")
instruction(code, opts)
oComment match i.comment match
case Some(c) => case Some(c) =>
String.format(s"%-${maximumLength}s", codeUpperLower) + " " + toComment(c) String.format(s"%-${maximumLength}s", leftSlug) + " " + toComment(c)
case None => case None =>
codeUpperLower leftSlug
case Instruction.One(code, operand, oComment) =>
val leftSlug =
instruction(code, opts) + " " + operand
oComment match
case Some(c) =>
String.format(s"%-${maximumLength}s", leftSlug) + " " + toComment(c)
case None =>
leftSlug
} }
.map(withIndent) .map(withIndent)

View File

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

View File

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

View File

@ -78,11 +78,11 @@ object PrintThree:
AsmBlock AsmBlock
.Intent .Intent
.Instruction .Instruction
.One("LDA", argument, s"a = ${mv.src.toComment}".some), .one("LDA", argument, s"a = ${mv.src.toComment}".some),
AsmBlock AsmBlock
.Intent .Intent
.Instruction .Instruction
.One( .one(
"STA", "STA",
argumentTwo, argumentTwo,
s"${mv.dest.toComment} = a".some s"${mv.dest.toComment} = a".some