ccalc-engine 0.25.0

Core computation engine for ccalc: tokenizer, parser, AST evaluator, and memory cells
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
use std::collections::HashMap;
use std::rc::Rc;

/// Parsed function body cache: body source string → pre-parsed, all-silent statements.
type BodyCache = HashMap<String, Rc<Vec<(Stmt, bool)>>>;

/// Expands a leading `~` to the user's home directory.
///
/// On Windows `USERPROFILE` is tried as a fallback for `HOME`. If neither is set the
/// string is returned unchanged.
fn expand_tilde(path: &str) -> String {
    if path == "~" || path.starts_with("~/") || path.starts_with("~\\") {
        let home = std::env::var("HOME")
            .or_else(|_| std::env::var("USERPROFILE"))
            .unwrap_or_default();
        if home.is_empty() {
            return path.to_string();
        }
        if path == "~" {
            home
        } else {
            format!("{}{}", home, &path[1..])
        }
    } else {
        path.to_string()
    }
}

use indexmap::IndexMap;
use ndarray::Array2;

use crate::env::{Env, Value};
use crate::env::{load_workspace, save_workspace, save_workspace_vars};
use crate::eval::{
    Base, Expr, FormatMode, autoload_cache_insert, current_func_name, eval_with_io, format_complex,
    format_scalar, format_value_full, get_display_base, get_display_compact, get_display_fmt,
    global_declare, global_frame_pop, global_frame_push, global_get, global_init_if_absent,
    global_refresh_into_env, global_set, is_global, is_persistent, persistent_declare,
    persistent_frame_pop, persistent_frame_push, persistent_load, persistent_save,
    set_autoload_hook, set_display_ctx, set_fn_call_hook, set_last_err, set_nargout,
};
use crate::io::IoContext;
use crate::parser::{Stmt, parse_stmts};

thread_local! {
    /// Tracks the current script nesting depth to prevent infinite recursion via `run()`.
    static RUN_DEPTH: std::cell::Cell<u32> = const { std::cell::Cell::new(0) };

    /// Stack of directories for currently executing scripts.
    ///
    /// When `run()`/`source()` starts executing a script, the script's parent directory is
    /// pushed here. `resolve_script_path` searches this stack (top-first) so that helper
    /// scripts can be referenced by bare name relative to the calling script's directory.
    static SCRIPT_DIR_STACK: std::cell::RefCell<Vec<std::path::PathBuf>> =
        const { std::cell::RefCell::new(Vec::new()) };

    /// Session search path — initialized from `config.toml` at startup.
    ///
    /// `addpath`/`rmpath` mutate this list for the current session; changes are never
    /// written back to `config.toml`. `resolve_script_path` searches here after CWD.
    static SESSION_PATH: std::cell::RefCell<Vec<std::path::PathBuf>> =
        const { std::cell::RefCell::new(Vec::new()) };

    /// Parse cache for named function bodies.
    ///
    /// Key: body source string (verbatim text between `function` and `end`).
    /// Value: pre-parsed, all-silent statement sequence.
    ///
    /// Populated on the first call to any function; subsequent calls with the
    /// same body string skip parsing entirely and reuse the shared `Rc`.
    /// Cache entries are never evicted — acceptable because the number of
    /// unique function bodies in a session is small.
    static BODY_CACHE: std::cell::RefCell<BodyCache> =
        std::cell::RefCell::new(HashMap::new());
}

/// Recursively marks every statement in `stmts` (and all nested block bodies) as silent.
///
/// Function bodies must suppress all output — assignments, expressions, everything.
/// Because [`parse_stmts`] records the original semicolon flag per statement, a simple
/// `map(|(s,_)| (s, true))` only silences the top level.  Nested bodies inside `if`,
/// `for`, `while`, `switch`, `do..until`, and `try..catch` keep their original flags
/// and would still print.  This function walks the full statement tree.
fn silence_all(stmts: Vec<(Stmt, bool)>) -> Vec<(Stmt, bool)> {
    stmts
        .into_iter()
        .map(|(stmt, _)| {
            let stmt = match stmt {
                Stmt::If {
                    cond,
                    body,
                    elseif_branches,
                    else_body,
                } => Stmt::If {
                    cond,
                    body: silence_all(body),
                    elseif_branches: elseif_branches
                        .into_iter()
                        .map(|(c, b)| (c, silence_all(b)))
                        .collect(),
                    else_body: else_body.map(silence_all),
                },
                Stmt::For {
                    var,
                    range_expr,
                    body,
                } => Stmt::For {
                    var,
                    range_expr,
                    body: silence_all(body),
                },
                Stmt::While { cond, body } => Stmt::While {
                    cond,
                    body: silence_all(body),
                },
                Stmt::DoUntil { body, cond } => Stmt::DoUntil {
                    body: silence_all(body),
                    cond,
                },
                Stmt::Switch {
                    expr,
                    cases,
                    otherwise_body,
                } => Stmt::Switch {
                    expr,
                    cases: cases
                        .into_iter()
                        .map(|(v, b)| (v, silence_all(b)))
                        .collect(),
                    otherwise_body: otherwise_body.map(silence_all),
                },
                Stmt::TryCatch {
                    try_body,
                    catch_var,
                    catch_body,
                } => Stmt::TryCatch {
                    try_body: silence_all(try_body),
                    catch_var,
                    catch_body: silence_all(catch_body),
                },
                other => other,
            };
            (stmt, true)
        })
        .collect()
}

/// Returns a parsed, all-silent body for `body_source`, using the cache when possible.
///
/// "All-silent" means every `(Stmt, bool)` has `bool = true` — function bodies
/// never print output directly. The parse result is shared via `Rc` so that
/// repeated calls to the same function avoid both allocation and parsing work.
fn get_or_parse_body(body_source: &str) -> Result<Rc<Vec<(Stmt, bool)>>, String> {
    BODY_CACHE.with(|cache| {
        let mut cache = cache.borrow_mut();
        if let Some(body) = cache.get(body_source) {
            return Ok(Rc::clone(body));
        }
        let stmts =
            parse_stmts(body_source).map_err(|e| format!("function body parse error: {e}"))?;
        let silent = silence_all(stmts);
        let rc = Rc::new(silent);
        cache.insert(body_source.to_string(), Rc::clone(&rc));
        Ok(rc)
    })
}

/// Flow control signal returned by [`exec_stmts`].
///
/// Used to propagate `break`, `continue`, and `return` through nested block calls.
/// Loop implementations catch `Break`/`Continue`; function call implementation catches `Return`.
/// Uncaught signals at the top level are reported as errors.
pub enum Signal {
    /// `break` — exit the innermost enclosing loop immediately.
    Break,
    /// `continue` — skip the rest of the current iteration and advance to the next.
    Continue,
    /// `return` inside a named function — carries no value (outputs are read from env).
    Return,
}

/// Initialises exec-level hooks in `eval.rs` so that `eval_inner` can call user functions.
///
/// Must be called once at program startup before any evaluation takes place.
pub fn init() {
    set_fn_call_hook(call_user_function);
    set_autoload_hook(try_autoload);
}

