From d5b70c66abd992c9f1efa852c88f8ee9bd5754fb Mon Sep 17 00:00:00 2001 From: Mark Canlas Date: Wed, 7 Dec 2022 13:53:05 -0500 Subject: [PATCH] use table type with description --- data/print-three-upper-math.asm | 2 ++ data/print-three-upper.asm | 2 ++ .../com/htmlism/firepower/core/AsmBlock.scala | 16 +++++++++------- .../com/htmlism/firepower/demo/Definable.scala | 6 +++--- .../com/htmlism/firepower/demo/Easy6502.scala | 11 ++++++----- .../com/htmlism/firepower/demo/PrintThree.scala | 9 +++------ 6 files changed, 25 insertions(+), 21 deletions(-) diff --git a/data/print-three-upper-math.asm b/data/print-three-upper-math.asm index cc9dacf..f6c75ff 100644 --- a/data/print-three-upper-math.asm +++ b/data/print-three-upper-math.asm @@ -1,3 +1,4 @@ +; Colors that each screen pixel can be set to define COLOR_Black $00 define COLOR_White $01 define COLOR_Red $02 @@ -16,6 +17,7 @@ define COLOR_LightBlue $0E define COLOR_LightGrey $0F +; The screen as a collection of pixels define SCREEN $200 diff --git a/data/print-three-upper.asm b/data/print-three-upper.asm index 08952b9..be04f75 100644 --- a/data/print-three-upper.asm +++ b/data/print-three-upper.asm @@ -1,3 +1,4 @@ +; Colors that each screen pixel can be set to define COLOR_Black $00 define COLOR_White $01 define COLOR_Red $02 @@ -16,6 +17,7 @@ define COLOR_LightBlue $0E define COLOR_LightGrey $0F +; The screen as a collection of pixels define SCREEN $200 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 e0a25f4..9ffd783 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 @@ -21,8 +21,7 @@ object AsmBlock: /** * Renders as a table of aliases */ - // TODO needs descriptive field - case class DefinesBlock(xs: List[(String, Int)]) extends AsmBlock + case class DefinesBlock(comment: Option[String], xs: List[(String, Int)]) extends AsmBlock /** * Renders as a labeled subroutine @@ -70,16 +69,19 @@ object AsmBlock: case CommentBlock(ys) => ys.map(toComment) - case DefinesBlock(kvs) => + case DefinesBlock(oComment, kvs) => val maximumLength = kvs .map(_._1.length) .max - kvs - .map { case (k, v) => - String.format(s"define %-${maximumLength}s ${toHex(v)}", k) - } + val defines = + kvs + .map { case (k, v) => + String.format(s"define %-${maximumLength}s ${toHex(v)}", k) + } + + oComment.map(toComment).toList ::: defines case NamedCodeBlock(label, oComment, intents) => val headerParagraph = diff --git a/firepower-demo/src/main/scala/com/htmlism/firepower/demo/Definable.scala b/firepower-demo/src/main/scala/com/htmlism/firepower/demo/Definable.scala index 3800dc9..dc2f893 100644 --- a/firepower-demo/src/main/scala/com/htmlism/firepower/demo/Definable.scala +++ b/firepower-demo/src/main/scala/com/htmlism/firepower/demo/Definable.scala @@ -1,9 +1,7 @@ package com.htmlism.firepower.demo -import scala.collection.immutable._ - trait Definable[A]: - def table(x: A): ListMap[String, Int] + def table(x: A): Definable.Table extension (x: A) def toComment: String @@ -14,5 +12,7 @@ trait Definable[A]: extension (x: A) def toDefineWithMath: String object Definable: + case class Table(description: String, xs: List[(String, Int)]) + def apply[A](using ev: Definable[A]): Definable[A] = ev diff --git a/firepower-demo/src/main/scala/com/htmlism/firepower/demo/Easy6502.scala b/firepower-demo/src/main/scala/com/htmlism/firepower/demo/Easy6502.scala index 2bec7ba..e5ab763 100644 --- a/firepower-demo/src/main/scala/com/htmlism/firepower/demo/Easy6502.scala +++ b/firepower-demo/src/main/scala/com/htmlism/firepower/demo/Easy6502.scala @@ -1,6 +1,5 @@ package com.htmlism.firepower.demo -import scala.collection.immutable._ import scala.util.chaining._ object Easy6502: @@ -10,14 +9,15 @@ object Easy6502: object Color: given Definable[Color] with - def table(x: Color): ListMap[String, Int] = + def table(x: Color): Definable.Table = Color .values .iterator .map { c => "COLOR_" + c.toString -> c.ordinal } - .pipe(ListMap.from) + .toList + .pipe(Definable.Table("Colors that each screen pixel can be set to", _)) extension (x: Color) def toComment: String = @@ -44,8 +44,9 @@ object Easy6502: object Pixel: given Definable[Pixel] with - def table(x: Pixel): ListMap[String, Int] = - ListMap("SCREEN" -> x.baseAddr) // define table needs to be a function of an instance + def table(x: Pixel): Definable.Table = + // define table needs to be a function of an instance + Definable.Table("The screen as a collection of pixels", List("SCREEN" -> x.baseAddr)) extension (x: Pixel) def toComment: String = 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 eb9dff1..da30464 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 @@ -1,6 +1,5 @@ package com.htmlism.firepower.demo -import scala.collection.immutable._ import scala.util.chaining._ import cats.syntax.all._ @@ -10,7 +9,7 @@ import com.htmlism.firepower.core._ object PrintThree: case class Move[A: Definable, B: Definable](src: A, dest: B): - def defines: List[ListMap[String, Int]] = + def defines: List[Definable.Table] = List( Definable[A] .table(src), @@ -42,10 +41,8 @@ object PrintThree: program .flatMap(_.defines) .distinct - .map { xs => - xs - .toList - .pipe(AsmBlock.DefinesBlock(_)) + .map { dt => + AsmBlock.DefinesBlock(dt.description.some, dt.xs) } private def codes(opts: AssemblerOptions.DefinitionsMode) =