aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/tree.cc10
-rw-r--r--src/utils.cc53
-rw-r--r--src/utils.h13
3 files changed, 44 insertions, 32 deletions
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<std::string> tmpLexer;
std::vector<std::string> 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<OperatorNode*>(topNodeFrom(operatorStack))
);
- if ( getPriority(lastNode->getFunction()) < priority ) {
+ if ( token > getTokenType(lastNode->getFunction()) ) {
operatorStack.push(
this->addOperator(nullptr, currTerm[0])
);
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]);
}
diff --git a/src/utils.h b/src/utils.h
index f71c835..6e43605 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -8,7 +8,18 @@
namespace SimpleParser {
-int8_t getPriority(char);
+enum class TokenType : int8_t {
+ OPERATOR_PLUS = 10,
+ OPERATOR_MINUS = 11,
+ OPERATOR_DIVIDE = 20,
+ OPERATOR_MULTIPLY = 21,
+ OPERATOR_POWER = 30,
+ PARENTHESES_OPEN = 90,
+ PARENTHESES_CLOSE = 91,
+ VALUE_NUMBER = -1,
+};
+
+TokenType getTokenType(char);
std::vector<std::string> lexer(std::string);
}