File: Expression.cs

package info (click to toggle)
mono-debugger 0.60%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 9,556 kB
  • ctags: 22,787
  • sloc: ansic: 99,407; cs: 42,429; sh: 9,192; makefile: 376
file content (3125 lines) | stat: -rw-r--r-- 79,165 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
using System;
using System.Text;
using System.IO;
using System.Reflection;
using System.Collections;
using System.Globalization;
using Mono.Debugger;
using Mono.Debugger.Languages;

namespace Mono.Debugger.Frontend
{
	public class ExpressionParser : IExpressionParser
	{
		public readonly Interpreter Interpreter;

		private readonly CSharp.ExpressionParser parser;

		internal ExpressionParser (Interpreter interpreter)
		{
			this.Interpreter = interpreter;

			parser = new CSharp.ExpressionParser ("C#");
		}

		public Expression Parse (string expression)
		{
			return parser.Parse (expression);
		}

		protected static SourceLocation FindFile (Thread target, string filename, int line)
		{
			SourceFile file = target.Process.FindFile (filename);
			if (file == null)
				throw new ScriptingException ("Cannot find source file `{0}'.",
							      filename);

			MethodSource source = file.FindMethod (line);
			if (source == null)
				throw new ScriptingException (
					"Cannot find method corresponding to line {0} in `{1}'.",
					line, file.Name);

			return new SourceLocation (source, file, line);
		}

		protected SourceLocation DoParseExpression (Thread target, StackFrame frame,
							    LocationType type, string arg)
		{
			ScriptingContext context = new ScriptingContext (Interpreter);
			context.CurrentThread = frame.Thread;
			context.CurrentFrame = frame;

			Expression expr = Interpreter.ExpressionParser.Parse (arg);
			if (expr == null)
				throw new ScriptingException ("Cannot resolve expression `{0}'.", arg);

			MethodExpression mexpr = expr.ResolveMethod (context, type);

			if (mexpr != null)
				return mexpr.EvaluateSource (context);
			else
				return context.FindMethod (arg);
		}

		public static bool ParseLocation (Thread target, StackFrame frame,
						  string arg, out SourceLocation location)
		{
			int line;
			int pos = arg.IndexOf (':');
			if (pos >= 0) {
				string filename = arg.Substring (0, pos);
				try {
					line = (int) UInt32.Parse (arg.Substring (pos+1));
				} catch {
					throw new ScriptingException ("Expected filename:line");
				}

				location = FindFile (frame.Thread, filename, line);
				return true;
			}

			try {
				line = (int) UInt32.Parse (arg);
			} catch {
				location = null;
				return false;
			}

			if ((frame == null) || (frame.SourceLocation == null) ||
			    (frame.SourceLocation.FileName == null))
				throw new ScriptingException (
					"Current stack frame doesn't have source code");

			location = FindFile (frame.Thread, frame.SourceLocation.FileName, line);
			return true;
		}

		protected SourceLocation DoParse (Thread target, StackFrame frame,
						  LocationType type, string arg)
		{
			if (type != LocationType.Default)
				return DoParseExpression (target, frame, type, arg);

			SourceLocation location;
			if (ParseLocation (target, frame, arg, out location))
				return location;

			return DoParseExpression (target, frame, type, arg);
		}

		public SourceLocation Parse (Thread target, StackFrame frame,
					     LocationType type, string arg)
		{
			try {
				return DoParse (target, frame, type, arg);
			} catch (ScriptingException ex) {
				throw new TargetException (TargetError.LocationInvalid, ex.Message);
			}
		}

		public string EvaluateExpression (ScriptingContext context, string text,
						  DisplayFormat format)
		{
			Expression expression;

			try {
				expression = Interpreter.ExpressionParser.Parse (text);
			} catch (ScriptingException ex) {
				throw new ScriptingException ("Cannot parse expression `{0}': {1}.",
							      text, ex.Message);
			} catch {
				throw new ScriptingException ("Cannot parse expression `{0}'.", text);
			}

			try {
				expression = expression.Resolve (context);
			} catch (ScriptingException ex) {
				throw new ScriptingException ("Cannot resolve expression `{0}': {1}",
							      text, ex.Message);
			} catch {
				throw new ScriptingException ("Cannot resolve expression `{0}'.", text);
			}

			try {
				object retval = expression.Evaluate (context);
				return context.FormatObject (retval, format);
			} catch (ScriptingException ex) {
				throw new ScriptingException ("Cannot evaluate expression `{0}': {1}",
							      text, ex.Message);
			} catch {
				throw new ScriptingException ("Cannot evaluate expression `{0}'.", text);
			}
		}
	}

	public abstract class Expression
	{
		public abstract string Name {
			get;
		}

		protected bool resolved;

		protected virtual TargetType DoEvaluateType (ScriptingContext context)
		{
			return EvaluateObject (context).Type;
		}

		public TargetType EvaluateType (ScriptingContext context)
		{
			if (!resolved)
				throw new InvalidOperationException (
					String.Format (
						"Some clown tried to evaluate the " +
						"unresolved expression `{0}'", Name));

			try {
				TargetType type = DoEvaluateType (context);
				if (type == null)
					throw new ScriptingException (
						"Cannot get type of expression `{0}'", Name);

				return type;
			} catch (LocationInvalidException ex) {
				throw new ScriptingException (
					"Location of variable `{0}' is invalid: {1}",
					Name, ex.Message);
			}
		}

		protected virtual object DoEvaluate (ScriptingContext context)
		{
			return DoEvaluateObject (context);
		}

		public object Evaluate (ScriptingContext context)
		{
			if (!resolved)
				throw new InvalidOperationException (
					String.Format (
						"Some clown tried to evaluate the " +
						"unresolved expression `{0}'", Name));

			object result = DoEvaluate (context);
			if (result == null)
				throw new ScriptingException (
					"Cannot evaluate expression `{0}'", Name);

			return result;
		}

		protected virtual TargetVariable DoEvaluateVariable (ScriptingContext context)
		{
			return null;
		}

		public TargetVariable EvaluateVariable (ScriptingContext context)
		{
			if (!resolved)
				throw new InvalidOperationException (
					String.Format (
						"Some clown tried to evaluate the " +
						"unresolved expression `{0}' ({1})", Name,
						GetType ()));

			try {
				TargetVariable retval = DoEvaluateVariable (context);
				if (retval == null)
					throw new ScriptingException (
						"Expression `{0}' is not a variable", Name);

				return retval;
			} catch (LocationInvalidException ex) {
				throw new ScriptingException (
					"Location of variable `{0}' is invalid: {1}",
					Name, ex.Message);
			}
		}

		protected virtual TargetObject DoEvaluateObject (ScriptingContext context)
		{
			TargetVariable var = DoEvaluateVariable (context);
			if (var == null)
				return null;

			TargetObject obj = var.GetObject (context.CurrentFrame);
			if (obj == null)
				return null;

			TargetClassObject cobj = obj as TargetClassObject;
			if (cobj != null) {
				TargetObject current;
				try {
					current = cobj.GetCurrentObject (context.CurrentThread);
				} catch {
					current = null;
				}
				if (current != null)
					return current;
			}

			return obj;
		}

		public TargetObject EvaluateObject (ScriptingContext context)
		{
			if (!resolved)
				throw new InvalidOperationException (
					String.Format (
						"Some clown tried to evaluate the " +
						"unresolved expression `{0}' ({1})", Name,
						GetType ()));

			try {
				TargetObject retval = DoEvaluateObject (context);
				if (retval == null)
					throw new ScriptingException (
						"Expression `{0}' is not a variable", Name);

				return retval;
			} catch (LocationInvalidException ex) {
				throw new ScriptingException (
					"Location of variable `{0}' is invalid: {1}",
					Name, ex.Message);
			}
		}

		protected virtual TargetFunctionType DoEvaluateMethod (ScriptingContext context,
								       LocationType type,
								       Expression[] types)
		{
			return null;
		}

		public TargetFunctionType EvaluateMethod (ScriptingContext context,
							  LocationType type, Expression [] types)
		{
			if (!resolved)
				throw new InvalidOperationException (
					String.Format (
						"Some clown tried to evaluate the " +
						"unresolved expression `{0}'", Name));

			try {
				TargetFunctionType func = DoEvaluateMethod (context, type, types);
				if (func == null)
					throw new ScriptingException (
						"Expression `{0}' is not a method", Name);

				return func;
			} catch (LocationInvalidException ex) {
				throw new ScriptingException (
					"Location of variable `{0}' is invalid: {1}",
					Name, ex.Message);
			}
		}

		protected virtual bool DoAssign (ScriptingContext context, TargetObject obj)
		{
			return false;
		}

		public void Assign (ScriptingContext context, TargetObject obj)
		{
			if (!resolved)
				throw new InvalidOperationException (
					String.Format (
						"Some clown tried to evaluate the " +
						"unresolved expression `{0}'", Name));

			bool ok = DoAssign (context, obj);
			if (!ok)
				throw new ScriptingException (
					"Expression `{0}' is not an lvalue", Name);
		}

		protected virtual Expression DoResolveType (ScriptingContext context)
		{
			return null;
		}

		public Expression ResolveType (ScriptingContext context)
		{
			Expression expr = DoResolveType (context);
			if (expr == null)
				throw new ScriptingException (
					"Expression `{0}' is not a type.", Name);

			return expr;
		}

		public Expression TryResolveType (ScriptingContext context)
		{
			try {
				return DoResolveType (context);
			} catch (ScriptingException) {
				return null;
			} catch (TargetException) {
				return null;
			}
		}

		protected abstract Expression DoResolve (ScriptingContext context);

