Skip to content

behai-nguyen/rlox

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rlox: A Rust Implementation of “Crafting Interpreters”

A work-in-progress Rust implementation of Robert Nystrom's Lox language, as presented in Crafting Interpreters.

For each completed stage, I document my progress in a post, which is listed in the Related post(s) section below.

📘 RLox Language Guide

A working guide to the Lox language features currently implemented in this project.
View RLoxGuide.md

Process Flow: Scanner, Parser, Resolver, and Interpreter

When the Scanner encounters an error, no token list is produced, so the Parser cannot and should not run. Similarly, if the Parser fails, no statement list is available, and the Resolver should not run. If the Resolver fails, the Interpreter should not run either. While the statement list from the Parser remains available, attempting to evaluate unresolved statements with the Interpreter would be futile.

This interaction between the four components is illustrated in the flowchart below:

scanner-parser-resolver-interpret.png

To Run

Clone the repository to your local machine:

$ git clone https://github.com/behai-nguyen/rlox.git

Change to the rlox directory.

💥 The application's behavior will evolve as development progresses. As a result, the discussions below will be updated accordingly.

$ cargo run --release ./tests/data/constructor/call_init_explicitly.lox

If there are no errors, you will see the results printed out.

Related post(s)

  1. rlox: A Rust Implementation of “Crafting Interpreters” – Scanner

The code version for the above post has been tagged with v0.1.0. It can be cloned with:

git clone -b v0.1.0 https://github.com/behai-nguyen/rlox.git

I am attempting a Rust implementation of Robert Nystrom's Lox language discussed in Crafting Interpreters. This post describes my Rust code equivalence for the Scanning chapter.

  1. Visitor Pattern with Rust

Crafting Interpreters makes use of the visitor pattern, which I’m not yet familiar with. To better understand it, I’ve attempted to implement the C# and Java examples from the Wikipedia Visitor pattern article in Rust. Short, isolated examples like these help us grasp the underlying theory more effectively.

We won’t be discussing the theory of the visitor pattern in this post.

  1. rlox: A Rust Implementation of “Crafting Interpreters” – Abstract Syntax Tree (AST) – Representing Code

The code version for the above post has been tagged with v0.1.1. It can be cloned with:

git clone -b v0.1.1 https://github.com/behai-nguyen/rlox.git

The primary focus of this post is Chapter 5: Representing Code, in which the author introduces an independent tool to generate ASTs for both expressions and statements, followed by a printer for displaying the AST. This post briefly discusses my Rust implementation of both tools.

  1. rlox: A Rust Implementation of “Crafting Interpreters” – Parsing and Evaluating Expressions

The code version for the above post has been tagged with v0.2.0. It can be cloned with:

git clone -b v0.2.0 https://github.com/behai-nguyen/rlox.git

In this post, I briefly describe the implementation of the code in Chapter 6: Parsing Expressions, and Chapter 7: Evaluating Expressions.

  1. rlox: A Rust Implementation of “Crafting Interpreters” – Global Variables, Assignment, and Scope

The code version for the above post has been tagged with v0.3.0. It can be cloned with:

git clone -b v0.3.0 https://github.com/behai-nguyen/rlox.git

I have completed Chapter 8: Statements and State. The following additional statements and expressions have been implemented: Stmt::Expression, Stmt::Print, Stmt::Var, Expr::Variable, Expr::Assign and Stmt::Block. We can now declare global variables, define scoped variables, and assign values to variables. This post discusses some implementation issues that deserve attention.

  1. rlox: A Rust Implementation of “Crafting Interpreters” – Control Flow

The code version for the above post has been tagged with v0.4.0. It can be cloned with:

git clone -b v0.4.0 https://github.com/behai-nguyen/rlox.git

This is Chapter 8: Control Flow. The following additional statements and expressions have been implemented: Stmt::If, Expr::Logical, and Stmt::While. Lox now supports if, else, and, or, while, and for. Despite this long list of new features, the implementation remains fairly straightforward.

  1. rlox: A Rust Implementation of “Crafting Interpreters” – Functions

The code version for the above post has been tagged with v0.5.0. It can be cloned with:

git clone -b v0.5.0 https://github.com/behai-nguyen/rlox.git

This post covers Chapter 10 of Crafting Interpreters: Functions. The following new syntax elements have been implemented: Expr::Call, Stmt::Function, and Stmt::Return. Lox now supports fun, return, and closures. This post discusses several implementation details that deserve attention.

  1. rlox: A Rust Implementation of “Crafting Interpreters” – Resolving and Binding

The code version for the above post has been tagged with v0.5.1. It can be cloned with:

git clone -b v0.5.1 https://github.com/behai-nguyen/rlox.git

This post covers Chapter 11 of Crafting Interpreters: Resolving and Binding. No new syntax elements are introduced in this chapter. Instead, Chapter 11 serves as a kind of patch to Chapter 10: it ensures that variables are resolved within their correct closures. The code for this chapter is relatively straightforward, but I made a mistake that introduced a subtle bug—one that took me a long time to diagnose and finally fix.

  1. rlox: A Rust Implementation of “Crafting Interpreters” – Classes

The code version for the above post has been tagged with v0.6.0. It can be cloned with:

git clone -b v0.6.0 https://github.com/behai-nguyen/rlox.git

This post covers Chapter 12 of Crafting Interpreters: Classes. The following new syntax elements have been implemented: Stmt::Class, Expr::Get, Expr::Set, and Expr::This. Lox now supports class, this, and init. While implementing this chapter, I encountered two stack overflow bugs and several cases where author-provided test scripts produced incorrect results. This post discusses those issues in detail.

  1. rlox: A Rust Implementation of “Crafting Interpreters” – Inheritance

The code version for the above post has been tagged with v0.6.1. It can be cloned with:

git clone -b v0.6.1 https://github.com/behai-nguyen/rlox.git

This post covers Chapter 13 of Crafting Interpreters: Inheritance. Class inheritance syntax <Class < SuperClass — has been implemented. The final remaining syntax element, Expr::Super, representing the super keyword, has also been added. In this post, we briefly discuss the new code, followed by bug fixes and test updates.

  1. Code Revision v0.6.2

Clone this version with:

git clone -b v0.6.2 https://github.com/behai-nguyen/rlox.git
  • General refactoring: test structure and error display improvements.
  • Bug fix: input script file existence is now checked before execution.

License

MIT license and the Creative Commons.

About

A Rust implementation of Robert Nystrom's Lox language Interpreter, as presented in “Crafting Interpreters”: https://craftinginterpreters.com/contents.html

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages