Commit 4ff877f5 authored by Sapphie's avatar Sapphie
Browse files

Restructure code and implement mult primitive representation

parent 6ee6828e
......@@ -8,25 +8,42 @@ 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))
}
case H.LetP(n, L3.IntSub, Seq(x, y), body) =>
tempLetP(CPS.Add, Seq(Left(x), Right(1))) { x1 =>
L.LetP(n, CPS.Sub, Seq(x1, rewrite(y)), apply(body))
}
case H.LetP(n, prim, Seq(x, y), body) => applyLetPBin(n, prim, x, y, body)
case _ => ???
}
// Transforms primitives that take 2 arguments
private def applyLetPBin(n: H.Name, prim: L3, x: H.Atom, y: H.Atom, body: H.Tree): L.LetP = {
val lAtomOne: Either[H.Atom, L.Atom] = Right(L.AtomL(1))
prim match {
case L3.IntAdd =>
tempLetP(CPS.Sub, Seq(Left(x), lAtomOne)) { x1 =>
L.LetP(n, CPS.Add, Seq(x1, rewrite(y)), apply(body))
}
case L3.IntSub =>
tempLetP(CPS.Add, Seq(Left(x), lAtomOne)) { x1 =>
L.LetP(n, CPS.Sub, Seq(x1, rewrite(y)), apply(body))
}
case L3.IntMul =>
tempLetP(CPS.Sub, Seq(Left(x), lAtomOne)) { x1 =>
tempLetP(CPS.ShiftRight, Seq(Left(y), lAtomOne)) { y1 =>
tempLetP(CPS.Mul, Seq(Right(x1), Right(y1))) { z =>
L.LetP(n, CPS.Add, Seq(z, L.AtomL(1)), 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 = {
private def tempLetP(p: CPS, args: Seq[Either[H.Atom, L.Atom]]) (mkBody: L.Atom => L.LetP): L.LetP = {
val lArgs = args.map {
case Left(atom) => rewrite(atom)
case Right(int) => L.AtomL(int)
case Left(hAtom) => rewrite(hAtom)
case Right(lAtom) => lAtom
}
val tmpName = Symbol.fresh("x")
val innerLetP = mkBody(L.AtomN(tmpName))
......
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