strykelang 0.7.2

A highly parallel Perl 5 interpreter written in Rust
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
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
use std::fs::File;
use std::io::{self, BufReader, IsTerminal, Read as IoRead, Write};
use std::path::{Path, PathBuf};
use std::process;
use std::sync::Mutex;

use clap::Parser;
use rand::Rng;
use rayon::prelude::*;

use stryke::ast::Program;
use stryke::error::{ErrorKind, PerlError};
use stryke::interpreter::Interpreter;
use stryke::perl_fs::{
    decode_utf8_or_latin1, read_file_text_perl_compat, read_line_perl_compat,
    read_logical_line_perl_compat,
};

mod repl;

/// stryke — A highly parallel Perl 5 interpreter written in Rust
#[derive(Parser, Debug, Default)]
#[command(name = "stryke", version, about, long_about = None)]
#[command(disable_version_flag = true, disable_help_flag = true)]
#[command(override_usage = "stryke [switches] [--] [programfile] [arguments]")]
pub(crate) struct Cli {
    /// Specify record separator (\0 if no argument); -0777 for slurp mode
    #[arg(short = '0', value_name = "OCTAL")]
    input_separator: Option<Option<String>>,

    /// Autosplit mode with -n or -p (splits $_ into @F)
    #[arg(short = 'a')]
    auto_split: bool,

    /// Enables the listed Unicode features
    #[arg(short = 'C', value_name = "NUMBER/LIST")]
    unicode_features: Option<Option<String>>,

    /// Check syntax only (parse; does not compile or run)
    #[arg(short = 'c')]
    check_only: bool,

    /// Parse and compile without executing (bytecode compile check; alias `--check`)
    #[arg(long = "lint", alias = "check")]
    lint: bool,

    /// Print bytecode disassembly to stderr before VM execution (alias `--disassemble`)
    #[arg(long = "disasm", alias = "disassemble")]
    disasm: bool,

    /// Dump the parsed abstract syntax tree as JSON to stdout and exit (no execution)
    #[arg(long = "ast")]
    dump_ast: bool,

    /// Pretty-print parsed Perl to stdout and exit (no execution)
    #[arg(long = "fmt")]
    format_source: bool,

    /// Wall-clock profile: per-line + per-sub timings on stderr (VM: opcode-level lines; JIT off)
    #[arg(long = "profile")]
    profile: bool,

    /// Flamegraph: colored terminal bars (TTY) or SVG to stdout (piped: stryke --flame x.stk > flame.svg)
    #[arg(long = "flame")]
    flame: bool,

    /// Disable Cranelift JIT for bytecode VM (opcode interpreter only)
    #[arg(long = "no-jit")]
    no_jit: bool,

    /// Print expanded hint for an error code (e.g. E0001) and exit
    #[arg(long = "explain", value_name = "CODE")]
    explain: Option<String>,

    /// Run program under debugger or module Devel::MOD
    #[arg(short = 'd', value_name = "MOD")]
    debugger: Option<Option<String>>,

    /// Set debugging flags (argument is a bit mask or alphabets)
    #[arg(short = 'D', value_name = "FLAGS")]
    debug_flags: Option<Option<String>>,

    /// One line of program (several -e's allowed, omit programfile)
    #[arg(short = 'e')]
    execute: Vec<String>,

    /// Like -e, but enables all optional features
    #[arg(short = 'E')]
    execute_features: Vec<String>,

    /// Don't do $sitelib/sitecustomize.pl at startup
    #[arg(short = 'f')]
    no_sitecustomize: bool,

    /// Split() pattern for -a switch (//'s are optional)
    #[arg(short = 'F', value_name = "PATTERN")]
    field_separator: Option<String>,

    /// Read all input in one go (slurp), alias for -0777
    #[arg(short = 'g')]
    slurp: bool,

    /// Edit <> files in place (makes backup if extension supplied)
    #[arg(short = 'i', value_name = "EXTENSION")]
    inplace: Option<Option<String>>,

    /// Specify @INC/#include directory (several -I's allowed)
    #[arg(short = 'I', value_name = "DIRECTORY")]
    include: Vec<String>,

    /// Enable line ending processing, specifies line terminator
    #[arg(short = 'l', value_name = "OCTNUM")]
    line_ending: Option<Option<String>>,

    /// Execute "use module..." before executing program
    #[arg(short = 'M', value_name = "MODULE")]
    use_module: Vec<String>,

    /// Execute "use module ()" before executing program (no import)
    #[arg(short = 'm', value_name = "MODULE")]
    use_module_no_import: Vec<String>,

    /// Assume "while (<>) { ... }" loop around program
    #[arg(short = 'n')]
    line_mode: bool,

    /// Assume loop like -n but print line also, like sed
    #[arg(short = 'p')]
    print_mode: bool,

    /// Enable rudimentary parsing for switches after programfile
    #[arg(short = 's')]
    switch_parsing: bool,

    /// Look for programfile using PATH environment variable
    #[arg(short = 'S')]
    path_lookup: bool,

    /// Enable tainting warnings
    #[arg(short = 't')]
    taint_warn: bool,

    /// Enable tainting checks
    #[arg(short = 'T')]
    taint_check: bool,

    /// Dump core after parsing program
    #[arg(short = 'u')]
    dump_core: bool,

    /// Allow unsafe operations
    #[arg(short = 'U')]
    unsafe_ops: bool,

    /// Print version, patchlevel and license
    #[arg(short = 'v')]
    show_version: bool,

    /// Print configuration summary (or a single Config.pm variable)
    #[arg(short = 'V', value_name = "CONFIGVAR")]
    show_config: Option<Option<String>>,

    /// Enable many useful warnings
    #[arg(short = 'w')]
    warnings: bool,

    /// Enable all warnings
    #[arg(short = 'W')]
    all_warnings: bool,

    /// Ignore text before #!perl line (optionally cd to directory)
    #[arg(short = 'x', value_name = "DIRECTORY")]
    extract: Option<Option<String>>,

    /// Disable all warnings
    #[arg(short = 'X')]
    no_warnings: bool,

    /// Print help
    #[arg(short = 'h', long = "help")]
    help: bool,

    /// Number of threads for parallel operations (stryke extension)
    #[arg(short = 'j', long = "threads", value_name = "N")]
    threads: Option<usize>,

    /// Perl 5 strict-compatibility mode: disable all stryke extensions
    #[arg(long = "compat")]
    compat: bool,

    /// Force argument to be treated as a script file (skip code detection)
    #[arg(long = "script")]
    force_script: bool,

    /// Script file to execute
    #[arg(value_name = "SCRIPT")]
    script: Option<String>,

    /// Arguments passed to the script (@ARGV)
    #[arg(value_name = "ARGS", trailing_var_arg = true)]
    args: Vec<String>,
}

/// Expand Perl-style bundled short switches (`-lane` → `-l -a -n -e`, `-0777` unchanged) before
/// clap parses. Stock clap treats `-lane` as `-l` with value `ane`.
fn expand_perl_bundled_argv(args: Vec<String>) -> Vec<String> {
    if args.is_empty() {
        return args;
    }
    let mut out = vec![args[0].clone()];
    let mut seen_dd = false;
    for arg in args.into_iter().skip(1) {
        if seen_dd {
            out.push(arg);
            continue;
        }
        if arg == "--" {
            seen_dd = true;
            out.push(arg);
            continue;
        }
        match expand_perl_bundled_token(&arg) {
            Some(parts) => out.extend(parts),
            None => out.push(arg),
        }
    }
    out
}

/// Perl documents `-help` / `-version` as aliases; bundling would mis-parse them as `-h`+`-e`+….
fn expand_perl_bundled_token(arg: &str) -> Option<Vec<String>> {
    match arg {
        "-help" | "--help" => return Some(vec!["-h".to_string()]),
        "-version" | "--version" => return Some(vec!["-v".to_string()]),
        _ => {}
    }
    if arg == "-" || !arg.starts_with('-') || arg.starts_with("--") {
        return None;
    }
    let s = arg.strip_prefix('-')?;
    if s.is_empty() || s.len() == 1 {
        return None;
    }
    // Operators like `->>`, `->`, `-~>` start with non-letter after `-`; not bundled flags.
    if s.starts_with('>') || s.starts_with('~') {
        return None;
    }
    // `-0` / `-0777` — record separator; do not split into `-0` `-7` …
    if let Some(rest) = s.strip_prefix('0') {
        let rest_ok = rest.chars().all(|c| matches!(c, '0'..='7'));
        if rest_ok {
            return None;
        }
    }
    let mut out = Vec::new();
    let b = s.as_bytes();
    let mut i = 0usize;
    while i < b.len() {
        match b[i] {
            b'0' if i == 0 => {
                let mut j = i + 1;
                while j < b.len() && matches!(b[j], b'0'..=b'7') {
                    j += 1;
                }
                out.push("-0".to_string());
                if j > i + 1 {
                    out.push(s[i + 1..j].to_string());
                }
                i = j;
            }
            b'e' | b'E' => {
                let flag = if b[i] == b'e' { "-e" } else { "-E" };
                out.push(flag.to_string());
                if i + 1 < b.len() {
                    out.push(s[i + 1..].to_string());
                }
                return Some(out);
            }
            b'l' => {
                out.push("-l".to_string());
                i += 1;
                let start = i;
                while i < b.len() && matches!(b[i], b'0'..=b'7') {
                    i += 1;
                }
                if i > start {
                    out.push(s[start..i].to_string());
                }
            }
            // Flags that consume the rest of the token as their value:
            //   -F pattern  — split pattern for -a
            //   -M module   — use module
            //   -m module   — use module ()
            //   -I dir      — @INC directory
            //   -V:var      — config variable (Perl: `perl -V:version`)
            //   -d:mod      — debugger module
            //   -D flags    — debug flags
            //   -x dir      — ignore text before #!perl
            //   -C flags    — unicode features
            b'F' | b'M' | b'm' | b'I' | b'd' | b'D' | b'x' | b'C' => {
                let ch = b[i] as char;
                out.push(format!("-{ch}"));
                i += 1;
                if i < b.len() {
                    out.push(s[i..].to_string());
                }
                return Some(out);
            }
            b'V' => {
                // `-V:var` → `-V` `:var`; `-V` alone → `-V`
                out.push("-V".to_string());
                i += 1;
                if i < b.len() {
                    // Perl's `-V:version` passes `:version` but the handler expects just `version`.
                    let rest = &s[i..];
                    let rest = rest.strip_prefix(':').unwrap_or(rest);
                    out.push(rest.to_string());
                }
                return Some(out);
            }
            b'i' => {
                out.push("-i".to_string());
                i += 1;
                if i < b.len() && matches!(b[i], b'e' | b'E') {
                    continue;
                }
                if i < b.len() && b[i] == b'.' {
                    let start = i;
                    while i < b.len() && !matches!(b[i], b'e' | b'E') {
                        i += 1;
                    }
                    out.push(s[start..i].to_string());
                }
            }
            _ => {
                out.push(format!("-{}", b[i] as char));
                i += 1;
            }
        }
    }
    Some(out)
}

