[Compiler-sig] Visitor pattern

Finn Bock bckfnn@worldonline.dk
Sun, 14 Apr 2002 20:45:50 GMT


Hi,

Any thoughs about how a visitor pattern should be added to the AST
nodes? I took a quick look at the visitor in the 'compiler' package but
it wasn't immediately obvious to me how it works.

My own thoughts goes like this (I'm clearly thinking in java here). In
each AST node a method is generated:

    public Object accept(Visitor visitor) throws Exception {
        return visitor.visit_ClassDef(this);
    }

and a Visitor interface is generated like this:

public interface Visitor {
    public Object visit_Module(Module node) throws Exception;
    public Object visit_FunctionDef(FunctionDef node) throws Exception;
    public Object visit_ClassDef(ClassDef node) throws Exception;
    ...
}


As a result, the visitor implementation is itself responsible for
calling the .accept() method on all its children and there is no default
recursion.

Something as simple as that would fulfill the needs for visitor patterns
as used by jython itself, but if you are thinking about something more
powerful I might as well use that in jython's CodeCompiler.

regards,
finn