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
use crate::{
    Error, ErrorKind, TulispContext, TulispObject,
    bytecode::{
        Instruction, Pos,
        bytecode::CompiledDefun,
        compiler::{
            VMDefunParams,
            compiler::{
                compile_expr, compile_expr_keep_result, compile_progn, compile_progn_keep_result,
            },
        },
    },
    eval::substitute_lexical,
    list,
    object::wrappers::generic::SharedMut,
    parse::mark_tail_calls,
};

pub(super) fn compile_fn_print(
    ctx: &mut TulispContext,
    name: &TulispObject,
    args: &TulispObject,
) -> Result<Vec<Instruction>, Error> {
    ctx.compile_1_arg_call(name, args, false, |ctx, arg, _| {
        let mut result = compile_expr_keep_result(ctx, arg)?;
        if ctx.compiler.as_ref().unwrap().keep_result {
            result.push(Instruction::Print);
        } else {
            result.push(Instruction::PrintPop);
        }
        Ok(result)
    })
}

pub(super) fn compile_fn_quote(
    ctx: &mut TulispContext,
    name: &TulispObject,
    args: &TulispObject,
) -> Result<Vec<Instruction>, Error> {
    ctx.compile_1_arg_call(name, args, false, |ctx, arg, _| {
        let compiler = ctx.compiler.as_mut().unwrap();
        if compiler.keep_result {
            Ok(vec![Instruction::Push(arg.clone())])
        } else {
            Ok(vec![])
        }
    })
}

pub(super) fn compile_fn_cons(
    ctx: &mut TulispContext,
    name: &TulispObject,
    args: &TulispObject,
) -> Result<Vec<Instruction>, Error> {
    ctx.compile_2_arg_call(name, args, false, |ctx, arg1, arg2, _| {
        let mut result = compile_expr(ctx, arg1)?;
        result.append(&mut compile_expr(ctx, arg2)?);
        if ctx.compiler.as_ref().unwrap().keep_result {
            result.push(Instruction::Cons);
        }
        Ok(result)
    })
}

pub(super) fn compile_fn_list(
    ctx: &mut TulispContext,
    _name: &TulispObject,
    args: &TulispObject,
) -> Result<Vec<Instruction>, Error> {
    if args.is_bounced() {
        // args: (Bounce fn arg1 arg2 ...). The bounced function identity
        // is the second element.
        let name = args.cdr()?.car()?;
        return compile_fn_defun_bounce_call(ctx, &name, args);
    }

    let mut result = vec![];
    let mut len = 0;
    for arg in args.base_iter() {
        result.append(&mut compile_expr(ctx, &arg)?);
        len += 1;
    }
    if ctx.compiler.as_ref().unwrap().keep_result {
        result.push(Instruction::List(len));
    }
    Ok(result)
}

pub(super) fn compile_fn_append(
    ctx: &mut TulispContext,
    _name: &TulispObject,
    args: &TulispObject,
) -> Result<Vec<Instruction>, Error> {
    let mut result = vec![];
    let mut len = 0;
    for arg in args.base_iter() {
        result.append(&mut compile_expr(ctx, &arg)?);
        len += 1;
    }
    if ctx.compiler.as_ref().unwrap().keep_result {
        result.push(Instruction::Append(len));
    }
    Ok(result)
}

