当前位置: 首页>>代码示例>>C#>>正文


C# BinaryOperatorExpression.ReplaceWith方法代码示例

本文整理汇总了C#中BinaryOperatorExpression.ReplaceWith方法的典型用法代码示例。如果您正苦于以下问题:C# BinaryOperatorExpression.ReplaceWith方法的具体用法?C# BinaryOperatorExpression.ReplaceWith怎么用?C# BinaryOperatorExpression.ReplaceWith使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BinaryOperatorExpression的用法示例。


在下文中一共展示了BinaryOperatorExpression.ReplaceWith方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: VisitBinaryOperatorExpression

        public override void VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression)
        {
            base.VisitBinaryOperatorExpression(binaryOperatorExpression);

            var left = binaryOperatorExpression.Left;
            var right = binaryOperatorExpression.Right;

            if (left is IdentifierExpression &&
                right is IdentifierExpression)
            {
                var leftIdent = (left as IdentifierExpression).Identifier;
                var rightIdent = (right as IdentifierExpression).Identifier;
                if (leftIdent == rightIdent)
                {
                    if (binaryOperatorExpression.Operator == BinaryOperatorType.Subtract)
                    {
                        binaryOperatorExpression.ReplaceWith(new PrimitiveExpression(0));
                        return;
                    }
                }
            }

            if (right is PrimitiveExpression &&
                (right as PrimitiveExpression).Value is int &&
                (int)(right as PrimitiveExpression).Value == 0)
            {
                if (binaryOperatorExpression.Operator == BinaryOperatorType.Add ||
                    binaryOperatorExpression.Operator == BinaryOperatorType.Subtract)
                {
                    binaryOperatorExpression.ReplaceWith(left);
                    return;
                }
            }

            if (binaryOperatorExpression.Operator == BinaryOperatorType.ConditionalAnd)
            {
                if (ReplaceConditionalAnd(binaryOperatorExpression, left, right) ||
                    ReplaceConditionalAnd(binaryOperatorExpression, right, left))
                    return;
            }

            if (binaryOperatorExpression.Operator == BinaryOperatorType.ConditionalOr)
            {
                if (ReplaceConditionalOr(binaryOperatorExpression, left, right) ||
                    ReplaceConditionalOr(binaryOperatorExpression, right, left))
                    return;
            }
        }
开发者ID:TreeSeed,项目名称:Tychaia,代码行数:48,代码来源:SimplifyZeroAndConditionalExpressionsVisitor.cs

示例2: VisitBinaryOperatorExpression

        public override void VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression)
        {
            base.VisitBinaryOperatorExpression(binaryOperatorExpression);

            // Looking for patterns like:
            //
            // 2 - 2
            var valueLeft = AstHelpers.GetValueFromExpression(binaryOperatorExpression.Left);
            var valueRight = AstHelpers.GetValueFromExpression(binaryOperatorExpression.Right);
            if (valueLeft == null || valueRight == null)
                return;

            try
            {
                dynamic valueResult;
                switch (binaryOperatorExpression.Operator)
                {
                    case BinaryOperatorType.Add:
                        valueResult = valueLeft + valueRight;
                        break;
                    case BinaryOperatorType.Subtract:
                        valueResult = valueLeft - valueRight;
                        break;
                    case BinaryOperatorType.Divide:
                        valueResult = valueLeft / valueRight;
                        break;
                    case BinaryOperatorType.Multiply:
                        valueResult = valueLeft * valueRight;
                        break;
                    default:
                        return;
                }

                binaryOperatorExpression.ReplaceWith(new PrimitiveExpression(valueResult));
            }
            catch (Exception)
            {
                // Can't do anything with this value perhaps, so just ignore it and leave
                // the code as-is.
            }
        }
开发者ID:TreeSeed,项目名称:Tychaia,代码行数:41,代码来源:SimplifyConstantMathExpressionsVisitor.cs

