private Operation ConvertFunction(Token functionToken) { try { string functionName = ((string)functionToken.Value).ToLowerInvariant(); if (functionRegistry.IsFunctionName(functionName)) { FunctionInfo functionInfo = functionRegistry.GetFunctionInfo(functionName); List<Operation> operations = new List<Operation>(); for (int i = 0; i < functionInfo.NumberOfParameters; i++) operations.Add(resultStack.Pop()); operations.Reverse(); return new Function(DataType.FloatingPoint, functionName, operations); } else { throw new ArgumentException(string.Format("Unknown function \"{0}\".", functionToken.Value), "function"); } } catch (InvalidOperationException) { // If we encounter a Stack empty issue this means there is a syntax issue in // the mathematical formula throw new ParseException(string.Format("There is a syntax issue for the function \"{0}\" at position {1}. " + "The number of arguments does not match with what is expected.", functionToken.Value, functionToken.StartPosition)); } }
private void PopOperations(bool untillLeftBracket, Token? currentToken) { if (untillLeftBracket && !currentToken.HasValue) throw new ArgumentNullException("currentToken", "If the parameter \"untillLeftBracket\" is set to true, " + "the parameter \"currentToken\" cannot be null."); while (operatorStack.Count > 0 && operatorStack.Peek().TokenType != TokenType.LeftBracket) { Token token = operatorStack.Pop(); switch (token.TokenType) { case TokenType.Operation: resultStack.Push(ConvertOperation(token)); break; case TokenType.Text: resultStack.Push(ConvertFunction(token)); break; } } if (untillLeftBracket) { if (operatorStack.Count > 0 && operatorStack.Peek().TokenType == TokenType.LeftBracket) operatorStack.Pop(); else throw new ParseException(string.Format("No matching left bracket found for the right " + "bracket at position {0}.", currentToken.Value.StartPosition)); } else { if (operatorStack.Count > 0 && operatorStack.Peek().TokenType == TokenType.LeftBracket && !(currentToken.HasValue && currentToken.Value.TokenType == TokenType.ArgumentSeparator)) throw new ParseException(string.Format("No matching right bracket found for the left " + "bracket at position {0}.", operatorStack.Peek().StartPosition)); } }
private Operation ConvertOperation(Token operationToken) { try { DataType dataType; Operation argument1; Operation argument2; Operation divisor; Operation divident; switch ((char)operationToken.Value) { case '+': argument2 = resultStack.Pop(); argument1 = resultStack.Pop(); dataType = RequiredDataType(argument1, argument2); return new Addition(dataType, argument1, argument2); case '-': argument2 = resultStack.Pop(); argument1 = resultStack.Pop(); dataType = RequiredDataType(argument1, argument2); return new Subtraction(dataType, argument1, argument2); case '*': argument2 = resultStack.Pop(); argument1 = resultStack.Pop(); dataType = RequiredDataType(argument1, argument2); return new Multiplication(dataType, argument1, argument2); case '/': divisor = resultStack.Pop(); divident = resultStack.Pop(); return new Division(DataType.FloatingPoint, divident, divisor); case '%': divisor = resultStack.Pop(); divident = resultStack.Pop(); return new Modulo(DataType.FloatingPoint, divident, divisor); case '_': argument1 = resultStack.Pop(); return new UnaryMinus(argument1.DataType, argument1); case '^': Operation exponent = resultStack.Pop(); Operation @base = resultStack.Pop(); return new Exponentiation(DataType.FloatingPoint, @base, exponent); default: throw new ArgumentException(string.Format("Unknown operation \"{0}\".", operationToken), "operation"); } } catch (InvalidOperationException) { // If we encounter a Stack empty issue this means there is a syntax issue in // the mathematical formula throw new ParseException(string.Format("There is a syntax issue for the operation \"{0}\" at position {1}. " + "The number of arguments does not match with what is expected.", operationToken.Value, operationToken.StartPosition)); } }
private Operation ConvertFunction(Token functionToken) { try { switch ((string)functionToken.Value) { case "sin": return new Function(DataType.FloatingPoint, FunctionType.Sine, new Operation[] { resultStack.Pop() }); case "cos": return new Function(DataType.FloatingPoint, FunctionType.Cosine, new Operation[] { resultStack.Pop() }); case "loge": return new Function(DataType.FloatingPoint, FunctionType.Loge, new Operation[] { resultStack.Pop() }); case "log10": return new Function(DataType.FloatingPoint, FunctionType.Log10, new Operation[] { resultStack.Pop() }); case "logn": Operation[] operations = new Operation[2]; operations[1] = resultStack.Pop(); operations[0] = resultStack.Pop(); return new Function(DataType.FloatingPoint, FunctionType.Logn, operations); default: throw new ArgumentException(string.Format("Unknown function \"{0}\".", functionToken.Value), "function"); } } catch (InvalidOperationException) { // If we encounter a Stack empty issue this means there is a syntax issue in // the mathematical formula throw new ParseException(string.Format("There is a syntax issue for the function \"{0}\" at position {1}. " + "The number of arguments does not match with what is expected.", functionToken.Value, functionToken.StartPosition)); } }