I wonder what is the state AST processing? For example, when I try to process
pub fn fun_name(x: u32) bool {
return x < 1;
}
I cannot find (in the node list created by the AST parser) a reference to fun_name (the token exists, but it is not referenced on any node). Also the root node doesn’t point in to the fun declaration proper.
If I add another trivial statement before the fun declaration (say, a const), I do not get a pointer to the const name, neither a clear list of statements at the root.
If folks are interested I can add this thread my AST code along with the outputs that I am getting for the example above… For reference my entry point is something like
const tree = try Ast.parse(allocator, buffer_0, Ast.Mode.zig);
My point is to try to understand if this is a skill issue or that the AST module is still under construction.
std.zig.Ast definitely works, because the compiler uses it!
To save memory, we don’t unnecessarily duplicate data in the AST: for instance, there isn’t any string data stored in the AST. To get at the string fun_name, you find the node for the function declaration, and get to the token for the function name. We don’t directly store that token index; you need to find it implicitly (try ast.fullFnProto(fn_node).name_token). Then, use Ast.tokenSlice to pull the relevant slice out from the parser’s original input.
Not only does this decrease memory consumption, but it can also increase performance, since performance and memory consumption are often quite heavily correlated. This is Data Oriented Design in action; for more information, see this talk from Andrew.
If that doesn’t help for a node you’re looking at, the next thing you can try is to look for a function whose name starts with full, such as fullFnProto which @mlugg mentioned above. Those functions will take a node in the AST and derive other information about the node: you can reference their logic to see how they’re getting those details from the AST structure.
Another good reference, if the full functions don’t give you what you need, is to look at how other components of Zig are handling the AST. For example, if you want to know more about how simple_var_decl works, you can look at
In this case, the name of the declaration is inferred from the structure: as the documentation in Node.Tag states, the main_token of a simple_var_decl is the var or const token, and given that information and the fact that the declaration name is always the token right after var or const, you can add 1 to the main_token index to get the identifier.