fn print_cyberpunk_help() {
    let version = env!("CARGO_PKG_VERSION");
    let bin = env!("CARGO_BIN_NAME");
    let threads = std::thread::available_parallelism()
        .map(|n| n.get())
        .unwrap_or(1);

    // ANSI color codes
    const C: &str = "\x1b[36m"; // cyan
    const M: &str = "\x1b[35m"; // magenta
    const R: &str = "\x1b[31m"; // red
    const Y: &str = "\x1b[33m"; // yellow
    const G: &str = "\x1b[32m"; // green
    const N: &str = "\x1b[0m"; // reset

    println!("{C} ███████╗████████╗██████╗ ██╗   ██╗██╗  ██╗███████╗{N}");
    println!("{C} ██╔════╝╚══██╔══╝██╔══██╗╚██╗ ██╔╝██║ ██╔╝██╔════╝{N}");
    println!("{M} ███████╗   ██║   ██████╔╝ ╚████╔╝ █████╔╝ █████╗  {N}");
    println!("{M} ╚════██║   ██║   ██╔══██╗  ╚██╔╝  ██╔═██╗ ██╔══╝  {N}");
    println!("{R} ███████║   ██║   ██║  ██║   ██║   ██║  ██╗███████╗{N}");
    println!("{R} ╚══════╝   ╚═╝   ╚═╝  ╚═╝   ╚═╝   ╚═╝  ╚═╝╚══════╝{N}");
    println!("{C} ┌──────────────────────────────────────────────────┐{N}");
    println!("{C} │ STATUS: ONLINE // CORES: {threads:<2} // SIGNAL: ██░      │{N}");
    println!("{C} └──────────────────────────────────────────────────┘{N}");
    println!("{M}  >> PARALLEL PERL5 INTERPRETER // RUST-POWERED v{version} <<{N}");
    println!();
    println!();
    println!("A highly parallel Perl 5 interpreter written in Rust");
    println!();
    println!("{Y}  USAGE:{N} {bin} 'CODE'                     {G}//{N} -e is optional");
    println!("{Y}        {N} {bin} [switches] [--] [programfile] [arguments]");
    println!();
    println!("{C}  ── EXECUTION ──────────────────────────────────────────{N}");
    println!("  'CODE'                 {G}//{N} Inline code — no -e needed if arg looks like code");
    println!("  -e CODE                {G}//{N} Explicit inline (required with -n/-p/-l/-a)");
    println!("  -E CODE                {G}//{N} Like -e, but enables all optional features");
    println!("  --script               {G}//{N} Force arg to be a file (skip code detection)");
    println!("  -c                     {G}//{N} Check syntax only (parse; no compile/run)");
    println!("  --lint / --check       {G}//{N} Parse + compile bytecode without running");
    println!(
        "  --disasm / --disassemble {G}//{N} Print bytecode disassembly to stderr before VM run"
    );
    println!("  --ast                  {G}//{N} Dump parsed AST as JSON and exit (no execution)");
    println!("  --fmt                  {G}//{N} Pretty-print parsed Perl to stdout and exit");
    println!(
        "  --explain CODE         {G}//{N} Print expanded hint for an error code (e.g. E0001) and exit"
    );
    println!(
        "  --profile              {G}//{N} Wall-clock profile stderr (VM op lines; flamegraph-ready)"
    );
    println!(
        "  --flame                {G}//{N} Flamegraph: terminal bars (TTY) or SVG (piped to file)"
    );
    println!("  --no-jit               {G}//{N} Disable Cranelift JIT (bytecode interpreter only)");
    println!(
        "  --compat               {G}//{N} Perl 5 strict-compat: disable all stryke extensions"
    );
    println!("  -d[t][:MOD]            {G}//{N} Run program under debugger or module Devel::MOD");
    println!("  -D[number/letters]     {G}//{N} Set debugging flags");
    println!("  -u                     {G}//{N} Dump core after parsing program");
    println!("{C}  ── INPUT PROCESSING ─────────────────────────────────{N}");
    println!("  -n                     {G}//{N} Assume \"while (<>) {{...}}\" loop around program");
    println!("  -p                     {G}//{N} Like -n but print line also, like sed");
    println!("  -a                     {G}//{N} Autosplit mode (splits $_ into @F)");
    println!("  -F/pattern/            {G}//{N} split() pattern for -a switch");
    println!("  -l[octnum]             {G}//{N} Enable line ending processing");
    println!("  -0[octal]              {G}//{N} Specify record separator (\\0 if no arg)");
    println!("  -g                     {G}//{N} Slurp all input at once (alias for -0777)");
    println!("  -i[extension]          {G}//{N} Edit <> files in place (backup if ext supplied; multiple files in parallel)");
    println!("{C}  ── MODULES & PATHS ──────────────────────────────────{N}");
    println!("  -M MODULE              {G}//{N} Execute \"use module...\" before program");
    println!(
        "  -m MODULE              {G}//{N} Execute \"use module ()\" before program (no import)"
    );
    println!("  -I DIRECTORY           {G}//{N} Specify @INC directory (several allowed)");
    println!("  -f                     {G}//{N} Don't do $sitelib/sitecustomize.pl at startup");
    println!("  -S                     {G}//{N} Look for programfile using PATH");
    println!("  -x[directory]          {G}//{N} Ignore text before #!perl line");
    println!("{C}  ── UNICODE & SAFETY ─────────────────────────────────{N}");
    println!("  -C[number/list]        {G}//{N} Enable listed Unicode features");
    println!("  -t                     {G}//{N} Enable tainting warnings");
    println!("  -T                     {G}//{N} Enable tainting checks");
    println!("  -U                     {G}//{N} Allow unsafe operations");
    println!("  -s                     {G}//{N} Enable switch parsing for programfile args");
    println!("{C}  ── WARNINGS ─────────────────────────────────────────{N}");
    println!("  -w                     {G}//{N} Enable many useful warnings");
    println!("  -W                     {G}//{N} Enable all warnings");
    println!("  -X                     {G}//{N} Disable all warnings");
    println!("{C}  ── INFO ─────────────────────────────────────────────{N}");
    println!("  -v                     {G}//{N} Print version, patchlevel and license");
    println!("  -V[:configvar]         {G}//{N} Print configuration summary");
    println!("  -h, --help             {G}//{N} Print help");
    println!("{C}  ── TOOLCHAIN ─────────────────────────────────────────{N}");
    println!(
        "  --lsp                  {G}//{N} Language Server (JSON-RPC on stdio); must be the only arg after {bin}"
    );
    println!(
        "  build SCRIPT [-o OUT]  {G}//{N} AOT: copy this binary with SCRIPT embedded (standalone exe)"
    );
    println!("  docs [TOPIC]           {G}//{N} Built-in docs (stryke docs pmap, stryke docs |>, stryke docs)");
    println!(
        "  serve [PORT] [SCRIPT]  {G}//{N} HTTP server (stryke serve, stryke serve 8080 app.stk)"
    );
    println!(
        "  --remote-worker        {G}//{N} Persistent cluster worker (stdio); only arg after {bin}"
    );
    println!(
        "  --remote-worker-v1     {G}//{N} Legacy one-shot worker (stdio); only arg after {bin}"
    );
    if matches!(bin, "stryke" | "st") {
        println!(
            "  (no switches, TTY stdin) {G}//{N} Interactive REPL (readline; exit with quit or EOF)"
        );
    }
    println!("{C}  ── PARALLEL EXTENSIONS (stryke) ─────────────────────{N}");
    println!("  -j N                   {G}//{N} Set number of parallel threads (rayon)");
    println!(
        "  pmap  {{BLOCK}} @list [, progress => EXPR] {G}//{N} Parallel map; optional stderr progress bar"
    );
    println!(
        "  pmap_chunked N {{BLOCK}} @list [, progress => EXPR] {G}//{N} Parallel map in batches of N items per thread"
    );
    println!(
        "  pcache {{BLOCK}} @list [, progress => EXPR] {G}//{N} Parallel memoize (key = stringified topic)"
    );
    println!(
        "  par_lines PATH, CODE [, progress => EXPR] {G}//{N} mmap + parallel line scan (tree-walker)"
    );
    println!(
        "  par_walk PATH, CODE [, progress => EXPR] {G}//{N} parallel recursive dir walk; topic is each path"
    );
    println!(
        "  par_sed PATTERN, REPLACEMENT, FILES... [, progress => EXPR] {G}//{N} parallel in-place regex replace per file (g)"
    );
    println!(
        "  pipeline @list ->filter/map/take/collect {G}//{N} Lazy iterator (runs on collect); chain ->pmap/pgrep/pfor/pmap_chunked/psort/pcache/preduce/… like top-level p*"
    );
    println!(
        "  par_pipeline @list same chain; filter/map parallel on collect (order kept); par_pipeline(source=>…,stages=>…,workers=>…) channel stages"
    );
    println!(
        "  async {{BLOCK}}           {G}//{N} Run block on a worker thread; returns a task handle"
    );
    println!("  spawn {{BLOCK}}           {G}//{N} Same as async (Rust-style); join with await");
    println!("  await EXPR                {G}//{N} Join async task or pass through non-task value");
    println!(
        "  pgrep {{BLOCK}} @list [, progress => EXPR] {G}//{N} Parallel grep across all cores"
    );
    println!(
        "  pfor  {{BLOCK}} @list [, progress => EXPR] {G}//{N} Parallel foreach across all cores"
    );
    println!(
        "  psort {{BLOCK}} @list [, progress => EXPR] {G}//{N} Parallel sort across all cores"
    );
    println!(
        "  @list |> reduce {{BLOCK}}   {G}//{N} Sequential left fold ($a accum, $b next element); also reduce {{BLOCK}} @list"
    );
    println!(
        "  @list |> preduce {{BLOCK}} [, progress => EXPR] {G}//{N} Parallel tree fold (rayon; associative ops only); also preduce {{BLOCK}} @list"
    );
    println!(
        "  @list |> preduce_init EXPR, {{BLOCK}} [, progress => EXPR] {G}//{N} Parallel fold with identity; also preduce_init EXPR, {{BLOCK}} @list"
    );
    println!(
        "  @list |> pmap_reduce {{MAP}} {{REDUCE}} [, progress => EXPR] {G}//{N} Fused parallel map + tree reduce; also pmap_reduce {{MAP}} {{REDUCE}} @list"
    );
    println!(
        "  fan [N] {{BLOCK}} [, progress => EXPR]  {G}//{N} Execute BLOCK N times (default N = rayon pool; $_ = index); progress may follow }} without a comma"
    );
    println!(
        "  fan_cap [N] {{BLOCK}} [, progress => EXPR]  {G}//{N} Like fan; returns list of block return values (index order)"
    );
    println!("{C}  ── TYPING (stryke) ───────────────────────────────────{N}");
    println!(
        "  typed my \\$x : Int|Str|Float  {G}//{N} Optional scalar types; runtime checks on assign"
    );
    println!(
        "  fn (\\$a: Int, \\$b: Str) {{}}   {G}//{N} Typed sub params; runtime checks on call"
    );
    println!("{C}  ── SERIALIZATION (stryke) ───────────────────────────────{N}");
    println!(
        "  str \\$val / stringify \\$val  {G}//{N} Convert any value to parseable stryke literal"
    );
    println!("  eval str \\$fn              {G}//{N} Round-trip: serialize + deserialize coderefs");
    println!("{C}  ── POSITIONAL ─────────────────────────────────────────{N}");
    println!("  [programfile]          {G}//{N} Perl script to execute");
    println!("  [arguments]            {G}//{N} Arguments passed to script (@ARGV)");
    println!();
    println!();
    println!("{C}  ── SYSTEM ─────────────────────────────────────────{N}");
    println!("{M}  v{version} {N}// {Y}(c) MenkeTechnologies{N}");
    println!("{M}  There is more than one way to do it — in parallel.{N}");
    println!("{Y}  >>> PARSE. EXECUTE. PARALLELIZE. OWN YOUR CORES. <<<{N}");
    println!("{C} ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░{N}");
}

/// `-M` / `-m` prelude prepended to each program line (shared with REPL).
pub(crate) fn module_prelude(cli: &Cli) -> String {
    let mut full_code = String::new();
    for module in &cli.use_module {
        if let Some((mod_name, args)) = module.split_once('=') {
            full_code.push_str(&format!(
                "use {} qw({});\n",
                mod_name,
                args.replace(',', " ")
            ));
        } else {
            full_code.push_str(&format!("use {};\n", module));
        }
    }
    for module in &cli.use_module_no_import {
        if let Some(rest) = module.strip_prefix('-') {
            full_code.push_str(&format!("no {};\n", rest));
        } else {
            full_code.push_str(&format!("use {} ();\n", module));
        }
    }
    full_code
}

/// Like `perl`, arguments after the script (or after `-e` / `-E` code) are passed to the program
/// unchanged, including tokens that look like long options (`--regex`, …). Clap rejects unknown
/// `--flags` unless they appear after `--`; we find the Perl-consistent split and insert `--`
/// before the first script argument when needed.
fn parse_cli_prelude(args: &[String]) -> Option<Cli> {
    if args.len() <= 1 {
        return None;
    }
    // User already used `--` as the end-of-options delimiter; let clap handle it.
    if args[1..].iter().any(|s| s == "--") {
        return None;
    }
    for k in (1..=args.len()).rev() {
        let trial: Vec<String> = if k == args.len() {
            args.to_vec()
        } else {
            let mut t = args[..k].to_vec();
            t.push("--".to_string());
            t.extend(args[k..].iter().cloned());
            t
        };
        let Some(cli) = Cli::try_parse_from(&trial).ok() else {
            continue;
        };
        if cli.args.as_slice() == args[k..].as_ref() {
            return Some(cli);
        }
    }
    None
}

/// When `-e` / `-E` supplies the program, the optional positional `SCRIPT` is actually the first
/// `@ARGV` element (Perl semantics), not a second script path. Fold it into `args`.
fn normalize_argv_after_dash_e(cli: &mut Cli) {
    if (!cli.execute.is_empty() || !cli.execute_features.is_empty()) && cli.script.is_some() {
        let mut v = vec![cli.script.take().unwrap()];
        v.append(&mut cli.args);
        cli.args = v;
    }
}

/// Unique temp path next to `target` for atomic in-place replace (`rename` into place).
fn adjacent_temp_path(target: &Path) -> PathBuf {
    let dir = target.parent().unwrap_or_else(|| Path::new("."));
    let name = target
        .file_name()
        .map(|s| s.to_string_lossy().into_owned())
        .unwrap_or_else(|| "file".to_string());
    let rnd: u32 = rand::thread_rng().gen();
    dir.join(format!("{name}.stryke-tmp-{rnd}"))
}

/// Write `new_content` to `path` in place; optional backup `path` + `inplace_edit` (Perl `$^I`).
fn commit_in_place_edit(path: &Path, inplace_edit: &str, new_content: &str) -> std::io::Result<()> {
    let tmp = adjacent_temp_path(path);
    std::fs::write(&tmp, new_content)?;
    if !inplace_edit.is_empty() {
        let backup = PathBuf::from(format!("{}{}", path.display(), inplace_edit));
        let _ = std::fs::remove_file(&backup);
        std::fs::rename(path, &backup)?;
    }
    std::fs::rename(&tmp, path)?;
    Ok(())
}

/// `BufRead::lines()` strips the terminator; Perl’s `<>` leaves it in `$_` unless **`-l`** is set,
/// in which case Perl **chomps** each record. Match that so `print` + `$\` does not double newlines.
fn line_mode_input_record(cli: &Cli, l: String) -> String {
    if cli.line_ending.is_some() {
        l
    } else {
        format!("{}\n", l)
    }
}

/// Content of one `read_line` result, without the trailing `\n` / `\r\n` / `\r` (same string `lines()`
/// would have yielded for that physical line).
fn line_content_from_stdin_read_line(buf: &str) -> String {
    buf.strip_suffix("\r\n")
        .or_else(|| buf.strip_suffix('\n'))
        .or_else(|| buf.strip_suffix('\r'))
        .unwrap_or(buf)
        .to_string()
}

