Lines
98.96 %
Functions
50 %
Branches
100 %
//! `ZEROP`, `PLUSP`, `MINUSP` (CL): the sign of an index, scalar or money.
//! An index is tested on its i32; a scalar or money on its numerator, since a
//! canonical denominator is positive — so money needs no currency and a
//! compound unit term is answered too. An `any` operand must be narrowed with
//! `THE` first.
use super::NativeSpec;
use super::comparison::bool_result;
use super::convert::one_arg;
use crate::ast::{Expr, Number, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::{compile_for_stack, eval_value, fold_call, format_expr, index_literal};
use crate::error::{Error, Result};
use crate::runtime::SymbolTable;
pub(super) const NATIVES: &[NativeSpec] = &[
NativeSpec {
name: "ZEROP",
eval: zerop_eval,
stack: Some(compile_zerop),
effect: None,
},
name: "PLUSP",
eval: plusp_eval,
stack: Some(compile_plusp),
name: "MINUSP",
eval: minusp_eval,
stack: Some(compile_minusp),
];
/// The `$ratio` and `$commodity` structs both keep the numerator in field 0.
const NUMERATOR: u32 = 0;
#[derive(Clone, Copy, Debug)]
enum Sign {
Zero,
Plus,
Minus,
}
impl Sign {
fn name(self) -> &'static str {
match self {
Self::Zero => "ZEROP",
Self::Plus => "PLUSP",
Self::Minus => "MINUSP",
fn holds(self, numerator: i64) -> bool {
Self::Zero => numerator == 0,
Self::Plus => numerator > 0,
Self::Minus => numerator < 0,
/// Replaces the i64 numerator on the stack by the i32 truth value.
fn emit_test(self, emit: &mut FunctionEmitter) {
Self::Zero => emit.i64_eqz(),
Self::Plus => {
emit.i64_const(0);
emit.i64_gt_s();
Self::Minus => {
emit.i64_lt_s();
fn mismatch(self, got: &str) -> Error {
Error::Compile(format!(
"{} expects an index, scalar or money, got {got}",
self.name().to_ascii_lowercase()
))
fn type_error(self, ty: WasmType) -> Error {
match ty {
WasmType::AnyRef => self.mismatch("any; narrow it with THE, e.g. (the scalar x)"),
other => self.mismatch(&other.to_string()),
fn zerop_eval(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
sign_eval(symbols, args, Sign::Zero)
fn plusp_eval(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
sign_eval(symbols, args, Sign::Plus)
fn minusp_eval(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
sign_eval(symbols, args, Sign::Minus)
fn sign_eval(symbols: &mut SymbolTable, args: &[Expr], sign: Sign) -> Result<Expr> {
let value = eval_value(symbols, one_arg(args, sign.name())?)?;
match (&value, value.wasm_type()) {
(Expr::Number(n), _) => {
if let Number::Index(i) = n {
index_literal(*i)?;
Ok(bool_result(sign.holds(*n.value().numer())))
(_, Some(WasmType::I32 | WasmType::Ratio | WasmType::Commodity)) => {
Ok(Expr::WasmRuntime(WasmType::Bool))
(_, Some(ty)) => Err(sign.type_error(ty)),
(_, None) => Err(sign.mismatch(&format_expr(&value))),
fn compile_zerop(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
symbols: &mut SymbolTable,
args: &[Expr],
) -> Result<WasmType> {
compile_sign(ctx, emit, symbols, args, Sign::Zero)
fn compile_plusp(
compile_sign(ctx, emit, symbols, args, Sign::Plus)
fn compile_minusp(
compile_sign(ctx, emit, symbols, args, Sign::Minus)
fn compile_sign(
sign: Sign,
let arg = one_arg(args, sign.name())?;
if let Some(folded) = fold_call(symbols, sign.name(), args)? {
emit.i32_const(i32::from(!folded.is_nil()));
return Ok(WasmType::Bool);
match compile_for_stack(ctx, emit, symbols, arg)? {
WasmType::I32 => emit.i64_extend_i32_s(),
WasmType::Ratio => emit.struct_get(ctx.ids.ty_ratio, NUMERATOR),
WasmType::Commodity => emit.struct_get(ctx.ids.ty_commodity, NUMERATOR),
ty => {
emit.reject_if_live(sign.type_error(ty))?;
sign.emit_test(emit);
Ok(WasmType::Bool)
#[cfg(test)]
#[path = "sign_tests.rs"]
mod tests;