1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
|
;; file warmelt-normal.melt -*- Lisp -*-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(comment "***
Copyright 2008, 2009, 2010 Free Software Foundation, Inc.
Contributed by Basile Starynkevitch <basile@starynkevitch.net>
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>.
***")
;; the copyright notice above apply both to warmelt-normal.melt and
;; to the generated file warmelt-normal*.c
;; This file is the third part of a bootstrapping compiler for the
;; MELT lisp dialect, compiler which should be able to
;; compile itself (into generated C file[s])
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; ================ normalized representations
;; basically, the normalized representation of (f a (g x))
;; is let y=(g x) in (f a y)
;; etc... where y is a cloned symbol
;;; common superclass for normalized representations
(defclass class_nrep
:super class_root
:doc #{The $CLASS_NREP is the common super class of normalized
representations. Its $NREP_LOC field gives the location in source,
if any.}#
:fields (nrep_loc ;location in source
))
;; a simple stuff is a non-object, a symbol (or keyword or clonedsym),
;; ...
(defclass class_nrep_simple
:doc #{The $CLASS_NREP_SIMPLE is for simple normal things -e.g. normal
symbols.}#
:super class_nrep
:fields ( ))
;; a normalized expression should never be the result of normal_exp;
;; it should only appear in bindings!
(defclass class_nrep_expression
:doc #{Common superclass $CLASS_NREP_EXPRESSION of normalized expressions
which can only appear in normal bindings.}#
:super class_nrep
:fields ( ))
;; normalized typed expressions also have a ctype
(defclass class_nrep_typed_expression
:doc #{Common super-class $CLASS_NREP_TYPED_EXPRESSION of typed normalized
expressions. $NEXPR_CTYP gives its c-type.}#
:super class_nrep_expression
:fields (nexpr_ctyp ;the ctype
))
;; normalized typed expressions with arguments
(defclass class_nrep_typed_expression_with_arguments
:doc #{Common super-class
$CLASS_NREP_TYPED_EXPRESSION_WITH_ARGUMENTS of typed normalized
expressions with normalized arguments. $NEXPR_ARGS is the tuple of
normalized arguments.}#
:super class_nrep_typed_expression
:fields (nexpr_args))
;; normal applications have simple functions & arguments
(defclass class_nrep_apply
:super class_nrep_typed_expression_with_arguments
:doc #{The $CLASS_NREP_APPLY class is for normal
applications. $NAPP_FUN is the simple function to apply to $NEXPR_ARGS.}#
:fields (napp_fun ;simple function to apply
))
;; normalized multiresult apply
(defclass class_nrep_multiapply
:doc #{The $CLASS_NREP_MULTIAPPLY is for normal applications of
functions with several results within $MULTICALL. $NMULAPP_BINDINGS is
the tuple of formal result bindings. $NMULAPP_BODY is the normal
body.}#
:super class_nrep_apply
:fields (nmulapp_bindings ;a tuple of formal result bindings
nmulapp_body ;body normexp
))
;;; normal message sending
(defclass class_nrep_msend
:doc #{The $CLASS_NREP_SEND is for normal message send, with a
single result. $NSEND_SEL is the normalized selector, $NSEND_RECV
the normalized reciever, with $NEXPR_ARGS giving the normalized arguments.}#
:super class_nrep_typed_expression_with_arguments
:fields (nsend_sel ;the normalized selector occurrence
nsend_recv ;the reciever
))
;; normalized multiresult message send
(defclass class_nrep_multimsend
:doc #{The $CLASS_NREP_MULTIMSEND is for normal message sends with
multiple results thru $MULTICALL. $NMULSEND_BINDINGS is the tuple of
formal result bindings, and $NMULSEND_BODY is the normalized body.}#
:super class_nrep_msend
:fields (nmulsend_bindings ;tuple of formal bindings
nmulsend_body ;body normexp
))
;; normal chunk is a normalized expansion of primitive
(defclass class_nrep_chunk
:doc #{The $CLASS_NREP_CHUNK is for normalized expansion of
primitive or cmatcher expressions. Field $NCHUNK_EXPANSION is the
expansion - where strings of $DISCR_VERBATIM_STRING are handled
specifically. Field $NCHUNK_OPER is the operator.}#
:super class_nrep_typed_expression
:fields (nchunk_expansion ;the expansion
nchunk_oper ;the operator (primitive or cmatcher)
))
;; normal comment is a normalized comment
(defclass class_nrep_comment
:doc #{The $CLASS_NREP_COMMENT if for normalized comments in the
generated C code. The field $NCOMM_STRING gives the comment.}#
:super class_nrep_expression
:fields (ncomm_string ;the comment
))
;; normal lets have simple binding & body subexpressions
(defclass class_nrep_let
:doc #{The $CLASS_NREP_LET is for normalized lets. The
$NLET_BINDINGS field is a tuple of $CLASS_NORMAL_LET_BINDING
instances. The $NLET_BODY field is the normal body. The normalization
process introduce many such normal lets.}#
:super class_nrep_expression
:fields (nlet_bindings ;a tuple of class_normal_let_binding-s
nlet_body
))
;; normal letrec
(defclass class_nrep_letrec
:doc #{The $CLASS_NREP_LETREC is for normalized letrec. The field
$NLETREC_FILL_BINDINGS is the list of internal normal bindings to fill
the letrec-ed constructions. The field $NLETREC_BODY_BINDINGS is the
tuple of internal normal bindings for the body. The field
$NLETREC_LOCSYMS is the tuple of local symbol occurrences.}#
:super class_nrep_let
:fields (
;; the nlet_bindings is a tuple of constructive bindings
nletrec_fill_bindings
nletrec_body_bindings
nletrec_locsyms
))
;; normal return have a main & supplementary subexpressions
(defclass class_nrep_return
:doc #{The $CLASS_NREP_RETURN is for normalized returns. The primary
returned value is given thru $NRET_MAIN field. The secondary
returned things are thru $NRET_REST tuple.}#
:super class_nrep_expression
:fields (nret_main ;main normal expression to return
nret_rest ;tuple of normal expr...
))
(compile_warning "should add documentation below")
;; common normal for if & ifisa
(defclass class_nrep_ifcommon
:super class_nrep_typed_expression
:fields (nif_then
nif_else
))
;; normal if is_a(value,class) then else
(defclass class_nrep_ifisa
:super class_nrep_ifcommon
:fields (nifa_val ;normal value to be tested
nifa_class ;normal class
))
;; normal if have simple test, then, else clauses & a ctype
(defclass class_nrep_if
:super class_nrep_ifcommon
:fields (nif_test
))
;; normal ifcpp have a symbol and a ctyp
(defclass class_nrep_cppif
:super class_nrep_expression
:fields (nifp_cond
nifp_then
nifp_else
nifp_ctyp
))
;; normal progn has a distingished last
(defclass class_nrep_progn
:super class_nrep_expression
:fields (nprogn_seq ;tuple of all but last
nprogn_last
))
;; normalized unsafe get field
(defclass class_nrep_unsafe_get_field
:super class_nrep_expression
:fields (nuget_obj
nuget_field))
;; normalized unsafe_put_field
(defclass class_nrep_unsafe_put_fields
:super class_nrep_expression
:fields (nuput_obj
nuput_fields))
;; normalized setq
(defclass class_nrep_setq
:super class_nrep_expression
:fields (nstq_var
nstq_exp
))
;; normalized forever
(defclass class_nrep_forever
:super class_nrep_expression
:fields (nforever_bind ;the label binding
nforever_body ;a tuple
nforever_result ;cloned symbol for result
))
;; normalized exit
(defclass class_nrep_exit
:super class_nrep_expression
:fields (nexit_bind ;the label binding
nexit_val ;the exited value
))
;; normalized field assign (in make instance)
(defclass class_nrep_fieldassign
:super class_nrep
:fields (nfla_field ;the field
nfla_val ;its normalized value
))
;; normalized make instance
(defclass class_nrep_instance
:super class_nrep_expression
:fields (nmins_class ;the instanciated class
nmins_cladata ;its data
nmins_fields ;the tuple of field assignments
))
;; normalized lambda
(defclass class_nrep_lambda
:super class_nrep_expression
:fields (nlambda_proc ;the procedure
nlambda_constrout ;the constant routine
nlambda_closedv ;the tuple of closed normal values
))
;; normalized citeration
(defclass class_nrep_citeration
:super class_nrep_expression
:fields (nciter_citerator ;the citerator
nciter_chunkbefore ;the expansed chunk before
nciter_chunkafter ;the expansed chunk after
nciter_body ;the normalized body
nciter_statocc ;the state local occurrence
nciter_locbindings ;the local bindings
nciter_bodbindings ;normalized body bindings
))
;; normalized tests sequence, used for matches
(defclass class_nrep_tests
:super class_nrep_expression
:fields (ntests_testseq ;the tuples of normal tests
;;;; see file warmelt-normatch.melt
))
;;;;;;;;;;;;;;;;
(defclass class_normal_constructor_binding
:doc #{The internal $CLASS_NORMAL_CONSTRUCTOR_BINDING is the common
super-class of constructor bindings in LETREC... Field $NCONSB_LOC
gives the optional location, field $NCONSB_DISCR gives the normalized
discriminant, and field $NCONSB_NLETREC gives the normal letrec containing it..}#
:super class_any_binding
:fields (nconsb_loc nconsb_discr nconsb_nletrec)
)
(defclass class_normal_constructed_tuple_binding
:doc #{The internal $CLASS_NORMAL_CONSTRUCTED_TUPLE_BINDING is the
class of tuple constructor bindings. Field $NTUPB_COMP gives the tuple
of initial normalized components.}#
:super class_normal_constructor_binding
:fields (ntupb_comp))
(defclass class_normal_constructed_pair_binding
:doc #{The internal $CLASS_NORMAL_CONSTRUCTED_PAIR_BINDING is the
class of pair constructor bindings. Field $NPAIRB_HEAD gives the
normalized head, and $NPAIRB_TAIL gives the normalized tail.}#
:super class_normal_constructor_binding
:fields (npairb_head npairb_tail))
(defclass class_normal_constructed_list_binding
:doc #{The internal $CLASS_NORMAL_CONSTRUCTED_LIST_BINDING is the
class of list constructor bindings. Field $NLISTB_FIRST gives the
initial normalized first pair, and field $NLISTB_LAST gives the last
one. Field $NLISTB_PAIRSB gives the tuple of constructed pair bindings}#
:super class_normal_constructor_binding
:fields (nlistb_first nlistb_last nlistb_pairsb))
(defclass class_normal_constructed_lambda_binding
:doc #{The internal $CLASS_NORMAL_CONSTRUCTED_LAMBDA_BINDING is the
class of lambda constructor bindings. Field $NLAMBDAB_NCLOSED gives
the normalized closed values, and $NLAMBDAB_DATAROUT gives the normalized routine data, and $NLAMBDAB_CONSTROUT its constant.}#
:super class_normal_constructor_binding
:fields (nlambdab_nclosed nlambdab_constrout nlambdab_datarout))
(defclass class_normal_constructed_instance_binding
:doc #{The internal $CLASS_NORMAL_CONSTRUCTED_INSTANCE_BINDING is
the class of instance constructor bindings. Field $NINSTB_SLOTS is the
tuple of the normalized slots, and $NINSTB_CLABIND is the class binding.}#
:super class_normal_constructor_binding
:fields (ninstb_slots ninstb_clabind))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; procedures
(defclass class_nrep_anyproc
:doc #{The $CLASS_NREP_ANYPROC is the common super-class for
procedures. Field $NPROC_BODY is the normalized body.}#
:super class_nrep
:fields (
nproc_body
))
;; the class of the initial procedure
(defclass class_nrep_initproc
:super class_nrep_anyproc
:doc #{The $CLASS_NREP_ANYPROC is the class for the initial
procedure in a module. Field $NINIT_TOPL is the list of top-level
normalized expressions.}#
:fields (ninit_topl ;list of toplevel nrep
))
;; normal routine procedure
(defclass class_nrep_routproc
:super class_nrep_anyproc
:doc #{The $CLASS_NREP_ROUTPROC is the class for normal routine
procedures, representing $DEFUN or $LAMBDA code. Field $NRPRO_NAME
is the name, field $NRPRO_ARGB is the formal arguments
binding. Field $NRPRO_CLOSEDB is the list of closed bindings. Field
$NRPRO_CONST is the list of constants. Field $NRPRO_DATAROUT is the
routine data object. Field $NRPRO_DATACLOS is the closure data
object. Field $NRPRO_THUNKLIST is the list of thunks to be called
when compiling it.}#
:fields (
nrpro_name ;name (if any)
nrpro_argb ;argument bindings
nrpro_closedb ;list of closed bindings
nrpro_const ;list of constants
nrpro_datarout ;routine data object
nrpro_dataclos ;closure data object
nrpro_thunklist ;list of thunks to be called when compiling it
))
(defclass class_nrep_lambdaroutproc
:super class_nrep_routproc
:doc #{The $CLASS_NREP_LAMBDAROUTPROC is the class for $LAMBDA
related routine procedures.}#
:fields ())
(defclass class_nrep_defunroutproc
:super class_nrep_routproc
:doc #{The $CLASS_NREP_DEFUNROUTPROC is the class for $DEFUN related
routine procedures.}#
:fields ())
;;; static normalized predef
(defclass class_nrep_predef
:super class_nrep_simple
:fields (
nrpredef ;the predef is a symbol or a boxed integer
))
;; normalized nil
(defclass class_nrep_nil
:super class_nrep_simple
:fields (
))
;;; quasidata are stuff to be computed inside the initial routine;
;;; most of them are plain data, but current_module_environment_container &
;;; parent_module_environment need specific stuff
(defclass class_nrep_quasidata
:super class_nrep
:fields (
))
;;;; static data is build at module initialization time
(defclass class_nrep_data
;; the objnum is the predefined rank if any
:super class_nrep_quasidata
:fields (ndata_name ;name if any of the data
ndata_discrx ;discriminant normal expression
ndata_rank ;boxed integer rank of the data
;;; we box the integer and don't use the objnum bzcause we
;;; might have a lot (>30000) of data
ndata_locbind ;local binding tuple to fill the data
))
;; normal "static" instance - built at modules initialization
(defclass class_nrep_datainstance
:super class_nrep_data
:fields (ninst_objnum ;object number (a number or a symbol)
ninst_predef ;predefined rank (number or symbol)
ninst_hash ;integer hash
ninst_slots ;tuple of normalized slots expressions
))
;; normal "static" string
(defclass class_nrep_datastring
:super class_nrep_data
:fields ( nstr_string ;the string
))
;; normal "static" boxed integer
(defclass class_nrep_databoxedinteger
:super class_nrep_data
:fields ( nboxint_num ;the numerical integer
))
;; normal "static" tuple
(defclass class_nrep_datatuple
:super class_nrep_data
:fields ( ntup_comp ;the tuple of component values expressions
))
;; normal interned static symbol
(defclass class_nrep_datasymbol
:super class_nrep_datainstance
:fields ( ndsy_namestr
))
;; normal interned static keyword
(defclass class_nrep_datakeyword
:super class_nrep_datasymbol
:fields (
))
;; normal static routine data
(defclass class_nrep_dataroutine
:super class_nrep_data
:fields (ndrou_proc ;associated procedure
))
;; normal static closure data
(defclass class_nrep_dataclosure
:super class_nrep_data
:fields (ndclo_proc ;associated procedure
ndclo_closv ;tuple of closed values
))
;; normal static start value
;; obtained from an initial binding
(defclass class_nrep_importedval
:super class_nrep_simple
:fields (nimport_symb ;the symbol
nimport_sydata ;the symbol data
))
;; normal occurrence of a symbol
(defclass class_nrep_symocc
:super class_nrep_simple
:fields (nocc_symb
nocc_ctyp ;the ctype of the symbol, eg ctype_value
nocc_bind ;the binding of the symbol
))
;; normal local occurrence of a symbol
(defclass class_nrep_locsymocc
:super class_nrep_symocc
:fields (
))
;; normal closed occurrence of a symbol
(defclass class_nrep_closedocc
:super class_nrep_symocc
:fields (ncloc_procs ;list of enclosing procedures
))
;; normal constant occurrence of a symbol
(defclass class_nrep_constocc
:super class_nrep_closedocc
)
;;; normal quasi constants for current_module_environment_container &
;;; parent_module_environment & constants
(defclass class_nrep_quasiconstant
:super class_nrep_simple
:fields (nconst_sval ;source value
nconst_proc ;containing proc
nconst_data ;normalized data or stuff inside iniproc
))
;; normal constant (.e.g a quoted symbol, a keyword, ...)
(defclass class_nrep_constant
:super class_nrep_quasiconstant
:fields (
))
;; noormal current_module_environment_container quasiconst
(defclass class_nrep_quasiconst_current_module_environment_container
:super class_nrep_quasiconstant
:fields ( nqcmec_comment
))
;; normal current_module_environment_container quasidata
(defclass class_nrep_quasidata_current_module_environment_container
:super class_nrep_quasidata
:fields (
))
;; noormal parent_module_environment quasiconst
(defclass class_nrep_quasiconst_parent_module_environment
:super class_nrep_quasiconstant
:fields (
))
;; normal parent_module_environment quasidata
(defclass class_nrep_quasidata_parent_module_environment
:super class_nrep_quasidata
:fields (
))
;; data field accessor (mostly used for defclass initialization) this
;; translates into melt_field_object(<obj>,<off>) of obj is not a
;; datainstance and directly to the field if it is a datainstance
(defclass class_nrep_fieldacc
:super class_nrep_expression
:fields (naccf_obj ;data for the object to be accessed
naccf_fld ;rank or field to be accessoed
))
;;; data multiple accessor (mostly used for defclass initialization)
;; this translates into melt_multiple_nth(<mul>,<ix>) if mul is not
;; a datatuple and directly to the component if it is a datatuple
(defclass class_nrep_multacc
:super class_nrep_expression
:fields (naccm_mul ;data for the multiple to be accessed
naccm_ix ;index to be accessed (a boxed integer)
))
;; normalized store predefined
(defclass class_nrep_store_predefined
:super class_nrep_expression
:fields (nstpd_predef
nstpd_value
))
;; normalized update current module environment box
(defclass class_nrep_update_current_module_environment_container
:super class_nrep_expression
:fields (
nucmeb_expr ;the normalized expression
;computing the box
ncumeb_comment ;optional comment
))
;;; export all the normalized representations classes
(export_class ;; normal representations classes in alphabetical order
class_normal_constructed_instance_binding
class_normal_constructed_lambda_binding
class_normal_constructed_list_binding
class_normal_constructed_pair_binding
class_normal_constructed_tuple_binding
class_normal_constructor_binding
class_nrep
class_nrep_anyproc
class_nrep_apply
class_nrep_chunk
class_nrep_citeration
class_nrep_closedocc
class_nrep_comment
class_nrep_constant
class_nrep_constocc
class_nrep_cppif
class_nrep_data
class_nrep_databoxedinteger
class_nrep_dataclosure
class_nrep_datainstance
class_nrep_datakeyword
class_nrep_dataroutine
class_nrep_datastring
class_nrep_datasymbol
class_nrep_datatuple
class_nrep_defunroutproc
class_nrep_exit
class_nrep_expression
class_nrep_fieldacc
class_nrep_fieldassign
class_nrep_forever
class_nrep_if
class_nrep_ifcommon
class_nrep_ifisa
class_nrep_importedval
class_nrep_initproc
class_nrep_instance
class_nrep_lambda
class_nrep_lambdaroutproc
class_nrep_let
class_nrep_letrec
class_nrep_locsymocc
class_nrep_msend
class_nrep_multacc
class_nrep_multiapply
class_nrep_multimsend
class_nrep_nil
class_nrep_predef
class_nrep_progn
class_nrep_quasiconst_current_module_environment_container
class_nrep_quasiconst_parent_module_environment
class_nrep_quasiconstant
class_nrep_quasidata
class_nrep_quasidata_current_module_environment_container
class_nrep_quasidata_parent_module_environment
class_nrep_return
class_nrep_routproc
class_nrep_setq
class_nrep_simple
class_nrep_store_predefined
class_nrep_symocc
class_nrep_typed_expression
class_nrep_typed_expression_with_arguments
class_nrep_unsafe_get_field
class_nrep_unsafe_put_fields
class_nrep_update_current_module_environment_container
) ;end of export normal classes
;;;;;;; primitive for extra warnings
(defprimitive has_extra_warnings () :long "(extra_warnings)")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; add some data to a normalization context and return it
(defun add_nctx_data (nctx ndata)
(assert_msg "check nctx" (is_a nctx class_normalization_context))
(assert_msg "check ndata" (is_a ndata class_nrep_data))
(assert_msg "fresh ndata" (null (unsafe_get_field :ndata_rank ndata)))
(let ( (datlis (unsafe_get_field :nctx_datalist nctx)) )
(assert_msg "check datlis" (is_list datlis))
(let ( (lastdat (pair_head (list_last datlis))) )
(if (is_a lastdat class_nrep_data)
(let ( (:long lastrk (get_int (unsafe_get_field :ndata_rank lastdat))) )
(assert_msg "check lastrk" (>i lastrk 0))
(let ( (rkbox (make_integerbox discr_integer (+i 1 lastrk))) )
(unsafe_put_fields ndata :ndata_rank rkbox)
))
(let ( (rkbox1 (make_integerbox discr_integer 1)) )
(unsafe_put_fields ndata :ndata_rank rkbox1)
)))
(list_append datlis ndata)
ndata
))
;; the automatically generated warmelt-predef.melt file defines a fill_initial_predefmap function
(load "warmelt-predef.melt")
;; internal primitive to return the last predefined index
(defprimitive last_globpredef_index () :long
"BGLOB__LASTGLOB")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; create a normalization context
(defun create_normcontext (modctx)
(let ( (:long maxpredefix (last_globpredef_index))
(:long ix 1)
(predefmap (make_mapobject discr_map_objects (+i 19 (*i 2 maxpredefix))))
(valmap (make_mapobject discr_map_objects 350))
)
(assert_msg "check modctx" (is_a modctx class_module_context))
(forever predefloop
(if (>=i ix maxpredefix) (exit predefloop))
(let ( (curpredef (get_globpredef ix)) )
(if (is_object curpredef)
(mapobject_put predefmap curpredef (make_integerbox discr_integer ix)))
)
(setq ix (+i ix 1)))
(fill_initial_predefmap predefmap)
(let ( (inipro (instance class_nrep_initproc
:ninit_topl (make_list discr_list)
:nrep_loc ()
))
(ncx (instance
class_normalization_context
:nctx_initproc inipro
:nctx_proclist (make_list discr_list)
:nctx_datalist (make_list discr_list)
:nctx_valuelist (make_list discr_list)
:nctx_symbmap (make_mapstring discr_map_strings 50)
:nctx_keywmap (make_mapstring discr_map_strings 40)
:nctx_predefmap predefmap
:nctx_valmap valmap
:nctx_valbindmap (make_mapobject discr_map_objects 20)
;; we need a symbcachemap for the toplevel expressions
:nctx_symbcachemap (make_mapobject discr_map_objects 30)
:nctx_curproc inipro
:nctx_modulcontext modctx
:nctx_qdatcurmodenvbox (instance class_nrep_quasidata_current_module_environment_container
)
:nctx_qdatparmodenv (instance class_nrep_quasidata_parent_module_environment
)
:nctx_procurmodenvlist (make_list discr_list)
))
)
(debug_msg ncx "create_normcontext return ncx")
(return ncx)
)))
;;; the normal_exp selector
;;;;; expected arguments:
;;; recv = the reciever, eg a sexpr
;;; env = the environment
;;; ncx = the normalization context
;;; psloc = (parent) source location
;;;;; expected results: normalized + binding list
;;; IMPORTANT NOTICE: even for simple expressions [like side-effecting
;;; expressions returning void, e.g. RETURN, EXIT, ... other simple
;;; side-effecting expressions like SETQ], the normalized should
;;; always be a simple occurrence, and the work being done in the
;;; bindings list.
(defselector normal_exp class_selector
; :named_name (stringconst2val discr_namestring "NORMAL_EXP")
)
;; many stuff are already normalized
(defun normexp_identical (recv env ncx psloc)
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
;; is a no-op
(debug_msg recv "normexp_identical recv")
recv)
(install_method discr_any_receiver normal_exp normexp_identical)
;; I'm not sure to understand why we need this.
(install_method discr_string normal_exp normexp_identical)
(defun normexp_null (recv env ncx psloc)
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(let ( (normnull (instance class_nrep_nil :nrep_loc psloc)) )
(debug_msg normnull "normexp_null normnull")
normnull
))
(install_method discr_null_receiver normal_exp normexp_null)
;;; catchall for src
(defun normexp_src_catchall (recv env ncx psloc)
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_src_catchall recv" )
(let ( (myclass (discrim recv))
(myclassname (unsafe_get_field :named_name myclass)) )
(error_strv (unsafe_get_field :loca_location recv)
"unimplemented normalization for " myclassname)
(assert_msg "normexp_src_catchall unimplemented normexp for src" ())
))
(install_method class_source normal_exp normexp_src_catchall)
;; the selector to compute the ctype of a value in an environment
;;; argument: environment
;;;; sometimes this selector is used with a null environment, for
;;;; instance in the code generation phase
(defselector get_ctype class_selector
; :named_name (stringconst2val discr_namestring "GET_CTYPE")
)
;;; selector to compile a normalized stuff into an object
;;; reciever: the normalized stuff
;;; arguments:
;;;; * GCX the code generation context
;;; result = the obj instruction or value
(defselector compile_obj class_selector
)
;;; most stuff are really ctype_value
(defun gectyp_anyrecv (recv env) ctype_value)
(install_method discr_any_receiver get_ctype gectyp_anyrecv)
(defun gectyp_root (recv env) ctype_value)
(install_method class_root get_ctype gectyp_root)
;; integers are ctype_long
(defun gectyp_integer (recv env)
(debug_msg recv "gectyp_integer recv")
ctype_long)
(install_method discr_integer get_ctype gectyp_integer)
;; strings are ctype_cstring
(defun gectyp_string (recv env)
ctype_cstring)
(install_method discr_string get_ctype gectyp_string)
;;; normalize a tuple - returning a tuple & a bindinglist
(defun normalize_tuple (tup env ncx psloc)
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(if (null tup) (return () ()))
(assert_msg "check tup" (is_multiple tup))
(debug_msg tup "normalize_tuple tup" )
(let ( (bindlist (make_list discr_list))
(res (multiple_map
tup
(lambda (comp :long ix)
(debug_msg comp "normalize_tuple comp")
(multicall
(norcomp nbinds)
(normal_exp comp env ncx psloc)
(debug_msg norcomp "normalize_tuple norcomp")
(debug_msg nbinds "normalize_tuple nbinds")
(assert_msg "check nbinds" (is_list_or_null nbinds))
(assert_msg "check norcomp not nrep_expr" (is_not_a norcomp class_nrep_expression))
(if (is_list nbinds)
(list_every
nbinds
(lambda (bnd)
(assert_msg "check bnd" (is_a bnd class_any_binding))
(assert_msg "check bindlist" (is_list bindlist))
(list_append bindlist bnd)
))
)
norcomp
))))
)
(if (not (is_pair (list_first bindlist)))
(setq bindlist ()))
(debug_msg res "normalize_tuple res")
(debug_msg bindlist "normalize_tuple bindlist" )
(return res bindlist)
))
;; wrap a normal let around a tuple of normalized expressions and a bindinglist
(defun wrap_normal_letseq (tupnexp bindlist loc)
(assert_msg "check tupnexp" (is_multiple_or_null tupnexp))
(assert_msg "check bindlist" (is_list_or_null bindlist))
(list_every
bindlist
(lambda (cbind)
(if (not (is_a cbind class_normal_let_binding))
(progn
(debug_msg tupnexp "wrap_normal_letseq tuplexp")
(debug_msg bindlist "wrap_normal_letseq bindlist")
(debug_msg cbind "wrap_normal_letseq cbind")))
(assert_msg "check cbind wrapnormletseq" (is_a cbind class_normal_let_binding))))
(let ( (wnlet
(instance class_nrep_let
:nrep_loc loc
:nlet_bindings (list_to_multiple bindlist)
:nlet_body tupnexp))
)
(return wnlet)))
;; wrap a normal let around a single normalized expression & a bindinglist
(defun wrap_normal_let1 (nexp bindlist loc)
(assert_msg "check bindlist" (is_list_or_null bindlist))
(list_every
bindlist
(lambda (cbind)
(if (not (is_a cbind class_normal_let_binding))
(progn
(debug_msg nexp "wrap_normal_let1 nexp")
(debug_msg bindlist "wrap_normal_let1 bindlist")
(debug_msg cbind "wrap_normal_let1 cbind")))
(assert_msg "check cbind wrapnormlet1" (is_a cbind class_normal_let_binding))))
(if
(and (is_list bindlist)
(is_pair (list_first bindlist)))
(let ( (wnlet
(instance class_nrep_let
:nrep_loc loc
:nlet_bindings (list_to_multiple bindlist)
:nlet_body (tuple nexp)))
)
wnlet)
nexp
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; utility to check that every normalized argument has a passable ctype
(defun check_ctype_nargs (nargs env sloc)
(multiple_every
nargs
(lambda (cnarg :long ix)
(let ( (ctyp (get_ctype cnarg env)) )
(assert_msg "check_ctype_nargs ctyp" (is_a ctyp class_ctype))
(if (not (is_string (unsafe_get_field :ctype_parstring ctyp)))
(error_strv sloc "argument has invalid type" (unsafe_get_field :named_name ctyp))
))
)))
;; for symbols which are imported from a previous environment (this
;; only happens when compiling stuff which is not this warmelt-*) we
;; should detect them and generate some special data to fetch them, in
;; the start routine, from the given environment (which is the only
;; argument to the start routine). Detecting such symbols is easy : their
;; binding is a class_value_binding
;;;; normalize a symbol occurrence
(defun normexp_symbol (recv env ncx psloc)
(debug_msg recv "normexp_symbol recv")
(multicall
(bind procs)
(find_enclosing_env env recv)
(debug_msg bind "normexp_symbol after find_enclosing_env bind")
(debug_msg procs "normexp_symbol after find_enclosing_env procs")
(assert_msg "normexp_symbol check recv" (is_a recv class_symbol))
(if (null bind)
(progn
(error_strv psloc "unbound symbol to normalize"
(unsafe_get_field :named_name recv))
(return ())))
(if (null psloc)
(shortbacktrace_dbg "normex_symbol null psloc" 10)
)
(let ( (sycmap (unsafe_get_field :nctx_symbcachemap ncx))
(syca (mapobject_get sycmap recv)) )
(assert_msg "check sycmap" (is_mapobject sycmap))
(cond
;; check if in the cache
(syca syca) ;already cached
;; value binding, get/put it into the map
( (is_a bind class_value_binding)
(let ( (bvar (mapobject_get (unsafe_get_field :nctx_valbindmap ncx) bind)) )
(debug_msg bind "normexp_symbol value bind")
(debug_msg procs "normexp_symbol value procs")
(if (null bvar)
(let ( (newbvar
(instance class_nrep_importedval
:nrep_loc psloc
:nimport_symb recv
:nimport_sydata (normal_symbol_data recv ncx psloc))) )
(mapobject_put (unsafe_get_field :nctx_valbindmap ncx) bind newbvar)
(setq bvar newbvar)
(list_append (unsafe_get_field :nctx_valuelist ncx) newbvar)
))
;; if procs ia non-empty list, symbol is a "closed" constant for the value
(if (and (is_list procs)
(is_pair (list_first procs)))
(let ( (fxocc
(instance class_nrep_constocc
:nrep_loc psloc
:nocc_symb recv
:nocc_bind bind
:nocc_ctyp ctype_value
:ncloc_procs procs))
)
;; cache the result
(mapobject_put sycmap recv fxocc)
(debug_msg fxocc "normexp_symbol const value fxocc")
;; put the const occurrence if needed in the const list of each proc
(list_every
procs
(lambda (pr)
(assert_msg "check pr" (is_a pr class_nrep_anyproc))
(let ( (clbox (make_box discr_box fxocc))
(cnstproc (unsafe_get_field :nrpro_const pr)) )
(list_every
cnstproc
(lambda (cx) (if (== cx fxocc) (progn (box_put clbox ()) ()))))
(let ( (newcl (box_content clbox)) )
(if newcl (list_append cnstproc newcl)))
)))
fxocc
)
;; otherwise symbol is the direct value
(progn
;; cache the result
(mapobject_put sycmap recv bvar)
(debug_msg bvar "normexp_symbol local value bvar")
bvar
)
)))
;; the procs is a non-empty list, so the symbol is closed
( (and (is_list procs)
(is_pair (list_first procs)))
(debug_msg procs "normexp_symbol closed procs")
(debug_msg bind "normexp_symbol closed bind")
;; check that a closed symbol is always a value
(let ( (bty
(cond ( (is_a bind class_formal_binding)
(unsafe_get_field :fbind_type bind) )
( (is_a bind class_let_binding)
(unsafe_get_field :letbind_type bind))
(:else ())))
)
(if bty
(if (!= bty ctype_value)
(error_strv psloc
"closed variable has non value type (boxing required)"
(unsafe_get_field :named_name recv)
)))
(setq bty ctype_value)
(if (is_a bind class_fixed_binding)
(let ( (fxocc
(instance class_nrep_constocc
:nrep_loc psloc
:nocc_symb recv
:nocc_bind bind
:nocc_ctyp ctype_value
:ncloc_procs procs)) )
;; cache the result
(mapobject_put sycmap recv fxocc)
(debug_msg fxocc "normexp_symbol fxocc")
;; put the const occurrence if needed in the const list of each proc
(list_every
procs
(lambda (pr)
(assert_msg "check pr" (is_a pr class_nrep_anyproc))
(let ( (clbox (make_box discr_box fxocc))
(cnstproc (unsafe_get_field :nrpro_const pr)) )
(list_every
cnstproc
(lambda (cx) (if (== cx fxocc) (progn (box_put clbox ()) ()))))
(let ( (newcl (box_content clbox)) )
(if newcl (list_append cnstproc newcl)))
)))
fxocc
)
(let ( (clocc
(instance class_nrep_closedocc
:nrep_loc psloc
:nocc_symb recv
:nocc_ctyp ctype_value
:nocc_bind bind
:ncloc_procs procs))
)
;; cache the result
(mapobject_put sycmap recv clocc)
;; put the closed occurrence if needed in the closed list of each proc
(list_every
procs
(lambda (pr)
(assert_msg "check pr" (is_a pr class_nrep_anyproc))
(let ( (clbox (make_box discr_box clocc))
(clobindl (unsafe_get_field :nrpro_closedb pr)) )
(list_every
clobindl
(lambda (clbnd) (if (== clbnd bind) (progn (box_put clbox ()) ()))))
(let ( (newcl (box_content clbox)) )
(if newcl (list_append clobindl bind)))
)))
clocc
) ) ) )
;; formal arg is a local
( (is_a bind class_formal_binding)
(let ( (syocc
(instance class_nrep_locsymocc
:nrep_loc psloc
:nocc_ctyp (unsafe_get_field :fbind_type bind)
:nocc_symb recv
:nocc_bind bind) ) )
;; cache the result & return it
(mapobject_put sycmap recv syocc)
syocc
))
;; let binding is a local
( (is_a bind class_let_binding)
(let ( (syocc
(instance class_nrep_locsymocc
:nrep_loc psloc
:nocc_ctyp (unsafe_get_field :letbind_type bind)
:nocc_symb recv
:nocc_bind bind) ) )
;; cache the result & return it
(mapobject_put sycmap recv syocc)
syocc
))
;; handle fixed bindings (like defun definstance defciterator defclass ....)
( (is_a bind class_fixed_binding)
(debug_msg bind "normexp_symbol fixed_binding")
(let ( (fixdat (unsafe_get_field :fixbind_data bind)) )
(if (null fixdat)
(error_strv psloc "unresolved forward fixed reference to"
(unsafe_get_field :named_name recv)
)
)
(debug_msg fixdat "normexp_symbol fixdat")
(assert_msg "normexp_symbol check fixdat" (is_a fixdat class_nrep_data))
;; cache the result & return it
(mapobject_put sycmap recv fixdat)
fixdat)
)
;; handle letrec constructed bindings...
( (is_a bind class_normal_constructor_binding)
(debug_msg bind "normexp_symbol constructed bind")
(debug_msg procs "normexp_symbol constructed procs")
(debug_msg env "normexp_symbol constructed in env")
(debug_msg recv "normexp_symbol constructed for recv")
(let (
(nletrec (get_field :nconsb_nletrec bind))
)
(debug_msg nletrec "normexp_symbol nletrec")
(assert_msg "normexp_symbol check nletrec" (is_a nletrec class_nrep_letrec))
(let ( (nlocsyms (get_field :nletrec_locsyms nletrec))
(ourlocsym ())
)
(debug_msg nlocsyms "normexp_symbol nlocsyms")
;; find the right locsym in nlocsyms and cache it
(foreach_in_multiple
(nlocsyms)
(curlocsym :long syix)
(if (== (get_field :nocc_bind curlocsym) bind)
(progn
(setq syix -99) ;to exit the loop [-1 don't work!]
(setq ourlocsym curlocsym)))
)
(debug_msg ourlocsym "normexp_symbol ourlocsym")
(assert_msg "normexp_symbol should have ourlocsym"
(is_a ourlocsym class_nrep_locsymocc))
;; cache the result & return it
(mapobject_put sycmap recv ourlocsym)
(return ourlocsym)
)))
;; all other cases are constants, probably unusual, so we warn
(:else
(debug_msg bind "normexp_symbol const? bind")
(warning_strv psloc "bizarre?? constant reference to"
(unsafe_get_field :named_name recv))
(let ( (kocc
(instance class_nrep_constocc
:nrep_loc psloc
:nocc_ctyp ctype_value
:nocc_symb recv
:nocc_bind bind) ) )
(debug_msg kocc "normexp_symbol kocc" )
;; cache the result & return it
(mapobject_put sycmap recv kocc)
;; put the const occurrence if needed in the const list of each proc
(list_every
procs
(lambda (pr)
(debug_msg pr "normexp_symbol const pr")
(assert_msg "check pr" (is_a pr class_nrep_anyproc))
(let ( (clbox (make_box discr_box kocc))
(constproc (unsafe_get_field :nrpro_const pr)) )
(list_every
constproc
(lambda (cl) (if (== cl kocc) (progn (box_put clbox ()) ()))))
(let ( (newcl (box_content clbox)) )
(if newcl (list_append constproc newcl)))
)))
kocc
))))))
(install_method class_symbol normal_exp normexp_symbol)
;;;
(defun gectyp_symocc (recv env)
(assert_msg "check recv" (is_a recv class_nrep_symocc))
(unsafe_get_field :nocc_ctyp recv)
)
(install_method class_nrep_symocc get_ctype gectyp_symocc)
;;; normalize a class - used in particular in normalization of get_field
;; this does not work well when the class's name is locally rebound,
;; which rarely happens in practice
(defun normexp_class (recv env ncx psloc)
(debug_msg recv "normexp_class recv")
(assert_msg "check recv" (is_a recv class_class))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(let ( (clasymb (get_symbolstr (unsafe_get_field :named_name recv)))
(clabind (find_env env clasymb))
)
(debug_msg clabind "normexp_class clabind")
(cond ( (is_a clabind class_class_binding)
(let ( (normcla (normexp_symbol clasymb env ncx psloc)) )
(debug_msg normcla "normexp_class normcla class data inst")
(assert_msg "check normcla"
(or
(is_a normcla class_nrep_datainstance)
(is_a normcla class_nrep_constocc)
))
(return normcla)
))
( (is_a clabind class_value_binding)
(assert_msg "check clabind value" (== recv (unsafe_get_field :vbind_value clabind)))
(let ( (normcla (normexp_symbol clasymb env ncx psloc)) )
(debug_msg normcla "normexp_class normcla class value")
;; normcla can be a class_nrep_constocc or a class_nrep_importedval ...
(assert_msg "check normcla" (is_a normcla class_nrep))
(return normcla)
))
(:else
;; this could happen if the class's name has been locally
;; rebound, But we don't really handle that. We might scan
;; the environment stack to find the real class binding and
;; normalize accordingly, but this won't happen often...
(error_strv psloc "class incorrectly bound, perhaps locally rebound"
(unsafe_get_field :named_name recv))
(debug_msg () "normexp_class failed")
(return)
))
))
(install_method class_class normal_exp normexp_class)
;;; normalize a primitive invocation
(defun normexp_primitive (recv env ncx psloc)
(debug_msg recv "normexp_primitive recv")
(assert_msg "check prim recv" (is_a recv class_source_primitive))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(let ( (sloc (unsafe_get_field :loca_location recv))
(soper (unsafe_get_field :sprim_oper recv))
(sargs (unsafe_get_field :sargop_args recv))
)
(assert_msg "check soper" (is_a soper class_primitive))
(multicall
(nargs nbind)
(normalize_tuple sargs env ncx sloc)
(let ( (sopnamstr (unsafe_get_field :named_name soper))
(sopformals (unsafe_get_field :prim_formals soper))
(soptype (unsafe_get_field :prim_type soper))
(sopexp (unsafe_get_field :prim_expansion soper))
(:long nbarg (multiple_length nargs))
(:long nbexp (multiple_length sopexp))
)
(assert_msg "check soptype" (is_a soptype class_ctype))
(if (!=i nbarg (multiple_length sopformals))
(progn
(error_strv sloc "length mismatch between formals & actuals in primitive"
sopnamstr)
(return ()))
)
(let ( (bmap (make_mapobject discr_map_objects (+i 5 (/iraw (*i 3 nbarg) 2))))
(expargs (make_multiple discr_multiple nbexp))
)
(multiple_every
sopformals
(lambda (forb :long ix)
(assert_msg "check forb" (is_a forb class_formal_binding))
(debug_msg forb "normexp_primitive forb")
(let ( (forarg (unsafe_get_field :binder forb))
(actarg (multiple_nth nargs ix))
(fortype (unsafe_get_field :fbind_type forb))
(actype (get_ctype actarg env))
)
(debug_msg actarg "normexp_primitive actarg")
(debug_msg actype "normexp_primitive actype")
(if (and (is_a fortype class_ctype)
(is_a actype class_ctype)
(!= fortype actype))
(progn
(error_strv sloc "type mismatch between formals & actuals in primitive"
sopnamstr)
(inform_strv sloc "mismatched primitive formal name"
(unsafe_get_field :named_name forarg))
(inform_strv sloc "mismatched primitive actual type"
(unsafe_get_field :named_name actype))
(inform_strv sloc "mismatched primitive expected type"
(unsafe_get_field :named_name fortype))
))
(mapobject_put bmap forarg actarg)
)))
(debug_msg bmap "normexp_primitive bmap in sopexp")
(multiple_every
sopexp
(lambda (excu :long jx)
;;(debug_msg excu "normexp_primitive excu in sopexp")
(let ( (exval
(if (is_a excu class_symbol)
(let ( (bval (mapobject_get bmap excu)) )
(if (null bval)
(progn
;; we could perhaps handle symbols which are not primitive arguments
;; as some kind of closed constants, but this is rarely needed and
;; requires a lot of work: the excu should then be the constant
;; itself, and code should be generated to fill the primitive with
;; non-symbol values.
(debug_msg recv "normexp_primitive recv unexpected symbol in expansion")
(debug_msg excu "normexp_primitive excu")
(error_strv sloc "unexpected symbol in primitive expansion"
(unsafe_get_field :named_name excu))
(error_strv sloc "bad primitive name" sopnamstr)
))
bval)
excu)) )
(if (null exval)
(progn
(warning_strv sloc "null expansion of primitive argument for"
sopnamstr)
(if (is_a excu class_named)
(warning_strv sloc "null primitive original piece is"
(unsafe_get_field :named_name excu)))
))
;(debug_msg exval "normexp_primitive exval in sopexp")
(multiple_put_nth expargs jx exval))
))
(debug_msg soper "normexp_primitive soper")
(assert_msg "check soper is named" (is_a soper class_named))
(let ( (csym (clone_symbol soper))
(cbind (instance class_normal_let_binding
:letbind_loc sloc
:binder csym
:letbind_type soptype
:letbind_expr
(instance class_nrep_chunk
:nrep_loc sloc
:nchunk_expansion expargs
:nchunk_oper soper
:nexpr_ctyp soptype
)))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp soptype
:nocc_symb csym
:nocc_bind cbind))
)
(if (is_list nbind)
(list_append nbind cbind)
(progn
(setq nbind (make_list discr_list))
(list_append nbind cbind)
))
(debug_msg clocc "normexp_primitive result clocc")
(return
clocc
nbind
)))))))
(install_method class_source_primitive normal_exp normexp_primitive)
;;; normalize a code_chunk
(defun normexp_code_chunk (recv env ncx psloc)
(debug_msg recv "normexp_code_chunk recv")
(assert_msg "check code_chunk recv" (is_a recv class_source_codechunk))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(let ( (sloc (unsafe_get_field :loca_location recv))
(schk (unsafe_get_field :sch_chunks recv))
(gsym (unsafe_get_field :sch_gensym recv))
(csym (clone_symbol gsym))
(csymstr (let ( (sbuf (make_strbuf discr_strbuf)) )
(add2sbuf_string sbuf (get_field :named_name csym))
(add2sbuf_strconst sbuf "__")
(add2sbuf_longdec sbuf (get_int (get_field :csym_urank csym)))
(strbuf2string discr_verbatim_string sbuf)
))
(nchk (multiple_map
schk
(lambda (curcomp :long curix)
(cond
( (is_string curcomp) curcomp)
( (== curcomp gsym) csymstr)
( (is_a curcomp class_symbol)
(normal_exp curcomp env ncx sloc))
(:else
(assert_msg "impossible curcomp in src code_chunk" ()))
))))
(cbind (instance class_normal_let_binding
:letbind_loc sloc
:binder csym
:letbind_type ctype_void
:letbind_expr
(instance class_nrep_chunk
:nrep_loc sloc
:nchunk_expansion nchk
:nchunk_oper csym
:nexpr_ctyp ctype_void
)))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctype_void
:nocc_symb csym
:nocc_bind cbind))
)
(debug_msg clocc "normexp_code_chunk clocc")
(assert_msg "check nchk" (is_multiple nchk))
(return clocc (list cbind))
))
(install_method class_source_codechunk normal_exp normexp_code_chunk)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; normalize a cmatchexpr
(defun normexp_cmatchexpr (recv env ncx psloc)
(debug_msg recv "normexp_cmatchexpr recv")
(assert_msg "check recv" (is_a recv class_source_cmatchexpr))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(let ( (sloc (unsafe_get_field :loca_location recv))
(scmat (unsafe_get_field :scmatx_cmatcher recv))
(sargs (unsafe_get_field :sargop_args recv))
)
(assert_msg "check scmat" (is_a scmat class_cmatcher))
(multicall
(nargs nbind)
(normalize_tuple sargs env ncx sloc)
(let ( (cmanamstr (unsafe_get_field :named_name scmat))
;; the outformals are really the "input" arguments for cmatchexpr
(oformals (unsafe_get_field :amatch_out scmat))
;; the matchbind gives the result of the cmatchexpr
(mabind (let ( (mb (unsafe_get_field :amatch_matchbind scmat)) )
(assert_msg "check mabind" (is_a mb class_formal_binding))
mb))
;; the type of the cmatchexpr
(otype (unsafe_get_field :fbind_type mabind))
(sopexp (unsafe_get_field :cmatch_expoper scmat))
(:long nbarg (multiple_length nargs))
(:long nbexp (multiple_length sopexp))
)
(assert_msg "check otype" (is_a otype class_ctype))
(if (!=i nbarg (multiple_length oformals))
(progn
(error_strv sloc "length mismatch between formals & actuals in cmatch expr"
cmanamstr)
(return))
)
(let ( (bmap (make_mapobject discr_map_objects (+i 5 (/iraw (*i 3 nbarg) 2))))
(expargs (make_multiple discr_multiple nbexp))
)
(multiple_every
oformals
(lambda (forb :long ix)
(assert_msg "check forb" (is_a forb class_formal_binding))
(debug_msg forb "normexp_cmatchexpr forb")
(let ( (forarg (unsafe_get_field :binder forb))
(actarg (multiple_nth nargs ix))
(fortype (unsafe_get_field :fbind_type forb))
(actype (get_ctype actarg env))
)
(debug_msg actarg "normexp_cmatchexpr actarg")
(debug_msg actype "normexp_cmatchexpr actype")
(if (and (is_a fortype class_ctype)
(is_a actype class_ctype)
(!= fortype actype))
(progn
(error_strv sloc "type mismatch between formals & actuals in cmatch oper"
cmanamstr)
(inform_strv sloc "mismatched primitive formal name"
(unsafe_get_field :named_name forarg))
(inform_strv sloc "mismatched primitive actual type"
(unsafe_get_field :named_name actype))
(inform_strv sloc "mismatched primitive expected type"
(unsafe_get_field :named_name fortype))
))
(mapobject_put bmap forarg actarg)
)))
(debug_msg bmap "normexp_cmatchexpr bmap in sopexp")
(multiple_every
sopexp
(lambda (excu :long jx)
;;(debug_msg excu "normexp_cmatchexpr excu in sopexp")
(let ( (exval
(if (is_a excu class_symbol)
(let ( (bval (mapobject_get bmap excu)) )
(if (null bval)
(progn
;; we could perhaps handle symbols which are not primitive arguments
;; as some kind of closed constants, but this is rarely needed and
;; requires a lot of work: the excu should then be the constant
;; itself, and code should be generated to fill the primitive with
;; non-symbol values.
(debug_msg recv "normexp_cmatchexpr recv unexpected symbol in expansion")
(debug_msg excu "normexp_cmatchexpr excu")
(error_strv sloc "unexpected symbol in cmatch expression expansion"
(unsafe_get_field :named_name excu))
(error_strv sloc "bad primitive name" cmanamstr)
))
bval)
excu)) )
(if (null exval)
(progn
(warning_strv sloc "null expansion of cmatch expression argument for"
cmanamstr)
(if (is_a excu class_named)
(warning_strv sloc "null cmatch expression original piece is"
(unsafe_get_field :named_name excu)))
))
;(debug_msg exval "normexp_cmatchexpr exval in sopexp")
(multiple_put_nth expargs jx exval))
))
(let ( (csym (clone_symbol cmanamstr))
(cbind (instance class_normal_let_binding
:letbind_loc sloc
:binder csym
:letbind_type otype
:letbind_expr
(instance class_nrep_chunk
:nrep_loc sloc
:nchunk_expansion expargs
:nchunk_oper scmat
:nexpr_ctyp otype
)))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp otype
:nocc_symb csym
:nocc_bind cbind))
)
(if (is_list nbind)
(list_append nbind cbind)
(progn
(setq nbind (make_list discr_list))
(list_append nbind cbind)
))
(debug_msg clocc "normexp_cmatchexpr result clocc")
(return
clocc
nbind
)))))))
(install_method class_source_cmatchexpr normal_exp normexp_cmatchexpr)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; normalize a funmatchexpr
(defun normexp_funmatchexpr (recv env ncx psloc)
(debug_msg recv "normexp_funmatchexpr recv")
(assert_msg "check recv" (is_a recv class_source_funmatchexpr))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(let ( (sloc (unsafe_get_field :loca_location recv))
(sfmat (unsafe_get_field :sfmatx_fmatcher recv))
(sfbind (unsafe_get_field :sfmatx_fmatbind recv))
(sargs (unsafe_get_field :sargop_args recv))
(nbind (make_list discr_list))
)
(assert_msg "check sfmat" (is_a sfmat class_funmatcher))
(assert_msg "check sfbind" (is_a sfbind class_any_binding))
(debug_msg sfbind "normexp_funmatchexpr sfbind")
(let ( (fmatsym (unsafe_get_field :binder sfbind)) )
(assert_msg "check fmatsym" (is_a fmatsym class_symbol))
(assert_msg "check good sfbind" (== sfbind (find_env env fmatsym)))
(let ( (nfmat (normal_exp fmatsym env ncx psloc)) )
(debug_msg nfmat "normexp_funmatchexpr nfmat")
;; should create a normlet binding to hold the nfmat's
;; fmatch_applyf field
(let ( (csym (clone_symbol fmatsym))
(cbind (instance class_normal_let_binding
:letbind_loc sloc
:binder csym
:letbind_type ctype_value
:letbind_expr
(instance class_nrep_unsafe_get_field
:nrep_loc sloc
:nuget_obj nfmat
:nuget_field fmatch_applyf)
))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctype_value
:nocc_symb csym
:nocc_bind cbind))
)
(list_append nbind cbind)
(multicall
(nargs nargbind)
(normalize_tuple sargs env ncx sloc)
(debug_msg nargs "normexp_funmatchexpr nargs")
(debug_msg nargbind "normexp_funmatchexpr nargbind")
(list_append2list nbind nargbind)
(let (
(asym (clone_symbol fmatsym))
(abind (instance class_normal_let_binding
:letbind_loc sloc
:binder asym
:letbind_type ctype_value
:letbind_expr
(instance class_nrep_apply
:nexpr_ctyp ctype_value
:nrep_loc sloc
:napp_fun clocc
:nexpr_args nargs
)))
(calocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctype_value
:nocc_symb asym
:nocc_bind abind
))
)
(list_append nbind abind)
(debug_msg calocc "normexp_funmatchexpr final calocc")
(debug_msg nbind "normexp_funmatchexpr finale nbind")
(return calocc nbind)
)))))))
(install_method class_source_funmatchexpr normal_exp normexp_funmatchexpr)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; normalize an application
(defun normexp_apply (recv env ncx psloc)
(assert_msg "check apply recv" (is_a recv class_source_apply))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(let ( (sloc (unsafe_get_field :loca_location recv))
(sfun (unsafe_get_field :sapp_fun recv))
(sargs (unsafe_get_field :sargop_args recv))
(sfusymb (if (is_a sfun class_symbol) sfun '_fun_))
)
(multicall
(nfun nbindfun)
(normal_exp sfun env ncx sloc)
(assert_msg "check nbindfun" (is_list_or_null nbindfun))
(multicall
(nargs nbindargs)
(normalize_tuple sargs env ncx sloc)
(assert_msg "check nbindargs" (is_list_or_null nbindargs))
(check_ctype_nargs nargs env sloc)
(setq nbindargs (list_append2list nbindargs nbindfun))
(let ( (csym (clone_symbol sfusymb))
(cbind (instance class_normal_let_binding
:letbind_loc sloc
:binder csym
:letbind_type ctype_value
:letbind_expr
(instance class_nrep_apply
:nrep_loc sloc
:nexpr_ctyp ctype_value
:napp_fun nfun
:nexpr_args nargs
)))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctype_value
:nocc_symb csym
:nocc_bind cbind
))
)
(if (not (is_list nbindargs))
(setq nbindargs (make_list discr_list)))
(list_append nbindargs cbind)
(return clocc nbindargs)
)))))
(install_method class_source_apply normal_exp normexp_apply)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; normalize a message send
(defun normexp_msend (msnd env ncx psloc)
(assert_msg "check msnd" (is_a msnd class_source_msend))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg msnd "normexp_msend msnd")
(let ( (msrecv (unsafe_get_field :msend_recv msnd))
(msargs (unsafe_get_field :sargop_args msnd))
(selnam (unsafe_get_field :msend_selsymb msnd))
(curproc (unsafe_get_field :nctx_curproc ncx))
(sloc (unsafe_get_field :loca_location msnd))
(nsel (normexp_symbol selnam env ncx sloc))
(selbind (find_env env selnam))
)
(debug_msg curproc "normexp_msend curproc")
(debug_msg selbind "normexp_msend selbind")
;;;; we should add the constant selector into the current routine's constant pool
(multicall
(nrecv nbindrecv)
(normal_exp msrecv env ncx sloc)
(assert_msg "check nbindrecv" (is_list_or_null nbindrecv))
;; check that reciever is a value
(let ( (ctypr (get_ctype nrecv env)) )
(assert_msg "normexp_msend check ctypr " (is_a ctypr class_ctype))
(if (!= ctypr ctype_value)
(error_strv sloc "non value reciever for message send of selector"
(unsafe_get_field :named_name selnam)))
)
(multicall
(nargs nbindargs)
(normalize_tuple msargs env ncx sloc)
(assert_msg "check nbindargs" (is_list_or_null nbindargs))
(setq nbindrecv (list_append2list nbindrecv nbindargs))
(check_ctype_nargs nargs env sloc)
(let ( (selformals
(cond ( (is_a selbind class_selector_binding)
(get_field :sdefsel_formals (get_field :sbind_selectordef selbind)
)
)
( (is_a selbind class_value_binding)
(let ( (valsel (get_field :vbind_value selbind)) )
(assert_msg "check valsel" (is_a valsel class_selector))
(get_field :sel_signature valsel))
)
(:else
(assert_msg "invalid selbind" ())
())))
(csym (clone_symbol selnam))
(nsend (instance class_nrep_msend
:nrep_loc sloc
:nexpr_ctyp ctype_value
:nsend_sel nsel
:nsend_recv nrecv
:nexpr_args nargs
))
(cbind (instance class_normal_let_binding
:letbind_loc sloc
:binder csym
:letbind_type ctype_value
:letbind_expr nsend))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctype_value
:nocc_symb csym
:nocc_bind cbind))
)
(debug_msg nsend "normexp_msend nsend middle")
(assert_msg "check nrecv" (is_object nrecv))
(if (is_multiple selformals)
(let ( (recvformal (multiple_nth selformals 0))
(:long nbformals (multiple_length selformals))
)
(debug_msg selformals "normexp_msend")
(assert_msg "check recvformal" (== (get_field :fbind_type recvformal) ctype_value))
(if (!=i nbformals (+i 1 (multiple_length nargs)))
(warning_strv sloc "unexpected number of arguments for method"
(get_field :named_name selnam))
)
(foreach_in_multiple
(nargs)
(curnarg :long nix)
(let ( (curformal (multiple_nth selformals (+i nix 1)))
(curctyp (get_ctype curnarg env))
(formctyp (get_field :fbind_type curformal))
(formbinder (get_field :binder curformal))
)
(assert_msg "check curformal"
(is_a curformal class_formal_binding))
(if (!= curctyp formctyp)
(progn
(warning_strv sloc "c-type mismatch in method send argument"
(get_field :named_name selnam))
(inform_strv sloc "mismatched method formal name"
(get_field :named_name formbinder))
(inform_strv sloc "mismatched method actual type"
(get_field :named_name curctyp))
(inform_strv sloc "mismatched method expected type"
(get_field :named_name formctyp))
)
)
))))
(unsafe_put_fields clocc :nocc_bind cbind)
(if (not (is_list nbindrecv))
(setq nbindrecv (make_list discr_list)))
(list_append nbindrecv cbind)
(debug_msg nbindrecv "normexp_msend final nbindrecv")
(debug_msg clocc "normexp_msend clocc")
(return clocc nbindrecv)
)))))
(install_method class_source_msend normal_exp normexp_msend)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; normalize a return
(defun normexp_return (recv env ncx psloc)
(debug_msg recv "normexp_return recv")
(assert_msg "check return recv" (is_a recv class_source_return))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(let ( (sloc (unsafe_get_field :loca_location recv))
(srets (unsafe_get_field :sargop_args recv))
(:long nbrets (multiple_length srets))
(csym (clone_symbol 'return_))
;; while the return effectively go out, it is preferable to give it a value type
;; to avoid make warning on code like (if (p x) (return) (.....))
(cbind (instance class_normal_let_binding
:letbind_loc sloc
:binder csym
:letbind_type ctype_value
;; :letbind_expr is filled later
))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctype_value
:nocc_symb csym
:nocc_bind cbind))
)
;; special case for empty return
(if (<=i nbrets 0)
(let (
(nbindemp (make_list discr_list))
(nemptret
(instance class_nrep_return
:nrep_loc sloc
:nret_main ()
:nret_rest ()
))
)
(unsafe_put_fields cbind :letbind_expr nemptret)
(list_append nbindemp cbind)
(debug_msg clocc "normexp_return empty; return clocc")
(debug_msg nbindemp "normexp_return empty; return nbindemp")
(return clocc nbindemp))
(multicall
(nrets nbindrets)
(normalize_tuple srets env ncx sloc)
(let ( (nret0 (multiple_nth nrets 0))
(toth (make_multiple discr_multiple (-i nbrets 1)))
(ctyp0 (get_ctype nret0 env))
)
(if (!= ctyp0 ctype_value)
(error_plain sloc "main return is not a value"))
(if (null nbindrets)
(setq nbindrets (make_list discr_list)))
(multiple_every
nrets
(lambda (ncomp :long ix)
(if (>i ix 0)
(multiple_put_nth toth (-i ix 1) ncomp))))
(let ( (nret
(instance class_nrep_return
:nrep_loc sloc
:nret_main nret0
:nret_rest (if (>i nbrets 0) toth)))
)
(unsafe_put_fields cbind :letbind_expr nret)
(list_append nbindrets cbind)
(debug_msg clocc "normexp_return result nret")
(debug_msg nbindrets "normexp_return result nbindrets")
(return clocc nbindrets)
))))))
(install_method class_source_return normal_exp normexp_return)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; normalize an if
(defun normexp_if (recv env ncx psloc)
(assert_msg "check if recv" (is_a recv class_source_if))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_if recv")
(let ( (sloc (unsafe_get_field :loca_location recv))
(stest (unsafe_get_field :sif_test recv))
(ctypif ctype_void)
(sthen (unsafe_get_field :sif_then recv))
)
(multicall
(ntest nbindif) ;nbindif is also the whole result binding
(normal_exp stest env ncx sloc)
(assert_msg "check nbindif test" (is_list_or_null nbindif))
(debug_msg ntest "normexp_if ntest")
;; in practice we don't need to make a common super-
;; environment with nbindif since all relevant bindings there are
;; generated, with unique cloned symbols, and these bindings
;; are local to the test part
(multicall
(nthen nbindthen)
(normal_exp sthen env ncx sloc)
(debug_msg nthen "normexp_if nthen")
(assert_msg "check nbindthen" (is_list_or_null nbindthen))
(let ( (newthenenv (fresh_env env)) )
(list_every
nbindthen
(lambda (b) (put_env newthenenv b)))
;; the ctyp of the whole if is initialized to the ctype of the then part
(setq ctypif (get_ctype nthen newthenenv))
;;
(let ( (csym (clone_symbol '_if_))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctypif
:nocc_symb csym))
(wthen (wrap_normal_let1 nthen nbindthen sloc))
(cbind (instance class_normal_let_binding
:letbind_loc sloc
:binder csym
:letbind_type ctypif
:letbind_expr
(instance class_nrep_if
:nrep_loc sloc
:nif_test ntest
:nif_then wthen
:nif_else ()
:nexpr_ctyp ctypif
)))
)
(unsafe_put_fields clocc :nocc_bind cbind)
(if (not (is_list nbindif))
(setq nbindif (make_list discr_list)))
(list_append nbindif cbind)
(debug_msg clocc "normexp_if result clocc")
(debug_msg nbindif "normexp_if result nbindif")
(return clocc nbindif)
))
))))
(install_method class_source_if normal_exp normexp_if)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; normalize an ifelse
(defun normexp_ifelse (recv env ncx psloc)
(assert_msg "check if recv" (is_a recv class_source_ifelse))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_ifelse recv")
(let ( (sloc (unsafe_get_field :loca_location recv))
(stest (unsafe_get_field :sif_test recv))
(ctypif ctype_void)
(sthen (unsafe_get_field :sif_then recv))
(selse (unsafe_get_field :sif_else recv))
)
(multicall
(ntest nbindif) ;nbindif is also the whole result binding
(normal_exp stest env ncx sloc)
(debug_msg ntest "normexp_ifelse ntest")
(assert_msg "check nbindif test" (is_list_or_null nbindif))
;; in practice we don't need to make a common super-
;; environment with nbindif since all relevant bindings there are
;; generated, with unique cloned symbols, and these bindings
;; are local to the test part
(multicall
(nthen nbindthen)
(normal_exp sthen env ncx sloc)
(debug_msg nthen "normexp_ifelse nthen")
(assert_msg "check nbindthen" (is_list_or_null nbindthen))
(let ( (newthenenv (fresh_env env)) )
(list_every
nbindthen
(lambda (b) (put_env newthenenv b)))
;; the ctyp of the whole if is initialized to the ctype of the then part
(setq ctypif (get_ctype nthen newthenenv))
(multicall
(nelse nbindelse)
(normal_exp selse env ncx sloc)
(debug_msg nelse "normexp_ifelse nelse")
(assert_msg "check nbindelse" (is_list_or_null nbindelse))
;; if we have both then & else branches,
;; ensure their compatibility of types
(let ( (newelseenv (let ( (nenv (fresh_env env)) )
(list_every
nbindelse
(lambda (b) (put_env nenv b)))
nenv
))
(ctypelse (get_ctype nelse newelseenv)) )
(assert_msg "check ctypif" (is_a ctypif class_ctype))
(assert_msg "check ctypelse" (is_a ctypelse class_ctype))
(cond
( (== ctypif ctypelse)
()
)
( (and (!= ctypif ctype_void) (== ctypelse ctype_void))
() ;; ctypif is correct
)
( (and (== ctypif ctype_void) (!= ctypelse ctype_void))
(setq ctypif ctypelse)
)
(:else
(warning_plain sloc
"incompatible types in conditional IF/OR/COND branches")
(warning_strv sloc "then type in conditional is "
(unsafe_get_field :named_name ctypif))
(warning_strv sloc "else type in conditional is "
(unsafe_get_field :named_name ctypelse))
(setq ctypif ctype_void)
)
))
;;
;;
(let ( (csym (clone_symbol '_ifelse_))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctypif
:nocc_symb csym))
(wthen (wrap_normal_let1 nthen nbindthen sloc))
(welse (wrap_normal_let1 nelse nbindelse sloc))
(cbind (instance class_normal_let_binding
:letbind_loc sloc
:binder csym
:letbind_type ctypif
:letbind_expr
(instance class_nrep_if
:nrep_loc sloc
:nif_test ntest
:nif_then wthen
:nif_else welse
:nexpr_ctyp ctypif
))) )
(unsafe_put_fields clocc :nocc_bind cbind)
(if (not (is_list nbindif))
(setq nbindif (make_list discr_list)))
(list_append nbindif cbind)
(debug_msg clocc "normexp_ifelse result clocc")
(debug_msg nbindif "normexp_ifelse result nbindif")
(return clocc nbindif)
)))))))
(install_method class_source_ifelse normal_exp normexp_ifelse)
;;;;;;;;;;;;;;;; normalize a cppif
(defun normexp_cppif (recv env ncx psloc)
(assert_msg "check cppif recv" (is_a recv class_source_cppif))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_cppif recv")
(let ( (sloc (unsafe_get_field :loca_location recv))
(scond (unsafe_get_field :sifp_cond recv))
(ctypif ctype_void)
(sthen (unsafe_get_field :sifp_then recv))
(selse (unsafe_get_field :sifp_else recv))
)
;; normalize the then-part
(multicall
(nthen nbindthen)
(normal_exp sthen env ncx sloc)
(debug_msg nthen "normexp_cppif nthen")
(assert_msg "check nbindthen" (is_list_or_null nbindthen))
(let ( (newthenenv (fresh_env env)) )
(list_every
nbindthen
(lambda (b) (put_env newthenenv b)))
;; the ctyp of the whole cppif is initialized to the ctype of the then part
(setq ctypif (get_ctype nthen newthenenv))
(assert_msg "check ctypif" (is_a ctypif class_ctype))
;; normalize the else-part
(multicall
(nelse nbindelse)
(normal_exp selse env ncx sloc)
(debug_msg nelse "normexp_cppif nelse")
(assert_msg "check nbindelse" (is_list_or_null nbindelse))
(let ( (newelseenv (fresh_env env)) )
(list_every
nbindelse
(lambda (b) (put_env newelseenv b)))
(let ( (ctypelse (get_ctype nelse newelseenv)) )
(if (and (!= ctypif ctypelse) (!= ctypif ctype_void) (!= ctypelse ctype_void))
(progn
(error_plain sloc "CPPIF incompatible then & else types")
(error_strv sloc "CPPIF incompatible then type :"
(unsafe_get_field :named_name ctypif))
(error_strv sloc "CPPIF incompatible else type :"
(unsafe_get_field :named_name ctypelse))))
(let ( (csym (clone_symbol 'ifcpp_))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctypif
:nocc_symb csym))
(wthen (wrap_normal_let1 nthen nbindthen sloc))
(welse (wrap_normal_let1 nelse nbindelse sloc))
(cbind (instance class_normal_let_binding
:letbind_loc sloc
:binder csym
:letbind_type ctypif
:letbind_expr
(instance class_nrep_cppif
:nrep_loc sloc
:nifp_cond scond
:nifp_then wthen
:nifp_else welse
:nifp_ctyp ctypif
)))
(nbindres (make_list discr_list))
)
(unsafe_put_fields clocc :nocc_bind cbind)
(list_append nbindres cbind)
(debug_msg clocc "normexp_cppif result clocc")
(debug_msg nbindres "normexp_cppif result nbindres")
(return clocc nbindres)
))))))))
(install_method class_source_cppif normal_exp normexp_cppif)
;;;;;;;;;;;;;;;; normalize an or
;; (OR (f1 a1)) is let d1 = (f1 a1) in d1
;; (OR (f1 a1) (f2 a2)) is let o1 = (let d1 = (f1 a1) in (if d1 d1 (let d2 = (f2 a2) in d2))) in o1
(defun normexp_or (recv env ncx psloc)
(assert_msg "check or recv" (is_a recv class_source_or))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_or recv")
(let (
(boxorcount (make_integerbox discr_integer (the_callcount)))
(sloc (unsafe_get_field :loca_location recv))
(sdisj (unsafe_get_field :sor_disj recv))
(:long nbdisj (multiple_length sdisj))
(:long ix (-i nbdisj 1))
(norbox (make_box discr_box ()))
(nbindorbox (make_box discr_box (make_list discr_list)))
(ctyporbox (make_box discr_box ctype_void))
(newenv (fresh_env env))
)
(multiple_backward_every
sdisj
(lambda (scur :long six)
(debug_msg scur "normexp scur" (get_int boxorcount))
(multicall
(ncur nbind)
(normal_exp scur env ncx sloc)
(debug_msg ncur "normexp ncur" (get_int boxorcount))
(list_every ncur
(lambda (bnd) (put_env newenv bnd)))
(if (null (box_content norbox))
(progn
(box_put nbindorbox nbind)
(box_put norbox ncur)
(box_put ctyporbox (get_ctype ncur newenv))
()
)
(let ( (ctypcur (get_ctype ncur newenv))
)
(assert_msg "check ctypcur" (is_a ctypcur class_ctype))
(if (!= ctypcur (box_content ctyporbox))
(error_plain sloc "disjuncts' type mismatch in OR | COND"))
(let (
;; ncur is normal, so simple
(nifor (instance
class_nrep_if
:nrep_loc sloc
:nif_test ncur
:nif_then ncur
:nif_else (wrap_normal_let1 (box_content norbox) (box_content nbindorbox) sloc)
:nexpr_ctyp ctypcur
)
)
(csymor (clone_symbol 'or_))
(corbind (instance
class_normal_let_binding
:binder csymor
:letbind_loc sloc
:letbind_type ctypcur
:letbind_expr nifor))
(corocc (instance
class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctypcur
:nocc_symb csymor
:nocc_bind corbind))
)
(box_put nbindorbox (make_list discr_list))
(list_append2list (box_content nbindorbox) nbind)
(list_append (box_content nbindorbox) corbind)
(box_put norbox corocc)
()
)
)
)
)
)
)
(debug_msg (box_content norbox) "normexp_or result nor")
(debug_msg (box_content nbindorbox) "normexp_or result nbindor")
(return (box_content norbox) (box_content nbindorbox))
)
)
(install_method class_source_or normal_exp normexp_or)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; normalize a PROGN
;;;; (PROGN a1 a2 ... an) is based upon the normalization of (LET () a1 a2 ... an)
(defun normexp_progn (recv env ncx psloc)
(assert_msg "check progn recv" (is_a recv class_source_progn))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_progn recv")
(let ( (sloc (unsafe_get_field :loca_location recv))
(sbody (unsafe_get_field :sprogn_body recv))
(:long lnbody (multiple_length sbody))
(boxlnbody (make_integerbox discr_integer lnbody))
)
(if (<=i lnbody 0)
(let ( (onull (instance class_nrep_nil :nrep_loc sloc)) )
(error_plain sloc "empty PROGN")
(debug_msg onull "normexp_progn return empty onull")
(return onull)))
(multicall
(nbody nbind)
(normalize_tuple sbody env ncx sloc)
(debug_msg nbody "normexp_progn nbody")
(debug_msg nbind "normexp_progn nbind")
(let ( (nlast (multiple_nth nbody -1))
(:long lenbody (multiple_length nbody))
(nallbutlast (if (>i lenbody 0) (make_multiple discr_multiple (-i lenbody 1))))
)
;; fill nallbutlast
(let ( (:long ix (-i lenbody 1)) )
(forever bodyloop
(if (<i ix 0) (exit bodyloop))
(multiple_put_nth nallbutlast ix (multiple_nth nbody ix))
(setq ix (-i ix 1))))
(debug_msg nallbutlast "normexp_progn nallbutlast")
(if (null nbind) (setq nbind (make_list discr_list)))
(let (
(csym (clone_symbol 'progn_))
(lastctyp (get_ctype nlast env))
(cbind (instance class_normal_let_binding
:binder csym
:letbind_loc sloc
:letbind_type lastctyp
:letbind_expr (instance class_nrep_progn
:nrep_loc sloc
:nprogn_seq nallbutlast
:nprogn_last nlast)))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp lastctyp
:nocc_symb csym
:nocc_bind cbind))
)
(list_append nbind cbind)
(debug_msg clocc "normexp_progn return clocc")
(debug_msg nbind "normexp_progn return nbind")
(return clocc nbind)
)))))
(install_method class_source_progn normal_exp normexp_progn)
(install_method class_nrep_progn get_ctype
(lambda (recv env) (get_ctype (unsafe_get_field :nprogn_last recv) env)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; normalize a LET
(defun normexp_let (recv env ncx psloc)
(assert_msg "check let recv" (is_a recv class_source_let))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_let recv")
(let ( (sloc (unsafe_get_field :loca_location recv))
(sbindings (unsafe_get_field :slet_bindings recv))
(sbody (unsafe_get_field :slet_body recv))
(newenv (fresh_env env))
(bindlist (make_list discr_list))
)
(multiple_every
sbindings
(lambda (sb :long sbix)
(assert_msg "check sb" (is_a sb class_source_let_binding))
(let ( (sbloc (unsafe_get_field :loca_location sb))
(sbtyp (unsafe_get_field :sletb_type sb))
(sbinder (unsafe_get_field :sletb_binder sb))
(sbexpr (unsafe_get_field :sletb_expr sb))
)
;; issue an inform message if the let binding overrides a previous one
(and
(has_extra_warnings)
(find_env env sbinder)
(inform_strv sbloc "this LET binding hides another one in enclosing scope - "
(get_field :named_name sbinder))
)
;;; normalize the binding's expression
(multicall
(nbdexpr nbindings)
(normal_exp sbexpr newenv ncx sbloc)
(let ( (lastnbinding (pair_head (list_last nbindings))) )
;; common case of a normalized apply or primitive, hence a
;; gensymed variable which is the last in the nbindings
(if (and
(is_a lastnbinding class_normal_let_binding)
(is_a nbdexpr class_nrep_locsymocc)
(== (unsafe_get_field :binder lastnbinding)
(unsafe_get_field :nocc_symb nbdexpr))
)
(let ( (lastnormexp (unsafe_get_field :letbind_expr nbdexpr)) )
(list_every
nbindings
(lambda (b)
(if (!= b lastnbinding)
(list_append bindlist b))))
(let ( (newcbnd
(instance class_normal_let_binding
:binder sbinder
:letbind_type (unsafe_get_field :letbind_type lastnbinding)
:letbind_expr (unsafe_get_field :letbind_expr lastnbinding)
:letbind_loc (unsafe_get_field :letbind_loc lastnbinding)))
)
(list_append bindlist newcbnd)
(put_env newenv newcbnd)
)
)
(progn
;;; otherwise, eg a plain constant, a complex if...
(list_append2list bindlist nbindings)
(let ( (newpbnd
(instance class_normal_let_binding
:binder sbinder
:letbind_type sbtyp
:letbind_expr nbdexpr
:letbind_loc sbloc)) )
(list_append bindlist newpbnd)
(put_env newenv newpbnd)
))
))))
))
;;; end of loop on source bindings
(multicall
(nbody nbodbindings)
(normalize_tuple sbody newenv ncx sloc)
(list_append2list bindlist nbodbindings)
(assert_msg "normexp_let check bindlist" (is_list_or_null bindlist))
(list_every
bindlist
(lambda (cbnd)
(assert_msg "normexp_let check cbnd" (is_a cbnd class_normal_let_binding))))
;;; remove every locally bound symbol from the symbol cache map
(let ( (sycmap (unsafe_get_field :nctx_symbcachemap ncx)) )
(list_every bindlist
(lambda (bnd)
(mapobject_remove sycmap (unsafe_get_field :binder bnd))
)))
;;;; make the result
(let (
(nlastbody (multiple_nth nbody -1))
;; the type of a let with empty body is void
(nlastyp (or (get_ctype nlastbody newenv) ctype_void))
(csym (clone_symbol 'let_))
(nlet
(instance class_nrep_let
:nrep_loc sloc
:nlet_bindings (list_to_multiple bindlist discr_multiple)
:nlet_body nbody))
(cbind (instance class_normal_let_binding
:binder csym
:letbind_loc sloc
:letbind_type nlastyp
:letbind_expr nlet))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp nlastyp
:nocc_bind cbind))
(resbinds (make_list discr_list))
)
(list_append resbinds cbind)
(debug_msg clocc "normexp_let result clocc")
(debug_msg resbinds "normexp_let result resbinds")
(return clocc resbinds)
)
)))
(install_method class_source_let normal_exp normexp_let)
(install_method class_nrep_let get_ctype
(lambda (recv env)
(let ( (lbod (unsafe_get_field :nlet_body recv))
(:long lenb (multiple_length lbod)) )
(if (<=i lenb 0) ctype_void
(get_ctype (multiple_nth lbod (-i lenb 1)) env)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; normalize an UNSAFE_GET_FIELD
(defun normexp_unsafe_get_field (recv env ncx psloc)
(assert_msg "check unsafegetfield recv" (is_a recv class_source_unsafe_get_field))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp unsafeget recv")
(let ( (sloc (unsafe_get_field :loca_location recv))
(sobj (unsafe_get_field :suget_obj recv))
(sfld (unsafe_get_field :suget_field recv))
)
(assert_msg "check sfld" (is_a sfld class_field))
(multicall
(nobj nbind)
(normal_exp sobj env ncx sloc)
(if (null nbind) (setq nbind (make_list discr_list)))
(let ( (csym (clone_symbol (unsafe_get_field :named_name sfld)))
(cbind (instance class_normal_let_binding
:binder csym
:letbind_loc sloc
:letbind_type ctype_value
:letbind_expr
(instance class_nrep_unsafe_get_field
:nrep_loc sloc
:nuget_obj nobj
:nuget_field sfld)))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctype_value
:nocc_symb csym
:nocc_bind cbind))
)
(list_append nbind cbind)
(debug_msg clocc "normexp unsafeget result clocc")
(debug_msg nbind "normexp unsafeget result nbind")
(return clocc nbind
)))))
(install_method class_source_unsafe_get_field normal_exp normexp_unsafe_get_field)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; normalize an GET_FIELD
(defun normexp_get_field (recv env ncx psloc)
(assert_msg "check getfield recv" (is_a recv class_source_get_field))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_get_field recv")
(let ( (sloc (unsafe_get_field :loca_location recv))
(sobj (unsafe_get_field :suget_obj recv))
(sfld (unsafe_get_field :suget_field recv))
)
(assert_msg "check sfld" (is_a sfld class_field))
(multicall
(nobj nbind)
(normal_exp sobj env ncx sloc)
(if (null nbind) (setq nbind (make_list discr_list)))
(debug_msg nobj "normexp_get_field nobj")
(let ( (csym (clone_symbol (unsafe_get_field :named_name sfld)))
(fcla (unsafe_get_field :fld_ownclass sfld))
(ncla (let ( (nc (normal_exp fcla env ncx psloc)) )
(debug_msg nc "normexp_get_field ncla")
nc))
(nuget (instance class_nrep_unsafe_get_field
:nrep_loc sloc
:nuget_obj nobj
:nuget_field sfld))
(cbind (instance class_normal_let_binding
:binder csym
:letbind_loc sloc
:letbind_type ctype_value
:letbind_expr
(instance class_nrep_ifisa
:nrep_loc sloc
:nifa_val nobj
:nifa_class ncla
:nif_then nuget
:nexpr_ctyp ctype_value
)))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctype_value
:nocc_symb csym
:nocc_bind cbind))
)
(list_append nbind cbind)
(debug_msg clocc "normexp_get_field clocc")
(debug_msg nbind "normexp_get_field nbind")
(return clocc nbind
)))))
(install_method class_source_get_field normal_exp normexp_get_field)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; normalize an UNSAFE_PUT_FIELDS
(defun normexp_unsafe_put_fields (recv env ncx psloc)
(assert_msg "check unsafeputfields recv" (is_a recv class_source_unsafe_put_fields))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_unsafe_put_fields recv")
(let ( (sloc (unsafe_get_field :loca_location recv))
(sobj (unsafe_get_field :suput_obj recv))
(sfields (unsafe_get_field :suput_fields recv))
(:long nbfields (multiple_length sfields))
(nfields (make_multiple discr_multiple nbfields))
(nbindlist (make_list discr_list))
(csym (clone_symbol 'unsafput_))
(cbind (instance class_normal_let_binding
:binder csym
:letbind_loc sloc
:letbind_type ctype_void
;; letbind_expr filled later
:letbind_expr ()))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctype_void
:nocc_symb csym
:nocc_bind cbind))
)
(multicall
(nobj nobjbind)
(normal_exp sobj env ncx sloc)
(list_append2list nbindlist nobjbind)
(multiple_every
sfields
(lambda (fla :long ix)
(assert_msg "check fla" (is_a fla class_source_fieldassign))
(let ( (fld (unsafe_get_field :sfla_field fla))
(exp (unsafe_get_field :sfla_expr fla)) )
(assert_msg "check fld" (is_a fld class_field))
(multicall
(nexp nexpbind)
(normal_exp exp env ncx sloc)
(list_append2list nbindlist nexpbind)
(let ( (nfla (instance class_nrep_fieldassign
:nrep_loc sloc
:nfla_field fld
:nfla_val nexp)) )
(multiple_put_nth nfields ix nfla)
)))))
(let ( (npuf (instance class_nrep_unsafe_put_fields
:nrep_loc sloc
:nuput_obj nobj
:nuput_fields nfields)) )
(unsafe_put_fields cbind :letbind_expr npuf)
(list_append nbindlist cbind)
(debug_msg clocc "normexp_unsafe_put_fields result clocc")
(debug_msg nbindlist "normexp_unsafe_put_fields result nbindlist")
(return clocc nbindlist)
))))
(install_method class_source_unsafe_put_fields normal_exp normexp_unsafe_put_fields)
(install_method class_nrep_unsafe_put_fields get_ctype (lambda (recv env) ctype_void))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; normalize an PUT_FIELDS
(defun normexp_put_fields (recv env ncx psloc)
(assert_msg "check putfields recv" (is_a recv class_source_put_fields))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_put_fields recv")
(let ( (sloc (unsafe_get_field :loca_location recv))
(sobj (unsafe_get_field :suput_obj recv))
(sfields (unsafe_get_field :suput_fields recv))
(:long nbfields (multiple_length sfields))
(nfields (make_multiple discr_multiple nbfields))
(nbindlist (make_list discr_list))
(csym (clone_symbol 'putfld_))
(cbind (instance class_normal_let_binding
:binder csym
:letbind_loc sloc
:letbind_type ctype_void
;; letbind_expr filled later
:letbind_expr ()))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctype_void
:nocc_symb csym
:nocc_bind cbind))
(clabox (make_box discr_box ()))
)
(multicall
(nobj nobjbind)
(normal_exp sobj env ncx sloc)
(list_append2list nbindlist nobjbind)
(foreach_in_multiple
(sfields)
(fla :long ix)
(debug_msg fla "normexp_put_fields fla")
(debug_msg clabox "normexp_put_fields clabox")
(assert_msg "check fla" (is_a fla class_source_fieldassign))
(let ( (fld (unsafe_get_field :sfla_field fla))
(exp (unsafe_get_field :sfla_expr fla)) )
(assert_msg "check fld" (is_a fld class_field))
(let ( (fcla (unsafe_get_field :fld_ownclass fld))
(precla (box_content clabox))
)
(cond ( (null precla)
(box_put clabox fcla)
)
( (== precla fcla)
()
)
( (subclass_of precla fcla)
()
)
( (subclass_of fcla precla)
(box_put clabox fcla)
)
(:else
(debug_msg fld "normexp_put_fields bad fld")
(debug_msg fcla "normexp_put_fields bad fcla")
(debug_msg precla "normexp_put_fields bad precla")
(error_strv sloc "bad field name in PUT_FIELD" (unsafe_get_field :named_name fld))
(inform_strv sloc "PUT_FIELD expecting field of class" (unsafe_get_field :named_name precla))
(inform_strv sloc "PUT_FIELD got field of class" (get_field :named_name fcla))
)
)
)
(multicall
(nexp nexpbind)
(normal_exp exp env ncx sloc)
(list_append2list nbindlist nexpbind)
(let ( (nfla (instance class_nrep_fieldassign
:nrep_loc sloc
:nfla_field fld
:nfla_val nexp)) )
(multiple_put_nth nfields ix nfla)
))))
(let (
(ncla (normal_exp (box_content clabox) env ncx sloc))
(npuf (instance class_nrep_unsafe_put_fields
:nrep_loc sloc
:nuput_obj nobj
:nuput_fields nfields))
(nif (instance class_nrep_ifisa
:nrep_loc sloc
:nifa_val nobj
:nifa_class ncla
:nif_then npuf
:nexpr_ctyp ctype_void
)
))
(unsafe_put_fields cbind :letbind_expr nif)
(list_append nbindlist cbind)
(debug_msg clocc "normexp_put_fields result clocc")
(debug_msg nbindlist "normexp_put_fields result nbindlist")
(return clocc nbindlist)
))))
(install_method class_source_put_fields normal_exp normexp_put_fields)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; normalize a setq
(defun normexp_setq (recv env ncx psloc)
(assert_msg "check setq recv" (is_a recv class_source_setq))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp setq recv")
(let ( (sloc (unsafe_get_field :loca_location recv))
(svar (unsafe_get_field :sstq_var recv))
(sexp (unsafe_get_field :sstq_expr recv)) )
(assert_msg "check svar" (is_a svar class_symbol))
(let ( (nvar (normexp_symbol svar env ncx sloc))
(varctyp (get_ctype nvar env))
)
(debug_msg varctyp "normexp_setq varctyp")
(assert_msg "check varctyp" (is_a varctyp class_ctype))
(multicall
(nexp nbind)
(normal_exp sexp env ncx sloc)
(if (null nbind) (setq nbind (make_list discr_list)))
(let ( (expctyp (get_ctype nexp env))
)
(debug_msg expctyp "normexp_setq expctyp")
(assert_msg "check expctyp" (is_a expctyp class_ctype))
(if (!= varctyp expctyp)
(progn
(error_strv sloc "incompatible type for SETQ"
(unsafe_get_field :named_name svar))
(inform_strv sloc "left [var] type for incomatible SETQ"
(unsafe_get_field :named_name varctyp))
(inform_strv sloc "right [expr] type for incomatible SETQ"
(unsafe_get_field :named_name expctyp))
)))
(let ( (csym (clone_symbol 'setq_))
(cbind (instance class_normal_let_binding
:binder csym
:letbind_loc sloc
:letbind_type varctyp
:letbind_expr
(instance class_nrep_setq
:nrep_loc sloc
:nstq_var nvar
:nstq_exp nexp)))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp varctyp
:nocc_symb csym
:nocc_bind cbind))
)
(list_append nbind cbind)
(return clocc nbind)
)))))
(install_method class_source_setq normal_exp normexp_setq)
(install_method class_nrep_setq get_ctype
(lambda (recv env)
(get_ctype (unsafe_get_field :nstq_var recv) env)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; normalize a instance
(defun normexp_instance (recv env ncx psloc)
(assert_msg "check instance recv" (is_a recv class_source_instance))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_instance recv" )
(let ( (sloc (unsafe_get_field :loca_location recv))
(sclass (unsafe_get_field :smins_class recv))
(sclabind (unsafe_get_field :smins_clabind recv))
(sfields (unsafe_get_field :smins_fields recv))
(sclasym (if (is_a sclabind class_any_binding) (unsafe_get_field :binder sclabind)))
(cladata (if (is_a sclasym class_symbol)
(normal_exp sclasym env ncx sloc)))
(bindlist (make_list discr_list))
)
(if (not (is_a cladata class_nrep))
(progn
(error_strv sloc "invalid class in (INSTANCE ..)" (unsafe_get_field :named_name sclass))
(return ())))
(let ( (nfields
(multiple_map
sfields
(lambda (curflda :long curk)
(assert_msg "check curflda" (is_a curflda class_source_fieldassign))
(let ( (curfloc (unsafe_get_field :loca_location curflda))
(curfield (unsafe_get_field :sfla_field curflda))
(curexp (unsafe_get_field :sfla_expr curflda)) )
(if (null curfloc) (setq curfloc sloc))
(multicall
(nexp nbind)
(normal_exp curexp env ncx curfloc)
(assert_msg "check nbind" (is_list_or_null nbind))
(list_append2list bindlist nbind)
(instance class_nrep_fieldassign
:nrep_loc curfloc
:nfla_field curfield
:nfla_val nexp)
)))))
(nmkins
(instance class_nrep_instance
:nrep_loc sloc
:nmins_class sclass
:nmins_cladata cladata
:nmins_fields nfields))
(csym (clone_symbol 'inst_))
(cbind (instance class_normal_let_binding
:binder csym
:letbind_loc sloc
:letbind_type ctype_value
:letbind_expr nmkins))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctype_value
:nocc_symb csym
:nocc_bind cbind))
)
(list_append bindlist cbind)
(debug_msg clocc "normexp_instance result clocc")
(debug_msg bindlist "normexp_instance result bindlist")
(return clocc bindlist)
)
)
)
(install_method class_source_instance normal_exp normexp_instance)
(install_method class_source_instance get_ctype
(lambda (recv env) ctype_value))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; normalize a forever
(defun normexp_forever (recv env ncx psloc)
(assert_msg "check forever recv" (is_a recv class_source_forever))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_forever recv" )
(let ( (sloc (unsafe_get_field :loca_location recv))
(slbind (unsafe_get_field :slabel_bind recv))
(sbody (unsafe_get_field :sfrv_body recv))
(newenv (fresh_env env))
)
(assert_msg "check slbind" (is_a slbind class_label_binding))
(put_env newenv slbind)
(let ( (resy (clone_symbol (unsafe_get_field :binder slbind))) )
(debug_msg resy "normexp_forever putting resy..")
(debug_msg slbind "..normexp_forever in slbind")
(assert_msg "check resy obj1" (is_object resy))
(unsafe_put_fields slbind :labind_clonsy resy)
(debug_msg slbind "normexp_forever updated slbind")
(debug_msg resy "normexp_forever clonsy put resy")
(assert_msg "check resy" (is_a resy class_cloned_symbol))
(debug_msg (unsafe_get_field :labind_clonsy slbind) "normexp_forever got1 clonsy")
(assert_msg "check did1 put resy" (== (unsafe_get_field :labind_clonsy slbind) resy))
(assert_msg "check resy obj2" (is_object resy))
(multicall
(nbody nbodbindings)
(normalize_tuple sbody newenv ncx sloc)
(debug_msg slbind "normexp_forever again slbind")
(assert_msg "check did2 put resy" (== (unsafe_get_field :labind_clonsy slbind) resy))
(debug_msg (unsafe_get_field :labind_clonsy slbind) "normexp_forever got2 clonsy")
(assert_msg "check size slbind" (<i (get_int labind_clonsy) (object_length slbind)))
(let (
(resbody (tuple
(wrap_normal_letseq nbody nbodbindings sloc)))
(csym (clone_symbol 'forever_))
(nforever (instance class_nrep_forever
:nrep_loc sloc
:nforever_bind slbind
:nforever_body resbody
:nforever_result resy))
(cbind (instance class_normal_let_binding
:binder csym
:letbind_loc sloc
:letbind_type ctype_value
:letbind_expr nforever))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctype_value
:nocc_symb csym
:nocc_bind cbind))
(nforbindings (make_list discr_list))
)
(list_append nforbindings cbind)
(debug_msg clocc "normexp_forever return nforever")
(debug_msg clocc "normexp_forever return nforever")
(return clocc nforbindings)
)
))))
(install_method class_source_forever normal_exp normexp_forever)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; normalize an exit
;;; the normalization of an exit is a local variable of ctype_void
;;; this enables detection of applications like (foo (exit looplab))
(defun normexp_exit (recv env ncx psloc)
(assert_msg "check exit recv" (is_a recv class_source_exit))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_exit recv")
(let ( (sloc (unsafe_get_field :loca_location recv))
(slbind (unsafe_get_field :slabel_bind recv))
(sbody (unsafe_get_field :sexi_body recv))
(newenv (fresh_env env))
)
(assert_msg "check slbind" (is_a slbind class_label_binding))
(put_env newenv slbind)
(multicall
(nbody nbodbindings)
(normalize_tuple sbody newenv ncx sloc)
(if (null nbodbindings)
(setq nbodbindings (make_list discr_list)))
;; the only interesting value of nbody is the last one
(let ( (nexit (instance class_nrep_exit
:nrep_loc sloc
:nexit_bind slbind
:nexit_val (multiple_nth nbody (-i (multiple_length nbody) 1))))
(csym (clone_symbol 'exit_))
(cbind (instance class_normal_let_binding
:binder csym
:letbind_loc sloc
:letbind_type ctype_void
:letbind_expr nexit))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctype_void
:nocc_symb csym
:nocc_bind cbind))
)
(list_append nbodbindings cbind)
(debug_msg nexit "normexp_exit nexit")
(debug_msg nbodbindings "normexp_exit nbodbindings")
(return clocc nbodbindings)
))))
(install_method class_source_exit normal_exp normexp_exit)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; normalize the compile_warning
(defun normexp_compilewarning (recv env ncx psloc)
(assert_msg "check compilewarn recv" (is_a recv class_source_compilewarning))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_compile_warning recv")
(let ((sloc (unsafe_get_field :loca_location recv))
(swmsg (unsafe_get_field :scwarn_msg recv))
(swexp (unsafe_get_field :scwarn_expr recv))
)
(warning_strv sloc "COMPILE_WARNING:" swmsg)
(multicall
(nexp nbind)
(normal_exp swexp env ncx sloc)
(debug_msg nexp "normexp_compile_warning nesult nexp")
(debug_msg nbind "normexp_compile_warning result nbind")
(return nexp nbind)
)))
(Install_method class_source_compilewarning normal_exp normexp_compilewarning)
(install_method class_source_compilewarning get_ctype
(lambda (recv env) (get_ctype (unsafe_get_field :scwarn_expr recv) env)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; replace in a tupe of normalized stuff the last with a return
;;;;;; for normalization of defun-s or lambda-s
(defun replace_last_by_return (tup env sloc)
(assert_msg "check tup" (is_multiple tup))
(assert_msg "check env" (is_a env class_environment))
(let ( (:long tuplen (multiple_length tup))
(lastcomp (if (>i tuplen 0) (multiple_nth tup (-i tuplen 1)))) )
(cond
;;; last expression is already a return - do nothing
( (is_a lastcomp class_nrep_return)
(return tup)
)
;;; last expression is a symbol occurrence (closed or local) - return it if it is a value
( (is_a lastcomp class_nrep_symocc)
(if (== (unsafe_get_field :nocc_ctyp lastcomp) ctype_value)
(multiple_put_nth
tup (-i tuplen 1)
(instance class_nrep_return
:nrep_loc sloc
:nret_main lastcomp)))
(return tup)
)
;;; last expression is a normal data, return it
( (is_a lastcomp class_nrep_data)
(multiple_put_nth
tup (-i tuplen 1)
(instance class_nrep_return
:nrep_loc sloc
:nret_main lastcomp))
(return tup)
)
;;; no last expression - don't bother to return
( (null lastcomp)
(return tup)
)
;;; last expression is a normalized let, recurse on the body within a new env
( (is_a lastcomp class_nrep_let)
(let ( (lbody (unsafe_get_field :nlet_body lastcomp))
(lbinding (unsafe_get_field :nlet_bindings lastcomp))
(lloc (unsafe_get_field :nrep_loc lastcomp))
(newenv (fresh_env env))
)
(multiple_every
lbinding
(lambda (bnd :long ix)
(put_env newenv bnd)
))
(if (is_multiple lbody)
(replace_last_by_return lbody newenv lloc))
(return tup)
))
;;; last expression is some more complex normalized stuff
;;; if it is a value wrap it into a normalized let with return
( (is_a lastcomp class_nrep)
(let ( (lastyp (get_ctype lastcomp env))
(loc (unsafe_get_field :nrep_loc lastcomp))
)
(if (== lastyp ctype_value)
(let ( (rclosym (clone_symbol '_retval_))
(rclocc (instance class_nrep_locsymocc
:nrep_loc loc
:nocc_symb rclosym
:nocc_ctyp ctype_value))
(retn (instance class_nrep_return
:nrep_loc loc
:nret_main rclocc
))
(rbind (instance class_normal_let_binding
:binder rclosym
:letbind_type ctype_value
:letbind_expr lastcomp
:letbind_loc loc
))
(rbintup (tuple rbind))
(rlet (instance class_nrep_let
:nrep_loc loc
:nlet_bindings rbintup
:nlet_body (tuple retn)))
)
(unsafe_put_fields rclocc :nocc_bind rbind)
(multiple_put_nth
tup (-i tuplen 1)
rlet)
(return tup)
)))))
(return tup) ; returns the original tuple
;;; general case, do nothing
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; normalize a DEFUN
(defun normexp_defun (recv env ncx psloc)
(assert_msg "check defun recv" (is_a recv class_source_defun))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_defun recv")
(let ( (sloc (unsafe_get_field :loca_location recv))
(snam (unsafe_get_field :sdef_name recv))
(sformals (unsafe_get_field :sformal_args recv))
(sbody (unsafe_get_field :sfun_body recv))
(sfubind (find_env env snam))
(newenv (fresh_env env))
(oldproc (unsafe_get_field :nctx_curproc ncx))
(oldsymbcache (unsafe_get_field :nctx_symbcachemap ncx))
(closblis (make_list discr_list))
(nproc (instance class_nrep_defunroutproc
;;; dont forget to put the nil fields at end
:nrep_loc sloc
:nproc_body () ;filled later
:nrpro_name snam
:nrpro_argb sformals
:nrpro_closedb closblis
:nrpro_const (make_list discr_list)
:nrpro_thunklist (make_list discr_list)
:nrpro_datarout () ; filled below
:nrpro_dataclos () ; filled below
))
(ndatarout (instance class_nrep_dataroutine
:ndata_name snam
:ndata_discrx (normal_predef discr_routine ncx sloc "discr_routine")
:ndrou_proc nproc
))
(ndataclos (instance class_nrep_dataclosure
:ndata_name snam
:ndata_discrx (normal_predef discr_closure ncx sloc "discr_closure")
:ndclo_proc nproc
:ndclo_closv () ;filled below
))
)
(unsafe_put_fields ncx
:nctx_curproc nproc
:nctx_symbcachemap (make_mapobject discr_map_objects 40))
(add_nctx_data ncx ndatarout)
(add_nctx_data ncx ndataclos)
(assert_msg "check sfubind" (is_a sfubind class_function_binding))
(unsafe_put_fields nproc :nrpro_datarout ndatarout :nrpro_dataclos ndataclos)
(multiple_every
sformals
(lambda (fbi :long ix)
(assert_msg "check fbi" (is_a fbi class_formal_binding))
(put_env newenv fbi)))
(unsafe_put_fields newenv :env_proc nproc)
(debug_msg newenv "normexp_defun updated :env_proc of of newenv")
;; add nproc into ncx
(list_append (unsafe_get_field :nctx_proclist ncx) nproc)
(multicall
(nbody nbindings)
(normalize_tuple sbody newenv ncx sloc)
(debug_msg nbody "normexp_defun nbody before replace_last_by_return")
(multiple_every
nbindings
(lambda (nbi :long ix)
(put_env newenv nbi)))
(let ( (nrbody (replace_last_by_return nbody newenv sloc))
(npbody (wrap_normal_letseq nrbody nbindings sloc))
)
(unsafe_put_fields nproc :nproc_body npbody)
(debug_msg npbody "normexp_defun npbody after replace_last_by_return")
)
(unsafe_put_fields ncx :nctx_curproc oldproc :nctx_symbcachemap oldsymbcache)
(let ( (clovtup
(list_to_multiple
closblis
discr_multiple
(lambda (bnd)
(assert_msg "normexp_defun check bnd" (is_a bnd class_any_binding))
(let ( (sy (unsafe_get_field :binder bnd))
;; since sy is a symbol, its normalized form does not add any binding
;; we normalize it in the *old* environment, not the new one
(nsy (normal_exp sy env ncx sloc))
)
nsy
)))) )
(unsafe_put_fields ndataclos :ndclo_closv clovtup)
)
(unsafe_put_fields sfubind
:fixbind_data ndataclos
)
(debug_msg nproc "normexp_defun return nproc")
(return nproc ())
)
))
(install_method class_source_defun normal_exp normexp_defun)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; normalize a LAMBDA
;;; an internal routine useful for lambda inside letrec ... to share
;;; code between normexp_lambda and handling of lambda-s inside letrec
(defun normalize_lambda (recv env newenv ncx psloc)
(assert_msg "check lambda recv" (is_a recv class_source_lambda))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check newenv" (is_a newenv class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normalize_lambda recv")
(debug_msg env "normalize_lambda env")
(debug_msg newenv "normalize_lambda newenv")
(let ( (sloc (unsafe_get_field :loca_location recv))
(sargs (unsafe_get_field :slam_argbind recv))
(sbody (unsafe_get_field :slam_body recv))
(csym (clone_symbol 'lambda_))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctype_value
:nocc_symb csym))
(oldproc (unsafe_get_field :nctx_curproc ncx))
(savedcachemap (unsafe_get_field :nctx_symbcachemap ncx))
(closedblist (make_list discr_list))
(nproc (instance class_nrep_lambdaroutproc
:nrep_loc sloc
:nproc_body () ;filled later
:nrpro_name csym
:nrpro_argb sargs
:nrpro_closedb closedblist
:nrpro_const (make_list discr_list)
:nrpro_datarout () ; filled below
:nrpro_dataclos () ; not filled
:nrpro_thunklist (make_list discr_list)
))
(ndatarout (instance class_nrep_dataroutine
:ndata_name csym
:ndata_discrx (normal_predef discr_routine ncx sloc "discr_routine")
:ndrou_proc nproc
))
)
(add_nctx_data ncx ndatarout)
;; update the context for the new proc & a fresh symbol cache map
(unsafe_put_fields ncx
:nctx_curproc nproc
:nctx_symbcachemap (make_mapobject discr_map_objects 40))
(unsafe_put_fields nproc :nrpro_datarout ndatarout)
(multiple_every
sargs
(lambda (fbi :long ix)
(assert_msg "check fbi" (is_a fbi class_formal_binding))
(put_env newenv fbi)))
(unsafe_put_fields newenv :env_proc nproc)
(debug_msg newenv "normalize_lambda updated :env_proc of newenv")
(shortbacktrace_dbg "normalize_lambda" 15)
;; add nproc into ncx
(list_append (unsafe_get_field :nctx_proclist ncx) nproc)
(multicall
(nbody nbindings)
(normalize_tuple sbody newenv ncx sloc)
(multiple_every
nbindings
(lambda (nbi :long ix)
(put_env newenv nbi)))
(unsafe_put_fields
nproc
:nproc_body (wrap_normal_letseq (replace_last_by_return nbody newenv sloc) nbindings sloc)
)
;; restore the previous symbol cache map & the old proc and return the normalized lambda
(unsafe_put_fields ncx
:nctx_symbcachemap savedcachemap
:nctx_curproc oldproc
)
(let (
;; we make an anonymous constant for the routine unless in toplevel
(:long insideflag (is_a oldproc class_nrep_routproc))
(krout (if insideflag
(instance class_nrep_constant
:nrep_loc sloc
:nconst_sval recv
:nconst_data ndatarout
:nconst_proc oldproc)))
(clovtup
(list_to_multiple closedblist
discr_multiple
(lambda (bnd)
(assert_msg "normalize_lambda check bnd" (is_a bnd class_any_binding))
(let ( (sy (unsafe_get_field :binder bnd))
;; since sy is a symbol, its normalized form does not add any binding
;; we normalize it in the *old* environment, not the new one
(nsy (normal_exp sy env ncx sloc))
)
nsy))))
(constrout (if insideflag krout ndatarout))
)
(return nproc csym clocc constrout clovtup sloc oldproc ndatarout insideflag)
))))
;;;;
(defun normexp_lambda (recv env ncx psloc)
(assert_msg "check lambda recv" (is_a recv class_source_lambda))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_lambda recv")
(let (
(newenv (fresh_env env))
)
(multicall
(nproc csym clocc constrout clovtup sloc oldproc ndatarout :long insideflag)
(normalize_lambda recv env newenv ncx psloc)
(debug_msg nproc "normexp_lambda after normalize_lambda nproc")
(let (
(nlambda (instance class_nrep_lambda
:nrep_loc sloc
:nlambda_proc nproc
:nlambda_constrout constrout
:nlambda_closedv clovtup
))
(cbind (instance class_normal_let_binding
:letbind_loc sloc
:binder csym
:letbind_type ctype_value
:letbind_expr nlambda))
(nbindlist (make_list discr_list))
)
(unsafe_put_fields clocc :nocc_bind cbind)
(list_append nbindlist cbind)
(if insideflag
(list_append (unsafe_get_field :nrpro_const oldproc) ndatarout))
(debug_msg clocc "normexp_lambda return clocc")
(debug_msg nbindlist "normexp_lambda return nbindlist")
(return clocc nbindlist)
))))
(install_method class_source_lambda normal_exp normexp_lambda)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun normexp_multicall (recv env ncx psloc)
(assert_msg "check multicall recv" (is_a recv class_source_multicall))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_multicall recv")
(let ( (sloc (unsafe_get_field :loca_location recv))
(sresbind (unsafe_get_field :smulc_resbind recv))
(scall (unsafe_get_field :smulc_call recv))
(sbody (unsafe_get_field :smulc_body recv))
(newenv (fresh_env env))
)
(debug_msg scall "normexp_multicall scall")
(multicall
(ncall ncallbindings)
(normal_exp scall env ncx sloc)
(debug_msg ncall "normexp_multicall ncall")
(debug_msg ncallbindings "normexp_multicall ncallbindings")
;;; since ncall is normalized, it is a class_nrep_locsymocc and
;;; the last binding in ncallbindings is a class_normal_let_binding
;;; whose binder is the nocc_symb of the ncall
(assert_msg "normexp_multicall check ncall" (is_a ncall class_nrep_locsymocc))
(let ( (ncallsym (unsafe_get_field :nocc_symb ncall))
(nboxcall (make_box discr_box ())) ;box to contain the real normalized call
(nrealcall ()) ;the real call
(nincallbindings (make_list discr_list)) ;list of internal bindings to the call
)
(list_iterate_test ;loop exited when cbnd is for ncallsym
ncallbindings
(lambda (cbnd)
(assert_msg "normexp_multicall check cbnd" (is_a cbnd class_normal_let_binding))
(if (== ncallsym (unsafe_get_field :binder cbnd))
(let ( (nrealcallex (unsafe_get_field :letbind_expr cbnd)) )
(box_put nboxcall nrealcallex)
())
(progn (list_append nincallbindings cbnd) cbnd))))
(setq nrealcall (box_content nboxcall))
(debug_msg nrealcall "normexp_multicall got nrealcall")
(multiple_every
sresbind
(lambda (bnd :long ix)
(put_env newenv bnd)))
(multicall
(nbody nbodybindings)
(normalize_tuple sbody newenv ncx sloc)
(debug_msg nbody "normexp_multicall nbody")
(debug_msg nbodybindings "normexp_multicall nbodybindings")
(let ( (wnbodylet (wrap_normal_letseq nbody nbodybindings sloc)) )
;;; remove every locally bound symbol from the symbol cache map
(let ( (sycmap (unsafe_get_field :nctx_symbcachemap ncx)) )
(multiple_every sresbind
(lambda (bnd)
(mapobject_remove sycmap (unsafe_get_field :binder bnd))
)))
(let (
(lastnbody (multiple_nth nbody -1))
(lastntype (if lastnbody (get_ctype lastnbody newenv) ctype_void))
(csym (clone_symbol 'multi_))
(cbind (instance class_normal_let_binding
:binder csym
:letbind_loc sloc
:letbind_type lastntype
; :letbind_expr filled below
))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp lastntype
:nocc_symb csym
:nocc_bind cbind))
)
;;; handle differently apply & sends
(cond
( (is_a scall class_source_apply)
(assert_msg "normexp_multicall check nrealcall apply" (is_a nrealcall class_nrep_apply))
(let ( (nres (instance class_nrep_multiapply
:nrep_loc sloc
:napp_fun (unsafe_get_field :napp_fun nrealcall)
:nexpr_args (unsafe_get_field :nexpr_args nrealcall)
:nexpr_ctyp lastntype
:nmulapp_bindings sresbind
:nmulapp_body wnbodylet)) )
(unsafe_put_fields cbind :letbind_expr nres)
(list_append nincallbindings cbind)
(debug_msg clocc "normexp_multicall multiapply result clocc")
(debug_msg nincallbindings "normexp_multicall multiapply result nincallbindings")
(return clocc nincallbindings)
)
)
( (is_a scall class_source_msend)
(debug_msg nrealcall "normexp_multicall multicall nrealcall")
(compile_warning "in normexp_multicall we should check against the formals of the selector if available")
(assert_msg "normexp_multicall check nrealcall send" (is_a nrealcall class_nrep_msend))
(let ( (nrecv (get_field :nsend_recv nrealcall))
(nres (instance class_nrep_multimsend
:nrep_loc sloc
:nexpr_ctyp lastntype
:nsend_sel (unsafe_get_field :nsend_sel nrealcall)
:nsend_recv nrecv
:nexpr_args (unsafe_get_field :nexpr_args nrealcall)
:nmulsend_bindings sresbind
:nmulsend_body wnbodylet))
)
(debug_msg nrecv "normexp_multicall nrecv from multicall")
(assert_msg "check nrecv" (is_object nrecv))
(unsafe_put_fields cbind :letbind_expr nres)
(list_append nincallbindings cbind)
(debug_msg clocc "normexp_multicall multisend result clocc")
(debug_msg nincallbindings "normexp_multicall multisend result nincallbindings")
(return clocc nincallbindings)
)
)
( :else
(error_plain sloc "multi-called expression neither apply nor send")
(return ())
)
))))))))
(install_method class_source_multicall normal_exp normexp_multicall)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun normexp_tuple (recv env ncx psloc)
(assert_msg "check tuple recv" (is_a recv class_source_tuple))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_tuple recv")
(let ( (sloc (unsafe_get_field :loca_location recv))
(sargs (unsafe_get_field :sargop_args recv))
(csymrec (clone_symbol 'tuplrec_))
(csymtup (clone_symbol 'tuple_))
(normdiscrmult (normal_predef discr_multiple ncx sloc "discr_multiple"))
)
(debug_msg csymrec "normexp_tuple csymrec")
(multicall
(nargs nbindings)
(normalize_tuple sargs env ncx sloc)
(debug_msg nargs "normexp_tuple nargs")
(debug_msg nbindings "normexp_tuple nbindings")
;; insight: normalize (tuple x1 x2) exactly as
;; as an anonymous letrec
(letrec (
(constupbind (instance class_normal_constructed_tuple_binding
:binder csymrec
:nconsb_loc sloc
:nconsb_discr normdiscrmult
:nconsb_nletrec nletrec
:ntupb_comp nargs
))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctype_value
:nocc_symb csymrec
:nocc_bind constupbind))
(tup1bind (tuple constupbind))
(tup1loc (tuple clocc))
(nbdy (tuple clocc))
(nletrec (instance class_nrep_letrec
:nrep_loc sloc
:nlet_bindings tup1bind
:nlet_body nbdy
:nletrec_fill_bindings ()
:nletrec_body_bindings ()
;; perhaps we should avoid having tup1loc to share the same location
;; for the letrec and the tuple result?
:nletrec_locsyms tup1loc
))
(ctupbind (instance class_normal_let_binding
:binder csymtup
:letbind_loc sloc
:letbind_type ctype_value
:letbind_expr nletrec))
(ctuplocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctype_value
:nocc_symb csymtup
:nocc_bind ctupbind))
(ctupbindlist (list ctupbind))
)
(if (null nbindings)
(setq nbindings ctupbindlist)
(list_append nbindings ctupbind)
)
(debug_msg ctupbind "normexp_tuple ctupbind")
(debug_msg clocc "normexp_tuple clocc")
(debug_msg nbdy "normexp_tuple nbdy")
(debug_msg ctuplocc "normexp_tuple return ctuplocc")
(debug_msg nbindings "normexp_tuple return final nbindings")
(return ctuplocc nbindings)
)
;;
)))
(install_method class_source_tuple normal_exp normexp_tuple)
;;;;;;;;;;;;;;;;
(defun normexp_list (recv env ncx psloc)
(assert_msg "check list recv" (is_a recv class_source_list))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_list recv")
(let ( (sloc (or (unsafe_get_field :loca_location recv) psloc))
(sargs (unsafe_get_field :sargop_args recv))
(csymrec (clone_symbol 'listrec_))
(csymlist (clone_symbol 'list_))
(normdiscrpair (normal_predef discr_pair ncx sloc "discr_pair"))
(normdiscrlist (normal_predef discr_pair ncx sloc "discr_list"))
(:long nbargs (multiple_length sargs))
(consbindtup (list_to_multiple (prepare_constructor_binding recv csymlist ncx sloc)))
(resbindings (make_list discr_list))
(conslistbind (multiple_nth consbindtup -1))
(tuprecloc (multiple_map
consbindtup
(lambda (curbind :long bindix)
(debug_msg curbind "normexp_list curbind")
(assert_msg "check curbind" (is_a curbind class_normal_constructor_binding))
(instance class_nrep_locsymocc
:nrep_loc (or (get_field :nconsb_loc curbind) sloc)
:nocc_ctyp ctype_value
:nocc_symb (get_field :binder curbind)
:nocc_bind curbind)
)
))
)
(debug_msg csymrec "normexp_list csymrec")
(debug_msg consbindtup "normexp_list consbindtup")
(debug_msg conslistbind "normexp_list conslistbind")
(debug_msg tuprecloc "normexp_list tuprecloc")
(assert_msg "check conslistbind" (is_a conslistbind class_normal_constructed_list_binding))
(multicall
(nargs nbindings)
(normalize_tuple sargs env ncx sloc)
(debug_msg nargs "normexp_list nargs")
(debug_msg nbindings "normexp_list nbindings")
(assert_msg "check nbindings" (is_list_or_null nbindings))
(list_append2list resbindings nbindings)
(letrec (
(creclocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctype_value
:nocc_symb csymrec
:nocc_bind conslistbind))
(nbdy (tuple creclocc))
(nletrec (instance class_nrep_letrec
:nrep_loc sloc
:nlet_bindings consbindtup
:nlet_body nbdy
:nletrec_fill_bindings ()
:nletrec_body_bindings ()
:nletrec_locsyms tuprecloc
))
(clistbind (instance class_normal_let_binding
:binder csymlist
:letbind_loc sloc
:letbind_type ctype_value
:letbind_expr nletrec))
(clistlocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctype_value
:nocc_symb csymlist
:nocc_bind clistbind))
(clistbindlist (list clistbind))
)
(foreach_in_multiple
(nargs)
(curnarg :long nix)
(let ( (curpairb (multiple_nth consbindtup nix))
(nextb (multiple_nth consbindtup (+i nix 1)))
)
(put_int curpairb nix)
(debug_msg curpairb "normexp_list curpairb")
(assert_msg "check curpairb" (is_a curpairb class_normal_constructed_pair_binding))
(assert_msg "check curpairb objnum" (==i (get_int curpairb) nix))
(debug_msg curnarg "normexp_list curnarg")
(debug_msg nextb "normexp_list nextb")
(assert_msg "check nextb" (is_a nextb class_normal_constructor_binding))
(let ( (nextsym (get_field :binder nextb))
(nextloc (if (is_a nextb class_normal_constructed_pair_binding)
(nreclist_find_locsym nextsym nletrec)))
)
(put_fields curpairb
:npairb_head curnarg :npairb_tail nextloc
:nconsb_nletrec nletrec)
(debug_msg curpairb "normexp_list updated curpairb")
)
))
;;
;; update the list binding
(let ( (firstpairb (multiple_nth consbindtup 0))
(lastpairb (multiple_nth consbindtup -2))
(firstpairsymb
(if
(is_a firstpairb class_normal_constructed_pair_binding)
(unsafe_get_field :binder firstpairb)))
(lastpairsymb
(if
(is_a lastpairb class_normal_constructed_pair_binding)
(unsafe_get_field :binder lastpairb)))
(firstpairloc
(if firstpairsymb
(nreclist_find_locsym firstpairsymb nletrec)))
(lastpairloc
(if lastpairsymb
(nreclist_find_locsym lastpairsymb nletrec)))
)
(put_fields
conslistbind
:nlistb_first firstpairloc :nlistb_last lastpairloc
:nconsb_nletrec nletrec)
(put_int conslistbind nbargs)
(debug_msg conslistbind "normexp_list updated conslistbind")
)
;;
(if (null nbindings)
(setq nbindings clistbindlist)
(list_append nbindings clistbind)
)
(debug_msg nletrec "normexp_list final nletrec")
(debug_msg clistlocc "normexp_list final clistlocc")
(debug_msg nbindings "normexp_list final nbindings")
(compile_warning "normexp_list is probably still buggy!!")
(return clistlocc nbindings)
))))
(install_method class_source_list normal_exp normexp_list)
;;;;;;;;;;;;;;;;
(defselector prepare_constructor_binding class_selector
:doc #{The $PREPARE_CONSTRUCTOR_BINDING selector applied to: an
constructible expression $RECV, a symbol $SYMB, a normalization
context $NCX, a source location $SLOC gives a half-filled instance
of a sub-class of $CLASS_NORMAL_CONSTRUCTOR_BINDING.}#
:formals (recv symb ncx sloc)
)
(defun badmeth_prepare_constructor_binding (recv symb ncx sloc)
(debug_msg recv "bad_prepare_constructor_binding recv")
(debug_msg symb "bad_prepare_constructor_binding symb")
(error_strv sloc "invalid constructor binding" (get_field :named_name symb))
(error_strv sloc "reciever unprepared to constructor binding"
(get_field :named_name (discrim recv)))
(assert_msg "@$@badmeth_prepare_constructor_binding" ())
)
(install_method discr_any_receiver prepare_constructor_binding badmeth_prepare_constructor_binding)
;;;;;;;;;;;;;;;;
(defselector normal_letrec_constructive class_selector
:doc #{The $NORMAL_LETREC selector applied to: a constructive
expression $CEXPR for reciever, a symbol $SYMB, a normal
constructive binding $CBIND, an environment $ENV, a normal context
$NCX, a location $SLOC}#
:formals (cexpr symb cbind env ncx sloc))
(defun badmeth_normal_letrec_constructive (cexpr symb cbind env ncx sloc)
(debug_msg cexpr "badmeth_normal_letrec_constructive cexpr")
(debug_msg symb "badmeth_normal_letrec_constructive symb")
(debug_msg cbind "badmeth_normal_letrec_constructive cbind")
(error_strv sloc "invalid letrec constructive symbol" (get_field :named_name symb))
(error_strv sloc "reciever unprepared to normal_letrec_constructive"
(get_field :named_name (discrim cexpr)))
(assert_msg "@$@badmeth_normal_letrec_constructive" ())
)
(install_method discr_any_receiver normal_letrec_constructive badmeth_normal_letrec_constructive)
;;;;;;;;;;;;;;;;
(defun prepcons_lambda (recv symb ncx sloc)
(debug_msg recv "prepcons_lambda recv")
(debug_msg symb "prepcons_lambda symb")
(assert_msg "check recv" (is_a recv class_source_lambda))
(let ( (conslam (instance class_normal_constructed_lambda_binding
:binder symb
:nconsb_loc sloc
:nconsb_discr (normal_predef discr_closure ncx sloc "discr_closure")
))
)
(debug_msg conslam "prepcons_lambda gives conslam")
(return conslam)
))
(install_method class_source_lambda prepare_constructor_binding prepcons_lambda)
;;;;
(defun normletrec_lambda (cexpr symb cbind env ncx psloc)
(debug_msg cexpr "normletrec_lambda cexpr")
(debug_msg symb "normletrec_lambda symb")
(debug_msg cbind "normletrec_lambda cbind")
(debug_msg env "normletrec_lambda env")
(assert_msg "check cbind" (is_a cbind class_normal_constructed_lambda_binding))
(let ( (newenv (fresh_env env))
)
(debug_msg newenv "normletrec_lambda newenv")
(assert_msg "check newenv" (is_a newenv class_environment))
(multicall
(nproc csym clocc constrout clovtup sloc oldproc ndatarout :long insideflag)
(normalize_lambda cexpr newenv newenv ncx psloc)
;;was (normalize_lambda cexpr env newenv ncx psloc)
(debug_msg csym "normletrec_lambda after normalize_lambda csym")
(debug_msg clocc "normletrec_lambda after normalize_lambda clocc")
(debug_msg constrout "normletrec_lambda after normalize_lambda constrout")
(debug_msg clovtup "normletrec_lambda after normalize_lambda clovtup")
(debug_msg oldproc "normletrec_lambda after normalize_lambda oldproc")
(debug_msg ndatarout "normletrec_lambda after normalize_lambda ndatarout")
(if insideflag
(list_append (unsafe_get_field :nrpro_const oldproc) ndatarout))
(put_fields cbind
:nlambdab_nclosed clovtup
:nlambdab_constrout constrout
:nlambdab_datarout ndatarout)
(debug_msg cbind "normletrec_lambda updated cbind")
(compile_warning "unimplemented normletrec_lambda, maybe store the newenv in the cbind")
(shortbacktrace_dbg "normletrec_lambda ended" 15)
)))
(install_method class_source_lambda normal_letrec_constructive normletrec_lambda)
;;;;;;;;;;;;;;;;
(defun prepcons_tuple (recv symb ncx sloc)
(debug_msg recv "prepcons_tuple recv")
(debug_msg symb "prepcons_tuple symb")
(assert_msg "check recv" (is_a recv class_source_tuple))
(let (
(loc (get_field :loca_location recv))
(tuparg (get_field :sargop_args recv))
(:long nbtuparg (multiple_length tuparg))
(ntup (make_multiple discr_multiple nbtuparg))
(constup (instance class_normal_constructed_tuple_binding
:binder symb
:nconsb_loc (or loc sloc)
:nconsb_discr (normal_predef discr_multiple ncx sloc "discr_multiple")
:ntupb_comp ntup
))
)
(debug_msg constup "prepcons_tuple gives constup")
(return constup)
))
(install_method class_source_tuple prepare_constructor_binding prepcons_tuple)
;;;;
(defun normletrec_tuple (cexpr symb cbind env ncx sloc)
(debug_msg cexpr "normletrec_tuple cexpr")
(debug_msg symb "normletrec_tuple symb")
(debug_msg cbind "normletrec_tuple cbind")
(assert_msg "check cexpr" (is_a cexpr class_source_tuple))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check cbind" (is_a cbind class_normal_constructed_tuple_binding))
(let (
(loc (or (get_field :loca_location cexpr) sloc))
(sargs (unsafe_get_field :sargop_args cexpr))
(bcomp (get_field :ntupb_comp cbind))
(nletrec (get_field :nconsb_nletrec cbind))
(nrecbinds (get_field :nletrec_fill_bindings nletrec))
)
(debug_msg sargs "normletrec_tuple sargs")
(debug_msg bcomp "normletrec_tuple bcomp")
(debug_msg nletrec "normletrec_tuple nletrec")
(debug_msg nrecbinds "normletrec_tuple nrecbinds")
(assert_msg "check nletrec" (is_a nletrec class_nrep_letrec))
(assert_msg "check nrecbinds" (is_list nrecbinds))
(multicall
(nargs nbinds)
(normalize_tuple sargs env ncx loc)
(debug_msg nargs "normletrec_tuple nargs")
(debug_msg nbinds "normletrec_tuple nbinds")
(assert_msg "check nargs & bcomp samelength" (==i (multiple_length bcomp) (multiple_length nargs)))
(assert_msg "check nbinds" (is_list_or_null nbinds))
(foreach_in_multiple
(nargs)
(curnarg :long nix)
(multiple_put_nth bcomp nix curnarg))
(debug_msg bcomp "normletrec_tuple updated bcomp")
(list_append2list nrecbinds nbinds)
(debug_msg nrecbinds "normletrec_tuple updated nrecbinds")
(shortbacktrace_dbg "normletrec_tuple ended" 15)
)))
(install_method class_source_tuple normal_letrec_constructive normletrec_tuple)
;;;;;;;;;;;;;;;;
(defun prepcons_list (recv symb ncx sloc)
(debug_msg recv "prepcons_list recv")
(debug_msg symb "prepcons_list symb")
(assert_msg "check recv" (is_a recv class_source_list))
(let (
(reslist (make_list discr_list))
(loc (get_field :loca_location recv))
(listarg (get_field :sargop_args recv))
(:long nblistarg (multiple_length listarg))
(pairsb (make_multiple discr_multiple nblistarg))
(conslist (instance class_normal_constructed_list_binding
:binder symb
:nconsb_loc (or loc sloc)
:nconsb_discr (normal_predef discr_list ncx sloc "discr_list")
:nlistb_first ()
:nlistb_last ()
:nlistb_pairsb pairsb
))
)
(foreach_in_multiple
(listarg)
(curarg :long curix)
(debug_msg curarg "prepcons_list curarg")
(let ( (pairsymb (clone_symbol (if (is_a curarg class_named) curarg '_pairoflist)))
(conspair (instance class_normal_constructed_pair_binding
:binder pairsymb
:nconsb_loc (or (get_field :loca_location curarg) loc sloc)
:nconsb_discr (normal_predef discr_pair ncx sloc "discr_pair")
:npairb_head ()
:npairb_tail ()
))
)
(multiple_put_nth pairsb curix conspair)
(list_append reslist conspair)
))
(list_append reslist conslist)
(debug_msg reslist "prepcons_list gives reslist")
(return reslist)
))
(install_method class_source_list prepare_constructor_binding prepcons_list)
;;;;
;; auxiliary function to find a symbol in the locsym of a nletrec
(defun nreclist_find_locsym (symb nletrec)
(debug_msg symb "nreclist_find_locsym symb")
(assert_msg "check symb" (is_a symb class_symbol))
(assert_msg "check nletrec" (is_a nletrec class_nrep_letrec))
(let ( (nlocsyms (get_field :nletrec_locsyms nletrec))
)
(foreach_in_multiple
(nlocsyms)
(curlocsym :long locsymix)
(if (== (get_field :nocc_symb curlocsym) symb)
(progn
(debug_msg curlocsym "nreclist_find_locsym found curlocsym")
(return curlocsym))))
(debug_msg () "nreclist_find_locsym not found")
(return)
))
(defun normletrec_list (cexpr symb cbind env ncx sloc)
(debug_msg cexpr "normletrec_list cexpr")
(debug_msg symb "normletrec_list symb")
(debug_msg cbind "normletrec_list cbind")
(assert_msg "check cexpr" (is_a cexpr class_source_list))
;; here cbind is a tuple of constructor bindings -for all the pairs
;; & the list
(assert_msg "check cbind is tuple" (is_multiple cbind))
(let ( (loc (or (get_field :loca_location cexpr) sloc))
(sargs (get_field :sargop_args cexpr))
(conslibind (multiple_nth cbind -1))
(nletrec (get_field :nconsb_nletrec conslibind))
(nrecbinds (get_field :nletrec_fill_bindings nletrec))
(nlocsyms (get_field :nletrec_locsyms nletrec))
(firstpairbind (multiple_nth cbind 0))
(lastpairbind (multiple_nth cbind -2))
)
(debug_msg conslibind "normletrec_list conslibind")
(assert_msg "check conslibind"
(is_a conslibind class_normal_constructed_list_binding))
(debug_msg sargs "normletrec_list sargs")
(debug_msg nletrec "normletrec_list nletrec")
(debug_msg nrecbinds "normletrec_list nrecbinds")
(assert_msg "check nletrec" (is_a nletrec class_nrep_letrec))
(assert_msg "check nrecbinds" (is_list nrecbinds))
(multicall
(nargs nbinds)
(normalize_tuple sargs env ncx loc)
(debug_msg nargs "normletrec_list nargs")
(debug_msg nbinds "normletrec_list nbinds")
(assert_msg "check nbinds" (is_list_or_null nbinds))
(assert_msg "check nargs's length vs cbind's length"
(==i (multiple_length nargs) (-i (multiple_length cbind) 1)))
(foreach_in_multiple
(nargs)
(curnarg :long nix)
(debug_msg curnarg "normletrec_list curnarg")
(let ( (curcbind (multiple_nth cbind nix))
(nextcbind (multiple_nth cbind (+i 1 nix)))
(nextpairsymb (if (is_a nextcbind class_normal_constructed_pair_binding)
(get_field :binder nextcbind)))
)
(debug_msg curcbind "normletrec_list curcbind")
(debug_msg nextcbind "normletrec_list nextcbind")
(debug_msg nextpairsymb "normletrec_list nextpairsymb")
(assert_msg "check curcbind" (is_a curcbind class_normal_constructed_pair_binding))
(let ( (nextpairloc (if nextpairsymb (nreclist_find_locsym nextpairsymb nletrec))) )
(debug_msg nextpairloc "normletrec_list got nextpairloc")
(put_fields curcbind
:npairb_head curnarg
:npairb_tail nextpairloc)
(debug_msg curcbind "normletrec_list updated curcbind")
(assert_msg "check curcbind" (is_a curcbind class_normal_constructed_pair_binding))
)))
(debug_msg firstpairbind "normletrec_list firstpairbind")
(debug_msg lastpairbind "normletrec_list lastpairbind")
(debug_msg conslibind "normletrec_list conslibind again")
(and (is_a firstpairbind class_normal_constructed_pair_binding)
(is_a lastpairbind class_normal_constructed_pair_binding)
(let ( (firstpairsymb (get_field :binder firstpairbind))
(lastpairsymb (get_field :binder lastpairbind))
(firstpairlocsy (nreclist_find_locsym firstpairsymb nletrec))
(lastpairlocsy (nreclist_find_locsym lastpairsymb nletrec))
)
(debug_msg firstpairlocsy "normletrec_list firstpairlocsy")
(debug_msg lastpairlocsy "normletrec_list lastpairlocsy")
(put_fields conslibind
:nlistb_first firstpairlocsy
:nlistb_last lastpairlocsy)
(debug_msg conslibind "normletrec_list updated conslibind")
(void)
))
(debug_msg nbinds "normletrec_list appending nbinds")
(list_append2list nrecbinds nbinds)
(debug_msg nrecbinds "normletrec_list ended updated nrecbinds")
)))
(install_method class_source_list normal_letrec_constructive normletrec_list)
;;;;;;;;;;;;;;;;
(defun prepcons_instance (recv symb ncx sloc)
(debug_msg recv "prepcons_instance recv")
(debug_msg symb "prepcons_instance symb")
(assert_msg "check recv" (is_a recv class_source_instance))
(let (
(loc (get_field :loca_location recv))
(cla (get_field :smins_class recv))
(clabind (get_field :smins_clabind recv))
(sclasym (if (is_a clabind class_any_binding) (unsafe_get_field :binder clabind)))
(:long nbclafld (multiple_length (get_field :class_fields cla)))
(tupslot (make_multiple discr_multiple nbclafld))
(consinst (instance class_normal_constructed_instance_binding
:binder symb
:nconsb_loc (or loc sloc)
:nconsb_discr (compile_warning "don't forget to set the discr later...")
:ninstb_slots tupslot
:ninstb_clabind clabind
))
)
(assert_msg "prepcons_instance check class" (is_a cla class_class))
(debug_msg consinst "prepcons_instance gives consinst")
(return consinst)
)
)
(install_method class_source_instance prepare_constructor_binding prepcons_instance)
;;;;
(defun normletrec_instance (cexpr symb cbind env ncx sloc)
(debug_msg cexpr "normletrec_instance cexpr")
(debug_msg symb "normletrec_instance symb")
(debug_msg cbind "normletrec_instance cbind")
(let ( (loc (or (unsafe_get_field :loca_location cexpr) sloc))
(bslots (get_field :ninstb_slots cbind))
(nletrec (get_field :nconsb_nletrec cbind))
(nrecbinds (get_field :nletrec_fill_bindings nletrec))
(sclass (unsafe_get_field :smins_class cexpr))
(sclabind (unsafe_get_field :smins_clabind cexpr))
(sfields (unsafe_get_field :smins_fields cexpr))
(sclasym (if (is_a sclabind class_any_binding) (unsafe_get_field :binder sclabind)))
(cladata (if (is_a sclasym class_symbol)
(normal_exp sclasym env ncx sloc)))
(bindlist (make_list discr_list))
)
(debug_msg nletrec "normletrec_instance nletrec")
(if (not (is_a cladata class_nrep))
(progn
(error_strv sloc "invalid class in (INSTANCE ..)" (unsafe_get_field :named_name sclass))
(return ())))
(let ( (nfields
(multiple_map
sfields
(lambda (curflda :long curk)
(assert_msg "check curflda" (is_a curflda class_source_fieldassign))
(let ( (curfloc (unsafe_get_field :loca_location curflda))
(curfield (unsafe_get_field :sfla_field curflda))
(curexp (unsafe_get_field :sfla_expr curflda)) )
(if (null curfloc) (setq curfloc sloc))
(multicall
(nexp nbind)
(normal_exp curexp env ncx curfloc)
(debug_msg nexp "normletrec_instance nexp")
(assert_msg "check nbind" (is_list_or_null nbind))
(list_append2list bindlist nbind)
(instance class_nrep_fieldassign
:nrep_loc curfloc
:nfla_field curfield
:nfla_val nexp)
)))))
)
(debug_msg nfields "normletrec_instance nfields")
(foreach_in_multiple
(nfields)
(curnfieldass :long fldix)
(debug_msg curnfieldass "normletrec_instance curnfieldass")
(let ( (curfield (get_field :nfla_field curnfieldass))
(curfval (get_field :nfla_val curnfieldass))
(:long curfoff (get_int curfield))
)
(assert_msg "check curfield " (is_a curfield class_field))
(multiple_put_nth bslots curfoff curfval)
))
(list_append2list nrecbinds bindlist)
(debug_msg nrecbinds "normletrec_instance ended updated nrecbinds")
)
))
(install_method class_source_instance normal_letrec_constructive normletrec_instance)
;;;;;;;;;;;;;;;;
(defun normexp_letrec (recv env ncx psloc)
(assert_msg "check letrec recv" (is_a recv class_source_letrec))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_letrec recv")
(let ( (sloc (unsafe_get_field :loca_location recv))
(sbinds (unsafe_get_field :slet_bindings recv))
(sbody (unsafe_get_field :slet_body recv))
(:long nbbind (multiple_length sbinds))
(bindlist (make_list discr_list))
(consbindlist (make_list discr_list))
(newenv (fresh_env env))
(:long maplen (+i 5 (*i nbbind 2)))
(symbexprmap (make_mapobject discr_map_objects maplen))
(symbindmap (make_mapobject discr_map_objects maplen))
)
;;; first preparation loop
(foreach_in_multiple
(sbinds)
(cursbind :long sbix)
(debug_msg cursbind "normexp_letrec cursbind")
(assert_msg "check cursbind" (is_a cursbind class_source_letrec_binding))
(let ( (locb (or (unsafe_get_field :loca_location cursbind) sloc psloc))
(cursymb (unsafe_get_field :sletb_binder cursbind))
(curexpr (unsafe_get_field :sletb_expr cursbind))
)
(debug_msg curexpr "normexp_letrec curexpr")
(debug_msg cursymb "normexp_letrec cursymb")
(assert_msg "check curexpr" (is_a curexpr class_source))
(assert_msg "check cursymb" (is_a cursymb class_symbol))
(mapobject_put symbexprmap cursymb curexpr)
(let ( (curconsbind (prepare_constructor_binding curexpr cursymb ncx locb))
)
(debug_msg curconsbind "normexp_letrec curconsbind")
(cond ( (is_a curconsbind class_normal_constructor_binding)
(debug_msg curconsbind "normexp_letrec curconsbind plain consbind")
(list_append consbindlist curconsbind)
(mapobject_put symbindmap cursymb curconsbind)
)
( (is_multiple curconsbind)
(debug_msg curconsbind "normexp_letrec curconsbind multiple")
(foreach_in_multiple
(curconsbind)
(subconsbind :long subix)
(assert_msg "normexp_letrec check subconsbind"
(is_a subconsbind class_normal_constructor_binding))
(list_append consbindlist subconsbind))
(mapobject_put symbindmap cursymb curconsbind)
)
( (is_list curconsbind)
(debug_msg curconsbind "normexp_letrec curconsbind list")
(foreach_in_list
(curconsbind)
(subconspair subconsbind)
(assert_msg "normexp_letrec check subconsbind"
(is_a subconsbind class_normal_constructor_binding))
(list_append consbindlist subconsbind))
(mapobject_put symbindmap cursymb (list_to_multiple curconsbind discr_multiple))
)
(:else
(assert_msg "normexp_letrec bad curconsbind" ())
))
)
)
)
(debug_msg consbindlist "normexp_letrec consbindlist")
(let (
(:long nbconsbind (list_length consbindlist))
(:long curcix 0)
(nlocsyms (make_multiple discr_multiple nbconsbind))
(ncbindtup (make_multiple discr_multiple nbconsbind))
(recfillbindings (make_list discr_list))
(nletrec (instance class_nrep_letrec
:nrep_loc sloc
:nlet_bindings ncbindtup
:nletrec_fill_bindings recfillbindings
:nletrec_locsyms nlocsyms
;; nlet_body & nletrec_body_bindings is set after
:nlet_body ()
:nletrec_body_bindings ()
))
)
(debug_msg nletrec "normexp_letrec unfilled nletrec")
;;
;; second loop to make an environment where each constructive
;; binding is set, and to create the local symbol occurrences
(foreach_in_list
(consbindlist)
(curpair curcbind)
(debug_msg curcbind "normexp_letrec curcbind")
(assert_msg "check curcbind" (is_a curcbind class_normal_constructor_binding))
(multiple_put_nth ncbindtup curcix curcbind)
(put_int curcbind curcix)
(put_fields curcbind :nconsb_nletrec nletrec)
(let ( (curbdiscr (unsafe_get_field :nconsb_discr curcbind))
(curbinder (unsafe_get_field :binder curcbind))
(nlocsym (instance class_nrep_locsymocc
:nrep_loc (or (get_field :nconsb_loc curcbind)
sloc psloc)
:nocc_ctyp ctype_value
:nocc_symb curbinder
:nocc_bind curcbind
))
)
(multiple_put_nth nlocsyms curcix nlocsym)
(if (null curbdiscr)
(let ( (clabind (get_field :ninstb_clabind curcbind))
(clasym (get_field :binder clabind))
)
(debug_msg clabind "normexp_letrec clabind")
(debug_msg clasym "normexp_letrec clasym")
;; the only case when this happens is for instance constructors...
(assert_msg "check curcbind for instance" (is_a curcbind class_normal_constructed_instance_binding))
(let ( ;; we normalize the class symbol in the parent environment, not the new one!
(cladata (normal_exp clasym env ncx sloc))
)
(debug_msg cladata "normexp_letrec cladata")
(assert_msg "check cladata" (is_a cladata class_nrep))
(put_fields curcbind :nconsb_discr cladata)
)))
)
(setq curcix (+i curcix 1))
(put_env newenv curcbind)
(compile_warning "normexp_letrec should normalize the expression using symbexprmap & curbinder...")
)
;;; third loop to normalize the bindings content
(foreach_in_multiple
(sbinds)
(cursbind :long sbix)
(debug_msg cursbind "normexp_letrec thirdloop cursbind")
(assert_msg "check cursbind" (is_a cursbind class_source_letrec_binding))
(let ( (locb (or (unsafe_get_field :loca_location cursbind) sloc psloc))
(cursymb (unsafe_get_field :sletb_binder cursbind))
(curexpr (unsafe_get_field :sletb_expr cursbind))
(curbind (mapobject_get symbindmap cursymb))
(cursexpr (mapobject_get symbexprmap cursymb))
)
(debug_msg curexpr "normexp_letrec thirdloop curexpr")
(debug_msg cursymb "normexp_letrec thirdloop cursymb")
(debug_msg curbind "normexp_letrec thirdloop curbind")
(debug_msg cursexpr "normexp_letrec thirdloop cursexpr")
(assert_msg "check curxpr same cursexpr" (== curexpr cursexpr))
(normal_letrec_constructive curexpr cursymb curbind newenv ncx sloc)
(debug_msg curexpr "normexp_letrec thirdloop done curexpr")
)
)
(debug_msg recfillbindings "normexp_letrec recfillbindings before normalizing the body")
;;;
;;; normalize the body
(debug_msg sbody "normexp_letrec normalizing sbody")
(multicall
(nbody nbodbindings)
(normalize_tuple sbody newenv ncx sloc)
(debug_msg nbody "normexp_letrec nbody")
(debug_msg nbodbindings "normexp_letrec nbodbindings")
(assert_msg "normexp_letrec check nbodbindings" (is_list_or_null nbodbindings))
(put_fields nletrec
:nlet_body nbody
:nletrec_body_bindings nbodbindings)
(debug_msg nletrec "normexp_letrec updated nletrec")
(assert_msg "normexp_letrec check bindlist" (is_list_or_null bindlist))
;;; remove every locally bound symbol from the symbol cache map
(let ( (sycmap (unsafe_get_field :nctx_symbcachemap ncx)) )
(foreach_in_list
(bindlist)
(curpair bnd)
(debug_msg bnd "normexp_letrec removing from sycmap bnd")
(assert_msg "normexp_letrec check bnd" (is_a bnd class_normal_let_binding))
(mapobject_remove sycmap (unsafe_get_field :binder bnd))
))
;;;; make the result
(let (
(nlastbody (multiple_nth nbody -1))
;; the type of a let with empty body is void
(nlastyp (or (get_ctype nlastbody newenv) ctype_void))
(csym (clone_symbol 'letrec_))
(cbind (instance class_normal_let_binding
:binder csym
:letbind_loc sloc
:letbind_type nlastyp
:letbind_expr nletrec))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp nlastyp
:nocc_bind cbind))
(resbinds (make_list discr_list))
)
(list_append resbinds cbind)
(debug_msg clocc "normexp_letrec result clocc")
(debug_msg resbinds "normexp_letrec result resbinds")
(shortbacktrace_dbg "normexp_letrec ending" 15)
(return clocc resbinds)
)))))
(install_method class_source_letrec normal_exp normexp_letrec)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; create the normal predef (or fail with a msg)
(defun normal_predef (pred ncx sloc msg)
(assert_msg "check pred" (is_object pred))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(let ( (predefmap (unsafe_get_field :nctx_predefmap ncx))
(brk (mapobject_get predefmap pred)) )
(if (or (is_integerbox brk) (is_a brk class_symbol))
(instance class_nrep_predef
:nrep_loc sloc
:nrpredef brk)
(if (is_string msg)
(progn
(debug_msg predefmap "normalpredef predefmap")
(debug_msg pred "normalpredef pred")
(error_strv sloc "not a predef:" msg)
()
)))))
;; retrieve or create the normalized datasym for a symbol
(defun normal_symbol_data (sym ncx sloc)
;; sym should be strictly a symbol (not be in a subclass of class_symbol!)
(debug_msg sym "normal_symbol_data start sym")
(assert_msg "check symb" (== (discrim sym) class_symbol))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(let ( (valmap (unsafe_get_field :nctx_valmap ncx))
(osydata (mapobject_get valmap sym)) )
(if osydata
(progn
(debug_msg osydata "normal_symbol_data found osydata")
(return osydata))
(let ( (:long syhash (obj_hash sym))
(synamstr (unsafe_get_field :named_name sym))
;; make the datastring from synamstr
(synamstrdata
(instance class_nrep_datastring
:ndata_discrx (normal_predef discr_string ncx sloc "discr_string")
:nstr_string synamstr
))
(syslots (make_multiple discr_multiple
(multiple_length (unsafe_get_field :class_fields class_symbol))))
(sydata (instance class_nrep_datasymbol
:ndata_name sym
:ndata_discrx (normal_predef class_symbol ncx sloc "class_symbol")
:ninst_hash (make_integerbox discr_integer syhash)
:ninst_slots syslots
:ndsy_namestr synamstr))
)
(multiple_put_nth syslots (obj_num named_name) synamstrdata)
(add_nctx_data ncx sydata)
(add_nctx_data ncx synamstrdata)
(mapobject_put valmap sym sydata)
(mapstring_putstr (unsafe_get_field :nctx_symbmap ncx) synamstr sydata)
(debug_msg sydata "normal_symbol_data return sydata")
(return sydata)
))))
;; retrieve or create the normalized datakeyword for a keyword
(defun normal_keyword_data (keyw ncx sloc)
;; keyw should be strictly a keyword (not be in a subclass of class_keyword!)
(assert_msg "check keywb" (== (discrim keyw) class_keyword))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(let ( (valmap (unsafe_get_field :nctx_valmap ncx))
(osydata (mapobject_get valmap keyw)) )
(if osydata osydata
(let ( (:long syhash (obj_hash keyw))
(synamstr (unsafe_get_field :named_name keyw))
;; make the datastring from synamstr
(synamstrdata
(instance class_nrep_datastring
:ndata_discrx (normal_predef discr_string ncx sloc "discr_string")
:nstr_string synamstr
))
(syslots (make_multiple discr_multiple
(multiple_length (unsafe_get_field :class_fields class_keyword))))
(sydata (instance class_nrep_datakeyword
:ndata_name keyw
:ndata_discrx (normal_predef class_keyword ncx sloc "class_keyword")
:ninst_hash (make_integerbox discr_integer syhash)
:ninst_slots syslots
:ndsy_namestr synamstr))
)
(multiple_put_nth syslots (obj_num named_name) synamstrdata)
(add_nctx_data ncx sydata)
(add_nctx_data ncx synamstrdata)
(mapobject_put valmap keyw sydata)
(mapstring_putstr (unsafe_get_field :nctx_keywmap ncx) synamstr sydata)
(return sydata)
))))
;; create the tuples of slots of a datainstance for a particular class
(defun create_data_slots (cla)
(assert_msg "check cla" (is_a cla class_class))
(debug_msg cla "create_data_slots cla")
(let ( (tupslo
(make_multiple discr_multiple (multiple_length (unsafe_get_field :class_fields cla))))
)
(debug_msg tupslo "create_data_slots tupslo")
tupslo
))
;; fill a slot of a datainstance
(defun fill_data_slot (di field val)
(assert_msg "check di" (is_a di class_nrep_datainstance))
(assert_msg "check field" (is_a field class_field))
(let ( (:long fix (obj_num field))
(slots (unsafe_get_field :ninst_slots di)) )
(multiple_put_nth slots fix val)
))
;;;;;; normalize a QUOTE-d symbol, string or integer
(defun normexp_quote (recv env ncx psloc)
(assert_msg "check quote recv" (is_a recv class_source_quote))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
;(debug_msg ncx "normexp_quote ncx")
(debug_msg recv "normexp_quote recv")
(let ( (sloc (unsafe_get_field :loca_location recv))
(quoted (unsafe_get_field :squoted recv))
(curproc (unsafe_get_field :nctx_curproc ncx))
(ndata
(cond
( (== (discrim quoted) class_symbol)
(normal_symbol_data quoted ncx psloc))
( (is_integerbox quoted)
(let (
(nintdata (instance class_nrep_databoxedinteger
:ndata_discrx (normal_predef discr_constant_integer ncx sloc "discr_constant_integer")
:nboxint_num quoted))
)
(add_nctx_data ncx nintdata)
nintdata
))
( (is_string quoted)
(let ( (nstrdata
(instance class_nrep_datastring
:ndata_discrx (normal_predef discr_string ncx sloc "discr_string")
:nstr_string quoted))
)
(add_nctx_data ncx nstrdata)
nstrdata))
(:else
;; this should not happen, because it is checked at
;; macroexpansion time.
(assert_msg "unexpected quoted stuff" ()))
))
)
(debug_msg ndata "normexp_quote ndata")
(if (is_a curproc class_nrep_routproc)
(let (
(nconst (instance class_nrep_constant
:nrep_loc sloc
:nconst_sval quoted
:nconst_data ndata
:nconst_proc curproc
)) )
(list_append (unsafe_get_field :nrpro_const curproc) ndata)
(debug_msg nconst "normexp_quote in routine nconst")
(return nconst))
(progn
(debug_msg ndata "normexp_quote in init ndata")
(return ndata)
))))
(install_method class_source_quote normal_exp normexp_quote)
;;;;;; normalize a COMMENT
(defun normexp_comment (recv env ncx psloc)
(assert_msg "check comment recv" (is_a recv class_source_comment))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_comment start recv")
(let ( (sloc (unsafe_get_field :loca_location recv))
(scomm (unsafe_get_field :scomm_str recv))
(ncomm (instance class_nrep_comment
:nrep_loc sloc
:ncomm_string scomm
))
(csym (clone_symbol 'comment_))
(cbind (instance class_normal_let_binding
:letbind_loc sloc
:binder csym
:letbind_type ctype_void
:letbind_expr ncomm))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctype_void
:nocc_symb csym
:nocc_bind cbind))
(bindlist (make_list discr_list))
)
(if scomm (assert_msg "check scomm" (is_string scomm)))
(list_append bindlist cbind)
(debug_msg ncomm "normexp_comment end ncomm")
(debug_msg clocc "normexp_comment return clocc")
(debug_msg bindlist "normexp_comment return bindlist")
(return clocc bindlist)
))
(install_method class_source_comment normal_exp normexp_comment)
;;;;;; normalize a keyword
(defun normexp_keyword (recv env ncx psloc)
(assert_msg "check keyword recv" (is_a recv class_keyword))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
;;(debug_msg ncx "normexp_keyword ncx")
(debug_msg recv "normexp_keyword recv")
(let ( (kdata (normal_keyword_data recv ncx psloc))
(curproc (unsafe_get_field :nctx_curproc ncx))
)
(assert_msg "check curproc" (is_a curproc class_nrep_anyproc))
(if (is_a curproc class_nrep_routproc)
(let (
(constlist (unsafe_get_field :nrpro_const curproc))
(nconst (instance class_nrep_constant
:nrep_loc psloc
:nconst_sval recv
:nconst_data kdata
:nconst_proc curproc))
)
(debug_msg curproc "normexp_keyword routine curproc")
(list_append constlist kdata)
(debug_msg nconst "normexp_keyword result nconst")
(return nconst)
)
(progn
(debug_msg kdata "normexp_keyword routineinit result kdata")
(return kdata)
))))
(install_method class_keyword normal_exp normexp_keyword)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; utilities for normalization of DEFPRIMITIVE & DEFCITERATOR
;;;; fill the normal single formal bind
(defun fill_normal_formalbind (fargb formsymbmap env ncx sloc)
(assert_msg "check fargb" (is_a fargb class_formal_binding))
(let ( (ftyp (unsafe_get_field :fbind_type fargb))
(fsymb (unsafe_get_field :binder fargb))
(fdataslot (create_data_slots class_formal_binding))
(fargdata
(instance
class_nrep_datainstance
:nrep_loc sloc
:ndata_discrx (normal_predef class_formal_binding ncx sloc "class_formal_binding")
:ninst_hash (make_integerbox discr_integer (nonzero_hash))
:ninst_slots fdataslot
))
(fsymbdata (normal_symbol_data fsymb ncx sloc))
(ftypdata (normal_predef ftyp ncx sloc "primitive arg type"))
)
(assert_msg "check ftyp" (is_a ftyp class_ctype))
(add_nctx_data ncx fargdata)
(fill_data_slot fargdata binder fsymbdata)
(fill_data_slot fargdata fbind_type ftypdata)
(mapobject_put formsymbmap fsymb fargdata)
(return fargdata)
))
;;;; fill the normal formal args
(defun fill_normal_formals (sargs nargtuple formsymbmap env ncx sloc)
(foreach_in_multiple
(sargs)
(fargb :long ix)
(let ( (fargdata (fill_normal_formalbind fargb formsymbmap env ncx sloc))
)
(assert_msg "check fargdata" (is_a fargdata class_nrep_datainstance))
(unsafe_put_fields fargdata
:ninst_objnum (make_integerbox discr_integer ix))
(multiple_put_nth nargtuple ix fargdata)
)))
;;;; fill the normal expansion for primitive etc...
(defun fill_normal_expansion (sexp nexptuple ncx sloc)
(multiple_every
sexp
(lambda (expcomp :long ix)
(let ( (discrcomp (discrim expcomp))
(compdata
(cond ( (== discrcomp discr_verbatim_string)
(add_nctx_data ncx
(instance
class_nrep_datastring
:ndata_discrx (normal_predef discr_verbatim_string ncx sloc "discr_verbatim_string")
:nstr_string expcomp
)))
( (== discrcomp class_symbol)
(normal_symbol_data expcomp ncx sloc)
)
( :else
(debug_msg expcomp "bad component in C code expansion expcomp")
(error_plain sloc "unexpected component in C code expansion"))))
)
(multiple_put_nth nexptuple ix compdata)
)))
)
;;;;;; normalize a DEFPRIMITIVE
(defun normexp_defprimitive (recv env ncx psloc)
(assert_msg "check defprimitive recv" (is_a recv class_source_defprimitive))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(let ( (sloc (unsafe_get_field :loca_location recv))
(sname (unsafe_get_field :sdef_name recv))
(sargs (unsafe_get_field :sformal_args recv))
(stype (unsafe_get_field :sprim_type recv))
(sexp (unsafe_get_field :sprim_expansion recv))
(sprimbind (find_env env sname))
;; we compile to the making of an instance of class_primitive
(nslotuple (create_data_slots class_primitive))
(nexptuple (make_multiple discr_multiple
(multiple_length sexp)))
(nargtuple (make_multiple discr_multiple
(multiple_length sargs)))
(nexpdata (instance class_nrep_datatuple
:nrep_loc sloc
:ndata_name sname
:ndata_discrx (normal_predef discr_multiple ncx sloc "discr_multiple")
:ntup_comp nexptuple))
(nargdata (instance class_nrep_datatuple
:nrep_loc sloc
:ndata_name sname
:ndata_discrx (normal_predef discr_multiple ncx sloc "discr_multiple")
:ntup_comp nargtuple))
(nprimdata (instance class_nrep_datainstance
:nrep_loc sloc
:ndata_name sname
:ndata_discrx (normal_predef class_primitive ncx sloc "class_primitive")
:ninst_hash (make_integerbox discr_integer (nonzero_hash))
:ninst_slots nslotuple))
(nsymdata (normal_symbol_data sname ncx sloc))
;; map of formal symbol -> data of formal_binding
(formsymbmap (make_mapobject discr_map_objects (+i 3 (*i (multiple_length sargs) 2))))
)
(add_nctx_data ncx nprimdata)
(add_nctx_data ncx nexpdata)
(add_nctx_data ncx nargdata)
;; dont add nsymdata, it has already been added
;; fill the formal arguments of the data
(fill_normal_formals sargs nargtuple formsymbmap env ncx sloc)
;; fill the expansion of the data
(fill_normal_expansion sexp nexptuple ncx sloc)
;;; fill the primitive data
(fill_data_slot nprimdata named_name
(add_nctx_data
ncx
(instance
class_nrep_datastring
:ndata_discrx (normal_predef discr_string ncx sloc "discr_string")
:nstr_string (unsafe_get_field :named_name sname))))
(fill_data_slot nprimdata prim_formals nargdata)
(fill_data_slot nprimdata prim_expansion nexpdata)
(fill_data_slot nprimdata prim_type
(normal_predef stype ncx sloc "primitive res type"))
;;; put the data into the primitive binding
(if (is_a sprimbind class_primitive_binding)
(unsafe_put_fields sprimbind
:fixbind_data nprimdata
))
(return ()) ;normalized defprimitive is empty
))
(install_method class_source_defprimitive normal_exp normexp_defprimitive)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; normalize a DEFCITERATOR
(defun normexp_defciterator (recv env ncx psloc)
(assert_msg "check defciterator recv" (is_a recv class_source_defciterator))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(let (
(sloc (unsafe_get_field :loca_location recv))
(sname (unsafe_get_field :sdef_name recv))
(sciter (let ( (sc (unsafe_get_field :sciterdef_citerator recv)) )
(assert_msg "check sciter" (is_a sc class_citerator))
sc))
(citbind (find_env env sname))
(citstafor (unsafe_get_field :citer_start_formals sciter))
(slotup (make_multiple discr_multiple
(multiple_length (unsafe_get_field :class_fields class_citerator))))
(formstatup (make_multiple discr_multiple
(multiple_length citstafor)))
(citbodfor (unsafe_get_field :citer_body_formals sciter))
(formbodtup (make_multiple discr_multiple
(multiple_length citbodfor)))
(citstatsy (unsafe_get_field :citer_state sciter))
(citexpbef (unsafe_get_field :citer_expbefore sciter))
(expbeftup (make_multiple discr_multiple (multiple_length citexpbef)))
(citexpaft (unsafe_get_field :citer_expafter sciter))
(expafttup (make_multiple discr_multiple (multiple_length citexpaft)))
;; map of formal symbol -> data of formal_binding
(formsymbmap (make_mapobject discr_map_objects
(+i 5 (*i (+i (multiple_length citstafor) (multiple_length citbodfor)) 2))))
(namstrdata (instance class_nrep_datastring
:nrep_loc sloc
:ndata_discrx (normal_predef discr_string ncx sloc "discr_string")
:nstr_string (unsafe_get_field :named_name sname)))
(insdata (instance class_nrep_datainstance
:nrep_loc sloc
:ndata_name sname
:ndata_discrx (normal_predef class_citerator ncx sloc "class_citerator")
:ninst_hash (make_integerbox discr_integer (nonzero_hash))
:ninst_slots slotup
))
)
(add_nctx_data ncx insdata)
(add_nctx_data ncx namstrdata)
;;; fill the named_name of insdata
(multiple_put_nth slotup (get_int named_name) namstrdata)
;;; fill the citer_start_formals of insdata
(fill_normal_formals citstafor formstatup formsymbmap env ncx sloc)
(let ( (nstatupdata (instance class_nrep_datatuple
:nrep_loc sloc
:ndata_name sname
:ndata_discrx (normal_predef discr_multiple ncx sloc "discr_multiple")
:ntup_comp formstatup))
)
(add_nctx_data ncx nstatupdata)
(multiple_put_nth slotup (get_int citer_start_formals)
nstatupdata)
)
;;; fill the citer_state
(assert_msg "check citstatsy" (is_a citstatsy class_symbol))
(let ( (nstatsydata (normal_symbol_data citstatsy ncx sloc)) )
(multiple_put_nth slotup (get_int citer_state)
nstatsydata)
)
;;; fill the citer_body_formals of insdata
(fill_normal_formals citbodfor formbodtup formsymbmap env ncx sloc)
(let ( (nbodtupdata (instance class_nrep_datatuple
:nrep_loc sloc
:ndata_name sname
:ndata_discrx (normal_predef discr_multiple ncx sloc "discr_multiple")
:ntup_comp formbodtup))
)
(add_nctx_data ncx nbodtupdata)
(multiple_put_nth slotup (get_int citer_body_formals) nbodtupdata)
)
;;; fill the citer_expbefore of insdata
(fill_normal_expansion citexpbef expbeftup ncx sloc)
(let ( (nbeftupdata (instance class_nrep_datatuple
:nrep_loc sloc
:ndata_name sname
:ndata_discrx (normal_predef discr_multiple ncx sloc "discr_multiple")
:ntup_comp expbeftup))
)
(add_nctx_data ncx nbeftupdata)
(multiple_put_nth slotup (get_int citer_expbefore) nbeftupdata)
)
;;; fill the citer_expafter of insdata
(fill_normal_expansion citexpaft expafttup ncx sloc)
(let ( (nafttupdata (instance class_nrep_datatuple
:nrep_loc sloc
:ndata_name sname
:ndata_discrx (normal_predef discr_multiple ncx sloc "discr_multiple")
:ntup_comp expafttup))
)
(add_nctx_data ncx nafttupdata)
(multiple_put_nth slotup (get_int citer_expafter) nafttupdata)
)
(assert_msg "check citbind" (is_a citbind class_citerator_binding))
(unsafe_put_fields citbind
:fixbind_data insdata
)
;;;;;;;
;; return the data
(debug_msg insdata "normexp_defciterator return insdata")
(return insdata)
))
(install_method class_source_defciterator normal_exp normexp_defciterator)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; normalize a citeration
(defun normexp_citeration (recv env ncx psloc)
(debug_msg recv "normexp_citeration recv")
(assert_msg "check citeration recv" (is_a recv class_source_citeration))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(let ( (sloc (unsafe_get_field :loca_location recv))
(soper (unsafe_get_field :sciter_oper recv))
(sargs (unsafe_get_field :sargop_args recv))
(svbind (unsafe_get_field :sciter_varbind recv))
(sbody (unsafe_get_field :sciter_body recv))
(nbndtup (make_multiple discr_multiple (multiple_length svbind)))
(bodyenv (fresh_env env))
;; we need to remove or add stuff from the symbol cache map, as normexp_let does
(sycmap (unsafe_get_field :nctx_symbcachemap ncx))
;; the list of symbol to remove at end from the above map
(uncachelist (make_list discr_list))
)
(assert_msg "check soper" (is_a soper class_citerator))
;; normalize the iterator input arguments
(multicall
(nargs nbindings)
(normalize_tuple sargs env ncx sloc)
(if (null nbindings) (setq nbindings (make_list discr_list)))
(debug_msg nargs "normexp_citeration nargs")
(debug_msg nbindings "normexp_citeration nbindings")
(let ( (starformals (unsafe_get_field :citer_start_formals soper)) )
(debug_msg starformals "normexp_citeration starformals")
(if (!=i (multiple_length starformals) (multiple_length nargs))
(progn
(error_strv sloc "start formals and actuals length mismatch in citerator" (unsafe_get_field :named_name soper))
(return)))
(multiple_every_both
nargs starformals
(lambda (curnarg curforb :long ix)
(debug_msg curnarg "normexp_citeration curnarg")
(debug_msg curforb "normexp_citeration curforb")
(assert_msg "check curforb" (is_a curforb class_formal_binding))
(let ( (curctyp (get_ctype curnarg env))
(formctyp (unsafe_get_field :fbind_type curforb))
(formarg (unsafe_get_field :binder curforb))
)
(if (== curctyp formctyp)
(let ( (nlbind (instance class_normal_let_binding
:letbind_loc sloc
:binder formarg
:letbind_type curctyp
:letbind_expr curnarg
)) )
(multiple_put_nth nbndtup ix nlbind)
)
(progn
(error_strv sloc "start formal and actual type mismatch in citerator" (unsafe_get_field :named_name soper))
(inform_strv sloc "mismatched citerator start formal argument" (unsafe_get_field :named_name formarg))))
))
)
(debug_msg nbndtup "normexp_citeration nbndtup")
;; bind the local vars
(debug_msg svbind "normexp_citeration svbind")
(let ( (citbform (unsafe_get_field :citer_body_formals soper))
(:long nbcitbform (multiple_length citbform))
(nlocbindtup (make_multiple discr_multiple nbcitbform))
(nsymocctup (make_multiple discr_multiple nbcitbform))
)
(debug_msg citbform "normexp_citeration citbform")
(if (!=i nbcitbform (multiple_length svbind))
(progn
(error_strv sloc "body formals and actuals length mismatch in citerator" (unsafe_get_field :named_name soper))
(return)))
(multiple_every_both
svbind citbform
(lambda (curvbind curbform :long ix)
(debug_msg curvbind "normexp_citeration curvbind")
(debug_msg curbform "normexp_citeration curbform")
(assert_msg "check curvbind" (is_a curvbind class_formal_binding))
(assert_msg "check curbform" (is_a curbform class_formal_binding))
(let ( (curvsym (unsafe_get_field :binder curvbind))
(curctyp (unsafe_get_field :fbind_type curvbind))
(curvfor (unsafe_get_field :binder curbform))
)
(if (== curctyp (unsafe_get_field :fbind_type curbform))
(let ( (nlvbind (instance class_normal_let_binding
:letbind_loc sloc
:binder curvsym
:letbind_type curctyp
:letbind_expr ()
))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp curctyp
:nocc_symb curvsym
:nocc_bind nlvbind))
)
(multiple_put_nth nlocbindtup ix nlvbind)
(multiple_put_nth nsymocctup ix clocc)
(debug_msg nlvbind "normexp_citeration nlvbind")
;;(put_env bodyenv nlvbind)
;; update the curvsym in the symbol cache map to
;; ensure that it will be normalized as the same
;; local symbol occurence
(mapobject_put sycmap curvsym clocc)
(list_append uncachelist curvsym)
)
(progn
(error_strv sloc "local formal and actual type mismatch in citerator" (unsafe_get_field :named_name soper))
(inform_strv sloc "mismatched citerator local variable"
(unsafe_get_field :named_name (unsafe_get_field :binder curvbind)))
))
)
(put_env bodyenv curvbind)
))
(debug_msg nlocbindtup "normexp_citeration nlocbindtup")
(debug_msg nsymocctup "normexp_citeration nsymocctup")
(multicall
(nbody nbodbindings)
(normalize_tuple sbody bodyenv ncx sloc)
(debug_msg nbody "normexp_citeration nbody")
(debug_msg nbodbindings "normexp_citeration nbodbindings")
(let ( (citstate (unsafe_get_field :citer_state soper))
(citstsym (clone_symbol citstate))
(citstbind (instance class_normal_let_binding
:letbind_loc sloc
:binder citstsym
:letbind_type ctype_void
:letbind_expr ()))
(citstocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctype_void
:nocc_symb citstsym
:nocc_bind citstbind))
(citexpbefore (unsafe_get_field :citer_expbefore soper))
(citexpafter (unsafe_get_field :citer_expafter soper))
(citlocmap (make_mapobject discr_map_objects (+i 10 nbcitbform)))
(citexpander
(lambda (xtup)
(debug_msg xtup "normexp_citeration expanding xtup")
(multiple_map
xtup
(lambda (curelem :long ix)
(if (is_a curelem class_symbol)
(let ( (exelem (mapobject_get citlocmap curelem)) )
(if (null exelem)
(debug_msg curelem "normexp_citeration bad curelem"))
(assert_msg "check exelem in citeration" exelem)
exelem)
curelem)
))
))
)
(mapobject_put citlocmap citstate citstocc)
(debug_msg starformals "normexp_citeration starformals again")
(debug_msg nsymocctup "normexp_citeration nsymocctup again")
(multiple_every_both
starformals nargs
(lambda (curvloc curnarg :long ix)
(debug_msg curvloc "normexp_citeration curvloc")
(debug_msg curnarg "normexp_citeration curnarg")
(mapobject_put citlocmap (unsafe_get_field :binder curvloc) curnarg)
))
(debug_msg citlocmap "normexp_citeration citlocmap middle")
(debug_msg citbform "normexp_citeration citbform again")
(multiple_every
citbform
(lambda (curformb :long ix)
(assert_msg "check curform" (is_a curformb class_formal_binding))
(mapobject_put citlocmap
(unsafe_get_field :binder curformb)
(multiple_nth nsymocctup ix))
))
(debug_msg citlocmap "normexp_citeration citlocmap done")
(let ( (chkbefore (citexpander citexpbefore))
(chkafter (citexpander citexpafter))
)
(debug_msg chkbefore "normexp_citeration chkbefore")
(debug_msg chkafter "normexp_citeration chkafter")
(let ( (nciter
(instance class_nrep_citeration
:nrep_loc sloc
:nciter_citerator soper
:nciter_locbindings nlocbindtup
:nciter_chunkbefore chkbefore
:nciter_body nbody
:nciter_statocc citstocc
:nciter_bodbindings nbodbindings
:nciter_chunkafter chkafter
))
(csym (clone_symbol (unsafe_get_field :named_name soper)))
(cbind (instance class_normal_let_binding
:letbind_loc sloc
:binder csym
:letbind_type ctype_void
:letbind_expr nciter))
(clocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctype_void
:nocc_symb csym
:nocc_bind cbind))
)
(list_append nbindings cbind)
;; remove all required stuff from the symbol cache
(list_every
uncachelist
(lambda (csy) (mapobject_remove sycmap csy)))
(debug_msg nciter "normexp_citeration nciter")
(debug_msg clocc "normexp_citeration result clocc")
(debug_msg nbindings "normexp_citeration result nbindings")
(return clocc nbindings)
))))))))
)
(install_method class_source_citeration normal_exp normexp_citeration)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;; normalize a DEFCMATCHER
(defun normexp_defcmatcher (recv env ncx psloc)
(assert_msg "check defcmatcher recv" (is_a recv class_source_defcmatcher))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_defcmatcher recv")
(let ( (sloc (unsafe_get_field :loca_location recv))
(sname (unsafe_get_field :sdef_name recv))
(sformals (unsafe_get_field :sformal_args recv))
(cmatcher (let ( (cm (unsafe_get_field :scmatdef_cmatcher recv)) )
(debug_msg cm "normexp_defcmatcher cmatcher")
(assert_msg "check cmatcher" (is_a cm class_cmatcher))
cm))
(cmbind (let ( (b (find_env env sname)) )
(debug_msg b "normexp_defcmatcher cmbind")
(assert_msg "check cmbind" (is_a b class_cmatcher_binding))
b))
(slotup (make_multiple discr_multiple (object_length cmatcher)))
(inscma (unsafe_get_field :amatch_in cmatcher))
(mbicma (unsafe_get_field :amatch_matchbind cmatcher))
(outscma (unsafe_get_field :amatch_out cmatcher))
(statcma (unsafe_get_field :cmatch_state cmatcher))
(testcma (unsafe_get_field :cmatch_exptest cmatcher))
(fillcma (unsafe_get_field :cmatch_expfill cmatcher))
(opercma (unsafe_get_field :cmatch_expoper cmatcher))
(instup (make_multiple discr_multiple (multiple_length inscma)))
(outstup (make_multiple discr_multiple (multiple_length outscma)))
(testtup (if testcma (make_multiple discr_multiple (multiple_length testcma))))
(filltup (if fillcma (make_multiple discr_multiple (multiple_length fillcma))))
(opertup (if opercma (make_multiple discr_multiple (multiple_length opercma))))
;; map of formal symbol -> data of formal_binding
(formsymbmap (make_mapobject discr_map_objects
(+i 5 (*i (+i (multiple_length inscma) (multiple_length outscma)) 2))))
(namstrdata (instance class_nrep_datastring
:nrep_loc sloc
:ndata_discrx (normal_predef discr_string ncx sloc "discr_string")
:nstr_string (unsafe_get_field :named_name sname)))
(insdata (instance class_nrep_datainstance
:nrep_loc sloc
:ndata_name sname
:ndata_discrx (normal_predef class_cmatcher ncx sloc "class_cmatcher")
:ninst_hash (make_integerbox discr_integer (obj_hash cmatcher))
:ninst_slots slotup
))
)
(add_nctx_data ncx insdata)
(add_nctx_data ncx namstrdata)
;;; fill the named_name of insdata
(multiple_put_nth slotup (get_int named_name) namstrdata)
;;; fill the amatch_in of insdata
(fill_normal_formals inscma instup formsymbmap env ncx sloc)
(let ( (instupdata (instance class_nrep_datatuple
:nrep_loc sloc
:ndata_name sname
:ndata_discrx (normal_predef discr_multiple ncx sloc "discr_multiple")
:ntup_comp instup))
)
(add_nctx_data ncx instupdata)
(multiple_put_nth slotup (get_int amatch_in)
instupdata)
)
;;; fill the amatch_matchbind of insdata
(let ( (mbdata (fill_normal_formalbind mbicma formsymbmap env ncx sloc)) )
(multiple_put_nth slotup (get_int amatch_matchbind)
mbdata)
)
;;; fill the amatch_out of insdata
(fill_normal_formals outscma outstup formsymbmap env ncx sloc)
(let ( (outstupdata (instance class_nrep_datatuple
:nrep_loc sloc
:ndata_name sname
:ndata_discrx (normal_predef discr_multiple ncx sloc "discr_multiple")
:ntup_comp outstup))
)
(add_nctx_data ncx outstupdata)
(multiple_put_nth slotup (get_int amatch_out)
outstupdata)
)
;;; fill the cmatch_state of insdata
(assert_msg "check statcma" (is_a statcma class_symbol))
(let ( (nstatcmadata (normal_symbol_data statcma ncx sloc)) )
(multiple_put_nth slotup (get_int cmatch_state)
nstatcmadata)
)
;;; fill the cmatch_exptest of insdata
(if (is_multiple testcma)
(progn
(fill_normal_expansion testcma testtup ncx sloc)
(let ( (ntesttupdata (instance class_nrep_datatuple
:nrep_loc sloc
:ndata_name sname
:ndata_discrx (normal_predef discr_multiple ncx sloc "discr_multiple")
:ntup_comp testtup))
)
(add_nctx_data ncx ntesttupdata)
(multiple_put_nth slotup (get_int cmatch_exptest) ntesttupdata)
)
))
;;; fill the cmatch_expfill of insdata
(if (is_multiple fillcma)
(progn
(fill_normal_expansion fillcma filltup ncx sloc)
(let ( (nfilltupdata (instance class_nrep_datatuple
:nrep_loc sloc
:ndata_name sname
:ndata_discrx (normal_predef discr_multiple ncx sloc "discr_multiple")
:ntup_comp filltup))
)
(add_nctx_data ncx nfilltupdata)
(multiple_put_nth slotup (get_int cmatch_expfill) nfilltupdata)
)
))
;;; fill the cmatch_expoper of insdata
(if (is_multiple opercma)
(progn
(fill_normal_expansion opercma opertup ncx sloc)
(let ( (nopertupdata (instance class_nrep_datatuple
:nrep_loc sloc
:ndata_name sname
:ndata_discrx (normal_predef discr_multiple ncx sloc "discr_multiple")
:ntup_comp opertup))
)
(add_nctx_data ncx nopertupdata)
(multiple_put_nth slotup (get_int cmatch_expoper) nopertupdata)
)
))
;;; put the data in the binding
(unsafe_put_fields cmbind :fixbind_data insdata)
;; return the data
(debug_msg insdata "normexp_defcmatcher return insdata")
(return insdata)
)
)
(install_method class_source_defcmatcher normal_exp normexp_defcmatcher)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; normalize a DEFUNMATCHER
(defun normexp_defunmatcher (recv env ncx psloc)
(debug_msg recv "normexp_defunmatcher start recv")
(assert_msg "normexp_defunmatcher check recv" (is_a recv class_source_defunmatcher))
(assert_msg "normexp_defunmatcher check env" (is_a env class_environment))
(assert_msg "normexp_defunmatcher check ncx" (is_a ncx class_normalization_context))
(let ( (sloc (unsafe_get_field :loca_location recv))
(sname (unsafe_get_field :sdef_name recv))
(smatched (multiple_nth (get_field :sformal_args recv) 0))
(sins (unsafe_get_field :sfumatdef_ins recv))
(souts (unsafe_get_field :sfumatdef_outs recv))
(smatchf (unsafe_get_field :sfumatdef_matchf recv))
(sapplyf (unsafe_get_field :sfumatdef_applyf recv))
(sdata (unsafe_get_field :sfumatdef_data recv))
(resbinds (make_list discr_list))
(insfma (subseq_multiple sins 1 -1))
(mbifma (multiple_nth sins 0))
(fmbind (let ( (b (find_env env sname)) )
(debug_msg b "normexp_defunmatcher fmbind")
(assert_msg "check fmbind" (is_a b class_funmatcher_binding))
b))
(funmatcher (get_field :fmbind_funmatcher fmbind))
)
(debug_msg funmatcher "normexp_defunmatcher made funmatcher")
(assert_msg "check smatched" (is_a smatched class_formal_binding))
(assert_msg "check sins" (is_multiple sins))
(assert_msg "check souts" (is_multiple souts))
(assert_msg "check mbifma" (is_a mbifma class_formal_binding))
(multicall
(nmatchf nmabinds)
(normal_exp smatchf env ncx sloc)
(debug_msg nmatchf "normexp_defunmatcher nmatchf")
(list_append2list resbinds nmabinds)
(multicall
(napplf napbinds)
(normal_exp sapplyf env ncx sloc)
(debug_msg napplf "normexp_defunmatcher napplf")
(list_append2list resbinds napbinds)
(multicall
(ndata ndabinds)
(normal_exp sdata env ncx sloc)
(debug_msg ndata "normexp_defunmatcher ndata")
(debug_msg resbinds "normexp_defunmatcher resbinds")
(let (
(namstrdata (instance class_nrep_datastring
:nrep_loc sloc
:ndata_discrx (normal_predef discr_string ncx sloc "discr_string")
:nstr_string (unsafe_get_field :named_name sname)))
(slotup (make_multiple discr_multiple (object_length funmatcher)))
(insdata (instance class_nrep_datainstance
:nrep_loc sloc
:ndata_name sname
:ndata_discrx (normal_predef class_cmatcher ncx sloc "class_funmatcher")
:ninst_hash (make_integerbox discr_integer (nonzero_hash))
:ninst_slots slotup
))
(instup (make_multiple discr_multiple (multiple_length insfma)))
(outstup (make_multiple discr_multiple (multiple_length souts)))
;; map of formal symbol -> data of formal_binding
(formsymbmap (make_mapobject discr_map_objects
(+i 5 (*i (+i (multiple_length insfma)
(multiple_length souts)) 2))))
)
(add_nctx_data ncx namstrdata)
(add_nctx_data ncx insdata)
;;; fill the named_name of insdata
(multiple_put_nth slotup (get_int named_name) namstrdata)
;;; fill the amatch_in of insdata
(fill_normal_formals insfma instup formsymbmap env ncx sloc)
(let ( (instupdata (instance class_nrep_datatuple
:nrep_loc sloc
:ndata_name sname
:ndata_discrx (normal_predef discr_multiple ncx sloc "discr_multiple")
:ntup_comp instup))
)
(add_nctx_data ncx instupdata)
(multiple_put_nth slotup (get_int amatch_in)
instupdata)
)
;;; fill the amatch_matchbind of insdata
(let ( (mbdata (fill_normal_formalbind mbifma formsymbmap env ncx sloc)) )
(multiple_put_nth slotup (get_int amatch_matchbind)
mbdata)
)
;;; fill the amatch_out of insdata
(fill_normal_formals souts outstup formsymbmap env ncx sloc)
(let ( (outstupdata (instance class_nrep_datatuple
:nrep_loc sloc
:ndata_name sname
:ndata_discrx (normal_predef discr_multiple ncx sloc "discr_multiple")
:ntup_comp outstup))
)
(add_nctx_data ncx outstupdata)
(multiple_put_nth slotup (get_int amatch_out)
outstupdata)
)
;;; fill the fmatch_matchf of insdata
(multiple_put_nth slotup (get_int fmatch_matchf)
nmatchf)
;;; fill the fmatch_applyf of insdata
(multiple_put_nth slotup (get_int fmatch_applyf)
napplf)
;;; fill the fmatch_data of insdata
(multiple_put_nth slotup (get_int fmatch_data)
ndata)
;;; put the data in the binding
(unsafe_put_fields fmbind :fixbind_data insdata)
;;; make a funmatcher binding
(debug_msg fmbind "normexp_defunmatcher final fmbind")
(compile_warning "$@$incomplete normexp_defunmatcher" ())
;;; return insdata and resbinds
(debug_msg insdata "normexp_defunmatcher return insdata")
(debug_msg resbinds "normexp_defunmatcher return resbinds")
(return insdata resbinds)
)
)))
))
(install_method class_source_defunmatcher normal_exp normexp_defunmatcher)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; normalize a DEFCLASS
(defun normexp_defclass (recv env ncx psloc)
(assert_msg "check defclass recv" (is_a recv class_source_defclass))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(let ( (sloc (unsafe_get_field :loca_location recv))
(sname (unsafe_get_field :sdef_name recv))
(spredef (unsafe_get_field :sobj_predef recv))
(sclabind (unsafe_get_field :sclass_clabind recv))
(superbind (unsafe_get_field :sclass_superbind recv))
(sfldbinds (unsafe_get_field :sclass_fldbinds recv))
)
(assert_msg "check sclabind" (is_a sclabind class_class_binding))
(let ( (claobj (unsafe_get_field :cbind_class sclabind))
(namsymdata (normal_symbol_data sname ncx sloc))
(namstrdata
(instance class_nrep_datastring
:ndata_discrx (normal_predef discr_string ncx sloc "discr_string")
:nstr_string (unsafe_get_field :named_name sname)
))
(claslots (make_multiple discr_multiple (obj_len claobj)))
(cladata
(instance class_nrep_datainstance
:nrep_loc sloc
:ndata_name sname
:ndata_discrx (normal_predef class_class ncx sloc "class_class")
:ninst_hash (make_integerbox discr_integer (obj_hash claobj))
:ninst_predef spredef
:ninst_slots claslots
:ninst_objnum 'OBMAG_OBJECT
))
(ancseq (unsafe_get_field :class_ancestors claobj))
(:long nbanc (multiple_length ancseq))
(anctup (make_multiple discr_multiple nbanc))
(ancdata (instance
class_nrep_datatuple
:nrep_loc sloc
:ndata_name sname
:ndata_discrx (normal_predef discr_class_sequence ncx sloc "discr_class_sequence")
:ntup_comp anctup
))
(fldseq (unsafe_get_field :class_fields claobj))
(:long nbfld (multiple_length fldseq)) ;total number of fields
(:long nbownfld (multiple_length sfldbinds)) ;number of own fields
(:long nbsupfld (-i nbfld nbownfld)) ;number of super(ie inherited) fields
(:long ix 0) ;temporary index
(fldtup (make_multiple discr_multiple nbfld))
(flddata (instance
class_nrep_datatuple
:nrep_loc sloc
:ndata_name sname
:ndata_discrx (normal_predef discr_field_sequence ncx sloc "discr_field_sequence")
:ntup_comp fldtup
))
;; the data representing the superclass
(superdata (if (is_a superbind class_any_binding)
(normal_exp (unsafe_get_field :binder superbind) env ncx sloc)))
)
(assert_msg "check claobj" (is_a claobj class_class))
(add_nctx_data ncx cladata)
(add_nctx_data ncx namstrdata)
(add_nctx_data ncx ancdata)
(add_nctx_data ncx flddata)
(fill_data_slot cladata named_name namstrdata)
(fill_data_slot cladata class_ancestors ancdata)
(fill_data_slot cladata class_fields flddata)
(assert_msg "check sclabind" (is_a sclabind class_class_binding))
(unsafe_put_fields sclabind
:fixbind_data cladata
)
;; for each field which is not own, make a data to copy it from the superclass
(setq ix 0)
(forever loopsuperfield
(if (>=i ix nbsupfld) (exit loopsuperfield))
(let ( (supfldata
(instance class_nrep_multacc
:nrep_loc sloc
:naccm_mul
(instance class_nrep_fieldacc
:nrep_loc sloc
:naccf_obj superdata
:naccf_fld class_fields
)
:naccm_ix (make_integerbox discr_integer ix)
))
)
(multiple_put_nth fldtup ix supfldata)
)
(setq ix (+i ix 1))
)
(setq ix 0)
;; for each own field, make an instance of it
(forever loopownfield
(if (>=i ix nbownfld) (exit loopownfield))
(let ( (ownfldbind (multiple_nth sfldbinds ix)) )
(assert_msg "check ownfldbind" (is_a ownfldbind class_field_binding))
(let ( (ownfldsym (unsafe_get_field :binder ownfldbind))
(ownfld (unsafe_get_field :flbind_field ownfldbind))
)
(assert_msg "check ownfldsym" (is_a ownfldsym class_symbol))
(assert_msg "check ownfld" (is_a ownfld class_field))
(let ( (ownfldsymdata (normal_symbol_data ownfldsym ncx sloc))
(ownfldslots (make_multiple discr_multiple (obj_len ownfld)))
(ownflstrdata
(instance class_nrep_datastring
:ndata_discrx (normal_predef discr_string ncx sloc "discr_string")
:nstr_string (unsafe_get_field :named_name ownfld)
))
(ownfldata (instance class_nrep_datainstance
:nrep_loc sloc
:ndata_name ownfldsym
:ndata_discrx (normal_predef class_field ncx sloc "class_field")
:ninst_hash (make_integerbox discr_integer (obj_hash ownfld))
:ninst_objnum (make_integerbox discr_integer (obj_num ownfld))
:ninst_slots ownfldslots))
)
(add_nctx_data ncx ownfldata)
(add_nctx_data ncx ownflstrdata)
(fill_data_slot ownfldata named_name ownflstrdata)
(fill_data_slot ownfldata fld_ownclass cladata)
(multiple_put_nth fldtup (+i ix nbsupfld) ownfldata)
;; fill the field binding with its compiled data
(unsafe_put_fields ownfldbind
:fixbind_data ownfldata
)
)))
(setq ix (+i ix 1))
)
;; set the disc_super field to the superclass
(if superdata (fill_data_slot cladata disc_super superdata))
;; compute the class_ancestors into anctup
(setq ix 0)
;; loop on the ancestors of the superclass
(forever loopancestorsuper
(if (>=i ix (-i nbanc 1)) (exit loopancestorsuper))
(let ( (supancdata
(instance class_nrep_multacc
:nrep_loc sloc
:naccm_mul
(instance class_nrep_fieldacc
:nrep_loc sloc
:naccf_obj superdata
:naccf_fld class_ancestors)
:naccm_ix (make_integerbox discr_integer ix))) )
(multiple_put_nth anctup ix supancdata)
)
(setq ix (+i ix 1))
)
;; add the superdata as the last component of anctup
(if superdata (multiple_put_nth anctup (-i nbanc 1) superdata))
;; the normalized form of the defclass is the classdata
cladata
)))
(install_method class_source_defclass normal_exp normexp_defclass)
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; normalize a DEFINSTANCE
(defun normexp_definstance (recv env ncx psloc)
(assert_msg "check definstance recv" (is_a recv class_source_definstance))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_definstance recv")
(let ( (sloc (unsafe_get_field :loca_location recv))
(sname (unsafe_get_field :sdef_name recv))
(spredef (unsafe_get_field :sobj_predef recv))
(sdocstr (unsafe_get_field :sdef_doc recv))
(sinstclass (unsafe_get_field :sinst_class recv))
(sinstclabnd (unsafe_get_field :sinst_clabind recv))
(sinstclasym (if (is_a sinstclabnd class_any_binding) (unsafe_get_field :binder sinstclabnd)))
(sinstobjnum (unsafe_get_field :sinst_objnum recv))
(sinstfields (unsafe_get_field :sinst_fields recv))
(nbindlist (make_list discr_list))
(nbindbox (make_box discr_box nbindlist))
(bindins (find_env env sname))
(namdata (normal_symbol_data sname ncx sloc))
;; data representing the class
(icladata (if (is_a sinstclasym class_symbol)
(normal_exp sinstclasym env ncx sloc)))
)
(debug_msg bindins "normexp_definstance bindins")
(assert_msg "check bindins" (is_a bindins class_instance_binding))
(if (not (is_a icladata class_nrep))
(progn
(error_strv sloc "invalid class in definstance" (unsafe_get_field :named_name sname))
(return ())))
(cond
( (null spredef) () )
( (is_integerbox spredef) () )
( (is_a spredef class_symbol) () )
(:else
(error_strv sloc "bad predef in DEFINSTANCE" (unsafe_get_field :named_name sname))
(return ())))
(assert_msg "check sinstclass" (is_a sinstclass class_class))
(assert_msg "check sinstclasym" (is_a sinstclasym class_symbol))
(let (
(slotup (make_multiple discr_multiple
(multiple_length (unsafe_get_field :class_fields sinstclass))))
(insdata (instance class_nrep_datainstance
:nrep_loc sloc
:ndata_name sname
:ndata_discrx icladata
:ninst_hash (make_integerbox discr_integer (nonzero_hash))
:ninst_predef spredef
:ninst_slots slotup
:ninst_objnum sinstobjnum
))
)
(add_nctx_data ncx insdata)
(put_env env bindins)
(unsafe_put_fields bindins
:fixbind_data insdata
)
;; scan the fields initialization
(multiple_every
sinstfields
(lambda (flda :long ix)
(debug_msg flda "normexp_definstance flda")
(assert_msg "check flda" (is_a flda class_source_fieldassign))
(let ( (curfld (unsafe_get_field :sfla_field flda))
(curexp (unsafe_get_field :sfla_expr flda))
(:long curoff (obj_num curfld))
)
(assert_msg "check curfld" (is_a curfld class_field))
(assert_msg "good curfld"
(== (multiple_nth (unsafe_get_field :class_fields sinstclass) curoff)
curfld))
(debug_msg curexp "normexp_definstance field curexp")
(multicall
(ncur nbindcur)
(normal_exp curexp env ncx sloc)
(debug_msg ncur "normexp_definstance field ncur")
(multiple_put_nth slotup curoff ncur)
(if (is_list nbindcur)
(let ( (thebindlist (box_content nbindbox)))
(setq thebindlist (list_append2list thebindlist nbindcur))
(box_put nbindbox thebindlist)))))))
;;; put the binding into the data
(let ( (thebindlist (box_content nbindbox))
(nbindtup (list_to_multiple thebindlist discr_multiple))
)
(if (>i (multiple_length nbindtup) 0)
(unsafe_put_fields insdata :ndata_locbind nbindtup))
;; return the data
(debug_msg insdata "normexp_definstance return insdata")
insdata
))))
(install_method class_source_definstance normal_exp normexp_definstance)
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; normalize a DEFSELECTOR
(defun normexp_defselector (recv env ncx psloc)
(assert_msg "check defselector recv" (is_a recv class_source_defselector))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp defselector recv")
(let (
(sloc (unsafe_get_field :loca_location recv))
(sname (unsafe_get_field :sdef_name recv))
(spredef (unsafe_get_field :sobj_predef recv))
(sdocstr (unsafe_get_field :sdef_doc recv))
(sinstclass (unsafe_get_field :sinst_class recv))
(sinstclabnd (unsafe_get_field :sinst_clabind recv))
(sinstclasym (if (is_a sinstclabnd class_any_binding) (unsafe_get_field :binder sinstclabnd)))
(sinstobjnum (unsafe_get_field :sinst_objnum recv))
(sinstfields (unsafe_get_field :sinst_fields recv))
(sformals (unsafe_get_field :sdefsel_formals recv))
(nbindlist (make_list discr_list))
(nbindbox (make_box discr_box nbindlist))
(bindsel (find_env env sname))
(namdata (normal_symbol_data sname ncx sloc))
;; data representing the class
(icladata (if (is_a sinstclasym class_symbol)
(normal_exp sinstclasym env ncx sloc)))
)
(assert_msg "check bindsel" (is_a bindsel class_selector_binding))
(if (not (is_a icladata class_nrep))
(progn
(error_strv sloc "invalid class in defselector" (unsafe_get_field :named_name sname))
(return ())))
(if spredef
(if (not (or (is_integerbox spredef) (is_a spredef class_symbol)))
(progn (error_strv sloc "bad predef in defselector" (unsafe_get_field :named_name sname))
(return ()))))
(assert_msg "check sinstclass" (is_a sinstclass class_class))
(assert_msg "check sinstclasym" (is_a sinstclasym class_symbol))
(assert_msg "check sname" (is_a sname class_symbol))
(let (
(slotup (make_multiple discr_multiple
(multiple_length (unsafe_get_field :class_fields sinstclass))))
(namstrdata (instance class_nrep_datastring
:nrep_loc sloc
:ndata_discrx (normal_predef discr_string ncx sloc "discr_string")
:nstr_string (unsafe_get_field :named_name sname)))
(insdata (instance class_nrep_datainstance
:nrep_loc sloc
:ndata_name sname
:ndata_discrx icladata
:ninst_hash (make_integerbox discr_integer (nonzero_hash))
:ninst_predef spredef
:ninst_slots slotup
:ninst_objnum sinstobjnum
))
(formsymbmap (make_mapobject discr_map_objects (+i 3 (*i (multiple_length sformals) 2))))
(nformtup (if sformals
(let ( (:long nbformals (multiple_length sformals))
(nformaltuple (make_multiple discr_multiple nbformals))
(nformdata (instance
class_nrep_datatuple
:nrep_loc sloc
:ndata_name sname
:ndata_discrx (normal_predef discr_multiple ncx sloc "discr_multiple")
:ntup_comp nformaltuple))
)
(fill_normal_formals sformals nformaltuple formsymbmap env ncx sloc)
(add_nctx_data ncx nformdata)
(fill_data_slot insdata sdefsel_formals nformdata)
nformaltuple
)))
)
(add_nctx_data ncx insdata)
(add_nctx_data ncx namstrdata)
(unsafe_put_fields bindsel
:fixbind_data insdata
)
;; scan the fields initialization
(multiple_every
sinstfields
(lambda (flda :long ix)
(assert_msg "check flda" (is_a flda class_source_fieldassign))
(let ( (curfld (unsafe_get_field :sfla_field flda))
(curexp (unsafe_get_field :sfla_expr flda))
(:long curoff (obj_num curfld))
)
(assert_msg "check curfld" (is_a curfld class_field))
(assert_msg "good curfld"
(== (multiple_nth (unsafe_get_field :class_fields sinstclass) curoff)
curfld))
(multicall
(ncur nbindcur)
(normal_exp curexp env ncx sloc)
(multiple_put_nth slotup curoff ncur)
(if (is_list nbindcur)
(let ( (thebindlist (box_content nbindbox)))
(setq thebindlist (list_append2list thebindlist nbindcur))
(box_put nbindbox thebindlist)))))))
;;; put the binding into the data
(let ( (thebindlist (box_content nbindbox))
(nbindtup (list_to_multiple thebindlist discr_multiple))
)
(if (>i (multiple_length nbindtup) 0)
(unsafe_put_fields insdata :ndata_locbind nbindtup))
;; force the name of the selectordata
(multiple_put_nth slotup (get_int named_name) namstrdata)
(if (is_a bindsel class_selector_binding)
(unsafe_put_fields bindsel
:fixbind_data insdata
))
;; return the data
(debug_msg insdata "normexp_defselector return insdata")
insdata
))))
(install_method class_source_defselector normal_exp normexp_defselector)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; (export_values v ...)
;; is equivalent to
;;;;;; let _valuexporter = the_system_data.sysdata_value_exporter
;;;;;; then _quotev = 'v
;;;;;; then _norm_v = v
;;;;;; then _curenv = (current_module_environment)
;;;;;; then :void _exported_v = _valueexporter _quotev _norm_v
;;;;;; in _exported_v
;; where _valuexporter _quotev _norm_v _exported_v are fresh variables
;; and the_system_data is accessed using predef-s
;;; internal function giving a normalized cloned symbol for the value exporter
;;; return only one result, and update the bindslist
(defun normal_value_exporter (env ncx psloc bindslist)
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check bindslist" (is_list bindslist))
(assert_msg "check ncx" (is_a ncx class_normalization_context))
(let ( (csymvalexp (clone_symbol '_valuexporter_))
(nsysdat (instance class_nrep_predef
:nrep_loc psloc
:nrpredef 'INITIAL_SYSTEM_DATA))
(cbind (instance class_normal_let_binding
:letbind_loc psloc
:binder csymvalexp
:letbind_type ctype_value
:letbind_expr (instance class_nrep_unsafe_get_field
:nrep_loc psloc
:nuget_obj nsysdat
:nuget_field sysdata_value_exporter)))
(locsym (instance class_nrep_locsymocc
:nrep_loc psloc
:nocc_ctyp ctype_value
:nocc_symb csymvalexp
:nocc_bind cbind))
)
(list_append bindslist cbind)
(return locsym)
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; internal function for value export
;;
;;; sym is the exported symbol
;;
;;; nexp is the normalized expression of its value
;;
;;; nvalueexp is the normalized expression of the value exporter given
;;; by normal_value_exporter above
(defun normal_exported_value (sym nexp nvaluexp env ncx psloc bindslist)
(debug_msg sym "normal_exported_value start sym")
(debug_msg nexp "normal_exported_value start nexp")
(debug_msg nvaluexp "normal_exported_value start nvaluexp")
(assert_msg "check sym" (is_a sym class_symbol))
(assert_msg "check nexp" (is_a nexp class_nrep))
(assert_msg "check nvaluexp" (is_a nvaluexp class_nrep))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check ncx" (is_a ncx class_normalization_context))
(assert_msg "check bindslist" (is_list bindslist))
(let ( (csymexpo (clone_symbol '_exported_))
(symdata (normal_symbol_data sym ncx psloc))
(iniproc (unsafe_get_field :nctx_initproc ncx))
(curproc (unsafe_get_field :nctx_curproc ncx))
(csbuf (let ( (sb (make_strbuf discr_strbuf)) )
(add2sbuf_strconst sb "norm.exp.val : ")
(add2sbuf_string sb (unsafe_get_field :named_name sym))
sb))
(scurenvbox (instance class_source_current_module_environment_container
:loca_location psloc
:cmec_comment (strbuf2string discr_string csbuf)))
)
(debug_msg scurenvbox "normal_exported_value scurenvbox")
(multicall
(ncurenvbox curenvbinds)
(normal_exp scurenvbox env ncx psloc)
(debug_msg ncurenvbox "normal_exported_value ncurenvbox")
(debug_msg curenvbinds "normal_exported_value curenvbinds")
;;; if we are not at topevel it has no sense to call the cont_fresh_env!
(list_append2list bindslist curenvbinds)
(let (
(argtup (tuple symdata nexp ncurenvbox))
(cbind (instance class_normal_let_binding
:letbind_loc psloc
:binder csymexpo
:letbind_type ctype_void
:letbind_expr (instance class_nrep_apply
:nrep_loc psloc
:nexpr_ctyp ctype_value
:napp_fun nvaluexp
:nexpr_args argtup
)))
(syocc (instance class_nrep_locsymocc
:nrep_loc psloc
:nocc_ctyp ctype_void
:nocc_symb csymexpo
:nocc_bind cbind) )
)
(debug_msg cbind "normal_exported_value cbind")
(debug_msg syocc "normal_exported_value return syocc")
(list_append bindslist cbind)
syocc
;;
))))
;;;; normalize an export_values
(defun normexp_export_values (recv env ncx psloc)
;; actually, export of values & classes could be simple. the
;; export of a symbol should be expanded as the invocation of the
;; value exporter on the current module environment. we don't need
;; anything special in the initproc for the export.
;;;; this implies that a locally let-bound symbol could be passed to
;;;; export_values, some kind of strange practice.
(assert_msg "check exportval recv" (is_a recv class_source_export_values))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_export_values recv")
(let (
(sloc (unsafe_get_field :loca_location recv))
(sxnames (unsafe_get_field :sexport_names recv))
(bindslist (make_list discr_list))
(nvaluexporter (normal_value_exporter env ncx sloc bindslist))
(nilnrep (instance class_nrep_nil
:nrep_loc sloc))
)
(debug_msg nvaluexporter "normexp_export_values nvaluexporter")
(multiple_every
sxnames
(lambda (xnam :long ix)
(assert_msg "check xnam" (is_a xnam class_symbol))
(debug_msg xnam "normexp_export_values xnam")
(multicall
(nsym nsymbinds)
(normal_exp xnam env ncx sloc)
(debug_msg nsym "normexp_export_values nsym")
(debug_msg nsymbinds "normexp_export_values nsymbinds")
(list_append2list bindslist nsymbinds)
(debug_msg xnam "normexp_export_values xnam again")
(debug_msg bindslist "normexp_export_values before normal_exported_value bindslist")
(let ( (nexpv (normal_exported_value xnam nsym nvaluexporter env ncx sloc bindslist))
)
(debug_msg nexpv "normexp_export_values normal_exported_value gave nexpv")
))))
(debug_msg nilnrep "normexp_export_values final nilnrep")
(debug_msg bindslist "normexp_export_values final bindslist")
(return nilnrep bindslist)
))
(install_method class_source_export_values normal_exp normexp_export_values)
;;;; normalize an export_synonym
(defun normexp_export_synonym (recv env ncx psloc)
;; it should be a bit like export_values, since it create a value binding..
(assert_msg "check exportsyn recv" (is_a recv class_source_export_synonym))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_export_synonym recv")
(let (
(sloc (unsafe_get_field :loca_location recv))
(newname (unsafe_get_field :sexpsyn_newname recv))
(oldname (unsafe_get_field :sexpsyn_oldname recv))
(bindslist (make_list discr_list))
(nvaluexporter (normal_value_exporter env ncx sloc bindslist))
(nilnrep (instance class_nrep_nil
:nrep_loc sloc))
)
(debug_msg nvaluexporter "normexp_export_synonym nvaluexporter")
(debug_msg newname "normexp_export_synonym newname")
(debug_msg oldname "normexp_export_synonym oldname")
(assert_msg "check newname" (is_a newname class_symbol))
(assert_msg "check oldname" (is_a oldname class_symbol))
;; when a class has a synonym, we should generate in the C code
;;; const int meltclasslen__<SYNONYMNAME>
;; when a field has a synonym, we should generate in the C code
;;; const int meltfieldoff__<SYNONYMNAME>
(compile_warning "export_synonym should also generate a class length or field offset when needed")
(multicall
(noldsym noldsymbinds)
(normal_exp oldname env ncx sloc)
(debug_msg noldsym "normexp_export_synonym noldsym")
(debug_msg noldsymbinds "normexp_export_synonym noldsymbinds")
(list_append2list bindslist noldsymbinds)
(let ( (nexpv (normal_exported_value newname noldsym nvaluexporter env ncx sloc bindslist))
)
(debug_msg nexpv "normexp_export_synonym normal_exported_value gave nexpv")
(debug_msg nilnrep "normexp_export_synonym final nilnrep")
(debug_msg bindslist "normexp_export_synonym final bindslist")
(return nilnrep bindslist)
))))
(install_method class_source_export_synonym normal_exp normexp_export_synonym)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; normalize an export_class
(defun normexp_export_class (recv env ncx psloc)
(assert_msg "check export_class recv" (is_a recv class_source_export_class))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp export_class recv")
(let (
(sloc (unsafe_get_field :loca_location recv))
(sxnames (unsafe_get_field :sexport_names recv))
(bindslist (make_list discr_list))
(nvaluexporter (normal_value_exporter env ncx sloc bindslist))
(nilnrep (instance class_nrep_nil
:nrep_loc sloc))
(mocx (unsafe_get_field :nctx_modulcontext ncx))
)
(assert_msg "check mocx" (is_a mocx class_module_context))
(multiple_every
sxnames
(lambda (xnam :long ix)
(assert_msg "check xnam" (is_a xnam class_symbol))
(let ( (xbind (find_env env xnam))
(xsymdata (normal_symbol_data xnam ncx sloc))
)
(debug_msg xbind "normexp_export_class xbind")
(assert_msg "check xsymdata" (is_a xsymdata class_nrep_datasymbol))
(if (is_not_a xbind class_class_binding)
(progn
(error_strv sloc "EXPORT_CLASS with non-class-bound symbol"
(unsafe_get_field :named_name xnam))
(return)
))
(let ( (xdata (unsafe_get_field :fixbind_data xbind))
(xclass (unsafe_get_field :cbind_class xbind))
)
(debug_msg xdata "normexp_export_class xdata")
(if (is_not_a xdata class_nrep_data)
(error_strv sloc "forward defined class symbol to EXPORT_CLASS"
(unsafe_get_field :named_name xnam)))
(let ( (nclav (normal_exported_value xnam xdata nvaluexporter env ncx sloc bindslist))
)
(debug_msg nclav "normexp_export_class nclav")
(assert_msg "check xclass" (is_a xclass class_class))
(let ( (xclfields (unsafe_get_field :class_fields xclass))
;; temporarily, to be able to melt bootstrap, we
;; have to check the object length of mocx
(expcladic
(if (>i (object_length mocx) (get_int mocx_expclassdict))
(unsafe_get_field :mocx_expclassdict mocx)))
(expfldic
(if (>i (object_length mocx) (get_int mocx_expfieldict))
(unsafe_get_field :mocx_expfieldict mocx)))
)
(mapstring_putstr expcladic (unsafe_get_field :named_name xclass) xclass)
(multiple_every
xclfields
(lambda (curfld :long ix)
(assert_msg "check curfld" (is_a curfld class_field))
(let ( (fldnam (unsafe_get_field :named_name curfld))
(fldclass (unsafe_get_field :fld_ownclass curfld)) )
;; export as value each field belonging to this class
(if (== fldclass xclass)
(let ( (fldsym (get_symbolstr fldnam))
(fldbind (find_env env fldsym))
)
(assert_msg "check fldbind" (is_a fldbind class_field_binding))
(let ( (fldata (unsafe_get_field :fixbind_data fldbind))
(field (unsafe_get_field :flbind_field fldbind))
(nfld (normal_exported_value fldsym fldata nvaluexporter env ncx sloc bindslist))
)
(assert_msg "check field" (is_a field class_field))
(mapstring_putstr expfldic (unsafe_get_field :named_name field) field)
(debug_msg nfld "normexp_export_class nfld")
))))))))))
))
(debug_msg nilnrep "normexp_export_class final nilnrep")
(debug_msg bindslist "normexp_export_class final bindslist")
(return nilnrep bindslist)
))
(install_method class_source_export_class normal_exp normexp_export_class)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; internal function giving a normalized cloned symbol for the macro exporter
;;; return only one result, and update the bindslist
(defun normal_macro_exporter (env ncx psloc bindslist)
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check bindslist" (is_list bindslist))
(assert_msg "check ncx" (is_a ncx class_normalization_context))
(debug_msg psloc "normal_macro_exporter start psloc")
(let ( (csymvalexp (clone_symbol '_macroxporter_))
(nsysdat (instance class_nrep_predef
:nrep_loc psloc
:nrpredef 'INITIAL_SYSTEM_DATA))
(cbind (instance class_normal_let_binding
:letbind_loc psloc
:binder csymvalexp
:letbind_type ctype_value
:letbind_expr (instance class_nrep_unsafe_get_field
:nrep_loc psloc
:nuget_obj nsysdat
:nuget_field sysdata_macro_exporter)))
(locsym (instance class_nrep_locsymocc
:nrep_loc psloc
:nocc_ctyp ctype_value
:nocc_symb csymvalexp
:nocc_bind cbind))
)
(list_append bindslist cbind)
(debug_msg locsym "normal_macro_exporter return locsym")
(return locsym)
))
;;; internal function giving a normalized cloned symbol for the patmacro exporter
;;; return only one result, and update the bindslist
(defun normal_patmacro_exporter (env ncx psloc bindslist)
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check bindslist" (is_list bindslist))
(assert_msg "check ncx" (is_a ncx class_normalization_context))
(debug_msg psloc "normal_patmacro_exporter start psloc")
(let ( (csymvalexp (clone_symbol '_patmacroxporter_))
(nsysdat (instance class_nrep_predef
:nrep_loc psloc
:nrpredef 'INITIAL_SYSTEM_DATA))
(cbind (instance class_normal_let_binding
:letbind_loc psloc
:binder csymvalexp
:letbind_type ctype_value
:letbind_expr (instance class_nrep_unsafe_get_field
:nrep_loc psloc
:nuget_obj nsysdat
:nuget_field sysdata_patmacro_exporter)))
(locsym (instance class_nrep_locsymocc
:nrep_loc psloc
:nocc_ctyp ctype_value
:nocc_symb csymvalexp
:nocc_bind cbind))
)
(list_append bindslist cbind)
(debug_msg locsym "normal_patmacro_exporter return locsym")
(return locsym)
))
;;;;;;;;;;;;;;;;
;; return the normalized application to do the macro expport
(defun normal_exported_macro (sym nexp nmacroxp env ncx psloc bindslist)
(debug_msg sym "normal_exported_macro start sym")
(debug_msg nexp "normal_exported_macro start nexp")
(debug_msg nmacroxp "normal_exported_macro start nmacroxp")
(assert_msg "check sym" (is_a sym class_symbol))
(assert_msg "check nexp" (is_a nexp class_nrep))
(assert_msg "check nmacroxp" (is_a nmacroxp class_nrep))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check ncx" (is_a ncx class_normalization_context))
(assert_msg "check bindslist" (is_list bindslist))
(let ( (csymexpo (clone_symbol '_exportedm_))
(symdata (normal_symbol_data sym ncx psloc))
(csbuf (let ( (sb (make_strbuf discr_strbuf)) )
(add2sbuf_strconst sb "norm.exp.val : ")
(add2sbuf_string sb (unsafe_get_field :named_name sym))
sb))
(scurenvbox (instance class_source_current_module_environment_container
:loca_location psloc
:cmec_comment (strbuf2string discr_string csbuf)))
)
(debug_msg scurenvbox "normal_exported_macro sgetcurenvbox")
(multicall
(ncurenvbox curenvbinds)
(normal_exp scurenvbox env ncx psloc)
(debug_msg ncurenvbox "normal_exported_macro ncurenvbox")
(debug_msg curenvbinds "normal_exported_macro curenvbinds")
(if (is_list curenvbinds) (list_append2list bindslist curenvbinds))
(let (
(argtup (tuple symdata nexp ncurenvbox))
(cbind (instance class_normal_let_binding
:letbind_loc psloc
:binder csymexpo
:letbind_type ctype_void
:letbind_expr (instance class_nrep_apply
:nrep_loc psloc
:nexpr_ctyp ctype_void
:napp_fun nmacroxp
:nexpr_args argtup
)))
(syocc (instance class_nrep_locsymocc
:nrep_loc psloc
:nocc_ctyp ctype_void
:nocc_symb csymexpo
:nocc_bind cbind) )
)
(debug_msg cbind "normal_exported_macro cbind")
(debug_msg syocc "normal_exported_macro return syocc")
(list_append bindslist cbind)
syocc
;;
))))
;;;; normalize an export_macro
(defun normexp_export_macro (recv env ncx psloc)
(assert_msg "check export_macro recv" (is_a recv class_source_export_macro))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp export_macro recv")
(let (
(sloc (unsafe_get_field :loca_location recv))
(mname (unsafe_get_field :sexpmac_mname recv))
(mvalexp (unsafe_get_field :sexpmac_mval recv))
(bindslist (make_list discr_list))
(nmacexp (normal_macro_exporter env ncx sloc bindslist))
(nrepnil (instance class_nrep_nil :nrep_loc sloc))
)
(assert_msg "check mname" (is_a mname class_symbol))
(multicall
(nexp nbinds)
(normal_exp mvalexp env ncx sloc)
(list_append2list bindslist nbinds)
(let (
(nexpm (normal_exported_macro mname nexp nmacexp env ncx sloc bindslist))
)
(debug_msg nexpm "normexp_export_macro nexpm")
)
(debug_msg nrepnil "normexp_export_macro final nrepnil")
(debug_msg bindslist "normexp_export_macro final bindslist")
(return nrepnil bindslist)
)))
(install_method class_source_export_macro normal_exp normexp_export_macro)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; return the normalized application to do the patmacro expport
;; sym is the symbol which is export_patmacro-ed
;; nexp is the nrep of the patmatchexporter
;; nmacroxp is the nrep of the macro expander
;; npattxp is the nrep of the pattern expander
(defun normal_exported_patmacro (sym nexp nmacroxp npattxp env ncx psloc bindslist)
(debug_msg sym "normal_exported_patmacro start sym")
(debug_msg nexp "normal_exported_patmacro start nexp")
(debug_msg nmacroxp "normal_exported_patmacro start nmacroxp")
(debug_msg npattxp "normal_exported_patmacro start npattxp")
(assert_msg "check sym" (is_a sym class_symbol))
(assert_msg "check nexp" (is_a nexp class_nrep))
(assert_msg "check nmacroxp" (is_a nmacroxp class_nrep))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check ncx" (is_a ncx class_normalization_context))
(assert_msg "check bindslist" (is_list bindslist))
(let ( (csymexpo (clone_symbol '_exportedpat_))
(symdata (normal_symbol_data sym ncx psloc))
(csbuf (let ( (sb (make_strbuf discr_strbuf)) )
(add2sbuf_strconst sb "norm.exp.pat : ")
(add2sbuf_string sb (unsafe_get_field :named_name sym))
sb))
(scurenvbox (instance class_source_current_module_environment_container
:loca_location psloc
:cmec_comment (strbuf2string discr_string csbuf)))
)
(debug_msg scurenvbox "normal_exported_patmacro sgetcurenvbox")
(multicall
(ncurenvbox curenvbinds)
(normal_exp scurenvbox env ncx psloc)
(debug_msg ncurenvbox "normal_exported_patmacro ncurenvbox")
(debug_msg curenvbinds "normal_exported_patmacro curenvbinds")
(if (is_list curenvbinds) (list_append2list bindslist curenvbinds))
(let (
(argtup (tuple symdata nmacroxp npattxp ncurenvbox))
(cbind (instance class_normal_let_binding
:letbind_loc psloc
:binder csymexpo
:letbind_type ctype_void
:letbind_expr (instance class_nrep_apply
:nrep_loc psloc
:nexpr_ctyp ctype_void
:napp_fun nexp
:nexpr_args argtup
)))
(syocc (instance class_nrep_locsymocc
:nrep_loc psloc
:nocc_ctyp ctype_void
:nocc_symb csymexpo
:nocc_bind cbind) )
)
(debug_msg cbind "normal_exported_patmacro cbind")
(debug_msg syocc "normal_exported_patmacro return syocc")
(list_append bindslist cbind)
syocc
;;
))))
;;;;;;;;;;;;;;;;
;;;; normalize an export_patmacro
(defun normexp_export_patmacro (recv env ncx psloc)
(assert_msg "check export_patmacro recv" (is_a recv class_source_export_patmacro))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp export_patmacro recv")
(let (
(sloc (unsafe_get_field :loca_location recv))
(mname (unsafe_get_field :sexpmac_mname recv))
(mvalexp (unsafe_get_field :sexpmac_mval recv))
(mpatexp (unsafe_get_field :sexppat_pval recv))
(bindslist (make_list discr_list))
(npmaexp (normal_patmacro_exporter env ncx sloc bindslist))
(nrepnil (instance class_nrep_nil :nrep_loc sloc))
)
(debug_msg mname "normexp export_patmacro mname")
(debug_msg npmaexp "normexp export_patmacro npmaexp")
(assert_msg "check mname" (is_a mname class_symbol))
(multicall
(nexpmac nbindms)
(normal_exp mvalexp env ncx sloc)
(list_append2list bindslist nbindms)
(debug_msg nexpmac "normexp_export_patmacro nexpmac")
(multicall
(nexppat nbindps)
(normal_exp mpatexp env ncx sloc)
(list_append2list bindslist nbindps)
(debug_msg nexpmac "normexp_export_patmacro nexppat")
(let ( (nexpm (normal_exported_patmacro mname npmaexp nexpmac nexppat env ncx sloc bindslist)) )
(debug_msg nexpm "normexp_export_patmacro nexpm")
(debug_msg nrepnil "normexp_export_macro final nrepnil")
(debug_msg bindslist "normexp_export_macro final bindslist")
(return nrepnil bindslist)
)
))
))
(install_method class_source_export_patmacro normal_exp normexp_export_patmacro)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; normalize current_module_environment_container
(defun normexp_current_module_environment_container (recv env ncx psloc)
(assert_msg "check current_module_environment_container recv" (is_a recv class_source_current_module_environment_container))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_current_module_environment_container recv")
(let (
(sloc (unsafe_get_field :loca_location recv))
(scomm (unsafe_get_field :cmec_comment recv))
(ncurmodenvlist (unsafe_get_field :nctx_procurmodenvlist ncx))
(curproc (unsafe_get_field :nctx_curproc ncx))
(qdatcur (unsafe_get_field :nctx_qdatcurmodenvbox ncx))
(nquasi (instance class_nrep_quasiconst_current_module_environment_container
:nrep_loc sloc
:nconst_sval recv
:nconst_proc curproc
:nconst_data qdatcur
:nqcmec_comment scomm
))
)
(assert_msg "check qdatcur" (is_a qdatcur class_nrep_quasidata_current_module_environment_container))
(if (is_a curproc class_nrep_routproc)
(progn
(list_append (unsafe_get_field :nrpro_const curproc) qdatcur)
(list_append ncurmodenvlist curproc)
))
(debug_msg nquasi "normexp_current_module_environment_container nquasi")
(return nquasi)
))
(install_method class_source_current_module_environment_container normal_exp normexp_current_module_environment_container)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; normalize parent_module_environment
(defun normexp_parent_module_environment (recv env ncx psloc)
(assert_msg "check parent_module_environment recv" (is_a recv class_source_parent_module_environment))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_parent_module_environment recv")
(let (
(sloc (unsafe_get_field :loca_location recv))
(curproc (unsafe_get_field :nctx_curproc ncx))
(qdatpar (unsafe_get_field :nctx_qdatparmodenv ncx))
(nquasi (instance class_nrep_quasiconst_parent_module_environment
:nrep_loc sloc
:nconst_sval recv
:nconst_proc curproc
:nconst_data qdatpar
))
)
(assert_msg "check qdatpar" (is_a qdatpar class_nrep_quasidata_parent_module_environment))
(if (is_a curproc class_nrep_routproc)
(list_append (unsafe_get_field :nrpro_const curproc) qdatpar))
(debug_msg nquasi "normexp_parent_module_environment nquasi")
(return nquasi)
))
(install_method class_source_parent_module_environment normal_exp normexp_parent_module_environment)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; normalize update_current_module_environment_container
(defun normexp_update_current_module_environment_container (recv env ncx psloc)
(assert_msg "check update_current_module_environment_container recv" (is_a recv class_source_update_current_module_environment_container))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_update_current_module_environment_container recv")
(let (
(sloc (unsafe_get_field :loca_location recv))
(scomm (unsafe_get_field :sucme_comment recv))
(curproc (unsafe_get_field :nctx_curproc ncx))
(iniproc (unsafe_get_field :nctx_initproc ncx))
(modctx (unsafe_get_field :nctx_modulcontext ncx))
(modnam (if (is_a modctx class_module_context) (unsafe_get_field :mocx_modulename modctx)))
)
(if (!= curproc iniproc)
(progn
(error_plain sloc "(UPDATE_CURRENT_MODULE_ENVIRONMENT_CONTAINER) not at toplevel")
(return)))
(let ( (nup (instance class_nrep_update_current_module_environment_container
;; :ncumeb_expr filled later
:nrep_loc sloc
:ncumeb_comment scomm
))
(csym (clone_symbol 'updatcurmodenvbox_))
(cbind (instance
class_normal_let_binding
:letbind_loc sloc
:binder csym
:letbind_type ctype_void
;; ctype_void because the sideffect is in nup
:letbind_expr nup))
(clocc (instance
class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctype_void
:nocc_symb csym
:nocc_bind cbind))
(csbuf (let ( (sb (make_strbuf discr_strbuf)) )
(add2sbuf_strconst sb "cur.mod.env.cont : ")
(add2sbuf_string sb scomm)
sb))
(scurenvbox (instance class_source_current_module_environment_container
:loca_location sloc
:cmec_comment (strbuf2string discr_string csbuf)))
(sgetcurenvbox
(instance
class_source_or
:loca_location psloc
:sor_disj
(tuple
scurenvbox
(instance
class_source_apply
:loca_location psloc
:sapp_fun (instance
class_source_unsafe_get_field
:loca_location psloc
:suget_obj (instance
class_source_fetch_predefined
:loca_location psloc
:sfepd_predef 'initial_system_data
)
:suget_field sysdata_cont_fresh_env
)
:sargop_args (tuple
(instance class_source_parent_module_environment
:loca_location psloc)
modnam
)))))
)
(multicall
(ncurenvbox bindlist)
(normal_exp sgetcurenvbox env ncx sloc)
(list_append bindlist cbind)
(unsafe_put_fields nup :nucmeb_expr ncurenvbox)
(debug_msg bindlist "normexp_update_current_module_environment_container result bindlist")
(debug_msg clocc "normexp_update_current_module_environment_container result clocc")
(return clocc bindlist)
)
)))
(install_method class_source_update_current_module_environment_container normal_exp normexp_update_current_module_environment_container)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; normalize a fetch_predefined
(defun normexp_fetch_predefined (recv env ncx psloc)
(assert_msg "check fetch_predefined recv" (is_a recv class_source_fetch_predefined))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_fetch_predefined recv")
(let (
(sloc (unsafe_get_field :loca_location recv))
(spred (unsafe_get_field :sfepd_predef recv))
(predefmap (unsafe_get_field :nctx_predefmap ncx))
)
(cond ( (is_a spred class_symbol)
;; if the spred is a symbol, check that it is a value in the predefmap
(let ( (boxk (make_box discr_box ()))
)
(mapobject_every predefmap
(lambda (key val)
(if (== val spred) (box_put boxk key))))
(if (null (box_content boxk))
(warning_strv sloc "FETCH_PREDEFINED unknown predef name"
(unsafe_get_field :named_name spred)))
)
)
( (is_integerbox spred)
;; if spred is an integer, check it
(let ( (:long predrk (get_int spred)) )
(if (or (<=i predrk 0) (>=i predrk (last_globpredef_index)))
(warning_plain sloc "FETCH_PREDEFINED invalid predef rank"))
)
)
(:else
(assert_msg "FETCH_PREDEFINED bad predef" ()))
)
(let ( (npre (instance class_nrep_predef
:nrep_loc sloc
:nrpredef spred
))
)
(debug_msg npre "normexp_fetch_predefined result npre")
(return npre)
)))
(install_method class_source_fetch_predefined normal_exp normexp_fetch_predefined)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; normalize a store_predefined
(defun normexp_store_predefined (recv env ncx psloc)
(assert_msg "check store_predefined recv" (is_a recv class_source_store_predefined))
(assert_msg "check env" (is_a env class_environment))
(assert_msg "check nctxt" (is_a ncx class_normalization_context))
(debug_msg recv "normexp_store_predefined recv")
(let (
(sloc (unsafe_get_field :loca_location recv))
(spred (unsafe_get_field :sstpd_predef recv))
(sval (unsafe_get_field :sstpd_value recv))
(predefmap (unsafe_get_field :nctx_predefmap ncx))
)
(cond ( (is_a spred class_symbol)
;; if the spred is a symbol, check that it is a value in the predefmap
(let ( (boxk (make_box discr_box ()))
)
(mapobject_every predefmap
(lambda (key val)
(if (== val spred) (box_put boxk key))))
(if (null (box_content boxk))
(warning_strv sloc "STORE_PREDEFINED unknown predef name"
(unsafe_get_field :named_name spred)))
)
)
( (is_integerbox spred)
;; if spred is an integer, check it
(let ( (:long predrk (get_int spred)) )
(if (or (<=i predrk 0) (>=i predrk (last_globpredef_index)))
(warning_plain sloc "STORE_PREDEFINED invalid predef rank"))
)
)
(:else
(assert_msg "STORE_PREDEFINED bad predef" ()))
)
(multicall
(nval nbinds)
(normal_exp sval env ncx sloc)
(if (null nbinds)
(setq nbinds (make_list discr_list)))
(let ( (csym (clone_symbol '_storepredef_))
(nfpre (instance class_nrep_store_predefined
:nrep_loc sloc
:nstpd_predef spred
:nstpd_value nval))
(cbind (instance class_normal_let_binding
:binder csym
:letbind_type ctype_value
:letbind_expr nfpre))
(syocc (instance class_nrep_locsymocc
:nrep_loc sloc
:nocc_ctyp ctype_value
:nocc_symb csym
:nocc_bind cbind))
)
(list_append nbinds cbind)
(debug_msg nbinds "normexp_store_predefined result nbinds")
(debug_msg syocc "normexp_store_predefined result syocc")
(return syocc nbinds)
))))
(install_method class_source_store_predefined normal_exp normexp_store_predefined)
(export_values
check_ctype_nargs
compile_obj
create_normcontext
get_ctype
normal_exp
normal_letrec_constructive
normal_predef
normalize_tuple
prepare_constructor_binding
wrap_normal_let1
wrap_normal_letseq
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; eof warmelt-normal.melt
|