예제 #1
0
파일: testeval.py 프로젝트: hermetique/dao
 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)
예제 #2
0
 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)
예제 #3
0
파일: define.py 프로젝트: hermetique/dao
 def alpha(self, env, compiler):
   new_env = env.extend()
   for var, value in self.bindings:
     if isinstance(value, Rules):
       if not isinstance(var, Const):
         new_var = compiler.new_var(LamdaVar(var.name))
       else:
         new_var = compiler.new_var(ConstLamdaVar(var.name))
     elif isinstance(value, Lamda):
       if not isinstance(var, Const):
         new_var = compiler.new_var(LamdaVar(var.name))
       else:
         new_var = compiler.new_var(ConstLamdaVar(var.name))
     elif isinstance(value, MacroRules):
       if not isinstance(var, Const):
         new_var = compiler.new_var(MacroVar(var.name))
       else:
         new_var = compiler.new_var(ConstMacroVar(var.name))
     else:
       new_var = compiler.new_var(var)
     if isinstance(var, Const):
       new_var.assigned = True
     env[var ]  = new_var
   alphaed_body = self.body.alpha(new_env, compiler)
   assign_bindings = tuple(Assign(new_env[var], value.alpha(env, compiler)) 
                           for var, value in self.bindings)
   return begin(*(assign_bindings+(alphaed_body,)))
예제 #4
0
파일: testeval.py 프로젝트: hermetique/dao
 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)
예제 #5
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')
예제 #6
0
파일: testeval.py 프로젝트: hermetique/dao
 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)
예제 #7
0
파일: testeval.py 프로젝트: hermetique/dao
 def testletdouble1(self):
     x, f = Var('x'), MacroVar('f')
     eq_(eval(let([(f, macro([x], add(x, x)))], f(1))), 2)
예제 #8
0
파일: testeval.py 프로젝트: hermetique/dao
 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)
예제 #9
0
파일: testeval.py 프로젝트: hermetique/dao
 def xtest_letrec2(self):
     x, f = Var('x'), MacroVar('f')
     eq_(eval(letrec([(f, macro((x, ), f(1)))], f(2))), 1)
예제 #10
0
파일: testeval.py 프로젝트: hermetique/dao
 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)