
Writing an Interpreter in Go: The Paperback Edition - misternugget
https://thorstenball.com/blog/2017/02/22/writing-an-interpreter-in-go-the-paperback-edition/
======
staticassertion
I bought this book and I've been using it - but I'm writing the interpreter in
Rust instead of Go. I really like it, and I think Go is actually a cool
language for this due to its simplicity. I had very little Go experience but
the language is drop dead boring, incredibly easy to pick up.

I'd recommend it.

~~~
pselbert
Funny, I did the exact same thing!

Tried to push learning Rust via the Go examples. It has been a great exercise,
but highlighted the pain of dealing with various string representations in
Rust.

~~~
staticassertion
I'm used to it I guess. Got &'a str all over my code.

------
joemi
Are createspace books still of a noticeably lesser quality printing than non-
print-on-demand books? I saw a few a while (a few years?) ago, and the quality
offended me, so I wrote off createspace. I'd be interested to learn that's not
the case anymore.

(Just to preemptively clarify: The bad quality I mentioned was most noticeable
when compared to a non-POD book. On its own, it looks OK-ish and you might not
think anything of it, but when you look at it next to another book you can
tell.)

The book itself seems pretty neat though! I'm a PDFs for tech books kind of
guy, personally.

~~~
wtbob
I used CreateSpace for a hardback book seven years ago (LaTeX → PDF), and it
came out very, very nice. Absolutely no complaints.

Was your experience with their paperbacks or hardbacks?

~~~
joemi
Given that it was a few years ago, I can't be certain, but I'm pretty sure I
only saw paperbacks.

------
dom96
This is really awesome. What I love especially is that the source code is
syntax highlighted, I really wish I could have gotten that for my book.

~~~
misternugget
I didn't really think about this that much before, it was more like "yeah,
full colors, sure, why not?". Holding the proof copy in my hands I realized
what a nice touch it was and that I haven't seen this in other programming
books. Of course, the full colors come with a cost, but once I saw it, I
couldn't go back to black/white :)

------
vortegne
I will be getting one, because I loved the book so very much. It helped me
tremendously when I was implementing my own little language.

~~~
misternugget
It makes me really happy to read that. Not because you want to buy two
editions, but because you apparently enjoyed the book a lot :)

------
joshbaptiste
Purchased an ebook copy (jfltech), while I will likely use Nim for my future
toy language, this will help me immensely.

------
Entangled
Monkey is such a nice language, clean and syntactically lovable. I wonder if
the same theory can be applied to make a space indented language like python
or nim, I'd like to make one.

~~~
coldtea
Yes, it can. Space indenting is just a small change to the lexer (or overall
parser).

Whether those see "\t" or "{" or "begin" it's not really different, it will be
the same "BLOCK_STARTS" kind of token.

~~~
WaxProlix
I don't think that's true - \t happens at the beginning of every line in a
block, often more than once, whereas BLOCK_STARTS and BLOCK_ENDS tokens are
pretty easy to track with a stack or whathaveyou.

~~~
coldtea
Of course you'll check the context too. But the same is also true for "{"
occurs in many places with different meaning -- in struts, inside text
strings, in interfaces (in Go), etc.

Besides it's not the \t that you need to find as a marker for e.g. scope
start. Python has "def xxx():" for example, so you see that and you know
you're in a function declaration scope, same way function (xxx) { in C works.

After you see that you only need to keep track of \t or " " in the beginning
of lines (before any other character) to know whether you're still in the same
scope or not. That's why you can't just have arbitrary whitespace count in
Python lines, but you need to maintain the same "indent" in the same scope.

------
zerr
It is becoming quite popular (or maybe it was always the case?) that someone
learning some topic and at the same time writing a book about it. Interesting
how it affects the quality of the content (versus books authored by persons
with expertise in given topics).

~~~
happy-go-lucky
My belief is that writing about a topic that one is trying to come to grips
with helps ensure they achieve the best possible learning outcomes.

~~~
zerr
The best learning outcomes for the author I believe. But I'm more interested
in the prospective reader's PoV.

~~~
misternugget
I recently read a comment (I think it might have been on HN, but I can't find
it) that said something like this: if you recently learned/mastered a topic,
you're in a far better position to teach that topic than an expert, who's so
deep into it, that they can't understand the beginners.

I wish I could find the source for this, because it was much better put, but
that comment contains the gist of my motivation behind writing the book.

~~~
andrewbinstock
The great benefit of experts over I-just-learned-this authors is that experts
know how design x or feature y relates to other designs and features not
implemented in the given project.

