aether-azathoth 0.5.3

A lightweight, embeddable domain-specific language (DSL) interpreter with rich standard library
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
// src/evaluator.rs
//! Evaluator for executing Aether AST

use crate::ast::{BinOp, Expr, Program, Stmt, UnaryOp};
use crate::builtins::BuiltInRegistry;
use crate::environment::Environment;
use crate::module_system::{
    DisabledModuleResolver, ModuleContext, ModuleResolveError, ModuleResolver, ResolvedModule,
};
use crate::value::{GeneratorState, Value};
use serde_json::{Value as JsonValue, json};
use std::cell::RefCell;
use std::collections::HashMap;
use std::collections::VecDeque;
use std::rc::Rc;

#[derive(Debug, Clone, PartialEq)]
pub struct CallFrame {
    pub name: String,
    pub signature: String,
}

#[derive(Debug, Clone, PartialEq)]
pub enum ImportErrorKind {
    ImportDisabled,
    InvalidSpecifier,
    NoBaseDir,
    NotFound,
    AccessDenied,
    IoError,
    NotExported,
    CircularImport,
    ParseFailed,
}

#[derive(Debug, Clone, PartialEq)]
pub struct ImportError {
    pub kind: ImportErrorKind,
    pub specifier: String,
    pub detail: Option<String>,
    pub symbol: Option<String>,
    pub module_id: Option<String>,
    pub import_chain: Vec<String>,
    pub cycle: Option<Vec<String>>,
}

impl ImportError {
    fn from_resolve_error(
        specifier: &str,
        err: ModuleResolveError,
        import_chain: Vec<String>,
    ) -> Self {
        match err {
            ModuleResolveError::ImportDisabled => ImportError {
                kind: ImportErrorKind::ImportDisabled,
                specifier: specifier.to_string(),
                detail: None,
                symbol: None,
                module_id: None,
                import_chain,
                cycle: None,
            },
            ModuleResolveError::InvalidSpecifier(s) => ImportError {
                kind: ImportErrorKind::InvalidSpecifier,
                specifier: specifier.to_string(),
                detail: Some(s),
                symbol: None,
                module_id: None,
                import_chain,
                cycle: None,
            },
            ModuleResolveError::NoBaseDir(s) => ImportError {
                kind: ImportErrorKind::NoBaseDir,
                specifier: specifier.to_string(),
                detail: Some(s),
                symbol: None,
                module_id: None,
                import_chain,
                cycle: None,
            },
            ModuleResolveError::NotFound(s) => ImportError {
                kind: ImportErrorKind::NotFound,
                specifier: specifier.to_string(),
                detail: Some(s),
                symbol: None,
                module_id: None,
                import_chain,
                cycle: None,
            },
            ModuleResolveError::AccessDenied(s) => ImportError {
                kind: ImportErrorKind::AccessDenied,
                specifier: specifier.to_string(),
                detail: Some(s),
                symbol: None,
                module_id: None,
                import_chain,
                cycle: None,
            },
            ModuleResolveError::IoError(s) => ImportError {
                kind: ImportErrorKind::IoError,
                specifier: specifier.to_string(),
                detail: Some(s),
                symbol: None,
                module_id: None,
                import_chain,
                cycle: None,
            },
        }
    }

    fn not_exported(specifier: &str, symbol: &str, import_chain: Vec<String>) -> Self {
        ImportError {
            kind: ImportErrorKind::NotExported,
            specifier: specifier.to_string(),
            detail: None,
            symbol: Some(symbol.to_string()),
            module_id: None,
            import_chain,
            cycle: None,
        }
    }

    fn circular(module_id: &str, cycle: Vec<String>, import_chain: Vec<String>) -> Self {
        ImportError {
            kind: ImportErrorKind::CircularImport,
            specifier: module_id.to_string(),
            detail: None,
            symbol: None,
            module_id: Some(module_id.to_string()),
            import_chain,
            cycle: Some(cycle),
        }
    }

    fn parse_failed(module_id: &str, detail: String, import_chain: Vec<String>) -> Self {
        ImportError {
            kind: ImportErrorKind::ParseFailed,
            specifier: module_id.to_string(),
            detail: Some(detail),
            symbol: None,
            module_id: Some(module_id.to_string()),
            import_chain,
            cycle: None,
        }
    }
}

/// Runtime errors
#[derive(Debug, Clone, PartialEq)]
pub enum RuntimeError {
    /// Variable not found
    UndefinedVariable(String),

    /// Type mismatch - simple message
    TypeError(String),

    /// Type mismatch - detailed version
    TypeErrorDetailed { expected: String, got: String },

    /// Invalid operation
    InvalidOperation(String),

    /// Division by zero
    DivisionByZero,

    /// Function not found or not callable
    NotCallable(String),

    /// Wrong number of arguments
    WrongArity { expected: usize, got: usize },

    /// Return statement (used for control flow)
    Return(Value),

    /// Yield statement (used for generators)
    Yield(Value),

    /// Break statement (used for loop control)
    Break,

    /// Continue statement (used for loop control)
    Continue,

    /// Throw statement (user-thrown error)
    Throw(Value),

    /// Structured Import/Export module errors (with import chain)
    ImportError(Box<ImportError>),

    /// Attach a captured call stack to an error.
    WithCallStack {
        error: Box<RuntimeError>,
        call_stack: Vec<CallFrame>,
    },

    /// Execution limit exceeded
    ExecutionLimit(crate::runtime::ExecutionLimitError),

    /// Custom error message (用于IO操作等)
    CustomError(String),

    /// Debugger pause (not a real error, used for control flow)
    DebugPause,
}

impl std::fmt::Display for RuntimeError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            RuntimeError::UndefinedVariable(name) => write!(f, "Undefined variable: {}", name),
            RuntimeError::TypeError(msg) => write!(f, "Type error: {}", msg),
            RuntimeError::TypeErrorDetailed { expected, got } => {
                write!(f, "Type error: expected {}, got {}", expected, got)
            }
            RuntimeError::InvalidOperation(msg) => write!(f, "Invalid operation: {}", msg),
            RuntimeError::DivisionByZero => write!(f, "Division by zero"),
            RuntimeError::NotCallable(name) => write!(f, "Not callable: {}", name),
            RuntimeError::WrongArity { expected, got } => {
                write!(
                    f,
                    "Wrong number of arguments: expected {}, got {}",
                    expected, got
                )
            }
            RuntimeError::Return(val) => write!(f, "Return: {}", val),
            RuntimeError::Yield(val) => write!(f, "Yield: {}", val),
            RuntimeError::Break => write!(f, "Break outside of loop"),
            RuntimeError::Continue => write!(f, "Continue outside of loop"),
            RuntimeError::Throw(val) => write!(f, "Throw: {}", val),
            RuntimeError::ImportError(e) => {
                let msg = match e.kind {
                    ImportErrorKind::ImportDisabled => "Import is disabled".to_string(),
                    ImportErrorKind::InvalidSpecifier => format!(
                        "Invalid module specifier: {}",
                        e.detail.clone().unwrap_or_else(|| e.specifier.clone())
                    ),
                    ImportErrorKind::NoBaseDir => format!(
                        "No base directory to resolve specifier: {}",
                        e.detail.clone().unwrap_or_else(|| e.specifier.clone())
                    ),
                    ImportErrorKind::NotFound => format!(
                        "Module not found: {}",
                        e.detail.clone().unwrap_or_else(|| e.specifier.clone())
                    ),
                    ImportErrorKind::AccessDenied => format!(
                        "Module access denied: {}",
                        e.detail.clone().unwrap_or_else(|| e.specifier.clone())
                    ),
                    ImportErrorKind::IoError => format!(
                        "Module IO error: {}",
                        e.detail.clone().unwrap_or_else(|| e.specifier.clone())
                    ),
                    ImportErrorKind::NotExported => format!(
                        "'{}' is not exported by module {}",
                        e.symbol.clone().unwrap_or_else(|| "<unknown>".to_string()),
                        e.specifier
                    ),
                    ImportErrorKind::CircularImport => {
                        let cycle = e.cycle.clone().unwrap_or_else(|| vec![e.specifier.clone()]);
                        format!("circular import detected: {}", cycle.join(" -> "))
                    }
                    ImportErrorKind::ParseFailed => format!(
                        "parse failed for module {}: {}",
                        e.module_id.clone().unwrap_or_else(|| e.specifier.clone()),
                        e.detail.clone().unwrap_or_else(|| "<unknown>".to_string())
                    ),
                };

                write!(f, "Import error: {}", msg)?;
                if !e.import_chain.is_empty() {
                    write!(f, "\nImport chain: {}", e.import_chain.join(" -> "))?;
                }
                Ok(())
            }
            RuntimeError::WithCallStack { error, call_stack } => {
                write!(f, "{}", error)?;
                if !call_stack.is_empty() {
                    let frames = call_stack
                        .iter()
                        .map(|fr| fr.signature.as_str())
                        .collect::<Vec<_>>()
                        .join(" -> ");
                    write!(f, "\nCall stack: {}", frames)?;
                }
                Ok(())
            }
            RuntimeError::CustomError(msg) => write!(f, "{}", msg),
            RuntimeError::ExecutionLimit(e) => write!(f, "{}", e),
            RuntimeError::DebugPause => write!(f, "Debugger pause"),
        }
    }
}

