Rust implementation of CodeCrafters's "Build your own Interpreter" Challenge.
This challenge follows the book Crafting Interpreters by Robert Nystrom. It involves building an interpreter for Lox, a simple scripting language.
Note
As of now (2024 Dec), the challenge only supports up to "Chapter 10 - Functions". Hence, this solution have not implemented Chapters 11~13 (Resolving and Binding, Classes, Inheritance).
To run the Hello World program:
cargo run -- run examples/hello-world.loxTo learn more about the CLI:
cargo run -- --helpThis implementation deviates from the book in a few ways:
- The parser uses Pratt Parsing instead of Recursive Decent to parse expressions.
- Abstract Syntax Tree (AST):
- Some AST nodes have slightly different names
- Each AST node stores a
Spanthat records the start and end positions of the source code.
- Evaluation:
- For-loop desugaring is done at evaluation stage instead of parsing stage, so there is an AST node for for-loops.
- Function call returns are handled using Rust's
ControlFlowinstead of "try-catch" approach.
- Error handling:
-
Errors are represented as enums instead of strings. A drawback is that the error messages are generic and does not match the book.
-
miette is used for pretty diagnostic printing. Only the
tokenizecommand returns error messages in the book's format in order to pass CodeCrafters' tests.
-
These implementations greatly influenced the implementation:
- Implementing a Lox interpreter in Rust - Jon Gjengset
- Darksecond/lox
- jeschkies/lox-rs
- sagark4/rlox_ast_walk
- rami3l/dolores
Also huge thanks to Oxidation Compiler for their resources:
