1
mod context;
2
mod emit;
3
pub(crate) mod expr;
4
mod layout;
5
mod native;
6
pub mod special;
7

            
8
use tracing::debug;
9

            
10
use crate::ast::{Expr, Program, WasmType};
11
use crate::error::{Error, Result};
12
use crate::host_fn::HostFnSpec;
13
use crate::runtime::SymbolTable;
14

            
15
use context::CompileContext;
16
use emit::FunctionEmitter;
17

            
18
/// Selects which export shape the compiler emits.
19
///
20
/// `Script` keeps the entity-script surface (`should_apply` + `process`
21
/// exports, env.* imports) the script executor consumes. `Eval` emits a
22
/// `nomi-eval` export that runs the program and returns the final value
23
/// via the function's `(ref null any)` return slot; the host decodes it
24
/// with `scripting::runtime::decode_eval_result`.
25
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26
pub enum CompileMode {
27
    Script,
28
    Eval,
29
}
30

            
31
pub struct Compiler {
32
    host_fns: Vec<HostFnSpec>,
33
}
34

            
35
impl Compiler {
36
    #[must_use]
37
59451
    pub fn new() -> Self {
38
59451
        Self {
39
59451
            host_fns: Vec::new(),
40
59451
        }
41
59451
    }
42

            
43
    /// Builds a compiler that recognises the given host fn names in eval-mode
44
    /// programs. Each spec gets a wasm import declared in the module and an
45
    /// entry the native dispatcher consults when emitting calls. Has no effect
46
    /// in `Script` mode (entity-script bytecode imports `env.*`, not `nomi.*`).
47
    #[must_use]
48
13329
    pub fn with_host_fns(host_fns: Vec<HostFnSpec>) -> Self {
49
13329
        Self { host_fns }
50
13329
    }
51

            
52
82023
    pub fn compile(&mut self, program: &Program, symbols: &mut SymbolTable) -> Result<Vec<u8>> {
53
82023
        self.compile_with_mode(program, symbols, CompileMode::Script)
54
82023
    }
55

            
56
82097
    pub fn compile_with_mode(
57
82097
        &mut self,
58
82097
        program: &Program,
59
82097
        symbols: &mut SymbolTable,
60
82097
        mode: CompileMode,
61
82097
    ) -> Result<Vec<u8>> {
62
82097
        debug!(expr_count = program.exprs.len(), ?mode, "compilation start");
63
82097
        match mode {
64
82093
            CompileMode::Script => self.compile_script(program, symbols),
65
4
            CompileMode::Eval => self.compile_eval(program, symbols),
66
        }
67
82097
    }
68

            
69
82093
    fn compile_script(&mut self, program: &Program, symbols: &mut SymbolTable) -> Result<Vec<u8>> {
70
82093
        let mut ctx = CompileContext::new()?;
71

            
72
        // Snapshot how many helpers (gcd / ratio_* / commodity_* / pair_*)
73
        // were queued during context bootstrap; these correspond to the
74
        // function-section slots between `should_apply` and `process`,
75
        // so their bodies must drain *with* `should_apply`'s emit. User
76
        // code inside `process` may queue more helpers (e.g. a real
77
        // wasm fn for a `(lambda ...)` value); those slots land *after*
78
        // `process` and have to drain *with* `process`'s emit so the
79
        // code-section ordering matches the function-section.
80
82093
        let bootstrap_helper_count = ctx.pending_helper_count();
81

            
82
82093
        let mut process = FunctionEmitter::new();
83

            
84
        // Wrap the body in the Tier 3 boundary `try_table` so an uncaught
85
        // `(error)` throw bridges to `__nomi_raise` (ADR-0026). `process`
86
        // returns void.
87
82093
        ctx.emit_boundary_wrapper(&mut process, None, |ctx, emit| {
88
            // output_base = get_output_offset()
89
82093
            emit.call(ctx.ids.get_output_offset()?);
90
82093
            emit.local_set(expr::LOCAL_OUTPUT_BASE);
91
82093
            expr::compile_program(ctx, emit, symbols, program)
92
82093
        })?;
93
71891
        process.end();
94

            
95
71891
        let process_locals = ctx.build_locals_declaration();
96
71891
        ctx.reset_locals();
97

            
98
71891
        let should_apply = self.build_should_apply(&mut ctx, symbols)?;
99
71891
        ctx.add_should_apply(should_apply, bootstrap_helper_count);
100
71891
        ctx.add_process(process.finish(&process_locals));
101

            
102
71891
        let wasm = ctx.finish();
103
71891
        debug!(wasm_size = wasm.len(), "compilation complete");
104
71891
        Ok(wasm)
105
82093
    }
106

            
107
4
    fn compile_eval(&mut self, program: &Program, symbols: &mut SymbolTable) -> Result<Vec<u8>> {
108
4
        let (wasm, _ty) = self.compile_eval_with_type(program, symbols)?;
109
4
        Ok(wasm)
110
4
    }
111

            
112
    /// Compiles `program` in eval mode and returns the wasm bytes paired
113
    /// with the form's final result type. `None` for empty programs /
114
    /// definition-only forms (nomi-eval returns `ref.null any`); `Some(ty)`
115
    /// for any other terminator. Hosts decode the returned anyref via the
116
    /// type hint — see `scripting::runtime::decode_eval_result`.
117
137025
    pub fn compile_eval_with_type(
118
137025
        &mut self,
119
137025
        program: &Program,
120
137025
        symbols: &mut SymbolTable,
121
137025
    ) -> Result<(Vec<u8>, Option<WasmType>)> {
122
137025
        let mut ctx = CompileContext::new_eval_with_host_fns(&self.host_fns)?;
123
137025
        let mut emit = FunctionEmitter::new();
124

            
125
        // `nomi-eval` returns anyref. The boundary wrapper bridges an
126
        // uncaught `(error)` to `__nomi_raise` (ADR-0026). The body's
127
        // final result type is computed inside the closure and surfaced
128
        // out for the host's `decode_eval_result` hint.
129
137025
        let mut result_ty: Option<WasmType> = None;
130
137025
        ctx.emit_boundary_wrapper(&mut emit, Some(WasmType::AnyRef), |ctx, emit| {
131
137025
            if program.exprs.is_empty() {
132
69
                emit.ref_null_any();
133
69
                return Ok(());
134
136956
            }
135
136956
            let last_idx = program.exprs.len() - 1;
136
136956
            for expr in &program.exprs[..last_idx] {
137
544
                expr::compile_for_effect(ctx, emit, symbols, expr)?;
138
            }
139
136956
            let last = &program.exprs[last_idx];
140
136956
            if is_definition_form(last) {
141
476
                expr::compile_for_effect(ctx, emit, symbols, last)?;
142
476
                emit.ref_null_any();
143
            } else {
144
136480
                let ty = expr::compile_for_stack(ctx, emit, symbols, last)?;
145
135119
                emit_to_anyref(emit, ty);
146
135119
                result_ty = Some(ty);
147
            }
148
135595
            Ok(())
149
137025
        })?;
150
135664
        emit.end();
151

            
152
135664
        let locals = ctx.build_locals_declaration();
153
135664
        ctx.reset_locals();
154
135664
        ctx.add_nomi_eval(emit.finish(&locals));
155

            
156
135664
        let wasm = ctx.finish();
157
135664
        debug!(wasm_size = wasm.len(), "eval compilation complete");
158
135664
        Ok((wasm, result_ty))
159
137025
    }
160

            
161
71891
    fn build_should_apply(
162
71891
        &self,
163
71891
        ctx: &mut CompileContext,
164
71891
        symbols: &mut SymbolTable,
165
71891
    ) -> Result<wasm_encoder::Function> {
166
71891
        let body = symbols
167
71891
            .lookup("SHOULD-APPLY")
168
71891
            .and_then(|s| s.function().cloned());
169

            
170
1160
        let Some(Expr::Lambda(params, body)) = body else {
171
70731
            return Ok(CompileContext::default_should_apply());
172
        };
173

            
174
1160
        if !params.required.is_empty() {
175
            return Err(Error::Compile(
176
                "SHOULD-APPLY must take no parameters".to_string(),
177
            ));
178
1160
        }
179

            
180
1160
        debug!("compiling custom should-apply");
181
1160
        let mut emit = FunctionEmitter::new();
182

            
183
        // Boundary wrapper bridges an uncaught `(error)` in the body to
184
        // `__nomi_raise` (ADR-0026). `should_apply` returns i32.
185
1160
        ctx.emit_boundary_wrapper(&mut emit, Some(WasmType::I32), |ctx, emit| {
186
1160
            let ty = expr::compile_for_stack(ctx, emit, symbols, &body)?;
187
1160
            match ty {
188
                // Bool is the natural should-apply result (a predicate); I32
189
                // (a raw count used as truthy) is also accepted. Both are
190
                // i32-repr, so the i32 return passes through unchanged.
191
1160
                WasmType::I32 | WasmType::Bool => Ok(()),
192
                WasmType::Ratio => {
193
                    emit.struct_get(ctx.ids.ty_ratio, 0);
194
                    emit.i64_const(0);
195
                    emit.i64_ne();
196
                    Ok(())
197
                }
198
                _ => Err(Error::Compile(
199
                    "SHOULD-APPLY must return a boolean or numeric value".to_string(),
200
                )),
201
            }
202
1160
        })?;
203
1160
        emit.end();
204

            
205
1160
        let locals = ctx.build_locals_declaration();
206
1160
        ctx.reset_locals();
207
1160
        Ok(emit.finish(&locals))
208
71891
    }
209
}
210

            
211
/// Promotes the form's final value (already on the wasm stack as its
212
/// declared `WasmType`) to an anyref-subtype so it satisfies nomi-eval's
213
/// `(ref null any)` return slot. Reference-typed values (ratio_ref,
214
/// commodity_ref, i8_array, pair_ref, entity_ref) are anyref subtypes
215
/// already; primitive `i32` needs `ref.i31` boxing.
216
135119
fn emit_to_anyref(emit: &mut FunctionEmitter, ty: WasmType) {
217
135119
    match ty {
218
        // Both i32-repr value types box into an `(ref i31)`.
219
36451
        WasmType::I32 | WasmType::Bool => emit.ref_i31(),
220
        WasmType::Ratio
221
        | WasmType::Commodity
222
        | WasmType::StringRef
223
        | WasmType::PairRef(_)
224
        | WasmType::EntityRef(_)
225
        | WasmType::Closure(_)
226
98668
        | WasmType::AnyRef => {}
227
    }
228
135119
}
229

            
230
136956
fn is_definition_form(expr: &Expr) -> bool {
231
136956
    let Expr::List(elems) = expr else {
232
886
        return false;
233
    };
234
136070
    let Some(Expr::Symbol(name)) = elems.first() else {
235
        return false;
236
    };
237
476
    matches!(
238
136070
        name.as_str(),
239
136070
        "DEFUN" | "DEFVAR" | "DEFMACRO" | "DEFPARAMETER" | "DEFSTRUCT"
240
    )
241
136956
}
242

            
243
impl Default for Compiler {
244
    fn default() -> Self {
245
        Self::new()
246
    }
247
}
248

            
249
#[cfg(test)]
250
mod tests {
251
    use super::*;
252
    use crate::ast::Expr;
253
    use crate::runtime::{Symbol, SymbolKind};
254

            
255
    #[test]
256
1
    fn test_compile_empty_program() {
257
1
        let program = Program::default();
258
1
        let mut compiler = Compiler::new();
259
1
        let wasm = compiler.compile(&program, &mut SymbolTable::new()).unwrap();
260
1
        assert!(!wasm.is_empty());
261
1
        assert_eq!(&wasm[0..4], b"\0asm");
262
1
    }
263

            
264
    #[test]
265
1
    fn test_compile_eval_empty_program_emits_capture_nil() {
266
1
        let program = Program::default();
267
1
        let mut compiler = Compiler::new();
268
1
        let wasm = compiler
269
1
            .compile_with_mode(&program, &mut SymbolTable::new(), CompileMode::Eval)
270
1
            .unwrap();
271
1
        assert!(!wasm.is_empty());
272
1
        assert_eq!(&wasm[0..4], b"\0asm");
273
1
    }
274

            
275
    #[test]
276
1
    fn test_compile_eval_integer_literal() {
277
1
        let program = Program::new(vec![Expr::Number(num_rational::Ratio::from_integer(7))]);
278
1
        let mut compiler = Compiler::new();
279
1
        let wasm = compiler
280
1
            .compile_with_mode(&program, &mut SymbolTable::new(), CompileMode::Eval)
281
1
            .unwrap();
282
1
        assert!(!wasm.is_empty());
283
1
    }
284

            
285
    #[test]
286
1
    fn test_compile_eval_arithmetic() {
287
1
        let program = Program::new(vec![Expr::List(vec![
288
1
            Expr::Symbol("+".into()),
289
1
            Expr::Number(num_rational::Ratio::from_integer(1)),
290
1
            Expr::Number(num_rational::Ratio::from_integer(2)),
291
1
        ])]);
292
1
        let mut compiler = Compiler::new();
293
1
        let mut symbols = SymbolTable::with_builtins();
294
1
        let wasm = compiler
295
1
            .compile_with_mode(&program, &mut symbols, CompileMode::Eval)
296
1
            .unwrap();
297
1
        assert!(!wasm.is_empty());
298
1
    }
299

            
300
    #[test]
301
1
    fn test_compile_default_uses_script_mode() {
302
1
        let program = Program::new(vec![Expr::Bool(true)]);
303
1
        let mut compiler = Compiler::new();
304
1
        let mut symbols = SymbolTable::new();
305
1
        let default_bytes = compiler.compile(&program, &mut symbols).unwrap();
306
1
        let mut symbols = SymbolTable::new();
307
1
        let explicit_bytes = compiler
308
1
            .compile_with_mode(&program, &mut symbols, CompileMode::Script)
309
1
            .unwrap();
310
1
        assert_eq!(default_bytes, explicit_bytes);
311
1
    }
312

            
313
    #[test]
314
1
    fn test_compile_eval_and_script_produce_distinct_bytes() {
315
1
        let program = Program::new(vec![Expr::Number(num_rational::Ratio::from_integer(1))]);
316
1
        let mut compiler = Compiler::new();
317
1
        let mut s1 = SymbolTable::new();
318
1
        let mut s2 = SymbolTable::new();
319
1
        let script_bytes = compiler
320
1
            .compile_with_mode(&program, &mut s1, CompileMode::Script)
321
1
            .unwrap();
322
1
        let eval_bytes = compiler
323
1
            .compile_with_mode(&program, &mut s2, CompileMode::Eval)
324
1
            .unwrap();
325
1
        assert_ne!(script_bytes, eval_bytes);
326
1
    }
327

            
328
    #[test]
329
1
    fn test_compile_nil() {
330
1
        let program = Program::new(vec![Expr::Nil]);
331
1
        let mut compiler = Compiler::new();
332
1
        let wasm = compiler.compile(&program, &mut SymbolTable::new()).unwrap();
333
1
        assert!(!wasm.is_empty());
334
1
        assert_eq!(&wasm[0..4], b"\0asm");
335
1
    }
336

            
337
    #[test]
338
1
    fn test_compile_bool() {
339
1
        let program = Program::new(vec![Expr::Bool(true)]);
340
1
        let mut compiler = Compiler::new();
341
1
        let wasm = compiler.compile(&program, &mut SymbolTable::new()).unwrap();
342
1
        assert!(!wasm.is_empty());
343
1
    }
344

            
345
    #[test]
346
1
    fn test_compile_number() {
347
        use num_rational::Ratio;
348
1
        let program = Program::new(vec![Expr::Number(Ratio::new(1, 2))]);
349
1
        let mut compiler = Compiler::new();
350
1
        let wasm = compiler.compile(&program, &mut SymbolTable::new()).unwrap();
351
1
        assert!(!wasm.is_empty());
352
1
    }
353

            
354
    #[test]
355
1
    fn test_compile_string() {
356
1
        let program = Program::new(vec![Expr::String("hello".into())]);
357
1
        let mut compiler = Compiler::new();
358
1
        let wasm = compiler.compile(&program, &mut SymbolTable::new()).unwrap();
359
1
        assert!(!wasm.is_empty());
360
1
        assert_eq!(&wasm[0..4], b"\0asm");
361
1
    }
362

            
363
    #[test]
364
1
    fn test_compile_symbol_with_value() {
365
1
        let mut symbols = SymbolTable::new();
366
1
        symbols.define(Symbol::new("REVISION", SymbolKind::Variable).with_value(Expr::Bool(true)));
367
1
        let program = Program::new(vec![Expr::Symbol("REVISION".into())]);
368
1
        let mut compiler = Compiler::new();
369
1
        let wasm = compiler.compile(&program, &mut symbols).unwrap();
370
1
        assert!(!wasm.is_empty());
371
1
    }
372

            
373
    #[test]
374
1
    fn test_compile_undefined_symbol() {
375
1
        let program = Program::new(vec![Expr::Symbol("UNKNOWN".into())]);
376
1
        let mut compiler = Compiler::new();
377
1
        let result = compiler.compile(&program, &mut SymbolTable::new());
378
1
        assert!(result.is_err());
379
1
        let err = result.unwrap_err();
380
1
        assert!(matches!(err, crate::error::Error::UndefinedSymbol(_)));
381
1
    }
382

            
383
    #[test]
384
1
    fn test_defun_populates_function_cell() {
385
1
        let program = Program::new(vec![Expr::List(vec![
386
1
            Expr::Symbol("DEFUN".into()),
387
1
            Expr::Symbol("SUM".into()),
388
1
            Expr::List(vec![
389
1
                Expr::Symbol("A".into()),
390
1
                Expr::Symbol("B".into()),
391
1
                Expr::Symbol("C".into()),
392
1
            ]),
393
1
            Expr::String("Sums A, B, C".into()),
394
1
            Expr::List(vec![
395
1
                Expr::Symbol("+".into()),
396
1
                Expr::Symbol("A".into()),
397
1
                Expr::Symbol("B".into()),
398
1
                Expr::Symbol("C".into()),
399
1
            ]),
400
1
        ])]);
401
1
        let mut compiler = Compiler::new();
402
1
        let mut symbols = SymbolTable::with_builtins();
403
1
        compiler.compile(&program, &mut symbols).unwrap();
404

            
405
1
        let sym = symbols.lookup("SUM").expect("SUM should be defined");
406
1
        assert!(sym.function().is_some());
407
1
        assert!(matches!(sym.function(), Some(Expr::Lambda(_, _))));
408
1
        assert_eq!(sym.doc(), Some("Sums A, B, C"));
409
1
    }
410

            
411
    #[test]
412
1
    fn test_defun_no_doc_populates_function_cell() {
413
1
        let program = Program::new(vec![Expr::List(vec![
414
1
            Expr::Symbol("DEFUN".into()),
415
1
            Expr::Symbol("ADD".into()),
416
1
            Expr::List(vec![Expr::Symbol("A".into()), Expr::Symbol("B".into())]),
417
1
            Expr::List(vec![
418
1
                Expr::Symbol("+".into()),
419
1
                Expr::Symbol("A".into()),
420
1
                Expr::Symbol("B".into()),
421
1
            ]),
422
1
        ])]);
423
1
        let mut compiler = Compiler::new();
424
1
        let mut symbols = SymbolTable::with_builtins();
425
1
        compiler.compile(&program, &mut symbols).unwrap();
426

            
427
1
        let sym = symbols.lookup("ADD").expect("ADD should be defined");
428
1
        assert!(sym.function().is_some());
429
1
        assert!(sym.doc().is_none());
430
1
    }
431
}