vimlrs 0.2.3

Faithful Rust port of the Vimscript (VimL) interpreter, from the Neovim C eval engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
//! `fuzz-parity` — differential fuzzer: vimlrs vs. real Vim/Neovim.
//!
//! EXTENSION — NO `vendor/` counterpart. This is a development tool, not part
//! of the language runtime.
//!
//! ## What it does
//!
//! 1. **Generates** random VimL expressions from a grammar seeded by a 64-bit
//!    seed (reproducible: same seed → same corpus, no `rand` dependency).
//! 2. **Runs each through vimlrs in a child process** (this same binary,
//!    re-executed as `--child`), which evaluates in-process via
//!    [`vimlrs::eval_expr`] under `catch_unwind` and flushes one result line per
//!    expression. Values render with `string()` semantics (`encode_tv2string`);
//!    errors come from the `assert_fails()` capture hook, so nothing reaches the
//!    terminal. The child is capped by [`MEM_LIMIT`] and a wall-clock deadline,
//!    so a panic, a hard crash, a runaway allocation, and an infinite loop are
//!    all *findings* attributed to the exact expression that caused them — the
//!    parent restarts the child after that index and keeps going.
//! 3. **Runs the same corpus through the oracles** — `nvim` and `vim` — by
//!    emitting one driver script per engine that `eval()`s every expression
//!    inside `try`/`catch` and `writefile()`s the results. One process per
//!    engine for the whole corpus, not one per expression.
//! 4. **Triages** each expression by comparing all three results:
//!
//!    | class      | condition                                    | meaning                       |
//!    |------------|----------------------------------------------|-------------------------------|
//!    | `Ok`       | vimlrs == oracle                             | parity                        |
//!    | `Panic`    | vimlrs panicked / crashed / hung             | crash bug (always a finding)  |
//!    | `Gap`      | `nvim == vim` and vimlrs differs             | confirmed parity gap          |
//!    | `Divergent`| `nvim != vim`, vimlrs matches one of them    | Vim/Neovim differ — advisory  |
//!
//!    Only `Gap` and `Panic` are actionable; `Divergent` is reported separately
//!    so a Vim-vs-Neovim behavior split is never mistaken for a vimlrs bug.
//!    An expression the *oracle* couldn't answer (it crashed or hung Vim too —
//!    `range(9223372036854775807)` does exactly that) is `OracleFail`, and is
//!    never counted against vimlrs: with no spec there is nothing to be wrong
//!    about.
//!
//! Errors compare by **E-number only** (`E121`), never by message prose: the
//! number is Vim's stable contract, the wording is not.
//!
//! ## Determinism
//!
//! Every expression is evaluated against a fresh [`PRELUDE`] (the same `g:`
//! variables in every engine, re-established before each expression), so a
//! mutating call like `add(g:l, 4)` can't leak into the next case. The builtin
//! allow-list ([`FUNCS`]) admits only pure, deterministic, non-blocking
//! functions — nothing touching the clock, the filesystem, the process table,
//! the RNG, or an editor buffer.
//!
//! ## Usage
//!
//! ```text
//! cargo run --bin fuzz-parity -- --count 3000 --seed 7
//! cargo run --bin fuzz-parity -- --count 3000 --seed 7 --corpus fuzz_corpus.txt
//! cargo run --bin fuzz-parity -- --only substitute,printf --count 500
//! ```
//!
//! `--corpus FILE` appends every confirmed gap as an oracle-recorded
//! `expr<TAB>expected` line, which is what `tests/fuzz_corpus.rs` replays as a
//! Vim-free CI gate.

use std::alloc::{GlobalAlloc, Layout, System};
use std::collections::BTreeMap;
use std::fmt::Write as _;
use std::io::Write as _;
use std::panic::{self, AssertUnwindSafe};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{Duration, Instant};

use vimlrs::fusevm_bridge::{observe_errors_begin, observe_errors_take};
use vimlrs::ported::eval::encode::encode_tv2string;
use vimlrs::ported::eval::funcs_argc::BUILTIN_ARGC;
use vimlrs::ported::message::did_emsg;

// ─── Resource guards ────────────────────────────────────────────────────────
// A fuzzer that can take the machine down with it is not a fuzzer. Both sides
// need the same two guards, because *both* engines will happily try to
// materialize `range(9223372036854775807)`.

/// Heap ceiling for a child evaluating expressions. Exceeding it fails the
/// allocation, which Rust turns into an abort — the parent sees the child die,
/// attributes it to the expression in flight, and resumes after it. Without
/// this, a runaway allocation thrashes the machine for a minute before the
/// kernel steps in.
const MEM_LIMIT: usize = 1 << 30; // 1 GiB

/// Wall-clock ceiling per child / per oracle chunk.
const CHUNK_TIMEOUT: Duration = Duration::from_secs(30);

/// Expressions per oracle process. A chunk that dies costs only its own
/// remainder, not the whole corpus.
const CHUNK: usize = 250;

/// Allocator that enforces [`MEM_LIMIT`] once armed. The parent runs unarmed
/// (`LIMIT` = `usize::MAX`); a `--child` arms it before evaluating anything.
struct Budget;

static USED: AtomicUsize = AtomicUsize::new(0);
static LIMIT: AtomicUsize = AtomicUsize::new(usize::MAX);

unsafe impl GlobalAlloc for Budget {
    unsafe fn alloc(&self, l: Layout) -> *mut u8 {
        if USED.fetch_add(l.size(), Ordering::Relaxed) + l.size() > LIMIT.load(Ordering::Relaxed) {
            USED.fetch_sub(l.size(), Ordering::Relaxed);
            return std::ptr::null_mut();
        }
        let p = unsafe { System.alloc(l) };
        if p.is_null() {
            USED.fetch_sub(l.size(), Ordering::Relaxed);
        }
        p
    }

    unsafe fn dealloc(&self, p: *mut u8, l: Layout) {
        USED.fetch_sub(l.size(), Ordering::Relaxed);
        unsafe { System.dealloc(p, l) }
    }
}

#[global_allocator]
static ALLOC: Budget = Budget;

/// Wait for `child` up to `deadline`, killing it if it overruns. Returns
/// `Ok(true)` when it exited on its own, `Ok(false)` when it had to be killed.
fn wait_bounded(child: &mut Child, deadline: Duration) -> bool {
    let start = Instant::now();
    loop {
        match child.try_wait() {
            Ok(Some(_)) => return true,
            Ok(None) if start.elapsed() < deadline => std::thread::sleep(Duration::from_millis(20)),
            _ => {
                let _ = child.kill();
                let _ = child.wait();
                return false;
            }
        }
    }
}

// ─── PRNG ───────────────────────────────────────────────────────────────────
// SplitMix64 — 3 lines, no dependency, identical stream on every platform, so a
// seed reported in a bug is reproducible on any machine and in any year.

struct Rng(u64);

impl Rng {
    fn next(&mut self) -> u64 {
        self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15);
        let mut z = self.0;
        z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
        z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
        z ^ (z >> 31)
    }

    /// Uniform in `0..n` (`n > 0`).
    fn below(&mut self, n: usize) -> usize {
        (self.next() % n as u64) as usize
    }

    /// Pick one element of a non-empty slice.
    fn pick<'a, T>(&mut self, xs: &'a [T]) -> &'a T {
        &xs[self.below(xs.len())]
    }

    /// True with probability `n`/`d`.
    fn chance(&mut self, n: u64, d: u64) -> bool {
        self.next() % d < n
    }
}

// ─── Argument shapes ────────────────────────────────────────────────────────

