Arithmetica.Tokenization.Rewriting.ImplicitMultiplicationRewriter.Rewrite C# (CSharp) Метод

Rewrite() публичный Метод

public Rewrite ( TokenStream tokens ) : TokenStream
tokens TokenStream
Результат TokenStream
        public override TokenStream Rewrite(TokenStream tokens)
        {
            // Token stream needs to be in infix notation.
            if (tokens.Notation != TokenNotation.Infix)
            {
                throw new MathExpressionException("Token stream is not in infix notation.");
            }

            List<Token> result = new List<Token>();
            for (int index = 0; index < tokens.Count; index++)
            {
                if (index > 0)
                {
                    // Was the previous token an operand and the current one an opening parenthesis?
                    if (tokens[index].Type == TokenType.OpeningParenthesis && tokens[index - 1].IsOperand())
                    {
                        // Insert an multiplication operator token.
                        result.Add(new Token(TokenType.Multiplication, "*"));
                    }
                    // Was the previous token a constant and the current one is a variable?
                    if (tokens[index].Type == TokenType.Variable && tokens[index - 1].Type == TokenType.Numeric)
                    {
                        result.Add(new Token(TokenType.Multiplication, "*"));
                    }
                }
                result.Add(tokens[index]);
            }
            return new TokenStream(result, TokenNotation.Infix);
        }

Usage Example

 public void ImplicitMultiplicationRewriter_TokenStream_Needs_To_Be_In_Infix_Notation()
 {
     TokenStream stream = new TokenStream(Enumerable.Empty<Token>(), TokenNotation.Postfix);
     ImplicitMultiplicationRewriter rewriter = new ImplicitMultiplicationRewriter();
     var exception = Assert.Throws<MathExpressionException>(() => rewriter.Rewrite(stream));
     Assert.AreEqual("Token stream is not in infix notation.", exception.Message);
 }
All Usage Examples Of Arithmetica.Tokenization.Rewriting.ImplicitMultiplicationRewriter::Rewrite
ImplicitMultiplicationRewriter