File: psyntax73.ss

package info (click to toggle)
gambc 4.2.8-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 54,404 kB
  • ctags: 10,660
  • sloc: ansic: 661,388; lisp: 143,554; sh: 3,531; makefile: 3,320; cpp: 2,143; perl: 1,730; sed: 498; java: 265
file content (4478 lines) | stat: -rw-r--r-- 185,827 bytes parent folder | download | duplicates (3)
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
;;; Portable implementation of syntax-case
;;; Extracted from Chez Scheme Version 7.3 (Feb 26, 2007)
;;; Authors: R. Kent Dybvig, Oscar Waddell, Bob Hieb, Carl Bruggeman

;;; ***************************************************************************
;;; *** Modified for Gambit 4.0 by Marc Feeley (April 3, 2007).             ***
;;; *** Look for "***" to see what was modified.                            ***
;;; ***************************************************************************

;;; Copyright (c) 1992-2002 Cadence Research Systems
;;; Permission to copy this software, in whole or in part, to use this
;;; software for any lawful purpose, and to redistribute this software
;;; is granted subject to the restriction that all copies made of this
;;; software must include this copyright notice in full.  This software
;;; is provided AS IS, with NO WARRANTY, EITHER EXPRESS OR IMPLIED,
;;; INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY
;;; OR FITNESS FOR ANY PARTICULAR PURPOSE.  IN NO EVENT SHALL THE
;;; AUTHORS BE LIABLE FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES OF ANY
;;; NATURE WHATSOEVER.

;;; Before attempting to port this code to a new implementation of
;;; Scheme, please read the notes below carefully.

;;; This file defines the syntax-case expander, sc-expand, and a set
;;; of associated syntactic forms and procedures.  Of these, the
;;; following are documented in The Scheme Programming Language,
;;; Third Edition (R. Kent Dybvig, MIT Press, 2003), which can be
;;; found online at http://www.scheme.com/tspl3/.  Most are also documented
;;; in the R4RS and draft R5RS.
;;;
;;;   bound-identifier=?
;;;   datum->syntax-object
;;;   define-syntax
;;;   fluid-let-syntax
;;;   free-identifier=?
;;;   generate-temporaries
;;;   identifier?
;;;   identifier-syntax
;;;   let-syntax
;;;   letrec-syntax
;;;   syntax
;;;   syntax-case
;;;   syntax-object->datum
;;;   syntax-rules
;;;   with-syntax
;;;
;;; All standard Scheme syntactic forms are supported by the expander
;;; or syntactic abstractions defined in this file.  Only the R4RS
;;; delay is omitted, since its expansion is implementation-dependent.

;;; Also defined are three forms that support modules: module, import,
;;; and import-only.  These are documented in the Chez Scheme User's
;;; Guide (R. Kent Dybvig, Cadence Research Systems, 1998), which can
;;; also be found online at http://www.scheme.com/csug/.  They are
;;; described briefly here as well.
    
;;; All are definitions and may appear where and only where other
;;; definitions may appear.  modules may be named:
;;;
;;;   (module id (ex ...) defn ... init ...)
;;;
;;; or anonymous:
;;;
;;;   (module (ex ...) defn ... init ...)
;;;
;;; The latter form is semantically equivalent to:
;;;
;;;   (module T (ex ...) defn ... init ...)
;;;   (import T)
;;;
;;; where T is a fresh identifier.
;;;
;;; In either form, each of the exports in (ex ...) is either an
;;; identifier or of the form (id ex ...).  In the former case, the
;;; single identifier ex is exported.  In the latter, the identifier
;;; id is exported and the exports ex ... are "implicitly" exported.
;;; This listing of implicit exports is useful only when id is a
;;; keyword bound to a transformer that expands into references to
;;; the listed implicit exports.  In the present implementation,
;;; listing of implicit exports is necessary only for top-level
;;; modules and allows the implementation to avoid placing all
;;; identifiers into the top-level environment where subsequent passes
;;; of the compiler will be unable to deal effectively with them.
;;;
;;; Named modules may be referenced in import statements, which
;;; always take one of the forms:
;;;
;;;   (import id)
;;;   (import-only id)
;;;
;;; id must name a module.  Each exported identifier becomes visible
;;; within the scope of the import form.  In the case of import-only,
;;; all other identifiers become invisible in the scope of the
;;; import-only form, except for those established by definitions
;;; that appear textually after the import-only form.

