aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdrian Kummerländer2013-10-19 21:41:19 +0200
committerAdrian Kummerländer2013-10-19 21:41:19 +0200
commit69ce370c9ebf92caea64ef1ba28a502affdad3d5 (patch)
tree35d84b2c3080622f990bfe75396966c861d75e92 /src
parentc292b29f3bb87cf11edf899c023cfb574c20ed6c (diff)
downloadSimpleParser-69ce370c9ebf92caea64ef1ba28a502affdad3d5.tar
SimpleParser-69ce370c9ebf92caea64ef1ba28a502affdad3d5.tar.gz
SimpleParser-69ce370c9ebf92caea64ef1ba28a502affdad3d5.tar.bz2
SimpleParser-69ce370c9ebf92caea64ef1ba28a502affdad3d5.tar.lz
SimpleParser-69ce370c9ebf92caea64ef1ba28a502affdad3d5.tar.xz
SimpleParser-69ce370c9ebf92caea64ef1ba28a502affdad3d5.tar.zst
SimpleParser-69ce370c9ebf92caea64ef1ba28a502affdad3d5.zip
Switched OperatorNode to TokenType
* OperatorNode class now uses TokenType for internal operator storage and logic selection * print-Method resolves the TokenType back into its character representation
Diffstat (limited to 'src')
-rw-r--r--src/nodes.cc68
-rw-r--r--src/nodes.h15
-rw-r--r--src/tree.cc12
-rw-r--r--src/tree.h2
-rw-r--r--src/utils.h1
5 files changed, 58 insertions, 40 deletions
diff --git a/src/nodes.cc b/src/nodes.cc
index fac74a0..bab7ff7 100644
--- a/src/nodes.cc
+++ b/src/nodes.cc
@@ -7,18 +7,15 @@
namespace SimpleParser {
-OperandNode::OperandNode(double val):
- value_(val) { }
-
-OperandNode::OperandNode(std::string identifier):
- value_(42) { }
+OperandNode::OperandNode(double value):
+ value_(value) { }
double OperandNode::solve() {
return this->value_;
}
NodeType OperandNode::getType() {
- return NodeType::OPERAND_NODE;
+ return NodeType::OPERAND;
}
std::string OperandNode::print() {
@@ -30,15 +27,26 @@ std::string OperandNode::print() {
return convertStream.str();
}
-OperatorNode::OperatorNode(char op):
- function_(op) { }
+OperatorNode::OperatorNode(TokenType token):
+ operator_(token) { }
double OperatorNode::solve() {
- switch ( this->function_ ) {
- case '*': {
+ switch ( this->operator_ ) {
+ case TokenType::OPERATOR_MULTIPLY: {
return this->leftChild->solve() * this->rightChild->solve();
}
- case '/': {
+ case TokenType::OPERATOR_PLUS: {
+ return this->leftChild->solve() + this->rightChild->solve();
+ }
+ case TokenType::OPERATOR_MINUS: {
+ return this->leftChild->solve() - this->rightChild->solve();
+ }
+ case TokenType::OPERATOR_POWER: {
+ return std::pow(
+ this->leftChild->solve(), this->rightChild->solve()
+ );
+ }
+ case TokenType::OPERATOR_DIVIDE: {
double rightChild = this->rightChild->solve();
if ( rightChild != 0 ) {
@@ -48,17 +56,6 @@ double OperatorNode::solve() {
throw divide_exception();
}
}
- case '+': {
- return this->leftChild->solve() + this->rightChild->solve();
- }
- case '-': {
- return this->leftChild->solve() - this->rightChild->solve();
- }
- case '^': {
- return std::pow(
- this->leftChild->solve(), this->rightChild->solve()
- );
- }
default: {
throw operator_exception();
}
@@ -66,15 +63,34 @@ double OperatorNode::solve() {
}
NodeType OperatorNode::getType() {
- return NodeType::OPERATOR_NODE;
+ return NodeType::OPERATOR;
}
std::string OperatorNode::print() {
- return std::string(1, this->function_);
+ switch ( this->operator_ ) {
+ case TokenType::OPERATOR_PLUS: {
+ return std::string(1, '+');
+ }
+ case TokenType::OPERATOR_MINUS: {
+ return std::string(1, '-');
+ }
+ case TokenType::OPERATOR_MULTIPLY: {
+ return std::string(1, '*');
+ }
+ case TokenType::OPERATOR_DIVIDE: {
+ return std::string(1, '/');
+ }
+ case TokenType::OPERATOR_POWER: {
+ return std::string(1, '^');
+ }
+ default: {
+ throw operator_exception();
+ }
+ }
}
-char OperatorNode::getFunction() {
- return this->function_;
+TokenType OperatorNode::getToken() {
+ return this->operator_;
}
}
diff --git a/src/nodes.h b/src/nodes.h
index a59f4a5..e90669c 100644
--- a/src/nodes.h
+++ b/src/nodes.h
@@ -3,11 +3,13 @@
#include <string>
+#include "utils.h"
+
namespace SimpleParser {
-enum NodeType {
- OPERAND_NODE,
- OPERATOR_NODE,
+enum class NodeType {
+ OPERAND,
+ OPERATOR,
};
class Node {
@@ -24,22 +26,21 @@ class Node {
class OperatorNode: public Node {
public:
- explicit OperatorNode(char);
+ explicit OperatorNode(TokenType);
virtual double solve();
virtual NodeType getType();
virtual std::string print();
- char getFunction();
+ TokenType getToken();
private:
- char function_;
+ TokenType operator_;
};
class OperandNode: public Node {
public:
explicit OperandNode(double);
- explicit OperandNode(std::string);
virtual double solve();
virtual NodeType getType();
diff --git a/src/tree.cc b/src/tree.cc
index 2dfb786..e63b7b3 100644
--- a/src/tree.cc
+++ b/src/tree.cc
@@ -41,7 +41,7 @@ std::string Tree::print() {
<< "\"];"
<< std::endl;
- if ( (*it)->getType() == NodeType::OPERATOR_NODE ) {
+ if ( (*it)->getType() == NodeType::OPERATOR ) {
for ( auto iter = this->node_collection_.begin();
iter != this->node_collection_.end();
++iter ) {
@@ -96,9 +96,9 @@ Node* Tree::addOperand(Node** place, std::string value) {
return this->node_collection_.back().get();
}
-Node* Tree::addOperator(Node** place, char oper) {
+Node* Tree::addOperator(Node** place, TokenType token) {
this->node_collection_.emplace_back(
- new OperatorNode(oper)
+ new OperatorNode(token)
);
if ( place != nullptr ) {
@@ -137,9 +137,9 @@ Node* Tree::buildTree(std::string term) {
static_cast<OperatorNode*>(topNodeFrom(operatorStack))
);
- if ( token > getTokenType(lastNode->getFunction()) ) {
+ if ( token > lastNode->getToken() ) {
operatorStack.push(
- this->addOperator(nullptr, currTerm[0])
+ this->addOperator(nullptr, token)
);
} else {
Node* currOperator = topNodeFrom(operatorStack);
@@ -157,7 +157,7 @@ Node* Tree::buildTree(std::string term) {
}
} else {
operatorStack.push(
- this->addOperator(nullptr, currTerm[0])
+ this->addOperator(nullptr, token)
);
}
} else {
diff --git a/src/tree.h b/src/tree.h
index f20f765..858d737 100644
--- a/src/tree.h
+++ b/src/tree.h
@@ -19,7 +19,7 @@ class Tree {
private:
Node* addOperand(Node**, double);
Node* addOperand(Node**, std::string);
- Node* addOperator(Node**, char);
+ Node* addOperator(Node**, TokenType);
Node* buildTree(std::string);
std::vector<std::unique_ptr<Node>> node_collection_;
diff --git a/src/utils.h b/src/utils.h
index 6e43605..9727d6a 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -17,6 +17,7 @@ enum class TokenType : int8_t {
PARENTHESES_OPEN = 90,
PARENTHESES_CLOSE = 91,
VALUE_NUMBER = -1,
+ VALUE_IDENTIFIER = -2,
};
TokenType getTokenType(char);