1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
//! WASM Emitter Implementation
//!
//! Core WASM code generation from Ruchy AST.
use crate::frontend::ast::{BinaryOp, Expr, ExprKind, Literal, Pattern, StringPart};
use wasm_encoder::{
CodeSection, ConstExpr, ExportSection, Function, FunctionSection, GlobalSection, GlobalType,
Instruction, MemorySection, MemoryType, Module, TypeSection, ValType,
};
use super::symbol_table::SymbolTable;
use super::types::WasmType;
use super::utils;
pub struct WasmEmitter {
module: Module,
symbols: std::cell::RefCell<SymbolTable>,
/// Maps function name to `(index, is_void)`
functions: std::cell::RefCell<std::collections::HashMap<String, (u32, bool)>>,
/// Maps struct name to ordered field names (order determines memory offset)
/// Complexity: Field at index N is at offset N * 4 bytes
structs: std::cell::RefCell<std::collections::HashMap<String, Vec<String>>>,
/// Maps variable name to tuple element types (for mixed-type tuple support)
/// Example: "x" -> [I32, F32] for tuple (1, 3.0)
tuple_types: std::cell::RefCell<std::collections::HashMap<String, Vec<WasmType>>>,
}
impl WasmEmitter {
/// # Examples
///
/// ```ignore
/// use ruchy::backend::wasm::WasmEmitter;
/// let instance = WasmEmitter::new();
/// ```
pub fn new() -> Self {
Self {
module: Module::new(),
symbols: std::cell::RefCell::new(SymbolTable::new()),
functions: std::cell::RefCell::new(std::collections::HashMap::new()),
structs: std::cell::RefCell::new(std::collections::HashMap::new()),
tuple_types: std::cell::RefCell::new(std::collections::HashMap::new()),
}
}
/// Infer element WASM type from expression for tuple support
/// Complexity: 5 (Toyota Way: <10 ✓)
///
/// Returns `WasmType` based on expression kind
/// Used for mixed-type tuple support
pub(crate) fn infer_element_type(&self, expr: &Expr) -> WasmType {
match &expr.kind {
ExprKind::Literal(Literal::Float(_)) => WasmType::F32,
ExprKind::Literal(Literal::Integer(_, _)) => WasmType::I32,
ExprKind::Literal(Literal::Bool(_)) => WasmType::I32,
ExprKind::Literal(Literal::String(_)) => WasmType::I32, // Address
ExprKind::Binary { op, .. } => {
// Float operations return float, others return int
match op {
BinaryOp::Add
| BinaryOp::Subtract
| BinaryOp::Multiply
| BinaryOp::Divide
| BinaryOp::Modulo => WasmType::F32, // Could be either
_ => WasmType::I32, // Comparisons, logical ops
}
}
_ => WasmType::I32, // Default to I32 for complex expressions
}
}
/// Collect tuple element types from AST for mixed-type support
/// Complexity: 7 (Toyota Way: <10 ✓)
///
/// Traverses AST recursively to find `Let { value: Tuple(..) }` nodes
/// Stores variable name → element types mapping for correct load/store
fn collect_tuple_types(&self, expr: &Expr) {
match &expr.kind {
ExprKind::Let {
name, value, body, ..
} => {
// Check if value is a tuple and register element types
if let ExprKind::Tuple(elements) = &value.kind {
let element_types: Vec<WasmType> = elements
.iter()
.map(|e| self.infer_element_type(e))
.collect();
self.tuple_types
.borrow_mut()
.insert(name.clone(), element_types);
}
// Recursively traverse
self.collect_tuple_types(value);
self.collect_tuple_types(body);
}
ExprKind::Block(exprs) => {
for e in exprs {
self.collect_tuple_types(e);
}
}
ExprKind::Function { body, .. } => {
self.collect_tuple_types(body);
}
ExprKind::If {
condition,
then_branch,
else_branch,
} => {
self.collect_tuple_types(condition);
self.collect_tuple_types(then_branch);
if let Some(else_expr) = else_branch {
self.collect_tuple_types(else_expr);
}
}
_ => {}
}
}
/// Collect struct definitions from AST to build field layout map
/// Complexity: 8 (Toyota Way: <10 ✓)
///
/// Traverses AST recursively to find `ExprKind::Struct` nodes
/// Stores struct name → field names mapping for offset calculation
fn collect_struct_definitions(&self, expr: &Expr) {
match &expr.kind {
ExprKind::Struct { name, fields, .. } => {
// Extract field names in order (order determines memory offset)
let field_names: Vec<String> = fields.iter().map(|f| f.name.clone()).collect();
self.structs.borrow_mut().insert(name.clone(), field_names);
}
// Recursively traverse block expressions
ExprKind::Block(exprs) => {
for e in exprs {
self.collect_struct_definitions(e);
}
}
// Recursively traverse let bindings
ExprKind::Let { value, body, .. } => {
self.collect_struct_definitions(value);
self.collect_struct_definitions(body);
}
// Recursively traverse function definitions
ExprKind::Function { body, .. } => {
self.collect_struct_definitions(body);
}
_ => {}
}
}
/// Emit a complete WASM module from a Ruchy AST expression
/// # Examples
///
/// ```ignore
/// use ruchy::backend::wasm::WasmEmitter;
/// use ruchy::frontend::ast::{Expr, ExprKind};
/// let instance = WasmEmitter::new();
/// let expr = Expr::new(ExprKind::Block(vec![]), Default::default());
/// let result = instance.emit(&expr);
/// ```
pub fn emit(&self, expr: &Expr) -> Result<Vec<u8>, String> {
// Collect tuple types BEFORE building symbol table (needed for type inference)
self.collect_tuple_types(expr);
// Build symbol table from entire expression tree
self.build_symbol_table(expr);
// Collect struct definitions for field layout mapping
self.collect_struct_definitions(expr);
let mut module = Module::new();
let func_defs = self.collect_functions(expr);
// Build function index map (must be done after collecting functions)
self.build_function_index_map(expr, &func_defs);
// Add sections to module (order matters in WASM)
let types = self.emit_type_section(expr, &func_defs);
module.section(&types);
// Import section must come before function section
if let Some(imports) = self.emit_import_section(expr) {
module.section(&imports);
}
let functions = self.emit_function_section(&func_defs, expr);
module.section(&functions);
if let Some(memories) = self.emit_memory_section(expr) {
module.section(&memories);
}
// Global section for heap pointer (if memory is needed)
if let Some(globals) = self.emit_global_section(expr) {
module.section(&globals);
}
if let Some(exports) = self.emit_export_section(expr) {
module.section(&exports);
}
let codes = self.emit_code_section(expr, &func_defs)?;
module.section(&codes);
Ok(module.finish())
}
/// Emit type section with function signatures
/// Complexity: 7 (Toyota Way: <10 ✓)
fn emit_type_section(
&self,
expr: &Expr,
func_defs: &[(String, Vec<crate::frontend::ast::Param>, Box<Expr>)],
) -> TypeSection {
let mut types = TypeSection::new();
let has_functions = !func_defs.is_empty();
// Type index 0: Built-in functions println_i32 - (i32) -> ()
// Type index 1: Built-in functions println_f32 - (f32) -> ()
// These must be first because imports reference them
if utils::uses_builtins(expr) {
types.function(vec![wasm_encoder::ValType::I32], vec![]);
types.function(vec![wasm_encoder::ValType::F32], vec![]);
}
if has_functions {
// Add a type for each function
for (_name, params, body) in func_defs {
let param_types = vec![wasm_encoder::ValType::I32; params.len()];
// Determine return type: check for explicit return OR implicit value
let returns_value =
utils::has_return_with_value(body) || self.expression_produces_value(body);
let return_types = if returns_value {
vec![wasm_encoder::ValType::I32]
} else {
vec![] // Void function
};
types.function(param_types, return_types);
}
// Also add a type for the main function if there's non-function code
if let Some(main_expr) = self.get_non_function_code(expr) {
self.add_main_type(&mut types, &main_expr);
}
} else {
// Single implicit main function
self.add_main_type(&mut types, expr);
}
types
}
/// Add main function type based on expression
/// Complexity: 3 (Toyota Way: <10 ✓)
fn add_main_type(&self, types: &mut TypeSection, expr: &Expr) {
if self.expression_produces_value(expr) {
let wasm_ty = self.wasm_type_to_valtype(self.infer_type(expr));
types.function(vec![], vec![wasm_ty]);
} else {
types.function(vec![], vec![]);
}
}
/// Convert `WasmType` to `wasm_encoder::ValType`
/// Complexity: 1 (Toyota Way: <10 ✓)
pub(crate) fn wasm_type_to_valtype(&self, ty: WasmType) -> wasm_encoder::ValType {
match ty {
WasmType::I32 => wasm_encoder::ValType::I32,
WasmType::F32 => wasm_encoder::ValType::F32,
WasmType::I64 => wasm_encoder::ValType::I64,
WasmType::F64 => wasm_encoder::ValType::F64,
}
}
/// Emit import section for built-in functions
/// Complexity: 3 (Toyota Way: <10 ✓)
fn emit_import_section(&self, expr: &Expr) -> Option<wasm_encoder::ImportSection> {
// Check if expression uses any built-in functions
if !utils::uses_builtins(expr) {
return None;
}
let mut imports = wasm_encoder::ImportSection::new();
// Import println_i32 and println_f32 from host environment
// Function index 0: println_i32 uses type index 0: (i32) -> ()
// Function index 1: println_f32 uses type index 1: (f32) -> ()
imports.import("env", "println_i32", wasm_encoder::EntityType::Function(0));
imports.import("env", "println_f32", wasm_encoder::EntityType::Function(1));
Some(imports)
}
/// Emit function section
/// Complexity: 6 (Toyota Way: <10 ✓)
fn emit_function_section(
&self,
func_defs: &[(String, Vec<crate::frontend::ast::Param>, Box<Expr>)],
expr: &Expr,
) -> FunctionSection {
let mut functions = FunctionSection::new();
let has_functions = !func_defs.is_empty();
// Type index offset: if we have built-ins, they occupy type indices 0 and 1
let type_offset = if utils::uses_builtins(expr) { 2 } else { 0 };
if has_functions {
for i in 0..func_defs.len() {
functions.function((i as u32) + type_offset);
}
if self.get_non_function_code(expr).is_some() {
functions.function((func_defs.len() as u32) + type_offset);
}
} else {
functions.function(type_offset);
}
functions
}
/// Emit memory section if needed
/// Complexity: 2 (Toyota Way: <10 ✓)
fn emit_memory_section(&self, expr: &Expr) -> Option<MemorySection> {
if utils::needs_memory(expr) {
let mut memories = MemorySection::new();
memories.memory(MemoryType {
minimum: 1,
maximum: Some(1), // Fixed 64KB for MVP
memory64: false,
shared: false,
page_size_log2: None,
});
Some(memories)
} else {
None
}
}
/// Emit global section for heap pointer
/// Complexity: 3 (Toyota Way: <10 ✓)
///
/// Creates a mutable global `$heap_ptr` initialized to 0
/// This is used by the bump allocator for memory allocation
fn emit_global_section(&self, expr: &Expr) -> Option<GlobalSection> {
if utils::needs_memory(expr) {
let mut globals = GlobalSection::new();
// Global 0: heap pointer (mutable i32, starts at 0)
globals.global(
GlobalType {
val_type: ValType::I32,
mutable: true,
shared: false,
},
&ConstExpr::i32_const(0),
);
Some(globals)
} else {
None
}
}
/// Emit export section if needed
/// Complexity: 2 (Toyota Way: <10 ✓)
fn emit_export_section(&self, expr: &Expr) -> Option<ExportSection> {
if utils::has_main_function(expr) {
let mut exports = ExportSection::new();
exports.export("main", wasm_encoder::ExportKind::Func, 0);
Some(exports)
} else {
None
}
}
/// Emit code section with compiled functions
/// Complexity: 8 (Toyota Way: <10 ✓)
fn emit_code_section(
&self,
expr: &Expr,
func_defs: &[(String, Vec<crate::frontend::ast::Param>, Box<Expr>)],
) -> Result<CodeSection, String> {
let mut codes = CodeSection::new();
let has_functions = !func_defs.is_empty();
if has_functions {
for (_name, _params, body) in func_defs {
let func = self.compile_function(body.as_ref())?;
codes.function(&func);
}
if let Some(main_expr) = self.get_non_function_code(expr) {
let func = self.compile_function(&main_expr)?;
codes.function(&func);
}
} else {
let func = self.compile_function(expr)?;
codes.function(&func);
}
Ok(codes)
}
/// Compile a single function body
/// Complexity: 6 (Toyota Way: <10 ✓)
fn compile_function(&self, body: &Expr) -> Result<Function, String> {
let locals = self.collect_local_types(body);
let mut func = Function::new(locals);
let instructions = self.lower_expression(body)?;
for instr in instructions {
func.instruction(&instr);
}
// If expression produces value but function is void, drop it
if self.expression_produces_value(body) {
// Value producing expressions leave result on stack
// If function should return void, we need to drop it
// But we don't know here if this is a void function...
// This is handled by type section already
}
func.instruction(&Instruction::End);
Ok(func)
}
/// Build symbol table by scanning expression tree for let bindings
/// Complexity: 7 (within <10 limit)
fn build_symbol_table(&self, expr: &Expr) {
match &expr.kind {
ExprKind::Let {
name, value, body, ..
} => {
let value_ty = self.infer_type(value);
self.symbols.borrow_mut().insert(name.clone(), value_ty);
self.build_symbol_table(body);
}
ExprKind::LetPattern { pattern, body, .. } => {
// Register all identifiers in the pattern
self.register_pattern_symbols(pattern);
self.build_symbol_table(body);
}
ExprKind::Block(exprs) => {
for e in exprs {
self.build_symbol_table(e);
}
}
ExprKind::Binary { left, right, .. } => {
self.build_symbol_table(left);
self.build_symbol_table(right);
}
ExprKind::If {
condition,
then_branch,
else_branch,
} => {
self.build_symbol_table(condition);
self.build_symbol_table(then_branch);
if let Some(else_expr) = else_branch {
self.build_symbol_table(else_expr);
}
}
ExprKind::Function { body, .. } => {
self.build_symbol_table(body);
}
ExprKind::While {
condition, body, ..
} => {
self.build_symbol_table(condition);
self.build_symbol_table(body);
}
ExprKind::Match { expr, arms } => {
self.build_symbol_table(expr);
// Note: Pattern variables in match arms are NOT registered as locals in MVP
// This is because match arms have different scopes and WASM doesn't support
// variable shadowing easily. For MVP, match patterns with bindings will fail
// during lowering. Full implementation requires scoped locals.
for arm in arms {
// Only recurse into body, don't register pattern variables
self.build_symbol_table(&arm.body);
}
}
_ => {} // Other expression types don't introduce bindings
}
}
/// Collect all local variable types from expression tree
/// Returns vector of (count, type) for WASM function locals section
/// Complexity: 9 (within <10 limit)
fn collect_local_types(&self, expr: &Expr) -> Vec<(u32, wasm_encoder::ValType)> {
let symbols = self.symbols.borrow();
let local_count = symbols.local_count();
let needs_temp = utils::needs_memory(expr); // Need temp local for tuple allocation
if local_count == 0 && !needs_temp {
return vec![];
}
// Collect all unique (type, index) pairs
let mut locals: Vec<(WasmType, u32)> = symbols.all_locals();
// Sort by index
locals.sort_by_key(|(_, index)| *index);
// Convert to (count, ValType) format
// For now, just declare each local individually
let mut result: Vec<(u32, wasm_encoder::ValType)> = locals
.into_iter()
.map(|(ty, _)| {
let val_type = match ty {
WasmType::I32 => wasm_encoder::ValType::I32,
WasmType::F32 => wasm_encoder::ValType::F32,
WasmType::I64 => wasm_encoder::ValType::I64,
WasmType::F64 => wasm_encoder::ValType::F64,
};
(1, val_type)
})
.collect();
// Add temporary local for tuple/struct allocation if needed
if needs_temp {
result.push((1, wasm_encoder::ValType::I32));
}
result
}
/// Register all identifiers in a pattern to the symbol table
/// Complexity: 5 (Toyota Way: <10 ✓)
fn register_pattern_symbols(&self, pattern: &Pattern) {
match pattern {
Pattern::Identifier(name) => {
// Register as i32 type (default for MVP)
self.symbols
.borrow_mut()
.insert(name.clone(), WasmType::I32);
}
Pattern::Tuple(patterns) => {
// Recursively register all identifiers in tuple elements
for p in patterns {
self.register_pattern_symbols(p);
}
}
Pattern::Wildcard => {
// Wildcard doesn't bind any variable
}
_ => {
// Other patterns not yet supported in WASM
}
}
}
/// Infer the WASM type of an expression
/// Complexity: 7 (Toyota Way: <10 ✓)
fn infer_type(&self, expr: &Expr) -> WasmType {
match &expr.kind {
ExprKind::Literal(Literal::Integer(_, _)) => WasmType::I32,
ExprKind::Literal(Literal::Float(_)) => WasmType::F32,
ExprKind::Literal(Literal::Bool(_)) => WasmType::I32,
ExprKind::Binary { op, left, right } => self.infer_binary_type(op, left, right),
ExprKind::Let { body, .. } => self.infer_let_type(body),
ExprKind::Identifier(name) => self.infer_identifier_type(name),
ExprKind::Block(exprs) => exprs.last().map_or(WasmType::I32, |e| self.infer_type(e)),
ExprKind::Call { .. } => WasmType::I32,
ExprKind::Unary { operand, .. } => self.infer_type(operand),
ExprKind::FieldAccess { object, field } => {
// For tuple field access, look up the element type
if let Ok(index) = field.parse::<usize>() {
if let ExprKind::Identifier(name) = &object.kind {
return self
.tuple_types
.borrow()
.get(name)
.and_then(|types| types.get(index))
.copied()
.unwrap_or(WasmType::I32);
}
}
WasmType::I32
}
_ => WasmType::I32,
}
}
/// Convert `WasmType` to `wasm_encoder::ValType`
/// Complexity: 1 (Toyota Way: <10 ✓)
fn infer_wasm_type(&self, expr: &Expr) -> wasm_encoder::ValType {
match self.infer_type(expr) {
WasmType::I32 => wasm_encoder::ValType::I32,
WasmType::I64 => wasm_encoder::ValType::I64,
WasmType::F32 => wasm_encoder::ValType::F32,
WasmType::F64 => wasm_encoder::ValType::F64,
}
}
/// Infer type for binary expression
/// Complexity: 3 (Toyota Way: <10 ✓)
fn infer_binary_type(&self, op: &BinaryOp, left: &Expr, right: &Expr) -> WasmType {
use crate::frontend::ast::BinaryOp;
if matches!(
op,
BinaryOp::Equal
| BinaryOp::NotEqual
| BinaryOp::Less
| BinaryOp::LessEqual
| BinaryOp::Greater
| BinaryOp::GreaterEqual
| BinaryOp::Gt
) {
return WasmType::I32;
}
// Arithmetic: Type promotion (f32 > i32)
let left_ty = self.infer_type(left);
let right_ty = self.infer_type(right);
if left_ty == WasmType::F32 || right_ty == WasmType::F32 {
WasmType::F32
} else {
WasmType::I32
}
}
/// Infer type for let expression
/// Complexity: 2 (Toyota Way: <10 ✓)
fn infer_let_type(&self, body: &Expr) -> WasmType {
match &body.kind {
ExprKind::Literal(Literal::Unit) => WasmType::I32,
_ => self.infer_type(body),
}
}
/// Infer type for identifier
/// Complexity: 1 (Toyota Way: <10 ✓)
fn infer_identifier_type(&self, name: &str) -> WasmType {
self.symbols
.borrow()
.lookup_type(name)
.unwrap_or(WasmType::I32)
}
/// Lower a binary operation to WASM instructions
/// Complexity: 8 (Toyota Way: <10 ✓)
fn lower_binary(
&self,
op: &BinaryOp,
left: &Expr,
right: &Expr,
) -> Result<Vec<Instruction<'static>>, String> {
let mut instructions = vec![];
// Infer result type based on operands
let result_type = self.infer_binary_result_type(left, right);
// Emit left operand with type conversion
instructions.extend(self.lower_expression(left)?);
if self.infer_type(left) == WasmType::I32 && result_type == WasmType::F32 {
instructions.push(Instruction::F32ConvertI32S);
}
// Emit right operand with type conversion
instructions.extend(self.lower_expression(right)?);
if self.infer_type(right) == WasmType::I32 && result_type == WasmType::F32 {
instructions.push(Instruction::F32ConvertI32S);
}
// Emit operation instruction
let op_instr = self.binary_op_to_instruction(op, result_type)?;
instructions.push(op_instr);
Ok(instructions)
}
/// Infer result type for binary operation
/// Complexity: 2 (Toyota Way: <10 ✓)
fn infer_binary_result_type(&self, left: &Expr, right: &Expr) -> WasmType {
let left_ty = self.infer_type(left);
let right_ty = self.infer_type(right);
if left_ty == WasmType::F32 || right_ty == WasmType::F32 {
WasmType::F32
} else {
WasmType::I32
}
}
/// Map binary operation to WASM instruction
/// Complexity: 1 (Toyota Way: <10 ✓)
fn binary_op_to_instruction(
&self,
op: &BinaryOp,
ty: WasmType,
) -> Result<Instruction<'static>, String> {
match (op, ty) {
(BinaryOp::Add, WasmType::I32) => Ok(Instruction::I32Add),
(BinaryOp::Add, WasmType::F32) => Ok(Instruction::F32Add),
(BinaryOp::Subtract, WasmType::I32) => Ok(Instruction::I32Sub),
(BinaryOp::Subtract, WasmType::F32) => Ok(Instruction::F32Sub),
(BinaryOp::Multiply, WasmType::I32) => Ok(Instruction::I32Mul),
(BinaryOp::Multiply, WasmType::F32) => Ok(Instruction::F32Mul),
(BinaryOp::Divide, WasmType::I32) => Ok(Instruction::I32DivS),
(BinaryOp::Divide, WasmType::F32) => Ok(Instruction::F32Div),
(BinaryOp::Modulo, WasmType::I32) => Ok(Instruction::I32RemS),
(BinaryOp::Modulo, WasmType::F32) => Err("Modulo not supported for floats".to_string()),
(BinaryOp::Equal, WasmType::I32) => Ok(Instruction::I32Eq),
(BinaryOp::Equal, WasmType::F32) => Ok(Instruction::F32Eq),
(BinaryOp::NotEqual, WasmType::I32) => Ok(Instruction::I32Ne),
(BinaryOp::NotEqual, WasmType::F32) => Ok(Instruction::F32Ne),
(BinaryOp::Less, WasmType::I32) => Ok(Instruction::I32LtS),
(BinaryOp::Less, WasmType::F32) => Ok(Instruction::F32Lt),
(BinaryOp::Greater, WasmType::I32) => Ok(Instruction::I32GtS),
(BinaryOp::Greater, WasmType::F32) => Ok(Instruction::F32Gt),
(BinaryOp::LessEqual, WasmType::I32) => Ok(Instruction::I32LeS),
(BinaryOp::LessEqual, WasmType::F32) => Ok(Instruction::F32Le),
(BinaryOp::GreaterEqual, WasmType::I32) => Ok(Instruction::I32GeS),
(BinaryOp::GreaterEqual, WasmType::F32) => Ok(Instruction::F32Ge),
(BinaryOp::And, _) => Ok(Instruction::I32And),
(BinaryOp::Or, _) => Ok(Instruction::I32Or),
_ => Err(format!("Unsupported binary operation: {op:?}")),
}
}
/// Lower an if expression to WASM instructions
/// Complexity: 4 (Toyota Way: <10 ✓)
fn lower_if(
&self,
condition: &Expr,
then_branch: &Expr,
else_branch: Option<&Expr>,
) -> Result<Vec<Instruction<'static>>, String> {
let mut instructions = vec![];
// Emit condition
instructions.extend(self.lower_expression(condition)?);
// Determine block type based on whether branches produce values
let block_type = if self.expression_produces_value(then_branch) {
wasm_encoder::BlockType::Result(wasm_encoder::ValType::I32)
} else {
wasm_encoder::BlockType::Empty
};
// If instruction
instructions.push(Instruction::If(block_type));
// Then branch
instructions.extend(self.lower_expression(then_branch)?);
// Else branch (if present)
if let Some(else_expr) = else_branch {
instructions.push(Instruction::Else);
instructions.extend(self.lower_expression(else_expr)?);
} else if self.expression_produces_value(then_branch) {
// If no else branch but we expect a value, push a default
instructions.push(Instruction::Else);
instructions.push(Instruction::I32Const(0));
}
// End if
instructions.push(Instruction::End);
Ok(instructions)
}
/// Lower a unary operation to WASM instructions
/// Complexity: 6 (Toyota Way: <10 ✓)
fn lower_unary(
&self,
op: &crate::frontend::ast::UnaryOp,
operand: &Expr,
) -> Result<Vec<Instruction<'static>>, String> {
let mut instructions = vec![];
// Emit operand
instructions.extend(self.lower_expression(operand)?);
// Emit unary operation
match op {
crate::frontend::ast::UnaryOp::Negate => {
self.emit_negate(&mut instructions, operand);
}
crate::frontend::ast::UnaryOp::Not => {
// Logical not: compare with 0
instructions.push(Instruction::I32Eqz);
}
crate::frontend::ast::UnaryOp::BitwiseNot => {
// Bitwise not: XOR with -1
instructions.push(Instruction::I32Const(-1));
instructions.push(Instruction::I32Xor);
}
crate::frontend::ast::UnaryOp::Reference
| crate::frontend::ast::UnaryOp::MutableReference
| crate::frontend::ast::UnaryOp::Deref => {
// Reference/dereference operators not supported in WASM (needs memory)
// Keep operand value on stack (PARSER-085: Issue #71)
}
}
Ok(instructions)
}
/// Emit type-aware negation instruction
/// Complexity: 1 (Toyota Way: <10 ✓)
fn emit_negate(&self, instructions: &mut Vec<Instruction<'static>>, operand: &Expr) {
let operand_ty = self.infer_type(operand);
match operand_ty {
WasmType::I32 => {
instructions.insert(0, Instruction::I32Const(0));
instructions.push(Instruction::I32Sub);
}
WasmType::F32 => {
instructions.push(Instruction::F32Neg);
}
WasmType::I64 => {
instructions.insert(0, Instruction::I64Const(0));
instructions.push(Instruction::I64Sub);
}
WasmType::F64 => {
instructions.push(Instruction::F64Neg);
}
}
}
/// Lower a block expression to WASM instructions
/// Complexity: 3 (Toyota Way: <10 ✓)
fn lower_block(&self, exprs: &[Expr]) -> Result<Vec<Instruction<'static>>, String> {
let mut instructions = vec![];
for (i, expr) in exprs.iter().enumerate() {
instructions.extend(self.lower_expression(expr)?);
// Drop intermediate values (keep only last if block produces value)
let is_last = i == exprs.len() - 1;
if !is_last && self.expression_produces_value(expr) {
instructions.push(Instruction::Drop);
}
}
Ok(instructions)
}
/// Lower a Ruchy expression to WASM instructions
/// Complexity: 4 (Toyota Way: <10 ✓)
fn lower_expression(&self, expr: &Expr) -> Result<Vec<Instruction<'static>>, String> {
match &expr.kind {
ExprKind::Literal(literal) => self.lower_literal(literal),
ExprKind::Binary { op, left, right } => self.lower_binary(op, left, right),
ExprKind::Block(exprs) => self.lower_block(exprs),
ExprKind::If {
condition,
then_branch,
else_branch,
} => self.lower_if(condition, then_branch, else_branch.as_deref()),
ExprKind::While {
condition, body, ..
} => self.lower_while(condition, body),
ExprKind::Function { .. } => Ok(vec![]),
ExprKind::Lambda { .. } => Ok(vec![]), // Lambda definitions generate no instructions
ExprKind::Call { func, args } => self.lower_call(func, args),
ExprKind::Let {
name, value, body, ..
} => self.lower_let(name, value, body),
ExprKind::LetPattern {
pattern,
value,
body,
..
} => self.lower_let_pattern(pattern, value, body),
ExprKind::Identifier(name) => self.lower_identifier(name),
ExprKind::Unary { op, operand } => self.lower_unary(op, operand),
ExprKind::List(items) => self.lower_list(items),
ExprKind::Return { value } => self.lower_return(value.as_deref()),
ExprKind::StringInterpolation { parts } => self.lower_string_interpolation(parts),
ExprKind::Match { expr, arms } => self.lower_match(expr, arms),
ExprKind::Tuple(elements) => self.lower_tuple(elements),
ExprKind::FieldAccess { object, field } => self.lower_field_access(object, field),
ExprKind::StructLiteral { name, fields, .. } => self.lower_struct_literal(name, fields),
ExprKind::IndexAccess { object, index } => self.lower_index_access(object, index),
ExprKind::Assign { target, value } => self.lower_assign(target, value),
_ => Ok(vec![]),
}
}
/// Lower a while loop to WASM instructions
/// Complexity: 4 (Toyota Way: <10 ✓)
fn lower_while(
&self,
condition: &Expr,
body: &Expr,
) -> Result<Vec<Instruction<'static>>, String> {
let mut instructions = vec![];
instructions.push(Instruction::Loop(wasm_encoder::BlockType::Empty));
instructions.extend(self.lower_expression(condition)?);
instructions.push(Instruction::I32Eqz);
instructions.push(Instruction::BrIf(1));
instructions.extend(self.lower_expression(body)?);
instructions.push(Instruction::Br(0));
instructions.push(Instruction::End);
Ok(instructions)
}
/// Lower a function call to WASM instructions
/// Complexity: 8 (Toyota Way: <10 ✓)
fn lower_call(&self, func: &Expr, args: &[Expr]) -> Result<Vec<Instruction<'static>>, String> {
let mut instructions = vec![];
// Determine function index and handle built-in functions specially
let func_index = if let ExprKind::Identifier(name) = &func.kind {
if matches!(name.as_str(), "println" | "print" | "eprintln" | "eprint") {
// Built-in print functions only take one argument in WASM
// Find the first non-string argument to print
// Function index 0: println_i32 - (i32) -> ()
// Function index 1: println_f32 - (f32) -> ()
let mut selected_arg: Option<&Expr> = None;
let mut func_idx = 0u32;
for arg in args {
// Skip string literals (format strings)
if matches!(&arg.kind, ExprKind::Literal(Literal::String(_))) {
continue;
}
// Skip string interpolations (format strings)
if matches!(&arg.kind, ExprKind::StringInterpolation { .. }) {
continue;
}
selected_arg = Some(arg);
let arg_type = self.infer_type(arg);
func_idx = match arg_type {
WasmType::F32 => 1, // Use println_f32
_ => 0, // Use println_i32 (default)
};
break;
}
// Push only the selected argument
if let Some(arg) = selected_arg {
instructions.extend(self.lower_expression(arg)?);
} else if let Some(first_arg) = args.first() {
// No non-string arg found, use first arg (handles println("hello"))
instructions.extend(self.lower_expression(first_arg)?);
}
func_idx
} else {
// Regular function call - push all arguments
for arg in args {
instructions.extend(self.lower_expression(arg)?);
}
// Look up user-defined function index (extract index from tuple)
self.functions
.borrow()
.get(name)
.map(|&(idx, _)| idx)
.ok_or_else(|| format!("Unknown function: {name}"))?
}
} else {
// Non-identifier function (should not happen in normal code)
for arg in args {
instructions.extend(self.lower_expression(arg)?);
}
return Err("Function calls must use identifiers".to_string());
};
instructions.push(Instruction::Call(func_index));
Ok(instructions)
}
/// Lower a let binding to WASM instructions
/// Complexity: 4 (Toyota Way: <10 ✓)
fn lower_let(
&self,
name: &str,
value: &Expr,
body: &Expr,
) -> Result<Vec<Instruction<'static>>, String> {
let mut instructions = vec![];
instructions.extend(self.lower_expression(value)?);
let local_index = self.symbols.borrow().lookup_index(name).unwrap_or(0);
instructions.push(Instruction::LocalSet(local_index));
// Track tuple element types for mixed-type support
if let ExprKind::Tuple(elements) = &value.kind {
let element_types: Vec<WasmType> = elements
.iter()
.map(|e| self.infer_element_type(e))
.collect();
self.tuple_types
.borrow_mut()
.insert(name.to_string(), element_types);
}
if !matches!(&body.kind, ExprKind::Literal(Literal::Unit)) {
instructions.extend(self.lower_expression(body)?);
}
Ok(instructions)
}
/// Lower a let pattern binding to WASM instructions
/// Complexity: 7 (Toyota Way: <10 ✓)
///
/// For tuples: Loads each element from memory and stores to pattern variables
/// Example: `let (x, y) = (3, 4)` loads values from tuple memory into x and y
fn lower_let_pattern(
&self,
pattern: &Pattern,
value: &Expr,
body: &Expr,
) -> Result<Vec<Instruction<'static>>, String> {
let mut instructions = vec![];
// Evaluate the value expression (returns address for tuples)
instructions.extend(self.lower_expression(value)?);
// Store to all identifiers in the pattern
// For tuples: loads from memory at address + offset
self.store_pattern_values(pattern, &mut instructions)?;
// Evaluate the body
if !matches!(&body.kind, ExprKind::Literal(Literal::Unit)) {
instructions.extend(self.lower_expression(body)?);
}
Ok(instructions)
}
/// Store value on stack to all identifiers in pattern
/// Complexity: 9 (Toyota Way: <10 ✓)
///
/// For tuple patterns, loads each element from memory and stores to locals
fn store_pattern_values(
&self,
pattern: &Pattern,
instructions: &mut Vec<Instruction<'static>>,
) -> Result<(), String> {
match pattern {
Pattern::Identifier(name) => {
let local_index = self.symbols.borrow().lookup_index(name).unwrap_or(0);
instructions.push(Instruction::LocalSet(local_index));
Ok(())
}
Pattern::Tuple(patterns) => {
// Real implementation: Load each tuple element from memory
// Value on stack is the tuple address
// Use temp local to save the tuple address
let temp_local = self.symbols.borrow().local_count();
instructions.push(Instruction::LocalSet(temp_local));
// Load each tuple element and store to pattern variable
for (i, p) in patterns.iter().enumerate() {
// Get tuple address
instructions.push(Instruction::LocalGet(temp_local));
// Load element at offset (index * 4 bytes)
let offset = i as i32 * 4;
instructions.push(Instruction::I32Load(wasm_encoder::MemArg {
offset: offset as u64,
align: 2, // 4-byte alignment
memory_index: 0,
}));
// Store to pattern variable
self.store_pattern_values(p, instructions)?;
}
Ok(())
}
Pattern::Wildcard => {
// Wildcard: pop value from stack without storing
instructions.push(Instruction::Drop);
Ok(())
}
_ => Err(format!("Pattern {pattern:?} not yet supported in WASM")),
}
}
/// Lower an identifier to WASM instructions
/// Complexity: 1 (Toyota Way: <10 ✓)
fn lower_identifier(&self, name: &str) -> Result<Vec<Instruction<'static>>, String> {
let local_index = self.symbols.borrow().lookup_index(name).unwrap_or(0);
Ok(vec![Instruction::LocalGet(local_index)])
}
/// Lower a list/array literal to WASM instructions
/// Complexity: 9 (Toyota Way: <10 ✓)
///
/// Allocates memory for array elements and stores them sequentially
/// Returns the address of the array in memory
///
/// Memory layout: Each element is 4 bytes (i32), same as tuples
/// Example: [1, 2, 3] -> [addr+0: 1, addr+4: 2, addr+8: 3]
fn lower_list(&self, elements: &[Expr]) -> Result<Vec<Instruction<'static>>, String> {
let mut instructions = vec![];
// Empty array: return placeholder 0 (no allocation needed)
if elements.is_empty() {
return Ok(vec![Instruction::I32Const(0)]);
}
// Calculate size needed: 4 bytes per element (all i32 for MVP)
let size = elements.len() as i32 * 4;
// Temporary local index: last local (reserved in collect_local_types)
let temp_local = self.symbols.borrow().local_count();
// Inline malloc: allocate memory using bump allocator
// 1. Get current heap pointer (global 0) and save it
instructions.push(Instruction::GlobalGet(0));
instructions.push(Instruction::LocalSet(temp_local));
// 2. Update heap pointer: old_ptr + size
instructions.push(Instruction::GlobalGet(0));
instructions.push(Instruction::I32Const(size));
instructions.push(Instruction::I32Add);
instructions.push(Instruction::GlobalSet(0));
// 3. Store each array element in memory
for (i, element) in elements.iter().enumerate() {
let offset = i as i32 * 4;
// Get the base address
instructions.push(Instruction::LocalGet(temp_local));
// Evaluate the element value
instructions.extend(self.lower_expression(element)?);
// Store at address + offset
instructions.push(Instruction::I32Store(wasm_encoder::MemArg {
offset: offset as u64,
align: 2, // 4-byte alignment (2^2 = 4)
memory_index: 0,
}));
}
// 4. Return the base address
instructions.push(Instruction::LocalGet(temp_local));
Ok(instructions)
}
/// Lower a tuple literal to WASM instructions
/// Complexity: 9 (Toyota Way: <10 ✓)
///
/// Allocates memory for tuple elements and stores them sequentially
/// Returns the address of the tuple in memory
///
/// Memory layout: Each element is 4 bytes (i32)
/// Example: (3, 4) -> [addr+0: 3, addr+4: 4]
fn lower_tuple(&self, elements: &[Expr]) -> Result<Vec<Instruction<'static>>, String> {
let mut instructions = vec![];
// Empty tuple: return placeholder 0 (no allocation needed)
if elements.is_empty() {
return Ok(vec![Instruction::I32Const(0)]);
}
// Calculate size needed: 4 bytes per element (all i32 for MVP)
let size = elements.len() as i32 * 4;
// Temporary local index: last local (reserved in collect_local_types)
let temp_local = self.symbols.borrow().local_count();
// Inline malloc: allocate memory using bump allocator
// 1. Get current heap pointer (global 0) and save it
instructions.push(Instruction::GlobalGet(0));
instructions.push(Instruction::LocalSet(temp_local));
// 2. Update heap pointer: old_ptr + size
instructions.push(Instruction::GlobalGet(0));
instructions.push(Instruction::I32Const(size));
instructions.push(Instruction::I32Add);
instructions.push(Instruction::GlobalSet(0));
// 3. Store each tuple element in memory (with correct type)
let element_types: Vec<WasmType> = elements
.iter()
.map(|e| self.infer_element_type(e))
.collect();
for (i, element) in elements.iter().enumerate() {
let offset = i as i32 * 4;
let elem_type = element_types[i];
// Get the base address
instructions.push(Instruction::LocalGet(temp_local));
// Evaluate the element value
instructions.extend(self.lower_expression(element)?);
// Store at address + offset (use correct store instruction per type)
match elem_type {
WasmType::F32 => {
instructions.push(Instruction::F32Store(wasm_encoder::MemArg {
offset: offset as u64,
align: 2, // 4-byte alignment (2^2 = 4)
memory_index: 0,
}));
}
_ => {
instructions.push(Instruction::I32Store(wasm_encoder::MemArg {
offset: offset as u64,
align: 2, // 4-byte alignment (2^2 = 4)
memory_index: 0,
}));
}
}
}
// 4. Return the base address
instructions.push(Instruction::LocalGet(temp_local));
Ok(instructions)
}
/// Lower field access to WASM instructions
/// Complexity: 9 (Toyota Way: <10 ✓)
///
/// Loads field value from memory
/// For tuples: field is numeric index ("0", "1", "2", ...)
/// For structs: looks up field offset from struct registry
fn lower_field_access(
&self,
object: &Expr,
field: &str,
) -> Result<Vec<Instruction<'static>>, String> {
let mut instructions = vec![];
// Evaluate object expression (should return address for tuples/structs)
instructions.extend(self.lower_expression(object)?);
// Determine field type and offset
let (offset, field_type) = if let Ok(index) = field.parse::<i32>() {
// Tuple field access: "0", "1", "2", etc.
let offset = index * 4; // Each element is 4 bytes
// Look up tuple element type if object is an identifier
let elem_type = if let ExprKind::Identifier(name) = &object.kind {
self.tuple_types
.borrow()
.get(name)
.and_then(|types| types.get(index as usize))
.copied()
.unwrap_or(WasmType::I32)
} else {
WasmType::I32 // Default for non-identifier tuples
};
(offset, elem_type)
} else {
// Struct field access: look up field in struct registry
let structs = self.structs.borrow();
let field_index = structs
.values()
.find_map(|fields| fields.iter().position(|f| f == field));
match field_index {
Some(index) => (index as i32 * 4, WasmType::I32),
None => (0, WasmType::I32), // Field not found - use 0 as fallback
}
};
// Load value from memory using correct instruction for field type
match field_type {
WasmType::F32 => {
instructions.push(Instruction::F32Load(wasm_encoder::MemArg {
offset: offset as u64,
align: 2, // 4-byte alignment (2^2 = 4)
memory_index: 0,
}));
}
_ => {
instructions.push(Instruction::I32Load(wasm_encoder::MemArg {
offset: offset as u64,
align: 2, // 4-byte alignment (2^2 = 4)
memory_index: 0,
}));
}
}
Ok(instructions)
}
/// Lower struct literal to WASM instructions
/// Complexity: 10 (Toyota Way: ≤10 ✓)
///
/// Allocates memory for struct fields and stores them in field order
/// Returns the address of the struct in memory
///
/// Memory layout: Fields stored in definition order, 4 bytes per i32 field
/// Example: Point { x: 3, y: 4 } with fields [x, y] → [addr+0: 3, addr+4: 4]
fn lower_struct_literal(
&self,
name: &str,
fields: &[(String, Expr)],
) -> Result<Vec<Instruction<'static>>, String> {
let mut instructions = vec![];
// Look up struct definition to get field order
let field_order = self.structs.borrow().get(name).cloned();
let field_order = match field_order {
Some(order) => order,
None => {
// Struct not defined - return placeholder for now
// This can happen if struct is defined in external module
return Ok(vec![Instruction::I32Const(0)]);
}
};
// Calculate size needed: 4 bytes per field (all i32 for MVP)
let size = field_order.len() as i32 * 4;
// Temporary local index: last local (reserved in collect_local_types)
let temp_local = self.symbols.borrow().local_count();
// Inline malloc: allocate memory using bump allocator
// 1. Get current heap pointer (global 0) and save it
instructions.push(Instruction::GlobalGet(0));
instructions.push(Instruction::LocalSet(temp_local));
// 2. Update heap pointer: old_ptr + size
instructions.push(Instruction::GlobalGet(0));
instructions.push(Instruction::I32Const(size));
instructions.push(Instruction::I32Add);
instructions.push(Instruction::GlobalSet(0));
// 3. Store each field at correct offset based on field order
for (field_name, field_value) in fields {
// Find field index in definition order
let field_index = field_order
.iter()
.position(|f| f == field_name)
.unwrap_or(0);
let offset = field_index as i32 * 4;
// Get the base address
instructions.push(Instruction::LocalGet(temp_local));
// Evaluate the field value
instructions.extend(self.lower_expression(field_value)?);
// Store at address + offset
instructions.push(Instruction::I32Store(wasm_encoder::MemArg {
offset: offset as u64,
align: 2, // 4-byte alignment (2^2 = 4)
memory_index: 0,
}));
}
// 4. Return the base address
instructions.push(Instruction::LocalGet(temp_local));
Ok(instructions)
}
/// Lower index access to WASM instructions
/// Complexity: 6 (Toyota Way: <10 ✓)
///
/// Loads array/tuple element from memory using dynamic index
/// Computes offset at runtime: `base_address` + (index * 4)
fn lower_index_access(
&self,
object: &Expr,
index: &Expr,
) -> Result<Vec<Instruction<'static>>, String> {
let mut instructions = vec![];
// 1. Evaluate object to get base address
instructions.extend(self.lower_expression(object)?);
// 2. Evaluate index (runtime value)
instructions.extend(self.lower_expression(index)?);
// 3. Compute offset: index * 4 (each element is 4 bytes)
instructions.push(Instruction::I32Const(4));
instructions.push(Instruction::I32Mul);
// 4. Add base address + offset
instructions.push(Instruction::I32Add);
// 5. Load value from computed address
instructions.push(Instruction::I32Load(wasm_encoder::MemArg {
offset: 0, // Offset already computed, no additional offset needed
align: 2, // 4-byte alignment (2^2 = 4)
memory_index: 0,
}));
Ok(instructions)
}
/// Lower assignment expression to WASM instructions
/// Complexity: 10 (Toyota Way: ≤10 ✓)
///
/// Supports identifiers, field access, and index access
/// Field mutations now work with real memory stores
fn lower_assign(
&self,
target: &Expr,
value: &Expr,
) -> Result<Vec<Instruction<'static>>, String> {
let mut instructions = vec![];
match &target.kind {
ExprKind::Identifier(name) => {
// Standard local variable assignment
instructions.extend(self.lower_expression(value)?);
let local_index = self.symbols.borrow().lookup_index(name).unwrap_or(0);
instructions.push(Instruction::LocalSet(local_index));
}
ExprKind::FieldAccess { object, field } => {
// Field mutation: store value to memory at address + offset
// 1. Evaluate object to get base address
instructions.extend(self.lower_expression(object)?);
// 2. Evaluate value
instructions.extend(self.lower_expression(value)?);
// 3. Calculate field offset (same logic as lower_field_access)
let offset = if let Ok(index) = field.parse::<i32>() {
index * 4 // Tuple field
} else {
// Struct field: look up in registry
let structs = self.structs.borrow();
let field_index = structs
.values()
.find_map(|fields| fields.iter().position(|f| f == field));
match field_index {
Some(index) => index as i32 * 4,
None => 0,
}
};
// 4. Store value at address + offset
instructions.push(Instruction::I32Store(wasm_encoder::MemArg {
offset: offset as u64,
align: 2, // 4-byte alignment
memory_index: 0,
}));
}
ExprKind::IndexAccess { object, index } => {
// Array element mutation: store value to memory at address + dynamic offset
// 1. Evaluate object to get base address
instructions.extend(self.lower_expression(object)?);
// 2. Evaluate index (runtime value)
instructions.extend(self.lower_expression(index)?);
// 3. Compute offset: index * 4
instructions.push(Instruction::I32Const(4));
instructions.push(Instruction::I32Mul);
// 4. Add base + offset to get final address
instructions.push(Instruction::I32Add);
// 5. Evaluate value
instructions.extend(self.lower_expression(value)?);
// 6. Store value at computed address
instructions.push(Instruction::I32Store(wasm_encoder::MemArg {
offset: 0, // Offset already computed
align: 2, // 4-byte alignment
memory_index: 0,
}));
}
_ => {
return Err(format!(
"Assignment target {:?} not supported in WASM",
target.kind
))
}
}
Ok(instructions)
}
/// Lower a return statement to WASM instructions
/// Complexity: 2 (Toyota Way: <10 ✓)
fn lower_return(&self, value: Option<&Expr>) -> Result<Vec<Instruction<'static>>, String> {
let mut instructions = vec![];
if let Some(val) = value {
instructions.extend(self.lower_expression(val)?);
}
instructions.push(Instruction::Return);
Ok(instructions)
}
/// Lower string interpolation to WASM instructions
/// Complexity: 8 (Toyota Way: <10 ✓)
///
/// Current implementation: MVP string interpolation support
/// - Text-only f-strings: concatenated into single string literal
/// - F-strings with expressions: evaluated and represented as i32 (memory pointer)
///
/// WASM strings are represented as i32 pointers to linear memory.
/// Full string concatenation requires host function support (`string_concat`).
/// This is implemented in stages per `docs/specifications/wasm-fstring-spec.md`
fn lower_string_interpolation(
&self,
parts: &[StringPart],
) -> Result<Vec<Instruction<'static>>, String> {
// Stage 1: Text-only f-strings
let all_text = parts.iter().all(|p| matches!(p, StringPart::Text(_)));
if all_text {
// Concatenate all text parts into a single string literal
let text: String = parts
.iter()
.filter_map(|p| {
if let StringPart::Text(s) = p {
Some(s.as_str())
} else {
None
}
})
.collect();
// Lower as a string literal (i32 memory pointer)
self.lower_literal(&Literal::String(text))
} else {
// Stage 2: F-strings with expressions
// Strategy: For single-expression f-strings, evaluate and return the expression value
// Multi-part f-strings require host function support for concatenation
// Check if this is a simple case: single expression, no text
if parts.len() == 1 {
if let StringPart::Expr(expr) = &parts[0] {
// Single expression: just evaluate it
return self.lower_expression(expr);
}
}
// For multi-part f-strings: requires host function support
// For now, evaluate first expression if present (partial fix)
for part in parts {
if let StringPart::Expr(expr) | StringPart::ExprWithFormat { expr, .. } = part {
return self.lower_expression(expr);
}
}
// No expressions found: return placeholder
Ok(vec![Instruction::I32Const(0)])
}
}
/// Lower match expression to WASM instructions
/// Complexity: 9 (Toyota Way: <10 ✓)
///
/// Strategy: Desugar match into cascading if-else expressions
/// Reference: `docs/specifications/wasm-match-spec.md`
///
/// # Examples
///
/// ```ignore
/// match x { 1 => 10, 2 => 20, _ => 0 }
/// // Becomes:
/// if x == 1 { 10 } else if x == 2 { 20 } else { 0 }
/// ```
fn lower_match(
&self,
match_expr: &Expr,
arms: &[crate::frontend::ast::MatchArm],
) -> Result<Vec<Instruction<'static>>, String> {
if arms.is_empty() {
return Ok(vec![Instruction::I32Const(0)]);
}
// Build cascading if-else from match arms
// Start from the last arm (usually wildcard) and work backwards
let mut result_instructions = vec![];
// Process arms in reverse to build nested if-else
for (i, arm) in arms.iter().enumerate().rev() {
let is_last = i == arms.len() - 1;
match &arm.pattern {
Pattern::Wildcard => {
// Wildcard: just emit the body
result_instructions = self.lower_expression(&arm.body)?;
}
Pattern::Literal(lit) => {
if is_last {
// Last arm without wildcard: just emit body
result_instructions = self.lower_expression(&arm.body)?;
} else {
// Compare match_expr with pattern literal
let mut instr = vec![];
instr.extend(self.lower_expression(match_expr)?);
instr.extend(self.lower_literal(lit)?);
instr.push(Instruction::I32Eq);
// if (condition) { arm.body } else { rest }
let then_body = self.lower_expression(&arm.body)?;
let else_body = result_instructions;
// Determine result type from body
let result_type = self.infer_wasm_type(&arm.body);
instr.push(Instruction::If(wasm_encoder::BlockType::Result(
result_type,
)));
instr.extend(then_body);
instr.push(Instruction::Else);
instr.extend(else_body);
instr.push(Instruction::End);
result_instructions = instr;
}
}
Pattern::Or(patterns) => {
if is_last {
// Last arm: just emit body
result_instructions = self.lower_expression(&arm.body)?;
} else {
// OR pattern: match_expr == pat1 || match_expr == pat2 || ...
let mut instr = vec![];
// Build comparison for each pattern in OR
for (j, pattern) in patterns.iter().enumerate() {
// Compare match_expr with this pattern
instr.extend(self.lower_expression(match_expr)?);
if let Pattern::Literal(lit) = pattern {
instr.extend(self.lower_literal(lit)?);
instr.push(Instruction::I32Eq);
} else {
return Err(format!(
"Non-literal patterns in OR not yet supported: {pattern:?}"
));
}
// OR with previous comparison (except for first)
if j > 0 {
instr.push(Instruction::I32Or);
}
}
// if (any match) { arm.body } else { rest }
let then_body = self.lower_expression(&arm.body)?;
let else_body = result_instructions;
let result_type = self.infer_wasm_type(&arm.body);
instr.push(Instruction::If(wasm_encoder::BlockType::Result(
result_type,
)));
instr.extend(then_body);
instr.push(Instruction::Else);
instr.extend(else_body);
instr.push(Instruction::End);
result_instructions = instr;
}
}
Pattern::Tuple(_patterns) => {
// MVP: Tuple patterns in match always succeed (placeholder)
// Full implementation would destructure and compare tuple elements
if is_last {
result_instructions = self.lower_expression(&arm.body)?;
} else {
// For now, treat tuple patterns as "always match" (like wildcard)
// This allows code to compile but doesn't do proper pattern matching
result_instructions = self.lower_expression(&arm.body)?;
}
}
Pattern::Identifier(_name) => {
// Identifier pattern: binds the value to a variable (always matches)
result_instructions = self.lower_expression(&arm.body)?;
}
_ => {
// Other patterns not yet supported in MVP
return Err(format!(
"Pattern {:?} not yet supported in WASM",
arm.pattern
));
}
}
}
Ok(result_instructions)
}
/// Build function index map for resolving function calls
/// Complexity: 3 (Toyota Way: <10 ✓)
///
/// Function indices in WASM:
/// - Import functions come first (e.g., println at index 0 if imports exist)
/// - User-defined functions follow
fn build_function_index_map(
&self,
expr: &Expr,
func_defs: &[(String, Vec<crate::frontend::ast::Param>, Box<Expr>)],
) {
let mut index_map = std::collections::HashMap::new();
// Calculate offset: imports come first (2 built-ins: println_i32 and println_f32)
let import_offset = if utils::uses_builtins(expr) { 2 } else { 0 };
// Map each user function to (index, is_void)
for (i, (name, _, body)) in func_defs.iter().enumerate() {
let index = (i as u32) + import_offset;
let returns_value =
utils::has_return_with_value(body) || self.expression_produces_value(body);
let is_void = !returns_value;
index_map.insert(name.clone(), (index, is_void));
}
*self.functions.borrow_mut() = index_map;
}
/// Collect all function definitions from the AST
pub(crate) fn collect_functions(
&self,
expr: &Expr,
) -> Vec<(String, Vec<crate::frontend::ast::Param>, Box<Expr>)> {
let mut functions = Vec::new();
Self::collect_functions_rec(expr, &mut functions);
functions
}
fn collect_functions_rec(
expr: &Expr,
functions: &mut Vec<(String, Vec<crate::frontend::ast::Param>, Box<Expr>)>,
) {
match &expr.kind {
ExprKind::Function {
name, params, body, ..
} => {
functions.push((name.clone(), params.clone(), body.clone()));
}
ExprKind::Let {
name, value, body, ..
} => {
// Check if the value is a lambda (closure)
if let ExprKind::Lambda {
params,
body: lambda_body,
} = &value.kind
{
functions.push((name.clone(), params.clone(), lambda_body.clone()));
}
// Continue recursing into the let body
Self::collect_functions_rec(body, functions);
}
ExprKind::Block(exprs) => {
for e in exprs {
Self::collect_functions_rec(e, functions);
}
}
_ => {}
}
}
/// Get non-function code from the expression (e.g., function calls)
fn get_non_function_code(&self, expr: &Expr) -> Option<Expr> {
match &expr.kind {
ExprKind::Block(exprs) => {
let non_func_exprs: Vec<Expr> = exprs
.iter()
.filter(|e| {
// Filter out function definitions
if matches!(e.kind, ExprKind::Function { .. }) {
return false;
}
// Filter out let bindings that bind lambdas
if let ExprKind::Let { value, .. } = &e.kind {
if matches!(value.kind, ExprKind::Lambda { .. }) {
return false;
}
}
true
})
.cloned()
.collect();
if non_func_exprs.is_empty() {
None
} else if non_func_exprs.len() == 1 {
Some(
non_func_exprs
.into_iter()
.next()
.expect("non_func_exprs.len() == 1, so next() must return Some"),
)
} else {
Some(Expr::new(ExprKind::Block(non_func_exprs), expr.span))
}
}
ExprKind::Function { .. } => None,
_ => Some(expr.clone()),
}
}
// NOTE: uses_builtins, needs_memory, has_main_function, has_return_with_value,
// needs_locals moved to utils.rs
/// Check if an expression produces a value on the stack
/// Complexity: 8 (Toyota Way: <10 ✓)
fn expression_produces_value(&self, expr: &Expr) -> bool {
match &expr.kind {
ExprKind::Literal(_) => true,
ExprKind::Binary { .. } => true,
ExprKind::Unary { .. } => true,
ExprKind::Identifier(_) => true,
ExprKind::Call { func, .. } => {
// Check if this is a void function
if let ExprKind::Identifier(name) = &func.kind {
// Built-in void functions
if matches!(name.as_str(), "println" | "print" | "eprintln" | "eprint") {
return false;
}
// User-defined functions - check registry
if let Some(&(_idx, is_void)) = self.functions.borrow().get(name) {
return !is_void;
}
// Unknown function - assume it produces a value
true
} else {
true
}
}
ExprKind::List(_) => true,
ExprKind::Block(exprs) => {
// Block produces value if last expression does
exprs
.last()
.is_some_and(|e| self.expression_produces_value(e))
}
ExprKind::If {
then_branch,
else_branch,
..
} => {
// If produces value only if both branches produce values
let then_produces = self.expression_produces_value(then_branch);
let else_produces = else_branch
.as_ref()
.is_some_and(|e| self.expression_produces_value(e));
then_produces && else_produces
}
ExprKind::Let { body, .. } => {
// Let produces value only if body is not Unit
match &body.kind {
ExprKind::Literal(Literal::Unit) => false,
_ => self.expression_produces_value(body),
}
}
ExprKind::Return { .. } => false, // Return doesn't leave value on stack
ExprKind::While { .. } => false, // Loops are void
ExprKind::Function { .. } => false, // Function definitions don't produce values
_ => false,
}
}
fn lower_literal(&self, literal: &Literal) -> Result<Vec<Instruction<'static>>, String> {
match literal {
Literal::Integer(n, _) => Ok(vec![Instruction::I32Const(*n as i32)]),
Literal::Float(f) => Ok(vec![Instruction::F32Const(*f as f32)]),
Literal::Bool(b) => Ok(vec![Instruction::I32Const(i32::from(*b))]),
Literal::String(_) => {
// String literals would need memory allocation
// For now, return a placeholder
Ok(vec![Instruction::I32Const(0)])
}
Literal::Unit => {
// Unit type () is represented as i32 const 0 in WASM
Ok(vec![Instruction::I32Const(0)])
}
Literal::Char(c) => {
// Character literals represented as i32 (UTF-32 code point)
Ok(vec![Instruction::I32Const(*c as i32)])
}
_ => Ok(vec![Instruction::I32Const(0)]), // Other literals default to 0
}
}
}
impl Default for WasmEmitter {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::frontend::ast::Span;
use crate::frontend::parser::Parser;
#[test]
fn test_wasm_emitter_new() {
let emitter = WasmEmitter::new();
// Just verify creation works
assert!(emitter.functions.borrow().is_empty());
}
#[test]
fn test_wasm_emitter_default() {
let emitter = WasmEmitter::default();
assert!(emitter.functions.borrow().is_empty());
}
#[test]
fn test_lower_literal_integer() {
let emitter = WasmEmitter::new();
let result = emitter.lower_literal(&Literal::Integer(42, None));
assert!(result.is_ok());
}
#[test]
fn test_lower_literal_float() {
let emitter = WasmEmitter::new();
let result = emitter.lower_literal(&Literal::Float(3.14));
assert!(result.is_ok());
}
#[test]
fn test_lower_literal_bool_true() {
let emitter = WasmEmitter::new();
let result = emitter.lower_literal(&Literal::Bool(true));
assert!(result.is_ok());
}
#[test]
fn test_lower_literal_bool_false() {
let emitter = WasmEmitter::new();
let result = emitter.lower_literal(&Literal::Bool(false));
assert!(result.is_ok());
}
#[test]
fn test_lower_literal_string() {
let emitter = WasmEmitter::new();
let result = emitter.lower_literal(&Literal::String("test".to_string()));
assert!(result.is_ok());
}
#[test]
fn test_lower_literal_unit() {
let emitter = WasmEmitter::new();
let result = emitter.lower_literal(&Literal::Unit);
assert!(result.is_ok());
}
#[test]
fn test_lower_literal_char() {
let emitter = WasmEmitter::new();
let result = emitter.lower_literal(&Literal::Char('A'));
assert!(result.is_ok());
}
#[test]
fn test_infer_element_type_via_parser() {
let emitter = WasmEmitter::new();
let mut parser = Parser::new("42");
if let Ok(ast) = parser.parse() {
let ty = emitter.infer_element_type(&ast);
// Integer literals infer to I32
let _ = ty;
}
}
#[test]
fn test_infer_element_type_float_via_parser() {
let emitter = WasmEmitter::new();
let mut parser = Parser::new("3.14");
if let Ok(ast) = parser.parse() {
let ty = emitter.infer_element_type(&ast);
let _ = ty;
}
}
#[test]
fn test_collect_tuple_types_via_parser() {
let emitter = WasmEmitter::new();
let mut parser = Parser::new("let x = (1, 2.0); x");
if let Ok(ast) = parser.parse() {
emitter.collect_tuple_types(&ast);
}
}
#[test]
fn test_structs_registration() {
let emitter = WasmEmitter::new();
// Register a struct manually
emitter
.structs
.borrow_mut()
.insert("Point".to_string(), vec!["x".to_string(), "y".to_string()]);
assert!(emitter.structs.borrow().contains_key("Point"));
}
#[test]
fn test_functions_registration() {
let emitter = WasmEmitter::new();
// Register a function manually
emitter
.functions
.borrow_mut()
.insert("main".to_string(), (0, false));
let funcs = emitter.functions.borrow();
assert!(funcs.contains_key("main"));
assert_eq!(funcs["main"], (0, false));
}
#[test]
fn test_tuple_types_registration() {
let emitter = WasmEmitter::new();
emitter
.tuple_types
.borrow_mut()
.insert("pair".to_string(), vec![WasmType::I32, WasmType::F32]);
let types = emitter.tuple_types.borrow();
assert!(types.contains_key("pair"));
assert_eq!(types["pair"].len(), 2);
}
// === COVERAGE: Additional tests for uncovered branches ===
#[test]
fn test_infer_element_type_bool() {
let emitter = WasmEmitter::new();
let expr = Expr::new(
ExprKind::Literal(Literal::Bool(true)),
Span { start: 0, end: 4 },
);
let ty = emitter.infer_element_type(&expr);
assert_eq!(ty, WasmType::I32);
}
#[test]
fn test_infer_element_type_string() {
let emitter = WasmEmitter::new();
let expr = Expr::new(
ExprKind::Literal(Literal::String("hello".to_string())),
Span { start: 0, end: 7 },
);
let ty = emitter.infer_element_type(&expr);
assert_eq!(ty, WasmType::I32); // Address
}
#[test]
fn test_infer_element_type_binary_add() {
let emitter = WasmEmitter::new();
let expr = Expr::new(
ExprKind::Binary {
op: BinaryOp::Add,
left: Box::new(Expr::new(
ExprKind::Literal(Literal::Integer(1, None)),
Span { start: 0, end: 1 },
)),
right: Box::new(Expr::new(
ExprKind::Literal(Literal::Integer(2, None)),
Span { start: 4, end: 5 },
)),
},
Span { start: 0, end: 5 },
);
let ty = emitter.infer_element_type(&expr);
assert_eq!(ty, WasmType::F32); // Binary ops return F32
}
#[test]
fn test_infer_element_type_binary_comparison() {
let emitter = WasmEmitter::new();
let expr = Expr::new(
ExprKind::Binary {
op: BinaryOp::Less,
left: Box::new(Expr::new(
ExprKind::Literal(Literal::Integer(1, None)),
Span { start: 0, end: 1 },
)),
right: Box::new(Expr::new(
ExprKind::Literal(Literal::Integer(2, None)),
Span { start: 4, end: 5 },
)),
},
Span { start: 0, end: 5 },
);
let ty = emitter.infer_element_type(&expr);
assert_eq!(ty, WasmType::I32); // Comparisons return I32
}
#[test]
fn test_infer_element_type_identifier() {
let emitter = WasmEmitter::new();
let expr = Expr::new(
ExprKind::Identifier("x".to_string()),
Span { start: 0, end: 1 },
);
let ty = emitter.infer_element_type(&expr);
assert_eq!(ty, WasmType::I32); // Default for complex expressions
}
#[test]
fn test_collect_tuple_types_block() {
let emitter = WasmEmitter::new();
let block = Expr::new(
ExprKind::Block(vec![
Expr::new(
ExprKind::Literal(Literal::Integer(1, None)),
Span { start: 0, end: 1 },
),
Expr::new(
ExprKind::Literal(Literal::Integer(2, None)),
Span { start: 3, end: 4 },
),
]),
Span { start: 0, end: 5 },
);
emitter.collect_tuple_types(&block);
// Should not panic on block traversal
}
#[test]
fn test_collect_tuple_types_if() {
let emitter = WasmEmitter::new();
let if_expr = Expr::new(
ExprKind::If {
condition: Box::new(Expr::new(
ExprKind::Literal(Literal::Bool(true)),
Span { start: 0, end: 4 },
)),
then_branch: Box::new(Expr::new(
ExprKind::Literal(Literal::Integer(1, None)),
Span { start: 6, end: 7 },
)),
else_branch: Some(Box::new(Expr::new(
ExprKind::Literal(Literal::Integer(2, None)),
Span { start: 13, end: 14 },
))),
},
Span { start: 0, end: 15 },
);
emitter.collect_tuple_types(&if_expr);
// Should not panic on if traversal
}
#[test]
fn test_lower_literal_null() {
let emitter = WasmEmitter::new();
let result = emitter.lower_literal(&Literal::Null);
assert!(result.is_ok());
}
#[test]
fn test_lower_literal_byte() {
let emitter = WasmEmitter::new();
let result = emitter.lower_literal(&Literal::Byte(255));
assert!(result.is_ok());
}
#[test]
fn test_lower_literal_atom() {
let emitter = WasmEmitter::new();
let result = emitter.lower_literal(&Literal::Atom("ok".to_string()));
assert!(result.is_ok());
}
#[test]
fn test_symbols_operations() {
let emitter = WasmEmitter::new();
// Verify symbols table starts with zero local count and depth 1
assert_eq!(emitter.symbols.borrow().local_count(), 0);
assert_eq!(emitter.symbols.borrow().scope_depth(), 1);
}
#[test]
fn test_multiple_structs_registration() {
let emitter = WasmEmitter::new();
emitter
.structs
.borrow_mut()
.insert("Point".to_string(), vec!["x".to_string(), "y".to_string()]);
emitter.structs.borrow_mut().insert(
"Rect".to_string(),
vec!["width".to_string(), "height".to_string()],
);
assert_eq!(emitter.structs.borrow().len(), 2);
}
#[test]
fn test_multiple_functions_registration() {
let emitter = WasmEmitter::new();
emitter
.functions
.borrow_mut()
.insert("main".to_string(), (0, false));
emitter
.functions
.borrow_mut()
.insert("helper".to_string(), (1, true));
assert_eq!(emitter.functions.borrow().len(), 2);
assert_eq!(emitter.functions.borrow()["helper"], (1, true));
}
#[test]
fn test_infer_element_type_binary_subtract() {
let emitter = WasmEmitter::new();
let expr = Expr::new(
ExprKind::Binary {
op: BinaryOp::Subtract,
left: Box::new(Expr::new(
ExprKind::Literal(Literal::Integer(5, None)),
Span { start: 0, end: 1 },
)),
right: Box::new(Expr::new(
ExprKind::Literal(Literal::Integer(3, None)),
Span { start: 4, end: 5 },
)),
},
Span { start: 0, end: 5 },
);
let ty = emitter.infer_element_type(&expr);
assert_eq!(ty, WasmType::F32);
}
#[test]
fn test_infer_element_type_binary_multiply() {
let emitter = WasmEmitter::new();
let expr = Expr::new(
ExprKind::Binary {
op: BinaryOp::Multiply,
left: Box::new(Expr::new(
ExprKind::Literal(Literal::Integer(2, None)),
Span { start: 0, end: 1 },
)),
right: Box::new(Expr::new(
ExprKind::Literal(Literal::Integer(3, None)),
Span { start: 4, end: 5 },
)),
},
Span { start: 0, end: 5 },
);
let ty = emitter.infer_element_type(&expr);
assert_eq!(ty, WasmType::F32);
}
#[test]
fn test_infer_element_type_binary_divide() {
let emitter = WasmEmitter::new();
let expr = Expr::new(
ExprKind::Binary {
op: BinaryOp::Divide,
left: Box::new(Expr::new(
ExprKind::Literal(Literal::Integer(10, None)),
Span { start: 0, end: 2 },
)),
right: Box::new(Expr::new(
ExprKind::Literal(Literal::Integer(2, None)),
Span { start: 5, end: 6 },
)),
},
Span { start: 0, end: 6 },
);
let ty = emitter.infer_element_type(&expr);
assert_eq!(ty, WasmType::F32);
}
#[test]
fn test_infer_element_type_binary_modulo() {
let emitter = WasmEmitter::new();
let expr = Expr::new(
ExprKind::Binary {
op: BinaryOp::Modulo,
left: Box::new(Expr::new(
ExprKind::Literal(Literal::Integer(10, None)),
Span { start: 0, end: 2 },
)),
right: Box::new(Expr::new(
ExprKind::Literal(Literal::Integer(3, None)),
Span { start: 5, end: 6 },
)),
},
Span { start: 0, end: 6 },
);
let ty = emitter.infer_element_type(&expr);
assert_eq!(ty, WasmType::F32);
}
}