How engboris/stellogen works
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: evaluator.ml Evaluator · sgen.ml sgen · web_interface.ml Web interface · parser.mly Parser
Components 21
Frontends
Command-line and browser entrypoints that drive the language pipeline.
- sgen · Main command-line driver for run, preprocess, trace, and watch modes.
bin/sgen.ml - Playground · Browser-facing entrypoint for experimenting with Stellogen programs.
web/playground.ml - Web interface · 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 · Converts source text into tokens before parsing.
src/lexer.ml - Parser · Builds syntax from tokens using the OCaml parser specification.
src/parser.mly - Parse context · Tracks parsing context and helps shape parser errors and recovery.
src/parser_context.ml - Parsing glue · Coordinates parsing utilities and error boundaries.
src/parsing/stellogen_parsing.ml - Parse errors · 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 · Core language object model built around unification-centered computation.
src/core/constellation.ml - Expression · Expression forms and semantic structure used throughout the core.
src/core/expression.ml - Syntax · Shared syntax representation bridging parsed forms and core terms.
src/core/syntax.ml - Unification · Central unification engine for both computation and typing.
src/core/unification.ml - Expr errors · 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 · Defines the evaluation contract for running core programs.
src/eval/evaluator.ml - Executor · Drives execution steps and runtime progression.
src/eval/executor.ml - Constellation eval · Adapts constellation semantics into executable evaluation steps.
src/eval/constellation_eval.ml
Output
User-facing formatting, terminal behavior, and tracing.
- Pretty print · Formats language structures for human-readable output.
src/output/pretty.ml - Terminal · Console behavior and command-line presentation helpers.
src/output/terminal.ml - Tracer · Visualizes or streams execution for trace mode.
src/output/tracer.ml
Connections 26
- 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.