Live data from Hacker News

Crafting “Crafting Interpreters”

journal.stuffwithstuff.com

61–70 of 76 posts

Re: Crafting “Crafting Interpreters”

#61

This is amazing! I remember when you started this, and have been somewhat following along since then. This is a masterpiece. Is there any way to pay you for this book? :-)

Not yet, no. But thank you, very much. Once the print and eBook editions are ready, buying one of those will be the best way to say thanks. But, honestly, right now my family and I are in so much better shape than a lot of people. I'm able to work from home in a well-paid field. We're all safe and healthy. So if you feel like doing something kind for someone, instead of throwing some cash my way, consider donating to…

Thank you so much Bob, both for the awesome book and for thinking of others in this trying time. May God bless you and your family!

Re: Crafting “Crafting Interpreters”

#62
I usually don't buy computer books anymore because I can't really finish them. I went through this post and signed-up for the mailing list to notify when its available in physical form. The effort he put in is an inspiration - if he has the tenacity and this much pure love towards this work, I have to try harder to overcome my inertia and read it once it is available.

Re: Crafting “Crafting Interpreters”

#63

I've read the entire post and I still don't know how he did it. And his books are incredibly well written. The day after I announced publicly I'm writing a book, my wife announced she was pregnant. I still continued writing but the motivation of the first day was halfed. Two weeks later, she told me it was twins, I quickly decided to get a stable job. I still wrote in the evening and weekends. The kids came earlier t…

If you're doing a plug you might just as well tell us what you're writing about ;)

Re: Crafting “Crafting Interpreters”

#64

> Writing this all out makes me sound like a crazy person. What the hell am I doing with my life? Or, more importantly, what could I have been doing instead of doing all that? That's why your book is so good! Singular focus on one subject like this either causes insanity or genius. Examples: redis, Linux, sqlite, dwarf fortress, etc. I (believe, not sure) all were started by a single person who was insane by any meas…

I feel like this might be a required but insufficient condition for what Bob has achieved here. With both of his books, he has taken a somewhat dry topic (object oriented patterns, and now compilers / interpreters), and turned them into books anyone can understand. More importantly, he has somehow made them interesting , in a way that I have rarely seen before. I suspect you're right, the focus probably helps with th…

Because the topics were interesting to him, and his goal was to convey what made them interesting. All great teachers have this in common regarding the subjects they teach.

Re: Crafting “Crafting Interpreters”

#65
I have to ask about http://craftinginterpreters.com/introduction.html#snippets

> In the center, you have the new code being added in this snippet. It may have a few faded out lines above or below to show you where to insert it in the existing code. There is also a little blurb telling you which file and where in the file it goes. If it says “replace _ lines”, there was some previous code between the faded lines that you need to remove and replace with this snippet.

> ...lox/Scanner.java in scanToken() replace 1 line

How are these done? Are they compiled from actual diffs, or what? I looked at the source but the CSS/HTML just puts `source-code-narrow` in a class and doesn't tell me anything about them.

Re: Crafting “Crafting Interpreters”

#66
> get readers to understand there’s no magic in there and nothing keeping them out

This was a key feature of the book for me; reading it really helped me grok that compilers/interpreters are just programs. Very complicated programs, perhaps, but still many of the same concerns that apply to my day job writing applications. It's code all the way down, and that feels like a hugely useful insight to have. Plus I think I now have the language bug myself. :)

Well done, Bob, and congratulations! Can't wait to be able to buy a copy.

Re: Crafting “Crafting Interpreters”

#67

I've read the entire post and I still don't know how he did it. And his books are incredibly well written. The day after I announced publicly I'm writing a book, my wife announced she was pregnant. I still continued writing but the motivation of the first day was halfed. Two weeks later, she told me it was twins, I quickly decided to get a stable job. I still wrote in the evening and weekends. The kids came earlier t…

> we had to spend 2 months in the NICU. Writing took a backseat.

That sounds entirely like the right priority to me. Your book isn't going anywhere and kids change so fast at that age that it would be a shame to miss out.

Re: Crafting “Crafting Interpreters”

#68
post #65

I have to ask about http://craftinginterpreters.com/introduction.html#snippets > In the center, you have the new code being added in this snippet. It may have a few faded out lines above or below to show you where to insert it in the existing code. There is also a little blurb telling you which file and where in the file it goes. If it says “replace _ lines”, there was some previous code between the faded lines that…

The build system figures out all of this location information automatically based on the snippet markers in the code.

The code is pretty hairy since it's a pile of Python that sort of accreted organically as I was writing the book. I have been sorely tempted to rewrite the whole thing but abstained because I wanted to focus on writing.

The basic idea is that the build system walks every line of code. As it steps through, it keeps track of:

* What is the current source file. This is pretty easy to track. :)

* What is the current stack of snippet markers? The marker comments support nesting because it makes it easier to, say, introduce a function in one chapter and then insert a couple of lines into the middle later. The topmost snippet on the stack indicates which snippet owns the current line of code.

* What is the current surrounding declaration? Using some fairly hacky regexes, it tracks whether it is inside a class, method, struct, etc.

Given that, the build system knows for every line of code which snippet it first appears in, which snippet it disappears in if it's one that gets replaced, and the surrounding declaration if any.

When compiling a chapter, when it inserts a snippet, it finds all of the lines owned by that snippet. It also grabs some of the preceding and following lines (which is made trickier by the fact that some of the surrounding lines that may be textually present in the actual source file may not exist in the code as it is at the point in time that that snippet is inserted). And it looks for any lines that are removed by that snippet so that it knows what number to put in the "replace _" annotation.

From that, it can create the HTML that shows the code for the snippet, some surrounding lines if needed, and a location annotation describing the file, declaration, and number of replaced lines.

It's a pretty neat tool, though the implementation is fairly hacky. It's just good enough to produce the correct output for this one book and this book only. :)

Re: Crafting “Crafting Interpreters”

#69

> get readers to understand there’s no magic in there and nothing keeping them out This was a key feature of the book for me; reading it really helped me grok that compilers/interpreters are just programs . Very complicated programs, perhaps, but still many of the same concerns that apply to my day job writing applications. It's code all the way down, and that feels like a hugely useful insight to have. Plus I think…

> compilers/interpreters are just programs

And what's more they're simpler programs than most web applications. A compiler is conceptually a pure function. No need to worry about provisioning anything, any APIs, Docker images. A compiler is just a function from one array of bytes to another array of bytes.

Re: Crafting “Crafting Interpreters”

#70
I went through most of the Java half of this using c# right around the time that portion was completed. I think it may be time to build the C half, the question now becomes doing it in c or C++ (but still hand writing the hash table and similar things instead of falling back to the STL)
Post reply on HN