Open kaelumania opened 6 years ago
I pushed an initial implementation of this to the visitor branch.
@rubysolo it looks already pretty nice. However, I can imagine a few improvements:
visit_type
methods, check for respond_to?
?visit
callback. Thus, if the visitor responds to it, it might filter on type by its ownvisit_negation
like send("visit_#{self.class.name.demodulize}", ...)
accept
on some nodes. Because the AST should decide which nodes are visited an in which order and the visitor should only react on it.What do you think?
Yes, these are good suggestions.
Regarding PrintVisitor calling accept
-- my thought was that the visitor needs to control the order of evaluation (e.g. a visitor might want to print out in prefix notation). Is there a better way to handle that?
Hmm, here are my thoughts:
accept
methods, that can indicate traversal order (by a flag or rich object, e.g. Traversal::PreOrder
, Traversal::PostOrder
). This looks to me a bit like a composite visitor, one visitor controls the traversal ordering and another the "real" logic.process
method (template method). Similar to 1. but baked into 1 concept StackOverflowvisitEnter
, visitExit
. This way one could collect the visits
instead of printing them directly, and whenever an Exit
marker is reached, the collected visits
can be combined. FowlerI think I like the first idea best -- when I have some time I'll work on this a bit more.
It might be quiet useful to support a visitor pattern for traversing the AST.