		public Expression Resolve (ScriptingContext context)
		{
			Expression expr = DoResolve (context);
			if (expr == null)
				throw new ScriptingException (
					"Expression `{0}' is not a variable or value.", Name);

			return expr;
		}

		protected virtual MethodExpression DoResolveMethod (ScriptingContext context,
								    LocationType type)
		{
			return null;
		}

		public MethodExpression ResolveMethod (ScriptingContext context, LocationType type)
		{
			MethodExpression expr = DoResolveMethod (context, type);
			if (expr == null)
				throw new ScriptingException (
					"Expression `{0}' is not a method.", Name);

			return expr;
		}

		public Expression TryResolve (ScriptingContext context)
		{
			try {
				return DoResolve (context);
			} catch (ScriptingException) {
				return null;
			} catch (TargetException) {
				return null;
			}
		}

		public override string ToString ()
		{
			return String.Format ("{0} ({1})", GetType (), Name);
		}
	}

	public class NumberExpression : PointerExpression
	{
		object val;

		public NumberExpression (int val)
		{
			this.val = val;
		}

		public NumberExpression (uint val)
		{
			this.val = val;
		}

		public NumberExpression (long val)
		{
			this.val = val;
		}

		public NumberExpression (ulong val)
		{
			this.val = val;
		}

		public NumberExpression (float val)
		{
			this.val = val;
		}

		public NumberExpression (double val)
		{
			this.val = val;
		}

		public NumberExpression (decimal val)
		{
			this.val = val;
		}

		public long Value {
			get {
				if (val is int)
					return (long) (int) val;
				else if (val is uint)
					return (long) (uint) val;
				else if (val is ulong)
					return (long) (ulong) val;
				else
					return (long) val;
			}
		}

		public override string Name {
			get {
				if (val is long)
					return String.Format ("0x{0:x}", (long) val);
				else
					return val.ToString ();
			}
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
			resolved = true;
			return this;
		}

		protected override TargetObject DoEvaluateObject (ScriptingContext context)
		{
			StackFrame frame = context.CurrentFrame;
			if ((frame.Language == null) ||
			    !frame.Language.CanCreateInstance (val.GetType ()))
				throw new ScriptingException ("Cannot instantiate value '{0}' in the current frame's language", Name);

			return frame.Language.CreateInstance (frame.Thread, val);
		}

		public override TargetAddress EvaluateAddress (ScriptingContext context)
		{
			return new TargetAddress (context.AddressDomain, Value);
		}

		protected override object DoEvaluate (ScriptingContext context)
		{
			return val;
		}

		public override string ToString ()
		{
			return Name;
		}
	}

	public class StringExpression : Expression
	{
		string val;

		public StringExpression (string val)
		{
			this.val = val;
		}

		public override string Name {
			get { return '"' + val + '"'; }
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
			resolved = true;
			return this;
		}

		protected override object DoEvaluate (ScriptingContext context)
		{
			return val;
		}

		protected override TargetObject DoEvaluateObject (ScriptingContext context)
		{
			StackFrame frame = context.CurrentFrame;
			if ((frame.Language == null) ||
			    !frame.Language.CanCreateInstance (typeof (string)))
				throw new ScriptingException ("Cannot instantiate value '{0}' in the current frame's language", Name);

			return frame.Language.CreateInstance (frame.Thread, val);
		}

		public override string ToString ()
		{
			return Name;
		}
	}

	public class BoolExpression : Expression
	{
		bool val;

		public BoolExpression (bool val)
		{
			this.val = val;
		}

		public override string Name {
			get { return val.ToString(); }
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
			resolved = true;
			return this;
		}

		protected override object DoEvaluate (ScriptingContext context)
		{
			return val;
		}

		protected override TargetObject DoEvaluateObject (ScriptingContext context)
		{
			StackFrame frame = context.CurrentFrame;
			if ((frame.Language == null) ||
			    !frame.Language.CanCreateInstance (typeof (bool)))
				throw new ScriptingException ("Cannot instantiate value '{0}' in the current frame's language", Name);

			return frame.Language.CreateInstance (frame.Thread, val);
		}

		public override string ToString ()
		{
			return Name;
		}
	}

	public class NullExpression : Expression
	{
		public override string Name {
			get { return "null"; }
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
			resolved = true;
			return this;
		}

		protected override object DoEvaluate (ScriptingContext context)
		{
			return null;
		}

		protected override TargetObject DoEvaluateObject (ScriptingContext context)
		{
			throw new InvalidOperationException ();
		}

		public override string ToString ()
		{
			return Name;
		}
	}

	public class ArgumentExpression : Expression
	{
		TargetObject obj;

		public ArgumentExpression (TargetObject obj)
		{
			this.obj = obj;
			resolved = true;
		}

		public override string Name {
			get { return obj.ToString(); }
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
			return this;
		}

		protected override TargetObject DoEvaluateObject (ScriptingContext context)
		{
			return obj;
		}

		public override string ToString ()
		{
			return Name;
		}
	}

	public class ThisExpression : Expression
	{
		public override string Name {
			get { return "this"; }
		}

		protected StackFrame frame;
		protected TargetVariable var;

		protected override Expression DoResolve (ScriptingContext context)
		{
			frame = context.CurrentFrame;
			Method method = frame.Method;
			if (method == null)
				throw new ScriptingException (
					"Keyword `this' not allowed: no current method.");

			if (!method.HasThis)
				throw new ScriptingException (
					"Keyword `this' not allowed: current method is " +
					"either static or unmanaged.");

			var = method.GetThis (context.CurrentThread);
			resolved = true;
			return this;
		}

		protected override TargetVariable DoEvaluateVariable (ScriptingContext context)
		{
			return var;
		}
	}

	public class BaseExpression : ThisExpression
	{
		public override string Name {
			get { return "base"; }
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
			Expression expr = base.DoResolve (context);
			if (expr == null)
				return null;

			if (var.Type.Kind != TargetObjectKind.Class)
				throw new ScriptingException (
					"`base' is only allowed in a class.");
			if (!((TargetClassType) var.Type).HasParent)
				throw new ScriptingException (
					"Current class has no base class.");

			return expr;
		}

		protected override TargetObject DoEvaluateObject (ScriptingContext context)
		{
			TargetVariable var = DoEvaluateVariable (context);
			if (var == null)
				return null;

			TargetObject obj = var.GetObject (context.CurrentFrame);
			if (obj == null)
				return null;

			TargetClassObject cobj = (TargetClassObject) obj;
			return cobj.GetParentObject (context.CurrentThread);
		}
	}

	public class CatchExpression : Expression
	{
		public override string Name {
			get { return "catch"; }
		}

		TargetObject exc;

		protected override Expression DoResolve (ScriptingContext context)
		{
			exc = context.CurrentFrame.ExceptionObject;
			if (exc == null)
				throw new ScriptingException ("No current exception.");

			resolved = true;
			return this;
		}

		protected override TargetObject DoEvaluateObject (ScriptingContext context)
		{
			return exc;
		}
	}

	public abstract class TypeExpr : Expression
	{
		public abstract TargetType Type {
			get;
		}

		protected override TargetType DoEvaluateType (ScriptingContext context)
		{
			return Type;
		}

		protected override object DoEvaluate (ScriptingContext context)
		{
			return Type;
		}
	}

	public class TypeExpression : TypeExpr
	{
		TargetType type;

		public TypeExpression (TargetType type)
		{
			this.type = type;
			resolved = true;
		}

		public override string Name {
			get { return type.Name; }
		}

		public override TargetType Type {
			get { return type; }
		}

		protected override Expression DoResolveType (ScriptingContext context)
		{
			return this;
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
			return this;
		}
	}

	public class VariableAccessExpression : Expression
	{
		TargetVariable var;

		public VariableAccessExpression (TargetVariable var)
		{
			this.var = var;
			resolved = true;
		}

		public override string Name {
			get { return var.Name; }
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
			resolved = true;
			return this;
		}

		protected override TargetType DoEvaluateType (ScriptingContext context)
		{
			return var.Type;
		}

		protected override TargetVariable DoEvaluateVariable (ScriptingContext context)
		{
			return var;
		}

		protected override bool DoAssign (ScriptingContext context, TargetObject obj)
		{
			if (!var.CanWrite)
				return false;

			TargetObject new_obj = Convert.ImplicitConversionRequired (
				context, obj, var.Type);

			var.SetObject (context.CurrentFrame, new_obj);
			return true;
		}
	}

	public class SourceExpression : Expression
	{
		SourceLocation location;

		public SourceExpression (SourceLocation location)
		{
			this.location = location;
			resolved = true;
		}

		public override string Name {
			get { return location.Name; }
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
			resolved = true;
			return this;
		}
	}

	public class SimpleNameExpression : Expression
	{
		string name;

		public SimpleNameExpression (string name)
		{
			this.name = name;
		}

		public override string Name {
			get { return name; }
		}

                public static string MakeFQN (string nsn, string name)
                {
                        if (nsn == "")
                                return name;
                        return String.Concat (nsn, ".", name);
                }

		TargetVariable GetVariableByName (StackFrame frame, string name)
		{
			TargetVariable[] locals = frame.Method.GetLocalVariables (frame.Thread);
			foreach (TargetVariable var in locals) {
				if ((var.Name == name) && var.IsInScope (frame.TargetAddress))
					return var;
			}

			TargetVariable[] param_vars = frame.Method.GetParameters (frame.Thread);
			foreach (TargetVariable var in param_vars) {
				if ((var.Name == name) && var.IsInScope (frame.TargetAddress))
					return var;
			}

			return null;
		}

