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
|
# MODPREP - CLISP module preprocessor
# Bruno Haible 20.9.1998, 10.-11.10.1998
# This preprocessor generates all necessary tables for a CLISP module.
# The input file is normal C code, modified like this:
# - It includes "clisp.h".
# - There is one (and only one) declaration
# DEFMODULE(module_name,"PACKAGE-NAME")
# module_name is the clisp module name.
# PACKAGE-NAME is the default package name (in upper case) for Lisp
# functions.
# - Constant Lisp objects can be referred to using the backquote syntax:
# pushSTACK(`:TEST`);
# value1 = `#()`;
# The backquoted strings are read in at module load time.
# - The definition of Lisp functions is done using the macro
# DEFUN(function_name, lambda_list)
# for example
# DEFUN(foo::bar, x y [&optional z] [&rest foo | &key a b c] )
# &rest and &key cannot be combined (this is a restriction for SUBRs).
# &key requires at least one keyword (this is a restriction for SUBRs too).
# - Variables containing Lisp objects (known to the garbage collector) are
# defined using the macro
# DEFVAR(variable_name, initform)
# where initform is a C form. (You can also specify a Lisp initform, by
# using the backquote syntax.) The variable can be referred to as
# O(variable_name)
# These variables are private to the module.
# Restrictions and caveats:
# - A module should consist of a single file.
# - The last line in the input file should be terminated with a newline.
# - #line lines should not be separated into multiple lines using
# backslash-newline.
# - No multi-line comments should start in a preprocessor line.
# - #if conditions are assumed to be constant from the DEFMODULE call to
# the end of the file. All necessary #define's should therefore be done
# before the DEFMODULE call.
#define local static
#define global
#define var
#define loop while (1)
#define until(exp) while (!(exp))
typedef unsigned char uintB;
typedef unsigned short uintW;
typedef unsigned long uintL;
typedef int boolean;
#define FALSE 0
#define TRUE 1
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#ifndef NULL
#define NULL ((void*)0)
#endif
#ifdef __cplusplus
extern "C" void exit(int);
#endif
#if !(defined(__GNUC__) && !defined(__STRICT_ANSI__))
#define inline
#endif
# Memory utilities.
local char* xmalloc (uintL count)
{
var char* tmp = (char*)malloc(count);
if (!tmp) {
fprintf(stderr,"Virtual memory exhausted.\n");
exit(1);
}
return tmp;
}
local char* xrealloc (void* data, uintL count)
{
var char* tmp = (char*)realloc(data,count);
if (!tmp) {
fprintf(stderr,"Virtual memory exhausted.\n");
exit(1);
}
return tmp;
}
local inline void xfree (void* ptr)
{
free((char*)ptr);
}
# Character utilities.
# Determine whether a character (not newline) is whitespace.
local inline boolean is_whitespace (char c)
{
return (c == ' ') || (c == '\t');
}
# Determine whether a charater is a digit (locale independent).
local inline boolean is_digit (char c)
{
return (c >= '0') && (c <= '9');
}
# String utilities.
# Returns the freshly allocated contenation of 2 strings.
local char* concat2 (const char* str1, const char* str2)
{
var uintL len1 = strlen(str1);
var uintL len2 = strlen(str2);
var char* result = xmalloc(len1+len2+1);
memcpy(result+0,str1,len1);
memcpy(result+len1,str2,len2+1);
return result;
}
# Returns the freshly allocated contenation of 3 strings.
local char* concat3 (const char* str1, const char* str2, const char* str3)
{
var uintL len1 = strlen(str1);
var uintL len2 = strlen(str2);
var uintL len3 = strlen(str3);
var char* result = xmalloc(len1+len2+len3+1);
memcpy(result+0,str1,len1);
memcpy(result+len1,str2,len2);
memcpy(result+len1+len2,str3,len3+1);
return result;
}
# Returns the freshly allocated contenation of 4 strings.
local char* concat4 (const char* str1, const char* str2, const char* str3, const char* str4)
{
var uintL len1 = strlen(str1);
var uintL len2 = strlen(str2);
var uintL len3 = strlen(str3);
var uintL len4 = strlen(str4);
var char* result = xmalloc(len1+len2+len3+len4+1);
memcpy(result+0,str1,len1);
memcpy(result+len1,str2,len2);
memcpy(result+len1+len2,str3,len3);
memcpy(result+len1+len2+len3,str4,len4+1);
return result;
}
# Returns a freshly allocated substring.
local char* substring (const char* str, uintL index1, uintL index2)
{
if (!(index1 <= index2)) abort();
if (!(index2 <= strlen(str))) abort();
{ var uintL len = index2-index1;
var char* result = xmalloc(len+1);
if (len > 0) memcpy(result,str+index1,len);
result[len] = '\0';
return result;
} }
# Returns a freshly allocated substring.
local char* substring_from_to (const char* p1, const char* p2)
{
var uintL length = p2 - p1;
var char* result = (char*) xmalloc(length+1);
memcpy(result,p1,length);
result[length] = '\0';
return result;
}
# Compares two strings for equality.
local inline boolean String_equals (const char* str1, const char* str2)
{
return !strcmp(str1,str2);
}
# Compares two strings for case-insensitive equality.
local boolean String_equalsIgnoreCase (const char* str1, const char* str2)
{
while (*str1 != '\0' && *str2 != '\0') {
var unsigned char c1 = (unsigned char)(*str1++);
var unsigned char c2 = (unsigned char)(*str2++);
if (c1 < 0x80) /* isascii(c1) */
c1 = toupper(c1);
if (c2 < 0x80) /* isascii(c2) */
c2 = toupper(c2);
if (c1 != c2)
return FALSE;
}
# Now *str1 == '\0' || *str2 == '\0'.
return (*str1 == *str2);
}
# Extensible vectors.
typedef struct {
uintL index;
uintL size;
const void* * data;
} Vector;
local inline void Vector_init (Vector* v)
{
v->size = 5;
v->data = (const void* *) xmalloc(v->size * sizeof(const void*));
v->index = 0;
}
local inline uintL Vector_length (const Vector* v)
{
return v->index;
}
local void Vector_add (Vector* v, const void* elt)
{
if (v->index >= v->size) {
v->size = 2 * v->size;
v->data = (const void* *) xrealloc(v->data, v->size * sizeof(const void*));
}
v->data[v->index++] = elt;
}
local const void * Vector_element (const Vector* v, uintL i)
{
if (!(i < v->index)) {
fprintf(stderr,"vector index out of bounds");
abort();
}
return v->data[i];
}
local void Vector_set_element (Vector* v, uintL i, const void* elt)
{
if (!(i < v->index)) {
fprintf(stderr,"vector index out of bounds");
abort();
}
v->data[i] = elt;
}
local void Vector_remove_element (Vector* v, uintL i)
{
if (!(i < v->index)) {
fprintf(stderr,"vector index out of bounds");
abort();
}
v->index--;
for (; i < v->index; i++)
v->data[i] = v->data[i+1];
}
#ifdef unused
local void Vector_init_clone (Vector* w, const Vector* v)
{
w->size = (v->size < 5 ? 5 : v->size);
w->data = (const void* *) xmalloc(w->size * sizeof(const void*));
memcpy(w->data,v->data,v->size * sizeof(const void*));
w->index = v->size;
}
#endif
#ifdef unused
local Vector* Vector_clone (const Vector* v)
{
var Vector* w = (Vector*) xmalloc(sizeof(Vector));
Vector_init_clone(w,v);
return w;
}
#endif
# All data is bufferized in lines.
typedef struct {
long number;
char* contents;
} Line;
local inline Line* make_Line (long number, char* contents)
{
var Line* result = (Line*) xmalloc(sizeof(Line));
result->number = number;
result->contents = contents;
return result;
}
# Current source file.
local const char* file = "(stdin)";
# Current line number (used for input and later the output as well).
local long lineno;
# A vector of lines.
typedef struct {
Vector rep;
} VectorLine;
local inline void VectorLine_init (VectorLine* v)
{
Vector_init(&v->rep);
}
local inline uintL VectorLine_length (const VectorLine* v)
{
return Vector_length(&v->rep);
}
local inline void VectorLine_add (VectorLine* v, const Line* elt)
{
Vector_add(&v->rep,elt);
}
local inline Line* VectorLine_element (const VectorLine* v, uintL i)
{
return (Line*) Vector_element(&v->rep,i);
}
#ifdef unused
local inline void VectorLine_set_element (VectorLine* v, uintL i, const Line* elt)
{
Vector_set_element(&v->rep,i,elt);
}
#endif
# ================================== INPUT ==================================
# The vector of all lines read.
local VectorLine* lines;
# Read a line, or NULL if EOF is encountered.
local char* get_line (FILE* fp)
{
var int len = 1;
var char* line = (char*) xmalloc(len);
var int index = 0;
loop {
var int c = getc(fp);
if (c==EOF) { if (index>0) break; else { xfree(line); return NULL; } }
if (c=='\n') break;
if (index >= len-1) {
len = 2*len;
line = (char*) xrealloc(line,len);
}
line[index++] = c;
}
line[index] = '\0';
return line;
}
# Decode a #line directive. If the line represents a #line directive,
# return the line number. Else return -1.
local int decode_line_directive (const char* line)
{
var uintL n = strlen(line);
var uintL i = 0;
# Skip whitespace.
for (; i < n && is_whitespace(line[i]); i++);
# Parse a '#'.
if (i < n && line[i] == '#')
i++;
else
return -1;
# Skip whitespace.
for (; i < n && is_whitespace(line[i]); i++);
# Check for "line".
if (i+4 < n
&& line[i+0] == 'l'
&& line[i+1] == 'i'
&& line[i+2] == 'n'
&& line[i+3] == 'e'
&& is_whitespace(line[i+4])) {
i += 4;
for (; i < n && is_whitespace(line[i]); i++);
}
# Check for a digit.
if (!(i < n && is_digit(line[i])))
return -1;
{ var uintL i1 = i;
for (; i < n && is_digit(line[i]); i++);
{ var uintL i2 = i;
# Convert digit string to a `long'.
var char* digits = substring(line,i1,i2);
errno = 0;
{ var long result = strtol(digits,NULL,10);
xfree(digits);
if (errno != 0) return -1;
if (result < 0) abort();
# Check for a source file name.
for (; i < n && is_whitespace(line[i]); i++);
if (i < n && line[i] == '"') {
var uintL i3;
i++;
i3 = i;
for (; i < n && line[i] != '"'; i++);
if (i < n && line[i] == '"') {
var uintL i4 = i;
file = substring(line,i3,i4);
}
}
return result;
} } } }
# Read the input file. Fill `lines'.
local void read_all_input (FILE* fp)
{
lineno = 1;
lines = (VectorLine*)xmalloc(sizeof(VectorLine)); VectorLine_init(lines);
{
var boolean is_continuation_line = FALSE;
loop {
var char* line = get_line(fp);
if (!line) break;
if (is_continuation_line) {
var Line* last = VectorLine_element(lines,VectorLine_length(lines)-1);
var char* prev = substring(last->contents,0,strlen(last->contents)-1);
var char* conc = concat2(prev,line);
xfree(last->contents);
xfree(prev);
xfree(line);
line = last->contents = conc;
lineno++;
} else {
var long line_directive = decode_line_directive(line);
if (line_directive >= 0) {
lineno = line_directive;
line = "";
} else {
VectorLine_add(lines,make_Line(lineno,line));
lineno++;
}
}
{ var uintL linelen = strlen(line);
is_continuation_line = (linelen > 0 && line[linelen-1] == '\\');
}
}
} }
# ================================== PARSE ==================================
# A vector of strings.
typedef struct {
Vector rep;
} VectorString;
local inline void VectorString_init (VectorString* v)
{
Vector_init(&v->rep);
}
local VectorString* make_VectorString ()
{
var VectorString* v = (VectorString*) xmalloc(sizeof(VectorString));
VectorString_init(v);
return v;
}
local inline uintL VectorString_length (const VectorString* v)
{
return Vector_length(&v->rep);
}
local inline void VectorString_add (VectorString* v, const char* elt)
{
Vector_add(&v->rep,elt);
}
local inline const char* VectorString_element (const VectorString* v, uintL i)
{
return (const char*) Vector_element(&v->rep,i);
}
local inline void VectorString_set_element (VectorString* v, uintL i, const char* elt)
{
Vector_set_element(&v->rep,i,elt);
}
#ifdef unused
local inline void VectorString_init_clone (VectorString* w, const VectorString* v)
{
Vector_init_clone(&w->rep,&v->rep);
}
#endif
#ifdef unused
local VectorString* VectorString_clone (const VectorString* v)
{
var VectorString* w = (VectorString*) xmalloc(sizeof(VectorString));
VectorString_init_clone(w,v);
return w;
}
#endif
# Tests whether v starts with w.
local boolean VectorString_startsWith (const VectorString* v, const VectorString* w)
{
var uintL n = VectorString_length(w);
if (VectorString_length(v) >= n) {
var uintL i;
for (i = 0; i < n; i++)
if (!String_equals(VectorString_element(v,i),VectorString_element(w,i)))
return FALSE;
return TRUE;
}
return FALSE;
}
# A stack of vectors of strings.
typedef struct {
Vector rep;
} StackVectorString;
local inline void StackVectorString_init (StackVectorString* v)
{
Vector_init(&v->rep);
}
local StackVectorString* make_StackVectorString ()
{
var StackVectorString* v = (StackVectorString*) xmalloc(sizeof(StackVectorString));
StackVectorString_init(v);
return v;
}
local inline uintL StackVectorString_length (const StackVectorString* v)
{
return Vector_length(&v->rep);
}
#ifdef unused
local inline boolean StackVectorString_is_empty (const StackVectorString* v)
{
return StackVectorString_length(v) == 0;
}
#endif
local inline void StackVectorString_push (StackVectorString* v, const VectorString* elt)
{
Vector_add(&v->rep,elt);
}
local inline VectorString* StackVectorString_element (const StackVectorString* v, uintL i)
{
return (VectorString*) Vector_element(&v->rep,i);
}
local VectorString* StackVectorString_peek (StackVectorString* v)
{
var uintL n = StackVectorString_length(v);
if (n == 0) { fprintf(stderr,"stack empty\n"); exit(1); }
return StackVectorString_element(v,n-1);
}
local VectorString* StackVectorString_pop (StackVectorString* v)
{
var uintL n = StackVectorString_length(v);
if (n == 0) { fprintf(stderr,"stack empty\n"); exit(1); }
{ var VectorString* result = StackVectorString_element(v,n-1);
v->rep.index -= 1;
return result;
} }
# Push elt, and optimize: elt can be removed if is starts with an already
# present string sequence. If another element starts with elt, that one can
# be removed.
local void StackVectorString_push_optimize (StackVectorString* v, const VectorString* elt)
{
var uintL n = StackVectorString_length(v);
var uintL i;
for (i = 0; i < n; i++)
if (VectorString_startsWith(elt,StackVectorString_element(v,i)))
return;
for (i = 0; i < n; )
if (VectorString_startsWith(StackVectorString_element(v,i),elt)) {
Vector_remove_element(&v->rep,i);
n--;
} else
i++;
Vector_add(&v->rep,elt);
}
# The #if[def] stack. All the conditions are implicitly combined by &&.
# For every #if we start a new entry in the stack, which is popped when we
# see the corresponding #endif. This is a stack of vector of string, not a
# stack of string, because when a #elif is seen, we add an element to the
# stack without popping the previous one.
local StackVectorString* ifdef_stack;
# Operations on the #if[def] stack.
local void do_if (const char * condition)
{
var VectorString* v = make_VectorString();
VectorString_add(v,condition);
StackVectorString_push(ifdef_stack,v);
}
local void do_else ()
{
var VectorString* v = StackVectorString_peek(ifdef_stack);
var uintL i = VectorString_length(v) - 1;
var const char* lastcondition = VectorString_element(v,i);
lastcondition = concat3("!(",lastcondition,")");
VectorString_set_element(v,i,lastcondition);
}
local void do_elif (const char * condition)
{
var VectorString* v = StackVectorString_peek(ifdef_stack);
var uintL i = VectorString_length(v) - 1;
var const char* lastcondition = VectorString_element(v,i);
lastcondition = concat3("!(",lastcondition,")");
VectorString_set_element(v,i,lastcondition);
VectorString_add(v,condition);
}
local void do_endif ()
{
StackVectorString_pop(ifdef_stack);
}
# Returns the current #if condition.
# It is a vector of strings, implicitly combined by &&.
# The vector is freshly constructed, but the strings are shared.
local VectorString* current_condition ()
{
var VectorString* result = make_VectorString();
var uintL n = StackVectorString_length(ifdef_stack);
var uintL i;
for (i = 0; i < n; i++) {
var const VectorString* v = StackVectorString_element(ifdef_stack,i);
var uintL m = VectorString_length(v);
var uintL j;
for (j = 0; j < m; j++)
VectorString_add(result,VectorString_element(v,j));
}
return result;
}
# Parsing of #if/#else/#elif/#endif lines.
local const char* is_if (const char* line)
{
var uintL n = strlen(line);
var uintL i = 0;
# Skip whitespace.
for (; i < n && is_whitespace(line[i]); i++);
# Parse a '#'.
if (i < n && line[i] == '#')
i++;
else
return NULL;
# Skip whitespace.
for (; i < n && is_whitespace(line[i]); i++);
# Check for "if".
if (i+2 < n
&& line[i+0] == 'i'
&& line[i+1] == 'f'
&& is_whitespace(line[i+2])) {
i += 3;
for (; i < n && is_whitespace(line[i]); i++);
for (; n > i && is_whitespace(line[n-1]); n--);
return substring(line,i,n);
}
# Check for "ifdef".
if (i+5 < n
&& line[i+0] == 'i'
&& line[i+1] == 'f'
&& line[i+2] == 'd'
&& line[i+3] == 'e'
&& line[i+4] == 'f'
&& is_whitespace(line[i+5])) {
i += 6;
for (; i < n && is_whitespace(line[i]); i++);
for (; n > i && is_whitespace(line[n-1]); n--);
{ var char* term = substring(line,i,n);
var const char* result = concat3("defined(",term,")");
xfree(term);
return result;
} }
# Check for "ifndef".
if (i+6 < n
&& line[i+0] == 'i'
&& line[i+1] == 'f'
&& line[i+2] == 'n'
&& line[i+3] == 'd'
&& line[i+4] == 'e'
&& line[i+5] == 'f'
&& is_whitespace(line[i+6])) {
i += 7;
for (; i < n && is_whitespace(line[i]); i++);
for (; n > i && is_whitespace(line[n-1]); n--);
{ var char* term = substring(line,i,n);
var const char* result = concat3("!defined(",term,")");
xfree(term);
return result;
} }
return NULL;
}
local boolean is_else (const char* line)
{
var uintL n = strlen(line);
var uintL i = 0;
# Skip whitespace.
for (; i < n && is_whitespace(line[i]); i++);
# Parse a '#'.
if (i < n && line[i] == '#')
i++;
else
return FALSE;
# Skip whitespace.
for (; i < n && is_whitespace(line[i]); i++);
# Check for "else".
if (i+4 <= n
&& line[i+0] == 'e'
&& line[i+1] == 'l'
&& line[i+2] == 's'
&& line[i+3] == 'e'
&& (i+4 == n || is_whitespace(line[i+4]))) {
return TRUE;
}
return FALSE;
}
local const char* is_elif (const char* line)
{
var uintL n = strlen(line);
var uintL i = 0;
# Skip whitespace.
for (; i < n && is_whitespace(line[i]); i++);
# Parse a '#'.
if (i < n && line[i] == '#')
i++;
else
return NULL;
# Skip whitespace.
for (; i < n && is_whitespace(line[i]); i++);
# Check for "elif".
if (i+4 < n
&& line[i+0] == 'e'
&& line[i+1] == 'l'
&& line[i+2] == 'i'
&& line[i+3] == 'f'
&& is_whitespace(line[i+4])) {
i += 5;
for (; i < n && is_whitespace(line[i]); i++);
for (; n > i && is_whitespace(line[n-1]); n--);
return substring(line,i,n);
}
return NULL;
}
local boolean is_endif (const char* line)
{
var uintL n = strlen(line);
var uintL i = 0;
# Skip whitespace.
for (; i < n && is_whitespace(line[i]); i++);
# Parse a '#'.
if (i < n && line[i] == '#')
i++;
else
return FALSE;
# Skip whitespace.
for (; i < n && is_whitespace(line[i]); i++);
# Check for "endif".
if (i+5 <= n
&& line[i+0] == 'e'
&& line[i+1] == 'n'
&& line[i+2] == 'd'
&& line[i+3] == 'i'
&& line[i+4] == 'f'
&& (i+5 == n || is_whitespace(line[i+5]))) {
return TRUE;
}
return FALSE;
}
# When we see the DEFMODULE(name,package) line, we store the name.
local const char* name_defmodule = NULL;
local uintL line_defmodule = 0; # index of line containing DEFMODULE
local const char* default_packname = NULL;
local boolean is_defmodule (const char* line) {
var uintL n = strlen(line);
var uintL i = 0;
# Skip whitespace.
for (; i < n && is_whitespace(line[i]); i++);
# Check for "DEFMODULE".
if (i+9 < n
&& line[i+0] == 'D'
&& line[i+1] == 'E'
&& line[i+2] == 'F'
&& line[i+3] == 'M'
&& line[i+4] == 'O'
&& line[i+5] == 'D'
&& line[i+6] == 'U'
&& line[i+7] == 'L'
&& line[i+8] == 'E'
&& (is_whitespace(line[i+9]) || line[i+9] == '(')) {
i += 9;
for (; i < n && is_whitespace(line[i]); i++);
if (i < n && line[i] == '(') {
var uintL i1, i2;
i += 1;
i1 = i;
for (; i < n; i++)
if (line[i] == ',' || line[i] == '(' || line[i] == ')')
break;
i2 = i;
if (i < n && line[i] == ',') {
var uintL i3, i4;
i += 1;
i3 = i;
for (; i < n; i++)
if (line[i] == ',' || line[i] == '(' || line[i] == ')')
break;
i4 = i;
if (i < n && line[i] == ')') {
# First macro argument is from i1 to i2, second is from i3 to i4.
while (i1 < i2 && is_whitespace(line[i1])) i1++;
while (i1 < i2 && is_whitespace(line[i2-1])) i2--;
while (i3 < i4 && is_whitespace(line[i3])) i3++;
while (i3 < i4 && is_whitespace(line[i4-1])) i4--;
if (i4-i3 >= 2 && line[i3] == '"' && line[i4-1] == '"') {
name_defmodule = substring(line,i1,i2);
default_packname = substring(line,i3+1,i4-1);
return TRUE;
}
}
}
}
fprintf(stderr,"%s:%ld: invalid DEFMODULE macro syntax: %s\n",file,lineno,line);
exit(1);
}
return FALSE;
}
# Entries for the object table.
typedef struct {
const char* initstring; # The string that, when read by the Lisp reader,
# initializes the object.
const char* tag; # The struct tag we use for this object.
StackVectorString* condition; # The #if condition of this object.
} Objdef;
# A vector of Objdef.
typedef struct {
Vector rep;
} VectorObjdef;
local inline void VectorObjdef_init (VectorObjdef* v)
{
Vector_init(&v->rep);
}
local VectorObjdef* make_VectorObjdef ()
{
var VectorObjdef* v = (VectorObjdef*) xmalloc(sizeof(VectorObjdef));
VectorObjdef_init(v);
return v;
}
local inline uintL VectorObjdef_length (const VectorObjdef* v)
{
return Vector_length(&v->rep);
}
local inline void VectorObjdef_add (VectorObjdef* v, Objdef* elt)
{
Vector_add(&v->rep,elt);
}
local inline Objdef* VectorObjdef_element (const VectorObjdef* v, uintL i)
{
return (Objdef*) Vector_element(&v->rep,i);
}
#ifdef unused
local inline void VectorObjdef_set_element (VectorObjdef* v, uintL i, Objdef* elt)
{
Vector_set_element(&v->rep,i,elt);
}
#endif
# The table of all Objdefs.
local VectorObjdef* objdefs;
# Looks up an Objdef with a given tag.
local Objdef* get_objdef_from_tag (const char* tag)
{
var uintL n = VectorObjdef_length(objdefs);
var uintL i;
for (i = 0; i < n; i++) {
var Objdef* odef = VectorObjdef_element(objdefs,i);
if (String_equals(odef->tag,tag))
return odef;
}
return NULL;
}
# Looks up or creates an Objdef for a given initstring.
local Objdef* get_objdef_aux (const char* initstring, VectorString* condition)
{
var Objdef* odef;
# First search in the table.
{
var uintL n = VectorObjdef_length(objdefs);
var uintL i;
for (i = 0; i < n; i++) {
odef = VectorObjdef_element(objdefs,i);
if (String_equals(odef->initstring,initstring))
goto found;
}
}
# Create the tag.
{
var char* tagbuf = (char*) xmalloc(3*strlen(initstring)+10);
var char* q = tagbuf;
strcpy(q,"object_");
q += strlen(q);
{
var const char* p;
for (p = initstring; *p != '\0'; p++) {
var char c = *p;
if (c >= 'A' && c <= 'Z')
*q++ = c+32;
else if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'))
*q++ = c;
else if (c == ':' && p == initstring)
*q++ = 'K';
else {
*q++ = '_';
*q++ = "0123456789ABCDEF"[((unsigned char)c >> 4) & 0x0F];
*q++ = "0123456789ABCDEF"[(unsigned char)c & 0x0F];
}
}
}
*q = '\0';
if (get_objdef_from_tag(tagbuf) != NULL) {
var int i;
# Append a suffix to make sure tags are unique.
for (i = 1; ; i++) {
sprintf(q,"_%d",i);
if (get_objdef_from_tag(tagbuf) == NULL)
break;
}
}
# Found a unique tag. Allocate a new Objdef.
{
var char* tag = (char*) xmalloc(strlen(tagbuf)+1);
strcpy(tag,tagbuf);
xfree(tagbuf);
odef = (Objdef*) xmalloc(sizeof(Objdef));
odef->initstring = initstring;
odef->tag = tag;
odef->condition = make_StackVectorString();
VectorObjdef_add(objdefs,odef);
}
}
found:
StackVectorString_push_optimize(odef->condition,condition);
return odef;
}
local inline Objdef* get_objdef (const char* initstring)
{
return get_objdef_aux(initstring,current_condition());
}
# Representation of a function signature.
typedef struct {
int req;
int opt;
boolean rest;
boolean key;
VectorObjdef* keywords;
StackVectorString* condition; # The #if condition of this object.
} Signature;
# Compares two signatures for equality (without condition).
local boolean Signature_equals (const Signature* sig1, const Signature* sig2)
{
if (sig1->req == sig2->req) {
if (sig1->opt == sig2->opt) {
if (sig1->rest == sig2->rest) {
if (sig1->key == sig2->key) {
var uintL len1 = VectorObjdef_length(sig1->keywords);
var uintL len2 = VectorObjdef_length(sig2->keywords);
if (len1 == len2) {
var uintL i;
for (i = 0; i < len1; i++)
if (VectorObjdef_element(sig1->keywords,i) != VectorObjdef_element(sig2->keywords,i))
return FALSE;
return TRUE;
}
}
}
}
}
return FALSE;
}
# Returns the signature (without condition) denoted by the line, or NULL
# in case of syntax error.
# Example for line: "x y [&optional z] [&rest foo | &key a b c]"
local Signature* parseSignature (const char* line)
{
var int req = 0;
var int opt = 0;
var int rest = 0;
var VectorObjdef* keywords = make_VectorObjdef();
var boolean optional_seen = FALSE;
var boolean rest_seen = FALSE;
var boolean key_seen = FALSE;
# Go through the line and tokenize it.
var const char* p1 = line;
var const char* p2;
for (;;) {
while (is_whitespace(*p1))
p1++;
if (*p1 == '\0')
break;
p2 = p1;
while (*p2 != '\0' && !is_whitespace(*p2))
p2++;
# Found a token from p1 to p2.
{
var char* token = substring_from_to(p1,p2);
# Analyze the token.
if (String_equalsIgnoreCase(token,"&optional")) {
if (optional_seen || rest_seen || key_seen)
return NULL;
optional_seen = TRUE;
} else if (String_equalsIgnoreCase(token,"&key")) {
if (rest_seen || key_seen)
return NULL;
key_seen = TRUE;
} else if (String_equalsIgnoreCase(token,"&rest")) {
if (rest_seen || key_seen)
return NULL;
rest_seen = TRUE;
} else {
if (rest_seen)
rest++;
else if (key_seen) {
var char* keyword_name;
var char* keyword_initstring;
var Objdef* keyword_odef;
if ((keyword_name = strrchr(token,':')) != NULL)
keyword_name = keyword_name+1;
else
keyword_name = token;
keyword_initstring = concat2(":",keyword_name);
keyword_odef = get_objdef(keyword_initstring);
VectorObjdef_add(keywords,keyword_odef);
} else if (optional_seen)
opt++;
else
req++;
}
xfree(token);
}
p1 = p2;
}
if (rest_seen && rest != 1)
return NULL;
{
var Signature* sig = (Signature*) xmalloc(sizeof(Signature));
sig->req = req;
sig->opt = opt;
sig->rest = rest_seen;
sig->key = key_seen;
sig->keywords = keywords;
return sig;
}
}
# A vector of Signature.
typedef struct {
Vector rep;
} VectorSignature;
local inline void VectorSignature_init (VectorSignature* v)
{
Vector_init(&v->rep);
}
local VectorSignature* make_VectorSignature ()
{
var VectorSignature* v = (VectorSignature*) xmalloc(sizeof(VectorSignature));
VectorSignature_init(v);
return v;
}
local inline uintL VectorSignature_length (const VectorSignature* v)
{
return Vector_length(&v->rep);
}
local inline void VectorSignature_add (VectorSignature* v, Signature* elt)
{
Vector_add(&v->rep,elt);
}
local inline Signature* VectorSignature_element (const VectorSignature* v, uintL i)
{
return (Signature*) Vector_element(&v->rep,i);
}
#ifdef unused
local inline void VectorSignature_set_element (VectorSignature* v, uintL i, Signature* elt)
{
Vector_set_element(&v->rep,i,elt);
}
#endif
# Entries for the function table.
typedef struct {
const char* packname; # The symbol's package name
const char* printname; # The symbol's print name
const char* tag; # The struct tag we use for this function
StackVectorString* condition; # The total #if condition of this function.
VectorSignature* signatures; # The functions possible signatures, together
# with their individual #if conditions.
} Fundef;
# A vector of Fundef.
typedef struct {
Vector rep;
} VectorFundef;
local inline void VectorFundef_init (VectorFundef* v)
{
Vector_init(&v->rep);
}
local VectorFundef* make_VectorFundef ()
{
var VectorFundef* v = (VectorFundef*) xmalloc(sizeof(VectorFundef));
VectorFundef_init(v);
return v;
}
local inline uintL VectorFundef_length (const VectorFundef* v)
{
return Vector_length(&v->rep);
}
local inline void VectorFundef_add (VectorFundef* v, Fundef* elt)
{
Vector_add(&v->rep,elt);
}
local inline Fundef* VectorFundef_element (const VectorFundef* v, uintL i)
{
return (Fundef*) Vector_element(&v->rep,i);
}
#ifdef unused
local inline void VectorFundef_set_element (VectorFundef* v, uintL i, Fundef* elt)
{
Vector_set_element(&v->rep,i,elt);
}
#endif
# The table of all Fundefs.
local VectorFundef* fundefs;
# Looks up an Fundef with a given tag.
local Fundef* get_fundef_from_tag (const char* tag)
{
var uintL n = VectorFundef_length(fundefs);
var uintL i;
for (i = 0; i < n; i++) {
var Fundef* fdef = VectorFundef_element(fundefs,i);
if (String_equals(fdef->tag,tag))
return fdef;
}
return NULL;
}
# Looks up or creates a Fundef for a given function name, and adds the given
# signature to it.
local Fundef* get_fundef_aux (const char* funname, Signature* sig, VectorString* condition)
{
var Fundef* fdef;
# Split the function name into a package name and a print name.
var const char* packname;
var const char* printname;
{
var const char* tmp = strchr(funname,':');
if (tmp) {
packname = substring_from_to(funname,tmp);
tmp++;
if (*tmp == ':')
tmp++;
if (strchr(tmp,':')) {
fprintf(stderr,"%s:%ld: Function definition symbol has too many package markers: %s\n",file,lineno,funname);
exit(1);
}
printname = substring(tmp,0,strlen(tmp));
} else {
if (default_packname == NULL) {
fprintf(stderr,"%s:%ld: Function definition needs a packaged symbol: %s\n",file,lineno,funname);
exit(1);
} else {
packname = default_packname;
printname = funname;
}
}
}
# First search in the table.
{
var uintL n = VectorFundef_length(fundefs);
var uintL i;
for (i = 0; i < n; i++) {
fdef = VectorFundef_element(fundefs,i);
if (String_equals(fdef->packname,packname) && String_equals(fdef->printname,printname))
goto found;
}
}
# Create the tag.
{
var char* initstring = concat3(packname,":",printname);
var char* tagbuf = (char*) xmalloc(3*strlen(initstring)+10);
var char* q = tagbuf;
strcpy(q,"subr_");
q += strlen(q);
{
var const char* p;
for (p = initstring; *p != '\0'; p++) {
var char c = *p;
if (c >= 'A' && c <= 'Z')
*q++ = c+32;
else if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'))
*q++ = c;
else if (c == ':') {
*q++ = '_';
*q++ = '_';
} else {
*q++ = '_';
*q++ = "0123456789ABCDEF"[((unsigned char)c >> 4) & 0x0F];
*q++ = "0123456789ABCDEF"[(unsigned char)c & 0x0F];
}
}
}
*q = '\0';
if (get_fundef_from_tag(tagbuf) != NULL) {
var int i;
# Append a suffix to make sure tags are unique.
for (i = 1; ; i++) {
sprintf(q,"_%d",i);
if (get_fundef_from_tag(tagbuf) == NULL)
break;
}
}
# Found a unique tag. Allocate a new Fundef.
{
var char* tag = (char*) xmalloc(strlen(tagbuf)+1);
strcpy(tag,tagbuf);
xfree(tagbuf);
fdef = (Fundef*) xmalloc(sizeof(Fundef));
fdef->packname = packname;
fdef->printname = printname;
fdef->tag = tag;
fdef->condition = make_StackVectorString();
fdef->signatures = make_VectorSignature();
VectorFundef_add(fundefs,fdef);
}
}
found:
StackVectorString_push_optimize(fdef->condition,condition);
# Now add the signature.
{
var VectorSignature* signatures = fdef->signatures;
var uintL n = VectorSignature_length(signatures);
var uintL i;
for (i = 0; i < n; i++) {
var Signature* sig_i = VectorSignature_element(signatures,i);
if (Signature_equals(sig_i,sig)) {
StackVectorString_push_optimize(sig_i->condition,condition);
goto done_signature;
}
}
sig->condition = make_StackVectorString();
StackVectorString_push_optimize(sig->condition,condition);
VectorSignature_add(signatures,sig);
}
done_signature:
return fdef;
}
local inline Fundef* get_fundef (const char* funname, Signature* sig)
{
return get_fundef_aux(funname,sig,current_condition());
}
# Print a signature in a form suitable as argument list for LISPFUN.
local char* get_signature_for_LISPFUN (const Fundef* fdef, const Signature* sig)
{
# sprintf(buffer,"(%s,%d,%d,%s,%s,%lu,NIL)",
# fdef->tag,
# sig->req,
# sig->opt,
# (sig->rest?"rest":"norest"),
# (sig->key?"key":"nokey"),
# VectorObjdef_length(sig->keywords));
var char buffer[1+10+1+10+1+6+1+5+10+5+1];
sprintf(buffer,",%d,%d,%s,%s,%lu,NIL)",
sig->req,
sig->opt,
(sig->rest?"rest":"norest"),
(sig->key?"key":"nokey"),
VectorObjdef_length(sig->keywords));
return concat3("(",fdef->tag,buffer);
}
# Parse a DEFUN(funname,lambdalist) line,
# and turn it into DEFUN(funname,lambdalist,signature).
local char* is_defun (const char* line) {
var uintL n = strlen(line);
var uintL i = 0;
# Skip whitespace.
for (; i < n && is_whitespace(line[i]); i++);
# Check for "DEFUN".
if (i+5 < n
&& line[i+0] == 'D'
&& line[i+1] == 'E'
&& line[i+2] == 'F'
&& line[i+3] == 'U'
&& line[i+4] == 'N'
&& (is_whitespace(line[i+5]) || line[i+5] == '(')) {
i += 5;
for (; i < n && is_whitespace(line[i]); i++);
if (i < n && line[i] == '(') {
var uintL i1, i2, i3, i4;
i += 1;
i1 = i;
for (; i < n; i++)
if (line[i] == ',' || line[i] == '(' || line[i] == ')')
break;
i2 = i;
if (i < n && line[i] == ',') {
i += 1;
i3 = i;
for (; i < n; i++)
if (line[i] == ',' || line[i] == '(' || line[i] == ')')
break;
i4 = i;
if (i < n && line[i] == ')') {
# First macro argument is from i1 to i2, second is from i3 to i4.
while (i1 < i2 && is_whitespace(line[i1])) i1++;
while (i1 < i2 && is_whitespace(line[i2-1])) i2--;
{
var char* funname = substring(line,i1,i2);
var char* lambdalist = substring(line,i3,i4);
var Signature* signature = parseSignature(lambdalist);
if (signature == NULL) {
fprintf(stderr,"%s:%ld: invalid lambdalist syntax for function `%s': %s\n",file,lineno,funname,lambdalist);
exit(1);
}
{
var Fundef* fdef = get_fundef(funname,signature);
return concat4(substring(line,0,i),",",get_signature_for_LISPFUN(fdef,signature),substring(line,i,n));
}
}
}
}
}
fprintf(stderr,"%s:%ld: invalid DEFUN macro syntax: %s\n",file,lineno,line);
exit(1);
}
return NULL;
}
# Entries for the variable table.
typedef struct {
const char* tag; # The struct tag of this variable.
StackVectorString* condition; # The total #if condition of this variable.
} Vardef;
# A vector of Vardef.
typedef struct {
Vector rep;
} VectorVardef;
local inline void VectorVardef_init (VectorVardef* v)
{
Vector_init(&v->rep);
}
local VectorVardef* make_VectorVardef ()
{
var VectorVardef* v = (VectorVardef*) xmalloc(sizeof(VectorVardef));
VectorVardef_init(v);
return v;
}
local inline uintL VectorVardef_length (const VectorVardef* v)
{
return Vector_length(&v->rep);
}
local inline void VectorVardef_add (VectorVardef* v, Vardef* elt)
{
Vector_add(&v->rep,elt);
}
local inline Vardef* VectorVardef_element (const VectorVardef* v, uintL i)
{
return (Vardef*) Vector_element(&v->rep,i);
}
local inline void VectorVardef_set_element (VectorVardef* v, uintL i, Vardef* elt)
{
Vector_set_element(&v->rep,i,elt);
}
# The table of all Vardefs.
local VectorVardef* vardefs;
# Looks up an Vardef with a given tag.
local Vardef* get_vardef_from_tag (const char* tag)
{
var uintL n = VectorVardef_length(vardefs);
var uintL i;
for (i = 0; i < n; i++) {
var Vardef* vdef = VectorVardef_element(vardefs,i);
if (String_equals(vdef->tag,tag))
return vdef;
}
return NULL;
}
# Looks up or creates a Vardef for a given variable name.
local Vardef* get_vardef_aux (const char* varname, VectorString* condition)
{
var Vardef* vdef;
# First search in the table.
vdef = get_vardef_from_tag(varname);
if (vdef == NULL) {
# Allocate a new Vardef.
vdef = (Vardef*) xmalloc(sizeof(Vardef));
vdef->tag = varname;
vdef->condition = make_StackVectorString();
VectorVardef_add(vardefs,vdef);
}
StackVectorString_push_optimize(vdef->condition,condition);
return vdef;
}
local inline Vardef* get_vardef (const char* varname)
{
return get_vardef_aux(varname,current_condition());
}
# Variable initializers. (We treat them separately from the variables
# themselves, so that they are executed in order.)
typedef struct {
const char* tag; # The struct tag of this variable.
const char* initform; # A C expression initializing this variable.
VectorString* condition; # The #if condition of this initializer.
} Varinit;
local inline Varinit* make_Varinit (const char* tag, const char* initform, VectorString* condition)
{
var Varinit* v = (Varinit*) xmalloc(sizeof(Varinit));
v->tag = tag;
v->initform = initform;
v->condition = condition;
return v;
}
# A vector of Varinit.
typedef struct {
Vector rep;
} VectorVarinit;
local inline void VectorVarinit_init (VectorVarinit* v)
{
Vector_init(&v->rep);
}
local VectorVarinit* make_VectorVarinit ()
{
var VectorVarinit* v = (VectorVarinit*) xmalloc(sizeof(VectorVarinit));
VectorVarinit_init(v);
return v;
}
local inline uintL VectorVarinit_length (const VectorVarinit* v)
{
return Vector_length(&v->rep);
}
local inline void VectorVarinit_add (VectorVarinit* v, Varinit* elt)
{
Vector_add(&v->rep,elt);
}
local inline Varinit* VectorVarinit_element (const VectorVarinit* v, uintL i)
{
return (Varinit*) Vector_element(&v->rep,i);
}
local inline void VectorVarinit_set_element (VectorVarinit* v, uintL i, Varinit* elt)
{
Vector_set_element(&v->rep,i,elt);
}
# The list of all variable initializers, as they appear in the source.
local VectorVarinit* varinits;
# Parse a DEFVAR(varname,initform) line, and turn it into DEFVAR(varname).
# (We remove the initform because it might contain backquoted stuff including
# commas and parentheses.)
local char* is_defvar (const char* line) {
var uintL n = strlen(line);
var uintL i = 0;
# Skip whitespace.
for (; i < n && is_whitespace(line[i]); i++);
# Check for "DEFVAR".
if (i+6 < n
&& line[i+0] == 'D'
&& line[i+1] == 'E'
&& line[i+2] == 'F'
&& line[i+3] == 'V'
&& line[i+4] == 'A'
&& line[i+5] == 'R'
&& (is_whitespace(line[i+6]) || line[i+6] == '(')) {
i += 6;
for (; i < n && is_whitespace(line[i]); i++);
if (i < n && line[i] == '(') {
var uintL i1, i2, i3, i4;
i += 1;
i1 = i;
for (; i < n; i++)
if (line[i] == ',' || line[i] == '(' || line[i] == ')')
break;
i2 = i;
if (i < n && line[i] == ',') {
i += 1;
i3 = i;
# Start a lexical analysis.
{
var int in_comment = 0; # comment depth
var boolean in_string = FALSE; # in "..." ?
var boolean in_char = FALSE; # in '...' ?
var boolean in_subst = FALSE; # in `...` ?
var long subst_start = -1;
var int in_paren = 0; # parentheses depth
while (i < n) {
var char c = line[i];
if (!in_comment && !in_char && !in_string && !in_subst
&& c == '(') {
in_paren++;
i++;
} else if (!in_comment && !in_char && !in_string && !in_subst
&& !in_paren && c == ',') {
fprintf(stderr,"%s:%ld: DEFVAR macro with two many arguments\n",file,lineno);
exit(1);
} else if (!in_comment && !in_char && !in_string && !in_subst
&& c == ')') {
if (in_paren > 0) {
in_paren--;
i++;
} else
break;
} else if (!in_char && !in_string && !in_subst
&& i+1 < n && c == '/' && line[i+1] == '*') {
in_comment++;
i += 2;
} else if (!in_char && !in_string && !in_subst
&& i+1 < n && c == '*' && line[i+1] == '/') {
in_comment--;
if (in_comment < 0)
in_comment = 0;
i += 2;
} else if (i+1 < n && c == '\\') {
if (in_subst && line[i+1] == '`') {
# Inside subst, convert \` to simple `
line = concat2(substring(line,0,i),substring(line,i+1,n));
n--;
i++;
} else
i += 2;
} else if (!in_comment && !in_char && !in_subst && c == '"') {
in_string = !in_string;
i++;
} else if (!in_comment && !in_string && !in_subst && c == '\'') {
in_char = !in_char;
i++;
} else if (!in_comment && !in_char && !in_string && c == '`') {
if (!in_subst) {
in_subst = TRUE;
subst_start = i;
i++;
} else {
var const char* initstring = substring(line,subst_start+1,i);
var Objdef* odef = get_objdef(initstring);
line = concat3(substring(line,0,subst_start),odef->tag,substring(line,i+1,n));
n = strlen(line);
i = subst_start + strlen(odef->tag);
in_subst = FALSE;
subst_start = -1;
}
} else
i++;
}
if (i == n) {
if (in_string) {
fprintf(stderr,"%s:%ld: string not terminated\n",file,lineno);
exit(1);
}
if (in_char) {
fprintf(stderr,"%s:%ld: character not terminated\n",file,lineno);
exit(1);
}
if (in_subst) {
fprintf(stderr,"%s:%ld: backquote not terminated\n",file,lineno);
exit(1);
}
if (in_comment) {
fprintf(stderr,"%s:%ld: comment inside DEFVAR not terminated\n",file,lineno);
exit(1);
}
fprintf(stderr,"%s:%ld: DEFVAR macro not terminated\n",file,lineno);
exit(1);
}
}
i4 = i;
# First macro argument is from i1 to i2, second is from i3 to i4.
while (i1 < i2 && is_whitespace(line[i1])) i1++;
while (i1 < i2 && is_whitespace(line[i2-1])) i2--;
{
var char* varname = substring(line,i1,i2);
var Vardef* vdef = get_vardef(varname);
VectorVarinit_add(varinits,make_Varinit(vdef->tag,substring(line,i3,i4),current_condition()));
# Remove the second macro argument.
return concat2(substring(line,0,i3-1),substring(line,i4,n));
}
}
}
fprintf(stderr,"%s:%ld: invalid DEFVAR macro syntax: %s\n",file,lineno,line);
exit(1);
}
return NULL;
}
# Parse the entire input.
local void parse ()
{
# Initialize variables.
ifdef_stack = make_StackVectorString();
objdefs = make_VectorObjdef();
fundefs = make_VectorFundef();
vardefs = make_VectorVardef();
varinits = make_VectorVarinit();
{
var int in_comment = 0; # comment depth
var boolean in_string = FALSE; # in "..." ?
var boolean in_char = FALSE; # in '...' ?
var boolean in_subst = FALSE; # in `...` ?
var long subst_start = -1;
var int m = VectorLine_length(lines);
var int j;
for (j = 0; j < m; j++) {
var char* line = VectorLine_element(lines,j)->contents;
lineno = VectorLine_element(lines,j)->number;
if (in_comment == 0) {
# Check for DEFMODULE.
if (is_defmodule(line))
line_defmodule = j;
# Check for preprocessor commands.
{
var const char* condition;
if ((condition = is_if(line)) != NULL)
do_if(condition);
else if (is_else(line))
do_else();
else if ((condition = is_elif(line)) != NULL)
do_elif(condition);
else if (is_endif(line))
do_endif();
}
# Check for DEFUN.
{
var char* expanded_line = is_defun(line);
if (expanded_line != NULL)
line = expanded_line;
}
# Check for DEFVAR.
{
var char* expanded_line = is_defvar(line);
if (expanded_line != NULL)
line = expanded_line;
}
}
# General lexical analysis of the line.
{
var uintL n = strlen(line);
var uintL i;
for (i = 0; i < n; ) {
var char c = line[i];
if (!in_char && !in_string && !in_subst
&& i+1 < n && c == '/' && line[i+1] == '*') {
in_comment++;
i += 2;
} else if (!in_char && !in_string && !in_subst
&& i+1 < n && c == '*' && line[i+1] == '/') {
in_comment--;
if (in_comment < 0)
in_comment = 0;
i += 2;
} else if (i+1 < n && c == '\\') {
if (in_subst && line[i+1] == '`') {
# Inside subst, convert \` to simple `
line = concat2(substring(line,0,i),substring(line,i+1,n));
n--;
i++;
} else
i += 2;
} else if (!in_comment && !in_char && !in_subst && c == '"') {
in_string = !in_string;
i++;
} else if (!in_comment && !in_string && !in_subst && c == '\'') {
in_char = !in_char;
i++;
} else if (!in_comment && !in_char && !in_string && c == '`') {
if (!in_subst) {
in_subst = TRUE;
subst_start = i;
i++;
} else {
var const char* initstring = substring(line,subst_start+1,i);
var Objdef* odef = get_objdef(initstring);
line = concat3(substring(line,0,subst_start),odef->tag,substring(line,i+1,n));
n = strlen(line);
i = subst_start + strlen(odef->tag);
in_subst = FALSE;
subst_start = -1;
}
} else
i++;
}
}
VectorLine_element(lines,j)->contents = line;
if (in_string) {
fprintf(stderr,"%s:%ld: string not terminated\n",file,lineno);
exit(1);
}
if (in_char) {
fprintf(stderr,"%s:%ld: character not terminated\n",file,lineno);
exit(1);
}
if (in_subst) {
fprintf(stderr,"%s:%ld: backquote not terminated\n",file,lineno);
exit(1);
}
}
}
}
# ================================== OUTPUT =================================
# Print a list (cond1 && cond2 && ...) to a stream.
local void print_condition_part (FILE* stream, const VectorString* condition)
{
var uintL n = VectorString_length(condition);
if (n == 0) {
fprintf(stream,"1");
return;
}
if (n == 1) {
fprintf(stream,"%s",VectorString_element(condition,0));
return;
}
{
var uintL i;
for (i = 0; i < n; i++) {
if (i > 0)
fprintf(stream," && ");
fprintf(stream,"(%s)",VectorString_element(condition,i));
}
}
}
# Tests whether a condition is equivalent to 1 (true).
local inline boolean is_true_condition_part (VectorString* condition)
{
var uintL n = VectorString_length(condition);
return (n == 0);
}
# Print a list of lists (cond1 || cond2 || ...) to a stream.
local void print_condition (FILE* stream, StackVectorString* condition)
{
var uintL n = StackVectorString_length(condition);
if (n == 0) {
fprintf(stream,"0");
return;
}
if (n == 1) {
print_condition_part(stream,StackVectorString_element(condition,0));
return;
}
{
var uintL i;
for (i = 0; i < n; i++) {
if (i > 0)
fprintf(stream," || ");
fprintf(stream,"(");
print_condition_part(stream,StackVectorString_element(condition,i));
fprintf(stream,")");
}
}
}
# Tests whether a condition is equivalent to 0 (false).
local inline boolean is_false_condition (StackVectorString* condition)
{
var uintL n = StackVectorString_length(condition);
return (n == 0);
}
# Tests whether a condition is equivalent to 1 (true).
local boolean is_true_condition (StackVectorString* condition)
{
var uintL n = StackVectorString_length(condition);
var uintL i;
for (i = 0; i < n; i++)
if (is_true_condition_part(StackVectorString_element(condition,i)))
return TRUE;
return FALSE;
}
# Prints a newline. Don't print a "\n" directly!
local inline void print_nl (FILE* stream)
{
fprintf(stream,"\n");
lineno++;
}
# Prints a string in C syntax.
local void print_C_string (FILE* stream, const char* data)
{
putc('"',stream);
for (; *data != '\0'; data++) {
var char c = *data;
if (c == '"' || c == '\\')
putc('\\',stream);
putc(c,stream);
}
putc('"',stream);
}
# Output the tables just after the DEFMODULE line.
local void output_tables1 (FILE* stream)
{
var const char* modname = name_defmodule;
var const char* object_tab = concat3("module__",modname,"__object_tab");
var const char* object_tab_initdata = concat3("module__",modname,"__object_tab_initdata");
fprintf(stream,"#define O(varname) %s._##varname",object_tab);
print_nl(stream);
print_nl(stream);
fprintf(stream,"struct {");
print_nl(stream);
{
var uintL n = VectorObjdef_length(objdefs);
var uintL i;
for (i = 0; i < n; i++) {
var Objdef* odef = VectorObjdef_element(objdefs,i);
if (!is_false_condition(odef->condition)) {
if (!is_true_condition(odef->condition)) {
fprintf(stream,"#if ");
print_condition(stream,odef->condition);
print_nl(stream);
}
fprintf(stream," object _%s;",odef->tag);
print_nl(stream);
if (!is_true_condition(odef->condition)) {
fprintf(stream,"#endif");
print_nl(stream);
}
}
}
}
{
var uintL n = VectorVardef_length(vardefs);
var uintL i;
for (i = 0; i < n; i++) {
var Vardef* vdef = VectorVardef_element(vardefs,i);
if (!is_false_condition(vdef->condition)) {
if (!is_true_condition(vdef->condition)) {
fprintf(stream,"#if ");
print_condition(stream,vdef->condition);
print_nl(stream);
}
fprintf(stream," object _%s;",vdef->tag);
print_nl(stream);
if (!is_true_condition(vdef->condition)) {
fprintf(stream,"#endif");
print_nl(stream);
}
}
}
}
fprintf(stream,"} %s;",object_tab);
print_nl(stream);
{
var uintL n = VectorObjdef_length(objdefs);
var uintL i;
for (i = 0; i < n; i++) {
var Objdef* odef = VectorObjdef_element(objdefs,i);
if (!is_false_condition(odef->condition)) {
fprintf(stream,"#define %s %s._%s",odef->tag,object_tab,odef->tag);
print_nl(stream);
}
}
}
fprintf(stream,"uintC module__%s__object_tab_size = sizeof(%s)/sizeof(object);",modname,object_tab);
print_nl(stream);
print_nl(stream);
fprintf(stream,"struct {");
print_nl(stream);
{
var uintL n = VectorObjdef_length(objdefs);
var uintL i;
for (i = 0; i < n; i++) {
var Objdef* odef = VectorObjdef_element(objdefs,i);
if (!is_false_condition(odef->condition)) {
if (!is_true_condition(odef->condition)) {
fprintf(stream,"#if ");
print_condition(stream,odef->condition);
print_nl(stream);
}
fprintf(stream," object_initdata _%s;",odef->tag);
print_nl(stream);
if (!is_true_condition(odef->condition)) {
fprintf(stream,"#endif");
print_nl(stream);
}
}
}
}
{
var uintL n = VectorVardef_length(vardefs);
var uintL i;
for (i = 0; i < n; i++) {
var Vardef* vdef = VectorVardef_element(vardefs,i);
if (!is_false_condition(vdef->condition)) {
if (!is_true_condition(vdef->condition)) {
fprintf(stream,"#if ");
print_condition(stream,vdef->condition);
print_nl(stream);
}
fprintf(stream," object_initdata _%s;",vdef->tag);
print_nl(stream);
if (!is_true_condition(vdef->condition)) {
fprintf(stream,"#endif");
print_nl(stream);
}
}
}
}
fprintf(stream," int _dummy_to_avoid_trailing_comma_in_initializer;");
print_nl(stream);
fprintf(stream,"} %s = {",object_tab_initdata);
print_nl(stream);
{
var uintL n = VectorObjdef_length(objdefs);
var uintL i;
for (i = 0; i < n; i++) {
var Objdef* odef = VectorObjdef_element(objdefs,i);
if (!is_false_condition(odef->condition)) {
if (!is_true_condition(odef->condition)) {
fprintf(stream,"#if ");
print_condition(stream,odef->condition);
print_nl(stream);
}
fprintf(stream," { ");
print_C_string(stream,odef->initstring);
fprintf(stream," },");
print_nl(stream);
if (!is_true_condition(odef->condition)) {
fprintf(stream,"#endif");
print_nl(stream);
}
}
}
}
{
var uintL n = VectorVardef_length(vardefs);
var uintL i;
for (i = 0; i < n; i++) {
var Vardef* vdef = VectorVardef_element(vardefs,i);
if (!is_false_condition(vdef->condition)) {
if (!is_true_condition(vdef->condition)) {
fprintf(stream,"#if ");
print_condition(stream,vdef->condition);
print_nl(stream);
}
fprintf(stream," { ");
print_C_string(stream,"NIL");
fprintf(stream," },");
print_nl(stream);
if (!is_true_condition(vdef->condition)) {
fprintf(stream,"#endif");
print_nl(stream);
}
}
}
}
fprintf(stream," 0");
print_nl(stream);
fprintf(stream,"};");
print_nl(stream);
print_nl(stream);
}
# Output the tables at the end of the file.
local void output_tables2 (FILE* stream)
{
var const char* modname = name_defmodule;
var const char* subr_tab = concat3("module__",modname,"__subr_tab");
var const char* subr_tab_initdata = concat3("module__",modname,"__subr_tab_initdata");
print_nl(stream);
fprintf(stream,"struct {");
print_nl(stream);
{
var uintL n = VectorFundef_length(fundefs);
var uintL i;
for (i = 0; i < n; i++) {
var Fundef* fdef = VectorFundef_element(fundefs,i);
if (!is_false_condition(fdef->condition)) {
if (!is_true_condition(fdef->condition)) {
fprintf(stream,"#if ");
print_condition(stream,fdef->condition);
print_nl(stream);
}
fprintf(stream," subr_ _%s;",fdef->tag);
print_nl(stream);
if (!is_true_condition(fdef->condition)) {
fprintf(stream,"#endif");
print_nl(stream);
}
}
}
}
fprintf(stream," int _dummy_to_avoid_trailing_comma_in_initializer;");
print_nl(stream);
fprintf(stream,"} %s = {",subr_tab);
print_nl(stream);
{
var uintL n = VectorFundef_length(fundefs);
var uintL i;
for (i = 0; i < n; i++) {
var Fundef* fdef = VectorFundef_element(fundefs,i);
var VectorSignature* signatures = fdef->signatures;
var uintL m = VectorSignature_length(signatures);
var uintL j;
for (j = 0; j < m; j++) {
var Signature* sig = VectorSignature_element(signatures,j);
if (!is_false_condition(sig->condition)) {
if (!is_true_condition(sig->condition)) {
fprintf(stream,"#if ");
print_condition(stream,sig->condition);
print_nl(stream);
}
fprintf(stream," LISPFUN_F%s",get_signature_for_LISPFUN(fdef,sig));
print_nl(stream);
if (!is_true_condition(sig->condition)) {
fprintf(stream,"#endif");
print_nl(stream);
}
}
}
}
}
fprintf(stream," 0");
print_nl(stream);
fprintf(stream,"};");
print_nl(stream);
fprintf(stream,"uintC module__%s__subr_tab_size = ((char*)&%s._dummy_to_avoid_trailing_comma_in_initializer-(char*)&%s)/sizeof(subr_);",modname,subr_tab,subr_tab);
print_nl(stream);
print_nl(stream);
fprintf(stream,"struct {");
print_nl(stream);
{
var uintL n = VectorFundef_length(fundefs);
var uintL i;
for (i = 0; i < n; i++) {
var Fundef* fdef = VectorFundef_element(fundefs,i);
if (!is_false_condition(fdef->condition)) {
if (!is_true_condition(fdef->condition)) {
fprintf(stream,"#if ");
print_condition(stream,fdef->condition);
print_nl(stream);
}
fprintf(stream," subr_initdata _%s;",fdef->tag);
print_nl(stream);
if (!is_true_condition(fdef->condition)) {
fprintf(stream,"#endif");
print_nl(stream);
}
}
}
}
fprintf(stream," int _dummy_to_avoid_trailing_comma_in_initializer;");
print_nl(stream);
fprintf(stream,"} %s = {",subr_tab_initdata);
print_nl(stream);
{
var uintL n = VectorFundef_length(fundefs);
var uintL i;
for (i = 0; i < n; i++) {
var Fundef* fdef = VectorFundef_element(fundefs,i);
if (!is_false_condition(fdef->condition)) {
if (!is_true_condition(fdef->condition)) {
fprintf(stream,"#if ");
print_condition(stream,fdef->condition);
print_nl(stream);
}
fprintf(stream," { ");
print_C_string(stream,fdef->packname);
fprintf(stream,", ");
print_C_string(stream,fdef->printname);
fprintf(stream," },");
print_nl(stream);
if (!is_true_condition(fdef->condition)) {
fprintf(stream,"#endif");
print_nl(stream);
}
}
}
}
fprintf(stream," 0");
print_nl(stream);
fprintf(stream,"};");
print_nl(stream);
print_nl(stream);
fprintf(stream,"void module__%s__init_function_1 (module_* module)",modname);
print_nl(stream);
fprintf(stream,"{");
print_nl(stream);
{
var uintL n = VectorFundef_length(fundefs);
var uintL i;
for (i = 0; i < n; i++) {
var Fundef* fdef = VectorFundef_element(fundefs,i);
var VectorSignature* signatures = fdef->signatures;
var uintL m = VectorSignature_length(signatures);
var uintL j;
for (j = 0; j < m; j++) {
var Signature* sig = VectorSignature_element(signatures,j);
if (sig->key) {
if (!is_false_condition(sig->condition)) {
if (!is_true_condition(sig->condition)) {
fprintf(stream,"#if ");
print_condition(stream,sig->condition);
print_nl(stream);
}
{
var uintL o = VectorObjdef_length(sig->keywords);
var uintL k;
for (k = 0; k < o; k++) {
fprintf(stream," pushSTACK(%s);",VectorObjdef_element(sig->keywords,k)->tag);
print_nl(stream);
}
fprintf(stream," %s._%s.keywords = vectorof(%lu);",subr_tab,fdef->tag,o);
print_nl(stream);
}
if (!is_true_condition(sig->condition)) {
fprintf(stream,"#endif");
print_nl(stream);
}
}
}
}
}
}
{
var uintL n = VectorVarinit_length(varinits);
var uintL i;
for (i = 0; i < n; i++) {
var Varinit* vinit = VectorVarinit_element(varinits,i);
if (!is_true_condition_part(vinit->condition)) {
fprintf(stream,"#if ");
print_condition_part(stream,vinit->condition);
print_nl(stream);
}
fprintf(stream," O(%s) = (%s);",vinit->tag,vinit->initform);
print_nl(stream);
if (!is_true_condition_part(vinit->condition)) {
fprintf(stream,"#endif");
print_nl(stream);
}
}
}
fprintf(stream,"}");
print_nl(stream);
print_nl(stream);
fprintf(stream,"void module__%s__init_function_2 (module_* module)",modname);
print_nl(stream);
fprintf(stream,"{");
print_nl(stream);
fprintf(stream,"}");
print_nl(stream);
}
# Output everything.
local void output (FILE* stream, const char* infilename)
{
if (infilename != NULL)
fprintf(stdout,"#line 1 \"%s\"\n",infilename);
lineno = 1;
{
var uintL m = VectorLine_length(lines);
var uintL j;
for (j = 0; j < m; j++) {
var const Line* line = VectorLine_element(lines,j);
if (line->number != lineno) {
fprintf(stream,"#line %ld\n",line->number);
lineno = line->number;
}
fprintf(stream,"%s",line->contents);
print_nl(stream);
if (j == line_defmodule)
output_tables1(stream);
}
}
output_tables2(stream);
}
# =============================== MAIN PROGRAM ==============================
int main (int argc, char* argv[])
{
var char* infilename;
var FILE* infile;
var FILE* outfile;
# Argument parsing.
if (argc == 2) {
infilename = argv[1];
infile = fopen(infilename,"r");
if (infile == NULL)
exit(1);
file = infilename;
} else if (argc == 1) {
infilename = NULL;
infile = stdin;
} else
exit(1);
outfile = stdout;
# Main job.
read_all_input(infile);
parse();
output(stdout,infilename);
# Clean up.
if (ferror(infile) || ferror(outfile)) {
fclose(infile);
fclose(outfile);
exit(1);
}
fclose(infile);
fclose(outfile);
exit(0);
}
|