Beispiel #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;
            }

            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());
            }
        }
Beispiel #2
0
 public override AstVisitAction VisitBinaryExpression(BinaryExpressionAst binaryExpressionAst)
 {
     Console.WriteLine("Visited an BinaryExpressionAst.");
     Console.WriteLine("    " + binaryExpressionAst.ToString().Replace(Environment.NewLine, Environment.NewLine + "    "));
     return(AstVisitAction.Continue);
 }
Beispiel #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());
            }
        }