This project implements the code from the book crafting interpreters in go.
The first version literally follows the book and doesn't attempt to use go specific features.
The lox grammar is defined here.
The lox/lang package includes the AST nodes (ast.go),
the tokens (tokens.go), scanner (scanner.go)
and parser (parser.go). The scanner takes a script
as string and returns a slice of tokens ([]*Tokens).
The parser takes a slice of tokens and returns a slice of AST
nodes ([]Stmt).
The lox/interp package includes the interpreter itself (interp.go) and the resolver (resolver.go).
The resolver performs static analysis. It could have been part
of the lang package since it checks for compile errors
but it is bundled with the interpreter in the original text.
It also has one direct call to the interpreter (Interp.Resolve())
which makes it dependent on the interp package.
There are unit tests for the low level lang package
and the interpreter itself. The interpreter tests are
written as go testable example since it makes them very
readable. Each test consists of a lox script and the
expected results as // Ouput: comments.
The main deviations from the original java implementation described in the crafting interpreters are:
- The AST statements (
lang.Stmt) and expressions (lang.Expr) do not use the visitor pattern as in the java code. - The java code uses
Objectfor the dynamic values in expressions. The go code usesinterface{} - The AST Nodes implement
PrettyPrint()which allows to pretty print any AST tree without special package.
You can install the lox interpreter using go get github.com/rmonnet/glox.
See the examples in the examples directory. They are
mostly taken from the original text.
You can install the source code on your machine by typing:
git clone github.com/rmonnet/glox`
cd glox
go install github.com/rmonnet/glox
This is an implementation of the lox interpreter presented by Bob Nystrom here.
The go code in this project is licensed under the MIT license.