		MemberExpression LookupMember (ScriptingContext context, StackFrame frame,
					       string full_name)
		{
			MemberExpression member;

			TargetFunctionType function = frame.Function;
			if (function != null) {
				member = StructAccessExpression.FindMember (
					frame.Thread, function.DeclaringType, null,
					full_name, true, true);
				if (member != null)
					return member;
			}

			Method method = frame.Method;
			if (method == null)
				return null;

			TargetClassType decl_type = method.GetDeclaringType (context.CurrentThread);
			if (decl_type == null)
				return null;

			TargetClassObject instance = null;
			if (method.HasThis) {
				TargetVariable this_var = method.GetThis (context.CurrentThread);
				instance = (TargetClassObject) this_var.GetObject (frame);
			}

			member = StructAccessExpression.FindMember (
				context.CurrentThread, decl_type, instance, full_name, true, true);
			if (member == null)
				return null;

			return member;
		}

		MemberExpression Lookup (ScriptingContext context, StackFrame frame)
		{
			MemberExpression member = LookupMember (context, frame, name);
			if (member != null)
				return member;

			string[] namespaces = context.GetNamespaces (frame);
			if (namespaces == null)
				return null;

			foreach (string ns in namespaces) {
				string full_name = MakeFQN (ns, name);
				member = LookupMember (context, frame, full_name);
				if (member != null)
					return member;
			}

			return null;
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
			if (context.HasFrame) {
				StackFrame frame = context.CurrentFrame;
				if ((frame.Method != null) && frame.Method.IsLoaded) {
					TargetVariable var = GetVariableByName (frame, name);
					if (var != null)
						return new VariableAccessExpression (var);
				}

				Expression expr = Lookup (context, frame);
				if (expr != null)
					return expr;
			}

			SourceLocation location = context.FindMethod (name);
			if (location != null)
				return new SourceExpression (location);

			Expression texpr = DoResolveType (context);
			if (texpr != null)
				return texpr;

			throw new ScriptingException ("No symbol `{0}' in current context.", Name);
		}

		protected override MethodExpression DoResolveMethod (ScriptingContext context,
								     LocationType type)
		{
			MemberExpression member = null;
			if (type == LocationType.Constructor) {
				Expression texpr = ResolveType (context);
				if (texpr == null)
					return null;

				TargetClassType ctype = texpr.EvaluateType (context) as TargetClassType;
				if (ctype == null)
					return null;

				member = StructAccessExpression.FindMember (
					context.CurrentThread, ctype, null, ".ctor", false, true);
			} else if (context.HasFrame) {
				member = Lookup (context, context.CurrentFrame);
			}

			if (member != null)
				return member.ResolveMethod (context, type);

			return DoResolve (context) as MethodExpression;
		}

		protected override Expression DoResolveType (ScriptingContext context)
		{
			Language language = context.CurrentLanguage;
			TargetType type = language.LookupType (name);
			if (type != null)
				return new TypeExpression (type);

			string[] namespaces = context.GetNamespaces (context.CurrentFrame);
			if (namespaces == null)
				return null;

			foreach (string ns in namespaces) {
				string full_name = MakeFQN (ns, name);
				type = language.LookupType (full_name);
				if (type != null)
					return new TypeExpression (type);
			}

			return null;
		}
	}

	public class PointerTypeExpression : TypeExpr
	{
		TargetType underlying_type;
		TargetType pointer_type;
		Expression expr;

		public PointerTypeExpression (Expression expr)
		{
			this.expr = expr;
		}

		public override string Name {
			get { return expr.Name + "*"; }
		}

		public override TargetType Type {
			get {
				if (!resolved)
					throw new InvalidOperationException ();

				return pointer_type;
			}
		}

		protected bool DoResolveBase (ScriptingContext context)
		{
			if (expr is SimpleNameExpression) {
				Process process = context.CurrentThread.Process;
				Language native = process.NativeLanguage;

				underlying_type = native.LookupType (expr.Name);
			} else {
				TypeExpr te = expr.ResolveType (context) as TypeExpr;
				if (te == null)
					return false;

				underlying_type = te.Type;
			}

			if (underlying_type == null)
				return false;

			pointer_type = underlying_type.Language.CreatePointerType (underlying_type);
			if (pointer_type == null)
				throw new ScriptingException ("Can't create of pointer of type `{0}'",
							      underlying_type.Name);

			resolved = true;
			return true;
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
			if (!DoResolveBase (context))
				return null;
			return this;
		}

		protected override Expression DoResolveType (ScriptingContext context)
		{
			if (!DoResolveBase (context))
				return null;
			return this;
		}
	}

	public class MemberAccessExpression : Expression
	{
		Expression left;
		string name;

		public MemberAccessExpression (Expression left, string name)
		{
			this.left = left;
			this.name = name;
		}

		public override string Name {
			get { return left.Name + "." + name; }
		}

		public MemberExpression ResolveMemberAccess (ScriptingContext context,
							     bool allow_instance)
		{
			MemberExpression member;

			Thread target = context.CurrentThread;

			Expression lexpr = left.TryResolve (context);
			if (lexpr is TypeExpr) {
				TargetClassType stype = Convert.ToClassType (((TypeExpr) lexpr).Type);

				member = StructAccessExpression.FindMember (
					target, stype, null, name, true, true);
				if (member == null)
					throw new ScriptingException (
						"Type `{0}' has no member `{1}'",
						stype.Name, name);

				if (!member.IsStatic && !allow_instance)
					throw new ScriptingException (
						"Cannot access instance member `{0}' with a type " +
						"reference.", Name);

				return member;
			}

			if (lexpr != null) {
				TargetClassObject sobj = Convert.ToClassObject (
					target, lexpr.EvaluateObject (context));
				if (sobj == null)
					throw new ScriptingException (
						"`{0}' is not a struct or class", left.Name);

				member = StructAccessExpression.FindMember (
					target, sobj.Type, sobj, name, true, true);
				if (member == null)
					throw new ScriptingException (
						"Type `{0}' has no member `{1}'",
						sobj.Type.Name, name);

				if (!member.IsInstance)
					throw new ScriptingException (
						"Cannot access static member `{0}.{1}' with an " +
						"instance reference; use a type name instead.",
						sobj.Type.Name, name);

				return member;
			}

			Expression ltype = left.TryResolveType (context);
			if (ltype != null) {
				TargetClassType stype = Convert.ToClassType (
					ltype.EvaluateType (context));

				member = StructAccessExpression.FindMember (
					target, stype, null, name, true, true);
				if (member == null)
					throw new ScriptingException (
						"Type `{0}' has no member `{1}'",
						stype.Name, name);

				if (!member.IsStatic && !allow_instance)
					throw new ScriptingException (
						"Cannot access instance member `{0}' with a type " +
						"reference.", Name);

				return member;
			}

			throw new ScriptingException (
				"No such variable or type: `{0}'", left.Name);
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
			return ResolveMemberAccess (context, false);
		}

		protected override MethodExpression DoResolveMethod (ScriptingContext context,
								     LocationType type)
		{
			MemberExpression member;
			if (type == LocationType.Constructor) {
				Expression texpr = ResolveType (context);
				if (texpr == null)
					return null;

				TargetClassType ctype = texpr.EvaluateType (context) as TargetClassType;
				if (ctype == null)
					return null;

				member = StructAccessExpression.FindMember (
					context.CurrentFrame.Thread, ctype, null, ".ctor", false, true);
			} else {
				member = ResolveMemberAccess (context, true);
			}

			if (member != null)
				return member.ResolveMethod (context, type);

			return null;
		}

		protected override Expression DoResolveType (ScriptingContext context)
		{
			StackFrame frame = context.CurrentFrame;

			TargetType the_type;

			Expression ltype = left.TryResolveType (context);
			if (ltype == null)
				the_type = frame.Language.LookupType (Name);
			else {
				string nested = ltype.Name + "+" + name;
				the_type = frame.Language.LookupType (nested);
			}

			if (the_type == null)
				return null;

			return new TypeExpression (the_type);
		}
	}

	public abstract class MemberExpression : Expression
	{
		public abstract TargetClassObject InstanceObject {
			get;
		}

		public abstract bool IsInstance {
			get;
		}

		public abstract bool IsStatic {
			get;
		}
	}

	public abstract class MethodExpression : MemberExpression
	{
		protected abstract SourceLocation DoEvaluateSource (ScriptingContext context);

		public SourceLocation EvaluateSource (ScriptingContext context)
		{
			if (!resolved)
				throw new InvalidOperationException (
					String.Format (
						"Some clown tried to evaluate the " +
						"unresolved expression `{0}'", Name));

			try {
				SourceLocation location = DoEvaluateSource (context);
				if (location == null)
					throw new ScriptingException (
						"Expression `{0}' is not a method", Name);

				return location;
			} catch (LocationInvalidException ex) {
				throw new ScriptingException (
					"Location of variable `{0}' is invalid: {1}",
					Name, ex.Message);
			}
		}
	}

	public class MethodGroupExpression : MethodExpression
	{
		protected readonly TargetClassType stype;
		protected readonly TargetClassObject instance;
		protected readonly string name;
		protected readonly TargetFunctionType[] methods;
		protected readonly bool is_instance, is_static;

		public MethodGroupExpression (TargetClassType stype, TargetClassObject instance,
					      string name, TargetFunctionType[] methods,
					      bool is_instance, bool is_static)
		{
			this.stype = stype;
			this.instance = instance;
			this.name = name;
			this.methods = methods;
			this.is_instance = is_instance;
			this.is_static = is_static;
			resolved = true;
		}

		public override string Name {
			get { return stype.Name + "." + name; }
		}

