/// <summary>
		/// Deserializes expressions of the form:
		/// 
		/// System.Windows.Form.AnchorStyles.Top | System.Windows.Form.AnchorStyles.Bottom
		/// </summary>
		public object Deserialize(BinaryExpression binaryExpression)
		{
			object lhs = Deserialize(binaryExpression.Left);
			object rhs = Deserialize(binaryExpression.Right);
			
			int value = Convert.ToInt32(lhs) | Convert.ToInt32(rhs);
			return Enum.ToObject(lhs.GetType(), value);			
		}
Exemplo n.º 2
0
        public Node VisitExpression(IronPython.Compiler.Ast.BinaryExpression node)
        {
            var  changed = false;
            Node newCode = node;

            foreach (var edit in _edits)
            {
                if (edit.CanApply(node))
                {
                    changed = true;
                    if (edit is Update)
                    {
                        newCode = edit.ModifiedNode.InnerNode;
                    }
                    else if (edit is Delete)
                    {
                        var deleted = new IronPython.Compiler.Ast.ConstantExpression("Tutor:deletedNode");
                        if (edit.ModifiedNode.InnerNode.Equals(node.Left))
                        {
                            var binary = newCode as IronPython.Compiler.Ast.BinaryExpression;
                            newCode = new IronPython.Compiler.Ast.BinaryExpression(binary.Operator, VisitExpression(binary.Right), deleted);
                        }
                        else
                        {
                            var binary = newCode as IronPython.Compiler.Ast.BinaryExpression;
                            newCode = new IronPython.Compiler.Ast.BinaryExpression(binary.Operator, VisitExpression(binary.Left), deleted);
                        }
                    }
                    else
                    {
                        switch (((Insert)edit).Index)
                        {
                        case 0:
                            var binary = newCode as IronPython.Compiler.Ast.BinaryExpression;
                            newCode = new IronPython.Compiler.Ast.BinaryExpression(binary.Operator, (IronPython.Compiler.Ast.Expression)edit.ModifiedNode.InnerNode, VisitExpression(binary.Right));
                            break;

                        case 1:
                            binary  = newCode as IronPython.Compiler.Ast.BinaryExpression;
                            newCode = new IronPython.Compiler.Ast.BinaryExpression(binary.Operator, VisitExpression(binary.Left), (IronPython.Compiler.Ast.Expression)edit.ModifiedNode.InnerNode);
                            break;
                        }
                    }
                }
            }
            if (changed)
            {
                return(newCode);
            }

            var left  = VisitExpression(node.Left);
            var right = VisitExpression(node.Right);

            return(new IronPython.Compiler.Ast.BinaryExpression(node.Operator, left, right));
        }
Exemplo n.º 3
0
        public string Visit(PyAst.BinaryExpression node)
        {
            string operatorText;

            switch (node.Operator)
            {
            case IronPython.Compiler.PythonOperator.Add: operatorText = "+"; break;

            case IronPython.Compiler.PythonOperator.Subtract: operatorText = "-"; break;

            case IronPython.Compiler.PythonOperator.Multiply: operatorText = "*"; break;

            case IronPython.Compiler.PythonOperator.Divide: operatorText = "/"; break;

            case IronPython.Compiler.PythonOperator.Equal: operatorText = "=="; break;

            case IronPython.Compiler.PythonOperator.NotEqual: operatorText = "!="; break;

            case IronPython.Compiler.PythonOperator.GreaterThan: operatorText = ">"; break;

            case IronPython.Compiler.PythonOperator.GreaterThanOrEqual: operatorText = ">="; break;

            case IronPython.Compiler.PythonOperator.LessThan: operatorText = "<"; break;

            case IronPython.Compiler.PythonOperator.LessThanOrEqual: operatorText = "<="; break;

            case IronPython.Compiler.PythonOperator.Mod: operatorText = "%"; break;

            case IronPython.Compiler.PythonOperator.LeftShift: operatorText = "<<"; break;

            case IronPython.Compiler.PythonOperator.RightShift: operatorText = ">>"; break;

            case IronPython.Compiler.PythonOperator.BitwiseAnd: operatorText = "&"; break;

            case IronPython.Compiler.PythonOperator.BitwiseOr: operatorText = "|"; break;

            case IronPython.Compiler.PythonOperator.ExclusiveOr: operatorText = "^"; break;

            default:
                throw new NotImplementedException($"Printing of operator {node.Operator} not implemented");
            }
            return($"({Visit(node.Left)} {operatorText} {Visit(node.Right)})");
        }
