Beispiel #1
0
        internal override MSAst.Expression TransformSet(AstGenerator ag, SourceSpan span, MSAst.Expression right, PythonOperationKind op) {
            MSAst.Expression assignment;

            if (op != PythonOperationKind.None) {
                right = ag.Operation(
                    typeof(object),
                    op,
                    Transform(ag, typeof(object)),
                    right
                );
            }

            if (_reference.PythonVariable != null) {
                assignment = ag.Globals.Assign(
                    ag.Globals.GetVariable(_reference.PythonVariable), 
                    AstGenerator.ConvertIfNeeded(right, typeof(object))
                );
            } else {
                assignment = Ast.Call(
                    null,
                    typeof(ScriptingRuntimeHelpers).GetMethod("SetName"),
                    new [] {
                        ag.LocalContext, 
                        ag.Globals.GetSymbol(_name),
                        AstUtils.Convert(right, typeof(object))
                        }
                );
            }

            SourceSpan aspan = span.IsValid ? new SourceSpan(Span.Start, span.End) : SourceSpan.None;
            return ag.AddDebugInfoAndVoid(assignment, aspan);
        }
Beispiel #2
0
 internal override MSAst.Expression Transform(AstGenerator ag, Type type) {
     return ag.Get(
         type,
         _name,
         ag.Transform(_target)
     );
 }
 internal override MSAst.Expression Transform(AstGenerator ag, Type type) {
     return Ast.Call(
         AstGenerator.GetHelperMethod("Repr"),   // method
         ag.LocalContext,
         ag.TransformAsObject(_expression)       // args
     );                                  
 }
Beispiel #4
0
        internal override MSAst.Expression Transform(AstGenerator ag, Type type) {
            MSAst.Expression left = ag.Transform(_left);
            MSAst.Expression right = ag.Transform(_right);

            Type t = left.Type == right.Type ? left.Type : typeof(object);
            MSAst.ParameterExpression tmp = ag.GetTemporary("__all__", t);
            
            return Ast.Condition(
                Binders.Convert(
                    ag.BinderState,
                    typeof(bool),
                    ConversionResultKind.ExplicitCast,
                    AstUtils.Assign(
                        tmp,
                        AstUtils.Convert(
                            left,
                            t
                        )
                    )
                ),
                AstUtils.Convert(
                    right,
                    t
                ),
                tmp
            );            
        }
Beispiel #5
0
 internal AstGenerator(AstGenerator/*!*/ parent, string name, bool generator, bool print)
     : this(name, generator, false) {
     Assert.NotNull(parent);
     _context = parent.Context;
     _binderState = parent.BinderState;
     _document = _context.SourceUnit.Document;
 }
        internal override MSAst.Expression Transform(AstGenerator ag, Type type) {
            MSAst.ParameterExpression list = ag.GetTemporary("list_comprehension_list", typeof(List));

            // 1. Initialization code - create list and store it in the temp variable
            MSAst.Expression initialize =
                Ast.Assign(
                    list,
                    Ast.Call(
                        AstGenerator.GetHelperMethod("MakeList", Type.EmptyTypes) // method
                    )                    
                );

            // 2. Create body from _item:   list.Append(_item)
            MSAst.Expression body = ag.AddDebugInfo(
                Ast.Call(
                    AstGenerator.GetHelperMethod("ListAddForComprehension"),
                    list,
                    ag.TransformAsObject(_item)
                ),
                _item.Span
            );

            // 3. Transform all iterators in reverse order, building the true body:
            int current = _iterators.Length;
            while (current-- > 0) {
                ListComprehensionIterator iterator = _iterators[current];
                body = iterator.Transform(ag, body);
            }

            return Ast.Block(
                initialize,
                body,
                list                        // result
            );
        }
Beispiel #7
0
 internal override MSAst.Expression Transform(AstGenerator ag, Type type) {
     return ag.Get(
         type,
         SymbolTable.IdToString(_name),
         ag.Transform(_target)
     );
 }
Beispiel #8
0
        internal override MSAst.Expression Transform(AstGenerator ag) {
            List<MSAst.Expression> statements = new List<MSAst.Expression>();

            for (int i = 0; i < _names.Length; i++) {
                statements.Add(
                    // _references[i] = PythonOps.Import(<code context>, _names[i])
                    ag.AddDebugInfo(
                        AstUtils.Assign(
                            _variables[i].Variable, 
                            Ast.Call(
                                AstGenerator.GetHelperMethod(                           // helper
                                    _asNames[i] == SymbolId.Empty ? "ImportTop" : "ImportBottom"
                                ),
                                AstUtils.CodeContext(),                                 // 1st arg - code context
                                Ast.Constant(_names[i].MakeString()),                   // 2nd arg - module name
                                Ast.Constant(_forceAbsolute ? 0 : -1)                   // 3rd arg - absolute or relative imports
                            )
                        ),
                        _names[i].Span
                    )
                );
            }

            statements.Add(Ast.Empty());
            return ag.AddDebugInfo(Ast.Block(statements.ToArray()), Span);
        }
