Commit 82cd4b8c authored by Luca Bataillard's avatar Luca Bataillard
Browse files

implement CPS application transform

parent 6c34b416
......@@ -10,6 +10,25 @@ object CL3ToCPSTranslator extends (S.Tree => C.Tree) {
)
def transform(tree: S.Tree)(ctx: C.Atom => C.Tree): C.Tree = {
def transformApp(fun: S.Tree, args: Seq[S.Tree]) = {
// Build return continuation
val cntName = Symbol.fresh("c")
val retName = Symbol.fresh("r")
val returnCnt = C.Cnt(cntName, Seq(retName), ctx(C.AtomN(retName)))
// Transforms each arg successively, accumulate generated atoms. At bottom create letc-apply
// expression. Return resulting tree
def transformArgs(es: Seq[S.Tree])(fId: C.Atom, ids: Seq[C.Atom]): C.Tree = args match {
case Nil => C.LetC(Seq(returnCnt), C.AppF(fId, cntName, ids))
case e :: es => transform(e)(v => transformArgs(es)(fId, ids :+ v))
}
// Transform initial function then start argument transformation
transform(fun)(fId => transformArgs(args)(fId, Seq()))
}
tree match {
case S.Lit(v) => ctx(C.AtomL(v))
case S.If(cnd, thn, els) =>
......@@ -41,9 +60,15 @@ object CL3ToCPSTranslator extends (S.Tree => C.Tree) {
val newIf = S.If(newCnd, els, thn)(cnd.pos)
transform(newIf)(ctx)
}
}
case S.App(fun, args) => transformApp(fun, args)
case _ => ???
}
}
def transformTestPrimitive( p: L3TestPrimitive,
primArgs: Seq[S.Tree],
thenC: C.Name,
......
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