Beispiel #1
0
        public override object Evaluate(object source = null)
        {
            var left  = LeftOperand.Evaluate();
            var right = RightOperand.Evaluate();

            switch (Operator)
            {
            case QueryComparisonOperator.Equal:
                return((dynamic)left == (dynamic)right);

            case QueryComparisonOperator.NotEqual:
                return((dynamic)left != (dynamic)right);

            case QueryComparisonOperator.GreaterThan:
                return((dynamic)left > (dynamic)right);

            case QueryComparisonOperator.GreaterThanOrEqual:
                return((dynamic)left >= (dynamic)right);

            case QueryComparisonOperator.LessThan:
                return((dynamic)left < (dynamic)right);

            case QueryComparisonOperator.LessThanOrEqual:
                return((dynamic)left <= (dynamic)right);

            default:
                throw new StorageArgumentOutOfRangeException(nameof(Operator), $"Operator {Operator} not supported");
            }
        }
        // Evaluate the expression.
        public float Evaluate()
        {
            switch (Operator)
            {
            case Operators.Literal:
                return(float.Parse(LiteralText));

            case Operators.Plus:
                return(LeftOperand.Evaluate() + RightOperand.Evaluate());

            case Operators.Minus:
                return(LeftOperand.Evaluate() - RightOperand.Evaluate());

            case Operators.Times:
                return(LeftOperand.Evaluate() * RightOperand.Evaluate());

            case Operators.Divide:
                return(LeftOperand.Evaluate() / RightOperand.Evaluate());

            case Operators.Negate:
                return(-LeftOperand.Evaluate());
            }

            throw new ArithmeticException("Unknown operator " + Operator.ToString());
        }
        /// <summary>
        /// Calculates the result of the binary operation.
        /// </summary>
        /// <param name="evalContext">Evaluation context</param>
        /// <returns>Result of the operation</returns>
        public override ExpressionValue Calculate(IEvaluationContext evalContext)
        {
            var left  = LeftOperand.Evaluate(evalContext);
            var right = RightOperand.Evaluate(evalContext);

            switch (left.Type)
            {
            case ExpressionValueType.Bool:
            case ExpressionValueType.Integer:
                var leftNum = left.AsLong();
                switch (right.Type)
                {
                case ExpressionValueType.Bool:
                case ExpressionValueType.Integer:
                    return(new ExpressionValue(leftNum ^ right.AsLong()));

                case ExpressionValueType.Real:
                case ExpressionValueType.String:
                    EvaluationError = RIGHT_OPER_ERROR;
                    return(ExpressionValue.Error);

                default:
                    return(ExpressionValue.Error);
                }

            case ExpressionValueType.Real:
            case ExpressionValueType.String:
                EvaluationError = LEFT_OPER_ERROR;
                return(ExpressionValue.Error);

            default:
                return(ExpressionValue.Error);
            }
        }
        /// <summary>
        /// Calculates the result of the binary operation.
        /// </summary>
        /// <param name="evalContext">Evaluation context</param>
        /// <returns>Result of the operation</returns>
        public override ExpressionValue Calculate(IExpressionEvaluationContext evalContext)
        {
            // --- Check operands for errors
            var leftValue = LeftOperand.Evaluate(evalContext);

            if (leftValue.Type == ExpressionValueType.Error)
            {
                EvaluationError = LeftOperand.EvaluationError;
                return(ExpressionValue.Error);
            }
            var rightValue = RightOperand.Evaluate(evalContext);

            if (rightValue.Type == ExpressionValueType.Error)
            {
                EvaluationError = RightOperand.EvaluationError;
                return(ExpressionValue.Error);
            }

            // --- Test for incompatible types
            if (leftValue.Type == ExpressionValueType.ByteArray || rightValue.Type == ExpressionValueType.ByteArray)
            {
                EvaluationError = "'>>' operator cannot be applied on a byte array";
                return(ExpressionValue.Error);
            }

            // --- Numeric operands
            return(new ExpressionValue(leftValue.AsNumber() >> (ushort)rightValue.AsNumber()));
        }