/// Searches for `<name>.calc` or `<name>.m` on the session path and loads it.
///
/// Mirrors MATLAB/Octave autoload: when a function name is not found in the
/// environment, ccalc looks for a matching file on the path, loads its primary
/// function into the autoload cache, and returns `true` on success.
/// For package-qualified names (containing `.`), delegates to [`try_autoload_pkg`].
fn try_autoload(name: &str) -> bool {
    if name.contains('.') {
        return try_autoload_pkg(name);
    }
    let candidates = [format!("{name}.calc"), format!("{name}.m")];
    for candidate in &candidates {
        let Some(path) = resolve_script_path(candidate) else {
            continue;
        };
        let Ok(content) = std::fs::read_to_string(&path) else {
            continue;
        };
        let Ok(stmts) = parse_stmts(&content) else {
            continue;
        };
        if !matches!(stmts.first(), Some((Stmt::FunctionDef { .. }, _))) {
            continue;
        }
        let primary_name = match &stmts[0].0 {
            Stmt::FunctionDef { name, .. } => name.clone(),
            _ => continue,
        };
        let mut locals: IndexMap<String, Value> = IndexMap::new();
        for (stmt, _) in &stmts {
            if let Stmt::FunctionDef {
                name: n,
                outputs,
                params,
                body_source,
                doc,
            } = stmt
                && n != &primary_name
            {
                locals.insert(
                    n.clone(),
                    Value::Function {
                        outputs: outputs.clone(),
                        params: params.clone(),
                        body_source: body_source.clone(),
                        locals: IndexMap::new(),
                        doc: doc.clone(),
                    },
                );
            }
        }
        if let Stmt::FunctionDef {
            outputs,
            params,
            body_source,
            doc,
            ..
        } = &stmts[0].0
        {
            autoload_cache_insert(
                primary_name,
                Value::Function {
                    outputs: outputs.clone(),
                    params: params.clone(),
                    body_source: body_source.clone(),
                    locals,
                    doc: doc.clone(),
                },
            );
            return true;
        }
    }
    false
}

/// Searches for a package-qualified function and loads it into the autoload cache.
///
/// A qualified name like `"utils.my_func"` maps to `+utils/my_func.calc`.
/// Nested packages like `"utils.math.lerp"` map to `+utils/+math/lerp.calc`.
/// Searches SCRIPT_DIR_STACK → CWD → SESSION_PATH and caches under the qualified name.
fn try_autoload_pkg(qualified: &str) -> bool {
    let parts: Vec<&str> = qualified.split('.').collect();
    if parts.len() < 2 {
        return false;
    }
    let func_name = *parts.last().unwrap();
    let pkg_parts = &parts[..parts.len() - 1];

    // Build relative path prefix: +pkg1/+pkg2/...
    let mut rel_prefix = std::path::PathBuf::new();
    for pkg in pkg_parts {
        rel_prefix.push(format!("+{pkg}"));
    }

    let candidates = [
        rel_prefix.join(format!("{func_name}.calc")),
        rel_prefix.join(format!("{func_name}.m")),
    ];

    // Collect search directories: script dirs + CWD + session path.
    let mut search_dirs: Vec<std::path::PathBuf> = Vec::new();
    SCRIPT_DIR_STACK.with(|s| search_dirs.extend(s.borrow().iter().cloned()));
    search_dirs.push(std::path::PathBuf::from("."));
    SESSION_PATH.with(|s| search_dirs.extend(s.borrow().iter().cloned()));

    for dir in &search_dirs {
        for candidate in &candidates {
            let full = dir.join(candidate);
            let Ok(content) = std::fs::read_to_string(&full) else {
                continue;
            };
            let Ok(stmts) = parse_stmts(&content) else {
                continue;
            };
            if !matches!(stmts.first(), Some((Stmt::FunctionDef { .. }, _))) {
                continue;
            }
            let primary_name = match &stmts[0].0 {
                Stmt::FunctionDef { name, .. } => name.clone(),
                _ => continue,
            };
            let mut locals: IndexMap<String, Value> = IndexMap::new();
            for (stmt, _) in &stmts {
                if let Stmt::FunctionDef {
                    name: n,
                    outputs,
                    params,
                    body_source,
                    doc,
                } = stmt
                    && n != &primary_name
                {
                    locals.insert(
                        n.clone(),
                        Value::Function {
                            outputs: outputs.clone(),
                            params: params.clone(),
                            body_source: body_source.clone(),
                            locals: IndexMap::new(),
                            doc: doc.clone(),
                        },
                    );
                }
            }
            if let Stmt::FunctionDef {
                outputs,
                params,
                body_source,
                doc,
                ..
            } = &stmts[0].0
            {
                autoload_cache_insert(
                    qualified.to_string(),
                    Value::Function {
                        outputs: outputs.clone(),
                        params: params.clone(),
                        body_source: body_source.clone(),
                        locals,
                        doc: doc.clone(),
                    },
                );
                return true;
            }
        }
    }
    false
}

/// Push a script directory onto the search stack.
///
/// Call this before executing a top-level script file so that `run()`/`source()` calls
/// inside the script can find helper files by bare name relative to the script's directory.
/// Always paired with a matching `script_dir_pop`.
pub fn script_dir_push(dir: &std::path::Path) {
    SCRIPT_DIR_STACK.with(|s| s.borrow_mut().push(dir.to_path_buf()));
}

/// Pop the most recently pushed script directory from the search stack.
pub fn script_dir_pop() {
    SCRIPT_DIR_STACK.with(|s| s.borrow_mut().pop());
}

/// Initializes the session search path from the config `path` array.
///
/// Called once at startup (after loading `config.toml`). Each entry has `~` already
/// expanded by the caller.
pub fn session_path_init(paths: Vec<std::path::PathBuf>) {
    SESSION_PATH.with(|p| *p.borrow_mut() = paths);
}

/// Prepends (default) or appends a directory to the session search path.
///
/// If the same path is already present it is removed from its current position
/// before being re-inserted, so the path list contains no duplicates.
pub fn session_path_add(path: std::path::PathBuf, append: bool) {
    SESSION_PATH.with(|p| {
        let mut v = p.borrow_mut();
        v.retain(|e| e != &path);
        if append {
            v.push(path);
        } else {
            v.insert(0, path);
        }
    });
}

/// Removes a directory from the session search path (exact match).
pub fn session_path_remove(path: &std::path::Path) {
    SESSION_PATH.with(|p| p.borrow_mut().retain(|e| e.as_path() != path));
}

/// Returns a snapshot of the current session search path.
pub fn session_path_list() -> Vec<std::path::PathBuf> {
    SESSION_PATH.with(|p| p.borrow().clone())
}

