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);
}