Beispiel #5
0
        /// <summary>
        /// Calculates the result of the binary operation.
        /// </summary>
        /// <param name="evalContext">Evaluation context</param>
        /// <returns>Result of the operation</returns>
        public override ExpressionValue Calculate(IEvaluationContext evalContext)
        {
            var left  = LeftOperand.Evaluate(evalContext);
            var right = RightOperand.Evaluate(evalContext);

            switch (left.Type)
            {
            case ExpressionValueType.Bool:
            case ExpressionValueType.Integer:
                var leftNum = left.AsLong();
                switch (right.Type)
                {
                case ExpressionValueType.Bool:
                case ExpressionValueType.Integer:
                    return(new ExpressionValue(leftNum >= right.AsLong()));

                case ExpressionValueType.Real:
                    return(new ExpressionValue(leftNum >= right.AsReal()));

                case ExpressionValueType.String:
                    EvaluationError = "Cannot compare an integer number with a string";
                    return(ExpressionValue.Error);

                default:
                    return(ExpressionValue.Error);
                }

            case ExpressionValueType.Real:
                var leftReal = left.AsReal();
                switch (right.Type)
                {
                case ExpressionValueType.Bool:
                case ExpressionValueType.Integer:
                    return(new ExpressionValue(leftReal >= right.AsLong()));

                case ExpressionValueType.Real:
                    return(new ExpressionValue(leftReal >= right.AsReal()));

                case ExpressionValueType.String:
                    EvaluationError = "Cannot compare an integer number with a string";
                    return(ExpressionValue.Error);

                default:
                    return(ExpressionValue.Error);
                }

            case ExpressionValueType.String:
                if (right.Type == ExpressionValueType.String)
                {
                    return(new ExpressionValue(string.CompareOrdinal(left.AsString(), right.AsString()) >= 0));
                }

                EvaluationError = "String can be compared only to another string";
                return(ExpressionValue.Error);

            default:
                return(ExpressionValue.Error);
            }
        }
Beispiel #6
0
        public bool Evaluate(IReadOnlyMemory memory)
        {
            var leftValue  = LeftOperand.Evaluate(memory).ToInt();
            var rightValue = RightOperand.Evaluate(memory).ToInt();
            var result     = EvaluateExpression(leftValue, rightValue, Operation);

            return(result);
        }
        /// <summary>
        /// Calculates the result of the binary operation.
        /// </summary>
        /// <param name="evalContext">Evaluation context</param>
        /// <returns>Result of the operation</returns>
        public override ExpressionValue Calculate(IExpressionEvaluationContext evalContext)
        {
            var left  = LeftOperand.Evaluate(evalContext);
            var right = RightOperand.Evaluate(evalContext);

            SuggestWiderType();
            return(left.IsValid && right.IsValid
                ? new ExpressionValue(left.Value >> (int)right.Value)
                : ExpressionValue.Error);
        }
Beispiel #8
0
        /// <summary>
        /// Calculates the result of the binary operation.
        /// </summary>
        /// <param name="evalContext">Evaluation context</param>
        /// <returns>Result of the operation</returns>
        public override ExpressionValue Calculate(IExpressionEvaluationContext evalContext)
        {
            var left  = LeftOperand.Evaluate(evalContext);
            var right = RightOperand.Evaluate(evalContext);

            SuggestType(ExpressionValueType.Bool);
            return(left.IsValid && right.IsValid
                ? new ExpressionValue(left.Value >= right.Value ? 1u : 0u)
                : ExpressionValue.Error);
        }
        /// <summary>
        /// Calculates the result of the binary operation.
        /// </summary>
        /// <param name="evalContext">Evaluation context</param>
        /// <returns>Result of the operation</returns>
        public override ushort Calculate(IEvaluationContext evalContext)
        {
            var divider = RightOperand.Evaluate(evalContext);

            if (divider != 0)
            {
                return((ushort)(LeftOperand.Evaluate(evalContext) / divider));
            }

            EvaluationError = "Divide by zero error";
            return(0);
        }
