tulisp 0.29.0

An embeddable lisp interpreter.
Documentation
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
use std::collections::HashMap;

use crate::{
    Error, ErrorKind, TulispContext, TulispObject, TulispValue,
    bytecode::{Bytecode, Instruction},
    object::wrappers::generic::SharedMut,
};

use super::forms::{VMCompilers, compile_form};

#[derive(Default, Clone)]
pub(crate) struct VMDefunParams {
    pub required: Vec<TulispObject>,
    pub optional: Vec<TulispObject>,
    pub rest: Option<TulispObject>,
}

#[allow(dead_code)]
pub(crate) struct Compiler {
    pub vm_compilers: VMCompilers,
    pub defun_args: HashMap<usize, VMDefunParams>, // fn_name.addr_as_usize() -> arg symbol idx
    pub bytecode: Bytecode,
    pub keep_result: bool,
    pub current_defun: Option<TulispObject>,
    /// Lexical bindings introduced by the enclosing `let` / `let*`
    /// forms in source-order. Forms that emit a function-escaping
    /// instruction (`TailCall`, self-recursion's
    /// `Jump(Pos::Abs(0))`) read this list and emit `EndScope`s for
    /// the active bindings before the escape — otherwise the trailing
    /// `EndScope`s appended by `compile_fn_let_star` are skipped on
    /// the escape path and the bindings stay pushed on `LEX_STACKS`
    /// permanently. Saved/restored at lambda + defun boundaries so
    /// nested function bodies start fresh.
    pub active_let_scopes: Vec<TulispObject>,
    label_counter: usize,
}

impl Compiler {
    pub fn new(vm_compilers: VMCompilers) -> Self {
        Compiler {
            vm_compilers,
            defun_args: HashMap::new(),
            bytecode: Bytecode::new(),
            keep_result: true,
            current_defun: None,
            active_let_scopes: Vec::new(),
            label_counter: 0,
        }
    }

    pub fn new_label(&mut self) -> TulispObject {
        self.label_counter += 1;
        TulispObject::symbol(format!(":{}", self.label_counter), true)
    }

    pub fn reset_label_counter(&mut self) {
        self.label_counter = 0;
    }
}

pub fn compile(ctx: &mut TulispContext, value: &TulispObject) -> Result<Bytecode, Error> {
    // Snapshot the accumulated function set before compilation so the
    // returned `Bytecode` carries only the defuns produced by *this*
    // compile. The compiler itself keeps accumulating so subsequent
    // compiles (e.g., REPL-style) can resolve names that were defined
    // earlier, and the machine's own `bytecode.functions` grows via
    // `import_functions` on each run.
    let before: std::collections::HashSet<usize> = ctx
        .compiler
        .as_ref()
        .unwrap()
        .bytecode
        .functions
        .keys()
        .copied()
        .collect();
    let output = compile_progn(ctx, value)?;
    // Lift the form-trace markers in the global instruction
    // stream into a side table. Per-function bodies were already
    // stripped at their `CompiledDefun` boundary inside
    // `compile_fn_defun`.
    let (output, global_trace_ranges) = crate::bytecode::bytecode::strip_trace_markers(output);
    let global_trace_ranges =
        crate::object::wrappers::generic::Shared::new_sized(global_trace_ranges);
    let compiler = ctx.compiler.as_mut().unwrap();
    compiler.bytecode.global = SharedMut::new(output);
    compiler.bytecode.global_trace_ranges = global_trace_ranges.clone();
    let new_functions = compiler
        .bytecode
        .functions
        .iter()
        .filter(|(k, _)| !before.contains(k))
        .map(|(k, v)| (*k, v.clone()))
        .collect();
    Ok(Bytecode {
        global: compiler.bytecode.global.clone(),
        global_trace_ranges,
        functions: new_functions,
    })
}

