diff options
author | Adrian Kummerländer | 2013-01-05 23:06:51 +0100 |
---|---|---|
committer | Adrian Kummerländer | 2013-01-05 23:06:51 +0100 |
commit | 39038fddb2032c9944e549be8f1665e028cb68b8 (patch) | |
tree | 69b27a62fcb828602f458746976b223c0ecf4d11 /src | |
parent | cde848ce1eb995170723f6f070b9fcba0dfdb880 (diff) | |
download | SimpleParser-39038fddb2032c9944e549be8f1665e028cb68b8.tar SimpleParser-39038fddb2032c9944e549be8f1665e028cb68b8.tar.gz SimpleParser-39038fddb2032c9944e549be8f1665e028cb68b8.tar.bz2 SimpleParser-39038fddb2032c9944e549be8f1665e028cb68b8.tar.lz SimpleParser-39038fddb2032c9944e549be8f1665e028cb68b8.tar.xz SimpleParser-39038fddb2032c9944e549be8f1665e028cb68b8.tar.zst SimpleParser-39038fddb2032c9944e549be8f1665e028cb68b8.zip |
Deprecated parser class in favour of plain functions; Internal lexer,
priority determination and tree building functions were hidden in
unnamed namespace
Diffstat (limited to 'src')
-rw-r--r-- | src/nodes.cc | 2 | ||||
-rw-r--r-- | src/parser.cc | 34 | ||||
-rw-r--r-- | src/parser.h | 16 | ||||
-rw-r--r-- | src/tree.cc | 2 |
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 |