1
//! `LIST` — multi-arg list constructor. Lowers to a `$pair` chain when any
2
//! argument is a runtime value (so the result rides a `$pair` chain rather
3
//! than being held as a compile-time `Expr::List`) through the CONS cell
4
//! builders DIRECTLY — never by synthesizing a `(CONS …)` form.
5

            
6
use crate::ast::{Expr, WasmType};
7
use crate::compiler::context::CompileContext;
8
use crate::compiler::datum::bounded;
9
use crate::compiler::emit::FunctionEmitter;
10
use crate::compiler::expr::{compile_expr, eval_value, fold_value, serialize_stack_to_output};
11
use crate::error::Result;
12
use crate::runtime::SymbolTable;
13

            
14
use super::cons::compile_pair_chain;
15
use super::infer::chain_pair_element;
16
use super::spill::lambda_runs_at_runtime;
17

            
18
11897493
pub(super) fn list(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
19
11897493
    let resolved: Vec<Expr> = args
20
11897493
        .iter()
21
471262546
        .map(|a| eval_value(symbols, a))
22
6038
        .collect::<Result<_>>()?;
23
    // A runtime element can't ride a quoted compile-time list: consumers
24
    // (CAR/CDR/…) would extract a bare `WasmRuntime` placeholder with no real
25
    // stack value and emit invalid wasm. Report the chain `compile_pair_chain`
26
    // emits, typed from the ALREADY-RESOLVED elements so their side effects
27
    // stay single-applied.
28
11891455
    if lambda_runs_at_runtime(symbols, &resolved) {
29
47342
        return Ok(Expr::WasmRuntime(WasmType::AnyRef));
30
11844113
    }
31
11844113
    if resolved.iter().any(Expr::is_wasm_runtime) {
32
2577064
        return Ok(Expr::WasmRuntime(WasmType::PairRef(chain_pair_element(
33
2577064
            &resolved,
34
2577064
        )?)));
35
9267049
    }
36
9267049
    bounded(symbols, Expr::quoted_list(resolved))
37
11897493
}
38

            
39
44131
pub(super) fn compile_list(
40
44131
    ctx: &mut CompileContext,
41
44131
    emit: &mut FunctionEmitter,
42
44131
    symbols: &mut SymbolTable,
43
44131
    args: &[Expr],
44
44131
) -> Result<()> {
45
44131
    let form = Expr::List(
46
44131
        std::iter::once(Expr::symbol("LIST"))
47
44131
            .chain(args.iter().cloned())
48
44131
            .collect(),
49
44131
    );
50
44131
    if let Some(folded) = fold_value(symbols, &form)? {
51
4646
        return compile_expr(ctx, emit, symbols, &folded);
52
39485
    }
53
39485
    let ty = compile_list_to_stack(ctx, emit, symbols, args)?;
54
34024
    serialize_stack_to_output(ctx, emit, ty)
55
44131
}
56

            
57
/// Emit `(list …)` as a `$pair` chain. Resolves each element ONCE, then folds
58
/// right-to-left through `compile_cons_to_stack` — building each pair as a
59
/// two-element arg slice `[car, <inner-chain>]` whose cdr is the already-built
60
/// inner chain expression. Calls the CONS builder directly (no `(CONS …)`
61
/// form) and threads the resolved car values so element side effects are
62
/// applied exactly once.
63
301840
pub(super) fn compile_list_to_stack(
64
301840
    ctx: &mut CompileContext,
65
301840
    emit: &mut FunctionEmitter,
66
301840
    symbols: &mut SymbolTable,
67
301840
    args: &[Expr],
68
301840
) -> Result<WasmType> {
69
301840
    compile_pair_chain(ctx, emit, symbols, args)
70
301840
}