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
//! Ahead-of-time Nybl-to-Rust transpiler.
//!
//! [`transpile`] parses a Nybl program and emits readable Rust source that
//! links against `nybl-lang` for runtime values, operators, built-ins, host
//! calls, and diagnostics. The generated source can be compiled as a native
//! binary or included as a module in a larger Rust application.
//!
//! The AOT engine implements the same language semantics as the tree-walker
//! and bytecode VM, including methods, closures, string interpolation,
//! indexed and field mutation, transactional `ref` parameters, structs and
//! enums, pattern matching, `Result`, lazy iteration, and all forms of `use`.
//! A three-engine differential suite exercises their observable behavior
//! together.
//!
//! # Basic transpilation
//!
//! ```
//! use nybl_compile::{Options, transpile};
//!
//! let rust_source = transpile(
//! r#"let answer = 6 * 7
//! print("answer = {answer}")"#,
//! &Options::default(),
//! ).unwrap();
//! assert!(rust_source.contains("fn main()"));
//! ```
//!
//! `use` statements need a compile-time [`ModuleResolver`]. For bundled
//! stdlib modules, callers normally delegate that resolver to
//! `nybl::stdlib::resolve`; for in-memory programs, [`modules_from_map`] is
//! convenient:
//!
//! ```
//! use nybl_compile::{Options, modules_from_map, transpile};
//!
//! let rust_source = transpile(
//! "use config.{answer}\nprint(answer)",
//! &Options {
//! module_resolver: Some(modules_from_map([
//! ("config", "let answer = 42"),
//! ])),
//! ..Options::default()
//! },
//! ).unwrap();
//! assert!(rust_source.contains("42"));
//! ```
//!
//! With the default options, the output contains `fn main()` and constructs
//! `nybl_sys::StandardHost`. Set [`Options::emit_main`] and
//! [`Options::use_nybl_sys`] to `false` for a library surface driven by a
//! custom [`nybl::NyblHost`].
//!
//! # Sandboxed output
//!
//! [`Options::sandbox`] adds [`nybl::NyblLimits`] accounting and host tick
//! callbacks to generated code. With `sandbox: true` and `emit_main: false`,
//! direct root-level `pub fn` declarations also generate a persistent
//! `NyblInstance` API with `load`, `entry_points`, `call`, and `call_value`.
//! Globals, modules, functions, callbacks, types, methods, and random-number
//! state remain live between calls.
//!
//! Rust calls into the generated `NyblInstance` remain value-only and reject
//! ref-bearing entries or callbacks before execution. A value-only public
//! entry can use reference parameters internally. Generated ref calls preserve
//! the language's target validation, copy-in/copy-out staging, atomic commit,
//! error rollback, forwarding, and mutating receiver behavior. See the
//! [reference-parameters
//! guide](https://nybl-lang.com/docs/functions/reference-parameters/).
//! Generated user methods also preserve `ref self` receiver write-back and
//! rollback; ordinary `self` receivers are read-only.
//!
//! Unsandboxed library output exposes the one-shot `run` API and deliberately
//! omits accounting overhead. Do not run untrusted programs through that
//! mode.
//!
//! # Output shaping
//!
//! [`Options::module_name`] wraps the generated items in a public Rust module,
//! which lets one crate include several transpiled programs without name
//! collisions. [`transpile_ast`] accepts an already parsed Nybl AST for build
//! tools that perform their own parse and diagnostic pass.
//!
//! See the [embedding guide](https://nybl-lang.com/docs/embedding/) and
//! [stateful instance guide](https://nybl-lang.com/docs/embedding/instances/)
//! for generated signatures, dependency setup, limits, and lifecycle rules.
use RefCell;
use Rc;
use NyblError;
use Stmt;
/// A compile-time module resolver. The AOT runs this eagerly for
/// every `use` it encounters, threading the entire module graph
/// into the generated Rust. Same contract as
/// [`nybl::NyblHost::resolve_module`]: `None` = not handled,
/// `Some(Ok(source))` = module source text,
/// `Some(Err(_))` = resolver failure.
///
/// Wrapped as `Rc<RefCell<..>>` so `Options` stays `Clone` while
/// the callback keeps `FnMut` freedom.
pub type ModuleResolver =
;
/// Build a [`ModuleResolver`] from an in-memory name→source map.
/// Convenience for tests and simple embedders.
/// Options that control the shape of the emitted Rust.
///
/// `Debug` is skipped — `module_resolver` is a trait object that
/// has no useful debug representation. If you need to diff options
/// in tests, compare specific fields directly.
/// Parse Nybl source and emit the equivalent Rust source.
///
/// The caller is responsible for writing the returned string to a
/// crate (or `src/main.rs`) and invoking `cargo` or `rustc` on it.
/// The result depends on:
///
/// - `nybl-lang` (`nybl` crate) — for `Value`, `ops`, `builtins`, …
/// - `nybl-sys` — only if [`Options::use_nybl_sys`] is true.
/// Lower-level entry point: emit Rust from an already-parsed AST.