Commit 16a5ec53 authored by Sapphie's avatar Sapphie
Browse files

Implement If translation

parent 2d44c866
......@@ -12,6 +12,49 @@ object CL3ToCPSTranslator extends (S.Tree => C.Tree) {
def transform(tree: S.Tree)(ctx: C.Atom => C.Tree): C.Tree = {
tree match {
case S.Lit(v) => ctx(C.AtomL(v))
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)))
// 'Else' continuation name and body
val elseCNme = Symbol.fresh("ce")
val elseCBody = transform(thn)(v3 => C.AppC(ctxCNme, Seq(v3)))
???
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)
}
}
}
def transformTestPrimitive( p: L3TestPrimitive,
primArgs: Seq[S.Tree],
thenC: C.Name,
elseC: C.Name): C.Tree = {
val start: C.Tree = C.If(p, Seq(), thenC, elseC)
primArgs.foldLeft(start){
case (C.If(_, s, _, _), e) =>
transform(e)(v => {
C.If(p, s :+ v, thenC, elseC)
})
}
}
}
Supports Markdown
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