aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/nodes.cc16
-rw-r--r--src/nodes.h17
-rw-r--r--src/tree.cc53
-rw-r--r--src/tree.h5
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 <cmath>
@@ -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 <string>
-#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<Node*>& 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 <typename NType, typename... Args>
+Node* Tree::addNode(Node** place, Args&&... args) {
this->node_collection_.emplace_back(
- new OperandNode(value)
+ new NType(std::forward<Args>(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<Node*>& stack) {
- if ( !stack.empty() ) {
- return stack.top();
- } else {
- throw operator_exception();
- }
-}
-
Node* Tree::buildTree(std::string term) {
std::stack<Node*> operandStack;
std::stack<Node*> operatorStack;
@@ -139,7 +116,7 @@ Node* Tree::buildTree(std::string term) {
if ( token > lastNode->getToken() ) {
operatorStack.push(
- this->addOperator(nullptr, token)
+ this->addNode<OperatorNode>(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<OperatorNode>(nullptr, token)
);
}
} else {
@@ -171,14 +148,14 @@ Node* Tree::buildTree(std::string term) {
convertStream >> value;
operandStack.push(
- this->addOperand(nullptr,value)
+ this->addNode<OperandNode>(nullptr, value)
);
break;
}
case TokenType::VALUE_IDENTIFIER: {
operandStack.push(
- this->addOperand(nullptr,tmpLexer[0])
+ this->addNode<ConstantNode>(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 <class NType, typename... Args>
+ Node* addNode(Node**, Args&&... args);
Node* buildTree(std::string);
std::vector<std::unique_ptr<Node>> node_collection_;