Lines
100 %
Functions
Branches
//! Operands compiled to one numeric dimension (ADR-0028): a literal coerces
//! across the Index/Scalar boundary, a runtime value must already have it.
use crate::ast::{Expr, Number, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::error::{Error, Result};
use crate::runtime::SymbolTable;
use super::effect::compile_for_effect;
use super::fold::{probe_value, raises_division};
use super::stack::{compile_for_stack, push_number_as};
use crate::compiler::special::form_diverges_scratch;
const PROGN: &str = "progn";
/// Emits `expr` when it always raises (a constant division by zero, an
/// `ERROR`, a `PROGN` with either in its body) and answers `true`; its value
/// is never produced, so the caller gives it no dimension (ISS-087 keeps
/// program conditions at runtime).
pub(in crate::compiler) fn compile_raising(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
symbols: &mut SymbolTable,
expr: &Expr,
) -> Result<bool> {
if !always_raises(symbols, expr)? {
return Ok(false);
}
compile_for_stack(ctx, emit, symbols, expr)?;
emit.drop_value();
Ok(true)
/// [`compile_raising`] that also seals the dead code after the raise, so the
/// caller compiles nothing further.
pub(in crate::compiler) fn compile_raising_to_end(
compile_diverging_to_end(ctx, emit, symbols, expr, always_raises)
/// Emits `expr` and seals the dead code after it when `diverges` holds.
pub(in crate::compiler) fn compile_diverging_to_end(
diverges: fn(&SymbolTable, &Expr) -> Result<bool>,
if !diverges(symbols, expr)? {
emit.unreachable();
/// Compiles `expr` into a fresh local and answers the local with its type;
/// `None` when it never returns. In dead code the operand is compiled once on
/// a live scratch, since the dead emitter yields a placeholder: the scratch
/// reports what it pushes when reached and whether it returns.
pub(in crate::compiler) fn compile_to_local(
) -> Result<Option<(u32, WasmType)>> {
let (ty, returns) = match emit.is_reachable() {
true => {
let ty = compile_for_stack(ctx, emit, symbols, expr)?;
(ty, emit.is_reachable())
false => emit.on_live_scratch(|scratch| compile_for_stack(ctx, scratch, symbols, expr))?,
};
if !returns {
return Ok(None);
let local = ctx.alloc_local(ty)?;
emit.local_set(local);
Ok(Some((local, ty)))
/// [`compile_raising`] for a boundary that needs an integer placeholder for the
/// never-observed value, which adopts the dimension the boundary demands.
pub(in crate::compiler) fn compile_raising_constant(
) -> Result<Option<Number>> {
Ok(compile_raising(ctx, emit, symbols, expr)?.then_some(Number::Index(1)))
/// True when evaluating `expr` always raises, so its value is never produced.
pub(in crate::compiler) fn always_raises(symbols: &SymbolTable, expr: &Expr) -> Result<bool> {
Ok(progn_divides(symbols, expr) || form_diverges_scratch(symbols, expr)?)
/// A constant division by zero in `expr` or, recursively, in a `PROGN` body;
/// the other raising forms are `form_diverges`' walk.
fn progn_divides(symbols: &SymbolTable, expr: &Expr) -> bool {
if raises_division(symbols, expr) {
return true;
match expr {
Expr::List(items) => match items.as_slice() {
[Expr::Symbol(head), body @ ..] if head.eq_ignore_ascii_case(PROGN) => {
body.iter().any(|form| progn_divides(symbols, form))
_ => false,
},
/// Compiles `expr` as a Scalar operand.
///
/// ADR-0028: a numeric literal is a dimension-flexible token — coerce it to
/// Scalar (the sanctioned Index↔Scalar crossing). A RUNTIME index never
/// coerces; it must bridge explicitly via `(index->scalar …)`. A non-literal
/// operand that *resolves* to a number still emits its effects on the live
/// table first (the probe runs on a clone, so it applies none).
pub(in crate::compiler) fn compile_ratio_operand(
) -> Result<()> {
if let Expr::Number(n) = probe_value(&mut symbols.clone(), expr)? {
if !matches!(expr, Expr::Number(_)) {
compile_for_effect(ctx, emit, symbols, expr)?;
return push_number_as(ctx, emit, n, WasmType::Ratio);
if let Some(n) = compile_raising_constant(ctx, emit, symbols, expr)? {
match ty {
WasmType::Ratio => Ok(()),
WasmType::Commodity => Err(Error::Dimension(
"commodity-bearing values cannot mix with pure-rational arithmetic; \
use `(convert-commodity ...)` to bridge"
.to_string(),
)),
WasmType::I32 => Err(Error::Dimension(
"a runtime index (count) cannot be used as a scalar; \
bridge explicitly with `(index->scalar ...)`"
WasmType::Bool
| WasmType::PairRef(_)
| WasmType::StringRef
| WasmType::SymbolRef
| WasmType::EntityRef(_)
| WasmType::StructRef(_)
| WasmType::Closure(_)
| WasmType::AnyRef => Err(Error::Compile(format!(
"arithmetic requires ratio values, got {ty}"
))),
.or_else(|err| emit.reject_if_live(err))
/// Compiles `expr` as a raw i32 Index value: a runtime `I32` (a count/length/
/// index) or an integer literal. A fractional literal is a Scalar, and a
/// runtime Ratio/Commodity/ref is not an Index — all rejected (ADR-0028:
/// Index combines only with Index + integer literals). See
/// [`compile_ratio_operand`].
pub(in crate::compiler) fn compile_index_operand(
return push_number_as(ctx, emit, n, WasmType::I32);
WasmType::I32 => Ok(()),
WasmType::Ratio | WasmType::Commodity => Err(Error::Dimension(
"a scalar/money value cannot be used as an index; \
bridge explicitly with `(scalar->index ...)`"
"index arithmetic requires an integer count, got {ty}"