Script Interpreter Work
Because I'm never content with having too much to do, I like to start an additional new project every few weeks. One of the more recent was writing a script interpreter. Since Blades of Avernum came out, I've tried to push its scripting system to its limits, both to create cool things and just to see what I can get it to do. Unfortunately, it's all too easy to hit Avernumscript's limits. In fact, one can give a whole long dreary list of what I won't do: allow more than 20 integer variables, execute more than 8000 units of code at a time, allow direct string comparison, and so on. For a few months I researched its shortcomings and complained about them to all and sundry. Then, over Christmas break, I decided I was going to take up the implied challenge and see if I really could do better.
I started out by deciding the priorities that I wanted to pursue. Foremost was power, in the sense of flexibility of the language. I wanted scoped variables, for loops, trustworthy operator precedence, string comparison and string concatenation. A close second goal was speed; Avernumscript is slow, and I wanted to at least not do worse. I decided that memory consumption was of far less concern, both because I could see a few easy ways to make some quick improvements, and since the quantities of memory involved are only kilobytes.
The next step was to envision a basic structure. Avernumscript simply tokenizes its input and stores a ingle long buffer containing numerical descriptors of the tokens, even semicolons. I decided I'd try something wildly different and make my entire system heavily object-oriented. By having a tree structure of objects, I could sort out which bit should execute when a single time during parsing and then just tell the objects to evaluate themselves as needed.
That was all months ago now. Today I finished another of my goals, for loops. So far I have a system which can execute most simple Avernumscripts (as long as they only entail math and not any function calls that require the game itself). A number of features like the for loops and scoped variables are already in and exceed the capabilities of Avernumscript, but there are still a few features that are lacking which I need for compatibility, such as the special feature to allow execution to jump to another 'state' in the script. I've also managed to reach a massive speed advantage for simple scripts doing operations like accessing variables in a loop. On the whole I'm having a great time with it, and I plan to write some more about my design here, both so I can refer to it if I forget how part of it works, and because for a long time I had had no idea how such a task could be accomplished. I had long assumed it far beyond my abilities;in fact it required only a basic working knowledge of C++, so who knows, maybe somebody else can learn something from this.