Skip to content
DanielleB-RPublic

About

The first interpreter in Crafting Interpreters, reinterpreted in Go from Java

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

44 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

golox

The tree-walking interpreter from Crafting Interpreters (jlox), reinterpreted in Go.

Build and run

Requires Go 1.26 or newer.

go build -o golox .      # build the binary
./golox script.lox       # run a script
./golox                  # start the REPL

You can also skip the build step with go run . script.lox.

Exit codes: 64 for bad usage, 65 for scan, parse or resolver errors, and 1 if the script file can't be read.

The language

Lox is a small dynamically typed language.

class Animal {
  init(name) { this.name = name; }
  speak() { print this.name + " makes a sound"; }
}

class Dog < Animal {
  speak() {
    super.speak();
    print "Woof!";
  }
}

fun fib(n) {
  if (n < 2) return n;
  return fib(n - 1) + fib(n - 2);
}

var d = Dog("Rex");
d.speak();
for (var i = 0; i < 10; i = i + 1) print fib(i);

Supported features:

  • Types: numbers, strings, booleans, nil
  • Variables (var), block scoping, if/else, while, for, break, continue, and/or
  • Functions, closures and return
  • Classes with init, this, single inheritance and super
  • Built-in clock(), which returns the time in seconds
  • print statements, // line comments and nestable /* ... */ block comments

Not implemented: static methods, getters and anonymous functions (book challenges).

Development

go test ./...
go vet ./...

Layout:

Path Purpose
main.go Command-line entry point
interpreter/scanner.go Source text to tokens
interpreter/parser.go Tokens to AST (interpreter/ast)
interpreter/resolver.go Static variable resolution pass
interpreter/interpreter.go Evaluates the AST
interpreter/environment.go Variable scopes

Note that .lox files are git-ignored.

License

See LICENSE.

About

The first interpreter in Crafting Interpreters, reinterpreted in Go from Java

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages