Exemple #1
0
def unify_head_item1(arg, head_item):
  # for direct call
  if isinstance(head_item, Var):
    if not isinstance(head_item, LogicVar):
      return Assign(head_item, arg)
    else: 
      raise CompileTypeError(head_item)
  else: 
    if isinstance(head_item, Cons):
      raise CompileTypeError(head_item)
    else:
      return unify(arg, head_item)
Exemple #2
0
def element(exp):
  if isinstance(exp, Element):
    return exp
  else:
    try: 
      return type_map[type(exp)](exp)
    except: 
      raise CompileTypeError(exp)
Exemple #3
0
def char(compiler, cont, argument):
  text = compiler.new_var(il.ConstLocalVar('text'))
  pos = compiler.new_var(il.ConstLocalVar('pos'))
  if isinstance(argument, String):
    argument = argument.interlang()
    return il.Begin((
      il.AssignFromList(text, pos, il.parse_state),
      il.If(il.Ge(pos, il.Len(text)), 
        il.failcont(il.NONE),
        il.If(il.Eq(argument, il.GetItem(text, pos)),
              il.begin(il.append_failcont(compiler, 
                              il.SetParseState(il.Tuple(text, pos))),
                       il.SetParseState(il.Tuple(text, il.add(pos, il.Integer(1)))),
                       cont(il.GetItem(text, pos))),
              il.failcont(il.NONE)))))
  
  elif isinstance(argument, Var):
    v = compiler.new_var(il.ConstLocalVar('v'))
    argument = argument.interlang()
    argument1 = compiler.new_var(il.ConstLocalVar('argument'))
    return il.Begin((
      il.AssignFromList(text, pos, il.parse_state),
      il.If(il.Ge(pos,il.Len(text)), 
        il.failcont(il.FALSE),
        il.Begin((
          il.Assign(argument1, il.Deref(argument)),
          #il.Prin(text), il.Prin(pos), il.PrintLn(argument1),
          il.If(il.Isinstance(argument1, il.Symbol('str')),
            il.If(il.Eq(argument1, il.GetItem(text, pos)),
                  il.begin(il.append_failcont(compiler, 
                                  il.SetParseState(il.Tuple(text, pos))),
                           il.SetParseState(il.Tuple(text, il.add(pos, il.Integer(1)))),
                           cont(il.GetItem(text, pos))),
                  il.failcont(il.NONE)),
            il.If(il.IsLogicVar(argument1),
                  il.begin(il.SetParseState(il.Tuple(text, il.add(pos, il.Integer(1)))),
                           il.SetBinding(argument1, il.GetItem(text, pos)),
                           il.append_failcont(compiler, 
                              il.SetParseState(il.Tuple(text, pos)),
                              il.DelBinding(argument1)),
                           cont(il.GetItem(text, pos))),
                  il.RaiseTypeError(argument1))))))))
      
  # elif isinstance(argument, il.LogicVar) #how about this? It should be include above.
  else: raise CompileTypeError(argument)
Exemple #4
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)))
Exemple #5
0
def identifier(compiler, cont, arg):
  '''underline or letter lead, follow underline, letter or digit'''
  text = compiler.new_var(il.ConstLocalVar('text'))
  pos = compiler.new_var(il.ConstLocalVar('pos'))
  p = compiler.new_var(il.LocalVar('p'))
  length = compiler.new_var(il.ConstLocalVar('length'))
  if isinstance(arg, Var):
    arg = arg.interlang()
    x = compiler.new_var(il.ConstLocalVar('x'))
    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.and_(il.Ne(il.GetItem(text, pos), il.String('_')),
                         il.Not(il.Cle(il.String('a'), il.GetItem(text, pos), il.String('z'))), 
                         il.Not(il.Cle(il.String('A'), il.GetItem(text, pos), il.String('Z')))), 
            il.failcont(il.FALSE),
            il.Begin((
              il.Assign(p, il.add(pos, il.Integer(1))),
              il.while_(il.And(il.Lt(p, length), 
                              il.or_(il.Eq(il.GetItem(text, pos), il.String('_')),
                                    il.Cle(il.String('a'), il.GetItem(text, p), il.String('z')), 
                                    il.Cle(il.String('A'),il.GetItem(text, p),il.String('Z')),
                                    il.Cle(il.String('0'),il.GetItem(text, p),il.String('9')))), 
                       il.AddAssign(p, il.Integer(1))),
              il.Assign(x, il.Deref(arg)),
              il.If(il.IsLogicVar(x),
                    il.begin(il.SetParseState(il.Tuple(text, p)),
                                   il.SetBinding(x, il.GetItem(text, il.Slice2(pos, p))),
                                   il.append_failcont(compiler, 
                                      il.SetParseState(il.Tuple(text, pos)),
                                      il.DelBinding(x)),
                                   cont(il.GetItem(text, pos))),
                    il.If(il.Isinstance(x, il.String('str')),
                          il.If(il.Eq(x, il.GetItem(text, il.Slice2(pos, p))),
                                il.begin(il.append_failcont(compiler, 
                                  il.SetParseState(il.Tuple(text, pos))),
                                  il.SetParseState(il.Tuple(text, p)),
                                  cont(il.GetItem(text, pos))),
                                il.failcont(il.NONE)),
                          il.RaiseTypeError(x)))))))))
  elif isinstance(arg, String):
    arg = arg.interlang()
    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.and_(il.Ne(il.GetItem(text, pos), il.String('_')),
                         il.Not(il.Cle(il.String('a'), il.GetItem(text, pos), il.String('z'))), 
                         il.Not(il.Cle(il.String('A'), il.GetItem(text, pos), il.String('Z')))), 
            il.failcont(il.FALSE),
            il.Begin((
              il.Assign(p, il.add(pos, il.Integer(1))),
              il.while_(il.And(il.Lt(p, length), 
                              il.or_(il.Eq(il.GetItem(text, pos), il.String('_')),
                                    il.Cle(il.String('a'), il.GetItem(text, p), il.String('z')), 
                                    il.Cle(il.String('A'),il.GetItem(text, p),il.String('Z')),
                                    il.Cle(il.String('0'),il.GetItem(text, p),il.String('9')))), 
                       il.AddAssign(p, il.Integer(1))),
              il.If(il.Eq(arg, il.GetItem(text, il.Slice2(pos, p))),
                    il.begin(il.append_failcont(compiler, 
                      il.SetParseState(il.Tuple(text, pos))),
                      il.SetParseState(il.Tuple(text, p)),
                      cont(arg)),
                    il.failcont(il.NONE))))))))
  else:
    raise CompileTypeError(arg)
Exemple #6
0
def or_(*clauses):
  if not clauses: raise CompileTypeError("should have least 1 clauses in or")
  if len(clauses)==1: return clauses[0]
  elif len(clauses)==2: return or2(*clauses)
  else:
    return or2(clauses[0], or_(*clauses[1:]))