/// The kind of value a builtin's positional argument wants. Random *typed*
/// arguments keep the corpus in the region where the interesting semantics live
/// (coercion, clamping, empty/negative/out-of-range edges); random *untyped*
/// arguments would mostly reproduce E-number checks that the arity table
/// already gates.
#[derive(Clone, Copy, PartialEq)]
enum Shape {
    /// Any string, including empty / unicode / embedded quotes.
    Str,
    /// Integer, weighted toward the edges (0, ±1, INT_MAX, 64-bit extremes).
    Num,
    /// Float, including negative / zero / very large.
    Float,
    /// 0 or 1 (Vim's boolean convention).
    Bool,
    /// A regex pattern — the highest-value shape: Vim's regex dialect is where
    /// a hand-written engine diverges most.
    Pat,
    /// A `printf()` format string.
    Fmt,
    /// A list of mixed values.
    List,
    /// A list of strings (for `join`, `sort`, `filter` over strings).
    StrList,
    /// A list of numbers (for `max`, `min`, `sort` numeric).
    NumList,
    /// A dict.
    Dict,
    /// A blob.
    Blob,
    /// Any value at all (for `type()`, `string()`, `empty()`, …).
    Any,
    /// A lambda expression, `{x -> …}` (for `map`/`filter`/`sort`/`reduce`).
    Lambda,
    /// A `substitute()` replacement string (may carry `\1`, `\=expr`, `~`).
    Repl,
    /// `sort()`/`uniq()` comparison flag: `'i'`, `'n'`, `'N'`, `'l'`, `1`, `0`.
    SortFlag,
    /// A funcref — `function('strlen')` or a lambda — for `call`/`funcref`.
    Func,
    /// A *size*: how many elements/copies to materialize (`range`, `repeat`,
    /// `flatten` depth). Bounded on purpose — `range(9223372036854775807)`
    /// allocates without bound in **real Vim and Neovim too**, so it is a
    /// resource pathology shared by all three engines, not a parity gap, and
    /// generating it only costs a timeout per case.
    Count,
}

use Shape::*;

/// Per-builtin positional argument shapes.
///
/// The *count* of arguments emitted is not taken from this table — it is drawn
/// from [`BUILTIN_ARGC`], the generated arity metadata the compiler itself
/// enforces, so the fuzzer and the interpreter can never disagree about how
/// many arguments a call may carry. This table only says what each slot should
/// *look like*; a call that wants more slots than are listed repeats the last
/// shape.
///
/// The allow-list is deliberate: every function here is pure, deterministic,
/// non-blocking, and independent of the filesystem, clock, environment, RNG,
/// and editor state. Nothing else may be added — an impure builtin would report
/// a false "gap" on every run.
const FUNCS: &[(&str, &[Shape])] = &[
    // ── strings ─────────────────────────────────────────────────────────────
    ("strlen", &[Str]),
    ("strchars", &[Str, Bool]),
    ("strwidth", &[Str]),
    ("strdisplaywidth", &[Str, Num]),
    ("strcharlen", &[Str]),
    ("strpart", &[Str, Num, Num, Bool]),
    ("strcharpart", &[Str, Num, Num]),
    ("strgetchar", &[Str, Num]),
    ("strcharatpos", &[Str, Num]),
    ("stridx", &[Str, Str, Num]),
    ("strridx", &[Str, Str, Num]),
    ("strtrans", &[Str]),
    ("tolower", &[Str]),
    ("toupper", &[Str]),
    ("trim", &[Str, Str, Num]),
    ("repeat", &[Any, Count]),
    ("reverse", &[Any]),
    ("split", &[Str, Pat, Bool]),
    ("join", &[List, Str]),
    ("substitute", &[Str, Pat, Repl, Str]),
    ("escape", &[Str, Str]),
    ("shellescape", &[Str, Bool]),
    ("fnameescape", &[Str]),
    ("printf", &[Fmt, Any, Any, Any]),
    ("nr2char", &[Num, Bool]),
    ("char2nr", &[Str, Bool]),
    ("str2nr", &[Str, Num, Bool]),
    ("str2float", &[Str, Bool]),
    ("str2list", &[Str, Bool]),
    ("list2str", &[NumList, Bool]),
    ("byteidx", &[Str, Num, Bool]),
    ("byteidxcomp", &[Str, Num, Bool]),
    ("charidx", &[Str, Num, Bool]),
    ("strspn", &[Str, Str]),
    ("strcspn", &[Str, Str]),
    ("eval", &[Str]),
    ("tr", &[Str, Str, Str]),
    ("slice", &[Any, Num, Num]),
    ("sha256", &[Str]),
    ("charclass", &[Str]),
    ("strutf16len", &[Str, Bool]),
    ("utf16idx", &[Str, Num, Bool, Bool]),
    ("byteidxcomp", &[Str, Num]),
    ("string", &[Any]),
    ("iconv", &[Str, Str, Str]),
    ("keytrans", &[Str]),
    // ── regex ───────────────────────────────────────────────────────────────
    ("match", &[Str, Pat, Num, Num]),
    ("matchend", &[Str, Pat, Num, Num]),
    ("matchstr", &[Str, Pat, Num, Num]),
    ("matchstrpos", &[Str, Pat, Num, Num]),
    ("matchlist", &[Str, Pat, Num, Num]),
    ("matchbufline", &[Num, Pat, Num, Num]),
    ("matchfuzzy", &[StrList, Str]),
    // ── numbers / math ──────────────────────────────────────────────────────
    ("abs", &[Float]),
    ("ceil", &[Float]),
    ("floor", &[Float]),
    ("round", &[Float]),
    ("trunc", &[Float]),
    ("float2nr", &[Float]),
    ("fmod", &[Float, Float]),
    ("pow", &[Float, Float]),
    ("sqrt", &[Float]),
    ("exp", &[Float]),
    ("log", &[Float]),
    ("log10", &[Float]),
    ("sin", &[Float]),
    ("cos", &[Float]),
    ("tan", &[Float]),
    ("asin", &[Float]),
    ("acos", &[Float]),
    ("atan", &[Float]),
    ("atan2", &[Float, Float]),
    ("sinh", &[Float]),
    ("cosh", &[Float]),
    ("tanh", &[Float]),
    ("and", &[Num, Num]),
    ("or", &[Num, Num]),
    ("xor", &[Num, Num]),
    ("invert", &[Num]),
    ("max", &[NumList]),
    ("min", &[NumList]),
    ("isinf", &[Float]),
    ("isnan", &[Float]),
    // ── lists / dicts ───────────────────────────────────────────────────────
    ("len", &[Any]),
    ("empty", &[Any]),
    ("get", &[Any, Any, Any]),
    ("add", &[List, Any]),
    ("insert", &[List, Any, Num]),
    ("remove", &[Any, Any, Any]),
    ("extend", &[List, List, Num]),
    ("copy", &[Any]),
    ("deepcopy", &[Any, Bool]),
    ("count", &[Any, Any, Bool, Num]),
    ("index", &[List, Any, Num, Bool]),
    ("indexof", &[List, Lambda]),
    ("map", &[Any, Lambda]),
    ("filter", &[Any, Lambda]),
    ("sort", &[List, SortFlag]),
    ("uniq", &[List, SortFlag]),
    ("reduce", &[List, Lambda, Any]),
    ("flatten", &[List, Count]),
    ("flattennew", &[List, Count]),
    ("range", &[Count, Count, Count]),
    ("keys", &[Dict]),
    ("values", &[Dict]),
    ("items", &[Dict]),
    ("has_key", &[Dict, Str]),
    ("zip", &[List, List]),
    ("extendnew", &[List, List, Num]),
    ("mapnew", &[Any, Lambda]),
    ("call", &[Func, List]),
    ("funcref", &[Str, List]),
    ("matchfuzzypos", &[StrList, Str]),
    // ── blobs ───────────────────────────────────────────────────────────────
    ("blob2list", &[Blob]),
    ("list2blob", &[NumList]),
    ("blob2str", &[Blob]),
    ("str2blob", &[StrList]),
    // ── types / conversion ──────────────────────────────────────────────────
    ("type", &[Any]),
    ("typename", &[Any]),
    ("json_encode", &[Any]),
    ("json_decode", &[Str]),
    ("js_encode", &[Any]),
    ("js_decode", &[Str]),
    ("msgpackdump", &[List]),
    ("id", &[Any]),
];

