Skip to main content
AXON is a compiled language with a multi-stage transformation pipeline. Unlike interpreted languages that execute source directly, AXON transforms .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: /axon/compiler/lexer.py:1
Type: 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 comments

String Escapes

Handles \n, \t, \", \\ in string literals

Keyword 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: /axon/compiler/parser.py:1
Algorithm: Recursive descent parser with one method per grammar rule
parser.py

AST Node Types

Each cognitive primitive has a corresponding AST node:
  • PersonaDefinition
  • ContextDefinition
  • AnchorConstraint
  • FlowDefinition
  • ToolDefinition
  • MemoryDefinition
  • TypeDefinition

Example AST

Input
Output AST (simplified)
Cognitive AST: Unlike traditional ASTs with mechanical nodes (e.g., BinaryExpression), AXON’s AST uses semantic nodes (ReasonChain, ProbeDirective) that map directly to cognitive operations.

Stage 3: Type Checker (Semantic Validation)

Purpose

Validate the semantic correctness of the program using AXON’s epistemic type system.

Implementation

Location: /axon/compiler/type_checker.py:1
Type 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 rules
3

Reference Resolution

Ensure run Analyze(doc) references a defined flow Analyze
4

Uncertainty Propagation

Track Uncertainty taint through operations
5

Range Validation

Verify RiskScore(0.0..1.0) only accepts values in range

Type Errors

The type checker returns a list of AxonTypeError objects:
Compile-Time Safety: Type errors prevent compilation. AXON guarantees that well-typed programs satisfy epistemic constraints.

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: /axon/compiler/ir_generator.py:1
Pattern: 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:1
base_backend.py

Example: Anthropic Backend

anthropic.py
Backend Customization: Each backend optimizes prompts for its model’s strengths. Anthropic uses XML tags, OpenAI prefers JSON, Gemini uses structured examples.

Stage 6: Runtime (Execution + Validation)

Purpose

Execute the compiled prompts, validate outputs, handle failures, and trace execution.

Runtime Components

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)

Outputs type errors without executing.

Compile (Generate IR)

Produces JSON IR for inspection or caching.

Run (End-to-End)

Executes with chosen backend and saves trace.

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