move is a composition

This commit is contained in:
Mark Canlas 2022-11-19 23:04:27 -05:00
parent b91df3e1b8
commit c4e8c2a936
9 changed files with 43 additions and 19 deletions

View File

@ -1,4 +0,0 @@
package com.htmlism.scratchpad
// anything can be encoded as a byte
trait ByteLike[A] {}

View File

@ -0,0 +1,6 @@
package com.htmlism.scratchpad
object Encoded:
trait Byte[A]
trait Word[A]

View File

@ -0,0 +1,9 @@
package com.htmlism.scratchpad
object Load:
def const[R: Register, X: Encoded.Byte](x: X): Asm1[R] =
Const(x)
case class Const[R, X: Encoded.Byte](x: X)(using R: Register[R]) extends Asm1[R]:
def xs: List[String] =
List("LD" + R.name)

View File

@ -2,13 +2,14 @@ package com.htmlism.scratchpad
// https://mads.atari8.info/mads_eng.html#_mv
object Move:
def const[R: Register, A: WriteLease, X: ByteLike](x: X): Asm2[R, A] =
// TODO freeze this into a data structure or a composition of data structures of load and store
Asm2Instructions(List(""))
def const[R: Register, A: WriteLease.Byte, X: Encoded.Byte](x: X): Asm2[R, A] =
Load
.const[R, X](x)
.widenWith[A] andThen Store.from[R, A]
def from[R: Register, A: ReadLease, B: WriteLease]: Asm3[R, A, B] =
def from[R: Register, A: ReadLease.Byte, B: WriteLease.Byte]: Asm3[R, A, B] =
Asm3Instructions(List(""))
object Word:
def const[R: Register, A: WriteLease, X: WordLike](x: X): Asm2[R, A] =
def const[R: Register, A: WriteLease.Word, X: Encoded.Word](x: X): Asm2[R, A] =
Asm2Instructions(List(""))

View File

@ -1,3 +1,6 @@
package com.htmlism.scratchpad
trait ReadLease[A] {}
object ReadLease:
trait Byte[A]
trait Word[A]

View File

@ -0,0 +1,9 @@
package com.htmlism.scratchpad
object Store:
def from[R: Register, A: WriteLease.Byte]: Asm2[R, A] =
Byte[R, A]()
case class Byte[R, A]()(using R: Register[R], A: WriteLease.Byte[A]) extends Asm2[R, A]:
def xs: List[String] =
List("ST" + R.name)

View File

@ -1,4 +0,0 @@
package com.htmlism.scratchpad
// anything can be encoded as a word (two bytes)
trait WordLike[A] {}

View File

@ -1,3 +1,6 @@
package com.htmlism.scratchpad
trait WriteLease[A] {}
object WriteLease:
trait Byte[A]
trait Word[A]

View File

@ -3,17 +3,18 @@ package com.htmlism.scratchpad
trait Asm1[A]:
def xs: List[String]
case class Asm1Instructions[A](xs: List[String]) extends Asm1[A]:
def and[B]: Asm2Instructions[A, B] =
def widenWith[B]: Asm2[A, B] =
Asm2Instructions(xs)
case class Asm1Instructions[A](xs: List[String]) extends Asm1[A]
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]): Asm2[A, B] =
def andThen(that: Asm2[A, B]): Asm2[A, B] =
AndThen2(this, that)
case class Asm2Instructions[A, B](xs: List[String]) extends Asm2[A, B]:
// 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])): Asm2Instructions[AA, BB] =