calcit 0.12.32

Interpreter and js codegen for Calcit
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
mod args;
mod deps;
pub mod gen_stack;
mod helpers;
mod internal_states;
mod paths;
mod runtime;
use std::fmt::Write;
mod snippets;
mod symbols;
mod tags;

use im_ternary_tree::TernaryTreeList;

use std::cell::{Cell, RefCell};
use std::collections::HashSet;
use std::fs;
use std::path::Path;
use std::sync::Arc;

use cirru_edn::EdnTag;

use crate::builtins::meta::{js_gensym, reset_js_gensym_index};
use crate::builtins::syntax::get_raw_args_fn;
use crate::builtins::{is_js_syntax_procs, is_proc_name};
use crate::calcit::{self, CalcitArgLabel, CalcitFnArgs, CalcitImport, CalcitList, CalcitLocal, CalcitProc, MethodKind};
use crate::calcit::{Calcit, CalcitSyntax, ImportInfo};
use crate::call_stack::StackKind;
use crate::codegen::skip_arity_check;
use crate::program;
use crate::util::string::{has_ns_part, matches_js_var, wrap_js_str};
use args::{gen_args_code, gen_call_args_with_temps};
use deps::{contains_symbol, sort_compiled_defs_by_deps};
use helpers::{cirru_to_js, is_js_unavailable_procs, write_file_if_changed};
use paths::{to_js_import_name, to_mjs_filename};
use runtime::{get_proc_prefix, is_cirru_string};
use symbols::{escape_cirru_str, escape_var};

pub fn escape_symbol_for_js(name: &str) -> String {
  escape_var(name)
}

pub fn unescape_symbol_from_js(name: &str) -> String {
  symbols::unescape_var(name)
}

thread_local! {
  static INLINE_ALL_ARGS: Cell<bool> = const { Cell::new(false) };
}

struct ImportsDict(HashSet<CalcitImport>);

impl ImportsDict {
  fn new() -> Self {
    ImportsDict(HashSet::new())
  }

  fn insert(&mut self, item: CalcitImport) {
    // println!("insert import: {:?}", item);
    self.0.insert(item);
  }

  fn is_empty(&self) -> bool {
    self.0.is_empty()
  }
}

fn escape_ns(name: &str) -> String {
  // use `$` to tell namespace from normal variables, thus able to use same token like clj
  let piece = if is_cirru_string(name) {
    name[1..].replace('@', "_AT_").replace('/', "_SLSH_").replace('.', "_DOT_") // TODO
  } else {
    name.to_owned()
  };
  format!("${}", escape_var(&piece))
}

// code generated from calcit.core.cirru may not be faster enough,
// possible way to use code from calcit.procs.ts
fn is_preferred_js_proc(name: &str) -> bool {
  matches!(
    name,
    "number?"
      | "tag?"
      | "map?"
      | "nil?"
      | "list?"
      | "set?"
      | "string?"
      | "fn?"
      | "bool?"
      | "ref?"
      | "record?"
      | "tuple?"
      | "starts-with?"
      | "ends-with?"
  )
}

fn is_quote_head(value: &Calcit) -> bool {
  matches!(value, Calcit::Syntax(CalcitSyntax::Quote, _))
    || matches!(value, Calcit::Symbol { sym, .. } if sym.as_ref() == "quote")
    || matches!(value, Calcit::Import(CalcitImport { ns, def, .. }) if &**ns == calcit::CORE_NS && &**def == "quote")
}

fn is_runtime_placeholder_form(value: &Calcit) -> bool {
  matches!(value, Calcit::Symbol { sym, .. } if sym.as_ref() == "&runtime-implementation")
}

fn is_runtime_placeholder_quote(value: &Calcit) -> bool {
  let Calcit::List(items) = value else {
    return false;
  };
  items.len() == 2 && items.first().is_some_and(is_quote_head) && items.get(1).is_some_and(is_runtime_placeholder_form)
}

fn should_skip_core_def_codegen(def: &str, compiled_def: &program::CompiledDef) -> bool {
  if CalcitSyntax::is_valid(def) || is_proc_name(def) || is_js_syntax_procs(def) {
    return true;
  }

  compiled_def.source_code.as_ref().is_some_and(is_runtime_placeholder_quote)
}

fn quote_to_js(xs: &Calcit, var_prefix: &str, tags: &RefCell<HashSet<EdnTag>>) -> Result<String, String> {
  match xs {
    Calcit::Symbol { sym, .. } => Ok(format!("new {var_prefix}CalcitSymbol({})", escape_cirru_str(sym))),
    Calcit::Str(s) => Ok(escape_cirru_str(s)),
    Calcit::Bool(b) => Ok(b.to_string()),
    Calcit::Number(n) => Ok(n.to_string()),
    Calcit::Nil => Ok(String::from("null")),
    // mainly for methods, which are recognized during reading
    Calcit::Proc(p) => Ok(format!("new {var_prefix}CalcitSymbol({})", escape_cirru_str(p.as_ref()))),
    Calcit::List(ys) => {
      let mut chunk = String::from("");
      ys.traverse_result::<String>(&mut |y| {
        if !chunk.is_empty() {
          chunk.push_str(", ");
        }
        chunk.push_str(&quote_to_js(y, var_prefix, tags)?);
        Ok(())
      })?;
      Ok(format!("new {var_prefix}CalcitSliceList([{chunk}])"))
    }
    Calcit::Tag(s) => {
      let mut tags = tags.borrow_mut();
      tags.insert(s.to_owned());
      Ok(tags::tag_access(s.ref_str()))
    }
    Calcit::CirruQuote(code) => Ok(format!("new {var_prefix}CalcitCirruQuote({})", cirru_to_js(code)?)),
    Calcit::Method(name, kind) => {
      let code = match kind {
        MethodKind::Access => ".-",
        MethodKind::InvokeNative => ".!",
        MethodKind::Invoke(_) => ".",
        MethodKind::TagAccess => ".:",
        MethodKind::AccessOptional => ".?-",
        MethodKind::InvokeNativeOptional => ".?!",
      };
      Ok(format!("new {var_prefix}CalcitSymbol(\"{code}{}\")", name.escape_default()))
    }
    Calcit::Syntax(s, _) => Ok(format!("new {var_prefix}CalcitSymbol('{}')", s.to_string().escape_default())),
    _ => unreachable!("Unexpected data in quote for js: {}", xs),
  }
}

fn make_let_with_bind(left: &str, right: &str, body: &str, has_await: bool) -> String {
  let (await_mark, async_mark) = if has_await { ("await ", "async ") } else { ("", "") };
  let body = indent_block(body, "  ");
  format!("{await_mark}({async_mark}function __bind__({left}){{\n{body}\n}})({right})")
}

fn make_let_with_wrapper(left: &str, right: &str, body: &str, has_await: bool) -> String {
  let (await_mark, async_mark) = if has_await { ("await ", "async ") } else { ("", "") };
  let body = indent_block(&format!("let {left} = {right};\n{body}"), "  ");
  format!("{await_mark}({async_mark}function __let__(){{\n{body}\n}})()")
}

fn make_fn_wrapper(body: &str, is_async: bool) -> String {
  let body = indent_block(body, "  ");
  if is_async {
    format!("await (async function _async_fn_(){{\n{body}\n}})()")
  } else {
    format!("(function _fn_(){{\n{body}\n}})()")
  }
}

fn indent_block(body: &str, indent: &str) -> String {
  body
    .lines()
    .map(|line| {
      if line.trim().is_empty() {
        String::from("")
      } else {
        format!("{indent}{line}")
      }
    })
    .collect::<Vec<_>>()
    .join("\n")
}

fn raw_syntax_codegen_error(syntax: &CalcitSyntax) -> String {
  format!(
    "invalid JS codegen: raw syntax node `{syntax}` cannot be emitted as a standalone JS value. LLM hint: special forms must start an expression, for example `(if cond a b)`, or appear at the beginning of a line / after `$`, instead of being left as a separate argument node."
  )
}