Beispiel #10
0
        /// <summary>
        /// Calculates the result of the binary operation.
        /// </summary>
        /// <param name="evalContext">Evaluation context</param>
        /// <returns>Result of the operation</returns>
        public override ExpressionValue Calculate(IExpressionEvaluationContext evalContext)
        {
            // --- Check operands for errors
            var leftValue = LeftOperand.Evaluate(evalContext);

            if (leftValue.Type == ExpressionValueType.Error)
            {
                EvaluationError = LeftOperand.EvaluationError;
                return(ExpressionValue.Error);
            }
            var rightValue = RightOperand.Evaluate(evalContext);

            if (rightValue.Type == ExpressionValueType.Error)
            {
                EvaluationError = RightOperand.EvaluationError;
                return(ExpressionValue.Error);
            }

            // --- Test if both operands are byte arrays
            if (leftValue.Type == ExpressionValueType.ByteArray)
            {
                if (rightValue.Type != ExpressionValueType.ByteArray)
                {
                    EvaluationError = "Cannot apply bitwise XOR on a numeric value and a byte array";
                    return(ExpressionValue.Error);
                }

                // --- Bitwise XOR each array elements (shortest)
                var leftArray   = leftValue.AsByteArray();
                var rightArray  = rightValue.AsByteArray();
                var shortest    = leftArray.Length > rightArray.Length ? leftArray.Length : rightArray.Length;
                var resultArray = new byte[shortest];
                for (var i = 0; i < shortest; i++)
                {
                    resultArray[i] = (byte)(leftArray[i] ^ rightArray[i]);
                }
                return(new ExpressionValue(resultArray));
            }

            // --- Test incompatible types
            if (rightValue.Type == ExpressionValueType.ByteArray)
            {
                EvaluationError = "Cannot apply bitwise XOR on a byte array and a numeric value";
                return(ExpressionValue.Error);
            }

            // --- Numeric operands
            return(new ExpressionValue(leftValue.AsNumber() ^ rightValue.AsNumber()));
        }
Beispiel #11
0
        public override void Evaluate([CanBeNull] object parameter, [NotNull] RuleStack stack)
        {
            Assert.ArgumentNotNull(stack, nameof(stack));

            LeftOperand.Evaluate(parameter, stack);

            var left = (bool)stack.Pop();

            if (!left)
            {
                stack.Push(false);
                return;
            }

            RightOperand.Evaluate(parameter, stack);
        }
Beispiel #12
0
        /// <summary>
        /// Calculates the result of the binary operation.
        /// </summary>
        /// <param name="evalContext">Evaluation context</param>
        /// <returns>Result of the operation</returns>
        public override ExpressionValue Calculate(IEvaluationContext evalContext)
        {
            var left  = LeftOperand.Evaluate(evalContext);
            var right = RightOperand.Evaluate(evalContext);

            if (left.Type != ExpressionValueType.Bool && left.Type != ExpressionValueType.Integer)
            {
                EvaluationError = "The left operand of the shift left operator must be integral";
                return(ExpressionValue.Error);
            }
            if (right.Type != ExpressionValueType.Bool && right.Type != ExpressionValueType.Integer)
            {
                EvaluationError = "Right operand of the shift left operator must be integral";
                return(ExpressionValue.Error);
            }
            return(new ExpressionValue(left.AsLong() << (ushort)right.AsLong()));
        }
