Skip to content

ehsanmok/create-your-own-lang-with-rust

Repository files navigation

CreateLang.rs Logo

Create Your Own Programming Language with Rust

Learn compiler fundamentals by building four languages from scratch

OverviewLanguagesQuick StartRequirementsBook

CI GitHub Pages Read the Book License


Edition 1.0 Complete
Four languages covering interpretation, compilation, type systems, and object-oriented programming.
LLVM code generation, JIT compilation, type inference, and memory management.


Overview

This book arose from frustration with the lack of modern, clear, and concise teaching materials for beginners who want to learn how to create their own programming language.

"If you don't know how compilers work, then you don't know how computers work" 1

"If you can't explain something in simple terms, you don't understand it" 2

Languages

The book teaches programming language implementation through four progressively complex languages:

Language Type System Execution Features Rust Version
Calculator None Interpreter/VM/JIT Arithmetic expressions stable / nightly
Firstlang Dynamic Interpreter Variables, functions, recursion stable 1.70+
Secondlang Static LLVM JIT Type inference, compilation nightly + LLVM
Thirdlang Static LLVM JIT Classes, methods, OOP nightly + LLVM
create-your-own-lang-with-rust/
├── calculator/     # Simple arithmetic (interpreter, VM, JIT backends)
├── firstlang/      # Interpreted language with recursion
├── secondlang/     # Compiled language with type inference
├── thirdlang/      # Object-oriented with classes and methods
└── book/           # mdbook source for createlang.rs

Each language has its own README with detailed instructions.

Quick Start

Firstlang (stable Rust)

cd firstlang
cargo run -- examples/fibonacci.fl    # Run a program
cargo run                              # Start REPL

Secondlang (nightly + LLVM)

cd secondlang
rustup run nightly cargo run -- examples/fibonacci.sl
rustup run nightly cargo run -- --ir examples/fibonacci.sl   # View LLVM IR

Thirdlang (nightly + LLVM)

cd thirdlang
rustup run nightly cargo run --bin thirdlang -- examples/point.tl
rustup run nightly cargo run --bin thirdlang -- examples/counter.tl

Calculator

cd calculator
cargo run --bin main examples/simple.calc                              # Interpreter
cargo run --bin main --features vm examples/simple.calc                # VM
rustup run nightly cargo run --bin main --features jit examples/simple.calc  # JIT

Requirements

Stable Rust (Calculator interpreter/VM, Firstlang)

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Nightly Rust + LLVM (Calculator JIT, Secondlang, Thirdlang)

# Install nightly
rustup toolchain install nightly

# Install LLVM
# macOS:
brew install llvm

# Debian/Ubuntu:
# See https://apt.llvm.org/

Check your LLVM version and update Cargo.toml accordingly:

LLVM Version inkwell feature
20.x llvm20-1
19.x llvm19-1
18.x llvm18-1

Running Tests

# All projects
cargo test -p calculator -p firstlang
rustup run nightly cargo test -p secondlang -p thirdlang

Building the Book

cd book
mdbook serve    # Local preview at http://localhost:3000
mdbook build    # Build static site

Support
If you found this project useful, please consider donating to:

Child FoundationBlack Lives MatterFood Bank of Canada

Contributors 9

Languages