Ejemplo n.º 1
0
        object EvaluateBinaryExpression(BinaryExpressionAst binaryExpressionAst)
        {
            var leftOperand = EvaluateAst(binaryExpressionAst.Left);
            var rightOperand = EvaluateAst(binaryExpressionAst.Right);

            if (leftOperand is PSObject) leftOperand = ((PSObject)leftOperand).BaseObject;
            if (rightOperand is PSObject) rightOperand = ((PSObject)rightOperand).BaseObject;

            switch (binaryExpressionAst.Operator)
            {
                case TokenKind.DotDot:
                    return Range(LanguagePrimitives.ConvertTo<int>(leftOperand), LanguagePrimitives.ConvertTo<int>(rightOperand));

                case TokenKind.Plus:
                    return ArithmeticOperations.Add(leftOperand, rightOperand);

                case TokenKind.Ieq:
                case TokenKind.Ceq:
                    if (leftOperand is string || rightOperand is string)
                    {
                        StringComparison ignoreCaseComparision =
                            (TokenKind.Ceq == binaryExpressionAst.Operator)
                            ? StringComparison.CurrentCulture
                            : StringComparison.CurrentCultureIgnoreCase;
                        var left = LanguagePrimitives.ConvertTo<string>(leftOperand);
                        var right = LanguagePrimitives.ConvertTo<string>(rightOperand);
                        return String.Equals(left, right, ignoreCaseComparision);
                    }
                    return Object.Equals(leftOperand, rightOperand);

                case TokenKind.Ine:
                case TokenKind.Cne:
                    if (leftOperand is string || rightOperand is string)
                    {
                        StringComparison ignoreCaseComparision =
                            (TokenKind.Cne == binaryExpressionAst.Operator)
                            ? StringComparison.CurrentCulture
                            : StringComparison.CurrentCultureIgnoreCase;
                        var left = LanguagePrimitives.ConvertTo<string>(leftOperand);
                        var right = LanguagePrimitives.ConvertTo<string>(rightOperand);
                        return !String.Equals(left, right, ignoreCaseComparision);
                    }
                    return !Object.Equals(leftOperand, rightOperand);

                case TokenKind.Igt:
                case TokenKind.Cgt:
                    return ComparisonOperations.GreaterThan(
                            leftOperand,
                            rightOperand,
                            (TokenKind.Cgt != binaryExpressionAst.Operator));

                case TokenKind.Ige:
                case TokenKind.Cge:
                    return ComparisonOperations.GreaterThanEquals(
                            leftOperand,
                            rightOperand,
                            (TokenKind.Cge != binaryExpressionAst.Operator));

                case TokenKind.Or:
                    return LanguagePrimitives.ConvertTo<bool>(leftOperand) || LanguagePrimitives.ConvertTo<bool>(rightOperand);

                case TokenKind.Xor:
                    return LanguagePrimitives.ConvertTo<bool>(leftOperand) != LanguagePrimitives.ConvertTo<bool>(rightOperand);

                case TokenKind.And:
                    return LanguagePrimitives.ConvertTo<bool>(leftOperand) && LanguagePrimitives.ConvertTo<bool>(rightOperand);

                case TokenKind.Ilt:
                case TokenKind.Clt:
                    return ComparisonOperations.LessThan(
                            leftOperand,
                            rightOperand,
                            (TokenKind.Clt != binaryExpressionAst.Operator));

                case TokenKind.Ile:
                case TokenKind.Cle:
                    return ComparisonOperations.LessThanEquals(
                            leftOperand,
                            rightOperand,
                            (TokenKind.Clt != binaryExpressionAst.Operator));

                case TokenKind.Band:
                    return BitwiseOperation.And(leftOperand, rightOperand);
                case TokenKind.Bor:
                    return BitwiseOperation.Or(leftOperand, rightOperand);
                case TokenKind.Bxor:
                    return BitwiseOperation.Xor(leftOperand, rightOperand);

                case TokenKind.Imatch:
                    return Match(leftOperand, rightOperand, RegexOptions.IgnoreCase);
                case TokenKind.Inotmatch:
                    return NotMatch(leftOperand, rightOperand, RegexOptions.IgnoreCase);
                case TokenKind.Cmatch:
                    return Match(leftOperand, rightOperand, RegexOptions.None);
                case TokenKind.Cnotmatch:
                    return NotMatch(leftOperand, rightOperand, RegexOptions.None);

                case TokenKind.Multiply:
                    return ArithmeticOperations.Multiply(leftOperand, rightOperand);

                case TokenKind.Divide:
                    return ArithmeticOperations.Divide(leftOperand, rightOperand);

                case TokenKind.Minus:
                    return ArithmeticOperations.Subtract(leftOperand, rightOperand);

                case TokenKind.Rem:
                    return ArithmeticOperations.Remainder(leftOperand, rightOperand);

                case TokenKind.Format:
                    {
                        var left = LanguagePrimitives.ConvertTo<string>(leftOperand);
                        var right = LanguagePrimitives.ConvertTo<object[]>(rightOperand);
                        return string.Format(left, right);
                    }

                case TokenKind.Equals:
                case TokenKind.PlusEquals:
                case TokenKind.MinusEquals:
                case TokenKind.MultiplyEquals:
                case TokenKind.DivideEquals:
                case TokenKind.RemainderEquals:
                case TokenKind.Not:
                case TokenKind.Bnot:
                case TokenKind.Join:
                case TokenKind.Ilike:
                case TokenKind.Inotlike:
                case TokenKind.Ireplace:
                case TokenKind.Icontains:
                case TokenKind.Inotcontains:
                case TokenKind.Iin:
                case TokenKind.Inotin:
                case TokenKind.Isplit:
                case TokenKind.Clike:
                case TokenKind.Cnotlike:
                case TokenKind.Creplace:
                case TokenKind.Ccontains:
                case TokenKind.Cnotcontains:
                case TokenKind.Cin:
                case TokenKind.Cnotin:
                case TokenKind.Csplit:
                case TokenKind.Is:
                case TokenKind.IsNot:
                case TokenKind.As:
                case TokenKind.Shl:
                case TokenKind.Shr:
                    throw new NotImplementedException(binaryExpressionAst.ToString());

                default:
                    throw new InvalidOperationException(binaryExpressionAst.ToString());
            }
        }
