[e-lang] templateTermVisitor
Dean Tribble
tribble at e-dean.com
Fri Dec 1 00:08:12 CST 2006
The templateVisitor generically walks a term tree. For each node, if the
specific visitor implementation (the object that extends the
templateVisitor) implements a method named visit_<T>, where <T> is the
node's type, that method is invoked on the specific visitor. Otherwise, the
visitAll method is invoked on the specific visitor, which typically recurs
generically an visits all the children.
The pattern uses extends because it implements the required circularity: the
templateVisitor must dispatch to the specific visitor and the specific
visitor must invoke the templateVisitor (for any visit operations that were
not overridden in the specific visitor). For example, the following visitor
snippet finds free refs in a grammar term tree. It does not need to
implement methods for any node types not involved in binding or using
variables. It does recursively invoke the overall visitor (and would need
to whether the tmeplate was contained in the specific visitor or the
template wrapped the specific visitor).
def findFree extends templateVisitor(findFree) {
to visit_ref(node, name) {
def nm := name.getOptString()
if (! bound.contains(nm)) {
free.addElement(nm)
}
}
to visit_bind(node, name, value) {
bound.push(name.getOptString())
findFree.visit(value)
}
to visit_seq(node) {
def boundCount := bound.size()
for a in node.getArgs() {
findFree.visit(a)
}
bound.setSize(boundCount)
}
to visitOther(node) {
findFree.visitAll(node.getArgs())
}
}
Without using extends, the visitor would need to be something like
def findFree := templateVisitor(def _ { to ...})
so that the recursive invocation would dispatch correctly for all node
types. That seems clumsier, but I'd be happy if you can propose a pattern
that doesn't require extends.
On 11/30/06, Mark S. Miller <markm at cs.jhu.edu> wrote:
>
> Hi Dean,
>
> Your templateTermVisitor[*] is very cool, and works fine for the trees
> produced by termE`...`. However, is there any reason why it uses "extends"
> instead of composition? Would the sub-object ever want to override any of
> super's messages?
>
> [*] I'll let Dean provide explanatory context.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.eros-os.org/pipermail/e-lang/attachments/20061130/c8ed59ff/attachment.html
More information about the e-lang
mailing list