# gladw-in/gladlang architecture

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

- Interactive diagram: https://gitdiagram.com/gladw-in/gladlang
- Repository: https://github.com/gladw-in/gladlang

## Overview

1. **Repository shape**
   - This is a Python package for an interpreter, not an application server or library API.
   - The main package lives under `src/gladlang`, with a thin source runner at `run.py`, packaging metadata in `setup.py`, and end-user tests as `.glad` programs in `tests/`.

2. **Execution entry points**
   - There are two front doors: the installed CLI (`src/gladlang/__main__.py` → `src/gladlang/cli/main.py`) and the source runner (`run.py`).
   - Both paths ultimately invoke the same interpreter pipeline, which is important for an architecture diagram: CLI/file execution and REPL are just different shells around one core engine.

3. **Language processing pipeline**
   - Source code flows through a classic interpreter stack: **source text → lexer → token stream → parser → AST → interpreter**.
   - `lexer/lexer.py` turns raw `.glad` text into tokens defined in `lexer/token.py` and `core/constants/token_types.py`.
   - `parser/parser.py` consumes tokens and builds AST nodes under `parser/ast/...`.

4. **AST organization**
   - The AST is intentionally split by syntactic domain: expressions, statements, declarations, access operations, and comprehensions.
   - That split mirrors the language features in the README: functions, classes, loops, try/catch, list/dict comprehensions, slicing, attribute access, and assignment variants.
   - For diagramming, treat the AST layer as the language’s structural model, not as runtime objects.

5. **Interpreter core**
   - `interpreter/interpreter.py` is the central evaluator that walks the AST and performs execution.
   - It is the bridge between parsed syntax and runtime behavior, dispatching on node types and producing/consuming runtime `Value` objects.

6. **Runtime state and scope**
   - `runtime/context.py`, `runtime/symbol_table.py`, and `runtime/rt_result.py` manage execution state.
   - `Context` and `SymbolTable` provide lexical scope, parent chaining, and traceback metadata.
   - `rt_result.py` is the control-flow carrier for non-local outcomes like returns, breaks, continues, and runtime errors.

7. **Runtime object model**
   - `values/` defines the interpreter’s internal data types: primitives (`number`, `string`, `list`, `dict`), functions, classes, instances, enums, and null variants.
   - This layer is where language semantics live: truthiness, operator behavior, callable objects, member access, and coercion.
   - `values/value.py` is the shared base abstraction that most runtime types build on.

8. **Object-oriented subsystem**
   - The OOP model is isolated in `values/classes/` and `values/functions/`.
   - `class_.py`, `instance.py`, `super_.py`, and `type_.py` handle class definitions, instance creation, inheritance resolution, and runtime type checks.
   - `function_group.py` and `bound_method.py` support overloaded functions/methods and method binding to instances.

9. **Error handling and diagnostics**
   - The interpreter has a dedicated error hierarchy in `core/errors/` for lexical, syntax, and runtime failures.
   - `position.py` and the error classes are used to report source locations and build tracebacks, matching the README’s emphasis on user-friendly diagnostics.
   - This boundary is important: parsing errors stop before runtime; runtime errors propagate through execution context.

10. **Execution safeguards and shared utilities**
   - `core/util/` contains helpers for global scope setup, REPL behavior, runner orchestration, final/immutable handling, memory protections, and instruction budgeting.
   - These utilities are part of the execution boundary rather than the language semantics: they keep the interpreter stable, enforce limits, and support interactive use.
   - `locking.py` and `memory.py` suggest runtime safeguards around constants and resource limits.

11. **Constants and language tables**
   - `core/constants/` centralizes lexical and syntax tables such as keywords, character sets, and token types.
   - This keeps the lexer/parser aligned with the language definition and avoids scattering reserved-word logic across the codebase.

12. **Testing model**
   - The `tests/*.glad` files are end-to-end language programs, not unit tests for internal Python classes.
   - That indicates the project is validated primarily through black-box language behavior: parse a `.glad` file, execute it, and compare the result/output/error behavior.

