Exemplo n.º 1
0
 private static Operators PythonOperatorToAction(PythonOperator op) {
     switch (op) {
         // Binary
         case PythonOperator.Add:
             return Operators.InPlaceAdd;
         case PythonOperator.Subtract:
             return Operators.InPlaceSubtract;
         case PythonOperator.Multiply:
             return Operators.InPlaceMultiply;
         case PythonOperator.Divide:
             return Operators.InPlaceDivide;
         case PythonOperator.TrueDivide:
             return Operators.InPlaceTrueDivide;
         case PythonOperator.Mod:
             return Operators.InPlaceMod;
         case PythonOperator.BitwiseAnd:
             return Operators.InPlaceBitwiseAnd;
         case PythonOperator.BitwiseOr:
             return Operators.InPlaceBitwiseOr;
         case PythonOperator.Xor:
             return Operators.InPlaceExclusiveOr;
         case PythonOperator.LeftShift:
             return Operators.InPlaceLeftShift;
         case PythonOperator.RightShift:
             return Operators.InPlaceRightShift;
         case PythonOperator.Power:
             return Operators.InPlacePower;
         case PythonOperator.FloorDivide:
             return Operators.InPlaceFloorDivide;
         default:
             Debug.Assert(false, "Unexpected PythonOperator: " + op.ToString());
             return Operators.None;
     }
 }
Exemplo n.º 2
0
        public BinaryExpression(PythonOperator op, Expression left, Expression right) {
            ContractUtils.RequiresNotNull(left, "left");
            ContractUtils.RequiresNotNull(right, "right");
            if (op == PythonOperator.None) throw new ArgumentException("bad operator");

            _op = op;
            _left = left;
            _right = right;
            Start = left.Start;
            End = right.End;
        }
Exemplo n.º 3
0
 public override ISet<Namespace> BinaryOperation(Node node, AnalysisUnit unit, PythonOperator operation, ISet<Namespace> rhs)
 {
     switch (operation) {
         case PythonOperator.GreaterThan:
         case PythonOperator.LessThan:
         case PythonOperator.LessThanOrEqual:
         case PythonOperator.GreaterThanOrEqual:
         case PythonOperator.Equal:
         case PythonOperator.NotEqual:
         case PythonOperator.Is:
         case PythonOperator.IsNot:
             return ProjectState._boolType.Instance;
     }
     return base.BinaryOperation(node, unit, operation, rhs);
 }
Exemplo n.º 4
0
 private static PythonOperationKind PythonOperatorToOperatorString(PythonOperator op) {
     switch (op) {
         // Unary
         case PythonOperator.Not:
             return PythonOperationKind.Not;
         case PythonOperator.Pos:
             return PythonOperationKind.Positive;
         case PythonOperator.Invert:
             return PythonOperationKind.OnesComplement;
         case PythonOperator.Negate:
             return PythonOperationKind.Negate;
         default:
             Debug.Assert(false, "Unexpected PythonOperator: " + op.ToString());
             return PythonOperationKind.None;
     }
 }
Exemplo n.º 5
0
        public static ISet<Namespace> BinaryOperation(this ISet<Namespace> self, Node node, AnalysisUnit unit, PythonOperator operation, ISet<Namespace> rhs)
        {
            ISet<Namespace> res = null;
            bool madeSet = false;
            foreach (var ns in self) {
                ISet<Namespace> got = ns.BinaryOperation(node, unit, operation, rhs);
                if (res == null) {
                    res = got;
                    continue;
                } else if (!madeSet) {
                    res = new HashSet<Namespace>(res);
                    madeSet = true;
                }
                res.UnionWith(got);
            }

            return res ?? EmptySet<Namespace>.Instance;
        }
Exemplo n.º 6
0
 public UnaryExpression(PythonOperator op, Expression expression) {
     _op = op;
     _expression = expression;
     End = expression.End;
 }
Exemplo n.º 7
0
        private static MethodInfo GetHelperMethod(PythonOperator op) {
            switch (op) {
                case PythonOperator.IsNot:
                    return AstMethods.IsNot;
                case PythonOperator.Is:
                    return AstMethods.Is;

                default:
                    Debug.Assert(false, "Invalid PythonOperator: " + op.ToString());
                    return null;
            }
        }
