Commit 239ea6e7 authored by Sapphie's avatar Sapphie
Browse files

Implement freeVars function

parent c93fbf93
......@@ -56,7 +56,32 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) {
}
}
private def freeVars(e: H.Tree): Set[Symbol] = e match {
case H.LetP(n, prim, args, body) =>
val argsFV = freeVarsAtomSeq(args)
(freeVars(body) - n) ++ argsFV
case H.LetC(cnts, body) =>
freeVars(body) ++ cnts.map(freeVarsCont).reduce(_ ++ _)
case H.LetF(funs, body) =>
val funsFreeVars = funs.map(freeVarsFun).reduce(_ ++ _)
(freeVars(body) ++ funsFreeVars) -- funs.map(_.name)
case H.AppC(cnt, args) =>
freeVarsAtomSeq(args)
case H.AppF(fun, retC, args) =>
fun.asName.toSet ++ freeVarsAtomSeq(args)
case H.If(_, args, _, _) =>
freeVarsAtomSeq(args)
case H.Halt(arg) => arg.asName.toSet
}
private def freeVarsAtomSeq(a: Seq[H.Atom]): Set[Symbol] =
a.map(_.asName).filter(_.isDefined).map(_.get).toSet
private def freeVarsCont(cnt: H.Cnt): Set[Symbol] =
freeVars(cnt.body) -- cnt.args
private def freeVarsFun(fun: H.Fun): Set[Symbol] =
freeVars(fun.body) -- fun.args
private def getMaskR(numBits: Int): Either[H.Atom, L.Atom] = Right(L.AtomL((1 << numBits) -1))
......
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