cljrs-interp 0.1.194

Tree-walking interpreter for clojurust (special forms, macros, destructuring)
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
//! Special form evaluators.

use std::collections::HashMap;
use std::sync::Arc;

use crate::destructure::bind_pattern;
use crate::eval::{eval, eval_body, is_special_form};
use cljrs_builtins::form::{expand_reader_conds, form_to_value, select_reader_cond};
use cljrs_env::env::{Env, RequireRefer, RequireSpec};
use cljrs_env::error::{EvalError, EvalResult};
use cljrs_env::loader::load_ns;
use cljrs_gc::GcPtr;
use cljrs_reader::Form;
use cljrs_reader::form::FormKind;
use cljrs_value::error::ExceptionInfo;
use cljrs_value::{
    CljxFn, CljxFnArity, FutureState, Keyword, MapValue, MultiFn, Protocol, ProtocolFn,
    ProtocolMethod, TypeHint, TypeInstance, Value, ValueError,
};

/// Dispatch to the right special-form handler.
pub fn eval_special(head: &str, args: &[Form], env: &mut Env) -> EvalResult {
    match head {
        "def" => eval_def(args, env),
        "fn*" | "fn" => eval_fn(args, env),
        "if" => eval_if(args, env),
        "do" => eval_body(args, env),
        "let*" | "let" => eval_let(args, env),
        "loop*" | "loop" => eval_loop(args, env),
        "recur" => eval_recur(args, env),
        "quote" => eval_quote(args),
        "var" => eval_var(args, env),
        "set!" => eval_set_bang(args, env),
        "throw" => eval_throw(args, env),
        "try" => eval_try(args, env),
        "defn" | "defn-" => eval_defn(args, env),
        "defmacro" => eval_defmacro(args, env),
        "defonce" => eval_defonce(args, env),
        "and" => eval_and(args, env),
        "or" => eval_or(args, env),
        "." => Err(EvalError::Runtime("interop not yet implemented".into())),
        "ns" => eval_ns(args, env),
        "require" => eval_require(args, env),
        "letfn" => eval_letfn(args, env),
        "in-ns" => eval_in_ns(args, env),
        "alias" => eval_alias(args, env),
        "defprotocol" => eval_defprotocol(args, env),
        "extend-type" => eval_extend_type(args, env),
        "extend-protocol" => eval_extend_protocol(args, env),
        "defmulti" => eval_defmulti(args, env),
        "defmethod" => eval_defmethod(args, env),
        "defrecord" => eval_defrecord(args, env),
        "reify" => eval_reify(args, env),
        "load-file" => eval_load_file(args, env),
        "binding" => eval_binding(args, env),
        "with-out-str" => eval_with_out_str(args, env),
        "await" => eval_await(args, env),
        _ => unreachable!("unknown special form: {head}"),
    }
}

// ── def ───────────────────────────────────────────────────────────────────────

fn eval_def(args: &[Form], env: &mut Env) -> EvalResult {
    if args.is_empty() {
        return Err(EvalError::Runtime("def requires a name".into()));
    }
    let (name, meta_opt) = extract_def_name(&args[0], env)?;
    // Skip optional docstring: (def name "docstring" value)
    let val_idx = if args.len() > 2 && matches!(args[1].kind, FormKind::Str(_)) {
        2
    } else {
        1
    };
    let val = if args.len() > val_idx {
        // Under no-gc: def value expressions go to the StaticArena since the
        // Var must outlive all scratch regions.
        #[cfg(feature = "no-gc")]
        let _static_ctx = cljrs_gc::alloc_ctx::StaticCtxGuard::new();
        eval(&args[val_idx], env)?
    } else {
        Value::Nil
    };
    let var = env
        .globals
        .intern(&env.current_ns, Arc::from(name.as_str()), val.clone());
    if let Some(meta_val) = meta_opt {
        var.get().set_meta(meta_val);
    }
    Ok(Value::Var(var))
}

/// Extract the def name and optional metadata from the name form.
fn extract_def_name(form: &Form, env: &mut Env) -> EvalResult<(String, Option<Value>)> {
    match &form.kind {
        FormKind::Symbol(s) => Ok((s.clone(), None)),
        FormKind::Meta(meta_form, inner) => {
            let meta_val = compile_meta_form(meta_form, env)?;
            match &inner.kind {
                FormKind::Symbol(s) => Ok((s.clone(), Some(meta_val))),
                _ => Err(EvalError::Runtime("def name must be a symbol".into())),
            }
        }
        _ => Err(EvalError::Runtime("def name must be a symbol".into())),
    }
}

/// Expand a metadata shorthand form into a map value.
fn compile_meta_form(meta: &Form, env: &mut Env) -> EvalResult<Value> {
    match &meta.kind {
        FormKind::Keyword(k) => {
            // ^:dynamic  →  {:dynamic true}
            let m = MapValue::empty().assoc(Value::keyword(Keyword::parse(k)), Value::Bool(true));
            Ok(Value::Map(m))
        }
        FormKind::Symbol(s) => {
            // ^TypeHint  →  {:tag "TypeHint"}
            let m = MapValue::empty().assoc(
                Value::keyword(Keyword::parse("tag")),
                Value::string(s.clone()),
            );
            Ok(Value::Map(m))
        }
        _ => eval(meta, env), // literal map or general expr
    }
}

// ── fn* ───────────────────────────────────────────────────────────────────────

/// Does a `^meta` form (or metadata map literal) request `:async`?
///
/// Handles the keyword shorthand `^:async` (a bare `:async` keyword form) and
/// an explicit map such as `^{:async true}` or a `defn` attr-map `{:async true}`.
pub fn meta_form_is_async(meta: &Form) -> bool {
    match &meta.kind {
        FormKind::Keyword(k) => k == "async",
        FormKind::Map(entries) => entries.chunks(2).any(|kv| {
            matches!(&kv[0].kind, FormKind::Keyword(k) if k == "async")
                && !matches!(
                    kv.get(1).map(|f| &f.kind),
                    None | Some(FormKind::Bool(false)) | Some(FormKind::Nil)
                )
        }),
        _ => false,
    }
}

fn eval_fn(args: &[Form], env: &mut Env) -> EvalResult {
    // Peel any leading `^meta` wrappers, e.g. `(fn ^:async [..] ..)` or
    // `(fn ^:async name [..] ..)`, recording whether `:async` was requested.
    let mut is_async = false;
    let peeled: Vec<Form>;
    let args: &[Form] = if matches!(args.first().map(|f| &f.kind), Some(FormKind::Meta(..))) {
        let mut head = args[0].clone();
        while let FormKind::Meta(meta, inner) = head.kind {
            is_async |= meta_form_is_async(&meta);
            head = *inner;
        }
        peeled = std::iter::once(head)
            .chain(args[1..].iter().cloned())
            .collect();
        &peeled
    } else {
        args
    };

    let mut idx = 0;
    let mut name: Option<Arc<str>> = None;

    // Optional name.
    if let Some(FormKind::Symbol(s)) = args.first().map(|f| &f.kind)
        && !is_special_form(s)
    {
        name = Some(Arc::from(s.as_str()));
        idx = 1;
    }

    let rest = &args[idx..];
    if rest.is_empty() {
        return Err(EvalError::Runtime("fn* requires params and body".into()));
    }

    let arities = match &rest[0].kind {
        FormKind::Vector(_) => {
            // Single arity: (fn* [params] body...)
            vec![parse_arity(&rest[0], &rest[1..])?]
        }
        FormKind::List(_) => {
            // Multi-arity: (fn* ([params] body...) ...)
            rest.iter()
                .map(|arity_form| {
                    if let FormKind::List(forms) = &arity_form.kind {
                        if forms.is_empty() {
                            return Err(EvalError::Runtime("arity clause requires params".into()));
                        }
                        parse_arity(&forms[0], &forms[1..])
                    } else {
                        Err(EvalError::Runtime("expected arity clause (list)".into()))
                    }
                })
                .collect::<EvalResult<Vec<_>>>()?
        }
        _ => {
            return Err(EvalError::Runtime(
                "fn* expects vector or arity clauses".into(),
            ));
        }
    };

    // Capture closed-over locals.
    let (closed_over_names, closed_over_vals) = env.all_local_bindings();

    let mut cljrs_fn = CljxFn::new(
        name.clone(),
        arities,
        closed_over_names,
        closed_over_vals,
        false,
        Arc::clone(&env.current_ns),
    );
    cljrs_fn.is_async = is_async;

    env.on_fn_defined(&cljrs_fn);

    // Eagerly lower each arity to IR if the compiler is ready.
    //eager_lower_fn(&cljrs_fn, env);

    let mut ptr = GcPtr::new(cljrs_fn);
    // For named anonymous functions (fn g ...), store a back-pointer so that
    // the self-reference returned from the body is pointer-equal to the outer
    // binding — required for `(= f (f))` to be `true`.
    if ptr.get().name.is_some() {
        let self_clone = ptr.clone();
        ptr.get_mut().self_ptr = Some(self_clone);
    }
    Ok(Value::Fn(ptr))
}