Beispiel #13
0
        /// <summary>
        /// Calculates the result of the binary operation.
        /// </summary>
        /// <param name="evalContext">Evaluation context</param>
        /// <returns>Result of the operation</returns>
        public override ExpressionValue Calculate(IExpressionEvaluationContext evalContext)
        {
            // --- Check operands for errors
            var leftValue = LeftOperand.Evaluate(evalContext);

            if (leftValue.Type == ExpressionValueType.Error)
            {
                EvaluationError = LeftOperand.EvaluationError;
                return(ExpressionValue.Error);
            }
            var rightValue = RightOperand.Evaluate(evalContext);

            if (rightValue.Type == ExpressionValueType.Error)
            {
                EvaluationError = RightOperand.EvaluationError;
                return(ExpressionValue.Error);
            }

            // --- Test if both operands are byte arrays
            if (leftValue.Type == ExpressionValueType.ByteArray)
            {
                if (rightValue.Type != ExpressionValueType.ByteArray)
                {
                    EvaluationError = "Cannot add a numeric value to a byte array";
                    return(ExpressionValue.Error);
                }

                // --- Concatenate the two byte arrays
                var leftArray   = leftValue.AsByteArray();
                var rightArray  = rightValue.AsByteArray();
                var resultArray = new byte[leftArray.Length + rightArray.Length];
                System.Buffer.BlockCopy(leftArray, 0, resultArray, 0, leftArray.Length);
                System.Buffer.BlockCopy(rightArray, 0, resultArray, leftArray.Length, rightArray.Length);
                return(new ExpressionValue(resultArray));
            }

            // --- Test for incompatible types
            if (rightValue.Type == ExpressionValueType.ByteArray)
            {
                EvaluationError = "Cannot add a byte array to a numeric value";
                return(ExpressionValue.Error);
            }

            // --- Numeric operands
            return(new ExpressionValue(leftValue.AsNumber() + rightValue.AsNumber()));
        }
        public override object Evaluate(object source = null)
        {
            switch (Operator)
            {
            case QueryLogicalOperator.And:
                return((bool)LeftOperand.Evaluate() && (bool)RightOperand.Evaluate());

            case QueryLogicalOperator.Or:
                return((bool)LeftOperand.Evaluate() || (bool)RightOperand.Evaluate());

            case QueryLogicalOperator.Not:
                return(!(bool)LeftOperand.Evaluate());

            default:
                throw new StorageArgumentOutOfRangeException(nameof(Operator), $"Operator {Operator} not supported");
            }
        }
        /// <summary>
        /// Calculates the result of the binary operation.
        /// </summary>
        /// <param name="evalContext">Evaluation context</param>
        /// <returns>Result of the operation</returns>
        public override ExpressionValue Calculate(IExpressionEvaluationContext evalContext)
        {
            var left  = LeftOperand.Evaluate(evalContext);
            var right = RightOperand.Evaluate(evalContext);

            if (!left.IsValid || !right.IsValid)
            {
                return(ExpressionValue.Error);
            }
            if (right.Value != 0)
            {
                SuggestWiderType();
                return(new ExpressionValue(left.Value % right.Value));
            }
            EvaluationError = "Divide by zero error";
            return(ExpressionValue.Error);
        }
        /// <summary>
        /// Calculates the result of the binary operation.
        /// </summary>
        /// <param name="evalContext">Evaluation context</param>
        /// <returns>Result of the operation</returns>
        public override ExpressionValue Calculate(IExpressionEvaluationContext evalContext)
        {
            // --- Check operands for errors
            var leftValue = LeftOperand.Evaluate(evalContext);

            if (leftValue.Type == ExpressionValueType.Error)
            {
                EvaluationError = LeftOperand.EvaluationError;
                return(ExpressionValue.Error);
            }
            var rightValue = RightOperand.Evaluate(evalContext);

            if (rightValue.Type == ExpressionValueType.Error)
            {
                EvaluationError = RightOperand.EvaluationError;
                return(ExpressionValue.Error);
            }

            // --- Test if both operands are byte arrays
            if (leftValue.Type == ExpressionValueType.ByteArray)
            {
                if (rightValue.Type != ExpressionValueType.ByteArray)
                {
                    EvaluationError = "Byte array can be compared only to byte array";
                    return(ExpressionValue.Error);
                }
                return(new ExpressionValue(!leftValue.AsByteArray().SequenceEqual(rightValue.AsByteArray())));
            }

            // --- Test for incompatible types
            if (rightValue.Type == ExpressionValueType.ByteArray)
            {
                EvaluationError = "Cannot compare a byte array with a numeric value";
                return(ExpressionValue.Error);
            }

            // --- Numeric operands
            return(new ExpressionValue(leftValue.AsNumber() != (ushort)rightValue.AsNumber()));
        }
        // Evaluate the expression.
        public float Evaluate()
        {
            switch (Operator)
            {
            case Operators.Literal:
                return(float.Parse(LiteralText));

            case Operators.Plus:
                return(LeftOperand.Evaluate() + RightOperand.Evaluate());

            case Operators.Minus:
                return(LeftOperand.Evaluate() - RightOperand.Evaluate());

            case Operators.Times:
                return(LeftOperand.Evaluate() * RightOperand.Evaluate());

            case Operators.Divide:
                return(LeftOperand.Evaluate() / RightOperand.Evaluate());

            case Operators.Negate:
                return(-LeftOperand.Evaluate());

            case Operators.SquareRoot:
                return((float)Math.Sqrt(LeftOperand.Evaluate()));

            case Operators.Factorial:
                return(Factorial(LeftOperand.Evaluate()));

            case Operators.Sine:
                return((float)Math.Sin(Math.PI / 180.0 * LeftOperand.Evaluate()));

            case Operators.Squared:
                float left = LeftOperand.Evaluate();
                return(left * left);
            }

            throw new ArithmeticException("Unknown operator " + Operator.ToString());
        }
 /// <summary>
 /// Calculates the result of the binary operation.
 /// </summary>
 /// <param name="evalContext">Evaluation context</param>
 /// <returns>Result of the operation</returns>
 public override ushort Calculate(IEvaluationContext evalContext)
 => (ushort)(LeftOperand.Evaluate(evalContext)
             & RightOperand.Evaluate(evalContext));
