Commit 91f52f3c authored by Sapphie's avatar Sapphie
Browse files

Implement Substitution

parent b149a8dc
......@@ -46,7 +46,7 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) {
// Creates a letP
def argsBindings(freeVars: Seq[Symbol], counter: Int, accSubst: Map[Symbol, Symbol]): L.Tree = freeVars match {
case Nil =>
substitute(accSubst)(funBody)
substitute(funBody)(accSubst)
case freeVar :: vs =>
// Bind the fresh variable v to a block get
val v = Symbol.fresh("v")
......@@ -87,7 +87,43 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) {
L.LetF(funsAndVars.unzip3._1, closureAllocInit)
}
def substitute(subst: Map[Symbol, Symbol])(tree: L.Tree): L.Tree = ???
// Substitutes _free_ variables in `tree`
// meaning that `subst` should only contain variables
// that are free in `tree`
def substitute(tree: L.Tree)(implicit subst: Map[Symbol, Symbol]): L.Tree = {
def subtituteArgs(args: Seq[L.Atom]): Seq[L.Atom] = args.map(substituteAtom)
def substituteAtom(atom: L.Atom) = atom match {
case L.AtomL(_) => atom
case L.AtomN(n) => L.AtomN(subst.getOrElse(n,n))
}
tree match {
case L.LetP(name, prim, args, body) =>
val newArgs = subtituteArgs(args)
val newBody = substitute(body)
L.LetP(name, prim, newArgs, newBody)
case L.AppC(cnt, args) =>
L.AppC(cnt, subtituteArgs(args))
case L.AppF(fun, retC, args) =>
L.AppF(substituteAtom(fun), retC, subtituteArgs(args))
case L.Halt(arg) => L.Halt(substituteAtom(arg))
case L.If(cond, args, thenC, elseC) =>
L.If(cond, subtituteArgs(args), thenC, elseC)
case L.LetC(cnts, body) =>
val newCnts = cnts.map { cnt =>
L.Cnt(cnt.name, cnt.args, substitute(cnt.body))
}
val newBody = substitute(body)
L.LetC(newCnts, newBody)
case L.LetF(funs, body) =>
val newFuns = funs.map {fun =>
L.Fun(fun.name, fun.retC, fun.args, substitute(fun.body))
}
val newBody = substitute(body)
L.LetF(newFuns, newBody)
}
}
private def transformIf(cond: L3TestPrimitive, args: Seq[H.Atom], thenC: H.Name, elseC: H.Name): L.Tree = {
def maskAndCheck(numBits: Int, target: Bits32): L.LetP = {
......
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