aboutsummaryrefslogtreecommitdiff
path: root/src/parser.cpp
diff options
context:
space:
mode:
authorAdrian Kummerländer2013-01-05 22:30:35 +0100
committerAdrian Kummerländer2013-01-05 22:30:35 +0100
commitcde848ce1eb995170723f6f070b9fcba0dfdb880 (patch)
treeb28b436619ade0f9b3ff7603cc987d7b5a621ff3 /src/parser.cpp
parente3081360c65eb4994e7e8042898cec72de0d560b (diff)
downloadSimpleParser-cde848ce1eb995170723f6f070b9fcba0dfdb880.tar
SimpleParser-cde848ce1eb995170723f6f070b9fcba0dfdb880.tar.gz
SimpleParser-cde848ce1eb995170723f6f070b9fcba0dfdb880.tar.bz2
SimpleParser-cde848ce1eb995170723f6f070b9fcba0dfdb880.tar.lz
SimpleParser-cde848ce1eb995170723f6f070b9fcba0dfdb880.tar.xz
SimpleParser-cde848ce1eb995170723f6f070b9fcba0dfdb880.tar.zst
SimpleParser-cde848ce1eb995170723f6f070b9fcba0dfdb880.zip
Moved node classes into separate compilation unit; File extension change
Diffstat (limited to 'src/parser.cpp')
-rw-r--r--src/parser.cpp190
1 files changed, 0 insertions, 190 deletions
diff --git a/src/parser.cpp b/src/parser.cpp
deleted file mode 100644
index 3aaf7e2..0000000
--- a/src/parser.cpp
+++ /dev/null
@@ -1,190 +0,0 @@
-#include "parser.h"
-
-#include <sstream>
-
-namespace SimpleParser {
-
-int8_t Parser::getPriority(char tmp)
-{
- switch ( tmp ) {
- case '-':
- return 10;
- case '+':
- return 10;
- case '/':
- return 20;
- case '*':
- return 20;
- case '^':
- return 30;
- case '(':
- return 90;
- case ')':
- return 90;
- case ',':
- return -1;
- default:
- return -1;
- }
-}
-
-std::vector<std::string> Parser::lexer(std::string term)
-{
- std::string tmp;
- std::string tmpNum;
- std::string::iterator termIter;
- std::vector<std::string> output;
-
- int8_t priority = 0;
- int8_t lastPriority = 0;
- uint32_t level = 0;
-
- for ( termIter = term.begin(); termIter != term.end(); termIter++ ) {
- priority = this->getPriority(*termIter);
-
- if ( priority == -1 || ( termIter == term.begin() && priority == 10 ) ) {
- if ( level > 0 ) {
- tmp += *termIter;
- } else {
- tmpNum += *termIter;
- }
- } else {
- if ( lastPriority == -1 && level == 0 ) {
- output.push_back(tmpNum);
- tmpNum.clear();
- }
-
- switch ( *termIter ) {
- case '(': {
- if ( level > 0 ) {
- tmp += *termIter;
- }
-
- level++;
- break;
- }
- case ')': {
- level--;
-
- if ( level == 0 ) {
- output.push_back(tmp);
- tmp.clear();
- }
- else {
- tmp += *termIter;
- }
- break;
- }
- default: {
- if ( level == 0 ) {
- std::string helper;
- helper = *termIter;
-
- output.push_back(helper);
- }
- else {
- tmp += *termIter;
- }
- break;
- }
- }
- }
-
- lastPriority = priority;
- }
-
- if ( lastPriority == -1 ) {
- output.push_back(tmpNum);
- } else if ( lastPriority != 90 ) {
- throw operator_exception();
- }
-
- if (level != 0) {
- throw parenthese_exception();
- }
-
- if ( lastPriority == 90 && output.size() == 1 ) {
- output = lexer(output[0]);
- }
-
- return output;
-}
-
-Node* Parser::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);
-
- int8_t priority;
-
- for ( auto termIter = lexerOutput.begin(); termIter != lexerOutput.end(); termIter++ ) {
- std::string& currTerm = (*termIter);
- priority = this->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 ) {
- operatorStack.push(
- tree->addOperator(nullptr, currTerm[0])
- );
- } else {
- Node *currOperator = operatorStack.top();
- operatorStack.pop();
-
- currOperator->rightChild = operandStack.top();
- operandStack.pop();
-
- currOperator->leftChild = operandStack.top();
- operandStack.pop();
-
- operandStack.push( currOperator );
-
- termIter--;
- }
- } else {
- operatorStack.push(tree->addOperator(nullptr, currTerm[0]));
- }
- } else {
- tmpLexer = this->lexer(*termIter);
-
- if ( tmpLexer.size() == 1 ) {
- double value;
- std::istringstream convertStream(tmpLexer[0]);
- convertStream >> value;
-
- operandStack.push(tree->addOperand(nullptr,value));
- }
- else if ( tmpLexer.size() > 1 ) {
- operandStack.push(buildTree(tree, *termIter));
- }
- }
- }
-
- while ( !operatorStack.empty() ) {
- OperatorNode *currOperator = static_cast<OperatorNode*>(operatorStack.top());
- operatorStack.pop();
-
- currOperator->rightChild = operandStack.top();
- operandStack.pop();
-
- currOperator->leftChild = operandStack.top();
- operandStack.pop();
-
- operandStack.push(currOperator);
- }
-
- return operandStack.top();
-}
-
-double Parser::calculate(std::string term) {
- Tree termTree;
- termTree.setRoot(this->buildTree(&termTree, term));
-
- return termTree.solve();
-}
-
-}