aboutsummaryrefslogtreecommitdiff
path: root/src/utils.cc
diff options
context:
space:
mode:
authorAdrian Kummerländer2013-10-19 20:53:51 +0200
committerAdrian Kummerländer2013-10-19 20:53:51 +0200
commitc292b29f3bb87cf11edf899c023cfb574c20ed6c (patch)
treeca1915dd4ed8edff605b77e6db9820b9e5d9232b /src/utils.cc
parent754cc721222ccb01acc4d93ffd8f88f172a0cdd0 (diff)
parent9a95b5d24b2b2f5111e1862875d4136964a59548 (diff)
downloadSimpleParser-c292b29f3bb87cf11edf899c023cfb574c20ed6c.tar
SimpleParser-c292b29f3bb87cf11edf899c023cfb574c20ed6c.tar.gz
SimpleParser-c292b29f3bb87cf11edf899c023cfb574c20ed6c.tar.bz2
SimpleParser-c292b29f3bb87cf11edf899c023cfb574c20ed6c.tar.lz
SimpleParser-c292b29f3bb87cf11edf899c023cfb574c20ed6c.tar.xz
SimpleParser-c292b29f3bb87cf11edf899c023cfb574c20ed6c.tar.zst
SimpleParser-c292b29f3bb87cf11edf899c023cfb574c20ed6c.zip
Merge branch 'master' into feature_constants
Conflicts: src/tree.cc src/utils.cc
Diffstat (limited to 'src/utils.cc')
-rw-r--r--src/utils.cc61
1 files changed, 32 insertions, 29 deletions
diff --git a/src/utils.cc b/src/utils.cc
index 8914166..4e2ed91 100644
--- a/src/utils.cc
+++ b/src/utils.cc
@@ -6,29 +6,29 @@
namespace SimpleParser {
-int8_t getPriority(char tmp) {
+TokenType getTokenType(char tmp) {
if ( std::isalpha(tmp) ) {
- return 100;
+ return TokenType::VALUE_IDENTIFIER;
} else {
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;
}
}
}
@@ -38,40 +38,42 @@ std::vector<std::string> lexer(std::string term) {
std::string tmpNumber;
std::string tmpIdentifier;
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 || priority == 100 || ( termIter == term.begin() &&
- priority == 10 ) ) {
+ if ( token == TokenType::VALUE_NUMBER ||
+ token == TokenType::VALUE_IDENTIFIER ||
+ ( token == TokenType::OPERATOR_MINUS &&
+ termIter == term.begin() ) ) {
if ( level > 0 ) {
tmp += *termIter;
} else {
- if ( priority == -1 ) {
+ if ( token == TokenType::VALUE_NUMBER ) {
tmpNumber += *termIter;
- } else if ( priority == 100 ) {
+ } else if ( token == TokenType::VALUE_IDENTIFIER ) {
tmpIdentifier += *termIter;
}
}
} else {
if ( level == 0 ) {
- if ( lastPriority == -1 ) {
+ if ( lastToken == TokenType::VALUE_NUMBER ) {
output.push_back(tmpNumber);
tmpNumber.clear();
- } else if ( lastPriority == 100 ) {
+ } else if ( lastToken == TokenType::VALUE_IDENTIFIER ) {
output.push_back(tmpIdentifier);
tmpIdentifier.clear();
}
}
- switch ( *termIter ) {
- case '(': {
+ switch ( token ) {
+ case TokenType::PARENTHESES_OPEN: {
if ( level > 0 ) {
tmp += *termIter;
}
@@ -80,7 +82,7 @@ std::vector<std::string> lexer(std::string term) {
break;
}
- case ')': {
+ case TokenType::PARENTHESES_CLOSE: {
level--;
if ( level == 0 ) {
@@ -107,14 +109,14 @@ std::vector<std::string> lexer(std::string term) {
}
}
- lastPriority = priority;
+ lastToken = token;
}
- if ( lastPriority == -1 ) {
+ if ( lastToken == TokenType::VALUE_NUMBER ) {
output.push_back(tmpNumber);
- } else if ( lastPriority == 100 ) {
+ } else if ( lastToken == TokenType::VALUE_IDENTIFIER ) {
output.push_back(tmpIdentifier);
- } else if ( lastPriority != 90 ) {
+ } else if ( lastToken != TokenType::PARENTHESES_CLOSE ) {
throw operator_exception();
}
@@ -122,7 +124,8 @@ std::vector<std::string> lexer(std::string term) {
throw parenthese_exception();
}
- if ( lastPriority == 90 && output.size() == 1 ) {
+ if ( lastToken == TokenType::PARENTHESES_CLOSE &&
+ output.size() == 1 ) {
output = lexer(output[0]);
}