/// `-n` / `-p` input loop: `@ARGV` files when non-empty, else stdin; `-i` rewrites named files.
fn run_line_mode_loop(
    cli: &Cli,
    interp: &mut Interpreter,
    program: &Program,
    slurp: bool,
) -> Result<(), PerlError> {
    let inplace = cli.inplace.is_some();
    let use_argv_files = !interp.argv.is_empty();
    let suppressed_stdout_for_inplace = inplace && use_argv_files;
    let print_to_stdout = cli.print_mode && !suppressed_stdout_for_inplace;
    // With `-i` and named files, per-line print is suppressed; files are independent, so rayon can
    // process them in parallel (stock `perl` processes `@ARGV` files sequentially).
    let parallel_argv_inplace = inplace && use_argv_files;

    if slurp {
        if use_argv_files {
            if parallel_argv_inplace {
                let template = Mutex::new(interp.line_mode_worker_clone());
                let paths = interp.argv.clone();
                paths.into_par_iter().try_for_each(|path| {
                    let mut local = template
                        .lock()
                        .expect("line-mode template mutex poisoned")
                        .line_mode_worker_clone();
                    local.line_number = 0;
                    local.argv_current_file = path.clone();
                    let content = read_file_text_perl_compat(&path).map_err(|e| {
                        PerlError::new(
                            ErrorKind::IO,
                            format!("Can't open {}: {}", path, e),
                            0,
                            "-e",
                        )
                    })?;
                    if let Some(output) = local.process_line(&content, program, true)? {
                        commit_in_place_edit(Path::new(&path), &local.inplace_edit, &output)
                            .map_err(|e| PerlError::new(ErrorKind::IO, e.to_string(), 0, "-e"))?;
                    }
                    Ok(())
                })?;
            } else {
                for path in interp.argv.clone() {
                    interp.line_number = 0;
                    interp.argv_current_file = path.clone();
                    let content = read_file_text_perl_compat(&path).map_err(|e| {
                        PerlError::new(
                            ErrorKind::IO,
                            format!("Can't open {}: {}", path, e),
                            0,
                            "-e",
                        )
                    })?;
                    if let Some(output) = interp.process_line(&content, program, true)? {
                        if inplace {
                            commit_in_place_edit(Path::new(&path), &interp.inplace_edit, &output)
                                .map_err(|e| {
                                    PerlError::new(ErrorKind::IO, e.to_string(), 0, "-e")
                                })?;
                        } else if cli.print_mode {
                            print!("{}", output);
                            let _ = io::stdout().flush();
                        }
                    }
                }
            }
        } else {
            let mut input = String::new();
            let mut raw = Vec::new();
            let _ = IoRead::read_to_end(&mut io::stdin(), &mut raw);
            input.push_str(&decode_utf8_or_latin1(&raw));
            if let Some(output) = interp.process_line(&input, program, true)? {
                if print_to_stdout {
                    print!("{}", output);
                    let _ = io::stdout().flush();
                }
            }
        }
        return Ok(());
    }

    if use_argv_files {
        if parallel_argv_inplace {
            let template = Mutex::new(interp.line_mode_worker_clone());
            let paths = interp.argv.clone();
            paths.into_par_iter().try_for_each(|path| {
                let mut local = template
                    .lock()
                    .expect("line-mode template mutex poisoned")
                    .line_mode_worker_clone();
                local.line_number = 0;
                local.argv_current_file = path.clone();
                let file = File::open(&path).map_err(|e| {
                    PerlError::new(
                        ErrorKind::IO,
                        format!("Can't open {}: {}", path, e),
                        0,
                        "-e",
                    )
                })?;
                let mut reader = BufReader::new(file);
                let mut accumulated = String::new();
                let mut pending: Option<String> = None;
                loop {
                    let l = if let Some(s) = pending.take() {
                        s
                    } else {
                        match read_logical_line_perl_compat(&mut reader).map_err(|e| {
                            PerlError::new(
                                ErrorKind::IO,
                                format!("Error reading {}: {}", path, e),
                                0,
                                "-e",
                            )
                        })? {
                            None => break,
                            Some(s) => s,
                        }
                    };
                    let is_last = match read_logical_line_perl_compat(&mut reader).map_err(|e| {
                        PerlError::new(
                            ErrorKind::IO,
                            format!("Error reading {}: {}", path, e),
                            0,
                            "-e",
                        )
                    })? {
                        None => true,
                        Some(next) => {
                            pending = Some(next);
                            false
                        }
                    };
                    let input = line_mode_input_record(cli, l);
                    if let Some(output) = local.process_line(&input, program, is_last)? {
                        accumulated.push_str(&output);
                    }
                }
                commit_in_place_edit(Path::new(&path), &local.inplace_edit, &accumulated)
                    .map_err(|e| PerlError::new(ErrorKind::IO, e.to_string(), 0, "-e"))?;
                Ok(())
            })?;
        } else {
            for path in interp.argv.clone() {
                interp.line_number = 0;
                interp.argv_current_file = path.clone();
                let file = File::open(&path).map_err(|e| {
                    PerlError::new(
                        ErrorKind::IO,
                        format!("Can't open {}: {}", path, e),
                        0,
                        "-e",
                    )
                })?;
                let mut reader = BufReader::new(file);
                let mut accumulated = String::new();
                let mut pending: Option<String> = None;
                loop {
                    let l = if let Some(s) = pending.take() {
                        s
                    } else {
                        match read_logical_line_perl_compat(&mut reader).map_err(|e| {
                            PerlError::new(
                                ErrorKind::IO,
                                format!("Error reading {}: {}", path, e),
                                0,
                                "-e",
                            )
                        })? {
                            None => break,
                            Some(s) => s,
                        }
                    };
                    let is_last = match read_logical_line_perl_compat(&mut reader).map_err(|e| {
                        PerlError::new(
                            ErrorKind::IO,
                            format!("Error reading {}: {}", path, e),
                            0,
                            "-e",
                        )
                    })? {
                        None => true,
                        Some(next) => {
                            pending = Some(next);
                            false
                        }
                    };
                    let input = line_mode_input_record(cli, l);
                    if let Some(output) = interp.process_line(&input, program, is_last)? {
                        if print_to_stdout {
                            print!("{}", output);
                            let _ = io::stdout().flush();
                        }
                        if inplace {
                            accumulated.push_str(&output);
                        }
                    }
                }
                if inplace {
                    commit_in_place_edit(Path::new(&path), &interp.inplace_edit, &accumulated)
                        .map_err(|e| PerlError::new(ErrorKind::IO, e.to_string(), 0, "-e"))?;
                }
            }
        }
    } else {
        // Read stdin with `read_line` and **do not** hold `StdinLock` across `process_line` (the body
        // may call `<>` / `readline`, which also locks stdin — exclusive lock would deadlock).
        //
        // Peek-reading the next line to set `is_last` for `eof` would consume that line from the
        // kernel buffer; push it onto [`Interpreter::line_mode_stdin_pending`] so body `<>` reads it
        // first (Perl shares one fd between the implicit `while (<>)` and inner `readline`).
        interp.line_mode_stdin_pending.clear();
        loop {
            let mut current = String::new();
            let n = if let Some(queued) = interp.line_mode_stdin_pending.pop_front() {
                current = queued;
                current.len()
            } else {
                let mut lock = io::stdin().lock();
                read_line_perl_compat(&mut lock, &mut current).map_err(|e| {
                    PerlError::new(ErrorKind::IO, format!("Error reading stdin: {e}"), 0, "-e")
                })?
            };
            if n == 0 {
                break;
            }
            let (is_last, peek_line) = {
                let mut lock = io::stdin().lock();
                let mut peek = String::new();
                let n = read_line_perl_compat(&mut lock, &mut peek).map_err(|e| {
                    PerlError::new(ErrorKind::IO, format!("Error reading stdin: {e}"), 0, "-e")
                })?;
                if n == 0 {
                    (true, None)
                } else {
                    (false, Some(peek))
                }
            };
            if let Some(pl) = peek_line {
                interp.line_mode_stdin_pending.push_back(pl);
            }
            let l = line_content_from_stdin_read_line(&current);
            let input = line_mode_input_record(cli, l);
            match interp.process_line(&input, program, is_last) {
                Ok(Some(output)) => {
                    if print_to_stdout {
                        print!("{}", output);
                        let _ = io::stdout().flush();
                    }
                }
                Ok(None) => {}
                Err(e) => return Err(e),
            }
        }
    }
    Ok(())
}

pub(crate) fn configure_interpreter(cli: &Cli, interp: &mut Interpreter, filename: &str) {
    interp.set_file(filename);
    interp.warnings = (cli.warnings || cli.all_warnings) && !cli.no_warnings;
    interp.auto_split = cli.auto_split;
    interp.field_separator = cli.field_separator.clone();
    interp.program_name = filename.to_string();

    if let Some(ref sep) = cli.input_separator {
        match sep.as_deref() {
            None | Some("") => interp.irs = Some("\0".to_string()),
            Some("777") => interp.irs = None, // perl `-0777` enables slurp mode
            Some(oct_str) => {
                if let Ok(val) = u32::from_str_radix(oct_str, 8) {
                    if let Some(ch) = char::from_u32(val) {
                        interp.irs = Some(ch.to_string());
                    }
                }
            }
        }
    }

    if let Some(ref octnum) = cli.line_ending {
        match octnum.as_deref() {
            None | Some("") => {
                interp.ors = "\n".to_string();
            }
            Some(oct_str) => {
                if let Ok(val) = u32::from_str_radix(oct_str, 8) {
                    if let Some(ch) = char::from_u32(val) {
                        interp.ors = ch.to_string();
                    }
                }
            }
        }
    }

    if (cli.taint_check || cli.taint_warn) && cli.warnings {
        eprintln!("stryke: taint mode acknowledged but not enforced");
    }

    if let Some(ref ext_opt) = cli.inplace {
        interp.inplace_edit = ext_opt.clone().unwrap_or_default();
    }

    // Trailing arguments become `@ARGV` for `perl script.pl …` and for `perl -e '…' …` (Perl
    // compatibility).
    let mut argv: Vec<String> =
        if cli.script.is_some() || !cli.execute.is_empty() || !cli.execute_features.is_empty() {
            cli.args.clone()
        } else {
            Vec::new()
        };

    if cli.switch_parsing {
        let mut switches_done = false;
        let mut remaining = Vec::new();
        for arg in &argv {
            if switches_done || !arg.starts_with('-') || arg == "--" {
                if arg == "--" {
                    switches_done = true;
                } else {
                    remaining.push(arg.clone());
                }
            } else {
                let switch = &arg[1..];
                if let Some((name, val)) = switch.split_once('=') {
                    let _ = interp
                        .scope
                        .set_scalar(name, stryke::value::PerlValue::string(val.to_string()));
                } else {
                    let _ = interp
                        .scope
                        .set_scalar(switch, stryke::value::PerlValue::integer(1));
                }
            }
        }
        argv = remaining;
    }

    interp.argv = argv.clone();
    interp.scope.declare_array(
        "ARGV",
        argv.into_iter()
            .map(stryke::value::PerlValue::string)
            .collect(),
    );

    // Order: `-I`, in-tree `vendor/perl` (pure-Perl List::Util, …), system `perl`’s @INC, script
    // dir, `STRYKE_INC`, then `.` (deduped).
    let mut inc_paths: Vec<String> = cli.include.clone();
    let vendor = stryke::vendor_perl_inc_path();
    if vendor.is_dir() {
        stryke::perl_inc::push_unique_string_paths(
            &mut inc_paths,
            vec![vendor.to_string_lossy().into_owned()],
        );
    }
    stryke::perl_inc::push_unique_string_paths(
        &mut inc_paths,
        stryke::perl_inc::paths_from_system_perl(),
    );
    if filename != "-e" && filename != "-" && filename != "repl" {
        if let Some(parent) = std::path::Path::new(filename).parent() {
            if !parent.as_os_str().is_empty() {
                stryke::perl_inc::push_unique_string_paths(
                    &mut inc_paths,
                    vec![parent.to_string_lossy().into_owned()],
                );
            }
        }
    }
    if let Ok(extra) = std::env::var("STRYKE_INC") {
        let extra: Vec<String> = std::env::split_paths(&extra)
            .map(|p| p.to_string_lossy().into_owned())
            .collect();
        stryke::perl_inc::push_unique_string_paths(&mut inc_paths, extra);
    }
    stryke::perl_inc::push_unique_string_paths(&mut inc_paths, vec![".".to_string()]);
    let inc_dirs: Vec<stryke::value::PerlValue> = inc_paths
        .into_iter()
        .map(stryke::value::PerlValue::string)
        .collect();
    interp.scope.declare_array("INC", inc_dirs);

    if cli.debugger.is_some() {
        eprintln!("stryke: debugger not yet implemented, running normally");
    }
}

/// Emit profiler output.
///
/// `--flame` + piped stdout → SVG flamegraph to saved fd.
/// `--flame` + TTY stdout  → colored terminal bars to stderr.
/// `--profile` (no flame)  → plain text report to stderr.
fn emit_profiler_report(
    p: &mut stryke::profiler::Profiler,
    flame_out: &Option<File>,
    flame_tty: bool,
) {
    if let Some(f) = flame_out {
        // stdout was piped — write SVG to the saved fd
        let mut w = io::BufWriter::new(f);
        if let Err(e) = p.render_flame_svg(&mut w) {
            eprintln!("stryke --flame: {}", e);
        }
    } else if flame_tty {
        // stdout is a TTY — render colored bars to stderr
        p.render_flame_tty();
    } else {
        // plain --profile
        p.print_report();
    }
}

