Visitor pattern and separating iteration

Ian hobson42 at gmaiil.com
Thu Jul 22 16:25:56 EDT 2010


Hi Karsten,

On 22/07/2010 12:03, Karsten Wutzke wrote:
> What is it I'm missing?
>    
I think you are making it more complicated than it really is.

The visitor pattern is about bringing all the little bits that would 
otherwise be scattered all over
many node classes into one visitor class. For code generation this means 
that all the code generation is done
in your visitor class. For pretty-printing you have another visitor 
class. For code optimization a third
visitor class.

If there is only one sequence in which the nodes should be visited, then 
you can build it
in to the nodes of the tree.

If you need to visit your nodes in different orders you might be better 
off building a separated
tree-walker class for each order. Thus you might have a 
post-order-walker for use with the code
generating visitor, and a in-order-walker for a pretty-printing visitor.

The optimizing walker may need to take different routes through the tree 
depending
upon what it finds. For example, it may compute the value of the RHS of 
an assignment
before computing the address of the LHS, so it can make better use of 
registers. Here the
visitor class itself needs to do the walking.

Regards

Ian











More information about the Python-list mailing list