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 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 findall_2(compiler, cont, goal, template, bag): v = compiler.new_var(il.ConstLocalVar('v')) v2 = compiler.new_var(il.ConstLocalVar('v')) fc = compiler.new_var(il.ConstLocalVar('old_failcont')) bag = bag.interlang() template = template.interlang() return il.begin( il.Assign(bag, il.empty_list), il.Assign(fc, il.failcont), il.SetFailCont(il.clamda(v2, il.SetFailCont(fc), cont(v2))), goal.cps(compiler, il.clamda(v, il.ListAppend(bag, il.GetValue(template)), il.failcont(v))))
def greedy_some2(compiler, cont, item, template, result): template = template.interlang() result = result.interlang() item_matched = compiler.new_var(il.Var('item_matched')) v = compiler.new_var(il.ConstLocalVar('v')) v2 = compiler.new_var(il.ConstLocalVar('v')) fc = compiler.new_var(il.ConstLocalVar('old_failcont')) greedy_some_fcont = compiler.new_var(il.ConstLocalVar('greedy_some_fcont')) greedy_some_cont = compiler.new_var(il.ConstLocalVar('greedy_some_cont')) append_result_cont = il.clamda( v2, il.ListAppend(result, il.GetValue(template)), greedy_some_cont(il.TRUE)) return il.begin( il.Assign(result, il.empty_list), il.Assign(fc, il.failcont), il.cfunction(greedy_some_fcont, v, il.SetFailCont(fc), cont(il.TRUE)), il.cfunction(greedy_some_cont, v, il.SetFailCont(greedy_some_fcont), item.cps(compiler, append_result_cont)), item.cps(compiler, append_result_cont))
def lazy_any2(compiler, cont, item, template, result): template = template.interlang() result = result.interlang() fc = compiler.new_var(il.ConstLocalVar('fc')) lazy_any_cont = compiler.new_var(il.ConstLocalVar('lazy_any_cont')) lazy_any_fcont = compiler.new_var(il.ConstLocalVar('lazy_any_fcont')) v = compiler.new_var(il.ConstLocalVar('v')) v1 = compiler.new_var(il.ConstLocalVar('v')) v2 = compiler.new_var(il.ConstLocalVar('v')) return il.begin( il.Assign(result, il.empty_list), il.Assign(fc, il.failcont), il.cfunction( lazy_any_fcont, v, il.SetFailCont(fc), item.cps( compiler, il.clamda(v2, il.ListAppend(result, il.GetValue(template)), lazy_any_cont(il.TRUE)))), il.cfunction(lazy_any_cont, v, il.SetFailCont(lazy_any_fcont), 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))