News for hackers. Stuff that matters.
Re:Crafting Interpreters by munificent on Saturday July 13, 2024 @03:17PM (#40956186)
Author here. Seeing all of the positive comments about my book is really warming my heart. I appreciate everyone and I'm glad so many people have enjoyed the book. I put a ton of time and love into it and it's gratifying to see it had the effect I'd hoped for.
Re:Crafting Interpreters by AQuantized on Saturday July 13, 2024 @04:17PM (#40956565)
Game Programming Patterns is also excellent. There's something that feels quite 'honest' and straightforward about the style of both books. It sometimes seems like authors feel like they have to play along with the idea that it's all black magic, or maybe I'm just too stupid to understand GoF design patterns.
Re:Crafting Interpreters by WJW on Saturday July 13, 2024 @03:47PM (#40956380)
Thank you for writing the book! I'm currently almost done with the first part (doing it in Haskell because why not) and every chapter I encounter situations where my "clever hack" turns out not to be so clever in the light of later requirements. It's been great fun so far. Have you considered writing up a follow-up book doing (say) a compiler or a JIT or something for Lox?

PS: the lexical analygator is the best and has shown up (with attributions of course) in several of the internal presentations at $WORK. At one point we even had fan art of him on the whiteboard, but it had to be sacrificed when we had a particularly large diagram to put on there. :|

Re:Crafting Interpreters by iftheshoefitss on Saturday July 13, 2024 @03:55PM (#40956436)
On bro analygator is the best
Re:Crafting Interpreters by munificent on Sunday July 14, 2024 @05:55PM (#40963492)
It's my favorite illustration in the book too. :)
Re:Crafting Interpreters by iftheshoefitss on Sunday July 14, 2024 @06:12PM (#40963580)
on bro
Re:Crafting Interpreters by munificent on Saturday July 13, 2024 @05:52PM (#40957175)
Yes, people have definitely asked for a follow-up on compilers and/or static types. There's a little context here:

https://news.ycombinator.com/item?id=40956138

I'm not sure if I'm the best person to write a compiler book, or, at least, not yet. I've written a lot of language front ends and interpreters, but I don't have much experience targeting native code. It's something I've wanted to do for a long time, but haven't made the time to make it happen.

Static types are also hard for the reasons the linked comment talks about.

I still think about it sometimes, but writing a book is a lot of work and I've been enjoying not writing a book for the past couple of years. :)

Re:Crafting Interpreters by mike_hearn on Monday July 15, 2024 @12:33PM (#40969318)
You could do a compromise - write a book on crafting interpreters that use Truffle to be partially evaluated into compilers.
Re:Crafting Interpreters by purplerabbit on Saturday July 13, 2024 @04:59PM (#40956832)
Self-taught sweng here. (And late self-taught -- didn't really start learning programming until age 21.) Early in my first job, I was asked to design a simple query language and create an interpreter for it. I had absolutely no idea what was involved in interpreters.

I found your book online (it was less complete back then -- this was 2017), and in 3 or 4 days I had a working prototype. In another week or two, I had a working product. Thank you so much for creating this resource -- learning from it and leveraging it gave me a huge confidence boost, and was super interesting and fun. One of the very best resources I've come across. I still reference that project as one of my very favorite I've ever worked on. Best wishes

Re:Crafting Interpreters by keoghpe on Saturday July 13, 2024 @05:26PM (#40957025)
I LOVE this book. I have been dipping in and out the last few months implementing Lox in Rust.

It’s so well written, full of personality and beautifully illustrated - for me this book is a 10 and the bar I’d use to judge other programming books.

Re:Crafting Interpreters by HexDecOctBin on Saturday July 13, 2024 @08:49PM (#40958046)
Hi, loved your previous work on languages (Magpie, etc.). Any update on the statically typed language you were working on? Is it still in progress? The syntax for dealing with Sum types was pretty ingenious.

Thanks for both the books, cheers.

Re:Crafting Interpreters by munificent on Sunday July 14, 2024 @03:49PM (#40962794)
Still noodling on it!

I spent a lot of time trying to figure out how I wanted to handle generics and heterogeneous data in a way that felt simple enough for me to design and implement it. I think I have something more or less figured out. It's about a 50/50 blend of Go interfaces and Rust traits.

I have a prototype implementation in progress but haven't had much time to work on it. I broke my ankle a couple of months ago and while I've had plenty of time being stuck on the couch, I just haven't had the brainpower to work on it.

I haven't given up on it, though.

Re:Crafting Interpreters by HexDecOctBin on Sunday July 14, 2024 @10:06PM (#40964739)
Oh, get well soon! And best of luck on the project.
Re:Crafting Interpreters by Forricide on Saturday July 13, 2024 @11:24PM (#40958597)
Your book is legendary and I have recommended it to a good number of students/programmers-in-learning. Haven't ever made it through (no matter how good the textbook, I'm still not a textbook person...) but immense respect for the time and dedication required to make such a brilliant and engaging learning material.
Re:Crafting Interpreters by __loam on Saturday July 13, 2024 @11:49PM (#40958705)
Thank you for game programming patterns as well.
Re:Crafting Interpreters by kerkeslager on Sunday July 14, 2024 @12:47AM (#40958899)
That book is an absolute treasure and you should be proud.

At a personal level, this book contained so much that I had always wanted to know, from how hashmaps work, to how to compile in a single pass. And I connected with your note on getting into writing languages partly due to a feeling of being less-than programmers who could create their own language.

One thing I wanted to share: I found a small design change to Clox that eliminates an entire class of hard-to-debug GC bugs.

Basically, the change is that garbage collection never occurs during the execution of an instruction.

Instead, garbage collection is an instruction which is never emitted by the compiler. When the heap size crosses the garbage collection threshold, we schedule a garbage collection, which will occur after the current instruction is done executing, and before the next instruction executes.

To implement this, in the Self-Adjusting Heap[1] section, we set a global variable (let's call it "resume pointer") to the next instruction, similar to the return pointer in a call frame. Then we set the instruction pointer to point to a global constant which contains only the garbage collection instruction.

The way this works: let's say the current instruction allocates something, and the allocation needs to trigger a GC. To trigger the GC, we set the resume pointer to the next instruction, and the instruction pointer to the global constant garbage collection instruction (let's call this GCGCI). When the current instruction completes, the VM goes to the instruction pointer to get the next instruction, and finds it pointing to the GCGCI. It then executes the garbage collection instruction, which runs the garbage collection. The final step of the garbage collection instruction is to set the instruction pointer to the resume pointer (similar to a return pointer) so that when the VM goes to execute the next instruction it resumes the execution of the user program right where it left off.

This adds a bit of performance overhead to garbage collection, but I would guess we more than get it back from the fact that it's no longer necessary to temporarily push objects onto the stack to prevent them from getting GCed mid-instruction. And this eliminates that entire class of GC bugs; code stability and programmer time are worth something!

An alternative implementation might observe that the resume pointer is basically a return pointer, so you could potentially store this on the call stack. I didn't really think through the pros and cons of this as it seemed like just adding a global resume pointer solved the problem more simply. However, one thing I was working on is making my interpreter multithreaded, and this means that most of the global variables used by the VM get stuffed into a "Thread" struct. Keeping the size of this thread struct low is a high priority because that memory usage is a limiting factor in the number of threads you can reasonably run simultaneously, so in this case moving the resume pointer onto the call stack starts to look like a bigger benefit.

[1] https://craftinginterpreters.com/garbage-collection.html#sel...

Re:Crafting Interpreters by munificent on Sunday July 14, 2024 @03:53PM (#40962820)
This is a really really cool idea. Thank you for writing this up.

Yes, that sounds like it would work fine to me. The only downside to keep in mind is that you will periods of time where you need to allocate more memory and have to do that first and then only free up memory in a GC after that allocation takes place. If you're very memory constrained or you need to allocate a lot, that could potentially be a problem.

But if I'm totally honest, I'm not sure how big of a problem that is in practice. One of the really annoying things about garbage collectors is how little literature there is on real-world collection strategies. You can find plenty of papers on collection algorithms, but it's hard to find material on when a GC should run.

Re:Crafting Interpreters by kerkeslager on Sunday July 14, 2024 @05:29PM (#40963318)
Yeah, "When to schedule?" is a really good question.

Have you ever contacted the authors of The Garbage Collection Handbook[1]? It seems like they intend that book to have the most breadth of knowledge about GC, and if there's anybody out there who has studied when to collect they might know who.

[1] https://gchandbook.org/index.html

Re:Crafting Interpreters by munificent on Sunday July 14, 2024 @05:56PM (#40963500)
I haven't. That's a good idea. I'm pretty good at tracking down resources, but really bad at actually contacting humans.
Re:Crafting Interpreters by kerkeslager on Sunday July 14, 2024 @06:19PM (#40963620)
As a hobbyist freelancer with no access to resources like JSTOR, contacting the authors is often the only way I can obtain access to academic papers. It works great!

Incidentally, that book might contain information on when to GC; I haven't actually finished it yet.

Re:Crafting Interpreters by nathan_douglas on Thursday July 18, 2024 @08:55PM (#41001182)
That's a really neat idea! I think I'll try to work that into my own Lox derivative :)
Re:Crafting Interpreters by kerkeslager on Saturday July 20, 2024 @01:28PM (#41018079)
Let me know how it goes!
Re:Crafting Interpreters by Measter on Sunday July 14, 2024 @06:15AM (#40960001)
Thank you for putting the time into writing it! Your book was what really made it all click for me, and I'm currently working on my own static-type compiled language as a result. I plan on doing part 2 of your book in that language; it's going to be so horrific!
Re:Crafting Interpreters by ralmidani on Sunday July 14, 2024 @06:34AM (#40960091)
Hi Bob, I haven’t completed the book yet, but I wanted to note I appreciate you making it available for free online. I do my best to support authors like you (I have the book in print, PDF, and Kindle editions) and encourage others to do the same. Thank you!
Re:Crafting Interpreters by munificent on Sunday July 14, 2024 @03:41PM (#40962761)
Thank you for buying it!

Putting the book online is one of those things that turned out to be a real non-zero-sum proposition. It draws a lot of people into the book and gives them the ability to check it out before they buy it, which has certainly increased total sales and made me more money.

But, also, for those who can't easily afford it, it ensures they still have access to it, which is good for them.

Kind of an honor system market segmentation.

Re:Crafting Interpreters by psychoslave on Sunday July 14, 2024 @12:11PM (#40961721)
Would you be interested by translations? I can do it in French and Esperanto.

And by the way, thank you very much for the original version.

Re:Crafting Interpreters by munificent on Sunday July 14, 2024 @03:47PM (#40962785)
There are licensed translations from foreign publishers for several languages (which makes me feel like a Real Author :) ). Those contracts generally give the publisher sole access to translating in a certain language, so I have to be careful about saying yes.

I'm guessing no publisher will show up wanting to do an Esperanto translation. :)

I'd like to say "yes", but I also have to worry about sending a confusing signal to other people who might think I'm giving a blanket yes to translations.

I really do appreciate the offer, though.

Re:Crafting Interpreters by Cordiali on Monday July 15, 2024 @06:55AM (#40966721)
Your book, as well as 'Writing An Interpreter In Go', are two books I definitely want to read when I get more free time! I'd like to have a go at making a basic typesetting language, I have a bunch of ideas for it.