Beispiel #9
0
        internal override MSAst.Expression Transform(AstGenerator ag) {
            MSAst.Expression raiseExpression;
            if (_type == null && _value == null && _traceback == null) {
                raiseExpression = Ast.Call(
                    AstGenerator.GetHelperMethod("MakeRethrownException"),
                    ag.LocalContext
                );

                if (!ag._isEmittingFinally) {
                    raiseExpression = Ast.Block(
                        ag.UpdateLineUpdated(true),
                        raiseExpression
                    );
                }
            } else {
                raiseExpression = Ast.Call(
                    AstGenerator.GetHelperMethod("MakeException"),
                    ag.LocalContext,
                    ag.TransformOrConstantNull(_type, typeof(object)),
                    ag.TransformOrConstantNull(_value, typeof(object)),
                    ag.TransformOrConstantNull(_traceback, typeof(object))
                );
            }
            return ag.AddDebugInfo(
                Ast.Throw(raiseExpression),
                Span
            );
        }
Beispiel #10
0
        internal override MSAst.Expression Transform(AstGenerator ag) {
            MSAst.MethodCallExpression call;

            if (_locals == null && _globals == null) {
                // exec code
                call = Ast.Call(
                    AstGenerator.GetHelperMethod("UnqualifiedExec"), 
                    AstUtils.CodeContext(), 
                    ag.TransformAsObject(_code)
                );
            } else {
                // exec code in globals [ , locals ]
                // We must have globals now (locals is last and may be absent)
                Debug.Assert(_globals != null);
                call = Ast.Call(
                    AstGenerator.GetHelperMethod("QualifiedExec"), 
                    AstUtils.CodeContext(), 
                    ag.TransformAsObject(_code), 
                    ag.TransformAndDynamicConvert(_globals, typeof(IAttributesCollection)), 
                    ag.TransformOrConstantNull(_locals, typeof(object))
                );
            }

            return ag.AddDebugInfo(call, Span);
        }
Beispiel #11
0
        internal override MSAst.Expression Transform(AstGenerator ag) {
            MSAst.Expression result;

            if (_else != null) {
                result = ag.Transform(_else);
            } else {
                result = AstUtils.Empty();
            }

            // Now build from the inside out
            int i = _tests.Length;
            while (i-- > 0) {
                IfStatementTest ist = _tests[i];

                result = ag.AddDebugInfoAndVoid(
                    Ast.Condition(
                        ag.TransformAndDynamicConvert(ist.Test, typeof(bool)),
                        ag.TransformMaybeSingleLineSuite(ist.Body, ist.Test.Start), 
                        result
                    ),
                    new SourceSpan(ist.Start, ist.Header)
                );
            }

            return result;
        }
Beispiel #12
0
 internal override MSAst.Expression Transform(AstGenerator ag) {
     if (ag.InLoop) {
         return ag.AddDebugInfo(MSAst.Expression.Break(ag.BreakLabel), Span);
     } else {
         return null;
     }
 }
Beispiel #13
0
        internal override MSAst.Expression Transform(AstGenerator ag) {
            ReadOnlyCollectionBuilder<MSAst.Expression> statements = new ReadOnlyCollectionBuilder<MSAst.Expression>();

            for (int i = 0; i < _names.Length; i++) {
                statements.Add(
                    // _references[i] = PythonOps.Import(<code context>, _names[i])
                    ag.AddDebugInfoAndVoid(
                        GlobalAllocator.Assign(
                            ag.Globals.GetVariable(ag, _variables[i]), 
                            Ast.Call(
                                AstGenerator.GetHelperMethod(                           // helper
                                    _asNames[i] == null ? "ImportTop" : "ImportBottom"
                                ),
                                ag.LocalContext,                                        // 1st arg - code context
                                AstUtils.Constant(_names[i].MakeString()),                   // 2nd arg - module name
                                AstUtils.Constant(_forceAbsolute ? 0 : -1)                   // 3rd arg - absolute or relative imports
                            )
                        ),
                        _names[i].Span
                    )
                );
            }

            statements.Add(AstUtils.Empty());
            return ag.AddDebugInfo(Ast.Block(statements.ToReadOnlyCollection()), Span);
        }
