def test_letrec(self): f, k, function = il.Var('f'), il.Var('k'), il.Var('function') result = cps(letrec([(f, lamda((), f()))], f())) expect = il.Clamda(v, il.Assign(f, v), v)(il.Lamda( (k, ), il.Clamda(function, function(k))(f))) eq_(result, expect)
def cps_convert_exps(compiler, exps, cont): v = compiler.new_var(il.ConstLocalVar('v')) if not exps: return il.PassStatement() if len(exps) == 1: return exps[0].cps(compiler, cont) else: return exps[0].cps( compiler, il.Clamda(v, cps_convert_exps(compiler, exps[1:], cont)))
def cps(self, compiler, cont): #see The 90 minute Scheme to C compiler by Marc Feeley args = self.args vars = tuple(compiler.new_var(il.ConstLocalVar('a'+repr(i))) for i in range(len(args))) fun = cont(self.function.function(*vars)) for var, arg in reversed(zip(vars, args)): fun = arg.cps(compiler, il.Clamda(var, fun)) return fun
def cps_call(self, compiler, cont, args): # see The 90 minute Scheme to C compiler by Marc Feeley if self.has_cut(): fun = wrap_cut(self.body).cps(compiler, cont) else: fun = self.body.cps(compiler, cont) params = tuple(x.interlang() for x in self.params) for var, arg in reversed(zip(params, args)): fun = arg.cps(compiler, il.Clamda(var, fun)) return fun
def findall_1(compiler, cont, goal): v = compiler.new_var(il.ConstLocalVar('v')) v2 = compiler.new_var(il.ConstLocalVar('v')) fc = compiler.new_var(il.ConstLocalVar('old_failcont')) return il.begin( il.Assign(fc, il.failcont), il.SetFailCont(il.clamda(v2, il.SetFailCont(fc), cont(v2))), goal.cps(compiler, il.Clamda(v, il.failcont(v))) )
def cps_call(self, compiler, cont, args): # see The 90 minute Scheme to C compiler by Marc Feeley if self.has_cut(): fun = wrap_cut(self.body).cps(compiler, cont) else: fun = self.body.cps(compiler, cont) params = tuple(x.interlang() for x in self.params) for var, arg in reversed(zip(params, args)): k = compiler.new_var(il.ConstLocalVar('cont')) fun = direct_interlang(il.Lamda((k, ), arg.cps(compiler, k)) ).cps(compiler, il.Clamda(var, fun)) return fun
def test_add(self): result = cps(add(1, 2)) expect = il.Clamda(a0, il.Clamda(a1, done()(il.add((a0, a1))))(2))(1) eq_(result, expect)
def test_if(self): result = cps(if_(0, 1, 2)) expect = il.Clamda(v, il.If(v, done()(1), done()(2)))(0) eq_(result, expect)
def test_begin(self): result = cps(begin(1, 2)) expect = il.Clamda(v, done()(2))(1) eq_(result, expect)
def test_let(self): x, y, k = il.Var('x'), il.Var('y'), il.Var('k') result = cps(let(((x, 1), ), x)) expect = il.Clamda(x, done()(x))(1) eq_(result, expect)
def test_lambda_apply2(self): v1 = il.Var('v1') result = optimize(il.Clamda(v1, v1)(v)) expect = v eq_(result, expect)
def if_(compiler, cont, test, then, else_): v = compiler.new_var(il.ConstLocalVar('v')) return test.cps( compiler, il.Clamda( v, il.If(v, then.cps(compiler, cont), else_.cps(compiler, cont))))
def greedy_may(compiler, cont, item): v = compiler.new_var(il.ConstLocalVar('v')) return cps(compiler, item, il.Clamda(v, cont(v)), il.Clamda(v, cont(v)))
def lazy_may(compiler, cont, item): v = compiler.new_var(il.ConstLocalVar('v')) return il.Clamda(v, cont(v, cps(compiler, item, cont)))
def may(compiler, cont, item): v = compiler.new_var(il.ConstLocalVar('v')) return cps(compiler, clause, cont, il.Clamda(v, cont(v)))
def if_p(compiler, cont, condition, action): v = compiler.new_var(il.ConstLocalVar('v')) return condition.cps(compiler, il.Clamda(v, action.cps(compiler, cont)))