# engboris/stellogen architecture

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

- Interactive diagram: https://gitdiagram.com/engboris/stellogen
- Repository: https://github.com/engboris/stellogen

## Overview

1. **Project shape**
   Stellogen is an OCaml language implementation, not a general app. The main product is the `sgen` command-line tool, with a separate web playground. The repository is organized around a small core language runtime plus multiple frontends and outputs.

2. **Language pipeline**
   Source files (`.sg`) flow through lexical analysis (`src/lexer.ml`), parsing (`src/parser.mly`), and parsing-context/error handling (`src/parser_context.ml`, `src/parsing/*`). The parsed result is then converted into the language’s core representation under `src/core/`.

3. **Core semantic model**
   The architectural center is `src/core/constellation.ml`, `expression.ml`, `syntax.ml`, and `unification.ml`. This is where Stellogen’s distinctive idea lives: computation and typing are both expressed through term unification. `expression_error.ml` and `unification.mli` indicate a carefully separated error/contract boundary around the core algorithm.

4. **Execution layer**
   Runtime behavior sits in `src/eval/`. `evaluator.ml` / `evaluator.mli` define the evaluation interface, `executor.ml` likely drives stepwise execution, and `constellation_eval.ml` adapts the core constellation model into executable steps. This layer is the bridge between parsed programs and observable results.

5. **Command-line interface**
   `bin/sgen.ml` is the executable entrypoint, with `bin/dune` wiring it into the build. The CLI exposes the documented modes: `run`, `preprocess`, `trace`, and `watch`. These are not separate engines; they are different presentations of the same parse/evaluate pipeline.

6. **Output and diagnostics**
   User-facing rendering is isolated in `src/output/`: `pretty.ml` for formatting, `terminal.ml` for console behavior, and `tracer.ml` for execution visualization. This suggests a clean split between semantics and presentation, especially for the `trace` mode.

7. **Error handling boundaries**
   Errors are separated by stage: parse errors in `src/parsing/parse_error.ml`, unification or semantic failures in core/evaluation modules, and expression-level issues in `src/core/expression_error.ml`. The tests under `test/errors/` show the repository treats malformed syntax as a first-class boundary case.

8. **Web frontend**
   `src/web/web_interface.ml` plus `web/playground.ml` provide a browser-facing entrypoint. The `web/` directory contains its own build scripts and HTML shell, so the playground is a distinct frontend around the same compiler/evaluator logic rather than a different implementation.

9. **Repository content for language design**
   `examples/` and `exercises/` are not runtime subsystems, but they are important to architecture because they encode supported language features and intended usage patterns. The many `ai/research/*` documents and `docs/refocusing_plan.md` are design notes that explain the language model and future direction.

10. **Build, packaging, and automation**
   The project is built with OCaml/Dune, packaged via `stellogen.opam`, and can also be developed with Nix (`default.nix`, `flake.nix`). GitHub Actions (`.github/workflows/build.yml`) provide CI. Editor support is included through `nvim/` syntax and filetype definitions, which is useful for diagramming the ecosystem around the core tool.

11. **How to diagram the system**
   A good architecture diagram would show: source `.sg` files entering the lexer/parser, then the core unification-based model, then evaluation/execution, then one of several outputs (terminal run, step tracer, preprocessing view, or web playground). The main boundary worth emphasizing is between language semantics in `src/core` + `src/eval` and presentation/frontends in `bin/`, `src/output/`, and `src/web/`.

