-
Notifications
You must be signed in to change notification settings - Fork 6
/
pl_ast.h
188 lines (178 loc) · 3.18 KB
/
pl_ast.h
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#ifndef __PL_AST_H__
#define __PL_AST_H__
#include <iostream>
#include <cstring>
#include <sstream>
#include <vector>
enum ast_type
{
ast_root,
ast_prog,
ast_block,
ast_condecl,
ast_vardecl,
ast_proc,
ast_id,
ast_number,
ast_parameter,
ast_body,
ast_assign,
ast_condition,
ast_if,
ast_else,
ast_while,
ast_call,
ast_argument,
ast_read,
ast_write,
ast_odd,
ast_add,
ast_sub,
ast_mul,
ast_div,
ast_eq,
ast_neq,
ast_less,
ast_leq,
ast_great,
ast_geq
};
struct
{
const char* ast_content;
int type;
}ast_table[]=
{
{"root", ast_root},
{"program", ast_prog},
{"block", ast_block},
{"const_declare",ast_condecl},
{"var_declare", ast_vardecl},
{"procedure", ast_proc},
{"identifier", ast_id},
{"number", ast_number},
{"parameter", ast_parameter},
{"body", ast_body},
{"assignment", ast_assign},
{"conditional", ast_condition},
{"if", ast_if},
{"else", ast_else},
{"while", ast_while},
{"call", ast_call},
{"argument", ast_argument},
{"read", ast_read},
{"write", ast_write},
{"odd", ast_odd},
{"+", ast_add},
{"-", ast_sub},
{"*", ast_mul},
{"/", ast_div},
{"=", ast_eq},
{"<>", ast_neq},
{"<", ast_less},
{"<=", ast_leq},
{">", ast_great},
{">=", ast_geq},
{NULL,-1}
};
class ast
{
private:
int line;
int type;
std::string content;
std::vector<ast> child;
public:
ast(int=0,int=ast_root,std::string="");
ast(const ast&);
~ast();
void addchild(ast);
void setline(int);
void settype(int);
void setstr(std::string&);
std::vector<ast>& getchild();
int getline();
int gettype();
std::string getstr();
ast& operator=(const ast&);
void print(int);
};
ast::ast(int default_line,int default_type,std::string default_content)
{
line=default_line;
type=default_type;
content=default_content;
return;
}
ast::ast(const ast& tmp)
{
line=tmp.line;
type=tmp.type;
content=tmp.content;
child=tmp.child;
return;
}
ast::~ast()
{
content="";
child.clear();
return;
}
void ast::addchild(ast tmp)
{
child.push_back(tmp);
return;
}
void ast::setline(int l)
{
line=l;
return;
}
void ast::settype(int t)
{
type=t;
return;
}
void ast::setstr(std::string& s)
{
content=s;
return;
}
std::vector<ast>& ast::getchild()
{
return child;
}
int ast::getline()
{
return line;
}
int ast::gettype()
{
return type;
}
std::string ast::getstr()
{
return content;
}
ast& ast::operator=(const ast& tmp)
{
line=tmp.line;
type=tmp.type;
content=tmp.content;
child=tmp.child;
return *this;
}
void ast::print(int depth=0)
{
std::string s="";
for(int i=0;i<depth;++i)
s+="| ";
std::cout<<s<<ast_table[type].ast_content;
if(type==ast_id || type==ast_number || type==ast_prog || type==ast_proc || type==ast_call || type==ast_assign)
std::cout<<": "<<content;
std::cout<<"\n";
for(std::vector<ast>::iterator i=child.begin();i!=child.end();++i)
i->print(depth+1);
return;
}
#endif