Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f635abe

Browse files
committed
simplify visitor walker class
- remove postorder - remove protocol for automatically walking children based on visitor method return value; now only walks if there is no method
1 parent b631b8e commit f635abe

2 files changed

Lines changed: 44 additions & 64 deletions

File tree

Lib/compiler/visitor.py

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,40 +47,27 @@ def preorder(self, tree, visitor):
4747
self._preorder(tree)
4848

4949
def _preorder(self, node, *args):
50-
stop = apply(self.dispatch, (node,) + args)
51-
if stop:
52-
return
53-
for child in node.getChildren():
54-
if isinstance(child, ast.Node):
55-
self._preorder(child)
56-
57-
def postorder(self, tree, visitor):
58-
"""Do preorder walk of tree using visitor"""
59-
self.visitor = visitor
60-
visitor.visit = self._postorder
61-
self._postorder(tree)
50+
return apply(self.dispatch, (node,) + args)
6251

63-
def _postorder(self, tree, *args):
52+
def default(self, node, *args):
6453
for child in node.getChildren():
6554
if isinstance(child, ast.Node):
66-
self._postorder(child)
67-
apply(self.dispatch, (node,) + args)
55+
apply(self._preorder, (child,) + args)
6856

6957
def dispatch(self, node, *args):
7058
self.node = node
7159
meth = self._cache.get(node.__class__, None)
7260
className = node.__class__.__name__
7361
if meth is None:
74-
meth = getattr(self.visitor, 'visit' + className, 0)
62+
meth = getattr(self.visitor, 'visit' + className, self.default)
7563
self._cache[node.__class__] = meth
7664
if self.VERBOSE > 0:
7765
if self.VERBOSE == 1:
7866
if meth == 0:
7967
print "dispatch", className
8068
else:
8169
print "dispatch", className, (meth and meth.__name__ or '')
82-
if meth:
83-
return apply(meth, (node,) + args)
70+
return apply(meth, (node,) + args)
8471

8572
class ExampleASTVisitor(ASTVisitor):
8673
"""Prints examples of the nodes that aren't visited
@@ -93,24 +80,27 @@ class ExampleASTVisitor(ASTVisitor):
9380

9481
def dispatch(self, node, *args):
9582
self.node = node
96-
className = node.__class__.__name__
97-
meth = getattr(self.visitor, 'visit' + className, None)
98-
if self.VERBOSE > 0:
99-
if self.VERBOSE > 1:
100-
print "dispatch", className, (meth and meth.__name__ or '')
83+
meth = self._cache.get(node.__class__, None)
84+
className = node.__class__.__name__
85+
if meth is None:
86+
meth = getattr(self.visitor, 'visit' + className, 0)
87+
self._cache[node.__class__] = meth
88+
if self.VERBOSE > 1:
89+
print "dispatch", className, (meth and meth.__name__ or '')
10190
if meth:
10291
return apply(meth, (node,) + args)
10392
elif self.VERBOSE > 0:
10493
klass = node.__class__
105-
if self.examples.has_key(klass):
106-
return
107-
self.examples[klass] = klass
108-
print
109-
print klass
110-
for attr in dir(node):
111-
if attr[0] != '_':
112-
print "\t", "%-12.12s" % attr, getattr(node, attr)
113-
print
94+
if not self.examples.has_key(klass):
95+
self.examples[klass] = klass
96+
print
97+
print self.visitor
98+
print klass
99+
for attr in dir(node):
100+
if attr[0] != '_':
101+
print "\t", "%-12.12s" % attr, getattr(node, attr)
102+
print
103+
return apply(self.default, (node,) + args)
114104

115105
_walker = ASTVisitor
116106
def walk(tree, visitor, verbose=None):

Tools/compiler/compiler/visitor.py

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,40 +47,27 @@ def preorder(self, tree, visitor):
4747
self._preorder(tree)
4848

4949
def _preorder(self, node, *args):
50-
stop = apply(self.dispatch, (node,) + args)
51-
if stop:
52-
return
53-
for child in node.getChildren():
54-
if isinstance(child, ast.Node):
55-
self._preorder(child)
56-
57-
def postorder(self, tree, visitor):
58-
"""Do preorder walk of tree using visitor"""
59-
self.visitor = visitor
60-
visitor.visit = self._postorder
61-
self._postorder(tree)
50+
return apply(self.dispatch, (node,) + args)
6251

63-
def _postorder(self, tree, *args):
52+
def default(self, node, *args):
6453
for child in node.getChildren():
6554
if isinstance(child, ast.Node):
66-
self._postorder(child)
67-
apply(self.dispatch, (node,) + args)
55+
apply(self._preorder, (child,) + args)
6856

6957
def dispatch(self, node, *args):
7058
self.node = node
7159
meth = self._cache.get(node.__class__, None)
7260
className = node.__class__.__name__
7361
if meth is None:
74-
meth = getattr(self.visitor, 'visit' + className, 0)
62+
meth = getattr(self.visitor, 'visit' + className, self.default)
7563
self._cache[node.__class__] = meth
7664
if self.VERBOSE > 0:
7765
if self.VERBOSE == 1:
7866
if meth == 0:
7967
print "dispatch", className
8068
else:
8169
print "dispatch", className, (meth and meth.__name__ or '')
82-
if meth:
83-
return apply(meth, (node,) + args)
70+
return apply(meth, (node,) + args)
8471

8572
class ExampleASTVisitor(ASTVisitor):
8673
"""Prints examples of the nodes that aren't visited
@@ -93,24 +80,27 @@ class ExampleASTVisitor(ASTVisitor):
9380

9481
def dispatch(self, node, *args):
9582
self.node = node
96-
className = node.__class__.__name__
97-
meth = getattr(self.visitor, 'visit' + className, None)
98-
if self.VERBOSE > 0:
99-
if self.VERBOSE > 1:
100-
print "dispatch", className, (meth and meth.__name__ or '')
83+
meth = self._cache.get(node.__class__, None)
84+
className = node.__class__.__name__
85+
if meth is None:
86+
meth = getattr(self.visitor, 'visit' + className, 0)
87+
self._cache[node.__class__] = meth
88+
if self.VERBOSE > 1:
89+
print "dispatch", className, (meth and meth.__name__ or '')
10190
if meth:
10291
return apply(meth, (node,) + args)
10392
elif self.VERBOSE > 0:
10493
klass = node.__class__
105-
if self.examples.has_key(klass):
106-
return
107-
self.examples[klass] = klass
108-
print
109-
print klass
110-
for attr in dir(node):
111-
if attr[0] != '_':
112-
print "\t", "%-12.12s" % attr, getattr(node, attr)
113-
print
94+
if not self.examples.has_key(klass):
95+
self.examples[klass] = klass
96+
print
97+
print self.visitor
98+
print klass
99+
for attr in dir(node):
100+
if attr[0] != '_':
101+
print "\t", "%-12.12s" % attr, getattr(node, attr)
102+
print
103+
return apply(self.default, (node,) + args)
114104

115105
_walker = ASTVisitor
116106
def walk(tree, visitor, verbose=None):

0 commit comments

Comments
 (0)