/// Parse one arity: params-form and body forms.
pub fn parse_arity(params_form: &Form, body: &[Form]) -> EvalResult<CljxFnArity> {
    let param_forms = match &params_form.kind {
        FormKind::Vector(v) => v,
        _ => {
            return Err(EvalError::Runtime(
                "fn arity params must be a vector".into(),
            ));
        }
    };

    let mut params: Vec<Arc<str>> = Vec::new();
    let mut param_hints: Vec<Option<TypeHint>> = Vec::new();
    let mut rest_param: Option<Arc<str>> = None;
    let mut rest_hint: Option<TypeHint> = None;
    let mut destructure_params: Vec<(usize, Form)> = Vec::new();
    let mut destructure_rest: Option<Form> = None;
    let mut saw_amp = false;

    for p in param_forms {
        // Peel a leading `^hint` (e.g. `^long x`, `^doubles a`) from the param,
        // resolving the `:tag` to a primitive `TypeHint`.  Unknown / non-primitive
        // tags resolve to `None` and are simply ignored (Clojure treats them as
        // advisory).  The unwrapped form keeps the existing symbol/destructure
        // handling unchanged.
        let (hint, p) = peel_param_hint(p);
        match &p.kind {
            FormKind::Symbol(s) if s == "&" => {
                saw_amp = true;
            }
            FormKind::Symbol(s) => {
                if saw_amp {
                    rest_param = Some(Arc::from(s.as_str()));
                    rest_hint = hint;
                    break;
                } else {
                    params.push(Arc::from(s.as_str()));
                    param_hints.push(hint);
                }
            }
            // Destructuring patterns: vectors and maps
            FormKind::Vector(_) | FormKind::Map(_) => {
                if saw_amp {
                    let gensym = format!("__destructure_rest_{}", params.len());
                    rest_param = Some(Arc::from(gensym.as_str()));
                    destructure_rest = Some(p.clone());
                    break;
                } else {
                    let idx = params.len();
                    let gensym = format!("__destructure_{idx}");
                    params.push(Arc::from(gensym.as_str()));
                    // Destructured params can't be primitive-tagged.
                    param_hints.push(None);
                    destructure_params.push((idx, p.clone()));
                }
            }
            _ => {
                return Err(EvalError::Runtime(
                    "fn params must be symbols, vectors, or maps".into(),
                ));
            }
        }
    }

    let body = desugar_pre_post_conditions(body);

    Ok(CljxFnArity {
        params,
        rest_param,
        body,
        destructure_params,
        destructure_rest,
        ir_arity_id: crate::arity::fresh_arity_id(),
        param_hints,
        rest_hint,
    })
}

/// Desugar a `:pre`/`:post` conditions map at the head of a function body.
///
/// Clojure binds `%` to the return value inside `:post` conditions. This
/// transforms the raw body forms into equivalent assertion code so the
/// interpreter does not need to handle conditions separately at call time.
///
/// Input body (simplified):
/// ```text
/// [{:pre [(pos? x)] :post [(pos? %)]} (inc x)]
/// ```
/// Output body:
/// ```text
/// [(assert (pos? x))
///  (let* [% (inc x)]
///    (assert (pos? %))
///    %)]
/// ```
fn desugar_pre_post_conditions(body: &[Form]) -> Vec<Form> {
    let first = match body.first() {
        Some(f) => f,
        None => return body.to_vec(),
    };

    let entries = match &first.kind {
        FormKind::Map(entries) => entries,
        _ => return body.to_vec(),
    };

    let mut pre_conds: Vec<Form> = Vec::new();
    let mut post_conds: Vec<Form> = Vec::new();
    let mut has_conditions = false;

    for chunk in entries.chunks(2) {
        if chunk.len() < 2 {
            break;
        }
        let (key, val) = (&chunk[0], &chunk[1]);
        match &key.kind {
            FormKind::Keyword(k) if k == "pre" => {
                if let FormKind::Vector(conds) = &val.kind {
                    pre_conds.extend_from_slice(conds);
                    has_conditions = true;
                }
            }
            FormKind::Keyword(k) if k == "post" => {
                if let FormKind::Vector(conds) = &val.kind {
                    post_conds.extend_from_slice(conds);
                    has_conditions = true;
                }
            }
            _ => {}
        }
    }

    if !has_conditions {
        return body.to_vec();
    }

    let real_body = &body[1..]; // strip the conditions map
    let span = first.span.clone();
    let mut new_body: Vec<Form> = Vec::new();

    // Emit (assert cond) for each :pre condition.
    for cond in &pre_conds {
        new_body.push(make_assert_call(cond, &span));
    }

    if post_conds.is_empty() {
        new_body.extend_from_slice(real_body);
    } else {
        // Wrap real body in (let* [% <body>] (assert post-cond)... %)
        // so that % refers to the return value inside :post conditions.
        let percent = Form::new(FormKind::Symbol("%".to_string()), span.clone());

        let body_expr = if real_body.len() == 1 {
            real_body[0].clone()
        } else {
            let mut do_forms = vec![Form::new(FormKind::Symbol("do".to_string()), span.clone())];
            do_forms.extend_from_slice(real_body);
            Form::new(FormKind::List(do_forms), span.clone())
        };

        let binding_vec = Form::new(
            FormKind::Vector(vec![percent.clone(), body_expr]),
            span.clone(),
        );

        let mut let_forms = vec![
            Form::new(FormKind::Symbol("let*".to_string()), span.clone()),
            binding_vec,
        ];
        for cond in &post_conds {
            let_forms.push(make_assert_call(cond, &span));
        }
        let_forms.push(percent);

        new_body.push(Form::new(FormKind::List(let_forms), span));
    }

    new_body
}

/// Build `(assert <cond>)` as a Form node.
fn make_assert_call(cond: &Form, span: &cljrs_types::span::Span) -> Form {
    Form::new(
        FormKind::List(vec![
            Form::new(FormKind::Symbol("assert".to_string()), span.clone()),
            cond.clone(),
        ]),
        span.clone(),
    )
}

/// Peel a `^hint` wrapper off a parameter form, returning the resolved
/// primitive [`TypeHint`] (if the tag is a recognized primitive) together with
/// the unwrapped inner form.  A param with no metadata returns `(None, form)`.
fn peel_param_hint(p: &Form) -> (Option<TypeHint>, &Form) {
    if let FormKind::Meta(meta, inner) = &p.kind {
        let hint = tag_name_of_meta(meta).and_then(|n| TypeHint::from_tag(&n));
        // Recurse in case of stacked metadata; the innermost form is the param.
        let (inner_hint, inner_form) = peel_param_hint(inner);
        (hint.or(inner_hint), inner_form)
    } else {
        (None, p)
    }
}

/// Extract the bare tag name from a `^meta` form: `^long` (symbol shorthand) or
/// `^{:tag long}` (explicit map).  Strips any namespace from the tag symbol.
fn tag_name_of_meta(meta: &Form) -> Option<Arc<str>> {
    match &meta.kind {
        // `^long x` — the metadata is a bare symbol naming the tag.
        FormKind::Symbol(s) => Some(strip_tag_ns(s)),
        // `^{:tag long} x` — pull the `:tag` entry out of the map literal.
        FormKind::Map(entries) => entries.chunks(2).find_map(|kv| {
            let is_tag = matches!(&kv[0].kind, FormKind::Keyword(k) if k == "tag");
            if !is_tag {
                return None;
            }
            match kv.get(1).map(|f| &f.kind) {
                Some(FormKind::Symbol(s)) => Some(strip_tag_ns(s)),
                Some(FormKind::Str(s)) => Some(strip_tag_ns(s)),
                _ => None,
            }
        }),
        _ => None,
    }
}

/// Strip a namespace prefix from a tag name (`clojure.core/long` → `long`).
fn strip_tag_ns(s: &str) -> Arc<str> {
    match s.rfind('/') {
        Some(pos) if pos + 1 < s.len() => Arc::from(&s[pos + 1..]),
        _ => Arc::from(s),
    }
}

// ── if ────────────────────────────────────────────────────────────────────────

fn eval_if(args: &[Form], env: &mut Env) -> EvalResult {
    if args.is_empty() {
        return Err(EvalError::Runtime("if requires a test".into()));
    }
    let test = eval(&args[0], env)?;
    let truthy = !matches!(test, Value::Nil | Value::Bool(false));
    if truthy {
        if args.len() > 1 {
            eval(&args[1], env)
        } else {
            Ok(Value::Nil)
        }
    } else if args.len() > 2 {
        eval(&args[2], env)
    } else {
        Ok(Value::Nil)
    }
}

// ── let* ──────────────────────────────────────────────────────────────────────

fn eval_let(args: &[Form], env: &mut Env) -> EvalResult {
    let raw_bindings = match args.first().map(|f| &f.kind) {
        Some(FormKind::Vector(v)) => v.clone(),
        _ => return Err(EvalError::Runtime("let* requires a binding vector".into())),
    };
    let bindings = if raw_bindings
        .iter()
        .any(|f| matches!(f.kind, FormKind::ReaderCond { .. }))
    {
        expand_reader_conds(&raw_bindings)
    } else {
        raw_bindings
    };

    if bindings.len() % 2 != 0 {
        return Err(EvalError::Runtime(
            "let* binding vector must have even length".into(),
        ));
    }

    let body = &args[1..];

    // Detect assoc/conj chains that can be virtualized.
    let chains = crate::virtualize::detect_let_chains(&bindings);
    let virtualizable_chains = find_virtualizable_chains(&chains, &bindings, body);

    env.push_frame();

    let pairs: Vec<_> = bindings.chunks(2).collect();
    let mut i = 0;
    while i < pairs.len() {
        // Check if this binding starts a virtualizable chain.
        if let Some(chain) = virtualizable_chains.iter().find(|c| c.start == i) {
            match eval_virtualized_chain(chain, &pairs, env) {
                Ok(()) => {
                    i += chain.len;
                    continue;
                }
                Err(e) => {
                    env.pop_frame();
                    return Err(e);
                }
            }
        }

        // Normal evaluation.
        let pair = pairs[i];
        let val = match eval(&pair[1], env) {
            Ok(v) => v,
            Err(e) => {
                env.pop_frame();
                return Err(e);
            }
        };
        if let Err(e) = bind_pattern(&pair[0], val, env) {
            env.pop_frame();
            return Err(e);
        }
        i += 1;
    }

    let result = eval_body(body, env);
    env.pop_frame();
    result
}

