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 testLoopUntil(self): eq_( eval( tag_loop_label( let([(i, 3)], LoopUntilForm((set(i, sub(i, 1)), prin(i)), eq(i, 0))))), None)
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 testLoopWhen(self): eq_( eval( tag_loop_label( let([(i, 3)], LoopWhenForm((set(i, sub(i, 1)), prin(i)), gt(i, 0))))), None)
def testLoopTimes(self): eq_( eval( tag_loop_label( let([(i, 3)], LoopTimesForm(3, (set(i, sub(i, 1)), prin(i)))))), None)
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 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_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_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_rules(self): f = Var('f') x = Var('x') eq_(eval(letrec([(f, rules([[1], 1], [[x], f(sub(x, 1))]))], f(1))), 1) # passed eq_(eval(letrec([(f, rules([[1], 1], [[x], f(sub(x, 1))]))], f(2))), 1)
def test_arithmetic(self): eq_(eval(add(1, 2)), 3) eq_(eval(sub(1, 1)), 0) eq_(eval(mul(2, 2)), 4) eq_(eval(div(2, 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 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_rules(self): f = Var('f') x = Var('x') eq_(eval(letrec([(f, rules([[1], 1],[[x],f(sub(x,1))]))], f(1))), 1) # passed eq_(eval(letrec([(f, rules([[1], 1],[[x],f(sub(x,1))]))], f(2))), 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 testLoopTimes(self): eq_(eval(tag_loop_label(let([(i,3)], LoopTimesForm(3, (set(i, sub(i, 1)), prin(i)))))), None)
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 testLoopUntil(self): eq_(eval(tag_loop_label(let([(i,3)], LoopUntilForm((set(i, sub(i, 1)), prin(i)), eq(i,0))))), None)
def testLoopWhen(self): eq_(eval(tag_loop_label(let([(i,3)], LoopWhenForm((set(i, sub(i, 1)), prin(i)), gt(i,0))))), None)
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_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)