// ─── Value pools ────────────────────────────────────────────────────────────
// Edge-weighted literals. Every entry is a VimL source fragment.

const STRINGS: &[&str] = &[
    "''",
    "'a'",
    "'abc'",
    "'ABC'",
    "'hello world'",
    "'  padded  '",
    "'a,b,,c'",
    "'foo.bar'",
    "'1234'",
    "'-7'",
    "'3.5e2'",
    "'0x1f'",
    "'tab\\there'",
    "'ünïcø∂é'",
    "'日本語'",
    "'e\u{0301}combining'",
    "'a''quote'",
    "\"dq\\tstr\"",
    "\"nl\\nhere\"",
    "'*.[]^$\\'",
    "'\\d\\+'",
    // Double-quoted escapes — a lexer/eval_string surface a single-quoted pool
    // can never reach (`\<Esc>` is what R5-13 was).
    "\"\\<Esc>\"",
    "\"\\<C-A>\"",
    "\"\\<Space>x\"",
    "\"\\x41\\x42\"",
    "\"\\101\\102\"",
    "\"\\u00e9\"",
    "\"a\\\\b\"",
];

const NUMS: &[&str] = &[
    "0",
    "1",
    "2",
    "3",
    "-1",
    "-2",
    "7",
    "10",
    "-10",
    "255",
    "256",
    "-255",
    "2147483647",
    "-2147483648",
    "9223372036854775807",
    "-9223372036854775808",
    "0x1f",
    "0b1011",
    "017",
    "100000",
];

/// Funcref values: a named builtin, a lambda, and a partial.
const FUNCS_REF: &[&str] = &[
    "function('strlen')",
    "function('toupper')",
    "function('abs')",
    "function('add')",
    "{x -> x}",
    "{x -> type(x)}",
    "function('printf', ['%d'])",
];

/// Bounded sizes for [`Shape::Count`] slots — still covering the interesting
/// edges (zero, negative, one-past) without asking any engine to materialize a
/// 9-quintillion-element list.
const COUNTS: &[&str] = &["0", "1", "2", "3", "5", "10", "64", "1000", "-1", "-3"];

const FLOATS: &[&str] = &[
    "0.0",
    "1.0",
    "-1.0",
    "0.5",
    "-0.5",
    "1.5",
    "2.75",
    "-3.25",
    "3.141592653589793",
    "1.0e10",
    "1.0e-10",
    "-1.0e300",
    "1.0e308",
    "123456789.123456789",
    "0.1",
];

/// Regex patterns in Vim's dialect — magic atoms, quantifiers, classes,
/// anchors, zero-width bounds, alternation, lookaround, POSIX classes.
const PATS: &[&str] = &[
    "'a'",
    "'.'",
    "'.*'",
    "'^a'",
    "'c$'",
    "'\\.'",
    "'a\\+'",
    "'a\\='",
    "'a\\?'",
    "'a\\{2}'",
    "'a\\{1,3}'",
    "'a\\{-}'",
    "'a\\{-1,}'",
    "'\\d'",
    "'\\d\\+'",
    "'\\D'",
    "'\\w\\+'",
    "'\\W'",
    "'\\s'",
    "'\\S\\+'",
    "'\\a'",
    "'\\l'",
    "'\\u'",
    "'\\x'",
    "'\\o'",
    "'\\h'",
    "'\\k'",
    "'\\p'",
    "'[abc]'",
    "'[^abc]'",
    "'[a-z]\\+'",
    "'[[:digit:]]\\+'",
    "'[[:alpha:]]'",
    "'[[:space:]]'",
    "'[[:punct:]]'",
    "'\\(a\\)\\(b\\)'",
    "'\\(ab\\)\\1'",
    "'a\\|b'",
    "'\\%(ab\\)\\+'",
    "'\\zsa'",
    "'a\\zs.'",
    "'.\\zea'",
    "'\\<a'",
    "'a\\>'",
    "'\\ca'",
    "'\\Ca'",
    "'\\vA+'",
    "'\\v(a|b)+'",
    "'\\V.'",
    "'\\Ma.'",
    "'a\\@='",
    "'a\\@!'",
    "'\\(a\\)\\@<=b'",
    "'\\(a\\)\\@<!b'",
    "'\\%[abc]'",
    "'\\%d97'",
    "'\\%x61'",
    "'\\_.'",
    "'\\n'",
    "'\\t'",
    "''",
];

/// `printf()` formats: every conversion vimlrs claims to support, plus the
/// width/precision/flag combinations that are easy to get subtly wrong.
const FMTS: &[&str] = &[
    "'%d'",
    "'%5d'",
    "'%-5d|'",
    "'%05d'",
    "'%+d'",
    "'% d'",
    "'%x'",
    "'%X'",
    "'%#x'",
    "'%o'",
    "'%b'",
    "'%c'",
    "'%s'",
    "'%10s|'",
    "'%-10s|'",
    "'%.3s'",
    "'%S'",
    "'%f'",
    "'%.2f'",
    "'%e'",
    "'%E'",
    "'%g'",
    "'%G'",
    "'%%'",
    "'%*d'",
    "'%.*f'",
    "'%1$s %1$s'",
    "'a%db%sc'",
    "'%s=%d'",
];

const LISTS: &[&str] = &[
    "[]",
    "[1]",
    "[1,2,3]",
    "[3,1,2]",
    "[1,1,2,2,3]",
    "['a','b','c']",
    "['b','a','C','A']",
    "[1,'a',2.5]",
    "[[1,2],[3,4]]",
    "[[1,[2,[3]]]]",
    "[{'a':1},{'a':2}]",
    "[0,-1,255]",
    "[v:true,v:false,v:null]",
    "['10','9','2']",
];

const STRLISTS: &[&str] = &[
    "[]",
    "['a']",
    "['a','b','c']",
    "['foo','bar','baz']",
    "['B','a','C']",
    "['','x','']",
];

const NUMLISTS: &[&str] = &[
    "[]",
    "[0]",
    "[1,2,3]",
    "[3,1,2]",
    "[-1,0,1]",
    "[65,66,67]",
    "[255,256,0]",
    "[97,0x1f,10]",
];

const DICTS: &[&str] = &[
    "{}",
    "{'a':1}",
    "{'a':1,'b':2}",
    "{'b':2,'a':1}",
    "{'a':[1,2],'b':{'c':3}}",
    "{'1':'x','2':'y'}",
    "#{a:1,b:2}",
];

const BLOBS: &[&str] = &["0z", "0z00", "0zFF", "0z0011DEADBEEF", "0z61.62.63"];

const SPECIALS: &[&str] = &["v:true", "v:false", "v:null", "v:none"];

/// Replacement strings for `substitute()` — backrefs, whole-match `&`, `~`, the
/// `\=` expression form, and case-folding escapes.
const REPLS: &[&str] = &[
    "'X'",
    "''",
    "'[&]'",
    "'\\0\\0'",
    "'\\1'",
    "'<\\1>'",
    "'\\u&'",
    "'\\U&'",
    "'\\l&'",
    "'\\e'",
    "'\\=submatch(0)'",
    "'\\=toupper(submatch(0))'",
    "'\\=submatch(0).submatch(0)'",
    "'\\=len(submatch(0))'",
    "'\\n'",
    "'\\r'",
    "'~'",
];

const LAMBDAS: &[&str] = &[
    "{i,v -> v}",
    "{i,v -> i}",
    "{_,v -> v * 2}",
    "{_,v -> type(v)}",
    "{_,v -> string(v)}",
    "{_,v -> v is v}",
    "{i,v -> i % 2}",
    "{_,v -> empty(v)}",
];

/// Comparators for `sort()`/`uniq()` (the 2nd argument).
const SORT_FLAGS: &[&str] = &[
    "'i'",
    "'n'",
    "'N'",
    "'l'",
    "1",
    "0",
    "{a,b -> a > b ? -1 : 1}",
];

