mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-18 21:30:38 +01:00
[poincare] Simpler precedence comparison in parser
This commit is contained in:
committed by
Émilie Feral
parent
f5cbd56ac0
commit
20a1e048de
@@ -50,32 +50,18 @@ static inline bool tokenTypesCanBeImplicitlyMultiplied(Token::Type t1, Token::Ty
|
||||
//TODO if (t1 == Token::Type::Identifier && t2 == Token::Type::LeftParenthesis) t1 should be parsed as a function
|
||||
}
|
||||
|
||||
static inline bool comparePrecedence(Token::Type nextTokenType, Token::Type stoppingType) {
|
||||
// if (stoppingType == EndOfStream) return nextTokenType > EndOfStream
|
||||
// if (stoppingType == RightParenthesis) return nextTokenType > RightParenthesis
|
||||
// if (stoppingType == Plus) return nextTokenType > Plus
|
||||
// if (stoppingType == Times) return nextTokenType > Times
|
||||
// if (stoppingType == Power) return nextTokenType >= Power // >= makes the operator right-associative
|
||||
// EndOfStream < RightParenthesis < Plus < Times < Power
|
||||
return
|
||||
(nextTokenType > stoppingType)
|
||||
||
|
||||
(nextTokenType == stoppingType && stoppingType == Token::Type::Power);
|
||||
}
|
||||
|
||||
bool Parser::canPopToken(Token::Type stoppingType) {
|
||||
if (tokenTypesCanBeImplicitlyMultiplied(m_currentToken.type(), m_nextToken.type())) {
|
||||
m_currentToken = Token(Token::Type::Times);
|
||||
return true;
|
||||
}
|
||||
if (comparePrecedence(m_nextToken.type(), stoppingType)) {
|
||||
if (m_nextToken.type() > stoppingType) {
|
||||
popToken();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/* Specific TokenParsers */
|
||||
|
||||
Expression Parser::parseNumber(Expression leftHandSide) {
|
||||
@@ -108,7 +94,7 @@ Expression Parser::parseMinus(Expression leftHandSide) {
|
||||
|
||||
Expression Parser::parsePower(Expression leftHandSide) {
|
||||
assert(!leftHandSide.isUninitialized());
|
||||
return Power(leftHandSide, parseUntil(Token::Type::Power)); // Power is right-associative
|
||||
return Power(leftHandSide, parseUntil(Token::Type::Slash)); // Power is right-associative
|
||||
}
|
||||
|
||||
Expression Parser::parseLeftParenthesis(Expression leftHandSide) {
|
||||
|
||||
Reference in New Issue
Block a user