pub fn compile_progn(
    ctx: &mut TulispContext,
    value: &TulispObject,
) -> Result<Vec<Instruction>, Error> {
    // Pre-pass: register the arities of every top-level
    // `(defun NAME PARAMS …)` in this progn before compiling any
    // body. Without this, `mark_tail_calls` only sees siblings that
    // were defined earlier in the form list, so cyclic mutual
    // recursion `(defun a () (b))` / `(defun b () (a))` wouldn't get
    // both directions TCO'd.
    pre_register_defun_arities(ctx, value);

    let mut result = vec![];
    let mut prev = None;
    let compiler = ctx.compiler.as_mut().unwrap();
    let keep_result = compiler.keep_result;
    compiler.keep_result = false;
    #[allow(dropping_references)]
    drop(compiler);
    for expr in value.base_iter() {
        if let Some(prev) = prev {
            result.append(&mut compile_expr(ctx, &prev)?);
        }
        prev = Some(expr);
    }
    let compiler = ctx.compiler.as_mut().unwrap();
    compiler.keep_result = keep_result;
    #[allow(dropping_references)]
    drop(compiler);
    if let Some(prev) = prev {
        result.append(&mut compile_expr(ctx, &prev)?);
    } else if keep_result {
        // Empty progn body — `(progn)` and `(let ((x 1)))` both end
        // up here. Per Emacs semantics, the form's value is `nil`.
        // Without this push, the VM stack underflows when the caller
        // expects a value.
        result.push(Instruction::Push(false.into()));
    }
    Ok(result)
}

/// Walk a progn-shaped form list and pre-populate `defun_args` with
/// arity entries for every top-level `(defun NAME (PARAMS) …)` it
/// contains. Used so `mark_tail_calls` can identify mutual-recursion
/// targets even before their own `compile_fn_defun` runs.
///
/// Only required/optional/rest **lengths** are consulted by
/// `mark_tail_calls` and `compile_fn_defun_bounce_call`'s non-self
/// arity check — the actual `TulispObject` values stored here are
/// just placeholders (the parameter names from source). When the
/// real `compile_fn_defun` runs for that defun, it overwrites this
/// entry with one that carries fresh `LexicalBinding` objects.
fn pre_register_defun_arities(ctx: &mut TulispContext, body: &TulispObject) {
    for expr in body.base_iter() {
        try_pre_register_one(ctx, &expr);
    }
}

fn try_pre_register_one(ctx: &mut TulispContext, expr: &TulispObject) {
    if !expr.consp() {
        return;
    }
    let Ok(head) = expr.car() else { return };
    let Ok(head_sym) = head.as_symbol() else {
        return;
    };
    if head_sym != "defun" {
        return;
    }
    let Ok(after_defun) = expr.cdr() else { return };
    let Ok(name_obj) = after_defun.car() else {
        return;
    };
    let Ok(after_name) = after_defun.cdr() else {
        return;
    };
    let Ok(params) = after_name.car() else { return };

    let mut required = Vec::new();
    let mut optional = Vec::new();
    let mut rest_param: Option<TulispObject> = None;
    let mut is_optional = false;
    let mut is_rest = false;
    for p in params.base_iter() {
        if p.eq(&ctx.keywords.amp_optional) {
            is_optional = true;
        } else if p.eq(&ctx.keywords.amp_rest) {
            is_optional = false;
            is_rest = true;
        } else if is_rest {
            rest_param = Some(p.clone());
        } else if is_optional {
            optional.push(p.clone());
        } else {
            required.push(p.clone());
        }
    }

    let params_struct = VMDefunParams {
        required,
        optional,
        rest: rest_param,
    };
    let compiler = ctx.compiler.as_mut().unwrap();
    compiler
        .defun_args
        .insert(name_obj.addr_as_usize(), params_struct);
}

pub(crate) fn compile_expr_keep_result(
    ctx: &mut TulispContext,
    expr: &TulispObject,
) -> Result<Vec<Instruction>, Error> {
    let compiler = ctx.compiler.as_mut().unwrap();
    let keep_result = compiler.keep_result;
    compiler.keep_result = true;
    #[allow(dropping_references)]
    drop(compiler);
    let ret = compile_expr(ctx, expr);
    ctx.compiler.as_mut().unwrap().keep_result = keep_result;
    ret
}

pub(crate) fn compile_progn_keep_result(
    ctx: &mut TulispContext,
    expr: &TulispObject,
) -> Result<Vec<Instruction>, Error> {
    let compiler = ctx.compiler.as_mut().unwrap();
    let keep_result = compiler.keep_result;
    compiler.keep_result = true;
    #[allow(dropping_references)]
    drop(compiler);
    let ret = compile_progn(ctx, expr);
    ctx.compiler.as_mut().unwrap().keep_result = keep_result;
    ret
}

