/// <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())); }
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"); } }
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); }
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); }
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)); }
/// <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); } }
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); }
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); }
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)); }
/// <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); }
/// <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); }
/// <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())); }
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); }
/// <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())); }
/// <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())); }
/// <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"); } }
/// <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())); }
/// <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); } }
public override double Evaluate() { return(LeftOperand.Evaluate() - RightOperand.Evaluate()); }
public double Evaluate(Dictionary <string, double> input) { return(LeftOperand.Evaluate(input) + RightOperand.Evaluate(input)); }
public override JSValue Evaluate(Context context) { return(JSObject.getOwnPropertyNames(new Arguments { LeftOperand.Evaluate(context) })); }
public override int Evaluate() { return(LeftOperand.Evaluate() + RightOperand.Evaluate()); }
/// <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); } }
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);