diff --git a/firepower-core/src/main/scala/com/htmlism/firepower/core/AsmBlock.scala b/firepower-core/src/main/scala/com/htmlism/firepower/core/AsmBlock.scala index 9b66380..e0a25f4 100644 --- a/firepower-core/src/main/scala/com/htmlism/firepower/core/AsmBlock.scala +++ b/firepower-core/src/main/scala/com/htmlism/firepower/core/AsmBlock.scala @@ -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,28 +117,16 @@ object AsmBlock: val instructions = x .instructions - .map { - case Instruction.Zero(code, oComment) => - val codeUpperLower = - instruction(code, opts) + .map { i => + val leftSlug = + instruction(i.code, opts) + i.operand.map(" " + _).getOrElse("") - oComment match - case Some(c) => - String.format(s"%-${maximumLength}s", codeUpperLower) + " " + toComment(c) + i.comment match + case Some(c) => + String.format(s"%-${maximumLength}s", leftSlug) + " " + toComment(c) - case None => - codeUpperLower - - 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 + case None => + leftSlug } .map(withIndent) diff --git a/firepower-core/src/main/scala/com/htmlism/firepower/core/Subroutine.scala b/firepower-core/src/main/scala/com/htmlism/firepower/core/Subroutine.scala index a5e5f20..d3b02e8 100644 --- a/firepower-core/src/main/scala/com/htmlism/firepower/core/Subroutine.scala +++ b/firepower-core/src/main/scala/com/htmlism/firepower/core/Subroutine.scala @@ -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) diff --git a/firepower-demo/src/main/scala/com/htmlism/firepower/demo/AnnotatedSnake.scala b/firepower-demo/src/main/scala/com/htmlism/firepower/demo/AnnotatedSnake.scala index 4e0019a..66a1e77 100644 --- a/firepower-demo/src/main/scala/com/htmlism/firepower/demo/AnnotatedSnake.scala +++ b/firepower-demo/src/main/scala/com/htmlism/firepower/demo/AnnotatedSnake.scala @@ -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) ) ) ) diff --git a/firepower-demo/src/main/scala/com/htmlism/firepower/demo/PrintThree.scala b/firepower-demo/src/main/scala/com/htmlism/firepower/demo/PrintThree.scala index a24830f..559eb1c 100644 --- a/firepower-demo/src/main/scala/com/htmlism/firepower/demo/PrintThree.scala +++ b/firepower-demo/src/main/scala/com/htmlism/firepower/demo/PrintThree.scala @@ -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