A small Rust implementation of a tree-walk interpreter for the Lox programming language (from Crafting Interpreters). This is a personal playground project meant for learning and experimentation.
It is not optimized and not production-ready. A serious, high-performance interpreter/runtime typically compiles to bytecode and runs on a dedicated VM (often with custom bytecodes, optimizations, and a sandboxed execution environment). This project intentionally stays simpler: it scans, parses, and directly evaluates the AST.
crates/scanner: turns source text into tokenscrates/parser: builds an AST and interprets itcrates/types: shared token/typescrates/cli: a small command-line interface (interpreter-cli)test.lox: a sample program that exercises most supported features
- literals: numbers, strings,
true,false,nil - grouping:
( ... ) - unary:
!expr,-expr - binary arithmetic:
+,-,*,/ - comparisons:
<,<=,>,>= - equality:
==,!= - logical:
and,or(short-circuit) - variables: identifier references
- assignment:
name = expr - calls:
callee(arg1, arg2, ...)(max 255 arguments)
- expression statements:
expr; - print:
print expr; - variable declarations:
var name;andvar name = expr; - blocks:
{ ... }(lexical scoping / shadowing) - if / else:
if (condition) statement;
if (condition) statement; else statement;- while:
while (condition) statement;- for (desugared to
whileby the parser):
for (initializer; condition; increment) statement;- functions:
fun name(a, b) {
return a + b;
}- return:
return;orreturn expr;
clock()returns a number (seconds since Unix epoch).
This interpreter currently focuses on the core “functions + control flow” part of Lox. Notably absent:
- classes / instances / inheritance (
class,this,super) - methods / property access (
obj.field) - break / continue
The CLI currently exposes two subcommands:
tokenize: scan a.loxfile and print tokensparse: scan + parse + interpret a.loxfile
cargo run -p cli -- tokenize test.lox
cargo run -p cli -- parse test.loxcargo build -p cli
./target/debug/interpreter-cli tokenize test.lox
./target/debug/interpreter-cli parse test.loxtest.lox is intended as a feature smoke-test (variables, expressions, printing, functions, for/while, closures, recursion, etc.).
This is a straightforward AST interpreter and makes no attempt to be fast. If you want speed and stronger isolation, the typical next steps are:
- compile the AST to bytecode
- execute on a purpose-built VM (custom opcodes, stack/register model)
- add sandboxing controls (time limits, memory limits, I/O restrictions)