13 ms·
Crafting Interpreters: A Review
- vertexmachina 4y agoI'm in the middle of this book at the moment and I have mixed feelings on it. It's definitely well-written and you can feel the love and care that went into producing it. But I think it would have been stronger had Nystrom skipped the Java version and spent those pages on theory instead before jumping into the C implementation. While going through the Java stuff (implementing in C# instead because I have an emetic reaction to Java) I found myself just wanting to hurry up and get to the good stuff in C. And I found the visitor pattern and code generation stuff to be a distraction. The code can also be a bit hard to follow at times because he jumps around from file to file and function to function. He'll present a function that calls a function that doesn't exist yet, and then define the new function, etc. He does it that way because he wanted to ensure that every single line of code was present in the book (he wrote a tool to ensure that was the case), and it certainly helps if you're trying to copy his code verbatim, but not so much for high-level understanding. Maybe that's a failing on my part. Finally I wish he had gone into more detail on how one might implement a register-based VM because they are faster and more interesting (to me) than a stack-based one.
- native_samples 4y agoThe Java part is probably very lucky to have if he ever writes a second edition though. The reason is, for many dynamic languages the best way to make an interpreter fast is now to use the Truffle framework, not write a bytecode interpreter in C. Truffle changes the whole space around language interpreters so radically that it feels like it should definitely be worth a mention in any future take on the topic. With Truffle you start with a Java based tree walking interpreter (could use Kotlin too for less boilerplate), so the very easiest stuff. Then you annotate it in some places, add a dependency on Truffle and ... that's it. Now you have a JIT compiler for your interpreted language. The next step is to start refining the use of the annotations and to write specializations in order to accelerate your JIT by incorporating standard techniques like polymorphic inline caches. Finally you can compile your new interpreter+JIT engine down to a Graal native image (single standalone native binary), thus ensuring it can start as fast as an interpreter written in C and can warm up as fast as V8. The binary can also be used as a shared library. Given that this tech exists now, people who choose to write their interpreter in Java will have a massive edge in performance over people walking the traditional path. It's still relatively new technology but it's hard to see how it doesn't change interpreter design forever.
- Banana699 4y agoI sympathize with your criticisms about java because the language is... not my favorite. It would be helpful here to look at its choice as a result of Nystrom solving the intersection of multiple optimization problems: - Manual Memory Management Is Hard. Interpreters are complex pieces of software, you don't need another rabbit hole to dive into while you're learning your first parser. You don't need to agonize over where to put the contents of the file buffer you're parsing before writing your first lexing switch. People spend years with C and C++ and still get MMM wrong. The book is supposed to be fun. - Data Structures Are Hard. This doesn't apply to C++ or really any modern language, but since you wanted it done in C the first time, that would entail the obligatory "Implement your own universe from scratch" exercise C is infamous for. I don't mind, I always like implementing my own universes (although I despise C even more than Java, it can't be over-emphasized how badly engineered that language is). But again, Pedagogy says that you should introduce the minimum possible surface area while approaching a new topic, ideally a single topic at a time. - Interpreters Should Be Fast, so overly dynamic languages like python and javascript are out. - A teaching language should be popular and familiar. The obvious benefit is accessibility to as many learners as possible, but a less obvious one is the availability of tools and cross platform support. Out of the vast array of available programming languages and their toolchains, the combination of GC, powerful standard library and reasonable performance excludes a whole lot. The popularity requirement basically only leaves Java, C# and Golang standing.
- munificent 4y agoThat's a really good summary. I didn't pick C# in part because it feels more tied to Microsoft and Windows than I wanted the book's language to be. Java (to me at least) feels fairly platform and corporation independent. If you would have prefered I pick Go, you'll definitely like Thorsten Ball's two books. Choosing a language for books is really hard these days. There are so many choose from and most are quite large and complex, so it's hard to find a single language that is familiar to a large enough segment of the audience.
- b3morales 4y agoEh, you don't have to use Java for the first part. I didn't, and I've seen many other people in discussions of the book say they didn't either. It's readable enough, and the explanations are clear enough, that you can follow along in any other memory-managed language that you're comfortable with.
- sydthrowaway 4y agoJava is such a god damn turn off.
- EamonnMR 4y agoI think of it as an excuse to implement it in a language you actually like. Makes it more interesting and keeps you honest (since you can't copy pasta your way through it.)
- EamonnMR 4y agoI keep meaning to pick this back up. It was my go-to project during air travel and train travel for a while, but I stopped going in to the office so much and command line only projects have trouble competing for mind space with visual stuff, at least for me.
- wrp 4y agoThe literature on interpreters (vs. compilers) is pretty thin. I enjoyed reading these: 1. Peter John Brown. 1979. Writing Interactive Compilers and Interpreters. 2. Timothy Budd. 1987. A Little Smalltalk. 3. Ralph E. Griswold. 2000. The Icon Programming Language, 3e. 4. Pat Shaughnessy. 2013. Ruby Under a Microscope: An Illustrated Guide to Ruby Internals. This is a new one I haven't read yet, but it looks very practical: 5. Clinton L. Jeffery. 2021. Build Your Own Programming Language: A Programmer's Guide to Designing Compilers, Interpreters, and DSLs for Solving Modern Computing Problems. I don't know anything about this one, but people have told me they prefer the first edition: 6. Ronald Mak. 1991. Writing Compilers and Interpreters: An Applied Approach.
- mhh__ 4y agoI think this book is ever so slightly overrated when it comes to actually implementing programming languages, but it contains such a nice introduction to parsing that I have for years been using it as my goto "You're not ready yet, read this" book when it comes go guiding people going down the dark unhappy path of working on language implementations.
- jesselawson 4y agoPLT and implementing languages is something that's always fascinated me, so I enjoy hearing everyone's experiences in this domain. What makes you call it a "dark unhappy path"?
- mhh__ 4y agoI was being sarcastic but also noting that 85% of the work is not in a book
- icholy 4y agoI'm glad to see this book get the attention it deserves.
- geekraver 4y agoIf you’ve never read it, a fun article on Bob from some years back. https://www.wired.com/2013/01/comedy-in-the-hacker-world/amp https://www.wired.com/2013/01/comedy-in-the-hacker-world/amp I was lucky enough to be working on the Dart team in Seattle at the time with Bob, who is one of the smartest, funniest and nicest people I have ever met (although the whole team was an exceptional bunch).
- chapliboy 4y agoThe author says it took about 6 months to thoroughly go through the book and examples. This is really useful in setting expectations.
- thundergolfer 4y agoI got through all chapters in around 1.5 months, spending 50-60 hours[1]. I'd do solid 4-5 hour blocks on Saturdays and Sundays when I was most into it. I'm following teachyourselfcs.com[2], so I'm hoping to solidify the concepts the book intro'd by reimplementing the bytecode interpeter in Rust (rlox) and following Alex Aiken's lectures online. OP appears to have reimplemented Lox in Golang (glox) for reinforcement. So far the time has been well spent. Compilers are now much more 'just another program', and I have new conceptual tools for tackling software engineering problems. 1. https://github.com/thundergolfer/uni/tree/main/books/crafting_interpreters https://github.com/thundergolfer/uni/tree/main/books/craftin... 2. https://teachyourselfcs.com/#languages https://teachyourselfcs.com/#languages
- dragon96 4y agoAs another data point, I've taken about 1.5 months for Part 1 of the book, chipping away at it for 1-2 hours on weekday mornings.
- RheingoldRiver 4y agoDid you read Computer Systems: A Programmer's Perspective first? I read about 300 pages of it (maybe 200 only, I forget) and got _extremely_ bored. I want to read Crafting Interpreters but I'm concerned that Computer Systems is too much of a prerequisite.
- thundergolfer 4y agoI read the first 3 chapters and worked through the in-chapter exercises (not the end-of-chapter exercises). I think that’s about 150-200 pages. Yeah it’s quite a slog. I think I averaged 10-20 min per page of progress. I intend to return and finish it but it will take me ages. It’s not a prerequisite at all though. Crafting Interpreters is pretty high level, in comparison.
- hyperzhi 4y agoI've been wanting to read this book, but I'm not sure how I should go about it. Are you supposed to read and type out the code examples? I'm just wondering if I will learn anything that way?
- chidiw 4y agoYes, I read and typed out all the code snippets by hand and it worked really well.
- thundergolfer 4y agoAnd don't be too careful in your typing either. The times when I mistyped and had to crack out `gdb` were where my learning was actively tested.
- lolinder 4y agoHe does a great job of breaking down the code samples into chunks that are small enough to be meaningful. The longest chunk I remember seeing was around 10 lines. Each of those small blocks of code is surrounded by lots of very helpful and approachable explanatory text. The results was, at least for me, that I never felt like I was copying something I didn't understand.
- dgb23 4y agoWell yes, the code is pretty much there 1:1. However the juice is in the straight forward explanations and excellent writing. I used the book by translating the code into my favorite programming languages and heavily played around with different approaches, which is way more time consuming but I don't think it is necessary at all. I think if you are already a seasoned programmer you might find it even useful to just read the book and the code without doing much of coding at all. Given that you are good at understanding code without executing it.
- scrapheap 4y agoI read it through but didn't type out the code, mainly as I know that I would get focused on the code rather than the concepts the book is teaching.
- d23 4y agoThis book is a work of art. It's one of the best technical books I've ever read. Truly a masterpiece along every dimension.
- marai2 4y agoAs the footer on the online version of the book says "Handcrafted by Robert Nystorm" - it really is lovingly hand crafted. It shows that the author went over the content many, many times polishing it with every go. The content builds up really well as it goes along. On a minor note, whenever I read a technical book I always come across some mistake in the copy-editing, a missed article, a misplaced article, a typo. I'm 70% of the way in and haven't come across any! Which again speaks to the well-craftedness and hand-craftedness of the book! Though I do feel the author ran out of steam with respect to the lovely, quirky artwork in the book. I'm disappointed Bob, I want my money back.
- mhoad 4y agoI’m pretty involved in Dart these days which is what Bob works on full time now, it’s one of my favourite things about the language, there are so many freaking smart people on a totally different level doing some very cool stuff behind the scenes there with the same level of love and care that went into that book.
- munificent 4y ago> Though I do feel the author ran out of steam with respect to the lovely, quirky artwork in the book. Definitely true. I was hoping to write the book in two years and it took me six. By the end (which was also during the pandemic) I was pretty fried and it was hard to summon much energy for whismy. I tried to pack as many illustrations in as I could, but by later chapters I was really just trying to get it done. Also, pragmatically, I found the chapters on the bytecode VM really needed a lot more useful illustrations for things the execution flow so that left less space for the fun ones. > I'm disappointed Bob, I want my money back. Sorry, all sales are final. I've already spent the money on whiskey and synthesizers. That's what you get for not scrutinizing the later chapters on the free web version first to ensure that they were up to your standards.
- recursivedoubts 4y agohighly, highly recommend this book when i was given a compilers class to teach i wanted to teach hand-written recursive descent because it is simple, elegant and makes the recursive nature of grammars so readily apparent without hiding concepts behind obscure tools i was despairing that there were no good books around on recursive descent, but stumbled on crafting interpreters before it was even released as a book and immediately structured my class around it absolute life saver and my students all love it cannot say enough good things about it!
- ufo 4y agoThis is one thing I find funny about this book. Despite the name, it's a great intro to compilers in general.
- alexobenauer 4y agoTruly one of the best books in our field; very well done. Doing the tree-walk interpreter (the first “half” of the book) took two weekends, working much of the day on Saturdays and Sundays. When I went through it, I built my interpreter in another language, and added other features as I went along. Both helped solidify the concepts. I often had to pull out my additional features as I learned better in subsequent chapters; getting to see the difference between my amateur approach versus the author’s experienced methods was quite helpful!
- dingosity 4y agoThis is the type of content I come here for. I also enjoyed the "other posts" listed at the bottom of this article. Well done, Chidi!
- ParallelThread 4y agoWhat other books are of the same caliber that HN would recommend?
- vertexmachina 4y agoCODE: The Hidden Language of Computer Hardware and Software - Charles Petzold The Elements of Computing Systems - Noam Nisan, Shimon Schocken Operating Systems: Three Easy Pieces - Remzi H. Arpaci-Dusseau, Andrea C. Arpaci-Dusseau
- codezero 4y agoHuge plus one for OS: Three Easy Pieces. It sticks in my memory forever and I just confirmed, I had a random question about "why" from part of that book in 2014 and emailed Remzi and within a few days had a very detailed response back. It was much appreciated because I can get hung up on little details that I can't explain and it can really create a wall for me, so thanks Remzi for being responsive to a learner!
- runevault 4y agoWorth mentioning (I think it was elsewhere in the overall thread) CODE is getting a 2nd edition later this year. I've always meant to read the book but never did, so I'm planning on using the second edition as my excuse to finally do so.
- alfiedotwtf 4y agoIf by caliber you mean blow your socks off, The Little Schemer
- 323 4y agoThe programming meme books that you must have on your shelf (reading optional) are: - The C Programming Language (original C book) - The Art of Computer Programming (TAOCP) - Structure and Interpretation of Computer Programs (SICP) - Compilers: Principles, Techniques, and Tools (Dragon book) - Don't Make Me Think (design) - Zen and the Art of Motorcycle Maintenance
- Noe2097 4y agoFollowing a slightly different path, "Modern compiler implementation" goes over several topics mentioned in the review (lexing, parsing, type checking, code optimization and generation, runtime environments aspects e.g. garbage collection). There exists various flavors of the book (in ML, Java, and C).
- chidiw 4y agoThere's also Thorsten Ball's Writing an Interpreter in Go[1] and Writing a Compiler in Go[2]. [1] https://interpreterbook.com/ https://interpreterbook.com/ [2] https://compilerbook.com/ https://compilerbook.com/
- zvmaz 4y agoExcellent book indeed. Note that Ball's interpreter uses Pratt parsing, which doesn't require you to delve into the theory of formal languages.
- shadowofneptune 4y agoI've found Pratt parsing meshes rather well with formal grammars. Anything that is better described by an operator precedence table rather than BNF should be handled by the Pratt parser, rest can be done with recursive decent.
- munificent 4y agoYes! Thorsten's books are great!
- Ambroisie 4y agoThe only worthwhile version is the ML one though, see https://assignments.lrde.epita.fr/tools/modern_compiler_implementation/first_editions.html https://assignments.lrde.epita.fr/tools/modern_compiler_impl...
- deleted 4y ago[deleted]
- shaftoe444 4y agoThis is an excellent book. I got so much out the first half I keep building little toy interpreted languages and haven't got round to the second half yet!
- cpard 4y agoAnd it's so much fun to go through. Most CS books are usually very dry, this one is such a fun read. I would encourage everyone to go and read at least the web version of the book, I'm sure that you will end up buying it at the end.
- respondo2134 4y agoNand2Tetris is another fun one IMO, you can do a chapter a week (an hour of reading and a couple of implementation sessions) and be done in a few months
- cpard 4y agothis is awesome! I wasn't aware of it, now that I'm reaching my time for a midlife crisis, I'll give it a try as a way to feel young again and remember my college days. Thank you!
- collinvandyck76 4y agoThis is the kind of book that really pays dividends into the future. Once you realize that lexing and parsing is not really that difficult, it makes a whole class of problems much easier.
- pdpi 4y ago> it makes a whole class of problems much easier. The other big realisation is just how freaking enormous that class of problems is.
- lordleft 4y agoBeautiful typeface. Clear writing. Passion and humor seeping out of every page. The author has also kindly made a web version of his book freely available: https://craftinginterpreters.com/introduction.html https://craftinginterpreters.com/introduction.html
- Derbasti 4y agoWithout a doubt one of the best technical books I have ever read. To me, it was a missing piece of the big puzzle of "how do computers work". I read many a book to answer this question, and came away with three books: - CODE by Charles Petzold explains the CPU - Operating Systems: Three Easy Pieces by Arpaci-Dusseau explain OSes - Crafting Interpreters by Robert Nystrom explains programming languages Masterfully done.
- skrtskrt 4y agoAny defining book for networks?
- zvmaz 4y agoTCP/IP Illustrated, Volume 1, Richard Stevens.
- hnxs 4y agoFYI there is a second edition of CODE coming out at the end of August. I will check out Three Easy Pieces! Thanks for the rec.
- password4321 4y ago“Code” 2nd Edition https://news.ycombinator.com/item?id=31696901 https://news.ycombinator.com/item?id=31696901
- easterncalculus 4y agoI have started reading Introduction to Computer Organization by Bob Plantz, and I think it would also tell someone a lot about how computers work. It starts with hardware and builds into Assembly and eventually higher level concepts like OOP.
- volkadav 4y agoSimilar vein re: CPU, targeted at an undergrad level, you might enjoy _Computer Systems: A Programmer's Perspective_ by Bryant and O'Hallaron (https://csapp.cs.cmu.edu/ https://csapp.cs.cmu.edu/) At least I found it a really well-written and integrative text coming from knowing C and wanting to learn the next level down well. Ties in with the OS bits on the "top side" too.
- twh270 4y agoHas anyone worked their way through both this and Beautiful Racket? I'd love to get a comparison of your experience with both.
- nphard85 4y agoJust completed part 1 (used Python instead of Java to spice it up a bit) and it was a great experience! Looking forward to working through part 2, but it will be a while before I can get around to it. I hope more books follow this engaging writing pattern (when it makes sense). Kudos to Bob Nystrom for this great book.
- xbar 4y agoThanks Chidi for a great review and a blog with a lovely design.
- mengibar10 4y agoBeautiful typeface, well organized and very appealing to eyes.
- munificent 4y agoThank you for the excellent review! Also, I'll second the comments here: The design of your blog is beautiful.
- michael-go 4y agoDear Bob, in case it helps FYI both the book's website http://craftinginterpreters.com/ http://craftinginterpreters.com/ and your blog https://journal.stuffwithstuff.com/ https://journal.stuffwithstuff.com/ seem down right now. And if anyone was curious like me to see the book's website, here is a archive link: https://web.archive.org/web/20220616005500/http://www.craftinginterpreters.com/ https://web.archive.org/web/20220616005500/http://www.crafti...
- munificent 4y agoThey appear to be up for me now. Not sure if it was a temporary glitch or got swamped or what.
- chidiw 4y agoYou're welcome - and thank you!
- MarquesMa 4y agoThis is my favorite book! But I think it could be more straight forward if the first interpreter didn't use Java, but a modern main stream language that can avoid code generation and Visitor Pattern. Those parts feels like unnecessary detour.
- munificent 4y ago> but a modern main stream language that can avoid code generation and Visitor Pattern. Yeah, a language with nice algebraic datatypes and pattern matching does eliminate the need for Visitor patterns. But the reality is that many programmers are working in object-oriented languages where the Visitor pattern is useful, so I deliberately picked Java in order to teach that approach. The book tries to meet you where you are and not where you might wish you were.
- bow_ 4y agoI hesitated a little bit before deciding to purchase and go through the book. Had already read many glowing reviews then, but was not sure if the choice of language (Java) for the first half of the book would be a hindrance (it's not the stack I am most familiar with). Decided to buy it anyway, primarily because the two implementations of the toy language piqued my interest (most other books only go through one implementation). Boy was I glad I did. Writing is superb, end-of-chapter challenges are instructive, code samples are meticulously and beautifully done. Here and there I also encountered references to other languages (e.g. had a brief detour to Lua internals [1], thanks to a side note), which I have enjoyed too. Went through the first half twice: first using Java as it is written and then secondly implemented the interpreter in Rust (my initial fear turned out to be unfounded). Currently going through the second half. So if you're reading this, thank you, Bob :). [1] https://www.lua.org/doc/jucs05.pdf https://www.lua.org/doc/jucs05.pdf
- munificent 4y agoYou're welcome! :D
- danhau 4y agoDoes the book go into more „modern“ parser architectures ala Roslyn Red Green Trees?
- llimllib 4y agoYou can read it on the web here: https://craftinginterpreters.com/contents.html https://craftinginterpreters.com/contents.html (I don't know how to answer your question accurately)
- throwaway17_17 4y agoThe parser’s data structure is a more traditional Abstract Syntax Node design. Roslyn’s tree design is, IMO, a good internal structure for use in a production-grade compiler meant to be coupled with a developer environment (IDE and other tools) because the complexity pays off in terms of developer experience. However, Bob set out (as far as I can tell from the book and his talks/comments on the subject) to provide a fairly beginner friendly, introduction to the topic of language implementation. Within his chosen scope the parser implementation in Crafting Interpreters is more than sufficient and clearly presents the concepts intended.
- hardwaregeek 4y agoI love this book! My only gripe is that I wish it was a whole series, no, a whole genre of books. We need more hands on, code first books that take you through 1-2 extended projects where you can type along and get working code at the end. These books should have CS theory, but still be fundamentally focused on implementation. It'd be really fun to have one of these for a more functional compiler (type checking, code generation, optimization), for a language service (query based compilation, concrete syntax trees, language server protocol), for databases, operating systems, and so on. Ideally they'd be a cross between an O'Reilly book and a SerenityOS stream
- dunefox 4y agoYeah, I'd love to read this book with a more functional language as target, and compilers as you mentioned.
- xrobledo84 4y agoI agree. I would also like an example with static typing. Also I would like a book on how to create a visual scripting language.