private string evalSignOperator(SignOperatorToken token) { string expr = token.SubTokens[1].Evaluate(this); bool brackets = token.SubTokens[1] is OperatorToken; if (brackets) { expr = $"<mo>(</mo>{expr}<mo>)</mo>"; } return($"<mo>{token.Symbol}</mo>" + expr); }
private string evalSignOperator(SignOperatorToken token) { string expr = token.SubTokens[1].Evaluate(this); bool brackets = token.SubTokens[1] is OperatorToken; if (brackets) { expr = String.Format("({0})", expr); } return(token.Symbol + expr); }
/// <summary> /// Reads the next token from the input stream and adds it to the given token list. /// </summary> private Token readNextToken(LinkedList <Token> tokenList, Token lastToken, InputStream str) { if (str.AtEnd) { throw new ParserSyntaxException(str.Position); } // Check for numbers if (numberReader(str, out Token outtok)) { tokenList.AddLast(outtok); return(outtok); } // Check for operators, functions, vars foreach (var item in TokenDict) { int pos = str.Position; if (str.StartsWith(item.Key)) { Token newToken; if (item.Value.Item2 != 0) { newToken = (Token)Activator.CreateInstance(item.Value.Item1, item.Key, pos, item.Value.Item2); } else if (item.Value.Item1 == typeof(LeftBracketToken) || item.Value.Item1 == typeof(RightBracketToken)) { newToken = (Token)Activator.CreateInstance(item.Value.Item1, pos); } else { newToken = (Token)Activator.CreateInstance(item.Value.Item1, item.Key, pos); } // Special handling for '+' and '-' signs if (lastToken is null || lastToken is StructToken || lastToken is LeftBracketToken) { if (item.Key == ParserSymbols.Add) { Token nextToken = readNextToken(tokenList, newToken, str); if (nextToken is OperatorToken) { throw new ParserSyntaxException("Invalid operator sequence.", pos); } return(nextToken); } else if (item.Key == ParserSymbols.Subtract) { var signToken = new SignOperatorToken(pos); tokenList.AddLast(signToken); return(signToken); } } // Special handling for syntax that omits the '*' operator if (newToken is VarToken || newToken is ConstantToken) { if (lastToken is NumberToken) { tokenList.AddLast(new OperatorToken(ParserSymbols.Multiply, pos)); } } tokenList.AddLast(newToken); return(newToken); } } // If no valid valid token was found throw new ParserCharException(str.Position, str.FirstChar()); }