def test_letrec_odd_even(self): from util import n, odd, even eq_( eval( letrec([(odd, lamda([n], if_(eq(n, 0), 0, even(sub(n, 1))))), (even, lamda([n], if_(eq(n, 0), 1, odd(sub(n, 1)))))], odd(3))), 1)
def test_letrec_odd_even(self): from util import n odd, even = MacroVar('odd'), MacroVar('even') eq_( eval( letrec([(odd, macro([n], if_(eq(n, 0), 0, even(sub(n, 1))))), (even, macro([n], if_(eq(n, 0), 1, odd(sub(n, 1)))))], odd(3))), 1)
def test_letrec_fac(self): from util import m, n, fac eq_( eval( letrec([ (fac, lamda([n], if_(eq(n, 1), 1, mul(n, fac(sub(n, 1)))))) ], fac(3))), 6)
def testblock3(self): a = Var('a') eq_( eval( block(a, 1, if_(0, continue_block(a), begin(exit_block(a, 2), 3)))), 2)
def test_unwind_protect_loop(self): from util import a, i eq_(eval(let([(i,3)], block(a, assign(i, sub(i, 1)), if_(eq(i, 0), exit_block(a, 1), unwind_protect(continue_block(a), prin(i)))), i)), 0)
def testloop(self): from util import a, i eq_( eval( let([(i, 3)], block(a, assign(i, sub(i, 1)), if_(eq(i, 0), exit_block(a, 1), continue_block(a))), i)), 0)
def testloop(self): from util import a, i eq_(eval(let([(i,3)], block(a, assign(i, sub(i, 1)), if_(eq(i, 0), exit_block(a, 1), continue_block(a))), i)), 0)
def test_letrec_fac(self): from util import m, n fac = MacroVar('fac') eq_( eval( letrec([ (fac, macro([n], if_(eq(n, 1), 1, mul(n, fac(sub(n, 1)))))) ], fac(3))), 6)
def test_macro6(self): n, act, ntimes = Var('n'), Var('act'), MacroVar('ntimes') eq_( eval( letrec([(ntimes, macro((n, act), if_(eq(n, 0), 1, begin(act, ntimes(sub(n, 1), act)))))], ntimes(3, println(1)))), 1)
def test_unwind_protect_loop(self): from util import a, i eq_( eval( let([(i, 3)], block( a, assign(i, sub(i, 1)), if_(eq(i, 0), exit_block(a, 1), unwind_protect(continue_block(a), prin(i)))), i)), 0)
def test_if(self): result = compile_optimize(if_(0, 1, 2)) expect = il.Integer(2) eq_(result, expect)
def test_letrec3(self): x, f = Var('x'), MacroVar('f') eq_( eval( letrec([(f, macro((x, ), if_(eq(x, 1), 1, f(sub(x, 1)))))], f(2))), 1)
def test_letrec_fac(self): from util import m, n, fac eq_(eval(letrec([(fac, lamda([n], if_(eq(n,1), 1, mul(n, fac(sub(n, 1))))))], fac(3))), 6)
def test_letrec_odd_even(self): from util import n, odd, even eq_(eval(letrec([(odd, lamda([n], if_(eq(n,0), 0, even(sub(n,1))))), (even, lamda([n], if_(eq(n,0), 1, odd(sub(n, 1)))))], odd(3))), 1)
def test_if(self): result = compile(if_(0, 1, 2)) expect = '2' 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 testblock3(self): a = Var('a') eq_(eval(block(a, 1, if_(0, continue_block(a), begin(exit_block(a, 2), 3)))), 2)
def test_letrec_odd_even(self): from util import n odd, even = MacroVar('odd'), MacroVar('even') eq_(eval(letrec([(odd, macro([n], if_(eq(n,0), 0, even(sub(n,1))))), (even, macro([n], if_(eq(n,0), 1, odd(sub(n, 1)))))], odd(3))), 1)
def test_letrec_fac(self): from util import m, n fac = MacroVar('fac') eq_(eval(letrec([(fac, macro([n], if_(eq(n,1), 1, mul(n, fac(sub(n, 1))))))], fac(3))), 6)
def test_letrec3(self): x, f = Var('x'), MacroVar('f') eq_(eval(letrec([(f, macro((x,), if_(eq(x,1), 1, f(sub(x,1)))))], f(2))), 1)
def test_macro6(self): n, act, ntimes = Var('n'), Var('act'), MacroVar('ntimes') eq_(eval(letrec([(ntimes, macro((n, act), if_(eq(n, 0), 1, begin(act, ntimes(sub(n, 1), act)))))], ntimes(3, println(1)))), 1)
def testif_(self): eq_(eval(if_(0, 1, 2)), 2)