[pypy-svn] r23789 - in pypy/dist/pypy: interpreter/astcompiler interpreter/pyparser interpreter/pyparser/test interpreter/stablecompiler module/symbol
stuart at codespeak.net
stuart at codespeak.net
Tue Feb 28 22:51:28 CET 2006
Author: stuart
Date: Tue Feb 28 22:51:27 2006
New Revision: 23789
Modified:
pypy/dist/pypy/interpreter/astcompiler/ast.py
pypy/dist/pypy/interpreter/astcompiler/ast.txt
pypy/dist/pypy/interpreter/pyparser/astbuilder.py
pypy/dist/pypy/interpreter/pyparser/pysymbol.py
pypy/dist/pypy/interpreter/pyparser/pythonparse.py
pypy/dist/pypy/interpreter/pyparser/pythonutil.py
pypy/dist/pypy/interpreter/pyparser/test/test_astbuilder.py
pypy/dist/pypy/interpreter/stablecompiler/transformer.py
pypy/dist/pypy/module/symbol/__init__.py
Log:
(Arre, Ale, Stuart Williams)
Removed dependencies on symbol.py and pysymbol in order to make the
astcompiler and stablecompiler independent of each other (facilitating
grammar changes).
Modified: pypy/dist/pypy/interpreter/astcompiler/ast.py
==============================================================================
--- pypy/dist/pypy/interpreter/astcompiler/ast.py (original)
+++ pypy/dist/pypy/interpreter/astcompiler/ast.py Tue Feb 28 22:51:27 2006
@@ -4186,6 +4186,63 @@
else_=GetSetProperty(While.fget_else_, While.fset_else_ ),
)
+class With(Node):
+ def __init__(self, expr, body, var, lineno=-1):
+ Node.__init__(self, lineno)
+ self.expr = expr
+ self.body = body
+ self.var = var
+
+ def getChildren(self):
+ "NOT_RPYTHON"
+ return self.expr, self.body, self.var
+
+ def getChildNodes(self):
+ return [self.expr, self.body]
+
+ def __repr__(self):
+ return "With(%s, %s, %s)" % (self.expr.__repr__(), self.body.__repr__(), self.var.__repr__())
+
+ def accept(self, visitor):
+ return visitor.visitWith(self)
+
+ def fget_expr( space, self):
+ return space.wrap(self.expr)
+ def fset_expr( space, self, w_arg):
+ self.expr = space.interp_w(Node, w_arg, can_be_None=False)
+ def fget_body( space, self):
+ return space.wrap(self.body)
+ def fset_body( space, self, w_arg):
+ self.body = space.interp_w(Node, w_arg, can_be_None=False)
+ def fget_var( space, self):
+ return space.wrap(self.var)
+ def fset_var( space, self, w_arg):
+ self.var = space.str_w(w_arg)
+
+def descr_With_new(space, w_subtype, w_expr, w_body, w_var, lineno=-1):
+ self = space.allocate_instance(With, w_subtype)
+ expr = space.interp_w(Node, w_expr, can_be_None=False)
+ self.expr = expr
+ body = space.interp_w(Node, w_body, can_be_None=False)
+ self.body = body
+ var = space.str_w(w_var)
+ self.var = var
+ self.lineno = lineno
+ return space.wrap(self)
+
+def descr_With_accept( space, w_self, w_visitor):
+ w_callable = space.getattr(w_visitor, space.wrap('visitWith'))
+ args = Arguments(space, [ w_self ])
+ return space.call_args(w_callable, args)
+
+With.typedef = TypeDef('With', Node.typedef,
+ __new__ = interp2app(descr_With_new, unwrap_spec=[ObjSpace, W_Root, W_Root, W_Root, W_Root, int]),
+ accept=interp2app(descr_With_accept, unwrap_spec=[ObjSpace, W_Root, W_Root] ),
+ expr=GetSetProperty(With.fget_expr, With.fset_expr ),
+ body=GetSetProperty(With.fget_body, With.fset_body ),
+ var=GetSetProperty(With.fget_var, With.fset_var ),
+ )
+
class Yield(Node):
def __init__(self, value, lineno=-1):
Node.__init__(self, lineno)
@@ -4399,6 +4456,8 @@
return self.default( node )
def visitWhile(self, node):
return self.default( node )
+ def visitWith(self, node):
+ return self.default( node )
def visitYield(self, node):
return self.default( node )
Modified: pypy/dist/pypy/interpreter/astcompiler/ast.txt
==============================================================================
--- pypy/dist/pypy/interpreter/astcompiler/ast.txt (original)
+++ pypy/dist/pypy/interpreter/astcompiler/ast.txt Tue Feb 28 22:51:27 2006
@@ -27,6 +27,7 @@
Break:
Continue:
For: assign, list, body, else_&
+With: expr, body, var*str
While: test, body, else_&
If: tests!, else_&
Exec: expr, locals&, globals&
Modified: pypy/dist/pypy/interpreter/pyparser/astbuilder.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/astbuilder.py (original)
+++ pypy/dist/pypy/interpreter/pyparser/astbuilder.py Tue Feb 28 22:51:27 2006
@@ -5,11 +5,12 @@
from grammar import BaseGrammarBuilder, AbstractContext
from pypy.interpreter.astcompiler import ast, consts
-from pypy.interpreter.pyparser.pysymbol import _cpython_symbols as sym
+from pypy.interpreter.pyparser import pythonparse
import pypy.interpreter.pyparser.pytoken as tok
from pypy.interpreter.pyparser.error import SyntaxError
from pypy.interpreter.pyparser.parsestring import parsestr
+sym = pythonparse.PYTHON_PARSER.symbols
DEBUG_MODE = 0
### Parsing utilites #################################################
@@ -1236,6 +1237,21 @@
builder.push(ast.While(test, body, else_, atoms[0].lineno))
+def build_with_stmt(builder, nb):
+ """with_stmt: 'with' test [ 'as' NAME ] ':' suite"""
+ atoms = get_atoms(builder, nb)
+ # skip 'with'
+ test = atoms[1]
+ if len(atoms) == 4:
+ body = atoms[3]
+ var = None
+ # if there is an "as" clause
+ else:
+ var = atoms[3]
+ body = atoms[5]
+ builder.push(ast.With(test, body, var, atoms[0].lineno))
+
+
def build_import_name(builder, nb):
"""import_name: 'import' dotted_as_names
@@ -1484,6 +1500,7 @@
sym['break_stmt'] : build_break_stmt,
sym['for_stmt'] : build_for_stmt,
sym['while_stmt'] : build_while_stmt,
+# sym['with_stmt'] : build_with_stmt,
sym['import_name'] : build_import_name,
sym['import_from'] : build_import_from,
sym['yield_stmt'] : build_yield_stmt,
Modified: pypy/dist/pypy/interpreter/pyparser/pysymbol.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/pysymbol.py (original)
+++ pypy/dist/pypy/interpreter/pyparser/pysymbol.py Tue Feb 28 22:51:27 2006
@@ -1,9 +1,4 @@
-# replacement for the CPython symbol module
-try:
- from pypy.interpreter.pyparser import symbol
-except ImportError:
- # for standalone testing
- import symbol
+from pypy.interpreter.pyparser import symbol
# try to avoid numeric values conflict with tokens
# it's important for CPython, but I'm not so sure it's still
@@ -55,17 +50,17 @@
_cpython_symbols = SymbolMapper( symbol.sym_name )
-# prepopulate symbol table from symbols used by CPython
-for _value, _name in _cpython_symbols.sym_name.items():
- globals()[_name] = _value
+### prepopulate symbol table from symbols used by CPython
+##for _value, _name in _cpython_symbols.sym_name.items():
+## globals()[_name] = _value
-# (This function does not seen to be called right now)
-def update_symbols( parser ):
- """Update the symbol module according to rules
- in PythonParser instance : parser"""
- for rule in parser.rules:
- _cpython_symbols.add_symbol( rule )
+### (This function does not seen to be called right now)
+##def update_symbols( parser ):
+## """Update the symbol module according to rules
+## in PythonParser instance : parser"""
+## for rule in parser.rules:
+## _cpython_symbols.add_symbol( rule )
# There is no symbol in this module until the grammar is loaded
# once loaded the grammar parser will fill the mappings with the
Modified: pypy/dist/pypy/interpreter/pyparser/pythonparse.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/pythonparse.py (original)
+++ pypy/dist/pypy/interpreter/pyparser/pythonparse.py Tue Feb 28 22:51:27 2006
@@ -25,6 +25,7 @@
self.rules = grammar_builder.rules
# Build first sets for each rule (including anonymous ones)
grammar.build_first_sets(self.items)
+ self.symbols = grammar_builder.symbols
def parse_source(self, textsrc, goal, builder, flags=0):
"""Parse a python source according to goal"""
@@ -45,7 +46,7 @@
return self.parse_lines(lines, goal, builder, flags)
def parse_lines(self, lines, goal, builder, flags=0):
- goalnumber = pysymbol._cpython_symbols.sym_values[goal]
+ goalnumber = self.symbols.sym_values[goal]
target = self.rules[goalnumber]
src = Source(lines, flags)
Modified: pypy/dist/pypy/interpreter/pyparser/pythonutil.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/pythonutil.py (original)
+++ pypy/dist/pypy/interpreter/pyparser/pythonutil.py Tue Feb 28 22:51:27 2006
@@ -5,7 +5,6 @@
import pythonparse
from tuplebuilder import TupleBuilder
from astbuilder import AstBuilder
-from pypy.interpreter.pyparser import pysymbol
PYTHON_PARSER = pythonparse.PYTHON_PARSER
TARGET_DICT = {
@@ -73,10 +72,7 @@
"""NOT_RPYTHON"""
source_encoding, stack_element = parse_result
nested_tuples = stack_element.as_tuple(lineno)
- if source_encoding is not None:
- return (pysymbol.encoding_decl, nested_tuples, source_encoding)
- else:
- return nested_tuples
+ return nested_tuples
def pypy_parse(source, mode='exec', lineno=False, flags=0, parser = PYTHON_PARSER):
"""
Modified: pypy/dist/pypy/interpreter/pyparser/test/test_astbuilder.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/test/test_astbuilder.py (original)
+++ pypy/dist/pypy/interpreter/pyparser/test/test_astbuilder.py Tue Feb 28 22:51:27 2006
@@ -746,6 +746,7 @@
yield check_expression, expr, 'exec'
SNIPPETS = [
+# 'snippet_with_1.py',
'snippet_1.py',
'snippet_several_statements.py',
'snippet_simple_function.py',
Modified: pypy/dist/pypy/interpreter/stablecompiler/transformer.py
==============================================================================
--- pypy/dist/pypy/interpreter/stablecompiler/transformer.py (original)
+++ pypy/dist/pypy/interpreter/stablecompiler/transformer.py Tue Feb 28 22:51:27 2006
@@ -26,14 +26,26 @@
# and replace OWNER, ORGANIZATION, and YEAR as appropriate.
# make sure we import the parser with the correct grammar
-import pypy.interpreter.pyparser.pythonparse
+from pypy.interpreter.pyparser import pythonparse
+
+import pypy.interpreter.pyparser.pythonparse as pythonparse
+
from pypy.interpreter.stablecompiler.ast import *
import parser
-import pypy.interpreter.pyparser.pysymbol as symbol
import pypy.interpreter.pyparser.pytoken as token
import sys
-sym_name = symbol._cpython_symbols.sym_name
+# Create parser from Grammar_stable, not current grammar.
+stable_grammar, _ = pythonparse.get_grammar_file("stable")
+stable_parser = pythonparse.python_grammar(stable_grammar)
+
+sym_name = stable_parser.symbols.sym_name
+
+class symbol:
+ pass
+
+for value, name in sym_name.iteritems():
+ setattr(symbol, name, value)
# transforming is requiring a lot of recursion depth so make sure we have enough
if sys.getrecursionlimit()<5000:
Modified: pypy/dist/pypy/module/symbol/__init__.py
==============================================================================
--- pypy/dist/pypy/module/symbol/__init__.py (original)
+++ pypy/dist/pypy/module/symbol/__init__.py Tue Feb 28 22:51:27 2006
@@ -21,10 +21,10 @@
# Export the values from our custom symbol module.
# Skip negative values (the corresponding symbols are not visible in
# pure Python).
-from pypy.interpreter.pyparser import pysymbol
+from pypy.interpreter.pyparser.pythonparse import PYTHON_PARSER
sym_name = {}
-for val, name in pysymbol._cpython_symbols.sym_name.items():
+for val, name in PYTHON_PARSER.symbols.sym_name.items():
if val >= 0:
Module.interpleveldefs[name] = 'space.wrap(%d)' % val
sym_name[val] = name
More information about the Pypy-commit
mailing list