In other words, they know a lot more context and can tell whether something
that appears totally normal in the project is actually done in an unusual way
that has limitations, etc.

~~~
metalon
That is what is well-done in the Haskell book imho, the combination of someone
just learning everything and an experienced programmer

------
tfryman
I didn't see it anywhere, but does the print version come with the PDF version
too?

~~~
misternugget
Author here. Sadly, I can't offer digital+print bundles. I sell the digital
version through Gumroad and the paperback through createspace, so I don't have
to worry about printing/shipping. That makes it very easy for me to sell a
paperback version, but also that much harder to offer discounts or bundles or
anything, if you already purchased the eBook.

------
BlackjackCF
I'm definitely buying this. Still working my way through the PDF version, and
it's just so well-written and helpful!

------
AYBABTME
Anyone can share what sort of interpreter this is? Is it a walking-AST kind of
thing, or something more advanced?

~~~
misternugget
It's a AST walking interpreter, completely built from scratch, including the
lexer, the parser, the AST and, of course, the walking/evaluation step.

~~~
AYBABTME
Cool, asking because the level _past_ the AST walking is where I'm at
personally in my learnings. I feel most treatments stop at the AST interpreter
step, which is what most people see in school already. I wish there was
equally good treatment for everything that comes after that. Type systems,
bytecode generation, JIT, optimization phases, etc.

~~~
abecedarius
Lisp in Small Pieces is pretty good for that.

~~~
AYBABTME
Cool I'll check it out!

------
munificent
Ordered my copy. :)

------
wjh_
I think I might be buying this, seems like a good book! Have never heard of it
before.

------
rargulati
Just got the paperback version - excited to dig into this. Signed up to the
mailing list as well. Is there a central place for errata, or will that only
be made available as updates to the pdf version?

------
spraak
Does anyone have some resources or examples of writing a Scheme in Go?

~~~
archevel
I have been writing one available at
[https://github.com/archevel/ghoul](https://github.com/archevel/ghoul) it is
not finished in any way shape or form. I'm currently rewriting the evaluator
to do tail call optimization in my spare time and when that's done I'll work
on handling macros.

If you want a more complete one you could check out zygomys
[https://github.com/glycerine/zygomys](https://github.com/glycerine/zygomys)

~~~
spraak
Thank you!

------
xenihn
I will definitely be grabbing a copy of this and going through it, thanks!.

Anyone have recommendations for a book or tutorial for creating a REST API
with Go?

~~~
roberte3
This book is a pretty good tutorial on getting web service with rest up and
running. [https://www.manning.com/books/go-web-
programming](https://www.manning.com/books/go-web-programming)

------
TurboHaskal
A printed Go book with full-color syntax highlighting? Heresy!

Ordered the non dead tree version as the sample chapter is of great quality.

------
orloffm
So what was the source format? Markdown?

~~~
misternugget
Yes. 5-6 markdown files, converted to epub, mobi, pdf and html with pandoc. I
know there are better workflows, especially in regards to embedding code (my
approach was simple copy and paste), but if there's one recommendation I can
make it's this one: check out pandoc. It's an amazing tool.

~~~
orloffm
Thanks.

------
Zikes
Very timely! I'm in the early stages of writing a custom interpreter in Go, so
this is perfect for me.

------
skybrian
Is the complete version on Github somewhere? (I just want to skim through the
source code.)

~~~
misternugget
I haven't uploaded the code to Github myself, but as of v1.3 of the book the
code is MIT licensed. That means there are a lot of implementations floating
around, either copied straight from the book or adapter to another language.
Here's the repo of a reader who followed the book closely in Go:
[https://github.com/RyanBrushett/interpreter](https://github.com/RyanBrushett/interpreter)

------
baconomatic
I wish I would've waited to buy that, print would've been much better!

~~~
autoreleasepool
Really? I find ebooks side by side a terminal a much more efficient way of
going through a text like this

~~~
baconomatic
I agree, but sometimes it's nice to unplug and just look through books.

------
betodelrio
Interesting. I would like to purchase the PDF version.

Is there a PROMO CODE available?

------
codewritinfool
I bought the PDF. Looks great so far!

------
wwweston
Well, if there's anything that might lure me back to Go, it's an escape hatch
into another language. :b

------
morboX
I like that Monkey language.

------
swah
I've been recently writing a web app in Go - I like the libraries and the
tools - but when pausing to write a test script in python I immediately
realized Go can't be the right tool for regular CRUD websites. Python is a
joy, I love dynamic patterns when writing code. Dicts and strings! It flows...

So, yay escape hatches!

At some point it was going to be a pattern (Java + jruby/ola bini's
language)...

