grift_eval 1.4.0

Lisp evaluator for the Grift Scheme language
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
//! Builtin function implementations for the evaluator.
//!
//! Contains apply_builtin, apply_binary_builtin, and related helper functions
//! for numeric operations, comparisons, and string/char operations.

use grift_parser::{ArenaIndex, Value, Builtin, fsize};

use crate::error::{ErrorKind, EvalError, EvalResult};
use crate::continuation::{TrampolineState, ContType, is_binary_builtin, EnvRef, ExprRef};
use crate::helpers::{int_pow, equal_recursive, float_floor, float_ceil, float_truncate, float_round, float_sqrt, float_pow};
use crate::{
    extract_args, builtin_unary_pred, builtin_rounding_op, builtin_div_op,
    builtin_char_to_int, builtin_char_transform,
    binary_int_cmp, binary_int_op, binary_div_op,
};

use super::Evaluator;

impl<'a, const N: usize> Evaluator<'a, N> {
    pub(super) fn apply_builtin_with_args(&mut self, builtin: Builtin, args_expr: ArenaIndex, env: EnvRef, call_expr: ArenaIndex) 
        -> Result<Option<TrampolineState>, EvalError> 
    {
        // Get first arg expression
        let first_arg = self.lisp.car(args_expr)?;
        let rest_args = self.lisp.cdr(args_expr)?;
        
        // Check for binary builtins optimization
        if is_binary_builtin(builtin) {
            // Check if exactly 2 args
            if !self.lisp.get(rest_args)?.is_nil() {
                let second_arg_expr = self.lisp.car(rest_args)?;
                let third_check = self.lisp.cdr(rest_args)?;
                if self.lisp.get(third_check)?.is_nil() {
                    // Exactly 2 args - use optimized binary path
                    // Evaluate second arg expr (store for later), then evaluate first
                    // Data: (builtin_encoded . (second_arg . (call_expr . eval_env)))
                    let builtin_encoded = Self::encode_builtin(builtin);
                    self.cont(ContType::BinaryBuiltinFirst, env).data4(builtin_encoded, second_arg_expr, call_expr, env.0)?;
                    return Ok(Some(TrampolineState::Eval { expr: ExprRef(first_arg), env }));
                }
            }
        }
        
        // General case: collect args and apply
        // Data: (builtin_encoded . (remaining_args . (collected . (call_expr . eval_env))))
        let nil = self.lisp.nil()?;
        let builtin_encoded = Self::encode_builtin(builtin);
        self.cont(ContType::BuiltinForceArg, env).data5(builtin_encoded, rest_args, nil, call_expr, env.0)?;
        
        Ok(Some(TrampolineState::Eval { expr: ExprRef(first_arg), env }))
    }
    
    /// Reverse a list (used for BuiltinForceArg fallback path)
    pub(super) fn reverse_list(&self, mut list: ArenaIndex) -> EvalResult {
        let mut result = self.lisp.nil()?;
        loop {
            match self.lisp.get(list)? {
                Value::Nil => return Ok(result),
                Value::Cons { .. } => {
                    let car = self.lisp.car(list)?;
                    let cdr = self.lisp.cdr(list)?;
                    result = self.lisp.cons(car, result)?;
                    list = cdr;
                }
                _ => return Err(self.make_error(ErrorKind::TypeError, list)),
            }
        }
    }
    
    /// Apply a builtin function (trampolined version)
    /// Arguments are already evaluated in strict mode
    pub(super) fn apply_builtin_trampolined(&mut self, builtin: Builtin, args: ArenaIndex, call_expr: ArenaIndex) 
        -> Result<TrampolineState, EvalError> 
    {
        // Special handling for error - creates error object and raises through exception system
        if matches!(builtin, Builtin::Error) {
            return self.apply_error_builtin(args, call_expr);
        }
        
        // Special handling for load - reads file and evaluates all expressions
        if matches!(builtin, Builtin::Load) {
            return self.apply_load_builtin(args, call_expr);
        }
        
        // Special handling for vector-map - needs to apply proc via trampolining
        if matches!(builtin, Builtin::VectorMap) {
            return self.apply_vector_map(args, call_expr);
        }
        
        // Special handling for vector-for-each - needs to apply proc via trampolining
        if matches!(builtin, Builtin::VectorForEach) {
            return self.apply_vector_for_each(args, call_expr);
        }
        
        // In strict evaluation, args are already evaluated values
        let result = self.apply_builtin(builtin, args, call_expr)?;
        Ok(TrampolineState::Return { val: result })
    }
    
    /// Create an R7RS error object and raise it through the exception handler chain.
    /// (error message obj ...) — R7RS §6.11
    pub(super) fn apply_error_builtin(&mut self, args: ArenaIndex, call_expr: ArenaIndex)
        -> Result<TrampolineState, EvalError>
    {
        // First arg is the message
        let msg = self.lisp.car(args)?;
        // Rest are irritants
        let irritants = self.lisp.cdr(args)?;
        // Type is Nil for standard (error msg ...) calls
        let nil = self.lisp.nil()?;
        let irritants_and_type = self.lisp.cons(irritants, nil)?;
        let error_obj = self.lisp.alloc(Value::ErrorObject { message: msg, irritants_and_type })?;
        
        // Raise through the exception handler chain
        match self.invoke_exception_handler(error_obj, false)? {
            Some(state) => Ok(state),
            None => {
                // No continuation to return to — shouldn't happen for raise
                Err(self.make_error(ErrorKind::UserError, call_expr))
            }
        }
    }
    
    /// Implement (load filename) — R7RS §6.13.
    /// Reads the file, parses all expressions, and evaluates them sequentially.
    fn apply_load_builtin(&mut self, args: ArenaIndex, call_expr: ArenaIndex)
        -> Result<TrampolineState, EvalError>
    {
        let arg = self.lisp.car(args)?;
        let path = self.extract_string_arg(arg, call_expr)?;

        // Read file content and parse all expressions while holding the io borrow.
        // We must parse before releasing the borrow, since the content &str is tied to it.
        let forms = match &mut self.io {
            Some(io) => {
                let content = match io.read_file(&path) {
                    Ok(s) => s,
                    Err(_) => return Err(self.make_error(ErrorKind::Generic, call_expr)),
                };
                grift_parser::parse_all(self.lisp, content)?
            }
            None => return Err(self.make_error(ErrorKind::Generic, call_expr)),
        };

        // Evaluate each expression sequentially at the top level.
        // Use eval_for_macro to preserve the current continuation.
        let mut current = forms;
        let mut last_val = self.lisp.void_val()?;
        while let Value::Cons { .. } = self.lisp.get(current)? {
            let form = self.lisp.car(current)?;
            last_val = self.eval_for_macro(ExprRef(form), self.global_env)?;
            current = self.lisp.cdr(current)?;
        }

        Ok(TrampolineState::Return { val: last_val })
    }
    
    /// Apply vector-map: (vector-map proc vec1 vec2 ...)
    /// Uses VectorMapStep continuation for trampolined iteration.
    fn apply_vector_map(&mut self, args: ArenaIndex, call_expr: ArenaIndex)
        -> Result<TrampolineState, EvalError>
    {
        let proc = self.lisp.car(args)?;
        let vecs_args = self.lisp.cdr(args)?;
        
        // Collect vectors into a list and validate they're all vectors of same length
        let first_vec = self.lisp.car(vecs_args)?;
        let len = match self.lisp.get(first_vec)? {
            Value::Array { .. } => self.lisp.array_len(first_vec)?,
            v => return Err(self.type_error(call_expr, "vector", v.type_name())),
        };
        
        // Validate remaining vectors have same length
        let mut current = self.lisp.cdr(vecs_args)?;
        while !self.lisp.get(current)?.is_nil() {
            let vec = self.lisp.car(current)?;
            match self.lisp.get(vec)? {
                Value::Array { .. } => {
                    let vlen = self.lisp.array_len(vec)?;
                    if vlen != len {
                        return Err(self.make_error(ErrorKind::TypeError, call_expr));
                    }
                }
                v => return Err(self.type_error(call_expr, "vector", v.type_name())),
            }
            current = self.lisp.cdr(current)?;
        }
        
        if len == 0 {
            // Empty vectors - return empty vector
            let placeholder = self.lisp.number(0)?;
            let result = self.lisp.make_array(0, placeholder)?;
            return Ok(TrampolineState::Return { val: result });
        }
        
        // Start iteration: apply proc to elements at index 0
        let first_args = self.vector_map_build_args(vecs_args, 0, call_expr)?;
        let nil = self.lisp.nil()?;
        let index_enc = self.lisp.number(0)?;
        let len_enc = self.lisp.number(len as isize)?;
        let env = self.global_env.0;
        
        // Push VectorMapStep continuation
        let d4 = self.lisp.cons(nil, call_expr)?;
        let d3 = self.lisp.cons(len_enc, d4)?;
        let d2 = self.lisp.cons(index_enc, d3)?;
        let d1 = self.lisp.cons(vecs_args, d2)?;
        let packed = self.lisp.cons(proc, d1)?;
        self.cont(ContType::VectorMapStep, EnvRef(env)).data1(packed)?;
        
        // Apply proc via ApplyForced
        self.cont(ContType::ApplyForced, EnvRef(env)).data3(first_args, env, call_expr)?;
        Ok(TrampolineState::Return { val: proc })
    }
    
    /// Apply vector-for-each: (vector-for-each proc vec1 vec2 ...)
    /// Uses VectorForEachStep continuation for trampolined iteration.
    fn apply_vector_for_each(&mut self, args: ArenaIndex, call_expr: ArenaIndex)
        -> Result<TrampolineState, EvalError>
    {
        let proc = self.lisp.car(args)?;
        let vecs_args = self.lisp.cdr(args)?;
        
        // Collect vectors and validate
        let first_vec = self.lisp.car(vecs_args)?;
        let len = match self.lisp.get(first_vec)? {
            Value::Array { .. } => self.lisp.array_len(first_vec)?,
            v => return Err(self.type_error(call_expr, "vector", v.type_name())),
        };
        
        // Validate remaining vectors
        let mut current = self.lisp.cdr(vecs_args)?;
        while !self.lisp.get(current)?.is_nil() {
            let vec = self.lisp.car(current)?;
            match self.lisp.get(vec)? {
                Value::Array { .. } => {
                    let vlen = self.lisp.array_len(vec)?;
                    if vlen != len {
                        return Err(self.make_error(ErrorKind::TypeError, call_expr));
                    }
                }
                v => return Err(self.type_error(call_expr, "vector", v.type_name())),
            }
            current = self.lisp.cdr(current)?;
        }
        
        if len == 0 {
            let void = self.lisp.void_val()?;
            return Ok(TrampolineState::Return { val: void });
        }
        
        // Start iteration: apply proc to elements at index 0
        let first_args = self.vector_map_build_args(vecs_args, 0, call_expr)?;
        let index_enc = self.lisp.number(0)?;
        let len_enc = self.lisp.number(len as isize)?;
        let env = self.global_env.0;
        
        // Push VectorForEachStep continuation
        let d3 = self.lisp.cons(len_enc, call_expr)?;
        let d2 = self.lisp.cons(index_enc, d3)?;
        let d1 = self.lisp.cons(vecs_args, d2)?;
        let packed = self.lisp.cons(proc, d1)?;
        self.cont(ContType::VectorForEachStep, EnvRef(env)).data1(packed)?;
        
        // Apply proc via ApplyForced
        self.cont(ContType::ApplyForced, EnvRef(env)).data3(first_args, env, call_expr)?;
        Ok(TrampolineState::Return { val: proc })
    }
    
