def between(compiler, cont, lower, upper, mid): lower1 = compiler.new_var(il.ConstLocalVar('lower')) upper1 = compiler.new_var(il.ConstLocalVar('upper')) mid1 = compiler.new_var(il.ConstLocalVar('mid')) fc = compiler.new_var(il.ConstLocalVar('fc')) i = compiler.new_var(il.Var('i')) v = compiler.new_var(il.ConstLocalVar('v')) return lower.cps(compiler, il.clamda(lower1, upper.cps(compiler, il.clamda(upper1, mid.cps(compiler, il.clamda(mid1, il.If(il.IsLogicVar(mid1), il.begin( il.Assign(i, lower1), il.Assign(fc, il.failcont), il.SetFailCont(il.clamda(v, il.If(il.Eq(i, upper1), il.Begin(( il.Assign(il.failcont, fc), fc(il.FALSE))), il.Begin(( il.AddAssign(i, il.Integer(1)), il.SetBinding(mid1, i), cont(il.TRUE)))))), il.SetBinding(mid1, lower1), cont(il.TRUE)), il.If(il.Cle(lower1, mid1, upper1), cont(il.TRUE), il.failcont(il.FALSE))))) ))))
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 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)
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)))))
def any2(compiler, cont, item, template, result): template = template.interlang() result = result.interlang() any_cont = compiler.new_var(il.ConstLocalVar('any_cont')) fc = compiler.new_var(il.ConstLocalVar('old_fail_cont')) v = compiler.new_var(il.ConstLocalVar('v')) v2 = compiler.new_var(il.ConstLocalVar('v')) v3 = compiler.new_var(il.ConstLocalVar('v')) return il.Begin( (il.Assign(result, il.empty_list), il.cfunction( any_cont, v, il.Assign(fc, il.failcont), il.SetFailCont( il.clamda( v, il.SetFailCont( il.clamda( v3, il.if2(result, il.DelListItem(result, il.Integer(-1))), fc(v3))), cont(v))), item.cps( compiler, il.clamda(v2, il.ListAppend(result, il.GetValue(template)), any_cont(v2))))(il.NONE)))
def subsequence(compiler, cont, start, end): text = compiler.new_var(il.ConstLocalVar('text')) pos = compiler.new_var(il.ConstLocalVar('pos')) start = il.Integer(start.item) end = il.Integer(end.item) return il.Begin(( il.AssignFromList(text, pos, il.parse_state), cont(il.GetItem(text, il.Slice2(start, end)))))
def left(compiler, cont, length=NONE): text = compiler.new_var(il.ConstLocalVar('text')) pos = compiler.new_var(il.ConstLocalVar('pos')) return il.Begin(( il.AssignFromList(text, pos, il.parse_state), cont(il.GetItem(text, il.If(il.IsNot(length, NONE), il.Slice2(pos, il.add(pos,length)), il.Slice2(pos, NONE))))))
def literal(compiler, cont, arg): '''any given instance string''' text = compiler.new_var(il.ConstLocalVar('text')) pos = compiler.new_var(il.ConstLocalVar('pos')) p = compiler.new_var(il.LocalVar('p')) i = compiler.new_var(il.LocalVar('i')) x = compiler.new_var(il.ConstLocalVar('x')) length = compiler.new_var(il.ConstLocalVar('length')) length2 = compiler.new_var(il.ConstLocalVar('length2')) if isinstance(arg, Var): return il.Begin(( il.Assign(x, il.Deref(arg)), il.AssignFromList(text, pos, il.parse_state), il.Assign(length, il.Len(text)), il.Assign(length, il.Len(x)), il.Assign(i, il.Integer(0)), il.Assign(p, pos), il.while_(il.and_(il.Lt(i, length2), il.Lt(p, length), il.Eq(il.GetItem(text, p), il.GetItem(x, i))), il.AddAssign(p, il.Integer(1))), il.If(il.Lt(i, length2), il.failcont(il.NONE), il.begin(il.append_failcont(compiler, il.SetParseState(il.Tuple(text, pos))), il.SetParseState(il.Tuple(text, p)), cont(arg))))) elif isinstance(arg, String): arg = arg.interlang() return il.Begin(( il.AssignFromList(text, pos, il.parse_state), il.Assign(length, il.Len(text)), il.Assign(length2, il.Len(arg)), il.Assign(i, il.Integer(0)), il.Assign(p, pos), il.while_(il.and_(il.Lt(i, length2), il.Lt(p, length), il.Eq(il.GetItem(text, p), il.GetItem(arg, i))), il.AddAssign(p, il.Integer(1)), il.AddAssign(i, il.Integer(1)), ), il.If(il.Lt(i, length2), il.failcont(il.NONE), il.begin(il.append_failcont(compiler, il.SetParseState(il.Tuple(text, pos))), il.SetParseState(il.Tuple(text, p)), cont(arg))))) else: raise CompileTypeError
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 skip(compiler, cont, n=il.Integer(1)): # return element after skip text = compiler.new_var(il.ConstLocalVar('text')) pos = compiler.new_var(il.ConstLocalVar('pos')) return il.Begin(( il.AssignFromList(text, pos, il.parse_state), il.append_failcont(compiler, il.SetParseState(il.Tuple(text, pos))), il.SetParseState(il.Tuple(text, il.add(pos, n))), cont(il.GetItem(text, il.add(pos, n)))))
def goto(compiler, cont, position): text = compiler.new_var(il.ConstLocalVar('text')) pos = compiler.new_var(il.ConstLocalVar('pos')) position = il.Integer(position.item) return il.Begin(( il.AssignFromList(text, pos, il.parse_state), il.append_failcont(compiler, il.SetParseState(il.Tuple(text, pos))), il.SetParseState(il.Tuple(text, position)), cont(il.GetItem(text, position))))
def unify_parse_sequence(compiler, cont, sequence): x = compiler.new_var(il.ConstLocalVar('x')) try: sequence.cps_convert_unify except: sequence = sequence.interlang() return il.If(il.Eq(sequence, il.GetItem(il.parse_state, il.Integer(0))), cont(TRUE), il.failcont(il.TRUE)) return il.Begin(( il.Assign(x, il.Deref(sequence.interlang())), il.If(il.IsLogicVar(x), il.begin(il.SetBinding(x, il.GetItem(il.parse_state, il.Integer(0))), il.append_failcont(compiler, il.DelBinding(x)), cont(il.TRUE)), il.If(il.Eq(x, il.GetItem(il.parse_state, il.Integer(0))), cont(TRUE), il.failcont(il.TRUE)))))
def next_element(compiler, cont): text = compiler.new_var(il.ConstLocalVar('text')) pos = compiler.new_var(il.ConstLocalVar('pos')) return il.Begin(( il.AssignFromList(text, pos, il.parse_state), cont(il.GetItem(text, pos))))