Ejemplo n.º 2
0
        object EvaluateBinaryExpression(BinaryExpressionAst binaryExpressionAst)
        {
            var leftOperand = EvaluateAst(binaryExpressionAst.Left);
            var rightOperand = EvaluateAst(binaryExpressionAst.Right);

            if (leftOperand is PSObject) leftOperand = ((PSObject)leftOperand).BaseObject;
            if (rightOperand is PSObject) rightOperand = ((PSObject)rightOperand).BaseObject;

            int? leftOperandInt = leftOperand is int ? ((int?)leftOperand) : null;
            int? rightOperandInt = rightOperand is int ? ((int?)rightOperand) : null;

            bool? leftOperandBool = leftOperand is bool ? ((bool?)leftOperand) : null;
            bool? rightOperandBool = rightOperand is bool ? ((bool?)rightOperand) : null;

            switch (binaryExpressionAst.Operator)
            {
                case TokenKind.DotDot:
                    return Range((int)leftOperand, (int)rightOperand);

                case TokenKind.Plus:
                    return Add(leftOperand, rightOperand);

                case TokenKind.Ieq:
                    if (leftOperandInt.HasValue) return leftOperandInt == rightOperandInt;
                    throw new NotImplementedException(binaryExpressionAst.ToString());

                case TokenKind.Ine:
                    if (leftOperandInt.HasValue) return leftOperandInt != rightOperandInt;
                    throw new NotImplementedException(binaryExpressionAst.ToString());

                case TokenKind.Igt:
                    if (leftOperandInt.HasValue) return leftOperandInt > rightOperandInt;
                    throw new NotImplementedException(binaryExpressionAst.ToString());

                case TokenKind.Or:
                    if (leftOperandBool.HasValue) return leftOperandBool.Value || rightOperandBool.Value;
                    throw new NotImplementedException(binaryExpressionAst.ToString());

                case TokenKind.Xor:
                    if (leftOperandBool.HasValue) return leftOperandBool != rightOperandBool;
                    throw new NotImplementedException(binaryExpressionAst.ToString());

                case TokenKind.And:
                    if (leftOperandBool.HasValue) return leftOperandBool.Value && rightOperandBool.Value;
                    throw new NotImplementedException(binaryExpressionAst.ToString());

                case TokenKind.Ilt:
                    if (leftOperandInt.HasValue) return leftOperandInt < rightOperandInt;
                    throw new NotImplementedException(binaryExpressionAst.ToString());

                case TokenKind.Ile:
                    if (leftOperandInt.HasValue) return leftOperandInt <= rightOperandInt;
                    throw new NotImplementedException(binaryExpressionAst.ToString());

                case TokenKind.Multiply:
                case TokenKind.Divide:
                case TokenKind.Minus:
                case TokenKind.Equals:
                case TokenKind.PlusEquals:
                case TokenKind.MinusEquals:
                case TokenKind.MultiplyEquals:
                case TokenKind.DivideEquals:
                case TokenKind.RemainderEquals:
                case TokenKind.Format:
                case TokenKind.Not:
                case TokenKind.Bnot:
                case TokenKind.Band:
                case TokenKind.Bor:
                case TokenKind.Bxor:
                case TokenKind.Join:
                case TokenKind.Ige:
                case TokenKind.Ilike:
                case TokenKind.Inotlike:
                case TokenKind.Imatch:
                case TokenKind.Inotmatch:
                case TokenKind.Ireplace:
                case TokenKind.Icontains:
                case TokenKind.Inotcontains:
                case TokenKind.Iin:
                case TokenKind.Inotin:
                case TokenKind.Isplit:
                case TokenKind.Ceq:
                case TokenKind.Cne:
                case TokenKind.Cge:
                case TokenKind.Cgt:
                case TokenKind.Clt:
                case TokenKind.Cle:
                case TokenKind.Clike:
                case TokenKind.Cnotlike:
                case TokenKind.Cmatch:
                case TokenKind.Cnotmatch:
                case TokenKind.Creplace:
                case TokenKind.Ccontains:
                case TokenKind.Cnotcontains:
                case TokenKind.Cin:
                case TokenKind.Cnotin:
                case TokenKind.Csplit:
                case TokenKind.Is:
                case TokenKind.IsNot:
                case TokenKind.As:
                case TokenKind.Shl:
                case TokenKind.Shr:
                    throw new NotImplementedException(binaryExpressionAst.ToString());

                default:
                    throw new InvalidOperationException(binaryExpressionAst.ToString());
            }
        }
