A TypeScript implementation of the Lox programming language tree-walk interpreter from Crafting Interpreters by Robert Nystrom.
-
Lexical Analysis (Scanner)
- Tokenization of Lox source code
- Support for all Lox token types
- Line number tracking for error reporting
-
Parsing
- Recursive descent parser
- Expression parsing with proper precedence
- Support for:
- Binary expressions (
+,-,*,/,>,>=,<,<=,==,!=) - Unary expressions (
-,!) - Grouping with parentheses
- Literals (numbers, strings, booleans, nil)
- Binary expressions (
-
Runtime Interpreter
- Expression evaluation
- Arithmetic operations
- String concatenation
- Comparison operators
- Truthiness semantics (nil and 0 are falsy)
- Runtime error handling
-
REPL & File Execution
- Interactive REPL mode
- Script file execution
- Variable declarations and assignments
- Statements
- Control flow (if/else, while, for)
- Functions and closures
- Classes and inheritance
- Logical operators (
and,or) - Ternary and comma operators
- Node.js (v22.20.0 or higher)
- pnpm (v10.15.1 or compatible)
pnpm installpnpm buildpnpm startThis starts an interactive prompt where you can enter Lox expressions:
> 2 + 3 * 4
14
> "Hello, " + "World!"
Hello, World!
> !(5 - 4 > 3 * 2 == !nil)
true
>
pnpm start path/to/script.loxtslox/
├── src/
│ ├── expression.ts # AST node definitions
│ ├── interpreter.ts # Tree-walk interpreter & expression evaluator
│ ├── lox.ts # Main entry point
│ ├── parser.ts # Recursive descent parser
│ ├── printer.ts # AST pretty-printer
│ ├── runtimeerror.ts # Runtime error handling
│ ├── scanner.ts # Lexical analyzer/tokenizer
│ ├── token-type.ts # Token type enumeration
│ └── token.ts # Token class definition
├── package.json
├── tsconfig.json
└── README.md