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


C# BinaryOperatorExpression.Match方法代码示例

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


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

示例1: 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

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