def macrorules(*rules): result = [] for rule in rules: head = tuple(element(x) for x in rule[0]) body = begin(*(element(x) for x in rule[1:])) result.append((head, body)) return MacroRules(result)
def compile_to_python(exp, env, done=None): '''assemble steps from dao expression to python code''' original_exp = exp compiler = Compiler() if done is None: done = il.Done(compiler.new_var(il.ConstLocalVar('v'))) compiler.exit_block_cont_map = {} compiler.continue_block_cont_map = {} compiler.protect_cont = done if env is None: env = Environment() exp = element(exp) exp = exp.alpha(env, compiler) exp = exp.cps(compiler, done) exp.analyse(compiler) env = Environment() exp = exp.optimize(env, compiler) #exp = exp.tail_recursive_convert() function = compiler.new_var(il.ConstLocalVar('compiled_dao_function')) exp = il.Function(function, (), exp) exp = il.begin(*exp.pythonize(env, compiler)[0]) if isinstance(exp, il.Begin): exp = exp.statements[0] exp.body = exp.body.replace_return_with_yield() compiler = Compiler() result = exp.to_code(compiler) return prelude + result
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(*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 unquote_splice(item): return UnquoteSplice(element(item))
def letrec(bindings, *body): return Letrec(tuple((element(var), element(value)) for var, value in bindings), begin(*tuple(element(exp) for exp in body)))
def let(bindings, *body): bindings = tuple((var, element(value)) for var, value in bindings) return Let(bindings, begin(*body))
def lamda(params, *body): body = tuple(element(x) for x in body) return Lamda(params, begin(*body))
def exit_block(label=NONE, value=NONE): return ExitBlock(element(label), element(value))
def block(label, *exps): return Block(label, begin(*tuple(element(x) for x in exps)))
def continue_block(label=NONE): return ContinueBlock(element(label))
def quasiquote(item): return Quasiquote(element(item))
def __call__(self, *args): return Apply(self, tuple(element(arg) for arg in args))
def macro(params, *body): body = tuple(element(x) for x in body) return Macro(params, begin(*body))
def letrec(bindings, *body): return Letrec( tuple((element(var), element(value)) for var, value in bindings), begin(*tuple(element(exp) for exp in body)) )
def unquote(item): return Unquote(element(item))