aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2014-09-25 19:05:01 +0200
committerAdrian Kummerlaender2014-09-25 19:05:01 +0200
commit2fcef9b48bb97da82d5828776e9be50be1fdafbc (patch)
treefb06fca2695fdf9dadb582b14ea846285edc63ee
parent0e3cfcde619fb60e6e43bf0ffe715df6d5c22218 (diff)
downloadSimpleParser-2fcef9b48bb97da82d5828776e9be50be1fdafbc.tar
SimpleParser-2fcef9b48bb97da82d5828776e9be50be1fdafbc.tar.gz
SimpleParser-2fcef9b48bb97da82d5828776e9be50be1fdafbc.tar.bz2
SimpleParser-2fcef9b48bb97da82d5828776e9be50be1fdafbc.tar.lz
SimpleParser-2fcef9b48bb97da82d5828776e9be50be1fdafbc.tar.xz
SimpleParser-2fcef9b48bb97da82d5828776e9be50be1fdafbc.tar.zst
SimpleParser-2fcef9b48bb97da82d5828776e9be50be1fdafbc.zip
Increased Node constness and removed type member method
* marked "solve" and "print" implementations as const * converted member variables into constants where possible * "type" member method is not necessary and partially defeats the purpose of declaring virtual members
-rw-r--r--src/nodes.cc38
-rw-r--r--src/nodes.h43
2 files changed, 33 insertions, 48 deletions
diff --git a/src/nodes.cc b/src/nodes.cc
index f15a8c9..283b583 100644
--- a/src/nodes.cc
+++ b/src/nodes.cc
@@ -10,17 +10,13 @@
namespace SimpleParser {
OperandNode::OperandNode(double value):
- value_(value) { }
+ value_{value} { }
-double OperandNode::solve() {
+double OperandNode::solve() const {
return this->value_;
}
-NodeType OperandNode::type() {
- return NodeType::OPERAND;
-}
-
-std::string OperandNode::print() {
+std::string OperandNode::print() const {
std::stringstream convertStream;
convertStream.precision(std::numeric_limits<double>::digits10);
@@ -30,9 +26,9 @@ std::string OperandNode::print() {
}
OperatorNode::OperatorNode(TokenType token):
- operator_(token) { }
+ operator_{token} { }
-double OperatorNode::solve() {
+double OperatorNode::solve() const {
switch ( this->operator_ ) {
case TokenType::OPERATOR_MULTIPLY: {
return this->leftChild->solve() * this->rightChild->solve();
@@ -63,11 +59,7 @@ double OperatorNode::solve() {
}
}
-NodeType OperatorNode::type() {
- return NodeType::OPERATOR;
-}
-
-std::string OperatorNode::print() {
+std::string OperatorNode::print() const {
switch ( this->operator_ ) {
case TokenType::OPERATOR_PLUS: {
return std::string(1, '+');
@@ -90,21 +82,23 @@ std::string OperatorNode::print() {
}
}
-TokenType OperatorNode::token() {
+TokenType OperatorNode::token() const {
return this->operator_;
}
-ConstantNode::ConstantNode(std::string identifier,
- const ConstantMap* constants):
+ConstantNode::ConstantNode(
+ const std::string& identifier,
+ const ConstantMap* constants
+):
identifier_(identifier),
constants_(constants) { }
-double ConstantNode::solve() {
+double ConstantNode::solve() const {
if ( this->constants_ != nullptr ) {
try {
return this->constants_->at(this->identifier_);
}
- catch ( std::out_of_range &e ) {
+ catch ( std::out_of_range& ) {
throw identifier_exception();
}
} else {
@@ -112,11 +106,7 @@ double ConstantNode::solve() {
}
}
-NodeType ConstantNode::type() {
- return NodeType::CONSTANT;
-}
-
-std::string ConstantNode::print() {
+std::string ConstantNode::print() const {
return this->identifier_;
}
diff --git a/src/nodes.h b/src/nodes.h
index 2581a2d..9559722 100644
--- a/src/nodes.h
+++ b/src/nodes.h
@@ -9,61 +9,56 @@ namespace SimpleParser {
enum class TokenType;
typedef std::map<std::string, double> ConstantMap;
-enum class NodeType {
- OPERAND,
- OPERATOR,
- CONSTANT,
-};
-
class Node {
public:
virtual ~Node() {};
- virtual double solve() = 0;
- virtual NodeType type() = 0;
- virtual std::string print() = 0;
+ virtual double solve() const = 0;
+ virtual std::string print() const = 0;
Node* leftChild;
Node* rightChild;
+
};
class OperatorNode: public Node {
public:
explicit OperatorNode(TokenType);
- virtual double solve();
- virtual NodeType type();
- virtual std::string print();
+ TokenType token() const;
+
+ virtual double solve() const;
+ virtual std::string print() const;
- TokenType token();
private:
- TokenType operator_;
+ const TokenType operator_;
+
};
class OperandNode: public Node {
public:
explicit OperandNode(double);
- virtual double solve();
- virtual NodeType type();
- virtual std::string print();
+ virtual double solve() const;
+ virtual std::string print() const;
private:
- double value_;
+ const double value_;
+
};
class ConstantNode: public Node {
public:
- explicit ConstantNode(std::string, const ConstantMap*);
+ ConstantNode(const std::string&, const ConstantMap*);
- virtual double solve();
- virtual NodeType type();
- virtual std::string print();
+ virtual double solve() const;
+ virtual std::string print() const;
private:
- std::string identifier_;
- const ConstantMap* constants_;
+ const std::string identifier_;
+ const ConstantMap* const constants_;
+
};
}