Exemplo n.º 8
0
        private static PythonOperationKind PythonOperatorToAction(PythonOperator op) {
            switch (op) {
                // Binary
                case PythonOperator.Add:
                    return PythonOperationKind.Add;
                case PythonOperator.Subtract:
                    return PythonOperationKind.Subtract;
                case PythonOperator.Multiply:
                    return PythonOperationKind.Multiply;
                case PythonOperator.Divide:
                    return PythonOperationKind.Divide;
                case PythonOperator.TrueDivide:
                    return PythonOperationKind.TrueDivide;
                case PythonOperator.Mod:
                    return PythonOperationKind.Mod;
                case PythonOperator.BitwiseAnd:
                    return PythonOperationKind.BitwiseAnd;
                case PythonOperator.BitwiseOr:
                    return PythonOperationKind.BitwiseOr;
                case PythonOperator.Xor:
                    return PythonOperationKind.ExclusiveOr;
                case PythonOperator.LeftShift:
                    return PythonOperationKind.LeftShift;
                case PythonOperator.RightShift:
                    return PythonOperationKind.RightShift;
                case PythonOperator.Power:
                    return PythonOperationKind.Power;
                case PythonOperator.FloorDivide:
                    return PythonOperationKind.FloorDivide;

                // Comparisons
                case PythonOperator.LessThan:
                    return PythonOperationKind.LessThan;
                case PythonOperator.LessThanOrEqual:
                    return PythonOperationKind.LessThanOrEqual;
                case PythonOperator.GreaterThan:
                    return PythonOperationKind.GreaterThan;
                case PythonOperator.GreaterThanOrEqual:
                    return PythonOperationKind.GreaterThanOrEqual;
                case PythonOperator.Equal:
                    return PythonOperationKind.Equal;
                case PythonOperator.NotEqual:
                    return PythonOperationKind.NotEqual;

                case PythonOperator.In:
                    return PythonOperationKind.Contains;

                case PythonOperator.NotIn:
                case PythonOperator.IsNot:
                case PythonOperator.Is:
                    return PythonOperationKind.None;

                default:
                    Debug.Assert(false, "Unexpected PythonOperator: " + op.ToString());
                    return PythonOperationKind.None;
            }
        }
Exemplo n.º 9
0
 private bool CanEmitWarning(PythonOperator op) {
     
     return op == PythonOperator.Divide &&
                         (GlobalParent.DivisionOptions == PythonDivisionOptions.Warn || GlobalParent.DivisionOptions == PythonDivisionOptions.WarnAll);
 }
Exemplo n.º 10
0
        private MSAst.Expression MakeBinaryOperation(PythonOperator op, MSAst.Expression left, MSAst.Expression right, SourceSpan span) {
            if (op == PythonOperator.NotIn) {
                return AstUtils.Convert(
                    Ast.Not(
                        GlobalParent.Operation(
                            typeof(bool),
                            PythonOperationKind.Contains,
                            left,
                            right
                        )
                    ),
                    typeof(object)
                );
            } else if (op == PythonOperator.In) {
                return AstUtils.Convert(
                    GlobalParent.Operation(
                        typeof(bool),
                        PythonOperationKind.Contains,
                        left,
                        right
                    ),
                    typeof(object)
                );
            }

            PythonOperationKind action = PythonOperatorToAction(op);
            if (action != PythonOperationKind.None) {
                // Create action expression
                if (CanEmitWarning(op)) {
                    MSAst.ParameterExpression tempLeft = Ast.Parameter(left.Type, "left");
                    MSAst.ParameterExpression tempRight = Ast.Parameter(right.Type, "right");
                    return Ast.Block(
                        new[] { tempLeft, tempRight },
                        Ast.Call(
                            AstMethods.WarnDivision,
                            Parent.LocalContext,
                            AstUtils.Constant(GlobalParent.DivisionOptions),
                            AstUtils.Convert(
                                Ast.Assign(tempLeft, left),
                                typeof(object)
                            ),
                            AstUtils.Convert(
                                Ast.Assign(tempRight, right),
                                typeof(object)
                            )
                        ),
                        GlobalParent.Operation(
                            typeof(object),
                            action,
                            tempLeft,
                            tempRight
                        )
                    );
                }

                return GlobalParent.Operation(
                    typeof(object),
                    action,
                    left,
                    right
                );
            } else {
                // Call helper method
                return Ast.Call(
                    GetHelperMethod(op),
                    ConvertIfNeeded(left, typeof(object)),
                    ConvertIfNeeded(right, typeof(object))
                );
            }
        }
Exemplo n.º 11
0
        private static string GetHelperName(PythonOperator op) {
            switch (op) {
                case PythonOperator.In:
                    return "In";
                case PythonOperator.NotIn:
                    return "NotIn";
                case PythonOperator.IsNot:
                    return "IsNot";
                case PythonOperator.Is:
                    return "Is";

                default:
                    Debug.Assert(false, "Invalid PythonOperator: " + op.ToString());
                    return null;
            }
        }
