private void handleOperator(IToken token) { //If the current op has higher precedence, add to the stack //true if the last operator on the stack has precedence over the current operator while (operatorStack.Count() > 0 && operatorStack.First().Type == TokenType.infixOperator && precedenceTest(operatorStack.First().TokenString, token.TokenString)) { InAList.Add(operatorStack.Pop()); } operatorStack.Push(token); }
public void Add(int newFactor) { InAList.Add(newFactor); if (InAList.Count > 0 && InAList.Last() > newFactor) { throw new Exception("Your list is out of order!"); } if (newFactor == lastFactor || newFactor == 2) { consecutiveFactorCounter++; } else { consecutiveFactorCounter = 0; } lastFactor = newFactor; }
public void Add(int newFactor) { InAList.Add(newFactor); if (InAList.Count > 0 && InAList.Last() > newFactor) { throw new Exception("Your list is out of order!"); } if (newFactor == lastFactor || newFactor == 2) { consecutiveFactorCounter++; } else { AsExponents.Add(new Value((double)lastFactor, (double)consecutiveFactorCounter, NumberType.exponent, Restrictions.dontFactorDontSetFraction)); consecutiveFactorCounter = 0; } lastFactor = newFactor; }
public PostfixedTokens(List <IToken> inputTokens) { foreach (IToken token in inputTokens) { switch (token.Type) { case TokenType.keyword: InAList.Add(token); break; case TokenType.number: InAList.Add(token); break; case TokenType.variable: InAList.Add(token); break; case TokenType.function: operatorStack.Push(token); numberOfFunctionParameters.Add(0); break; case TokenType.argSeperator: if (operatorStack.Count() == 0) { ErrorLog.Add(new ErrorMessage(token.TokenString + " operator syntax error.")); } else { if (numberOfFunctionParameters.Count > 0) { numberOfFunctionParameters[numberOfFunctionParameters.Count - 1] = numberOfFunctionParameters.Last() + 1; } while (operatorStack.First().Type != TokenType.openBrace) { InAList.Add(operatorStack.Pop()); } } break; case TokenType.infixOperator: handleOperator(token); break; case TokenType.suffixOperator: InAList.Add(token); break; case TokenType.openBrace: operatorStack.Push(token); break; case TokenType.closedBrace: if (numberOfFunctionParameters.Count() > 0) { numberOfFunctionParameters[numberOfFunctionParameters.Count - 1] = numberOfFunctionParameters.Last() + 1; } while (operatorStack.First().Type != TokenType.openBrace) { if (operatorStack.Count() == 0) { ErrorLog.Add(new ErrorMessage("mismatched parenthesis error")); } InAList.Add(operatorStack.Pop()); } operatorStack.Pop(); //Pop the left parenthesis off the stack if (operatorStack.Count > 0 && operatorStack.First().Type == TokenType.function) { FunctionToken tokenToAdd = (FunctionToken)operatorStack.Pop(); tokenToAdd.numberOfChildren = numberOfFunctionParameters.Last(); numberOfFunctionParameters.RemoveAt(numberOfFunctionParameters.Count() - 1); InAList.Add(tokenToAdd); } break; default: throw new Exception("Unknown token type"); } } while (operatorStack.Count() > 0) { InAList.Add(operatorStack.Pop()); } //TODO: Handle mismatched bracket exception }
public override void Add(IToken token) { InAList.Add(token); }