Materials in this book are distributed under the terms of Creative Commons BY-NC-SA 4.0

license

This book assumes some basic knowledge of Rust language. Please take a look at the official Rust book.

The accompanying codes and materials for this book are available in GitHub. To follow along, make sure you have

  • Rust toolchain installed

  • Cloned the repository

    git clone https://github.com/ehsanmok/create-your-own-lang-with-rust
    cd create-your-own-lang-with-rust
    
  • LLVM installed to run and test locally cargo test --tests

    • Easiest option is LLVM v14.0 (Debian/Ubuntu or macOS)
    • Otherwise, in Cargo.toml you'd need to change the inkwell = { ..., branch = "master", features = ["your-llvm-version"] } with LLVM version on your system (output of llvm-config --version)

Motivations and Goals

This book arises from my frustration of not finding modern, clear and concise teaching materials that are readily accessible to beginners like me who wants to learn a bit on how to create their own programming language.

The following are my guidelines

"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

Pedagogically, one of the most effective methods of teaching is co-creating interactively. Introducing the core aspects around the simplest example (here, our calculator language) helps a lot to build knowledge and confidence. For that, we will use mature technologies instead of spending tone of time on partially reinventing-the-wheel and bore the reader.

Here is the outline of the contents

  • Crash Course on Computing which we briefly set up the definitions and foundations
  • We create our first programming language Calc that supports simple integer addition and subtraction. The simplicity allows us to touch a lot of important topics. We will use PEG to define our grammar, pest to generate our CalcParser and explain what AST is and interpreting the AST means. Next, we will introduce JIT compilation and use inkwell to JIT compile our Calc language from its AST. To show an alternative compilation approach, we will create a Virtual Machine and a Runtime environment and discuss its features. Finally, we will write a simple REPL for our Calc language and test out different execution paths.
  • TODO: We will create Firstlang, a statically typed language, by gradually working our way up from our Calc
  • TODO: Object system and minimal object oriented programming support
  • TENTATIVE: Create a mini standard library
  • TODO: Resources

Donation

If you have found this book useful, please consider donating to any of the organizations below