/// Compile a backquoted form at quasi-quote `depth` (1 inside the
/// outer `\``, bumped by inner `\``, decremented by `,` / `,@`).
/// At depth 1 unquote/splice expressions are evaluated and their
/// values become list elements (or, for splice, multiple elements).
/// At depth > 1 they're treated as data: the inner expression is
/// compiled at `depth - 1` and the result is wrapped back in a
/// `Backquote` / `Unquote` / `Splice` cell with the matching `Wrap*`
/// instruction. This native compilation matches Emacs' nested
/// backquote semantics — `\`(a \`(b ,,x ,y) c)` with `x = 1`
/// produces `(a \`(b ,1 ,y) c)` — without falling back to a TW
/// runtime helper that would re-borrow `ctx.vm` on `CompiledDefun`
/// callees.
fn compile_back_quote(
    ctx: &mut TulispContext,
    value: &TulispObject,
    depth: u32,
) -> Result<Vec<Instruction>, Error> {
    let compiler = ctx.compiler.as_mut().unwrap();
    if !compiler.keep_result {
        return Ok(vec![]);
    }
    match &*value.inner_ref() {
        (TulispValue::Quote { value }, _) => {
            // `'X` inside a backquote is data — descend at the same
            // depth so nested unquotes inside still resolve.
            return compile_back_quote(ctx, value, depth).map(|mut v| {
                v.push(Instruction::Quote);
                v
            });
        }
        (TulispValue::Unquote { value }, _) => {
            if depth == 1 {
                return compile_expr(ctx, value).map_err(|e| e.with_trace(value.clone()));
            }
            let mut v = compile_back_quote(ctx, value, depth - 1)?;
            v.push(Instruction::WrapUnquote);
            return Ok(v);
        }
        (TulispValue::Splice { value }, _) => {
            if depth == 1 {
                return Err(Error::new(
                    crate::ErrorKind::SyntaxError,
                    "Splice must be within a backquoted list.".to_string(),
                ));
            }
            let mut v = compile_back_quote(ctx, value, depth - 1)?;
            v.push(Instruction::WrapSplice);
            return Ok(v);
        }
        (TulispValue::Backquote { value }, _) => {
            let mut v = compile_back_quote(ctx, value, depth + 1)?;
            v.push(Instruction::WrapBackquote);
            return Ok(v);
        }
        (TulispValue::List { .. }, _) => {}
        _ => return Ok(vec![Instruction::Push(value.clone())]),
    }
    let mut result = vec![];

    let mut value = value.clone();
    let mut items = 0;
    let mut need_list = true;
    let mut need_append = false;
    loop {
        value.car_and_then(|first| {
            let first_inner = &*first.inner_ref();
            if let (TulispValue::Unquote { value }, _) = first_inner {
                items += 1;
                if depth == 1 {
                    result.append(
                        &mut compile_expr(ctx, value).map_err(|e| e.with_trace(first.clone()))?,
                    );
                } else {
                    result.append(&mut compile_back_quote(ctx, value, depth - 1)?);
                    result.push(Instruction::WrapUnquote);
                }
            } else if let (TulispValue::Splice { value }, _) = first_inner {
                if depth == 1 {
                    let mut splice_result = compile_expr(ctx, value)?;
                    let list_inst = splice_result.pop().unwrap();
                    if let Instruction::List(n) = list_inst {
                        result.append(&mut splice_result);
                        items += n;
                    } else if let Instruction::Load(idx) = list_inst {
                        result.append(&mut splice_result);
                        result.push(Instruction::List(items));
                        if need_append {
                            result.push(Instruction::Append(2));
                        }
                        result.append(&mut vec![Instruction::Load(idx), Instruction::Append(2)]);
                        need_append = true;
                        items = 0;
                    } else {
                        if !value.consp() {
                            return Err(Error::new(
                                ErrorKind::SyntaxError,
                                format!(
                                    "Can only splice an inplace-list or a variable binding: {}",
                                    value
                                ),
                            )
                            .with_trace(first.clone()));
                        }
                        result.push(Instruction::List(items));
                        if need_append {
                            result.push(Instruction::Append(2));
                        }
                        result.append(&mut splice_result);
                        result.push(list_inst);
                        result.push(Instruction::Append(2));
                        need_append = true;
                        items = 0;
                    }
                } else {
                    // depth > 1: splice at this level is just data —
                    // wrap as a Splice value and treat as one element.
                    items += 1;
                    result.append(&mut compile_back_quote(ctx, value, depth - 1)?);
                    result.push(Instruction::WrapSplice);
                }
            } else if let (TulispValue::Backquote { value }, _) = first_inner {
                items += 1;
                result.append(&mut compile_back_quote(ctx, value, depth + 1)?);
                result.push(Instruction::WrapBackquote);
            } else {
                items += 1;
                result.append(&mut compile_back_quote(ctx, first, depth)?);
            }
            Ok(())
        })?;
        let rest = value.cdr()?;
        if let (TulispValue::Unquote { value }, _) = &*rest.inner_ref() {
            if depth == 1 {
                result.append(&mut compile_expr(ctx, value)?);
                result.push(Instruction::Cons);
                need_list = false;
                break;
            }
            result.append(&mut compile_back_quote(ctx, value, depth - 1)?);
            result.push(Instruction::WrapUnquote);
            result.push(Instruction::Cons);
            need_list = false;
            break;
        }
        if !rest.consp() {
            if !rest.null() {
                result.push(Instruction::Push(rest.clone()));
                result.push(Instruction::Cons);
                need_list = false;
            }
            break;
        }
        value = rest;
    }
    if need_list {
        result.push(Instruction::List(items));
    }
    if need_append {
        result.push(Instruction::Append(2));
    }
    Ok(result)
}