/// Filter chains to only those that are safe to virtualize.
///
/// A chain is safe if no intermediate binding is used outside the chain
/// (i.e., it's only used as the collection argument of the next step).
fn find_virtualizable_chains<'a>(
    chains: &'a [crate::virtualize::LetChain],
    bindings: &[Form],
    body: &[Form],
) -> Vec<&'a crate::virtualize::LetChain> {
    chains
        .iter()
        .filter(|chain| {
            // Check that no intermediate (all except the last) is used in body
            // or in other bindings outside the chain.
            for j in chain.start..(chain.start + chain.len - 1) {
                let name = match &bindings[j * 2].kind {
                    FormKind::Symbol(s) => s.as_str(),
                    _ => return false,
                };
                if crate::virtualize::binding_used_in_body(name, body) {
                    return false;
                }
                if crate::virtualize::binding_used_in_other_bindings(
                    name,
                    bindings,
                    chain.start,
                    chain.len,
                ) {
                    return false;
                }
            }
            true
        })
        .collect()
}

/// Evaluate an assoc/conj chain using transient operations.
///
/// Instead of creating N intermediate persistent collections, we:
/// 1. Evaluate the root collection (first arg of the first assoc/conj)
/// 2. Convert to transient
/// 3. Apply each assoc!/conj! mutably
/// 4. Convert back to persistent
/// 5. Bind only the final name (and intermediate names point to intermediate
///    transient values for correctness, though they shouldn't be used).
fn eval_virtualized_chain(
    chain: &crate::virtualize::LetChain,
    pairs: &[&[Form]],
    env: &mut Env,
) -> Result<(), EvalError> {
    use cljrs_builtins::transients::{
        builtin_assoc_bang, builtin_conj_bang, builtin_persistent_bang, builtin_transient,
    };

    // Step 1: Evaluate the root collection (first arg of the first call).
    let first_pair = pairs[chain.start];
    let first_expr_forms = match &first_pair[1].kind {
        FormKind::List(forms) => forms,
        _ => unreachable!("chain detection ensures this is a list"),
    };
    let root_collection = eval(&first_expr_forms[1], env)?;

    // Step 2: Convert to transient.
    let mut transient = match builtin_transient(std::slice::from_ref(&root_collection)) {
        Ok(t) => t,
        Err(_) => {
            // Collection doesn't support transients (e.g., sorted map).
            // Fall back to normal evaluation for the whole chain.
            return eval_chain_normally(chain, pairs, env);
        }
    };

    // Step 3: Apply each chain operation using transient mutation.
    for j in 0..chain.len {
        let pair_idx = chain.start + j;
        let pair = pairs[pair_idx];
        let expr_forms = match &pair[1].kind {
            FormKind::List(forms) => forms,
            _ => unreachable!(),
        };

        // Evaluate the non-collection arguments.
        let mut args = vec![transient.clone()];
        for arg_form in expr_forms.iter().skip(2) {
            args.push(eval(arg_form, env)?);
        }

        // Apply the transient operation.
        transient = match chain.ops[j] {
            crate::virtualize::ChainOpKind::Assoc => {
                builtin_assoc_bang(&args).map_err(|e| EvalError::Runtime(e.to_string()))?
            }
            crate::virtualize::ChainOpKind::Conj => {
                builtin_conj_bang(&args).map_err(|e| EvalError::Runtime(e.to_string()))?
            }
        };

        // Bind intermediate names to a placeholder (they shouldn't be used,
        // but we need them in the env for structural correctness).
        // Only the last binding gets the persistent result.
        if j < chain.len - 1 {
            let name = match &pair[0].kind {
                FormKind::Symbol(s) => s.clone(),
                _ => unreachable!(),
            };
            // Bind to nil as placeholder — intermediates are verified as unused.
            env.bind(Arc::from(name.as_str()), Value::Nil);
        }
    }

    // Step 4: Convert back to persistent.
    let persistent =
        builtin_persistent_bang(&[transient]).map_err(|e| EvalError::Runtime(e.to_string()))?;

    // Step 5: Bind the final name.
    let last_pair = pairs[chain.start + chain.len - 1];
    bind_pattern(&last_pair[0], persistent, env)?;

    Ok(())
}

/// Fallback: evaluate a chain using normal persistent operations.
fn eval_chain_normally(
    chain: &crate::virtualize::LetChain,
    pairs: &[&[Form]],
    env: &mut Env,
) -> Result<(), EvalError> {
    for j in 0..chain.len {
        let pair_idx = chain.start + j;
        let pair = pairs[pair_idx];
        let val = eval(&pair[1], env)?;
        bind_pattern(&pair[0], val, env)?;
    }
    Ok(())
}

// ── loop* / recur ─────────────────────────────────────────────────────────────

pub fn eval_loop(args: &[Form], env: &mut Env) -> EvalResult {
    let bindings = match args.first().map(|f| &f.kind) {
        Some(FormKind::Vector(v)) => v.clone(),
        _ => return Err(EvalError::Runtime("loop* requires a binding vector".into())),
    };

    if bindings.len() % 2 != 0 {
        return Err(EvalError::Runtime(
            "loop* binding vector must have even length".into(),
        ));
    }

    let body = &args[1..];

    // Separate pattern forms and initial values.
    let patterns: Vec<Form> = bindings.iter().step_by(2).cloned().collect();
    let mut current_vals: Vec<Value> = Vec::new();

    // Evaluate initial values.
    for i in (1..bindings.len()).step_by(2) {
        current_vals.push(eval(&bindings[i], env)?);
    }

    loop {
        // Root current_vals so they survive GC — they're not yet bound in env.
        let _vals_root = cljrs_env::gc_roots::root_values(&current_vals);

        // GC safepoint on every loop iteration so tight recur loops
        // don't starve the collector.
        cljrs_env::gc_roots::gc_safepoint(env);

        // Under no-gc: push a fresh scratch region for this iteration.
        // Intermediates allocated in the body land here and are freed
        // after the iteration ends.
        #[cfg(feature = "no-gc")]
        let mut scratch = cljrs_gc::alloc_ctx::ScratchGuard::new();

        // Under GC: scope this iteration's heap allocations in a fresh alloc
        // frame.  Every value the body allocates is rooted (via ALLOC_ROOTS)
        // only until this frame drops at the end of the iteration; the
        // intermediates — and the previous iteration's now-dead recur values —
        // then become collectable, instead of being pinned for the lifetime of
        // the enclosing top-level form.  `result` (the return value or recur
        // values) is moved out before the frame drops and is re-rooted at the
        // top of the next iteration (`root_values`) or by the caller during
        // return unwinding; no GC safepoint runs in the interval, exactly as
        // the IR/JIT dispatch seam relies on (see cljrs-eval `apply.rs`).
        #[cfg(not(feature = "no-gc"))]
        let _iter_frame = cljrs_gc::push_alloc_frame();

        env.push_frame();
        for (pat, val) in patterns.iter().zip(current_vals.iter()) {
            if let Err(e) = bind_pattern(pat, val.clone(), env) {
                env.pop_frame();
                return Err(e);
            }
        }

        // Under no-gc: pop scratch before tail expression so the return value
        // or recur args are allocated in the enclosing scope's context.
        #[cfg(not(feature = "no-gc"))]
        let result = eval_body_recur(body, env);
        #[cfg(feature = "no-gc")]
        let result = eval_body_with_scratch_loop(body, &mut scratch, env);

        env.pop_frame();
        // scratch / _iter_frame drop at the end of the iteration (after the
        // match below), freeing this iteration's intermediates.

        match result {
            Ok(v) => return Ok(v),
            Err(EvalError::Recur(new_vals)) => {
                if new_vals.len() != patterns.len() {
                    return Err(EvalError::Arity {
                        name: "recur".into(),
                        expected: patterns.len().to_string(),
                        got: new_vals.len(),
                    });
                }
                // new_vals were evaluated in the caller's context (scratch was
                // popped before the tail form), so they survive the reset.
                current_vals = new_vals;
            }
            Err(e) => return Err(e),
        }
    }
}

fn eval_recur(args: &[Form], env: &mut Env) -> EvalResult {
    let vals: Vec<Value> = args
        .iter()
        .map(|f| eval(f, env))
        .collect::<EvalResult<_>>()?;
    Err(EvalError::Recur(vals))
}

/// Eval body forms, propagating Recur without catching it.
pub fn eval_body_recur(body: &[Form], env: &mut Env) -> EvalResult {
    let mut result = Value::Nil;
    for form in body {
        result = eval(form, env)?;
    }
    Ok(result)
}

/// Under `no-gc`: eval loop body with scratch-region semantics.
///
/// Evaluates all non-tail forms inside the scratch region, then pops the
/// scratch before the tail (return/recur) expression so it lands in the
/// caller's context.
#[cfg(feature = "no-gc")]
fn eval_body_with_scratch_loop(
    body: &[Form],
    scratch: &mut cljrs_gc::alloc_ctx::ScratchGuard,
    env: &mut Env,
) -> EvalResult {
    if body.is_empty() {
        scratch.pop_for_return();
        return Ok(Value::Nil);
    }
    for form in &body[..body.len() - 1] {
        eval(form, env)?;
    }
    scratch.pop_for_return();
    eval(&body[body.len() - 1], env)
}

