boaold /Backend/AST/Statement.cs

Language C# Lines 1729
MD5 Hash 29231b00b8989c2e303b77271fd8c4f1 Estimated Cost $40,488 (why?)
Repository https://bitbucket.org/AdamMil/boaold View Raw File View Project SPDX
   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
/*
Boa is the reference implementation for a language similar to Python,
also called Boa. This implementation is both interpreted and compiled,
targeting the Microsoft .NET Framework.

http://www.adammil.net/
Copyright (C) 2004-2005 Adam Milazzo

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Reflection;
using System.Reflection.Emit;
using Boa.Runtime;

namespace Boa.AST
{

// TODO: possibly rework compiled closures
// TODO: add versions optimized for System.Array?
// TODO: disallow 'yield' and 'return' outside of functions
/* TODO:
If a variable is referenced in an enclosing scope, it is illegal
to delete the name. An error will be reported at compile time.

If the wild card form of import -- "import *" -- is used in a function and the function
contains or is a nested block with free variables, the compiler will raise a SyntaxError.

If exec is used in a function and the function contains or is a nested block with free
variables, the compiler will raise a SyntaxError unless the exec explicitly specifies the local
namespace for the exec. (In other words, "exec obj" would be illegal, but "exec obj in ns" would be legal.)
*/
// TODO: make sure all enumerators can handle the underlying collection being changed, if possible
// TODO: disallow 'break' and 'continue' inside 'finally' blocks (difficult to implement)
// TODO: allow 'from xxx import yyy/*' inside functions/class defs

// TODO: using exceptions is very slow
#region Exceptions (used to aid implementation)
public class BreakException : Exception
{ public BreakException() { }
  public BreakException(string name) { Name=name; }

  public string Name;

  public static BreakException Value = new BreakException();
}
public class ContinueException : Exception
{ public ContinueException() { }
  public ContinueException(string name) { Name=name; }

  public string Name;

  public static ContinueException Value = new ContinueException();
}
public class ReturnException : Exception
{ public ReturnException(object value) { Value=value; }
  public object Value;
}
#endregion

#region Walkers
#region JumpFinder
sealed class JumpFinder : IWalker
{ public JumpFinder(string name, Label start, Label end) { this.name=name; this.start=start; this.end=end; }

  public void PostWalk(Node node)
  { if(node is ExceptionStatement) inTry--;
  }

  public bool Walk(Node node)
  { if(node is BreakStatement)
    { BreakStatement bs = (BreakStatement)node;
      if(bs.Name==name)
      { bs.Label = end;
        bs.NeedsLeave = InTry;
      }
    }
    else if(node is ContinueStatement)
    { ContinueStatement cs = (ContinueStatement)node;
      if(cs.Name==name)
      { cs.Label = start;
        cs.NeedsLeave = InTry;
      }
    }
    else if(name==null && (node is WhileStatement || node is ForStatement) || node is DefStatement) return false;
    else if(node is ExceptionStatement) inTry++;
    return true;
  }

  bool InTry { get { return inTry>0; } }

  Label start, end;
  string name;
  int inTry;
}
#endregion

#region NameFinder
sealed class NameFinder : IWalker
{ public static Name[] Find(Node n)
  { NameFinder nf = new NameFinder();
    n.Walk(nf);
    return (Name[])nf.names.ToArray(typeof(Name));
  }
  
  public void PostWalk(Node n) { }

  public bool Walk(Node n)
  { if(n is NameExpression) names.Add(((NameExpression)n).Name);
    return true;
  }

  ArrayList names = new ArrayList();
}
#endregion

#region Optimizer
sealed class Optimizer : IWalker
{ public void PostWalk(Node n) { n.Optimize(); }
  public bool Walk(Node n) { return true; }
}
#endregion
#endregion

#region Statement
public abstract class Statement : Node
{ public abstract void Emit(CodeGenerator cg);
  public abstract void Execute(Frame frame);
  
  public void PostProcessForCompile()
  { PostProcess();
    Walk(new NameDecorator());
  }
  public void PostProcessForInterpret() { PostProcess(); }

  void PostProcess()
  { Walk(new JumpChecker());
    Walk(new TryChecker());
    if(Options.Optimize) Walk(new Optimizer());
  }

  #region JumpChecker
  sealed class JumpChecker : IWalker
  { public void PostWalk(Node node)
    { if(node is Suite)
      { if(((Suite)node).Name!=null) blocks.RemoveAt(blocks.Count-1);
      }
      else if(node is WhileStatement || node is ForStatement) blocks.RemoveAt(blocks.Count-1);
      else if(node is DefStatement)
      { if(blocks!=null)
        { blocks.Clear();
          if(flist!=null) flist = new Stack();
          flist.Push(blocks);
          blocks = (ArrayList)lists.Pop();
        }
      }
    }

    public bool Walk(Node node)
    { if(node is Suite)
      { if(((Suite)node).Name!=null)
        { if(blocks==null) blocks = new ArrayList();
          blocks.Add(node);
        }
      }
      else if(node is WhileStatement || node is ForStatement)
      { if(blocks==null) blocks = new ArrayList();
        blocks.Add(node);
      }
      else if(node is JumpStatement)
      { string name = ((JumpStatement)node).Name;
        if(name!=null)
        { if(blocks!=null)
            foreach(object n in blocks)
              if(n is Suite && ((Suite)n).Name==name) return false;
          Ops.SyntaxError(node, "break/continue: unable to find a valid jump target named '{0}'", name);
        }
        else
        { if(blocks!=null)
            for(int i=blocks.Count-1; i>=0; i--)
              if(blocks[i] is WhileStatement || blocks[i] is ForStatement) return false;
          Ops.SyntaxError(node, "break/continue: unable to find a valid enclosing loop");
        }
      }
      else if(node is DefStatement)
      { if(lists==null) lists = new Stack();
        lists.Push(blocks);
        blocks = flist==null || flist.Count==0 ? null : (ArrayList)flist.Pop();
      }
      return true;
    }

    ArrayList blocks;
    Stack lists, flist;
  }
  #endregion

  #region NameDecorator
  sealed class NameDecorator : IWalker
  { public NameDecorator() { }
    NameDecorator(SortedList names) { parentNames=names; }

    public void PostWalk(Node node)
    { if(node==current)
      { inDef=false;

        ArrayList inherit = innerFuncs.Count==0 ? null : new ArrayList();
        foreach(BoaFunction func in innerFuncs)
        { NameDecorator dec = new NameDecorator(names);
          func.Walk(dec);
          foreach(Name dname in dec.names.Values)
            if(dname.Scope==Scope.Free)
            { Name name = (Name)names[dname.String];
              if(name==null) names[dname.String] = dname;
              else if(name.Scope==Scope.Local) inherit.Add(name);
            }
          if(inherit.Count>0)
          { func.Inherit = (Name[])inherit.ToArray(typeof(Name));
            inherit.Clear();
          }
        }
      }
    }

