# GCSL Technical Reference Manual

> The interpreter pipeline, execution state, and tooling boundaries.

---

# 100 The Language Pipeline

_GCSL / gcsl-internals_

> Follow source through checking and type-specialized evaluation.

GCSL is a tree-walking interpreter with a typed intermediate
representation. The stages are separate so a formatter or editor can stop
before execution while using the same syntax and type rules as the CLI.

The main pipeline is:

```text
SourceFile
    -> lexer and interned names/strings
    -> parser and arena-based syntax tree
    -> workspace resolution and type-check side tables
    -> type-specialized intermediate representation
    -> evaluation and a value with warnings
```

The lexer and parser can recover enough structure to report multiple
errors. A failed check is never lowered into executable code. Recovery
supports diagnostics and editor queries; it does not make a partially
valid program executable.

The checker resolves names and types expressions, recording captures,
slots, argument destinations, and required awaits in side tables.
Lowering makes those awaits explicit. Evaluation dispatches through
specialized operations whose types have already been checked, rather than
repeating the type system at each operation.

## 100.1 Workspace scheduling

The workspace scanner merges public field paths, resolves imports, and
orders source files by dependencies. Each public leaf has an owner and a
cell. Reading an interior record can require all its contributing owners.
A cycle is a diagnostic, not an order selected at runtime.

Top-level field scheduling differs from ordinary block order. It supports
forward references while ensuring dependencies are initialized before a
read. Shadowed declarations are still checked even when they do not
contribute a runtime value.

## 100.2 Sources

The principal implementation is in `crates/gcsl/src/`: the lexer in
`lexer.rs`, and the parser, workspace, checker, lowering and evaluator
each in a root module (`parser.rs`, `workspace.rs`, `check.rs`,
`lower.rs`, `eval.rs`) with a directory of topic modules beside it, such
as `check/records.rs` or `eval/futures.rs`. Each root documents its
internal invariants and lists its modules, and each module documents its
part. The implementation here is not the separate Tree-sitter grammar
used for highlighting.

---

# 200 Evaluation State

_GCSL / gcsl-internals_

> Understand values, futures, memo tables, and failure cleanup.

GCSL evaluation uses reference-counted values and explicit execution
contexts. A closure captures values; a frame holds the current call's
slots. This supports value semantics without exposing shared mutable
variables to programs.

## 200.1 Futures and memo tables

A future begins pending, becomes running during evaluation, and records a
successful result. Reentering running work is an error rather than an
infinite cycle through the same pending computation. A failed evaluation
does not become a cached success.

Memo tables belong to the execution context and are keyed by closure
identity and argument values, with supported ambient inputs included when
used. Keeping tables outside closures avoids introducing reference cycles
through cached keys and results. Successful entries for live closures
remain until their context ends; they are not an eviction-based cache.

## 200.2 Exits and cleanup

Runtime errors and language control flow share an unwinding path through
the evaluator. A loop handles its own `break` or `continue`; a function,
future, or labeled block handles a matching `return`. Intermediate
operations restore stack and evaluation state before propagating an exit.

A failed REPL input does not commit its new bindings or memo-table changes
to the session. Effects already performed outside the interpreter are not
rolled back. A file written or a subprocess started before an error still
requires the host's ordinary handling.

A workspace remembers the first runtime failure of each file. A later
request that needs that file receives the original diagnostic without
rerunning its effects or starting dependent files. Successful prior files
keep their values, and unrelated files can still run. Create a new
workspace object when you intentionally need to retry evaluation.

## 200.3 Resource limits

The parser's maximum expression depth is `gcsl::MAX_DEPTH`, 10,000 levels.
Runtime expressions, calls, and awaits share a nesting budget; calls cost
additional levels. Iterations reuse their body's depth rather than adding
one level per iteration. Tail calls are not eliminated.

These limits protect the recursive implementation, not total computation.
A program can still loop indefinitely, allocate large values, capture
large process output, or retain many memoized results. The embedding host
owns any execution timeout and memory limit.

The main implementation lives in `eval.rs` and the modules under `eval/`,
`value.rs`, and `session.rs`.

---

# 300 Tooling State

_GCSL / gcsl-internals_

> Understand source discovery, editor snapshots, and formatter boundaries.

GCSL tooling shares language analysis while keeping disk access and editor
protocol handling outside the core. This prevents checking and formatting
from depending on an evaluator run.

## 300.1 Source discovery

`gcsl-disk` finds workspace markers and returns a source collection with
stable path and diagnostic mappings. It excludes hidden names, nested
workspace trees, and paths the root's `.gcslignore` names when reading one
workspace. The CLI and language server
use this shared layer rather than implementing separate import rules.

A check of a directory discovers workspaces. A format of a directory
finds source files, including standalone files. Their traversal results
are therefore intentionally different.

## 300.2 Editor snapshots

`gcsl-lsp` overlays open document text on disk sources and asks a worker to
check the affected workspace. Queries use an up-to-date semantic index;
they can force a pending check before answering. Diagnostics continue to
refer to actual source even when a query temporarily repairs unmatched
braces to find usable names.

Rename computes proposed edits and checks their effect before offering
them. Standard-library, file, and directory identities are not editable
through that operation. Formatting returns edits from the language's
formatter rather than applying the editor's formatting preferences.

Only local `file:` document URIs with an empty or `localhost` authority
and an absolute decoded path map to disk files. Other URI forms are
checked as standalone documents, not treated as local workspace paths.

## 300.3 Formatter

The formatter works from tokens, comments, and parsed structure. It does
not need evaluation or successful type inference. Syntax errors prevent a
rewrite of that file. The CLI uses a temporary file and replacement so
readers don't observe a partially written formatted file.

The principal code is in `crates/gcsl-disk/src/lib.rs`,
`crates/gcsl-lsp/src/`, `crates/gcsl/src/index/`, and
`crates/gcsl/src/fmt.rs`. The CLI adapters live in
`crates/gcsl-cli/src/`.