impl std::error::Error for RuntimeError {}

pub type EvalResult = Result<Value, RuntimeError>;

/// A structured, machine-readable error report.
///
/// This is intended for CLI/host integrations that need stable fields
/// (instead of parsing human-readable error strings).
#[derive(Debug, Clone, PartialEq)]
pub struct ErrorReport {
    pub phase: String,
    pub kind: String,
    pub message: String,
    pub import_chain: Vec<String>,
    pub call_stack: Vec<CallFrame>,
}

impl ErrorReport {
    pub fn io_error(message: impl Into<String>) -> Self {
        ErrorReport {
            phase: "io".to_string(),
            kind: "IoError".to_string(),
            message: message.into(),
            import_chain: Vec::new(),
            call_stack: Vec::new(),
        }
    }

    pub fn parse_error(message: impl Into<String>) -> Self {
        ErrorReport {
            phase: "parse".to_string(),
            kind: "ParseError".to_string(),
            message: message.into(),
            import_chain: Vec::new(),
            call_stack: Vec::new(),
        }
    }

    pub fn to_json_value(&self) -> JsonValue {
        let call_stack = self
            .call_stack
            .iter()
            .map(|fr| json!({"name": fr.name, "signature": fr.signature}))
            .collect::<Vec<_>>();

        json!({
            "phase": self.phase,
            "kind": self.kind,
            "message": self.message,
            "import_chain": self.import_chain,
            "call_stack": call_stack,
        })
    }

    pub fn to_json_pretty(&self) -> String {
        serde_json::to_string_pretty(&self.to_json_value()).unwrap_or_else(|_| {
            "{\n  \"error\": \"failed to serialize ErrorReport\"\n}".to_string()
        })
    }
}

impl RuntimeError {
    fn peel_call_stack(&self) -> (&RuntimeError, Vec<CallFrame>) {
        let mut current = self;
        let mut frames: Vec<CallFrame> = Vec::new();

        while let RuntimeError::WithCallStack { error, call_stack } = current {
            if frames.is_empty() {
                frames = call_stack.clone();
            }
            current = error.as_ref();
        }

        (current, frames)
    }

    fn kind_name(&self) -> String {
        match self {
            RuntimeError::UndefinedVariable(_) => "UndefinedVariable",
            RuntimeError::TypeError(_) | RuntimeError::TypeErrorDetailed { .. } => "TypeError",
            RuntimeError::InvalidOperation(_) => "InvalidOperation",
            RuntimeError::DivisionByZero => "DivisionByZero",
            RuntimeError::NotCallable(_) => "NotCallable",
            RuntimeError::WrongArity { .. } => "WrongArity",
            RuntimeError::Return(_) => "Return",
            RuntimeError::Yield(_) => "Yield",
            RuntimeError::Break => "Break",
            RuntimeError::Continue => "Continue",
            RuntimeError::Throw(_) => "Throw",
            RuntimeError::ImportError(e) => match e.kind {
                ImportErrorKind::ImportDisabled => "ImportDisabled",
                ImportErrorKind::InvalidSpecifier => "InvalidSpecifier",
                ImportErrorKind::NoBaseDir => "NoBaseDir",
                ImportErrorKind::NotFound => "NotFound",
                ImportErrorKind::AccessDenied => "AccessDenied",
                ImportErrorKind::IoError => "IoError",
                ImportErrorKind::NotExported => "NotExported",
                ImportErrorKind::CircularImport => "CircularImport",
                ImportErrorKind::ParseFailed => "ParseFailed",
            },
            RuntimeError::WithCallStack { .. } => "WithCallStack",
            RuntimeError::ExecutionLimit(_) => "ExecutionLimit",
            RuntimeError::CustomError(_) => "CustomError",
            RuntimeError::DebugPause => "DebugPause",
        }
        .to_string()
    }

    fn base_message(&self) -> String {
        match self {
            RuntimeError::WithCallStack { error, .. } => error.base_message(),
            RuntimeError::ImportError(e) => {
                let msg = match e.kind {
                    ImportErrorKind::ImportDisabled => "Import is disabled".to_string(),
                    ImportErrorKind::InvalidSpecifier => format!(
                        "Invalid module specifier: {}",
                        e.detail.clone().unwrap_or_else(|| e.specifier.clone())
                    ),
                    ImportErrorKind::NoBaseDir => format!(
                        "No base directory to resolve specifier: {}",
                        e.detail.clone().unwrap_or_else(|| e.specifier.clone())
                    ),
                    ImportErrorKind::NotFound => format!(
                        "Module not found: {}",
                        e.detail.clone().unwrap_or_else(|| e.specifier.clone())
                    ),
                    ImportErrorKind::AccessDenied => format!(
                        "Module access denied: {}",
                        e.detail.clone().unwrap_or_else(|| e.specifier.clone())
                    ),
                    ImportErrorKind::IoError => format!(
                        "Module IO error: {}",
                        e.detail.clone().unwrap_or_else(|| e.specifier.clone())
                    ),
                    ImportErrorKind::NotExported => format!(
                        "'{}' is not exported by module {}",
                        e.symbol.clone().unwrap_or_else(|| "<unknown>".to_string()),
                        e.specifier
                    ),
                    ImportErrorKind::CircularImport => {
                        let cycle = e.cycle.clone().unwrap_or_else(|| vec![e.specifier.clone()]);
                        format!("circular import detected: {}", cycle.join(" -> "))
                    }
                    ImportErrorKind::ParseFailed => format!(
                        "parse failed for module {}: {}",
                        e.module_id.clone().unwrap_or_else(|| e.specifier.clone()),
                        e.detail.clone().unwrap_or_else(|| "<unknown>".to_string())
                    ),
                };

                format!("Import error: {msg}")
            }
            other => other.to_string(),
        }
    }

    pub fn to_error_report(&self) -> ErrorReport {
        let (base, call_stack) = self.peel_call_stack();

        let import_chain = match base {
            RuntimeError::ImportError(e) => e.import_chain.clone(),
            _ => Vec::new(),
        };

        ErrorReport {
            phase: "runtime".to_string(),
            kind: base.kind_name(),
            message: base.base_message(),
            import_chain,
            call_stack,
        }
    }
}

/// Evaluator for Aether programs
pub struct Evaluator {
    /// Global environment
    env: Rc<RefCell<Environment>>,
    /// Built-in function registry
    registry: BuiltInRegistry,
    /// In-memory trace buffer (for DSL-safe debugging; no stdout/files/network)
    trace: VecDeque<String>,
    /// Monotonic sequence for trace entries (starts at 1)
    trace_seq: u64,
    /// Structured trace entries (new in Stage 3.2)
    trace_entries: VecDeque<crate::runtime::TraceEntry>,
    /// Maximum number of trace entries to keep in buffer
    trace_buffer_size: usize,

    /// Module resolver (Import/Export). Defaults to disabled for DSL safety.
    module_resolver: Box<dyn ModuleResolver>,
    /// Module export cache: module_id -> exports
    module_cache: HashMap<String, HashMap<String, Value>>,
    /// Module load stack for cycle detection
    module_stack: Vec<String>,
    /// Current module export table stack (only when evaluating an imported module)
    export_stack: Vec<HashMap<String, Value>>,
    /// Optional base directory context for resolving relative imports (e.g. eval_file)
    import_base_stack: Vec<ModuleContext>,

    /// Call stack for better debugging (user functions + builtins)
    call_stack: Vec<CallFrame>,

