Esempio n. 1
0
 def test_function2(self):
     x = il.Var('x')
     f = il.Var('f')
     result = optimize(
         il.Begin((il.Assign(f, il.Lamda(
             (x, ), f(il.Integer(1)))), il.Apply(f, (il.Integer(3), )))))
     expect = il.Begin((il.Assign(f, il.Lamda(
         (x, ), f(il.Integer(1)))), il.Apply(f, (il.Integer(3), ))))
     eq_(result, expect)
Esempio n. 2
0
def callcc(compiler, cont, function):
    body = function.body.subst(
        {function.params[0]: LamdaVar(function.params[0].name)})
    k = compiler.new_var(il.ConstLocalVar('cont'))
    params = tuple(x.interlang() for x in function.params)
    function1 = il.Lamda((k, ) + params, body.cps(compiler, k))
    k1 = compiler.new_var(il.ConstLocalVar('cont'))
    v = compiler.new_var(il.ConstLocalVar('v'))
    return function1(cont, il.Lamda((k1, v), cont(v)))
Esempio n. 3
0
 def test_letrec(self):
     f, k, function = il.Var('f'), il.Var('k'), il.Var('function')
     result = cps(letrec([(f, lamda((), f()))], f()))
     expect = il.Clamda(v, il.Assign(f, v), v)(il.Lamda(
         (k, ),
         il.Clamda(function, function(k))(f)))
     eq_(result, expect)
Esempio n. 4
0
def unify_macro_head_item1(compiler, cont, arg, head_item):
  # for direct call
  if isinstance(head_item, Var) and not isinstance(head_item, LogicVar):
    k = compiler.new_var(il.ConstLocalVar('cont'))
    return Assign(head_item, direct_interlang(il.Lamda((k, ), 
                  arg.cps(compiler, k))))
  else: 
    return unify(arg, head_item)
Esempio n. 5
0
 def cps(self, compiler, cont):
   k = compiler.new_var(il.ConstLocalVar('cont'))
   params = tuple(x.interlang() for x in self.params)
   if self.has_cut():
     body = wrap_cut(self.body).cps(compiler, k)
   else:
     body = self.body.cps(compiler, k)
   return cont(il.Lamda((k,)+params, body))
Esempio n. 6
0
 def cps_call(self, compiler, cont, args):
   function = self.interlang()
   k = compiler.new_var(il.ConstLocalVar('cont'))
   v = compiler.new_var(il.ConstLocalVar('v'))
   #macro_args = tuple(il.Lamda((), arg.cps(compiler, il.clamda(v, v))) 
                               #for arg in args)
   macro_args = tuple(il.Lamda((k,), arg.cps(compiler, k)) for arg in args)
   return self.cps(compiler, 
                           il.clamda(function, il.Apply(function, (cont,)+macro_args)))
Esempio n. 7
0
 def cps_call(self, compiler, cont, args):
   # see The 90 minute Scheme to C compiler by Marc Feeley
   if self.has_cut():
     fun = wrap_cut(self.body).cps(compiler, cont)
   else:
     fun = self.body.cps(compiler, cont)
   params = tuple(x.interlang() for x in self.params)
   for var, arg in reversed(zip(params, args)):
     k = compiler.new_var(il.ConstLocalVar('cont'))
     fun = direct_interlang(il.Lamda((k, ), arg.cps(compiler, k))
                            ).cps(compiler, il.Clamda(var, fun))
   return fun
Esempio n. 8
0
 def cps(self, compiler, cont):
   k = compiler.new_var(il.ConstLocalVar('cont'))
   params = tuple([compiler.new_var(il.ConstLocalVar('arg')) 
                   for x in range(self.arity)])
   clauses = []
   for head, body in self.rules:
     head_exps = begin(*tuple(unify_macro_head_item2(param, head_item) 
                          for param, head_item in zip(params, head)))
     clauses.append(begin(head_exps, body))
   if self.has_cut():
     body = wrap_cut(or_(*clauses)).cps(compiler, k)
   else:  
     body = or_(*clauses).cps(compiler, k)
   return cont(il.Lamda((k,)+params, body))
Esempio n. 9
0
 def cps_call(self, compiler, cont, args):
   # see The 90 minute Scheme to C compiler by Marc Feeley
   raise CompileTypeError(self)
 
   function = compiler.new_var(il.ConstLocalVar('function'))
   vars = tuple(compiler.new_var(il.ConstLocalVar('a'+repr(i))) for i in range(len(args)))
   body = il.Apply(function, (cont,)+vars)
   for var, item in reversed(zip(vars, args)):
     body = item.cps(compiler, il.clamda(var, body)) 
   v = compiler.new_var(il.ConstLocalVar('v'))
   macro_args1 = tuple(il.ExpressionWithCode(arg, 
                                     il.Lamda((), arg.cps(compiler, il.clamda(v, v)))) 
                               for arg in args)
   macro_args2 = il.macro_args(macro_args1)
   return self.cps(compiler, il.clamda(function,
                 il.If(il.IsMacro(function),
                       il.If(il.IsMacroRules(function),
                             il.Apply(function, (cont, macro_args2)),
                             il.Apply(function, (cont,)+macro_args1)),
                       body)))
Esempio n. 10
0
def expression_with_code(compiler, cont, exp):
  v = compiler.new_var(il.ConstLocalVar('v'))
  return cont(il.ExpressionWithCode(exp, il.Lamda((), exp.cps(compiler, il.clamda(v, v)))))
Esempio n. 11
0
 def cps(self, compiler, cont):
   return il.Lamda((params), self.function.function(*params))
Esempio n. 12
0
 def interlang(self):
   return il.Lamda(tuple(x.interlang() for x in self.params), self.body.interlang())
Esempio n. 13
0
 def test_quote(self):
     result = compile_optimize(quote(1))
     expect = il.ExpressionWithCode(il.Integer(1),
                                    il.Lamda((), il.Integer(1)))
     eq_(result, expect)
Esempio n. 14
0
def quote(compiler, cont, exp):
    return cont(
        il.ExpressionWithCode(exp,
                              il.Lamda((), exp.cps(compiler, il.equal_cont))))