def testoptionalcut(self): x, s = Var('x'), Var('s') ruleList = [(s, function(((x, ), and_p(-char('a'), cut, char(x)))))] eq_(eval(let(ruleList, parse_text(s(x), 'aa'), x)), 'a') eq_(eval(let(ruleList, parse_text(s(x), 'b'), x)), 'b') assert_raises(NoSolution, eval, let(ruleList, parse_text(s(x), 'a'), x))
def test2(self): x = Var('x') Lx = LogicVar('x') eq_(eval(unify(1, 1)), True) eq_(eval(begin(unify(1, 1), unify(2, 2))), True) eq_(eval(begin(unify(Lx, 1), unify(Lx, 1))), True) eq_(eval(let([(x, 1)], unify(x, 1))), True)
def testLoopWhen(self): eq_( eval( tag_loop_label( let([(i, 3)], LoopWhenForm((set(i, sub(i, 1)), prin(i)), gt(i, 0))))), None)
def testLoopUntil(self): eq_( eval( tag_loop_label( let([(i, 3)], LoopUntilForm((set(i, sub(i, 1)), prin(i)), eq(i, 0))))), None)
def test2(self): x = Var('x') Lx = LogicVar('x') eq_(eval(unify(1, 1)), True) eq_(eval(begin(unify(1, 1), unify(2, 2))), True) eq_(eval(begin(unify(Lx, 1), unify(Lx,1))), True) eq_(eval(let([(x,1)], unify(x,1))), True)
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_findall_template_func(self): x, y, z = LogicVar('x'), LogicVar('y'), LogicVar('z') f = Var('f') eq_( eval( let([(f, rules(((), 2), ((), 3)))], findall(is_(x, f()), x, y), getvalue(y))), [2, 3])
def testLoopTimes(self): eq_( eval( tag_loop_label( let([(i, 3)], LoopTimesForm(3, (set(i, sub(i, 1)), prin(i)))))), None)
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_open_read2(self): file1 = Var('file1') x = Var('x') eq_( eval( let([(file1, open_file('test.txt'))], readline(file1), assign(x, readlines(file1)), close_file(file1), x)), ['hello'])
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_chars(self): x, cs, chars = LogicVar('x'), Var('cs'), MacroVar('chars') x1 = Var('x') eq_( eval( let([(chars, lamda( (x1, cs), begin(char(x1), contain(cs, x1))))], parse_text(chars(x, 'a'), 'a'), getvalue(x))), 'a')
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_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_open_read2(self): file1 = Var('file1') x = Var('x') eq_(eval(let([(file1, open_file('test.txt'))], readline(file1), assign(x, readlines(file1)), close_file(file1), x)), ['hello'])
def testblock(self): f = Var('f') foo = Var('foo') eq_( eval( block( foo, let([(f, lamda((), exit_block(foo, 1)))], mul(2, block(foo, f()))))), 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_chars(self): x, cs, chars = LogicVar('x'), Var('cs'), MacroVar('chars') x1 = Var('x') eq_(eval(let([(chars, lamda((x1, cs), begin(char(x1), contain(cs, x1))))], parse_text(chars(x, 'a'), 'a'), getvalue(x))), 'a')
def testeval3(self): x = Const('x') eq_(eval(let([(x, quote(1))], eval_(x))), 1)
def test_nongreedy_optional(self): x, s = Var('x'), Var('s') ruleList =[(s, function( ((x,), and_p(-char('a'),char(x)))))] eq_(eval(let(ruleList, parse_text(s(x), 'a'), x)), 'a') eq_(eval(let(ruleList, parse_text(s(x), 'aa'), x)), 'a') eq_(eval(let(ruleList, parse_text(s(x), 'b'), x)), 'b')
def test_embeded_module(self): a, m1, m2 = Var('a'), Var('m1'), Var('m2') eq_( eval( let([(m1, module(define(a, 1), define(m2, module(define( a, 2)))))], from_(from_(m1, m2), a))), 2)
def test_let_assign_const(self): a = Const('a') assert_raises(MultiAssignToConstError, eval, let([(a, 2)], assign(a, 3)))
def test_replace(self): from dao.builtins.rule import replace eq_(eval(let([(f,function(([1], 1), ([2], 2)))], replace(f, [2], [3]), f(2))), 3)
def test_closure2(self): x, f = Const('x'), Const('f') eq_(eval(let([(f, macrorules([[x], x])), (x, 1)], f(add(x,x)))), 2)
def testletdouble1(self): x, f = Var('x'), MacroVar('f') eq_(eval(let([(f, macro([x], add(x, x)))], f(1))), 2)
def testletdouble2(self): x, f = Const('x'), ConstMacroVar('f') eq_(eval(let([(f, macro([x], add(x, x)))], f(1))), 2)
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_let_in_module(self): a, m1, m2 = Var('a'), Var('m1'), Var('m2') eq_(eval(let([(m1,module(define(a,1), let([(a,2)], define(a,3))))], from_(m1,a))), 1)
def test_let_assign_const(self): a = Const('a') assert_raises(MultiAssignToConstError, eval, let([(a,2)], assign(a, 3)))
def test_findall_template_func(self): x, y, z = LogicVar('x'), LogicVar('y'), LogicVar('z') f = Var('f') eq_(eval(let([(f, rules(((), 2), ((), 3)))], findall(is_(x, f()), x, y), getvalue(y))), [2, 3])
def test_closure3(self): x, y, f = Const('x'), Const('y'), Const('f') eq_( eval( let([(f, macrorules([[x, y], begin(x, y)])), (x, 1)], f(prin(x), prin(add(x, x))))), None)
def test_fail(self): x = Var('x') x1 = LogicVar('x') eq_(eval(let([(f,rules([[1], fail], [[x], succeed]))], f(x1))), True)
def testeval4(self): x = Const('x') eq_(eval(let([(x, quote(add(1,1)))], eval_(x))), 2)
def testfrom(self): a, m = Var('a'), Var('m') eq_(eval(let([(m,module(define(a,1)))], from_(m,a))), 1)
def test_embeded_module(self): a, m1, m2 = Var('a'), Var('m1'), Var('m2') eq_(eval(let([(m1,module(define(a,1),define(m2, module(define(a,2)))))], from_(from_(m1,m2),a))), 2)
def testfrom(self): a, m = Var('a'), Var('m') eq_(eval(let([(m, module(define(a, 1)))], from_(m, a))), 1)
def testdouble4(self): f = Var('f') x = Var('x') eq_(eval(let([(f, rules([[x], add(x, x)]))], f(f(1)))), 4)
def test_let_in_module(self): a, m1, m2 = Var('a'), Var('m1'), Var('m2') eq_( eval( let([(m1, module(define(a, 1), let([(a, 2)], define(a, 3))))], from_(m1, a))), 1)
def testdouble5(self): f = Var('f') x = Var('x') assert_raises(TypeError, eval, let([(f, rules([[x], add(x, x)]))], f(1, 2))) # passed
def testeval2(self): x = Const('x') eq_(eval(let([(x, 1)], eval_(quote(x)))), 1)
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 testeval4(self): x = Const('x') eq_(eval(let([(x, quote(add(1, 1)))], eval_(x))), 2)
def test_open_read(self): file1 = Var('file1') eq_(eval(let([(file1, open_file('test.txt'))], assign(x, read(file1)), close_file(file1), x)), 'hello\nhello')
def testoptionalcut(self): x, s = Var('x'), Var('s') ruleList = [(s, function( ((x,), and_p(-char('a'), cut, char(x)))))] eq_(eval(let(ruleList, parse_text(s(x), 'aa'), x)), 'a') eq_(eval(let(ruleList, parse_text(s(x), 'b'), x)), 'b') assert_raises(NoSolution, eval, let(ruleList, parse_text(s(x), 'a'), x))
def test_write(self): file1 = Var('file1') eq_(eval(let([(file1, open_file('test2.txt', 'w'))], write(file1, 'test'), close_file(file1))), None)
def test_let_embed_var2(self): e, f = LogicVar('e'), Var('f') eq_(eval(let([(f, rules([[1], 1]))], f(e), getvalue(e))), 1)
def testeval2(self): x = Const('x') eq_(eval(let([(x,1)], eval_(quote(x)))), 1)
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_closure1(self): x, f = Const('x'), Const('f') eq_(eval(let([(f, macrorules([[x], prin(x)])), (x, 1)], f(add(x,x)))), None)
def test5(self): x = Var('x') y = Var('y') eq_(eval(let([(y, 1)], macrorules([[x], prin(x)])(add(y, 1)))), None)
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_abolish(self): from dao.builtins.rule import abolish eq_(eval(let([(f,function([[1], 1]))], abolish(f, 1))), {})
def test_closure1(self): x, f = Const('x'), Const('f') eq_(eval(let([(f, macrorules([[x], prin(x)])), (x, 1)], f(add(x, x)))), None)
def test_asserta(self): from dao.builtins.rule import asserta eq_(eval(let([(f,function(([1], 1)))], asserta(f, [2], [2]), f(2))), 2)
def test_closure2(self): x, f = Const('x'), Const('f') eq_(eval(let([(f, macrorules([[x], x])), (x, 1)], f(add(x, x)))), 2)
def test_succeed(self): x = LogicVar('x') eq_(eval(let([(f,rules([[1], succeed]))], f(x))), True)
def test_closure3(self): x, y, f = Const('x'), Const('y'), Const('f') eq_(eval(let([(f, macrorules([[x, y], begin(x, y)])), (x, 1)], f(prin(x), prin(add(x,x))))), None)