    /// Execution limits configuration
    limits: crate::runtime::ExecutionLimits,
    /// Current source file being executed (for debugger)
    current_source_file: Option<String>,
    /// Current line number being executed (for debugger)
    current_line: std::cell::Cell<usize>,
    /// Step counter (for step limit enforcement)
    step_counter: std::cell::Cell<usize>,
    /// Call stack depth counter (for recursion depth limit enforcement)
    call_stack_depth: std::cell::Cell<usize>,
    /// Execution start time (for timeout enforcement)
    start_time: std::cell::Cell<Option<std::time::Instant>>,
}

impl Evaluator {
    /// Default maximum number of trace entries to keep in buffer
    const DEFAULT_TRACE_BUFFER_SIZE: usize = 1024;

    fn register_builtins_into_env(registry: &BuiltInRegistry, env: &mut Environment) {
        for name in registry.names() {
            let arity = registry.get(&name).map(|(_, a)| a).unwrap_or(0);
            env.set(name.clone(), Value::BuiltIn { name, arity });
        }
    }

    /// Check and increment step counter
    fn eval_step(&self) -> Result<(), RuntimeError> {
        let steps = self.step_counter.get();

        if let Some(limit) = self.limits.max_steps
            && steps >= limit
        {
            return Err(RuntimeError::ExecutionLimit(
                crate::runtime::ExecutionLimitError::StepLimitExceeded { steps, limit },
            ));
        }

        self.step_counter.set(steps + 1);
        Ok(())
    }

    /// Reset execution step counter (host-facing).
    ///
    /// This is intended to be called at the start of a *top-level* evaluation.
    pub fn reset_step_counter(&mut self) {
        self.step_counter.set(0);
    }

    /// Return the current execution step count.
    ///
    /// Note: this counts evaluated statements (one increment per statement).
    pub fn step_count(&self) -> usize {
        self.step_counter.get()
    }

    /// Set the current source file (for debugger)
    pub fn set_source_file(&mut self, file: String) {
        self.current_source_file = Some(file);
    }

    /// Get the current source file (for debugger)
    pub fn get_source_file(&self) -> Option<&str> {
        self.current_source_file.as_deref()
    }

    /// Set the current line number (for debugger)
    pub fn set_current_line(&self, line: usize) {
        self.current_line.set(line);
    }

    /// Get the current line number (for debugger)
    pub fn get_current_line(&self) -> usize {
        self.current_line.get()
    }

    /// Get the call stack (for debugger)
    pub fn get_call_stack(&self) -> &[CallFrame] {
        &self.call_stack
    }

    /// Get call stack depth (for debugger)
    pub fn get_call_stack_depth(&self) -> usize {
        self.call_stack_depth.get()
    }

    /// Check execution timeout
    fn check_timeout(&self) -> Result<(), RuntimeError> {
        if let Some(limit_ms) = self.limits.max_duration_ms
            && let Some(start) = self.start_time.get()
        {
            let elapsed = start.elapsed().as_millis() as u64;
            if elapsed >= limit_ms {
                return Err(RuntimeError::ExecutionLimit(
                    crate::runtime::ExecutionLimitError::DurationExceeded {
                        duration_ms: elapsed,
                        limit: limit_ms,
                    },
                ));
            }
        }
        Ok(())
    }

    /// Enter function call (check recursion depth)
    fn enter_call(&self) -> Result<(), RuntimeError> {
        if let Some(limit) = self.limits.max_recursion_depth {
            let depth = self.call_stack_depth.get();
            if depth >= limit {
                return Err(RuntimeError::ExecutionLimit(
                    crate::runtime::ExecutionLimitError::RecursionDepthExceeded { depth, limit },
                ));
            }
            self.call_stack_depth.set(depth + 1);
        }
        Ok(())
    }

    /// Exit function call (decrement recursion depth)
    fn exit_call(&self) {
        if self.limits.max_recursion_depth.is_some() {
            let depth = self.call_stack_depth.get();
            self.call_stack_depth.set(depth.saturating_sub(1));
        }
    }

    /// Set execution limits (public API)
    pub fn set_limits(&mut self, limits: crate::runtime::ExecutionLimits) {
        self.limits = limits;
    }

    /// Get execution limits (public API)
    pub fn limits(&self) -> &crate::runtime::ExecutionLimits {
        &self.limits
    }

    fn is_control_flow_error(err: &RuntimeError) -> bool {
        matches!(
            err,
            RuntimeError::Return(_)
                | RuntimeError::Yield(_)
                | RuntimeError::Break
                | RuntimeError::Continue
                | RuntimeError::DebugPause
        )
    }

    fn attach_call_stack_if_absent(&self, err: RuntimeError) -> RuntimeError {
        if Self::is_control_flow_error(&err) {
            return err;
        }
        match err {
            RuntimeError::WithCallStack { .. } => err,
            other => RuntimeError::WithCallStack {
                error: Box::new(other),
                call_stack: self.call_stack.clone(),
            },
        }
    }

    /// Create a new evaluator (默认禁用IO)
    pub fn new() -> Self {
        Self::with_permissions_and_trace_buffer(
            crate::builtins::IOPermissions::default(),
            Self::DEFAULT_TRACE_BUFFER_SIZE,
        )
    }

    /// Create a new evaluator with custom IO permissions
    pub fn with_permissions(permissions: crate::builtins::IOPermissions) -> Self {
        Self::with_permissions_and_trace_buffer(permissions, Self::DEFAULT_TRACE_BUFFER_SIZE)
    }

    /// Create a new evaluator with custom IO permissions and trace buffer size
    pub fn with_permissions_and_trace_buffer(
        permissions: crate::builtins::IOPermissions,
        trace_buffer_size: usize,
    ) -> Self {
        let env = Rc::new(RefCell::new(Environment::new()));

        // Register built-in functions with permissions
        let registry = BuiltInRegistry::with_permissions(permissions);
        Self::register_builtins_into_env(&registry, &mut env.borrow_mut());

        Evaluator {
            env,
            registry,
            trace: VecDeque::new(),
            trace_seq: 0,
            trace_entries: VecDeque::new(),
            trace_buffer_size,

            module_resolver: Box::new(DisabledModuleResolver),
            module_cache: HashMap::new(),
            module_stack: Vec::new(),
            export_stack: Vec::new(),
            import_base_stack: Vec::new(),

            call_stack: Vec::new(),

            limits: crate::runtime::ExecutionLimits::default(),
            current_source_file: None,
            current_line: std::cell::Cell::new(0),
            step_counter: std::cell::Cell::new(0),
            call_stack_depth: std::cell::Cell::new(0),
            start_time: std::cell::Cell::new(None),
        }
    }

    /// Create evaluator with custom environment
    pub fn with_env(env: Rc<RefCell<Environment>>) -> Self {
        let registry = BuiltInRegistry::new();
        Evaluator {
            env,
            registry,
            trace: VecDeque::new(),
            trace_seq: 0,
            trace_entries: VecDeque::new(),
            trace_buffer_size: Self::DEFAULT_TRACE_BUFFER_SIZE,

            module_resolver: Box::new(DisabledModuleResolver),
            module_cache: HashMap::new(),
            module_stack: Vec::new(),
            export_stack: Vec::new(),
            import_base_stack: Vec::new(),

            call_stack: Vec::new(),

            limits: crate::runtime::ExecutionLimits::default(),
            current_source_file: None,
            current_line: std::cell::Cell::new(0),
            step_counter: std::cell::Cell::new(0),
            call_stack_depth: std::cell::Cell::new(0),
            start_time: std::cell::Cell::new(None),
        }
    }

    /// Clear the call stack (used by top-level entry points like `Aether::eval`).
    pub fn clear_call_stack(&mut self) {
        self.call_stack.clear();
    }

    /// Configure the module resolver used for `Import/Export`.
    pub fn set_module_resolver(&mut self, resolver: Box<dyn ModuleResolver>) {
        self.module_resolver = resolver;
    }

    /// Push a base directory context for resolving relative imports.
    ///
    /// This is typically used by CLI `eval_file()` wrappers.
    pub fn push_import_base(&mut self, module_id: String, base_dir: Option<std::path::PathBuf>) {
        self.import_base_stack.push(ModuleContext {
            module_id,
            base_dir,
        });
    }

    /// Pop the most recent base directory context.
    pub fn pop_import_base(&mut self) {
        self.import_base_stack.pop();
    }

