/// <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);
            }
        }
Ejemplo n.º 2
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 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()));
        }
Ejemplo n.º 3
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");
            }
        }
Ejemplo n.º 4
0
        public double Evaluate(Dictionary <string, double> input)
        {
            var leftValue  = LeftOperand.Evaluate(input);
            var rightValue = Math.Min(100, RightOperand.Evaluate(input));

            return(Complex.Pow(leftValue, rightValue).Real);
        }
Ejemplo n.º 5
0
        public IExpressionNode Simplify()
        {
            LeftOperand  = LeftOperand.Simplify();
            RightOperand = RightOperand.Simplify();
            if (!LeftOperand.ContainsVariable() && !RightOperand.ContainsVariable())
            {
                return(new Constant(this.Evaluate(null)));
            }
            else if (!LeftOperand.ContainsVariable() && RightOperand.ContainsVariable())
            {
                if (LeftOperand.Evaluate(null) == 0)
                {
                    return(new Constant(0));
                }
            }
            else if (LeftOperand.ContainsVariable() && !RightOperand.ContainsVariable())
            {
                if (RightOperand.Evaluate(null) == 0)
                {
                    throw new DivideByZeroException();
                }

                if (RightOperand.Evaluate(null) == 1)
                {
                    return(LeftOperand.DeepCopy());
                }
            }

            return(this);
        }
Ejemplo n.º 6
0
        public override JSValue Evaluate(Context context)
        {
            var task = new Task <JSValue>(() =>
            {
                Module module = context._module.Import(LeftOperand.Evaluate(context).ToString());

                var m = JSObject.CreateObject();
                foreach (var item in module.Exports)
                {
                    var key = item.Key;
                    if (key == string.Empty)
                    {
                        key = "default";
                    }

                    m[key] = item.Value;
                }

                return(m);
            });

            task.Start();

            var result = new Promise(task);

            return(context.GlobalContext.ProxyValue(result));
        }
Ejemplo n.º 7
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);
            }
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
        public override void Evaluate([CanBeNull] object parameter, [NotNull] RuleStack stack)
        {
            Assert.ArgumentNotNull(parameter, nameof(parameter));
            Assert.ArgumentNotNull(stack, nameof(stack));

            LeftOperand.Evaluate(parameter, stack);
            RightOperand.Evaluate(parameter, stack);
        }
Ejemplo n.º 10
0
 public double Evaluate(Dictionary <string, double> input)
 {
     if (Math.Abs(RightOperand.Evaluate(input)) < 0.001)
     {
         return(LeftOperand.Evaluate(input) * 1000);
     }
     return(LeftOperand.Evaluate(input) / RightOperand.Evaluate(input));
 }
Ejemplo n.º 11
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);

            SuggestWiderType();
            return(left.IsValid && right.IsValid
                ? new ExpressionValue(left.Value >> (int)right.Value)
                : ExpressionValue.Error);
        }
Ejemplo n.º 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(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);
        }
Ejemplo n.º 14
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()));
        }
Ejemplo n.º 15
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);
        }
Ejemplo n.º 16
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()));
        }
Ejemplo n.º 17
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()));
        }
Ejemplo n.º 18
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);

            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);
        }
        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");
            }
        }
Ejemplo n.º 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(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()));
        }
Ejemplo n.º 21
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 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);
            }
        }
Ejemplo n.º 22
0
 public override double Evaluate()
 {
     return(LeftOperand.Evaluate() - RightOperand.Evaluate());
 }
Ejemplo n.º 23
0
 public double Evaluate(Dictionary <string, double> input)
 {
     return(LeftOperand.Evaluate(input) + RightOperand.Evaluate(input));
 }
Ejemplo n.º 24
0
 public override JSValue Evaluate(Context context)
 {
     return(JSObject.getOwnPropertyNames(new Arguments {
         LeftOperand.Evaluate(context)
     }));
 }
Ejemplo n.º 25
0
 public override int Evaluate()
 {
     return(LeftOperand.Evaluate() + RightOperand.Evaluate());
 }
Ejemplo n.º 26
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);
            }
        }
Ejemplo n.º 27
0
 public override double Evaluate(double x)
 {
     return(LeftOperand.Evaluate(x) * RightOperand.Evaluate(x));
 }
 /// <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));
 public double Evaluate(Dictionary <string, double> input) => LeftOperand.Evaluate(input) * RightOperand.Evaluate(input);