labels are derived

This commit is contained in:
Mark Canlas 2020-08-15 17:19:36 -04:00
parent 973172f90e
commit 6d5da43684
4 changed files with 53 additions and 32 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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))

View File

@ -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
}
}