1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// The `Word::widen` extension-trait method (`src/word.rs`) collides by name with an
// unstable integer method the standard library is adding; a recent stable toolchain
// surfaces this as `unstable_name_collisions`, which `-D warnings` (the CI Clippy
// job) turns into an error. Keleusma's method predates it and every call site
// (`src/vm.rs`, `src/word.rs`) resolves to the trait method as intended. Allow the
// forward-compatibility lint here; revisit (rename, or fully-qualify the calls) if
// and when the standard-library method stabilizes.
//! Keleusma is a Total Functional Stream Processor that compiles
//! to bytecode and runs on a stack-based virtual machine. The crate
//! targets `no_std + alloc` environments and is designed for embedded
//! scripting where definitive worst-case execution time and worst-case
//! memory usage bounds matter.
//!
//! The crate has no built-in standard library. All domain functionality
//! is provided by host-registered native functions through
//! [`vm::Vm::register_fn`], [`vm::Vm::register_native`], and the
//! related entry points. The bundled libraries in [`stddsl`] are
//! examples of the registration pattern, not a closed set.
//!
//! # Quick start
//!
//! ```ignore
//! // Requires the `compile` cargo feature (default on).
//! use keleusma::compiler::compile;
//! use keleusma::lexer::tokenize;
//! use keleusma::parser::parse;
//! use keleusma::vm::{DEFAULT_ARENA_CAPACITY, Vm, VmState};
//! use keleusma::{Arena, Value};
//!
//! let source = r#"
//! fn double(x: Word) -> Word { x * 2 }
//! fn main(n: Word) -> Word { n |> double() }
//! "#;
//!
//! let tokens = tokenize(source).expect("lex");
//! let program = parse(&tokens).expect("parse");
//! let module = compile(&program).expect("compile");
//! let arena = Arena::with_capacity(DEFAULT_ARENA_CAPACITY);
//! let mut vm = Vm::new(module, &arena).expect("verify");
//!
//! match vm.call(&[Value::Int(21)]).unwrap() {
//! VmState::Finished(value) => println!("{:?}", value),
//! _ => unreachable!(),
//! }
//! // prints: Int(42)
//! ```
//!
//! # Cargo features
//!
//! The crate exposes orthogonal feature gates so hosts can strip
//! pipeline stages they do not need.
//!
//! - `compile` (default on): lexer, parser, type checker,
//! monomorphizer, compiler. Drop when the host ships precompiled
//! bytecode.
//! - `verify` (default on): structural verifier and WCET/WCMU
//! resource-bounds pass. With this off, [`vm::Vm::new`] behaves
//! like [`vm::Vm::new_unchecked`].
//! - `floats` (default on): the `Float` surface type, `Value::Float`,
//! `Op::IntToFloat`/`FloatToInt`, and the [`audio_natives`] and
//! [`stddsl::Math`]/[`stddsl::Audio`] bundles. Drop to eliminate the
//! soft-float `compiler_builtins` routines from the runtime image.
//! - `signatures` (default off): Ed25519 module signing and
//! load-time verification.
//! - `shell` (default off): the [`stddsl::Shell`] bundle. Requires
//! `std`.
//! - `sdl3-example` (default off): builds the bundled SDL3 audio
//! piano-roll example. `cmake`-builds SDL3 from source.
//!
//! Seven mutually-exclusive `narrow-word-*`, `narrow-address-*`, and
//! `narrow-float-32` parametric-runtime selectors gate the
//! framing-level upper bound on bytecode widths for hosts that ship
//! only a sub-64-bit `GenericVm` instance.
//!
//! # Further reading
//!
//! The repository's `README.md` and `docs/` knowledge graph describe
//! the language design, execution model, instruction set, wire
//! format, and conservative-verification stance in depth.
extern crate alloc;
/// Address-type abstraction used by the parametric [`vm::GenericVm`]
/// to model the runtime's address width.
/// Runtime values, instructions, the [`Module`] type, and the cost
/// model.
/// Strippable debug metadata: the chunk-local [`debug_meta::DebugPool`]
/// section and its canonical byte encoding. Parallel infrastructure for
/// B29; the foundational data model and serialization, not yet attached
/// to [`bytecode::Chunk`] nor emitted by the compiler.
/// Authenticated encryption of compiled [`Module`]s under the
/// optional `encryption` feature. Implements the V0.2.1 hybrid
/// asymmetric key wrapping (X25519 ECDH plus HKDF-SHA-256 plus
/// AES-256-GCM). Feature-gated because the crypto stack adds
/// meaningful binary footprint for hosts that do not need it.
/// Flat-byte composite representation helpers and the
/// [`flat_value::FlatComposite`] byte buffer for B28's runtime
/// composite-value refactor. A composite value is pure bytes; the
/// field offsets are baked into the access instructions by the
/// compiler, so the buffer carries no layout reference.
/// Arena-resident dynamic strings ([`KString`]) at the host-VM
/// boundary.
/// Type-driven marshalling between host Rust types and runtime
/// [`Value`]s for native function registration.
/// Opaque host-value support: the [`opaque::HostOpaque`] trait and
/// the [`opaque::host_arc`] constructor that produces
/// `Value::Opaque(Arc<dyn HostOpaque>)`.
/// The unsafe API layer for the borrowed host-owned shared-data buffer
/// (B28 item 2). All raw-pointer code in the shared-data path is confined to
/// [`shared_buf::SharedBuf`]; the rest of the runtime works with safe slices.
/// Bundled utility natives. V0.2.0 ships only `println` here; other
/// utilities are host-registered.
/// Layout descriptors for composite Keleusma values. Parallel
/// infrastructure for B28's runtime composite-value
/// representation refactor. Not yet consumed by the runtime.
/// The stack-based virtual machine and its coroutine driver.
/// Wire-format encoding and decoding of compiled [`Module`]s.
/// Word-type abstraction used by the parametric [`vm::GenericVm`]
/// to model the runtime's word width.
// Audio natives use floating-point arithmetic throughout (note
// frequency, phase, filter coefficients) and are only useful on
// hosts that enable the `floats` feature. Without floats the
// bundle's native signatures cannot satisfy the `IntoNativeFn`
// trait bounds because `KeleusmaType for f64` is not in scope.
/// Bundled audio-DSP natives (`audio::midi_to_freq`,
/// `audio::db_to_linear`, and similar). Requires the `floats` feature.
/// Parametric floating-point trait used by [`vm::GenericVm`]. The
/// trait and its `f32` and `f64` impls are always compiled so the
/// generic shape carries a `Float` type parameter regardless of the
/// `floats` feature; the floating-point variants of [`Value`] and the
/// floating-point opcodes remain gated by `floats`.
/// Bundled standard-library DSLs ([`stddsl::Math`], [`stddsl::Audio`],
/// [`stddsl::Shell`]) registered through [`vm::Vm::register_library`].
// Compile-pipeline modules. Gated behind the `compile` feature
// (default on). With the feature off, the runtime accepts only
// precompiled bytecode through `Module::from_bytes` and
// `Vm::view_bytes_zero_copy`. Hosts that ship precompiled
// bytecode for the smallest possible runtime binary leave this
// feature off.
/// Abstract syntax tree node definitions.
/// AST to bytecode emission.
/// Closed-signed-interval lattice over `i64` used by the type
/// checker and the refinement-elision pass.
/// Compile-time layout pass. Bridges AST type expressions to
/// the [`value_layout::LayoutDescriptor`] byte-layout
/// descriptors used by subsequent B28 phases for composite
/// allocation and field access.
/// Source-text tokenisation (lexer).
/// Compile-time monomorphization of generic functions, structs, and
/// enums.
/// Token-to-AST recursive-descent parser.
/// Target descriptor (word/address/float widths, endianness) used by
/// [`compiler::compile_with_target`] for cross-architecture
/// portability.
/// Token definitions and keyword recognition.
/// Hindley-Milner type checker with generics, traits, and bounds.
/// Visitor and mutable-visitor traits with default walk methods over
/// the AST.
/// Canonical zero value per type and lowest-valid resolution for
/// refined newtypes. Parallel infrastructure for B35's Partial
/// Operation Handling; native code generation is the intended
/// consumer. Not yet consumed by the runtime.
// Verifier modules. Gated behind the `verify` feature (default
// on). With the feature off, `Vm::new` skips structural and
// resource-bound verification and behaves like
// `Vm::new_unchecked` from the caller's perspective. The
// compiler's call to the verifier at the end of
// `compile_with_target` is likewise gated; with the feature off
// the compiler leaves the bytecode header's WCET and WCMU
// fields at 0 (auto).
/// Abstract-interpretation text-size lattice used by the WCMU pass
/// for dynamic-text allocations.
/// Structural verifier plus WCET and WCMU resource-bounds pass.
/// Phase 0 spike for the A.2.1 typed operand-stack verifier pass. Not yet
/// wired into [`verify`]; a standalone proof of concept for the abstract
/// operand-stack domain and baked-offset validation.
pub use ;
pub use KString;
pub use Address;
pub use ;
pub use Float;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use Word;