    /// Append a trace entry (host-readable; no IO side effects).
    pub fn trace_push(&mut self, msg: String) {
        self.trace_seq = self.trace_seq.saturating_add(1);
        let entry = format!("#{} {}", self.trace_seq, msg);

        if self.trace.len() >= self.trace_buffer_size {
            self.trace.pop_front();
        }
        self.trace.push_back(entry);
    }

    /// Push a structured trace entry (Stage 3.2)
    fn trace_push_entry(&mut self, entry: crate::runtime::TraceEntry) {
        self.trace_seq = self.trace_seq.saturating_add(1);

        // Add to structured entries
        if self.trace_entries.len() >= self.trace_buffer_size {
            self.trace_entries.pop_front();
        }
        self.trace_entries.push_back(entry.clone());

        // Also add to formatted trace (for backward compatibility)
        let formatted = entry.format();
        let msg = format!("#{} {}", self.trace_seq, formatted);
        if self.trace.len() >= self.trace_buffer_size {
            self.trace.pop_front();
        }
        self.trace.push_back(msg);
    }

    /// Drain the trace buffer.
    pub fn take_trace(&mut self) -> Vec<String> {
        std::mem::take(&mut self.trace).into_iter().collect()
    }

    /// Get all structured trace entries
    pub fn trace_records(&self) -> Vec<crate::runtime::TraceEntry> {
        self.trace_entries.iter().cloned().collect()
    }

    /// Filter trace entries by level
    pub fn trace_by_level(
        &self,
        level: crate::runtime::TraceLevel,
    ) -> Vec<crate::runtime::TraceEntry> {
        self.trace_entries
            .iter()
            .filter(|e| e.level == level)
            .cloned()
            .collect()
    }

    /// Filter trace entries by category
    pub fn trace_by_category(&self, category: &str) -> Vec<crate::runtime::TraceEntry> {
        self.trace_entries
            .iter()
            .filter(|e| e.category == category)
            .cloned()
            .collect()
    }

    /// Filter trace entries by label
    pub fn trace_by_label(&self, label: &str) -> Vec<crate::runtime::TraceEntry> {
        self.trace_entries
            .iter()
            .filter(|e| e.label.as_deref() == Some(label))
            .cloned()
            .collect()
    }

    /// Filter trace entries by time range (since)
    pub fn trace_since(&self, since: std::time::Instant) -> Vec<crate::runtime::TraceEntry> {
        self.trace_entries
            .iter()
            .filter(|e| e.timestamp >= since)
            .cloned()
            .collect()
    }

    /// Apply complex filter to trace entries
    pub fn trace_filter(
        &self,
        filter: &crate::runtime::TraceFilter,
    ) -> Vec<crate::runtime::TraceEntry> {
        self.trace_entries
            .iter()
            .filter(|e| filter.matches(e))
            .cloned()
            .collect()
    }

    /// Get trace statistics
    pub fn trace_stats(&self) -> crate::runtime::TraceStats {
        use std::collections::HashMap;

        let mut by_level = HashMap::new();
        let mut by_category = HashMap::new();

        for entry in &self.trace_entries {
            *by_level.entry(entry.level).or_insert(0) += 1;
            *by_category.entry(entry.category.clone()).or_insert(0) += 1;
        }

        crate::runtime::TraceStats {
            total_entries: self.trace_entries.len(),
            by_level,
            by_category,
            buffer_size: self.trace_buffer_size,
            buffer_full: self.trace_entries.len() >= self.trace_buffer_size,
        }
    }

    /// Clear the trace buffer.
    pub fn clear_trace(&mut self) {
        self.trace.clear();
        self.trace_entries.clear();
        self.trace_seq = 0;
    }

    /// Set the maximum number of trace entries to keep in buffer
    ///
    /// If the new size is smaller than the current number of entries,
    /// excess entries will be removed from the front of the buffer.
    pub fn set_trace_buffer_size(&mut self, size: usize) {
        self.trace_buffer_size = size;

        // Trim existing buffers if necessary
        while self.trace.len() > size {
            self.trace.pop_front();
        }
        while self.trace_entries.len() > size {
            self.trace_entries.pop_front();
        }
    }

    /// Reset the environment (clear all variables and re-register built-ins)
    ///
    /// This is useful for engine pooling and global singleton patterns
    /// where you want to reuse an engine instance but ensure isolation.
    pub fn reset_env(&mut self) {
        // Create new environment
        self.env = Rc::new(RefCell::new(Environment::new()));

        // Avoid leaking trace across pooled executions
        self.trace.clear();
        self.trace_entries.clear();
        self.trace_seq = 0;

        // Reset module contexts (cache is kept; can be cleared explicitly by host if needed)
        self.import_base_stack.clear();
        self.export_stack.clear();
        self.module_stack.clear();

        // Avoid leaking call stack across pooled executions
        self.call_stack.clear();

        // Re-register built-in functions
        Self::register_builtins_into_env(&self.registry, &mut self.env.borrow_mut());
    }

    /// Set a global variable from the host (without requiring `eval`).
    pub fn set_global(&mut self, name: impl Into<String>, value: Value) {
        self.env.borrow_mut().set(name.into(), value);
    }

    /// Get a global variable value from the environment
    pub fn get_global(&self, name: &str) -> Option<Value> {
        self.env.borrow().get(name)
    }

    /// Enter a child scope (new environment whose parent is the current env).
    ///
    /// Returns the previous environment handle; pass it back to `restore_env()`.
    pub fn enter_child_scope(&mut self) -> Rc<RefCell<Environment>> {
        let prev = Rc::clone(&self.env);
        let child = Rc::new(RefCell::new(Environment::with_parent(Rc::clone(&prev))));
        self.env = child;
        prev
    }

    /// Restore a previously saved environment handle (typically from `enter_child_scope()`).
    pub fn restore_env(&mut self, prev: Rc<RefCell<Environment>>) {
        self.env = prev;
    }

    /// Evaluate a program
    pub fn eval_program(&mut self, program: &Program) -> EvalResult {
        // Record start time for timeout checking
        if self.limits.max_duration_ms.is_some() {
            self.start_time.set(Some(std::time::Instant::now()));
        }

        let mut result = Value::Null;

        for stmt in program {
            result = self.eval_statement(stmt)?;
        }

        Ok(result)
    }