Exemplo n.º 4
0
 internal Compare(BinaryExpression expr, cmpop op)
     : this() {
     _left = Convert(expr.Left);
     _ops = PythonOps.MakeListNoCopy(op);
     _comparators = PythonOps.MakeListNoCopy(Convert(expr.Right));
 }
		/// <summary>
		/// Walks the binary expression which is the right hand side of an assignment statement.
		/// </summary>
		void WalkAssignment(BinaryExpression binaryExpression)
		{
			object value = deserializer.Deserialize(binaryExpression);
			fieldExpression.SetPropertyValue(componentCreator, value);
		}
Exemplo n.º 6
0
        public override void PostWalk(BinaryExpression node)
        {
            string right = Content();
            string left = Content();

            switch (node.Operator)
            {
                case IronPython.Compiler.PythonOperator.Add:
                    Content("{0} + {1}", left, right);
                    break;

                case IronPython.Compiler.PythonOperator.BitwiseAnd:
                    Content("{0} & {1}", left, right);
                    break;

                case IronPython.Compiler.PythonOperator.BitwiseOr:
                    Content("{0} | {1}", left, right);
                    break;

                case IronPython.Compiler.PythonOperator.Divide:
                    Content("{0} / {1}", left, right);
                    break;

                case IronPython.Compiler.PythonOperator.Equal:
                    //case IronPython.Compiler.PythonOperator.Equals:
                    Content("{0} === {1}", left, right);
                    break;

                case IronPython.Compiler.PythonOperator.Xor:
                    //case IronPython.Compiler.PythonOperator.ExclusiveOr:
                    Content("{0} ^ {1}", left, right);

                    break;

                case IronPython.Compiler.PythonOperator.FloorDivide:
                    Content("Math.floor({0} / {1})", left, right);
                    break;

                case IronPython.Compiler.PythonOperator.GreaterThan:
                    Content("{0} > {1}", left, right);
                    break;

                case IronPython.Compiler.PythonOperator.GreaterThanOrEqual:
                    Content("{0} >== {1}", left, right);
                    break;

                case IronPython.Compiler.PythonOperator.LeftShift:
                    Content("{0} << {1}", left, right);
                    break;

                case IronPython.Compiler.PythonOperator.LessThan:
                    Content("{0} < {1}", left, right);
                    break;

                case IronPython.Compiler.PythonOperator.LessThanOrEqual:
                    Content("{0} <== {1}", left, right);
                    break;

                case IronPython.Compiler.PythonOperator.Mod:
                    Content("{0} % {1}", left, right);
                    break;

                case IronPython.Compiler.PythonOperator.Multiply:
                    Content("Python.mul({0}, {1})", left, right);
                    break;

                case IronPython.Compiler.PythonOperator.NotEqual:
                    //case IronPython.Compiler.PythonOperator.NotEquals:
                    Content("{0} != {1}", left, right);
                    break;

                case IronPython.Compiler.PythonOperator.Power:
                    Content("Math.pow({0}, {1})", left, right);
                    break;

                case IronPython.Compiler.PythonOperator.RightShift:
                    Content("{0} >> {1}", left, right);
                    break;

                case IronPython.Compiler.PythonOperator.Subtract:
                    Content("{0} - {1}", left, right);
                    break;

                case IronPython.Compiler.PythonOperator.In:
                    Content("Python.isIn({0}, {1})", left, right);
                    break;

                case IronPython.Compiler.PythonOperator.NotIn:
                    Content("!Python.isIn({0}, {1})", left, right);
                    break;

                case IronPython.Compiler.PythonOperator.Invert:
                case IronPython.Compiler.PythonOperator.Is:
                case IronPython.Compiler.PythonOperator.IsNot:
                case IronPython.Compiler.PythonOperator.None:
                case IronPython.Compiler.PythonOperator.Pos:
                case IronPython.Compiler.PythonOperator.TrueDivide:
                default:
                    sink.Add(src, String.Format("Operator {0} is not supported.", node.Operator), node.Span, UNSUPPORTED_OPERATOR, Severity.FatalError);
                    break;
            }

            CommonPostWalk(node);
        }