/// Global variables the [`PRELUDE`] defines in every engine before each
/// expression, so a generated expression can name (and mutate) a value.
const VARS: &[&str] = &["g:n", "g:f", "g:s", "g:l", "g:d", "g:b", "g:e"];

/// Re-established before *every* expression in every engine, so a mutating call
/// (`add`, `remove`, `sort`, `map`, …) starts from identical state and can't
/// leak into the next case.
const PRELUDE: &[&str] = &[
    "let g:n = 42",
    "let g:f = 2.5",
    "let g:s = 'hello'",
    "let g:l = [1, 2, 3]",
    "let g:d = {'a': 1, 'b': 2}",
    "let g:b = 0z0011",
    "let g:e = []",
];

/// Binary operators. Comparison operators appear in all three case forms
/// (bare / `#` match-case / `?` ignore-case) because that family has regressed
/// before (BUGS.md R1-1).
const BINOPS: &[&str] = &[
    "+", "-", "*", "/", "%", ".", "..", "&&", "||", "==", "!=", ">", ">=", "<", "<=", "=~", "!~",
    "==#", "!=#", ">#", "<#", "=~#", "!~#", "==?", "!=?", ">?", "<?", "=~?", "!~?", "is", "isnot",
];

// ─── Generator ──────────────────────────────────────────────────────────────

/// One value of the given shape.
fn shaped(rng: &mut Rng, s: Shape) -> String {
    match s {
        Str => rng.pick(STRINGS).to_string(),
        Num => rng.pick(NUMS).to_string(),
        Float => rng.pick(FLOATS).to_string(),
        Bool => ["0", "1"][rng.below(2)].to_string(),
        Pat => rng.pick(PATS).to_string(),
        Fmt => rng.pick(FMTS).to_string(),
        List => rng.pick(LISTS).to_string(),
        StrList => rng.pick(STRLISTS).to_string(),
        NumList => rng.pick(NUMLISTS).to_string(),
        Dict => rng.pick(DICTS).to_string(),
        Blob => rng.pick(BLOBS).to_string(),
        Lambda => rng.pick(LAMBDAS).to_string(),
        Repl => rng.pick(REPLS).to_string(),
        SortFlag => rng.pick(SORT_FLAGS).to_string(),
        Count => rng.pick(COUNTS).to_string(),
        Func => rng.pick(FUNCS_REF).to_string(),
        Any => any_value(rng),
    }
}

/// A value of *any* type — the pool that exercises coercion and `E7xx` type
/// errors.
fn any_value(rng: &mut Rng) -> String {
    match rng.below(8) {
        0 => rng.pick(NUMS).to_string(),
        1 => rng.pick(STRINGS).to_string(),
        2 => rng.pick(FLOATS).to_string(),
        3 => rng.pick(LISTS).to_string(),
        4 => rng.pick(DICTS).to_string(),
        5 => rng.pick(BLOBS).to_string(),
        6 => rng.pick(SPECIALS).to_string(),
        _ => rng.pick(VARS).to_string(),
    }
}

/// Accepted argument-count range for `name`, straight from the arity table the
/// compiler enforces.
fn argc_range(name: &str) -> (u8, u8) {
    BUILTIN_ARGC
        .binary_search_by_key(&name, |(n, _, _)| n)
        .map(|i| (BUILTIN_ARGC[i].1, BUILTIN_ARGC[i].2))
        .unwrap_or((0, 0))
}

/// A call to one allow-listed builtin, with an argument count drawn from the
/// real arity range and typed arguments from its shape row.
fn gen_call(rng: &mut Rng, funcs: &[(&str, &[Shape])]) -> String {
    let (name, shapes) = *rng.pick(funcs);
    let (min, max) = argc_range(name);
    // Cap the varargs sentinel (255) at the shapes we have; `printf` is the only
    // real varargs case and its row already lists the slots worth filling.
    let hi = (max as usize).min(shapes.len().max(min as usize));
    let n = if hi > min as usize {
        min as usize + rng.below(hi - min as usize + 1)
    } else {
        min as usize
    };
    let args: Vec<String> = (0..n)
        .map(|i| shaped(rng, shapes[i.min(shapes.len() - 1)]))
        .collect();
    format!("{name}({})", args.join(","))
}

// ─── Statement generation ───────────────────────────────────────────────────
//
// The generator above only ever produced *expressions*, and the two worst bugs
// this harness's targets ever had — `:try` not catching a runtime error, and a
// failed `:let` storing a corrupted value — live at the **statement** level and
// had to be found by hand.
//
// A statement snippet is compared through the very same three-engine pipeline by
// wrapping it in `execute()`, which runs the commands and returns their output as
// a string. So `let x = 1 | echo x` becomes the expression
// `execute("let x = 1 | echo x")`, whose value is "\n1" in every engine — and any
// divergence in control flow, error handling, or output shows up as a plain value
// mismatch.

// ─── Regex generation ───────────────────────────────────────────────────────
//
// `viml_regex` is the largest hand-written carve-out in the crate: it reproduces
// Vim's pattern *dialect* from the documentation rather than porting
// `regexp_bt.c`/`regexp_nfa.c`. That makes it the most likely place for a
// divergence to hide, and drawing patterns from a fixed list (as `PATS` does) only
// ever exercises the shapes someone already thought of.
//
// So build patterns from a grammar instead, and check each one through every API
// that reaches the engine — `match()` (the index), `matchstr()` (the text),
// `matchlist()` (the capture groups), `substitute()` (the rewrite, with the
// zero-width and `\zs` rules) and `split()` — against a subject pool chosen to
// straddle the boundaries: empty, ASCII, multibyte, combining, punctuation, digits.

/// Subjects a generated pattern is matched against.
const RX_SUBJECTS: &[&str] = &[
    "''",
    "'a'",
    "'abc'",
    "'aaa'",
    "'abcabc'",
    "'a1b2c3'",
    "'Hello World'",
    "'  spaced  '",
    "'a.b*c'",
    "'foo_bar-baz'",
    "'ünïcø∂é'",
    "'日本語abc'",
    "'e\u{0301}combining'",
    "'tab\there'",
    "'AbCdEf'",
];

/// A regex *atom* — the leaves of the grammar.
fn rx_atom(rng: &mut Rng) -> String {
    match rng.below(14) {
        0 => rng
            .pick(&["a", "b", "c", "x", "1", "_", "-", ".", "é"])
            .to_string(),
        1 => ".".into(),
        2 => rng
            .pick(&[
                "\\d", "\\D", "\\w", "\\W", "\\s", "\\S", "\\a", "\\l", "\\u", "\\x", "\\o", "\\h",
                "\\k", "\\p", "\\i",
            ])
            .to_string(),
        3 => rng
            .pick(&[
                "[abc]", "[^abc]", "[a-z]", "[A-Z0-9]", "[^0-9]", "[.*]", "[]a]",
            ])
            .to_string(),
        4 => rng
            .pick(&[
                "[[:alpha:]]",
                "[[:digit:]]",
                "[[:space:]]",
                "[[:punct:]]",
                "[[:upper:]]",
                "[[:alnum:]]",
            ])
            .to_string(),
        5 => format!("\\({}\\)", rx_branch(rng, 0)),
        6 => format!("\\%({}\\)", rx_branch(rng, 0)),
        7 => rng.pick(&["\\<", "\\>", "^", "$"]).to_string(),
        8 => rng.pick(&["\\zs", "\\ze"]).to_string(),
        9 => rng
            .pick(&["\\%d97", "\\%x62", "\\%o143", "\\%u0061"])
            .to_string(),
        10 => rng.pick(&["\\_.", "\\_s", "\\_a", "\\_[a-z]"]).to_string(),
        11 => format!("\\%[{}]", rng.pick(&["abc", "ab", "xyz"])),
        12 => rng.pick(&["\\1", "\\2"]).to_string(),
        _ => rng.pick(&["\\.", "\\*", "\\[", "\\\\"]).to_string(),
    }
}