Main entry points: [`src/eval/evaluator.ml`](https://github.com/engboris/stellogen/blob/master/src/eval/evaluator.ml) (Evaluator), [`bin/sgen.ml`](https://github.com/engboris/stellogen/blob/master/bin/sgen.ml) (sgen), [`src/web/web_interface.ml`](https://github.com/engboris/stellogen/blob/master/src/web/web_interface.ml) (Web interface), [`src/parser.mly`](https://github.com/engboris/stellogen/blob/master/src/parser.mly) (Parser)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontends["Frontends"]
  node_bin_sgen["sgen<br/>CLI entrypoint<br/>[sgen.ml]"]
  node_web_playground["Playground<br/>web frontend<br/>[playground.ml]"]
  node_web_interface["Web interface<br/>web bridge<br/>[web_interface.ml]"]
end

subgraph group_pipeline["Language Pipeline"]
  node_lexer["Lexer<br/>tokenizer<br/>[lexer.ml]"]
  node_parser["Parser<br/>[parser.mly]"]
  node_parser_context["Parse context<br/>parse state<br/>[parser_context.ml]"]
  node_stellogen_parsing["Parsing glue<br/>parse orchestration"]
  node_parse_error["Parse errors<br/>error boundary<br/>[parse_error.ml]"]
end

subgraph group_core["Core Model"]
  node_constellation(("Constellation<br/>core semantics<br/>[constellation.ml]"))
  node_expression["Expression<br/>core AST<br/>[expression.ml]"]
  node_syntax["Syntax<br/>[syntax.ml]"]
  node_unification(("Unification<br/>core algorithm<br/>[unification.ml]"))
  node_expression_error["Expr errors<br/>core error"]
end

subgraph group_runtime["Execution"]
  node_evaluator["Evaluator<br/>eval interface<br/>[evaluator.ml]"]
  node_executor["Executor<br/>step runner<br/>[executor.ml]"]
  node_constellation_eval["Constellation eval<br/>eval adapter"]
end

subgraph group_presentation["Output"]
  node_pretty["Pretty print<br/>formatter<br/>[pretty.ml]"]
  node_terminal["Terminal<br/>console output<br/>[terminal.ml]"]
  node_tracer["Tracer<br/>trace renderer<br/>[tracer.ml]"]
end

subgraph group_ecosystem["Ecosystem"]
  node_examples["Examples<br/>language samples<br/>[hello.sg]"]
  node_tests["Tests<br/>validation suite<br/>[errors.t]"]
end

node_bin_sgen -->|"starts"| node_lexer
node_bin_sgen -->|"parses"| node_parser
node_bin_sgen -->|"runs"| node_evaluator
node_bin_sgen -->|"prints"| node_pretty
node_bin_sgen -->|"traces"| node_tracer
node_web_playground -->|"uses"| node_web_interface
node_web_interface -->|"parses"| node_lexer
node_web_interface -->|"parses"| node_parser
node_web_interface -->|"evaluates"| node_evaluator
node_lexer -->|"tokens"| node_parser
node_parser -->|"context"| node_parser_context
node_parser_context -->|"supports"| node_stellogen_parsing
node_stellogen_parsing -->|"reports"| node_parse_error
node_stellogen_parsing -->|"produces"| node_syntax
node_syntax -->|"lowers to"| node_constellation
node_constellation -->|"contains"| node_expression
node_constellation -->|"relies on"| node_unification
node_expression -->|"fails with"| node_expression_error
node_unification -->|"drives"| node_evaluator
node_constellation_eval -->|"implements"| node_evaluator
node_executor -->|"steps"| node_constellation_eval
node_evaluator -->|"formats"| node_pretty
node_evaluator -->|"outputs"| node_terminal
node_evaluator -->|"records"| node_tracer
node_examples -->|"exercises"| node_bin_sgen
node_tests -->|"guards"| node_parse_error

click node_bin_sgen "https://github.com/engboris/stellogen/blob/master/bin/sgen.ml"
click node_web_playground "https://github.com/engboris/stellogen/blob/master/web/playground.ml"
click node_web_interface "https://github.com/engboris/stellogen/blob/master/src/web/web_interface.ml"
click node_lexer "https://github.com/engboris/stellogen/blob/master/src/lexer.ml"
click node_parser "https://github.com/engboris/stellogen/blob/master/src/parser.mly"
click node_parser_context "https://github.com/engboris/stellogen/blob/master/src/parser_context.ml"
click node_stellogen_parsing "https://github.com/engboris/stellogen/blob/master/src/parsing/stellogen_parsing.ml"
click node_parse_error "https://github.com/engboris/stellogen/blob/master/src/parsing/parse_error.ml"
click node_constellation "https://github.com/engboris/stellogen/blob/master/src/core/constellation.ml"
click node_expression "https://github.com/engboris/stellogen/blob/master/src/core/expression.ml"
click node_syntax "https://github.com/engboris/stellogen/blob/master/src/core/syntax.ml"
click node_unification "https://github.com/engboris/stellogen/blob/master/src/core/unification.ml"
click node_expression_error "https://github.com/engboris/stellogen/blob/master/src/core/expression_error.ml"
click node_evaluator "https://github.com/engboris/stellogen/blob/master/src/eval/evaluator.ml"
click node_executor "https://github.com/engboris/stellogen/blob/master/src/eval/executor.ml"
click node_constellation_eval "https://github.com/engboris/stellogen/blob/master/src/eval/constellation_eval.ml"
click node_pretty "https://github.com/engboris/stellogen/blob/master/src/output/pretty.ml"
click node_terminal "https://github.com/engboris/stellogen/blob/master/src/output/terminal.ml"
click node_tracer "https://github.com/engboris/stellogen/blob/master/src/output/tracer.ml"
click node_examples "https://github.com/engboris/stellogen/blob/master/examples/hello.sg"
click node_tests "https://github.com/engboris/stellogen/blob/master/test/errors.t"

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_bin_sgen,node_web_playground,node_web_interface toneBlue
class node_lexer,node_parser,node_parser_context,node_stellogen_parsing,node_parse_error toneAmber
class node_constellation,node_expression,node_syntax,node_unification,node_expression_error toneMint
class node_evaluator,node_executor,node_constellation_eval toneRose
class node_pretty,node_terminal,node_tracer toneIndigo
class node_examples,node_tests toneTeal
```

## Components

### Frontends

Command-line and browser entrypoints that drive the language pipeline.

- [sgen](https://github.com/engboris/stellogen/blob/master/bin/sgen.ml): CLI entrypoint · Main command-line driver for run, preprocess, trace, and watch modes. · `bin/sgen.ml`
- [Playground](https://github.com/engboris/stellogen/blob/master/web/playground.ml): web frontend · Browser-facing entrypoint for experimenting with Stellogen programs. · `web/playground.ml`
- [Web interface](https://github.com/engboris/stellogen/blob/master/src/web/web_interface.ml): web bridge · Adapts the compiler/evaluator stack for the web playground. · `src/web/web_interface.ml`

### Language Pipeline

Lexing, parsing, and parse-context handling for .sg sources.

- [Lexer](https://github.com/engboris/stellogen/blob/master/src/lexer.ml): tokenizer · Converts source text into tokens before parsing. · `src/lexer.ml`
- [Parser](https://github.com/engboris/stellogen/blob/master/src/parser.mly): grammar parser · Builds syntax from tokens using the OCaml parser specification. · `src/parser.mly`
- [Parse context](https://github.com/engboris/stellogen/blob/master/src/parser_context.ml): parse state · Tracks parsing context and helps shape parser errors and recovery. · `src/parser_context.ml`
- [Parsing glue](https://github.com/engboris/stellogen/blob/master/src/parsing/stellogen_parsing.ml): parse orchestration · Coordinates parsing utilities and error boundaries. · `src/parsing/stellogen_parsing.ml`
- [Parse errors](https://github.com/engboris/stellogen/blob/master/src/parsing/parse_error.ml): error boundary · Represents malformed syntax and parser-stage failures. · `src/parsing/parse_error.ml`

### Core Model

The unification-based semantic representation at the heart of Stellogen.

- [Constellation](https://github.com/engboris/stellogen/blob/master/src/core/constellation.ml): core semantics · Core language object model built around unification-centered computation. · `src/core/constellation.ml`
- [Expression](https://github.com/engboris/stellogen/blob/master/src/core/expression.ml): core AST · Expression forms and semantic structure used throughout the core. · `src/core/expression.ml`
- [Syntax](https://github.com/engboris/stellogen/blob/master/src/core/syntax.ml): core syntax · Shared syntax representation bridging parsed forms and core terms. · `src/core/syntax.ml`
- [Unification](https://github.com/engboris/stellogen/blob/master/src/core/unification.ml): core algorithm · Central unification engine for both computation and typing. · `src/core/unification.ml`
- [Expr errors](https://github.com/engboris/stellogen/blob/master/src/core/expression_error.ml): core error · Expression-level failure boundary separate from parsing and evaluation. · `src/core/expression_error.ml`

### Execution

Evaluation and execution layers that turn core terms into behavior.

- [Evaluator](https://github.com/engboris/stellogen/blob/master/src/eval/evaluator.ml): eval interface · Defines the evaluation contract for running core programs. · `src/eval/evaluator.ml`
- [Executor](https://github.com/engboris/stellogen/blob/master/src/eval/executor.ml): step runner · Drives execution steps and runtime progression. · `src/eval/executor.ml`
- [Constellation eval](https://github.com/engboris/stellogen/blob/master/src/eval/constellation_eval.ml): eval adapter · Adapts constellation semantics into executable evaluation steps. · `src/eval/constellation_eval.ml`

### Output

User-facing formatting, terminal behavior, and tracing.

- [Pretty print](https://github.com/engboris/stellogen/blob/master/src/output/pretty.ml): formatter · Formats language structures for human-readable output. · `src/output/pretty.ml`
- [Terminal](https://github.com/engboris/stellogen/blob/master/src/output/terminal.ml): console output · Console behavior and command-line presentation helpers. · `src/output/terminal.ml`
- [Tracer](https://github.com/engboris/stellogen/blob/master/src/output/tracer.ml): trace renderer · Visualizes or streams execution for trace mode. · `src/output/tracer.ml`

### Ecosystem

Examples, exercises, tests, and documentation around the language.

- [Examples](https://github.com/engboris/stellogen/blob/master/examples/hello.sg): language samples · Representative Stellogen programs showcasing supported features. · `examples/hello.sg`
- [Tests](https://github.com/engboris/stellogen/blob/master/test/errors.t): validation suite · Syntax and error fixtures that define expected language behavior. · `test/errors.t`

## Connections

- sgen → Lexer: starts · CLI invokes the source-processing pipeline.
- sgen → Parser: parses · Command modes begin with syntax analysis.
- sgen → Evaluator: runs · Runtime commands delegate to evaluation.
- sgen → Pretty print: prints · Textual results are formatted for the terminal.
- sgen → Tracer: traces · Trace mode renders execution output.
- Playground → Web interface: uses · Browser UI is backed by the web interface bridge.
- Web interface → Lexer: parses · Web frontend uses the same language frontend.
- Web interface → Parser: parses · Web frontend uses the same language frontend.
- Web interface → Evaluator: evaluates · Playground executes programs through the shared runtime.
- Lexer → Parser: tokens · Lexed tokens feed the grammar parser.
- Parser → Parse context: context · Parser uses contextual state for parsing and recovery.
- Parse context → Parsing glue: supports · Context helpers are orchestrated by parsing utilities.
- Parsing glue → Parse errors: reports · Parsing glue routes malformed input into parse errors.
- Parsing glue → Syntax: produces · Parsed input is converted into shared syntax.
- Syntax → Constellation: lowers to · Syntax is refined into the core semantic model.
- Constellation → Expression: contains · Core constellation structure is expressed with expressions.
- Constellation → Unification: relies on · Unification is the central computation mechanism.
- Expression → Expr errors: fails with · Expression-level issues are isolated from parser errors.
- Unification → Evaluator: drives · The runtime depends on unification-based semantics.
- Constellation eval → Evaluator: implements · Evaluation adapter satisfies the evaluator contract.
- Executor → Constellation eval: steps · Executor advances the adapted constellation runtime.
- Evaluator → Pretty print: formats · Evaluation results are prepared for display.
- Evaluator → Terminal: outputs · Command-line execution targets terminal presentation.
- Evaluator → Tracer: records · Execution can be rendered as a trace.
- Examples → sgen: exercises · Examples are meant to be run through the CLI.
- Tests → Parse errors: guards · Error fixtures validate malformed syntax handling.
