Lines
99.08 %
Functions
100 %
Branches
use super::{Sign, compile_sign, sign_eval};
use crate::ast::{Expr, Fraction, Number, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::fold_value;
use crate::error::{Error, Result};
use crate::reader::Reader;
use crate::runtime::SymbolTable;
const SIGNS: [Sign; 3] = [Sign::Zero, Sign::Plus, Sign::Minus];
fn eval(sign: Sign, operand: Expr) -> Result<Expr> {
sign_eval(&mut SymbolTable::with_builtins(), &[operand], sign)
}
fn number(numer: i64, denom: i64) -> Expr {
Expr::Number(Number::Scalar(Fraction::new(numer, denom)))
fn truth(value: bool) -> Expr {
if value { Expr::T } else { Expr::Nil }
#[test]
fn a_constant_folds_to_its_sign() -> Result<()> {
for (numer, denom, zero, plus, minus) in [
(-2, 1, false, false, true),
(0, 1, true, false, false),
(2, 1, false, true, false),
(-1, 2, false, false, true),
(1, 2, false, true, false),
] {
for (sign, want) in SIGNS.into_iter().zip([zero, plus, minus]) {
assert_eq!(
eval(sign, number(numer, denom))?,
truth(want),
"{sign:?} {numer}/{denom}"
);
Ok(())
fn a_runtime_number_answers_a_runtime_boolean() -> Result<()> {
for sign in SIGNS {
for ty in [WasmType::I32, WasmType::Ratio, WasmType::Commodity] {
eval(sign, Expr::WasmRuntime(ty))?,
Expr::WasmRuntime(WasmType::Bool),
"{sign:?} {ty}"
fn compile_error(result: Result<impl std::fmt::Debug>) -> String {
match result {
Err(Error::Compile(message)) => message,
other => format!("not a compile error: {other:?}"),
fn an_any_operand_must_be_narrowed_with_the() {
let message = compile_error(eval(sign, Expr::WasmRuntime(WasmType::AnyRef)));
assert!(message.contains("got any; narrow it with THE"), "{message}");
fn a_non_number_is_a_compile_error() {
for operand in [
Expr::T,
Expr::String("a".to_string()),
Expr::WasmRuntime(WasmType::StringRef),
let message = compile_error(eval(Sign::Zero, operand));
assert!(
message.starts_with("zerop expects an index, scalar or money, got "),
"{message}"
fn a_wrong_arity_is_an_arity_error() {
for args in [vec![], vec![number(1, 1), number(2, 1)]] {
let result = sign_eval(&mut SymbolTable::with_builtins(), &args, Sign::Plus);
matches!(result, Err(Error::Arity { expected: 1, .. })),
"{result:?}"
fn lower(sign: Sign, ty: WasmType) -> Result<WasmType> {
let mut ctx = CompileContext::new_with_host_fns(&[])?;
let mut emit = FunctionEmitter::new();
let local = ctx.alloc_local(ty)?;
let lowered = compile_sign(
&mut ctx,
&mut emit,
&mut SymbolTable::with_builtins(),
&[Expr::WasmLocal(local, ty)],
sign,
)?;
emit.finish(&[])?;
Ok(lowered)
fn every_numeric_local_lowers_to_a_boolean() -> Result<()> {
assert_eq!(lower(sign, ty)?, WasmType::Bool, "{sign:?} {ty}");
fn a_local_of_another_type_is_refused() {
let message = compile_error(lower(Sign::Minus, WasmType::AnyRef));
assert!(message.contains("narrow it with THE"), "{message}");
let message = compile_error(lower(Sign::Minus, WasmType::Bool));
message,
"minusp expects an index, scalar or money, got bool"
fn a_constant_call_folds_on_the_codegen_surface() -> Result<()> {
let mut symbols = SymbolTable::with_builtins();
for (source, want) in [
("(zerop 0)", Some(Expr::T)),
("(plusp 1/2)", Some(Expr::T)),
("(minusp (- 3 5))", Some(Expr::T)),
("(plusp 0)", Some(Expr::Nil)),
("(zerop \"a\")", None),
("(zerop)", None),
let before = symbols.state_digest();
fold_value(&mut symbols, &Reader::parse_expr(source)?)?,
want,
"{source}"
strip_charge(&before),
strip_charge(&symbols.state_digest()),
"{source} changed the table"
fn strip_charge(digest: &str) -> Vec<&str> {
digest
.lines()
.filter(|line| !line.starts_with("charged:"))
.collect()