diff options
author | Adrian Kummerländer | 2013-01-05 23:29:33 +0100 |
---|---|---|
committer | Adrian Kummerländer | 2013-01-05 23:29:33 +0100 |
commit | fc747105077c7ebab15963c5e69e334357c675e7 (patch) | |
tree | 2609473ea930f907c74e5f0354cec24e1e41d948 /src | |
parent | 39038fddb2032c9944e549be8f1665e028cb68b8 (diff) | |
download | SimpleParser-fc747105077c7ebab15963c5e69e334357c675e7.tar SimpleParser-fc747105077c7ebab15963c5e69e334357c675e7.tar.gz SimpleParser-fc747105077c7ebab15963c5e69e334357c675e7.tar.bz2 SimpleParser-fc747105077c7ebab15963c5e69e334357c675e7.tar.lz SimpleParser-fc747105077c7ebab15963c5e69e334357c675e7.tar.xz SimpleParser-fc747105077c7ebab15963c5e69e334357c675e7.tar.zst SimpleParser-fc747105077c7ebab15963c5e69e334357c675e7.zip |
Made existing Graphviz tree generation available
to the libary user by providing a new plain "exportTree" function
Diffstat (limited to 'src')
-rw-r--r-- | src/parser.cc | 8 | ||||
-rw-r--r-- | src/parser.h | 3 | ||||
-rw-r--r-- | src/tree.cc | 30 |
3 files changed, 34 insertions, 7 deletions
diff --git a/src/parser.cc b/src/parser.cc index 5989027..53eb311 100644 --- a/src/parser.cc +++ b/src/parser.cc @@ -1,6 +1,7 @@ #include "parser.h" #include "exceptions.h" +#include <stack> #include <sstream> namespace SimpleParser { @@ -12,6 +13,13 @@ double calculate(std::string term) { return termTree.solve(); } +std::string exportTree(std::string term) { + Tree termTree; + termTree.setRoot(buildTree(&termTree, term)); + + return termTree.print(term); +} + namespace { int8_t getPriority(char tmp) { diff --git a/src/parser.h b/src/parser.h index 7e594ca..677717a 100644 --- a/src/parser.h +++ b/src/parser.h @@ -1,14 +1,15 @@ #ifndef PARSER_SRC_PARSER_H_ #define PARSER_SRC_PARSER_H_ +#include <string> #include <vector> -#include <stack> #include "tree.h" namespace SimpleParser { double calculate(std::string); +std::string exportTree(std::string); namespace { int8_t getPriority(char); diff --git a/src/tree.cc b/src/tree.cc index 3755423..0fa41ad 100644 --- a/src/tree.cc +++ b/src/tree.cc @@ -38,22 +38,40 @@ std::string Tree::print(std::string term) { std::stringstream out; out.precision(std::numeric_limits<double>::digits10); - out << "digraph \"" << term << "\"" << std::endl - << "{" << std::endl - << "node [shape = box];" << std::endl; + out << "digraph \"" + << term + << "\"" + << std::endl; + out << "{" << std::endl; + out << "node [shape = box];" << std::endl; int i = 0; for ( auto it = this->node_collection_.begin(); it != this->node_collection_.end(); ++it ) { - out << "node" << i << " [ label = \"" << (*it)->print() << "\"];" << std::endl; + out << "node" + << i + << " [ label = \"" + << (*it)->print() + << "\"];" + << std::endl; if ( (*it)->getType() == OPERATOR_NODE ) { for ( auto iter = this->node_collection_.begin(); iter != this->node_collection_.end(); ++iter ) { if ( (*iter).get() == (*it)->leftChild ) { - out << "\"node" << i << "\" -> \"node" << (iter - this->node_collection_.begin()) << "\";" << std::endl; + out << "\"node" + << i + << "\" -> \"node" + << (iter - this->node_collection_.begin()) + << "\";" + << std::endl; } if ( (*iter).get() == (*it)->rightChild ) { - out << "\"node" << i << "\" -> \"node" << (iter - this->node_collection_.begin()) << "\";" << std::endl; + out << "\"node" + << i + << "\" -> \"node" + << (iter - this->node_collection_.begin()) + << "\";" + << std::endl; } } } |