# somespi/lumen architecture

> Architecture diagram of the GitHub repository somespi/lumen, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/somespi/lumen
- Repository: https://github.com/somespi/lumen

## Overview

1. **What this repository is**
   - Lumen is not a typical web app; it is a small language/runtime implemented in PHP.
   - The repo contains the language engine, a command-line entry point, standard/native libraries, and example programs.

2. **Top-level runtime entry point**
   - `lumen.php` is the main executable/bootstrap file.
   - It likely wires together CLI handling and the interpreter pipeline so a `.lumen` source file can be executed from the command line.

3. **CLI layer**
   - `src/cli.php` is the user-facing interface for running Lumen programs.
   - This boundary is where command-line arguments, file selection, and execution mode are handled before control passes into the language pipeline.

4. **Lexing: source text to tokens**
   - `src/lexer.php` converts raw Lumen source text into tokens.
   - Token definitions and related structures live in `src/structures/tokens.php`.
   - `src/structures/cursor.php` likely tracks position in the input stream for scanning and error reporting.

5. **Parsing: tokens to program structure**
   - `src/parser.php` turns token streams into a structured syntax tree.
   - Abstract syntax tree or node types are defined in `src/structures/nodes.php`.
   - This is the main boundary where language syntax is interpreted into executable semantic structure.

6. **Optimization pass**
   - `src/optimizer.php` sits between parsing and execution.
   - Its role is likely to simplify or transform the AST before interpretation, improving runtime efficiency or normalizing constructs.

7. **Execution engine**
   - `src/interpreter.php` is the core evaluator for Lumen programs.
   - It consumes the parsed/optimized node structure and executes language semantics.
   - This is the central runtime boundary where expressions, statements, variables, and function-like behavior are resolved.

8. **Native/library surface**
   - `src/nativelib/` contains built-in functionality exposed to Lumen code.
   - The modules are split by domain:
     - `core.php` for language/runtime primitives
     - `std.php` for general standard utilities
     - `array.php` for array operations
     - `math.php` for numeric/math functions
   - These files represent the bridge between interpreted Lumen code and PHP-hosted capabilities.

9. **Diagnostics and error handling**
   - `src/structures/diagnostic.php` suggests structured error or warning reporting.
   - Together with cursor/token/node structures, this indicates the language pipeline is designed to produce source-aware diagnostics rather than generic PHP errors.

10. **Data flow through the system**
   - The main flow is:
     - source `.lumen` file
     - lexer
     - tokens
     - parser
     - AST nodes
     - optimizer
     - interpreter
     - native library calls when needed
   - This is the key architecture to draw: a classical interpreter pipeline implemented in PHP.

11. **Examples as language contracts**
   - The `examples/` directory (`hello.lumen`, `add.lumen`, `object.lumen`, `population.lumen`, `example.lumen`) demonstrates language features and likely serves as a regression/usage set.
   - These examples help define the supported surface area of the language: basic I/O, arithmetic, objects, and possibly data processing.

12. **Repository shape and intended use**
   - The project is organized around a single-purpose language runtime rather than a general framework.
   - The architecture is compact: one bootstrap, one CLI interface, one front-end compiler-like pipeline, one interpreter, and a small standard library layer.
   - For an architecture diagram, the important boundaries are between **CLI**, **language front end**, **optimization**, **execution**, and **native PHP-backed libraries**.

