Skip to main content

truthlinked_axiom_compiler/
lib.rs

1//! Compiler pipeline for TruthLinked Axiom cell source files.
2//!
3//! The compiler turns `.cell` source into deterministic Axiom bytecode plus a
4//! manifest describing declared reads, writes, and commutative storage keys.
5//! Downstream tooling relies on this crate to produce stable artifacts for cell
6//! deployment, local simulation, and conflict-aware scheduling.
7
8pub mod ast;
9pub mod lexer;
10pub mod lower;
11pub mod manifest;
12pub mod parser;
13pub mod typeck;
14
15use std::path::Path;
16
17#[derive(Debug, thiserror::Error)]
18pub enum CompileError {
19    #[error("lex error: {0}")]
20    Lex(#[from] lexer::LexError),
21    #[error("parse error: {0}")]
22    Parse(#[from] parser::ParseError),
23    #[error("type error: {0}")]
24    Type(#[from] typeck::TypeError),
25    #[error("io error: {0}")]
26    Io(#[from] std::io::Error),
27}
28
29pub struct CompileOutput {
30    pub bytecode: Vec<u8>,
31    pub manifest: manifest::Manifest,
32}
33
34pub fn compile(src: &str) -> Result<CompileOutput, CompileError> {
35    let tokens = lexer::lex(src)?;
36    let ast = parser::parse(tokens)?;
37    typeck::check(&ast)?;
38    let ir = lower::lower(&ast);
39    let alloc = truthlinked_axiom_sdk::regalloc::allocate(&ir);
40    let bytecode = truthlinked_axiom_sdk::codegen::emit(&ir, &alloc);
41    let manifest = manifest::generate(&ast);
42    Ok(CompileOutput { bytecode, manifest })
43}
44
45pub fn compile_file(path: &Path) -> Result<CompileOutput, CompileError> {
46    let src = std::fs::read_to_string(path)?;
47    compile(&src)
48}