Understanding the AST...

joe something at example.com
Tue Feb 6 12:03:06 UTC 2018


Hello everybody!

Last week end I found this post ( 
https://dlang.org/blog/2017/08/01/a-dub-case-study-compiling-dmd-as-a-library/ ) on the Blog and thought to myself awesome.

So I built the library and everything went smooth. Thanks for the 
effort of all the involved people who made that possible!

I've had a look at the 2 examples, too, the avg. function lines ( 
https://github.com/dlang/dmd/blob/master/src/examples/avg.d ) and 
the import ( 
https://github.com/dlang/dmd/blob/master/src/examples/impvisitor.d ) ones and for a start I decided to make a program that prints the outline of a module.

Turns out I don't really understand how to access the data in the 
AST.
For everything there's a visitor method and overriding a few of 
them to print return statements and some such works as advertised.

However, I have no idea where I am in the tree when any of those 
methods are called.
Like for example in 
FunctionLengthVisitor(AST).visitFuncBody(AST.FuncDeclaration fd).
I have a function declaration object which tells me everything 
about what's inside the function, but how do I know what or where 
this function belongs to, where can I get that information ? I 
don't see anything about UDAs either, nor the doc comment.

I understand when visitor.getAvgLen is called with the parsed 
module, the accept function calls a visitor overload for each 
member.
But this sounds to me like I'd have to do a lot of book keeping 
in my visitor to keep track of things which are already present 
in the AST.

Any insight to this would be much appreciated :)


More information about the Digitalmars-d-learn mailing list