fn to_js_code(
  xs: &Calcit,
  ns: &str,
  local_defs: &HashSet<Arc<str>>,
  file_imports: &RefCell<ImportsDict>,
  tags: &RefCell<HashSet<EdnTag>>,
  return_label: Option<&str>,
) -> Result<String, String> {
  // println!("to js code handle: {} {:?}", xs, xs);
  if let Calcit::List(ys) = xs {
    gen_call_code(ys, ns, local_defs, xs, file_imports, tags, return_label)
  } else {
    let ret = match xs {
      Calcit::Symbol { sym, info, .. } => {
        let passed_defs = PassedDefs {
          ns,
          local_defs,
          file_imports,
        };

        gen_symbol_code(sym, &info.at_ns, &info.at_def, xs, &passed_defs)
      }
      Calcit::Import(item @ CalcitImport { def, info, .. }) => {
        match &**info {
          ImportInfo::Core { at_ns } => {
            if &**at_ns == calcit::CORE_NS {
              // functions under core uses built $clt module entry
              Ok(escape_var(def))
            } else {
              Ok(format!("$clt.{}", escape_var(def)))
            }
          }
          ImportInfo::NsAs { .. } => {
            file_imports.borrow_mut().insert(item.to_owned());
            Ok(format!("{}.{}", escape_ns(&item.ns), escape_var(def)))
          }
          ImportInfo::JsDefault { alias, .. } => {
            // println!("Js Default: {:?}", info);
            file_imports.borrow_mut().insert(item.to_owned());
            Ok(escape_var(alias))
          }
          _ => {
            file_imports.borrow_mut().insert(item.to_owned());
            Ok(escape_var(def))
          }
        }
      }
      Calcit::Local(CalcitLocal { sym, .. }) => Ok(escape_var(sym)),
      Calcit::Proc(s) => {
        let proc_prefix = get_proc_prefix(ns);
        // println!("gen proc {} under {}", s, ns,);
        // let resolved = Some(ResolvedDef(String::from(primes::CORE_NS), s.to_owned()));
        // gen_symbol_code(s, primes::CORE_NS, &resolved, ns, xs, local_defs)
        Ok(format!("{proc_prefix}{}", escape_var(s.as_ref())))
      }
      Calcit::Registered(alias) => {
        let proc_prefix = get_proc_prefix(ns);
        Ok(format!("{proc_prefix}{}", escape_var(alias)))
      }
      Calcit::Method(name, kind) => {
        let proc_prefix = get_proc_prefix(ns);
        if matches!(kind, MethodKind::Invoke(_)) {
          Ok(format!("{proc_prefix}invoke_method_closure({})", escape_cirru_str(name)))
        } else {
          Err(format!("Does not expect native method as closure: {kind}"))
        }
      }
      Calcit::Fn { info, .. } => {
        let passed_defs = PassedDefs {
          ns,
          local_defs,
          file_imports,
        };
        if let Some(def_ref) = info.def_ref.as_ref() {
          let is_local_def = passed_defs.local_defs.contains(&def_ref.def_name);
          let has_top_level_def = program::has_def_code(def_ref.def_ns.as_ref(), def_ref.def_name.as_ref());
          if def_ref.is_macro_gen || (!is_local_def && !has_top_level_def) {
            return Err(format!(
              "cannot emit JS for function literal without resolvable def: {}/{} (used_in_impl: {})",
              info.def_ns, info.name, info.usage.used_in_impl
            ));
          }
          return gen_symbol_code(
            def_ref.def_name.as_ref(),
            def_ref.def_ns.as_ref(),
            def_ref.def_name.as_ref(),
            xs,
            &passed_defs,
          );
        }
        Err(format!(
          "cannot emit JS for function literal without def reference: {}/{} (used_in_impl: {})",
          info.def_ns, info.name, info.usage.used_in_impl
        ))
      }
      Calcit::Syntax(s, ..) => Err(raw_syntax_codegen_error(s)),
      Calcit::Str(s) => Ok(escape_cirru_str(s)),
      Calcit::Bool(b) => Ok(b.to_string()),
      Calcit::Number(n) => Ok(n.to_string()),
      Calcit::Nil => Ok(String::from("null")),
      Calcit::Tag(s) => {
        let mut tags = tags.borrow_mut();
        tags.insert(s.to_owned());
        Ok(tags::tag_access(s.ref_str()))
      }
      Calcit::List(_) => unreachable!("[Error] list handled in another branch"),
      Calcit::CirruQuote(code) => {
        let proc_prefix = get_proc_prefix(ns);
        Ok(format!("new {proc_prefix}CalcitCirruQuote({})", cirru_to_js(code)?))
      }
      Calcit::RawCode(_, code) => Ok((**code).to_owned()),
      a => unreachable!("[Error] unknown kind to gen js code: {}", a),
    };

    match (return_label, &ret) {
      (Some(label), Ok(code)) => Ok(format!("{label}{code}")),
      (_, _) => ret,
    }
  }
}

fn to_js_code_inline(
  xs: &Calcit,
  ns: &str,
  local_defs: &HashSet<Arc<str>>,
  file_imports: &RefCell<ImportsDict>,
  tags: &RefCell<HashSet<EdnTag>>,
  return_label: Option<&str>,
) -> Result<String, String> {
  INLINE_ALL_ARGS.with(|flag| {
    let previous = flag.replace(true);
    let result = to_js_code(xs, ns, local_defs, file_imports, tags, return_label);
    flag.set(previous);
    result
  })
}