		public override TargetClassObject InstanceObject {
			get { return instance; }
		}

		public override bool IsInstance {
			get { return is_instance; }
		}

		public override bool IsStatic {
			get { return is_static; }
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
			return this;
		}

		protected override MethodExpression DoResolveMethod (ScriptingContext context,
								     LocationType type)
		{
			return this;
		}

		protected override TargetObject DoEvaluateObject (ScriptingContext context)
		{
			throw new ScriptingException ("Expression `{0}' is a method, not a " +
						      "field or property.", Name);
		}

		protected override TargetFunctionType DoEvaluateMethod (ScriptingContext context,
									LocationType type,
									Expression[] types)
		{
			if (type != LocationType.Method)
				return null;

			if (types == null) {
				if (methods.Length == 1)
					return (TargetFunctionType) methods [0];

                                throw new ScriptingException (
                                        "Ambiguous method `{0}'; need to use full name", Name);
			}

			TargetType[] argtypes = new TargetType [types.Length];
			for (int i = 0; i < types.Length; i++)
				argtypes [i] = types [i].EvaluateType (context);

			TargetFunctionType func = OverloadResolve (context, argtypes);
			if (func != null)
				return func;

			return context.Interpreter.QueryMethod (methods);
		}

		protected override SourceLocation DoEvaluateSource (ScriptingContext context)
		{
			if (methods.Length == 1)
				return new SourceLocation ((TargetFunctionType) methods [0]);

			throw new ScriptingException (
				"Ambiguous method `{0}'; need to use full name", Name);
		}

		public TargetFunctionType OverloadResolve (ScriptingContext context,
							   TargetType[] argtypes)
		{
			ArrayList candidates = new ArrayList ();

			foreach (TargetFunctionType method in methods) {
				if (method.ParameterTypes.Length != argtypes.Length)
					continue;

				candidates.Add (method);
			}

			TargetFunctionType candidate;
			if (candidates.Count == 1) {
				candidate = (TargetFunctionType) candidates [0];
				string error;
				if (IsApplicable (context, candidate, argtypes, out error))
					return candidate;

				throw new ScriptingException (
					"The best overload of method `{0}' has some invalid " +
					"arguments:\n{1}", Name, error);
			}

			if (candidates.Count == 0)
				throw new ScriptingException (
					"No overload of method `{0}' has {1} arguments.",
					Name, argtypes.Length);

			candidate = OverloadResolve (context, argtypes, candidates);

			if (candidate == null)
				throw new ScriptingException (
					"Ambiguous method `{0}'; need to use " +
					"full name", Name);

			return candidate;
		}

		public static bool IsApplicable (ScriptingContext context, TargetFunctionType method,
						 TargetType[] types, out string error)
		{
			for (int i = 0; i < types.Length; i++) {
				TargetType param_type = method.ParameterTypes [i];

				if (param_type == types [i])
					continue;

				if (Convert.ImplicitConversionExists (context, types [i], param_type))
					continue;

				error = String.Format (
					"Argument {0}: Cannot implicitly convert `{1}' to `{2}'",
					i, types [i].Name, param_type.Name);
				return false;
			}

			error = null;
			return true;
		}

		static TargetFunctionType OverloadResolve (ScriptingContext context,
							   TargetType[] argtypes,
							   ArrayList candidates)
		{
			// Ok, no we need to find an exact match.
			TargetFunctionType match = null;
			foreach (TargetFunctionType method in candidates) {
				string error;
				if (!IsApplicable (context, method, argtypes, out error))
					continue;

				// We need to find exactly one match
				if (match != null)
					return null;

				match = method;
			}

			return match;
		}
	}

	// So you can extend this by just creating a subclass
	// of BinaryOperator that implements DoEvaluate and
	// a constructor, but you'll need to add a new rule to
	// the parser of the form
	//
	// expression: my_param_kind MY_OP_TOKEN my_param_kind 
	//             { $$ = new MyBinarySubclass ((MyParam) $1, (MyParam) $3); }
	//
	// If you want to extend on of { +, -, *, /} for non-integers,
	// like supporting "a" + "b" = "ab", then larger changes would
	// be needed.

	public class BinaryOperator : PointerExpression
	{
		public enum Kind { Mult, Plus, Minus, Div };

		protected Kind kind;
		protected Expression left, right;

		public BinaryOperator (Kind kind, Expression left, Expression right)
		{
			this.kind = kind;
			this.left = left;
			this.right = right;
		}

		public override string Name {
			get {
				string op;
				switch (kind) {
				case Kind.Mult:
					op = "*";
					break;
				case Kind.Plus:
					op = "+";
					break;
				case Kind.Minus:
					op = "-";
					break;
				case Kind.Div:
					op = "/";
					break;
				default:
					throw new InternalError ();
				}
				return left.Name + op + right.Name;
			}
		}

		protected long DoEvaluate (ScriptingContext context, long lvalue, long rvalue)
		{
			switch (kind) {
			case Kind.Mult:
				return lvalue * rvalue;
			case Kind.Plus:
				return lvalue + rvalue;
			case Kind.Minus:
				return lvalue - rvalue;
			case Kind.Div:
				return lvalue / rvalue;
			}

			throw new ScriptingException ("Unknown binary operator kind: {0}", kind);
		}

		private long GetValue (ScriptingContext context, Expression expr)
		{
			object val = expr.Evaluate (context);
			if (val is int)
				return (long) (int) val;
			else if (val is uint)
				return (long) (uint) val;
			else if (val is ulong)
				return (long) (ulong) val;
			else if (val is long)
				return (long) val;
			else if (val is TargetPointerObject) {
				TargetPointerObject pobj = (TargetPointerObject) val;
				return pobj.GetAddress (context.CurrentThread).Address;
			} else
				throw new ScriptingException ("Cannot evaluate expression `{0}'", expr);
		}

		protected override object DoEvaluate (ScriptingContext context)
		{
			long lvalue = GetValue (context, left);
			long rvalue = GetValue (context, right);

			try {
				long retval = DoEvaluate (context, lvalue, rvalue);
				return new NumberExpression (retval);
			} catch {
				throw new ScriptingException ("Cannot evaluate expression `{0}'", Name);
			}
		}

		public override TargetAddress EvaluateAddress (ScriptingContext context)
		{
			NumberExpression result = (NumberExpression) Evaluate (context);
			return result.EvaluateAddress (context);
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
			left = left.Resolve (context);
			if (left == null)
				return null;

			right = right.Resolve (context);
			if (right == null)
				return null;

			resolved = true;
			return this;
		}
	}

	public class TypeOfExpression : Expression
	{
		Expression expr;

		public TypeOfExpression (Expression expr)
		{
			this.expr = expr;
		}

		public override string Name {
			get { return String.Format ("typeof ({0})", expr.Name); }
		}

		protected override Expression DoResolveType (ScriptingContext context)
		{
			return expr.ResolveType (context);
		}
		
		protected override Expression DoResolve (ScriptingContext context)
		{
			return expr.Resolve (context);
		}
	}

	public abstract class PointerExpression : Expression
	{
		public abstract TargetAddress EvaluateAddress (ScriptingContext context);
	}

	public class RegisterExpression : PointerExpression
	{
		string name;
		int register = -1;

		public RegisterExpression (string register)
		{
			this.name = register;
			resolved = true;
		}

		public override string Name {
			get { return '%' + name; }
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
			return this;
		}

		protected override TargetType DoEvaluateType (ScriptingContext context)
		{
			return context.CurrentLanguage.PointerType;
		}

		protected override TargetObject DoEvaluateObject (ScriptingContext context)
		{
			StackFrame frame = context.CurrentFrame;
			Register register = frame.Registers [name];
			if (register == null)
				throw new ScriptingException ("No such register `{0}'.", name);

			try {
				long value = register.Value;
				TargetAddress address = new TargetAddress (
					context.AddressDomain, value);
				return context.CurrentLanguage.CreatePointer (frame, address);
			} catch {
				throw new ScriptingException (
					"Can't access register `{0}' selected stack frame.", name);
			}
		}

		public override TargetAddress EvaluateAddress (ScriptingContext context)
		{
			TargetPointerObject pobj = (TargetPointerObject) EvaluateObject (context);
			return pobj.GetAddress (context.CurrentThread);
		}

		protected override bool DoAssign (ScriptingContext context, TargetObject tobj)
		{
			TargetFundamentalObject fobj = tobj as TargetFundamentalObject;
			if (fobj == null)
				throw new ScriptingException (
					"Cannot store non-fundamental object `{0}' in " +
					" a registers", tobj);

			object obj = fobj.GetObject (context.CurrentThread);
			long value = System.Convert.ToInt64 (obj);

			Register register = context.CurrentFrame.Registers [name];
			if (register == null)
				throw new ScriptingException ("No such register `{0}'.", name);

			register.WriteRegister (context.CurrentThread, value);
			return true;
		}
	}

	public class StructAccessExpression : MemberExpression
	{
		public readonly TargetClassType Type;
		public readonly TargetMemberInfo Member;
		protected readonly TargetClassObject instance;
		TargetClass class_info;

		protected StructAccessExpression (TargetClassType type,
						  TargetClassObject instance,
						  TargetMemberInfo member)
		{
			this.Type = type;
			this.Member = member;
			this.instance = instance;
			resolved = true;
		}

		public override string Name {
			get { return Member.Name; }
		}

		public override TargetClassObject InstanceObject {
			get { return instance; }
		}

		public override bool IsInstance {
			get { return !Member.IsStatic; }
		}

		public override bool IsStatic {
			get { return Member.IsStatic; }
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
			return this;
		}