pub(crate) fn compile_expr(
    ctx: &mut TulispContext,
    expr: &TulispObject,
) -> Result<Vec<Instruction>, Error> {
    let expr_ref = expr.inner_ref();
    let compiler = ctx.compiler.as_mut().unwrap();
    match &*expr_ref {
        (TulispValue::Number { .. }, _) => {
            if compiler.keep_result {
                // Preserve the original AST cell so identity-based ops
                // (`eq`) keep working when an int literal here is
                // compared against the same int from the parser's
                // per-parse cache. Going through `Number::into()`
                // would route through `INT_CACHE` instead and break
                // pointer equality across the two cells.
                Ok(vec![Instruction::Push(expr.clone())])
            } else {
                Ok(vec![])
            }
        }
        (TulispValue::Nil, _) | (TulispValue::T, _) => {
            if compiler.keep_result {
                // Push the parsed object itself, not a fresh
                // `false.into()` / `true.into()`. Otherwise a `nil`
                // / `t` argument loses its source span and error
                // backtraces miss the trace line for it (TW path
                // keeps the span, so VM and TW would diverge).
                Ok(vec![Instruction::Push(expr.clone())])
            } else {
                Ok(vec![])
            }
        }
        (TulispValue::String { .. }, _) | (TulispValue::Any(_), _) => {
            if compiler.keep_result {
                Ok(vec![Instruction::Push(expr.clone())])
            } else {
                Ok(vec![])
            }
        }
        (TulispValue::Lambda { .. }, _)
        | (TulispValue::Func(_), _)
        | (TulispValue::Defun { .. }, _)
        | (TulispValue::CompiledDefun { .. }, _)
        | (TulispValue::Macro(_), _)
        | (TulispValue::Defmacro { .. }, _)
        | (TulispValue::Bounce, _) => Ok(vec![]),

        (TulispValue::Backquote { value }, _) => {
            compile_back_quote(ctx, value, 1).map_err(|e| e.with_trace(expr.clone()))
        }
        (TulispValue::Quote { value }, _) | (TulispValue::Sharpquote { value }, _) => {
            if compiler.keep_result {
                Ok(vec![Instruction::Push(value.clone())])
            } else {
                Ok(vec![])
            }
        }
        (TulispValue::List { .. }, _) => {
            drop(expr_ref);
            // Wrap the form's compiled bytecode with `PushTrace` /
            // `PopTrace` markers. `strip_trace_markers` lifts these
            // into a side-table at compile time so the runtime
            // pays nothing for them on the happy path; on the
            // error path, `run_impl` looks up which ranges contain
            // the failing PC and applies their forms via
            // `with_trace`. Same shape TW's `eval_basic` produces.
            let mut inner = compile_form(ctx, expr).map_err(|e| e.with_trace(expr.clone()))?;
            if inner.is_empty() {
                return Ok(inner);
            }
            let mut wrapped = Vec::with_capacity(inner.len() + 2);
            wrapped.push(Instruction::PushTrace(expr.clone()));
            wrapped.append(&mut inner);
            wrapped.push(Instruction::PopTrace);
            Ok(wrapped)
        }
        (TulispValue::Symbol { .. }, _) | (TulispValue::LexicalBinding { .. }, _) => {
            if !compiler.keep_result {
                return Ok(vec![]);
            }
            Ok(vec![if expr.keywordp() {
                Instruction::Push(expr.clone())
            } else {
                Instruction::Load(expr.clone())
            }])
        }
        (TulispValue::Unquote { .. }, _) => Err(Error::new(
            crate::ErrorKind::SyntaxError,
            "Unquote without backquote".to_string(),
        )),
        (TulispValue::Splice { .. }, _) => Err(Error::new(
            crate::ErrorKind::SyntaxError,
            "Splice without backquote".to_string(),
        )),
    }
}