A Java implementation of the Lox programming language interpreter, following the design from "Crafting Interpreters" by Robert Nystrom.
This project implements a tree-walk interpreter for the Lox programming language. Lox is a dynamically-typed scripting language with features like:
- Variables and basic data types (numbers, strings, booleans)
- Arithmetic and logical operations
- Control flow (if/else statements, loops)
- Functions and closures
- Object-oriented programming with classes
learncompiler/
├── src/main/java/com/craftingintepreters/lox/
│ ├── Lox.java # Main interpreter entry point
│ ├── Scanner.java # Lexical analyzer (tokenizer)
│ ├── Token.java # Token representation
│ └── TokenType.java # Token type definitions
├── pom.xml # Maven build configuration
└── README.md # This file
- Java 17 or later
- Maven 3.6+ (for building)
To build the project using Maven:
mvn clean compileTo create a JAR file:
mvn clean packageRun the interpreter without any arguments to start the interactive prompt:
java -cp target/classes com.craftingintepreters.lox.LoxThis will start a Read-Eval-Print Loop where you can type Lox expressions:
> print "Hello, World!";
Hello, World!
> var a = 5;
> print a + 3;
8
Run a Lox script file:
java -cp target/classes com.craftingintepreters.lox.Lox script.loxThis implementation currently includes:
- ✅ Lexical Analysis: Complete tokenizer that recognizes all Lox tokens
- ✅ Token Types: Support for literals, operators, keywords, and identifiers
- ✅ Basic REPL: Interactive command-line interface
- ✅ File Execution: Ability to run Lox scripts from files
- ✅ Error Reporting: Basic error handling with line numbers
The scanner currently recognizes these language constructs:
Operators: +, -, *, /, =, ==, !=, <, <=, >, >=, !
Keywords: and, class, else, false, for, fun, if, nil, or, print, return, super, this, true, var, while
Literals: Numbers, strings, identifiers
Delimiters: (, ), {, }, ,, ., ;
mvn testThis project serves as a learning exercise to understand:
- Lexical analysis and tokenization
- Recursive descent parsing
- Abstract syntax trees (AST)
- Tree-walk interpretation
- Programming language design principles
Future development will include:
- Parser implementation (syntax analysis)
- Abstract Syntax Tree (AST) generation
- Expression evaluation
- Statement execution
- Variable binding and environments
- Function calls and closures
- Object-oriented features
- Crafting Interpreters by Robert Nystrom
- Lox Language Specification
This project is for educational purposes. Please refer to the original "Crafting Interpreters" book for comprehensive explanations of the concepts implemented here.