1
use super::{compile_the_to_stack, the_eval, the_result_type};
2
use crate::ast::{Expr, Fraction, Number, WasmType};
3
use crate::compiler::context::CompileContext;
4
use crate::compiler::emit::FunctionEmitter;
5
use crate::error::{Error, Result};
6
use crate::reader::Reader;
7
use crate::runtime::DOTIMES_COUNT;
8
use crate::runtime::SymbolTable;
9

            
10
25
fn args(target: &str, form: Expr) -> Vec<Expr> {
11
25
    vec![Expr::Symbol(target.to_string()), form]
12
25
}
13

            
14
17
fn eval(target: &str, form: Expr) -> Result<Expr> {
15
17
    the_eval(&mut SymbolTable::with_builtins(), &args(target, form))
16
17
}
17

            
18
8
fn int(n: i64) -> Expr {
19
8
    Expr::Number(Number::Index(n))
20
8
}
21

            
22
#[test]
23
1
fn a_static_operand_of_the_target_type_is_itself() {
24
1
    assert_eq!(eval("index", int(5)).unwrap(), int(5));
25
1
    assert_eq!(
26
1
        eval("scalar", Expr::Number(Number::Scalar(Fraction::new(1, 2)))).unwrap(),
27
1
        Expr::Number(Number::Scalar(Fraction::new(1, 2)))
28
    );
29
1
    let text = Expr::String("a".to_string());
30
1
    assert_eq!(eval("string", text.clone()).unwrap(), text);
31
1
}
32

            
33
#[test]
34
1
fn an_integer_under_scalar_is_a_runtime_scalar() {
35
1
    assert_eq!(
36
1
        eval("scalar", int(3)).unwrap(),
37
1
        Expr::WasmRuntime(WasmType::Ratio)
38
    );
39
1
}
40

            
41
#[test]
42
1
fn an_any_operand_narrows_to_the_target_type() {
43
1
    for (target, ty) in [
44
1
        ("index", WasmType::I32),
45
1
        ("scalar", WasmType::Ratio),
46
1
        ("money", WasmType::Commodity),
47
1
        ("string", WasmType::StringRef),
48
1
    ] {
49
4
        assert_eq!(
50
4
            eval(target, Expr::WasmRuntime(WasmType::AnyRef)).unwrap(),
51
4
            Expr::WasmRuntime(ty),
52
            "{target}"
53
        );
54
    }
55
1
}
56

            
57
#[test]
58
1
fn a_static_operand_of_another_type_is_a_compile_error() {
59
1
    let cases = [
60
1
        ("index", Expr::String("a".to_string())),
61
1
        ("index", Expr::Number(Number::Scalar(Fraction::new(1, 2)))),
62
1
        ("money", int(1)),
63
1
        ("scalar", Expr::WasmRuntime(WasmType::I32)),
64
1
        ("index", Expr::Nil),
65
1
    ];
66
1
    for (target, form) in cases {
67
5
        let err = eval(target, form);
68
5
        assert!(
69
5
            matches!(err, Err(Error::Dimension(ref m)) if m.contains("never converts")),
70
            "{target}: {err:?}"
71
        );
72
    }
73
1
}
74

            
75
#[test]
76
1
fn a_number_under_string_is_a_plain_compile_error() {
77
1
    let err = eval("string", int(5));
78
1
    assert!(
79
1
        matches!(err, Err(Error::Compile(ref m)) if m.contains("never converts")),
80
        "{err:?}"
81
    );
82
1
}
83

            
84
#[test]
85
1
fn an_unknown_type_names_the_four() {
86
1
    let err = eval("integer", int(5));
87
1
    assert!(
88
1
        matches!(err, Err(Error::Compile(ref m)) if m.contains("index, scalar, money, string"))
89
    );
90
1
    let not_a_symbol = the_eval(&mut SymbolTable::with_builtins(), &[int(1), int(2)]);
91
1
    assert!(matches!(not_a_symbol, Err(Error::Compile(_))));
92
1
}
93

            
94
#[test]
95
1
fn a_wrong_arity_is_an_arity_error() {
96
1
    let mut symbols = SymbolTable::with_builtins();
97
1
    assert!(matches!(
98
1
        the_eval(&mut symbols, &[Expr::Symbol("index".to_string())]),
99
        Err(Error::Arity { expected: 2, .. })
100
    ));
101
1
}
102

            
103
#[test]
104
1
fn the_result_type_is_the_target() {
105
1
    for (src, ty) in [
106
1
        ("(the index x)", Some(WasmType::I32)),
107
1
        ("(THE Scalar x)", Some(WasmType::Ratio)),
108
1
        ("(the money x)", Some(WasmType::Commodity)),
109
1
        ("(the string x)", Some(WasmType::StringRef)),
110
1
        ("(the integer x)", None),
111
1
        ("(the index)", None),
112
1
        ("(other index x)", None),
113
1
    ] {
114
7
        let Expr::List(elems) = Reader::parse_expr(src).unwrap() else {
115
            panic!("{src} is not a list");
116
        };
117
7
        assert_eq!(the_result_type(&elems), ty, "{src}");
118
    }
119
1
}
120

            
121
8
fn lower(target: &str, ty: WasmType) -> Result<WasmType> {
122
8
    let mut ctx = CompileContext::new_with_host_fns(&[])?;
123
8
    let mut emit = FunctionEmitter::new();
124
8
    let local = ctx.alloc_local(ty)?;
125
8
    let form = Expr::WasmLocal(local, ty);
126
8
    let lowered = compile_the_to_stack(
127
8
        &mut ctx,
128
8
        &mut emit,
129
8
        &mut SymbolTable::with_builtins(),
130
8
        &args(target, form),
131
8
    )?;
132
6
    emit.finish(&[])?;
133
6
    Ok(lowered)
134
8
}
135

            
136
#[test]
137
1
fn an_any_local_lowers_to_the_target_type() {
138
1
    for (target, ty) in [
139
1
        ("index", WasmType::I32),
140
1
        ("scalar", WasmType::Ratio),
141
1
        ("money", WasmType::Commodity),
142
1
        ("string", WasmType::StringRef),
143
1
    ] {
144
4
        assert_eq!(lower(target, WasmType::AnyRef).unwrap(), ty, "{target}");
145
    }
146
1
}
147

            
148
#[test]
149
1
fn a_local_already_of_the_target_type_lowers_unchanged() {
150
1
    assert_eq!(lower("index", WasmType::I32).unwrap(), WasmType::I32);
151
1
    assert_eq!(
152
1
        lower("string", WasmType::StringRef).unwrap(),
153
1
        WasmType::StringRef
154
    );
155
1
}
156

            
157
#[test]
158
1
fn a_local_of_another_type_is_refused() {
159
1
    assert!(lower("scalar", WasmType::I32).is_err());
160
1
    assert!(lower("index", WasmType::Ratio).is_err());
161
1
}
162

            
163
#[test]
164
1
fn every_target_downcasts_as_its_list_element() {
165
    use super::Target;
166
    use crate::ast::PairElement;
167
1
    for target in [Target::Index, Target::Scalar, Target::Money, Target::String] {
168
4
        assert_eq!(target.element().as_wasm_type(), target.wasm());
169
    }
170
1
    assert_eq!(Target::Index.element(), PairElement::I32);
171
1
}
172

            
173
2
fn scalar() -> Expr {
174
2
    Expr::WasmRuntime(WasmType::Ratio)
175
2
}
176

            
177
#[test]
178
1
fn a_dotimes_count_refusal_names_the_count() {
179
1
    let refusal = eval(DOTIMES_COUNT, scalar()).expect_err("a scalar is not an index");
180
1
    assert_eq!(
181
1
        refusal.to_string(),
182
1
        "Compile error: DOTIMES count expects an index value, got ratio"
183
    );
184
1
}
185

            
186
#[test]
187
1
fn a_user_the_keeps_its_own_wording() {
188
1
    let refusal = eval("index", scalar()).expect_err("a scalar is not an index");
189
1
    assert_eq!(
190
1
        refusal.to_string(),
191
1
        "Compile error: THE index expects an index value, got ratio; THE narrows, it never converts"
192
    );
193
1
}