Take the 2-minute tour ×

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.)

share|improve this question
It makes sense to have different representation for lvalues and rvalues. Here, your AST could be something like: (AssignExpr (LVar Foo) (AssignExpr (LVar asd) (IntLiteral 3))), note the right associativity of the assignment, and note that it is an expression, not a statement. –  SK-logic Sep 13 '11 at 12:20
add comment

2 Answers

up vote 2 down vote accepted

In most languages that allow this kind of input, that would be parsed as

Foo = (asd = 3);

and Foo would be assigned the result of the expression asd = 3. Which usually happens to be the value of asd, 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 Foo and right hand side (assignment with left hand side variable asd and right hand side integer constant 3)”.

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.

share|improve this answer
add comment

You might be able to take some inspiration from Go's AST package: http://golang.org/pkg/go/ast/

It's a C-like language, although your example doesn't apply since assignment in Go is a statement and doesn't return a value. If it did, your code would parse to something like this: (this is pseudo-Go, but will hopefully be understandable by people who don't know it)

    Lhs: Ident{
        Name: "Foo",
    Tok: token.ASSIGN
    Rhs: AssignExpr{
        Lhs: Ident{
            Name: "asd",
        Rhs: BasicLit{
            Kind: token.INT,
            Value: "3",
share|improve this answer
add comment

Your Answer

