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
import cats.syntax.all._
sealed trait AsmBlock
object AsmBlock:
@ -94,16 +96,13 @@ object AsmBlock:
case class Intent(label: Option[String], instructions: List[Intent.Instruction])
object Intent:
sealed trait Instruction:
def length: Int
case class Instruction(code: String, operand: Option[String], comment: Option[String]):
def length: Int =
(code + " " + operand.getOrElse("")).length
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 one(code: String, operand: String, comment: Option[String]): Instruction =
Instruction(code, operand.some, comment)
def toLines(opts: AssemblerOptions.InstructionCase)(x: Intent): List[String] =
val comment =
@ -118,23 +117,11 @@ object AsmBlock:
val instructions =
x
.instructions
.map {
case Instruction.Zero(code, oComment) =>
val codeUpperLower =
instruction(code, opts)
oComment match
case Some(c) =>
String.format(s"%-${maximumLength}s", codeUpperLower) + " " + toComment(c)
case None =>
codeUpperLower
case Instruction.One(code, operand, oComment) =>
.map { i =>
val leftSlug =
instruction(code, opts) + " " + operand
instruction(i.code, opts) + i.operand.map(" " + _).getOrElse("")
oComment match
i.comment match
case Some(c) =>
String.format(s"%-${maximumLength}s", leftSlug) + " " + toComment(c)

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.One("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.One("lda", "$00", None),
AsmBlock.Intent.Instruction.One("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.One("lda", "$00", None),
AsmBlock.Intent.Instruction.One("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.One("lda", "$00", None),
AsmBlock.Intent.Instruction.One("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.One("lda", "$00", None),
AsmBlock.Intent.Instruction.One("lda", "$01", "instruction comment".some)
AsmBlock.Intent.Instruction.one("lda", "$00", None),
AsmBlock.Intent.Instruction.one("lda", "$01", "instruction comment".some)
)
)
)

View File

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