/// An atom plus an optional quantifier.
fn rx_piece(rng: &mut Rng, depth: u32) -> String {
    let atom = if depth > 0 && rng.chance(1, 5) {
        format!("\\({}\\)", rx_branch(rng, depth - 1))
    } else {
        rx_atom(rng)
    };
    match rng.below(9) {
        0 => format!("{atom}*"),
        1 => format!("{atom}\\+"),
        2 => format!("{atom}\\?"),
        3 => format!("{atom}\\="),
        4 => format!("{atom}\\{{2}}"),
        5 => format!("{atom}\\{{1,3}}"),
        6 => format!("{atom}\\{{-}}"),
        7 => format!("{atom}\\{{-1,}}"),
        _ => atom,
    }
}

/// A concatenation, possibly with alternation.
fn rx_branch(rng: &mut Rng, depth: u32) -> String {
    let n = 1 + rng.below(3);
    let mut out = String::new();
    for _ in 0..n {
        out.push_str(&rx_piece(rng, depth));
    }
    if rng.chance(1, 4) {
        out.push_str("\\|");
        out.push_str(&rx_piece(rng, depth));
    }
    out
}

/// One generated pattern, wrapped as a single-quoted VimL string.
fn rx_pattern(rng: &mut Rng) -> String {
    let body = rx_branch(rng, 1);
    // Occasionally force case control or a magic-mode switch — both change how the
    // whole pattern is read.
    let prefix = match rng.below(8) {
        0 => "\\c",
        1 => "\\C",
        2 => "\\v",
        3 => "\\V",
        4 => "\\M",
        _ => "",
    };
    format!("'{prefix}{body}'")
}

/// One regex case: a generated pattern, a subject, and one of the APIs that reach
/// the engine. Comparing several APIs matters — a pattern can match in the same
/// place yet report different capture groups or rewrite differently.
fn gen_regex(rng: &mut Rng) -> String {
    let pat = rx_pattern(rng);
    let subj = rng.pick(RX_SUBJECTS);
    match rng.below(8) {
        0 => format!("match({subj}, {pat})"),
        1 => format!("matchstr({subj}, {pat})"),
        2 => format!("matchend({subj}, {pat})"),
        3 => format!("matchlist({subj}, {pat})"),
        4 => format!("substitute({subj}, {pat}, 'X', '')"),
        5 => format!("substitute({subj}, {pat}, 'X', 'g')"),
        6 => format!("substitute({subj}, {pat}, '[&]', 'g')"),
        _ => format!("split({subj}, {pat})"),
    }
}

/// Statement shapes beyond a bare `:echo`/`:let` — the surfaces the generator was
/// blind to: user functions (arguments, defaults, varargs, closures, recursion),
/// compound and unpacking `:let`, indexed assignment, `:unlet`, `:for` over a Dict
/// or String, and string interpolation. Each is a complete snippet; `%E` is
/// substituted with a generated expression.
const STMT_SHAPES: &[&str] = &[
    // user functions: argument scope, defaults, varargs, return
    "function! F(a) \n return a:a \n endfunction \n echo F(%E)",
    "function! F(a, b = 2) \n return a:b \n endfunction \n echo F(1)",
    "function! F(...) \n return a:0 \n endfunction \n echo F(%E, 2)",
    "function! F(...) \n return a:000 \n endfunction \n echo F(%E)",
    "function! F(a) \n if a:a > 0 \n return 'p' \n endif \n return 'n' \n endfunction \n echo F(1)",
    // a closure over a local
    "function! F() closure \n return 1 \n endfunction \n echo F()",
    "let l:x = %E | let Fn = {-> l:x} | echo Fn()",
    // compound assignment
    "let x = 1 | let x += %E | echo x",
    "let s = 'a' | let s .= 'b' | echo s",
    "let n = 10 | let n -= 3 | echo n",
    // unpacking
    "let [a, b] = [1, 2] | echo a + b",
    "let [a; rest] = [1, 2, 3] | echo rest",
    // indexed / member assignment
    "let l = [1,2,3] | let l[0] = %E | echo l",
    "let d = {'a': 1} | let d.a = %E | echo d",
    "let d = {'a': 1} | let d['b'] = 2 | echo d",
    "let l = [1,2,3] | let l[0:1] = [9,9] | echo l",
    // unlet
    "let g:tmp = 1 | unlet g:tmp | echo exists('g:tmp')",
    "let d = {'a':1,'b':2} | unlet d.a | echo d",
    // :for over the containers
    "for [k, v] in items({'a':1}) | echo k . v | endfor",
    "for c in split('ab', '\\zs') | echo c | endfor",
    "for b in 0z0102 | echo b | endfor",
    "for i in range(3) | if i == 1 | continue | endif | echo i | endfor",
    "for i in range(5) | if i == 2 | break | endif | echo i | endfor",
    // string interpolation
    "let x = %E | echo $'v={x}'",
    // while
    "let i = 0 | while i < 3 | let i += 1 | endwhile | echo i",
];

/// One VimL *command*, for use inside a statement snippet.
fn gen_cmd(rng: &mut Rng, funcs: &[(&str, &[Shape])], depth: u32) -> String {
    let e = |rng: &mut Rng| gen_expr(rng, funcs, depth);
    match rng.below(12) {
        0 => format!("echo {}", e(rng)),
        1 => format!("let x = {}", e(rng)),
        2 => format!("let g:l = {}", e(rng)),
        3 => format!("echo {} | echo {}", e(rng), e(rng)),
        // The `|`-separated line: Vim abandons the rest of it when one errors.
        4 => format!("echo {} | echo 'tail'", e(rng)),
        5 => format!("if {} | echo 'y' | else | echo 'n' | endif", e(rng)),
        6 => format!("for i in {} | echo i | endfor", rng.pick(LISTS)),
        7 => format!("try | echo {} | catch | echo 'caught' | endtry", e(rng)),
        8 => format!("try\necho {}\ncatch\necho 'caught'\nendtry", e(rng)),
        9 => format!("silent! echo {}", e(rng)),
        10 => format!("let d = {} | echo d", e(rng)),
        _ => format!("call add(g:l, {}) | echo g:l", e(rng)),
    }
}

/// A statement snippet, wrapped as `execute('…')` so it rides the expression
/// pipeline (see the note above). Newlines inside the snippet become `\n` in the
/// single-quoted VimL string, which `execute()` treats as separate command lines —
/// that is how the multi-line `:try` form is reached.
fn gen_stmt(rng: &mut Rng, funcs: &[(&str, &[Shape])], depth: u32) -> String {
    // Half the snippets come from the shape table (user functions, `:let` targets,
    // `:for` forms …), half from the free-form command generator.
    let cmd = if rng.chance(1, 2) {
        rng.pick(STMT_SHAPES)
            .replace("%E", &gen_expr(rng, funcs, depth.min(1)))
    } else {
        gen_cmd(rng, funcs, depth)
    };
    // `execute()` takes the command text; a literal newline cannot appear inside a
    // single-quoted VimL string, so the generator emits the two-character escape
    // `\n` and `execute()` splits on it.
    format!("execute({})", vim_quote(&cmd))
}