fn main() {
    // AOT: if the running binary carries an embedded script trailer, execute it and
    // exit. Bypasses clap, flags, REPL — the embedded binary behaves like a plain native
    // program: all command-line args become `@ARGV` for the embedded script. The probe
    // costs one file open + one 32-byte read (~50 µs) on the no-trailer path.
    if let Ok(exe) = std::env::current_exe() {
        if let Some(embedded) = stryke::aot::try_load_embedded(&exe) {
            let argv: Vec<String> = std::env::args().skip(1).collect();
            process::exit(run_embedded_script(embedded, argv));
        }
    }

    let args = expand_perl_bundled_argv(std::env::args().collect());

    if args.len() == 2 && args[1] == "--remote-worker" {
        // Persistent v3 session loop: HELLO → SESSION_INIT → many JOBs → SHUTDOWN.
        // The basic v1 one-shot loop is still reachable via `--remote-worker-v1` for the
        // round-trip integration test.
        process::exit(stryke::remote_wire::run_remote_worker_session());
    }
    if args.len() == 2 && args[1] == "--remote-worker-v1" {
        process::exit(stryke::remote_wire::run_remote_worker_stdio());
    }

    if args.len() == 2 && args[1] == "--lsp" {
        process::exit(stryke::run_lsp_stdio());
    }

    // `stryke build SCRIPT -o OUT` subcommand: intercept before clap so `build` does not have
    // to be added to the main `Cli` struct (keeping the perl-compatible flag surface clean).
    if args.len() >= 2 && args[1] == "build" {
        process::exit(run_build_subcommand(&args[2..]));
    }

    // `stryke convert FILE...` subcommand: convert Perl source to stryke syntax with |> pipes.
    if args.len() >= 2 && args[1] == "convert" {
        process::exit(run_convert_subcommand(&args[2..]));
    }

    // `stryke deconvert FILE...` subcommand: convert stryke .stk files back to standard Perl .pl syntax.
    if args.len() >= 2 && args[1] == "deconvert" {
        process::exit(run_deconvert_subcommand(&args[2..]));
    }

    // `stryke docs [TOPIC]` subcommand: built-in documentation browser.
    if args.len() >= 2 && args[1] == "docs" {
        process::exit(run_doc_subcommand(&args[2..]));
    }

    // `stryke serve PORT SCRIPT` or `stryke serve PORT -e CODE` subcommand.
    if args.len() >= 2 && args[1] == "serve" {
        process::exit(run_serve_subcommand(&args[2..]));
    }

    // Fast path: `stryke SCRIPT [ARGS...]` with no dashes anywhere — the common case, and
    // clap parsing is the dominant term on `print "hello\n"` (it knocks ~1ms off the
    // startup bench). We can't bypass clap when any flag is present, so fall through to the
    // full parser in that case.
    // Exception: `->>`, `->`, `~>` look like flags but are actually threading operators for
    // inline code — detect via `looks_like_code` and treat as script.
    let arg1_is_code_not_flag =
        args.len() >= 2 && args[1].starts_with('-') && looks_like_code(&args[1]);
    let mut cli = if args.len() >= 2
        && (!args[1].starts_with('-') || arg1_is_code_not_flag)
        && !args[1].is_empty()
        && args[2..].iter().all(|a| !a.starts_with('-'))
    {
        Cli {
            script: Some(args[1].clone()),
            args: if args.len() > 2 {
                args[2..].to_vec()
            } else {
                Vec::new()
            },
            ..Default::default()
        }
    } else {
        parse_cli_prelude(&args).unwrap_or_else(|| Cli::parse_from(&args))
    };
    normalize_argv_after_dash_e(&mut cli);

    // Set global compat-mode flag before any parsing happens.
    if cli.compat {
        stryke::set_compat_mode(true);
    }

    if cli.help {
        print_cyberpunk_help();
        return;
    }

    if cli.show_version {
        println!(
            "This is stryke v{} — A highly parallel Perl 5 interpreter (Rust)\n",
            env!("CARGO_PKG_VERSION")
        );
        println!("Built with rayon for parallel map/grep/for/sort");
        println!(
            "Threads available: {}\n",
            std::thread::available_parallelism()
                .map(|n| n.get())
                .unwrap_or(1)
        );
        println!(
            "Copyright 2026 MenkeTechnologies. Licensed under MIT.\n\n\
             This is free software; you can redistribute it and/or modify it\n\
             under the terms of the MIT License."
        );
        return;
    }

    if let Some(ref configvar) = cli.show_config {
        print_config(configvar.as_deref());
        return;
    }

    if let Some(code) = &cli.explain {
        match stryke::error::explain_error(code) {
            Some(text) => println!("{}", text),
            None => {
                eprintln!("stryke: unknown explain code {:?}", code);
                process::exit(1);
            }
        }
        return;
    }

    // Configure rayon thread pool
    if let Some(n) = cli.threads {
        rayon::ThreadPoolBuilder::new()
            .num_threads(n)
            .build_global()
            .ok();
    }

    let is_repl = matches!(env!("CARGO_BIN_NAME"), "stryke" | "st")
        && cli.script.is_none()
        && cli.execute.is_empty()
        && cli.execute_features.is_empty()
        && !cli.line_mode
        && !cli.print_mode
        && !cli.check_only
        && !cli.lint
        && !cli.disasm
        && !cli.dump_ast
        && !cli.format_source
        && !cli.profile
        && !cli.flame
        && !cli.dump_core
        && cli.explain.is_none()
        && io::stdin().is_terminal();

    if is_repl {
        repl::run(&cli);
        return;
    }

    // Determine slurp mode
    let slurp = cli.slurp
        || cli
            .input_separator
            .as_ref()
            .is_some_and(|v| v.as_deref() == Some("777"));

    // Build the source code (`__DATA__` is split out before shebang / `-x` handling)
    let (raw_script, filename): (String, String) = if !cli.execute.is_empty() {
        (cli.execute.join("; "), "-e".to_string())
    } else if !cli.execute_features.is_empty() {
        (cli.execute_features.join("; "), "-E".to_string())
    } else if let Some(ref script) = cli.script {
        if script == "-" {
            // Like `perl -`: program text from stdin (not a file named `-` in cwd).
            let mut code = Vec::new();
            let _ = IoRead::read_to_end(&mut io::stdin(), &mut code);
            let code = decode_utf8_or_latin1(&code);
            (code, "-".to_string())
        } else {
            let script_path = if cli.path_lookup {
                find_in_path(script).unwrap_or_else(|| script.clone())
            } else {
                script.clone()
            };
            match read_file_text_perl_compat(&script_path) {
                Ok(content) => (content, script_path),
                Err(_) if !cli.force_script && looks_like_code(&script_path) => {
                    // One-liner-first: `stryke 'p 1+2'` works without `-e`
                    (script_path, "-e".to_string())
                }
                Err(e) => {
                    eprintln!("Can't open perl script \"{}\": {}", script_path, e);
                    process::exit(2);
                }
            }
        }
    } else if cli.line_mode || cli.print_mode {
        (String::new(), "-".to_string())
    } else {
        let mut code = Vec::new();
        // Match `perl`: program from stdin is the full script (pipe, heredoc, or terminal until EOF).
        let _ = IoRead::read_to_end(&mut io::stdin(), &mut code);
        let code = decode_utf8_or_latin1(&code);
        (code, "-".to_string())
    };

    let (program_text, data_opt) = stryke::data_section::split_data_section(&raw_script);
    let code = strip_shebang_and_extract(&program_text, cli.extract.is_some());

    let mut full_code = module_prelude(&cli);
    full_code.push_str(&code);

    // `.pec` bytecode cache fast path — skip parse AND compile on warm starts.
    //
    // Keyed on (crate version, filename, full source including `-M` prelude). Enabled by
    // `STRYKE_BC_CACHE=1` (opt-in for v1 — see [`stryke::pec::cache_enabled`]). On a hit,
    // the [`stryke::pec::PecBundle`] carries both the AST `Program` and the compiled
    // `Chunk`; we hand the chunk to the interpreter via a sideband field that
    // [`stryke::try_vm_execute`] consumes. On a miss, we parse normally and stash the
    // fingerprint so the try-VM path persists the freshly-compiled chunk after run.
    //
    // **Disabled for `-e` / `-E` one-liners.** Measured: warm `.pec` is ~2-3× *slower* than
    // cold for tiny scripts because the deserialize cost (~1-2 ms for fs read + zstd decode
    // + bincode) dominates the parse+compile work it replaces (~500 µs). One-liners would
    // also pollute the cache directory with one entry per unique `-e` invocation, with no
    // GC in v1. The break-even is around 1000+ lines, so file-based scripts only.
    let is_one_liner = !cli.execute.is_empty() || !cli.execute_features.is_empty();
    let pec_on = stryke::pec::cache_enabled()
        && !cli.line_mode
        && !cli.print_mode
        && !cli.lint
        && !cli.check_only
        && !cli.dump_ast
        && !cli.format_source
        && !cli.profile
        && !cli.flame
        && !is_one_liner
        && !filename.is_empty();
    let pec_fp_opt: Option<[u8; 32]> = if pec_on {
        // `strict_vars` enters the fingerprint as `false` here; an eventual [`PecBundle::strict_vars`]
        // mismatch at load time is treated as a miss (see [`stryke::pec::try_load`]), so two strict
        // modes may collide in one slot without producing wrong answers.
        Some(stryke::pec::source_fingerprint(
            false, &filename, &full_code,
        ))
    } else {
        None
    };
    let cached_bundle = pec_fp_opt
        .as_ref()
        .and_then(|fp| stryke::pec::try_load(fp, false).ok().flatten());

    let (program, pec_precompiled) = if let Some(bundle) = cached_bundle {
        (bundle.program, Some(bundle.chunk))
    } else {
        let parsed = match stryke::parse_with_file(&full_code, &filename) {
            Ok(p) => p,
            Err(e) => {
                eprintln!("{}", e);
                process::exit(255);
            }
        };
        (parsed, None)
    };

    if cli.dump_ast {
        match serde_json::to_string_pretty(&program) {
            Ok(json) => println!("{}", json),
            Err(e) => {
                eprintln!("stryke: failed to serialize AST to JSON: {}", e);
                process::exit(1);
            }
        }
        return;
    }

    if cli.format_source {
        // Use convert_program for clean stryke (.stk) syntax with pipes
        println!("{}", stryke::convert::convert_program(&program));
        return;
    }

    if cli.lint {
        let mut interp = Interpreter::new();
        if cli.no_jit {
            interp.vm_jit_enabled = false;
        }
        configure_interpreter(&cli, &mut interp, &filename);
        if let Some(data) = data_opt {
            interp.install_data_handle(data);
        }
        match stryke::lint_program(&program, &mut interp) {
            Ok(()) => {
                eprintln!("{} compile OK", filename);
                return;
            }
            Err(e) => {
                eprintln!("{}", e);
                process::exit(255);
            }
        }
    }

    if cli.check_only {
        eprintln!("{} syntax OK", filename);
        return;
    }

    if cli.dump_core {
        eprintln!("{} syntax OK (dump not supported)", filename);
        return;
    }

    let mut interp = Interpreter::new();
    if cli.no_jit {
        interp.vm_jit_enabled = false;
    }
    if cli.disasm {
        interp.disasm_bytecode = true;
    }
    if cli.profile || cli.flame {
        interp.profiler = Some(stryke::profiler::Profiler::new(filename.clone()));
    }
    // Hand the `.pec` sideband to the interpreter so `try_vm_execute` either runs the
    // pre-compiled chunk (cache hit) or saves the freshly-compiled one (cache miss).
    interp.pec_precompiled_chunk = pec_precompiled;
    interp.pec_cache_fingerprint = if pec_on { pec_fp_opt } else { None };
    configure_interpreter(&cli, &mut interp, &filename);
    if let Some(data) = data_opt {
        interp.install_data_handle(data);
    }

    // --flame: when stdout is piped to a file, save real stdout for the SVG and redirect
    // script output to stderr so `stryke --flame x.stk > flame.svg` captures a clean SVG.
    // When stdout is a TTY, skip the redirect — we'll render colored bars to stderr instead.
    let flame_is_tty = cli.flame && io::stdout().is_terminal();
    #[cfg(unix)]
    let flame_stdout: Option<File> = if cli.flame && !flame_is_tty {
        use std::os::unix::io::FromRawFd;
        let saved = unsafe { libc::dup(1) };
        if saved >= 0 {
            unsafe { libc::dup2(2, 1) };
            Some(unsafe { File::from_raw_fd(saved) })
        } else {
            None
        }
    } else {
        None
    };
    #[cfg(not(unix))]
    let flame_stdout: Option<File> = None;

    // Line processing mode (-n / -p)
    if cli.line_mode || cli.print_mode {
        if cli.line_ending.is_some() {
            interp.ors = "\n".to_string();
        }

        // Prelude only: subs / `use` / BEGIN … INIT — main runs per line in `process_line`, not here
        // (stock `perl` wraps `-e` in `while (<>) { … }`, so a bare `print` must not run before input).
        interp.line_mode_skip_main = true;
        if let Err(e) = interp.execute(&program) {
            interp.line_mode_skip_main = false;
            if let Some(mut p) = interp.profiler.take() {
                emit_profiler_report(&mut p, &flame_stdout, flame_is_tty);
            }
            if let ErrorKind::Exit(code) = e.kind {
                process::exit(code);
            }
            eprintln!("{}", e);
            process::exit(255);
        }
        interp.line_mode_skip_main = false;

        if let Err(e) = run_line_mode_loop(&cli, &mut interp, &program, slurp) {
            if let Some(mut p) = interp.profiler.take() {
                emit_profiler_report(&mut p, &flame_stdout, flame_is_tty);
            }
            if let ErrorKind::Exit(code) = e.kind {
                process::exit(code);
            }
            eprintln!("{}", e);
            process::exit(255);
        }
        if let Err(e) = interp.run_end_blocks() {
            if let Some(mut p) = interp.profiler.take() {
                emit_profiler_report(&mut p, &flame_stdout, flame_is_tty);
            }
            if let ErrorKind::Exit(code) = e.kind {
                process::exit(code);
            }
            eprintln!("{}", e);
            process::exit(255);
        }
        let _ = interp.run_global_teardown();
        if let Some(mut p) = interp.profiler.take() {
            emit_profiler_report(&mut p, &flame_stdout, flame_is_tty);
        }
    } else {
        // Normal execution
        match interp.execute(&program) {
            Ok(_) => {
                let _ = interp.run_global_teardown();
                let _ = io::stdout().flush();
                if let Some(mut p) = interp.profiler.take() {
                    emit_profiler_report(&mut p, &flame_stdout, flame_is_tty);
                }
            }
            Err(e) => match e.kind {
                ErrorKind::Exit(code) => {
                    if let Some(mut p) = interp.profiler.take() {
                        emit_profiler_report(&mut p, &flame_stdout, flame_is_tty);
                    }
                    process::exit(code);
                }
                ErrorKind::Die => {
                    if let Some(mut p) = interp.profiler.take() {
                        emit_profiler_report(&mut p, &flame_stdout, flame_is_tty);
                    }
                    eprint!("{}", e);
                    process::exit(255);
                }
                _ => {
                    if let Some(mut p) = interp.profiler.take() {
                        emit_profiler_report(&mut p, &flame_stdout, flame_is_tty);
                    }
                    eprintln!("{}", e);
                    process::exit(255);
                }
            },
        }
    }
}

/// Run an [`stryke::aot::EmbeddedScript`] as if it were the primary program. Minimal
/// `@INC` setup: current directory only — the AOT binary is meant to be self-contained, so
/// the target machine's `perl` (which may not exist) is not consulted. `-I` at build time
/// is not yet supported (v1); drop everything into the `rust { ... }` block instead.
fn run_embedded_script(embedded: stryke::aot::EmbeddedScript, argv: Vec<String>) -> i32 {
    // AOT binaries pick up the `.pec` bytecode cache for free when `STRYKE_BC_CACHE=1` —
    // the first run of a shipped binary parses and compiles the embedded source, then
    // every subsequent run reuses the cached chunk. Cache key includes the script name
    // embedded in the trailer, so two binaries with different embedded scripts will not
    // collide.
    let pec_on = stryke::pec::cache_enabled();
    let pec_fp = if pec_on {
        Some(stryke::pec::source_fingerprint(
            false,
            &embedded.name,
            &embedded.source,
        ))
    } else {
        None
    };
    let cached = pec_fp
        .as_ref()
        .and_then(|fp| stryke::pec::try_load(fp, false).ok().flatten());
    let (program, pec_precompiled) = if let Some(bundle) = cached {
        (bundle.program, Some(bundle.chunk))
    } else {
        let parsed = match stryke::parse_with_file(&embedded.source, &embedded.name) {
            Ok(p) => p,
            Err(e) => {
                eprintln!("{}", e);
                return 255;
            }
        };
        (parsed, None)
    };
    let mut interp = Interpreter::new();
    interp.set_file(&embedded.name);
    interp.program_name = embedded.name.clone();
    interp.argv = argv.clone();
    interp.scope.declare_array(
        "ARGV",
        argv.into_iter()
            .map(stryke::value::PerlValue::string)
            .collect(),
    );
    interp.scope.declare_array(
        "INC",
        vec![stryke::value::PerlValue::string(".".to_string())],
    );
    interp.pec_precompiled_chunk = pec_precompiled;
    interp.pec_cache_fingerprint = pec_fp;
    match interp.execute(&program) {
        Ok(_) => {
            let _ = interp.run_global_teardown();
            let _ = io::stdout().flush();
            0
        }
        Err(e) => match e.kind {
            ErrorKind::Exit(code) => code,
            ErrorKind::Die => {
                eprint!("{}", e);
                255
            }
            _ => {
                eprintln!("{}", e);
                255
            }
        },
    }
}

