Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support visitor pattern for AST #151

Open
kaelumania opened this issue Dec 19, 2017 · 5 comments
Open

Support visitor pattern for AST #151

kaelumania opened this issue Dec 19, 2017 · 5 comments

Comments

@kaelumania
Copy link

It might be quiet useful to support a visitor pattern for traversing the AST.

@rubysolo
Copy link
Owner

I pushed an initial implementation of this to the visitor branch.

@kaelumania
Copy link
Author

kaelumania commented Jan 2, 2018

@rubysolo it looks already pretty nice. However, I can imagine a few improvements:

  1. Don't force a visitor to implement all visit_type methods, check for respond_to??
  2. Add a general visit callback. Thus, if the visitor responds to it, it might filter on type by its own
  3. Generate default implementations for visit_negation like send("visit_#{self.class.name.demodulize}", ...)
  4. PrintVisitor, I find a bit strage that the PrintVisitor calls 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?

@rubysolo
Copy link
Owner

rubysolo commented Jan 2, 2018

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?

@kaelumania
Copy link
Author

Hmm, here are my thoughts:

  1. We could add more parameters to the 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.
  2. The visitor is responsible for traversal and we provide base implementations that define the traversal and call some kind of process method (template method). Similar to 1. but baked into 1 concept StackOverflow
  3. Provide more context while traversing by using visitEnter, 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. Fowler
  4. Define an overall default ordering that fits our needs, like the print ordering

@rubysolo
Copy link
Owner

rubysolo commented Jan 9, 2018

I think I like the first idea best -- when I have some time I'll work on this a bit more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants