https://github.com/matthisk/golox
A toy implementation of the Lox language in Golang
https://github.com/matthisk/golox
Last synced: 11 months ago
JSON representation
A toy implementation of the Lox language in Golang
- Host: GitHub
- URL: https://github.com/matthisk/golox
- Owner: matthisk
- Created: 2025-08-20T12:57:27.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-08-20T12:58:34.000Z (11 months ago)
- Last Synced: 2025-08-20T14:48:24.683Z (11 months ago)
- Language: Go
- Size: 6.4 MB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Lox Interpreter
A toy implementation of the Lox programming language from Robert Nystrom's excellent book ["Crafting Interpreters"](https://craftinginterpreters.com/), written in Go.
## About
This project implements a tree-walking interpreter for the Lox language, a dynamically-typed scripting language with features like:
- Variables and basic data types (numbers, strings, booleans, nil)
- Arithmetic and logical expressions
- Control flow (if/else, while, for loops)
- Functions with closures
- Classes with inheritance
- Object-oriented programming with methods and fields
## Building
Build the interpreter binary:
```bash
go build -o lox .
```
## Usage
Run a Lox program from a file:
```bash
./lox
```
Example:
```bash
./lox engine/testdata/fibonacci.lox
```
## Testing
Run the complete test suite:
```bash
go test ./...
```
The test suite includes:
- Unit tests for the lexer, parser, and interpreter components
- End-to-end integration tests with sample Lox programs
- Test programs in `engine/testdata/` covering various language features
## Architecture
The interpreter is organized into three main components:
- **Lexer** (`lexer/`) - Tokenizes Lox source code into a stream of tokens
- **Parser** (`parser/`) - Builds an Abstract Syntax Tree (AST) from tokens using recursive descent parsing
- **Interpreter** (`interpreter/`) - Evaluates the AST using the visitor pattern
## Language Features
This implementation supports the full Lox language specification including:
- Expression evaluation with proper operator precedence
- Variable declaration and assignment
- Functions with parameters and return values
- Closures and lexical scoping
- Classes with methods and constructors
- Inheritance with `super` keyword support
- Built-in functions like `print` and `clock`
## Example Programs
See the `engine/testdata/` directory for example Lox programs demonstrating various language features.