Lines
93.69 %
Functions
97.3 %
Branches
100 %
//! Eval handlers + the effect-path compile wrappers. Eval handlers
//! constant-fold over `Expr::Number` args; the compile wrappers
//! detect the const-fold path inline and only re-enter `to_stack`
//! when at least one arg is a runtime value.
use crate::ast::{Expr, Fraction, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::{eval_value, serialize_stack_to_output};
use crate::error::{Error, Result};
use crate::runtime::SymbolTable;
use super::super::shared::{
bool_result, compile_args_for_effect, emit_bool, extract_numbers, has_runtime, resolve_all,
same_datum, validate_valued_args,
};
use super::to_stack::{
compile_cmp_to_stack_by_name, compile_eql_to_stack, compile_equal_to_stack,
compile_neq_to_stack,
pub(in crate::compiler::native::comparison) fn eval_eq(
s: &mut SymbolTable,
args: &[Expr],
) -> Result<Expr> {
num_cmp(s, args, "=", |a, b| a == b)
}
pub(in crate::compiler::native::comparison) fn eval_lt(
num_cmp(s, args, "<", |a, b| a < b)
pub(in crate::compiler::native::comparison) fn eval_gt(
num_cmp(s, args, ">", |a, b| a > b)
pub(in crate::compiler::native::comparison) fn eval_le(
num_cmp(s, args, "<=", |a, b| a <= b)
pub(in crate::compiler::native::comparison) fn eval_ge(
num_cmp(s, args, ">=", |a, b| a >= b)
pub(in crate::compiler::native::comparison) fn compile_eq(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
) -> Result<()> {
compile_num_cmp(ctx, emit, s, args, "=", |a, b| a == b)
pub(in crate::compiler::native::comparison) fn compile_lt(
compile_num_cmp(ctx, emit, s, args, "<", |a, b| a < b)
pub(in crate::compiler::native::comparison) fn compile_gt(
compile_num_cmp(ctx, emit, s, args, ">", |a, b| a > b)
pub(in crate::compiler::native::comparison) fn compile_le(
compile_num_cmp(ctx, emit, s, args, "<=", |a, b| a <= b)
pub(in crate::compiler::native::comparison) fn compile_ge(
compile_num_cmp(ctx, emit, s, args, ">=", |a, b| a >= b)
/// Evaluates every one of `args` EXACTLY ONCE, in source order, directly on
/// the caller's LIVE table. Eval-only comparisons (unlike the codegen
/// paths, which separately probe-on-a-clone then emit-for-real) have no
/// second "emission" step at all — this single pass over the live table IS
/// the one place their operands' compile-time effects apply, whether the
/// result folds to a constant or turns out to need a runtime placeholder.
/// A disposable clone here would leave a runtime-detected operand's effects
/// (a nested `setf`/`defvar`) unapplied, and — for a caller like `eq?`
/// classifying ITS OWN two operands in sequence — would let a later operand
/// probe against stale state that later real emission has already moved
/// past (predicting the wrong static type for it).
fn eval_all_for_effect(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Vec<Expr>> {
args.iter().map(|arg| eval_value(symbols, arg)).collect()
fn num_cmp(
symbols: &mut SymbolTable,
name: &str,
cmp: fn(&Fraction, &Fraction) -> bool,
if args.is_empty() {
return Err(Error::Compile(format!(
"{name} requires at least 1 argument"
)));
let resolved = eval_all_for_effect(symbols, args)?;
if let Some(nums) = extract_numbers(&resolved, name)? {
let result = nums.windows(2).all(|w| cmp(&w[0], &w[1]));
return Ok(bool_result(result));
if has_runtime(&resolved) {
validate_valued_args(symbols, args, &resolved, name)?;
return Ok(Expr::WasmRuntime(WasmType::Bool));
Err(Error::Compile(format!("{name} expects numeric arguments")))
pub(in crate::compiler::native::comparison) fn num_neq(
return Err(Error::Compile(
"/= requires at least 1 argument".to_string(),
));
if let Some(nums) = extract_numbers(&resolved, "/=")? {
let result = (0..nums.len()).all(|i| (i + 1..nums.len()).all(|j| nums[i] != nums[j]));
validate_valued_args(symbols, args, &resolved, "/=")?;
Err(Error::Compile("/= expects numeric arguments".to_string()))
pub(in crate::compiler::native::comparison) fn eql(
eval_identity(symbols, args, "EQL")
pub(in crate::compiler::native::comparison) fn equal(
eval_identity(symbols, args, "EQUAL")
fn eval_identity(symbols: &mut SymbolTable, args: &[Expr], name: &str) -> Result<Expr> {
let [first, second] = args else {
return Err(Error::Arity {
name: name.to_string(),
expected: 2,
actual: args.len(),
});
let a = eval_value(symbols, first)?;
let b = eval_value(symbols, second)?;
let same = same_datum(&a, &b, name)?;
if a.is_wasm_runtime() || b.is_wasm_runtime() {
Ok(bool_result(same))
fn compile_num_cmp(
let resolved = resolve_all(symbols, args)?;
compile_args_for_effect(ctx, emit, symbols, args)?;
emit_bool(ctx, emit, result)?;
return Ok(());
let ty = compile_cmp_to_stack_by_name(ctx, emit, symbols, args, name, cmp)?;
serialize_stack_to_output(ctx, emit, ty)?;
Ok(())
pub(in crate::compiler::native::comparison) fn compile_num_neq(
let ty = compile_neq_to_stack(ctx, emit, symbols, args)?;
pub(in crate::compiler::native::comparison) fn compile_eql(
let ty = compile_eql_to_stack(ctx, emit, symbols, args)?;
serialize_stack_to_output(ctx, emit, ty)
pub(in crate::compiler::native::comparison) fn compile_equal(
let ty = compile_equal_to_stack(ctx, emit, symbols, args)?;