Skip to content

AnsonH/codecrafters-lox-rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

104 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

codecrafters-lox-rust

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).

Running

To run the Hello World program:

cargo run -- run examples/hello-world.lox

To learn more about the CLI:

cargo run -- --help

What's done differently?

This 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 Span that records the start and end positions of the source code.
  • Evaluation:
  • 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 tokenize command returns error messages in the book's format in order to pass CodeCrafters' tests.

      syntax-error-demo

Inspirations

These implementations greatly influenced the implementation:

Also huge thanks to Oxidation Compiler for their resources:

About

Rust solution to CodeCrafters "Build your own Interpreter" challenge

Topics

Resources

Stars

Watchers

Forks

Contributors