    /// Evaluate a statement
    pub fn eval_statement(&mut self, stmt: &Stmt) -> EvalResult {
        // Check execution limits before each statement
        self.eval_step()?;
        self.check_timeout()?;

        match stmt {
            Stmt::Set { name, value } => {
                let val = self.eval_expression(value)?;
                self.env.borrow_mut().set(name.clone(), val.clone());
                Ok(val)
            }

            Stmt::SetIndex {
                object,
                index,
                value,
            } => {
                // Evaluate the value to be assigned
                let val = self.eval_expression(value)?;

                // For simple identifier objects, we can modify in place
                if let Expr::Identifier(name) = object.as_ref() {
                    // Get the object from environment
                    let obj = self
                        .env
                        .borrow()
                        .get(name)
                        .ok_or_else(|| RuntimeError::UndefinedVariable(name.clone()))?;

                    // Evaluate the index
                    let idx_val = self.eval_expression(index)?;

                    // Modify based on object type
                    let new_obj = match (obj, idx_val) {
                        (Value::Array(mut arr), Value::Number(n)) => {
                            let idx = n as usize;
                            if idx >= arr.len() {
                                return Err(RuntimeError::InvalidOperation(format!(
                                    "Index {} out of bounds (array length: {})",
                                    idx,
                                    arr.len()
                                )));
                            }
                            arr[idx] = val.clone();
                            Value::Array(arr)
                        }
                        (Value::Dict(mut dict), Value::String(key)) => {
                            dict.insert(key, val.clone());
                            Value::Dict(dict)
                        }
                        (obj, idx) => {
                            return Err(RuntimeError::TypeError(format!(
                                "Cannot index {} with {}",
                                obj.type_name(),
                                idx.type_name()
                            )));
                        }
                    };

                    // Update the variable in environment
                    self.env.borrow_mut().set(name.clone(), new_obj);
                    Ok(val)
                } else {
                    // For complex expressions, we can't modify in place
                    Err(RuntimeError::InvalidOperation(
                        "Can only assign to simple variable indices (e.g., dict[key], not expr[key])"
                            .to_string(),
                    ))
                }
            }

            Stmt::FuncDef { name, params, body } => {
                let func = Value::Function {
                    name: Some(name.clone()),
                    params: params.clone(),
                    body: body.clone(),
                    env: Rc::clone(&self.env),
                };
                self.env.borrow_mut().set(name.clone(), func.clone());
                Ok(func)
            }

            Stmt::GeneratorDef { name, params, body } => {
                let r#gen = Value::Generator {
                    params: params.clone(),
                    body: body.clone(),
                    env: Rc::clone(&self.env),
                    state: GeneratorState::NotStarted,
                };
                self.env.borrow_mut().set(name.clone(), r#gen.clone());
                Ok(r#gen)
            }

            Stmt::LazyDef { name, expr } => {
                let lazy = Value::Lazy {
                    expr: expr.clone(),
                    env: Rc::clone(&self.env),
                    cached: None,
                };
                self.env.borrow_mut().set(name.clone(), lazy.clone());
                Ok(lazy)
            }

            Stmt::Return(expr) => {
                let val = self.eval_expression(expr)?;
                Err(RuntimeError::Return(val))
            }

            Stmt::Yield(expr) => {
                let val = self.eval_expression(expr)?;
                Err(RuntimeError::Yield(val))
            }

            Stmt::Break => Err(RuntimeError::Break),

            Stmt::Continue => Err(RuntimeError::Continue),

            Stmt::While { condition, body } => {
                let mut result = Value::Null;

                loop {
                    let cond = self.eval_expression(condition)?;
                    if !cond.is_truthy() {
                        break;
                    }

                    let mut should_break = false;
                    for stmt in body {
                        match self.eval_statement(stmt) {
                            Ok(val) => result = val,
                            Err(RuntimeError::Break) => {
                                should_break = true;
                                break;
                            }
                            Err(RuntimeError::Continue) => break,
                            Err(e) => return Err(e),
                        }
                    }

                    if should_break {
                        break;
                    }
                }

                Ok(result)
            }

            Stmt::For {
                var,
                iterable,
                body,
            } => {
                let iter_val = self.eval_expression(iterable)?;
                let mut result = Value::Null;

                match iter_val {
                    Value::Array(arr) => {
                        let mut should_break = false;
                        for item in arr {
                            self.env.borrow_mut().set(var.clone(), item);
                            for stmt in body {
                                match self.eval_statement(stmt) {
                                    Ok(val) => result = val,
                                    Err(RuntimeError::Break) => {
                                        should_break = true;
                                        break;
                                    }
                                    Err(RuntimeError::Continue) => break,
                                    Err(e) => return Err(e),
                                }
                            }
                            if should_break {
                                break;
                            }
                        }
                    }
                    _ => {
                        return Err(RuntimeError::TypeError(format!(
                            "Cannot iterate over {}",
                            iter_val.type_name()
                        )));
                    }
                }

                Ok(result)
            }

            Stmt::ForIndexed {
                index_var,
                value_var,
                iterable,
                body,
            } => {
                let iter_val = self.eval_expression(iterable)?;
                let mut result = Value::Null;

                match iter_val {
                    Value::Array(arr) => {
                        let mut should_break = false;
                        for (idx, item) in arr.iter().enumerate() {
                            self.env
                                .borrow_mut()
                                .set(index_var.clone(), Value::Number(idx as f64));
                            self.env.borrow_mut().set(value_var.clone(), item.clone());
                            for stmt in body {
                                match self.eval_statement(stmt) {
                                    Ok(val) => result = val,
                                    Err(RuntimeError::Break) => {
                                        should_break = true;
                                        break;
                                    }
                                    Err(RuntimeError::Continue) => break,
                                    Err(e) => return Err(e),
                                }
                            }
                            if should_break {
                                break;
                            }
                        }
                    }
                    _ => {
                        return Err(RuntimeError::TypeError(format!(
                            "Cannot iterate over {}",
                            iter_val.type_name()
                        )));
                    }
                }

                Ok(result)
            }

            Stmt::Switch {
                expr,
                cases,
                default,
            } => {
                let val = self.eval_expression(expr)?;

                for (case_expr, case_body) in cases {
                    let case_val = self.eval_expression(case_expr)?;
                    if val.equals(&case_val) {
                        let mut result = Value::Null;
                        for stmt in case_body {
                            result = self.eval_statement(stmt)?;
                        }
                        return Ok(result);
                    }
                }

                if let Some(default_body) = default {
                    let mut result = Value::Null;
                    for stmt in default_body {
                        result = self.eval_statement(stmt)?;
                    }
                    return Ok(result);
                }

                Ok(Value::Null)
            }

            Stmt::Import {
                names,
                path,
                aliases,
                namespace,
            } => self.eval_import(names, path, aliases, namespace.as_ref()),

            Stmt::Export(name) => self.eval_export(name),

            Stmt::Throw(expr) => {
                let val = self.eval_expression(expr)?;
                Err(RuntimeError::Throw(val))
            }

            Stmt::Expression(expr) => self.eval_expression(expr),
        }
    }

    /// Evaluate an expression
    pub fn eval_expression(&mut self, expr: &Expr) -> EvalResult {
        match expr {
            Expr::Number(n) => Ok(Value::Number(*n)),

            Expr::BigInteger(s) => {
                // 将大整数字符串转换为 Fraction (分母为1的分数)
                use num_bigint::BigInt;
                use num_rational::Ratio;

                match s.parse::<BigInt>() {
                    Ok(big_int) => Ok(Value::Fraction(Ratio::new(big_int, BigInt::from(1)))),
                    Err(_) => Err(RuntimeError::InvalidOperation(format!(
                        "Invalid big integer: {}",
                        s
                    ))),
                }
            }

            Expr::String(s) => Ok(Value::String(s.clone())),

            Expr::Boolean(b) => Ok(Value::Boolean(*b)),

            Expr::Null => Ok(Value::Null),

            Expr::Identifier(name) => self
                .env
                .borrow()
                .get(name)
                .ok_or_else(|| RuntimeError::UndefinedVariable(name.clone())),

            Expr::Binary { left, op, right } => {
                // Short-circuit evaluation for And and Or
                match op {
                    BinOp::And => {
                        let left_val = self.eval_expression(left)?;
                        if !left_val.is_truthy() {
                            // Short-circuit: left is falsy, return left without evaluating right
                            Ok(left_val)
                        } else {
                            // left is truthy, return right value
                            self.eval_expression(right)
                        }
                    }
                    BinOp::Or => {
                        let left_val = self.eval_expression(left)?;
                        if left_val.is_truthy() {
                            // Short-circuit: left is truthy, return left without evaluating right
                            Ok(left_val)
                        } else {
                            // left is falsy, return right value
                            self.eval_expression(right)
                        }
                    }
                    // For other operators, evaluate both sides
                    _ => {
                        let left_val = self.eval_expression(left)?;
                        let right_val = self.eval_expression(right)?;
                        self.eval_binary_op(&left_val, op, &right_val)
                    }
                }
            }

            Expr::Unary { op, expr } => {
                let val = self.eval_expression(expr)?;
                self.eval_unary_op(op, &val)
            }

            Expr::Call { func, args } => {
                let name_hint = match func.as_ref() {
                    Expr::Identifier(name) => Some(name.clone()),
                    _ => None,
                };
                let func_val = self.eval_expression(func)?;
                let arg_vals: Result<Vec<_>, _> =
                    args.iter().map(|arg| self.eval_expression(arg)).collect();
                let arg_vals = arg_vals?;

                self.call_function(name_hint.as_deref(), &func_val, arg_vals)
            }

            Expr::Array(elements) => {
                let vals: Result<Vec<_>, _> =
                    elements.iter().map(|e| self.eval_expression(e)).collect();
                Ok(Value::Array(vals?))
            }

            Expr::Dict(pairs) => {
                let mut map = std::collections::HashMap::new();
                for (key, value_expr) in pairs {
                    let value = self.eval_expression(value_expr)?;
                    map.insert(key.clone(), value);
                }
                Ok(Value::Dict(map))
            }

            Expr::Index { object, index } => {
                let obj_val = self.eval_expression(object)?;
                let idx_val = self.eval_expression(index)?;

                match (obj_val, idx_val) {
                    (Value::Array(arr), Value::Number(n)) => {
                        let idx = n as usize;
                        arr.get(idx).cloned().ok_or_else(|| {
                            RuntimeError::InvalidOperation(format!("Index {} out of bounds", idx))
                        })
                    }
                    (Value::String(s), Value::Number(n)) => {
                        let idx = n as usize;
                        let chars: Vec<char> = s.chars().collect();
                        chars
                            .get(idx)
                            .cloned()
                            .map(|ch| Value::String(ch.to_string()))
                            .ok_or_else(|| {
                                RuntimeError::InvalidOperation(format!(
                                    "Index {} out of bounds (string length: {})",
                                    idx,
                                    chars.len()
                                ))
                            })
                    }
                    (Value::Dict(dict), Value::String(key)) => {
                        dict.get(&key).cloned().ok_or_else(|| {
                            RuntimeError::InvalidOperation(format!("Key '{}' not found", key))
                        })
                    }
                    (obj, idx) => Err(RuntimeError::TypeError(format!(
                        "Cannot index {} with {}",
                        obj.type_name(),
                        idx.type_name()
                    ))),
                }
            }

            Expr::If {
                condition,
                then_branch,
                elif_branches,
                else_branch,
            } => {
                let cond = self.eval_expression(condition)?;

                if cond.is_truthy() {
                    let mut result = Value::Null;
                    for stmt in then_branch {
                        result = self.eval_statement(stmt)?;
                    }
                    return Ok(result);
                }

                for (elif_cond, elif_body) in elif_branches {
                    let cond = self.eval_expression(elif_cond)?;
                    if cond.is_truthy() {
                        let mut result = Value::Null;
                        for stmt in elif_body {
                            result = self.eval_statement(stmt)?;
                        }
                        return Ok(result);
                    }
                }

                if let Some(else_body) = else_branch {
                    let mut result = Value::Null;
                    for stmt in else_body {
                        result = self.eval_statement(stmt)?;
                    }
                    return Ok(result);
                }

                Ok(Value::Null)
            }

            Expr::Lambda { params, body } => {
                // Create a closure by capturing the current environment
                Ok(Value::Function {
                    name: None,
                    params: params.clone(),
                    body: body.clone(),
                    env: Rc::clone(&self.env),
                })
            }
        }
    }

    /// Evaluate binary operation
    fn eval_binary_op(&self, left: &Value, op: &BinOp, right: &Value) -> EvalResult {
        match op {
            BinOp::Add => match (left, right) {
                (Value::Number(a), Value::Number(b)) => Ok(Value::Number(a + b)),
                (Value::String(a), Value::String(b)) => Ok(Value::String(format!("{}{}", a, b))),
                (Value::Fraction(a), Value::Fraction(b)) => Ok(Value::Fraction(a + b)),
                (Value::Number(a), Value::Fraction(b)) | (Value::Fraction(b), Value::Number(a)) => {
                    use num_bigint::BigInt;
                    use num_rational::Ratio;
                    if a.fract() == 0.0 {
                        let a_frac = Ratio::new(BigInt::from(*a as i64), BigInt::from(1));
                        Ok(Value::Fraction(a_frac + b))
                    } else {
                        // 浮点数和分数混合运算,转换为浮点数
                        use num_traits::ToPrimitive;
                        let b_float =
                            b.numer().to_f64().unwrap_or(0.0) / b.denom().to_f64().unwrap_or(1.0);
                        Ok(Value::Number(a + b_float))
                    }
                }
                _ => Err(RuntimeError::TypeError(format!(
                    "Cannot add {} and {}",
                    left.type_name(),
                    right.type_name()
                ))),
            },

            BinOp::Subtract => match (left, right) {
                (Value::Number(a), Value::Number(b)) => Ok(Value::Number(a - b)),
                (Value::Fraction(a), Value::Fraction(b)) => Ok(Value::Fraction(a - b)),
                (Value::Number(a), Value::Fraction(b)) => {
                    use num_bigint::BigInt;
                    use num_rational::Ratio;
                    if a.fract() == 0.0 {
                        let a_frac = Ratio::new(BigInt::from(*a as i64), BigInt::from(1));
                        Ok(Value::Fraction(a_frac - b))
                    } else {
                        use num_traits::ToPrimitive;
                        let b_float =
                            b.numer().to_f64().unwrap_or(0.0) / b.denom().to_f64().unwrap_or(1.0);
                        Ok(Value::Number(a - b_float))
                    }
                }
                (Value::Fraction(a), Value::Number(b)) => {
                    use num_bigint::BigInt;
                    use num_rational::Ratio;
                    if b.fract() == 0.0 {
                        let b_frac = Ratio::new(BigInt::from(*b as i64), BigInt::from(1));
                        Ok(Value::Fraction(a - b_frac))
                    } else {
                        use num_traits::ToPrimitive;
                        let a_float =
                            a.numer().to_f64().unwrap_or(0.0) / a.denom().to_f64().unwrap_or(1.0);
                        Ok(Value::Number(a_float - b))
                    }
                }
                _ => Err(RuntimeError::TypeError(format!(
                    "Cannot subtract {} from {}",
                    right.type_name(),
                    left.type_name()
                ))),
            },

            BinOp::Multiply => match (left, right) {
                (Value::Number(a), Value::Number(b)) => {
                    // 如果两个数都是整数,且足够大,使用精确计算
                    if a.fract() == 0.0 && b.fract() == 0.0 {
                        // 检查是否超过 f64 的安全整数范围 (2^53)
                        let max_safe = 9007199254740992.0; // 2^53
                        if a.abs() > max_safe || b.abs() > max_safe {
                            // 使用 Fraction (BigInt) 进行精确计算
                            use num_bigint::BigInt;
                            use num_rational::Ratio;

                            // 将 f64 转换为字符串再转为 BigInt,避免精度损失
                            let a_str = format!("{:.0}", a);
                            let b_str = format!("{:.0}", b);

                            if let (Ok(a_big), Ok(b_big)) =
                                (a_str.parse::<BigInt>(), b_str.parse::<BigInt>())
                            {
                                let result_big = a_big * b_big;
                                let frac = Ratio::new(result_big, BigInt::from(1));
                                return Ok(Value::Fraction(frac));
                            }
                        }
                    }
                    Ok(Value::Number(a * b))
                }
                (Value::Fraction(a), Value::Fraction(b)) => Ok(Value::Fraction(a * b)),
                (Value::Number(a), Value::Fraction(b)) | (Value::Fraction(b), Value::Number(a)) => {
                    use num_bigint::BigInt;
                    use num_rational::Ratio;
                    if a.fract() == 0.0 {
                        let a_frac = Ratio::new(BigInt::from(*a as i64), BigInt::from(1));
                        Ok(Value::Fraction(a_frac * b))
                    } else {
                        Err(RuntimeError::TypeError(
                            "Cannot multiply non-integer Number with Fraction".to_string(),
                        ))
                    }
                }
                _ => Err(RuntimeError::TypeError(format!(
                    "Cannot multiply {} and {}",
                    left.type_name(),
                    right.type_name()
                ))),
            },

            BinOp::Divide => match (left, right) {
                (Value::Number(a), Value::Number(b)) => {
                    if *b == 0.0 {
                        Err(RuntimeError::DivisionByZero)
                    } else {
                        Ok(Value::Number(a / b))
                    }
                }
                (Value::Fraction(a), Value::Fraction(b)) => {
                    use num_traits::Zero;
                    if b.is_zero() {
                        Err(RuntimeError::DivisionByZero)
                    } else {
                        Ok(Value::Fraction(a / b))
                    }
                }
                (Value::Number(a), Value::Fraction(b)) => {
                    use num_bigint::BigInt;
                    use num_rational::Ratio;
                    use num_traits::Zero;
                    if b.is_zero() {
                        Err(RuntimeError::DivisionByZero)
                    } else if a.fract() == 0.0 {
                        let a_frac = Ratio::new(BigInt::from(*a as i64), BigInt::from(1));
                        Ok(Value::Fraction(a_frac / b))
                    } else {
                        use num_traits::ToPrimitive;
                        let b_float =
                            b.numer().to_f64().unwrap_or(0.0) / b.denom().to_f64().unwrap_or(1.0);
                        Ok(Value::Number(a / b_float))
                    }
                }
                (Value::Fraction(a), Value::Number(b)) => {
                    use num_bigint::BigInt;
                    use num_rational::Ratio;
                    if *b == 0.0 {
                        Err(RuntimeError::DivisionByZero)
                    } else if b.fract() == 0.0 {
                        let b_frac = Ratio::new(BigInt::from(*b as i64), BigInt::from(1));
                        Ok(Value::Fraction(a / b_frac))
                    } else {
                        use num_traits::ToPrimitive;
                        let a_float =
                            a.numer().to_f64().unwrap_or(0.0) / a.denom().to_f64().unwrap_or(1.0);
                        Ok(Value::Number(a_float / b))
                    }
                }
                _ => Err(RuntimeError::TypeError(format!(
                    "Cannot divide {} by {}",
                    left.type_name(),
                    right.type_name()
                ))),
            },

            BinOp::Modulo => match (left, right) {
                (Value::Number(a), Value::Number(b)) => {
                    if *b == 0.0 {
                        Err(RuntimeError::DivisionByZero)
                    } else {
                        Ok(Value::Number(a % b))
                    }
                }
                _ => Err(RuntimeError::TypeError(format!(
                    "Cannot modulo {} by {}",
                    left.type_name(),
                    right.type_name()
                ))),
            },

            BinOp::Equal => Ok(Value::Boolean(left.equals(right))),

            BinOp::NotEqual => Ok(Value::Boolean(!left.equals(right))),

            BinOp::Less => match left.compare(right) {
                Some(ord) => Ok(Value::Boolean(ord == std::cmp::Ordering::Less)),
                None => Err(RuntimeError::TypeError(format!(
                    "Cannot compare {} and {}",
                    left.type_name(),
                    right.type_name()
                ))),
            },

            BinOp::LessEqual => match left.compare(right) {
                Some(ord) => Ok(Value::Boolean(ord != std::cmp::Ordering::Greater)),
                None => Err(RuntimeError::TypeError(format!(
                    "Cannot compare {} and {}",
                    left.type_name(),
                    right.type_name()
                ))),
            },

            BinOp::Greater => match left.compare(right) {
                Some(ord) => Ok(Value::Boolean(ord == std::cmp::Ordering::Greater)),
                None => Err(RuntimeError::TypeError(format!(
                    "Cannot compare {} and {}",
                    left.type_name(),
                    right.type_name()
                ))),
            },

            BinOp::GreaterEqual => match left.compare(right) {
                Some(ord) => Ok(Value::Boolean(ord != std::cmp::Ordering::Less)),
                None => Err(RuntimeError::TypeError(format!(
                    "Cannot compare {} and {}",
                    left.type_name(),
                    right.type_name()
                ))),
            },

            BinOp::And => {
                if !left.is_truthy() {
                    Ok(left.clone())
                } else {
                    Ok(right.clone())
                }
            }

            BinOp::Or => {
                if left.is_truthy() {
                    Ok(left.clone())
                } else {
                    Ok(right.clone())
                }
            }
        }
    }

    /// Evaluate unary operation
    fn eval_unary_op(&self, op: &UnaryOp, val: &Value) -> EvalResult {
        match op {
            UnaryOp::Minus => match val {
                Value::Number(n) => Ok(Value::Number(-n)),
                _ => Err(RuntimeError::TypeError(format!(
                    "Cannot negate {}",
                    val.type_name()
                ))),
            },

            UnaryOp::Not => Ok(Value::Boolean(!val.is_truthy())),
        }
    }

    /// Call a function with arguments
    fn call_function(
        &mut self,
        name_hint: Option<&str>,
        func: &Value,
        args: Vec<Value>,
    ) -> EvalResult {
        // Check recursion depth limit
        self.enter_call()?;

        let frame = match func {
            Value::Function { name, params, .. } => {
                let display_name = name_hint
                    .map(|s| s.to_string())
                    .or_else(|| name.clone())
                    .unwrap_or_else(|| "<lambda>".to_string());
                let signature = format!("{}({})", display_name, params.join(", "));
                CallFrame {
                    name: display_name.clone(),
                    signature,
                }
            }
            Value::BuiltIn { name, .. } => {
                let arity = self.registry.get(name).map(|(_, a)| a).unwrap_or(0);
                let params = if arity == 0 {
                    String::new()
                } else {
                    (1..=arity)
                        .map(|i| format!("arg{}", i))
                        .collect::<Vec<_>>()
                        .join(", ")
                };
                let signature = format!("{}({})", name, params);
                CallFrame {
                    name: name.clone(),
                    signature,
                }
            }
            other => {
                let name = name_hint.unwrap_or("<call>").to_string();
                let signature = format!("{}(<{}>)", name, other.type_name());
                CallFrame { name, signature }
            }
        };

        self.call_stack.push(frame);

        match func {
            Value::Function {
                params, body, env, ..
            } => {
                if params.len() != args.len() {
                    let err = RuntimeError::WrongArity {
                        expected: params.len(),
                        got: args.len(),
                    };
                    let err = self.attach_call_stack_if_absent(err);
                    let _ = self.call_stack.pop();
                    self.exit_call();
                    return Err(err);
                }

                // Create new environment for function execution
                let func_env = Rc::new(RefCell::new(Environment::with_parent(Rc::clone(env))));

                // Bind parameters
                for (param, arg) in params.iter().zip(args.iter()) {
                    func_env.borrow_mut().set(param.clone(), arg.clone());
                }

                // Execute function body
                let prev_env = Rc::clone(&self.env);
                self.env = func_env;

                let mut result = Value::Null;
                for stmt in body {
                    match self.eval_statement(stmt) {
                        Ok(val) => result = val,
                        Err(RuntimeError::Return(val)) => {
                            result = val;
                            break;
                        }
                        Err(e) => {
                            self.env = prev_env;
                            let e = self.attach_call_stack_if_absent(e);
                            let _ = self.call_stack.pop();
                            self.exit_call();
                            return Err(e);
                        }
                    }
                }

                self.env = prev_env;
                let _ = self.call_stack.pop();
                self.exit_call();
                Ok(result)
            }

            Value::BuiltIn { name, .. } => {
                // Special handling for TRACE functions
                let res = match name.as_str() {
                    "TRACE" => {
                        if args.is_empty() {
                            return {
                                let err = RuntimeError::WrongArity {
                                    expected: 1,
                                    got: 0,
                                };
                                let err = self.attach_call_stack_if_absent(err);
                                let _ = self.call_stack.pop();
                                self.exit_call();
                                Err(err)
                            };
                        }

                        // Optional label: TRACE("label", x, y)
                        // If only one argument is provided, treat it as the payload (backward compatible).
                        let (label, payload_args) = if args.len() >= 2 {
                            match &args[0] {
                                Value::String(s) => (Some(s.as_str()), &args[1..]),
                                _ => (None, args.as_slice()),
                            }
                        } else {
                            (None, args.as_slice())
                        };

                        let payload = payload_args
                            .iter()
                            .map(|v| v.to_string())
                            .collect::<Vec<_>>()
                            .join(" ");

                        let msg = match label {
                            Some(l) => format!("[{}] {}", l, payload),
                            None => payload,
                        };

                        self.trace_push(msg);
                        Ok(Value::Null)
                    }
                    "TRACE_DEBUG" | "TRACE_INFO" | "TRACE_WARN" | "TRACE_ERROR" => {
                        // Structured TRACE functions (Stage 3.2)
                        // Usage: TRACE_DEBUG("category", value1, value2, ...)
                        if args.len() < 2 {
                            return {
                                let err = RuntimeError::WrongArity {
                                    expected: 2,
                                    got: args.len(),
                                };
                                let err = self.attach_call_stack_if_absent(err);
                                let _ = self.call_stack.pop();
                                self.exit_call();
                                Err(err)
                            };
                        }

                        // Parse level from function name
                        let level = match name.as_str() {
                            "TRACE_DEBUG" => crate::runtime::TraceLevel::Debug,
                            "TRACE_INFO" => crate::runtime::TraceLevel::Info,
                            "TRACE_WARN" => crate::runtime::TraceLevel::Warn,
                            "TRACE_ERROR" => crate::runtime::TraceLevel::Error,
                            _ => unreachable!(),
                        };

                        // Parse category
                        let category = match &args[0] {
                            Value::String(s) => s.clone(),
                            _ => {
                                return {
                                    let err = RuntimeError::CustomError(format!(
                                        "TRACE category must be a string, got {}",
                                        args[0].type_name()
                                    ));
                                    let err = self.attach_call_stack_if_absent(err);
                                    let _ = self.call_stack.pop();
                                    self.exit_call();
                                    Err(err)
                                };
                            }
                        };

                        // Collect values (args[1..])
                        let values = args[1..].to_vec();

                        // Create and push structured entry
                        let entry = crate::runtime::TraceEntry::new(level, category, values);
                        self.trace_push_entry(entry);

                        Ok(Value::Null)
                    }
                    "MAP" => self.builtin_map(&args),
                    "FILTER" => self.builtin_filter(&args),
                    "REDUCE" => self.builtin_reduce(&args),
                    _ => {
                        // Get the built-in function from the registry
                        if let Some((func, _arity)) = self.registry.get(name) {
                            // Call the built-in function
                            func(&args)
                        } else {
                            Err(RuntimeError::NotCallable(format!(
                                "Built-in function '{}' not found",
                                name
                            )))
                        }
                    }
                };

                let _ = self.call_stack.pop();
                self.exit_call();
                match res {
                    Ok(v) => Ok(v),
                    Err(e) => Err(self.attach_call_stack_if_absent(e)),
                }
            }

            _ => {
                let err = RuntimeError::NotCallable(func.type_name().to_string());
                let err = self.attach_call_stack_if_absent(err);
                let _ = self.call_stack.pop();
                self.exit_call();
                Err(err)
            }
        }
    }

    // 实现 MAP 内置函数
    fn builtin_map(&mut self, args: &[Value]) -> EvalResult {
        if args.len() != 2 {
            return Err(RuntimeError::WrongArity {
                expected: 2,
                got: args.len(),
            });
        }

        let arr = match &args[0] {
            Value::Array(a) => a,
            other => {
                return Err(RuntimeError::TypeErrorDetailed {
                    expected: "Array".to_string(),
                    got: format!("{:?}", other),
                });
            }
        };

        let func = &args[1];

        let mut result = Vec::new();
        for item in arr {
            let mapped = self.call_function(None, func, vec![item.clone()])?;
            result.push(mapped);
        }

        Ok(Value::Array(result))
    }

    // 实现 FILTER 内置函数
    fn builtin_filter(&mut self, args: &[Value]) -> EvalResult {
        if args.len() != 2 {
            return Err(RuntimeError::WrongArity {
                expected: 2,
                got: args.len(),
            });
        }

        let arr = match &args[0] {
            Value::Array(a) => a,
            other => {
                return Err(RuntimeError::TypeErrorDetailed {
                    expected: "Array".to_string(),
                    got: format!("{:?}", other),
                });
            }
        };

        let predicate = &args[1];

        let mut result = Vec::new();
        for item in arr {
            let test_result = self.call_function(None, predicate, vec![item.clone()])?;
            if test_result.is_truthy() {
                result.push(item.clone());
            }
        }

        Ok(Value::Array(result))
    }

    // 实现 REDUCE 内置函数
    fn builtin_reduce(&mut self, args: &[Value]) -> EvalResult {
        if args.len() != 3 {
            return Err(RuntimeError::WrongArity {
                expected: 3,
                got: args.len(),
            });
        }

        let arr = match &args[0] {
            Value::Array(a) => a,
            other => {
                return Err(RuntimeError::TypeErrorDetailed {
                    expected: "Array".to_string(),
                    got: format!("{:?}", other),
                });
            }
        };

        let func = match &args[1] {
            Value::Function { .. } | Value::BuiltIn { .. } => &args[1],
            other => {
                return Err(RuntimeError::TypeErrorDetailed {
                    expected: "Function".to_string(),
                    got: format!("{:?}", other),
                });
            }
        };

        let mut accumulator = args[2].clone();

        for (idx, item) in arr.iter().enumerate() {
            let arg_count = match func {
                Value::Function { params, .. } => params.len(),
                Value::BuiltIn { arity, .. } => *arity,
                _ => 0,
            };

            let mut call_args = Vec::new();
            call_args.push(accumulator);
            call_args.push(item.clone());
            if arg_count >= 3 {
                call_args.push(Value::Number(idx as f64));
            }

            if arg_count < 2 {
                return Err(RuntimeError::WrongArity {
                    expected: 2,
                    got: arg_count,
                });
            }

            accumulator = self.call_function(None, func, call_args)?;
        }

        Ok(accumulator)
    }
}

impl Evaluator {
    fn import_chain(&self) -> Vec<String> {
        self.import_base_stack
            .iter()
            .map(|c| c.module_id.clone())
            .collect()
    }

