Sunday, June 5, 2011

ANTLR translator for embedded scripting language

Update 7/16/2011 I have posted code for this.

Last month, I wrote a post detailing the building of a scripting language interpreter inside of ActionScript. The interpreter worked from abstract syntax trees represented in XML, however the idea of programming in this environment is not too appealing. Therefore, I knew I had to build a translator to take a C-like syntax and translate it into my XML abstract syntax tree format. I chose to use ANTLR as my parser generator and used the related StringTemplate template engine to bridge the gap between parsed AST and XML representation. The Definitive ANTLR Reference by Terrence Parr (the creator of ANTLR) was a lot of help when it came to learning about how to use ANTLR and about non-trivial parsing in general.

So far my language consists of functions that may be called by various aspects of the game engine and that may call each other. Various types are supported, type casting is implementing, scoping is implemented, function returns are implemented and while/for/if statements are all supported. I am probably going to implement array support as well. Now that I can write complicated scripting procedures, a lot more opportunities will open for game design when I begin that phase but the code will remain penetrable since it is compiled from a readable C-subset syntax.