aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerländer2013-01-05 22:30:35 +0100
committerAdrian Kummerländer2013-01-05 22:30:35 +0100
commitcde848ce1eb995170723f6f070b9fcba0dfdb880 (patch)
treeb28b436619ade0f9b3ff7603cc987d7b5a621ff3
parente3081360c65eb4994e7e8042898cec72de0d560b (diff)
downloadSimpleParser-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
-rw-r--r--Makefile12
-rw-r--r--main.cc (renamed from main.cpp)2
-rw-r--r--src/nodes.cc77
-rw-r--r--src/nodes.h53
-rw-r--r--src/parser.cc (renamed from src/parser.cpp)1
-rw-r--r--src/parser.h19
-rw-r--r--src/tree.cc (renamed from src/tree.cpp)62
-rw-r--r--src/tree.h61
-rw-r--r--test.cc (renamed from test.cpp)0
9 files changed, 149 insertions, 138 deletions
diff --git a/Makefile b/Makefile
index dcfeee7..6db0c9b 100644
--- a/Makefile
+++ b/Makefile
@@ -1,13 +1,15 @@
-LIB_FILES = src/tree.cpp src/parser.cpp
-PROG_FILES = main.cpp
-TEST_FILES = test.cpp
+LIB_FILES = src/nodes.cc src/tree.cc src/parser.cc
+PROG_FILES = main.cc
+TEST_FILES = test.cc
+
+FLAGS = -std=c++11 -W -Wall -Wextra -pedantic
all: parser test
parser: $(PROG_FILES) $(LIB_FILES)
- g++ -std=c++11 -g -o bin/parser $(PROG_FILES) $(LIB_FILES)
+ g++ -g -o bin/parser $(FLAGS) $(PROG_FILES) $(LIB_FILES)
test: $(LIB_FILES) $(TEST_FILES)
- g++ -std=c++11 -O3 -o bin/test -lgtest $(LIB_FILES) $(TEST_FILES)
+ g++ -O3 -o bin/test -lgtest $(FLAGS) $(LIB_FILES) $(TEST_FILES)
./bin/test
diff --git a/main.cpp b/main.cc
index 0342e28..c03cce2 100644
--- a/main.cpp
+++ b/main.cc
@@ -3,7 +3,7 @@
#include "src/parser.h"
-int main(int argc, char *argv[])
+int main()
{
std::string inputTerm;
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;
}
diff --git a/src/tree.h b/src/tree.h
index 4177098..da611c7 100644
--- a/src/tree.h
+++ b/src/tree.h
@@ -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_
diff --git a/test.cpp b/test.cc
index a752ad3..a752ad3 100644
--- a/test.cpp
+++ b/test.cc