Fisher Darling | Blog | Resume

Lox in Rust Part 0 - Introduction and Setup

Hello everyone! Welcome to my first blog series, Lox in Rust where we will create an interpreter for the Lox programming language.

Inspiration

There are two main inspirations for this blog series. Firstly, when I first started learning Rust, I found it hard to learn about (or even know about) all of the different crates and idioms of the language. The Book is incredible, but I was missing the hands-on experience. Once I snagged an internship where the codebase was in Rust, everything changed! I had some concrete usage of different crates in what could be considered almost production ready Rust!

Secondly, as a student I am constantly being told I need to have projects on my resume that are complex and show off my skills. A lot of the time the advice is to "throw one of your class projects up on GitHub." I find this advice just fine (I have many class projects on GitHub), but much of the time these projects are about specific code or algorithms, not code that follows best practices, is unit tested, benchmarked, or is close to being production-ready. On the flip-side, there are many tutorials out there for deploying your first web-app or blockchain. These teach valuable lessons such as: how containers and docker work or how to deploy to the Google Cloud Platform.

But I want something fun and relatively simple, whose complexity can be derived from its own architecture and extensions to a small core, not necessarily from APIs or deployment. I want this series to create just that.

So why are we building an interpreter?

I find Programming Languages fascinating, I loved my PL class and had a blast implementing a lisp-like language. Interpreters are exactly what I want project-wise, they can be as simple or complex as you want them to be, they're easily extendable, eventually lead to writing a compiler, and aren't dependant on some API or platform: the focus is on the code you write.

Enter Crafting Interpreters by Bob Nystrom. This is an incredible (e)book which I can't give enough praise. His writing style is engaging, examples are clean, and it's easy to learn something new. It really is a blast.

Here's an excerpt from the landing page:

This book contains everything you need to implement a full-featured, efficient scripting language. You’ll learn both high-level concepts around parsing and semantics and gritty details like bytecode representation and garbage collection. Your brain will light up with new ideas, and your hands will get dirty and calloused. It’s gonna be a blast.

We are going to be following the book in this blog series until we have a fully functioning interpreter for the Lox programming language, written purely in Rust. We'll be covering unit/integration testing, code coverage, benchmarking, fuzzing, and recommended idioms. I hope that the tools and patterns in this series help you write better Rust code, and that you'll learn a lot!

Structure

You won't need to read Crafting Interpreters to write the code in this series, though it would be a great idea to follow along. You'll learn all of the terminology and overall structure of an interpreter, as well as having a better idea of the difference between clean Java code and clean Rust code. I'm going to try to keep the code beginner friendly, but reading The Book will be really beneficial. Something every Rust developer should read!

Sections and chapters of Crafting Interpreters will be hyperlinked when appropriate. We won't be following the exact order of the book, so keep an eye out for those links.

Before we finally get going, you should read the welcome section to get an idea of what the lox language is all about.

Now let's write some Rust!

Setting Up the Project

If you don't already have Rust installed, install it at rustup.rs. Make sure you're using nightly! rustup default nightly

Let's open up our favorite terminal, alacritty, and create a new rust project1:

cargo new --lib lox
cd lox    

Create a new file src/main.rs, this will be our application's main binary, and add the following code:

use lox;

fn main() {

}

Perfect! Now we're ready to start writing some actual Rust! Head on over to LINK ME Part 2 where we'll setup a CLI and proper error handling.