Files
@ ddddcd3cc9aa
Branch filter:
Location: CSY/reowolf/src/protocol/ast.rs
ddddcd3cc9aa
76.7 KiB
application/rls-services+xml
WIP on compiler rearchitecting
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 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 | // TODO: @cleanup, rigorous cleanup of dead code and silly object-oriented
// trait impls where I deem them unfit.
use std::fmt;
use std::fmt::{Debug, Display, Formatter};
use std::ops::{Index, IndexMut};
use super::arena::{Arena, Id};
use crate::collections::StringRef;
use crate::protocol::inputsource::*;
use crate::protocol::input_source2::{InputPosition2, InputSpan};
/// Global limits to the AST, should be checked by lexer and parser. Some are
/// arbitrary
const MAX_LEVEL: usize = 128;
const MAX_NAMESPACES: usize = 64;
/// Helper macro that defines a type alias for a AST element ID. In this case
/// only used to alias the `Id<T>` types.
macro_rules! define_aliased_ast_id {
// Variant where we just defined the alias, without any indexing
($name:ident, $parent:ty) => {
pub type $name = $parent;
};
// Variant where we define the type, and the Index and IndexMut traits
(
$name:ident, $parent:ty,
index($indexed_type:ty, $indexed_arena:ident)
) => {
define_aliased_ast_id!($name, $parent);
impl Index<$name> for Heap {
type Output = $indexed_type;
fn index(&self, index: $name) -> &Self::Output {
&self.$indexed_arena[index]
}
}
impl IndexMut<$name> for Heap {
fn index_mut(&mut self, index: $name) -> &mut Self::Output {
&mut self.$indexed_arena[index]
}
}
};
// Variant where we define type, Index(Mut) traits and an allocation function
(
$name:ident, $parent:ty,
index($indexed_type:ty, $indexed_arena:ident),
alloc($fn_name:ident)
) => {
define_aliased_ast_id!($name, $parent, index($indexed_type, $indexed_arena));
impl Heap {
pub fn $fn_name(&mut self, f: impl FnOnce($name) -> $indexed_type) -> $name {
self.$indexed_arena.alloc_with_id(|id| f(id))
}
}
};
}
/// Helper macro that defines a wrapper type for a particular variant of an AST
/// element ID. Only used to define single-wrapping IDs.
macro_rules! define_new_ast_id {
// Variant where we just defined the new type, without any indexing
($name:ident, $parent:ty) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct $name (pub(crate) $parent);
impl $name {
pub(crate) fn new_invalid() -> Self { Self($parent::new_invalid()) }
pub(crate) fn is_invalid(&self) -> bool { self.0.is_invalid() }
pub fn upcast(self) -> $parent { self.0 }
}
};
// Variant where we define the type, and the Index and IndexMut traits
(
$name:ident, $parent:ty,
index($indexed_type:ty, $wrapper_type:path, $indexed_arena:ident)
) => {
define_new_ast_id!($name, $parent);
impl Index<$name> for Heap {
type Output = $indexed_type;
fn index(&self, index: $name) -> &Self::Output {
if let $wrapper_type(v) = &self.$indexed_arena[index.0] {
v
} else {
unreachable!()
}
}
}
impl IndexMut<$name> for Heap {
fn index_mut(&mut self, index: $name) -> &mut Self::Output {
if let $wrapper_type(v) = &mut self.$indexed_arena[index.0] {
v
} else {
unreachable!()
}
}
}
};
// Variant where we define the type, the Index and IndexMut traits, and an allocation function
(
$name:ident, $parent:ty,
index($indexed_type:ty, $wrapper_type:path, $indexed_arena:ident),
alloc($fn_name:ident)
) => {
define_new_ast_id!($name, $parent, index($indexed_type, $wrapper_type, $indexed_arena));
impl Heap {
pub fn $fn_name(&mut self, f: impl FnOnce($name) -> $indexed_type) -> $name {
$name(
self.$indexed_arena.alloc_with_id(|id| {
$wrapper_type(f($name(id)))
})
)
}
}
}
}
define_aliased_ast_id!(RootId, Id<Root>, index(Root, protocol_descriptions), alloc(alloc_protocol_description));
define_aliased_ast_id!(PragmaId, Id<Pragma>, index(Pragma, pragmas), alloc(alloc_pragma));
define_aliased_ast_id!(ImportId, Id<Import>, index(Import, imports), alloc(alloc_import));
define_aliased_ast_id!(ParserTypeId, Id<ParserType>, index(ParserType, parser_types), alloc(alloc_parser_type));
define_aliased_ast_id!(VariableId, Id<Variable>, index(Variable, variables));
define_new_ast_id!(ParameterId, VariableId, index(Parameter, Variable::Parameter, variables), alloc(alloc_parameter));
define_new_ast_id!(LocalId, VariableId, index(Local, Variable::Local, variables), alloc(alloc_local));
define_aliased_ast_id!(DefinitionId, Id<Definition>, index(Definition, definitions));
define_new_ast_id!(StructDefinitionId, DefinitionId, index(StructDefinition, Definition::Struct, definitions), alloc(alloc_struct_definition));
define_new_ast_id!(EnumDefinitionId, DefinitionId, index(EnumDefinition, Definition::Enum, definitions), alloc(alloc_enum_definition));
define_new_ast_id!(UnionDefinitionId, DefinitionId, index(UnionDefinition, Definition::Union, definitions), alloc(alloc_union_definition));
define_new_ast_id!(ComponentDefinitionId, DefinitionId, index(ComponentDefinition, Definition::Component, definitions), alloc(alloc_component_definition));
define_new_ast_id!(FunctionDefinitionId, DefinitionId, index(FunctionDefinition, Definition::Function, definitions), alloc(alloc_function_definition));
define_aliased_ast_id!(StatementId, Id<Statement>, index(Statement, statements));
define_new_ast_id!(BlockStatementId, StatementId, index(BlockStatement, Statement::Block, statements), alloc(alloc_block_statement));
define_new_ast_id!(LocalStatementId, StatementId, index(LocalStatement, Statement::Local, statements), alloc(alloc_local_statement));
define_new_ast_id!(MemoryStatementId, LocalStatementId);
define_new_ast_id!(ChannelStatementId, LocalStatementId);
define_new_ast_id!(SkipStatementId, StatementId, index(SkipStatement, Statement::Skip, statements), alloc(alloc_skip_statement));
define_new_ast_id!(LabeledStatementId, StatementId, index(LabeledStatement, Statement::Labeled, statements), alloc(alloc_labeled_statement));
define_new_ast_id!(IfStatementId, StatementId, index(IfStatement, Statement::If, statements), alloc(alloc_if_statement));
define_new_ast_id!(EndIfStatementId, StatementId, index(EndIfStatement, Statement::EndIf, statements), alloc(alloc_end_if_statement));
define_new_ast_id!(WhileStatementId, StatementId, index(WhileStatement, Statement::While, statements), alloc(alloc_while_statement));
define_new_ast_id!(EndWhileStatementId, StatementId, index(EndWhileStatement, Statement::EndWhile, statements), alloc(alloc_end_while_statement));
define_new_ast_id!(BreakStatementId, StatementId, index(BreakStatement, Statement::Break, statements), alloc(alloc_break_statement));
define_new_ast_id!(ContinueStatementId, StatementId, index(ContinueStatement, Statement::Continue, statements), alloc(alloc_continue_statement));
define_new_ast_id!(SynchronousStatementId, StatementId, index(SynchronousStatement, Statement::Synchronous, statements), alloc(alloc_synchronous_statement));
define_new_ast_id!(EndSynchronousStatementId, StatementId, index(EndSynchronousStatement, Statement::EndSynchronous, statements), alloc(alloc_end_synchronous_statement));
define_new_ast_id!(ReturnStatementId, StatementId, index(ReturnStatement, Statement::Return, statements), alloc(alloc_return_statement));
define_new_ast_id!(AssertStatementId, StatementId, index(AssertStatement, Statement::Assert, statements), alloc(alloc_assert_statement));
define_new_ast_id!(GotoStatementId, StatementId, index(GotoStatement, Statement::Goto, statements), alloc(alloc_goto_statement));
define_new_ast_id!(NewStatementId, StatementId, index(NewStatement, Statement::New, statements), alloc(alloc_new_statement));
define_new_ast_id!(ExpressionStatementId, StatementId, index(ExpressionStatement, Statement::Expression, statements), alloc(alloc_expression_statement));
define_aliased_ast_id!(ExpressionId, Id<Expression>, index(Expression, expressions));
define_new_ast_id!(AssignmentExpressionId, ExpressionId, index(AssignmentExpression, Expression::Assignment, expressions), alloc(alloc_assignment_expression));
define_new_ast_id!(BindingExpressionId, ExpressionId, index(BindingExpression, Expression::Binding, expressions), alloc(alloc_binding_expression));
define_new_ast_id!(ConditionalExpressionId, ExpressionId, index(ConditionalExpression, Expression::Conditional, expressions), alloc(alloc_conditional_expression));
define_new_ast_id!(BinaryExpressionId, ExpressionId, index(BinaryExpression, Expression::Binary, expressions), alloc(alloc_binary_expression));
define_new_ast_id!(UnaryExpressionId, ExpressionId, index(UnaryExpression, Expression::Unary, expressions), alloc(alloc_unary_expression));
define_new_ast_id!(IndexingExpressionId, ExpressionId, index(IndexingExpression, Expression::Indexing, expressions), alloc(alloc_indexing_expression));
define_new_ast_id!(SlicingExpressionId, ExpressionId, index(SlicingExpression, Expression::Slicing, expressions), alloc(alloc_slicing_expression));
define_new_ast_id!(SelectExpressionId, ExpressionId, index(SelectExpression, Expression::Select, expressions), alloc(alloc_select_expression));
define_new_ast_id!(ArrayExpressionId, ExpressionId, index(ArrayExpression, Expression::Array, expressions), alloc(alloc_array_expression));
define_new_ast_id!(LiteralExpressionId, ExpressionId, index(LiteralExpression, Expression::Literal, expressions), alloc(alloc_literal_expression));
define_new_ast_id!(CallExpressionId, ExpressionId, index(CallExpression, Expression::Call, expressions), alloc(alloc_call_expression));
define_new_ast_id!(VariableExpressionId, ExpressionId, index(VariableExpression, Expression::Variable, expressions), alloc(alloc_variable_expression));
// TODO: @cleanup - pub qualifiers can be removed once done
#[derive(Debug)]
pub struct Heap {
// Root arena, contains the entry point for different modules. Each root
// contains lists of IDs that correspond to the other arenas.
pub(crate) protocol_descriptions: Arena<Root>,
// Contents of a file, these are the elements the `Root` elements refer to
pragmas: Arena<Pragma>,
pub(crate) imports: Arena<Import>,
pub(crate) parser_types: Arena<ParserType>,
pub(crate) variables: Arena<Variable>,
pub(crate) definitions: Arena<Definition>,
pub(crate) statements: Arena<Statement>,
pub(crate) expressions: Arena<Expression>,
}
impl Heap {
pub fn new() -> Heap {
Heap {
// string_alloc: StringAllocator::new(),
protocol_descriptions: Arena::new(),
pragmas: Arena::new(),
imports: Arena::new(),
parser_types: Arena::new(),
variables: Arena::new(),
definitions: Arena::new(),
statements: Arena::new(),
expressions: Arena::new(),
}
}
pub fn alloc_memory_statement(
&mut self,
f: impl FnOnce(MemoryStatementId) -> MemoryStatement,
) -> MemoryStatementId {
MemoryStatementId(LocalStatementId(self.statements.alloc_with_id(|id| {
Statement::Local(LocalStatement::Memory(
f(MemoryStatementId(LocalStatementId(id)))
))
})))
}
pub fn alloc_channel_statement(
&mut self,
f: impl FnOnce(ChannelStatementId) -> ChannelStatement,
) -> ChannelStatementId {
ChannelStatementId(LocalStatementId(self.statements.alloc_with_id(|id| {
Statement::Local(LocalStatement::Channel(
f(ChannelStatementId(LocalStatementId(id)))
))
})))
}
}
impl Index<MemoryStatementId> for Heap {
type Output = MemoryStatement;
fn index(&self, index: MemoryStatementId) -> &Self::Output {
&self.statements[index.0.0].as_memory()
}
}
impl Index<ChannelStatementId> for Heap {
type Output = ChannelStatement;
fn index(&self, index: ChannelStatementId) -> &Self::Output {
&self.statements[index.0.0].as_channel()
}
}
#[derive(Debug, Clone)]
pub struct Root {
pub this: RootId,
// Phase 1: parser
// pub position: InputPosition,
pub pragmas: Vec<PragmaId>,
pub imports: Vec<ImportId>,
pub definitions: Vec<DefinitionId>,
}
impl Root {
pub fn get_definition_ident(&self, h: &Heap, id: &[u8]) -> Option<DefinitionId> {
for &def in self.definitions.iter() {
if h[def].identifier().value == id {
return Some(def);
}
}
None
}
}
impl SyntaxElement for Root {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub enum Pragma {
Version(PragmaVersion),
Module(PragmaModule),
}
impl Pragma {
pub(crate) fn as_module(&self) -> &PragmaModule {
match self {
Pragma::Module(pragma) => pragma,
_ => unreachable!("Tried to obtain {:?} as PragmaModule", self),
}
}
}
#[derive(Debug, Clone)]
pub struct PragmaVersion {
pub this: PragmaId,
// Phase 1: parser
pub span: InputSpan, // of full pragma
pub version: u64,
}
#[derive(Debug, Clone)]
pub struct PragmaModule {
pub this: PragmaId,
// Phase 1: parser
pub span: InputSpan, // of full pragma
pub value: Identifier,
}
#[derive(Debug, Clone)]
pub enum Import {
Module(ImportModule),
Symbols(ImportSymbols)
}
impl Import {
pub(crate) fn as_module(&self) -> &ImportModule {
match self {
Import::Module(m) => m,
_ => unreachable!("Unable to cast 'Import' to 'ImportModule'")
}
}
pub(crate) fn as_symbols(&self) -> &ImportSymbols {
match self {
Import::Symbols(m) => m,
_ => unreachable!("Unable to cast 'Import' to 'ImportSymbols'")
}
}
pub(crate) fn as_symbols_mut(&mut self) -> &mut ImportSymbols {
match self {
Import::Symbols(m) => m,
_ => unreachable!("Unable to cast 'Import' to 'ImportSymbols'")
}
}
}
impl SyntaxElement for Import {
fn position(&self) -> InputPosition {
match self {
Import::Module(m) => m.position,
Import::Symbols(m) => m.position
}
}
}
#[derive(Debug, Clone)]
pub struct ImportModule {
pub this: ImportId,
// Phase 1: parser
pub span: InputSpan,
pub module: Identifier,
pub alias: Identifier,
pub module_id: RootId,
}
#[derive(Debug, Clone)]
pub struct AliasedSymbol {
pub name: Identifier,
pub alias: Option<Identifier>,
pub definition_id: DefinitionId,
}
#[derive(Debug, Clone)]
pub struct ImportSymbols {
pub this: ImportId,
// Phase 1: parser
pub span: InputSpan,
pub module: Identifier,
pub module_id: RootId,
pub symbols: Vec<AliasedSymbol>,
}
#[derive(Debug, Clone)]
pub struct Identifier {
pub span: InputSpan,
pub value: StringRef<'static>,
}
impl PartialEq for Identifier {
fn eq(&self, other: &Self) -> bool {
return self.value == other.value
}
}
impl Display for Identifier {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
// A source identifier is in ASCII range.
write!(f, "{}", String::from_utf8_lossy(&self.value))
}
}
#[derive(Debug, Clone)]
pub enum NamespacedIdentifierPart {
// Regular identifier
Identifier{start: u16, end: u16},
// Polyargs associated with a preceding identifier
PolyArgs{start: u16, end: u16},
}
impl NamespacedIdentifierPart {
pub(crate) fn is_identifier(&self) -> bool {
match self {
NamespacedIdentifierPart::Identifier{..} => true,
NamespacedIdentifierPart::PolyArgs{..} => false,
}
}
pub(crate) fn as_identifier(&self) -> (u16, u16) {
match self {
NamespacedIdentifierPart::Identifier{start, end} => (*start, *end),
NamespacedIdentifierPart::PolyArgs{..} => {
unreachable!("Tried to obtain {:?} as Identifier", self);
}
}
}
pub(crate) fn as_poly_args(&self) -> (u16, u16) {
match self {
NamespacedIdentifierPart::PolyArgs{start, end} => (*start, *end),
NamespacedIdentifierPart::Identifier{..} => {
unreachable!("Tried to obtain {:?} as PolyArgs", self)
}
}
}
}
/// An identifier with optional namespaces and polymorphic variables. Note that
/// we allow each identifier to be followed by polymorphic arguments during the
/// parsing phase (e.g. Foo<A,B>::Bar<C,D>::Qux). But in our current language
/// implementation we can only have valid namespaced identifier that contain one
/// set of polymorphic arguments at the appropriate position.
/// TODO: @tokens Reimplement/rename once we have a tokenizer
#[derive(Debug, Clone)]
pub struct NamespacedIdentifier {
pub position: InputPosition,
pub value: Vec<u8>, // Full name as it resides in the input source
pub poly_args: Vec<ParserTypeId>, // All poly args littered throughout the namespaced identifier
pub parts: Vec<NamespacedIdentifierPart>, // Indices into value/poly_args
}
impl NamespacedIdentifier {
/// Returns the identifier value without any of the specific polymorphic
/// arguments.
pub fn strip_poly_args(&self) -> Vec<u8> {
debug_assert!(!self.parts.is_empty() && self.parts[0].is_identifier());
let mut result = Vec::with_capacity(self.value.len());
let mut iter = self.iter();
let (first_ident, _) = iter.next().unwrap();
result.extend(first_ident);
for (ident, _) in iter.next() {
result.push(b':');
result.push(b':');
result.extend(ident);
}
result
}
/// Returns an iterator of the elements in the namespaced identifier
pub fn iter(&self) -> NamespacedIdentifierIter {
return NamespacedIdentifierIter{
identifier: self,
element_idx: 0
}
}
pub fn get_poly_args(&self) -> Option<&[ParserTypeId]> {
let has_poly_args = self.parts.iter().any(|v| !v.is_identifier());
if has_poly_args {
Some(&self.poly_args)
} else {
None
}
}
// Check if two namespaced identifiers match eachother when not considering
// the polymorphic arguments
pub fn matches_namespaced_identifier(&self, other: &Self) -> bool {
let mut iter_self = self.iter();
let mut iter_other = other.iter();
loop {
let val_self = iter_self.next();
let val_other = iter_other.next();
if val_self.is_some() != val_other.is_some() {
// One is longer than the other
return false;
}
if val_self.is_none() {
// Both are none
return true;
}
// Both are something
let (val_self, _) = val_self.unwrap();
let (val_other, _) = val_other.unwrap();
if val_self != val_other { return false; }
}
}
// Check if the namespaced identifier matches an identifier when not
// considering the polymorphic arguments
pub fn matches_identifier(&self, other: &Identifier) -> bool {
let mut iter = self.iter();
let (first_ident, _) = iter.next().unwrap();
if first_ident != other.value {
return false;
}
if iter.next().is_some() {
return false;
}
return true;
}
}
/// Iterator over elements of the namespaced identifier. The element index will
/// only ever be at the start of an identifier element.
#[derive(Debug)]
pub struct NamespacedIdentifierIter<'a> {
identifier: &'a NamespacedIdentifier,
element_idx: usize,
}
impl<'a> Iterator for NamespacedIdentifierIter<'a> {
type Item = (&'a [u8], Option<&'a [ParserTypeId]>);
fn next(&mut self) -> Option<Self::Item> {
match self.get(self.element_idx) {
Some((ident, poly)) => {
self.element_idx += 1;
if poly.is_some() {
self.element_idx += 1;
}
Some((ident, poly))
},
None => None
}
}
}
impl<'a> NamespacedIdentifierIter<'a> {
/// Returns number of parts iterated over, may not correspond to number of
/// times one called `next()` because returning an identifier with
/// polymorphic arguments increments the internal counter by 2.
pub fn num_returned(&self) -> usize {
return self.element_idx;
}
pub fn num_remaining(&self) -> usize {
return self.identifier.parts.len() - self.element_idx;
}
pub fn returned_section(&self) -> &[u8] {
if self.element_idx == 0 { return &self.identifier.value[0..0]; }
let last_idx = match &self.identifier.parts[self.element_idx - 1] {
NamespacedIdentifierPart::Identifier{end, ..} => *end,
NamespacedIdentifierPart::PolyArgs{end, ..} => *end,
};
return &self.identifier.value[..last_idx as usize];
}
/// Returns a specific element from the namespaced identifier
pub fn get(&self, idx: usize) -> Option<<Self as Iterator>::Item> {
if idx >= self.identifier.parts.len() {
return None
}
let cur_part = &self.identifier.parts[idx];
let next_part = self.identifier.parts.get(idx + 1);
let (ident_start, ident_end) = cur_part.as_identifier();
let poly_slice = match next_part {
Some(part) => match part {
NamespacedIdentifierPart::Identifier{..} => None,
NamespacedIdentifierPart::PolyArgs{start, end} => Some(
&self.identifier.poly_args[*start as usize..*end as usize]
),
},
None => None
};
Some((
&self.identifier.value[ident_start as usize..ident_end as usize],
poly_slice
))
}
/// Returns the previously returend index into the parts array of the
/// identifier.
pub fn prev_idx(&self) -> Option<usize> {
if self.element_idx == 0 {
return None;
};
if self.identifier.parts[self.element_idx - 1].is_identifier() {
return Some(self.element_idx - 1);
}
// Previous part had polymorphic arguments, so the one before that must
// be an identifier (if well formed)
debug_assert!(self.element_idx >= 2 && self.identifier.parts[self.element_idx - 2].is_identifier());
return Some(self.element_idx - 2)
}
/// Returns the previously returned result from `next()`
pub fn prev(&self) -> Option<<Self as Iterator>::Item> {
match self.prev_idx() {
None => None,
Some(idx) => self.get(idx)
}
}
}
/// TODO: @types Remove the Message -> Byte hack at some point...
#[derive(Debug, Clone)]
pub enum ParserTypeVariant {
// Basic builtin
Message,
Bool,
UInt8, Uint16, UInt32, UInt64,
SInt8, SInt16, SInt32, SInt64,
Character, String,
// Literals (need to get concrete builtin type during typechecking)
IntegerLiteral,
Inferred,
// Complex builtins
Array(ParserTypeId), // array of a type
Input(ParserTypeId), // typed input endpoint of a channel
Output(ParserTypeId), // typed output endpoint of a channel
Symbolic(SymbolicParserType), // symbolic type (definition or polyarg)
}
impl ParserTypeVariant {
pub(crate) fn supports_polymorphic_args(&self) -> bool {
use ParserTypeVariant::*;
match self {
Message | Bool | Byte | Short | Int | Long | String | IntegerLiteral | Inferred => false,
_ => true
}
}
}
/// ParserType is a specification of a type during the parsing phase and initial
/// linker/validator phase of the compilation process. These types may be
/// (partially) inferred or represent literals (e.g. a integer whose bytesize is
/// not yet determined).
#[derive(Debug, Clone)]
pub struct ParserType {
pub this: ParserTypeId,
pub pos: InputPosition,
pub variant: ParserTypeVariant,
}
/// SymbolicParserType is the specification of a symbolic type. During the
/// parsing phase we will only store the identifier of the type. During the
/// validation phase we will determine whether it refers to a user-defined type,
/// or a polymorphic argument. After the validation phase it may still be the
/// case that the resulting `variant` will not pass the typechecker.
#[derive(Debug, Clone)]
pub struct SymbolicParserType {
// Phase 1: parser
pub identifier: NamespacedIdentifier,
// Phase 2: validation/linking (for types in function/component bodies) and
// type table construction (for embedded types of structs/unions)
pub poly_args2: Vec<ParserTypeId>, // taken from identifier or inferred
pub variant: Option<SymbolicParserTypeVariant>
}
/// Specifies whether the symbolic type points to an actual user-defined type,
/// or whether it points to a polymorphic argument within the definition (e.g.
/// a defined variable `T var` within a function `int func<T>()`
#[derive(Debug, Clone)]
pub enum SymbolicParserTypeVariant {
Definition(DefinitionId),
// TODO: figure out if I need the DefinitionId here
PolyArg(DefinitionId, usize), // index of polyarg in the definition
}
/// ConcreteType is the representation of a type after resolving symbolic types
/// and performing type inference
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum ConcreteTypePart {
// Markers for the use of polymorphic types within a procedure's body that
// refer to polymorphic variables on the procedure's definition. Different
// from markers in the `InferenceType`, these will not contain nested types.
Marker(usize),
// Special types (cannot be explicitly constructed by the programmer)
Void,
// Builtin types without nested types
Message,
Bool,
Byte,
Short,
Int,
Long,
String,
// Builtin types with one nested type
Array,
Slice,
Input,
Output,
// User defined type with any number of nested types
Instance(DefinitionId, usize),
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ConcreteType {
pub(crate) parts: Vec<ConcreteTypePart>
}
impl Default for ConcreteType {
fn default() -> Self {
Self{ parts: Vec::new() }
}
}
impl ConcreteType {
pub(crate) fn has_marker(&self) -> bool {
self.parts
.iter()
.any(|p| {
if let ConcreteTypePart::Marker(_) = p { true } else { false }
})
}
}
// TODO: Remove at some point
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PrimitiveType {
Unassigned,
Input,
Output,
Message,
Boolean,
Byte,
Short,
Int,
Long,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Type {
pub primitive: PrimitiveType,
pub array: bool,
}
#[allow(dead_code)]
impl Type {
pub const UNASSIGNED: Type = Type { primitive: PrimitiveType::Unassigned, array: false };
pub const INPUT: Type = Type { primitive: PrimitiveType::Input, array: false };
pub const OUTPUT: Type = Type { primitive: PrimitiveType::Output, array: false };
pub const MESSAGE: Type = Type { primitive: PrimitiveType::Message, array: false };
pub const BOOLEAN: Type = Type { primitive: PrimitiveType::Boolean, array: false };
pub const BYTE: Type = Type { primitive: PrimitiveType::Byte, array: false };
pub const SHORT: Type = Type { primitive: PrimitiveType::Short, array: false };
pub const INT: Type = Type { primitive: PrimitiveType::Int, array: false };
pub const LONG: Type = Type { primitive: PrimitiveType::Long, array: false };
pub const INPUT_ARRAY: Type = Type { primitive: PrimitiveType::Input, array: true };
pub const OUTPUT_ARRAY: Type = Type { primitive: PrimitiveType::Output, array: true };
pub const MESSAGE_ARRAY: Type = Type { primitive: PrimitiveType::Message, array: true };
pub const BOOLEAN_ARRAY: Type = Type { primitive: PrimitiveType::Boolean, array: true };
pub const BYTE_ARRAY: Type = Type { primitive: PrimitiveType::Byte, array: true };
pub const SHORT_ARRAY: Type = Type { primitive: PrimitiveType::Short, array: true };
pub const INT_ARRAY: Type = Type { primitive: PrimitiveType::Int, array: true };
pub const LONG_ARRAY: Type = Type { primitive: PrimitiveType::Long, array: true };
}
impl Display for Type {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match &self.primitive {
PrimitiveType::Unassigned => {
write!(f, "unassigned")?;
}
PrimitiveType::Input => {
write!(f, "in")?;
}
PrimitiveType::Output => {
write!(f, "out")?;
}
PrimitiveType::Message => {
write!(f, "msg")?;
}
PrimitiveType::Boolean => {
write!(f, "boolean")?;
}
PrimitiveType::Byte => {
write!(f, "byte")?;
}
PrimitiveType::Short => {
write!(f, "short")?;
}
PrimitiveType::Int => {
write!(f, "int")?;
}
PrimitiveType::Long => {
write!(f, "long")?;
}
}
if self.array {
write!(f, "[]")
} else {
Ok(())
}
}
}
#[derive(Debug, Clone)]
pub enum Field {
Length,
Symbolic(FieldSymbolic),
}
impl Field {
pub fn is_length(&self) -> bool {
match self {
Field::Length => true,
_ => false,
}
}
pub fn as_symbolic(&self) -> &FieldSymbolic {
match self {
Field::Symbolic(v) => v,
_ => unreachable!("attempted to get Field::Symbolic from {:?}", self)
}
}
}
#[derive(Debug, Clone)]
pub struct FieldSymbolic {
// Phase 1: Parser
pub(crate) identifier: Identifier,
// Phase 3: Typing
pub(crate) definition: Option<DefinitionId>,
pub(crate) field_idx: usize,
}
#[derive(Debug, Clone, Copy)]
pub enum Scope {
Definition(DefinitionId),
Regular(BlockStatementId),
Synchronous((SynchronousStatementId, BlockStatementId)),
}
impl Scope {
pub fn is_block(&self) -> bool {
match &self {
Scope::Definition(_) => false,
Scope::Regular(_) => true,
Scope::Synchronous(_) => true,
}
}
pub fn to_block(&self) -> BlockStatementId {
match &self {
Scope::Regular(id) => *id,
Scope::Synchronous((_, id)) => *id,
_ => panic!("unable to get BlockStatement from Scope")
}
}
}
pub trait VariableScope {
fn parent_scope(&self, h: &Heap) -> Option<Scope>;
fn get_variable(&self, h: &Heap, id: &Identifier) -> Option<VariableId>;
}
impl VariableScope for Scope {
fn parent_scope(&self, h: &Heap) -> Option<Scope> {
match self {
Scope::Definition(def) => h[*def].parent_scope(h),
Scope::Regular(stmt) => h[*stmt].parent_scope(h),
Scope::Synchronous((stmt, _)) => h[*stmt].parent_scope(h),
}
}
fn get_variable(&self, h: &Heap, id: &Identifier) -> Option<VariableId> {
match self {
Scope::Definition(def) => h[*def].get_variable(h, id),
Scope::Regular(stmt) => h[*stmt].get_variable(h, id),
Scope::Synchronous((stmt, _)) => h[*stmt].get_variable(h, id),
}
}
}
#[derive(Debug, Clone)]
pub enum Variable {
Parameter(Parameter),
Local(Local),
}
impl Variable {
pub fn identifier(&self) -> &Identifier {
match self {
Variable::Parameter(var) => &var.identifier,
Variable::Local(var) => &var.identifier,
}
}
pub fn is_parameter(&self) -> bool {
match self {
Variable::Parameter(_) => true,
_ => false,
}
}
pub fn as_parameter(&self) -> &Parameter {
match self {
Variable::Parameter(result) => result,
_ => panic!("Unable to cast `Variable` to `Parameter`"),
}
}
pub fn as_local(&self) -> &Local {
match self {
Variable::Local(result) => result,
_ => panic!("Unable to cast `Variable` to `Local`"),
}
}
pub fn as_local_mut(&mut self) -> &mut Local {
match self {
Variable::Local(result) => result,
_ => panic!("Unable to cast 'Variable' to 'Local'"),
}
}
}
impl SyntaxElement for Variable {
fn position(&self) -> InputPosition {
match self {
Variable::Parameter(decl) => decl.position(),
Variable::Local(decl) => decl.position(),
}
}
}
/// TODO: Remove distinction between parameter/local and add an enum to indicate
/// the distinction between the two
#[derive(Debug, Clone)]
pub struct Parameter {
pub this: ParameterId,
// Phase 1: parser
pub position: InputPosition,
pub parser_type: ParserTypeId,
pub identifier: Identifier,
}
impl SyntaxElement for Parameter {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub struct Local {
pub this: LocalId,
// Phase 1: parser
pub position: InputPosition,
pub parser_type: ParserTypeId,
pub identifier: Identifier,
// Phase 2: linker
pub relative_pos_in_block: u32,
}
impl SyntaxElement for Local {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub enum Definition {
Struct(StructDefinition),
Enum(EnumDefinition),
Union(UnionDefinition),
Component(ComponentDefinition),
Function(FunctionDefinition),
}
impl Definition {
pub fn is_struct(&self) -> bool {
match self {
Definition::Struct(_) => true,
_ => false
}
}
pub fn as_struct(&self) -> &StructDefinition {
match self {
Definition::Struct(result) => result,
_ => panic!("Unable to cast 'Definition' to 'StructDefinition'"),
}
}
pub fn is_enum(&self) -> bool {
match self {
Definition::Enum(_) => true,
_ => false,
}
}
pub fn as_enum(&self) -> &EnumDefinition {
match self {
Definition::Enum(result) => result,
_ => panic!("Unable to cast 'Definition' to 'EnumDefinition'"),
}
}
pub fn is_union(&self) -> bool {
match self {
Definition::Union(_) => true,
_ => false,
}
}
pub fn as_union(&self) -> &UnionDefinition {
match self {
Definition::Union(result) => result,
_ => panic!("Unable to cast 'Definition' to 'UnionDefinition'"),
}
}
pub fn is_component(&self) -> bool {
match self {
Definition::Component(_) => true,
_ => false,
}
}
pub fn as_component(&self) -> &ComponentDefinition {
match self {
Definition::Component(result) => result,
_ => panic!("Unable to cast `Definition` to `Component`"),
}
}
pub fn is_function(&self) -> bool {
match self {
Definition::Function(_) => true,
_ => false,
}
}
pub fn as_function(&self) -> &FunctionDefinition {
match self {
Definition::Function(result) => result,
_ => panic!("Unable to cast `Definition` to `Function`"),
}
}
pub fn identifier(&self) -> &Identifier {
match self {
Definition::Struct(def) => &def.identifier,
Definition::Enum(def) => &def.identifier,
Definition::Union(def) => &def.identifier,
Definition::Component(def) => &def.identifier,
Definition::Function(def) => &def.identifier,
}
}
pub fn parameters(&self) -> &Vec<ParameterId> {
// TODO: Fix this
static EMPTY_VEC: Vec<ParameterId> = Vec::new();
match self {
Definition::Component(com) => &com.parameters,
Definition::Function(fun) => &fun.parameters,
_ => &EMPTY_VEC,
}
}
pub fn body(&self) -> StatementId {
// TODO: Fix this
match self {
Definition::Component(com) => com.body,
Definition::Function(fun) => fun.body,
_ => panic!("cannot retrieve body (for EnumDefinition or StructDefinition)")
}
}
}
impl SyntaxElement for Definition {
fn position(&self) -> InputPosition {
match self {
Definition::Struct(def) => def.position,
Definition::Enum(def) => def.position,
Definition::Union(def) => def.position,
Definition::Component(def) => def.position(),
Definition::Function(def) => def.position(),
}
}
}
impl VariableScope for Definition {
fn parent_scope(&self, _h: &Heap) -> Option<Scope> {
None
}
fn get_variable(&self, h: &Heap, id: &Identifier) -> Option<VariableId> {
for ¶meter_id in self.parameters().iter() {
let parameter = &h[parameter_id];
if parameter.identifier == *id {
return Some(parameter_id.0);
}
}
None
}
}
#[derive(Debug, Clone)]
pub struct StructFieldDefinition {
pub field: Identifier,
pub parser_type: ParserTypeId,
}
#[derive(Debug, Clone)]
pub struct StructDefinition {
pub this: StructDefinitionId,
// Phase 1: parser
pub span: InputSpan,
pub identifier: Identifier,
pub poly_vars: Vec<Identifier>,
pub fields: Vec<StructFieldDefinition>
}
impl StructDefinition {
pub(crate) fn new_empty(this: StructDefinitionId, span: InputSpan, identifier: Identifier) -> Self {
Self{ this, span, identifier, poly_vars: Vec::new(), fields: Vec::new() }
}
}
#[derive(Debug, Clone)]
pub enum EnumVariantValue {
None,
Integer(i64),
}
#[derive(Debug, Clone)]
pub struct EnumVariantDefinition {
pub position: InputPosition,
pub identifier: Identifier,
pub value: EnumVariantValue,
}
#[derive(Debug, Clone)]
pub struct EnumDefinition {
pub this: EnumDefinitionId,
// Phase 1: parser
pub span: InputSpan,
pub identifier: Identifier,
pub poly_vars: Vec<Identifier>,
pub variants: Vec<EnumVariantDefinition>,
}
impl EnumDefinition {
pub(crate) fn new_empty(this: EnumDefinitionId, span: InputSpan, identifier: Identifier) -> Self {
Self{ this, span, identifier, poly_vars: Vec::new(), variants: Vec::new() }
}
}
#[derive(Debug, Clone)]
pub enum UnionVariantValue {
None,
Embedded(Vec<ParserTypeId>),
}
#[derive(Debug, Clone)]
pub struct UnionVariantDefinition {
pub position: InputPosition,
pub identifier: Identifier,
pub value: UnionVariantValue,
}
#[derive(Debug, Clone)]
pub struct UnionDefinition {
pub this: UnionDefinitionId,
// Phase 1: parser
pub span: InputSpan,
pub identifier: Identifier,
pub poly_vars: Vec<Identifier>,
pub variants: Vec<UnionVariantDefinition>,
}
impl UnionDefinition {
pub(crate) fn new_empty(this: UnionDefinitionId, span: InputSpan, identifier: Identifier) -> Self {
Self{ this, span, identifier, poly_vars: Vec::new(), variants: Vec::new() }
}
}
#[derive(Debug, Clone, Copy)]
pub enum ComponentVariant {
Primitive,
Composite,
}
#[derive(Debug, Clone)]
pub struct ComponentDefinition {
pub this: ComponentDefinitionId,
// Phase 1: parser
pub span: InputSpan,
pub variant: ComponentVariant,
pub identifier: Identifier,
pub poly_vars: Vec<Identifier>,
pub parameters: Vec<ParameterId>,
pub body: StatementId,
}
impl ComponentDefinition {
pub(crate) fn new_empty(this: ComponentDefinitionId, span: InputSpan, variant: ComponentVariant, identifier: Identifier) -> Self {
Self{
this, span, variant, identifier,
poly_vars: Vec::new(),
parameters: Vec::new(),
body: StatementId::new_invalid()
}
}
}
impl SyntaxElement for ComponentDefinition {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub struct FunctionDefinition {
pub this: FunctionDefinitionId,
// Phase 1: parser
pub span: InputSpan,
pub return_type: ParserTypeId,
pub identifier: Identifier,
pub poly_vars: Vec<Identifier>,
pub parameters: Vec<ParameterId>,
pub body: StatementId,
}
impl FunctionDefinition {
pub(crate) fn new_empty(this: FunctionDefinitionId, span: InputSpan, identifier: Identifier) -> Self {
Self {
this, span, identifier,
return_type: ParserTypeId::new_invalid(),
poly_vars: Vec::new(),
parameters: Vec::new(),
body: StatementId::new_invalid(),
}
}
}
impl SyntaxElement for FunctionDefinition {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub enum Statement {
Block(BlockStatement),
Local(LocalStatement),
Skip(SkipStatement),
Labeled(LabeledStatement),
If(IfStatement),
EndIf(EndIfStatement),
While(WhileStatement),
EndWhile(EndWhileStatement),
Break(BreakStatement),
Continue(ContinueStatement),
Synchronous(SynchronousStatement),
EndSynchronous(EndSynchronousStatement),
Return(ReturnStatement),
Assert(AssertStatement),
Goto(GotoStatement),
New(NewStatement),
Expression(ExpressionStatement),
}
impl Statement {
pub fn as_block(&self) -> &BlockStatement {
match self {
Statement::Block(result) => result,
_ => panic!("Unable to cast `Statement` to `BlockStatement`"),
}
}
pub fn as_block_mut(&mut self) -> &mut BlockStatement {
match self {
Statement::Block(result) => result,
_ => panic!("Unable to cast `Statement` to `BlockStatement`"),
}
}
pub fn as_local(&self) -> &LocalStatement {
match self {
Statement::Local(result) => result,
_ => panic!("Unable to cast `Statement` to `LocalStatement`"),
}
}
pub fn as_memory(&self) -> &MemoryStatement {
self.as_local().as_memory()
}
pub fn as_channel(&self) -> &ChannelStatement {
self.as_local().as_channel()
}
pub fn as_skip(&self) -> &SkipStatement {
match self {
Statement::Skip(result) => result,
_ => panic!("Unable to cast `Statement` to `SkipStatement`"),
}
}
pub fn as_labeled(&self) -> &LabeledStatement {
match self {
Statement::Labeled(result) => result,
_ => panic!("Unable to cast `Statement` to `LabeledStatement`"),
}
}
pub fn as_labeled_mut(&mut self) -> &mut LabeledStatement {
match self {
Statement::Labeled(result) => result,
_ => panic!("Unable to cast `Statement` to `LabeledStatement`"),
}
}
pub fn as_if(&self) -> &IfStatement {
match self {
Statement::If(result) => result,
_ => panic!("Unable to cast `Statement` to `IfStatement`"),
}
}
pub fn as_if_mut(&mut self) -> &mut IfStatement {
match self {
Statement::If(result) => result,
_ => panic!("Unable to cast 'Statement' to 'IfStatement'"),
}
}
pub fn as_end_if(&self) -> &EndIfStatement {
match self {
Statement::EndIf(result) => result,
_ => panic!("Unable to cast `Statement` to `EndIfStatement`"),
}
}
pub fn is_while(&self) -> bool {
match self {
Statement::While(_) => true,
_ => false,
}
}
pub fn as_while(&self) -> &WhileStatement {
match self {
Statement::While(result) => result,
_ => panic!("Unable to cast `Statement` to `WhileStatement`"),
}
}
pub fn as_while_mut(&mut self) -> &mut WhileStatement {
match self {
Statement::While(result) => result,
_ => panic!("Unable to cast `Statement` to `WhileStatement`"),
}
}
pub fn as_end_while(&self) -> &EndWhileStatement {
match self {
Statement::EndWhile(result) => result,
_ => panic!("Unable to cast `Statement` to `EndWhileStatement`"),
}
}
pub fn as_break(&self) -> &BreakStatement {
match self {
Statement::Break(result) => result,
_ => panic!("Unable to cast `Statement` to `BreakStatement`"),
}
}
pub fn as_break_mut(&mut self) -> &mut BreakStatement {
match self {
Statement::Break(result) => result,
_ => panic!("Unable to cast `Statement` to `BreakStatement`"),
}
}
pub fn as_continue(&self) -> &ContinueStatement {
match self {
Statement::Continue(result) => result,
_ => panic!("Unable to cast `Statement` to `ContinueStatement`"),
}
}
pub fn as_continue_mut(&mut self) -> &mut ContinueStatement {
match self {
Statement::Continue(result) => result,
_ => panic!("Unable to cast `Statement` to `ContinueStatement`"),
}
}
pub fn as_synchronous(&self) -> &SynchronousStatement {
match self {
Statement::Synchronous(result) => result,
_ => panic!("Unable to cast `Statement` to `SynchronousStatement`"),
}
}
pub fn as_synchronous_mut(&mut self) -> &mut SynchronousStatement {
match self {
Statement::Synchronous(result) => result,
_ => panic!("Unable to cast `Statement` to `SynchronousStatement`"),
}
}
pub fn as_end_synchronous(&self) -> &EndSynchronousStatement {
match self {
Statement::EndSynchronous(result) => result,
_ => panic!("Unable to cast `Statement` to `EndSynchronousStatement`"),
}
}
pub fn as_return(&self) -> &ReturnStatement {
match self {
Statement::Return(result) => result,
_ => panic!("Unable to cast `Statement` to `ReturnStatement`"),
}
}
pub fn as_assert(&self) -> &AssertStatement {
match self {
Statement::Assert(result) => result,
_ => panic!("Unable to cast `Statement` to `AssertStatement`"),
}
}
pub fn as_goto(&self) -> &GotoStatement {
match self {
Statement::Goto(result) => result,
_ => panic!("Unable to cast `Statement` to `GotoStatement`"),
}
}
pub fn as_goto_mut(&mut self) -> &mut GotoStatement {
match self {
Statement::Goto(result) => result,
_ => panic!("Unable to cast `Statement` to `GotoStatement`"),
}
}
pub fn as_new(&self) -> &NewStatement {
match self {
Statement::New(result) => result,
_ => panic!("Unable to cast `Statement` to `NewStatement`"),
}
}
pub fn as_expression(&self) -> &ExpressionStatement {
match self {
Statement::Expression(result) => result,
_ => panic!("Unable to cast `Statement` to `ExpressionStatement`"),
}
}
pub fn link_next(&mut self, next: StatementId) {
match self {
Statement::Block(_) => todo!(),
Statement::Local(stmt) => match stmt {
LocalStatement::Channel(stmt) => stmt.next = Some(next),
LocalStatement::Memory(stmt) => stmt.next = Some(next),
},
Statement::Skip(stmt) => stmt.next = Some(next),
Statement::EndIf(stmt) => stmt.next = Some(next),
Statement::EndWhile(stmt) => stmt.next = Some(next),
Statement::EndSynchronous(stmt) => stmt.next = Some(next),
Statement::Assert(stmt) => stmt.next = Some(next),
Statement::New(stmt) => stmt.next = Some(next),
Statement::Expression(stmt) => stmt.next = Some(next),
Statement::Return(_)
| Statement::Break(_)
| Statement::Continue(_)
| Statement::Synchronous(_)
| Statement::Goto(_)
| Statement::While(_)
| Statement::Labeled(_)
| Statement::If(_) => unreachable!(),
}
}
}
impl SyntaxElement for Statement {
fn position(&self) -> InputPosition {
match self {
Statement::Block(stmt) => stmt.position(),
Statement::Local(stmt) => stmt.position(),
Statement::Skip(stmt) => stmt.position(),
Statement::Labeled(stmt) => stmt.position(),
Statement::If(stmt) => stmt.position(),
Statement::EndIf(stmt) => stmt.position(),
Statement::While(stmt) => stmt.position(),
Statement::EndWhile(stmt) => stmt.position(),
Statement::Break(stmt) => stmt.position(),
Statement::Continue(stmt) => stmt.position(),
Statement::Synchronous(stmt) => stmt.position(),
Statement::EndSynchronous(stmt) => stmt.position(),
Statement::Return(stmt) => stmt.position(),
Statement::Assert(stmt) => stmt.position(),
Statement::Goto(stmt) => stmt.position(),
Statement::New(stmt) => stmt.position(),
Statement::Expression(stmt) => stmt.position(),
}
}
}
#[derive(Debug, Clone)]
pub struct BlockStatement {
pub this: BlockStatementId,
// Phase 1: parser
pub position: InputPosition,
pub statements: Vec<StatementId>,
// Phase 2: linker
pub parent_scope: Option<Scope>,
pub relative_pos_in_parent: u32,
pub locals: Vec<LocalId>,
pub labels: Vec<LabeledStatementId>,
}
impl BlockStatement {
pub fn parent_block(&self, h: &Heap) -> Option<BlockStatementId> {
let parent = self.parent_scope.unwrap();
match parent {
Scope::Definition(_) => {
// If the parent scope is a definition, then there is no
// parent block.
None
}
Scope::Synchronous((parent, _)) => {
// It is always the case that when this function is called,
// the parent of a synchronous statement is a block statement:
// nested synchronous statements are flagged illegal,
// and that happens before resolving variables that
// creates the parent_scope references in the first place.
Some(h[parent].parent_scope(h).unwrap().to_block())
}
Scope::Regular(parent) => {
// A variable scope is either a definition, sync, or block.
Some(parent)
}
}
}
pub fn first(&self) -> StatementId {
// It is an invariant (guaranteed by the lexer) that block statements have at least one stmt
*self.statements.first().unwrap()
}
}
impl SyntaxElement for BlockStatement {
fn position(&self) -> InputPosition {
self.position
}
}
impl VariableScope for BlockStatement {
fn parent_scope(&self, _h: &Heap) -> Option<Scope> {
self.parent_scope.clone()
}
fn get_variable(&self, h: &Heap, id: &Identifier) -> Option<VariableId> {
for local_id in self.locals.iter() {
let local = &h[*local_id];
if local.identifier == *id {
return Some(local_id.0);
}
}
None
}
}
#[derive(Debug, Clone)]
pub enum LocalStatement {
Memory(MemoryStatement),
Channel(ChannelStatement),
}
impl LocalStatement {
pub fn this(&self) -> LocalStatementId {
match self {
LocalStatement::Memory(stmt) => stmt.this.upcast(),
LocalStatement::Channel(stmt) => stmt.this.upcast(),
}
}
pub fn as_memory(&self) -> &MemoryStatement {
match self {
LocalStatement::Memory(result) => result,
_ => panic!("Unable to cast `LocalStatement` to `MemoryStatement`"),
}
}
pub fn as_channel(&self) -> &ChannelStatement {
match self {
LocalStatement::Channel(result) => result,
_ => panic!("Unable to cast `LocalStatement` to `ChannelStatement`"),
}
}
pub fn next(&self) -> Option<StatementId> {
match self {
LocalStatement::Memory(stmt) => stmt.next,
LocalStatement::Channel(stmt) => stmt.next,
}
}
}
impl SyntaxElement for LocalStatement {
fn position(&self) -> InputPosition {
match self {
LocalStatement::Memory(stmt) => stmt.position(),
LocalStatement::Channel(stmt) => stmt.position(),
}
}
}
#[derive(Debug, Clone)]
pub struct MemoryStatement {
pub this: MemoryStatementId,
// Phase 1: parser
pub position: InputPosition,
pub variable: LocalId,
// Phase 2: linker
pub next: Option<StatementId>,
}
impl SyntaxElement for MemoryStatement {
fn position(&self) -> InputPosition {
self.position
}
}
/// ChannelStatement is the declaration of an input and output port associated
/// with the same channel. Note that the polarity of the ports are from the
/// point of view of the component. So an output port is something that a
/// component uses to send data over (i.e. it is the "input end" of the
/// channel), and vice versa.
#[derive(Debug, Clone)]
pub struct ChannelStatement {
pub this: ChannelStatementId,
// Phase 1: parser
pub position: InputPosition,
pub from: LocalId, // output
pub to: LocalId, // input
// Phase 2: linker
pub relative_pos_in_block: u32,
pub next: Option<StatementId>,
}
impl SyntaxElement for ChannelStatement {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub struct SkipStatement {
pub this: SkipStatementId,
// Phase 1: parser
pub position: InputPosition,
// Phase 2: linker
pub next: Option<StatementId>,
}
impl SyntaxElement for SkipStatement {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub struct LabeledStatement {
pub this: LabeledStatementId,
// Phase 1: parser
pub position: InputPosition,
pub label: Identifier,
pub body: StatementId,
// Phase 2: linker
pub relative_pos_in_block: u32,
pub in_sync: Option<SynchronousStatementId>,
}
impl SyntaxElement for LabeledStatement {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub struct IfStatement {
pub this: IfStatementId,
// Phase 1: parser
pub position: InputPosition,
pub test: ExpressionId,
pub true_body: StatementId,
pub false_body: StatementId,
// Phase 2: linker
pub end_if: Option<EndIfStatementId>,
}
impl SyntaxElement for IfStatement {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub struct EndIfStatement {
pub this: EndIfStatementId,
// Phase 2: linker
pub start_if: IfStatementId,
pub position: InputPosition, // of corresponding if statement
pub next: Option<StatementId>,
}
impl SyntaxElement for EndIfStatement {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub struct WhileStatement {
pub this: WhileStatementId,
// Phase 1: parser
pub position: InputPosition,
pub test: ExpressionId,
pub body: StatementId,
// Phase 2: linker
pub end_while: Option<EndWhileStatementId>,
pub in_sync: Option<SynchronousStatementId>,
}
impl SyntaxElement for WhileStatement {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub struct EndWhileStatement {
pub this: EndWhileStatementId,
// Phase 2: linker
pub start_while: WhileStatementId,
pub position: InputPosition, // of corresponding while
pub next: Option<StatementId>,
}
impl SyntaxElement for EndWhileStatement {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub struct BreakStatement {
pub this: BreakStatementId,
// Phase 1: parser
pub position: InputPosition,
pub label: Option<Identifier>,
// Phase 2: linker
pub target: Option<EndWhileStatementId>,
}
impl SyntaxElement for BreakStatement {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub struct ContinueStatement {
pub this: ContinueStatementId,
// Phase 1: parser
pub position: InputPosition,
pub label: Option<Identifier>,
// Phase 2: linker
pub target: Option<WhileStatementId>,
}
impl SyntaxElement for ContinueStatement {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub struct SynchronousStatement {
pub this: SynchronousStatementId,
// Phase 1: parser
pub position: InputPosition,
// pub parameters: Vec<ParameterId>,
pub body: StatementId,
// Phase 2: linker
pub end_sync: Option<EndSynchronousStatementId>,
pub parent_scope: Option<Scope>,
}
impl SyntaxElement for SynchronousStatement {
fn position(&self) -> InputPosition {
self.position
}
}
impl VariableScope for SynchronousStatement {
fn parent_scope(&self, _h: &Heap) -> Option<Scope> {
self.parent_scope.clone()
}
fn get_variable(&self, _h: &Heap, _id: &Identifier) -> Option<VariableId> {
// TODO: Another case of "where was this used for?"
// for parameter_id in self.parameters.iter() {
// let parameter = &h[*parameter_id];
// if parameter.identifier.value == id.value {
// return Some(parameter_id.0);
// }
// }
None
}
}
#[derive(Debug, Clone)]
pub struct EndSynchronousStatement {
pub this: EndSynchronousStatementId,
// Phase 2: linker
pub position: InputPosition, // of corresponding sync statement
pub start_sync: SynchronousStatementId,
pub next: Option<StatementId>,
}
impl SyntaxElement for EndSynchronousStatement {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub struct ReturnStatement {
pub this: ReturnStatementId,
// Phase 1: parser
pub position: InputPosition,
pub expression: ExpressionId,
}
impl SyntaxElement for ReturnStatement {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub struct AssertStatement {
pub this: AssertStatementId,
// Phase 1: parser
pub position: InputPosition,
pub expression: ExpressionId,
// Phase 2: linker
pub next: Option<StatementId>,
}
impl SyntaxElement for AssertStatement {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub struct GotoStatement {
pub this: GotoStatementId,
// Phase 1: parser
pub position: InputPosition,
pub label: Identifier,
// Phase 2: linker
pub target: Option<LabeledStatementId>,
}
impl SyntaxElement for GotoStatement {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub struct NewStatement {
pub this: NewStatementId,
// Phase 1: parser
pub position: InputPosition,
pub expression: CallExpressionId,
// Phase 2: linker
pub next: Option<StatementId>,
}
impl SyntaxElement for NewStatement {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub struct ExpressionStatement {
pub this: ExpressionStatementId,
// Phase 1: parser
pub position: InputPosition,
pub expression: ExpressionId,
// Phase 2: linker
pub next: Option<StatementId>,
}
impl SyntaxElement for ExpressionStatement {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum ExpressionParent {
None, // only set during initial parsing
If(IfStatementId),
While(WhileStatementId),
Return(ReturnStatementId),
Assert(AssertStatementId),
New(NewStatementId),
ExpressionStmt(ExpressionStatementId),
Expression(ExpressionId, u32) // index within expression (e.g LHS or RHS of expression)
}
#[derive(Debug, Clone)]
pub enum Expression {
Assignment(AssignmentExpression),
Binding(BindingExpression),
Conditional(ConditionalExpression),
Binary(BinaryExpression),
Unary(UnaryExpression),
Indexing(IndexingExpression),
Slicing(SlicingExpression),
Select(SelectExpression),
Array(ArrayExpression),
Literal(LiteralExpression),
Call(CallExpression),
Variable(VariableExpression),
}
impl Expression {
pub fn as_assignment(&self) -> &AssignmentExpression {
match self {
Expression::Assignment(result) => result,
_ => panic!("Unable to cast `Expression` to `AssignmentExpression`"),
}
}
pub fn as_conditional(&self) -> &ConditionalExpression {
match self {
Expression::Conditional(result) => result,
_ => panic!("Unable to cast `Expression` to `ConditionalExpression`"),
}
}
pub fn as_binary(&self) -> &BinaryExpression {
match self {
Expression::Binary(result) => result,
_ => panic!("Unable to cast `Expression` to `BinaryExpression`"),
}
}
pub fn as_unary(&self) -> &UnaryExpression {
match self {
Expression::Unary(result) => result,
_ => panic!("Unable to cast `Expression` to `UnaryExpression`"),
}
}
pub fn as_indexing(&self) -> &IndexingExpression {
match self {
Expression::Indexing(result) => result,
_ => panic!("Unable to cast `Expression` to `IndexingExpression`"),
}
}
pub fn as_slicing(&self) -> &SlicingExpression {
match self {
Expression::Slicing(result) => result,
_ => panic!("Unable to cast `Expression` to `SlicingExpression`"),
}
}
pub fn as_select(&self) -> &SelectExpression {
match self {
Expression::Select(result) => result,
_ => panic!("Unable to cast `Expression` to `SelectExpression`"),
}
}
pub fn as_array(&self) -> &ArrayExpression {
match self {
Expression::Array(result) => result,
_ => panic!("Unable to cast `Expression` to `ArrayExpression`"),
}
}
pub fn as_constant(&self) -> &LiteralExpression {
match self {
Expression::Literal(result) => result,
_ => panic!("Unable to cast `Expression` to `ConstantExpression`"),
}
}
pub fn as_call(&self) -> &CallExpression {
match self {
Expression::Call(result) => result,
_ => panic!("Unable to cast `Expression` to `CallExpression`"),
}
}
pub fn as_call_mut(&mut self) -> &mut CallExpression {
match self {
Expression::Call(result) => result,
_ => panic!("Unable to cast `Expression` to `CallExpression`"),
}
}
pub fn as_variable(&self) -> &VariableExpression {
match self {
Expression::Variable(result) => result,
_ => panic!("Unable to cast `Expression` to `VariableExpression`"),
}
}
pub fn as_variable_mut(&mut self) -> &mut VariableExpression {
match self {
Expression::Variable(result) => result,
_ => panic!("Unable to cast `Expression` to `VariableExpression`"),
}
}
// TODO: @cleanup
pub fn parent(&self) -> &ExpressionParent {
match self {
Expression::Assignment(expr) => &expr.parent,
Expression::Binding(expr) => &expr.parent,
Expression::Conditional(expr) => &expr.parent,
Expression::Binary(expr) => &expr.parent,
Expression::Unary(expr) => &expr.parent,
Expression::Indexing(expr) => &expr.parent,
Expression::Slicing(expr) => &expr.parent,
Expression::Select(expr) => &expr.parent,
Expression::Array(expr) => &expr.parent,
Expression::Literal(expr) => &expr.parent,
Expression::Call(expr) => &expr.parent,
Expression::Variable(expr) => &expr.parent,
}
}
// TODO: @cleanup
pub fn parent_expr_id(&self) -> Option<ExpressionId> {
if let ExpressionParent::Expression(id, _) = self.parent() {
Some(*id)
} else {
None
}
}
// TODO: @cleanup
pub fn set_parent(&mut self, parent: ExpressionParent) {
match self {
Expression::Assignment(expr) => expr.parent = parent,
Expression::Binding(expr) => expr.parent = parent,
Expression::Conditional(expr) => expr.parent = parent,
Expression::Binary(expr) => expr.parent = parent,
Expression::Unary(expr) => expr.parent = parent,
Expression::Indexing(expr) => expr.parent = parent,
Expression::Slicing(expr) => expr.parent = parent,
Expression::Select(expr) => expr.parent = parent,
Expression::Array(expr) => expr.parent = parent,
Expression::Literal(expr) => expr.parent = parent,
Expression::Call(expr) => expr.parent = parent,
Expression::Variable(expr) => expr.parent = parent,
}
}
pub fn get_type(&self) -> &ConcreteType {
match self {
Expression::Assignment(expr) => &expr.concrete_type,
Expression::Binding(expr) => &expr.concrete_type,
Expression::Conditional(expr) => &expr.concrete_type,
Expression::Binary(expr) => &expr.concrete_type,
Expression::Unary(expr) => &expr.concrete_type,
Expression::Indexing(expr) => &expr.concrete_type,
Expression::Slicing(expr) => &expr.concrete_type,
Expression::Select(expr) => &expr.concrete_type,
Expression::Array(expr) => &expr.concrete_type,
Expression::Literal(expr) => &expr.concrete_type,
Expression::Call(expr) => &expr.concrete_type,
Expression::Variable(expr) => &expr.concrete_type,
}
}
// TODO: @cleanup
pub fn get_type_mut(&mut self) -> &mut ConcreteType {
match self {
Expression::Assignment(expr) => &mut expr.concrete_type,
Expression::Binding(expr) => &mut expr.concrete_type,
Expression::Conditional(expr) => &mut expr.concrete_type,
Expression::Binary(expr) => &mut expr.concrete_type,
Expression::Unary(expr) => &mut expr.concrete_type,
Expression::Indexing(expr) => &mut expr.concrete_type,
Expression::Slicing(expr) => &mut expr.concrete_type,
Expression::Select(expr) => &mut expr.concrete_type,
Expression::Array(expr) => &mut expr.concrete_type,
Expression::Literal(expr) => &mut expr.concrete_type,
Expression::Call(expr) => &mut expr.concrete_type,
Expression::Variable(expr) => &mut expr.concrete_type,
}
}
}
impl SyntaxElement for Expression {
fn position(&self) -> InputPosition {
match self {
Expression::Assignment(expr) => expr.position(),
Expression::Binding(expr) => expr.position,
Expression::Conditional(expr) => expr.position(),
Expression::Binary(expr) => expr.position(),
Expression::Unary(expr) => expr.position(),
Expression::Indexing(expr) => expr.position(),
Expression::Slicing(expr) => expr.position(),
Expression::Select(expr) => expr.position(),
Expression::Array(expr) => expr.position(),
Expression::Literal(expr) => expr.position(),
Expression::Call(expr) => expr.position(),
Expression::Variable(expr) => expr.position(),
}
}
}
#[derive(Debug, Clone)]
pub enum AssignmentOperator {
Set,
Multiplied,
Divided,
Remained,
Added,
Subtracted,
ShiftedLeft,
ShiftedRight,
BitwiseAnded,
BitwiseXored,
BitwiseOred,
}
#[derive(Debug, Clone)]
pub struct AssignmentExpression {
pub this: AssignmentExpressionId,
// Phase 1: parser
pub position: InputPosition,
pub left: ExpressionId,
pub operation: AssignmentOperator,
pub right: ExpressionId,
// Phase 2: linker
pub parent: ExpressionParent,
// Phase 3: type checking
pub concrete_type: ConcreteType,
}
impl SyntaxElement for AssignmentExpression {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub struct BindingExpression {
pub this: BindingExpressionId,
// Phase 1: parser
pub position: InputPosition,
pub left: LiteralExpressionId,
pub right: ExpressionId,
// Phase 2: linker
pub parent: ExpressionParent,
// Phase 3: type checking
pub concrete_type: ConcreteType,
}
#[derive(Debug, Clone)]
pub struct ConditionalExpression {
pub this: ConditionalExpressionId,
// Phase 1: parser
pub position: InputPosition,
pub test: ExpressionId,
pub true_expression: ExpressionId,
pub false_expression: ExpressionId,
// Phase 2: linker
pub parent: ExpressionParent,
// Phase 3: type checking
pub concrete_type: ConcreteType,
}
impl SyntaxElement for ConditionalExpression {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BinaryOperator {
Concatenate,
LogicalOr,
LogicalAnd,
BitwiseOr,
BitwiseXor,
BitwiseAnd,
Equality,
Inequality,
LessThan,
GreaterThan,
LessThanEqual,
GreaterThanEqual,
ShiftLeft,
ShiftRight,
Add,
Subtract,
Multiply,
Divide,
Remainder,
}
#[derive(Debug, Clone)]
pub struct BinaryExpression {
pub this: BinaryExpressionId,
// Phase 1: parser
pub position: InputPosition,
pub left: ExpressionId,
pub operation: BinaryOperator,
pub right: ExpressionId,
// Phase 2: linker
pub parent: ExpressionParent,
// Phase 3: type checking
pub concrete_type: ConcreteType,
}
impl SyntaxElement for BinaryExpression {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UnaryOperation {
Positive,
Negative,
BitwiseNot,
LogicalNot,
PreIncrement,
PreDecrement,
PostIncrement,
PostDecrement,
}
#[derive(Debug, Clone)]
pub struct UnaryExpression {
pub this: UnaryExpressionId,
// Phase 1: parser
pub position: InputPosition,
pub operation: UnaryOperation,
pub expression: ExpressionId,
// Phase 2: linker
pub parent: ExpressionParent,
// Phase 3: type checking
pub concrete_type: ConcreteType,
}
impl SyntaxElement for UnaryExpression {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub struct IndexingExpression {
pub this: IndexingExpressionId,
// Phase 1: parser
pub position: InputPosition,
pub subject: ExpressionId,
pub index: ExpressionId,
// Phase 2: linker
pub parent: ExpressionParent,
// Phase 3: type checking
pub concrete_type: ConcreteType,
}
impl SyntaxElement for IndexingExpression {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub struct SlicingExpression {
pub this: SlicingExpressionId,
// Phase 1: parser
pub position: InputPosition,
pub subject: ExpressionId,
pub from_index: ExpressionId,
pub to_index: ExpressionId,
// Phase 2: linker
pub parent: ExpressionParent,
// Phase 3: type checking
pub concrete_type: ConcreteType,
}
impl SyntaxElement for SlicingExpression {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub struct SelectExpression {
pub this: SelectExpressionId,
// Phase 1: parser
pub position: InputPosition,
pub subject: ExpressionId,
pub field: Field,
// Phase 2: linker
pub parent: ExpressionParent,
// Phase 3: type checking
pub concrete_type: ConcreteType,
}
impl SyntaxElement for SelectExpression {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub struct ArrayExpression {
pub this: ArrayExpressionId,
// Phase 1: parser
pub position: InputPosition,
pub elements: Vec<ExpressionId>,
// Phase 2: linker
pub parent: ExpressionParent,
// Phase 3: type checking
pub concrete_type: ConcreteType,
}
impl SyntaxElement for ArrayExpression {
fn position(&self) -> InputPosition {
self.position
}
}
// TODO: @tokenizer Symbolic function calls are ambiguous with union literals
// that accept embedded values (although the polymorphic arguments are placed
// differently). To prevent double work we parse as CallExpression, and during
// validation we may transform the expression into a union literal.
#[derive(Debug, Clone)]
pub struct CallExpression {
pub this: CallExpressionId,
// Phase 1: parser
pub position: InputPosition,
pub method: Method,
pub arguments: Vec<ExpressionId>,
pub poly_args: Vec<ParserTypeId>, // if symbolic will be determined during validation phase
// Phase 2: linker
pub parent: ExpressionParent,
// Phase 3: type checking
pub concrete_type: ConcreteType,
}
impl SyntaxElement for CallExpression {
fn position(&self) -> InputPosition {
self.position
}
}
#[derive(Debug, Clone)]
pub enum Method {
Get,
Put,
Fires,
Create,
Symbolic(MethodSymbolic)
}
#[derive(Debug, Clone)]
pub struct MethodSymbolic {
pub(crate) identifier: NamespacedIdentifier,
pub(crate) definition: Option<DefinitionId>
}
#[derive(Debug, Clone)]
pub struct LiteralExpression {
pub this: LiteralExpressionId,
// Phase 1: parser
pub position: InputPosition,
pub value: Literal,
// Phase 2: linker
pub parent: ExpressionParent,
// Phase 3: type checking
pub concrete_type: ConcreteType,
}
impl SyntaxElement for LiteralExpression {
fn position(&self) -> InputPosition {
self.position
}
}
type LiteralCharacter = Vec<u8>;
type LiteralInteger = i64; // TODO: @int_literal
#[derive(Debug, Clone)]
pub enum Literal {
Null, // message
True,
False,
Character(LiteralCharacter),
Integer(LiteralInteger),
Struct(LiteralStruct),
Enum(LiteralEnum),
Union(LiteralUnion),
}
impl Literal {
pub(crate) fn as_struct(&self) -> &LiteralStruct {
if let Literal::Struct(literal) = self{
literal
} else {
unreachable!("Attempted to obtain {:?} as Literal::Struct", self)
}
}
pub(crate) fn as_struct_mut(&mut self) -> &mut LiteralStruct {
if let Literal::Struct(literal) = self{
literal
} else {
unreachable!("Attempted to obtain {:?} as Literal::Struct", self)
}
}
pub(crate) fn as_enum(&self) -> &LiteralEnum {
if let Literal::Enum(literal) = self {
literal
} else {
unreachable!("Attempted to obtain {:?} as Literal::Enum", self)
}
}
pub(crate) fn as_union(&self) -> &LiteralUnion {
if let Literal::Union(literal) = self {
literal
} else {
unreachable!("Attempted to obtain {:?} as Literal::Union", self)
}
}
}
#[derive(Debug, Clone)]
pub struct LiteralStructField {
// Phase 1: parser
pub(crate) identifier: Identifier,
pub(crate) value: ExpressionId,
// Phase 2: linker
pub(crate) field_idx: usize, // in struct definition
}
#[derive(Debug, Clone)]
pub struct LiteralStruct {
// Phase 1: parser
pub(crate) identifier: NamespacedIdentifier,
pub(crate) fields: Vec<LiteralStructField>,
// Phase 2: linker
pub(crate) poly_args2: Vec<ParserTypeId>, // taken from identifier once linked to a definition
pub(crate) definition: Option<DefinitionId>
}
// TODO: @tokenizer Enum literals are ambiguous with union literals that do not
// accept embedded values. To prevent double work for now we parse as a
// LiteralEnum, and during validation we may transform the expression into a
// union literal.
#[derive(Debug, Clone)]
pub struct LiteralEnum {
// Phase 1: parser
pub(crate) identifier: NamespacedIdentifier,
// Phase 2: linker
pub(crate) poly_args2: Vec<ParserTypeId>, // taken from identifier once linked to a definition
pub(crate) definition: Option<DefinitionId>,
pub(crate) variant_idx: usize, // as present in the type table
}
#[derive(Debug, Clone)]
pub struct LiteralUnion {
// Phase 1: parser
pub(crate) identifier: NamespacedIdentifier,
pub(crate) values: Vec<ExpressionId>,
// Phase 2: linker
pub(crate) poly_args2: Vec<ParserTypeId>, // taken from identifier once linked to a definition
pub(crate) definition: Option<DefinitionId>,
pub(crate) variant_idx: usize, // as present in type table
}
#[derive(Debug, Clone)]
pub struct VariableExpression {
pub this: VariableExpressionId,
// Phase 1: parser
pub position: InputPosition,
pub identifier: NamespacedIdentifier,
// Phase 2: linker
pub declaration: Option<VariableId>,
pub parent: ExpressionParent,
// Phase 3: type checking
pub concrete_type: ConcreteType,
}
impl SyntaxElement for VariableExpression {
fn position(&self) -> InputPosition {
self.position
}
}
|