File: ucparse.yy

package info (click to toggle)
exult 1.12.1-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 43,856 kB
  • sloc: cpp: 170,016; xml: 7,400; yacc: 2,850; makefile: 2,419; java: 1,901; ansic: 1,654; lex: 673; sh: 550; objc: 416
file content (3131 lines) | stat: -rw-r--r-- 73,387 bytes parent folder | download
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
%{
/**
 **	Ucparse.y - Usecode parser.
 **
 **	Written: 12/30/2000 - JSF
 **/

/*
Copyright (C) 2000-2022 The Exult Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#ifdef HAVE_CONFIG_H
#	include <config.h>
#endif

#include "opcodes.h"
#include "ucclass.h"
#include "ucexpr.h"
#include "ucfun.h"
#include "ucscriptop.h"
#include "ucstmt.h"

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using std::string;
using std::vector;

void                        yyerror(const char*);
void                        yywarning(const char*);
extern int                  yylex();
extern void                 start_script();
extern void                 end_script();
extern void                 start_converse();
extern void                 end_converse();
extern void                 start_loop();
extern void                 end_loop();
extern void                 start_breakable();
extern void                 end_breakable();
extern void                 start_fun_id();
extern void                 end_fun_id();
extern bool                 can_break();
extern bool                 can_continue();
static Uc_var_symbol*       Get_variable(const char*);
static Uc_array_expression* Create_array(int, Uc_expression*);
static Uc_array_expression* Create_array(int, Uc_expression*, Uc_expression*);
static Uc_class*            Find_class(const char* nm);
static bool                 Class_unexpected_error(Uc_expression* expr);
static bool                 Nonclass_unexpected_error(Uc_expression* expr);
static bool Incompatible_classes_error(Uc_class* src, Uc_class* trg);
static Uc_call_expression* cls_method_call(
		Uc_expression* ths, Uc_class* curcls, Uc_class* clsscope, char* nm,
		Uc_array_expression* parms);
static Uc_call_expression* cls_function_call(
		Uc_expression* ths, Uc_class* curcls, char* nm, bool original,
		Uc_array_expression* parms);

std::vector<Uc_design_unit*> units;    // THIS is what we produce.

static Uc_function* cur_fun = nullptr;    // Current function being parsed.
static Uc_class* cur_class  = nullptr;    // ...or, current class being parsed.
static Uc_struct_symbol* cur_struct
		= nullptr;                // ...or, current struct being parsed.
static int  enum_val  = -1;       // Keeps track of enum elements.
static bool is_extern = false;    // Marks a function symbol as being an extern
static Uc_class* class_type = nullptr;    // For declaration of class variables.
static Uc_struct_symbol* struct_type
		= nullptr;    // For declaration of struct variables.
static bool                    has_ret        = false;
static int                     repeat_nesting = 0;
static std::vector<UsecodeOps> const_opcode;
static std::vector<bool>       nested_if;
static int converse = 0;    // If we are in a converse block.

struct Fun_id_info {
	Uc_function_symbol::Function_kind kind;
	int                               id;

	Fun_id_info(Uc_function_symbol::Function_kind k, int i) : kind(k), id(i) {}
};

struct Member_selector {
	Uc_expression* expr;
	char*          name;

	Member_selector(Uc_expression* e, char* n) : expr(e), name(n) {}
};

struct Loop_Init {
	const char*    var;
	Uc_var_symbol* array;

	Loop_Init(const char* v, Uc_var_symbol* a) : var(v), array(a) {}
};

struct Loop_Vars {
	Uc_var_symbol* var;
	Uc_var_symbol* array;
	Uc_var_symbol* index;
	Uc_var_symbol* length;

	Loop_Vars(
			Uc_var_symbol* v, Uc_var_symbol* a, Uc_var_symbol* i = nullptr,
			Uc_var_symbol* l = nullptr)
			: var(v), array(a), index(i), length(l) {}
};

#ifdef __GNUC__
#	pragma GCC diagnostic push
#	pragma GCC diagnostic ignored "-Wold-style-cast"
#	pragma GCC diagnostic ignored "-Wcast-qual"
#	if !defined(__llvm__) && !defined(__clang__)
#		pragma GCC diagnostic ignored "-Wredundant-tags"
#	endif
#endif    // __GNUC__
%}

%define parse.lac full
%define parse.error detailed

%union {
	class Uc_symbol *sym;
	class Uc_var_symbol *var;
	class Uc_class *cls;
	class Uc_struct_symbol *struc;
	class Uc_expression *expr;
	class Uc_call_expression *funcall;
	class Uc_function_symbol *funsym;
	class Uc_statement *stmt;
	class std::vector<Uc_var_symbol *> *varvec;
	class Uc_block_statement *block;
	class Uc_array_expression *exprlist;
	class Uc_label_statement *label;
	class std::vector<int> *intlist;
	class std::vector<Uc_statement *> *stmtlist;
	struct Fun_id_info *funid;
	struct Member_selector *membersel;
	struct Loop_Init *loopinit;
	struct Loop_Vars *loopvars;
	int intval;
	char *strval;
}

/*
 *	Keywords:
 */
%token IF "'if'"
%token ELSE "'else'"
%token RETURN "'return'"
%token DO "'do'"
%token WHILE "'while'"
%token FOR "'for'"
%token WITH "'with'"
%token TO "'to'"
%token EXTERN "'extern'"
%token DECLARE_ "'declare'"
%token BREAK "'break'"
%token GOTO "'goto'"
%token CASE "'case'"
%token VAR "'var'"
%token VOID "'void'"
%token ALIAS "'alias'"
%token STRUCT "'struct'"
%token UCC_CHAR "'char'"
%token UCC_BYTE "'byte'"
%token UCC_INT "'int'"
%token UCC_LONG "'long'"
%token UCC_CONST "'const'"
%token STRING "'string'"
%token ENUM "'enum'"
%token CONVERSE "'converse'"
%token NESTED "'nested'"
%token SAY "'say'"
%token MESSAGE "'message'"
%token RESPONSE "'response'"
%token EVENT "'event'"
%token FLAG "'gflags'"
%token ITEM "'item'"
%token UCTRUE "'true'"
%token UCFALSE "'false'"
%token REMOVE "'remove'"
%token ADD "'add'"
%token HIDE "'hide'"
%token SCRIPT "'script'"
%token AFTER "'after'"
%token TICKS "'ticks'"
%token STATIC_ "'static'"
%token ORIGINAL "'original'"
%token SHAPENUM "'shape#'"
%token OBJECTNUM "'object#'"
%token IDNUM "'id#'"
%token CLASS "'class'"
%token RUNSCRIPT "'runscript'"
%token SWITCH "'switch'"
%token DEFAULT "'default'"
%token FALLTHROUGH "'fallthrough'"
%token ALWAYS "'always'"
%token CHOICE "'user_choice'"
%token TRY "'try'"
%token CATCH "'catch'"
%token ABORT "'abort'"
%token THROW "'throw'"
%token ATTEND "'attend'"
%token ENDCONV "'endconv'"

/*
 *	Script keywords:
 */
					/* Script commands. */
%token NOBREAK "'nobreak'"
%token CONTINUE "'continue'"
%token REPEAT "'repeat'"
%token NOP "'nop'"
%token NOHALT "'nohalt'"
%token WAIT "'wait'"
%token RISE "'rise'"
%token DESCEND "'descend'"
%token FRAME "'frame'"
%token HATCH "'hatch'"
%token NEXT "'next'"
%token PREVIOUS "'previous'"
%token CYCLE "'cycle'"
%token STEP "'step'"
%token MUSIC "'music'"
%token CALL "'call'"
%token SPEECH "'speech'"
%token SFX "'sfx'"
%token FACE "'face'"
%token HIT "'hit'"
%token HOURS "'hours'"
%token ACTOR "'actor'"
%token ATTACK "'attack'"
%token FINISH "'finish'"
%token RESURRECT "'resurrect'"
%token SETEGG "'setegg'"
%token MINUTES "'minutes'"
%token RESET "'reset'"
%token WEATHER "'weather'"
%token NEAR "'near'"
%token FAR "'far'"
%token NORTH "'north'"
%token SOUTH "'south'"
%token EAST "'east'"
%token WEST "'west'"
%token NE "'ne'"
%token NW "'nw'"
%token SE "'se'"
%token SW "'sw'"
%token NOP2 "'nop2'"
%token RAW "'raw'"
%token STANDING "'standing'"
%token STEP_RIGHT "'step_right'"
%token STEP_LEFT "'step_left'"
%token READY "'ready'"
%token RAISE_1H "'raise_1h'"
%token REACH_1H "'reach_1h'"
%token STRIKE_1H "'strike_1h'"
%token RAISE_2H "'raise_2h'"
%token REACH_2H "'reach_2h'"
%token STRIKE_2H "'strike_2h'"
%token SITTING "'sitting'"
%token BOWING "'bowing'"
%token KNEELING "'kneeling'"
%token SLEEPING "'sleeping'"
%token CAST_UP "'cast_up'"
%token CAST_OUT "'cast_out'"
%token CACHED_IN "'cached_in'"
%token PARTY_NEAR "'party_near'"
%token AVATAR_NEAR "'avatar_near'"
%token AVATAR_FAR "'avatar_far'"
%token AVATAR_FOOTPAD "'avatar_footpad'"
%token PARTY_FOOTPAD "'party_footpad'"
%token SOMETHING_ON "'something_on'"
%token EXTERNAL_CRITERIA "'external_criteria'"
%token NORMAL_DAMAGE "'normal_damage'"
%token FIRE_DAMAGE "'fire_damage'"
%token MAGIC_DAMAGE "'magic_damage'"
%token LIGHTNING_DAMAGE "'lightning_damage'"
%token POISON_DAMAGE "'poison_damage'"
%token STARVATION_DAMAGE "'starvation_damage'"
%token FREEZING_DAMAGE "'freezing_damage'"
%token ETHEREAL_DAMAGE "'ethereal_damage'"
%token SONIC_DAMAGE "'sonic_damage'"
%token FOREVER "'forever'"
%token BREAKABLE "'breakable'"

/*
 *	Operators
 */
%token UCC_INSERT "'<<'"
%token ADD_EQ "'+='"
%token SUB_EQ "'-='"
%token MUL_EQ "'*='"
%token DIV_EQ "'/='"
%token MOD_EQ "'%='"
%token AND_EQ "'&='"
%token AND "'&&'"
%token OR "'||'"
%token EQUALS "'=='"
%token NEQUALS "'!='"
%token LTEQUALS "'<='"
%token GTEQUALS "'>='"
%token UCC_IN "'in'"
%token NOT "'!'"
%token ADDRESSOF "'&'"
%token UMINUS "'-'"
%token UPLUS "'+'"
%token NEW "'new'"
%token DELETE "'delete'"
%token UCC_POINTS "'->'"
%token UCC_SCOPE "'::'"

/*
 *	Other tokens:
 */
%token <strval> STRING_LITERAL "string literal"
%token <strval> STRING_PREFIX "string prefix"
%token <strval> IDENTIFIER "identifier"
%token <intval> INT_LITERAL "integer"

/*
 *	Handle if-then-else conflict.
 */
%precedence IF
%precedence ELSE

/*
 *	Handle <loop>-nobreak conflict.
 */

%precedence LOOP
%precedence NOBREAK

/*
 *	Expression precedence rules (lowest to highest):
 */
%left UCC_INSERT ADD_EQ SUB_EQ MUL_EQ DIV_EQ MOD_EQ AND_EQ '='
%left AND OR
%left EQUALS NEQUALS
%left LTEQUALS GTEQUALS '<' '>' UCC_IN
%left '-' '+' '&'
%left '*' '/' '%'
%right NOT ADDRESSOF UMINUS UPLUS NEW DELETE UCC_CAST
%left UCC_SYM
%left UCC_POINTS '.' '(' ')' '[' ']'
%left UCC_SCOPE

/*
 *	Production types:
 */
%type <expr> expression primary declared_var_value opt_script_delay item
%type <expr> script_command start_call new_expr class_expr
%type <expr> nonclass_expr opt_delay appended_element int_literal
%type <expr> opt_primary_expression conv_expression repeat_count
%type <intval> direction converse_options actor_frames egg_criteria
%type <intval> opt_original assignment_operator const_int_val opt_const_int_val
%type <intval> const_int_type int_cast dam_type opt_nest opt_int_value
%type <intval> sign_int_literal const_int_expr opt_const_int_expr addressof
%type <funid> opt_funid
%type <membersel> member_selector
%type <intlist> string_list response_expression
%type <sym> declared_sym
%type <var> declared_var param
%type <cls> opt_inheritance defined_class
%type <struc> defined_struct
%type <funsym> function_proto function_decl
%type <varvec> param_list opt_param_list
%type <stmt> statement assignment_statement if_statement while_statement
%type <stmt> statement_block return_statement function_call_statement
%type <stmt> special_method_call_statement trycatch_statement trystart_statement
%type <stmt> try_statement simple_statement noncase_statement simple_or_if_statement
%type <stmt> array_loop_statement var_decl var_decl_list stmt_declaration
%type <stmt> class_decl class_decl_list struct_decl_list struct_decl
%type <stmt> break_statement converse_statement opt_nobreak opt_nobreak_do
%type <stmt> converse_case switch_case script_statement switch_statement
%type <stmt> goto_statement answer_statement delete_statement opt_nobreak_conv
%type <stmt> continue_statement response_case fallthrough_statement
%type <stmt> scoped_statement converse_case_attend
%type <stmt> array_enum_statement opt_trailing_label
%type <block> statement_list noncase_statement_list
%type <label> label_statement
%type <loopvars> start_array_variables
%type <loopinit> start_array_loop
%type <exprlist> opt_expression_list expression_list script_command_list
%type <exprlist> opt_nonclass_expr_list nonclass_expr_list appended_element_list
%type <stmtlist> switch_case_list converse_case_list response_case_list
%type <funcall> function_call script_expr run_script_expression
%type <strval> string_literal string_prefix

%%

design:
	design global_decl
	| global_decl
	;

global_decl:
	function
	| function_decl
		{
		if (!Uc_function::add_global_function_symbol($1)) {
			delete $1;
		}
		}
	| const_int_decl
	| enum_decl
	| static_decl
	| class_definition
	| struct_definition
	;

class_definition:
	CLASS IDENTIFIER opt_inheritance
		{
		if ($3) {
			cur_class = new Uc_class($2, $3);
		} else {
			cur_class = new Uc_class($2);
		}
		units.push_back(cur_class);
		}
		'{' class_item_list '}'
		{
		// Add to 'globals' symbol table.
		Uc_class_symbol::create($2, cur_class);
		cur_class = nullptr;
		}
	;

opt_inheritance:
	':' defined_class
		{ $$ = $2; }
	| %empty
		{ $$ = nullptr; }
	;

class_item_list:
	class_item_list class_item
	| %empty
	;

class_item:
	VAR { has_ret = true; } class_var_def
		{ has_ret = false; }
	| VAR alias_tok IDENTIFIER '=' declared_var ';'
		{ cur_class->add_alias($3, $5); }
	| STRUCT '<' defined_struct '>' { struct_type = $3; } class_struct_def
		{ struct_type = nullptr; }
	| STRUCT '<' defined_struct '>' alias_tok IDENTIFIER '=' declared_var ';'
		{ cur_class->add_alias($6, $8, $3); }
	| CLASS '<' defined_class '>' { class_type = $3; } method
		{ class_type = nullptr; }
	| opt_void method
	;

class_var_def:
	var_decl_list ';'
	| method
	;

class_struct_def:
	struct_decl_list ';'
	| method
	;

method:
	IDENTIFIER '(' opt_param_list ')'
		{
		$3->insert(
				$3->begin(),    // So it's local[0].
				new Uc_class_inst_symbol("this", cur_class, 0));
		auto* funsym = Uc_function_symbol::create(
				$1, -1, *$3, false, cur_class->get_scope(),
				Uc_function_symbol::utility_fun);
		delete $3;    // A copy was made.

		// Set return type.
		if (has_ret) {
			funsym->set_ret_type(true);
		} else if (class_type) {
			funsym->set_ret_type(class_type);
		} else if (struct_type) {
			funsym->set_ret_type(struct_type);
		}
		has_ret     = false;
		class_type  = nullptr;
		struct_type = nullptr;

		cur_fun = new Uc_function(funsym, cur_class->get_scope());
		cur_class->add_method(cur_fun);
		}
	function_body
		{ cur_fun = nullptr; }
	;

struct_definition:
	STRUCT IDENTIFIER
		{
		cur_struct = new Uc_struct_symbol($2);
		Uc_function::add_global_struct_symbol(cur_struct);
		}
		'{' struct_item_list '}'
		{
		// Add to 'globals' symbol table.
		cur_struct = nullptr;
		}
	;

struct_item_list:
	struct_item_list struct_item
	| %empty
	;

struct_item:
	VAR IDENTIFIER ';'
		{ cur_struct->add($2); }
	| STRUCT '<' defined_struct '>' ';'
		{ cur_struct->merge_struct($3); }
	| STRUCT '<' defined_struct '>' IDENTIFIER ';'
		{ cur_struct->merge_struct($3); }
	;

function:
	function_proto
		{
		cur_fun = new Uc_function($1);
		units.push_back(cur_fun);
		}
	function_body
		{
		cur_fun = nullptr;
		}
	;

opt_trailing_label:
	label_statement
		{
		if (cur_fun->has_ret()) {
			char buf[180];
			snprintf(
					buf, sizeof(buf),
					"Trailing label '%s' in non-void function '%s' is not "
					"allowed",
					$1->get_label().c_str(), cur_fun->get_name());
			yyerror(buf);
			$$ = nullptr;
		}
		}
	| %empty
		{
		$$ = nullptr;
		}
	;

function_body:
	'{' statement_list opt_trailing_label '}'
		{
		if ($3 != nullptr) {
			$2->add($3);
		}
		cur_fun->set_statement($2);
		}
	;

					/* opt_const_int_val assigns function #. */
function_proto:
	ret_type IDENTIFIER { start_fun_id(); } opt_funid '(' opt_param_list ')'
		{
		end_fun_id();
		if ($4->kind != Uc_function_symbol::utility_fun) {
			char buf[180];
			if (has_ret || struct_type) {
				snprintf(
						buf, sizeof(buf),
						"Functions declared with '%s#' cannot return a value",
						$4->kind == Uc_function_symbol::shape_fun ? "shape"
																  : "object");
				yyerror(buf);
			}
			if (!$6->empty()) {
				snprintf(
						buf, sizeof(buf),
						"Functions declared with '%s#' cannot have arguments",
						$4->kind == Uc_function_symbol::shape_fun ? "shape"
																  : "object");
				yyerror(buf);
			}
		}
		$$ = Uc_function_symbol::create(
				$2, $4->id, *$6, is_extern, nullptr, $4->kind);
		if (has_ret) {
			$$->set_ret_type(true);
		} else if (struct_type) {
			$$->set_ret_type(struct_type);
		}
		delete $6;    // A copy was made.
		delete $4;
		has_ret     = false;
		struct_type = nullptr;
		}
	| CLASS '<' defined_class '>' IDENTIFIER opt_const_int_val '(' opt_param_list ')'
		{
		$$ = Uc_function_symbol::create(
				$5, $6, *$8, is_extern, nullptr,
				Uc_function_symbol::utility_fun);
		$$->set_ret_type($3);
		delete $8;    // A copy was made.
		}
	;

opt_funid:
	SHAPENUM '(' const_int_expr ')'
		{
		$$ = new Fun_id_info(Uc_function_symbol::shape_fun, $3 < 0 ? -1 : $3);
		if ($3 < 0) {
			yyerror("Shape number cannot be negative");
		}
		}
	| OBJECTNUM '(' opt_const_int_expr ')'
		{ $$ = new Fun_id_info(Uc_function_symbol::object_fun, $3); }
	| opt_const_int_val
		{ $$ = new Fun_id_info(Uc_function_symbol::utility_fun, $1); }
	| IDNUM '(' const_int_expr ')'
		{ $$ = new Fun_id_info(Uc_function_symbol::utility_fun, $3); }
	;

opt_const_int_expr:
	const_int_expr
	| %empty
		{ $$ = -1; }
	;

const_int_expr:
	const_int_val
	| const_int_expr '+' const_int_expr
		{ $$ = $1 + $3; }
	| const_int_expr '-' const_int_expr
		{ $$ = $1 - $3; }
	| const_int_expr '*' const_int_expr
		{ $$ = $1 * $3; }
	| const_int_expr '/' const_int_expr
		{
		if ($3 == 0) {
			yyerror("Division by 0");
			$$ = -1;
		} else {
			$$ = $1 / $3;
		}
		}
	| const_int_expr '%' const_int_expr
		{
		if ($3 == 0) {
			yyerror("Division by 0");
			$$ = -1;
		} else {
			$$ = $1 % $3;
		}
		}
	| const_int_expr EQUALS const_int_expr
		{ $$ = $1 == $3; }
	| const_int_expr NEQUALS const_int_expr
		{ $$ = $1 != $3; }
	| const_int_expr '<' const_int_expr
		{ $$ = $1 < $3; }
	| const_int_expr LTEQUALS const_int_expr
		{ $$ = $1 <= $3; }
	| const_int_expr '>' const_int_expr
		{ $$ = $1 > $3; }
	| const_int_expr GTEQUALS const_int_expr
		{ $$ = $1 >= $3; }
	| const_int_expr AND const_int_expr
		{ $$ = $1 && $3; }
	| const_int_expr OR const_int_expr
		{ $$ = $1 || $3; }
	| NOT const_int_expr
		{ $$ = $2 == 0; }
	| '(' const_int_expr ')'
		{ $$ = $2; }
	| addressof
	;

opt_const_int_val:
	const_int_val
	| %empty
		{ $$ = -1; }
	;

const_int_val:
	sign_int_literal
	| IDENTIFIER
		{
		Uc_symbol*           sym = Uc_function::search_globals($1);
		Uc_const_int_symbol* var;
		char                 buf[180];
		if (!sym) {
			snprintf(buf, sizeof(buf), "'%s' not declared", $1);
			yyerror(buf);
			$$ = -1;
		} else if ((var = dynamic_cast<Uc_const_int_symbol*>(sym)) == nullptr) {
			snprintf(buf, sizeof(buf), "'%s' is not a constant integer", $1);
			yyerror(buf);
			$$ = -1;
		} else {
			$$ = var->get_value();
		}
		}
	;

statement_block:
	statement_block_start statement_list opt_trailing_label '}'
		{	// Block ends in label.
		if ($3 != nullptr) {
			$2->add($3);
		}
		$$ = $2;
		nested_if.pop_back();
		cur_fun->pop_scope();
		}
	| label_statement statement
		{	// Label followed by statements; "grab" next statement for label.
		if ($2) {
			auto* stmt = new Uc_block_statement();
			stmt->add($1);
			stmt->add($2);
			$$ = stmt;
		} else {    // This is the case for the "null" statement ';'.
			$$ = $1;
		}
		}
	;

statement_block_start:
	'{'
		{
		cur_fun->push_scope();
		nested_if.push_back(false);
		}
	;

statement_list:
	statement_list statement
		{
		if ($2) {
			$$->add($2);
		}
		}
	| %empty
		{ $$ = new Uc_block_statement(); }
	;

noncase_statement_list:
	noncase_statement_list noncase_statement
		{
		if ($2) {
			$$->add($2);
		}
		}
	| %empty
		{ $$ = new Uc_block_statement(); }
	;

simple_or_if_statement:
	simple_statement
	| if_statement
	;

simple_statement:
	stmt_declaration
	| assignment_statement
	| trycatch_statement
	| while_statement
	| array_loop_statement
	| array_enum_statement
	| function_call_statement
	| special_method_call_statement
	| return_statement
	| converse_statement
	| switch_statement
	| script_statement
	| break_statement
		{
		if (!can_break()) {
			yyerror("'break' statement not allowed outside of "
					"loops/converse/breakable/forever statements");
		}
		$$ = $1;
		}
	| continue_statement
		{
		if (!can_continue()) {
			yyerror("'continue' statement not allowed outside of "
					"loops/converse/breakable/forever statements");
		}
		$$ = $1;
		}
	| fallthrough_statement
	| goto_statement
	| delete_statement
	| SAY  '(' opt_nonclass_expr_list ')' ';'
		{ $$ = new Uc_say_statement($3); }
	| MESSAGE '(' opt_nonclass_expr_list ')' ';'
		{ $$ = new Uc_message_statement($3); }
	| answer_statement
	| ENDCONV ';'
		{ $$ = new Uc_endconv_statement(); }
	| throwabort_statement ';'
		{ $$ = new Uc_abort_statement(); }
	| throwabort_statement expression ';'
		{ $$ = new Uc_abort_statement($2); }
	| ';'				/* Null statement */
		{ $$ = nullptr; }
	;

statement:
	simple_or_if_statement
	| statement_block
	| converse_case_attend
	;

noncase_statement:
	simple_or_if_statement
	| statement_block
	;

throwabort_statement:
	ABORT
	| THROW
	;

alias_tok:
	ALIAS
	| '&'
	;

stmt_declaration:
	DECLARE_ VAR IDENTIFIER ';'
		{
		if (cur_fun) {
			cur_fun->add_symbol($3, false);
		} else {
			cur_class->add_symbol($3);
		}
		$$ = nullptr;
		}
	| DECLARE_ STRUCT '<' defined_struct '>' { struct_type = $4; } IDENTIFIER ';'
		{
		if (cur_fun) {
			cur_fun->add_symbol($7, struct_type, false);
		} else {
			cur_class->add_symbol($7, struct_type);
		}
		struct_type = nullptr;
		$$          = nullptr;
		}
	| DECLARE_ CLASS '<' defined_class '>' { class_type = $4; } IDENTIFIER ';'
		{
		if (class_type && cur_fun) {
			cur_fun->add_symbol($7, class_type, false);
		} else {
			// Unsupported for now
		}
		class_type = nullptr;
		$$         = nullptr;
		}
	| VAR var_decl_list ';'
		{ $$ = $2; }
	| VAR alias_tok IDENTIFIER '=' declared_var ';'
		{ cur_fun->add_alias($3, $5); $$ = nullptr; }
	| STRUCT '<' defined_struct '>' { struct_type = $3; } struct_decl_list ';'
		{ struct_type = nullptr; $$ = $6; }
	| STRUCT '<' defined_struct '>' alias_tok IDENTIFIER '=' declared_var ';'
		{ cur_fun->add_alias($6, $8, $3); $$ = nullptr; }
	| CLASS '<' defined_class '>' { class_type = $3; } class_decl_list ';'
		{ class_type = nullptr; $$ = $6; }
	| CLASS '<' defined_class '>' alias_tok IDENTIFIER '=' declared_var ';'
		{
		if (!$8->get_cls()) {
			yyerror("Can't convert non-class into class.");
		} else if (!Incompatible_classes_error($8->get_cls(), $3)) {
			// Alias may be of different (compatible) class.
			cur_fun->add_alias($6, $8, $3);
		}
		$$ = nullptr;
		}
	| STRING string_decl_list opt_comma ';'
		{ $$ = nullptr; }
	| const_int_decl
		{ $$ = nullptr; }
	| enum_decl
		{ $$ = nullptr; }
	| function_decl
		{
		if (!cur_fun->add_function_symbol(
					$1, cur_class ? cur_class->get_scope() : nullptr)) {
			delete $1;
		}
		$$ = nullptr;
		}
	| static_decl
		{ $$ = nullptr; }
	;

var_decl_list:
	var_decl_list ',' var_decl
		{
		if (!$3) {
			$$ = $1;
		} else if (!$1) {
			$$ = $3;
		} else /* Both nonzero.  Need a list. */
		{
			auto* b = dynamic_cast<Uc_block_statement*>($1);
			if (!b) {
				b = new Uc_block_statement();
				b->add($1);
			}
			b->add($3);
			$$ = b;
		}
		}
	| var_decl
		{ $$ = $1; }
	;

opt_comma:
	','
	| %empty
	;

enum_decl:				/* Decls. the elems, not the enum. */
	ENUM IDENTIFIER { enum_val = -1; }
			opt_enum_type '{' enum_item_list opt_comma '}' ';'
		{ const_opcode.pop_back(); }
	;

const_int_type:
	UCC_INT
		{ $$ = UC_PUSHI; }
	| UCC_CHAR
		{ $$ = UC_PUSHB; }
	| UCC_BYTE
		{ $$ = UC_PUSHB; }
	| UCC_LONG
		{ $$ = UC_PUSHI32; }
	| UCC_LONG UCC_INT
		{ $$ = UC_PUSHI32; }
	;

opt_enum_type:
	':' const_int_type
		{ const_opcode.push_back(static_cast<UsecodeOps>($2)); }
	| %empty
		{ const_opcode.push_back(UC_PUSHI); }
	;

enum_item_list:
	enum_item_list ',' enum_item
	| enum_item
	;

enum_item:
	const_int
	| IDENTIFIER
		{			/* Increment last value.	*/
		++enum_val;
		int op = const_opcode.back();
		if (cur_fun) {
			cur_fun->add_int_const_symbol($1, enum_val, op);
		} else {    // Global.
			Uc_function::add_global_int_const_symbol($1, enum_val, op);
		}
		}
	;

const_int_decl:
	UCC_CONST const_int_type { const_opcode.push_back(static_cast<UsecodeOps>($2)); }
		const_int_decl_list opt_comma ';'
		{ const_opcode.pop_back(); }
	;

const_int_decl_list:
	const_int_decl_list ',' const_int
	| const_int
	;

const_int:
	IDENTIFIER '=' const_int_expr
		{
		int op = const_opcode.back();
		if (cur_fun) {
			cur_fun->add_int_const_symbol($1, $3, op);
		} else {    // Global.
			Uc_function::add_global_int_const_symbol($1, $3, op);
		}
		enum_val = $3;    // In case we're in an enum.
		}
	| IDENTIFIER '=' int_cast const_int_expr
		{
		auto op = static_cast<UsecodeOps>($3);
		if (cur_fun) {
			cur_fun->add_int_const_symbol($1, $4, op);
		} else {    // Global.
			Uc_function::add_global_int_const_symbol($1, $4, op);
		}
		enum_val = $4;    // In case we're in an enum.
		}
	;

var_decl:
	IDENTIFIER
		{
		if (cur_fun) {
			cur_fun->add_symbol($1, true);
		} else {
			cur_class->add_symbol($1);
		}
		$$ = nullptr;
		}
	| IDENTIFIER '=' nonclass_expr
		{
		if (cur_class && !cur_fun) {
			char buf[180];
			snprintf(
					buf, sizeof(buf),
					"Initialization of class member var '%s' must be done "
					"through constructor",
					$1);
			yyerror(buf);
			$$ = nullptr;
		} else {
			auto* var = cur_fun ? cur_fun->add_symbol($1, true)
								: cur_class->add_symbol($1);
			var->set_is_obj_fun($3->is_object_function(false));
			$$ = new Uc_assignment_statement(new Uc_var_expression(var), $3);
		}
		}
	| IDENTIFIER '=' script_expr
		{
		if (cur_class && !cur_fun) {
			char buf[180];
			snprintf(
					buf, sizeof(buf),
					"Initialization of class member var '%s' must be done "
					"through constructor",
					$1);
			yyerror(buf);
			$$ = nullptr;
		} else {
			auto* var = cur_fun ? cur_fun->add_symbol($1, true)
								: cur_class->add_symbol($1);
			$$ = new Uc_assignment_statement(new Uc_var_expression(var), $3);
		}
		}
	;

class_decl_list:
	class_decl_list ',' class_decl
		{
		if (!$3) {
			$$ = $1;
		} else if (!$1) {
			$$ = $3;
		} else /*	Both nonzero; need a list.	*/
		{
			auto* b = dynamic_cast<Uc_block_statement*>($1);
			if (!b) {
				b = new Uc_block_statement();
				b->add($1);
			}
			b->add($3);
			$$ = b;
		}
		}
	| class_decl
		{ $$ = $1; }
	;

class_decl:
	IDENTIFIER
		{
		if (class_type && cur_fun) {
			cur_fun->add_symbol($1, class_type, true);
		} else {
			// Unsupported for now
		}
		$$ = nullptr;
		}
	| IDENTIFIER '=' class_expr
		{
		if (!class_type || !$3 || Nonclass_unexpected_error($3)) {
			$$ = nullptr;
		} else {
			Uc_class* src = $3->get_cls();
			if (Incompatible_classes_error(src, class_type)) {
				$$ = nullptr;
			} else {
				auto* v = cur_fun->add_symbol($1, class_type, true);
				$$      = new Uc_assignment_statement(
				                        new Uc_class_expression(v), $3);
			}
		}
		}
	;

struct_decl_list:
	struct_decl_list ',' struct_decl
		{
		if (!$3) {
			$$ = $1;
		} else if (!$1) {
			$$ = $3;
		} else /*	Both nonzero; need a list.	*/
		{
			auto* b = dynamic_cast<Uc_block_statement*>($1);
			if (!b) {
				b = new Uc_block_statement();
				b->add($1);
			}
			b->add($3);
			$$ = b;
		}
		}
	| struct_decl
		{ $$ = $1; }
	;

struct_decl:
	IDENTIFIER
		{
		if (cur_fun) {
			cur_fun->add_symbol($1, struct_type, true);
		} else {
			cur_class->add_symbol($1, struct_type);
		}
		$$ = nullptr;
		}
	| IDENTIFIER '=' nonclass_expr
		{
		if (cur_class && !cur_fun) {
			char buf[180];
			snprintf(
					buf, sizeof(buf),
					"Initialization of class member struct '%s' must be done "
					"through constructor",
					$1);
			yyerror(buf);
			$$ = nullptr;
		} else {
			auto* var = cur_fun ? cur_fun->add_symbol($1, struct_type, true)
								: cur_class->add_symbol($1, struct_type);
			var->set_is_obj_fun($3->is_object_function(false));
			$$ = new Uc_assignment_statement(new Uc_var_expression(var), $3);
		}
		}
	;


class_expr:
	new_expr
		{ $$ = $1; }
	| IDENTIFIER
		{
		Uc_symbol* sym = cur_fun->search_up($1);
		if (!sym) {
			char buf[150];
			snprintf(buf, sizeof(buf), "'%s' not declared", $1);
			yyerror(buf);
			cur_fun->add_symbol($1, false);
			$$ = nullptr;
		} else if (sym->get_sym_type() != Uc_symbol::Class) {
			char buf[150];
			snprintf(buf, sizeof(buf), "'%s' not a class", $1);
			yyerror(buf);
			$$ = nullptr;
		} else {
			// Tests above guarantee this will always work.
			auto* cls = dynamic_cast<Uc_class_inst_symbol*>(sym->get_sym());
			$$        = new Uc_class_expression(cls);
		}
		}
	| function_call
		{
		$$ = $1;
		}
	;

static_decl:
	STATIC_ VAR static_var_decl_list ';'
	| STATIC_ STRUCT '<' defined_struct '>'
		{ struct_type = $4; }
		static_struct_var_decl_list ';'
		{ struct_type = nullptr; }
	| STATIC_ CLASS '<' defined_class '>'
		{ class_type = $4; }
		static_cls_decl_list ';'
		{ class_type = nullptr; }
	;

static_var_decl_list:
	static_var
	| static_var_decl_list ',' static_var
	;

static_var:
	IDENTIFIER
		{
		if (cur_fun) {
			cur_fun->add_static($1);
		} else {
			Uc_function::add_global_static($1);
		}
		}
	;

static_struct_var_decl_list:
	static_struct_var
	| static_struct_var_decl_list ',' static_struct_var
	;

static_struct_var:
	IDENTIFIER
		{
		if (cur_fun) {
			cur_fun->add_static($1, struct_type);
		} else {
			Uc_function::add_global_static($1, struct_type);
		}
		}
	;

static_cls_decl_list:
	static_cls
	| static_cls_decl_list ',' static_cls
	;

static_cls:
	IDENTIFIER
		{
		if (cur_fun) {
			cur_fun->add_static($1, class_type);
		} else {
			Uc_function::add_global_static($1, class_type);
		}
		}
	;

string_literal:
	string_literal STRING_LITERAL
		{
		size_t left_len  = strlen($1);
		size_t right_len = strlen($2);
		char*  ret       = new char[left_len + right_len + 1];
		std::memcpy(ret, $1, left_len);
		std::memcpy(ret + left_len, $2, right_len);
		ret[left_len + right_len] = '\0';
		delete[] $1;
		delete[] $2;
		$$ = ret;
		}
	| STRING_LITERAL
	;

string_prefix:
	string_literal STRING_PREFIX
		{
		size_t left_len  = strlen($1);
		size_t right_len = strlen($2);
		char*  ret       = new char[left_len + right_len + 1];
		std::memcpy(ret, $1, left_len);
		std::memcpy(ret + left_len, $2, right_len);
		ret[left_len + right_len] = '\0';
		delete[] $1;
		delete[] $2;
		$$ = ret;
		}
	| STRING_PREFIX
	;

string_decl_list:
	string_decl_list ',' string_decl
	| string_decl
	;

string_decl:
	IDENTIFIER '=' string_literal
		{
		cur_fun->add_string_symbol($1, $3);
		}
	;

function_decl:
	EXTERN { is_extern = true; } function_proto ';'
		{ $$ = $3; is_extern = false; }
	;

assignment_statement:
	expression '=' expression ';'
		{
		// Some rudimentary type-checking for classes
		if ($1->is_class()) {
			if (Nonclass_unexpected_error($3)) {
				$$ = nullptr;
			} else {
				Uc_class* trg = $1->get_cls();
				Uc_class* src = $3->get_cls();
				if (Incompatible_classes_error(src, trg)) {
					$$ = nullptr;
				} else {
					$1->set_is_obj_fun($3->is_object_function(false));
					$$ = new Uc_assignment_statement($1, $3);
				}
			}
		} else if (Class_unexpected_error($3)) {
			$$ = nullptr;
		} else {
			$1->set_is_obj_fun($3->is_object_function(false));
			$$ = new Uc_assignment_statement($1, $3);
		}
		}
	| expression '=' script_expr ';'
		{
		if (Class_unexpected_error($1)) {
			$$ = nullptr;
		} else {
			$$ = new Uc_assignment_statement($1, $3);
		}
		}
	| nonclass_expr assignment_operator nonclass_expr ';'
		{
		$1->set_is_obj_fun(-1);
		$$ = new Uc_assignment_statement(
				$1,
				new Uc_binary_expression(static_cast<UsecodeOps>($2), $1, $3));
		}
	| nonclass_expr UCC_INSERT appended_element_list ';'
		{
		$1->set_is_obj_fun(-1);
		$$ = new Uc_assignment_statement($1, new Uc_array_expression($1, $3));
		}
	;

assignment_operator:
	ADD_EQ
		{ $$ = UC_ADD; }
	| SUB_EQ
		{ $$ = UC_SUB; }
	| MUL_EQ
		{ $$ = UC_MUL; }
	| DIV_EQ
		{ $$ = UC_DIV; }
	| MOD_EQ
		{ $$ = UC_MOD; }
	| AND_EQ
		{ $$ = UC_ARRA; }
	;

appended_element_list:
	appended_element_list UCC_INSERT appended_element
		{ $$->add($3); }
	| appended_element
		{
		$$ = new Uc_array_expression();
		$$->add($1);
		}
	;

appended_element:
	nonclass_expr
	| '{' { start_script(); } script_command_list '}'
		{
		$$ = $3;
		end_script();
		}
	;

scoped_statement:
	statement_block_start statement_list opt_trailing_label '}'
		{
		if ($3 != nullptr) {
			$2->add($3);
		}
		cur_fun->pop_scope();
		nested_if.pop_back();
		$$ = $2;
		}
	| { cur_fun->push_scope(); nested_if.push_back(false); } simple_statement
		{
		if (Uc_location::get_strict_mode()) {
			yyerror("A Statements must be surrounded by braces in strict mode");
		}
		cur_fun->pop_scope();
		nested_if.pop_back();
		$$ = $2;
		}
	| { cur_fun->push_scope(); nested_if.push_back(false); } if_statement
		{
		nested_if.pop_back();
		if (Uc_location::get_strict_mode() && (nested_if.empty() || !nested_if.back())) {
			yyerror("B Statements must be surrounded by braces in strict mode");
		}
		cur_fun->pop_scope();
		$$ = $2;
		}
	;

if_statement:
	IF '(' expression ')' scoped_statement %prec IF
		{
		int val;
		if ($3->eval_const(val)) {
			if (val) {
				if (dynamic_cast<Uc_bool_expression*>($3) == nullptr) {
					$3->warning("'if' clause will always be executed");
				}
				$$ = $5;
			} else {    // Need this because of those pesky GOTOs...
				if (dynamic_cast<Uc_bool_expression*>($3) == nullptr) {
					$3->warning("'if' clause may never execute");
				}
				$$ = new Uc_if_statement(nullptr, $5, nullptr);
			}
			delete $3;
		} else {
			$$ = new Uc_if_statement($3, $5, nullptr);
		}
		}
	| IF '(' expression ')' scoped_statement
		ELSE { nested_if.push_back(true); } scoped_statement
		{
		int val;
		if ($3->eval_const(val)) {
			if (val) {
				// Need this because of those pesky GOTOs...
				if (dynamic_cast<Uc_bool_expression*>($3) == nullptr) {
					$3->warning("'else' clause may never execute");
				}
				$$ = new Uc_if_statement(
						new Uc_int_expression(val != 0), $5, $8);
			} else {
				// Need this because of those pesky GOTOs...
				if (dynamic_cast<Uc_bool_expression*>($3) == nullptr) {
					$3->warning("'if' clause may never execute");
				}
				$$ = new Uc_if_statement(nullptr, $5, $8);
			}
			delete $3;
		} else {
			$$ = new Uc_if_statement($3, $5, $8);
		}
		nested_if.pop_back();
		}
	;

trycatch_statement:
	trystart_statement '{' statement_list opt_trailing_label '}'
		{
		if ($4 != nullptr) {
			$3->add($4);
		}
		auto* stmt = dynamic_cast<Uc_trycatch_statement*>($1);
		if (!stmt) {
			yyerror("try/catch statement is not a try/catch statement");
		} else {
			stmt->set_catch_statement($3);
		}
		cur_fun->pop_scope();
		$$ = stmt;
		}
	;

trystart_statement:
	try_statement statement_list opt_trailing_label '}' CATCH '(' ')'
		{
		if ($3 != nullptr) {
			$2->add($3);
		}
		cur_fun->pop_scope();
		cur_fun->push_scope();
		$$ = new Uc_trycatch_statement($2);
		}
	| try_statement statement_list opt_trailing_label '}' CATCH '(' IDENTIFIER ')'
		{
		if ($3 != nullptr) {
			$2->add($3);
		}
		cur_fun->pop_scope();
		cur_fun->push_scope();
		auto* stmt = new Uc_trycatch_statement($2);
		stmt->set_catch_variable(cur_fun->add_symbol($7, true));
		$$ = stmt;
		}
	;

try_statement:
	TRY '{'
		{
		cur_fun->push_scope();
		}
	;

opt_nobreak:
	NOBREAK statement_block
		{ $$ = $2; }
	| %empty %prec LOOP
		{ $$ = nullptr; }
	;

opt_nobreak_do:
	NOBREAK statement_block
		{ $$ = $2; }
	| ';' %prec LOOP
		{ $$ = nullptr; }
	;

opt_nobreak_conv:
	NOBREAK statement_block
		{ $$ = $2; }
	| %empty %prec LOOP
		{ $$ = nullptr; }
	;

while_statement:
	WHILE '(' nonclass_expr ')' { start_loop(); } scoped_statement { end_loop(); } opt_nobreak
		{
		int val;
		if ($3->eval_const(val)) {
			if (val) {
				if (dynamic_cast<Uc_bool_expression*>($3) == nullptr) {
					$3->warning("Infinite loop detected");
				}
				$$ = new Uc_infinite_loop_statement($6, $8);
			} else {    // Need 'may' because of those pesky GOTOs...
				if (dynamic_cast<Uc_bool_expression*>($3) == nullptr) {
					$3->warning("Body of 'while' statement may never execute");
				}
				$$ = new Uc_while_statement(nullptr, $6, $8);
			}
			delete $3;
		} else {
			$$ = new Uc_while_statement($3, $6, $8);
		}
		}
	| FOREVER { start_loop(); } scoped_statement { end_loop(); } opt_nobreak
		{ $$ = new Uc_infinite_loop_statement($3, $5); }
	| DO { start_loop(); } scoped_statement WHILE '(' nonclass_expr ')' { end_loop(); } opt_nobreak_do
		{
		int val;
		if ($6->eval_const(val)) {
			if (val) {
				if (dynamic_cast<Uc_bool_expression*>($6) == nullptr) {
					$6->warning("Infinite loop detected");
				}
				$$ = new Uc_infinite_loop_statement($3, $9);
			} else    // Optimize loop away.
			{
				$$ = new Uc_breakable_statement($3, $9);
			}
			delete $6;
		} else {
			$$ = new Uc_dowhile_statement($6, $3, $9);
		}
		}
	| BREAKABLE { start_loop(); } scoped_statement { end_loop(); } opt_nobreak
		{ $$ = new Uc_breakable_statement($3, $5); }
	;

array_enum_statement:
	ENUM '(' ')' ';'
		{ $$ = new array_enum_statement(); }
	;

array_loop_statement:
	start_array_variables { start_loop(); } scoped_statement { end_loop(); } opt_nobreak
		{
		auto* expr = new Uc_arrayloop_statement($1->var, $1->array);
		expr->set_statement($3);
		expr->set_nobreak($5);
		expr->set_index($1->index);
		expr->set_array_size($1->length);
		expr->finish(cur_fun);
		cur_fun->pop_scope();
		delete $1;    // No onger needed
		$$ = expr;
		}
	| start_array_variables ATTEND IDENTIFIER ';'
		{
		auto* expr = new Uc_arrayloop_attend_statement($1->var, $1->array);
		expr->set_index($1->index);
		expr->set_array_size($1->length);
		expr->set_target($3);
		cur_fun->pop_scope();
		expr->finish(cur_fun);
		delete $1;    // No onger needed
		$$ = expr;
		}
	;

start_array_variables:
	start_array_loop ')'
		{
		Uc_var_symbol* index
				= cur_fun->add_symbol("$$for_loop_index_tempvar", true);
		Uc_var_symbol* length
				= cur_fun->add_symbol("$$for_loop_length_tempvar", true);
		Uc_var_symbol* var = Get_variable($1->var);
		$$                 = new Loop_Vars(var, $1->array, index, length);
		delete $1;
		}
	| start_array_loop WITH IDENTIFIER ')'
		{
		Uc_var_symbol* index = Get_variable($3);
		Uc_var_symbol* length
				= cur_fun->add_symbol("$$for_loop_length_tempvar", true);
		Uc_var_symbol* var = Get_variable($1->var);
		$$                 = new Loop_Vars(var, $1->array, index, length);
		delete $1;
		}
	| start_array_loop WITH IDENTIFIER TO IDENTIFIER ')'
		{
		Uc_var_symbol* index  = Get_variable($3);
		Uc_var_symbol* length = Get_variable($5);
		Uc_var_symbol* var    = Get_variable($1->var);
		$$                    = new Loop_Vars(var, $1->array, index, length);
		delete $1;
		}
	;

start_array_loop:
	start_for IDENTIFIER UCC_IN declared_var
		{
		if ($4->get_cls()) {
			char buf[150];
			snprintf(
					buf, sizeof(buf), "Can't convert class '%s' into non-class",
					$4->get_name());
			yyerror(buf);
		}
		$$ = new Loop_Init($2, $4);
		}
	;

start_for:
	FOR '('
		{ cur_fun->push_scope(); }
	;

function_call_statement:
	function_call ';'
		{ $$ = new Uc_call_statement($1);  }
	;

special_method_call_statement:
					/* Have 'primary' say something.*/
	primary hierarchy_tok SAY '(' opt_nonclass_expr_list ')' ';'
		{
		auto* stmts = new Uc_block_statement();
		/* Set up 'show' call.		*/
		stmts->add(new Uc_call_statement(new Uc_call_expression(
				Uc_function::get_show_face(),
				new Uc_array_expression($1, new Uc_int_expression(0)),
				cur_fun)));
		stmts->add(new Uc_say_statement($5));
		$$ = stmts;
		}
	| primary hierarchy_tok HIDE '(' ')' ';'
		{
		$$ = new Uc_call_statement(new Uc_call_expression(
				Uc_function::get_remove_face(), new Uc_array_expression($1),
				cur_fun));
		}
	| run_script_expression ';'
		{ $$ = new Uc_call_statement($1); }
	;

run_script_expression:
	opt_primary_expression RUNSCRIPT '(' nonclass_expr opt_delay ')'
		{
		auto* parms = new Uc_array_expression();
		parms->add($1);    // Itemref.
		parms->add($4);    // Script.
		if ($5) {
			parms->add($5);    // Delay.
							   // Get the script intrinsic.
		}
		Uc_symbol* sym = Uc_function::get_intrinsic($5 ? 2 : 1);
		$$             = new Uc_call_expression(sym, parms, cur_fun);
		}
	;

opt_delay:
	',' nonclass_expr
		{ $$ = $2; }
	| %empty
		{ $$ = nullptr; }
	;

return_statement:
	RETURN expression ';'
		{
		if (!cur_fun->has_ret()) {
			char buf[180];
			snprintf(
					buf, sizeof(buf), "Function '%s' can't return a value",
					cur_fun->get_name());
			yyerror(buf);
			$$ = nullptr;
		} else {
			Uc_class* src = $2->get_cls();
			Uc_class* trg = cur_fun->get_cls();
			if (!src && !trg) {
				$$ = new Uc_return_statement($2);
			} else if (!src || !trg) {
				int ival;
				if (trg && $2->eval_const(ival) && ival == 0) {
					$$ = new Uc_return_statement($2);
				} else {
					char buf[210];
					snprintf(
							buf, sizeof(buf),
							"Function '%s' expects a return of %s '%s' but "
							"supplied value is %s'%s'",
							cur_fun->get_name(), trg ? "class" : "type",
							trg ? trg->get_name() : "var", src ? "class " : "",
							src ? src->get_name() : "var");
					yyerror(buf);
					$$ = nullptr;
				}
			} else if (Incompatible_classes_error(src, trg)) {
				$$ = nullptr;
			} else {
				$$ = new Uc_return_statement($2);
			}
		}
		}
	| RETURN ';'
		{
		if (cur_fun->has_ret()) {
			Uc_class* cls = cur_fun->get_cls();
			char      buf[180];
			snprintf(
					buf, sizeof(buf), "Function '%s' must return a '%s'",
					cur_fun->get_name(), cls ? cls->get_name() : "var");
			yyerror(buf);
			$$ = nullptr;
		} else {
			$$ = new Uc_return_statement();
		}
		}
	;

opt_nest:
	':' NESTED
		{ $$ = 1; }
	| %empty
		{ $$ = 0; }
	;

converse_statement:
	CONVERSE start_conv
			noncase_statement_list response_case_list '}' { end_converse(); } opt_nobreak_conv
		{
		cur_fun->pop_scope();
		--converse;
		$$ = new Uc_converse_statement(nullptr, $3, $4, false, $7);
		}
	| CONVERSE opt_nest conv_expression start_conv
			noncase_statement_list converse_case_list '}' { end_converse(); } opt_nobreak_conv
		{
		cur_fun->pop_scope();
		--converse;
		$$ = new Uc_converse_statement($3, $5, $6, $2, $9);
		}
	| CONVERSE ATTEND IDENTIFIER ';'
		{
		$$ = new Uc_converse_attend_statement($3);
		}
	;

start_conv:
	'{'
		{
		start_converse();
		cur_fun->push_scope();
		++converse;
		}
	;

conv_expression:
	'(' expression ')'
		{
		if (Class_unexpected_error($2)) {
			$$ = nullptr;
		} else {
			int ival;
			if ($2->eval_const(ival)) {
				$$ = nullptr;
			} else {
				$$ = $2;
			}
		}
		}
	;

converse_case_list:
	converse_case_list converse_case
		{
		if ($2) {
			$$->push_back($2);
		}
		}
	| %empty
		{ $$ = new vector<Uc_statement *>; }
	;

converse_case:
	CASE declared_var_value converse_options ':'
			{ cur_fun->push_scope(); } noncase_statement_list
		{
		Uc_expression* expr = $2;
		if (Class_unexpected_error(expr)) {
			expr = nullptr;
		}
		$$ = new Uc_converse_variable_case_statement(
				expr, $6, ($3 ? true : false));
		cur_fun->pop_scope();
		}
	| CASE string_list converse_options ':'
			{ cur_fun->push_scope(); } noncase_statement_list
		{
		$$ = new Uc_converse_strings_case_statement(
				*$2, $6, ($3 ? true : false));
		delete $2;    // A copy was made.
		cur_fun->pop_scope();
		}
	| ALWAYS ':'
			{ cur_fun->push_scope(); } noncase_statement_list
		{
		$$ = new Uc_converse_always_case_statement($4);
		cur_fun->pop_scope();
		}
	| DEFAULT ':'
			{ cur_fun->push_scope(); } noncase_statement_list
		{
		$$ = new Uc_converse_default_case_statement($4);
		cur_fun->pop_scope();
		}
	;

opt_int_value:
	'(' int_literal ')'
		{
		int ival;
		if (!$2->eval_const(ival)) {
			yyerror("Failed to obtain value from integer constant");
		}
		$$ = ival;
		}
	| %empty
		{ $$ = 1; }
	;

converse_case_attend:
	CASE declared_var_value converse_options ATTEND IDENTIFIER ':'
		{
		Uc_expression* expr = $2;
		if (Class_unexpected_error(expr)) {
			expr = nullptr;
		}
		$$ = new Uc_converse_variable_case_attend_statement(
				expr, $5, ($3 ? true : false));
		}
	| CASE string_list converse_options ATTEND IDENTIFIER ':'
		{
		$$ = new Uc_converse_strings_case_attend_statement(
				*$2, $5, ($3 ? true : false));
		delete $2;    // A copy was made.
		}
	| DEFAULT opt_int_value ATTEND IDENTIFIER ':'
		{ $$ = new Uc_converse_default_case_attend_statement($4, $2); }
	;

response_case_list:
	response_case_list ELSE { nested_if.push_back(true); } response_case
		{
		if ($4) {
			$$->push_back($4);
		}
		nested_if.pop_back();
		}
	| response_case %prec IF
		{
		$$ = new vector<Uc_statement*>;
		$$->push_back($1);
		}
	;

response_case:
	response_expression
			{ cur_fun->push_scope(); } statement_list
		{
		$$ = new Uc_converse_strings_case_statement(*$1, $3, false);
		delete $1;    // A copy was made.
		cur_fun->pop_scope();
		}
	;

response_expression:
	IF '(' RESPONSE EQUALS string_literal ')'
		{
		$$ = new vector<int>;
		$$->push_back(cur_fun->add_string($5));
		}
	| IF '(' RESPONSE UCC_IN '[' string_list ']' ')'
		{ $$ = $6; }
	;

string_list:
	string_list ',' string_literal
		{ $$->push_back(cur_fun->add_string($3)); }
	| string_literal
		{
		$$ = new vector<int>;
		$$->push_back(cur_fun->add_string($1));
		}
	;

converse_options:
	'(' REMOVE ')'			/* For now, just one.		*/
		{ $$ = 1; }
	| %empty
		{ $$ = 0; }
	;

switch_statement:
	SWITCH '('
			{ cur_fun->push_scope(); }
			expression ')' '{'
			{ start_breakable(); } switch_case_list '}'
		{
		if (Class_unexpected_error($4)) {
			$$ = nullptr;
		} else {
			end_breakable();
			$$ = new Uc_switch_statement($4, $8);
			delete $8;    // a copy has been made.
			cur_fun->pop_scope();
		}
		}
	;

switch_case_list:
	switch_case_list switch_case
		{ $$->push_back($2); }
	| switch_case
		{
		$$ = new vector<Uc_statement*>;
		$$->push_back($1);
		}
	;

switch_case:
	CASE int_literal ':' noncase_statement_list
		{ $$ = new Uc_switch_expression_case_statement($2, $4); }
	| CASE string_literal ':' noncase_statement_list
		{ $$ = new Uc_switch_expression_case_statement(
				new Uc_string_expression(cur_fun->add_string($2)), $4); }
	| DEFAULT ':' noncase_statement_list
		{ $$ = new Uc_switch_default_case_statement($3); }
	;

script_expr:
	SCRIPT { start_script(); } item opt_script_delay script_command
		{
		end_script();
		auto* parms = new Uc_array_expression();
		parms->add($3);    // Itemref.
		parms->add($5);    // Script.
		if ($4) {          // Delay?
			parms->add($4);
		}
		// Get the script intrinsic.
		Uc_symbol* sym = Uc_function::get_intrinsic($4 ? 2 : 1);
		$$             = new Uc_call_expression(sym, parms, cur_fun);
		}
	;

script_statement:			/* Yes, this could be an intrinsic. */
	script_expr
		{ $$ = new Uc_call_statement($1); }
	;

item:					/* Any object, NPC.	*/
	nonclass_expr
	;

script_command_list:
	script_command_list script_command
		{ $$->concat($2); }
	| script_command
		{
		$$ = new Uc_array_expression();
		$$->concat($1);
		}
	;

script_command:
	FINISH ';'
		{ $$ = new Uc_int_expression(Ucscript::finish, UC_PUSHB); }
	| RESURRECT ';'
		{ $$ = new Uc_int_expression(Ucscript::resurrect, UC_PUSHB); }
	| CONTINUE ';'			/* Continue script without painting. */
		{ $$ = new Uc_int_expression(Ucscript::cont, UC_PUSHB); }
	| RESET ';'			/* Go back to the beginning of the script */
		{ $$ = new Uc_int_expression(Ucscript::reset, UC_PUSHB); }
	| REPEAT repeat_count { repeat_nesting++; } script_command  ';'
		{
		repeat_nesting--;
		auto* result = new Uc_array_expression();
		result->concat($4);    // Start with cmnds. to repeat.
		int sz = result->get_exprs().size();
		result->add(new Uc_int_expression(
				repeat_nesting ? Ucscript::repeat2 : Ucscript::repeat,
				UC_PUSHB));
		// Then -offset to start.
		result->add(new Uc_int_expression(-sz));
		result->add($2);    // Loop var for repeat2.
		if (repeat_nesting) {
			result->add($2);    // Then #times to repeat.
		}
		$$ = result;
		}
	| REPEAT nonclass_expr ',' nonclass_expr { repeat_nesting++; }
		script_command  ';'
		{
		// Allow setting a different initial number of repeats.
		repeat_nesting--;
		auto* result = new Uc_array_expression();
		result->concat($6);    // Start with cmnds. to repeat.
		int sz = result->get_exprs().size();
		result->add(new Uc_int_expression(Ucscript::repeat2, UC_PUSHB));
		// Then -offset to start.
		result->add(new Uc_int_expression(-sz));
		result->add($2);    // Loop var for repeat2.
		result->add($4);    // Then #times to repeat.
		$$ = result;
		}
	| RAW '(' int_literal ')' ';'
		{ $$ = $3; }
	| NOP  ';'
		{ $$ = new Uc_int_expression(Ucscript::nop2, UC_PUSHB); }
	| NOP2 ';'
		{ $$ = new Uc_int_expression(Ucscript::nop1, UC_PUSHB); }
	| NOHALT  ';'
		{ $$ = new Uc_int_expression(Ucscript::dont_halt, UC_PUSHB); }
	| WAIT nonclass_expr  ';'		/* Ticks. */
		{ $$ = Create_array(Ucscript::delay_ticks, $2); }
	| WAIT nonclass_expr MINUTES  ';'	/* Game minutes. */
		{ $$ = Create_array(Ucscript::delay_minutes, $2); }
	| WAIT nonclass_expr HOURS  ';'	/* Game hours. */
		{ $$ = Create_array(Ucscript::delay_hours, $2); }
	| WAIT WHILE NEAR nonclass_expr ';'	/* Wait while avatar is near. */
		{ $$ = Create_array(Ucscript::wait_while_near, $4); }
	| WAIT WHILE FAR nonclass_expr ';'	/* Wait while avatar is far. */
		{ $$ = Create_array(Ucscript::wait_while_far, $4); }
	| REMOVE ';'			/* Remove item. */
		{ $$ = new Uc_int_expression(Ucscript::remove, UC_PUSHB); }
	| RISE ';'			/* For flying barges. */
		{ $$ = new Uc_int_expression(Ucscript::rise, UC_PUSHB); }
	| DESCEND ';'
		{ $$ = new Uc_int_expression(Ucscript::descend, UC_PUSHB); }
	| FRAME nonclass_expr ';'
		{ $$ = Create_array(Ucscript::frame, $2); }
	| ACTOR FRAME nonclass_expr ';'
		{
		$$ = new Uc_binary_expression(
				UC_ADD, new Uc_int_expression(Ucscript::npc_frame),
				new Uc_binary_expression(UC_MOD, $3, new Uc_int_expression(16)),
				UC_PUSHB);    // Want byte.
		}
	| ACTOR FRAME actor_frames ';'
		{ $$ = new Uc_int_expression(Ucscript::npc_frame + ($3 & 15), UC_PUSHB); }
	| HATCH ';'			/* Assumes item is an egg. */
		{ $$ = new Uc_int_expression(Ucscript::egg, UC_PUSHB); }
	| SETEGG nonclass_expr ',' nonclass_expr ';'
		{ $$ = Create_array(Ucscript::set_egg, $2, $4); }
	| SETEGG egg_criteria ',' nonclass_expr ';'
		{ $$ = Create_array(Ucscript::set_egg, new Uc_int_expression($2), $4); }
	| NEXT FRAME ';'		/* Next, but stop at last. */
		{ $$ = new Uc_int_expression(Ucscript::next_frame_max, UC_PUSHB); }
	| NEXT FRAME CYCLE ';'		/* Next, or back to 0. */
		{ $$ = new Uc_int_expression(Ucscript::next_frame, UC_PUSHB); }
	| PREVIOUS FRAME ';'		/* Prev. but stop at 0. */
		{ $$ = new Uc_int_expression(Ucscript::prev_frame_min, UC_PUSHB); }
	| PREVIOUS FRAME CYCLE ';'
		{ $$ = new Uc_int_expression(Ucscript::prev_frame, UC_PUSHB); }
	| SAY nonclass_expr ';'
		{ $$ = Create_array(Ucscript::say, $2); }
	| STEP nonclass_expr ';'		/* Step in given direction (0-7). */
		{
		$$ = Create_array(
				Ucscript::step,
				new Uc_binary_expression(
						UC_ADD, $2, new Uc_int_expression(0x30), UC_PUSHB),
				new Uc_int_expression(0));
		}
	| STEP nonclass_expr ',' nonclass_expr ';'	/* Step + dz. */
		{
		$$ = Create_array(
				Ucscript::step,
				new Uc_binary_expression(
						UC_ADD,
						new Uc_binary_expression(
								UC_MOD, $2,
								new Uc_int_expression(8)),    // dir is 0-7.
						new Uc_int_expression(0x30), UC_PUSHB),
				new Uc_binary_expression(
						UC_MOD, $4,
						new Uc_int_expression(16)));    // Allow max |dz| == 15.
		}
	| STEP direction ';'
		{ $$ = new Uc_int_expression(Ucscript::step_n + $2, UC_PUSHB); }
	| MUSIC nonclass_expr ';'
		{ $$ = Create_array(Ucscript::music, $2, new Uc_int_expression(0)); }
	| MUSIC nonclass_expr ',' nonclass_expr ';'
		{
		// This is the 'repeat' flag.
		Uc_expression* expr;
		int            ival;
		if ($4->eval_const(ival)) {
			expr = new Uc_int_expression(ival ? 1 : 0);
		} else {    // Argh.
			expr = $4;
		}
		$$ = Create_array(Ucscript::music, $2, expr);
		}
	| start_call ';'
		{
		int ival;
		if ($1->eval_const(ival) && ival >= 0 && ival < 0x100) {
			// Matches original usecode.
			$$ = Create_array(Ucscript::usecode, $1, new Uc_int_expression(0));
		} else {
			$$ = Create_array(Ucscript::usecode, $1);
		}
		}
	| start_call ',' nonclass_expr ';'
		{ $$ = Create_array(Ucscript::usecode2, $1, $3); }
	| SPEECH nonclass_expr ';'
		{ $$ = Create_array(Ucscript::speech, $2); }
	| SFX nonclass_expr ';'
		{ $$ = Create_array(Ucscript::sfx, $2); }
	| FACE nonclass_expr ';'
		{ $$ = Create_array(Ucscript::face_dir, $2); }
	| FACE direction ';'
		{ $$ = Create_array(Ucscript::face_dir, new Uc_int_expression($2)); }
	| WEATHER nonclass_expr ';'
		{ $$ = Create_array(Ucscript::weather, $2); }
	| HIT nonclass_expr ',' nonclass_expr ';'
		{ $$ = Create_array(Ucscript::hit, $2, $4); }
	| HIT nonclass_expr ',' dam_type ';'
		{ $$ = Create_array(Ucscript::hit, $2, new Uc_int_expression($4)); }
	| ATTACK ';'
		{ $$ = new Uc_int_expression(Ucscript::attack, UC_PUSHB); }
	| declared_var_value ';'
	| '{' script_command_list '}'
		{ $$ = $2; }
	;

start_call:
	CALL nonclass_expr
		{
		if (!$2) {
			$$ = new Uc_int_expression(0);
		} else {
			// May generate errors.
			if ($2->is_object_function() == -1) {    // Don't know.
				char buf[180];
				snprintf(
						buf, sizeof(buf),
						"Please ensure that 'call' uses a function declared "
						"with 'shape#' or 'object#'");
				yywarning(buf);
			}
			$$ = $2;
		}
		}
	;

repeat_count:
	nonclass_expr
	| FOREVER
		{
		if (repeat_nesting != 0) {
			yyerror("'repeat forever' is not allowed for nested 'repeat' "
					"commands");
		}
		$$ = new Uc_int_expression(255);
		}
	;

dam_type:
	NORMAL_DAMAGE
		{ $$ = 0; }
	| FIRE_DAMAGE
		{ $$ = 1; }
	| MAGIC_DAMAGE
		{ $$ = 2; }
	| LIGHTNING_DAMAGE
		{ $$ = 3; }
	| POISON_DAMAGE
		{ $$ = 3; }
	| STARVATION_DAMAGE
		{ $$ = 3; }
	| FREEZING_DAMAGE
		{ $$ = 3; }
	| ETHEREAL_DAMAGE
		{ $$ = 4; }
	| SONIC_DAMAGE
		{ $$ = 5; }
	;

direction:
	NORTH
		{ $$ = 0; }
	| NE
		{ $$ = 1; }
	| EAST
		{ $$ = 2; }
	| SE
		{ $$ = 3; }
	| SOUTH
		{ $$ = 4; }
	| SW
		{ $$ = 5; }
	| WEST
		{ $$ = 6; }
	| NW
		{ $$ = 7; }
	;

actor_frames:
	STANDING
		{ $$ = 0; }
	| STEP_RIGHT
		{ $$ = 1; }
	| STEP_LEFT
		{ $$ = 2; }
	| READY
		{ $$ = 3; }
	| RAISE_1H
		{ $$ = 4; }
	| REACH_1H
		{ $$ = 5; }
	| STRIKE_1H
		{ $$ = 6; }
	| RAISE_2H
		{ $$ = 7; }
	| REACH_2H
		{ $$ = 8; }
	| STRIKE_2H
		{ $$ = 9; }
	| SITTING
		{ $$ = 10; }
	| BOWING
		{ $$ = 11; }
	| KNEELING
		{ $$ = 12; }
	| SLEEPING
		{ $$ = 13; }
	| CAST_UP
		{ $$ = 14; }
	| CAST_OUT
		{ $$ = 15; }
	;

egg_criteria:
	CACHED_IN
		{ $$ = 0; }
	| PARTY_NEAR
		{ $$ = 1; }
	| AVATAR_NEAR
		{ $$ = 2; }
	| AVATAR_FAR
		{ $$ = 3; }
	| AVATAR_FOOTPAD
		{ $$ = 4; }
	| PARTY_FOOTPAD
		{ $$ = 5; }
	| SOMETHING_ON
		{ $$ = 6; }
	| EXTERNAL_CRITERIA
		{ $$ = 7; }
	;

opt_script_delay:
	AFTER nonclass_expr TICKS
		{ $$ = $2; }
	| %empty
		{ $$ = nullptr; }
	;

break_statement:
	BREAK ';'
		{ $$ = new Uc_break_statement(); }
	;

continue_statement:
	CONTINUE ';'
		{ $$ = new Uc_continue_statement(); }
	;

fallthrough_statement:
	FALLTHROUGH ';'
		{ $$ = new Uc_fallthrough_statement(); }
	;

label_statement:
	IDENTIFIER ':'
		{
		if (cur_fun->search_label($1)) {
			char buf[150];
			snprintf(buf, sizeof(buf), "duplicate label: '%s'", $1);
			yyerror(buf);
			$$ = nullptr;
		} else {
			cur_fun->add_label($1);
			$$ = new Uc_label_statement($1);
		}
		}
	;

goto_statement:
	GOTO IDENTIFIER
		{
		yywarning("You *really* shouldn't using goto statements...");
		$$ = new Uc_goto_statement($2);
		}
	;

delete_statement:
	DELETE declared_var ';'
		{
		auto* cls = dynamic_cast<Uc_class_inst_symbol*>($2->get_sym());
		if (!cls) {
			char buf[150];
			snprintf(buf, sizeof(buf), "'%s' is not a class", $2->get_name());
			yyerror(buf);
			$$ = nullptr;
		} else {
			$$ = new Uc_delete_statement(new Uc_del_expression(cls));
		}
		}
	;

answer_statement:
	ADD '(' nonclass_expr_list ')' ';'
		{
		$$ = new Uc_call_statement(new Uc_call_expression(
				Uc_function::get_add_answer(), $3, cur_fun));
		}
	| REMOVE '(' nonclass_expr_list ')' ';'
		{
		$$ = new Uc_call_statement(new Uc_call_expression(
				Uc_function::get_remove_answer(), $3, cur_fun));
		}
	;

opt_nonclass_expr_list:
	nonclass_expr_list
	| %empty
		{ $$ = new Uc_array_expression(); }
	;

nonclass_expr_list:
	nonclass_expr_list ',' nonclass_expr
		{ $$->add($3); }
	| nonclass_expr
		{
		$$ = new Uc_array_expression();
		$$->add($1);
		}
	;

nonclass_expr:
	expression
		{
		if (Class_unexpected_error($1)) {
			$$ = nullptr;
		} else {
			$$ = $1;
		}
		}
	;

expression:
	primary
		{ $$ = $1; }
	| nonclass_expr '+' nonclass_expr
		{ $$ = new Uc_binary_expression(UC_ADD, $1, $3); }
	| nonclass_expr '-' nonclass_expr
		{ $$ = new Uc_binary_expression(UC_SUB, $1, $3); }
	| nonclass_expr '*' nonclass_expr
		{ $$ = new Uc_binary_expression(UC_MUL, $1, $3); }
	| nonclass_expr '/' nonclass_expr
		{ $$ = new Uc_binary_expression(UC_DIV, $1, $3); }
	| nonclass_expr '%' nonclass_expr
		{ $$ = new Uc_binary_expression(UC_MOD, $1, $3); }
	| nonclass_expr EQUALS nonclass_expr
		{ $$ = new Uc_binary_expression(UC_CMPEQ, $1, $3); }
	| NEW SCRIPT { start_script(); } script_command
		{
		$$ = $4;
		end_script();
		}
	| CHOICE
		{
		if (!converse) {
			/* Only valid in converse blocks */
			yyerror("'CHOICE' can only be used in a conversation block!");
			$$ = nullptr;
		}
		$$ = new Uc_choice_expression();
		}
	| nonclass_expr NEQUALS nonclass_expr
		{ $$ = new Uc_binary_expression(UC_CMPNE, $1, $3); }
	| nonclass_expr '<' nonclass_expr
		{ $$ = new Uc_binary_expression(UC_CMPLT, $1, $3); }
	| nonclass_expr LTEQUALS nonclass_expr
		{ $$ = new Uc_binary_expression(UC_CMPLE, $1, $3); }
	| nonclass_expr '>' nonclass_expr
		{ $$ = new Uc_binary_expression(UC_CMPGT, $1, $3); }
	| nonclass_expr GTEQUALS nonclass_expr
		{ $$ = new Uc_binary_expression(UC_CMPGE, $1, $3); }
	| nonclass_expr AND nonclass_expr
		{ $$ = new Uc_binary_expression(UC_AND, $1, $3); }
	| nonclass_expr OR nonclass_expr
		{ $$ = new Uc_binary_expression(UC_OR, $1, $3); }
	| nonclass_expr UCC_IN nonclass_expr	/* Value in array. */
		{ $$ = new Uc_binary_expression(UC_IN, $1, $3); }
	| nonclass_expr '&' nonclass_expr	/* append arrays */
		{ $$ = new Uc_binary_expression(UC_ARRA, $1, $3); }
	| '+' primary %prec UPLUS
		{
		if (Class_unexpected_error($2)) {
			$$ = nullptr;
		} else {
			$$ = $2;
		}
		}
	| '-' primary %prec UMINUS
		{
		if (Class_unexpected_error($2)) {
			$$ = nullptr;
		} else {
			$$ = new Uc_binary_expression(UC_SUB, new Uc_int_expression(0), $2);
		}
		}
	| addressof
		{
		int        funid = $1;
		UsecodeOps op    = is_int_32bit(funid) ? UC_PUSHI32 : UC_PUSHI;
		$$               = new Uc_int_expression(funid, op);
		}
	| NOT primary
		{ $$ = new Uc_unary_expression(UC_NOT, $2); }
	| '[' opt_expression_list ']'	/* Concat. into an array. */
		{ $$ = $2; }
	| string_literal
		{ $$ = new Uc_string_expression(cur_fun->add_string($1)); }
	| string_prefix
		{ $$ = new Uc_string_prefix_expression(cur_fun, $1); }
	| new_expr
	| run_script_expression
		{ $$ = $1; }
	;

addressof:
	'&' IDENTIFIER %prec ADDRESSOF
		{
		// A way to retrieve the function's assigned
		// usecode number
		Uc_symbol* sym = cur_fun->search_up($2);
		if (!sym) {
			/* See if the symbol is defined */
			char buf[150];
			snprintf(buf, sizeof(buf), "'%s' not declared", $2);
			yyerror(buf);
			$$ = -1;
		}
		auto* fun = dynamic_cast<Uc_function_symbol*>(sym);
		if (!fun) {
			/* See if the symbol is a function */
			char buf[150];
			snprintf(buf, sizeof(buf), "'%s' is not a function", $2);
			yyerror(buf);
			$$ = -1;
		} else {
			/* Output the function's assigned number */
			$$ = fun->get_usecode_num();
		}
		}
	| '&' STRUCT '<' defined_struct '>' UCC_SCOPE IDENTIFIER
		{
		auto* sym = $4;
		if (sym) {
			int value = sym->search($7);
			if (value < 0) {
				char buf[150];
				snprintf(
						buf, sizeof(buf),
						"'struct<%s>::%s' is not a valid structure member",
						$4->get_name(), $7);
				yyerror(buf);
			}
			$$ = value;
		} else {
			$$ = -1;
		}
		}
	;

opt_expression_list:
	expression_list opt_comma
	| %empty
		{ $$ = new Uc_array_expression(); }
	;

expression_list:
	expression_list ',' expression
		{ $$->add($3); }
	| expression
		{
		$$ = new Uc_array_expression();
		$$->add($1);
		}
	;

int_cast:
	'(' const_int_type ')'
		{ $$ = $2; }
	;

primary:
	INT_LITERAL
		{
		UsecodeOps op = !const_opcode.empty() ? const_opcode.back() : UC_PUSHI;
		if (is_sint_32bit($1) && op != UC_PUSHI32) {
			char buf[150];
			if (is_int_32bit($1)) {
				snprintf(
						buf, sizeof(buf),
						"Literal integer '%d' cannot be represented as 16-bit "
						"integer. Assuming '(long)' cast.",
						$1);
				op = UC_PUSHI32;
			} else {
				snprintf(
						buf, sizeof(buf),
						"Interpreting integer '%d' as the signed 16-bit "
						"integer '%d'. If this is incorrect, use '(long)' "
						"cast.",
						$1, static_cast<short>($1));
			}
			yywarning(buf);
		}
		$$ = new Uc_int_expression($1, op);
		}
	| int_cast INT_LITERAL %prec UCC_CAST
		{ $$ = new Uc_int_expression($2, static_cast<UsecodeOps>($1)); }
	| member_selector
		{
		auto*             expr = dynamic_cast<Uc_var_expression*>($1->expr);
		Uc_struct_symbol* base;
		if (!expr || !(base = expr->get_struct())) {
			yyerror("Expression is not a 'struct'");
			$$ = new Uc_int_expression(0);
		} else {
			int offset = base->search($1->name);
			if (offset < 0) {
				char buf[150];
				snprintf(
						buf, sizeof(buf), "'%s' does not belong to struct '%s'",
						$1->name, base->get_name());
				yyerror(buf);
				$$ = new Uc_int_expression(0);
			} else {
				auto* var   = expr->get_var();
				auto* index = new Uc_int_expression(offset);
				if (var->is_static()) {
					$$ = new Uc_static_arrayelem_expression(var, index);
				} else if (var->get_sym_type() == Uc_symbol::Member_var) {
					$$ = new Uc_class_arrayelem_expression(var, index);
				} else {
					$$ = new Uc_arrayelem_expression(var, index);
				}
			}
		}
		delete $1;
		}
	| declared_var_value
		{ $$ = $1; }
	| declared_var '[' expression ']'
		{
		if ($1->get_cls()) {
			char buf[150];
			snprintf(
					buf, sizeof(buf), "Can't convert class '%s' into non-class",
					$1->get_name());
			yyerror(buf);
			$$ = new Uc_arrayelem_expression($1, $3);
		} else if ($1->is_static()) {
			$$ = new Uc_static_arrayelem_expression($1, $3);
		} else if ($1->get_sym_type() == Uc_symbol::Member_var) {
			$$ = new Uc_class_arrayelem_expression($1, $3);
		} else {
			$$ = new Uc_arrayelem_expression($1, $3);
		}
		}
	| FLAG '[' nonclass_expr ']'
		{ $$ = new Uc_flag_expression($3); }
	| function_call
		{
		if ($1) {
			$$ = $1;
		} else {
			$$ = new Uc_int_expression(0);
		}
		}
	| UCTRUE
		{ $$ = new Uc_bool_expression(true); }
	| UCFALSE
		{ $$ = new Uc_bool_expression(false); }
	| EVENT
		{ $$ = new Uc_event_expression(); }
	| ITEM
		{ $$ = new Uc_item_expression(); }
	| '(' expression ')'
		{ $$ = $2; }
	;

new_expr:
	NEW defined_class '(' opt_nonclass_expr_list ')'
		{
		if ($2) {
			$$ = new Uc_new_expression(new Uc_class_inst_symbol("", $2, 0), $4);
		} else {
			$$ = new Uc_int_expression(0);
		}
		}
	;

hierarchy_tok:
	UCC_POINTS
	| '.'
	;

member_selector:
	primary hierarchy_tok IDENTIFIER
		{ $$ = new Member_selector($1, $3); }
	;

opt_primary_expression:
	primary hierarchy_tok
		{ $$ = $1; }
	| %empty
		{ $$ = new Uc_item_expression(); }

function_call:
	member_selector opt_original '(' opt_expression_list ')'
		{
		$$ = cls_function_call($1->expr, cur_class, $1->name, $2, $4);
		delete $1;
		}
	| IDENTIFIER opt_original '(' opt_expression_list ')'
		{
		$$ = cls_function_call(nullptr, cur_class, $1, $2, $4);
		}
	| primary hierarchy_tok defined_class UCC_SCOPE IDENTIFIER '(' opt_expression_list ')'
		{
		$$ = cls_method_call($1, $1->get_cls(), $3, $5, $7);
		}
	| defined_class UCC_SCOPE IDENTIFIER '(' opt_expression_list ')'
		{
		$$ = cls_method_call(nullptr, cur_class, $1, $3, $5);
		}
	| primary hierarchy_tok '(' '*' primary ')' '(' opt_expression_list ')'
		{
		$$ = new Uc_call_expression($5, $8, cur_fun);
		$$->set_itemref($1);
		}
	| '(' '*' primary ')' '(' opt_expression_list ')'
		{
		$$ = new Uc_call_expression($3, $6, cur_fun);
		$$->set_itemref(nullptr);
		}
	| primary hierarchy_tok '(' '@' int_literal ')' '(' opt_expression_list ')'
		{
		int num;
		if (!$5->eval_const(num)) {
			yyerror("Failed to obtain value from integer constant");
			$$ = nullptr;
		} else {
			$$ = new Uc_call_expression(
					Uc_function::get_intrinsic(num), $8, cur_fun);
			$$->set_itemref($1);
			$$->check_params();
		}
		}
	| '(' '@' int_literal ')' '(' opt_expression_list ')'
		{
		int num;
		if (!$3->eval_const(num)) {
			yyerror("Failed to obtain value from integer constant");
			$$ = nullptr;
		} else {
			$$ = new Uc_call_expression(
					Uc_function::get_intrinsic(num), $6, cur_fun);
			$$->check_params();
		}
		}
	;

opt_original:
	ORIGINAL
		{ $$ = 1; }
	| %empty
		{ $$ = 0; }
	;

opt_param_list:
	param_list
	| %empty
		{ $$ = new std::vector<Uc_var_symbol *>; }
	;

param_list:
	param_list ',' param
		{ $1->push_back($3); }
	| param
		{
		$$ = new std::vector<Uc_var_symbol *>;
		$$->push_back($1);
		}
	;

param:
	IDENTIFIER
		{ $$ = new Uc_var_symbol($1, 0); }
	| VAR IDENTIFIER
		{ $$ = new Uc_var_symbol($2, 0); }
	| STRUCT '<' defined_struct '>' IDENTIFIER
		{ $$ = new Uc_struct_var_symbol($5, $3, 0); }
	| CLASS '<' defined_class '>' IDENTIFIER
		{ $$ = new Uc_class_inst_symbol($5, $3, 0); }
	;

sign_int_literal:
	INT_LITERAL
	| '-' INT_LITERAL %prec UMINUS
		{ $$ = -$2; }
	| '+' INT_LITERAL %prec UPLUS
		{ $$ = $2; }
	;

int_literal:				/* A const. integer value.	*/
	sign_int_literal
		{
		UsecodeOps op = !const_opcode.empty() ? const_opcode.back() : UC_PUSHI;
		if (is_sint_32bit($1) && op != UC_PUSHI32) {
			char buf[150];
			if (is_int_32bit($1)) {
				snprintf(
						buf, sizeof(buf),
						"Literal integer '%d' cannot be represented as 16-bit "
						"integer. Assuming '(long)' cast.",
						$1);
				op = UC_PUSHI32;
			} else {
				snprintf(
						buf, sizeof(buf),
						"Interpreting integer '%d' as the signed 16-bit "
						"integer '%d'. If this is incorrect, use '(long)' "
						"cast.",
						$1, static_cast<short>($1));
			}
			yywarning(buf);
		}
		$$ = new Uc_int_expression($1, op);
		}
	| int_cast sign_int_literal %prec UCC_CAST
		{ $$ = new Uc_int_expression($2, static_cast<UsecodeOps>($1)); }
	| declared_sym
		{
		auto* sym = dynamic_cast<Uc_const_int_symbol*>($1);
		if (!sym) {
			char buf[150];
			snprintf(
					buf, sizeof(buf), "'%s' is not a const int",
					$1->get_name());
			yyerror(buf);
			$$ = nullptr;
		} else {
			$$ = sym->create_expression();
		}
		}
	| UCTRUE
		{ $$ = new Uc_bool_expression(true); }
	| UCFALSE
		{ $$ = new Uc_bool_expression(false); }
	;

opt_void:
	VOID
	| %empty
		{
		yywarning("You should prepend 'void' for functions that do not return "
				  "a value.");
		}
	;

ret_type:
	VAR
		{ has_ret = true; }
	| STRUCT '<' defined_struct '>'
		{ struct_type = $3; }
	| opt_void
		{ has_ret = false; }
	;

declared_var_value:
	declared_sym %prec UCC_SYM
		{
		$$ = $1->create_expression();
		if (!$$) {
			char buf[150];
			snprintf(buf, sizeof(buf), "Can't use '%s' here", $1->get_name());
			yyerror(buf);
			$$ = new Uc_int_expression(0);
		}
		}
	;

declared_var:
	declared_sym %prec UCC_SYM
		{
		auto* var = dynamic_cast<Uc_var_symbol*>($1);
		if (!var) {
			char buf[150];
			snprintf(buf, sizeof(buf), "'%s' not a 'var'", $1->get_name());
			yyerror(buf);
			snprintf(buf, sizeof(buf), "%s_needvar", $1->get_name());
			var = cur_fun->add_symbol(buf, false);
		}
		$$ = var;
		}
	;

declared_sym:
	IDENTIFIER
		{
		Uc_symbol* sym = cur_fun->search_up($1);
		if (!sym) {
			char buf[150];
			snprintf(buf, sizeof(buf), "'%s' not declared", $1);
			yyerror(buf);
			sym = cur_fun->add_symbol($1, false);
		}
		$$ = sym;
		}
	;

defined_class:
	IDENTIFIER
		{ $$ = Find_class($1); }
	;

defined_struct:
	IDENTIFIER
		{
		auto* sym = dynamic_cast<Uc_struct_symbol*>(
				Uc_function::search_globals($1));
		if (!sym) {
			char buf[150];
			snprintf(
					buf, sizeof(buf), "'%s' not found, or is not a struct.",
					$1);
			yyerror(buf);
			$$ = nullptr;
		} else {
			$$ = sym;
		}
		}
	;

%%

#ifdef __GNUC__
#	pragma GCC diagnostic pop
#endif    // __GNUC__

static Uc_var_symbol* Get_variable(const char* ident) {
	Uc_symbol* sym = cur_fun->search_up(ident);
	auto*      var = dynamic_cast<Uc_var_symbol*>(sym);
	if (!var) {
		var = cur_fun->add_symbol(ident, true);
	}
	return var;
}

/*
 *	Create an array with an integer as the first element.
 */

static Uc_array_expression* Create_array(int e1, Uc_expression* e2) {
	auto* arr = new Uc_array_expression();
	arr->add(new Uc_int_expression(e1, UC_PUSHB));
	arr->add(e2);
	return arr;
}

static Uc_array_expression* Create_array(
		int e1, Uc_expression* e2, Uc_expression* e3) {
	auto* arr = new Uc_array_expression();
	arr->add(new Uc_int_expression(e1, UC_PUSHB));
	arr->add(e2);
	arr->add(e3);
	return arr;
}

static Uc_class* Find_class(const char* nm) {
	auto* csym
			= dynamic_cast<Uc_class_symbol*>(Uc_function::search_globals(nm));
	if (!csym) {
		char buf[150];
		snprintf(buf, sizeof(buf), "'%s' not found, or is not a class.", nm);
		yyerror(buf);
		return nullptr;
	}
	return csym->get_cls();
}

static bool Class_unexpected_error(Uc_expression* expr) {
	if (expr->is_class()) {
		yyerror("Can't convert class into non-class");
		return true;
	}
	return false;
}

static bool Nonclass_unexpected_error(Uc_expression* expr) {
	if (!expr->is_class()) {
		yyerror("Can't convert non-class into class.");
		return true;
	}
	return false;
}

static bool Incompatible_classes_error(Uc_class* src, Uc_class* trg) {
	if (!src->is_class_compatible(trg->get_name())) {
		char buf[180];
		snprintf(
				buf, sizeof(buf),
				"Class '%s' can't be converted into class '%s'",
				src->get_name(), trg->get_name());
		yyerror(buf);
		return true;
	}
	return false;
}

static Uc_call_expression* cls_method_call(
		Uc_expression* ths, Uc_class* curcls, Uc_class* clsscope, char* nm,
		Uc_array_expression* parms) {
	if (!curcls) {
		char buf[150];
		snprintf(buf, sizeof(buf), "'%s' requires a class object", nm);
		yyerror(buf);
		return nullptr;
	}

	if (Incompatible_classes_error(curcls, clsscope)) {
		return nullptr;
	}

	Uc_symbol* sym = clsscope->get_scope()->search(nm);
	if (!sym) {
		char buf[150];
		snprintf(
				buf, sizeof(buf), "Function '%s' is not declared in class '%s'",
				nm, clsscope->get_name());
		yyerror(buf);
		return nullptr;
	}

	auto* fun = dynamic_cast<Uc_function_symbol*>(sym);
	if (!fun) {
		char buf[150];
		snprintf(buf, sizeof(buf), "'%s' is not a function", nm);
		yyerror(buf);
		return nullptr;
	}

	auto* ret = new Uc_call_expression(sym, parms, cur_fun, false);
	ret->set_itemref(ths);
	ret->set_call_scope(clsscope);
	ret->check_params();
	return ret;
}

static bool Uc_is_valid_calle(
		Uc_symbol* sym, Uc_expression*& ths, char* nm, bool original) {
	if (original) {
		return true;
	}
	auto* fun = dynamic_cast<Uc_function_symbol*>(sym);
	if (!fun) {    // Most likely an intrinsic.
		return true;
	}

	if (fun->get_function_type() == Uc_function_symbol::utility_fun) {
		if (ths && !ths->is_class()) {
			char buf[150];
			snprintf(
					buf, sizeof(buf), "'%s' is not an object or shape function",
					nm);
			yyerror(buf);
			return false;
		} else if (ths) {
			char buf[150];
			snprintf(
					buf, sizeof(buf), "'%s' is not a member of class '%s'", nm,
					ths->get_cls()->get_name());
			yyerror(buf);
			return false;
		}
	} else {
		if (!ths) {
			char buf[180];
			snprintf(
					buf, sizeof(buf),
					"'%s' expects an itemref, but none was supplied; using "
					"current itemref",
					nm);
			ths = new Uc_item_expression();
			yywarning(buf);
			return true;
		}
	}
	return true;
}

static Uc_call_expression* cls_function_call(
		Uc_expression* ths, Uc_class* curcls, char* nm, bool original,
		Uc_array_expression* parms) {
	Uc_symbol* sym = nullptr;
	// Check class methods first.
	if (!ths && curcls) {
		sym = curcls->get_scope()->search(nm);
	} else if (ths && ths->is_class()) {
		sym = ths->get_cls()->get_scope()->search(nm);
	}

	// Search for defined functions.
	if (!sym) {
		sym = cur_fun->search_up(nm);
		if (original && !ths) {
			ths = new Uc_item_expression();
		}
		if (!Uc_is_valid_calle(sym, ths, nm, original)) {
			return nullptr;
		}
	}

	// Check for intrinsic name.
	if (!sym) {
		string iname = string("UI_") + nm;
		sym          = cur_fun->search_up(iname.c_str());
		// Treat as method call on 'item'.
		if (sym && !ths) {
			ths = new Uc_item_expression();
		}
	}

	// UI_UNKNOWN_XX
	if (!sym) {
		string       name(nm);
		const string uiUnk("UI_UNKNOWN_");
		if (name.size() == uiUnk.size() + 2) {
			string num("0x");
			num += name.substr(uiUnk.size(), 2);
			if (name.substr(0, uiUnk.size()) == uiUnk) {
				try {
					auto val = std::stoul(num, nullptr, 0);
					sym      = Uc_function::get_intrinsic(val);
				} catch (std::exception&) {
					sym = nullptr;
				}
			}
		}
	}

	if (!sym) {
		char buf[150];
		snprintf(buf, sizeof(buf), "'%s' not declared", nm);
		yyerror(buf);
		return nullptr;
	} else {
		auto* ret = new Uc_call_expression(sym, parms, cur_fun, original);
		ret->set_itemref(ths);
		ret->check_params();
		return ret;
	}
}