diff options
author | Jeremy Hylton <jeremy@alum.mit.edu> | 2000-10-25 18:02:02 (GMT) |
---|---|---|
committer | Jeremy Hylton <jeremy@alum.mit.edu> | 2000-10-25 18:02:02 (GMT) |
commit | 66d2c1f7e5b968280137712463404ee2a13809ab (patch) | |
tree | 97347f1ae0fd1b5326b64cbf745a1c28a76a1479 /Lib/compiler | |
parent | 821eee3321f36562e45aa97e25746ad0b6cad31e (diff) | |
download | cpython-66d2c1f7e5b968280137712463404ee2a13809ab.zip cpython-66d2c1f7e5b968280137712463404ee2a13809ab.tar.gz cpython-66d2c1f7e5b968280137712463404ee2a13809ab.tar.bz2 |
Small optimizations in dispatch method: 1) lookup node's __class__ once
and store in local; 2) define _preorder to be dispatch (rather than
method that called dispatch).
Diffstat (limited to 'Lib/compiler')
-rw-r--r-- | Lib/compiler/visitor.py | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/Lib/compiler/visitor.py b/Lib/compiler/visitor.py index 32e72e1..a6604f4 100644 --- a/Lib/compiler/visitor.py +++ b/Lib/compiler/visitor.py @@ -1,3 +1,4 @@ +import sys from compiler import ast class ASTVisitor: @@ -40,15 +41,6 @@ class ASTVisitor: self.node = None self._cache = {} - def preorder(self, tree, visitor): - """Do preorder walk of tree using visitor""" - self.visitor = visitor - visitor.visit = self._preorder - self._preorder(tree) - - def _preorder(self, node, *args): - return apply(self.dispatch, (node,) + args) - def default(self, node, *args): for child in node.getChildren(): if isinstance(child, ast.Node): @@ -56,12 +48,14 @@ class ASTVisitor: def dispatch(self, node, *args): self.node = node - meth = self._cache.get(node.__class__, None) - className = node.__class__.__name__ + klass = node.__class__ + meth = self._cache.get(klass, None) if meth is None: + className = klass.__name__ meth = getattr(self.visitor, 'visit' + className, self.default) - self._cache[node.__class__] = meth + self._cache[klass] = meth if self.VERBOSE > 0: + className = klass.__name__ if self.VERBOSE == 1: if meth == 0: print "dispatch", className @@ -69,6 +63,14 @@ class ASTVisitor: print "dispatch", className, (meth and meth.__name__ or '') return apply(meth, (node,) + args) + def preorder(self, tree, visitor): + """Do preorder walk of tree using visitor""" + self.visitor = visitor + visitor.visit = self._preorder + self._preorder(tree) + + _preorder = dispatch + class ExampleASTVisitor(ASTVisitor): """Prints examples of the nodes that aren't visited |