diff options
author | Adrian Kummerländer | 2013-01-05 22:30:35 +0100 |
---|---|---|
committer | Adrian Kummerländer | 2013-01-05 22:30:35 +0100 |
commit | cde848ce1eb995170723f6f070b9fcba0dfdb880 (patch) | |
tree | b28b436619ade0f9b3ff7603cc987d7b5a621ff3 /src | |
parent | e3081360c65eb4994e7e8042898cec72de0d560b (diff) | |
download | SimpleParser-cde848ce1eb995170723f6f070b9fcba0dfdb880.tar SimpleParser-cde848ce1eb995170723f6f070b9fcba0dfdb880.tar.gz SimpleParser-cde848ce1eb995170723f6f070b9fcba0dfdb880.tar.bz2 SimpleParser-cde848ce1eb995170723f6f070b9fcba0dfdb880.tar.lz SimpleParser-cde848ce1eb995170723f6f070b9fcba0dfdb880.tar.xz SimpleParser-cde848ce1eb995170723f6f070b9fcba0dfdb880.tar.zst SimpleParser-cde848ce1eb995170723f6f070b9fcba0dfdb880.zip |
Moved node classes into separate compilation unit; File extension change
Diffstat (limited to 'src')
-rw-r--r-- | src/nodes.cc | 77 | ||||
-rw-r--r-- | src/nodes.h | 53 | ||||
-rw-r--r-- | src/parser.cc (renamed from src/parser.cpp) | 1 | ||||
-rw-r--r-- | src/parser.h | 19 | ||||
-rw-r--r-- | src/tree.cc (renamed from src/tree.cpp) | 62 | ||||
-rw-r--r-- | src/tree.h | 61 |
6 files changed, 141 insertions, 132 deletions
diff --git a/src/nodes.cc b/src/nodes.cc new file mode 100644 index 0000000..70b9e50 --- /dev/null +++ b/src/nodes.cc @@ -0,0 +1,77 @@ +#include "nodes.h" +#include "exceptions.h" + +#include <cmath> +#include <sstream> +#include <limits> + +namespace SimpleParser { + +OperandNode::OperandNode(double val) { + this->value_ = val; +} + +double OperandNode::solve() { + return this->value_; +} + +NodeType OperandNode::getType() { + return OPERAND_NODE; +} + +std::string OperandNode::print() { + std::stringstream convertStream; + convertStream.precision(std::numeric_limits<double>::digits10); + + convertStream << this->value_; + + return convertStream.str(); +} + +OperatorNode::OperatorNode(char op) { + this->function_ = op; +} + +double OperatorNode::solve() { + switch ( this->function_ ) { + case '*': { + return this->leftChild->solve() * this->rightChild->solve(); + } + case '/': { + double rightChild = this->rightChild->solve(); + + if ( rightChild != 0 ) { + return this->leftChild->solve() / rightChild; + } + else { + 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(); + } + } +} + +NodeType OperatorNode::getType() { + return OPERATOR_NODE; +} + +std::string OperatorNode::print() { + return std::string(1, this->function_); +} + +char OperatorNode::getFunction() { + return this->function_; +} + +} diff --git a/src/nodes.h b/src/nodes.h new file mode 100644 index 0000000..3a99474 --- /dev/null +++ b/src/nodes.h @@ -0,0 +1,53 @@ +#ifndef PARSER_SRC_NODES_H_ +#define PARSER_SRC_NODES_H_ + +#include <string> + +namespace SimpleParser { + +enum NodeType { + OPERAND_NODE, + OPERATOR_NODE, +}; + +class Node { + public: + virtual ~Node() {}; + + virtual double solve() = 0; + virtual NodeType getType() = 0; + virtual std::string print() = 0; + + Node* leftChild; + Node* rightChild; +}; + +class OperatorNode: public Node { + public: + explicit OperatorNode(char); + + virtual double solve(); + virtual NodeType getType(); + virtual std::string print(); + + char getFunction(); + + private: + char function_; +}; + +class OperandNode: public Node { + public: + explicit OperandNode(double); + + virtual double solve(); + virtual NodeType getType(); + virtual std::string print(); + + private: + double value_; +}; + +} + +#endif // PARSER_SRC_NODES_H_ diff --git a/src/parser.cpp b/src/parser.cc index 3aaf7e2..0f05790 100644 --- a/src/parser.cpp +++ b/src/parser.cc @@ -1,4 +1,5 @@ #include "parser.h" +#include "exceptions.h" #include <sstream> diff --git a/src/parser.h b/src/parser.h index 12d745a..b943ebe 100644 --- a/src/parser.h +++ b/src/parser.h @@ -1,9 +1,8 @@ -#ifndef PARSER_PARSER_H_ -#define PARSER_PARSER_H_ +#ifndef PARSER_SRC_PARSER_H_ +#define PARSER_SRC_PARSER_H_ #include <vector> #include <stack> -#include <exception> #include "tree.h" @@ -19,18 +18,6 @@ class Parser { Node* buildTree(Tree*, std::string); }; -class parenthese_exception: public std::exception { - virtual const char* what() const throw() { - return "Invalid parenthesized expression - check your input term."; - } -}; - -class operator_exception: public std::exception { - virtual const char* what() const throw() { - return "Unexpected operator placement - check your input term."; - } -}; - } -#endif // PARSER_PARSER_H_ +#endif // PARSER_SRC_PARSER_H_ diff --git a/src/tree.cpp b/src/tree.cc index c8f0aac..675009e 100644 --- a/src/tree.cpp +++ b/src/tree.cc @@ -1,69 +1,11 @@ #include "tree.h" +#include "exceptions.h" +#include <sstream> #include <limits> namespace SimpleParser { -OperandNode::OperandNode(double val) { - this->value_ = val; -} - -double OperandNode::solve() { - return this->value_; -} - -NodeType OperandNode::getType() { - return OPERAND_NODE; -} - -std::string OperandNode::print() { - std::stringstream convertStream; - convertStream.precision(std::numeric_limits<double>::digits10); - - convertStream << this->value_; - - return convertStream.str(); -} - -OperatorNode::OperatorNode(char op) { - this->function_ = op; -} - -double OperatorNode::solve() { - switch ( this->function_ ) { - case '*': - return this->leftChild->solve() * this->rightChild->solve(); - case '/': { - double rightChild = this->rightChild->solve(); - - if ( rightChild != 0 ) { - return this->leftChild->solve() / rightChild; - } - else { - 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() ); - } -} - -NodeType OperatorNode::getType() { - return OPERATOR_NODE; -} - -std::string OperatorNode::print() { - return std::string(1, this->function_); -} - -char OperatorNode::getFunction() { - return this->function_; -} - void Tree::setRoot(Node* root) { this->root_node_ = root; } @@ -1,57 +1,13 @@ -#ifndef PARSER_NODE_H_ -#define PARSER_NODE_H_ +#ifndef PARSER_SRC_NODE_H_ +#define PARSER_SRC_NODE_H_ #include <vector> #include <string> -#include <sstream> #include <memory> -#include <cmath> -namespace SimpleParser { - -enum NodeType { - OPERAND_NODE, - OPERATOR_NODE, -}; - -class Node { - public: - virtual ~Node() {}; - - virtual double solve() = 0; - virtual NodeType getType() = 0; - virtual std::string print() = 0; - - Node* leftChild; - Node* rightChild; -}; - -class OperatorNode: public Node { - public: - explicit OperatorNode(char); +#include "nodes.h" - virtual double solve(); - virtual NodeType getType(); - virtual std::string print(); - - char getFunction(); - - private: - char function_; -}; - - -class OperandNode: public Node { - public: - explicit OperandNode(double); - - virtual double solve(); - virtual NodeType getType(); - virtual std::string print(); - - private: - double value_; -}; +namespace SimpleParser { class Tree { public: @@ -68,13 +24,6 @@ class Tree { std::vector<std::unique_ptr<Node>> node_collection_; }; -class divide_exception: public std::exception { - virtual const char* what() const throw() - { - return "A divison through zero had to be prevented by the parser - check your input term."; - } -}; - } -#endif // PARSER_NODE_H_ +#endif // PARSER_SRC_NODE_H_ |