Beispiel #14
0
        private MSAst.Expression[] GetActionArgumentsForGetOrDelete(AstGenerator ag) {
            TupleExpression te = _index as TupleExpression;
            if (te != null && te.IsExpandable) {
                return ArrayUtils.Insert(AstUtils.CodeContext(), ag.Transform(_target), ag.Transform(te.Items));
            }

            SliceExpression se = _index as SliceExpression;
            if (se != null) {
                if (se.StepProvided) {
                    return new MSAst.Expression[] { 
                        AstUtils.CodeContext(),
                        ag.Transform(_target),
                        GetSliceValue(ag, se.SliceStart),
                        GetSliceValue(ag, se.SliceStop),
                        GetSliceValue(ag, se.SliceStep) 
                    };
                }

                return new MSAst.Expression[] { 
                    AstUtils.CodeContext(),
                    ag.Transform(_target),
                    GetSliceValue(ag, se.SliceStart),
                    GetSliceValue(ag, se.SliceStop)
                };
            }

            return new MSAst.Expression[] { AstUtils.CodeContext(), ag.Transform(_target), ag.Transform(_index) };
        }
Beispiel #15
0
 internal override MSAst.Expression TransformSet(AstGenerator ag, SourceSpan span, MSAst.Expression right, Operators op) {
     if (op == Operators.None) {
         return ag.AddDebugInfoAndVoid(
             Binders.Set(
                 ag.BinderState,
                 typeof(object),
                 SymbolTable.IdToString(_name),
                 ag.Transform(_target),
                 right
             ),
             span
         );
     } else {
         MSAst.ParameterExpression temp = ag.GetTemporary("inplace");
         return ag.AddDebugInfo(
             Ast.Block(
                 Ast.Assign(temp, ag.Transform(_target)),
                 SetMemberOperator(ag, right, op, temp),
                 Ast.Empty()
             ),
             Span.Start,
             span.End
         );
     }
 }
 internal override MSAst.Expression Transform(AstGenerator ag, Type type) {
     return Ast.Call(
         AstGenerator.GetHelperMethod("Repr"),   // method
         Microsoft.Scripting.Ast.Utils.CodeContext(),
         ag.TransformAsObject(_expression)       // args
     );                                  
 }
Beispiel #17
0
        internal override MSAst.Expression TransformSet(AstGenerator ag, SourceSpan span, MSAst.Expression right, Operators op) {
            MSAst.Expression variable = _reference.Variable;
            MSAst.Expression assignment;

            Type vt = variable != null ? variable.Type : typeof(object);

            if (op != Operators.None) {
                right = Binders.Operation(
                    ag.BinderState,
                    vt,
                    StandardOperators.FromOperator(op),
                    Transform(ag, vt),
                    right
                );
            }

            if (variable != null) {
                assignment = AstUtils.Assign(variable, AstGenerator.ConvertIfNeeded(right, variable.Type));
            } else {
                assignment = AstUtils.Assign(_name, right);
            }

            SourceSpan aspan = span.IsValid ? new SourceSpan(Span.Start, span.End) : SourceSpan.None;
            return ag.AddDebugInfoAndVoid(assignment, aspan);
        }
Beispiel #18
0
        private static MSAst.Expression GetSliceValue(AstGenerator ag, Expression expr) {
            if (expr != null) {
                return ag.Transform(expr);
            }

            return Ast.Field(null, typeof(MissingParameter).GetField("Value"));
        }
Beispiel #19
0
 internal override MSAst.Expression Transform(AstGenerator ag, Type type) {
     return ag.Operation(
         type,
         PythonOperatorToOperatorString(_op),
         ag.Transform(_expression)
     );
 }
Beispiel #20
0
        internal override MSAst.Expression Transform(AstGenerator ag) {
            if (ag.IsGenerator) {
                if (_expression != null) {
                    // Statements can't return null, so return a rethrow. 
                    // Callers should detecet the ag.AddError and avoid trying to execute the tree, 
                    // but if they accidentally do, use Throw instead of empty so that
                    // we'll get an exception.
                    return Ast.Throw(
                        Ast.New(
                            typeof(InvalidOperationException).GetConstructor(Type.EmptyTypes)
                        )
                    );
                }

                return ag.AddDebugInfo(AstUtils.YieldBreak(ag.GeneratorLabel), Span);
            }

            return ag.AddDebugInfo(
                Ast.Return(
                    ag.ReturnLabel,
                    ag.TransformOrConstantNull(_expression, typeof(object))
                ),
                Span
            );
        }