fn gen_call_code(
  ys: &CalcitList,
  ns: &str,
  local_defs: &HashSet<Arc<str>>,
  xs: &Calcit,
  file_imports: &RefCell<ImportsDict>,
  tags: &RefCell<HashSet<EdnTag>>,
  return_label: Option<&str>,
) -> Result<String, String> {
  let return_code = return_label.unwrap_or("");
  let var_prefix = if ns == calcit::CORE_NS { "" } else { "$clt." };
  let proc_prefix = get_proc_prefix(ns);
  let inline_all = INLINE_ALL_ARGS.with(|flag| flag.get());
  if ys.is_empty() {
    eprintln!("[Warn] Unexpected empty list inside {xs}");
    return Ok(String::from("()"));
  }

  let head = ys[0].to_owned();
  let body = ys.drop_left();
  match &head {
    Calcit::Syntax(s, ..) => {
      match &s {
        CalcitSyntax::If => gen_if_code(&body, local_defs, xs, ns, file_imports, tags, return_label),
        CalcitSyntax::CoreLet => gen_let_code(&body, local_defs, xs, ns, file_imports, tags, return_label),

        CalcitSyntax::Quote => match body.first() {
          Some(item) => quote_to_js(item, var_prefix, tags),
          None => Err(format!("quote expected a node, got nothing from {body}")),
        },
        CalcitSyntax::Defatom => match (body.first(), body.get(1)) {
          _ if body.len() > 2 => Err(format!("defatom expected name and value, got too many: {body}")),
          (Some(Calcit::Symbol { sym, .. }), Some(v)) | (Some(Calcit::Import(CalcitImport { def: sym, .. })), Some(v)) => {
            let ref_path = wrap_js_str(&format!("{ns}/{sym}"));
            gen_stack::push_call_stack(ns, sym, StackKind::Codegen, xs.to_owned(), &[]);
            let value_code = &to_js_code(v, ns, local_defs, file_imports, tags, None)?;
            gen_stack::pop_call_stack();
            Ok(format!(
              "\n({}peekDefatom({}) ?? {}defatom({}, {value_code}))\n",
              &var_prefix, &ref_path, &var_prefix, &ref_path
            ))
          }
          (_, _) => Err(format!("defatom expected name and value, got: {body}")),
        },

        CalcitSyntax::Defn => match (body.first(), body.get(1)) {
          (Some(Calcit::Symbol { sym, .. }), Some(Calcit::List(ys))) => {
            let func_body = body.skip(2)?;
            gen_stack::push_call_stack(ns, sym, StackKind::Codegen, xs.to_owned(), &[]);
            let passed_defs = PassedDefs {
              ns,
              local_defs,
              file_imports,
            };
            let ret = gen_js_func(sym, &get_raw_args_fn(ys)?, &func_body.to_vec(), &passed_defs, false, tags, ns);
            gen_stack::pop_call_stack();
            match ret {
              Ok(code) => Ok(format!("{return_code}{code}")),
              _ => ret,
            }
          }
          (_, _) => Err(format!("defn expected name arguments, got: {}", Calcit::from(body))),
        },
        CalcitSyntax::Try => match (body.first(), body.get(1)) {
          (Some(expr), Some(handler)) => {
            gen_stack::push_call_stack(ns, "try", StackKind::Codegen, xs.to_owned(), &[]);
            let next_return_label = return_label.unwrap_or("return ");
            let try_code = to_js_code(expr, ns, local_defs, file_imports, tags, Some(next_return_label))?;
            let err_var = js_gensym("errMsg");
            let handler = to_js_code(handler, ns, local_defs, file_imports, tags, None)?;

            gen_stack::pop_call_stack();
            let code = snippets::tmpl_try(err_var, try_code, handler, next_return_label);
            match return_label {
              Some(_) => Ok(code),
              None => Ok(snippets::tmpl_fn_wrapper(code)),
            }
          }
          (_, _) => Err(format!("try expected 2 nodes, got: {body}")),
        },
        CalcitSyntax::Eval => {
          let (prelude, args_code) =
            gen_call_args_with_temps(&body, ns, local_defs, file_imports, tags, return_label.is_some(), inline_all)?;
          let call_code = format!("{proc_prefix}{}({args_code})", escape_var("eval"));
          Ok(wrap_call_with_prelude(prelude, call_code, return_label, detect_await(&body)))
        }
        CalcitSyntax::Reset => {
          let (prelude, args_code) =
            gen_call_args_with_temps(&body, ns, local_defs, file_imports, tags, return_label.is_some(), inline_all)?;
          let call_code = format!("{proc_prefix}{}({args_code})", escape_var("reset!"));
          Ok(wrap_call_with_prelude(prelude, call_code, return_label, detect_await(&body)))
        }
        // for `&call-spread`, just translate as normal call
        CalcitSyntax::CallSpread => gen_call_code(&body, ns, local_defs, xs, file_imports, tags, return_label),
        CalcitSyntax::HintFn => Ok(format!("{return_code}null")),
        CalcitSyntax::AssertType => Ok(format!("{return_code}null")),
        CalcitSyntax::AssertTraits => Ok(format!("{return_code}null")),
        CalcitSyntax::Match => gen_match_code(&body, local_defs, xs, ns, file_imports, tags, return_label),
        _ => {
          let (prelude, args_code) =
            gen_call_args_with_temps(&body, ns, local_defs, file_imports, tags, return_label.is_some(), inline_all)?;
          let call_code = format!("{}({})", to_js_code(&head, ns, local_defs, file_imports, tags, None)?, args_code);
          Ok(wrap_call_with_prelude(prelude, call_code, return_label, detect_await(&body)))
        }
      }
    }
    Calcit::Proc(CalcitProc::Raise) => {
      // not core syntax, but treat as macro for better debugging experience
      match body.first() {
        Some(m) => {
          let message: String = to_js_code(m, ns, local_defs, file_imports, tags, None)?;
          let has_await = detect_await(&body);
          let data_code = match body.get(1) {
            Some(d) => to_js_code(d, ns, local_defs, file_imports, tags, None)?,
            None => String::from("null"),
          };
          let err_var = js_gensym("err");
          let ret = format!("let {err_var} = new Error({message});\n {err_var}.data = {data_code};\n throw {err_var};");
          // println!("inside raise: {:?} {}", return_label, xs);
          match return_label {
            Some(_) => Ok(ret),
            _ => Ok(make_fn_wrapper(&ret, has_await)),
          }
        }
        None => Err(format!("raise expected 1~2 arguments, got: {body}")),
      }
    }
    // deftype-slot and bind-type are preprocessing-only; they have no JS runtime effect.
    Calcit::Proc(CalcitProc::DeftypeSlot) | Calcit::Proc(CalcitProc::BindType) => Ok(format!("{return_code}null")),
    // &record:nth: with 3 args (record, idx, :field-tag), use record.get(tag) for JS
    // because JS CalcitRecord fields are sorted by tag.idx (registration order), not alphabetically.
    // With 2 args (record, idx), fall back to record.values[idx] (only valid when index matches).
    Calcit::Proc(CalcitProc::NativeRecordNth) => {
      if body.len() == 3 {
        let record_code = to_js_code(&body[0], ns, local_defs, file_imports, tags, None)?;
        let tag_code = to_js_code(&body[2], ns, local_defs, file_imports, tags, None)?;
        Ok(format!("{return_code}{record_code}.get({tag_code})"))
      } else if body.len() == 2 {
        let record_code = to_js_code(&body[0], ns, local_defs, file_imports, tags, None)?;
        let idx_code = to_js_code(&body[1], ns, local_defs, file_imports, tags, None)?;
        Ok(format!("{return_code}{record_code}.values[{idx_code}]"))
      } else {
        Err(format!("&record:nth expected 2-3 arguments, got: {body}"))
      }
    }
    // &record:assoc-at: optimized assoc with pre-resolved index.
    // JS uses record.assoc(tag, value) since JS field ordering differs from Rust.
    Calcit::Proc(CalcitProc::NativeRecordAssocAt) => {
      if body.len() == 4 {
        let record_code = to_js_code(&body[0], ns, local_defs, file_imports, tags, None)?;
        let tag_code = to_js_code(&body[2], ns, local_defs, file_imports, tags, None)?;
        let value_code = to_js_code(&body[3], ns, local_defs, file_imports, tags, None)?;
        Ok(format!("{return_code}{record_code}.assoc({tag_code}, {value_code})"))
      } else {
        Err(format!("&record:assoc-at expected 4 arguments, got: {body}"))
      }
    }
    // &record:with-at: optimized with pre-resolved indices.
    // JS ignores indices and calls the same _$n_record_$o_with(proto, tag, val, ...) function.
    Calcit::Proc(CalcitProc::NativeRecordWithAt) => {
      if body.len() >= 3 && (body.len() - 1) % 3 == 0 {
        let proc_prefix = get_proc_prefix(ns);
        let record_code = to_js_code(&body[0], ns, local_defs, file_imports, tags, None)?;
        let triple_count = (body.len() - 1) / 3;
        let mut all_args = vec![record_code];
        for i in 0..triple_count {
          let base = 1 + i * 3;
          // Skip index (body[base]), emit tag and value for JS
          let tag_code = to_js_code(&body[base + 1], ns, local_defs, file_imports, tags, None)?;
          let value_code = to_js_code(&body[base + 2], ns, local_defs, file_imports, tags, None)?;
          all_args.push(tag_code);
          all_args.push(value_code);
        }
        Ok(format!(
          "{return_code}{proc_prefix}{}({})",
          escape_var("&record:with"),
          all_args.join(", ")
        ))
      } else {
        Err(format!(
          "&record:with-at expected (record, idx, tag, val, ...) triples, got: {body}"
        ))
      }
    }
    Calcit::Proc(_) => {
      let (prelude, args_code) =
        gen_call_args_with_temps(&body, ns, local_defs, file_imports, tags, return_label.is_some(), inline_all)?;
      let call_code = format!("{}({})", to_js_code(&head, ns, local_defs, file_imports, tags, None)?, args_code);
      Ok(wrap_call_with_prelude(prelude, call_code, return_label, detect_await(&body)))
    }
    Calcit::Symbol { sym: s, .. } | Calcit::Registered(s) => {
      match &**s {
        ";" => Ok(format!("(/* {body} */ null)")),
        "hint-fn" => Ok(format!("{return_code}null")),

        "echo" | "println" => {
          // not core syntax, but treat as macro for better debugging experience
          let args = ys.drop_left();
          let args_code = gen_args_code(&args, ns, local_defs, file_imports, tags)?;
          Ok(format!("console.log({proc_prefix}printable({args_code}))"))
        }
        "eprintln" => {
          // not core syntax, but treat as macro for better debugging experience
          let args = ys.drop_left();
          let args_code = gen_args_code(&args, ns, local_defs, file_imports, tags)?;
          Ok(format!("console.error({proc_prefix}printable({args_code}))"))
        }
        "exists?" => {
          // not core syntax, but treat as macro for availability
          match body.first() {
            Some(Calcit::Symbol { .. }) | Some(Calcit::RawCode(..)) => {
              let target = to_js_code(&body[0], ns, local_defs, file_imports, tags, None)?; // TODO could be simpler
              Ok(format!("{return_code}(typeof {target} !== 'undefined')"))
            }
            Some(a) => Err(format!("exists? expected a symbol, got: {a}")),
            None => Err(format!("exists? expected 1 node, got: {body}")),
          }
        }
        "new" => match body.first() {
          Some(ctor) => {
            let args = body.drop_left();
            let (prelude, args_code) =
              gen_call_args_with_temps(&args, ns, local_defs, file_imports, tags, return_label.is_some(), inline_all)?;
            let call_code = format!("new {}({})", to_js_code(ctor, ns, local_defs, file_imports, tags, None)?, args_code);
            Ok(wrap_call_with_prelude(prelude, call_code, return_label, detect_await(&args)))
          }
          None => Err(format!("`new` expected constructor, got nothing, {xs}")),
        },
        "js-await" => match body.first() {
          Some(body) => Ok(format!(
            "{}(await {})",
            return_code,
            to_js_code(body, ns, local_defs, file_imports, tags, None)?
          )),
          None => Err(format!("`new` expected constructor, got nothing, {xs}")),
        },
        "instance?" => match (body.first(), body.get(1)) {
          (Some(ctor), Some(v)) => Ok(format!(
            "{}({} instanceof {})",
            return_code,
            to_js_code(v, ns, local_defs, file_imports, tags, None)?,
            to_js_code(ctor, ns, local_defs, file_imports, tags, None)?
          )),
          (_, _) => Err(format!("instance? expected 2 arguments, got: {body}")),
        },
        "set!" => match (body.first(), body.get(1)) {
          (Some(target), Some(v)) => Ok(format!(
            "{} = {}",
            to_js_code(target, ns, local_defs, file_imports, tags, None)?,
            to_js_code(v, ns, local_defs, file_imports, tags, None)?
          )),
          (_, _) => Err(format!("set! expected 2 nodes, got: {body}")),
        },
        "&raw-code" => match body.first() {
          Some(Calcit::Str(s)) => Ok(format!("{}{}", return_label.unwrap_or(""), s)),
          Some(a) => Err(format!("&raw-code expected a string, got: {a}")),
          None => Err(format!("&raw-code expected 1 node, got: {body}")),
        },
        _ => {
          // TODO
          let (prelude, args_code) =
            gen_call_args_with_temps(&body, ns, local_defs, file_imports, tags, return_label.is_some(), inline_all)?;
          let call_code = format!("{}({})", to_js_code(&head, ns, local_defs, file_imports, tags, None)?, args_code);
          Ok(wrap_call_with_prelude(prelude, call_code, return_label, detect_await(&body)))
        }
      }
    }
    Calcit::Method(name, kind) => match kind {
      MethodKind::Access => {
        if body.len() == 1 {
          let obj = to_js_code(&body[0], ns, local_defs, file_imports, tags, None)?;
          if matches_js_var(name) {
            Ok(format!("{return_code}{obj}.{name}"))
          } else {
            Ok(format!("{return_code}{obj}[{}]", escape_cirru_str(name)))
          }
        } else {
          Err(format!("accessor takes only 1 argument, {xs}"))
        }
      }
      MethodKind::AccessOptional => {
        if body.len() == 1 {
          let obj = to_js_code(&body[0], ns, local_defs, file_imports, tags, None)?;
          if matches_js_var(name) {
            Ok(format!("{return_code}{obj}?.{name}"))
          } else {
            Ok(format!("{return_code}{obj}?.[{}]", escape_cirru_str(name)))
          }
        } else {
          Err(format!("optional accessor takes only 1 argument, {xs}"))
        }
      }
      MethodKind::InvokeNative => {
        if !body.is_empty() {
          let obj = to_js_code(&body[0], ns, local_defs, file_imports, tags, None)?;
          let (prelude, args_code) = gen_call_args_with_temps(
            &body.skip(1).expect("get args"),
            ns,
            local_defs,
            file_imports,
            tags,
            return_label.is_some(),
            inline_all,
          )?;

          let caller = if matches_js_var(name) {
            format!("{obj}.{name}")
          } else {
            format!("{obj}[{}]", escape_cirru_str(name))
          };
          let call_code = format!("{caller}({args_code})");
          Ok(wrap_call_with_prelude(prelude, call_code, return_label, detect_await(&body)))
        } else {
          Err(format!("invoke-native expected at least 1 object, got: {xs}"))
        }
      }
      MethodKind::InvokeNativeOptional => {
        if !body.is_empty() {
          let obj = to_js_code(&body[0], ns, local_defs, file_imports, tags, None)?;
          let (prelude, args_code) = gen_call_args_with_temps(
            &body.skip(1).expect("get args"),
            ns,
            local_defs,
            file_imports,
            tags,
            return_label.is_some(),
            inline_all,
          )?;

          let caller = if matches_js_var(name) {
            format!("{obj}.{name}")
          } else {
            format!("{obj}[{}]", escape_cirru_str(name))
          };
          let call_code = format!("{caller}?.({args_code})");
          Ok(wrap_call_with_prelude(prelude, call_code, return_label, detect_await(&body)))
        } else {
          Err(format!("invoke-native-optional expected at least 1 object, got: {xs}"))
        }
      }
      MethodKind::Invoke(_) => {
        let proc_prefix = get_proc_prefix(ns);
        if !body.is_empty() {
          let obj = to_js_code(&body[0], ns, local_defs, file_imports, tags, None)?;
          let (prelude, args_code) = gen_call_args_with_temps(
            &body.skip(1).expect("get args"),
            ns,
            local_defs,
            file_imports,
            tags,
            return_label.is_some(),
            inline_all,
          )?;

          let call_code = format!("{}invoke_method({},{},{})", proc_prefix, escape_cirru_str(name), obj, args_code);
          Ok(wrap_call_with_prelude(prelude, call_code, return_label, detect_await(&body)))
        } else {
          Err(format!("expected at least 1 object, got: {xs}"))
        }
      }
      MethodKind::TagAccess => {
        if body.len() == 1 {
          let obj = to_js_code(&body[0], ns, local_defs, file_imports, tags, None)?;
          let tag = tags::tag_access(name);
          Ok(format!("{obj}.get({tag})"))
        } else {
          Err(format!("tag-accessor takes only 1 argument, {xs}"))
        }
      }
    },
    _ => {
      let (prelude, args_code) =
        gen_call_args_with_temps(&body, ns, local_defs, file_imports, tags, return_label.is_some(), inline_all)?;
      let call_code = format!("{}({})", to_js_code(&head, ns, local_defs, file_imports, tags, None)?, args_code);
      Ok(wrap_call_with_prelude(prelude, call_code, return_label, detect_await(&body)))
    }
  }
}

