Overview

This documentation describes the architecture and design decisions of the Ori compiler.

Design Principle: Lean Core, Rich Libraries

The compiler implements only constructs that require special syntax or static analysis. Everything else belongs in the standard library.

LocationWhatWhy
Compilerrun, try, match, recurse, parallel, spawn, timeout, cache, withRequire special syntax, bindings, self(), concurrency primitives, or capability checking
Stdlibmap, filter, fold, find, retry, validateStandard method calls on collections; no special compiler support needed

This keeps the compiler small (~30K lines), focused, and maintainable. The stdlib can evolve without compiler changes. When considering new features, ask: “Does this need special syntax or static analysis?” If no, it’s a library function.

Overview

The Ori compiler is a Rust-based incremental compiler built on the Salsa framework. It is organized as a multi-crate workspace with clear separation of concerns:

  • ori_ir - Core IR types with no dependencies (AST, arena, interning, derives)
  • ori_diagnostic - Error reporting system
  • ori_lexer - Tokenization
  • ori_types - Type system definitions
  • ori_parse - Recursive descent parser
  • ori_typeck - Type checking and inference
  • ori_patterns - Pattern system, Value types, EvalError (single source of truth)
  • ori_eval - Core evaluator components (Environment, operators)
  • oric - CLI orchestrator, Salsa queries, evaluator, reporting

The compiler features:

  • Incremental compilation via Salsa’s automatic caching and dependency tracking
  • Flat AST representation using arena allocation for memory efficiency
  • String interning for O(1) identifier comparison
  • Extensible pattern system with registry-based pattern definitions
  • Comprehensive diagnostics with code fixes and multiple output formats

Statistics

ComponentLines of CodePurpose
IR~4,500AST types, arena, visitor, interning
Evaluator~5,500Tree-walking interpreter
Type System~4,300Type checking, inference, TypeContext
Parser~3,200Recursive descent parsing
Patterns~3,000Pattern system and builtins
Diagnostics~2,800Error reporting, DiagnosticQueue, fixes
Lexer~700DFA-based tokenization
Tests~1,100Test discovery, execution, error matching
Total~30,000

Compilation Pipeline

flowchart TB
    A["SourceFile (Salsa input)"] -->|"tokens() query"| B["TokenList"]
    B -->|"parsed() query"| C["ParseResult { Module, ExprArena, errors }"]
    C -->|"typed() query"| D["TypedModule { expr_types, errors }"]
    D -->|"evaluated() query"| E["ModuleEvalResult { Value, EvalOutput }"]
    D -.->|"codegen() query (pending)"| F["LLVM IR → Native Binary"]
    style F stroke-dasharray: 5 5

Each step is a Salsa query with automatic caching. If the input doesn’t change, the cached output is returned immediately.

Documentation Sections

Architecture

Intermediate Representation

Lexer

Parser

Type System

Pattern System

Evaluator

Diagnostics

Testing

Appendices

Source Paths

The compiler is organized as a multi-crate workspace:

CratePathPurpose
ori_ircompiler/ori_ir/src/Core IR types (tokens, spans, AST, arena, interning, derives)
ori_diagnosticcompiler/ori_diagnostic/src/DiagnosticQueue, error reporting, suggestions, emitters
ori_lexercompiler/ori_lexer/src/Tokenization via logos
ori_typescompiler/ori_types/src/Type, TypeError, TypeContext, InferenceContext
ori_parsecompiler/ori_parse/src/Recursive descent parser
ori_typeckcompiler/ori_typeck/src/Type checking, inference, BuiltinMethodRegistry
ori_patternscompiler/ori_patterns/src/Pattern definitions, Value types, EvalError, EvalContext
ori_evalcompiler/ori_eval/src/Environment, OperatorRegistry (core eval components)
ori-macroscompiler/ori-macros/src/Diagnostic derive macros
oriccompiler/oric/src/CLI, Salsa queries, eval orchestration, reporting

Note: oric modules (ir, parser, diagnostic, types) re-export from source crates for DRY.

oric Internal Paths

ComponentPath
Library rootcompiler/oric/src/lib.rs
Salsa databasecompiler/oric/src/db.rs
Query systemcompiler/oric/src/query/
Evaluatorcompiler/oric/src/eval/
Problem typescompiler/oric/src/problem/
Diagnostic renderingcompiler/oric/src/reporting/
Testscompiler/oric/src/test/

Architecture Notes

  • Patterns: Pattern definitions and Value types are in ori_patterns. oric re-exports from this crate.
  • Environment: The Environment type for variable scoping is in ori_eval. oric uses this directly.
  • Re-exports: oric modules (ir, types, diagnostic) re-export from their source crates for DRY.