Skip to content
Snippets Groups Projects
Commit 04502edc authored by Luca Bataillard's avatar Luca Bataillard
Browse files

fix if translation, all tests pass

parent 212fc8c1
No related branches found
No related tags found
No related merge requests found
......@@ -57,10 +57,10 @@ object CL3ToCPSTranslator extends (S.Tree => C.Tree) {
}
// Builds a CPS If out of its arguments
def transformTestPrim( p: L3TestPrimitive,
primArgs: Seq[S.Tree],
thenC: C.Name,
elseC: C.Name): C.Tree = {
def transformTestPrim(p: L3TestPrimitive,
primArgs: Seq[S.Tree],
thenC: C.Name,
elseC: C.Name): C.Tree = {
def transformArgs(args: Seq[S.Tree])(atoms: Seq[C.Atom]): C.Tree = args match {
case Nil => C.If(p, atoms, thenC, elseC)
case x::xs =>
......@@ -77,44 +77,30 @@ object CL3ToCPSTranslator extends (S.Tree => C.Tree) {
case ((name, expr), tree) => transform(expr)(v => C.LetP(name, L3.Id, Seq(v), tree))
}
case S.If(cnd, thn, els) => cnd match {
case S.Prim(p: L3TestPrimitive, primArgs) =>
// Context continuation name
val ctxCNme = Symbol.fresh("c")
val ctxCId = C.AtomN(ctxCNme)
// Context continuation arg
val ctxCArg = Symbol.fresh("a")
// Context continuation body
// This is simply what we already had
val ctxCBody = ctx(C.AtomN(ctxCArg))
// Context continuation
val ctxC = C.Cnt(ctxCNme, Seq(ctxCArg), ctxCBody)
// 'Then' continuation name and body
val thenCNme = Symbol.fresh("ct")
val thenCBody = transform(thn)(v2 => C.AppC(ctxCNme, Seq(v2)))
val thenC = C.Cnt(thenCNme, Seq(), thenCBody)
// 'Else' continuation name and body
val elseCNme = Symbol.fresh("ce")
val elseCBody = transform(thn)(v3 => C.AppC(ctxCNme, Seq(v3)))
val elseC = C.Cnt(elseCNme, Seq(), elseCBody)
val letBody = transformTestPrim(p, primArgs, thenCNme, elseCNme)
C.LetC(Seq(ctxC),
C.LetC(Seq(thenC),
C.LetC(Seq(elseC), letBody)
)
)
case e1 =>
val pArgs = S.Lit(BooleanLit(false))(cnd.pos)
val newCnd = S.Prim(L3.Eq, Seq(e1, pArgs))(cnd.pos)
val newIf = S.If(newCnd, els, thn)(cnd.pos)
transform(newIf)(ctx)
case S.If(S.Prim(p: L3TestPrimitive, primArgs), thn, els) => {
val retCntName = Symbol.fresh("c")
val retArgName = Symbol.fresh("r")
val retCnt = C.Cnt(retCntName, Seq(retArgName), ctx(C.AtomN(retArgName)))
val thnCntName = Symbol.fresh("ct")
val thnCntBody = transform(thn)(v => C.AppC(retCntName, Seq(v)))
val thnCnt = C.Cnt(thnCntName, Seq(), thnCntBody)
val elsCntName = Symbol.fresh("cf")
val elsCntBody = transform(els)(v => C.AppC(retCntName, Seq(v)))
val elsCnt = C.Cnt(elsCntName, Seq(), elsCntBody)
val ifBody = transformTestPrim(p, primArgs, thnCntName, elsCntName)
C.LetC(Seq(retCnt), C.LetC(Seq(thnCnt), C.LetC(Seq(elsCnt), ifBody)))
}
case S.If(cond, thn, els) => {
implicit val pos = tree.pos
val primCond = S.Prim(L3.Eq, Seq(cond, S.Lit(BooleanLit(false))))
transform(S.If(primCond, els, thn))(ctx)
}
case S.App(fun, args) => transformApp(fun, args)
case S.LetRec(funs, body) => transformLetRec(funs, body)
case S.Prim(p, args) => p match {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment