Saturday, March 7, 2009

Visualizing Groovy AST

So you want to write a Groovy AST transformation... it couldn't be simpler to read a compiling program than with the new local transformation capabilities. The hard part, for me at least, is figuring out how to modify that program. To do that you're going to have to hand write something into the abstract syntax tree (AST).

This post explains Groovy AST by example, starting simple and ending... well, still pretty simple.

An AST is a tree representation of your source code. The tree is composed of leafs and branches, most of which are subtypes of ASTNode. The two main classes of node are Expression objects (like MethodCallExpression and BinaryExpression) and StatementObjects (like BlockStatement and ReturnStatement).

Here is the AST for 4. Just plain old 4.

4 is a BlockStatement with one expression attached. That ExpressionStatement has one ConstantExpression, the Integer 4, attached.

The AST for 4+2 is more interesting:

Again, there's a BlockStatement with one ExpressionStatement attached. But now that expression has a BinaryExpression attached. The BinaryExpression is made up of 4:Integer, the + Token, and the 2:Integer expression.

The next AST shows some variable declarations from this code: int x = 4; x*x
This time the BlockStatement is made up of two expressions, a DeclarationExpression and a BinaryExpression.

The last example is for the code "println 4". It shows a method invocation:
Hopefully I've given you a flavor of what AST looks like and a feeling for the size of the task you're facing when you're thinking about writing some. It's not all that difficult but there are a lot of different types involved and there's not exactly great tooling built around it.


Anonymous said...

What? You're telling me it isn't magic!?

Hamlet D'Arcy said...

It won't be magic until I finish the ASTBuilder I'm working on! :)