Built from scratch · Java · Crafting Interpreters · Summer 2026

LoxLang

A complete tree-walking interpreter for Lox, a small dynamically-typed language, built following Crafting Interpreters by Robert Nystrom. Scanner, recursive-descent parser, and evaluator, all handwritten. Supports variables, functions, closures, classes with inheritance, and a custom string-interpolation extension I added myself. Ported to JavaScript below so you can write and run real Lox code in this page.

Java Recursive Descent Tree-Walking Closures OOP

Write Lox code on the left, hit Run, see the output on the right. This is the same scanner → parser → interpreter pipeline as the Java version, just ported to run in your browser instead of the JVM. Pick an example to see closures, classes and inheritance, or the custom f-string extension in action.

example.lox
Output

Two implementations exist in the real project. The tree-walking interpreter above is fully working and is what's running in this page.

🔎
Scanner
Converts raw source text into a flat list of tokens, one pass, character by character. Also where the custom f"..." string interpolation is detected and expanded into a chain of concatenations.
🌳
Parser
Recursive descent: one method per grammar rule, from expression() down to primary(). Precedence is baked into the call structure, lower-precedence rules call higher-precedence ones.
🔗
Environment chain
Each block and function call gets its own Environment pointing at its enclosing scope. Closures work because a function stores a reference to the environment active when it was defined, not when it's called.
⚙️
Bytecode VM (in progress)
Part 3 of the book replaces the tree-walker with a stack-based bytecode compiler and a mark-sweep garbage collector, the same architecture as CPython. The headers are scaffolded in lox-c/; the compiler and VM aren't finished yet.
Scanner.java
Lexer: source text → tokens
Parser.java
Recursive descent parser: tokens → AST
Interpreter.java
Tree-walking evaluator, one visit method per AST node
Environment.java
Scope chain for variable lookup and closures
LoxFunction.java
First-class functions, closures, and method binding
LoxClass.java / LoxInstance.java
Runtime classes, inheritance, and instances

Selected pieces of the actual Java source.

What I learned

The Visitor pattern finally made sense.

I'd seen it described in textbooks but never understood why you'd want it. The interpreter uses it to add interpret, print, and resolve operations to the AST without modifying the AST node classes. Once I built it, I got it.

Closures are surprisingly subtle.

The Java version stores the closing Environment directly in the function object. It's conceptually clean but keeps every captured variable alive as long as the closure exists. The planned C VM uses "upvalues" instead, a smarter scheme where captured variables live on the stack until the closure outlives its scope, then get moved to the heap.

Writing a hash table from scratch would teach me more than years of using one.

That's the part of the C VM I haven't built yet, load factors, open addressing, tombstone deletion, and why string interning makes equality checks O(1) rather than O(n). Reason enough to finish lox-c.