A complete Java implementation of the Lox programming language, built by following Crafting Interpreters and implemented as part of the CodeCrafters Interpreter Challenge.
This project implements lexing, parsing, static resolution, and execution for the full Lox language, including functions, closures, classes, inheritance, and native functions.
-
Data types: numbers, strings, booleans,
nil -
Expressions: arithmetic, comparison, equality, logical operators
-
Variables: lexical scoping, assignment, shadowing
-
Control flow:
if,else,while,for -
Functions: first-class functions, closures, recursion
-
Classes and object-oriented programming:
- Constructors (
init) - Instance methods and properties
- Single inheritance using
super thisbinding
- Constructors (
-
Native functions:
clock()
- Tree-walk interpreter
- Recursive-descent parser
- Static scope resolution (two-pass execution)
- Visitor pattern for AST traversal
- Runtime error handling with defined exit codes
- Language: Java 11+
- Build tool: Maven
- Architecture: AST with Visitor pattern
- Platform: CodeCrafters
codecrafters-interpreter-java/
├── pom.xml
├── codecrafters.yml
├── your_program.sh
└── src/main/java/nox/
├── Main.java
├── parser/
│ ├── Token.java
│ ├── Parser.java
│ ├── Expr.java
│ ├── Stmt.java
│ ├── LiteralParser.java
│ ├── StatementParser.java
│ └── AstPrinter.java
└── interpreter/
├── Interpreter.java
├── Resolver.java
├── Environment.java
├── LoxCallable.java
├── LoxFunction.java
├── LoxClass.java
├── LoxInstance.java
├── ClockFunction.java
├── RuntimeError.java
└── ResolverError.java
- Java 11 or newer
- Maven
mvn clean package./your_program.sh tokenize file.lox./your_program.sh parse file.lox./your_program.sh evaluate file.lox./your_program.sh run file.loxclass Counter {
init() {
this.value = 0;
}
inc() {
this.value = this.value + 1;
return this.value;
}
}
var c = Counter();
print c.inc();
print c.inc();
1
2
- Lexing: source code to tokens
- Parsing: tokens to AST
- Resolution: static scope binding
- Interpretation: AST execution
- Ensures correct lexical scoping
- Enables constant-time variable lookup
- Detects invalid use of
return,this, andsuperbefore execution
| Error Type | Exit Code |
|---|---|
| Syntax and resolution errors | 65 |
| Runtime errors | 70 |
Handled cases include:
- Undefined variables
- Invalid inheritance
- Incorrect function arity
- Type mismatches
- Invalid property access
program → declaration* EOF ;
declaration → classDecl | funDecl | varDecl | statement ;
classDecl → "class" IDENTIFIER ( "<" IDENTIFIER )? "{" function* "}" ;
funDecl → "fun" function ;
function → IDENTIFIER "(" parameters? ")" block ;
statement → exprStmt | ifStmt | whileStmt | forStmt | printStmt | returnStmt | block ;
expression → assignment ;
assignment → IDENTIFIER "=" assignment | logic_or ;- Crafting Interpreters — Robert Nystrom
- CodeCrafters Interpreter Challenge https://app.codecrafters.io/courses/interpreter/overview
Educational project created for learning language design and interpreter implementation.