I am trying to use llvm to implement a simple, dumb c-like language.
And i’m stucked at designing a good AST.
For example i’m wondering if separating a variable into two kind of nodes is a good idea:
one for allocating and one for loading it. I tried that, but i got into some obstacles:
Foo = asd = 3;
At this case Foo and add would be an allocating, but add would be a loading as well.
But the ast nodes are joined at their code() methods.
Is there any good resources on designing ast?
(I tried to find clang’s but it’s kinda complex to easily comprehend from it’s source files.)
In most languages that allow this kind of input, that would be parsed as
and
Foowould be assigned the result of the expressionasd = 3. Which usually happens to be the value ofasd, but the AST doesn’t need to represent that.The AST usually does not represent semantics like “read access” anyway. It’s a graph representation of the syntax, and the syntax is just “assignment with left hand side variable
Fooand right hand side (assignment with left hand side variableasdand right hand side integer constant3)”.If I remember the Kaleidoscope example correctly, you’ll see they have every statement return a value. Most of those will be optimized away further down the chain, but they are the easiest way of getting useful behavior for nested assignments and the like. The left hand side of an assignment, obviously, needs special treatment, but nothing that is difficult to understand or hard to implement.