Exemplo n.º 7
0
 public override bool Walk(BinaryExpression node)
 {
     CommonWalk(node);
     return true;
 }
 public void PostWalk(BinaryExpression node)
 {
     PostProcess(node);
 }
Exemplo n.º 9
0
		public override bool Walk(BinaryExpression node)
		{
			writer.WriteLine("Binary");
			return base.Walk(node);
		}
Exemplo n.º 10
0
        private Expression ParseExpr(int precedence) {
            Expression ret = ParseFactor();
            while (true) {
                Token t = PeekToken();
                OperatorToken ot = t as OperatorToken;
                if (ot == null) return ret;

                int prec = ot.Precedence;
                if (prec >= precedence) {
                    NextToken();
                    Expression right = ParseExpr(prec + 1);
                    var start = ret.StartIndex;
                    ret = new BinaryExpression(GetBinaryOperator(ot), ret, right);
                    ret.SetLoc(_globalParent, start, GetEnd());
                } else {
                    return ret;
                }
            }
        }
Exemplo n.º 11
0
 // power: atom trailer* ['**' factor]
 private Expression ParsePower() {
     Expression ret = ParsePrimary();
     ret = AddTrailers(ret);
     if (MaybeEat(TokenKind.Power)) {
         SourceLocation start = ret.Start;
         ret = new BinaryExpression(PythonOperator.Power, ret, ParseFactor());
         ret.SetLoc(start, GetEnd());
     }
     return ret;
 }
Exemplo n.º 12
0
            internal static expr Convert(BinaryExpression expr) {
                AST op = Convert(expr.Operator);
                if (op is @operator)
                    return new BinOp(expr, (@operator)op);
                if (op is cmpop)
                    return new Compare(expr, (cmpop)op);

                throw new ArgumentTypeException("Unexpected operator type: " + op.GetType());
            }
Exemplo n.º 13
0
 // BinaryExpression
 public override bool Walk(BinaryExpression node)
 {
     node.Parent = _currentScope;
     return(base.Walk(node));
 }
Exemplo n.º 14
0
 public virtual void PostWalk(BinaryExpression node)
 {
 }
Exemplo n.º 15
0
 // BinaryExpression
 public virtual bool Walk(BinaryExpression node)
 {
     return true;
 }
Exemplo n.º 16
0
        public static bool IsComparison(Expression expression)
        {
            BinaryExpression be = expression as BinaryExpression;

            return(be != null && be.IsComparison());
        }
Exemplo n.º 17
0
        private Expression ParseExpr(int precedence)
        {
            Expression ret = ParseFactor();
            while (true) {
                Token t = PeekToken();
                OperatorToken ot = t as OperatorToken;
                if (ot == null) return ret;

                int prec = ot.Operator.Precedence;
                if (prec >= precedence) {
                    NextToken();
                    Expression right = ParseExpr(prec + 1);
                    Location start = ret.Start;
                    ret = new BinaryExpression((BinaryOperator)ot.Operator, ret, right);
                    ret.SetLoc(GetExternal(), start, GetEnd());
                } else {
                    return ret;
                }
            }
        }
Exemplo n.º 18
0
 internal BinOp(BinaryExpression expr, @operator op)
     : this() {
     _left = Convert(expr.Left);
     _right = Convert(expr.Right);
     _op = op;
 }
