Live data from Hacker News

Crafting Interpreters: A Review

chidiwilliams.com

61–70 of 155 posts

Re: Crafting Interpreters: A Review

#61

The author says it took about 6 months to thoroughly go through the book and examples. This is really useful in setting expectations.

I 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.…

Did 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.

Re: Crafting Interpreters: A Review

#62
I'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.

Re: Crafting Interpreters: A Review

#63
post #20
post #18

Following 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).

There's also Thorsten Ball's Writing an Interpreter in Go[1] and Writing a Compiler in Go[2]. [1] https://interpreterbook.com/ [2] https://compilerbook.com/

Excellent book indeed. Note that Ball's interpreter uses Pratt parsing, which doesn't require you to delve into the theory of formal languages.

Re: Crafting Interpreters: A Review

#64

What other books are of the same caliber that HN would recommend?

CODE: 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

Re: Crafting Interpreters: A Review

#65

Without 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…

Any defining book for networks?

TCP/IP Illustrated, Volume 1, Richard Stevens.

Re: Crafting Interpreters: A Review

#66

I'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 re…

The 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.

Re: Crafting Interpreters: A Review

#67

Without 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…

FYI 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.

Re: Crafting Interpreters: A Review

#68

Without 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…

I 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.

Re: Crafting Interpreters: A Review

#69

Without 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…

Similar 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/) 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.

Re: Crafting Interpreters: A Review

#70
post #3

I'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?

The way that worked better for me was doing the book in a different language (I did it in Crystal and Rust). I had to fully understand the intent being every code example so I could translate it correctly. This adds another layer of difficulty, depending on how comfortable you are with Java and the language of your choice, but it was the way that worked better for me. I truly felt that I created a language, not just…

I also took this approach, and I think it has really helped me understand what I'm doing as opposed to merely transcribing the code.

In my case, I chose to use C++ (more specifically, C++20 under GCC 11) with minimal external dependencies (currently just Boost for memory mapped IO and fmt). I'm working on the second part of the book now (only just getting started, really). My tree-walking interpreter works, but does have some limitations (I currently leak memory in a few locations, such as closures, due to reference-counting cycles).

I've had a few gotchas following along this way, but it's been entirely due to my choices.

One thing I'd highly recommend doing: setup tests to validate your code immediately! I don't have anything fancy setup, but basically what I have is mechanisms to hand a script to my interpreter and capture stdout/stderr. It's not the best level of testing, for sure, but it does allow me to basically place the lox snippets from the book into a reproducible framework to make sure my interpreter is doing what's expected. I also added tracing/memory dump facilities to my tree-walker to facilitate debugging as well (the byte-code interpreter builds this in from the start, so I've not deviated in that aspect, yet).

I'm really enjoying the book. It's definitely created a spark in me I've not felt in quite a while.

Post reply on HN