Had a go at implementing Lox in Zig. It’s not complete, but can run a range of interesting programs. This is my first time through the book and might structure things differently next time now I know a bit more about how the code gets updated through the chapters.
Hope it’s useful to anyone else trying to translate the book into Zig.
thanks for sharing. I also have the book but didn’t get to read it yet, I have a couple questions
Did you have previous Zig experience? Do you think it can be interesting for learning Zig itself? Personally, I already implemented a couple small interpreters in Go but don’t have much Zig experience so I was looking on some nice projects to do
How long did it take? Is it something you can do in a weekend? The physical book I have right next to me looks scarily big
I do already have some Zig experience and understood the C and Java examples in the book.
The book is divided into two sections. The first half implements a tree-walk interpreter in Java and leans heavily on the Java runtime. I skimmed this, but didn’t write any code.
The second section implements a compiler and vm in C. I followed this, converting to Zig as I went along. There’s surprisingly little code, most of the book is explanations.
It took me about a week of a few hours each night. The book is structured well and leaves you with a working system at the end of each chapter showing a new feature.
(The downside of this is that there’s some refactoring to do as you go along which in my case would break all of my tests each time, so watch out for that)
To complement @tobyjaffey, I have implemented the interpreter from the book previously in Python, currently redoing it in Zig, trying to use the Zig parser itself as my main reference. Eventually I’ll be following up on this. I hope.
Coming from Go, I think the main thing that will trip you up is that Zig doesn’t have a language builtin for dynamic dispatch (interfaces), which the book uses pretty liberally in the first half.
Other than that, I think it’d be a good resource for learning Zig, as long as you’re relatively comfortable with pointers and aren’t scared of reading Java/C. It’s a surprisingly enjoyable read, too.
The first time I worked through the book it took about ~30 hours, on and off for two months.