		protected MethodGroupExpression CreateMethodGroup (TargetFunctionType func)
		{
			return new MethodGroupExpression (
				func.DeclaringType, instance, func.Name,
				new TargetFunctionType[] { func },
				!func.IsStatic, func.IsStatic);
		}

		protected void ResolveClass (Thread target)
		{
			if (class_info != null)
				return;

			class_info = Type.GetClass (target);
			if (class_info == null)
				throw new ScriptingException ("Class `{0}' not initialized yet.",
							      Type.Name);
		}

		protected override MethodExpression DoResolveMethod (ScriptingContext context,
								     LocationType type)
		{
			switch (type) {
			case LocationType.PropertyGetter:
			case LocationType.PropertySetter:
				TargetPropertyInfo property = Member as TargetPropertyInfo;
				if (property == null)
					return null;

				if (type == LocationType.PropertyGetter) {
					if (!property.CanRead)
						throw new ScriptingException (
							"Property {0} doesn't have a getter.", Name);
					return CreateMethodGroup (property.Getter);
				} else {
					if (!property.CanWrite)
						throw new ScriptingException (
							"Property {0} doesn't have a setter.", Name);
					return CreateMethodGroup (property.Setter);
				}

			case LocationType.EventAdd:
			case LocationType.EventRemove:
				TargetEventInfo ev = Member as TargetEventInfo;
				if (ev == null)
					return null;

				if (type == LocationType.EventAdd)
					return CreateMethodGroup (ev.Add);
				else
					return CreateMethodGroup (ev.Remove);

			case LocationType.Method:
			case LocationType.DelegateInvoke:
				TargetMethodInfo method = Member as TargetMethodInfo;
				if (method != null)
					return CreateMethodGroup (method.Type);

				return InvocationExpression.ResolveDelegate (context, this);

			default:
				return null;
			}
		}

		protected TargetObject GetField (Thread target, TargetFieldInfo field)
		{
			ResolveClass (target);
			return class_info.GetField (target, InstanceObject, field);
		}

		protected TargetObject GetProperty (ScriptingContext context,
						    TargetPropertyInfo prop)
		{
			RuntimeInvokeResult result = context.Interpreter.RuntimeInvoke (
				context.CurrentThread, prop.Getter, InstanceObject,
				new TargetObject [0], true, false);

			if (result.ExceptionMessage != null)
				throw new ScriptingException (
					"Invocation of `{0}' raised an exception: {1}",
					Name, result.ExceptionMessage);

			return result.ReturnObject;
		}

		protected TargetObject GetMember (ScriptingContext context, Thread target,
						  TargetMemberInfo member)
		{
			if (member is TargetPropertyInfo)
				return GetProperty (context, (TargetPropertyInfo) member);
			else if (member is TargetFieldInfo)
				return GetField (target, (TargetFieldInfo) member);
			else
				throw new ScriptingException ("Member `{0}' is of unknown type {1}",
							      Name, member.GetType ());
		}

		public static MemberExpression FindMember (Thread target, TargetStructType stype,
							   TargetClassObject instance, string name,
							   bool search_static, bool search_instance)
		{
		again:
			TargetClassType ctype = stype as TargetClassType;
			if (ctype == null)
				return null;

			TargetMemberInfo member = ctype.FindMember (
				name, search_static, search_instance);

			if (member != null)
				return new StructAccessExpression (ctype, instance, member);

			ArrayList methods = new ArrayList ();
			bool is_instance = false;
			bool is_static = false;

			if (name == ".ctor") {
				foreach (TargetMethodInfo method in ctype.Constructors) {
					if (method.IsStatic)
						continue;
					methods.Add (method.Type);
					is_instance = true;
				}
			} else if (name == ".cctor") {
				foreach (TargetMethodInfo method in ctype.Constructors) {
					if (!method.IsStatic)
						continue;
					methods.Add (method.Type);
					is_static = true;
				}
			} else {
				foreach (TargetMethodInfo method in ctype.Methods) {
					if (method.IsStatic && !search_static)
						continue;
					if (!method.IsStatic && !search_instance)
						continue;
					if (method.Name != name)
						continue;

					methods.Add (method.Type);
					if (method.IsStatic)
						is_static = true;
					else
						is_instance = true;
				}
			}

			if (methods.Count > 0) {
				TargetFunctionType[] funcs = new TargetFunctionType [methods.Count];
				methods.CopyTo (funcs, 0);
				return new MethodGroupExpression (
					ctype, instance, name, funcs, is_instance, is_static);
			}

			if (ctype.HasParent) {
				stype = ctype.GetParentType (target);
				if (instance != null) {
					instance = instance.GetParentObject (target);
					if (instance == null)
						return null;
				}
				goto again;
			}

			return null;
		}

		protected override TargetObject DoEvaluateObject (ScriptingContext context)
		{
			StackFrame frame = context.CurrentFrame;

			if (!Member.IsStatic && (InstanceObject == null))
				throw new ScriptingException (
					"Instance member `{0}' cannot be used in static context.", Name);

			try {
				return GetMember (context, frame.Thread, Member);
			} catch (TargetException ex) {
				throw new ScriptingException ("Cannot access struct member `{0}': {1}",
							      Name, ex.Message);
			}
		}

		protected override TargetType DoEvaluateType (ScriptingContext context)
		{
			return Member.Type;
		}

		public TargetFunctionType ResolveDelegate (ScriptingContext context)
		{
			MethodGroupExpression mg = InvocationExpression.ResolveDelegate (
				context, this);
			if (mg == null)
				return null;

			return mg.OverloadResolve (context, null);
		}

		protected override TargetFunctionType DoEvaluateMethod (ScriptingContext context,
									LocationType type,
									Expression[] types)
		{
			switch (type) {
			case LocationType.PropertyGetter:
			case LocationType.PropertySetter:
				TargetPropertyInfo property = Member as TargetPropertyInfo;
				if (property == null)
					return null;

				if (type == LocationType.PropertyGetter) {
					if (!property.CanRead)
						throw new ScriptingException (
							"Property {0} doesn't have a getter.", Name);
					return property.Getter;
				} else {
					if (!property.CanWrite)
						throw new ScriptingException (
							"Property {0} doesn't have a setter.", Name);
					return property.Setter;
				}

			case LocationType.EventAdd:
			case LocationType.EventRemove:
				TargetEventInfo ev = Member as TargetEventInfo;
				if (ev == null)
					return null;

				if (type == LocationType.EventAdd)
					return ev.Add;
				else
					return ev.Remove;

			case LocationType.Method:
			case LocationType.DelegateInvoke:
				MethodGroupExpression mg = InvocationExpression.ResolveDelegate (
					context, this);
				if (mg == null)
					return null;

				return mg.EvaluateMethod (context, LocationType.Method, types);

			default:
				return null;
			}
		}

		protected void SetField (Thread target, TargetFieldInfo field, TargetObject obj)
		{
			ResolveClass (target);
			class_info.SetField (target, InstanceObject, field, obj);
		}

		protected override bool DoAssign (ScriptingContext context, TargetObject obj)
		{
			if (Member is TargetFieldInfo) {
				if (Member.Type != obj.Type)
					throw new ScriptingException (
						"Type mismatch: cannot assign expression of type " +
						"`{0}' to field `{1}', which is of type `{2}'.",
						obj.TypeName, Name, Member.Type.Name);

				SetField (context.CurrentThread, (TargetFieldInfo) Member, obj);
			}
			else if (Member is TargetPropertyInfo) 
			  	throw new ScriptingException ("Can't set properties directly.");
			else if (Member is TargetEventInfo)
				throw new ScriptingException ("Can't set events directly.");
			else if (Member is TargetMethodInfo)
				throw new ScriptingException ("Can't set methods directly.");

			return true;
		}
	}

	public class PointerDereferenceExpression : PointerExpression
	{
		Expression expr;
		string name;
		bool current_ok;

		public PointerDereferenceExpression (Expression expr, bool current_ok)
		{
			this.expr = expr;
			this.current_ok = current_ok;
			name = '*' + expr.Name;
		}

		public override string Name {
			get {
				return name;
			}
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
			expr = expr.Resolve (context);
			if (expr == null)
				return null;

			resolved = true;
			return this;
		}

		protected override TargetType DoEvaluateType (ScriptingContext context)
		{
			TargetType type = expr.EvaluateType (context);

			TargetPointerType ptype = type as TargetPointerType;
			if (ptype != null)
				return ptype.StaticType;

			throw new ScriptingException (
				"Expression `{0}' is not a pointer.", expr.Name);
		}

		protected override TargetObject DoEvaluateObject (ScriptingContext context)
		{
			TargetObject obj = expr.EvaluateObject (context);

			TargetPointerObject pobj = obj as TargetPointerObject;
			if (pobj != null) {
				TargetObject result;
				try {
					result = pobj.GetDereferencedObject (context.CurrentThread);
				} catch {
					result = null;
				}

				if (result != null)
					return result;
			}

			TargetClassObject cobj = obj as TargetClassObject;
			if (current_ok && (cobj != null))
				return cobj;

			PointerExpression pexpr = expr as PointerExpression;
			if (pexpr != null) {
				TargetAddress address = pexpr.EvaluateAddress (context);
				TargetAddress result;
				try {
					result = context.CurrentThread.ReadAddress (address);
					return context.CurrentFrame.Language.CreatePointer (
						context.CurrentFrame, result);
				} catch {
					throw new ScriptingException ("Cannot dereference `{0}'.",
								      expr.Name);
				}
			}

			throw new ScriptingException (
				"Expression `{0}' is not a pointer.", expr.Name);
		}

