Skip to content

3 3 semantic frames and expressions

TransCompilator edited this page Nov 26, 2026 · 1 revision

3.3 Semantic Frames and Expressions

Source: https://deepwiki.com/PonvasanthPon/TransCompilator/3-3-semantic-frames-and-expressions

Semantic Frames and Expressions

Relevant source files

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

Purpose and Scope

This page documents the core semantic structures and transformation routines within tc-semantics that bridge normalized natural-language English clauses to typed Abstract Syntax Tree (AST) nodes (AstStmt, AstExpr) and target IR. Specifically, it covers the SemanticFrame model (Emit, Assign, Transfer, Swap), place specifications (PlaceSpec), the deterministic expression parser (ExprEnv and Parser), and declarative phrase signatures (PhraseBinding and PhraseMatch).

Sources: crates/tc-semantics/src/frame.rs:1-16, crates/tc-semantics/src/expr.rs:1-5, crates/tc-semantics/src/phrases.rs:1-10


1. The SemanticFrame Data Model

Finite semantic frames serve as the intermediate representation between sentence normalisation and statement lowering. Every executable statement is classified into one of four primary variants within SemanticFrame:

  • Emit: Concept-driven output actions (e.g., print, show, display, output, say, report).
  • Assign: Variable and place assignments (Let, Put, Set, Store, Keep, Make, becomes, =, Reset, Copy, Convert).
  • Transfer: Mutating arithmetic or state adjustments (Increment, Decrement, Adjust) that share place and amount parameters via TransferOp.
  • Swap: Exchange operations that swap two places (lhs and rhs).

The SemanticFrame enum and its helper constructors are defined as follows:

pub(crate) enum SemanticFrame {
    Emit { value: ValueSpec },
    Assign { target: PlaceSpec, value: ValueSpec, allow_declare: bool },
    Transfer { op: TransferOp, amount: Option<ValueSpec>, dest: PlaceSpec },
    Swap { lhs: PlaceSpec, rhs: PlaceSpec },
}

Lowering a SemanticFrame via Lowerer::lower_frame evaluates value specifications, performs type checks, and emits the corresponding sequence of AstStmt structures (such as print_str, print_u64, or direct assignments).

Sources: crates/tc-semantics/src/frame.rs:17-77, crates/tc-semantics/src/frame.rs:88-108


2. Place Specifications and Lvalues

Place specifications (PlaceSpec) handle locations in memory that can be read from or written to. These include scalar variables, struct field accesses (base.field), array/pointer indexing (ptr[index]), and dereferences (*ptr).

The normaliser parses place text using parse_place, translating raw English noun phrases into structured lvalue targets. During expression lowering, these places resolve to typed pointer or address expressions (AstExpr::Var, AstExpr::FieldAddr, AstExpr::IndexAddr, AstExpr::Load).

Sources: crates/tc-semantics/src/frame.rs:80-86, crates/tc-semantics/src/expr.rs:83-121


3. Deterministic Expression Lowering

English mathematical, logical, and computational expressions are parsed by a deterministic, zero-ML, total recursive descent parser (Parser) operating over tokenized clause streams.

Environment and Type Resolution (ExprEnv)

The parser relies on ExprEnv to resolve local variables, module-level globals, aggregate structure definitions, and imported symbols:

pub(crate) struct ExprEnv {
    pub locals: HashMap<String, AstType>,
    pub globals: HashMap<String, AstType>,
    pub aggregates: HashMap<String, AggregateInfo>,
    pub modules: Vec<String>,
}

ExprEnv::expr_type computes the exact AstType of any AstExpr, ensuring strict type safety before code generation or interpretation.

Sources: crates/tc-semantics/src/expr.rs:46-149, crates/tc-semantics/src/expr.rs:151-158


4. Phrase Declarations and Calls

Users can declare custom English syntax patterns using the Phrase: keyword. These patterns bind custom natural language signatures to strongly-typed functions.

Phrase Matching (PhraseBinding and PhraseMatch)

The phrase engine matches sentences against registered bindings using greedy token matching. Every literal word and captured parameter (<param>) must be fully consumed:

pub struct PhraseBinding {
    pub module: String,
    pub function: String,
    pub vis: Visibility,
    pub pattern: PhrasePattern,
    pub param_names: Vec<String>,
    pub params: Vec<AstType>,
    pub type_params: Vec<String>,
}

match_phrase evaluates all in-scope phrase bindings and prefers patterns with the highest number of literal matching words (score). Type-directed phrase overloading permits the same phrase pattern (e.g. "emit <x>") to resolve to different functions based on the inferred type of argument <x> across modules.

Sources: crates/tc-semantics/src/phrases.rs:13-48, crates/tc-semantics/src/phrases.rs:178-188, crates/tc-semantics/tests/battletest_core_regressions.rs:107-131


5. Integration and Data Flow

The diagrams below bridge Natural Language Space to Code Entity Space, linking natural language syntax constructs directly to their corresponding Rust structs and functions within tc-semantics.

Diagram 1: Statement Frame Lowering Pipeline

graph TD
    A["Natural Language Clause"] --> B["parse_place (crates/tc-semantics/src/place.rs)"]
    A --> C["parse_value (crates/tc-semantics/src/value.rs)"]
    B --> D["SemanticFrame::Assign / Emit / Transfer / Swap (crates/tc-semantics/src/frame.rs)"]
    C --> D
    D --> E["Lowerer::lower_frame (crates/tc-semantics/src/frame.rs)"]
    E --> F["AstStmt Vector (tc-typeck)"]

    subgraph "Code Entity Space"
        B
        C
        D
        E
        F
    end
Loading

Sources: crates/tc-semantics/src/frame.rs:28-108, crates/tc-semantics/src/frame.rs:80-86

Diagram 2: Phrase Declaration and Expression Parsing Data Flow

graph TD
    NL["English Source Sentence"] --> T["tokenize (crates/tc-semantics/src/token.rs)"]
    T --> MP["match_phrase (crates/tc-semantics/src/phrases.rs)"]
    MP --> PB["PhraseMatch (crates/tc-semantics/src/phrases.rs)"]
    PB --> EP["Parser::parse_expression (crates/tc-semantics/src/expr.rs)"]
    EE["ExprEnv (crates/tc-semantics/src/expr.rs)"] --> EP
    EP --> AE["AstExpr (tc-typeck)"]

    subgraph "Code Entity Space"
        T
        MP
        PB
        EP
        EE
        AE
    end
Loading

Sources: crates/tc-semantics/src/expr.rs:46-158, crates/tc-semantics/src/phrases.rs:13-48, crates/tc-semantics/src/phrases.rs:178-188


Clone this wiki locally