private int processOperand(string expression, int charCtr, int operatorIndex, Stack operandStack, Operator unaryOperator) { string operandString = null; int rtnCtr = -1; // Get the operand to process. if (operatorIndex == -1) { operandString = expression.Substring(charCtr).Trim(); rtnCtr = expression.Length; } else { operandString = expression.Substring(charCtr, operatorIndex - charCtr).Trim(); rtnCtr = operatorIndex; } if (operandString.Length == 0) { throw new EvaluationException("Expression is invalid."); } ExpressionOperand operand = new ExpressionOperand(operandString, unaryOperator); operandStack.Push(operand); return(rtnCtr); }
public void correct_decimal_value_for_subtraction_is_returned() { var operand = new ExpressionOperand( GetSpecifiers(SpecifierName, "1.5"), SpecifierName, SimpleMathOperation.SUBTRACT, 1); Assert.AreEqual((decimal)0.5, operand.GetDecimalValue().Result, "Decimal value returned by operand is not correct"); }
public void correct_int_value_for_subtraction_is_returned() { var operand = new ExpressionOperand( GetSpecifiers(SpecifierName, "2"), SpecifierName, SimpleMathOperation.SUBTRACT, 1); Assert.AreEqual(1, operand.GetIntValue().Result, "Int value returned by operand is not correct"); }
private static bool ParseSimpleFormulaBranch(ExpressionOperand root, List <Cortege3 <PerformerStatField, ExpressionOperator, double> > filters) { if (root.Operator == ExpressionOperator.And) { if (root.OperandLeft.IsConstant || root.OperandLeft.IsConstant) { return(false); } if (root.OperandRight.IsConstant || root.OperandRight.IsConstant) { return(false); } if (!ParseSimpleFormulaBranch(root.OperandLeft, filters)) { return(false); } if (!ParseSimpleFormulaBranch(root.OperandRight, filters)) { return(false); } return(true); } if (root.Operator == ExpressionOperator.Greater || root.Operator == ExpressionOperator.Lower || root.Operator == ExpressionOperator.Equal) { if (!root.OperandLeft.IsVariable) { return(false); } if (!root.OperandRight.IsConstant) { return(false); } var field = fields.FirstOrDefault(f => f.ExpressionParamName != null && f.ExpressionParamName.Equals(root.OperandLeft.VariableName, StringComparison.OrdinalIgnoreCase)); if (field == null) { return(false); } filters.Add(new Cortege3 <PerformerStatField, ExpressionOperator, double>(field, root.Operator, root.OperandRight.ConstantValue)); return(true); } return(false); }
public void get_string_value_causes_exception() { var operand = new ExpressionOperand( GetSpecifiers(SpecifierName, "1.5"), SpecifierName, SimpleMathOperation.SUBTRACT, 1); try { var x = operand.GetStringValue().Result; Assert.IsNotNull(x); } catch (AggregateException ex) { throw ex.InnerExceptions.First(); } }
public void get_int_value_for_decimal_value_throws() { var operand = new ExpressionOperand( GetSpecifiers(SpecifierName, "1.5"), SpecifierName, SimpleMathOperation.ADD, 1); try { var x = operand.GetIntValue().Result; Assert.IsNotNull(x); } catch (NameExpressionException) { return; } Assert.Fail("A NameExpressionException should be thrown"); }
public virtual string evaluate(bool wrapStringFunctionResults) { string rtnResult = null; // Get the left operand. string leftResultString = null; double?leftResultDouble = null; if (leftOperand is ExpressionTree) { leftResultString = ((ExpressionTree)leftOperand).evaluate(wrapStringFunctionResults); try { leftResultDouble = Convert.ToDouble(leftResultString); leftResultString = null; } catch (System.FormatException) { leftResultDouble = null; } } else if (leftOperand is ExpressionOperand) { ExpressionOperand leftExpressionOperand = (ExpressionOperand)leftOperand; leftResultString = leftExpressionOperand.Value; leftResultString = evaluator.replaceVariables(leftResultString); if (!evaluator.isExpressionString(leftResultString)) { try { leftResultDouble = Convert.ToDouble(leftResultString); leftResultString = null; } catch (System.FormatException nfe) { throw new EvaluationException("Expression is invalid.", nfe); } if (leftExpressionOperand.UnaryOperator != null) { leftResultDouble = new double?(leftExpressionOperand.UnaryOperator.evaluate(leftResultDouble.Value)); } } else { if (leftExpressionOperand.UnaryOperator != null) { throw new EvaluationException("Invalid operand for " + "unary operator."); } } } else if (leftOperand is ParsedFunction) { ParsedFunction parsedFunction = (ParsedFunction)leftOperand; Function function = parsedFunction.Function; string arguments = parsedFunction.Arguments; arguments = evaluator.replaceVariables(arguments); if (evaluator.ProcessNestedFunctions) { arguments = evaluator.processNestedFunctions(arguments); } try { FunctionResult functionResult = function.execute(evaluator, arguments); leftResultString = functionResult.Result; if (functionResult.Type == FunctionConstants.FUNCTION_RESULT_TYPE_NUMERIC) { double?resultDouble = Convert.ToDouble(leftResultString); if (parsedFunction.UnaryOperator != null) { resultDouble = new double?(parsedFunction.UnaryOperator.evaluate(resultDouble.Value)); } // Get the final result. leftResultString = resultDouble.ToString(); } else if (functionResult.Type == FunctionConstants.FUNCTION_RESULT_TYPE_STRING) { // The result must be a string result. if (wrapStringFunctionResults) { leftResultString = evaluator.QuoteCharacter + leftResultString + evaluator.QuoteCharacter; } if (parsedFunction.UnaryOperator != null) { throw new EvaluationException("Invalid operand for " + "unary operator."); } } } catch (FunctionException fe) { throw new EvaluationException(fe.Message, fe); } if (!evaluator.isExpressionString(leftResultString)) { try { leftResultDouble = Convert.ToDouble(leftResultString); leftResultString = null; } catch (System.FormatException nfe) { throw new EvaluationException("Expression is invalid.", nfe); } } } else { if (leftOperand != null) { throw new EvaluationException("Expression is invalid."); } } // Get the right operand. string rightResultString = null; double?rightResultDouble = null; if (rightOperand is ExpressionTree) { rightResultString = ((ExpressionTree)rightOperand).evaluate(wrapStringFunctionResults); try { rightResultDouble = Convert.ToDouble(rightResultString); rightResultString = null; } catch (System.FormatException) { rightResultDouble = null; } } else if (rightOperand is ExpressionOperand) { //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final ExpressionOperand rightExpressionOperand = (ExpressionOperand) rightOperand; ExpressionOperand rightExpressionOperand = (ExpressionOperand)rightOperand; rightResultString = ((ExpressionOperand)rightOperand).Value; rightResultString = evaluator.replaceVariables(rightResultString); // Check if the operand is a string or not. If it not a string, // then it must be a number. if (!evaluator.isExpressionString(rightResultString)) { try { rightResultDouble = Convert.ToDouble(rightResultString); rightResultString = null; } catch (System.FormatException nfe) { throw new EvaluationException("Expression is invalid.", nfe); } if (rightExpressionOperand.UnaryOperator != null) { rightResultDouble = new double?(rightExpressionOperand.UnaryOperator.evaluate(rightResultDouble.Value)); } } else { if (rightExpressionOperand.UnaryOperator != null) { throw new EvaluationException("Invalid operand for " + "unary operator."); } } } else if (rightOperand is ParsedFunction) { ParsedFunction parsedFunction = (ParsedFunction)rightOperand; Function function = parsedFunction.Function; string arguments = parsedFunction.Arguments; arguments = evaluator.replaceVariables(arguments); if (evaluator.ProcessNestedFunctions) { arguments = evaluator.processNestedFunctions(arguments); } try { FunctionResult functionResult = function.execute(evaluator, arguments); rightResultString = functionResult.Result; if (functionResult.Type == FunctionConstants.FUNCTION_RESULT_TYPE_NUMERIC) { double?resultDouble = Convert.ToDouble(rightResultString); // Process a unary operator if one exists. if (parsedFunction.UnaryOperator != null) { resultDouble = new double?(parsedFunction.UnaryOperator.evaluate(resultDouble.Value)); } // Get the final result. rightResultString = resultDouble.ToString(); } else if (functionResult.Type == FunctionConstants.FUNCTION_RESULT_TYPE_STRING) { // The result must be a string result. if (wrapStringFunctionResults) { rightResultString = evaluator.QuoteCharacter + rightResultString + evaluator.QuoteCharacter; } if (parsedFunction.UnaryOperator != null) { throw new EvaluationException("Invalid operand for " + "unary operator."); } } } catch (FunctionException fe) { throw new EvaluationException(fe.Message, fe); } if (!evaluator.isExpressionString(rightResultString)) { try { rightResultDouble = Convert.ToDouble(rightResultString); rightResultString = null; } catch (System.FormatException nfe) { throw new EvaluationException("Expression is invalid.", nfe); } } } else if (rightOperand == null) { // Do nothing. } else { throw new EvaluationException("Expression is invalid."); } // Evaluate the the expression. if (leftResultDouble != null && rightResultDouble != null) { double doubleResult = @operator.evaluate(leftResultDouble.Value, rightResultDouble.Value); if (UnaryOperator != null) { doubleResult = UnaryOperator.evaluate(doubleResult); } rtnResult = (new double?(doubleResult)).ToString(); } else if (!string.ReferenceEquals(leftResultString, null) && !string.ReferenceEquals(rightResultString, null)) { rtnResult = @operator.evaluate(leftResultString, rightResultString); } else if (leftResultDouble != null && rightResultDouble == null) { double doubleResult = -1; if (unaryOperator != null) { doubleResult = unaryOperator.evaluate(leftResultDouble.Value); } else { // Do not allow numeric (left) and // string (right) to be evaluated together. throw new EvaluationException("Expression is invalid."); } rtnResult = (new double?(doubleResult)).ToString(); } else { throw new EvaluationException("Expression is invalid."); } return(rtnResult); }
private string getResult(Stack operatorStack, Stack operandStack, bool wrapStringFunctionResults) { // The result to return. string resultString = null; // Process the rest of the operators left on the stack. while (operatorStack.Count > 0) { processTree(operandStack, operatorStack); } if (operandStack.Count != 1) { throw new EvaluationException("Expression is invalid."); } object finalOperand = operandStack.Pop(); // Check if the final operand is a tree. if (finalOperand is ExpressionTree) { // Get the final result. resultString = ((ExpressionTree)finalOperand).evaluate(wrapStringFunctionResults); } // Check if the final operand is an operand. else if (finalOperand is ExpressionOperand) { ExpressionOperand resultExpressionOperand = (ExpressionOperand)finalOperand; resultString = ((ExpressionOperand)finalOperand).Value; resultString = replaceVariables(resultString); // Check if the operand is a string or not. If it not a string, // then it must be a number. if (!isExpressionString(resultString)) { double?resultDouble = null; try { resultDouble = Convert.ToDouble(resultString); } catch (Exception e) { throw new EvaluationException("Expression is invalid.", e); } // Process a unary operator if one exists. if (resultExpressionOperand.UnaryOperator != null) { resultDouble = new double?(resultExpressionOperand.UnaryOperator.evaluate(resultDouble.Value)); } // Get the final result. resultString = resultDouble.ToString(); } else { if (resultExpressionOperand.UnaryOperator != null) { throw new EvaluationException("Invalid operand for " + "unary operator."); } } } else if (finalOperand is ParsedFunction) { ParsedFunction parsedFunction = (ParsedFunction)finalOperand; Function function = parsedFunction.Function; string arguments = parsedFunction.Arguments; if (processNestedFunctions_Renamed) { arguments = processNestedFunctions(arguments); } arguments = replaceVariables(arguments); // Get the final result. try { FunctionResult functionResult = function.execute(this, arguments); resultString = functionResult.Result; if (functionResult.Type == FunctionConstants.FUNCTION_RESULT_TYPE_NUMERIC) { double?resultDouble = Convert.ToDouble(resultString); // Process a unary operator if one exists. if (parsedFunction.UnaryOperator != null) { resultDouble = new double?(parsedFunction.UnaryOperator.evaluate(resultDouble.Value)); } // Get the final result. resultString = resultDouble.ToString(); } else if (functionResult.Type == FunctionConstants.FUNCTION_RESULT_TYPE_STRING) { // The result must be a string result. if (wrapStringFunctionResults) { resultString = quoteCharacter + resultString + quoteCharacter; } if (parsedFunction.UnaryOperator != null) { throw new EvaluationException("Invalid operand for " + "unary operator."); } } } catch (FunctionException fe) { throw new EvaluationException(fe.Message, fe); } } else { throw new EvaluationException("Expression is invalid."); } return(resultString); }
private NextOperator processFunction(string expression, int operatorIndex, Stack operandStack) { int parenthesisCount = 1; NextOperator nextOperator = null; int nextOperatorIndex = operatorIndex; // Loop until we find the function's closing parentheses. while (parenthesisCount > 0) { nextOperator = getNextOperator(expression, nextOperatorIndex + 1, null); if (nextOperator == null) { throw new EvaluationException("Function is not closed."); } else if (nextOperator.Operator is OpenParenthesesOperator) { parenthesisCount++; } else if (nextOperator.Operator is ClosedParenthesesOperator) { parenthesisCount--; } // Get the next operator index. nextOperatorIndex = nextOperator.Index; } string arguments = expression.Substring(operatorIndex + 1, nextOperatorIndex - (operatorIndex + 1)); // Pop the function name from the stack. ExpressionOperand operand = (ExpressionOperand)operandStack.Pop(); Operator unaryOperator = operand.UnaryOperator; string functionName = operand.Value; // Validate that the function name is valid. try { isValidName(functionName); } catch (System.ArgumentException iae) { throw new EvaluationException("Invalid function name of \"" + functionName + "\".", iae); } // Get the function object. //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final net.sourceforge.jeval.function.Function function = (net.sourceforge.jeval.function.Function) functions.get(functionName); Function function = (Function)functions[functionName]; if (function == null) { throw new EvaluationException("A function is not defined (index=" + operatorIndex + ")."); } ParsedFunction parsedFunction = new ParsedFunction(function, arguments, unaryOperator); operandStack.Push(parsedFunction); return(nextOperator); }