		public override TargetAddress EvaluateAddress (ScriptingContext context)
		{
			object obj = expr.Resolve (context);
			if (obj is int)
				obj = (long) (int) obj;
			if (obj is long)
				return new TargetAddress (context.AddressDomain, (long) obj);
			else if (obj is PointerExpression)
				return ((PointerExpression) obj).EvaluateAddress (context);

			TargetPointerObject pobj = obj as TargetPointerObject;
			if (pobj == null)
				throw new ScriptingException (
					"Expression `{0}' is not a pointer type.", expr.Name);

			return pobj.GetAddress (context.CurrentThread);
		}
	}

	public class AddressOfExpression : PointerExpression
	{
		Expression expr;
		string name;

		public AddressOfExpression (Expression expr)
		{
			this.expr = expr;
			name = '&' + expr.Name;
		}

		public override string Name {
			get {
				return name;
			}
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
			expr = expr.Resolve (context);
			if (expr == null)
				return null;

			resolved = true;
			return this;
		}

		protected override TargetType DoEvaluateType (ScriptingContext context)
		{
			TargetPointerType ptype = expr.EvaluateType (context)
				as TargetPointerType;
			if (ptype != null)
				return ptype;

			return context.CurrentLanguage.PointerType;
		}

		protected override TargetObject DoEvaluateObject (ScriptingContext context)
		{
			StackFrame frame = context.CurrentFrame;

			TargetAddress address = EvaluateAddress (context);

			return context.CurrentLanguage.CreatePointer (frame, address);
		}

		public override TargetAddress EvaluateAddress (ScriptingContext context)
		{
			PointerExpression pexpr = expr as PointerExpression;
			if (pexpr != null)
				return pexpr.EvaluateAddress (context);

			TargetObject obj = expr.EvaluateObject (context);
			if ((obj == null) || !obj.HasAddress)
				throw new ScriptingException (
					"Cannot take address of expression `{0}'", expr.Name);

			return obj.GetAddress (context.CurrentThread);
		}
	}

	public class ArrayAccessExpression : Expression
	{
		Expression expr;
		Expression[] indices;
		string name;

		public ArrayAccessExpression (Expression expr, Expression[] indices)
		{
			this.expr = expr;
			this.indices = indices;

			StringBuilder sb = new StringBuilder("");
			bool comma = false;
			foreach (Expression index in indices) {
				if (comma) sb.Append(",");
				sb.Append (index.ToString());
				comma = true;
			}
			name = String.Format ("{0}[{1}]", expr.Name, sb.ToString());
		}

		public override string Name {
			get {
				return name;
			}
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
			int i;
			expr = expr.Resolve (context);
			if (expr == null)
				return null;

			for (i = 0; i < indices.Length; i ++) {
				indices[i] = indices[i].Resolve (context);
				if (indices[i] == null)
					return null;
			}

			resolved = true;
			return this;
		}

		int GetIntIndex (Thread target, Expression index, ScriptingContext context)
		{
			try {
				object idx = index.Evaluate (context);

				if (idx is int)
					return (int) idx;

				TargetFundamentalObject obj = (TargetFundamentalObject) idx;
				return (int) obj.GetObject (target);
			} catch (Exception e) {
				throw new ScriptingException (
					"Cannot convert {0} to an integer for indexing: {1}",
					index, e);
			}
		}

		int[] GetIntIndices (Thread target, ScriptingContext context)
		{
			int[] int_indices = new int [indices.Length];
			for (int i = 0; i < indices.Length; i++)
				int_indices [i] = GetIntIndex (target, indices [i], context);
			return int_indices;
		}

		protected override TargetObject DoEvaluateObject (ScriptingContext context)
		{
			Thread target = context.CurrentThread;
			TargetObject obj = expr.EvaluateObject (context);

			// array[int]
			TargetArrayObject aobj = obj as TargetArrayObject;
			if (aobj != null) {
				int[] int_indices = GetIntIndices (target, context);
				try {
					return aobj.GetElement (target, int_indices);
				} catch (ArgumentException) {
					throw new ScriptingException (
						"Index of array expression `{0}' out of bounds.",
						expr.Name);
				}
			}

			// pointer[int]
			TargetPointerObject pobj = obj as TargetPointerObject;
			if (pobj != null) {
				// single dimensional array only at present
				int[] int_indices = GetIntIndices (target, context);
				if (int_indices.Length != 1)
					throw new ScriptingException (
						"Multi-dimensial arrays of type {0} are not yet supported",
						expr.Name);

				if (pobj.Type.IsArray)
					return pobj.GetArrayElement (target, int_indices [0]);

				throw new ScriptingException (
						       "Variable {0} is not an array type.", expr.Name);
			}

			// indexers
			TargetClassObject sobj = Convert.ToClassObject (target, obj);
			if (sobj != null) {
				ArrayList props = new ArrayList ();
				foreach (TargetPropertyInfo prop in sobj.Type.Properties) {
					if (!prop.CanRead)
						continue;

					props.Add (prop.Getter);
				}

				if (props.Count == 0)
					throw new ScriptingException (
						"Indexer `{0}' doesn't have a getter.", expr.Name);

				TargetFunctionType[] funcs = new TargetFunctionType [props.Count];
				props.CopyTo (funcs, 0);

				MethodGroupExpression mg = new MethodGroupExpression (
					sobj.Type, sobj, expr.Name + ".this", funcs, true, false);

				InvocationExpression invocation = new InvocationExpression (
					mg, indices);
				invocation.Resolve (context);

				return invocation.EvaluateObject (context);
			}

			throw new ScriptingException (
				"{0} is neither an array/pointer type, nor is it " +
				"an object with a valid indexer.", expr);
		}

		protected override TargetType DoEvaluateType (ScriptingContext context)
		{
			TargetArrayType type = expr.EvaluateType (context)
				as TargetArrayType;
			if (type == null)
				throw new ScriptingException (
					"Variable {0} is not an array type.", expr.Name);

			return type.ElementType;
		}

		protected override bool DoAssign (ScriptingContext context, TargetObject right)
		{
			Thread target = context.CurrentThread;
			TargetObject obj = expr.EvaluateObject (context);

			// array[int]
			TargetArrayObject aobj = obj as TargetArrayObject;
			if (aobj != null) {
				int[] int_indices = GetIntIndices (target, context);
				try {
					aobj.SetElement (target, int_indices, right);
				} catch (ArgumentException) {
					throw new ScriptingException (
						"Index of array expression `{0}' out of bounds.",
						expr.Name);
				}

				return true;
			}

			// indexers
			TargetClassObject sobj = Convert.ToClassObject (target, obj);
			if (sobj != null) {
				ArrayList props = new ArrayList ();
				foreach (TargetPropertyInfo prop in sobj.Type.Properties) {
					if (!prop.CanWrite)
						continue;

					props.Add (prop.Setter);
				}

				if (props.Count == 0)
					throw new ScriptingException (
						"Indexer `{0}' doesn't have a setter.", expr.Name);

				TargetFunctionType[] funcs = new TargetFunctionType [props.Count];
				props.CopyTo (funcs, 0);

				MethodGroupExpression mg = new MethodGroupExpression (
					sobj.Type, sobj, expr.Name + "[]", funcs, true, false);

				Expression[] indexargs = new Expression [indices.Length + 1];
				indices.CopyTo (indexargs, 0);
				indexargs [indices.Length] = new ArgumentExpression (right);

				InvocationExpression invocation = new InvocationExpression (
					mg, indexargs);
				invocation.Resolve (context);

				invocation.Invoke (context, false);
				return true;
			}
			
			throw new ScriptingException (
				"{0} is neither an array/pointer type, nor is it " +
				"an object with a valid indexer.", expr);
		}

	}

	public class CastExpression : Expression
	{
		Expression target, expr;
		string name;

		public CastExpression (Expression target, Expression expr)
		{
			this.target = target;
			this.expr = expr;
			this.name = String.Format ("(({0}) {1})", target.Name, expr.Name);
		}

		public override string Name {
			get {
				return name;
			}
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
			target = target.ResolveType (context);
			if (target == null)
				return null;

			expr = expr.Resolve (context);
			if (expr == null)
				return null;

			resolved = true;
			return this;
		}

		static TargetClassObject TryParentCast (ScriptingContext context,
							TargetClassObject source,
							TargetStructType source_type,
							TargetStructType target_type)
		{
			if (source_type == target_type)
				return source;

			if (!source_type.HasParent)
				return null;

			TargetStructType parent_type = source_type.GetParentType (context.CurrentThread);
			source = TryParentCast (context, source, parent_type, target_type);
			if (source == null)
				return null;

			return source.GetParentObject (context.CurrentThread);
		}

		static TargetClassObject TryCurrentCast (ScriptingContext context,
							 TargetClassObject source,
							 TargetClassType target_type)
		{
			TargetClassObject current = source.GetCurrentObject (context.CurrentThread);
			if (current == null)
				return null;

			return TryParentCast (context, current, current.Type, target_type);
		}

		public static TargetObject TryCast (ScriptingContext context, TargetObject source,
						    TargetClassType target_type)
		{
			if (source.Type == target_type)
				return source;

			TargetClassObject sobj = Convert.ToClassObject (context.CurrentThread, source);
			if (sobj == null)
				return null;

			TargetClassObject result;
			result = TryParentCast (context, sobj, sobj.Type, target_type);
			if (result != null)
				return result;

			return TryCurrentCast (context, sobj, target_type);
		}

