def integer(compiler, cont, arg): '''integer''' 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.Not(il.Cle(il.String('0'), il.GetItem(text, pos), il.String('9'))), il.failcont(il.FALSE), il.Begin(( il.Assign(p, il.add(pos, il.Integer(1))), il.while_(il.And(il.Lt(p, length), 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, il.String): 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.Not(il.Cle(il.String('0'), il.GetItem(text, pos), il.String('9'))), il.failcont(il.FALSE), il.Begin(( il.Assign(p, il.add(pos, il.Integer(1))), il.while_(il.And(il.Lt(p, length), 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
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
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)
def times1(compiler, cont, item, expect_times): expect_times = expect_times.interlang() i = compiler.new_var(il.Var('i')) times_cont = compiler.new_var(il.ConstLocalVar('times_cont')) v = compiler.new_var(il.ConstLocalVar('v')) return il.begin( il.Assert( il.And(il.Isinstance(expect_times, il.Int), il.Gt(expect_times, il.Integer(0)))), il.Assign(i, il.Integer(0)), il.cfunction( times_cont, v, item.cps( compiler, il.clamda( v, il.AddAssign(i, il.Integer(1)), il.If(il.Eq(i, expect_times), cont(v), times_cont(il.TRUE)))))(il.TRUE))
def times2(compiler, cont, item, expect_times, template, result): expect_times = expect_times.interlang() template = template.interlang() result = result.interlang() i = compiler.new_var(il.Var('i')) times_cont = compiler.new_var(il.ConstLocalVar('times_cont')) v = compiler.new_var(il.ConstLocalVar('v')) return il.begin( il.Assert( il.And(il.Isinstance(expect_times, il.Int), il.Gt(expect_times, il.Integer(0)))), il.Assign(result, il.empty_list), il.Assign(i, il.Integer(0)), il.cfunction( times_cont, v, item.cps( compiler, il.clamda( v, il.AddAssign(i, il.Integer(1)), il.ListAppend(result, il.GetValue(template)), il.If(il.Eq(i, expect_times), cont(v), times_cont(il.TRUE)))))(il.TRUE))
def isstr(compiler, cont, item): return cont(il.Isinstance(item.interlang(), il.Symbol('str')))
def isnumber(compiler, cont, item): return cont( il.or_(il.Isinstance(item.interlang(), il.Symbol('int')), il.Isinstance(item.interlang(), il.Symbol('float'))))
def isfloat(compiler, cont, item): return cont(il.Isinstance(item.interlang(), il.Symbol('float')))
def istuple(compiler, cont, item): return cont(il.Isinstance(item.interlang(), il.Symbol('tuple')))