.axon files through multiple representations before generating backend-specific prompts for LLMs.
Pipeline Overview
1
Lexer — Source → Tokens
Character stream becomes structured tokens
2
Parser — Tokens → AST
Token stream becomes cognitive syntax tree
3
Type Checker — Semantic Validation
AST validated for type correctness
4
IR Generator — AST → IR
Cognitive AST lowered to intermediate representation
5
Backend — IR → Prompts
IR compiled to model-specific prompts
6
Runtime — Execution + Validation
Prompts executed, validated, traced
Stage 1: Lexer (Tokenization)
Purpose
Convert raw.axon source text into a stream of tokens — the atomic units of the language.
Implementation
Location:
Type: Hand-written, single-pass character scanner
/axon/compiler/lexer.py:1Type: Hand-written, single-pass character scanner
lexer.py
Token Types
The lexer recognizes 35 keywords (cognitive primitives) and various symbols:Features
Comment Stripping
Removes
// line comments and /* */ block commentsString Escapes
Handles
\n, \t, \", \\ in string literalsKeyword Discrimination
Distinguishes
flow (keyword) from flow_name (identifier)Location Tracking
Tracks line and column for error messages
Example
Input source
Output tokens
Stage 2: Parser (AST Construction)
Purpose
Transform the flat token stream into a hierarchical Abstract Syntax Tree (AST) representing the program’s cognitive structure.Implementation
Location:
Algorithm: Recursive descent parser with one method per grammar rule
/axon/compiler/parser.py:1Algorithm: Recursive descent parser with one method per grammar rule
parser.py
AST Node Types
Each cognitive primitive has a corresponding AST node:- Declarations
- Statements
- Expressions
PersonaDefinitionContextDefinitionAnchorConstraintFlowDefinitionToolDefinitionMemoryDefinitionTypeDefinition
Example AST
Input
Output AST (simplified)
Stage 3: Type Checker (Semantic Validation)
Purpose
Validate the semantic correctness of the program using AXON’s epistemic type system.Implementation
Location:
Type System: Epistemic partial order lattice
/axon/compiler/type_checker.py:1Type System: Epistemic partial order lattice
type_checker.py
Validation Rules
1
Symbol Table Construction
Build registry of all personas, contexts, flows, anchors, types
2
Type Compatibility
Check
Opinion ≰ FactualClaim and other subsumption rules3
Reference Resolution
Ensure
run Analyze(doc) references a defined flow Analyze4
Uncertainty Propagation
Track
Uncertainty taint through operations5
Range Validation
Verify
RiskScore(0.0..1.0) only accepts values in rangeType Errors
The type checker returns a list ofAxonTypeError objects:
Stage 4: IR Generator (Lowering)
Purpose
Lower the cognitive AST into the AXON Intermediate Representation (IR) — a JSON-serializable format ready for backend compilation.Implementation
Location:
Pattern: Visitor pattern with explicit dispatch
/axon/compiler/ir_generator.py:1Pattern: Visitor pattern with explicit dispatch
ir_generator.py
IR Node Types
The IR uses simplified, backend-agnostic nodes:ir_nodes.py
Cross-Reference Resolution
The IR generator links symbolic references:Source
Generated IR
Why IR? The IR decouples language design from backend implementation. New backends (e.g., Gemini, Llama) only need to compile IR, not parse AXON source.
Stage 5: Backend (Prompt Compilation)
Purpose
Compile the backend-agnostic IR into model-specific prompts for LLM providers.Supported Backends
Anthropic
Claude 3.x (Opus, Sonnet, Haiku)
OpenAI
GPT-4, GPT-4 Turbo, GPT-3.5
Gemini
Gemini 1.5 Pro, Flash
Ollama
Local models (Llama, Mistral)
Backend Interface
Location:
/axon/backends/base_backend.py:1base_backend.py
Example: Anthropic Backend
anthropic.py
Stage 6: Runtime (Execution + Validation)
Purpose
Execute the compiled prompts, validate outputs, handle failures, and trace execution.Runtime Components
- Executor
- SemanticValidator
- RetryEngine
- Tracer
Location:
/axon/runtime/executor.py:1Orchestrates flow execution:- Resolves step dependencies (DAG)
- Invokes model with compiled prompts
- Passes outputs between steps
Execution Flow
CLI Usage
Check (Lex + Parse + Type Check)
Compile (Generate IR)
Run (End-to-End)
Pipeline Comparison
Next Steps
Cognitive Primitives
Learn what gets compiled
Type System
Understand type checking
Error Handling
See runtime behavior
CLI Reference
Use the compiler tools