		static bool TryParentCast (ScriptingContext context,
					   TargetStructType source_type,
					   TargetStructType target_type)
		{
			if (source_type == target_type)
				return true;

			if (!source_type.HasParent)
				return false;

			TargetStructType parent_type = source_type.GetParentType (context.CurrentThread);
			return TryParentCast (context, parent_type, target_type);
		}

		public static bool TryCast (ScriptingContext context, TargetType source,
					    TargetClassType target_type)
		{
			if (source == target_type)
				return true;

			TargetClassType stype = Convert.ToClassType (source);
			if (stype == null)
				return false;

			return TryParentCast (context, stype, target_type);
		}

		protected override TargetObject DoEvaluateObject (ScriptingContext context)
		{
			TargetType target_type = target.EvaluateType (context);

			if (target_type is TargetPointerType) {
				TargetAddress address;

				PointerExpression pexpr = expr as PointerExpression;
				if (pexpr != null)
					address = pexpr.EvaluateAddress (context);
				else {
					TargetType etype = expr.EvaluateType (context);
					TargetPointerType ptype = etype as TargetPointerType;

					if ((ptype == null) || ptype.IsTypesafe)
						throw new ScriptingException (
							"Cannot cast from {0} to {1}.",
							etype.Name, target.Name);

					pexpr = new AddressOfExpression (expr);
					pexpr.Resolve (context);

					address = pexpr.EvaluateAddress (context);
				}

				return ((TargetPointerType) target_type).GetObject (address);
			}

			TargetClassType type = Convert.ToClassType (target_type);
			TargetClassObject source = Convert.ToClassObject (
				context.CurrentThread, expr.EvaluateObject (context));

			if (source == null)
				throw new ScriptingException (
					"Variable {0} is not a class type.", expr.Name);

			TargetObject obj = TryCast (context, source, type);
			if (obj == null)
				throw new ScriptingException (
					"Cannot cast from {0} to {1}.", source.Type.Name,
					type.Name);

			return obj;
		}

		protected override TargetType DoEvaluateType (ScriptingContext context)
		{
			TargetObject obj = EvaluateObject (context);
			if (obj == null)
				return null;

			return obj.Type;
		}
	}


	public static class Convert
	{
		static bool ImplicitFundamentalConversionExists (FundamentalKind skind,
								 FundamentalKind tkind)
		{
			//
			// See Convert.ImplicitStandardConversionExists in MCS.
			//
			switch (skind) {
			case FundamentalKind.SByte:
				if ((tkind == FundamentalKind.Int16) ||
				    (tkind == FundamentalKind.Int32) ||
				    (tkind == FundamentalKind.Int64) ||
				    (tkind == FundamentalKind.Single) ||
				    (tkind == FundamentalKind.Double))
					return true;
				break;

			case FundamentalKind.Byte:
				if ((tkind == FundamentalKind.Int16) ||
				    (tkind == FundamentalKind.UInt16) ||
				    (tkind == FundamentalKind.Int32) ||
				    (tkind == FundamentalKind.UInt32) ||
				    (tkind == FundamentalKind.Int64) ||
				    (tkind == FundamentalKind.UInt64) ||
				    (tkind == FundamentalKind.Single) ||
				    (tkind == FundamentalKind.Double))
					return true;
				break;

			case FundamentalKind.Int16:
				if ((tkind == FundamentalKind.Int32) ||
				    (tkind == FundamentalKind.Int64) ||
				    (tkind == FundamentalKind.Single) ||
				    (tkind == FundamentalKind.Double))
					return true;
				break;

			case FundamentalKind.UInt16:
				if ((tkind == FundamentalKind.Int32) ||
				    (tkind == FundamentalKind.UInt32) ||
				    (tkind == FundamentalKind.Int64) ||
				    (tkind == FundamentalKind.UInt64) ||
				    (tkind == FundamentalKind.Single) ||
				    (tkind == FundamentalKind.Double))
					return true;
				break;

			case FundamentalKind.Int32:
				if ((tkind == FundamentalKind.Int64) ||
				    (tkind == FundamentalKind.Single) ||
				    (tkind == FundamentalKind.Double))
					return true;
				break;

			case FundamentalKind.UInt32:
				if ((tkind == FundamentalKind.Int64) ||
				    (tkind == FundamentalKind.UInt64) ||
				    (tkind == FundamentalKind.Single) ||
				    (tkind == FundamentalKind.Double))
					return true;
				break;

			case FundamentalKind.Int64:
			case FundamentalKind.UInt64:
				if ((tkind == FundamentalKind.Single) ||
				    (tkind == FundamentalKind.Double))
					return true;
				break;

			case FundamentalKind.Char:
				if ((tkind == FundamentalKind.UInt16) ||
				    (tkind == FundamentalKind.Int32) ||
				    (tkind == FundamentalKind.UInt32) ||
				    (tkind == FundamentalKind.Int64) ||
				    (tkind == FundamentalKind.UInt64) ||
				    (tkind == FundamentalKind.Single) ||
				    (tkind == FundamentalKind.Double))
					return true;
				break;

			case FundamentalKind.Single:
				if (tkind == FundamentalKind.Double)
					return true;
				break;

			default:
				break;
			}

			return false;
		}

		static bool ImplicitFundamentalConversionExists (ScriptingContext context,
								 TargetFundamentalType source,
								 TargetFundamentalType target)
		{
			return ImplicitFundamentalConversionExists (
				source.FundamentalKind, target.FundamentalKind);
		}

		static object ImplicitFundamentalConversion (object value, FundamentalKind tkind)
		{
			switch (tkind) {
			case FundamentalKind.Char:
				return System.Convert.ToChar (value);
			case FundamentalKind.SByte:
				return System.Convert.ToSByte (value);
			case FundamentalKind.Byte:
				return System.Convert.ToByte (value);
			case FundamentalKind.Int16:
				return System.Convert.ToInt16 (value);
			case FundamentalKind.UInt16:
				return System.Convert.ToUInt16 (value);
			case FundamentalKind.Int32:
				return System.Convert.ToInt32 (value);
			case FundamentalKind.UInt32:
				return System.Convert.ToUInt32 (value);
			case FundamentalKind.Int64:
				return System.Convert.ToInt64 (value);
			case FundamentalKind.UInt64:
				return System.Convert.ToUInt64 (value);
			case FundamentalKind.Single:
				return System.Convert.ToSingle (value);
			case FundamentalKind.Double:
				return System.Convert.ToDouble (value);
			default:
				return null;
			}
		}

		static TargetObject ImplicitFundamentalConversion (ScriptingContext context,
								   TargetFundamentalObject obj,
								   TargetFundamentalType type)
		{
			FundamentalKind skind = obj.Type.FundamentalKind;
			FundamentalKind tkind = type.FundamentalKind;

			if (!ImplicitFundamentalConversionExists (skind, tkind))
				return null;

			object value = obj.GetObject (context.CurrentThread);

			object new_value = ImplicitFundamentalConversion (value, tkind);
			if (new_value == null)
				return null;

			return type.Language.CreateInstance (context.CurrentThread, new_value);
		}

		static bool ImplicitReferenceConversionExists (ScriptingContext context,
							       TargetStructType source,
							       TargetStructType target)
		{
			if (source == target)
				return true;

			if (!source.HasParent)
				return false;

			TargetStructType parent_type = source.GetParentType (context.CurrentThread);
			return ImplicitReferenceConversionExists (context, parent_type, target);
		}

		static TargetObject ImplicitReferenceConversion (ScriptingContext context,
								 TargetClassObject obj,
								 TargetClassType type)
		{
			if (obj.Type == type)
				return obj;

			if (!obj.Type.HasParent)
				return null;

			return obj.GetParentObject (context.CurrentThread);
		}

		public static bool ImplicitConversionExists (ScriptingContext context,
							     TargetType source, TargetType target)
		{
			if (source.Equals (target))
				return true;

			if ((source is TargetFundamentalType) && (target is TargetFundamentalType))
				return ImplicitFundamentalConversionExists (
					context, (TargetFundamentalType) source,
					(TargetFundamentalType) target);

			if ((source is TargetClassType) && (target is TargetClassType))
				return ImplicitReferenceConversionExists (
					context, (TargetClassType) source,
					(TargetClassType) target);

			return false;
		}

		public static TargetObject ImplicitConversion (ScriptingContext context,
							       TargetObject obj, TargetType type)
		{
			if (obj.Type.Equals (type))
				return obj;

			if ((obj is TargetFundamentalObject) && (type is TargetFundamentalType))
				return ImplicitFundamentalConversion (
					context, (TargetFundamentalObject) obj,
					(TargetFundamentalType) type);

			if ((obj is TargetClassObject) && (type is TargetClassType))
				return ImplicitReferenceConversion (
					context, (TargetClassObject) obj,
					(TargetClassType) type);

			return null;
		}

		public static TargetObject ImplicitConversionRequired (ScriptingContext context,
								       TargetObject obj, TargetType type)
		{
			TargetObject new_obj = ImplicitConversion (context, obj, type);
			if (new_obj != null)
				return new_obj;

			throw new ScriptingException (
				"Cannot implicitly convert `{0}' to `{1}'", obj.Type.Name, type.Name);
		}

		public static TargetClassType ToClassType (TargetType type)
		{
			TargetClassType ctype = type as TargetClassType;
			if (ctype != null)
				return ctype;

			TargetObjectType otype = type as TargetObjectType;
			if (otype != null) {
				ctype = otype.ClassType;
				if (ctype != null)
					return ctype;
			}

			TargetArrayType atype = type as TargetArrayType;
			if (atype != null) {
				if (atype.Language.ArrayType != null)
					return atype.Language.ArrayType;
			}

			throw new ScriptingException (
				"Type `{0}' is not a struct or class.", type.Name);
		}

