-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.cpp
148 lines (119 loc) · 3.12 KB
/
ast.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <string>
#include <vector>
#include <stack>
#include "ast.h"
using namespace std;
extern vector<ast_node*>* content_children;
extern vector<ast_node*>* section_children;
extern vector<ast_node*>* subsection_children;
extern vector<ast_node*>* list_children;
extern vector<ast_node*>* content_children;
extern vector<ast_node*>* r_content_children;
extern vector<ast_node*>* rows_children;
extern vector<ast_node*>* cell_children;
extern vector<ast_node*>* figure_children;
extern vector<ast_node*>* math_children;
extern stack<vector<ast_node*> > content_stack;
extern stack<vector<ast_node*> > r_content_stack;
extern stack<vector<ast_node*> > list_stack;
ast_node* new_node(){
ast_node* temp = new ast_node;
return temp;
}
ast_node* new_node(string data){
ast_node* temp = new ast_node;
temp->data = data;
return temp;
}
void init_content_children(){
content_children = new vector<ast_node*>();
}
void adopt_content_children(ast_node* current){
current->children = *content_children;
if(!content_stack.empty()){
*content_children = content_stack.top();
content_stack.pop();
}
}
void make_new_content(){
content_stack.push(*content_children);
init_content_children();
}
void init_list_children(){
list_children = new vector<ast_node*>();
}
void adopt_list_children(ast_node* current){
current->children = *list_children;
if(!list_stack.empty()){
*list_children = list_stack.top();
list_stack.pop();
}
}
void make_new_list(){
//cout<<"New list \n";
list_stack.push(*list_children);
init_list_children();
}
void adopt_section_children(ast_node* current){
current->children = *section_children;
}
void init_section_children(){
section_children = new vector<ast_node*>();
}
void adopt_math_children(ast_node* current){
current->children = *math_children;
}
void init_math_children(){
math_children = new vector<ast_node*>();
}
void adopt_subsection_children(ast_node* current){
current->children = *subsection_children;
}
void init_subsection_children(){
subsection_children = new vector<ast_node*>();
}
void adopt_rows_children(ast_node* current){
current->children = *rows_children;
}
void init_rows_children(){
rows_children = new vector<ast_node*>();
}
void adopt_cell_children(ast_node* current){
current->children = *cell_children;
}
void init_cell_children(){
cell_children = new vector<ast_node*>();
}
void print(ast_node* root, int tabs){
if(root == NULL){
return;
}
for(int i=0; i<tabs; i++){
cout<<" ";
}
cout<<root->node_type<<endl;
for(int i=0; i<root->children.size(); i++){
print(root->children.at(i), tabs+1);
}
}
void init_r_content_children(){
r_content_children = new vector<ast_node*>();
}
void adopt_r_content_children(ast_node* current){
current->children = *r_content_children;
if(!r_content_stack.empty()){
*r_content_children = r_content_stack.top();
r_content_stack.pop();
}
}
void make_new_r_content(){
//cout<<"New content children\n";
r_content_stack.push(*r_content_children);
init_r_content_children();
}
void init_figure_children(){
figure_children = new vector<ast_node*>();
}
void adopt_figure_children(ast_node* current){
current->children = *figure_children;
}