Beispiel #21
0
        internal override MSAst.Expression Transform(AstGenerator ag) {
            MSAst.Expression destination = ag.TransformAsObject(_dest);

            if (_expressions.Length == 0) {
                MSAst.Expression result;
                if (destination != null) {
                    result = Ast.Call(
                        AstGenerator.GetHelperMethod("PrintNewlineWithDest"),
                        ag.LocalContext,
                        destination
                    );
                } else {
                    result = Ast.Call(
                        AstGenerator.GetHelperMethod("PrintNewline"),
                        ag.LocalContext
                    );
                }
                return ag.AddDebugInfo(result, Span);
            } else {
                // Create list for the individual statements
                List<MSAst.Expression> statements = new List<MSAst.Expression>();

                // Store destination in a temp, if we have one
                if (destination != null) {
                    MSAst.ParameterExpression temp = ag.GetTemporary("destination");

                    statements.Add(
                        ag.MakeAssignment(temp, destination)
                    );

                    destination = temp;
                }
                for (int i = 0; i < _expressions.Length; i++) {
                    string method = (i < _expressions.Length - 1 || _trailingComma) ? "PrintComma" : "Print";
                    Expression current = _expressions[i];
                    MSAst.MethodCallExpression mce;

                    if (destination != null) {
                        mce = Ast.Call(
                            AstGenerator.GetHelperMethod(method + "WithDest"),
                            ag.LocalContext,
                            destination,
                            ag.TransformAsObject(current)
                        );
                    } else {
                        mce = Ast.Call(
                            AstGenerator.GetHelperMethod(method),
                            ag.LocalContext,
                            ag.TransformAsObject(current)
                        );
                    }

                    statements.Add(mce);
                }

                statements.Add(AstUtils.Empty());
                return ag.AddDebugInfo(Ast.Block(statements.ToArray()), Span);
            }
        }
Beispiel #22
0
 internal override MSAst.Expression Transform(AstGenerator ag, Type type) {
     return Ast.Call(
         AstGenerator.GetHelperMethod("MakeSlice"),                  // method
         ag.TransformOrConstantNull(_sliceStart, typeof(object)),    // parameters
         ag.TransformOrConstantNull(_sliceStop, typeof(object)),
         ag.TransformOrConstantNull(_sliceStep, typeof(object))
     );
 }
Beispiel #23
0
 internal override MSAst.Expression Transform(AstGenerator ag) {
     if (_left.Length == 1) {
         // Do not need temps for simple assignment
         return AssignOne(ag);
     } else {
         return AssignComplex(ag, ag.Transform(_right));
     }
 }
Beispiel #24
0
 internal override MSAst.Expression Transform(AstGenerator ag, Type type) {
     return Binders.Operation(
         ag.BinderState,
         type,
         GetOperatorString,
         ArrayUtils.RemoveFirst(GetActionArgumentsForGetOrDelete(ag))
     );
 }
Beispiel #25
0
        internal override MSAst.Expression TransformSet(AstGenerator ag, SourceSpan span, MSAst.Expression right, PythonOperationKind op) {
            if (Items.Length == 0) {
                ag.AddError("can't assign to ()", Span);
                return null;
            }

            return base.TransformSet(ag, span, right, op);
        }
        internal override MSAst.Expression Transform(AstGenerator ag, Type type) {
            MSAst.Expression func = _function.TransformToFunctionExpression(ag);

            return Ast.Call(
                AstGenerator.GetHelperMethod("MakeGeneratorExpression"),
                func,
                ag.TransformAsObject(_iterable)
            );
        }
 internal override MSAst.Expression Transform(AstGenerator ag, MSAst.Expression body) {
     return ag.AddDebugInfoAndVoid(
         AstUtils.If(
             ag.TransformAndDynamicConvert(_test, typeof(bool)),
             body
         ),
         Span
     );
 }
Beispiel #28
0
 internal override MSAst.Expression Transform(AstGenerator ag) {
     if (ag.InFinally) {
         return null;
     } else if (ag.InLoop) {
         return ag.AddDebugInfo(MSAst.Expression.Continue(ag.ContinueLabel), Span);
     } else {
         return null;
     }
 }
Beispiel #29
0
 internal override MSAst.Expression Transform(AstGenerator ag) {
     // Transform to series of individual del statements.
     ReadOnlyCollectionBuilder<MSAst.Expression> statements = new ReadOnlyCollectionBuilder<MSAst.Expression>(_expressions.Length + 1);
     for (int i = 0; i < _expressions.Length; i++) {
         statements.Add(_expressions[i].TransformDelete(ag));
     }
     statements.Add(AstUtils.Empty());
     return ag.AddDebugInfo(MSAst.Expression.Block(statements), Span);
 }
Beispiel #30
0
 internal override MSAst.Expression Transform(AstGenerator ag) {
     // Transform to series of individual del statements.
     MSAst.Expression[] statements = new MSAst.Expression[_expressions.Length + 1];
     for (int i = 0; i < _expressions.Length; i++) {
         statements[i] = _expressions[i].TransformDelete(ag);
     }
     statements[_expressions.Length] = AstUtils.Empty();
     return ag.AddDebugInfo(MSAst.Expression.Block(statements), Span);
 }