AST to IR Transformation
The IR Generator bridges the gap between the language front-end (Phase 1) and backend compilation (Phase 2). It transforms the cognitive AST into a model-agnostic Intermediate Representation (IR).Why IR Matters
The Problem
Without an IR layer, every backend would need to:- Understand the full AST structure
- Resolve cross-references between declarations
- Compute data dependencies between steps
- Handle tool resolution and validation
The Solution
The IR layer provides:- Model Agnosticism: Zero dependencies on Claude, GPT, Gemini, etc.
- Resolved References: All names are linked to their definitions
- DAG Ordering: Steps are topologically sorted by dependencies
- JSON Serializable: Complete programs can be saved and loaded
- Immutable: Once generated, IR nodes are never mutated
IR Node Design
Base IR Node
frozen=True— IR nodes are immutableto_dict()— Full JSON serialization supportnode_type— Runtime type identification- Tuples instead of lists — Enforces immutability
IR Program Structure
The IR Generator
Overview
Visitor Pattern
The IR Generator uses an explicit visitor registry:Declaration Lowering
Persona Example
AST → IR:Flow Lowering with DAG Computation
Key Challenge: Steps may reference each other’s outputs. The IR must order them correctly.DAG Algorithm
The_calculate_execution_dag method:
-
Extract dependencies from step expressions:
-
Build dependency graph:
-
Topological sort (Kahn’s algorithm):
-
Execution levels (for potential parallelism):
Cross-Reference Resolution
The Problem
The Solution
Phase 2 of IR generation resolves all name references:IR Data Structures
IRFlow — Compiled Flow
IRReason — Compiled Reasoning
IRRun — Resolved Execution
Tool Resolution
The IR Generator verifies that all tool references are valid:JSON Serialization
The IR is fully JSON-serializable:Next Steps
Type Checker
See how epistemic types are validated before IR generation
Backend Compilation
Learn how the IR is compiled into LLM-specific prompts
