definition groups have names

This commit is contained in:
Mark Canlas 2020-08-15 14:44:23 -04:00
parent d3e3d314bb
commit e5d7f30bbc
3 changed files with 18 additions and 9 deletions

View File

@ -19,7 +19,7 @@ sealed trait TopLevelAsmDocumentFragment
sealed trait AsmBlockFragment extends TopLevelAsmDocumentFragment
case class DefineGroup(xs: List[Definition[_]]) extends TopLevelAsmDocumentFragment
case class DefineGroup(comment: String, xs: List[Definition[_]]) extends TopLevelAsmDocumentFragment
class DefineGroupContext {
private val xs: ListBuffer[Definition[_]] =
@ -28,8 +28,8 @@ class DefineGroupContext {
def push(x: Definition[_]): Unit =
xs.append(x)
def toGroup: DefineGroup =
DefineGroup(xs.toList)
def toGroup(s: String): DefineGroup =
DefineGroup(s, xs.toList)
}
case class Definition[A : Operand](name: String, x: A)

View File

@ -11,14 +11,14 @@ package object dsl {
.toDoc
}
def group(f: DefineGroupContext => Unit)(implicit ctx: AsmDocumentContext): Unit = {
def group(s: String)(f: DefineGroupContext => Unit)(implicit ctx: AsmDocumentContext): Unit = {
val g: DefineGroupContext =
new DefineGroupContext
f(g)
ctx
.push(g.toGroup)
.push(g.toGroup(s))
}
def define[A : Operand](name: String, x: A)(implicit ctx: DefineGroupContext): Unit =

View File

@ -8,31 +8,40 @@ class DslSpec extends AnyFlatSpec with should.Matchers {
"the dsl" should "compile" in {
val doc =
asmDoc { implicit ctx =>
group { implicit g =>
group("snake things") { implicit g =>
define("snakeBodyStart", 0x12.z)
define("snakeDirection", 0x02.z)
define("snakeLength", 0x03.z)
}
group { implicit g =>
group("ASCII values of keys controlling the snake") { implicit g =>
define("ASCII_w", 0x77)
define("ASCII_a", 0x61)
define("ASCII_s", 0x73)
define("ASCII_d", 0x64)
}
group("System variables") { implicit g =>
define("sysRandom", 0xfe)
define("sysLastKey", 0xff)
}
}
doc shouldEqual AsmDocument(List(
DefineGroup(List(
DefineGroup("snake things", List(
Definition("snakeBodyStart", 0x12.z),
Definition("snakeDirection", 0x02.z),
Definition("snakeLength", 0x03.z)
)),
DefineGroup(List(
DefineGroup("ASCII values of keys controlling the snake", List(
Definition("ASCII_w", 0x77),
Definition("ASCII_a", 0x61),
Definition("ASCII_s", 0x73),
Definition("ASCII_d", 0x64)
)),
DefineGroup("System variables", List(
Definition("sysRandom", 0xfe),
Definition("sysLastKey", 0xff)
))
))
}