[pypy-svn] r16208 - in pypy/dist/pypy: interpreter interpreter/pyparser tool

ludal at codespeak.net ludal at codespeak.net
Mon Aug 22 16:28:19 CEST 2005


Author: ludal
Date: Mon Aug 22 16:28:17 2005
New Revision: 16208

Modified:
   pypy/dist/pypy/interpreter/baseobjspace.py
   pypy/dist/pypy/interpreter/pycompiler.py
   pypy/dist/pypy/interpreter/pyparser/astbuilder.py
   pypy/dist/pypy/interpreter/pyparser/pythonutil.py
   pypy/dist/pypy/tool/option.py
Log:
(ludal,nik)
 - adds an PythonAstCompiler class that uses the astbuilder module
 - provide a command line option --compiler=astparser to use it


Modified: pypy/dist/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/dist/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/dist/pypy/interpreter/baseobjspace.py	Mon Aug 22 16:28:17 2005
@@ -1,7 +1,7 @@
 from pypy.interpreter.executioncontext import ExecutionContext
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.argument import Arguments
-from pypy.interpreter.pycompiler import CPythonCompiler
+from pypy.interpreter.pycompiler import CPythonCompiler, PythonAstCompiler
 from pypy.interpreter.pycompiler import PythonCompiler, PyPyCompiler
 from pypy.interpreter.miscutils import ThreadLocals
 from pypy.tool.cache import Cache 
@@ -258,6 +258,8 @@
                 compiler = PythonCompiler(self)
             elif self.options.compiler == 'cpython':
                 compiler = CPythonCompiler(self)
+            elif self.options.compiler == 'astparser':
+                compiler = PythonAstCompiler(self)
             else:
                 raise ValueError('unknown --compiler option value: %r' % (
                     self.options.compiler,))

Modified: pypy/dist/pypy/interpreter/pycompiler.py
==============================================================================
--- pypy/dist/pypy/interpreter/pycompiler.py	(original)
+++ pypy/dist/pypy/interpreter/pycompiler.py	Mon Aug 22 16:28:17 2005
@@ -232,7 +232,6 @@
         return PyCode(space)._from_code(c)
     compile_parse_result._annspecialcase_ = 'override:cpy_stablecompiler'
 
-
 class PythonCompilerApp(PythonCompiler):
     """Temporary.  Calls the stablecompiler package at app-level."""
 
@@ -294,6 +293,64 @@
         return code
 
 