/// `stryke build SCRIPT [-o OUT]` — compile a Perl script into a standalone binary by
/// copying the currently-running `stryke` and appending a zstd-compressed source trailer.
/// The resulting file behaves as a native program: all CLI args go to the embedded script.
fn run_build_subcommand(args: &[String]) -> i32 {
    let mut script: Option<String> = None;
    let mut out: Option<String> = None;
    let mut i = 0usize;
    while i < args.len() {
        match args[i].as_str() {
            "-o" | "--output" => {
                i += 1;
                if i >= args.len() {
                    eprintln!("stryke build: -o requires an argument");
                    return 2;
                }
                out = Some(args[i].clone());
            }
            "-h" | "--help" => {
                println!("usage: stryke build SCRIPT [-o OUTPUT]");
                println!();
                println!(
                    "Compile a Perl script into a standalone executable binary. The output is"
                );
                println!(
                    "a copy of this stryke binary with the script source embedded as a compressed"
                );
                println!(
                    "trailer. `scp` the result to any compatible machine and run it directly —"
                );
                println!("no perl, no stryke, no @INC setup required.");
                println!();
                println!("Examples:");
                println!("  stryke build app.pl                     # → ./app");
                println!("  stryke build app.pl -o /usr/local/bin/app");
                return 0;
            }
            s if script.is_none() && !s.starts_with('-') => script = Some(s.to_string()),
            other => {
                eprintln!("stryke build: unknown argument: {}", other);
                eprintln!("usage: stryke build SCRIPT [-o OUTPUT]");
                return 2;
            }
        }
        i += 1;
    }
    let Some(script) = script else {
        eprintln!("stryke build: missing SCRIPT");
        eprintln!("usage: stryke build SCRIPT [-o OUTPUT]");
        return 2;
    };
    let script_path = PathBuf::from(&script);
    let out_path = PathBuf::from(out.unwrap_or_else(|| {
        script_path
            .file_stem()
            .map(|s| s.to_string_lossy().into_owned())
            .unwrap_or_else(|| "a.out".to_string())
    }));
    match stryke::aot::build(&script_path, &out_path) {
        Ok(p) => {
            eprintln!("stryke build: wrote {}", p.display());
            0
        }
        Err(e) => {
            eprintln!("{}", e);
            1
        }
    }
}

/// `stryke convert FILE...` — convert Perl source to idiomatic stryke syntax.
fn run_convert_subcommand(args: &[String]) -> i32 {
    let mut files: Vec<String> = Vec::new();
    let mut in_place = false;
    let mut output_delim: Option<char> = None;
    let mut i = 0;
    while i < args.len() {
        match args[i].as_str() {
            "-i" | "--in-place" => in_place = true,
            "-d" | "--output-delim" => {
                i += 1;
                if i >= args.len() {
                    eprintln!("stryke convert: --output-delim requires an argument");
                    return 2;
                }
                let delim_str = &args[i];
                if delim_str.chars().count() != 1 {
                    eprintln!(
                        "stryke convert: --output-delim must be a single character, got {:?}",
                        delim_str
                    );
                    return 2;
                }
                output_delim = delim_str.chars().next();
            }
            "-h" | "--help" => {
                println!("usage: stryke convert [-i] [-d DELIM] FILE...");
                println!();
                println!("Convert standard Perl source to idiomatic stryke syntax:");
                println!("  - Nested calls → |> pipe-forward chains");
                println!("  - map/grep/sort/join LIST → LIST |> map/grep/sort/join");
                println!("  - No trailing semicolons");
                println!("  - 4-space indentation");
                println!("  - #!/usr/bin/env stryke shebang");
                println!();
                println!("Options:");
                println!("  -i, --in-place       Write .stk files alongside originals");
                println!("  -d, --output-delim   Delimiter for s///, tr///, m// (default: preserve original)");
                println!();
                println!("Examples:");
                println!("  stryke convert app.pl              # print to stdout");
                println!("  stryke convert -i lib/*.pm         # write lib/*.stk");
                println!("  stryke convert -d '|' app.pl       # use | as delimiter: s|old|new|g");
                return 0;
            }
            s if s.starts_with('-') => {
                eprintln!("stryke convert: unknown option: {}", s);
                eprintln!("usage: stryke convert [-i] [-d DELIM] FILE...");
                return 2;
            }
            s => files.push(s.to_string()),
        }
        i += 1;
    }
    if files.is_empty() {
        eprintln!("stryke convert: no input files");
        eprintln!("usage: stryke convert [-i] [-d DELIM] FILE...");
        return 2;
    }
    let opts = stryke::convert::ConvertOptions { output_delim };
    let mut errors = 0;
    for f in &files {
        let code = match std::fs::read_to_string(f) {
            Ok(c) => c,
            Err(e) => {
                eprintln!("stryke convert: {}: {}", f, e);
                errors += 1;
                continue;
            }
        };
        let program = match stryke::parse_with_file(&code, f) {
            Ok(p) => p,
            Err(e) => {
                eprintln!("stryke convert: {}: {}", f, e);
                errors += 1;
                continue;
            }
        };
        let converted = stryke::convert_to_stryke_with_options(&program, &opts);
        if in_place {
            let out_path = std::path::Path::new(f).with_extension("pr");
            if let Err(e) = std::fs::write(&out_path, &converted) {
                eprintln!("stryke convert: {}: {}", out_path.display(), e);
                errors += 1;
            }
        } else {
            println!("{}", converted);
        }
    }
    if errors > 0 {
        1
    } else {
        0
    }
}

/// `stryke serve [PORT] [SCRIPT]` or `stryke serve [PORT] -e CODE` — start an HTTP server (default port 8000).
///
/// Wraps the user's handler in `serve(PORT, fn ($req) { ... })`.
fn run_serve_subcommand(args: &[String]) -> i32 {
    if !args.is_empty() && (args[0] == "-h" || args[0] == "--help") {
        eprintln!("usage: stryke serve [PORT] [SCRIPT | -e CODE]");
        eprintln!();
        eprintln!("  stryke serve                   serve $PWD on port 8000");
        eprintln!("  stryke serve PORT              serve $PWD as static files");
        eprintln!("  stryke serve PORT SCRIPT       run script (must call serve())");
        eprintln!("  stryke serve PORT -e CODE      one-liner handler");
        eprintln!();
        eprintln!("  Handler receives $req (hashref: method, path, query, headers, body, peer)");
        eprintln!("  and returns: string (200 OK), key-value pairs, hashref, or undef (404).");
        eprintln!();
        eprintln!("examples:");
        eprintln!(
            "  stryke serve                                              # static file server on 8000"
        );
        eprintln!(
            "  stryke serve 8080                                         # static file server"
        );
        eprintln!("  stryke serve 8080 app.stk                                 # script handler");
        eprintln!("  stryke serve 3000 -e '\"hello \" . $req->{{path}}'           # one-liner");
        eprintln!("  stryke serve 8080 -e 'status => 200, body => json_encode(+{{ok => 1}})'");
        return 0;
    }

    // If first arg is a valid port number, consume it; otherwise default to 8000.
    let (port, rest) = if !args.is_empty() && args[0].parse::<u16>().is_ok() {
        (args[0].clone(), &args[1..])
    } else {
        ("8000".to_string(), args)
    };

    // Detect mode: no arg or directory = static file server, -e = one-liner, else = script
    let static_dir = if rest.is_empty() {
        Some(
            std::env::current_dir()
                .unwrap_or_default()
                .to_string_lossy()
                .to_string(),
        )
    } else if rest[0] != "-e" && Path::new(&rest[0]).is_dir() {
        Some(
            std::fs::canonicalize(&rest[0])
                .unwrap_or_else(|_| PathBuf::from(&rest[0]))
                .to_string_lossy()
                .to_string(),
        )
    } else {
        None
    };

    let code = if let Some(dir) = static_dir {
        let dir_escaped = dir.replace('\\', "\\\\").replace('"', "\\\"");
        eprintln!("stryke: serving {} on http://0.0.0.0:{}", dir, port);
        format!(
            r#"
chdir "{dir_escaped}"

my %mime = (
    html => "text/html; charset=utf-8",
    htm => "text/html; charset=utf-8",
    css => "text/css; charset=utf-8",
    js => "application/javascript; charset=utf-8",
    mjs => "application/javascript; charset=utf-8",
    json => "application/json; charset=utf-8",
    xml => "text/xml; charset=utf-8",
    md => "text/markdown; charset=utf-8",
    txt => "text/plain; charset=utf-8",
    toml => "application/toml; charset=utf-8",
    pl => "text/x-perl; charset=utf-8",
    pr => "text/x-perl; charset=utf-8",
    pm => "text/x-perl; charset=utf-8",
    png => "image/png",
    jpg => "image/jpeg",
    jpeg => "image/jpeg",
    gif => "image/gif",
    svg => "image/svg+xml",
    webp => "image/webp",
    avif => "image/avif",
    ico => "image/x-icon",
    woff2 => "font/woff2",
    woff => "font/woff",
    ttf => "font/ttf",
    mp3 => "audio/mpeg",
    ogg => "audio/ogg",
    mp4 => "video/mp4",
    webm => "video/webm",
    zip => "application/zip",
    gz => "application/gzip",
    wasm => "application/wasm",
    pdf => "application/pdf"
)

fn mime_for($path) {{
    my $ext = $path =~ /\.([^.]+)$/ ? lc($1) : ""
    $mime{{$ext}} // "text/plain"
}}

fn dir_listing($url_path, $fs_path) {{
    $url_path .= "/" unless $url_path =~ m|/$|
    my $prefix = $fs_path eq "." ? "" : "$fs_path/"
    my @entries
    push @entries, ".." unless $url_path eq "/"
    push @entries, dirs($fs_path)
    push @entries, filesf($fs_path)
    my $html = ""
    for my $e (@entries) {{
        my $full = $e eq ".." ? ".." : "$prefix$e"
        my $name = $e
        my $href = $url_path . $name
        if (-d $full) {{
            $html .= "<li class=\"dir\"><a href=\"$href/\">$name/</a></li>"
        }} else {{
            my $sz = (stat($full))[7] // 0
            $html .= "<li><a href=\"$href\">$name</a> <span style=\"color:#888\">($sz bytes)</span></li>"
        }}
    }}
    "<!DOCTYPE html><html><head><meta charset=\"utf-8\">"
    . "<title>Directory listing for $url_path</title>"
    . "<style>body{{font-family:monospace;margin:2em}}a{{text-decoration:none}}a:hover{{text-decoration:underline}}li{{padding:2px 0}}.dir{{font-weight:bold}}</style>"
    . "</head><body><h1>Directory listing for $url_path</h1><hr><ul>"
    . $html
    . "</ul><hr><p style=\"color:#888\">stryke/{port}</p></body></html>"
}}

serve {port}, fn ($req) {{
    my $url_path = $req->{{path}}
    $url_path =~ s|\.\./||g
    my $fs_path = $url_path =~ s|^/||r
    $fs_path = "." if $fs_path eq ""

    if (-d $fs_path) {{
        my $idx = $fs_path eq "." ? "index.html" : "$fs_path/index.html"
        if (-f $idx) {{
            +{{ status => 200, body => cat($idx), headers => +{{ "content-type" => "text/html; charset=utf-8" }} }}
        }} else {{
            +{{ status => 200, body => dir_listing($url_path, $fs_path), headers => +{{ "content-type" => "text/html; charset=utf-8" }} }}
        }}
    }} elsif (-f $fs_path) {{
        +{{ status => 200, body => cat($fs_path), headers => +{{ "content-type" => mime_for($fs_path) }} }}
    }} else {{
        +{{ status => 404, body => "404 Not Found: $url_path\n" }}
    }}
}}
"#
        )
    } else if rest[0] == "-e" {
        if rest.len() < 2 {
            eprintln!("stryke serve: -e requires an argument");
            return 1;
        }
        let handler_body = rest[1..].join(" ");
        format!("serve {}, fn ($req) {{ {} }}", port, handler_body)
    } else {
        // Script file — the script must call serve() itself.
        // PORT is injected as $ENV{STRYKE_PORT} for convenience.
        let script_path = &rest[0];
        match std::fs::read_to_string(script_path) {
            Ok(src) => {
                format!("$ENV{{STRYKE_PORT}} = {}\n{}", port, src)
            }
            Err(e) => {
                eprintln!("stryke serve: {}: {}", script_path, e);
                return 1;
            }
        }
    };

    let mut interp = stryke::interpreter::Interpreter::new();
    match stryke::parse_and_run_string(&code, &mut interp) {
        Ok(_) => 0,
        Err(e) => {
            if let stryke::error::ErrorKind::Exit(code) = e.kind {
                return code;
            }
            eprintln!("{}", e);
            255
        }
    }
}

