-
Notifications
You must be signed in to change notification settings - Fork 17
/
astnode.h
217 lines (205 loc) · 4.18 KB
/
astnode.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#ifndef ASTNODE_H
#define ASTNODE_H
#include <vector>
#include <iostream>
using namespace std;
#include "mytype.h"
#include "token.h"
#include "env.h"
//using std::vector;
/**
* \brief record ASTNode real type. ex: int, int pointer, char, function ...
*/
struct ObjType
{
public:
ObjType()
{
clear();
}
void clear_pointer()
{
pointer_number_ = 0;
pointer_ = false;
}
void clear()
{
pointer_ = char_ = int_ = func_ = array_ = global_ = false;
pointer_number_ = 0;
}
std::string str();
void set_int(){int_ = true;}
void set_char() {char_ = true;}
void set_func() {func_ = true;}
void set_global() {global_ = true;}
void set_pointer(int num=1)
{
pointer_number_ = num;
pointer_ = true;
}
bool is_int() const
{
if (int_)
return true;
else
return false;
}
bool is_pointer() const
{
if (pointer_ == true)
return true;
else
return false;
}
bool is_string() const
{
if (pointer_ == true && char_ == true)
return true;
else
return false;
}
bool pointer_;
bool char_;
bool int_;
bool func_;
bool array_;
bool global_;
u32 pointer_number_;
};
class ASTNode
{
public:
//ASTNode(const ASTNode* l, const ASTNode* op, const ASTNode* r);
ASTNode()
{
id_ = no_;
++no_;
eval_result_ = 0;
}
ASTNode(const Token &token):token_(token)
{
id_ = no_;
++no_;
eval_result_ = 0;
}
ASTNode(const ASTNode *n)
{
token_ = n->token();
set_obj_type(n->obj_type());
}
~ASTNode()
{
static u32 i=0;
//cout << "~ASTNode: " << id_ << ": " << str() << endl;
++i;
free_children();
// delete eval_result_; // need not delete eval_result_;
}
bool add_child(const std::vector<ASTNode*> &children)
{
for (auto i : children)
children_.push_back(i);
return true;
}
void set_token(const Token &t)
{
token_ = t;
}
bool add_child(ASTNode* l) // for - (NEG), ex: -52
{
children_.push_back(l);
return true;
}
bool add_child(ASTNode* l, ASTNode* r)
{
children_.push_back(l);
children_.push_back(r);
return true;
}
ASTNode* eval(Environment *env);
void set_ast_type(ASTType ast_type)
{
token_.ast_type_ = ast_type;
}
ASTType ast_type() const
{
return token_.ast_type();
}
const char* type_str() const
{
const char *type_str[]=
{
"INVALID", "ROOT", "PROG", "COMMENT",
"TRUE", "FALSE",
"ENUM", "INT", "CHAR",
"ADDR_OF", "DEREF", // &, *
"IF", "WHILE", "RETURN", "RETURN_VAL",
"THEN_BLOCK", "ELSE_BLOCK", "SEP", "NEG", "ASSIGN", "EQUAL",
"NAME", "GLOBAL_VAR", "VAR",
"FUNC_CALL", "FUNC_BODY", "FUNC_NAME", "FUNC_PARA",
"ADD", "MIN", "MUL", "DIV", "GREAT", "LESS",
"NUMBER", "STRING", "EOL"
};
if (INVALID <= ast_type() && ast_type() < LAST)
{
return type_str[ast_type()];
}
else
{
return "unknown";
}
}
const std::vector<ASTNode*> &children() const
{
return children_;
}
// for tool tree to print AST
void print_tree();
void print();
const ObjType & obj_type() const
{
return obj_type_;
}
void set_obj_type(const ObjType &obj_type)
{
obj_type_ = obj_type;
}
const std::string str() const
{
return token_.str_;
}
void set_str(const std::string &s)
{
token_.str_ = s;
}
bool is_leaf() const
{
if (children().size() == 0)
return true;
else
return false;
}
void free_children()
{
#if 1
for (auto &i : children_)
delete i;
#endif
children_.resize(0);
}
u32 num() const
{
return no_;
}
Token token() const {return token_;}
private:
vector<ASTNode*> children_;
ASTNode* eval_result_;
Token token_;
ObjType obj_type_;
u32 id_;
static u32 no_;
};
ASTNode *get_root();
void print_ast();
#endif