aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerländer2013-07-29 21:07:40 +0200
committerAdrian Kummerländer2013-07-29 21:07:40 +0200
commitb765b57739463d0aa3b833a70e0ea87bca68e82e (patch)
tree096324034bd40cdb88fe4855c86194f3d4b1b48b
parent948cb0482be1783db036f40fd4f93a3355da232f (diff)
downloadSimpleParser-b765b57739463d0aa3b833a70e0ea87bca68e82e.tar
SimpleParser-b765b57739463d0aa3b833a70e0ea87bca68e82e.tar.gz
SimpleParser-b765b57739463d0aa3b833a70e0ea87bca68e82e.tar.bz2
SimpleParser-b765b57739463d0aa3b833a70e0ea87bca68e82e.tar.lz
SimpleParser-b765b57739463d0aa3b833a70e0ea87bca68e82e.tar.xz
SimpleParser-b765b57739463d0aa3b833a70e0ea87bca68e82e.tar.zst
SimpleParser-b765b57739463d0aa3b833a70e0ea87bca68e82e.zip
Coding style improvements
-rw-r--r--src/nodes.cc16
-rw-r--r--src/parser.cc58
-rw-r--r--src/tree.cc18
3 files changed, 58 insertions, 34 deletions
diff --git a/src/nodes.cc b/src/nodes.cc
index 2ec3167..4710400 100644
--- a/src/nodes.cc
+++ b/src/nodes.cc
@@ -7,9 +7,8 @@
namespace SimpleParser {
-OperandNode::OperandNode(double val) {
- this->value_ = val;
-}
+OperandNode::OperandNode(double val):
+ value_(val) { }
double OperandNode::solve() {
return this->value_;
@@ -28,9 +27,8 @@ std::string OperandNode::print() {
return convertStream.str();
}
-OperatorNode::OperatorNode(char op) {
- this->function_ = op;
-}
+OperatorNode::OperatorNode(char op):
+ function_(op) { }
double OperatorNode::solve() {
switch ( this->function_ ) {
@@ -54,7 +52,9 @@ double OperatorNode::solve() {
return this->leftChild->solve() - this->rightChild->solve();
}
case '^': {
- return std::pow( this->leftChild->solve(), this->rightChild->solve() );
+ return std::pow(
+ this->leftChild->solve(), this->rightChild->solve()
+ );
}
default: {
throw operator_exception();
@@ -74,4 +74,4 @@ char OperatorNode::getFunction() {
return this->function_;
}
-} // SimpleParser
+}
diff --git a/src/parser.cc b/src/parser.cc
index 53eb311..25ffc14 100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -8,14 +8,18 @@ namespace SimpleParser {
double calculate(std::string term) {
Tree termTree;
- termTree.setRoot(buildTree(&termTree, term));
+ termTree.setRoot(
+ buildTree(&termTree, term)
+ );
return termTree.solve();
}
std::string exportTree(std::string term) {
Tree termTree;
- termTree.setRoot(buildTree(&termTree, term));
+ termTree.setRoot(
+ buildTree(&termTree, term)
+ );
return termTree.print(term);
}
@@ -48,17 +52,19 @@ int8_t getPriority(char tmp) {
std::vector<std::string> 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++ ) {
+ for ( auto termIter = term.begin();
+ termIter != term.end();
+ termIter++ ) {
priority = getPriority(*termIter);
- if ( priority == -1 || ( termIter == term.begin() && priority == 10 ) ) {
+ if ( priority == -1 || ( termIter == term.begin() &&
+ priority == 10 ) ) {
if ( level > 0 ) {
tmp += *termIter;
} else {
@@ -77,6 +83,7 @@ std::vector<std::string> lexer(std::string term) {
}
level++;
+
break;
}
case ')': {
@@ -85,10 +92,10 @@ std::vector<std::string> lexer(std::string term) {
if ( level == 0 ) {
output.push_back(tmp);
tmp.clear();
- }
- else {
+ } else {
tmp += *termIter;
}
+
break;
}
default: {
@@ -97,10 +104,10 @@ std::vector<std::string> lexer(std::string term) {
helper = *termIter;
output.push_back(helper);
- }
- else {
+ } else {
tmp += *termIter;
}
+
break;
}
}
@@ -135,34 +142,40 @@ Node* buildTree(Tree *tree, std::string term) {
int8_t priority;
- for ( auto termIter = lexerOutput.begin(); termIter != lexerOutput.end(); termIter++ ) {
+ for ( auto termIter = lexerOutput.begin();
+ termIter != lexerOutput.end();
+ termIter++ ) {
std::string& currTerm = (*termIter);
priority = getPriority(currTerm[0]);
if ( priority != -1 && (*termIter).size() == 1 ) {
if ( !operatorStack.empty() ) {
- OperatorNode *lastNode = static_cast<OperatorNode*>(operatorStack.top());
+ OperatorNode* lastNode(
+ static_cast<OperatorNode*>(operatorStack.top())
+ );
if ( getPriority(lastNode->getFunction()) < priority ) {
operatorStack.push(
tree->addOperator(nullptr, currTerm[0])
);
} else {
- Node *currOperator = operatorStack.top();
+ Node* currOperator = operatorStack.top();
operatorStack.pop();
currOperator->rightChild = operandStack.top();
operandStack.pop();
- currOperator->leftChild = operandStack.top();
+ currOperator->leftChild = operandStack.top();
operandStack.pop();
- operandStack.push( currOperator );
+ operandStack.push(currOperator);
termIter--;
}
} else {
- operatorStack.push(tree->addOperator(nullptr, currTerm[0]));
+ operatorStack.push(
+ tree->addOperator(nullptr, currTerm[0])
+ );
}
} else {
tmpLexer = lexer(*termIter);
@@ -172,22 +185,25 @@ Node* buildTree(Tree *tree, std::string term) {
std::istringstream convertStream(tmpLexer[0]);
convertStream >> value;
- operandStack.push(tree->addOperand(nullptr,value));
- }
- else if ( tmpLexer.size() > 1 ) {
+ 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());
+ OperatorNode *currOperator(
+ static_cast<OperatorNode*>(operatorStack.top())
+ );
operatorStack.pop();
currOperator->rightChild = operandStack.top();
operandStack.pop();
- currOperator->leftChild = operandStack.top();
+ currOperator->leftChild = operandStack.top();
operandStack.pop();
operandStack.push(currOperator);
@@ -198,4 +214,4 @@ Node* buildTree(Tree *tree, std::string term) {
}
-} // SimpleParser
+}
diff --git a/src/tree.cc b/src/tree.cc
index 0fa41ad..5c47a50 100644
--- a/src/tree.cc
+++ b/src/tree.cc
@@ -15,7 +15,9 @@ double Tree::solve() {
}
Node* Tree::addOperand(Node** place, double value) {
- this->node_collection_.emplace_back(new OperandNode(value));
+ this->node_collection_.emplace_back(
+ new OperandNode(value)
+ );
if ( place != nullptr ) {
*place = this->node_collection_.back().get();
@@ -25,7 +27,9 @@ Node* Tree::addOperand(Node** place, double value) {
}
Node* Tree::addOperator(Node** place, char oper) {
- this->node_collection_.emplace_back(new OperatorNode(oper));
+ this->node_collection_.emplace_back(
+ new OperatorNode(oper)
+ );
if ( place != nullptr ) {
*place = this->node_collection_.back().get();
@@ -47,7 +51,9 @@ std::string Tree::print(std::string term) {
int i = 0;
- for ( auto it = this->node_collection_.begin(); it != this->node_collection_.end(); ++it ) {
+ for ( auto it = this->node_collection_.begin();
+ it != this->node_collection_.end();
+ ++it ) {
out << "node"
<< i
<< " [ label = \""
@@ -56,7 +62,9 @@ std::string Tree::print(std::string term) {
<< std::endl;
if ( (*it)->getType() == OPERATOR_NODE ) {
- for ( auto iter = this->node_collection_.begin(); iter != this->node_collection_.end(); ++iter ) {
+ for ( auto iter = this->node_collection_.begin();
+ iter != this->node_collection_.end();
+ ++iter ) {
if ( (*iter).get() == (*it)->leftChild ) {
out << "\"node"
<< i
@@ -84,4 +92,4 @@ std::string Tree::print(std::string term) {
return out.str();
}
-} // SimpleParser
+}