public void Convert() { while (index < tokens.Count) { if (TokenInitializer.IsNumber(Token)) { result.Push(Token); } else if (TokenInitializer.IsOpenBracket(Token)) { operators.Push(Token); } else if (TokenInitializer.IsOperator(Token)) { PerformOperator(); } else if (TokenInitializer.IsFunction(Token)) { PerformFunction(); } else if (TokenInitializer.IsCloseBracket(Token)) { if (!PerformCloseBracket()) { throw new InvalidBracketEcxeption(); } } else if (TokenInitializer.IsDelimeter(Token)) { if (TokenInitializer.IsCorrectDelimeter(GetPrevNotSpace(), GetNextNotSpace())) { PerformDelimeter(); } else { throw new DelimeterPositioningException(); } } else if (TokenInitializer.IsSpace(Token)) { index++; continue; } else { throw new InvalidTokenException(Token); } index++; } if (!IsExpressionParsed()) { throw new InvalidBracketEcxeption(); } ReverseResult(); }
private string GetPrevNotSpace() { for (int i = index - 1; i >= 0; i--) { if (!TokenInitializer.IsSpace(tokens[i])) { return(tokens[i]); } } return(""); }
private string GetNextNotSpace() { for (int i = index + 1; i < tokens.Count; i++) { if (!TokenInitializer.IsSpace(tokens[i])) { return(tokens[i]); } } return(""); }
private bool IsUnary() { var maybeUnary = Token == "-" || Token == "+"; var isOpenBracketPrev = TokenInitializer.IsOpenBracket(GetPrevNotSpace()); var isOpenBracketNext = TokenInitializer.IsOpenBracket(GetNextNotSpace()); var isNumberNext = TokenInitializer.IsNumber(GetNextNotSpace()); var isFunctionNext = TokenInitializer.IsFunction(GetNextNotSpace()); var isDelimeterPrev = TokenInitializer.IsDelimeter(GetPrevNotSpace()); if (maybeUnary && (isOpenBracketPrev || isDelimeterPrev) && (isNumberNext || isFunctionNext || isOpenBracketNext)) { return(true); } return(false); }