Architecture
Sun is a compiled language with an LLVM 20 backend.
This page gives a high-level overview of the compiler. Two stages have dedicated deep-dives:
- Semantic Analysis — type inference, scope resolution, generics, and capture detection
- Code Generation — LLVM IR emission, lowering conventions, and source layout
Compilation Pipeline
Source → Lexer → Parser → AST → Manifest Processing → Merged AST → Semantic Analyzer → Borrow Checker → Codegen Visitor → LLVM IR → JIT/AOTPipeline Steps
- Lexer — Tokenize source file
- Parser — Build AST from tokens
- Manifest Processing — Extract manifest, parse additional
suns, loadmoons - AST Merging — Combine all parsed ASTs into a single merged AST
- Semantic Analysis — Type inference, scope resolution, capture detection
- Borrow Checking — Ownership and lifetime analysis
- Code Generation — Emit LLVM IR
- JIT/AOT — Execute immediately or compile to native binary
Component Overview
| Component | Files | Description |
|---|---|---|
| Driver | driver/driver.h/cpp | Orchestrates the full pipeline: parse → analyze → codegen → execute |
| Lexer | parsing/lexer.h, parsing/nfa.h | NFA-based regex tokenizer supporting keywords, operators, literals (header-only) |
| Parser | parsing/parser.h/cpp | Recursive descent parser producing AST nodes |
| AST | ast.h, ast/*.h, src/ast/*.cpp | Expression/statement nodes with type annotations |
| Semantic Analyzer | semantic_analysis/semantic_analyzer.h, src/semantic_analysis/*.cpp | Type inference, scope analysis, closure capture detection |
| Borrow Checker | borrow_checker/*.h/cpp | Rust-style ownership and borrowing analysis (WIP) |
| Codegen Visitor | codegen/codegen_visitor.h, src/codegen/*.cpp | Visitor pattern for AST-to-LLVM-IR translation |
| LLVM Codegen | codegen/codegen.h, codegen/llvm_type_resolver.h/cpp | LLVM context, module, and type management |
| Compiler | driver/compiler.h | AOT compilation: emit object files, link executables |
| SunJIT | driver/sun_jit.h | LLVM ORC-based JIT compiler for immediate execution |
| Moon | moon_bundling/moon.h/cpp, moon_bundling/library_cache.h/cpp | Precompiled library format (.moon) and caching |
Headers live under include/ and implementations under src/ in the same
directory layout: support/, driver/, parsing/, ast/,
semantic_analysis/, borrow_checker/, codegen/ (with abi/ and
intrinsics/), serialization/, moon_bundling/, lsp/, debug/.
Semantic Analysis
The semantic analyzer turns a merely parsed program into an understood
one: it makes two passes over the merged AST — a shallow declaration
collection sweep, then a deep walk that stamps every expression with a
resolved sun::Type. Type inference, expected-type propagation,
compatibility checking, closure capture detection, and generic
monomorphization all happen here.
See Semantic Analysis for the full deep-dive.
Code Generation
The codegen visitor walks the analyzed AST and emits LLVM IR, reading the
types and annotations the semantic analyzer recorded. Its source is split
across src/codegen/ by expression type.
See Code Generation for lowering conventions and the per-file breakdown.
Type System
The type system (types.h) includes:
- Primitives:
i8,i16,i32,i64,u8,u16,u32,u64,f32,f64,bool,void - Arrays: Fixed-size arrays with N-dimensional indexing
- Functions/Lambdas: First-class function types with closure support
- Pointers:
ptr<T>owning pointers (RAII),raw_ptr<T>for C interop,static_ptr<T>for static data - References:
ref Tfor borrowing without ownership transfer - Classes: User-defined value types with methods and fields
- Interfaces: Contracts for polymorphism (including builtin
IError; the stdlib suppliesIIterator,IIterable) - Enums: Named integer variants with pattern matching support
- Generics: Parameterized types with monomorphization
- Errors:
T, IErrorfunctions throw classes implementingIErroras native exceptions - Threads:
Thread<T>for OS thread handles viaspawn
Execution Modes
JIT Execution
sun program.sunUses LLVM ORC JIT for immediate execution without producing binaries.
AOT Compilation
sun -c -o program program.sunEmits object file via LLVM, links with system C compiler to produce native executable.
Debug Mode
sun --debug program.sunGenerates debugging artifacts in program_debug/:
ast.dot— GraphViz AST visualizationir.ll— Full LLVM IR outputscope_tree.html— Semantic scope visualization