File size: 136,887 Bytes
08c8a6d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 |
And so Forth... Copyright J.L. Bezemer 2001-04-06 2 Contents 1 Preface 1.1 Copyright . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.3 About this primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Forth fundamentals 2.1 Making calculations without parenthesis . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 Manipulating the stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3 Deep stack manipulators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.4 Pass arguments to functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.5 Making your own words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.6 Adding comment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.7 Text-format of Forth source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.8 Displaying string constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.9 Declaring variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.10 Using variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.11 Built-in variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.12 What is a cell? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.13 Declaring and using constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.14 Built-in constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.15 Using booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.16 IF-ELSE constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.17 FOR-NEXT constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.18 WHILE-DO constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.19 REPEAT-UNTIL constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.20 Infinite loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.21 Getting a number from the keyboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.22 Aligning numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 5 5 5 5 7 7 8 9 9 10 11 11 12 12 12 13 13 13 13 13 14 14 15 16 16 16 17 4 3 Arrays and strings CONTENTS 3.1 Declaring arrays of numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2 Using arrays of numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.3 Creating arrays of constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.4 Using arrays of constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.5 Creating strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.6 Initializing strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.7 Getting the length of a string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.8 Printing a string variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.9 Copying a string variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.10 Slicing strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.11 Appending strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.12 Comparing strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.13 Removing trailing spaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.14 String constants and string variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.15 The count byte . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.16 Printing individual characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.17 Getting ASCII values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.18 When to use [CHAR] or CHAR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.19 Printing spaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.20 Fetching individual characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.21 Storing individual characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.22 Getting a string from the keyboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.23 What is the TIB? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.24 What is the PAD? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.25 How do I use TIB and PAD? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.26 Temporary string constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.27 Simple parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.28 Converting a string to a number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.29 Controlling the radix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.30 Pictured numeric output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.31 Converting a number to a string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 19 19 19 20 20 20 21 21 22 23 25 26 26 27 27 27 28 28 28 29 30 31 32 32 32 33 33 34 35 38 40 CONTENTS 4 Stacks and colon definitions 4.1 The address of a colon-definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2 Vectored execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.3 Using values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.4 The stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.5 Saving temporary values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.6 The Return Stack and the DO..LOOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.7 Other Return Stack manipulations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.8 Altering the flow with the Return Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.9 Leaving a colon-definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.10 How deep is your stack? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Advanced topics 5.1 Booleans and numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2 Including your own definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.3 Conditional compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.4 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.5 Lookup tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.6 What DOES> CREATE do? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.7 Fixed point calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.8 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.9 Forward declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.10 This is the end . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I Appendices 5 41 41 41 42 43 44 45 46 47 48 48 49 49 50 51 53 56 59 61 63 63 64 65 6 CONTENTS Chapter 1 Preface 1.1 Copyright Copyright (c) 2001 J.L. Bezemer. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with the Invariant Sections being ”GNU Free Documentation License”, ”Introduction and ”About this primer”, with the Front-Cover Texts being ”And so Forth..., J.L. Bezemer”, and with the Back-Cover Texts being ”The initial version of this primer was written by Hans Bezemer, author of the 4tH compiler.”. A copy of the license is included in the section entitled "GNU Free Documentation License". 1.2 Introduction Don’t you hate it? You’ve just got a new programming language and you’re trying to write your first program. You want to use a certain feature (you know it’s got to be there) and you can’t find it in the manual. I’ve had that experience many times. So in this manual you will find many short features on all kind of topics. How to input a number from the keyboard, what a cell is, etc. I hope this will enable you to get quickly on your way. If it didn’t, email me at ’hansoft@bigfoot.com’. You will not only get an answer, but you will help future Forth users as well. You can use this manual two ways. You can either just get what you need or work your way through. Every section builds on the knowledge you obtained in the previous sections. All sections are grouped into levels. We advise you to use what you’ve learned after you’ve worked your way through a level. If this isn’t enough to teach you Forth you can always get a real good textbook on Forth, like "Starting Forth" by Leo Brodie. Have fun! 1.3 About this primer This primer was originally written for 4tH, my own Forth compiler. 4tH isn’t ANS-Forth compliant, by ANS-Forth standards not even a ANS-Forth system. After a while I got questions why certain examples weren’t working. Since I tested every single one of them I wondered why. Until it dawned on me: people learning Forth were using my primer! 7 8 CHAPTER1. PREFACE So due to high demand I started to rewrite it for ANS-Forth compliant systems. Most of these systems don’t even have a manual at all so the need for it should be great. The next question was: which format. Since I wanted to learn LYX anyway, I settled for that. You can produce various formats with it which are readable on most systems, including MS-DOS, MS-Windows and Linux. The next question was: how far do you go. The original version was heavily geared towards 4tH, which reflects my own views on Forth. And those views sometimes contradict to those of ANS-Forth. However, since Leo Brodie took the liberty in ”Thinking Forth” to express his views, I thought I should have the freedom to express mine. Some examples, especially in the ”Advanced topics” chapter, use special 4tH extensions. Fortunately Wil Baden had helped me to write a 4tH-to-ANS-Forth interface. Since some of these extensions cover functionalities commonly found in other languages I decided to keep those sections in, using the Easy4tH definitions. In the previous chapters you’ll find some 4tH words as well, but very sparingly. You may find that some examples are not working with your specific Forth compiler. That may have several reasons. First, your compiler may not support all ANS-Forth wordsets. Second, your compiler may not be completely ANS-Forth compliant. I’ve tested most of these examples with GForth or Win32Forth, which are (almost) 100% ANS-Forth compliant. Third, your compiler might be case-sensitive. The ANS-Forth standard is a very important document. I can only advise you to get it. You should have no trouble finding it on the internet. I can only hope that the compiler you chose at least documented its ANS-Forth compatibility. This primer was written in the hope that it will be useful and that starting Forthers aren’t put off by the high price of Forth textbooks. It is dedicated to Leo Brodie, who taught me much more than just Forth. Hans Bezemer Den Haag, 2001-03-07 Chapter 2 Forth fundamentals 2.1 Making calculations without parenthesis To use Forth you must understand Reverse Polish Notation. This is a way to write arithmetic expressions. The form is a bit tricky for people to understand, since it is geared towards making it easy for the computer to perform calculations; however, most people can get used to the notation with a bit of practice. Reverse Polish Notation stores values in a stack. A stack of values is just like a stack of books: one value is placed on top of another. When you want to perform a calculation, the calculation uses the top numbers on the stack. For example, here’s a typical addition operation: 1 2 + When Forth reads a number, it just puts the value onto the stack. Thus 1 goes on the stack, then 2 goes on the stack. When you put a value onto the stack, we say that you push it onto the stack. When Forth reads the operator ’+’, it takes the top two values off the stack, adds them, then pushes the result back onto the stack. This means that the stack contains: 3 after the above addition. As another example, consider: 2 3 4 + * (The ’*’ stands for multiplication.) Forth begins by pushing the three numbers onto the stack. When it finds the ’+’, it takes the top two numbers off the stack and adds them. (Taking a value off the stack is called popping the stack.) Forth then pushes the result of the addition back onto the stack in place of the two numbers. Thus the stack contains: 2 7 When Forth finds the ’*’ operator, it again pops the top two values off the stack. It multiplies them, then pushes the result back onto the stack, leaving: 14 9 10 CHAPTER2. FORTHFUNDAMENTALS The following list gives a few more examples of Reverse Polish expressions. After each, we show the contents of the stack, in parentheses. 7 2 - 2 7 - 12 3 / -12 3 / 4 5 + 2 * 4 5 2 + * 4 5 2 * - (5) (-5) (4) (-4) (18) (28) (-6) 2.2 Manipulating the stack You will often find that the items on the stack are not in the right order or that you need a copy. There are stack-manipulators which can take care of that. To display a number you use ’.’, pronounced "dot". ’SWAP’ reverses the order of two items on the stack. If we enter: It takes a number from the stack and displays it. 2 3 . . cr Forth answers: 3 2 If you want to display the numbers in the same order as you entered them, you have to enter: 2 3 swap . . cr In that case Forth will answer: 2 3 You can duplicate a number using ’DUP’. If you enter: 2 . . cr Forth will complain that the stack is empty. However, if you enter: 2 dup . . cr Forth will display: 2 2 Another way to duplicate a number is using ’OVER’. In that case not the topmost number of the stack is duplicated, but the number beneath. E.g. 2 3 dup . . . cr 2.3. DEEPSTACKMANIPULATORS 11 will give you the following result: 3 3 2 But this one: 2 3 over . . . cr will give you: 2 3 2 Sometimes you want to discard a number, e.g. you duplicated it to check a condition, but since the test failed, you don’t need it anymore. ’DROP’ is the word we use to discard numbers. So this: 2 3 drop . will give you "2" instead of "3", since we dropped the "3". The final one I want to introduce is ’ROT’. Most users find ’ROT’ the most complex one since it has its effects deep in the stack. The thirdmost item to be exact. This item is taken from its place and put on top of the stack. It is ’rotated’, as this small program will show you: 1 2 3 . . . cr ( This will display ’3 2 1’ as expected) 1 2 3 rot . . . cr ( This will display ’1 3 2’!) \ 1 is the thirdmost item \ display all numbers \ same numbers stacked \ performs a ’ROT’ \ same operation 2.3 Deep stack manipulators There are two manipulators that can dig deeper into the stack, called ’PICK’ and ’ROLL’ but I cannot recommend them. A stack is NOT an array! So if there are some Forth-83 users out there, I can only tell you: learn Forth the proper way. Programs that have so many items on the stack are just badly written. Leo Brodie agrees with me. If you are in ’deep’ trouble you can always use the returnstack manipulators. Check out that section. 2.4 Pass arguments to functions There is no easier way to pass arguments to functions as in Forth. Functions have another name in Forth. We call them "words". Words take their "arguments" from the stack and leave the "result" on the stack. Other languages, like C, do exactly the same. But they hide the process from you. Because passing data to the stack is made explicit in Forth it has powerful capabilities. In other languages, you can get back only one result. In Forth you can get back several! All words in Forth have a stack-effect-diagram. It describes what data is passed to the stack in what order and what is returned. The word ’*’ for instance takes numbers from the stack, multiplies them and leaves the result on the stack. It’s stack-effect-diagram is: 12 CHAPTER2. FORTHFUNDAMENTALS n1 n2 -- n3 Meaning it takes number n1 and n2 from the stack, multiplies them and leaves the product (number n3) on the stack. The rightmost number is always on top of the stack, which means it is the first number which will be taken from the stack. The word ’.’ is described like this: n -- Which means it takes a number from the stack and leaves nothing. Now we get to the most powerful feature of it all. Take this program: 2 3 * . ( leaves a number on the stack) ( leaves a number on the stack on top of the 2) ( takes both from the stack and leaves the result) ( takes the result from the stack and displays it) Note that all data between the words ’*’ and ’.’ is passed implicitly! Like putting LEGO stones on top of another. Isn’t it great? 2.5 Making your own words Of course, every serious language has to have a capability to extend it. So has Forth. The only thing you have to do is to determine what name you want to give it. Let’s say you want to make a word which multiplies two numbers and displays the result. Well, that’s easy. We’ve already seen how you have to code it. The only words you need are ’*’ and ’.’. You can’t name it ’*’ because that name is already taken. You could name it ’multiply’, but is that a word you want to type in forever? No, far too long. Let’s call it ’*.’. Is that a valid name? If you’ve programmed in other languages, you’ll probably say it isn’t. But it is! The only characters you can’t use in a name are whitespace characters (<CR>, <LF>, <space>, <TAB>). It depends on the Forth you’re using whether it is case- sensitive or not, but usually it isn’t. So ’*.’ is okay. Now how do we turn it into a self-defined word. Just add a colon at the beginning and a semi-colon at the end: : *. * . ; That’s it. Your word is ready for use. So instead of: 2 3 * . We can type: : *. * . ; 2 3 *. And we can use our ’*.’ over and over again. Hurray, you’ve just defined your first word in Forth! 2.6. ADDINGCOMMENT 2.6 Adding comment 13 Adding comment is very simple. In fact, there are two ways to add comment in Forth. That is because we like programs with a lot of comment. The first form you’ve already encountered. Let’s say we want to add comment to this little program: : *. * . ; 2 3 *. So we add our comment: : *. * . ; 2 3 *. This will multiply and print two numbers Forth will not understand this. It will desperately look for the words ’this’, ’will’, etc. However the word ’\’ will mark everything up to the end of the line as comment. So this will work: : *. * . ; 2 3 *. \ This will multiply and print two numbers There is another word called ’(’ which will mark everything up to the next ’)’ as comment. Yes, even multiple lines. Of course, these lines may not contain a ’)’ or you’ll make Forth very confused. So this comment will be recognized too: : *. * . ; 2 3 *. ( This will multiply and print two numbers) Note that there is a whitespace-character after both ’\’ and ’(’. This is mandatory! 2.7 Text-format of Forth source Forth source can be simple ASCII-files. And you can use any layout as long a this rule is followed: All words are separated by at least one whitespace character! Well, in Forth everything is a word or becoming a word. Yes, even ’\’ and ’(’ are words! And you can add all the empty lines or spaces or tabs you like, Forth won’t care and your harddisk supplier either. However, some Forths still use a special line editor, which works with screens. Screens are usually 1K blocks, divided into 16 lines of 64 characters. Explaining how these kind of editors work goes beyond the scope of this manual. You have to check the documentation of your Forth compiler on that. The files these editors produce are called blockfiles. 14 CHAPTER2. FORTHFUNDAMENTALS 2.8 Displaying string constants Displaying a string is as easy as adding comment. Let’s say you want to make the ultimate program, one that is displaying "Hello world!". Well, that’s almost the entire program. The famous ’hello world’ program is simply this in Forth: .( Hello world!) Enter this and it works. Yes, that’s it! No declaration that this is the main function and it is beginning here and ending there. May be you think it looks funny on the display. Well, you can add a carriage return by adding the word ’CR’. So now it looks like: .( Hello world!) cr Still pretty simple, huh? 2.9 Declaring variables One time or another you’re going to need variables. Declaring a variable is easy. variable one The same rules for declaring words apply for variables. You can’t use a name that already has been taken or you’ll get pretty strange results. A variable is a word too! And whitespace characters are not allowed. Note that Forth is usually not case-sensitive! 2.10 Using variables Of course variables are of little use when you can’t assign values to them. This assigns the number 6 to variable ’ONE’: 6 one ! We don’t call ’!’ bang or something like that, we call it ’store’. Of course you don’t have to put a number on the stack to use it, you can use a number that is already on the stack. To retrieve the value stored in ’ONE’ we use: one @ The word ’@’ is called ’fetch’ and it puts the number stored in ’one’ on the stack. To display it you use ’.’: one @ . There is a shortcut for that, the word ’?’, which will fetch the number stored in ’ONE’ and displays it: one ? 2.11. BUILT-INVARIABLES 2.11 Built-in variables 15 Forth has two built-in variables you can use for your own purposes. They are called ’BASE’ and ’>IN’. ’BASE’ controls the radix at run-time, ’>IN’ is used by ’WORD’ and ’PARSE’. 2.12 What is a cell? A cell is simply the space a number takes up. So the size of a variable is one cell. The size of a cell is important since it determines the range Forth can handle. We’ll come to that further on. 2.13 Declaring and using constants Declaring a simple constant is easy too. Let’s say we want to make a constant called ’FIVE’: 5 constant five Now you can use ’FIVE’ like you would ’5’. E.g. this will print five spaces: five spaces The same rules for declaring words apply for constants. You shouldn’t use a name that already has been taken. A constant is a word too! And whitespace characters are not allowed. Note that Forth is usually not case-sensitive. 2.14 Built-in constants There are several built-in constants. Of course, they are all literals in case you wonder. Here’s a list. Refer to the glossary for a more detailed description: 1. BL 2. FALSE 3. PAD 4. TIB 5. TRUE 2.15 Using booleans Booleans are expressions or values that are either true or false. They are used to conditionally execute parts of your program. In Forth a value is false when it is zero and true when it is non-zero. Most booleans come into existence when you do comparisons. This one will determine whether the value in variable ’VAR’ is greater than 5. Try to predict whether it will evaluate to true or false: variable var 4 var ! var @ 5 > . No, it wasn’t! But hey, you can print booleans as numbers. Well, they are numbers. But with a special meaning as we will see in the next section. 16 CHAPTER2. FORTHFUNDAMENTALS 2.16 IF-ELSE constructs Like most other languages you can use IF-ELSE constructs. Let’s enhance our previous example: variable var 4 var ! : test var @ 5 > if ." Greater" cr else ." Less or equal" cr then ; test So now our program does the job. It tells you when it’s greater and when not. Note that contrary to other languages the condition comes before the ’IF’ and ’THEN’ ends the IF-clause. In other words, whatever path the program takes, it always continues after the ’THEN’. A tip: think of ’THEN’ as ’ENDIF’.. 2.17 FOR-NEXT constructs Forth does also have FOR-NEXT constructs. The number of iterations is known in this construct. E.g. let’s print the numbers from 1 to 10: : test 11 1 do i . cr loop ; test The first number presents the limit. When the limit is goes beyond the limit minus one the loop terminates. The second number presents the initial value of the index. That’s where it starts of. So remember, this loop iterates at least once! You can use ’?DO’ instead of ’DO’. That will not enter the loop if the limit and the index are the same to begin with: : test 0 0 ?do i . cr loop ; test ’I’ represents the index. It is not a variable or a constant, it is a predefined word, which puts the index on the stack, so ’.’ can get it from the stack and print it. But what if I want to increase the index by two? Or want to count downwards? Is that possible. Sure. There is another construct to do just that. Okay, let’s take the first question: : test 11 1 do i . cr 2 +loop ; test 2.18. WHILE-DOCONSTRUCTS 17 This one will produce exactly what you asked for. An increment by two. This one will produce all negative numbers from -1 to -11: : test -11 -1 do i . cr -1 +loop ; test Why -11? Because the loop terminates when it reached the limit minus one. And when you’re counting downward, that is -11. You can change the step if you want to, e.g.: : test 32767 1 do i . i +loop ; test This will print: 1, 2, 4, 8, all up to 16384. Pretty flexible, I guess. You can break out of a loop by using ’LEAVE’: : test 10 0 do i dup 5 = if drop leave else . cr then loop : test 2.18 WHILE-DO constructs A WHILE-DO construction is a construction that will perform zero or more iterations. First a condition is checked, then the body is executed. Then it will branch back to the condition. In Forth it looks like this: BEGIN <condition> WHILE <body> REPEAT The condition will have to evaluate to TRUE in order to execute the body. If it evaluates to FALSE it branches to just after the REPEAT. This example does a Fibbonaci test. : fib 0 1 begin while dup >r rot dup r> > \ condition rot rot dup rot + dup . \ body repeat drop drop drop ; \ after loop has executed You might not understand all of the commands, but we’ll get to that. If you enter "20 fib" you will get: 1 2 3 5 8 13 21 This construct is particularly handy if you are not sure that all data will pass the condition. 18 CHAPTER2. FORTHFUNDAMENTALS 2.19 REPEAT-UNTIL constructs The counterpart of WHILE-DO constructs is the REPEAT-UNTIL construct. This executes the body, then checks a condition at ’UNTIL’. If the expression evaluates to FALSE, it branches back to the top of the body (marked by ’BEGIN’) again. It executes at least once. This program calculates the largest common divisor. : lcd begin swap over mod dup 0= until drop . ; \ body \ condition If you enter "27 21 lcd" the programs will answer "3". 2.20 Infinite loops In order to make an infinite loop one could write: : test begin ." Diamonds are forever" cr 0 until ; test But there is a nicer way to do just that: : test begin ." Diamonds are forever" cr again ; test This will execute until the end of times, unless you exit the program another way. 2.21 Getting a number from the keyboard Let’s start with "you’re not supposed to understand this". If you dig deeper in Forth you’ll find out why it works the way it works. But if you define this word in your program it will read a number from the keyboard and put it on the stack. If you haven’t entered a valid number, it will prompt you again. S" MAX-N" ENVIRONMENT? [IF] NEGATE 1- CONSTANT (ERROR) [THEN] \ query environment \ if successful \ create constant (ERROR) : number ( a -- n) 0. Rot dup 1+ c@ [char] - = >r count r@ if 1 /string then >number nip 2.22. ALIGNINGNUMBERS 19 0= if d>s r> if negate then else r> drop 2drop (error) then ; : input# begin refill drop bl word number dup (error) <> dup 0= if swap drop then until ; ( -- n) ( n) ( n f) ( n f -f) ( f | n f) 2.22 Aligning numbers You may find that printing numbers in columns (I prefer "right-aligned") can be pretty hard. That is because the standard word to print numbers (’.’) prints the number and then a trailing space. That is why ’.R’ was added. The word ’.R’ works just like ’.’ but instead of just printing the number with a trailing space ’.R’ will print the number right-aligned in a field of N characters wide. Try this and you will see the difference: 140 . cr 150 5 .r cr In this example the field is five characters wide, so ’150’ will be printed with two leading spaces. 20 CHAPTER2. FORTHFUNDAMENTALS Chapter 3 Arrays and strings 3.1 Declaring arrays of numbers You can make arrays of numbers very easily. It is very much like making a variable. Let’s say we want an array of 16 numbers: create sixteen 16 cells allot That’s it, we’re done! 3.2 Using arrays of numbers You can use arrays of numbers just like variables. The array cells are numbered from 0 to N, N being the size of the array minus one. Storing a value in the 0th cell is easy. It works just like a simple variable: 5 sixteen 0 cells + ! Which will store ’5’ in the 0th cell. So storing ’7’ in the 8th cell is done like this: 7 sixteen 8 cells + ! Isn’t Forth wonderful? Fetching is done the same of course: sixteen 0 cells + @ sixteen 4 cells + @ Plain and easy. 3.3 Creating arrays of constants Making an array of constants is quite easy. First you have to define the name of the array by using the word ’CREATE. Then you specify all its elements. All elements (even the last) are terminated by the word ’,’. An example: create sizes 18 , 21 , 24 , 27 , 30 , 255 , Please note that ’,’ is a word! It has to be separated by spaces on both ends. 21 22 CHAPTER3. ARRAYSANDSTRINGS 3.4 Using arrays of constants Accessing an array of constants is exactly like accessing an array of numbers. In an array of numbers you access the 0th element like this: sixteen 0 cells + @ When you access the first element of an array of constants you use this construction: sizes 0 cells + @ So I don’t think you’ll have any problems here. 3.5 Creating strings In Forth you have to define the maximum length of the string, like Pascal: create name 10 chars allot Note that the string variable includes the count byte. That is a special character that tells Forth how long a string is. You usually don’t have to add that yourself because Forth will do that for you. But you will have to reserve space for it. That means that the string "name" we just declared can contain up to nine characters *AND* the count byte. These kind of strings are usually referred to as counted strings. E.g. when you want to define a string that has to contain "Hello!" (without the quotes) you have to define a string that is at least 7 characters long: create hello 7 chars allot When you later refer to the string you just defined its address is thrown on the stack. An address is simply a number that refers to its location. As you will see you can work with string-addresses without ever knowing what that number is. But because it is a number you can manipulate it like any other number. E.g. this is perfectly valid: hello dup drop drop \ address of string on stack \ duplicate it \ drop them both In the next section we will tell you how to get "Hello!" into the string. 3.6 Initializing strings You can initialize a string with the ’S"’ word. You haven’t seen this one yet, but we will discuss it in more depth later on. If you want the string to contain your first name use this construction: 3.7. GETTINGTHELENGTHOFASTRING 23 : place over over >r >r char+ swap chars cmove r> r> c! ; create name 16 chars allot s" Hello! " name place The word ”PLACE”, which is a common word1, copies the contents of a string constant into a string- variable. If you still don’t understand it yet, don’t worry. As long as you use this construction, you’ll get what you want. Just remember that assigning a string constant to a string that is too short will result in an error or even worse, corrupt other strings. 3.7 Getting the length of a string You get the length of a string by using the word ’COUNT’. It will not only return the length of the string, but also the string address. It is illustrated by this short program: : place over over >r >r char+ swap chars cmove r> r> c! ; create greeting 32 chars allot S" Hello!" greeting place greeting count .( String length: ) . cr drop \ define string greeting \ set string to ’Hello!’ \ get string length \ print the length \ discard the address You usually have nothing to do with the string address. However, it may be required by other words like we will see in the following section. If you just want the bare length of the string you can always define a word like ’length$’: : place over over >r >r char+ swap chars cmove r> r> c! ; : length$ count swap drop ; create greeting 32 cells allot s" Hello!" greeting place greeting length$ .( String length: ) . cr \ define string greeting \ set string to ’Hello!’ \ get string length \ print the length 3.8 Printing a string variable Printing a string variable is pretty straight forward. The word that is required to print a string variable is ’TYPE’. It requires the string address and the number of characters it has to print. Yes, that are the values that are left on the stack by ’COUNT’! So printing a string means issuing both ’COUNT’ and ’TYPE’: : place over over >r >r char+ swap chars cmove r> r> c! ; create greeting 32 cells allot s" Hello!" greeting place greeting count type cr \ define string greeting \ set string to ’Hello!’ \ print the string If you don’t like this you can always define a word like ’PRINT$’: 1Although not part of the ANS-Forth standard. 24 CHAPTER3. ARRAYSANDSTRINGS : place over over >r >r char+ swap chars cmove r> r> c! ; : print$ count type ; create greeting 32 cells allot s" Hello!" greeting place greeting print$ cr \ define string greeting \ set string to ’Hello!’ \ print the string 3.9 Copying a string variable You might want to copy one string variable to another. There is a special word for that, named ’CMOVE’. It takes the two strings and copies a given number of characters from the source to the destination. Let’s take a look at this example: : place over over >r >r char+ swap chars cmove r> r> c! ; create one 16 chars allot create two 16 chars allot \ define the first string \ define the second string s" Greetings!" one place one dup count 1+ swap drop two swap cmove two count type cr \ initialize string one \ save the real address \ get the length of string one \ account for the count byte \ get the real address \ get the order right \ copy the string \ print string two The most difficult part to understand is probably why and how to set up the data for ’CMOVE’. Well, ’CMOVE’ wants to see these values on the stack: source destination #chars With the expression: one count We get these data on the stack: source+1 length But the count byte hasn’t been accounted for so far. That’s why we add: 1+ So now this parameter has the right value. Now we have to restore the true address of the string and tell ’CMOVE’ where to copy the contents of string one to. Initially, we got the correct address. That is why we saved it using: dup 3.10. SLICINGSTRINGS 25 Now we’re getting rid of the "corrupted" address by issuing: swap drop This is what we got right now: source #chars If we simply add: two The data is still not presented in the right order: source #chars destination So we add the extra ’SWAP’ in order to get it right. Of course you may define a word that takes care of all that: : place over over >r >r char+ swap chars cmove r> r> c! ; : copy$ swap dup count 1+ swap drop rot swap cmove ; create one 32 chars allot create two 32 chars allot s" Greetings!" one place one two copy$ You may wonder why we keep on defining words to make your life easier. Why didn’t we simply define these words in the compiler instead of using these hard to understand words? Sure, but I didn’t write the standard. However, most Forths allow you to permanently store these words in their vocabulary. Check your documentation for details. 3.10 Slicing strings Slicing strings is just like copying strings. We just don’t copy all of it and we don’t always start copying at the beginning of a string. We’ll show you what we mean: : place over over >r >r char+ swap chars cmove r> r> c! ; : nextchar dup dup c@ 1- swap char+ c! char+ ; create one 32 chars allot s" Hans Bezemer" one place one dup count type cr nextchar dup count type cr nextchar dup count type cr nextchar count type cr \ define string one \ initialize string one \ duplicate and print it \ move one character forward \ duplicate and print it again \ move one character forward \ duplicate and print it again \ move one character forward \ print it for the last time 26 CHAPTER3. ARRAYSANDSTRINGS First it will print "Hans Bezemer", then "ans Bezemer", then "ns Bezemer" and finally "s Bezemer". The word CHAR+ is usually equivalent to 1+, but Forth was defined to run on unusual hardware too - the CPU of a pocket calculator could be a nibble-machine (4-bit) so each CHAR occupies in fact two addresses. And of course, some Forth systems may treat CHAR to be a 16-bit unicode. If we want to discard the first name at all we could even write: : place over over >r >r char+ swap chars cmove r> r> c! ; create one 32 chars allot s" Hans Bezemer" one place one dup c@ 5 - swap 5 chars + dup rot swap c! count type cr \ define string one \ initialize string one \ copy address and get count \ save new count \ print sliced string The five characters we want to skip are the first name (which is four characters) and a space (which adds up to five). There is no special word for slicing strings. There is a smarter way to handle strings in Forth, which we will discuss later on. But if you desperately need slicing you might want to use a word like this. It works just like ’CMOVE’ with an extra parameter: : slice$ swap over over >r >r >r >r + r> r> char+ swap cmove r> r> c! ; \ reverse dest and #chars \ copy the dest and #chars \ store on the return stack \ make address to the source \ restore dest and #chars \ make address to destination \ copy the string \ restore dest and #chars \ save This is another example of "you’re not supposed to understand this". You call it with: source index-to-source destination #chars The index-to-source starts counting at one. So this will copy the first name to string "two" and print it: : place over over >r >r char+ swap chars cmove r> r> c! ; : slice$ swap over over >r >r >r >r + r> r> char+ swap cmove r> r> c! ; \ reverse dest and #chars \ copy the dest and #chars \ store on the return stack \ make address to the source \ restore dest and #chars \ make address to destination \ copy the string \ restore dest and #chars \ save create one 32 chars allot \ declare string one 3.11. APPENDINGSTRINGS 27 create two 32 chars allot s" Hans Bezemer" one place one 1 two 4 slice$ two count type cr \ declare string two \ initialize string one \ slice the first name \ print string two This will slice the last name off and store it in string "two": : place over over >r >r char+ swap chars cmove r> r> c! ; : slice$ swap over over >r >r >r >r + r> r> char+ swap cmove r> r> c! ; \ reverse dest and #chars \ copy the dest and #chars \ store on the return stack \ make address to the source \ restore dest and #chars \ make address to destination \ copy the string \ restore dest and #chars \ save create one 32 chars allot create two 32 chars allot s" Hans Bezemer" one place one 6 two 7 slice$ two count type cr \ declare string one \ declare string two \ initialize string one \ slice the first name \ print string two Since the last name is seven characters long and starts at position six (start counting with one!). Although this is very "Basic" way to slice strings, we can do this kind of string processing the Forth way. It will probably require less stack manipulations. 3.11 Appending strings There is no standard word in Forth to concatenate strings. As a matter of fact, string manipulation is one of Forths weakest points. But since we are focused here on doing things, we will present you a word which will get the work done. The word ’APPEND’ appends two strings. In this example string "one" holds the first name and ”Bezemer” is appended to string "one" to form the full name. Finally string "one" is printed. : place over over >r >r char+ swap chars cmove r> r> c! ; : append over over >r >r count chars + swap chars move r> r> dup >r c@ + r> c! ; ( a1 n2 a2 --) \ duplicate target and count \ save them on the return stack \ calculate offset target \ now move the source string \ get target and count \ duplicate target and save one \ calculate new count \ get address and store 28 CHAPTER3. ARRAYSANDSTRINGS create one 32 chars allot s" Hans " one place s" Bezemer" one append one count type cr \ define string one \ initialize first string \ append ’Bezemer’ to string \ print first string Of course, you can also fetch the string to be appended from a string variable by using ’COUNT’. 3.12 Comparing strings If you ever sorted strings you know how indispensable comparing strings is. As we mentioned before, there are very few words in Forth that act on strings. But here is a word that can compare two strings. : place over over >r >r char+ swap chars cmove r> r> c! ; create one 32 chars allot create two 32 chars allot \ define string one \ define string two : test s" H. Bezemer" one place s" R. Bezemer" two place \ initialize string one \ initialize string two one count two count compare if \ compare two strings ." Strings differ" \ message: strings ok else ." Strings are the same" \ message: strings not ok then cr ; test \ send CR Simply pass two strings to ’COMPARE’ and it will return a TRUE flag when the strings are different. This might seem a bit odd, but strcmp() does exactly the same. If you don’t like that you can always add ’0=’ to the end of ’COMPARE’ to reverse the flag. 3.13 Removing trailing spaces You probably know the problem. The user of your well-made program types his name and hits the spacebar before hitting the enter-key. There you go. His name will be stored in your datafile with a space and nobody will ever find it. In Forth there is a special word called ’-TRAILING’ that removes the extra spaces at the end with very little effort. Just paste it after ’COUNT’. Like we did in this example: : place over over >r >r char+ swap chars cmove r> r> c! ; create one 32 chars allot s" Hans Bezemer " \ define a string \ string with trailing spaces 3.14. STRINGCONSTANTSANDSTRINGVARIABLES 29 one place one dup ." [" count type ." ]" cr \ now copy it to string one \ save the address \ print a bracket \ old method of printing \ print bracket and newline ." [" count -trailing type ." ]" cr \ print a bracket \ new method of printing \ print a bracket and newline You will see that the string is printed twice. First with the trailing spaces, second without trailing spaces. And what about leading spaces? Patience, old chap. You’ve got a lot of ground to cover. 3.14 String constants and string variables Most computer languages allow you to mix string constants and string variables. Not in Forth. In Forth they are two distinct datatypes. To print a string constant you use the word ’."’. To print a string variable you use the ’COUNT TYPE’ construction. There are only two different actions you can do with a string constant. First, you can define one using ’s"’. Second, you can print one using ’."’. There are two different ways to represent a string variable in Forth. First, by using just its address, the so-called counted string. Forth relies on the count byte to find the end of the string. Second, by using its address and its length. This requires two values. The word ’TYPE’ requires the latter form. Therefore, you have to convert a counted string in order to print it. You can convert an counted string to an "address-count string" with the word ’COUNT’. If you moved a string (by using ’CMOVE’) without taking the count byte into account you have to set it yourself. This may seem a bit mind-boggling to you now, but we’ll elaborate a bit further on this subject in the following sections. 3.15 The count byte The count byte is used to set the length of a counted string. It has nothing to do with British royalty! It is simply the very first byte of a string, containing the length of the actual string following it. 3.16 Printing individual characters "I already know that!" Sure you do. If you want to print "G" you simply write: .( G) Don’t you? But what if you want to use a TAB character (ASCII 9)? You can’t type in that one so easily, huh? You may even find it doesn’t work at all! Don’t ever use characters outside the ASCII range 32 to 127 decimal. It may or may not work, but it won’t be portable anyway. the word ’EMIT’ may be of some help. If you want to use the TAB-character simply write: CHAPTER3. ARRAYSANDSTRINGS 30 9 emit That works! 3.17 Getting ASCII values Ok, ’EMIT’ is a nice addition, but it has its drawbacks. What if you want to emit the character "G". Do you have to look up the ASCII value in a table? No. Forth has another word that can help you with that. It is called ’CHAR’. This will emit a "G": char G emit The word ’CHAR’ looks up the ASCII-value of "G" and leave it on the stack. Note that ’CHAR’ only works with printable characters (ASCII 33 to 127 decimal). 3.18 When to use [CHAR] or CHAR There is not one, but two words for getting the ASCII code of a character, ’[CHAR]’ and ’CHAR’. Why is that? Well, the complete story is somewhat complex, but one is for use inside colon definitions and one is for use outside colon definitions. And ’CHAR’ isn’t the only word which is affected. We’ve put it all together in a neat table for you: INSIDE A DEFINITION OUTSIDE A DEFINITION .( .” CHAR [CHAR] ’ [’] For example, this produces the same results: : Hello ." Hello world" [char] ! emit cr ; Hello .( Hello world!) char ! emit cr You should also have noticed in the meanwhile that you can’t use control structures like DO..LOOP or IF..THEN outside colon definitions. And not only these, others like ’C”” can’t be used as well. Real Forth- ers call this ”inside a colon definition” thing compilation mode and working from the prompt interpretation mode. You can do really neat things with it, but that is still beyond you now. 3.19 Printing spaces If you try to print a space by using this construction: char emit You will notice it won’t work. Sure, you can also use: .( ) 3.20. FETCHINGINDIVIDUALCHARACTERS 31 But that isn’t too elegant. You can use the built-in constant ’BL’ which holds the ASCII-value of a space: bl emit That is much better. But you can achieve the same thing by simply writing: space Which means that if you want to write two spaces you have to write: space space If you want to write ten spaces you either have to repeat the command ’SPACE’ ten times or use a DO- LOOP construction, which is a bit cumbersome. Of course, Forth has a more elegant solution for that: 10 spaces Which will output ten spaces. Need I say more? 3.20 Fetching individual characters Take a look at this small program: : place over over >r >r char+ swap chars cmove r> r> c! ; create one 32 chars allot s" Hans" one place \ define string one \ initialize string one What is the second character of string "one"? Sure, its an "a". But how can you let your program determine that? You can’t use ’@’ because that word can only access variables. Sure, you can do that in Forth, but it requires a new word, called ’C@’. Think of a string as an array of characters and you will find it much easier to picture the idea. Arrays in Forth always start with zero instead of one, but that is the count byte. So accessing the first character might be done with: one 1 chars + c@ This is the complete program: : place over over >r >r char+ swap chars cmove r> r> c! ; create one 32 chars allot s" Hans" one place one 2 chars + c@ emit cr \ define string one \ initialize string one \ get the second character \ print it 32 CHAPTER3. ARRAYSANDSTRINGS 3.21 Storing individual characters Storing individual characters works just the same. Keep that array of characters in mind. When we want to fetch a variable we write: my_var @ When we want to store a value in a variable we write: 5 my_var ! Fetching only requires the address of the variable. Storing requires both the address of the variable *AND* the value we want to store. On top of the stack is the address of the variable, below that is value we want to store. Keep that in mind, this is very important. Let’s say we have this program: : place over over >r >r char+ swap chars cmove r> r> c! ; create one 32 chars allot s" Hans" one place \ define string one \ initialize string one Now we want to change "Hans" to "Hand". If we want to find out what the 4th character of string "one" is we write: : place over over >r >r char+ swap chars cmove r> r> c! ; create one 32 chars allot s" Hans" one place one 4 chars + c@ \ define string one \ initialize string one \ get the fourth character Remember, we start counting from one! If we want to store the character "d" in the fourth character, we have to use a new word, and (yes, you guessed it right!) it is called ’C!’: : place over over >r >r char+ swap chars cmove r> r> c! ; create one 32 chars allot s" Hans" one place one 4 chars + char d swap c! \ define string one \ initialize string one \ address of the fourth char \ we want to store ’d’ \ get the order right \ now store ’d’ If we throw the character "d" on the stack before we calculate the address, we can even remove the ’SWAP’: : place over over >r >r char+ swap chars cmove r> r> c! ; create one 32 chars allot char d s" Hans" one place one 4 chars + c! \ define string one \ we want to store ’d’ \ initialize string one \ address of the fourth char \ now store ’d’ 3.22. GETTINGASTRINGFROMTHEKEYBOARD 33 We will present the very same programs, but now with stack-effect-diagrams in order to explain how this works. We will call the index ’i’, the character we want to store ’c’ and the address of the string ’a’. By convention, stack-effect-diagrams are enclosed by parenthesis. If you create complex programs this technique can help you to understand more clearly how your program actually works. It might even save you a lot of debugging. This is the first version: : place over over >r >r char+ swap chars cmove r> r> c! ; create one 32 chars allot s" Hans" one place one 4 chars + char d swap c! ( --) ( --) ( a) ( a i) ( a+i) ( a+i c) ( c a+i) ( --) Now the second, optimized version: : place over over >r >r char+ swap chars cmove r> r> c! ; create one 32 chars allot char d s" Hans" one place one 4 chars + c! ( --) ( c) ( c) ( c a) ( c a i) ( c a+i) ( --) 3.22 Getting a string from the keyboard Of course, you don’t want to initialize strings all your life. Real applications get their input from the keyboard. We’ve already shown you how to get a number from the keyboard. Now we turn to strings. When programming in BASIC, strings usually have an undefined length. Some BASICs move strings around in memory, others have to perform some kind of "garbage-collection". Whatever method they use, it takes up memory and processor-time. Forth forces you to think about your application. E.g. when you want to store somebodies name in a string variable, 16 characters will be too few and 256 characters too many. But 64 characters will probably do. But that poses a problem when you want to get a string from the keyboard. How can you prevent that somebody types a string that is just too long? The word ’ACCEPT’ takes two arguments. First, the string variable where you want to save the input and second, the maximum number of characters it can take. But there is a catch. This program can get you into trouble: 64 constant #name create name #name chars allot \ length of string \ define string ’name’ name #name accept name 1+ swap type cr \ input string \ swap count and print 34 CHAPTER3. ARRAYSANDSTRINGS Since 64 characters *PLUS* the count byte add up to 65 characters. You will probably want to use this definition instead: : saccept 1- swap 1+ swap accept ; \ define safe ’ACCEPT’ 64 constant #name create name #name chars allot \ length of string \ define string ’name’ name #name saccept name 1+ swap type cr \ input string \ print string This "safe" version decrements the count so the user input will fit nicely into the string variable. In order to terminate it you write: : saccept 1- swap 1+ swap accept ; \ define safe ’ACCEPT’ 64 constant #name create name #name chars allot \ length of string \ define string ’name’ name dup #name saccept swap c! \ input string \ set count byte The word ’ACCEPT’ always returns the number of characters it received. This is the end of the second level. Now you should be able to understand most of the example programs and write simple ones. I suggest you do just that. Experience is the best teacher after all. 3.23 What is the TIB? The TIB stands for "Terminal Input Buffer" and is used by one single, but very important word called ’REFILL’. In essence, ’REFILL’ does the same thing as ’ACCEPT’, except that it has a dedicated area to store its data and sets up everything for parsing. Whatever you type when you call ’REFILL’, it is stored in the TIB. 3.24 What is the PAD? The PAD is short for "scratch-pad". It is a temporary storage area for strings. It is heavily used by Forth itself, e.g. when you print a number the string is formed in the PAD. Yes, that’s right: when you print a number it is first converted to a string. Then that string is ’COUNT’ed and ’TYPE’d. You can even program that subsystem yourself as we will see when we encounter formatted numbers. 3.25 How do I use TIB and PAD? In general, you don’t. The TIB is a system-related area and it is considered bad practice when you ma- nipulate it yourself. The PAD can be used for temporary storage, but beware! Temporary really means temporary. A few words at the most, provided you don’t generate any output or do any parsing. Think of both these areas as predefined strings. You can refer to them as ’TIB’ and ’PAD’. You don’t have to declare them in any way. This program is perfectly alright: 3.26. TEMPORARYSTRINGCONSTANTS 35 : place over over >r >r char+ swap chars cmove r> r> c! ; s" Hello world" pad place pad count type cr \ store a string in pad \ print contents of the pad 3.26 Temporary string constants Hey, haven’t we already seen this? Yes, you have. s" This is a string" type cr No ’COUNT’? No. ’S"’ leaves its address and its length on the stack, so we can call ’TYPE’ right away. Note that this string doesn’t last forever. If you wait too long it will be overwritten. It depends on your system how long the string will last. 3.27 Simple parsing We have already discussed ’REFILL’ a bit. We’ve seen that it is closely related to ’ACCEPT’. ’REFILL’ returns a true flag if all is well. When you use the keyboard it usually is, so we can safely drop it, but we will encounter a situation where this flag comes in handy. If you want to get a string from the keyboard, you only have to type: refill drop \ get string from keyboard Every next call to ’REFILL’ will overwrite any previously entered string. So if you want to do something with that string you’ve got to get it out of there, usually to one of your own strings. But if accessing the TIB directly is not the proper way, what is? The use of ’REFILL’ is closely linked to the word ’WORD’, which is a parser. ’WORD’ looks for the delimiter, whose ASCII code is on the stack. If the string starts with the delimiter, it will skip this and all subsequent occurrences until it finds a string. Then it will look for the delimiter again and slice the string right there. It then copies the sliced string to PAD and returns its address. This extremely handy when you want to obtain filtered input. E.g. when you want to split somebodies name into first name, initials and lastname: Hans L. Bezemer Just use this program: : test ." Give first name, initials, lastname: " refill drop bl word ." First name: " count type cr bl word ." Initials count type cr bl word ." Last name : " \ get string from keyboard \ parse first name \ write message \ type first name \ parse initials \ write message \ type initials \ parse last name \ write message : " 36 CHAPTER3. ARRAYSANDSTRINGS count type cr \ write last name ; test You don’t have to parse the entire string with the same character. This program will split up an MS-DOS filename into its components: : test ." DOS filename: " refill drop cr \ input a DOS filename \ get rid of the flag [char] : word ." Drive: " count type ." :" cr \ parse drive begin while [char] \ word dup count 0<> \ print drive \ parse path \ if not a NULL string \ print path drop ." Path : " count type cr repeat drop drop \ parse again \ discard addresses ; test If ’WORD’ reaches the end of the string and the delimiter is still not found, it returns the remainder of that string. If you try to parse beyond the end of the string, it returns a NULL string. That is an empty string or, in other words, a string with length zero. Therefore, we checked whether the string had zero length. If it had, we had reached the end of the string and further parsing was deemed useless. 3.28 Converting a string to a number We now learned how to parse strings and retrieve components from them. But what if these components are numbers? Well, there is a way in Forth to convert a string to a number, but like every number-conversion routine it has to act on invalid strings. That is, strings that cannot be converted to a valid number. This implementation uses an internal error-value, called ’(ERROR)’. The constant ’(ERROR)’ is a strange number. You can’t negate it, you can’t subtract any number from it and you can’t print it. If ’NUMBER’ can’t convert a string it returns that constant. Forth has its own conversion word called ’>NUMBER’, but that is a lot harder to use. Let’s take a look at this program: S" MAX-N" ENVIRONMENT? [IF] NEGATE 1- CONSTANT (ERROR) [THEN] \ query environment \ if successful \ create constant (ERROR) : number 0. Rot dup 1+ c@ [char] - = >r count r@ if 1 /string then >number nip 0= if d>s r> if negate then else r> drop 2drop (error) then ; 3.29. CONTROLLINGTHERADIX 37 : test ." Enter a number: " refill drop bl word number dup (error) = if \ write prompt \ enter string \ parse string \ convert to a number \ test for valid number \ if not valid ." You didn’t enter a valid number!" drop cr \ print if valid ." The number was: " . cr else then ; test You first enter a string, then it parsed and ’WORD’ returns the address where that string is stored. ’NUM- BER’ tries to convert it. If ’NUMBER’ returns ’(ERROR)’ it wasn’t a valid string. Otherwise, the number it right on the stack, waiting to be printed. That wasn’t so hard, was it? 3.29 Controlling the radix If you are a programmer, you know how important this subject is to you. Sometimes, you want to print numbers in octal, binary or hex. Forth can do that too. Let’s take the previous program and alter it a bit: S" MAX-N" ENVIRONMENT? [IF] NEGATE 1- CONSTANT (ERROR) [THEN] \ query environment \ if successful \ create constant (ERROR) : number 0. Rot dup 1+ c@ [char] - = >r count r@ if 1 /string then >number nip 0= if d>s r> if negate then else r> drop 2drop (error) then ; : test ." Enter a number: " refill drop bl word number dup (error) = if \ write prompt \ enter string \ parse string \ convert to a number \ test for valid number \ if not valid ." You didn’t enter a valid number!" drop cr \ print if valid hex ." The number was: " . cr else then ; test We added the word ’HEX’ just before printing the number. Now the number will be printed in hexadecimal. Forth has a number of words that can change the radix, like ’DECIMAL’ and ’OCTAL’. They work in the same way as ’HEX’. 38 CHAPTER3. ARRAYSANDSTRINGS Forth always starts in decimal. After that you are responsible. Note that all radix control follows the flow of the program. If you call a self-defined word that alters the radix all subsequent conversion is done too in that radix: S" MAX-N" ENVIRONMENT? [IF] NEGATE 1- CONSTANT (ERROR) [THEN] \ query environment \ if successful \ create constant (ERROR) : number 0. Rot dup 1+ c@ [char] - = >r count r@ if 1 /string then >number nip 0= if d>s r> if negate then else r> drop 2drop (error) then ; : .hex hex . ; \ print a number in hex : test ." Enter a number: " refill drop bl word number dup (error) = if \ write prompt \ enter string \ parse string \ convert to a number \ test for valid number \ if not valid ." You didn’t enter a valid number!" drop cr else \ print if valid ." The number was: " .hex cr then ; test In this example not only that single number is printed in hex, but also all subsequent numbers will be printed in hex! A better version of the ".HEX" definition would be: : .hex hex . decimal ; Since that one resets the radix back to decimal. Words like ’HEX’ do not only control the output of a number, but the input of numbers is also affected: S" MAX-N" ENVIRONMENT? [IF] NEGATE 1- CONSTANT (ERROR) [THEN] \ query environment \ if successful \ create constant (ERROR) : number 0. Rot dup 1+ c@ [char] - = >r count r@ if 1 /string then >number nip 0= if d>s r> if negate then else r> drop 2drop (error) then ; : test ." Enter a number: " refill drop bl word hex \ write prompt \ enter string \ parse string \ convert hexadecimal 3.29. CONTROLLINGTHERADIX 39 number dup (error) = if \ convert to a number \ test for valid number \ if not valid ." You didn’t enter a valid number!" drop cr else \ print if valid dup ." The number was: " decimal . ." decimal" cr ." The number was: " hex . ." hex" cr then ; test ’NUMBER’ will now also accept hexadecimal numbers. If the number is not a valid hexadecimal number, it will return ’(ERROR)’. You probably know there is more to radix control than ’OCTAL’, ’HEX’ and ’DECIMAL’. No, we have not forgotten them. In fact, you can choose any radix between 2 and 36. This slightly modified program will only accept binary numbers: S" MAX-N" ENVIRONMENT? [IF] NEGATE 1- CONSTANT (ERROR) [THEN] \ query environment \ if successful \ create constant (ERROR) : number 0. Rot dup 1+ c@ [char] - = >r count r@ if 1 /string then >number nip 0= if d>s r> if negate then else r> drop 2drop (error) then ; : binary 2 base ! ; : test ." Enter a number: " refill drop bl word binary number dup (error) = if \ write prompt \ enter string \ parse string \ convert hexadecimal \ convert to a number \ test for valid number \ if not valid ." You didn’t enter a valid number!" drop cr else dup ." The number was: " decimal . ." decimal" cr ." The number was: " hex . ." hex" cr \ print if valid \ both decimal and hex then ; test ’BASE’ is a predefined variable that enables you to select any radix between 2 and 36. This makes Forth very flexible: hex 02B decimal . cr However, this won’t work: 40 CHAPTER3. ARRAYSANDSTRINGS : wont-work hex 02B decimal . cr ; But this will: hex : will-work 02B decimal . cr ; Why that? Well, ’HEX’ will just be compiled, not executed. So when Forth tries to compile ”02B”, it doesn’t recognize it as a hexadecimal number and will try to find word ’02B’. Which it can’t of course. Note that after ”WILL-WORK” has been compiled all numbers following it will stuill be compiled as hexadecimal numbers. Why? Because ’DECIMAL’ is compiled too! You should place a ’DECIMAL’ outside the definition in order to reset the radix. BTW, it is always a good idea to add a leading zero to a hexadecimal number. For example, is this a hex number or a word: face 3.30 Pictured numeric output You probably have used this before, like when writing Basic. Never heard of "PRINT USING.."? Well, it is a way to print numbers in a certain format. Like telephone-numbers, time, dates, etc. Of course Forth can do this too. In fact, you’ve probably used it before. Both ’.’ and ’.R’ use the same internal routines. They are called just before a number is printed. This numeric string is created in the PAD and overwritten with each new call. But we’ll go into that a bit later on. What you have to remember is that you define the format reverse. What is printed first, is defined last in the format. So if you want to print: 060-5556916 You have to define it this way: 6196555-060 Formatting begins with the word ’<#’ and ends with the word ’#>’. A single number is printed using ’#’ and the remainder of the number is printed using ’#s’ (which is always at least one digit). Let’s go a bit further into that: : print# s>d <# #s #> type cr ; 256 print# This simply prints a single number (since only ’#S’ is between the ’<#’ and the ’#>’ and goes to a new line. There is hardly any difference with ’.’. You can try any (positive) number. Note that the values that ’#>’ leaves on the stack can directly be used by ’TYPE’. You can forget about the ’S>D’ word. Just don’t forget to put it there. This is a slightly different format: : print3# s>d <# # # # #> type cr ; 256 print3# 1 print3# 1000 print3# 3.30. PICTUREDNUMERICOUTPUT 41 This one will print "256", "001" and "000". Always the last three positions. The ’#’ simply stands for ’print a single digit’. So if you want to print a number with at least three digits, the format would be: #s # # That is: print the remainder of the number (at least one digit) and then two more. Now reverse it: # # #s Enclose it by ’S>D’, ’<#’ and ’#>’ and add ’TYPE CR’: s>d <# # # #s #> type cr And that’s it! Is it? Not quite. So far we’ve only printed positive numbers. If you try a negative number, you will find it prints garbage. This behavior can be fixed with the word ’SIGN’. ’SIGN’ simply takes the number from the stack and prints a "-" when it is negative. The problem is that all other formatting words can only handle positive numbers. So we need the same number twice. One with the sign and one without. A typical signed number formatting word looks like: : signed# dup >r abs s>d <# #s r> sign #> type ; Note the ’DUP ABS’ sequence. First the number is duplicated (for ’SIGN’) and then the absolute value is taken (for the other formatting words). So we got the on the stack twice. First on the returnstack with sign (for ’SIGN’), second without sign (for the other formatting words). Does that make sense to you? We can place ’SIGN’ wherever we want. If we want to place the sign after the number (like some accoun- tants do) we would write: : account# dup >r abs s>d <# r> sign #s #> type ; But that is still not enough to write "$2000.15" is it? Well, in order to do that there is another very handy word called ’HOLD’. The word ’HOLD’ just copies any character into the formatted number. Let’s give it a try: $2000.16 Let’s reverse that: 61.0002$ So we first want to print two numbers, even when they are zero: # # .0002$ Then we want to print a dot. This is where ’HOLD’ comes in. ’HOLD’ takes an ASCII code and places the equivalent character in the formatting string. We don’t have to look up the ASCII code for a dot of course. We can use ’CHAR’: # # char . hold 0002$ 42 CHAPTER3. ARRAYSANDSTRINGS Then we want to print the rest of the number (which is at least one digit): # # char . hold #s $ Finally we want to print the character "$". Another job for ’HOLD’: # # char . hold #s char $ hold So this is our formatting word: : currency <# # # [char] . hold #s [char] $ hold #> type cr ; And we call it like this: 200016 currency You can do some pretty complex stuff with these formatting words. Try to figure out this one from the master himself, Leo Brodie: : sextal 6 base ! ; : :00 # sextal # decimal 58 hold ; : time# s>d <# :00 :00 #S #> type cr ; 3615 time# Yeah, it prints the time! Pretty neat, huh? Now try the telephone-number we discussed in the beginning. That shouldn’t be too hard. 3.31 Converting a number to a string Since there is no special word in Forth which will convert a number to a string, we’ll have to create it ourselves. In the previous section we have seen how a numeric string is created in the PAD. We can use this to create a word that converts a number to a string. Because the PAD is highly volatile, we have to move the string immediately after its creation. So we’ll create a word that not only creates the string, but moves it directly to its proper location: : >string >r dup >r abs s>d <# #s r> sign #> r@ char+ swap dup >r cmove r> r> c! ; ( n a -- ) It takes a number, the address of a string and returns nothing. Example: create num$ 16 chars allot -1024 num$ >string num$ count type cr Chapter 4 Stacks and colon definitions 4.1 The address of a colon-definition You can get the address of a colon definition by using the word ”’ (tick): : add + ; ’ add . cr \ a colon definition \ display address Very nice, but what good is it for? Well, first of all the construction "’ ADD" throws the address of "ADD" on the stack. You can assign it to a variable, define a constant for it, or compile it into an array of constants: ’ add constant add-address variable addr ’ add addr ! create addresses ’ add , Are you with us so far? If we would simply write "ADD", "ADD" would be executed right away and no value would be left on the stack. Tick forces Forth to throw the address of "ADD" on the stack instead of executing "ADD". What you can actually do with it, we will show you in the next section. 4.2 Vectored execution This is a thing that can be terribly difficult in other languages, but is extremely easy in Forth. Maybe you’ve ever seen a BASIC program like this: 10 LET A=40 20 GOSUB A 30 END 40 PRINT "Hello" 50 RETURN 60 PRINT "Goodbye" 70 RETURN 43 44 CHAPTER4. STACKSANDCOLONDEFINITIONS If you execute this program, it will print "Hello". If you change variable "A" to "60", it will print "Good- bye". In fact, the mere expression "GOSUB A" can do two different things. In Forth you can do this much more comfortable: : goodbye ." Goodbye" cr ; : hello ." Hello" cr ; variable a : greet a @ execute ; ’ hello a ! greet ’ goodbye a ! greet What are we doing here? First, we define a few colon-definitions, called "HELLO" and "GOODBYE". Second, we define a variable called "A". Third, we define another colon-definition which fetches the value of "A" and executes it by calling ’EXECUTE’. Then, we get the address of "HELLO" (by using "’ HELLO") and assign it to "A" (by using "A !"). Finally, we execute "GREET" and it says "Hello". It seems as if "GREET" is simply an alias for "HELLO", but if it were it would print "Hello" throughout the program. However, the second time we execute "GREET", it prints "Goodbye". That is because we assigned the address of "GOODBYE" to "A". The trick behind this all is ’EXECUTE’. ’EXECUTE’ takes the address of e.g. "HELLO" from the stack and calls it. In fact, the expression: hello Is equivalent to: ’ hello execute This can be extremely useful. We’ll give you a little hint: create subs ’ hello , ’ goodbye , Does this give you any ideas? 4.3 Using values A value is a cross-over between a variable and a constant. May be this example will give you an idea: declaration: variable a 1 constant b 2 b + value c ( No initial value) ( Initial value, can’t change) ( Initial value, can change) 4.4. THESTACKS fetching: 45 a @ b c ( Variable throws address on stack) ( Constant throws value on stack) ( Value throws value on stack) storing: 2 b + a ! 2 b + to c ( Expression can be stored at runtime) ( Constant cannot be reassigned) ( Expression can be stored at runtime) In many aspects, values behave like variables and can replace variables. The only thing you cannot do is make arrays of values. In fact, a value is a variable that behaves in certain aspects like a constant. Why use a value at all? Well, there are situations where a value can help, e.g. when a constant CAN change during execution. It is certainly not a good idea to replace all variables by values. 4.4 The stacks Forth has two stacks. So far we’ve talked about one stack, which is the Data Stack. The Data Stack is heavily used, e.g. when you execute this code: 2 3 + . Only the Data Stack is used. First, "2" is thrown on it. Second, "3" is thrown on it. Third, ’+’ takes both values from the stack and returns the sum. Fourth, this value is taken from the stack by ’.’ and displayed. So where do we need the other stack for? Well, we need it when we want to call a colon-definition. Before execution continues at the colon-definition, it saves the address of the currently executed definition on the other stack, which is called the Return Stack for obvious reasons. Then execution continues at the colon-definition. Every colon-defini tion is terminated by ’;’, which com- piles into ’EXIT’. When ’EXIT’ is encountered, the address on top of the Return Stack is popped. Execu- tion then continues at that address, which in fact is the place where we came from. If we would store that address on the Data Stack, things would go wrong, because we can never be sure how many values were on that stack when we called the colon-definition, nor would be know how many there are on that stack when we encounter ’EXIT’. A separate stack takes care of that. Try and figure out how this algorithm works when we call a colon-definition from a colon-definition and you will see that it works (Forth is proof of that). It now becomes clear how ’EXECUTE’ works. When ’EXECUTE’ is called, the address of the colon- definition is on the Data Stack. All ’EXECUTE’ does is copy its address on the Return Stack, take the address from the Data Stack and call it. ’EXIT’ never knows the difference.. But the Return Stack is used by other words too. Like ’DO’ and ’LOOP’. ’DO’ takes the limit and the counter from the Data Stack and puts them on the Return Stack. ’LOOP’ takes both of them from the Return Stack and compares them. If they don’t match, it continues execution after ’DO’. That is one of the reasons that you cannot split a ’DO..’LOOP’. However, if you call a colon-definition from within a ’DO’..’LOOP’ you will see it works: the return address is put on top of the limit and the counter. As long as you keep the Return Stack balanced (which isn’t too hard) you can get away with quite a few things as we will see in the following section. 46 CHAPTER4. STACKSANDCOLONDEFINITIONS 4.5 Saving temporary values We haven’t shown you how the Return Stack works just for the fun of it. Although it is an area that is almost exclusively used by the system you can use it too. We know we can manipulate the Data Stack only three items deep (using ’ROT’). Most of the time that is more than enough, but sometimes it isn’t. In Forth there are special words to manipulate stack items in pairs, e.g. "2DUP" ( n1 n2 – n1 n2 n1 n2) or "2DROP" ( n1 n2 –). In most Forths they are already available, but we could easily define those two ourselves: : 2dup over over ; : 2drop drop drop ; You will notice that "2SWAP" ( n1 n2 n3 n4 – n3 n4 n1 n2) becomes a lot harder. How can we get this deep? You can use the Return Stack for that.. The word ’>R’ takes an item from the Data Stack and puts it on the Return Stack. The word ’R>’ does it the other way around. It takes the topmost item from the Return Stack and puts it on the Data Stack. Let’s try it out: : 2swap rot >r rot r> ; ( n1 n2 n3 n4) \ four items on the stack ( n1 n3 n4 n2) \ rotate the topmost three ( n1 n3 n4) ( n3 n4 n1) ( n3 n4 n1 n2) \ get n2 from the Return Stack \ n2 is now on the Return Stack \ rotate other items And why does it work in this colon-definition? Why doesn’t the program go haywire? Because the Return Stack is and was perfectly balanced. The only thing we had to do was to get off "n2" before the semi-colon was encountered. Remember, the semi-colon compiles into ’EXIT’ and ’EXIT’ pops a return-address from the Return Stack. Okay, let me show you the Return Stack effects: : 2swap rot >r rot r> ; ( r1) ( r1) ( r1 n2) ( r1 n2) ( r1) ( --) Note, these are the Return Stack effects! "R1" is the return-address. And it is there on top on the Return Stack when ’EXIT’ is encountered. The general rule is: Clean up your mess inside a colon-definition If you save two values on the Return Stack, get them off there before you attempt to leave. If you save three, get three off. And so on. This means you have to be very careful with looping and branching. Otherwise you have a program that works perfectly in one situation and not in another: : this-wont-work >r 0= if ( n1 n2 -- n1 n2) ( n1) ( --) 4.6. THERETURNSTACKANDTHEDO..LOOP 47 r> dup 1 2 else then ; ( n2) ( n2 n2) ( 1 2) This program will work perfectly if n1 equals zero. Why? Let’s look at the Return Stack effects: : this-wont-work >r 0= if else then r> dup 1 2 ; ( r1) ( r1 n2) ( r1 n2) ( r1) ( r1) ( r1 n2) ( r1 n2) You see when it enters the ’ELSE’ clause the Return Stack is never cleaned up, so Forth attempts to return to the wrong address. Avoid this, since this can be very hard bugs to fix. 4.6 The Return Stack and the DO..LOOP We’ve already told you that the limit and the counter of a DO..LOOP (or DO..+LOOP) are stored on the Return Stack. But how does this affect saving values in the middle of a loop? Well, this example will make that quite clear: : test 1 10 0 do >r i . r> loop cr drop ; test ( n) ( n) ( --) ( --) ( n) ( n) ( n) ( --) You might expect that it will show you the value of the counter ten times. In fact, it doesn’t. Let’s take a look at the Return Stack: : test 1 10 0 do >r i . r> ( --) ( l c) ( l c n) ( l c n) ( l c) 48 CHAPTER4. STACKSANDCOLONDEFINITIONS loop cr drop ; test ( --) ( --) ( --) You might have noticed (unless you’re blind) that it prints ten times the number "1". Where does it come from? Usually ’I’ prints the value of the counter, which is on top of the Return Stack. This time it isn’t: the number "1" is there. So ’I’ thinks that "1" is actually the counter and displays it. Since that value is removed from the Return Stack when ’LOOP’ is encountered, it doesn’t do much harm. We see that we can safely store temporary values on the Return Stack inside a DO..LOOP, but we have to clean up the mess, before we encounter ’LOOP’. So, this rule applies here too: Clean up your mess inside a DO..LOOP But we still have to be prepared that the word ’I’ will not provide the expected result (which is the current value of the counter). In fact, ’I’ does simply copy the topmost value on the Return Stack. Which is usually correct, unless you’ve manipulated the Return Stack yourself. Note that there are other words beside ’I’, which do exactly the same thing: copy the top of the Return Stack. But they are intended to be used outside a DO..LOOP. We’ll see an example of that in the following section. 4.7 Other Return Stack manipulations The Return Stack can avoid some complex stack acrobatics. Stack acrobatics? Well, you know it by now. Sometimes all these values and addresses are just not in proper sequence, so you have to ’SWAP’ and ’ROT’ a lot until they are. You can avoid some of these constructions by just moving a single value on the Return Stack. You can return it to the Data Stack when the time is there. Or you can use the top of the Return Stack as a kind of local variable. No, you don’t have to move it around between both stacks all the time and you don’t have to use ’I’ out of its context. There is a well-established word, which does the same thing: ’R@’. This is an example of the use of ’R@’: : delete >r #lag + r@ - #lag r@ negate r# +! #lead + swap cmove r> blanks ; ( n --) ( a1) ( a1 a2 n2) ( a1 a2 n2 n3) ( a1 a2 n2) ( a1 a2 n2 a3) ( a1) ( --) ’R@’ copies the top of the Return Stack to the Data Stack. This example is taken from a Forth-editor. It deletes "n" characters left of the cursor. By putting the number of characters on the Return Stack right away, its value can be fetched by ’R@’ without using ’DUP’ or ’OVER’. Since it can be fetched at any time, no ’SWAP’ or ’ROT’ has to come in. 4.8. ALTERINGTHEFLOWWITHTHERETURNSTACK 49 4.8 Altering the flow with the Return Stack The mere fact that return addresses are kept on the stack means that you can alter the flow of a program. This is hardly ever necessary, but if you’re a real hacker you’ll try this anyway, so we’d better give you some pointers on how it is done. Let’s take a look at this program. Note that we comment on the Return Stack effects: : soup ." soup " ; : dessert ." dessert " ; : chicken ." chicken " ; : rice ." rice " ; : entree chicken rice ; : dinner soup entree dessert ; dinner cr ( r1 r2) ( r1 r6) ( r1 r3 r4) ( r1 r3 r5) ( r1 r3) ( r1) ( --) And this is the output: soup chicken rice dessert Before we execute "DINNER" the Return Stack is empty. When we enter "DINNER" the return address to the main program is on the Return Stack (r1). "DINNER" calls "SOUP". When we enter "SOUP" the return address to "DINNER" is on the Return Stack (r2). When we are done with "SOUP", its return address disappears from the Return Stack and execution continues within "DINNER". Then "ENTREE" is called, putting another return address on the Return Stack (r3). "ENTREE" on its turn, calls "CHICKEN". Another return address (r4) is put on the Return Stack. Let’s take a look on what currently lies on the Return Stack: - Top Of Return Stack (TORS) - r4 r3 r1 returns to ENTREE returns to DINNER returns to main program - - - As we already know, ’;’ compiles an ’EXIT’, which takes the TORS and jumps to that address. What if we lose the current TORS? Will the system crash? Apart from other stack effects (e.g. too few or the wrong data are left on the Data Stack) nothing will go wrong. Unless the colon-definition was called from inside a DO..LOOP, of course. But what DOES happen? The solution is provided by the table: it will jump back to "DINNER" and continue execution from there. : soup ." soup " ; : dessert ." dessert " ; : chicken ." chicken " r> drop ; : rice ." rice " ; : entree chicken rice ; : dinner soup entree dessert ; dinner cr ( r1 r2) ( r1 r6) ( r1 r3 - r4 gets lost!) ( r1 r3 r5) ( r1 r3) ( r1) ( --) Since "CHICKEN" gets rid of the return address to "ENTREE", "RICE" is never called. Instead, a jump is made to "DINNER" that assumes that "ENTREE" is done, so it continues with "DESSERT". This is the output: 50 CHAPTER4. STACKSANDCOLONDEFINITIONS soup chicken dessert Note that this is not common practice and we do not encourage its use. However, it gives you a pretty good idea how the Return Stack is used by the system. 4.9 Leaving a colon-definition You can sometimes achieve the very same effect by using the word ’EXIT’ on a strategic place. We’ve already encountered ’EXIT’. It is the actual word that is compiled by ’;’. What you didn’t know is that you can compile an ’EXIT’ without using a ’;’. And it does the very same thing: it pops the return address from the Return Stack and jumps to it. Let’s take a look at our slightly modified previous example: : soup ." soup " ; : dessert ." dessert " ; : chicken ." chicken " ; : rice ." rice " ; : entree chicken exit rice ; : dinner soup entree dessert ; dinner cr ( r1 r2) ( r1 r6) ( r1 r3 r4) ( is never reached) ( r1 r3) ( r1) ( --) After "CHICKEN" has been executed by "ENTREE", an ’EXIT’ is encountered. ’EXIT’ works just like ’;’, so Forth thinks the colon-definition has come to an end and jumps back to "DINNER". It never comes to calling "RICE", so the output is: soup chicken dessert ’EXIT’ is mostly used in combination with some kind of branching like IF..ELSE..THEN. Compare it with ’LEAVE’ that leaves a DO..LOOP early. But now for the big question: what is the difference between ’EXIT’ and ’;’? Both compile an ’EXIT’, but they are not aliases. Forth shuts down the compiler when encountering ’;’. This is not performed by ’EXIT’. 4.10 How deep is your stack? You can ask Forth how many values are on the Data Stack using ’DEPTH’. It will report the number of values, before you executed ’DEPTH’. Let’s elaborate on that a little more: .( Begin) cr 10 5 9 depth . cr ( no values on the stack) ( 1 value on the stack) ( 2 values on the stack) ( 3 values on the stack) ( 4 values on the stack) ( Forth reports "3") Chapter 5 Advanced topics 5.1 Booleans and numbers You might have expected we had discussed this subject much earlier. But we haven’t and for one very good reason. We’ve told you a few chapters ago that ’IF’ branches if the top of the stack is non-zero. Any number will do. So you would expect that this program will print "I’m here": : test 1 2 and if ." I’m here" then ; test In fact, it doesn’t! Why? Well, ’AND’ is a BINARY operator, not a LOGICAL operator. That means it reacts on bit-patterns. Given two numbers, it will evaluate bits at the same position. The number "1" is "01" in binary. The number "2" is "10" in binary. ’AND’ will evaluate the first bit (binary digit, now you know where that came from!). The first bit is the rightmost bit, so "0" for the number "2" and "1" for the number "1". ’AND’ works on a simple rule, if both bits are "1" the result will be "1" on that position. Otherwise it will be "0". So "1" and "0" are "0". The evaluation of the second bit has the same result: "0". We’re stuck with a number that is "0". False. So ’IF’ concludes that the expression is not true: 2 base ! 10 01 AND = . cr \ set radix to binary ( binary number "2") ( binary number "1") ( binary result after AND) It will print "0". However, "3" and "2" would work just fine: 2 base ! 10 11 AND . cr \ set radix to binary ( binary number "2") ( binary number "3") ( binary result after AND) 51 52 CHAPTER5. ADVANCEDTOPICS It will print "10". The same applies to other binary operators as ’OR’ and ’INVERT’. ’OR’ works just like ’AND’ but works the other way around. If both bits are "0" the result will be "0" on that position. Otherwise it will be "1": 2 base ! 10 01 OR . cr \ set radix to binary ( binary number "2") ( binary number "1") ( binary result after OR) It will print "11". We do not encourage the use of ’INVERT’ for logical operations, although the standard allows it. You should use ’0=’ instead. ’0=’ takes the top of the stack and leave a true-flag if it is zero. Otherwise it will leave a false-flag. That means that if a condition istrue (non-zero), it will leave a false-flag. Which is exactly what a logical NOT should do. Take a look at his brother ’0<>’. ’0<>’ takes the top of the stack and leaves a true-flag if it is non-zero. Otherwise it will leave a false- flag. The funny thing is ’AND’ and ’OR’ work perfectly with flags and behave as expected. ’0<>’ will convert a value to a flag for you. So this works: : test 1 0<> 2 0<> and if ." I’m here" cr then ; test Of course, you don’t have to use ’0<>’ when a word returns a flag. You should check the standard for details on that. 5.2 Including your own definitions At a certain point you may have written a lot of definitions you’re very fond of. You use them in most of your programs, so before you actually get to the programs you have to work your way through all these standard definitions. Even worse, when you change one of them you have to edit all your programs. Most Forths have a way to permanently include them in the kernel, but if you’re not up to that or want your programs to be as portable as possible you can solve this in a better way. Just put all of your definitions in a single file and start your program with: s" mydefs.fs" included The compiler will now first compile all the definitions in ”mydefs.fs” before starting with the main program. We’ve done exactly the same in the following sections. Most of the code you’ll find there uses the Easy4tH extensions, so instead of listing them every single time, we’ve just included them. Easy4tH has old favorites like ”PLACE” and ”NUMBER” already available to you. You have to define the constant ”/STRING-SPACE” first in order to use it. A value of 16384 should be fine in most cases. If you get an error, you can always increase it. 5.3. CONDITIONALCOMPILATION 53 5.3 Conditional compilation This is something which can be very handy when you’re designing a Forth program for different environ- ments or even different Forth compilers. Let’s say you’ve written a general ledger program in Forth that is so good, you can sell it. Your customers want a demo, of course. You’re willing to give one to them, but you’re afraid they’re going to use the demo without ever paying for it. One thing you can do is limit the number of entries they can make. So, you copy the source and make a special demo version. But you have to do that for every new release. Wouldn’t it just be easier to have one version of the program and just change one single constant? You can with conditional compilation: true constant DEMO DEMO [if] 256 constant #Entries [else] 65536 constant #Entries [then] variable CurrentEntry create Entries #Entries cells allot We defined a constant, called "DEMO", which is true. So, when the compiler reaches the "DEMO [if]" line, it knows that it has to compile "256 constant Entries", since "DEMO" is true. When it comes to "[else]", it knows it has to skip everything up to "[then]". So, in this case the compiler behaves like you’ve written: 256 constant #Entries variable CurrentEntry create Entries #Entries cells allot Would you change "DEMO" to false, the compiler would behave as if you wrote: variable CurrentEntry 65536 constant #Entries create Entries #Entries cells allot The word ’[IF]’ only works at compile time and is never compiled into the object. ’[IF]’ takes a expression. If this expression is true, the code from ’[IF]’ until ’[ELSE]’ is compiled, just as ’[IF]’ wasn’t there. Is this expression is false, everything ’[IF]’ up to ’[ELSE]’ is discarded as if it wasn’t there. That also means you can discard any code that is superfluous in the program. E.g. when you’re making a colon-definition to check whether you can make any more entries. If you didn’t use conditional compila- tion, you might have written it like this: : CheckIfFull dup #Entries = if drop DEMO if else ( n -- n) ( n f) ( n) ( --) ( f) ( --) ." Buy the full version" \ give message and exit program 54 CHAPTER5. ADVANCEDTOPICS ." No more entries" then cr quit then ; ( --) ( n) But this one is nicer and will take up less code: DEMO [IF] : .Message ." Buy the full version" ; [ELSE] : .Message ." No more entries" ; [THEN] : CheckIfFull dup #Entries = if drop .Message cr quit then ; ( n -- n) ( n f) ( n) ( --) ( n) You can also use conditional compilation to discard large chunks of code. This is a much better way than to comment all the lines out, e.g. this won’t work anyway: ( ) : room? dup size 1- invert and if \ is it a valid variable? ( n n) ( n f) \ exit program drop ." Not an element of ROOM" cr quit then ; This is pretty cumbersome and prone to error: \ \ \ \ \ \ \ : room? dup size 1- invert and if \ is it a valid variable? ( n n) ( n f) \ exit program drop ." Not an element of ROOM" cr quit then ; But this is something that can easily be handled: false [if] : room? dup \ is it a valid variable? ( n n) 5.4. EXCEPTIONS 55 size 1- invert and if ( n f) \ exit program drop ." Not an element of ROOM" cr quit then ; [then] Just change "false" to "true" and the colon-definition is part of the program again. Note that ’[IF] .. [THEN]’ can be nested! Conditional compilation is very powerful and one of the easiest features a language can have. 5.4 Exceptions You know when you violate the integrity of Forth, it will exit and report the cause and location of the error. Wouldn’t it be nice if you could catch these errors within the program? It would save a lot of error-checking anyway. It is quite possible to check every value within Forth, but it takes code and performance, which makes your program less compact and slower. Well, you can do that too in Forth. And not even that, you can trigger your own errors as well. This simple program triggers an error and exits Forth when you enter a "0": 16384 constant /string-space s" easy4th.fs" included : input# begin until ; refill drop bl word number dup (error) <> dup 0= if swap drop then : could-fail input# dup 0= if 1 throw then ; : do-it drop drop could-fail ; : try-it 1 2 [’] do-it execute ." The number was" . cr ; try-it \ get a number ( --) ( n ) ( n f ) ( n f -f ) ( f | n f ) ( input routine ) \ get a number \ if non-zero, return it \ if zero, throw exception ( -- n) \ drop numbers and \ call COULD-FAIL ( --) \ put 2 nums on stack and \ execute DO-IT ( --) \ call TRY-IT 56 CHAPTER5. ADVANCEDTOPICS "TRY-IT" puts two numbers on the stack, gets the execution token of "DO-IT" and executes it. "DO-IT" drops both numbers and calls "COULD-FAIL". "COULD-FAIL" gets a number and compares it against "0". If zero, it calls an exception. If not, it returns the number. The expression "1 THROW" has the same effect as calling ’QUIT’. The program exits, but with the error message "Unhandled exception". You can use any positive number for ’THROW’, but "0 THROW" has no effect. This is called a "user exception", which means you defined and triggered the error. There are also system exceptions. These are triggered by the system, e.g. when you want to access an undefined variable or print a number when the stack is empty. These exceptions have a negative number, so: throw -4 Will trigger the "Stack empty" error. You can use these if you want but we don’t recommend it, since it will confuse the users of your program. You’re probably not interested in an alternative for ’QUIT’. Well, ’THROW’ isn’t. It just enables you to "throw" an exception and exceptions can be caught by your program. That means that Forth won’t exit, but transfers control back to some routine. Let’s do just that: 16384 constant /string-space s" easy4th.fs" included : input# begin refill drop bl word number dup (error) <> dup 0= if swap drop then until ; : could-fail input# dup 0= if 1 throw then ; : do-it drop drop could-fail ; : try-it ( --) ( n ) ( n f ) ( n f -f ) ( f | n f ) ( input routine ) ( -- n) ( --) ( --) 1 2 [’] do-it catch if drop drop ." There was an exception" cr else ." The number was" . cr then ; try-it The only things we changed is a somewhat more elaborate "TRY-IT" definition and we replaced ’EXE- CUTE’ by ’CATCH’. 5.4. EXCEPTIONS 57 ’CATCH’ works just like ’EXECUTE’, except it returns a result-code. If the result-code is zero, everything is okay. If it isn’t, it returns the value of ’THROW’. In this case it would be "1", since we execute "1 THROW". That is why "0 THROW" doesn’t have any effect. If you enter a nonzero value at the prompt, you won’t see any difference with the previous version. How- ever, if we enter "0", we’ll get the message "There was an exception", before the program exits. But hey, if we got that message, that means Forth was still in control! In fact, it was. When "1 THROW" was executed, the stack-pointers were restored and we were directly returned to "TRY-IT". As if "1 THROW" performed an ’EXIT’ to the token following ’CATCH’. Since the stack-pointers were returned to their original state, the two values we discarded in "DO-IT" are still on the stack. But the possibility exists they have been altered by previous definitions. The best thing we can do is discard them. So, the first version exited when you didn’t enter a nonzero value. The second version did too, but not after giving us a message. Can’t we make a version in which we can have another try? Yes we can: 16384 constant /string-space s" easy4th.fs" included : input# begin refill drop bl word number dup (error) <> dup 0= if swap drop then until ; : could-fail input# dup 0= if 1 throw then ; : do-it drop drop could-fail ; : retry-it begin while 1 2 [’] do-it catch ( --) ( n ) ( n f ) ( n f -f ) ( f | n f ) ( input routine ) ( -- n) ( --) ( --) drop drop ." Exception, keep trying" cr repeat ." The number was " . cr ; retry-it This version will not only catch the error, but it allows us to have another go! We can keep on entering "0", until we enter a nonzero value. Isn’t that great? But it gets even better! We can exhaust the stack, trigger a system exception and still keep on going. But let’s take it one step at the time. First we change "COULD-FAIL" into: 58 CHAPTER5. ADVANCEDTOPICS : could-fail ( -- n) input# dup 0= if drop ." Stack: " depth . cr 1 throw then ; This will tell us that the stack is exhausted at his point. Let’s exhaust is a little further by redefining "COULD-FAIL" again: : could-fail ( -- n) input# dup 0= if drop drop then ; Another ’DROP’? But wouldn’t that trigger an "Stack empty" error? Yeah, it does. But instead of exiting, the program will react as if we wrote "-4 THROW" instead of "DROP DROP". The program will correctly report an exception when we enter "0" and act accordingly. This will work with virtually every runtime error. Which means we won’t have to protect our program against every possible user-error, but let Forth do the checking. We won’t even have to set flags in every possible colon-definition, since Forth will automatically skip every level between ’THROW’ and ’CATCH’. Even better, the stacks will be restored to the same depth as they were before ’CATCH’ was called. You can handle the error in any way you want. You can display an error message, call some kind of error-handler, or just ignore the error. Is that enough flexibility for you? 5.5 Lookup tables Leo Brodie wrote: "I consider the case statement an elegant solution to a misguided problem: attempting an algorithmic expression of what is more aptly described in a decision table". And that is exactly what we are going to teach you. Let’s say we want a routine that takes a number and then prints the appropriate month. In ANS-Forth, you could do that this way: : Get-Month case 1 of ." January " endof 2 of ." February " endof " endof March 3 of ." " endof April 4 of ." " endof 5 of ." May " endof June 6 of ." " endof 7 of ." July 8 of ." August " endof 9 of ." September" endof 10 of ." October " endof 11 of ." November " endof 12 of ." December " endof endcase cr ; 5.5. LOOKUPTABLES 59 This takes a lot of code and a lot of comparing. In this case (little wordplay) you would be better of with an indexed table, like this: 16384 constant /string-space s" easy4th.fs" included create MonthTable May June July $" January " , $" February " , March " , $" April " , $" " , $" " , $" $" " , August " , $" $" September" , $" October " , $" November " , $" December " , : Get-Month ( n -- ) 12 min 1- MonthTable swap cells + @ pad copy1 count type cr ; Which does the very same thing and will certainly work faster. Normally, you can’t do that this easily in ANS-Forth, but with this primer you can, so use it! But can you use the same method when you’re working with a random set of values like "2, 1, 3, 12, 5, 6, 4, 7, 11, 8, 10, 9". Yes, you can. But you need a special routine to access such a table. Of course we designed one for you: : Search-Table swap >r rot rot over over 0 begin swap over cells + @ dup 0> >r rot <> r@ and r> drop r@ + >r over over r> while repeat r@ if ( n1 a1 n2 n3 -- n4 f) ( n1 a1 n3) ( n3 n1 a1) ( n3 n1 a1 n1 a1) ( n3 n1 a1 n1 a1 n2) ( n3 n1 a1 n1 a1 n2) ( n3 n1 a1 n1 n2 a1 n2) ( n3 n1 a1 n1 n2 a2) ( n3 n1 a1 n1 n2 n3 n3) ( n3 n1 a1 n1 n2 n3) ( n3 n1 a1 n2 f) ( n3 n1 a1 n2 f) ( n3 n1 a1 n2) ( n3 n1 a1 n2) ( n3 n1 a1 n2+2) ( n3 n1 a1 n1 a1) ( n3 n1 a1 n1 a1 n2+2) ( n3 n1 a1 n2) >r rot r> ( n1 a1 n3 n2) 1”COPY” is part of the Easy4tH extensions and will copy a counted string from one address to another (addr1 addr2 – addr2). 60 CHAPTER5. ADVANCEDTOPICS + cells + @ swap drop ( n1 n4) ( n3) drop drop drop ( n1) else then r> r> drop ; ( n f) ( n f) This routine takes four values. The first one is the value you want to search. The second is the address of the table you want to search. The third one is the number of fields this table has. And on top of the stack you’ll find the field which value it has to return. The first field must be the "index" field. It contains the values which have to be compared. That field has number zero. This routine can search zero-terminated tables. That means the last value in the index field must be zero. Finally, it can only lookup positive values. You can change all that by modifying the line with "0> >r". It returns the value in the appropriate field and a flag. If the flag is false, the value was not found. Now, how do we apply this to our month table? First, we have to redefine it: 16384 constant /string-space s" easy4th.fs" included 0 Constant NULL create MonthTable March April May June July 1 , $" January " , 2 , $" February " , " , 3 , $" " , 4 , $" " , 5 , $" " , 6 , $" " , 7 , $" 8 , $" August " , 9 , $" September" , 10 , $" October " , 11 , $" November " , 12 , $" December " , NULL , Note that this table is sorted, but that doesn’t matter. It would work just as well when it was unsorted. Let’s get our stuff together: the address of the table is "MonthTable", it has two fields and we want to return the address of the string, which is located in field 1. Field 0 contains the values we want to compare. We can now define a routine which searches our table: : Search-Month MonthTable 2 1 Search-Table ; ( n1 -- n2 f) Now, we define a new "Get-Month" routine: : Get-Month Search-Month ( n --) \ search table 5.6. WHATDOES>CREATEDO? 61 pad copy count type drop ." Not found" \ if month is found \ print its name \ if month is not found \ drop value \ and show message if else then cr ; Is this flexible? Oh, you bet! We can extend the table with ease: 16384 constant /string-space s" easy4th.fs" included 0 Constant NULL 3 Constant #MonthFields create MonthTable 1 , $" January " , 31 , 2 , $" February " , 28 , " , 31 , March 3 , $" " , 30 , April 4 , $" " , 31 , May 5 , $" " , 30 , June 6 , $" " , 31 , 7 , $" July 8 , $" August " , 31 , 9 , $" September" , 30 , 10 , $" October " , 31 , 11 , $" November " , 30 , 12 , $" December " , 31 , NULL , Now we make a slight modification to "Search-Month": : Search-Month MonthTable #MonthFields 1 Search-Table ; This enables us to add more fields without ever having to modify "Search-Month" again. If we add another field, we just have to modify "#MonthFields". We can now even add another routine, which enables us to retrieve the number of days in a month: : Search-#Days MonthTable #Monthfields 2 Search-Table ; Of course, there is room for even more optimization, but for now we leave it at that. Do you now understand why Forth shouldn’t have a CASE construct? 5.6 What DOES> CREATE do? Let’s take a closer look at ’CREATE’. What does ’CREATE’ actually do? Well, it takes the string afterward as a name and makes a word out of it. Let’s try this: CREATE aname 62 CHAPTER5. ADVANCEDTOPICS You can even type that at the prompt. It works, it just makes a word. If you don’t believe me, type this: aname . Now "ANAME" just wrote out an address. When you’re still at the prompt and haven’t done anything else, type this: 5 , Right after our definition we just compiled in "5". Is that useful? Can we ever retrieve it? Sure, we just created "ANAME", didn’t we? And the address "ANAME" gave is exactly the address where out "5" was compiled! Believe it or not: aname @ . Sure, it answers "5". We can even compile another number: 10 , And retrieve it: aname 1 cells + @ . Looks a lot like an array doesn’t it? Well, under the hood Forth is actually doing the same thing. Let’s say we want a word that compiles a number and makes a name for it. We could define this: : compilenumber create , ; Now let’s use it: 10 compilenumber anothername anothername @ . First ’CREATE’ does it’s job and and creates the word "ANOTHERNAME". Second, ’,’ kicks in and compiles the number that is on the stack, just like at our previous example. When we execute, the address of the number is thrown on the stack, so we can retrieve the contents and display them. Don’t you think "COMPILENUMBER" is a bad name. What we actually did was create an initialized variable! So what do you think the word ’VARIABLE’ does? Simple: : variable create 1 cells allot ; It creates a name and reserves some space for it! But can’t we do anything else than just throw a address. Yes, you can not just define the way Forth compiles a word, but also what it does at runtime. You use ’DOES>’ to define it. Let’s say we want it to display the contents right away. We already got an address. What next? Sure: @ . That will get the contents of the address and show ’em. Just put them behind ’DOES>’: 5.7. FIXEDPOINTCALCULATION 63 : displaynumber create , does> @ . ; That’s it! Let’s use it: 11 displaynumber anumber anumber Great, isn’t it? Looks a lot like ’CONSTANT’, doesn’t it? Let’s fill you in on that. ’CONSTANT’ is defined like this: : constant create , does> @ ; There is really nothing more to it. Yeah, you can do great things with that. Make you own datatypes and such. We’ll see more of that later on. 5.7 Fixed point calculation We already learned that if we can’t calculate it out in dollars, we can calculate it in cents. And still present the result in dollars using pictured numeric output: : currency <# # # [char] . hold #s [char] $ hold #> type cr ; In this case, this: 200012 currency will print this: $2000.12 Well, that may be a relief for the bookkeepers, but what about us scientists? You can do the very same trick. We have converted some Forth code for you that gives you very accurate results. You can use routines like SIN, COS and SQRT. A small example: 31415 CONSTANT PI 10000 CONSTANT 10K VARIABLE XS ( scaling constant ) ( square of scaled angle ) : KN ( n1 n2 -- n3, n3=10000-n1*x*x/n2 where x is the angle ) XS @ SWAP / NEGATE 10K */ 10K + ( x*x/n2 ) ( -n1*x*x/n2 ) ( 10000-n1*x*x/n2 ) ; : (SIN) DUP DUP 10K */ XS ! 10K 72 KN 42 KN 20 KN 6 KN ( x -- sine*10K, x in radian*10K ) ( x*x scaled by 10K ) ( save it in XS ) ( last term ) ( terms 3, 2, and 1 ) 64 CHAPTER5. ADVANCEDTOPICS 10K */ ( times x ) ; : SIN ; PI 180 */ (SIN) If you enter: 45 sin . cr ( degree -- sine*10K ) ( convert to radian ) ( compute sine ) You will get "7071", because the result is multiplied by 10000. You can correct this the same way you did with the dollars: just print the number in the right format. : /10K. <# # # # # [char] . hold #S #> type cr ; 45 sin /10K. This one will actually print: 0.7071 But note that Forth internally still works with the scaled number, which is "7071". Another example: : SQRT 0 SWAP 0 DO 1 + DUP 2* 1 + +LOOP ; ( n1 -- n2, n2**2<=n1 ) ( initial root ) ( set n1 as the limit ) ( refresh root ) ( 2n+1 ) ( add 2n+1 to sum, loop if ) ( less than n1, else done ) : .fp <# # [char] . hold #S #> type cr ; If you enter a number of which the root is an integer, you will get a correct answer. You don’t even need a special formatting routine. If you enter any other number, it will return only the integer part. You can fix this by scaling the number. However, scaling it by 10 will get you nowhere, since "3" is the square root of "9", but "30" is not the square root of "90". In that case, we have to scale it by 100, 10,000 or even 1,000,000 to get a correct answer. In order to retrieve the next digit of the square root of "650", we have to multiply it by 100: 650 100 * sqrt .fp Which will print: 25.4 To acquire greater precision we have to scale it up even further, like 10,000. This will show us, that "25.49" brings us even closer to the correct answer. 5.8. RECURSION 5.8 Recursion 65 Yes, but can she do recursion? Of course she can! In order to let a colon-definition call itself, you have to use the word ’RECURSE’. Everybody knows how to calculate a factorial. In Forth you can do this by: : factorial ( n1 -- n2) dup 2 > if dup 1- recurse * then ; 10 factorial . cr If you use the word ’RECURSE’ outside a colon-definition, the results are undefined. Note that recursion lays a heavy burden on the return stack. Sometimes it is wiser to implement such a routine differently: : factorial dup begin while dup 2 > 1- swap over * swap repeat drop ; 10 factorial . cr So if you ever run into stack errors when you use recursion, keep this in mind. 5.9 Forward declarations It doesn’t happen very often, but sometimes you have a program where two colon-definitions call each other. There is no special instruction in Forth to do this, like Pascals "FORWARD" keyword, but still it can be done. It even works the same way. Let’s say we’ve got two colon-definitions called "STEP1" and "STEP2". "STEP1" calls "STEP2" and vice versa. First we create a value called "(STEP2)". We assign it the value ’-1’ since it is highly unlikely, there will ever be a word with that address: -1 value (Step2) Then we use vectored execution to create a forward declaration for "STEP2": : Step2 (Step2) execute ; Now we can create "STEP1" without a problem: 66 CHAPTER5. ADVANCEDTOPICS : Step1 1+ dup . cr Step2 ; But "STEP2" does not have a body yet. Of course, you could create a new colon-definition, tick it and assign the execution token to "(STEP2)", but this creates a superfluous word. It is much neater to use ’:NONAME’. ’:NONAME’ can be used like a normal ’:’, but it doesn’t require a name. Instead, it pushes the execution token of the colon-definition it created on the stack. No, ’:NON- AME’ does *NOT* create a literal expression, but it is just what we need: :noname 1+ dup . cr Step1 ; to (Step2) Now we are ready! We can simply execute the program by calling "STEP1": 1 Step1 Note that if you run this program, you’ll get stack errors! Sorry, but the example has been taken from a Turbo Pascal manual ;). 5.10 This is the end This is the end of it. If you mastered all we have written about Forth, you may be just as proficient as we are. Or even better. In the meanwhile you may even have acquired a taste for this strange, but elegant language. If you do, there is plenty left for you to learn. If you find any errors in this primer or just want to make a remark or suggestion, you can contact us by sending an email to: hansoft@bigfoot.com We do also have a web-site: http://hansoft.come.to You will find there lots of documentation and news on 4tH, our own Forth compiler. Part I Appendices 67 Bibliography ANSI X3/X3J14 (1993). Draft proposed American National Standrad for Information Systems — Programming Languages — Forth. Global Engineering Documents, 15 Inverness Way East, Englewood, CO 80122-5704, USA, sixth edition, 1993. Document Number: ANSI/IEEE X3.215-1994. Leo Brodie (1982). Starting Forth. Prentice Hall International, second edition, 1982. Leo Brodie (1984). Thinking Forth. Prentice Hall International, 1984. Hans Bezemer / Benjamin Hoyt (1997). Lookup Tables. Forth Dimensions, Volume XIX, Number 3, September 1997 October. 69 70 History VERSION AUTHOR DATE MODIFICATION 0.1 0.2 0.3 0.4 Hans Bezemer 2001-03-07 Initial document Hans Bezemer 2001-03-11 Used ’COMUS’ APPEND and changed ” to $” in section ’Lookup Tables’ Hans Bezemer 2001-03-25 Changed several things in Easy4tH and fixed some errors in example programs Hans Bezemer 2001-04-06 Got rid of SCOPY and added ’What DOES> CREATE do’ 71 72 Easy4tH \ easy4th V1.0d A 4tH to ANS Forth interface \ Typical usage: \ \ 4096 constant /string-space s" easy4th.fs" included \ This is an ANS Forth program requiring: \ \ \ \ \ 1. The word NIP in the Core Ext. word set 2. The word /STRING in the String word set 3. The word D>S in the Double word set 4. The words MS and TIME&DATE in the Facility Ext. word set 5. The words [IF] and [THEN] in the Tools Ext. word set. \ (c) Copyright 1997,2001 Wil Baden, Hans Bezemer. Permission is granted by the \ authors to use this software for any application provided this \ copyright notice is preserved. \ Uncomment the next line if REFILL does not function properly \ : refill query cr true ; \ 4tH datatypes : ARRAY CREATE CELLS ALLOT ; : STRING CREATE CHARS ALLOT ; : TABLE CREATE ; \ 4tH constants S" MAX-N" ENVIRONMENT? [IF] NEGATE 1- CONSTANT (ERROR) [ELSE] .( Error: MAX-N undefined) cr [THEN] S" MAX-N" ENVIRONMENT? [IF] CONSTANT MAX-N [ELSE] .( Error: MAX-N undefined) cr [THEN] \ query environment \ if successful \ create constant (ERROR) \ query environment \ if successful \ create constant MAX-N S" STACK-CELLS" ENVIRONMENT? \ query environment 73 74 [IF] CONSTANT STACK-CELLS [ELSE] .( Error: STACK-CELLS undefined) cr [THEN] \ if successful \ create constant STACK-CELLS S" /PAD" ENVIRONMENT? [IF] CONSTANT /PAD [ELSE] .( Error: /PAD undefined) cr [THEN] \ query environment \ if successful \ create constant /PAD \ 4tH compiletime words : [NOT] 0= ; : [*] * ; : [+] + ; \ 4tH wordset : TH CELLS + ; : @’ @ ; : COPY ( a b -- b ) >R : WAIT 1000 * MS ; DUP C@ 1+ R@ SWAP MOVE R> ; : NUMBER ( a -- n) 0. ROT DUP 1+ C@ [CHAR] - = >R COUNT R@ IF 1 /STRING THEN >NUMBER NIP 0= IF D>S R> IF NEGATE THEN ELSE R> DROP 2DROP (ERROR) THEN ; ( Reserve STRING-SPACE CREATE STRING-SPACE VARIABLE NEXT-STRING in data-space. ) /STRING-SPACE CHARS ALLOT 0 NEXT-STRING ! ( caddr n addr -- ) : PLACE OVER OVER >R >R CHAR+ SWAP CHARS MOVE R> R> C! ; ( "string<">" -- caddr ) : $" [CHAR] " PARSE DUP 1+ NEXT-STRING @ + /STRING-SPACE > ABORT" String Space Exhausted. " STRING-SPACE NEXT-STRING @ CHARS + >R DUP 1+ NEXT-STRING +! R@ PLACE R> ; \ 4tHs Random generator ( Default RNG from the C Standard. ( properties, plus the advantage of being widely used. ) VARIABLE RANDSEED ‘RAND’ has reasonable ) 32767 CONSTANT MAX-RAND 75 : RAND RANDSEED @ ( random) 1103515245 * 16 RSHIFT MAX-RAND AND ( -- random ) 12345 + DUP RANDSEED ! ; : SRAND ( n -- ) RANDSEED ! ; 1 SRAND ( Don’t mumble. ) : random ( -- n ) RAND ; : set-random ( n -- ) SRAND ; 12 * + 31 * + 24 * + 60 * + 60 * + set-random ( -- ) ( Mix ’em up. ) : randomize TIME&DATE ; randomize 76 GNU Free Documentation License GNU Free Documentation License Version 1.1, March 2000 Copyright (C) 2000 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. 0. PREAMBLE The purpose of this License is to make a manual, textbook, or other written document "free" in the sense of freedom: to assure everyone the effective freedom to copy and redistribute it, with or without modifying it, either commercially or noncommercially. Secondarily, this License preserves for the author and publisher a way to get credit for their work, while not being considered responsible for modifications made by others. This License is a kind of "copyleft", which means that derivative works of the document must themselves be free in the same sense. It complements the GNU General Public License, which is a copyleft license designed for free software. We have designed this License in order to use it for manuals for free software, because free software needs free documentation: a free program should come with manuals providing the same freedoms that the software does. But this License is not limited to software manuals; it can be used for any textual work, regardless of subject matter or whether it is published as a printed book. We recommend this License principally for works whose purpose is instruction or reference. 1. APPLICABILITY AND DEFINITIONS This License applies to any manual or other work that contains a notice placed by the copyright holder saying it can be distributed under the terms of this License. The "Document", below, refers to any such manual or work. Any member of the public is a licensee, and is addressed as "you". A "Modified Version" of the Document means any work containing the Document or a portion of it, either copied verbatim, or with modifications and/or translated into another language. A "Secondary Section" is a named appendix or a front-matter section of the Document that deals exclu- sively with the relationship of the publishers or authors of the Document to the Document’s overall subject (or to related matters) and contains nothing that could fall directly within that overall subject. (For ex- ample, if the Document is in part a textbook of mathematics, a Secondary Section may not explain any mathematics.) The relationship could be a matter of historical connection with the subject or with related matters, or of legal, commercial, philosophical, ethical or political position regarding them. 77 78 The "Invariant Sections" are certain Secondary Sections whose titles are designated, as being those of Invariant Sections, in the notice that says that the Document is released under this License. The "Cover Texts" are certain short passages of text that are listed, as Front-Cover Texts or Back-Cover Texts, in the notice that says that the Document is released under this License. A "Transparent" copy of the Document means a machine-readable copy, represented in a format whose specification is available to the general public, whose contents can be viewed and edited directly and straightforwardly with generic text editors or (for images composed of pixels) generic paint programs or (for drawings) some widely available drawing editor, and that is suitable for input to text formatters or for automatic translation to a variety of formats suitable for input to text formatters. A copy made in an otherwise Transparent file format whose markup has been designed to thwart or discourage subsequent modification by readers is not Transparent. A copy that is not "Transparent" is called "Opaque". Examples of suitable formats for Transparent copies include plain ASCII without markup, Texinfo input format, LATEX input format, SGML or XML using a publicly available DTD, and standard-conforming simple HTML designed for human modification. Opaque formats include PostScript, PDF, proprietary formats that can be read and edited only by proprietary word processors, SGML or XML for which the DTD and/or processing tools are not generally available, and the machine-generated HTML produced by some word processors for output purposes only. The "Title Page" means, for a printed book, the title page itself, plus such following pages as are needed to hold, legibly, the material this License requires to appear in the title page. For works in formats which do not have any title page as such, "Title Page" means the text near the most prominent appearance of the work’s title, preceding the beginning of the body of the text. 2. VERBATIM COPYING You may copy and distribute the Document in any medium, either commercially or noncommercially, provided that this License, the copyright notices, and the license notice saying this License applies to the Document are reproduced in all copies, and that you add no other conditions whatsoever to those of this License. You may not use technical measures to obstruct or control the reading or further copying of the copies you make or distribute. However, you may accept compensation in exchange for copies. If you distribute a large enough number of copies you must also follow the conditions in section 3. You may also lend copies, under the same conditions stated above, and you may publicly display copies. 3. COPYING IN QUANTITY If you publish printed copies of the Document numbering more than 100, and the Document’s license notice requires Cover Texts, you must enclose the copies in covers that carry, clearly and legibly, all these Cover Texts: Front-Cover Texts on the front cover, and Back-Cover Texts on the back cover. Both covers must also clearly and legibly identify you as the publisher of these copies. The front cover must present the full title with all words of the title equally prominent and visible. You may add other material on the covers in addition. Copying with changes limited to the covers, as long as they preserve the title of the Document and satisfy these conditions, can be treated as verbatim copying in other respects. If the required texts for either cover are too voluminous to fit legibly, you should put the first ones listed (as many as fit reasonably) on the actual cover, and continue the rest onto adjacent pages. If you publish or distribute Opaque copies of the Document numbering more than 100, you must either in- clude a machine-readable Transparent copy along with each Opaque copy, or state in or with each Opaque copy a publicly-accessible computer-network location containing a complete Transparent copy of the Doc- ument, free of added material, which the general network-using public has access to download anony- mously at no charge using public-standard network protocols. If you use the latter option, you must take 79 reasonably prudent steps, when you begin distribution of Opaque copies in quantity, to ensure that this Transparent copy will remain thus accessible at the stated location until at least one year after the last time you distribute an Opaque copy (directly or through your agents or retailers) of that edition to the public. It is requested, but not required, that you contact the authors of the Document well before redistributing any large number of copies, to give them a chance to provide you with an updated version of the Document. 4. MODIFICATIONS You may copy and distribute a Modified Version of the Document under the conditions of sections 2 and 3 above, provided that you release the Modified Version under precisely this License, with the Modified Ver- sion filling the role of the Document, thus licensing distribution and modification of the Modified Version to whoever possesses a copy of it. In addition, you must do these things in the Modified Version: A. Use in the Title Page (and on the covers, if any) a title distinct from that of the Document, and from those of previous versions (which should, if there were any, be listed in the History section of the Doc- ument). You may use the same title as a previous version if the original publisher of that version gives permission. B. List on the Title Page, as authors, one or more persons or entities responsible for authorship of the modifications in the Modified Version, together with at least five of the principal authors of the Document (all of its principal authors, if it has less than five). C. State on the Title page the name of the publisher of the Modified Version, as the publisher. D. Preserve all the copyright notices of the Document. E. Add an appropriate copyright notice for your modifications adjacent to the other copyright notices. F. Include, immediately after the copyright notices, a license notice giving the public permission to use the Modified Version under theterms of this License, in the form shown in the Addendum below. G. Preserve in that license notice the full lists of Invariant Sections and required Cover Texts given in the Document’s license notice. H. Include an unaltered copy of this License. I. Preserve the section entitled "History", and its title, and add to it an item stating at least the title, year, new authors, and publisher of the Modified Version as given on the Title Page. If there is no section entitled "History" in the Document, create one stating the title, year, authors, and publisher of the Document as given on its Title Page, then add an item describing the Modified Version as stated in the previous sentence. J. Preserve the network location, if any, given in the Document for public access to a Transparent copy of the Document, and likewise the network locations given in the Document for previous versions it was based on. These may be placed in the "History" section. You may omit a network location for a work that was published at least four years before the Document itself, or if the original publisher of the version it refers to gives permission. K. In any section entitled "Acknowledgements" or "Dedications", preserve the section’s title, and preserve in the section all the substance and tone of each of the contributor acknowledgements and/or dedications given therein. L. Preserve all the Invariant Sections of the Document, unaltered in their text and in their titles. Section numbers or the equivalent are not considered part of the section titles. M. Delete any section entitled "Endorsements". Such a section may not be included in the Modified Ver- sion. N. Do not retitle any existing section as "Endorsements" or to conflict in title with any Invariant Section. If the Modified Version includes new front-matter sections or appendices that qualify as Secondary Sections 80 and contain no material copied from the Document, you may at your option designate some or all of these sections as invariant. To do this, add their titles to the list of Invariant Sections in the Modified Version’s license notice. These titles must be distinct from any other section titles. You may add a section entitled "Endorsements", provided it contains nothing but endorsements of your Modified Version by various parties–for example, statements of peer review or that the text has been ap- proved by an organization as the authoritative definition of a standard. You may add a passage of up to five words as a Front-Cover Text, and a passage of up to 25 words as a Back-Cover Text, to the end of the list of Cover Texts in the Modified Version. Only one passage of Front-Cover Text and one of Back-Cover Text may be added by (or through arrangements made by) any one entity. If the Document already includes a cover text for the same cover, previously added by you or by arrangement made by the same entity you are acting on behalf of, you may not add another; but you may replace the old one, on explicit permission from the previous publisher that added the old one. The author(s) and publisher(s) of the Document do not by this License give permission to use their names for publicity for or to assert or imply endorsement of any Modified Version. 5. COMBINING DOCUMENTS You may combine the Document with other documents released under this License, under the terms defined in section 4 above for modified versions, provided that you include in the combination all of the Invari- ant Sections of all of the original documents, unmodified, and list them all as Invariant Sections of your combined work in its license notice. The combined work need only contain one copy of this License, and multiple identical Invariant Sections may be replaced with a single copy. If there are multiple Invariant Sections with the same name but different contents, make the title of each such section unique by adding at the end of it, in parentheses, the name of the original author or publisher of that section if known, or else a unique number. Make the same adjustment to the section titles in the list of Invariant Sections in the license notice of the combined work. In the combination, you must combine any sections entitled "History" in the various original documents, forming one section entitled "History"; likewise combine any sections entitled "Acknowledgements", and any sections entitled "Dedications". You must delete all sections entitled "Endorsements." 6. COLLECTIONS OF DOCUMENTS You may make a collection consisting of the Document and other documents released under this License, and replace the individual copies of this License in the various documents with a single copy that is included in the collection, provided that you follow the rules of this License for verbatim copying of each of the documents in all other respects. You may extract a single document from such a collection, and distribute it individually under this License, provided you insert a copy of this License into the extracted document, and follow this License in all other respects regarding verbatim copying of that document. 7. AGGREGATION WITH INDEPENDENT WORKS A compilation of the Document or its derivatives with other separate and independent documents or works, in or on a volume of a storage or distribution medium, does not as a whole count as a Modified Version 81 of the Document, provided no compilation copyright is claimed for the compilation. Such a compilation is called an "aggregate", and this License does not apply to the other self-contained works thus compiled with the Document, on account of their being thus compiled, if they are not themselves derivative works of the Document. If the Cover Text requirement of section 3 is applicable to these copies of the Document, then if the Document is less than one quarter of the entire aggregate, the Document’s Cover Texts may be placed on covers that surround only the Document within the aggregate. Otherwise they must appear on covers around the whole aggregate. 8. TRANSLATION Translation is considered a kind of modification, so you may distribute translations of the Document under the terms of section 4. Replacing Invariant Sections with translations requires special permission from their copyright holders, but you may include translations of some or all Invariant Sections in addition to the original versions of these Invariant Sections. You may include a translation of this License provided that you also include the original English version of this License. In case of a disagreement between the translation and the original English version of this License, the original English version will prevail. 9. TERMINATION You may not copy, modify, sublicense, or distribute the Document except as expressly provided for under this License. Any other attempt to copy, modify, sublicense or distribute the Document is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 10. FUTURE REVISIONS OF THIS LICENSE The Free Software Foundation may publish new, revised versions of the GNU Free Documentation License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. See http://www.gnu.org/copyleft/. Each version of the License is given a distinguishing version number. If the Document specifies that a particular numbered version of this License "or any later version" applies to it, you have the option of following the terms and conditions either of that specified version or of any later version that has been published (not as a draft) by the Free Software Foundation. If the Document does not specify a version number of this License, you may choose any version ever published (not as a draft) by the Free Software Foundation. ADDENDUM: How to use this License for your documents To use this License in a document you have written, include a copy of the License in the document and put the following copyright and license notices just after the title page: Copyright (c) YEAR YOUR NAME. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free 82 Software Foundation; with the Invariant Sections being LIST THEIR TITLES, with the Front- Cover Texts being LIST, and with the Back-Cover Texts being LIST. A copy of the license is included in the section entitled "GNU Free Documentation License". If you have no Invariant Sections, write "with no Invariant Sections" instead of saying which ones are invariant. If you have no Front-Cover Texts, write "no Front-Cover Texts" instead of "Front-Cover Texts being LIST"; likewise for Back-Cover Texts. If your document contains nontrivial examples of program code, we recommend releasing these examples in parallel under your choice of free software license, such as the GNU General Public License, to permit their use in free software. |