def __init__(self, local_dict, global_dict): Transformer.__init__(self) self.symbol_class = 'Symbol' self.local_dict = local_dict self.global_dict = global_dict
def compile(self): tknizer = Tokenizer(self.input_code) parser = Parser(tknizer.run()) transformer = Transformer(parser.run()) code_generator = CodeGenerator(transformer.run()) return code_generator.run()
def parse(buf, mode="exec", transformer=None): 'Extends compiler.parse to take a transformer.' if transformer is None: transformer = Transformer() if mode == "exec" or mode == "single": return transformer.parsesuite(buf) elif mode == "eval": return transformer.parseexpr(buf) else: raise ValueError("compile() arg 3 must be" " 'exec' or 'eval' or 'single'")
def atom_number(self, nodelist): n = Transformer.atom_number(self, nodelist) number, lineno = nodelist[0][1:] if _is_integer(number): n = Const(long(number), lineno) return CallFunc(Name('Integer'), [n]) if number.endswith('j'): n = Const(complex(number), lineno) return CallFunc(Name('sympify'), [n]) n = Const(number, lineno) return CallFunc(Name('Real'), [n])
def atom_number(self, nodelist): n = Transformer.atom_number(self, nodelist) number, lineno = nodelist[0][1:] if _is_integer(number): n = Const(long(number), lineno) return CallFunc(Name("Integer"), [n]) if number.endswith("j"): n = Const(complex(number), lineno) return CallFunc(Name("sympify"), [n]) n = Const(number, lineno) return CallFunc(Name("Float"), [n])
def _check_tuples_equality(pypy_tuples, python_tuples, testname): # compare the two tuples by transforming them into AST, to hide irrelevant # differences -- typically newlines at the end of the tree. print 'Comparing the ASTs of', testname transformer1 = PyPyTransformer("") ast_pypy = transformer1.compile_node(pypy_tuples) repr_pypy = repr(ast_pypy) key = os.path.basename(testname) if key not in REAL_EXPECTED_OUTPUT: transformer2 = PythonTransformer() ast_python = transformer2.compile_node(python_tuples) repr_python = repr(ast_python) else: repr_python = REAL_EXPECTED_OUTPUT[key] if GRAMMAR_MISMATCH: # XXX hack: # hide the more common difference between 2.3 and 2.4, which is # Function(None, ...) where 'None' stands for no decorator in 2.4 repr_pypy = repr_pypy.replace("Function(None, ", "Function(") repr_python = repr_python.replace("Function(None, ", "Function(") # XXX hack(bis): # we changed stablecompiler to use [] instead of () in several # places (for consistency), so let's make sure the test won't fail # because of that (the workaround is as drastic as the way we # compare python and pypy tuples :-), but we'll change that with # astbuilder.py repr_pypy = repr_pypy.replace("[]", "()") repr_python = repr_python.replace("[]", "()") # We also changed constants 'OP_ASSIGN' 'OP_DELETE' 'OP_APPLY' to use numeric values repr_python = repr_python.replace("'OP_ASSIGN'", repr(OP_ASSIGN) ) repr_python = repr_python.replace("'OP_DELETE'", repr(OP_DELETE) ) repr_python = repr_python.replace("'OP_APPLY'", repr(OP_APPLY) ) assert repr_pypy == repr_python
def parse(buf, mode="exec", transformer=None): 'Extends compiler.parse to take a transformer.' if transformer is None: transformer = Transformer() if mode == "exec" or mode == "single": # Since the parser gives a SyntaxError with the 'with' keyword, # add the import along with the buffer, then remove it. The lineno # on the ast nodes must also be adjusted py_version = sys.version_info py_version = int(py_version[0]) + 0.1 * int(py_version[1]) if py_version < 2.6: new_buf = 'from __future__ import with_statement\n' + buf ast = transformer.parsesuite(new_buf) ast.node.nodes = ast.node.nodes[1:] for node in ast.node.nodes: if node.lineno is not None: node.lineno -= 1 return ast return transformer.parsesuite(buf) elif mode == "eval": return transformer.parseexpr(buf) else: raise ValueError("compile() arg 3 must be" " 'exec' or 'eval' or 'single'")
def parse(buf, mode="exec", transformer=None): 'Extends compiler.parse to take a transformer.' if transformer is None: transformer = Transformer() if mode == "exec" or mode == "single": # Since the parser gives a SyntaxError with the 'with' keyword, # add the import along with the buffer, then remove it. The lineno # on the ast nodes must also be adjusted py_version = sys.version_info py_version = int(py_version[0])+0.1*int(py_version[1]) if py_version < 2.6: new_buf = 'from __future__ import with_statement\n' + buf ast = transformer.parsesuite(new_buf) ast.node.nodes = ast.node.nodes[1:] for node in ast.node.nodes: if node.lineno is not None: node.lineno -= 1 return ast return transformer.parsesuite(buf) elif mode == "eval": return transformer.parseexpr(buf) else: raise ValueError("compile() arg 3 must be" " 'exec' or 'eval' or 'single'")