diff options
author | Adrian Kummerlaender | 2014-10-01 20:30:08 +0200 |
---|---|---|
committer | Adrian Kummerlaender | 2014-10-01 20:30:08 +0200 |
commit | 0840f434541b57fbd6d3c9d7b2a8b127cc680912 (patch) | |
tree | 330df31c2083a48c33be95a2bc7d40f8b5d25470 /src/nodes.cc | |
parent | 6498a968cd34e49e355cc0765ca2dafb842bc12b (diff) | |
download | SimpleParser-0840f434541b57fbd6d3c9d7b2a8b127cc680912.tar SimpleParser-0840f434541b57fbd6d3c9d7b2a8b127cc680912.tar.gz SimpleParser-0840f434541b57fbd6d3c9d7b2a8b127cc680912.tar.bz2 SimpleParser-0840f434541b57fbd6d3c9d7b2a8b127cc680912.tar.lz SimpleParser-0840f434541b57fbd6d3c9d7b2a8b127cc680912.tar.xz SimpleParser-0840f434541b57fbd6d3c9d7b2a8b127cc680912.tar.zst SimpleParser-0840f434541b57fbd6d3c9d7b2a8b127cc680912.zip |
Moved child pointers to OperatorNode class
* access was restricted by declaring them private and offering member methods such as "hasChildren"
* only OperatorNode instances have children, there is no reason for wasting space by keeping empty pointers around in all other instances
* this enabled the removal of all nullptr comparisons from tree construction
* changed "Tree::addNode" factory method template to return a pointer to the type of the constructed node instance instead of a plain node pointer
Diffstat (limited to 'src/nodes.cc')
-rw-r--r-- | src/nodes.cc | 36 |
1 files changed, 31 insertions, 5 deletions
diff --git a/src/nodes.cc b/src/nodes.cc index 256a609..dd003c4 100644 --- a/src/nodes.cc +++ b/src/nodes.cc @@ -4,11 +4,20 @@ #include "exceptions.h" #include <cmath> +#include <cassert> #include <sstream> #include <limits> namespace SimpleParser { +bool Node::hasChildren() const { + return false; +} + +bool Node::isParentOf(Node*const) const { + return false; +} + OperandNode::OperandNode(double value): value_{value} { } @@ -31,20 +40,20 @@ OperatorNode::OperatorNode(TokenType token): double OperatorNode::solve() const { switch ( this->operator_ ) { case TokenType::OPERATOR_PLUS: { - return this->leftChild->solve() + this->rightChild->solve(); + return this->left_->solve() + this->right_->solve(); } case TokenType::OPERATOR_MINUS: { - return this->leftChild->solve() - this->rightChild->solve(); + return this->left_->solve() - this->right_->solve(); } case TokenType::OPERATOR_MULTIPLY: { - return this->leftChild->solve() * this->rightChild->solve(); + return this->left_->solve() * this->right_->solve(); } case TokenType::OPERATOR_DIVIDE: { - return this->leftChild->solve() / this->rightChild->solve(); + return this->left_->solve() / this->right_->solve(); } case TokenType::OPERATOR_POWER: { return std::pow( - this->leftChild->solve(), this->rightChild->solve() + this->left_->solve(), this->right_->solve() ); } default: { @@ -76,10 +85,27 @@ std::string OperatorNode::print() const { } } +bool OperatorNode::hasChildren() const { + return this->right_ != nullptr && + this->left_ != nullptr; +} + +bool OperatorNode::isParentOf(Node*const node) const { + return this->right_ == node || + this->left_ == node; +} + TokenType OperatorNode::token() const { return this->operator_; } +void OperatorNode::setChildren(Node*const right, Node*const left) { + assert(right != nullptr && left != nullptr); + + this->right_ = right; + this->left_ = left; +} + ConstantNode::ConstantNode( const std::string& identifier, const ConstantMap* constants |