;;; import and import-only also support a variety of identifier
;;; selection and renaming forms: only, except, add-prefix,
;;; drop-prefix, rename, and alias.
;;;
;;;   (import (only m x y))
;;; 
;;; imports x and y (and nothing else) from m.
;;;
;;;   (import (except m x y))
;;; 
;;; imports all of m's imports except for x and y.
;;;
;;;   (import (add-prefix (only m x y) m:))
;;; 
;;; imports x and y as m:x and m:y.
;;;
;;;   (import (drop-prefix m foo:))
;;; 
;;; imports all of m's imports, dropping the common foo: prefix
;;; (which must appear on all of m's exports).
;;; 
;;;   (import (rename (except m a b) (m-c c) (m-d d)))
;;; 
;;; imports all of m's imports except for x and y, renaming c
;;; m-c and d m-d.
;;; 
;;;   (import (alias (except m a b) (m-c c) (m-d d)))
;;; 
;;; imports all of m's imports except for x and y, with additional
;;; aliases m-c for c and m-d for d.
;;; 
;;; multiple imports may be specified with one import form:
;;; 
;;;   (import (except m1 x) (only m2 x))
;;; 
;;; imports all of m1's exports except for x plus x from m2.

;;; Another form, meta, may be used as a prefix for any definition and
;;; causes any resulting variable bindings to be created at expansion
;;; time.  Meta variables (variables defined using meta) are available
;;; only at expansion time.  Meta definitions are often used to create
;;; data and helpers that can be shared by multiple macros, for example:

;;; (module (alpha beta)
;;;   (meta define key-error
;;;     (lambda (key)
;;;       (syntax-error key "invalid key")))
;;;   (meta define parse-keys
;;;     (lambda (keys)
;;;       (let f ((keys keys) (c #'white) (s 10))
;;;         (syntax-case keys (color size)
;;;           (() (list c s))
;;;           (((color c) . keys) (f #'keys #'c s))
;;;           (((size s) . keys) (f #'keys c #'s))
;;;           ((k . keys) (key-error #'k))))))
;;;   (define-syntax alpha
;;;     (lambda (x)
;;;       (syntax-case x ()
;;;         ((_ (k ...) <other stuff>)
;;;          (with-syntax (((c s) (parse-keys (syntax (k ...)))))
;;;            ---)))))
;;;   (define-syntax beta
;;;     (lambda (x)
;;;       (syntax-case x ()
;;;         ((_ (k ...) <other stuff>)
;;;          (with-syntax (((c s) (parse-keys (syntax (k ...)))))
;;;            ---))))))

;;; As with define-syntax rhs expressions, meta expressions can evaluate
;;; references only to identifiers whose values are (already) available
;;; in the compile-time environment, e.g., macros and meta variables.
;;; They can, however, like define-syntax rhs expressions, build syntax
;;; objects containing occurrences of any identifiers in their scope.

;;; meta definitions propagate through macro expansion, so one can write,
;;; for example:
;;; 
;;;   (module (a)
;;;     (meta define-structure (foo x))
;;;     (define-syntax a
;;;       (let ((q (make-foo (syntax 'q))))
;;;         (lambda (x)
;;;           (foo-x q)))))
;;;   a -> q
;;; 
;;; where define-record is a macro that expands into a set of defines.
;;; 
;;; It is also sometimes convenient to write
;;; 
;;;   (meta begin defn ...)
;;; 
;;; or
;;; 
;;;   (meta module {exports} defn ...)
;;; 
;;; to create groups of meta bindings.

;;; Another form, alias, is used to create aliases from one identifier
;;; to another.  This is used primarily to support the extended import
;;; syntaxes (add-prefix, drop-prefix, rename, and alias).

;;; (let ((x 3)) (alias y x) y) -> 3

;;; The remaining exports are listed below.  sc-expand, eval-when, and
;;; syntax-error are described in the Chez Scheme User's Guide.
;;;
;;;   (sc-expand datum)
;;;      if datum represents a valid expression, sc-expand returns an
;;;      expanded version of datum in a core language that includes no
;;;      syntactic abstractions.  The core language includes begin,
;;;      define, if, lambda, letrec, quote, and set!.
;;;   (eval-when situations expr ...)
;;;      conditionally evaluates expr ... at compile-time or run-time
;;;      depending upon situations
;;;   (syntax-error object message)
;;;      used to report errors found during expansion
;;;   ($syntax-dispatch e p)
;;;      used by expanded code to handle syntax-case matching
;;;   ($sc-put-cte symbol val top-token)
;;;      used to establish top-level compile-time (expand-time) bindings.

;;; The following nonstandard procedures must be provided by the
;;; implementation for this code to run.
;;;
;;; (void)
;;; returns the implementation's cannonical "unspecified value".  The
;;; following usually works:
;;;
;;; (define void (lambda () (if #f #f))).
;;;
;;; (andmap proc list1 list2 ...)
;;; returns true if proc returns true when applied to each element of list1
;;; along with the corresponding elements of list2 ....  The following
;;; definition works but does no error checking:
;;;
;;; (define andmap
;;;   (lambda (f first . rest)
;;;     (or (null? first)
;;;         (if (null? rest)
;;;             (let andmap ((first first))
;;;               (let ((x (car first)) (first (cdr first)))
;;;                 (if (null? first)
;;;                     (f x)
;;;                     (and (f x) (andmap first)))))
;;;             (let andmap ((first first) (rest rest))
;;;               (let ((x (car first))
;;;                     (xr (map car rest))
;;;                     (first (cdr first))
;;;                     (rest (map cdr rest)))
;;;                 (if (null? first)
;;;                     (apply f (cons x xr))
;;;                     (and (apply f (cons x xr)) (andmap first rest)))))))))
;;;
;;; (ormap proc list1)
;;; returns the first non-false return result of proc applied to
;;; the elements of list1 or false if none.  The following definition
;;; works but does no error checking:
;;;
;;; (define ormap
;;;   (lambda (proc list1)
;;;     (and (not (null? list1))
;;;          (or (proc (car list1)) (ormap proc (cdr list1))))))
;;;
;;; The following nonstandard procedures must also be provided by the
;;; implementation for this code to run using the standard portable
;;; hooks and output constructors.  They are not used by expanded code,
;;; and so need be present only at expansion time.
;;;
;;; (eval x)
;;; where x is always in the form ("noexpand" expr).
;;; returns the value of expr.  the "noexpand" flag is used to tell the
;;; evaluator/expander that no expansion is necessary, since expr has
;;; already been fully expanded to core forms.
;;;
;;; eval will not be invoked during the loading of psyntax.pp.  After
;;; psyntax.pp has been loaded, the expansion of any macro definition,
;;; whether local or global, results in a call to eval.  If, however,
;;; sc-expand has already been registered as the expander to be used
;;; by eval, and eval accepts one argument, nothing special must be done
;;; to support the "noexpand" flag, since it is handled by sc-expand.
;;;
;;; (error who format-string why what)
;;; where who is either a symbol or #f, format-string is always "~a ~s",
;;; why is always a string, and what may be any object.  error should
;;; signal an error with a message something like
;;;
;;;    "error in <who>: <why> <what>"
;;;
;;; (gensym)
;;; returns a unique symbol each time it's called.  In Chez Scheme, gensym
;;; returns a symbol with a "globally" unique name so that gensyms that
;;; end up in the object code of separately compiled files cannot conflict.
;;; This is necessary only if you intend to support compiled files.
;;;
;;; (gensym? x)
;;; returns #t if x is a gensym, otherwise false.
;;;
;;; (putprop symbol key value)
;;; (getprop symbol key)
;;; (remprop symbol key)
;;; key is always a symbol; value may be any object.  putprop should
;;; associate the given value with the given symbol and key in some way
;;; that it can be retrieved later with getprop.  getprop should return
;;; #f if no value is associated with the given symbol and key.  remprop
;;; should remove the association between the given symbol and key.

;;; When porting to a new Scheme implementation, you should define the
;;; procedures listed above, load the expanded version of psyntax.ss
;;; (psyntax.pp, which should be available whereever you found
;;; psyntax.ss), and register sc-expand as the current expander (how
;;; you do this depends upon your implementation of Scheme).  You may
;;; change the hooks and constructors defined toward the beginning of
;;; the code below, but to avoid bootstrapping problems, do so only
;;; after you have a working version of the expander.

;;; Chez Scheme allows the syntactic form (syntax <template>) to be
;;; abbreviated to #'<template>, just as (quote <datum>) may be
;;; abbreviated to '<datum>.  The #' syntax makes programs written
;;; using syntax-case shorter and more readable and draws out the
;;; intuitive connection between syntax and quote.  If you have access
;;; to the source code of your Scheme system's reader, you might want
;;; to implement this extension.

;;; If you find that this code loads or runs slowly, consider
;;; switching to faster hardware or a faster implementation of
;;; Scheme.  In Chez Scheme on a 200Mhz Pentium Pro, expanding,
;;; compiling (with full optimization), and loading this file takes
;;; between one and two seconds.

;;; In the expander implementation, we sometimes use syntactic abstractions
;;; when procedural abstractions would suffice.  For example, we define
;;; top-wrap and top-marked? as
;;;   (define-syntax top-wrap (identifier-syntax '((top))))
;;;   (define-syntax top-marked?
;;;     (syntax-rules ()
;;;       ((_ w) (memq 'top (wrap-marks w)))))
;;; rather than
;;;   (define top-wrap '((top)))
;;;   (define top-marked?
;;;     (lambda (w) (memq 'top (wrap-marks w))))
;;; On ther other hand, we don't do this consistently; we define make-wrap,
;;; wrap-marks, and wrap-subst simply as
;;;   (define make-wrap cons)
;;;   (define wrap-marks car)
;;;   (define wrap-subst cdr)
;;; In Chez Scheme, the syntactic and procedural forms of these
;;; abstractions are equivalent, since the optimizer consistently
;;; integrates constants and small procedures.  Some Scheme
;;; implementations, however, may benefit from more consistent use
;;; of one form or the other.


;;; Implementation notes:

;;; "begin" is treated as a splicing construct at top level and at
;;; the beginning of bodies.  Any sequence of expressions that would
;;; be allowed where the "begin" occurs is allowed.

;;; "let-syntax" and "letrec-syntax" are also treated as splicing
;;; constructs, in violation of the R5RS.  A consequence is that let-syntax
;;; and letrec-syntax do not create local contours, as do let and letrec.
;;; Although the functionality is greater as it is presently implemented,
;;; we will probably change it to conform to the R5RS.  modules provide
;;; similar functionality to nonsplicing letrec-syntax when the latter is
;;; used as a definition.

;;; Objects with no standard print syntax, including objects containing
;;; cycles and syntax objects, are allowed in quoted data as long as they
;;; are contained within a syntax form or produced by datum->syntax-object.
;;; Such objects are never copied.

;;; When the expander encounters a reference to an identifier that has
;;; no global or lexical binding, it treats it as a global-variable
;;; reference.  This allows one to write mutually recursive top-level
;;; definitions, e.g.:
;;;
;;;   (define f (lambda (x) (g x)))
;;;   (define g (lambda (x) (f x)))
;;;
;;; but may not always yield the intended when the variable in question
;;; is later defined as a keyword.

;;; Top-level variable definitions of syntax keywords are permitted.
;;; In order to make this work, top-level define not only produces a
;;; top-level definition in the core language, but also modifies the
;;; compile-time environment (using $sc-put-cte) to record the fact
;;; that the identifier is a variable.

;;; Top-level definitions of macro-introduced identifiers are visible
;;; only in code produced by the macro.  That is, a binding for a
;;; hidden (generated) identifier is created instead, and subsequent
;;; references within the macro output are renamed accordingly.  For
;;; example:
;;;
;;; (define-syntax a
;;;   (syntax-rules ()
;;;     ((_ var exp)
;;;      (begin
;;;        (define secret exp)
;;;        (define var
;;;          (lambda ()
;;;            (set! secret (+ secret 17))
;;;            secret))))))
;;; (a x 0)
;;; (x) => 17
;;; (x) => 34
;;; secret => Error: variable secret is not bound
;;;
;;; The definition above would fail if the definition for secret
;;; were placed after the definition for var, since the expander would
;;; encounter the references to secret before the definition that
;;; establishes the compile-time map from the identifier secret to
;;; the generated identifier.

;;; Identifiers and syntax objects are implemented as vectors for
;;; portability.  As a result, it is possible to "forge" syntax
;;; objects.

;;; The input to sc-expand may contain "annotations" describing, e.g., the
;;; source file and character position from where each object was read if
;;; it was read from a file.  These annotations are handled properly by
;;; sc-expand only if the annotation? hook (see hooks below) is implemented
;;; properly and the operators annotation-expression and annotation-stripped
;;; are supplied.  If annotations are supplied, the proper annotated
;;; expression is passed to the various output constructors, allowing
;;; implementations to accurately correlate source and expanded code.
;;; Contact one of the authors for details if you wish to make use of
;;; this feature.

;;; Implementation of modules:
;;;
;;; The implementation of modules requires that implicit top-level exports
;;; be listed with the exported macro at some level where both are visible,
;;; e.g.,
;;;
;;;   (module M (alpha (beta b))
;;;     (module ((alpha a) b)
;;;       (define-syntax alpha (identifier-syntax a))
;;;       (define a 'a)
;;;       (define b 'b))
;;;     (define-syntax beta (identifier-syntax b)))
;;;
;;; Listing of implicit imports is not needed for macros that do not make
;;; it out to top level, including all macros that are local to a "body".
;;; (They may be listed in this case, however.)  We need this information
;;; for top-level modules since a top-level module expands into a letrec
;;; for non-top-level variables and top-level definitions (assignments) for
;;; top-level variables.  Because of the general nature of macro
;;; transformers, we cannot determine the set of implicit exports from the
;;; transformer code, so without the user's help, we'd have to put all
;;; variables at top level.
;;;
;;; Each such top-level identifier is given a generated name (gensym).
;;; When a top-level module is imported at top level, a compile-time
;;; alias is established from the top-level name to the generated name.
;;; The expander follows these aliases transparently.  When any module is
;;; imported anywhere other than at top level, the id-var-name of the
;;; import identifier is set to the id-var-name of the export identifier.
;;; Since we can't determine the actual labels for identifiers defined in
;;; top-level modules until we determine which are placed in the letrec
;;; and which make it to top level, we give each an "indirect" label---a
;;; pair whose car will eventually contain the actual label.  Import does
;;; not follow the indirect, but id-var-name does.
;;;
;;; All identifiers defined within a local module are folded into the
;;; letrec created for the enclosing body.  Visibility is controlled in
;;; this case and for nested top-level modules by introducing a new wrap
;;; for each module.


;;; Bootstrapping:

;;; When changing syntax-object representations, it is necessary to support
;;; both old and new syntax-object representations in id-var-name.  It
;;; should be sufficient to redefine syntax-object-expression to work for
;;; both old and new representations and syntax-object-wrap to return the
;;; empty-wrap for old representations.


;;; The following set of definitions establishes bindings for the
;;; top-level variables assigned values in the let expression below.
;;; Uncomment them here and copy them to the front of psyntax.pp if
;;; required by your system.

; (define $sc-put-cte #f)
; (define sc-expand #f)
; (define $make-environment #f)
; (define environment? #f)
; (define interaction-environment #f)
; (define identifier? #f)
; (define syntax->list #f)
; (define syntax-object->datum #f)
; (define datum->syntax-object #f)
; (define generate-temporaries #f)
; (define free-identifier=? #f)
; (define bound-identifier=? #f)
; (define literal-identifier=? #f)
; (define syntax-error #f)
; (define $syntax-dispatch #f)

(let ()

(define-syntax when
  (syntax-rules ()
    ((_ test e1 e2 ...) (if test (begin e1 e2 ...)))))
(define-syntax unless
  (syntax-rules ()
    ((_ test e1 e2 ...) (when (not test) (begin e1 e2 ...)))))
(define-syntax define-structure
  (lambda (x)
    (define construct-name
      (lambda (template-identifier . args)
        (datum->syntax-object
          template-identifier
          (string->symbol
            (apply string-append
                   (map (lambda (x)
                          (if (string? x)
                              x
                              (symbol->string (syntax-object->datum x))))
                        args))))))
    (syntax-case x ()
      ((_ (name id1 ...))
       (andmap identifier? (syntax (name id1 ...)))
       (with-syntax
         ((constructor (construct-name (syntax name) "make-" (syntax name)))
          (predicate (construct-name (syntax name) (syntax name) "?"))
          ((access ...)
           (map (lambda (x) (construct-name x (syntax name) "-" x))
                (syntax (id1 ...))))
          ((assign ...)
           (map (lambda (x)
                  (construct-name x "set-" (syntax name) "-" x "!"))
                (syntax (id1 ...))))
          (structure-length
           (+ (length (syntax (id1 ...))) 1))
          ((index ...)
           (let f ((i 1) (ids (syntax (id1 ...))))
              (if (null? ids)
                  '()
                  (cons i (f (+ i 1) (cdr ids)))))))
         (syntax (begin
                   (define constructor
                     (lambda (id1 ...)
                       (vector 'name id1 ... )))
                   (define predicate
                     (lambda (x)
                       (and (vector? x)
                            (= (vector-length x) structure-length)
                            (eq? (vector-ref x 0) 'name))))
                   (define access
                     (lambda (x)
                       (vector-ref x index)))
                   ...
                   (define assign
                     (lambda (x update)
                       (vector-set! x index update)))
                   ...)))))))

(define-syntax let-values ; impoverished one-clause version
  (syntax-rules ()
    ((_ ((formals expr)) form1 form2 ...)
     (call-with-values (lambda () expr) (lambda formals form1 form2 ...)))))

(define noexpand "noexpand")

(define-structure (syntax-object expression wrap))

;;; hooks to nonportable run-time helpers
(begin
;*** Gambit supports fx+, etc.
;*** (define-syntax fx+ (identifier-syntax +))
;*** (define-syntax fx- (identifier-syntax -))
;*** (define-syntax fx= (identifier-syntax =))
;*** (define-syntax fx< (identifier-syntax <))
;*** (define-syntax fx> (identifier-syntax >))
;*** (define-syntax fx<= (identifier-syntax <=))
;*** (define-syntax fx>= (identifier-syntax >=))

;*** (define annotation? (lambda (x) #f))

; top-level-eval-hook is used to create "permanent" code (e.g., top-level
; transformers), so it might be a good idea to compile it
(define top-level-eval-hook
  (lambda (x)
    (eval `(,noexpand ,x))))

; local-eval-hook is used to create "temporary" code (e.g., local
; transformers), so it might be a good idea to interpret it
(define local-eval-hook
  (lambda (x)
    (eval `(,noexpand ,x))))

(define define-top-level-value-hook
  (lambda (sym val)
    (top-level-eval-hook
      (build-global-definition no-source sym
        (build-data no-source val)))))

;*** (define error-hook
;***   (lambda (who why what)
;***     (error who "~a ~s" why what)))

(define put-cte-hook
  (lambda (symbol val)
    ($sc-put-cte symbol val '*top*)))

(define get-global-definition-hook
  (lambda (symbol)
    (getprop symbol '*sc-expander*)))

(define put-global-definition-hook
  (lambda (symbol x)
    (if (not x)
        (remprop symbol '*sc-expander*)
        (putprop symbol '*sc-expander* x))))

; if you treat certain bindings (say from environments like ieee or r5rs)
; read-only, this should return #t for those bindings
(define read-only-binding?
  (lambda (symbol)
    #f))

; should return #f if symbol has no binding for token
(define get-import-binding
  (lambda (symbol token)
    (getprop symbol token)))

; remove binding if x is false
(define update-import-binding!
  (lambda (symbol token p)
    (let ((x (p (get-import-binding symbol token))))
      (if (not x)
          (remprop symbol token)
          (putprop symbol token x)))))

;;; generate-id ideally produces globally unique symbols, i.e., symbols
;;; unique across system runs, to support separate compilation/expansion.
;;; Use gensyms if you do not need to support separate compilation/
;;; expansion or if your system's gensym creates globally unique
;;; symbols (as in Chez Scheme).  Otherwise, use the following code
;;; as a starting point.  session-key should be a unique string for each
;;; system run to support separate compilation; the default value given
;;; is satisfactory during initial development only.
(define generate-id
  (let ((digits "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!$%&*/:<=>?~_^.+-"))
    (let ((base (string-length digits)) (session-key "_"))
      (define make-digit (lambda (x) (string-ref digits x)))
      (define fmt
        (lambda (n)
          (let fmt ((n n) (a '()))
            (if (< n base)
                (list->string (cons (make-digit n) a))
                (let ((r (modulo n base)) (rest (quotient n base)))
                  (fmt rest (cons (make-digit r) a)))))))
      (let ((n -1))
        (lambda (name) ; name is #f or a symbol
          (set! n (+ n 1))
          (string->symbol (string-append session-key (fmt n))))))))
)



;;; output constructors
(begin
(define-syntax build-application
  (syntax-rules ()
    ((_ ae fun-exp arg-exps)
;***      `(,fun-exp . ,arg-exps))))
     (build-source ae `(,fun-exp . ,arg-exps)))))

(define-syntax build-conditional
  (syntax-rules ()
    ((_ ae test-exp then-exp else-exp)
;***      `(if ,test-exp ,then-exp ,else-exp))))
     (build-source ae `(,(build-source ae 'if) ,test-exp ,then-exp ,else-exp)))))

(define-syntax build-lexical-reference
  (syntax-rules ()
    ((_ type ae var)
;***      var)))
     (build-source ae var))))

(define-syntax build-lexical-assignment
  (syntax-rules ()
    ((_ ae var exp)
;***      `(set! ,var ,exp))))
     (build-source ae `(,(build-source ae 'set!) ,(build-source ae var) ,exp)))))

(define-syntax build-global-reference
  (syntax-rules ()
    ((_ ae var)
;***      var)))
     (build-source ae var))))

(define-syntax build-global-assignment
  (syntax-rules ()
    ((_ ae var exp)
;***      `(set! ,var ,exp))))
     (build-source ae `(,(build-source ae 'set!) ,(build-source ae var) ,exp)))))

(define-syntax build-global-definition
  (syntax-rules ()
    ((_ ae var exp)
;***      `(define ,var ,exp))))
     (build-source ae `(,(build-source ae 'define) ,(build-source ae var) ,exp)))))

(define-syntax build-cte-install
 ; should build a call that has the same effect as calling put-cte-hook
  (syntax-rules ()
;***     ((_ sym exp token) `($sc-put-cte ',sym ,exp ',token))))
    ((_ sym exp token) (build-source #f `(,(build-source #f '$sc-put-cte) ',sym ,exp ,(build-source #f (list (build-source #f 'quote) token)))))))

(define-syntax build-visit-only
 ; should mark the result as "visit only" for compile-file
 ; in implementations that support visit/revisit
  (syntax-rules ()
    ((_ exp) exp)))

(define-syntax build-revisit-only
 ; should mark the result as "revisit only" for compile-file,
 ; in implementations that support visit/revisit
  (syntax-rules ()
    ((_ exp) exp)))

(define-syntax build-lambda
  (syntax-rules ()
    ((_ ae vars exp)
;***      `(lambda ,vars ,exp))))
     (build-source ae
                   `(,(build-source ae 'lambda)
                     ,(build-params ae vars)
                     ,exp)))))

(define built-lambda?
  (lambda (x)
;***     (and (pair? x) (eq? (car x) 'lambda))))
    (or (and (pair? x) (eq? (car x) 'lambda))
        (and (##source? x)
             (pair? (##source-code x))
             (##source? (car (##source-code x)))
             (eq? (##source-code (car (##source-code x))) 'lambda)))))

(define-syntax build-primref
  (syntax-rules ()
;***     ((_ ae name) name)
;***     ((_ ae level name) name)))
    ((_ ae name) (build-source ae name))
    ((_ ae level name) (build-source ae name))))

(define-syntax build-data
  (syntax-rules ()
;***     ((_ ae exp) `',exp)))
    ((_ ae exp) (let ((x (attach-source ae exp))) (if (self-eval? exp) x (build-source ae (list (build-source ae 'quote) x)))))))

(define build-sequence
  (lambda (ae exps)
    (let loop ((exps exps))
      (if (null? (cdr exps))
          (car exps)
         ; weed out leading void calls, assuming ordinary list representation
;***           (if (equal? (car exps) '(void))
;***               (loop (cdr exps))
;***               `(begin ,@exps))))))
          (if (let ((x (car exps)))
                (or (equal? x '(void))
                    (and (##source? x)
                         (pair? (##source-code x))
                         (##source? (car (##source-code x)))
                         (eq? (##source-code (car (##source-code x))) 'void)
                         (null? (cdr (##source-code x))))))
              (loop (cdr exps))
              (build-source ae (cons (build-source ae 'begin) exps)))))))

(define build-letrec
  (lambda (ae vars val-exps body-exp)
    (if (null? vars)
        body-exp
;***         `(letrec ,(map list vars val-exps) ,body-exp))))
        (build-source ae `(,(build-source ae 'letrec) ,(build-source ae (map (lambda (var val) (build-source ae (list (build-source ae var) val))) vars val-exps)) ,body-exp)))))

(define build-body
  (lambda (ae vars val-exps body-exp)
    (build-letrec ae vars val-exps body-exp)))

(define build-top-module
 ; each type is either global (exported) or local (not exported)
 ; we produce global definitions and assignments for globals and
 ; letrec bindings for locals.  if you don't need the definitions,
 ; (just assignments) you can eliminate them.  if you wish to
 ; have your module definitions ordered from left-to-right (ala
 ; letrec*), you can replace the global var-exps with dummy vars
 ; and global val-exps with global assignments, and produce a letrec*
 ; in place of a letrec.
  (lambda (ae types vars val-exps body-exp)
    (let-values (((vars defns sets)
                  (let f ((types types) (vars vars))
                    (if (null? types)
                        (values '() '() '())
                        (let ((var (car vars)))
                          (let-values (((vars defns sets) (f (cdr types) (cdr vars))))
                            (if (eq? (car types) 'global)
                                (let ((x (build-lexical-var no-source var)))
                                  (values
                                    (cons x vars)
                                    (cons (build-global-definition no-source var (chi-void)) defns)
                                    (cons (build-global-assignment no-source var (build-lexical-reference 'value no-source x)) sets)))
                                (values (cons var vars) defns sets))))))))
      (if (null? defns)
          (build-letrec ae vars val-exps body-exp)
          (build-sequence no-source
            (append defns
              (list
                (build-letrec ae vars val-exps
                  (build-sequence no-source (append sets (list body-exp)))))))))))

(define-syntax build-lexical-var
  (syntax-rules ()
;***     ((_ ae id) (gensym))))
    ((_ ae id) (gensym id))))

(define-syntax lexical-var? gensym?)

(define-syntax self-evaluating?
  (syntax-rules ()
    ((_ e)
     (let ((x e))
;***       (or (boolean? x) (number? x) (string? x) (char? x) (null? x))))))
       (self-eval? x)))))
)

(define-syntax unannotate
  (syntax-rules ()
    ((_ x)
     (let ((e x))
       (if (annotation? e)
           (annotation-expression e)
           e)))))

(define-syntax no-source (identifier-syntax #f))

(define-syntax arg-check
  (syntax-rules ()
    ((_ pred? e who)
     (let ((x e))
;***        (if (not (pred? x)) (error-hook who "invalid argument" x))))))
       (if (not (pred? x))
           (error (string-append "(in "
                                 (symbol->string who)
                                 ") invalid argument")
                  x))))))

;;; compile-time environments

;;; wrap and environment comprise two level mapping.
;;;   wrap : id --> label
;;;   env : label --> <element>

;;; environments are represented in two parts: a lexical part and a global
;;; part.  The lexical part is a simple list of associations from labels
;;; to bindings.  The global part is implemented by
;;; {put,get}-global-definition-hook and associates symbols with
;;; bindings.

;;; global (assumed global variable) and displaced-lexical (see below)
;;; do not show up in any environment; instead, they are fabricated by
;;; lookup when it finds no other bindings.

;;; <environment>              ::= ((<label> . <binding>)*)

;;; identifier bindings include a type and a value

;;; <binding> ::= <procedure>                     macro keyword
;;;               (macro . <procedure>)           macro keyword
;;;               (deferred . <thunk>)            macro keyword w/lazily evaluated transformer
;;;               (macro! . <procedure>)          extended identifier macro keyword
;;;               (core . <procedure>)            core keyword
;;;               (begin)                         begin keyword
;;;               (define)                        define keyword
;;;               (define-syntax)                 define-syntax keyword
;;;               (local-syntax . <boolean>)      let-syntax (#f)/letrec-syntax (#t) keyword
;;;               (eval-when)                     eval-when keyword
;;;               (set!)                          set! keyword
;;;               (meta)                          meta keyword
;;;               ($module-key)                   $module keyword
;;;               ($import)                       $import keyword
;;;               ($module . <interface>)         modules
;;;               (syntax . (<var> . <level>))    pattern variables
;;;               (global . <symbol>)             assumed global variable
;;;               (meta-variable . <symbol>)      meta variable
;;;               (lexical . <var>)               lexical variables
;;;               (displaced-lexical . #f)        id-var-name not found in store
;;; <level>   ::= <nonnegative integer>
;;; <var>     ::= variable returned by build-lexical-var

;;; a macro is a user-defined syntactic-form.  a core is a system-defined
;;; syntactic form.  begin, define, define-syntax, let-syntax, letrec-syntax,
;;; eval-when, and meta are treated specially since they are sensitive to
;;; whether the form is at top-level and can denote valid internal
;;; definitions.

;;; a pattern variable is a variable introduced by syntax-case and can
;;; be referenced only within a syntax form.

;;; any identifier for which no top-level syntax definition or local
;;; binding of any kind has been seen is assumed to be a global
;;; variable.

;;; a lexical variable is a lambda- or letrec-bound variable.

;;; a displaced-lexical identifier is a lexical identifier removed from
;;; it's scope by the return of a syntax object containing the identifier.
;;; a displaced lexical can also appear when a letrec-syntax-bound
;;; keyword is referenced on the rhs of one of the letrec-syntax clauses.
;;; a displaced lexical should never occur with properly written macros.

(define sanitize-binding
  (lambda (b)
    (cond
      ((procedure? b) (make-binding 'macro b))
      ((binding? b)
       (and (case (binding-type b)
              ((core macro macro! deferred) (and (procedure? (binding-value b))))
              (($module) (interface? (binding-value b)))
              ((lexical) (lexical-var? (binding-value b)))
             ((global meta-variable) (symbol? (binding-value b)))
              ((syntax) (let ((x (binding-value b)))
                          (and (pair? x)
                               (lexical-var? (car x))
                               (let ((n (cdr x)))
                                 (and (integer? n) (exact? n) (>= n 0))))))
              ((begin define define-syntax set! $module-key $import eval-when meta) (null? (binding-value b)))
              ((local-syntax) (boolean? (binding-value b)))
              ((displaced-lexical) (eq? (binding-value b) #f))
              (else #t))
            b))
      (else #f))))

(define-syntax make-binding
  (syntax-rules (quote)
    ((_ 'type #f) '(type . #f))
    ((_ type value) (cons type value))))
(define binding-type car)
(define binding-value cdr)
(define set-binding-type! set-car!)
(define set-binding-value! set-cdr!)
(define binding? (lambda (x) (and (pair? x) (symbol? (car x)))))

(define-syntax null-env (identifier-syntax '()))

(define extend-env
  (lambda (label binding r)
    (cons (cons label binding) r)))

(define extend-env*
  (lambda (labels bindings r)
    (if (null? labels)
        r
        (extend-env* (cdr labels) (cdr bindings)
          (extend-env (car labels) (car bindings) r)))))

(define extend-var-env*
  ; variant of extend-env* that forms "lexical" binding
  (lambda (labels vars r)
    (if (null? labels)
        r
        (extend-var-env* (cdr labels) (cdr vars)
          (extend-env (car labels) (make-binding 'lexical (car vars)) r)))))

(define (displaced-lexical? id r)
  (let ((n (id-var-name id empty-wrap)))
    (and n
         (let ((b (lookup n r)))
           (eq? (binding-type b) 'displaced-lexical)))))

(define displaced-lexical-error
  (lambda (id)
    (syntax-error id
      (if (id-var-name id empty-wrap)
          "identifier out of context"
          "identifier not visible"))))

(define lookup*
  ; x may be a label or a symbol
  ; although symbols are usually global, we check the environment first
  ; anyway because a temporary binding may have been established by
  ; fluid-let-syntax
  (lambda (x r)
    (cond
      ((assq x r) => cdr)
      ((symbol? x)
       (or (get-global-definition-hook x) (make-binding 'global x)))
      (else (make-binding 'displaced-lexical #f)))))

(define lookup
  (lambda (x r)
    (define whack-binding!
      (lambda (b *b)
        (set-binding-type! b (binding-type *b))
        (set-binding-value! b (binding-value *b))))
    (let ((b (lookup* x r)))
      (when (eq? (binding-type b) 'deferred)
        (whack-binding! b (make-transformer-binding ((binding-value b)))))
      b)))

(define make-transformer-binding
  (lambda (b)
    (or (sanitize-binding b)
        (syntax-error b "invalid transformer"))))

(define defer-or-eval-transformer
  (lambda (eval x)
    (if (built-lambda? x)
        (make-binding 'deferred (lambda () (eval x)))
        (make-transformer-binding (eval x)))))

(define global-extend
  (lambda (type sym val)
    (put-cte-hook sym (make-binding type val))))


;;; Conceptually, identifiers are always syntax objects.  Internally,
;;; however, the wrap is sometimes maintained separately (a source of
;;; efficiency and confusion), so that symbols are also considered
;;; identifiers by id?.  Externally, they are always wrapped.

(define nonsymbol-id?
  (lambda (x)
    (and (syntax-object? x)
         (symbol? (unannotate (syntax-object-expression x))))))

(define id?
  (lambda (x)
    (cond
      ((symbol? x) #t)
      ((syntax-object? x) (symbol? (unannotate (syntax-object-expression x))))
      ((annotation? x) (symbol? (annotation-expression x)))
      (else #f))))

(define-syntax id-sym-name
  (syntax-rules ()
    ((_ e)
     (let ((x e))
       (unannotate (if (syntax-object? x) (syntax-object-expression x) x))))))

(define id-marks
  (lambda (id)
    (if (syntax-object? id)
        (wrap-marks (syntax-object-wrap id))
        (wrap-marks top-wrap))))

(define id-subst
  (lambda (id)
    (if (syntax-object? id)
        (wrap-subst (syntax-object-wrap id))
        (wrap-marks top-wrap))))

(define id-sym-name&marks
  (lambda (x w)
    (if (syntax-object? x)
        (values
          (unannotate (syntax-object-expression x))
          (join-marks (wrap-marks w) (wrap-marks (syntax-object-wrap x))))
        (values (unannotate x) (wrap-marks w)))))

;;; syntax object wraps

;;;         <wrap>     ::= ((<mark> ...) . (<subst> ...))
;;;        <subst>     ::= <ribcage> | <shift>
;;;      <ribcage>     ::= #((<ex-symname> ...) (<mark> ...) (<label> ...)) ; extensible, for chi-internal/external
;;;                      | #(#(<symname> ...) #(<mark> ...) #(<label> ...)) ; nonextensible
;;;   <ex-symname>     ::= <symname> | <import token> | <barrier>
;;;        <shift>     ::= shift
;;;      <barrier>     ::= #f                                               ; inserted by import-only
;;; <import interface> ::= #<import-interface interface new-marks>
;;;        <token>     ::= <generated id>

(define make-wrap cons)
(define wrap-marks car)
(define wrap-subst cdr)


(define-syntax empty-wrap (identifier-syntax '(())))

(define-syntax top-wrap (identifier-syntax '((top))))

(define-syntax tmp-wrap (identifier-syntax '((tmp)))) ; for generate-temporaries

(define-syntax top-marked?
  (syntax-rules ()
    ((_ w) (memq 'top (wrap-marks w)))))

(define-syntax only-top-marked?
  (syntax-rules ()
    ((_ id) (same-marks? (wrap-marks (syntax-object-wrap id)) (wrap-marks top-wrap)))))

;;; labels

;;; simple labels must be comparable with "eq?" and distinct from symbols
;;; and pairs.

;;; indirect labels, which are implemented as pairs, are used to support
;;; import aliasing for identifiers exported (explictly or implicitly) from
;;; top-level modules.  chi-external creates an indirect label for each
;;; defined identifier, import causes the pair to be shared with aliases it
;;; establishes, and chi-top-module whacks the pair to hold the top-level
;;; identifier name (symbol) if the id is to be placed at top level, before
;;; expanding the right-hand sides of the definitions in the module.

(module (gen-indirect-label indirect-label? get-indirect-label set-indirect-label!)
  (define-structure (indirect-label label))
  (define gen-indirect-label
    (lambda ()
      (make-indirect-label (gen-label))))
  (define get-indirect-label (lambda (x) (indirect-label-label x)))
  (define set-indirect-label! (lambda (x v) (set-indirect-label-label! x v))))

(define gen-label
  (lambda () (string #\i)))
(define label?
  (lambda (x)
    (or (string? x) ; normal lexical labels
        (symbol? x) ; global labels (symbolic names)
        (indirect-label? x))))

(define gen-labels
  (lambda (ls)
    (if (null? ls)
        '()
        (cons (gen-label) (gen-labels (cdr ls))))))

(define-structure (ribcage symnames marks labels))
(define-structure (top-ribcage key mutable?))
(define-structure (import-interface interface new-marks))
(define-structure (env top-ribcage wrap))

;;; Marks must be comparable with "eq?" and distinct from pairs and
;;; the symbol top.  We do not use integers so that marks will remain
;;; unique even across file compiles.

(define-syntax the-anti-mark (identifier-syntax #f))

(define anti-mark
  (lambda (w)
    (make-wrap (cons the-anti-mark (wrap-marks w))
               (cons 'shift (wrap-subst w)))))

(define-syntax new-mark
  (syntax-rules ()
    ((_) (string #\m))))

(define barrier-marker #f)

;;; make-empty-ribcage and extend-ribcage maintain list-based ribcages for
;;; internal definitions, in which the ribcages are built incrementally
(define-syntax make-empty-ribcage
  (syntax-rules ()
    ((_) (make-ribcage '() '() '()))))

(define extend-ribcage!
 ; must receive ids with complete wraps
 ; ribcage guaranteed to be list-based
  (lambda (ribcage id label)
    (set-ribcage-symnames! ribcage
      (cons (unannotate (syntax-object-expression id))
            (ribcage-symnames ribcage)))
    (set-ribcage-marks! ribcage
      (cons (wrap-marks (syntax-object-wrap id))
            (ribcage-marks ribcage)))
    (set-ribcage-labels! ribcage
      (cons label (ribcage-labels ribcage)))))

(define import-extend-ribcage!
 ; must receive ids with complete wraps
 ; ribcage guaranteed to be list-based
  (lambda (ribcage new-marks id label)
    (set-ribcage-symnames! ribcage
      (cons (unannotate (syntax-object-expression id))
            (ribcage-symnames ribcage)))
    (set-ribcage-marks! ribcage
      (cons (join-marks new-marks (wrap-marks (syntax-object-wrap id)))
            (ribcage-marks ribcage)))
    (set-ribcage-labels! ribcage
      (cons label (ribcage-labels ribcage)))))

(define extend-ribcage-barrier!
 ; must receive ids with complete wraps
 ; ribcage guaranteed to be list-based
  (lambda (ribcage killer-id)
    (extend-ribcage-barrier-help! ribcage (syntax-object-wrap killer-id))))

(define extend-ribcage-barrier-help!
  (lambda (ribcage wrap)
    (set-ribcage-symnames! ribcage
      (cons barrier-marker (ribcage-symnames ribcage)))
    (set-ribcage-marks! ribcage
      (cons (wrap-marks wrap) (ribcage-marks ribcage)))))

(define extend-ribcage-subst!
 ; ribcage guaranteed to be list-based
  (lambda (ribcage import-iface)
    (set-ribcage-symnames! ribcage
      (cons import-iface (ribcage-symnames ribcage)))))

(define lookup-import-binding-name
  (lambda (sym marks token new-marks)
    (let ((new (get-import-binding sym token)))
      (and new
           (let f ((new new))
             (cond
               ((pair? new) (or (f (car new)) (f (cdr new))))
               ((symbol? new)
                (and (same-marks? marks (join-marks new-marks (wrap-marks top-wrap))) new))
               ((same-marks? marks (join-marks new-marks (wrap-marks (syntax-object-wrap new)))) new)
               (else #f)))))))
  
(define store-import-binding
  (lambda (id token new-marks)
    (define cons-id
      (lambda (id x)
        (if (not x) id (cons id x))))
    (define weed ; remove existing binding for id, if any
      (lambda (marks x)
        (if (pair? x)
            (if (same-marks? (id-marks (car x)) marks)
                (weed marks (cdr x))
                (cons-id (car x) (weed marks (cdr x))))
            (and x (not (same-marks? (id-marks x) marks)) x))))
    (let ((id (if (null? new-marks)
                  id
                  (make-syntax-object (id-sym-name id)
                    (make-wrap
                      (join-marks new-marks (id-marks id))
                      (id-subst id))))))
      (let ((sym (id-sym-name id)))
       ; no need to record bindings mapping symbol to self, since this   
       ; assumed by default.
        (unless (eq? id sym)
          (let ((marks (id-marks id)))
            (update-import-binding! sym token
              (lambda (old-binding)
                (let ((x (weed marks old-binding)))
                  (cons-id
                    (if (same-marks? marks (wrap-marks top-wrap))
                       ; need full id only if more than top-marked.
                        (resolved-id-var-name id)
                        id)
                    x))))))))))

;;; make-binding-wrap creates vector-based ribcages
(define make-binding-wrap
  (lambda (ids labels w)
    (if (null? ids)
        w
        (make-wrap
          (wrap-marks w)
          (cons
            (let ((labelvec (list->vector labels)))
              (let ((n (vector-length labelvec)))
                (let ((symnamevec (make-vector n)) (marksvec (make-vector n)))
                  (let f ((ids ids) (i 0))
                    (unless (null? ids)
                      (let-values (((symname marks) (id-sym-name&marks (car ids) w)))
                        (vector-set! symnamevec i symname)
                        (vector-set! marksvec i marks)
                        (f (cdr ids) (fx+ i 1)))))
                  (make-ribcage symnamevec marksvec labelvec))))
            (wrap-subst w))))))

;;; resolved ids contain no unnecessary substitutions or marks.  they are
;;; used essentially as indirects or aliases in modules interfaces.
(define make-resolved-id
  (lambda (fromsym marks tosym)
    (make-syntax-object fromsym
      (make-wrap marks
        (list (make-ribcage (vector fromsym) (vector marks) (vector tosym)))))))

(define id->resolved-id
  (lambda (id)
    (let-values (((tosym marks) (id-var-name&marks id empty-wrap)))
      (unless tosym
        (syntax-error id "identifier not visible for export"))
      (make-resolved-id (id-sym-name id) marks tosym))))

(define resolved-id-var-name
  (lambda (id)
    (vector-ref
      (ribcage-labels (car (wrap-subst (syntax-object-wrap id))))
      0)))

;;; Scheme's append should not copy the first argument if the second is
;;; nil, but it does, so we define a smart version here.
(define smart-append
  (lambda (m1 m2)
    (if (null? m2)
        m1
        (append m1 m2))))

(define join-wraps
  (lambda (w1 w2)
    (let ((m1 (wrap-marks w1)) (s1 (wrap-subst w1)))
      (if (null? m1)
          (if (null? s1)
              w2
              (make-wrap
                (wrap-marks w2)
                (join-subst s1 (wrap-subst w2))))
          (make-wrap
            (join-marks m1 (wrap-marks w2))
            (join-subst s1 (wrap-subst w2)))))))

(define join-marks
  (lambda (m1 m2)
    (smart-append m1 m2)))

(define join-subst
  (lambda (s1 s2)
    (smart-append s1 s2)))

(define same-marks?
  (lambda (x y)
    (or (eq? x y)
        (and (not (null? x))
             (not (null? y))
             (eq? (car x) (car y))
             (same-marks? (cdr x) (cdr y))))))

(define diff-marks
  (lambda (m1 m2)
    (let ((n1 (length m1)) (n2 (length m2)))
      (let f ((n1 n1) (m1 m1))
        (cond
          ((> n1 n2) (cons (car m1) (f (- n1 1) (cdr m1))))
          ((equal? m1 m2) '())
;***           (else (error 'sc-expand
;***                   "internal error in diff-marks: ~s is not a tail of ~s"
;***                   m1 m2)))))))
          (else (error
                  "internal error in diff-marks"
                  m1 m2)))))))

(module (top-id-bound-var-name top-id-free-var-name)
  ;; top-id-bound-var-name is used to look up or establish new top-level
  ;; substitutions, while top-id-free-var-name is used to look up existing
  ;; (possibly implicit) substitutions.  Implicit substitutions exist
  ;; for top-marked names in all environments, but we represent them
  ;; explicitly only on demand.
  ;;
  ;; In both cases, we first look for an existing substitution for sym
  ;; and the given marks.  If we find one, we return it.  Otherwise, we
  ;; extend the appropriate top-level environment
  ;;
  ;; For top-id-bound-var-name, we extend the environment with a substition
  ;; keyed by the given marks, so that top-level definitions introduced by
  ;; a macro are distinct from other top-level definitions for the same
  ;; name.  For example, if macros a and b both introduce definitions and
  ;; bound references to identifier x, the two x's should be different,
  ;; i.e., keyed by their own marks.
  ;;
  ;; For top-id-free-var-name, we extend the environment with a substition
  ;; keyed by the top marks, since top-level free identifier references
  ;; should refer to the existing implicit (top-marked) substitution.  For
  ;; example, if macros a and b both introduce free references to identifier
  ;; x, they should both refer to the same (global, unmarked) x.
  ;;
 ;; If the environment is *top*, we map a symbol to itself

 (define leave-implicit? (lambda (token) (eq? token '*top*)))

  (define new-binding
    (lambda (sym marks token)
      (let ((loc (if (and (leave-implicit? token)
                          (same-marks? marks (wrap-marks top-wrap)))
                     sym
                     (generate-id sym))))
        (let ((id (make-resolved-id sym marks loc)))
          (store-import-binding id token '())
          (values loc id)))))

  (define top-id-bound-var-name
   ; should be called only when top-ribcage is mutable
    (lambda (sym marks top-ribcage)
      (let ((token (top-ribcage-key top-ribcage)))
        (cond
          ((lookup-import-binding-name sym marks token '()) =>
           (lambda (id)
             (if (symbol? id) ; symbol iff marks == (wrap-marks top-wrap)
                 (if (read-only-binding? id)
                     (new-binding sym marks token)
                     (values id (make-resolved-id sym marks id)))
                 (values (resolved-id-var-name id) id))))
          (else (new-binding sym marks token))))))

  (define top-id-free-var-name
    (lambda (sym marks top-ribcage)
      (let ((token (top-ribcage-key top-ribcage)))
        (cond
          ((lookup-import-binding-name sym marks token '()) =>
           (lambda (id) (if (symbol? id) id (resolved-id-var-name id))))
          ((and (top-ribcage-mutable? top-ribcage)
                (same-marks? marks (wrap-marks top-wrap)))
           (let-values (((sym id) (new-binding sym (wrap-marks top-wrap) token)))
             sym))
          (else #f))))))

(define id-var-name-loc&marks
  (lambda (id w)
    (define search
      (lambda (sym subst marks)
        (if (null? subst)
            (values #f marks)
            (let ((fst (car subst)))
               (cond
                 ((eq? fst 'shift) (search sym (cdr subst) (cdr marks)))
                 ((ribcage? fst)
                  (let ((symnames (ribcage-symnames fst)))
                    (if (vector? symnames)
                        (search-vector-rib sym subst marks symnames fst)
                        (search-list-rib sym subst marks symnames fst))))
                 ((top-ribcage? fst)
                  (cond
                    ((top-id-free-var-name sym marks fst) =>
                     (lambda (var-name) (values var-name marks)))
                    (else (search sym (cdr subst) marks))))
                 (else
;***                   (error 'sc-expand
;***                     "internal error in id-var-name-loc&marks: improper subst ~s"
;***                     subst)))))))
                  (error
                    "internal error in id-var-name-loc&marks: improper subst"
                    subst)))))))
    (define search-list-rib
      (lambda (sym subst marks symnames ribcage)
        (let f ((symnames symnames) (i 0))
          (if (null? symnames)
              (search sym (cdr subst) marks)
              (let ((x (car symnames)))
                (cond
                  ((and (eq? x sym)
                        (same-marks? marks (list-ref (ribcage-marks ribcage) i)))
                   (values (list-ref (ribcage-labels ribcage) i) marks))
                  ((import-interface? x)
                   (let ((iface (import-interface-interface x))
                         (new-marks (import-interface-new-marks x)))
                     (cond
                       ((interface-token iface) =>
                        (lambda (token)
                          (cond
                            ((lookup-import-binding-name sym marks token new-marks) =>
                             (lambda (id)
                               (values
                                 (if (symbol? id) id (resolved-id-var-name id))
                                 marks)))
                            (else (f (cdr symnames) i)))))
                       (else
                        (let* ((ie (interface-exports iface))
                               (n (vector-length ie)))
                          (let g ((j 0))
                            (if (fx= j n)
                                (f (cdr symnames) i)
                                (let ((id (vector-ref ie j)))
                                  (let ((id.sym (id-sym-name id))
                                        (id.marks (join-marks new-marks (id-marks id))))
                                    (if (help-bound-id=? id.sym id.marks sym marks)
                                        (values (lookup-import-label id) marks)
                                        (g (fx+ j 1))))))))))))
                  ((and (eq? x barrier-marker)
                        (same-marks? marks (list-ref (ribcage-marks ribcage) i)))
                   (values #f marks))
                  (else (f (cdr symnames) (fx+ i 1)))))))))
    (define search-vector-rib
      (lambda (sym subst marks symnames ribcage)
        (let ((n (vector-length symnames)))
          (let f ((i 0))
            (cond
              ((fx= i n) (search sym (cdr subst) marks))
              ((and (eq? (vector-ref symnames i) sym)
                    (same-marks? marks (vector-ref (ribcage-marks ribcage) i)))
               (values (vector-ref (ribcage-labels ribcage) i) marks))
              (else (f (fx+ i 1))))))))
    (cond
      ((symbol? id) (search id (wrap-subst w) (wrap-marks w)))
      ((syntax-object? id)
       (let ((sym (unannotate (syntax-object-expression id)))
             (w1 (syntax-object-wrap id)))
         (let-values (((name marks) (search sym (wrap-subst w)
                                      (join-marks
                                        (wrap-marks w)
                                        (wrap-marks w1)))))
           (if name
               (values name marks)
               (search sym (wrap-subst w1) marks)))))
      ((annotation? id) (search (unannotate id) (wrap-subst w) (wrap-marks w)))
;***       (else (error-hook 'id-var-name "invalid id" id)))))
      (else (error "(in id-var-name) invalid id" id)))))

(define id-var-name&marks
 ; this version follows indirect labels
  (lambda (id w)
    (let-values (((label marks) (id-var-name-loc&marks id w)))
      (values (if (indirect-label? label) (get-indirect-label label) label) marks))))

(define id-var-name-loc
 ; this version doesn't follow indirect labels
  (lambda (id w)
    (let-values (((label marks) (id-var-name-loc&marks id w)))
      label)))

(define id-var-name
 ; this version follows indirect labels
  (lambda (id w)
    (let-values (((label marks) (id-var-name-loc&marks id w)))
      (if (indirect-label? label) (get-indirect-label label) label))))

;;; free-id=? must be passed fully wrapped ids since (free-id=? x y)
;;; may be true even if (free-id=? (wrap x w) (wrap y w)) is not.

(define free-id=?
  (lambda (i j)
    (and (eq? (id-sym-name i) (id-sym-name j)) ; accelerator
         (eq? (id-var-name i empty-wrap) (id-var-name j empty-wrap)))))

(define literal-id=?
  (lambda (id literal)
    (and (eq? (id-sym-name id) (id-sym-name literal))
         (let ((n-id (id-var-name id empty-wrap))
               (n-literal (id-var-name literal empty-wrap)))
           (or (eq? n-id n-literal)
               (and (or (not n-id) (symbol? n-id))
                    (or (not n-literal) (symbol? n-literal))))))))

;;; bound-id=? may be passed unwrapped (or partially wrapped) ids as
;;; long as the missing portion of the wrap is common to both of the ids
;;; since (bound-id=? x y) iff (bound-id=? (wrap x w) (wrap y w))

(define help-bound-id=?
  (lambda (i.sym i.marks j.sym j.marks)
    (and (eq? i.sym j.sym)
         (same-marks? i.marks j.marks))))
   
(define bound-id=?
  (lambda (i j)
    (help-bound-id=? (id-sym-name i) (id-marks i) (id-sym-name j) (id-marks j))))

;;; "valid-bound-ids?" returns #t if it receives a list of distinct ids.
;;; valid-bound-ids? may be passed unwrapped (or partially wrapped) ids
;;; as long as the missing portion of the wrap is common to all of the
;;; ids.

(define valid-bound-ids?
  (lambda (ids)
     (and (let all-ids? ((ids ids))
            (or (null? ids)
                (and (id? (car ids))
                     (all-ids? (cdr ids)))))
          (distinct-bound-ids? ids))))

;;; distinct-bound-ids? expects a list of ids and returns #t if there are
;;; no duplicates.  It is quadratic on the length of the id list; long
;;; lists could be sorted to make it more efficient.  distinct-bound-ids?
;;; may be passed unwrapped (or partially wrapped) ids as long as the
;;; missing portion of the wrap is common to all of the ids.

(define distinct-bound-ids?
  (lambda (ids)
    (let distinct? ((ids ids))
      (or (null? ids)
          (and (not (bound-id-member? (car ids) (cdr ids)))
               (distinct? (cdr ids)))))))

(define invalid-ids-error
 ; find first bad one and complain about it
  (lambda (ids exp class)
    (let find ((ids ids) (gooduns '()))
      (if (null? ids)
          (syntax-error exp) ; shouldn't happen
          (if (id? (car ids))
              (if (bound-id-member? (car ids) gooduns)
                  (syntax-error (car ids) "duplicate " class)
                  (find (cdr ids) (cons (car ids) gooduns)))
              (syntax-error (car ids) "invalid " class))))))

(define bound-id-member?
   (lambda (x list)
      (and (not (null? list))
           (or (bound-id=? x (car list))
               (bound-id-member? x (cdr list))))))

;;; wrapping expressions and identifiers

(define wrap
  (lambda (x w)
    (cond
      ((and (null? (wrap-marks w)) (null? (wrap-subst w))) x)
      ((syntax-object? x)
       (make-syntax-object
         (syntax-object-expression x)
         (join-wraps w (syntax-object-wrap x))))
      ((null? x) x)
      (else (make-syntax-object x w)))))

(define source-wrap
  (lambda (x w ae)
    (wrap (if (annotation? ae)
              (begin
                (unless (eq? (annotation-expression ae) x)
;***                  (error 'sc-expand "internal error in source-wrap: ae/x mismatch"))
                  (error "internal error in source-wrap: ae/x mismatch"))
                ae)
              x)
          w)))

;;; expanding

(define chi-when-list
  (lambda (when-list w)
    ; when-list is syntax'd version of list of situations
    (map (lambda (x)
           (cond
             ((literal-id=? x (syntax compile)) 'compile)
             ((literal-id=? x (syntax load)) 'load)
             ((literal-id=? x (syntax visit)) 'visit)
             ((literal-id=? x (syntax revisit)) 'revisit)
             ((literal-id=? x (syntax eval)) 'eval)
             (else (syntax-error (wrap x w) "invalid eval-when situation"))))
         when-list)))

;;; syntax-type returns five values: type, value, e, w, and ae.  The first
;;; two are described in the table below.
;;;
;;;    type                   value         explanation
;;;    -------------------------------------------------------------------
;;;    alias                  none          alias keyword
;;;    alias-form             none          alias expression
;;;    begin                  none          begin keyword
;;;    begin-form             none          begin expression
;;;    call                   none          any other call
;;;    constant               none          self-evaluating datum
;;;    core                   procedure     core form (including singleton)
;;;    define                 none          define keyword
;;;    define-form            none          variable definition
;;;    define-syntax          none          define-syntax keyword
;;;    define-syntax-form     none          syntax definition
;;;    displaced-lexical      none          displaced lexical identifier
;;;    eval-when              none          eval-when keyword
;;;    eval-when-form         none          eval-when form
;;;    global                 name          global variable reference
;;;    $import                none          $import keyword
;;;    $import-form           none          $import form
;;;    lexical                name          lexical variable reference
;;;    lexical-call           name          call to lexical variable
;;;    local-syntax           rec?          letrec-syntax/let-syntax keyword
;;;    local-syntax-form      rec?          syntax definition
;;;    meta                   none          meta keyword
;;;    meta-form              none          meta form
;;;    meta-variable          name          meta variable
;;;    $module                none          $module keyword
;;;    $module-form           none          $module definition
;;;    syntax                 level         pattern variable
;;;    other                  none          anything else
;;;
;;; For all forms, e is the form, w is the wrap for e. and ae is the
;;; (possibly) source-annotated form.
;;;
;;; syntax-type expands macros and unwraps as necessary to get to
;;; one of the forms above.

(define syntax-type
  (lambda (e r w ae rib)
    (cond
      ((symbol? e)
       (let* ((n (id-var-name e w))
              (b (lookup n r))
              (type (binding-type b)))
         (case type
           ((macro macro!) (syntax-type (chi-macro (binding-value b) e r w ae rib) r empty-wrap #f rib))
           (else (values type (binding-value b) e w ae)))))
      ((pair? e)
       (let ((first (car e)))
         (if (id? first)
             (let* ((n (id-var-name first w))
                    (b (lookup n r))
                    (type (binding-type b)))
               (case type
                 ((lexical) (values 'lexical-call (binding-value b) e w ae))
                 ((macro macro!)
                  (syntax-type (chi-macro (binding-value b) e r w ae rib)
                    r empty-wrap #f rib))
                 ((core) (values type (binding-value b) e w ae))
                 ((begin) (values 'begin-form #f e w ae))
                 ((alias) (values 'alias-form #f e w ae))
                 ((define) (values 'define-form #f e w ae))
                 ((define-syntax) (values 'define-syntax-form #f e w ae))
                 ((set!) (chi-set! e r w ae rib))
                 (($module-key) (values '$module-form #f e w ae))
                 (($import) (values '$import-form #f e w ae))
                 ((eval-when) (values 'eval-when-form #f e w ae))
                 ((meta) (values 'meta-form #f e w ae))
                 ((local-syntax)
                  (values 'local-syntax-form (binding-value b) e w ae))
                 (else (values 'call #f e w ae))))
             (values 'call #f e w ae))))
      ((syntax-object? e)
       (syntax-type (syntax-object-expression e)
                    r
                    (join-wraps w (syntax-object-wrap e))
                    #f rib))
      ((annotation? e)
       (syntax-type (annotation-expression e) r w e rib))
      ((self-evaluating? e) (values 'constant #f e w ae))
      (else (values 'other #f e w ae)))))

(define chi-top*
  (lambda (e r w ctem rtem meta? top-ribcage)
    (let ((meta-residuals '()))
      (define meta-residualize!
        (lambda (x)
          (set! meta-residuals
            (cons x meta-residuals))))
      (let ((e (chi-top e r w ctem rtem meta? top-ribcage meta-residualize! #f)))
        (build-sequence no-source
          (reverse (cons e meta-residuals)))))))

(define chi-top-sequence
  (lambda (body r w ae ctem rtem meta? ribcage meta-residualize!)
    (build-sequence ae
      (let dobody ((body body))
        (if (null? body)
            '()
            (let ((first (chi-top (car body) r w ctem rtem meta? ribcage meta-residualize! #f)))
              (cons first (dobody (cdr body)))))))))

(define chi-top
  (lambda (e r w ctem rtem meta? top-ribcage meta-residualize! meta-seen?)
    (let-values (((type value e w ae) (syntax-type e r w no-source top-ribcage)))
      (case type
        ((begin-form)
         (let ((forms (parse-begin e w ae #t)))
           (if (null? forms)
               (chi-void)
               (chi-top-sequence forms r w ae ctem rtem meta? top-ribcage meta-residualize!))))
        ((local-syntax-form)
         (let-values (((forms r mr w ae) (chi-local-syntax value e r r w ae)))
          ; mr should be same as r here
           (chi-top-sequence forms r w ae ctem rtem meta? top-ribcage meta-residualize!)))
        ((eval-when-form)
         (let-values (((when-list forms) (parse-eval-when e w ae)))
           (let ((ctem (update-mode-set when-list ctem))
                 (rtem (update-mode-set when-list rtem)))
             (if (and (null? ctem) (null? rtem))
                 (chi-void)
                 (chi-top-sequence forms r w ae ctem rtem meta? top-ribcage meta-residualize!)))))
        ((meta-form) (chi-top (parse-meta e w ae) r w ctem rtem #t top-ribcage meta-residualize! #t))
        ((define-syntax-form)
         (let-values (((id rhs w) (parse-define-syntax e w ae)))
           (let ((id (wrap id w)))
             (when (displaced-lexical? id r) (displaced-lexical-error id))
             (unless (top-ribcage-mutable? top-ribcage)
               (syntax-error (source-wrap e w ae)
                 "invalid definition in read-only environment"))
             (let ((sym (id-sym-name id)))
               (let-values (((valsym bound-id) (top-id-bound-var-name sym (wrap-marks (syntax-object-wrap id)) top-ribcage)))
                 (unless (eq? (id-var-name id empty-wrap) valsym)
                   (syntax-error (source-wrap e w ae)
                     "definition not permitted"))
                 (when (read-only-binding? valsym)
                   (syntax-error (source-wrap e w ae)
                     "invalid definition of read-only identifier"))
                 (ct-eval/residualize2 ctem
                   (lambda ()
                     (build-cte-install
                       bound-id
                       (chi rhs r r w #t)
                       (top-ribcage-key top-ribcage)))))))))
        ((define-form)
         (let-values (((id rhs w) (parse-define e w ae)))
           (let ((id (wrap id w)))
             (when (displaced-lexical? id r) (displaced-lexical-error id))
             (unless (top-ribcage-mutable? top-ribcage)
               (syntax-error (source-wrap e w ae)
                 "invalid definition in read-only environment"))
             (let ((sym (id-sym-name id)))
               (let-values (((valsym bound-id) (top-id-bound-var-name sym (wrap-marks (syntax-object-wrap id)) top-ribcage)))
                 (unless (eq? (id-var-name id empty-wrap) valsym)
                   (syntax-error (source-wrap e w ae)
                     "definition not permitted"))
                 (when (read-only-binding? valsym)
                   (syntax-error (source-wrap e w ae)
                     "invalid definition of read-only identifier"))
                 (if meta?
                     (ct-eval/residualize2 ctem
                       (lambda ()
                         (build-sequence no-source
                           (list
                             (build-cte-install bound-id
                               (build-data no-source (make-binding 'meta-variable valsym))
                               (top-ribcage-key top-ribcage))
                             (build-global-definition ae valsym (chi rhs r r w #t))))))
                    ; make sure compile-time definitions occur before we
                    ; expand the run-time code
                     (let ((x (ct-eval/residualize2 ctem
                                (lambda ()
                                  (build-cte-install
                                    bound-id
                                    (build-data no-source (make-binding 'global valsym))
                                    (top-ribcage-key top-ribcage))))))
                       (build-sequence no-source
                         (list
                           x
                           (rt-eval/residualize rtem
                             (lambda ()
                               (build-global-definition ae valsym (chi rhs r r w #f)))))))))
             ))))
        (($module-form)
         (let ((ribcage (make-empty-ribcage)))
           (let-values (((orig id exports forms)
                         (parse-module e w ae
                           (make-wrap (wrap-marks w) (cons ribcage (wrap-subst w))))))
             (when (displaced-lexical? id r) (displaced-lexical-error (wrap id w)))
             (unless (top-ribcage-mutable? top-ribcage)
               (syntax-error orig
                 "invalid definition in read-only environment"))
             (chi-top-module orig r r top-ribcage ribcage ctem rtem meta? id exports forms meta-residualize!))))
        (($import-form)
         (let-values (((orig only? mid) (parse-import e w ae)))
           (unless (top-ribcage-mutable? top-ribcage)
             (syntax-error orig
               "invalid definition in read-only environment"))
           (ct-eval/residualize2 ctem
             (lambda ()
               (let ((binding (lookup (id-var-name mid empty-wrap) null-env)))
                 (case (binding-type binding)
                   (($module) (do-top-import only? top-ribcage mid (interface-token (binding-value binding))))
                   ((displaced-lexical) (displaced-lexical-error mid))
                   (else (syntax-error mid "unknown module"))))))))
        ((alias-form)
         (let-values (((new-id old-id) (parse-alias e w ae)))
           (let ((new-id (wrap new-id w)))
             (when (displaced-lexical? new-id r) (displaced-lexical-error new-id))
             (unless (top-ribcage-mutable? top-ribcage)
               (syntax-error (source-wrap e w ae)
                 "invalid definition in read-only environment"))
             (let ((sym (id-sym-name new-id)))
               (let-values (((valsym bound-id) (top-id-bound-var-name sym (wrap-marks (syntax-object-wrap new-id)) top-ribcage)))
                 (unless (eq? (id-var-name new-id empty-wrap) valsym)
                   (syntax-error (source-wrap e w ae)
                     "definition not permitted"))
                 (when (read-only-binding? valsym)
                   (syntax-error (source-wrap e w ae)
                     "invalid definition of read-only identifier"))
                 (ct-eval/residualize2 ctem
                   (lambda ()
                     (build-cte-install
                       (make-resolved-id sym (wrap-marks (syntax-object-wrap new-id)) (id-var-name old-id w))
                       (build-data no-source (make-binding 'do-alias #f))
                       (top-ribcage-key top-ribcage)))))))))
        (else
         (when meta-seen? (syntax-error (source-wrap e w ae) "invalid meta definition"))
         (if meta?
             (let ((x (chi-expr type value e r r w ae #t)))
               (top-level-eval-hook x)
               (ct-eval/residualize3 ctem void (lambda () x)))
             (rt-eval/residualize rtem
               (lambda ()
                 (chi-expr type value e r r w ae #f)))))))))

(define flatten-exports
  (lambda (exports)
    (let loop ((exports exports) (ls '()))
      (if (null? exports)
          ls
          (loop (cdr exports)
                (if (pair? (car exports))
                    (loop (car exports) ls)
                    (cons (car exports) ls)))))))


(define-structure (interface marks exports token))

;; leaves interfaces unresolved so that indirect labels can be followed.
;; (can't resolve until indirect labels have their final value)
(define make-unresolved-interface
 ; trim out implicit exports
  (lambda (mid exports)
    (make-interface
      (wrap-marks (syntax-object-wrap mid))
      (list->vector (map (lambda (x) (if (pair? x) (car x) x)) exports))
      #f)))

(define make-resolved-interface
 ; trim out implicit exports & resolve others to actual top-level symbol
  (lambda (mid exports token)
    (make-interface
      (wrap-marks (syntax-object-wrap mid))
      (list->vector (map (lambda (x) (id->resolved-id (if (pair? x) (car x) x))) exports))
      token)))

(define-structure (module-binding type id label imps val exported))
(define create-module-binding
  (lambda (type id label imps val)
    (make-module-binding type id label imps val #f)))

;;; frobs represent body forms
(define-structure (frob e meta?))

(define chi-top-module
  (lambda (orig r mr top-ribcage ribcage ctem rtem meta? id exports forms meta-residualize!)
    (let ((fexports (flatten-exports exports)))
      (let-values (((r mr bindings inits)
                    (chi-external ribcage orig
                      (map (lambda (d) (make-frob d meta?)) forms) r mr ctem exports fexports
                      meta-residualize!)))
       ; identify exported identifiers, create ctdefs
        (let process-exports ((fexports fexports) (ctdefs (lambda () '())))
          (if (null? fexports)
             ; remaining bindings are either identified global vars,
             ; local vars, or local compile-time entities
             ; dts: type (local/global)
             ; dvs & des: define lhs & rhs
              (let process-locals ((bs bindings) (r r) (dts '()) (dvs '()) (des '()))
                (if (null? bs)
                    (let ((des (chi-frobs des r mr #f))
                          (inits (chi-frobs inits r mr #f)))
                      (build-sequence no-source
                        (append
                         ; we wait to establish global compile-time definitions so that
                         ; expansion of des use local versions of modules and macros
                         ; in case ctem tells us not to eval ctdefs now.  this means that
                         ; local code can use exported compile-time values (modules, macros,
                         ; meta variables) just as it can unexported ones.
                          (ctdefs)
                          (list
                            (ct-eval/residualize2 ctem
                              (lambda ()
                                (let ((sym (id-sym-name id)))
                                  (let* ((token (generate-id sym))
                                         (b (build-data no-source
                                              (make-binding '$module
                                                (make-resolved-interface id exports token)))))
                                    (let-values (((valsym bound-id)
                                                  (top-id-bound-var-name sym
                                                    (wrap-marks (syntax-object-wrap id))
                                                    top-ribcage)))
                                      (unless (eq? (id-var-name id empty-wrap) valsym)
                                        (syntax-error orig
                                          "definition not permitted"))
                                      (when (read-only-binding? valsym)
                                        (syntax-error orig
                                          "invalid definition of read-only identifier"))
                                      (build-cte-install bound-id b
                                        (top-ribcage-key top-ribcage)))))))
                            (rt-eval/residualize rtem
                              (lambda ()
                                (build-top-module no-source dts dvs des
                                  (if (null? inits)
                                      (chi-void)
                                      (build-sequence no-source
                                        (append inits (list (chi-void))))))))))))
                    (let ((b (car bs)) (bs (cdr bs)))
                      (let ((t (module-binding-type b)))
                        (case (module-binding-type b)
                          ((define-form)
                           (let ((label (get-indirect-label (module-binding-label b))))
                             (if (module-binding-exported b)
                                 (let ((var (module-binding-id b)))
                                   (process-locals bs r (cons 'global dts) (cons label dvs)
                                     (cons (module-binding-val b) des)))
                                 (let ((var (gen-var (module-binding-id b))))
                                   (process-locals bs
                                    ; add lexical bindings only to run-time environment
                                     (extend-env label (make-binding 'lexical var) r)
                                     (cons 'local dts) (cons var dvs)
                                     (cons (module-binding-val b) des))))))
                          ((ctdefine-form define-syntax-form $module-form alias-form) (process-locals bs r dts dvs des))
;***                           (else (error 'sc-expand-internal "unexpected module binding type ~s" t)))))))
                          (else (error "unexpected module binding type" t)))))))
              (let ((id (car fexports)) (fexports (cdr fexports)))
                (let loop ((bs bindings))
                  (if (null? bs)
                     ; must be rexport from an imported module
                      (process-exports fexports ctdefs)
                      (let ((b (car bs)) (bs (cdr bs)))
                       ; following formerly used bound-id=?, but free-id=? can prevent false positives
                       ; and is okay since the substitutions have already been applied
                        (if (free-id=? (module-binding-id b) id)
                            (if (module-binding-exported b)
                                (process-exports fexports ctdefs)
                                (let* ((t (module-binding-type b))
                                       (label (module-binding-label b))
                                       (imps (module-binding-imps b))
                                       (fexports (append imps fexports)))
                                  (set-module-binding-exported! b #t)
                                  (case t
                                    ((define-form)
                                     (let ((sym (generate-id (id-sym-name id))))
                                       (set-indirect-label! label sym)
                                       (process-exports fexports ctdefs)))
                                    ((ctdefine-form)
                                     (let ((b (module-binding-val b)))
                                       (process-exports fexports
                                         (lambda ()
                                           (let ((sym (binding-value b)))
                                             (set-indirect-label! label sym)
                                             (cons (ct-eval/residualize3 ctem
                                                     (lambda () (put-cte-hook sym b))
                                                     (lambda () (build-cte-install sym (build-data no-source b) #f)))
                                                   (ctdefs)))))))
                                    ((define-syntax-form)
                                     (let ((sym (generate-id (id-sym-name id))))
                                       (process-exports fexports
                                         (lambda () 
                                           (let ((local-label (get-indirect-label label)))
                                             (set-indirect-label! label sym)
                                             (cons
                                               (ct-eval/residualize3 ctem
                                                 (lambda () (put-cte-hook sym (car (module-binding-val b))))
                                                 (lambda () (build-cte-install sym (cdr (module-binding-val b)) #f)))
                                               (ctdefs)))))))
                                    (($module-form)
                                     (let ((sym (generate-id (id-sym-name id)))
                                           (exports (module-binding-val b)))
                                       (process-exports (append (flatten-exports exports) fexports)
                                         (lambda ()
                                           (set-indirect-label! label sym)
                                           (let ((rest (ctdefs))) ; set indirect labels before resolving
                                             (let ((x (make-binding '$module (make-resolved-interface id exports sym))))
                                               (cons (ct-eval/residualize3 ctem
                                                       (lambda () (put-cte-hook sym x))
                                                       (lambda () (build-cte-install sym (build-data no-source x) #f)))
                                                     rest)))))))
                                    ((alias-form)
                                     (process-exports
                                       fexports
                                       (lambda ()
                                         (let ((rest (ctdefs))) ; set indirect labels before resolving
                                           (when (indirect-label? label)
                                             (unless (symbol? (get-indirect-label label))
                                               (syntax-error (module-binding-id b) "unexported target of alias")))
                                           rest))))
;***                                     (else (error 'sc-expand-internal "unexpected module binding type ~s" t)))))
                                    (else (error "unexpected module binding type" t)))))
                            (loop bs))))))))))))

(define id-set-diff
  (lambda (exports defs)
    (cond
      ((null? exports) '())
      ((bound-id-member? (car exports) defs) (id-set-diff (cdr exports) defs))
      (else (cons (car exports) (id-set-diff (cdr exports) defs))))))

(define check-module-exports
  ; After processing the definitions of a module this is called to verify that the
  ; module has defined or imported each exported identifier.  Because ids in fexports are
  ; wrapped with the given ribcage, they will contain substitutions for anything defined
  ; or imported here.  These subsitutions can be used by do-import! and do-import-top! to
  ; provide access to reexported bindings, for example.
  (lambda (source-exp fexports ids)
    (define defined?
      (lambda (e ids)
        (ormap (lambda (x)
                 (if (import-interface? x)
                     (let ((x.iface (import-interface-interface x))
                           (x.new-marks (import-interface-new-marks x)))
                       (cond
                         ((interface-token x.iface) =>
                          (lambda (token)
                            (lookup-import-binding-name (id-sym-name e) (id-marks e) token x.new-marks)))
                         (else
                          (let ((v (interface-exports x.iface)))
                            (let lp ((i (fx- (vector-length v) 1)))
                              (and (fx>= i 0)
                                   (or (let ((id (vector-ref v i)))
                                         (help-bound-id=?
                                           (id-sym-name id)
                                           (join-marks x.new-marks (id-marks id))
                                           (id-sym-name e) (id-marks e)))
                                       (lp (fx- i 1)))))))))
                     (bound-id=? e x)))
               ids)))
    (let loop ((fexports fexports) (missing '()))
      (if (null? fexports)
          (unless (null? missing)
            (syntax-error (car missing)
              (if (= (length missing) 1)
                  "missing definition for export"
                  "missing definition for multiple exports, including")))
          (let ((e (car fexports)) (fexports (cdr fexports)))
            (if (defined? e ids)
                (loop fexports missing)
                (loop fexports (cons e missing))))))))

(define check-defined-ids
  (lambda (source-exp ls)
    (define vfold
      (lambda (v p cls)
        (let ((len (vector-length v)))
          (let lp ((i 0) (cls cls))
            (if (fx= i len)
                cls
                (lp (fx+ i 1) (p (vector-ref v i) cls)))))))
    (define conflicts
      (lambda (x y cls)
        (if (import-interface? x)
            (let ((x.iface (import-interface-interface x))
                  (x.new-marks (import-interface-new-marks x)))
              (if (import-interface? y)
                  (let ((y.iface (import-interface-interface y))
                        (y.new-marks (import-interface-new-marks y)))
                    (let ((xe (interface-exports x.iface)) (ye (interface-exports y.iface)))
                      (if (fx> (vector-length xe) (vector-length ye))
                          (vfold ye
                            (lambda (id cls)
                              (id-iface-conflicts id y.new-marks x.iface x.new-marks cls)) cls)
                          (vfold xe
                            (lambda (id cls)
                              (id-iface-conflicts id x.new-marks y.iface y.new-marks cls)) cls))))
                  (id-iface-conflicts y '() x.iface x.new-marks cls)))
            (if (import-interface? y)
                (let ((y.iface (import-interface-interface y))
                      (y.new-marks (import-interface-new-marks y)))
                  (id-iface-conflicts x '() y.iface y.new-marks cls))
                (if (bound-id=? x y) (cons x cls) cls)))))
     (define id-iface-conflicts
       (lambda (id id.new-marks iface iface.new-marks cls)
         (let ((id.sym (id-sym-name id))
               (id.marks (join-marks id.new-marks (id-marks id))))
           (cond
             ((interface-token iface) =>
              (lambda (token)
                (if (lookup-import-binding-name id.sym id.marks token iface.new-marks)
                    (cons id cls)
                    cls)))
             (else
              (vfold (interface-exports iface)
                     (lambda (*id cls)
                       (let ((*id.sym (id-sym-name *id))
                             (*id.marks (join-marks iface.new-marks (id-marks *id))))
                         (if (help-bound-id=? *id.sym *id.marks id.sym id.marks)
                             (cons *id cls)
                             cls)))
                     cls))))))
     (unless (null? ls)
       (let lp ((x (car ls)) (ls (cdr ls)) (cls '()))
         (if (null? ls)
             (unless (null? cls)
               (let ((cls (syntax-object->datum cls)))
                 (syntax-error source-exp "duplicate definition for "
                  (symbol->string (car cls))
                   " in")))
             (let lp2 ((ls2 ls) (cls cls))
               (if (null? ls2)
                   (lp (car ls) (cdr ls) cls)
                   (lp2 (cdr ls2) (conflicts x (car ls2) cls)))))))))

(define chi-external
  (lambda (ribcage source-exp body r mr ctem exports fexports meta-residualize!)
    (define return
      (lambda (r mr bindings ids inits)
        (check-defined-ids source-exp ids)
        (check-module-exports source-exp fexports ids)
        (values r mr bindings inits)))
    (define get-implicit-exports
      (lambda (id)
        (let f ((exports exports))
          (if (null? exports)
              '()
              (if (and (pair? (car exports)) (bound-id=? id (caar exports)))
                  (flatten-exports (cdar exports))
                  (f (cdr exports)))))))
    (define update-imp-exports
      (lambda (bindings exports)
        (let ((exports (map (lambda (x) (if (pair? x) (car x) x)) exports)))
          (map (lambda (b)
                 (let ((id (module-binding-id b)))
                   (if (not (bound-id-member? id exports))
                       b
                       (create-module-binding
                         (module-binding-type b)
                         id
                         (module-binding-label b)
                         (append (get-implicit-exports id) (module-binding-imps b))
                         (module-binding-val b)))))
               bindings))))
    (let parse ((body body) (r r) (mr mr) (ids '()) (bindings '()) (inits '()) (meta-seen? #f))
      (if (null? body)
          (return r mr bindings ids inits)
          (let* ((fr (car body)) (e (frob-e fr)) (meta? (frob-meta? fr)))
            (let-values (((type value e w ae) (syntax-type e r empty-wrap no-source ribcage)))
              (case type
                ((define-form)
                 (let-values (((id rhs w) (parse-define e w ae)))
                   (let* ((id (wrap id w))
                          (label (gen-indirect-label))
                          (imps (get-implicit-exports id)))
                     (extend-ribcage! ribcage id label)
                     (cond
                       (meta?
                        (let* ((sym (generate-id (id-sym-name id)))
                               (b (make-binding 'meta-variable sym)))
                         ; add meta bindings only to meta environment
                          (let ((mr (extend-env (get-indirect-label label) b mr)))
                            (let ((exp (chi rhs mr mr w #t)))
                              (define-top-level-value-hook sym (top-level-eval-hook exp))
                              (meta-residualize!
                                (ct-eval/residualize3 ctem
                                  void
                                  (lambda () (build-global-definition no-source sym exp))))
                              (parse (cdr body) r mr
                                (cons id ids)
                                (cons (create-module-binding 'ctdefine-form id label imps b) bindings)
                                inits
                                #f)))))
                       (else
                        (parse (cdr body) r mr
                          (cons id ids)
                          (cons (create-module-binding type id label
                                  imps (make-frob (wrap rhs w) meta?))
                                bindings)
                          inits
                          #f))))))
                ((define-syntax-form)
                 (let-values (((id rhs w) (parse-define-syntax e w ae)))
                   (let* ((id (wrap id w))
                          (label (gen-indirect-label))
                          (imps (get-implicit-exports id))
                          (exp (chi rhs mr mr w #t)))
                     (extend-ribcage! ribcage id label)
                     (let ((l (get-indirect-label label)) (b (defer-or-eval-transformer top-level-eval-hook exp)))
                       (parse (cdr body)
                         (extend-env l b r)
                         (extend-env l b mr)
                         (cons id ids)
                         (cons (create-module-binding type id label imps (cons b exp))
                               bindings)
                         inits
                         #f)))))
                (($module-form)
                 (let* ((*ribcage (make-empty-ribcage))
                        (*w (make-wrap (wrap-marks w) (cons *ribcage (wrap-subst w)))))
                   (let-values (((orig id *exports forms) (parse-module e w ae *w)))
                     (let-values (((r mr *bindings *inits)
                                   (chi-external *ribcage orig
                                     (map (lambda (d) (make-frob d meta?)) forms)
                                     r mr ctem *exports (flatten-exports *exports) meta-residualize!)))
                       (let ((iface (make-unresolved-interface id *exports))
                             (bindings (append *bindings bindings))
                             (inits (append inits *inits))
                             (label (gen-indirect-label))
                             (imps (get-implicit-exports id)))
                         (extend-ribcage! ribcage id label)
                         (let ((l (get-indirect-label label)) (b (make-binding '$module iface)))
                           (parse (cdr body)
                             (extend-env l b r)
                             (extend-env l b mr)
                             (cons id ids)
                             (cons (create-module-binding type id label imps *exports) bindings)
                             inits
                             #f)))))))
               (($import-form)
                (let-values (((orig only? mid) (parse-import e w ae)))
                  (let ((mlabel (id-var-name mid empty-wrap)))
                    (let ((binding (lookup mlabel r)))
                      (case (binding-type binding)
                        (($module)
                         (let* ((iface (binding-value binding))
                                (import-iface (make-import-interface iface (import-mark-delta mid iface))))
                           (when only? (extend-ribcage-barrier! ribcage mid))
                           (do-import! import-iface ribcage)
                           (parse (cdr body) r mr
                             (cons import-iface ids)
                             (update-imp-exports bindings (vector->list (interface-exports iface)))
                             inits
                             #f)))
                        ((displaced-lexical) (displaced-lexical-error mid))
                        (else (syntax-error mid "unknown module")))))))
               ((alias-form)
                (let-values (((new-id old-id) (parse-alias e w ae)))
                  (let* ((new-id (wrap new-id w))
                         (label (id-var-name-loc old-id w))
                         (imps (get-implicit-exports new-id)))
                    (extend-ribcage! ribcage new-id label)
                    (parse (cdr body) r mr
                      (cons new-id ids)
                      (cons (create-module-binding type new-id label imps #f)
                            bindings)
                      inits
                      #f))))
                ((begin-form)
                 (parse (let f ((forms (parse-begin e w ae #t)))
                          (if (null? forms)
                              (cdr body)
                              (cons (make-frob (wrap (car forms) w) meta?)
                                    (f (cdr forms)))))
                   r mr ids bindings inits #f))
                ((eval-when-form)
                 (let-values (((when-list forms) (parse-eval-when e w ae)))
                    (parse (if (memq 'eval when-list) ; mode set is implicitly (E)
                               (let f ((forms forms))
                                 (if (null? forms)
                                     (cdr body)
                                     (cons (make-frob (wrap (car forms) w) meta?)
                                           (f (cdr forms)))))
                               (cdr body))
                      r mr ids bindings inits #f)))
                ((meta-form)
                 (parse (cons (make-frob (wrap (parse-meta e w ae) w) #t)
                              (cdr body))
                   r mr ids bindings inits #t))
                ((local-syntax-form)
                 (let-values (((forms r mr w ae) (chi-local-syntax value e r mr w ae)))
                   (parse (let f ((forms forms))
                            (if (null? forms)
                                (cdr body)
                                (cons (make-frob (wrap (car forms) w) meta?)
                                      (f (cdr forms)))))
                     r mr ids bindings inits #f)))
                (else ; found an init expression
                 (when meta-seen? (syntax-error (source-wrap e w ae) "invalid meta definition"))
                 (let f ((body (cons (make-frob (source-wrap e w ae) meta?) (cdr body))))
                   (if (or (null? body) (not (frob-meta? (car body))))
                       (return r mr bindings ids (append inits body))
                       (begin
                        ; expand and eval meta inits for effect only
                         (let ((x (chi-meta-frob (car body) mr)))
                           (top-level-eval-hook x)
                           (meta-residualize! (ct-eval/residualize3 ctem void (lambda () x))))
                         (f (cdr body)))))))))))))

(define vmap
  (lambda (fn v)
    (do ((i (fx- (vector-length v) 1) (fx- i 1))
         (ls '() (cons (fn (vector-ref v i)) ls)))
        ((fx< i 0) ls))))

(define vfor-each
  (lambda (fn v)
    (let ((len (vector-length v)))
      (do ((i 0 (fx+ i 1)))
          ((fx= i len))
        (fn (vector-ref v i))))))

(define do-top-import
  (lambda (import-only? top-ribcage mid token)
   ; silently treat import-only like regular import at top level
    (build-cte-install mid
      (build-data no-source
        (make-binding 'do-import token))
      (top-ribcage-key top-ribcage))))

(define update-mode-set
  (let ((table
         '((L (load . L) (compile . C) (visit . V) (revisit . R) (eval . -))
           (C (load . -) (compile . -) (visit . -) (revisit . -) (eval . C))
           (V (load . V) (compile . C) (visit . V) (revisit . -) (eval . -))
           (R (load . R) (compile . C) (visit . -) (revisit . R) (eval . -))
           (E (load . -) (compile . -) (visit . -) (revisit . -) (eval . E)))))
    (lambda (when-list mode-set)
      (define remq
        (lambda (x ls)
          (if (null? ls)
              '()
              (if (eq? (car ls) x)
                  (remq x (cdr ls))
                  (cons (car ls) (remq x (cdr ls)))))))
      (remq '-
        (apply append
          (map (lambda (m)
                 (let ((row (cdr (assq m table))))
                   (map (lambda (s) (cdr (assq s row)))
                        when-list)))
               mode-set))))))

(define initial-mode-set
  (lambda (when-list compiling-a-file)
    (apply append
      (map (lambda (s)
             (if compiling-a-file
                 (case s
                   ((compile) '(C))
                   ((load) '(L))
                   ((visit) '(V))
                   ((revisit) '(R))
                   (else '()))
                 (case s
                   ((eval) '(E))
                   (else '()))))
           when-list))))

(define rt-eval/residualize
  (lambda (rtem thunk)
    (if (memq 'E rtem)
        (thunk)
        (let ((thunk (if (memq 'C rtem)
                         (let ((x (thunk)))
                           (top-level-eval-hook x)
                           (lambda () x))
                         thunk)))
          (if (memq 'V rtem)
              (if (or (memq 'L rtem) (memq 'R rtem))
                  (thunk) ; visit-revisit
                  (build-visit-only (thunk)))
              (if (or (memq 'L rtem) (memq 'R rtem))
                  (build-revisit-only (thunk))
                  (chi-void)))))))

(define ct-eval/residualize2
  (lambda (ctem thunk)
    (let ((t #f))
      (ct-eval/residualize3 ctem
        (lambda ()
          (unless t (set! t (thunk)))
          (top-level-eval-hook t))
        (lambda () (or t (thunk)))))))
(define ct-eval/residualize3
  (lambda (ctem eval-thunk residualize-thunk)
    (if (memq 'E ctem)
        (begin (eval-thunk) (chi-void))
        (begin
          (when (memq 'C ctem) (eval-thunk))
          (if (memq 'R ctem)
              (if (or (memq 'L ctem) (memq 'V ctem))
                  (residualize-thunk) ; visit-revisit
                  (build-revisit-only (residualize-thunk)))
              (if (or (memq 'L ctem) (memq 'V ctem))
                  (build-visit-only (residualize-thunk))
                  (chi-void)))))))

(define chi-frobs
  (lambda (frob* r mr m?)
    (map (lambda (x) (chi (frob-e x) r mr empty-wrap m?)) frob*)))

(define chi-meta-frob
  (lambda (x mr)
    (chi (frob-e x) mr mr empty-wrap #t)))

(define chi-sequence
  (lambda (body r mr w ae m?)
    (build-sequence ae
      (let dobody ((body body))
        (if (null? body)
            '()
            (let ((first (chi (car body) r mr w m?)))
              (cons first (dobody (cdr body)))))))))

(define chi
  (lambda (e r mr w m?)
    (let-values (((type value e w ae) (syntax-type e r w no-source #f)))
      (chi-expr type value e r mr w ae m?))))

(define chi-expr
  (lambda (type value e r mr w ae m?)
    (case type
      ((lexical)
       (build-lexical-reference 'value ae value))
      ((core) (value e r mr w ae m?))
      ((lexical-call)
       (chi-application
         (build-lexical-reference 'fun
           (let ((x (car e)))
             (if (syntax-object? x) (syntax-object-expression x) x))
           value)
         e r mr w ae m?))
      ((constant) (build-data ae (strip (source-wrap e w ae) empty-wrap)))
      ((global) (build-global-reference ae value))
      ((meta-variable)
       (if m?
           (build-global-reference ae value)
           (displaced-lexical-error (source-wrap e w ae))))
      ((call) (chi-application (chi (car e) r mr w m?) e r mr w ae m?))
      ((begin-form) (chi-sequence (parse-begin e w ae #f) r mr w ae m?))
      ((local-syntax-form)
       (let-values (((forms r mr w ae) (chi-local-syntax value e r mr w ae)))
         (chi-sequence forms r mr w ae m?)))
      ((eval-when-form)
       (let-values (((when-list forms) (parse-eval-when e w ae)))
         (if (memq 'eval when-list) ; mode set is implicitly (E)
             (chi-sequence forms r mr w ae m?)
             (chi-void))))
      ((meta-form)
       (syntax-error (source-wrap e w ae) "invalid context for meta definition"))
      ((define-form)
       (parse-define e w ae)
       (syntax-error (source-wrap e w ae) "invalid context for definition"))
      ((define-syntax-form)
       (parse-define-syntax e w ae)
       (syntax-error (source-wrap e w ae) "invalid context for definition"))
      (($module-form)
       (let-values (((orig id exports forms) (parse-module e w ae w)))
         (syntax-error orig "invalid context for definition")))
      (($import-form)
       (let-values (((orig only? mid) (parse-import e w ae)))
         (syntax-error orig "invalid context for definition")))
      ((alias-form)
       (parse-alias e w ae)
       (syntax-error (source-wrap e w ae) "invalid context for definition"))
      ((syntax)
       (syntax-error (source-wrap e w ae)
         "reference to pattern variable outside syntax form"))
      ((displaced-lexical) (displaced-lexical-error (source-wrap e w ae)))
      (else (syntax-error (source-wrap e w ae))))))

(define chi-application
  (lambda (x e r mr w ae m?)
    (syntax-case e ()
      ((e0 e1 ...)
       (build-application ae x
         (map (lambda (e) (chi e r mr w m?)) (syntax (e1 ...)))))
      (_ (syntax-error (source-wrap e w ae))))))

(define chi-set!
  (lambda (e r w ae rib)
    (syntax-case e ()
      ((_ id val)
       (id? (syntax id))
       (let ((n (id-var-name (syntax id) w)))
         (let ((b (lookup n r)))
           (case (binding-type b)
             ((macro!)
              (let ((id (wrap (syntax id) w)) (val (wrap (syntax val) w)))
                (syntax-type (chi-macro (binding-value b)
                               `(,(syntax set!) ,id ,val)
                               r empty-wrap #f rib) r empty-wrap #f rib)))
             (else
              (values 'core
                (lambda (e r mr w ae m?)
                 ; repeat lookup in case we were first expression (init) in
                 ; module or lambda body.  we repeat id-var-name as well,
                 ; although this is only necessary if we allow inits to
                 ; preced definitions
                  (let ((val (chi (syntax val) r mr w m?))
                        (n (id-var-name (syntax id) w)))
                    (let ((b (lookup n r)))
                      (case (binding-type b)
                        ((lexical) (build-lexical-assignment ae (binding-value b) val))
                        ((global)
                         (let ((sym (binding-value b)))
                           (when (read-only-binding? n)
                             (syntax-error (source-wrap e w ae)
                               "invalid assignment to read-only variable"))
                           (build-global-assignment ae sym val)))
                        ((meta-variable)
                         (if m?
                             (build-global-assignment ae (binding-value b) val)
                             (displaced-lexical-error (wrap (syntax id) w))))
                        ((displaced-lexical)
                         (displaced-lexical-error (wrap (syntax id) w)))
                        (else (syntax-error (source-wrap e w ae)))))))
                e w ae))))))
      (_ (syntax-error (source-wrap e w ae))))))

(define chi-macro
  (lambda (p e r w ae rib)
    (define rebuild-macro-output
      (lambda (x m)
        (cond ((pair? x)
               (cons (rebuild-macro-output (car x) m)
                     (rebuild-macro-output (cdr x) m)))
              ((syntax-object? x)
               (let ((w (syntax-object-wrap x)))
                 (let ((ms (wrap-marks w)) (s (wrap-subst w)))
                   (make-syntax-object (syntax-object-expression x)
                     (if (and (pair? ms) (eq? (car ms) the-anti-mark))
                         (make-wrap (cdr ms) (cdr s))
                         (make-wrap (cons m ms)
                           (if rib
                               (cons rib (cons 'shift s))
                               (cons 'shift s))))))))
              ((vector? x)
               (let* ((n (vector-length x)) (v (make-vector n)))
                 (do ((i 0 (fx+ i 1)))
                     ((fx= i n) v)
                     (vector-set! v i
                       (rebuild-macro-output (vector-ref x i) m)))))
              ((symbol? x)
               (syntax-error (source-wrap e w ae)
                 "encountered raw symbol "
                (symbol->string x)
                 " in output of macro"))
              (else x))))
    (rebuild-macro-output
      (let ((out (p (source-wrap e (anti-mark w) ae))))
        (if (procedure? out)
            (out (lambda (id)
                   (unless (identifier? id)
                     (syntax-error id
                       "environment argument is not an identifier"))
                   (lookup (id-var-name id empty-wrap) r)))
            out))
      (new-mark))))

(define chi-body
  (lambda (body outer-form r mr w m?)
    (let* ((ribcage (make-empty-ribcage))
           (w (make-wrap (wrap-marks w) (cons ribcage (wrap-subst w))))
           (body (map (lambda (x) (make-frob (wrap x w) #f)) body)))
      (let-values (((r mr exprs ids vars vals inits)
                    (chi-internal ribcage outer-form body r mr m?)))
        (when (null? exprs) (syntax-error outer-form "no expressions in body"))
        (build-body no-source
          (reverse vars) (chi-frobs (reverse vals) r mr m?)
          (build-sequence no-source
            (chi-frobs (append inits exprs) r mr m?)))))))

(define chi-internal
  ;; In processing the forms of the body, we create a new, empty wrap.
  ;; This wrap is augmented (destructively) each time we discover that
  ;; the next form is a definition.  This is done:
  ;;
  ;;   (1) to allow the first nondefinition form to be a call to
  ;;       one of the defined ids even if the id previously denoted a
  ;;       definition keyword or keyword for a macro expanding into a
  ;;       definition;
  ;;   (2) to prevent subsequent definition forms (but unfortunately
  ;;       not earlier ones) and the first nondefinition form from
  ;;       confusing one of the bound identifiers for an auxiliary
  ;;       keyword; and
  ;;   (3) so that we do not need to restart the expansion of the
  ;;       first nondefinition form, which is problematic anyway
  ;;       since it might be the first element of a begin that we
  ;;       have just spliced into the body (meaning if we restarted,
  ;;       we'd really need to restart with the begin or the macro
  ;;       call that expanded into the begin, and we'd have to give
  ;;       up allowing (begin <defn>+ <expr>+), which is itself
  ;;       problematic since we don't know if a begin contains only
  ;;       definitions until we've expanded it).
  ;;
  ;; Subforms of a begin, let-syntax, or letrec-syntax are spliced
  ;; into the body.
  ;;
  ;; outer-form is fully wrapped w/source
  (lambda (ribcage source-exp body r mr m?)
    (define return
      (lambda (r mr exprs ids vars vals inits)
        (check-defined-ids source-exp ids)
        (values r mr exprs ids vars vals inits)))
    (let parse ((body body) (r r) (mr mr) (ids '()) (vars '()) (vals '()) (inits '()) (meta-seen? #f))
      (if (null? body)
          (return r mr body ids vars vals inits)
          (let* ((fr (car body)) (e (frob-e fr)) (meta? (frob-meta? fr)))
            (let-values (((type value e w ae) (syntax-type e r empty-wrap no-source ribcage)))
              (case type
                ((define-form)
                 (let-values (((id rhs w) (parse-define e w ae)))
                   (let ((id (wrap id w)) (label (gen-label)))
                     (cond
                       (meta?
                        (let ((sym (generate-id (id-sym-name id))))
                          (extend-ribcage! ribcage id label)
                         ; add meta bindings only to meta environment
                         ; so visible only to next higher level and beyond
                          (let ((mr (extend-env label (make-binding 'meta-variable sym) mr)))
                            (define-top-level-value-hook sym
                              (top-level-eval-hook (chi rhs mr mr w #t)))
                            (parse (cdr body) r mr (cons id ids) vars vals inits #f))))
                       (else
                        (let ((var (gen-var id)))
                          (extend-ribcage! ribcage id label)
                         ; add lexical bindings only to run-time environment
                          (parse (cdr body)
                            (extend-env label (make-binding 'lexical var) r)
                            mr
                            (cons id ids)
                            (cons var vars)
                            (cons (make-frob (wrap rhs w) meta?) vals)
                            inits
                            #f)))))))
                ((define-syntax-form)
                 (let-values (((id rhs w) (parse-define-syntax e w ae)))
                   (let ((id (wrap id w))
                         (label (gen-label))
                         (exp (chi rhs mr mr w #t)))
                     (extend-ribcage! ribcage id label)
                     (let ((b (defer-or-eval-transformer local-eval-hook exp)))
                       (parse (cdr body)
                         (extend-env label b r) (extend-env label b mr)
                         (cons id ids) vars vals inits #f)))))
                (($module-form)
                 (let* ((*ribcage (make-empty-ribcage))
                        (*w (make-wrap (wrap-marks w) (cons *ribcage (wrap-subst w)))))
                   (let-values (((orig id exports forms) (parse-module e w ae *w)))
                     (let-values (((r mr *body *ids *vars *vals *inits)
                                   (chi-internal *ribcage orig
                                     (map (lambda (d) (make-frob d meta?)) forms)
                                     r mr m?)))
                      ; valid bound ids checked already by chi-internal
                       (check-module-exports source-exp (flatten-exports exports) *ids)
                       (let ((iface (make-resolved-interface id exports #f))
                             (vars (append *vars vars))
                             (vals (append *vals vals))
                             (inits (append inits *inits *body))
                             (label (gen-label)))
                         (extend-ribcage! ribcage id label)
                         (let ((b (make-binding '$module iface)))
                           (parse (cdr body)
                             (extend-env label b r) (extend-env label b mr)
                             (cons id ids) vars vals inits #f)))))))
               (($import-form)
                (let-values (((orig only? mid) (parse-import e w ae)))
                  (let ((mlabel (id-var-name mid empty-wrap)))
                    (let ((binding (lookup mlabel r)))
                      (case (binding-type binding)
                        (($module)
                         (let* ((iface (binding-value binding))
                                (import-iface (make-import-interface iface (import-mark-delta mid iface))))
                           (when only? (extend-ribcage-barrier! ribcage mid))
                           (do-import! import-iface ribcage)
                           (parse (cdr body) r mr (cons import-iface ids) vars vals inits #f)))
                        ((displaced-lexical) (displaced-lexical-error mid))
                        (else (syntax-error mid "unknown module")))))))
                ((alias-form)
                 (let-values (((new-id old-id) (parse-alias e w ae)))
                   (let ((new-id (wrap new-id w)))
                     (extend-ribcage! ribcage new-id (id-var-name-loc old-id w))
                     (parse (cdr body) r mr
                       (cons new-id ids)
                       vars
                       vals
                       inits
                       #f))))
                ((begin-form)
                 (parse (let f ((forms (parse-begin e w ae #t)))
                          (if (null? forms)
                              (cdr body)
                              (cons (make-frob (wrap (car forms) w) meta?)
                                    (f (cdr forms)))))
                   r mr ids vars vals inits #f))
                ((eval-when-form)
                 (let-values (((when-list forms) (parse-eval-when e w ae)))
                   (parse (if (memq 'eval when-list) ; mode set is implicitly (E)
                              (let f ((forms forms))
                                (if (null? forms)
                                    (cdr body)
                                    (cons (make-frob (wrap (car forms) w) meta?)
                                          (f (cdr forms)))))
                              (cdr body))
                     r mr ids vars vals inits #f)))
                ((meta-form)
                 (parse (cons (make-frob (wrap (parse-meta e w ae) w) #t)
                              (cdr body))
                   r mr ids vars vals inits #t))
                ((local-syntax-form)
                 (let-values (((forms r mr w ae) (chi-local-syntax value e r mr w ae)))
                   (parse (let f ((forms forms))
                            (if (null? forms)
                                (cdr body)
                                (cons (make-frob (wrap (car forms) w) meta?)
                                      (f (cdr forms)))))
                     r mr ids vars vals inits #f)))
                (else ; found a non-definition
                 (when meta-seen? (syntax-error (source-wrap e w ae) "invalid meta definition"))
                 (let f ((body (cons (make-frob (source-wrap e w ae) meta?) (cdr body))))
                   (if (or (null? body) (not (frob-meta? (car body))))
                       (return r mr body ids vars vals inits)
                       (begin
                        ; expand meta inits for effect only
                         (top-level-eval-hook (chi-meta-frob (car body) mr))
                         (f (cdr body)))))))))))))

(define import-mark-delta
 ; returns list of marks layered on top of module id beyond those
 ; cached in the interface
  (lambda (mid iface)
    (diff-marks (id-marks mid) (interface-marks iface))))

(define lookup-import-label
  (lambda (id)
    (let ((label (id-var-name-loc id empty-wrap)))
      (unless label
        (syntax-error id "exported identifier not visible"))
      label)))
  
(define do-import!
  (lambda (import-iface ribcage)
    (let ((ie (interface-exports (import-interface-interface import-iface))))
      (if (<= (vector-length ie) 20)
          (let ((new-marks (import-interface-new-marks import-iface)))
            (vfor-each
              (lambda (id)
                (import-extend-ribcage! ribcage new-marks id
                  (lookup-import-label id)))
              ie))
          (extend-ribcage-subst! ribcage import-iface)))))

(define parse-module
  (lambda (e w ae *w)
    (define listify
      (lambda (exports)
        (if (null? exports)
            '()
            (cons (syntax-case (car exports) ()
                    ((ex ...) (listify (syntax (ex ...))))
                    (x (if (id? (syntax x))
                           (wrap (syntax x) *w)
                           (syntax-error (source-wrap e w ae)
                             "invalid exports list in"))))
                  (listify (cdr exports))))))
    (syntax-case e ()
      ((_ orig mid (ex ...) form ...)
       (id? (syntax mid))
      ; id receives old wrap so it won't be confused with id of same name
      ; defined within the module
       (values (syntax orig) (wrap (syntax mid) w) (listify (syntax (ex ...))) (map (lambda (x) (wrap x *w)) (syntax (form ...)))))
      (_ (syntax-error (source-wrap e w ae))))))

(define parse-import
  (lambda (e w ae)
    (syntax-case e ()
      ((_ orig #t mid)
       (id? (syntax mid))
       (values (syntax orig) #t (wrap (syntax mid) w)))
      ((_ orig #f mid)
       (id? (syntax mid))
       (values (syntax orig) #f (wrap (syntax mid) w)))
      (_ (syntax-error (source-wrap e w ae))))))

(define parse-define
  (lambda (e w ae)
    (syntax-case e ()
      ((_ name val)
       (id? (syntax name))
       (values (syntax name) (syntax val) w))
      ((_ (name . args) e1 e2 ...)
       (and (id? (syntax name))
            (valid-bound-ids? (lambda-var-list (syntax args))))
       (values (wrap (syntax name) w)
               (cons (syntax lambda) (wrap (syntax (args e1 e2 ...)) w))
               empty-wrap))
      ((_ name)
       (id? (syntax name))
       (values (wrap (syntax name) w) (syntax (void)) empty-wrap))
      (_ (syntax-error (source-wrap e w ae))))))

(define parse-define-syntax
  (lambda (e w ae)
    (syntax-case e ()
      ((_ (name id) e1 e2 ...)
       (and (id? (syntax name)) (id? (syntax id)))
       (values (wrap (syntax name) w)
               `(,(syntax lambda) ,(wrap (syntax (id)) w)
                   ,@(wrap (syntax (e1 e2 ...)) w))
               empty-wrap))
      ((_ name val)
       (id? (syntax name))
       (values (syntax name) (syntax val) w))
      (_ (syntax-error (source-wrap e w ae))))))

(define parse-meta
  (lambda (e w ae)
    (syntax-case e ()
      ((_ . form) (syntax form))
      (_ (syntax-error (source-wrap e w ae))))))

(define parse-eval-when
  (lambda (e w ae)
    (syntax-case e ()
      ((_ (x ...) e1 e2 ...)
       (values (chi-when-list (syntax (x ...)) w) (syntax (e1 e2 ...))))
      (_ (syntax-error (source-wrap e w ae))))))

(define parse-alias
  (lambda (e w ae)
    (syntax-case e ()
      ((_ new-id old-id)
       (and (id? (syntax new-id)) (id? (syntax old-id)))
       (values (syntax new-id) (syntax old-id)))
      (_ (syntax-error (source-wrap e w ae))))))

(define parse-begin
  (lambda (e w ae empty-okay?)
    (syntax-case e ()
      ((_) empty-okay? '())
      ((_ e1 e2 ...) (syntax (e1 e2 ...)))
      (_ (syntax-error (source-wrap e w ae))))))

(define chi-lambda-clause
  (lambda (e c r mr w m?)
    (syntax-case c ()
      (((id ...) e1 e2 ...)
       (let ((ids (syntax (id ...))))
         (if (not (valid-bound-ids? ids))
             (syntax-error e "invalid parameter list in")
             (let ((labels (gen-labels ids))
                   (new-vars (map gen-var ids)))
               (values
                 new-vars
                 (chi-body (syntax (e1 e2 ...))
                           e
                           (extend-var-env* labels new-vars r)
                           mr
                           (make-binding-wrap ids labels w)
                           m?))))))
      ((ids e1 e2 ...)
       (let ((old-ids (lambda-var-list (syntax ids))))
         (if (not (valid-bound-ids? old-ids))
             (syntax-error e "invalid parameter list in")
             (let ((labels (gen-labels old-ids))
                   (new-vars (map gen-var old-ids)))
               (values
                 (let f ((ls1 (cdr new-vars)) (ls2 (car new-vars)))
                   (if (null? ls1)
                       ls2
                       (f (cdr ls1) (cons (car ls1) ls2))))
                 (chi-body (syntax (e1 e2 ...))
                           e
                           (extend-var-env* labels new-vars r)
                           mr
                           (make-binding-wrap old-ids labels w)
                           m?))))))
      (_ (syntax-error e)))))

(define chi-local-syntax
  (lambda (rec? e r mr w ae)
    (syntax-case e ()
      ((_ ((id val) ...) e1 e2 ...)
       (let ((ids (syntax (id ...))))
         (if (not (valid-bound-ids? ids))
             (invalid-ids-error (map (lambda (x) (wrap x w)) ids)
               (source-wrap e w ae)
               "keyword")
             (let ((labels (gen-labels ids)))
               (let ((new-w (make-binding-wrap ids labels w)))
                 (let ((b* (let ((w (if rec? new-w w)))
                             (map (lambda (x)
                                    (defer-or-eval-transformer
                                      local-eval-hook
                                      (chi x mr mr w #t)))
                                  (syntax (val ...))))))
                   (values
                     (syntax (e1 e2 ...))
                     (extend-env* labels b* r)
                     (extend-env* labels b* mr)
                     new-w
                     ae)))))))
      (_ (syntax-error (source-wrap e w ae))))))

(define chi-void
  (lambda ()
    (build-application no-source (build-primref no-source 'void) '())))

(define ellipsis?
  (lambda (x)
    (and (nonsymbol-id? x)
         (literal-id=? x (syntax (... ...))))))

;;; data

;;; strips all annotations from potentially circular reader output.

(define strip-annotation
  (lambda (x)
    (cond
      ((pair? x)
       (cons (strip-annotation (car x))
             (strip-annotation (cdr x))))
      ((annotation? x) (annotation-stripped x))
      (else x))))

;;; strips syntax-objects down to top-wrap; if top-wrap is layered directly
;;; on an annotation, strips the annotation as well.
;;; since only the head of a list is annotated by the reader, not each pair
;;; in the spine, we also check for pairs whose cars are annotated in case
;;; we've been passed the cdr of an annotated list

(define strip*
  (lambda (x w fn)
    (if (top-marked? w)
        (fn x)
        (let f ((x x))
          (cond
            ((syntax-object? x)
             (strip* (syntax-object-expression x) (syntax-object-wrap x) fn))
            ((pair? x)
             (let ((a (f (car x))) (d (f (cdr x))))
               (if (and (eq? a (car x)) (eq? d (cdr x)))
                   x
                   (cons a d))))
            ((vector? x)
             (let ((old (vector->list x)))
                (let ((new (map f old)))
                   (if (andmap eq? old new) x (list->vector new)))))
            (else x))))))

(define strip
  (lambda (x w)
    (strip* x w
      (lambda (x)
        (if (or (annotation? x) (and (pair? x) (annotation? (car x))))
            (strip-annotation x)
            x)))))

;;; lexical variables

(define gen-var
  (lambda (id)
    (let ((id (if (syntax-object? id) (syntax-object-expression id) id)))
      (if (annotation? id)
          (build-lexical-var id (annotation-expression id))
          (build-lexical-var id id)))))

(define lambda-var-list
  (lambda (vars)
    (let lvl ((vars vars) (ls '()) (w empty-wrap))
       (cond
         ((pair? vars) (lvl (cdr vars) (cons (wrap (car vars) w) ls) w))
         ((id? vars) (cons (wrap vars w) ls))
         ((null? vars) ls)
         ((syntax-object? vars)
          (lvl (syntax-object-expression vars)
               ls
               (join-wraps w (syntax-object-wrap vars))))
         ((annotation? vars)
          (lvl (annotation-expression vars) ls w))
       ; include anything else to be caught by subsequent error
       ; checking
         (else (cons vars ls))))))


; must precede global-extends

(set! $sc-put-cte
  (lambda (id b top-token)
     (define sc-put-module
       (lambda (exports token new-marks)
         (vfor-each
           (lambda (id) (store-import-binding id token new-marks))
           exports)))
     (define (put-cte id binding token)
       (let ((sym (if (symbol? id) id (id-var-name id empty-wrap))))
        (store-import-binding id token '())
         (put-global-definition-hook sym
          ; global binding is assumed; if global pass #f to remove existing binding, if any
           (if (and (eq? (binding-type binding) 'global)
                    (eq? (binding-value binding) sym))
               #f
               binding))))
     (let ((binding (make-transformer-binding b)))
       (case (binding-type binding)
         (($module)
          (let ((iface (binding-value binding)))
            (sc-put-module (interface-exports iface) (interface-token iface) '()))
          (put-cte id binding top-token))
         ((do-alias) (store-import-binding id top-token '()))
         ((do-import)
          ; fake binding: id is module id binding-value is token
          (let ((token (binding-value b)))
             (let ((b (lookup (id-var-name id empty-wrap) null-env)))
               (case (binding-type b)
                 (($module)
                  (let* ((iface (binding-value b))
                         (exports (interface-exports iface)))
                    (unless (eq? (interface-token iface) token)
                      (syntax-error id "import mismatch for module"))
                    (sc-put-module (interface-exports iface) top-token
                      (import-mark-delta id iface))))
                 (else (syntax-error id "unknown module"))))))
         (else (put-cte id binding top-token))))
     ))


;;; core transformers

;*** These special forms are handled by Gambit, so pass them through

(global-extend 'core '##c-define-type
   (lambda (e r mr w ae m?)
     (attach-source ae (strip e w))))

(global-extend 'core '##c-declare
   (lambda (e r mr w ae m?)
     (attach-source ae (strip e w))))

(global-extend 'core '##c-initialize
   (lambda (e r mr w ae m?)
     (attach-source ae (strip e w))))

(global-extend 'core '##c-lambda
   (lambda (e r mr w ae m?)
     (attach-source ae (strip e w))))

(global-extend 'core '##c-define
   (lambda (e r mr w ae m?)
     (attach-source ae (strip e w))))

(global-extend 'core '##define
   (lambda (e r mr w ae m?)
     (attach-source ae (strip e w))))

(global-extend 'core '##define-macro
   (lambda (e r mr w ae m?)
     (attach-source ae (strip e w))))

(global-extend 'core '##define-syntax
   (lambda (e r mr w ae m?)
     (attach-source ae (strip e w))))

(global-extend 'core '##include
   (lambda (e r mr w ae m?)
     (attach-source ae (strip e w))))

(global-extend 'core '##declare
   (lambda (e r mr w ae m?)
     (attach-source ae (strip e w))))

(global-extend 'core '##namespace
   (lambda (e r mr w ae m?)
     (attach-source ae (strip e w))))


(global-extend 'local-syntax 'letrec-syntax #t)
(global-extend 'local-syntax 'let-syntax #f)


(global-extend 'core 'fluid-let-syntax
  (lambda (e r mr w ae m?)
    (syntax-case e ()
      ((_ ((var val) ...) e1 e2 ...)
       (valid-bound-ids? (syntax (var ...)))
       (let ((names (map (lambda (x) (id-var-name x w)) (syntax (var ...)))))
         (for-each
           (lambda (id n)
             (case (binding-type (lookup n r))
               ((displaced-lexical) (displaced-lexical-error (wrap id w)))))
           (syntax (var ...))
           names)
         (let ((b* (map (lambda (x)
                          (defer-or-eval-transformer
                            local-eval-hook
                            (chi x mr mr w #t)))
                        (syntax (val ...)))))
           (chi-body
             (syntax (e1 e2 ...))
             (source-wrap e w ae)
             (extend-env* names b* r)
             (extend-env* names b* mr)
             w
             m?))))
      (_ (syntax-error (source-wrap e w ae))))))

(global-extend 'core 'quote
   (lambda (e r mr w ae m?)
      (syntax-case e ()
         ((_ e) (build-data ae (strip (syntax e) w)))
         (_ (syntax-error (source-wrap e w ae))))))

(global-extend 'core 'syntax
  (let ()
    (define gen-syntax
      (lambda (src e r maps ellipsis? vec?)
        (if (id? e)
            (let ((label (id-var-name e empty-wrap)))
              (let ((b (lookup label r)))
                (if (eq? (binding-type b) 'syntax)
                    (let-values (((var maps)
                                  (let ((var.lev (binding-value b)))
                                    (gen-ref src (car var.lev) (cdr var.lev) maps))))
                      (values `(ref ,var) maps))
                    (if (ellipsis? e)
                        (syntax-error src "misplaced ellipsis in syntax form")
                        (values `(quote ,e) maps)))))
            (syntax-case e ()
              ((dots e)
               (ellipsis? (syntax dots))
               (if vec?
                   (syntax-error src "misplaced ellipsis in syntax template")
                   (gen-syntax src (syntax e) r maps (lambda (x) #f) #f)))
              ((x dots . y)
               ; this could be about a dozen lines of code, except that we
               ; choose to handle (syntax (x ... ...)) forms
               (ellipsis? (syntax dots))
               (let f ((y (syntax y))
                       (k (lambda (maps)
                            (let-values (((x maps)
                                          (gen-syntax src (syntax x) r
                                            (cons '() maps) ellipsis? #f)))
                              (if (null? (car maps))
                                  (syntax-error src
                                    "extra ellipsis in syntax form")
                                  (values (gen-map x (car maps))
                                          (cdr maps)))))))
                 (syntax-case y ()
                   ((dots . y)
                    (ellipsis? (syntax dots))
                    (f (syntax y)
                       (lambda (maps)
                         (let-values (((x maps) (k (cons '() maps))))
                           (if (null? (car maps))
                               (syntax-error src
                                 "extra ellipsis in syntax form")
                               (values (gen-mappend x (car maps))
                                       (cdr maps)))))))
                   (_ (let-values (((y maps) (gen-syntax src y r maps ellipsis? vec?)))
                        (let-values (((x maps) (k maps)))
                          (values (gen-append x y) maps)))))))
              ((x . y)
               (let-values (((xnew maps) (gen-syntax src (syntax x) r maps ellipsis? #f)))
                 (let-values (((ynew maps) (gen-syntax src (syntax y) r maps ellipsis? vec?)))
                   (values (gen-cons e (syntax x) (syntax y) xnew ynew)
                           maps))))
              (#(x1 x2 ...)
               (let ((ls (syntax (x1 x2 ...))))
                 (let-values (((lsnew maps) (gen-syntax src ls r maps ellipsis? #t)))
                   (values (gen-vector e ls lsnew) maps))))
              (_ (values `(quote ,e) maps))))))

    (define gen-ref
      (lambda (src var level maps)
        (if (fx= level 0)
            (values var maps)
            (if (null? maps)
                (syntax-error src "missing ellipsis in syntax form")
                (let-values (((outer-var outer-maps) (gen-ref src var (fx- level 1) (cdr maps))))
                  (let ((b (assq outer-var (car maps))))
                    (if b
                        (values (cdr b) maps)
                        (let ((inner-var (gen-var 'tmp)))
                          (values inner-var
                                  (cons (cons (cons outer-var inner-var)
                                              (car maps))
                                        outer-maps))))))))))

    (define gen-append
      (lambda (x y)
        (if (equal? y '(quote ()))
            x
            `(append ,x ,y))))

    (define gen-mappend
      (lambda (e map-env)
        `(apply (primitive append) ,(gen-map e map-env))))

    (define gen-map
      (lambda (e map-env)
        (let ((formals (map cdr map-env))
              (actuals (map (lambda (x) `(ref ,(car x))) map-env)))
          (cond
            ((eq? (car e) 'ref)
             ; identity map equivalence:
             ; (map (lambda (x) x) y) == y
             (car actuals))
            ((andmap
                (lambda (x) (and (eq? (car x) 'ref) (memq (cadr x) formals)))
                (cdr e))
             ; eta map equivalence:
             ; (map (lambda (x ...) (f x ...)) y ...) == (map f y ...)
             `(map (primitive ,(car e))
                   ,@(map (let ((r (map cons formals actuals)))
                            (lambda (x) (cdr (assq (cadr x) r))))
                          (cdr e))))
            (else `(map (lambda ,formals ,e) ,@actuals))))))

   ; 12/12/00: semantic change: we now return original syntax object (e)
   ; if no pattern variables were found within, to avoid dropping
   ; source annotations prematurely.  the "syntax returns lists" for
   ; lists in its input guarantee counts only for substructure that
   ; contains pattern variables
    (define gen-cons
      (lambda (e x y xnew ynew)
        (case (car ynew)
          ((quote)
           (if (eq? (car xnew) 'quote)
               (let ((xnew (cadr xnew)) (ynew (cadr ynew)))
                 (if (and (eq? xnew x) (eq? ynew y))
                     `',e
                     `'(,xnew . ,ynew)))
               (if (eq? (cadr ynew) '()) `(list ,xnew) `(cons ,xnew ,ynew))))
          ((list) `(list ,xnew ,@(cdr ynew)))
          (else `(cons ,xnew ,ynew)))))

    (define gen-vector
      (lambda (e ls lsnew)
        (cond
          ((eq? (car lsnew) 'quote)
           (if (eq? (cadr lsnew) ls)
               `',e
               `(quote #(,@(cadr lsnew)))))
          ((eq? (car lsnew) 'list) `(vector ,@(cdr lsnew)))
          (else `(list->vector ,lsnew)))))


    (define regen
      (lambda (x)
        (case (car x)
          ((ref) (build-lexical-reference 'value no-source (cadr x)))
          ((primitive) (build-primref no-source (cadr x)))
          ((quote) (build-data no-source (cadr x)))
          ((lambda) (build-lambda no-source (cadr x) (regen (caddr x))))
          ((map) (let ((ls (map regen (cdr x))))
                   (build-application no-source
                     (if (fx= (length ls) 2)
                         (build-primref no-source 'map)
                        ; really need to do our own checking here
                         (build-primref no-source 2 'map)) ; require error check
                     ls)))
          (else (build-application no-source
                  (build-primref no-source (car x))
                  (map regen (cdr x)))))))

    (lambda (e r mr w ae m?)
      (let ((e (source-wrap e w ae)))
        (syntax-case e ()
          ((_ x)
           (let-values (((e maps) (gen-syntax e (syntax x) r '() ellipsis? #f)))
             (regen e)))
          (_ (syntax-error e)))))))


(global-extend 'core 'lambda
  (lambda (e r mr w ae m?)
    (syntax-case e ()
      ((_ . c)
       (let-values (((vars body) (chi-lambda-clause (source-wrap e w ae) (syntax c) r mr w m?)))
         (build-lambda ae vars body))))))


(global-extend 'core 'letrec
  (lambda (e r mr w ae m?)
    (syntax-case e ()
      ((_ ((id val) ...) e1 e2 ...)
       (let ((ids (syntax (id ...))))
         (if (not (valid-bound-ids? ids))
             (invalid-ids-error (map (lambda (x) (wrap x w)) ids)
               (source-wrap e w ae) "bound variable")
             (let ((labels (gen-labels ids))
                   (new-vars (map gen-var ids)))
               (let ((w (make-binding-wrap ids labels w))
                    (r (extend-var-env* labels new-vars r)))
                 (build-letrec ae
                   new-vars
                   (map (lambda (x) (chi x r mr w m?)) (syntax (val ...)))
                   (chi-body (syntax (e1 e2 ...)) (source-wrap e w ae) r mr w m?)))))))
      (_ (syntax-error (source-wrap e w ae))))))


(global-extend 'core 'if
   (lambda (e r mr w ae m?)
      (syntax-case e ()
         ((_ test then)
          (build-conditional ae
             (chi (syntax test) r mr w m?)
             (chi (syntax then) r mr w m?)
             (chi-void)))
         ((_ test then else)
          (build-conditional ae
             (chi (syntax test) r mr w m?)
             (chi (syntax then) r mr w m?)
             (chi (syntax else) r mr w m?)))
         (_ (syntax-error (source-wrap e w ae))))))



(global-extend 'set! 'set! '())

(global-extend 'alias 'alias '())
(global-extend 'begin 'begin '())

(global-extend '$module-key '$module '())
(global-extend '$import '$import '())

(global-extend 'define 'define '())

(global-extend 'define-syntax 'define-syntax '())

(global-extend 'eval-when 'eval-when '())

(global-extend 'meta 'meta '())

(global-extend 'core 'syntax-case
  (let ()
    (define convert-pattern
      ; accepts pattern & keys
      ; returns syntax-dispatch pattern & ids
      (lambda (pattern keys)
        (define cvt*
          (lambda (p* n ids)
            (if (null? p*)
                (values '() ids)
                (let-values (((y ids) (cvt* (cdr p*) n ids)))
                  (let-values (((x ids) (cvt (car p*) n ids)))
                    (values (cons x y) ids))))))
        (define cvt
          (lambda (p n ids)
            (if (id? p)
                (if (bound-id-member? p keys)
                    (values (vector 'free-id p) ids)
                    (values 'any (cons (cons p n) ids)))
                (syntax-case p ()
                  ((x dots)
                   (ellipsis? (syntax dots))
                   (let-values (((p ids) (cvt (syntax x) (fx+ n 1) ids)))
                     (values (if (eq? p 'any) 'each-any (vector 'each p))
                             ids)))
                  ((x dots y ... . z)
                   (ellipsis? (syntax dots))
                   (let-values (((z ids) (cvt (syntax z) n ids)))
                     (let-values (((y ids) (cvt* (syntax (y ...)) n ids)))
                       (let-values (((x ids) (cvt (syntax x) (fx+ n 1) ids)))
                         (values `#(each+ ,x ,(reverse y) ,z) ids)))))
                  ((x . y)
                   (let-values (((y ids) (cvt (syntax y) n ids)))
                     (let-values (((x ids) (cvt (syntax x) n ids)))
                       (values (cons x y) ids))))
                  (() (values '() ids))
                  (#(x ...)
                   (let-values (((p ids) (cvt (syntax (x ...)) n ids)))
                     (values (vector 'vector p) ids)))
                  (x (values (vector 'atom (strip p empty-wrap)) ids))))))
        (cvt pattern 0 '())))

    (define build-dispatch-call
      (lambda (pvars exp y r mr m?)
        (let ((ids (map car pvars)) (levels (map cdr pvars)))
          (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
            (build-application no-source
              (build-primref no-source 'apply)
              (list (build-lambda no-source new-vars
                      (chi exp
                         (extend-env*
                             labels
                             (map (lambda (var level)
                                    (make-binding 'syntax `(,var . ,level)))
                                  new-vars
                                  (map cdr pvars))
                             r)
                         mr
                         (make-binding-wrap ids labels empty-wrap)
                         m?))
                    y))))))

    (define gen-clause
      (lambda (x keys clauses r mr m? pat fender exp)
        (let-values (((p pvars) (convert-pattern pat keys)))
          (cond
            ((not (distinct-bound-ids? (map car pvars)))
             (invalid-ids-error (map car pvars) pat "pattern variable"))
            ((not (andmap (lambda (x) (not (ellipsis? (car x)))) pvars))
             (syntax-error pat
               "misplaced ellipsis in syntax-case pattern"))
            (else
             (let ((y (gen-var 'tmp)))
               ; fat finger binding and references to temp variable y
               (build-application no-source
                 (build-lambda no-source (list y)
                   (let-syntax ((y (identifier-syntax
                                     (build-lexical-reference 'value no-source y))))
                     (build-conditional no-source
                       (syntax-case fender ()
                         (#t y)
                         (_ (build-conditional no-source
                              y
                              (build-dispatch-call pvars fender y r mr m?)
                              (build-data no-source #f))))
                       (build-dispatch-call pvars exp y r mr m?)
                       (gen-syntax-case x keys clauses r mr m?))))
                 (list (if (eq? p 'any)
                           (build-application no-source
                             (build-primref no-source 'list)
                             (list (build-lexical-reference no-source 'value x)))
                           (build-application no-source
                             (build-primref no-source '$syntax-dispatch)
                             (list (build-lexical-reference no-source 'value x)
                                   (build-data no-source p))))))))))))

    (define gen-syntax-case
      (lambda (x keys clauses r mr m?)
        (if (null? clauses)
            (build-application no-source
              (build-primref no-source 'syntax-error)
              (list (build-lexical-reference 'value no-source x)))
            (syntax-case (car clauses) ()
              ((pat exp)
               (if (and (id? (syntax pat))
                        (not (bound-id-member? (syntax pat) keys))
                        (not (ellipsis? (syntax pat))))
                   (let ((label (gen-label))
                         (var (gen-var (syntax pat))))
                     (build-application no-source
                       (build-lambda no-source (list var)
                         (chi (syntax exp)
                              (extend-env label (make-binding 'syntax `(,var . 0)) r)
                              mr
                              (make-binding-wrap (syntax (pat))
                                (list label) empty-wrap)
                              m?))
                       (list (build-lexical-reference 'value no-source x))))
                   (gen-clause x keys (cdr clauses) r mr m?
                     (syntax pat) #t (syntax exp))))
              ((pat fender exp)
               (gen-clause x keys (cdr clauses) r mr m?
                 (syntax pat) (syntax fender) (syntax exp)))
              (_ (syntax-error (car clauses) "invalid syntax-case clause"))))))

    (lambda (e r mr w ae m?)
      (let ((e (source-wrap e w ae)))
        (syntax-case e ()
          ((_ val (key ...) m ...)
           (if (andmap (lambda (x) (and (id? x) (not (ellipsis? x))))
                       (syntax (key ...)))
               (let ((x (gen-var 'tmp)))
                 ; fat finger binding and references to temp variable x
                 (build-application ae
                   (build-lambda no-source (list x)
                     (gen-syntax-case x
                       (syntax (key ...)) (syntax (m ...))
                       r mr m?))
                   (list (chi (syntax val) r mr empty-wrap m?))))
               (syntax-error e "invalid literals list in"))))))))

(put-cte-hook 'module
  (lambda (x)
    (define proper-export?
      (lambda (e)
        (syntax-case e ()
          ((id e ...)
           (and (identifier? (syntax id))
                (andmap proper-export? (syntax (e ...)))))
          (id (identifier? (syntax id))))))
    (with-syntax ((orig x))
      (syntax-case x ()
        ((_ (e ...) d ...)
         (if (andmap proper-export? (syntax (e ...)))
             (syntax (begin ($module orig anon (e ...) d ...) ($import orig #f anon)))
             (syntax-error x "invalid exports list in")))
        ((_ m (e ...) d ...)
         (identifier? (syntax m))
         (if (andmap proper-export? (syntax (e ...)))
             (syntax ($module orig m (e ...) d ...))
             (syntax-error x "invalid exports list in")))))))

(let ()
  (define $module-exports
    (lambda (m r)
      (let ((b (r m)))
        (case (binding-type b)
          (($module)
           (let* ((interface (binding-value b))
                  (new-marks (import-mark-delta m interface)))
             (vmap (lambda (x)
                     (let ((id (if (pair? x) (car x) x)))
                       (make-syntax-object
                         (syntax-object->datum id)
                         (let ((marks (join-marks new-marks (wrap-marks (syntax-object-wrap id))))) 
                           (make-wrap marks
                                     ; the anti mark should always be present at the head
                                     ; of new-marks, but we paranoically check anyway
                                      (if (eq? (car marks) the-anti-mark)
                                          (cons 'shift (wrap-subst top-wrap))
                                          (wrap-subst top-wrap)))))))
                   (interface-exports interface))))
          ((displaced-lexical) (displaced-lexical-error m))
          (else (syntax-error m "unknown module"))))))
  (define $import-help
    (lambda (orig import-only?)
      (lambda (r)
        (define difference
          (lambda (ls1 ls2)
            (if (null? ls1)
                ls1
                (if (bound-id-member? (car ls1) ls2)
                    (difference (cdr ls1) ls2)
                    (cons (car ls1) (difference (cdr ls1) ls2))))))
        (define prefix-add
          (lambda (prefix-id)
            (let ((prefix (symbol->string (syntax-object->datum prefix-id))))
              (lambda (id)
                (datum->syntax-object id
                  (string->symbol
                    (string-append prefix
                      (symbol->string (syntax-object->datum id)))))))))
        (define prefix-drop
          (lambda (prefix-id)
            (let ((prefix (symbol->string (syntax-object->datum prefix-id))))
              (lambda (id)
                (let ((s (symbol->string (syntax-object->datum id))))
                  (let ((np (string-length prefix)) (ns (string-length s)))
                    (unless (and (>= ns np) (string=? (substring s 0 np) prefix))
                      (syntax-error id (string-append "missing expected prefix " prefix)))
                    (datum->syntax-object id
                      (string->symbol (substring s np ns)))))))))
        (define gen-mid
          (lambda (mid)
           ; introduced module ids must have same marks as original
           ; for import-only, since the barrier carries the marks of
           ; the module id
            (datum->syntax-object mid (generate-id (id-sym-name mid)))))
        (define (modspec m exports?)
          (with-syntax ((orig orig) (import-only? import-only?))
            (syntax-case m (only-for-syntax also-for-syntax
                            only except
                            add-prefix drop-prefix rename alias)
              ((only m id ...)
               (andmap identifier? (syntax (id ...)))
               (let-values (((mid d exports) (modspec (syntax m) #f)))
                 (with-syntax ((d d) (tmid (gen-mid mid)))
                   (values mid
                           (syntax (begin ($module orig tmid (id ...) d) ($import orig import-only? tmid)))
                           (and exports? (syntax (id ...)))))))
              ((except m id ...)
               (andmap identifier? (syntax (id ...)))
               (let-values (((mid d exports) (modspec (syntax m) #t)))
                 (with-syntax ((d d)
                               (tmid (gen-mid mid))
                               ((id ...) (difference exports (syntax (id ...)))))
                   (values mid
                           (syntax (begin ($module orig tmid (id ...) d) ($import orig import-only? tmid)))
                           (and exports? (syntax (id ...)))))))
              ((add-prefix m prefix-id)
               (identifier? (syntax prefix-id))
               (let-values (((mid d exports) (modspec (syntax m) #t)))
                 (with-syntax ((d d)
                               (tmid (gen-mid mid))
                               ((old-id ...) exports)
                               ((tmp ...) (generate-temporaries exports))
                               ((id ...) (map (prefix-add (syntax prefix-id)) exports)))
                   (values mid
                           (syntax (begin ($module orig tmid ((id tmp) ...)
                                            ($module orig tmid ((tmp old-id) ...) d (alias tmp old-id) ...)
                                            ($import orig import-only? tmid)
                                            (alias id tmp) ...)
                                          ($import orig import-only? tmid)))
                           (and exports? (syntax (id ...)))))))
              ((drop-prefix m prefix-id)
               (identifier? (syntax prefix-id))
               (let-values (((mid d exports) (modspec (syntax m) #t)))
                 (with-syntax ((d d)
                               (tmid (gen-mid mid))
                               ((old-id ...) exports)
                               ((tmp ...) (generate-temporaries exports))
                               ((id ...) (map (prefix-drop (syntax prefix-id)) exports)))
                   (values mid
                           (syntax (begin ($module orig tmid ((id tmp) ...)
                                            ($module orig tmid ((tmp old-id) ...) d (alias tmp old-id) ...)
                                            ($import orig import-only? tmid)
                                            (alias id tmp) ...)
                                          ($import orig import-only? tmid)))
                           (and exports? (syntax (id ...)))))))
              ((rename m (new-id old-id) ...)
               (and (andmap identifier? (syntax (new-id ...)))
                    (andmap identifier? (syntax (old-id ...))))
               (let-values (((mid d exports) (modspec (syntax m) #t)))
                 (with-syntax ((d d)
                               (tmid (gen-mid mid))
                               ((tmp ...) (generate-temporaries (syntax (old-id ...))))
                               ((other-id ...) (difference exports (syntax (old-id ...)))))
                   (values mid
                           (syntax (begin ($module orig tmid ((new-id tmp) ... other-id ...)
                                            ($module orig tmid (other-id ... (tmp old-id) ...) d (alias tmp old-id) ...)
                                            ($import orig import-only? tmid)
                                            (alias new-id tmp) ...)
                                          ($import orig import-only? tmid)))
                           (and exports? (syntax (new-id ... other-id ...)))))))
              ((alias m (new-id old-id) ...)
               (and (andmap identifier? (syntax (new-id ...)))
                    (andmap identifier? (syntax (old-id ...))))
               (let-values (((mid d exports) (modspec (syntax m) #t)))
                 (with-syntax ((d d)
                               (tmid (gen-mid mid))
                               ((other-id ...) exports))
                   (values mid
                           (syntax (begin ($module orig tmid ((new-id old-id) ... other-id ...) d (alias new-id old-id) ...)
                                          ($import orig import-only? tmid)))
                           (and exports? (syntax (new-id ... other-id ...)))))))
             ; base cases
              (mid
               (identifier? (syntax mid))
               (values (syntax mid)
                       (syntax ($import orig import-only? mid))
                       (and exports? ($module-exports (syntax mid) r))))
              ((mid)
               (identifier? (syntax mid))
               (values (syntax mid)
                       (syntax ($import orig import-only? mid))
                       (and exports? ($module-exports (syntax mid) r))))
            (_ (syntax-error m "invalid module specifier")))))
        (define modspec*
          (lambda (m)
            (let-values (((mid d exports) (modspec m #f))) d)))
        (syntax-case orig ()
          ((_ m ...)
           (with-syntax (((d ...) (map modspec* (syntax (m ...)))))
             (syntax (begin d ...))))))))

  (put-cte-hook 'import
    (lambda (orig)
      ($import-help orig #f)))
  
  (put-cte-hook 'import-only
    (lambda (orig)
      ($import-help orig #t)))
)

;;; To support eval-when, we maintain two mode sets:
;;;
;;; ctem (compile-time-expression mode)
;;;   determines whether/when to evaluate compile-time expressions such
;;;   as macro definitions, module definitions, and compile-time
;;;   registration of variable definitions
;;;
;;; rtem (run-time-expression mode)
;;;   determines whether/when to evaluate run-time expressions such
;;;   as the actual assignment performed by a variable definition or
;;;   arbitrary top-level expressions

;;; Possible modes in the mode set are:
;;;
;;; L (load): evaluate at load time.  implies V for compile-time
;;;     expressions and R for run-time expressions.
;;;
;;; C (compile): evaluate at compile (file) time
;;;
;;; E (eval): evaluate at evaluation (compile or interpret) time
;;;
;;; V (visit): evaluate at visit time
;;;
;;; R (revisit): evaluate at revisit time

;;; The mode set for the body of an eval-when is determined by
;;; translating each mode in the old mode set based on the situations
;;; present in the eval-when form and combining these into a set,
;;; using the following table.  See also update-mode-set.

;;;      load  compile  visit  revisit  eval
;;;
;;; L     L      C        V       R      -
;;;
;;; C     -      -        -       -      C
;;;
;;; V     V      C        V       -      -
;;;
;;; R     R      C        -       R      -
;;;
;;; E     -      -        -       -      E

;;; When we complete the expansion of a compile or run-time expression,
;;; the current ctem or rtem determines how the expression will be
;;; treated.  See ct-eval/residualize and rt-eval/residualize.

;;; Initial mode sets
;;;
;;; when compiling a file:
;;;
;;;     initial ctem: (L C)
;;;
;;;     initial rtem: (L)
;;;
;;; when not compiling a file:
;;;
;;;     initial ctem: (E)
;;;
;;;     initial rtem: (E)
;;;
;;;
;;; This means that top-level syntactic definitions are evaluated
;;; immediately after they are expanded, and the expanded definitions
;;; are also residualized into the object file if we are compiling
;;; a file.

(set! sc-expand
  (let ((ctem '(E)) (rtem '(E)))
    (lambda (x)
      (let ((env (interaction-environment)))
        (if (and (pair? x) (equal? (car x) noexpand))
            (cadr x)
            (chi-top* x null-env
              (env-wrap env)
              ctem rtem #f
              (env-top-ribcage env)))))))



(set! $make-environment
  (lambda (token mutable?)
    (let ((top-ribcage (make-top-ribcage token mutable?)))
      (make-env
        top-ribcage
        (make-wrap
          (wrap-marks top-wrap)
          (cons top-ribcage (wrap-subst top-wrap)))))))

(set! environment?
  (lambda (x)
    (env? x)))



(set! interaction-environment
  (let ((e ($make-environment '*top* #t)))
    (lambda () e)))

(set! identifier?
  (lambda (x)
    (nonsymbol-id? x)))

(set! datum->syntax-object
  (lambda (id datum)
    (arg-check nonsymbol-id? id 'datum->syntax-object)
    (make-syntax-object
      datum
      (syntax-object-wrap id))))

(set! syntax->list
  (lambda (orig-ls)
    (let f ((ls orig-ls))
      (syntax-case ls ()
        (() '())
        ((x . r) (cons #'x (f #'r)))
;***         (_ (error 'syntax->list "invalid argument ~s" orig-ls))))))
        (_ (error "(in syntax->list) invalid argument" orig-ls))))))

(set! syntax->vector
  (lambda (v)
    (syntax-case v ()
;***       (#(x ...) (apply vector (syntax->list #'(x ...))))
;***       (_ (error 'syntax->vector "invalid argument ~s" v)))))
      (#(x ...) (list->vector (syntax->list #'(x ...))))
      (_ (error "(in syntax->vector) invalid argument" v)))))

(set! syntax-object->datum
  ; accepts any object, since syntax objects may consist partially
  ; or entirely of unwrapped, nonsymbolic data
  (lambda (x)
    (strip x empty-wrap)))

(set! generate-temporaries
  (let ((n 0))
    (lambda (ls)
      (arg-check list? ls 'generate-temporaries)
      (map (lambda (x)
             (set! n (+ n 1))
             (wrap
              ; unique name to distinguish from other temporaries
               (string->symbol (string-append "t" (number->string n)))
              ; unique mark (in tmp-wrap) to distinguish from non-temporaries
               tmp-wrap))
           ls))))
  
(set! free-identifier=?
   (lambda (x y)
      (arg-check nonsymbol-id? x 'free-identifier=?)
      (arg-check nonsymbol-id? y 'free-identifier=?)
      (free-id=? x y)))

(set! bound-identifier=?
   (lambda (x y)
      (arg-check nonsymbol-id? x 'bound-identifier=?)
      (arg-check nonsymbol-id? y 'bound-identifier=?)
      (bound-id=? x y)))

(set! literal-identifier=?
  (lambda (x y)
    (arg-check nonsymbol-id? x 'literal-identifier=?)
    (arg-check nonsymbol-id? y 'literal-identifier=?)
    (literal-id=? x y)))

(set! syntax-error
  (lambda (object . messages)
    (for-each (lambda (x) (arg-check string? x 'syntax-error)) messages)
    (let ((message (if (null? messages)
                       "invalid syntax"
                       (apply string-append messages))))
;***       (error-hook #f message (strip object empty-wrap)))))
      (error message (strip object empty-wrap)))))

;;; syntax-dispatch expects an expression and a pattern.  If the expression
;;; matches the pattern a list of the matching expressions for each
;;; "any" is returned.  Otherwise, #f is returned.  (This use of #f will
;;; not work on r4rs implementations that violate the ieee requirement
;;; that #f and () be distinct.)

;;; The expression is matched with the pattern as follows:

;;; p in pattern:                        matches:
;;;   ()                                 empty list
;;;   any                                anything
;;;   (p1 . p2)                          pair (list)
;;;   #(free-id <key>)                   <key> with literal-identifier=?
;;;   each-any                           any proper list
;;;   #(each p)                          (p*)
;;;   #(each+ p1 (p2_1 ...p2_n) p3)      (p1* (p2_n ... p2_1) . p3)
;;;   #(vector p)                        (list->vector p)
;;;   #(atom <object>)                   <object> with "equal?"

;;; Vector cops out to pair under assumption that vectors are rare.  If
;;; not, should convert to:
;;;   #(vector p)                        #(p*)

(let ()

(define match-each
  (lambda (e p w)
    (cond
      ((annotation? e)
       (match-each (annotation-expression e) p w))
      ((pair? e)
       (let ((first (match (car e) p w '())))
         (and first
              (let ((rest (match-each (cdr e) p w)))
                 (and rest (cons first rest))))))
      ((null? e) '())
      ((syntax-object? e)
       (match-each (syntax-object-expression e)
                   p
                   (join-wraps w (syntax-object-wrap e))))
      (else #f))))

(define match-each+
  (lambda (e x-pat y-pat z-pat w r)
    (let f ((e e) (w w))
      (cond
        ((pair? e)
         (let-values (((xr* y-pat r) (f (cdr e) w)))
           (if r
               (if (null? y-pat)
                   (let ((xr (match (car e) x-pat w '())))
                     (if xr
                         (values (cons xr xr*) y-pat r)
                         (values #f #f #f)))
                   (values '() (cdr y-pat) (match (car e) (car y-pat) w r)))
               (values #f #f #f))))
        ((annotation? e) (f (annotation-expression e) w))
        ((syntax-object? e) (f (syntax-object-expression e)
                               (join-wraps w (syntax-object-wrap e))))
        (else (values '() y-pat (match e z-pat w r)))))))

(define match-each-any
  (lambda (e w)
    (cond
      ((annotation? e)
       (match-each-any (annotation-expression e) w))
      ((pair? e)
       (let ((l (match-each-any (cdr e) w)))
         (and l (cons (wrap (car e) w) l))))
      ((null? e) '())
      ((syntax-object? e)
       (match-each-any (syntax-object-expression e)
                       (join-wraps w (syntax-object-wrap e))))
      (else #f))))

(define match-empty
  (lambda (p r)
    (cond
      ((null? p) r)
      ((eq? p 'any) (cons '() r))
      ((pair? p) (match-empty (car p) (match-empty (cdr p) r)))
      ((eq? p 'each-any) (cons '() r))
      (else
       (case (vector-ref p 0)
         ((each) (match-empty (vector-ref p 1) r))
         ((each+) (match-empty (vector-ref p 1)
                    (match-empty (reverse (vector-ref p 2))
                      (match-empty (vector-ref p 3) r))))
         ((free-id atom) r)
         ((vector) (match-empty (vector-ref p 1) r)))))))

(define combine
  (lambda (r* r)
    (if (null? (car r*))
        r
        (cons (map car r*) (combine (map cdr r*) r)))))

(define match*
  (lambda (e p w r)
    (cond
      ((null? p) (and (null? e) r))
      ((pair? p)
       (and (pair? e) (match (car e) (car p) w
                        (match (cdr e) (cdr p) w r))))
      ((eq? p 'each-any)
       (let ((l (match-each-any e w))) (and l (cons l r))))
      (else
       (case (vector-ref p 0)
         ((each)
          (if (null? e)
              (match-empty (vector-ref p 1) r)
              (let ((r* (match-each e (vector-ref p 1) w)))
                (and r* (combine r* r)))))
         ((free-id) (and (id? e) (literal-id=? (wrap e w) (vector-ref p 1)) r))
         ((each+)
          (let-values (((xr* y-pat r)
                        (match-each+ e (vector-ref p 1) (vector-ref p 2)
                          (vector-ref p 3) w r)))
            (and r (null? y-pat)
              (if (null? xr*)
                  (match-empty (vector-ref p 1) r)
                  (combine xr* r)))))
         ((atom) (and (equal? (vector-ref p 1) (strip e w)) r))
         ((vector)
          (and (vector? e)
               (match (vector->list e) (vector-ref p 1) w r))))))))

(define match
  (lambda (e p w r)
    (cond
      ((not r) #f)
      ((eq? p 'any) (cons (wrap e w) r))
      ((syntax-object? e)
       (match*
         (unannotate (syntax-object-expression e))
         p
         (join-wraps w (syntax-object-wrap e))
         r))
      (else (match* (unannotate e) p w r)))))

(set! $syntax-dispatch
  (lambda (e p)
    (cond
      ((eq? p 'any) (list e))
      ((syntax-object? e)
       (match* (unannotate (syntax-object-expression e))
         p (syntax-object-wrap e) '()))
      (else (match* (unannotate e) p empty-wrap '())))))
))


(define-syntax with-syntax
   (lambda (x)
      (syntax-case x ()
         ((_ () e1 e2 ...)
          (syntax (begin e1 e2 ...)))
         ((_ ((out in)) e1 e2 ...)
          (syntax (syntax-case in () (out (begin e1 e2 ...)))))
         ((_ ((out in) ...) e1 e2 ...)
          (syntax (syntax-case (list in ...) ()
                     ((out ...) (begin e1 e2 ...))))))))

(define-syntax with-implicit
  (syntax-rules ()
    ((_ (tid id ...) e1 e2 ...)
     (andmap identifier? (syntax (tid id ...)))
     (begin
       (unless (identifier? (syntax tid))
         (syntax-error (syntax tid) "non-identifier with-implicit template"))
       (with-syntax ((id (datum->syntax-object (syntax tid) 'id)) ...)
         e1 e2 ...)))))

(define-syntax datum
  (syntax-rules ()
    ((_ x) (syntax-object->datum (syntax x)))))

(define-syntax syntax-rules
  (lambda (x)
    (define clause
      (lambda (y)
        (syntax-case y ()
          (((keyword . pattern) template)
           (syntax ((dummy . pattern) (syntax template))))
          (((keyword . pattern) fender template)
           (syntax ((dummy . pattern) fender (syntax template))))
          (_ (syntax-error x)))))
    (syntax-case x ()
      ((_ (k ...) cl ...)
       (andmap identifier? (syntax (k ...)))
       (with-syntax (((cl ...) (map clause (syntax (cl ...)))))
         (syntax (lambda (x) (syntax-case x (k ...) cl ...))))))))

(define-syntax or
   (lambda (x)
      (syntax-case x ()
         ((_) (syntax #f))
         ((_ e) (syntax e))
         ((_ e1 e2 e3 ...)
          (syntax (let ((t e1)) (if t t (or e2 e3 ...))))))))

(define-syntax and
   (lambda (x)
      (syntax-case x ()
         ((_ e1 e2 e3 ...) (syntax (if e1 (and e2 e3 ...) #f)))
         ((_ e) (syntax e))
         ((_) (syntax #t)))))

(define-syntax let
   (lambda (x)
      (syntax-case x ()
         ((_ ((x v) ...) e1 e2 ...)
          (andmap identifier? (syntax (x ...)))
          (syntax ((lambda (x ...) e1 e2 ...) v ...)))
         ((_ f ((x v) ...) e1 e2 ...)
          (andmap identifier? (syntax (f x ...)))
          (syntax ((letrec ((f (lambda (x ...) e1 e2 ...))) f)
                    v ...))))))

(define-syntax let*
  (lambda (x)
    (syntax-case x ()
      ((let* ((x v) ...) e1 e2 ...)
       (andmap identifier? (syntax (x ...)))
       (let f ((bindings (syntax ((x v)  ...))))
         (if (null? bindings)
             (syntax (let () e1 e2 ...))
             (with-syntax ((body (f (cdr bindings)))
                           (binding (car bindings)))
               (syntax (let (binding) body)))))))))

(define-syntax cond
  (lambda (x)
    (syntax-case x ()
      ((_ m1 m2 ...)
       (let f ((clause (syntax m1)) (clauses (syntax (m2 ...))))
         (if (null? clauses)
             (syntax-case clause (else =>)
               ((else e1 e2 ...) (syntax (begin e1 e2 ...)))
               ((e0) (syntax (let ((t e0)) (if t t))))
               ((e0 => e1) (syntax (let ((t e0)) (if t (e1 t)))))
               ((e0 e1 e2 ...) (syntax (if e0 (begin e1 e2 ...))))
               (_ (syntax-error x)))
             (with-syntax ((rest (f (car clauses) (cdr clauses))))
               (syntax-case clause (else =>)
                 ((e0) (syntax (let ((t e0)) (if t t rest))))
                 ((e0 => e1) (syntax (let ((t e0)) (if t (e1 t) rest))))
                 ((e0 e1 e2 ...) (syntax (if e0 (begin e1 e2 ...) rest)))
                 (_ (syntax-error x))))))))))

(define-syntax do
   (lambda (orig-x)
      (syntax-case orig-x ()
         ((_ ((var init . step) ...) (e0 e1 ...) c ...)
          (with-syntax (((step ...)
                         (map (lambda (v s)
                                 (syntax-case s ()
                                    (() v)
                                    ((e) (syntax e))
                                    (_ (syntax-error orig-x))))
                              (syntax (var ...))
                              (syntax (step ...)))))
             (syntax-case (syntax (e1 ...)) ()
                (() (syntax (let do ((var init) ...)
                               (if (not e0)
                                   (begin c ... (do step ...))))))
                ((e1 e2 ...)
                 (syntax (let do ((var init) ...)
                            (if e0
                                (begin e1 e2 ...)
                                (begin c ... (do step ...))))))))))))

(define-syntax quasiquote
  (let ()
    (define (quasi p lev)
      (syntax-case p (unquote quasiquote)
        ((unquote p)
         (if (= lev 0)
             #'("value" p)
             (quasicons #'("quote" unquote) (quasi #'(p) (- lev 1)))))
        ((quasiquote p) (quasicons #'("quote" quasiquote) (quasi #'(p) (+ lev 1))))
        ((p . q)
         (syntax-case #'p (unquote unquote-splicing)
           ((unquote p ...)
            (if (= lev 0)
                (quasilist* #'(("value" p) ...) (quasi #'q lev))
                (quasicons
                  (quasicons #'("quote" unquote) (quasi #'(p ...) (- lev 1)))
                  (quasi #'q lev))))
           ((unquote-splicing p ...)
            (if (= lev 0)
                (quasiappend #'(("value" p) ...) (quasi #'q lev))
                (quasicons
                  (quasicons #'("quote" unquote-splicing) (quasi #'(p ...) (- lev 1)))
                  (quasi #'q lev))))
           (_ (quasicons (quasi #'p lev) (quasi #'q lev)))))
        (#(x ...) (quasivector (vquasi #'(x ...) lev)))
        (p #'("quote" p))))
    (define (vquasi p lev)
      (syntax-case p ()
        ((p . q)
         (syntax-case #'p (unquote unquote-splicing)
           ((unquote p ...)
            (if (= lev 0)
                (quasilist* #'(("value" p) ...) (vquasi #'q lev))
                (quasicons
                  (quasicons #'("quote" unquote) (quasi #'(p ...) (- lev 1)))
                  (vquasi #'q lev))))
           ((unquote-splicing p ...)
            (if (= lev 0)
                (quasiappend #'(("value" p) ...) (vquasi #'q lev))
                (quasicons
                  (quasicons
                    #'("quote" unquote-splicing)
                    (quasi #'(p ...) (- lev 1)))
                  (vquasi #'q lev))))
           (_ (quasicons (quasi #'p lev) (vquasi #'q lev)))))
        (() #'("quote" ()))))
    (define (quasicons x y)
      (with-syntax ((x x) (y y))
        (syntax-case #'y ()
          (("quote" dy)
           (syntax-case #'x ()
             (("quote" dx) #'("quote" (dx . dy)))
             (_ (if (null? #'dy) #'("list" x) #'("list*" x y)))))
          (("list" . stuff) #'("list" x . stuff))
          (("list*" . stuff) #'("list*" x . stuff))
          (_ #'("list*" x y)))))
    (define (quasiappend x y)
      (syntax-case y ()
        (("quote" ())
         (cond
           ((null? x) #'("quote" ()))
           ((null? (cdr x)) (car x))
           (else (with-syntax (((p ...) x)) #'("append" p ...)))))
        (_
         (cond
           ((null? x) y)
           (else (with-syntax (((p ...) x) (y y)) #'("append" p ... y)))))))
    (define (quasilist* x y)
      (let f ((x x))
        (if (null? x)
            y
            (quasicons (car x) (f (cdr x))))))
    (define (quasivector x)
      (syntax-case x ()
        (("quote" (x ...)) #'("quote" #(x ...)))
        (_
         (let f ((y x) (k (lambda (ls) #`("vector" #,@ls))))
           (syntax-case y ()
             (("quote" (y ...)) (k #'(("quote" y) ...)))
             (("list" y ...) (k #'(y ...)))
             (("list*" y ... z) (f #'z (lambda (ls) (k (append #'(y ...) ls)))))
             (else #`("list->vector" #,x)))))))
    (define (emit x)
      (syntax-case x ()
        (("quote" x) #''x)
        (("list" x ...) #`(list #,@(map emit #'(x ...))))
      ; could emit list* for 3+ arguments if implementation supports list*
       (("list*" x ... y)
        (let f ((x* #'(x ...)))
          (if (null? x*)
              (emit #'y)
              #`(cons #,(emit (car x*)) #,(f (cdr x*))))))
        (("append" x ...) #`(append #,@(map emit #'(x ...))))
        (("vector" x ...) #`(vector #,@(map emit #'(x ...))))
        (("list->vector" x) #`(list->vector #,(emit #'x)))
        (("value" x) #'x)))
    (lambda (x)
      (syntax-case x ()
       ; convert to intermediate language, combining introduced (but not
       ; unquoted source) quote expressions where possible and choosing
       ; optimal construction code otherwise, then emit Scheme code
       ; corresponding to the intermediate language forms.
        ((_ e) (emit (quasi #'e 0)))))))

;*** "unquote" and "unquote-splicing" might be variables and defining
;*** them as macros would interfere.
;***
;*** (define-syntax unquote
;***   (lambda (x)
;***     (syntax-error x "misplaced")))
;***
;*** (define-syntax unquote-splicing
;***   (lambda (x)
;***     (syntax-error x "misplaced")))

(define-syntax quasisyntax
  (lambda (x)
    (define (qs q n b* k)
      (syntax-case q (quasisyntax unsyntax unsyntax-splicing)
        ((quasisyntax . d)
         (qs #'d (+ n 1) b*
           (lambda (b* dnew)
             (k b*
                (if (eq? dnew #'d)
                    q
                    (with-syntax ((d dnew)) #'(quasisyntax . d)))))))
        ((unsyntax . d)
         (not (= n 0))
         (qs #'d (- n 1) b*
           (lambda (b* dnew)
             (k b*
                (if (eq? dnew #'d)
                    q
                    (with-syntax ((d dnew)) #'(unsyntax . d)))))))
        ((unsyntax-splicing . d)
         (not (= n 0))
         (qs #'d (- n 1) b*
           (lambda (b* dnew)
             (k b*
                (if (eq? dnew #'d)
                    q
                    (with-syntax ((d dnew)) #'(unsyntax-splicing . d)))))))
        ((unsyntax q)
         (= n 0)
         (with-syntax (((t) (generate-temporaries #'(q))))
           (k (cons #'(t q) b*) #'t)))
        (((unsyntax q ...) . d)
         (= n 0)
         (qs #'d n b*
           (lambda (b* dnew)
             (with-syntax (((t ...) (generate-temporaries #'(q ...))))
               (k (append #'((t q) ...) b*)
                  (with-syntax ((d dnew)) #'(t ... . d)))))))
        (((unsyntax-splicing q ...) . d)
         (= n 0)
         (qs #'d n b*
           (lambda (b* dnew)
             (with-syntax (((t ...) (generate-temporaries #'(q ...))))
               (k (append #'(((t (... ...)) q) ...) b*)
                  (with-syntax ((((m ...) ...) #'((t (... ...)) ...)))
                    (with-syntax ((d dnew)) #'(m ... ... . d))))))))
        ((a . d)
         (qs #'a n b*
           (lambda (b* anew)
             (qs #'d n b*
               (lambda (b* dnew)
                 (k b*
                    (if (and (eq? anew #'a) (eq? dnew #'d))
                        q
                        (with-syntax ((a anew) (d dnew)) #'(a . d)))))))))
        (#(x ...)
         (vqs #'(x ...) n b*
           (lambda (b* xnew*)
             (k b*
                (if (let same? ((x* #'(x ...)) (xnew* xnew*))
                      (if (null? x*)
                          (null? xnew*)
                          (and (not (null? xnew*))
                               (eq? (car x*) (car xnew*))
                               (same? (cdr x*) (cdr xnew*)))))
                    q
                    (with-syntax (((x ...) xnew*)) #'#(x ...)))))))
        (_ (k b* q))))
    (define (vqs x* n b* k)
      (if (null? x*)
          (k b* '())
          (vqs (cdr x*) n b*
            (lambda (b* xnew*)
              (syntax-case (car x*) (unsyntax unsyntax-splicing)
                ((unsyntax q ...)
                 (= n 0)
                 (with-syntax (((t ...) (generate-temporaries #'(q ...))))
                   (k (append #'((t q) ...) b*)
                      (append #'(t ...) xnew*))))
                ((unsyntax-splicing q ...)
                 (= n 0)
                 (with-syntax (((t ...) (generate-temporaries #'(q ...))))
                   (k (append #'(((t (... ...)) q) ...) b*)
                      (with-syntax ((((m ...) ...) #'((t (... ...)) ...)))
                        (append #'(m ... ...) xnew*)))))
                (_ (qs (car x*) n b*
                     (lambda (b* xnew)
                       (k b* (cons xnew xnew*))))))))))
    (syntax-case x ()
      ((_ x)
       (qs #'x 0 '()
         (lambda (b* xnew)
           (if (eq? xnew #'x)
               #'(syntax x)
               (with-syntax (((b ...) b*) (x xnew))
                 #'(with-syntax (b ...) (syntax x))))))))))

;*** "unsyntax" and "unsyntax-splicing" might be variables and defining
;*** them as macros would interfere.
;***
;*** (define-syntax unsyntax
;***   (lambda (x)
;***     (syntax-error x "misplaced")))
;***
;*** (define-syntax unsyntax-splicing
;***   (lambda (x)
;***     (syntax-error x "misplaced")))

(define-syntax include
  (lambda (x)
    (define read-file
      (lambda (fn k)
        (let ((p (open-input-file fn)))
          (let f ()
            (let ((x (read p)))
              (if (eof-object? x)
                  (begin (close-input-port p) '())
                  (cons (datum->syntax-object k x) (f))))))))
    (syntax-case x ()
      ((k filename)
       (let ((fn (syntax-object->datum (syntax filename))))
;***          (with-syntax (((exp ...) (read-file fn (syntax k))))
;***            (syntax (begin exp ...))))))))
         (datum->syntax-object
          (syntax k)
          (let* ((src
                  (##include-file-as-a-begin-expr
                   (let ((y (vector-ref x 1)))
                     (if (##source? y)
                         y
                         (##make-source y #f)))))
                 (locat
                  (##source-locat src)))
            src
            #;
            (##make-source (cons (##make-source 'begin locat)
                                 (cdr (##source-code src)))
                           locat))))))))

(define-syntax case
  (lambda (x)
    (syntax-case x ()
      ((_ e m1 m2 ...)
       (with-syntax
         ((body (let f ((clause (syntax m1)) (clauses (syntax (m2 ...))))
                  (if (null? clauses)
                      (syntax-case clause (else)
                        ((else e1 e2 ...) (syntax (begin e1 e2 ...)))
                        (((k ...) e1 e2 ...)
                         (syntax (if (memv t '(k ...)) (begin e1 e2 ...))))
                        (_ (syntax-error x)))
                      (with-syntax ((rest (f (car clauses) (cdr clauses))))
                        (syntax-case clause (else)
                          (((k ...) e1 e2 ...)
                           (syntax (if (memv t '(k ...))
                                       (begin e1 e2 ...)
                                       rest)))
                          (_ (syntax-error x))))))))
         (syntax (let ((t e)) body)))))))

(define-syntax identifier-syntax
  (syntax-rules (set!)
    ((_ e)
     (lambda (x)
       (syntax-case x ()
         (id (identifier? (syntax id)) (syntax e))
         ((_ x (... ...)) (syntax (e x (... ...)))))))
    ((_ (id exp1) ((set! var val) exp2))
     (and (identifier? (syntax id)) (identifier? (syntax var)))
     (cons 'macro!
       (lambda (x)
         (syntax-case x (set!)
           ((set! var val) (syntax exp2))
           ((id x (... ...)) (syntax (exp1 x (... ...))))
           (id (identifier? (syntax id)) (syntax exp1))))))))

;*** Gambit extensions:

(define-syntax cond-expand
  (syntax-rules (and or not else srfi-0 gambit)
    ((cond-expand) (syntax-error "Unfulfilled cond-expand"))
    ((cond-expand (else body ...))
     (begin body ...))
    ((cond-expand ((and) body ...) more-clauses ...)
     (begin body ...))
    ((cond-expand ((and req1 req2 ...) body ...) more-clauses ...)
     (cond-expand
       (req1
         (cond-expand
           ((and req2 ...) body ...)
           more-clauses ...))
       more-clauses ...))
    ((cond-expand ((or) body ...) more-clauses ...)
     (cond-expand more-clauses ...))
    ((cond-expand ((or req1 req2 ...) body ...) more-clauses ...)
     (cond-expand
       (req1
        (begin body ...))
       (else
        (cond-expand
           ((or req2 ...) body ...)
           more-clauses ...))))
    ((cond-expand ((not req) body ...) more-clauses ...)
     (cond-expand
       (req
         (cond-expand more-clauses ...))
       (else body ...)))
    ((cond-expand (srfi-0 body ...) more-clauses ...)
       (begin body ...))
    ((cond-expand (gambit body ...) more-clauses ...)
       (begin body ...))
    ((cond-expand (feature-id body ...) more-clauses ...)
       (cond-expand more-clauses ...))))

(define-syntax define-macro
  (lambda (x)
    (syntax-case x ()
      ((_ (name . params) body1 body2 ...)
       (syntax (define-macro name (lambda params body1 body2 ...))))
      ((_ name expander)
       (syntax (define-syntax name
                 (lambda (y)
                   (syntax-case y ()
                     ((k . args)
                      (let ((lst (syntax-object->datum (syntax args))))
                        (datum->syntax-object
                         (syntax k)
                         (apply expander lst))))))))))))

(define-syntax ##begin
  (lambda (x)
    (syntax-case x ()
      ((_ body1 ...)
       (syntax (begin body1 ...))))))