// ── quote ─────────────────────────────────────────────────────────────────────

fn eval_quote(args: &[Form]) -> EvalResult {
    match args.first() {
        Some(f) => Ok(form_to_value(f)),
        None => Err(EvalError::Runtime("quote requires an argument".into())),
    }
}

// ── var ───────────────────────────────────────────────────────────────────────

fn eval_var(args: &[Form], env: &mut Env) -> EvalResult {
    let sym = match args.first().map(|f| &f.kind) {
        Some(FormKind::Symbol(s)) => s.clone(),
        _ => return Err(EvalError::Runtime("var requires a symbol".into())),
    };
    let parsed = cljrs_value::Symbol::parse(&sym);
    let ns: Arc<str> = match parsed.namespace.as_deref() {
        Some(ns_part) => env
            .globals
            .resolve_alias(&env.current_ns, ns_part)
            .unwrap_or_else(|| Arc::from(ns_part)),
        None => env.current_ns.clone(),
    };
    let name = parsed.name.as_ref();
    env.globals
        .lookup_var_in_ns(&ns, name)
        .map(Value::Var)
        .ok_or_else(|| EvalError::UnboundSymbol(sym))
}

// ── set! ──────────────────────────────────────────────────────────────────────

fn eval_set_bang(args: &[Form], env: &mut Env) -> EvalResult {
    let sym = match args.first().map(|f| &f.kind) {
        Some(FormKind::Symbol(s)) => s.clone(),
        _ => return Err(EvalError::Runtime("set! requires a symbol".into())),
    };
    let val = if args.len() > 1 {
        eval(&args[1], env)?
    } else {
        Value::Nil
    };
    let parsed = cljrs_value::Symbol::parse(&sym);
    let ns = parsed.namespace.as_deref().unwrap_or(&env.current_ns);
    let var = env
        .globals
        .lookup_var_in_ns(ns, &parsed.name)
        .ok_or_else(|| EvalError::UnboundSymbol(sym))?;
    // Prefer updating the thread-local binding if one exists.
    if !cljrs_env::dynamics::set_thread_local(&var, val.clone()) {
        var.get().bind(val.clone());
    }
    Ok(val)
}

// ── throw ─────────────────────────────────────────────────────────────────────

fn eval_throw(args: &[Form], env: &mut Env) -> EvalResult {
    let val = match args.first() {
        Some(f) => eval(f, env)?,
        None => Value::Nil,
    };
    // Wrap non-error values in an ExceptionInfo so try/catch always sees a
    // Value::Error and ex-message / ex-data work uniformly inside the handler.
    let val = match val {
        Value::Error(_) => val,
        other => {
            let msg = format!("{}", other);
            Value::Error(GcPtr::new(ExceptionInfo::new(
                ValueError::Other(msg.clone()),
                msg,
                None,
                None,
            )))
        }
    };
    Err(EvalError::Thrown(val))
}

// ── try ───────────────────────────────────────────────────────────────────────

/// A catch clause: `(catch Type binding body...)`.
///
/// Public so the async evaluator (`cljrs-async`) can reuse `parse_try_args` to
/// build a yielding `try`/`catch`.
pub struct CatchClause<'a> {
    pub type_sym: &'a str,
    pub binding: &'a str,
    pub body: &'a [Form],
}

/// Convert a non-Thrown EvalError into a `Value::Error` so it can be bound
/// inside a catch clause and inspected with `ex-message` / `ex-data`.
///
/// Public so the async evaluator can convert errors the same way `try` does.
pub fn eval_error_to_value(err: &EvalError) -> Value {
    let msg = err.to_string();
    Value::Error(GcPtr::new(ExceptionInfo::new(
        ValueError::Other(msg.clone()),
        msg,
        None,
        None,
    )))
}

/// Test whether the type symbol on a `(catch <Type> e ...)` clause matches a
/// thrown value. Type names are matched by their last `.`-separated segment so
/// fully-qualified names like `java.lang.Exception` work as well as bare ones.
pub fn catch_type_matches(type_name: &str, val: &Value) -> bool {
    let short = type_name.rsplit('.').next().unwrap_or(type_name);
    match short {
        // Catch-all (matches any value, error or not — back-compat).
        // `:default` is the ClojureScript universal catch keyword; it arrives
        // here as the keyword's name ("default") via `parse_try_args`.
        "Object" | "Exception" | "Throwable" | "Error" | "default" => true,
        // ExceptionInfo only matches actual ex-info / Exception values.
        "ExceptionInfo" => matches!(val, Value::Error(_)),
        _ => false,
    }
}

fn eval_try(args: &[Form], env: &mut Env) -> EvalResult {
    let (body, catches, fin_body) = parse_try_args(args);

    let mut result = eval_body(body, env);

    // Handle catch: never intercept Recur (loop trampoline signal).
    let err_opt = match std::mem::replace(&mut result, Ok(Value::Nil)) {
        Ok(v) => {
            result = Ok(v);
            None
        }
        Err(EvalError::Recur(args)) => {
            result = Err(EvalError::Recur(args));
            None
        }
        Err(other) => Some(other),
    };

    if let Some(err) = err_opt {
        let thrown_val = match err {
            EvalError::Thrown(v) => v,
            ref other => eval_error_to_value(other),
        };
        let mut handled = false;
        for c in &catches {
            if catch_type_matches(c.type_sym, &thrown_val) {
                env.push_frame();
                env.bind(Arc::from(c.binding), thrown_val.clone());
                result = eval_body(c.body, env);
                env.pop_frame();
                handled = true;
                break;
            }
        }
        if !handled {
            // No matching catch — re-throw.
            result = Err(EvalError::Thrown(thrown_val));
        }
    }

    // Always run finally.  Its normal (Ok) value is discarded — `try` returns
    // the body/catch result — but an exception thrown from `finally` supersedes
    // the pending result or exception (matching JVM/Clojure semantics).
    if !fin_body.is_empty() {
        eval_body(fin_body, env)?;
    }

    result
}

/// Split try args into (body, catch clauses, finally body).
///
/// Public so the async evaluator can parse `try` forms identically.
pub fn parse_try_args(args: &[Form]) -> (&[Form], Vec<CatchClause<'_>>, &[Form]) {
    let mut body_end = args.len();
    let mut catches: Vec<CatchClause<'_>> = Vec::new();
    let mut fin_body: &[Form] = &[];

    for (i, form) in args.iter().enumerate() {
        if let FormKind::List(parts) = &form.kind
            && let Some(FormKind::Symbol(s)) = parts.first().map(|f| &f.kind)
        {
            if s == "catch" {
                if i < body_end {
                    body_end = i;
                }
                // The catch type may be a symbol (`Throwable`, `java.lang.Exception`),
                // the ClojureScript `:default` catch-all keyword, or a reader
                // conditional `#?(:rust Exception :clj ...)` whose selected branch
                // resolves to one of those.
                let type_sym = match parts.get(1).map(|f| &f.kind) {
                    Some(FormKind::Symbol(s)) => s.as_str(),
                    Some(FormKind::Keyword(s)) => s.as_str(),
                    Some(FormKind::ReaderCond {
                        splicing: false,
                        clauses,
                    }) => match select_reader_cond(clauses).map(|f| &f.kind) {
                        Some(FormKind::Symbol(s)) => s.as_str(),
                        Some(FormKind::Keyword(s)) => s.as_str(),
                        _ => continue,
                    },
                    _ => continue,
                };
                let binding = match parts.get(2).map(|f| &f.kind) {
                    Some(FormKind::Symbol(s)) => s.as_str(),
                    _ => continue,
                };
                catches.push(CatchClause {
                    type_sym,
                    binding,
                    body: &parts[3..],
                });
                continue;
            }
            if s == "finally" {
                if i < body_end {
                    body_end = i;
                }
                fin_body = &parts[1..];
                continue;
            }
        }
    }

    (&args[..body_end], catches, fin_body)
}

// ── defn ──────────────────────────────────────────────────────────────────────

pub fn eval_defn(args: &[Form], env: &mut Env) -> EvalResult {
    // The name may carry reader metadata, e.g. `(defn ^:async fetch ...)`.
    let (name, mut is_async) = match args.first().map(|f| &f.kind) {
        Some(FormKind::Symbol(s)) => (s.clone(), false),
        Some(FormKind::Meta(meta, inner)) => match &inner.kind {
            FormKind::Symbol(s) => (s.clone(), meta_form_is_async(meta)),
            _ => return Err(EvalError::Runtime("defn name must be a symbol".into())),
        },
        _ => return Err(EvalError::Runtime("defn requires a symbol name".into())),
    };
    // Skip optional docstring and/or metadata map after the name.
    // Valid orderings: (defn name body...), (defn name "doc" body...),
    // (defn name {:meta ...} body...), (defn name "doc" {:meta ...} body...).
    let mut rest_start = 1;
    if rest_start < args.len() && matches!(args[rest_start].kind, FormKind::Str(_)) {
        rest_start += 1;
    }
    if rest_start < args.len() && matches!(args[rest_start].kind, FormKind::Map(_)) {
        // An attr-map such as `{:async true}` can also request async dispatch.
        is_async |= meta_form_is_async(&args[rest_start]);
        rest_start += 1;
    }
    // Build (fn* name ...)
    let mut fn_args = vec![Form::new(
        FormKind::Symbol(name.to_string()),
        args[0].span.clone(),
    )];
    fn_args.extend_from_slice(&args[rest_start..]);
    // Under no-gc: the Fn object must live in the StaticArena since the Var
    // intern outlives all scratch regions.
    #[cfg(feature = "no-gc")]
    let _static_ctx = cljrs_gc::alloc_ctx::StaticCtxGuard::new();
    let mut fn_val = eval_fn(&fn_args, env)?;
    if is_async && let Value::Fn(ref mut f) = fn_val {
        f.get_mut().is_async = true;
    }
    let var = env
        .globals
        .intern(&env.current_ns, Arc::from(name.as_str()), fn_val.clone());
    Ok(Value::Var(var))
}

