# tayadev/luma architecture

> Architecture diagram of the GitHub repository tayadev/luma, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-11-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tayadev/luma
- Repository: https://github.com/tayadev/luma

## Overview

##### Project Type and Purpose
- Luma is a reference implementation of a programming language. It provides:
- A CLI front-end for running, REPL, type-checking, and compiling scripts.
- A multi-stage compiler pipeline (lexing, parsing, AST, type checking, bytecode IR).
- A virtual machine (VM) runtime with native/FFI modules.
- An LSP server for editor integration.
- A comprehensive test suite (parser tests, type-check tests, runtime tests, etc.).

##### High-Level Architectural Components
a. CLI Layer
- Commands: run, repl, check, compile, upgrade
- Parses CLI flags, invokes compiler front-end or runtime.

b. Front-End Pipeline
1. Lexer & Parser (src/parser/…)
2. AST Construction (src/ast/…)
3. Diagnostics & Error Recovery (src/diagnostics/…)
4. Type Checker (src/typecheck/…)

c. Middle-End (Bytecode Compiler)
- IR definitions (src/bytecode/ir.rs)
- Compile passes: compile.rs, emit_expr, emit_stmt, helpers

d. Back-End (Virtual Machine)
- Interpreter core (src/vm/interpreter.rs, execute.rs)
- Stack, frames, value representations (src/vm/stack.rs, value.rs)
- Native modules & FFI (src/vm/native/…)
- Module loader (src/vm/modules.rs)

e. LSP Server (src/lsp/mod.rs)
- Language server protocol handlers
- Ties into parser, typechecker, diagnostics

f. Support & Infrastructure
- Build scripts (build.rs, Cargo.toml)
- Tests & Benchmarks (benches/, tests/)
- Documentation site (docs/)

##### Interactions and Data Flow
- The user invokes CLI →
- “run” / “check” / “compile” → (1) front-end pipeline →
– Parser outputs AST + span metadata →
– Diagnostics module reports errors/warnings →
– Type Checker annotates AST with types →
– On “compile”: Bytecode Compiler emits .lumac →
– On “run”: VM Interpreter executes directly on AST or bytecode
- “repl” spins up an interactive loop using the same pipeline internally
- LSP Server accepts editor requests → uses Parser + Type Checker + Diagnostics → returns diagnostics, completions

##### Architectural Patterns & Principles
- Pipeline pattern: distinct phases (lexing, parsing, type checking, codegen, runtime)
- Single process, modular Rust crates and modules
- Separation of concerns: AST vs. IR vs. VM data structures
- Dependency inversion: CLI and LSP layers depend on back-end abstractions via public APIs

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Actor
    User["User / Editor / CI"]:::external

    %% CLI Layer
    subgraph "CLI Layer" 
        CLI["CLI Front-End"]:::tooling
    end

    %% Front-End Pipeline
    subgraph "Front-End Pipeline" 
        LP["Lexer & Parser"]:::compileTime
        AST["AST Construction"]:::compileTime
        DIAG["Diagnostics & Error Recovery"]:::compileTime
        TC["Type Checker"]:::compileTime
    end

    %% Middle-End
    subgraph "Bytecode Compiler (Middle-End)" 
        IR["IR Definitions"]:::compileTime
        CP["Compiler Passes"]:::compileTime
    end

    %% Bytecode Artifact
    BYTECODE[".lumac Bytecode"]:::artifact

    %% Back-End Runtime
    subgraph "VM Runtime (Back-End)" 
        INT["Interpreter Core"]:::runTime
        SF["Stack & Frames"]:::runTime
        VAL["Value Representations"]:::runTime
        NATIVE["Native Modules & FFI"]:::runTime
        MODL["Module Loader"]:::runTime
    end

    %% Execution Results
    RESULT["Execution Results"]:::artifact

    %% LSP Server
    subgraph "LSP Server" 
        LSP["LSP Server"]:::tooling
    end

    %% Support & Infrastructure
    subgraph "Support & Infrastructure" 
        BUILD["Build Scripts"]:::tooling
        TESTS["Tests & Benchmarks"]:::tooling
        DOCS["Documentation Site"]:::tooling
    end

    %% Data Flow
    User -->|invokes CLI| CLI
    CLI -->|parses flags| LP
    LP -->|produces AST| AST
    AST -->|annotates spans| DIAG
    DIAG -->|reports issues| TC
    TC -->|type-annotated AST| CP
    CP -->|emits IR| IR
    IR -->|passes through| CP
    CP -->|generates| BYTECODE
    CP -->|on run| INT
    INT -->|executes| SF
    SF -->|manages| VAL
    INT -->|loads modules| MODL
    MODL -->|resolves| NATIVE
    INT -->|produces| RESULT

    User -->|in editor| LSP
    LSP -->|requests parse| LP
    LSP -->|requests type| TC
    LSP -->|requests diagnostics| DIAG
    DIAG -->|returns report| LSP
    LSP -->|sends results| User

    %% Tooling Consumers
    TESTS -.-> LP
    TESTS -.-> TC
    TESTS -.-> INT
    DOCS -.-> LP
    DOCS -.-> TC
    DOCS -.-> INT

    %% Click Events
    click CLI "https://github.com/tayadev/luma/blob/main/src/main.rs"
    click LP "https://github.com/tayadev/luma/tree/main/src/parser/"
    click AST "https://github.com/tayadev/luma/blob/main/src/ast.rs"
    click DIAG "https://github.com/tayadev/luma/blob/main/src/diagnostics/mod.rs"
    click TC "https://github.com/tayadev/luma/blob/main/src/typecheck/mod.rs"
    click IR "https://github.com/tayadev/luma/blob/main/src/bytecode/ir.rs"
    click CP "https://github.com/tayadev/luma/blob/main/src/bytecode/mod.rs"
    click INT "https://github.com/tayadev/luma/blob/main/src/vm/interpreter.rs"
    click SF "https://github.com/tayadev/luma/blob/main/src/vm/stack.rs"
    click VAL "https://github.com/tayadev/luma/blob/main/src/vm/value.rs"
    click NATIVE "https://github.com/tayadev/luma/blob/main/src/vm/native/mod.rs"
    click MODL "https://github.com/tayadev/luma/blob/main/src/vm/modules.rs"
    click LSP "https://github.com/tayadev/luma/blob/main/src/lsp/mod.rs"
    click BUILD "https://github.com/tayadev/luma/blob/main/build.rs"
    click TESTS "https://github.com/tayadev/luma/tree/main/tests/"
    click DOCS "https://github.com/tayadev/luma/tree/main/docs/"

    %% Styles
    classDef compileTime fill:#cce5ff,stroke:#3399ff,stroke-width:2px
    classDef runTime fill:#d4edda,stroke:#28a745,stroke-width:2px
    classDef tooling fill:#fff3cd,stroke:#ffc107,stroke-width:2px
    classDef external fill:#f0f0f0,stroke:#999999,stroke-width:2px,stroke-dasharray: 4 2
    classDef artifact fill:#e2e3e5,stroke:#6c757d,stroke-width:2px,stroke-dasharray: 2 2
```