    public bool Walk(Node node)
    { while(node is ParenExpression) node = ((ParenExpression)node).Expression;

      if(inDef)
      { if(node is BoaFunction)
        { BoaFunction fun = (BoaFunction)node;
          innerFuncs.Add(fun);
          if(fun.Name!=null)
          { fun.Name = AddName(fun.Name);
            fun.Name.Scope = Scope.Local;
          }
          for(int i=0; i<fun.Parameters.Length; i++)
            if(fun.Parameters[i].Default!=null) fun.Parameters[i].Default.Walk(this);
          return false;
        }
        else if(node is AssignStatement)
          foreach(Expression e in ((AssignStatement)node).LHS) HandleAssignment(e);
        else if(node is ForStatement) HandleAssignment(((ForStatement)node).Names);
        else if(node is ListCompExpression)
          foreach(ListCompFor f in ((ListCompExpression)node).Fors) HandleAssignment(f.Names);
        else if(node is ExceptionStatement)
        { foreach(ExceptClause ec in ((ExceptionStatement)node).Except)
            if(ec.Target!=null) HandleAssignment(ec.Target);
        }
        else if(node is ImportStatement)
        { ImportStatement imp = (ImportStatement)node;
          foreach(ImportName n in imp.Names) AddName(new Name(n.AsName==null ? n.SlotName : n.AsName, Scope.Local));
        }
        else if(node is ImportFromStatement)
        { ImportFromStatement imp = (ImportFromStatement)node;
          if(imp.Names[0].Name=="*") // TODO: perhaps this should be disallowed within other contexts as well
            Ops.SyntaxError(node, "'from ... import *' is disallowed within function definitions");
          foreach(ImportName n in imp.Names) AddName(new Name(n.AsName==null ? n.Name : n.AsName, Scope.Local));
        }
        else if(node is NameExpression)
        { NameExpression ne = (NameExpression)node;
          ne.Name = AddName(ne.Name);
        }
      }
      else if(node is BoaFunction)
      { if(innerFuncs==null)
        { innerFuncs = new ArrayList();
          names = new SortedList();
        }

        BoaFunction func = (BoaFunction)node;
        foreach(Parameter p in func.Parameters)
        { if(func.Globals!=null)
            for(int i=0; i<func.Globals.Length; i++)
              if(func.Globals[i].String==p.Name.String)
                throw Ops.SyntaxError(node, "'{0}' is both local and global", p.Name.String);
          names[p.Name.String] = p.Name;
        }
        current=func; inDef=true;
      }
      return true;
    }

    Name AddName(Name name)
    { Name lname = (Name)names[name.String];
      if(lname==null)
      { names[name.String] = lname = name;
        if(current.Globals!=null)
          for(int i=0; i<current.Globals.Length; i++)
            if(current.Globals[i].String==name.String)
            { name.Scope = Scope.Global;
              break;
            }
      }
      return lname;
    }
    
    void HandleAssignment(Expression assignedTo)
    { while(assignedTo is ParenExpression) assignedTo = ((ParenExpression)assignedTo).Expression;
      if(assignedTo is NameExpression)
      { NameExpression ne = (NameExpression)assignedTo;
        ne.Name = AddName(ne.Name);
        if(ne.Name.Scope==Scope.Free && (parentNames==null || parentNames[ne.Name.String]==null))
          ne.Name.Scope=Scope.Local;
      }
      else if(assignedTo is TupleExpression)
        foreach(Expression e in ((TupleExpression)assignedTo).Expressions) HandleAssignment(e);
    }

    BoaFunction current;
    ArrayList  innerFuncs;
    SortedList names, parentNames;
    bool inDef;
  }
  #endregion
  
  #region TryChecker
  sealed class TryChecker : IWalker // checks for exceptionless 'raise' outside of a try statement
  { public void PostWalk(Node node) { }

    public bool Walk(Node node)
    { if(inTry)
      { if(node is TryStatement)
        { if(nested==null) nested = new ArrayList();
          nested.Add(node);
        }
        return true;
      }
      else if(node is TryStatement)
      { TryStatement ts = (TryStatement)node;
        ts.Body.Walk(this);
        if(ts.Else!=null) ts.Else.Walk(this);
        if(ts.Finally!=null) ts.Finally.Walk(this);

        inTry = true;
        foreach(ExceptClause ec in ts.Except) ec.Body.Walk(this);
        inTry = false;

        if(nested!=null && nested.Count>0)
        { TryStatement[] nest = (TryStatement[])nested.ToArray(typeof(TryStatement));
          nested.Clear();
          foreach(TryStatement nts in nest) nts.Walk(this);
        }
        return false;
      }
      else if(node is RaiseStatement)
      { if(((RaiseStatement)node).Expression==null)
          throw Ops.SyntaxError(node, "expression-less 'raise' can only occur inside 'except' block");
      }
      else return true;
      return false;
    }

    ArrayList nested;
    bool inTry;
  }
  #endregion
}
#endregion

#region AssertStatement
public class AssertStatement : Statement
{ public AssertStatement(Expression e) { Expression=e; }
  
  public override void Emit(CodeGenerator cg)
  { if(Options.Debug)
    { Label good = cg.ILG.DefineLabel();
      Expression.Emit(cg);
      cg.EmitCall(typeof(Ops), "IsTrue");
      cg.ILG.Emit(OpCodes.Brtrue, good);
      cg.EmitString("assertion failed: ");
      cg.EmitString(Expression.ToCode());
      cg.EmitCall(typeof(string), "Concat", new Type[] { typeof(string), typeof(string) });
      cg.EmitNew(typeof(AssertionErrorException), new Type[] { typeof(string) });
      cg.ILG.Emit(OpCodes.Throw);
      cg.ILG.MarkLabel(good);
    }
  }
  
  public override void Execute(Frame frame)
  { if(Options.Debug && !Ops.IsTrue(Expression.Evaluate(frame)))
      throw Ops.AssertionError(this, "assertion failed: "+Expression.ToCode());
  }

  public override void ToCode(System.Text.StringBuilder sb, int indent)
  { sb.Append("assert ");
    Expression.ToCode(sb, 0);
    sb.Append('\n');
  }

  public override void Walk(IWalker w)
  { if(w.Walk(this)) Expression.Walk(w);
    w.PostWalk(this);
  }

  public Expression Expression;
}
#endregion

// FIXME: make this work: ((a,b)) = ((1,2))
#region AssignStatement
public class AssignStatement : Statement
{ public AssignStatement() { }
  public AssignStatement(Expression lhs) { LHS=new Expression[] { lhs }; }
  public AssignStatement(Expression lhs, Expression rhs) : this(lhs) { RHS=rhs; }
  public AssignStatement(Expression[] lhs, Expression rhs) { LHS=lhs; RHS=rhs; }