Ejemplo n.º 3
0
        object EvaluateBinaryExpression(BinaryExpressionAst binaryExpressionAst)
        {
            var leftOperand = EvaluateAst(binaryExpressionAst.Left);
            var rightOperand = EvaluateAst(binaryExpressionAst.Right);

            switch (binaryExpressionAst.Operator)
            {
                case TokenKind.DotDot:
                    return Range((int)leftOperand, (int)rightOperand);

                case TokenKind.Plus:
                    return Add(leftOperand, rightOperand);

                case TokenKind.Ieq:
                    if (leftOperand.GetType() == typeof(int)) return ((int)leftOperand) == ((int)rightOperand);
                    throw new NotImplementedException(binaryExpressionAst.ToString());

                case TokenKind.Multiply:
                case TokenKind.Divide:
                case TokenKind.Minus:
                case TokenKind.Equals:
                case TokenKind.PlusEquals:
                case TokenKind.MinusEquals:
                case TokenKind.MultiplyEquals:
                case TokenKind.DivideEquals:
                case TokenKind.RemainderEquals:
                case TokenKind.Format:
                case TokenKind.Not:
                case TokenKind.Bnot:
                case TokenKind.And:
                case TokenKind.Or:
                case TokenKind.Xor:
                case TokenKind.Band:
                case TokenKind.Bor:
                case TokenKind.Bxor:
                case TokenKind.Join:
                case TokenKind.Ine:
                case TokenKind.Ige:
                case TokenKind.Igt:
                case TokenKind.Ilt:
                case TokenKind.Ile:
                case TokenKind.Ilike:
                case TokenKind.Inotlike:
                case TokenKind.Imatch:
                case TokenKind.Inotmatch:
                case TokenKind.Ireplace:
                case TokenKind.Icontains:
                case TokenKind.Inotcontains:
                case TokenKind.Iin:
                case TokenKind.Inotin:
                case TokenKind.Isplit:
                case TokenKind.Ceq:
                case TokenKind.Cne:
                case TokenKind.Cge:
                case TokenKind.Cgt:
                case TokenKind.Clt:
                case TokenKind.Cle:
                case TokenKind.Clike:
                case TokenKind.Cnotlike:
                case TokenKind.Cmatch:
                case TokenKind.Cnotmatch:
                case TokenKind.Creplace:
                case TokenKind.Ccontains:
                case TokenKind.Cnotcontains:
                case TokenKind.Cin:
                case TokenKind.Cnotin:
                case TokenKind.Csplit:
                case TokenKind.Is:
                case TokenKind.IsNot:
                case TokenKind.As:
                case TokenKind.Shl:
                case TokenKind.Shr:
                    throw new NotImplementedException(binaryExpressionAst.ToString());

                default:
                    throw new InvalidOperationException(binaryExpressionAst.ToString());
            }
        }