Rúnar Rust Compiler
Alternative Rúnar compiler implemented in Rust.
Status
| Phase | Description | Status |
|---|---|---|
| Phase 1 | IR consumer: accepts canonical ANF IR JSON, performs stack lowering and emission (Passes 5-6). | Implemented |
| Phase 2 | Full frontend: parses .runar.ts source files directly (Passes 1-4), produces canonical ANF IR. |
Implemented |
Phase 1 validates that the Rust implementation can produce identical Bitcoin Script from the same ANF IR as the reference compiler. Phase 2 adds an independent frontend that must produce byte-identical ANF IR.
Architecture
Phase 1: IR Consumer
ANF IR (JSON) --> [Stack Lower] --> [Peephole] --> [Emit] --> Bitcoin Script
Rust pass 5 Optimize Rust pass 6
The Rust compiler reads canonical ANF IR JSON and performs stack scheduling and opcode emission.
Phase 2: Full Frontend
.runar.ts --> [Parse] --> [Validate] --> [Typecheck] --> [ANF Lower]
SWC parser Rust pass 2 Rust pass 3 Rust pass 4
frontend
|
v
ANF IR (JSON)
|
v
[Stack Lower] --> [Peephole] --> [Emit] --> Bitcoin Script
Rust pass 5 Optimize Rust pass 6
The parsing frontend uses SWC (Speedy Web Compiler) for parsing .runar.ts files. SWC is a Rust-native TypeScript/JavaScript parser that provides a full AST. Since SWC is already written in Rust, it integrates naturally as a library dependency.
Why SWC instead of tree-sitter or a custom parser? SWC provides a typed Rust AST rather than a generic CST, reducing the amount of manual tree-walking needed. It is also the fastest TypeScript parser available, which matters for large projects. The Rust ecosystem already depends heavily on SWC for tooling (Next.js, Parcel, Deno), so it is well-maintained.
Multi-format source files (.runar.sol, .runar.move, .runar.rs) are parsed by hand-written recursive descent parsers that produce the same Rúnar AST.
Dedicated Codegen Modules
src/codegen/ec.rs— EC point operations (ecAdd,ecMul,ecMulGen,ecNegate,ecOnCurve, etc.)src/codegen/slh_dsa.rs— SLH-DSA (SPHINCS+) signature verificationsrc/codegen/optimizer.rs— Peephole optimizer (runs on Stack IR between stack lowering and emit)
Building
# The binary is at target/release/runar-compiler-rust
Prerequisites
- Rust (2021 edition)
- Cargo
Running
Phase 1: IR Consumer Mode
# Compile from ANF IR to Bitcoin Script (full artifact JSON)
# Output only the script hex
# Output only the script ASM
# Write output to a file (-o is shorthand for --output)
Phase 2: Full Compilation
# Full compilation from source (outputs artifact JSON)
# Output only hex
# Dump ANF IR for conformance checking
# Write output to a file (-o is shorthand for --output)
Conformance Testing
The Rust compiler must pass the same conformance suite as the TypeScript reference compiler.
For each test case in conformance/tests/:
- Read
*.runar.tssource as input. - Run the full pipeline (Passes 1-6).
- Compare script hex output with
expected-script.hex(string equality). - If
expected-ir.jsonexists, also compile from IR and verify the IR-compiled script matches the source-compiled script.
# Run conformance from repo root
# Or directly
Testing
Unit tests cover each pass independently, using synthetic IR inputs and asserting structural properties of the output. Integration tests in tests/compiler_tests.rs run the full pipeline against conformance test cases. Multi-format tests in tests/multiformat_tests.rs verify .runar.sol, .runar.move, and .runar.rs parsing.