  public override void Emit(CodeGenerator cg)
  { if(Op!=null)
    { new BinaryOpExpression(Op, LHS[0], RHS).Emit(cg);
      LHS[0].EmitSet(cg);
      return;
    }

    bool leftAllTups = RHS is TupleExpression;
    if(leftAllTups) foreach(Expression e in LHS) if(!(e is TupleExpression)) { leftAllTups=false; break; }
    if(leftAllTups) // RHS is TupleExpression
    { TupleExpression rhs = (TupleExpression)RHS;
      TupleExpression[] lhs = new TupleExpression[LHS.Length];
      for(int i=0; i<LHS.Length; i++)
      { lhs[i] = (TupleExpression)LHS[i];
        if(lhs[i].Expressions.Length != rhs.Expressions.Length)
          throw Ops.ValueError(this, "wrong number of values to unpack");
      }

      for(int i=0; i<rhs.Expressions.Length; i++) rhs.Expressions[i].Emit(cg);
      for(int i=rhs.Expressions.Length-1; i>=0; i--)
        for(int j=lhs.Length-1; j>=0; j--)
        { if(j!=0) cg.ILG.Emit(OpCodes.Dup);
          lhs[j].Expressions[i].EmitSet(cg);
        }
    }
    else
    { object value=null;
      bool emitted=true;
      if(RHS!=null)
      { if(RHS.IsConstant) { value=RHS.GetValue(); emitted=false; }
        else RHS.Emit(cg);
      }

      for(int ti=LHS.Length-1; ti>=0; ti--)
      { if(LHS[ti] is TupleExpression)
        { TupleExpression lhs = (TupleExpression)LHS[ti];
          if(IsConstant)
          { if(value is ISequence)
            { if(((ISequence)value).__len__() != lhs.Expressions.Length)
                throw Ops.ValueError(this, "wrong number of values to unpack");
            }
            else if(value is string)
            { if(((string)value).Length != lhs.Expressions.Length)
                throw Ops.ValueError(this, "wrong number of values to unpack");
            }
            else
            { object len;
              if(!Ops.TryInvoke(value, "__len__", out len))
                throw Ops.TypeError(this, "expecting a sequence on the right side of a tuple assignment");
              if(Ops.ToInt(len) != lhs.Expressions.Length)
                throw Ops.ValueError(this, "wrong number of values to unpack");
            }
            IEnumerator e = Ops.GetEnumerator(value);
            int i=0;
            while(e.MoveNext() && i<lhs.Expressions.Length)
            { cg.EmitConstant(e.Current);
              lhs.Expressions[i++].EmitSet(cg);
            }
            if(emitted && ti==0) cg.ILG.Emit(OpCodes.Pop);
          }
          else
          { if(!emitted) { RHS.Emit(cg); emitted=true; }
            if(ti!=0) cg.ILG.Emit(OpCodes.Dup);
            
            Slot eslot = cg.AllocLocalTemp(typeof(IEnumerator));
            cg.EmitInt(lhs.Expressions.Length);
            cg.EmitCall(typeof(Ops), "PrepareTupleAssignment");

            for(int i=0; i<lhs.Expressions.Length; i++)
            { if(i!=lhs.Expressions.Length-1) cg.ILG.Emit(OpCodes.Dup);
              cg.ILG.Emit(OpCodes.Dup);
              cg.EmitCall(typeof(IEnumerator), "MoveNext");
              cg.ILG.Emit(OpCodes.Pop); // ignore return value
              cg.EmitPropGet(typeof(IEnumerator), "Current");
              lhs.Expressions[i].EmitSet(cg);
            }

            cg.FreeLocalTemp(eslot);
          }
        }
        else
        { if(!emitted) { RHS.Emit(cg); emitted=true; }
          if(ti!=0) cg.ILG.Emit(OpCodes.Dup);
          LHS[ti].EmitSet(cg);
        }
      }
    }
  }

  public override void Execute(Frame frame)
  { if(Op!=null) LHS[0].Assign(new BinaryOpExpression(Op, LHS[0], RHS).Evaluate(frame), frame);
    else
    { object value = RHS.Evaluate(frame);
      for(int i=LHS.Length-1; i>=0; i--) LHS[i].Assign(value, frame);
    }
  }

  public override object GetValue() { return RHS.GetValue(); }

  public override void Optimize() { IsConstant = RHS!=null && RHS.IsConstant; }

  public override void ToCode(System.Text.StringBuilder sb, int indent)
  { foreach(Expression e in LHS)
    { e.ToCode(sb, 0);
      sb.Append(" = ");
    }
    RHS.ToCode(sb, 0);
  }

  public override void Walk(IWalker w)
  { if(w.Walk(this))
    { foreach(Expression e in LHS) e.Walk(w);
      RHS.Walk(w);
    }
    w.PostWalk(this);
  }

  public Expression[] LHS;
  public Expression   RHS;
  public BinaryOperator Op;
}
#endregion

#region BreakStatement
public class BreakStatement : JumpStatement
{ public BreakStatement() { }
  public BreakStatement(string name) : base(name) { }
  public override void Execute(Frame frame)
  { throw Name==null ? BreakException.Value : new BreakException(Name);
  }
  public override void ToCode(System.Text.StringBuilder sb, int indent)
  { sb.Append("break");
    if(Name!=null) sb.Append(' ').Append(Name);
  }
}
#endregion

// TODO: http://www.python.org/2.2.3/descrintro.html#metaclasses
#region ClassStatement
public class ClassStatement : Statement
{ public ClassStatement(string name, Expression[] bases, Statement body)
  { Name  = new Name(name);
    Bases = bases;
    Body  = body;
    docstring = Misc.BodyToDocString(body);
  }

  public override void Emit(CodeGenerator cg)
  { bool interactive = Options.Interactive;
    Options.Interactive = false;
    try
    { CodeGenerator icg = cg.TypeGenerator.DefineMethod(Name.String+"$maker"+Misc.NextIndex,
                                                        typeof(IDictionary), Type.EmptyTypes);
      LocalNamespace ns = new LocalNamespace(cg.Namespace, icg);
      icg.Namespace = ns;
      Body.Emit(icg);
      ns.EmitLocalsDict(icg);
      icg.EmitReturn();
      icg.Finish();

      cg.EmitGet(new Name("__name__"));
      cg.EmitCall(typeof(Ops), "ToString", new Type[] { typeof(object) }); // FIXME: doesn't handle non-existant __name__
      cg.EmitString(Name.String);
      cg.EmitObjectArray(Bases);
      cg.EmitNew(typeof(Tuple), new Type[] { typeof(object[]) });
      cg.EmitCall((MethodInfo)icg.MethodBase);
      cg.EmitCall(typeof(Ops), "MakeClass");
      if(docstring!=null)
      { cg.ILG.Emit(OpCodes.Dup);
        cg.EmitString("__doc__");
        cg.EmitString(docstring);
        cg.EmitCall(typeof(IHasAttributes), "__setattr__");
      }
      cg.EmitSet(Name);
    }
    finally { Options.Interactive = interactive; }
  }

  public override void Execute(Frame frame)
  { Frame dict = new Frame(frame);
    Body.Execute(dict);
    object[] bases = new object[Bases.Length];
    for(int i=0; i<bases.Length; i++) bases[i] = Bases[i].Evaluate(frame);
    frame.Set(Name.String,
              Ops.MakeClass(Ops.ToString(frame.Get("__name__")), Name.String, new Tuple(bases), dict.Locals));
  }

  public override void ToCode(System.Text.StringBuilder sb, int indent)
  { sb.Append("class ");
    sb.Append(Name.String);
    sb.Append(" (");
    for(int i=0; i<Bases.Length; i++)
    { if(i!=0) sb.Append(", ");
      Bases[i].ToCode(sb, 0);
    }
    sb.Append("):");
    StatementToCode(sb, Body, indent+Options.IndentSize);
  }

  public override void Walk(IWalker w)
  { if(w.Walk(this))
    { foreach(Expression e in Bases) e.Walk(w);
      Body.Walk(w);
    }
    w.PostWalk(this);
  }

  public Name Name;
  public Expression[] Bases;
  public Statement Body;
  
  string docstring;
}
#endregion

#region ContinueStatement
public class ContinueStatement : JumpStatement
{ public ContinueStatement() { }
  public ContinueStatement(string name) : base(name) { }
  public override void Execute(Frame frame)
  { throw Name==null ? ContinueException.Value : new ContinueException(Name);
  }
  public override void ToCode(System.Text.StringBuilder sb, int indent)
  { sb.Append("continue");
    if(Name!=null) sb.Append(' ').Append(Name);
  }
}
#endregion

#region DefStatement
public class DefStatement : Statement
{ public DefStatement(string name, Parameter[] parms, Statement body)
  { Function = new BoaFunction(this, name, parms, body);
    Function.Globals = GlobalFinder.Find(Function.Body);
  }

  public override void Emit(CodeGenerator cg)
  { Slot funcSlot = cg.Namespace.GetSlotForSet(Function.Name);
    Function.Emit(cg);
    funcSlot.EmitSet(cg);
  }

  public override void Execute(Frame frame) { frame.Set(Function.FuncName, Function.MakeFunction(frame)); }

  public override void ToCode(System.Text.StringBuilder sb, int indent)
  { sb.Append("def ");
    sb.Append(Function.Name.String);
    sb.Append("(");
    for(int i=0; i<Function.Parameters.Length; i++)
    { if(i!=0) sb.Append(", ");
      Function.Parameters[i].ToCode(sb);
    }
    sb.Append("):");
    StatementToCode(sb, Function.Body, indent+Options.IndentSize);
  }

