From 1bf474d6bbceb495821f2b9941674a495ed5ac64 Mon Sep 17 00:00:00 2001 From: Mark Canlas Date: Tue, 6 Dec 2022 18:06:09 -0500 Subject: [PATCH] thread opts more specifically --- .../com/htmlism/firepower/core/AsmBlock.scala | 25 +++++++++++++----- .../firepower/demo/AnnotatedSnake.scala | 2 +- .../htmlism/firepower/demo/PrintThree.scala | 26 +++++++------------ 3 files changed, 28 insertions(+), 25 deletions(-) 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 d1945f9..065b557 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 @@ -40,7 +40,7 @@ object AsmBlock: "$" + hex.toUpperCase - def toLines(xs: AsmBlock): List[String] = + def toLines(opts: AssemblerOptions.InstructionCase)(xs: AsmBlock): List[String] = xs match case CommentBlock(ys) => ys.map(toComment) @@ -61,12 +61,12 @@ object AsmBlock: List(label + ":") ++ oComment.map(toComment).map(withIndent).toList val intentParagraphs = - intents.map(Intent.toLines) + intents.map(Intent.toLines(opts)) interFlatMap(headerParagraph :: intentParagraphs)(List(""), identity) case AnonymousCodeBlock(intents) => - interFlatMap(intents)(List(""), Intent.toLines) + interFlatMap(intents)(List(""), Intent.toLines(opts)) case class Intent(label: Option[String], instructions: List[Intent.Instruction]) @@ -82,7 +82,7 @@ object AsmBlock: def length: Int = 0 - def toLines(x: Intent): List[String] = + def toLines(opts: AssemblerOptions.InstructionCase)(x: Intent): List[String] = val comment = x.label.map(toComment).map(withIndent).toList @@ -97,16 +97,19 @@ object AsmBlock: .instructions .map { case Instruction.Zero(code, oComment) => + val codeUpperLower = + instruction(code, opts) + oComment match case Some(c) => - String.format(s"%-${maximumLength}s", code) + " " + toComment(c) + String.format(s"%-${maximumLength}s", codeUpperLower) + " " + toComment(c) case None => - code + codeUpperLower case Instruction.One(code, operand, oComment) => val leftSlug = - code + " " + operand + instruction(code, opts) + " " + operand oComment match case Some(c) => @@ -118,3 +121,11 @@ object AsmBlock: .map(withIndent) comment ++ instructions + + def instruction(s: String, instructionCase: AssemblerOptions.InstructionCase): String = + instructionCase match + case AssemblerOptions.InstructionCase.Uppercase => + s.toUpperCase + + case AssemblerOptions.InstructionCase.Lowercase => + s.toLowerCase 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 40cbb30..4e0019a 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 @@ -51,7 +51,7 @@ object AnnotatedSnake: ) ) ) - .map(AsmBlock.toLines) + .map(AsmBlock.toLines(AssemblerOptions.InstructionCase.Lowercase)) .pipe(xs => AsmBlock.interFlatMap(xs)(List("", ""), identity)) .pipe(str.Line.mkString) 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 289f522..a24830f 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 @@ -29,13 +29,13 @@ object PrintThree: build(Easy6502.Screen(0x200)) def assemble(opts: AssemblerOptions): String = - (defines(opts) ++ codes(opts)) - .map(AsmBlock.toLines) + (defines(opts.definitionsMode) ++ codes(opts.definitionsMode)) + .map(AsmBlock.toLines(opts.instructionCase)) .pipe(xs => AsmBlock.interFlatMap(xs)(List("", ""), identity)) .pipe(str.Line.mkString) - private def defines(opts: AssemblerOptions) = - opts.definitionsMode match + private def defines(opts: AssemblerOptions.DefinitionsMode) = + opts match case AssemblerOptions.DefinitionsMode.UseLiterals => Nil @@ -49,11 +49,11 @@ object PrintThree: .pipe(AsmBlock.DefinesBlock(_)) } - private def codes(opts: AssemblerOptions) = + private def codes(opts: AssemblerOptions.DefinitionsMode) = program .map { mv => val argument = - opts.definitionsMode match + opts match case AssemblerOptions.DefinitionsMode.UseLiterals => f"#$$${mv.src.toValue}%02X" @@ -62,7 +62,7 @@ object PrintThree: "#" + mv.src.toDefine val argumentTwo = - opts.definitionsMode match + opts match case AssemblerOptions.DefinitionsMode.UseLiterals => AsmBlock.toHex(mv.dest.toValue) @@ -78,12 +78,12 @@ object PrintThree: AsmBlock .Intent .Instruction - .One(instruction("LDA", opts.instructionCase), argument, s"a = ${mv.src.toComment}".some), + .One("LDA", argument, s"a = ${mv.src.toComment}".some), AsmBlock .Intent .Instruction .One( - instruction("STA", opts.instructionCase), + "STA", argumentTwo, s"${mv.dest.toComment} = a".some ) @@ -92,11 +92,3 @@ object PrintThree: } .pipe(AnonymousCodeBlock(_)) .pipe(List(_)) - - def instruction(s: String, instructionCase: AssemblerOptions.InstructionCase): String = - instructionCase match - case AssemblerOptions.InstructionCase.Uppercase => - s.toUpperCase - - case AssemblerOptions.InstructionCase.Lowercase => - s.toLowerCase