示例3: VisitBinaryOperatorExpression

        public override void VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression)
        {
            base.VisitBinaryOperatorExpression(binaryOperatorExpression);

            // Looking for patterns like:
            //
            // (x - 2) - 2
            // (x / 2) / 2
            // (x - 2) + 4
            // (x * 2) / 3
            var pattern = new BinaryOperatorExpression
            {
                Left = new ParenthesizedExpression
                {
                    Expression = new BinaryOperatorExpression
                    {
                        Left = new AnyNode("left"),
                        Operator = BinaryOperatorType.Any,
                        Right = new AnyNode("rightA")
                    }
                },
                Operator = BinaryOperatorType.Any,
                Right = new AnyNode("rightB")
            };

            if (binaryOperatorExpression.Operator != BinaryOperatorType.Add &&
                binaryOperatorExpression.Operator != BinaryOperatorType.Subtract &&
                binaryOperatorExpression.Operator != BinaryOperatorType.Multiply &&
                binaryOperatorExpression.Operator != BinaryOperatorType.Divide)
            {
                return;
            }

            if (pattern.IsMatch(binaryOperatorExpression))
            {
                var match = pattern.Match(binaryOperatorExpression);
                var outerOperator = binaryOperatorExpression.Operator;
                var innerOperator = (BinaryOperatorType)((dynamic)binaryOperatorExpression.Left).Expression.Operator;
                var innerValue = AstHelpers.GetValueFromExpression((Expression)match.Get("rightA").First());
                var outerValue = AstHelpers.GetValueFromExpression((Expression)match.Get("rightB").First());
                if (innerValue == null && outerValue == null)
                    return;

                // If the operators are equal, then we handle expression like (x - 2) - 4.
                if (innerOperator == outerOperator)
                {
                    dynamic resultValue;
                    switch (binaryOperatorExpression.Operator)
                    {
                        case BinaryOperatorType.Add:
                        case BinaryOperatorType.Subtract:
                            resultValue = innerValue + outerValue;
                            break;
                        case BinaryOperatorType.Multiply:
                        case BinaryOperatorType.Divide:
                            resultValue = innerValue * outerValue;
                            break;
                        default:
                            return;
                    }

                    binaryOperatorExpression.ReplaceWith(new BinaryOperatorExpression(
                        (match.Get("left").First() as Expression).Clone(),
                        binaryOperatorExpression.Operator,
                        new PrimitiveExpression(resultValue)));
                    return;
                }

                // Otherwise handle the slightly more complex cases.
                PrimitiveExpression result = null;
                switch (innerOperator)
                {
                    case BinaryOperatorType.Add:
                        switch (outerOperator)
                        {
                            case BinaryOperatorType.Subtract:
                                result = new PrimitiveExpression(innerValue - outerValue);
                                break;
                        }

                        break;
                    case BinaryOperatorType.Subtract:
                        switch (outerOperator)
                        {
                            case BinaryOperatorType.Add:
                                result = new PrimitiveExpression(innerValue - outerValue);
                                break;
                        }

                        break;
                }

                if (result != null)
                {
                    binaryOperatorExpression.ReplaceWith(new BinaryOperatorExpression(
                        (match.Get("left").First() as Expression).Clone(),
                        binaryOperatorExpression.Operator,
                        result));
                }
            }
//.........这里部分代码省略.........
开发者ID:TreeSeed,项目名称:Tychaia,代码行数:101,代码来源:SimplifyCombinedMathExpressionsVisitor.cs

示例4: VisitBinaryOperatorExpression

			public override void VisitBinaryOperatorExpression (BinaryOperatorExpression binaryOperatorExpression)
			{
				base.VisitBinaryOperatorExpression (binaryOperatorExpression);

				var leftT = GetTypeDef (binaryOperatorExpression.Left);

				if (leftT != null && !(leftT.IsPrimitive || leftT.FullName=="System.String")) {

					var name = "";

					switch (binaryOperatorExpression.Operator) {
					case BinaryOperatorType.Add:
						name = "op_Addition";
						break;
					case BinaryOperatorType.Subtract:
						name = "op_Subtraction";
						break;
					case BinaryOperatorType.Multiply:
						name = "op_Multiply";
						break;
					case BinaryOperatorType.Divide:
						name = "op_Division";
						break;
					case BinaryOperatorType.Equality:
						name = "op_Equality";
						break;
					case BinaryOperatorType.InEquality:
						name = "op_Inequality";
						break;
					case BinaryOperatorType.LessThan:
						name = "op_LessThan";
						break;
					case BinaryOperatorType.LessThanOrEqual:
						name = "op_LessThanOrEqual";
						break;
					case BinaryOperatorType.GreaterThan:
						name = "op_GreaterThan";
						break;
					case BinaryOperatorType.GreaterThanOrEqual:
						name = "op_GreaterThanOrEqual";
						break;
					}

					var m = FindMethod (leftT, name);

					if (m != null && m.DeclaringType.FullName != "System.MulticastDelegate") {
						var left = binaryOperatorExpression.Left;
						var right = binaryOperatorExpression.Right;
						left.Remove ();
						right.Remove ();
						var n = new InvocationExpression (
						new MemberReferenceExpression (new IdentifierExpression (leftT.Name), name),
							left, right);

						n.AddAnnotation (m.ReturnType);

						binaryOperatorExpression.ReplaceWith (n);
					}
				}
			}
开发者ID:RReverser,项目名称:Netjs,代码行数:60,代码来源:CsToTs.cs

示例5: VisitBinaryOperatorExpression

        public void VisitBinaryOperatorExpression(BinaryOperatorExpression node)
        {
            VisitChildren(node);

            // Force certain operations on integers to stay integers afterwards.
            // This allows JavaScript JITs to omit overflow deoptimizations.
            if (!WillConvertOperandsToIntegers(node) && !WillConvertOperandsToIntegers(UnparenthesizedParent(node))) {
                var result = resolver.Resolve(node) as OperatorResolveResult;
                if (result != null && IsIntegerTypeCode(TypeCode(result.Type))) {
                    var temp = new NullReferenceExpression();
                    node.ReplaceWith(temp);
                    temp.ReplaceWith(new BinaryOperatorExpression(node, BinaryOperatorType.BitwiseOr, new PrimitiveExpression(0)));
                }
            }
        }
开发者ID:evanw,项目名称:minisharp,代码行数:15,代码来源:Lower.cs

示例6: VisitBinaryOperatorExpression

        public override void VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression)
        {
            base.VisitBinaryOperatorExpression(binaryOperatorExpression);

            // Looking for patterns like:
            //
            // (x - 2) - x
            var pattern = new BinaryOperatorExpression
            {
                Left = new ParenthesizedExpression
                {
                    Expression = new BinaryOperatorExpression
                    {
                        Left = new NamedNode(
                            "ident",
                            new IdentifierExpression
                        {
                            Identifier = Pattern.AnyString
                        }),
                        Operator = BinaryOperatorType.Any,
                        Right = new AnyNode("other")
                    }
                },
                Operator = BinaryOperatorType.Any,
                Right = new IdentifierExpressionBackreference("ident")
            };

            if (pattern.IsMatch(binaryOperatorExpression))
            {
                var match = pattern.Match(binaryOperatorExpression);
                var innerOperator = (BinaryOperatorType)((dynamic)binaryOperatorExpression).Left.Expression.Operator;
                var outerOperator = binaryOperatorExpression.Operator;
                switch (outerOperator)
                {
                    case BinaryOperatorType.Subtract:
                        switch (innerOperator)
                        {
                            case BinaryOperatorType.Add:
                                binaryOperatorExpression.ReplaceWith(((AstNode)match.Get("other").First()).Clone());
                                return;
                            case BinaryOperatorType.Subtract:
                                binaryOperatorExpression.ReplaceWith(new UnaryOperatorExpression(
                                    UnaryOperatorType.Minus,
                                    ((Expression)match.Get("other").First()).Clone()));
                                return;
                        }

                        break;
                }
            }

            // Looking for patterns like:
            //
            // (2 - x) + x
            pattern = new BinaryOperatorExpression
            {
                Left = new ParenthesizedExpression
                {
                    Expression = new BinaryOperatorExpression
                    {
                        Left = new AnyNode("other"),
                        Operator = BinaryOperatorType.Any,
                        Right = new NamedNode(
                            "ident",
                            new IdentifierExpression
                        {
                            Identifier = Pattern.AnyString
                        })
                    }
                },
                Operator = BinaryOperatorType.Any,
                Right = new IdentifierExpressionBackreference("ident")
            };

            if (pattern.IsMatch(binaryOperatorExpression))
            {
                var match = pattern.Match(binaryOperatorExpression);
                var innerOperator = (BinaryOperatorType)((dynamic) binaryOperatorExpression).Left.Expression.Operator;
                var outerOperator = binaryOperatorExpression.Operator;
                switch (outerOperator)
                {
                    case BinaryOperatorType.Add:
                        switch (innerOperator)
                        {
                            case BinaryOperatorType.Subtract:
                                binaryOperatorExpression.ReplaceWith(((AstNode)match.Get("other").First()).Clone());
                                return;
                        }

                        break;
                    case BinaryOperatorType.Subtract:
                        switch (innerOperator)
                        {
                            case BinaryOperatorType.Add:
                                binaryOperatorExpression.ReplaceWith(((AstNode)match.Get("other").First()).Clone());
                                return;
                        }

                        break;
                }
//.........这里部分代码省略.........
开发者ID:TreeSeed,项目名称:Tychaia,代码行数:101,代码来源:SimplifyRedundantMathExpressionsVisitor.cs


注:本文中的BinaryOperatorExpression.ReplaceWith方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。