  public override void Walk(IWalker w) { Function.Walk(w); }

  public BoaFunction Function;

  #region GlobalFinder
  sealed class GlobalFinder : IWalker
  { public static Name[] Find(Node node)
    { GlobalFinder gf = new GlobalFinder();
      node.Walk(gf);
      return gf.Globals==null || gf.Globals.Count==0 ? null : (Name[])gf.Globals.ToArray(typeof(Name));
    }

    public void PostWalk(Node node) { }

    public bool Walk(Node node)
    { while(node is ParenExpression) node = ((ParenExpression)node).Expression;
      if(node is DefStatement || node is LambdaExpression) return false; // TODO: what if 'def' and 'global' collide?
      else if(node is GlobalStatement)
      { if(Globals==null) Globals = new ArrayList();
        foreach(Name n in ((GlobalStatement)node).Names)
          if(assigned!=null && assigned.Contains(n.String))
            throw Ops.SyntaxError(node, "'{0}' assigned to before associated 'global' statement", n.String);
          else Globals.Add(n);
        return false;
      }
      else if(node is AssignStatement)
      { foreach(Expression e in ((AssignStatement)node).LHS) HandleAssignment(e);
        return false;
      }
      else if(node is ForStatement) HandleAssignment(((ForStatement)node).Names);
      else if(node is ListCompExpression)
        foreach(ListCompFor f in ((ListCompExpression)node).Fors) HandleAssignment(f.Names);
      else if(node is ExceptionStatement)
        foreach(ExceptClause ec in ((ExceptionStatement)node).Except) if(ec.Target!=null) HandleAssignment(ec.Target);
      return true;
    }

    void HandleAssignment(Expression e)
    { while(e is ParenExpression) e = ((ParenExpression)e).Expression;
      if(e is NameExpression)
      { if(assigned==null) assigned=new HybridDictionary();
        assigned[((NameExpression)e).Name.String] = null;
      }
      else if(e is TupleExpression) foreach(Expression te in ((TupleExpression)e).Expressions) HandleAssignment(te);
    }

    ArrayList Globals;
    HybridDictionary assigned;
  }
  #endregion
}
#endregion

#region DelStatement
public class DelStatement : Statement
{ public DelStatement(Expression[] exprs) { Expressions=exprs; }

  public override void Emit(CodeGenerator cg) { foreach(Expression e in Expressions) e.EmitDel(cg); }
  public override void Execute(Frame frame) { foreach(Expression e in Expressions) e.Delete(frame); }

  public override void ToCode(System.Text.StringBuilder sb, int indent)
  { sb.Append("del ");
    for(int i=0; i<Expressions.Length; i++)
    { if(i!=0) sb.Append(", ");
      Expressions[i].ToCode(sb, 0);
    }
  }

  public override void Walk(IWalker w)
  { if(w.Walk(this)) foreach(Expression e in Expressions) e.Walk(w);
    w.PostWalk(this);
  }

  public Expression[] Expressions;
}
#endregion

#region ExceptionStatement
public abstract class ExceptionStatement : Statement
{ public ExceptionStatement(Statement body, ExceptClause[] except) { Body=body; Except=except; }

  public override void Emit(CodeGenerator cg)
  { bool haveExcept = Except!=null && Except.Length!=0;
    Slot fromElse = NeedElse && haveExcept ? cg.AllocLocalTemp(typeof(bool)) : null;

    if(fromElse!=null)
    { cg.EmitInt(0);
      fromElse.EmitSet(cg);
    }

    Slot choice=null;
    if(Yields!=null)
    { choice = cg.AllocLocalTemp(typeof(int));
      Label label = cg.ILG.DefineLabel();
      cg.EmitInt(int.MaxValue);
      choice.EmitSet(cg);
      cg.ILG.Emit(OpCodes.Br, label);
      for(int i=0; i<Yields.Length; i++)
      { YieldStatement ys = Yields[i];
        for(int j=0; j<ys.Targets.Length; j++)
          if(ys.Targets[j].Statement==this) { cg.ILG.MarkLabel(ys.Targets[j].Label); break; }
        cg.EmitInt(i);
        choice.EmitSet(cg);
        if(i!=Yields.Length-1) cg.ILG.Emit(OpCodes.Br, label);
      }
      cg.ILG.MarkLabel(label);
    }

    EmitPreTry(cg);
    Label done = cg.ILG.BeginExceptionBlock();      // try {
    if(Yields!=null)
    { Label[] jumps = new Label[Yields.Length];
      for(int i=0; i<Yields.Length; i++)
      { YieldStatement ys = (YieldStatement)Yields[i];
        for(int j=0; j<ys.Targets.Length; j++)
          if(ys.Targets[j].Statement==this) { jumps[i] = ys.Targets[j+1].Label; break; }
      }
      choice.EmitGet(cg);
      cg.ILG.Emit(OpCodes.Switch, jumps);
      cg.FreeLocalTemp(choice);
    }
    Body.Emit(cg);                                  //   body
    if(NeedElse)                                    //   else
    { if(fromElse!=null)
      { cg.EmitInt(1);
        fromElse.EmitSet(cg);
      }
      EmitElse(cg);
    }

    if(haveExcept)
    { cg.ILG.BeginCatchBlock(typeof(Exception));      // } catch(Exception e) {
      Slot dt = cg.AllocLocalTemp(typeof(DynamicType)), type = cg.AllocLocalTemp(typeof(object)),
          et = cg.AllocLocalTemp(typeof(DynamicType)), e = cg.AllocLocalTemp(typeof(Exception));

      if(fromElse!=null)                              // if(fromElse) throw;
      { Label prop = cg.ILG.DefineLabel();
        fromElse.EmitGet(cg);
        cg.ILG.Emit(OpCodes.Brfalse_S, prop);
        cg.ILG.Emit(OpCodes.Rethrow);
        cg.ILG.MarkLabel(prop);
      }

      if(Except[0].Type!=null)
      { cg.ILG.Emit(OpCodes.Dup);
        e.EmitSet(cg);
        cg.EmitCall(typeof(Ops), "GetDynamicType");     // dt = GetDynamicType(e)
        dt.EmitSet(cg);

        foreach(ExceptClause ec in Except)              // foreach(ExceptClause ec in Except) {
        { if(ec.Type==null) break;
          Label isDT=cg.ILG.DefineLabel(), next=cg.ILG.DefineLabel();
          ec.Type.Emit(cg);                             // type = ec.Type
          cg.ILG.Emit(OpCodes.Dup);
          type.EmitSet(cg);
          cg.ILG.Emit(OpCodes.Isinst, typeof(DynamicType)); // DynamicType et = type as DynamicType
          cg.ILG.Emit(OpCodes.Dup);
          et.EmitSet(cg);
          cg.ILG.Emit(OpCodes.Brtrue, isDT);                // if(et==null) et = Ops.GetDynamicType(type)
          type.EmitGet(cg);
          cg.EmitCall(typeof(Ops), "GetDynamicType");
          et.EmitSet(cg);
          cg.ILG.MarkLabel(isDT);
          et.EmitGet(cg);                                   // if(et.IsSubclassOf(dt)) {
          dt.EmitGet(cg);
          cg.EmitCall(typeof(DynamicType), "IsTryMatch");
          cg.ILG.Emit(OpCodes.Brfalse, next);
          if(ec.Target!=null)                               // ec.Target = e
          { e.EmitGet(cg);
            new AssignStatement(ec.Target).Emit(cg);
          }
          ec.Body.Emit(cg);                                 // ec.Body()
          cg.ILG.Emit(OpCodes.Leave, done);                 // }
          cg.ILG.MarkLabel(next);
        }
      }

      if(Except[Except.Length-1].Type!=null) cg.ILG.Emit(OpCodes.Rethrow);
      else
      { ExceptClause ec = Except[Except.Length-1];
        if(ec.Target!=null)
        { e.EmitGet(cg);
          new AssignStatement(ec.Target).Emit(cg);
        }
        ec.Body.Emit(cg);
      }
      cg.FreeLocalTemp(dt);
      cg.FreeLocalTemp(type);
      cg.FreeLocalTemp(et);
      cg.FreeLocalTemp(e);
    }

    if(NeedFinally)
    { cg.ILG.BeginFinallyBlock();
      EmitFinally(cg);
    }
    cg.ILG.EndExceptionBlock();

    if(fromElse!=null) cg.FreeLocalTemp(fromElse);
  }
  
