package tiny_languages

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file St_kernel.ml

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
(* generated from kernel/*.st by languages/smalltalk/dune *)
let files = [
("Definitions.st", {st|"The kernel's classes, the Blue Book's hierarchy (its part two), a
subset. St_boot reads these definitions before anything can run: it
makes every class and its metaclass from them, ties the knot at the top
(Object class superclass == Class, Metaclass class class == Metaclass),
then compiles the methods of the other files. The fields the virtual
machine reads -- a class's, a context's, a Character's value -- are in
the order St_class.mli and St_interp.mli say."!

nil subclass: #Object
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Kernel-Objects'!

Object subclass: #Behavior
	instanceVariableNames: 'superclass methodDict format'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Kernel-Classes'!

Behavior subclass: #ClassDescription
	instanceVariableNames: 'instanceVariables organization'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Kernel-Classes'!

ClassDescription subclass: #Class
	instanceVariableNames: 'name category classPool comment'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Kernel-Classes'!

ClassDescription subclass: #Metaclass
	instanceVariableNames: 'thisClass'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Kernel-Classes'!

Object subclass: #UndefinedObject
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Kernel-Objects'!

Object subclass: #Boolean
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Kernel-Objects'!

Boolean subclass: #True
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Kernel-Objects'!

Boolean subclass: #False
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Kernel-Objects'!

Object subclass: #Association
	instanceVariableNames: 'key value'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Kernel-Objects'!

Object subclass: #Message
	instanceVariableNames: 'selector arguments'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Kernel-Methods'!

Object subclass: #Magnitude
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Numeric-Magnitudes'!

Magnitude subclass: #Character
	instanceVariableNames: 'value'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Numeric-Magnitudes'!

Magnitude subclass: #Number
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Numeric-Numbers'!

Number subclass: #Integer
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Numeric-Numbers'!

Integer subclass: #SmallInteger
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Numeric-Numbers'!

Integer variableByteSubclass: #LargePositiveInteger
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Numeric-Numbers'!

LargePositiveInteger variableByteSubclass: #LargeNegativeInteger
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Numeric-Numbers'!

Number subclass: #Float
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Numeric-Numbers'!

Number subclass: #Fraction
	instanceVariableNames: 'numerator denominator'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Numeric-Numbers'!

Object subclass: #Point
	instanceVariableNames: 'x y'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Graphics-Primitives'!

Object subclass: #Rectangle
	instanceVariableNames: 'origin corner'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Graphics-Primitives'!

Object subclass: #Collection
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Collections-Abstract'!

Collection subclass: #SequenceableCollection
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Collections-Abstract'!

SequenceableCollection subclass: #ArrayedCollection
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Collections-Abstract'!

ArrayedCollection variableSubclass: #Array
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Collections-Arrayed'!

ArrayedCollection variableByteSubclass: #String
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Collections-Text'!

String variableByteSubclass: #Symbol
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Collections-Text'!

ArrayedCollection variableByteSubclass: #ByteArray
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Collections-Arrayed'!

SequenceableCollection subclass: #Interval
	instanceVariableNames: 'start stop step'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Collections-Sequenceable'!

SequenceableCollection variableSubclass: #OrderedCollection
	instanceVariableNames: 'firstIndex lastIndex'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Collections-Sequenceable'!

OrderedCollection variableSubclass: #SortedCollection
	instanceVariableNames: 'sortBlock'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Collections-Sequenceable'!

Collection variableSubclass: #Set
	instanceVariableNames: 'tally'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Collections-Unordered'!

Set variableSubclass: #Dictionary
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Collections-Unordered'!

Collection subclass: #Bag
	instanceVariableNames: 'contents'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Collections-Unordered'!

Object subclass: #SystemDictionary
	instanceVariableNames: 'associations'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'System-Support'!

Object subclass: #Stream
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Collections-Streams'!

Stream subclass: #PositionableStream
	instanceVariableNames: 'collection position readLimit'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Collections-Streams'!

PositionableStream subclass: #ReadStream
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Collections-Streams'!

PositionableStream subclass: #WriteStream
	instanceVariableNames: 'writeLimit'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Collections-Streams'!

WriteStream subclass: #ReadWriteStream
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Collections-Streams'!

Object subclass: #MethodDictionary
	instanceVariableNames: 'selectors methods'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Kernel-Methods'!

Object subclass: #CompiledMethod
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Kernel-Methods'!

Object variableSubclass: #ContextPart
	instanceVariableNames: 'sender pc stackp'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Kernel-Methods'!

ContextPart variableSubclass: #MethodContext
	instanceVariableNames: 'method unused receiver'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Kernel-Methods'!

ContextPart variableSubclass: #BlockContext
	instanceVariableNames: 'nargs startpc home'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Kernel-Methods'!

Object subclass: #TextCollector
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Interface-Transcript'!

Object subclass: #Time
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'System-Support'!

Object subclass: #Form
	instanceVariableNames: 'bits width height'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Graphics-Display Objects'!

Object subclass: #BitBlt
	instanceVariableNames: 'destForm sourceForm halftoneForm combinationRule destX destY width height sourceX sourceY clipX clipY clipWidth clipHeight'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Graphics-Support'!

BitBlt subclass: #Pen
	instanceVariableNames: 'frame location direction penDown'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Graphics-Primitives'!

Object subclass: #InputSensor
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Kernel-Processes'!
|st});
("Objects.st", {st|"Object, nil, the Booleans, and the two small classes everything
uses: Association (key -> value) and Message (what doesNotUnderstand:
receives)."!

!Object methodsFor: 'comparing'!
== anObject
	"The same object: the oops equal. Never fails."
	<primitive: 110>
	self primitiveFailed!

= anObject
	"Equal: by default, the same object. Subclasses say more."
	^self == anObject!

~= anObject
	^(self = anObject) == false!

~~ anObject
	^(self == anObject) == false!

hash
	"Equal objects must have equal hashes."
	^self identityHash!

identityHash
	<primitive: 75>
	self primitiveFailed! !

!Object methodsFor: 'class membership'!
class
	<primitive: 111>
	self primitiveFailed!

isKindOf: aClass
	| c |
	c := self class.
	[c == nil] whileFalse: [c == aClass ifTrue: [^true]. c := c superclass].
	^false!

isMemberOf: aClass
	^self class == aClass!

respondsTo: aSymbol
	^self class canUnderstand: aSymbol! !

!Object methodsFor: 'testing'!
isNil
	^false!

notNil
	^true!

isString
	^false!

isSymbol
	^false!

isNumber
	^false!

isInteger
	^false!

isSmallInteger
	^false!

isCharacter
	^false!

isClass
	^false!

isArray
	^false!

ifNil: aBlock
	^self!

ifNotNil: aBlock
	^aBlock value: self! !

!Object methodsFor: 'accessing'!
at: index
	"The indexed field, from 1."
	<primitive: 60>
	self checkIndex: index!

at: index put: value
	<primitive: 61>
	self checkIndex: index!

basicAt: index
	<primitive: 60>
	self checkIndex: index!

basicAt: index put: value
	<primitive: 61>
	self checkIndex: index!

size
	"How many indexed fields."
	<primitive: 62>
	^0!

basicSize
	<primitive: 62>
	^0!

instVarAt: index
	<primitive: 73>
	self error: 'Index out of bounds'!

instVarAt: index put: value
	<primitive: 74>
	self error: 'Index out of bounds'!

yourself
	^self!

checkIndex: index
	index isInteger ifFalse: [^self error: 'Index must be an integer'].
	(index < 1 or: [index > self basicSize])
		ifTrue: [^self error: 'Index out of bounds: ', index printString].
	^self error: 'Improper store into indexable object'! !

!Object methodsFor: 'copying'!
shallowCopy
	"A new object with the same fields: they are shared, not copied."
	<primitive: 153>
	self primitiveFailed!

copy
	^self shallowCopy!

deepCopy
	| new |
	new := self shallowCopy.
	1 to: self class instSize do: [:i | new instVarAt: i put: (self instVarAt: i) copy].
	^new! !

!Object methodsFor: 'printing'!
printString
	"What 'print it' shows: printOn: into a stream on a String."
	| aStream |
	aStream := WriteStream on: (String new: 16).
	self printOn: aStream.
	^aStream contents!

printOn: aStream
	"By default, the class's name with an article: a Point, an Object."
	| title |
	title := self class name.
	aStream nextPutAll: ((title at: 1) isVowel ifTrue: ['an '] ifFalse: ['a ']).
	aStream nextPutAll: title!

displayString
	^self printString!

displayNl
	Transcript show: self displayString; cr!

printNl
	Transcript show: self printString; cr! !

!Object methodsFor: 'error handling'!
error: aString
	"Stop the process, a notifier titled aString: St_debug.mli.
	Proceeding answers nil."
	<primitive: 141>!

doesNotUnderstand: aMessage
	"No method for aMessage selector, up the whole superclass chain."
	^self error: 'Message not understood: ', aMessage selector!

halt
	^self error: 'Halt'!

primitiveFailed
	^self error: 'Primitive failed'!

subclassResponsibility
	^self error: 'My subclass should have overridden one of my messages'!

shouldNotImplement
	^self error: 'This message is not appropriate for this object'!

mustBeBoolean
	^self error: 'NonBoolean receiver--proceed for truth'!

cannotReturn: value
	^self error: 'Context cannot return'! !

!Object methodsFor: 'message handling'!
perform: aSymbol
	<primitive: 83>
	^self error: 'Wrong number of arguments'!

perform: aSymbol with: anObject
	<primitive: 83>
	^self error: 'Wrong number of arguments'!

perform: aSymbol with: first with: second
	<primitive: 83>
	^self error: 'Wrong number of arguments'!

perform: aSymbol withArguments: anArray
	<primitive: 84>
	^self error: 'Wrong number of arguments'! !

!Object methodsFor: 'system primitives'!
become: otherObject
	"Swap the two objects: every reference to one now reaches the
	other (St_memory.mli)."
	<primitive: 72>
	self primitiveFailed!

inspect
	"An Inspector on the receiver."
	<primitive: 158>!

-> anObject
	^Association key: self value: anObject! !

!UndefinedObject methodsFor: 'testing'!
isNil
	^true!

notNil
	^false!

ifNil: aBlock
	^aBlock value!

ifNotNil: aBlock
	^self! !

!UndefinedObject methodsFor: 'printing'!
printOn: aStream
	aStream nextPutAll: 'nil'! !

!UndefinedObject methodsFor: 'class definition'!
subclass: nameSymbol instanceVariableNames: instVarNames classVariableNames: classVarNames poolDictionaries: pools category: categoryString
	"A class with no superclass: Object's own definition."
	<primitive: 143>
	self primitiveFailed! !

!Boolean methodsFor: 'printing'!
printOn: aStream
	aStream nextPutAll: (self ifTrue: ['true'] ifFalse: ['false'])! !

!Boolean methodsFor: 'copying'!
shallowCopy
	"true and false are unique."
	^self! !

!True methodsFor: 'controlling'!
ifTrue: trueBlock
	"Sent only when the argument is not a literal block: the compiler
	inlines the other cases into jumps (St_compile.mli)."
	^trueBlock value!

ifFalse: falseBlock
	^nil!

ifTrue: trueBlock ifFalse: falseBlock
	^trueBlock value!

ifFalse: falseBlock ifTrue: trueBlock
	^trueBlock value!

and: aBlock
	^aBlock value!

or: aBlock
	^true! !

!True methodsFor: 'logical operations'!
& aBoolean
	^aBoolean!

| aBoolean
	^true!

not
	^false! !

!False methodsFor: 'controlling'!
ifTrue: trueBlock
	^nil!

ifFalse: falseBlock
	^falseBlock value!

ifTrue: trueBlock ifFalse: falseBlock
	^falseBlock value!

ifFalse: falseBlock ifTrue: trueBlock
	^falseBlock value!

and: aBlock
	^false!

or: aBlock
	^aBlock value! !

!False methodsFor: 'logical operations'!
& aBoolean
	^false!

| aBoolean
	^aBoolean!

not
	^true! !

!Association class methodsFor: 'instance creation'!
key: aKey value: aValue
	^self new key: aKey value: aValue! !

!Association methodsFor: 'accessing'!
key
	^key!

value
	^value!

key: aKey value: aValue
	key := aKey.
	value := aValue!

value: aValue
	value := aValue! !

!Association methodsFor: 'comparing'!
= anAssociation
	^(anAssociation isKindOf: Association)
		and: [key = anAssociation key and: [value = anAssociation value]]!

hash
	^key hash! !

!Association methodsFor: 'printing'!
printOn: aStream
	key printOn: aStream.
	aStream nextPutAll: '->'.
	value printOn: aStream! !

!Message methodsFor: 'accessing'!
selector
	^selector!

arguments
	^arguments! !

!Message methodsFor: 'printing'!
printOn: aStream
	aStream nextPutAll: 'a Message ('; nextPutAll: selector.
	arguments do: [:each | aStream space; print: each].
	aStream nextPut: $)! !
|st});
("Classes.st", {st|"Classes are objects: Behavior (what makes instances and finds
methods), ClassDescription (instance variables' names, the methods'
categories), Class and Metaclass. And the objects of the machine
itself: the SystemDictionary Smalltalk, method dictionaries, compiled
methods, the contexts, and Time."!

!Behavior methodsFor: 'instance creation'!
new
	<primitive: 70>
	^self error: 'This class cannot make instances with new'!

new: anInteger
	<primitive: 71>
	^self error: 'This class cannot make indexed instances'!

basicNew
	<primitive: 70>
	self primitiveFailed!

basicNew: anInteger
	<primitive: 71>
	self primitiveFailed! !

!Behavior methodsFor: 'accessing'!
superclass
	^superclass!

methodDictionary
	^methodDict!

instSize
	"The named fields of an instance: the format's high bits."
	^format bitShift: -3!

isVariable
	^(format bitAnd: 7) between: 1 and: 2!

instanceVariableNames
	| names |
	names := superclass == nil ifTrue: [OrderedCollection new] ifFalse: [superclass instanceVariableNames].
	instanceVariables == nil ifFalse: [instanceVariables do: [:each | names add: each]].
	^names!

selectors
	<primitive: 149>
	self primitiveFailed!

allInstances
	<primitive: 145>
	self primitiveFailed!

subclasses
	| result |
	result := OrderedCollection new.
	Smalltalk classesDo: [:each | each superclass == self ifTrue: [result add: each]].
	^result!

withAllSuperclasses
	| result c |
	result := OrderedCollection new.
	c := self.
	[c == nil] whileFalse: [result add: c. c := c superclass].
	^result! !

!Behavior methodsFor: 'testing'!
isClass
	^true!

inheritsFrom: aClass
	| c |
	c := superclass.
	[c == nil] whileFalse: [c == aClass ifTrue: [^true]. c := c superclass].
	^false!

canUnderstand: aSymbol
	<primitive: 147>
	self primitiveFailed!

includesSelector: aSymbol
	<primitive: 148>
	self primitiveFailed! !

!Behavior methodsFor: 'compiling'!
compile: code classified: category
	"What the Browser's accept does: the selector, or the compiler's
	complaint, a String."
	<primitive: 142>
	self primitiveFailed!

compile: code
	^self compile: code classified: 'as yet unclassified'! !

!Behavior methodsFor: 'printing'!
printOn: aStream
	aStream nextPutAll: self name! !

!Class methodsFor: 'accessing'!
name
	^name!

category
	^category!

comment
	^comment!

comment: aString
	comment := aString! !

!Class methodsFor: 'subclass creation'!
subclass: nameSymbol instanceVariableNames: instVarNames classVariableNames: classVarNames poolDictionaries: pools category: categoryString
	<primitive: 143>
	self primitiveFailed!

variableSubclass: nameSymbol instanceVariableNames: instVarNames classVariableNames: classVarNames poolDictionaries: pools category: categoryString
	<primitive: 151>
	self primitiveFailed!

variableByteSubclass: nameSymbol instanceVariableNames: instVarNames classVariableNames: classVarNames poolDictionaries: pools category: categoryString
	<primitive: 152>
	self primitiveFailed! !

!Metaclass methodsFor: 'accessing'!
name
	^thisClass name , ' class'!

soleInstance
	^thisClass!

category
	^thisClass category! !

!SystemDictionary methodsFor: 'accessing'!
at: key
	^self at: key ifAbsent: [self error: 'Key not found: ', key]!

at: key ifAbsent: aBlock
	| a |
	a := self associationAt: key ifAbsent: [nil].
	a == nil ifTrue: [^aBlock value].
	^a value!

at: key put: value
	"The same Association kept: the methods that use the global see
	its new value (St_class.mli)."
	| a |
	a := self associationAt: key ifAbsent: [nil].
	a == nil ifFalse: [a value: value. ^value].
	associations := associations copyWith: (Association key: key asSymbol value: value).
	^value!

associationAt: key ifAbsent: aBlock
	| k |
	k := key asSymbol.
	associations do: [:each | each key == k ifTrue: [^each]].
	^aBlock value!

includesKey: key
	self associationAt: key ifAbsent: [^false].
	^true!

keys
	^associations collect: [:each | each key]!

classesDo: aBlock
	associations do: [:each | (each value isClass and: [each value name == each key]) ifTrue: [aBlock value: each value]]!

garbageCollect
	"The entries freed."
	<primitive: 146>
	self primitiveFailed! !

!SystemDictionary methodsFor: 'printing'!
printOn: aStream
	aStream nextPutAll: 'Smalltalk'! !

!MethodDictionary methodsFor: 'accessing'!
keys
	^selectors!

at: aSymbol
	1 to: selectors size do: [:i | (selectors at: i) == aSymbol ifTrue: [^methods at: i]].
	^self error: 'Key not found'!

size
	^selectors size! !

!CompiledMethod methodsFor: 'accessing'!
selector
	<primitive: 156>
	self primitiveFailed!

methodClass
	<primitive: 157>
	self primitiveFailed! !

!CompiledMethod methodsFor: 'printing'!
printOn: aStream
	aStream nextPutAll: self methodClass name; nextPutAll: '>>'; nextPutAll: self selector! !

!ContextPart methodsFor: 'controlling'!
blockCopy: numArgs
	"What the compiler sends for each [...]: a BlockContext that will
	start after the jump that follows this send (St_compile.mli)."
	<primitive: 80>
	self primitiveFailed! !

!ContextPart methodsFor: 'accessing'!
sender
	"The virtual machine answers it, to know that someone holds the
	sender now: a context nobody holds is used again (St_interp.mli)."
	<primitive: 159>
	^sender!

method
	^self subclassResponsibility!

receiver
	^self subclassResponsibility! !

!MethodContext methodsFor: 'accessing'!
method
	^method!

receiver
	^receiver!

home
	^self! !

!MethodContext methodsFor: 'printing'!
printOn: aStream
	"Point(Object)>>printString: the receiver's class, and the class
	the method was found in if another"
	| mclass |
	mclass := method methodClass.
	aStream nextPutAll: receiver class name.
	mclass == receiver class ifFalse: [aStream nextPut: $(; nextPutAll: mclass name; nextPut: $)].
	aStream nextPutAll: '>>'; nextPutAll: method selector! !

!BlockContext methodsFor: 'accessing'!
method
	^home method!

receiver
	^home receiver!

home
	^home!

numArgs
	^nargs! !

!BlockContext methodsFor: 'evaluating'!
value
	"Evaluate the block. It runs in this same context each time: the
	Blue Book's blocks are not reentrant (St_compile.mli)."
	<primitive: 81>
	^self error: 'Wrong number of arguments'!

value: arg
	<primitive: 81>
	^self error: 'Wrong number of arguments'!

value: arg1 value: arg2
	<primitive: 81>
	^self error: 'Wrong number of arguments'!

value: arg1 value: arg2 value: arg3
	<primitive: 81>
	^self error: 'Wrong number of arguments'!

value: arg1 value: arg2 value: arg3 value: arg4
	<primitive: 81>
	^self error: 'Wrong number of arguments'!

valueWithArguments: anArray
	<primitive: 82>
	^self error: 'Wrong number of arguments'! !

!BlockContext methodsFor: 'controlling'!
whileTrue: aBlock
	"Sent only when the compiler could not inline it."
	[self value] whileTrue: [aBlock value].
	^nil!

whileFalse: aBlock
	[self value] whileFalse: [aBlock value].
	^nil!

whileTrue
	[self value] whileTrue.
	^nil!

repeat
	[true] whileTrue: [self value]! !

!BlockContext methodsFor: 'printing'!
printOn: aStream
	aStream nextPutAll: '[] in '.
	home printOn: aStream! !

!Time class methodsFor: 'general inquiries'!
millisecondClock
	<primitive: 144>
	self primitiveFailed!

millisecondsToRun: aBlock
	| start |
	start := self millisecondClock.
	aBlock value.
	^self millisecondClock - start! !

!TextCollector methodsFor: 'accessing'!
show: aString
	"The Transcript's text, given to the host."
	<primitive: 140>
	^self show: aString printString!

nextPutAll: aString
	^self show: aString!

nextPut: aCharacter
	^self show: (String with: aCharacter)!

cr
	^self nextPut: Character cr!

tab
	^self nextPut: Character tab!

space
	^self nextPut: Character space!

print: anObject
	^self show: anObject printString!

display: anObject
	^self show: anObject displayString! !

!TextCollector methodsFor: 'printing'!
printOn: aStream
	aStream nextPutAll: 'a TextCollector'! !
|st});
("Numbers.st", {st|"The numbers. SmallInteger's arithmetic is primitives (and even the
bytecodes' own, St_interp.mli); when one fails -- an overflow, or an
argument of another kind -- the method's Smalltalk runs instead:
overflows go on in LargePositiveInteger and LargeNegativeInteger,
written here over their bytes as in the Blue Book, and mixed kinds are
coerced to the more general one (retry:coercing:, generality: a
SmallInteger 20, a large one 40, a Fraction 60, a Float 80)."!

!Magnitude methodsFor: 'comparing'!
< aMagnitude
	^self subclassResponsibility!

> aMagnitude
	^aMagnitude < self!

<= aMagnitude
	^(aMagnitude < self) not!

>= aMagnitude
	^(self < aMagnitude) not!

between: min and: max
	^self >= min and: [self <= max]!

max: aMagnitude
	^self > aMagnitude ifTrue: [self] ifFalse: [aMagnitude]!

min: aMagnitude
	^self < aMagnitude ifTrue: [self] ifFalse: [aMagnitude]! !

!Character class methodsFor: 'instance creation'!
value: anInteger
	"The unique Character of a code: $a == (Character value: 97)."
	<primitive: 120>
	^self error: 'Not a character code'!

digitValue: anInteger
	^'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' at: anInteger + 1!

cr
	^self value: 13!

lf
	^self value: 10!

tab
	^self value: 9!

space
	^self value: 32! !

!Character methodsFor: 'accessing'!
value
	^value!

asInteger
	^value!

asCharacter
	^self!

digitValue
	value <= 57 ifTrue: [^value - 48].
	value <= 90 ifTrue: [^value - 55].
	^value - 87! !

!Character methodsFor: 'comparing'!
< aCharacter
	^value < aCharacter asInteger!

= aCharacter
	^self == aCharacter!

hash
	^value! !

!Character methodsFor: 'testing'!
isCharacter
	^true!

isVowel
	^'aeiouAEIOU' includes: self!

isLetter
	^self isUppercase or: [self isLowercase]!

isUppercase
	^value between: 65 and: 90!

isLowercase
	^value between: 97 and: 122!

isDigit
	^value between: 48 and: 57!

isSeparator
	^value = 32 or: [value = 13 or: [value = 9 or: [value = 10]]]! !

!Character methodsFor: 'converting'!
asUppercase
	self isLowercase ifTrue: [^Character value: value - 32].
	^self!

asLowercase
	self isUppercase ifTrue: [^Character value: value + 32].
	^self!

asString
	^String with: self! !

!Character methodsFor: 'printing'!
printOn: aStream
	aStream nextPut: $$; nextPut: self!

displayString
	^String with: self! !

!Character methodsFor: 'copying'!
shallowCopy
	^self! !

!Number methodsFor: 'arithmetic'!
+ aNumber
	^self subclassResponsibility!

- aNumber
	^self + aNumber negated!

negated
	^0 - self!

abs
	self negative ifTrue: [^self negated].
	^self!

reciprocal
	^1 / self!

squared
	^self * self!

// aNumber
	^(self / aNumber) floor!

\\ aNumber
	^self - (self // aNumber * aNumber)!

quo: aNumber
	^(self / aNumber) truncated!

rem: aNumber
	^self - ((self quo: aNumber) * aNumber)!

raisedTo: anInteger
	| result |
	anInteger < 0 ifTrue: [^(self raisedTo: anInteger negated) reciprocal].
	result := 1.
	anInteger timesRepeat: [result := result * self].
	^result!

sqrt
	^self asFloat sqrt! !

!Number methodsFor: 'testing'!
isNumber
	^true!

negative
	^self < 0!

positive
	^self >= 0!

strictlyPositive
	^self > 0!

isZero
	^self = 0!

sign
	self > 0 ifTrue: [^1].
	self < 0 ifTrue: [^-1].
	^0!

even
	^self \\ 2 = 0!

odd
	^self even not! !

!Number methodsFor: 'truncation and round off'!
truncated
	^self subclassResponsibility!

floor
	| t |
	t := self truncated.
	(self < 0 and: [t ~= self]) ifTrue: [^t - 1].
	^t!

ceiling
	| t |
	t := self truncated.
	(self > 0 and: [t ~= self]) ifTrue: [^t + 1].
	^t!

rounded
	^(self + (self sign / 2)) truncated!

asInteger
	^self truncated! !

!Number methodsFor: 'coercing'!
generality
	^self subclassResponsibility!

coerce: aNumber
	^self subclassResponsibility!

retry: aSymbol coercing: aNumber
	"Do the operation again, the less general number made like the
	other: 3 + (1/2) becomes (3/1) + (1/2)."
	aNumber isNumber ifFalse: [
		aSymbol == #= ifTrue: [^false].
		aSymbol == #~= ifTrue: [^true].
		^self error: 'Not a number: ', aNumber printString].
	self generality < aNumber generality
		ifTrue: [^(aNumber coerce: self) perform: aSymbol with: aNumber].
	self generality > aNumber generality
		ifTrue: [^self perform: aSymbol with: (self coerce: aNumber)].
	^self error: 'Coercion failed'! !

!Number methodsFor: 'intervals'!
to: stop
	^Interval from: self to: stop by: 1!

to: stop by: step
	^Interval from: self to: stop by: step!

to: stop do: aBlock
	"Sent only when the compiler could not inline it."
	| i |
	i := self.
	[i <= stop] whileTrue: [aBlock value: i. i := i + 1]!

to: stop by: step do: aBlock
	| i |
	i := self.
	step > 0
		ifTrue: [[i <= stop] whileTrue: [aBlock value: i. i := i + step]]
		ifFalse: [[i >= stop] whileTrue: [aBlock value: i. i := i + step]]! !

!Number methodsFor: 'converting'!
@ y
	"A Point."
	<primitive: 18>
	^Point x: self y: y!

asPoint
	^self @ self! !

!Number methodsFor: 'printing'!
printString: base
	| aStream |
	aStream := WriteStream on: (String new: 16).
	self printOn: aStream base: base.
	^aStream contents! !

!Integer methodsFor: 'testing'!
isInteger
	^true! !

!Integer methodsFor: 'arithmetic'!
+ anInteger
	"Here when a large integer is involved: SmallInteger's own + is a
	primitive."
	anInteger isInteger ifFalse: [^self retry: #+ coercing: anInteger].
	^self asLargeInteger digitPlus: anInteger asLargeInteger!

- anInteger
	anInteger isInteger ifFalse: [^self retry: #- coercing: anInteger].
	^self asLargeInteger digitPlus: anInteger negated asLargeInteger!

* anInteger
	anInteger isInteger ifFalse: [^self retry: #* coercing: anInteger].
	^self asLargeInteger digitTimes: anInteger asLargeInteger!

/ anInteger
	"An Integer if it divides exactly, else a Fraction."
	anInteger isInteger ifFalse: [^self retry: #/ coercing: anInteger].
	anInteger = 0 ifTrue: [^self error: 'Division by zero'].
	^(Fraction numerator: self denominator: anInteger) reduced!

quo: anInteger
	"Truncated towards zero."
	anInteger isInteger ifFalse: [^self retry: #quo: coercing: anInteger].
	anInteger = 0 ifTrue: [^self error: 'Division by zero'].
	^(self digitDivide: anInteger) at: 1!

rem: anInteger
	anInteger isInteger ifFalse: [^self retry: #rem: coercing: anInteger].
	anInteger = 0 ifTrue: [^self error: 'Division by zero'].
	^(self digitDivide: anInteger) at: 2!

// anInteger
	"Truncated towards minus infinity."
	| qr |
	anInteger isInteger ifFalse: [^self retry: #// coercing: anInteger].
	anInteger = 0 ifTrue: [^self error: 'Division by zero'].
	qr := self digitDivide: anInteger.
	((qr at: 2) ~= 0 and: [self negative ~~ anInteger negative]) ifTrue: [^(qr at: 1) - 1].
	^qr at: 1!

\\ anInteger
	"The remainder of //: the sign of the divisor."
	| r |
	anInteger isInteger ifFalse: [^self retry: #\\ coercing: anInteger].
	anInteger = 0 ifTrue: [^self error: 'Division by zero'].
	r := (self digitDivide: anInteger) at: 2.
	(r ~= 0 and: [r negative ~~ anInteger negative]) ifTrue: [^r + anInteger].
	^r!

< anInteger
	anInteger isInteger ifFalse: [^self retry: #< coercing: anInteger].
	^(self asLargeInteger compareWith: anInteger asLargeInteger) < 0!

= anInteger
	anInteger isNumber ifFalse: [^false].
	anInteger isInteger ifFalse: [^self retry: #= coercing: anInteger].
	^(self asLargeInteger compareWith: anInteger asLargeInteger) = 0!

hash
	^(self \\ 1073741823) hash!

negated
	^0 - self!

gcd: anInteger
	"Euclid's."
	| a b t |
	a := self abs.
	b := anInteger abs.
	[b = 0] whileFalse: [t := a \\ b. a := b. b := t].
	^a!

lcm: anInteger
	^(self * anInteger) abs // (self gcd: anInteger)!

factorial
	"100 factorial printString size = 158."
	| result |
	self < 0 ifTrue: [^self error: 'Not valid for negative integers'].
	result := 1.
	2 to: self do: [:i | result := result * i].
	^result!

timesRepeat: aBlock
	| i |
	i := 1.
	[i <= self] whileTrue: [aBlock value. i := i + 1]!

isPrime
	self < 2 ifTrue: [^false].
	2 to: self sqrtFloor do: [:i | self \\ i = 0 ifTrue: [^false]].
	^true!

sqrtFloor
	| r |
	r := self asFloat sqrt truncated.
	[r * r > self] whileTrue: [r := r - 1].
	[(r + 1) * (r + 1) <= self] whileTrue: [r := r + 1].
	^r! !

!Integer methodsFor: 'coercing'!
generality
	^40!

coerce: aNumber
	^aNumber asLargeInteger! !

!Integer methodsFor: 'converting'!
asFloat
	"A large integer's bytes, summed as floats."
	| f |
	f := 0.0.
	self digitLength to: 1 by: -1 do: [:i | f := f * 256.0 + (self digitAt: i)].
	self negative ifTrue: [^f negated].
	^f!

truncated
	^self!

asCharacter
	^Character value: self!

digitDivide: anInteger
	"The quotient and the remainder truncated towards zero, an Array:
	by the schoolbook's short division when the divisor is small
	enough for a digit times it to stay a SmallInteger, else bit by
	bit."
	| a b qr q r |
	a := self abs asLargeInteger.
	b := anInteger abs.
	qr := (b isSmallInteger and: [b < 4194304])
		ifTrue: [a shortDivide: b]
		ifFalse: [a longDivide: b].
	q := qr at: 1.
	r := qr at: 2.
	self negative == anInteger negative ifFalse: [q := q negated].
	self negative ifTrue: [r := r negated].
	^Array with: q with: r! !

!Integer methodsFor: 'printing'!
printOn: aStream
	self printOn: aStream base: 10!

printOn: aStream base: base
	self < 0 ifTrue: [aStream nextPut: $-. ^self negated printOn: aStream base: base].
	self >= base ifTrue: [self // base printOn: aStream base: base].
	aStream nextPut: (Character digitValue: self \\ base)! !

!SmallInteger class methodsFor: 'instance creation'!
new
	^self error: 'SmallIntegers are not made, they are'! !

!SmallInteger methodsFor: 'arithmetic'!
+ aNumber
	<primitive: 1>
	^super + aNumber!

- aNumber
	<primitive: 2>
	^super - aNumber!

< aNumber
	<primitive: 3>
	^super < aNumber!

> aNumber
	<primitive: 4>
	^super > aNumber!

<= aNumber
	<primitive: 5>
	^super <= aNumber!

>= aNumber
	<primitive: 6>
	^super >= aNumber!

= aNumber
	<primitive: 7>
	^super = aNumber!

~= aNumber
	<primitive: 8>
	^super ~= aNumber!

* aNumber
	<primitive: 9>
	^super * aNumber!

/ aNumber
	<primitive: 10>
	^super / aNumber!

\\ aNumber
	<primitive: 11>
	^super \\ aNumber!

// aNumber
	<primitive: 12>
	^super // aNumber!

quo: aNumber
	<primitive: 13>
	^super quo: aNumber!

bitAnd: anInteger
	<primitive: 14>
	^self error: 'Not a SmallInteger'!

bitOr: anInteger
	<primitive: 15>
	^self error: 'Not a SmallInteger'!

bitXor: anInteger
	<primitive: 16>
	^self error: 'Not a SmallInteger'!

bitShift: anInteger
	<primitive: 17>
	anInteger > 0 ifTrue: [^self asLargeInteger * (2 raisedTo: anInteger)].
	^self error: 'Not a SmallInteger'! !

!SmallInteger methodsFor: 'testing'!
isSmallInteger
	^true!

even
	^(self bitAnd: 1) = 0! !

!SmallInteger methodsFor: 'coercing'!
generality
	^20!

coerce: aNumber
	^aNumber!

asLargeInteger
	<primitive: 154>
	self primitiveFailed! !

!SmallInteger methodsFor: 'converting'!
asFloat
	<primitive: 40>
	self primitiveFailed!

hash
	^self! !

!SmallInteger methodsFor: 'copying'!
shallowCopy
	^self! !

!LargePositiveInteger methodsFor: 'digits'!
digitAt: index
	"A byte of the magnitude, least significant first; 0 past the end."
	<primitive: 60>
	^0!

digitAt: index put: value
	<primitive: 61>
	self primitiveFailed!

digitLength
	<primitive: 62>
	self primitiveFailed!

normalize
	"The leading zero bytes dropped, a SmallInteger if it fits."
	<primitive: 155>
	self primitiveFailed!

asLargeInteger
	^self!

negative
	^false!

negated
	| result |
	result := (self negative ifTrue: [LargePositiveInteger] ifFalse: [LargeNegativeInteger]) new: self digitLength.
	1 to: self digitLength do: [:i | result digitAt: i put: (self digitAt: i)].
	^result normalize!

abs
	self negative ifTrue: [^self negated].
	^self! !

!LargePositiveInteger methodsFor: 'digit arithmetic'!
digitPlus: arg
	"The sum of two large integers of any signs."
	self negative == arg negative
		ifTrue: [^(self magnitudeAdd: arg negative: self negative) normalize].
	(self magnitudeCompare: arg) >= 0
		ifTrue: [^(self magnitudeSubtract: arg negative: self negative) normalize].
	^(arg magnitudeSubtract: self negative: arg negative) normalize!

magnitudeAdd: arg negative: aBoolean
	| len result carry sum |
	len := (self digitLength max: arg digitLength) + 1.
	result := (aBoolean ifTrue: [LargeNegativeInteger] ifFalse: [LargePositiveInteger]) new: len.
	carry := 0.
	1 to: len do: [:i |
		sum := (self digitAt: i) + (arg digitAt: i) + carry.
		result digitAt: i put: (sum bitAnd: 255).
		carry := sum bitShift: -8].
	^result!

magnitudeSubtract: arg negative: aBoolean
	"The receiver's magnitude is the larger."
	| len result borrow diff |
	len := self digitLength.
	result := (aBoolean ifTrue: [LargeNegativeInteger] ifFalse: [LargePositiveInteger]) new: len.
	borrow := 0.
	1 to: len do: [:i |
		diff := (self digitAt: i) - (arg digitAt: i) - borrow.
		diff < 0
			ifTrue: [diff := diff + 256. borrow := 1]
			ifFalse: [borrow := 0].
		result digitAt: i put: diff].
	^result!

magnitudeCompare: arg
	"-1, 0 or 1."
	| a b |
	(self digitLength max: arg digitLength) to: 1 by: -1 do: [:i |
		a := self digitAt: i.
		b := arg digitAt: i.
		a < b ifTrue: [^-1].
		a > b ifTrue: [^1]].
	^0!

compareWith: arg
	self negative == arg negative ifFalse: [self negative ifTrue: [^-1] ifFalse: [^1]].
	self negative ifTrue: [^arg magnitudeCompare: self].
	^self magnitudeCompare: arg!

digitTimes: arg
	"The schoolbook's: each digit of one by each of the other."
	| la lb result carry prod k |
	la := self digitLength.
	lb := arg digitLength.
	result := (self negative == arg negative ifTrue: [LargePositiveInteger] ifFalse: [LargeNegativeInteger]) new: la + lb.
	1 to: la do: [:i |
		carry := 0.
		1 to: lb do: [:j |
			k := i + j - 1.
			prod := (self digitAt: i) * (arg digitAt: j) + (result digitAt: k) + carry.
			result digitAt: k put: (prod bitAnd: 255).
			carry := prod bitShift: -8].
		result digitAt: i + lb put: carry].
	^result normalize!

shortDivide: divisor
	"The magnitude by a small SmallInteger, a digit at a time from the
	top: the quotient and the remainder."
	| len q r |
	len := self digitLength.
	q := LargePositiveInteger new: len.
	r := 0.
	len to: 1 by: -1 do: [:i |
		r := r * 256 + (self digitAt: i).
		q digitAt: i put: r // divisor.
		r := r \\ divisor].
	^Array with: q normalize with: r!

longDivide: divisor
	"A bit at a time: slow, but only for large divisors."
	| q r bit |
	q := 0.
	r := 0.
	self digitLength * 8 to: 1 by: -1 do: [:i |
		bit := ((self digitAt: i - 1 // 8 + 1) bitShift: 0 - (i - 1 \\ 8)) bitAnd: 1.
		r := r * 2 + bit.
		q := q * 2.
		r >= divisor ifTrue: [r := r - divisor. q := q + 1]].
	^Array with: q with: r! !

!LargePositiveInteger methodsFor: 'bit manipulation'!
bitShift: anInteger
	anInteger >= 0 ifTrue: [^self * (2 raisedTo: anInteger)].
	^self // (2 raisedTo: anInteger negated)!

hash
	^self digitAt: 1! !

!LargeNegativeInteger methodsFor: 'testing'!
negative
	^true! !

!Fraction class methodsFor: 'instance creation'!
numerator: n denominator: d
	"Not reduced: Integer>>/ reduces it."
	^self new setNumerator: n denominator: d! !

!Fraction methodsFor: 'private'!
setNumerator: n denominator: d
	d < 0
		ifTrue: [numerator := n negated. denominator := d negated]
		ifFalse: [numerator := n. denominator := d]! !

!Fraction methodsFor: 'accessing'!
numerator
	^numerator!

denominator
	^denominator! !

!Fraction methodsFor: 'arithmetic'!
+ aNumber
	aNumber isNumber ifFalse: [^self retry: #+ coercing: aNumber].
	aNumber generality = self generality ifFalse: [^self retry: #+ coercing: aNumber].
	^(Fraction
		numerator: numerator * aNumber denominator + (aNumber numerator * denominator)
		denominator: denominator * aNumber denominator) reduced!

- aNumber
	aNumber isNumber ifFalse: [^self retry: #- coercing: aNumber].
	aNumber generality = self generality ifFalse: [^self retry: #- coercing: aNumber].
	^(Fraction
		numerator: numerator * aNumber denominator - (aNumber numerator * denominator)
		denominator: denominator * aNumber denominator) reduced!

* aNumber
	aNumber isNumber ifFalse: [^self retry: #* coercing: aNumber].
	aNumber generality = self generality ifFalse: [^self retry: #* coercing: aNumber].
	^(Fraction
		numerator: numerator * aNumber numerator
		denominator: denominator * aNumber denominator) reduced!

/ aNumber
	aNumber isNumber ifFalse: [^self retry: #/ coercing: aNumber].
	aNumber generality = self generality ifFalse: [^self retry: #/ coercing: aNumber].
	^(Fraction
		numerator: numerator * aNumber denominator
		denominator: denominator * aNumber numerator) reduced!

negated
	^Fraction numerator: numerator negated denominator: denominator!

reduced
	"Lowest terms, and an Integer when the denominator is 1."
	| g |
	numerator = 0 ifTrue: [^0].
	g := numerator gcd: denominator.
	g = denominator ifTrue: [^numerator // g].
	^Fraction numerator: numerator // g denominator: denominator // g! !

!Fraction methodsFor: 'comparing'!
< aNumber
	aNumber isNumber ifFalse: [^self retry: #< coercing: aNumber].
	aNumber generality = self generality ifFalse: [^self retry: #< coercing: aNumber].
	^numerator * aNumber denominator < (aNumber numerator * denominator)!

= aNumber
	aNumber isNumber ifFalse: [^false].
	aNumber generality = self generality ifFalse: [^self retry: #= coercing: aNumber].
	^numerator = aNumber numerator and: [denominator = aNumber denominator]!

hash
	^numerator hash bitXor: denominator hash! !

!Fraction methodsFor: 'truncation and round off'!
truncated
	^numerator quo: denominator! !

!Fraction methodsFor: 'coercing'!
generality
	^60!

coerce: aNumber
	^Fraction numerator: aNumber denominator: 1! !

!Fraction methodsFor: 'converting'!
asFloat
	^numerator asFloat / denominator asFloat! !

!Fraction methodsFor: 'printing'!
printOn: aStream
	aStream print: numerator; nextPut: $/; print: denominator! !

!Float methodsFor: 'arithmetic'!
+ aNumber
	<primitive: 41>
	^self retry: #+ coercing: aNumber!

- aNumber
	<primitive: 42>
	^self retry: #- coercing: aNumber!

< aNumber
	<primitive: 43>
	^self retry: #< coercing: aNumber!

> aNumber
	<primitive: 44>
	^self retry: #> coercing: aNumber!

<= aNumber
	<primitive: 45>
	^self retry: #<= coercing: aNumber!

>= aNumber
	<primitive: 46>
	^self retry: #>= coercing: aNumber!

= aNumber
	<primitive: 47>
	^self retry: #= coercing: aNumber!

~= aNumber
	<primitive: 48>
	^self retry: #~= coercing: aNumber!

* aNumber
	<primitive: 49>
	^self retry: #* coercing: aNumber!

/ aNumber
	<primitive: 50>
	aNumber = 0 ifTrue: [^self error: 'Division by zero'].
	^self retry: #/ coercing: aNumber!

sqrt
	<primitive: 55>
	self primitiveFailed!

sin
	<primitive: 56>
	self primitiveFailed!

cos
	^(self + 1.5707963267948966) sin!

arcTan
	<primitive: 57>
	self primitiveFailed!

ln
	<primitive: 58>
	self primitiveFailed!

exp
	<primitive: 59>
	self primitiveFailed! !

!Float methodsFor: 'truncation and round off'!
truncated
	"Too large for a SmallInteger: the high part and the low part."
	| q |
	<primitive: 51>
	q := (self / 65536.0) truncated.
	^q * 65536 + (self - (q * 65536.0)) truncated! !

!Float methodsFor: 'truncation and round off'!
rounded
	^(self + (self sign * 0.5)) truncated! !

!Float methodsFor: 'coercing'!
generality
	^80!

coerce: aNumber
	^aNumber asFloat!

asFloat
	^self!

hash
	^self truncated hash! !

!Float methodsFor: 'printing'!
printString
	<primitive: 130>
	self primitiveFailed!

printOn: aStream
	aStream nextPutAll: self printString! !

!Point class methodsFor: 'instance creation'!
x: xInteger y: yInteger
	^self new setX: xInteger setY: yInteger! !

!Point methodsFor: 'private'!
setX: xValue setY: yValue
	x := xValue.
	y := yValue! !

!Point methodsFor: 'accessing'!
x
	^x!

y
	^y! !

!Point methodsFor: 'arithmetic'!
+ arg
	| p |
	p := arg asPoint.
	^(x + p x) @ (y + p y)!

- arg
	| p |
	p := arg asPoint.
	^(x - p x) @ (y - p y)!

* arg
	| p |
	p := arg asPoint.
	^(x * p x) @ (y * p y)!

/ arg
	| p |
	p := arg asPoint.
	^(x / p x) @ (y / p y)!

// arg
	| p |
	p := arg asPoint.
	^(x // p x) @ (y // p y)!

abs
	^x abs @ y abs! !

!Point methodsFor: 'comparing'!
= aPoint
	^(aPoint isKindOf: Point) and: [x = aPoint x and: [y = aPoint y]]!

hash
	^x hash bitXor: y hash!

< aPoint
	^x < aPoint x and: [y < aPoint y]!

> aPoint
	^x > aPoint x and: [y > aPoint y]!

<= aPoint
	^x <= aPoint x and: [y <= aPoint y]!

>= aPoint
	^x >= aPoint x and: [y >= aPoint y]!

max: aPoint
	^(x max: aPoint x) @ (y max: aPoint y)!

min: aPoint
	^(x min: aPoint x) @ (y min: aPoint y)! !

!Point methodsFor: 'point functions'!
dist: aPoint
	| dx dy |
	dx := aPoint x - x.
	dy := aPoint y - y.
	^(dx * dx + (dy * dy)) sqrt!

transpose
	^y @ x!

corner: aPoint
	^Rectangle origin: self corner: aPoint!

extent: aPoint
	^Rectangle origin: self corner: self + aPoint! !

!Point methodsFor: 'converting'!
asPoint
	^self! !

!Point methodsFor: 'printing'!
printOn: aStream
	x printOn: aStream.
	aStream nextPut: $@.
	y printOn: aStream! !

!Rectangle class methodsFor: 'instance creation'!
origin: originPoint corner: cornerPoint
	^self new setOrigin: originPoint corner: cornerPoint! !

!Rectangle methodsFor: 'private'!
setOrigin: originPoint corner: cornerPoint
	origin := originPoint.
	corner := cornerPoint! !

!Rectangle methodsFor: 'accessing'!
origin
	^origin!

corner
	^corner!

width
	^corner x - origin x!

height
	^corner y - origin y!

extent
	^corner - origin!

area
	^self width * self height! !

!Rectangle methodsFor: 'rectangle functions'!
center
	"The Blue Book's example of compiled code (St_bytecode.mli):
	0 1 176 119 185 124."
	^origin + corner / 2!

containsPoint: aPoint
	^origin <= aPoint and: [aPoint < corner]!

intersects: aRectangle
	^(origin max: aRectangle origin) < (corner min: aRectangle corner)! !

!Rectangle methodsFor: 'comparing'!
= aRectangle
	^(aRectangle isKindOf: Rectangle) and: [origin = aRectangle origin and: [corner = aRectangle corner]]!

hash
	^origin hash bitXor: corner hash! !

!Rectangle methodsFor: 'printing'!
printOn: aStream
	origin printOn: aStream.
	aStream nextPutAll: ' corner: '.
	corner printOn: aStream! !
|st});
("Collections.st", {st|"The collections, and their iteration protocol -- do:, collect:,
select:, reject:, detect:, inject:into: -- the one the rest of the
world copied (Ruby's each, map, select, detect, inject; Python's
comprehensions). Every collection answers them once it has do:.

OrderedCollection and Set grow the Blue Book's way: a bigger copy,
then 'self become: newSelf' (St_memory.mli), so that everything that
knew the collection now knows the bigger one."!

!Collection class methodsFor: 'instance creation'!
with: anObject
	^self new add: anObject; yourself!

with: first with: second
	^self new add: first; add: second; yourself!

with: first with: second with: third
	^self new add: first; add: second; add: third; yourself!

with: first with: second with: third with: fourth
	^self new add: first; add: second; add: third; add: fourth; yourself!

withAll: aCollection
	^self new addAll: aCollection; yourself! !

!Collection methodsFor: 'adding'!
add: anObject
	^self subclassResponsibility!

addAll: aCollection
	aCollection do: [:each | self add: each].
	^aCollection! !

!Collection methodsFor: 'removing'!
remove: oldObject
	^self remove: oldObject ifAbsent: [self error: 'Object is not in the collection']!

remove: oldObject ifAbsent: aBlock
	^self subclassResponsibility!

removeAll: aCollection
	aCollection do: [:each | self remove: each].
	^aCollection! !

!Collection methodsFor: 'testing'!
includes: anObject
	self do: [:each | anObject = each ifTrue: [^true]].
	^false!

isEmpty
	^self size = 0!

notEmpty
	^self isEmpty not!

occurrencesOf: anObject
	| tally |
	tally := 0.
	self do: [:each | anObject = each ifTrue: [tally := tally + 1]].
	^tally! !

!Collection methodsFor: 'accessing'!
size
	| tally |
	tally := 0.
	self do: [:each | tally := tally + 1].
	^tally! !

!Collection methodsFor: 'enumerating'!
do: aBlock
	^self subclassResponsibility!

do: aBlock separatedBy: separatorBlock
	| first |
	first := true.
	self do: [:each |
		first ifTrue: [first := false] ifFalse: [separatorBlock value].
		aBlock value: each]!

collect: aBlock
	| result |
	result := OrderedCollection new.
	self do: [:each | result add: (aBlock value: each)].
	^result!

select: aBlock
	| result |
	result := self species new.
	self do: [:each | (aBlock value: each) ifTrue: [result add: each]].
	^result!

reject: aBlock
	^self select: [:each | (aBlock value: each) == false]!

detect: aBlock
	^self detect: aBlock ifNone: [self error: 'Object is not in the collection']!

detect: aBlock ifNone: exceptionBlock
	self do: [:each | (aBlock value: each) ifTrue: [^each]].
	^exceptionBlock value!

inject: thisValue into: binaryBlock
	| nextValue |
	nextValue := thisValue.
	self do: [:each | nextValue := binaryBlock value: nextValue value: each].
	^nextValue!

anySatisfy: aBlock
	self do: [:each | (aBlock value: each) ifTrue: [^true]].
	^false!

allSatisfy: aBlock
	self do: [:each | (aBlock value: each) ifFalse: [^false]].
	^true!

max
	^self inject: self anyOne into: [:a :b | a max: b]!

min
	^self inject: self anyOne into: [:a :b | a min: b]!

sum
	^self inject: 0 into: [:a :b | a + b]!

anyOne
	self do: [:each | ^each].
	^self error: 'Collection is empty'! !

!Collection methodsFor: 'converting'!
asArray
	| result i |
	result := Array new: self size.
	i := 0.
	self do: [:each | result at: (i := i + 1) put: each].
	^result!

asOrderedCollection
	^OrderedCollection withAll: self!

asBag
	^Bag withAll: self!

asSet
	^Set withAll: self!

asSortedCollection
	^SortedCollection withAll: self!

asSortedCollection: aBlock
	^(SortedCollection sortBlock: aBlock) addAll: self; yourself! !

!Collection methodsFor: 'private'!
species
	^self class! !

!Collection methodsFor: 'printing'!
printOn: aStream
	"OrderedCollection (1 2 3 ), as the Blue Book prints it."
	aStream nextPutAll: self class name; nextPutAll: ' ('.
	self do: [:element | element printOn: aStream. aStream space].
	aStream nextPut: $)! !

!SequenceableCollection methodsFor: 'comparing'!
= aCollection
	self class == aCollection class ifFalse: [^false].
	self size = aCollection size ifFalse: [^false].
	1 to: self size do: [:i | (self at: i) = (aCollection at: i) ifFalse: [^false]].
	^true!

hash
	^self size! !

!SequenceableCollection methodsFor: 'accessing'!
first
	self isEmpty ifTrue: [^self error: 'Collection is empty'].
	^self at: 1!

last
	self isEmpty ifTrue: [^self error: 'Collection is empty'].
	^self at: self size!

indexOf: anElement
	1 to: self size do: [:i | (self at: i) = anElement ifTrue: [^i]].
	^0!

allButFirst
	^self copyFrom: 2 to: self size!

allButLast
	^self copyFrom: 1 to: self size - 1! !

!SequenceableCollection methodsFor: 'enumerating'!
do: aBlock
	1 to: self size do: [:i | aBlock value: (self at: i)]!

reverseDo: aBlock
	self size to: 1 by: -1 do: [:i | aBlock value: (self at: i)]!

doWithIndex: aBlock
	1 to: self size do: [:i | aBlock value: (self at: i) value: i]!

keysAndValuesDo: aBlock
	1 to: self size do: [:i | aBlock value: i value: (self at: i)]!

with: aCollection do: aBlock
	1 to: self size do: [:i | aBlock value: (self at: i) value: (aCollection at: i)]!

collect: aBlock
	| result |
	result := self species new: self size.
	1 to: self size do: [:i | result at: i put: (aBlock value: (self at: i))].
	^result!

select: aBlock
	| result |
	result := OrderedCollection new.
	self do: [:each | (aBlock value: each) ifTrue: [result add: each]].
	^(self species inheritsFrom: ArrayedCollection)
		ifTrue: [(self species new: result size) replaceFrom: 1 to: result size with: result startingAt: 1]
		ifFalse: [result]! !

!SequenceableCollection methodsFor: 'copying'!
copyFrom: start to: stop
	| result |
	result := self species new: (stop - start + 1 max: 0).
	start to: stop do: [:i | result at: i - start + 1 put: (self at: i)].
	^result!

copyWith: newElement
	| result |
	result := self species new: self size + 1.
	result replaceFrom: 1 to: self size with: self startingAt: 1.
	result at: result size put: newElement.
	^result!

reversed
	| result n |
	n := self size.
	result := self species new: n.
	1 to: n do: [:i | result at: i put: (self at: n + 1 - i)].
	^result!

reverse
	^self reversed!

replaceFrom: start to: stop with: replacement startingAt: repStart
	| j |
	j := repStart.
	start to: stop do: [:i | self at: i put: (replacement at: j). j := j + 1]!

, aSequenceableCollection
	| result |
	result := self species new: self size + aSequenceableCollection size.
	result replaceFrom: 1 to: self size with: self startingAt: 1.
	result replaceFrom: self size + 1 to: result size with: aSequenceableCollection startingAt: 1.
	^result! !

!SequenceableCollection methodsFor: 'converting'!
readStream
	^ReadStream on: self! !

!ArrayedCollection class methodsFor: 'instance creation'!
new
	^self new: 0!

with: anObject
	^(self new: 1) at: 1 put: anObject; yourself!

with: first with: second
	^(self new: 2) at: 1 put: first; at: 2 put: second; yourself!

with: first with: second with: third
	^(self new: 3) at: 1 put: first; at: 2 put: second; at: 3 put: third; yourself!

with: first with: second with: third with: fourth
	^(self new: 4) at: 1 put: first; at: 2 put: second; at: 3 put: third; at: 4 put: fourth; yourself!

withAll: aCollection
	| result i |
	result := self new: aCollection size.
	i := 0.
	aCollection do: [:each | result at: (i := i + 1) put: each].
	^result! !

!ArrayedCollection methodsFor: 'accessing'!
size
	<primitive: 62>
	^0! !

!ArrayedCollection methodsFor: 'adding'!
add: anObject
	^self shouldNotImplement! !

!ArrayedCollection methodsFor: 'copying'!
replaceFrom: start to: stop with: replacement startingAt: repStart
	"The primitive when both are pointers or both bytes."
	<primitive: 105>
	^super replaceFrom: start to: stop with: replacement startingAt: repStart! !

!ArrayedCollection methodsFor: 'sorting'!
asSortedArray
	^self asSortedCollection asArray! !

!Array methodsFor: 'testing'!
isArray
	^true! !

!Array methodsFor: 'printing'!
printOn: aStream
	"#(1 $a 'b'), as a literal reads"
	aStream nextPutAll: '#('.
	self do: [:each | each printOn: aStream] separatedBy: [aStream space].
	aStream nextPut: $)! !

!String class methodsFor: 'instance creation'!
new
	^self new: 0! !

!String methodsFor: 'accessing'!
at: index
	"A Character: the bytes are characters."
	<primitive: 63>
	^self checkIndex: index!

at: index put: aCharacter
	<primitive: 64>
	aCharacter isCharacter ifFalse: [^self error: 'Strings only store Characters'].
	^self checkIndex: index!

size
	<primitive: 62>
	^0! !

!String methodsFor: 'comparing'!
= aString
	<primitive: 123>
	^false!

< aString
	<primitive: 124>
	^self error: 'Not a String'!

> aString
	^aString < self!

<= aString
	^(aString < self) not!

>= aString
	^(self < aString) not!

hash
	<primitive: 125>
	^0! !

!String methodsFor: 'testing'!
isString
	^true!

includesSubstring: aString
	| n |
	n := aString size.
	1 to: self size - n + 1 do: [:i | (self copyFrom: i to: i + n - 1) = aString ifTrue: [^true]].
	^false! !

!String methodsFor: 'converting'!
asSymbol
	<primitive: 122>
	self primitiveFailed!

asString
	^self!

asNumber
	"The number the text reads as, or nil."
	<primitive: 150>
	^nil!

asUppercase
	^self collect: [:c | c asUppercase]!

asLowercase
	^self collect: [:c | c asLowercase]!

displayString
	^self!

species
	^String! !

!String methodsFor: 'printing'!
printOn: aStream
	"'it''s': quoted, a quote doubled"
	aStream nextPut: $'.
	self do: [:c | aStream nextPut: c. c == $' ifTrue: [aStream nextPut: c]].
	aStream nextPut: $'! !

!Symbol class methodsFor: 'instance creation'!
new: anInteger
	^self error: 'Symbols are made by asSymbol, unique'! !

!Symbol methodsFor: 'comparing'!
= anObject
	"Symbols are unique: equal is identical."
	^self == anObject!

hash
	^self identityHash! !

!Symbol methodsFor: 'accessing'!
at: index put: aCharacter
	^self error: 'Symbols cannot be changed'! !

!Symbol methodsFor: 'converting'!
asSymbol
	^self!

asString
	| s |
	s := String new: self size.
	s replaceFrom: 1 to: self size with: self startingAt: 1.
	^s!

numArgs
	| n |
	n := 0.
	self do: [:c | c == $: ifTrue: [n := n + 1]].
	(n = 0 and: [(self at: 1) isLetter not]) ifTrue: [^1].
	^n! !

!Symbol methodsFor: 'testing'!
isSymbol
	^true! !

!Symbol methodsFor: 'printing'!
printOn: aStream
	aStream nextPut: $#; nextPutAll: self!

displayString
	^self asString! !

!Symbol methodsFor: 'copying'!
shallowCopy
	^self! !

!ByteArray methodsFor: 'accessing'!
size
	<primitive: 62>
	^0! !

!Interval class methodsFor: 'instance creation'!
from: start to: stop by: step
	^self new setFrom: start to: stop by: step! !

!Interval methodsFor: 'private'!
setFrom: startNumber to: stopNumber by: stepNumber
	start := startNumber.
	stop := stopNumber.
	step := stepNumber!

species
	^Array! !

!Interval methodsFor: 'accessing'!
size
	step > 0
		ifTrue: [stop < start ifTrue: [^0]]
		ifFalse: [start < stop ifTrue: [^0]].
	^stop - start // step + 1!

at: index
	(index >= 1 and: [index <= self size]) ifTrue: [^start + (step * (index - 1))].
	^self error: 'Index out of bounds'!

at: index put: anObject
	^self error: 'Intervals cannot be changed'! !

!Interval methodsFor: 'enumerating'!
do: aBlock
	| i |
	i := start.
	step > 0
		ifTrue: [[i <= stop] whileTrue: [aBlock value: i. i := i + step]]
		ifFalse: [[i >= stop] whileTrue: [aBlock value: i. i := i + step]]!

collect: aBlock
	| result i |
	result := Array new: self size.
	i := 0.
	self do: [:each | result at: (i := i + 1) put: (aBlock value: each)].
	^result! !

!Interval methodsFor: 'printing'!
printOn: aStream
	aStream nextPut: $(; print: start; nextPutAll: ' to: '; print: stop.
	step = 1 ifFalse: [aStream nextPutAll: ' by: '; print: step].
	aStream nextPut: $)! !

!OrderedCollection class methodsFor: 'instance creation'!
new
	^self new: 8!

new: anInteger
	^(self basicNew: anInteger) setIndices! !

!OrderedCollection methodsFor: 'private'!
setIndices
	firstIndex := self basicSize // 2 max: 1.
	lastIndex := firstIndex - 1!

setFirst: first last: last
	firstIndex := first.
	lastIndex := last!

grow
	"A copy with room at both ends, then become: -- the receiver is the
	bigger one from now on, for whoever holds it."
	| newSelf newFirst n |
	n := self basicSize * 2 + 8.
	newSelf := self class basicNew: n.
	1 to: self class instSize do: [:i | newSelf instVarAt: i put: (self instVarAt: i)].
	newFirst := n - self size // 2 + 1.
	1 to: self size do: [:i | newSelf basicAt: newFirst + i - 1 put: (self basicAt: firstIndex + i - 1)].
	newSelf setFirst: newFirst last: newFirst + self size - 1.
	self become: newSelf! !

!OrderedCollection methodsFor: 'accessing'!
size
	^lastIndex - firstIndex + 1!

at: index
	(index < 1 or: [index > self size]) ifTrue: [^self error: 'Index out of bounds: ', index printString].
	^self basicAt: index + firstIndex - 1!

at: index put: anObject
	(index < 1 or: [index > self size]) ifTrue: [^self error: 'Index out of bounds: ', index printString].
	^self basicAt: index + firstIndex - 1 put: anObject! !

!OrderedCollection methodsFor: 'adding'!
add: anObject
	^self addLast: anObject!

addLast: anObject
	lastIndex = self basicSize ifTrue: [self grow].
	lastIndex := lastIndex + 1.
	^self basicAt: lastIndex put: anObject!

addFirst: anObject
	firstIndex = 1 ifTrue: [self grow].
	firstIndex := firstIndex - 1.
	^self basicAt: firstIndex put: anObject! !

!OrderedCollection methodsFor: 'removing'!
removeFirst
	| answer |
	self isEmpty ifTrue: [^self error: 'Collection is empty'].
	answer := self basicAt: firstIndex.
	self basicAt: firstIndex put: nil.
	firstIndex := firstIndex + 1.
	^answer!

removeLast
	| answer |
	self isEmpty ifTrue: [^self error: 'Collection is empty'].
	answer := self basicAt: lastIndex.
	self basicAt: lastIndex put: nil.
	lastIndex := lastIndex - 1.
	^answer!

remove: oldObject ifAbsent: aBlock
	| index |
	index := firstIndex.
	[index <= lastIndex] whileTrue: [
		oldObject = (self basicAt: index) ifTrue: [
			index to: lastIndex - 1 do: [:i | self basicAt: i put: (self basicAt: i + 1)].
			self basicAt: lastIndex put: nil.
			lastIndex := lastIndex - 1.
			^oldObject].
		index := index + 1].
	^aBlock value! !

!OrderedCollection methodsFor: 'enumerating'!
do: aBlock
	firstIndex to: lastIndex do: [:i | aBlock value: (self basicAt: i)]!

collect: aBlock
	| result |
	result := OrderedCollection new: self size.
	self do: [:each | result addLast: (aBlock value: each)].
	^result!

select: aBlock
	| result |
	result := self copyEmpty.
	self do: [:each | (aBlock value: each) ifTrue: [result add: each]].
	^result!

copyEmpty
	^self class new! !

!OrderedCollection methodsFor: 'copying'!
copyFrom: start to: stop
	| result |
	result := OrderedCollection new.
	start to: stop do: [:i | result addLast: (self at: i)].
	^result!

, aCollection
	^self copy addAll: aCollection; yourself!

copy
	^self collect: [:each | each]!

reversed
	| result |
	result := OrderedCollection new.
	self do: [:each | result addFirst: each].
	^result! !

!SortedCollection class methodsFor: 'instance creation'!
new
	^self sortBlock: [:a :b | a <= b]!

sortBlock: aBlock
	^(self new: 8) setSortBlock: aBlock! !

!SortedCollection methodsFor: 'private'!
setSortBlock: aBlock
	sortBlock := aBlock!

copyEmpty
	^SortedCollection sortBlock: sortBlock! !

!SortedCollection methodsFor: 'adding'!
add: anObject
	"Put at the end, then moved down past the ones it sorts before:
	an insertion sort, one element at a time."
	| i |
	self addLast: anObject.
	i := lastIndex.
	[i > firstIndex and: [sortBlock value: anObject value: (self basicAt: i - 1)]] whileTrue: [
		self basicAt: i put: (self basicAt: i - 1).
		i := i - 1].
	self basicAt: i put: anObject.
	^anObject!

addFirst: anObject
	^self shouldNotImplement!

at: index put: anObject
	^self shouldNotImplement! !

!SortedCollection methodsFor: 'accessing'!
sortBlock
	^sortBlock!

median
	^self at: self size + 1 // 2! !

!SortedCollection methodsFor: 'enumerating'!
collect: aBlock
	"The answers are not sorted: an OrderedCollection."
	| result |
	result := OrderedCollection new: self size.
	self do: [:each | result addLast: (aBlock value: each)].
	^result! !

!Set class methodsFor: 'instance creation'!
new
	^self new: 16!

new: anInteger
	^(self basicNew: anInteger) setTally! !

!Set methodsFor: 'private'!
setTally
	tally := 0!

findIndexFor: anObject
	"Where anObject is, or the empty slot where it would go: its hash,
	then the next slots in turn (open addressing)."
	| index element |
	index := anObject hash \\ self basicSize + 1.
	[element := self basicAt: index.
	element == nil or: [self matches: element key: anObject]]
		whileFalse: [index := index \\ self basicSize + 1].
	^index!

matches: element key: anObject
	^element = anObject!

grow
	| newSelf |
	newSelf := self class new: self basicSize * 2.
	"the slots themselves: a Dictionary's do: gives its values"
	1 to: self basicSize do: [:i |
		(self basicAt: i) == nil ifFalse: [newSelf noCheckAdd: (self basicAt: i)]].
	self become: newSelf!

noCheckAdd: anObject
	self basicAt: (self findIndexFor: (self keyOf: anObject)) put: anObject.
	tally := tally + 1!

keyOf: element
	^element! !

!Set methodsFor: 'accessing'!
size
	^tally! !

!Set methodsFor: 'adding'!
add: newObject
	| index |
	newObject == nil ifTrue: [^newObject].
	index := self findIndexFor: newObject.
	(self basicAt: index) == nil ifTrue: [
		self basicAt: index put: newObject.
		tally := tally + 1.
		tally * 4 > (self basicSize * 3) ifTrue: [self grow]].
	^newObject! !

!Set methodsFor: 'removing'!
remove: oldObject ifAbsent: aBlock
	| index rest |
	index := self findIndexFor: oldObject.
	(self basicAt: index) == nil ifTrue: [^aBlock value].
	self basicAt: index put: nil.
	tally := tally - 1.
	"the ones after it, in its run, put again"
	rest := OrderedCollection new.
	index := index \\ self basicSize + 1.
	[(self basicAt: index) == nil] whileFalse: [
		rest add: (self basicAt: index).
		self basicAt: index put: nil.
		tally := tally - 1.
		index := index \\ self basicSize + 1].
	rest do: [:each | self noCheckAdd: each].
	^oldObject! !

!Set methodsFor: 'testing'!
includes: anObject
	^(self basicAt: (self findIndexFor: anObject)) ~~ nil! !

!Set methodsFor: 'enumerating'!
do: aBlock
	1 to: self basicSize do: [:i | | each | each := self basicAt: i. each == nil ifFalse: [aBlock value: each]]! !

!Dictionary methodsFor: 'private'!
matches: element key: aKey
	^element key = aKey!

keyOf: element
	^element key! !

!Dictionary methodsFor: 'accessing'!
at: key
	^self at: key ifAbsent: [self error: 'Key not found: ', key printString]!

at: key ifAbsent: aBlock
	| assoc |
	assoc := self basicAt: (self findIndexFor: key).
	assoc == nil ifTrue: [^aBlock value].
	^assoc value!

at: key put: value
	| index assoc |
	index := self findIndexFor: key.
	assoc := self basicAt: index.
	assoc == nil
		ifTrue: [
			self basicAt: index put: (Association key: key value: value).
			tally := tally + 1.
			tally * 4 > (self basicSize * 3) ifTrue: [self grow]]
		ifFalse: [assoc value: value].
	^value!

associationAt: key
	^self basicAt: (self findIndexFor: key)!

keys
	| result |
	result := Set new.
	self associationsDo: [:assoc | result add: assoc key].
	^result!

values
	| result |
	result := OrderedCollection new.
	self do: [:value | result add: value].
	^result!

keyAtValue: value
	self associationsDo: [:assoc | assoc value = value ifTrue: [^assoc key]].
	^nil! !

!Dictionary methodsFor: 'adding'!
add: anAssociation
	self at: anAssociation key put: anAssociation value.
	^anAssociation! !

!Dictionary methodsFor: 'removing'!
removeKey: key
	^self removeKey: key ifAbsent: [self error: 'Key not found']!

removeKey: key ifAbsent: aBlock
	| assoc |
	assoc := self basicAt: (self findIndexFor: key).
	assoc == nil ifTrue: [^aBlock value].
	super remove: key ifAbsent: [].
	^assoc value! !

!Dictionary methodsFor: 'testing'!
includesKey: key
	^(self basicAt: (self findIndexFor: key)) ~~ nil!

includes: value
	self do: [:each | each = value ifTrue: [^true]].
	^false! !

!Dictionary methodsFor: 'enumerating'!
associationsDo: aBlock
	super do: aBlock!

do: aBlock
	self associationsDo: [:assoc | aBlock value: assoc value]!

keysDo: aBlock
	self associationsDo: [:assoc | aBlock value: assoc key]!

keysAndValuesDo: aBlock
	self associationsDo: [:assoc | aBlock value: assoc key value: assoc value]!

collect: aBlock
	| result |
	result := Bag new.
	self do: [:each | result add: (aBlock value: each)].
	^result!

select: aBlock
	| result |
	result := Dictionary new.
	self associationsDo: [:assoc | (aBlock value: assoc value) ifTrue: [result add: assoc]].
	^result! !

!Dictionary methodsFor: 'printing'!
printOn: aStream
	aStream nextPutAll: self class name; nextPutAll: ' ('.
	self associationsDo: [:assoc | aStream print: assoc; space].
	aStream nextPut: $)! !

!Bag class methodsFor: 'instance creation'!
new
	^self basicNew setContents! !

!Bag methodsFor: 'private'!
setContents
	contents := Dictionary new! !

!Bag methodsFor: 'adding'!
add: newObject
	^self add: newObject withOccurrences: 1!

add: newObject withOccurrences: anInteger
	contents at: newObject put: (contents at: newObject ifAbsent: [0]) + anInteger.
	^newObject! !

!Bag methodsFor: 'removing'!
remove: oldObject ifAbsent: aBlock
	| count |
	count := contents at: oldObject ifAbsent: [^aBlock value].
	count = 1
		ifTrue: [contents removeKey: oldObject]
		ifFalse: [contents at: oldObject put: count - 1].
	^oldObject! !

!Bag methodsFor: 'accessing'!
size
	| tally |
	tally := 0.
	contents do: [:count | tally := tally + count].
	^tally!

occurrencesOf: anObject
	^contents at: anObject ifAbsent: [0]! !

!Bag methodsFor: 'enumerating'!
do: aBlock
	contents associationsDo: [:assoc | assoc value timesRepeat: [aBlock value: assoc key]]! !
|st});
("Streams.st", {st|"Streams over collections: printString is printOn: into a WriteStream
on a String, and a ReadStream reads one element at a time."!

!Stream methodsFor: 'accessing'!
next
	^self subclassResponsibility!

nextPut: anObject
	^self subclassResponsibility!

nextPutAll: aCollection
	aCollection do: [:each | self nextPut: each].
	^aCollection!

atEnd
	^self subclassResponsibility!

do: aBlock
	[self atEnd] whileFalse: [aBlock value: self next]! !

!Stream methodsFor: 'printing'!
print: anObject
	anObject printOn: self!

cr
	self nextPut: Character cr!

tab
	self nextPut: Character tab!

space
	self nextPut: Character space! !

!PositionableStream class methodsFor: 'instance creation'!
on: aCollection
	^self basicNew on: aCollection! !

!PositionableStream methodsFor: 'private'!
on: aCollection
	collection := aCollection.
	readLimit := aCollection size.
	position := 0! !

!PositionableStream methodsFor: 'accessing'!
contents
	^collection copyFrom: 1 to: readLimit!

position
	^position!

position: anInteger
	position := anInteger!

reset
	position := 0!

isEmpty
	^readLimit = 0!

atEnd
	^position >= readLimit!

peek
	self atEnd ifTrue: [^nil].
	^collection at: position + 1!

skip: anInteger
	position := (position + anInteger min: readLimit) max: 0!

upTo: anObject
	| start |
	start := position + 1.
	[self atEnd or: [self next = anObject]] whileFalse.
	^collection copyFrom: start to: (self atEnd ifTrue: [position] ifFalse: [position - 1])!

upToEnd
	| start |
	start := position + 1.
	position := readLimit.
	^collection copyFrom: start to: readLimit!

skipSeparators
	[self atEnd not and: [self peek isSeparator]] whileTrue: [self next]! !

!ReadStream methodsFor: 'accessing'!
next
	self atEnd ifTrue: [^nil].
	position := position + 1.
	^collection at: position!

nextPut: anObject
	^self shouldNotImplement! !

!WriteStream class methodsFor: 'instance creation'!
with: aCollection
	^(self on: aCollection) setToEnd! !

!WriteStream methodsFor: 'private'!
on: aCollection
	super on: aCollection.
	readLimit := 0.
	writeLimit := aCollection size!

setToEnd
	position := readLimit := writeLimit!

growCollection
	| bigger |
	bigger := collection species new: collection size * 2 + 16.
	bigger replaceFrom: 1 to: collection size with: collection startingAt: 1.
	collection := bigger.
	writeLimit := collection size! !

!WriteStream methodsFor: 'accessing'!
nextPut: anObject
	position >= writeLimit ifTrue: [self growCollection].
	position := position + 1.
	collection at: position put: anObject.
	position > readLimit ifTrue: [readLimit := position].
	^anObject!

contents
	^collection copyFrom: 1 to: position!

next
	^self shouldNotImplement! !

!ReadWriteStream methodsFor: 'accessing'!
next
	self atEnd ifTrue: [^nil].
	position := position + 1.
	^collection at: position!

contents
	^collection copyFrom: 1 to: readLimit! !

!SequenceableCollection methodsFor: 'converting'!
writeStream
	^WriteStream on: self! !
|st});
("Graphics.st", {st|"The display, as the Blue Book's chapters 18 to 20 have it: Forms,
pictures of one bit per pixel; BitBlt, the one primitive that draws
them (St_bitblt.mli); and Pen, a turtle that draws lines with it,
Logo's idea brought to Smalltalk (Papert and Kay were friends).
Display is the Form of the screen: what a Pen draws there is drawn over
the windows, until the screen menu's 'restore display' erases it."!

!Form class methodsFor: 'instance creation'!
extent: aPoint
	^self width: aPoint x height: aPoint y!

width: w height: h
	^self new setBits: (ByteArray new: w + 15 // 16 * 2 * h) width: w height: h! !

!Form methodsFor: 'private'!
setBits: aByteArray width: w height: h
	bits := aByteArray.
	width := w.
	height := h! !

!Form methodsFor: 'accessing'!
width
	^width!

height
	^height!

extent
	^width @ height!

boundingBox
	^0 @ 0 corner: width @ height!

center
	^(width // 2) @ (height // 2)!

bits
	^bits! !

!Form methodsFor: 'displaying'!
fill: aRectangle rule: anInteger
	(BitBlt destForm: self sourceForm: nil halftoneForm: nil combinationRule: anInteger
		destOrigin: aRectangle origin sourceOrigin: 0 @ 0 extent: aRectangle extent clipRect: self boundingBox) copyBits!

fillWhite
	self fill: self boundingBox rule: 0!

fillBlack
	self fill: self boundingBox rule: 15!

reverse
	self fill: self boundingBox rule: 10!

displayOn: aForm at: aPoint rule: anInteger
	(BitBlt destForm: aForm sourceForm: self halftoneForm: nil combinationRule: anInteger
		destOrigin: aPoint sourceOrigin: 0 @ 0 extent: self extent clipRect: aForm boundingBox) copyBits!

displayAt: aPoint
	self displayOn: Display at: aPoint rule: 3!

pixelAt: aPoint
	"0 or 1, read the BitBlt way: the pixel copied onto a Form of one."
	| one |
	one := Form width: 1 height: 1.
	(BitBlt destForm: one sourceForm: self halftoneForm: nil combinationRule: 3
		destOrigin: 0 @ 0 sourceOrigin: aPoint extent: 1 @ 1 clipRect: one boundingBox) copyBits.
	^(one bits at: 1) bitShift: -7! !

!Form methodsFor: 'printing'!
printOn: aStream
	aStream nextPutAll: 'a Form('; print: width; nextPut: $x; print: height; nextPut: $)! !

!BitBlt class methodsFor: 'instance creation'!
destForm: df sourceForm: sf halftoneForm: hf combinationRule: cr destOrigin: destOrigin sourceOrigin: sourceOrigin extent: extent clipRect: clipRect
	^self new
		setDestForm: df sourceForm: sf halftoneForm: hf combinationRule: cr
		destOrigin: destOrigin sourceOrigin: sourceOrigin extent: extent clipRect: clipRect! !

!BitBlt methodsFor: 'private'!
setDestForm: df sourceForm: sf halftoneForm: hf combinationRule: cr destOrigin: destOrigin sourceOrigin: sourceOrigin extent: extent clipRect: clipRect
	destForm := df.
	sourceForm := sf.
	halftoneForm := hf.
	combinationRule := cr.
	destX := destOrigin x.
	destY := destOrigin y.
	sourceX := sourceOrigin x.
	sourceY := sourceOrigin y.
	width := extent x.
	height := extent y.
	clipX := clipRect origin x.
	clipY := clipRect origin y.
	clipWidth := clipRect width.
	clipHeight := clipRect height! !

!BitBlt methodsFor: 'accessing'!
destForm: aForm
	destForm := aForm!

sourceForm: aForm
	sourceForm := aForm!

halftoneForm: aForm
	halftoneForm := aForm!

combinationRule: anInteger
	combinationRule := anInteger!

destOrigin: aPoint
	destX := aPoint x.
	destY := aPoint y!

destX: x destY: y
	destX := x.
	destY := y!

width: w height: h
	width := w.
	height := h!

clipRect: aRectangle
	clipX := aRectangle origin x.
	clipY := aRectangle origin y.
	clipWidth := aRectangle width.
	clipHeight := aRectangle height! !

!BitBlt methodsFor: 'copying'!
copyBits
	"The one drawing primitive (St_bitblt.mli)."
	<primitive: 96>
	^self error: 'Bad BitBlt arguments'! !

!Pen class methodsFor: 'instance creation'!
new
	"A pen of one pixel on the Display, at its center, pointing up."
	^super new defaultNib: 1! !

!Pen methodsFor: 'initialize'!
defaultNib: n
	destForm := Display.
	sourceForm := (Form width: n height: n) fillBlack; yourself.
	combinationRule := 7.
	width := n.
	height := n.
	sourceX := 0.
	sourceY := 0.
	self clipRect: Display boundingBox.
	frame := Display boundingBox.
	location := Display center.
	direction := 270.
	penDown := true! !

!Pen methodsFor: 'accessing'!
location
	^location!

direction
	^direction! !

!Pen methodsFor: 'moving'!
up
	penDown := false!

down
	penDown := true!

turn: degrees
	"Clockwise, the y axis going down."
	direction := direction + degrees \\ 360!

north
	direction := 270!

home
	location := Display center!

place: aPoint
	location := aPoint!

go: distance
	"Forward, drawing if the pen is down."
	| old radians |
	old := location.
	radians := direction degreesToRadians.
	location := location + ((radians cos * distance) @ (radians sin * distance)).
	penDown ifTrue: [self drawFrom: old to: location]!

goto: aPoint
	| old |
	old := location.
	location := aPoint.
	penDown ifTrue: [self drawFrom: old to: location]!

drawFrom: startPoint to: endPoint
	"Bresenham's line (1965), a copyBits of the nib at each point:
	the error term says when to step across as well as along."
	| x0 y0 x1 y1 dx dy sx sy err e2 |
	x0 := startPoint x rounded.
	y0 := startPoint y rounded.
	x1 := endPoint x rounded.
	y1 := endPoint y rounded.
	dx := (x1 - x0) abs.
	dy := (y1 - y0) abs negated.
	sx := x0 < x1 ifTrue: [1] ifFalse: [-1].
	sy := y0 < y1 ifTrue: [1] ifFalse: [-1].
	err := dx + dy.
	[destX := x0.
	destY := y0.
	self copyBits.
	x0 = x1 and: [y0 = y1]] whileFalse: [
		e2 := 2 * err.
		e2 >= dy ifTrue: [err := err + dy. x0 := x0 + sx].
		e2 <= dx ifTrue: [err := err + dx. y0 := y0 + sy]]! !

!Pen methodsFor: 'geometric designs'!
dragon: n
	"The dragon curve, the Blue Book's (chapter 20): Pen new dragon: 9."
	n = 0
		ifTrue: [self go: 10]
		ifFalse: [
			n > 0
				ifTrue: [self dragon: n - 1; turn: 90; dragon: 1 - n]
				ifFalse: [self dragon: -1 - n; turn: -90; dragon: 1 + n]]!

spiral: n angle: a
	"Pen new spiral: 200 angle: 89."
	1 to: n do: [:i | self go: i; turn: a]!

polygon: sides length: length
	sides timesRepeat: [self go: length; turn: 360 // sides]!

scribble
	"Pen new scribble: press the button and draw, release to stop --
	the mouse read by Smalltalk while it runs."
	self place: Sensor waitButton.
	[Sensor anyButtonPressed] whileTrue: [self goto: Sensor mousePoint]! !

!Number methodsFor: 'converting'!
degreesToRadians
	^self asFloat * 0.017453292519943295! !

!InputSensor methodsFor: 'mouse'!
mousePoint
	"Where the mouse is, on the Display."
	<primitive: 90>
	self primitiveFailed!

cursorPoint
	^self mousePoint!

buttons
	<primitive: 91>
	self primitiveFailed!

redButtonPressed
	^(self buttons bitAnd: 4) ~= 0!

yellowButtonPressed
	^(self buttons bitAnd: 2) ~= 0!

blueButtonPressed
	^(self buttons bitAnd: 1) ~= 0!

anyButtonPressed
	^self buttons ~= 0!

waitButton
	[self anyButtonPressed] whileFalse.
	^self mousePoint!

waitNoButton
	[self anyButtonPressed] whileTrue! !
|st});
("System.st", {st|"The last chunks the bootstrap runs, once every method is compiled:
the globals that are objects rather than classes."!

Smalltalk at: #Transcript put: TextCollector new!

Smalltalk at: #Display put: (Form extent: 800 @ 600)!

Smalltalk at: #Sensor put: InputSensor new!

Rectangle comment: 'A rectangle of the screen, from its origin (top left) to its corner (bottom right), each a Point.'!

Point comment: 'A point of the screen, x to the right and y downwards: 3@4.'!

Object comment: 'The root of the classes: what every object answers.'!
|st});
]
let squeak = files @ [
("squeak/Closures.st", {st|"Squeak's kernel, the first file: the blocks. A kernel that defines
BlockClosure is compiled with closures (St_compile.mli): a block is no
longer a BlockContext sharing its method's temporaries but a
BlockClosure, an object holding what it needs from outside, and each
value of it runs in a context of its own. So a block can call itself,
and two blocks made by the same code do not share their arguments.

MethodContext's unused field gets its Squeak name: the closure a
context is running, nil for a method's."!

Object variableSubclass: #BlockClosure
	instanceVariableNames: 'outerContext startpc numArgs'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Kernel-Methods'!

ContextPart variableSubclass: #MethodContext
	instanceVariableNames: 'method closureOrNil receiver'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Kernel-Methods'!

!BlockClosure methodsFor: 'accessing'!
outerContext
	^outerContext!

home
	^outerContext home!

method
	^outerContext method!

receiver
	^outerContext receiver!

numArgs
	^numArgs!

numCopiedValues
	^self size! !

!BlockClosure methodsFor: 'evaluating'!
value
	"Evaluate the block, in a new context: the virtual machine makes
	a MethodContext whose closure is the receiver (St_interp.mli)."
	<primitive: 81>
	^self error: 'Wrong number of arguments'!

value: arg
	<primitive: 81>
	^self error: 'Wrong number of arguments'!

value: arg1 value: arg2
	<primitive: 81>
	^self error: 'Wrong number of arguments'!

value: arg1 value: arg2 value: arg3
	<primitive: 81>
	^self error: 'Wrong number of arguments'!

value: arg1 value: arg2 value: arg3 value: arg4
	<primitive: 81>
	^self error: 'Wrong number of arguments'!

valueWithArguments: anArray
	<primitive: 82>
	^self error: 'Wrong number of arguments'! !

!BlockClosure methodsFor: 'controlling'!
whileTrue: aBlock
	"Sent only when the compiler could not inline it."
	[self value] whileTrue: [aBlock value].
	^nil!

whileFalse: aBlock
	[self value] whileFalse: [aBlock value].
	^nil!

whileTrue
	[self value] whileTrue.
	^nil!

repeat
	[true] whileTrue: [self value]! !

!BlockClosure methodsFor: 'printing'!
printOn: aStream
	aStream nextPutAll: '[] in '.
	self home printOn: aStream! !

!MethodContext methodsFor: 'accessing'!
closure
	^closureOrNil!

home
	"The context of the method a block's ^ returns from: for a
	closure's activation, up its outer contexts."
	^closureOrNil == nil ifTrue: [self] ifFalse: [closureOrNil home]! !

!MethodContext methodsFor: 'printing'!
printOn: aStream
	| mclass |
	closureOrNil == nil ifFalse: [aStream nextPutAll: '[] in '].
	mclass := method methodClass.
	aStream nextPutAll: receiver class name.
	mclass == receiver class ifFalse: [aStream nextPut: $(; nextPutAll: mclass name; nextPut: $)].
	aStream nextPutAll: '>>'; nextPutAll: method selector! !
|st});
]
let mini_morphic = squeak @ [
("morphic/MiniMorphic.st", {st|"MiniMorphic: Morphic in one file, to read before the real one.

Morphic (John Maloney and Randall Smith, for Self, 1995; Squeak's
since 1997) has few ideas, all here:

- everything on the screen is a Morph: a rectangle of it, a colour,
  an owner and submorphs. The screen itself is one, the world; so is
  the hand, the mouse made visible, and what it carries are its
  submorphs;
- a morph is alive: the world sends step to every morph, every cycle,
  and a morph that wants to move just changes its position;
- nobody redraws: a morph that changed says where (changed), the
  world remembers those rectangles (its damage), and at the end of
  the cycle redraws only them, back to front, clipped;
- the cycle is the whole program: read the mouse, step, redraw.

What is left out is what makes Squeak's Morphic big, not different:
text, menus, halos, layout, events for the keyboard, colours (the
Display here has one bit a pixel: black, white, and two grays made of
both). The classes: Canvas (a Form to draw on, through one BitBlt),
Morph, AtomMorph (a morph that bounces: the demo), HandMorph,
WorldMorph. The interpreter knows none of them: all it gives is
BitBlt, the mouse and the clock (St_bitblt.mli, St_primitives.mli)."!

Object subclass: #Canvas
	instanceVariableNames: 'form blt clip'
	classVariableNames: 'Gray LightGray'
	poolDictionaries: ''
	category: 'MiniMorphic'!

Object subclass: #Morph
	instanceVariableNames: 'bounds owner submorphs color'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'MiniMorphic'!

Morph subclass: #AtomMorph
	instanceVariableNames: 'velocity'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'MiniMorphic'!

Morph subclass: #HandMorph
	instanceVariableNames: 'wasDown'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'MiniMorphic'!

Morph subclass: #WorldMorph
	instanceVariableNames: 'hand canvas damage'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'MiniMorphic'!

!Rectangle methodsFor: 'morphic'!
left
	^origin x!

top
	^origin y!

right
	^corner x!

bottom
	^corner y!

translateBy: aPoint
	^Rectangle origin: origin + aPoint corner: corner + aPoint!

insetBy: anInteger
	"Smaller by that much on every side."
	^Rectangle origin: origin + anInteger corner: corner - anInteger!

intersects: aRectangle
	"The kernel's, in four comparisons of numbers and no Point made:
	it is asked of every morph for every rectangle redrawn."
	^origin x < aRectangle corner x
		and: [aRectangle origin x < corner x
		and: [origin y < aRectangle corner y
		and: [aRectangle origin y < corner y]]]!

intersect: aRectangle
	"What the two have in common; empty (no width) if nothing."
	^Rectangle origin: (origin max: aRectangle origin) corner: (corner min: aRectangle corner)!

merge: aRectangle
	"The smallest rectangle holding both."
	^Rectangle origin: (origin min: aRectangle origin) corner: (corner max: aRectangle corner)! !

!Canvas class methodsFor: 'instance creation'!
on: aForm
	^self new setForm: aForm! !

!Canvas class methodsFor: 'patterns'!
pattern: evenByte with: oddByte
	"A halftone (St_bitblt.mli): 16 by 16, its even rows of one byte,
	its odd rows of another."
	| f |
	f := Form extent: 16 @ 16.
	1 to: 32 do: [:i | f bits at: i put: (i - 1 // 2 \\ 2 = 0 ifTrue: [evenByte] ifFalse: [oddByte])].
	^f!

gray
	Gray == nil ifTrue: [Gray := self pattern: 16rAA with: 16r55].
	^Gray!

lightGray
	LightGray == nil ifTrue: [LightGray := self pattern: 16r88 with: 16r22].
	^LightGray! !

!Canvas methodsFor: 'private'!
setForm: aForm
	form := aForm.
	clip := aForm boundingBox.
	blt := BitBlt destForm: aForm sourceForm: nil halftoneForm: nil combinationRule: 3
		destOrigin: 0 @ 0 sourceOrigin: 0 @ 0 extent: 0 @ 0 clipRect: clip! !

!Canvas methodsFor: 'clipping'!
clip
	^clip!

clip: aRectangle
	"Nothing is drawn outside it, whatever is asked: how the world
	redraws one rectangle of the screen and no more."
	clip := aRectangle.
	blt clipRect: aRectangle! !

!Canvas methodsFor: 'drawing'!
fill: aRectangle color: aSymbol
	"One BitBlt with no source: the rule alone for black (15: ones) and
	white (0: zeros), a halftone stored (3) for the grays."
	aSymbol == #black
		ifTrue: [blt halftoneForm: nil; combinationRule: 15]
		ifFalse: [aSymbol == #white
			ifTrue: [blt halftoneForm: nil; combinationRule: 0]
			ifFalse: [blt halftoneForm: (aSymbol == #gray ifTrue: [Canvas gray] ifFalse: [Canvas lightGray]); combinationRule: 3]].
	blt destX: aRectangle left destY: aRectangle top; width: aRectangle width height: aRectangle height.
	blt copyBits! !

!Morph class methodsFor: 'instance creation'!
new
	^super new initialize! !

!Morph methodsFor: 'initialize'!
initialize
	bounds := 0 @ 0 corner: 50 @ 40.
	submorphs := OrderedCollection new.
	color := #gray! !

!Morph methodsFor: 'accessing'!
bounds
	^bounds!

position
	^bounds origin!

extent
	^bounds extent!

color
	^color!

owner
	^owner!

submorphs
	^submorphs!

world
	"The morph at the top, if it is a world: a morph in nobody's hands
	and on no screen has none."
	^owner == nil ifTrue: [nil] ifFalse: [owner world]! !

!Morph methodsFor: 'changing'!
color: aSymbol
	color := aSymbol.
	self changed!

extent: aPoint
	self changed.
	bounds := bounds origin extent: aPoint.
	self changed!

position: aPoint
	"Move, with all it holds. The place left and the place taken both
	changed."
	| delta |
	delta := aPoint - bounds origin.
	(delta x = 0 and: [delta y = 0]) ifTrue: [^self].
	self changed.
	self privateMoveBy: delta.
	self changed!

privateMoveBy: delta
	bounds := bounds translateBy: delta.
	submorphs do: [:m | m privateMoveBy: delta]!

changed
	"All a morph does about the screen: say where it is no longer
	right. Its submorphs may stick out of it."
	self invalidRect: bounds.
	submorphs do: [:m | m changed]!

invalidRect: aRectangle
	"Up to the world, which keeps it."
	owner == nil ifFalse: [owner invalidRect: aRectangle]! !

!Morph methodsFor: 'submorphs'!
addMorph: aMorph
	"In front of the others: the first submorph is the frontmost."
	aMorph delete.
	submorphs addFirst: aMorph.
	aMorph privateOwner: self.
	aMorph changed!

delete
	"Out of its owner, off the screen."
	owner == nil ifTrue: [^self].
	self changed.
	owner submorphs remove: self.
	owner := nil!

privateOwner: aMorph
	owner := aMorph!

morphAt: aPoint
	"The frontmost morph there: a submorph before its owner, the first
	submorph before the others."
	submorphs do: [:m | | found |
		found := m morphAt: aPoint.
		found == nil ifFalse: [^found]].
	^(bounds containsPoint: aPoint) ifTrue: [self] ifFalse: [nil]! !

!Morph methodsFor: 'drawing'!
drawOn: aCanvas
	"What a morph looks like: the one method a new kind of morph
	writes."
	aCanvas fill: bounds color: #black.
	aCanvas fill: (bounds insetBy: 1) color: color!

fullDrawOn: aCanvas
	"Itself, then what it holds, the frontmost last. A morph outside
	the canvas's clipping rectangle is not drawn at all."
	(bounds intersects: aCanvas clip) ifTrue: [self drawOn: aCanvas].
	submorphs reverseDo: [:m | m fullDrawOn: aCanvas]! !

!Morph methodsFor: 'stepping'!
step
	"Sent every cycle. A morph that does something by itself does it
	here."!

fullStep
	self step.
	submorphs do: [:m | m fullStep]! !

!AtomMorph methodsFor: 'initialize'!
initialize
	super initialize.
	bounds := 0 @ 0 corner: 12 @ 12.
	velocity := 3 @ 2! !

!AtomMorph methodsFor: 'accessing'!
velocity
	^velocity!

velocity: aPoint
	velocity := aPoint! !

!AtomMorph methodsFor: 'stepping'!
step
	"Move by the velocity, and turn back at the owner's walls. In the
	hand, be still."
	| box p vx vy |
	(owner isKindOf: HandMorph) ifTrue: [^self].
	box := owner bounds.
	vx := velocity x.
	vy := velocity y.
	p := bounds origin + velocity.
	(p x < box left or: [p x + bounds width > box right]) ifTrue: [vx := vx negated].
	(p y < box top or: [p y + bounds height > box bottom]) ifTrue: [vy := vy negated].
	velocity := vx @ vy.
	self position: bounds origin + velocity! !

!HandMorph methodsFor: 'initialize'!
initialize
	super initialize.
	bounds := 0 @ 0 corner: 8 @ 8.
	color := #black.
	wasDown := false! !

!HandMorph methodsFor: 'events'!
processEvents
	"The mouse read once a cycle: the hand goes where it is, with what
	it carries; the button going down picks up the morph under it, the
	button going up puts it down, in front."
	| p down |
	p := Sensor mousePoint.
	down := Sensor redButtonPressed.
	self position: p.
	(down and: [wasDown not]) ifTrue: [self grab: (owner morphAt: p)].
	(down not and: [wasDown]) ifTrue: [self dropAll].
	wasDown := down!

grab: aMorph
	(aMorph == nil or: [aMorph == owner]) ifTrue: [^self].
	self addMorph: aMorph!

dropAll
	submorphs copy do: [:m | owner addMorph: m]! !

!WorldMorph class methodsFor: 'instance creation'!
on: aForm
	^self new setForm: aForm! !

!WorldMorph methodsFor: 'initialize'!
setForm: aForm
	bounds := aForm boundingBox.
	color := #white.
	canvas := Canvas on: aForm.
	damage := OrderedCollection new.
	hand := HandMorph new.
	hand privateOwner: self.
	self invalidRect: bounds! !

!WorldMorph methodsFor: 'accessing'!
world
	^self!

hand
	^hand!

damage
	^damage! !

!WorldMorph methodsFor: 'submorphs'!
morphAt: aPoint
	"A morph of the world, never the world itself."
	| found |
	found := super morphAt: aPoint.
	^found == self ifTrue: [nil] ifFalse: [found]! !

!WorldMorph methodsFor: 'damage'!
invalidRect: aRectangle
	"Remember a rectangle to redraw. Nothing clever here: this is sent
	twice for every morph that moves."
	damage add: aRectangle!

damageToRedraw
	"The rectangles to redraw this cycle, and none remembered after.
	A few (one morph dragged): each of them, and what did not change
	is not touched. Many (everything moving): the one rectangle that
	holds them all, drawn once -- going through the morphs for each of
	a hundred small rectangles costs more than drawing them all."
	| rects all |
	rects := damage.
	damage := OrderedCollection new.
	rects size <= 8 ifTrue: [^rects].
	all := rects first.
	rects do: [:r | all := all merge: r].
	^OrderedCollection new add: all; yourself! !

!WorldMorph methodsFor: 'stepping'!
doOneCycle
	"The whole of Morphic's main loop."
	hand processEvents.
	submorphs copy do: [:m | m fullStep].
	self displayWorld!

displayWorld
	"Each damaged rectangle: the background, then every morph, back to
	front, the hand last -- the canvas clipped to the rectangle, so
	that what did not change is not touched."
	self damageToRedraw do: [:r | | clip |
		clip := r intersect: bounds.
		canvas clip: clip.
		canvas fill: clip color: color.
		submorphs reverseDo: [:m | m fullDrawOn: canvas].
		hand fullDrawOn: canvas]! !

!WorldMorph class methodsFor: 'examples'!
bouncingAtoms: n
	"The classic first demo: n atoms in a box. Each cycle moves them
	all, and redraws only where they were and where they are.
	(WorldMorph bouncingAtoms: 50) doOneCycle"
	| world |
	world := self on: Display.
	1 to: n do: [:i | | atom |
		atom := AtomMorph new.
		atom color: (#(#black #gray #lightGray) at: i \\ 3 + 1).
		atom velocity: (i \\ 7 - 3 * 2 + 1) @ (i \\ 5 - 2 * 2 + 1).
		world addMorph: atom.
		atom position: (i * 37 \\ (world bounds width - 20)) @ (i * 53 \\ (world bounds height - 20))].
	^world! !
|st});
]