    fn import_chain_with(&self, leaf: impl Into<String>) -> Vec<String> {
        let mut chain = self.import_chain();
        chain.push(leaf.into());
        chain
    }

    fn current_import_context(&self) -> Option<&ModuleContext> {
        self.import_base_stack.last()
    }

    fn eval_import(
        &mut self,
        names: &[String],
        specifier: &str,
        aliases: &[Option<String>],
        namespace: Option<&String>,
    ) -> EvalResult {
        let from_ctx = self.current_import_context();

        let chain_for_resolve = self.import_chain_with(specifier.to_string());

        let resolved = self
            .module_resolver
            .resolve(specifier, from_ctx)
            .map_err(|e| {
                RuntimeError::ImportError(Box::new(ImportError::from_resolve_error(
                    specifier,
                    e,
                    chain_for_resolve,
                )))
            })?;

        let exports = self.load_module(resolved)?;

        if let Some(ns) = namespace {
            self.env.borrow_mut().set(ns.clone(), Value::Dict(exports));
            return Ok(Value::Null);
        }

        for (i, name) in names.iter().enumerate() {
            let alias = aliases
                .get(i)
                .and_then(|a| a.clone())
                .unwrap_or_else(|| name.clone());
            let v = exports.get(name).cloned().ok_or_else(|| {
                RuntimeError::ImportError(Box::new(ImportError::not_exported(
                    specifier,
                    name,
                    self.import_chain_with(specifier.to_string()),
                )))
            })?;
            self.env.borrow_mut().set(alias, v);
        }

        Ok(Value::Null)
    }

