My Most Important Project Was a Bytecode Interpreter
Two approaches are severely underused in the software world:<p>1) Domain-specific languages (DSLs)<p>2) Virtual machines (or just explicit state machines more generally)<p>What I mean is, alot of problems could be solved cleanly, elegantly, more safely, and more powerfully by using one (or both) of the above. The problem is that when people think DSL or VM, they think big (Scheme or JVM) instead of thinking small (printf). A DSL or VM doesn&#x27;t need to be complex; it could be incredibly simple but still be immensely more powerful than coding a solution directly in an existing language using its constructs and APIs.<p>Case in point: the BSD hexdump(1) utility. POSIX defines the od(1) utility for formatting binary data as text, and it takes a long list of complex command-line arguments. The hexdump utility, by contrast, uses a simple DSL to specify how to format output. hexdump can implement almost every conceivable output format of od and then some using its DSL. The DSL is basically printf format specifiers combined with looping declarations.<p>I got bored one day and decided to implement hexdump as a library (i.e. &quot;one hexdump to rule them all&quot;), with a thin command-line wrapper that emulates the BSD utility version. Unlike BSD hexdump(1) or POSIX od(1), which implement everything in C in the typical manner, I decided to translate the hexdump DSL into bytecode for a simple virtual machine.<p><pre><code>  http:&#x2F;&#x2F;25thandclement.com&#x2F;~william&#x2F;projects&#x2F;hexdump.c.html
</code></pre>
The end result was that my implementation was about the same size as either of those, but 1) could built as a shared library, command-line utility, or Lua module, 2) is more performant (formats almost 30% faster for the common outputs, thanks to a couple of obvious, easy, single-line optimizations the approach opened up) than either of the others, and 3) is arguably easier to read and hack on.<p>Granted, my little hexdump utility doesn&#x27;t have much value. I still tend to rewrite a simple dumper in a couple dozen lines of code for different projects (I&#x27;m big on avoiding dependencies), and not many other people use it. But I really liked the experience and the end result. I&#x27;ve used simple DSLs, VMs, and especially explicit state machines many times before and after, but this one was one of the largest and most satisfying.<p>The only more complex VM I&#x27;ve written was for an asynchronous I&#x2F;O SPF C library, but that one is more difficult to explain and justify, though I will if pressed.