Exemplo n.º 19
0
 internal Compare(BinaryExpression expr)
     : this()
 {
     _left = Convert(expr.Left);
     _ops = PythonOps.MakeList();
     _comparators = PythonOps.MakeList();
     while (BinaryExpression.IsComparison(expr.Right)) {
         BinaryExpression right = (BinaryExpression)expr.Right;
         // start accumulating ops and comparators
         _ops.Add(Convert(expr.Operator));
         _comparators.Add(Convert(right.Left));
         expr = right;
     }
     _ops.Add(Convert(expr.Operator));
     _comparators.Add(Convert(expr.Right));
 }
Exemplo n.º 20
0
        //comparison: expr (comp_op expr)*
        //comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
        private Expression ParseComparison() {
            Expression ret = ParseExpr();
            while (true) {
                PythonOperator op;
                switch (PeekToken().Kind) {
                    case TokenKind.LessThan: NextToken(); op = PythonOperator.LessThan; break;
                    case TokenKind.LessThanOrEqual: NextToken(); op = PythonOperator.LessThanOrEqual; break;
                    case TokenKind.GreaterThan: NextToken(); op = PythonOperator.GreaterThan; break;
                    case TokenKind.GreaterThanOrEqual: NextToken(); op = PythonOperator.GreaterThanOrEqual; break;
                    case TokenKind.Equals: NextToken(); op = PythonOperator.Equal; break;
                    case TokenKind.NotEquals: NextToken(); op = PythonOperator.NotEqual; break;
                    case TokenKind.LessThanGreaterThan: NextToken(); op = PythonOperator.NotEqual; break;

                    case TokenKind.KeywordIn: NextToken(); op = PythonOperator.In; break;
                    case TokenKind.KeywordNot: NextToken(); Eat(TokenKind.KeywordIn); op = PythonOperator.NotIn; break;

                    case TokenKind.KeywordIs:
                        NextToken();
                        if (MaybeEat(TokenKind.KeywordNot)) {
                            op = PythonOperator.IsNot;
                        } else {
                            op = PythonOperator.Is;
                        }
                        break;
                    default:
                        return ret;
                }
                Expression rhs = ParseComparison();
                BinaryExpression be = new BinaryExpression(op, ret, rhs);
                be.SetLoc(_globalParent, ret.StartIndex, GetEnd());
                ret = be;
            }
        }
Exemplo n.º 21
0
            internal override AstExpression Revert()
            {
                // the most likely case first
                if (ops.Count == 1) {
                    return new BinaryExpression(
                        ((cmpop)(ops[0])).Revert(),
                        expr.Revert(left),
                        expr.Revert(comparators[0]));
                }

                // chaining of comparators is processed here (a>b>c> ...)
                Debug.Assert(ops.Count > 1, "expected 2 or more ops in chained comparator");
                int i = ops.Count - 1;
                BinaryExpression right = new BinaryExpression(
                        ((cmpop)(ops[i])).Revert(),
                        expr.Revert(comparators[i - 1]),
                        expr.Revert(comparators[i]));
                i--;
                while (i > 0) {
                    right = new BinaryExpression(
                        ((cmpop)(ops[i])).Revert(),
                        expr.Revert(comparators[i - 1]),
                        right);
                    i--;
                }
                return new BinaryExpression(
                        ((cmpop)(ops[0])).Revert(),
                        expr.Revert(left),
                        right);
            }
Exemplo n.º 22
0
 // power: atom trailer* ['**' factor]
 private Expression ParsePower() {
     Expression ret = ParsePrimary();
     ret = AddTrailers(ret);
     if (MaybeEat(TokenKind.Power)) {
         var start = ret.StartIndex;
         ret = new BinaryExpression(PythonOperator.Power, ret, ParseFactor());
         ret.SetLoc(_globalParent, start, GetEnd());
     }
     return ret;
 }
 // BinaryExpression
 public bool Walk(BinaryExpression node)
 {
     return Process(node);
 }