def define_syntax(self, vm, bindings): rules = zip(bindings[Symbol('pattern')], bindings[Symbol('template')]) compiler = mania.compiler.SimpleCompiler(types.Nil()) for pattern, templates in rules: compiler.compile_any(pattern) compiler.builder.add(instructions.BuildPattern()) for template in templates: compiler.compile_any(template) compiler.builder.add(instructions.BuildTemplate(len(templates))) compiler.builder.add(instructions.BuildRule()) compiler.builder.add(instructions.BuildMacro(len(rules))) compiler.builder.add(instructions.Duplicate(1)) compiler.builder.add( instructions.Store( compiler.builder.constant(bindings[Symbol('name')]))) module = compiler.builder.module return [ module.code(module.entry_point, len(module) - module.entry_point) ]
def define_function(self, vm, bindings): name = bindings[Symbol('name')] if ':' in name.value and any(c != ':' for c in name.value): raise mania.types.ExpandError() compiler = self.compile_function(bindings) compiler.builder.add(instructions.Duplicate(1)) compiler.builder.add( instructions.Store(compiler.builder.constant(name))) module = compiler.builder.module return [ module.code(module.entry_point, len(module) - module.entry_point) ]
def and_(self, vm, bindings): compiler = mania.compiler.SimpleCompiler(types.Nil()) compiler.compile_any(bindings[Symbol('left')]) compiler.builder.add(instructions.Eval()) compiler.builder.add(instructions.Duplicate(1)) left_false = compiler.builder.add(None) compiler.builder.add(instructions.Pop(1)) compiler.compile_any(bindings[Symbol('right')]) compiler.builder.add(instructions.Eval()) end = compiler.builder.add(instructions.Restore()) compiler.builder.replace(left_false, instructions.JumpIfFalse(end)) module = compiler.builder.module return [module.code(module.entry_point, len(module))]
def define_value(vm, bindings): name = bindings[Symbol('name')] if ':' in name.value and any(c != ':' for c in name.value): raise types.ExpandError() compiler = mania.compiler.SimpleCompiler(types.Nil()) compiler.compile_any(bindings[Symbol('value')]) compiler.builder.add(instructions.Eval()) compiler.builder.add(instructions.Duplicate(1)) compiler.builder.add( instructions.Store(compiler.builder.constant(name))) module = compiler.builder.module return [ module.code(module.entry_point, len(module) - module.entry_point) ]
def define_values(self, vm, bindings): compiler = mania.compiler.SimpleCompiler(types.Nil()) compiler.compile_any(bindings[Symbol('body')]) compiler.builder.add(instructions.Eval()) for name in bindings[Symbol('names')]: compiler.builder.add(instructions.Duplicate(1)) compiler.builder.add(instructions.Head()) compiler.builder.add( instructions.Store(compiler.builder.constant(name))) compiler.builder.add(instructions.Tail()) compiler.builder.add(instructions.Pop(1)) module = compiler.builder.module return [ module.code(module.entry_point, len(module) - module.entry_point) ]