From 354ebe560494e538853642ebe1bc90b67fdab2f9 Mon Sep 17 00:00:00 2001
From: Joel Grunbaum <joelgrun@gmail.com>
Date: Wed, 14 Jul 2021 13:27:47 +0000
Subject: [PATCH] refactored functions into file
---
ast.h | 513 +++++++++++++--------------------------
ast.cpp | 250 +++++++++++++++++++
2 files changed, 422 insertions(+), 341 deletions(-)
diff --git a/ast.cpp b/ast.cpp
new file mode 100644
index 0000000..64fa1ad
--- /dev/null
+++ b/ast.cpp
@@ -0,0 +1,250 @@
+#include <vector>
+#include <string>
+#include "ast.h"
+
+MathInline::MathInline(std::string *e) {
+ expr = e;
+}
+MathInline::~MathInline() {
+ delete expr;
+}
+std::string* MathInline::get_expr() {
+ return expr;
+}
+void* MathInline::visit(ast_visitor *v) {
+ return v->visit_MathInline(this);
+}
+
+Link::Link(std::string *l, std::string *t) {
+ link = l;
+ text = t;
+}
+Link::~Link() {
+ delete link;
+ delete text;
+}
+std::string* Link::get_link() {
+ return link;
+}
+std::string* Link::get_text() {
+ return text;
+}
+void* Link::visit(ast_visitor *v) {
+ return v->visit_Link(this);
+}
+
+Text::Text(std::string tt) {
+ t=tt;
+}
+std::string* Text::get_text() {
+ return &t;
+}
+void* Text::visit(ast_visitor *v) {
+ return v->visit_Text(this);
+}
+
+Format_Bold::Format_Bold(Format* ff) {
+ f=ff;
+}
+Format_Bold::~Format_Bold() {
+ delete f;
+}
+Format* Format_Bold::get_f() {
+ return f;
+}
+void* Format_Bold::visit(ast_visitor *v) {
+ return v->visit_Format_Bold(this);
+}
+
+Format_Italic::Format_Italic(Format* ff) {
+ f=ff;
+}
+Format_Italic::~Format_Italic() {
+ delete f;
+}
+Format* Format_Italic::get_f() {
+ return f;
+}
+void* Format_Italic::visit(ast_visitor *v) {
+ return v->visit_Format_Italic(this);
+}
+
+Format_Verbatim::Format_Verbatim(Format* ff) {
+ f=ff;
+}
+Format_Verbatim::~Format_Verbatim() {
+ delete f;
+}
+Format* Format_Verbatim::get_f() {
+ return f;
+}
+void* Format_Verbatim::visit(ast_visitor *v) {
+ return v->visit_Format_Verbatim(this);
+}
+
+Heading::Heading(int l, std::vector<Word*> w) {
+ level = l;
+ words = w;
+}
+Heading::~Heading() {
+ for (std::vector<Word*>::size_type i = 0; i < words.size(); i++) {
+ delete words[i];
+ }
+ words.clear();
+}
+int Heading::get_level() {
+ return level;
+}
+std::vector<Word*>* Heading::get_words() {
+ return &words;
+}
+void* Heading::visit(ast_visitor *v) {
+ return v->visit_Heading(this);
+}
+
+List::List(int l, int n, std::vector<Word*> w) {
+ level = l;
+ number = n;
+ words = w;
+}
+List::~List() {
+ for (std::vector<Word*>::size_type i = 0; i < words.size(); i++) {
+ delete words[i];
+ }
+ words.clear();
+}
+int List::get_level() {
+ return level;
+}
+int List::get_number() {
+ return number;
+}
+std::vector<Word*>* List::get_words() {
+ return &words;
+}
+void* List::visit(ast_visitor *v) {
+ return v->visit_List(this);
+}
+
+MathDisp::MathDisp(std::string *e) {
+ expr = e;
+}
+MathDisp::~MathDisp() {
+ delete expr;
+}
+std::string* MathDisp::get_expr() {
+ return expr;
+}
+void* MathDisp::visit(ast_visitor *v) {
+ return v->visit_MathDisp(this);
+}
+
+Line_Word::~Line_Word() {
+ for (std::vector<Word*>::size_type i = 0; i < words.size(); i++) {
+ delete words[i];
+ }
+ words.clear();
+}
+int Line_Word::add_word(Word *w) {
+ words.push_back(w);
+ return 0;
+}
+std::vector<Word*>* Line_Word::get_words() {
+ return &words;
+}
+void* Line_Word::visit(ast_visitor *v) {
+ return v->visit_Line_Word(this);
+}
+
+Block_Table::~Block_Table() {
+ for (std::vector<std::vector<Line_Word*>>::size_type i = 0; i < table.size(); i++) {
+ for (std::vector<Line_Word*>::size_type j = 0; j < table[i].size(); j++) {
+ delete table[i][j];
+ }
+ table[i].clear();
+ }
+ table.clear();
+}
+std::vector<std::vector<Line_Word*>> Block_Table::get_table() {
+ return table;
+}
+int Block_Table::add_row(std::vector<Line_Word*> row) {
+ table.push_back(row);
+ return 0;
+}
+void* Block_Table::visit(ast_visitor *v) {
+ return v->visit_Block_Table(this);
+}
+
+Block_Code::Block_Code(std::string *l) {
+ lang = l;
+ c = new std::string();
+}
+Block_Code::~Block_Code() {
+ delete c;
+ delete lang;
+}
+int Block_Code::add_line(std::string *add) {
+ *c = *c + *add + "\n";
+ return 0;
+}
+std::string* Block_Code::get_lang() {
+ return lang;
+}
+std::string* Block_Code::get_code() {
+ return c;
+}
+void* Block_Code::visit(ast_visitor *v) {
+ return v->visit_Block_Code(this);
+}
+
+Block_Line::~Block_Line() {
+ for (std::vector<Line*>::size_type i = 0; i < lines.size(); i++) {
+ delete lines[i];
+ }
+ lines.clear();
+}
+std::vector<Line*>* Block_Line::get_lines() {
+ return &lines;
+}
+int Block_Line::add_line(Line* l) {
+ lines.push_back(l);
+ return 0;
+}
+void* Block_Line::visit(ast_visitor *v) {
+ return v->visit_Block_Line(this);
+}
+
+Document::Document(std::string *t, std::string *a, std::string *d) {
+ title = t;
+ author = a;
+ date = d;
+}
+Document::~Document() {
+ for (std::vector<Block*>::size_type i = 0; i < blocks.size(); i++) {
+ delete blocks[i];
+ }
+ blocks.clear();
+ delete title;
+ delete author;
+ delete date;
+}
+std::string* Document::get_title() {
+ return title;
+}
+std::string* Document::get_author() {
+ return author;
+}
+std::string* Document::get_date() {
+ return date;
+}
+std::vector<Block*>* Document::get_blocks() {
+ return &blocks;
+}
+int Document::add_block(Block *b) {
+ blocks.push_back(b);
+ return 0;
+}
+void* Document::visit(ast_visitor *v) {
+ return v->visit_Document(this);
+}
diff --git a/ast.h b/ast.h
index 613b5c4..ccc5e74 100644
--- a/ast.h
+++ b/ast.h
@@ -3,26 +3,178 @@
#include <vector>
#include <string>
-class ast_node;
-//class ast_visitor;
-class Document;
-class Block;
-class Block_Table;
-class Block_Code;
-class Block_Line;
-class Line;
-class Word;
-class Heading;
-class List;
-class MathDisp;
-class Line_Word;
-class Link;
-class MathInline;
-class Format;
-class Format_Bold;
-class Format_Italic;
-class Format_Verbatim;
-class Text;
+//class ast_node;
+class ast_visitor;
+//class Document;
+// class Block;
+// class Block_Table;
+// class Block_Code;
+// class Block_Line;
+// class Line;
+// class Word;
+// class Heading;
+// class List;
+// class MathDisp;
+// class Line_Word;
+// class Link;
+// class MathInline;
+// class Format;
+// class Format_Bold;
+// class Format_Italic;
+// class Format_Verbatim;
+// class Text;
+
+class ast_node {
+public:
+ virtual ~ast_node()=default;
+ virtual void* visit(ast_visitor *v)=0;
+};
+
+class Word: public ast_node {};
+
+class MathInline: public Word {
+ std::string *expr;
+public:
+ MathInline(std::string *e);
+ ~MathInline();
+ std::string* get_expr();
+ void* visit(ast_visitor *v);
+};
+
+class Link: public Word {
+ std::string *link, *text;
+public:
+ Link(std::string *l, std::string *t);
+ ~Link();
+ std::string* get_link();
+ std::string* get_text();
+ void* visit(ast_visitor *v);
+};
+
+class Format: public Word {};
+
+class Text: public Format {
+ std::string t;
+public:
+ Text(std::string tt);
+ std::string* get_text();
+ void* visit(ast_visitor *v);
+};
+
+class Format_Bold: public Format {
+ Format *f;
+public:
+ Format_Bold(Format* ff);
+ ~Format_Bold();
+ Format* get_f();
+ void* visit(ast_visitor *v);
+};
+
+class Format_Italic: public Format {
+ Format *f;
+public:
+ Format_Italic(Format* ff);
+ ~Format_Italic();
+ Format* get_f();
+ void* visit(ast_visitor *v);
+};
+
+class Format_Verbatim: public Format {
+ Format *f;
+public:
+ Format_Verbatim(Format* ff);
+ ~Format_Verbatim();
+ Format* get_f();
+ void* visit(ast_visitor *v);
+};
+class Line: public ast_node {};
+
+class Heading: public Line {
+ int level;
+ std::vector<Word*> words;
+public:
+ Heading(int l, std::vector<Word*> w);
+ ~Heading();
+ int get_level();
+ std::vector<Word*>* get_words();
+ void* visit(ast_visitor *v);
+};
+
+class List: public Line {
+ int level, number;
+ std::vector<Word*> words;
+public:
+ List(int l, int n, std::vector<Word*> w);
+ ~List();
+ int get_level();
+ int get_number();
+ std::vector<Word*>* get_words();
+ void* visit(ast_visitor *v);
+};
+
+class MathDisp: public Line {
+ std::string *expr;
+public:
+ MathDisp(std::string *e);
+ ~MathDisp();
+ std::string* get_expr();
+ void* visit(ast_visitor *v);
+};
+
+class Line_Word: public Line {
+ std::vector<Word*> words;
+public:
+ ~Line_Word();
+ int add_word(Word *w);
+ std::vector<Word*>* get_words();
+ void* visit(ast_visitor *v);
+};
+
+class Block: public ast_node {};
+
+class Block_Table: public Block {
+ std::vector<std::vector<Line_Word*>> table;
+public:
+ ~Block_Table();
+ std::vector<std::vector<Line_Word*>> get_table();
+ int add_row(std::vector<Line_Word*> row);
+ void* visit(ast_visitor *v);
+};
+
+class Block_Code: public Block {
+ std::string *c;
+ std::string *lang;
+public:
+ Block_Code(std::string *l);
+ ~Block_Code();
+ int add_line(std::string *add);
+ std::string* get_lang();
+ std::string* get_code();
+ void* visit(ast_visitor *v);
+};
+
+class Block_Line: public Block {
+ std::vector<Line*> lines;
+public:
+ ~Block_Line();
+ std::vector<Line*>* get_lines();
+ int add_line(Line* l);
+ void* visit(ast_visitor *v);
+};
+
+class Document: public ast_node {
+ std::string *title, *author, *date;
+ std::vector<Block*> blocks;
+public:
+ Document(std::string *t, std::string *a, std::string *d);
+ ~Document();
+ std::string* get_title();
+ std::string* get_author();
+ std::string* get_date();
+ std::vector<Block*>* get_blocks();
+ int add_block(Block *b);
+ void* visit(ast_visitor *v);
+};
class ast_visitor {
public:
@@ -41,325 +193,4 @@
virtual void* visit_Format_Verbatim(Format_Verbatim *fv)=0;
virtual void* visit_Text(Text *t)=0;
};
-
-class ast_node {
-public:
- virtual ~ast_node()=default;
- virtual void* visit(ast_visitor *v)=0;
-};
-
-class Word: public ast_node {};
-
-class MathInline: public Word {
- std::string *expr;
-public:
- MathInline(std::string *e) {
- expr = e;
- }
- ~MathInline() {
- delete expr;
- }
- std::string *get_expr() {
- return expr;
- }
- void* visit(ast_visitor *v) {
- return v->visit_MathInline(this);
- }
-};
-
-class Link: public Word {
- std::string *link, *text;
-public:
- Link(std::string *l, std::string *t) {
- link = l;
- text = t;
- }
- ~Link() {
- delete link;
- delete text;
- }
- std::string *get_link() {
- return link;
- }
- std::string *get_text() {
- return text;
- }
- void* visit(ast_visitor *v) {
- return v->visit_Link(this);
- }
-};
-
-class Format: public Word {};
-
-class Text: public Format {
- std::string t;
-public:
- Text(std::string tt) {
- t=tt;
- }
- std::string *get_text() {
- return &t;
- }
- void* visit(ast_visitor *v) {
- return v->visit_Text(this);
- }
-};
-
-class Format_Bold: public Format {
- Format *f;
-public:
- Format_Bold(Format* ff) {
- f=ff;
- }
- ~Format_Bold() {
- delete f;
- }
- Format *get_f() {
- return f;
- }
- void* visit(ast_visitor *v) {
- return v->visit_Format_Bold(this);
- }
-};
-
-class Format_Italic: public Format {
- Format *f;
-public:
- Format_Italic(Format* ff) {
- f=ff;
- }
- ~Format_Italic() {
- delete f;
- }
- Format *get_f() {
- return f;
- }
- void* visit(ast_visitor *v) {
- return v->visit_Format_Italic(this);
- }
-};
-
-class Format_Verbatim: public Format {
- Format *f;
-public:
- Format_Verbatim(Format* ff) {
- f=ff;
- }
- ~Format_Verbatim() {
- delete f;
- }
- Format *get_f() {
- return f;
- }
- void* visit(ast_visitor *v) {
- return v->visit_Format_Verbatim(this);
- }
-};
-class Line: public ast_node {};
-
-class Heading: public Line {
- int level;
- std::vector<Word*> words;
-public:
- Heading(int l, std::vector<Word*> w) {
- level = l;
- words = w;
- }
- ~Heading() {
- for (std::vector<Word*>::size_type i = 0; i < words.size(); i++) {
- delete words[i];
- }
- words.clear();
- }
- int get_level() {
- return level;
- }
- std::vector<Word*>* get_words() {
- return &words;
- }
- void* visit(ast_visitor *v) {
- return v->visit_Heading(this);
- }
-};
-
-class List: public Line {
- int level, number;
- std::vector<Word*> words;
-public:
- List(int l, int n, std::vector<Word*> w) {
- level = l;
- number = n;
- words = w;
- }
- ~List() {
- for (std::vector<Word*>::size_type i = 0; i < words.size(); i++) {
- delete words[i];
- }
- words.clear();
- }
- int get_level() {
- return level;
- }
- int get_number() {
- return number;
- }
- std::vector<Word*>* get_words() {
- return &words;
- }
- void* visit(ast_visitor *v) {
- return v->visit_List(this);
- }
-};
-
-class MathDisp: public Line {
- std::string *expr;
-public:
- MathDisp(std::string *e) {
- expr = e;
- }
- ~MathDisp() {
- delete expr;
- }
- std::string *get_expr() {
- return expr;
- }
- void* visit(ast_visitor *v) {
- return v->visit_MathDisp(this);
- }
-};
-
-class Line_Word: public Line {
- std::vector<Word*> words;
-public:
- ~Line_Word() {
- for (std::vector<Word*>::size_type i = 0; i < words.size(); i++) {
- delete words[i];
- }
- words.clear();
- }
- int add_word(Word *w) {
- words.push_back(w);
- return 0;
- }
- std::vector<Word*> *get_words() {
- return &words;
- }
- void* visit(ast_visitor *v) {
- return v->visit_Line_Word(this);
- }
-};
-
-class Block: public ast_node {};
-
-class Block_Table: public Block {
- std::vector<std::vector<Line_Word*>> table;
-public:
- ~Block_Table() {
- for (std::vector<std::vector<Line_Word*>>::size_type i = 0; i < table.size(); i++) {
- for (std::vector<Line_Word*>::size_type j = 0; j < table[i].size(); j++) {
- delete table[i][j];
- }
- table[i].clear();
- }
- table.clear();
- }
- std::vector<std::vector<Line_Word*>> get_table() {
- return table;
- }
- int add_row(std::vector<Line_Word*> row) {
- table.push_back(row);
- return 0;
- }
- void* visit(ast_visitor *v) {
- return v->visit_Block_Table(this);
- }
-};
-
-class Block_Code: public Block {
- std::string *c;
- std::string *lang;
-public:
- Block_Code(std::string *l) {
- lang = l;
- c = new std::string();
- }
- ~Block_Code() {
- delete c;
- delete lang;
- }
- int add_line(std::string *add) {
- *c = *c + *add + "\n";
- return 0;
- }
- std::string *get_lang() {
- return lang;
- }
- std::string *get_code() {
- return c;
- }
- void* visit(ast_visitor *v) {
- return v->visit_Block_Code(this);
- }
-};
-
-class Block_Line: public Block {
- std::vector<Line*> lines;
-public:
- ~Block_Line() {
- for (std::vector<Line*>::size_type i = 0; i < lines.size(); i++) {
- delete lines[i];
- }
- lines.clear();
- }
- std::vector<Line*> *get_lines() {
- return &lines;
- }
- int add_line(Line* l) {
- lines.push_back(l);
- return 0;
- }
- void* visit(ast_visitor *v) {
- return v->visit_Block_Line(this);
- }
-};
-
-class Document: public ast_node {
- std::string *title, *author, *date;
- std::vector<Block*> blocks;
-public:
- Document(std::string *t, std::string *a, std::string *d) {
- title = t;
- author = a;
- date = d;
- }
- ~Document() {
- for (std::vector<Block*>::size_type i = 0; i < blocks.size(); i++) {
- delete blocks[i];
- }
- blocks.clear();
- delete title;
- delete author;
- delete date;
- }
- std::string *get_title() {
- return title;
- }
- std::string *get_author() {
- return author;
- }
- std::string *get_date() {
- return date;
- }
- std::vector<Block*> *get_blocks() {
- return &blocks;
- }
- int add_block(Block *b) {
- blocks.push_back(b);
- return 0;
- }
- void* visit(ast_visitor *v) {
- return v->visit_Document(this);
- }
-
-};
#endif
--
Gitblit v1.10.0