5

Book review: "Crafting Interpreters" by Robert Nystrom

 2 years ago
source link: https://eli.thegreenplace.net/2022/book-review-crafting-interpreters-by-robert-nystrom/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Book review: "Crafting Interpreters" by Robert Nystrom

July 11, 2022 at 20:10 Tags Book reviews , Compilation

A comprehensive introduction to writing an interpreter for a dynamically typed, object-oriented toy programming language (Lox). Throughout the book the author develops two complete interpreters for the language from start to finish, including all the front-end and backend parts. The first is a simple AST walking interpreter implemented in Java, and the second is a moderately optimized VM written in C, including a garbage collector.

Overall, this is a great teaching approach and it mirrors the one I took in my Bob project almost exactly [1].

The book follows an interesting plan - all the code for both interpreters is fully included in the text and presented in pieces. You don't need anything additional - no external packages, no external repositories to look at. If you use the same language(s) the author chose, just read along and copy the code into your editor. It will work. In this approach lies the book's strength and also its weakness, as I will explain below.

If this is the first interpreter you ever write, I do recommend following this approach (using the same languages the book uses). Otherwise, you may be tempted to follow along but implement your interpreter in another language. This is much more challenging, though, especially with the second (C-based) interpreter which uses a lot of C idiosyncrasies. I personally reimplemented the complete first interpreter in Go as I went along with the book - this was very easy. For the second one I picked Rust and reimplemented about 2/3rds of clox in it before running out of steam. Since I already wrote Bob in the past I felt like I wasn't learning much new and I wasn't particularly interested in Lox's OOP features anyway.

I'll state the conclusion of my review here, before moving on: this book is really great. Especially if you haven't had too much exposure to writing compilers in the past. Among the many compiler books I've seen/read, this is by far the most practical. It's hard for me to judge how beginner-friendly it is since I have quite a bit of background on this subject, but coupled with a more theoretical compilation book or course, it should be absolutely terrific to get anyone started. The attention to detail is nothing short of amazing - I don't think I found a single mistake in this book, even a typo. The code is well thought out, the explanations are solid and the diagrams are delightful.

Now on to some (minor) criticism: remember how I said that placing all the code in the book is somewhat of a weakness? Well, this is for a few reasons. First, the book is enormous. Long, large, heavy. It's hard to read the physical book in any other way than laid flat on top of a table. Second, because it's so large and packed already, the author left quite a few things out, particularly testing. There's precious little actual Lox code in the book, and no testing strategy. Testing interpreters well is relatively easy and very powerful, especially when implementing multiple ones for the same language. I do understand the space constraints, however.

Also, I fell like the sticking to single-pass compilation needlessly complicates the interpreter, and is unwarranted in this day and age. But doing a two-pass compiler would likely have made the book even longer.

A more subtle issue with writing all the code down in sequence is that the author tries very hard to keep it working at all times without refactoring too much, and this means that early code sometimes has subtle provisions for the future. Especially when re-implementing in another language, these are a constant source of grief - "oh I don't need this field in my language", and 50 pages later "oops, now I understand why it was there". It's full of Chesterton's fences! These are excusable for an old and dusty code base, but not for new code being developed on the fly. So my advice to you, if you're reading the book and implementing along in another language - beware of these! Read a chapter or two ahead before going back and actually implementing.

As I said, these criticisms are minor, and I understand the constraints the author was acting under - making the book even larger would have been difficult. Overall, it's a very good book; it fills an important niche in the compiler literature, and someone just had to write it. I'm happy Bob Nystrom did.


[1]Bob is a Scheme implementation with a simpl-ish interpreter in Python, as well as a compiler written in Python targeting a VM bytecode; the other implementation is a VM for this bytecode implemented in C++ that includes a garbage collector.

Recommend

  • 30

    Our little VM can represent three types of values right now: numbers, Booleans, and nil . Those types have two important things in common: they’re immutable and they’re small. Numbers are the largest, and th...

  • 42
    • www.tuicool.com 5 years ago
    • Cache

    Local Variables · Crafting Interpreters

    Thelast chapter introduced variables to clox, but only of the global variety. In this chapter, we’ll extend that to support blocks, block scope, and local variables. In jlox, we managed to pack all of that a...

  • 52

    Any problem in computer science can be solved with another level of indirection. Except for the problem of too many layers of indirection. David Wheeler This chapter is a beast. I try to break features...

  • 21
    • craftinginterpreters.com 4 years ago
    • Cache

    Crafting Interpreters: Classes and Instances

    Caring too much for objects can destroy you. Only — if you care for a thing enough, it takes on a life of its own, doesn’t it? And isn’t the whole point of things — beautiful things —

  • 11
    • craftinginterpreters.com 4 years ago
    • Cache

    Crafting Interpreters: Superclasses

    You can choose your friends but you sho’ can’t choose your family, an’ they’re still kin to you no matter whether you acknowledge ‘em or not, and it makes you look right silly when you don’t. Harper Lee, To...

  • 15

    Book review: Rich Dad Poor Dad by Robert T. Kiyosaki Published: 2020.10.14 | 1 minutes read Personal finance is something that interests me a little bit more recently. Am I becoming mature n...

  • 6

    Why I Wrote a Book About Interpreters 30 Nov 2016 Last week I’ve self-published my first book called “Writing An Interpreter In Go”, which you can get at interpreterbook.com. I w...

  • 5
    • www.se-radio.net 2 years ago
    • Cache

    Episode 486: Bob Nystrom on Dart

    Episode 486: Bob Nystrom on Dart Filed in Episodes by

  • 1
    • chidiwilliams.com 2 years ago
    • Cache

    Crafting Interpreters: A Review

    Crafting Interpreters: A Review Jun 21, 2022 • 6 min read

  • 5

    RC Week 12: What's Next, and Speedrunning Crafting Interpreters Saturday, December 10, 2022 And that's it. My batch at RC ended yesterday. I have so many thoughts and feelings from this time, bu...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK