Simple Virtual Machines

Terrence Parr presented an interesting introduction to virtual machines. He implemented a simple virtual machine, named appropriately. The code is available in a Java implementation and a C implementation on GitHub. I decided to implement it one of the worst languages you could imagine for such a task: Lua. Lua uses a virtual machine. So we'll have a virtual machine in a virtual machine. Lua doesn't even have an integer type or the switch statement. This means the default word size is actually a double and the dispatch loop is fairly verbose. I tried a number of dispatch methods: an array of functions and a giant if ... else. The array of opcodes is definitely more elegant in Lua but it was slower on my machine.

MoonScript (and Lua)

Lua is my favorite programming language. That puts me in a very small minority, even among Lua programmers (who seem to prefer C). For me, Lua is the first choice. But the more you use any tool the more you come to realize it has some faults. Lua is no exception. First and foremost is the lack of libraries and tooling. Lua does not come 'batteries-included' like Python or Ruby. It does not come with a package manager. There isn't even a standard linter (luac -p is closest). This is because Lua is an embeddable language, first and foremost. It forgoes the batteries. It expects you, the embedder, to provide them. LuaRocks is the most popular package manager for Lua and is easiest method to provide batteries.