logicaffeine-compile 0.10.1

LOGOS compilation pipeline - codegen and interpreter
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
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
//! Register bytecode VM (work/VM_PLAN.md).
//!
//! A fast, portable interpreter that runs the same `RuntimeValue` semantics as
//! the tree-walker. It is the browser/WASM execution engine and the substrate
//! the native copy-and-patch JIT tiers up from. Built incrementally, RED-first,
//! differential-tested against the tree-walker.

pub mod compiler;
// Off-thread native compilation (HOTSWAP §6). Native-only: it needs std::thread and
// the forge backend, neither of which exists on wasm (which never installs a tier).
#[cfg(not(target_arch = "wasm32"))]
mod bg_compile;
// AOT-native tier loader (HOTSWAP §Axis-3): dlopen a rustc-built cdylib. Native-only.
#[cfg(not(target_arch = "wasm32"))]
pub mod aot_tier;
// Background AOT-native compilation (HOTSWAP §Axis-3 / P18): build+load off-thread.
#[cfg(not(target_arch = "wasm32"))]
pub mod bg_aot;
// Relocatable per-function bytecode units (HOTSWAP §7) — the producer behind the
// Axis-1 warm side-table and the OPFS tier cache. Pure data; available on all targets.
pub mod fn_bytecode;
// LOGOS_TIER_TRACE observability (HOTSWAP §P13): one stderr line per tier hot-swap.
pub mod tier_trace;
// Tier cache (HOTSWAP §P12): persist a compiled FnBytecode keyed by source+config+tier.
pub mod tier_cache;
#[cfg(test)]
mod fuzz;
/// Bytecode disassembler — readable text for the Studio debug drawer and any
/// future `largo --disasm`. Pure, headless, no VM state.
pub mod disasm;
mod instruction;
mod machine;
/// WS-F representation overhaul: the narrow (8-byte) NaN-boxed register-file
/// value (see the module docs). Under `feature = "narrow-value"` it backs
/// [`value::Value`] and the VM register file; the default build keeps the fat
/// `RuntimeValue` path, so the scalar accessors/arith here are exercised only by
/// the in-module tests then. `allow(dead_code)` keeps the default build quiet
/// about the methods only the narrow path calls.
#[cfg_attr(not(feature = "narrow-value"), allow(dead_code))]
mod nanbox;
mod native_tier;
mod value;

/// WebAssembly code generation from VM bytecode: the shared byte emitter (`wasm::encode` +
/// `wasm::func`, always built) plus the WS6 browser WASM-JIT tier (`wasm::region_jit`, behind
/// the `wasm-jit` feature). The direct AOT backend (`largo build --emit wasm`) consumes the
/// same emitter.
pub mod wasm;

/// Back-compat facade: the WS6 browser WASM-JIT tier moved into the shared `wasm` codegen
/// submodule — `wasm::func` for the byte emitter, `wasm::region_jit` for the tier + host.
/// `machine.rs` (`super::wasm_jit::WasmTier`) and the `wasm_jit_*` differential/browser test
/// suites still reach it here, unchanged.
#[cfg(feature = "wasm-jit")]
pub mod wasm_jit {
    pub use super::wasm::func::{compile_function_to_wasm, compile_region_to_wasm};
    pub use super::wasm::region_jit::WasmTier;
    #[cfg(target_arch = "wasm32")]
    pub use super::wasm::region_jit::run_on_host;
}

pub use compiler::Compiler;
pub use disasm::{disassemble, format_constant, op_io, DisasmLine, OpIo};
pub use instruction::{ChanElem, CompiledProgram, Constant, Op};
pub use machine::Vm;
pub(crate) use machine::{DebugFrameView, DebugView, DebugVmState, HeapObjView};
pub(crate) use machine::{VmBlock, VmStep};
pub use native_tier::{
    install_native_tier, installed_native_tier, ArrayPin, CalleeSig, FnTable, HoistGuard, NativeCtx,
    NativeFn,
    NativeFrame, NativeOutcome, NativeRet, NativeTier, ObservedKind, ParamKind, PinElem,
    RegBox, RegionFn, RegionOutcome, RegionReturn, RegionReturnKind, SlotKind,
    NATIVE_TIER_THRESHOLD, REGION_TIER_THRESHOLD,
};
pub use value::Value;

use crate::ast::stmt::Stmt;
use crate::intern::Interner;

/// Hard cap on registers a single frame may claim. Generous for real programs;
/// prevents a pathological frame from claiming the whole register file.
pub const MAX_REGISTERS_PER_FRAME: usize = 16_384;
/// Hard cap on the total register file across all live frames. Deep recursion
/// hits this and errors instead of consuming unbounded memory.
pub const MAX_REGISTER_FILE: usize = 1 << 20;
/// Maximum expression nesting depth the compiler will descend before erroring
/// (guards the compiler's own recursion against adversarial inputs). Debug
/// builds allocate every match arm's locals in `compile_expr_into_inner`'s
/// frame — measured in the tens of KiB per nesting level as the compiler
/// grows — so 64 keeps the worst case well inside a default 2 MiB
/// test-thread stack while still exceeding any realistic expression depth.
/// (An explicit-work-stack compiler would remove the native-stack coupling
/// entirely; tracked as future work.)
pub const MAX_EXPR_DEPTH: usize = 64;

/// Shannon entropy of a two-outcome branch profile, in bits (EXODIA 3.3).
/// 0.0 = perfectly predictable (the hardware branch predictor wins; Tier-1
/// code is fine), 1.0 = a coin flip (Tier-2 should restructure the branch
/// away). Zero samples are zero entropy.
pub fn branch_entropy(taken: u64, not_taken: u64) -> f64 {
    let total = taken + not_taken;
    if total == 0 || taken == 0 || not_taken == 0 {
        return 0.0;
    }
    let p = taken as f64 / total as f64;
    -(p * p.log2() + (1.0 - p) * (1.0 - p).log2())
}

/// Compile a statement block to bytecode and run it, returning captured output.
pub fn compile_and_run(stmts: &[Stmt], interner: &Interner) -> Result<String, String> {
    let program = Compiler::compile(stmts, interner)?;
    let mut vm = Vm::new(&program);
    if let Some(tier) = installed_native_tier() {
        vm = vm.with_native_tier(tier);
    }
    vm.run()?;
    Ok(vm.output().to_string())
}

/// Compile and run, preserving partial output alongside any error — the shape
/// the differential harness compares against the tree-walker (which also keeps
/// the lines emitted before a runtime error). `types` carries the discovery
/// pass's struct definitions (for default-fill on construction).
pub fn run_to_outcome(
    stmts: &[Stmt],
    interner: &Interner,
    types: Option<&crate::analysis::TypeRegistry>,
    policies: Option<&crate::analysis::PolicyRegistry>,
) -> (String, Option<String>) {
    let oracle = crate::optimize::oracle_analyze_with(stmts, interner);
    let program = match Compiler::compile_with_oracle(stmts, interner, types, Some(oracle)) {
        Ok(p) => p,
        Err(e) => return (String::new(), Some(e)),
    };
    let mut vm = Vm::new(&program);
    if let Some(tier) = installed_native_tier() {
        vm = vm.with_native_tier(tier);
    }
    if let Some(registry) = policies {
        vm = vm.with_policy_ctx(registry, interner);
    }
    match vm.run() {
        Ok(()) => (vm.output().to_string(), None),
        Err(e) => (vm.output().to_string(), Some(e)),
    }
}

/// [`run_to_outcome`] with the program argument vector for the `args()`
/// system native (full argv; index 0 is the program name) and an optional
/// caller-supplied native tier. A `Some(tier)` overrides the process-wide
/// installed tier — differential suites use a private tier per program so
/// its compile counters are isolated from every other test in the binary.
pub fn run_to_outcome_with_args(
    stmts: &[Stmt],
    interner: &Interner,
    types: Option<&crate::analysis::TypeRegistry>,
    policies: Option<&crate::analysis::PolicyRegistry>,
    program_args: &[String],
    tier: Option<&dyn NativeTier>,
) -> (String, Option<String>) {
    let oracle = crate::optimize::oracle_analyze_with(stmts, interner);
    let program = match Compiler::compile_with_oracle(stmts, interner, types, Some(oracle)) {
        Ok(p) => p,
        Err(e) => return (String::new(), Some(e)),
    };
    let mut vm = Vm::new(&program).with_program_args(program_args.to_vec());
    let chosen: Option<&dyn NativeTier> = match tier {
        Some(t) => Some(t),
        None => installed_native_tier().map(|t| t as &dyn NativeTier),
    };
    if let Some(t) = chosen {
        vm = vm.with_native_tier(t);
    }
    if let Some(registry) = policies {
        vm = vm.with_policy_ctx(registry, interner);
    }
    match vm.run() {
        Ok(()) => (vm.output().to_string(), None),
        Err(e) => (vm.output().to_string(), Some(e)),
    }
}

