ternlang_codegen/lib.rs
1// SPDX-License-Identifier: LGPL-3.0-or-later
2// Ternlang — RFI-IRFOS Ternary Intelligence Stack
3// Copyright (C) 2026 RFI-IRFOS
4// Open-core compiler. See LICENSE-LGPL in the repository root.
5
6//! ternlang-codegen — AST → C transpiler backend.
7//!
8//! Converts a ternlang `Program` (produced by `ternlang-core::Parser`) into
9//! a valid, self-contained C source file that can be compiled with any C11
10//! compiler.
11//!
12//! # Ternary representation
13//! Trits are represented as `int8_t` with values `-1`, `0`, `+1`.
14//! The generated file includes a small header of inline trit primitives so it
15//! has no external dependencies beyond `<stdint.h>` and `<stdio.h>`.
16//!
17//! # Usage
18//! ```rust
19//! use ternlang_codegen::CTranspiler;
20//! use ternlang_core::{Parser, StdlibLoader};
21//!
22//! let src = r#"fn main() -> trit { return consensus(1, -1); }"#;
23//! let mut parser = ternlang_core::Parser::new(src);
24//! let mut prog = parser.parse_program().unwrap();
25//! StdlibLoader::resolve(&mut prog);
26//!
27//! let c_src = CTranspiler::new().emit(&prog);
28//! println!("{c_src}");
29//! ```
30
31
32//! C transpiler backend for the Ternlang compiler — emits C source from the Ternlang AST for native cross-compilation targets.
33use ternlang_core::ast::*;