Compiler architecture
Om's compiler is written in Rust and split into crates so each stage can be read, tested and replaced on its own.
┌───────────┐ ┌────────────┐ ┌──────────────────┐ ┌──────────┐
file.om ─▶ │ om-lexer │─▶ │ om-parser │─▶ │ om-codegen │─▶ │ omc │─▶ executable
│ tokens │ │ AST │ │ check + lower to │ │ link with│
│ + spans │ │ │ │ Cranelift IR │ │ cc │
└───────────┘ └────────────┘ └──────────────────┘ └──────────┘om-lexer
A hand-written, single-pass lexer over bytes. Each Token carries a Span
(byte offsets) so every later error can point at source text. Keywords are
recognised after reading an identifier.
om-parser
A recursive-descent parser. Statements and blocks use plain functions; binary
operators use precedence climbing (Parser::binary), which handles
associativity and precedence in one loop. The grammar is at the top of
crates/om-parser/src/lib.rs, and the AST types are in ast.rs.
Semicolons are optional: a block's final expression without ; becomes its
tail, which is the block's value.
om-codegen
Phase 0 checks types while lowering to Cranelift IR, in two passes:
- Declare every function's signature, so calls can refer to functions defined later in the file.
- Define each body: walk the AST, compute each expression's
Ty, and emit Cranelift instructions. Variables become CraneliftVariables, which the frontend turns into SSA form for us.
Control flow maps to Cranelift blocks: if creates then, else and merge
blocks, and the merge block takes a block parameter for the if's value.
&& and || short-circuit the same way. After return, lowering continues in
an unreachable block so code after it still type-checks.
Newer features are mostly desugaring: the parser rewrites x |> f(a) into
f(x, a), and parses each {...} inside a string with a nested parser whose
spans are shifted to point at the original file. Named and default arguments
are matched to parameter slots in Lower::call. defer keeps a stack of
deferred expressions per scope and emits them at every exit point (end of
block and each return), newest first, the same way Zig does.
Om functions are exported as om_<name>; Om's main becomes the C main
symbol so the object links like a C program. Output goes through a tiny C runtime
(RUNTIME_C, two functions: write a string, write an integer), because
Cranelift cannot call variadic functions like printf portably.
omc
The driver. It renders diagnostics (file:line:col with a caret under the
span), writes the object file and runtime to a temp directory, and runs the
system C compiler to link.
Planned changes
- Phase 1: move type checking into an
om-checkcrate that produces a typed AST, so code generation never reports user errors. - Phase 2: an ownership pass over the typed AST (moves and scoped borrows).
- Phase 3: an
om-ircrate (SSA, monomorphized) between checking and Cranelift, which also makes an LLVM backend possible in phase 5.
Adding a feature, step by step
Example: adding a for loop.
- Add the keyword to
TokenKindand the match inLexer::ident, with a lexer test. - Add a
Stmt::Forvariant inast.rsand parse it inParser::stmt, with a parser test. - Lower it in
Lower::stmtinom-codegen(it is awhilewith an increment). - Add an example program and its
.expectedoutput inexamples/; the end-to-end test picks it up automatically.