		public static TargetClassObject ToClassObject (Thread target, TargetObject obj)
		{
			TargetClassObject cobj = obj as TargetClassObject;
			if (cobj != null)
				return cobj;

			TargetObjectObject oobj = obj as TargetObjectObject;
			if (oobj != null)
				return oobj.GetClassObject (target);

			TargetArrayObject aobj = obj as TargetArrayObject;
			if ((aobj != null) && aobj.HasClassObject)
				return aobj.GetClassObject (target);

			return null;
		}
	}

	public class ConditionalExpression : Expression
	{
		Expression test;
		Expression true_expr;
		Expression false_expr;

		public override string Name {
			get { return "conditional"; }
		}

		public ConditionalExpression (Expression test, Expression true_expr, Expression false_expr)
		{
		  this.test = test;
		  this.true_expr = true_expr;
		  this.false_expr = false_expr;
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
		  this.test = this.test.Resolve (context);
		  if (this.test == null)
		    return null;

		  this.true_expr = this.true_expr.Resolve (context);
		  if (this.true_expr == null)
		    return null;

		  this.false_expr = this.false_expr.Resolve (context);
		  if (this.false_expr == null)
		    return null;

			resolved = true;
			return this;
		}

		protected override object DoEvaluate (ScriptingContext context)
		{
			bool cond;

			try {
				cond = (bool) this.test.Evaluate (context);
			}
			catch (Exception e) {
				throw new ScriptingException (
					"Cannot convert {0} to a boolean for conditional: {1}",
					this.test, e);
			}

			return cond ? true_expr.Evaluate (context) : false_expr.Evaluate (context);
		}

		protected override TargetObject DoEvaluateObject (ScriptingContext context)
		{
			bool cond;

			try {
				cond = (bool) this.test.Evaluate (context);
			}
			catch (Exception e) {
				throw new ScriptingException (
					"Cannot convert {0} to a boolean for conditional: {1}",
					this.test, e);
			}

			return cond ? true_expr.EvaluateObject (context) : false_expr.EvaluateObject (context);
		}
	}

	public class InvocationExpression : MethodExpression
	{
		Expression expr;
		Expression[] arguments;
		MethodGroupExpression method_expr;
		string name;

		TargetType[] argtypes;
		TargetFunctionType method;

		public InvocationExpression (Expression expr, Expression[] arguments)
		{
			this.expr = expr;
			this.arguments = arguments;

			name = String.Format ("{0} ()", expr.Name);
		}

		public override string Name {
			get { return name; }
		}

		public static MethodGroupExpression ResolveDelegate (ScriptingContext context,
								     Expression expr)
		{
			TargetClassType ctype = Convert.ToClassType (expr.EvaluateType (context));
			if (ctype == null)
				return null;

			TargetClassObject cobj;
			try {
				cobj = Convert.ToClassObject (
					context.CurrentThread, expr.EvaluateObject (context));
			} catch {
				cobj = null;
			}

			TargetClassType delegate_type = ctype.Language.DelegateType;
			if (!CastExpression.TryCast (context, ctype, delegate_type))
				return null;

			TargetFunctionType invoke = null;
			foreach (TargetMethodInfo method in ctype.Methods) {
				if (method.Name == "Invoke") {
					invoke = method.Type;
					break;
				}
			}

			if (invoke == null)
				return null;

			TargetFunctionType[] methods = new TargetFunctionType[] { invoke };

			MethodGroupExpression mg = new MethodGroupExpression (
				ctype, cobj, "Invoke", methods, true, false);
			return mg;
		}

		public override TargetClassObject InstanceObject {
			get { return method_expr.InstanceObject; }
		}

		public override bool IsInstance {
			get { return method_expr.IsInstance; }
		}

		public override bool IsStatic {
			get { return method_expr.IsStatic; }
		}

		protected override MethodExpression DoResolveMethod (ScriptingContext context,
								     LocationType type)
		{
			method_expr = (MethodGroupExpression) expr.ResolveMethod (context, type);
			if (method_expr == null)
				return null;

			resolved = true;
			return this;
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
			method_expr = (MethodGroupExpression) expr.ResolveMethod (
				context, LocationType.Method);
			if (method_expr == null)
				return null;

			argtypes = new TargetType [arguments.Length];

			for (int i = 0; i < arguments.Length; i++) {
				arguments [i] = arguments [i].Resolve (context);
				if (arguments [i] == null)
					return null;

				argtypes [i] = arguments [i].EvaluateType (context);
			}

			method = method_expr.OverloadResolve (context, argtypes);

			resolved = true;
			return this;
		}

		protected override TargetObject DoEvaluateObject (ScriptingContext context)
		{
			TargetObject retval = DoInvoke (context, false);

			if (!method.HasReturnValue)
				throw new ScriptingException (
					"Method `{0}' doesn't return a value.", Name);

			return retval;
		}

		protected override TargetType DoEvaluateType (ScriptingContext context)
		{
			return method.ReturnType;
		}

		protected override TargetFunctionType DoEvaluateMethod (ScriptingContext context,
									LocationType type,
									Expression[] types)
		{
			return method_expr.EvaluateMethod (context, type, types);
		}

		protected override SourceLocation DoEvaluateSource (ScriptingContext context)
		{
			Expression[] types = new Expression [arguments.Length];
			TargetType[] argtypes = new TargetType [types.Length];

			for (int i = 0; i < arguments.Length; i++) {
				types [i] = arguments [i].ResolveType (context);
				argtypes [i] = types [i].EvaluateType (context);
			}

			TargetFunctionType func = method_expr.OverloadResolve (context, argtypes);
			if (func != null)
				return new SourceLocation (func);

			return null;
		}

		protected TargetObject DoInvoke (ScriptingContext context, bool debug)
		{
			TargetObject[] args = new TargetObject [arguments.Length];

			for (int i = 0; i < arguments.Length; i++)
				args [i] = arguments [i].EvaluateObject (context);

			TargetObject[] objs = new TargetObject [args.Length];
			for (int i = 0; i < args.Length; i++) {
				objs [i] = Convert.ImplicitConversionRequired (
					context, args [i], method.ParameterTypes [i]);
			}

			TargetClassObject instance = method_expr.InstanceObject;

			if (!method.IsStatic && !method.IsConstructor && (instance == null))
				throw new ScriptingException (
					"Cannot invoke instance method `{0}' with a type reference.",
					method.FullName);

			try {
				Thread thread = context.CurrentThread;
				RuntimeInvokeResult result;

				result = context.Interpreter.RuntimeInvoke (
					thread, method, instance, objs, true, debug);

				if (result == null)
					throw new ScriptingException (
						"Invocation of `{0}' aborted abnormally.", Name);

				if (result.ExceptionMessage != null)
					throw new ScriptingException (
						"Invocation of `{0}' raised an exception: {1}",
						Name, result.ExceptionMessage);

				return result.ReturnObject;
			} catch (TargetException ex) {
				throw new ScriptingException (
					"Invocation of `{0}' raised an exception: {1}", Name, ex.Message);
			}
		}

		public void Invoke (ScriptingContext context, bool debug)
		{
			DoInvoke (context, debug);
		}
	}

	public class NewExpression : Expression
	{
		Expression type_expr;
		Expression[] arguments;
		string name;

		public NewExpression (Expression type_expr, Expression[] arguments)
		{
			this.type_expr = type_expr;
			this.arguments = arguments;

			name = String.Format ("new {0} ()", type_expr.Name);
		}

		public override string Name {
			get { return name; }
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
			type_expr = type_expr.ResolveType (context);
			if (type_expr == null)
				return null;

			for (int i = 0; i < arguments.Length; i++) {
				arguments [i] = arguments [i].Resolve (context);
				if (arguments [i] == null)
					return null;
			}

			resolved = true;
			return this;
		}

		protected override TargetType DoEvaluateType (ScriptingContext context)
		{
			return type_expr.EvaluateType (context);
		}

		protected override TargetObject DoEvaluateObject (ScriptingContext context)
		{
			return Invoke (context);
		}

		public TargetObject Invoke (ScriptingContext context)
		{
			TargetClassType stype = Convert.ToClassType (
				type_expr.EvaluateType (context));

			TargetMethodInfo[] ctors = stype.Constructors;
			TargetFunctionType[] funcs = new TargetFunctionType [ctors.Length];
			for (int i = 0; i < ctors.Length; i++)
				funcs [i] = ctors [i].Type;

			MethodGroupExpression mg = new MethodGroupExpression (
				stype, null, ".ctor", funcs, false, true);

			InvocationExpression invocation = new InvocationExpression (mg, arguments);
			invocation.Resolve (context);

			return invocation.EvaluateObject (context);
		}
	}

	public class AssignmentExpression : Expression
	{
		Expression left, right;
		string name;

		public AssignmentExpression (Expression left, Expression right)
		{
			this.left = left;
			this.right = right;

			name = left.Name + "=" + right.Name;
		}

		public override string Name {
			get { return name; }
		}

		protected override Expression DoResolve (ScriptingContext context)
		{
			left = left.Resolve (context);
			if (left == null)
				return null;

			right = right.Resolve (context);
			if (right == null)
				return null;

			resolved = true;
			return this;
		}

		protected override TargetObject DoEvaluateObject (ScriptingContext context)
		{
			TargetObject obj;
			if (right is NullExpression) {
				StackFrame frame = context.CurrentFrame;
				TargetType ltype = left.EvaluateType (context);
				obj = frame.Language.CreateNullObject (frame.Thread, ltype);
			} else
				obj = right.EvaluateObject (context);

			left.Assign (context, obj);
			return obj;
		}
	}
}