1
use super::*;
2
use crate::ast::LambdaParams;
3

            
4
2
fn lambda(params: &[&str]) -> Expr {
5
2
    let names = params.iter().map(ToString::to_string).collect();
6
2
    Expr::Lambda(LambdaParams::simple(names), Box::new(Expr::Nil))
7
2
}
8

            
9
#[test]
10
1
fn a_closure_of_another_arity_names_both_signatures() {
11
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
12
1
    let sig = ctx
13
1
        .intern_closure_signature(&[WasmType::Ratio], WasmType::Ratio)
14
1
        .expect("sig");
15
1
    let err = cell_store_error(&ctx, "F", &lambda(&["a", "b"]), WasmType::Closure(sig));
16
1
    assert_eq!(
17
1
        err.to_string(),
18
1
        "Compile error: F: store of a 2-parameter closure into a closure (ratio) -> ratio cell"
19
    );
20
1
}
21

            
22
#[test]
23
1
fn a_closure_place_is_described_by_its_signature() {
24
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
25
1
    let sig = ctx
26
1
        .intern_closure_signature(&[], WasmType::Ratio)
27
1
        .expect("sig");
28
1
    let stored = Expr::WasmLocal(3, WasmType::Closure(sig));
29
1
    let err = cell_store_error(&ctx, "F", &stored, WasmType::I32);
30
1
    assert_eq!(
31
1
        err.to_string(),
32
1
        "Compile error: F: store of a closure () -> ratio into an Index cell"
33
    );
34
1
}
35

            
36
#[test]
37
1
fn a_number_into_a_closure_cell_names_the_closure() {
38
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
39
1
    let sig = ctx
40
1
        .intern_closure_signature(&[], WasmType::Ratio)
41
1
        .expect("sig");
42
1
    let err = cell_store_error(
43
1
        &ctx,
44
1
        "F",
45
1
        &Expr::Number(Number::Index(5.into())),
46
1
        WasmType::Closure(sig),
47
1
    );
48
1
    assert_eq!(
49
1
        err.to_string(),
50
1
        "Compile error: F: store of a number into a closure () -> ratio cell"
51
    );
52
1
}
53

            
54
#[test]
55
1
fn an_untyped_value_says_so() {
56
1
    let ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
57
1
    let stored = Expr::WasmRuntime(WasmType::AnyRef);
58
1
    let err = cell_store_error(&ctx, "F", &stored, WasmType::I32);
59
1
    assert_eq!(
60
1
        err.to_string(),
61
1
        "Compile error: F: store of a value of unknown type into an Index cell"
62
    );
63
1
}
64

            
65
#[test]
66
1
fn a_closure_place_of_another_signature_names_both_types() {
67
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
68
1
    let stored = ctx
69
1
        .intern_closure_signature(&[WasmType::I32], WasmType::Ratio)
70
1
        .expect("sig");
71
1
    let cell = ctx
72
1
        .intern_closure_signature(&[WasmType::Ratio], WasmType::Ratio)
73
1
        .expect("sig");
74
1
    let err = cell_store_error(
75
1
        &ctx,
76
1
        "F",
77
1
        &Expr::WasmLocal(3, WasmType::Closure(stored)),
78
1
        WasmType::Closure(cell),
79
1
    );
80
1
    assert_eq!(
81
1
        err.to_string(),
82
1
        "Compile error: F: store of a closure (i32) -> ratio into a closure (ratio) -> ratio cell"
83
    );
84
1
}
85

            
86
#[test]
87
1
fn a_lambda_of_the_same_arity_is_another_signature() {
88
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
89
1
    let cell = ctx
90
1
        .intern_closure_signature(&[WasmType::Ratio, WasmType::Ratio], WasmType::Ratio)
91
1
        .expect("sig");
92
1
    let err = cell_store_error(&ctx, "F", &lambda(&["a", "b"]), WasmType::Closure(cell));
93
1
    assert_eq!(
94
1
        err.to_string(),
95
1
        "Compile error: F: store of a closure of another signature into a closure (ratio, ratio) -> ratio cell"
96
    );
97
1
}
98

            
99
#[test]
100
1
fn parameter_count_counts_every_section() {
101
1
    let mut params = LambdaParams::simple(vec!["a".to_string()]);
102
1
    params.optional.push(("b".to_string(), None));
103
1
    params.rest = Some("r".to_string());
104
1
    assert_eq!(parameter_count(&params), 3);
105
1
}
106

            
107
4
fn compile_eval(source: &str) -> Result<()> {
108
4
    let program = crate::reader::Reader::parse(source).expect("parses");
109
4
    let mut symbols = SymbolTable::with_builtins();
110
4
    crate::compiler::Compiler::new()
111
4
        .compile_eval_with_type(&program, &mut symbols)
112
4
        .map(drop)
113
4
}
114

            
115
#[test]
116
1
fn a_store_that_fails_for_another_reason_reports_that_reason() {
117
1
    let err = compile_eval("(defvar n 0) (setf n (entity-count)) n").expect_err("no import");
118
1
    assert!(err.to_string().contains("is not registered"), "{err}");
119
1
}
120

            
121
#[test]
122
1
fn a_store_of_another_kind_into_a_closure_cell_is_the_cell_error() {
123
1
    let err = compile_eval("(defvar f (lambda () 1)) (setf f 5) f").expect_err("refused");
124
1
    assert!(err.to_string().contains("cell"), "{err}");
125
1
}
126

            
127
#[test]
128
1
fn a_list_of_closures_stored_into_a_global_rides_an_any_home() {
129
1
    compile_eval("(defvar l nil) (setf l (list (lambda () 1))) (length l)").expect("compiles");
130
1
}
131

            
132
2
fn compiled_type_of(source: &str) -> Option<WasmType> {
133
2
    let program = crate::reader::Reader::parse(source).expect("parses");
134
2
    let mut symbols = SymbolTable::with_builtins();
135
2
    crate::compiler::Compiler::new()
136
2
        .compile_eval_with_type(&program, &mut symbols)
137
2
        .expect("compiles")
138
2
        .1
139
2
}
140

            
141
#[test]
142
1
fn a_store_is_typed_by_what_codegen_pushed() {
143
1
    let source = "(defvar g nil) (defun mk () (list 1 2)) (setf g (mk)) (car g)";
144
1
    assert_eq!(compiled_type_of(source), Some(WasmType::I32));
145
1
}
146

            
147
#[test]
148
1
fn a_store_that_reads_its_own_narrow_home_is_typed_at_the_init() {
149
1
    let source = "(defvar total 0/1) (defun add (a) (setf total (+ total a))) (add 1/2) total";
150
1
    assert_eq!(compiled_type_of(source), Some(WasmType::Ratio));
151
1
}
152

            
153
#[test]
154
1
fn a_store_that_adds_a_scalar_to_an_index_home_is_a_mix() {
155
    let err =
156
1
        compile_eval("(defvar total 0) (defun add (a) (setf total (+ total a))) (add 1/2) total")
157
1
            .expect_err("a mix, CL 1/2");
158
1
    assert!(
159
1
        err.to_string()
160
1
            .contains("+ cannot mix an index (count) with a scalar"),
161
        "{err}"
162
    );
163
1
}