fn compile_fn_defun_bounce_call(
    ctx: &mut TulispContext,
    name: &TulispObject,
    args: &TulispObject,
) -> Result<Vec<Instruction>, Error> {
    let compiler = ctx.compiler.as_mut().unwrap();
    let is_self = compiler.current_defun.as_ref().is_some_and(|n| n.eq(name));

    if !is_self {
        let mut result = vec![];
        let mut args_count = 0;
        // cdr twice: first skips `Bounce`, second skips the function identity.
        for arg in args.cdr()?.cdr()?.base_iter() {
            result.append(&mut compile_expr_keep_result(ctx, &arg)?);
            args_count += 1;
        }
        // If the target is a known VM defun (mutual-recursion TCO
        // path; `mark_tail_calls` only marks `Bounce` for these and
        // self / `TulispValue::Lambda`), validate arity at compile
        // time — same shape as the self-bounce path below and the
        // `TulispValue::Defun` arm in `compile_form`. The runtime
        // `TailCall` handler also re-checks against the resolved
        // `bytecode.functions[name].params`, so missing it here on a
        // `Lambda` target stays a runtime error.
        let target_arity = ctx
            .compiler
            .as_ref()
            .and_then(|c| c.defun_args.get(&name.addr_as_usize()).cloned());
        if let Some(params) = target_arity {
            if args_count < params.required.len() {
                return Err(Error::arity_mismatch(format!(
                    "tail call to {}: too few arguments. expected: {} got: {}",
                    name,
                    params.required.len(),
                    args_count,
                ))
                .with_trace(args.clone()));
            }
            if params.rest.is_none() && args_count > params.required.len() + params.optional.len() {
                return Err(Error::arity_mismatch(format!(
                    "tail call to {}: too many arguments. expected: {} got: {}",
                    name,
                    params.required.len() + params.optional.len(),
                    args_count,
                ))
                .with_trace(args.clone()));
            }
        }
        // Tail-call escape: `TailCall` returns from `run_function`
        // directly, skipping the `EndScope`s the enclosing
        // `let` / `let*` would otherwise emit after this instruction.
        // Drain those scopes here so their bindings don't get stuck on
        // `LEX_STACKS` for the rest of the program. LIFO order.
        push_active_scope_endscopes(ctx, &mut result);
        result.push(Instruction::TailCall {
            name: name.clone(),
            // `args` is the rewritten `(list Bounce f a b)` shape
            // produced by `mark_tail_calls`. Its source span is set
            // to the original tail-call form's span (see
            // `mark_tail_calls`'s `with_span(span)` call), so using
            // it as the trace anchor still highlights the right
            // source range even though the text differs from the
            // pre-rewrite form.
            form: args.clone(),
            args_count,
            function: None,
            optional_count: 0,
            rest_count: 0,
        });
        return Ok(result);
    }

    let mut result = vec![];
    let params = compiler.defun_args[&name.addr_as_usize()].clone();
    let mut args_count = 0;
    // cdr twice: first skips `Bounce`, second skips the function identity.
    for arg in args.cdr()?.cdr()?.base_iter() {
        result.append(&mut compile_expr_keep_result(ctx, &arg)?);
        args_count += 1;
    }
    if args_count < params.required.len() {
        return Err(Error::arity_mismatch(format!(
            "defun bounce call: too few arguments. expected: {} got: {}",
            params.required.len(),
            args_count
        ))
        .with_trace(args.clone()));
    }
    let mut optional_count = 0;
    let left_args = args_count - params.required.len();
    if left_args > params.optional.len() {
        if params.rest.is_none() {
            return Err(Error::arity_mismatch(format!(
                "defun bounce call: too many arguments. expected: {} got: {}",
                params.required.len() + params.optional.len(),
                args_count
            ))
            .with_trace(args.clone()));
        }
        result.push(Instruction::List(left_args - params.optional.len()));
        optional_count = params.optional.len();
    } else if params.rest.is_some() {
        result.push(Instruction::Push(TulispObject::nil()));
    }
    if let Some(param) = &params.rest {
        result.push(Instruction::StorePop(param.clone()))
    }
    if left_args <= params.optional.len() && left_args > 0 {
        optional_count = left_args;
    }

    for (ii, param) in params.optional.iter().enumerate().rev() {
        if ii >= optional_count {
            result.push(Instruction::Push(TulispObject::nil()));
            result.push(Instruction::StorePop(param.clone()))
        } else {
            result.push(Instruction::StorePop(param.clone()));
        }
    }

    for param in params.required.iter().rev() {
        result.push(Instruction::StorePop(param.clone()))
    }
    // Self-recursion escape: `Jump(Pos::Abs(0))` jumps back to the
    // start of the function, skipping the trailing `EndScope`s of any
    // enclosing `let` / `let*`. Drain them here in LIFO order so the
    // bindings don't accumulate on `LEX_STACKS` across recursion
    // depths.
    push_active_scope_endscopes(ctx, &mut result);
    result.push(Instruction::Jump(Pos::Abs(0)));
    Ok(result)
}

/// Emit `EndScope` instructions for every active `let` / `let*`
/// binding tracked on the compiler, in LIFO order (newest first).
/// Called at function-escaping sites so bindings introduced by
/// enclosing let-forms don't leak past the escape.
fn push_active_scope_endscopes(ctx: &TulispContext, out: &mut Vec<Instruction>) {
    let compiler = ctx.compiler.as_ref().unwrap();
    for binding in compiler.active_let_scopes.iter().rev() {
        out.push(Instruction::EndScope(binding.clone()));
    }
}

pub(super) fn compile_fn_defun_call(
    ctx: &mut TulispContext,
    name: &TulispObject,
    args: &TulispObject,
) -> Result<Vec<Instruction>, Error> {
    let mut result = vec![];
    let mut args_count = 0;
    if name.consp() && name.car_and_then(|name| Ok(name.eq(&ctx.keywords.lambda)))? {
        compile_fn_defun(ctx, &name.car()?, &list!(name.clone() ,@name.cdr()?)?)?;
    }

    for arg in args.base_iter() {
        result.append(&mut compile_expr_keep_result(ctx, &arg)?);
        args_count += 1;
    }
    // Compile-time-only callers (`compile_form`) hold the source
    // span of the original `(name args…)` AST; this entry point
    // doesn't, so reconstruct a form from `name` and `args`. Without
    // a span, runtime trace formatting skips the line — VM defun
    // calls won't show their outer call-site in error backtraces.
    // Threading `form_span` through `Compiler` would close that gap.
    let synthetic_form = TulispObject::cons(name.clone(), args.clone());
    result.push(Instruction::Call {
        name: name.clone(),
        form: synthetic_form,
        args_count,
        function: None,
        optional_count: 0,
        rest_count: 0,
    });
    if !ctx.compiler.as_ref().unwrap().keep_result {
        result.push(Instruction::Pop);
    }
    Ok(result)
}

