コード例 #1
0
ファイル: define.py プロジェクト: chaosim/dao
 def cps(self, compiler, cont):
     k = compiler.new_var(il.ConstLocalVar("cont"))
     params = tuple([compiler.new_var(Const("arg")) for x in range(self.arity)])
     clauses = []
     for head, body in self.rules:
         head_exps = begin(*tuple(unify_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)
     params = tuple(param.interlang() for param in params)
     return cont(il.Lamda((k,) + params, body))
コード例 #2
0
ファイル: define.py プロジェクト: hermetique/dao
 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))
コード例 #3
0
ファイル: matcher.py プロジェクト: chaosim/dao
def greedy_seplist(compiler, cont, item, separator, template=None, result=None):
  if result is None:
    return begin(item, greedy_any1(begin(separator, item))
                 ).cps(compiler, cont)  
  else:
    result1  = compiler.new_var(il.ConstLocalVar('result'))
    result2  = compiler.new_var(Var('result'))
    result2_2  = result2.interlang()
    template1 = template.interlang()
    return begin(item, 
                 direct_interlang(il.Assign(result1, il.GetValue(template1))), 
                 greedy_any2(begin(separator, item), template, result2),
                 eval_unify(result, direct_interlang(il.add(il.MakeList(result1), result2_2)))
                 ).cps(compiler, cont)  
コード例 #4
0
def times(compiler, cont, item, expect_times, template=None, result=None):
    if result is None:
        expect_times1 = compiler.new_var(Const('expect_times'))
        return begin(assign(expect_times1, getvalue(expect_times)),
                     times1(item, expect_times1)).cps(compiler, cont)
    else:
        expect_times1 = compiler.new_var(Const('expect_times'))
        result1 = compiler.new_var(il.ConstLocalVar('result'))
        result2 = compiler.new_var(Var('result'))
        result2_2 = result2.interlang()
        template1 = template.interlang()
        return begin(assign(expect_times1, getvalue(expect_times)),
                     times2(item, expect_times1, template, result2),
                     unify(result, result2)).cps(compiler, cont)
コード例 #5
0
def seplist(compiler, cont, item, separator, template=None, result=None):
    if result is None:
        return begin(item, any1(begin(separator, item))).cps(compiler, cont)
    else:
        result1 = compiler.new_var(il.ConstLocalVar('result'))
        result2 = compiler.new_var(Var('result'))
        result2_2 = result2.interlang()
        template1 = template.interlang()
        return begin(
            item, direct_interlang(il.Assign(result1, il.GetValue(template1))),
            any2(begin(separator, item), template, result2),
            eval_unify(
                result,
                direct_interlang(il.add(il.MakeList(result1),
                                        result2_2)))).cps(compiler, cont)
コード例 #6
0
ファイル: matcher.py プロジェクト: chaosim/dao
def times(compiler, cont, item, expect_times, template=None, result=None):
  if result is None:
    expect_times1 = compiler.new_var(Const('expect_times'))
    return begin(assign(expect_times1, getvalue(expect_times)), 
                    times1(item, expect_times1)).cps(compiler, cont)
  else:
    expect_times1 = compiler.new_var(Const('expect_times'))
    result1  = compiler.new_var(il.ConstLocalVar('result'))
    result2  = compiler.new_var(Var('result'))
    result2_2  = result2.interlang()
    template1 = template.interlang()
    return begin(assign(expect_times1, getvalue(expect_times)), 
                 times2(item, expect_times1, template, result2),
                 unify(result, result2)
                 ).cps(compiler, cont)  
コード例 #7
0
ファイル: define.py プロジェクト: chaosim/dao
def macrorules(*rules):
    result = []
    for rule in rules:
        head = tuple(element(x) for x in rule[0])
        body = begin(*(element(x) for x in rule[1:]))
        result.append((head, body))
    return MacroRules(result)
コード例 #8
0
ファイル: define.py プロジェクト: chaosim/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,)))
コード例 #9
0
def lazy_any(compiler, cont, item, template=None, result=None):
    if result is None:
        return lazy_any1(item).cps(compiler, cont)
    else:
        _result = compiler.new_var(Var('result'))
        return begin(lazy_any2(item, template, _result),
                     unify(result, _result)).cps(compiler, cont)
コード例 #10
0
ファイル: matcher.py プロジェクト: chaosim/dao
def lazy_any(compiler, cont, item, template=None, result=None):
  if result is None:
    return lazy_any1(item).cps(compiler, cont)  
  else:
    _result  = compiler.new_var(Var('result'))
    return begin(lazy_any2(item, template, _result), 
                     unify(result, _result)).cps(compiler, cont)  
コード例 #11
0
ファイル: define.py プロジェクト: hermetique/dao
def macrorules(*rules):
  result = []
  for rule in rules:
    head = tuple(element(x) for x in rule[0])
    body = begin(*(element(x) for x in rule[1:]))
    result.append((head, body))
  return MacroRules(result)
コード例 #12
0
def greedy_some(compiler, cont, item, template=None, result=None):
    if result is None:
        return greedy_some1(item).cps(compiler, cont)
    else:
        _result = compiler.new_var(Var('result'))
        return begin(greedy_some2(item, template, _result),
                     unify(result, _result)).cps(compiler, cont)
コード例 #13
0
ファイル: matcher.py プロジェクト: chaosim/dao
def greedy_some(compiler, cont, item, template=None, result=None):
  if result is None:
    return greedy_some1(item).cps(compiler, cont)  
  else:
    _result  = compiler.new_var(Var('result'))
    return begin(greedy_some2(item, template, _result), 
                     unify(result, _result)).cps(compiler, cont)  
