1
use super::shared::{
2
    fully_macro_expanded, is_closure_factory_call, mentions, mentions_expanded, reads_free,
3
    used_as_function,
4
};
5
use crate::ast::{Expr, LambdaParams};
6
use crate::reader::Reader;
7
use crate::runtime::{Symbol, SymbolKind, SymbolTable};
8

            
9
120
fn form(source: &str) -> Expr {
10
120
    Reader::parse(source).expect("parse").exprs.remove(0)
11
120
}
12

            
13
6
fn function(body: &str) -> Expr {
14
6
    Expr::Lambda(LambdaParams::simple(Vec::new()), Box::new(form(body)))
15
6
}
16

            
17
13
fn table_with_macro(name: &str, body: Expr) -> SymbolTable {
18
13
    let mut symbols = SymbolTable::with_builtins_for_wasm();
19
13
    let params = LambdaParams::simple(Vec::new());
20
13
    symbols.define(
21
13
        Symbol::new(name, SymbolKind::Macro).with_function(Expr::Lambda(params, Box::new(body))),
22
13
    );
23
13
    symbols
24
13
}
25

            
26
#[test]
27
1
fn a_template_macro_use_is_read_through_its_expansion() {
28
1
    let symbols = table_with_macro("CALLG", Expr::Quote(Box::new(form("(g)"))));
29
1
    let reader = function("(callg)");
30
1
    assert!(reads_free(&symbols, &reader, "G").expect("scan"));
31
1
    assert!(!reads_free(&symbols, &reader, "H").expect("scan"));
32
1
}
33

            
34
#[test]
35
1
fn an_unexpandable_macro_use_reads_everything() {
36
1
    let symbols = table_with_macro("OPAQUE", form("(car 1 2)"));
37
1
    assert!(reads_free(&symbols, &function("(opaque)"), "ANY").expect("scan"));
38
1
}
39

            
40
#[test]
41
1
fn a_computed_macro_expansion_reads_only_what_it_mentions() {
42
1
    let symbols = table_with_macro("OPAQUE", form("(list 'g)"));
43
1
    assert!(reads_free(&symbols, &function("(opaque)"), "G").expect("scan"));
44
1
    assert!(!reads_free(&symbols, &function("(opaque)"), "H").expect("scan"));
45
1
}
46

            
47
#[test]
48
1
fn a_binding_spec_or_clause_named_like_a_macro_is_not_a_use() {
49
1
    let mut symbols = table_with_macro("M", form("1"));
50
1
    let params = LambdaParams::simple(vec!["A".to_string()]);
51
1
    symbols.define(
52
1
        Symbol::new("M", SymbolKind::Macro)
53
1
            .with_function(Expr::Lambda(params, Box::new(form("1")))),
54
1
    );
55
1
    for text in [
56
1
        "(let ((m c)) m)",
57
1
        "(case k (m c) (t 0))",
58
1
        "(case k ((m) c))",
59
1
        "(let* ((m c)) m)",
60
1
        "(dolist (m (list c)) m)",
61
1
        "(do ((m c (+ m 1))) ((> m 0) m))",
62
1
        "(do* ((m 0 (+ m c))) ((> m 0) m))",
63
1
        "(do ((i 0 (+ i 1))) (m c))",
64
1
        "(do () (m c))",
65
1
        "(do* () (m c))",
66
1
        "(cond (m c) (t 0))",
67
1
        "(handler-case (error 'e \"x\") (m (e) c))",
68
1
        "(labels ((m (y) (+ y c))) (m 1))",
69
1
    ] {
70
13
        assert!(
71
13
            mentions_expanded(&symbols, &form(text), "C").expect("scan"),
72
            "{text}"
73
        );
74
    }
75
1
}
76

            
77
#[test]
78
1
fn a_macro_in_a_lambda_list_default_is_expanded() {
79
1
    let symbols = table_with_macro("GETC", Expr::Quote(Box::new(form("c"))));
80
1
    assert!(
81
1
        mentions_expanded(&symbols, &form("(lambda (&optional (y (getc))) y)"), "C").expect("scan")
82
    );
83
1
}
84

            
85
#[test]
86
1
fn a_labels_form_hides_its_function_only_inside_itself() {
87
1
    let symbols = SymbolTable::with_builtins_for_wasm();
88
1
    let inside = function("(labels ((g () 3)) (g))");
89
1
    assert!(!reads_free(&symbols, &inside, "G").expect("scan"));
90
1
    let after = function("(progn (labels ((g () 3)) (g)) (g))");
91
1
    assert!(reads_free(&symbols, &after, "G").expect("scan"));
92
1
}
93

            
94
#[test]
95
1
fn a_macro_use_mentions_what_it_expands_to() {
96
1
    let symbols = table_with_macro("M", Expr::Quote(Box::new(form("x"))));
97
1
    assert!(mentions_expanded(&symbols, &form("(m)"), "X").expect("scan"));
98
1
    assert!(!mentions_expanded(&symbols, &form("(m)"), "Y").expect("scan"));
99
1
}
100

            
101
#[test]
102
1
fn a_computed_macro_expansion_is_scanned() {
103
1
    let symbols = table_with_macro("ADD-X", form("(list '+ 'x 1)"));
104
1
    assert!(mentions_expanded(&symbols, &form("(add-x)"), "X").expect("scan"));
105
1
}
106

            
107
#[test]
108
1
fn an_unexpandable_macro_use_mentions_every_name() {
109
1
    let symbols = table_with_macro("BAD", form("(car 1 2)"));
110
1
    assert!(mentions_expanded(&symbols, &form("(bad)"), "ANY").expect("scan"));
111
1
}
112

            
113
#[test]
114
1
fn a_macro_use_in_an_unquote_is_expanded() {
115
1
    let symbols = table_with_macro("M", Expr::Quote(Box::new(form("x"))));
116
1
    assert!(mentions_expanded(&symbols, &form("`(a ,(m) b)"), "X").expect("scan"));
117
1
    assert!(mentions_expanded(&symbols, &form("`(a ,@(m) b)"), "X").expect("scan"));
118
1
}
119

            
120
#[test]
121
1
fn a_macro_use_in_the_quoted_part_of_a_template_stays_data() {
122
1
    let symbols = table_with_macro("M", Expr::Quote(Box::new(form("x"))));
123
1
    assert!(!mentions_expanded(&symbols, &form("`(a (m) b)"), "X").expect("scan"));
124
1
}
125

            
126
#[test]
127
1
fn a_store_in_an_unquoted_macro_use_makes_a_cell() {
128
1
    let symbols = table_with_macro("M", Expr::Quote(Box::new(form("(setf x 7)"))));
129
1
    let scope = [form("(lambda () x)"), form("`(a ,(m))")];
130
1
    assert!(super::shared::needs_cell(&symbols, "X", &scope).expect("scan"));
131
1
}
132

            
133
#[test]
134
1
fn an_unquote_under_a_quote_in_a_template_is_a_mention() {
135
1
    let symbols = table_with_macro("M", Expr::Quote(Box::new(form("x"))));
136
1
    assert!(mentions_expanded(&symbols, &form("`(a ',x b)"), "X").expect("scan"));
137
1
    assert!(mentions_expanded(&symbols, &form("`(a ',(m))"), "X").expect("scan"));
138
1
}
139

            
140
#[test]
141
1
fn a_quote_outside_a_template_stays_opaque() {
142
1
    let symbols = SymbolTable::with_builtins_for_wasm();
143
1
    assert!(!mentions_expanded(&symbols, &form("'(a ,x)"), "X").expect("scan"));
144
1
}
145

            
146
#[test]
147
1
fn plain_template_text_is_not_a_mention() {
148
1
    let symbols = SymbolTable::with_builtins_for_wasm();
149
1
    assert!(!mentions_expanded(&symbols, &form("`(a x b)"), "X").expect("scan"));
150
1
}
151

            
152
#[test]
153
1
fn a_store_under_a_quote_in_a_template_makes_a_cell() {
154
1
    let symbols = SymbolTable::with_builtins_for_wasm();
155
1
    let scope = [form("(lambda () x)"), form("`(a ',(setf x 7))")];
156
1
    assert!(super::shared::needs_cell(&symbols, "X", &scope).expect("scan"));
157
1
}
158

            
159
#[test]
160
1
fn used_as_function_tells_a_call_from_a_test() {
161
1
    let table = SymbolTable::with_builtins();
162
5
    let used = |text: &str| used_as_function(&table, &form(text), "F");
163
1
    assert!(used("(f 1)").unwrap_or(false));
164
1
    assert!(used("(funcall f 1)").unwrap_or(false));
165
1
    assert!(used("(mapcar f l)").unwrap_or(false));
166
1
    assert!(!used("(if f 1 0)").unwrap_or(true));
167
1
    assert!(!used("(quote (f 1))").unwrap_or(true));
168
1
}
169

            
170
3
fn table_with_function(name: &str, param: &str, body: &str) -> SymbolTable {
171
3
    let mut symbols = SymbolTable::with_builtins_for_wasm();
172
3
    let params = LambdaParams::simple(vec![param.to_string()]);
173
3
    symbols.define(
174
3
        Symbol::new(name, SymbolKind::Function)
175
3
            .with_function(Expr::Lambda(params, Box::new(form(body)))),
176
3
    );
177
3
    symbols
178
3
}
179

            
180
#[test]
181
1
fn a_function_returning_a_lambda_over_its_parameter_is_a_factory() {
182
1
    let symbols = table_with_function("MK", "A", "(lambda (x) (+ x a))");
183
1
    assert!(is_closure_factory_call(&symbols, &form("(mk 3)")).expect("scan"));
184
1
}
185

            
186
#[test]
187
1
fn a_lambda_that_binds_the_parameter_itself_is_no_factory() {
188
1
    let symbols = table_with_function("MK", "X", "(lambda (x) (* x 2))");
189
1
    assert!(!is_closure_factory_call(&symbols, &form("(mk 3)")).expect("scan"));
190
1
}
191

            
192
#[test]
193
1
fn a_function_not_returning_a_lambda_literal_is_no_factory() {
194
1
    let symbols = table_with_function("MK", "A", "(map (lambda (x) (+ x a)) '(1))");
195
1
    assert!(!is_closure_factory_call(&symbols, &form("(mk 3)")).expect("scan"));
196
1
    assert!(!is_closure_factory_call(&symbols, &form("a")).expect("scan"));
197
1
    assert!(!is_closure_factory_call(&symbols, &form("(unknown 3)")).expect("scan"));
198
1
}
199

            
200
#[test]
201
1
fn a_block_name_is_not_a_mention() {
202
1
    let body = Reader::parse_expr("(block f (return-from f (+ a 1)))").expect("parses");
203
1
    assert!(!mentions(&body, "F"));
204
1
    let value = Reader::parse_expr("(block b (return-from b (f 1)))").expect("parses");
205
1
    assert!(mentions(&value, "F"));
206
1
}
207

            
208
19
fn expanded(source: &str) -> Expr {
209
19
    let mut params = LambdaParams::simple(Vec::new());
210
19
    params.rest = Some("ARGS".to_string());
211
19
    let mut symbols = SymbolTable::with_builtins_for_wasm();
212
19
    symbols.define(
213
19
        Symbol::new("DBL", SymbolKind::Macro).with_function(Expr::Lambda(
214
19
            params,
215
19
            Box::new(Expr::Quote(Box::new(form("(* 2 2)")))),
216
19
        )),
217
19
    );
218
19
    fully_macro_expanded(&symbols, &form(source)).expect("expands")
219
19
}
220

            
221
#[test]
222
1
fn a_name_in_a_binding_position_is_not_a_macro_use() {
223
1
    for (source, kept) in [
224
1
        ("(let ((dbl 1)) dbl)", "(let ((dbl 1)) dbl)"),
225
1
        ("(let* ((dbl 1) (z dbl)) z)", "(let* ((dbl 1) (z dbl)) z)"),
226
1
        (
227
1
            "(do ((dbl 0 1)) (dbl dbl) 1)",
228
1
            "(do ((dbl 0 1)) (dbl dbl) 1)",
229
1
        ),
230
1
        ("(dolist (dbl nil dbl) 1)", "(dolist (dbl nil dbl) 1)"),
231
1
        ("(lambda (dbl) 1)", "(lambda (dbl) 1)"),
232
1
        ("(defun f (dbl) 1)", "(defun f (dbl) 1)"),
233
1
        ("(case x (dbl 1) (t 2))", "(case x (dbl 1) (t 2))"),
234
1
        ("(cond (dbl 1) (t 2))", "(cond (dbl 1) (t 2))"),
235
1
        (
236
1
            "(handler-case 1 (dbl (dbl) 2))",
237
1
            "(handler-case 1 (dbl (dbl) 2))",
238
1
        ),
239
1
        ("(labels ((dbl (dbl) 1)) 2)", "(labels ((dbl (dbl) 1)) 2)"),
240
1
    ] {
241
10
        assert_eq!(expanded(source), form(kept), "{source}");
242
    }
243
1
}
244

            
245
#[test]
246
1
fn a_macro_use_in_a_form_position_of_a_binder_is_expanded() {
247
1
    for (source, then) in [
248
1
        ("(let ((a (dbl 1))) (dbl 2))", "(let ((a (* 2 2))) (* 2 2))"),
249
1
        (
250
1
            "(do ((i (dbl 1) (dbl 2))) ((dbl 3)) (dbl 4))",
251
1
            "(do ((i (* 2 2) (* 2 2))) ((* 2 2)) (* 2 2))",
252
1
        ),
253
1
        ("(lambda (a) (dbl 1))", "(lambda (a) (* 2 2))"),
254
1
        ("(case (dbl 1) (k (dbl 2)))", "(case (* 2 2) (k (* 2 2)))"),
255
1
        ("(cond ((dbl 1) (dbl 2)))", "(cond ((* 2 2) (* 2 2)))"),
256
1
        (
257
1
            "(labels ((g (x) (dbl x))) (g 1))",
258
1
            "(labels ((g (x) (* 2 2))) (g 1))",
259
1
        ),
260
1
        ("(defun f (x) (dbl x))", "(defun f (x) (* 2 2))"),
261
1
    ] {
262
7
        assert_eq!(expanded(source), form(then), "{source}");
263
    }
264
1
}
265

            
266
#[test]
267
1
fn a_labels_function_over_a_macro_is_not_expanded_as_the_macro() {
268
1
    assert_eq!(
269
1
        expanded("(labels ((dbl (x) (+ x 1))) (dbl 2))"),
270
1
        form("(labels ((dbl (x) (+ x 1))) (dbl 2))")
271
    );
272
1
    assert_eq!(
273
1
        expanded("(labels ((g (x) x)) (dbl 2))"),
274
1
        form("(labels ((g (x) x)) (* 2 2))")
275
    );
276
1
}
277

            
278
#[test]
279
1
fn a_labels_function_hides_a_macro_of_its_name() {
280
1
    let symbols = table_with_macro("M", form("1"));
281
1
    let shadowed = form("(labels ((m (y) y)) (m c))");
282
1
    assert!(mentions_expanded(&symbols, &shadowed, "C").expect("scan"));
283
1
}