| | |
| | | #ifndef DOT_H |
| | | #define DOT_H |
| | | #include <fstream> |
| | | #include <iostream> |
| | | #include <string> |
| | | #include "../ast.h" |
| | | #include "../gens.h" |
| | | |
| | | class dot_gen: public ast_visitor { |
| | | std::ofstream out; |
| | | int node_cnt; |
| | | public: |
| | | dot_gen(std::string fname) { |
| | | out.open(fname); |
| | | } |
| | | ~dot_gen() { |
| | | out.close(); |
| | | } |
| | | |
| | | std::string *new_node() { |
| | | std::string *o = new std::string(); |
| | | *o = "Node"+std::to_string(node_cnt++); |
| | | return o; |
| | | } |
| | | |
| | | void* visit_Document(Document *d) { |
| | | out << "digraph ast {" << std::endl; |
| | | node_cnt = 0; |
| | | std::string *doc_id = new_node(); |
| | | out << *doc_id << " [label=\"Document\"];" << std::endl; |
| | | std::vector<Block*> *b = d->get_blocks(); |
| | | for (std::vector<Block*>::size_type i = 0; i < b->size(); i++) { |
| | | std::string* ret_id = (std::string *)(*b)[i]->visit(this); |
| | | out << *doc_id << "->" << *ret_id << ";" << std::endl; |
| | | delete ret_id; |
| | | } |
| | | out << "}" << std::endl; |
| | | delete doc_id; |
| | | return NULL; |
| | | } |
| | | |
| | | void* visit_Block_Table(Block_Table *bt) { |
| | | std::string *b_id = new_node(); |
| | | out << *b_id << " [label=\"Block Table\"];" << std::endl; |
| | | return b_id; |
| | | } |
| | | |
| | | void* visit_Block_Code(Block_Code *bc) { |
| | | std::string *c_id = new_node(); |
| | | out << *c_id << " [label=\"Block Code(" << *(bc->get_lang()) << ")];" << std::endl; |
| | | return c_id; |
| | | } |
| | | |
| | | void* visit_Block_Line(Block_Line *bl) { |
| | | std::string *bl_id = new_node(); |
| | | std::vector<Line*> *l = bl->get_lines(); |
| | | out << *bl_id << " [label=\"Block Line\"];" << std::endl; |
| | | for (std::vector<Line*>::size_type i = 0; i < l->size(); i++) { |
| | | std::string *ret_id = (std::string*)(*l)[i]->visit(this); |
| | | out << *bl_id << "->" << *ret_id << ";" << std::endl; |
| | | delete ret_id; |
| | | } |
| | | return bl_id; |
| | | } |
| | | |
| | | void* visit_Heading(Heading *h) { |
| | | std::string *h_id = new_node(); |
| | | std::vector<Word*> *w = h->get_words(); |
| | | out << *h_id << " [label=\"Heading\"];" << std::endl; |
| | | for (std::vector<Word*>::size_type i = 0; i < w->size(); i++) { |
| | | std::string *ret_id = (std::string*)(*w)[i]->visit(this); |
| | | out << *h_id << "->" << *ret_id << ";" << std::endl; |
| | | delete ret_id; |
| | | } |
| | | return h_id; |
| | | } |
| | | |
| | | void* visit_List(List *l) { |
| | | std::string *l_id = new_node(); |
| | | std::vector<Word*> *w = l->get_words(); |
| | | out << *l_id << " [label=\"List(l: " << l->get_level() << ", n:" << l->get_number() << "\"];" << std::endl; |
| | | for (std::vector<Word*>::size_type i = 0; i < w->size(); i++) { |
| | | std::string *ret_id = (std::string*)(*w)[i]->visit(this); |
| | | out << *l_id << "->" << *ret_id << ";" << std::endl; |
| | | delete ret_id; |
| | | } |
| | | return l_id; |
| | | } |
| | | |
| | | void* visit_MathDisp(MathDisp *md) { |
| | | std::string *md_id = new_node(); |
| | | out << *md_id << " [label=\"MathDisp(" << *(md->get_expr()) << ")\"];" << std::endl; |
| | | return md_id; |
| | | } |
| | | |
| | | void* visit_Line_Word(Line_Word *lw) { |
| | | std::string *lw_id = new_node(); |
| | | std::vector<Word*> *w = lw->get_words(); |
| | | out << *lw_id << " [label=\"Line Word\"];" << std::endl; |
| | | for (std::vector<Word*>::size_type i = 0; i < w->size(); i++) { |
| | | std::string *ret_id = (std::string*)(*w)[i]->visit(this); |
| | | out << *lw_id << "->" << *ret_id << ";" << std::endl; |
| | | delete ret_id; |
| | | } |
| | | return lw_id; |
| | | } |
| | | |
| | | void* visit_MathInline(MathInline *mi) { |
| | | std::string *mi_id = new_node(); |
| | | out << *mi_id << " [label=\"Math Inline(" << *(mi->get_expr()) << ")];" << std::endl; |
| | | return mi_id; |
| | | } |
| | | |
| | | void* visit_Link(Link *l) { |
| | | std::string *l_id = new_node(); |
| | | out << *l_id << " [label=\"Link(" << l->get_link() << "," << l->get_text() << ")\"];" << std::endl; |
| | | return l_id; |
| | | } |
| | | |
| | | void* visit_Text(Text *t) { |
| | | std::string *t_id = new_node(); |
| | | out << *t_id << " [label=\"Text(" << *(t->get_text()) << ")\"];" << std::endl; |
| | | return t_id; |
| | | } |
| | | |
| | | void* visit_Format_Bold(Format_Bold *fb) { |
| | | std::string *fb_id = new_node(); |
| | | out << *fb_id << " [label=\"Format Bold\"];" << std::endl; |
| | | std::string *ret_id = (std::string*)fb->get_f()->visit(this); |
| | | out << *fb_id << "->" << *ret_id << ";" << std::endl; |
| | | delete ret_id; |
| | | return fb_id; |
| | | } |
| | | |
| | | void* visit_Format_Italic(Format_Italic *fi) { |
| | | std::string *fi_id = new_node(); |
| | | out << *fi_id << " [label=\"Format Italic\"];" << std::endl; |
| | | std::string *ret_id = (std::string*)fi->get_f()->visit(this); |
| | | out << *fi_id << "->" << *ret_id << ";" << std::endl; |
| | | delete ret_id; |
| | | return fi_id; |
| | | } |
| | | |
| | | void* visit_Format_Verbatim(Format_Verbatim *fv) { |
| | | std::string *fv_id = new_node(); |
| | | out << *fv_id << " [label=\"Format Verbatim\"];" << std::endl; |
| | | std::string *ret_id = (std::string*)fv->get_f()->visit(this); |
| | | out << *fv_id << "->" << *ret_id << ";" << std::endl; |
| | | delete ret_id; |
| | | return fv_id; |
| | | } |
| | | dot_gen(std::string fname); |
| | | ~dot_gen(); |
| | | std::string* new_node(); |
| | | void* visit_Document(Document *d); |
| | | void* visit_Block_Table(Block_Table *bt); |
| | | void* visit_Block_Code(Block_Code *bc); |
| | | void* visit_Block_Line(Block_Line *bl); |
| | | void* visit_Heading(Heading *h); |
| | | void* visit_List(List *l); |
| | | void* visit_MathDisp(MathDisp *md); |
| | | void* visit_Line_Word(Line_Word *lw); |
| | | void* visit_MathInline(MathInline *mi); |
| | | void* visit_Link(Link *l); |
| | | void* visit_Text(Text *t); |
| | | void* visit_Format_Bold(Format_Bold *fb); |
| | | void* visit_Format_Italic(Format_Italic *fi); |
| | | void* visit_Format_Verbatim(Format_Verbatim *fv); |
| | | }; |
| | | #endif |