aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2014-09-26 22:24:49 +0200
committerAdrian Kummerlaender2014-09-26 22:24:49 +0200
commit3aad19966f81189219d00d0ea905b6b1e83d5576 (patch)
treedc1128f5cb0315466292c13755aaaf2bdd947497
parentb1e863bf2ae274da155446a8bb4aec5f975a86c5 (diff)
downloadSimpleParser-3aad19966f81189219d00d0ea905b6b1e83d5576.tar
SimpleParser-3aad19966f81189219d00d0ea905b6b1e83d5576.tar.gz
SimpleParser-3aad19966f81189219d00d0ea905b6b1e83d5576.tar.bz2
SimpleParser-3aad19966f81189219d00d0ea905b6b1e83d5576.tar.lz
SimpleParser-3aad19966f81189219d00d0ea905b6b1e83d5576.tar.xz
SimpleParser-3aad19966f81189219d00d0ea905b6b1e83d5576.tar.zst
SimpleParser-3aad19966f81189219d00d0ea905b6b1e83d5576.zip
Simplified Tree::addNode and continued cleanup
* member method "addNode" of "Tree" was relieved of its uneccessary "place" argument * again contified where possible ** e.g. the "buildTree" local element vectors for storing the lexer result
-rw-r--r--src/tree.cc62
-rw-r--r--src/tree.h7
2 files changed, 31 insertions, 38 deletions
diff --git a/src/tree.cc b/src/tree.cc
index 8ccb108..11bb299 100644
--- a/src/tree.cc
+++ b/src/tree.cc
@@ -52,15 +52,16 @@ std::string Tree::print() const {
<< this->term_
<< "\""
<< std::endl
- << "{" << std::endl
+ << "{"
+ << std::endl
<< "node [shape = box];"
<< std::endl;
- size_t i{};
+ std::size_t nodeIndex{};
for ( auto&& node : this->node_collection_ ) {
out << "node"
- << i
+ << nodeIndex
<< " [ label = \""
<< node->print()
<< "\"];"
@@ -68,58 +69,49 @@ std::string Tree::print() const {
if ( node->rightChild != nullptr &&
node->leftChild != nullptr ) {
- size_t j{};
+ std::size_t childIndex{};
for ( auto&& child : this->node_collection_ ) {
- if ( child.get() == node->leftChild ) {
+ if ( child.get() == node->leftChild ||
+ child.get() == node->rightChild ) {
out << "\"node"
- << i
- << "\" -> \"node"
- << j
+ << nodeIndex
+ << "\" -> \"node"
+ << childIndex
<< "\";"
<< std::endl;
}
- if ( child.get() == node->rightChild ) {
- out << "\"node"
- << i
- << "\" -> \"node"
- << j
- << "\";"
- << std::endl;
- }
-
- ++j;
+ ++childIndex;
}
}
- ++i;
+ ++nodeIndex;
}
- out << "}"
- << std::endl;
+ out << "}" << std::endl;
return out.str();
}
-template <typename NType, typename... Args>
-Node* Tree::addNode(Node** place, Args&&... args) {
+template <typename NodeType, typename... Args>
+Node* Tree::addNode(Args&&... args) {
this->node_collection_.emplace_back(
- std::make_unique<NType>(std::forward<Args>(args)...)
+ std::make_unique<NodeType>(
+ std::forward<Args>(args)...
+ )
);
- if ( place != nullptr ) {
- *place = this->node_collection_.back().get();
- }
-
return this->node_collection_.back().get();
}
-Node* Tree::buildTree(std::string term) {
+Node* Tree::buildTree(const std::string& term) {
std::stack<Node*> operands;
std::stack<Node*> operators;
- std::vector<std::string> topElements(lexer(term));
+ const std::vector<std::string> topElements{
+ lexer(term)
+ };
for ( auto elementIterator = topElements.begin();
elementIterator != topElements.end();
@@ -132,7 +124,7 @@ Node* Tree::buildTree(std::string term) {
element.size() == 1 ) {
if ( operators.empty() ) {
operators.push(
- this->addNode<OperatorNode>(nullptr, elementToken)
+ this->addNode<OperatorNode>(elementToken)
);
} else {
OperatorNode*const lastNode(
@@ -141,7 +133,7 @@ Node* Tree::buildTree(std::string term) {
if ( precedence(elementToken) > precedence(lastNode->token()) ) {
operators.push(
- this->addNode<OperatorNode>(nullptr, elementToken)
+ this->addNode<OperatorNode>(elementToken)
);
} else {
Node*const currOperator = popNode(operators);
@@ -154,14 +146,15 @@ Node* Tree::buildTree(std::string term) {
}
}
} else {
- std::vector<std::string> subElements(lexer(element));
+ const std::vector<std::string> subElements{
+ lexer(element)
+ };
if ( subElements.size() == 1 ) {
switch ( determineToken(subElements[0][0]) ) {
case TokenType::VALUE_NUMBER:
case TokenType::OPERATOR_MINUS: {
operands.push(this->addNode<OperandNode>(
- nullptr,
doubleToString(subElements[0])
));
@@ -169,7 +162,6 @@ Node* Tree::buildTree(std::string term) {
}
case TokenType::VALUE_IDENTIFIER: {
operands.push(this->addNode<ConstantNode>(
- nullptr,
subElements[0],
this->constants_
));
diff --git a/src/tree.h b/src/tree.h
index 186f162..e2eb7a6 100644
--- a/src/tree.h
+++ b/src/tree.h
@@ -18,9 +18,10 @@ class Tree {
std::string print() const;
private:
- template <class NType, typename... Args>
- Node* addNode(Node**, Args&&... args);
- Node* buildTree(std::string);
+ template <class NodeType, typename... Args>
+ Node* addNode(Args&&... args);
+
+ Node* buildTree(const std::string&);
const std::string term_;
const ConstantMap* constants_;