// ── defmacro ──────────────────────────────────────────────────────────────────

/// Prepend `&form` and `&env` Form symbols to an arity form's parameter vector.
///
/// Handles:
/// - Single-arity vector `[params...]` → `[&form &env params...]`
/// - Multi-arity clause list `([params...] body...)` → `([&form &env params...] body...)`
fn prepend_macro_params(form: &Form) -> Form {
    let span = form.span.clone();
    match &form.kind {
        FormKind::Vector(params) => {
            let mut new_params = vec![
                Form::new(FormKind::Symbol("&form".to_string()), span.clone()),
                Form::new(FormKind::Symbol("&env".to_string()), span.clone()),
            ];
            new_params.extend_from_slice(params);
            Form::new(FormKind::Vector(new_params), span)
        }
        FormKind::List(forms) => {
            // Arity clause: ([params...] body...) — prepend to first element (params vector).
            if let Some(first) = forms.first() {
                let new_params_form = prepend_macro_params(first);
                let mut new_forms = vec![new_params_form];
                new_forms.extend_from_slice(&forms[1..]);
                Form::new(FormKind::List(new_forms), span)
            } else {
                form.clone()
            }
        }
        _ => form.clone(),
    }
}

fn eval_defmacro(args: &[Form], env: &mut Env) -> EvalResult {
    let name = require_sym(args, 0, "defmacro")?;
    let mut rest_start = 1;
    if rest_start < args.len() && matches!(args[rest_start].kind, FormKind::Str(_)) {
        rest_start += 1;
    }
    if rest_start < args.len() && matches!(args[rest_start].kind, FormKind::Map(_)) {
        rest_start += 1;
    }
    // Prepend implicit &form and &env params to each arity.
    let mut fn_args = vec![Form::new(
        FormKind::Symbol(name.to_string()),
        args[0].span.clone(),
    )];
    for form in &args[rest_start..] {
        fn_args.push(prepend_macro_params(form));
    }
    // Under no-gc: the Macro object must live in the StaticArena since the Var
    // intern outlives all scratch regions.
    #[cfg(feature = "no-gc")]
    let _static_ctx = cljrs_gc::alloc_ctx::StaticCtxGuard::new();
    let fn_val = eval_fn(&fn_args, env)?;

    // Convert Fn → Macro by setting is_macro = true.
    let macro_val = match fn_val {
        Value::Fn(f) => {
            let mut mfn = f.get().clone();
            mfn.is_macro = true;
            Value::Macro(GcPtr::new(mfn))
        }
        other => other,
    };

    let var = env
        .globals
        .intern(&env.current_ns, Arc::from(name), macro_val.clone());
    Ok(Value::Var(var))
}

// ── defonce ───────────────────────────────────────────────────────────────────

fn eval_defonce(args: &[Form], env: &mut Env) -> EvalResult {
    let name = require_sym(args, 0, "defonce")?;
    // If already bound, return immediately.
    if let Some(var) = env.globals.lookup_var(&env.current_ns, name)
        && var.get().is_bound()
    {
        return Ok(Value::Var(var));
    }
    eval_def(args, env)
}

// ── and / or ──────────────────────────────────────────────────────────────────

fn eval_and(args: &[Form], env: &mut Env) -> EvalResult {
    let mut result = Value::Bool(true);
    for form in args {
        result = eval(form, env)?;
        if matches!(result, Value::Nil | Value::Bool(false)) {
            return Ok(result);
        }
    }
    Ok(result)
}

fn eval_or(args: &[Form], env: &mut Env) -> EvalResult {
    let mut last = Value::Nil;
    for form in args {
        last = eval(form, env)?;
        if !matches!(last, Value::Nil | Value::Bool(false)) {
            return Ok(last);
        }
    }
    Ok(last)
}

// ── require ───────────────────────────────────────────────────────────────────

fn eval_require(args: &[Form], env: &mut Env) -> EvalResult {
    for arg in args {
        let val = eval(arg, env)?;
        let spec = parse_require_spec_val(val).map_err(EvalError::Runtime)?;
        load_ns(env.globals.clone(), &spec, &env.current_ns)?;
    }
    Ok(Value::Nil)
}

/// Parse a `RequireSpec` from an already-evaluated `Value`.
/// Accepts: `'some.ns`, `['some.ns :as alias]`, `['some.ns :refer [syms]]`,
/// `['some.ns :refer :all]`, and versioned forms like `'some.ns@abc1234` or
/// `['some.ns@abc1234 :as alias]`.
fn parse_require_spec_val(val: Value) -> Result<RequireSpec, String> {
    match val {
        Value::Symbol(s) => {
            let sym = s.get();
            Ok(RequireSpec {
                ns: sym.name.clone(),
                version: sym.version.clone(),
                alias: None,
                refer: RequireRefer::None,
            })
        }
        Value::Vector(v) => {
            let items: Vec<Value> = v.get().iter().cloned().collect();
            if items.is_empty() {
                return Err("require spec vector must not be empty".into());
            }
            let (ns, version) = match &items[0] {
                Value::Symbol(s) => {
                    let sym = s.get();
                    (sym.name.clone(), sym.version.clone())
                }
                other => {
                    return Err(format!(
                        "require spec: first element must be a symbol, got {}",
                        other.type_name()
                    ));
                }
            };
            let mut alias = None;
            let mut refer = RequireRefer::None;
            let mut i = 1;
            while i < items.len() {
                match &items[i] {
                    Value::Keyword(k) if k.get().name.as_ref() == "as" => {
                        i += 1;
                        alias = Some(match items.get(i) {
                            Some(Value::Symbol(s)) => s.get().name.clone(),
                            _ => return Err("require :as expects a symbol".into()),
                        });
                    }
                    Value::Keyword(k) if k.get().name.as_ref() == "refer" => {
                        i += 1;
                        refer = match items.get(i) {
                            Some(Value::Keyword(k2)) if k2.get().name.as_ref() == "all" => {
                                RequireRefer::All
                            }
                            Some(Value::Vector(rv)) => {
                                let names: Vec<Arc<str>> = rv
                                    .get()
                                    .iter()
                                    .map(|v| match v {
                                        Value::Symbol(s) => Ok(s.get().name.clone()),
                                        other => Err(format!(
                                            "require :refer expects symbols, got {}",
                                            other.type_name()
                                        )),
                                    })
                                    .collect::<Result<_, _>>()?;
                                RequireRefer::Named(names)
                            }
                            _ => {
                                return Err(
                                    "require :refer expects :all or a vector of symbols".into()
                                );
                            }
                        };
                    }
                    other => {
                        return Err(format!(
                            "require spec: unexpected option {}",
                            other.type_name()
                        ));
                    }
                }
                i += 1;
            }
            Ok(RequireSpec {
                ns,
                version,
                alias,
                refer,
            })
        }
        other => Err(format!(
            "require expects a symbol or vector, got {}",
            other.type_name()
        )),
    }
}

/// Parse a `RequireSpec` from a raw `Form` (unevaluated, used in `ns` macro).
/// Also handles versioned namespace symbols such as `my.ns@abc1234`.
fn parse_require_spec_form(form: &Form) -> Result<RequireSpec, String> {
    match &form.kind {
        FormKind::Symbol(s) => {
            let sym = cljrs_value::Symbol::parse(s);
            Ok(RequireSpec {
                ns: sym.name.clone(),
                version: sym.version.clone(),
                alias: None,
                refer: RequireRefer::None,
            })
        }
        FormKind::Vector(items) => {
            if items.is_empty() {
                return Err("require spec vector must not be empty".into());
            }
            let (ns, version) = match &items[0].kind {
                FormKind::Symbol(s) => {
                    let sym = cljrs_value::Symbol::parse(s);
                    (sym.name.clone(), sym.version.clone())
                }
                _ => return Err("require spec: first element must be a symbol".into()),
            };
            let mut alias = None;
            let mut refer = RequireRefer::None;
            let mut i = 1;
            while i < items.len() {
                // Resolve reader conditionals inline (e.g. #?(:cljs :refer-macros :default :refer)).
                let item = match &items[i].kind {
                    FormKind::ReaderCond { clauses, .. } => {
                        match select_reader_cond(clauses) {
                            Some(f) => f,
                            None => {
                                i += 1;
                                continue;
                            } // no matching branch — skip
                        }
                    }
                    _ => &items[i],
                };
                match &item.kind {
                    FormKind::Keyword(k) if k == "as" => {
                        i += 1;
                        alias = Some(match items.get(i).map(|f| &f.kind) {
                            Some(FormKind::Symbol(s)) => Arc::from(s.as_str()),
                            _ => return Err("require :as expects a symbol".into()),
                        });
                    }
                    FormKind::Keyword(k) if k == "refer" => {
                        i += 1;
                        refer = match items.get(i).map(|f| &f.kind) {
                            Some(FormKind::Keyword(k2)) if k2 == "all" => RequireRefer::All,
                            Some(FormKind::Vector(rv)) => {
                                let names: Vec<Arc<str>> = rv
                                    .iter()
                                    .map(|f| match &f.kind {
                                        FormKind::Symbol(s) => Ok(Arc::from(s.as_str())),
                                        _ => Err("require :refer expects symbols".to_string()),
                                    })
                                    .collect::<Result<_, _>>()?;
                                RequireRefer::Named(names)
                            }
                            _ => return Err("require :refer expects :all or a vector".into()),
                        };
                    }
                    _ => return Err(format!("require spec: unexpected form at position {i}")),
                }
                i += 1;
            }
            Ok(RequireSpec {
                ns,
                version,
                alias,
                refer,
            })
        }
        _ => Err("require spec must be a symbol or vector".into()),
    }
}

