Lines
84.15 %
Functions
83.33 %
Branches
100 %
//! `LENGTH` — count the cells of a list. Constant-folds over a list
//! literal; for a runtime `PairRef` chain, walks the cells with an i32
//! counter. ADR-0028: a length is a count, so the result is an **Index**
//! (`I32`) — it composes with Index arithmetic / comparison, and crosses to
//! Scalar only via an explicit `(index->scalar …)`.
use crate::ast::{Expr, Number, PairElement, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::{
compile_for_stack, eval_value, fold_call, format_expr, serialize_stack_to_output,
};
use crate::error::{Error, Result};
use crate::runtime::SymbolTable;
use super::car_cdr::{coerce_anyref_to_pair, throw_unless_nil};
use super::map::extract_list_elements;
pub(super) fn length(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
if args.len() != 1 {
return Err(Error::Arity {
name: "LENGTH".to_string(),
expected: 1,
actual: args.len(),
});
}
let arg = eval_value(symbols, &args[0])?;
if matches!(
arg.wasm_type(),
Some(WasmType::PairRef(_) | WasmType::AnyRef)
) {
return Ok(Expr::WasmRuntime(WasmType::I32));
let count = extract_list_elements(&arg)
.map_err(|_| Error::Compile(format!("LENGTH expects a list, got {}", format_expr(&arg))))?
.len();
Ok(Expr::Number(Number::Index(count as i64)))
pub(super) fn compile_length(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
symbols: &mut SymbolTable,
args: &[Expr],
) -> Result<()> {
let ty = compile_length_to_stack(ctx, emit, symbols, args)?;
serialize_stack_to_output(ctx, emit, ty)
/// The count a fold-safe `(length …)` form folds to; `None` when the form
/// is compiled — including when LENGTH's own eval errs on a constant that is
/// not a list, so the `TYPE-ERROR` is raised at runtime where a handler can
/// catch it (0014 obligation (a)).
fn fold_length(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Option<i32>> {
match fold_call(symbols, "LENGTH", args)? {
Some(Expr::Number(Number::Index(n))) => i32::try_from(n)
.map(Some)
.map_err(|_| Error::Compile("LENGTH: list length exceeds i32 range".to_string())),
_ => Ok(None),
pub(super) fn compile_length_to_stack(
) -> Result<WasmType> {
if let Some(count) = fold_length(symbols, args)? {
emit.i32_const(count);
return Ok(WasmType::I32);
let arg_ty = compile_for_stack(ctx, emit, symbols, &args[0])?;
let elem = match arg_ty {
WasmType::PairRef(elem) => elem,
WasmType::AnyRef => {
coerce_anyref_to_pair(ctx, emit)?;
PairElement::AnyRef
other => {
throw_unless_nil(ctx, emit, symbols, "LENGTH", &args[0], other)?;
emit.i32_const(0);
let pair_idx = ctx.ids.ty_pair;
let pair_local = ctx.alloc_local(WasmType::PairRef(elem))?;
let count_local = ctx.alloc_local(WasmType::I32)?;
emit.local_set(pair_local);
emit.local_set(count_local);
emit.block_start();
emit.loop_start();
emit.local_get(pair_local);
emit.ref_is_null();
emit.br_if(1);
emit.local_get(count_local);
emit.i32_const(1);
emit.i32_add();
emit.struct_get(pair_idx, 1);
emit.br(0);
emit.block_end()?;
Ok(WasmType::I32)