Commit 284fa4ce authored by Sapphie's avatar Sapphie
Browse files

Implement add primitive

parent 68ff58c4
package l3
object CPSValueRepresenter extends (Any => Nothing) {
def apply(tree: Any): Nothing =
???
import l3.{SymbolicCPSTreeModule => H}
import l3.{SymbolicCPSTreeModuleLow => L}
import l3.{L3Primitive => L3}
import l3.{CPSValuePrimitive => CPS}
import l3.{CPSTestPrimitive => CPST}
object CPSValueRepresenter extends (H.Tree => L.Tree) {
def apply(tree: H.Tree): L.Tree = tree match {
case H.LetP(n, L3.IntAdd, Seq(x, y), body) =>
tempLetP(CPS.Sub, Seq(Left(x), Right(1))) { x1 =>
L.LetP(n, CPS.Add, Seq(x1, rewrite(y)), apply(body))
}
}
// Creates an outer LetP, and binds the result to a name,
// then passes the name to mkBody
// Works similarly to transform in the CL3->CPS translation
// Does *not* tag the integers given as arguments
private def tempLetP(p: CPS, args: Seq[Either[H.Atom, Int]]) (mkBody: L.Atom => L.LetP): L.LetP = {
val lArgs = args.map {
case Left(atom) => rewrite(atom)
case Right(int) => L.AtomL(int)
}
val tmpName = Symbol.fresh("x")
val innerLetP = mkBody(L.AtomN(tmpName))
L.LetP(tmpName, p, lArgs, innerLetP)
}
private def rewrite(a: H.Atom): L.Atom = a match {
case H.AtomN(n) => L.AtomN(n)
case H.AtomL(IntLit(i)) => L.AtomL((i.toInt << 1) | 0x1)
case H.AtomL(CharLit(c)) => L.AtomL((c.toInt << 3) | 0x6) // 110
case H.AtomL(BooleanLit(b)) => if (b)
L.AtomL(0x1a) // 11010
else
L.AtomL(0x0a) // 01010
case H.AtomL(UnitLit) => L.AtomL(2)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment