if len(exps) == 1: return exps[0] else: result = [] for exp in exps: if isinstance(exp, SpecialCall) and exp.command is Begin: result += list(exp.args) else: result.append(exp) return Begin(*result) def Begin_fun(compiler, cont, *exps): return cps_convert_exps(compiler, exps, cont) Begin = special(Begin_fun) @special def if_(compiler, cont, test, then, else_): v = compiler.new_var(il.ConstLocalVar('v')) return test.cps( compiler, il.Clamda( v, il.If(v, then.cps(compiler, cont), else_.cps(compiler, cont)))) def cps_convert_exps(compiler, exps, cont): v = compiler.new_var(il.ConstLocalVar('v')) if not exps: return il.PassStatement() if len(exps) == 1:
def begin(*exps): exps = tuple(element(e) for e in exps) if len(exps)==1: return exps[0] else: result = [] for exp in exps: if isinstance(exp, SpecialCall) and exp.command is Begin: result += list(exp.args) else: result.append(exp) return Begin(*result) def Begin_fun(compiler, cont, *exps): return cps_convert_exps(compiler, exps, cont) Begin = special(Begin_fun) @special def if_(compiler, cont, test, then, else_): v = compiler.new_var(il.ConstLocalVar('v')) return test.cps(compiler, il.Clamda(v, il.If(v, then.cps(compiler, cont), else_.cps(compiler, cont)))) def cps_convert_exps(compiler, exps, cont): v = compiler.new_var(il.ConstLocalVar('v')) if not exps: return il.PassStatement() if len(exps)==1: return exps[0].cps(compiler, cont) else: return exps[0].cps(compiler,
il.Assign(il.parse_state, parse_state), il.SetBindings(bindings), fc(il.FALSE))), il.SetFailCont(il.clamda(v1, il.SetFailCont(fc), clause2.cps(compiler, or_cont))), clause1.cps(compiler, or_cont)) else: return il.begin( il.Assign(fc, il.failcont), il.SetFailCont(il.clamda(v1, il.SetFailCont(fc), clause2.cps(compiler, cont))), clause1.cps(compiler, cont)) or2 = special(or2_fun) @special def first_(compiler, cont, clause1, clause2): v = compiler.new_var(il.ConstLocalVar('v')) fc = compiler.new_var(il.ConstLocalVar('fc')) first_cont = il.clamda(v, il.SetFailCont(fc), cont(v)) return il.begin( il.Assign(fc, il.failcont), il.append_failcont(compiler, clause2.cps(compiler, first_cont)), clause1.cps(compiler, first_cont)) def first_p(*exps): if not exps: raise elif len(exps)==1: return once(exps[0]) elif len(exps)==2: return first_(*exps)