/// a group of arguments related to scopes
struct PassedDefs<'a> {
  ns: &'a str,
  local_defs: &'a HashSet<Arc<str>>,
  file_imports: &'a RefCell<ImportsDict>,
}

fn gen_symbol_code(s: &str, def_ns: &str, at_def: &str, xs: &Calcit, passed_defs: &PassedDefs) -> Result<String, String> {
  // println!("gen symbol: {} {} {} {:?}", s, def_ns, ns, resolved);
  let var_prefix = if passed_defs.ns == calcit::CORE_NS { "" } else { "$clt." };
  if has_ns_part(s) {
    unreachable!("unknown feature: {s} {def_ns} {at_def} {xs}");
  }
  if is_js_syntax_procs(s) || is_proc_name(s) || CalcitSyntax::is_valid(s) {
    // return Ok(format!("{}{}", var_prefix, escape_var(s)));
    let proc_prefix = get_proc_prefix(passed_defs.ns);
    Ok(format!("{proc_prefix}{}", escape_var(s)))
  } else if passed_defs.local_defs.contains(s) {
    Ok(escape_var(s))
  } else if def_ns == calcit::CORE_NS {
    if !program::has_def_code(calcit::CORE_NS, s) {
      eprintln!(
        "[Warn] unresolved core symbol `{s}` during JS codegen in {}/{at_def}",
        passed_defs.ns
      );
    }
    Ok(format!("{var_prefix}{}", escape_var(s)))
  } else if def_ns.is_empty() {
    Err(format!("Unexpected ns at symbol, {xs}"))
  } else if def_ns != passed_defs.ns {
    // probably via macro
    // TODO dirty code collecting imports

    Ok(escape_var(s))
  } else if def_ns == passed_defs.ns {
    eprintln!("[Warn] detected unresolved variable `{s}` in {}/{at_def}", passed_defs.ns);
    Ok(escape_var(s))
  } else {
    eprintln!("[Warn] Unexpected case, code gen for `{s}` in {}/{at_def}", passed_defs.ns);
    Ok(format!("{var_prefix}{}", escape_var(s)))
  }
}

fn detect_await(xs: &CalcitList) -> bool {
  for x in xs {
    match x {
      Calcit::List(al) => {
        if let Some(Calcit::Syntax(CalcitSyntax::Defn, _s)) = al.get(0) {
          // a nested function has its own scope deciding if it's async
          return false;
        } else if detect_await(al) {
          return true;
        }
      }
      Calcit::Symbol { sym, .. } => {
        if &**sym == "js-await" {
          return true;
        }
      }
      _ => {}
    }
  }
  false
}