/// Called by `eval_inner` whenever a user function (`Value::Function`) is invoked.
///
/// Executes the function body in an isolated scope containing only the parameters plus
/// any callable values (`Function`/`Lambda`) from the caller's environment, enabling
/// recursion and mutual recursion.
/// Multi-return: if the function has >1 output, returns `Value::Tuple`.
fn call_user_function(
    name: &str,
    func: &Value,
    args: &[Value],
    caller_env: &Env,
    io: &mut IoContext,
) -> Result<Value, String> {
    let Value::Function {
        outputs,
        params,
        body_source,
        locals,
        ..
    } = func
    else {
        return Err("call_user_function: not a Function value".to_string());
    };

    // Push global and persistent tracking frames for this function call.
    global_frame_push();
    persistent_frame_push(name); // `name` is the function name from the call site

    // Build isolated scope: seed imaginary unit and ans, then copy all callable
    // values (Function/Lambda) from the caller's environment so that recursion
    // and mutual recursion work correctly.
    let mut local_env = Env::new();
    local_env.insert("i".to_string(), Value::Complex(0.0, 1.0));
    local_env.insert("j".to_string(), Value::Complex(0.0, 1.0));
    local_env.insert("ans".to_string(), Value::Scalar(0.0));
    // Local helper functions from the same function file (MATLAB-style scoping).
    // These take priority and are always available regardless of the caller's env.
    for (fn_name, val) in locals.iter() {
        local_env.insert(fn_name.clone(), val.clone());
    }
    for (var_name, val) in caller_env.iter() {
        if matches!(val, Value::Function { .. } | Value::Lambda(_)) {
            local_env.insert(var_name.clone(), val.clone());
        }
    }

    // Check for varargin: last parameter is 'varargin' → variadic function.
    let has_varargin = params.last().is_some_and(|p| p == "varargin");
    let fixed_params = if has_varargin {
        &params[..params.len() - 1]
    } else {
        params.as_slice()
    };

    // Trim any trailing args beyond what the function declares.
    // The parser injects `ans` for empty `f()` calls; for 0-param functions
    // we must silently ignore it. For N-param functions, allow up to N+1
    // args before complaining (one implicit `ans` is always present).
    let effective_args = if args.len() > params.len() {
        if !has_varargin && args.len() > params.len() + 1 {
            return Err(format!(
                "Too many arguments: expected at most {}, got {}",
                params.len(),
                args.len()
            ));
        }
        if has_varargin {
            args
        } else {
            // Exactly 1 extra: the implicit `ans` — trim it.
            &args[..params.len()]
        }
    } else {
        args
    };

    // Bind fixed parameters
    for (p, a) in fixed_params.iter().zip(effective_args.iter()) {
        local_env.insert(p.clone(), a.clone());
    }

    // If varargin, collect remaining args into a Cell
    if has_varargin {
        // Collect extra args beyond the fixed parameters into varargin.
        // User functions do not receive an injected `ans`; the arg list reflects
        // exactly what the caller passed.
        let extra: Vec<Value> = effective_args
            .get(fixed_params.len()..)
            .unwrap_or(&[])
            .to_vec();
        let varargin = Value::Cell(extra);
        local_env.insert("varargin".to_string(), varargin);
    }

    let nargin = effective_args.len().min(params.len());
    local_env.insert("nargin".to_string(), Value::Scalar(nargin as f64));
    local_env.insert("nargout".to_string(), Value::Scalar(outputs.len() as f64));

    // Retrieve (or parse-and-cache) the function body, then execute it.
    let body = get_or_parse_body(body_source)?;
    let fmt = get_display_fmt();
    let base = get_display_base();
    let compact = get_display_compact();
    let exec_result = exec_stmts(&body, &mut local_env, io, &fmt, base, compact);

    // Save persistent variables before unwinding the frame (even on error).
    let (func_name_saved, persistent_names) = persistent_frame_pop();
    for var_name in &persistent_names {
        if let Some(val) = local_env.get(var_name) {
            persistent_save(&func_name_saved, var_name, val.clone());
        }
    }
    // Pop the global names frame.
    global_frame_pop();

    // Propagate any execution error after saving persistent state.
    match exec_result? {
        None | Some(Signal::Return) => {}
        Some(Signal::Break) => return Err("'break' outside loop".to_string()),
        Some(Signal::Continue) => return Err("'continue' outside loop".to_string()),
    }

    // Collect return values
    if outputs.is_empty() {
        return Ok(Value::Void);
    }

    // varargout: single output named 'varargout' — expand from cell
    if outputs.len() == 1 && outputs[0] == "varargout" {
        let cell = local_env.remove("varargout").unwrap_or(Value::Cell(vec![]));
        return match cell {
            Value::Cell(mut v) => {
                if v.is_empty() {
                    Ok(Value::Void)
                } else if v.len() == 1 {
                    Ok(v.remove(0))
                } else {
                    Ok(Value::Tuple(v))
                }
            }
            other => Ok(other),
        };
    }

    if outputs.len() == 1 {
        return Ok(local_env.remove(&outputs[0]).unwrap_or(Value::Void));
    }
    let vals: Vec<Value> = outputs
        .iter()
        .map(|o| local_env.remove(o).unwrap_or(Value::Void))
        .collect();
    Ok(Value::Tuple(vals))
}

/// Resolves a script filename to an existing path.
///
/// If `name` already has an extension, it is used verbatim.
/// Otherwise, `.calc` is tried first (native ccalc format), then `.m` (Octave/MATLAB compatibility).
/// The search is relative to the current working directory.
pub fn resolve_script_path(name: &str) -> Option<std::path::PathBuf> {
    // Build candidate base paths.
    //
    // MATLAB `private/` semantics: a `private/` sub-directory is visible only
    // to scripts/functions in its parent directory.  Therefore:
    //   • SCRIPT_DIR_STACK entries (the calling script's own directory) ARE
    //     allowed to search their `private/` sub-folder — checked first.
    //   • CWD and SESSION_PATH entries do NOT get a `private/` look-aside;
    //     they can only see files directly in those directories.
    let p = std::path::Path::new(name);
    let mut bases: Vec<std::path::PathBuf> = Vec::new();

    // Stacked script directories (most-recent first): private/ before the dir.
    SCRIPT_DIR_STACK.with(|stack| {
        for dir in stack.borrow().iter().rev() {
            bases.push(dir.join("private").join(p));
            bases.push(dir.join(p));
        }
    });

    // CWD-relative (no private/ look-aside).
    bases.push(p.to_path_buf());

    // Session search-path entries (no private/ look-aside).
    SESSION_PATH.with(|sp| {
        for dir in sp.borrow().iter() {
            bases.push(dir.join(p));
        }
    });

    for base in &bases {
        if base.extension().is_some() {
            if base.exists() {
                return Some(base.clone());
            }
            // Explicit extension given but not found — try next base.
            continue;
        }
        let with_calc = base.with_extension("calc");
        if with_calc.exists() {
            return Some(with_calc);
        }
        let with_m = base.with_extension("m");
        if with_m.exists() {
            return Some(with_m);
        }
    }
    None
}

/// Returns `true` if `val` is considered truthy by MATLAB `if`/`while` semantics.
///
/// - Scalar: nonzero and not NaN.
/// - Matrix: all elements nonzero and not NaN.
/// - Complex: either part nonzero.
/// - Str/StringObj: nonempty.
/// - Void: always false.
fn is_truthy(val: &Value) -> bool {
    match val {
        Value::Scalar(n) => *n != 0.0 && !n.is_nan(),
        Value::Matrix(m) => m.iter().all(|&x| x != 0.0 && !x.is_nan()),
        Value::Complex(re, im) => *re != 0.0 || *im != 0.0,
        Value::Str(s) | Value::StringObj(s) => !s.is_empty(),
        Value::Void => false,
        // Functions are truthy (they exist), but comparing them to 0 makes no sense.
        // Treat as truthy so that `if f` doesn't silently fail.
        Value::Lambda(_) | Value::Function { .. } | Value::Tuple(_) => true,
        // A cell is truthy if nonempty.
        Value::Cell(v) => !v.is_empty(),
        // A struct / struct array is always truthy.
        Value::Struct(_) | Value::StructArray(_) => true,
    }
}

