How gladw-in/gladlang works
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: interpreter.py Interpreter · value.py Value base · util Exec utils · parser.py Parser
Components 19
Entry points
CLI and source runner shells that start interpretation.
- run.py · Thin runner for executing Glad source from the repository root.
run.py - CLI main · Installed command entry that routes into the interpreter pipeline.
src/gladlang/cli/main.py - Module entry · Package __main__ hook that enables python -m gladlang.
src/gladlang/__main__.py
Language pipeline
Lexing, parsing, and AST construction for Glad source.
- Lexer · Converts Glad source text into a token stream.
src/gladlang/lexer/lexer.py - Tokens · Token definitions and values consumed by the parser.
src/gladlang/lexer/token.py - Parser · Consumes tokens and builds parse trees and AST nodes.
src/gladlang/parser/parser.py - AST model · Language structure organized into expressions, statements, declarations, access, and comprehensions.
src/gladlang/parser/ast
Runtime core
Execution state, control flow, and object model.
- Interpreter · Central AST walker that executes programs and dispatches runtime behavior.
src/gladlang/interpreter/interpreter.py - Context · Carries scope, traceback metadata, and execution nesting.
src/gladlang/runtime/context.py - Symbol table · Lexical scope storage with parent chaining for names and bindings.
src/gladlang/runtime/symbol_table.py - RT result · Propagates returns, breaks, continues, and runtime failures through execution.
src/gladlang/runtime/rt_result.py - Value base · Shared abstraction for interpreter values and language semantics.
src/gladlang/values/value.py - Primitives · Core mutable and immutable data types such as numbers, strings, lists, and dicts.
src/gladlang/values/primitives - Functions · User-defined and built-in callables, including bound methods and overload groups.
src/gladlang/values/functions - Classes · Class, instance, super, and type behavior for object-oriented execution.
src/gladlang/values/classes - Enums · Runtime enum representation used by the language.
src/gladlang/values/enums/enum.py
Support layers
Errors, constants, and execution safeguards.
- Errors · Lexical, syntax, and runtime error hierarchy with source positions and tracebacks.
src/gladlang/core/errors - Language tables · Keywords, character sets, and token types shared by lexer and parser.
src/gladlang/core/constants - Exec utils · Runner orchestration, global scope setup, REPL helpers, source detaching, and safeguards.
src/gladlang/core/util
Connections 22
- 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.