// ── ns ────────────────────────────────────────────────────────────────────────

fn eval_ns(args: &[Form], env: &mut Env) -> EvalResult {
    let name = require_sym(args, 0, "ns")?;
    env.globals.get_or_create_ns(name);
    env.current_ns = Arc::from(name);
    // Auto-refer clojure.core (Clojure default behaviour).
    if name != "clojure.core" {
        env.globals.refer_all(name, "clojure.core");
    }
    sync_star_ns(env);

    for clause in &args[1..] {
        if let FormKind::List(items) = &clause.kind {
            match items.first().map(|f| &f.kind) {
                Some(FormKind::Keyword(k)) if k == "require" => {
                    // Expand reader conditionals among require specs
                    let expanded = expand_reader_conds(&items[1..]);
                    for spec_form in &expanded {
                        let spec =
                            parse_require_spec_form(spec_form).map_err(EvalError::Runtime)?;
                        load_ns(env.globals.clone(), &spec, name)?;
                    }
                }
                // Other clauses (:refer-clojure, :use, :import) — skip for now.
                _ => {}
            }
        }
    }

    let ns_ptr = env.globals.get_or_create_ns(&env.current_ns);
    Ok(Value::Namespace(ns_ptr))
}

// ── load-file ─────────────────────────────────────────────────────────────────

fn eval_load_file(args: &[Form], env: &mut Env) -> EvalResult {
    if args.is_empty() {
        return Err(EvalError::Runtime(
            "load-file requires a path argument".into(),
        ));
    }
    let path_val = eval(&args[0], env)?;
    let path = match &path_val {
        Value::Str(s) => s.get().clone(),
        v => {
            return Err(EvalError::Runtime(format!(
                "load-file: expected string, got {}",
                v.type_name()
            )));
        }
    };
    let src = std::fs::read_to_string(&path)
        .map_err(|e| EvalError::Runtime(format!("load-file: {e}")))?;
    let mut parser = cljrs_reader::Parser::new(src, path.clone());
    let forms = parser
        .parse_all()
        .map_err(|e| EvalError::Runtime(format!("load-file parse error: {e}")))?;
    let mut result = Value::Nil;
    for form in forms {
        let _alloc_frame = cljrs_gc::push_alloc_frame();
        result = eval(&form, env)?;
    }
    Ok(result)
}

// ── letfn ─────────────────────────────────────────────────────────────────────

fn eval_letfn(args: &[Form], env: &mut Env) -> EvalResult {
    // (letfn [(f [params] body...) ...] body...)
    let bindings = match args.first().map(|f| &f.kind) {
        Some(FormKind::Vector(v)) => v.clone(),
        _ => return Err(EvalError::Runtime("letfn requires a binding vector".into())),
    };

    env.push_frame();

    for binding in &bindings {
        if let FormKind::List(parts) = &binding.kind {
            if parts.is_empty() {
                continue;
            }
            // parts[0] = name, parts[1] = params, parts[2..] = body
            // Reuse eval_fn: it expects (optional-name params body...)
            // We pass parts directly since parts[0] is the function name symbol.
            let fn_val = match eval_fn(parts, env) {
                Ok(v) => v,
                Err(e) => {
                    env.pop_frame();
                    return Err(e);
                }
            };
            let name = match &parts[0].kind {
                FormKind::Symbol(s) => s.clone(),
                _ => {
                    env.pop_frame();
                    return Err(EvalError::Runtime(
                        "letfn binding name must be a symbol".into(),
                    ));
                }
            };
            env.bind(Arc::from(name.as_str()), fn_val);
        }
    }

    let body = &args[1..];
    let result = eval_body(body, env);
    env.pop_frame();
    result
}

// ── in-ns ─────────────────────────────────────────────────────────────────────

fn eval_in_ns(args: &[Form], env: &mut Env) -> EvalResult {
    // (in-ns 'foo.bar)
    if args.is_empty() {
        return Err(EvalError::Runtime("in-ns requires a namespace name".into()));
    }
    let ns_val = eval(&args[0], env)?;
    let ns_name = extract_ns_name(&ns_val)?;
    env.globals.get_or_create_ns(&ns_name);
    env.globals.refer_all(&ns_name, "clojure.core");
    env.current_ns = Arc::from(ns_name.as_str());
    sync_star_ns(env);
    let ns_ptr = env.globals.get_or_create_ns(&env.current_ns);
    Ok(Value::Namespace(ns_ptr))
}

// ── alias ─────────────────────────────────────────────────────────────────────

fn eval_alias(args: &[Form], env: &mut Env) -> EvalResult {
    // (alias 'short 'some.long.ns)
    if args.len() < 2 {
        return Err(EvalError::Runtime(
            "alias requires alias-sym and namespace-sym".into(),
        ));
    }
    let alias_val = eval(&args[0], env)?;
    let ns_val = eval(&args[1], env)?;

    let alias_name = extract_ns_name(&alias_val)?;
    let full_ns = extract_ns_name(&ns_val)?;

    let ns_ptr = env.globals.get_or_create_ns(&env.current_ns);
    let mut aliases = ns_ptr.get().aliases.lock().unwrap();
    aliases.insert(Arc::from(alias_name.as_str()), Arc::from(full_ns.as_str()));
    Ok(Value::Nil)
}

/// Extract a namespace-name string from a Value::Symbol, Value::Str, or Value::Keyword.
fn extract_ns_name(v: &Value) -> EvalResult<String> {
    match v {
        Value::Symbol(s) => {
            // Use the full name (e.g. "clojure.core").
            Ok(s.get().name.as_ref().to_string())
        }
        Value::Str(s) => Ok(s.get().clone()),
        Value::Keyword(k) => Ok(k.get().name.as_ref().to_string()),
        other => Err(EvalError::Runtime(format!(
            "expected a symbol or string for namespace name, got {}",
            other.type_name()
        ))),
    }
}

// ── defprotocol ───────────────────────────────────────────────────────────────

fn eval_defprotocol(args: &[Form], env: &mut Env) -> EvalResult {
    // (defprotocol Name "doc?" (method [this & args] "doc?") ...)
    let name = require_sym(args, 0, "defprotocol")?;
    let proto_name: Arc<str> = Arc::from(name);

    // Skip optional docstring.
    let methods_start = if args.len() > 1 && matches!(args[1].kind, FormKind::Str(_)) {
        2
    } else {
        1
    };

    let mut methods: Vec<ProtocolMethod> = Vec::new();

    for form in &args[methods_start..] {
        // Each method spec is (method-name [params...] "doc"?)
        let parts = match &form.kind {
            FormKind::List(parts) => parts,
            _ => continue, // skip unknown forms
        };
        if parts.is_empty() {
            continue;
        }
        let method_name = match &parts[0].kind {
            FormKind::Symbol(s) => Arc::from(s.as_str()),
            _ => continue,
        };
        // Find the parameter vector (first vector in parts).
        let (min_arity, variadic) = if let Some(params_form) =
            parts.iter().find(|f| matches!(f.kind, FormKind::Vector(_)))
        {
            if let FormKind::Vector(param_forms) = &params_form.kind {
                let variadic = param_forms
                    .iter()
                    .any(|p| matches!(&p.kind, FormKind::Symbol(s) if s == "&"));
                let fixed: usize = param_forms
                    .iter()
                    .filter(|p| !matches!(&p.kind, FormKind::Symbol(s) if s == "&"))
                    .count();
                (fixed, variadic)
            } else {
                (1, false)
            }
        } else {
            (1, false)
        };
        methods.push(ProtocolMethod {
            name: method_name,
            min_arity,
            variadic,
        });
    }

    let ns: Arc<str> = env.current_ns.clone();
    let proto = Protocol::new(proto_name.clone(), ns, methods.clone());
    let proto_ptr = GcPtr::new(proto);

    // Intern the protocol itself.
    let proto_var = env.globals.intern(
        &env.current_ns,
        proto_name.clone(),
        Value::Protocol(proto_ptr.clone()),
    );

    // Create and intern a ProtocolFn for each method.
    for method in &methods {
        let pf = ProtocolFn {
            protocol: proto_ptr.clone(),
            method_name: method.name.clone(),
            min_arity: method.min_arity,
            variadic: method.variadic,
        };
        env.globals.intern(
            &env.current_ns,
            method.name.clone(),
            Value::ProtocolFn(GcPtr::new(pf)),
        );
    }

    Ok(Value::Var(proto_var))
}

