aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/nodes.cc2
-rw-r--r--src/parser.cc34
-rw-r--r--src/parser.h16
-rw-r--r--src/tree.cc2
4 files changed, 27 insertions, 27 deletions
diff --git a/src/nodes.cc b/src/nodes.cc
index 70b9e50..2ec3167 100644
--- a/src/nodes.cc
+++ b/src/nodes.cc
@@ -74,4 +74,4 @@ char OperatorNode::getFunction() {
return this->function_;
}
-}
+} // SimpleParser
diff --git a/src/parser.cc b/src/parser.cc
index 0f05790..5989027 100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -5,8 +5,16 @@
namespace SimpleParser {
-int8_t Parser::getPriority(char tmp)
-{
+double calculate(std::string term) {
+ Tree termTree;
+ termTree.setRoot(buildTree(&termTree, term));
+
+ return termTree.solve();
+}
+
+namespace {
+
+int8_t getPriority(char tmp) {
switch ( tmp ) {
case '-':
return 10;
@@ -29,8 +37,7 @@ int8_t Parser::getPriority(char tmp)
}
}
-std::vector<std::string> Parser::lexer(std::string term)
-{
+std::vector<std::string> lexer(std::string term) {
std::string tmp;
std::string tmpNum;
std::string::iterator termIter;
@@ -41,7 +48,7 @@ std::vector<std::string> Parser::lexer(std::string term)
uint32_t level = 0;
for ( termIter = term.begin(); termIter != term.end(); termIter++ ) {
- priority = this->getPriority(*termIter);
+ priority = getPriority(*termIter);
if ( priority == -1 || ( termIter == term.begin() && priority == 10 ) ) {
if ( level > 0 ) {
@@ -111,24 +118,24 @@ std::vector<std::string> Parser::lexer(std::string term)
return output;
}
-Node* Parser::buildTree(Tree *tree, std::string term) {
+Node* buildTree(Tree *tree, std::string term) {
std::stack<Node*> operandStack;
std::stack<Node*> operatorStack;
std::vector<std::string> tmpLexer;
- std::vector<std::string> lexerOutput = this->lexer(term);
+ std::vector<std::string> lexerOutput = lexer(term);
int8_t priority;
for ( auto termIter = lexerOutput.begin(); termIter != lexerOutput.end(); termIter++ ) {
std::string& currTerm = (*termIter);
- priority = this->getPriority(currTerm[0]);
+ priority = getPriority(currTerm[0]);
if ( priority != -1 && (*termIter).size() == 1 ) {
if ( !operatorStack.empty() ) {
OperatorNode *lastNode = static_cast<OperatorNode*>(operatorStack.top());
- if ( this->getPriority(lastNode->getFunction()) < priority ) {
+ if ( getPriority(lastNode->getFunction()) < priority ) {
operatorStack.push(
tree->addOperator(nullptr, currTerm[0])
);
@@ -150,7 +157,7 @@ Node* Parser::buildTree(Tree *tree, std::string term) {
operatorStack.push(tree->addOperator(nullptr, currTerm[0]));
}
} else {
- tmpLexer = this->lexer(*termIter);
+ tmpLexer = lexer(*termIter);
if ( tmpLexer.size() == 1 ) {
double value;
@@ -181,11 +188,6 @@ Node* Parser::buildTree(Tree *tree, std::string term) {
return operandStack.top();
}
-double Parser::calculate(std::string term) {
- Tree termTree;
- termTree.setRoot(this->buildTree(&termTree, term));
-
- return termTree.solve();
}
-}
+} // SimpleParser
diff --git a/src/parser.h b/src/parser.h
index b943ebe..7e594ca 100644
--- a/src/parser.h
+++ b/src/parser.h
@@ -8,15 +8,13 @@
namespace SimpleParser {
-class Parser {
- public:
- double calculate(std::string);
-
- private:
- int8_t getPriority(char);
- std::vector<std::string> lexer(std::string);
- Node* buildTree(Tree*, std::string);
-};
+double calculate(std::string);
+
+namespace {
+ int8_t getPriority(char);
+ std::vector<std::string> lexer(std::string);
+ Node* buildTree(Tree*, std::string);
+}
}
diff --git a/src/tree.cc b/src/tree.cc
index 675009e..3755423 100644
--- a/src/tree.cc
+++ b/src/tree.cc
@@ -66,4 +66,4 @@ std::string Tree::print(std::string term) {
return out.str();
}
-}
+} // SimpleParser