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/nodes.cc | 16 ++++++++++++++++ src/nodes.h | 17 +++++++++++++++-- src/tree.cc | 53 +++++++++++++++-------------------------------------- src/tree.h | 5 ++--- 4 files changed, 48 insertions(+), 43 deletions(-) diff --git a/src/nodes.cc b/src/nodes.cc index bab7ff7..1ab1502 100644 --- a/src/nodes.cc +++ b/src/nodes.cc @@ -1,4 +1,5 @@ #include "nodes.h" +#include "utils.h" #include "exceptions.h" #include @@ -93,4 +94,19 @@ TokenType OperatorNode::getToken() { return this->operator_; } +ConstantNode::ConstantNode(std::string identifier): + identifier_(identifier) { } + +double ConstantNode::solve() { + +} + +NodeType ConstantNode::getType() { + return NodeType::CONSTANT; +} + +std::string ConstantNode::print() { + return this->identifier_; +} + } diff --git a/src/nodes.h b/src/nodes.h index e90669c..6783f3d 100644 --- a/src/nodes.h +++ b/src/nodes.h @@ -3,13 +3,14 @@ #include -#include "utils.h" - namespace SimpleParser { +enum class TokenType : int8_t; + enum class NodeType { OPERAND, OPERATOR, + CONSTANT, }; class Node { @@ -50,6 +51,18 @@ class OperandNode: public Node { double value_; }; +class ConstantNode: public Node { + public: + explicit ConstantNode(std::string); + + virtual double solve(); + virtual NodeType getType(); + virtual std::string print(); + + private: + std::string identifier_; +}; + } #endif // PARSER_SRC_NODES_H_ 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; diff --git a/src/tree.h b/src/tree.h index 858d737..68a31c1 100644 --- a/src/tree.h +++ b/src/tree.h @@ -17,9 +17,8 @@ class Tree { std::string print(); private: - Node* addOperand(Node**, double); - Node* addOperand(Node**, std::string); - Node* addOperator(Node**, TokenType); + template + Node* addNode(Node**, Args&&... args); Node* buildTree(std::string); std::vector> node_collection_; -- cgit v1.2.3