  public override void Execute(Frame frame)
  { ExecutePreTry(frame);

    bool fromElse=false, occurred=false;
    try
    { Body.Execute(frame);
      if(NeedElse) { fromElse=true; ExecuteElse(frame); }
    }
    catch(BreakException) { throw; }
    catch(ContinueException) { throw; }
    catch(Exception e)
    { if(fromElse) throw;
      occurred = true;
      Boa.Modules.sys.Exceptions.Push(e);

      DynamicType dt = Ops.GetDynamicType(e);
      foreach(ExceptClause ec in Except)
      { DynamicType et;
        if(ec.Type!=null)
        { object type = ec.Type.Evaluate(frame);
          et = type as DynamicType;
          if(et==null) et = Ops.GetDynamicType(type);
        }
        else et=null;
        if(et==null || et.IsSubclassOf(dt))
        { if(ec.Target!=null) ec.Target.Assign(e, frame);
          ec.Body.Execute(frame);
          goto done;
        }
      }
      done:;
    }
    finally
    { if(NeedFinally) ExecuteFinally(frame);
      if(occurred) Boa.Modules.sys.Exceptions.Pop();
    }
  }

  public Statement Body;
  public ExceptClause[] Except;
  public YieldStatement[] Yields;
  
  protected virtual void EmitPreTry(CodeGenerator cg) { }
  protected virtual void EmitBody(CodeGenerator cg) { Body.Emit(cg); }
  protected virtual void EmitElse(CodeGenerator cg) { }
  protected virtual void EmitExcept(CodeGenerator cg) { }
  protected virtual void EmitFinally(CodeGenerator cg) { }
  protected virtual void PostEmit(CodeGenerator cg) { }

  protected virtual void ExecutePreTry(Frame frame) { }
  protected virtual void ExecuteElse(Frame frame) { }
  protected virtual void ExecuteFinally(Frame frame) { }

  protected bool NeedElse, NeedFinally;
}
#endregion

#region ExpressionStatement
public class ExpressionStatement : Statement
{ public ExpressionStatement(Expression expr) { Expression=expr; SetLocation(expr); }

  public override void Emit(CodeGenerator cg)
  { // optimize away expressions that do nothing
    if(Options.Optimize && Expression.IsConstant && !Options.Interactive) return;
    Expression.Emit(cg);
    if(Options.Interactive)
    { Slot temp = cg.AllocLocalTemp(typeof(object));
      temp.EmitSet(cg);
      cg.EmitFieldGet(typeof(Boa.Modules.sys), "displayhook");
      temp.EmitGet(cg);
      cg.EmitCall(typeof(Ops), "Call1");
      cg.FreeLocalTemp(temp);
    }
    cg.ILG.Emit(OpCodes.Pop);
  }

  public override void Execute(Frame frame)
  { if(Options.Optimize && Expression.IsConstant) return;
    object ret = Expression.Evaluate(frame);
    if(Options.Interactive) Ops.Call(Boa.Modules.sys.displayhook, ret);
  }

  public override void ToCode(System.Text.StringBuilder sb, int indent) { Expression.ToCode(sb, indent); }

  public override void Walk(IWalker w)
  { if(w.Walk(this)) Expression.Walk(w);
    w.PostWalk(this);
  }

  public Expression Expression;
}
#endregion

#region IfStatement
public class IfStatement : Statement
{ public IfStatement(Expression test, Statement body, Statement elze) { Test=test; Body=body; Else=elze; }

  public override void Emit(CodeGenerator cg)
  { if(Options.Optimize && Test.IsConstant)
    { if(Ops.IsTrue(Test.GetValue())) Body.Emit(cg);
      else if(Else!=null) Else.Emit(cg);
    }
    else
    { Label endLab = cg.ILG.DefineLabel(), elseLab = Else==null ? new Label() : cg.ILG.DefineLabel();
      if(Options.Optimize && Test is UnaryExpression && ((UnaryExpression)Test).Op==UnaryOperator.LogicalNot)
      { cg.EmitIsTrue(((UnaryExpression)Test).Expression);
        cg.ILG.Emit(OpCodes.Brtrue, Else==null ? endLab : elseLab);
      }
      else
      { cg.EmitIsTrue(Test);
        cg.ILG.Emit(OpCodes.Brfalse, Else==null ? endLab : elseLab);
      }

      Body.Emit(cg);
      if(Else!=null)
      { cg.ILG.Emit(OpCodes.Br, endLab);
        cg.ILG.MarkLabel(elseLab);
        Else.Emit(cg);
      }
      cg.ILG.MarkLabel(endLab);
    }
  }

  public override void Execute(Frame frame)
  { if(Ops.IsTrue(Test.Evaluate(frame))) Body.Execute(frame);
    else if(Else!=null) Else.Execute(frame);
  }

  public override void ToCode(System.Text.StringBuilder sb, int indent) { ToCode(sb, indent, false); }

  public override void Walk(IWalker w)
  { if(w.Walk(this))
    { Test.Walk(w);
      Body.Walk(w);
      if(Else!=null) Else.Walk(w);
    }
    w.PostWalk(this);
  }

  public Expression Test;
  public Statement Body, Else;

  void ToCode(System.Text.StringBuilder sb, int indent, bool elif)
  { sb.Append(elif ? "elif " : "if ");
    Test.ToCode(sb, 0);
    sb.Append(':');
    StatementToCode(sb, Body, indent+Options.IndentSize);
    if(Else!=null)
    { sb.Append(' ', indent);
      if(Else is IfStatement) ((IfStatement)Else).ToCode(sb, indent, true);
      else
      { sb.Append("else:");
        StatementToCode(sb, Else, indent+Options.IndentSize);
      }
    }
  }
}
#endregion

// FIXME: make this be scoped properly in functions, classes, etc
#region ImportFromStatement
public class ImportFromStatement : Statement
{ public ImportFromStatement(string module, params ImportName[] names) { Module=module; Names=names; }

  public override void Emit(CodeGenerator cg)
  { cg.TypeGenerator.ModuleField.EmitGet(cg);
    cg.EmitString(Module);
    if(Names[0].Name=="*") cg.EmitCall(typeof(Ops), "ImportStar");
    else
    { string[] names=new string[Names.Length], asNames=new string[Names.Length];
      for(int i=0; i<Names.Length; i++) { names[i]=Names[i].Name; asNames[i]=Names[i].AsName; }
      cg.EmitStringArray(names);
      cg.EmitStringArray(asNames);
      cg.EmitCall(typeof(Ops), "ImportFrom");
    }
  }

  public override void Execute(Frame frame)
  { if(Names[0].Name=="*") Ops.ImportStar(frame.Module, Module);
    else
    { string[] names=new string[Names.Length], asNames=new string[Names.Length];
      for(int i=0; i<Names.Length; i++) { names[i]=Names[i].Name; asNames[i]=Names[i].AsName; }
      Ops.ImportFrom(frame.Module, Module, names, asNames);
    }
  }

  public override void ToCode(System.Text.StringBuilder sb, int indent)
  { sb.Append("from ");
    sb.Append(Module);
    sb.Append(" import ");
    for(int i=0; i<Names.Length; i++)
    { if(i!=0) sb.Append(", ");
      Names[i].ToCode(sb);
    }
  }

