definitions can emit many

This commit is contained in:
Mark Canlas 2020-08-26 02:15:40 -04:00
parent dd0cfbf445
commit 87213fd0a4
4 changed files with 18 additions and 8 deletions

View File

@ -65,7 +65,9 @@ class DefinitionGroupContext {
ListBuffer()
def push[A](x: A)(implicit ev: Definable[A]): Unit =
xs.append(ev.toDefinition(x))
ev
.toDefinitions(x)
.foreach(xs.append)
def toGroup(s: String): DefinitionGroup =
DefinitionGroup(s, xs.toList)
@ -80,8 +82,8 @@ case class Definition[A: Operand](name: String, x: A) {
object Definition {
implicit def definitionDefinable[A]: Definable[Definition[A]] =
new Definable[Definition[A]] {
def toDefinition(x: Definition[A]) =
x
def toDefinitions(x: Definition[A]): List[Definition[_]] =
List(x)
}
}

View File

@ -1,5 +1,9 @@
package com.htmlism.mos6502.dsl
trait Definable[A] {
def toDefinition(x: A): Definition[_]
/**
* A `Definable` can emit multiple definitions. Usually in the case of `word`s being split across two byte-definitions
*/
def toDefinitions(x: A): List[Definition[_]]
}

View File

@ -22,7 +22,9 @@ case class ReadWriteLocation[A](name: String, address: Address) {
object ReadWriteLocation {
implicit def readWriteLocationDefinable[A]: Definable[ReadWriteLocation[A]] =
new Definable[ReadWriteLocation[A]] {
def toDefinition(x: ReadWriteLocation[A]): Definition[ZeroAddress] =
Definition(x.name, 0x00.z)
def toDefinitions(x: ReadWriteLocation[A]): List[Definition[ZeroAddress]] =
List {
Definition(x.name, 0x00.z)
}
}
}

View File

@ -19,7 +19,9 @@ case class VolatileDevice[A](name: String, address: Address) {
object VolatileDevice {
implicit def volatileDeviceDefinable[A]: Definable[VolatileDevice[A]] =
new Definable[VolatileDevice[A]] {
def toDefinition(x: VolatileDevice[A]): Definition[ZeroAddress] =
Definition(x.name, 0x00.z)
def toDefinitions(x: VolatileDevice[A]): List[Definition[ZeroAddress]] =
List {
Definition(x.name, 0x00.z)
}
}
}