vaguely start using traits

This commit is contained in:
Mark Canlas 2022-10-12 12:48:42 -04:00
parent f1ca97f00b
commit 8ed8a0f7d2
2 changed files with 22 additions and 16 deletions

View File

@ -5,32 +5,38 @@ trait Load[A]:
// from constant
def instruction: String
def const: Asm1[A] =
Asm1(List(instruction))
def const: Asm1Instructions[A] =
Asm1Instructions(List(instruction))
// from register
def from[B <: Address: ReadAddress]: Asm2[A, B] =
Asm2(List(instruction))
def from[B <: Address: ReadAddress]: Asm2Instructions[A, B] =
Asm2Instructions(List(instruction))
object Load:
def apply[A: Load]: Load[A] =
summon[Load[A]]
case class Asm1[A](xs: List[String]):
def and[B]: Asm2[A, B] =
Asm2(xs)
trait Asm1[A]:
def xs: List[String]
case class Asm2[A, B](xs: List[String]):
def andThen(that: Asm2[A, B]): Asm2[2, B] =
Asm2(xs ++ that.xs)
case class Asm1Instructions[A](xs: List[String]) extends Asm1[A]:
def and[B]: Asm2Instructions[A, B] =
Asm2Instructions(xs)
trait Asm2[A, B]:
def xs: List[String]
case class Asm2Instructions[A, B](xs: List[String]) extends Asm2[A, B]:
def andThen(that: Asm2Instructions[A, B]): Asm2Instructions[2, B] =
Asm2Instructions(xs ++ that.xs)
// TODO not tested
// B type needs to be a class type, with evidence, not a case class
def swap[AA, BB](f: (R[A], R[B]) => (R[AA], R[BB])): Asm2[AA, BB] =
Asm2.from(f(R[A](), R[B]()), xs)
def swap[AA, BB](f: (R[A], R[B]) => (R[AA], R[BB])): Asm2Instructions[AA, BB] =
Asm2Instructions.from(f(R[A](), R[B]()), xs)
object Asm2:
object Asm2Instructions:
def from[A, B](t2: (R[A], R[B]), xs: List[String]) =
Asm2[A, B](xs)
Asm2Instructions[A, B](xs)
case class R[A]()

View File

@ -5,7 +5,7 @@ package object syntax:
def writeConst[A: Loadable](x: A): syntax.PartiallyAppliedWrite[A, Addr] =
new syntax.PartiallyAppliedWrite(reg, x)
def writeFrom[R: Store]: Asm2[R, Addr] =
def writeFrom[R: Store]: Asm2Instructions[R, Addr] =
val storeInstruction =
Store[R].to
@ -14,7 +14,7 @@ package object syntax:
// TODO encoding now already makes the structures lose semantic meaning
// TODO maybe AsmN's should be traits such that semantic structures can just obey the contracts and be AND'ed
Asm2(List(s"$storeInstruction ${reg.n.toString}"))
Asm2Instructions(List(s"$storeInstruction ${reg.n.toString}"))
class PartiallyAppliedWrite[Addr: Loadable, A](reg: WriteAddress[A], x: Addr):
def apply[R: Load: Store: Register]: String =