From 0b3eebbf3b8644f06b2ae9d512135870938fb1c3 Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Sat, 19 Oct 2013 22:06:53 +0200 Subject: Merged Node factory-methods into template method Added ConstantNode blueprint --- src/tree.cc | 53 +++++++++++++++-------------------------------------- 1 file changed, 15 insertions(+), 38 deletions(-) (limited to 'src/tree.cc') 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& 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 +Node* Tree::addNode(Node** place, Args&&... args) { this->node_collection_.emplace_back( - new OperandNode(value) + new NType(std::forward(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& stack) { - if ( !stack.empty() ) { - return stack.top(); - } else { - throw operator_exception(); - } -} - Node* Tree::buildTree(std::string term) { std::stack operandStack; std::stack operatorStack; @@ -139,7 +116,7 @@ Node* Tree::buildTree(std::string term) { if ( token > lastNode->getToken() ) { operatorStack.push( - this->addOperator(nullptr, token) + this->addNode(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(nullptr, token) ); } } else { @@ -171,14 +148,14 @@ Node* Tree::buildTree(std::string term) { convertStream >> value; operandStack.push( - this->addOperand(nullptr,value) + this->addNode(nullptr, value) ); break; } case TokenType::VALUE_IDENTIFIER: { operandStack.push( - this->addOperand(nullptr,tmpLexer[0]) + this->addNode(nullptr, tmpLexer[0]) ); break; -- cgit v1.2.3