Skip to content

3 5 clause graphs and the lowering engine

TransCompilator edited this page Nov 26, 2026 · 1 revision

3.5 Clause Graphs and the Lowering Engine

Source: https://deepwiki.com/PonvasanthPon/TransCompilator/3-5-clause-graphs-and-the-lowering-engine

Clause Graphs and the Lowering Engine

Relevant source files

The following files were used as context for generating this wiki page:

Purpose and Scope

This page details section 3.5 of the TransCompilator (tc_r1) codebase, focusing on compositional sentence decomposition via ClauseGraph and the multi-file lowering engine housed within the tc-semantics::lower module. The lowering engine transforms raw linguistic inputs and semantic frames into typed Abstract Syntax Tree (AST) nodes and Core-7 IR structures.


1. ClauseGraph Decomposition (clause_graph.rs)

The compositional clause decomposition engine evaluates sentences to recognize structured linguistic patterns such as leading Given/Assuming bindings, trailing where binding clauses, sequential then chains, and compound declarations crates/tc-semantics/src/clause_graph.rs:1-8.

The core data structure is ClauseGraph, which categorizes sentence components into bindings, sequential clauses, and compound names crates/tc-semantics/src/clause_graph.rs:13-23.

pub(crate) struct ClauseGraph {
    pub bindings: Vec<String>,
    pub clauses: Vec<String>,
    pub compound_names: Vec<String>,
    pub scoped: bool,
}

[crates/tc-semantics/src/clause_graph.rs:13-23]

The entry point for sentence splitting is decompose(text), which checks for leading bindings (decompose_leading), trailing scope bindings (decompose_where), sequential then transitions (split_then), compound declarations (split_compound_declaration), and general binding sequences crates/tc-semantics/src/clause_graph.rs:25-99.

graph TD
    Input["text"] --> Dec["decompose(text)"]
    Dec --> Lead["decompose_leading"]
    Dec --> Where["decompose_where"]
    Dec --> Then["split_then"]
    Dec --> Comp["split_compound_declaration"]
    Lead --> CG["ClauseGraph"]
    Where --> CG
    Then --> CG
    Comp --> CG
Loading

Diagram 1: Natural Language Space to Code Entity Space: Sentence Decomposition Pipeline

Sources: [crates/tc-semantics/src/clause_graph.rs:1-150]()


2. The Lowering Engine Architecture (lower/mod.rs)

The lowering engine is centered around the Lowerer struct, which maintains typing environments, discourse stacks, active trackers, loop depth guards, and error accumulation buffers crates/tc-semantics/src/lower/mod.rs:161-191.

pub(crate) struct Lowerer<'a> {
    parser: &'a crate::LinkGrammarParser,
    module: String,
    globals: HashMap<String, AstType>,
    types: HashSet<String>,
    aggregates: HashMap<String, AggregateInfo>,
    module_names: Vec<String>,
    phrases: Vec<PhraseBinding>,
    function_returns: HashMap<String, Option<AstType>>,
    scopes: Vec<Scope>,
    discourse: DiscourseStack,
    paraphrase_entries: Vec<ParaphraseEntry>,
    active_entry: Option<usize>,
    synthetic_depth: usize,
    trace: Vec<SentenceTrace>,
    errors: Vec<SemanticError>,
    temp_counter: usize,
    loop_depth: usize,
    // ... specialized trackers and counters ...
}

[crates/tc-semantics/src/lower/mod.rs:161-191]

The lowering driver processes modules into ModuleJob records, separating executable top-level sentences from function definitions awaiting body lowering crates/tc-semantics/src/lower/mod.rs:78-86.

Sources: [crates/tc-semantics/src/lower/mod.rs:52-220]()


3. Submodules of the Lowering Engine

The lower/ directory is partitioned into five specialized submodules:

graph TD
    Lowerer["Lowerer<'a>"] --> Stmts["stmts.rs: parse_expr"]
    Lowerer --> Decls["decls.rs: normalize_function_header"]
    Lowerer --> Control["control.rs: lower_items"]
    Lowerer --> Sequences["sequences.rs: ensure_largest_tracker"]
    Lowerer --> Helpers["helpers.rs: transform_operator"]
Loading

Diagram 2: Natural Language Space to Code Entity Space: Lowerer Submodule Dispatch

Sources: [crates/tc-semantics/src/lower/decls.rs:1-184](), [crates/tc-semantics/src/lower/stmts.rs:1-164](), [crates/tc-semantics/src/lower/control.rs:1-140](), [crates/tc-semantics/src/lower/sequences.rs:1-178](), [crates/tc-semantics/src/lower/helpers.rs:1-208]()


4. Data Flow and Execution State

During lowering, natural language phrases interact with stateful tracking mechanisms in Lowerer. For example, extreme value lookups and running totals are automatically injected into AST statement vectors when semantic patterns (such as running sums or extrema printing) are detected crates/tc-semantics/src/lower/sequences.rs:23-115.

Targets can resolve to local variables, global references, or raw memory locations through the Target enum:

#[derive(Debug, Clone)]
pub(crate) enum Target {
    Local(String),
    Global(String),
    Memory(AstExpr),
}

[crates/tc-semantics/src/lower/mod.rs:121-125]

Sources: [crates/tc-semantics/src/lower/mod.rs:107-138](), [crates/tc-semantics/src/lower/sequences.rs:23-115]()


Clone this wiki locally