This is the repository where I keep all the projects I wrote while reading the excellent Crafting Interpreters by Bob Nystrom.
While the interpreters are written in D, I kept the original names.
Mostly straightforward translation from the book. The code was written while I was a D beginner and the goal was only to practice while reading Part II, so you shouldn't expect idiomatic code.
cd jlox
dub build
./jlox a_lox_source_file # interprets the contents of the file
./jlox # opens an interactive prompt (CTRL-C to quit)
Rather than generating the AST types from an external program, I chose to use the metaprogramming facilities of D, in particular string mixins and CTFE.
It is interesting that the code is basically identical to the one I would have written for the external generator.
This went through several iterations. I first tried to copy the visitor pattern from the book, only to find out that in D you can't derive from template functions (caveat emptor, I might be wrong.)
I then reimplemented Expr
as a std.variant.Algebraic
alias and used
std.variant.visit
to implement the printer.
Algebraic
was satisfying but as it so happens I watched the next day
Jean-Louis Leroy's presentation at DConf
2018 and decided to try his
openmethods instead.
I had some fun using a static foreach
to generate at compile-time case
statements for a long and boring switch. I also played with an alias with lambda
syntax and wrote a bit more unittests than usual.
This is a work in progress but I'm regularly working through the book.