1
use super::FORMS;
2
use crate::ast::{Expr, 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::SymbolTable;
8

            
9
19
fn form(name: &str) -> &'static crate::compiler::special::SpecialFormSpec {
10
19
    FORMS
11
19
        .iter()
12
27
        .find(|spec| spec.name == name)
13
19
        .expect("registered case form")
14
19
}
15

            
16
19
fn args(sources: &[&str]) -> Vec<Expr> {
17
19
    sources
18
19
        .iter()
19
46
        .map(|s| Reader::parse_expr(s).unwrap())
20
        .collect()
21
19
}
22

            
23
13
fn eval(name: &str, sources: &[&str]) -> Result<Expr> {
24
13
    let mut symbols = SymbolTable::with_builtins();
25
13
    (form(name).eval)(&mut symbols, &args(sources))
26
13
}
27

            
28
5
fn value(source: &str) -> Expr {
29
5
    Reader::parse_expr(source).unwrap()
30
5
}
31

            
32
#[test]
33
1
fn case_answers_the_first_matching_clause() {
34
1
    assert_eq!(
35
1
        eval("CASE", &["2", "(1 10)", "(2 20)", "(2 30)"]).unwrap(),
36
1
        value("20")
37
    );
38
1
}
39

            
40
#[test]
41
1
fn case_matches_any_key_of_a_key_list() {
42
1
    assert_eq!(
43
1
        eval("CASE", &[":b", "((:a :b) 1)", "(t 9)"]).unwrap(),
44
1
        value("1")
45
    );
46
1
}
47

            
48
#[test]
49
1
fn case_keys_are_data_not_variables() {
50
1
    assert_eq!(eval("CASE", &["'a", "(a 1)", "(t 9)"]).unwrap(), value("1"));
51
1
}
52

            
53
#[test]
54
1
fn case_falls_to_t_or_otherwise_then_to_nil() {
55
1
    assert_eq!(
56
1
        eval("CASE", &["5", "(1 10)", "(otherwise 9)"]).unwrap(),
57
1
        value("9")
58
    );
59
1
    assert_eq!(eval("CASE", &["5", "(1 10)"]).unwrap(), Expr::Nil);
60
1
}
61

            
62
#[test]
63
1
fn a_clause_without_forms_answers_nil() {
64
1
    assert_eq!(eval("CASE", &["1", "(1)"]).unwrap(), Expr::Nil);
65
1
}
66

            
67
#[test]
68
1
fn a_default_clause_must_be_last_and_not_in_ecase() {
69
1
    for (name, clauses) in [
70
1
        ("CASE", ["(t 1)", "(2 3)"]),
71
1
        ("ECASE", ["(1 2)", "(t 3)"]),
72
1
        ("ECASE", ["(otherwise 3)", "(1 2)"]),
73
1
    ] {
74
3
        let err = eval(name, &["1", clauses[0], clauses[1]]);
75
3
        assert!(
76
3
            matches!(err, Err(Error::Compile(ref m)) if m.contains("last clause")),
77
            "{name} {clauses:?}"
78
        );
79
    }
80
1
}
81

            
82
#[test]
83
1
fn a_malformed_form_is_refused() {
84
1
    assert!(matches!(
85
1
        eval("CASE", &[]),
86
        Err(Error::Arity { expected: 1, .. })
87
    ));
88
1
    assert!(matches!(
89
1
        eval("ECASE", &["1", "2"]),
90
1
        Err(Error::Compile(ref m)) if m.contains("clause must be")
91
    ));
92
1
}
93

            
94
#[test]
95
1
fn ecase_without_a_match_is_a_raise_not_a_value() {
96
1
    let raised = eval("ECASE", &["5", "(1 1)"]).unwrap();
97
1
    assert!(raised.is_wasm_runtime());
98
1
}
99

            
100
#[test]
101
1
fn ecase_with_a_match_answers_the_clause() {
102
1
    assert_eq!(
103
1
        eval("ECASE", &["2", "(1 10)", "((2 3) 20)"]).unwrap(),
104
1
        value("20")
105
    );
106
1
}
107

            
108
2
fn lower(name: &str, sources: &[&str]) -> Result<WasmType> {
109
2
    let mut ctx = CompileContext::new_with_host_fns(&[])?;
110
2
    let mut emit = FunctionEmitter::new();
111
2
    let mut symbols = SymbolTable::with_builtins();
112
2
    let stack = form(name).stack.expect("case forms lower to the stack");
113
2
    let ty = stack(&mut ctx, &mut emit, &mut symbols, &args(sources))?;
114
2
    emit.finish(&[])?;
115
2
    Ok(ty)
116
2
}
117

            
118
#[test]
119
1
fn a_runtime_key_lowers_to_the_clause_type() {
120
1
    assert_eq!(
121
1
        lower("CASE", &["(progn (print 1) :a)", "(:a 1)", "(t 2)"]).unwrap(),
122
1
        WasmType::I32
123
    );
124
1
    assert_eq!(
125
1
        lower("ECASE", &["(progn (print 1) :a)", "(:a 1)"]).unwrap(),
126
1
        WasmType::I32
127
    );
128
1
}
129

            
130
#[test]
131
1
fn the_effect_and_compile_paths_lower_too() {
132
1
    for name in ["CASE", "ECASE"] {
133
2
        for path in [form(name).compile, form(name).effect.expect("effect path")] {
134
4
            let mut ctx = CompileContext::new_with_host_fns(&[]).unwrap();
135
4
            let mut emit = FunctionEmitter::new();
136
4
            let mut symbols = SymbolTable::with_builtins();
137
4
            path(&mut ctx, &mut emit, &mut symbols, &args(&["1", "(1 2)"])).unwrap();
138
4
            emit.finish(&[]).unwrap();
139
        }
140
    }
141
1
}