In the book, the author chooses the Visitor Pattern for their implementation, which makes a lot of sense for an object-oriented language like Java. I know it’s possible to use this pattern in Go as well, but because we don’t have classes, I find it a little unergonomic to use it. I decided to just create recursive functions that do type switches.
It’s been interesting to do this challenge in Go because I have to kind of “translate” the deeply object-oriented approach to the book’s implementation into more idiomatic Go code.
I implement the interpreter to learn Go along the way. Even though Go is not an OO-language in the strict sense I found the availability of interfaces, structs and methods sufficient to use the visitor pattern for my interpreter. But - as I said - I am a Go newbie. so this might not be an idiomatic approach in Go.
Each one of these functions end up recursively calling Evaluate(expr) until the expression passed is a Literal, in which case its value is returned. Simple and clear recursion.