/// Prints a value to stdout with MATLAB-style formatting.
///
/// `label` is `Some("name")` for assignment output and `None` for expression output.
/// In expression context scalars/complex print without a label; matrices print `ans =`.
fn print_value(label: Option<&str>, val: &Value, fmt: &FormatMode, base: Base, compact: bool) {
    match val {
        Value::Void => {}
        Value::Scalar(n) => {
            if let Some(name) = label {
                println!("{name} = {}", format_scalar(*n, base, fmt));
            } else {
                println!("{}", format_scalar(*n, base, fmt));
            }
        }
        Value::Matrix(_) => {
            if let Some(full) = format_value_full(val, fmt) {
                let prefix = label.unwrap_or("ans");
                println!("{prefix} =");
                println!("{full}");
                if !compact {
                    println!();
                }
            }
        }
        Value::Complex(re, im) => {
            if let Some(name) = label {
                println!("{name} = {}", format_complex(*re, *im, fmt));
            } else {
                println!("{}", format_complex(*re, *im, fmt));
            }
        }
        Value::Str(s) | Value::StringObj(s) => {
            if let Some(name) = label {
                println!("{name} = {s}");
            } else {
                println!("{s}");
            }
        }
        Value::Lambda(_) => {
            if let Some(name) = label {
                println!("{name} = @<lambda>");
            } else {
                println!("@<lambda>");
            }
        }
        Value::Function {
            outputs, params, ..
        } => {
            let params_str = params.join(", ");
            let out_str = match outputs.len() {
                0 => String::new(),
                1 => format!("{} = ", outputs[0]),
                _ => format!("[{}] = ", outputs.join(", ")),
            };
            if let Some(name) = label {
                println!("{name} = @function {out_str}{name}({params_str})");
            } else {
                println!("@function {out_str}f({params_str})");
            }
        }
        Value::Tuple(vals) => {
            // Tuples are internal and shouldn't normally be displayed at the REPL level.
            // This can happen if a multi-output function is called without multi-assign.
            for (i, v) in vals.iter().enumerate() {
                print_value(label.map(|_| "ans").or(Some("ans")), v, fmt, base, compact);
                let _ = i;
            }
        }
        Value::Cell(_) | Value::Struct(_) | Value::StructArray(_) => {
            if let Some(full) = format_value_full(val, fmt) {
                let prefix = label.unwrap_or("ans");
                println!("{prefix} =");
                println!("{full}");
                if !compact {
                    println!();
                }
            }
        }
    }
}

/// Recursively sets a value at `path` inside a nested struct map.
///
/// Ownership-by-value approach: consumes the map, updates it, and returns the updated map.
/// Intermediate structs are created on demand if a path segment does not yet exist.
fn set_nested(
    mut map: IndexMap<String, Value>,
    path: &[String],
    val: Value,
) -> Result<IndexMap<String, Value>, String> {
    let (first, rest) = path.split_first().expect("set_nested: empty path");
    if rest.is_empty() {
        map.insert(first.clone(), val);
    } else {
        let inner = match map.shift_remove(first) {
            Some(Value::Struct(m)) => m,
            None => IndexMap::new(),
            Some(other) => {
                map.insert(first.clone(), other);
                return Err(format!("'{first}' is not a struct"));
            }
        };
        let updated = set_nested(inner, rest, val)?;
        map.insert(first.clone(), Value::Struct(updated));
    }
    Ok(map)
}