  public ImportName[] Names;
  public string Module;
}
#endregion

#region ImportStatement
public class ImportStatement : Statement
{ public ImportStatement(params ImportName[] names) { Names=names; }

  public override void Emit(CodeGenerator cg)
  { foreach(ImportName n in Names)
    { cg.EmitString(n.Name);
      cg.EmitCall(typeof(Importer), n.AsName==null ? "ImportTop" : "Import", new Type[] { typeof(string) });
      cg.EmitSet(new Name(n.AsName==null ? n.SlotName : n.AsName, Scope.Local));
    }
  }

  public override void Execute(Frame frame)
  { foreach(ImportName n in Names)
      if(n.AsName==null) frame.Set(n.SlotName, Importer.ImportTop(n.Name));
      else frame.Set(n.AsName, Importer.Import(n.Name));
  }

  public override void ToCode(System.Text.StringBuilder sb, int indent)
  { sb.Append("import ");
    for(int i=0; i<Names.Length; i++)
    { if(i!=0) sb.Append(", ");
      Names[i].ToCode(sb);
    }
  }

  public ImportName[] Names;
}
#endregion

#region ForStatement
public class ForStatement : Statement
{ public ForStatement(Name[] names, Expression expr, Statement body, Statement elze)
  { if(names.Length==1) Names = new NameExpression(names[0]);
    else
    { Expression[] ne = new Expression[names.Length];
      for(int i=0; i<names.Length; i++) ne[i] = new NameExpression(names[i]);
      Names=new TupleExpression(ne);
    }
    Expression=expr; Body=body; Else=elze;
  }

  public override void Emit(CodeGenerator cg)
  { AssignStatement ass = new AssignStatement(Names);
    Slot list = cg.AllocLocalTemp(typeof(IEnumerator), true);
    Label start=cg.ILG.DefineLabel(), end=cg.ILG.DefineLabel(), elze=(Else==null ? end : cg.ILG.DefineLabel());

    Body.Walk(new JumpFinder(null, start, end));
    Expression.Emit(cg);
    cg.EmitCall(typeof(Ops), "GetEnumerator", new Type[] { typeof(object) });
    list.EmitSet(cg);
    cg.ILG.BeginExceptionBlock();
    cg.ILG.MarkLabel(start);
    list.EmitGet(cg);
    cg.EmitCall(typeof(IEnumerator), "MoveNext");
    cg.ILG.Emit(OpCodes.Brfalse, elze);
    list.EmitGet(cg);
    cg.EmitPropGet(typeof(IEnumerator), "Current");
    ass.Emit(cg);
    Body.Emit(cg);
    cg.ILG.Emit(OpCodes.Br, start);
    if(Else!=null)
    { cg.ILG.MarkLabel(elze);
      Else.Emit(cg);
    }
    cg.ILG.MarkLabel(end);
    cg.ILG.BeginCatchBlock(typeof(StopIterationException));
    cg.ILG.EndExceptionBlock();
    cg.FreeLocalTemp(list);
  }

  public override void Execute(Frame frame)
  { ConstantExpression ce = new ConstantExpression(null);
    AssignStatement ass = new AssignStatement(Names, ce);
    ass.Optimize();

    IEnumerator e = Ops.GetEnumerator(Expression.Evaluate(frame));
    while(e.MoveNext())
    { ce.Value=e.Current;
      ass.Execute(frame);
      try { Body.Execute(frame); }
      catch(BreakException ex) { if(ex.Name!=null) throw; goto done; }
      catch(StopIterationException) { goto done; }
      catch(ContinueException ex) { if(ex.Name!=null) throw; }
    }
    if(Else!=null) Else.Execute(frame);
    done:;
  }

  public override void ToCode(System.Text.StringBuilder sb, int indent)
  { sb.Append("for ");
    if(Names is TupleExpression)
    { TupleExpression te = (TupleExpression)Names;
      for(int i=0; i<te.Expressions.Length; i++)
      { if(i!=0) sb.Append(',');
        te.Expressions[i].ToCode(sb, 0);
      }
    }
    else Names.ToCode(sb, 0);
    sb.Append(" in ");
    Expression.ToCode(sb, 0);
    sb.Append(':');
    StatementToCode(sb, Body, indent+Options.IndentSize);
  }

  public override void Walk(IWalker w)
  { if(w.Walk(this))
    { Names.Walk(w);
      Expression.Walk(w);
      Body.Walk(w);
      if(Else!=null) Else.Walk(w);
    }
    w.PostWalk(this);
  }

  public Expression Names;
  public Expression Expression;
  public Statement  Body, Else;
}
#endregion

#region GlobalStatement
public class GlobalStatement : Statement
{ public GlobalStatement(string[] names)
  { Names = new Name[names.Length];
    for(int i=0; i<names.Length; i++) Names[i] = new Name(names[i], Scope.Global);
  }

  public override void Emit(CodeGenerator cg) { }
  public override void Execute(Frame frame) { foreach(Name name in Names) frame.MarkGlobal(name.String); }

  public override void ToCode(System.Text.StringBuilder sb, int indent)
  { sb.Append("global ");
    for(int i=0; i<Names.Length; i++)
    { if(i!=0) sb.Append(", ");
      sb.Append(Names[i].String);
    }
  }

  public Name[] Names;
}
#endregion

#region JumpStatement
public abstract class JumpStatement : Statement
{ public JumpStatement() { }
  public JumpStatement(string name) { Name=name; }

  public override void Emit(CodeGenerator cg) { cg.ILG.Emit(NeedsLeave ? OpCodes.Leave : OpCodes.Br, Label); }
  public Label Label;
  public string Name;
  public bool   NeedsLeave;
}
#endregion

#region LockStatement
public class LockStatement : ExceptionStatement
{ public LockStatement(Expression expr, Statement body) : base(body, null) { Expression=expr; NeedFinally=true; }

  public override void Execute(Frame frame)
  { if(Expression is TupleExpression)
    { TupleExpression tup = (TupleExpression)Expression;
      object[] arr = new object[tup.Expressions.Length];
      for(int i=0; i<arr.Length; i++)
      { arr[i] = tup.Expressions[i].Evaluate(frame);
        if(arr[i]==null) throw Ops.ValueError(this, "cannot lock a null value");
      }
      for(int i=0; i<arr.Length; i++) System.Threading.Monitor.Enter(arr[i]);
      try { Body.Execute(frame); }
      finally { foreach(object o in arr) if(o!=null) System.Threading.Monitor.Exit(o); }
    }
    else
    { object o = Expression.Evaluate(frame);
      if(o==null) throw Ops.ValueError(this, "cannot lock a null value");
      lock(o) Body.Execute(frame);
    }
  }

  public override void ToCode(System.Text.StringBuilder sb, int indent)
  { sb.Append("lock ");
    Expression.ToCode(sb, 0);
    sb.Append(":\n");
    StatementToCode(sb, Body, indent+Options.IndentSize);
  }

  public override void Walk(IWalker w)
  { if(w.Walk(this))
    { Expression.Walk(w);
      Body.Walk(w);
    }
    w.PostWalk(this);
  }

  public Expression Expression;