    /// Apply a builtin with already-evaluated arguments
    pub(super) fn apply_builtin(&mut self, builtin: Builtin, args: ArenaIndex, call_expr: ArenaIndex) 
        -> EvalResult 
    {
        match builtin {
            Builtin::Car => {
                let arg = self.lisp.car(args)?;
                match self.lisp.get(arg)? {
                    Value::Cons { .. } => self.lisp.car(arg).map_err(Into::into),
                    // Scheme R7RS: car of empty list is an error
                    Value::Nil => Err(self.type_error(call_expr, "pair", "null")),
                    _ => Err(self.type_error(call_expr, "pair", self.lisp.get(arg)?.type_name())),
                }
            }
            
            Builtin::Cdr => {
                let arg = self.lisp.car(args)?;
                match self.lisp.get(arg)? {
                    Value::Cons { .. } => self.lisp.cdr(arg).map_err(Into::into),
                    // Scheme R7RS: cdr of empty list is an error
                    Value::Nil => Err(self.type_error(call_expr, "pair", "null")),
                    _ => Err(self.type_error(call_expr, "pair", self.lisp.get(arg)?.type_name())),
                }
            }
            
            Builtin::Cons => {
                extract_args!(self, args, a, b);
                self.lisp.cons(a, b).map_err(Into::into)
            }
            
            Builtin::List => Ok(args),
            
            // Scheme-compliant equality predicates
            // eq? and eqv? have identical semantics in this integer-only implementation
            Builtin::EqP | Builtin::EqvP => {
                extract_args!(self, args, a, b);
                
                let val_a = self.lisp.get(a)?;
                let val_b = self.lisp.get(b)?;
                
                let eq = match (val_a, val_b) {
                    (Value::Nil, Value::Nil) => true,
                    (Value::True, Value::True) => true,
                    (Value::False, Value::False) => true,
                    (Value::Number(x), Value::Number(y)) => x == y,
                    (Value::Float(x), Value::Float(y)) => x == y,
                    (Value::Char(x), Value::Char(y)) => x == y,
                    (Value::Symbol(_), Value::Symbol(_)) => self.lisp.symbol_eq(a, b)?,
                    (Value::String { len: la, data: da }, Value::String { len: lb, data: db }) => {
                        a == b || (la == lb && da == db)
                    }
                    _ => a == b,
                };
                
                self.lisp.boolean(eq).map_err(Into::into)
            }
            
            Builtin::EqualP => {
                // equal? - tests structural equality recursively
                let result = equal_recursive(self.lisp, self.lisp.car(args)?, self.lisp.car(self.lisp.cdr(args)?)?)?;
                self.lisp.boolean(result).map_err(Into::into)
            }
            
            Builtin::Null => builtin_unary_pred!(self, args, |v: Value| v.is_nil()),
            
            Builtin::Pairp => builtin_unary_pred!(self, args, |v: Value| v.is_cons()),
            
            Builtin::Numberp => builtin_unary_pred!(self, args, |v: Value| v.is_number()),
            
            Builtin::Booleanp => builtin_unary_pred!(self, args, |v: Value| v.is_boolean()),
            
            Builtin::Procedurep => builtin_unary_pred!(self, args, |v: Value| v.is_procedure()),
            
            Builtin::Symbolp => builtin_unary_pred!(self, args, |v: Value| v.is_symbol()),
            
            Builtin::Add => self.numeric_fold(args, 0, 
                |a, b| a.checked_add(b), 
                |a, b| a + b,
                call_expr),
            
            Builtin::Sub => {
                let first_idx = self.lisp.car(args)?;
                let rest = self.lisp.cdr(args)?;
                if self.lisp.get(rest)?.is_nil() {
                    // Unary minus
                    match self.lisp.get(first_idx)? {
                        Value::Number(n) => self.lisp.number(-n).map_err(Into::into),
                        Value::Float(f) => self.lisp.float(-f).map_err(Into::into),
                        v => Err(self.type_error(call_expr, "number", v.type_name())),
                    }
                } else {
                    match self.lisp.get(first_idx)? {
                        Value::Number(first) => {
                            self.numeric_fold(rest, first, 
                                |a, b| a.checked_sub(b),
                                |a, b| a - b,
                                call_expr)
                        }
                        Value::Float(first) => {
                            // Start with float accumulator
                            self.numeric_fold_float(rest, first, |a, b| a - b, call_expr)
                        }
                        v => Err(self.type_error(call_expr, "number", v.type_name())),
                    }
                }
            }
            
            Builtin::Mul => self.numeric_fold(args, 1, 
                |a, b| a.checked_mul(b),
                |a, b| a * b,
                call_expr),
            
            Builtin::Div => {
                // Division: (/ n) => 1/n, (/ n m ...) => n/m/...
                let first_idx = self.lisp.car(args)?;
                let rest = self.lisp.cdr(args)?;
                match self.lisp.get(first_idx)? {
                    Value::Number(first) => {
                        self.numeric_fold(rest, first, 
                            |a, b| if b == 0 { None } else { a.checked_div(b) },
                            |a, b| a / b,
                            call_expr)
                    }
                    Value::Float(first) => {
                        self.numeric_fold_float(rest, first, |a, b| a / b, call_expr)
                    }
                    v => Err(self.type_error(call_expr, "number", v.type_name())),
                }
            }
            
            // Division operations with zero check - generated by builtin_div_op! macro
            // Scheme modulo: result has the sign of the divisor
            Builtin::Modulo => builtin_div_op!(self, args, call_expr, |a, b| ((a % b) + b) % b),
            // Scheme remainder: result has the sign of the dividend
            Builtin::Remainder => builtin_div_op!(self, args, call_expr, |a, b| a % b),
            // Integer quotient (truncated towards zero)
            Builtin::Quotient => builtin_div_op!(self, args, call_expr, |a, b| a / b),
            
            Builtin::Expt => {
                // Exponentiation: (expt base power)
                let base_idx = self.lisp.car(args)?;
                let power_idx = self.lisp.car(self.lisp.cdr(args)?)?;
                let base_val = self.lisp.get(base_idx)?;
                let power_val = self.lisp.get(power_idx)?;
                
                match (base_val, power_val) {
                    (Value::Number(base), Value::Number(power)) => {
                        if power < 0 {
                            // Negative exponent produces float result
                            if base == 0 {
                                return Err(self.make_error(ErrorKind::DivisionByZero, call_expr));
                            }
                            let fb = base as fsize;
                            let fp = power as fsize;
                            self.lisp.float(float_pow(fb, fp)).map_err(Into::into)
                        } else {
                            let result = int_pow(base, power as usize);
                            self.lisp.number(result).map_err(Into::into)
                        }
                    }
                    _ => {
                        let base_f = match base_val {
                            Value::Number(n) => n as fsize,
                            Value::Float(f) => f,
                            v => return Err(self.type_error(call_expr, "number", v.type_name())),
                        };
                        let power_f = match power_val {
                            Value::Number(n) => n as fsize,
                            Value::Float(f) => f,
                            v => return Err(self.type_error(call_expr, "number", v.type_name())),
                        };
                        self.lisp.float(float_pow(base_f, power_f)).map_err(Into::into)
                    }
                }
            }
            
            Builtin::Sqrt => {
                // Square root - always returns float for non-perfect squares
                let arg = self.lisp.car(args)?;
                match self.lisp.get(arg)? {
                    Value::Number(n) => {
                        if n < 0 {
                            return Err(self.type_error(call_expr, "non-negative number", "negative integer"));
                        }
                        // Check for perfect square
                        let root = float_sqrt(n as fsize);
                        let iroot = root as isize;
                        if iroot * iroot == n {
                            self.lisp.number(iroot).map_err(Into::into)
                        } else {
                            self.lisp.float(root).map_err(Into::into)
                        }
                    }
                    Value::Float(f) => {
                        self.lisp.float(float_sqrt(f)).map_err(Into::into)
                    }
                    v => Err(self.type_error(call_expr, "number", v.type_name())),
                }
            }
            
            // integer? is true for exact integers and floats that are whole numbers
            Builtin::Integerp => {
                builtin_unary_pred!(self, args, |v: Value| match v {
                    Value::Number(_) => true,
                    Value::Float(f) => f.is_finite() && f == (f as isize as fsize),
                    _ => false,
                })
            }
            
            // exact? is true for integers (exact numbers)
            Builtin::Exactp => {
                let val = self.lisp.car(args)?;
                match self.lisp.get(val)? {
                    Value::Number(_) => self.lisp.boolean(true).map_err(Into::into),
                    Value::Float(_) => self.lisp.boolean(false).map_err(Into::into),
                    v => Err(self.type_error(call_expr, "number", v.type_name())),
                }
            }
            
            Builtin::Inexactp => {
                // inexact? is true for floats
                let val = self.lisp.car(args)?;
                match self.lisp.get(val)? {
                    Value::Number(_) => self.lisp.boolean(false).map_err(Into::into),
                    Value::Float(_) => self.lisp.boolean(true).map_err(Into::into),
                    v => Err(self.type_error(call_expr, "number", v.type_name())),
                }
            }
            
            Builtin::Finitep => {
                let val = self.lisp.car(args)?;
                match self.lisp.get(val)? {
                    Value::Number(_) => self.lisp.boolean(true).map_err(Into::into),
                    Value::Float(f) => self.lisp.boolean(f.is_finite()).map_err(Into::into),
                    v => Err(self.type_error(call_expr, "number", v.type_name())),
                }
            }
            
            Builtin::Infinitep => {
                let val = self.lisp.car(args)?;
                match self.lisp.get(val)? {
                    Value::Number(_) => self.lisp.boolean(false).map_err(Into::into),
                    Value::Float(f) => self.lisp.boolean(f.is_infinite()).map_err(Into::into),
                    v => Err(self.type_error(call_expr, "number", v.type_name())),
                }
            }
            
            Builtin::Nanp => {
                let val = self.lisp.car(args)?;
                match self.lisp.get(val)? {
                    Value::Number(_) => self.lisp.boolean(false).map_err(Into::into),
                    Value::Float(f) => self.lisp.boolean(f.is_nan()).map_err(Into::into),
                    v => Err(self.type_error(call_expr, "number", v.type_name())),
                }
            }
            
            // Rounding operations - identity for integers, actual rounding for floats
            Builtin::Floor => builtin_rounding_op!(self, args, call_expr, float_floor),
            Builtin::Ceiling => builtin_rounding_op!(self, args, call_expr, float_ceil),
            Builtin::Truncate => builtin_rounding_op!(self, args, call_expr, float_truncate),
            Builtin::Round => builtin_rounding_op!(self, args, call_expr, float_round),
            
            // Exactness conversion (R7RS §6.2.6)
            Builtin::ExactToInexact | Builtin::Inexact => {
                let val = self.lisp.car(args)?;
                match self.lisp.get(val)? {
                    Value::Number(n) => self.lisp.float(n as fsize).map_err(Into::into),
                    Value::Float(f) => self.lisp.float(f).map_err(Into::into),
                    v => Err(self.type_error(call_expr, "number", v.type_name())),
                }
            }
            
            Builtin::InexactToExact | Builtin::Exact => {
                let val = self.lisp.car(args)?;
                match self.lisp.get(val)? {
                    Value::Number(n) => self.lisp.number(n).map_err(Into::into),
                    Value::Float(f) => {
                        if f.is_finite() {
                            self.lisp.number(f as isize).map_err(Into::into)
                        } else {
                            Err(self.type_error(call_expr, "finite number", "infinite or nan"))
                        }
                    }
                    v => Err(self.type_error(call_expr, "number", v.type_name())),
                }
            }
            
            Builtin::Lt => self.compare_numbers(args, |a, b| a < b, call_expr),
            Builtin::Gt => self.compare_numbers(args, |a, b| a > b, call_expr),
            Builtin::Le => self.compare_numbers(args, |a, b| a <= b, call_expr),
            Builtin::Ge => self.compare_numbers(args, |a, b| a >= b, call_expr),
            Builtin::NumEq => self.compare_numbers(args, |a, b| a == b, call_expr),
            
            Builtin::Display => {
                let val = self.lisp.car(args)?;
                // Call output callback if set
                if let Some(callback) = self.output_callback {
                    callback(self.lisp, val);
                }
                // Return void (unspecified value) per R7RS
                self.lisp.void_val().map_err(Into::into)
            }
            
            Builtin::Newline => {
                // Call output callback with nil (marker for newline)
                if let Some(callback) = self.output_callback {
                    callback(self.lisp, self.lisp.nil()?);
                }
                // Return void (unspecified value) per R7RS
                self.lisp.void_val().map_err(Into::into)
            }
            
            Builtin::Error => {
                // Fallback: if called directly without trampolining, create error object
                // and return as Rust error. Normal path goes through apply_error_builtin.
                let msg = self.lisp.car(args)?;
                let irritants = self.lisp.cdr(args)?;
                let nil = self.lisp.nil()?;
                let irritants_and_type = self.lisp.cons(irritants, nil)?;
                let error_obj = self.lisp.alloc(Value::ErrorObject { message: msg, irritants_and_type })?;
                Err(self.make_error(ErrorKind::UserError, error_obj))
            }
            
            Builtin::ErrorObjectP => {
                builtin_unary_pred!(self, args, |v: Value| matches!(v, Value::ErrorObject { .. }))
            }
            
            Builtin::ErrorObjectMessage => {
                // (error-object-message error-object) — R7RS §6.11
                let arg = self.lisp.car(args)?;
                match self.lisp.get(arg)? {
                    Value::ErrorObject { message, .. } => Ok(message),
                    _ => Err(self.type_error(arg, "error-object", self.lisp.get(arg)?.type_name())),
                }
            }
            
            Builtin::ErrorObjectIrritants => {
                // (error-object-irritants error-object) — R7RS §6.11
                let arg = self.lisp.car(args)?;
                match self.lisp.get(arg)? {
                    Value::ErrorObject { irritants_and_type, .. } => {
                        self.lisp.car(irritants_and_type).map_err(Into::into)
                    }
                    _ => Err(self.type_error(arg, "error-object", self.lisp.get(arg)?.type_name())),
                }
            }
            
            Builtin::ErrorObjectType => {
                // (error-object-type error-object) — R7RS §6.11
                let arg = self.lisp.car(args)?;
                match self.lisp.get(arg)? {
                    Value::ErrorObject { irritants_and_type, .. } => {
                        self.lisp.cdr(irritants_and_type).map_err(Into::into)
                    }
                    _ => Err(self.type_error(arg, "error-object", self.lisp.get(arg)?.type_name())),
                }
            }
            
            Builtin::SetCar => {
                // (set-car! pair value) - mutate the car of a cons cell
                extract_args!(self, args, pair, value);
                
                // Verify it's a pair
                match self.lisp.get(pair)? {
                    Value::Cons { .. } => {
                        self.lisp.set_car(pair, value).map_err(Into::into)
                    }
                    _ => Err(self.make_error(ErrorKind::NotAPair, call_expr)),
                }
            }
            
            Builtin::SetCdr => {
                // (set-cdr! pair value) - mutate the cdr of a cons cell
                extract_args!(self, args, pair, value);
                
                // Verify it's a pair
                match self.lisp.get(pair)? {
                    Value::Cons { .. } => {
                        self.lisp.set_cdr(pair, value).map_err(Into::into)
                    }
                    _ => Err(self.make_error(ErrorKind::NotAPair, call_expr)),
                }
            }
            
            // ============================================================
            // Vector operations (R7RS Section 6.8)
            // ============================================================
            
            Builtin::Vectorp => {
                builtin_unary_pred!(self, args, |v: Value| matches!(v, Value::Array { .. }))
            }
            
            Builtin::MakeVector => {
                // (make-vector k) or (make-vector k fill) - create a vector
                let len_val = self.lisp.car(args)?;
                let rest = self.lisp.cdr(args)?;
                
                let len = match self.lisp.get(len_val)? {
                    Value::Number(n) if n >= 0 => n as usize,
                    _ => return Err(self.make_error(ErrorKind::TypeError, call_expr)),
                };
                
                // Default fill is 0 (unspecified in R7RS, we use 0)
                let fill = if self.lisp.get(rest)?.is_nil() {
                    self.lisp.number(0)?
                } else {
                    self.lisp.car(rest)?
                };
                
                self.lisp.make_array(len, fill).map_err(Into::into)
            }
            
            Builtin::Vector => {
                // (vector obj ...) - create vector from arguments
                self.list_to_array(args, call_expr)
            }
            
            Builtin::VectorLength => {
                // (vector-length vec) - get length of vector
                let vec = self.lisp.car(args)?;
                
                match self.lisp.get(vec)? {
                    Value::Array { .. } => {
                        let len = self.lisp.array_len(vec)?;
                        self.lisp.number(len as isize).map_err(Into::into)
                    }
                    _ => Err(self.make_error(ErrorKind::TypeError, call_expr)),
                }
            }
            
            Builtin::VectorRef => {
                // (vector-ref vec k) - get element at index k
                extract_args!(self, args, vec, index_val);
                
                let index = match self.lisp.get(index_val)? {
                    Value::Number(n) if n >= 0 => n as usize,
                    _ => return Err(self.make_error(ErrorKind::TypeError, call_expr)),
                };
                
                match self.lisp.get(vec)? {
                    Value::Array { .. } => {
                        self.lisp.array_get(vec, index).map_err(Into::into)
                    }
                    _ => Err(self.make_error(ErrorKind::TypeError, call_expr)),
                }
            }
            
            Builtin::VectorSet => {
                // (vector-set! vec k obj) - set element at index k
                extract_args!(self, args, vec, index_val, value);
                
                let index = match self.lisp.get(index_val)? {
                    Value::Number(n) if n >= 0 => n as usize,
                    _ => return Err(self.make_error(ErrorKind::TypeError, call_expr)),
                };
                
                match self.lisp.get(vec)? {
                    Value::Array { .. } => {
                        self.lisp.array_set(vec, index, value)?;
                        // R7RS: returns unspecified, we return the vector
                        Ok(vec)
                    }
                    _ => Err(self.make_error(ErrorKind::TypeError, call_expr)),
                }
            }
            
            Builtin::VectorToList => {
                // (vector->list vec) - convert vector to list
                let vec = self.lisp.car(args)?;
                
                match self.lisp.get(vec)? {
                    Value::Array { .. } => {
                        let len = self.lisp.array_len(vec)?;
                        // Build list from end to front
                        let mut result = self.lisp.nil()?;
                        for i in (0..len).rev() {
                            let elem = self.lisp.array_get(vec, i)?;
                            result = self.lisp.cons(elem, result)?;
                        }
                        Ok(result)
                    }
                    _ => Err(self.make_error(ErrorKind::TypeError, call_expr)),
                }
            }
            
            Builtin::ListToVector => {
                // (list->vector lst) - convert list to vector
                let lst = self.lisp.car(args)?;
                self.list_to_array(lst, call_expr)
            }
            
            Builtin::VectorFill => {
                // (vector-fill! vec fill) - fill vector with value
                extract_args!(self, args, vec, fill);
                
                match self.lisp.get(vec)? {
                    Value::Array { .. } => {
                        let len = self.lisp.array_len(vec)?;
                        for i in 0..len {
                            self.lisp.array_set(vec, i, fill)?;
                        }
                        // R7RS: returns unspecified, we return the vector
                        Ok(vec)
                    }
                    _ => Err(self.make_error(ErrorKind::TypeError, call_expr)),
                }
            }
            
            Builtin::VectorCopy => {
                // (vector-copy vec [start [end]]) - copy a vector
                let vec = self.lisp.car(args)?;
                
                match self.lisp.get(vec)? {
                    Value::Array { .. } => {
                        let len = self.lisp.array_len(vec)?;
                        let rest = self.lisp.cdr(args)?;
                        
                        let (start, end) = if self.lisp.get(rest)?.is_nil() {
                            (0usize, len)
                        } else {
                            let start_idx = self.lisp.car(rest)?;
                            let s = self.get_int(start_idx, call_expr)?;
                            if s < 0 { return Err(self.make_error(ErrorKind::TypeError, call_expr)); }
                            let rest2 = self.lisp.cdr(rest)?;
                            let e = if self.lisp.get(rest2)?.is_nil() {
                                len
                            } else {
                                let end_idx = self.lisp.car(rest2)?;
                                let e = self.get_int(end_idx, call_expr)?;
                                if e < 0 { return Err(self.make_error(ErrorKind::TypeError, call_expr)); }
                                e as usize
                            };
                            (s as usize, e)
                        };
                        
                        if start > len || end > len || start > end {
                            return Err(self.make_error(ErrorKind::TypeError, call_expr));
                        }
                        
                        let new_len = end - start;
                        let placeholder = self.lisp.number(0)?;
                        let new_vec = self.lisp.make_array(new_len, placeholder)?;
                        
                        for i in 0..new_len {
                            let elem = self.lisp.array_get(vec, start + i)?;
                            self.lisp.array_set(new_vec, i, elem)?;
                        }
                        
                        Ok(new_vec)
                    }
                    _ => Err(self.make_error(ErrorKind::TypeError, call_expr)),
                }
            }
            
            Builtin::VectorCopyTo => {
                // (vector-copy! to at from [start [end]]) - copy elements between vectors
                let to_vec = self.lisp.car(args)?;
                let rest = self.lisp.cdr(args)?;
                let at_idx = self.lisp.car(rest)?;
                let rest2 = self.lisp.cdr(rest)?;
                let from_vec = self.lisp.car(rest2)?;
                let rest3 = self.lisp.cdr(rest2)?;
                
                let at = match self.lisp.get(at_idx)? {
                    Value::Number(n) if n >= 0 => n as usize,
                    _ => return Err(self.make_error(ErrorKind::TypeError, call_expr)),
                };
                
                let (to_len, from_len) = match (self.lisp.get(to_vec)?, self.lisp.get(from_vec)?) {
                    (Value::Array { .. }, Value::Array { .. }) => {
                        (self.lisp.array_len(to_vec)?, self.lisp.array_len(from_vec)?)
                    }
                    _ => return Err(self.make_error(ErrorKind::TypeError, call_expr)),
                };
                
                let (start, end) = if self.lisp.get(rest3)?.is_nil() {
                    (0usize, from_len)
                } else {
                    let start_val = self.lisp.car(rest3)?;
                    let s = self.get_int(start_val, call_expr)?;
                    if s < 0 { return Err(self.make_error(ErrorKind::TypeError, call_expr)); }
                    let rest4 = self.lisp.cdr(rest3)?;
                    let e = if self.lisp.get(rest4)?.is_nil() {
                        from_len
                    } else {
                        let end_val = self.lisp.car(rest4)?;
                        let ev = self.get_int(end_val, call_expr)?;
                        if ev < 0 { return Err(self.make_error(ErrorKind::TypeError, call_expr)); }
                        ev as usize
                    };
                    (s as usize, e)
                };
                
                if start > from_len || end > from_len || start > end {
                    return Err(self.make_error(ErrorKind::TypeError, call_expr));
                }
                let count = end - start;
                if at + count > to_len {
                    return Err(self.make_error(ErrorKind::TypeError, call_expr));
                }
                
                // Use a temporary arena vector to handle overlapping ranges correctly.
                // array_get returns slot ArenaIndices; if source/dest overlap, slots may be
                // overwritten before being read. A temp vector stores independent copies.
                let placeholder = self.lisp.number(0)?;
                let temp_vec = self.lisp.make_array(count, placeholder)?;
                for i in 0..count {
                    let elem = self.lisp.array_get(from_vec, start + i)?;
                    self.lisp.array_set(temp_vec, i, elem)?;
                }
                for i in 0..count {
                    let elem = self.lisp.array_get(temp_vec, i)?;
                    self.lisp.array_set(to_vec, at + i, elem)?;
                }
                
                self.lisp.void_val().map_err(Into::into)
            }
            
            Builtin::VectorAppend => {
                // (vector-append vec ...) - concatenate vectors
                // Maximum total elements across all appended vectors
                const MAX_TOTAL_ELEMS: usize = 4096;
                let mut elems: [ArenaIndex; MAX_TOTAL_ELEMS] = [ArenaIndex::default(); MAX_TOTAL_ELEMS];
                let mut total_len = 0;
                
                let mut current = args;
                loop {
                    match self.lisp.get(current)? {
                        Value::Nil => break,
                        Value::Cons { .. } => {
                            let car = self.lisp.car(current)?;
                            let cdr = self.lisp.cdr(current)?;
                            match self.lisp.get(car)? {
                                Value::Array { .. } => {
                                    let len = self.lisp.array_len(car)?;
                                    if total_len + len > MAX_TOTAL_ELEMS {
                                        return Err(self.make_error(ErrorKind::TypeError, call_expr));
                                    }
                                    for i in 0..len {
                                        elems[total_len] = self.lisp.array_get(car, i)?;
                                        total_len += 1;
                                    }
                                }
                                v => return Err(self.type_error(call_expr, "vector", v.type_name())),
                            }
                            current = cdr;
                        }
                        _ => return Err(self.make_error(ErrorKind::TypeError, current)),
                    }
                }
                
                let placeholder = self.lisp.number(0)?;
                let result = self.lisp.make_array(total_len, placeholder)?;
                for i in 0..total_len {
                    self.lisp.array_set(result, i, elems[i])?;
                }
                Ok(result)
            }
            
            Builtin::VectorMap | Builtin::VectorForEach => {
                // These are handled in apply_builtin_trampolined
                unreachable!("vector-map and vector-for-each are trampolined")
            }
            
            Builtin::Gc => {
                // (gc) - Manually trigger garbage collection
                // Returns a list: (marked collected total-before)
                // Built right-to-left since cons prepends
                let stats = self.gc();
                let marked = self.lisp.number(stats.marked as isize)?;
                let collected = self.lisp.number(stats.collected as isize)?;
                let total_before = self.lisp.number(stats.total_before as isize)?;
                let nil = self.lisp.nil()?;
                let list = self.lisp.cons(total_before, nil)?;
                let list = self.lisp.cons(collected, list)?;
                let list = self.lisp.cons(marked, list)?;
                Ok(list)
            }
            
            Builtin::GcEnable => {
                // (gc-enable) - Enable automatic garbage collection
                self.lisp.arena().set_gc_enabled(true);
                self.lisp.true_val().map_err(Into::into)
            }
            
            Builtin::GcDisable => {
                // (gc-disable) - Disable automatic garbage collection
                self.lisp.arena().set_gc_enabled(false);
                self.lisp.false_val().map_err(Into::into)
            }
            
            Builtin::GcEnabledP => {
                // (gc-enabled?) - Check if GC is enabled
                let enabled = self.lisp.arena().is_gc_enabled();
                self.lisp.boolean(enabled).map_err(Into::into)
            }
            
            Builtin::ArenaStats => {
                // (arena-stats) - Get arena statistics
                // Returns a list: (capacity allocated free usage-percent)
                let stats = self.lisp.stats();
                let capacity = self.lisp.number(stats.capacity as isize)?;
                let allocated = self.lisp.number(stats.allocated as isize)?;
                let free = self.lisp.number(stats.free as isize)?;
                let usage = self.lisp.number(stats.usage_percent() as isize)?;
                let nil = self.lisp.nil()?;
                let list = self.lisp.cons(usage, nil)?;
                let list = self.lisp.cons(free, list)?;
                let list = self.lisp.cons(allocated, list)?;
                let list = self.lisp.cons(capacity, list)?;
                Ok(list)
            }
            
            // ============================================================
            // Character operations (R7RS Section 6.6)
            // ============================================================
            
            Builtin::Charp => {
                // (char? obj) - Check if value is a character
                builtin_unary_pred!(self, args, |v: Value| matches!(v, Value::Char(_)))
            }
            
            Builtin::CharEq => {
                // (char=? char1 char2 ...) - Character equality
                self.char_chain_compare(args, |a, b| a == b, call_expr)
            }
            
            Builtin::CharLt => {
                // (char<? char1 char2 ...) - Monotonically increasing
                self.char_chain_compare(args, |a, b| a < b, call_expr)
            }
            
            Builtin::CharGt => {
                // (char>? char1 char2 ...) - Monotonically decreasing
                self.char_chain_compare(args, |a, b| a > b, call_expr)
            }
            
            Builtin::CharLe => {
                // (char<=? char1 char2 ...) - Monotonically non-decreasing
                self.char_chain_compare(args, |a, b| a <= b, call_expr)
            }
            
            Builtin::CharGe => {
                // (char>=? char1 char2 ...) - Monotonically non-increasing
                self.char_chain_compare(args, |a, b| a >= b, call_expr)
            }
            
            Builtin::CharToInteger => builtin_char_to_int!(self, args, call_expr),
            
            Builtin::IntegerToChar => {
                // (integer->char n) - Convert Unicode code point to char
                let n = self.get_int(self.lisp.car(args)?, call_expr)?;
                if n < 0 {
                    return Err(self.type_error(call_expr, "non-negative integer", "negative integer"));
                }
                match char::from_u32(n as u32) {
                    Some(c) => self.lisp.char(c).map_err(Into::into),
                    None => Err(self.type_error(call_expr, "valid Unicode code point", "invalid code point")),
                }
            }
            
            Builtin::CharUpcase => builtin_char_transform!(self, args, call_expr, 'a', 'z', b'a', b'A'),
            Builtin::CharDowncase => builtin_char_transform!(self, args, call_expr, 'A', 'Z', b'A', b'a'),
            
            // ============================================================
            // String operations (R7RS Section 6.7)
            // ============================================================
            
            Builtin::Stringp => {
                // (string? obj) - Check if value is a string
                builtin_unary_pred!(self, args, |v: Value| matches!(v, Value::String { .. }))
            }
            
            Builtin::MakeString => {
                // (make-string k) or (make-string k char)
                let k = self.get_int(self.lisp.car(args)?, call_expr)?;
                if k < 0 {
                    return Err(self.type_error(call_expr, "non-negative integer", "negative integer"));
                }
                let rest = self.lisp.cdr(args)?;
                let fill = if self.lisp.get(rest)?.is_nil() {
                    ' ' // Default fill character
                } else {
                    self.get_char(self.lisp.car(rest)?, call_expr)?
                };
                
                // Create string of k characters
                const MAX_MAKE_STRING_LEN: usize = 1024;
                let len = k as usize;
                if len > MAX_MAKE_STRING_LEN {
                    return Err(self.make_error(ErrorKind::TypeError, call_expr));
                }
                let mut chars = ['\0'; MAX_MAKE_STRING_LEN];
                chars[..len].fill(fill);
                self.lisp.string_from_chars(&chars[..len]).map_err(Into::into)
            }
            
            Builtin::String => {
                // (string char ...) - Create string from characters
                const MAX_STRING_LEN: usize = 1024;
                let mut chars = ['\0'; MAX_STRING_LEN];
                let mut len = 0;
                let mut current = args;
                loop {
                    match self.lisp.get(current)? {
                        Value::Nil => break,
                        Value::Cons { .. } => {
                            let car = self.lisp.car(current)?;
                            let cdr = self.lisp.cdr(current)?;
                            if len >= MAX_STRING_LEN {
                                return Err(self.make_error(ErrorKind::TypeError, call_expr));
                            }
                            chars[len] = self.get_char(car, call_expr)?;
                            len += 1;
                            current = cdr;
                        }
                        _ => return Err(self.make_error(ErrorKind::TypeError, current)),
                    }
                }
                self.lisp.string_from_chars(&chars[..len]).map_err(Into::into)
            }
            
            Builtin::StringLength => {
                // (string-length string) - Get length
                let str_idx = self.lisp.car(args)?;
                match self.lisp.get(str_idx)? {
                    Value::String { .. } => {
                        let len = self.lisp.string_len(str_idx)?;
                        self.lisp.number(len as isize).map_err(Into::into)
                    }
                    v => Err(self.type_error(call_expr, "string", v.type_name())),
                }
            }
            
            Builtin::StringRef => {
                // (string-ref string k) - Get character at index
                extract_args!(self, args, str_idx, k_idx);
                match self.lisp.get(str_idx)? {
                    Value::String { len, data } => {
                        let k = self.get_int(k_idx, call_expr)?;
                        if k < 0 || (k as usize) >= len {
                            return Err(self.make_error(ErrorKind::TypeError, call_expr));
                        }
                        // Characters start at data (no header with inline length)
                        let char_slot = self.lisp.arena_index_at_offset(data, k as usize)?;
                        match self.lisp.get(char_slot)? {
                            Value::Char(c) => self.lisp.char(c).map_err(Into::into),
                            _ => Err(self.make_error(ErrorKind::TypeError, call_expr)),
                        }
                    }
                    v => Err(self.type_error(call_expr, "string", v.type_name())),
                }
            }
            
            Builtin::StringSet => {
                // (string-set! string k char) - Set character at index
                let str_idx = self.lisp.car(args)?;
                let rest = self.lisp.cdr(args)?;
                let k_idx = self.lisp.car(rest)?;
                let rest2 = self.lisp.cdr(rest)?;
                let char_arg = self.lisp.car(rest2)?;
                
                match self.lisp.get(str_idx)? {
                    Value::String { len, data } => {
                        let k = self.get_int(k_idx, call_expr)?;
                        if k < 0 || (k as usize) >= len {
                            return Err(self.make_error(ErrorKind::TypeError, call_expr));
                        }
                        let c = self.get_char(char_arg, call_expr)?;
                        // Copy-on-write: if the data is shared (interned), make a private copy
                        if self.lisp.string_data_is_interned(data)? {
                            self.lisp.string_copy_data(str_idx)?;
                        }
                        // Re-read data after potential COW
                        let current_data = match self.lisp.get(str_idx)? {
                            Value::String { data: d, .. } => d,
                            _ => return Err(self.make_error(ErrorKind::TypeError, call_expr)),
                        };
                        // Characters start at data (no header with inline length)
                        let char_slot = self.lisp.arena_index_at_offset(current_data, k as usize)?;
                        self.lisp.set(char_slot, Value::Char(c))?;
                        // Return unspecified value (we use the string itself)
                        Ok(str_idx)
                    }
                    v => Err(self.type_error(call_expr, "string", v.type_name())),
                }
            }
            
            Builtin::StringEq => {
                // (string=? string1 string2 ...) - String equality
                self.string_chain_compare(args, |ordering| ordering == core::cmp::Ordering::Equal, call_expr)
            }
            
            Builtin::StringLt => {
                // (string<? string1 string2 ...) - Monotonically increasing
                self.string_chain_compare(args, |ordering| ordering == core::cmp::Ordering::Less, call_expr)
            }
            
            Builtin::StringGt => {
                // (string>? string1 string2 ...) - Monotonically decreasing
                self.string_chain_compare(args, |ordering| ordering == core::cmp::Ordering::Greater, call_expr)
            }
            
            Builtin::StringLe => {
                // (string<=? string1 string2 ...) - Monotonically non-decreasing
                self.string_chain_compare(args, |ordering| ordering != core::cmp::Ordering::Greater, call_expr)
            }
            
            Builtin::StringGe => {
                // (string>=? string1 string2 ...) - Monotonically non-increasing
                self.string_chain_compare(args, |ordering| ordering != core::cmp::Ordering::Less, call_expr)
            }
            
            Builtin::StringAppend => {
                // (string-append string ...) - Concatenate strings
                const MAX_TOTAL_LEN: usize = 4096;
                let mut chars = ['\0'; MAX_TOTAL_LEN];
                let mut total_len = 0;
                
                let mut current = args;
                loop {
                    match self.lisp.get(current)? {
                        Value::Nil => break,
                        Value::Cons { .. } => {
                            let car = self.lisp.car(current)?;
                            let cdr = self.lisp.cdr(current)?;
                            match self.lisp.get(car)? {
                                Value::String { len, data } => {
                                    if total_len + len > MAX_TOTAL_LEN {
                                        return Err(self.make_error(ErrorKind::TypeError, call_expr));
                                    }
                                    for i in 0..len {
                                        // Characters start at data (no header with inline length)
                                        let char_slot = self.lisp.arena_index_at_offset(data, i)?;
                                        match self.lisp.get(char_slot)? {
                                            Value::Char(c) => {
                                                chars[total_len] = c;
                                                total_len += 1;
                                            }
                                            _ => return Err(self.make_error(ErrorKind::TypeError, call_expr)),
                                        }
                                    }
                                }
                                v => return Err(self.type_error(call_expr, "string", v.type_name())),
                            }
                            current = cdr;
                        }
                        _ => return Err(self.make_error(ErrorKind::TypeError, current)),
                    }
                }
                
                self.lisp.string_from_chars(&chars[..total_len]).map_err(Into::into)
            }
            
            Builtin::StringToList => {
                // (string->list string) - Convert string to list of characters
                let str_idx = self.lisp.car(args)?;
                match self.lisp.get(str_idx)? {
                    Value::String { len, data } => {
                        let mut result = self.lisp.nil()?;
                        // Build list from end to start
                        for i in (0..len).rev() {
                            // Characters start at data (no header with inline length)
                            let char_slot = self.lisp.arena_index_at_offset(data, i)?;
                            match self.lisp.get(char_slot)? {
                                Value::Char(c) => {
                                    let char_val = self.lisp.char(c)?;
                                    result = self.lisp.cons(char_val, result)?;
                                }
                                _ => return Err(self.make_error(ErrorKind::TypeError, call_expr)),
                            }
                        }
                        Ok(result)
                    }
                    v => Err(self.type_error(call_expr, "string", v.type_name())),
                }
            }
            
            Builtin::ListToString => {
                // (list->string list) - Convert list of characters to string
                const MAX_STRING_LEN: usize = 1024;
                let mut chars = ['\0'; MAX_STRING_LEN];
                let mut len = 0;
                
                let mut current = self.lisp.car(args)?;
                loop {
                    match self.lisp.get(current)? {
                        Value::Nil => break,
                        Value::Cons { .. } => {
                            let car = self.lisp.car(current)?;
                            let cdr = self.lisp.cdr(current)?;
                            if len >= MAX_STRING_LEN {
                                return Err(self.make_error(ErrorKind::TypeError, call_expr));
                            }
                            chars[len] = self.get_char(car, call_expr)?;
                            len += 1;
                            current = cdr;
                        }
                        _ => return Err(self.make_error(ErrorKind::TypeError, current)),
                    }
                }
                
                self.lisp.string_from_chars(&chars[..len]).map_err(Into::into)
            }
            
            Builtin::Substring => {
                // (substring string start end) - Extract substring
                let str_idx = self.lisp.car(args)?;
                let rest = self.lisp.cdr(args)?;
                let start_idx = self.lisp.car(rest)?;
                let rest2 = self.lisp.cdr(rest)?;
                let end_idx = self.lisp.car(rest2)?;
                
                match self.lisp.get(str_idx)? {
                    Value::String { len, data } => {
                        let start = self.get_int(start_idx, call_expr)?;
                        let end = self.get_int(end_idx, call_expr)?;
                        
                        if start < 0 || end < 0 || (start as usize) > len || (end as usize) > len || start > end {
                            return Err(self.make_error(ErrorKind::TypeError, call_expr));
                        }
                        
                        let sub_len = (end - start) as usize;
                        const MAX_SUBSTRING_LEN: usize = 1024;
                        let mut chars = ['\0'; MAX_SUBSTRING_LEN];
                        if sub_len > MAX_SUBSTRING_LEN {
                            return Err(self.make_error(ErrorKind::TypeError, call_expr));
                        }
                        
                        for i in 0..sub_len {
                            // Characters start at data (no header with inline length)
                            let char_slot = self.lisp.arena_index_at_offset(data, (start as usize) + i)?;
                            match self.lisp.get(char_slot)? {
                                Value::Char(c) => chars[i] = c,
                                _ => return Err(self.make_error(ErrorKind::TypeError, call_expr)),
                            }
                        }
                        
                        self.lisp.string_from_chars(&chars[..sub_len]).map_err(Into::into)
                    }
                    v => Err(self.type_error(call_expr, "string", v.type_name())),
                }
            }
            
            Builtin::StringCopy => {
                // (string-copy string [start [end]]) - Copy a string
                let str_idx = self.lisp.car(args)?;
                match self.lisp.get(str_idx)? {
                    Value::String { len, data } => {
                        let rest = self.lisp.cdr(args)?;
                        let (start, end) = if self.lisp.get(rest)?.is_nil() {
                            (0usize, len)
                        } else {
                            let start_val = self.lisp.car(rest)?;
                            let s = self.get_int(start_val, call_expr)?;
                            if s < 0 { return Err(self.make_error(ErrorKind::TypeError, call_expr)); }
                            let rest2 = self.lisp.cdr(rest)?;
                            let e = if self.lisp.get(rest2)?.is_nil() {
                                len
                            } else {
                                let end_val = self.lisp.car(rest2)?;
                                let ev = self.get_int(end_val, call_expr)?;
                                if ev < 0 { return Err(self.make_error(ErrorKind::TypeError, call_expr)); }
                                ev as usize
                            };
                            (s as usize, e)
                        };
                        
                        if start > len || end > len || start > end {
                            return Err(self.make_error(ErrorKind::TypeError, call_expr));
                        }
                        
                        let sub_len = end - start;
                        const MAX_STRING_LEN: usize = 1024;
                        let mut chars = ['\0'; MAX_STRING_LEN];
                        if sub_len > MAX_STRING_LEN {
                            return Err(self.make_error(ErrorKind::TypeError, call_expr));
                        }
                        
                        for i in 0..sub_len {
                            let char_slot = self.lisp.arena_index_at_offset(data, start + i)?;
                            match self.lisp.get(char_slot)? {
                                Value::Char(c) => chars[i] = c,
                                _ => return Err(self.make_error(ErrorKind::TypeError, call_expr)),
                            }
                        }
                        
                        self.lisp.string_from_chars(&chars[..sub_len]).map_err(Into::into)
                    }
                    v => Err(self.type_error(call_expr, "string", v.type_name())),
                }
            }
            
            Builtin::StringCopyTo => {
                // (string-copy! to at from [start [end]]) - copy characters between strings
                let to_str = self.lisp.car(args)?;
                let rest = self.lisp.cdr(args)?;
                let at_idx = self.lisp.car(rest)?;
                let rest2 = self.lisp.cdr(rest)?;
                let from_str = self.lisp.car(rest2)?;
                let rest3 = self.lisp.cdr(rest2)?;
                
                let at = match self.lisp.get(at_idx)? {
                    Value::Number(n) if n >= 0 => n as usize,
                    _ => return Err(self.make_error(ErrorKind::TypeError, call_expr)),
                };
                
                let (to_len, _to_data, from_len, from_data) = match (self.lisp.get(to_str)?, self.lisp.get(from_str)?) {
                    (Value::String { len: tl, data: td }, Value::String { len: fl, data: fd }) => (tl, td, fl, fd),
                    _ => return Err(self.make_error(ErrorKind::TypeError, call_expr)),
                };
                
                let (start, end) = if self.lisp.get(rest3)?.is_nil() {
                    (0usize, from_len)
                } else {
                    let start_val = self.lisp.car(rest3)?;
                    let s = self.get_int(start_val, call_expr)?;
                    if s < 0 { return Err(self.make_error(ErrorKind::TypeError, call_expr)); }
                    let rest4 = self.lisp.cdr(rest3)?;
                    let e = if self.lisp.get(rest4)?.is_nil() {
                        from_len
                    } else {
                        let end_val = self.lisp.car(rest4)?;
                        let ev = self.get_int(end_val, call_expr)?;
                        if ev < 0 { return Err(self.make_error(ErrorKind::TypeError, call_expr)); }
                        ev as usize
                    };
                    (s as usize, e)
                };
                
                if start > from_len || end > from_len || start > end {
                    return Err(self.make_error(ErrorKind::TypeError, call_expr));
                }
                let count = end - start;
                if at + count > to_len {
                    return Err(self.make_error(ErrorKind::TypeError, call_expr));
                }
                
                // Copy-on-write: ensure destination is mutable
                if self.lisp.string_data_is_interned(match self.lisp.get(to_str)? {
                    Value::String { data, .. } => data,
                    _ => return Err(self.make_error(ErrorKind::TypeError, call_expr)),
                })? {
                    self.lisp.string_copy_data(to_str)?;
                }
                
                // Read source characters into temp buffer for overlapping safety
                const MAX_STRING_COPY: usize = 4096;
                if count > MAX_STRING_COPY {
                    return Err(self.make_error(ErrorKind::TypeError, call_expr));
                }
                let mut temp = ['\0'; MAX_STRING_COPY];
                for i in 0..count {
                    let char_slot = self.lisp.arena_index_at_offset(from_data, start + i)?;
                    match self.lisp.get(char_slot)? {
                        Value::Char(c) => temp[i] = c,
                        _ => return Err(self.make_error(ErrorKind::TypeError, call_expr)),
                    }
                }
                
                // Write to destination
                let to_data = match self.lisp.get(to_str)? {
                    Value::String { data, .. } => data,
                    _ => return Err(self.make_error(ErrorKind::TypeError, call_expr)),
                };
                for i in 0..count {
                    let char_slot = self.lisp.arena_index_at_offset(to_data, at + i)?;
                    self.lisp.set(char_slot, Value::Char(temp[i]))?;
                }
                
                self.lisp.void_val().map_err(Into::into)
            }
            
            Builtin::StringFill => {
                // (string-fill! string fill [start [end]]) - fill string with character
                let str_idx = self.lisp.car(args)?;
                let rest = self.lisp.cdr(args)?;
                let fill_arg = self.lisp.car(rest)?;
                let rest2 = self.lisp.cdr(rest)?;
                
                let fill_char = self.get_char(fill_arg, call_expr)?;
                
                match self.lisp.get(str_idx)? {
                    Value::String { len, data } => {
                        let (start, end) = if self.lisp.get(rest2)?.is_nil() {
                            (0usize, len)
                        } else {
                            let start_val = self.lisp.car(rest2)?;
                            let s = self.get_int(start_val, call_expr)?;
                            if s < 0 { return Err(self.make_error(ErrorKind::TypeError, call_expr)); }
                            let rest3 = self.lisp.cdr(rest2)?;
                            let e = if self.lisp.get(rest3)?.is_nil() {
                                len
                            } else {
                                let end_val = self.lisp.car(rest3)?;
                                let ev = self.get_int(end_val, call_expr)?;
                                if ev < 0 { return Err(self.make_error(ErrorKind::TypeError, call_expr)); }
                                ev as usize
                            };
                            (s as usize, e)
                        };
                        
                        if start > len || end > len || start > end {
                            return Err(self.make_error(ErrorKind::TypeError, call_expr));
                        }
                        
                        // Copy-on-write: ensure string is mutable
                        if self.lisp.string_data_is_interned(data)? {
                            self.lisp.string_copy_data(str_idx)?;
                        }
                        
                        // Re-read data after potential COW
                        let current_data = match self.lisp.get(str_idx)? {
                            Value::String { data: d, .. } => d,
                            _ => return Err(self.make_error(ErrorKind::TypeError, call_expr)),
                        };
                        
                        for i in start..end {
                            let char_slot = self.lisp.arena_index_at_offset(current_data, i)?;
                            self.lisp.set(char_slot, Value::Char(fill_char))?;
                        }
                        
                        self.lisp.void_val().map_err(Into::into)
                    }
                    v => Err(self.type_error(call_expr, "string", v.type_name())),
                }
            }
            
            Builtin::StringCiLt => {
                // (string-ci<? string1 string2 ...) - Case-insensitive less than
                self.string_ci_chain_compare(args, |ordering| ordering == core::cmp::Ordering::Less, call_expr)
            }
            
            Builtin::StringCiGt => {
                // (string-ci>? string1 string2 ...) - Case-insensitive greater than
                self.string_ci_chain_compare(args, |ordering| ordering == core::cmp::Ordering::Greater, call_expr)
            }
            
            Builtin::StringCiLe => {
                // (string-ci<=? string1 string2 ...) - Case-insensitive less than or equal
                self.string_ci_chain_compare(args, |ordering| ordering != core::cmp::Ordering::Greater, call_expr)
            }
            
            Builtin::StringCiGe => {
                // (string-ci>=? string1 string2 ...) - Case-insensitive greater than or equal
                self.string_ci_chain_compare(args, |ordering| ordering != core::cmp::Ordering::Less, call_expr)
            }
            
            // ============================================================
            // Syntax-case support (R6RS Chapter 11)
            // ============================================================
            
            Builtin::Identifierp => {
                // (identifier? x) - Check if x is an identifier
                // An identifier is either a symbol or a syntax object wrapping a symbol
                let arg = self.lisp.car(args)?;
                let is_id = match self.lisp.get(arg)? {
                    Value::Symbol(_) => true,
                    Value::Syntax { expr, .. } => {
                        matches!(self.lisp.get(expr)?, Value::Symbol(_))
                    }
                    _ => false,
                };
                self.lisp.boolean(is_id).map_err(Into::into)
            }
            
            Builtin::BoundIdentifierEq => {
                // (bound-identifier=? id1 id2) - Check if two identifiers have same name and marks
                extract_args!(self, args, id1, id2);
                let result = self.bound_identifier_eq(id1, id2)?;
                self.lisp.boolean(result).map_err(Into::into)
            }
            
            Builtin::FreeIdentifierEq => {
                // (free-identifier=? id1 id2) - Check if two identifiers resolve to same binding
                extract_args!(self, args, id1, id2);
                let result = self.free_identifier_eq(id1, id2)?;
                self.lisp.boolean(result).map_err(Into::into)
            }
            
            Builtin::SyntaxToDatum | Builtin::SyntaxE | Builtin::SyntaxObjectToDatum => {
                // syntax->datum / syntax-e / syntax-object->datum - Strip syntax wrapper
                let stx = self.lisp.car(args)?;
                self.syntax_to_datum_recursive(stx)
            }
            
            Builtin::DatumToSyntax | Builtin::DatumToSyntaxObject => {
                // datum->syntax / datum->syntax-object - Wrap datum with syntax context
                extract_args!(self, args, template_id, datum);
                self.datum_to_syntax(template_id, datum)
            }
            
            Builtin::GenerateTemporaries => {
                // (generate-temporaries list) - Generate a list of fresh identifiers
                // For each element in the input list, generate a unique temporary identifier
                let input = self.lisp.car(args)?;
                self.generate_temporaries(input)
            }
            
            Builtin::SymbolToString => {
                // (symbol->string sym) - Convert symbol to string
                let arg = self.lisp.car(args)?;
                match self.lisp.get(arg)? {
                    Value::Symbol(chars) => {
                        // The symbol stores a reference to a String value
                        // We return that string directly
                        Ok(chars)
                    }
                    _ => Err(self.type_error(call_expr, "symbol", self.lisp.get(arg)?.type_name())),
                }
            }
            
            Builtin::StringToSymbol => {
                // (string->symbol str) - Convert string to symbol
                let arg = self.lisp.car(args)?;
                match self.lisp.get(arg)? {
                    Value::String { .. } => {
                        // Create/intern a symbol with this string
                        self.lisp.symbol_from_string(arg).map_err(Into::into)
                    }
                    _ => Err(self.type_error(call_expr, "string", self.lisp.get(arg)?.type_name())),
                }
            }
            
            Builtin::NumberToString => {
                // (number->string num) - Convert number to string
                let arg = self.lisp.car(args)?;
                match self.lisp.get(arg)? {
                    Value::Number(n) => {
                        // Convert integer to string using stack buffer (no_std compatible)
                        let mut buf = [0u8; 21]; // enough for i64 including sign
                        let mut pos = buf.len();
                        let negative = n < 0;
                        let mut val = if negative { 
                            // Handle isize::MIN by working with unsigned
                            (n as isize).unsigned_abs()
                        } else { 
                            n as usize 
                        };
                        
                        if val == 0 {
                            pos -= 1;
                            buf[pos] = b'0';
                        } else {
                            while val > 0 {
                                pos -= 1;
                                buf[pos] = b'0' + (val % 10) as u8;
                                val /= 10;
                            }
                        }
                        
                        if negative {
                            pos -= 1;
                            buf[pos] = b'-';
                        }
                        
                        let len = buf.len() - pos;
                        let mut chars = ['\0'; 21];
                        for i in 0..len {
                            chars[i] = buf[pos + i] as char;
                        }
                        self.lisp.string_from_chars(&chars[..len]).map_err(Into::into)
                    }
                    Value::Float(f) => {
                        // Convert float to string using stack buffer
                        let mut chars = ['\0'; 32];
                        let len = format_float_to_chars(f, &mut chars);
                        self.lisp.string_from_chars(&chars[..len]).map_err(Into::into)
                    }
                    v => Err(self.type_error(call_expr, "number", v.type_name())),
                }
            }
            
            Builtin::StringToNumber => {
                // (string->number str) - Convert string to number, or #f if invalid
                let arg = self.lisp.car(args)?;
                match self.lisp.get(arg)? {
                    Value::String { len, data } => {
                        if len == 0 {
                            return self.lisp.false_val().map_err(Into::into);
                        }
                        // Read chars into stack buffer
                        let mut buf = [0u8; 32];
                        if len > buf.len() {
                            return self.lisp.false_val().map_err(Into::into);
                        }
                        for i in 0..len {
                            let char_slot = self.lisp.arena_index_at_offset(data, i)?;
                            match self.lisp.get(char_slot)? {
                                Value::Char(c) => {
                                    if !c.is_ascii() {
                                        return self.lisp.false_val().map_err(Into::into);
                                    }
                                    buf[i] = c as u8;
                                }
                                _ => return self.lisp.false_val().map_err(Into::into),
                            }
                        }
                        // Parse the number - try integer first, then float
                        let s = core::str::from_utf8(&buf[..len]).unwrap_or("");
                        
                        // Check for special float constants
                        match s {
                            "+inf.0" => return self.lisp.float(fsize::INFINITY).map_err(Into::into),
                            "-inf.0" => return self.lisp.float(fsize::NEG_INFINITY).map_err(Into::into),
                            "+nan.0" | "-nan.0" => return self.lisp.float(fsize::NAN).map_err(Into::into),
                            _ => {}
                        }
                        
                        // Try integer parse first
                        if let Ok(n) = s.parse::<isize>() {
                            return self.lisp.number(n).map_err(Into::into);
                        }
                        // Try float parse
                        if let Some(f) = parse_float_no_std(s) {
                            return self.lisp.float(f).map_err(Into::into);
                        }
                        self.lisp.false_val().map_err(Into::into)
                    }
                    v => Err(self.type_error(call_expr, "string", v.type_name())),
                }
            }

            // ================================================================
            // Port operations (R7RS §6.13)
            // ================================================================

            Builtin::Portp => {
                let arg = self.lisp.car(args)?;
                let is_port = matches!(self.lisp.get(arg)?, Value::Port(_));
                self.lisp.boolean(is_port).map_err(Into::into)
            }

            Builtin::InputPortp => {
                let arg = self.lisp.car(args)?;
                let result = match self.lisp.get(arg)? {
                    Value::Port(pid) => {
                        if let Some(ref io) = self.io { io.is_input_port(pid) } else { false }
                    }
                    _ => false,
                };
                self.lisp.boolean(result).map_err(Into::into)
            }

            Builtin::OutputPortp => {
                let arg = self.lisp.car(args)?;
                let result = match self.lisp.get(arg)? {
                    Value::Port(pid) => {
                        if let Some(ref io) = self.io { io.is_output_port(pid) } else { false }
                    }
                    _ => false,
                };
                self.lisp.boolean(result).map_err(Into::into)
            }

            Builtin::CurrentInputPort => {
                self.lisp.port(grift_parser::PortId::STDIN).map_err(Into::into)
            }

            Builtin::CurrentOutputPort => {
                self.lisp.port(grift_parser::PortId::STDOUT).map_err(Into::into)
            }

            Builtin::CurrentErrorPort => {
                self.lisp.port(grift_parser::PortId::STDERR).map_err(Into::into)
            }

            Builtin::ClosePort | Builtin::CloseInputPort | Builtin::CloseOutputPort => {
                let arg = self.lisp.car(args)?;
                match self.lisp.get(arg)? {
                    Value::Port(pid) => {
                        if let Some(ref mut io) = self.io {
                            io.close_port(pid).map_err(|_| self.make_error(ErrorKind::Generic, call_expr))?;
                        }
                        self.lisp.void_val().map_err(Into::into)
                    }
                    v => Err(self.type_error(call_expr, "port", v.type_name())),
                }
            }

            Builtin::ReadChar => {
                // (read-char) or (read-char port)
                let pid = self.extract_input_port(args, call_expr)?;
                match &mut self.io {
                    Some(io) => {
                        match io.read_char(pid) {
                            Ok(c) => self.lisp.alloc(Value::Char(c)).map_err(Into::into),
                            Err(grift_parser::IoErrorKind::Eof) => self.lisp.eof().map_err(Into::into),
                            Err(_) => Err(self.make_error(ErrorKind::Generic, call_expr)),
                        }
                    }
                    None => Err(self.make_error(ErrorKind::Generic, call_expr)),
                }
            }

            Builtin::PeekChar => {
                // (peek-char) or (peek-char port)
                let pid = self.extract_input_port(args, call_expr)?;
                match &mut self.io {
                    Some(io) => {
                        match io.peek_char(pid) {
                            Ok(c) => self.lisp.alloc(Value::Char(c)).map_err(Into::into),
                            Err(grift_parser::IoErrorKind::Eof) => self.lisp.eof().map_err(Into::into),
                            Err(_) => Err(self.make_error(ErrorKind::Generic, call_expr)),
                        }
                    }
                    None => Err(self.make_error(ErrorKind::Generic, call_expr)),
                }
            }

            Builtin::CharReadyp => {
                // (char-ready?) or (char-ready? port)
                let pid = self.extract_input_port(args, call_expr)?;
                match &mut self.io {
                    Some(io) => {
                        let ready = io.char_ready(pid).unwrap_or(false);
                        self.lisp.boolean(ready).map_err(Into::into)
                    }
                    None => self.lisp.boolean(false).map_err(Into::into),
                }
            }

            Builtin::WriteChar => {
                // (write-char char) or (write-char char port)
                let ch_arg = self.lisp.car(args)?;
                let c = match self.lisp.get(ch_arg)? {
                    Value::Char(c) => c,
                    v => return Err(self.type_error(call_expr, "char", v.type_name())),
                };
                let rest = self.lisp.cdr(args)?;
                let pid = if self.lisp.get(rest)?.is_nil() {
                    grift_parser::PortId::STDOUT
                } else {
                    let port_arg = self.lisp.car(rest)?;
                    match self.lisp.get(port_arg)? {
                        Value::Port(pid) => pid,
                        v => return Err(self.type_error(call_expr, "port", v.type_name())),
                    }
                };
                if let Some(ref mut io) = self.io {
                    io.write_char(pid, c).map_err(|_| self.make_error(ErrorKind::Generic, call_expr))?;
                } else if let Some(callback) = self.output_callback {
                    callback(self.lisp, ch_arg);
                }
                self.lisp.void_val().map_err(Into::into)
            }

            Builtin::Write => {
                // (write obj) or (write obj port)
                let val = self.lisp.car(args)?;
                let rest = self.lisp.cdr(args)?;
                let pid = if self.lisp.get(rest)?.is_nil() {
                    grift_parser::PortId::STDOUT
                } else {
                    let port_arg = self.lisp.car(rest)?;
                    match self.lisp.get(port_arg)? {
                        Value::Port(pid) => pid,
                        v => return Err(self.type_error(call_expr, "port", v.type_name())),
                    }
                };
                if let Some(ref mut io) = self.io {
                    // Use DisplayValue (write mode with quotes) through a fmt::Write adapter
                    let dv = grift_parser::DisplayValue::new(val, self.lisp);
                    let mut writer = IoPortWriter { io: &mut **io, port: pid, error: false };
                    use core::fmt::Write;
                    let _ = write!(writer, "{}", dv);
                    if writer.error {
                        return Err(self.make_error(ErrorKind::Generic, call_expr));
                    }
                } else if let Some(callback) = self.output_callback {
                    callback(self.lisp, val);
                }
                self.lisp.void_val().map_err(Into::into)
            }

            Builtin::Read => {
                // (read) or (read port)
                let pid = self.extract_input_port(args, call_expr)?;
                self.apply_read_builtin(pid, call_expr)
            }

            Builtin::EofObject => {
                self.lisp.eof().map_err(Into::into)
            }

            Builtin::EofObjectp => {
                let arg = self.lisp.car(args)?;
                let is_eof = matches!(self.lisp.get(arg)?, Value::Eof);
                self.lisp.boolean(is_eof).map_err(Into::into)
            }

            Builtin::OpenInputString => {
                // (open-input-string str)
                let arg = self.lisp.car(args)?;
                match self.lisp.get(arg)? {
                    Value::String { len, data } => {
                        // Extract string content into stack buffer
                        let mut buf = [0u8; 1024];
                        let mut byte_len = 0;
                        for i in 0..len {
                            let char_slot = self.lisp.arena_index_at_offset(data, i)?;
                            if let Value::Char(c) = self.lisp.get(char_slot)? {
                                let enc_len = c.len_utf8();
                                if byte_len + enc_len > buf.len() { break; }
                                c.encode_utf8(&mut buf[byte_len..]);
                                byte_len += enc_len;
                            }
                        }
                        let s = core::str::from_utf8(&buf[..byte_len])
                            .map_err(|_| self.make_error(ErrorKind::Generic, call_expr))?;
                        match &mut self.io {
                            Some(io) => {
                                let pid = io.open_input_string(s)
                                    .map_err(|_| self.make_error(ErrorKind::Generic, call_expr))?;
                                self.lisp.port(pid).map_err(Into::into)
                            }
                            None => Err(self.make_error(ErrorKind::Generic, call_expr)),
                        }
                    }
                    v => Err(self.type_error(call_expr, "string", v.type_name())),
                }
            }

            Builtin::OpenOutputString => {
                // (open-output-string)
                match &mut self.io {
                    Some(io) => {
                        let pid = io.open_output_string()
                            .map_err(|_| self.make_error(ErrorKind::Generic, call_expr))?;
                        self.lisp.port(pid).map_err(Into::into)
                    }
                    None => Err(self.make_error(ErrorKind::Generic, call_expr)),
                }
            }

            Builtin::GetOutputString => {
                // (get-output-string port)
                let arg = self.lisp.car(args)?;
                match self.lisp.get(arg)? {
                    Value::Port(pid) => {
                        match &self.io {
                            Some(io) => {
                                let s = io.get_output_string(pid)
                                    .map_err(|_| self.make_error(ErrorKind::Generic, call_expr))?;
                                self.lisp.string(s).map_err(Into::into)
                            }
                            None => Err(self.make_error(ErrorKind::Generic, call_expr)),
                        }
                    }
                    v => Err(self.type_error(call_expr, "port", v.type_name())),
                }
            }

            Builtin::ReadLine => {
                // (read-line) or (read-line port)
                let pid = self.extract_input_port(args, call_expr)?;
                let io = match &mut self.io {
                    Some(io) => io,
                    None => return Err(self.make_error(ErrorKind::Generic, call_expr)),
                };
                let mut buf = [0u8; 2048];
                let mut byte_len = 0;
                let mut got_char = false;
                loop {
                    match io.read_char(pid) {
                        Ok('\n') => break,
                        Ok('\r') => {
                            // Handle \r\n: peek to consume \n if present
                            if let Ok('\n') = io.peek_char(pid) {
                                let _ = io.read_char(pid);
                            }
                            break;
                        }
                        Ok(c) => {
                            got_char = true;
                            let enc_len = c.len_utf8();
                            if byte_len + enc_len > buf.len() { break; }
                            c.encode_utf8(&mut buf[byte_len..]);
                            byte_len += enc_len;
                        }
                        Err(grift_parser::IoErrorKind::Eof) => {
                            if !got_char {
                                return self.lisp.eof().map_err(Into::into);
                            }
                            break;
                        }
                        Err(_) => return Err(self.make_error(ErrorKind::Generic, call_expr)),
                    }
                }
                let s = core::str::from_utf8(&buf[..byte_len])
                    .map_err(|_| self.make_error(ErrorKind::Generic, call_expr))?;
                self.lisp.string(s).map_err(Into::into)
            }

            Builtin::ReadString => {
                // (read-string k) or (read-string k port)
                let k_arg = self.lisp.car(args)?;
                let k = match self.lisp.get(k_arg)? {
                    Value::Number(n) if n >= 0 => n as usize,
                    v => return Err(self.type_error(call_expr, "non-negative integer", v.type_name())),
                };
                let rest = self.lisp.cdr(args)?;
                let pid = if self.lisp.get(rest)?.is_nil() {
                    grift_parser::PortId::STDIN
                } else {
                    let port_arg = self.lisp.car(rest)?;
                    match self.lisp.get(port_arg)? {
                        Value::Port(pid) => pid,
                        v => return Err(self.type_error(call_expr, "port", v.type_name())),
                    }
                };
                let io = match &mut self.io {
                    Some(io) => io,
                    None => return Err(self.make_error(ErrorKind::Generic, call_expr)),
                };
                let mut buf = [0u8; 2048];
                let mut byte_len = 0;
                let mut chars_read = 0;
                while chars_read < k {
                    match io.read_char(pid) {
                        Ok(c) => {
                            let enc_len = c.len_utf8();
                            if byte_len + enc_len > buf.len() { break; }
                            c.encode_utf8(&mut buf[byte_len..]);
                            byte_len += enc_len;
                            chars_read += 1;
                        }
                        Err(grift_parser::IoErrorKind::Eof) => break,
                        Err(_) => return Err(self.make_error(ErrorKind::Generic, call_expr)),
                    }
                }
                if chars_read == 0 {
                    return self.lisp.eof().map_err(Into::into);
                }
                let s = core::str::from_utf8(&buf[..byte_len])
                    .map_err(|_| self.make_error(ErrorKind::Generic, call_expr))?;
                self.lisp.string(s).map_err(Into::into)
            }

            Builtin::WriteShared | Builtin::WriteSimple => {
                // (write-shared obj [port]) / (write-simple obj [port])
                // In this implementation, both behave like write since we don't
                // have circular structures.
                let val = self.lisp.car(args)?;
                let rest = self.lisp.cdr(args)?;
                let pid = if self.lisp.get(rest)?.is_nil() {
                    grift_parser::PortId::STDOUT
                } else {
                    let port_arg = self.lisp.car(rest)?;
                    match self.lisp.get(port_arg)? {
                        Value::Port(pid) => pid,
                        v => return Err(self.type_error(call_expr, "port", v.type_name())),
                    }
                };
                if let Some(ref mut io) = self.io {
                    let dv = grift_parser::DisplayValue::new(val, self.lisp);
                    let mut writer = IoPortWriter { io: &mut **io, port: pid, error: false };
                    use core::fmt::Write;
                    let _ = write!(writer, "{}", dv);
                    if writer.error {
                        return Err(self.make_error(ErrorKind::Generic, call_expr));
                    }
                } else if let Some(callback) = self.output_callback {
                    callback(self.lisp, val);
                }
                self.lisp.void_val().map_err(Into::into)
            }

            Builtin::TextualPortp => {
                let arg = self.lisp.car(args)?;
                let result = match self.lisp.get(arg)? {
                    Value::Port(pid) => {
                        if let Some(ref io) = self.io { io.is_textual_port(pid) } else { false }
                    }
                    _ => false,
                };
                self.lisp.boolean(result).map_err(Into::into)
            }

            Builtin::BinaryPortp => {
                let arg = self.lisp.car(args)?;
                let result = match self.lisp.get(arg)? {
                    Value::Port(pid) => {
                        if let Some(ref io) = self.io { io.is_binary_port(pid) } else { false }
                    }
                    _ => false,
                };
                self.lisp.boolean(result).map_err(Into::into)
            }

            Builtin::InputPortOpenp => {
                let arg = self.lisp.car(args)?;
                let result = match self.lisp.get(arg)? {
                    Value::Port(pid) => {
                        if let Some(ref io) = self.io {
                            io.is_input_port(pid) && io.is_port_open(pid)
                        } else { false }
                    }
                    _ => false,
                };
                self.lisp.boolean(result).map_err(Into::into)
            }

            Builtin::OutputPortOpenp => {
                let arg = self.lisp.car(args)?;
                let result = match self.lisp.get(arg)? {
                    Value::Port(pid) => {
                        if let Some(ref io) = self.io {
                            io.is_output_port(pid) && io.is_port_open(pid)
                        } else { false }
                    }
                    _ => false,
                };
                self.lisp.boolean(result).map_err(Into::into)
            }

            // ================================================================
            // File system operations (R7RS §6.13)
            // ================================================================

            Builtin::FileExistsP => {
                // (file-exists? filename)
                let arg = self.lisp.car(args)?;
                let path = self.extract_string_arg(arg, call_expr)?;
                match &self.io {
                    Some(io) => {
                        let exists = io.file_exists(&path)
                            .map_err(|_| self.make_error(ErrorKind::Generic, call_expr))?;
                        self.lisp.boolean(exists).map_err(Into::into)
                    }
                    None => Err(self.make_error(ErrorKind::Generic, call_expr)),
                }
            }

            Builtin::DeleteFile => {
                // (delete-file filename)
                let arg = self.lisp.car(args)?;
                let path = self.extract_string_arg(arg, call_expr)?;
                match &mut self.io {
                    Some(io) => {
                        io.delete_file(&path)
                            .map_err(|_| self.make_error(ErrorKind::Generic, call_expr))?;
                        self.lisp.void_val().map_err(Into::into)
                    }
                    None => Err(self.make_error(ErrorKind::Generic, call_expr)),
                }
            }

            Builtin::Load => {
                // (load filename) — handled via trampolining in apply_builtin_trampolined
                // This fallback should not be reached; see apply_load_builtin.
                Err(self.make_error(ErrorKind::Generic, call_expr))
            }

            // ================================================================
            // Process / environment operations (R7RS §6.14)
            // ================================================================

            Builtin::CommandLine => {
                // (command-line) -> list of strings
                match &self.io {
                    Some(io) => {
                        let count = io.command_line_count()
                            .map_err(|_| self.make_error(ErrorKind::Generic, call_expr))?;
                        // Build list in reverse then reverse it
                        let mut list = self.lisp.nil()?;
                        for i in (0..count).rev() {
                            let s = io.command_line_arg(i)
                                .map_err(|_| self.make_error(ErrorKind::Generic, call_expr))?;
                            let str_val = self.lisp.string(s)?;
                            list = self.lisp.cons(str_val, list)?;
                        }
                        Ok(list)
                    }
                    None => Err(self.make_error(ErrorKind::Generic, call_expr)),
                }
            }

            Builtin::Exit => {
                // (exit) or (exit obj)
                let code = self.extract_exit_code(args, call_expr)?;
                match &mut self.io {
                    Some(io) => {
                        let _ = io.exit_process(code);
                        // If the io provider doesn't actually exit (e.g. in tests),
                        // return void
                        self.lisp.void_val().map_err(Into::into)
                    }
                    None => Err(self.make_error(ErrorKind::Generic, call_expr)),
                }
            }

            Builtin::EmergencyExit => {
                // (emergency-exit) or (emergency-exit obj)
                let code = self.extract_exit_code(args, call_expr)?;
                match &mut self.io {
                    Some(io) => {
                        let _ = io.emergency_exit_process(code);
                        self.lisp.void_val().map_err(Into::into)
                    }
                    None => Err(self.make_error(ErrorKind::Generic, call_expr)),
                }
            }

            Builtin::GetEnvironmentVariable => {
                // (get-environment-variable name) -> string or #f
                let arg = self.lisp.car(args)?;
                let name = self.extract_string_arg(arg, call_expr)?;
                match &mut self.io {
                    Some(io) => {
                        match io.get_environment_variable(&name) {
                            Ok(Some(val)) => self.lisp.string(val).map_err(Into::into),
                            Ok(None) => self.lisp.false_val().map_err(Into::into),
                            Err(_) => Err(self.make_error(ErrorKind::Generic, call_expr)),
                        }
                    }
                    None => Err(self.make_error(ErrorKind::Generic, call_expr)),
                }
            }

            Builtin::GetEnvironmentVariables => {
                // (get-environment-variables) -> alist of (name . value)
                // First, get count (requires &mut io)
                let count = match &mut self.io {
                    Some(io) => io.environment_variables_count()
                        .unwrap_or(0),
                    None => return Err(self.make_error(ErrorKind::Generic, call_expr)),
                };
                // Now build the alist by iterating (environment_variable_at uses &self)
                let mut list = self.lisp.nil()?;
                for i in (0..count).rev() {
                    let (name, value) = match &self.io {
                        Some(io) => io.environment_variable_at(i)
                            .map_err(|_| self.make_error(ErrorKind::Generic, call_expr))?,
                        None => return Err(self.make_error(ErrorKind::Generic, call_expr)),
                    };
                    let name_str = self.lisp.string(name)?;
                    let value_str = self.lisp.string(value)?;
                    let pair = self.lisp.cons(name_str, value_str)?;
                    list = self.lisp.cons(pair, list)?;
                }
                Ok(list)
            }

            Builtin::InteractionEnvironment => {
                // (interaction-environment) -> mutable environment object
                let env = self.global_env.0;
                self.lisp.alloc(Value::Environment { env, mutable: true }).map_err(Into::into)
            }
        }
    }
    
