Expand description
Semantic IR for grammar-embedded predicates and actions.
ANTLR grammars embed target-language semantic predicates and actions that a metadata-first runtime cannot execute directly (issue #9). This module defines the small data-driven language those snippets are translated into: heuristic template matching at codegen time, hand-written tables, and (long term) a real Rust target all lower to the same IR, and the runtime evaluates only the IR.
Design constraints, in priority order:
- Prediction-safe: predicates run speculatively inside adaptive
prediction, possibly many times on abandoned paths.
PExprtherefore has no mutating node — effects exist only inAStmt, which the runtime executes on committed paths (or transactionally for member-state speculation). - Allocation-free on the hot path: expression storage is a flat arena
indexed by
ExprId, and text comparisons resolve borrowed&stroperands without materializingStrings (seeeval_text_cmp). - Absence is explicit: recognizer queries that can fail (missing
lookahead token, absent context child, no rule argument) produce
Value::Null, and comparison semantics over Null are fixed here so every producer of IR agrees on them.
§Null semantics
Eqis true iff both sides are present and equal, or both are Null.Neis the negation ofEq.- Ordering comparisons (
Lt,Le,Gt,Ge) with any Null side are false. - Arithmetic with any Null operand is Null; division/modulo by zero is Null.
- Truthiness: Null is false,
Bool(b)isb,Int(i)isi != 0.
These rules are load-bearing: {...}? lookahead-text predicates must fail
when the token is absent (Eq(Null, "text") == false), while
context-child text guards must pass when the child is absent
(Ne(Null, "text") == true). Predicates that are non-restrictive when a
value is absent (rule arguments) compose PExpr::IsNull with Or.
Structs§
- ExprId
- Index of an expression node inside a
SemIrarena. - HookId
- Opaque identifier of an externally implemented hook.
- SemIr
- Flat expression/statement arena with an interned string pool.
- StmtId
- Index of a statement node inside a
SemIrarena. - StrId
- Index of an interned string inside a
SemIrarena.
Enums§
- AStmt
- Effectful action statement node.
- ArithOp
- Arithmetic operator for
PExpr::Arith. - CmpOp
- Comparison operator for
PExpr::Cmp. - PExpr
- Pure predicate expression node.
- Value
- Evaluation result of a non-text expression.
Traits§
- ActContext
- Mutations the action evaluator needs, on top of predicate queries.
- Pred
Context - Recognizer-state queries the predicate evaluator needs.