diff --git a/scratchpad/src/main/scala/com/htmlism/scratchpad/Companion.scala b/scratchpad/src/main/scala/com/htmlism/scratchpad/Companion.scala new file mode 100644 index 0000000..fde02fd --- /dev/null +++ b/scratchpad/src/main/scala/com/htmlism/scratchpad/Companion.scala @@ -0,0 +1,3 @@ +package com.htmlism.scratchpad +trait Companion[A]: + def canon: A diff --git a/scratchpad/src/main/scala/com/htmlism/scratchpad/Encoded.scala b/scratchpad/src/main/scala/com/htmlism/scratchpad/Encoded.scala index aaceab1..77b2e0b 100644 --- a/scratchpad/src/main/scala/com/htmlism/scratchpad/Encoded.scala +++ b/scratchpad/src/main/scala/com/htmlism/scratchpad/Encoded.scala @@ -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] diff --git a/scratchpad/src/main/scala/com/htmlism/scratchpad/GrantsWriteLeases.scala b/scratchpad/src/main/scala/com/htmlism/scratchpad/GrantsWriteLeases.scala new file mode 100644 index 0000000..9c50df6 --- /dev/null +++ b/scratchpad/src/main/scala/com/htmlism/scratchpad/GrantsWriteLeases.scala @@ -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) diff --git a/scratchpad/src/main/scala/com/htmlism/scratchpad/Load.scala b/scratchpad/src/main/scala/com/htmlism/scratchpad/Load.scala index 5019796..b3cfe39 100644 --- a/scratchpad/src/main/scala/com/htmlism/scratchpad/Load.scala +++ b/scratchpad/src/main/scala/com/htmlism/scratchpad/Load.scala @@ -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)}") diff --git a/scratchpad/src/main/scala/com/htmlism/scratchpad/Store.scala b/scratchpad/src/main/scala/com/htmlism/scratchpad/Store.scala index 4ef95cf..a8da6b9 100644 --- a/scratchpad/src/main/scala/com/htmlism/scratchpad/Store.scala +++ b/scratchpad/src/main/scala/com/htmlism/scratchpad/Store.scala @@ -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) diff --git a/scratchpad/src/test/scala/com/htmlism/scratchpad/ComplicatedResourceSuite.scala b/scratchpad/src/test/scala/com/htmlism/scratchpad/ComplicatedResourceSuite.scala index 0b15016..c004aa5 100644 --- a/scratchpad/src/test/scala/com/htmlism/scratchpad/ComplicatedResourceSuite.scala +++ b/scratchpad/src/test/scala/com/htmlism/scratchpad/ComplicatedResourceSuite.scala @@ -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) }