aboutsummaryrefslogtreecommitdiff
path: root/src/tree.cc
diff options
context:
space:
mode:
authorAdrian Kummerländer2013-10-19 22:06:53 +0200
committerAdrian Kummerländer2013-10-19 22:06:53 +0200
commit0b3eebbf3b8644f06b2ae9d512135870938fb1c3 (patch)
treede0792d97b52328b3651acddd86293d32be54936 /src/tree.cc
parent69ce370c9ebf92caea64ef1ba28a502affdad3d5 (diff)
downloadSimpleParser-0b3eebbf3b8644f06b2ae9d512135870938fb1c3.tar
SimpleParser-0b3eebbf3b8644f06b2ae9d512135870938fb1c3.tar.gz
SimpleParser-0b3eebbf3b8644f06b2ae9d512135870938fb1c3.tar.bz2
SimpleParser-0b3eebbf3b8644f06b2ae9d512135870938fb1c3.tar.lz
SimpleParser-0b3eebbf3b8644f06b2ae9d512135870938fb1c3.tar.xz
SimpleParser-0b3eebbf3b8644f06b2ae9d512135870938fb1c3.tar.zst
SimpleParser-0b3eebbf3b8644f06b2ae9d512135870938fb1c3.zip
Merged Node factory-methods into template method
Added ConstantNode blueprint
Diffstat (limited to 'src/tree.cc')
-rw-r--r--src/tree.cc53
1 files changed, 15 insertions, 38 deletions
diff --git a/src/tree.cc b/src/tree.cc
index e63b7b3..f6d2282 100644
--- a/src/tree.cc
+++ b/src/tree.cc
@@ -9,6 +9,14 @@
namespace SimpleParser {
+Node* topNodeFrom(const std::stack<Node*>& stack) {
+ if ( !stack.empty() ) {
+ return stack.top();
+ } else {
+ throw operator_exception();
+ }
+}
+
Tree::Tree(std::string term):
term_(term) {
this->root_node_ = this->buildTree(term);
@@ -72,21 +80,10 @@ std::string Tree::print() {
return out.str();
}
-Node* Tree::addOperand(Node** place, double value) {
- this->node_collection_.emplace_back(
- new OperandNode(value)
- );
-
- if ( place != nullptr ) {
- *place = this->node_collection_.back().get();
- }
-
- return this->node_collection_.back().get();
-}
-
-Node* Tree::addOperand(Node** place, std::string value) {
+template <typename NType, typename... Args>
+Node* Tree::addNode(Node** place, Args&&... args) {
this->node_collection_.emplace_back(
- new OperandNode(value)
+ new NType(std::forward<Args>(args)...)
);
if ( place != nullptr ) {
@@ -96,26 +93,6 @@ Node* Tree::addOperand(Node** place, std::string value) {
return this->node_collection_.back().get();
}
-Node* Tree::addOperator(Node** place, TokenType token) {
- this->node_collection_.emplace_back(
- new OperatorNode(token)
- );
-
- if ( place != nullptr ) {
- *place = this->node_collection_.back().get();
- }
-
- return this->node_collection_.back().get();
-}
-
-Node* topNodeFrom(const std::stack<Node*>& stack) {
- if ( !stack.empty() ) {
- return stack.top();
- } else {
- throw operator_exception();
- }
-}
-
Node* Tree::buildTree(std::string term) {
std::stack<Node*> operandStack;
std::stack<Node*> operatorStack;
@@ -139,7 +116,7 @@ Node* Tree::buildTree(std::string term) {
if ( token > lastNode->getToken() ) {
operatorStack.push(
- this->addOperator(nullptr, token)
+ this->addNode<OperatorNode>(nullptr, token)
);
} else {
Node* currOperator = topNodeFrom(operatorStack);
@@ -157,7 +134,7 @@ Node* Tree::buildTree(std::string term) {
}
} else {
operatorStack.push(
- this->addOperator(nullptr, token)
+ this->addNode<OperatorNode>(nullptr, token)
);
}
} else {
@@ -171,14 +148,14 @@ Node* Tree::buildTree(std::string term) {
convertStream >> value;
operandStack.push(
- this->addOperand(nullptr,value)
+ this->addNode<OperandNode>(nullptr, value)
);
break;
}
case TokenType::VALUE_IDENTIFIER: {
operandStack.push(
- this->addOperand(nullptr,tmpLexer[0])
+ this->addNode<ConstantNode>(nullptr, tmpLexer[0])
);
break;