pub(super) fn compile_fn_defun(
    ctx: &mut TulispContext,
    defun_kw: &TulispObject,
    args: &TulispObject,
) -> Result<Vec<Instruction>, Error> {
    let mut defun_params = VMDefunParams {
        required: vec![],
        optional: vec![],
        rest: None,
    };
    let mut fn_name = TulispObject::nil();
    let res = ctx.compile_2_arg_call(defun_kw, args, true, |ctx, defun_name, args, body| {
        fn_name = defun_name.clone();
        let compiler = ctx.compiler.as_mut().unwrap();
        compiler
            .vm_compilers
            .functions
            .insert(defun_name.addr_as_usize(), compile_fn_defun_call);
        let args = args.base_iter().collect::<Vec<_>>();
        let mut is_optional = false;
        let mut is_rest = false;
        let mut mappings: Vec<(TulispObject, TulispObject)> = Vec::new();
        for arg in args.iter() {
            if arg.eq(&ctx.keywords.amp_optional) {
                if is_rest {
                    return Err(Error::new(
                        ErrorKind::Undefined,
                        "optional after rest".to_string(),
                    )
                    .with_trace(arg.clone()));
                }
                is_optional = true;
            } else if arg.eq(&ctx.keywords.amp_rest) {
                if is_rest {
                    return Err(
                        Error::new(ErrorKind::Undefined, "rest after rest".to_string())
                            .with_trace(arg.clone()),
                    );
                }
                is_optional = false;
                is_rest = true;
            } else {
                let lex = TulispObject::lexical_binding(ctx.lex_allocator.clone(), arg.clone());
                mappings.push((arg.clone(), lex.clone()));
                if is_optional {
                    defun_params.optional.push(lex);
                } else if is_rest {
                    if defun_params.rest.is_some() {
                        return Err(Error::new(
                            ErrorKind::Undefined,
                            "multiple rest arguments".to_string(),
                        )
                        .with_trace(arg.clone()));
                    }
                    defun_params.rest = Some(lex);
                } else {
                    defun_params.required.push(lex);
                }
            }
        }

        // This is required at this point, before the body is compiled, in case
        // of tail calls.
        compiler
            .defun_args
            .insert(defun_name.addr_as_usize(), defun_params.clone());
        let prev_defun = compiler.current_defun.replace(defun_name.clone());
        // The body starts a fresh function frame — escapes inside it
        // unwind to *this* defun, not whatever surrounding scope was
        // being compiled. Stash and clear `active_let_scopes` so the
        // body's tail-call sites only see the let scopes they're
        // actually nested in.
        let prev_scopes = std::mem::take(&mut compiler.active_let_scopes);

        // TODO: replace with `is_string`
        let body = if body.car()?.as_string().is_ok() {
            body.cdr()?
        } else {
            body.clone()
        };
        let body = mark_tail_calls(ctx, defun_name.clone(), body)?;
        let body = substitute_lexical(body, &mappings)?;
        let mut result = compile_progn_keep_result(ctx, &body)?;
        result.push(Instruction::Ret);

        let compiler = ctx.compiler.as_mut().unwrap();
        compiler.current_defun = prev_defun;
        compiler.active_let_scopes = prev_scopes;
        Ok(result)
    })?;
    // Lift the body's `PushTrace` / `PopTrace` markers into a
    // side table. Stripping happens at the `CompiledDefun`
    // boundary so the runtime never executes these markers — see
    // `strip_trace_markers`.
    let (res, trace_ranges) = crate::bytecode::bytecode::strip_trace_markers(res);
    let function = CompiledDefun {
        name: fn_name.clone(),
        instructions: SharedMut::new(res),
        trace_ranges: crate::object::wrappers::generic::Shared::new_sized(trace_ranges),
        params: defun_params,
    };
    let compiler = ctx.compiler.as_mut().unwrap();
    compiler
        .bytecode
        .functions
        .insert(fn_name.addr_as_usize(), function);
    Ok(vec![])
}

pub(super) fn compile_fn_progn(
    ctx: &mut TulispContext,
    _name: &TulispObject,
    args: &TulispObject,
) -> Result<Vec<Instruction>, Error> {
    compile_progn(ctx, args)
}

pub(super) fn compile_fn_load_file(
    ctx: &mut TulispContext,
    _name: &TulispObject,
    args: &TulispObject,
) -> Result<Vec<Instruction>, Error> {
    ctx.compile_1_arg_call(_name, args, true, |ctx, arg, _| {
        let mut result = compile_expr_keep_result(ctx, arg)?;
        result.push(Instruction::LoadFile);
        Ok(result)
    })
}

pub(super) fn compile_fn_noop(
    _ctx: &mut TulispContext,
    _name: &TulispObject,
    _args: &TulispObject,
) -> Result<Vec<Instruction>, Error> {
    Ok(vec![])
}