    /// OPTIMIZED: Apply binary builtin directly without list allocation
    pub(super) fn apply_binary_builtin(&mut self, builtin: Builtin, a: ArenaIndex, b: ArenaIndex, call_expr: ArenaIndex) 
        -> EvalResult 
    {
        match builtin {
            // Arithmetic with overflow check (mixed int/float support)
            Builtin::Add => binary_int_op!(self, a, b, call_expr, 
                |x: isize, y: isize| x.checked_add(y),
                |x: fsize, y: fsize| x + y),
            Builtin::Sub => binary_int_op!(self, a, b, call_expr, 
                |x: isize, y: isize| x.checked_sub(y),
                |x: fsize, y: fsize| x - y),
            Builtin::Mul => binary_int_op!(self, a, b, call_expr, 
                |x: isize, y: isize| x.checked_mul(y),
                |x: fsize, y: fsize| x * y),
            
            // Division operations with zero check
            Builtin::Div => binary_div_op!(self, a, b, call_expr, 
                |x, y| x / y,
                |x: fsize, y: fsize| x / y),
            Builtin::Modulo => binary_div_op!(self, a, b, call_expr, 
                |x, y| ((x % y) + y) % y,
                |x: fsize, y: fsize| ((x % y) + y) % y),
            Builtin::Remainder => binary_div_op!(self, a, b, call_expr, 
                |x, y| x % y,
                |x: fsize, y: fsize| x % y),
            
            // Comparisons
            Builtin::Lt => binary_int_cmp!(self, a, b, call_expr, |x, y| x < y),
            Builtin::Gt => binary_int_cmp!(self, a, b, call_expr, |x, y| x > y),
            Builtin::Le => binary_int_cmp!(self, a, b, call_expr, |x, y| x <= y),
            Builtin::Ge => binary_int_cmp!(self, a, b, call_expr, |x, y| x >= y),
            Builtin::NumEq => binary_int_cmp!(self, a, b, call_expr, |x, y| x == y),
            Builtin::EqP | Builtin::EqvP => {
                let val_a = self.lisp.get(a)?;
                let val_b = self.lisp.get(b)?;
                
                let eq = match (val_a, val_b) {
                    (Value::Nil, Value::Nil) => true,
                    (Value::True, Value::True) => true,
                    (Value::False, Value::False) => true,
                    (Value::Number(x), Value::Number(y)) => x == y,
                    (Value::Float(x), Value::Float(y)) => x == y,
                    (Value::Char(x), Value::Char(y)) => x == y,
                    (Value::Symbol(_), Value::Symbol(_)) => self.lisp.symbol_eq(a, b)?,
                    (Value::String { len: la, data: da }, Value::String { len: lb, data: db }) => {
                        a == b || (la == lb && da == db)
                    }
                    _ => a == b,
                };
                
                self.lisp.boolean(eq).map_err(Into::into)
            }
            Builtin::Cons => {
                self.lisp.cons(a, b).map_err(Into::into)
            }
            // For other builtins, fall back to list-based approach
            _ => {
                let rest = self.lisp.cons(b, self.lisp.nil()?)?;
                let args = self.lisp.cons(a, rest)?;
                self.apply_builtin(builtin, args, call_expr)
            }
        }
    }
    