Beispiel #19
0
 public override double Evaluate()
 {
     return(LeftOperand.Evaluate() - RightOperand.Evaluate());
 }
Beispiel #20
0
        /// <summary>
        /// Calculates the result of the binary operation.
        /// </summary>
        /// <param name="evalContext">Evaluation context</param>
        /// <returns>Result of the operation</returns>
        public override ExpressionValue Calculate(IEvaluationContext evalContext)
        {
            var left  = LeftOperand.Evaluate(evalContext);
            var right = RightOperand.Evaluate(evalContext);

            switch (right.Type)
            {
            case ExpressionValueType.Bool:
            case ExpressionValueType.Integer:
                var rightNum = right.AsLong();
                if (rightNum == 0)
                {
                    EvaluationError = DIV_BY_ZERO_ERROR;
                    return(ExpressionValue.Error);
                }
                switch (left.Type)
                {
                case ExpressionValueType.Bool:
                case ExpressionValueType.Integer:
                    return(new ExpressionValue(left.AsLong() / rightNum));

                case ExpressionValueType.Real:
                    return(new ExpressionValue(left.AsReal() / rightNum));

                case ExpressionValueType.String:
                    EvaluationError = LEFT_STRING_ERROR;
                    return(ExpressionValue.Error);

                default:
                    return(ExpressionValue.Error);
                }

            case ExpressionValueType.Real:
                var rightReal = right.AsReal();
                if (Math.Abs(rightReal) < double.Epsilon)
                {
                    EvaluationError = DIV_BY_ZERO_ERROR;
                    return(ExpressionValue.Error);
                }
                switch (left.Type)
                {
                case ExpressionValueType.Bool:
                case ExpressionValueType.Integer:
                    return(new ExpressionValue(left.AsLong() / rightReal));

                case ExpressionValueType.Real:
                    return(new ExpressionValue(left.AsReal() / rightReal));

                case ExpressionValueType.String:
                    EvaluationError = LEFT_STRING_ERROR;
                    return(ExpressionValue.Error);

                default:
                    return(ExpressionValue.Error);
                }

            case ExpressionValueType.String:
                EvaluationError = RIGHT_STRING_ERROR;
                return(ExpressionValue.Error);

            default:
                return(ExpressionValue.Error);
            }
        }
        /// <summary>
        /// Calculates the result of the binary operation.
        /// </summary>
        /// <param name="evalContext">Evaluation context</param>
        /// <returns>Result of the operation</returns>
        public override ExpressionValue Calculate(IEvaluationContext evalContext)
        {
            var left  = LeftOperand.Evaluate(evalContext);
            var right = RightOperand.Evaluate(evalContext);

            switch (left.Type)
            {
            case ExpressionValueType.Bool:
            case ExpressionValueType.Integer:
                var leftNum = left.AsLong();
                switch (right.Type)
                {
                case ExpressionValueType.Bool:
                case ExpressionValueType.Integer:
                    return(new ExpressionValue(leftNum + right.AsLong()));

                case ExpressionValueType.Real:
                    return(new ExpressionValue(leftNum + right.AsReal()));

                case ExpressionValueType.String:
                    EvaluationError = "Cannot add an integral value and a string";
                    return(ExpressionValue.Error);

                default:
                    return(ExpressionValue.Error);
                }

            case ExpressionValueType.Real:
                var leftReal = left.AsReal();
                switch (right.Type)
                {
                case ExpressionValueType.Bool:
                case ExpressionValueType.Integer:
                    return(new ExpressionValue(leftReal + right.AsLong()));

                case ExpressionValueType.Real:
                    return(new ExpressionValue(leftReal + right.AsReal()));

                case ExpressionValueType.String:
                    EvaluationError = "Cannot add a real value and a string";
                    return(ExpressionValue.Error);

                default:
                    return(ExpressionValue.Error);
                }

            case ExpressionValueType.String:
                if (right.Type == ExpressionValueType.String)
                {
                    return(new ExpressionValue($"{left.AsString()}{right.AsString()}"));
                }
                else
                {
                    EvaluationError = "Only a string can be added to a string";
                    return(ExpressionValue.Error);
                }

            default:
                return(ExpressionValue.Error);
            }
        }
Beispiel #22
0
 public override int Evaluate()
 {
     return(LeftOperand.Evaluate() + RightOperand.Evaluate());
 }