brhfl.com

wo: Implementing the interpreter

I’ve been thinking a lot about instructions for wo, how to eke the most out of low-byte-count programs. And, while I haven’t touched on it here yet (soon!), I’ve been thinking a lot about what system registers could prove useful as well. But a big part of me wonders how I’ll implement the thing, if I ever do. That so many esoteric/golfing languages are just layers on top of another scripting language makes me a bit grumpy, and that isn’t a path I’d like to take with wo. My plan would be to write the reference interpreter in go, but I wouldn’t discount ANSI C either. That decision is trivial, though.

The trick of implementation comes from the flexibility of wo. Since words can be any length, but most languages only want to deal in bytes, something must be done to break it apart. Internally, there will be a bit cursor (if you will) that will jump around based on word size and word number. A ‘chunker’ will break out ‘chunks’ to work with, of (word size * 8) bits. This will be separated into an array of however many words it contains. If (word size * 8) > (remaining bits), we take remaining bits and mod the next binary place up from the number of extra bits to extract that word. Extracting the other words is fairly simple as well; shift right as many times as there are places to move, then subtract (the new value times the binary place corresponding with word size) from the old value.

We can do all of this, and then just make use of the decimal value – instructions can be referenced by their decimal value in a lookup table, and input can be fed to a simple push positive, negative, or MNum function. Since LSB determines instruction vs. input, we only have to test for oddness to know what to do (and we can immediately pass a right-shifted value).

Lastly, GOTOs, user instruction calls, wordsize changes, and who knows what else will spawn a rechunk. Anything to avoid unpredictable behavior relating to word size.


this post is part of the series, wo:
  1. Introduction
  2. Word size
  3. Numbers
  4. A truth machine in wo3
  5. Implementing the interpreter
  6. Stacks
  7. Registers
  8. 9-byte modulo