From 08bc50f1376de8afeb2f70dbef528799946112b4 Mon Sep 17 00:00:00 2001 From: Mark Canlas Date: Mon, 3 Oct 2022 01:28:18 -0400 Subject: [PATCH] as instruction --- .../scala/com/htmlism/scratchpad/Load.scala | 27 +++++++++++++++++-- .../com/htmlism/scratchpad/Register.scala | 15 +++-------- .../htmlism/scratchpad/syntax/package.scala | 2 +- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/scratchpad/src/main/scala/com/htmlism/scratchpad/Load.scala b/scratchpad/src/main/scala/com/htmlism/scratchpad/Load.scala index 8b5cf97..7f37bd4 100644 --- a/scratchpad/src/main/scala/com/htmlism/scratchpad/Load.scala +++ b/scratchpad/src/main/scala/com/htmlism/scratchpad/Load.scala @@ -3,11 +3,34 @@ package com.htmlism.scratchpad trait Load[A]: // TODO genericize to take in encoder // from constant - def loadInt: String + def instruction: String + + def const: Asm1[A] = + Asm1(List(instruction)) // from register - def from: String + def from[B <: Address: ReadAddress]: Asm2[A, B] = + Asm2(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) + +case class Asm2[A, B](xs: List[String]): + def andThen(that: Asm2[A, B]): Asm2[2, B] = + Asm2(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) + +object Asm2: + def from[A, B](t2: (R[A], R[B]), xs: List[String]) = + Asm2[A, B](xs) + +case class R[A]() diff --git a/scratchpad/src/main/scala/com/htmlism/scratchpad/Register.scala b/scratchpad/src/main/scala/com/htmlism/scratchpad/Register.scala index 4ebb17b..495dfba 100644 --- a/scratchpad/src/main/scala/com/htmlism/scratchpad/Register.scala +++ b/scratchpad/src/main/scala/com/htmlism/scratchpad/Register.scala @@ -15,10 +15,7 @@ object Register: object A: given loadA: Load[A] with - def loadInt: String = - "LDA" - - def from: String = + def instruction: String = "LDA" given storeA: Store[A] with @@ -29,10 +26,7 @@ object Register: object X: given loadX: Load[X] with - def loadInt: String = - "LDX" - - def from: String = + def instruction: String = "LDX" given storeX: Store[X] with @@ -43,10 +37,7 @@ object Register: object Y: given loadY: Load[Y] with - def loadInt: String = - "LDY" - - def from: String = + def instruction: String = "LDY" given storeY: Store[Y] with diff --git a/scratchpad/src/main/scala/com/htmlism/scratchpad/syntax/package.scala b/scratchpad/src/main/scala/com/htmlism/scratchpad/syntax/package.scala index 663bf52..6ccad19 100644 --- a/scratchpad/src/main/scala/com/htmlism/scratchpad/syntax/package.scala +++ b/scratchpad/src/main/scala/com/htmlism/scratchpad/syntax/package.scala @@ -14,7 +14,7 @@ package object syntax: summon[Register[C]].self val loadInstruction = - Load[C].loadInt // TODO load action needs to interact with encoder + Load[C].instruction // TODO load action needs to interact with encoder val storeInstruction = Store[C].to // TODO store action needs to interact with encoder