Yesterday we concluded the live session on live coding a bytecode compiler and interpreter (VM) for a tiny subset of Python in Python. Even though I said I will not be sharing the recording, I think the session went quite smooth so I am sharing it here.
The original goal of the session was quite ambitious to try to implement a non-trivial subset of Python in 3 hours which included loops, conditionals, and functions apart from the basic syntax. We could only get to a small subset of what we planned, but that’s the nature of live coding.
Note that we did not talk about tokenization or parsing, we used Python’s ast module to avoid writing the parser. Discussing or implementing a parser can take several hours in itself and it isn’t as interesting as the compiler, so that wasn’t on the agenda.
Within three hours we were able to cover this much:
What a compiler does and its components
Structure of Python’s abstract syntax tree (AST), and a walk through some of the node types that we would be using in our compiler
Implementing a tree walking interpreter—we started by learning how to evaluate a program by walking an AST.
Implementing a bytecode compiler and VM incrementally. We covered these features:
Handling constants
Assigning values to variables
Being able to get values back from variables
Handling binary operations
The original objective was to also cover if/else, while loops and function calls. But it would have probably taken another couple of hours or possibly more.
Prerequisites
If you want to watch it but are concerned about prerequisites, here’s what you need:
Python
Pattern matching syntax of Python—if you’ve used pattern matching in other languages such as Rust, Java, Scala, it should not be a problem to understand Python’s pattern matching.
Recursion—the compiler requires tree traversal to generate the bytecode
Future Iterations
Many people were interested in the session but could not attend because of schedule conflicts.
I will plan it again, but I will most likely have to break it down into multiple parts which might mean multiple committing multiple weekends to it.
I will plan it again if there is significant interest. Please let me know by voting below.
Slides and Code
You can find the slides and code from the session at below links: