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 testembedvar2(self): e, e2, f, g, h = Var('e'), Var('e2'), Var('f'), Var('g'), Var('h') eq_( eval( letrec([(f, macro([[cons(1, e2)], g(e2)])), (g, function([[e], h(e)])), (h, function([[1], True]))], f(e), e)), cons(1, 1))
def test_kleene2(self): f, kleene = MacroVar('f'), MacroVar('kleene') _ = DummyVar('_') item = Var('item') fun = macro((item, ), letrec([(f, macrorules(((), item, f()), ((), nullword)))], f())) eq_(eval(let([(kleene, fun)], set_text('ab'), kleene(char(_)))), True)
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_kleene1(self): f, kleene = MacroVar('f'), MacroVar('kleene') item = Var('item') fun = macro((item,), letrec([(f, macrorules( ((), item, f()), ((), nullword)))], f())) eq_(eval(let([(kleene, fun)], set_text('aa'), kleene(char('a')))), True)
def test_macro5(self): x, y, f = Const('x'), Const('y'), MacroVar('f') eq_(eval(let([(f, macro((x, y), y, x, 1))], f(println(1), prin(2)))), 1)
def test_macro3(self): x, y = Const('x'), Const('y') eq_(eval(macro((x, y), y, x, 1)(println(1), prin(2))), 1)
def test_macro2(self): x, y = Var('x'), Var('y') eq_(eval(macro((x, y), add(x, y))(1, 3)), 4)
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 testletdouble1(self): x, f = Var('x'), MacroVar('f') eq_(eval(let([(f, macro([x], add(x, x)))], f(1))), 2)
def test_letrec_assign2(self): x, f = Const('x'), Const('f') assert_raises(MultiAssignToConstError, eval, letrec([(f, macro((x, ), f(1), assign(f, 1)))], f(2)))
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 testletdouble2(self): x, f = Const('x'), ConstMacroVar('f') eq_(eval(let([(f, macro([x], add(x, x)))], f(1))), 2)
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_assign2(self): x, f = Const('x'), Const('f') assert_raises(MultiAssignToConstError, eval, letrec([(f, macro((x,), f(1), assign(f, 1)))], f(2)))
def xtest_letrec2(self): x, f = Var('x'), MacroVar('f') eq_(eval(letrec([(f, macro((x,), f(1)))], f(2))), 1)
def xtest_letrec2(self): x, f = Var('x'), MacroVar('f') eq_(eval(letrec([(f, macro((x, ), f(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 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 testembedvar2(self): e, e2, f, g, h = Var('e'), Var('e2'), Var('f'), Var('g'), Var('h') eq_(eval(letrec([(f, macro([[cons(1, e2)], g(e2)])), (g, function([[e], h(e)])), (h, function([[1], True]))], f(e), e)), cons(1, 1))
def test_macro(self): x, y = Var('x'), Var('y') eq_(eval(macro((x, ), 1)(1)), 1) eq_(eval(macro((x, ), 1)(2)), 1) eq_(eval(macro((x, ), x)(2)), 2)
def test_macro(self): x, y = Var('x'), Var('y') eq_(eval(macro((x,), 1)(1)), 1) eq_(eval(macro((x,), 1)(2)), 1) eq_(eval(macro((x,), x)(2)), 2)