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
|
%
% Create Standard Lisp versions of the source files for CSL where I keep
% the master versions coded in RLISP.
%
% Since this is (probably) not done very often I build a version of
% RLISP specially for the occasion.
%
% Run this as
% csl -z lispfile.lsp
%
% or csl -z lispfile.lsp -D@cslbase="<directory for csl base>"
% (eg csl -z lispfile.lsp -D@cslbase="d:\acn\cslbase")
(spool "lispfile.log")
(load!-module 'compat)
% Note that if too much of the system has changed or been damaged it may
% be necessary to disable the compiler here (by commenting out the next
% couple of lines). This code should then run correctly but painfully
% slowly.
(load!-module 'compiler)
(setq !*comp t)
%
% If the files compat.fsl and compiler.fsl had been corrupted you
% could go
% (rdf "compat.lsp")
% here... then later on see (*)
%
(de symerr (u v)
(progn (terpri)
(print (list 'symerr u v))
(error 'failure)))
% Standard LISP equivalent of BOOT.RED.
(fluid '(!*blockp !*mode))
(global '(oldchan!*))
(global '(crchar!* cursym!* fname!* nxtsym!* ttype!* !$eol!$))
(put '!; 'switch!* '(nil !*semicol!*))
(put '!( 'switch!* '(nil !*lpar!*))
(put '!) 'switch!* '(nil !*rpar!*))
(put '!, 'switch!* '(nil !*comma!*))
(put '!. 'switch!* '(nil cons))
(put '!: 'switch!* '(((!= nil setq)) !*colon!*))
(put '!*comma!* 'infix 1)
(put 'setq 'infix 2)
(put 'cons 'infix 3)
(flag '(!*comma!*) 'nary)
(flag '(!*colon!* !*semicol!* end then else) 'delim)
(put 'begin 'stat 'blockstat)
(put 'if 'stat 'ifstat)
(put 'symbolic 'stat 'procstat)
(de begin2 nil
(prog nil
(setq cursym!* '!*semicol!*)
a (cond
((eq cursym!* 'end) (progn (rds oldchan!*) (return nil)))
(t (prin2 (errorset '(eval (form (xread nil))) t t)) ))
(go a)))
(de form (u) u)
(de xread (u) (progn (scan) (xread1 u)))
(de xread1 (u)
(prog (v w x y z z2)
a (setq z cursym!*)
a1 (cond
((or (null (atom z)) (numberp z)) (setq y nil))
((flagp z 'delim) (go end1))
((eq z '!*lpar!*) (go lparen))
((eq z '!*rpar!*) (go end1))
((setq y (get z 'infix)) (go infx))
((setq y (get z 'stat)) (go stat)))
a3 (setq w (cons z w))
next (setq z (scan))
(go a1)
lparen(setq y nil)
(cond
((eq (scan) '!*rpar!*)
(and w (setq w (cons (list (car w)) (cdr w)))) )
((eqcar (setq z (xread1 'paren)) '!*comma!*)
(setq w (cons (cons (car w) (cdr z)) (cdr w))))
(t (go a3)))
(go next)
infx (setq z2 (mkvar (car w) z))
un1 (setq w (cdr w))
(cond
((null w) (go un2))
(t (setq z2 (cons (car w) (list z2)))) )
(go un1)
un2 (setq v (cons z2 v))
preced(cond ((null x) (go pr4)) ((lessp y (car (car x))) (go pr2)))
pr1 (setq x (cons (cons y z) x))
(go next)
pr2 (setq v
(cons
(cond
((and (eqcar (car v) (cdar x)) (flagp (cdar x) 'nary))
(cons (cdar x) (cons (cadr v) (cdar v))))
(t (cons (cdar x) (list (cadr v) (car v)))) )
(cdr (cdr v))))
(setq x (cdr x))
(go preced)
stat (setq w (cons (eval (list y)) w))
(setq y nil)
(go a)
end1 (cond
((and (and (null v) (null w)) (null x)) (return nil))
(t (setq y 0)))
(go infx)
pr4 (cond ((null (equal y 0)) (go pr1)) (t (return (car v)))) ))
% (de eqcar (u v) (and (null (atom u)) (eq (car u) v)))
(de mksetq (u v) (list 'setq u v))
(de mkvar (u v) u)
(de rread nil
(prog (x)
(setq x (token))
(return
(cond
((and (equal ttype!* 3) (eq x '!()) (rrdls))
(t x)))) )
(de reverse2 (a b)
(prog nil
a (cond ((null a) (return b)))
(setq b (cons (car a) b))
(setq a (cdr a))))
(de rrdls nil
(prog (x r)
a (setq x (rread))
(cond
((null (equal ttype!* 3)) (go b))
((eq x '!)) (return (reversip r)))
((null (eq x '!.)) (go b)))
(setq x (rread))
(token)
(return (reverse2 r x))
b (setq r (cons x r))
(go a)))
(de token nil
(prog (x y)
(setq x crchar!*)
a (cond
((seprp x) (go sepr))
((digit x) (go number))
((liter x) (go letter))
((eq x '!%) (go coment))
((eq x '!!) (go escape))
((eq x '!') (go quote))
((eq x '!") (go string)))
(setq ttype!* 3)
(cond ((delcp x) (go d)))
(setq nxtsym!* x)
a1 (setq crchar!* (readch))
(go c)
escape(setq y (cons x y))
(setq x (readch))
letter(setq ttype!* 0)
let1 (setq y (cons x y))
(cond
((or (digit (setq x (readch))) (liter x)) (go let1))
((eq x '!!) (go escape)))
(setq nxtsym!* (intern (compress (reverse y))))
b (setq crchar!* x)
c (return nxtsym!*)
number(setq ttype!* 2)
num1 (setq y (cons x y))
(cond ((digit (setq x (readch))) (go num1)))
(setq nxtsym!* (compress (reverse y)))
(go b)
quote (setq crchar!* (readch))
(setq nxtsym!* (list 'quote (rread)))
(setq ttype!* 4)
(go c)
string(prog (raise)
(setq raise !*raise)
(setq !*raise nil)
strinx(setq y (cons x y))
(cond ((null (eq (setq x (readch)) '!")) (go strinx)))
(setq y (cons x y))
(setq nxtsym!* (mkstrng (compress (reverse y))))
(setq !*raise raise))
(setq ttype!* 1)
(go a1)
coment(cond ((null (eq (readch) !$eol!$)) (go coment)))
sepr (setq x (readch))
(go a)
d (setq nxtsym!* x)
(setq crchar!* '! )
(go c)))
(setq crchar!* '! )
(de delcp (u) (or (eq u '!;) (eq u '!$)))
(de mkstrng (u) u)
(de scan nil
(prog (x y)
(cond ((null (eq cursym!* '!*semicol!*)) (go b)))
a (setq nxtsym!* (token))
b (cond
((or (null (atom nxtsym!*)) (numberp nxtsym!*)) (go l))
((and (setq x (get nxtsym!* 'newnam)) (setq nxtsym!* x))
(go b))
((eq nxtsym!* 'comment) (go comm))
((and
(eq nxtsym!* '!')
(setq cursym!* (list 'quote (rread))))
(go l1))
((null (setq x (get nxtsym!* 'switch!*))) (go l))
((eq (cadr x) '!*semicol!*)
(return (setq cursym!* (cadr x)))) )
sw1 (setq nxtsym!* (token))
(cond
((or
(null (car x))
(null (setq y (assoc nxtsym!* (car x)))) )
(return (setq cursym!* (cadr x)))) )
(setq x (cdr y))
(go sw1)
comm (cond ((eq (readch) '!;) (setq crchar!* '! )) (t (go comm)))
(go a)
l (setq cursym!*
(cond
((null (eqcar nxtsym!* 'string)) nxtsym!*)
(t (cons 'quote (cdr nxtsym!*)))) )
l1 (setq nxtsym!* (token))
(return cursym!*)))
(de ifstat nil
(prog (condx condit)
a (setq condx (xread t))
(setq condit (nconc condit (list (list condx (xread t)))) )
(cond
((null (eq cursym!* 'else)) (go b))
((eq (scan) 'if) (go a))
(t (setq condit
(nconc condit (list (list t (xread1 t)))) )))
b (return (cons 'cond condit))))
(de procstat nil
(prog (x y)
(cond ((eq cursym!* 'symbolic) (scan)))
(cond
((eq cursym!* '!*semicol!*)
(return (null (setq !*mode 'symbolic)))) )
(setq fname!* (scan))
(cond ((atom (setq x (xread1 nil))) (setq x (list x))))
(setq y (xread nil))
(cond ((flagp (car x) 'lose) (return nil)))
(putd (car x) 'expr (list 'lambda (cdr x) y))
(setq fname!* nil)
(return (list 'quote (car x)))) )
(de blockstat nil
(prog (x hold varlis !*blockp)
a0 (setq !*blockp t)
(scan)
(cond
((null (or (eq cursym!* 'integer) (eq cursym!* 'scalar)))
(go a)))
(setq x (xread nil))
(setq varlis
(nconc
(cond ((eqcar x '!*comma!*) (cdr x)) (t (list x)))
varlis))
(go a0)
a (setq hold (nconc hold (list (xread1 nil))))
(setq x cursym!*)
(scan)
(cond ((not (eq x 'end)) (go a)))
(return (mkprog varlis hold))))
(de mkprog (u v) (cons 'prog (cons u v)))
(de gostat nil
(prog (x) (scan) (setq x (scan)) (scan) (return (list 'go x))))
(put 'go 'stat 'gostat)
(de rlis nil
(prog (x)
(setq x cursym!*)
(return (cond ((not (flagp (scan) 'delim))
(list x (list 'quote (list (xread1 t)))))
(t (list x))))))
(begin2)
%
% This file is "rlisp.red" taken from the REDUCE 3.3 sources, and can be
% used to reconstruct the CSL files compiler.lsp and ccomp.lsp from
% the associated RLISP source files. Note that REDUCE 3.3 is now a
% VERY old and out-of-date system, and the code here is only from the
% parser from it - no algebra is included.
%
% module module; % Support for module use.
% Author: Anthony C. Hearn.
% Copyright (c) 1987 The RAND Corporation. All rights reserved.
fluid '(!*mode);
global '(exportslist!* importslist!* module!-name!* old!-mode!*);
!*mode := 'symbolic; % initial value.
symbolic procedure exports u;
begin exportslist!* := union(u,exportslist!*); end;
symbolic procedure imports u;
begin importslist!* := union(u,importslist!*); end;
symbolic procedure module u;
%Sets up a module definition;
begin
if null module!-name!* then old!-mode!* := !*mode;
module!-name!* := car u . module!-name!*;
!*mode := 'symbolic
end;
symbolic procedure endmodule;
begin
if null module!-name!*
then rederr "ENDMODULE called outside module";
exportslist!* := nil;
importslist!* := nil;
module!-name!* := cdr module!-name!*;
if module!-name!* then return nil;
!*mode := old!-mode!*;
old!-mode!* := nil
end;
deflist('((exports rlis) (imports rlis) (module rlis)),'stat);
if null get('endmodule, 'stat) then put('endmodule,'stat,'rlis);
% For bootstrap only
flag('(endmodule),'go);
% endmodule;
module newtok; % Functions for introducing infix tokens to the system.
% Author: Anthony C. Hearn.
% Copyright (c) 1987 The RAND Corporation. All rights reserved.
fluid '(!*redeflg!*);
global '(!*msg preclis!*);
%Several operators in REDUCE are used in an infix form (e.g.,
%+,- ). The internal alphanumeric names associated with these
%operators are introduced by the function NEWTOK defined below.
%This association, and the precedence of each infix operator, is
%initialized in this section. We also associate printing characters
%with each internal alphanumeric name as well;
preclis!*:= '(or and not member memq equal neq eq geq greaterp leq
lessp freeof plus difference times quotient expt cons);
deflist ('(
(not not)
(plus plus)
(difference minus)
(minus minus)
(times times)
(quotient recip)
(recip recip)
), 'unary);
flag ('(and or !*comma!* plus times),'nary);
flag ('(cons setq plus times),'right);
deflist ('((minus plus) (recip times)),'alt);
symbolic procedure mkprec;
begin scalar x,y,z;
x := 'where . ('!*comma!* . ('setq . preclis!*));
y := 1;
a: if null x then return nil;
put(car x,'infix,y);
put(car x,'op,list list(y,y)); %for RPRINT;
if z := get(car x,'unary) then put(z,'infix,y);
if and(z,null flagp(z,'nary)) then put(z,'op,list(nil,y));
x := cdr x;
y := add1 y;
go to a
end;
mkprec();
symbolic procedure newtok u;
begin scalar !*redeflg!*,x,y;
if atom u or atom car u or null idp caar u
then typerr(u,"NEWTOK argument");
% set up SWITCH* property.
put(caar u,'switch!*,
cdr newtok1(car u,cadr u,get(caar u,'switch!*)));
% set up PRTCH property.
y := intern compress consescc car u;
if !*redeflg!* then lprim list(y,"redefined");
put(cadr u,'prtch,y);
if x := get(cadr u,'unary) then put(x,'prtch,y)
end;
symbolic procedure newtok1(charlist,name,propy);
if null propy then lstchr(charlist,name)
else if null cdr charlist
then begin
if cdr propy and !*msg then !*redeflg!* := t;
return list(car charlist,car propy,name)
end
else car charlist . newtok2(cdr charlist,name,car propy)
. cdr propy;
symbolic procedure newtok2(charlist,name,assoclist);
if null assoclist then list lstchr(charlist,name)
else if car charlist eq caar assoclist
then newtok1(charlist,name,cdar assoclist) . cdr assoclist
else car assoclist . newtok2(charlist,name,cdr assoclist);
symbolic procedure consescc u;
if null u then nil else '!! . car u . consescc cdr u;
symbolic procedure lstchr(u,v);
if null cdr u then list(car u,nil,v)
else list(car u,list lstchr(cdr u,v));
newtok '((!$) !*semicol!*);
newtok '((!;) !*semicol!*);
newtok '((!+) plus);
newtok '((!-) difference);
newtok '((!*) times);
newtok '((!^) expt);
newtok '((!* !*) expt);
newtok '((!/) quotient);
newtok '((!=) equal);
newtok '((!,) !*comma!*);
newtok '((!() !*lpar!*);
newtok '((!)) !*rpar!*);
newtok '((!:) !*colon!*);
newtok '((!: !=) setq);
newtok '((!.) cons);
newtok '((!<) lessp);
newtok '((!< !=) leq);
newtok '((!< !<) !*lsqb!*);
newtok '((!>) greaterp);
newtok '((!> !=) geq);
newtok '((!> !>) !*rsqb!*);
put('expt,'prtch,'!*!*); % To ensure that FORTRAN output is correct.
flag('(difference minus plus setq),'spaced);
flag('(newtok),'eval);
endmodule;
module support; % Basic functions needed to support RLISP and REDUCE.
% Author: Anthony C. Hearn.
% Copyright (c) 1987 The RAND Corporation. All rights reserved.
symbolic procedure aconc(u,v);
%adds element v to the tail of u. u is destroyed in process;
nconc(u,list v);
symbolic procedure arrayp u; get(u,'rtype) eq 'array;
symbolic procedure atsoc(u,v);
if null v then nil
else if u eq caar v then car v
else atsoc(u,cdr v);
symbolic procedure eqcar(u,v); null atom u and car u eq v;
symbolic procedure flagpcar(u,v);
null atom u and idp car u and flagp(car u,v);
symbolic procedure idlistp u;
% True if u is a list of id's.
null u or null atom u and idp car u and idlistp cdr u;
symbolic procedure mkprog(u,v); 'prog . (u . v);
symbolic procedure mkquote u; list('quote,u);
symbolic procedure mksetq(u,v); list('setq,u,v);
symbolic procedure pairvars(u,vars,mode);
% Sets up pairings of parameters and modes.
begin scalar x;
a: if null u then return append(reversip!* x,vars)
else if null idp car u then symerr("Invalid parameter",nil);
x := (car u . mode) . x;
u := cdr u;
go to a
end;
symbolic procedure prin2t u; progn(prin2 u, terpri(), u);
symbolic procedure reversip u;
begin scalar x,y;
a: if null u then return y;
x := cdr u; y := rplacd(u,y); u := x;
go to a
end;
symbolic procedure smemq(u,v);
%true if id U is a member of V at any level (excluding
%quoted expressions);
if atom v then u eq v
else if car v eq 'quote then nil
else smemq(u,car v) or smemq(u,cdr v);
symbolic procedure union(x,y);
if null x then y
else union(cdr x,if car x member y then y else car x . y);
symbolic procedure xn(u,v);
if null u then nil
else if car u member v then car u . xn(cdr u,delete(car u,v))
else xn(cdr u,v);
symbolic procedure u>=v; null(u<v);
symbolic procedure u<=v; null(u>v);
symbolic procedure u neq v; null(u=v);
symbolic procedure setdiff(u,v);
if null v then u else setdiff(delete(car v,u),cdr v);
% symbolic smacro procedure u>=v; null(u<v);
% symbolic smacro procedure u<=v; null(u>v);
% symbolic smacro procedure u neq v; null(u=v);
% List changing alternates (may also be defined as copying functions)
symbolic procedure aconc!*(u,v); nconc(u,list v); % append(u,list v);
symbolic procedure nconc!*(u,v); nconc(u,v); % append(u,v);
symbolic procedure reversip!* u; reversip u; % reverse u;
symbolic procedure rplaca!*(u,v); rplaca(u,v); % v . cdr u;
symbolic procedure rplacd!*(u,v); rplacd(u,v); % car u . v;
% The following functions should be provided in the compiler for
% efficient coding.
symbolic procedure apply1(u,v); apply(u,list v);
symbolic procedure apply2(u,v,w); apply(u,list(v,w));
symbolic procedure apply3(u,v,w,x); apply(u,list(v,w,x));
% The following function is needed by several modules. It is more
% REDUCE-specific than other functions in this module, but since it
% needs to be defined early on, it might as well go here.
symbolic procedure gettype u;
% Returns a REDUCE-related type for the expression U.
% It needs to be more table driven than the current definition.
if numberp u then 'number
else if null atom u or null u or null idp u then 'form
else if get(u,'simpfn) then 'operator
else if get(u,'avalue) then 'variable
else if getd u then 'procedure
else if globalp u then 'global
else if fluidp u then 'fluid
else if flagp(u,'parm) then 'parameter
else get(u,'rtype);
endmodule;
module slfns; % Complete list of Standard LISP functions.
% Author: Anthony C. Hearn.
global '(!*argnochk slfns!*);
slfns!* := '(
(abs 1)
(add1 1)
(append 2)
(apply 2)
(assoc 2)
(atom 1)
(car 1)
(cdr 1)
(caar 1)
(cadr 1)
(cdar 1)
(cddr 1)
(caaar 1)
(caadr 1)
(cadar 1)
(caddr 1)
(cdaar 1)
(cdadr 1)
(cddar 1)
(cdddr 1)
(caaaar 1)
(caaadr 1)
(caadar 1)
(caaddr 1)
(cadaar 1)
(cadadr 1)
(caddar 1)
(cadddr 1)
(cdaaar 1)
(cdaadr 1)
(cdadar 1)
(cdaddr 1)
(cddaar 1)
(cddadr 1)
(cdddar 1)
(cddddr 1)
(close 1)
(codep 1)
(compress 1)
(cons 2)
(constantp 1)
(de 3)
(deflist 2)
(delete 2)
% (DF 3) conflicts with algebraic operator DF
(difference 2)
(digit 1)
(divide 2)
(dm 3)
(dn 3)
(ds 3)
(eject 0)
(eq 2)
(eqn 2)
(equal 2)
(error 2)
(errorset 3)
(eval 1)
(evlis 1)
(expand 2)
(explode 1)
(expt 2)
(fix 1)
(fixp 1)
(flag 2)
(flagp 2)
(float 1)
(floatp 1)
(fluid 1)
(fluidp 1)
(function 1)
(gensym 0)
(get 2)
(getd 1)
(getv 2)
(global 1)
(globalp 1)
(go 1)
(greaterp 2)
(idp 1)
(intern 1)
(length 1)
(lessp 2)
(linelength 1)
(liter 1)
(lposn 0)
(map 2)
(mapc 2)
(mapcan 2)
(mapcar 2)
(mapcon 2)
(maplist 2)
(max2 2)
(member 2)
(memq 2)
(minus 1)
(minusp 1)
(min2 2)
(mkvect 1)
(nconc 2)
(not 1)
(null 1)
(numberp 1)
(onep 1)
(open 2)
(pagelength 1)
(pair 2)
(pairp 1)
(plus2 2)
(posn 0)
(print 1)
(prin1 1)
(prin2 1)
(prog2 2)
(put 3)
(putd 3)
(putv 3)
(quote 1)
(quotient 2)
(rds 1)
(read 0)
(readch 0)
(remainder 2)
(remd 1)
(remflag 2)
(remob 1)
(remprop 2)
(return 1)
(reverse 1)
(rplaca 2)
(rplacd 2)
(sassoc 3)
(set 2)
(setq 2)
(stringp 1)
(sublis 2)
(subst 3)
(sub1 1)
(terpri 0)
(times2 2)
(unfluid 1)
(upbv 1)
(vectorp 1)
(wrs 1)
(zerop 1)
);
if !*argnochk then deflist(slfns!*,'number!-of!-args);
endmodule;
module superv; % REDUCE supervisory functions.
% Author: Anthony C. Hearn.
% Modified by: Jed B. Marti.
% Copyright (c) 1987 The RAND Corporation. All rights reserved.
fluid '(!*backtrace
!*defn
!*errcont
!*int
!*mode
!*slin
!*time
dfprint!*
lreadfn!*
semic!*
tslin!*);
global '(!$eof!$
!*byeflag!*
!*demo
!*echo
!*extraecho
!*lessspace
!*micro!-version
!*nosave!*
!*output
!*pret
!*rlisp2
!*strind
!*struct
cloc!*
cmsg!*
crbuf!*
crbuflis!*
crbuf1!*
cursym!*
eof!*
erfg!*
ifl!*
ipl!*
initl!*
inputbuflis!*
key!*
ofl!*
opl!*
ogctime!*
otime!*
program!*
programl!*
promptexp!*
resultbuflis!*
st!*
statcounter
symchar!*
tok!*
ttype!*
ws);
!*output := t;
eof!* := 0;
initl!* := '(fname!* outl!*);
statcounter := 0;
% The true REDUCE supervisory function is BEGIN, again defined in the
% system dependent part of this program. However, most of the work is
% done by BEGIN1, which is called by BEGIN for every file encountered
% on input;
symbolic procedure errorp u;
%returns true if U is an ERRORSET error format;
atom u or cdr u;
symbolic procedure flagp!*!*(u,v); idp u and flagp(u,v);
symbolic procedure printprompt u;
%Prints the prompt expression for input;
progn(ofl!* and wrs nil, prin2 u, ofl!* and wrs cdr ofl!*);
symbolic procedure setcloc!*;
% Used to set for file input a global variable CLOC!* to dotted pair
% of file name and dotted pair of line and page being read.
% Currently a place holder for system specific function, since not
% supported in Standard LISP. CLOC!* is used in the INTER and RCREF
% modules.
cloc!* := if null ifl!* then nil else car ifl!* . nil;
symbolic procedure command;
begin scalar x;
if !*demo and (x := ifl!*)
then progn(terpri(),rds nil,readch(),rds cadr x);
if null !*slin
then if !*rlisp2
then progn(s!&(),
key!* := tok!*,
m!-metarlisp(),
(if st!* then x := car st!* else x := nil),
st!* := nil)
else progn(scan(), setcloc!*(), key!* := cursym!*,
x := xread1 nil)
else progn(key!* := (semic!* := '!;),
setcloc!*(),
x := (if lreadfn!* then apply(lreadfn!*,nil)
else read()),
if key!* eq '!;
then key!* := if atom x then x else car x);
if !*struct then x := structchk x;
if !*pret then progn(terpri(),rprint x);
if null !*slin then x := form x;
return x
end;
symbolic procedure begin1;
begin scalar mode,parserr,result,x;
if !*rlisp2 then prolog 'm!-metarlisp;
otime!* := time();
% the next line is that way for bootstrapping purposes.
if getd 'gctime then ogctime!* := gctime() else ogctime!* := 0;
a0: cursym!* := '!*semicol!*;
a: if null terminalp() or !*nosave!* then go to b
else if statcounter>0 then add2buflis();
statcounter := statcounter + 1;
crbuf1!* := nil; % For input string editor.
!*strind := 0; % Used by some versions of input editor.
promptexp!* :=
compress('!! . append(explode statcounter,
explode if null symchar!* or !*mode eq 'algebraic
then '!:! else '!*! ));
setpchar promptexp!*;
b: parserr := nil;
!*nosave!* := nil;
if !*time then eval '(showtime); %Since a STAT;
if !*output and null ofl!* and terminalp() and null !*defn
and null !*lessspace
then terpri();
if tslin!*
then progn(!*slin := car tslin!*,
lreadfn!* := cdr tslin!*,
tslin!* := nil);
mapcar(initl!*,function sinitl);
if !*int then erfg!* := nil; %to make editing work properly;
if null !*rlisp2 and cursym!* eq 'end
then progn(comm1 'end, return nil)
else if terminalp() and (!*rlisp2 or null(key!* eq 'ed))
then printprompt promptexp!*;
program!* := errorset('(command),t,!*backtrace);
if !*rlisp2
then if tok!* eq '!*semic!* then semic!* := '!;
else semic!* := '!$;
condterpri();
if errorp program!* then go to err1;
program!* := car program!*;
if eofcheck() then go to c else eof!* := 0;
if !*rlisp2 then if program!* = '(end) then return nil else nil
else if cursym!* eq 'end
then if !*micro!-version and terminalp() then go to a0
else progn(comm1 'end, return nil)
else if eqcar(program!*,'retry) then program!* := programl!*;
%The following section decides what the target mode should be.
%That mode is also assumed to be the printing mode;
if flagp!*!*(key!*,'modefn) then mode := key!*
else if null atom program!* % and null !*micro!-version
and null(car program!* eq 'quote)
and (null(idp car program!*
and (flagp(car program!*,'nochange)
or flagp(car program!*,'intfn)
or car program!* eq 'list))
or car program!* memq '(setq setel setf)
and eqcar(caddr program!*,'quote))
then mode := 'symbolic
else if key!* eq 'input
and (x := rassoc!*(program!*,inputbuflis!*))
then mode := cddr x
else mode := !*mode;
program!* := convertmode1(program!*,nil,'symbolic,mode);
add2inputbuf(program!*,!*mode);
% This used to be MODE, but then ED n wouldn't work.
if null !*rlisp2 and null atom program!*
and car program!* memq '(bye quit)
then if getd 'bye then progn(eval program!*, go to b)
else progn(!*byeflag!* := t, return nil)
else if null !*rlisp2 and eqcar(program!*,'ed)
then progn((if getd 'cedit and terminalp()
then cedit cdr program!*
else lprim "ED not supported"),
go to b)
else if !*defn
then if erfg!* then go to a
else if null flagp!*!*(key!*,'ignore)
and null eqcar(program!*,'quote)
then go to d;
b1: if !*output and ifl!* and !*echo and null !*lessspace
then terpri();
result := errorset((if mode eq 'symbolic then program!*
else list('assgneval,mkquote program!*)),
t,!*backtrace);
if errorp result or erfg!*
then progn(programl!* := program!*,go to err2)
else if !*defn then go to a;
if null(mode eq 'symbolic)
then progn(program!* := cdar result,
result := list caar result);
add2resultbuf(car result,mode);
if null !*output then go to a
else if (null !*rlisp2 and semic!* eq '!;)
or (!*rlisp2 and tok!* eq '!*semic!*)
then if mode eq 'symbolic
then if null car result and null(!*mode eq 'symbolic)
then nil
else begin
terpri();
result := errorset(list('print,mkquote car result),
t,!*backtrace)
end
else if car result
then result := errorset(list('varpri,mkquote car result,
mkquote program!*,
mkquote 'only),
t,!*backtrace);
if errorp result then go to err3 else go to a;
c: if crbuf1!* then
progn(lprim "Closing object improperly removed. Redo edit.",
crbuf1!* := nil, go to a)
else if eof!*>4
then progn(lprim "End-of-file read", return eval '(bye))
else if terminalp() then progn(crbuf!* := nil, go to b)
else return nil;
d: if program!* then dfprint program!*;
if null flagp!*!*(key!*,'eval) then go to a else go to b1;
err1:
if eofcheck() or eof!*>0 then go to c
else if program!*="BEGIN invalid" then go to a;
parserr := t;
err2:
resetparser(); %in case parser needs to be modified;
err3:
erfg!* := t;
if null !*int and null !*errcont
then progn(!*defn := t,
!*echo := t,
(if null cmsg!*
then lprie "Continuing with parsing only ..."),
cmsg!* := t)
else if null !*errcont
then progn(result := pause1 parserr,
(if result then return null eval result),
erfg!* := nil)
else erfg!* := nil;
go to a
end;
flag ('(deflist flag fluid global remflag remprop unfluid),'eval);
symbolic procedure assgneval u;
% Evaluate (possible) assignment statements and return results in a
% form that allows required printing of such assignments.
begin scalar x,y;
a: if atom u then go to b
else if car u eq 'setq then x := ('setq . cadr u) . x
else if car u eq 'setel
then x := ('setel . mkquote eval cadr u) . x
else if car u eq 'setk
then x := ('setk . mkquote if atom (y := eval cadr u) then y
else car y . revlis cdr y) . x
else go to b;
u := caddr u;
go to a;
b: u := mkquote eval u;
c: if null x then return(eval u . u);
u := list(caar x,cdar x,u);
x := cdr x;
go to c
end;
symbolic procedure rassoc!*(u,v);
% Finds term in which U is the first term in the right part of a term
% in the association list V, or NIL if term is not found;
if null v then nil
else if u = cadar v then car v
else rassoc!*(u,cdr v);
symbolic procedure close!-input!-files;
% Close all input files currently open;
begin
if ifl!* then progn(rds nil,ifl!* := nil);
aa: if null ipl!* then return nil;
close cdar ipl!*;
ipl!* := cdr ipl!*;
go to aa
end;
symbolic procedure close!-output!-files;
% Close all output files currently open;
begin
if ofl!* then progn(wrs nil,ofl!* := nil);
aa: if null opl!* then return nil;
close cdar opl!*;
opl!* := cdr opl!*;
go to aa
end;
symbolic procedure add2buflis;
begin
if null crbuf!* then return nil;
crbuf!* := reversip crbuf!*; %put in right order;
a: if crbuf!* and seprp car crbuf!*
then progn(crbuf!* := cdr crbuf!*, go to a);
crbuflis!* := (statcounter . crbuf!*) . crbuflis!*;
crbuf!* := nil
end;
symbolic procedure add2inputbuf(u,mode);
begin
if null terminalp() or !*nosave!* then return nil;
inputbuflis!* := (statcounter . u . mode) . inputbuflis!*
end;
symbolic procedure add2resultbuf(u,mode);
begin
if mode eq 'symbolic or null u or !*nosave!* then return nil;
ws := u;
if terminalp()
then resultbuflis!* := (statcounter . u) . resultbuflis!*
end;
symbolic procedure condterpri;
!*output and !*echo and !*extraecho and (null !*int or ifl!*)
and null !*defn and terpri();
symbolic procedure eofcheck;
% true if an end-of-file has been read in current input sequence;
program!* eq !$eof!$ and ttype!*=3 and (eof!* := eof!*+1);
symbolic procedure resetparser;
%resets the parser after an error;
if null !*slin then comm1 t;
symbolic procedure terminalp;
%true if input is coming from an interactive terminal;
!*int and null ifl!*;
symbolic procedure dfprint u;
%Looks for special action on a form, otherwise prettyprints it;
if dfprint!* then apply(dfprint!*,list u)
else if cmsg!* then nil
else if null eqcar(u,'progn) then prettyprint u
else begin
a: u := cdr u;
if null u then return nil;
dfprint car u;
go to a
end;
symbolic procedure showtime;
begin scalar x,y;
x := otime!*;
otime!* := time();
x := otime!*-x;
y := ogctime!*;
ogctime!* := gctime();
y := ogctime!* - y;
x := x - y;
terpri();
prin2 "Time: "; prin2 x; prin2 " ms";
if y = 0 then return terpri();
prin2 " plus GC time: "; prin2 y; prin2 " ms"
end;
symbolic procedure sinitl u;
set(u,get(u,'initl));
endmodule;
module tok; % Identifier and reserved character reading.
% Author: Anthony C. Hearn.
% Copyright (c) 1987 The RAND Corporation. All rights reserved.
fluid '(semic!*);
global '(!$eof!$
!$eol!$
!*quotenewnam
!*raise
!*lower
crbuf!*
crbuf1!*
crchar!*
curline!*
cursym!*
eof!*
ifl!*
nxtsym!*
outl!*
ttype!*);
!*quotenewnam := t;
crchar!* := '! ;
curline!* := 1;
% The function TOKEN defined below is used for reading identifiers
% and reserved characters (such as parentheses and infix operators).
% It is called by the function SCAN, which translates reserved
% characters into their internal name, and sets up the output of the
% input line. The following definitions of TOKEN and SCAN are quite
% general, but also inefficient. The reading process can often be
% speeded up considerably if these functions (especially token) are
% written in terms of the explicit LISP used.
symbolic procedure prin2x u;
outl!* := u . outl!*;
symbolic procedure mkstrng u;
%converts the uninterned id U into a string;
%if strings are not constants, this should be replaced by
%list('string,u);
u;
symbolic procedure readch1;
begin scalar x;
if null terminalp()
then progn(x := readch(),
x eq !$eol!$ and (curline!* := curline!*+1),
return x)
else if crbuf1!*
then begin x := car crbuf1!*; crbuf1!* := cdr crbuf1!* end
else x := readch();
crbuf!* := x . crbuf!*;
return x
end;
symbolic procedure token1;
begin scalar x,y,z;
x := crchar!*;
a: if seprp x then progn(x := readch1(), go to a)
else if digit x then go to number
else if liter x or x eq '!_ then go to letter
else if x eq '!% then go to coment
else if x eq '!! then go to escape
else if x eq '!'
then progn(crchar!* := readch1(),
nxtsym!* := mkquote rread(),
ttype!* := 4,
return nxtsym!*)
else if x eq '!" then go to string;
ttype!* := 3;
if x eq !$eof!$ then prog2(crchar!* := '! ,filenderr());
nxtsym!* := x;
a1: if delcp x then crchar!*:= '! else crchar!*:= readch1();
go to c;
escape:
begin scalar raise, !*lower;
raise := !*raise;
!*raise := !*lower := nil;
y := x . y;
x := readch1();
!*raise := raise
end;
letter:
ttype!* := 0;
let1:
y := x . y;
if digit (x := readch1()) or liter x or x eq '!_ then go to let1
else if x eq '!! then go to escape;
nxtsym!* := intern compress reversip!* y;
b: crchar!* := x;
c: return nxtsym!*;
number:
ttype!* := 2;
num1:
y := x . y;
z := x;
if digit (x := readch1())
or x eq '!.
or x eq 'e
or z eq 'e
then go to num1;
nxtsym!* := compress reversip!* y;
go to b;
string:
begin scalar raise, !*lower;
raise := !*raise;
!*raise := !*lower := nil;
strinx:
y := x . y;
if null((x := readch1()) eq '!") then go to strinx;
y := x . y;
nxtsym!* := mkstrng compress reversip!* y;
!*raise := raise
end;
ttype!* := 1;
go to a1;
coment:
if null(readch1() eq !$eol!$) then go to coment;
x := readch1();
go to a
end;
symbolic procedure token;
%This provides a hook for a faster TOKEN;
token1();
symbolic procedure filenderr;
begin
eof!* := eof!*+1;
if terminalp() then error1()
else error(99,if ifl!*
then list("End-of-file read in file",car ifl!*)
else "End-of-file read")
end;
symbolic procedure ptoken;
begin scalar x;
x := token();
if x eq '!) and eqcar(outl!*,'! ) then outl!*:= cdr outl!*;
%an explicit reference to OUTL!* used here;
prin2x x;
if null ((x eq '!() or (x eq '!))) then prin2x '! ;
return x
end;
symbolic procedure rread1;
% Modified to use QUOTENEWNAM's for ids.
begin scalar x,y;
x := ptoken();
if null (ttype!*=3)
then return if null idp x
or null !*quotenewnam
or null(y := get(x,'quotenewnam))
then x
else y
else if x eq '!( then return rrdls()
else if null (x eq '!+ or x eq '!-) then return x;
y := ptoken();
if null numberp y
then progn(nxtsym!* := " ",
symerr("Syntax error: improper number",nil))
else if x eq '!- then y := apply('minus,list y);
%we need this construct for bootstrapping purposes;
return y
end;
symbolic procedure rrdls;
begin scalar x,y,z;
a: x := rread1();
if null (ttype!*=3) then go to b
else if x eq '!) then return z
else if null (x eq '!.) then go to b;
x := rread1();
y := ptoken();
if null (ttype!*=3) or null (y eq '!))
then progn(nxtsym!* := " ",symerr("Invalid S-expression",nil))
else return nconc(z,x);
b: z := nconc(z,list x);
go to a
end;
symbolic procedure rread;
progn(prin2x " '",rread1());
%--- symbolic procedure scan;
%--- begin scalar x,y;
%--- if null (cursym!* eq '!*semicol!*) then go to b;
%--- a: nxtsym!* := token();
%--- b: if null atom nxtsym!* then go to q1
%--- else if nxtsym!* eq 'else or cursym!* eq '!*semicol!*
%--- then outl!* := nil;
%--- prin2x nxtsym!*;
%--- c: if null idp nxtsym!* then go to l
%--- else if (x:=get(nxtsym!*,'newnam)) and
%--- (null (x=nxtsym!*)) then go to new
%--- else if nxtsym!* eq 'comment OR NXTSYM!* EQ '!% AND TTYPE!*=3
%--- THEN GO TO COMM
%--- ELSE IF NULL(TTYPE!* = 3) THEN GO TO L
%--- ELSE IF NXTSYM!* EQ !$eof!$ then return filenderr()
%--- else if nxtsym!* eq '!' then go to quote
%--- else if null (x:= get(nxtsym!*,'switch!*)) then go to l
%--- else if eqcar(cdr x,'!*semicol!*) then go to delim;
%--- sw1: nxtsym!* := token();
%--- if null(ttype!* = 3) then go to sw2
%--- else if nxtsym!* eq !$eof!$ then return filenderr()
%--- else if car x then go to sw3;
%--- sw2: cursym!*:=cadr x;
%--- if cursym!* eq '!*rpar!* then go to l2
%--- else return cursym!*;
%--- sw3: if null (y:= atsoc(nxtsym!*,car x)) then go to sw2;
%--- prin2x nxtsym!*;
%--- x := cdr y;
%--- go to sw1;
%--- comm: if delcp crchar!* then go to com1;
%--- crchar!* := readch();
%--- go to comm;
%--- com1: crchar!* := '! ;
%--- condterpri();
%--- go to a;
%--- delim:
%--- semic!*:=nxtsym!*;
%--- return (cursym!*:='!*semicol!*);
%--- new: nxtsym!* := x;
%--- if stringp x then go to l
%--- else if atom x then go to c
%--- else go to l;
%--- quote:
%--- nxtsym!* := mkquote rread1();
%--- go to l;
%--- q1: if null (car nxtsym!* eq 'string) then go to l;
%--- prin2x " ";
%--- prin2x cadr(nxtsym!* := mkquote cadr nxtsym!*);
%--- l: cursym!*:=nxtsym!*;
%--- l1: nxtsym!* := token();
%--- if nxtsym!* eq !$eof!$ and ttype!* = 3 then return filenderr();
%--- l2: if numberp nxtsym!*
%--- or (atom nxtsym!* and null get(nxtsym!*,'switch!*))
%--- then prin2x " ";
%--- return cursym!*
%--- end;
global '(!*eoldelimp comment!*);
symbolic procedure scan;
begin scalar bool,x,y;
if null (cursym!* eq '!*semicol!*) then go to b;
a: nxtsym!* := token();
b: if null atom nxtsym!* then go to q1
else if nxtsym!* eq 'else or cursym!* eq '!*semicol!*
then outl!* := nil;
prin2x nxtsym!*;
c: if null idp nxtsym!* then go to l
else if (x:=get(nxtsym!*,'newnam)) and
(null (x=nxtsym!*)) then go to new
else if nxtsym!* eq 'Comment then go to comm
else if nxtsym!* eq '!#if then go to conditional
else if nxtsym!* eq '!#else then progn(nxtsym!* := x := nil,
go to skipping)
else if nxtsym!* eq '!#endif then go to a
else if nxtsym!* eq '!% and ttype!*=3
% then progn(prin2t "****** Tell Hearn you got to SCAN comment",
% go to comm)
then go to comm
else if null(ttype!* = 3) then go to l
else if nxtsym!* eq !$eof!$ then return filenderr()
else if nxtsym!* eq '!' then rederr "Invalid QUOTE"
else if !*eoldelimp and nxtsym!* eq !$eol!$ then go to delim
else if null (x:= get(nxtsym!*,'switch!*)) then go to l
else if eqcar(cdr x,'!*semicol!*) then go to delim;
bool := seprp crchar!*;
sw1: nxtsym!* := token();
if null(ttype!* = 3) then go to sw2
else if nxtsym!* eq !$eof!$ then return filenderr()
else if car x then go to sw3;
sw2: cursym!*:=cadr x;
bool := nil;
if cursym!* eq '!*rpar!* then go to l2 else return cursym!*;
sw3: if bool or null (y:= atsoc(nxtsym!*,car x)) then go to sw2;
prin2x nxtsym!*;
x := cdr y;
if null car x and cadr x eq '!*Comment!*
then progn(comment!* := read!-comment(),go to a);
go to sw1;
conditional:
% The conditional expression used here must be written in Lisp form
x := errorset(rread(), !*backtrace, nil);
% errors in evaluation count as NIL
if null errorp x and car x then go to a;
x := nil;
skipping:
% I support nesting of conditional inclusion.
if nxtsym!* eq '!#endif then
if null x then go to a else x := cdr x
else if nxtsym!* eq '!#if then x := nil . x
else if (nxtsym!* eq '!#else) and null x then go to a;
nxtsym!* := token();
go to skipping;
comm: if delcp crchar!* and null(crchar!* eq !$eol!$)
then progn(crchar!* := '! , condterpri(), go to a);
crchar!* := readch();
go to comm;
delim:
semic!*:=nxtsym!*;
return (cursym!*:='!*semicol!*);
new: nxtsym!* := x;
if stringp x then go to l
else if atom x then go to c
else go to l;
q1: if null (car nxtsym!* eq 'string) then go to l;
prin2x " ";
prin2x cadr(nxtsym!* := mkquote cadr nxtsym!*);
l: cursym!*:=nxtsym!*;
nxtsym!* := token();
if nxtsym!* eq !$eof!$ and ttype!* = 3 then return filenderr();
l2: if numberp nxtsym!*
or (atom nxtsym!* and null get(nxtsym!*,'switch!*))
then prin2x " ";
return cursym!*
end;
endmodule;
module xread; % Routines for parsing REDUCE input.
% Author: Anthony C. Hearn.
% Copyright (c) 1987 The RAND Corporation. All rights reserved.
fluid '(!*blockp);
global '(cursym!* nxtsym!*);
% The conversion of a REDUCE expression to LISP prefix form is carried
% out by the function XREAD. This function initiates the scanning
% process, and then calls the auxiliary function XREAD1 to perform the
% actual parsing. Both XREAD and XREAD1 are used by many functions
% whenever an expression must be read;
flag ('(end !*colon!* !*semicol!*),'delim);
symbolic procedure chknewnam u;
% Check to see if U has a newnam, and return it else return U.
begin scalar x;
return if null(x := get(u,'newnam)) or x eq u then u
else if idp x then chknewnam x
else x
end;
symbolic procedure mkvar(u,v); u;
symbolic procedure remcomma u;
if eqcar(u,'!*comma!*) then cdr u else list u;
symbolic procedure xread1 u;
begin scalar v,w,x,y,z,z1,z2;
% v: expression being built
% w: prefix operator stack
% x: infix operator stack
% y: infix value or stat property
% z: current symbol
% z1: next symbol
% z2: temporary storage;
a: z := cursym!*;
a1: if null idp z then nil
else if z eq '!*lpar!* then go to lparen
else if z eq '!*rpar!* then go to rparen
else if y := get(z,'infix) then go to infx
% The next line now commented out was intended to allow a STAT
% to be used as a label. However, it prevents the definition of
% a diphthong whose first character is a colon.
% else if nxtsym!* eq '!: then nil
else if flagp(z,'delim) then go to delimit
else if y := get(z,'stat) then go to stat;
a2: y := nil;
a3: w := z . w;
if numberp z
and idp (z1 := chknewnam nxtsym!*)
and null flagp(z1,'delim)
and null(get(z1,'switch!*) and null(z1 eq '!())
and null get(z1,'infix)
then progn(cursym!* := 'times, go to a);
% allow for implicit * after a number.
next: z := scan();
go to a1;
lparen:
y := nil;
if scan() eq '!*rpar!* then go to lp1 % no args
else if flagpcar(w,'struct) then z := xread1 car w
else z := xread1 'paren;
if flagp(u,'struct) then progn(z := remcomma z, go to a3)
else if null eqcar(z,'!*comma!*) then go to a3
else if null w
then (if u eq 'lambda then go to a3
else symerr("Improper delimiter",nil))
else w := (car w . cdr z) . cdr w;
go to next;
lp1: if w then w := list car w . cdr w; %function of no args;
go to next;
rparen:
if null u or u eq 'group or u eq 'proc
then symerr("Too many right parentheses",nil)
else go to end1;
infx: if z eq '!*comma!* or null atom (z1 := scan())
or numberp z1 then go to in1
else if z1 eq '!*rpar!*%infix operator used as variable;
or z1 eq '!*comma!*
or flagp(z1,'delim)
then go to in2
else if z1 eq '!*lpar!*%infix operator in prefix position;
and null atom(z1 := xread 'paren)
and car z1 eq '!*comma!*
and (z := z . cdr z1)
then go to a1;
in1: if w then go to unwind
else if null(z := get(z,'unary))
then symerr("Redundant operator",nil);
v := '!*!*un!*!* . v;
go to pr1;
in2: y := nil;
w := z . w;
in3: z := z1;
go to a1;
unwind:
z2 := mkvar(car w,z);
un1: w:= cdr w;
if null w then go to un2
else if numberp car w then symerr("Missing operator",nil);
z2 := list(car w,z2);
go to un1;
un2: v:= z2 . v;
preced:
if null x then if y=0 then go to end2 else nil
else if y<caar x
or (y=caar x
and ((z eq cdar x and null flagp(z,'nary)
and null flagp(z,'right))
or get(cdar x,'alt)))
then go to pr2;
pr1: x:= (y . z) . x;
if null(z eq '!*comma!*) then go to in3
else if cdr x or null u or u memq '(lambda paren)
or flagp(u,'struct)
then go to next
else go to end2;
pr2: %if cdar x eq 'setq then go to assign else;
if cadr v eq '!*!*un!*!*
then (if car v eq '!*!*un!*!* then go to pr1
else z2 := list(cdar x,car v))
else z2 := cdar x .
if eqcar(car v,cdar x) and flagp(cdar x,'nary)
then (cadr v . cdar v)
else list(cadr v,car v);
x:= cdr x;
v := z2 . cddr v;
go to preced;
stat: if null(flagp(z,'go)
or null(u eq 'proc) and (flagp(y,'endstat)
or (null delcp nxtsym!* and null (nxtsym!* eq '!,))))
then go to a2;
w := apply(y,nil) . w;
y := nil;
go to a;
delimit:
if z eq '!*colon!* and null(u eq 'for)
and (null !*blockp or null w or null atom car w or cdr w)
or flagp(z,'nodel)
and (null u
or u eq 'group and null (z memq '(!*rsqb!* !*rcbkt!*)))
then symerr("Improper delimiter",nil)
else if idp u and (u eq 'paren or flagp(u,'struct))
then symerr("Too few right parentheses",nil);
end1: if y then symerr("Improper delimiter",nil)
else if null v and null w and null x then return nil;
y := 0;
go to unwind;
end2: if null cdr v then return car v
else symerr("Improper delimiter",nil)
end;
%symbolic procedure getels u;
% getel(car u . !*evlis cdr u);
%symbolic procedure !*evlis u;
% mapcar(u,function eval);
flag ('(endstat retstat),'endstat);
flag ('(else until),'nodel);
flag ('(begin),'go);
symbolic procedure xread u;
progn(scan(),xread1 u);
flag('(xread),'opfn); %to make it an operator;
endmodule;
module lpri; % Functions for printing diagnostic and error messages.
% Author: Anthony C. Hearn.
% Copyright (c) 1987 The RAND Corporation. All rights reserved.
fluid '(!*defn !*int);
global '(!*echo !*fort !*msg !*nat !*rlisp2 cursym!* erfg!* ofl!*
outl!*);
symbolic procedure lpri u;
begin
a: if null u then return nil;
prin2 car u;
prin2 " ";
u := cdr u;
go to a
end;
symbolic procedure lpriw (u,v);
begin scalar x;
u := u . if v and atom v then list v else v;
if ofl!* and (!*fort or not !*nat or !*defn) then go to c;
terpri();
a: lpri u;
terpri();
if null x then go to b;
wrs cdr x;
return nil;
b: if null ofl!* then return nil;
c: x := ofl!*;
wrs nil;
go to a
end;
symbolic procedure lprim u;
!*msg and lpriw("***",u);
symbolic procedure lprie u;
begin scalar x;
if !*int then go to a;
x:= !*defn;
!*defn := nil;
a: erfg!* := t;
lpriw ("*****",u);
if null !*int then !*defn := x
end;
symbolic procedure printty u;
begin scalar ofl;
if null !*fort and !*nat then print u;
if null ofl!* then return nil;
ofl := ofl!*;
wrs nil;
print u;
wrs cdr ofl
end;
symbolic procedure rederr u;
begin lprie u; error1() end;
symbolic procedure symerr(u,v);
begin scalar x;
erfg!* := t;
if numberp cursym!* or not(x := get(cursym!*,'prtch))
then x := cursym!*;
terpri();
if !*echo then terpri();
outl!*:=car outl!* . '!$!$!$ . cdr outl!*;
comm1 t;
mapcar(reversip!* outl!*,function prin2);
terpri();
outl!* := nil;
if null v then rederr u
else rederr(x . ("invalid" .
(if u then list("in",u,"statement") else nil)))
end;
symbolic procedure typerr(u,v); rederr list(u,"invalid as",v);
endmodule;
module parser; % Functions for parsing RLISP expressions.
% Author: Anthony C. Hearn.
% Copyright (c) 1987 The RAND Corporation. All rights reserved.
fluid '(!*backtrace !*mode);
global '(cursym!* letl!* nxtsym!*);
%With the exception of assignment statements, which are handled by
%XREAD, statements in REDUCE are introduced by a key-word, which
%initiates a reading process peculiar to that statement. The key-word
%is recognized (in XREAD1) by the indicator STAT on its property list.
%The corresponding property is the name of the function (of no
%arguments) which carries out the reading sequence.
% ***** COMMENTS *****
symbolic procedure comm1 u;
begin scalar bool;
if u eq 'end then go to b;
a: if cursym!* eq '!*semicol!*
or u eq 'end
and cursym!* memq
'(end else then until !*rpar!* !*rsqb!*)
then return nil
else if u eq 'end and null bool
then progn(lprim list("END-COMMENT NO LONGER SUPPORTED"),
bool := t);
b: scan();
go to a
end;
% ***** CONDITIONAL STATEMENT *****
symbolic procedure ifstat;
begin scalar condx,condit;
a: condx := xread t;
if not cursym!* eq 'then then symerr('if,t);
condit := aconc!*(condit,list(condx,xread t));
if not cursym!* eq 'else then nil
else if scan() eq 'if then go to a
else condit := aconc!*(condit,list(t,xread1 t));
return ('cond . condit)
end;
put('if,'stat,'ifstat);
flag ('(then else),'delim);
% ***** LAMBDA STATEMENT *****
symbolic procedure lamstat;
begin scalar x,y;
x:= xread 'lambda;
% x := flagtype(if null x then nil else remcomma x,'scalar);
if x then x := remcomma x;
y := list('lambda,x,xread t);
% remtype x;
return y
end;
put ('lambda,'stat,'lamstat);
% ***** GROUP STATEMENT *****
symbolic procedure mkprogn;
%Expects a list of statements terminated by a >>;
begin scalar lst;
a: lst := aconc!*(lst,xread 'group);
if null(cursym!* eq '!*rsqb!*) then go to a;
scan();
return ('progn . lst)
end;
put('!*lsqb!*,'stat,'mkprogn);
flag('(!*rsqb!*),'delim);
flag('(!*rsqb!*),'nodel);
% ***** END STATEMENT *****
symbolic procedure endstat;
%This procedure can also be used for any key-words which take no
%arguments;
begin scalar x; x := cursym!*; comm1 'end; return list x end;
put('end,'stat,'endstat);
put('endmodule,'stat,'endstat);
put('bye,'stat,'endstat);
put('quit,'stat,'endstat);
flag('(bye quit),'eval);
put('showtime,'stat,'endstat);
endmodule;
module block; % Block statement and related operators.
% Author: Anthony C. Hearn.
% Copyright (c) 1987 The RAND Corporation. All rights reserved.
fluid '(!*blockp);
global '(!*vars!* cursym!* nxtsym!*);
% ***** GO statement *****
symbolic procedure gostat;
begin scalar var;
var := if eq(scan(),'to) then scan() else cursym!*;
scan();
return list('go,var)
end;
put('go,'stat,'gostat);
put('goto,'newnam,'go);
% ***** Declaration Statement *****
symbolic procedure decl u;
begin scalar varlis,w;
a: if cursym!* eq '!*semicol!* then go to c
else if not flagp!*!*(cursym!*,'type) then return varlis
else if cursym!* eq 'dcl then go to dclr;
w := cursym!*;
if scan() eq 'procedure then return procstat1 w;
varlis := append(varlis,pairvars(remcomma xread1 nil,nil,w));
b: if not cursym!* eq '!*semicol!* then symerr(nil,t)
else if null u then return list('dcl,mkquote varlis);
%top level declaration;
c: scan();
go to a;
dclr: varlis := append(varlis,dclstat1());
go to b
end;
flag ('(dcl real integer scalar),'type);
symbolic procedure dclstat; list('dcl,mkquote dclstat1());
symbolic procedure dclstat1;
begin scalar x,y;
a: x := xread nil;
if not cursym!* eq '!*colon!* then symerr('dcl,t);
y := append(y,pairvars(remcomma x,nil,scan()));
if scan() eq '!*semicol!* then return y
else if not cursym!* eq '!*comma!* then symerr('dcl,t)
else go to a
end;
symbolic procedure dcl u;
%U is a list of (id, mode) pairs, which are declared as global vars;
begin scalar x;
!*vars!* := append(u,!*vars!*);
x := mapcar(u,function car);
global x;
flag(x,'share);
a: if null u then return nil;
set(caar u,get(cdar u,'initvalue));
u := cdr u;
go to a
end;
put('integer,'initvalue,0);
put('dcl,'stat,'dclstat);
symbolic procedure decstat;
%only called if a declaration occurs at the top level or not first
%in a block;
begin scalar x,y,z;
if !*blockp then symerr('block,t);
x := cursym!*;
y := nxtsym!*;
z := decl nil;
if y neq 'procedure then rederr list(x,"invalid outside block");
return z
end;
put('integer,'stat,'decstat);
put('real,'stat,'decstat);
put('scalar,'stat,'decstat);
% ***** Block Statement *****
symbolic procedure blockstat;
begin scalar hold,varlis,x,!*blockp;
!*blockp := t;
scan();
if cursym!* memq '(nil !*rpar!*) then rederr "BEGIN invalid";
varlis := decl t;
a: if cursym!* eq 'end and not nxtsym!* eq '!: then go to b;
x := xread1 nil;
if eqcar(x,'end) then go to c;
not cursym!* eq 'end and scan();
if x then hold := aconc!*(hold,x);
go to a;
b: comm1 'end;
c: return mkblock(varlis,hold)
end;
symbolic procedure mkblock(u,v); 'block . (u . v);
putd('block,'macro,
'(lambda (u) (cons 'prog
(cons (mapcar (cadr u) (function car)) (cddr u)))));
symbolic procedure formblock(u,vars,mode);
'prog . append(initprogvars cadr u,
formprog1(cddr u,append(cadr u,vars),mode));
symbolic procedure initprogvars u;
begin scalar x,y,z;
a: if null u then return(reversip!* x . reversip!* y)
else if z := get(cdar u,'initvalue)
then y := mksetq(caar u,z) . y;
x := caar u . x;
u := cdr u;
go to a
end;
symbolic procedure formprog(u,vars,mode);
'prog . cadr u . formprog1(cddr u,pairvars(cadr u,vars,mode),mode);
symbolic procedure formprog1(u,vars,mode);
if null u then nil
else if atom car u then car u . formprog1(cdr u,vars,mode)
else if idp caar u and flagp(caar u,'modefn)
then formc(cadar u,vars,caar u) . formprog1(cdr u,vars,mode)
else formc(car u,vars,mode) . formprog1(cdr u,vars,mode);
put('block,'formfn,'formblock);
put('prog,'formfn,'formprog);
put('begin,'stat,'blockstat);
% ***** Return Statement *****
symbolic procedure retstat;
if not !*blockp then symerr(nil,t)
else list('return,
if flagp!*!*(scan(),'delim) then nil else xread1 t);
put('return,'stat,'retstat);
endmodule;
module form; % Performs a mode analysis of parsed forms.
% Author: Anthony C. Hearn.
% Modifications by: Jed Marti.
% Copyright (c) 1987 The RAND Corporation. All rights reserved.
fluid '(!*!*a2sfn !*cref !*defn !*mode current!-modulus);
global '(!*argnochk !*composites !*force !*micro!-version !*vars!*);
!*!*a2sfn := 'aeval;
flag('(algebraic symbolic),'modefn);
symbolic procedure formcond(u,vars,mode);
'cond . formcond1(cdr u,vars,mode);
symbolic procedure formcond1(u,vars,mode);
if null u then nil
else list(formbool(caar u,vars,mode),form1(cadar u,vars,mode))
% FORMC here would add REVAL
. formcond1(cdr u,vars,mode);
put('cond,'formfn,'formcond);
symbolic procedure formlamb(u,vars,mode);
list('lambda,cadr u,form1(caddr u,pairvars(cadr u,vars,mode),mode));
put('lambda,'formfn,'formlamb);
symbolic procedure formprogn(u,vars,mode);
'progn . formclis(cdr u,vars,mode);
put('progn,'formfn,'formprogn);
symbolic procedure expdrmacro u;
%returns the macro form for U if expansion is permitted;
begin scalar x;
if null(x := getrmacro u) or flagp(u,'noexpand) then return nil
else if null !*cref and (null !*defn or car x eq 'smacro)
or flagp(u,'expand) or !*force
then return x
else return nil
end;
symbolic procedure getrmacro u;
%returns a Reduce macro definition for U, if one exists,
%in GETD format;
begin scalar x;
return if not idp u then nil
else if (x := getd u) and car x eq 'macro then x
else if (x := get(u,'smacro)) then 'smacro . x
% else if (x := get(u,'nmacro)) then 'nmacro . x;
else nil
end;
symbolic procedure applmacro(u,v,w); apply1(u,w . v);
%symbolic procedure applnmacro(u,v,w);
% apply(u,if flagp(w,'nospread) then list v else v);
% symbolic procedure applsmacro(u,v,w);
% %We could use an atom sublis here, eg SUBLA;
% sublis(pair(cadr u,v),caddr u);
put('macro,'macrofn,'applmacro);
%put('nmacro,'macrofn,'applnmacro);
put('smacro,'macrofn,'applsmacro);
flag('(ed go quote),'noform);
symbolic procedure set!-global!-mode u;
begin !*mode := u end;
symbolic procedure form1(u,vars,mode);
begin scalar x,y;
if atom u
then return if not idp u then u
else if u eq 'ed then list u
else if flagp(u,'modefn) then set!-global!-mode u
else if x:= get(mode,'idfn)
then apply2(x,u,vars)
else u
else if not atom car u
then if caar u eq 'lambda then return formlis(u,vars,mode)
else typerr(car u,"operator")
else if not idp car u then typerr(car u,"operator")
else if get(car u, 'localfnname)
then return form1(get(car u,'localfnname) . cdr u,vars,mode)
else if flagp(car u,'noform) then return u
else if arrayp car u
and (mode eq 'symbolic or intexprlisp(cdr u,vars))
then return list('getel,intargfn(u,vars,mode))
else if flagp(car u,'modefn)
then return convertmode(cadr u,vars,mode,car u)
else if (x := get(car u,'formfn))
then return macrochk(apply(x,list(u,vars,mode)),mode)
else if get(car u,'stat) eq 'rlis
then return macrochk(formrlis(u,vars,mode),mode)
% else if (x := getd car u) and eqcar(x, 'macro) and
% not(mode eq 'algebraic) then
% return << x := apply(cdr x, list(u, vars, mode));
% formc(x, vars, mode) >>
;
argnochk u;
x := formlis(cdr u,vars,mode);
y := if x=cdr u then u else car u . x;
return if mode eq 'symbolic
or get(car u,'stat)
or cdr u and eqcar(cadr u,'quote)
and null !*micro!-version
or intexprnp(y,vars) and null !*composites
and null current!-modulus
then macrochk(y,mode)
else if not(mode eq 'algebraic)
then convertmode(y,vars,mode,'algebraic)
else ('list . algid(car u,vars) . x)
end;
symbolic procedure argnochk u;
begin scalar x;
if null !*argnochk then nil
else if (x := argsofopr car u) and x neq length cdr u
then rederr list(car u,"called with",
length cdr u,
if length cdr u=1 then "argument"
else "arguments",
"instead of",x)
end;
symbolic procedure argsofopr u;
% This function may be optimizable in various implementations.
get(u,'number!-of!-args);
symbolic procedure intexprnp(u,vars);
%determines if U is an integer expression;
if atom u then if numberp u then fixp u
else if (u := atsoc(u,vars)) then cdr u eq 'integer
else nil
else idp car u and flagp(car u,'intfn) and intexprlisp(cdr u,vars);
symbolic procedure intexprlisp(u,vars);
null u or intexprnp(car u,vars) and intexprlisp(cdr u,vars);
flag('(difference minus plus times),'intfn);
% EXPT is not included in this list, because a negative exponent can
% cause problems (i.e., result can be rational);
symbolic procedure formlis(u,vars,mode);
mapcar(u,function (lambda x; form1(x,vars,mode)));
symbolic procedure formclis(u,vars,mode);
mapcar(u,function (lambda x; formc(x,vars,mode)));
symbolic procedure form u; form1(u,!*vars!*,!*mode);
symbolic procedure macrochk(u,mode);
begin scalar y;
%expands U if CAR U is a macro and expansion allowed;
if atom u then return u
else if (y := expdrmacro car u)
and (mode eq 'symbolic or idp car u and flagp(car u,'opfn))
then return apply(get(car y,'macrofn),list(cdr y,cdr u,car u))
else return u
end;
put('symbolic,'idfn,'symbid);
symbolic procedure symbid(u,vars); u;
% if atsoc(u,vars) or fluidp u or globalp u or u memq '(nil t)
% or flagp(u,'share) then u
% else <<lprim list(u,"Non-Local Identifier");% u>>;
put('algebraic,'idfn,'algid);
symbolic procedure algid(u,vars);
if atsoc(u,vars) or flagp(u,'share) then u else mkquote u;
put('integer,'idfn,'intid);
symbolic procedure intid(u,vars);
begin scalar x,y;
return if (x := atsoc(u,vars))
then if cdr x eq 'integer then u
else if y := get(cdr x,'integer)
then apply2(y,u,vars)
else if cdr x eq 'scalar then !*!*a2i(u,vars)
else rederr list(cdr x,"not convertable to INTEGER")
else !*!*a2i(mkquote u,vars)
end;
symbolic procedure convertmode(exprn,vars,target,source);
convertmode1(form1(exprn,vars,source),vars,target,source);
symbolic procedure convertmode1(exprn,vars,target,source);
begin scalar x;
if source eq 'real then source := 'algebraic;
if target eq 'real then target := 'algebraic;
if target eq source then return exprn
else if idp exprn and (x := atsoc(exprn,vars))
and not(cdr x memq '(integer scalar real))
and not(cdr x eq source)
then return convertmode(exprn,vars,target,cdr x)
else if not (x := get(source,target))
then typerr(source,target)
else return apply2(x,exprn,vars)
end;
put('algebraic,'symbolic,'!*!*a2s);
put('symbolic,'algebraic,'!*!*s2a);
symbolic procedure !*!*a2s(u,vars);
% It would be nice if we could include the ATSOC(U,VARS) line,
% since in many cases that would save recomputation. However,
% in any sequential process, assignments or subsititution rules
% can change the value of a variable, so we have to check its
% value again. More comprehensive analysis could certainly
% optimize this.
if u = '(quote nil) then nil
else if null u or constantp u and null fixp u
or intexprnp(u,vars) and null !*composites
and null current!-modulus
or not atom u and idp car u
and flagp(car u,'nochange) and not(car u eq 'getel)
% or atsoc(u,vars) % means it was already evaluated
then u
else list(!*!*a2sfn,u);
symbolic procedure !*!*s2a(u,vars); u;
symbolic procedure formc(u,vars,mode);
%this needs to be generalized;
if mode eq 'algebraic and intexprnp(u,vars) then u
else convertmode(u,vars,'symbolic,mode);
symbolic procedure intargfn(u,vars,mode);
% transforms array element U into expression with integer arguments.
% Array name is treated as an algebraic variable;
'list . form1(car u,vars,'algebraic) .
mapcar(cdr u,
function (lambda x;
convertmode(x,vars,'integer,mode)));
put('algebraic,'integer,'!*!*a2i);
symbolic procedure !*!*a2i(u,vars);
if intexprnp(u,vars) then u else list('ieval,u);
symbolic procedure ieval u; !*s2i reval u;
flag('(ieval),'opfn); % To make it a symbolic operator.
flag('(ieval),'nochange);
put('symbolic,'integer,'!*!*s2i);
symbolic procedure !*!*s2i(u,vars);
if fixp u then u else list('!*s2i,u);
symbolic procedure !*s2i u;
if fixp u then u else typerr(u,"integer");
put('integer,'symbolic,'identity);
symbolic procedure identity(u,vars); u;
symbolic procedure formbool(u,vars,mode);
if mode eq 'symbolic then form1(u,vars,mode)
else if atom u then if not idp u or atsoc(u,vars) or u eq 't
then u
else formc!*(u,vars,mode)
else if intexprlisp(cdr u,vars) and get(car u,'boolfn) then u
else if idp car u and get(car u,'boolfn)
then get(car u,'boolfn) . formclis(cdr u,vars,mode)
else if idp car u and flagp(car u,'boolean)
then car u .
mapcar(cdr u,function (lambda x;
if flagp(car u,'boolargs)
then formbool(x,vars,mode)
else formc!*(x,vars,mode)))
else formc!*(u,vars,mode);
symbolic procedure formc!*(u,vars,mode);
begin scalar !*!*a2sfn;
!*!*a2sfn := 'reval;
return formc(u,vars,mode)
end;
% Functions with side effects must be handled carefully in this model,
% otherwise they are not always evaluated within blocks.
symbolic procedure formrederr(u,vars,mode);
begin scalar x;
x := formc!*(cadr u,vars,mode);
return list('rederr,x)
end;
put('rederr,'formfn,'formrederr);
symbolic procedure formreturn(u,vars,mode);
begin scalar x;
x := form1(cadr u,vars,mode); % FORMC here would add REVAL
if not(mode memq '(symbolic integer real))
and eqcar(x,'setq) % Should this be more general?
then x := list(!*!*a2sfn,x);
return list('return,x)
end;
put('return,'formfn,'formreturn);
symbolic procedure formsetq(u,vars,mode);
begin scalar target,x,y;
u := cdr u;
if eqcar(cadr u,'quote) then mode := 'symbolic;
if idp car u
and (y := atsoc(car u,vars)) and not(cdr y eq 'scalar)
then target := 'symbolic % used to be CDR Y
else target := 'symbolic;
% Make target always SYMBOLIC so that algebraic expressions
% are evaluated before being stored.
x := convertmode(cadr u,vars,target,mode);
return if not atom car u
then if not idp caar u then typerr(car u,"assignment")
else if arrayp caar u
then list('setel,intargfn(car u,vars,mode),x)
else if y := get(caar u,'setqfn)
then form1((y . append(cdar u,cdr u)),vars,mode)
% else if y := get(caar u, 'access)
% then list('m!-setf,
% list(caar u, form1(cadar u, vars, mode)),
% x)
else list('setk,form1(car u,vars,'algebraic),x)
% algebraic needed above, since SETK expects it.
else if not idp car u then typerr(car u,"assignment")
else if mode eq 'symbolic or y or flagp(car u,'share)
or eqcar(x,'quote)
then mksetq(car u,x)
else list('setk,mkquote car u,x)
end;
put('car,'setqfn,'rplaca);
put('cdr,'setqfn,'rplacd);
put('setq,'formfn,'formsetq);
symbolic procedure formfunc(u,vars,mode);
if idp cadr u then if getrmacro cadr u
then rederr list("Macro",cadr u,"Used as Function")
else list('function,cadr u)
else list('function,form1(cadr u,vars,mode));
put('function,'formfn,'formfunc);
% RLIS is a parser function that reads a list of arguments and returns
% this list as one argument. It needs to be defined in this module for
% bootstrapping purposes since this definition only works with its form
% function.
symbolic procedure rlis;
begin scalar x;
x := cursym!*;
return if flagp!*!*(scan(),'delim) then list(x,nil)
else x . remcomma xread1 'lambda
end;
symbolic procedure flagop u; begin flag(u,'flagop); rlistat u end;
symbolic procedure rlistat u;
begin
a: if null u then return nil;
put(car u,'stat,'rlis);
u := cdr u;
go to a
end;
rlistat '(flagop);
symbolic procedure formrlis(u,vars,mode);
if not flagp(car u,'flagop)
then list(car u,'list . formlis(cdr u,vars,'algebraic))
else if not idlistp cdr u
then typerr('!*comma!* . cdr u,"identifier list")
else mkprog(nil,list('flag,mkquote cdr u,mkquote car u)
. get(car u,'simpfg));
symbolic procedure mkarg(u,vars);
% Returns the "unevaled" form of U.
if null u or constantp u then u
else if atom u then if atsoc(u,vars) then u else mkquote u
else if car u eq 'quote then mkquote u
else 'list . mapcar(u,function (lambda x; mkarg(x,vars)));
endmodule;
module proc; % Procedure statement.
% Author: Anthony C. Hearn.
% Copyright (c) 1987 The RAND Corporation. All rights reserved.
fluid '(!*backtrace);
global '(!*argnochk !*comp !*lose cursym!* erfg!* fname!* ftypes!*);
fluid '(!*defn);
!*lose := t;
ftypes!* := '(expr fexpr macro);
symbolic procedure putc(name,type,body);
%defines a non-standard function, such as an smacro. Returns NAME;
begin
if !*comp and flagp(type,'compile) then compd(name,type,body)
else put(name,type,body);
return name
end;
% flag('(putc),'eval);
symbolic procedure formproc(u,vars,mode);
begin scalar body,name,type,varlis,x,y;
u := cdr u;
name := car u;
if cadr u then mode := cadr u; % overwrite previous mode
u := cddr u;
type := car u;
if flagp(name,'lose) and (!*lose or null !*defn)
then return progn(lprim list(name,
"not defined (LOSE flag)"),
nil);
varlis := cadr u;
u := caddr u;
x := if eqcar(u,'block) then cadr u else nil;
y := pairxvars(varlis,x,vars,mode);
if x then u := car u . rplaca!*(cdr u,cdr y);
body:= form1(u,car y,mode); % FORMC here would add REVAL
if type eq 'expr then body := list('de,name,varlis,body)
else if type eq 'fexpr then body := list('df,name,varlis,body)
else if type eq 'macro then body := list('dm,name,varlis,body)
else if type eq 'emb then return embfn(name,varlis,body)
else body := list('putc,
mkquote name,
mkquote type,
mkquote list('lambda,varlis,body));
if not(mode eq 'symbolic)
then body := list('progn,
list('flag,mkquote list name,mkquote 'opfn),
body);
if !*argnochk and type memq '(expr smacro)
then body := list('progn,
list('put,mkquote name,
mkquote 'number!-of!-args,
length varlis),
body);
if !*defn and type memq '(fexpr macro smacro) then eval body;
return body
end;
put('procedure,'formfn,'formproc);
symbolic procedure pairxvars(u,v,vars,mode);
%Pairs procedure variables and their modes, taking into account
%the convention which allows a top level prog to change the mode
%of such a variable;
begin scalar x,y;
a: if null u then return append(reversip!* x,vars) . v
else if (y := atsoc(car u,v))
then <<v := delete(y,v);
if not(cdr y eq 'scalar) then x := (car u . cdr y) . x
else x := (car u . mode) . x>>
else x := (car u . mode) . x;
u := cdr u;
go to a
end;
symbolic procedure procstat1 mode;
begin scalar bool,u,type,x,y,z;
bool := erfg!*;
if fname!* then go to b
else if cursym!* eq 'procedure then type := 'expr
else progn(type := cursym!*,scan());
if not cursym!* eq 'procedure then go to c;
x := errorset('(xread (quote proc)),nil,!*backtrace);
if errorp x then go to a
else if atom (x := car x) then x := list x; %no arguments;
fname!* := car x; %function name;
if idp fname!* %AND NOT(TYPE MEMQ FTYPES!*);
then if null fname!* or (z := gettype fname!*)
and not z memq '(procedure operator)
then go to d
else if not getd fname!* then flag(list fname!*,'fnc);
%to prevent invalid use of function name in body;
u := cdr x;
y := u;
x := car x . y;
a: z := errorset('(xread t),nil,!*backtrace);
if not errorp z then z := car z;
if null erfg!* then z:=list('procedure,car x,mode,type,y,z);
remflag(list fname!*,'fnc);
fname!*:=nil;
if erfg!* then progn(z := nil,if not bool then error1());
return z;
b: bool := t;
c: errorset('(symerr (quote procedure) t),nil,!*backtrace);
go to a;
d: typerr(list(z,fname!*),"procedure");
go to a
end;
symbolic procedure procstat; procstat1 nil;
deflist ('((procedure procstat) (expr procstat) (fexpr procstat)
(emb procstat) (macro procstat) (smacro procstat)),
'stat);
% Next line refers to bootstrapping process.
if get('symbolic,'stat) eq 'procstat then remprop('symbolic,'stat);
deflist('((lisp symbolic)),'newnam);
endmodule;
module forstat; % Definition of REDUCE FOR loops.
% Author: Anthony C. Hearn.
% Copyright (c) 1987 The RAND Corporation. All rights reserved.
fluid '(!*blockp);
global '(cursym!* foractions!*);
comment the syntax of the FOR statement is as follows:
{step i3 until}
{i := i1 { } i2 }
{ { : } }
for { } <action> <expr>
{ { in } }
{ each i { } <list> }
{ on }
In all cases, the <expr> is evaluated algebraically within the scope of
the current value of i. If <action> is DO, then nothing else happens.
In other cases, <action> is a binary operator that causes a result to be
built up and returned by FOR. In each case, the loop is initialized to
a default value. The test for the end condition is made before any
action is taken.
The effect of the definition here is to replace all for loops by
semantically equivalent blocks. As a result, none of the mapping
functions are needed in REDUCE.
To declare a set of actions, one says;
foractions!* := '(do collect conc product sum);
remflag(foractions!*,'delim); % For bootstrapping purposes.
% To associate a binary function with an action, one says:
deflist('((product times) (sum plus)),'bin);
% And to give these an initial value in a loop:
deflist('((product 1) (sum 0)),'initval);
% NB: We need to reset for and let delims if an error occurs. It's
% probably best to do this in the begin1 loop.
flag('(for),'nochange);
symbolic procedure forstat;
begin scalar !*blockp;
return if scan() eq 'all then forallstat()
else if cursym!* eq 'each then foreachstat()
else forloop()
end;
put('for,'stat,'forstat);
symbolic procedure forloop;
begin scalar action,bool,incr,var,x;
flag('(step),'delim);
x := errorset('(xread1 'for),t,t);
remflag('(step),'delim);
if errorp x then error1() else x := car x;
if not eqcar(x,'setq) or not idp(var := cadr x)
then symerr('for,t);
x := caddr x;
if cursym!* eq 'step
then <<flag('(until),'delim);
incr := xread t;
remflag('(until),'delim);
if not cursym!* eq 'until then symerr('for,t)>>
else if cursym!* eq '!*colon!* then incr := 1
else symerr('for,t);
if flagp(car foractions!*,'delim) then bool := t % nested loop
else flag(foractions!*,'delim);
incr := list(x,incr,xread t);
if null bool then remflag(foractions!*,'delim);
if not((action := cursym!*) memq foractions!*)
then symerr('for,t);
return list('for,var,incr,action,xread t)
end;
symbolic procedure formfor(u,vars,mode);
begin scalar action,algp,body,endval,incr,initval,var,x;
%ALGP is used to determine if the loop calculation must be
%done algebraically or not;
var := cadr u;
incr := caddr u;
incr := list(formc(car incr,vars,mode),
formc(cadr incr,vars,mode),
formc(caddr incr,vars,mode));
if intexprnp(car incr,vars) and intexprnp(cadr incr,vars)
and not atsoc(var,vars)
then vars := (var . 'integer) . vars;
action := cadddr u;
body :=
formc(car cddddr u,
(var .
if intexprlisp(caddr u,vars) then 'integer else mode)
. vars,mode);
algp := algmodep car incr or algmodep cadr incr
or algmodep caddr incr;
initval := car incr;
endval := caddr incr;
incr := cadr incr;
x := if algp then list('list,''difference,endval,var)
else list('difference,endval,var);
if incr neq 1
then x := if algp then list('list,''times,incr,x)
else list('times,incr,x);
% We could consider simplifying X here (via reval).
x := if algp then list('aminusp!:,x) else list('minusp,x);
return forformat(action,body,initval,x,
list('plus2,incr),var,vars,mode)
end;
put('for,'formfn,'formfor);
symbolic procedure algmodep u; eqcar(u,'aeval);
symbolic procedure aminusp!: u;
begin scalar x;
u := aeval u;
x := u;
if fixp x then return minusp x
else if not eqcar(x,'!*sq)
then msgpri(nil,reval u,"invalid in FOR statement",nil,t);
x := cadr x;
if fixp car x and fixp cdr x then return minusp car x
else if not cdr x = 1
or not (atom(x := car x) or atom car x)
% Should be DOMAINP, but SMACROs not yet defined.
then msgpri(nil,reval u,"invalid in FOR statement",nil,t)
else return apply('!:minusp,list x)
end;
symbolic procedure foreachstat;
begin scalar w,x,y,z;
if not idp(x := scan()) or not (y := scan()) memq '(in on)
then symerr("FOR EACH",t)
else if flagp(car foractions!*,'delim) then w := t
else flag(foractions!*,'delim);
z := xread t;
if null w then remflag(foractions!*,'delim);
w := cursym!*;
if not w memq foractions!* then symerr("FOR EACH",t);
return list('foreach,x,y,z,w,xread t)
end;
put('foreach,'stat,'foreachstat);
symbolic procedure formforeach(u,vars,mode);
begin scalar action,body,lst,mod,var;
var := cadr u; u := cddr u;
mod := car u; u := cdr u;
lst := formc(car u,vars,mode); u := cdr u;
if not(mode eq 'symbolic) then lst := list('getrlist,lst);
action := car u; u := cdr u;
body := formc(car u,(var . mode) . vars,mode);
if mod eq 'in
then body := list(list('lambda,list var,body),list('car,var))
else if not(mode eq 'symbolic) then typerr(mod,'action);
return forformat(action,body,lst,
list('null,var),list 'cdr,var,vars,mode)
end;
put('foreach,'formfn,'formforeach);
symbolic procedure forformat(action,body,initval,
testexp,updform,var,vars,mode);
begin scalar result;
result := gensym();
return
sublis(list('body2 .
if mode eq 'symbolic or intexprnp(body,vars)
then list(get(action,'bin),body,result)
else list('aeval,list('list,mkquote get(action,'bin),
body,result)),
'body3 .
if mode eq 'symbolic then body
else list('getrlist,body),
'body . body,
'initval . initval,
'nillist . if mode eq 'symbolic then nil else ''(list),
'result . result,
'initresult . get(action,'initval),
'resultlist . if mode eq 'symbolic then result
else list('cons,''list,result),
'testexp . testexp,
'updfn . car updform,
'updval . cdr updform,
'var . var),
if action eq 'do
then '(prog (var)
(setq var initval)
lab (cond (testexp (return nil)))
body
(setq var (updfn var . updval))
(go lab))
else if action eq 'collect
then '(prog (var result endptr)
(setq var initval)
(cond (testexp (return nillist)))
(setq result (setq endptr (cons body nil)))
looplabel
(setq var (updfn var . updval))
(cond (testexp (return resultlist)))
(rplacd endptr (cons body nil))
(setq endptr (cdr endptr))
(go looplabel))
else if action eq 'conc
then '(prog (var result endptr)
(setq var initval)
startover
(cond (testexp (return nillist)))
(setq result body)
(setq endptr (lastpair resultlist))
(setq var (updfn var . updval))
(cond ((atom endptr) (go startover)))
looplabel
(cond (testexp (return result)))
(rplacd endptr body3)
(setq endptr (lastpair endptr))
(setq var (updfn var . updval))
(go looplabel))
else '(prog (var result)
(setq var initval)
(setq result initresult)
lab1
(cond (testexp (return result)))
(setq result body2)
(setq var (updfn var . updval))
(go lab1)))
end;
symbolic procedure lastpair u;
% Return the last pair of the list u.
if atom u or atom cdr u then u else lastpair cdr u;
put('join,'newnam,'conc); % alternative for CONC
endmodule;
module loops; % Looping forms other than the FOR statement.
% Author: Anthony C. Hearn
% Copyright (c) 1987 The RAND Corporation. All rights reserved.
fluid '(!*blockp);
global '(cursym!*);
% ***** REPEAT STATEMENT *****
symbolic procedure repeatstat;
begin scalar body,!*blockp;
flag('(until),'delim);
body:= xread t;
remflag('(until),'delim);
if not cursym!* eq 'until then symerr('repeat,t);
return list('repeat,body,xread t);
end;
symbolic macro procedure repeat u;
begin scalar body,bool,lab;
body := cadr u; bool := caddr u;
lab := gensym();
return mkprog(nil,list(lab,body,
list('cond,list(list('not,bool),list('go,lab)))))
end;
put('repeat,'stat,'repeatstat);
flag('(repeat),'nochange);
symbolic procedure formrepeat(u,vars,mode);
list('repeat,formc(cadr u,vars,mode),formbool(caddr u,vars,mode));
put('repeat,'formfn,'formrepeat);
% ***** WHILE STATEMENT *****
symbolic procedure whilstat;
begin scalar bool,!*blockp;
flag('(do),'delim);
bool := xread t;
remflag('(do),'delim);
if not cursym!* eq 'do then symerr('while,t);
return list('while,bool,xread t)
end;
symbolic macro procedure while u;
begin scalar body,bool,lab;
bool := cadr u; body := caddr u;
lab := gensym();
return mkprog(nil,list(lab,list('cond,list(list('not,bool),
list('return,nil))),body,list('go,lab)))
end;
put('while,'stat,'whilstat);
flag('(while),'nochange);
symbolic procedure formwhile(u,vars,mode);
list('while,formbool(cadr u,vars,mode),formc(caddr u,vars,mode));
put('while,'formfn,'formwhile);
endmodule;
module write; % Miscellaneous statement definitions.
% Author: Anthony C. Hearn.
% Copyright (c) 1987 The RAND Corporation. All rights reserved.
% ***** DEFINE STATEMENT *****
remprop('define,'stat);
symbolic procedure define u;
for each x in u do
if not eqcar(x,'equal) or not idp cadr x
then typerr(x,"DEFINE declaration")
else put(cadr x,'newnam,caddr x);
put('define,'stat,'rlis);
flag('(define),'eval);
% ***** WRITE STATEMENT *****
symbolic procedure formwrite(u,vars,mode);
begin scalar bool1,bool2,x,z;
u := cdr u;
bool1 := mode eq 'symbolic;
while u do
<<x := formc(car u,vars,mode);
z := (if bool1 then list('prin2,x)
else list('writepri,mkarg1(x,vars),
if not cdr u then if not bool2 then ''only else ''last
else if not bool2 then ''first else nil)) .
z;
bool2 := t;
u := cdr u>>;
return mkprog(nil,reversip!* z)
end;
symbolic procedure writepri(u,v);
begin scalar x; x := assgneval u; return varpri(car x,cdr x,v) end;
symbolic procedure mkarg1(u,vars);
% Returns the "unevaled" form of U for the WRITE command.
if null u or constantp u then u
else if atom u then if atsoc(u,vars)
then list('mkquote,u) else mkquote u
else if car u eq 'quote then mkquote u
else if car u eq 'setq
then list('list,''setq,mkquote cadr u,mkarg1(caddr u,vars))
else 'list . mapcar(u,function (lambda x; mkarg1(x,vars)));
put('write,'stat,'rlis);
put('write,'formfn,'formwrite);
endmodule;
module smacro; % Support for SMACRO expansion.
% Author: Anthony C. Hearn.
% Copyright (c) 1987 The RAND Corporation. All rights reserved.
symbolic procedure applsmacro(u,vals,name);
% U is smacro body of form (lambda <varlist> <body>), VALS is
% argument list, NAME is name of smacro.
begin scalar body,remvars,varlist,w;
varlist := cadr u;
body := caddr u;
if length varlist neq length vals
then rederr list("Argument mismatch for SMACRO",name);
if no!-side!-effect!-listp vals or one!-entry!-listp(varlist,body)
then return subla!-q(pair(varlist,vals),body)
else if length varlist>1
then <<w := for each x in varlist collect (x . gensym());
body := subla!-q(w,body);
varlist := for each x in w collect cdr x>>;
for each x in vals do
<<if no!-side!-effectp x or one!-entryp(car varlist,body)
then body := subla!-q(list(car varlist . x),body)
else remvars := aconc(remvars,car varlist . x);
varlist := cdr varlist>>;
if null remvars then return body
else <<w := list('lambda,
for each x in remvars collect car x,
body) .
for each x in remvars collect cdr x;
% IF NOT EQCAR(CADR W,'SETQ)
% THEN <<PRIN2 "*** SMACRO: "; PRINT CDR W>>;
return w>>
end;
symbolic procedure no!-side!-effectp u;
if atom u then numberp u or idp u and not(fluidp u or globalp u)
else if car u eq 'quote then t
else if flagp!*!*(car u,'nosideeffects)
then no!-side!-effect!-listp u
else nil;
symbolic procedure no!-side!-effect!-listp u;
null u or no!-side!-effectp car u and no!-side!-effect!-listp cdr u;
flag('(car cdr caar cadr cdar cddr caaar caadr cadar caddr cdaar cdadr
cddar cdddr cons),'nosideeffects);
symbolic procedure one!-entryp(u,v);
% determines if id U occurs less than twice in V.
if atom v then t
else if smemq(u,car v)
then if smemq(u,cdr v) then nil else one!-entryp(u,car v)
else one!-entryp(u,cdr v);
symbolic procedure one!-entry!-listp(u,v);
null u or one!-entryp(car u,v) and one!-entry!-listp(cdr u,v);
symbolic procedure subla!-q(u,v);
begin scalar x;
if null u or null v then return v
else if atom v
then return if x:= atsoc(v,u) then cdr x else v
else if car v eq 'quote then return v
else return(subla!-q(u,car v) . subla!-q(u,cdr v))
end;
endmodule;
module infix; % Functions for introducing new infix operators.
% Author: Anthony C. Hearn.
% Copyright (c) 1987 The RAND Corporation. All rights reserved.
fluid '(!*mode);
global '(preclis!*);
symbolic procedure infix x;
begin scalar y;
a: if null x then go to b;
y := car x;
if !*mode eq 'algebraic then mkop y;
if not(y member preclis!*) then preclis!* := y . preclis!*;
x := cdr x;
go to a;
b: mkprec()
end;
symbolic procedure precedence u;
begin scalar x,y,z;
preclis!* := delete(car u,preclis!*);
y := cadr u;
x := preclis!*;
a: if null x then rederr list (y,"not found")
else if y eq car x
then <<preclis!* :=
nconc!*(reversip!* z,car x . (car u . cdr x));
mkprec();
return nil>>;
z := car x . z;
x := cdr x;
go to a
end;
deflist('((infix rlis) (precedence rlis)),'stat);
flag('(infix precedence),'eval);
endmodule;
module where; % Support for a where construct.
% Author: Anthony C. Hearn.
% Copyright (c) 1987 The RAND Corporation. All rights reserved.
symbolic procedure formwhere(u,vars,mode);
begin scalar expn,equivs,y,z;
expn := cadr u;
equivs := caddr u;
if eqcar(equivs,'!*comma!*) then equivs := cdr equivs
else equivs := list equivs;
for each x in equivs do
if not atom x and car x memq '(equal setq)
then <<y := caddr x . y; z := cadr x . z>>
else rederr list(x,"invalid in WHERE statement");
return formc(list('lambda,reversip z,expn) . reversip y,
vars,mode)
end;
put('where,'formfn,'formwhere);
% infix where; % We do this explicitly to avoid changing preclis*.
deflist('((where 1)),'infix);
put('where,'op,'((1 1)));
endmodule;
module list; % Define a list as a list of expressions in curly brackets.
% Author: Anthony C. Hearn.
% Copyright (c) 1987 The RAND Corporation. All rights reserved.
fluid '(orig!* posn!*);
global '(cursym!*);
% Add to system table.
put('list,'tag,'list);
put('list,'rtypefn,'(lambda (x) 'list));
% Parsing interface.
symbolic procedure xreadlist;
% expects a list of expressions enclosed by {, }.
% also allows expressions separated by ; --- treats these as progn.
begin scalar cursym,delim,lst;
if scan() eq '!*rcbkt!* then <<scan(); return list 'list>>;
a: lst := aconc(lst,xread1 'group);
cursym := cursym!*;
scan();
if cursym eq '!*rcbkt!*
then return if delim eq '!*semicol!* then 'progn . lst
else 'list . lst
else if null delim then delim := cursym
else if not(delim eq cursym)
then symerr("syntax error: mixed , and ; in list",nil);
go to a
end;
put('!*lcbkt!*,'stat,'xreadlist);
newtok '((!{) !*lcbkt!*);
newtok '((!}) !*rcbkt!*);
flag('(!*rcbkt!*),'delim);
flag('(!*rcbkt!*),'nodel);
% Evaluation interface.
put('list,'evfn,'listeval);
symbolic procedure getrlist u;
if eqcar(u,'list) then cdr u
else typerr(if eqcar(u,'!*sq) then prepsq cadr u else u,"list");
symbolic procedure listeval(u,v);
if atom u then listeval(get(u,'rvalue),v)
else car u . for each j in cdr u collect reval1(j,v);
% Length interface.
put('list,'lengthfn,'(lambda (x) (length (cdr x))));
% Printing interface.
put('list,'prifn,'listpri);
symbolic procedure listpri l;
% This definition is basically that of INPRINT, except that it
% decides when to split at the comma by looking at the size of
% the argument.
begin scalar orig,split,u;
u := l;
l := cdr l;
prin2!* get('!*lcbkt!*,'prtch);
% Do it this way so table can change.
orig := orig!*;
orig!* := if posn!*<18 then posn!* else orig!*+3;
if null l then go to b;
split := treesizep(l,40); % 40 is arbitrary choice.
a: maprint(negnumberchk car l,0);
l := cdr l;
if null l then go to b;
oprin '!*comma!*;
if split then terpri!* t;
go to a;
b: prin2!* get('!*rcbkt!*,'prtch);
% terpri!* nil;
orig!* := orig;
return u
end;
symbolic procedure treesizep(u,n);
% true if u has recursively more pairs than n.
treesizep1(u,n)=0;
symbolic procedure treesizep1(u,n);
if atom u then n-1
else if (n := treesizep1(car u,n))>0 then treesizep1(cdr u,n)
else 0;
% Definitions of operations on lists
symbolic procedure rfirst u;
<<argnochk ('first . u);
if null(getrtype(u := reval car u) eq 'list)
then typerr(u,"list")
else if null cdr u then parterr(u,1)
else cadr u>>;
put('first,'psopfn,'rfirst);
symbolic procedure parterr(u,v);
msgpri("Expression",u,"does not have part",v,t);
symbolic procedure rsecond u;
<<argnochk ('second . u);
if null(getrtype(u := reval car u) eq 'list) then typerr(u,"list")
else if null cdr u or null cddr u then parterr(u,2)
else caddr u>>;
put('second,'psopfn,'rsecond);
symbolic procedure rthird u;
<<argnochk ('third . u);
if null(getrtype(u := reval car u) eq 'list) then typerr(u,"list")
else if null cdr u or null cddr u or null cdddr u
then parterr(u,3)
else cadddr u>>;
put('third,'psopfn,'rthird);
symbolic procedure rrest u;
<<argnochk ('rest . u);
if null(getrtype(u := reval car u) eq 'list) then typerr(u,"list")
else if null cdr u then typerr(u,"non-empty list")
else 'list . cddr u>>;
put('rest,'psopfn,'rrest);
symbolic procedure rappend u;
begin scalar x,y;
argnochk ('append . u);
if null(getrtype(x := reval car u) eq 'list)
then typerr(x,"list")
else if null(getrtype(y := reval cadr u) eq 'list)
then typerr(y,"list")
else return 'list .append(cdr x,cdr y)
end;
put('append,'psopfn,'rappend);
symbolic procedure rcons u;
begin scalar x,y;
argnochk ('cons . u);
if (y := getrtype(x := reval cadr u)) eq 'vector
then return prepsq simpdot u
else if not(y eq 'list) then typerr(x,"list")
else return 'list . reval car u . cdr x
end;
put('cons,'psopfn,'rcons);
symbolic procedure rreverse u;
<<argnochk ('reverse . u);
if null(getrtype(u := reval car u) eq 'list) then typerr(u,"list")
else 'list . reverse cdr u>>;
put('reverse,'psopfn,'rreverse);
endmodule;
module array; % Array statement.
% Author: Anthony C. Hearn.
% Modifications by: Nancy Kirkwood.
% These definitions are very careful about bounds checking. Appropriate
% optimizations in a given system might really speed things up.
global '(erfg!*);
symbolic procedure getel u;
% Returns the value of the array element U.
getel1(get(car u,'rvalue),cdr u,get(car u,'dimension));
symbolic procedure getel1(u,v,dims);
if length v neq length dims
then rederr "Incorrect array reference"
else if null v then u
else if car v geq car dims then rederr "Array out of bounds"
else getel1(getv(u,car v),cdr v,cdr dims);
symbolic procedure setel(u,v);
% Sets array element U to V and returns V.
setel1(get(car u,'rvalue),cdr u,v,get(car u,'dimension));
symbolic procedure setel1(u,v,w,dims);
if length v neq length dims then rederr "Incorrect array reference"
else if car v geq car dims then rederr "Array out of bounds"
else if null cdr v then putv(u,car v,w)
else setel1(getv(u,car v),cdr v,w,cdr dims);
symbolic procedure dimension u; get(u,'dimension);
comment further support for REDUCE arrays;
symbolic procedure typechk(u,v);
begin scalar x;
if (x := gettype u) eq v or x eq 'parameter
then lprim list(v,u,"redefined")
else if x then typerr(list(x,u),v)
end;
symbolic procedure arrayfn(u,v);
% U is the defining mode, V a list of lists, assumed syntactically
% correct. ARRAYFN declares each element as an array unless a
% semantic mismatch occurs.
begin scalar y;
for each x in v do
<<typechk(car x,'array);
y := add1lis for each z in cdr x collect eval z;
if null erfg!*
then <<put(car x,'rtype,'array);
put(car x,'rvalue,mkarray(y,u));
put(car x,'dimension,y)>>>>
end;
symbolic procedure add1lis u;
if null u then nil else (car u+1) . add1lis cdr u;
symbolic procedure mkarray(u,v);
%U is a list of positive integers representing array bounds, V
%the defining mode. Value is an array structure;
if null u then if v eq 'symbolic then nil else 0
else begin integer n; scalar x;
n := car u-1;
x := mkvect n;
for i:=0:n do putv(x,i,mkarray(cdr u,v));
return x
end;
rlistat '(array);
flag ('(array arrayfn),'eval);
symbolic procedure formarray(u,vars,mode);
begin scalar x;
x := cdr u;
while x do <<if atom x then typerr(x,"Array List")
else if atom car x or not idp caar x
or not listp cdar x
then typerr(car x,"Array declaration");
x := cdr x>>;
u := for each z in cdr u collect intargfn(z,vars,mode);
%ARRAY arguments must be returned as quoted structures;
return list('arrayfn,mkquote mode,'list . u)
end;
symbolic procedure listp u;
% Returns T if U is a top level list.
null u or not atom u and listp cdr u;
put('array,'formfn,'formarray);
put('array,'rtypefn,'arraychk);
symbolic procedure arraychk u;
% If arraychk receives NIL, it means that array name is being used
% as an identifier. We no longer permit this.
if null u then 'array else nil;
% nil;
put('array,'evfn,'arrayeval);
symbolic procedure arrayeval(u,v);
% Eventually we'll support this.
rederr "Array arithmetic not defined";
put('array,'lengthfn,'arraylength);
symbolic procedure arraylength u; 'list . get(u,'dimension);
endmodule;
module switch; % Support for switches and ON and OFF statements.
% Author: Anthony C. Hearn.
% Copyright (c) 1987 The RAND Corporation. All rights reserved.
global '(!*switchcheck switchlist!*);
% No references to RPLAC-based functions in this module.
symbolic procedure on u; onoff(u,t);
symbolic procedure off u; onoff(u,nil);
symbolic procedure onoff(u,bool);
for each j in u do
begin scalar x,y;
if not idp j then typerr(j,"switch")
else if not flagp(j,'switch)
then if !*switchcheck
then rederr list(j,"not defined as switch")
else lpriw("*****",list(j,"not defined as switch"));
x := intern compress append(explode '!*,explode j);
if !*switchcheck and eval x eq bool then return nil
else if y := atsoc(bool,get(j,'simpfg))
then eval mkprog(nil,cdr y);
set(x,bool)
end;
symbolic procedure switch u;
% Declare list u as switches.
for each x in u do
begin scalar y;
if not idp x then typerr(x,"switch");
if not u memq switchlist!*
then switchlist!* := x . switchlist!*;
flag(list x,'switch);
y := intern compress append(explode '!*,explode x);
if not fluidp y and not globalp y then fluid list y
end;
deflist('((switch rlis)),'stat); % we use deflist since it's flagged
% eval
rlistat '(off on);
flag ('(off on),'ignore);
% Symbolic mode switches:
switch backtrace,comp,defn,demo,echo,errcont,int,msg,output,pret,
quotenewnam,raise,time; % switchcheck.
% The following are compiler switches that may not be supported in all
% versions:
switch pgwd,plap,pwrds;
% flag('(switch),'eval);
endmodule;
module io; % Reduce functions for handling input and output of files.
% Author: Anthony C. Hearn.
% Copyright (c) 1987 The RAND Corporation. All rights reserved.
fluid '(!*backtrace !*int semic!*);
global '(!*echo contl!* curline!* ifl!* ipl!* linelist!* ofl!* opl!*
techo!*);
symbolic procedure file!-transform(u,v);
% Performs a transformation on the file u. V is name of function
% used for the transformation;
begin scalar echo,ichan,oldichan,val;
echo := !*echo;
!*echo := nil;
ichan := open(u,'input);
oldichan := rds ichan;
val := errorset(list v,t,!*backtrace);
!*echo := echo;
close ichan;
rds oldichan;
if not errorp val then return car val
end;
symbolic procedure infile u;
% loads the single file u into REDUCE without echoing;
begin scalar !*int;
return file!-transform(u,function begin1)
end;
symbolic procedure in u;
begin scalar chan,echo,echop,type;
echop := semic!* eq '!;; %record echo character from input;
echo := !*echo; %save current echo status;
if null ifl!* then techo!* := !*echo; %terminal echo status;
for each fl in u do
<<if fl eq 't then fl := nil;
if null fl then <<!*echo := techo!*; rds nil; ifl!* := nil>>
else <<chan := open(fl := mkfil fl,'input);
rds chan;
% if assoc(fl,linelist!*) then nil;
curline!* := 1;
ifl!* := list(fl,chan,1)>>;
ipl!* := ifl!* . ipl!*; %add to input file stack;
!*echo := echop;
type := filetype fl;
if type and (type := get(type,'action)) then eval list type
else begin1();
if chan then close chan;
if fl eq caar ipl!* then ipl!* := cdr ipl!*
else errach list("FILE STACK CONFUSION",fl,ipl!*)>>;
!*echo := echo; %restore echo status;
if ipl!* and null contl!* then ifl!* := car ipl!*
else ifl!* := nil;
if ifl!* then <<rds cadr ifl!*; curline!* := caddr ifl!*>>
else rds nil
end;
symbolic procedure out u;
%U is a list of one file;
begin integer n; scalar chan,fl,x;
n := linelength nil;
if null u then return nil
else if car u eq 't then return <<wrs(ofl!* := nil); nil>>;
fl := mkfil car u;
if not (x := assoc(fl,opl!*))
then <<chan := open(fl,'output);
if chan
then <<ofl!*:= fl . chan; opl!*:= ofl!* . opl!*>>>>
else ofl!* := x;
wrs cdr ofl!*;
linelength n
end;
symbolic procedure shut u;
%U is a list of names of files to be shut;
begin scalar fl1;
for each fl in u do
<<if fl1 := assoc((fl := mkfil fl),opl!*)
then <<opl!* := delete(fl1,opl!*);
if fl1=ofl!* then <<ofl!* := nil; wrs nil>>;
close cdr fl1>>
else if not (fl1 := assoc(fl,ipl!*))
then rederr list(fl,"not open")
else if fl1 neq ifl!*
then <<close cadr fl1; ipl!* := delete(fl1,ipl!*)>>
else rederr list("Cannot shut current input file",car fl1)>>
end;
deflist ('((in rlis) (out rlis) (shut rlis)),'stat);
flag ('(in out shut),'eval);
flag ('(in out shut),'ignore);
endmodule;
module inter; % Functions for interactive support.
% Author: Anthony C. Hearn.
% Copyright (c) 1987 The RAND Corporation. All rights reserved.
fluid '(!*int);
global '(!$eof!$
!*echo
!*lessspace
cloc!*
contl!*
curline!*
edit!*
eof!*
erfg!*
flg!*
ifl!*
ipl!*
key!*
ofl!*
opl!*
techo!*);
symbolic procedure pause;
%Must appear at the top-most level;
if null !*int then nil
else if key!* eq 'pause then pause1 nil
else %typerr('pause,"lower level command");
pause1 nil; %Allow at lower level for now;
symbolic procedure pause1 bool;
begin
if bool then
if getd 'edit1 and erfg!* and cloc!* and yesp "Edit?"
then return <<contl!* := nil;
if ofl!* then <<lprim list(car ofl!*,'shut);
close cdr ofl!*;
opl!* := delete(ofl!*,opl!*);
ofl!* := nil>>;
edit1(cloc!*,nil)>>
else if flg!* then return (edit!* := nil);
if null ifl!* or yesp "Cont?" then return nil;
ifl!* := list(car ifl!*,cadr ifl!*,curline!*);
contl!* := ifl!* . !*echo . contl!*;
rds (ifl!* := nil);
!*echo := techo!*
end;
symbolic procedure yesp u;
begin scalar bool,ifl,ofl,x,y,z;
if ifl!*
then <<ifl := ifl!* := list(car ifl!*,cadr ifl!*,curline!*);
rds nil>>;
if ofl!* then <<ofl:= ofl!*; wrs nil>>;
if null !*lessspace then terpri();
if atom u then prin2 u else lpri u;
prin2t " (Y or N)";
if null !*lessspace then terpri();
z := setpchar '!?;
a: x := read();
% Assume an end-of-file is the same as "yes".
if (y := x eq 'y or x eq !$eof!$) or x eq 'n then go to b;
if null bool then prin2t "TYPE Y OR N";
bool := t;
go to a;
b: setpchar z;
if ofl then wrs cdr ofl;
if ifl then rds cadr ifl;
cursym!* := '!*semicol!*;
return y
end;
symbolic procedure cont;
begin scalar fl,techo;
if ifl!* then return nil %CONT only active from terminal;
else if null contl!* then rederr "No file open";
fl := car contl!*;
techo := cadr contl!*;
contl!* := cddr contl!*;
if car fl=caar ipl!* and cadr fl=cadar ipl!*
then <<ifl!* := fl;
if fl then <<rds cadr fl; curline!* := caddr fl>>
else rds nil;
!*echo := techo>>
else <<eof!* := 1; lprim list(fl,"not open"); error1()>>
end;
deflist ('((cont endstat) (pause endstat) (retry endstat)),'stat);
flag ('(cont),'ignore);
endmodule;
comment Codemist Standard Lisp based REDUCE "back-end";
% this file defines the system dependent code necessary to run REDUCE
% under Codemist Standard Lisp
comment the following functions, which are referenced in the basic
REDUCE source (rlisp, alg1, alg2, matr and phys) should be defined to
complete the definition of REDUCE:
bye
delcp
error1
filetype
mkfil
orderp
quit
random
seprp
setpchar.
prototypical descriptions of these functions are as follows;
remprop('bye,'stat);
symbolic procedure bye;
% returns control to the computer's operating system command level.
% the current REDUCE job cannot be restarted;
stop 0;
deflist('((bye endstat)),'stat);
remprop('quit,'stat);
symbolic procedure quit;
% returns control to the computer's operating system command level.
% the current REDUCE job cannot be restarted;
stop 0;
deflist('((quit endstat)),'stat);
symbolic procedure delcp u;
if u = '!; or u = '!$ then t else nil;
symbolic procedure filetype u; nil;
symbolic procedure mkfil u;
% converts file descriptor u into valid system filename;
if idp u then u
else if stringp u then string2file u
else if eqcar(u,'quote) then mkfil cadr u
else if atom u then nil
else for each z in u collect mkfil z;
symbolic procedure string2file s;
% converts a string into a valid file name.
s;
fluid '(promptstring!*);
procedure setpchar c; % set prompt, return old one
begin scalar oldprompt;
oldprompt := promptstring!*;
promptstring!* := c;
return oldprompt
end;
comment the following functions are only referenced if various flags are
set, or the functions are actually defined. they are defined in another
module, which is not needed to build the basic system. the name of the
flag follows the function name, enclosed in parentheses:
bfquotient!: (bigfloat)
cedit (?)
compd (comp)
edit1 this function provides a link to an editor. however, a
definition is not necessary, since REDUCE checks to see
if it has a function value.
embfn (?)
ezgcdf (ezgcd)
factorf (factor)
load!-module (?)
prettyprint (defn --- also called by dfprint)
this function is used in particular for output of RLISP
expressions in Lisp syntax. if that feature is needed,
and the prettyprint module is not available, then it
should be defined as print
rprint (pret)
texpt!: (bigfloat)
texpt!:any (bigfloat)
time (time) returns elapsed time from some arbitrary initial
point in milliseconds;
comment we also need to define a function begin, which acts as the top-
level call to REDUCE, and sets the appropriate variables. the following is
a minimum definition;
fluid '(!*int !*mode);
global '(crchar!* date!* ifl!* ipl!* ofl!* !*extraecho !*echo);
remflag('(begin),'go);
symbolic procedure begin;
begin
!*int := not batchp();
!*echo := not !*int;
!*extraecho := t;
ifl!* := ipl!* := ofl!* := nil;
if null date!* then go to a;
% verbos nil; % leave verbos flag as it had been
linelength 79;
prin2 date!*;
prin2t " ...";
!*mode := if getd 'addsq then 'algebraic else 'symbolic;
initreduce(); % resets date!*;
erfg!* := !*defn := cmsg!* := nil; % reset error status;
a: crchar!* := '! ;
if errorp errorset('(begin1),t,!*backtrace) then go to a;
if not yesp "are you sure you want to leave REDUCE & enter Lisp?" then
go to a;
prin2t "entering Lisp ... ";
prin2t "type (begin) to re-enter REDUCE, (stop) to exit from Lisp";
end;
flag('(begin),'go);
comment initial setups for REDUCE;
global '(spare!* statcounter);
spare!* := 10;
symbolic procedure initreduce; %. initial declarations for REDUCE
<<statcounter := 0;
date!* := compress('!" . append(explode2 "RLISP 3.3, ",
append(explode2 date(),list '!")));
crbuf!* := crbuflis!* := inputbuflis!* := resultbuflis!* := nil>>;
flag('(reclaim rdf),'opfn);
flag('(explode2 printc princ ttab unglobal random next!-random!-number), 'lose);
% redo showtime for Codemist Lisp interpretation of gctime vs. time
remflag('(showtime), 'lose);
symbolic procedure showtime;
begin scalar x,y;
x := otime!*;
otime!* := time();
x := otime!*-x;
y := ogctime!*;
ogctime!* := gctime();
y := ogctime!* - y;
% x := x - y; % not for Codemist Lisp
terpri();
prin2 "Time: "; prin2 x; prin2 " ms";
if y = 0 then return terpri();
prin2 " plus GC time: "; prin2 y; prin2 " ms"
end;
flag('(lengthc), 'lose);
symbolic procedure typerr(u, v);
% redefined when I get to alg1.red, but smashes before then
% need some support, I guess
<< terpri();
princ "+++++ typerr: ";
prin u;
princ " ";
prin v;
terpri();
error "typerr called"
>>;
symbolic procedure flush();
begin
scalar c;
while (c:=readch()) neq !$eol!$ and c neq !$eof!$ do nil;
return nil
end;
flag('(cedit1), 'lose);
% redefinition of yesp to improve prompt generation
remflag('(yesp), 'lose);
symbolic procedure yesp u;
begin scalar bool,ifl,ofl,x,y,z;
if ifl!*
then <<ifl := ifl!* := list(car ifl!*,cadr ifl!*,curline!*);
rds nil>>;
if ofl!* then <<ofl:= ofl!*; wrs nil>>;
if null !*lessspace then terpri();
if atom u then prin2 u else lpri u;
prin2t " (y or n)";
if null !*lessspace then terpri();
z := setpchar '!?;
a: prin2 '!?;
x := read();
% assume an end-of-file is the same as "yes".
if (y := x eq 'y or x eq !$eof!$) or x eq 'n then go to b;
if null bool then prin2t "type y or n";
bool := t;
go to a;
b: setpchar z;
if ofl then wrs cdr ofl;
if ifl then rds cadr ifl;
cursym!* := '!*semicol!*;
return y
end;
% support for some more floating point stuff
global '(ft!-tolerance!* e!-value!* pi!-value!*);
ft!-tolerance!* := float 1 / float 1000000000000;
e!-value!* := exp 1.0;
pi!-value!* := 4.0 * atan 1.0;
comment now set the system name;
global '(systemname!*);
systemname!* := 'csl;
initreduce();
eval '(begin);
% (*)
% in "extras.red"$
% in "compiler.red"$
% compile!-all();
off comp,echo;
verbos nil;
linelength 78;
!*force := t;
symbolic procedure pptt u;
<< prettyprint u;
terpri();
terpri();
u >>;
dfprint!* := 'pptt;
out "$cslbase/extras.lsp"$
on defn$
in "$cslbase/extras.red"$
off defn$
out t$
"extras.lsp created from extras.red";
out "$cslbase/compiler.lsp"$
on defn$
in "$cslbase/compiler.red"$
off defn$
out t$
"compiler.lsp created from compiler.red";
out "$cslbase/ccomp.lsp"$
on defn$
in "$cslbase/ccomp.red"$
off defn$
out t$
"ccomp.lsp created from ccomp.red";
bye;
|