fn gen_let_code(
  body: &CalcitList,
  local_defs: &HashSet<Arc<str>>,
  xs: &Calcit,
  ns: &str,
  file_imports: &RefCell<ImportsDict>,
  tags: &RefCell<HashSet<EdnTag>>,
  base_return_label: Option<&str>,
) -> Result<String, String> {
  let mut let_def_body = body.to_owned();
  let return_label = base_return_label.unwrap_or("return ");
  let has_await = detect_await(body);

  // defined new local variable
  let mut scoped_defs = local_defs.to_owned();
  let mut defs_code = String::from("");
  let mut body_part = String::from("");

  // break unless nested &let is found
  loop {
    if let_def_body.len() <= 1 {
      return Err(format!("&let expected body, but got empty, {}", xs.lisp_str()));
    }
    let pair = let_def_body[0].to_owned();
    let content = let_def_body.drop_left();

    match &let_def_body[0] {
      Calcit::Nil => {
        for (idx, x) in content.iter().enumerate() {
          if idx == content.len() - 1 {
            body_part.push_str(&to_js_code(x, ns, &scoped_defs, file_imports, tags, Some(return_label))?);
            body_part.push('\n');
          } else {
            let line = to_js_code(x, ns, &scoped_defs, file_imports, tags, Some(""))?;
            body_part.push_str("{\n");
            body_part.push_str(&line);
            body_part.push_str(";\n}\n");
          }
        }
        break;
      }
      Calcit::List(xs) if xs.is_empty() => {
        // non content defs_code

        for (idx, x) in content.iter().enumerate() {
          if idx == content.len() - 1 {
            body_part.push_str(&to_js_code(x, ns, &scoped_defs, file_imports, tags, Some(return_label))?);
            body_part.push('\n');
          } else {
            let line = to_js_code(x, ns, &scoped_defs, file_imports, tags, Some(""))?;
            body_part.push_str("{\n");
            body_part.push_str(&line);
            body_part.push_str(";\n}\n");
          }
        }
        break;
      }
      Calcit::List(xs) if xs.len() == 2 => {
        let def_name = xs[0].to_owned();
        let def_code = xs[1].to_owned();

        match &def_name {
          Calcit::Local(CalcitLocal { sym, .. }) => {
            // TODO `let` inside expressions makes syntax error
            let left = escape_var(sym);
            let right = to_js_code(&def_code, ns, &scoped_defs, file_imports, tags, None)?;
            writeln!(defs_code, "let {left} = {right};").expect("write");

            if scoped_defs.contains(sym) {
              for (idx, x) in content.iter().enumerate() {
                if idx == content.len() - 1 {
                  // normally, last item of function body returns as return value(even in recursion)
                  if local_defs.contains(sym) {
                    // however, to shallow a conflicted variable, we need to return explicitly
                    body_part.push_str(&to_js_code(x, ns, &scoped_defs, file_imports, tags, Some("return "))?);
                  } else {
                    body_part.push_str(&to_js_code(x, ns, &scoped_defs, file_imports, tags, Some(return_label))?);
                  }
                  body_part.push('\n');
                } else {
                  let line = to_js_code(x, ns, &scoped_defs, file_imports, tags, Some(""))?;
                  body_part.push_str("{\n");
                  body_part.push_str(&line);
                  body_part.push_str(";\n}\n");
                }
              }

              // first variable is using conflicted name
              let ret = if local_defs.contains(sym) {
                make_let_with_bind(&left, &right, &body_part, has_await)
              } else {
                make_let_with_wrapper(&left, &right, &body_part, has_await)
              };
              return match base_return_label {
                Some(label) => Ok(format!("{label}{ret}")),
                None => Ok(ret),
              };
            } else {
              // track variable
              scoped_defs.insert(sym.to_owned());

              if content.len() == 1 {
                match &content[0] {
                  Calcit::List(ys) if ys.len() > 2 => match (&ys[0], &ys[1]) {
                    (Calcit::Syntax(sym, _ns), Calcit::List(zs)) if *sym == CalcitSyntax::CoreLet && zs.len() == 2 => match &zs[0] {
                      Calcit::Symbol { sym: s2, .. } if !scoped_defs.contains(s2) => {
                        let_def_body = ys.drop_left();
                        continue;
                      }
                      _ => (),
                    },
                    _ => (),
                  },
                  _ => (),
                }
              }

              for (idx, x) in content.iter().enumerate() {
                if idx == content.len() - 1 {
                  body_part.push_str(&to_js_code(x, ns, &scoped_defs, file_imports, tags, Some(return_label))?);
                  body_part.push('\n');
                } else {
                  body_part.push_str(&to_js_code(x, ns, &scoped_defs, file_imports, tags, None)?);
                  body_part.push_str(";\n");
                }
              }

              break;
            }
          }
          _ => return Err(format!("Expected symbol in &let binding, got: {}", &pair)),
        }
      }
      Calcit::List(_xs) => return Err(format!("expected pair of length 2, got: {}", &pair)),
      _ => return Err(format!("expected pair of a list of length 2, got: {pair}")),
    }
  }
  if base_return_label.is_some() {
    Ok(format!("{defs_code}{body_part}"))
  } else {
    Ok(make_fn_wrapper(&format!("{defs_code}{body_part}"), has_await))
  }
}

/// Generate JS code for `match` syntax.
/// After preprocessing, `body` is: [<value>, (<pattern1> <body1>), (<pattern2> <body2>), ...]
/// Generated JS is an IIFE with if-else chain checking tuple tag and arity.
fn gen_match_code(
  body: &CalcitList,
  local_defs: &HashSet<Arc<str>>,
  _xs: &Calcit,
  ns: &str,
  file_imports: &RefCell<ImportsDict>,
  tags: &RefCell<HashSet<EdnTag>>,
  base_return_label: Option<&str>,
) -> Result<String, String> {
  if body.is_empty() {
    return Err("match expected value and branches".to_owned());
  }

  let has_await = detect_await(body);
  let return_label = base_return_label.unwrap_or("return ");
  let proc_prefix = get_proc_prefix(ns);

  let value_code = to_js_code(&body[0], ns, local_defs, file_imports, tags, None)?;
  let val_var = js_gensym("match_v");
  let tag_var = js_gensym("match_t");

  let mut chunk = String::new();
  writeln!(chunk, "let {val_var} = {value_code};").expect("write");
  writeln!(chunk, "let {tag_var} = {proc_prefix}_$n_tuple_$o_nth({val_var}, 0);").expect("write");

  let mut first = true;
  for branch_idx in 1..body.len() {
    let branch = match &body[branch_idx] {
      Calcit::List(xs) if xs.len() == 2 => xs,
      other => return Err(format!("match branch expected a pair, got: {other}")),
    };

    let pattern = &branch[0];
    let branch_body = &branch[1];

    match pattern {
      // Wildcard
      Calcit::Symbol { sym, .. } if sym.as_ref() == "_" => {
        let body_code = to_js_code(branch_body, ns, local_defs, file_imports, tags, Some(return_label))?;
        if first {
          writeln!(chunk, "{{ {body_code} }}").expect("write");
        } else {
          writeln!(chunk, " else {{ {body_code} }}").expect("write");
        }
        first = false;
      }
      Calcit::Local(CalcitLocal { sym, .. }) if sym.as_ref() == "_" => {
        let body_code = to_js_code(branch_body, ns, local_defs, file_imports, tags, Some(return_label))?;
        if first {
          writeln!(chunk, "{{ {body_code} }}").expect("write");
        } else {
          writeln!(chunk, " else {{ {body_code} }}").expect("write");
        }
        first = false;
      }
      // Tag pattern: (:tag binding1 binding2 ...)
      Calcit::List(pat_xs) if !pat_xs.is_empty() => {
        let tag_name = match &pat_xs[0] {
          Calcit::Tag(t) => t,
          other => return Err(format!("match pattern expected tag, got: {other}")),
        };

        tags.borrow_mut().insert(tag_name.to_owned());
        let tag_code = tags::tag_access(tag_name.ref_str());
        let arity = pat_xs.len(); // includes tag, so total tuple count

        let else_mark = if first { "" } else { " else " };
        write!(
          chunk,
          "{else_mark}if ({tag_var} === {tag_code} && {proc_prefix}_$n_tuple_$o_count({val_var}) === {arity}) {{"
        )
        .expect("write");

        // Generate binding code
        let mut scoped_defs = local_defs.to_owned();
        for (i, binding) in pat_xs.iter().skip(1).enumerate() {
          let bind_name = match binding {
            Calcit::Local(CalcitLocal { sym, .. }) => escape_var(sym),
            Calcit::Symbol { sym, .. } => escape_var(sym),
            other => return Err(format!("match binding expected symbol, got: {other}")),
          };
          if let Calcit::Local(CalcitLocal { sym, .. }) | Calcit::Symbol { sym, .. } = binding {
            scoped_defs.insert(sym.to_owned());
          }
          write!(chunk, "\nlet {bind_name} = {proc_prefix}_$n_tuple_$o_nth({val_var}, {});", i + 1).expect("write");
        }

        let body_code = to_js_code(branch_body, ns, &scoped_defs, file_imports, tags, Some(return_label))?;
        writeln!(chunk, "\n{body_code} }}").expect("write");
        first = false;
      }
      other => return Err(format!("match unexpected pattern: {other}")),
    }
  }

  // Add fallthrough error if no wildcard
  if !body.iter().skip(1).any(|b| {
    matches!(b,
      Calcit::List(xs) if xs.len() == 2 && matches!(&xs[0],
        Calcit::Symbol { sym, .. } | Calcit::Local(CalcitLocal { sym, .. }) if sym.as_ref() == "_"
      )
    )
  }) {
    write!(
      chunk,
      " else {{ throw new Error(\"match: no matching branch for tag \" + {tag_var}); }}"
    )
    .expect("write");
  }

  if base_return_label.is_some() {
    Ok(chunk)
  } else {
    Ok(make_fn_wrapper(&chunk, has_await))
  }
}

