1
// Skipped under Miri: these tests compile+run wasm via wasmtime, whose
2
// Cranelift backend refuses to run under Miri.
3
#![cfg(not(miri))]
4

            
5
//! Lisp-2 rows (plan L2-0): what a variable and a function of one name do
6
//! today, with the SBCL value in each row's comment.
7

            
8
#[path = "persist/rows.rs"]
9
mod persist;
10

            
11
use persist::{Row, assert_pinned, row};
12

            
13
/// One REPL unit holding every form, as a script is compiled.
14
55
fn whole(label: &'static str, forms: &[&str]) -> Row {
15
55
    row(label, &[&forms.join(" ")])
16
55
}
17

            
18
const DEFUN_F: &str = "(defun f (x) 100)";
19

            
20
1
fn rows() -> Vec<Row> {
21
1
    vec![
22
1
        whole(
23
1
            "R1",
24
1
            &["(let ((f 10)) (labels ((f (x) (* x 2))) (list (f 3) f)))"],
25
1
        ),
26
1
        whole(
27
1
            "R1m-map",
28
1
            &["(let ((f (lambda (x) (+ x 1)))) (labels ((f (x) 100)) (map f (list 1 2))))"],
29
1
        ),
30
1
        whole(
31
1
            "R1m-fold",
32
1
            &["(let ((f (lambda (a x) (+ a x 1)))) (labels ((f (x) 100)) (fold f 0 (list 1 2))))"],
33
1
        ),
34
1
        whole(
35
1
            "R1m-filter",
36
1
            &["(let ((f (lambda (x) (< x 2)))) (labels ((f (x) 100)) (filter f (list 1 2))))"],
37
1
        ),
38
1
        whole(
39
1
            "R2",
40
1
            &["(let ((f (lambda (a) (* a 1000)))) (labels ((f (a) (* a 10))) (funcall f 2)))"],
41
1
        ),
42
1
        whole(
43
1
            "R2b",
44
1
            &["(let ((f (lambda (x) (+ x 1)))) (labels ((f (x) 100)) (apply f (list 2))))"],
45
1
        ),
46
1
        whole(
47
1
            "R2b-runtime",
48
1
            &[
49
1
                "(let ((f (lambda (x) (+ x 1)))) (labels ((f (x) 100)) (apply f (list (index->scalar (entity-count))))))",
50
1
            ],
51
1
        ),
52
1
        whole("R3", &["(flet ((f (x) (* x 2))) (f 3))"]),
53
1
        whole(
54
1
            "R4",
55
1
            &[
56
1
                "(defun f (a) (+ a 1))",
57
1
                "(labels ((f (a) (* a 10))) (list (funcall (function f) 2) (funcall 'f 2)))",
58
1
            ],
59
1
        ),
60
1
        whole(
61
1
            "R4b",
62
1
            &[
63
1
                "(defun f (a) (+ a 1))",
64
1
                "(labels ((f (a) (* a 10))) (list (funcall #'f 2) (funcall 'f 2)))",
65
1
            ],
66
1
        ),
67
1
        whole(
68
1
            "R5a",
69
1
            &[DEFUN_F, "(let ((f (lambda (x) (+ x 1)))) (funcall f 4))"],
70
1
        ),
71
1
        whole(
72
1
            "R5b",
73
1
            &[DEFUN_F, "(defvar f (lambda (x) 1))", "(funcall f 4)"],
74
1
        ),
75
1
        whole(
76
1
            "R5c",
77
1
            &[
78
1
                "(defun g (x) 100)",
79
1
                "(defun mk (g) (lambda (x) (funcall g x)))",
80
1
                "(funcall (mk (lambda (y) (* y 2))) 4)",
81
1
            ],
82
1
        ),
83
1
        whole(
84
1
            "R5m-map",
85
1
            &[
86
1
                DEFUN_F,
87
1
                "(let ((f (lambda (x) (+ x 1)))) (map f (list 1 2)))",
88
1
            ],
89
1
        ),
90
1
        whole(
91
1
            "R5m-fold",
92
1
            &[
93
1
                DEFUN_F,
94
1
                "(let ((f (lambda (a x) (+ a x 1)))) (fold f 0 (list 1 2)))",
95
1
            ],
96
1
        ),
97
1
        whole(
98
1
            "R5m-filter",
99
1
            &[
100
1
                DEFUN_F,
101
1
                "(let ((f (lambda (x) (< x 2)))) (filter f (list 1 2)))",
102
1
            ],
103
1
        ),
104
1
        whole(
105
1
            "R5m-apply",
106
1
            &[
107
1
                DEFUN_F,
108
1
                "(let ((f (lambda (x) (+ x 1)))) (apply f (list 2)))",
109
1
            ],
110
1
        ),
111
1
        whole(
112
1
            "R5r-map",
113
1
            &[
114
1
                DEFUN_F,
115
1
                "(let ((f (lambda (x) (+ x 1/2)))) (map f (list 1/2 (index->scalar (entity-count)))))",
116
1
            ],
117
1
        ),
118
1
        whole(
119
1
            "R5r-fold",
120
1
            &[
121
1
                DEFUN_F,
122
1
                "(let ((f (lambda (a x) (+ a x x)))) (fold f 1/1 (list 1/2 (index->scalar (entity-count)))))",
123
1
            ],
124
1
        ),
125
1
        whole(
126
1
            "R5r-filter",
127
1
            &[
128
1
                DEFUN_F,
129
1
                "(let ((f (lambda (x) (< x 1)))) (filter f (list 1/2 (index->scalar (entity-count)))))",
130
1
            ],
131
1
        ),
132
1
        whole(
133
1
            "R5r-apply",
134
1
            &[
135
1
                DEFUN_F,
136
1
                "(let ((f (lambda (x y) (+ x y)))) (apply f (list 1/2 (index->scalar (entity-count)))))",
137
1
            ],
138
1
        ),
139
1
        whole(
140
1
            "R5r-funcall",
141
1
            &[
142
1
                DEFUN_F,
143
1
                "(let ((f (lambda (x) x))) (funcall f (car (list 1/2 (index->scalar (entity-count))))))",
144
1
            ],
145
1
        ),
146
1
        whole("R6", &["(defvar f 5)", DEFUN_F, "(list f (f 1))"]),
147
1
        whole("R7", &[DEFUN_F, "(defvar f 5)", "(list f (f 1))"]),
148
1
        row("R9", &[DEFUN_F, "(defvar f 5)", "(list f (f 1))"]),
149
1
        row("R9b", &["(defvar f 5)", DEFUN_F, "(list f (f 1))"]),
150
1
        whole("R8", &[DEFUN_F, "(defvar f (lambda (x) 1))", "(f 4)"]),
151
1
        whole(
152
1
            "R10",
153
1
            &[
154
1
                "(defstruct pt x)",
155
1
                "(let ((pt-x 5)) (list pt-x (pt-x (make-pt :x 1))))",
156
1
            ],
157
1
        ),
158
1
        whole("R11", &["(let ((f 3)) (defun f () 1))", "(f)"]),
159
1
        whole(
160
1
            "R12",
161
1
            &["(defun f () 1)", "(let ((f 3)) (defun f () 2))", "(f)"],
162
1
        ),
163
1
        whole(
164
1
            "R12b",
165
1
            &[
166
1
                "(defun f () 1)",
167
1
                "(labels ((f () 9)) (defun f () 2))",
168
1
                "(f)",
169
1
            ],
170
1
        ),
171
1
        whole(
172
1
            "R12c",
173
1
            &["(defun f () 1)", "(dolist (f '(1)) (defun f () 2))", "(f)"],
174
1
        ),
175
1
        whole("R13", &["(labels ((f () 1)) (defvar f 5))", "f"]),
176
1
        whole("R14", &["(defun x () 7)", "(dolist (x '(1 2)) x)", "(x)"]),
177
1
        whole("R15", &["(dolist (f '(1)) (defun f () 7))", "(f)"]),
178
1
        whole("R16", &[DEFUN_F, "(let ((f 3)) (f 4))"]),
179
1
        whole(
180
1
            "R17a",
181
1
            &[
182
1
                DEFUN_F,
183
1
                "(defun app (f v) (f v))",
184
1
                "(app (lambda (x) (+ x 1)) 4)",
185
1
            ],
186
1
        ),
187
1
        whole(
188
1
            "R17b",
189
1
            &[DEFUN_F, "(let ((f 1)) (let ((f (lambda (x) 2))) (f 4)))"],
190
1
        ),
191
1
        whole(
192
1
            "R17c",
193
1
            &[DEFUN_F, "(let ((f (lambda (x) 1))) (let ((f f)) (f 4)))"],
194
1
        ),
195
1
        whole(
196
1
            "R17d",
197
1
            &[
198
1
                "(defun g (x) 100)",
199
1
                "(defun mk (g) (lambda (x) (g x)))",
200
1
                "(funcall (mk (lambda (y) (* y 2))) 4)",
201
1
            ],
202
1
        ),
203
1
        whole(
204
1
            "R18",
205
1
            &[
206
1
                DEFUN_F,
207
1
                "(let ((f (lambda (x) 1))) (let ((g (lambda (z) (f z)))) (funcall g 4)))",
208
1
            ],
209
1
        ),
210
1
        whole(
211
1
            "R19",
212
1
            &["(labels ((f (k) (* k 2))) (let ((f (lambda (x) (+ x 1)))) (f 4)))"],
213
1
        ),
214
1
        whole(
215
1
            "R20",
216
1
            &["(defun app (f v) (f v))", "(app (lambda (x) (+ x 1)) 4)"],
217
1
        ),
218
1
        whole("R21", &["(defun add (a b) (+ a b))", "(funcall add 1 2)"]),
219
1
        whole(
220
1
            "R23a",
221
1
            &[
222
1
                "(defun g () 1)",
223
1
                "(defun f (n) (if (= n 0) (g) (f (- n 1))))",
224
1
                "(labels ((g () 2)) (f 2))",
225
1
            ],
226
1
        ),
227
1
        whole(
228
1
            "R23b",
229
1
            &[
230
1
                "(defun g () 1)",
231
1
                "(defun f () (g))",
232
1
                "(labels ((g () 2)) (f))",
233
1
            ],
234
1
        ),
235
1
        whole(
236
1
            "R23c",
237
1
            &[
238
1
                "(defun f (n) (if (= n 0) 0 (f (- n 1))))",
239
1
                "(labels ((f (n) 99)) (funcall 'f 3))",
240
1
            ],
241
1
        ),
242
1
        row(
243
1
            "L1",
244
1
            &[
245
1
                "(defun f () 1)",
246
1
                "(labels ((f () 9)) (defun f () 2))",
247
1
                "(f)",
248
1
            ],
249
1
        ),
250
1
        row("L2", &["(labels ((f () 1)) (defun f () 2) (f))", "(f)"]),
251
1
        row(
252
1
            "L3",
253
1
            &[
254
1
                "(labels ((f () 1)) (dolist (x '(1 2)) (defun f () 7)) (f))",
255
1
                "(f)",
256
1
            ],
257
1
        ),
258
1
        row(
259
1
            "L4",
260
1
            &[
261
1
                "(defun f () 0)",
262
1
                "(labels ((f () 1)) (labels ((f () 2)) (defun f () 3) (f)))",
263
1
                "(f)",
264
1
            ],
265
1
        ),
266
1
        row(
267
1
            "L5",
268
1
            &[
269
1
                "(defun f () 1)",
270
1
                "(labels ((f () 9)) (defun f () 2) 0)",
271
1
                "(list (f) (funcall 'f))",
272
1
            ],
273
1
        ),
274
1
        whole(
275
1
            "L6",
276
1
            &[
277
1
                "(defun f () 0)",
278
1
                "(labels ((f () 1)) (defun f () 2) (list (f) (funcall 'f) (funcall (function f))))",
279
1
            ],
280
1
        ),
281
1
        whole("L7", &["(labels ((f () 1)) (defun f () 2) (funcall 'f))"]),
282
1
        whole(
283
1
            "N1",
284
1
            &["(labels ((f () 1)) (let ((x 1)) (defun f () 2)) (funcall 'f))"],
285
1
        ),
286
1
        whole(
287
1
            "N2",
288
1
            &["(defun f () 0) (labels ((f () 1)) (let ((x 1)) (defun f () 2)) (funcall 'f))"],
289
1
        ),
290
1
        whole(
291
1
            "N3",
292
1
            &["(labels ((f () 1)) (dolist (x '(1)) (defun f () 7)) (funcall 'f))"],
293
1
        ),
294
1
        whole(
295
1
            "N3b",
296
1
            &["(defun f () 0) (labels ((f () 1)) (dolist (x '(1)) (defun f () 7)) (funcall 'f))"],
297
1
        ),
298
1
        whole(
299
1
            "N4",
300
1
            &["(labels ((f () 1)) (defun f () 2) (labels ((f () 3)) (defun f () 4)) (funcall 'f))"],
301
1
        ),
302
1
        whole(
303
1
            "N4b",
304
1
            &[
305
1
                "(defun f () 0) (labels ((f () 1)) (defun f () 2) (labels ((f () 3)) (defun f () 4)) (list (f) (funcall 'f)))",
306
1
            ],
307
1
        ),
308
1
        whole(
309
1
            "N6c",
310
1
            &["(defun f () 0) (labels ((f () 1)) (defmacro f () 5) (funcall 'f))"],
311
1
        ),
312
1
        whole(
313
1
            "N13",
314
1
            &[
315
1
                "(defun f () 0) (labels ((f () 1)) (let ((y 2)) (defun f () y)) (list (funcall 'f) (f)))",
316
1
            ],
317
1
        ),
318
1
        row(
319
1
            "N1r",
320
1
            &[
321
1
                "(labels ((f () 1)) (let ((x 1)) (defun f () 2)) (funcall 'f))",
322
1
                "(list (f) (funcall 'f))",
323
1
            ],
324
1
        ),
325
1
        row(
326
1
            "N4r",
327
1
            &[
328
1
                "(defun f () 0)",
329
1
                "(labels ((f () 1)) (defun f () 2) (labels ((f () 3)) (defun f () 4)) (funcall 'f))",
330
1
                "(list (f) (funcall 'f))",
331
1
            ],
332
1
        ),
333
1
        row(
334
1
            "Rb",
335
1
            &[
336
1
                "(defun f () 0)",
337
1
                "(labels ((f () 1)) (let ((x 1)) (defun f () 2)) (funcall 'f))",
338
1
                "(list (f) (funcall 'f))",
339
1
            ],
340
1
        ),
341
1
        row(
342
1
            "Rc",
343
1
            &[
344
1
                "(defun f () 0)",
345
1
                "(labels ((f () 1)) (defmacro f () 5) (funcall 'f))",
346
1
                "(f)",
347
1
            ],
348
1
        ),
349
1
        row(
350
1
            "P8x",
351
1
            &[
352
1
                "(labels ((f () 1)) (dolist (x (quote (1 2))) (defun f () 7)) (funcall 'f))",
353
1
                "(list (f) (funcall 'f))",
354
1
            ],
355
1
        ),
356
    ]
357
1
}
358

            
359
/// SBCL: R1 (6 10), R2 2000, R3 6, R4/R4b (20 3), R5a 5, R5b 1, R5c 8,
360
/// R5m map (2 3) fold 5 filter (1) apply 3, R6/R7 (5 100), R8 100,
361
/// R10 (5 1), R11 1, R12/R12b/R12c 2, R13 5, R14 7, R15 7, R16 100, R17 100 each,
362
/// R18 100, R19 8, R20 undefined function, R21 unbound variable, R23 1 1 0,
363
/// R1m map (2 3) fold 5 filter (1), R2b 3 and runtime 1 (list 0),
364
/// R5r map (1 1/2) fold 2 filter (1/2 0) apply 1/2 funcall 1/2.
365
/// L1 2, L2 (1 2), L3 (1 7), L4 (2 3), L5 (2 2), L6 (1 2 1), L7 2,
366
/// N1 2, N2 2, N3/N3b 7, N4 4, N4b (1 4), N6c undefined function, N13 (2 1).
367
const TODAY: &[(&str, &[&str])] = &[
368
    ("R1", &["(6 10)"]),
369
    ("R1m-map", &["(2 3)"]),
370
    ("R1m-fold", &["5"]),
371
    ("R1m-filter", &["(1)"]),
372
    ("R2", &["2000"]),
373
    ("R2b", &["3"]),
374
    // CL: 1; S5b: the closure's parameter is pinned to an index by its literal
375
    (
376
        "R2b-runtime",
377
        &["err: Compile error: + cannot mix an index (count)"],
378
    ),
379
    ("R3", &["err: Undefined symbol: FLET"]),
380
    ("R4", &["(20 3)"]),
381
    ("R4b", &["(20 3)"]),
382
    ("R5a", &["5"]),
383
    ("R5b", &["1"]),
384
    ("R5c", &["8"]),
385
    ("R5m-map", &["(2 3)"]),
386
    ("R5m-fold", &["5"]),
387
    ("R5m-filter", &["(1)"]),
388
    ("R5m-apply", &["3"]),
389
    ("R5r-map", &["(1/1 1/2)"]),
390
    ("R5r-fold", &["2/1"]),
391
    // CL: (1/2 0); S5b: the closure's parameter is pinned to an index by its literal
392
    (
393
        "R5r-filter",
394
        &["err: Compile error: < cannot mix an index (count)"],
395
    ),
396
    ("R5r-apply", &["1/2"]),
397
    ("R5r-funcall", &["1/2"]),
398
    ("R6", &["(5 100)"]),
399
    ("R7", &["(5 100)"]),
400
    ("R9", &["f", "-", "(5 100)"]),
401
    ("R9b", &["-", "f", "(5 100)"]),
402
    ("R8", &["100"]),
403
    ("R10", &["(5 1)"]),
404
    ("R11", &["1"]),
405
    ("R12", &["2"]),
406
    ("R12b", &["2"]),
407
    ("R12c", &["2"]),
408
    ("R13", &["5"]),
409
    ("R14", &["7"]),
410
    ("R15", &["7"]),
411
    ("R16", &["100"]),
412
    ("R17a", &["100"]),
413
    ("R17b", &["100"]),
414
    ("R17c", &["100"]),
415
    ("R17d", &["100"]),
416
    ("R18", &["100"]),
417
    ("R19", &["8"]),
418
    (
419
        "R20",
420
        &["err: Compile error: undefined function F; use (fu"],
421
    ),
422
    (
423
        "R21",
424
        &["err: Compile error: variable ADD is unbound; use "],
425
    ),
426
    (
427
        "R23a",
428
        &["err: Compile error: F: calls G, a local function "],
429
    ),
430
    (
431
        "R23b",
432
        &["err: Compile error: F: calls G, a local function "],
433
    ),
434
    (
435
        "R23c",
436
        &["err: Compile error: F (global): calls F, a local "],
437
    ),
438
    ("L1", &["f", "f", "2"]),
439
    ("L2", &["1", "2"]),
440
    ("L3", &["1", "7"]),
441
    ("L4", &["f", "2", "3"]),
442
    ("L5", &["f", "0", "(2 2)"]),
443
    ("L6", &["(1 2 1)"]),
444
    ("L7", &["2"]),
445
    ("N1", &["2"]),
446
    ("N2", &["2"]),
447
    ("N3", &["7"]),
448
    ("N3b", &["7"]),
449
    ("N4", &["4"]),
450
    ("N4b", &["(1 4)"]),
451
    ("N6c", &["err: Undefined symbol: F"]),
452
    ("N13", &["(2 1)"]),
453
    ("N1r", &["2", "(2 2)"]),
454
    ("N4r", &["f", "4", "(4 4)"]),
455
    ("Rb", &["f", "2", "(2 2)"]),
456
    ("Rc", &["f", "err: Undefined symbol: F", "0"]),
457
    ("P8x", &["7", "(7 7)"]),
458
];
459

            
460
#[test]
461
1
fn every_row_reads_as_pinned() {
462
1
    assert_pinned(&rows(), TODAY);
463
1
}