#[allow(non_snake_case)]
/// `stryke docs [TOPIC]` — interactive built-in documentation book.
///
/// - `stryke docs`          → full-screen interactive book (vim-style navigation)
/// - `stryke docs TOPIC`    → single-topic lookup
/// - `stryke docs -t`       → table of contents
/// - `stryke docs -s PAT`   → search topics
/// - `stryke docs -h`       → help
fn run_doc_subcommand(args: &[String]) -> i32 {
    let theme = DocTheme {
        C: "\x1b[36m",
        G: "\x1b[32m",
        Y: "\x1b[1;33m",
        M: "\x1b[35m",
        B: "\x1b[1m",
        D: "\x1b[2m",
        N: "\x1b[0m",
    };
    let DocTheme {
        C,
        G,
        Y,
        M,
        B,
        D,
        N,
    } = theme;

    // Build topic entries from categorized list, then pick up any uncategorized leftovers.
    // Deduplicate aliases that map to the same doc text (e.g. thread/t, hmac/hmac_sha256).
    let mut entries: Vec<(&str, &str, String)> = Vec::new();
    let mut seen = std::collections::HashSet::new();
    let mut seen_text_ptrs = std::collections::HashSet::new();
    for &(category, topics) in stryke::lsp::DOC_CATEGORIES {
        for &topic in topics {
            if let Some(text) = stryke::lsp::doc_text_for(topic) {
                let ptr = text.as_ptr() as usize;
                if !seen_text_ptrs.insert(ptr) {
                    seen.insert(topic);
                    continue; // alias — same doc already rendered under canonical name
                }
                let rendered = render_page_content(topic, text, C, G, D, N);
                entries.push((category, topic, rendered));
                seen.insert(topic);
            }
        }
    }
    // Pick up every documented topic not yet in a category
    for topic in stryke::lsp::doc_topics() {
        if seen.contains(topic) {
            continue;
        }
        if let Some(text) = stryke::lsp::doc_text_for(topic) {
            let ptr = text.as_ptr() as usize;
            if !seen_text_ptrs.insert(ptr) {
                continue; // alias already rendered
            }
            let rendered = render_page_content(topic, text, C, G, D, N);
            entries.push(("Other", topic, rendered));
        }
    }
    if entries.is_empty() {
        eprintln!("stryke docs: no documentation pages found");
        return 1;
    }

    // Pack topics until adding the next would overflow the content area.
    // Header=11 rows, footer=3 rows → content area = term_h - 14.
    let content_area = term_height().saturating_sub(14).max(4);
    let mut pages = build_fixed_pages(&entries, content_area);

    // Insert intro page at position 0
    let entry_count = entries.len();
    let chapter_count = stryke::lsp::DOC_CATEGORIES.len();
    let mut intro = format!(
        "\
  {D}>> THE STRYKE ENCYCLOPEDIA // INTERACTIVE REFERENCE SYSTEM <<{N}\n\
\n\
  {B}A comprehensive reference for every stryke builtin, keyword,{N}\n\
  {B}and extension. {G}{entry_count}{N} {B}topics across {G}{chapter_count}{N} {B}chapters.{N}\n\
\n\
  {D}── GETTING STARTED ─────────────────────────────────────────────{N}\n\
\n\
  {C}j{N} / {C}n{N} / {C}space{N}        next page\n\
  {C}k{N} / {C}p{N}                previous page\n\
  {C}]{N} / {C}[{N}                next / previous chapter\n\
  {C}d{N} / {C}u{N}                forward / back 5 pages\n\
  {C}g{N} / {C}G{N}                first / last page\n\
  {C}t{N}                    table of contents\n\
  {C}/{N}                    search all pages\n\
  {C}:{N}                    jump to page number\n\
  {C}r{N}                    random page\n\
  {C}?{N}                    full keybinding help\n\
  {C}q{N}                    quit\n\
\n\
  {D}── CHAPTERS ───────────────────────────────────────────────────{N}\n\
"
    );
    for (i, &(cat, topics)) in stryke::lsp::DOC_CATEGORIES.iter().enumerate() {
        intro.push_str(&format!(
            "  {C}{:>2}.{N} {B}{:<32}{N} {D}{} topics{N}\n",
            i + 1,
            cat,
            topics.len(),
        ));
    }
    intro.push_str(&format!(
        "\n  {D}press {C}j{D} or {C}space{D} to begin >>>{N}\n"
    ));
    // Pad intro to content area height
    let intro_page = pad_to_height(&intro, content_area);
    pages.insert(0, ("Introduction".to_string(), intro_page, Vec::new()));
    let total = pages.len();

    if args.first().map(|s| s.as_str()) == Some("-h")
        || args.first().map(|s| s.as_str()) == Some("--help")
    {
        println!();
        doc_print_banner(theme);
        doc_print_hline('', '', theme);
        doc_print_boxline(
            &format!(" {G}STATUS: ONLINE{N}  {D}//{N} {C}SIGNAL: {G}████████{D}░░{N}  {D}//{N} {M}STRYKE DOCS{N}"),
            theme,
        );
        doc_print_hline('', '', theme);
        println!("  {D}>> THE STRYKE ENCYCLOPEDIA // INTERACTIVE REFERENCE SYSTEM <<{N}");
        println!();
        println!("  {B}USAGE:{N} stryke docs {D}[OPTIONS] [PAGE|TOPIC]{N}");
        println!();
        doc_print_separator("OPTIONS", theme);
        println!("  {C}-h, --help{N}                          {D}// Show this help{N}");
        println!("  {C}-t, --toc{N}                           {D}// Table of contents{N}");
        println!("  {C}-s, --search <pattern>{N}              {D}// Search pages{N}");
        println!("  {C}-l, --list{N}                          {D}// List all pages{N}");
        println!(
            "  {C}TOPIC{N}                               {D}// Jump to topic (stryke docs pmap){N}"
        );
        println!("  {C}PAGE{N}                                {D}// Jump to page number{N}");
        println!();
        doc_print_separator("NAVIGATION (vim-style)", theme);
        println!("  {C}j / n / l / enter / space{N}           {D}// Next page{N}");
        println!("  {C}k / p / h{N}                           {D}// Previous page{N}");
        println!("  {C}d{N}                                   {D}// Forward 5 pages{N}");
        println!("  {C}u{N}                                   {D}// Back 5 pages{N}");
        println!("  {C}g / 0{N}                               {D}// First page{N}");
        println!("  {C}G / ${N}                               {D}// Last page{N}");
        println!("  {C}] / }}{N}                              {D}// Next chapter{N}");
        println!("  {C}[ / {{{N}                              {D}// Previous chapter{N}");
        println!("  {C}t{N}                                   {D}// Table of contents{N}");
        println!("  {C}/ <pattern>{N}                         {D}// Search pages{N}");
        println!("  {C}:<number>{N}                           {D}// Jump to page{N}");
        println!("  {C}r{N}                                   {D}// Random page{N}");
        println!("  {C}?{N}                                   {D}// Keybinding help{N}");
        println!("  {C}q{N}                                   {D}// Quit{N}");
        println!();
        doc_print_separator("EXAMPLES", theme);
        println!("  {C}stryke docs{N}                             {D}// start from page 1{N}");
        println!("  {C}stryke docs --toc{N}                       {D}// table of contents{N}");
        println!("  {C}stryke docs 42{N}                          {D}// jump to page 42{N}");
        println!("  {C}stryke docs pmap{N}                        {D}// jump to pmap{N}");
        println!("  {C}stryke docs --search parallel{N}           {D}// find parallel pages{N}");
        println!();
        return 0;
    }

    // --toc: print table of contents and exit
    if args.first().map(|s| s.as_str()) == Some("-t")
        || args.first().map(|s| s.as_str()) == Some("--toc")
    {
        doc_print_toc_entries(&entries, &pages, theme);
        return 0;
    }

    // --list: compact list
    if args.first().map(|s| s.as_str()) == Some("-l")
        || args.first().map(|s| s.as_str()) == Some("--list")
    {
        for (i, (_, topic, _)) in entries.iter().enumerate() {
            println!("{:>3}. {}", i + 1, topic);
        }
        return 0;
    }

    // --search: search and exit
    if (args.first().map(|s| s.as_str()) == Some("-s")
        || args.first().map(|s| s.as_str()) == Some("--search"))
        && args.len() >= 2
    {
        let pat = args[1].to_lowercase();
        let mut found = 0;
        for (i, (cat, topic, text)) in entries.iter().enumerate() {
            if topic.to_lowercase().contains(&pat)
                || cat.to_lowercase().contains(&pat)
                || text.to_lowercase().contains(&pat)
            {
                println!("  {C}{:>3}.{N} {B}{}{N}  {D}({}){N}", i + 1, topic, cat);
                found += 1;
            }
        }
        if found == 0 {
            println!("  {Y}no results for '{}'{N}", pat);
        }
        return 0;
    }

    // Single topic or page number — find which page contains it
    let mut start_page: usize = 0;
    if !args.is_empty() {
        let arg = &args[0];
        // Try page number
        if let Ok(n) = arg.parse::<usize>() {
            if n >= 1 && n <= total {
                start_page = n - 1;
            }
        } else {
            // Try topic name → find which page contains that entry
            let lower = arg.to_lowercase();
            let entry_idx = entries
                .iter()
                .position(|(_, t, _)| t.to_lowercase() == lower)
                .or_else(|| {
                    entries
                        .iter()
                        .position(|(_, t, _)| t.to_lowercase().contains(&lower))
                });
            match entry_idx {
                Some(eidx) => {
                    // Find the page that contains this entry index
                    start_page = pages
                        .iter()
                        .position(|(_, _, indices)| indices.contains(&eidx))
                        .unwrap_or(0);
                }
                None => {
                    eprintln!("stryke docs: no documentation for '{}'", arg);
                    eprintln!("run 'stryke docs -h' for help");
                    return 1;
                }
            }
        }
    }

    // ── Interactive TUI book mode ────────────────────────────
    if !io::stdout().is_terminal() {
        // Not a TTY — just dump the page
        print!("{}", pages[start_page].1);
        return 0;
    }

    doc_interactive_loop(&pages, &entries, &intro, start_page, total, theme)
}

/// Truncate/pad text to exactly `height` lines, joined with `\r\n`.
fn pad_to_height(text: &str, height: usize) -> String {
    let lines: Vec<&str> = text.lines().collect();
    let mut buf: Vec<&str> = Vec::with_capacity(height);
    for line in lines.iter().take(height) {
        buf.push(line);
    }
    while buf.len() < height {
        buf.push("");
    }
    buf.join("\r\n")
}

/// Pack topics into pages that fit within `max_lines` of content.
/// Pack 2–3 entries per page. Uses 3 when they fit in `max_lines`,
/// otherwise 2. New chapter always starts a new page.
fn build_fixed_pages(
    entries: &[(&str, &str, String)],
    max_lines: usize,
) -> Vec<(String, String, Vec<usize>)> {
    let mut pages: Vec<(String, String, Vec<usize>)> = Vec::new();
    let mut i = 0;
    while i < entries.len() {
        let cat = entries[i].0.to_string();
        // Always take at least 2 (or 1 if last entry)
        let mut end = (i + 2).min(entries.len());
        // Try to fit a 3rd if same chapter and lines fit
        if end < entries.len() && entries[end].0 == cat {
            let lines: usize = (i..=end).map(|j| entries[j].2.lines().count() + 1).sum();
            if lines <= max_lines {
                end += 1;
            }
        }
        // Stop at chapter boundary
        if let Some(pos) = entries[i + 1..end].iter().position(|e| e.0 != cat) {
            end = i + 1 + pos;
        }
        let mut buf = String::new();
        let mut indices = Vec::new();
        for (j, entry) in entries.iter().enumerate().take(end).skip(i) {
            if j > i {
                buf.push('\n');
            }
            buf.push_str(&entry.2);
            indices.push(j);
        }
        pages.push((cat, buf, indices));
        i = end;
    }
    pages
}

/// Find the page whose `indices` contains `entry_idx`.
fn find_page_for_entry(pages: &[(String, String, Vec<usize>)], entry_idx: usize) -> usize {
    for (pi, (_cat, _content, indices)) in pages.iter().enumerate() {
        if indices.contains(&entry_idx) {
            return pi;
        }
    }
    0
}

/// SIGWINCH flag — set by the signal handler, cleared after re-render.
static SIGWINCH_RECEIVED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

/// Bare signal handler — just sets the atomic flag.
#[cfg(unix)]
extern "C" fn sigwinch_handler(_sig: libc::c_int) {
    SIGWINCH_RECEIVED.store(true, std::sync::atomic::Ordering::Relaxed);
}

#[allow(non_snake_case)]
#[derive(Clone, Copy)]
struct DocTheme<'a> {
    C: &'a str,
    G: &'a str,
    Y: &'a str,
    M: &'a str,
    B: &'a str,
    D: &'a str,
    N: &'a str,
}