/// Executes a sequence of parsed statements, handling flow control signals.
///
/// Returns:
/// - `Ok(None)` — normal completion
/// - `Ok(Some(Signal::Break))` — `break` statement executed
/// - `Ok(Some(Signal::Continue))` — `continue` statement executed
/// - `Err(e)` — runtime error
///
/// Loop implementations (`For`, `While`) catch `Break`/`Continue` internally.
/// A signal that escapes to the top-level caller should be reported as an error.
pub fn exec_stmts(
    stmts: &[(Stmt, bool)],
    env: &mut Env,
    io: &mut IoContext,
    fmt: &FormatMode,
    base: Base,
    compact: bool,
) -> Result<Option<Signal>, String> {
    // Propagate display settings to eval.rs so named function bodies can use them.
    set_display_ctx(fmt, base, compact);

    for (stmt, silent) in stmts {
        match stmt {
            Stmt::Assign(name, expr) => {
                set_nargout(1);
                let val = eval_with_io(expr, env, io)?;
                env.insert(name.clone(), val.clone());
                // Mirror to the shared global store when declared global in this scope.
                if is_global(name) {
                    global_set(name, val.clone());
                }
                // Write-through for persistent vars: recursive calls can see the update.
                if is_persistent(name) {
                    persistent_save(&current_func_name(), name, val.clone());
                }
                if !silent && !matches!(val, Value::Void) {
                    print_value(Some(name), &val, fmt, base, compact);
                }
            }

            Stmt::Global(names) => {
                for name in names {
                    global_declare(name);
                    global_init_if_absent(name);
                    // If the name was already assigned in local env, promote it to global.
                    // Otherwise restore the current global value into local env.
                    if let Some(local_val) = env.remove(name) {
                        global_set(name, local_val.clone());
                        env.insert(name.clone(), local_val);
                    } else if let Some(global_val) = global_get(name) {
                        env.insert(name.clone(), global_val);
                    }
                }
            }

            Stmt::Persistent(names) => {
                // Persistent is only meaningful inside a named function.
                // At the top level it is accepted and treated as a no-op.
                let func = current_func_name();
                for name in names {
                    persistent_declare(name);
                    if let Some(saved) = persistent_load(&func, name) {
                        // Subsequent call: restore the saved value.
                        env.insert(name.clone(), saved);
                    } else {
                        // First call: initialize to [] (empty matrix), matching MATLAB.
                        // This makes isempty(x) true so guards like
                        // `if isempty(x); x = 0; end` work correctly.
                        env.insert(name.clone(), Value::Matrix(ndarray::Array2::zeros((0, 0))));
                    }
                }
            }

            Stmt::Expr(expr) => {
                // Intercept addpath()/rmpath()/path() — mutate the session search path.
                if let Expr::Call(fn_name, args) = expr
                    && matches!(fn_name.as_str(), "addpath" | "rmpath" | "path")
                {
                    match fn_name.as_str() {
                        "addpath" => {
                            if args.is_empty() || args.len() > 2 {
                                return Err(
                                    "addpath: expects 1 or 2 arguments: addpath(dir) or addpath(dir, '-end')".to_string()
                                );
                            }
                            let path_val = eval_with_io(&args[0], env, io)?;
                            let path_str = match &path_val {
                                Value::Str(s) | Value::StringObj(s) => s.clone(),
                                _ => {
                                    return Err(
                                        "addpath: argument must be a string (directory path)"
                                            .to_string(),
                                    );
                                }
                            };
                            let append = if args.len() == 2 {
                                let flag_val = eval_with_io(&args[1], env, io)?;
                                match &flag_val {
                                    Value::Str(s) | Value::StringObj(s) if s == "-end" => true,
                                    Value::Str(_) | Value::StringObj(_) => {
                                        return Err(
                                            "addpath: second argument must be '-end' (to append) or omitted (to prepend)".to_string()
                                        );
                                    }
                                    _ => {
                                        return Err(
                                            "addpath: second argument must be a string '-end'"
                                                .to_string(),
                                        );
                                    }
                                }
                            } else {
                                false
                            };
                            let expanded = expand_tilde(&path_str);
                            let pb = std::path::PathBuf::from(&expanded);
                            session_path_add(pb, append);
                            if !silent {
                                for p in session_path_list() {
                                    println!("{}", p.display());
                                }
                            }
                        }
                        "rmpath" => {
                            if args.len() != 1 {
                                return Err("rmpath: expects exactly 1 argument".to_string());
                            }
                            let path_val = eval_with_io(&args[0], env, io)?;
                            let path_str = match &path_val {
                                Value::Str(s) | Value::StringObj(s) => s.clone(),
                                _ => {
                                    return Err(
                                        "rmpath: argument must be a string (directory path)"
                                            .to_string(),
                                    );
                                }
                            };
                            let expanded = expand_tilde(&path_str);
                            session_path_remove(std::path::Path::new(&expanded));
                        }
                        "path" => {
                            if !args.is_empty() {
                                return Err("path: takes no arguments".to_string());
                            }
                            if !silent {
                                let paths = session_path_list();
                                if paths.is_empty() {
                                    println!("(search path is empty)");
                                } else {
                                    for p in &paths {
                                        println!("{}", p.display());
                                    }
                                }
                            }
                        }
                        _ => unreachable!(),
                    }
                    continue;
                }

                // Intercept run()/source() — execute a script file in the current workspace.
                // Variables defined in the script persist in the caller's scope (MATLAB `run` semantics).
                if let Expr::Call(fn_name, args) = expr
                    && matches!(fn_name.as_str(), "run" | "source")
                    && args.len() == 1
                {
                    let path_val = eval_with_io(&args[0], env, io)?;
                    let filename = match &path_val {
                        Value::Str(s) | Value::StringObj(s) => s.clone(),
                        _ => {
                            return Err(format!("{fn_name}: argument must be a string (filename)"));
                        }
                    };
                    let script_path = resolve_script_path(&filename)
                        .ok_or_else(|| format!("{fn_name}: script not found: '{filename}'"))?;
                    let content = std::fs::read_to_string(&script_path).map_err(|e| {
                        format!("{fn_name}: cannot read '{}': {e}", script_path.display())
                    })?;
                    let depth = RUN_DEPTH.with(|d| d.get());
                    if depth >= 64 {
                        return Err(format!(
                            "{fn_name}: maximum script nesting depth (64) exceeded"
                        ));
                    }
                    RUN_DEPTH.with(|d| d.set(depth + 1));
                    // Push the script's directory so nested run()/source() calls resolve
                    // helper scripts relative to the calling script's location.
                    if let Some(dir) = script_path.parent() {
                        SCRIPT_DIR_STACK.with(|s| s.borrow_mut().push(dir.to_path_buf()));
                    }
                    let run_stmts = parse_stmts(&content).map_err(|e| {
                        format!("{fn_name}: parse error in '{}': {e}", script_path.display())
                    })?;

                    // MATLAB scoping: if EVERY statement is a function definition,
                    // treat it as a function file — expose only the primary function and
                    // bundle all helpers into its `locals` (invisible to the caller).
                    // A mixed script+function file (functions-at-top style) is NOT a
                    // function file; its script body must also execute.
                    let is_fn_file = !run_stmts.is_empty()
                        && run_stmts
                            .iter()
                            .all(|(s, _)| matches!(s, Stmt::FunctionDef { .. }));
                    let result = if is_fn_file {
                        let primary_name = match &run_stmts[0].0 {
                            Stmt::FunctionDef { name, .. } => name.clone(),
                            _ => unreachable!(),
                        };
                        let mut locals: IndexMap<String, Value> = IndexMap::new();
                        for (stmt, _) in &run_stmts {
                            if let Stmt::FunctionDef {
                                name,
                                outputs,
                                params,
                                body_source,
                                doc,
                            } = stmt
                                && name != &primary_name
                            {
                                locals.insert(
                                    name.clone(),
                                    Value::Function {
                                        outputs: outputs.clone(),
                                        params: params.clone(),
                                        body_source: body_source.clone(),
                                        locals: IndexMap::new(),
                                        doc: doc.clone(),
                                    },
                                );
                            }
                        }
                        if let Stmt::FunctionDef {
                            outputs,
                            params,
                            body_source,
                            doc,
                            ..
                        } = &run_stmts[0].0
                        {
                            env.insert(
                                primary_name,
                                Value::Function {
                                    outputs: outputs.clone(),
                                    params: params.clone(),
                                    body_source: body_source.clone(),
                                    locals,
                                    doc: doc.clone(),
                                },
                            );
                        }
                        Ok(None)
                    } else {
                        // Pre-load all local function defs so that forward references
                        // within the script work (MATLAB local-function semantics).
                        for (stmt, _) in run_stmts.iter() {
                            if let Stmt::FunctionDef {
                                name,
                                outputs,
                                params,
                                body_source,
                                doc,
                            } = stmt
                            {
                                env.insert(
                                    name.clone(),
                                    Value::Function {
                                        outputs: outputs.clone(),
                                        params: params.clone(),
                                        body_source: body_source.clone(),
                                        locals: IndexMap::new(),
                                        doc: doc.clone(),
                                    },
                                );
                            }
                        }
                        exec_stmts(&run_stmts, env, io, fmt, base, compact)
                    };
                    SCRIPT_DIR_STACK.with(|s| s.borrow_mut().pop());
                    RUN_DEPTH.with(|d| d.set(depth));
                    // Propagate signals (return/break/continue) but do NOT early-return
                    // from exec_stmts — remaining statements in the outer script must run.
                    match result? {
                        None => {}
                        Some(sig) => return Ok(Some(sig)),
                    }
                    continue;
                }

                // Intercept clear() / clear('x','y') — workspace variable removal.
                if let Expr::Call(fn_name, args) = expr
                    && fn_name == "clear"
                {
                    if args.is_empty() {
                        env.clear();
                    } else {
                        for arg in args {
                            let key = match arg {
                                Expr::StrLiteral(s) | Expr::StringObjLiteral(s) => s.clone(),
                                other => match eval_with_io(other, env, io)? {
                                    Value::Str(s) | Value::StringObj(s) => s,
                                    _ => continue,
                                },
                            };
                            env.remove(&key);
                        }
                    }
                    continue;
                }

                // Intercept format — update thread-local display context.
                if let Expr::Call(fn_name, args) = expr
                    && fn_name == "format"
                {
                    let arg = match args.first() {
                        Some(Expr::StrLiteral(s)) => s.as_str(),
                        None => "",
                        _ => "",
                    };
                    let new_fmt = match arg {
                        "" | "short" => FormatMode::Short,
                        "long" => FormatMode::Long,
                        "shorte" | "shortE" => FormatMode::ShortE,
                        "longe" | "longE" => FormatMode::LongE,
                        "shortg" | "shortG" => FormatMode::ShortG,
                        "longg" | "longG" => FormatMode::LongG,
                        "bank" => FormatMode::Bank,
                        "rat" => FormatMode::Rat,
                        "hex" => FormatMode::Hex,
                        "+" => FormatMode::Plus,
                        "compact" | "loose" => get_display_fmt(),
                        s => s
                            .parse::<usize>()
                            .map(FormatMode::Custom)
                            .unwrap_or_else(|_| get_display_fmt()),
                    };
                    let new_compact = match arg {
                        "compact" => true,
                        "loose" => false,
                        _ => get_display_compact(),
                    };
                    set_display_ctx(&new_fmt, base, new_compact);
                    continue;
                }

                // Intercept save()/load() — workspace persistence (same semantics as REPL).
                if let Expr::Call(fn_name, args) = expr
                    && matches!(fn_name.as_str(), "save" | "load" | "ws" | "wl")
                {
                    let is_save = matches!(fn_name.as_str(), "save" | "ws");
                    if is_save {
                        let (path_opt, var_names) = if args.is_empty() {
                            (None, vec![])
                        } else {
                            let path_val = eval_with_io(&args[0], env, io)?;
                            let path_str = match path_val {
                                Value::Str(s) | Value::StringObj(s) => s,
                                _ => return Err("save: path argument must be a string".to_string()),
                            };
                            let mut vars: Vec<String> = Vec::new();
                            for a in &args[1..] {
                                let v = eval_with_io(a, env, io)?;
                                match v {
                                    Value::Str(s) | Value::StringObj(s) => vars.push(s),
                                    _ => {
                                        return Err(
                                            "save: variable names must be strings".to_string()
                                        );
                                    }
                                }
                            }
                            (Some(path_str), vars)
                        };
                        let result = match &path_opt {
                            None => {
                                let home = std::env::var("HOME")
                                    .or_else(|_| std::env::var("USERPROFILE"))
                                    .unwrap_or_default();
                                let p = std::path::Path::new(&home)
                                    .join(".config")
                                    .join("ccalc")
                                    .join("workspace.toml");
                                save_workspace(env, &p)
                            }
                            Some(p) if var_names.is_empty() => {
                                save_workspace(env, std::path::Path::new(p))
                            }
                            Some(p) => {
                                let refs: Vec<&str> =
                                    var_names.iter().map(String::as_str).collect();
                                save_workspace_vars(env, std::path::Path::new(p), &refs)
                            }
                        };
                        if let Err(e) = result {
                            return Err(format!("save: {e}"));
                        }
                    } else {
                        // load
                        let loaded = if args.is_empty() {
                            let home = std::env::var("HOME")
                                .or_else(|_| std::env::var("USERPROFILE"))
                                .unwrap_or_default();
                            let p = std::path::Path::new(&home)
                                .join(".config")
                                .join("ccalc")
                                .join("workspace.toml");
                            load_workspace(&p)
                        } else {
                            let path_val = eval_with_io(&args[0], env, io)?;
                            let path_str = match path_val {
                                Value::Str(s) | Value::StringObj(s) => s,
                                _ => return Err("load: path argument must be a string".to_string()),
                            };
                            load_workspace(std::path::Path::new(&path_str))
                        };
                        match loaded {
                            Ok(ws) => env.extend(ws),
                            Err(e) => return Err(format!("load: {e}")),
                        }
                    }
                    continue;
                }

                let val = eval_with_io(expr, env, io)?;
                env.insert("ans".to_string(), val.clone());
                if !silent && !matches!(val, Value::Void) {
                    print_value(None, &val, fmt, base, compact);
                }
            }

            Stmt::If {
                cond,
                body,
                elseif_branches,
                else_body,
            } => {
                let cond_val = eval_with_io(cond, env, io)?;
                let chosen: Option<&[(Stmt, bool)]> = if is_truthy(&cond_val) {
                    Some(body)
                } else {
                    let mut found = None;
                    for (ei_cond, ei_body) in elseif_branches {
                        if is_truthy(&eval_with_io(ei_cond, env, io)?) {
                            found = Some(ei_body.as_slice());
                            break;
                        }
                    }
                    if found.is_none() {
                        found = else_body.as_deref();
                    }
                    found
                };
                if let Some(body_stmts) = chosen
                    && let Some(sig) = exec_stmts(body_stmts, env, io, fmt, base, compact)?
                {
                    return Ok(Some(sig));
                }
            }

            Stmt::For {
                var,
                range_expr,
                body,
            } => {
                let range_val = eval_with_io(range_expr, env, io)?;
                let iter_cols: Vec<Value> = match range_val {
                    Value::Scalar(n) => vec![Value::Scalar(n)],
                    Value::Matrix(m) => {
                        let nrows = m.nrows();
                        let ncols = m.ncols();
                        (0..ncols)
                            .map(|j| {
                                if nrows == 1 {
                                    // Row vector: yield each element as a scalar
                                    Value::Scalar(m[[0, j]])
                                } else {
                                    // General matrix: yield each column as an M×1 matrix
                                    let mut col = Array2::zeros((nrows, 1));
                                    for i in 0..nrows {
                                        col[[i, 0]] = m[[i, j]];
                                    }
                                    Value::Matrix(col)
                                }
                            })
                            .collect()
                    }
                    _ => return Err("'for' range must evaluate to a scalar or matrix".to_string()),
                };

                'for_loop: for col_val in iter_cols {
                    env.insert(var.clone(), col_val);
                    match exec_stmts(body, env, io, fmt, base, compact)? {
                        None => {}
                        Some(Signal::Break) => break 'for_loop,
                        Some(Signal::Continue) => continue 'for_loop,
                        Some(Signal::Return) => return Ok(Some(Signal::Return)),
                    }
                }
            }

            Stmt::While { cond, body } => loop {
                if !is_truthy(&eval_with_io(cond, env, io)?) {
                    break;
                }
                match exec_stmts(body, env, io, fmt, base, compact)? {
                    None => {}
                    Some(Signal::Break) => break,
                    Some(Signal::Continue) => continue,
                    Some(Signal::Return) => return Ok(Some(Signal::Return)),
                }
            },

            Stmt::Break => return Ok(Some(Signal::Break)),
            Stmt::Continue => return Ok(Some(Signal::Continue)),

            // ── switch / case / otherwise / end ──────────────────────────────
            Stmt::Switch {
                expr,
                cases,
                otherwise_body,
            } => {
                let switch_val = eval_with_io(expr, env, io)?;
                let mut matched = false;
                'switch_loop: for (case_exprs, case_body) in cases {
                    for case_expr in case_exprs {
                        let case_val = eval_with_io(case_expr, env, io)?;
                        // When the case expression is a Cell, check if switch_val
                        // matches any element of the cell (Phase 12.5c).
                        let is_match = if let Value::Cell(cell_elems) = &case_val {
                            cell_elems.iter().any(|elem| match (&switch_val, elem) {
                                (Value::Scalar(a), Value::Scalar(b)) => a == b,
                                _ => {
                                    let sv = match &switch_val {
                                        Value::Str(s) | Value::StringObj(s) => Some(s.as_str()),
                                        _ => None,
                                    };
                                    let cv = match elem {
                                        Value::Str(s) | Value::StringObj(s) => Some(s.as_str()),
                                        _ => None,
                                    };
                                    matches!((sv, cv), (Some(a), Some(b)) if a == b)
                                }
                            })
                        } else {
                            match (&switch_val, &case_val) {
                                (Value::Scalar(a), Value::Scalar(b)) => a == b,
                                _ => {
                                    let sv = match &switch_val {
                                        Value::Str(s) | Value::StringObj(s) => Some(s.as_str()),
                                        _ => None,
                                    };
                                    let cv = match &case_val {
                                        Value::Str(s) | Value::StringObj(s) => Some(s.as_str()),
                                        _ => None,
                                    };
                                    matches!((sv, cv), (Some(a), Some(b)) if a == b)
                                }
                            }
                        };
                        if is_match {
                            if let Some(sig) = exec_stmts(case_body, env, io, fmt, base, compact)? {
                                return Ok(Some(sig));
                            }
                            matched = true;
                            break 'switch_loop;
                        }
                    }
                }
                if !matched
                    && let Some(ob) = otherwise_body
                    && let Some(sig) = exec_stmts(ob, env, io, fmt, base, compact)?
                {
                    return Ok(Some(sig));
                }
            }

            // ── do...until ───────────────────────────────────────────────────
            Stmt::DoUntil { body, cond } => loop {
                match exec_stmts(body, env, io, fmt, base, compact)? {
                    Some(Signal::Break) => break,
                    Some(Signal::Continue) | None => {}
                    Some(Signal::Return) => return Ok(Some(Signal::Return)),
                }
                if is_truthy(&eval_with_io(cond, env, io)?) {
                    break;
                }
            },

            // ── try / catch / end ────────────────────────────────────────────
            Stmt::TryCatch {
                try_body,
                catch_var,
                catch_body,
            } => match exec_stmts(try_body, env, io, fmt, base, compact) {
                Ok(None) => {}
                Ok(Some(sig)) => return Ok(Some(sig)),
                Err(msg) => {
                    set_last_err(&msg);
                    if let Some(var) = catch_var {
                        let mut map = IndexMap::new();
                        map.insert("message".to_string(), Value::Str(msg));
                        env.insert(var.clone(), Value::Struct(map));
                    }
                    if let Some(sig) = exec_stmts(catch_body, env, io, fmt, base, compact)? {
                        return Ok(Some(sig));
                    }
                }
            },

            // ── function definition ──────────────────────────────────────────
            Stmt::FunctionDef {
                name,
                outputs,
                params,
                body_source,
                doc,
            } => {
                env.insert(
                    name.clone(),
                    Value::Function {
                        outputs: outputs.clone(),
                        params: params.clone(),
                        body_source: body_source.clone(),
                        locals: IndexMap::new(),
                        doc: doc.clone(),
                    },
                );
            }

            // ── return ───────────────────────────────────────────────────────
            Stmt::Return => return Ok(Some(Signal::Return)),

            // ── cell element assignment ──────────────────────────────────────
            Stmt::CellSet(cell_name, idx_expr, val_expr) => {
                // Inject `end` so that c{end+1} works (end = current cell length).
                let cell_len = match env.get(cell_name) {
                    Some(Value::Cell(v)) => v.len(),
                    _ => 0,
                };
                let env_end = write_env_with_end(env, cell_len);
                let idx = eval_with_io(idx_expr, &env_end, io)?;
                let rhs = eval_with_io(val_expr, env, io)?;
                let i = match idx {
                    Value::Scalar(n) => n as isize,
                    _ => return Err(format!("{cell_name}{{}}: index must be a scalar integer")),
                };
                match env.get_mut(cell_name) {
                    Some(Value::Cell(v)) => {
                        if i < 1 {
                            return Err(format!(
                                "{cell_name}{{}}: index {i} out of range (1..{})",
                                v.len()
                            ));
                        }
                        let idx = (i - 1) as usize;
                        // Grow the cell if needed (MATLAB semantics: assigning beyond end grows it)
                        if idx >= v.len() {
                            v.resize(idx + 1, Value::Scalar(0.0));
                        }
                        v[idx] = rhs.clone();
                    }
                    Some(_) => {
                        return Err(format!(
                            "'{cell_name}' is not a cell array; use () for regular indexing"
                        ));
                    }
                    None => {
                        // Auto-create cell if not defined (MATLAB semantics)
                        if i < 1 {
                            return Err(format!("{cell_name}{{}}: index {i} must be >= 1"));
                        }
                        let idx = (i - 1) as usize;
                        let mut v = vec![Value::Scalar(0.0); idx + 1];
                        v[idx] = rhs.clone();
                        env.insert(cell_name.clone(), Value::Cell(v));
                    }
                }
                if !silent && let Some(val) = env.get(cell_name) {
                    print_value(Some(cell_name), val, fmt, base, compact);
                }
            }

            // ── struct field assignment ──────────────────────────────────────
            Stmt::FieldSet(base_name, path, rhs_expr) => {
                let rhs = eval_with_io(rhs_expr, env, io)?;
                let root = match env.remove(base_name) {
                    Some(Value::Struct(m)) => m,
                    None => IndexMap::new(),
                    Some(other) => {
                        env.insert(base_name.clone(), other);
                        return Err(format!("'{base_name}' is not a struct"));
                    }
                };
                let updated = set_nested(root, path, rhs)?;
                let struct_val = Value::Struct(updated);
                if !silent {
                    print_value(Some(base_name), &struct_val, fmt, base, compact);
                }
                env.insert(base_name.clone(), struct_val);
            }

            // ── struct array element field assignment ────────────────────────
            Stmt::StructArrayFieldSet(base_name, idx_expr, path, rhs_expr) => {
                let rhs = eval_with_io(rhs_expr, env, io)?;
                let idx_val = eval_with_io(idx_expr, env, io)?;
                let idx = match &idx_val {
                    Value::Scalar(n) => {
                        let i = *n as isize;
                        if i < 1 {
                            return Err(format!(
                                "Struct array index must be a positive integer, got {n}"
                            ));
                        }
                        i as usize
                    }
                    _ => return Err("Struct array index must be a scalar integer".to_string()),
                };
                // Load or create the struct array
                let mut arr: Vec<IndexMap<String, Value>> = match env.remove(base_name) {
                    Some(Value::StructArray(v)) => v,
                    // A scalar struct with no index yet — promote to 1-element array
                    Some(Value::Struct(m)) => vec![m],
                    None => Vec::new(),
                    Some(other) => {
                        env.insert(base_name.clone(), other);
                        return Err(format!("'{base_name}' is not a struct array"));
                    }
                };
                // Grow the array if needed (fill with empty structs)
                while arr.len() < idx {
                    arr.push(IndexMap::new());
                }
                // Set the field(s) on element idx (1-based → 0-based)
                let elem = arr[idx - 1].clone();
                let updated_elem = set_nested(elem, path, rhs)?;
                arr[idx - 1] = updated_elem;
                let arr_val = Value::StructArray(arr);
                if !silent {
                    print_value(Some(base_name), &arr_val, fmt, base, compact);
                }
                env.insert(base_name.clone(), arr_val);
            }

            // ── indexed assignment ────────────────────────────────────────────
            Stmt::IndexSet {
                name,
                indices,
                value,
            } => {
                let rhs = eval_with_io(value, env, io)?;
                // For persistent vars: refresh from the store before applying a partial
                // update so that values written by recursive calls are not overwritten.
                if is_persistent(name) {
                    let func = current_func_name();
                    if let Some(fresh) = persistent_load(&func, name) {
                        env.insert(name.clone(), fresh);
                    }
                }
                exec_index_set(name, indices, rhs, env, io)?;
                // Write-through: persist immediately so recursive callers see the update.
                if is_persistent(name)
                    && let Some(val) = env.get(name)
                {
                    persistent_save(&current_func_name(), name, val.clone());
                }
                if !silent && let Some(val) = env.get(name) {
                    print_value(Some(name), val, fmt, base, compact);
                }
            }

            // ── multi-assign ─────────────────────────────────────────────────
            Stmt::MultiAssign { targets, expr } => {
                set_nargout(targets.len());
                let val = eval_with_io(expr, env, io)?;
                let vals: Vec<Value> = match val {
                    Value::Tuple(v) => v,
                    other => vec![other],
                };
                for (i, target) in targets.iter().enumerate() {
                    if target == "~" {
                        continue; // discard output
                    }
                    let v = vals.get(i).cloned().unwrap_or(Value::Void);
                    env.insert(target.clone(), v.clone());
                    if !silent && !matches!(v, Value::Void) {
                        print_value(Some(target), &v, fmt, base, compact);
                    }
                }
            }
        }
    }
    // After all statements, refresh global vars in env from the global store.
    // This ensures that modifications made inside called functions (which write
    // to the global store) are visible in the current scope's environment.
    global_refresh_into_env(env);
    Ok(None)
}