  protected override void EmitPreTry(CodeGenerator cg)
  { Expression[] exprs = Expression is TupleExpression ? ((TupleExpression)Expression).Expressions
                                                       : new Expression[] { Expression };
    Label good=cg.ILG.DefineLabel(), evil=exprs.Length==1 ? new Label() : cg.ILG.DefineLabel();

    slots = new Slot[exprs.Length];
    for(int i=0; i<exprs.Length; i++)
    { slots[i] = cg.AllocLocalTemp(typeof(object), true);
      exprs[i].Emit(cg);
      cg.ILG.Emit(OpCodes.Dup);
      slots[i].EmitSet(cg);
      if(i==exprs.Length-1) cg.ILG.Emit(OpCodes.Brtrue_S, good);
      else cg.ILG.Emit(OpCodes.Brfalse, evil);
    }

    if(exprs.Length!=1) cg.ILG.MarkLabel(evil);
    cg.EmitString("cannot lock a null value");
    cg.EmitNew(typeof(ValueErrorException), new Type[] { typeof(string) });
    cg.ILG.Emit(OpCodes.Throw);

    cg.ILG.MarkLabel(good);
    foreach(Slot s in slots)
    { s.EmitGet(cg);
      cg.EmitCall(typeof(System.Threading.Monitor), "Enter");
    }
  }

  protected override void EmitFinally(CodeGenerator cg)
  { foreach(Slot s in slots)
    { s.EmitGet(cg);
      cg.EmitCall(typeof(System.Threading.Monitor), "Exit");
    }
  }

  protected override void PostEmit(CodeGenerator cg) { foreach(Slot s in slots) cg.FreeLocalTemp(s); }

  Slot[] slots;
}
#endregion

#region PassStatement
public class PassStatement : Statement
{ public override void Emit(CodeGenerator cg) { }
  public override void Execute(Frame frame) { }
  public override void ToCode(System.Text.StringBuilder sb, int indent) { sb.Append("pass"); }
}
#endregion

// FIXME: python puts spaces between arguments
#region PrintStatement
public class PrintStatement : Statement
{ public PrintStatement() { Expressions=null; TrailingNewline=true; }
  public PrintStatement(Expression file) : this() { File=file; }
  public PrintStatement(Expression file, Expression[] exprs, bool trailingNewline)
  { File=file; Expressions=exprs; TrailingNewline=trailingNewline;
  }

  public override void Emit(CodeGenerator cg)
  { Slot file = File==null ? null : cg.AllocLocalTemp(typeof(object));
    if(file!=null)
    { File.Emit(cg);
      file.EmitSet(cg);
    }

    if(Expressions!=null)
      foreach(Expression e in Expressions)
      { if(file==null) cg.ILG.Emit(OpCodes.Ldnull);
        else file.EmitGet(cg);
        e.Emit(cg);
        cg.EmitCall(typeof(Ops), "Print");
      }

    if(TrailingNewline || Expressions.Length==0)
    { if(file==null) cg.ILG.Emit(OpCodes.Ldnull);
      else file.EmitGet(cg);
      cg.EmitCall(typeof(Ops), "PrintNewline");
    }

    if(file!=null) cg.FreeLocalTemp(file);
  }

  public override void Execute(Frame frame)
  { object file = File==null ? null : File.Evaluate(frame);
    if(Expressions!=null) foreach(Expression e in Expressions) Ops.Print(file, e.Evaluate(frame));
    if(TrailingNewline || Expressions.Length==0) Ops.PrintNewline(file);
  }

  public override void ToCode(System.Text.StringBuilder sb, int indent)
  { sb.Append("print ");
    for(int i=0; i<Expressions.Length; i++)
    { if(i!=0) sb.Append(", ");
      Expressions[i].ToCode(sb, 0);
    }
    if(!TrailingNewline) sb.Append(',');
  }

  public override void Walk(IWalker w)
  { if(w.Walk(this) && Expressions!=null) foreach(Expression e in Expressions) e.Walk(w);
    w.PostWalk(this);
  }

  public Expression[] Expressions;
  public Expression   File;
  public bool TrailingNewline;
}
#endregion

// TODO: add source, line, and column to exceptions
#region RaiseStatement
public class RaiseStatement : Statement
{ public RaiseStatement() { }
  public RaiseStatement(Expression e) { Expression=e; }

  public override void Emit(CodeGenerator cg)
  { if(Expression==null) cg.ILG.Emit(OpCodes.Rethrow);
    else
    { Label bad=cg.ILG.DefineLabel();
      Expression.Emit(cg);
      cg.ILG.Emit(OpCodes.Isinst, typeof(Exception));
      cg.ILG.Emit(OpCodes.Dup);
      cg.ILG.Emit(OpCodes.Brfalse_S, bad);
      cg.ILG.Emit(OpCodes.Throw);
      cg.ILG.MarkLabel(bad);
      cg.ILG.Emit(OpCodes.Pop);
      cg.EmitString("exceptions must be derived from System.Exception");
      cg.EmitNew(typeof(TypeErrorException), new Type[] { typeof(string) });
      cg.ILG.Emit(OpCodes.Throw);
    }
  }

  public override void Execute(Frame frame)
  { if(Expression==null) throw (Exception)Boa.Modules.sys.Exceptions.Peek(); // this isn't quite the same...
    else
    { Exception e = Expression.Evaluate(frame) as Exception;
      if(e==null) throw Ops.TypeError("exceptions must be derived from System.Exception");
      throw e;
    }
  }

  public override void ToCode(System.Text.StringBuilder sb, int indent)
  { if(Expression==null) sb.Append("raise");
    else
    { sb.Append("raise ");
      Expression.ToCode(sb, 0);
    }
  }

  public override void Walk(IWalker w)
  { if(w.Walk(this) && Expression!=null) Expression.Walk(w);
    w.PostWalk(this);
  }

  public Expression Expression;
}
#endregion

#region ReturnStatement
public class ReturnStatement : Statement
{ public ReturnStatement() { }
  public ReturnStatement(Expression expression) { Expression=expression; }

  public override void Emit(CodeGenerator cg)
  { if(!InGenerator) cg.EmitReturn(Expression);
    else
    { cg.ILG.Emit(OpCodes.Ldc_I4_0);
      cg.EmitReturn();
    }
  }

  public override void Execute(Frame frame)
  { if(InGenerator) throw new NotImplementedException();
    throw new ReturnException(Expression==null ? null : Expression.Evaluate(frame));
  }

  public override void ToCode(System.Text.StringBuilder sb, int indent)
  { if(Expression==null) sb.Append("return");
    else
    { sb.Append("return ");
      Expression.ToCode(sb, 0);
    }
  }

  public override void Walk(IWalker w)
  { if(w.Walk(this) && Expression!=null) Expression.Walk(w);
    w.PostWalk(this);
  }

  public Expression Expression;
  public bool InGenerator;
}
#endregion

#region Suite
public class Suite : Statement
{ public Suite(Statement[] stmts) { Statements=stmts; SetLocation(stmts[0].Source, stmts[0].Line, stmts[0].Column); }

  public override void Emit(CodeGenerator cg)
  { Label start, end;
    if(Name!=null)
    { start=cg.ILG.DefineLabel(); end=cg.ILG.DefineLabel();
      Walk(new JumpFinder(Name, start, end));
      cg.ILG.MarkLabel(start);
    }
    else end=new Label();

    foreach(Statement stmt in Statements)
    { cg.EmitPosition(stmt);
      stmt.Emit(cg);
    }

    if(Name!=null) cg.ILG.MarkLabel(end);
  }

  public override void Execute(Frame frame)
  { if(Name==null) foreach(Statement stmt in Statements) stmt.Execute(frame);
    else
    { restart:
      try { foreach(Statement stmt in Statements) stmt.Execute(frame); }
      catch(BreakException e) { if(e.Name!=Name) throw; }
      catch(ContinueException e) { if(e.Name==Name) goto restart; throw; }
    }
  }

  public override void ToCode(System.Text.StringBuilder sb, int indent)
  { sb.Append('\n');

    if(Name!=null)
    { sb.Append(Name).Append(":\n");
      indent += Options.IndentSize;
    }

    foreach(Statement s in Statements)
    { if(indent>0) sb.Append(' ', indent);
      s.ToCode(sb, indent);
      if(!(s is IfStatement)) sb.Append('\n');
    }
  }

