Compiler Fun
This past spring I took my college’s compilers course which was an absolute blast. During the course, my partner and I wrote a compiler in OCaml that compiles a custom functional language straight down to x64 assembly. After the course finished, I spent a bit of time working on adding some more features to my compiler. My current feature set is:
- Basic integer arithmetic (+, -, *, /, %)
- Strings and various string operations (concatenation, char_at, etc)
- Mutable tuples including tuple unpacking
- Defining and calling functions
- Higher ordered functions
- Automatic garbage collection
- Defining, throwing, and catching exceptions
- Runtime type errors
- C bindings
I’m certainly not going to claim that it is a useful or novel language in any way, but it was a lot of fun to make. Working through implementing it made me far more comfortable with assembly. If you’re curious, I set up a simple godbolt.org-like playground that shows off some sample programs and the generated assembly. It also makes it possible to view which lines of assembly correspond to which expressions in my language. Check it out here:
http://daviddworken.com/compiler
Along the way, I also wrote a simple fuzzer for it that generates mostly valid programs and runs them through the compiler and an interpreter and compares their outputs. This caught a bunch of different bugs that otherwise I may have never found (ie some tricky memory alignment bugs).