fn gen_if_code(
  body: &CalcitList,
  local_defs: &HashSet<Arc<str>>,
  _xs: &Calcit,
  ns: &str,
  file_imports: &RefCell<ImportsDict>,
  tags: &RefCell<HashSet<EdnTag>>,
  base_return_label: Option<&str>,
) -> Result<String, String> {
  if body.len() < 2 || body.len() > 3 {
    Err(format!("if expected 2~3 nodes, got: {}", Calcit::from(body.to_owned())))
  } else {
    let mut chunk: String = String::from("");
    let mut cond_node = body[0].to_owned();
    let mut true_node = body[1].to_owned();
    let mut some_false_node = body.get(2);
    let mut need_else = false;
    let has_await = detect_await(body);

    let return_label = base_return_label.unwrap_or("return ");

    if base_return_label.is_none() && !has_await {
      let mut expr = String::from("");
      let mut depth = 0;
      loop {
        let cond_code = to_js_code(&cond_node, ns, local_defs, file_imports, tags, None)?;
        let true_code = to_js_code_inline(&true_node, ns, local_defs, file_imports, tags, None)?;
        write!(expr, "({cond_code} ? {true_code} : ").expect("write");
        depth += 1;

        if let Some(false_node) = some_false_node {
          if let Calcit::List(ys) = false_node
            && let Some(Calcit::Syntax(syn, _ns)) = ys.first()
            && syn == &CalcitSyntax::If
          {
            if ys.len() < 3 || ys.len() > 4 {
              return Err(format!("if expected 2~3 nodes, got: {}", Calcit::List(ys.to_owned())));
            }
            ys[1].clone_into(&mut cond_node);
            ys[2].clone_into(&mut true_node);
            some_false_node = ys.get(3);
            continue;
          }

          let false_code = to_js_code_inline(false_node, ns, local_defs, file_imports, tags, None)?;
          expr.push_str(&false_code);
        } else {
          expr.push_str("null");
        }

        for _ in 0..depth {
          expr.push(')');
        }
        break;
      }

      return Ok(expr);
    }

    loop {
      let cond_code = to_js_code(&cond_node, ns, local_defs, file_imports, tags, None)?;
      let true_code = to_js_code(&true_node, ns, local_defs, file_imports, tags, Some(return_label))?;
      let else_mark = if need_else { " else " } else { "" };

      write!(chunk, "\n{else_mark}if ({cond_code}) {{ {true_code} }}").expect("write");

      if let Some(false_node) = some_false_node {
        if let Calcit::List(ys) = false_node
          && let Some(Calcit::Syntax(syn, _ns)) = ys.first()
          && syn == &CalcitSyntax::If
        {
          if ys.len() < 3 || ys.len() > 4 {
            return Err(format!("if expected 2~3 nodes, got: {}", Calcit::List(ys.to_owned())));
          }
          ys[1].clone_into(&mut cond_node);
          ys[2].clone_into(&mut true_node);
          some_false_node = ys.get(3);
          need_else = true;
          continue;
        }

        let false_code = to_js_code(false_node, ns, local_defs, file_imports, tags, Some(return_label))?;
        write!(chunk, " else {{ {false_code} }}").expect("write");
      } else {
        write!(chunk, " else {{ {return_label} null; }}").expect("write");
      }
      break;
    }

    if base_return_label.is_some() {
      Ok(chunk)
    } else {
      Ok(make_fn_wrapper(&chunk, has_await))
    }
  }
}

fn wrap_call_with_prelude(prelude: String, call_code: String, return_label: Option<&str>, has_await: bool) -> String {
  if prelude.is_empty() {
    match return_label {
      Some(label) => format!("{label}{call_code}"),
      None => call_code,
    }
  } else {
    match return_label {
      Some(label) => format!("{prelude}{label}{call_code}"),
      None => make_fn_wrapper(&format!("{prelude}return {call_code};"), has_await),
    }
  }
}

fn list_to_js_code(
  xs: &TernaryTreeList<Calcit>,
  ns: &str,
  local_defs: HashSet<Arc<str>>,
  return_label: &str,
  file_imports: &RefCell<ImportsDict>,
  tags: &RefCell<HashSet<EdnTag>>,
) -> Result<String, String> {
  // TODO default returnLabel="return "
  let mut result = String::from("");
  for (idx, x) in xs.into_iter().enumerate() {
    // result = result & "// " & $x & "\n"
    if idx == xs.len() - 1 {
      let line = to_js_code(x, ns, &local_defs, file_imports, tags, Some(return_label))?;
      result.push_str(&line);
      result.push('\n');
    } else {
      let line = to_js_code(x, ns, &local_defs, file_imports, tags, Some(""))?;
      result.push_str("{\n");
      result.push_str(&line);
      result.push_str(";\n}\n");
    }
  }
  Ok(result)
}

fn uses_recur(xs: &Calcit) -> bool {
  match xs {
    Calcit::Symbol { sym: s, .. } => &**s == "recur",
    Calcit::Proc(s) => *s == CalcitProc::Recur,
    Calcit::List(ys) => match &ys.first() {
      Some(Calcit::Syntax(CalcitSyntax::Defn, _)) => false,
      Some(Calcit::Symbol { sym, .. }) if &**sym == "defn" => false,
      _ => {
        for y in &**ys {
          if uses_recur(y) {
            return true;
          }
        }
        false
      }
    },
    _ => false,
  }
}

fn gen_js_func(
  name: &str,
  args: &CalcitFnArgs,
  raw_body: &[Calcit],
  passed_defs: &PassedDefs,
  exported: bool,
  tags: &RefCell<HashSet<EdnTag>>,
  at_ns: &str,
) -> Result<String, String> {
  let var_prefix = if passed_defs.ns == "calcit.core" { "" } else { "$clt." };
  let mut local_defs = passed_defs.local_defs.to_owned();
  let mut spreading_code = String::from(""); // js list and calcit-js list are different, need to convert
  let mut args_code = String::from("");
  let mut spreading = false;
  let mut has_optional = false;
  let mut args_count = 0;
  let mut optional_count = 0;
  let mut recur_arg_names: Vec<String> = vec![];

  match args {
    CalcitFnArgs::MarkedArgs(args) => {
      for sym in args {
        if spreading {
          if let CalcitArgLabel::Idx(idx) = sym {
            if !args_code.is_empty() {
              args_code.push_str(", ");
            }
            let sym = CalcitLocal::read_name(*idx);
            let rest_used = raw_body.iter().any(|line| contains_symbol(line, &sym));
            let arg_name = escape_var(&sym);
            local_defs.insert(sym.into());
            args_code.push_str("...");
            args_code.push_str(&arg_name);
            // js list and calcit-js are different in spreading
            // only convert when rest arg is actually referenced in function body
            if rest_used {
              write!(spreading_code, "\n{arg_name} = {var_prefix}arrayToList({arg_name});").expect("write");
            }
            break; // no more args after spreading argument
          } else {
            return Err(format!("unexpected argument after spreading: {sym}"));
          }
        } else if has_optional {
          if let CalcitArgLabel::Idx(idx) = sym {
            if !args_code.is_empty() {
              args_code.push_str(", ");
            }
            let sym = CalcitLocal::read_name(*idx);
            args_code.push_str(&escape_var(&sym));
            local_defs.insert(sym.into());
            optional_count += 1;
          } else {
            return Err(format!("unexpected argument after optional: {sym}"));
          }
        } else {
          match sym {
            CalcitArgLabel::RestMark => {
              spreading = true;
            }
            CalcitArgLabel::OptionalMark => {
              has_optional = true;
            }
            CalcitArgLabel::Idx(idx) => {
              if !args_code.is_empty() {
                args_code.push_str(", ");
              }
              let sym = CalcitLocal::read_name(*idx);
              let arg_name = escape_var(&sym);
              args_code.push_str(&arg_name);
              recur_arg_names.push(arg_name);
              local_defs.insert(sym.into());
              args_count += 1;
            }
          }
        }
      }
    }
    CalcitFnArgs::Args(args) => {
      for idx in args {
        if !args_code.is_empty() {
          args_code.push_str(", ");
        }
        let sym = CalcitLocal::read_name(*idx);
        let arg_name = escape_var(&sym);
        args_code.push_str(&arg_name);
        recur_arg_names.push(arg_name);
        local_defs.insert(sym.into());
        args_count += 1;
      }
    }
  }

  let check_args = if skip_arity_check() {
    "".into()
  } else if spreading {
    snippets::tmpl_args_fewer_than(name, args_count, at_ns)
  } else if has_optional {
    snippets::tmpl_args_between(name, args_count, args_count + optional_count, at_ns)
  } else {
    snippets::tmpl_args_exact(name, args_count, at_ns)
  };

  let recur_assign_code_template = if !spreading && !has_optional {
    let mut code = String::new();
    for (idx, arg_name) in recur_arg_names.iter().enumerate() {
      if idx > 0 {
        code.push('\n');
      }
      write!(code, "{arg_name} = {{ret_var}}.args[{idx}];").expect("write");
    }
    code
  } else {
    format!("[ {args_code} ] = {{ret_var}}.args;")
  };

  let mut body: TernaryTreeList<Calcit> = TernaryTreeList::Empty;
  let mut async_prefix: String = String::from("");

  for line in raw_body {
    if let Calcit::List(xs) = line {
      let is_hint = match xs.first() {
        Some(Calcit::Syntax(sym, _ns)) => sym == &CalcitSyntax::HintFn,
        Some(Calcit::Symbol { sym, .. }) => sym.as_ref() == "hint-fn",
        _ => false,
      };
      if is_hint {
        if hinted_async(xs) {
          async_prefix = String::from("async ")
        } else if xs.len() > 1 && !xs.iter().skip(1).any(is_schema_map_form) {
          eprintln!(
            "[Warn] hint-fn args not in recognized schema map form in {}/{name}; correct usage: `hint-fn $ {{}} (:async true)`",
            passed_defs.ns
          );
        }
        continue;
      }
    }
    if line == &Calcit::Nil {
      continue;
    }
    body = body.push_right(line.to_owned());
  }

  if !body.is_empty() && uses_recur(&body[body.len() - 1]) {
    let return_var = js_gensym("return_mark");
    let body = list_to_js_code(
      &body,
      passed_defs.ns,
      local_defs,
      &format!("%%{return_var}%% ="),
      passed_defs.file_imports,
      tags,
    )?;
    let fn_def = snippets::tmpl_tail_recursion(
      /* name = */ escape_var(name),
      /* args_code = */ args_code,
      /* check_args = */ check_args,
      /* spreading_code = */ spreading_code,
      /* recur_assign_code_template = */ recur_assign_code_template,
      /* body = */
      body, // dirty trick
      snippets::RecurPrefixes {
        var_prefix: var_prefix.to_owned(),
        async_prefix,
        return_mark: format!("%%{return_var}%%"),
      },
    );

    let export_mark = if exported {
      format!("export let {} = ", escape_var(name))
    } else {
      String::from("")
    };
    Ok(format!("{export_mark}{fn_def}\n"))
  } else {
    let fn_definition = format!(
      "{}function {}({}) {{ {}{}\n{}}}",
      async_prefix,
      escape_var(name),
      args_code,
      check_args,
      spreading_code,
      list_to_js_code(&body, passed_defs.ns, local_defs, "return ", passed_defs.file_imports, tags)?
    );
    let export_mark = if exported { "export " } else { "" };
    Ok(format!("{export_mark}{fn_definition}\n"))
  }
}

