Lines
99.23 %
Functions
100 %
Branches
use super::{compile_the_to_stack, the_eval, the_result_type};
use crate::ast::{Expr, Fraction, Number, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::error::{Error, Result};
use crate::reader::Reader;
use crate::runtime::DOTIMES_COUNT;
use crate::runtime::SymbolTable;
fn args(target: &str, form: Expr) -> Vec<Expr> {
vec![Expr::Symbol(target.to_string()), form]
}
fn eval(target: &str, form: Expr) -> Result<Expr> {
the_eval(&mut SymbolTable::with_builtins(), &args(target, form))
fn int(n: i64) -> Expr {
Expr::Number(Number::Index(n))
#[test]
fn a_static_operand_of_the_target_type_is_itself() {
assert_eq!(eval("index", int(5)).unwrap(), int(5));
assert_eq!(
eval("scalar", Expr::Number(Number::Scalar(Fraction::new(1, 2)))).unwrap(),
Expr::Number(Number::Scalar(Fraction::new(1, 2)))
);
let text = Expr::String("a".to_string());
assert_eq!(eval("string", text.clone()).unwrap(), text);
fn an_integer_under_scalar_is_a_runtime_scalar() {
eval("scalar", int(3)).unwrap(),
Expr::WasmRuntime(WasmType::Ratio)
fn an_any_operand_narrows_to_the_target_type() {
for (target, ty) in [
("index", WasmType::I32),
("scalar", WasmType::Ratio),
("money", WasmType::Commodity),
("string", WasmType::StringRef),
] {
eval(target, Expr::WasmRuntime(WasmType::AnyRef)).unwrap(),
Expr::WasmRuntime(ty),
"{target}"
fn a_static_operand_of_another_type_is_a_compile_error() {
let cases = [
("index", Expr::String("a".to_string())),
("index", Expr::Number(Number::Scalar(Fraction::new(1, 2)))),
("money", int(1)),
("scalar", Expr::WasmRuntime(WasmType::I32)),
("index", Expr::Nil),
];
for (target, form) in cases {
let err = eval(target, form);
assert!(
matches!(err, Err(Error::Dimension(ref m)) if m.contains("never converts")),
"{target}: {err:?}"
fn a_number_under_string_is_a_plain_compile_error() {
let err = eval("string", int(5));
matches!(err, Err(Error::Compile(ref m)) if m.contains("never converts")),
"{err:?}"
fn an_unknown_type_names_the_four() {
let err = eval("integer", int(5));
matches!(err, Err(Error::Compile(ref m)) if m.contains("index, scalar, money, string"))
let not_a_symbol = the_eval(&mut SymbolTable::with_builtins(), &[int(1), int(2)]);
assert!(matches!(not_a_symbol, Err(Error::Compile(_))));
fn a_wrong_arity_is_an_arity_error() {
let mut symbols = SymbolTable::with_builtins();
assert!(matches!(
the_eval(&mut symbols, &[Expr::Symbol("index".to_string())]),
Err(Error::Arity { expected: 2, .. })
));
fn the_result_type_is_the_target() {
for (src, ty) in [
("(the index x)", Some(WasmType::I32)),
("(THE Scalar x)", Some(WasmType::Ratio)),
("(the money x)", Some(WasmType::Commodity)),
("(the string x)", Some(WasmType::StringRef)),
("(the integer x)", None),
("(the index)", None),
("(other index x)", None),
let Expr::List(elems) = Reader::parse_expr(src).unwrap() else {
panic!("{src} is not a list");
};
assert_eq!(the_result_type(&elems), ty, "{src}");
fn lower(target: &str, ty: WasmType) -> Result<WasmType> {
let mut ctx = CompileContext::new_with_host_fns(&[])?;
let mut emit = FunctionEmitter::new();
let local = ctx.alloc_local(ty)?;
let form = Expr::WasmLocal(local, ty);
let lowered = compile_the_to_stack(
&mut ctx,
&mut emit,
&mut SymbolTable::with_builtins(),
&args(target, form),
)?;
emit.finish(&[])?;
Ok(lowered)
fn an_any_local_lowers_to_the_target_type() {
assert_eq!(lower(target, WasmType::AnyRef).unwrap(), ty, "{target}");
fn a_local_already_of_the_target_type_lowers_unchanged() {
assert_eq!(lower("index", WasmType::I32).unwrap(), WasmType::I32);
lower("string", WasmType::StringRef).unwrap(),
WasmType::StringRef
fn a_local_of_another_type_is_refused() {
assert!(lower("scalar", WasmType::I32).is_err());
assert!(lower("index", WasmType::Ratio).is_err());
fn every_target_downcasts_as_its_list_element() {
use super::Target;
use crate::ast::PairElement;
for target in [Target::Index, Target::Scalar, Target::Money, Target::String] {
assert_eq!(target.element().as_wasm_type(), target.wasm());
assert_eq!(Target::Index.element(), PairElement::I32);
fn scalar() -> Expr {
fn a_dotimes_count_refusal_names_the_count() {
let refusal = eval(DOTIMES_COUNT, scalar()).expect_err("a scalar is not an index");
refusal.to_string(),
"Compile error: DOTIMES count expects an index value, got ratio"
fn a_user_the_keeps_its_own_wording() {
let refusal = eval("index", scalar()).expect_err("a scalar is not an index");
"Compile error: THE index expects an index value, got ratio; THE narrows, it never converts"