Skip to main content

Module ast_depth

Module ast_depth 

Source
Expand description

The AST depth gate — “parsed ⇒ bounded”.

Every consumer downstream of the parser (optimizer, codegen, tree-walker, VM compiler, transpiler) walks the AST recursively; a tree nested thousands of levels deep (a 10,000-term 1+1+… chain, a parenthesis tower, a generated block pyramid) overflows their stacks and aborts the process — on every surface at once (CLI, REPL, LSP, web Studio).

The gate enforces one contract at the single choke point (Parser::parse_program): any program that parses has nesting depth ≤ the enforced limit (max_ast_depth), so downstream recursion is bounded by construction. Programs past the limit get a graceful AstTooDeep diagnostic with a Socratic fix (split into Let bindings).

The walker itself is iterative (an explicit work stack) — measuring the depth must never be the thing that overflows. Its matches are exhaustive with no wildcard: adding an AST variant with children fails compilation here, forcing the author to classify the new children — the ratchet that keeps the gate honest as the language grows.

Boxed noun-phrase payloads (Categorical, Relation, NeoEvent, comparatives) carry Terms whose nesting is bounded by English sentence structure, not by program size — they count as one level.

Enums§

Visited
A node for_each_node hands its visitor.
Walk
Whether a walk_nodes visitor goes on into the children of the node it was handed.

Constants§

DEFAULT_MAX_AST_DEPTH
The DEFAULT maximum AST nesting depth.
MIN_AST_DEPTH
The smallest limit the override accepts — below this, ordinary programs stop parsing.

Functions§

expr_children
The expressions an expression evaluates directly — its operands, arguments, elements — in the same exhaustive enumeration the depth gate walks. A closure’s body is not among them: it runs when the closure is called, not when the closure expression is evaluated.
for_each_node
Visit every statement and expression in stmts, each once — the same exhaustive child enumeration the depth gate walks, so a new variant’s children are reached here the moment the gate reaches them. Iterative, like the gate: visiting must never be what overflows.
limit_from
Pure resolution of the override value (unit-testable without touching process environment): a parseable value is clamped to at least MIN_AST_DEPTH; anything else falls back to the default.
max_ast_depth
The EFFECTIVE depth limit: DEFAULT_MAX_AST_DEPTH unless the LOGOS_MAX_AST_DEPTH environment variable overrides it.
max_parse_recursion
The parse-time recursion budget: an EIGHTH of the AST limit (min the floor).
program_depth
Measure the maximum nesting depth of a parsed program, iteratively.
stmt_expr_children
The expressions a statement evaluates directly (its value, condition, target, arguments — not the statements of its nested blocks).
validate_program_depth
Validate a parsed program against the effective limit (max_ast_depth).
walk_nodes
for_each_node, with the visitor answering for each statement and expression whether the walk goes on into its children. Exhaustive in the same way: a child is left unvisited only because a visitor said so, never because the walk does not know the kind of node it is in.