  public override void Walk(IWalker w)
  { if(w.Walk(this)) foreach(Statement stmt in Statements) stmt.Walk(w);
    w.PostWalk(this);
  }

  public Statement[] Statements;
  public string Name;
}
#endregion

#region TryStatement
public class TryStatement : ExceptionStatement
{ public TryStatement(Statement body, ExceptClause[] except, Statement elze, Statement final)
    : base(body, except) { NeedElse=(elze!=null); NeedFinally=(final!=null); Else=elze; Finally=final; }

  public override void ToCode(System.Text.StringBuilder sb, int indent)
  { sb.Append("try:\n");
    StatementToCode(sb, Body, indent+Options.IndentSize);

    foreach(ExceptClause ec in Except)
    { sb.Append(' ', indent);
      ec.ToCode(sb, 0);
    }
    if(Else!=null)
    { sb.Append(' ', indent);
      sb.Append("else:");
      StatementToCode(sb, Else, indent+Options.IndentSize);
    }
    if(Finally!=null)
    { sb.Append(' ', indent);
      sb.Append("finally:");
      StatementToCode(sb, Finally, indent+Options.IndentSize);
    }
  }

  public override void Walk(IWalker w)
  { if(w.Walk(this))
    { Body.Walk(w);
      foreach(ExceptClause e in Except) e.Walk(w);
      if(Else!=null) Else.Walk(w);
      if(Finally!=null) Finally.Walk(w);
    }
    w.PostWalk(this);
  }

  public Statement Else, Finally;

  protected override void EmitElse(CodeGenerator cg) { Else.Emit(cg); }
  protected override void EmitFinally(CodeGenerator cg) { Finally.Emit(cg); }
  protected override void ExecuteElse(Frame frame) { Else.Execute(frame); }
  protected override void ExecuteFinally(Frame frame) { Finally.Execute(frame); }
}
#endregion

#region UsingStatement
public class UsingStatement : ExceptionStatement
{ public UsingStatement(Expression expr, Statement body) : base(body, null) { Expression=expr; NeedFinally=true; }

  public override void Execute(Frame frame)
  { if(Expression is TupleExpression)
    { TupleExpression tup = (TupleExpression)Expression;
      IDisposable[] arr = new IDisposable[tup.Expressions.Length];
      for(int i=0; i<arr.Length; i++) arr[i] = (IDisposable)tup.Expressions[i].Evaluate(frame);
      try { Body.Execute(frame); }
      finally { foreach(IDisposable o in arr) if(o!=null) o.Dispose(); }
    }
    else using((IDisposable)Expression.Evaluate(frame)) Body.Execute(frame);
  }

  public override void ToCode(System.Text.StringBuilder sb, int indent)
  { sb.Append("using ");
    Expression.ToCode(sb, 0);
    sb.Append(":\n");
    StatementToCode(sb, Body, indent+Options.IndentSize);
  }

  public override void Walk(IWalker w)
  { if(w.Walk(this))
    { Expression.Walk(w);
      Body.Walk(w);
    }
    w.PostWalk(this);
  }

  public Expression Expression;

  protected override void EmitPreTry(CodeGenerator cg)
  { Expression[] exprs = Expression is TupleExpression ? ((TupleExpression)Expression).Expressions
                                                       : new Expression[] { Expression };
    slots = new Slot[exprs.Length];
    for(int i=0; i<exprs.Length; i++)
    { slots[i] = cg.AllocLocalTemp(typeof(IDisposable), true);
      exprs[i].Emit(cg);
      cg.ILG.Emit(OpCodes.Castclass, typeof(IDisposable));
      slots[i].EmitSet(cg);
    }
  }

  protected override void EmitFinally(CodeGenerator cg)
  { foreach(Slot s in slots)
    { Label skip = cg.ILG.DefineLabel();
      s.EmitGet(cg);
      cg.ILG.Emit(OpCodes.Brfalse_S, skip);
      s.EmitGet(cg);
      cg.EmitCall(typeof(IDisposable), "Dispose");
      cg.ILG.MarkLabel(skip);
    }
  }

  protected override void PostEmit(CodeGenerator cg) { foreach(Slot s in slots) cg.FreeLocalTemp(s); }

  Slot[] slots;
}
#endregion

#region WhileStatement
public class WhileStatement : Statement
{ public WhileStatement(Expression test, Statement body, Statement elze) { Test=test; Body=body; Else=elze; }

  public override void Emit(CodeGenerator cg)
  { if(Options.Optimize && Test.IsConstant && !Ops.IsTrue(Test.GetValue())) return;

    Label start=cg.ILG.DefineLabel(), end=cg.ILG.DefineLabel(), elze=(Else==null ? end : cg.ILG.DefineLabel());
    Body.Walk(new JumpFinder(null, start, end));
    cg.ILG.BeginExceptionBlock();
    cg.ILG.MarkLabel(start);
    if(!Options.Optimize || !Test.IsConstant)
    { Test.Emit(cg);
      cg.EmitCall(typeof(Ops), "IsTrue");
      cg.ILG.Emit(OpCodes.Brfalse, elze);
    }
    Body.Emit(cg);
    cg.ILG.Emit(OpCodes.Br, start);
    if(Else!=null)
    { cg.ILG.MarkLabel(elze);
      Else.Emit(cg);
    }
    cg.ILG.MarkLabel(end);
    cg.ILG.BeginCatchBlock(typeof(StopIterationException));
    cg.ILG.EndExceptionBlock();
  }

  public override void Execute(Frame frame)
  { try
    { while(Ops.IsTrue(Test.Evaluate(frame)))
        try { Body.Execute(frame); }
        catch(BreakException e) { if(e.Name!=null) throw; goto done; }
        catch(StopIterationException) { goto done; }
        catch(ContinueException e) { if(e.Name!=null) throw; }
      if(Else!=null) Else.Execute(frame);
      done:;
    }
    catch(StopIterationException) { }
  }

  public override void ToCode(System.Text.StringBuilder sb, int indent)
  { sb.Append("while ");
    Test.ToCode(sb, 0);
    sb.Append(':');
    StatementToCode(sb, Body, indent+Options.IndentSize);
  }

  public override void Walk(IWalker w)
  { if(w.Walk(this))
    { Test.Walk(w);
      Body.Walk(w);
      if(Else!=null) Else.Walk(w);
    }
    w.PostWalk(this);
  }

  public Expression Test;
  public Statement  Body, Else;
}
#endregion

#region YieldStatement
public class YieldStatement : Statement
{ public YieldStatement(Expression e) { Expression=e; }

  public struct YieldTarget
  { public Statement Statement;
    public Label Label;
  }

  public override void Emit(CodeGenerator cg)
  { cg.ILG.Emit(OpCodes.Ldarg_0);
    cg.EmitInt(YieldNumber);
    cg.EmitFieldSet(typeof(Generator).GetField("jump", BindingFlags.Instance|BindingFlags.NonPublic));
    cg.ILG.Emit(OpCodes.Ldarg_1);
    Expression.Emit(cg);
    cg.ILG.Emit(OpCodes.Stind_Ref);
    cg.ILG.Emit(OpCodes.Ldc_I4_1);
    cg.EmitReturn();
    cg.ILG.MarkLabel(Targets[Targets.Length-1].Label);
  }

  public override void Execute(Frame frame)
  { throw new NotImplementedException();
  }

  public override void ToCode(System.Text.StringBuilder sb, int indent)
  { sb.Append("yield ");
    Expression.ToCode(sb, 0);
  }

  public override void Walk(IWalker w)
  { if(w.Walk(this)) Expression.Walk(w);
    w.PostWalk(this);
  }

  public Expression Expression;
  public YieldTarget[] Targets;
  public int YieldNumber;
}
#endregion

} // namespace Boa.AST
Back to Top