-
-
Notifications
You must be signed in to change notification settings - Fork 314
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
[Internals] Make it easier to walk the AST #773
Comments
One approach that I could see is to have an additional data structure that just represents the tree, e.g.
So all or Stmt and Expr nodes end up dangling on such TreeNodes, rather than forming the proper tree. It is not clear to me whether we still have pointers to semantical children, i.e.
or rather replace all of this with a pointer to the TreeNode, and then access the children through that.
This probably does not work for node types that have more chaotic types of children, but I don't have enough of an overview that I could tell from here. The nice thing about such a structure is that we can implement many recursive functions like |
A different approach could be to apply the visitor pattern to AST nodes. I.e., we create visitor interface for AST nodes, extend all Stmt and Expr types with methods for accepting visitors, and then build visitors that work on the tree. In my mind, that should make it easier to do more powerful things with the existing This might still make it difficult to implement common tree algorithms, and lend itself more to other problem areas. I'm not sure, would probably have to build a PoC and play with it to get a feel. Just throwing it out there for now. |
FWIW this can be closed rather soon if it feels like a burden. I mainly want these thoughts to be out there and written down. |
We already have an Apply() function on the AST which I thought was enough. If you have a patch needing something else, please send the patch and the patch using that patch. I'm not against this but I don't see it (likely my bad here) at the moment. I'm going to close for now, we can re-open if there's a patch. |
Versions:
mgmt version (eg:
mgmt --version
): 0.0.26operating system/distribution (eg:
uname -a
): anygolang version (eg:
go version
): anyDescription:
It is currently hard to work with the AST on a data structure level. For example, running a search to present the node path to a specific node is Very Hard. It is Very Hard to generate a nice topological representation of the tree, etcpp.
The only useful means we have is the
interfaces.Node.Apply()
method, but it is very basic, does not give any context information to the func/lambda it accepts, limits us to depth first traversal, and may have other important limitations.The tree currently is very heterogenous, with each tree node having a rather distinct type, with very little abstraction. This is not a bad design per se, but poses some challenges.
I would like to have some discussion of which code additions or changes could make sense in order to help with this.
The text was updated successfully, but these errors were encountered: