Developers / Compiler design

This section of the docs is written to help you understand compiler concepts in general, as well as Moth specific concepts.

This is to help break down what Moth does differently, why it does it and how it all fits into the bigger design picture.

From Moth source to a webpage

We'll follow a small Moth program from files on disk to a generated HTML page.

If you write Rust, you already know functions, enums, structs and ownership as a user. You may not know terms like intermediate representation, link plan or borrow validation yet.

Each term shows up when the example needs it and will be explained step by step.

These docs are focused on teaching rather than documenting the compiler's actual implemented behaviour.

The small example project we'll follow

The file structure

compiler_walkthrough/
├── config.moth
└── src/
    ├── @page.moth
    └── greeting.moth

The code

greeting.moth

greeting_prefix #= "Hello"

greet |name String| -> String:
    return [: [greeting_prefix], [name]!]
;

@page.moth

@greeting greet

visitor = "Priya"

[$html:
    <p>[greet(visitor)]</p>
]

The final fragment the series aims at:

<p>Hello, Priya!</p>

Priya never changes. The file names never change. The fragment never changes. Side examples may wander for a paragraph, then come back to this greeting.

What each stage hands the next

One checkpoint strip runs through every article:

project tree
-> command and config
-> canonical source graph
-> located tokens
-> prepared shells
-> bound interfaces and ordered declarations
-> typed AST and semantic identities
-> folded constants, generated requests and TIR handoff
-> structured diagnostics
-> validated HIR
-> borrow and optional-transfer side-table facts
-> lifetime-region and escape-validation facts
-> immutable module artefacts and fingerprints
-> entry assembly and reachable function union
-> project/link lifetime topology validation
-> deterministic target assignment
-> JavaScript and Wasm outputs
-> HTML, runtime assets and output manifest

Each article opens with the incoming representation and closes with the outgoing one. If a page jumps ahead, you have left the chronological route.


Each box links to the walkthrough page that expands that stage. Side branches show important internal work without lengthening the main pipeline.

Question: which step still looks like source, and which first looks like something a host can run?

Four owners you will meet again

Build system. Selects the command, discovers source, builds graphs, schedules modules, plans entries and writes files to disk.

Compiler frontend and analysis. Prepares syntax, binds interfaces, checks semantics, lowers HIR and validates access.

Artefact builder. Assembles selected compiler facts into an output plan for one kind of project, such as an HTML page.

Target backends. Lower validated functions and runtime needs into JavaScript, Wasm and glue.

A fact has one owner. Later stages consume that fact. They do not rebuild it from source text.

How to read the series

Read in order. The path groups ideas by time of appearance, not by internal crate name.

  1. 01. What a compiler does
  2. 02. Why Moth makes different choices
  3. 03. Starting a build
  4. 04. Project graphs and modules
  5. 05. Tokenization
  6. 06. Declarations, dependencies and ordering
  7. 07. AST semantics and types
  8. 08. Compile-time semantics
  9. 09. Templates and TIR
  10. 10. Diagnostics
  11. 11. HIR
  12. 12. Memory topology and lowering
  13. 13. Borrow validation and drops
  14. 14. Module artefacts and reuse
  15. 15. Linking, entries and reachability
  16. 16. Target planning and validation
  17. 17. Backend lowering
  18. 18. HTML assembly and output

This landing page acts as article 00.

Status language on every page

Later articles label four kinds of claim:

The series links the progress matrix and roadmap. It does not paste either document into the articles. Accepted architecture stays the main teaching path.

Where authority lives

Educational pages explain those authorities. They do not outrank them.

Next: what a compiler does between Priya's name and a paragraph on a page.