[e-lang] templateTermVisitor
Dean Tribble
dtribble at gmail.com
Thu Nov 30 23:45:25 CST 2006
The pattern uses extends because it implements the required circularity: the
tmeplateVisitor must invoke 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.
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.
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.
>
> --
> Text by me above is hereby placed in the public domain
>
> Cheers,
> --MarkM
>
> _______________________________________________
> e-lang mailing list
> e-lang at mail.eros-os.org
> http://www.eros-os.org/mailman/listinfo/e-lang
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.eros-os.org/pipermail/e-lang/attachments/20061130/2ca5ed39/attachment.html
More information about the e-lang
mailing list