[pypy-svn] r22399 - in pypy/branch/ast-experiments/pypy: interpreter interpreter/astcompiler module/recparser/hooksamples
adim at codespeak.net
adim at codespeak.net
Wed Jan 18 17:27:39 CET 2006
Author: adim
Date: Wed Jan 18 17:27:36 2006
New Revision: 22399
Added:
pypy/branch/ast-experiments/pypy/module/recparser/hooksamples/
pypy/branch/ast-experiments/pypy/module/recparser/hooksamples/constchanger.py
pypy/branch/ast-experiments/pypy/module/recparser/hooksamples/tracer.py
Modified:
pypy/branch/ast-experiments/pypy/interpreter/astcompiler/ast.py
pypy/branch/ast-experiments/pypy/interpreter/astcompiler/astgen.py
pypy/branch/ast-experiments/pypy/interpreter/pycompiler.py
Log:
- added a parent attribute to AST nodes
- added a few hook examples (they will probably be used as a basis for improvements)
Modified: pypy/branch/ast-experiments/pypy/interpreter/astcompiler/ast.py
==============================================================================
--- pypy/branch/ast-experiments/pypy/interpreter/astcompiler/ast.py (original)
+++ pypy/branch/ast-experiments/pypy/interpreter/astcompiler/ast.py Wed Jan 18 17:27:36 2006
@@ -31,6 +31,7 @@
def __init__(self, lineno = -1):
self.lineno = lineno
self.filename = ""
+ self.parent = None
#self.scope = None
def getChildren(self):
@@ -60,6 +61,12 @@
def descr_repr( self, space ):
return space.wrap( self.__repr__() )
+ def fget_parent(space, self):
+ return space.wrap(self.parent)
+
+ def fset_parent(space, self, w_parent):
+ self.parent = space.interp_w(Node, w_parent, can_be_None=False)
+
def descr_getChildNodes( self, space ):
lst = self.getChildNodes()
return space.newlist( [ space.wrap( it ) for it in lst ] )
@@ -81,6 +88,7 @@
accept = interp2app(descr_node_accept, unwrap_spec=[ ObjSpace, W_Root, W_Root ] ),
lineno = interp_attrproperty('lineno', cls=Node),
filename = interp_attrproperty('filename', cls=Node),
+ parent=GetSetProperty(Node.fget_parent, Node.fset_parent),
)
Modified: pypy/branch/ast-experiments/pypy/interpreter/astcompiler/astgen.py
==============================================================================
--- pypy/branch/ast-experiments/pypy/interpreter/astcompiler/astgen.py (original)
+++ pypy/branch/ast-experiments/pypy/interpreter/astcompiler/astgen.py Wed Jan 18 17:27:36 2006
@@ -573,6 +573,7 @@
def __init__(self, lineno = -1):
self.lineno = lineno
self.filename = ""
+ self.parent = None
#self.scope = None
def getChildren(self):
@@ -602,6 +603,12 @@
def descr_repr( self, space ):
return space.wrap( self.__repr__() )
+ def fget_parent(space, self):
+ return space.wrap(self.parent)
+
+ def fset_parent(space, self, w_parent):
+ self.parent = space.interp_w(Node, w_parent, can_be_None=False)
+
def descr_getChildNodes( self, space ):
lst = self.getChildNodes()
return space.newlist( [ space.wrap( it ) for it in lst ] )
@@ -623,6 +630,7 @@
accept = interp2app(descr_node_accept, unwrap_spec=[ ObjSpace, W_Root, W_Root ] ),
lineno = interp_attrproperty('lineno', cls=Node),
filename = interp_attrproperty('filename', cls=Node),
+ parent=GetSetProperty(Node.fget_parent, Node.fset_parent),
)
Modified: pypy/branch/ast-experiments/pypy/interpreter/pycompiler.py
==============================================================================
--- pypy/branch/ast-experiments/pypy/interpreter/pycompiler.py (original)
+++ pypy/branch/ast-experiments/pypy/interpreter/pycompiler.py Wed Jan 18 17:27:36 2006
@@ -13,7 +13,7 @@
def __init__(self, space):
self.space = space
- self.w_compile_hook = space.w_None
+ self.w_compile_hook = space.w_None
def compile(self, source, filename, mode, flags):
"""Compile and return an pypy.interpreter.eval.Code instance."""
@@ -220,8 +220,8 @@
raise OperationError(space.w_SyntaxError,
e.wrap_info(space, filename))
- if not space.is_w(self.w_compile_hook, space.w_None):
- w_ast_tree = space.call_function(self.w_compile_hook,
+ if not space.is_w(self.w_compile_hook, space.w_None):
+ w_ast_tree = space.call_function(self.w_compile_hook,
space.wrap(ast_tree),
space.wrap(encoding))
ast_tree = space.interp_w(Node, w_ast_tree)
Added: pypy/branch/ast-experiments/pypy/module/recparser/hooksamples/constchanger.py
==============================================================================
--- (empty file)
+++ pypy/branch/ast-experiments/pypy/module/recparser/hooksamples/constchanger.py Wed Jan 18 17:27:36 2006
@@ -0,0 +1,21 @@
+class ChangeConstVisitor:
+ def visitConst(self, node):
+ if node.value == 3:
+ node.value = 2
+
+ def defaultvisit(self, node):
+ for child in node.getChildNodes():
+ child.accept(self)
+
+ def __getattr__(self, attrname):
+ if attrname.startswith('visit'):
+ return self.defaultvisit
+ raise AttributeError(attrname)
+
+def threebecomestwo(ast, enc):
+ ast.accept(ChangeConstVisitor())
+ return ast
+
+# install the hook
+import parser
+parser.install_compiler_hook(threebecomestwo)
Added: pypy/branch/ast-experiments/pypy/module/recparser/hooksamples/tracer.py
==============================================================================
--- (empty file)
+++ pypy/branch/ast-experiments/pypy/module/recparser/hooksamples/tracer.py Wed Jan 18 17:27:36 2006
@@ -0,0 +1,42 @@
+"""this one logs simple assignments and somewhat clearly shows
+that we need a nice API to define "joinpoints". Maybe a SAX-like
+(i.e. event-based) API ?
+
+XXX: crashes on everything else than simple assignment (AssAttr, etc.)
+"""
+
+from parser import ASTPrintnl, ASTConst, ASTName
+from parser import install_compiler_hook
+
+class Tracer:
+ def visitModule(self, module):
+ module.node = module.node.accept(self)
+ return module
+
+ def default(self, node):
+ for child in node.getChildNodes():
+ # let's cheat a bit
+ child.parent = node
+ child.accept(self)
+ return node
+
+ def visitAssign(self, assign):
+ stmt = assign.parent
+ varname = assign.nodes[0].name
+ lognode = ASTPrintnl([ASTConst('%s <--' % varname), ASTName(varname)], None)
+ index = stmt.nodes.index(assign)
+ newstmts = stmt.nodes
+ newstmts.insert(index + 1, lognode)
+ stmt.nodes = newstmts
+ return assign
+
+ def __getattr__(self, attrname):
+ if attrname.startswith('visit'):
+ return self.default
+ raise AttributeError('No such attribute: %s' % attrname)
+
+
+def _trace(ast, enc):
+ return ast.accept(Tracer())
+
+install_compiler_hook(_trace)
More information about the Pypy-commit
mailing list