+class PythonAstCompiler(CPythonCompiler):
+    """Uses the stdlib's python implementation of compiler
+
+    XXX: This class should override the baseclass implementation of
+         compile_command() in order to optimize it, especially in case
+         of incomplete inputs (e.g. we shouldn't re-compile from sracth
+         the whole source after having only added a new '\n')
+    """
+    def compile(self, source, filename, mode, flags):
+        from pyparser.error import ParseError
+        from pyparser.pythonutil import internal_pypy_parse_to_ast
+        flags |= __future__.generators.compiler_flag   # always on (2.2 compat)
+        # XXX use 'flags'
+        space = self.space
+        try:
+            encoding, ast_tree = internal_pypy_parse_to_ast(source, mode, True, flags)
+        except ParseError, e:
+            raise OperationError(space.w_SyntaxError,
+                                 e.wrap_info(space, filename))
+        return self.compile_parse_result(ast_tree, filename, mode)
+
+    def compile_parse_result(self, ast_tree, filename, mode):
+        """NOT_RPYTHON"""
+        # __________
+        # XXX this uses the non-annotatable astcompiler at interp-level
+        from pypy.interpreter import astcompiler
+        from pypy.interpreter.astcompiler.pycodegen import ModuleCodeGenerator
+        from pypy.interpreter.astcompiler.pycodegen import InteractiveCodeGenerator
+        from pypy.interpreter.astcompiler.pycodegen import ExpressionCodeGenerator
+        space = self.space
+        try:
+            astcompiler.misc.set_filename(filename, ast_tree) ### TODO: doesn't work
+            if mode == 'exec':
+                codegenerator = ModuleCodeGenerator(ast_tree)
+            elif mode == 'single':
+                codegenerator = InteractiveCodeGenerator(ast_tree)
+            else: # mode == 'eval':
+                codegenerator = ExpressionCodeGenerator(ast_tree)
+            c = codegenerator.getCode()
+        except SyntaxError, e:
+            w_synerr = space.newtuple([space.wrap(e.msg),
+                                       space.newtuple([space.wrap(e.filename),
+                                                       space.wrap(e.lineno),
+                                                       space.wrap(e.offset),
+                                                       space.wrap(e.text)])])
+            raise OperationError(space.w_SyntaxError, w_synerr)
+        except ValueError,e:
+            raise OperationError(space.w_ValueError,space.wrap(str(e)))
+        except TypeError,e:
+            raise
+            raise OperationError(space.w_TypeError,space.wrap(str(e)))
+        # __________ end of XXX above
+        from pypy.interpreter.pycode import PyCode
+        return PyCode(space)._from_code(c)
+    compile_parse_result._annspecialcase_ = 'override:cpy_stablecompiler'
+
+
+
 class PyPyCompiler(CPythonCompiler):
     """Uses the PyPy implementation of Compiler
 

Modified: pypy/dist/pypy/interpreter/pyparser/astbuilder.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/astbuilder.py	(original)
+++ pypy/dist/pypy/interpreter/pyparser/astbuilder.py	Mon Aug 22 16:28:17 2005
@@ -413,6 +413,7 @@
     from pypy.objspace.std.strutil import ParseStringError
     if value.endswith('l') or value.endswith('L'):
         value = value[:-1]
+        return string_to_long(value) # ???
     try:
         return string_to_int(value)
     except ParseStringError:

Modified: pypy/dist/pypy/interpreter/pyparser/pythonutil.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/pythonutil.py	(original)
+++ pypy/dist/pypy/interpreter/pyparser/pythonutil.py	Mon Aug 22 16:28:17 2005
@@ -107,6 +107,15 @@
     PYTHON_PARSER.parse_source(input, target, builder)
     return builder.rule_stack[-1]
 
+
+def internal_pypy_parse_to_ast(source, mode='exec', lineno=False, flags=0):
+    builder = AstBuilder()
+    target_rule = TARGET_DICT[mode]
+    PYTHON_PARSER.parse_source(source, target_rule, builder, flags)
+    ast_tree = builder.rule_stack[-1]
+    return (builder.source_encoding, ast_tree)
+
+
 ## TARGET FOR ANNOTATORS #############################################
 def annotateme(source):
     """This function has no other role than testing the parser's annotation

Modified: pypy/dist/pypy/tool/option.py
==============================================================================
--- pypy/dist/pypy/tool/option.py	(original)
+++ pypy/dist/pypy/tool/option.py	Mon Aug 22 16:28:17 2005
@@ -13,8 +13,9 @@
     nofaking = 0
     parser = "recparser" # "cpython" / "recparser" / "parser"
     compiler = "pyparse" # "cpython"
-                         # "pyparse" pypy parser, cpython's compiler package
+                         # "pyparse" pypy parser, cpython's compiler and transformer package
                          # "pyparseapp" same, running the compiler at app-level
+                         # "astparser" pypy parser with ast builder using cpython's compiler
                          # "pycomp" pypy parser and compiler (TBD)
     usemodules = []                        
     version = "2.4" # "native" / "2.3" / "2.4"
@@ -55,7 +56,7 @@
     options.append(make_option(
         '--compiler', action="store", type="string", dest="compiler",
         help="select the compiler approach to use internally",
-        metavar="[pyparser|cpython|pyparseapp]"))
+        metavar="[pyparse|astparser|cpython|pyparseapp]"))
     options.append(make_option(
         '--parser', action="store",type="string", dest="parser",
         help="select the parser module to use",



More information about the Pypy-commit mailing list