// ── indexed assignment helper ──────────────────────────────────────────────

/// Resolved index positions (0-based) along one matrix dimension.
enum WriteIdx {
    /// `:` — all positions in the current dimension.
    All,
    /// Explicit 0-based positions (may exceed current size → grow).
    Positions(Vec<usize>),
}

/// Resolves one index expression to a `WriteIdx`.
///
/// Unlike the read-path `resolve_dim`, bounds checking is skipped so that
/// out-of-range indices can trigger array growth.  Logical-mask detection
/// (all-0/1 vector whose length equals `dim_size`) is supported.
fn resolve_write_dim(
    expr: &crate::eval::Expr,
    dim_size: usize,
    env: &Env,
    io: &mut IoContext,
) -> Result<WriteIdx, String> {
    if matches!(expr, crate::eval::Expr::Colon) {
        return Ok(WriteIdx::All);
    }
    let val = eval_with_io(expr, env, io)?;
    let floats: Vec<f64> = match val {
        Value::Scalar(n) => vec![n],
        Value::Complex(re, im) => {
            if im != 0.0 {
                return Err("Index must be real, not complex".to_string());
            }
            vec![re]
        }
        Value::Matrix(m) => {
            let total = m.nrows() * m.ncols();
            if m.nrows() > 1 && m.ncols() > 1 && total != dim_size {
                return Err("Index must be a scalar or vector, not a 2-D matrix".to_string());
            }
            // Collect in column-major order so mask positions align with linear indexing.
            if m.nrows() > 1 && m.ncols() > 1 {
                let mut v = Vec::with_capacity(total);
                for col in 0..m.ncols() {
                    for row in 0..m.nrows() {
                        v.push(m[[row, col]]);
                    }
                }
                v
            } else {
                m.iter().copied().collect()
            }
        }
        _ => return Err("Index must be numeric".to_string()),
    };
    // Logical mask: a 0/1 array whose element count matches dim_size.
    if dim_size > 0 && floats.len() == dim_size && floats.iter().all(|&f| f == 0.0 || f == 1.0) {
        let positions: Vec<usize> = floats
            .iter()
            .enumerate()
            .filter(|&(_, &f)| f == 1.0)
            .map(|(i, _)| i)
            .collect();
        return Ok(WriteIdx::Positions(positions));
    }
    // Numeric 1-based indices → 0-based (no upper-bound check — growth is handled by caller).
    let positions: Result<Vec<usize>, String> = floats
        .iter()
        .map(|&n| {
            let i = n.round() as i64;
            if i < 1 {
                return Err(format!("Index {i} must be >= 1"));
            }
            Ok(i as usize - 1)
        })
        .collect();
    Ok(WriteIdx::Positions(positions?))
}