/// The interactive full-screen pager loop.
#[cfg(unix)]
fn doc_interactive_loop(
    pages: &[(String, String, Vec<usize>)],
    entries: &[(&str, &str, String)],
    intro_raw: &str,
    start: usize,
    total: usize,
    theme: DocTheme,
) -> i32 {
    let DocTheme {
        C, G, M, B, D, N, ..
    } = theme;
    use std::os::unix::io::AsRawFd;

    let stdin_fd = io::stdin().as_raw_fd();
    // Save terminal state and enter raw mode
    let mut old_termios: libc::termios = unsafe { std::mem::zeroed() };
    unsafe { libc::tcgetattr(stdin_fd, &mut old_termios) };
    let mut raw = old_termios;
    unsafe { libc::cfmakeraw(&mut raw) };
    unsafe { libc::tcsetattr(stdin_fd, libc::TCSANOW, &raw) };

    // Install SIGWINCH handler
    let old_sigwinch = unsafe {
        libc::signal(
            libc::SIGWINCH,
            sigwinch_handler as *const () as libc::sighandler_t,
        )
    };

    // Mutable — rebuilt on terminal resize
    let mut pages = pages.to_vec();
    let mut total = total;
    let mut current: usize = start;

    // In raw mode, \n doesn't do \r\n — use this macro for every output line.
    macro_rules! rprint {
        () => { print!("\r\n"); };
        ($($arg:tt)*) => { print!("{}\r\n", format!($($arg)*)); };
    }

    let render = |cur: usize, pages: &[(String, String, Vec<usize>)], total: usize| {
        let (ref cat, ref content, ref indices) = pages[cur];
        // Build topic list for status line
        let topic_list: String = indices
            .iter()
            .take(3)
            .map(|&i| entries[i].1)
            .collect::<Vec<_>>()
            .join(", ");
        let topic_display = if indices.len() > 3 {
            format!("{} +{}", topic_list, indices.len() - 3)
        } else {
            topic_list
        };
        let term_h = term_height();

        // Clear entire screen
        print!("\x1b[H\x1b[2J");

        // ── Header (rows 1-11, absolute positioned) ──
        print!("\x1b[1;1H"); // row 1
        rprint!();
        rprint!(" {C}███████╗████████╗██████╗ ██╗   ██╗██╗  ██╗███████╗{N}");
        rprint!(" {C}██╔════╝╚══██╔══╝██╔══██╗╚██╗ ██╔╝██║ ██╔╝██╔════╝{N}");
        rprint!(" {M}███████╗   ██║   ██████╔╝ ╚████╔╝ █████╔╝ █████╗  {N}");
        rprint!(" {M}╚════██║   ██║   ██╔══██╗  ╚██╔╝  ██╔═██╗ ██╔══╝  {N}");
        rprint!(" {C}███████║   ██║   ██║  ██║   ██║   ██║  ██╗███████╗{N}");
        rprint!(" {C}╚══════╝   ╚═╝   ╚═╝  ╚═╝   ╚═╝   ╚═╝  ╚═╝╚══════╝{N}");
        // Status box
        print!(" {D}");
        for _ in 0..74 {
            print!("");
        }
        print!("{N}\r\n");
        let status = format!(
            " {G}{:>3}/{}{N}  {D}//{N} {C}{}{N}  {D}//{N} {M}{}{N}",
            cur + 1,
            total,
            topic_display,
            cat,
        );
        let vis_len = strip_ansi_len(&status);
        let pad = 74_usize.saturating_sub(vis_len);
        print!(" {D}{N}{status}{:>pad$}{D}{N}\r\n", "", pad = pad);
        print!(" {D}");
        for _ in 0..74 {
            print!("");
        }
        print!("{N}\r\n");

        // ── Content (row 12 onward, truncated to fit above footer) ──
        let content_start = 12;
        let footer_rows = 3; // separator + keybindings + prompt
        let max_content = if term_h > content_start + footer_rows {
            term_h - content_start - footer_rows
        } else {
            1
        };
        print!("\x1b[{};1H", content_start);
        for (li, line) in content.lines().enumerate() {
            if li >= max_content {
                break; // truncate — don't scroll past footer
            }
            print!("{line}\r\n");
        }

        // ── Footer (pinned to last 3 rows, absolute positioned) ──
        print!("\x1b[{};1H", term_h - 2);
        print!("  {D}");
        for _ in 0..76 {
            print!("");
        }
        print!("{N}\r\n");
        print!("  {C}j{N}/{C}n{N} next  {C}k{N}/{C}p{N} prev  {C}d{N}/{C}u{N} ±5  {C}]{N}/{C}[{N} chapter  {C}t{N} toc  {C}/{N} search  {C}:{N}num  {C}r{N} rand  {C}?{N} help  {C}q{N} quit\r\n");
        print!("  {D}>>>{N} ");
        let _ = io::stdout().flush();
    };

    render(current, &pages, total);

    loop {
        let mut buf = [0u8; 1];
        let nread = unsafe { libc::read(stdin_fd, buf.as_mut_ptr() as *mut libc::c_void, 1) };
        if nread != 1 {
            // SIGWINCH — rebuild pages for new terminal height, then re-render
            if SIGWINCH_RECEIVED.swap(false, std::sync::atomic::Ordering::Relaxed) {
                let entry_idx = pages[current].2.first().copied().unwrap_or(0);
                let th = term_height();
                let content_area = th.saturating_sub(14).max(4);
                let mut rebuilt = build_fixed_pages(entries, content_area);
                let intro_page = pad_to_height(intro_raw, content_area);
                rebuilt.insert(0, ("Introduction".to_string(), intro_page, Vec::new()));
                pages = rebuilt;
                total = pages.len();
                current = if entry_idx == 0 && current == 0 {
                    0
                } else {
                    find_page_for_entry(&pages, entry_idx).min(total - 1)
                };
                render(current, &pages, total);
                continue;
            }
            break;
        }
        let key = buf[0];
        match key {
            // Next: j n l space enter
            b'j' | b'n' | b'l' | b' ' | b'\n' | b'\r' if current < total - 1 => {
                current += 1;
            }
            // Prev: k p h
            b'k' | b'p' | b'h' => {
                current = current.saturating_sub(1);
            }
            // First: g 0
            b'g' | b'0' => current = 0,
            // Last: G $
            b'G' | b'$' => current = total - 1,
            // Forward 5: d
            b'd' => {
                current = (current + 5).min(total - 1);
            }
            // Back 5: u
            b'u' => {
                current = current.saturating_sub(5);
            }
            // Next chapter: ] }
            b']' | b'}' => {
                let cur_cat = &pages[current].0;
                while current < total - 1 {
                    current += 1;
                    if pages[current].0 != *cur_cat {
                        break;
                    }
                }
            }
            // Prev chapter: [ {
            b'[' | b'{' => {
                let cur_cat = pages[current].0.clone();
                while current > 0 {
                    current -= 1;
                    if pages[current].0 != cur_cat {
                        break;
                    }
                }
            }
            // Random: r
            b'r' => {
                current = rand::thread_rng().gen_range(0..total);
            }
            // TOC: t
            b't' => {
                // Restore cooked mode for line input
                unsafe { libc::tcsetattr(stdin_fd, libc::TCSANOW, &old_termios) };
                print!("\x1b[H\x1b[2J");
                doc_print_toc_entries(entries, &pages, theme);
                print!("  {D}enter page number or press enter to return >>>{N} ");
                let _ = io::stdout().flush();
                let mut line = String::new();
                let _ = io::stdin().read_line(&mut line);
                if let Ok(n) = line.trim().parse::<usize>() {
                    if n >= 1 && n <= total {
                        current = n - 1;
                    }
                }
                unsafe { libc::tcsetattr(stdin_fd, libc::TCSANOW, &raw) };
            }
            // Search: /
            b'/' => {
                unsafe { libc::tcsetattr(stdin_fd, libc::TCSANOW, &old_termios) };
                print!("\r  {C}/{N}");
                let _ = io::stdout().flush();
                let mut line = String::new();
                let _ = io::stdin().read_line(&mut line);
                let pat = line.trim().to_lowercase();
                if !pat.is_empty() {
                    // Search forward from current page
                    let start_from = (current + 1) % total;
                    let mut found = false;
                    for i in 0..total {
                        let idx = (start_from + i) % total;
                        let (ref cat, ref content, _) = pages[idx];
                        if cat.to_lowercase().contains(&pat)
                            || content.to_lowercase().contains(&pat)
                        {
                            current = idx;
                            found = true;
                            break;
                        }
                    }
                    let _ = found; // overwritten by render
                }
                unsafe { libc::tcsetattr(stdin_fd, libc::TCSANOW, &raw) };
            }
            // Goto: :
            b':' => {
                unsafe { libc::tcsetattr(stdin_fd, libc::TCSANOW, &old_termios) };
                print!("\r  {C}:{N}");
                let _ = io::stdout().flush();
                let mut line = String::new();
                let _ = io::stdin().read_line(&mut line);
                if let Ok(n) = line.trim().parse::<usize>() {
                    if n >= 1 && n <= total {
                        current = n - 1;
                    }
                }
                unsafe { libc::tcsetattr(stdin_fd, libc::TCSANOW, &raw) };
            }
            // Help: ?
            b'?' => {
                print!("\x1b[H\x1b[2J");
                rprint!();
                rprint!("  {D}── KEYBINDINGS ────────────────────────────────────────────────────{N}");
                rprint!();
                rprint!("  {B}Navigation{N}");
                rprint!("  {C}j n l space enter{N}    {D}next page{N}");
                rprint!("  {C}k p h{N}                {D}previous page{N}");
                rprint!("  {C}d{N}                    {D}forward 5 pages{N}");
                rprint!("  {C}u{N}                    {D}back 5 pages{N}");
                rprint!("  {C}g 0{N}                  {D}first page{N}");
                rprint!("  {C}G ${N}                  {D}last page{N}");
                rprint!("  {C}] }}{N}                  {D}next chapter{N}");
                rprint!("  {C}[ {{{N}                  {D}previous chapter{N}");
                rprint!();
                rprint!("  {B}Search & Jump{N}");
                rprint!("  {C}/{N}                    {D}search pages{N}");
                rprint!("  {C}:{N}                    {D}go to page number{N}");
                rprint!("  {C}t{N}                    {D}table of contents{N}");
                rprint!("  {C}r{N}                    {D}random page{N}");
                rprint!();
                rprint!("  {B}Other{N}");
                rprint!("  {C}?{N}                    {D}this help{N}");
                rprint!("  {C}q Q{N}                  {D}quit{N}");
                rprint!();
                rprint!("  {D}press any key to return{N}");
                let _ = io::stdout().flush();
                let mut b2 = [0u8; 1];
                let _ = unsafe { libc::read(stdin_fd, b2.as_mut_ptr() as *mut _, 1) };
            }
            // Quit: q Q
            b'q' | b'Q' | 0x03 /* ctrl-c */ => {
                break;
            }
            _ => {}
        }
        render(current, &pages, total);
    }

    // Restore terminal and SIGWINCH handler
    unsafe { libc::signal(libc::SIGWINCH, old_sigwinch) };
    unsafe { libc::tcsetattr(stdin_fd, libc::TCSANOW, &old_termios) };
    print!("\x1b[H\x1b[2J");
    let _ = io::stdout().flush();
    0
}

#[cfg(not(unix))]
fn doc_interactive_loop(
    pages: &[(String, String, Vec<usize>)],
    _entries: &[(&str, &str, String)],
    _intro_raw: &str,
    start: usize,
    _total: usize,
    _theme: DocTheme,
) -> i32 {
    // Fallback: just print the starting page
    print!("{}", pages[start].1);
    0
}

fn term_height() -> usize {
    #[cfg(unix)]
    {
        let mut ws = libc::winsize {
            ws_row: 0,
            ws_col: 0,
            ws_xpixel: 0,
            ws_ypixel: 0,
        };
        if unsafe { libc::ioctl(2, libc::TIOCGWINSZ, &mut ws) } == 0 && ws.ws_row > 0 {
            return ws.ws_row as usize;
        }
    }
    24
}

fn term_width() -> usize {
    #[cfg(unix)]
    {
        let mut ws = libc::winsize {
            ws_row: 0,
            ws_col: 0,
            ws_xpixel: 0,
            ws_ypixel: 0,
        };
        if unsafe { libc::ioctl(2, libc::TIOCGWINSZ, &mut ws) } == 0 && ws.ws_col > 0 {
            return ws.ws_col as usize;
        }
    }
    80
}

fn strip_ansi_len(s: &str) -> usize {
    let mut len = 0;
    let mut in_esc = false;
    for c in s.chars() {
        if c == '\x1b' {
            in_esc = true;
        } else if in_esc {
            if c == 'm' {
                in_esc = false;
            }
        } else {
            len += 1;
        }
    }
    len
}

fn doc_print_banner(theme: DocTheme) {
    let DocTheme { C, M, N, .. } = theme;
    println!(" {C}███████╗████████╗██████╗ ██╗   ██╗██╗  ██╗███████╗{N}");
    println!(" {C}██╔════╝╚══██╔══╝██╔══██╗╚██╗ ██╔╝██║ ██╔╝██╔════╝{N}");
    println!(" {M}███████╗   ██║   ██████╔╝ ╚████╔╝ █████╔╝ █████╗  {N}");
    println!(" {M}╚════██║   ██║   ██╔══██╗  ╚██╔╝  ██╔═██╗ ██╔══╝  {N}");
    println!(" {C}███████║   ██║   ██║  ██║   ██║   ██║  ██╗███████╗{N}");
    println!(" {C}╚══════╝   ╚═╝   ╚═╝  ╚═╝   ╚═╝   ╚═╝  ╚═╝╚══════╝{N}");
}

fn doc_print_hline(left: char, right: char, theme: DocTheme) {
    let DocTheme { D, N, .. } = theme;
    print!(" {D}{left}");
    for _ in 0..74 {
        print!("");
    }
    println!("{right}{N}");
}

fn doc_print_boxline(content: &str, theme: DocTheme) {
    let DocTheme { D, N, .. } = theme;
    // Strip ANSI to measure visible width
    let stripped = content
        .bytes()
        .fold((Vec::new(), false), |(mut acc, in_esc), b| {
            if b == 0x1b {
                (acc, true)
            } else if in_esc {
                (acc, b != b'm')
            } else {
                acc.push(b);
                (acc, false)
            }
        })
        .0;
    let visible = String::from_utf8_lossy(&stripped).chars().count();
    let inner: usize = 74;
    let pad = inner.saturating_sub(visible);
    println!(" {D}{N}{content}{:>pad$}{D}{N}", "", pad = pad);
}

fn doc_print_separator(label: &str, theme: DocTheme) {
    let DocTheme { D, N, .. } = theme;
    let trail = 72usize.saturating_sub(label.len());
    print!("  {D}── {label} ");
    for _ in 0..trail {
        print!("");
    }
    println!("{N}");
}

fn doc_print_toc_entries(
    entries: &[(&str, &str, String)],
    pages: &[(String, String, Vec<usize>)],
    theme: DocTheme,
) {
    let DocTheme {
        C, G, M, B, D, N, ..
    } = theme;
    let topic_count = entries.len();
    let page_count = pages.len();
    println!();
    doc_print_banner(theme);
    doc_print_hline('', '', theme);
    doc_print_boxline(
        &format!(
            " {G}TABLE OF CONTENTS{N}  {D}//{N} {C}{topic_count} topics, {page_count} pages{N}  {D}//{N} {M}The stryke Encyclopedia{N}"
        ),
        theme,
    );
    doc_print_hline('', '', theme);
    println!();
    let mut last_cat = "";
    for (entry_idx, (cat, topic, _)) in entries.iter().enumerate() {
        if *cat != last_cat {
            println!();
            println!("  {B}{cat}{N}");
            last_cat = cat;
        }
        // Find which page this entry is on (skip intro page at index 0)
        let page_num = pages
            .iter()
            .position(|(_, _, indices)| indices.contains(&entry_idx))
            .map(|p| p + 1)
            .unwrap_or(0);
        println!(
            "    {C}{:>3}.{N} {:<30} {D}p.{}{N}",
            entry_idx + 1,
            topic,
            page_num
        );
    }
    println!();
}