    /// Get integer from already-evaluated value
    pub(super) fn get_int(&self, idx: ArenaIndex, call_expr: ArenaIndex) -> Result<isize, EvalError> {
        match self.lisp.get(idx)? {
            Value::Number(n) => Ok(n),
            Value::Float(f) => {
                // Accept floats that represent exact integers
                if f.is_finite() && f == (f as isize as fsize) {
                    Ok(f as isize)
                } else {
                    Err(self.type_error(call_expr, "integer", "inexact number"))
                }
            }
            v => Err(self.type_error(call_expr, "integer", v.type_name())),
        }
    }
    
    /// Get number as fsize from already-evaluated value
    pub(super) fn get_num_as_fsize(&self, idx: ArenaIndex, call_expr: ArenaIndex) -> Result<fsize, EvalError> {
        match self.lisp.get(idx)? {
            Value::Number(n) => Ok(n as fsize),
            Value::Float(f) => Ok(f),
            v => Err(self.type_error(call_expr, "number", v.type_name())),
        }
    }
    
    /// Get character from already-evaluated value
    pub(super) fn get_char(&self, idx: ArenaIndex, call_expr: ArenaIndex) -> Result<char, EvalError> {
        match self.lisp.get(idx)? {
            Value::Char(c) => Ok(c),
            v => Err(self.type_error(call_expr, "char", v.type_name())),
        }
    }
    