/// Returns a clone of `env` with `end` set to `dim_size` as a `Value::Scalar`.
fn write_env_with_end(env: &Env, dim_size: usize) -> Env {
    let mut e = env.clone();
    e.insert("end".to_string(), Value::Scalar(dim_size as f64));
    e
}

/// Writes `rhs` into `name(indices...)` in `env`, growing the matrix if necessary.
///
/// Supports:
/// - 1 index: linear (column-major) indexing; grows row vectors / creates new ones.
/// - 2 indices: 2-D row/column indexing; grows the matrix in either dimension.
/// - Scalar RHS is broadcast to all selected positions.
/// - Logical mask indices (Phase 15d).
fn exec_index_set(
    name: &str,
    indices: &[crate::eval::Expr],
    rhs: Value,
    env: &mut Env,
    io: &mut IoContext,
) -> Result<(), String> {
    // Represent target variable as a matrix (scalar → 1×1, empty var → 0×0).
    let (mut mat, was_scalar) = match env.get(name) {
        Some(Value::Matrix(m)) => (m.clone(), false),
        Some(Value::Scalar(n)) => (Array2::from_elem((1, 1), *n), true),
        None | Some(Value::Void) => (Array2::zeros((0, 0)), false),
        Some(_) => {
            return Err(format!(
                "'{name}' is not a matrix; cannot use () indexed assignment"
            ));
        }
    };

    match indices.len() {
        1 => {
            let total = mat.nrows() * mat.ncols();
            let env_end = write_env_with_end(env, total);
            let widx = resolve_write_dim(&indices[0], total, &env_end, io)?;

            // Determine which 0-based positions to write.
            let positions: Vec<usize> = match widx {
                WriteIdx::All => (0..total).collect(),
                WriteIdx::Positions(p) => p,
            };

            // Determine the RHS values to write (scalar broadcasts).
            let rhs_vals: Vec<f64> = match &rhs {
                Value::Scalar(n) => vec![*n; positions.len()],
                Value::Matrix(m) => {
                    let flat: Vec<f64> = m.iter().copied().collect();
                    if flat.len() != positions.len() {
                        return Err(format!(
                            "Assignment dimension mismatch: {} positions but {} values",
                            positions.len(),
                            flat.len()
                        ));
                    }
                    flat
                }
                _ => {
                    return Err(
                        "Indexed assignment: RHS must be a numeric scalar or matrix".to_string()
                    );
                }
            };

            // Find the required flat size after growth.
            let required = positions.iter().copied().max().map(|m| m + 1).unwrap_or(0);
            let required = required.max(total);

            // Determine output shape: keep original orientation when growing a vector.
            let (out_rows, out_cols) = if mat.nrows() == 0 || mat.ncols() == 0 {
                // Empty → default to row vector.
                (1, required)
            } else if mat.nrows() == 1 {
                (1, required)
            } else if mat.ncols() == 1 {
                (required, 1)
            } else if required > total {
                return Err("Cannot grow a 2-D matrix with linear indexing".to_string());
            } else {
                (mat.nrows(), mat.ncols())
            };

            // Grow matrix if needed, preserving existing data at correct column-major positions.
            if required > total || out_rows != mat.nrows() || out_cols != mat.ncols() {
                let mut new_mat = Array2::<f64>::zeros((out_rows, out_cols));
                for old_p in 0..total {
                    let old_row = old_p % mat.nrows().max(1);
                    let old_col = old_p / mat.nrows().max(1);
                    let new_row = old_p % out_rows;
                    let new_col = old_p / out_rows;
                    if old_row < mat.nrows() && old_col < mat.ncols() {
                        new_mat[[new_row, new_col]] = mat[[old_row, old_col]];
                    }
                }
                mat = new_mat;
            }

            // Write values at column-major positions directly.
            for (&pos, &val) in positions.iter().zip(rhs_vals.iter()) {
                let row = pos % mat.nrows();
                let col = pos / mat.nrows();
                mat[[row, col]] = val;
            }
        }
        2 => {
            let nrows = mat.nrows();
            let ncols = mat.ncols();
            let env_r = write_env_with_end(env, nrows);
            let env_c = write_env_with_end(env, ncols);
            let ridx = resolve_write_dim(&indices[0], nrows, &env_r, io)?;
            let cidx = resolve_write_dim(&indices[1], ncols, &env_c, io)?;

            let rows: Vec<usize> = match ridx {
                WriteIdx::All => (0..nrows.max(1)).collect(),
                WriteIdx::Positions(p) => p,
            };
            let cols: Vec<usize> = match cidx {
                WriteIdx::All => (0..ncols.max(1)).collect(),
                WriteIdx::Positions(p) => p,
            };

            let req_rows = rows
                .iter()
                .copied()
                .max()
                .map(|m| m + 1)
                .unwrap_or(0)
                .max(nrows);
            let req_cols = cols
                .iter()
                .copied()
                .max()
                .map(|m| m + 1)
                .unwrap_or(0)
                .max(ncols);

            // Grow matrix if needed (fill new cells with 0).
            if req_rows != nrows || req_cols != ncols {
                let mut new_mat = Array2::<f64>::zeros((req_rows, req_cols));
                for r in 0..nrows {
                    for c in 0..ncols {
                        new_mat[[r, c]] = mat[[r, c]];
                    }
                }
                mat = new_mat;
            }

            // Collect RHS values.
            let n_sel = rows.len() * cols.len();
            let rhs_vals: Vec<f64> = match &rhs {
                Value::Scalar(n) => vec![*n; n_sel],
                Value::Matrix(m) => {
                    let flat: Vec<f64> = m.iter().copied().collect();
                    if flat.len() != n_sel {
                        return Err(format!(
                            "Assignment dimension mismatch: {}×{} = {} positions but {} values",
                            rows.len(),
                            cols.len(),
                            n_sel,
                            flat.len()
                        ));
                    }
                    flat
                }
                _ => {
                    return Err(
                        "Indexed assignment: RHS must be a numeric scalar or matrix".to_string()
                    );
                }
            };

            // Write values row-major over selected (row, col) pairs.
            let mut k = 0;
            for &r in &rows {
                for &c in &cols {
                    mat[[r, c]] = rhs_vals[k];
                    k += 1;
                }
            }
        }
        _ => return Err("Indexed assignment supports at most 2 indices".to_string()),
    }

    // Collapse a 1×1 matrix to a scalar.
    let result = if mat.nrows() == 1 && mat.ncols() == 1 {
        Value::Scalar(mat[[0, 0]])
    } else {
        Value::Matrix(mat)
    };
    let _ = was_scalar;
    env.insert(name.to_string(), result);
    Ok(())
}