/// this is a very rough implementation for now
fn hinted_async(xs: &CalcitList) -> bool {
  fn is_async_key(form: &Calcit) -> bool {
    match form {
      Calcit::Tag(tag) => tag.ref_str().trim_start_matches(':') == "async",
      Calcit::Symbol { sym, .. } => {
        let raw = sym.as_ref();
        raw == "async" || raw.trim_start_matches(':') == "async"
      }
      Calcit::Str(text) => text.as_ref() == "async",
      _ => false,
    }
  }

  fn is_truthy(form: &Calcit) -> bool {
    !matches!(form, Calcit::Nil | Calcit::Bool(false))
  }

  fn schema_marks_async(form: &Calcit) -> bool {
    match form {
      Calcit::Map(map) => map.iter().any(|(key, value)| is_async_key(key) && is_truthy(value)),
      Calcit::List(list) => {
        let is_map_literal = matches!(list.first(), Some(Calcit::Symbol { sym, .. }) if sym.as_ref() == "{}")
          || matches!(list.first(), Some(Calcit::Proc(CalcitProc::NativeMap)));
        if !is_map_literal {
          return false;
        }

        list.iter().skip(1).any(|entry| {
          let Calcit::List(pair) = entry else {
            return false;
          };
          if pair.len() < 2 {
            return false;
          }
          match (pair.get(0), pair.get(1)) {
            (Some(key), Some(value)) => is_async_key(key) && is_truthy(value),
            _ => false,
          }
        })
      }
      _ => false,
    }
  }

  xs.iter().skip(1).any(schema_marks_async)
}

/// Returns true when a value is in the schema map form recognised by hint-fn:
/// either an already-evaluated `Calcit::Map` or a list-literal starting with
/// `{}` / `NativeMap`.  Used to distinguish valid schema annotations (which
/// should never warn) from malformed async hints.
fn is_schema_map_form(form: &Calcit) -> bool {
  match form {
    Calcit::Map(_) => true,
    Calcit::List(list) => {
      matches!(list.first(), Some(Calcit::Symbol { sym, .. }) if sym.as_ref() == "{}")
        || matches!(list.first(), Some(Calcit::Proc(CalcitProc::NativeMap)))
    }
    _ => false,
  }
}

fn extract_preprocessed_fn_parts(code: &Calcit) -> Result<(CalcitFnArgs, Vec<Calcit>), String> {
  let Calcit::List(items) = code else {
    return Err(format!("expected preprocessed defn list, got: {code}"));
  };

  match (items.first(), items.get(1), items.get(2)) {
    (Some(Calcit::Syntax(CalcitSyntax::Defn, _)), Some(Calcit::Symbol { .. }), Some(Calcit::List(args))) => {
      let raw_args = get_raw_args_fn(args)?;
      Ok((raw_args, items.drop_left().drop_left().drop_left().to_vec()))
    }
    _ => Err(format!("expected preprocessed defn form, got: {code}")),
  }
}

