output ints

This commit is contained in:
Mark Canlas 2022-11-20 02:53:29 -05:00
parent a13a366b2c
commit 6345d6c0fd
6 changed files with 36 additions and 12 deletions

View File

@ -0,0 +1,3 @@
package com.htmlism.scratchpad
trait Companion[A]:
def canon: A

View File

@ -1,9 +1,12 @@
package com.htmlism.scratchpad
object Encoded:
trait Byte[A]
trait Byte[A]:
def int(x: A): Int
object Byte:
given Encoded.Byte[Int] with {}
given Encoded.Byte[Int] with
def int(x: Int): Int =
x
trait Word[A]

View File

@ -0,0 +1,10 @@
package com.htmlism.scratchpad
trait GrantsWriteLeases[A]:
def withWriteLease[B](f: WriteLease[A] => B)(using A: Companion[A]): B =
val lease =
new WriteLease[A]:
def canon: A =
A.canon
f(lease)

View File

@ -10,6 +10,6 @@ object Load:
def constY[B: Encoded.Byte](x: B): Asm1[Reg.Y] =
Const(x)
case class Const[R, A: Encoded.Byte](x: A)(using R: Register[R]) extends Asm1[R]:
case class Const[R, A](x: A)(using R: Register[R], A: Encoded.Byte[A]) extends Asm1[R]:
def xs: List[String] =
List(R.load)
List(s"${R.load} ${A.int(x)}")

View File

@ -12,4 +12,4 @@ object Store:
case class Byte[R, A](dest: WriteLease.ByteAddress[A])(using R: Register[R]) extends Asm2[R, A]:
def xs: List[String] =
List(R.store)
List(R.store + " " + dest.address)

View File

@ -18,16 +18,24 @@ object ComplicatedResourceSuite extends FunSuite:
class PlayerOne extends Player[PlayerOne](40)
object PlayerOne extends PlayerOne:
val lease: WriteLease[PlayerOne] =
FastLease(PlayerOne)
object PlayerOne extends PlayerOne with GrantsWriteLeases[PlayerOne]:
given Companion[PlayerOne] with
def canon: PlayerOne =
PlayerOne
class PlayerTwo extends Player[PlayerTwo](80)
object PlayerTwo extends PlayerTwo:
val lease: WriteLease[PlayerTwo] =
FastLease(PlayerTwo)
object PlayerTwo extends PlayerTwo with GrantsWriteLeases[PlayerTwo]:
given Companion[PlayerTwo] with
def canon: PlayerTwo =
PlayerTwo
test("use write lease") {
expect.eql(List("LDA", "STA"), PlayerOne.setHead(0)(using PlayerOne.lease).xs)
val asm =
PlayerOne
.withWriteLease { implicit w =>
PlayerOne.setHead(23).xs
}
expect.eql(List("LDA 23", "STA 40"), asm)
}