def rewrite_cps(self, cont): fst, snd, trd = self.fst, self.snd, self.trd if isinstance(cont, Var): if fst.is_cpsatom(): return If(fst.to_cpsatom(), snd.rewrite_cps(cont), trd.rewrite_cps(cont)) else: fstval = newvar('$PredRv_') fstcont = Lambda( [fstval], [If(fstval, snd.rewrite_cps(cont), trd.rewrite_cps(cont))]) fstcont.name = gensym('$PredCont_').sval return fst.rewrite_cps(fstcont) else: contval = newvar('$Cont_') contdef = Def(contval, cont) if fst.is_cpsatom(): return Seq([ contdef, If(fst.to_cpsatom(), snd.rewrite_cps(contval), trd.rewrite_cps(contval)) ]) else: fstval = newvar('$PredRv_') fstcont = Lambda([fstval], [ If(fstval, snd.rewrite_cps(contval), trd.rewrite_cps(contval)) ]) fstcont.name = gensym('$PredCont_').sval return Seq([contdef, fst.rewrite_cps(fstcont)])
def rewrite_cps(self, cont): formrv = newvar('$FormRv_') newdef = Def(self.name, formrv) newdef.toplevel = self.toplevel formcont = Lambda([formrv], [Apply(cont, [newdef])]) formcont.name = gensym('$FormCont_').sval return self.form.rewrite_cps(formcont)
def rewrite_cps(self, cont): proc, args = self.proc, self.args if proc.is_cpsatom(): proc = proc.to_cpsatom() else: procrv = newvar('$ProcRv_') proccont = Lambda([procrv], [Apply(procrv, args).rewrite_cps(cont)]) proccont.name = gensym('$ProcCont_').sval return proc.rewrite_cps(proccont) atom_args = [] for i in xrange(len(args)): # XXX pypy hack arg = args[i] if not arg.is_cpsatom(): argrv = newvar('$ArgRv_') newapply = Apply(proc, args[:i] + [argrv] + args[i + 1:]) argcont = Lambda([argrv], [newapply.rewrite_cps(cont)]) argcont.name = gensym('$ArgCont_').sval return arg.rewrite_cps(argcont) else: atom_args.append(arg.to_cpsatom()) return Apply(proc, atom_args + [cont])
def build_lambda(w_form, w_args): w_formals = w_args[0] body_w = w_args[1:] posargs_w, w_rest = w_formals.to_list() if not w_rest.is_null(): raise W_ValueError('variadic argument not supported (yet)', w_form, 'build_lambda()').wrap() argnodes = [None] * len(posargs_w) for i in xrange(len(posargs_w)): w_arg = posargs_w[i] # XXX pypy hack if not isinstance(w_arg, W_Symbol): raise W_TypeError('Symbol', w_arg, 'build_lambda()').wrap() argnodes[i] = Var(w_arg) return Lambda(argnodes, [w_x.to_ast() for w_x in body_w])
def run(self): if len(self.nodelist) == 0: return Const(w_unspec).rewrite_cps(self.lastcont) cont = self.lastcont nodelist_copy = self.nodelist[1:] nodelist_copy.reverse() for node in nodelist_copy: # XXX: maybe we have atomic op here if self.toplevel and isinstance(node, Def): node.toplevel = True ignore = newvar('$Ignore_') cont = Lambda([ignore], [node.rewrite_cps(cont)]) cont.name = gensym('$SeqCont_').sval firstnode = self.nodelist[0] if self.toplevel and isinstance(firstnode, Def): firstnode.toplevel = True self.cpsform = self.nodelist[0].rewrite_cps(cont) return self.cpsform
def to_cpsatom(self): cont = newvar('$LamK_') newbody = Rewriter(self.body, lastcont=cont).run() return Lambda(self.formals + [cont], [newbody], self.name)
def rewrite_cps(self, cont): formrv = newvar('$FormRv_') formcont = Lambda([formrv], [Apply(cont, [Sete(self.name, formrv)])]) formcont.name = gensym('$FormCont_').sval return self.form.rewrite_cps(formcont)