def _test_compile_source(self, source, expected_value, *, show_source=False, show_ptree=False, show_ast=False, show_compiled=False, show_value=False): if show_source: print("The Source:") print(source) compiler = Compiler(source) if show_ptree: print() print("The Parse Tree:") pprint_pt(compiler.parse_tree) if show_ast: print() print("The Abstract Syntax Tree (AST):") pprint_ast(compiler.abstract_syntax_tree) program = compiler.compile() _, ptype, assns, rstmt = program if show_compiled: print() print("The Compiled Code ({0}):".format(ptype)) for line in assns: print(line) print(rstmt) value = Interpreter().execute(program) if show_value: print() print("Value:") print(value) self.assertEqual(value, expected_value)
def interpret(self, source): """Compile and execute source code. source is pxp source text. """ # Give the compiler all the non-global_scope scopes. compiler = Compiler(source, self.scope.scopes[1:]) program = compiler.compile() return self.execute(program)