pub fn emit_js(entry_ns: &str, emit_path: &str) -> Result<(), String> {
  let code_emit_path = Path::new(emit_path);
  if !code_emit_path.exists() {
    let _ = fs::create_dir(code_emit_path);
  }

  let mut unchanged_ns: HashSet<Arc<str>> = HashSet::new();

  let program = program::clone_compiled_program_snapshot()?;
  for (ns, file) in program.iter() {
    // println!("\nstart handling: {}\n", ns);
    // side-effects, reset tracking state

    let file_imports: RefCell<ImportsDict> = RefCell::new(ImportsDict::new());
    let collected_tags: RefCell<HashSet<EdnTag>> = RefCell::new(HashSet::new());

    let mut defs_in_current: HashSet<Arc<str>> = HashSet::new();
    for k in file.keys() {
      defs_in_current.insert(k.to_owned());
    }

    if !internal_states::is_first_compilation() {
      let app_pkg_name = entry_ns.split('.').collect::<Vec<&str>>()[0];
      let pkg_name = ns.split('.').collect::<Vec<&str>>()[0]; // TODO simpler
      if app_pkg_name != pkg_name {
        match internal_states::lookup_prev_ns_cache(ns) {
          Some(v) if v == defs_in_current => {
            // same as last time, skip
            continue;
          }
          _ => (),
        }
      }
    }
    // remember defs of each ns for comparing
    internal_states::write_as_ns_cache(ns, defs_in_current);

    // reset index each file
    reset_js_gensym_index();

    let core_lib = to_js_import_name("calcit.core", true);

    let mut defs_code = String::from(""); // code generated by functions
    let mut vals_code = String::from(""); // code generated by thunks
    let mut direct_code = String::from(""); // dirty code to run directly
    let mut tags_code = String::new();

    let mut import_code = if &**ns == "calcit.core" {
      snippets::tmpl_import_procs(wrap_js_str("@calcit/procs"))
    } else {
      format!("\nimport * as $clt from {core_lib};")
    };

    let mut def_names: HashSet<Arc<str>> = HashSet::new(); // multiple parts of scoped defs need to be tracked

    // tracking top level scope definitions
    for def in file.keys() {
      def_names.insert(def.to_owned());
    }

    let deps_in_order = sort_compiled_defs_by_deps(file);
    // println!("deps order: {:?}", deps_in_order);

    for def in deps_in_order {
      let compiled_def = file.get(&def).expect("compiled def for codegen");

      if &**ns == calcit::CORE_NS {
        if should_skip_core_def_codegen(&def, compiled_def) {
          continue;
        }
        // some defs from core can be replaced by calcit.procs
        if is_js_unavailable_procs(&def) {
          continue;
        }
        if is_preferred_js_proc(&def) {
          writeln!(defs_code, "\nvar {} = $procs.{};", escape_var(&def), escape_var(&def)).expect("write");
          continue;
        }
      }

      match &compiled_def.kind {
        // probably not work here
        program::CompiledDefKind::Proc => {
          writeln!(defs_code, "\nvar {} = $procs.{};", escape_var(&def), escape_var(&def)).expect("write");
        }
        program::CompiledDefKind::Fn => {
          let (raw_args, raw_body) = extract_preprocessed_fn_parts(&compiled_def.preprocessed_code)?;
          gen_stack::push_call_stack(ns, &def, StackKind::Codegen, compiled_def.preprocessed_code.to_owned(), &[]);
          let passed_defs = PassedDefs {
            ns,
            local_defs: &def_names,
            file_imports: &file_imports,
          };
          defs_code.push_str(&gen_js_func(&def, &raw_args, &raw_body, &passed_defs, true, &collected_tags, ns)?);
          gen_stack::pop_call_stack();
        }
        program::CompiledDefKind::LazyValue => {
          // TODO need topological sorting for accuracy
          // values are called directly, put them after fns
          gen_stack::push_call_stack(ns, &def, StackKind::Codegen, compiled_def.codegen_form.to_owned(), &[]);
          writeln!(
            vals_code,
            "\nexport var {} = {};",
            escape_var(&def),
            to_js_code(&compiled_def.codegen_form, ns, &def_names, &file_imports, &collected_tags, None)?
          )
          .expect("write");
          gen_stack::pop_call_stack()
        }
        // macro are not traced in codegen since already expanded
        program::CompiledDefKind::Macro => {}
        program::CompiledDefKind::Syntax => {
          // should he handled inside compiler
        }
        program::CompiledDefKind::Value if matches!(&compiled_def.codegen_form, Calcit::Bool(_) | Calcit::Number(_)) => {
          eprintln!(
            "[Warn] expected thunk, got macro. skipped `{ns}/{def} {}`",
            compiled_def.codegen_form
          )
        }
        program::CompiledDefKind::Value => {
          eprintln!("[Warn] expected thunk for js, skipped `{ns}/{def} {}`", compiled_def.codegen_form)
        }
      }
    }
    if &**ns == calcit::CORE_NS {
      // add at end of file to register builtin classes
      direct_code.push_str(&snippets::tmpl_classes_registering())
    }

    let collected_imports = file_imports.borrow();
    if !collected_imports.is_empty() {
      let mut xs = collected_imports.0.iter().to_owned().collect::<Vec<_>>();
      xs.sort();
      for item in &xs {
        // println!("import item: {:?}", item);
        match &*item.info {
          ImportInfo::NsAs { .. } => {
            let import_target = if is_cirru_string(&item.ns) {
              wrap_js_str(&item.ns[1..])
            } else {
              to_js_import_name(&item.ns, true)
            };
            write!(import_code, "\nimport * as {} from {import_target};", escape_ns(&item.ns)).expect("write");
          }
          ImportInfo::JsDefault { alias, at_ns, .. } => {
            if is_cirru_string(&item.ns) {
              let import_target = wrap_js_str(&item.ns[1..]);
              write!(import_code, "\nimport {} from {import_target};", escape_var(alias)).expect("write");
            } else {
              unreachable!("only js import leads to default ns, but got: {}", at_ns)
            }
          }
          ImportInfo::NsReferDef { .. } => {
            let import_target = if is_cirru_string(&item.ns) {
              wrap_js_str(&item.ns[1..])
            } else {
              to_js_import_name(&item.ns, true)
            };
            write!(import_code, "\nimport {{ {} }} from {import_target};", escape_var(&item.def)).expect("write");
          }
          ImportInfo::Core { at_ns } => {
            if at_ns == &item.ns {
              continue;
            }
            write!(import_code, "\nimport {{ {} }} from {core_lib};", escape_var(&item.def)).expect("write");
          }
          ImportInfo::SameFile { .. } => {
            // nothing to do
          }
        }
      }
    }

    let tag_prefix = if &**ns == "calcit.core" { "" } else { "$clt." };
    let mut tag_arr = String::from("[");
    let mut ordered_tags: Vec<EdnTag> = vec![];
    for k in collected_tags.borrow().iter() {
      ordered_tags.push(k.to_owned());
    }
    // need to maintain a stable order to reduce redundant reloads
    ordered_tags.sort();

    for s in ordered_tags {
      let name = escape_cirru_str(s.ref_str());
      write!(tag_arr, "{name},").expect("write");
    }
    tag_arr.push(']');
    tags_code.push_str(&snippets::tmpl_tags_init(&tag_arr, tag_prefix));

    let js_file_path = code_emit_path.join(to_mjs_filename(ns));
    let wrote_new = write_file_if_changed(
      &js_file_path,
      &format!("{import_code}{tags_code}\n{defs_code}\n\n{vals_code}\n{direct_code}"),
    )?;
    if wrote_new {
      println!("emitted: {}", js_file_path.to_str().expect("exptract path"));
    } else {
      unchanged_ns.insert(ns.to_owned());
    }
  }

  if !unchanged_ns.is_empty() {
    println!("\n... and {} files not changed.", unchanged_ns.len());
  }

  let _ = internal_states::finish_compilation();

  Ok(())
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::calcit::CalcitSymbolInfo;

  fn runtime_placeholder_quote() -> Calcit {
    Calcit::List(Arc::new(CalcitList::from(&[
      Calcit::Syntax(CalcitSyntax::Quote, Arc::from(calcit::CORE_NS)),
      symbol("&runtime-implementation"),
    ])))
  }

  fn compiled_def_for_codegen_test(kind: program::CompiledDefKind, source_code: Option<Calcit>) -> program::CompiledDef {
    program::CompiledDef {
      def_id: program::DefId(0),
      version_id: 0,
      kind,
      preprocessed_code: Calcit::Nil,
      codegen_form: Calcit::Nil,
      deps: vec![],
      type_summary: None,
      source_code,
      schema: calcit::DYNAMIC_TYPE.clone(),
      doc: Arc::from(""),
      examples: vec![],
    }
  }

  fn symbol(name: &str) -> Calcit {
    Calcit::Symbol {
      sym: Arc::from(name),
      info: Arc::new(CalcitSymbolInfo {
        at_ns: Arc::from("tests.emit-js"),
        at_def: Arc::from("hinted-async"),
      }),
      location: None,
    }
  }

  #[test]
  fn hinted_async_accepts_schema_map_literal() {
    let schema = Calcit::List(Arc::new(CalcitList::from(&[
      symbol("{}"),
      Calcit::List(Arc::new(CalcitList::from(&[
        Calcit::Tag(EdnTag::from("async")),
        Calcit::Bool(true),
      ]))),
    ])));
    let hint = CalcitList::from(&[Calcit::Syntax(CalcitSyntax::HintFn, Arc::from("tests")), schema]);
    assert!(hinted_async(&hint));
  }

  #[test]
  fn hinted_async_ignores_false_schema_marker() {
    let schema = Calcit::List(Arc::new(CalcitList::from(&[
      symbol("{}"),
      Calcit::List(Arc::new(CalcitList::from(&[
        Calcit::Tag(EdnTag::from("async")),
        Calcit::Bool(false),
      ]))),
    ])));
    let hint = CalcitList::from(&[Calcit::Syntax(CalcitSyntax::HintFn, Arc::from("tests")), schema]);
    assert!(!hinted_async(&hint));
  }

  #[test]
  fn core_codegen_skips_runtime_placeholder_defs() {
    let compiled = compiled_def_for_codegen_test(program::CompiledDefKind::LazyValue, Some(runtime_placeholder_quote()));
    assert!(should_skip_core_def_codegen("range", &compiled));
  }

  #[test]
  fn core_codegen_skips_syntax_names_even_without_runtime_placeholder_source() {
    let compiled = compiled_def_for_codegen_test(program::CompiledDefKind::LazyValue, None);
    assert!(should_skip_core_def_codegen("eval", &compiled));
  }

  #[test]
  fn raw_syntax_nodes_fail_js_codegen_with_llm_hint() {
    let local_defs: HashSet<Arc<str>> = HashSet::new();
    let file_imports = RefCell::new(ImportsDict::new());
    let tags = RefCell::new(HashSet::new());

    let failure = to_js_code(
      &Calcit::Syntax(CalcitSyntax::If, Arc::from("tests.emit-js")),
      "tests.emit-js",
      &local_defs,
      &file_imports,
      &tags,
      None,
    )
    .expect_err("raw syntax should be rejected in JS codegen");

    assert!(failure.contains("raw syntax node `if`"), "unexpected error: {failure}");
    assert!(failure.contains("LLM hint"), "unexpected error: {failure}");
  }

  #[test]
  fn reset_syntax_call_codegen_uses_runtime_proc() {
    let local_defs: HashSet<Arc<str>> = HashSet::new();
    let file_imports = RefCell::new(ImportsDict::new());
    let tags = RefCell::new(HashSet::new());
    let form = Calcit::List(Arc::new(CalcitList::from(&[
      Calcit::Syntax(CalcitSyntax::Reset, Arc::from("tests.emit-js")),
      symbol("state"),
      Calcit::Number(1.0),
    ])));

    let code = to_js_code(&form, "tests.emit-js", &local_defs, &file_imports, &tags, None).expect("reset! should compile");

    assert_eq!(code, "$clt.reset_$x_(state, 1)");
  }

  #[test]
  fn eval_syntax_call_codegen_uses_runtime_proc() {
    let local_defs: HashSet<Arc<str>> = HashSet::new();
    let file_imports = RefCell::new(ImportsDict::new());
    let tags = RefCell::new(HashSet::new());
    let form = Calcit::List(Arc::new(CalcitList::from(&[
      Calcit::Syntax(CalcitSyntax::Eval, Arc::from("tests.emit-js")),
      symbol("code"),
    ])));

    let code = to_js_code(&form, "tests.emit-js", &local_defs, &file_imports, &tags, None).expect("eval should compile");

    assert_eq!(code, "$clt.eval(code)");
  }
}