Skip to content
Zone-InfinityPublic

Repository files navigation

YeyLoxrs

     )              (
  ( /(              )\ )
  )\())   (   (    (()/(          )  (
 ((_)\   ))\  )\ )  /(_))  (   ( /(  )(   (
__ ((_) /((_)(()/( (_))    )\  )\())(()\  )\
\ \ / /(_))   )(_))| |    ((_)((_)\  ((_)((_)
 \ V / / -_) | || || |__ / _ \\ \ / | '_|(_-<
  |_|  \___|  \_, ||____|\___//_\_\ |_|  /__/
              |__/

Rust CI License: MIT

A tree-walking interpreter for Lox, written in Rust, following Part II (A Tree-Walk Interpreter, chapters 4–13) of Robert Nystrom's Crafting Interpreters.

The goal is to be faithful to the book's semantics (the jlox reference implementation) while writing idiomatic Rust. Where the implementation currently differs from the book, it is tracked as an issue with the book-conformance label.

Status: the full language runs (classes, inheritance, closures), but several parts still need work, notably the resolver, error reporting (no line numbers yet), and the CLI. See the open issues, and contributions are welcome. Start with CONTRIBUTING.md.


Quick start

Requires Rust 1.91+ (see rust-version in Cargo.toml).

git clone https://github.com/Zone-Infinity/YeyLoxrs.git
cd YeyLoxrs
cargo build

cargo run -- run samples/print-modify.lox

Usage

yeyloxrs <command> <file>
Command What it does Book chapter
tokenize Print the token stream 4
parse Parse a single expression and print its AST 5–6
evaluate Parse and evaluate a single expression 7
run Run a full program (scan → parse → resolve → interpret) 8–13

There is no REPL yet (the book's runPrompt). See the issues.

Exit codes

Code Meaning
0 Success
65 Compile error (scan / parse / resolve)
70 Runtime error
127 Bad usage, unknown command, unreadable file

65 and 70 match the book (sysexits.h). The book uses 64 for usage errors; see the issues.

Example

var a = 46;
fun printAndModify() {
  print a;
  var a = 33;
  print a;
}

print a;
a = 65;
printAndModify();
$ cargo run -q -- run samples/print-modify.lox
46
65
33

Language support

Feature Chapter Status
Scanning (incl. block comments, an extension) 4 ✅
Expressions & evaluation 5–7 ✅
Statements, variables, blocks 8 ✅
if, while, for (desugared), and/or 9 ✅
Functions, closures, native clock() 10 ✅
Resolving & binding 11 ⚠️ works, but deviates from the book (see #2 and #3)
Classes, this, initializers 12 ✅ (some static errors missing)
Inheritance, super 13 ✅
Error messages with [line N] 4–13 ❌ not yet

Project structure

src/
  main.rs          CLI entrypoint: tokenize / parse / evaluate / run, exit codes
  tokenizer.rs     Scanner (ch. 4)
  ast.rs           Expr / Stmt AST + runtime callables (functions, classes, instances)
  parser.rs        Recursive-descent parser (ch. 6, 8–10, 12–13)
  resolver.rs      Static variable resolution: expr_id → scope distance (ch. 11)
  interpreter.rs   Tree-walk interpreter + environments (ch. 7–13)
  natives.rs       Native functions (clock)
  value.rs         LiteralValue, LoxCallable trait, truthiness helpers
  error.rs         ParseError / RuntimeError and exit-code mapping
  debug_guard.rs   debug! macro (toggle with DEBUG_MODE)

samples/           Lox programs used by the test harness
scripts/
  test-samples.sh  Runs every sample and checks exit code + expected output
.github/
  workflows/rust.yml   CI: build, cargo test, sample harness

Testing

cargo test                     # Rust unit tests
./scripts/test-samples.sh      # end-to-end sample programs

CI runs both on every push and pull request to main. To add a regression test, drop a .lox file into samples/ and register it (with its expected output / exit code) in scripts/test-samples.sh.

Contributing

Read CONTRIBUTING.md. AI coding agents should also read AGENTS.md. The short version: match the book's behaviour, and if your change deliberately doesn't, say so in the PR.

Beyond the book

Ideas for after the book's behaviour is matched. Language extensions go behind a clear opt-in and are tracked as enhancement issues:

  • REPL (the book's runPrompt, maybe with rustyline)
  • Line/column error reporting with source snippets
  • Lists / maps, more natives (string and math)
  • Anonymous functions (one of the book's chapter 10 challenges)
  • Part III: a bytecode compiler and VM (clox in Rust)
  • LSP, WebAssembly build

Origins

This project started as a solution to the CodeCrafters "Build Your Own Interpreter" challenge and is now developed independently.

License

MIT © iSoham