Weeknotes 148
Imaginary audience
-
A sense of doom pervades. Meanwhile, life continues.
-
I only managed two streams this week between various commitments and general laziness.
I’m getting into the details of the WebAssembly language proper. The official test suite forced me to understand that there are essentially two plain text syntaxes for WebAssembly expressions (e.g. function bodies):
-
nested, bracketed instructions like
(i32.mul (i32.add (local.get $x) (i32.const 2)) (i32.const 3))
; and -
flat instruction sequences like
local.get $x i32.const 2 i32.add i32.const 3 i32.mul
.
The former syntax is called “folded form”. It’s hierarchical with operators in prefix position, like a normal programming language (e.g. Lisp — I suppose “normal” is relative). This is the easiest possible kind of language to write a tree-walking interpreter for: you recursively evaluate the arguments and use the host language’s call stack to hold onto the resulting values until you’re ready to use them. Which is what I’d been getting away with until now, because this was the only kind of expression I’d encountered.
The latter, unfolded syntax is a list of operators and operands, with operators appearing in postfix position. To evaluate this you need an explicit stack and an interpreter that knows how to chop up the indifferentiated stream of tokens into executable instructions (e.g. what the static arity of each instruction is), which is more hassle than the simple recursive interpreter.
It seems like the latter syntax is canonical and the former a convenient abbreviation, so I changed my interpreter to execute the unfolded syntax and wrote an
#unfold
method to normalise all expressions into that form.In addition to the syntactic changes, I also had to rewire the interpreter to push & pop values to & from an explicit operand stack instead of relying on the Ruby call stack.
I was relieved I was able to do this surgery incrementally by hooking up the stack one instruction at a time, then unfolding one instruction at a time, without breaking the tests. If I’d needed to do it as a single monolithic change I think it would’ve been much more painful to get the details right. It was a bit painful anyway, but at least I got to deal with each silly mistake immediately instead of allowing them to accumulate.
My reward is that I can now successfully execute a big unfolded function. Feels good.
Although it doesn’t sound like it, this is the interesting stuff! I’m glad to be getting these big infrastructural details shaken out, and I finally feel like I’m learning something useful about WebAssembly instead of faffing about with various representations of numbers. The next big operation is to insert an abstract syntax parser between the source S-expression and the interpreter so that the latter becomes fully insulated from these fiddly details of the concrete syntax.
-
-
To a first approximation nobody is really watching these streams — and who can blame them? — but I’m enjoying making them anyway. For the purposes of this project an imaginary audience is almost as useful as a real one; it still forces me to think out loud and explain why I’m doing what I’m doing. 🦆
-
All of the treadmills at the gym have been switched back to kilometers, with the exception of a single rogue machine which “may seem faster”.
I’ve no idea what’s going on here.
-
I’ve been relistening to the Hypercritical podcast with the vague intention of finishing it on the tenth anniversary of its final episode, thereby making my own fun. When I originally listened to it a decade ago I didn’t bother with the After Darks, so this time I’ve been trying to include them out of a misguided sense of completism, but it’s a pain to manually pick them out of the firehose of other 5by5 shows’ episodes.
So I made my own combined feed and stuck it on GitHub where my podcast client can subscribe to it. I’ve been using it for a month and it works nicely. (As with Hello Internet I’m not sure the content has aged particularly well, but that’s not something I can so easily fix with Nokogiri.)
-
Part of me would like to start using Mastodon, but I am — like a few other people I’ve seen — completely paralysed by the choice of instance, so rather than commit to one, I’m just not posting anything anywhere. Thus I win.
-
I had a lovely spontaneous breakfast with Tom on Thursday, which is the sort of thing that makes me feel like it’s still worth living in London.
-
I turned the beeps off.
-
Bit dark out.