Main entry points: [`src/interpreter.php`](https://github.com/somespi/lumen/blob/master/src/interpreter.php) (Interpreter), [`src/lexer.php`](https://github.com/somespi/lumen/blob/master/src/lexer.php) (Lexer), [`src/parser.php`](https://github.com/somespi/lumen/blob/master/src/parser.php) (Parser), [`src/cli.php`](https://github.com/somespi/lumen/blob/master/src/cli.php) (CLI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_lumen_php["lumen.php<br/>bootstrap<br/>[lumen.php]"]
  node_cli["CLI<br/>entrypoint<br/>[cli.php]"]
  node_interpreter["Interpreter<br/>evaluator<br/>[interpreter.php]"]
end

subgraph group_frontend["Front End"]
  node_lexer["Lexer<br/>scanner<br/>[lexer.php]"]
  node_cursor(("Cursor<br/>input position<br/>[cursor.php]"))
  node_tokens["Tokens<br/>token model<br/>[tokens.php]"]
  node_parser["Parser<br/>[parser.php]"]
  node_nodes["AST Nodes<br/>syntax model<br/>[nodes.php]"]
  node_optimizer["Optimizer<br/>transform pass<br/>[optimizer.php]"]
  node_diagnostic["Diagnostics<br/>error model<br/>[diagnostic.php]"]
end

subgraph group_native["Native Libs"]
  node_native_core["Core<br/>native module<br/>[core.php]"]
  node_native_std["Std<br/>native module<br/>[std.php]"]
  node_native_array["Array<br/>native module<br/>[array.php]"]
  node_native_math["Math<br/>native module<br/>[math.php]"]
end

subgraph group_examples["Examples"]
  node_examples["Examples<br/>sample programs<br/>[example.lumen]"]
end

node_lumen_php -->|"launches"| node_cli
node_cli -->|"reads source"| node_lexer
node_lexer -->|"tracks position"| node_cursor
node_lexer -->|"emits"| node_tokens
node_lexer -->|"reports issues"| node_diagnostic
node_tokens -->|"consumes"| node_parser
node_parser -->|"builds"| node_nodes
node_parser -->|"reports issues"| node_diagnostic
node_nodes -->|"transforms"| node_optimizer
node_optimizer -->|"feeds"| node_interpreter
node_interpreter -->|"uses"| node_native_core
node_interpreter -->|"uses"| node_native_std
node_interpreter -->|"uses"| node_native_array
node_interpreter -->|"uses"| node_native_math
node_examples -.->|"run through"| node_cli

click node_lumen_php "https://github.com/somespi/lumen/blob/master/lumen.php"
click node_cli "https://github.com/somespi/lumen/blob/master/src/cli.php"
click node_lexer "https://github.com/somespi/lumen/blob/master/src/lexer.php"
click node_cursor "https://github.com/somespi/lumen/blob/master/src/structures/cursor.php"
click node_tokens "https://github.com/somespi/lumen/blob/master/src/structures/tokens.php"
click node_parser "https://github.com/somespi/lumen/blob/master/src/parser.php"
click node_nodes "https://github.com/somespi/lumen/blob/master/src/structures/nodes.php"
click node_optimizer "https://github.com/somespi/lumen/blob/master/src/optimizer.php"
click node_interpreter "https://github.com/somespi/lumen/blob/master/src/interpreter.php"
click node_diagnostic "https://github.com/somespi/lumen/blob/master/src/structures/diagnostic.php"
click node_native_core "https://github.com/somespi/lumen/blob/master/src/nativelib/core.php"
click node_native_std "https://github.com/somespi/lumen/blob/master/src/nativelib/std.php"
click node_native_array "https://github.com/somespi/lumen/blob/master/src/nativelib/array.php"
click node_native_math "https://github.com/somespi/lumen/blob/master/src/nativelib/math.php"
click node_examples "https://github.com/somespi/lumen/blob/master/examples/example.lumen"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_lumen_php,node_cli,node_interpreter toneBlue
class node_lexer,node_cursor,node_tokens,node_parser,node_nodes,node_optimizer,node_diagnostic toneAmber
class node_native_core,node_native_std,node_native_array,node_native_math toneMint
class node_examples toneRose
```

## Components

### Runtime

Bootstrap, CLI, and execution pipeline

- [lumen.php](https://github.com/somespi/lumen/blob/master/lumen.php): bootstrap · Main executable that wires CLI handling to the language runtime · `lumen.php`
- [CLI](https://github.com/somespi/lumen/blob/master/src/cli.php): entrypoint · Command-line interface for selecting and running Lumen programs · `src/cli.php`
- [Interpreter](https://github.com/somespi/lumen/blob/master/src/interpreter.php): evaluator · Executes optimized language nodes and resolves runtime semantics · `src/interpreter.php`

### Front End

Source processing and program structure

- [Lexer](https://github.com/somespi/lumen/blob/master/src/lexer.php): scanner · Converts source text into tokens · `src/lexer.php`
- [Cursor](https://github.com/somespi/lumen/blob/master/src/structures/cursor.php): input position · Tracks source position during scanning and diagnostics · `src/structures/cursor.php`
- [Tokens](https://github.com/somespi/lumen/blob/master/src/structures/tokens.php): token model · Token definitions and token structures used by the lexer and parser · `src/structures/tokens.php`
- [Parser](https://github.com/somespi/lumen/blob/master/src/parser.php): syntax parser · Turns token streams into structured syntax trees · `src/parser.php`
- [AST Nodes](https://github.com/somespi/lumen/blob/master/src/structures/nodes.php): syntax model · Abstract syntax tree node definitions for the language · `src/structures/nodes.php`
- [Optimizer](https://github.com/somespi/lumen/blob/master/src/optimizer.php): transform pass · Simplifies or normalizes the parsed program before execution · `src/optimizer.php`
- [Diagnostics](https://github.com/somespi/lumen/blob/master/src/structures/diagnostic.php): error model · Structured source-aware errors and warnings · `src/structures/diagnostic.php`

### Native Libs

PHP-backed built-in functionality

- [Core](https://github.com/somespi/lumen/blob/master/src/nativelib/core.php): native module · Runtime primitives and host-language integration · `src/nativelib/core.php`
- [Std](https://github.com/somespi/lumen/blob/master/src/nativelib/std.php): native module · General standard utilities exposed to Lumen code · `src/nativelib/std.php`
- [Array](https://github.com/somespi/lumen/blob/master/src/nativelib/array.php): native module · Built-in array operations · `src/nativelib/array.php`
- [Math](https://github.com/somespi/lumen/blob/master/src/nativelib/math.php): native module · Built-in numeric and math functions · `src/nativelib/math.php`

### Examples

Sample Lumen programs

- [Examples](https://github.com/somespi/lumen/blob/master/examples/example.lumen): sample programs · Representative Lumen programs demonstrating language features · `examples/example.lumen`

## Connections

- lumen.php → CLI: launches · Bootstrap hands control to the CLI entry point
- CLI → Lexer: reads source · CLI provides source text for lexing
- Lexer → Cursor: tracks position · Lexer uses cursor state while scanning input
- Lexer → Tokens: emits · Source text is converted into tokens
- Lexer → Diagnostics: reports issues · Lexing problems are emitted as structured diagnostics
- Tokens → Parser: consumes · Parser consumes token streams
- Parser → AST Nodes: builds · Parser produces syntax tree nodes
- Parser → Diagnostics: reports issues · Parsing problems are emitted as structured diagnostics
- AST Nodes → Optimizer: transforms · Optimizer rewrites parsed nodes
- Optimizer → Interpreter: feeds · Optimized program structure is executed by the interpreter
- Interpreter → Core: uses · Interpreter relies on core runtime primitives
- Interpreter → Std: uses · Interpreter can call standard utilities
- Interpreter → Array: uses · Interpreter can call array helpers
- Interpreter → Math: uses · Interpreter can call math helpers
- Examples → CLI: run through · Example programs are executed via the CLI/runtime path
