PageRenderTime 92ms CodeModel.GetById 21ms app.highlight 58ms RepoModel.GetById 2ms app.codeStats 1ms

/Languages/IronPython/IronPython/Modules/_ast.cs

http://github.com/IronLanguages/main
C# | 3664 lines | 3013 code | 560 blank | 91 comment | 362 complexity | 265edc1ebd043a6c2d7c6242a4637c06 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/* ****************************************************************************
   2 *
   3 * Copyright (c) Jeff Hardy 2010. 
   4 * Copyright (c) Dan Eloff 2008-2009. 
   5 *
   6 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
   7 * copy of the license can be found in the License.html file at the root of this distribution. If 
   8 * you cannot locate the  Apache License, Version 2.0, please send an email to 
   9 * ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
  10 * by the terms of the Apache License, Version 2.0.
  11 *
  12 * You must not remove this notice, or any other, from this software.
  13 *
  14 *
  15 * ***************************************************************************/
  16
  17using System;
  18using System.Collections;
  19using System.Collections.Generic;
  20using Generic = System.Collections.Generic;
  21using System.Diagnostics;
  22using System.Reflection;
  23using System.Runtime.CompilerServices;
  24using IronPython.Compiler;
  25using IronPython.Compiler.Ast;
  26using IronPython.Runtime;
  27using IronPython.Runtime.Operations;
  28using IronPython.Runtime.Types;
  29using IronPython.Runtime.Exceptions;
  30using Microsoft.Scripting;
  31using Microsoft.Scripting.Runtime;
  32using Microsoft.Scripting.Utils;
  33
  34using PyOperator = IronPython.Compiler.PythonOperator;
  35using PythonList = IronPython.Runtime.List;
  36using System.Runtime.InteropServices;
  37using AstExpression = IronPython.Compiler.Ast.Expression;
  38
  39#if FEATURE_NUMERICS
  40using System.Numerics;
  41#else
  42using Microsoft.Scripting.Math;
  43using Complex = Microsoft.Scripting.Math.Complex64;
  44#endif
  45
  46[assembly: PythonModule("_ast", typeof(IronPython.Modules._ast))]
  47namespace IronPython.Modules
  48{
  49    public static class _ast
  50    {
  51        public const string __version__ = "62047";
  52        public const int PyCF_ONLY_AST = 0x400;
  53
  54        private class ThrowingErrorSink : ErrorSink
  55        {
  56            public static new readonly ThrowingErrorSink/*!*/ Default = new ThrowingErrorSink();
  57
  58            private ThrowingErrorSink() {
  59            }
  60
  61            public override void Add(SourceUnit sourceUnit, string message, SourceSpan span, int errorCode, Severity severity) {
  62                if (severity == Severity.Warning) {
  63                    PythonOps.SyntaxWarning(message, sourceUnit, span, errorCode);
  64                } else {
  65                    throw PythonOps.SyntaxError(message, sourceUnit, span, errorCode);
  66                }
  67            }
  68        }
  69
  70        internal static PythonAst ConvertToPythonAst(CodeContext codeContext, AST source ) {
  71            Statement stmt;
  72            PythonCompilerOptions options = new PythonCompilerOptions(ModuleOptions.ExecOrEvalCode);
  73            SourceUnit unit = new SourceUnit(codeContext.LanguageContext, NullTextContentProvider.Null, "", SourceCodeKind.AutoDetect);
  74            CompilerContext compilerContext = new CompilerContext(unit, options, ErrorSink.Default);
  75            bool printExpression = false;
  76
  77            if (source is Expression) {
  78                Expression exp = (Expression)source;
  79                stmt = new ReturnStatement(expr.Revert(exp.body));
  80            } else if (source is Module) {
  81                Module module = (Module)source;
  82                stmt = _ast.stmt.RevertStmts(module.body);
  83            } else if (source is Interactive) {
  84                Interactive interactive = (Interactive)source;
  85                stmt = _ast.stmt.RevertStmts(interactive.body);
  86                printExpression = true;
  87            } else 
  88                throw PythonOps.TypeError("unsupported type of AST: {0}",(source.GetType()));
  89
  90            return new PythonAst(stmt, false, ModuleOptions.ExecOrEvalCode, printExpression, compilerContext, new int[] {} );
  91        }
  92
  93        internal static AST BuildAst(CodeContext context, SourceUnit sourceUnit, PythonCompilerOptions opts, string mode) {
  94            Parser parser = Parser.CreateParser(
  95                new CompilerContext(sourceUnit, opts, ThrowingErrorSink.Default),
  96                (PythonOptions)context.LanguageContext.Options);
  97
  98            PythonAst ast = parser.ParseFile(true);
  99            return ConvertToAST(ast, mode);
 100        }
 101
 102        private static mod ConvertToAST(PythonAst pythonAst, string kind) {
 103            ContractUtils.RequiresNotNull(pythonAst, "pythonAst");
 104            ContractUtils.RequiresNotNull(kind, "kind");
 105            return ConvertToAST((SuiteStatement)pythonAst.Body, kind);
 106        }
 107
 108        private static mod ConvertToAST(SuiteStatement suite, string kind) {
 109            ContractUtils.RequiresNotNull(suite, "suite");
 110            ContractUtils.RequiresNotNull(kind, "kind");
 111            switch (kind) {
 112                case "exec":
 113                    return new Module(suite);
 114                case "eval":
 115                    return new Expression(suite);
 116                case "single":
 117                    return new Interactive(suite);
 118                default:
 119                    throw new ArgumentException("kind must be 'exec' or 'eval' or 'single'");
 120            }
 121        }
 122
 123        [PythonType]
 124        public abstract class AST
 125        {
 126            private PythonTuple __fields = new PythonTuple();   // Genshi assumes _fields in not None
 127            private PythonTuple __attributes = new PythonTuple();   // Genshi assumes _fields in not None
 128            protected int? _lineno; // both lineno and col_offset are expected to be int, in cpython anything is accepted
 129            protected int? _col_offset;
 130
 131            public PythonTuple _fields {
 132                get { return __fields; }
 133                protected set { __fields = value; }
 134            }
 135
 136            public PythonTuple _attributes {
 137                get { return __attributes; }
 138                protected set { __attributes = value; }
 139            }
 140
 141            public int lineno {
 142                get { 
 143                    if (_lineno != null) return (int)_lineno;
 144                    throw PythonOps.AttributeErrorForMissingAttribute(PythonTypeOps.GetName(this), "lineno");
 145                }
 146                set { _lineno = value; }
 147            }
 148
 149            public int col_offset {
 150                get { 
 151                    if (_col_offset != null) return (int)_col_offset;
 152                    throw PythonOps.AttributeErrorForMissingAttribute(PythonTypeOps.GetName(this), "col_offset");
 153                }
 154                set { _col_offset = value; }
 155            }
 156
 157            public void __setstate__(PythonDictionary state) {
 158                restoreProperties(__attributes, state);
 159                restoreProperties(__fields, state);
 160            }
 161
 162            internal void restoreProperties(IEnumerable<object> names, IDictionary source) {
 163                foreach (object name in names) {
 164                    if (name is string) {
 165                        try {
 166                            string key = (string)name;
 167                            this.GetType().GetProperty(key).SetValue(this, source[key], null);
 168                        } catch (Generic.KeyNotFoundException) {
 169                            // ignore missing
 170                        }
 171                    }
 172                }
 173            }
 174
 175            internal void storeProperties(IEnumerable<object> names, IDictionary target) {
 176                foreach (object name in names) {
 177                    if (name is string) {
 178                        string key = (string)name;
 179                        object val;
 180                        try {
 181                            val = this.GetType().GetProperty(key).GetValue(this, null);
 182                            target.Add(key, val);
 183                        } catch (System.Reflection.TargetInvocationException) {
 184                            // field not set
 185                        }
 186                    }
 187                }
 188            }
 189
 190            internal PythonDictionary getstate() {
 191                PythonDictionary d = new PythonDictionary(10);
 192                storeProperties(__fields, d);
 193                storeProperties(__attributes, d);
 194                return d;
 195            }
 196
 197            public virtual object/*!*/ __reduce__() {
 198                return PythonTuple.MakeTuple(DynamicHelpers.GetPythonType(this), new PythonTuple(), getstate());
 199            }
 200
 201            public virtual object/*!*/ __reduce_ex__(int protocol) {
 202                return __reduce__();
 203            }
 204
 205            protected void GetSourceLocation(Node node) {
 206                _lineno = node.Start.Line;
 207
 208                // IronPython counts from 1; CPython counts from 0
 209                _col_offset = node.Start.Column - 1;
 210            }
 211
 212            internal static PythonList ConvertStatements(Statement stmt) {
 213                return ConvertStatements(stmt, false);
 214            }
 215
 216            internal static PythonList ConvertStatements(Statement stmt, bool allowNull) {
 217                if (stmt == null)
 218                    if (allowNull)
 219                        return PythonOps.MakeEmptyList(0);
 220                    else
 221                        throw new ArgumentNullException("stmt");
 222
 223                if (stmt is SuiteStatement) {
 224                    SuiteStatement suite = (SuiteStatement)stmt;
 225                    PythonList list = PythonOps.MakeEmptyList(suite.Statements.Count);
 226                    foreach (Statement s in suite.Statements) 
 227                        if (s is SuiteStatement)  // multiple stmt in a line
 228                            foreach (Statement s2 in ((SuiteStatement)s).Statements)
 229                                list.Add(Convert(s2));
 230                        else 
 231                            list.Add(Convert(s));
 232                    
 233                    return list;
 234                }
 235
 236                return PythonOps.MakeListNoCopy(Convert(stmt));
 237            }
 238
 239
 240            internal static stmt Convert(Statement stmt) {
 241                stmt ast;
 242
 243                if (stmt is FunctionDefinition)
 244                    ast = new FunctionDef((FunctionDefinition)stmt);
 245                else if (stmt is ReturnStatement)
 246                    ast = new Return((ReturnStatement)stmt);
 247                else if (stmt is AssignmentStatement)
 248                    ast = new Assign((AssignmentStatement)stmt);
 249                else if (stmt is AugmentedAssignStatement)
 250                    ast = new AugAssign((AugmentedAssignStatement)stmt);
 251                else if (stmt is DelStatement)
 252                    ast = new Delete((DelStatement)stmt);
 253                else if (stmt is PrintStatement)
 254                    ast = new Print((PrintStatement)stmt);
 255                else if (stmt is ExpressionStatement)
 256                    ast = new Expr((ExpressionStatement)stmt);
 257                else if (stmt is ForStatement)
 258                    ast = new For((ForStatement)stmt);
 259                else if (stmt is WhileStatement)
 260                    ast = new While((WhileStatement)stmt);
 261                else if (stmt is IfStatement)
 262                    ast = new If((IfStatement)stmt);
 263                else if (stmt is WithStatement)
 264                    ast = new With((WithStatement)stmt);
 265                else if (stmt is RaiseStatement)
 266                    ast = new Raise((RaiseStatement)stmt);
 267                else if (stmt is TryStatement)
 268                    ast = Convert((TryStatement)stmt);
 269                else if (stmt is AssertStatement)
 270                    ast = new Assert((AssertStatement)stmt);
 271                else if (stmt is ImportStatement)
 272                    ast = new Import((ImportStatement)stmt);
 273                else if (stmt is FromImportStatement)
 274                    ast = new ImportFrom((FromImportStatement)stmt);
 275                else if (stmt is ExecStatement)
 276                    ast = new Exec((ExecStatement)stmt);
 277                else if (stmt is GlobalStatement)
 278                    ast = new Global((GlobalStatement)stmt);
 279                else if (stmt is ClassDefinition)
 280                    ast = new ClassDef((ClassDefinition)stmt);
 281                else if (stmt is BreakStatement)
 282                    ast = new Break();
 283                else if (stmt is ContinueStatement)
 284                    ast = new Continue();
 285                else if (stmt is EmptyStatement)
 286                    ast = new Pass();
 287                else
 288                    throw new ArgumentTypeException("Unexpected statement type: " + stmt.GetType());
 289
 290                ast.GetSourceLocation(stmt);
 291                return ast;
 292            }
 293
 294            internal static stmt Convert(TryStatement stmt) {
 295                if (stmt.Finally != null) {
 296                    PythonList body;
 297                    if (stmt.Handlers != null && stmt.Handlers.Count != 0) {
 298                        stmt tryExcept = new TryExcept(stmt);
 299                        tryExcept.GetSourceLocation(stmt);
 300                        body = PythonOps.MakeListNoCopy(tryExcept);
 301                    } else
 302                        body = ConvertStatements(stmt.Body);
 303
 304                    return new TryFinally(body, ConvertStatements(stmt.Finally));
 305                }
 306
 307                return new TryExcept(stmt);
 308            }
 309
 310            internal static PythonList ConvertAliases(IList<DottedName> names, IList<string> asnames) {
 311                PythonList list = PythonOps.MakeEmptyList(names.Count);
 312
 313                if (names == FromImportStatement.Star) // does it ever happen?
 314                    list.Add(new alias("*", null));
 315                else
 316                    for (int i = 0; i < names.Count; i++)
 317                        list.Add(new alias(names[i].MakeString(), asnames[i]));
 318
 319                return list;
 320            }
 321
 322            internal static PythonList ConvertAliases(IList<string> names, IList<string> asnames) {
 323                PythonList list = PythonOps.MakeEmptyList(names.Count);
 324
 325                if (names == FromImportStatement.Star)
 326                    list.Add(new alias("*", null));
 327                else
 328                    for (int i = 0; i < names.Count; i++)
 329                        list.Add(new alias(names[i], asnames[i]));
 330
 331                return list;
 332            }
 333
 334            internal static slice TrySliceConvert(AstExpression expr) {
 335                if (expr is SliceExpression)
 336                    return new Slice((SliceExpression)expr);
 337                if (expr is ConstantExpression && ((ConstantExpression)expr).Value == PythonOps.Ellipsis)
 338                    return Ellipsis.Instance;
 339                if (expr is TupleExpression && ((TupleExpression)expr).IsExpandable)
 340                    return new ExtSlice(((Tuple)Convert(expr)).elts);
 341                return null;
 342            }
 343
 344            internal static expr Convert(AstExpression expr) {
 345                return Convert(expr, Load.Instance);
 346            }
 347
 348            internal static expr Convert(AstExpression expr, expr_context ctx) {
 349                expr ast;
 350
 351                if (expr is ConstantExpression)
 352                    ast = Convert((ConstantExpression)expr);
 353                else if (expr is NameExpression)
 354                    ast = new Name((NameExpression)expr, ctx);
 355                else if (expr is UnaryExpression) {
 356                    var unaryOp = new UnaryOp((UnaryExpression)expr);
 357                    ast = unaryOp.TryTrimTrivialUnaryOp();
 358                } else if (expr is BinaryExpression)
 359                    ast = Convert((BinaryExpression)expr);
 360                else if (expr is AndExpression)
 361                    ast = new BoolOp((AndExpression)expr);
 362                else if (expr is OrExpression)
 363                    ast = new BoolOp((OrExpression)expr);
 364                else if (expr is CallExpression)
 365                    ast = new Call((CallExpression)expr);
 366                else if (expr is ParenthesisExpression)
 367                    return Convert(((ParenthesisExpression)expr).Expression);
 368                else if (expr is LambdaExpression)
 369                    ast = new Lambda((LambdaExpression)expr);
 370                else if (expr is ListExpression)
 371                    ast = new List((ListExpression)expr, ctx);
 372                else if (expr is TupleExpression)
 373                    ast = new Tuple((TupleExpression)expr, ctx);
 374                else if (expr is DictionaryExpression)
 375                    ast = new Dict((DictionaryExpression)expr);
 376                else if (expr is ListComprehension)
 377                    ast = new ListComp((ListComprehension)expr);
 378                else if (expr is GeneratorExpression)
 379                    ast = new GeneratorExp((GeneratorExpression)expr);
 380                else if (expr is MemberExpression)
 381                    ast = new Attribute((MemberExpression)expr, ctx);
 382                else if (expr is YieldExpression)
 383                    ast = new Yield((YieldExpression)expr);
 384                else if (expr is ConditionalExpression)
 385                    ast = new IfExp((ConditionalExpression)expr);
 386                else if (expr is IndexExpression)
 387                    ast = new Subscript((IndexExpression)expr, ctx);
 388                else if (expr is BackQuoteExpression)
 389                    ast = new Repr((BackQuoteExpression)expr);
 390                else if (expr is SetExpression)
 391                    ast = new Set((SetExpression)expr);
 392                else if (expr is DictionaryComprehension)
 393                    ast = new DictComp((DictionaryComprehension)expr);
 394                else if (expr is SetComprehension)
 395                    ast = new SetComp((SetComprehension)expr);
 396                else
 397                    throw new ArgumentTypeException("Unexpected expression type: " + expr.GetType());
 398
 399                ast.GetSourceLocation(expr);
 400                return ast;
 401            }
 402
 403            internal static expr Convert(ConstantExpression expr) {
 404                expr ast;
 405
 406                if (expr.Value == null)
 407                    return new Name("None", Load.Instance);
 408
 409                if (expr.Value is int || expr.Value is double || expr.Value is Int64 || expr.Value is BigInteger || expr.Value is Complex)
 410                    ast = new Num(expr.Value);
 411                else if (expr.Value is string)
 412                    ast = new Str((string)expr.Value);
 413                else if (expr.Value is IronPython.Runtime.Bytes)
 414                    ast = new Str(Converter.ConvertToString(expr.Value));
 415
 416                else
 417                    throw new ArgumentTypeException("Unexpected constant type: " + expr.Value.GetType());
 418
 419                return ast;
 420            }
 421
 422            internal static expr Convert(BinaryExpression expr) {
 423                AST op = Convert(expr.Operator);
 424                if (BinaryExpression.IsComparison(expr)) {
 425                    return new Compare(expr);
 426                } else {
 427                    if (op is @operator) {
 428                        return new BinOp(expr, (@operator)op);
 429                    }
 430                }
 431
 432                throw new ArgumentTypeException("Unexpected operator type: " + op.GetType());
 433            }
 434
 435            internal static AST Convert(Node node) {
 436                AST ast;
 437
 438                if (node is TryStatementHandler)
 439                    ast = new ExceptHandler((TryStatementHandler)node);
 440                else
 441                    throw new ArgumentTypeException("Unexpected node type: " + node.GetType());
 442
 443                ast.GetSourceLocation(node);
 444                return ast;
 445            }
 446
 447            internal static PythonList Convert(IList<ComprehensionIterator> iterators) {
 448                ComprehensionIterator[] iters = new ComprehensionIterator[iterators.Count];
 449                iterators.CopyTo(iters, 0);
 450
 451                PythonList comps = new PythonList();
 452                int start = 1;
 453                for (int i = 0; i < iters.Length; i++) {
 454                    if (i == 0 || iters[i] is ComprehensionIf)
 455                        if (i == iters.Length - 1)
 456                            i++;
 457                        else
 458                            continue;
 459
 460                    ComprehensionIf[] ifs = new ComprehensionIf[i - start];
 461                    Array.Copy(iters, start, ifs, 0, ifs.Length);
 462                    comps.Add(new comprehension((ComprehensionFor)iters[start - 1], ifs));
 463                    start = i + 1;
 464                }
 465                return comps;
 466            }
 467
 468            internal static PythonList Convert(ComprehensionIterator[] iters) {
 469                Generic.List<ComprehensionFor> cfCollector =
 470                    new Generic.List<ComprehensionFor>();
 471                Generic.List<Generic.List<ComprehensionIf>> cifCollector =
 472                    new Generic.List<Generic.List<ComprehensionIf>>();
 473                Generic.List<ComprehensionIf> cif = null;
 474                for (int i = 0; i < iters.Length; i++) {
 475                    if (iters[i] is ComprehensionFor) {
 476                        ComprehensionFor cf = (ComprehensionFor)iters[i];
 477                        cfCollector.Add(cf);
 478                        cif = new Generic.List<ComprehensionIf>();
 479                        cifCollector.Add(cif);
 480                    } else {
 481                        ComprehensionIf ci = (ComprehensionIf)iters[i];
 482                        cif.Add(ci);
 483                    }
 484                }
 485
 486                PythonList comps = new PythonList();
 487                for (int i = 0; i < cfCollector.Count; i++)
 488                    comps.Add(new comprehension(cfCollector[i], cifCollector[i].ToArray()));
 489                return comps;
 490            }
 491
 492
 493
 494            internal static AST Convert(PyOperator op) {
 495                // We treat operator classes as singletons here to keep overhead down
 496                // But we cannot fully make them singletons if we wish to keep compatibility wity CPython
 497                switch (op) {
 498                    case PyOperator.Add:
 499                        return Add.Instance;
 500                    case PyOperator.BitwiseAnd:
 501                        return BitAnd.Instance;
 502                    case PyOperator.BitwiseOr:
 503                        return BitOr.Instance;
 504                    case PyOperator.Divide:
 505                        return Div.Instance;
 506                    case PyOperator.TrueDivide:
 507                        return TrueDivide.Instance;
 508                    case PyOperator.Equal:
 509                        return Eq.Instance;
 510                    case PyOperator.FloorDivide:
 511                        return FloorDiv.Instance;
 512                    case PyOperator.GreaterThan:
 513                        return Gt.Instance;
 514                    case PyOperator.GreaterThanOrEqual:
 515                        return GtE.Instance;
 516                    case PyOperator.In:
 517                        return In.Instance;
 518                    case PyOperator.Invert:
 519                        return Invert.Instance;
 520                    case PyOperator.Is:
 521                        return Is.Instance;
 522                    case PyOperator.IsNot:
 523                        return IsNot.Instance;
 524                    case PyOperator.LeftShift:
 525                        return LShift.Instance;
 526                    case PyOperator.LessThan:
 527                        return Lt.Instance;
 528                    case PyOperator.LessThanOrEqual:
 529                        return LtE.Instance;
 530                    case PyOperator.Mod:
 531                        return Mod.Instance;
 532                    case PyOperator.Multiply:
 533                        return Mult.Instance;
 534                    case PyOperator.Negate:
 535                        return USub.Instance;
 536                    case PyOperator.Not:
 537                        return Not.Instance;
 538                    case PyOperator.NotEqual:
 539                        return NotEq.Instance;
 540                    case PyOperator.NotIn:
 541                        return NotIn.Instance;
 542                    case PyOperator.Pos:
 543                        return UAdd.Instance;
 544                    case PyOperator.Power:
 545                        return Pow.Instance;
 546                    case PyOperator.RightShift:
 547                        return RShift.Instance;
 548                    case PyOperator.Subtract:
 549                        return Sub.Instance;
 550                    case PyOperator.Xor:
 551                        return BitXor.Instance;
 552                    default:
 553                        throw new ArgumentException("Unexpected PyOperator: " + op, "op");
 554                }
 555            }
 556        }
 557
 558        [PythonType]
 559        public class alias : AST
 560        {
 561            private string _name;
 562            private string _asname; // Optional
 563
 564            public alias() {
 565                _fields = new PythonTuple(new[] { "name", "asname" });
 566            }
 567
 568            internal alias(string name, [Optional]string asname)
 569                : this() {
 570                _name = name;
 571                _asname = asname;
 572            }
 573
 574            public string name {
 575                get { return _name; }
 576                set { _name = value; }
 577            }
 578
 579            public string asname {
 580                get { return _asname; }
 581                set { _asname = value; }
 582            }
 583        }
 584
 585        [PythonType]
 586        public class arguments : AST
 587        {
 588            private PythonList _args;
 589            private string _vararg; // Optional
 590            private string _kwarg; // Optional
 591            private PythonList _defaults;
 592
 593            public arguments() {
 594                _fields = new PythonTuple(new[] { "args", "vararg", "kwarg", "defaults" });
 595            }
 596
 597            public arguments(PythonList args, [Optional]string vararg, [Optional]string kwarg, PythonList defaults)
 598                :this() {
 599                _args = args;
 600                _vararg = vararg;
 601                _kwarg = kwarg;
 602                _defaults = defaults;
 603            }
 604
 605            internal arguments(IList<Parameter> parameters)
 606                : this() {
 607                _args = PythonOps.MakeEmptyList(parameters.Count);
 608                _defaults = PythonOps.MakeEmptyList(parameters.Count);
 609                foreach (Parameter param in parameters) {
 610                    if (param.IsList)
 611                        _vararg = param.Name;
 612                    else if (param.IsDictionary)
 613                        _kwarg = param.Name;
 614                    else {
 615                        args.Add(new Name(param));
 616                        if (param.DefaultValue != null)
 617                            defaults.Add(Convert(param.DefaultValue));
 618                    }
 619                }
 620            }
 621
 622
 623            internal arguments(Parameter[] parameters)
 624                : this(parameters as IList<Parameter>) {
 625            }
 626
 627            internal Parameter[] Revert() {
 628                List<Parameter> parameters = new List<Parameter>();
 629                int argIdx = args.Count - 1;
 630                for (int defIdx = defaults.Count - 1; defIdx >= 0; defIdx--, argIdx--) {
 631                    Name name = (Name)args[argIdx];
 632                    Parameter p = new Parameter(name.id);
 633                    p.DefaultValue = expr.Revert(defaults[defIdx]);
 634                    parameters.Add(p);
 635                }
 636                while (argIdx >= 0) {
 637                    Name name = (Name)args[argIdx--];
 638                    parameters.Add(new Parameter(name.id));
 639                }
 640                parameters.Reverse();
 641                if (vararg != null)
 642                    parameters.Add(new Parameter(vararg, ParameterKind.List));
 643                if (kwarg != null)
 644                    parameters.Add(new Parameter(kwarg, ParameterKind.Dictionary));
 645                return parameters.ToArray();
 646            }
 647
 648            public PythonList args {
 649                get { return _args; }
 650                set { _args = value; }
 651            }
 652
 653            public string vararg {
 654                get { return _vararg; }
 655                set { _vararg = value; }
 656            }
 657
 658            public string kwarg {
 659                get { return _kwarg; }
 660                set { _kwarg = value; }
 661            }
 662
 663            public PythonList defaults {
 664                get { return _defaults; }
 665                set { _defaults = value; }
 666            }
 667        }
 668
 669        [PythonType]
 670        public abstract class boolop : AST
 671        {
 672        }
 673
 674        [PythonType]
 675        public abstract class cmpop : AST
 676        {
 677            internal PythonOperator Revert() {
 678                if (this == Eq.Instance)
 679                    return PyOperator.Equal;
 680                if (this == Gt.Instance)
 681                    return PyOperator.GreaterThan;
 682                if (this == GtE.Instance)
 683                    return PyOperator.GreaterThanOrEqual;
 684                if (this == In.Instance)
 685                    return PyOperator.In;
 686                if (this == Is.Instance)
 687                    return PyOperator.Is;
 688                if (this == IsNot.Instance)
 689                    return PyOperator.IsNot;
 690                if (this == Lt.Instance)
 691                    return PyOperator.LessThan;
 692                if (this == LtE.Instance)
 693                    return PyOperator.LessThanOrEqual;
 694                if (this == NotEq.Instance)
 695                    return PythonOperator.NotEqual;
 696                if (this == NotIn.Instance)
 697                    return PythonOperator.NotIn;
 698                throw PythonOps.TypeError("Unexpected compare operator: {0}", GetType());
 699            }
 700
 701        }
 702
 703        [PythonType]
 704        public class comprehension : AST
 705        {
 706            private expr _target;
 707            private expr _iter;
 708            private PythonList _ifs;
 709
 710            public comprehension() {
 711                _fields = new PythonTuple(new[] { "target", "iter", "ifs" });
 712            }
 713
 714            public comprehension(expr target, expr iter, PythonList ifs)
 715                : this() {
 716                _target = target;
 717                _iter = iter;
 718                _ifs = ifs;
 719            }
 720
 721            internal comprehension(ComprehensionFor listFor, ComprehensionIf[] listIfs)
 722                : this() {
 723                _target = Convert(listFor.Left, Store.Instance);
 724                _iter = Convert(listFor.List);
 725                _ifs = PythonOps.MakeEmptyList(listIfs.Length);
 726                foreach (ComprehensionIf listIf in listIfs)
 727                    _ifs.Add(Convert(listIf.Test));
 728            }
 729            
 730            internal static ComprehensionIterator[] RevertComprehensions(PythonList comprehensions) {
 731                Generic.List<ComprehensionIterator> comprehensionIterators =
 732                    new Generic.List<ComprehensionIterator>();
 733                foreach (comprehension comp in comprehensions) {
 734                    ComprehensionFor cf = new ComprehensionFor(expr.Revert(comp.target), expr.Revert(comp.iter));
 735                    comprehensionIterators.Add(cf);
 736                    foreach (expr ifs in comp.ifs) {
 737                        comprehensionIterators.Add(new ComprehensionIf(expr.Revert(ifs)));
 738                    }
 739                }
 740                return comprehensionIterators.ToArray();
 741            }
 742
 743            public expr target {
 744                get { return _target; }
 745                set { _target = value; }
 746            }
 747
 748            public expr iter {
 749                get { return _iter; }
 750                set { _iter = value; }
 751            }
 752
 753            public PythonList ifs {
 754                get { return _ifs; }
 755                set { _ifs = value; }
 756            }
 757        }
 758
 759        [PythonType]
 760        public class excepthandler : AST
 761        {
 762            public excepthandler() {
 763                _attributes = new PythonTuple(new[] { "lineno", "col_offset" });
 764            }
 765        }
 766
 767        [PythonType]
 768        public abstract class expr : AST
 769        {
 770            protected expr() {
 771                _attributes = new PythonTuple(new[] { "lineno", "col_offset" });
 772            }
 773
 774            internal virtual AstExpression Revert() {
 775                throw PythonOps.TypeError("Unexpected expr type: {0}", GetType());
 776            }
 777
 778            internal static AstExpression Revert(expr ex) {
 779                if (ex == null)
 780                    return null;
 781                return ex.Revert();
 782            }
 783
 784            internal static AstExpression Revert(object ex) {
 785                if (ex == null)
 786                    return null;
 787                Debug.Assert(ex is expr);
 788                return ((expr)ex).Revert();
 789            }
 790
 791            internal static AstExpression[] RevertExprs(PythonList exprs) {
 792                // it is assumed that list elements are expr
 793                AstExpression[] ret = new AstExpression[exprs.Count];
 794                for (int i = 0; i < exprs.Count; i++)
 795                    ret[i] = ((expr)exprs[i]).Revert();
 796                return ret;
 797            }
 798
 799        }
 800
 801        [PythonType]
 802        public abstract class expr_context : AST
 803        {
 804        }
 805
 806        [PythonType]
 807        public class keyword : AST
 808        {
 809            private string _arg;
 810            private expr _value;
 811
 812            public keyword() {
 813                _fields = new PythonTuple(new[] { "arg", "value" });
 814            }
 815
 816            public keyword(string arg, expr value)
 817                : this() {
 818                _arg = arg;
 819                _value = value;
 820            }
 821
 822            internal keyword(IronPython.Compiler.Ast.Arg arg)
 823                : this() {
 824                _arg = arg.Name;
 825                _value = Convert(arg.Expression);
 826            }
 827
 828            public string arg {
 829                get { return _arg; }
 830                set { _arg = value; }
 831            }
 832
 833            public expr value {
 834                get { return _value; }
 835                set { _value = value; }
 836            }
 837        }
 838
 839        [PythonType]
 840        public abstract class mod : AST
 841        {
 842            internal abstract PythonList GetStatements();
 843        }
 844
 845        [PythonType]
 846        public abstract class @operator : AST 
 847        {
 848            internal PythonOperator Revert() {
 849                if (this == Add.Instance)
 850                    return PythonOperator.Add;
 851                if (this == BitAnd.Instance)
 852                    return PyOperator.BitwiseAnd;
 853                if (this == BitOr.Instance)
 854                    return PyOperator.BitwiseOr;
 855                if (this == Div.Instance)
 856                    return PyOperator.Divide;
 857                if (this == FloorDiv.Instance)
 858                    return PyOperator.FloorDivide;
 859                if (this == LShift.Instance)
 860                    return PyOperator.LeftShift;
 861                if (this == Mod.Instance)
 862                    return PythonOperator.Mod;
 863                if (this == Mult.Instance)
 864                    return PythonOperator.Multiply;
 865                if (this == Pow.Instance)
 866                    return PythonOperator.Power;
 867                if (this == RShift.Instance)
 868                    return PyOperator.RightShift;
 869                if (this == Sub.Instance)
 870                    return PythonOperator.Subtract;
 871                if (this == BitXor.Instance)
 872                    return PythonOperator.Xor;
 873                throw PythonOps.TypeError("Unexpected unary operator: {0}", GetType());
 874            }
 875
 876        }
 877
 878        [PythonType]
 879        public abstract class slice : AST
 880        {
 881        }
 882
 883        [PythonType]
 884        public abstract class stmt : AST
 885        {
 886            protected stmt() {
 887                _attributes = new PythonTuple(new[] { "lineno", "col_offset" });
 888            }
 889
 890            internal virtual Statement Revert() {
 891                throw PythonOps.TypeError("Unexpected statement type: {0}", GetType());
 892            }
 893
 894            internal static Statement RevertStmts(PythonList stmts) {
 895                if (stmts.Count == 1)
 896                    return ((stmt)stmts[0]).Revert();
 897                Statement[] statements = new Statement[stmts.Count];
 898                for (int i = 0; i < stmts.Count; i++)
 899                    statements[i] = ((stmt)stmts[i]).Revert();
 900                return new SuiteStatement(statements);
 901            }
 902        }
 903
 904        [PythonType]
 905        public abstract class unaryop : AST
 906        {
 907            internal PyOperator Revert() {
 908                if (this == Invert.Instance)
 909                    return PyOperator.Invert;
 910                if (this == USub.Instance)
 911                    return PythonOperator.Negate;
 912                if (this == Not.Instance)
 913                    return PythonOperator.Not;
 914                if (this == UAdd.Instance)
 915                    return PythonOperator.Pos;
 916                throw PythonOps.TypeError("Unexpected unary operator: {0}", GetType());
 917            }
 918        }
 919
 920        [PythonType]
 921        public class Add : @operator
 922        {
 923            internal static Add Instance = new Add();
 924        }
 925
 926        [PythonType]
 927        public class And : boolop
 928        {
 929            internal static And Instance = new And();
 930        }
 931
 932        [PythonType]
 933        public class Assert : stmt
 934        {
 935            private expr _test;
 936            private expr _msg; // Optional
 937
 938            public Assert() {
 939                _fields = new PythonTuple(new[] { "test", "msg" });
 940            }
 941
 942            public Assert(expr test, expr msg, [Optional]int? lineno, [Optional]int? col_offset)
 943                : this() {
 944                _test = test;
 945                _msg = msg;
 946                _lineno = lineno;
 947                _col_offset = col_offset;
 948            }
 949
 950            internal Assert(AssertStatement stmt)
 951                : this() {
 952                _test = Convert(stmt.Test);
 953                if (stmt.Message != null)
 954                    _msg = Convert(stmt.Message);
 955            }
 956
 957            internal override Statement Revert() {
 958                return new AssertStatement(expr.Revert(test), expr.Revert(msg));
 959            }
 960
 961            public expr test {
 962                get { return _test; }
 963                set { _test = value; }
 964            }
 965
 966            public expr msg {
 967                get { return _msg; }
 968                set { _msg = value; }
 969            }
 970        }
 971
 972        [PythonType]
 973        public class Assign : stmt
 974        {
 975            private PythonList _targets;
 976            private expr _value;
 977
 978            public Assign() {
 979                _fields = new PythonTuple(new[] { "targets", "value" });
 980            }
 981
 982            public Assign(PythonList targets, expr value, [Optional]int? lineno, [Optional]int? col_offset)
 983                : this() {
 984                _targets = targets;
 985                _value = value;
 986                _lineno = lineno;
 987                _col_offset = col_offset;
 988            }
 989
 990            internal Assign(AssignmentStatement stmt)
 991                : this() {
 992                _targets = PythonOps.MakeEmptyList(stmt.Left.Count);
 993                foreach (AstExpression expr in stmt.Left)
 994                    _targets.Add(Convert(expr, Store.Instance));
 995
 996                _value = Convert(stmt.Right);
 997            }
 998            
 999            internal override Statement Revert() {
1000                return new AssignmentStatement(expr.RevertExprs(targets), expr.Revert(value));
1001            }
1002
1003            public PythonList targets {
1004                get { return _targets; }
1005                set { _targets = value; }
1006            }
1007
1008            public expr value {
1009                get { return _value; }
1010                set { _value = value; }
1011            }
1012        }
1013
1014        [PythonType]
1015        public class Attribute : expr
1016        {
1017            private expr _value;
1018            private string _attr;
1019            private expr_context _ctx;
1020
1021            public Attribute() {
1022                _fields = new PythonTuple(new[] { "value", "attr", "ctx" });
1023            }
1024
1025            public Attribute(expr value, string attr, expr_context ctx,
1026                [Optional]int? lineno, [Optional]int? col_offset)
1027                : this() {
1028                _value = value;
1029                _attr = attr;
1030                _ctx = ctx;
1031                _lineno = lineno;
1032                _col_offset = col_offset;
1033            }
1034
1035            internal Attribute(MemberExpression attr, expr_context ctx)
1036                : this() {
1037                _value = Convert(attr.Target);
1038                _attr = attr.Name;
1039                _ctx = ctx;
1040            }
1041         
1042            internal override AstExpression Revert() {
1043                return new MemberExpression(expr.Revert(value), attr);
1044            }
1045
1046            public expr value {
1047                get { return _value; }
1048                set { _value = value; }
1049            }
1050
1051            public string attr {
1052                get { return _attr; }
1053                set { _attr = value; }
1054            }
1055
1056            public expr_context ctx {
1057                get { return _ctx; }
1058                set { _ctx = value; }
1059            }
1060        }
1061
1062        [PythonType]
1063        public class AugAssign : stmt
1064        {
1065            private expr _target;
1066            private @operator _op;
1067            private expr _value;
1068
1069            public AugAssign() {
1070                _fields = new PythonTuple(new[] { "target", "op", "value" });
1071            }
1072
1073            public AugAssign(expr target, @operator op, expr value,
1074                [Optional]int? lineno, [Optional]int? col_offset)
1075                : this() {
1076                _target = target;
1077                _op = op;
1078                _value = value;
1079                _lineno = lineno;
1080                _col_offset = col_offset;
1081            }
1082
1083            internal AugAssign(AugmentedAssignStatement stmt)
1084                : this() {
1085                _target = Convert(stmt.Left, Store.Instance);
1086                _value = Convert(stmt.Right);
1087                _op = (@operator)Convert(stmt.Operator);
1088            }
1089
1090            internal override Statement Revert() {
1091                return new AugmentedAssignStatement(op.Revert(), expr.Revert(target), expr.Revert(value));
1092            }
1093
1094            public expr target {
1095                get { return _target; }
1096                set { _target = value; }
1097            }
1098
1099            public @operator op {
1100                get { return _op; }
1101                set { _op = value; }
1102            }
1103
1104            public expr value {
1105                get { return _value; }
1106                set { _value = value; }
1107            }
1108        }
1109
1110        /// <summary>
1111        /// Not used.
1112        /// </summary>
1113        [PythonType]
1114        public class AugLoad : expr_context
1115        {
1116        }
1117
1118        /// <summary>
1119        /// Not used.
1120        /// </summary>
1121        [PythonType]
1122        public class AugStore : expr_context
1123        {
1124        }
1125
1126        [PythonType]
1127        public class BinOp : expr
1128        {
1129            private expr _left;
1130            private expr _right;
1131            private @operator _op;
1132
1133            public BinOp() {
1134                _fields = new PythonTuple(new[] { "left", "op", "right" });
1135            }
1136
1137            public BinOp(expr left, @operator op, expr right, [Optional]int? lineno, [Optional]int? col_offset)
1138                : this() {
1139                _left = left;
1140                _op = op;
1141                _right = right;
1142                _lineno = lineno;
1143                _col_offset = col_offset;
1144            }
1145
1146            internal BinOp(BinaryExpression expr, @operator op)
1147                : this() {
1148                _left = Convert(expr.Left);
1149                _right = Convert(expr.Right);
1150                _op = op;
1151            }
1152
1153            internal override AstExpression Revert() {
1154                return new BinaryExpression(op.Revert(), expr.Revert(left), expr.Revert(right));
1155            }
1156
1157            public expr left {
1158                get { return _left; }
1159                set { _left = value; }
1160            }
1161
1162            public expr right {
1163                get { return _right; }
1164                set { _right = value; }
1165            }
1166
1167            public @operator op {
1168                get { return _op; }
1169                set { _op = value; }
1170            }
1171        }
1172
1173        [PythonType]
1174        public class BitAnd : @operator
1175        {
1176            internal static BitAnd Instance = new BitAnd();
1177        }
1178
1179        [PythonType]
1180        public class BitOr : @operator
1181        {
1182            internal static BitOr Instance = new BitOr();
1183        }
1184
1185        [PythonType]
1186        public class BitXor : @operator
1187        {
1188            internal static BitXor Instance = new BitXor();
1189        }
1190
1191        [PythonType]
1192        public class BoolOp : expr
1193        {
1194            private boolop _op;
1195            private PythonList _values;
1196
1197            public BoolOp() {
1198                _fields = new PythonTuple(new[] { "op", "values" });
1199            }
1200
1201            public BoolOp(boolop op, PythonList values, [Optional]int? lineno, [Optional]int? col_offset)
1202                : this() {
1203                _op = op;
1204                _values = values;
1205                _lineno = lineno;
1206                _col_offset = col_offset;
1207            }
1208
1209            internal BoolOp(AndExpression and)
1210                : this() {
1211                _values = PythonOps.MakeListNoCopy(Convert(and.Left), Convert(and.Right));
1212                _op = And.Instance;
1213            }
1214
1215            internal BoolOp(OrExpression or)
1216                : this() {
1217                _values = PythonOps.MakeListNoCopy(Convert(or.Left), Convert(or.Right));
1218                _op = Or.Instance;
1219            }
1220
1221            internal override AstExpression Revert() {
1222                if (op == And.Instance) {
1223                    AndExpression ae = new AndExpression(
1224                        expr.Revert(values[0]),
1225                        expr.Revert(values[1]));
1226                    return ae;
1227                } else if (op == Or.Instance) {
1228                    OrExpression oe = new OrExpression(
1229                        expr.Revert(values[0]),
1230                        expr.Revert(values[1]));
1231                    return oe;
1232                }
1233                throw PythonOps.TypeError("Unexpected boolean operator: {0}", op);
1234            }
1235
1236            public boolop op {
1237                get { return _op; }
1238                set { _op = value; }
1239            }
1240
1241            public PythonList values {
1242                get { return _values; }
1243                set { _values = value; }
1244            }
1245        }
1246
1247        [PythonType]
1248        public class Break : stmt
1249        {
1250            internal static Break Instance = new Break();
1251
1252            internal Break()
1253                : this(null, null) { }
1254
1255            public Break([Optional]int? lineno, [Optional]int? col_offset) {
1256                _lineno = lineno;
1257                _col_offset = col_offset;
1258            }
1259
1260            internal override Statement Revert() {
1261                return new BreakStatement();
1262            }
1263        }
1264
1265        [PythonType]
1266        public class Call : expr
1267        {
1268            private expr _func;
1269            private PythonList _args;
1270            private PythonList _keywords;
1271            private expr _starargs; // Optional
1272            private expr _kwargs; // Optional
1273
1274            public Call() {
1275                _fields = new PythonTuple(new[] { "func", "args", "keywords", "starargs", "kwargs" });
1276            }
1277
1278            public Call( expr func, PythonList args, PythonList keywords, 
1279                [Optional]expr starargs, [Optional]expr kwargs,
1280                [Optional]int? lineno, [Optional]int? col_offset) 
1281                :this() {
1282                _func = func;
1283                _args = args;
1284                _keywords = keywords;
1285                _starargs = starargs;
1286                _kwargs = kwargs;
1287                _lineno = lineno;
1288                _col_offset = col_offset;
1289            }
1290
1291            internal Call(CallExpression call)
1292                : this() {
1293                _args = PythonOps.MakeEmptyList(call.Args.Count);
1294                _keywords = new PythonList();
1295                _func = Convert(call.Target);
1296                foreach (Arg arg in call.Args) {
1297
1298                    if (arg.Name == null)
1299                        _args.Add(Convert(arg.Expression));
1300                    else if (arg.Name == "*")
1301                        _starargs = Convert(arg.Expression);
1302                    else if (arg.Name == "**")
1303                        _kwargs = Convert(arg.Expression);
1304                    else
1305                        _keywords.Add(new keyword(arg));
1306                }
1307            }
1308
1309            internal override AstExpression Revert() {
1310                AstExpression target = expr.Revert(func);
1311                List<Arg> newArgs = new List<Arg>();
1312                foreach (expr ex in args)
1313                    newArgs.Add(new Arg(expr.Revert(ex)));
1314                if (null != starargs)
1315                    newArgs.Add(new Arg("*", expr.Revert(starargs)));
1316                if (null != kwargs)
1317                    newArgs.Add(new Arg("**", expr.Revert(kwargs)));
1318                foreach (keyword kw in keywords)
1319                    newArgs.Add(new Arg(kw.arg, expr.Revert(kw.value)));
1320                return new CallExpression(target, newArgs.ToArray());
1321            }
1322
1323            public expr func {
1324                get { return _func; }
1325                set { _func = value; }
1326            }
1327
1328            public PythonList args {
1329                get { return _args; }
1330                set { _args = value; }
1331            }
1332
1333            public PythonList keywords {
1334                get { return _keywords; }
1335                set { _keywords = value; }
1336            }
1337
1338            public expr starargs {
1339                get { return _starargs; }
1340                set { _starargs = value; }
1341            }
1342
1343            public expr kwargs {
1344                get { return _kwargs; }
1345                set { _kwargs = value; }
1346            }
1347        }
1348
1349        [PythonType]
1350        public class ClassDef : stmt
1351        {
1352            private string _name;
1353            private PythonList _bases;
1354            private PythonList _body;
1355            private PythonList _decorator_list;
1356
1357            public ClassDef() {
1358                _fields = new PythonTuple(new[] { "name", "bases", "body", "de…

Large files files are truncated, but you can click here to view the full file