    /// Numeric fold with already-evaluated args, supporting mixed int/float arithmetic.
    /// If any argument is a float, the result is a float.
    pub(super) fn numeric_fold<F, G>(&self, args: ArenaIndex, init: isize, int_f: F, float_f: G, call_expr: ArenaIndex) -> EvalResult
    where 
        F: Fn(isize, isize) -> Option<isize>,
        G: Fn(fsize, fsize) -> fsize,
    {
        let mut acc_int: isize = init;
        let mut acc_float: fsize = init as fsize;
        let mut is_float = false;
        let mut current = args;
        loop {
            match self.lisp.get(current)? {
                Value::Nil => {
                    return if is_float {
                        self.lisp.float(acc_float).map_err(Into::into)
                    } else {
                        self.lisp.number(acc_int).map_err(Into::into)
                    };
                }
                Value::Cons { .. } => {
                    let car = self.lisp.car(current)?;
                    let cdr = self.lisp.cdr(current)?;
                    match self.lisp.get(car)? {
                        Value::Number(n) => {
                            if is_float {
                                acc_float = float_f(acc_float, n as fsize);
                            } else {
                                match int_f(acc_int, n) {
                                    Some(r) => acc_int = r,
                                    None => return Err(self.make_error(ErrorKind::DivisionByZero, call_expr)),
                                }
                            }
                        }
                        Value::Float(f) => {
                            if !is_float {
                                // Promote accumulator to float
                                acc_float = acc_int as fsize;
                                is_float = true;
                            }
                            acc_float = float_f(acc_float, f);
                        }
                        _ => return Err(self.make_error(ErrorKind::TypeError, current)),
                    }
                    current = cdr;
                }
                _ => return Err(self.make_error(ErrorKind::TypeError, current)),
            }
        }
    }
    
