1
//! `REVERSE` — produce the input list in reversed order. Constant-folds
2
//! when the input is a compile-time list literal; for runtime
3
//! `PairRef(elem)` chains, walks the input cell-by-cell building a fresh
4
//! reversed chain via the `pair_new` helper.
5

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

            
15
use super::datum::{compile_folded_to_stack, is_datum_result};
16

            
17
544
pub(super) fn reverse(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
18
544
    if args.len() != 1 {
19
        return Err(Error::Arity {
20
            name: "REVERSE".to_string(),
21
            expected: 1,
22
            actual: args.len(),
23
        });
24
544
    }
25
544
    let arg = eval_value(symbols, &args[0])?;
26
544
    if let Some(WasmType::PairRef(elem)) = arg.wasm_type() {
27
68
        return Ok(Expr::WasmRuntime(WasmType::PairRef(elem)));
28
476
    }
29
476
    let elements = match arg {
30
        Expr::Nil => return Ok(Expr::Nil),
31
        Expr::List(elems) => elems,
32
476
        Expr::Quote(inner) => match *inner {
33
476
            Expr::List(elems) => elems,
34
            Expr::Nil => return Ok(Expr::Nil),
35
            other => {
36
                return Err(Error::Compile(format!(
37
                    "REVERSE expects a list, got {}",
38
                    format_expr(&other)
39
                )));
40
            }
41
        },
42
        other => {
43
            return Err(Error::Compile(format!(
44
                "REVERSE expects a list, got {}",
45
                format_expr(&other)
46
            )));
47
        }
48
    };
49
476
    let reversed: Vec<Expr> = elements.into_iter().rev().collect();
50
476
    Ok(Expr::Quote(Box::new(Expr::List(reversed))))
51
544
}
52

            
53
340
pub(super) fn compile_reverse(
54
340
    ctx: &mut CompileContext,
55
340
    emit: &mut FunctionEmitter,
56
340
    symbols: &mut SymbolTable,
57
340
    args: &[Expr],
58
340
) -> Result<()> {
59
340
    let folded = reverse(symbols, args)?;
60
340
    if matches!(folded.wasm_type(), Some(WasmType::PairRef(_))) {
61
68
        let ty = compile_reverse_to_stack(ctx, emit, symbols, args)?;
62
68
        serialize_stack_to_output(ctx, emit, ty)?;
63
68
        return Ok(());
64
272
    }
65
    // A fully-constant (reverse '(…)) folds to a quoted reversed list — render
66
    // it as a datum so both compile surfaces agree (it used to trap on the
67
    // eval-with-type stack path with no runtime pair).
68
272
    if is_datum_result(&folded) {
69
272
        let ty = compile_folded_to_stack(ctx, emit, symbols, folded)?;
70
272
        return serialize_stack_to_output(ctx, emit, ty);
71
    }
72
    compile_expr(ctx, emit, symbols, &folded)
73
340
}
74

            
75
204
pub(super) fn compile_reverse_to_stack(
76
204
    ctx: &mut CompileContext,
77
204
    emit: &mut FunctionEmitter,
78
204
    symbols: &mut SymbolTable,
79
204
    args: &[Expr],
80
204
) -> Result<WasmType> {
81
204
    if args.len() != 1 {
82
        return Err(Error::Arity {
83
            name: "REVERSE".to_string(),
84
            expected: 1,
85
            actual: args.len(),
86
        });
87
204
    }
88
204
    let resolved = eval_value(symbols, &args[0])?;
89
204
    let elem = match resolved.wasm_type() {
90
68
        Some(WasmType::PairRef(e)) => e,
91
        _ => {
92
            // Not a runtime pair: const-fold and render the reversed datum,
93
            // matching the effect path (and CAR/CDR) so a constant or
94
            // runtime-builder list at value position lowers instead of trapping.
95
136
            let folded = reverse(symbols, args)?;
96
136
            return compile_folded_to_stack(ctx, emit, symbols, folded);
97
        }
98
    };
99
68
    emit_reverse_loop(ctx, emit, symbols, &args[0], elem)?;
100
68
    Ok(WasmType::PairRef(elem))
101
204
}
102

            
103
/// Walks `list_expr` cell by cell, prepending each car onto a fresh
104
/// accumulator chain. The accumulator is initialised to a null `$pair`
105
/// ref and rebuilt every iteration via `pair_new(car, acc)`. After the
106
/// loop, the accumulator holds the input reversed and is left on the
107
/// wasm stack.
108
612
pub(in crate::compiler::native::list) fn emit_reverse_loop(
109
612
    ctx: &mut CompileContext,
110
612
    emit: &mut FunctionEmitter,
111
612
    symbols: &mut SymbolTable,
112
612
    list_expr: &Expr,
113
612
    elem: PairElement,
114
612
) -> Result<()> {
115
612
    let pair_idx = ctx.ids.ty_pair;
116
612
    let pair_local = ctx.alloc_local(WasmType::PairRef(elem))?;
117
612
    let acc_local = ctx.alloc_local(WasmType::PairRef(elem))?;
118

            
119
612
    compile_for_stack(ctx, emit, symbols, list_expr)?;
120
612
    emit.local_set(pair_local);
121

            
122
612
    emit.ref_null(pair_idx);
123
612
    emit.local_set(acc_local);
124

            
125
612
    emit.block_start();
126
612
    emit.loop_start();
127

            
128
612
    emit.local_get(pair_local);
129
612
    emit.ref_is_null();
130
612
    emit.br_if(1);
131

            
132
    // pair_new expects (anyref car, ref null $pair cdr). The car comes
133
    // straight off the input pair as anyref — no downcast needed since
134
    // we're rebuilding into a fresh pair of the same element type.
135
612
    emit.local_get(pair_local);
136
612
    emit.struct_get(pair_idx, 0);
137
612
    emit.local_get(acc_local);
138
612
    emit.call(ctx.ids.pair_new);
139
612
    emit.local_set(acc_local);
140

            
141
612
    emit.local_get(pair_local);
142
612
    emit.struct_get(pair_idx, 1);
143
612
    emit.local_set(pair_local);
144

            
145
612
    emit.br(0);
146
612
    emit.block_end();
147
612
    emit.block_end();
148

            
149
612
    emit.local_get(acc_local);
150
612
    Ok(())
151
612
}