added mapping support

This commit is contained in:
Mark Canlas 2020-08-16 00:08:18 -04:00
parent 00575f46f7
commit adf0ffc04b
2 changed files with 64 additions and 0 deletions

View File

@ -65,6 +65,26 @@ package object dsl {
.push(grp)
}
def mapping[A](implicit ctx: AsmDocumentContext, ev: Mapping[A]): Unit = {
val xs =
ev.all
.map(x => ev.label(x) -> ev.value(x))
.toList
val grp =
DefinitionGroup(
ev.comment,
xs
.map {
case (s, n) =>
Definition(s, n)
}
)
ctx
.push(grp)
}
def define[A <: Address: Operand](name: String, x: A)(implicit ctx: DefinitionGroupContext): Definition[A] = {
val definition =
Definition(name, x)

View File

@ -105,6 +105,27 @@ class DslSpec extends AnyFlatSpec with should.Matchers {
)
)
}
"mapping" should "compile" in {
val doc =
asmDoc { implicit ctx =>
mapping[Direction]
}
doc shouldEqual AsmDocument(
List(
DefinitionGroup(
"foo as a mapping",
List(
Definition("up", 0x77),
Definition("down", 0x61),
Definition("left", 0x73),
Definition("right", 0x64)
)
)
)
)
}
}
sealed trait Triforce
@ -152,4 +173,27 @@ object Direction {
def comment(x: Direction): String =
x.toString
}
implicit val mappingDirection: Mapping[Direction] =
new Mapping[Direction] {
def comment: String =
"foo as a mapping"
def all: NonEmptyList[Direction] =
NonEmptyList.of(Up, Down, Left, Right)
def value(x: Direction): Int =
x match {
case Up => 0x77
case Down => 0x61
case Left => 0x73
case Right => 0x64
}
def label(x: Direction): String =
x.toString.toLowerCase
def comment(x: Direction): String =
x.toString
}
}