r/ProgrammingLanguages • u/JanBitesTheDust • Sep 04 '22
Discussion Book recommendations after reading “crafting interpreters”
Hello, I finished the book crafting interpreters by Robert Nystrom. The book has helped me alot and felt like an amazing introduction to the field of language design and implementation.
My question however is: what next to read? I know of the dragon book and have read the first couple of chapters. But maybe there are better alternatives. Also, after crafting interpreters, i have a basic understanding of interpreted language design. However, I have the urge to study compiler design.
So are there any books you would recommend me for my level of knowledge?
51
u/mikemoretti3 Sep 04 '22
From what I remember, the book "Engineering a Compiler" goes over pretty much every part of a compiler (and you can probably skip the lexer/parser chapters since they'll probably duplicate much of "Crafting Interpreters"). There are some books specific to type theory if you want to learn more about that as well, "Types and Programming Languages", and the "advanced" sequel. If you're looking for something more like "Crafting an Interpreter", there may be other books (I think "Modern Compiler Implementation in Java" or "in C" or "in ML" by Appel). I wasn't a huge fan of the Appel book in Java when I first read it though, but I can't remember why.
10
u/agumonkey Sep 04 '22 edited Sep 04 '22
I only skimmed through some but there's:
ps: if I may add, Friendman series The little schemer, the reasoned schemer, the little typer are also very enlightening (if you can fathom the socratic discourse style)
2
u/kerkeslager2 Sep 05 '22
One important thing to realize is that if you've built out the CLox interpreter in Crafting Interpreters, you've built a compiler.
From the outside, this looks like an interpreter because it's compiling to a bytecode which is only runnable via the virtual machine you build from the book, and then the bytecode is lost after you finish running, because it's only stored in memory. But if instead of running the bytecode, you stored it to a file (similar to a .jar, for example) and then separated out the VM and made it load bytecode from the file, that would make it visible that you're actually compiling, without fundamentally changing much of the implementation.
The biggest difference between this and a more common conception of a compiler is actually what is generated; this compiler generates CLox bytecode, whereas a more common conception of a compiler would generate something like x64 assembly or LLVM assembly.
So the biggest gap in your knowledge after reading Crafting Interpreters is probably assembly. As such, I'd point you to My First Language Frontend with LLVM (skip to chapter 3, you already know how to lex/parse from Crafting Interpreters) or Art of Intel x86 Assembly (avoid the "modernized" Art of Assembly Language which teaches you "High Level Assembler", which is neither high level nor assembler). The LLVM route is probably more applicable to modern general-purpose language compilers, but the x86/x64 route is probably better for learning actual assembly, and techniques from that will be portable to other assemblies without relying on LLVM or similar tools.
2
u/mttd Sep 06 '22
From the previous discussions:
Compilers Books
I'd indeed start with "Engineering a Compiler" by Keith Cooper and Linda Torczon. I also like "Modern Compiler Design" by Dick Grune, Kees van Reeuwijk, Henri E. Bal, Ceriel J.H. Jacobs, and Koen G. Langendoen (https://dickgrune.com/Books/MCD_2nd_Edition/). Some chapters are better in one than the other, so you may read some of both to see if you like another explanation.
For more on the analysis & compiler optimization side, "SSA-based Compiler Design" is a good follow-up (original URL (inactive): http://ssabook.gforge.inria.fr/latest/; GitHub Mirror: https://github.com/pfalcon/ssabook, PDF: https://pfalcon.github.io/ssabook/latest/; to be published in 2022: https://link.springer.com/book/9783030805142).
Further readings: Book recommendations in https://github.com/MattPD/cpplinks/blob/master/compilers.md#books as well as program analysis resources (in particular lattice theory, type systems and programming languages theory, related notation): https://gist.github.com/MattPD/00573ee14bf85ccac6bed3c0678ddbef#program-analysis-resources
If you're interested in computer architecture background (relevant for the back-end optimizations), see computer architecture books & computer architecture courses. Personally I'd definitely recommend Prof. Onur Mutlu's Lecture Videos and Materials - http://people.inf.ethz.ch/omutlu/lecture-videos.html - fantastic lecturer and more up-to-date than textbooks (e.g., branch prediction lectures discussed modern TAGE and neural network predictors a few years before they've been implemented in, say, https://en.wikichip.org/wiki/amd/microarchitectures/zen_2#Branch_Prediction_Unit; in contrast, most textbooks stop at the level of two-level / saturating bits BPs from the early 1990s).
The CS 6120 course (see below) blog is a great resource for writeups on techniques and papers: https://www.cs.cornell.edu/courses/cs6120/2020fa/blog/
Compilers Courses
I can recommend the following: https://github.com/MattPD/cpplinks/blob/master/compilers.md#courses
Particularly (in alphabetical order--I think these are all great, so including highlights of what I've liked about them):
12
u/AnxiousBane Sep 04 '22
There are the writing an interpreter/compiler in go books by Thorsten Ball
10
u/ve_era Sep 05 '22
You can also read through An Incremental Approach to Compiler Construction to learn about compiling an AST to assembly.
3
u/Hinata-Hoshino Sep 05 '22
Engineering a Compiler is a good choice for the next step in my view, it includes SSA and other foundations for code improvement. And if you want to know more about the Type System or programming theory, a good choice may be is Software Foundations. And, I think you need to read the LLVM tutorial if possible.
2
u/somerandomdev49 Sep 05 '22
If you already did, nevermind, look at other comments :) but make a bunch of tiny languages, maybe all unfonished, not working, it doesnt really matter! you will learn a lot just with that :)
2
u/lazyear Sep 05 '22
Types and Programming Languages. Not directly related to compiler design, but having solid fundamentals of type systems will make your life easier!
1
u/Passname357 Sep 05 '22
I loved the dragon book. I’ve heard it’s a little bit outdated, but the theoretical concepts are still true. The fundamental grammar and regex structures are true, and the code gen and optimization stuff haven’t become false.