// ── extend-type ───────────────────────────────────────────────────────────────

fn eval_extend_type(args: &[Form], env: &mut Env) -> EvalResult {
    // (extend-type TypeSym Proto1 (m [this] body) ... Proto2 ...)
    if args.is_empty() {
        return Err(EvalError::Runtime(
            "extend-type requires a type symbol".into(),
        ));
    }
    let type_sym = match &args[0].kind {
        FormKind::Symbol(s) => s.clone(),
        _ => {
            return Err(EvalError::Runtime(
                "extend-type: first arg must be a type symbol".into(),
            ));
        }
    };
    let type_tag = crate::apply::resolve_type_tag(&type_sym);

    let mut current_proto: Option<GcPtr<Protocol>> = None;

    for form in &args[1..] {
        match &form.kind {
            FormKind::Symbol(s) => {
                // Look up protocol in env.
                let val = env.globals.lookup_in_ns(&env.current_ns, s);
                match val {
                    Some(Value::Protocol(p)) => {
                        current_proto = Some(p);
                    }
                    _ => {
                        return Err(EvalError::Runtime(format!(
                            "extend-type: {} is not a protocol",
                            s
                        )));
                    }
                }
            }
            FormKind::List(parts) => {
                // (method-name [params] body...)
                let proto = current_proto.as_ref().ok_or_else(|| {
                    EvalError::Runtime("extend-type: method before protocol name".into())
                })?;
                if parts.is_empty() {
                    continue;
                }
                let method_name = match &parts[0].kind {
                    FormKind::Symbol(s) => Arc::from(s.as_str()),
                    _ => continue,
                };
                let fn_val = build_impl_fn(parts, env)?;
                let mut impls = proto.get().impls.lock().unwrap();
                impls
                    .entry(type_tag.clone())
                    .or_default()
                    .insert(method_name, fn_val);
                drop(impls);
                cljrs_value::bump_protocol_generation();
            }
            _ => {}
        }
    }

    Ok(Value::Nil)
}

// ── extend-protocol ───────────────────────────────────────────────────────────

fn eval_extend_protocol(args: &[Form], env: &mut Env) -> EvalResult {
    // (extend-protocol Proto Type1 (m [this] body) ... Type2 ...)
    if args.is_empty() {
        return Err(EvalError::Runtime(
            "extend-protocol requires a protocol".into(),
        ));
    }
    let proto_sym = match &args[0].kind {
        FormKind::Symbol(s) => s.clone(),
        _ => {
            return Err(EvalError::Runtime(
                "extend-protocol: first arg must be a protocol symbol".into(),
            ));
        }
    };
    let proto_val = env.globals.lookup_in_ns(&env.current_ns, &proto_sym);
    let proto_ptr = match proto_val {
        Some(Value::Protocol(p)) => p,
        _ => {
            return Err(EvalError::Runtime(format!(
                "extend-protocol: {} is not a protocol",
                proto_sym
            )));
        }
    };

    let mut current_type: Option<Arc<str>> = None;

    for form in &args[1..] {
        match &form.kind {
            FormKind::Symbol(s) => {
                current_type = Some(crate::apply::resolve_type_tag(s));
            }
            FormKind::List(parts) => {
                let type_tag = current_type.as_ref().ok_or_else(|| {
                    EvalError::Runtime("extend-protocol: method before type name".into())
                })?;
                if parts.is_empty() {
                    continue;
                }
                let method_name = match &parts[0].kind {
                    FormKind::Symbol(s) => Arc::from(s.as_str()),
                    _ => continue,
                };
                let fn_val = build_impl_fn(parts, env)?;
                let mut impls = proto_ptr.get().impls.lock().unwrap();
                impls
                    .entry(type_tag.clone())
                    .or_default()
                    .insert(method_name, fn_val);
                drop(impls);
                cljrs_value::bump_protocol_generation();
            }
            _ => {}
        }
    }

    Ok(Value::Nil)
}

/// Build a `CljxFn` from the tail of a method-impl list: `(name [params] body...)`.
/// `parts[0]` is the method name symbol (ignored here — caller handles it).
/// `parts[1]` is the params vector.
/// `parts[2..]` is the body.
fn build_impl_fn(parts: &[Form], env: &mut Env) -> EvalResult<Value> {
    if parts.len() < 2 {
        return Err(EvalError::Runtime(
            "protocol method impl requires params and body".into(),
        ));
    }
    // parts[1] should be the params vector.
    let params_form = &parts[1];
    let body = &parts[2..];
    let arity = parse_arity(params_form, body)?;
    let (closed_over_names, closed_over_vals) = env.all_local_bindings();
    let fn_name = match &parts[0].kind {
        FormKind::Symbol(s) => Some(Arc::from(s.as_str())),
        _ => None,
    };
    let cljrs_fn = CljxFn::new(
        fn_name,
        vec![arity],
        closed_over_names,
        closed_over_vals,
        false,
        Arc::clone(&env.current_ns),
    );
    Ok(Value::Fn(GcPtr::new(cljrs_fn)))
}

// ── defmulti ──────────────────────────────────────────────────────────────────

fn eval_defmulti(args: &[Form], env: &mut Env) -> EvalResult {
    // (defmulti name dispatch-fn-form) or (defmulti name "doc" dispatch-fn :default val)
    let name = require_sym(args, 0, "defmulti")?;
    let name_arc: Arc<str> = Arc::from(name);

    let rest_start = if args.len() > 2 && matches!(args[1].kind, FormKind::Str(_)) {
        2
    } else {
        1
    };

    if args.len() <= rest_start {
        return Err(EvalError::Runtime(
            "defmulti requires a dispatch function".into(),
        ));
    }

    let dispatch_fn = eval(&args[rest_start], env)?;

    // Parse optional :default val.
    let mut default_dispatch = ":default".to_string();
    let mut i = rest_start + 1;
    while i + 1 < args.len() {
        if let FormKind::Keyword(k) = &args[i].kind
            && k == "default"
        {
            let dv = eval(&args[i + 1], env)?;
            default_dispatch = format!("{}", dv);
        }
        i += 2;
    }

    let mfn = MultiFn::new(name_arc.clone(), dispatch_fn, default_dispatch);
    let var = env
        .globals
        .intern(&env.current_ns, name_arc, Value::MultiFn(GcPtr::new(mfn)));
    Ok(Value::Var(var))
}

// ── defmethod ─────────────────────────────────────────────────────────────────

fn eval_defmethod(args: &[Form], env: &mut Env) -> EvalResult {
    // (defmethod multi-name dispatch-val [params] body...)
    if args.len() < 3 {
        return Err(EvalError::Runtime(
            "defmethod requires name, dispatch-val, params, and body".into(),
        ));
    }
    let multi_name = require_sym(args, 0, "defmethod")?;

    let mf_ptr = match env.globals.lookup_in_ns(&env.current_ns, multi_name) {
        Some(Value::MultiFn(mf)) => mf,
        _ => {
            return Err(EvalError::Runtime(format!(
                "defmethod: {} is not a multimethod",
                multi_name
            )));
        }
    };

    let dispatch_val = eval(&args[1], env)?;
    let key = format!("{}", dispatch_val);

    // Build CljxFn from ([params] body...).
    let params_form = &args[2];
    let body = &args[3..];
    let arity = parse_arity(params_form, body)?;
    let (closed_over_names, closed_over_vals) = env.all_local_bindings();
    let fn_name = Some(Arc::from(multi_name));
    let cljrs_fn = CljxFn::new(
        fn_name,
        vec![arity],
        closed_over_names,
        closed_over_vals,
        false,
        Arc::clone(&env.current_ns),
    );
    let fn_val = Value::Fn(GcPtr::new(cljrs_fn));

    mf_ptr.get().methods.lock().unwrap().insert(key, fn_val);

    Ok(Value::MultiFn(mf_ptr))
}

// ── binding ───────────────────────────────────────────────────────────────────

fn eval_binding(args: &[Form], env: &mut Env) -> EvalResult {
    let pairs = match args.first().map(|f| &f.kind) {
        Some(FormKind::Vector(v)) => v.clone(),
        _ => return Err(EvalError::Runtime("binding requires a vector".into())),
    };
    if pairs.len() % 2 != 0 {
        return Err(EvalError::Runtime(
            "binding vector must have even count".into(),
        ));
    }

    let mut frame: HashMap<usize, Value> = HashMap::new();
    for pair in pairs.chunks(2) {
        let sym_str = match &pair[0].kind {
            FormKind::Symbol(s) => s.clone(),
            _ => return Err(EvalError::Runtime("binding targets must be symbols".into())),
        };
        let parsed = cljrs_value::Symbol::parse(&sym_str);
        let ns_part = parsed
            .namespace
            .as_deref()
            .unwrap_or(env.current_ns.as_ref());
        let var_ptr = env
            .globals
            .lookup_var_in_ns(ns_part, &parsed.name)
            .ok_or_else(|| EvalError::UnboundSymbol(sym_str.clone()))?;
        let val = eval(&pair[1], env)?;
        frame.insert(cljrs_env::dynamics::var_key_of(&var_ptr), val);
    }

    let _guard = cljrs_env::dynamics::push_frame(frame);
    eval_body(&args[1..], env)
    // _guard drops here → pop_frame()
}

// ── defrecord ─────────────────────────────────────────────────────────────────

