def compile(self, body): program = reformat_expr(body) ast = self.parse(program) imp = Importer(self.space) w_mod = compile_module(self.space, '<test>', 'self.test', program, ast, imp) return compile_bytecode(ast.elements[0], program, w_mod)
def run_code(fname): interpreter = Interpreter() space.setup(interpreter) try: source = open(fname).read() except (OSError, IOError): print "Error reading file %s" % fname return 1 # XXX error handling try: ast = parser.parse(lexer.lex(fname, source), ParsingState(fname, source)) except ParseError as pe: format_parser_error(pe) return 1 importer = Importer(space, dirname(os.path.abspath(fname)), parser, lexer) dotted_name = parse_name(fname) w_mod = compile_module(space, fname, dotted_name, source, ast, importer) w_mod.setup(space) try: space.call_method(w_mod, 'main', [], None) except AppError as e: os.write(2, format_traceback(space, e)) return 1 return 0
def run_code(fname): interpreter = Interpreter() space.setup(interpreter) try: source = open(fname).read() except (OSError, IOError): print "Error reading file %s" % fname return 1 # XXX error handling try: ast = parser.parse(lexer.lex(fname, source), ParsingState(fname, source)) except ParseError as pe: format_parser_error(pe) return 1 importer = Importer(space, dirname(os.path.abspath(fname)), parser, lexer) dotted_name = parse_name(fname) w_mod = compile_module(space, fname, dotted_name, source, ast, importer) w_mod.setup(space) try: space.call_method(w_mod, 'main', []) except AppError as e: os.write(2, format_traceback(space, e)) return 1 return 0
def interpret(self, code): interpreter = Interpreter() source = reformat_code(code) ast = self.parse(source) w_mod = compile_module(source, ast) self.space = Space(interpreter) w_mod.initialize(self.space) return self.space.call_method(w_mod, 'main', [])
def test_compile_module(self): code = reformat_code(''' function foo() { return 3; } ''') w_mod = compile_module(code, self.parse(code)) assert isinstance(w_mod, W_Module) assert w_mod.name2index == {'foo': 0}
def test_compile_module(self): code = reformat_code(''' def foo() { return 3; } ''') imp = Importer(self.space) w_mod = compile_module(self.space, 'test', 'self.test', code, self.parse(code), imp) assert isinstance(w_mod, W_Module) assert w_mod.name2index['foo'] == len(self.space.builtins_w)
def import_module(self, space, path): # XXX error handling pth = os.path.join(self.basepath, os.path.sep.join(path)) + ".q" try: source = open(pth).read() except (IOError, OSError): raise ImportError("foo") ast = self.parser.parse(self.lexer.lex(pth, source), ParsingState(pth, source)) dotted_name = ".".join(['self'] + path) w_mod = compile_module(space, pth, dotted_name, source, ast, self) return w_mod
def interpret(self, code, args=None): source = reformat_code(code) ast = self.parse(source) imp = Importer(self.space) w_mod = compile_module(self.space, 'test', 'self.test', source, ast, imp) w_mod.setup(self.space) if args is not None: args_w = [self.space.newlist([self.space.newtext(x) for x in args])] else: args_w = [] return self.space.call_method(w_mod, 'main', args_w, None)