Tobechukwu Ikenwe
Back to projects
Lox — Crafting Interpreters Implementation

Lox — Crafting Interpreters Implementation

A complete implementation of the Lox programming language from Robert Nystrom's Crafting Interpreters book — featuring a tree-walking interpreter (jlox) in Java and a bytecode VM (clox) in C.

JavaCInterpretersCompilersLanguage Design

What This Project Does

Lox is a dynamically-typed, high-level scripting language designed by Robert Nystrom in the book Crafting Interpreters. This project is a full, working implementation of the language, built from scratch following the book. It includes two separate interpreters:

  • jlox (Java) — A tree-walking interpreter that parses source code into an Abstract Syntax Tree (AST) and evaluates it recursively. Great for understanding language semantics clearly.
  • clox (C) — A bytecode virtual machine that compiles Lox source into bytecode instructions and executes them on a stack-based VM. Faster and closer to how real production interpreters work.

How the Tree-Walking Interpreter Works (jlox)

The jlox interpreter processes Lox source code through a classic pipeline:

  • Scanner (Lexer): Converts raw source code like "var x = 5;" into a sequence of tokens — [VAR, IDENTIFIER(x), EQUAL, NUMBER(5), SEMICOLON].
  • Parser: Uses recursive descent parsing to build an Abstract Syntax Tree (AST) from the token stream.
  • Interpreter: Walks the AST recursively, evaluating expressions and executing statements in order.
  • Environment: Tracks variable bindings using nested scopes, supporting block-level scoping with { } braces.

Project Structure

The repository is organized into the two interpreter implementations and a shared examples folder:

  • jlox/ — Tree-walking interpreter in Java: Main.java (entry point & REPL), Scanner.java (lexer), Parser.java (recursive descent), Expr.java & Stmt.java (AST nodes), Interpreter.java (evaluation), Environment.java (scopes), Token.java & TokenType.java.
  • clox/ — Bytecode VM in C (compiled with GCC or Clang).
  • examples/ — Sample Lox programs: arithmetic, variables, blocks & scope, control flow, and a comprehensive Fibonacci demo.

Lox Language Features

Lox supports a rich set of features for a scripting language:

  • Literals: numbers, strings, true, false, nil.
  • Arithmetic: +, -, *, /, unary -.
  • Comparison & equality: ==, !=, <, <=, >, >=.
  • Variables: var x = 5; x = 10;
  • Print: print expr;
  • Blocks & scoping: { var x = 1; print x; }
  • Control flow: if (cond) stmt else stmt, while (cond) stmt.

Fibonacci Example (05_comprehensive.lox)

The comprehensive example demonstrates variables, while loops, arithmetic, and print all working together. The program declares three variables (a, b, count), then runs a while loop that prints the first 10 numbers of the Fibonacci sequence. Each iteration computes the next value by summing the previous two, storing the result in a temp variable and shifting the values forward. After the loop completes, it prints "Done!" — confirming control flow past the loop.

Lox — Crafting Interpreters Implementation - Fibonacci Example (05_comprehensive.lox) 1

Running the Fibonacci Example

The terminal output shows the interpreter executing 05_comprehensive.lox and printing the classic Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 — followed by "Done!". Each value is printed on its own line as the while loop iterates, demonstrating that variable assignment, arithmetic, comparison, and print all work correctly end-to-end.

Lox — Crafting Interpreters Implementation - Running the Fibonacci Example 1
View project code and details