fn eval_defrecord(args: &[Form], env: &mut Env) -> EvalResult {
    // (defrecord TypeName [field1 field2 ...] Proto1 (method [this] body) ...)
    if args.len() < 2 {
        return Err(EvalError::Runtime(
            "defrecord requires a name and field vector".into(),
        ));
    }
    let type_name = require_sym(args, 0, "defrecord")?;
    let type_tag: Arc<str> = Arc::from(type_name);

    // Parse field names from the vector.
    let field_names: Vec<Arc<str>> = match &args[1].kind {
        FormKind::Vector(fields) => fields
            .iter()
            .map(|f| match &f.kind {
                FormKind::Symbol(s) => Ok(Arc::from(s.as_str())),
                _ => Err(EvalError::Runtime(
                    "defrecord field names must be symbols".into(),
                )),
            })
            .collect::<EvalResult<_>>()?,
        _ => {
            return Err(EvalError::Runtime(
                "defrecord requires a field vector as second arg".into(),
            ));
        }
    };

    // Register protocol implementations (same as extend-type inner logic).
    register_impls_for_tag(&type_tag, &args[2..], env)?;

    // Generate constructors in clojure.core.
    // ->TypeName: positional constructor
    // map->TypeName: map constructor
    let ns = env.current_ns.clone();
    let globals = env.globals.clone();
    let type_tag2 = type_tag.clone();

    // Build `->TypeName` as a native-Clojure fn: (fn [f1 f2 ...] (make-type-instance "T" {:f1 f1 :f2 f2 ...}))
    {
        let params: Vec<Arc<str>> = field_names.clone();
        let rest_param = None;
        // Build body forms manually: (make-type-instance "TypeName" {:field1 field1 ...})
        use cljrs_reader::form::FormKind as FK;
        let dummy_span =
            cljrs_types::span::Span::new(std::sync::Arc::new("<defrecord>".into()), 0, 0, 1, 1);
        let make_form = |kind: FK| Form {
            kind,
            span: dummy_span.clone(),
        };
        let mut kv_forms: Vec<Form> = Vec::new();
        for f in &field_names {
            kv_forms.push(make_form(FK::Keyword(f.as_ref().to_string())));
            kv_forms.push(make_form(FK::Symbol(f.as_ref().to_string())));
        }
        let map_form = make_form(FK::Map(kv_forms));
        let body = vec![make_form(FK::List(vec![
            make_form(FK::Symbol("make-type-instance".into())),
            make_form(FK::Str(type_tag.as_ref().to_string())),
            map_form,
        ]))];
        let arity = CljxFnArity {
            params,
            rest_param,
            body,
            destructure_params: vec![],
            destructure_rest: None,
            ir_arity_id: crate::arity::fresh_arity_id(),
            param_hints: vec![],
            rest_hint: None,
        };
        let fn_name: Arc<str> = Arc::from(format!("->{}", type_name));
        let ctor = CljxFn::new(
            Some(fn_name.clone()),
            vec![arity],
            vec![],
            vec![],
            false,
            Arc::clone(&ns),
        );
        globals.intern(&ns, fn_name, Value::Fn(GcPtr::new(ctor)));
    }

    // Build `map->TypeName`: (fn [m] (make-type-instance "TypeName" m))
    {
        use cljrs_reader::form::FormKind as FK;
        let dummy_span =
            cljrs_types::span::Span::new(std::sync::Arc::new("<defrecord>".into()), 0, 0, 1, 1);
        let make_form = |kind: FK| Form {
            kind,
            span: dummy_span.clone(),
        };
        let m_sym: Arc<str> = Arc::from("m__");
        let body = vec![make_form(FK::List(vec![
            make_form(FK::Symbol("make-type-instance".into())),
            make_form(FK::Str(type_tag2.as_ref().to_string())),
            make_form(FK::Symbol(m_sym.as_ref().to_string())),
        ]))];
        let arity = CljxFnArity {
            params: vec![m_sym],
            rest_param: None,
            body,
            destructure_params: vec![],
            destructure_rest: None,
            ir_arity_id: crate::arity::fresh_arity_id(),
            param_hints: vec![],
            rest_hint: None,
        };
        let fn_name: Arc<str> = Arc::from(format!("map->{}", type_name));
        let ctor = CljxFn::new(
            Some(fn_name.clone()),
            vec![arity],
            vec![],
            vec![],
            false,
            Arc::clone(&ns),
        );
        globals.intern(&ns, fn_name, Value::Fn(GcPtr::new(ctor)));
    }

    // Intern the type name as a Symbol value so `(instance? TypeName x)` works.
    let type_sym = cljrs_value::Symbol::simple(type_name);
    globals.intern(
        &ns,
        Arc::from(type_name),
        Value::Symbol(GcPtr::new(type_sym)),
    );
    Ok(Value::Nil)
}

// ── reify ─────────────────────────────────────────────────────────────────────

fn eval_reify(args: &[Form], env: &mut Env) -> EvalResult {
    // (reify Proto1 (method [this] body) ...)
    // Generate a unique type tag for this instance.
    let n =
        cljrs_builtins::builtins::GENSYM_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    let type_tag: Arc<str> = Arc::from(format!("reify__{}", n));

    // Register protocol implementations.
    register_impls_for_tag(&type_tag, args, env)?;

    // Return an empty TypeInstance with the unique tag.
    Ok(Value::TypeInstance(GcPtr::new(TypeInstance {
        type_tag,
        fields: MapValue::empty(),
    })))
}

// ── register_impls_for_tag ────────────────────────────────────────────────────

/// Parse `Proto (method [params] body) ...` segments and register them under `type_tag`.
/// Shared by `defrecord` and `reify`.
fn register_impls_for_tag(type_tag: &Arc<str>, forms: &[Form], env: &mut Env) -> EvalResult<()> {
    let mut current_proto: Option<GcPtr<cljrs_value::Protocol>> = None;

    for form in forms {
        match &form.kind {
            FormKind::Symbol(s) => {
                let val = env.globals.lookup_in_ns(&env.current_ns, s);
                match val {
                    Some(Value::Protocol(p)) => {
                        current_proto = Some(p);
                    }
                    _ => {
                        return Err(EvalError::Runtime(format!(
                            "reify/defrecord: {} is not a protocol",
                            s
                        )));
                    }
                }
            }
            FormKind::List(parts) => {
                let proto = current_proto.as_ref().ok_or_else(|| {
                    EvalError::Runtime("reify/defrecord: method impl before protocol name".into())
                })?;
                if parts.is_empty() {
                    continue;
                }
                let method_name = match &parts[0].kind {
                    FormKind::Symbol(s) => Arc::from(s.as_str()),
                    _ => continue,
                };
                let fn_val = build_impl_fn(parts, env)?;
                let mut impls = proto.get().impls.lock().unwrap();
                impls
                    .entry(type_tag.clone())
                    .or_default()
                    .insert(method_name, fn_val);
                drop(impls);
                cljrs_value::bump_protocol_generation();
            }
            _ => {}
        }
    }
    Ok(())
}

// ── helpers ───────────────────────────────────────────────────────────────────

/// Update the root binding of `*ns*` in `clojure.core` to the current namespace.
/// Called whenever `env.current_ns` changes (ns, in-ns, standard_env setup).
pub fn sync_star_ns(env: &mut Env) {
    if let Some(star_ns_var) = env.globals.lookup_var("clojure.core", "*ns*") {
        let ns_ptr = env.globals.get_or_create_ns(&env.current_ns);
        star_ns_var.get().bind(Value::Namespace(ns_ptr));
    }
}

fn require_sym<'a>(args: &'a [Form], idx: usize, form_name: &str) -> EvalResult<&'a str> {
    match args.get(idx).map(|f| &f.kind) {
        Some(FormKind::Symbol(s)) => Ok(s.as_str()),
        _ => Err(EvalError::Runtime(format!(
            "{form_name} requires a symbol at position {idx}"
        ))),
    }
}

// ── with-out-str ──────────────────────────────────────────────────────────────

fn eval_with_out_str(body: &[Form], env: &mut Env) -> EvalResult {
    cljrs_builtins::builtins::push_output_capture();
    let result = eval_body(body, env);
    let captured = cljrs_builtins::builtins::pop_output_capture().unwrap_or_default();
    // Propagate errors but still pop the capture buffer
    result?;
    Ok(Value::string(captured))
}

// ── await ─────────────────────────────────────────────────────────────────────

/// Blocking deref in sync context; yielding deref in async context.
///
/// When `cljrs-async` is loaded, `eval_async` intercepts `await` forms before
/// the sync evaluator reaches this handler, so this path is only taken in
/// non-async (sync) code. It blocks the OS thread until the future/promise
/// resolves — equivalent to `(deref val)`.
fn eval_await(args: &[Form], env: &mut Env) -> EvalResult {
    if args.is_empty() {
        return Err(EvalError::Runtime("await requires one argument".into()));
    }
    let val = eval(&args[0], env)?;
    match val {
        Value::Future(f) => {
            let mut guard = f.get().state.lock().unwrap();
            loop {
                match &*guard {
                    FutureState::Done(v) => {
                        f.get().mark_observed();
                        return Ok(v.clone());
                    }
                    FutureState::Failed(v) => {
                        f.get().mark_observed();
                        return Err(EvalError::Thrown(v.clone()));
                    }
                    FutureState::Cancelled => {
                        return Err(EvalError::Runtime("future was cancelled".into()));
                    }
                    FutureState::Running => {
                        guard = f.get().cond.wait(guard).unwrap();
                    }
                }
            }
        }
        Value::Promise(p) => Ok(p.get().deref_blocking()),
        other => Ok(other),
    }
}