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

Introducing BoundTreeVisitor and BoundTreeWalker #104

Merged
merged 3 commits into from
Jul 13, 2024
Merged

Conversation

ChrisKXu
Copy link
Owner

Summary

This PR introduces BoundTreeVisitor and BoundTreeWalker patterns to all the bound nodes.
This PR also converted ControlGraphAnalyzer to inherit from BoundTreeWalker.

Detail

Similar to roslyn, we use the Visitor Pattern to traverse the BoundTree and the nodes. This requires that

  • All sealed BoundNode types needs to implement Accept(T) method. (This could even be generated in the future)
  • We have a corresponding virtual Visit* method present in the BoundTreeVisitor base class.

The benefit to this approach is that we don't need a centralized Visit method with switch statements for every single BoundNode types.

BoundTreeVisitor

BoundTreeVisitor class is the base class which has all the Visit* methods return null. It provides boilerplate code for children classes to extend. Analyzers and rewriters should not directly inherit from this class and instead use BoundTreeWalker or BoundTreeRewriter (not implemented yet).

BoundTreeWalker

BoundTreeWalker class inherits from BoundTreeVisitor and provides basic depth-first traversal functionality. All nodes are examined and are not modified. Analyzers that don't modify the nodes should inherit from this class.

BoundTreeRewriter (not in this PR)

BoundTreeRewriter class also inherits from BoundTreeVisitor. It is similar to BoundTreeWalker but nodes can be updated (note, BoundNodes are immutable by design so here "updates" to the node actually creates a new node and replace the old one). The detailed implementation will be provided in future PRs.

@ChrisKXu ChrisKXu merged commit 884aeac into main Jul 13, 2024
4 checks passed
@ChrisKXu ChrisKXu deleted the bound-node-visitor branch July 13, 2024 03:55
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

Successfully merging this pull request may close these issues.

1 participant