/// One expression, recursively. `depth` bounds nesting so the corpus stays
/// human-readable when a case has to be pasted into a bug report.
fn gen_expr(rng: &mut Rng, funcs: &[(&str, &[Shape])], depth: u32) -> String {
    if depth == 0 {
        return match rng.below(4) {
            0 => gen_call(rng, funcs),
            _ => any_value(rng),
        };
    }
    match rng.below(10) {
        // Builtin call — the bulk of the corpus.
        0..=4 => {
            let call = gen_call(rng, funcs);
            // Sometimes index or slice the result: `split(…)[0]`, `s[1:3]`.
            if rng.chance(1, 5) {
                let i = rng.pick(&["0", "1", "-1", "2", "5", "-5"]);
                if rng.chance(1, 2) {
                    format!("{call}[{i}]")
                } else {
                    let j = rng.pick(&["", "0", "1", "-1", "3"]);
                    format!("{call}[{i}:{j}]")
                }
            } else {
                call
            }
        }
        // Binary operator over two sub-expressions.
        5..=7 => {
            let a = gen_expr(rng, funcs, depth - 1);
            let b = gen_expr(rng, funcs, depth - 1);
            let op = rng.pick(BINOPS);
            // `.` needs surrounding space to stay concatenation, not a member
            // read (BUGS.md R4-1); `..` and the word operators need it too.
            format!("({a} {op} {b})")
        }
        // Unary.
        8 => {
            let a = gen_expr(rng, funcs, depth - 1);
            let op = rng.pick(&["!", "-", "+"]);
            format!("{op}({a})")
        }
        // Ternary.
        _ => {
            let c = gen_expr(rng, funcs, depth - 1);
            let a = gen_expr(rng, funcs, depth - 1);
            let b = gen_expr(rng, funcs, depth - 1);
            format!("({c} ? {a} : {b})")
        }
    }
}

// ─── Results ────────────────────────────────────────────────────────────────

/// The outcome of evaluating one expression in one engine.
#[derive(Clone, PartialEq, Eq)]
enum Outcome {
    /// `string()` of the value.
    Val(String),
    /// The E-number only (`E121`) — the message prose is not a contract.
    Err(String),
    /// vimlrs panicked (never a legal outcome).
    Panic(String),
    /// The oracle produced no line for this index (it died or hung).
    Missing,
}

impl Outcome {
    fn show(&self) -> String {
        match self {
            Outcome::Val(v) => v.clone(),
            Outcome::Err(e) => format!("<{e}>"),
            Outcome::Panic(p) => format!("<PANIC: {p}>"),
            Outcome::Missing => "<none>".into(),
        }
    }
}

/// Pull the first `E<digits>` out of an error message; Vim's message text is
/// not stable across versions but the number is.
fn enumber(msg: &str) -> String {
    let b = msg.as_bytes();
    for i in 0..b.len() {
        if b[i] == b'E' && b.get(i + 1).is_some_and(u8::is_ascii_digit) {
            let d: String = msg[i + 1..]
                .chars()
                .take_while(char::is_ascii_digit)
                .collect();
            if msg[i + 1 + d.len()..].starts_with(':') {
                return format!("E{d}");
            }
        }
    }
    "E?".into()
}

/// Evaluate one expression in-process, with the [`PRELUDE`] re-established
/// first. Panics are caught and reported rather than killing the run.
fn eval_one(expr: &str) -> Outcome {
    let r = panic::catch_unwind(AssertUnwindSafe(|| {
        // OBSERVE, don't capture. `capture_errors_begin` is Vim's `emsg_silent`
        // path, and a silenced error is deliberately never turned into an exception
        // (`cause_errthrow` declines) — so capturing in order to *read* the error
        // silently disabled `:try`/`:catch` in everything the fuzzer ran, and it
        // duly reported "vimlrs does not catch runtime errors" for a dozen cases the
        // real binary catches fine. The observer reads the message and changes
        // nothing.
        observe_errors_begin();
        for line in PRELUDE {
            let _ = vimlrs::eval_source(line);
        }
        let _ = observe_errors_take();

        // `did_emsg` is the "an error was reported and NOT handled" flag: `:catch`
        // resets it (c: ex_catch), and `:silent!` never sets it. So it — not the
        // presence of an observed message — is what decides whether this expression
        // ends in an error, exactly as it decides a script's exit status.
        did_emsg.with(|d| d.set(0));
        observe_errors_begin();
        let out = vimlrs::eval_expr(expr);
        let errs = observe_errors_take();
        let unhandled = did_emsg.with(|d| d.get()) != 0;
        match out {
            Ok(v) if !unhandled => Outcome::Val(encode_tv2string(&v)),
            // The FIRST unhandled message: Vim reports an error and abandons the
            // command, so the first one is what it shows. This VM keeps evaluating
            // and can raise more, and a `:catch` clears the ones it handled — so
            // what is left here begins with the error Vim would have reported.
            Ok(_) => Outcome::Err(enumber(errs.first().map_or("E?", |s| s.as_str()))),
            Err(e) => Outcome::Err(enumber(&e.to_string())),
        }
    }));
    r.unwrap_or_else(|e| {
        let msg = e
            .downcast_ref::<&str>()
            .map(|s| (*s).to_string())
            .or_else(|| e.downcast_ref::<String>().cloned())
            .unwrap_or_else(|| "?".into());
        Outcome::Panic(format!("panic: {}", msg.lines().next().unwrap_or("?")))
    })
}

/// `--child CORPUS START OUT`: evaluate `CORPUS` from line `START` to the end,
/// appending one flushed result line per expression to `OUT`. Runs under the
/// [`MEM_LIMIT`] budget, so a runaway allocation aborts *this* process only.
///
/// The flush-per-line is what lets the parent attribute a hard death (abort,
/// SIGSEGV, OOM kill, timeout) to an exact expression: the first index with no
/// line is the one that killed the child.
fn child_main(corpus: &Path, start: usize, out: &Path) -> ! {
    LIMIT.store(MEM_LIMIT, Ordering::Relaxed);
    panic::set_hook(Box::new(|_| {}));

    let text = std::fs::read_to_string(corpus).expect("corpus");
    let mut f = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(out)
        .expect("outcomes");

    for (i, expr) in text.lines().enumerate().skip(start) {
        let (tag, payload) = match eval_one(expr) {
            Outcome::Val(v) => ("V", v),
            Outcome::Err(e) => ("E", e),
            Outcome::Panic(p) => ("P", p),
            Outcome::Missing => ("P", "vanished".into()),
        };
        // One line, one flush — a crash on the *next* expression must not lose
        // the answer to this one. A value can legally contain a newline
        // (`nr2char(10)`), which would split the record; Vim's `writefile()`
        // encodes an embedded newline as NUL, so encode it the same way here and
        // the two transports stay byte-comparable.
        let _ = writeln!(f, "{i}\t{tag}\t{}", payload.replace('\n', "\0"));
        let _ = f.flush();
    }
    std::process::exit(0);
}

/// Read a result file as *bytes*, lossily decoded.
///
/// Never `read_to_string`: an engine's output is not necessarily valid UTF-8
/// (`nr2char(200)`, `blob2str(0zFF)`, and Vim's NUL-for-newline encoding all
/// produce non-UTF-8 bytes), and a strict decode would throw away every result
/// in the file — which silently looks like "the oracle answered nothing".
fn slurp(path: &Path) -> String {
    std::fs::read(path)
        .map(|b| String::from_utf8_lossy(&b).into_owned())
        .unwrap_or_default()
}

/// Parse an outcomes file (child protocol) into `res`.
fn read_outcomes(path: &Path, res: &mut [Outcome]) {
    let text = slurp(path);
    for line in text.lines() {
        let mut it = line.splitn(3, '\t');
        let (Some(i), Some(tag), Some(payload)) = (it.next(), it.next(), it.next()) else {
            continue;
        };
        let Ok(i) = i.parse::<usize>() else { continue };
        if i >= res.len() {
            continue;
        }
        res[i] = match tag {
            "V" => Outcome::Val(payload.to_string()),
            "E" => Outcome::Err(payload.to_string()),
            _ => Outcome::Panic(payload.to_string()),
        };
    }
}

