syntax traits

This commit is contained in:
Mark Canlas 2020-08-16 02:28:37 -04:00
parent 31d56060c5
commit 3f61977efb
5 changed files with 123 additions and 111 deletions

View File

@ -60,4 +60,4 @@ case class Label(s: String) extends Statement {
def toTriplet: (String, Option[String], Option[String]) =
(s, None, None)
}
}

View File

@ -1,6 +1,6 @@
package com.htmlism.mos6502
package object dsl {
package object dsl extends syntax.DefinitionGroupSyntax with syntax.AsmDocSyntax with syntax.AsmSyntax {
def asmDoc(f: AsmDocumentContext => Unit): AsmDocument = {
val ctx: AsmDocumentContext =
new AsmDocumentContext
@ -10,115 +10,6 @@ package object dsl {
ctx.toDoc
}
def asm(f: AssemblyContext => Unit)(implicit ctx: AsmDocumentContext): Unit = {
val asmCtx: AssemblyContext =
new AssemblyContext
f(asmCtx)
ctx
.push(asmCtx.toFragment)
}
def label(s: String)(implicit ctx: AssemblyContext): Unit =
ctx
.push(s)
def group[A](s: String)(f: DefinitionGroupContext => A)(implicit ctx: AsmDocumentContext): A = {
val g: DefinitionGroupContext =
new DefinitionGroupContext
val ret =
f(g)
ctx
.push(g.toGroup(s))
ret
}
def enum[A](implicit ctx: AsmDocumentContext, ev: EnumAsm[A]): Unit = {
val xs =
ev.all
.map(ev.label)
.toList
.zip(List.iterate(0, ev.all.size)(_ + 1))
val grp =
DefinitionGroup(
ev.comment,
xs
.map {
case (s, n) =>
Definition(s, n)
}
)
ctx
.push(grp)
}
def bitField[A](implicit ctx: AsmDocumentContext, ev: BitField[A]): Unit = {
val xs =
ev.all
.map(ev.label)
.toList
.zip(List.iterate(1, ev.all.size)(_ << 1))
val grp =
DefinitionGroup(
ev.comment,
xs
.map {
case (s, n) =>
Definition(s, n)
}
)
ctx
.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)
ctx
.push(Definition(name, x))
definition
}
def constant(name: String, x: Int)(implicit ctx: DefinitionGroupContext): Definition[Int] = {
val definition =
Definition(name, x)
ctx
.push(Definition(name, x))
definition
}
implicit class AddressOps(n: Int) {
def z: ZeroAddress =
ZeroAddress(n)

View File

@ -0,0 +1,89 @@
package com.htmlism.mos6502.dsl
package syntax
trait AsmDocSyntax {
def asm(f: AssemblyContext => Unit)(implicit ctx: AsmDocumentContext): Unit = {
val asmCtx: AssemblyContext =
new AssemblyContext
f(asmCtx)
ctx
.push(asmCtx.toFragment)
}
def group[A](s: String)(f: DefinitionGroupContext => A)(implicit ctx: AsmDocumentContext): A = {
val g: DefinitionGroupContext =
new DefinitionGroupContext
val ret =
f(g)
ctx
.push(g.toGroup(s))
ret
}
def enum[A](implicit ctx: AsmDocumentContext, ev: EnumAsm[A]): Unit = {
val xs =
ev.all
.map(ev.label)
.toList
.zip(List.iterate(0, ev.all.size)(_ + 1))
val grp =
DefinitionGroup(
ev.comment,
xs
.map {
case (s, n) =>
Definition(s, n)
}
)
ctx
.push(grp)
}
def bitField[A](implicit ctx: AsmDocumentContext, ev: BitField[A]): Unit = {
val xs =
ev.all
.map(ev.label)
.toList
.zip(List.iterate(1, ev.all.size)(_ << 1))
val grp =
DefinitionGroup(
ev.comment,
xs
.map {
case (s, n) =>
Definition(s, n)
}
)
ctx
.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)
}
}

View File

@ -0,0 +1,8 @@
package com.htmlism.mos6502.dsl
package syntax
trait AsmSyntax {
def label(s: String)(implicit ctx: AssemblyContext): Unit =
ctx
.push(s)
}

View File

@ -0,0 +1,24 @@
package com.htmlism.mos6502.dsl
package syntax
trait DefinitionGroupSyntax {
def define[A <: Address: Operand](name: String, x: A)(implicit ctx: DefinitionGroupContext): Definition[A] = {
val definition =
Definition(name, x)
ctx
.push(Definition(name, x))
definition
}
def constant(name: String, x: Int)(implicit ctx: DefinitionGroupContext): Definition[Int] = {
val definition =
Definition(name, x)
ctx
.push(Definition(name, x))
definition
}
}