[Python-checkins] CVS: python/dist/src/Tools/compiler/compiler visitor.py,1.6,1.7

Jeremy Hylton jhylton@users.sourceforge.net
Mon, 27 Aug 2001 13:47:10 -0700


Update of /cvsroot/python/python/dist/src/Tools/compiler/compiler
In directory usw-pr-cvs1:/tmp/cvs-serv14932

Modified Files:
	visitor.py 
Log Message:
Two changes to visitor API: 
    Remove _preorder as alias for dispatch and call dispatch directly.
    Add an extra optional argument to walk()

XXX Also comment out some code that does debugging prints.




Index: visitor.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Tools/compiler/compiler/visitor.py,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** visitor.py	2001/04/11 16:26:05	1.6
--- visitor.py	2001/08/27 20:47:08	1.7
***************
*** 1,5 ****
- import sys
  from compiler import ast
  
  class ASTVisitor:
      """Performs a depth-first walk of the AST
--- 1,7 ----
  from compiler import ast
  
+ # XXX should probably rename ASTVisitor to ASTWalker
+ # XXX can it be made even more generic?
+ 
  class ASTVisitor:
      """Performs a depth-first walk of the AST
***************
*** 45,49 ****
          for child in node.getChildren():
              if isinstance(child, ast.Node):
!                 apply(self._preorder, (child,) + args)
  
      def dispatch(self, node, *args):
--- 47,51 ----
          for child in node.getChildren():
              if isinstance(child, ast.Node):
!                 self.dispatch(child, *args)
  
      def dispatch(self, node, *args):
***************
*** 55,65 ****
              meth = getattr(self.visitor, 'visit' + className, self.default)
              self._cache[klass] = meth
!         if self.VERBOSE > 0:
!             className = klass.__name__
!             if self.VERBOSE == 1:
!                 if meth == 0:
!                     print "dispatch", className
!             else:
!                 print "dispatch", className, (meth and meth.__name__ or '')
          return meth(node, *args)
  
--- 57,67 ----
              meth = getattr(self.visitor, 'visit' + className, self.default)
              self._cache[klass] = meth
! ##        if self.VERBOSE > 0:
! ##            className = klass.__name__
! ##            if self.VERBOSE == 1:
! ##                if meth == 0:
! ##                    print "dispatch", className
! ##            else:
! ##                print "dispatch", className, (meth and meth.__name__ or '')
          return meth(node, *args)
  
***************
*** 67,74 ****
          """Do preorder walk of tree using visitor"""
          self.visitor = visitor
!         visitor.visit = self._preorder
!         self._preorder(tree, *args) # XXX *args make sense?
! 
!     _preorder = dispatch
  
  class ExampleASTVisitor(ASTVisitor):
--- 69,74 ----
          """Do preorder walk of tree using visitor"""
          self.visitor = visitor
!         visitor.visit = self.dispatch
!         self.dispatch(tree, *args) # XXX *args make sense?
  
  class ExampleASTVisitor(ASTVisitor):
***************
*** 91,95 ****
              print "dispatch", className, (meth and meth.__name__ or '')
          if meth:
!             return apply(meth, (node,) + args)
          elif self.VERBOSE > 0:
              klass = node.__class__
--- 91,95 ----
              print "dispatch", className, (meth and meth.__name__ or '')
          if meth:
!             meth(node, *args)
          elif self.VERBOSE > 0:
              klass = node.__class__
***************
*** 103,115 ****
                          print "\t", "%-12.12s" % attr, getattr(node, attr)
                  print
!             return apply(self.default, (node,) + args)
  
  _walker = ASTVisitor
! def walk(tree, visitor, verbose=None):
!     w = _walker()
      if verbose is not None:
!         w.VERBOSE = verbose
!     w.preorder(tree, visitor)
!     return w.visitor
  
  def dumpNode(node):
--- 103,118 ----
                          print "\t", "%-12.12s" % attr, getattr(node, attr)
                  print
!             return self.default(node, *args)
  
+ # XXX this is an API change
+ 
  _walker = ASTVisitor
! def walk(tree, visitor, walker=None, verbose=None):
!     if walker is None:
!         walker = _walker()
      if verbose is not None:
!         walker.VERBOSE = verbose
!     walker.preorder(tree, visitor)
!     return walker.visitor
  
  def dumpNode(node):