Lines
95.83 %
Functions
95.45 %
Branches
100 %
//! Runtime codegen for the comparison shape. Each per-op
//! `compile_*_to_stack` routes through one of three helpers:
//! `compile_cmp_to_stack` (forward operand order, dispatched by
//! name), `compile_cmp_to_stack_reversed` (`>` ≡ swap-and-`<`),
//! or the per-op explicit form (`<=` / `>=` need an `i32_eqz` over
//! the strict comparator since `ratio_le` / `commodity_le` don't
//! exist).
use crate::ast::{Expr, Fraction, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::{always_raises, is_untyped, probe_value};
use crate::error::{Error, Result};
use crate::runtime::SymbolTable;
use super::super::shared::{
compile_args_for_effect, extract_numbers, resolve_all, validate_cmp_args,
};
use super::emit::{ExprOperands, emit_runtime_cmp};
use super::identity::{emit_identity, identity_name};
use super::spilled::{compile_spilled_cmp, compile_spilled_identity};
pub(in crate::compiler::native::comparison) fn compile_eq_to_stack(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
symbols: &mut SymbolTable,
args: &[Expr],
) -> Result<WasmType> {
compile_cmp_to_stack(ctx, emit, symbols, args, "=", |a, b| a == b)
}
/// `EQL` / `EQUAL` / `EQ?` / `EQUAL?` — generic structural equality. Unlike the
/// numeric `=` (`compile_eq_to_stack`), this also compares strings: a runtime
/// `account-name` against a literal lowers to the shared `string_eq` byte
/// comparison. The result is a `Bool` truth value (Nil/Bool serialization), so
/// it composes inside `and` / `if` and as a value-position result.
pub(in crate::compiler::native::comparison) fn compile_eql_to_stack(
compile_identity_to_stack(ctx, emit, symbols, args, false)
pub(in crate::compiler::native::comparison) fn compile_equal_to_stack(
compile_identity_to_stack(ctx, emit, symbols, args, true)
fn compile_identity_to_stack(
structural: bool,
if args.len() != 2 {
return Err(Error::Arity {
name: identity_name(structural).to_string(),
expected: 2,
actual: args.len(),
});
// One disposable clone, probed in order: `b` must classify against the
// compile-time effects of `a` (a `setf`, a `defvar`).
let mut probe = symbols.clone();
let a = probe_value(&mut probe, &args[0])?;
let b = probe_value(&mut probe, &args[1])?;
let resolved = [a, b];
if is_spilled(symbols, args, &resolved)? {
return compile_spilled_identity(ctx, emit, symbols, args, &resolved, structural);
let identity = emit_identity(
&mut ExprOperands {
ctx,
emit,
symbols,
args,
},
&resolved[0],
&resolved[1],
structural,
);
match identity {
Err(mix @ Error::Dimension(_)) => {
compile_spilled_identity(ctx, emit, symbols, args, &resolved, structural)
.map_err(|_| mix)
other => other,
pub(in crate::compiler::native::comparison) fn compile_neq_to_stack(
if args.is_empty() {
return Err(Error::Compile(
"/= requires at least 1 argument".to_string(),
));
let resolved = resolve_all(symbols, args)?;
if let Some(nums) = extract_numbers(&resolved, "/=")? {
let result = (0..nums.len()).all(|i| (i + 1..nums.len()).all(|j| nums[i] != nums[j]));
compile_args_for_effect(ctx, emit, symbols, args)?;
emit.i32_const(i32::from(result));
return Ok(WasmType::Bool);
compile_runtime_cmp(ctx, emit, symbols, args, &resolved, "/=")
pub(in crate::compiler::native::comparison) fn compile_lt_to_stack(
compile_cmp_to_stack(ctx, emit, symbols, args, "<", |a, b| a < b)
pub(in crate::compiler::native::comparison) fn compile_gt_to_stack(
// a > b ≡ b < a
compile_cmp_to_stack_reversed(ctx, emit, symbols, args, ">", |a, b| a > b)
pub(in crate::compiler::native::comparison) fn compile_le_to_stack(
"<= requires exactly 2 arguments for runtime comparison".to_string(),
if let Some(nums) = extract_numbers(&resolved, "<=")? {
let result = nums[0] <= nums[1];
compile_runtime_cmp(ctx, emit, symbols, args, &resolved, "<=")
pub(in crate::compiler::native::comparison) fn compile_ge_to_stack(
">= requires exactly 2 arguments for runtime comparison".to_string(),
if let Some(nums) = extract_numbers(&resolved, ">=")? {
let result = nums[0] >= nums[1];
compile_runtime_cmp(ctx, emit, symbols, args, &resolved, ">=")
pub(in crate::compiler::native::comparison) fn compile_cmp_to_stack_by_name(
name: &str,
const_cmp: fn(&Fraction, &Fraction) -> bool,
match name {
"=" => compile_eq_to_stack(ctx, emit, symbols, args),
"<" => compile_lt_to_stack(ctx, emit, symbols, args),
">" => compile_gt_to_stack(ctx, emit, symbols, args),
"<=" => compile_le_to_stack(ctx, emit, symbols, args),
">=" => compile_ge_to_stack(ctx, emit, symbols, args),
_ => compile_cmp_to_stack(ctx, emit, symbols, args, name, const_cmp),
/// The runtime comparison of two operands.
fn compile_runtime_cmp(
resolved: &[Expr],
let chained = args.len() > 2 && name != "/=";
if chained || (args.len() == 2 && is_spilled(symbols, args, resolved)?) {
return compile_spilled_cmp(ctx, emit, symbols, args, resolved, name);
let dim = match runtime_cmp_dim(args, resolved, name) {
// A raising operand has no dimension; only compiling it shows that.
return compile_spilled_cmp(ctx, emit, symbols, args, resolved, name).map_err(|_| mix);
dim => dim?,
emit_runtime_cmp(
name,
dim,
)
fn is_spilled(symbols: &SymbolTable, args: &[Expr], resolved: &[Expr]) -> Result<bool> {
if resolved.iter().any(is_untyped) {
return Ok(true);
for arg in args {
if always_raises(symbols, arg)? {
Ok(false)
fn runtime_cmp_dim(args: &[Expr], resolved: &[Expr], name: &str) -> Result<WasmType> {
return Err(Error::Compile(format!(
"{name} with runtime args requires exactly 2 arguments"
)));
validate_cmp_args(resolved, name)
fn compile_cmp_to_stack(
"{name} requires at least 1 argument"
if let Some(nums) = extract_numbers(&resolved, name)? {
let result = nums.windows(2).all(|w| const_cmp(&w[0], &w[1]));
compile_runtime_cmp(ctx, emit, symbols, args, &resolved, name)
fn compile_cmp_to_stack_reversed(