diff --git a/src/main/scala/com/htmlism/mos6502/dsl/BitField.scala b/src/main/scala/com/htmlism/mos6502/dsl/BitField.scala index cf49238..7433938 100644 --- a/src/main/scala/com/htmlism/mos6502/dsl/BitField.scala +++ b/src/main/scala/com/htmlism/mos6502/dsl/BitField.scala @@ -5,15 +5,18 @@ import cats.data.NonEmptyList trait BitField[A] { def comment: String - def labels: NonEmptyList[String] + /** + * An ordered list of every status in this bit field + */ + def all: NonEmptyList[A] /** - * ASM-safe label - */ + * ASM-safe label + */ def label(x: A): String /** - * Comment string - */ + * Comment string + */ def comment(x: A): String } diff --git a/src/main/scala/com/htmlism/mos6502/dsl/EnumAsm.scala b/src/main/scala/com/htmlism/mos6502/dsl/EnumAsm.scala index aca3d0b..3bbb641 100644 --- a/src/main/scala/com/htmlism/mos6502/dsl/EnumAsm.scala +++ b/src/main/scala/com/htmlism/mos6502/dsl/EnumAsm.scala @@ -5,15 +5,18 @@ import cats.data.NonEmptyList trait EnumAsm[A] { def comment: String - def labels: NonEmptyList[String] + /** + * An ordered list of every value in this enumeration + */ + def all: NonEmptyList[A] /** - * ASM-safe label - */ + * ASM-safe label + */ def label(x: A): String /** - * Comment string - */ + * Comment string + */ def comment(x: A): String } diff --git a/src/main/scala/com/htmlism/mos6502/dsl/package.scala b/src/main/scala/com/htmlism/mos6502/dsl/package.scala index e150f2c..19a637d 100644 --- a/src/main/scala/com/htmlism/mos6502/dsl/package.scala +++ b/src/main/scala/com/htmlism/mos6502/dsl/package.scala @@ -25,7 +25,8 @@ package object dsl { def enum[A](implicit ctx: AsmDocumentContext, ev: EnumAsm[A]): Unit = { val (_, xs) = - ev.labels + ev.all + .map(ev.label) .foldLeft(0 -> List.empty[(String, Int)]) { case ((next, acc), s) => (next + 1) -> (acc :+ (s -> next)) @@ -47,7 +48,8 @@ package object dsl { def bitField[A](implicit ctx: AsmDocumentContext, ev: BitField[A]): Unit = { val (_, xs) = - ev.labels + ev.all + .map(ev.label) .foldLeft(1 -> List.empty[(String, Int)]) { case ((next, acc), s) => (next << 1) -> (acc :+ (s -> next)) diff --git a/src/test/scala/com/htmlism/mos6502/dsl/DslSpec.scala b/src/test/scala/com/htmlism/mos6502/dsl/DslSpec.scala index b65ab94..39a3692 100644 --- a/src/test/scala/com/htmlism/mos6502/dsl/DslSpec.scala +++ b/src/test/scala/com/htmlism/mos6502/dsl/DslSpec.scala @@ -68,7 +68,7 @@ class DslSpec extends AnyFlatSpec with should.Matchers { "enum" should "compile" in { val doc = asmDoc { implicit ctx => - enum[Foo] + enum[Triforce] } doc shouldEqual AsmDocument( @@ -88,7 +88,7 @@ class DslSpec extends AnyFlatSpec with should.Matchers { "bit field" should "compile" in { val doc = asmDoc { implicit ctx => - bitField[Foo] + bitField[Direction] } doc shouldEqual AsmDocument( @@ -107,36 +107,49 @@ class DslSpec extends AnyFlatSpec with should.Matchers { } } -class Foo +sealed trait Triforce -object Foo { - implicit val enumFoo: EnumAsm[Foo] = - new EnumAsm[Foo] { +case object Courage extends Triforce +case object Wisdom extends Triforce +case object Power extends Triforce + +object Triforce { + implicit val enumTriforce: EnumAsm[Triforce] = + new EnumAsm[Triforce] { def comment: String = "foo as enum" - def labels: NonEmptyList[String] = - NonEmptyList.of("courage", "wisdom", "power") + def all: NonEmptyList[Triforce] = + NonEmptyList.of(Courage, Wisdom, Power) - def label(x: Foo): String = - "fooNotEnum" + def label(x: Triforce): String = + x.toString.toLowerCase - def comment(x: Foo): String = - "Foo not an enum" + def comment(x: Triforce): String = + x.toString } +} - implicit val bitFieldFoo: BitField[Foo] = - new BitField[Foo] { +sealed trait Direction + +case object Up extends Direction +case object Down extends Direction +case object Left extends Direction +case object Right extends Direction + +object Direction { + implicit val bitFieldDirection: BitField[Direction] = + new BitField[Direction] { def comment: String = "foo as bit field" - def labels: NonEmptyList[String] = - NonEmptyList.of("up", "down", "left", "right") + def all: NonEmptyList[Direction] = + NonEmptyList.of(Up, Down, Left, Right) - def label(x: Foo): String = - "fooNotBitField" + def label(x: Direction): String = + x.toString.toLowerCase - def comment(x: Foo): String = - "Foo not a bit field" + def comment(x: Direction): String = + x.toString } }