Main entry points: [`src/gladlang/interpreter/interpreter.py`](https://github.com/gladw-in/gladlang/blob/main/src/gladlang/interpreter/interpreter.py) (Interpreter), [`src/gladlang/values/value.py`](https://github.com/gladw-in/gladlang/blob/main/src/gladlang/values/value.py) (Value base), [`src/gladlang/core/util`](https://github.com/gladw-in/gladlang/tree/main/src/gladlang/core/util) (Exec utils), [`src/gladlang/parser/parser.py`](https://github.com/gladw-in/gladlang/blob/main/src/gladlang/parser/parser.py) (Parser)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry points"]
  node_run_py["run.py<br/>source runner<br/>[run.py]"]
  node_cli_main["CLI main<br/>command entry<br/>[main.py]"]
  node_main_mod["Module entry<br/>package entry<br/>[__main__.py]"]
end

subgraph group_pipeline["Language pipeline"]
  node_lexer["Lexer<br/>tokenizer<br/>[lexer.py]"]
  node_token["Tokens<br/>token model<br/>[token.py]"]
  node_parser["Parser<br/>[parser.py]"]
  node_ast_root{{"AST model<br/>syntax tree"}}
end

subgraph group_runtime["Runtime core"]
  node_interp["Interpreter<br/>evaluator<br/>[interpreter.py]"]
  node_context["Context<br/>[context.py]"]
  node_symtab[("Symbol table<br/>scope chain<br/>[symbol_table.py]")]
  node_rt_result["RT result<br/>control carrier<br/>[rt_result.py]"]
  node_value_base{{"Value base<br/>runtime model<br/>[value.py]"}}
  node_primitives[("Primitives<br/>data values")]
  node_functions["Functions<br/>callables"]
  node_classes["Classes<br/>oop model"]
  node_enums["Enums<br/>enum model<br/>[enum.py]"]
end

subgraph group_support["Support layers"]
  node_errors["Errors<br/>diagnostics"]
  node_core_constants["Language tables<br/>constants"]
  node_core_util["Exec utils<br/>runtime support"]
end

node_run_py -->|"launches"| node_core_util
node_main_mod -->|"delegates"| node_cli_main
node_cli_main -->|"uses"| node_core_util
node_core_util -->|"feeds source"| node_lexer
node_lexer -->|"emits"| node_token
node_lexer -->|"reads tables"| node_core_constants
node_parser -->|"consumes"| node_token
node_parser -->|"builds"| node_ast_root
node_parser -->|"raises syntax"| node_errors
node_interp -->|"walks"| node_ast_root
node_interp -->|"updates"| node_context
node_interp -->|"resolves names"| node_symtab
node_interp -->|"propagates"| node_rt_result
node_interp -->|"operates on"| node_value_base
node_value_base -->|"specializes into"| node_primitives
node_value_base -->|"specializes into"| node_functions
node_value_base -->|"specializes into"| node_classes
node_value_base -->|"specializes into"| node_enums
node_functions -->|"binds methods"| node_classes
node_classes -->|"captures scope"| node_context
node_core_util -->|"reports"| node_errors
node_interp -->|"raises runtime"| node_errors

click node_run_py "https://github.com/gladw-in/gladlang/blob/main/run.py"
click node_cli_main "https://github.com/gladw-in/gladlang/blob/main/src/gladlang/cli/main.py"
click node_main_mod "https://github.com/gladw-in/gladlang/blob/main/src/gladlang/__main__.py"
click node_lexer "https://github.com/gladw-in/gladlang/blob/main/src/gladlang/lexer/lexer.py"
click node_token "https://github.com/gladw-in/gladlang/blob/main/src/gladlang/lexer/token.py"
click node_parser "https://github.com/gladw-in/gladlang/blob/main/src/gladlang/parser/parser.py"
click node_ast_root "https://github.com/gladw-in/gladlang/tree/main/src/gladlang/parser/ast"
click node_interp "https://github.com/gladw-in/gladlang/blob/main/src/gladlang/interpreter/interpreter.py"
click node_context "https://github.com/gladw-in/gladlang/blob/main/src/gladlang/runtime/context.py"
click node_symtab "https://github.com/gladw-in/gladlang/blob/main/src/gladlang/runtime/symbol_table.py"
click node_rt_result "https://github.com/gladw-in/gladlang/blob/main/src/gladlang/runtime/rt_result.py"
click node_value_base "https://github.com/gladw-in/gladlang/blob/main/src/gladlang/values/value.py"
click node_primitives "https://github.com/gladw-in/gladlang/tree/main/src/gladlang/values/primitives"
click node_functions "https://github.com/gladw-in/gladlang/tree/main/src/gladlang/values/functions"
click node_classes "https://github.com/gladw-in/gladlang/tree/main/src/gladlang/values/classes"
click node_enums "https://github.com/gladw-in/gladlang/blob/main/src/gladlang/values/enums/enum.py"
click node_errors "https://github.com/gladw-in/gladlang/tree/main/src/gladlang/core/errors"
click node_core_constants "https://github.com/gladw-in/gladlang/tree/main/src/gladlang/core/constants"
click node_core_util "https://github.com/gladw-in/gladlang/tree/main/src/gladlang/core/util"

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_run_py,node_cli_main,node_main_mod toneBlue
class node_lexer,node_token,node_parser,node_ast_root toneAmber
class node_interp,node_context,node_symtab,node_rt_result,node_value_base,node_primitives,node_functions,node_classes,node_enums toneMint
class node_errors,node_core_constants,node_core_util toneRose
```

## Components

### Entry points

CLI and source runner shells that start interpretation.

- [run.py](https://github.com/gladw-in/gladlang/blob/main/run.py): source runner · Thin runner for executing Glad source from the repository root. · `run.py`
- [CLI main](https://github.com/gladw-in/gladlang/blob/main/src/gladlang/cli/main.py): command entry · Installed command entry that routes into the interpreter pipeline. · `src/gladlang/cli/main.py`
- [Module entry](https://github.com/gladw-in/gladlang/blob/main/src/gladlang/__main__.py): package entry · Package __main__ hook that enables python -m gladlang. · `src/gladlang/__main__.py`

### Language pipeline

Lexing, parsing, and AST construction for Glad source.

- [Lexer](https://github.com/gladw-in/gladlang/blob/main/src/gladlang/lexer/lexer.py): tokenizer · Converts Glad source text into a token stream. · `src/gladlang/lexer/lexer.py`
- [Tokens](https://github.com/gladw-in/gladlang/blob/main/src/gladlang/lexer/token.py): token model · Token definitions and values consumed by the parser. · `src/gladlang/lexer/token.py`
- [Parser](https://github.com/gladw-in/gladlang/blob/main/src/gladlang/parser/parser.py): syntax parser · Consumes tokens and builds parse trees and AST nodes. · `src/gladlang/parser/parser.py`
- [AST model](https://github.com/gladw-in/gladlang/tree/main/src/gladlang/parser/ast): syntax tree · Language structure organized into expressions, statements, declarations, access, and comprehensions. · `src/gladlang/parser/ast`

### Runtime core

Execution state, control flow, and object model.

- [Interpreter](https://github.com/gladw-in/gladlang/blob/main/src/gladlang/interpreter/interpreter.py): evaluator · Central AST walker that executes programs and dispatches runtime behavior. · `src/gladlang/interpreter/interpreter.py`
- [Context](https://github.com/gladw-in/gladlang/blob/main/src/gladlang/runtime/context.py): exec context · Carries scope, traceback metadata, and execution nesting. · `src/gladlang/runtime/context.py`
- [Symbol table](https://github.com/gladw-in/gladlang/blob/main/src/gladlang/runtime/symbol_table.py): scope chain · Lexical scope storage with parent chaining for names and bindings. · `src/gladlang/runtime/symbol_table.py`
- [RT result](https://github.com/gladw-in/gladlang/blob/main/src/gladlang/runtime/rt_result.py): control carrier · Propagates returns, breaks, continues, and runtime failures through execution. · `src/gladlang/runtime/rt_result.py`
- [Value base](https://github.com/gladw-in/gladlang/blob/main/src/gladlang/values/value.py): runtime model · Shared abstraction for interpreter values and language semantics. · `src/gladlang/values/value.py`
- [Primitives](https://github.com/gladw-in/gladlang/tree/main/src/gladlang/values/primitives): data values · Core mutable and immutable data types such as numbers, strings, lists, and dicts. · `src/gladlang/values/primitives`
- [Functions](https://github.com/gladw-in/gladlang/tree/main/src/gladlang/values/functions): callables · User-defined and built-in callables, including bound methods and overload groups. · `src/gladlang/values/functions`
- [Classes](https://github.com/gladw-in/gladlang/tree/main/src/gladlang/values/classes): oop model · Class, instance, super, and type behavior for object-oriented execution. · `src/gladlang/values/classes`
- [Enums](https://github.com/gladw-in/gladlang/blob/main/src/gladlang/values/enums/enum.py): enum model · Runtime enum representation used by the language. · `src/gladlang/values/enums/enum.py`

### Support layers

Errors, constants, and execution safeguards.

- [Errors](https://github.com/gladw-in/gladlang/tree/main/src/gladlang/core/errors): diagnostics · Lexical, syntax, and runtime error hierarchy with source positions and tracebacks. · `src/gladlang/core/errors`
- [Language tables](https://github.com/gladw-in/gladlang/tree/main/src/gladlang/core/constants): constants · Keywords, character sets, and token types shared by lexer and parser. · `src/gladlang/core/constants`
- [Exec utils](https://github.com/gladw-in/gladlang/tree/main/src/gladlang/core/util): runtime support · Runner orchestration, global scope setup, REPL helpers, source detaching, and safeguards. · `src/gladlang/core/util`

## Connections

- run.py → Exec utils: launches · Source runner enters the shared execution helpers.
- Module entry → CLI main: delegates · Module execution hands off to the CLI entry.
- CLI main → Exec utils: uses · CLI relies on shared runner and REPL orchestration.
- Exec utils → Lexer: feeds source · Utilities supply source text to the lexing stage.
- Lexer → Tokens: emits · Lexer produces token stream objects.
- Lexer → Language tables: reads tables · Lexing uses keywords, charsets, and token type definitions.
- Parser → Tokens: consumes · Parser reads tokens from the lexer.
- Parser → AST model: builds · Parser constructs the AST hierarchy.
- Parser → Errors: raises syntax · Syntax failures stop in the parsing layer.
- Interpreter → AST model: walks · Interpreter evaluates AST nodes.
- Interpreter → Context: updates · Execution state and traceback metadata flow through context.
- Interpreter → Symbol table: resolves names · Name lookup and binding use lexical scope chains.
- Interpreter → RT result: propagates · Control-flow and error outcomes move through RT result objects.
- Interpreter → Value base: operates on · Evaluation produces and consumes runtime values.
- Value base → Primitives: specializes into · Primitive data types implement the base value contract.
- Value base → Functions: specializes into · Callable types derive from the shared value layer.
- Value base → Classes: specializes into · Object-oriented runtime types extend the base model.
- Value base → Enums: specializes into · Enum runtime objects participate in the same value system.
- Functions → Classes: binds methods · Bound methods connect function behavior to instances.
- Classes → Context: captures scope · Instance and method execution carry context and parent scope information.
- Exec utils → Errors: reports · Execution helpers surface diagnostics through the error hierarchy.
- Interpreter → Errors: raises runtime · Runtime failures are reported through interpreter errors.
