mirror of
https://github.com/mcanlas/6502-opcodes.git
synced 2024-11-18 16:08:47 +00:00
added mapping support
This commit is contained in:
parent
00575f46f7
commit
adf0ffc04b
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user