1
//! `PAIR?` — true when the argument is a non-empty cons cell. A
2
//! compile-time list literal folds to a constant; a runtime `PairRef`
3
//! chain emits `ref.is_null` + `i32.eqz` (non-null ≡ a real cell). Nil
4
//! and atoms are never pairs.
5

            
6
use crate::ast::{Expr, WasmType};
7
use crate::compiler::context::CompileContext;
8
use crate::compiler::emit::FunctionEmitter;
9
use crate::compiler::expr::{compile_for_stack, eval_value, fold_call, serialize_stack_to_output};
10
use crate::error::{Error, Result};
11
use crate::runtime::SymbolTable;
12

            
13
use super::super::comparison::bool_result;
14

            
15
1462
fn is_const_pair(expr: &Expr) -> bool {
16
1462
    match expr {
17
        Expr::Cons(_, _) => true,
18
626
        Expr::List(elems) => !elems.is_empty(),
19
626
        Expr::Datum(inner) => is_const_pair(inner),
20
210
        _ => false,
21
    }
22
1462
}
23

            
24
837
pub(super) fn pair_p(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
25
837
    if args.len() != 1 {
26
1
        return Err(Error::Arity {
27
1
            name: "PAIR?".to_string(),
28
1
            expected: 1,
29
1
            actual: args.len(),
30
1
        });
31
836
    }
32
836
    let arg = eval_value(symbols, &args[0])?;
33
836
    if matches!(arg.wasm_type(), Some(WasmType::PairRef(_))) {
34
        return Ok(Expr::WasmRuntime(WasmType::Bool));
35
836
    }
36
836
    Ok(bool_result(is_const_pair(&arg)))
37
837
}
38

            
39
832
pub(super) fn compile_pair_p(
40
832
    ctx: &mut CompileContext,
41
832
    emit: &mut FunctionEmitter,
42
832
    symbols: &mut SymbolTable,
43
832
    args: &[Expr],
44
832
) -> Result<()> {
45
832
    let ty = compile_pair_p_to_stack(ctx, emit, symbols, args)?;
46
832
    serialize_stack_to_output(ctx, emit, ty)
47
832
}
48

            
49
1248
pub(super) fn compile_pair_p_to_stack(
50
1248
    ctx: &mut CompileContext,
51
1248
    emit: &mut FunctionEmitter,
52
1248
    symbols: &mut SymbolTable,
53
1248
    args: &[Expr],
54
1248
) -> Result<WasmType> {
55
1248
    if args.len() != 1 {
56
        return Err(Error::Arity {
57
            name: "PAIR?".to_string(),
58
            expected: 1,
59
            actual: args.len(),
60
        });
61
1248
    }
62
1248
    if let Some(folded) = fold_call(symbols, "PAIR?", args)? {
63
832
        emit.i32_const(i32::from(!folded.is_nil()));
64
832
        return Ok(WasmType::Bool);
65
416
    }
66
416
    match compile_for_stack(ctx, emit, symbols, &args[0])? {
67
        WasmType::PairRef(_) => {
68
416
            emit.ref_is_null();
69
416
            emit.i32_eqz();
70
        }
71
        WasmType::AnyRef => {
72
            let local = ctx.alloc_local(WasmType::AnyRef)?;
73
            emit.local_tee(local);
74
            emit.ref_test(ctx.ids.ty_pair);
75
            emit.local_get(local);
76
            emit.ref_is_null();
77
            emit.i32_eqz();
78
            emit.i32_and();
79
        }
80
        _ => {
81
            emit.drop_value();
82
            emit.i32_const(0);
83
        }
84
    }
85
416
    Ok(WasmType::Bool)
86
1248
}