    /// Compare two numbers (supports mixed int/float)
    pub(super) fn compare_numbers<F>(&self, args: ArenaIndex, cmp: F, call_expr: ArenaIndex) -> EvalResult
    where F: Fn(fsize, fsize) -> bool
    {
        let a = self.get_num_as_fsize(self.lisp.car(args)?, call_expr)?;
        let b = self.get_num_as_fsize(self.lisp.car(self.lisp.cdr(args)?)?, call_expr)?;
        self.lisp.boolean(cmp(a, b)).map_err(Into::into)
    }
    
    /// Numeric fold that always produces a float result (starts with float accumulator)
    pub(super) fn numeric_fold_float<G>(&self, args: ArenaIndex, init: fsize, float_f: G, call_expr: ArenaIndex) -> EvalResult
    where 
        G: Fn(fsize, fsize) -> fsize,
    {
        let mut acc = init;
        let mut current = args;
        loop {
            match self.lisp.get(current)? {
                Value::Nil => return self.lisp.float(acc).map_err(Into::into),
                Value::Cons { .. } => {
                    let car = self.lisp.car(current)?;
                    let cdr = self.lisp.cdr(current)?;
                    let n = self.get_num_as_fsize(car, call_expr)?;
                    acc = float_f(acc, n);
                    current = cdr;
                }
                _ => return Err(self.make_error(ErrorKind::TypeError, current)),
            }
        }
    }
    