/// Word-wrap a plain-text line at `max_vis` visible characters.
/// Returns wrapped lines (without leading indent — caller adds it).
/// ANSI escapes are not counted toward visible width.
fn word_wrap(text: &str, max_vis: usize) -> Vec<String> {
    if max_vis == 0 {
        return vec![text.to_string()];
    }
    let mut lines: Vec<String> = Vec::new();
    let mut cur = String::new();
    let mut vis = 0usize;

    for word in text.split(' ') {
        let wvis = strip_ansi_len(word);
        if vis > 0 && vis + 1 + wvis > max_vis {
            // wrap
            lines.push(cur);
            cur = word.to_string();
            vis = wvis;
        } else {
            if vis > 0 {
                cur.push(' ');
                vis += 1;
            }
            cur.push_str(word);
            vis += wvis;
        }
    }
    if !cur.is_empty() || lines.is_empty() {
        lines.push(cur);
    }
    lines
}

/// Render a single page's content (without banner/chrome).
/// Prose lines are word-wrapped at 76 visible columns (80 - 2*indent).
/// Code lines are kept as-is (indented 4 spaces).
#[allow(non_snake_case)]
fn render_page_content(topic: &str, text: &str, C: &str, G: &str, D: &str, N: &str) -> String {
    let max_vis = term_width().saturating_sub(4).max(40); // width - 2 indent - 2 margin
    let mut out = String::with_capacity(text.len() + 512);
    out.push_str(&format!("  {C}{topic}{N}\n"));
    out.push_str(&format!(
        "  {D}{}{N}\n",
        "".repeat(topic.len().max(20).min(max_vis))
    ));
    let mut in_code = false;
    for line in text.split('\n') {
        if line.starts_with("```") {
            in_code = !in_code;
            continue;
        }
        if in_code {
            out.push_str(&format!("  {G}  {line}{N}\n"));
        } else if line.trim().is_empty() {
            out.push('\n');
        } else {
            let rendered = render_inline_code(line, C, N);
            for wrapped in word_wrap(&rendered, max_vis) {
                out.push_str(&format!("  {wrapped}\n"));
            }
        }
    }
    out
}

/// Replace `backtick` spans with colored versions for terminal output.
fn render_inline_code(line: &str, color: &str, reset: &str) -> String {
    let mut out = String::with_capacity(line.len() + 64);
    let mut in_tick = false;
    for ch in line.chars() {
        if ch == '`' {
            if in_tick {
                out.push_str(reset);
            } else {
                out.push_str(color);
            }
            in_tick = !in_tick;
        } else {
            out.push(ch);
        }
    }
    out
}

/// `stryke deconvert FILE...` — convert stryke .stk files back to standard Perl .pl syntax.
fn run_deconvert_subcommand(args: &[String]) -> i32 {
    let mut files: Vec<String> = Vec::new();
    let mut in_place = false;
    let mut output_delim: Option<char> = None;
    let mut i = 0;
    while i < args.len() {
        match args[i].as_str() {
            "-i" | "--in-place" => in_place = true,
            "-d" | "--output-delim" => {
                i += 1;
                if i >= args.len() {
                    eprintln!("stryke deconvert: --output-delim requires an argument");
                    return 2;
                }
                let delim_str = &args[i];
                if delim_str.chars().count() != 1 {
                    eprintln!(
                        "stryke deconvert: --output-delim must be a single character, got {:?}",
                        delim_str
                    );
                    return 2;
                }
                output_delim = delim_str.chars().next();
            }
            "-h" | "--help" => {
                println!("usage: stryke deconvert [-i] [-d DELIM] FILE...");
                println!();
                println!("Convert stryke .stk files back to standard Perl .pl syntax:");
                println!("  - Pipe chains and thread macros → nested function calls");
                println!("  - fn → sub");
                println!("  - p → say");
                println!("  - Adds trailing semicolons");
                println!("  - #!/usr/bin/env perl shebang prepended");
                println!();
                println!("Options:");
                println!("  -i, --in-place       Write .pl files alongside originals");
                println!("  -d, --output-delim   Delimiter for s///, tr///, m// (default: preserve original)");
                println!();
                println!("Examples:");
                println!("  stryke deconvert app.stk             # print to stdout");
                println!("  stryke deconvert -i lib/*.stk        # write lib/*.pl");
                println!(
                    "  stryke deconvert -d '|' app.stk      # use | as delimiter: s|old|new|g"
                );
                return 0;
            }
            s if s.starts_with('-') => {
                eprintln!("stryke deconvert: unknown option: {}", s);
                eprintln!("usage: stryke deconvert [-i] [-d DELIM] FILE...");
                return 2;
            }
            s => files.push(s.to_string()),
        }
        i += 1;
    }
    if files.is_empty() {
        eprintln!("stryke deconvert: no input files");
        eprintln!("usage: stryke deconvert [-i] [-d DELIM] FILE...");
        return 2;
    }
    let opts = stryke::deconvert::DeconvertOptions { output_delim };
    let mut errors = 0;
    for f in &files {
        let code = match std::fs::read_to_string(f) {
            Ok(c) => c,
            Err(e) => {
                eprintln!("stryke deconvert: {}: {}", f, e);
                errors += 1;
                continue;
            }
        };
        let program = match stryke::parse_with_file(&code, f) {
            Ok(p) => p,
            Err(e) => {
                eprintln!("stryke deconvert: {}: {}", f, e);
                errors += 1;
                continue;
            }
        };
        let deconverted = stryke::deconvert_to_perl_with_options(&program, &opts);
        if in_place {
            let out_path = std::path::Path::new(f).with_extension("pl");
            if let Err(e) = std::fs::write(&out_path, &deconverted) {
                eprintln!("stryke deconvert: {}: {}", out_path.display(), e);
                errors += 1;
            }
        } else {
            println!("{}", deconverted);
        }
    }
    if errors > 0 {
        1
    } else {
        0
    }
}

/// Strip shebang line; if extract mode (-x), skip everything until #!...perl line.
fn strip_shebang_and_extract(content: &str, extract: bool) -> String {
    if extract {
        // -x: skip lines until we find one starting with #! and containing "perl"
        let mut found = false;
        let mut lines = Vec::new();
        for line in content.lines() {
            if !found {
                if line.starts_with("#!") && line.contains("perl") {
                    found = true;
                    // Don't include the shebang line itself
                }
                continue;
            }
            // Stop at __END__ or __DATA__
            if line == "__END__" || line == "__DATA__" {
                break;
            }
            lines.push(line);
        }
        lines.join("\n")
    } else if content.starts_with("#!") {
        if let Some(pos) = content.find('\n') {
            content[pos + 1..].to_string()
        } else {
            String::new()
        }
    } else {
        content.to_string()
    }
}

/// Heuristic: does this string look like inline code rather than a filename?
/// Used for `stryke 'p 1+2'` (no `-e` needed).
fn looks_like_code(s: &str) -> bool {
    // Contains whitespace, Perl operators, or known statement starters
    s.contains(' ')
        || s.contains(';')
        || s.contains('|')
        || s.contains('{')
        || s.contains('(')
        || s.contains('$')
        || s.contains('@')
        || s.contains('>')
}

/// Look for a script file in PATH (-S flag).
fn find_in_path(script: &str) -> Option<String> {
    if std::path::Path::new(script).is_absolute() || script.contains('/') {
        return Some(script.to_string());
    }
    if let Ok(path_var) = std::env::var("PATH") {
        for dir in path_var.split(':') {
            let full = format!("{}/{}", dir, script);
            if std::path::Path::new(&full).exists() {
                return Some(full);
            }
        }
    }
    None
}

/// Print configuration summary (-V flag).
fn print_config(configvar: Option<&str>) {
    let version = env!("CARGO_PKG_VERSION");
    let arch = std::env::consts::ARCH;
    let os = std::env::consts::OS;
    let threads = std::thread::available_parallelism()
        .map(|n| n.get())
        .unwrap_or(1);

    if let Some(var) = configvar {
        // Print a single config variable
        let val = match var {
            "version" | "api_version" => version.to_string(),
            "archname" => format!("{}-{}", arch, os),
            "osname" => os.to_string(),
            "threads" => threads.to_string(),
            "useithreads" | "usethreads" => "define".to_string(),
            "use64bitint" | "use64bitall" => "define".to_string(),
            "cc" => "rustc".to_string(),
            "optimize" => "-O3 -lto".to_string(),
            "prefix" | "installprefix" => "/usr/local".to_string(),
            "perlpath" => "stryke".to_string(),
            _ => {
                eprintln!("Unknown config variable: {}", var);
                return;
            }
        };
        println!("{}='{}'", var, val);
    } else {
        println!("Summary of stryke v{} configuration:\n", version);
        println!("  Platform:");
        println!("    osname={}, archname={}-{}", os, arch, os);
        println!("  Compiler:");
        println!("    cc=rustc, optimize=-O3 -lto");
        println!("  Threading:");
        println!("    useithreads=define, threads={}", threads);
        println!("  Integer/Float:");
        println!("    use64bitint=define, use64bitall=define");
        println!("  Parallel extensions:");
        println!("    rayon=define, pmap=define, pmap_chunked=define, pipeline=define, par_pipeline=define, async=define, await=define, pgrep=define, pfor=define, psort=define, reduce=define, preduce=define, preduce_init=define, jit=define");
        println!("  Install:");
        println!("    perlpath=stryke");
    }
}

#[cfg(test)]
mod cli_argv_tests {
    use super::{expand_perl_bundled_argv, normalize_argv_after_dash_e, parse_cli_prelude, Cli};
    use clap::Parser;

    fn args(v: &[&str]) -> Vec<String> {
        v.iter().map(|s| (*s).to_string()).collect()
    }

    #[test]
    fn prelude_inserts_double_dash_before_script_argv_long_flags() {
        let a = args(&["stryke", "s.pl", "--regex", "--foo"]);
        let cli = parse_cli_prelude(&a).expect("expected prelude parse");
        assert_eq!(cli.script.as_deref(), Some("s.pl"));
        assert_eq!(cli.args, vec!["--regex".to_string(), "--foo".to_string()]);
    }

    #[test]
    fn prelude_with_dash_w_before_script() {
        let a = args(&["stryke", "-w", "s.pl", "--regex"]);
        let cli = parse_cli_prelude(&a).expect("expected prelude parse");
        assert!(cli.warnings);
        assert_eq!(cli.script.as_deref(), Some("s.pl"));
        assert_eq!(cli.args, vec!["--regex".to_string()]);
    }

    #[test]
    fn prelude_dash_e_then_argv_with_long_flag() {
        let a = args(&["stryke", "-e", "1", "foo", "--regex"]);
        let mut cli = parse_cli_prelude(&a).expect("expected prelude parse");
        normalize_argv_after_dash_e(&mut cli);
        assert_eq!(cli.execute, vec!["1"]);
        assert!(cli.script.is_none());
        assert_eq!(cli.args, vec!["foo".to_string(), "--regex".to_string()]);
    }

    #[test]
    fn explicit_user_double_dash_skips_prelude() {
        let a = args(&["stryke", "--", "s.pl", "x"]);
        assert!(parse_cli_prelude(&a).is_none());
    }

    #[test]
    fn bundled_lane_le_lne_maps_to_split_switches() {
        for (flag, code, expect_a, expect_n) in [
            ("-lane", "print 1", true, true),
            ("-le", "print 2", false, false),
            ("-lne", "print 3", false, true),
            ("-lnE", "say 4", false, true),
        ] {
            let a = expand_perl_bundled_argv(args(&["stryke", flag, code]));
            let cli = Cli::try_parse_from(&a).expect("parse bundled flags");
            assert!(
                cli.line_ending.is_some(),
                "{flag}: expected -l (line ending)"
            );
            assert_eq!(cli.auto_split, expect_a, "{flag}: autosplit (-a)");
            assert_eq!(cli.line_mode, expect_n, "{flag}: line loop (-n)");
            if flag.contains('E') {
                assert_eq!(cli.execute_features, vec![code]);
                assert!(cli.execute.is_empty());
            } else {
                assert_eq!(cli.execute, vec![code]);
                assert!(cli.execute_features.is_empty());
            }
        }
    }

    #[test]
    fn bundled_lpe_preserves_print_mode() {
        let a = expand_perl_bundled_argv(args(&["stryke", "-lpe", "print 1"]));
        let cli = Cli::try_parse_from(&a).expect("parse");
        assert!(cli.print_mode);
        assert_eq!(cli.execute, vec!["print 1"]);
    }

    #[test]
    fn bundled_0777_not_split() {
        let a = expand_perl_bundled_argv(args(&["stryke", "-0777", "-e", "1"]));
        assert!(
            a.contains(&"-0777".to_string()),
            "expected -0777 kept intact: {a:?}"
        );
    }

    #[test]
    fn bundled_0ne_splits_like_perl() {
        let a = expand_perl_bundled_argv(args(&["stryke", "-0ne", "print 1"]));
        let cli = Cli::try_parse_from(&a).expect("parse");
        assert_eq!(cli.execute, vec!["print 1"]);
        assert!(cli.line_mode);
    }

    #[test]
    fn bundled_f_colon_takes_rest_of_token() {
        let a = expand_perl_bundled_argv(args(&["stryke", "-F:", "-anE", "say $F[0]"]));
        let cli = Cli::try_parse_from(&a).expect("parse");
        assert_eq!(cli.field_separator.as_deref(), Some(":"));
        assert!(cli.auto_split);
        assert!(cli.line_mode);
        assert_eq!(cli.execute_features, vec!["say $F[0]"]);
    }

    #[test]
    fn bundled_f_comma_takes_rest_of_token() {
        let a = expand_perl_bundled_argv(args(&["stryke", "-F,", "-anE", "print 1"]));
        let cli = Cli::try_parse_from(&a).expect("parse");
        assert_eq!(cli.field_separator.as_deref(), Some(","));
    }

    #[test]
    fn help_alias_not_bundled_as_h_e_l_p() {
        let a = expand_perl_bundled_argv(args(&["stryke", "-help"]));
        let cli = Cli::try_parse_from(&a).expect("parse");
        assert!(cli.help);
    }

    #[test]
    fn thread_operator_not_bundled() {
        // `->>` and `~>` are threading operators, not bundled flags
        let a = expand_perl_bundled_argv(args(&["stryke", "->> 1 p"]));
        assert_eq!(a, args(&["stryke", "->> 1 p"]));

        let b = expand_perl_bundled_argv(args(&["stryke", "~> 1 p"]));
        assert_eq!(b, args(&["stryke", "~> 1 p"]));
    }
}