/// [`run_to_outcome_with_args`] with BACKGROUND native compilation (HOTSWAP §6): hot
/// functions compile on a worker thread while the interpreter keeps running bytecode,
/// then their chains are drained + published. Requires a process-installed tier (the
/// worker needs a `&'static` backend); with none installed it runs pure bytecode.
/// Drains outstanding compiles before returning so the native tier engages
/// deterministically for the differential gates.
#[cfg(not(target_arch = "wasm32"))]
pub fn run_to_outcome_bg(
    stmts: &[Stmt],
    interner: &Interner,
    types: Option<&crate::analysis::TypeRegistry>,
    policies: Option<&crate::analysis::PolicyRegistry>,
    program_args: &[String],
) -> (String, Option<String>) {
    let oracle = crate::optimize::oracle_analyze_with(stmts, interner);
    let program = match Compiler::compile_with_oracle(stmts, interner, types, Some(oracle)) {
        Ok(p) => p,
        Err(e) => return (String::new(), Some(e)),
    };
    let mut vm = Vm::new(&program).with_program_args(program_args.to_vec());
    if let Some(t) = installed_native_tier() {
        vm = vm.with_bg_native_tier(t);
    }
    if let Some(registry) = policies {
        vm = vm.with_policy_ctx(registry, interner);
    }
    let result = match vm.run() {
        Ok(()) => (vm.output().to_string(), None),
        Err(e) => (vm.output().to_string(), Some(e)),
    };
    vm.drain_pending_compiles();
    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::arena::Arena;
    use crate::ast::stmt::{BinaryOpKind, Expr, Literal, Stmt};
    use crate::intern::Interner;
    use std::cell::RefCell;
    use std::rc::Rc;

    /// Run a program through the production tree-walker, capturing its output —
    /// the independent oracle every VM result is checked against.
    fn run_treewalk(stmts: &[Stmt], interner: &Interner) -> Result<String, String> {
        use crate::interpreter::{Interpreter, OutputCallback};
        let buf = Rc::new(RefCell::new(String::new()));
        let sink = buf.clone();
        let cb: OutputCallback = Rc::new(RefCell::new(move |s: String| {
            sink.borrow_mut().push_str(&s);
            sink.borrow_mut().push('\n');
        }));
        let mut interp = Interpreter::new(interner).with_output_callback(cb);
        interp.run_sync(stmts)?;
        let out = buf.borrow().clone();
        Ok(out)
    }

    fn normalize(s: &str) -> String {
        s.lines()
            .map(|l| l.trim_end())
            .filter(|l| !l.is_empty())
            .collect::<Vec<_>>()
            .join("\n")
    }

    /// The differential gate: the VM's observable output must equal the
    /// tree-walker's for the same program.
    fn assert_vm_eq_treewalk(stmts: &[Stmt], interner: &Interner) {
        let vm_out = compile_and_run(stmts, interner).expect("vm run failed");
        let tw_out = run_treewalk(stmts, interner).expect("tree-walker run failed");
        assert_eq!(
            normalize(&vm_out),
            normalize(&tw_out),
            "VM output diverged from the tree-walker oracle"
        );
    }

    // ---- AST builder helpers (keep the function tests readable) ---------------

    use crate::ast::stmt::TypeExpr;
    use crate::intern::Symbol;

    fn num<'a>(ea: &'a Arena<Expr<'a>>, n: i64) -> &'a Expr<'a> {
        ea.alloc(Expr::Literal(Literal::Number(n)))
    }
    fn idref<'a>(ea: &'a Arena<Expr<'a>>, s: Symbol) -> &'a Expr<'a> {
        ea.alloc(Expr::Identifier(s))
    }
    fn bin<'a>(ea: &'a Arena<Expr<'a>>, op: BinaryOpKind, l: &'a Expr<'a>, r: &'a Expr<'a>) -> &'a Expr<'a> {
        ea.alloc(Expr::BinaryOp { op, left: l, right: r })
    }
    fn calle<'a>(ea: &'a Arena<Expr<'a>>, f: Symbol, args: Vec<&'a Expr<'a>>) -> &'a Expr<'a> {
        ea.alloc(Expr::Call { function: f, args })
    }
    fn letb<'a>(var: Symbol, value: &'a Expr<'a>) -> Stmt<'a> {
        Stmt::Let { var, ty: None, value, mutable: false }
    }
    fn ret<'a>(value: &'a Expr<'a>) -> Stmt<'a> {
        Stmt::Return { value: Some(value) }
    }
    fn show<'a>(ea: &'a Arena<Expr<'a>>, show_sym: Symbol, obj: &'a Expr<'a>) -> Stmt<'a> {
        Stmt::Show { object: obj, recipient: idref(ea, show_sym) }
    }
    fn fndef<'a>(
        name: Symbol,
        params: Vec<(Symbol, &'a TypeExpr<'a>)>,
        body: &'a [Stmt<'a>],
    ) -> Stmt<'a> {
        Stmt::FunctionDef {
            name,
            generics: vec![],
            params,
            body,
            return_type: None,
            is_native: false,
            native_path: None,
            is_exported: false,
            export_target: None,
            opt_flags: Default::default(),
        }
    }

    #[test]
    fn vm_function_single_param() {
        // ## To double (n: Int) -> Int: Return n * 2.
        // Main: Let r be double(21). Show r.   → 42
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let ta: Arena<TypeExpr> = Arena::new();
        let mut it = Interner::new();
        let double = it.intern("double");
        let n = it.intern("n");
        let r = it.intern("r");
        let show_s = it.intern("show");
        let int_ty = ta.alloc(TypeExpr::Primitive(it.intern("Int")));

        let body: &[Stmt] = sa.alloc_slice(vec![ret(bin(&ea, BinaryOpKind::Multiply, idref(&ea, n), num(&ea, 2)))]);
        let main_call = calle(&ea, double, vec![num(&ea, 21)]);

        let stmts = vec![
            fndef(double, vec![(n, int_ty)], body),
            letb(r, main_call),
            show(&ea, show_s, idref(&ea, r)),
        ];
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "42");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_function_two_params() {
        // ## To add (a, b): Return a + b.  Main: Show add(3, 4).   → 7
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let ta: Arena<TypeExpr> = Arena::new();
        let mut it = Interner::new();
        let add = it.intern("add");
        let a = it.intern("a");
        let b = it.intern("b");
        let r = it.intern("r");
        let show_s = it.intern("show");
        let int_ty = ta.alloc(TypeExpr::Primitive(it.intern("Int")));

        let body: &[Stmt] = sa.alloc_slice(vec![ret(bin(&ea, BinaryOpKind::Add, idref(&ea, a), idref(&ea, b)))]);
        let stmts = vec![
            fndef(add, vec![(a, int_ty), (b, int_ty)], body),
            letb(r, calle(&ea, add, vec![num(&ea, 3), num(&ea, 4)])),
            show(&ea, show_s, idref(&ea, r)),
        ];
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "7");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_function_recursion_factorial() {
        // ## To factorial (n: Int): If n <= 1: Return 1. Return n * factorial(n - 1).
        // Main: Show factorial(5).   → 120
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let ta: Arena<TypeExpr> = Arena::new();
        let mut it = Interner::new();
        let factorial = it.intern("factorial");
        let n = it.intern("n");
        let r = it.intern("r");
        let show_s = it.intern("show");
        let int_ty = ta.alloc(TypeExpr::Primitive(it.intern("Int")));

        let then_blk: &[Stmt] = sa.alloc_slice(vec![ret(num(&ea, 1))]);
        let cond = bin(&ea, BinaryOpKind::LtEq, idref(&ea, n), num(&ea, 1));
        let rec = calle(&ea, factorial, vec![bin(&ea, BinaryOpKind::Subtract, idref(&ea, n), num(&ea, 1))]);
        let tail = ret(bin(&ea, BinaryOpKind::Multiply, idref(&ea, n), rec));
        let body: &[Stmt] = sa.alloc_slice(vec![
            Stmt::If { cond, then_block: then_blk, else_block: None },
            tail,
        ]);

        let stmts = vec![
            fndef(factorial, vec![(n, int_ty)], body),
            letb(r, calle(&ea, factorial, vec![num(&ea, 5)])),
            show(&ea, show_s, idref(&ea, r)),
        ];
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "120");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_function_nested_calls() {
        // triple(n)=n*3, double(n)=n*2.  Main: Show double(triple(2)).   → 12
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let ta: Arena<TypeExpr> = Arena::new();
        let mut it = Interner::new();
        let triple = it.intern("triple");
        let double = it.intern("double");
        let n = it.intern("n");
        let r = it.intern("r");
        let show_s = it.intern("show");
        let int_ty = ta.alloc(TypeExpr::Primitive(it.intern("Int")));

        let triple_body: &[Stmt] = sa.alloc_slice(vec![ret(bin(&ea, BinaryOpKind::Multiply, idref(&ea, n), num(&ea, 3)))]);
        let double_body: &[Stmt] = sa.alloc_slice(vec![ret(bin(&ea, BinaryOpKind::Multiply, idref(&ea, n), num(&ea, 2)))]);
        let inner = calle(&ea, triple, vec![num(&ea, 2)]);
        let outer = calle(&ea, double, vec![inner]);
        let stmts = vec![
            fndef(triple, vec![(n, int_ty)], triple_body),
            fndef(double, vec![(n, int_ty)], double_body),
            letb(r, outer),
            show(&ea, show_s, idref(&ea, r)),
        ];
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "12");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_function_with_local_variable() {
        // ## To compute (x): Let y be x * x. Return y + 1.  Main: Show compute(4).   → 17
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let ta: Arena<TypeExpr> = Arena::new();
        let mut it = Interner::new();
        let compute = it.intern("compute");
        let x = it.intern("x");
        let y = it.intern("y");
        let r = it.intern("r");
        let show_s = it.intern("show");
        let int_ty = ta.alloc(TypeExpr::Primitive(it.intern("Int")));

        let body: &[Stmt] = sa.alloc_slice(vec![
            letb(y, bin(&ea, BinaryOpKind::Multiply, idref(&ea, x), idref(&ea, x))),
            ret(bin(&ea, BinaryOpKind::Add, idref(&ea, y), num(&ea, 1))),
        ]);
        let stmts = vec![
            fndef(compute, vec![(x, int_ty)], body),
            letb(r, calle(&ea, compute, vec![num(&ea, 4)])),
            show(&ea, show_s, idref(&ea, r)),
        ];
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "17");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_function_conditional_return_fallthrough() {
        // ## To sign (n): If n > 0: Return 1. Return 0.
        // Main: Show sign(5). Show sign(-3).   → 1, 0
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let ta: Arena<TypeExpr> = Arena::new();
        let mut it = Interner::new();
        let sign = it.intern("sign");
        let n = it.intern("n");
        let p = it.intern("p");
        let q = it.intern("q");
        let show_s = it.intern("show");
        let int_ty = ta.alloc(TypeExpr::Primitive(it.intern("Int")));

        let then_blk: &[Stmt] = sa.alloc_slice(vec![ret(num(&ea, 1))]);
        let body: &[Stmt] = sa.alloc_slice(vec![
            Stmt::If { cond: bin(&ea, BinaryOpKind::Gt, idref(&ea, n), num(&ea, 0)), then_block: then_blk, else_block: None },
            ret(num(&ea, 0)),
        ]);
        let stmts = vec![
            fndef(sign, vec![(n, int_ty)], body),
            letb(p, calle(&ea, sign, vec![num(&ea, 5)])),
            show(&ea, show_s, idref(&ea, p)),
            letb(q, calle(&ea, sign, vec![num(&ea, -3)])),
            show(&ea, show_s, idref(&ea, q)),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        let lines: Vec<&str> = out.lines().collect();
        assert_eq!(lines[0], "1");
        assert_eq!(lines[1], "0");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    // ---- Collection builder helpers ----------------------------------------

    fn list_lit<'a>(ea: &'a Arena<Expr<'a>>, items: Vec<&'a Expr<'a>>) -> &'a Expr<'a> {
        ea.alloc(Expr::List(items))
    }
    fn new_coll<'a>(ea: &'a Arena<Expr<'a>>, type_name: Symbol) -> &'a Expr<'a> {
        ea.alloc(Expr::New { type_name, type_args: vec![], init_fields: vec![] })
    }
    fn length_of<'a>(ea: &'a Arena<Expr<'a>>, c: &'a Expr<'a>) -> &'a Expr<'a> {
        ea.alloc(Expr::Length { collection: c })
    }
    fn index_at<'a>(ea: &'a Arena<Expr<'a>>, c: &'a Expr<'a>, i: &'a Expr<'a>) -> &'a Expr<'a> {
        ea.alloc(Expr::Index { collection: c, index: i })
    }
    fn contains_e<'a>(ea: &'a Arena<Expr<'a>>, c: &'a Expr<'a>, v: &'a Expr<'a>) -> &'a Expr<'a> {
        ea.alloc(Expr::Contains { collection: c, value: v })
    }
    fn range_e<'a>(ea: &'a Arena<Expr<'a>>, s: &'a Expr<'a>, e: &'a Expr<'a>) -> &'a Expr<'a> {
        ea.alloc(Expr::Range { start: s, end: e })
    }
    fn push_to<'a>(value: &'a Expr<'a>, collection: &'a Expr<'a>) -> Stmt<'a> {
        Stmt::Push { value, collection }
    }

    #[test]
    fn vm_list_literal_and_length() {
        // Let xs be [10, 20, 30]. Show length of xs.   → 3
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let xs = it.intern("xs");
        let show_s = it.intern("show");
        let lst = list_lit(&ea, vec![num(&ea, 10), num(&ea, 20), num(&ea, 30)]);
        let stmts = vec![
            letb(xs, lst),
            show(&ea, show_s, length_of(&ea, idref(&ea, xs))),
        ];
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "3");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_new_seq_push_length() {
        // Let xs be a new Seq of Int. Push 10 to xs. Push 20 to xs. Show length of xs.   → 2
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let xs = it.intern("xs");
        let seq = it.intern("Seq");
        let show_s = it.intern("show");
        let stmts = vec![
            Stmt::Let { var: xs, ty: None, value: new_coll(&ea, seq), mutable: true },
            push_to(num(&ea, 10), idref(&ea, xs)),
            push_to(num(&ea, 20), idref(&ea, xs)),
            show(&ea, show_s, length_of(&ea, idref(&ea, xs))),
        ];
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "2");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_empty_seq_length_is_zero() {
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let xs = it.intern("xs");
        let seq = it.intern("Seq");
        let show_s = it.intern("show");
        let stmts = vec![
            Stmt::Let { var: xs, ty: None, value: new_coll(&ea, seq), mutable: true },
            show(&ea, show_s, length_of(&ea, idref(&ea, xs))),
        ];
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "0");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_list_index_one_based() {
        // Let xs be [5, 6, 7]. Show xs at 2.   → 6
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let xs = it.intern("xs");
        let show_s = it.intern("show");
        let lst = list_lit(&ea, vec![num(&ea, 5), num(&ea, 6), num(&ea, 7)]);
        let stmts = vec![
            letb(xs, lst),
            show(&ea, show_s, index_at(&ea, idref(&ea, xs), num(&ea, 2))),
        ];
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "6");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_range_to_list() {
        // Let xs be 1 to 5. Show length of xs. Show xs at 3.   → 5, 3
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let xs = it.intern("xs");
        let show_s = it.intern("show");
        let stmts = vec![
            letb(xs, range_e(&ea, num(&ea, 1), num(&ea, 5))),
            show(&ea, show_s, length_of(&ea, idref(&ea, xs))),
            show(&ea, show_s, index_at(&ea, idref(&ea, xs), num(&ea, 3))),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        let lines: Vec<&str> = out.lines().collect();
        assert_eq!(lines[0], "5");
        assert_eq!(lines[1], "3");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_list_contains() {
        // Let xs be [1, 2, 3]. Show xs contains 2. Show xs contains 5.   → true, false
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let xs = it.intern("xs");
        let show_s = it.intern("show");
        let lst = list_lit(&ea, vec![num(&ea, 1), num(&ea, 2), num(&ea, 3)]);
        let stmts = vec![
            letb(xs, lst),
            show(&ea, show_s, contains_e(&ea, idref(&ea, xs), num(&ea, 2))),
            show(&ea, show_s, contains_e(&ea, idref(&ea, xs), num(&ea, 5))),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        let lines: Vec<&str> = out.lines().collect();
        assert_eq!(lines[0], "true");
        assert_eq!(lines[1], "false");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_push_then_index_sum_loop() {
        // Build [2,4,6] by pushing; sum via 1-based indexing in a While loop.   → 12
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let xs = it.intern("xs");
        let seq = it.intern("Seq");
        let total = it.intern("total");
        let i = it.intern("i");
        let show_s = it.intern("show");

        let cond = bin(&ea, BinaryOpKind::LtEq, idref(&ea, i), length_of(&ea, idref(&ea, xs)));
        let elem = index_at(&ea, idref(&ea, xs), idref(&ea, i));
        let body: &[Stmt] = sa.alloc_slice(vec![
            Stmt::Set { target: total, value: bin(&ea, BinaryOpKind::Add, idref(&ea, total), elem) },
            Stmt::Set { target: i, value: bin(&ea, BinaryOpKind::Add, idref(&ea, i), num(&ea, 1)) },
        ]);

        let stmts = vec![
            Stmt::Let { var: xs, ty: None, value: new_coll(&ea, seq), mutable: true },
            push_to(num(&ea, 2), idref(&ea, xs)),
            push_to(num(&ea, 4), idref(&ea, xs)),
            push_to(num(&ea, 6), idref(&ea, xs)),
            Stmt::Let { var: total, ty: None, value: num(&ea, 0), mutable: true },
            Stmt::Let { var: i, ty: None, value: num(&ea, 1), mutable: true },
            Stmt::While { cond, body, decreasing: None },
            show(&ea, show_s, idref(&ea, total)),
        ];
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "12");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_index_out_of_bounds_errors_like_treewalk() {
        // Let xs be [1, 2]. Show xs at 5.   → both VM and tree-walker error.
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let xs = it.intern("xs");
        let show_s = it.intern("show");
        let lst = list_lit(&ea, vec![num(&ea, 1), num(&ea, 2)]);
        let stmts = vec![
            letb(xs, lst),
            show(&ea, show_s, index_at(&ea, idref(&ea, xs), num(&ea, 5))),
        ];
        assert!(compile_and_run(&stmts, &it).is_err(), "VM should error on OOB index");
        assert!(run_treewalk(&stmts, &it).is_err(), "tree-walker should error on OOB index");
    }

    fn text<'a>(ea: &'a Arena<Expr<'a>>, sym: Symbol) -> &'a Expr<'a> {
        ea.alloc(Expr::Literal(Literal::Text(sym)))
    }

    #[test]
    fn vm_set_add_dedups() {
        // new Set; Add 1; Add 2; Add 1 (dup). Show length.   → 2
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let s = it.intern("s");
        let set_ty = it.intern("Set");
        let show_s = it.intern("show");
        let stmts = vec![
            Stmt::Let { var: s, ty: None, value: new_coll(&ea, set_ty), mutable: true },
            Stmt::Add { value: num(&ea, 1), collection: idref(&ea, s) },
            Stmt::Add { value: num(&ea, 2), collection: idref(&ea, s) },
            Stmt::Add { value: num(&ea, 1), collection: idref(&ea, s) },
            show(&ea, show_s, length_of(&ea, idref(&ea, s))),
        ];
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "2");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_set_contains_and_remove() {
        // new Set; Add 1,2,3; Remove 2; Show length; Show s contains 2; Show s contains 3.
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let s = it.intern("s");
        let set_ty = it.intern("Set");
        let show_s = it.intern("show");
        let stmts = vec![
            Stmt::Let { var: s, ty: None, value: new_coll(&ea, set_ty), mutable: true },
            Stmt::Add { value: num(&ea, 1), collection: idref(&ea, s) },
            Stmt::Add { value: num(&ea, 2), collection: idref(&ea, s) },
            Stmt::Add { value: num(&ea, 3), collection: idref(&ea, s) },
            Stmt::Remove { value: num(&ea, 2), collection: idref(&ea, s) },
            show(&ea, show_s, length_of(&ea, idref(&ea, s))),
            show(&ea, show_s, contains_e(&ea, idref(&ea, s), num(&ea, 2))),
            show(&ea, show_s, contains_e(&ea, idref(&ea, s), num(&ea, 3))),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        let lines: Vec<&str> = out.lines().collect();
        assert_eq!(lines, vec!["2", "false", "true"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_map_set_get_length() {
        // new Map; Set item "a" of m to 10; Set item "b" of m to 20.
        // Show item "a" of m; Show length of m.   → 10, 2
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let m = it.intern("m");
        let map_ty = it.intern("Map");
        let key_a = it.intern("a");
        let key_b = it.intern("b");
        let show_s = it.intern("show");
        let stmts = vec![
            Stmt::Let { var: m, ty: None, value: new_coll(&ea, map_ty), mutable: true },
            Stmt::SetIndex { collection: idref(&ea, m), index: text(&ea, key_a), value: num(&ea, 10) },
            Stmt::SetIndex { collection: idref(&ea, m), index: text(&ea, key_b), value: num(&ea, 20) },
            show(&ea, show_s, index_at(&ea, idref(&ea, m), text(&ea, key_a))),
            show(&ea, show_s, length_of(&ea, idref(&ea, m))),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        let lines: Vec<&str> = out.lines().collect();
        assert_eq!(lines[0], "10");
        assert_eq!(lines[1], "2");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_map_overwrite_and_remove() {
        // new Map; Set "a"→1; Set "a"→9 (overwrite); Set "b"→2; Remove "a".
        // Show length; Show item "b" of m.   → 1, 2
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let m = it.intern("m");
        let map_ty = it.intern("Map");
        let key_a = it.intern("a");
        let key_b = it.intern("b");
        let show_s = it.intern("show");
        let stmts = vec![
            Stmt::Let { var: m, ty: None, value: new_coll(&ea, map_ty), mutable: true },
            Stmt::SetIndex { collection: idref(&ea, m), index: text(&ea, key_a), value: num(&ea, 1) },
            Stmt::SetIndex { collection: idref(&ea, m), index: text(&ea, key_a), value: num(&ea, 9) },
            Stmt::SetIndex { collection: idref(&ea, m), index: text(&ea, key_b), value: num(&ea, 2) },
            Stmt::Remove { value: text(&ea, key_a), collection: idref(&ea, m) },
            show(&ea, show_s, length_of(&ea, idref(&ea, m))),
            show(&ea, show_s, index_at(&ea, idref(&ea, m), text(&ea, key_b))),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        let lines: Vec<&str> = out.lines().collect();
        assert_eq!(lines[0], "1");
        assert_eq!(lines[1], "2");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_list_set_index_one_based() {
        // Let xs be [1,2,3]. Set item 2 of xs to 99. Show xs at 2.   → 99
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let xs = it.intern("xs");
        let show_s = it.intern("show");
        let lst = list_lit(&ea, vec![num(&ea, 1), num(&ea, 2), num(&ea, 3)]);
        let stmts = vec![
            Stmt::Let { var: xs, ty: None, value: lst, mutable: true },
            Stmt::SetIndex { collection: idref(&ea, xs), index: num(&ea, 2), value: num(&ea, 99) },
            show(&ea, show_s, index_at(&ea, idref(&ea, xs), num(&ea, 2))),
        ];
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "99");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    // ---- Generative differential fuzzer (VM vs tree-walker) ------------------
    //
    // Generates random total (overflow-free) programs over Int variables with
    // arithmetic, comparisons, conditionals, and bounded loops, then asserts the
    // VM's behavior is identical to the tree-walker's. Any divergence is a real
    // translation bug. Deterministic (SplitMix64) so failures reproduce by seed.

    struct SplitMix64 {
        state: u64,
    }
    impl SplitMix64 {
        fn new(seed: u64) -> Self {
            SplitMix64 { state: seed.wrapping_add(0x9E37_79B9_7F4A_7C15) }
        }
        fn next_u64(&mut self) -> u64 {
            self.state = self.state.wrapping_add(0x9E37_79B9_7F4A_7C15);
            let mut z = self.state;
            z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
            z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
            z ^ (z >> 31)
        }
        fn below(&mut self, n: u64) -> u64 {
            self.next_u64() % n
        }
    }

    fn gen_atom<'a>(ea: &'a Arena<Expr<'a>>, rng: &mut SplitMix64, vars: &[Symbol]) -> &'a Expr<'a> {
        if rng.below(2) == 0 {
            num(ea, rng.below(6) as i64)
        } else {
            idref(ea, vars[rng.below(vars.len() as u64) as usize])
        }
    }

    // depth-1 arithmetic over small atoms. +,- keep values tiny; * uses a small
    // multiplier and / % use a non-zero literal divisor — all provably total and
    // overflow-free for the bounded value range.
    fn gen_arith<'a>(ea: &'a Arena<Expr<'a>>, rng: &mut SplitMix64, vars: &[Symbol]) -> &'a Expr<'a> {
        match rng.below(8) {
            0 | 1 | 2 => {
                let l = gen_atom(ea, rng, vars);
                let r = gen_atom(ea, rng, vars);
                bin(ea, BinaryOpKind::Add, l, r)
            }
            3 | 4 => {
                let l = gen_atom(ea, rng, vars);
                let r = gen_atom(ea, rng, vars);
                bin(ea, BinaryOpKind::Subtract, l, r)
            }
            5 => {
                let l = gen_atom(ea, rng, vars);
                bin(ea, BinaryOpKind::Multiply, l, num(ea, rng.below(4) as i64))
            }
            6 => {
                let l = gen_atom(ea, rng, vars);
                bin(ea, BinaryOpKind::Divide, l, num(ea, 1 + rng.below(5) as i64))
            }
            _ => {
                let l = gen_atom(ea, rng, vars);
                bin(ea, BinaryOpKind::Modulo, l, num(ea, 1 + rng.below(5) as i64))
            }
        }
    }

    fn gen_cmp<'a>(ea: &'a Arena<Expr<'a>>, rng: &mut SplitMix64, vars: &[Symbol]) -> &'a Expr<'a> {
        let l = idref(ea, vars[rng.below(vars.len() as u64) as usize]);
        let r = num(ea, rng.below(6) as i64);
        let op = match rng.below(6) {
            0 => BinaryOpKind::Lt,
            1 => BinaryOpKind::Gt,
            2 => BinaryOpKind::LtEq,
            3 => BinaryOpKind::GtEq,
            4 => BinaryOpKind::Eq,
            _ => BinaryOpKind::NotEq,
        };
        bin(ea, op, l, r)
    }

    fn gen_program<'a>(
        seed: u64,
        ea: &'a Arena<Expr<'a>>,
        sa: &'a Arena<Stmt<'a>>,
        vars: &[Symbol],
        show_s: Symbol,
    ) -> Vec<Stmt<'a>> {
        let mut rng = SplitMix64::new(seed);
        let mut stmts: Vec<Stmt> = Vec::new();
        for &v in vars {
            stmts.push(Stmt::Let { var: v, ty: None, value: num(ea, rng.below(6) as i64), mutable: true });
        }
        let m = 4 + rng.below(8);
        for _ in 0..m {
            match rng.below(10) {
                0..=5 => {
                    let v = vars[rng.below(vars.len() as u64) as usize];
                    stmts.push(Stmt::Set { target: v, value: gen_arith(ea, &mut rng, vars) });
                }
                6..=7 => {
                    let cond = gen_cmp(ea, &mut rng, vars);
                    let v = vars[rng.below(vars.len() as u64) as usize];
                    let then_blk: &[Stmt] =
                        sa.alloc_slice(vec![Stmt::Set { target: v, value: gen_arith(ea, &mut rng, vars) }]);
                    if rng.below(2) == 0 {
                        let v2 = vars[rng.below(vars.len() as u64) as usize];
                        let else_blk: &[Stmt] =
                            sa.alloc_slice(vec![Stmt::Set { target: v2, value: gen_arith(ea, &mut rng, vars) }]);
                        stmts.push(Stmt::If { cond, then_block: then_blk, else_block: Some(else_blk) });
                    } else {
                        stmts.push(Stmt::If { cond, then_block: then_blk, else_block: None });
                    }
                }
                _ => {
                    // A terminating bounded loop: the loop variable is mutated ONLY
                    // by its +1 increment, so it strictly increases to the bound.
                    let li = rng.below(vars.len() as u64) as usize;
                    let loop_var = vars[li];
                    let other = vars[(li + 1 + rng.below(vars.len() as u64 - 1) as usize) % vars.len()];
                    let bound = 2 + rng.below(4);
                    let cond = bin(ea, BinaryOpKind::Lt, idref(ea, loop_var), num(ea, bound as i64));
                    let body: &[Stmt] = sa.alloc_slice(vec![
                        Stmt::Set { target: other, value: gen_arith(ea, &mut rng, vars) },
                        Stmt::Set {
                            target: loop_var,
                            value: bin(ea, BinaryOpKind::Add, idref(ea, loop_var), num(ea, 1)),
                        },
                    ]);
                    stmts.push(Stmt::While { cond, body, decreasing: None });
                }
            }
        }
        for &v in vars {
            stmts.push(show(ea, show_s, idref(ea, v)));
        }
        stmts
    }

    #[test]
    fn vm_differential_fuzz_300_programs() {
        for seed in 0..300u64 {
            let ea: Arena<Expr> = Arena::new();
            let sa: Arena<Stmt> = Arena::new();
            let mut it = Interner::new();
            let show_s = it.intern("show");
            let vars: Vec<Symbol> = (0..4u32).map(|k| it.intern(&format!("v{k}"))).collect();
            let stmts = gen_program(seed, &ea, &sa, &vars, show_s);

            let vm = compile_and_run(&stmts, &it);
            let tw = run_treewalk(&stmts, &it);
            match (vm, tw) {
                (Ok(a), Ok(b)) => assert_eq!(
                    normalize(&a),
                    normalize(&b),
                    "seed {} diverged:\nVM:\n{}\nTREE-WALKER:\n{}",
                    seed, a, b
                ),
                (Err(_), Err(_)) => {}
                (a, b) => panic!("seed {} one engine errored: vm={:?} tw={:?}", seed, a, b),
            }
        }
    }

    #[test]
    fn vm_differential_fuzz_functions() {
        for seed in 0..150u64 {
            let ea: Arena<Expr> = Arena::new();
            let sa: Arena<Stmt> = Arena::new();
            let ta: Arena<TypeExpr> = Arena::new();
            let mut it = Interner::new();
            let show_s = it.intern("show");
            let int_ty = ta.alloc(TypeExpr::Primitive(it.intern("Int")));
            let mut rng = SplitMix64::new(seed ^ 0xF00D);

            let f0 = it.intern("f0");
            let f1 = it.intern("f1");
            let pa = it.intern("pa");
            let pb = it.intern("pb");
            let params = [pa, pb];
            // Function bodies are arithmetic over the parameters (divisors are
            // non-zero literals, so no division-by-zero from arguments).
            let body0: &[Stmt] = sa.alloc_slice(vec![ret(gen_arith(&ea, &mut rng, &params))]);
            let body1: &[Stmt] = sa.alloc_slice(vec![ret(gen_arith(&ea, &mut rng, &params))]);
            let mut stmts = vec![
                fndef(f0, vec![(pa, int_ty), (pb, int_ty)], body0),
                fndef(f1, vec![(pa, int_ty), (pb, int_ty)], body1),
            ];
            let calls = 3 + rng.below(4);
            for _ in 0..calls {
                let f = if rng.below(2) == 0 { f0 } else { f1 };
                let a = num(&ea, rng.below(6) as i64);
                let b = num(&ea, 1 + rng.below(5) as i64);
                stmts.push(show(&ea, show_s, calle(&ea, f, vec![a, b])));
            }

            let vm = compile_and_run(&stmts, &it);
            let tw = run_treewalk(&stmts, &it);
            match (vm, tw) {
                (Ok(a), Ok(b)) => assert_eq!(normalize(&a), normalize(&b), "fn seed {} diverged", seed),
                (Err(_), Err(_)) => {}
                (a, b) => panic!("fn seed {} one engine errored: vm={:?} tw={:?}", seed, a, b),
            }
        }
    }

    fn gen_float_lit<'a>(ea: &'a Arena<Expr<'a>>, rng: &mut SplitMix64) -> &'a Expr<'a> {
        ea.alloc(Expr::Literal(Literal::Float(rng.below(10) as f64 * 0.5)))
    }
    fn gen_float_atom<'a>(ea: &'a Arena<Expr<'a>>, rng: &mut SplitMix64, vars: &[Symbol]) -> &'a Expr<'a> {
        if rng.below(2) == 0 {
            gen_float_lit(ea, rng)
        } else {
            idref(ea, vars[rng.below(vars.len() as u64) as usize])
        }
    }
    // Float arithmetic uses only +,-,* (no division → no float div-by-zero),
    // which keeps every program total; any NaN/Inf displays identically in both.
    fn gen_float_arith<'a>(ea: &'a Arena<Expr<'a>>, rng: &mut SplitMix64, vars: &[Symbol]) -> &'a Expr<'a> {
        let l = gen_float_atom(ea, rng, vars);
        let r = gen_float_atom(ea, rng, vars);
        let op = match rng.below(3) {
            0 => BinaryOpKind::Add,
            1 => BinaryOpKind::Subtract,
            _ => BinaryOpKind::Multiply,
        };
        bin(ea, op, l, r)
    }

    #[test]
    fn vm_differential_fuzz_floats() {
        for seed in 0..200u64 {
            let ea: Arena<Expr> = Arena::new();
            let sa: Arena<Stmt> = Arena::new();
            let mut it = Interner::new();
            let show_s = it.intern("show");
            let vars: Vec<Symbol> = (0..3u32).map(|k| it.intern(&format!("f{k}"))).collect();
            let mut rng = SplitMix64::new(seed ^ 0xBEEF);

            let mut stmts: Vec<Stmt> = Vec::new();
            for &v in &vars {
                stmts.push(Stmt::Let { var: v, ty: None, value: gen_float_lit(&ea, &mut rng), mutable: true });
            }
            let m = 4 + rng.below(6);
            for _ in 0..m {
                if rng.below(8) < 5 {
                    let v = vars[rng.below(vars.len() as u64) as usize];
                    stmts.push(Stmt::Set { target: v, value: gen_float_arith(&ea, &mut rng, &vars) });
                } else {
                    let l = idref(&ea, vars[rng.below(vars.len() as u64) as usize]);
                    let r = idref(&ea, vars[rng.below(vars.len() as u64) as usize]);
                    let op = match rng.below(4) {
                        0 => BinaryOpKind::Lt,
                        1 => BinaryOpKind::Gt,
                        2 => BinaryOpKind::LtEq,
                        _ => BinaryOpKind::GtEq,
                    };
                    let cond = bin(&ea, op, l, r);
                    let v = vars[rng.below(vars.len() as u64) as usize];
                    let then_blk: &[Stmt] =
                        sa.alloc_slice(vec![Stmt::Set { target: v, value: gen_float_arith(&ea, &mut rng, &vars) }]);
                    stmts.push(Stmt::If { cond, then_block: then_blk, else_block: None });
                }
            }
            for &v in &vars {
                stmts.push(show(&ea, show_s, idref(&ea, v)));
            }

            let vm = compile_and_run(&stmts, &it);
            let tw = run_treewalk(&stmts, &it);
            match (vm, tw) {
                (Ok(a), Ok(b)) => assert_eq!(normalize(&a), normalize(&b), "float seed {} diverged", seed),
                (Err(_), Err(_)) => {}
                (a, b) => panic!("float seed {} one engine errored: vm={:?} tw={:?}", seed, a, b),
            }
        }
    }

    #[test]
    fn vm_function_mutual_recursion() {
        // isEven(n): If n == 0: Return 1. Return isOdd(n - 1).
        // isOdd(n):  If n == 0: Return 0. Return isEven(n - 1).
        // Main: Show isEven(4).   → 1   (forward reference to isOdd)
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let ta: Arena<TypeExpr> = Arena::new();
        let mut it = Interner::new();
        let is_even = it.intern("isEven");
        let is_odd = it.intern("isOdd");
        let n = it.intern("n");
        let r = it.intern("r");
        let show_s = it.intern("show");
        let int_ty = ta.alloc(TypeExpr::Primitive(it.intern("Int")));

        let even_then: &[Stmt] = sa.alloc_slice(vec![ret(num(&ea, 1))]);
        let even_body: &[Stmt] = sa.alloc_slice(vec![
            Stmt::If { cond: bin(&ea, BinaryOpKind::Eq, idref(&ea, n), num(&ea, 0)), then_block: even_then, else_block: None },
            ret(calle(&ea, is_odd, vec![bin(&ea, BinaryOpKind::Subtract, idref(&ea, n), num(&ea, 1))])),
        ]);
        let odd_then: &[Stmt] = sa.alloc_slice(vec![ret(num(&ea, 0))]);
        let odd_body: &[Stmt] = sa.alloc_slice(vec![
            Stmt::If { cond: bin(&ea, BinaryOpKind::Eq, idref(&ea, n), num(&ea, 0)), then_block: odd_then, else_block: None },
            ret(calle(&ea, is_even, vec![bin(&ea, BinaryOpKind::Subtract, idref(&ea, n), num(&ea, 1))])),
        ]);
        let stmts = vec![
            fndef(is_even, vec![(n, int_ty)], even_body),
            fndef(is_odd, vec![(n, int_ty)], odd_body),
            letb(r, calle(&ea, is_even, vec![num(&ea, 4)])),
            show(&ea, show_s, idref(&ea, r)),
        ];
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "1");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_runs_arithmetic_let_show() {
        // Let x be 5. Let y be x + 7. Show y.
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let x = it.intern("x");
        let y = it.intern("y");
        let console = it.intern("show");

        let five = ea.alloc(Expr::Literal(Literal::Number(5)));
        let xref = ea.alloc(Expr::Identifier(x));
        let seven = ea.alloc(Expr::Literal(Literal::Number(7)));
        let sum = ea.alloc(Expr::BinaryOp { op: BinaryOpKind::Add, left: xref, right: seven });
        let yref = ea.alloc(Expr::Identifier(y));
        let console_ref = ea.alloc(Expr::Identifier(console));

        let stmts = vec![
            Stmt::Let { var: x, ty: None, value: five, mutable: false },
            Stmt::Let { var: y, ty: None, value: sum, mutable: false },
            Stmt::Show { object: yref, recipient: console_ref },
        ];

        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.trim(), "12");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_arithmetic_and_comparison_chain() {
        // Let a be 6. Let b be a * 4. Let c be b - 2. Show c.   → 22
        // Let d be c > 20. Show d.                              → true
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let a = it.intern("a");
        let b = it.intern("b");
        let c = it.intern("c");
        let d = it.intern("d");
        let console = it.intern("show");

        let six = ea.alloc(Expr::Literal(Literal::Number(6)));
        let aref = ea.alloc(Expr::Identifier(a));
        let four = ea.alloc(Expr::Literal(Literal::Number(4)));
        let mul = ea.alloc(Expr::BinaryOp { op: BinaryOpKind::Multiply, left: aref, right: four });
        let bref = ea.alloc(Expr::Identifier(b));
        let two = ea.alloc(Expr::Literal(Literal::Number(2)));
        let subx = ea.alloc(Expr::BinaryOp { op: BinaryOpKind::Subtract, left: bref, right: two });
        let cref1 = ea.alloc(Expr::Identifier(c));
        let cref2 = ea.alloc(Expr::Identifier(c));
        let twenty = ea.alloc(Expr::Literal(Literal::Number(20)));
        let gt = ea.alloc(Expr::BinaryOp { op: BinaryOpKind::Gt, left: cref2, right: twenty });
        let dref = ea.alloc(Expr::Identifier(d));
        let console_ref1 = ea.alloc(Expr::Identifier(console));
        let console_ref2 = ea.alloc(Expr::Identifier(console));

        let stmts = vec![
            Stmt::Let { var: a, ty: None, value: six, mutable: false },
            Stmt::Let { var: b, ty: None, value: mul, mutable: false },
            Stmt::Let { var: c, ty: None, value: subx, mutable: false },
            Stmt::Show { object: cref1, recipient: console_ref1 },
            Stmt::Let { var: d, ty: None, value: gt, mutable: false },
            Stmt::Show { object: dref, recipient: console_ref2 },
        ];

        let out = compile_and_run(&stmts, &it).unwrap();
        let lines: Vec<&str> = out.lines().collect();
        assert_eq!(lines[0], "22");
        assert_eq!(lines[1], "true");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_while_loop_sums_to_15() {
        // Let total be 0. Let i be 1.
        // While i <= 5: Set total to total + i. Set i to i + 1.
        // Show total.   → 15
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let total = it.intern("total");
        let i = it.intern("i");
        let console = it.intern("show");

        let zero = ea.alloc(Expr::Literal(Literal::Number(0)));
        let one_init = ea.alloc(Expr::Literal(Literal::Number(1)));

        // cond: i <= 5
        let i_c = ea.alloc(Expr::Identifier(i));
        let five = ea.alloc(Expr::Literal(Literal::Number(5)));
        let cond = ea.alloc(Expr::BinaryOp { op: BinaryOpKind::LtEq, left: i_c, right: five });

        // body: Set total to total + i.
        let total_l = ea.alloc(Expr::Identifier(total));
        let i_r = ea.alloc(Expr::Identifier(i));
        let tplus = ea.alloc(Expr::BinaryOp { op: BinaryOpKind::Add, left: total_l, right: i_r });
        // body: Set i to i + 1.
        let i_l = ea.alloc(Expr::Identifier(i));
        let one = ea.alloc(Expr::Literal(Literal::Number(1)));
        let iplus = ea.alloc(Expr::BinaryOp { op: BinaryOpKind::Add, left: i_l, right: one });
        let body: &[Stmt] = sa.alloc_slice(vec![
            Stmt::Set { target: total, value: tplus },
            Stmt::Set { target: i, value: iplus },
        ]);

        let total_show = ea.alloc(Expr::Identifier(total));
        let console_ref = ea.alloc(Expr::Identifier(console));

        let stmts = vec![
            Stmt::Let { var: total, ty: None, value: zero, mutable: true },
            Stmt::Let { var: i, ty: None, value: one_init, mutable: true },
            Stmt::While { cond, body, decreasing: None },
            Stmt::Show { object: total_show, recipient: console_ref },
        ];

        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.trim(), "15");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    // ---- Sprint 1: error-string parity with the tree-walker -------------------
    //
    // The Err string IS part of the spec. Each test runs the same program
    // through both engines and asserts the error text is identical.

    fn assert_err_parity(stmts: &[Stmt], interner: &Interner) {
        let vm_err = compile_and_run(stmts, interner)
            .expect_err("VM should error");
        let tw_err = run_treewalk(stmts, interner)
            .expect_err("tree-walker should error");
        assert_eq!(vm_err, tw_err, "error strings diverged");
    }

    fn boolean<'a>(ea: &'a Arena<Expr<'a>>, b: bool) -> &'a Expr<'a> {
        ea.alloc(Expr::Literal(Literal::Boolean(b)))
    }
    fn nothing_lit<'a>(ea: &'a Arena<Expr<'a>>) -> &'a Expr<'a> {
        ea.alloc(Expr::Literal(Literal::Nothing))
    }

    #[test]
    fn vm_add_type_error_matches_treewalk() {
        // true + nothing — "Cannot add Bool and Nothing" (capital C).
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let x = it.intern("x");
        let stmts = vec![letb(x, bin(&ea, BinaryOpKind::Add, boolean(&ea, true), nothing_lit(&ea)))];
        assert_err_parity(&stmts, &it);
    }

    #[test]
    fn vm_subtract_type_error_matches_treewalk() {
        // true - nothing — TW phrases this "Cannot subtract Nothing from Bool".
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let x = it.intern("x");
        let stmts = vec![letb(x, bin(&ea, BinaryOpKind::Subtract, boolean(&ea, true), nothing_lit(&ea)))];
        assert_err_parity(&stmts, &it);
    }

    #[test]
    fn vm_multiply_type_error_matches_treewalk() {
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let x = it.intern("x");
        let stmts = vec![letb(x, bin(&ea, BinaryOpKind::Multiply, boolean(&ea, true), nothing_lit(&ea)))];
        assert_err_parity(&stmts, &it);
    }

    #[test]
    fn vm_divide_and_modulo_zero_messages_match_treewalk() {
        // Division by zero / Modulo by zero — exact text both engines.
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let x = it.intern("x");
        let stmts = vec![letb(x, bin(&ea, BinaryOpKind::Divide, num(&ea, 1), num(&ea, 0)))];
        assert_err_parity(&stmts, &it);

        let y = it.intern("y");
        let stmts = vec![letb(y, bin(&ea, BinaryOpKind::Modulo, num(&ea, 1), num(&ea, 0)))];
        assert_err_parity(&stmts, &it);
    }

    #[test]
    fn vm_comparison_type_error_matches_treewalk() {
        // true < 1 — TW says "Cannot compare Bool and Int".
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let x = it.intern("x");
        let stmts = vec![letb(x, bin(&ea, BinaryOpKind::Lt, boolean(&ea, true), num(&ea, 1)))];
        assert_err_parity(&stmts, &it);
    }

    #[test]
    fn vm_index_and_length_type_errors_match_treewalk() {
        // Indexing an Int / length of a Bool — kernel message parity.
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let x = it.intern("x");
        let stmts = vec![letb(x, index_at(&ea, num(&ea, 1), num(&ea, 1)))];
        assert_err_parity(&stmts, &it);

        let y = it.intern("y");
        let stmts = vec![letb(y, length_of(&ea, boolean(&ea, true)))];
        assert_err_parity(&stmts, &it);
    }

    // ---- Sprint 11: the generative differential fuzzer v2 ----------------------

    /// Run one generated program through both engines, asserting outcome
    /// equality (output AND error). Returns the divergence message if any.
    fn fuzz_one(seed: u64, features: super::fuzz::FeatureSet) -> Option<String> {
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let ta: Arena<TypeExpr> = Arena::new();
        let mut it = Interner::new();
        let generated = super::fuzz::generate(seed, features, &ea, &sa, &ta, &mut it);

        let (vm_out, vm_err) = run_to_outcome(&generated.stmts, &it, None, None);
        let (tw_out, tw_err) = run_treewalk_outcome(&generated.stmts, &it);
        if normalize(&vm_out) != normalize(&tw_out) || vm_err != tw_err {
            return Some(format!(
                "SEED={} FEATURES={:#x}\nvm out:\n{}\nvm err: {:?}\ntw out:\n{}\ntw err: {:?}",
                seed, features.0, vm_out, vm_err, tw_out, tw_err
            ));
        }
        None
    }

    #[test]
    fn fuzz_generator_is_deterministic() {
        // Same seed → identical program → identical outcome both times.
        for seed in [0u64, 42, 1234] {
            let f = super::fuzz::FeatureSet::all_supported();
            let run = |seed| {
                let ea: Arena<Expr> = Arena::new();
                let sa: Arena<Stmt> = Arena::new();
                let ta: Arena<TypeExpr> = Arena::new();
                let mut it = Interner::new();
                let g = super::fuzz::generate(seed, f, &ea, &sa, &ta, &mut it);
                run_to_outcome(&g.stmts, &it, None, None)
            };
            assert_eq!(run(seed), run(seed), "seed {seed} not deterministic");
        }
    }

    #[test]
    fn vm_fuzz_full_feature_differential() {
        // The standing CI gate: 1500 seeds over the full supported surface.
        let features = super::fuzz::FeatureSet::all_supported();
        for seed in 0..1500u64 {
            if let Some(divergence) = fuzz_one(seed, features) {
                panic!("fuzz divergence:\n{divergence}");
            }
        }
    }

    #[test]
    fn vm_fuzz_error_injection_differential() {
        // 500 seeds with one trap planted per program: the engines must agree
        // on partial output AND the exact error string.
        let features = super::fuzz::FeatureSet(
            super::fuzz::FeatureSet::all_supported().0 | super::fuzz::FeatureSet::ERROR_INJECTION,
        );
        for seed in 0..500u64 {
            if let Some(divergence) = fuzz_one(seed, features) {
                panic!("fuzz (error-injection) divergence:\n{divergence}");
            }
        }
    }

    /// Overnight soak: `cargo test vm_fuzz_overnight -- --ignored`.
    #[test]
    #[ignore]
    fn vm_fuzz_overnight() {
        let features = super::fuzz::FeatureSet::all_supported();
        let with_errors =
            super::fuzz::FeatureSet(features.0 | super::fuzz::FeatureSet::ERROR_INJECTION);
        for seed in 0..500_000u64 {
            if let Some(d) = fuzz_one(seed, features) {
                panic!("fuzz divergence:\n{d}");
            }
            if let Some(d) = fuzz_one(seed, with_errors) {
                panic!("fuzz divergence:\n{d}");
            }
        }
    }

    // ---- Sprint 9: strings, slices, options, temporal --------------------------

    #[test]
    fn vm_interpolated_string_spec_matrix() {
        use crate::ast::stmt::StringPart;
        // "{n} {f$} {f.1} {n>5} {n<5} {n^5} {n=}" over n=42, f=2.5.
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let n = it.intern("n");
        let fv = it.intern("fv");
        let r = it.intern("r");
        let show_s = it.intern("show");
        let sep = it.intern(" | ");
        let spec_cur = it.intern("$");
        let spec_p1 = it.intern(".1");
        let spec_r5 = it.intern(">5");
        let spec_l5 = it.intern("<5");
        let spec_c5 = it.intern("^5");

        let parts = vec![
            StringPart::Expr { value: idref(&ea, n), format_spec: None, debug: false },
            StringPart::Literal(sep),
            StringPart::Expr { value: idref(&ea, fv), format_spec: Some(spec_cur), debug: false },
            StringPart::Literal(sep),
            StringPart::Expr { value: idref(&ea, fv), format_spec: Some(spec_p1), debug: false },
            StringPart::Literal(sep),
            StringPart::Expr { value: idref(&ea, n), format_spec: Some(spec_r5), debug: false },
            StringPart::Literal(sep),
            StringPart::Expr { value: idref(&ea, n), format_spec: Some(spec_l5), debug: false },
            StringPart::Literal(sep),
            StringPart::Expr { value: idref(&ea, n), format_spec: Some(spec_c5), debug: false },
            StringPart::Literal(sep),
            StringPart::Expr { value: idref(&ea, n), format_spec: None, debug: true },
        ];
        let interp = ea.alloc(Expr::InterpolatedString(parts));
        let f25 = ea.alloc(Expr::Literal(Literal::Float(2.5)));
        let stmts = vec![
            letb(n, num(&ea, 42)),
            letb(fv, f25),
            letb(r, interp),
            show(&ea, show_s, idref(&ea, r)),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.trim_end(), "42 | $2.50 | 2.5 |    42 | 42    |  42   | n=42");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_slice_copy_tuple_union_intersection() {
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let xs = it.intern("xs");
        let part = it.intern("part");
        let cp = it.intern("cp");
        let tup = it.intern("tup");
        let s1 = it.intern("s1");
        let s2 = it.intern("s2");
        let u = it.intern("u");
        let i_ = it.intern("i_");
        let set_ty = it.intern("Set");
        let show_s = it.intern("show");

        let slice_e = ea.alloc(Expr::Slice {
            collection: idref(&ea, xs),
            start: num(&ea, 2),
            end: num(&ea, 4),
        });
        let copy_e = ea.alloc(Expr::Copy { expr: idref(&ea, xs) });
        let tuple_e = ea.alloc(Expr::Tuple(vec![num(&ea, 7), num(&ea, 8)]));
        let union_e = ea.alloc(Expr::Union { left: idref(&ea, s1), right: idref(&ea, s2) });
        let inter_e = ea.alloc(Expr::Intersection { left: idref(&ea, s1), right: idref(&ea, s2) });

        let stmts = vec![
            Stmt::Let { var: xs, ty: None, value: list_lit(&ea, (1..=5).map(|k| num(&ea, k)).collect()), mutable: true },
            letb(part, slice_e),
            show(&ea, show_s, length_of(&ea, idref(&ea, part))),
            show(&ea, show_s, index_at(&ea, idref(&ea, part), num(&ea, 1))),
            letb(cp, copy_e),
            push_to(num(&ea, 9), idref(&ea, xs)),
            show(&ea, show_s, length_of(&ea, idref(&ea, cp))),
            letb(tup, tuple_e),
            show(&ea, show_s, index_at(&ea, idref(&ea, tup), num(&ea, 2))),
            show(&ea, show_s, length_of(&ea, idref(&ea, tup))),
            Stmt::Let { var: s1, ty: None, value: new_coll(&ea, set_ty), mutable: true },
            Stmt::Let { var: s2, ty: None, value: new_coll(&ea, set_ty), mutable: true },
            Stmt::Add { value: num(&ea, 1), collection: idref(&ea, s1) },
            Stmt::Add { value: num(&ea, 2), collection: idref(&ea, s1) },
            Stmt::Add { value: num(&ea, 2), collection: idref(&ea, s2) },
            Stmt::Add { value: num(&ea, 3), collection: idref(&ea, s2) },
            letb(u, union_e),
            show(&ea, show_s, length_of(&ea, idref(&ea, u))),
            letb(i_, inter_e),
            show(&ea, show_s, length_of(&ea, idref(&ea, i_))),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["3", "2", "5", "8", "2", "3", "1"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_option_some_none_match_treewalk() {
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let a = it.intern("a");
        let b = it.intern("b");
        let show_s = it.intern("show");
        let some_e = ea.alloc(Expr::OptionSome { value: num(&ea, 5) });
        let none_e = ea.alloc(Expr::OptionNone);
        let stmts = vec![
            letb(a, some_e),
            show(&ea, show_s, idref(&ea, a)),
            letb(b, none_e),
            show(&ea, show_s, idref(&ea, b)),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["5", "nothing"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_today_now_with_fixed_clock_match_treewalk() {
        // Pin the clock so both engines see the same instant; also pin the
        // shadowing quirk — the NAME `today` resolves to the builtin even
        // after `Let today be 5`.
        crate::semantics::temporal::set_fixed_clock(19753, 1_700_000_000_000_000_000);
        let result = std::panic::catch_unwind(|| {
            let ea: Arena<Expr> = Arena::new();
            let mut it = Interner::new();
            let today_sym = it.intern("today");
            let a = it.intern("a");
            let b = it.intern("b");
            let show_s = it.intern("show");
            let stmts = vec![
                letb(a, idref(&ea, today_sym)),
                show(&ea, show_s, idref(&ea, a)),
                letb(b, idref(&ea, it.intern("now"))),
                show(&ea, show_s, idref(&ea, b)),
                letb(today_sym, num(&ea, 5)),
                show(&ea, show_s, idref(&ea, today_sym)),
            ];
            assert_vm_eq_treewalk(&stmts, &it);
        });
        crate::semantics::temporal::clear_fixed_clock();
        result.unwrap();
    }

    #[test]
    fn vm_escape_expr_errors_like_treewalk() {
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let x = it.intern("x");
        let lang = it.intern("Rust");
        let code = it.intern("raw");
        let esc = ea.alloc(Expr::Escape { language: lang, code });
        let stmts = vec![letb(x, esc)];
        assert_err_parity(&stmts, &it);
    }

    // ---- Sprint 8: closures, first-class calls, globals ------------------------

    use crate::ast::stmt::ClosureBody;

    fn closure_expr<'a>(
        ea: &'a Arena<Expr<'a>>,
        params: Vec<(Symbol, &'a TypeExpr<'a>)>,
        body: ClosureBody<'a>,
    ) -> &'a Expr<'a> {
        ea.alloc(Expr::Closure { params, body, return_type: None })
    }
    fn call_expr<'a>(
        ea: &'a Arena<Expr<'a>>,
        callee: &'a Expr<'a>,
        args: Vec<&'a Expr<'a>>,
    ) -> &'a Expr<'a> {
        ea.alloc(Expr::CallExpr { callee, args })
    }

    #[test]
    fn vm_closure_capture_is_snapshot() {
        // Mirrors the pinned e2e test: `Let x be 10. Let getX be () -> x.
        // Set x to 999. Show getX().` → 10.
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let x = it.intern("x");
        let get_x = it.intern("getX");
        let show_s = it.intern("show");
        let stmts = vec![
            Stmt::Let { var: x, ty: None, value: num(&ea, 10), mutable: true },
            letb(get_x, closure_expr(&ea, vec![], ClosureBody::Expression(idref(&ea, x)))),
            Stmt::Set { target: x, value: num(&ea, 999) },
            show(&ea, show_s, call_expr(&ea, idref(&ea, get_x), vec![])),
        ];
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "10");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_closure_capture_recloned_each_call() {
        // The closure body mutates its captured list — each call gets a fresh
        // deep clone (the tree-walker re-clones per invocation).
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let xs = it.intern("xs");
        let f = it.intern("f");
        let show_s = it.intern("show");
        let body: &[Stmt] = sa.alloc_slice(vec![
            push_to(num(&ea, 9), idref(&ea, xs)),
            show(&ea, show_s, length_of(&ea, idref(&ea, xs))),
            ret(num(&ea, 0)),
        ]);
        let stmts = vec![
            Stmt::Let { var: xs, ty: None, value: list_lit(&ea, vec![num(&ea, 1)]), mutable: true },
            letb(f, closure_expr(&ea, vec![], ClosureBody::Block(body))),
            Stmt::Call { function: f, args: vec![] },
            Stmt::Call { function: f, args: vec![] },
            show(&ea, show_s, length_of(&ea, idref(&ea, xs))),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        // Each call: clone has 1 element, push → 2. The original stays at 1.
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["2", "2", "1"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_closure_sees_live_global_when_created_before_definition() {
        // The closure references `g` before `Let g` runs: nothing to capture,
        // so the body falls through to the LIVE global.
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let g = it.intern("g");
        let f = it.intern("f");
        let show_s = it.intern("show");
        let stmts = vec![
            letb(f, closure_expr(&ea, vec![], ClosureBody::Expression(idref(&ea, g)))),
            Stmt::Let { var: g, ty: None, value: num(&ea, 5), mutable: true },
            show(&ea, show_s, call_expr(&ea, idref(&ea, f), vec![])),
            Stmt::Set { target: g, value: num(&ea, 7) },
            show(&ea, show_s, call_expr(&ea, idref(&ea, f), vec![])),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["5", "7"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_function_reads_and_writes_global_live() {
        // Functions see Main top-level bindings LIVE (lexical globals).
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let g = it.intern("g");
        let reader = it.intern("reader");
        let writer = it.intern("writer");
        let show_s = it.intern("show");
        let reader_body: &[Stmt] = sa.alloc_slice(vec![ret(idref(&ea, g))]);
        let writer_body: &[Stmt] = sa.alloc_slice(vec![
            Stmt::Set { target: g, value: num(&ea, 42) },
            ret(num(&ea, 0)),
        ]);
        let stmts = vec![
            fndef(reader, vec![], reader_body),
            fndef(writer, vec![], writer_body),
            Stmt::Let { var: g, ty: None, value: num(&ea, 1), mutable: true },
            show(&ea, show_s, calle(&ea, reader, vec![])),
            Stmt::Set { target: g, value: num(&ea, 2) },
            show(&ea, show_s, calle(&ea, reader, vec![])),
            Stmt::Call { function: writer, args: vec![] },
            show(&ea, show_s, idref(&ea, g)),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["1", "2", "42"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_main_block_var_invisible_to_function() {
        // Lexical scoping: a Let inside a Main If-block is NOT a global; the
        // function fails with "Undefined variable" — in both engines, with the
        // partial output intact.
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let t = it.intern("t");
        let f = it.intern("f");
        let show_s = it.intern("show");
        let f_body: &[Stmt] = sa.alloc_slice(vec![ret(idref(&ea, t))]);
        let then_blk: &[Stmt] = sa.alloc_slice(vec![
            letb(t, num(&ea, 5)),
            show(&ea, show_s, calle(&ea, f, vec![])),
        ]);
        let stmts = vec![
            fndef(f, vec![], f_body),
            show(&ea, show_s, num(&ea, 1)),
            Stmt::If { cond: boolean(&ea, true), then_block: then_blk, else_block: None },
        ];
        assert_outcome_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_closure_param_shadows_capture() {
        let ea: Arena<Expr> = Arena::new();
        let ta: Arena<TypeExpr> = Arena::new();
        let mut it = Interner::new();
        let x = it.intern("x");
        let f = it.intern("f");
        let show_s = it.intern("show");
        let int_ty = ta.alloc(TypeExpr::Primitive(it.intern("Int")));
        let stmts = vec![
            Stmt::Let { var: x, ty: None, value: num(&ea, 10), mutable: true },
            letb(
                f,
                closure_expr(&ea, vec![(x, int_ty)], ClosureBody::Expression(idref(&ea, x))),
            ),
            show(&ea, show_s, call_expr(&ea, idref(&ea, f), vec![num(&ea, 77)])),
        ];
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "77");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_callexpr_on_non_function_and_arity_errors_match() {
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let x = it.intern("x");
        let f = it.intern("f");

        // Cannot call value of type Int.
        let stmts = vec![letb(x, call_expr(&ea, num(&ea, 5), vec![]))];
        assert_err_parity(&stmts, &it);

        // Closure arity mismatch.
        let stmts = vec![
            letb(f, closure_expr(&ea, vec![], ClosureBody::Expression(num(&ea, 1)))),
            letb(x, call_expr(&ea, idref(&ea, f), vec![num(&ea, 9)])),
        ];
        assert_err_parity(&stmts, &it);
    }

    #[test]
    fn vm_closure_passed_to_function_and_called_via_param() {
        // Higher-order: apply(f, x) = f(x).
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let ta: Arena<TypeExpr> = Arena::new();
        let mut it = Interner::new();
        let apply = it.intern("apply");
        let fp = it.intern("fp");
        let xp = it.intern("xp");
        let n = it.intern("n");
        let show_s = it.intern("show");
        let int_ty = ta.alloc(TypeExpr::Primitive(it.intern("Int")));

        let apply_body: &[Stmt] =
            sa.alloc_slice(vec![ret(call_expr(&ea, idref(&ea, fp), vec![idref(&ea, xp)]))]);
        let doubler = closure_expr(
            &ea,
            vec![(n, int_ty)],
            ClosureBody::Expression(bin(&ea, BinaryOpKind::Multiply, idref(&ea, n), num(&ea, 2))),
        );
        let stmts = vec![
            fndef(apply, vec![(fp, int_ty), (xp, int_ty)], apply_body),
            show(&ea, show_s, calle(&ea, apply, vec![doubler, num(&ea, 21)])),
        ];
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "42");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    // ---- Sprint 7: structs, enums, Inspect, CRDT -------------------------------

    fn struct_def<'a>(name: Symbol, fields: Vec<(Symbol, Symbol, bool)>) -> Stmt<'a> {
        Stmt::StructDef { name, fields, is_portable: false }
    }
    fn new_struct<'a>(
        ea: &'a Arena<Expr<'a>>,
        type_name: Symbol,
        init_fields: Vec<(Symbol, &'a Expr<'a>)>,
    ) -> &'a Expr<'a> {
        ea.alloc(Expr::New { type_name, type_args: vec![], init_fields })
    }
    fn field_access<'a>(ea: &'a Arena<Expr<'a>>, object: &'a Expr<'a>, field: Symbol) -> &'a Expr<'a> {
        ea.alloc(Expr::FieldAccess { object, field })
    }

    #[test]
    fn vm_struct_new_with_defaults_and_field_access() {
        // Point { x: Int, y: Int }; new Point with x 3 — y defaults to 0.
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let point = it.intern("Point");
        let x = it.intern("x");
        let y = it.intern("y");
        let int_s = it.intern("Int");
        let p = it.intern("p");
        let show_s = it.intern("show");
        let stmts = vec![
            struct_def(point, vec![(x, int_s, true), (y, int_s, true)]),
            letb(p, new_struct(&ea, point, vec![(x, num(&ea, 3))])),
            show(&ea, show_s, field_access(&ea, idref(&ea, p), x)),
            show(&ea, show_s, field_access(&ea, idref(&ea, p), y)),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["3", "0"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_struct_value_semantics_no_aliasing() {
        // `Let b be a` copies the struct: SetField through b is invisible via a.
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let point = it.intern("Point");
        let x = it.intern("x");
        let int_s = it.intern("Int");
        let a = it.intern("a");
        let b = it.intern("b");
        let show_s = it.intern("show");
        let stmts = vec![
            struct_def(point, vec![(x, int_s, true)]),
            Stmt::Let { var: a, ty: None, value: new_struct(&ea, point, vec![(x, num(&ea, 1))]), mutable: true },
            Stmt::Let { var: b, ty: None, value: idref(&ea, a), mutable: true },
            Stmt::SetField { object: idref(&ea, b), field: x, value: num(&ea, 99) },
            show(&ea, show_s, field_access(&ea, idref(&ea, a), x)),
            show(&ea, show_s, field_access(&ea, idref(&ea, b), x)),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["1", "99"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_field_errors_match_treewalk() {
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let point = it.intern("Point");
        let x = it.intern("x");
        let ghost = it.intern("ghost");
        let int_s = it.intern("Int");
        let p = it.intern("p");
        let v = it.intern("v");

        // Missing field read.
        let stmts = vec![
            struct_def(point, vec![(x, int_s, true)]),
            letb(p, new_struct(&ea, point, vec![])),
            letb(v, field_access(&ea, idref(&ea, p), ghost)),
        ];
        assert_err_parity(&stmts, &it);

        // Field access on a non-struct.
        let stmts = vec![letb(v, field_access(&ea, num(&ea, 5), x))];
        assert_err_parity(&stmts, &it);

        // SetField on a non-struct.
        let stmts = vec![
            letb(p, num(&ea, 5)),
            Stmt::SetField { object: idref(&ea, p), field: x, value: num(&ea, 1) },
        ];
        assert_err_parity(&stmts, &it);
    }

    #[test]
    fn vm_inspect_struct_arms_and_otherwise() {
        use crate::ast::stmt::MatchArm;
        // Inspect p (a Point): the Point arm binds x; a Circle arm is skipped;
        // then inspect an Int hits Otherwise.
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let point = it.intern("Point");
        let circle = it.intern("Circle");
        let x = it.intern("x");
        let bx = it.intern("bx");
        let int_s = it.intern("Int");
        let p = it.intern("p");
        let show_s = it.intern("show");

        let point_body: &[Stmt] = sa.alloc_slice(vec![show(&ea, show_s, idref(&ea, bx))]);
        let circle_body: &[Stmt] = sa.alloc_slice(vec![show(&ea, show_s, num(&ea, 111))]);
        let otherwise_body: &[Stmt] = sa.alloc_slice(vec![show(&ea, show_s, num(&ea, 222))]);

        let stmts = vec![
            struct_def(point, vec![(x, int_s, true)]),
            letb(p, new_struct(&ea, point, vec![(x, num(&ea, 7))])),
            Stmt::Inspect {
                target: idref(&ea, p),
                arms: vec![
                    MatchArm { enum_name: None, variant: Some(circle), bindings: vec![], body: circle_body },
                    MatchArm { enum_name: None, variant: Some(point), bindings: vec![(x, bx)], body: point_body },
                    MatchArm { enum_name: None, variant: None, bindings: vec![], body: otherwise_body },
                ],
                has_otherwise: true,
            },
            Stmt::Inspect {
                target: num(&ea, 5),
                arms: vec![
                    MatchArm { enum_name: None, variant: Some(point), bindings: vec![], body: circle_body },
                    MatchArm { enum_name: None, variant: None, bindings: vec![], body: otherwise_body },
                ],
                has_otherwise: true,
            },
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["7", "222"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_inspect_inductive_positional_bindings() {
        use crate::ast::stmt::MatchArm;
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let shape = it.intern("Shape");
        let circle = it.intern("Circle");
        let r_field = it.intern("radius");
        let r_bind = it.intern("r");
        let c = it.intern("c");
        let show_s = it.intern("show");

        let circle_expr = ea.alloc(Expr::NewVariant {
            enum_name: shape,
            variant: circle,
            fields: vec![(r_field, num(&ea, 10))],
        });
        let circle_body: &[Stmt] = sa.alloc_slice(vec![show(&ea, show_s, idref(&ea, r_bind))]);

        let stmts = vec![
            letb(c, circle_expr),
            // Matching arm binds positionally.
            Stmt::Inspect {
                target: idref(&ea, c),
                arms: vec![MatchArm {
                    enum_name: None,
                    variant: Some(circle),
                    bindings: vec![(r_field, r_bind)],
                    body: circle_body,
                }],
                has_otherwise: false,
            },
            show(&ea, show_s, num(&ea, 1)),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["10", "1"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_inspect_unhandled_variant_without_otherwise_is_loud() {
        use crate::ast::stmt::MatchArm;
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let shape = it.intern("Shape");
        let circle = it.intern("Circle");
        let square = it.intern("Square");
        let r_field = it.intern("radius");
        let c = it.intern("c");
        let show_s = it.intern("show");

        let circle_expr = ea.alloc(Expr::NewVariant {
            enum_name: shape,
            variant: circle,
            fields: vec![(r_field, num(&ea, 10))],
        });
        let square_body: &[Stmt] = sa.alloc_slice(vec![show(&ea, show_s, num(&ea, 333))]);

        // A `Circle` scrutinee against a `Square`-only Inspect with no Otherwise
        // is a non-exhaustive match — LOUD, never a silent no-op. tw==VM.
        let stmts = vec![
            letb(c, circle_expr),
            Stmt::Inspect {
                target: idref(&ea, c),
                arms: vec![MatchArm { enum_name: None, variant: Some(square), bindings: vec![], body: square_body }],
                has_otherwise: false,
            },
        ];
        assert_err_parity(&stmts, &it);
    }

    #[test]
    fn vm_inductive_equality_is_structural() {
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let shape = it.intern("Shape");
        let circle = it.intern("Circle");
        let r_field = it.intern("radius");
        let a = it.intern("a");
        let b = it.intern("b");
        let show_s = it.intern("show");
        let c1 = ea.alloc(Expr::NewVariant {
            enum_name: shape,
            variant: circle,
            fields: vec![(r_field, num(&ea, 5))],
        });
        let c2 = ea.alloc(Expr::NewVariant {
            enum_name: shape,
            variant: circle,
            fields: vec![(r_field, num(&ea, 5))],
        });
        let stmts = vec![
            letb(a, c1),
            letb(b, c2),
            show(&ea, show_s, bin(&ea, BinaryOpKind::Eq, idref(&ea, a), idref(&ea, b))),
        ];
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "true");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_crdt_increase_decrease_merge_match_treewalk() {
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let counter = it.intern("Counter");
        let n_field = it.intern("n");
        let int_s = it.intern("Int");
        let a = it.intern("a");
        let b = it.intern("b");
        let show_s = it.intern("show");
        let stmts = vec![
            struct_def(counter, vec![(n_field, int_s, true)]),
            Stmt::Let { var: a, ty: None, value: new_struct(&ea, counter, vec![(n_field, num(&ea, 1))]), mutable: true },
            Stmt::Let { var: b, ty: None, value: new_struct(&ea, counter, vec![(n_field, num(&ea, 10))]), mutable: true },
            Stmt::IncreaseCrdt { object: idref(&ea, a), field: n_field, amount: num(&ea, 5) },
            Stmt::DecreaseCrdt { object: idref(&ea, a), field: n_field, amount: num(&ea, 2) },
            Stmt::MergeCrdt { source: idref(&ea, b), target: idref(&ea, a) },
            show(&ea, show_s, field_access(&ea, idref(&ea, a), n_field)),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.trim(), "14");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    // ---- Sprint 6: builtins, call edges, MAX_CALL_DEPTH ------------------------

    #[test]
    fn vm_builtins_match_treewalk() {
        // One Show per builtin, covering Int/Float coercions.
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let show_s = it.intern("show");
        let f25 = ea.alloc(Expr::Literal(Literal::Float(2.5)));
        let f29 = ea.alloc(Expr::Literal(Literal::Float(2.9)));
        let neg = bin(&ea, BinaryOpKind::Subtract, num(&ea, 0), num(&ea, 7));
        let calls: Vec<&Expr> = vec![
            calle(&ea, it.intern("abs"), vec![neg]),
            calle(&ea, it.intern("sqrt"), vec![num(&ea, 9)]),
            calle(&ea, it.intern("min"), vec![num(&ea, 3), f25]),
            calle(&ea, it.intern("max"), vec![num(&ea, 3), f25]),
            calle(&ea, it.intern("floor"), vec![f29]),
            calle(&ea, it.intern("ceil"), vec![f29]),
            calle(&ea, it.intern("round"), vec![f29]),
            calle(&ea, it.intern("pow"), vec![num(&ea, 2), num(&ea, 10)]),
            calle(&ea, it.intern("chr"), vec![num(&ea, 65)]),
            calle(&ea, it.intern("length"), vec![text(&ea, it.intern("hello"))]),
            calle(&ea, it.intern("format"), vec![num(&ea, 42)]),
        ];
        let mut stmts = Vec::new();
        for (k, c) in calls.into_iter().enumerate() {
            let v = it.intern(&format!("b{k}"));
            stmts.push(letb(v, c));
            stmts.push(show(&ea, show_s, idref(&ea, v)));
        }
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_parse_int_and_float_match_treewalk() {
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let show_s = it.intern("show");
        let a = it.intern("a");
        let b = it.intern("b");
        let s42 = it.intern(" 42 ");
        let s25 = it.intern("2.5");
        let stmts = vec![
            letb(a, calle(&ea, it.intern("parseInt"), vec![text(&ea, s42)])),
            show(&ea, show_s, idref(&ea, a)),
            letb(b, calle(&ea, it.intern("parseFloat"), vec![text(&ea, s25)])),
            show(&ea, show_s, idref(&ea, b)),
        ];
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_builtin_error_messages_match_treewalk() {
        // parseInt("zz"), chr(-1), sqrt("x") arity/type errors — exact strings.
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let x = it.intern("x");
        let zz = it.intern("zz");

        let stmts = vec![letb(x, calle(&ea, it.intern("parseInt"), vec![text(&ea, zz)]))];
        assert_err_parity(&stmts, &it);

        let neg1 = bin(&ea, BinaryOpKind::Subtract, num(&ea, 0), num(&ea, 1));
        let stmts = vec![letb(x, calle(&ea, it.intern("chr"), vec![neg1]))];
        assert_err_parity(&stmts, &it);

        // Wrong arity: the arity error fires BEFORE evaluating arguments.
        let stmts = vec![letb(x, calle(&ea, it.intern("abs"), vec![num(&ea, 1), num(&ea, 2)]))];
        assert_err_parity(&stmts, &it);
    }

    #[test]
    fn vm_copy_builtin_is_deep() {
        // copy(xs) then mutate xs — the copy must be unaffected (both engines).
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let xs = it.intern("xs");
        let ys = it.intern("ys");
        let show_s = it.intern("show");
        let stmts = vec![
            Stmt::Let { var: xs, ty: None, value: list_lit(&ea, vec![num(&ea, 1)]), mutable: true },
            letb(ys, calle(&ea, it.intern("copy"), vec![idref(&ea, xs)])),
            push_to(num(&ea, 2), idref(&ea, xs)),
            show(&ea, show_s, length_of(&ea, idref(&ea, xs))),
            show(&ea, show_s, length_of(&ea, idref(&ea, ys))),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["2", "1"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_let_binding_isolates_value_semantics() {
        // Value semantics: `Let a be xs` is an independent value — pushing through
        // `a` grows only `a`, leaving `xs` at length 1. VM and tree-walker agree.
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let xs = it.intern("xs");
        let a = it.intern("a");
        let show_s = it.intern("show");
        let stmts = vec![
            Stmt::Let { var: xs, ty: None, value: list_lit(&ea, vec![num(&ea, 1)]), mutable: true },
            Stmt::Let { var: a, ty: None, value: idref(&ea, xs), mutable: true },
            push_to(num(&ea, 2), idref(&ea, a)),
            show(&ea, show_s, length_of(&ea, idref(&ea, xs))),
        ];
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "1");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_unknown_function_error_matches_treewalk() {
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let x = it.intern("x");
        let stmts = vec![letb(x, calle(&ea, it.intern("frobnicate"), vec![num(&ea, 1)]))];
        assert_err_parity(&stmts, &it);
    }

    #[test]
    fn vm_user_fn_arity_mismatch_matches_treewalk() {
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let ta: Arena<TypeExpr> = Arena::new();
        let mut it = Interner::new();
        let f = it.intern("f");
        let n = it.intern("n");
        let x = it.intern("x");
        let int_ty = ta.alloc(TypeExpr::Primitive(it.intern("Int")));
        let body: &[Stmt] = sa.alloc_slice(vec![ret(idref(&ea, n))]);
        let stmts = vec![
            fndef(f, vec![(n, int_ty)], body),
            letb(x, calle(&ea, f, vec![num(&ea, 1), num(&ea, 2)])),
        ];
        assert_err_parity(&stmts, &it);
    }

    #[test]
    fn vm_infinite_recursion_hits_call_depth_limit_like_treewalk() {
        // Both engines must report the canonical stack-overflow error instead
        // of crashing the host. The tree-walker's SYNC path burns many native
        // frames per LOGOS call, so this runs on a big-stack thread (debug
        // frames are huge; release frames fit a normal stack at depth 1000).
        //
        // The recursion is deliberately NON-tail (`spin(n+1) - 1`): the call's
        // result feeds a subtraction, so it is not in tail position and is NOT
        // tail-call-optimized on any tier. (A direct `return spin(n+1)` is a
        // self-tail-call and would run in constant stack forever — TCO is a
        // language semantic here, covered by the `tco` differential tests.)
        // Subtraction also dodges the AOT accumulator transform, which only
        // strength-reduces `+`/`*`, so this stays unbounded recursion everywhere.
        std::thread::Builder::new()
            .stack_size(256 * 1024 * 1024)
            .spawn(|| {
                let ea: Arena<Expr> = Arena::new();
                let sa: Arena<Stmt> = Arena::new();
                let ta: Arena<TypeExpr> = Arena::new();
                let mut it = Interner::new();
                let spin = it.intern("spin");
                let n = it.intern("n");
                let x = it.intern("x");
                let int_ty = ta.alloc(TypeExpr::Primitive(it.intern("Int")));
                let body: &[Stmt] = sa.alloc_slice(vec![ret(bin(
                    &ea,
                    BinaryOpKind::Subtract,
                    calle(
                        &ea,
                        spin,
                        vec![bin(&ea, BinaryOpKind::Add, idref(&ea, n), num(&ea, 1))],
                    ),
                    num(&ea, 1),
                ))]);
                let stmts = vec![
                    fndef(spin, vec![(n, int_ty)], body),
                    letb(x, calle(&ea, spin, vec![num(&ea, 0)])),
                ];
                let vm_err = compile_and_run(&stmts, &it).unwrap_err();
                assert_eq!(vm_err, "Stack overflow: maximum call depth exceeded");
                let (_, tw_err) = run_treewalk_outcome(&stmts, &it);
                assert_eq!(
                    tw_err.as_deref(),
                    Some("Stack overflow: maximum call depth exceeded")
                );
            })
            .unwrap()
            .join()
            .unwrap();
    }

    #[test]
    fn vm_depth_900_recursion_still_fine() {
        // The limit is 1000; 900 must work in both engines.
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let ta: Arena<TypeExpr> = Arena::new();
        let mut it = Interner::new();
        let down = it.intern("down");
        let n = it.intern("n");
        let x = it.intern("x");
        let show_s = it.intern("show");
        let int_ty = ta.alloc(TypeExpr::Primitive(it.intern("Int")));
        let base: &[Stmt] = sa.alloc_slice(vec![ret(num(&ea, 0))]);
        let body: &[Stmt] = sa.alloc_slice(vec![
            Stmt::If {
                cond: bin(&ea, BinaryOpKind::LtEq, idref(&ea, n), num(&ea, 0)),
                then_block: base,
                else_block: None,
            },
            ret(calle(&ea, down, vec![bin(&ea, BinaryOpKind::Subtract, idref(&ea, n), num(&ea, 1))])),
        ]);
        let stmts = vec![
            fndef(down, vec![(n, int_ty)], body),
            letb(x, calle(&ea, down, vec![num(&ea, 900)])),
            show(&ea, show_s, idref(&ea, x)),
        ];
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "0");
    }

    #[test]
    fn vm_show_to_function_recipient_calls_it() {
        // `Show x to f` calls f(x); `Give x to f` likewise.
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let ta: Arena<TypeExpr> = Arena::new();
        let mut it = Interner::new();
        let sink = it.intern("sink");
        let v = it.intern("v");
        let show_s = it.intern("show");
        let int_ty = ta.alloc(TypeExpr::Primitive(it.intern("Int")));
        let body: &[Stmt] = sa.alloc_slice(vec![show(
            &ea,
            show_s,
            bin(&ea, BinaryOpKind::Multiply, idref(&ea, v), num(&ea, 2)),
        )]);
        let stmts = vec![
            fndef(sink, vec![(v, int_ty)], body),
            Stmt::Show { object: num(&ea, 21), recipient: idref(&ea, sink) },
            Stmt::Give { object: num(&ea, 5), recipient: idref(&ea, sink) },
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["42", "10"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    // ---- Sprint 5: block scoping, Break, Repeat --------------------------------

    /// Full-outcome differential: output AND error must both match the oracle.
    fn assert_outcome_eq_treewalk(stmts: &[Stmt], interner: &Interner) {
        let (vm_out, vm_err) = run_to_outcome(stmts, interner, None, None);
        let (tw_out, tw_err) = match run_treewalk_outcome(stmts, interner) {
            (o, e) => (o, e),
        };
        assert_eq!(
            normalize(&vm_out),
            normalize(&tw_out),
            "partial output diverged (vm err {:?}, tw err {:?})",
            vm_err,
            tw_err
        );
        assert_eq!(vm_err, tw_err, "error diverged");
    }

    /// Tree-walker outcome with partial output preserved.
    fn run_treewalk_outcome(stmts: &[Stmt], interner: &Interner) -> (String, Option<String>) {
        use crate::interpreter::{Interpreter, OutputCallback};
        let buf = Rc::new(RefCell::new(String::new()));
        let sink = buf.clone();
        let cb: OutputCallback = Rc::new(RefCell::new(move |s: String| {
            sink.borrow_mut().push_str(&s);
            sink.borrow_mut().push('\n');
        }));
        let mut interp = Interpreter::new(interner).with_output_callback(cb);
        let err = interp.run_sync(stmts).err();
        let out = buf.borrow().clone();
        (out, err)
    }

    fn brk<'a>() -> Stmt<'a> {
        Stmt::Break
    }
    fn repeat_ident<'a>(var: Symbol, iterable: &'a Expr<'a>, body: &'a [Stmt<'a>]) -> Stmt<'a> {
        use crate::ast::stmt::Pattern;
        Stmt::Repeat { pattern: Pattern::Identifier(var), iterable, body }
    }

    #[test]
    fn vm_break_exits_innermost_while() {
        // i counts 0..; If i >= 3: Break — output 0,1,2.
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let i = it.intern("i");
        let show_s = it.intern("show");
        let break_blk: &[Stmt] = sa.alloc_slice(vec![brk()]);
        let body: &[Stmt] = sa.alloc_slice(vec![
            Stmt::If {
                cond: bin(&ea, BinaryOpKind::GtEq, idref(&ea, i), num(&ea, 3)),
                then_block: break_blk,
                else_block: None,
            },
            show(&ea, show_s, idref(&ea, i)),
            Stmt::Set { target: i, value: bin(&ea, BinaryOpKind::Add, idref(&ea, i), num(&ea, 1)) },
        ]);
        let stmts = vec![
            Stmt::Let { var: i, ty: None, value: num(&ea, 0), mutable: true },
            Stmt::While { cond: bin(&ea, BinaryOpKind::Lt, idref(&ea, i), num(&ea, 100)), body, decreasing: None },
            show(&ea, show_s, idref(&ea, i)),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["0", "1", "2", "3"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_break_only_exits_inner_loop() {
        // Outer runs 2 iterations; inner breaks immediately each time.
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let i = it.intern("i");
        let j = it.intern("j");
        let show_s = it.intern("show");
        let inner_body: &[Stmt] = sa.alloc_slice(vec![brk()]);
        let outer_body: &[Stmt] = sa.alloc_slice(vec![
            Stmt::Let { var: j, ty: None, value: num(&ea, 0), mutable: true },
            Stmt::While { cond: bin(&ea, BinaryOpKind::Lt, idref(&ea, j), num(&ea, 5)), body: inner_body, decreasing: None },
            show(&ea, show_s, idref(&ea, i)),
            Stmt::Set { target: i, value: bin(&ea, BinaryOpKind::Add, idref(&ea, i), num(&ea, 1)) },
        ]);
        let stmts = vec![
            Stmt::Let { var: i, ty: None, value: num(&ea, 0), mutable: true },
            Stmt::While { cond: bin(&ea, BinaryOpKind::Lt, idref(&ea, i), num(&ea, 2)), body: outer_body, decreasing: None },
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["0", "1"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_break_at_top_level_halts_program() {
        // The tree-walker's run loop treats Break at Main top level as stop.
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let show_s = it.intern("show");
        let stmts = vec![
            show(&ea, show_s, num(&ea, 1)),
            brk(),
            show(&ea, show_s, num(&ea, 2)),
        ];
        assert_outcome_eq_treewalk(&stmts, &it);
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "1");
    }

    #[test]
    fn vm_return_at_top_level_halts_program() {
        // Return in Main = stop, not "return with no caller".
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let show_s = it.intern("show");
        let stmts = vec![
            show(&ea, show_s, num(&ea, 1)),
            ret(num(&ea, 99)),
            show(&ea, show_s, num(&ea, 2)),
        ];
        assert_outcome_eq_treewalk(&stmts, &it);
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "1");
    }

    #[test]
    fn vm_block_let_does_not_leak_scope() {
        // Let inside an If-block is undone after the block (tree-walker
        // execute_block scoping). The use after the block fails AT RUNTIME with
        // the partial output preserved.
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let t = it.intern("t");
        let show_s = it.intern("show");
        let then_blk: &[Stmt] = sa.alloc_slice(vec![
            letb(t, num(&ea, 5)),
            show(&ea, show_s, idref(&ea, t)),
        ]);
        let stmts = vec![
            Stmt::If { cond: boolean(&ea, true), then_block: then_blk, else_block: None },
            show(&ea, show_s, idref(&ea, t)),
        ];
        assert_outcome_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_unbound_identifier_in_dead_branch_is_free() {
        // `Show ghost` inside `If false:` must not fail in either engine —
        // unbound names are a RUNTIME error, not a compile error.
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let ghost = it.intern("ghost");
        let show_s = it.intern("show");
        let dead: &[Stmt] = sa.alloc_slice(vec![show(&ea, show_s, idref(&ea, ghost))]);
        let stmts = vec![
            Stmt::If { cond: boolean(&ea, false), then_block: dead, else_block: None },
            show(&ea, show_s, num(&ea, 7)),
        ];
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "7");
        assert_outcome_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_repeat_list_snapshot_semantics() {
        // Pushing to the list inside the loop must NOT extend the iteration:
        // the tree-walker snapshots the collection before looping.
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let xs = it.intern("xs");
        let x = it.intern("x");
        let show_s = it.intern("show");
        let body: &[Stmt] = sa.alloc_slice(vec![
            push_to(num(&ea, 9), idref(&ea, xs)),
            show(&ea, show_s, idref(&ea, x)),
        ]);
        let stmts = vec![
            Stmt::Let { var: xs, ty: None, value: list_lit(&ea, vec![num(&ea, 1), num(&ea, 2)]), mutable: true },
            repeat_ident(x, idref(&ea, xs), body),
            show(&ea, show_s, length_of(&ea, idref(&ea, xs))),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["1", "2", "4"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_repeat_over_text_iterates_chars() {
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let s = it.intern("s");
        let c = it.intern("c");
        let show_s = it.intern("show");
        let abc = it.intern("abc");
        let body: &[Stmt] = sa.alloc_slice(vec![show(&ea, show_s, idref(&ea, c))]);
        let stmts = vec![
            letb(s, text(&ea, abc)),
            repeat_ident(c, idref(&ea, s), body),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["a", "b", "c"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_repeat_map_single_entry_tuple_pattern() {
        use crate::ast::stmt::Pattern;
        // One-entry map (iteration order is nondeterministic for larger maps).
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let m = it.intern("m");
        let k = it.intern("k");
        let v = it.intern("v");
        let map_ty = it.intern("Map");
        let key_a = it.intern("a");
        let show_s = it.intern("show");
        let body: &[Stmt] = sa.alloc_slice(vec![
            show(&ea, show_s, idref(&ea, k)),
            show(&ea, show_s, idref(&ea, v)),
        ]);
        let stmts = vec![
            Stmt::Let { var: m, ty: None, value: new_coll(&ea, map_ty), mutable: true },
            Stmt::SetIndex { collection: idref(&ea, m), index: text(&ea, key_a), value: num(&ea, 10) },
            Stmt::Repeat { pattern: Pattern::Tuple(vec![k, v]), iterable: idref(&ea, m), body },
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["a", "10"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_repeat_tuple_pattern_on_non_tuple_errors_like_treewalk() {
        use crate::ast::stmt::Pattern;
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let k = it.intern("k");
        let v = it.intern("v");
        let show_s = it.intern("show");
        let body: &[Stmt] = sa.alloc_slice(vec![show(&ea, show_s, idref(&ea, k))]);
        let stmts = vec![Stmt::Repeat {
            pattern: Pattern::Tuple(vec![k, v]),
            iterable: list_lit(&ea, vec![num(&ea, 1)]),
            body,
        }];
        assert_err_parity(&stmts, &it);
    }

    #[test]
    fn vm_repeat_break_exits_and_loop_var_scoped() {
        // Break exits the Repeat; the loop variable is gone after the loop.
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let xs = it.intern("xs");
        let x = it.intern("x");
        let show_s = it.intern("show");
        let break_blk: &[Stmt] = sa.alloc_slice(vec![brk()]);
        let body: &[Stmt] = sa.alloc_slice(vec![
            show(&ea, show_s, idref(&ea, x)),
            Stmt::If {
                cond: bin(&ea, BinaryOpKind::GtEq, idref(&ea, x), num(&ea, 2)),
                then_block: break_blk,
                else_block: None,
            },
        ]);
        let stmts = vec![
            letb(xs, list_lit(&ea, vec![num(&ea, 1), num(&ea, 2), num(&ea, 3)])),
            repeat_ident(x, idref(&ea, xs), body),
            show(&ea, show_s, idref(&ea, x)),
        ];
        // Both engines: print 1, 2, then `x` is out of scope after the loop.
        assert_outcome_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_repeat_over_int_errors_like_treewalk() {
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let x = it.intern("x");
        let show_s = it.intern("show");
        let body: &[Stmt] = sa.alloc_slice(vec![show(&ea, show_s, idref(&ea, x))]);
        let stmts = vec![repeat_ident(x, num(&ea, 42), body)];
        assert_err_parity(&stmts, &it);
    }

    // ---- Sprint 5 (statement core): Pop, RuntimeAssert, Zone, Concurrent ------

    #[test]
    fn vm_pop_into_binds_and_empty_pop_is_nothing() {
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let xs = it.intern("xs");
        let v = it.intern("v");
        let w = it.intern("w");
        let show_s = it.intern("show");
        let stmts = vec![
            Stmt::Let { var: xs, ty: None, value: list_lit(&ea, vec![num(&ea, 1), num(&ea, 2)]), mutable: true },
            Stmt::Pop { collection: idref(&ea, xs), into: Some(v) },
            show(&ea, show_s, idref(&ea, v)),
            show(&ea, show_s, length_of(&ea, idref(&ea, xs))),
            Stmt::Pop { collection: idref(&ea, xs), into: None },
            Stmt::Pop { collection: idref(&ea, xs), into: Some(w) },
            show(&ea, show_s, idref(&ea, w)),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["2", "1", "nothing"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_runtime_assert_passes_and_fails_like_treewalk() {
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let show_s = it.intern("show");
        let ok_stmts = vec![
            Stmt::RuntimeAssert { condition: boolean(&ea, true) , hard: false },
            show(&ea, show_s, num(&ea, 1)),
        ];
        assert_eq!(compile_and_run(&ok_stmts, &it).unwrap().trim(), "1");
        assert_vm_eq_treewalk(&ok_stmts, &it);

        let fail_stmts = vec![
            show(&ea, show_s, num(&ea, 1)),
            Stmt::RuntimeAssert { condition: boolean(&ea, false) , hard: false },
            show(&ea, show_s, num(&ea, 2)),
        ];
        assert_outcome_eq_treewalk(&fail_stmts, &it);
    }

    #[test]
    fn vm_zone_swallows_return_inside_function() {
        // The tree-walker DISCARDS a zone body's ControlFlow: a Return inside
        // a Zone does not return from the function.
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let ta: Arena<TypeExpr> = Arena::new();
        let mut it = Interner::new();
        let f = it.intern("f");
        let z = it.intern("z");
        let r = it.intern("r");
        let show_s = it.intern("show");
        let int_ty = ta.alloc(TypeExpr::Primitive(it.intern("Int")));
        let _ = int_ty;

        let zone_body: &[Stmt] = sa.alloc_slice(vec![ret(num(&ea, 5))]);
        let f_body: &[Stmt] = sa.alloc_slice(vec![
            Stmt::Zone { name: z, capacity: None, source_file: None, body: zone_body },
            show(&ea, show_s, num(&ea, 77)),
            ret(num(&ea, 1)),
        ]);
        let stmts = vec![
            fndef(f, vec![], f_body),
            letb(r, calle(&ea, f, vec![])),
            show(&ea, show_s, idref(&ea, r)),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["77", "1"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_zone_swallows_break_in_loop() {
        // `While …: Zone: Break` — the zone catches the Break before the loop
        // sees it, so the loop runs to its own condition.
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let i = it.intern("i");
        let z = it.intern("z");
        let show_s = it.intern("show");
        let zone_body: &[Stmt] = sa.alloc_slice(vec![brk()]);
        let body: &[Stmt] = sa.alloc_slice(vec![
            Stmt::Zone { name: z, capacity: None, source_file: None, body: zone_body },
            show(&ea, show_s, idref(&ea, i)),
            Stmt::Set { target: i, value: bin(&ea, BinaryOpKind::Add, idref(&ea, i), num(&ea, 1)) },
        ]);
        let stmts = vec![
            Stmt::Let { var: i, ty: None, value: num(&ea, 0), mutable: true },
            Stmt::While { cond: bin(&ea, BinaryOpKind::Lt, idref(&ea, i), num(&ea, 2)), body, decreasing: None },
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["0", "1"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_return_inside_repeat_inside_zone_unwinds_iterator() {
        // The Return jumps out across a live Repeat into the zone end; the
        // iterator must be unwound so a LATER Repeat still works.
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let z = it.intern("z");
        let x = it.intern("x");
        let y = it.intern("y");
        let show_s = it.intern("show");
        let rep_body: &[Stmt] = sa.alloc_slice(vec![ret(idref(&ea, x))]);
        let zone_body: &[Stmt] = sa.alloc_slice(vec![repeat_ident(
            x,
            list_lit(&ea, vec![num(&ea, 1), num(&ea, 2), num(&ea, 3)]),
            rep_body,
        )]);
        let second_body: &[Stmt] = sa.alloc_slice(vec![show(&ea, show_s, idref(&ea, y))]);
        let stmts = vec![
            Stmt::Zone { name: z, capacity: None, source_file: None, body: zone_body },
            show(&ea, show_s, num(&ea, 0)),
            repeat_ident(y, list_lit(&ea, vec![num(&ea, 7), num(&ea, 8)]), second_body),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["0", "7", "8"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_concurrent_tasks_run_sequentially_share_scope_and_swallow_flow() {
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let a = it.intern("a");
        let show_s = it.intern("show");
        let tasks: &[Stmt] = sa.alloc_slice(vec![
            letb(a, num(&ea, 1)),
            show(&ea, show_s, idref(&ea, a)),
            ret(num(&ea, 9)),
            show(&ea, show_s, num(&ea, 3)),
        ]);
        let stmts = vec![
            Stmt::Concurrent { tasks },
            // The task's Let persists (tasks execute in the enclosing scope),
            // and the Return was swallowed.
            show(&ea, show_s, idref(&ea, a)),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["1", "3", "1"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    // ---- Sprint 4: short-circuit And/Or, Concat/BitXor/Shl/Shr, Not, literals --

    fn not_e<'a>(ea: &'a Arena<Expr<'a>>, operand: &'a Expr<'a>) -> &'a Expr<'a> {
        ea.alloc(Expr::Not { operand })
    }

    /// `## To noisy: Show 9. Return 1.` — its output is the side-effect probe.
    fn noisy_fn<'a>(
        ea: &'a Arena<Expr<'a>>,
        sa: &'a Arena<Stmt<'a>>,
        noisy: Symbol,
        show_s: Symbol,
    ) -> Stmt<'a> {
        let body: &[Stmt] = sa.alloc_slice(vec![
            show(ea, show_s, num(ea, 9)),
            ret(num(ea, 1)),
        ]);
        fndef(noisy, vec![], body)
    }

    #[test]
    fn vm_and_short_circuits_side_effects() {
        // false and noisy() — noisy must NOT run; output is just "false".
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let noisy = it.intern("noisy");
        let r = it.intern("r");
        let show_s = it.intern("show");
        let stmts = vec![
            noisy_fn(&ea, &sa, noisy, show_s),
            letb(r, bin(&ea, BinaryOpKind::And, boolean(&ea, false), calle(&ea, noisy, vec![]))),
            show(&ea, show_s, idref(&ea, r)),
        ];
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "false");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_or_short_circuits_side_effects() {
        // true or noisy() — noisy must NOT run; output is just "true".
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let noisy = it.intern("noisy");
        let r = it.intern("r");
        let show_s = it.intern("show");
        let stmts = vec![
            noisy_fn(&ea, &sa, noisy, show_s),
            letb(r, bin(&ea, BinaryOpKind::Or, boolean(&ea, true), calle(&ea, noisy, vec![]))),
            show(&ea, show_s, idref(&ea, r)),
        ];
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "true");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_and_is_logical_truthiness_to_bool() {
        // `and` is LOGICAL: `6 and 3` → true. A truthy left still evaluates
        // the right (noisy RUNS, prints 9; returns 1 → truthy → true). The
        // bitwise spelling is `&` (Op::BitAnd), locked below.
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let noisy = it.intern("noisy");
        let a = it.intern("a");
        let b = it.intern("b");
        let show_s = it.intern("show");
        let stmts = vec![
            noisy_fn(&ea, &sa, noisy, show_s),
            letb(a, bin(&ea, BinaryOpKind::And, num(&ea, 6), num(&ea, 3))),
            show(&ea, show_s, idref(&ea, a)),
            letb(b, bin(&ea, BinaryOpKind::And, num(&ea, 6), calle(&ea, noisy, vec![]))),
            show(&ea, show_s, idref(&ea, b)),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["true", "9", "true"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_bitand_bitor_int_are_bitwise() {
        // `6 & 3` → 2 and `6 | 3` → 7: the symbols are the bitwise ops.
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let a = it.intern("a");
        let b = it.intern("b");
        let show_s = it.intern("show");
        let stmts = vec![
            letb(a, bin(&ea, BinaryOpKind::BitAnd, num(&ea, 6), num(&ea, 3))),
            show(&ea, show_s, idref(&ea, a)),
            letb(b, bin(&ea, BinaryOpKind::BitOr, num(&ea, 6), num(&ea, 3))),
            show(&ea, show_s, idref(&ea, b)),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["2", "7"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_and_or_mixed_int_bool_uses_truthiness() {
        // Truthiness applies uniformly — Int operands coerce like any other.
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let a = it.intern("a");
        let b = it.intern("b");
        let show_s = it.intern("show");
        let stmts = vec![
            letb(a, bin(&ea, BinaryOpKind::And, num(&ea, 1), boolean(&ea, false))),
            show(&ea, show_s, idref(&ea, a)),
            letb(b, bin(&ea, BinaryOpKind::Or, num(&ea, 0), boolean(&ea, true))),
            show(&ea, show_s, idref(&ea, b)),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["false", "true"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_not_is_logical_truthiness() {
        // `not` negates truthiness — Bool out for every operand (`~` is the
        // bitwise complement and lowers to `x ^ -1`, never through Op::Not).
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let a = it.intern("a");
        let b = it.intern("b");
        let show_s = it.intern("show");
        let stmts = vec![
            letb(a, not_e(&ea, num(&ea, 6))),
            show(&ea, show_s, idref(&ea, a)),
            letb(b, not_e(&ea, boolean(&ea, true))),
            show(&ea, show_s, idref(&ea, b)),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["false", "false"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_not_text_is_emptiness_matches_treewalk() {
        // `not "hi"` → false (non-empty Text is truthy); total, never an error.
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let x = it.intern("x");
        let hi = it.intern("hi");
        let show_s = it.intern("show");
        let stmts = vec![
            letb(x, not_e(&ea, text(&ea, hi))),
            show(&ea, show_s, idref(&ea, x)),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["false"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_concat_bitxor_shl_shr_match_treewalk() {
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let show_s = it.intern("show");
        let names: Vec<Symbol> = (0..4).map(|k| it.intern(&format!("o{k}"))).collect();
        let stmts = vec![
            letb(names[0], bin(&ea, BinaryOpKind::Concat, num(&ea, 1), num(&ea, 2))),
            show(&ea, show_s, idref(&ea, names[0])),
            letb(names[1], bin(&ea, BinaryOpKind::BitXor, num(&ea, 6), num(&ea, 3))),
            show(&ea, show_s, idref(&ea, names[1])),
            letb(names[2], bin(&ea, BinaryOpKind::Shl, num(&ea, 1), num(&ea, 3))),
            show(&ea, show_s, idref(&ea, names[2])),
            letb(names[3], bin(&ea, BinaryOpKind::Shr, num(&ea, 16), num(&ea, 2))),
            show(&ea, show_s, idref(&ea, names[3])),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.lines().collect::<Vec<_>>(), vec!["12", "5", "8", "4"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_char_nothing_and_temporal_literals_display_like_treewalk() {
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let show_s = it.intern("show");
        let lits: Vec<&Expr> = vec![
            ea.alloc(Expr::Literal(Literal::Char('x'))),
            ea.alloc(Expr::Literal(Literal::Nothing)),
            ea.alloc(Expr::Literal(Literal::Duration(1_500_000_000))),
            ea.alloc(Expr::Literal(Literal::Date(19753))),
            ea.alloc(Expr::Literal(Literal::Moment(86_400_000_000_000))),
            ea.alloc(Expr::Literal(Literal::Span { months: 2, days: 3 })),
            ea.alloc(Expr::Literal(Literal::Time(3_600_000_000_000))),
        ];
        let mut stmts = Vec::new();
        for (k, lit) in lits.into_iter().enumerate() {
            let v = it.intern(&format!("lit{k}"));
            stmts.push(letb(v, lit));
            stmts.push(show(&ea, show_s, idref(&ea, v)));
        }
        assert_vm_eq_treewalk(&stmts, &it);
    }

    // ---- Sprint 0: encoding widening + compiler hardening ---------------------

    #[test]
    fn vm_compiles_300_element_list_literal() {
        // 300 > u8::MAX — list literals must not be capped at 255 elements.
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let xs = it.intern("xs");
        let show_s = it.intern("show");
        let items: Vec<&Expr> = (0..300).map(|k| num(&ea, k)).collect();
        let stmts = vec![
            letb(xs, list_lit(&ea, items)),
            show(&ea, show_s, length_of(&ea, idref(&ea, xs))),
            show(&ea, show_s, index_at(&ea, idref(&ea, xs), num(&ea, 300))),
        ];
        let out = compile_and_run(&stmts, &it).unwrap();
        let lines: Vec<&str> = out.lines().collect();
        assert_eq!(lines, vec!["300", "299"]);
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_compiles_main_with_400_locals() {
        // 400 locals exceed a u8 register index — frames must address more
        // than 256 registers.
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let show_s = it.intern("show");
        let mut stmts = Vec::new();
        let mut last = None;
        for k in 0..400i64 {
            let v = it.intern(&format!("v{k}"));
            stmts.push(letb(v, num(&ea, k)));
            last = Some(v);
        }
        stmts.push(show(&ea, show_s, idref(&ea, last.unwrap())));
        assert_eq!(compile_and_run(&stmts, &it).unwrap().trim(), "399");
        assert_vm_eq_treewalk(&stmts, &it);
    }

    #[test]
    fn vm_const_pool_dedups_identical_literals() {
        // The same literal used many times must occupy one constant-pool slot.
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let mut stmts = Vec::new();
        for k in 0..50 {
            let v = it.intern(&format!("c{k}"));
            stmts.push(letb(v, num(&ea, 7)));
        }
        let program = Compiler::compile(&stmts, &it).unwrap();
        assert_eq!(
            program.constants.len(),
            1,
            "identical Int literals must dedup to a single pool entry"
        );
    }

    #[test]
    fn vm_patch_jump_is_total() {
        // Backpatching a non-jump instruction is a compiler bug that must
        // surface as Err, never a panic.
        let mut code = vec![Op::Halt];
        assert!(super::compiler::patch_jump(&mut code, 0, 5).is_err());

        let mut code = vec![Op::Jump { target: usize::MAX }];
        assert!(super::compiler::patch_jump(&mut code, 0, 5).is_ok());
        assert!(matches!(code[0], Op::Jump { target: 5 }));

        // Out-of-bounds instruction index must also be an Err, not a panic.
        let mut code = vec![Op::Halt];
        assert!(super::compiler::patch_jump(&mut code, 9, 5).is_err());
    }

    #[test]
    fn vm_deeply_nested_expr_errors_not_overflows() {
        // A 100k-deep expression must produce a compile error, not blow the
        // native stack.
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let x = it.intern("x");
        let mut e = num(&ea, 1);
        for _ in 0..100_000 {
            e = bin(&ea, BinaryOpKind::Add, e, num(&ea, 1));
        }
        let stmts = vec![letb(x, e)];
        let err = Compiler::compile(&stmts, &it).unwrap_err();
        assert!(err.contains("too deeply nested"), "got: {err}");
    }

    #[test]
    fn vm_range_with_float_bound_errors_not_panics() {
        // `1 to 2.5` — the tree-walker errors with "Range requires Int bounds";
        // the VM must return the same Err, never panic.
        let ea: Arena<Expr> = Arena::new();
        let mut it = Interner::new();
        let xs = it.intern("xs");
        let f = ea.alloc(Expr::Literal(Literal::Float(2.5)));
        let stmts = vec![letb(xs, range_e(&ea, num(&ea, 1), f))];
        let vm_err = compile_and_run(&stmts, &it).unwrap_err();
        let tw_err = run_treewalk(&stmts, &it).unwrap_err();
        assert_eq!(vm_err, tw_err);
    }

    #[test]
    fn vm_register_file_growth_is_capped() {
        // WIDE frames recursing: each frame claims ~3000 registers (a 3000-
        // element list literal), so the register file hits MAX_REGISTER_FILE
        // (1M) near depth ~350 — well before the 1000 call-depth limit. The
        // VM must error, not consume unbounded memory. (VM-only: the
        // tree-walker would blow the native stack on this program.)
        //
        // The recursion is NON-tail (`spin(n+1) - 1`): a direct `return spin(n+1)`
        // is a self-tail-call and TCO loops it in constant stack (one frame, no
        // register growth), so it would never hit the cap. The `- 1` keeps the
        // call out of tail position so real frames accumulate.
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let ta: Arena<TypeExpr> = Arena::new();
        let mut it = Interner::new();
        let spin = it.intern("spin");
        let n = it.intern("n");
        let big = it.intern("big");
        let r = it.intern("r");
        let show_s = it.intern("show");
        let int_ty = ta.alloc(TypeExpr::Primitive(it.intern("Int")));

        let then_blk: &[Stmt] = sa.alloc_slice(vec![ret(num(&ea, 0))]);
        let cond = bin(&ea, BinaryOpKind::GtEq, idref(&ea, n), num(&ea, 900));
        let wide: Vec<&Expr> = (0..3000).map(|_| num(&ea, 0)).collect();
        let rec = calle(&ea, spin, vec![bin(&ea, BinaryOpKind::Add, idref(&ea, n), num(&ea, 1))]);
        let rec_nontail = bin(&ea, BinaryOpKind::Subtract, rec, num(&ea, 1));
        let body: &[Stmt] = sa.alloc_slice(vec![
            Stmt::If { cond, then_block: then_blk, else_block: None },
            letb(big, list_lit(&ea, wide)),
            ret(rec_nontail),
        ]);
        let stmts = vec![
            fndef(spin, vec![(n, int_ty)], body),
            letb(r, calle(&ea, spin, vec![num(&ea, 0)])),
            show(&ea, show_s, idref(&ea, r)),
        ];
        let err = compile_and_run(&stmts, &it).unwrap_err();
        assert!(err.contains("register file"), "got: {err}");
    }

    #[test]
    fn vm_if_else_takes_correct_branch() {
        // Let x be 3. If x > 5: Show 100. Otherwise: Show 200.   → 200
        let ea: Arena<Expr> = Arena::new();
        let sa: Arena<Stmt> = Arena::new();
        let mut it = Interner::new();
        let x = it.intern("x");
        let console = it.intern("show");

        let three = ea.alloc(Expr::Literal(Literal::Number(3)));
        let x_c = ea.alloc(Expr::Identifier(x));
        let five = ea.alloc(Expr::Literal(Literal::Number(5)));
        let cond = ea.alloc(Expr::BinaryOp { op: BinaryOpKind::Gt, left: x_c, right: five });

        let hundred = ea.alloc(Expr::Literal(Literal::Number(100)));
        let two_hundred = ea.alloc(Expr::Literal(Literal::Number(200)));
        let cref1 = ea.alloc(Expr::Identifier(console));
        let cref2 = ea.alloc(Expr::Identifier(console));
        let then_block: &[Stmt] = sa.alloc_slice(vec![Stmt::Show { object: hundred, recipient: cref1 }]);
        let else_block: &[Stmt] = sa.alloc_slice(vec![Stmt::Show { object: two_hundred, recipient: cref2 }]);

        let stmts = vec![
            Stmt::Let { var: x, ty: None, value: three, mutable: false },
            Stmt::If { cond, then_block, else_block: Some(else_block) },
        ];

        let out = compile_and_run(&stmts, &it).unwrap();
        assert_eq!(out.trim(), "200");
        assert_vm_eq_treewalk(&stmts, &it);
    }
}