    /// Helper for character chain comparisons (char=?, char<?, etc.)
    pub(super) fn char_chain_compare<F>(&self, args: ArenaIndex, compare_fn: F, call_expr: ArenaIndex) -> EvalResult
    where
        F: Fn(char, char) -> bool
    {
        // Need at least 2 arguments
        let first = self.get_char(self.lisp.car(args)?, call_expr)?;
        let rest = self.lisp.cdr(args)?;
        
        if self.lisp.get(rest)?.is_nil() {
            return Err(self.type_error(call_expr, "at least 2 arguments", "1 argument"));
        }
        
        let mut prev = first;
        let mut current = rest;
        
        loop {
            match self.lisp.get(current)? {
                Value::Nil => return self.lisp.true_val().map_err(Into::into),
                Value::Cons { .. } => {
                    let car = self.lisp.car(current)?;
                    let cdr = self.lisp.cdr(current)?;
                    let c = self.get_char(car, call_expr)?;
                    if !compare_fn(prev, c) {
                        return self.lisp.false_val().map_err(Into::into);
                    }
                    prev = c;
                    current = cdr;
                }
                _ => return Err(self.make_error(ErrorKind::TypeError, current)),
            }
        }
    }
    
    /// Helper for string chain comparisons (string=?, string<?, etc.)
    pub(super) fn string_chain_compare<F>(&self, args: ArenaIndex, compare_fn: F, call_expr: ArenaIndex) -> EvalResult
    where
        F: Fn(core::cmp::Ordering) -> bool
    {
        // Need at least 2 arguments
        let first_idx = self.lisp.car(args)?;
        let rest = self.lisp.cdr(args)?;
        
        if self.lisp.get(rest)?.is_nil() {
            return Err(self.type_error(call_expr, "at least 2 arguments", "1 argument"));
        }
        
        let mut prev_idx = first_idx;
        let mut current = rest;
        
        loop {
            match self.lisp.get(current)? {
                Value::Nil => return self.lisp.true_val().map_err(Into::into),
                Value::Cons { .. } => {
                    let car = self.lisp.car(current)?;
                    let cdr = self.lisp.cdr(current)?;
                    let ordering = self.compare_strings(prev_idx, car, call_expr)?;
                    if !compare_fn(ordering) {
                        return self.lisp.false_val().map_err(Into::into);
                    }
                    prev_idx = car;
                    current = cdr;
                }
                _ => return Err(self.make_error(ErrorKind::TypeError, current)),
            }
        }
    }
    
    /// Compare two strings lexicographically
    pub(super) fn compare_strings(&self, a: ArenaIndex, b: ArenaIndex, call_expr: ArenaIndex) -> Result<core::cmp::Ordering, EvalError> {
        let (len_a, data_a) = match self.lisp.get(a)? {
            Value::String { len, data } => (len, data),
            v => return Err(self.type_error(call_expr, "string", v.type_name())),
        };
        let (len_b, data_b) = match self.lisp.get(b)? {
            Value::String { len, data } => (len, data),
            v => return Err(self.type_error(call_expr, "string", v.type_name())),
        };
        
        let min_len = len_a.min(len_b);
        
        for i in 0..min_len {
            let slot_a = self.lisp.arena_index_at_offset(data_a, i)?;
            let char_a = match self.lisp.get(slot_a)? {
                Value::Char(c) => c,
                _ => return Err(self.make_error(ErrorKind::TypeError, call_expr)),
            };
            let slot_b = self.lisp.arena_index_at_offset(data_b, i)?;
            let char_b = match self.lisp.get(slot_b)? {
                Value::Char(c) => c,
                _ => return Err(self.make_error(ErrorKind::TypeError, call_expr)),
            };
            
            match char_a.cmp(&char_b) {
                core::cmp::Ordering::Equal => {}
                ord => return Ok(ord),
            }
        }
        
        // All compared characters are equal, compare lengths
        Ok(len_a.cmp(&len_b))
    }
    
