On Spell Parsing


Parsing the spells on a wand was my first experience with writing any sort of serious parser.

I had written rudimentary JSON parsers and other custom stuff, but those were really straightforward since they don’t perform any computation of any kind. The Moloch spell system has modifiers, which are functions with inputs and outputs (where the inputs are spells, and the outputs are more spells).

There’s also function composition at play - the output of a spell can be an input for another modifier. What do we do?

My solution was to make a stack-based system. I was lowkey into stack based computing at the time, and delved into UXN a little, so that’s probably why. But really, stack programming also seemed like the simplest to implement.

Each spell adds itself to the execution stack from last to first. The execution stack is then cast starting from the earliest addition.

Modifiers take spells out of the execution stack, and put a new one in it.

This means many things. For one, it means that it’s easy to know where the inputs of a spell are in the inventory, they’re just whatever came before the modifier. It also means it’s easy to know when the modifiers aren’t supplied enough inputs, and so I can stop the entire computation as soon as this happens. I could even make some “compile time error” sound play or something! I should do that.

An important thing, too, is that every modifier outputs a spell, meaning that they can work as inputs to other modifiers!

Cool, right?

Files

moloch-win.zip 34 MB
Version 0.16.0dev7.1 Sep 30, 2023
moloch-osx.zip 48 MB
Version 0.16.0dev7.1 Sep 30, 2023
moloch-linux.zip 51 MB
Version 0.16.0dev7.1 Sep 30, 2023

Get Moloch

Download NowName your own price

Leave a comment

Log in with itch.io to leave a comment.