예제 #1
0
 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();
 }
예제 #2
0
 private string GetPrevNotSpace()
 {
     for (int i = index - 1; i >= 0; i--)
     {
         if (!TokenInitializer.IsSpace(tokens[i]))
         {
             return(tokens[i]);
         }
     }
     return("");
 }
예제 #3
0
 private string GetNextNotSpace()
 {
     for (int i = index + 1; i < tokens.Count; i++)
     {
         if (!TokenInitializer.IsSpace(tokens[i]))
         {
             return(tokens[i]);
         }
     }
     return("");
 }
예제 #4
0
        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);
        }