def compile(self, source, filepath, initial_lineno=1): symtable = SymbolTable() astnode = self.parse(source, initial_lineno=initial_lineno, symtable=symtable) ctx = CompilerContext(self, "<main>", symtable, filepath) with ctx.set_lineno(initial_lineno): astnode.compile(ctx) return ctx.create_bytecode([], [], None, None)
def compile(self, ctx): function_ctx = ctx.get_subctx(self.name, self) defaults = [] arg_names = [] for arg in self.args: assert isinstance(arg, Argument) arg_names.append(arg.name) if function_ctx.symtable.is_local(arg.name): function_ctx.symtable.get_local_num(arg.name) elif function_ctx.symtable.is_cell(arg.name): function_ctx.symtable.get_cell_num(arg.name) arg_ctx = CompilerContext(ctx.space, self.name, function_ctx.symtable, ctx.filepath) if arg.defl is not None: arg.defl.compile(arg_ctx) arg_ctx.emit(consts.RETURN) bc = arg_ctx.create_bytecode([], [], None, None) defaults.append(bc) if self.splat_arg is not None: if function_ctx.symtable.is_local(self.splat_arg): function_ctx.symtable.get_local_num(self.splat_arg) elif function_ctx.symtable.is_cell(self.splat_arg): function_ctx.symtable.get_cell_num(self.splat_arg) if self.block_arg is not None: if function_ctx.symtable.is_local(self.block_arg): function_ctx.symtable.get_local_num(self.block_arg) elif function_ctx.symtable.is_cell(self.block_arg): function_ctx.symtable.get_cell_num(self.block_arg) self.body.compile(function_ctx) function_ctx.emit(consts.RETURN) bytecode = function_ctx.create_bytecode( arg_names, defaults, self.splat_arg, self.block_arg ) if self.parent is None: ctx.emit(consts.LOAD_SCOPE) else: self.parent.compile(ctx) ctx.emit(consts.LOAD_CONST, ctx.create_symbol_const(self.name)) ctx.emit(consts.LOAD_CONST, ctx.create_symbol_const(self.name)) ctx.emit(consts.LOAD_CONST, ctx.create_const(bytecode)) ctx.emit(consts.BUILD_FUNCTION) if self.parent is None: ctx.emit(consts.DEFINE_FUNCTION) else: ctx.emit(consts.ATTACH_FUNCTION)
def compile(self, ctx): function_ctx = ctx.get_subctx(self.name, self) defaults = [] arg_names = [] for arg in self.args: assert isinstance(arg, Argument) arg_names.append(arg.name) if function_ctx.symtable.is_local(arg.name): function_ctx.symtable.get_local_num(arg.name) elif function_ctx.symtable.is_cell(arg.name): function_ctx.symtable.get_cell_num(arg.name) arg_ctx = CompilerContext(ctx.space, self.name, function_ctx.symtable, ctx.filepath) if arg.defl is not None: arg.defl.compile(arg_ctx) arg_ctx.emit(consts.RETURN) bc = arg_ctx.create_bytecode([], [], None, None) defaults.append(bc) if self.splat_arg is not None: if function_ctx.symtable.is_local(self.splat_arg): function_ctx.symtable.get_local_num(self.splat_arg) elif function_ctx.symtable.is_cell(self.splat_arg): function_ctx.symtable.get_cell_num(self.splat_arg) if self.block_arg is not None: if function_ctx.symtable.is_local(self.block_arg): function_ctx.symtable.get_local_num(self.block_arg) elif function_ctx.symtable.is_cell(self.block_arg): function_ctx.symtable.get_cell_num(self.block_arg) self.body.compile(function_ctx) function_ctx.emit(consts.RETURN) bytecode = function_ctx.create_bytecode(arg_names, defaults, self.splat_arg, self.block_arg) if self.parent is None: ctx.emit(consts.LOAD_SCOPE) else: self.parent.compile(ctx) ctx.emit(consts.LOAD_CONST, ctx.create_symbol_const(self.name)) ctx.emit(consts.LOAD_CONST, ctx.create_symbol_const(self.name)) ctx.emit(consts.LOAD_CONST, ctx.create_const(bytecode)) ctx.emit(consts.BUILD_FUNCTION) if self.parent is None: ctx.emit(consts.DEFINE_FUNCTION) else: ctx.emit(consts.ATTACH_FUNCTION)