A Java implementation of the Lox programming language from Robert Nystrom's book "Crafting Interpreters".
This project is a production-ready implementation of the Lox programming language, featuring:
- Variables and basic data types (numbers, strings, booleans, nil)
- Control flow (if statements, while loops, for loops)
- First-class functions with closures
- Classes with single inheritance
- Lexical scoping
- String escape sequences (
\n,\t,\\,\") - Recursion depth limits (prevents stack overflow)
- Division by zero error handling
- Java 11 or higher
- Maven 3.6+
# Clone the repository
git clone https://github.com/yourusername/lox-interpreter.git
cd lox-interpreter
# Build the project
mvn package
# Run a Lox program
java -jar target/lox-interpreter-1.0.0.jar examples/hello.lox
# Start the REPL
java -jar target/lox-interpreter-1.0.0.jar# On Unix/macOS
./lox examples/fibonacci.lox
# On Windows
lox.bat examples\fibonacci.lox# Run a Lox script file
java -jar target/lox-interpreter-1.0.0.jar script.lox
# Or with Maven
mvn exec:java -Dexec.mainClass="com.lox.Lox" -Dexec.args="script.lox" -qjava -jar target/lox-interpreter-1.0.0.jarLox Interpreter v1.0
Type 'exit' to quit.
> print "Hello, World!";
Hello, World!
> var x = 42;
> print x * 2;
84
> exit
var name = "Alice";
var age = 30;
var isActive = true;
var nothing = nil;// If statement
if (age >= 18) {
print "Adult";
} else {
print "Minor";
}
// While loop
var i = 0;
while (i < 5) {
print i;
i = i + 1;
}
// For loop
for (var i = 0; i < 5; i = i + 1) {
print i;
}fun greet(name) {
print "Hello, " + name + "!";
}
fun fibonacci(n) {
if (n <= 1) return n;
return fibonacci(n - 2) + fibonacci(n - 1);
}
greet("World");
print fibonacci(10);fun makeCounter() {
var count = 0;
fun increment() {
count = count + 1;
return count;
}
return increment;
}
var counter = makeCounter();
print counter(); // 1
print counter(); // 2class Animal {
init(name) {
this.name = name;
}
speak() {
print this.name + " makes a sound.";
}
}
class Dog < Animal {
init(name, breed) {
super.init(name);
this.breed = breed;
}
speak() {
print this.name + " barks!";
}
}
var dog = Dog("Buddy", "Golden Retriever");
dog.speak(); // "Buddy barks!"print clock(); // Returns current time in secondslox-interpreter/
├── src/
│ ├── main/java/com/lox/
│ │ ├── Lox.java # Entry point
│ │ ├── ast/ # AST nodes (Expr, Stmt)
│ │ ├── debug/ # AstPrinter
│ │ ├── error/ # Error handling
│ │ ├── interpreter/ # Interpreter, Environment
│ │ ├── parser/ # Parser
│ │ ├── resolver/ # Variable resolver
│ │ ├── runtime/ # Functions, classes, instances
│ │ ├── scanner/ # Lexer
│ │ └── token/ # Token types
│ └── test/java/com/lox/ # Unit tests
├── examples/ # Example Lox programs
├── pom.xml # Maven configuration
└── README.md
# Compile
mvn compile
# Run tests
mvn test
# Package JAR
mvn package
# Clean build
mvn clean installmvn testThe test suite includes:
- Scanner tests (tokenization, escape sequences, error handling)
- Interpreter tests (expressions, statements, classes, closures)
See the examples/ directory:
hello.lox- Hello Worldfibonacci.lox- Fibonacci sequenceclosures.lox- Closure demonstrationclasses.lox- Class inheritance example
Source Code → Scanner → Tokens → Parser → AST → Resolver → Interpreter → Output
- Visitor Pattern: Clean AST traversal for interpretation
- Environment Chaining: Proper lexical scoping
- Static Resolution: Variable binding at compile time
- Recursion Limits: Prevents stack overflow (max 1000 calls)
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Run tests (
mvn test) - Commit (
git commit -m 'Add amazing feature') - Push (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Robert Nystrom for "Crafting Interpreters"
- The Lox language design