Exemplo n.º 12
0
        private static MSAst.Expression MakeBinaryOperation(AstGenerator ag, PythonOperator op, MSAst.Expression left, MSAst.Expression right, Type type, SourceSpan span) {
            if (op == PythonOperator.NotIn) {                
                return AstUtils.Convert(
                    Ast.Not(
                        Binders.Operation(
                            ag.BinderState,
                            typeof(bool),
                            StandardOperators.Contains,
                            left,
                            right
                        )                            
                    ),
                    type
                );
            }

            Operators action = PythonOperatorToAction(op);
            if (action != Operators.None) {
                // Create action expression
                if (op == PythonOperator.Divide &&
                    (ag.DivisionOptions == PythonDivisionOptions.Warn || ag.DivisionOptions == PythonDivisionOptions.WarnAll)) {
                    MSAst.ParameterExpression tempLeft = ag.GetTemporary("left", left.Type);
                    MSAst.ParameterExpression tempRight = ag.GetTemporary("right", right.Type);
                    return Ast.Block(
                        Ast.Call(
                            AstGenerator.GetHelperMethod("WarnDivision"),
                            AstUtils.CodeContext(),
                            Ast.Constant(ag.DivisionOptions),
                            AstUtils.Convert(
                                Ast.Assign(tempLeft, left),
                                typeof(object)
                            ),
                            AstUtils.Convert(
                                Ast.Assign(tempRight, right),
                                typeof(object)
                            )
                        ),
                        Binders.Operation(
                            ag.BinderState,
                            type,
                            StandardOperators.FromOperator(action),
                            tempLeft,
                            tempRight
                        )
                    );
                }

                return Binders.Operation(
                    ag.BinderState,
                    type,
                    StandardOperators.FromOperator(action),
                    left,
                    right
                );
            } else {
                // Call helper method
                return Ast.Call(
                    AstGenerator.GetHelperMethod(GetHelperName(op)),
                    AstGenerator.ConvertIfNeeded(left, typeof(object)),
                    AstGenerator.ConvertIfNeeded(right, typeof(object))
                );
            }
        }
Exemplo n.º 13
0
 public AugmentedAssignStatement(PythonOperator op, Expression left, Expression right) {
     _op = op;
     _left = left; 
     _right = right;
 }
Exemplo n.º 14
0
 private static bool CanEmitWarning(AstGenerator ag, PythonOperator op) {
     return op == PythonOperator.Divide &&
                         (ag.DivisionOptions == PythonDivisionOptions.Warn || ag.DivisionOptions == PythonDivisionOptions.WarnAll);
 }
Exemplo n.º 15
0
        private static MSAst.Expression MakeBinaryOperation(AstGenerator ag, PythonOperator op, MSAst.Expression left, MSAst.Expression right, Type type, SourceSpan span) {
            if (op == PythonOperator.NotIn) {                
                return AstUtils.Convert(
                    Ast.Not(
                        ag.Operation(
                            typeof(bool),
                            PythonOperationKind.Contains,
                            left,
                            right
                        )                            
                    ),
                    type
                );
            } else if (op == PythonOperator.In) {
                return AstUtils.Convert(
                    ag.Operation(
                        typeof(bool),
                        PythonOperationKind.Contains,
                        left,
                        right
                    ),
                    type
                );
            }

            PythonOperationKind action = PythonOperatorToAction(op);
            if (action != PythonOperationKind.None) {
                // Create action expression
                if (CanEmitWarning(ag, op)) {
                    MSAst.ParameterExpression tempLeft = ag.GetTemporary("left", left.Type);
                    MSAst.ParameterExpression tempRight = ag.GetTemporary("right", right.Type);
                    return Ast.Block(
                        Ast.Call(
                            AstGenerator.GetHelperMethod("WarnDivision"),
                            ag.LocalContext,
                            AstUtils.Constant(ag.DivisionOptions),
                            AstUtils.Convert(
                                Ast.Assign(tempLeft, left),
                                typeof(object)
                            ),
                            AstUtils.Convert(
                                Ast.Assign(tempRight, right),
                                typeof(object)
                            )
                        ),
                        ag.Operation(
                            type,
                            action,
                            tempLeft,
                            tempRight
                        )
                    );
                }

                return ag.Operation(
                    type,
                    action,
                    left,
                    right
                );
            } else {
                // Call helper method
                return Ast.Call(
                    AstGenerator.GetHelperMethod(GetHelperName(op)),
                    AstGenerator.ConvertIfNeeded(left, typeof(object)),
                    AstGenerator.ConvertIfNeeded(right, typeof(object))
                );
            }
        }
Exemplo n.º 16
0
 public OperatorToken(TokenKind kind, PythonOperator op)
     : base(kind)
 {
     this.op = op;
 }