From c4c28eff5ddca866efc0998612fe737f45544842 Mon Sep 17 00:00:00 2001
From: Joel Grunbaum <joelgrun@gmail.com>
Date: Tue, 20 Jul 2021 14:02:29 +0000
Subject: [PATCH] Updated ast to store strings rather than string pointers
---
ast.h | 112 +++++++++---------
gens/dot.h | 5
ast.cpp | 145 ++++++++++++++---------
Makefile | 15 +-
parsers/Makefile | 4
gens.h | 24 ++++
gens/Makefile | 2
gens/dot.cpp | 39 +++---
8 files changed, 199 insertions(+), 147 deletions(-)
diff --git a/Makefile b/Makefile
index 84ac690..28b96ea 100644
--- a/Makefile
+++ b/Makefile
@@ -10,24 +10,25 @@
OBJS-top=$(patsubst %,$(BUILD_DIR)/%,$(_OBJS-top))
-.PHONY: directories parser gen clean
+.PHONY: parser gen clean default all
-default: docconv directories
+default: docconv $(BUILD_DIR)
+all: default
docconv: parser gen $(OBJS-top)
$(CXX) $(CXXFLAGS) -o $@ $(BUILD_DIR)/*.o
$(BUILD_DIR)/%.o: %.cpp
- $(CXX) -c $(CXXFLAGS) -o $@ $<
+ $(CXX) -c $(CXXFLAGS) -o $@ $^
-parser: directories
+parser: $(BUILD_DIR)
$(MAKE) -C parsers
-gen: directories
+gen: $(BUILD_DIR)
$(MAKE) -C gens
-directories:
- mkdir -p $(BUILD_DIR)
+$(BUILD_DIR):
+ mkdir -p $@
clean:
rm -rf $(BUILD_DIR)
diff --git a/ast.cpp b/ast.cpp
index 64fa1ad..57bd9ae 100644
--- a/ast.cpp
+++ b/ast.cpp
@@ -2,33 +2,29 @@
#include <string>
#include "ast.h"
-MathInline::MathInline(std::string *e) {
+MathInline::MathInline(std::string e) {
expr = e;
}
-MathInline::~MathInline() {
- delete expr;
-}
-std::string* MathInline::get_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::Link(std::string l, std::string t) {
link = l;
text = t;
}
-Link::~Link() {
- delete link;
- delete text;
-}
-std::string* Link::get_link() {
+
+std::string Link::get_link() {
return link;
}
-std::string* Link::get_text() {
+
+std::string Link::get_text() {
return text;
}
+
void* Link::visit(ast_visitor *v) {
return v->visit_Link(this);
}
@@ -36,9 +32,11 @@
Text::Text(std::string tt) {
t=tt;
}
-std::string* Text::get_text() {
- return &t;
+
+std::string Text::get_text() {
+ return t;
}
+
void* Text::visit(ast_visitor *v) {
return v->visit_Text(this);
}
@@ -46,12 +44,15 @@
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);
}
@@ -59,12 +60,15 @@
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);
}
@@ -72,69 +76,74 @@
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) {
+Heading::Heading(int l, Line_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();
+ delete words;
}
+
int Heading::get_level() {
return level;
}
-std::vector<Word*>* Heading::get_words() {
- return &words;
+
+Line_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) {
+List::List(int l, int n, Line_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();
+ delete words;
}
+
int List::get_level() {
return level;
}
+
int List::get_number() {
return number;
}
-std::vector<Word*>* List::get_words() {
- return &words;
+
+Line_Word* List::get_words() {
+ return words;
}
+
void* List::visit(ast_visitor *v) {
return v->visit_List(this);
}
-MathDisp::MathDisp(std::string *e) {
+MathDisp::MathDisp(std::string e) {
expr = e;
}
-MathDisp::~MathDisp() {
- delete expr;
-}
-std::string* MathDisp::get_expr() {
+
+std::string MathDisp::get_expr() {
return expr;
}
+
void* MathDisp::visit(ast_visitor *v) {
return v->visit_MathDisp(this);
}
@@ -145,55 +154,70 @@
}
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];
+ for (std::vector<std::vector<Line_Word*>*>::size_type i = 0; i < table.size(); i++) {
+ std::vector<Line_Word*> *t = table[i];
+ for (std::vector<Line_Word*>::size_type j = 0; j < t->size(); j++) {
+ delete (*t)[j];
}
- table[i].clear();
+ table[i]->clear();
}
table.clear();
}
-std::vector<std::vector<Line_Word*>> Block_Table::get_table() {
- return table;
+
+std::vector<std::vector<Line_Word*>*>* Block_Table::get_table() {
+ return &table;
}
-int Block_Table::add_row(std::vector<Line_Word*> row) {
+
+int Block_Table::add_row(std::vector<Line_Word*>* row) {
table.push_back(row);
return 0;
}
+
+std::vector<Line_Word*>* Block_Table::get_row(std::vector<Line_Word*>::size_type i) {
+ if (i < table.size()) {
+ return table[i];
+ } else {
+ return NULL;
+ }
+}
+
void* Block_Table::visit(ast_visitor *v) {
return v->visit_Block_Table(this);
}
-Block_Code::Block_Code(std::string *l) {
+Block_Code::Block_Code(std::string l) {
lang = l;
- c = new std::string();
+ c = "";
}
-Block_Code::~Block_Code() {
- delete c;
- delete lang;
-}
-int Block_Code::add_line(std::string *add) {
- *c = *c + *add + "\n";
+
+int Block_Code::add_line(std::string add) {
+ c = c + add + "\n";
return 0;
}
-std::string* Block_Code::get_lang() {
+
+std::string Block_Code::get_lang() {
return lang;
}
-std::string* Block_Code::get_code() {
+
+std::string Block_Code::get_code() {
return c;
}
+
void* Block_Code::visit(ast_visitor *v) {
return v->visit_Block_Code(this);
}
@@ -204,47 +228,54 @@
}
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) {
+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() {
+
+std::string Document::get_title() {
return title;
}
-std::string* Document::get_author() {
+
+std::string Document::get_author() {
return author;
}
-std::string* Document::get_date() {
+
+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 ccc5e74..41ef05e 100644
--- a/ast.h
+++ b/ast.h
@@ -33,21 +33,19 @@
class Word: public ast_node {};
class MathInline: public Word {
- std::string *expr;
+ std::string expr;
public:
- MathInline(std::string *e);
- ~MathInline();
- std::string* get_expr();
+ MathInline(std::string e);
+ std::string get_expr();
void* visit(ast_visitor *v);
};
class Link: public Word {
- std::string *link, *text;
+ std::string link, text;
public:
- Link(std::string *l, std::string *t);
- ~Link();
- std::string* get_link();
- std::string* get_text();
+ Link(std::string l, std::string t);
+ std::string get_link();
+ std::string get_text();
void* visit(ast_visitor *v);
};
@@ -57,7 +55,7 @@
std::string t;
public:
Text(std::string tt);
- std::string* get_text();
+ std::string get_text();
void* visit(ast_visitor *v);
};
@@ -89,38 +87,6 @@
};
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:
@@ -130,26 +96,58 @@
void* visit(ast_visitor *v);
};
+class Heading: public Line {
+ int level;
+ Line_Word *words;
+public:
+ Heading(int l, Line_Word *w);
+ ~Heading();
+ int get_level();
+ Line_Word* get_words();
+ void* visit(ast_visitor *v);
+};
+
+class List: public Line {
+ int level, number;
+ Line_Word *words;
+public:
+ List(int l, int n, Line_Word *w);
+ ~List();
+ int get_level();
+ int get_number();
+ Line_Word* get_words();
+ void* visit(ast_visitor *v);
+};
+
+class MathDisp: public Line {
+ std::string expr;
+public:
+ MathDisp(std::string e);
+ std::string get_expr();
+ void* visit(ast_visitor *v);
+};
+
+
class Block: public ast_node {};
class Block_Table: public Block {
- std::vector<std::vector<Line_Word*>> table;
+ 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);
+ std::vector<std::vector<Line_Word*>*>* get_table();
+ int add_row(std::vector<Line_Word*>* row);
+ std::vector<Line_Word*>* get_row(std::vector<Line_Word*>::size_type i);
void* visit(ast_visitor *v);
};
class Block_Code: public Block {
- std::string *c;
- std::string *lang;
+ 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();
+ Block_Code(std::string l);
+ int add_line(std::string add);
+ std::string get_lang();
+ std::string get_code();
void* visit(ast_visitor *v);
};
@@ -163,14 +161,14 @@
};
class Document: public ast_node {
- std::string *title, *author, *date;
+ std::string title, author, date;
std::vector<Block*> blocks;
public:
- Document(std::string *t, std::string *a, std::string *d);
+ Document(std::string t, std::string a, std::string d);
~Document();
- std::string* get_title();
- std::string* get_author();
- std::string* get_date();
+ 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);
diff --git a/gens.h b/gens.h
index f48784f..d69d229 100644
--- a/gens.h
+++ b/gens.h
@@ -1 +1,25 @@
+#ifndef GENS_H
+#define GENS_H
+#include "ast.h"
+
+class gen: public ast_visitor {
+public:
+ virtual void set_file(std::string file_name)=0;
+ virtual void* visit_Document(Document *d)=0;
+ virtual void* visit_Block_Table(Block_Table *bt)=0;
+ virtual void* visit_Block_Code(Block_Code *bc)=0;
+ virtual void* visit_Block_Line(Block_Line *bl)=0;
+ virtual void* visit_Heading(Heading *h)=0;
+ virtual void* visit_List(List *l)=0;
+ virtual void* visit_MathDisp(MathDisp *md)=0;
+ virtual void* visit_Line_Word(Line_Word *lw)=0;
+ virtual void* visit_Link(Link *l)=0;
+ virtual void* visit_MathInline(MathInline *mi)=0;
+ virtual void* visit_Format_Bold(Format_Bold *fb)=0;
+ virtual void* visit_Format_Italic(Format_Italic *fi)=0;
+ virtual void* visit_Format_Verbatim(Format_Verbatim *fv)=0;
+ virtual void* visit_Text(Text *t)=0;
+};
+
#include "gens/dot.h"
+#endif
diff --git a/gens/Makefile b/gens/Makefile
index 1112400..d901d07 100644
--- a/gens/Makefile
+++ b/gens/Makefile
@@ -5,4 +5,4 @@
default: $(OBJS-gen)
$(BUILD_DIR)/%.o: %.cpp
- $(CXX) -c $(CXXFLAGS) -o $@ $<
+ $(CXX) -c $(CXXFLAGS) -o $@ $^
diff --git a/gens/dot.cpp b/gens/dot.cpp
index 9f987b7..e7fe3ef 100644
--- a/gens/dot.cpp
+++ b/gens/dot.cpp
@@ -3,15 +3,16 @@
#include <string>
#include "../ast.h"
#include "../gens.h"
-
-dot_gen::dot_gen(std::string fname) {
- out.open(fname);
-}
+#include "dot.h"
dot_gen::~dot_gen() {
out.close();
}
+void dot_gen::set_file(std::string file_name) {
+ out.open(file_name);
+}
+
std::string* dot_gen::new_node() {
std::string *o = new std::string();
*o = "Node"+std::to_string(node_cnt++);
@@ -42,7 +43,7 @@
void* dot_gen::visit_Block_Code(Block_Code *bc) {
std::string *c_id = new_node();
- out << *c_id << " [label=\"Block Code(" << *(bc->get_lang()) << ")];" << std::endl;
+ out << *c_id << " [label=\"Block Code(" << bc->get_lang() << ")\"];" << std::endl;
return c_id;
}
@@ -60,31 +61,27 @@
void* dot_gen::visit_Heading(Heading *h) {
std::string *h_id = new_node();
- std::vector<Word*> *w = h->get_words();
+ Line_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;
- }
+ std::string *ret_id = (std::string*)w->visit(this);
+ out << *h_id << "->" << *ret_id << ";" << std::endl;
+ delete ret_id;
return h_id;
}
void* dot_gen::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;
- }
+ Line_Word *w = l->get_words();
+ out << *l_id << " [label=\"List(l: " << l->get_level() << ", n:" << l->get_number() << ")\"];" << std::endl;
+ std::string *ret_id = (std::string*)w->visit(this);
+ out << *l_id << "->" << *ret_id << ";" << std::endl;
+ delete ret_id;
return l_id;
}
void* dot_gen::visit_MathDisp(MathDisp *md) {
std::string *md_id = new_node();
- out << *md_id << " [label=\"MathDisp(" << *(md->get_expr()) << ")\"];" << std::endl;
+ out << *md_id << " [label=\"MathDisp(" << md->get_expr() << ")\"];" << std::endl;
return md_id;
}
@@ -102,7 +99,7 @@
void* dot_gen::visit_MathInline(MathInline *mi) {
std::string *mi_id = new_node();
- out << *mi_id << " [label=\"Math Inline(" << *(mi->get_expr()) << ")];" << std::endl;
+ out << *mi_id << " [label=\"Math Inline(" << mi->get_expr() << ")\"];" << std::endl;
return mi_id;
}
@@ -114,7 +111,7 @@
void* dot_gen::visit_Text(Text *t) {
std::string *t_id = new_node();
- out << *t_id << " [label=\"Text(" << *(t->get_text()) << ")\"];" << std::endl;
+ out << *t_id << " [label=\"Text(" << t->get_text() << ")\"];" << std::endl;
return t_id;
}
diff --git a/gens/dot.h b/gens/dot.h
index 748215e..7c01fd5 100644
--- a/gens/dot.h
+++ b/gens/dot.h
@@ -2,13 +2,14 @@
#define DOT_H
#include <fstream>
#include "../ast.h"
+#include "../gens.h"
-class dot_gen: public ast_visitor {
+class dot_gen: public gen {
std::ofstream out;
int node_cnt;
public:
- dot_gen(std::string fname);
~dot_gen();
+ void set_file(std::string file_name);
std::string* new_node();
void* visit_Document(Document *d);
void* visit_Block_Table(Block_Table *bt);
diff --git a/parsers/Makefile b/parsers/Makefile
index b7186a3..b266792 100644
--- a/parsers/Makefile
+++ b/parsers/Makefile
@@ -1,8 +1,8 @@
-_OBJS-par=
+_OBJS-par=tester.o
export OBJS-par=$(patsubst %,$(BUILD_DIR)/%,$(_OBJS-par))
default: $(OBJS-par)
$(BUILD_DIR)/%.o: %.cpp
- $(CXX) -c $(CXXFLAGS) -o $@ $<
+ $(CXX) -c $(CXXFLAGS) -o $@ $^
--
Gitblit v1.10.0