[Python-checkins] CVS: /python/nondist/src/Compiler/compiler visitor.py,1.2,1.3

Jeremy Hylton jhylton@cnri.reston.va.us
Thu, 16 Mar 2000 15:04:19 -0500 (EST)


Update of /projects/cvsroot//python/nondist/src/Compiler/compiler
In directory bitdiddle:/home/jhylton/python/nondist/src/Compiler/compiler

Modified Files:
	visitor.py 
Log Message:
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




Index: visitor.py
===================================================================
RCS file: /projects/cvsroot//python/nondist/src/Compiler/compiler/visitor.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -r1.2 -r1.3
*** visitor.py	2000/03/06 18:54:30	1.2
--- visitor.py	2000/03/16 20:04:16	1.3
***************
*** 48,69 ****
  
      def _preorder(self, node, *args):
! 	stop = apply(self.dispatch, (node,) + args)
!         if stop:
!             return
!         for child in node.getChildren():
!             if isinstance(child, ast.Node):
!                 self._preorder(child)
  
!     def postorder(self, tree, visitor):
!         """Do preorder walk of tree using visitor"""
!         self.visitor = visitor
!         visitor.visit = self._postorder
!         self._postorder(tree)
! 
!     def _postorder(self, tree, *args):
          for child in node.getChildren():
              if isinstance(child, ast.Node):
!                 self._postorder(child)
! 	apply(self.dispatch, (node,) + args)
  
      def dispatch(self, node, *args):
--- 48,57 ----
  
      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):
!                 apply(self._preorder, (child,) + args)
  
      def dispatch(self, node, *args):
***************
*** 72,76 ****
  	className = node.__class__.__name__
  	if meth is None:
! 	    meth = getattr(self.visitor, 'visit' + className, 0)
  	    self._cache[node.__class__] = meth
          if self.VERBOSE > 0:
--- 60,64 ----
  	className = node.__class__.__name__
  	if meth is None:
! 	    meth = getattr(self.visitor, 'visit' + className, self.default)
  	    self._cache[node.__class__] = meth
          if self.VERBOSE > 0:
***************
*** 80,85 ****
              else:
                  print "dispatch", className, (meth and meth.__name__ or '')
!         if meth:
!             return apply(meth, (node,) + args)
  
  class ExampleASTVisitor(ASTVisitor):
--- 68,72 ----
              else:
                  print "dispatch", className, (meth and meth.__name__ or '')
! 	return apply(meth, (node,) + args)
  
  class ExampleASTVisitor(ASTVisitor):
***************
*** 94,115 ****
      def dispatch(self, node, *args):
          self.node = node
!         className = node.__class__.__name__
!         meth = getattr(self.visitor, 'visit' + className, None)
!         if self.VERBOSE > 0:
!             if self.VERBOSE > 1:
!                 print "dispatch", className, (meth and meth.__name__ or '')
          if meth:
              return apply(meth, (node,) + args)
          elif self.VERBOSE > 0:
              klass = node.__class__
!             if self.examples.has_key(klass):
!                 return
!             self.examples[klass] = klass
!             print
!             print klass
!             for attr in dir(node):
!                 if attr[0] != '_':
!                     print "\t", "%-12.12s" % attr, getattr(node, attr)
!             print
  
  _walker = ASTVisitor
--- 81,105 ----
      def dispatch(self, node, *args):
          self.node = node
! 	meth = self._cache.get(node.__class__, None)
! 	className = node.__class__.__name__
! 	if meth is None:
! 	    meth = getattr(self.visitor, 'visit' + className, 0)
! 	    self._cache[node.__class__] = meth
!         if self.VERBOSE > 1:
!             print "dispatch", className, (meth and meth.__name__ or '')
          if meth:
              return apply(meth, (node,) + args)
          elif self.VERBOSE > 0:
              klass = node.__class__
!             if not self.examples.has_key(klass):
! 		self.examples[klass] = klass
! 		print
! 		print self.visitor
! 		print klass
! 		for attr in dir(node):
! 		    if attr[0] != '_':
! 			print "\t", "%-12.12s" % attr, getattr(node, attr)
! 		print
! 	    return apply(self.default, (node,) + args)
  
  _walker = ASTVisitor