Architecture
Inside the AHA! compiler — lexer, Pratt parser, type system, LLVM codegen, and JIT
Compiler Architecture
AHA! is a complete compiler in a single binary:
Source Code → Lexer → Parser (Pratt) → AST → Code Generator → LLVM IR → JIT ExecutionStages
| Stage | Module | What it does |
|---|---|---|
| Lexer | src/lexer.rs | Tokenizes source: identifiers, integers, strings (with escapes), operators, line & block comments |
| Parser | src/parser.rs | Pratt parser producing the AST — expression-oriented, with correct operator precedence |
| Type System | src/types.rs | AhaType + TypedValue; compile-time checks for binary and prefix operators |
| Codegen | src/codegen.rs | LLVM IR generation via inkwell: functions (with return-type inference), loops, strings, arrays, C-runtime linkage |
| Driver | src/main.rs | CLI entry point: lex → parse → codegen → print IR → JIT execute |
Design Notes
- LLVM 14 through the
inkwellcrate — no hand-rolled backend - Strings are
{i8*, i64}structs;malloc,memcpy, andstrcmpcome from the linked C runtime - TypedValue: every codegen expression returns both an LLVM value and its
AhaType, so type errors surface before the IR is built - Booleans compose with math: all comparison and logical operators produce
i640/1, sois_even(n) * 100needs no casts - JIT execution runs the generated IR inside the compiler process — no separate interpreter
How a Program Executes
- The lexer turns source text into a token stream.
- The Pratt parser builds an expression-oriented AST with correct precedence.
- The type system checks every binary and prefix operation.
- Codegen emits LLVM IR (functions, loops, strings, builtin calls) via inkwell.
- The driver links and JIT-executes the IR, printing the program result.