[Compiler-sig] AST observations
Jeremy Hylton
jeremy@zope.com
Thu, 18 Apr 2002 14:31:41 -0400
>>>>> "ECN" == Eric C Newton <ecn@metaslash.com> writes:
ECN> compiler.visitor:
ECN> ASTVisitor
ECN> As the comment says, it's not a visitor, it's a walker.
ECN> Someone mentioned earlier that this is rather
ECN> confusing.
I'm sorry this is confusing, but I think it is one of the standard
variations on the visitor pattern. It's certainly the case that the
visitors we've all been writing have the signs of being a visitor,
e.g. a method for each class of object.
As for how the traversal occurs, GoF (p. 339) says:
"Who is responsible for traversing the object structure? A visitor
must visit each element of the object structure. The question is, how
does it get there? We can put the responsibility in any of three
places: in the object structure, in the visitor, or in a separate
iterator object."
The text goes on to discuss these alternatives and notes that you
could also use an internal iterator that is a kind of hybrid between
having the traversal in the object structure and using an iterator.
In this case, the iterator calls a method on the visitor with the
object as an argument as opposed to calling a method of the object
with the visitor as the argument.
It might be clearer to merge the walker and the visitor into a single
class using inheritance. (I think the Walkabout variant described by
Palsberg and Jay does this,
cf. http://citeseer.nj.nec.com/palsberg97essence.html.) But I
thought delegation would be clearer and would avoid the need for a
magic base class that all visitors must inherit from.
Jeremy