Posted by Tristan on November 19th, 2010.
Categories: programming, trudy's mechanicals.
I’ve always been intrigued by computer architecture. At Ryerson I took a course in compiler design and made my first simple JVM compiler. At Capybara Games I wrote simple scripting languages for Pirates of the Carribean: At World’s End Mobile and Heroes of Might And Magic: Clash of Heroes . Before I left the company I designed a cross compiler to convert a subset of Java (J2ME) to C++ ( BREW).
Now I find myself doing it all over again; creating a scripting language for the cut scenes in Trudy’s Mechanicals.
Why would you do that when you could use Python, LUA, V8 or some other embeddable language?
Because its fun! and you get to control exactly what goes in it. Here’s the feature list:
- statically typed and linked
- no dynamic memory allocation
- continuation like structures
- support for 32-bit integers and non-mutable strings
- multi-pass compilation (no &%^%^ header files)
- objective-c like calling syntax (I find it a lot more readable)
- simple loops and conditionals
Of course, this isn’t a complete language, but a domain specific one tailored to a limited device.
So I imagine you’re going to use a tool like Lex or Yacc right?
For complex languages or large source bases these are very useful, but I’ve found them to be unnecessary with the speed of modern computers. I find I can brute force parsing and expression matching without any noticeable performance penalty. Yes this does mean more initial work setting everything up, but it makes parsing order of operations in arithmetic and the various forms of “if” statements found in C like languages easier to work with.
For more information on other “compiler compilers” see The Lex and Yacc Page.
Perhaps this post series should be named the “Brute Force method to Design a Simple Scripting Language”.
This tutorial will be broken down in the following weeks into the following sections:
- Creating a Buffered Reader with look ahead
- Parsing tokens and comments
- Designing your Virtual Machine
- Outputting Assembly
At the end of this tutorial we will have a simple VM written in portable C, with a static scripting language that supports simple types.
See you next week!