public Operation Build(IList <Token> tokens) { resultStack.Clear(); operatorStack.Clear(); parameterCount.Clear(); foreach (Token token in tokens) { object value = token.Value; switch (token.TokenType) { case TokenType.FloatingPoint: resultStack.Push(new FloatingPointConstant((float)token.Value)); break; case TokenType.Text: if (functionRegistry.IsFunctionName((string)token.Value)) { operatorStack.Push(token); parameterCount.Push(1); } else { string tokenValue = (string)token.Value; resultStack.Push(new Variable(tokenValue)); } break; case TokenType.LeftBracket: operatorStack.Push(token); break; case TokenType.RightBracket: PopOperations(true, token); //parameterCount.Pop(); break; case TokenType.ArgumentSeparator: PopOperations(false, token); parameterCount.Push(parameterCount.Pop() + 1); break; case TokenType.Operation: Token operation1Token = token; char operation1 = (char)operation1Token.Value; while (operatorStack.Count > 0 && (operatorStack.Peek().TokenType == TokenType.Operation || operatorStack.Peek().TokenType == TokenType.Text)) { Token operation2Token = operatorStack.Peek(); bool isFunctionOnTopOfStack = operation2Token.TokenType == TokenType.Text; if (!isFunctionOnTopOfStack) { char operation2 = (char)operation2Token.Value; if ((IsLeftAssociativeOperation(operation1) && operationPrecedence[operation1] <= operationPrecedence[operation2]) || (operationPrecedence[operation1] < operationPrecedence[operation2])) { operatorStack.Pop(); resultStack.Push(ConvertOperation(operation2Token)); } else { break; } } else { operatorStack.Pop(); resultStack.Push(ConvertFunction(operation2Token)); } } operatorStack.Push(operation1Token); break; } } PopOperations(false, null); VerifyResultStack(); return(resultStack.First()); }
public Operation Build(IList <Token> tokens) { resultStack.Clear(); operatorStack.Clear(); parameterCount.Clear(); foreach (Token token in tokens) { object value = token.Value; switch (token.TokenType) { #if _USE_VARIABLE_ case TokenType.Integer: resultStack.Push(new VariableCalcurator((int)token.Value)); break; case TokenType.FloatingPoint: resultStack.Push(new VariableCalcurator(DataType.FloatingPoint, token.Value.ToString())); break; case TokenType.Hex: resultStack.Push(new VariableCalcurator((uint)token.Value)); break; case TokenType.Literal: resultStack.Push(new VariableCalcurator(DataType.Literal, (string)token.Value)); break; case TokenType.Identifier: if ((string)token.Value == "true") { resultStack.Push(new VariableCalcurator(true)); } else if ((string)token.Value == "false") { resultStack.Push(new VariableCalcurator(false)); } else { resultStack.Push(new VariableCalcurator(DataType.Identifier, (string)token.Value)); } break; #else case TokenType.Integer: resultStack.Push(new IntegerConstant((int)token.Value)); break; case TokenType.FloatingPoint: resultStack.Push(new FloatingPointConstant((double)token.Value)); break; case TokenType.Text: if (functionRegistry.IsFunctionName((string)token.Value)) { operatorStack.Push(token); parameterCount.Push(1); } else { string tokenValue = (string)token.Value; if (localConstantRegistry.IsConstantName(tokenValue)) { resultStack.Push(new FloatingPointConstant(localConstantRegistry.GetConstantInfo(tokenValue).Value)); } else { if (!caseSensitive) { tokenValue = tokenValue.ToLowerFast(); } resultStack.Push(new Variable(tokenValue)); } } break; #endif case TokenType.OpenParentheses: operatorStack.Push(token); break; case TokenType.CloseParentheses: PopOperations(TokenType.OpenParentheses, token); break; case TokenType.OpenBracket: operatorStack.Push(token); break; case TokenType.CloseBracket: PopOperations(TokenType.OpenBracket, token); break; case TokenType.ArgumentSeparator: PopOperations(TokenType.Null, token); parameterCount.Push(parameterCount.Pop() + 1); break; case TokenType.Operation: Token operation1Token = token; char operation1 = (char)operation1Token.Value; while (operatorStack.Count > 0 && (operatorStack.Peek().TokenType == TokenType.Operation || operatorStack.Peek().TokenType == TokenType.Identifier)) { Token operation2Token = operatorStack.Peek(); bool isFunctionOnTopOfStack = operation2Token.TokenType == TokenType.Identifier; if (!isFunctionOnTopOfStack) { char operation2 = (char)operation2Token.Value; if ((IsLeftAssociativeOperation(operation1) && operationPrecedence[operation1] <= operationPrecedence[operation2]) || (operationPrecedence[operation1] < operationPrecedence[operation2])) { operatorStack.Pop(); resultStack.Push(ConvertOperation(operation2Token)); } else { break; } } else { operatorStack.Pop(); resultStack.Push(ConvertFunction(operation2Token)); } } operatorStack.Push(operation1Token); break; } } PopOperations(TokenType.Null, null); VerifyResultStack(); return(resultStack.First()); }