Lines
89.89 %
Functions
80 %
Branches
100 %
//! `MAP` over a runtime `PairRef` list.
use crate::ast::{Expr, PairElement, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::{
as_literal_arg, call, classify_stack_type, compile_for_stack, emit_box_cell, eval_value,
};
use crate::error::{Error, Result};
use crate::runtime::SymbolTable;
use super::reverse::emit_reverse_loop;
/// MAP's output element for a runtime chain input: a let-bound closure's
/// recorded signature result (same rule `map_fn`'s eval mirror and
/// `compile_map_literal` use); otherwise (an inline lambda or bare
/// defun with no signature yet) probe one application against a
/// representative element of the input chain, mirroring FOLD's
/// `probe_body_type`. Falls back to the input element when the probe
/// itself can't classify (the per-row FUNCALL still runs the real check
/// below, so an actual mismatch still surfaces as a compile error there).
pub(super) fn predict_map_result_element(
symbols: &SymbolTable,
fn_arg: &Expr,
elem: PairElement,
) -> Result<PairElement> {
if let Some(sig) = eval_value(&mut symbols.clone(), fn_arg)
.ok()
.and_then(|v| super::sourceless_closure_sig(symbols, &v))
&& let Some(result_elem) = symbols
.closure_result(sig)
.and_then(PairElement::from_wasm_type)
{
return Ok(result_elem);
}
let sample = as_literal_arg(&Expr::WasmRuntime(elem.as_wasm_type()));
let mut probe = symbols.clone();
let result = match call(&mut probe, &[fn_arg.clone(), sample]) {
Ok(result) => result,
Err(err @ Error::Dimension(_)) => return Err(err),
Err(_) => return Ok(elem),
Ok(classify_stack_type(&result)
.unwrap_or(elem))
/// Walks the runtime `PairRef(elem)` input already on the stack. Builds the result reversed
/// (one prepend per car) by calling the function on each car, then
/// reverses once at the end so the output preserves input order.
pub(super) fn compile_map_runtime_list(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
symbols: &mut SymbolTable,
) -> Result<WasmType> {
// MAP's OUTPUT element is the closure's RESULT type (it transforms each
// element), NOT the input element — same rule `map_fn`'s eval mirror uses.
let result_elem = predict_map_result_element(symbols, fn_arg, elem)?;
let pair_idx = ctx.ids.ty_pair;
let pair_local = ctx.alloc_local(WasmType::PairRef(result_elem))?;
let acc_local = ctx.alloc_local(WasmType::PairRef(result_elem))?;
let car_local = ctx.alloc_local(elem.as_wasm_type())?;
emit.local_set(pair_local);
emit.ref_null(pair_idx);
emit.local_set(acc_local);
emit.block_start();
emit.loop_start();
emit.local_get(pair_local);
emit.ref_is_null();
emit.br_if(1);
emit.struct_get(pair_idx, 0);
crate::compiler::native::list::emit_pair_car_downcast(ctx, emit, elem)?;
emit.local_set(car_local);
let mapped_ty = compile_map_call_with_local(ctx, emit, symbols, fn_arg, car_local, elem)?;
let actual_elem = PairElement::from_wasm_type(mapped_ty).ok_or_else(|| {
Error::Compile(format!(
"MAP: closure result type {mapped_ty} can't ride a typed pair; \
flatten via let-bind first"
))
})?;
if actual_elem != result_elem {
return Err(Error::Compile(format!(
"MAP: closure result element {actual_elem} doesn't match input element {result_elem}; \
heterogeneous mapping isn't supported yet"
)));
emit_box_cell(ctx, emit, actual_elem, result_elem);
emit.local_get(acc_local);
emit.call(ctx.ids.pair_new);
emit.struct_get(pair_idx, 1);
emit.br(0);
emit.block_end()?;
// The accumulator is in reverse order — walk it once more through
// the standard reverse loop so the output preserves input order.
let acc_expr = Expr::WasmLocal(acc_local, WasmType::PairRef(result_elem));
emit_reverse_loop(ctx, emit, symbols, &acc_expr, result_elem)?;
Ok(WasmType::PairRef(result_elem))
/// Emit a per-iteration call of `fn_arg` against the value held in
/// `car_local`. A quoted function name (`'g2`) designates that function, so it
/// is called by name; any other callable rides FUNCALL.
fn compile_map_call_with_local(
car_local: u32,
let car_expr = Expr::WasmLocal(car_local, elem.as_wasm_type());
let call = match fn_arg {
Expr::Quote(designator) | Expr::Datum(designator)
if matches!(designator.as_ref(), Expr::Symbol(_)) =>
vec![designator.as_ref().clone(), car_expr]
_ => vec![Expr::symbol("FUNCALL"), fn_arg.clone(), car_expr],
compile_for_stack(ctx, emit, symbols, &Expr::List(call))