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 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511
|
/*
* Copyright (c) 2001 by The XFree86 Project, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Except as contained in this notice, the name of the XFree86 Project shall
* not be used in advertising or otherwise to promote the sale, use or other
* dealings in this Software without prior written authorization from the
* XFree86 Project.
*
* Author: Paulo César Pereira de Andrade
*/
/* $XFree86: xc/programs/xedit/lisp/lisp.c,v 1.87tsi Exp $ */
#include <stdlib.h>
#include <string.h>
#ifdef sun
#include <strings.h>
#endif
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <signal.h>
#include <sys/wait.h>
#ifndef X_NOT_POSIX
#include <unistd.h> /* for sysconf(), and getpagesize() */
#endif
#if defined(linux)
#define HAS_GETPAGESIZE
#define HAS_SC_PAGESIZE /* _SC_PAGESIZE may be an enum for Linux */
#endif
#if defined(CSRG_BASED)
#define HAS_GETPAGESIZE
#endif
#if defined(sun)
#define HAS_GETPAGESIZE
#endif
#if defined(QNX4)
#define HAS_GETPAGESIZE
#endif
#if defined(__QNXNTO__)
#define HAS_SC_PAGESIZE
#endif
#include "lisp/bytecode.h"
#include "lisp/read.h"
#include "lisp/format.h"
#include "lisp/math.h"
#include "lisp/hash.h"
#include "lisp/package.h"
#include "lisp/pathname.h"
#include "lisp/regex.h"
#include "lisp/require.h"
#include "lisp/stream.h"
#include "lisp/struct.h"
#include "lisp/time.h"
#include "lisp/write.h"
#include <math.h>
typedef struct {
LispObj **objects;
LispObj *freeobj;
int nsegs;
int nobjs;
int nfree;
} LispObjSeg;
/*
* Prototypes
*/
static void Lisp__GC(LispObj*, LispObj*);
static LispObj *Lisp__New(LispObj*, LispObj*);
/* run a user function, to be called only by LispEval */
static LispObj *LispRunFunMac(LispObj*, LispObj*, int, int);
/* expands and executes a setf method, to be called only by Lisp_Setf */
LispObj *LispRunSetf(LispArgList*, LispObj*, LispObj*, LispObj*);
LispObj *LispRunSetfMacro(LispAtom*, LispObj*, LispObj*);
/* increases storage size for environment */
void LispMoreEnvironment(void);
/* increases storage size for stack of builtin arguments */
void LispMoreStack(void);
/* increases storage size for global variables */
void LispMoreGlobals(LispPackage*);
#ifdef __GNUC__
static INLINE LispObj *LispDoGetVar(LispObj*);
#endif
static INLINE void LispDoAddVar(LispObj*, LispObj*);
/* Helper for importing symbol(s) functions,
* Search for the specified object in the current package */
static INLINE LispObj *LispGetVarPack(LispObj*);
/* create environment for function call */
static int LispMakeEnvironment(LispArgList*, LispObj*, LispObj*, int, int);
/* if not already in keyword package, move atom to keyword package */
static LispObj *LispCheckKeyword(LispObj*);
/* builtin backquote parsing */
static LispObj *LispEvalBackquoteObject(LispObj*, int, int);
/* used also by the bytecode compiler */
LispObj *LispEvalBackquote(LispObj*, int);
/* create or change object property */
void LispSetAtomObjectProperty(LispAtom*, LispObj*);
/* remove object property */
static void LispRemAtomObjectProperty(LispAtom*);
/* allocates a new LispProperty for the given atom */
static void LispAllocAtomProperty(LispAtom*);
/* Increment reference count of atom property */
static void LispIncrementAtomReference(LispAtom*);
/* Decrement reference count of atom property */
static void LispDecrementAtomReference(LispAtom*);
/* Removes all atom properties */
static void LispRemAtomAllProperties(LispAtom*);
static LispObj *LispAtomPropertyFunction(LispAtom*, LispObj*, int);
static INLINE void LispCheckMemLevel(void);
void LispAllocSeg(LispObjSeg*, int);
static INLINE void LispMark(LispObj*);
/* functions, macros, setf methods, and structure definitions */
static INLINE void LispProt(LispObj*);
static LispObj *LispCheckNeedProtect(LispObj*);
static
#ifdef SIGNALRETURNSINT
int
#else
void
#endif
LispSignalHandler(int);
/*
* Initialization
*/
LispMac lisp__data;
static LispObj lispunbound = {LispNil_t};
LispObj *UNBOUND = &lispunbound;
static volatile int lisp__disable_int;
static volatile int lisp__interrupted;
LispObj *Okey, *Orest, *Ooptional, *Oaux, *Olambda;
Atom_id Snil, St;
Atom_id Saux, Skey, Soptional, Srest;
Atom_id Satom, Ssymbol, Sinteger, Scharacter, Sstring, Slist,
Scons, Svector, Sarray, Sstruct, Skeyword, Sfunction, Spathname,
Srational, Sfloat, Scomplex, Sopaque, Sdefault;
LispObj *Oformat, *Kunspecific;
LispObj *Oexpand_setf_method;
static LispProperty noproperty;
LispProperty *NOPROPERTY = &noproperty;
static int segsize, minfree;
int pagesize, gcpro;
static LispObjSeg objseg = {NULL, NIL};
static LispObjSeg atomseg = {NULL, NIL};
int LispArgList_t;
LispFile *Stdout, *Stdin, *Stderr;
static LispBuiltin lispbuiltins[] = {
{LispFunction, Lisp_Mul, "* &rest numbers"},
{LispFunction, Lisp_Plus, "+ &rest numbers"},
{LispFunction, Lisp_Minus, "- number &rest more-numbers"},
{LispFunction, Lisp_Div, "/ number &rest more-numbers"},
{LispFunction, Lisp_OnePlus, "1+ number"},
{LispFunction, Lisp_OneMinus, "1- number"},
{LispFunction, Lisp_Less, "< number &rest more-numbers"},
{LispFunction, Lisp_LessEqual, "<= number &rest more-numbers"},
{LispFunction, Lisp_Equal_, "= number &rest more-numbers"},
{LispFunction, Lisp_Greater, "> number &rest more-numbers"},
{LispFunction, Lisp_GreaterEqual, ">= number &rest more-numbers"},
{LispFunction, Lisp_NotEqual, "/= number &rest more-numbers"},
{LispFunction, Lisp_Max, "max number &rest more-numbers"},
{LispFunction, Lisp_Min, "min number &rest more-numbers"},
{LispFunction, Lisp_Abs, "abs number"},
{LispFunction, Lisp_Acons, "acons key datum alist"},
{LispFunction, Lisp_Adjoin, "adjoin item list &key key test test-not"},
{LispFunction, Lisp_AlphaCharP, "alpha-char-p char"},
{LispMacro, Lisp_And, "and &rest args", 1, 0, Com_And},
{LispFunction, Lisp_Append, "append &rest lists"},
{LispFunction, Lisp_Apply, "apply function arg &rest more-args", 1},
{LispFunction, Lisp_Aref, "aref array &rest subscripts"},
{LispFunction, Lisp_Assoc, "assoc item list &key test test-not key"},
{LispFunction, Lisp_AssocIf, "assoc-if predicate list &key key"},
{LispFunction, Lisp_AssocIfNot, "assoc-if-not predicate list &key key"},
{LispFunction, Lisp_Atom, "atom object"},
{LispMacro, Lisp_Block, "block name &rest body", 1, 0, Com_Block},
{LispFunction, Lisp_BothCaseP, "both-case-p character"},
{LispFunction, Lisp_Boundp, "boundp symbol"},
{LispFunction, Lisp_Butlast, "butlast list &optional count"},
{LispFunction, Lisp_Nbutlast, "nbutlast list &optional count"},
{LispFunction, Lisp_Car, "car list", 0, 0, Com_C_r},
{LispFunction, Lisp_Car, "first list", 0, 0, Com_C_r},
{LispMacro, Lisp_Case, "case keyform &rest body"},
{LispMacro, Lisp_Catch, "catch tag &rest body", 1},
{LispFunction, Lisp_Cdr, "cdr list", 0, 0, Com_C_r},
{LispFunction, Lisp_Cdr, "rest list", 0, 0, Com_C_r},
{LispFunction, Lisp_Ceiling, "ceiling number &optional divisor", 1},
{LispFunction, Lisp_Fceiling, "fceiling number &optional divisor", 1},
{LispFunction, Lisp_Char, "char string index"},
{LispFunction, Lisp_Char, "schar simple-string index"},
{LispFunction, Lisp_CharLess, "char< character &rest more-characters"},
{LispFunction, Lisp_CharLessEqual, "char<= character &rest more-characters"},
{LispFunction, Lisp_CharEqual_, "char= character &rest more-characters"},
{LispFunction, Lisp_CharGreater, "char> character &rest more-characters"},
{LispFunction, Lisp_CharGreaterEqual, "char>= character &rest more-characters"},
{LispFunction, Lisp_CharNotEqual_, "char/= character &rest more-characters"},
{LispFunction, Lisp_CharLessp, "char-lessp character &rest more-characters"},
{LispFunction, Lisp_CharNotGreaterp, "char-not-greaterp character &rest more-characters"},
{LispFunction, Lisp_CharEqual, "char-equal character &rest more-characters"},
{LispFunction, Lisp_CharGreaterp, "char-greaterp character &rest more-characters"},
{LispFunction, Lisp_CharNotLessp, "char-not-lessp character &rest more-characters"},
{LispFunction, Lisp_CharNotEqual, "char-not-equal character &rest more-characters"},
{LispFunction, Lisp_CharDowncase, "char-downcase character"},
{LispFunction, Lisp_CharInt, "char-code character"},
{LispFunction, Lisp_CharInt, "char-int character"},
{LispFunction, Lisp_CharUpcase, "char-upcase character"},
{LispFunction, Lisp_Character, "character object"},
{LispFunction, Lisp_Characterp, "characterp object"},
{LispFunction, Lisp_Clrhash, "clrhash hash-table"},
{LispFunction, Lisp_IntChar, "code-char integer"},
{LispFunction, Lisp_Coerce, "coerce object result-type"},
{LispFunction, Lisp_Compile, "compile name &optional definition", 1},
{LispFunction, Lisp_Complex, "complex realpart &optional imagpart"},
{LispMacro, Lisp_Cond, "cond &rest body", 0, 0, Com_Cond},
{LispFunction, Lisp_Cons, "cons car cdr", 0, 0, Com_Cons},
{LispFunction, Lisp_Consp, "consp object", 0, 0, Com_Consp},
{LispFunction, Lisp_Constantp, "constantp form &optional environment"},
{LispFunction, Lisp_Conjugate, "conjugate number"},
{LispFunction, Lisp_Complexp, "complexp object"},
{LispFunction, Lisp_CopyAlist, "copy-alist list"},
{LispFunction, Lisp_CopyList, "copy-list list"},
{LispFunction, Lisp_CopyTree, "copy-tree list"},
{LispFunction, Lisp_Close, "close stream &key abort"},
{LispFunction, Lisp_C_r, "caar list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "cadr list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "cdar list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "cddr list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "caaar list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "caadr list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "cadar list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "caddr list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "cdaar list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "cdadr list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "cddar list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "cdddr list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "caaaar list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "caaadr list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "caadar list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "caaddr list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "cadaar list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "cadadr list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "caddar list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "cadddr list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "cdaaar list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "cdaadr list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "cdadar list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "cdaddr list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "cddaar list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "cddadr list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "cdddar list", 0, 0, Com_C_r},
{LispFunction, Lisp_C_r, "cddddr list", 0, 0, Com_C_r},
{LispMacro, Lisp_Decf, "decf place &optional delta"},
{LispMacro, Lisp_Defconstant, "defconstant name initial-value &optional documentation"},
{LispMacro, Lisp_Defmacro, "defmacro name lambda-list &rest body"},
{LispMacro, Lisp_Defstruct, "defstruct name &rest description"},
{LispMacro, Lisp_Defun, "defun name lambda-list &rest body"},
{LispMacro, Lisp_Defsetf, "defsetf function lambda-list &rest body"},
{LispMacro, Lisp_Defparameter, "defparameter name initial-value &optional documentation"},
{LispMacro, Lisp_Defvar, "defvar name &optional initial-value documentation"},
{LispFunction, Lisp_Delete, "delete item sequence &key from-end test test-not start end count key"},
{LispFunction, Lisp_DeleteDuplicates, "delete-duplicates sequence &key from-end test test-not start end key"},
{LispFunction, Lisp_DeleteIf, "delete-if predicate sequence &key from-end start end count key"},
{LispFunction, Lisp_DeleteIfNot, "delete-if-not predicate sequence &key from-end start end count key"},
{LispFunction, Lisp_DeleteFile, "delete-file filename"},
{LispFunction, Lisp_Denominator, "denominator rational"},
{LispFunction, Lisp_DigitChar, "digit-char weight &optional radix"},
{LispFunction, Lisp_DigitCharP, "digit-char-p character &optional radix"},
{LispFunction, Lisp_Directory, "directory pathname &key all if-cannot-read"},
{LispFunction, Lisp_DirectoryNamestring, "directory-namestring pathname"},
{LispFunction, Lisp_Disassemble, "disassemble function"},
{LispMacro, Lisp_Do, "do init test &rest body"},
{LispMacro, Lisp_DoP, "do* init test &rest body"},
{LispFunction, Lisp_Documentation, "documentation symbol type"},
{LispMacro, Lisp_DoList, "dolist init &rest body", 0, 0, Com_Dolist},
{LispMacro, Lisp_DoTimes, "dotimes init &rest body"},
{LispMacro, Lisp_DoAllSymbols, "do-all-symbols init &rest body"},
{LispMacro, Lisp_DoExternalSymbols, "do-external-symbols init &rest body"},
{LispMacro, Lisp_DoSymbols, "do-symbols init &rest body"},
{LispFunction, Lisp_Elt, "elt sequence index"},
{LispFunction, Lisp_Endp, "endp object"},
{LispFunction, Lisp_EnoughNamestring, "enough-namestring pathname &optional defaults"},
{LispFunction, Lisp_Eq, "eq left right", 0, 0, Com_Eq},
{LispFunction, Lisp_Eql, "eql left right", 0, 0, Com_Eq},
{LispFunction, Lisp_Equal, "equal left right", 0, 0, Com_Eq},
{LispFunction, Lisp_Equalp, "equalp left right", 0, 0, Com_Eq},
{LispFunction, Lisp_Error, "error control-string &rest arguments"},
{LispFunction, Lisp_Evenp, "evenp integer"},
{LispFunction, Lisp_Export, "export symbols &optional package"},
{LispFunction, Lisp_Eval, "eval form"},
{LispFunction, Lisp_Every, "every predicate sequence &rest more-sequences"},
{LispFunction, Lisp_Some, "some predicate sequence &rest more-sequences"},
{LispFunction, Lisp_Notevery, "notevery predicate sequence &rest more-sequences"},
{LispFunction, Lisp_Notany, "notany predicate sequence &rest more-sequences"},
{LispFunction, Lisp_Fboundp, "fboundp symbol"},
{LispFunction, Lisp_Find, "find item sequence &key from-end test test-not start end key"},
{LispFunction, Lisp_FindIf, "find-if predicate sequence &key from-end start end key"},
{LispFunction, Lisp_FindIfNot, "find-if-not predicate sequence &key from-end start end key"},
{LispFunction, Lisp_FileNamestring, "file-namestring pathname"},
{LispFunction, Lisp_Fill, "fill sequence item &key start end"},
{LispFunction, Lisp_FindAllSymbols, "find-all-symbols string-or-symbol"},
{LispFunction, Lisp_FindSymbol, "find-symbol string &optional package", 1},
{LispFunction, Lisp_FindPackage, "find-package name"},
{LispFunction, Lisp_Float, "float number &optional other"},
{LispFunction, Lisp_Floatp, "floatp object"},
{LispFunction, Lisp_Floor, "floor number &optional divisor", 1},
{LispFunction, Lisp_Ffloor, "ffloor number &optional divisor", 1},
{LispFunction, Lisp_Fmakunbound, "fmakunbound symbol"},
{LispFunction, Lisp_Format, "format destination control-string &rest arguments"},
{LispFunction, Lisp_FreshLine, "fresh-line &optional output-stream"},
{LispFunction, Lisp_Funcall, "funcall function &rest arguments", 1},
{LispFunction, Lisp_Functionp, "functionp object"},
{LispFunction, Lisp_Gc, "gc &optional car cdr"},
{LispFunction, Lisp_Gcd, "gcd &rest integers"},
{LispFunction, Lisp_Gensym, "gensym &optional arg"},
{LispFunction, Lisp_Get, "get symbol indicator &optional default"},
{LispFunction, Lisp_Gethash, "gethash key hash-table &optional default", 1},
{LispMacro, Lisp_Go, "go tag", 0, 0, Com_Go},
{LispFunction, Lisp_GraphicCharP, "graphic-char-p char"},
{LispFunction, Lisp_HashTableP, "hash-table-p object"},
{LispFunction, Lisp_HashTableCount, "hash-table-count hash-table"},
{LispFunction, Lisp_HashTableRehashSize, "hash-table-rehash-size hash-table"},
{LispFunction, Lisp_HashTableRehashThreshold, "hash-table-rehash-threshold hash-table"},
{LispFunction, Lisp_HashTableSize, "hash-table-size hash-table"},
{LispFunction, Lisp_HashTableTest, "hash-table-test hash-table"},
{LispFunction, Lisp_HostNamestring, "host-namestring pathname"},
{LispMacro, Lisp_If, "if test then &optional else", 0, 0, Com_If},
{LispMacro, Lisp_IgnoreErrors, "ignore-errors &rest body", 1},
{LispFunction, Lisp_Imagpart, "imagpart number"},
{LispMacro, Lisp_InPackage, "in-package name"},
{LispMacro, Lisp_Incf, "incf place &optional delta"},
{LispFunction, Lisp_Import, "import symbols &optional package"},
{LispFunction, Lisp_InputStreamP, "input-stream-p stream"},
{LispFunction, Lisp_IntChar, "int-char integer"},
{LispFunction, Lisp_Integerp, "integerp object"},
{LispFunction, Lisp_Intern, "intern string &optional package", 1},
{LispFunction, Lisp_Intersection, "intersection list1 list2 &key test test-not key"},
{LispFunction, Lisp_Nintersection, "nintersection list1 list2 &key test test-not key"},
{LispFunction, Lisp_Isqrt, "isqrt natural"},
{LispFunction, Lisp_Keywordp, "keywordp object"},
{LispFunction, Lisp_Last, "last list &optional count", 0, 0, Com_Last},
{LispMacro, Lisp_Lambda, "lambda lambda-list &rest body"},
{LispFunction, Lisp_Lcm, "lcm &rest integers"},
{LispFunction, Lisp_Length, "length sequence", 0, 0, Com_Length},
{LispMacro, Lisp_Let, "let init &rest body", 1, 0, Com_Let},
{LispMacro, Lisp_LetP, "let* init &rest body", 1, 0, Com_Letx},
{LispFunction, Lisp_ListP, "list* object &rest more-objects"},
{LispFunction, Lisp_ListAllPackages, "list-all-packages"},
{LispFunction, Lisp_List, "list &rest args"},
{LispFunction, Lisp_ListLength, "list-length list"},
{LispFunction, Lisp_Listp, "listp object", 0, 0, Com_Listp},
{LispFunction, Lisp_Listen, "listen &optional input-stream"},
{LispFunction, Lisp_Load, "load filename &key verbose print if-does-not-exist"},
{LispFunction, Lisp_Logand, "logand &rest integers"},
{LispFunction, Lisp_Logeqv, "logeqv &rest integers"},
{LispFunction, Lisp_Logior, "logior &rest integers"},
{LispFunction, Lisp_Lognot, "lognot integer"},
{LispFunction, Lisp_Logxor, "logxor &rest integers"},
{LispMacro, Lisp_Loop, "loop &rest body", 0, 0, Com_Loop},
{LispFunction, Lisp_LowerCaseP, "lower-case-p character"},
{LispFunction, Lisp_MakeArray, "make-array dimensions &key element-type initial-element initial-contents adjustable fill-pointer displaced-to displaced-index-offset"},
{LispFunction, Lisp_MakeHashTable, "make-hash-table &key test size rehash-size rehash-threshold initial-contents"},
{LispFunction, Lisp_MakeList, "make-list size &key initial-element"},
{LispFunction, Lisp_MakePackage, "make-package package-name &key nicknames use"},
{LispFunction, Lisp_MakePathname, "make-pathname &key host device directory name type version defaults"},
{LispFunction, Lisp_MakeString, "make-string size &key initial-element element-type"},
{LispFunction, Lisp_MakeSymbol, "make-symbol name"},
{LispFunction, Lisp_MakeStringInputStream, "make-string-input-stream string &optional start end"},
{LispFunction, Lisp_MakeStringOutputStream, "make-string-output-stream &key element-type"},
{LispFunction, Lisp_GetOutputStreamString, "get-output-stream-string string-output-stream"},
{LispFunction, Lisp_Makunbound, "makunbound symbol"},
{LispFunction, Lisp_Mapc, "mapc function list &rest more-lists"},
{LispFunction, Lisp_Mapcar, "mapcar function list &rest more-lists"},
{LispFunction, Lisp_Mapcan, "mapcan function list &rest more-lists"},
{LispFunction, Lisp_Maphash, "maphash function hash-table"},
{LispFunction, Lisp_Mapl, "mapl function list &rest more-lists"},
{LispFunction, Lisp_Maplist, "maplist function list &rest more-lists"},
{LispFunction, Lisp_Mapcon, "mapcon function list &rest more-lists"},
{LispFunction, Lisp_Member, "member item list &key test test-not key"},
{LispFunction, Lisp_MemberIf, "member-if predicate list &key key"},
{LispFunction, Lisp_MemberIfNot, "member-if-not predicate list &key key"},
{LispFunction, Lisp_Minusp, "minusp number"},
{LispFunction, Lisp_Mod, "mod number divisor"},
{LispMacro, Lisp_MultipleValueBind, "multiple-value-bind symbols values &rest body"},
{LispMacro, Lisp_MultipleValueCall, "multiple-value-call function &rest form", 1},
{LispMacro, Lisp_MultipleValueProg1, "multiple-value-prog1 first-form &rest form", 1},
{LispMacro, Lisp_MultipleValueList, "multiple-value-list form"},
{LispMacro, Lisp_MultipleValueSetq, "multiple-value-setq symbols form"},
{LispFunction, Lisp_Nconc, "nconc &rest lists"},
{LispFunction, Lisp_Nreverse, "nreverse sequence"},
{LispFunction, Lisp_NsetDifference, "nset-difference list1 list2 &key test test-not key"},
{LispFunction, Lisp_Nsubstitute, "nsubstitute newitem olditem sequence &key from-end test test-not start end count key"},
{LispFunction, Lisp_NsubstituteIf, "nsubstitute-if newitem test sequence &key from-end start end count key"},
{LispFunction, Lisp_NsubstituteIfNot, "nsubstitute-if-not newitem test sequence &key from-end start end count key"},
{LispFunction, Lisp_Nth, "nth index list"},
{LispFunction, Lisp_Nthcdr, "nthcdr index list", 0, 0, Com_Nthcdr},
{LispMacro, Lisp_NthValue, "nth-value index form"},
{LispFunction, Lisp_Numerator, "numerator rational"},
{LispFunction, Lisp_Namestring, "namestring pathname"},
{LispFunction, Lisp_Null, "not arg", 0, 0, Com_Null},
{LispFunction, Lisp_Null, "null list", 0, 0, Com_Null},
{LispFunction, Lisp_Numberp, "numberp object", 0, 0, Com_Numberp},
{LispFunction, Lisp_Oddp, "oddp integer"},
{LispFunction, Lisp_Open, "open filename &key direction element-type if-exists if-does-not-exist external-format"},
{LispFunction, Lisp_OpenStreamP, "open-stream-p stream"},
{LispMacro, Lisp_Or, "or &rest args", 1, 0, Com_Or},
{LispFunction, Lisp_OutputStreamP, "output-stream-p stream"},
{LispFunction, Lisp_Packagep, "packagep object"},
{LispFunction, Lisp_PackageName, "package-name package"},
{LispFunction, Lisp_PackageNicknames, "package-nicknames package"},
{LispFunction, Lisp_PackageUseList, "package-use-list package"},
{LispFunction, Lisp_PackageUsedByList, "package-used-by-list package"},
{LispFunction, Lisp_Pairlis, "pairlis key data &optional alist"},
{LispFunction, Lisp_ParseInteger, "parse-integer string &key start end radix junk-allowed", 1},
{LispFunction, Lisp_ParseNamestring, "parse-namestring object &optional host defaults &key start end junk-allowed", 1},
{LispFunction, Lisp_PathnameHost, "pathname-host pathname"},
{LispFunction, Lisp_PathnameDevice, "pathname-device pathname"},
{LispFunction, Lisp_PathnameDirectory, "pathname-directory pathname"},
{LispFunction, Lisp_PathnameName, "pathname-name pathname"},
{LispFunction, Lisp_PathnameType, "pathname-type pathname"},
{LispFunction, Lisp_PathnameVersion, "pathname-version pathname"},
{LispFunction, Lisp_Pathnamep, "pathnamep object"},
{LispFunction, Lisp_Plusp, "plusp number"},
{LispMacro, Lisp_Pop, "pop place"},
{LispFunction, Lisp_Position, "position item sequence &key from-end test test-not start end key"},
{LispFunction, Lisp_PositionIf, "position-if predicate sequence &key from-end start end key"},
{LispFunction, Lisp_PositionIfNot, "position-if-not predicate sequence &key from-end start end key"},
{LispFunction, Lisp_Prin1, "prin1 object &optional output-stream"},
{LispFunction, Lisp_Princ, "princ object &optional output-stream"},
{LispFunction, Lisp_Print, "print object &optional output-stream"},
{LispFunction, Lisp_ProbeFile, "probe-file pathname"},
{LispFunction, Lisp_Proclaim, "proclaim declaration"},
{LispMacro, Lisp_Prog1, "prog1 first &rest body"},
{LispMacro, Lisp_Prog2, "prog2 first second &rest body"},
{LispMacro, Lisp_Progn, "progn &rest body", 1, 0, Com_Progn},
{LispMacro, Lisp_Progv, "progv symbols values &rest body", 1},
{LispFunction, Lisp_Provide, "provide module"},
{LispMacro, Lisp_Push, "push item place"},
{LispMacro, Lisp_Pushnew, "pushnew item place &key key test test-not"},
{LispFunction, Lisp_Quit, "quit &optional status"},
{LispMacro, Lisp_Quote, "quote object"},
{LispFunction, Lisp_Rational, "rational number"},
{LispFunction, Lisp_Rationalp, "rationalp object"},
{LispFunction, Lisp_Read, "read &optional input-stream eof-error-p eof-value recursive-p"},
{LispFunction, Lisp_ReadChar, "read-char &optional input-stream eof-error-p eof-value recursive-p"},
{LispFunction, Lisp_ReadCharNoHang, "read-char-no-hang &optional input-stream eof-error-p eof-value recursive-p"},
{LispFunction, Lisp_ReadLine, "read-line &optional input-stream eof-error-p eof-value recursive-p", 1},
{LispFunction, Lisp_Realpart, "realpart number"},
{LispFunction, Lisp_Replace, "replace sequence1 sequence2 &key start1 end1 start2 end2"},
{LispFunction, Lisp_ReadFromString, "read-from-string string &optional eof-error-p eof-value &key start end preserve-whitespace", 1},
{LispFunction, Lisp_Require, "require module &optional pathname"},
{LispFunction, Lisp_Rem, "rem number divisor"},
{LispFunction, Lisp_Remhash, "remhash key hash-table"},
{LispFunction, Lisp_Remove, "remove item sequence &key from-end test test-not start end count key"},
{LispFunction, Lisp_RemoveDuplicates, "remove-duplicates sequence &key from-end test test-not start end key"},
{LispFunction, Lisp_RemoveIf, "remove-if predicate sequence &key from-end start end count key"},
{LispFunction, Lisp_RemoveIfNot, "remove-if-not predicate sequence &key from-end start end count key"},
{LispFunction, Lisp_Remprop, "remprop symbol indicator"},
{LispFunction, Lisp_RenameFile, "rename-file filename new-name", 1},
{LispMacro, Lisp_Return, "return &optional result", 1, 0, Com_Return},
{LispMacro, Lisp_ReturnFrom, "return-from name &optional result", 1, 0, Com_ReturnFrom},
{LispFunction, Lisp_Reverse, "reverse sequence"},
{LispFunction, Lisp_Round, "round number &optional divisor", 1},
{LispFunction, Lisp_Fround, "fround number &optional divisor", 1},
{LispFunction, Lisp_Rplaca, "rplaca place value", 0, 0, Com_Rplac_},
{LispFunction, Lisp_Rplacd, "rplacd place value", 0, 0, Com_Rplac_},
{LispFunction, Lisp_Search, "search sequence1 sequence2 &key from-end test test-not key start1 start2 end1 end2"},
{LispFunction, Lisp_Set, "set symbol value"},
{LispFunction, Lisp_SetDifference, "set-difference list1 list2 &key test test-not key"},
{LispFunction, Lisp_SetExclusiveOr, "set-exclusive-or list1 list2 &key test test-not key"},
{LispFunction, Lisp_NsetExclusiveOr, "nset-exclusive-or list1 list2 &key test test-not key"},
{LispMacro, Lisp_Setf, "setf &rest form"},
{LispMacro, Lisp_Psetf, "psetf &rest form"},
{LispMacro, Lisp_SetQ, "setq &rest form", 0, 0, Com_Setq},
{LispMacro, Lisp_Psetq, "psetq &rest form"},
{LispFunction, Lisp_Sleep, "sleep seconds"},
{LispFunction, Lisp_Sort, "sort sequence predicate &key key"},
{LispFunction, Lisp_Sqrt, "sqrt number"},
{LispFunction, Lisp_Elt, "svref sequence index"},
{LispFunction, Lisp_Sort, "stable-sort sequence predicate &key key"},
{LispFunction, Lisp_Streamp, "streamp object"},
{LispFunction, Lisp_String, "string object"},
{LispFunction, Lisp_Stringp, "stringp object"},
{LispFunction, Lisp_StringEqual_, "string= string1 string2 &key start1 end1 start2 end2"},
{LispFunction, Lisp_StringLess, "string< string1 string2 &key start1 end1 start2 end2"},
{LispFunction, Lisp_StringGreater, "string> string1 string2 &key start1 end1 start2 end2"},
{LispFunction, Lisp_StringLessEqual, "string<= string1 string2 &key start1 end1 start2 end2"},
{LispFunction, Lisp_StringGreaterEqual, "string>= string1 string2 &key start1 end1 start2 end2"},
{LispFunction, Lisp_StringNotEqual_, "string/= string1 string2 &key start1 end1 start2 end2"},
{LispFunction, Lisp_StringConcat, "string-concat &rest strings"},
{LispFunction, Lisp_StringEqual, "string-equal string1 string2 &key start1 end1 start2 end2"},
{LispFunction, Lisp_StringGreaterp, "string-greaterp string1 string2 &key start1 end1 start2 end2"},
{LispFunction, Lisp_StringNotEqual, "string-not-equal string1 string2 &key start1 end1 start2 end2"},
{LispFunction, Lisp_StringNotGreaterp, "string-not-greaterp string1 string2 &key start1 end1 start2 end2"},
{LispFunction, Lisp_StringNotLessp, "string-not-lessp string1 string2 &key start1 end1 start2 end2"},
{LispFunction, Lisp_StringLessp, "string-lessp string1 string2 &key start1 end1 start2 end2"},
{LispFunction, Lisp_StringTrim, "string-trim character-bag string"},
{LispFunction, Lisp_StringLeftTrim, "string-left-trim character-bag string"},
{LispFunction, Lisp_StringRightTrim, "string-right-trim character-bag string"},
{LispFunction, Lisp_StringUpcase, "string-upcase string &key start end"},
{LispFunction, Lisp_NstringUpcase, "nstring-upcase string &key start end"},
{LispFunction, Lisp_StringDowncase, "string-downcase string &key start end"},
{LispFunction, Lisp_NstringDowncase, "nstring-downcase string &key start end"},
{LispFunction, Lisp_StringCapitalize, "string-capitalize string &key start end"},
{LispFunction, Lisp_NstringCapitalize, "nstring-capitalize string &key start end"},
{LispFunction, Lisp_Subseq, "subseq sequence start &optional end"},
{LispFunction, Lisp_Subsetp, "subsetp list1 list2 &key test test-not key"},
{LispFunction, Lisp_Substitute, "substitute newitem olditem sequence &key from-end test test-not start end count key"},
{LispFunction, Lisp_SubstituteIf, "substitute-if newitem test sequence &key from-end start end count key"},
{LispFunction, Lisp_SubstituteIfNot, "substitute-if-not newitem test sequence &key from-end start end count key"},
{LispFunction, Lisp_SymbolFunction, "symbol-function symbol"},
{LispFunction, Lisp_SymbolName, "symbol-name symbol"},
{LispFunction, Lisp_Symbolp, "symbolp object"},
{LispFunction, Lisp_SymbolPlist, "symbol-plist symbol"},
{LispFunction, Lisp_SymbolPackage, "symbol-package symbol"},
{LispFunction, Lisp_SymbolValue, "symbol-value symbol"},
{LispMacro, Lisp_Tagbody, "tagbody &rest body", 0, 0, Com_Tagbody},
{LispFunction, Lisp_Terpri, "terpri &optional output-stream"},
{LispFunction, Lisp_Typep, "typep object type"},
{LispMacro, Lisp_The, "the value-type form"},
{LispMacro, Lisp_Throw, "throw tag result", 1},
{LispMacro, Lisp_Time, "time form"},
{LispFunction, Lisp_Truename, "truename pathname"},
{LispFunction, Lisp_TreeEqual, "tree-equal tree-1 tree-2 &key test test-not"},
{LispFunction, Lisp_Truncate, "truncate number &optional divisor", 1},
{LispFunction, Lisp_Ftruncate, "ftruncate number &optional divisor", 1},
{LispFunction, Lisp_Unexport, "unexport symbols &optional package"},
{LispFunction, Lisp_Union, "union list1 list2 &key test test-not key"},
{LispFunction, Lisp_Nunion, "nunion list1 list2 &key test test-not key"},
{LispMacro, Lisp_Unless, "unless test &rest body", 1, 0, Com_Unless},
{LispFunction, Lisp_UserHomedirPathname, "user-homedir-pathname &optional host"},
{LispMacro, Lisp_UnwindProtect, "unwind-protect protect &rest cleanup"},
{LispFunction, Lisp_UpperCaseP, "upper-case-p character"},
{LispFunction, Lisp_Values, "values &rest objects", 1},
{LispFunction, Lisp_ValuesList, "values-list list", 1},
{LispFunction, Lisp_Vector, "vector &rest objects"},
{LispMacro, Lisp_When, "when test &rest body", 1, 0, Com_When},
{LispFunction, Lisp_Write, " write object &key case circle escape length level lines pretty readably right-margin stream"},
{LispFunction, Lisp_WriteChar, "write-char string &optional output-stream"},
{LispFunction, Lisp_WriteLine, "write-line string &optional output-stream &key start end"},
{LispFunction, Lisp_WriteString, "write-string string &optional output-stream &key start end"},
{LispFunction, Lisp_XeditCharStore, "lisp::char-store string index value", 0, 1},
{LispFunction, Lisp_XeditEltStore, "lisp::elt-store sequence index value", 0, 1},
{LispFunction, Lisp_XeditMakeStruct, "lisp::make-struct atom &rest init", 0, 1},
{LispFunction, Lisp_XeditPut, " lisp::put symbol indicator value", 0, 1},
{LispFunction, Lisp_XeditPuthash, "lisp::puthash key hash-table value", 0, 1},
{LispFunction, Lisp_XeditSetSymbolPlist, "lisp::set-symbol-plist symbol list", 0, 1},
{LispFunction, Lisp_XeditStructAccess, "lisp::struct-access atom struct", 0, 1},
{LispFunction, Lisp_XeditStructType, "lisp::struct-type atom struct", 0, 1},
{LispFunction, Lisp_XeditStructStore, "lisp::struct-store atom struct value", 0, 1},
{LispFunction, Lisp_XeditVectorStore, "lisp::vector-store array &rest values", 0, 1},
{LispFunction, Lisp_XeditDocumentationStore, "lisp::documentation-store symbol type string", 0, 1},
{LispFunction, Lisp_Zerop, "zerop number"},
};
static LispBuiltin extbuiltins[] = {
{LispFunction, Lisp_Getenv, "getenv name"},
{LispFunction, Lisp_MakePipe, "make-pipe command-line &key direction element-type external-format"},
{LispFunction, Lisp_PipeBroken, "pipe-broken pipe-stream"},
{LispFunction, Lisp_PipeErrorStream, "pipe-error-stream pipe-stream"},
{LispFunction, Lisp_PipeInputDescriptor, "pipe-input-descriptor pipe-stream"},
{LispFunction, Lisp_PipeErrorDescriptor, "pipe-error-descriptor pipe-stream"},
{LispFunction, Lisp_Recomp, "re-comp pattern &key nospec icase nosub newline"},
{LispFunction, Lisp_Reexec, "re-exec regex string &key count start end notbol noteol"},
{LispFunction, Lisp_Rep, "re-p object"},
{LispFunction, Lisp_Setenv, "setenv name value &optional overwrite"},
{LispFunction, Lisp_Unsetenv, "unsetenv name"},
{LispFunction, Lisp_NstringTrim, "nstring-trim character-bag string"},
{LispFunction, Lisp_NstringLeftTrim, "nstring-left-trim character-bag string"},
{LispFunction, Lisp_NstringRightTrim, "nstring-right-trim character-bag string"},
{LispMacro, Lisp_Until, "until test &rest body", 0, 0, Com_Until},
{LispMacro, Lisp_While, "while test &rest body", 0, 0, Com_While},
};
/* byte code function argument list for functions that don't change it's
* &REST argument list. */
extern LispObj x_cons[8];
/*
* Implementation
*/
static int
LispGetPageSize(void)
{
static int pagesize = -1;
if (pagesize != -1)
return pagesize;
/* Try each supported method in the preferred order */
#if defined(_SC_PAGESIZE) || defined(HAS_SC_PAGESIZE)
pagesize = sysconf(_SC_PAGESIZE);
#endif
#ifdef _SC_PAGE_SIZE
if (pagesize == -1)
pagesize = sysconf(_SC_PAGE_SIZE);
#endif
#ifdef HAS_GETPAGESIZE
if (pagesize == -1)
pagesize = getpagesize();
#endif
#ifdef PAGE_SIZE
if (pagesize == -1)
pagesize = PAGE_SIZE;
#endif
if (pagesize < sizeof(LispObj) * 16)
pagesize = sizeof(LispObj) * 16; /* need a reasonable sane size */
return pagesize;
}
void
LispDestroy(char *fmt, ...)
{
static char Error[] = "*** ";
if (!lisp__data.destroyed) {
char string[128];
va_list ap;
va_start(ap, fmt);
vsnprintf(string, sizeof(string), fmt, ap);
va_end(ap);
if (!lisp__data.ignore_errors) {
if (Stderr->column)
LispFputc(Stderr, '\n');
LispFputs(Stderr, Error);
LispFputs(Stderr, string);
LispFputc(Stderr, '\n');
LispFflush(Stderr);
}
else
lisp__data.error_condition = STRING(string);
#ifdef DEBUGGER
if (lisp__data.debugging) {
LispDebugger(LispDebugCallWatch, NIL, NIL);
LispDebugger(LispDebugCallFatal, NIL, NIL);
}
#endif
lisp__data.destroyed = 1;
LispBlockUnwind(NULL);
if (lisp__data.errexit)
exit(1);
}
#ifdef DEBUGGER
if (lisp__data.debugging) {
/* when stack variables could be changed, this must be also changed! */
lisp__data.debug_level = -1;
lisp__data.debug = LispDebugUnspec;
}
#endif
while (lisp__data.mem.level) {
--lisp__data.mem.level;
if (lisp__data.mem.mem[lisp__data.mem.level])
free(lisp__data.mem.mem[lisp__data.mem.level]);
}
lisp__data.mem.index = 0;
/* If the package was changed and an error happened */
PACKAGE = lisp__data.savepackage;
lisp__data.pack = lisp__data.savepack;
LispTopLevel();
if (!lisp__data.running) {
static char Fatal[] = "*** Fatal: nowhere to longjmp.\n";
LispFputs(Stderr, Fatal);
LispFflush(Stderr);
abort();
}
siglongjmp(lisp__data.jmp, 1);
}
void
LispContinuable(char *fmt, ...)
{
va_list ap;
char string[128];
static char Error[] = "*** Error: ";
if (Stderr->column)
LispFputc(Stderr, '\n');
LispFputs(Stderr, Error);
va_start(ap, fmt);
vsnprintf(string, sizeof(string), fmt, ap);
va_end(ap);
LispFputs(Stderr, string);
LispFputc(Stderr, '\n');
LispFputs(Stderr, "Type 'continue' if you want to proceed: ");
LispFflush(Stderr);
/* NOTE: does not check if stdin is a tty */
if (LispFgets(Stdin, string, sizeof(string)) &&
strcmp(string, "continue\n") == 0)
return;
LispDestroy("aborted on continuable error");
}
void
LispMessage(char *fmt, ...)
{
va_list ap;
char string[128];
if (Stderr->column)
LispFputc(Stderr, '\n');
va_start(ap, fmt);
vsnprintf(string, sizeof(string), fmt, ap);
va_end(ap);
LispFputs(Stderr, string);
LispFputc(Stderr, '\n');
LispFflush(Stderr);
}
void
LispWarning(char *fmt, ...)
{
va_list ap;
char string[128];
static char Warning[] = "*** Warning: ";
if (Stderr->column)
LispFputc(Stderr, '\n');
LispFputs(Stderr, Warning);
va_start(ap, fmt);
vsnprintf(string, sizeof(string), fmt, ap);
va_end(ap);
LispFputs(Stderr, string);
LispFputc(Stderr, '\n');
LispFflush(Stderr);
}
void
LispTopLevel(void)
{
int count;
COD = NIL;
#ifdef DEBUGGER
if (lisp__data.debugging) {
DBG = NIL;
if (lisp__data.debug == LispDebugFinish)
lisp__data.debug = LispDebugUnspec;
lisp__data.debug_level = -1;
lisp__data.debug_step = 0;
}
#endif
gcpro = 0;
lisp__data.block.block_level = 0;
if (lisp__data.block.block_size) {
while (lisp__data.block.block_size)
free(lisp__data.block.block[--lisp__data.block.block_size]);
free(lisp__data.block.block);
lisp__data.block.block = NULL;
}
lisp__data.destroyed = lisp__data.ignore_errors = 0;
if (CONSP(lisp__data.input_list)) {
LispUngetInfo **info, *unget = lisp__data.unget[0];
while (CONSP(lisp__data.input_list))
lisp__data.input_list = CDR(lisp__data.input_list);
SINPUT = lisp__data.input_list;
while (lisp__data.nunget > 1)
free(lisp__data.unget[--lisp__data.nunget]);
if ((info = realloc(lisp__data.unget, sizeof(LispUngetInfo*))) != NULL)
lisp__data.unget = info;
lisp__data.unget[0] = unget;
lisp__data.iunget = 0;
lisp__data.eof = 0;
}
for (count = 0; lisp__data.mem.level;) {
--lisp__data.mem.level;
if (lisp__data.mem.mem[lisp__data.mem.level]) {
++count;
#if 0
printf("LEAK: %p\n", lisp__data.mem.mem[lisp__data.mem.level]);
#endif
}
}
lisp__data.mem.index = 0;
if (count)
LispWarning("%d raw memory pointer(s) left. Probably a leak.", count);
lisp__data.stack.base = lisp__data.stack.length =
lisp__data.env.lex = lisp__data.env.length = lisp__data.env.head = 0;
RETURN_COUNT = 0;
lisp__data.protect.length = 0;
lisp__data.savepackage = PACKAGE;
lisp__data.savepack = lisp__data.pack;
lisp__disable_int = lisp__interrupted = 0;
}
void
LispGC(LispObj *car, LispObj *cdr)
{
Lisp__GC(car, cdr);
}
static void
Lisp__GC(LispObj *car, LispObj *cdr)
{
register LispObj *entry, *last, *freeobj, **pentry, **eentry;
register int nfree;
unsigned i, j;
LispAtom *atom;
struct timeval start, end;
#ifdef DEBUG
long sec, msec;
int count = objseg.nfree;
#else
long msec;
#endif
if (gcpro)
return;
DISABLE_INTERRUPTS();
nfree = 0;
freeobj = NIL;
++lisp__data.gc.count;
#ifdef DEBUG
gettimeofday(&start, NULL);
#else
if (lisp__data.gc.timebits)
gettimeofday(&start, NULL);
#endif
/* Need to measure timings again to check if it is not better/faster
* to just mark these fields as any other data, as the interface was
* changed to properly handle circular lists in the function body itself.
*/
if (lisp__data.gc.immutablebits) {
for (j = 0; j < objseg.nsegs; j++) {
for (entry = objseg.objects[j], last = entry + segsize;
entry < last; entry++)
entry->prot = 0;
}
}
/* Protect all packages */
for (entry = PACK; CONSP(entry); entry = CDR(entry)) {
LispObj *package = CAR(entry);
LispPackage *pack = package->data.package.package;
/* Protect cons cell */
entry->mark = 1;
/* Protect the package cell */
package->mark = 1;
/* Protect package name */
package->data.package.name->mark = 1;
/* Protect package nicknames */
LispMark(package->data.package.nicknames);
/* Protect global symbols */
for (pentry = pack->glb.pairs, eentry = pentry + pack->glb.length;
pentry < eentry; pentry++)
LispMark((*pentry)->data.atom->property->value);
/* Traverse atom list, protecting properties, and function/structure
* definitions if lisp__data.gc.immutablebits set */
for (i = 0; i < STRTBLSZ; i++) {
atom = pack->atoms[i];
while (atom) {
if (atom->property != NOPROPERTY) {
if (atom->a_property)
LispMark(atom->property->properties);
if (lisp__data.gc.immutablebits) {
if (atom->a_function || atom->a_compiled)
LispProt(atom->property->fun.function);
if (atom->a_defsetf)
LispProt(atom->property->setf);
if (atom->a_defstruct)
LispProt(atom->property->structure.definition);
}
}
atom = atom->next;
}
}
}
/* protect environment */
for (pentry = lisp__data.env.values,
eentry = pentry + lisp__data.env.length;
pentry < eentry; pentry++)
LispMark(*pentry);
/* protect multiple return values */
for (pentry = lisp__data.returns.values,
eentry = pentry + lisp__data.returns.count;
pentry < eentry; pentry++)
LispMark(*pentry);
/* protect stack of arguments to builtin functions */
for (pentry = lisp__data.stack.values,
eentry = pentry + lisp__data.stack.length;
pentry < eentry; pentry++)
LispMark(*pentry);
/* protect temporary data used by builtin functions */
for (pentry = lisp__data.protect.objects,
eentry = pentry + lisp__data.protect.length;
pentry < eentry; pentry++)
LispMark(*pentry);
for (i = 0; i < sizeof(x_cons) / sizeof(x_cons[0]); i++)
x_cons[i].mark = 0;
LispMark(COD);
#ifdef DEBUGGER
LispMark(DBG);
LispMark(BRK);
#endif
LispMark(PRO);
LispMark(lisp__data.input_list);
LispMark(lisp__data.output_list);
LispMark(car);
LispMark(cdr);
for (j = 0; j < objseg.nsegs; j++) {
for (entry = objseg.objects[j], last = entry + segsize;
entry < last; entry++) {
if (entry->prot)
continue;
else if (entry->mark)
entry->mark = 0;
else {
switch (XOBJECT_TYPE(entry)) {
case LispString_t:
free(THESTR(entry));
entry->type = LispCons_t;
break;
case LispStream_t:
switch (entry->data.stream.type) {
case LispStreamString:
free(SSTREAMP(entry)->string);
free(SSTREAMP(entry));
break;
case LispStreamFile:
if (FSTREAMP(entry))
LispFclose(FSTREAMP(entry));
break;
case LispStreamPipe:
/* XXX may need special handling if child hangs */
if (PSTREAMP(entry)) {
if (IPSTREAMP(entry))
LispFclose(IPSTREAMP(entry));
if (OPSTREAMP(entry))
LispFclose(OPSTREAMP(entry));
/* don't bother with error stream, will also
* freed in this GC call, maybe just out
* of order */
if (PIDPSTREAMP(entry) > 0) {
kill(PIDPSTREAMP(entry), SIGTERM);
waitpid(PIDPSTREAMP(entry), NULL, 0);
}
free(PSTREAMP(entry));
}
break;
default:
break;
}
entry->type = LispCons_t;
break;
case LispBignum_t:
mpi_clear(entry->data.mp.integer);
free(entry->data.mp.integer);
entry->type = LispCons_t;
break;
case LispBigratio_t:
mpr_clear(entry->data.mp.ratio);
free(entry->data.mp.ratio);
entry->type = LispCons_t;
break;
case LispLambda_t:
if (!SYMBOLP(entry->data.lambda.name))
LispFreeArgList((LispArgList*)
entry->data.lambda.name->data.opaque.data);
entry->type = LispCons_t;
break;
case LispRegex_t:
refree(entry->data.regex.regex);
free(entry->data.regex.regex);
entry->type = LispCons_t;
break;
case LispBytecode_t:
free(entry->data.bytecode.bytecode->code);
free(entry->data.bytecode.bytecode);
entry->type = LispCons_t;
break;
case LispHashTable_t:
LispFreeHashTable(entry->data.hash.table);
entry->type = LispCons_t;
break;
case LispCons_t:
break;
default:
entry->type = LispCons_t;
break;
}
CDR(entry) = freeobj;
freeobj = entry;
++nfree;
}
}
}
objseg.nfree = nfree;
objseg.freeobj = freeobj;
lisp__data.gc.immutablebits = 0;
#ifdef DEBUG
gettimeofday(&end, NULL);
sec = end.tv_sec - start.tv_sec;
msec = end.tv_usec - start.tv_usec;
if (msec < 0) {
--sec;
msec += 1000000;
}
LispMessage("gc: "
"%ld sec, %ld msec, "
"%d recovered, %d free, %d protected, %d total",
sec, msec,
objseg.nfree - count, objseg.nfree,
objseg.nobjs - objseg.nfree, objseg.nobjs);
#else
if (lisp__data.gc.timebits) {
gettimeofday(&end, NULL);
if ((msec = end.tv_usec - start.tv_usec) < 0)
msec += 1000000;
lisp__data.gc.gctime += msec;
}
#endif
ENABLE_INTERRUPTS();
}
static INLINE void
LispCheckMemLevel(void)
{
int i;
/* Check for a free slot before the end. */
for (i = lisp__data.mem.index; i < lisp__data.mem.level; i++)
if (lisp__data.mem.mem[i] == NULL) {
lisp__data.mem.index = i;
return;
}
/* Check for a free slot in the beginning */
for (i = 0; i < lisp__data.mem.index; i++)
if (lisp__data.mem.mem[i] == NULL) {
lisp__data.mem.index = i;
return;
}
lisp__data.mem.index = lisp__data.mem.level;
++lisp__data.mem.level;
if (lisp__data.mem.index < lisp__data.mem.space)
/* There is free space to store pointer. */
return;
else {
void **ptr = (void**)realloc(lisp__data.mem.mem,
(lisp__data.mem.space + 16) *
sizeof(void*));
if (ptr == NULL)
LispDestroy("out of memory");
lisp__data.mem.mem = ptr;
lisp__data.mem.space += 16;
}
}
void
LispMused(void *pointer)
{
int i;
DISABLE_INTERRUPTS();
for (i = lisp__data.mem.index; i >= 0; i--)
if (lisp__data.mem.mem[i] == pointer) {
lisp__data.mem.mem[i] = NULL;
lisp__data.mem.index = i;
goto mused_done;
}
for (i = lisp__data.mem.level - 1; i > lisp__data.mem.index; i--)
if (lisp__data.mem.mem[i] == pointer) {
lisp__data.mem.mem[i] = NULL;
lisp__data.mem.index = i;
break;
}
mused_done:
ENABLE_INTERRUPTS();
}
void *
LispMalloc(size_t size)
{
void *pointer;
DISABLE_INTERRUPTS();
LispCheckMemLevel();
if ((pointer = malloc(size)) == NULL)
LispDestroy("out of memory, couldn't allocate %lu bytes",
(unsigned long)size);
lisp__data.mem.mem[lisp__data.mem.index] = pointer;
ENABLE_INTERRUPTS();
return (pointer);
}
void *
LispCalloc(size_t nmemb, size_t size)
{
void *pointer;
DISABLE_INTERRUPTS();
LispCheckMemLevel();
if ((pointer = calloc(nmemb, size)) == NULL)
LispDestroy("out of memory, couldn't allocate %lu bytes",
(unsigned long)size);
lisp__data.mem.mem[lisp__data.mem.index] = pointer;
ENABLE_INTERRUPTS();
return (pointer);
}
void *
LispRealloc(void *pointer, size_t size)
{
void *ptr;
int i;
DISABLE_INTERRUPTS();
if (pointer != NULL) {
for (i = lisp__data.mem.index; i >= 0; i--)
if (lisp__data.mem.mem[i] == pointer)
goto index_found;
for (i = lisp__data.mem.index + 1; i < lisp__data.mem.level; i++)
if (lisp__data.mem.mem[i] == pointer)
goto index_found;
}
LispCheckMemLevel();
i = lisp__data.mem.index;
index_found:
if ((ptr = realloc(pointer, size)) == NULL)
LispDestroy("out of memory, couldn't realloc");
lisp__data.mem.mem[i] = ptr;
ENABLE_INTERRUPTS();
return (ptr);
}
char *
LispStrdup(char *str)
{
char *ptr = LispMalloc(strlen(str) + 1);
strcpy(ptr, str);
return (ptr);
}
void
LispFree(void *pointer)
{
int i;
DISABLE_INTERRUPTS();
for (i = lisp__data.mem.index; i >= 0; i--)
if (lisp__data.mem.mem[i] == pointer) {
lisp__data.mem.mem[i] = NULL;
lisp__data.mem.index = i;
goto free_done;
}
for (i = lisp__data.mem.level - 1; i > lisp__data.mem.index; i--)
if (lisp__data.mem.mem[i] == pointer) {
lisp__data.mem.mem[i] = NULL;
lisp__data.mem.index = i;
break;
}
free_done:
free(pointer);
ENABLE_INTERRUPTS();
}
LispObj *
LispSetVariable(LispObj *var, LispObj *val, char *fname, int eval)
{
if (!SYMBOLP(var))
LispDestroy("%s: %s is not a symbol", fname, STROBJ(var));
if (eval)
val = EVAL(val);
return (LispSetVar(var, val));
}
int
LispRegisterOpaqueType(char *desc)
{
LispOpaque *opaque;
int ii = STRHASH(desc);
for (opaque = lisp__data.opqs[ii]; opaque; opaque = opaque->next)
if (strcmp(opaque->desc, desc) == 0)
return (opaque->type);
opaque = (LispOpaque*)LispMalloc(sizeof(LispOpaque));
opaque->desc = LispStrdup(desc);
opaque->next = lisp__data.opqs[ii];
lisp__data.opqs[ii] = opaque;
LispMused(opaque->desc);
LispMused(opaque);
return (opaque->type = ++lisp__data.opaque);
}
char *
LispIntToOpaqueType(int type)
{
int i;
LispOpaque *opaque;
if (type) {
for (i = 0; i < STRTBLSZ; i++) {
opaque = lisp__data.opqs[i];
while (opaque) {
if (opaque->type == type)
return (opaque->desc);
opaque = opaque->next;
}
}
LispDestroy("Opaque type %d not registered", type);
}
return (Snil);
}
int
LispDoHashString(char *string)
{
char *pp;
int ii, count;
for (pp = string, ii = count = 0; *pp && count < 32; pp++, count++)
ii = (ii << 1) ^ *pp;
if (ii < 0)
ii = -ii;
return (ii % STRTBLSZ);
}
char *
LispGetAtomString(char *string, int perm)
{
LispStringHash *entry;
int ii = STRHASH(string);
for (entry = lisp__data.strings[ii]; entry != NULL; entry = entry->next)
if (strcmp(entry->string, string) == 0)
return (entry->string);
entry = (LispStringHash*)LispCalloc(1, sizeof(LispStringHash));
if (perm)
entry->string = string;
else
entry->string = LispStrdup(string);
LispMused(entry);
if (!perm)
LispMused(entry->string);
entry->next = lisp__data.strings[ii];
lisp__data.strings[ii] = entry;
return (entry->string);
}
LispAtom *
LispDoGetAtom(char *str, int perm)
{
LispAtom *atom;
int ii = STRHASH(str);
for (atom = lisp__data.pack->atoms[ii]; atom; atom = atom->next)
if (strcmp(atom->string, str) == 0)
return (atom);
atom = (LispAtom*)LispCalloc(1, sizeof(LispAtom));
atom->string = LispGetAtomString(str, perm);
LispMused(atom);
atom->next = lisp__data.pack->atoms[ii];
lisp__data.pack->atoms[ii] = atom;
atom->property = NOPROPERTY;
return (atom);
}
static void
LispAllocAtomProperty(LispAtom *atom)
{
LispProperty *property;
if (atom->property != NOPROPERTY)
LispDestroy("internal error at ALLOC-ATOM-PROPERTY");
property = LispCalloc(1, sizeof(LispProperty));
LispMused(property);
atom->property = property;
property->package = lisp__data.pack;
if (atom->package == NULL)
atom->package = PACKAGE;
LispIncrementAtomReference(atom);
}
static void
LispIncrementAtomReference(LispAtom *atom)
{
if (atom->property != NOPROPERTY)
/* if atom->property is NOPROPERTY, this is an unbound symbol */
++atom->property->refcount;
}
/* Assumes atom property is not NOPROPERTY */
static void
LispDecrementAtomReference(LispAtom *atom)
{
if (atom->property == NOPROPERTY)
/* if atom->property is NOPROPERTY, this is an unbound symbol */
return;
if (atom->property->refcount <= 0)
LispDestroy("internal error at DECREMENT-ATOM-REFERENCE");
--atom->property->refcount;
if (atom->property->refcount == 0) {
LispRemAtomAllProperties(atom);
free(atom->property);
atom->property = NOPROPERTY;
}
}
static void
LispRemAtomAllProperties(LispAtom *atom)
{
if (atom->property != NOPROPERTY) {
if (atom->a_object)
LispRemAtomObjectProperty(atom);
if (atom->a_function) {
lisp__data.gc.immutablebits = 1;
LispRemAtomFunctionProperty(atom);
}
else if (atom->a_compiled) {
lisp__data.gc.immutablebits = 1;
LispRemAtomCompiledProperty(atom);
}
else if (atom->a_builtin) {
lisp__data.gc.immutablebits = 1;
LispRemAtomBuiltinProperty(atom);
}
if (atom->a_defsetf) {
lisp__data.gc.immutablebits = 1;
LispRemAtomSetfProperty(atom);
}
if (atom->a_defstruct) {
lisp__data.gc.immutablebits = 1;
LispRemAtomStructProperty(atom);
}
}
}
void
LispSetAtomObjectProperty(LispAtom *atom, LispObj *object)
{
if (atom->property == NOPROPERTY)
LispAllocAtomProperty(atom);
else if (atom->watch) {
if (atom->object == lisp__data.package) {
if (!PACKAGEP(object))
LispDestroy("Symbol %s must be a package, not %s",
ATOMID(lisp__data.package), STROBJ(object));
lisp__data.pack = object->data.package.package;
}
}
atom->a_object = 1;
SETVALUE(atom, object);
}
static void
LispRemAtomObjectProperty(LispAtom *atom)
{
if (atom->a_object) {
atom->a_object = 0;
atom->property->value = NULL;
}
}
void
LispSetAtomCompiledProperty(LispAtom *atom, LispObj *bytecode)
{
if (atom->property == NOPROPERTY)
LispAllocAtomProperty(atom);
lisp__data.gc.immutablebits = 1;
if (atom->a_builtin) {
atom->a_builtin = 0;
LispFreeArgList(atom->property->alist);
}
else
atom->a_function = 0;
atom->a_compiled = 1;
atom->property->fun.function = bytecode;
}
void
LispRemAtomCompiledProperty(LispAtom *atom)
{
if (atom->a_compiled) {
lisp__data.gc.immutablebits = 1;
atom->property->fun.function = NULL;
atom->a_compiled = 0;
LispFreeArgList(atom->property->alist);
atom->property->alist = NULL;
}
}
void
LispSetAtomFunctionProperty(LispAtom *atom, LispObj *function,
LispArgList *alist)
{
if (atom->property == NOPROPERTY)
LispAllocAtomProperty(atom);
lisp__data.gc.immutablebits = 1;
if (atom->a_function == 0 && atom->a_builtin == 0 && atom->a_compiled == 0)
atom->a_function = 1;
else {
if (atom->a_builtin) {
atom->a_builtin = 0;
LispFreeArgList(atom->property->alist);
}
else
atom->a_compiled = 0;
atom->a_function = 1;
}
atom->property->fun.function = function;
atom->property->alist = alist;
}
void
LispRemAtomFunctionProperty(LispAtom *atom)
{
if (atom->a_function) {
lisp__data.gc.immutablebits = 1;
atom->property->fun.function = NULL;
atom->a_function = 0;
LispFreeArgList(atom->property->alist);
atom->property->alist = NULL;
}
}
void
LispSetAtomBuiltinProperty(LispAtom *atom, LispBuiltin *builtin,
LispArgList *alist)
{
if (atom->property == NOPROPERTY)
LispAllocAtomProperty(atom);
lisp__data.gc.immutablebits = 1;
if (atom->a_builtin == 0 && atom->a_function == 0)
atom->a_builtin = 1;
else {
if (atom->a_function) {
atom->a_function = 0;
LispFreeArgList(atom->property->alist);
}
}
atom->property->fun.builtin = builtin;
atom->property->alist = alist;
}
void
LispRemAtomBuiltinProperty(LispAtom *atom)
{
if (atom->a_builtin) {
lisp__data.gc.immutablebits = 1;
atom->property->fun.function = NULL;
atom->a_builtin = 0;
LispFreeArgList(atom->property->alist);
atom->property->alist = NULL;
}
}
void
LispSetAtomSetfProperty(LispAtom *atom, LispObj *setf, LispArgList *alist)
{
if (atom->property == NOPROPERTY)
LispAllocAtomProperty(atom);
lisp__data.gc.immutablebits = 1;
if (atom->a_defsetf)
LispFreeArgList(atom->property->salist);
atom->a_defsetf = 1;
atom->property->setf = setf;
atom->property->salist = alist;
}
void
LispRemAtomSetfProperty(LispAtom *atom)
{
if (atom->a_defsetf) {
lisp__data.gc.immutablebits = 1;
atom->property->setf = NULL;
atom->a_defsetf = 0;
LispFreeArgList(atom->property->salist);
atom->property->salist = NULL;
}
}
void
LispSetAtomStructProperty(LispAtom *atom, LispObj *def, int fun)
{
if (fun > 0xff)
/* Not suported by the bytecode compiler... */
LispDestroy("SET-ATOM-STRUCT-PROPERTY: "
"more than 256 fields not supported");
if (atom->property == NOPROPERTY)
LispAllocAtomProperty(atom);
lisp__data.gc.immutablebits = 1;
atom->a_defstruct = 1;
atom->property->structure.definition = def;
atom->property->structure.function = fun;
}
void
LispRemAtomStructProperty(LispAtom *atom)
{
if (atom->a_defstruct) {
lisp__data.gc.immutablebits = 1;
atom->property->structure.definition = NULL;
atom->a_defstruct = 0;
}
}
LispAtom *
LispGetAtom(char *str)
{
return (LispDoGetAtom(str, 0));
}
LispAtom *
LispGetPermAtom(char *str)
{
return (LispDoGetAtom(str, 1));
}
#define GET_PROPERTY 0
#define ADD_PROPERTY 1
#define REM_PROPERTY 2
static LispObj *
LispAtomPropertyFunction(LispAtom *atom, LispObj *key, int function)
{
LispObj *list = NIL, *result = NIL;
if (function == ADD_PROPERTY) {
if (atom->property == NOPROPERTY)
LispAllocAtomProperty(atom);
if (atom->property->properties == NULL) {
atom->a_property = 1;
atom->property->properties = NIL;
}
}
if (atom->a_property) {
LispObj *base;
for (base = list = atom->property->properties;
CONSP(list);
list = CDR(list)) {
if (key == CAR(list)) {
result = CDR(list);
break;
}
base = list;
list = CDR(list);
if (!CONSP(list))
LispDestroy("%s: %s has an odd property list length",
STROBJ(atom->object),
function == REM_PROPERTY ? "REMPROP" : "GET");
}
if (CONSP(list) && function == REM_PROPERTY) {
if (!CONSP(CDR(list)))
LispDestroy("REMPROP: %s has an odd property list length",
STROBJ(atom->object));
if (base == list)
atom->property->properties = CDDR(list);
else
RPLACD(CDR(base), CDDR(list));
}
}
if (!CONSP(list)) {
if (function == ADD_PROPERTY) {
atom->property->properties =
CONS(key, CONS(NIL, atom->property->properties));
result = CDR(atom->property->properties);
}
}
else if (function == REM_PROPERTY)
result = T;
return (result);
}
LispObj *
LispGetAtomProperty(LispAtom *atom, LispObj *key)
{
return (LispAtomPropertyFunction(atom, key, GET_PROPERTY));
}
LispObj *
LispPutAtomProperty(LispAtom *atom, LispObj *key, LispObj *value)
{
LispObj *result = LispAtomPropertyFunction(atom, key, ADD_PROPERTY);
RPLACA(result, value);
return (result);
}
LispObj *
LispRemAtomProperty(LispAtom *atom, LispObj *key)
{
return (LispAtomPropertyFunction(atom, key, REM_PROPERTY));
}
LispObj *
LispReplaceAtomPropertyList(LispAtom *atom, LispObj *list)
{
if (atom->property == NOPROPERTY)
LispAllocAtomProperty(atom);
if (atom->property->properties == NULL)
atom->a_property = 1;
atom->property->properties = list;
return (list);
}
#undef GET_PROPERTY
#undef ADD_PROPERTY
#undef REM_PROPERTY
/* Used to make sure that when defining a function like:
* (defun my-function (... &key key1 key2 key3 ...)
* key1, key2, and key3 will be in the keyword package
*/
static LispObj *
LispCheckKeyword(LispObj *keyword)
{
if (KEYWORDP(keyword))
return (keyword);
return (KEYWORD(ATOMID(keyword)));
}
void
LispUseArgList(LispArgList *alist)
{
if (alist->normals.num_symbols)
LispMused(alist->normals.symbols);
if (alist->optionals.num_symbols) {
LispMused(alist->optionals.symbols);
LispMused(alist->optionals.defaults);
LispMused(alist->optionals.sforms);
}
if (alist->keys.num_symbols) {
LispMused(alist->keys.symbols);
LispMused(alist->keys.defaults);
LispMused(alist->keys.sforms);
LispMused(alist->keys.keys);
}
if (alist->auxs.num_symbols) {
LispMused(alist->auxs.symbols);
LispMused(alist->auxs.initials);
}
LispMused(alist);
}
void
LispFreeArgList(LispArgList *alist)
{
if (alist->normals.num_symbols)
LispFree(alist->normals.symbols);
if (alist->optionals.num_symbols) {
LispFree(alist->optionals.symbols);
LispFree(alist->optionals.defaults);
LispFree(alist->optionals.sforms);
}
if (alist->keys.num_symbols) {
LispFree(alist->keys.symbols);
LispFree(alist->keys.defaults);
LispFree(alist->keys.sforms);
LispFree(alist->keys.keys);
}
if (alist->auxs.num_symbols) {
LispFree(alist->auxs.symbols);
LispFree(alist->auxs.initials);
}
LispFree(alist);
}
static LispObj *
LispCheckNeedProtect(LispObj *object)
{
if (object) {
switch (OBJECT_TYPE(object)) {
case LispNil_t:
case LispAtom_t:
case LispFunction_t:
case LispFixnum_t:
case LispSChar_t:
return (NULL);
default:
return (object);
}
}
return (NULL);
}
LispObj *
LispListProtectedArguments(LispArgList *alist)
{
int i;
GC_ENTER();
LispObj *arguments, *cons, *obj, *prev;
arguments = cons = prev = NIL;
for (i = 0; i < alist->optionals.num_symbols; i++) {
if ((obj = LispCheckNeedProtect(alist->optionals.defaults[i])) != NULL) {
if (arguments == NIL) {
arguments = cons = prev = CONS(obj, NIL);
GC_PROTECT(arguments);
}
else {
RPLACD(cons, CONS(obj, NIL));
prev = cons;
cons = CDR(cons);
}
}
}
for (i = 0; i < alist->keys.num_symbols; i++) {
if ((obj = LispCheckNeedProtect(alist->keys.defaults[i])) != NULL) {
if (arguments == NIL) {
arguments = cons = prev = CONS(obj, NIL);
GC_PROTECT(arguments);
}
else {
RPLACD(cons, CONS(obj, NIL));
prev = cons;
cons = CDR(cons);
}
}
}
for (i = 0; i < alist->auxs.num_symbols; i++) {
if ((obj = LispCheckNeedProtect(alist->auxs.initials[i])) != NULL) {
if (arguments == NIL) {
arguments = cons = prev = CONS(obj, NIL);
GC_PROTECT(arguments);
}
else {
RPLACD(cons, CONS(obj, NIL));
prev = cons;
cons = CDR(cons);
}
}
}
GC_LEAVE();
/* Don't add a NIL cell at the end, to save some space */
if (arguments != NIL) {
if (arguments == cons)
arguments = CAR(cons);
else
CDR(prev) = CAR(cons);
}
return (arguments);
}
LispArgList *
LispCheckArguments(LispFunType type, LispObj *list, char *name, int builtin)
{
static char *types[4] = {"LAMBDA-LIST", "FUNCTION", "MACRO", "SETF-METHOD"};
static char *fnames[4] = {"LAMBDA", "DEFUN", "DEFMACRO", "DEFSETF"};
#define IKEY 0
#define IOPTIONAL 1
#define IREST 2
#define IAUX 3
static char *keys[4] = {"&KEY", "&OPTIONAL", "&REST", "&AUX"};
int rest, optional, key, aux, count;
LispArgList *alist;
LispObj *spec, *sform, *defval, *default_value;
char description[8], *desc;
/* If LispRealloc fails, the previous memory will be released
* in LispTopLevel, unless LispMused was called on the pointer */
#define REALLOC_OBJECTS(pointer, count) \
pointer = LispRealloc(pointer, (count) * sizeof(LispObj*))
alist = LispCalloc(1, sizeof(LispArgList));
if (!CONSP(list)) {
if (list != NIL)
LispDestroy("%s %s: %s cannot be a %s argument list",
fnames[type], name, STROBJ(list), types[type]);
alist->description = GETATOMID("");
return (alist);
}
default_value = builtin ? UNSPEC : NIL;
description[0] = '\0';
desc = description;
rest = optional = key = aux = 0;
for (; CONSP(list); list = CDR(list)) {
spec = CAR(list);
if (CONSP(spec)) {
if (builtin)
LispDestroy("builtin function argument cannot have default value");
if (aux) {
if (!SYMBOLP(CAR(spec)) ||
(CDR(spec) != NIL && CDDR(spec) != NIL))
LispDestroy("%s %s: bad &AUX argument %s",
fnames[type], name, STROBJ(spec));
defval = CDR(spec) != NIL ? CADR(spec) : NIL;
count = alist->auxs.num_symbols;
REALLOC_OBJECTS(alist->auxs.symbols, count + 1);
REALLOC_OBJECTS(alist->auxs.initials, count + 1);
alist->auxs.symbols[count] = CAR(spec);
alist->auxs.initials[count] = defval;
++alist->auxs.num_symbols;
if (count == 0)
*desc++ = 'a';
++alist->num_arguments;
}
else if (rest)
LispDestroy("%s %s: syntax error parsing %s",
fnames[type], name, keys[IREST]);
else if (key) {
LispObj *akey = CAR(spec);
defval = default_value;
sform = NULL;
if (CONSP(akey)) {
/* check for special case, as in:
* (defun a (&key ((key name) 'default-value)) name)
* (a 'key 'test) => TEST
* (a) => DEFAULT-VALUE
*/
if (!SYMBOLP(CAR(akey)) || !CONSP(CDR(akey)) ||
!SYMBOLP(CADR(akey)) || CDDR(akey) != NIL ||
(CDR(spec) != NIL && CDDR(spec) != NIL))
LispDestroy("%s %s: bad special &KEY %s",
fnames[type], name, STROBJ(spec));
if (CDR(spec) != NIL)
defval = CADR(spec);
spec = CADR(akey);
akey = CAR(akey);
}
else {
akey = NULL;
if (!SYMBOLP(CAR(spec)))
LispDestroy("%s %s: %s cannot be a %s argument name",
fnames[type], name,
STROBJ(CAR(spec)), types[type]);
/* check if default value provided, and optionally a `svar' */
else if (CDR(spec) != NIL && (!CONSP(CDR(spec)) ||
(CDDR(spec) != NIL &&
(!SYMBOLP(CAR(CDDR(spec))) ||
CDR(CDDR(spec)) != NIL))))
LispDestroy("%s %s: bad argument specification %s",
fnames[type], name, STROBJ(spec));
if (CONSP(CDR(spec))) {
defval = CADR(spec);
if (CONSP(CDDR(spec)))
sform = CAR(CDDR(spec));
}
/* Add to keyword package, and set the keyword in the
* argument list, so that a function argument keyword
* will reference the same object, and make comparison
* simpler. */
spec = LispCheckKeyword(CAR(spec));
}
count = alist->keys.num_symbols;
REALLOC_OBJECTS(alist->keys.keys, count + 1);
REALLOC_OBJECTS(alist->keys.defaults, count + 1);
REALLOC_OBJECTS(alist->keys.sforms, count + 1);
REALLOC_OBJECTS(alist->keys.symbols, count + 1);
alist->keys.symbols[count] = spec;
alist->keys.defaults[count] = defval;
alist->keys.sforms[count] = sform;
alist->keys.keys[count] = akey;
++alist->keys.num_symbols;
if (count == 0)
*desc++ = 'k';
alist->num_arguments += 1 + (sform != NULL);
}
else if (optional) {
defval = default_value;
sform = NULL;
if (!SYMBOLP(CAR(spec)))
LispDestroy("%s %s: %s cannot be a %s argument name",
fnames[type], name,
STROBJ(CAR(spec)), types[type]);
/* check if default value provided, and optionally a `svar' */
else if (CDR(spec) != NIL && (!CONSP(CDR(spec)) ||
(CDDR(spec) != NIL &&
(!SYMBOLP(CAR(CDDR(spec))) ||
CDR(CDDR(spec)) != NIL))))
LispDestroy("%s %s: bad argument specification %s",
fnames[type], name, STROBJ(spec));
if (CONSP(CDR(spec))) {
defval = CADR(spec);
if (CONSP(CDDR(spec)))
sform = CAR(CDDR(spec));
}
spec = CAR(spec);
count = alist->optionals.num_symbols;
REALLOC_OBJECTS(alist->optionals.symbols, count + 1);
REALLOC_OBJECTS(alist->optionals.defaults, count + 1);
REALLOC_OBJECTS(alist->optionals.sforms, count + 1);
alist->optionals.symbols[count] = spec;
alist->optionals.defaults[count] = defval;
alist->optionals.sforms[count] = sform;
++alist->optionals.num_symbols;
if (count == 0)
*desc++ = 'o';
alist->num_arguments += 1 + (sform != NULL);
}
/* Normal arguments cannot have default value */
else
LispDestroy("%s %s: syntax error parsing %s",
fnames[type], name, STROBJ(spec));
}
/* spec must be an atom, excluding keywords */
else if (!SYMBOLP(spec) || KEYWORDP(spec))
LispDestroy("%s %s: %s cannot be a %s argument",
fnames[type], name, STROBJ(spec), types[type]);
else {
Atom_id atom = ATOMID(spec);
if (atom[0] == '&') {
if (atom == Srest) {
if (rest || aux || CDR(list) == NIL || !SYMBOLP(CADR(list))
/* only &aux allowed after &rest */
|| (CDDR(list) != NIL && !SYMBOLP(CAR(CDDR(list))) &&
ATOMID(CAR(CDDR(list))) != Saux))
LispDestroy("%s %s: syntax error parsing %s",
fnames[type], name, ATOMID(spec));
if (key)
LispDestroy("%s %s: %s not allowed after %s",
fnames[type], name, keys[IREST], keys[IKEY]);
rest = 1;
continue;
}
else if (atom == Skey) {
if (rest || aux)
LispDestroy("%s %s: %s not allowed after %s",
fnames[type], name, ATOMID(spec),
rest ? keys[IREST] : keys[IAUX]);
key = 1;
continue;
}
else if (atom == Soptional) {
if (rest || optional || aux || key)
LispDestroy("%s %s: %s not allowed after %s",
fnames[type], name, ATOMID(spec),
rest ? keys[IREST] :
optional ?
keys[IOPTIONAL] :
aux ? keys[IAUX] : keys[IKEY]);
optional = 1;
continue;
}
else if (atom == Saux) {
/* &AUX must be the last keyword parameter */
if (aux)
LispDestroy("%s %s: syntax error parsing %s",
fnames[type], name, ATOMID(spec));
else if (builtin)
LispDestroy("builtin function cannot have &AUX arguments");
aux = 1;
continue;
}
/* Untill more lambda-list keywords supported, don't allow
* argument names starting with the '&' character */
else
LispDestroy("%s %s: %s not allowed/implemented",
fnames[type], name, ATOMID(spec));
}
/* Add argument to alist */
if (aux) {
count = alist->auxs.num_symbols;
REALLOC_OBJECTS(alist->auxs.symbols, count + 1);
REALLOC_OBJECTS(alist->auxs.initials, count + 1);
alist->auxs.symbols[count] = spec;
alist->auxs.initials[count] = default_value;
++alist->auxs.num_symbols;
if (count == 0)
*desc++ = 'a';
++alist->num_arguments;
}
else if (rest) {
alist->rest = spec;
*desc++ = 'r';
++alist->num_arguments;
}
else if (key) {
/* Add to keyword package, and set the keyword in the
* argument list, so that a function argument keyword
* will reference the same object, and make comparison
* simpler. */
spec = LispCheckKeyword(spec);
count = alist->keys.num_symbols;
REALLOC_OBJECTS(alist->keys.keys, count + 1);
REALLOC_OBJECTS(alist->keys.defaults, count + 1);
REALLOC_OBJECTS(alist->keys.sforms, count + 1);
REALLOC_OBJECTS(alist->keys.symbols, count + 1);
alist->keys.symbols[count] = spec;
alist->keys.defaults[count] = default_value;
alist->keys.sforms[count] = NULL;
alist->keys.keys[count] = NULL;
++alist->keys.num_symbols;
if (count == 0)
*desc++ = 'k';
++alist->num_arguments;
}
else if (optional) {
count = alist->optionals.num_symbols;
REALLOC_OBJECTS(alist->optionals.symbols, count + 1);
REALLOC_OBJECTS(alist->optionals.defaults, count + 1);
REALLOC_OBJECTS(alist->optionals.sforms, count + 1);
alist->optionals.symbols[count] = spec;
alist->optionals.defaults[count] = default_value;
alist->optionals.sforms[count] = NULL;
++alist->optionals.num_symbols;
if (count == 0)
*desc++ = 'o';
++alist->num_arguments;
}
else {
count = alist->normals.num_symbols;
REALLOC_OBJECTS(alist->normals.symbols, count + 1);
alist->normals.symbols[count] = spec;
++alist->normals.num_symbols;
if (count == 0)
*desc++ = '.';
++alist->num_arguments;
}
}
}
/* Check for dotted argument list */
if (list != NIL)
LispDestroy("%s %s: %s cannot end %s arguments",
fnames[type], name, STROBJ(list), types[type]);
*desc = '\0';
alist->description = LispGetAtomString(description, 0);
return (alist);
}
void
LispAddBuiltinFunction(LispBuiltin *builtin)
{
static LispObj stream;
static LispString string;
static int first = 1;
LispObj *name, *obj, *list, *cons, *code;
LispAtom *atom;
LispArgList *alist;
int length = lisp__data.protect.length;
if (first) {
stream.type = LispStream_t;
stream.data.stream.source.string = &string;
stream.data.stream.pathname = NIL;
stream.data.stream.type = LispStreamString;
stream.data.stream.readable = 1;
stream.data.stream.writable = 0;
string.output = 0;
first = 0;
}
string.string = builtin->declaration;
string.length = strlen(builtin->declaration);
string.input = 0;
code = COD;
LispPushInput(&stream);
name = LispRead();
list = cons = CONS(name, NIL);
if (length + 1 >= lisp__data.protect.space)
LispMoreProtects();
lisp__data.protect.objects[lisp__data.protect.length++] = list;
while ((obj = LispRead()) != NULL) {
RPLACD(cons, CONS(obj, NIL));
cons = CDR(cons);
}
LispPopInput(&stream);
atom = name->data.atom;
alist = LispCheckArguments(builtin->type, CDR(list), atom->string, 1);
builtin->symbol = CAR(list);
LispSetAtomBuiltinProperty(atom, builtin, alist);
LispUseArgList(alist);
/* Make function a extern symbol, unless told to not do so */
if (!builtin->internal)
LispExportSymbol(name);
lisp__data.protect.length = length;
COD = code; /* LispRead protect data in COD */
}
void
LispAllocSeg(LispObjSeg *seg, int cellcount)
{
unsigned int i;
LispObj **list, *obj;
DISABLE_INTERRUPTS();
while (seg->nfree < cellcount) {
if ((obj = (LispObj*)calloc(1, sizeof(LispObj) * segsize)) == NULL) {
ENABLE_INTERRUPTS();
LispDestroy("out of memory");
}
if ((list = (LispObj**)realloc(seg->objects,
sizeof(LispObj*) * (seg->nsegs + 1))) == NULL) {
free(obj);
ENABLE_INTERRUPTS();
LispDestroy("out of memory");
}
seg->objects = list;
seg->objects[seg->nsegs] = obj;
seg->nfree += segsize;
seg->nobjs += segsize;
for (i = 1; i < segsize; i++, obj++) {
/* Objects of type cons are the most used, save some time
* by not setting it's type in LispNewCons. */
obj->type = LispCons_t;
CDR(obj) = obj + 1;
}
obj->type = LispCons_t;
CDR(obj) = seg->freeobj;
seg->freeobj = seg->objects[seg->nsegs];
++seg->nsegs;
}
#ifdef DEBUG
LispMessage("gc: %d cell(s) allocated at %d segment(s)",
seg->nobjs, seg->nsegs);
#endif
ENABLE_INTERRUPTS();
}
static INLINE void
LispMark(register LispObj *object)
{
mark_again:
switch (OBJECT_TYPE(object)) {
case LispNil_t:
case LispAtom_t:
case LispFixnum_t:
case LispSChar_t:
case LispFunction_t:
return;
case LispLambda_t:
if (OPAQUEP(object->data.lambda.name))
object->data.lambda.name->mark = 1;
object->mark = 1;
LispMark(object->data.lambda.data);
object = object->data.lambda.code;
goto mark_cons;
case LispQuote_t:
case LispBackquote_t:
case LispFunctionQuote_t:
object->mark = 1;
object = object->data.quote;
goto mark_again;
case LispPathname_t:
object->mark = 1;
object = object->data.pathname;
goto mark_again;
case LispComma_t:
object->mark = 1;
object = object->data.comma.eval;
goto mark_again;
case LispComplex_t:
if (POINTERP(object->data.complex.real))
object->data.complex.real->mark = 1;
if (POINTERP(object->data.complex.imag))
object->data.complex.imag->mark = 1;
break;
case LispCons_t:
mark_cons:
for (; CONSP(object) && !object->mark; object = CDR(object)) {
object->mark = 1;
switch (OBJECT_TYPE(CAR(object))) {
case LispNil_t:
case LispAtom_t:
case LispFixnum_t:
case LispSChar_t:
case LispPackage_t: /* protected in gc */
break;
case LispInteger_t:
case LispDFloat_t:
case LispString_t:
case LispRatio_t:
case LispOpaque_t:
case LispBignum_t:
case LispBigratio_t:
CAR(object)->mark = 1;
break;
default:
LispMark(CAR(object));
break;
}
}
if (POINTERP(object) && !object->mark)
goto mark_again;
return;
case LispArray_t:
LispMark(object->data.array.list);
object->mark = 1;
object = object->data.array.dim;
goto mark_cons;
case LispStruct_t:
object->mark = 1;
object = object->data.struc.fields;
goto mark_cons;
case LispStream_t:
mark_stream:
LispMark(object->data.stream.pathname);
if (object->data.stream.type == LispStreamPipe) {
object->mark = 1;
object = object->data.stream.source.program->errorp;
goto mark_stream;
}
break;
case LispRegex_t:
object->data.regex.pattern->mark = 1;
break;
case LispBytecode_t:
object->mark = 1;
object = object->data.bytecode.code;
goto mark_again;
case LispHashTable_t: {
unsigned long i;
LispHashEntry *entry = object->data.hash.table->entries,
*last = entry + object->data.hash.table->num_entries;
if (object->mark)
return;
object->mark = 1;
for (; entry < last; entry++) {
for (i = 0; i < entry->count; i++) {
switch (OBJECT_TYPE(entry->keys[i])) {
case LispNil_t:
case LispAtom_t:
case LispFixnum_t:
case LispSChar_t:
case LispFunction_t:
case LispPackage_t:
break;
case LispInteger_t:
case LispDFloat_t:
case LispString_t:
case LispRatio_t:
case LispOpaque_t:
case LispBignum_t:
case LispBigratio_t:
entry->keys[i]->mark = 1;
break;
default:
LispMark(entry->keys[i]);
break;
}
switch (OBJECT_TYPE(entry->values[i])) {
case LispNil_t:
case LispAtom_t:
case LispFixnum_t:
case LispSChar_t:
case LispFunction_t:
case LispPackage_t:
break;
case LispInteger_t:
case LispDFloat_t:
case LispString_t:
case LispRatio_t:
case LispOpaque_t:
case LispBignum_t:
case LispBigratio_t:
entry->values[i]->mark = 1;
break;
default:
LispMark(entry->values[i]);
break;
}
}
}
} return;
default:
break;
}
object->mark = 1;
}
static INLINE void
LispProt(register LispObj *object)
{
prot_again:
switch (OBJECT_TYPE(object)) {
case LispNil_t:
case LispAtom_t:
case LispFixnum_t:
case LispSChar_t:
case LispFunction_t:
return;
case LispLambda_t:
if (OPAQUEP(object->data.lambda.name))
object->data.lambda.name->prot = 1;
object->prot = 1;
LispProt(object->data.lambda.data);
object = object->data.lambda.code;
goto prot_cons;
case LispQuote_t:
case LispBackquote_t:
case LispFunctionQuote_t:
object->prot = 1;
object = object->data.quote;
goto prot_again;
case LispPathname_t:
object->prot = 1;
object = object->data.pathname;
goto prot_again;
case LispComma_t:
object->prot = 1;
object = object->data.comma.eval;
goto prot_again;
case LispComplex_t:
if (POINTERP(object->data.complex.real))
object->data.complex.real->prot = 1;
if (POINTERP(object->data.complex.imag))
object->data.complex.imag->prot = 1;
break;
case LispCons_t:
prot_cons:
for (; CONSP(object) && !object->prot; object = CDR(object)) {
object->prot = 1;
switch (OBJECT_TYPE(CAR(object))) {
case LispNil_t:
case LispAtom_t:
case LispFixnum_t:
case LispSChar_t:
case LispFunction_t:
case LispPackage_t: /* protected in gc */
break;
case LispInteger_t:
case LispDFloat_t:
case LispString_t:
case LispRatio_t:
case LispOpaque_t:
case LispBignum_t:
case LispBigratio_t:
CAR(object)->prot = 1;
break;
default:
LispProt(CAR(object));
break;
}
}
if (POINTERP(object) && !object->prot)
goto prot_again;
return;
case LispArray_t:
LispProt(object->data.array.list);
object->prot = 1;
object = object->data.array.dim;
goto prot_cons;
case LispStruct_t:
object->prot = 1;
object = object->data.struc.fields;
goto prot_cons;
case LispStream_t:
prot_stream:
LispProt(object->data.stream.pathname);
if (object->data.stream.type == LispStreamPipe) {
object->prot = 1;
object = object->data.stream.source.program->errorp;
goto prot_stream;
}
break;
case LispRegex_t:
object->data.regex.pattern->prot = 1;
break;
case LispBytecode_t:
object->prot = 1;
object = object->data.bytecode.code;
goto prot_again;
case LispHashTable_t: {
unsigned long i;
LispHashEntry *entry = object->data.hash.table->entries,
*last = entry + object->data.hash.table->num_entries;
if (object->prot)
return;
object->prot = 1;
for (; entry < last; entry++) {
for (i = 0; i < entry->count; i++) {
switch (OBJECT_TYPE(entry->keys[i])) {
case LispNil_t:
case LispAtom_t:
case LispFixnum_t:
case LispSChar_t:
case LispFunction_t:
case LispPackage_t:
break;
case LispInteger_t:
case LispDFloat_t:
case LispString_t:
case LispRatio_t:
case LispOpaque_t:
case LispBignum_t:
case LispBigratio_t:
entry->keys[i]->prot = 1;
break;
default:
LispProt(entry->keys[i]);
break;
}
switch (OBJECT_TYPE(entry->values[i])) {
case LispNil_t:
case LispAtom_t:
case LispFixnum_t:
case LispSChar_t:
case LispFunction_t:
case LispPackage_t:
break;
case LispInteger_t:
case LispDFloat_t:
case LispString_t:
case LispRatio_t:
case LispOpaque_t:
case LispBignum_t:
case LispBigratio_t:
entry->values[i]->prot = 1;
break;
default:
LispProt(entry->values[i]);
break;
}
}
}
} return;
default:
break;
}
object->prot = 1;
}
void
LispProtect(LispObj *key, LispObj *list)
{
PRO = CONS(CONS(key, list), PRO);
}
void
LispUProtect(LispObj *key, LispObj *list)
{
LispObj *prev, *obj;
for (prev = obj = PRO; obj != NIL; prev = obj, obj = CDR(obj))
if (CAR(CAR(obj)) == key && CDR(CAR(obj)) == list) {
if (obj == PRO)
PRO = CDR(PRO);
else
CDR(prev) = CDR(obj);
return;
}
LispDestroy("no match for %s, at UPROTECT", STROBJ(key));
}
static LispObj *
Lisp__New(LispObj *car, LispObj *cdr)
{
int cellcount;
LispObj *obj;
Lisp__GC(car, cdr);
#if 0
lisp__data.gc.average = (objseg.nfree + lisp__data.gc.average) >> 1;
if (lisp__data.gc.average < minfree) {
if (lisp__data.gc.expandbits < 6)
++lisp__data.gc.expandbits;
}
else if (lisp__data.gc.expandbits)
--lisp__data.gc.expandbits;
/* For 32 bit computers, where sizeof(LispObj) == 16,
* minfree is set to 1024, and expandbits limited to 6,
* the maximum extra memory requested here should be 1Mb
*/
cellcount = minfree << lisp__data.gc.expandbits;
#else
/* Try to keep at least 3 times more free cells than the de number
* of used cells in the freelist, to amenize the cost of the gc time,
* in the, currently, very simple gc strategy code. */
cellcount = (objseg.nobjs - objseg.nfree) * 3;
cellcount = cellcount + (minfree - (cellcount % minfree));
#endif
if (objseg.freeobj == NIL || objseg.nfree < cellcount)
LispAllocSeg(&objseg, cellcount);
obj = objseg.freeobj;
objseg.freeobj = CDR(obj);
--objseg.nfree;
return (obj);
}
LispObj *
LispNew(LispObj *car, LispObj *cdr)
{
LispObj *obj = objseg.freeobj;
if (obj == NIL)
obj = Lisp__New(car, cdr);
else {
objseg.freeobj = CDR(obj);
--objseg.nfree;
}
return (obj);
}
LispObj *
LispNewAtom(char *str, int intern)
{
LispObj *object;
LispAtom *atom = LispDoGetAtom(str, 0);
if (atom->object) {
if (intern && atom->package == NULL)
atom->package = PACKAGE;
return (atom->object);
}
if (atomseg.freeobj == NIL)
LispAllocSeg(&atomseg, pagesize);
object = atomseg.freeobj;
atomseg.freeobj = CDR(object);
--atomseg.nfree;
object->type = LispAtom_t;
object->data.atom = atom;
atom->object = object;
if (intern)
atom->package = PACKAGE;
return (object);
}
LispObj *
LispNewStaticAtom(char *str)
{
LispObj *object;
LispAtom *atom = LispDoGetAtom(str, 1);
object = LispNewSymbol(atom);
return (object);
}
LispObj *
LispNewSymbol(LispAtom *atom)
{
if (atom->object) {
if (atom->package == NULL)
atom->package = PACKAGE;
return (atom->object);
}
else {
LispObj *symbol;
if (atomseg.freeobj == NIL)
LispAllocSeg(&atomseg, pagesize);
symbol = atomseg.freeobj;
atomseg.freeobj = CDR(symbol);
--atomseg.nfree;
symbol->type = LispAtom_t;
symbol->data.atom = atom;
atom->object = symbol;
atom->package = PACKAGE;
return (symbol);
}
}
/* function representation is created on demand and never released,
* even if the function is undefined and never defined again */
LispObj *
LispNewFunction(LispObj *symbol)
{
LispObj *function;
if (symbol->data.atom->function)
return (symbol->data.atom->function);
if (symbol->data.atom->package == NULL)
symbol->data.atom->package = PACKAGE;
if (atomseg.freeobj == NIL)
LispAllocSeg(&atomseg, pagesize);
function = atomseg.freeobj;
atomseg.freeobj = CDR(function);
--atomseg.nfree;
function->type = LispFunction_t;
function->data.atom = symbol->data.atom;
symbol->data.atom->function = function;
return (function);
}
/* symbol name representation is created on demand and never released */
LispObj *
LispSymbolName(LispObj *symbol)
{
LispObj *name;
LispAtom *atom = symbol->data.atom;
if (atom->name)
return (atom->name);
if (atomseg.freeobj == NIL)
LispAllocSeg(&atomseg, pagesize);
name = atomseg.freeobj;
atomseg.freeobj = CDR(name);
--atomseg.nfree;
name->type = LispString_t;
THESTR(name) = atom->string;
STRLEN(name) = strlen(atom->string);
name->data.string.writable = 0;
atom->name = name;
return (name);
}
LispObj *
LispNewFunctionQuote(LispObj *object)
{
LispObj *quote = LispNew(object, NIL);
quote->type = LispFunctionQuote_t;
quote->data.quote = object;
return (quote);
}
LispObj *
LispNewDFloat(double value)
{
LispObj *dfloat = objseg.freeobj;
if (dfloat == NIL)
dfloat = Lisp__New(NIL, NIL);
else {
objseg.freeobj = CDR(dfloat);
--objseg.nfree;
}
dfloat->type = LispDFloat_t;
dfloat->data.dfloat = value;
return (dfloat);
}
LispObj *
LispNewString(char *str, long length, int alloced)
{
char *cstring;
LispObj *string = objseg.freeobj;
if (string == NIL)
string = Lisp__New(NIL, NIL);
else {
objseg.freeobj = CDR(string);
--objseg.nfree;
}
if (alloced)
cstring = str;
else {
cstring = LispMalloc(length + 1);
memcpy(cstring, str, length);
cstring[length] = '\0';
}
LispMused(cstring);
string->type = LispString_t;
THESTR(string) = cstring;
STRLEN(string) = length;
string->data.string.writable = 1;
return (string);
}
LispObj *
LispNewComplex(LispObj *realpart, LispObj *imagpart)
{
LispObj *complexp = objseg.freeobj;
if (complexp == NIL)
complexp = Lisp__New(realpart, imagpart);
else {
objseg.freeobj = CDR(complexp);
--objseg.nfree;
}
complexp->type = LispComplex_t;
complexp->data.complex.real = realpart;
complexp->data.complex.imag = imagpart;
return (complexp);
}
LispObj *
LispNewInteger(long integer)
{
if (integer > MOST_POSITIVE_FIXNUM || integer < MOST_NEGATIVE_FIXNUM) {
LispObj *object = objseg.freeobj;
if (object == NIL)
object = Lisp__New(NIL, NIL);
else {
objseg.freeobj = CDR(object);
--objseg.nfree;
}
object->type = LispInteger_t;
object->data.integer = integer;
return (object);
}
return (FIXNUM(integer));
}
LispObj *
LispNewRatio(long num, long den)
{
LispObj *ratio = objseg.freeobj;
if (ratio == NIL)
ratio = Lisp__New(NIL, NIL);
else {
objseg.freeobj = CDR(ratio);
--objseg.nfree;
}
ratio->type = LispRatio_t;
ratio->data.ratio.numerator = num;
ratio->data.ratio.denominator = den;
return (ratio);
}
LispObj *
LispNewVector(LispObj *objects)
{
GC_ENTER();
long count;
LispObj *array, *dimension;
for (count = 0, array = objects; CONSP(array); count++, array = CDR(array))
;
GC_PROTECT(objects);
dimension = CONS(FIXNUM(count), NIL);
array = LispNew(objects, dimension);
array->type = LispArray_t;
array->data.array.list = objects;
array->data.array.dim = dimension;
array->data.array.rank = 1;
array->data.array.type = LispNil_t;
array->data.array.zero = count == 0;
GC_LEAVE();
return (array);
}
LispObj *
LispNewQuote(LispObj *object)
{
LispObj *quote = LispNew(object, NIL);
quote->type = LispQuote_t;
quote->data.quote = object;
return (quote);
}
LispObj *
LispNewBackquote(LispObj *object)
{
LispObj *backquote = LispNew(object, NIL);
backquote->type = LispBackquote_t;
backquote->data.quote = object;
return (backquote);
}
LispObj *
LispNewComma(LispObj *object, int atlist)
{
LispObj *comma = LispNew(object, NIL);
comma->type = LispComma_t;
comma->data.comma.eval = object;
comma->data.comma.atlist = atlist;
return (comma);
}
LispObj *
LispNewCons(LispObj *car, LispObj *cdr)
{
LispObj *cons = objseg.freeobj;
if (cons == NIL)
cons = Lisp__New(car, cdr);
else {
objseg.freeobj = CDR(cons);
--objseg.nfree;
}
CAR(cons) = car;
CDR(cons) = cdr;
return (cons);
}
LispObj *
LispNewLambda(LispObj *name, LispObj *code, LispObj *data, LispFunType type)
{
LispObj *fun = LispNew(data, code);
fun->type = LispLambda_t;
fun->funtype = type;
fun->data.lambda.name = name;
fun->data.lambda.code = code;
fun->data.lambda.data = data;
return (fun);
}
LispObj *
LispNewStruct(LispObj *fields, LispObj *def)
{
LispObj *struc = LispNew(fields, def);
struc->type = LispStruct_t;
struc->data.struc.fields = fields;
struc->data.struc.def = def;
return (struc);
}
LispObj *
LispNewOpaque(void *data, int type)
{
LispObj *opaque = LispNew(NIL, NIL);
opaque->type = LispOpaque_t;
opaque->data.opaque.data = data;
opaque->data.opaque.type = type;
return (opaque);
}
/* string argument must be static, or allocated */
LispObj *
LispNewKeyword(char *string)
{
LispObj *keyword;
if (PACKAGE != lisp__data.keyword) {
LispObj *savepackage;
LispPackage *savepack;
/* Save package environment */
savepackage = PACKAGE;
savepack = lisp__data.pack;
/* Change package environment */
PACKAGE = lisp__data.keyword;
lisp__data.pack = lisp__data.key;
/* Create symbol in keyword package */
keyword = LispNewStaticAtom(string);
/* Restore package environment */
PACKAGE = savepackage;
lisp__data.pack = savepack;
}
else
/* Just create symbol in keyword package */
keyword = LispNewStaticAtom(string);
/* Export keyword symbol */
LispExportSymbol(keyword);
/* All keywords are constants */
keyword->data.atom->constant = 1;
/* XXX maybe should bound the keyword to itself, but that would
* require allocating a LispProperty structure for every keyword */
return (keyword);
}
LispObj *
LispNewPathname(LispObj *obj)
{
LispObj *path = LispNew(obj, NIL);
path->type = LispPathname_t;
path->data.pathname = obj;
return (path);
}
LispObj *
LispNewStringStream(char *string, int flags, long length, int alloced)
{
LispObj *stream = LispNew(NIL, NIL);
SSTREAMP(stream) = LispCalloc(1, sizeof(LispString));
if (alloced)
SSTREAMP(stream)->string = string;
else {
SSTREAMP(stream)->string = LispMalloc(length + 1);
memcpy(SSTREAMP(stream)->string, string, length);
SSTREAMP(stream)->string[length] = '\0';
}
stream->type = LispStream_t;
SSTREAMP(stream)->length = length;
LispMused(SSTREAMP(stream));
LispMused(SSTREAMP(stream)->string);
stream->data.stream.type = LispStreamString;
stream->data.stream.readable = (flags & STREAM_READ) != 0;
stream->data.stream.writable = (flags & STREAM_WRITE) != 0;
SSTREAMP(stream)->space = length + 1;
stream->data.stream.pathname = NIL;
return (stream);
}
LispObj *
LispNewFileStream(LispFile *file, LispObj *path, int flags)
{
LispObj *stream = LispNew(NIL, NIL);
stream->type = LispStream_t;
FSTREAMP(stream) = file;
stream->data.stream.pathname = path;
stream->data.stream.type = LispStreamFile;
stream->data.stream.readable = (flags & STREAM_READ) != 0;
stream->data.stream.writable = (flags & STREAM_WRITE) != 0;
return (stream);
}
LispObj *
LispNewPipeStream(LispPipe *program, LispObj *path, int flags)
{
LispObj *stream = LispNew(NIL, NIL);
stream->type = LispStream_t;
PSTREAMP(stream) = program;
stream->data.stream.pathname = path;
stream->data.stream.type = LispStreamPipe;
stream->data.stream.readable = (flags & STREAM_READ) != 0;
stream->data.stream.writable = (flags & STREAM_WRITE) != 0;
return (stream);
}
LispObj *
LispNewStandardStream(LispFile *file, LispObj *description, int flags)
{
LispObj *stream = LispNew(NIL, NIL);
stream->type = LispStream_t;
FSTREAMP(stream) = file;
stream->data.stream.pathname = description;
stream->data.stream.type = LispStreamStandard;
stream->data.stream.readable = (flags & STREAM_READ) != 0;
stream->data.stream.writable = (flags & STREAM_WRITE) != 0;
return (stream);
}
LispObj *
LispNewBignum(mpi *bignum)
{
LispObj *integer = LispNew(NIL, NIL);
integer->type = LispBignum_t;
integer->data.mp.integer = bignum;
LispMused(bignum->digs);
LispMused(bignum);
return (integer);
}
LispObj *
LispNewBigratio(mpr *bigratio)
{
LispObj *ratio = LispNew(NIL, NIL);
ratio->type = LispBigratio_t;
ratio->data.mp.ratio = bigratio;
LispMused(mpr_num(bigratio)->digs);
LispMused(mpr_den(bigratio)->digs);
LispMused(bigratio);
return (ratio);
}
/* name must be of type LispString_t */
LispObj *
LispNewPackage(LispObj *name, LispObj *nicknames)
{
LispObj *package = LispNew(name, nicknames);
LispPackage *pack = LispCalloc(1, sizeof(LispPackage));
package->type = LispPackage_t;
package->data.package.name = name;
package->data.package.nicknames = nicknames;
package->data.package.package = pack;
LispMused(pack);
return (package);
}
LispObj *
LispSymbolFunction(LispObj *symbol)
{
LispAtom *atom = symbol->data.atom;
if ((atom->a_builtin &&
atom->property->fun.builtin->type == LispFunction) ||
(atom->a_function &&
atom->property->fun.function->funtype == LispFunction) ||
(atom->a_defstruct &&
atom->property->structure.function != STRUCT_NAME) ||
/* XXX currently bytecode is only generated for functions */
atom->a_compiled)
symbol = FUNCTION(symbol);
else
LispDestroy("SYMBOL-FUNCTION: %s is not a function", STROBJ(symbol));
return (symbol);
}
static INLINE LispObj *
LispGetVarPack(LispObj *symbol)
{
int ii;
char *string;
LispAtom *atom;
string = ATOMID(symbol);
ii = STRHASH(string);
atom = lisp__data.pack->atoms[ii];
while (atom) {
if (strcmp(atom->string, string) == 0)
return (atom->object);
atom = atom->next;
}
/* Symbol not found, just import it */
return (NULL);
}
/* package must be of type LispPackage_t */
void
LispUsePackage(LispObj *package)
{
unsigned i;
LispAtom *atom;
LispPackage *pack;
LispObj **pentry, **eentry;
/* Already using its own symbols... */
if (package == PACKAGE)
return;
/* Check if package not already in use-package list */
for (pentry = lisp__data.pack->use.pairs,
eentry = pentry + lisp__data.pack->use.length;
pentry < eentry; pentry++)
if (*pentry == package)
return;
/* Remember this package is in the use-package list */
if (lisp__data.pack->use.length + 1 >= lisp__data.pack->use.space) {
LispObj **pairs = realloc(lisp__data.pack->use.pairs,
(lisp__data.pack->use.space + 1) *
sizeof(LispObj*));
if (pairs == NULL)
LispDestroy("out of memory");
lisp__data.pack->use.pairs = pairs;
++lisp__data.pack->use.space;
}
lisp__data.pack->use.pairs[lisp__data.pack->use.length++] = package;
/* Import all extern symbols from package */
pack = package->data.package.package;
/* Traverse atom list, searching for extern symbols */
for (i = 0; i < STRTBLSZ; i++) {
atom = pack->atoms[i];
while (atom) {
if (atom->ext)
LispImportSymbol(atom->object);
atom = atom->next;
}
}
}
/* symbol must be of type LispAtom_t */
void
LispImportSymbol(LispObj *symbol)
{
int increment;
LispAtom *atom;
LispObj *current;
current = LispGetVarPack(symbol);
if (current == NULL || current->data.atom->property == NOPROPERTY) {
/* No conflicts */
if (symbol->data.atom->a_object) {
/* If it is a bounded variable */
if (lisp__data.pack->glb.length + 1 >= lisp__data.pack->glb.space)
LispMoreGlobals(lisp__data.pack);
lisp__data.pack->glb.pairs[lisp__data.pack->glb.length++] = symbol;
}
/* Create copy of atom in current package */
atom = LispDoGetAtom(ATOMID(symbol), 0);
/* Need to create a copy because if anything new is atached to the
* property, the current package is the owner, not the previous one. */
/* And reference the same properties */
atom->property = symbol->data.atom->property;
increment = 1;
}
else if (current->data.atom->property != symbol->data.atom->property) {
/* Symbol already exists in the current package,
* but does not reference the same variable */
LispContinuable("Symbol %s already defined in package %s. Redefine?",
ATOMID(symbol), THESTR(PACKAGE->data.package.name));
atom = current->data.atom;
/* Continued from error, redefine variable */
LispDecrementAtomReference(atom);
atom->property = symbol->data.atom->property;
atom->a_object = atom->a_function = atom->a_builtin =
atom->a_property = atom->a_defsetf = atom->a_defstruct = 0;
increment = 1;
}
else {
/* Symbol is already available in the current package, just update */
atom = current->data.atom;
increment = 0;
}
/* If importing an important system variable */
atom->watch = symbol->data.atom->watch;
/* Update constant flag */
atom->constant = symbol->data.atom->constant;
/* Set home-package and unique-atom associated with symbol */
atom->package = symbol->data.atom->package;
atom->object = symbol->data.atom->object;
if (symbol->data.atom->a_object)
atom->a_object = 1;
if (symbol->data.atom->a_function)
atom->a_function = 1;
else if (symbol->data.atom->a_builtin)
atom->a_builtin = 1;
else if (symbol->data.atom->a_compiled)
atom->a_compiled = 1;
if (symbol->data.atom->a_property)
atom->a_property = 1;
if (symbol->data.atom->a_defsetf)
atom->a_defsetf = 1;
if (symbol->data.atom->a_defstruct)
atom->a_defstruct = 1;
if (increment)
/* Increase reference count, more than one package using the symbol */
LispIncrementAtomReference(symbol->data.atom);
}
/* symbol must be of type LispAtom_t */
void
LispExportSymbol(LispObj *symbol)
{
/* This does not automatically export symbols to another package using
* the symbols of the current package */
symbol->data.atom->ext = 1;
}
#ifdef __GNUC__
LispObj *
LispGetVar(LispObj *atom)
{
return (LispDoGetVar(atom));
}
static INLINE LispObj *
LispDoGetVar(LispObj *atom)
#else
#define LispDoGetVar LispGetVar
LispObj *
LispGetVar(LispObj *atom)
#endif
{
LispAtom *name;
int i, base, offset;
Atom_id id;
name = atom->data.atom;
if (name->constant && name->package == lisp__data.keyword)
return (atom);
/* XXX offset should be stored elsewhere, it is unique, like the string
* pointer. Unless a multi-thread interface is implemented (where
* multiple stacks would be required, the offset value should be
* stored with the string, so that a few cpu cicles could be saved
* by initializing the value to -1, and only searching for the symbol
* binding if it is not -1, and if no binding is found, because the
* lexical scope was left, reset offset to -1. */
offset = name->offset;
id = name->string;
base = lisp__data.env.lex;
i = lisp__data.env.head - 1;
if (offset <= i && (offset >= base || name->dyn) &&
lisp__data.env.names[offset] == id)
return (lisp__data.env.values[offset]);
for (; i >= base; i--)
if (lisp__data.env.names[i] == id) {
name->offset = i;
return (lisp__data.env.values[i]);
}
if (name->dyn) {
/* Keep searching as maybe a rebound dynamic variable */
for (; i >= 0; i--)
if (lisp__data.env.names[i] == id) {
name->offset = i;
return (lisp__data.env.values[i]);
}
if (name->a_object) {
/* Check for a symbol defined as special, but not yet bound. */
if (name->property->value == UNBOUND)
return (NULL);
return (name->property->value);
}
}
return (name->a_object ? name->property->value : NULL);
}
#ifdef DEBUGGER
/* Same code as LispDoGetVar, but returns the address of the pointer to
* the object value. Used only by the debugger */
void *
LispGetVarAddr(LispObj *atom)
{
LispAtom *name;
int i, base;
Atom_id id;
name = atom->data.atom;
if (name->constant && name->package == lisp__data.keyword)
return (&atom);
id = name->string;
i = lisp__data.env.head - 1;
for (base = lisp__data.env.lex; i >= base; i--)
if (lisp__data.env.names[i] == id)
return (&(lisp__data.env.values[i]));
if (name->dyn) {
for (; i >= 0; i--)
if (lisp__data.env.names[i] == id)
return (&(lisp__data.env.values[i]));
if (name->a_object) {
/* Check for a symbol defined as special, but not yet bound */
if (name->property->value == UNBOUND)
return (NULL);
return (&(name->property->value));
}
}
return (name->a_object ? &(name->property->value) : NULL);
}
#endif
/* Only removes global variables. To be called by makunbound
* Local variables are unbounded once their block is closed anyway.
*/
void
LispUnsetVar(LispObj *atom)
{
LispAtom *name = atom->data.atom;
if (name->package) {
int i;
LispPackage *pack = name->package->data.package.package;
for (i = pack->glb.length - 1; i > 0; i--)
if (pack->glb.pairs[i] == atom) {
LispRemAtomObjectProperty(name);
--pack->glb.length;
if (i < pack->glb.length)
memmove(pack->glb.pairs + i, pack->glb.pairs + i + 1,
sizeof(LispObj*) * (pack->glb.length - i));
/* unset hint about dynamically binded variable */
if (name->dyn)
name->dyn = 0;
break;
}
}
}
LispObj *
LispAddVar(LispObj *atom, LispObj *obj)
{
if (lisp__data.env.length >= lisp__data.env.space)
LispMoreEnvironment();
LispDoAddVar(atom, obj);
return (obj);
}
static INLINE void
LispDoAddVar(LispObj *symbol, LispObj *value)
{
LispAtom *atom = symbol->data.atom;
atom->offset = lisp__data.env.length;
lisp__data.env.values[lisp__data.env.length] = value;
lisp__data.env.names[lisp__data.env.length++] = atom->string;
}
LispObj *
LispSetVar(LispObj *atom, LispObj *obj)
{
LispPackage *pack;
LispAtom *name;
int i, base, offset;
Atom_id id;
name = atom->data.atom;
offset = name->offset;
id = name->string;
base = lisp__data.env.lex;
i = lisp__data.env.head - 1;
if (offset <= i && (offset >= base || name->dyn) &&
lisp__data.env.names[offset] == id)
return (lisp__data.env.values[offset] = obj);
for (; i >= base; i--)
if (lisp__data.env.names[i] == id) {
name->offset = i;
return (lisp__data.env.values[i] = obj);
}
if (name->dyn) {
for (; i >= 0; i--)
if (lisp__data.env.names[i] == id)
return (lisp__data.env.values[i] = obj);
if (name->watch) {
LispSetAtomObjectProperty(name, obj);
return (obj);
}
return (SETVALUE(name, obj));
}
if (name->a_object) {
if (name->watch) {
LispSetAtomObjectProperty(name, obj);
return (obj);
}
return (SETVALUE(name, obj));
}
LispSetAtomObjectProperty(name, obj);
pack = name->package->data.package.package;
if (pack->glb.length >= pack->glb.space)
LispMoreGlobals(pack);
pack->glb.pairs[pack->glb.length++] = atom;
return (obj);
}
void
LispProclaimSpecial(LispObj *atom, LispObj *value, LispObj *doc)
{
int i = 0, dyn, glb;
LispAtom *name;
LispPackage *pack;
glb = 0;
name = atom->data.atom;
pack = name->package->data.package.package;
dyn = name->dyn;
if (!dyn) {
/* Note: don't check if a local variable already is using the symbol */
for (i = pack->glb.length - 1; i >= 0; i--)
if (pack->glb.pairs[i] == atom) {
glb = 1;
break;
}
}
if (dyn) {
if (name->property->value == UNBOUND && value)
/* if variable was just made special, but not bounded */
LispSetAtomObjectProperty(name, value);
}
else if (glb)
/* Already a global variable, but not marked as special.
* Set hint about dynamically binded variable. */
name->dyn = 1;
else {
/* create new special variable */
LispSetAtomObjectProperty(name, value ? value : UNBOUND);
if (pack->glb.length >= pack->glb.space)
LispMoreGlobals(pack);
pack->glb.pairs[pack->glb.length] = atom;
++pack->glb.length;
/* set hint about possibly dynamically binded variable */
name->dyn = 1;
}
if (doc != NIL)
LispAddDocumentation(atom, doc, LispDocVariable);
}
void
LispDefconstant(LispObj *atom, LispObj *value, LispObj *doc)
{
int i;
LispAtom *name = atom->data.atom;
LispPackage *pack = name->package->data.package.package;
/* Unset hint about dynamically binded variable, if set. */
name->dyn = 0;
/* Check if variable is bounded as a global variable */
for (i = pack->glb.length - 1; i >= 0; i--)
if (pack->glb.pairs[i] == atom)
break;
if (i < 0) {
/* Not a global variable */
if (pack->glb.length >= pack->glb.space)
LispMoreGlobals(pack);
pack->glb.pairs[pack->glb.length] = atom;
++pack->glb.length;
}
/* If already a constant variable */
if (name->constant && name->a_object && name->property->value != value)
LispWarning("constant %s is being redefined", STROBJ(atom));
else
name->constant = 1;
/* Set constant value */
LispSetAtomObjectProperty(name, value);
if (doc != NIL)
LispAddDocumentation(atom, doc, LispDocVariable);
}
void
LispAddDocumentation(LispObj *symbol, LispObj *documentation, LispDocType_t type)
{
int length;
char *string;
LispAtom *atom;
LispObj *object;
if (!SYMBOLP(symbol) || !STRINGP(documentation))
LispDestroy("DOCUMENTATION: invalid argument");
atom = symbol->data.atom;
if (atom->documentation[type])
LispRemDocumentation(symbol, type);
/* allocate documentation in atomseg */
if (atomseg.freeobj == NIL)
LispAllocSeg(&atomseg, pagesize);
length = STRLEN(documentation);
string = LispMalloc(length);
memcpy(string, THESTR(documentation), length);
string[length] = '\0';
object = atomseg.freeobj;
atomseg.freeobj = CDR(object);
--atomseg.nfree;
object->type = LispString_t;
THESTR(object) = string;
STRLEN(object) = length;
object->data.string.writable = 0;
atom->documentation[type] = object;
LispMused(string);
}
void
LispRemDocumentation(LispObj *symbol, LispDocType_t type)
{
LispAtom *atom;
if (!SYMBOLP(symbol))
LispDestroy("DOCUMENTATION: invalid argument");
atom = symbol->data.atom;
if (atom->documentation[type]) {
/* reclaim object to atomseg */
free(THESTR(atom->documentation[type]));
CDR(atom->documentation[type]) = atomseg.freeobj;
atomseg.freeobj = atom->documentation[type];
atom->documentation[type] = NULL;
++atomseg.nfree;
}
}
LispObj *
LispGetDocumentation(LispObj *symbol, LispDocType_t type)
{
LispAtom *atom;
if (!SYMBOLP(symbol))
LispDestroy("DOCUMENTATION: invalid argument");
atom = symbol->data.atom;
return (atom->documentation[type] ? atom->documentation[type] : NIL);
}
LispObj *
LispReverse(LispObj *list)
{
LispObj *tmp, *res = NIL;
while (list != NIL) {
tmp = CDR(list);
CDR(list) = res;
res = list;
list = tmp;
}
return (res);
}
LispBlock *
LispBeginBlock(LispObj *tag, LispBlockType type)
{
LispBlock *block;
unsigned blevel = lisp__data.block.block_level + 1;
if (blevel > lisp__data.block.block_size) {
LispBlock **blk;
if (blevel > MAX_STACK_DEPTH)
LispDestroy("stack overflow");
DISABLE_INTERRUPTS();
blk = realloc(lisp__data.block.block, sizeof(LispBlock*) * (blevel + 1));
block = NULL;
if (blk == NULL || (block = malloc(sizeof(LispBlock))) == NULL) {
ENABLE_INTERRUPTS();
LispDestroy("out of memory");
}
lisp__data.block.block = blk;
lisp__data.block.block[lisp__data.block.block_size] = block;
lisp__data.block.block_size = blevel;
ENABLE_INTERRUPTS();
}
block = lisp__data.block.block[lisp__data.block.block_level];
if (type == LispBlockCatch && !CONSTANTP(tag)) {
tag = EVAL(tag);
lisp__data.protect.objects[lisp__data.protect.length++] = tag;
}
block->type = type;
block->tag = tag;
block->stack = lisp__data.stack.length;
block->protect = lisp__data.protect.length;
block->block_level = lisp__data.block.block_level;
lisp__data.block.block_level = blevel;
#ifdef DEBUGGER
if (lisp__data.debugging) {
block->debug_level = lisp__data.debug_level;
block->debug_step = lisp__data.debug_step;
}
#endif
return (block);
}
void
LispEndBlock(LispBlock *block)
{
lisp__data.protect.length = block->protect;
lisp__data.block.block_level = block->block_level;
#ifdef DEBUGGER
if (lisp__data.debugging) {
if (lisp__data.debug_level >= block->debug_level) {
while (lisp__data.debug_level > block->debug_level) {
DBG = CDR(DBG);
--lisp__data.debug_level;
}
}
lisp__data.debug_step = block->debug_step;
}
#endif
}
void
LispBlockUnwind(LispBlock *block)
{
LispBlock *unwind;
int blevel = lisp__data.block.block_level;
while (blevel > 0) {
unwind = lisp__data.block.block[--blevel];
if (unwind->type == LispBlockProtect) {
BLOCKJUMP(unwind);
}
if (unwind == block)
/* jump above unwind block */
break;
}
}
static LispObj *
LispEvalBackquoteObject(LispObj *argument, int list, int quote)
{
LispObj *result = argument, *object;
if (!POINTERP(argument))
return (argument);
else if (XCOMMAP(argument)) {
/* argument may need to be evaluated */
int atlist;
if (!list && argument->data.comma.atlist)
/* cannot append, not in a list */
LispDestroy("EVAL: ,@ only allowed on lists");
--quote;
if (quote < 0)
LispDestroy("EVAL: comma outside of backquote");
result = object = argument->data.comma.eval;
atlist = COMMAP(object) && object->data.comma.atlist;
if (POINTERP(result) && (XCOMMAP(result) || XBACKQUOTEP(result)))
/* nested commas, reduce 1 level, or backquote,
* don't call LispEval or quote argument will be reset */
result = LispEvalBackquoteObject(object, 0, quote);
else if (quote == 0)
/* just evaluate it */
result = EVAL(result);
if (quote != 0)
result = result == object ? argument : COMMA(result, atlist);
}
else if (XBACKQUOTEP(argument)) {
object = argument->data.quote;
result = LispEvalBackquote(object, quote + 1);
if (quote)
result = result == object ? argument : BACKQUOTE(result);
}
else if (XQUOTEP(argument) && POINTERP(argument->data.quote) &&
(XCOMMAP(argument->data.quote) ||
XBACKQUOTEP(argument->data.quote) ||
XCONSP(argument->data.quote))) {
/* ensures `',sym to be the same as `(quote ,sym) */
object = argument->data.quote;
result = LispEvalBackquote(argument->data.quote, quote);
result = result == object ? argument : QUOTE(result);
}
return (result);
}
LispObj *
LispEvalBackquote(LispObj *argument, int quote)
{
int protect;
LispObj *result, *object, *cons, *cdr;
if (!CONSP(argument))
return (LispEvalBackquoteObject(argument, 0, quote));
result = cdr = NIL;
protect = lisp__data.protect.length;
/* always generate a new list for the result, even if nothing
* is evaluated. It is not expected to use backqoutes when
* not required. */
/* reserve a GC protected slot for the result */
if (protect + 1 >= lisp__data.protect.space)
LispMoreProtects();
lisp__data.protect.objects[lisp__data.protect.length++] = NIL;
for (cons = argument; ; cons = CDR(cons)) {
/* if false, last argument, and if cons is not NIL, a dotted list */
int list = CONSP(cons), insert;
if (list)
object = CAR(cons);
else
object = cons;
if (COMMAP(object))
/* need to insert list elements in result, not just cons it? */
insert = object->data.comma.atlist;
else
insert = 0;
/* evaluate object, if required */
if (CONSP(object))
object = LispEvalBackquote(object, quote);
else
object = LispEvalBackquoteObject(object, insert, quote);
if (result == NIL) {
/* if starting result list */
if (!insert) {
if (list)
result = cdr = CONS(object, NIL);
else
result = cdr = object;
/* gc protect result */
lisp__data.protect.objects[protect] = result;
}
else {
if (!CONSP(object)) {
result = cdr = object;
/* gc protect result */
lisp__data.protect.objects[protect] = result;
}
else {
result = cdr = CONS(CAR(object), NIL);
/* gc protect result */
lisp__data.protect.objects[protect] = result;
/* add remaining elements to result */
for (object = CDR(object);
CONSP(object);
object = CDR(object)) {
RPLACD(cdr, CONS(CAR(object), NIL));
cdr = CDR(cdr);
}
if (object != NIL) {
/* object was a dotted list */
RPLACD(cdr, object);
cdr = CDR(cdr);
}
}
}
}
else {
if (!CONSP(cdr))
LispDestroy("EVAL: cannot append to %s", STROBJ(cdr));
if (!insert) {
if (list) {
RPLACD(cdr, CONS(object, NIL));
cdr = CDR(cdr);
}
else {
RPLACD(cdr, object);
cdr = object;
}
}
else {
if (!CONSP(object)) {
RPLACD(cdr, object);
/* if object is NIL, it is a empty list appended, not
* creating a dotted list. */
if (object != NIL)
cdr = object;
}
else {
for (; CONSP(object); object = CDR(object)) {
RPLACD(cdr, CONS(CAR(object), NIL));
cdr = CDR(cdr);
}
if (object != NIL) {
/* object was a dotted list */
RPLACD(cdr, object);
cdr = CDR(cdr);
}
}
}
}
/* if last argument list element processed */
if (!list)
break;
}
lisp__data.protect.length = protect;
return (result);
}
void
LispMoreEnvironment(void)
{
Atom_id *names;
LispObj **values;
DISABLE_INTERRUPTS();
names = realloc(lisp__data.env.names,
(lisp__data.env.space + 256) * sizeof(Atom_id));
if (names != NULL) {
values = realloc(lisp__data.env.values,
(lisp__data.env.space + 256) * sizeof(LispObj*));
if (values != NULL) {
lisp__data.env.names = names;
lisp__data.env.values = values;
lisp__data.env.space += 256;
ENABLE_INTERRUPTS();
return;
}
else
free(names);
}
ENABLE_INTERRUPTS();
LispDestroy("out of memory");
}
void
LispMoreStack(void)
{
LispObj **values;
DISABLE_INTERRUPTS();
values = realloc(lisp__data.stack.values,
(lisp__data.stack.space + 256) * sizeof(LispObj*));
if (values == NULL) {
ENABLE_INTERRUPTS();
LispDestroy("out of memory");
}
lisp__data.stack.values = values;
lisp__data.stack.space += 256;
ENABLE_INTERRUPTS();
}
void
LispMoreGlobals(LispPackage *pack)
{
LispObj **pairs;
DISABLE_INTERRUPTS();
pairs = realloc(pack->glb.pairs,
(pack->glb.space + 256) * sizeof(LispObj*));
if (pairs == NULL) {
ENABLE_INTERRUPTS();
LispDestroy("out of memory");
}
pack->glb.pairs = pairs;
pack->glb.space += 256;
ENABLE_INTERRUPTS();
}
void
LispMoreProtects(void)
{
LispObj **objects;
DISABLE_INTERRUPTS();
objects = realloc(lisp__data.protect.objects,
(lisp__data.protect.space + 256) * sizeof(LispObj*));
if (objects == NULL) {
ENABLE_INTERRUPTS();
LispDestroy("out of memory");
}
lisp__data.protect.objects = objects;
lisp__data.protect.space += 256;
ENABLE_INTERRUPTS();
}
static int
LispMakeEnvironment(LispArgList *alist, LispObj *values,
LispObj *name, int eval, int builtin)
{
char *desc;
int i, count, base;
LispObj **symbols, **defaults, **sforms;
#define BUILTIN_ARGUMENT(value) \
lisp__data.stack.values[lisp__data.stack.length++] = value
/* If the index value is from register variables, this
* can save some cpu time. Useful for normal arguments
* that are the most common, and thus the ones that
* consume more time in LispMakeEnvironment. */
#define BUILTIN_NO_EVAL_ARGUMENT(index, value) \
lisp__data.stack.values[index] = value
#define NORMAL_ARGUMENT(symbol, value) \
LispDoAddVar(symbol, value)
if (builtin) {
base = lisp__data.stack.length;
if (base + alist->num_arguments > lisp__data.stack.space) {
do
LispMoreStack();
while (base + alist->num_arguments > lisp__data.stack.space);
}
}
else {
base = lisp__data.env.length;
if (base + alist->num_arguments > lisp__data.env.space) {
do
LispMoreEnvironment();
while (base + alist->num_arguments > lisp__data.env.space);
}
}
desc = alist->description;
switch (*desc++) {
case '.':
goto normal_label;
case 'o':
goto optional_label;
case 'k':
goto key_label;
case 'r':
goto rest_label;
case 'a':
goto aux_label;
default:
goto done_label;
}
/* Code below is done in several almost identical loops, to avoid
* checking the value of the arguments eval and builtin too much times */
/* Normal arguments */
normal_label:
i = 0;
count = alist->normals.num_symbols;
if (builtin) {
if (eval) {
for (; i < count && CONSP(values); i++, values = CDR(values)) {
BUILTIN_ARGUMENT(EVAL(CAR(values)));
}
}
else {
for (; i < count && CONSP(values); i++, values = CDR(values)) {
BUILTIN_NO_EVAL_ARGUMENT(base + i, CAR(values));
}
/* macro BUILTIN_NO_EVAL_ARGUMENT does not update
* lisp__data.stack.length, as there is no risk of GC while
* adding the arguments. */
lisp__data.stack.length += i;
}
}
else {
symbols = alist->normals.symbols;
if (eval) {
for (; i < count && CONSP(values); i++, values = CDR(values)) {
NORMAL_ARGUMENT(symbols[i], EVAL(CAR(values)));
}
}
else {
for (; i < count && CONSP(values); i++, values = CDR(values)) {
NORMAL_ARGUMENT(symbols[i], CAR(values));
}
}
}
if (i < count)
LispDestroy("%s: too few arguments", STROBJ(name));
switch (*desc++) {
case 'o':
goto optional_label;
case 'k':
goto key_label;
case 'r':
goto rest_label;
case 'a':
goto aux_label;
default:
goto done_label;
}
/* &OPTIONAL */
optional_label:
i = 0;
count = alist->optionals.num_symbols;
defaults = alist->optionals.defaults;
sforms = alist->optionals.sforms;
if (builtin) {
if (eval) {
for (; i < count && CONSP(values); i++, values = CDR(values))
BUILTIN_ARGUMENT(EVAL(CAR(values)));
for (; i < count; i++)
BUILTIN_ARGUMENT(UNSPEC);
}
else {
for (; i < count && CONSP(values); i++, values = CDR(values))
BUILTIN_ARGUMENT(CAR(values));
for (; i < count; i++)
BUILTIN_ARGUMENT(UNSPEC);
}
}
else {
symbols = alist->optionals.symbols;
if (eval) {
for (; i < count && CONSP(values); i++, values = CDR(values)) {
NORMAL_ARGUMENT(symbols[i], EVAL(CAR(values)));
if (sforms[i]) {
NORMAL_ARGUMENT(sforms[i], T);
}
}
}
else {
for (; i < count && CONSP(values); i++, values = CDR(values)) {
NORMAL_ARGUMENT(symbols[i], CAR(values));
if (sforms[i]) {
NORMAL_ARGUMENT(sforms[i], T);
}
}
}
/* default arguments are evaluated for macros */
for (; i < count; i++) {
if (!CONSTANTP(defaults[i])) {
int head = lisp__data.env.head;
int lex = lisp__data.env.lex;
lisp__data.env.lex = base;
lisp__data.env.head = lisp__data.env.length;
NORMAL_ARGUMENT(symbols[i], EVAL(defaults[i]));
lisp__data.env.head = head;
lisp__data.env.lex = lex;
}
else {
NORMAL_ARGUMENT(symbols[i], defaults[i]);
}
if (sforms[i]) {
NORMAL_ARGUMENT(sforms[i], NIL);
}
}
}
switch (*desc++) {
case 'k':
goto key_label;
case 'r':
goto rest_label;
case 'a':
goto aux_label;
default:
goto done_label;
}
/* &KEY */
key_label:
{
int argc, nused;
LispObj *val, *karg, **keys;
/* Count number of remaining arguments */
for (karg = values, argc = 0; CONSP(karg); karg = CDR(karg), argc++) {
karg = CDR(karg);
if (!CONSP(karg))
LispDestroy("%s: &KEY needs arguments as pairs",
STROBJ(name));
}
/* OPTIMIZATION:
* Builtin functions require that the keyword be in the keyword package.
* User functions don't need the arguments being pushed in the stack
* in the declared order (bytecode expects it...).
* XXX Error checking should be done elsewhere, code may be looping
* and doing error check here may consume too much cpu time.
* XXX Would also be good to already have the arguments specified in
* the correct order.
*/
nused = 0;
val = NIL;
count = alist->keys.num_symbols;
symbols = alist->keys.symbols;
defaults = alist->keys.defaults;
sforms = alist->keys.sforms;
if (builtin) {
/* Arguments must be created in the declared order */
i = 0;
if (eval) {
for (; i < count; i++) {
for (karg = values; CONSP(karg); karg = CDDR(karg)) {
/* This is only true if both point to the
* same symbol in the keyword package. */
if (symbols[i] == CAR(karg)) {
if (karg == values)
values = CDDR(values);
++nused;
BUILTIN_ARGUMENT(EVAL(CADR(karg)));
goto keyword_builtin_eval_used_label;
}
}
BUILTIN_ARGUMENT(UNSPEC);
keyword_builtin_eval_used_label:;
}
}
else {
for (; i < count; i++) {
for (karg = values; CONSP(karg); karg = CDDR(karg)) {
if (symbols[i] == CAR(karg)) {
if (karg == values)
values = CDDR(values);
++nused;
BUILTIN_ARGUMENT(CADR(karg));
goto keyword_builtin_used_label;
}
}
BUILTIN_ARGUMENT(UNSPEC);
keyword_builtin_used_label:;
}
}
if (argc != nused) {
/* Argument(s) may be incorrectly specified, or specified
* twice (what is not an error). */
for (karg = values; CONSP(karg); karg = CDDR(karg)) {
val = CAR(karg);
if (KEYWORDP(val)) {
for (i = 0; i < count; i++)
if (symbols[i] == val)
break;
}
else
/* Just make the error test true */
i = count;
if (i == count)
goto invalid_keyword_label;
}
}
}
#if 0
else {
/* The base offset of the atom in the stack, to check for
* keywords specified twice. */
LispObj *symbol;
int offset = lisp__data.env.length;
keys = alist->keys.keys;
for (karg = values; CONSP(karg); karg = CDDR(karg)) {
symbol = CAR(karg);
if (SYMBOLP(symbol)) {
/* Must be a keyword, but even if it is a keyword, may
* be a typo, so assume it is correct. If it is not
* in the argument list, it is an error. */
for (i = 0; i < count; i++) {
if (!keys[i] && symbols[i] == symbol) {
LispAtom *atom = symbol->data.atom;
/* Symbol found in the argument list. */
if (atom->offset >= offset &&
atom->offset < offset + nused &&
lisp__data.env.names[atom->offset] ==
atom->string)
/* Specified more than once... */
goto keyword_duplicated_label;
break;
}
}
}
else {
Atom_id id;
if (!QUOTEP(symbol) || !SYMBOLP(val = symbol->data.quote)) {
/* Bad argument. */
val = symbol;
goto invalid_keyword_label;
}
id = ATOMID(val);
for (i = 0; i < count; i++) {
if (keys[i] && ATOMID(keys[i]) == id) {
LispAtom *atom = val->data.atom;
/* Symbol found in the argument list. */
if (atom->offset >= offset &&
atom->offset < offset + nused &&
lisp__data.env.names[atom->offset] ==
atom->string)
/* Specified more than once... */
goto keyword_duplicated_label;
break;
}
}
}
if (i == count) {
/* Argument specification not found. */
val = symbol;
goto invalid_keyword_label;
}
++nused;
if (eval) {
NORMAL_ARGUMENT(symbols[i], EVAL(CADR(karg)));
}
else {
NORMAL_ARGUMENT(symbols[i], CADR(karg));
}
if (sforms[i]) {
NORMAL_ARGUMENT(sforms[i], T);
}
keyword_duplicated_label:;
}
/* Add variables that were not specified in the function call. */
if (nused < count) {
int j;
for (i = 0; i < count; i++) {
Atom_id id = ATOMID(symbols[i]);
for (j = offset + nused - 1; j >= offset; j--) {
if (lisp__data.env.names[j] == id)
break;
}
if (j < offset) {
/* Argument not specified. Use default value */
/* default arguments are evaluated for macros */
if (!CONSTANTP(defaults[i])) {
int head = lisp__data.env.head;
int lex = lisp__data.env.lex;
lisp__data.env.lex = base;
lisp__data.env.head = lisp__data.env.length;
NORMAL_ARGUMENT(symbols[i], EVAL(defaults[i]));
lisp__data.env.head = head;
lisp__data.env.lex = lex;
}
else {
NORMAL_ARGUMENT(symbols[i], defaults[i]);
}
if (sforms[i]) {
NORMAL_ARGUMENT(sforms[i], NIL);
}
}
}
}
}
#else
else {
int varset;
sforms = alist->keys.sforms;
keys = alist->keys.keys;
/* Add variables */
for (i = 0; i < alist->keys.num_symbols; i++) {
val = defaults[i];
varset = 0;
if (keys[i]) {
Atom_id atom = ATOMID(keys[i]);
/* Special keyword specification, need to compare ATOMID
* and keyword specification must be a quoted object */
for (karg = values; CONSP(karg); karg = CDR(karg)) {
val = CAR(karg);
if (QUOTEP(val) && atom == ATOMID(val->data.quote)) {
val = CADR(karg);
varset = 1;
++nused;
break;
}
karg = CDR(karg);
}
}
else {
/* Normal keyword specification, can compare object pointers,
* as they point to the same object in the keyword package */
for (karg = values; CONSP(karg); karg = CDR(karg)) {
/* Don't check if argument is a valid keyword or
* special quoted keyword */
if (symbols[i] == CAR(karg)) {
val = CADR(karg);
varset = 1;
++nused;
break;
}
karg = CDR(karg);
}
}
/* Add the variable to environment */
if (varset) {
NORMAL_ARGUMENT(symbols[i], eval ? EVAL(val) : val);
if (sforms[i]) {
NORMAL_ARGUMENT(sforms[i], T);
}
}
else {
/* default arguments are evaluated for macros */
if (!CONSTANTP(val)) {
int head = lisp__data.env.head;
int lex = lisp__data.env.lex;
lisp__data.env.lex = base;
lisp__data.env.head = lisp__data.env.length;
NORMAL_ARGUMENT(symbols[i], EVAL(val));
lisp__data.env.head = head;
lisp__data.env.lex = lex;
}
else {
NORMAL_ARGUMENT(symbols[i], val);
}
if (sforms[i]) {
NORMAL_ARGUMENT(sforms[i], NIL);
}
}
}
if (argc != nused) {
/* Argument(s) may be incorrectly specified, or specified
* twice (what is not an error). */
for (karg = values; CONSP(karg); karg = CDDR(karg)) {
val = CAR(karg);
if (KEYWORDP(val)) {
for (i = 0; i < count; i++)
if (symbols[i] == val)
break;
}
else if (QUOTEP(val) && SYMBOLP(val->data.quote)) {
Atom_id atom = ATOMID(val->data.quote);
for (i = 0; i < count; i++)
if (ATOMID(keys[i]) == atom)
break;
}
else
/* Just make the error test true */
i = count;
if (i == count)
goto invalid_keyword_label;
}
}
}
#endif
goto check_aux_label;
invalid_keyword_label:
{
/* If not in argument specification list... */
char function_name[36];
strcpy(function_name, STROBJ(name));
LispDestroy("%s: %s is an invalid keyword",
function_name, STROBJ(val));
}
}
check_aux_label:
if (*desc == 'a') {
/* &KEY uses all remaining arguments */
values = NIL;
goto aux_label;
}
goto finished_label;
/* &REST */
rest_label:
if (!CONSP(values)) {
if (builtin) {
BUILTIN_ARGUMENT(values);
}
else {
NORMAL_ARGUMENT(alist->rest, values);
}
values = NIL;
}
/* always allocate a new list, don't know if it will be retained */
else if (eval) {
LispObj *cons;
cons = CONS(EVAL(CAR(values)), NIL);
if (builtin) {
BUILTIN_ARGUMENT(cons);
}
else {
NORMAL_ARGUMENT(alist->rest, cons);
}
values = CDR(values);
for (; CONSP(values); values = CDR(values)) {
RPLACD(cons, CONS(EVAL(CAR(values)), NIL));
cons = CDR(cons);
}
}
else {
LispObj *cons;
cons = CONS(CAR(values), NIL);
if (builtin) {
BUILTIN_ARGUMENT(cons);
}
else {
NORMAL_ARGUMENT(alist->rest, cons);
}
values = CDR(values);
for (; CONSP(values); values = CDR(values)) {
RPLACD(cons, CONS(CAR(values), NIL));
cons = CDR(cons);
}
}
if (*desc != 'a')
goto finished_label;
/* &AUX */
aux_label:
i = 0;
count = alist->auxs.num_symbols;
defaults = alist->auxs.initials;
symbols = alist->auxs.symbols;
{
int lex = lisp__data.env.lex;
lisp__data.env.lex = base;
lisp__data.env.head = lisp__data.env.length;
for (; i < count; i++) {
NORMAL_ARGUMENT(symbols[i], EVAL(defaults[i]));
++lisp__data.env.head;
}
lisp__data.env.lex = lex;
}
done_label:
if (CONSP(values))
LispDestroy("%s: too many arguments", STROBJ(name));
finished_label:
if (builtin)
lisp__data.stack.base = base;
else {
lisp__data.env.head = lisp__data.env.length;
}
#undef BULTIN_ARGUMENT
#undef NORMAL_ARGUMENT
#undef BUILTIN_NO_EVAL_ARGUMENT
return (base);
}
LispObj *
LispFuncall(LispObj *function, LispObj *arguments, int eval)
{
LispAtom *atom;
LispArgList *alist;
LispBuiltin *builtin;
LispObj *lambda, *result;
int macro, base;
#ifdef DEBUGGER
if (lisp__data.debugging)
LispDebugger(LispDebugCallBegin, function, arguments);
#endif
switch (OBJECT_TYPE(function)) {
case LispFunction_t:
function = function->data.atom->object;
case LispAtom_t:
atom = function->data.atom;
if (atom->a_builtin) {
builtin = atom->property->fun.builtin;
if (eval)
eval = builtin->type != LispMacro;
base = LispMakeEnvironment(atom->property->alist,
arguments, function, eval, 1);
if (builtin->multiple_values) {
RETURN_COUNT = 0;
result = builtin->function(builtin);
}
else {
result = builtin->function(builtin);
RETURN_COUNT = 0;
}
lisp__data.stack.base = lisp__data.stack.length = base;
}
else if (atom->a_compiled) {
int lex = lisp__data.env.lex;
lambda = atom->property->fun.function;
alist = atom->property->alist;
base = LispMakeEnvironment(alist, arguments, function, eval, 0);
lisp__data.env.lex = base;
result = LispExecuteBytecode(lambda);
lisp__data.env.lex = lex;
lisp__data.env.head = lisp__data.env.length = base;
}
else if (atom->a_function) {
lambda = atom->property->fun.function;
macro = lambda->funtype == LispMacro;
alist = atom->property->alist;
lambda = lambda->data.lambda.code;
if (eval)
eval = !macro;
base = LispMakeEnvironment(alist, arguments, function, eval, 0);
result = LispRunFunMac(function, lambda, macro, base);
}
else if (atom->a_defstruct &&
atom->property->structure.function != STRUCT_NAME) {
LispObj cons;
if (atom->property->structure.function == STRUCT_CONSTRUCTOR)
atom = Omake_struct->data.atom;
else if (atom->property->structure.function == STRUCT_CHECK)
atom = Ostruct_type->data.atom;
else
atom = Ostruct_access->data.atom;
builtin = atom->property->fun.builtin;
cons.type = LispCons_t;
cons.data.cons.cdr = arguments;
if (eval) {
LispObj quote;
quote.type = LispQuote_t;
quote.data.quote = function;
cons.data.cons.car = "e;
base = LispMakeEnvironment(atom->property->alist,
&cons, function, 1, 1);
}
else {
cons.data.cons.car = function;
base = LispMakeEnvironment(atom->property->alist,
&cons, function, 0, 1);
}
result = builtin->function(builtin);
RETURN_COUNT = 0;
lisp__data.stack.length = base;
}
else {
LispDestroy("EVAL: the function %s is not defined",
STROBJ(function));
/*NOTREACHED*/
result = NIL;
}
break;
case LispLambda_t:
lambda = function->data.lambda.code;
alist = (LispArgList*)function->data.lambda.name->data.opaque.data;
base = LispMakeEnvironment(alist, arguments, function, eval, 0);
result = LispRunFunMac(function, lambda, 0, base);
break;
case LispCons_t:
if (CAR(function) == Olambda) {
function = EVAL(function);
if (LAMBDAP(function)) {
GC_ENTER();
GC_PROTECT(function);
lambda = function->data.lambda.code;
alist = (LispArgList*)function->data.lambda.name->data.opaque.data;
base = LispMakeEnvironment(alist, arguments, NIL, eval, 0);
result = LispRunFunMac(NIL, lambda, 0, base);
GC_LEAVE();
break;
}
}
default:
LispDestroy("EVAL: %s is invalid as a function",
STROBJ(function));
/*NOTREACHED*/
result = NIL;
break;
}
#ifdef DEBUGGER
if (lisp__data.debugging)
LispDebugger(LispDebugCallEnd, function, result);
#endif
return (result);
}
LispObj *
LispEval(LispObj *object)
{
LispObj *result;
switch (OBJECT_TYPE(object)) {
case LispAtom_t:
if ((result = LispDoGetVar(object)) == NULL)
LispDestroy("EVAL: the variable %s is unbound", STROBJ(object));
break;
case LispCons_t:
result = LispFuncall(CAR(object), CDR(object), 1);
break;
case LispQuote_t:
result = object->data.quote;
break;
case LispFunctionQuote_t:
result = object->data.quote;
if (SYMBOLP(result))
result = LispSymbolFunction(result);
else if (CONSP(result) && CAR(result) == Olambda)
result = EVAL(result);
else
LispDestroy("FUNCTION: %s is not a function", STROBJ(result));
break;
case LispBackquote_t:
result = LispEvalBackquote(object->data.quote, 1);
break;
case LispComma_t:
LispDestroy("EVAL: comma outside of backquote");
default:
result = object;
break;
}
return (result);
}
LispObj *
LispApply1(LispObj *function, LispObj *argument)
{
LispObj arguments;
arguments.type = LispCons_t;
arguments.data.cons.car = argument;
arguments.data.cons.cdr = NIL;
return (LispFuncall(function, &arguments, 0));
}
LispObj *
LispApply2(LispObj *function, LispObj *argument1, LispObj *argument2)
{
LispObj arguments, cdr;
arguments.type = cdr.type = LispCons_t;
arguments.data.cons.car = argument1;
arguments.data.cons.cdr = &cdr;
cdr.data.cons.car = argument2;
cdr.data.cons.cdr = NIL;
return (LispFuncall(function, &arguments, 0));
}
LispObj *
LispApply3(LispObj *function, LispObj *arg1, LispObj *arg2, LispObj *arg3)
{
LispObj arguments, car, cdr;
arguments.type = car.type = cdr.type = LispCons_t;
arguments.data.cons.car = arg1;
arguments.data.cons.cdr = &car;
car.data.cons.car = arg2;
car.data.cons.cdr = &cdr;
cdr.data.cons.car = arg3;
cdr.data.cons.cdr = NIL;
return (LispFuncall(function, &arguments, 0));
}
static LispObj *
LispRunFunMac(LispObj *name, LispObj *code, int macro, int base)
{
LispObj *result = NIL;
if (!macro) {
int lex = lisp__data.env.lex;
int did_jump = 1;
LispBlock *block;
block = LispBeginBlock(name, LispBlockClosure);
lisp__data.env.lex = base;
if (setjmp(block->jmp) == 0) {
for (; CONSP(code); code = CDR(code))
result = EVAL(CAR(code));
did_jump = 0;
}
LispEndBlock(block);
if (did_jump)
result = lisp__data.block.block_ret;
lisp__data.env.lex = lex;
lisp__data.env.head = lisp__data.env.length = base;
}
else {
GC_ENTER();
for (; CONSP(code); code = CDR(code))
result = EVAL(CAR(code));
/* FIXME this does not work if macro has &aux variables,
* but there are several other missing features, like
* destructuring and more lambda list keywords still missing.
* TODO later.
*/
lisp__data.env.head = lisp__data.env.length = base;
GC_PROTECT(result);
result = EVAL(result);
GC_LEAVE();
}
return (result);
}
LispObj *
LispRunSetf(LispArgList *alist, LispObj *setf, LispObj *place, LispObj *value)
{
GC_ENTER();
LispObj *store, *code, *expression, *result, quote;
int base;
code = setf->data.lambda.code;
store = setf->data.lambda.data;
quote.type = LispQuote_t;
quote.data.quote = value;
LispDoAddVar(CAR(store), "e);
++lisp__data.env.head;
base = LispMakeEnvironment(alist, place, Oexpand_setf_method, 0, 0);
/* build expansion macro */
expression = NIL;
for (; CONSP(code); code = CDR(code))
expression = EVAL(CAR(code));
/* Minus 1 to pop the added variable */
lisp__data.env.head = lisp__data.env.length = base - 1;
/* protect expansion, and executes it */
GC_PROTECT(expression);
result = EVAL(expression);
GC_LEAVE();
return (result);
}
LispObj *
LispRunSetfMacro(LispAtom *atom, LispObj *arguments, LispObj *value)
{
int base;
GC_ENTER();
LispObj *place, *body, *result, quote;
place = NIL;
base = LispMakeEnvironment(atom->property->alist,
arguments, atom->object, 0, 0);
body = atom->property->fun.function->data.lambda.code;
/* expand macro body */
for (; CONSP(body); body = CDR(body))
place = EVAL(CAR(body));
/* protect expansion */
GC_PROTECT(place);
/* restore environment */
lisp__data.env.head = lisp__data.env.length = base;
/* value is already evaluated */
quote.type = LispQuote_t;
quote.data.quote = value;
/* call setf again */
result = APPLY2(Osetf, place, "e);
GC_LEAVE();
return (result);
}
char *
LispStrObj(LispObj *object)
{
static int first = 1;
static char buffer[34];
static LispObj stream;
static LispString string;
if (first) {
stream.type = LispStream_t;
stream.data.stream.source.string = &string;
stream.data.stream.pathname = NIL;
stream.data.stream.type = LispStreamString;
stream.data.stream.readable = 0;
stream.data.stream.writable = 1;
string.string = buffer;
string.fixed = 1;
string.space = sizeof(buffer) - 1;
first = 0;
}
string.length = string.output = 0;
LispWriteObject(&stream, object);
/* make sure string is nul terminated */
string.string[string.length] = '\0';
if (string.length >= 32) {
if (buffer[0] == '(')
strcpy(buffer + 27, "...)");
else
strcpy(buffer + 28, "...");
}
return (buffer);
}
void
LispPrint(LispObj *object, LispObj *stream, int newline)
{
if (stream != NIL && !STREAMP(stream)) {
LispDestroy("PRINT: %s is not a stream", STROBJ(stream));
}
if (newline && LispGetColumn(stream))
LispWriteChar(stream, '\n');
LispWriteObject(stream, object);
if (stream == NIL || (stream->data.stream.type == LispStreamStandard &&
stream->data.stream.source.file == Stdout))
LispFflush(Stdout);
}
void
LispUpdateResults(LispObj *cod, LispObj *res)
{
LispSetVar(RUN[2], LispGetVar(RUN[1]));
LispSetVar(RUN[1], LispGetVar(RUN[0]));
LispSetVar(RUN[0], cod);
LispSetVar(RES[2], LispGetVar(RES[1]));
LispSetVar(RES[1], LispGetVar(RES[0]));
LispSetVar(RES[0], res);
}
#ifdef SIGNALRETURNSINT
int
#else
void
#endif
LispSignalHandler(int signum)
{
LispSignal(signum);
#ifdef SIGNALRETURNSINT
return (0);
#endif
}
void
LispSignal(int signum)
{
char *errstr;
char buffer[32];
if (lisp__disable_int) {
lisp__interrupted = signum;
return;
}
switch (signum) {
case SIGINT:
errstr = "interrupted";
break;
case SIGFPE:
errstr = "floating point exception";
break;
default:
sprintf(buffer, "signal %d received", signum);
errstr = buffer;
break;
}
LispDestroy(errstr);
}
void
LispDisableInterrupts(void)
{
++lisp__disable_int;
}
void
LispEnableInterrupts(void)
{
--lisp__disable_int;
if (lisp__disable_int <= 0 && lisp__interrupted)
LispSignal(lisp__interrupted);
}
void
LispMachine(void)
{
LispObj *cod, *obj;
lisp__data.sigint = signal(SIGINT, LispSignalHandler);
lisp__data.sigfpe = signal(SIGFPE, LispSignalHandler);
/*CONSTCOND*/
while (1) {
if (sigsetjmp(lisp__data.jmp, 1) == 0) {
lisp__data.running = 1;
if (lisp__data.interactive && lisp__data.prompt) {
LispFputs(Stdout, lisp__data.prompt);
LispFflush(Stdout);
}
if ((cod = LispRead()) != NULL) {
obj = EVAL(cod);
if (lisp__data.interactive) {
if (RETURN_COUNT >= 0)
LispPrint(obj, NIL, 1);
if (RETURN_COUNT > 0) {
int i;
for (i = 0; i < RETURN_COUNT; i++)
LispPrint(RETURN(i), NIL, 1);
}
LispUpdateResults(cod, obj);
if (LispGetColumn(NIL))
LispWriteChar(NIL, '\n');
}
}
LispTopLevel();
}
if (lisp__data.eof)
break;
}
signal(SIGINT, lisp__data.sigint);
signal(SIGFPE, lisp__data.sigfpe);
lisp__data.running = 0;
}
void *
LispExecute(char *str)
{
static LispObj stream;
static LispString string;
static int first = 1;
int running = lisp__data.running;
LispObj *result, *cod, *obj, **presult = &result;
if (str == NULL || *str == '\0')
return (NIL);
*presult = NIL;
if (first) {
stream.type = LispStream_t;
stream.data.stream.source.string = &string;
stream.data.stream.pathname = NIL;
stream.data.stream.type = LispStreamString;
stream.data.stream.readable = 1;
stream.data.stream.writable = 0;
string.output = 0;
first = 0;
}
string.string = str;
string.length = strlen(str);
string.input = 0;
LispPushInput(&stream);
if (!running) {
lisp__data.running = 1;
if (sigsetjmp(lisp__data.jmp, 1) != 0)
return (NULL);
}
cod = COD;
/*CONSTCOND*/
while (1) {
if ((obj = LispRead()) != NULL) {
result = EVAL(obj);
COD = cod;
}
if (lisp__data.eof)
break;
}
LispPopInput(&stream);
lisp__data.running = running;
return (result);
}
void
LispBegin(void)
{
int i;
LispAtom *atom;
char results[4];
LispObj *object, *path, *ext;
pagesize = LispGetPageSize();
segsize = pagesize / sizeof(LispObj);
/* Initialize memory management */
lisp__data.mem.mem = (void**)calloc(lisp__data.mem.space = 16,
sizeof(void*));
lisp__data.mem.index = lisp__data.mem.level = 0;
/* Allow LispGetVar to check ATOMID() of unbound symbols */
UNBOUND->data.atom = (LispAtom*)LispCalloc(1, sizeof(LispAtom));
LispMused(UNBOUND->data.atom);
noproperty.value = UNBOUND;
if (Stdin == NULL)
Stdin = LispFdopen(0, FILE_READ);
if (Stdout == NULL)
Stdout = LispFdopen(1, FILE_WRITE | FILE_BUFFERED);
if (Stderr == NULL)
Stderr = LispFdopen(2, FILE_WRITE);
/* minimum number of free cells after GC
* if sizeof(LispObj) == 16, than a minfree of 1024 would try to keep
* at least 16Kb of free cells.
*/
minfree = 1024;
MOD = COD = PRO = NIL;
#ifdef DEBUGGER
DBG = BRK = NIL;
#endif
/* allocate initial object cells */
LispAllocSeg(&objseg, minfree);
LispAllocSeg(&atomseg, pagesize);
lisp__data.gc.average = segsize;
/* Don't allow gc in initialization */
GCDisable();
/* Initialize package system, the current package is LISP. Order of
* initialization is very important here */
lisp__data.lisp = LispNewPackage(STRING("LISP"),
CONS(STRING("COMMON-LISP"), NIL));
/* Make LISP package the current one */
lisp__data.pack = lisp__data.savepack =
lisp__data.lisp->data.package.package;
/* Allocate space in LISP package */
LispMoreGlobals(lisp__data.pack);
/* Allocate space for multiple value return values */
lisp__data.returns.values = malloc(MULTIPLE_VALUES_LIMIT *
(sizeof(LispObj*)));
/* Create the first atom, do it "by hand" because macro "PACKAGE"
* cannot yet be used. */
atom = LispGetPermAtom("*PACKAGE*");
lisp__data.package = atomseg.freeobj;
atomseg.freeobj = CDR(atomseg.freeobj);
--atomseg.nfree;
lisp__data.package->type = LispAtom_t;
lisp__data.package->data.atom = atom;
atom->object = lisp__data.package;
atom->package = lisp__data.lisp;
/* Set package list, to be used by (gc) and (list-all-packages) */
PACK = CONS(lisp__data.lisp, NIL);
/* Make *PACKAGE* a special variable */
LispProclaimSpecial(lisp__data.package, lisp__data.lisp, NIL);
/* Value of macro "PACKAGE" is now properly available */
/* Changing *PACKAGE* is like calling (in-package) */
lisp__data.package->data.atom->watch = 1;
/* And available to other packages */
LispExportSymbol(lisp__data.package);
/* Initialize stacks */
LispMoreEnvironment();
LispMoreStack();
/* Create the KEYWORD package */
Skeyword = GETATOMID("KEYWORD");
object = LispNewPackage(STRING(Skeyword),
CONS(STRING(""), NIL));
/* Update list of packages */
PACK = CONS(object, PACK);
/* Allow easy access to the keyword package */
lisp__data.keyword = object;
lisp__data.key = object->data.package.package;
/* Initialize some static important symbols */
Olambda = STATIC_ATOM("LAMBDA");
LispExportSymbol(Olambda);
Okey = STATIC_ATOM("&KEY");
LispExportSymbol(Okey);
Orest = STATIC_ATOM("&REST");
LispExportSymbol(Orest);
Ooptional = STATIC_ATOM("&OPTIONAL");
LispExportSymbol(Ooptional);
Oaux = STATIC_ATOM("&AUX");
LispExportSymbol(Oaux);
Kunspecific = KEYWORD("UNSPECIFIC");
Oformat = STATIC_ATOM("FORMAT");
Oexpand_setf_method = STATIC_ATOM("EXPAND-SETF-METHOD");
Omake_struct = STATIC_ATOM("MAKE-STRUCT");
Ostruct_access = STATIC_ATOM("STRUCT-ACCESS");
Ostruct_store = STATIC_ATOM("STRUCT-STORE");
Ostruct_type = STATIC_ATOM("STRUCT-TYPE");
Smake_struct = ATOMID(Omake_struct);
Sstruct_access = ATOMID(Ostruct_access);
Sstruct_store = ATOMID(Ostruct_store);
Sstruct_type = ATOMID(Ostruct_type);
/* Initialize some static atom ids */
Snil = GETATOMID("NIL");
St = GETATOMID("T");
Saux = ATOMID(Oaux);
Skey = ATOMID(Okey);
Soptional = ATOMID(Ooptional);
Srest = ATOMID(Orest);
Sand = GETATOMID("AND");
Sor = GETATOMID("OR");
Snot = GETATOMID("NOT");
Satom = GETATOMID("ATOM");
Ssymbol = GETATOMID("SYMBOL");
Sinteger = GETATOMID("INTEGER");
Scharacter = GETATOMID("CHARACTER");
Sstring = GETATOMID("STRING");
Slist = GETATOMID("LIST");
Scons = GETATOMID("CONS");
Svector = GETATOMID("VECTOR");
Sarray = GETATOMID("ARRAY");
Sstruct = GETATOMID("STRUCT");
Sfunction = GETATOMID("FUNCTION");
Spathname = GETATOMID("PATHNAME");
Srational = GETATOMID("RATIONAL");
Sfloat = GETATOMID("FLOAT");
Scomplex = GETATOMID("COMPLEX");
Sopaque = GETATOMID("OPAQUE");
Sdefault = GETATOMID("DEFAULT");
LispArgList_t = LispRegisterOpaqueType("LispArgList*");
lisp__data.unget = malloc(sizeof(LispUngetInfo*));
lisp__data.unget[0] = calloc(1, sizeof(LispUngetInfo));
lisp__data.nunget = 1;
lisp__data.standard_input = ATOM2("*STANDARD-INPUT*");
SINPUT = STANDARDSTREAM(Stdin, lisp__data.standard_input, STREAM_READ);
lisp__data.interactive = 1;
LispProclaimSpecial(lisp__data.standard_input,
lisp__data.input_list = SINPUT, NIL);
LispExportSymbol(lisp__data.standard_input);
lisp__data.standard_output = ATOM2("*STANDARD-OUTPUT*");
SOUTPUT = STANDARDSTREAM(Stdout, lisp__data.standard_output, STREAM_WRITE);
LispProclaimSpecial(lisp__data.standard_output,
lisp__data.output_list = SOUTPUT, NIL);
LispExportSymbol(lisp__data.standard_output);
object = ATOM2("*STANDARD-ERROR*");
lisp__data.error_stream = STANDARDSTREAM(Stderr, object, STREAM_WRITE);
LispProclaimSpecial(object, lisp__data.error_stream, NIL);
LispExportSymbol(object);
lisp__data.modules = ATOM2("*MODULES*");
LispProclaimSpecial(lisp__data.modules, MOD, NIL);
LispExportSymbol(lisp__data.modules);
object = CONS(KEYWORD("UNIX"), CONS(KEYWORD("XEDIT"), NIL));
lisp__data.features = ATOM2("*FEATURES*");
LispProclaimSpecial(lisp__data.features, object, NIL);
LispExportSymbol(lisp__data.features);
object = ATOM2("MULTIPLE-VALUES-LIMIT");
LispDefconstant(object, FIXNUM(MULTIPLE_VALUES_LIMIT + 1), NIL);
LispExportSymbol(object);
/* Reenable gc */
GCEnable();
LispBytecodeInit();
LispPackageInit();
LispCoreInit();
LispMathInit();
LispPathnameInit();
LispStreamInit();
LispRegexInit();
LispWriteInit();
lisp__data.prompt = isatty(0) ? "> " : NULL;
lisp__data.errexit = !lisp__data.interactive;
if (lisp__data.interactive) {
/* add +, ++, +++, *, **, and *** */
for (i = 0; i < 3; i++) {
results[i] = '+';
results[i + 1] = '\0';
RUN[i] = ATOM(results);
LispSetVar(RUN[i], NIL);
LispExportSymbol(RUN[i]);
}
for (i = 0; i < 3; i++) {
results[i] = '*';
results[i + 1] = '\0';
RES[i] = ATOM(results);
LispSetVar(RES[i], NIL);
LispExportSymbol(RES[i]);
}
}
else
RUN[0] = RUN[1] = RUN[2] = RES[0] = RES[1] = RES[2] = NIL;
/* Add LISP builtin functions */
for (i = 0; i < sizeof(lispbuiltins) / sizeof(lispbuiltins[0]); i++)
LispAddBuiltinFunction(&lispbuiltins[i]);
EXECUTE("(require \"lisp\")");
object = ATOM2("*DEFAULT-PATHNAME-DEFAULTS*");
#ifdef LISPDIR
{
int length;
char *pathname = LISPDIR;
length = strlen(pathname);
if (length && pathname[length - 1] != '/') {
pathname = LispMalloc(length + 2);
strcpy(pathname, LISPDIR);
strcpy(pathname + length, "/");
path = LSTRING2(pathname, length + 1);
}
else
path = LSTRING(pathname, length);
}
#else
path = STRING("");
#endif
GCDisable();
LispProclaimSpecial(object, APPLY1(Oparse_namestring, path), NIL);
LispExportSymbol(object);
GCEnable();
/* Create and make EXT the current package */
PACKAGE = ext = LispNewPackage(STRING("EXT"), NIL);
lisp__data.pack = lisp__data.savepack = PACKAGE->data.package.package;
/* Update list of packages */
PACK = CONS(ext, PACK);
/* Import LISP external symbols in EXT package */
LispUsePackage(lisp__data.lisp);
/* Add EXT non standard builtin functions */
for (i = 0; i < sizeof(extbuiltins) / sizeof(extbuiltins[0]); i++)
LispAddBuiltinFunction(&extbuiltins[i]);
/* Create and make USER the current package */
GCDisable();
PACKAGE = LispNewPackage(STRING("USER"),
CONS(STRING("COMMON-LISP-USER"), NIL));
GCEnable();
lisp__data.pack = lisp__data.savepack = PACKAGE->data.package.package;
/* Update list of packages */
PACK = CONS(PACKAGE, PACK);
/* USER package inherits all LISP external symbols */
LispUsePackage(lisp__data.lisp);
/* And all EXT external symbols */
LispUsePackage(ext);
LispTopLevel();
}
void
LispEnd()
{
/* XXX needs to free all used memory, not just close file descriptors */
}
void
LispSetPrompt(char *prompt)
{
lisp__data.prompt = prompt;
}
void
LispSetInteractive(int interactive)
{
lisp__data.interactive = !!interactive;
}
void
LispSetExitOnError(int errexit)
{
lisp__data.errexit = !!errexit;
}
void
LispDebug(int enable)
{
lisp__data.debugging = !!enable;
#ifdef DEBUGGER
/* assumes we are at the toplevel */
DBG = BRK = NIL;
lisp__data.debug_level = -1;
lisp__data.debug_step = 0;
#endif
}
|