/// Run the whole corpus through vimlrs in child processes, restarting after any
/// expression that kills or hangs a child and recording it as a crash finding.
fn run_vimlrs(exprs: &[String], tmp: &Path) -> Vec<Outcome> {
    let corpus = tmp.join("corpus.txt");
    let out = tmp.join("outcomes.txt");
    std::fs::write(&corpus, format!("{}\n", exprs.join("\n"))).expect("write corpus");
    let _ = std::fs::remove_file(&out);

    let me = std::env::current_exe().expect("current exe");
    let mut res = vec![Outcome::Missing; exprs.len()];
    let mut next = 0usize;

    while next < exprs.len() {
        let mut child = Command::new(&me)
            .arg("--child")
            .arg(&corpus)
            .arg(next.to_string())
            .arg(&out)
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()
            .expect("spawn child");
        let finished = wait_bounded(&mut child, CHUNK_TIMEOUT);
        read_outcomes(&out, &mut res);

        // First still-unanswered index: the expression the child died on.
        let Some(stuck) = (next..exprs.len()).find(|&i| res[i] == Outcome::Missing) else {
            break;
        };
        if finished && stuck == exprs.len() {
            break;
        }
        res[stuck] = Outcome::Panic(if finished {
            "crashed (abort / OOM / signal)".into()
        } else {
            format!("hung (>{}s)", CHUNK_TIMEOUT.as_secs())
        });
        next = stuck + 1;
    }
    res
}

/// Quote a generated expression as a single-quoted VimL string literal (the
/// only quoting form with no escape sequences: `''` is the sole escape).
fn vim_quote(s: &str) -> String {
    format!("'{}'", s.replace('\'', "''"))
}

/// Build the driver script an oracle runs for `exprs[lo..hi]`: re-establish the
/// prelude, `eval()` each expression inside `try`/`catch`, and append the
/// `string()` of the value (or the exception) to `out_path`.
///
/// Results are written with `writefile(…, 'a')` *per expression*, not batched at
/// the end, for the same reason the child flushes per line: an expression that
/// kills or hangs the editor must not take the answers before it down too.
fn driver(exprs: &[String], lo: usize, hi: usize, out_path: &Path) -> String {
    let mut s = String::new();
    s.push_str("set cpo&vim\n");
    let _ = writeln!(
        s,
        "let s:exprs = [{}]",
        exprs[lo..hi]
            .iter()
            .map(|e| vim_quote(e))
            .collect::<Vec<_>>()
            .join(",")
    );
    let _ = writeln!(
        s,
        "let s:out = {}",
        vim_quote(&out_path.display().to_string())
    );
    let _ = writeln!(s, "for s:i in range({lo}, {})", hi - 1);
    for line in PRELUDE {
        let _ = writeln!(s, "  {line}");
    }
    s.push_str("  try\n");
    let _ = writeln!(
        s,
        "    let s:r = s:i . \"\\tV\\t\" . string(eval(s:exprs[s:i - {lo}]))"
    );
    s.push_str("  catch\n");
    s.push_str("    let s:r = s:i . \"\\tE\\t\" . v:exception\n");
    s.push_str("  endtry\n");
    s.push_str("  call writefile([s:r], s:out, 'a')\n");
    s.push_str("endfor\n");
    s.push_str("qa!\n");
    s
}

/// Read an oracle's output file into `res` (same three-field protocol as the
/// child, except the payload of an `E` line is the raw exception, from which
/// only the E-number is kept).
fn read_oracle(path: &Path, res: &mut [Outcome]) {
    let text = slurp(path);
    for line in text.lines() {
        let mut it = line.splitn(3, '\t');
        let (Some(i), Some(tag), Some(payload)) = (it.next(), it.next(), it.next()) else {
            continue;
        };
        let Ok(i) = i.parse::<usize>() else { continue };
        if i >= res.len() {
            continue;
        }
        res[i] = match tag {
            "V" => Outcome::Val(payload.to_string()),
            "E" => Outcome::Err(enumber(payload)),
            _ => continue,
        };
    }
}

/// Run one oracle over the corpus in [`CHUNK`]-sized processes, bounded by
/// [`CHUNK_TIMEOUT`]. An expression that kills or hangs the editor
/// (`range(9223372036854775807)` hangs both of them) stays [`Outcome::Missing`]
/// and the next process resumes right after it — one pathological case costs
/// one expression, not the rest of the run.
fn run_oracle(engine: &str, exprs: &[String], tmp: &Path) -> Vec<Outcome> {
    let script = tmp.join(format!("drv_{engine}.vim"));
    let out = tmp.join(format!("out_{engine}.txt"));
    let _ = std::fs::remove_file(&out);

    let mut res = vec![Outcome::Missing; exprs.len()];
    let mut next = 0usize;

    while next < exprs.len() {
        let hi = (next + CHUNK).min(exprs.len());
        std::fs::write(&script, driver(exprs, next, hi, &out)).expect("write driver");

        let mut cmd = Command::new(engine);
        if engine == "nvim" {
            cmd.args(["--headless", "--clean", "-S"]).arg(&script);
        } else {
            cmd.args(["-es", "-u", "NONE", "-i", "NONE", "-S"])
                .arg(&script);
        }
        let spawned = cmd
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn();
        let Ok(mut child) = spawned else {
            return res; // engine not installed — caller reports it
        };
        wait_bounded(&mut child, CHUNK_TIMEOUT);
        read_oracle(&out, &mut res);

        // Skip past whatever the editor choked on and carry on from there.
        next = match (next..hi).find(|&i| res[i] == Outcome::Missing) {
            Some(stuck) => stuck + 1,
            None => hi,
        };
    }
    res
}

// ─── Triage ─────────────────────────────────────────────────────────────────

#[derive(Clone, Copy, PartialEq)]
enum Class {
    Ok,
    Gap,
    Panic,
    Divergent,
    OracleFail,
}

/// Classify one expression from the three engines' outcomes.
fn classify(v: &Outcome, nv: &Outcome, vi: &Outcome) -> Class {
    if matches!(v, Outcome::Panic(_)) {
        return Class::Panic;
    }
    match (nv, vi) {
        (Outcome::Missing, _) | (_, Outcome::Missing) => Class::OracleFail,
        // Both oracles agree: that is the spec, and vimlrs either matches it or
        // has a gap.
        (a, b) if a == b => {
            if v == a {
                Class::Ok
            } else {
                Class::Gap
            }
        }
        // Vim and Neovim genuinely differ here. vimlrs ports the *Neovim* eval
        // engine, so matching nvim is parity; matching neither is still only
        // advisory, because there is no single spec to be wrong about.
        _ => {
            if v == nv {
                Class::Ok
            } else {
                Class::Divergent
            }
        }
    }
}

/// Bucket key for deduplication: the leading builtin name (or `<expr>` for a
/// pure-operator case) plus the two outcome kinds. Hundreds of instances of one
/// bug collapse to one report line with a count.
fn signature(expr: &str, v: &Outcome, o: &Outcome) -> String {
    let head: String = expr
        .trim_start_matches(['(', '!', '-', '+'])
        .chars()
        .take_while(|c| c.is_ascii_alphanumeric() || *c == '_')
        .collect();
    let name = if head.is_empty() {
        "<expr>".to_string()
    } else {
        head
    };
    let kind = |o: &Outcome| match o {
        Outcome::Val(_) => "val".to_string(),
        Outcome::Err(e) => e.clone(),
        Outcome::Panic(_) => "panic".into(),
        Outcome::Missing => "none".into(),
    };
    format!("{name} [{} vs {}]", kind(v), kind(o))
}

// ─── main ───────────────────────────────────────────────────────────────────

struct Args {
    count: usize,
    seed: u64,
    depth: u32,
    only: Vec<String>,
    corpus: Option<String>,
    verbose: bool,
    /// Generate *statements* (`:let`, `:if`, `:for`, `:try`, `|`-separated lines)
    /// instead of expressions, compared through `execute()`.
    stmts: bool,
    /// Generate *regex* cases: grammar-built patterns through match/matchstr/
    /// matchlist/substitute/split.
    regex: bool,
}

