Ejemplo n.º 1
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
            );
        }
Ejemplo n.º 2
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))
     );
 }
Ejemplo n.º 3
0
        internal override MSAst.Expression Transform(AstGenerator ag, Type type) {
            // create keys & values into array and then call helper function
            // which creates the dictionary
            if (_items.Length != 0) {
                MSAst.Expression[] parts = new MSAst.Expression[_items.Length * 2];
                Type t = null;
                bool heterogeneous = false;
                for (int index = 0; index < _items.Length; index++) {
                    SliceExpression slice = _items[index];
                    // Eval order should be:
                    //   { 2 : 1, 4 : 3, 6 :5 }
                    // This is backwards from parameter list eval, so create temporaries to swap ordering.

                    
                    parts[index * 2] = ag.TransformOrConstantNull(slice.SliceStop, typeof(object));
                    MSAst.Expression key = parts[index * 2 + 1] = ag.TransformOrConstantNull(slice.SliceStart, typeof(object));

                    Type newType;
                    if (key.NodeType == System.Linq.Expressions.ExpressionType.Convert) {
                        newType = ((MSAst.UnaryExpression)key).Operand.Type;
                    } else {
                        newType = key.Type;
                    }

                    if (t == null) {
                        t = newType;
                    } else if (newType == typeof(object)) {
                        heterogeneous = true;
                    } else if (newType != t) {
                        heterogeneous = true;
                    }
                }

                return Ast.Call(
                    typeof(PythonOps).GetMethod(heterogeneous ? "MakeDictFromItems" : "MakeHomogeneousDictFromItems"),
                    Ast.NewArrayInit(
                        typeof(object),
                        parts
                    )
                );
            }

            // empty dictionary
            return Ast.Call(
                typeof(PythonOps).GetMethod("MakeDict"),
                Ast.Constant(0)
            );
        }
Ejemplo n.º 4
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
            );
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
 internal override MSAst.Expression Transform(AstGenerator ag) {
     MSAst.MethodCallExpression raiseExpression;
     if (_type == null && _value == null && _traceback == null) {
         raiseExpression = Ast.Call(
             AstGenerator.GetHelperMethod("MakeRethrownException"),
             AstUtils.CodeContext()
         );
     } else {
         raiseExpression = Ast.Call(
             AstGenerator.GetHelperMethod("MakeException"),
             AstUtils.CodeContext(),
             ag.TransformOrConstantNull(_type, typeof(object)),
             ag.TransformOrConstantNull(_value, typeof(object)),
             ag.TransformOrConstantNull(_traceback, typeof(object))
         );
     }
     return ag.AddDebugInfo(
         Ast.Throw(raiseExpression),
         Span
     );
 }
Ejemplo n.º 7
0
        internal override MSAst.Expression Transform(AstGenerator ag) {
            // If debugging is off, return empty statement
            if (ag.Optimize) {
                return AstUtils.Empty();
            }

            // Transform into:
            // if (_test) {
            // } else {
            //     RaiseAssertionError(_message);
            // }
            return ag.AddDebugInfoAndVoid(
                AstUtils.Unless(                                 // if
                    ag.TransformAndDynamicConvert(_test, typeof(bool)), // _test
                    Ast.Call(                                           // else branch
                        AstGenerator.GetHelperMethod("RaiseAssertionError"),
                        ag.TransformOrConstantNull(_message, typeof(object))
                    )
                ),
                Span
            );
        }