Architecture

Architecture

Sun is a compiled language with an LLVM 20 backend.

Compilation Pipeline

Source → Lexer → Parser → AST → Semantic Analyzer → Borrow Checker → Codegen Visitor → LLVM IR → JIT/AOT

Component Overview

ComponentFilesDescription
Driverdriver.h/cppOrchestrates the full pipeline: parse → analyze → codegen → execute
Lexerlexer.h/cpp, nfa.hNFA-based regex tokenizer supporting keywords, operators, literals
Parserparser.h/cppRecursive descent parser producing AST nodes
ASTast.h/cppExpression/statement nodes with type annotations
Semantic Analyzersemantic_analyzer.h/cppType inference, scope analysis, closure capture detection
Borrow Checkerborrow_checker/*.h/cppRust-style ownership and borrowing analysis (WIP)
Codegen Visitorcodegen_visitor.h, src/codegen/*.cppVisitor pattern for AST-to-LLVM-IR translation
LLVM Codegencodegen.h/cpp, llvm_type_resolver.h/cppLLVM context, module, and type management
Matrix Codegenmatrix_codegen.h/cppSpecialized code generation for matrix operations
Compilercompiler.hAOT compilation: emit object files, link executables
SunJITsun_jit.hLLVM ORC-based JIT compiler for immediate execution

Codegen Structure

The code generation is split across multiple files by expression type:

FileResponsibility
codegen_visitor.cppMain visitor dispatch, module initialization
functions_lambdas.cppFunction/lambda codegen with closure support
classes.cppClass definitions, constructors, methods
call_expressions.cppFunction calls, method calls
variable_creation.cppVariable declarations (var, let)
variable_references.cppVariable loads, assignments
if_expressions.cppConditionals, ternary expressions
loops.cppfor and while loops
block_expressions.cppBlock scoping
return_statements.cppReturn value handling
error_handling.cpptry/catch/throw codegen
matrices.cppMatrix operations and indexing

Type System

The type system (types.h) includes:

  • Primitives: i8, i16, i32, i64, f32, f64, bool, void
  • Matrices: N-dimensional matrices with compile-time or runtime dimensions
  • Functions/Lambdas: First-class function types with closure support
  • Pointers: ptr<T> owning pointers, raw_ptr<T> for C interop, static_ptr<T> for static data
  • Classes: User-defined types with methods and fields
  • Interfaces: Contracts for polymorphism
  • Generics: Parameterized types with monomorphization
  • Error Unions: T, IError for explicit error handling

Execution Modes

JIT Execution

sun program.sun

Uses LLVM ORC JIT for immediate execution without producing binaries.

AOT Compilation

sun --compile -o program program.sun

Emits object file via LLVM, links with system C compiler to produce native executable.

Flow Diagram