fn parse_args() -> Args {
    let mut a = Args {
        count: 1000,
        seed: 1,
        depth: 2,
        only: Vec::new(),
        corpus: None,
        verbose: false,
        stmts: false,
        regex: false,
    };
    let argv: Vec<String> = std::env::args().skip(1).collect();
    let mut i = 0;
    while i < argv.len() {
        let next = |i: usize| argv.get(i + 1).cloned().unwrap_or_default();
        match argv[i].as_str() {
            "--count" | "-n" => {
                a.count = next(i).parse().unwrap_or(a.count);
                i += 1;
            }
            "--seed" | "-s" => {
                a.seed = next(i).parse().unwrap_or(a.seed);
                i += 1;
            }
            "--depth" | "-d" => {
                a.depth = next(i).parse().unwrap_or(a.depth);
                i += 1;
            }
            "--only" => {
                a.only = next(i).split(',').map(str::to_string).collect();
                i += 1;
            }
            "--corpus" => {
                a.corpus = Some(next(i));
                i += 1;
            }
            "--stmts" => a.stmts = true,
            "--regex" => a.regex = true,
            "--verbose" | "-v" => a.verbose = true,
            "--help" | "-h" => {
                println!(
                    "fuzz-parity — differential fuzzer: vimlrs vs nvim + vim\n\n\
                     USAGE: fuzz-parity [--count N] [--seed S] [--depth D]\n\
                     \x20                [--only fn1,fn2] [--corpus FILE] [--verbose]\n\n\
                     --count N     expressions to generate (default 1000)\n\
                     --seed S      PRNG seed; same seed → same corpus (default 1)\n\
                     --depth D     max expression nesting depth (default 2)\n\
                     --only LIST   restrict generation to these builtins\n\
                     --stmts       fuzz STATEMENTS (:let/:if/:for/:try/`|` lines) via execute()\n\
                     --regex       fuzz REGEX: grammar-built patterns x subjects x APIs\n\
                     --corpus FILE append confirmed gaps as `expr<TAB>expected` lines\n\
                     --verbose     list every case, not just divergences"
                );
                std::process::exit(0);
            }
            _ => {}
        }
        i += 1;
    }
    a
}

fn main() {
    // `--child CORPUS START OUT` — the worker half; never used by hand.
    let argv: Vec<String> = std::env::args().collect();
    if argv.get(1).map(String::as_str) == Some("--child") {
        let corpus = PathBuf::from(&argv[2]);
        let start: usize = argv[3].parse().expect("child start index");
        let out = PathBuf::from(&argv[4]);
        child_main(&corpus, start, &out);
    }

    let args = parse_args();

    let funcs: Vec<(&str, &[Shape])> = if args.only.is_empty() {
        FUNCS.to_vec()
    } else {
        FUNCS
            .iter()
            .filter(|(n, _)| args.only.iter().any(|o| o == n))
            .copied()
            .collect()
    };
    if funcs.is_empty() {
        eprintln!("fuzz-parity: --only matched no allow-listed builtin");
        std::process::exit(2);
    }

    // Generate.
    let mut rng = Rng(args.seed.wrapping_mul(0x9E37_79B9_7F4A_7C15) | 1);
    let mut exprs: Vec<String> = Vec::with_capacity(args.count);
    while exprs.len() < args.count {
        let e = if args.regex {
            gen_regex(&mut rng)
        } else if args.stmts {
            gen_stmt(&mut rng, &funcs, args.depth)
        } else {
            gen_expr(&mut rng, &funcs, args.depth)
        };
        // A newline or NUL in a generated literal would break the line-oriented
        // oracle transport, not the interpreter — drop those rather than report
        // a transport artifact as a bug.
        if !e.contains('\n') && !e.contains('\0') {
            exprs.push(e);
        }
    }
    eprintln!(
        "fuzz-parity: {} exprs, seed {}, depth {}",
        exprs.len(),
        args.seed,
        args.depth
    );

    let tmp = std::env::temp_dir().join("vimlrs-fuzz");
    std::fs::create_dir_all(&tmp).expect("tmp dir");

    // vimlrs, in child processes (crash/hang/OOM are findings, not lost runs).
    let mine = run_vimlrs(&exprs, &tmp);

    // Oracles, chunked processes with the same guards.
    let nv = run_oracle("nvim", &exprs, &tmp);
    let vi = run_oracle("vim", &exprs, &tmp);
    if nv.iter().all(|o| *o == Outcome::Missing) {
        eprintln!("fuzz-parity: nvim produced no results — is it installed?");
    }
    if vi.iter().all(|o| *o == Outcome::Missing) {
        eprintln!("fuzz-parity: vim produced no results — is it installed?");
    }

    // Triage, deduplicated by signature.
    let mut buckets: BTreeMap<(u8, String), (usize, String, String, String)> = BTreeMap::new();
    let mut counts = [0usize; 5];
    let mut corpus_lines: Vec<String> = Vec::new();

    for (i, e) in exprs.iter().enumerate() {
        let class = classify(&mine[i], &nv[i], &vi[i]);
        let slot = match class {
            Class::Ok => 0,
            Class::Gap => 1,
            Class::Panic => 2,
            Class::Divergent => 3,
            Class::OracleFail => 4,
        };
        counts[slot] += 1;
        if args.verbose {
            println!(
                "[{}] {e}\n    viml={} nvim={} vim={}",
                match class {
                    Class::Ok => "ok",
                    Class::Gap => "GAP",
                    Class::Panic => "PANIC",
                    Class::Divergent => "div",
                    Class::OracleFail => "oracle-fail",
                },
                mine[i].show(),
                nv[i].show(),
                vi[i].show()
            );
        }
        if matches!(class, Class::Ok | Class::OracleFail) {
            continue;
        }
        // Freeze confirmed gaps (and only those) into the replay corpus, with
        // the oracle's answer as the expectation.
        if class == Class::Gap {
            if let Outcome::Val(want) = &nv[i] {
                corpus_lines.push(format!("{e}\t{want}"));
            } else if let Outcome::Err(want) = &nv[i] {
                corpus_lines.push(format!("{e}\t!{want}"));
            }
        }
        let sig = signature(e, &mine[i], &nv[i]);
        let entry = buckets.entry((slot as u8, sig)).or_insert_with(|| {
            (
                0,
                e.clone(),
                mine[i].show(),
                format!("nvim={} vim={}", nv[i].show(), vi[i].show()),
            )
        });
        entry.0 += 1;
    }

    // Report.
    let heading = |slot: u8| match slot {
        1 => "CONFIRMED GAPS (nvim == vim, vimlrs differs)",
        2 => "PANICS (vimlrs crashed)",
        3 => "VIM/NEOVIM DIVERGENCE (advisory — no single spec)",
        _ => "OTHER",
    };
    let mut last = 0u8;
    for ((slot, sig), (n, expr, got, want)) in &buckets {
        if *slot != last {
            println!("\n══ {} ══", heading(*slot));
            last = *slot;
        }
        println!("\n  {sig}  ×{n}");
        println!("    expr:   {expr}");
        println!("    vimlrs: {got}");
        println!("    oracle: {want}");
    }

    println!(
        "\n── summary ──\n  ok:          {}\n  GAPS:        {} ({} distinct)\n  PANICS:      {}\n  divergent:   {}\n  oracle-fail: {}",
        counts[0],
        counts[1],
        buckets.keys().filter(|(s, _)| *s == 1).count(),
        counts[2],
        counts[3],
        counts[4]
    );

    if let Some(path) = &args.corpus {
        if !corpus_lines.is_empty() {
            corpus_lines.sort();
            corpus_lines.dedup();
            let prev = std::fs::read_to_string(path).unwrap_or_default();
            let mut all: Vec<&str> = prev
                .lines()
                .chain(corpus_lines.iter().map(String::as_str))
                .collect();
            all.sort_unstable();
            all.dedup();
            std::fs::write(path, format!("{}\n", all.join("\n"))).expect("write corpus");
            println!("  corpus:      {} cases → {path}", all.len());
        }
    }

    // Exit non-zero when there is something to fix, so the harness can gate.
    if counts[1] > 0 || counts[2] > 0 {
        std::process::exit(1);
    }
}