    fn eval_export(&mut self, name: &str) -> EvalResult {
        let exports = self.export_stack.last_mut().ok_or_else(|| {
            RuntimeError::CustomError("Export error: Export used outside of a module".to_string())
        })?;

        let val = self.env.borrow().get(name).ok_or_else(|| {
            RuntimeError::CustomError(format!("Export error: '{}' is not defined", name))
        })?;

        exports.insert(name.to_string(), val);
        Ok(Value::Null)
    }

    fn load_module(
        &mut self,
        resolved: ResolvedModule,
    ) -> Result<HashMap<String, Value>, RuntimeError> {
        let import_chain = self.import_chain_with(resolved.module_id.clone());

        if let Some(cached) = self.module_cache.get(&resolved.module_id) {
            return Ok(cached.clone());
        }

        if self.module_stack.contains(&resolved.module_id) {
            let mut chain = self.module_stack.clone();
            chain.push(resolved.module_id.clone());
            return Err(RuntimeError::ImportError(Box::new(ImportError::circular(
                &resolved.module_id,
                chain,
                import_chain,
            ))));
        }

        self.module_stack.push(resolved.module_id.clone());

        // Parse module
        let mut parser = crate::parser::Parser::new(&resolved.source);
        let program = match parser.parse_program() {
            Ok(p) => p,
            Err(e) => {
                let _ = self.module_stack.pop();
                return Err(RuntimeError::ImportError(Box::new(
                    ImportError::parse_failed(&resolved.module_id, e.to_string(), import_chain),
                )));
            }
        };

        // Evaluate in an isolated environment with builtins registered.
        let prev_env = Rc::clone(&self.env);
        let module_env = Rc::new(RefCell::new(Environment::new()));
        Self::register_builtins_into_env(&self.registry, &mut module_env.borrow_mut());
        self.env = module_env;

        // Push module import base (for relative imports inside the module)
        self.import_base_stack.push(ModuleContext {
            module_id: resolved.module_id.clone(),
            base_dir: resolved.base_dir.clone(),
        });

        // Push export table
        self.export_stack.push(HashMap::new());

        let eval_res = self.eval_program(&program);

        // Pop stacks and restore env (must happen even on error)
        let exports = self.export_stack.pop().unwrap_or_default();
        self.import_base_stack.pop();
        self.env = prev_env;

        // Pop module stack
        let _ = self.module_stack.pop();

        // Propagate module evaluation error (cleanup already done)
        let _ = eval_res.map_err(|e| self.attach_call_stack_if_absent(e))?;

        self.module_cache
            .insert(resolved.module_id.clone(), exports.clone());
        Ok(exports)
    }
}

impl Default for Evaluator {
    fn default() -> Self {
        Self::new()
    }
}