Experiments while reading the book Crafting Interpreters, building a toy compiler/interpreter in Rust
My compiler learnings/notes: Everything-About-Compilers
We’ll use a simple arithmetic language as an example.
Define the Language: Before you start writing a compiler, you need to define the language that you want to compile. For our example, let’s say our language can evaluate arithmetic expressions like 2 + 3 * 4.
#[derive(Debug)]
enum Token {
Number(i32),
Plus,
Minus,
Multiply,
Divide,
LParen,
RParen,
}
enum Expr {
Number(i32),
Add(Box<Expr>, Box<Expr>),
Subtract(Box<Expr>, Box<Expr>),
Multiply(Box<Expr>, Box<Expr>),
Divide(Box<Expr>, Box<Expr>),
}
enum Bytecode {
Push(i32),
Add,
Subtract,
Multiply,
Divide,
}
There are also many great resources available if you want to learn more. The book “Crafting Interpreters” by Bob Nystrom is a great place to start, and it’s available for free online. There’s also the “Rust Programming Language” book by Steve Klabnik and Carol Nichols, which is a great resource for learning Rust.