Esempio n. 1
0
def string_on_predicate1(compiler, cont, test):
  '''return current char and step if @test succeed, where 
   @test: a python function with one argument, which tests on one char and return True or False
          @test must be registered with register_function'''
  test = test.interlang()
  text = compiler.new_var(il.ConstLocalVar('text'))
  pos = compiler.new_var(il.ConstLocalVar('pos'))
  length = compiler.new_var(il.ConstLocalVar('length'))
  p = compiler.new_var(il.LocalVar('p'))
  if not isinstance(test, il.PyFunction):
    raise DaoCompileTypeError(test)
  return il.Begin((
    il.AssignFromList(text, pos, il.parse_state),
    il.Assign(length, il.Len(text)),
    il.If(il.Ge(pos, length), 
      il.failcont(il.FALSE),
      il.If(il.Call(test, il.GetItem(text, pos)),
        il.begin(
          il.Assign(p, il.add(pos, il.Integer(1))),
          il.While(il.And(il.Lt(p, length), il.Call(test, il.GetItem(text, p))),
                 il.AddAssign(p, il.Integer(1))),
          il.SetParseState(il.Tuple(text, p)),
          il.append_failcont(compiler, 
                             il.SetParseState(il.Tuple(text, pos))),
          cont(il.GetItem(text, il.Slice2(pos, p)))),
        il.failcont(il.FALSE)))))
Esempio n. 2
0
 def quasiquote(self, compiler, cont):
   result = compiler.new_var(il.LocalVar('result'))
   vars = tuple(compiler.new_var(il.ConstLocalVar('a'+repr(i))) for i in range(len(self.args)))
   body = (il.Assign(result, il.empty_list),)+tuple(
     il.If(il.Isinstance(var, il.Klass('UnquoteSplice')),
                 il.AddAssign(result, il.Call(il.Symbol('list'), il.Attr(var, il.Symbol('item')))),
                 il.ListAppend(result, var),
                 ) for var in vars)+(
     cont(il.Call(il.Klass(self.__class__.__name__), il.QuoteItem(self.function), il.MakeTuple(result))),)
   fun = il.begin(*body)
   for var, arg in reversed(zip(vars, self.args)):
     fun = arg.quasiquote(compiler, il.clamda(var, fun))
   return fun
Esempio n. 3
0
def eval_macro_args(compiler, cont, exp):
  v = compiler.new_var(il.ConstLocalVar('v'))
  return il.Call(exp.interlang(), cont)