aboutsummaryrefslogtreecommitdiff
path: root/src/utils.cc
diff options
context:
space:
mode:
authorAdrian Kummerländer2013-10-19 20:11:56 +0200
committerAdrian Kummerländer2013-10-19 20:11:56 +0200
commit9a95b5d24b2b2f5111e1862875d4136964a59548 (patch)
tree9ac96fdf846ae19e2278f9540593823fa8ba3b21 /src/utils.cc
parentaca18e1803b3d54e6c9d7444e8b9c1bf09d12f52 (diff)
downloadSimpleParser-9a95b5d24b2b2f5111e1862875d4136964a59548.tar
SimpleParser-9a95b5d24b2b2f5111e1862875d4136964a59548.tar.gz
SimpleParser-9a95b5d24b2b2f5111e1862875d4136964a59548.tar.bz2
SimpleParser-9a95b5d24b2b2f5111e1862875d4136964a59548.tar.lz
SimpleParser-9a95b5d24b2b2f5111e1862875d4136964a59548.tar.xz
SimpleParser-9a95b5d24b2b2f5111e1862875d4136964a59548.tar.zst
SimpleParser-9a95b5d24b2b2f5111e1862875d4136964a59548.zip
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
Diffstat (limited to 'src/utils.cc')
-rw-r--r--src/utils.cc53
1 files changed, 28 insertions, 25 deletions
diff --git a/src/utils.cc b/src/utils.cc
index 7457ae2..112bc20 100644
--- a/src/utils.cc
+++ b/src/utils.cc
@@ -5,26 +5,26 @@
namespace SimpleParser {
-int8_t getPriority(char tmp) {
+TokenType getTokenType(char tmp) {
switch ( tmp ) {
case '-':
- return 10;
+ return TokenType::OPERATOR_PLUS;
case '+':
- return 10;
+ return TokenType::OPERATOR_MINUS;
case '/':
- return 20;
+ return TokenType::OPERATOR_DIVIDE;
case '*':
- return 20;
+ return TokenType::OPERATOR_MULTIPLY;
case '^':
- return 30;
+ return TokenType::OPERATOR_POWER;
case '(':
- return 90;
+ return TokenType::PARENTHESES_OPEN;
case ')':
- return 90;
+ return TokenType::PARENTHESES_CLOSE;
case ',':
- return -1;
+ return TokenType::VALUE_NUMBER;
default:
- return -1;
+ return TokenType::VALUE_NUMBER;
}
}
@@ -32,31 +32,33 @@ std::vector<std::string> lexer(std::string term) {
std::string tmp;
std::string tmpNum;
std::vector<std::string> output;
+ TokenType token;
+ TokenType lastToken;
- int8_t priority = 0;
- int8_t lastPriority = 0;
- uint32_t level = 0;
+ uint32_t level = 0;
for ( auto termIter = term.begin();
termIter != term.end();
termIter++ ) {
- priority = getPriority(*termIter);
+ token = getTokenType(*termIter);
- if ( priority == -1 || ( termIter == term.begin() &&
- priority == 10 ) ) {
+ if ( token == TokenType::VALUE_NUMBER ||
+ ( token == TokenType::OPERATOR_MINUS &&
+ termIter == term.begin() ) ) {
if ( level > 0 ) {
tmp += *termIter;
} else {
tmpNum += *termIter;
}
} else {
- if ( lastPriority == -1 && level == 0 ) {
+ if ( lastToken == TokenType::VALUE_NUMBER &&
+ level == 0 ) {
output.push_back(tmpNum);
tmpNum.clear();
}
- switch ( *termIter ) {
- case '(': {
+ switch ( token ) {
+ case TokenType::PARENTHESES_OPEN: {
if ( level > 0 ) {
tmp += *termIter;
}
@@ -65,7 +67,7 @@ std::vector<std::string> lexer(std::string term) {
break;
}
- case ')': {
+ case TokenType::PARENTHESES_CLOSE: {
level--;
if ( level == 0 ) {
@@ -92,20 +94,21 @@ std::vector<std::string> lexer(std::string term) {
}
}
- lastPriority = priority;
+ lastToken = token;
}
- if ( lastPriority == -1 ) {
+ if ( lastToken == TokenType::VALUE_NUMBER ) {
output.push_back(tmpNum);
- } else if ( lastPriority != 90 ) {
+ } else if ( lastToken != TokenType::PARENTHESES_CLOSE ) {
throw operator_exception();
}
- if (level != 0) {
+ if ( level != 0 ) {
throw parenthese_exception();
}
- if ( lastPriority == 90 && output.size() == 1 ) {
+ if ( lastToken == TokenType::PARENTHESES_CLOSE &&
+ output.size() == 1 ) {
output = lexer(output[0]);
}