コード例 #14
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,)))
コード例 #15
0
def findall(compiler, cont, goal, template=None, bag=None):
  if bag is None: 
    return findall_1(goal).cps(compiler, cont)
  else:
    _bag  = compiler.new_var(Var('bag'))
    return begin(findall_2(goal, template, _bag),unify(bag, _bag)
                 ).cps(compiler, cont)
コード例 #16
0
ファイル: define.py プロジェクト: hermetique/dao
 def cps_call(self, compiler, cont, args):
   if self.arity!=len(args): raise
   clauses = []
   for head, body in self.rules:
     clauses.append(begin(unify_macro_head(compiler, cont, args, head), body))
   if self.has_cut():
     return wrap_cut(or_(*clauses)).cps(compiler, cont)
   else:
     return or_(*clauses).cps(compiler, cont)
コード例 #17
0
ファイル: define.py プロジェクト: chaosim/dao
 def cps_call(self, compiler, cont, args):
     if self.arity != len(args):
         raise
     clauses = []
     for head, body in self.rules:
         clauses.append(begin(unify_macro_head(compiler, cont, args, head), body))
     if self.has_cut():
         return wrap_cut(or_(*clauses)).cps(compiler, cont)
     else:
         return or_(*clauses).cps(compiler, cont)
コード例 #18
0
ファイル: define.py プロジェクト: hermetique/dao
 def alpha(self, env, compiler):
   new_env = env.extend()
   for var, value in self.bindings:
     if isinstance(value, Rules):
       new_var = compiler.new_var(RecursiveFunctionVar(var.name))
     elif isinstance(value, Lamda):
       new_var = compiler.new_var(RecursiveFunctionVar(var.name))
     elif isinstance(value, Macro):
       new_var = compiler.new_var(RecursiveMacroVar(var.name))
     elif isinstance(value, MacroRules):
       new_var = compiler.new_var(RecursiveMacroVar(var.name))
     else:
       new_var = compiler.new_var(var)
     if isinstance(new_var, Const):
       new_var.assigned = True
     new_env.bindings[var] = new_var
   return begin(*(tuple(Assign(new_env[var], value.alpha(new_env, compiler)) 
                        for var, value in self.bindings)
                  +(self.body.alpha(new_env, compiler),)))
コード例 #19
0
ファイル: define.py プロジェクト: chaosim/dao
 def alpha(self, env, compiler):
     new_env = env.extend()
     for var, value in self.bindings:
         if isinstance(value, Rules):
             new_var = compiler.new_var(RecursiveFunctionVar(var.name))
         elif isinstance(value, Lamda):
             new_var = compiler.new_var(RecursiveFunctionVar(var.name))
         elif isinstance(value, Macro):
             new_var = compiler.new_var(RecursiveMacroVar(var.name))
         elif isinstance(value, MacroRules):
             new_var = compiler.new_var(RecursiveMacroVar(var.name))
         else:
             new_var = compiler.new_var(var)
         if isinstance(new_var, Const):
             new_var.assigned = True
         new_env.bindings[var] = new_var
     return begin(
         *(
             tuple(Assign(new_env[var], value.alpha(new_env, compiler)) for var, value in self.bindings)
             + (self.body.alpha(new_env, compiler),)
         )
     )
コード例 #20
0
ファイル: define.py プロジェクト: chaosim/dao
def macro(params, *body):
    body = tuple(element(x) for x in body)
    return Macro(params, begin(*body))
コード例 #21
0
ファイル: define.py プロジェクト: chaosim/dao
def let(bindings, *body):
    bindings = tuple((var, element(value)) for var, value in bindings)
    return Let(bindings, begin(*body))
コード例 #22
0
ファイル: define.py プロジェクト: chaosim/dao
def letrec(bindings, *body):
    return Letrec(
        tuple((element(var), element(value)) for var, value in bindings), begin(*tuple(element(exp) for exp in body))
    )
コード例 #23
0
ファイル: define.py プロジェクト: chaosim/dao
def unify_macro_head(compiler, cont, args, head):
    # for direct call
    return begin(*tuple(unify_macro_head_item1(compiler, cont, arg, head_item) for arg, head_item, in zip(args, head)))
コード例 #24
0
ファイル: define.py プロジェクト: hermetique/dao
def macro(params, *body):
  body = tuple(element(x) for x in body)
  return Macro(params, begin(*body))
コード例 #25
0
ファイル: define.py プロジェクト: hermetique/dao
def unify_macro_head(compiler, cont, args, head):
  # for direct call
  return begin(*tuple(unify_macro_head_item1(compiler, cont, arg, head_item) 
                      for arg, head_item, in zip(args, head)))
コード例 #26
0
ファイル: define.py プロジェクト: chaosim/dao
def lamda(params, *body):
    body = tuple(element(x) for x in body)
    return Lamda(params, begin(*body))
コード例 #27
0
ファイル: define.py プロジェクト: hermetique/dao
def lamda(params, *body):
  body = tuple(element(x) for x in body)
  return Lamda(params, begin(*body))
コード例 #28
0
ファイル: define.py プロジェクト: hermetique/dao
def let(bindings, *body):
  bindings = tuple((var, element(value)) for var, value in bindings)
  return Let(bindings, begin(*body))
コード例 #29
0
ファイル: define.py プロジェクト: hermetique/dao
def letrec(bindings, *body):
  return Letrec(tuple((element(var), element(value)) for var, value in bindings), 
                begin(*tuple(element(exp) for exp in body)))