    /// Helper for case-insensitive string chain comparisons
    pub(super) fn string_ci_chain_compare<F>(&self, args: ArenaIndex, compare_fn: F, call_expr: ArenaIndex) -> EvalResult
    where
        F: Fn(core::cmp::Ordering) -> bool
    {
        let first_idx = self.lisp.car(args)?;
        let rest = self.lisp.cdr(args)?;
        
        if self.lisp.get(rest)?.is_nil() {
            return Err(self.type_error(call_expr, "at least 2 arguments", "1 argument"));
        }
        
        let mut prev_idx = first_idx;
        let mut current = rest;
        
        loop {
            match self.lisp.get(current)? {
                Value::Nil => return self.lisp.true_val().map_err(Into::into),
                Value::Cons { .. } => {
                    let car = self.lisp.car(current)?;
                    let cdr = self.lisp.cdr(current)?;
                    let ordering = self.compare_strings_ci(prev_idx, car, call_expr)?;
                    if !compare_fn(ordering) {
                        return self.lisp.false_val().map_err(Into::into);
                    }
                    prev_idx = car;
                    current = cdr;
                }
                _ => return Err(self.make_error(ErrorKind::TypeError, current)),
            }
        }
    }
    
    /// Compare two strings lexicographically, case-insensitive
    pub(super) fn compare_strings_ci(&self, a: ArenaIndex, b: ArenaIndex, call_expr: ArenaIndex) -> Result<core::cmp::Ordering, EvalError> {
        let (len_a, data_a) = match self.lisp.get(a)? {
            Value::String { len, data } => (len, data),
            v => return Err(self.type_error(call_expr, "string", v.type_name())),
        };
        let (len_b, data_b) = match self.lisp.get(b)? {
            Value::String { len, data } => (len, data),
            v => return Err(self.type_error(call_expr, "string", v.type_name())),
        };
        
        let min_len = len_a.min(len_b);
        
        for i in 0..min_len {
            let slot_a = self.lisp.arena_index_at_offset(data_a, i)?;
            let char_a = match self.lisp.get(slot_a)? {
                Value::Char(c) => c.to_ascii_lowercase(),
                _ => return Err(self.make_error(ErrorKind::TypeError, call_expr)),
            };
            let slot_b = self.lisp.arena_index_at_offset(data_b, i)?;
            let char_b = match self.lisp.get(slot_b)? {
                Value::Char(c) => c.to_ascii_lowercase(),
                _ => return Err(self.make_error(ErrorKind::TypeError, call_expr)),
            };
            
            match char_a.cmp(&char_b) {
                core::cmp::Ordering::Equal => {}
                ord => return Ok(ord),
            }
        }
        
        Ok(len_a.cmp(&len_b))
    }
    
    /// Convert a proper list to an array (vector).
    /// Used by both `(vector obj ...)` and `(list->vector lst)`.
    pub(super) fn list_to_array(&self, list: ArenaIndex, call_expr: ArenaIndex) -> EvalResult {
        // Count elements
        let mut count = 0usize;
        let mut current = list;
        loop {
            match self.lisp.get(current)? {
                Value::Nil => break,
                Value::Cons { .. } => {
                    count += 1;
                    current = self.lisp.cdr(current)?;
                }
                _ => return Err(self.make_error(ErrorKind::TypeError, call_expr)),
            }
        }
        
        // Create vector and fill
        let placeholder = self.lisp.number(0)?;
        let vec = self.lisp.make_array(count, placeholder)?;
        current = list;
        for i in 0..count {
            let val = self.lisp.car(current)?;
            self.lisp.array_set(vec, i, val)?;
            current = self.lisp.cdr(current)?;
        }
        Ok(vec)
    }

    /// Extract an input port from optional arguments.
    /// Returns STDIN if no argument is provided.
    fn extract_input_port(&self, args: ArenaIndex, call_expr: ArenaIndex) -> Result<grift_parser::PortId, EvalError> {
        if self.lisp.get(args)?.is_nil() {
            Ok(grift_parser::PortId::STDIN)
        } else {
            let port_arg = self.lisp.car(args)?;
            match self.lisp.get(port_arg)? {
                Value::Port(pid) => Ok(pid),
                v => Err(self.type_error(call_expr, "port", v.type_name())),
            }
        }
    }

    /// Implement (read) by reading characters from a port and parsing.
    fn apply_read_builtin(&mut self, pid: grift_parser::PortId, call_expr: ArenaIndex) -> EvalResult {
        // Read characters into a stack buffer until we have a complete S-expression
        let mut buf = [0u8; 2048];
        let mut byte_len = 0;
        let mut paren_depth: i32 = 0;
        let mut in_string = false;
        let mut escape = false;
        let mut got_token = false;

        let io = match &mut self.io {
            Some(io) => io,
            None => return Err(self.make_error(ErrorKind::Generic, call_expr)),
        };

        // Skip leading whitespace
        loop {
            match io.read_char(pid) {
                Ok(c) => {
                    if !c.is_whitespace() {
                        // Put this char into the buffer
                        let enc_len = c.len_utf8();
                        if byte_len + enc_len > buf.len() {
                            return Err(self.make_error(ErrorKind::Generic, call_expr));
                        }
                        c.encode_utf8(&mut buf[byte_len..]);
                        byte_len += enc_len;

                        if c == '(' || c == '[' {
                            paren_depth += 1;
                        } else if c == '"' {
                            in_string = true;
                        } else if paren_depth == 0 {
                            got_token = true;
                        }
                        break;
                    }
                }
                Err(grift_parser::IoErrorKind::Eof) => {
                    return self.lisp.eof().map_err(Into::into);
                }
                Err(_) => return Err(self.make_error(ErrorKind::Generic, call_expr)),
            }
        }

        // Read remaining characters to form a complete expression
        if paren_depth > 0 || in_string {
            loop {
                match io.read_char(pid) {
                    Ok(c) => {
                        let enc_len = c.len_utf8();
                        if byte_len + enc_len > buf.len() {
                            return Err(self.make_error(ErrorKind::Generic, call_expr));
                        }
                        c.encode_utf8(&mut buf[byte_len..]);
                        byte_len += enc_len;

                        if in_string {
                            if escape {
                                escape = false;
                            } else if c == '\\' {
                                escape = true;
                            } else if c == '"' {
                                in_string = false;
                                if paren_depth == 0 { break; }
                            }
                        } else {
                            if c == '(' || c == '[' {
                                paren_depth += 1;
                            } else if c == ')' || c == ']' {
                                paren_depth -= 1;
                                if paren_depth == 0 { break; }
                            } else if c == '"' {
                                in_string = true;
                            }
                        }
                    }
                    Err(grift_parser::IoErrorKind::Eof) => break,
                    Err(_) => return Err(self.make_error(ErrorKind::Generic, call_expr)),
                }
            }
        } else if got_token {
            // Continue reading the token (number, symbol, etc.)
            loop {
                match io.peek_char(pid) {
                    Ok(c) if c.is_whitespace() || c == '(' || c == ')' || c == '[' || c == ']' => break,
                    Ok(c) => {
                        let _ = io.read_char(pid);
                        let enc_len = c.len_utf8();
                        if byte_len + enc_len > buf.len() { break; }
                        c.encode_utf8(&mut buf[byte_len..]);
                        byte_len += enc_len;
                    }
                    Err(_) => break,
                }
            }
        }

        let s = core::str::from_utf8(&buf[..byte_len])
            .map_err(|_| self.make_error(ErrorKind::Generic, call_expr))?;

        if s.is_empty() {
            return self.lisp.eof().map_err(Into::into);
        }

        // Parse the expression
        match grift_parser::parse(self.lisp, s) {
            Ok(expr) => Ok(expr),
            Err(_) => Err(self.make_error(ErrorKind::Generic, call_expr)),
        }
    }

    /// Extract a Scheme string value into a stack-allocated UTF-8 buffer.
    /// Returns a fixed-size array wrapper that can be used as `&str`.
    fn extract_string_arg(&self, idx: ArenaIndex, call_expr: ArenaIndex) -> Result<StackString, EvalError> {
        match self.lisp.get(idx)? {
            Value::String { len, data } => {
                let mut buf = [0u8; STACK_STRING_BUF_SIZE];
                let mut byte_len = 0;
                for i in 0..len {
                    let char_slot = self.lisp.arena_index_at_offset(data, i)?;
                    if let Value::Char(c) = self.lisp.get(char_slot)? {
                        let enc_len = c.len_utf8();
                        if byte_len + enc_len > buf.len() { break; }
                        c.encode_utf8(&mut buf[byte_len..]);
                        byte_len += enc_len;
                    }
                }
                Ok(StackString { buf, len: byte_len })
            }
            v => Err(self.type_error(call_expr, "string", v.type_name())),
        }
    }

    /// Extract an exit code from optional arguments.
    /// - No args or #t: 0
    /// - #f: 1
    /// - integer: use directly
    fn extract_exit_code(&self, args: ArenaIndex, call_expr: ArenaIndex) -> Result<i32, EvalError> {
        if self.lisp.get(args)?.is_nil() {
            return Ok(0);
        }
        let arg = self.lisp.car(args)?;
        match self.lisp.get(arg)? {
            Value::True => Ok(0),
            Value::False => Ok(1),
            Value::Number(n) => Ok(n as i32),
            v => Err(self.type_error(call_expr, "integer or boolean", v.type_name())),
        }
    }
}

/// Maximum size of a stack-allocated string buffer for IoProvider arguments.
const STACK_STRING_BUF_SIZE: usize = 1024;

/// Stack-allocated UTF-8 string buffer for passing to IoProvider methods.
struct StackString {
    buf: [u8; STACK_STRING_BUF_SIZE],
    len: usize,
}

impl core::ops::Deref for StackString {
    type Target = str;
    fn deref(&self) -> &str {
        // The buffer was constructed from valid UTF-8 chars
        core::str::from_utf8(&self.buf[..self.len]).unwrap_or("")
    }
}

/// Adapter to write through an [`IoProvider`] port via [`core::fmt::Write`].
struct IoPortWriter<'a> {
    io: &'a mut dyn grift_parser::IoProvider,
    port: grift_parser::PortId,
    error: bool,
}

impl core::fmt::Write for IoPortWriter<'_> {
    fn write_str(&mut self, s: &str) -> core::fmt::Result {
        if self.io.write_str(self.port, s).is_err() {
            self.error = true;
            Err(core::fmt::Error)
        } else {
            Ok(())
        }
    }
}

// ============================================================================
// Float formatting and parsing helpers (no_std compatible)
// ============================================================================

/// Format a float value into a char buffer, returning the number of chars written.
/// Uses core::fmt::Write for no_std-compatible formatting.
fn format_float_to_chars(f: fsize, chars: &mut [char; 32]) -> usize {
    use core::fmt::Write;
    
    // Special values
    if f.is_nan() {
        let s = b"+nan.0";
        for (i, &b) in s.iter().enumerate() { chars[i] = b as char; }
        return s.len();
    }
    if f.is_infinite() {
        let s = if f > 0.0 { b"+inf.0" as &[u8] } else { b"-inf.0" as &[u8] };
        for (i, &b) in s.iter().enumerate() { chars[i] = b as char; }
        return s.len();
    }
    
    // Use a stack buffer to format via core::fmt
    let mut buf = [0u8; 32];
    let mut writer = BufWriter { buf: &mut buf, pos: 0 };
    
    // Check if this is a whole number
    let is_whole = f.is_finite() && f == (f as isize as fsize);
    if is_whole {
        let _ = write!(writer, "{:.1}", f);
    } else {
        let _ = write!(writer, "{}", f);
    }
    
    let len = writer.pos.min(32);
    for i in 0..len {
        chars[i] = buf[i] as char;
    }
    len
}

/// Minimal byte-buffer writer for core::fmt::Write
struct BufWriter<'a> {
    buf: &'a mut [u8; 32],
    pos: usize,
}

impl core::fmt::Write for BufWriter<'_> {
    fn write_str(&mut self, s: &str) -> core::fmt::Result {
        for &b in s.as_bytes() {
            if self.pos < 32 {
                self.buf[self.pos] = b;
                self.pos += 1;
            }
        }
        Ok(())
    }
}

/// Parse a float from a string in no_std. Handles decimal notation and exponents.
fn parse_float_no_std(s: &str) -> Option<fsize> {
    let bytes = s.as_bytes();
    if bytes.is_empty() { return None; }
    
    let mut pos = 0;
    let negative = if bytes[pos] == b'-' { pos += 1; true } 
                   else if bytes[pos] == b'+' { pos += 1; false }
                   else { false };
    
    // Parse integer part
    let mut result: fsize = 0.0;
    let mut has_digits = false;
    while pos < bytes.len() && bytes[pos].is_ascii_digit() {
        result = result * 10.0 + (bytes[pos] - b'0') as fsize;
        pos += 1;
        has_digits = true;
    }
    
    // Parse fractional part
    let mut has_dot = false;
    if pos < bytes.len() && bytes[pos] == b'.' {
        has_dot = true;
        pos += 1;
        let mut frac_scale: fsize = 0.1;
        while pos < bytes.len() && bytes[pos].is_ascii_digit() {
            result += (bytes[pos] - b'0') as fsize * frac_scale;
            frac_scale *= 0.1;
            pos += 1;
            has_digits = true;
        }
    }
    
    if !has_digits { return None; }
    
    // Parse exponent
    if pos < bytes.len() && (bytes[pos] == b'e' || bytes[pos] == b'E') {
        pos += 1;
        let exp_neg = if pos < bytes.len() && bytes[pos] == b'-' { pos += 1; true }
                      else if pos < bytes.len() && bytes[pos] == b'+' { pos += 1; false }
                      else { false };
        let mut exp: i32 = 0;
        while pos < bytes.len() && bytes[pos].is_ascii_digit() {
            exp = exp.saturating_mul(10).saturating_add((bytes[pos] - b'0') as i32);
            pos += 1;
        }
        if exp_neg { exp = -exp; }
        // Multiply by 10^exp
        if exp >= 0 {
            for _ in 0..exp { result *= 10.0; }
        } else {
            for _ in 0..(-exp) { result /= 10.0; }
        }
    }
    
    // Must have consumed all input and must have been a float (dot or exponent)
    if pos != bytes.len() { return None; }
    if !has_dot && pos == bytes.len() {
        // This would have parsed as integer, don't convert
        // Unless there was an exponent
    }
    
    if negative { result = -result; }
    Some(result)
}