From 9a95b5d24b2b2f5111e1862875d4136964a59548 Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Sat, 19 Oct 2013 20:11:56 +0200 Subject: Improvement: Replaced "priority" with TokenType * Made implementation more expressive by replacing the integer priority with an strictly typed enum called TokenType ** Made removal of character comparisons from tree construction and lexer possible * As a side effect distinct numbers had to be assigned to each token ** Operators of same priority do not have identical numbers anymore --- src/tree.cc | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'src/tree.cc') diff --git a/src/tree.cc b/src/tree.cc index 1dcfca6..0f5c2d8 100644 --- a/src/tree.cc +++ b/src/tree.cc @@ -111,21 +111,19 @@ Node* Tree::buildTree(std::string term) { std::vector tmpLexer; std::vector lexerOutput = lexer(term); - int8_t priority; - for ( auto termIter = lexerOutput.begin(); termIter != lexerOutput.end(); termIter++ ) { - std::string& currTerm = (*termIter); - priority = getPriority(currTerm[0]); + const std::string& currTerm = (*termIter); + const TokenType token = getTokenType(currTerm[0]); - if ( priority != -1 && (*termIter).size() == 1 ) { + if ( token != TokenType::VALUE_NUMBER && (*termIter).size() == 1 ) { if ( !operatorStack.empty() ) { OperatorNode* lastNode( static_cast(topNodeFrom(operatorStack)) ); - if ( getPriority(lastNode->getFunction()) < priority ) { + if ( token > getTokenType(lastNode->getFunction()) ) { operatorStack.push( this->addOperator(nullptr, currTerm[0]) ); -- cgit v1.2.3