An implementation of the Lox programming language Interpreter written in Go. This interpreter is based on the book "Crafting Interpreters" by Robert Nystrom and tested via Codecrafters.
Lox is a dynamically-typed scripting language that supports object-oriented programming with classes and inheritance. This implementation follows the tree-walk interpreter pattern.
Here's what's currently implemented in this interpreter:
Feature | Status |
---|---|
Basic Arithmetic | ✅ |
Variables | ✅ |
Control Flow (if/else) | ✅ |
Loops (while, for) | ✅ |
Functions | ✅ |
Closures | ✅ |
Classes | 🚧 |
Inheritance | 🚧 |
Standard Library | 🚧 |
Error Handling | 🚧 |
- Go 1.22 or higher
- Git
# Clone the repository
git clone https://github.com/yourusername/go-lox.git
# Navigate to project directory
cd go-lox
There are four ways to use the interpreter:
- Print tokens of a Lox script:
./go-lox tokenize <filename>.lox
- Print AST of a Lox script:
./go-lox parse <filename>.lox
- Evaluate basic expression:
./go-lox eval <filename>.lox
- Run a Lox script:
./go-lox run <filename>.lox
// This is a simple Lox program
fun fib(n) {
if (n <= 1) return n;
return fib(n - 2) + fib(n - 1);
}
print fib(10); // Outputs: 55
├── parser/ # Abstract Syntax Tree parser
├── lexer/ # Lexical analysis
├── interpreter/ # Interpreter implementation
├── expression/ # Expression definitions e.g., <, ==, +, >, -
├── stmt/ # Statement definitions e.g., var, fun, for, while
├── token/ # Token definition
└── main.go # Entry point
I will be glad to receive any of your questions/suggestions/contributions to this project! Feel free to open a PR or contact me via:
For more information about the Lox language, visit Crafting Interpreters.
Note: This implementation is for educational purposes and may not be suitable for production use.