[Compiler-sig] AST observations

Finn Bock bckfnn@worldonline.dk
Fri, 19 Apr 2002 14:33:49 GMT


[Jeremy]

> 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.

[Eric]

>The only advantage I can see for this approach is faster visitation:
>the base class could have default visit methods that would know how to
>iterate over the child nodes.  getChildNodes() would no longer be
>necessary.

You would not need a base class to get that benefit. I proposed a
'traverse' method on the AST nodes that will iterator over the children
of the node like this:

class Module:
   def traverse(self, walker):
      for stmt in self.body:
          walker.visit(stmt)

That way the information about children is kept in the AST nodes.

You would only need the visitor base class to please Jython.

regards,
finn