-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.c
354 lines (286 loc) · 9.54 KB
/
node.c
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include "codegen.h"
#include "compiler.h"
#include "helpers/vector.h"
#include <assert.h>
struct vector* node_vector = NULL;
struct vector* node_vector_root = NULL;
struct node* parser_current_body = NULL;
struct node* parser_current_function = NULL;
void node_set_vector(struct vector* vec, struct vector* root_vec)
{
node_vector = vec;
node_vector_root = root_vec;
}
void node_push(struct node* node)
{
vector_push(node_vector, &node);
}
struct node* node_peek_or_null()
{
return vector_back_ptr_or_null(node_vector);
}
struct node* node_peek()
{
return *(struct node**)(vector_back(node_vector));
}
struct node* node_pop()
{
struct node* last_node = vector_back_ptr(node_vector);
struct node* last_node_root = vector_empty(node_vector) ? NULL : vector_back_ptr_or_null(node_vector_root);
vector_pop(node_vector);
if (last_node == last_node_root)
{
vector_pop(node_vector_root);
}
return last_node;
}
bool node_is_expressionable(struct node* node)
{
return node->type == NODE_TYPE_EXPRESSION ||
node->type == NODE_TYPE_EXPRESSION_PARENTHESES ||
node->type == NODE_TYPE_UNARY ||
node->type == NODE_TYPE_IDENTIFIER ||
node->type == NODE_TYPE_NUMBER ||
node->type == NODE_TYPE_STRING;
}
bool node_is_struct_or_union(struct node* node)
{
return node && (node->type == NODE_TYPE_STRUCT || node->type == NODE_TYPE_UNION);
}
bool node_is_struct_or_union_variable(struct node* node)
{
if (node->type != NODE_TYPE_VARIABLE)
{
return false;
}
return datatype_is_struct_or_union(&node->var.type);
}
bool variable_node_is_primitive(struct node* node)
{
assert(node->type == NODE_TYPE_VARIABLE);
return datatype_is_primitive(&node->var.type);
}
bool node_is_expression_or_parentheses(struct node* node)
{
return node->type == NODE_TYPE_EXPRESSION || node->type == NODE_TYPE_EXPRESSION_PARENTHESES;
}
bool node_is_value_type(struct node* node)
{
return node_is_expression_or_parentheses(node) || node->type == NODE_TYPE_IDENTIFIER || node->type == NODE_TYPE_NUMBER || node->type == NODE_TYPE_UNARY || node->type == NODE_TYPE_TENARY || node->type == NODE_TYPE_STRING;
}
bool node_is_expression(struct node* node, const char* op)
{
return node->type == NODE_TYPE_EXPRESSION && S_EQ(node->exp.op, op);
}
bool is_assignment_node(struct node* node)
{
return S_EQ(node->exp.op, "=") ||
S_EQ(node->exp.op, "+=") ||
S_EQ(node->exp.op, "-=") ||
S_EQ(node->exp.op, "*=") ||
S_EQ(node->exp.op, "/=") ||
S_EQ(node->exp.op, ">>=") ||
S_EQ(node->exp.op, "<<=");
}
bool node_is_valid(struct node* node)
{
return node && node->type != NODE_TYPE_BLANK;
}
bool function_node_is_prototype(struct node* node)
{
return node->func.body_n == NULL;
}
struct node* node_peek_expressionable_or_null()
{
struct node* last_node = node_peek_or_null();
return node_is_expressionable(last_node) ? last_node : NULL;
}
struct node* variable_node(struct node* node)
{
struct node* var_node = NULL;
switch (node->type)
{
case NODE_TYPE_VARIABLE:
var_node = node;
break;
case NODE_TYPE_STRUCT:
var_node = node->_struct.var;
break;
case NODE_TYPE_UNION:
var_node = node->_union.var;
break;
}
return var_node;
}
struct node* variable_node_or_list(struct node* node)
{
if (node->type == NODE_TYPE_VARIABLE_LIST)
{
return node;
}
return variable_node(node);
}
size_t function_node_argument_stack_addition(struct node* node)
{
assert(node->type == NODE_TYPE_FUNCTION);
return node->func.args.stack_addition;
}
size_t function_node_stack_size(struct node* node)
{
assert(node->type == NODE_TYPE_FUNCTION);
return node->func.stack_size;
}
struct vector* function_node_argument_vec(struct node* node)
{
assert(node->type == NODE_TYPE_FUNCTION);
return node->func.args.vector;
}
struct node* node_create(struct node* _node)
{
struct node* node = malloc(sizeof(struct node));
memcpy(node, _node, sizeof(struct node));
node->binded.owner = parser_current_body;
node->binded.function = parser_current_function;
node_push(node);
return node;
}
void make_exp_node(struct node* left_node, struct node* right_node, const char* op)
{
assert(left_node);
assert(right_node);
node_create(&(struct node){.type=NODE_TYPE_EXPRESSION, .exp.left=left_node, .exp.right=right_node, .exp.op=op});
}
void make_exp_parenthesis_node(struct node* exp_node)
{
node_create(&(struct node){.type=NODE_TYPE_EXPRESSION_PARENTHESES, .parenthesis.exp=exp_node});
}
void make_tenary_node(struct node* true_node, struct node* false_node)
{
node_create(&(struct node){.type=NODE_TYPE_TENARY, .tenary.true_node=true_node, .tenary.false_node=false_node});
}
void make_bracket_node(struct node* node)
{
node_create(&(struct node){.type=NODE_TYPE_BRACKET, .bracket.inner=node});
}
void make_body_node(struct vector* body_vec, size_t size, bool padded, struct node* largest_var_node)
{
node_create(&(struct node){.type=NODE_TYPE_BODY, .body.statements=body_vec, .body.size=size, .body.padded=padded, .body.largest_var_node=largest_var_node});
}
void make_struct_node(const char* name, struct node* body_node)
{
int flags = 0;
if (!body_node)
{
flags |= NODE_FLAG_IS_FORWARD_DECLARATION;
}
node_create(&(struct node){.type=NODE_TYPE_STRUCT, ._struct.body_n=body_node, ._struct.name=name, .flags=flags});
}
void make_union_node(const char* name, struct node* body_node)
{
int flags = 0;
if (!body_node)
{
flags |= NODE_FLAG_IS_FORWARD_DECLARATION;
}
node_create(&(struct node){.type=NODE_TYPE_UNION, ._union.body_n=body_node, ._union.name=name, .flags=flags});
}
void make_function_node(struct datatype* ret_type, const char* name, struct vector* arguments, struct node* body_node)
{
struct node* function_node = node_create(&(struct node){.type=NODE_TYPE_FUNCTION, .func.name=name, .func.body_n=body_node, .func.rtype=*ret_type, .func.args.vector=arguments, .func.args.stack_addition=DATA_SIZE_DDWORD});
function_node->func.frame.elements = vector_create(sizeof(struct stack_frame_element));
}
void make_return_node(struct node* exp_node)
{
node_create(&(struct node){.type=NODE_TYPE_STATEMENT_RETURN, .stmt.return_stmt.exp=exp_node});
}
void make_if_node(struct node* cond_node, struct node* body_node, struct node* next_node)
{
node_create(&(struct node){.type=NODE_TYPE_STATEMENT_IF, .stmt.if_stmt.cond_node=cond_node, .stmt.if_stmt.body_node=body_node, .stmt.if_stmt.next=next_node});
}
void make_else_node(struct node* body_node)
{
node_create(&(struct node){.type=NODE_TYPE_STATEMENT_ELSE, .stmt.else_stmt.body_node=body_node});
}
void make_for_node(struct node* init_node, struct node* cond_node, struct node* loop_node, struct node* body_node)
{
node_create(&(struct node){.type=NODE_TYPE_STATEMENT_FOR, .stmt.for_stmt.init_node=init_node, .stmt.for_stmt.cond_node=cond_node, .stmt.for_stmt.loop_node=loop_node, .stmt.for_stmt.body_node=body_node});
}
void make_while_node(struct node* exp_node, struct node* body_node)
{
node_create(&(struct node){.type=NODE_TYPE_STATEMENT_WHILE, .stmt.while_stmt.exp_node=exp_node, .stmt.while_stmt.body_node=body_node});
}
void make_do_while_node(struct node* body_node, struct node* exp_node)
{
node_create(&(struct node){.type=NODE_TYPE_STATEMENT_DO_WHILE, .stmt.do_while_stmt.exp_node=exp_node, .stmt.do_while_stmt.body_node=body_node});
}
void make_switch_node(struct node* exp_node, struct node* body_node, struct vector* cases, bool has_default_case)
{
node_create(&(struct node){.type=NODE_TYPE_STATEMENT_SWITCH, .stmt.switch_stmt.exp_node=exp_node, .stmt.switch_stmt.body_node=body_node, .stmt.switch_stmt.cases=cases, .stmt.switch_stmt.has_default_case=has_default_case});
}
void make_case_node(struct node* exp_node)
{
node_create(&(struct node){.type=NODE_TYPE_STATEMENT_CASE, .stmt.case_stmt.exp=exp_node});
}
void make_default_node()
{
node_create(&(struct node){.type=NODE_TYPE_STATEMENT_DEFAULT});
}
void make_break_node()
{
node_create(&(struct node){.type=NODE_TYPE_STATEMENT_BREAK});
}
void make_continue_node()
{
node_create(&(struct node){.type=NODE_TYPE_STATEMENT_CONTINUE});
}
void make_goto_node(struct node* label_node)
{
node_create(&(struct node){.type=NODE_TYPE_STATEMENT_GOTO, .stmt.goto_stmt.label=label_node});
}
void make_label_node(struct node* name_node)
{
node_create(&(struct node){.type=NODE_TYPE_LABEL, .label.name=name_node});
}
void make_cast_node(struct datatype* dtype, struct node* operand_node)
{
node_create(&(struct node){.type=NODE_TYPE_CAST, .cast.dtype=*dtype, .cast.operand=operand_node});
}
void make_unary_node(const char* op, struct node* operand_node, int flags)
{
node_create(&(struct node){.type=NODE_TYPE_UNARY, .unary.op=op, .unary.operand=operand_node, .unary.flags=flags});
}
struct node* node_from_sym(struct symbol* sym)
{
if (sym->type != SYMBOL_TYPE_NODE)
{
return NULL;
}
return sym->data;
}
struct node* node_from_symbol(struct compile_process* current_process, const char* name)
{
struct symbol* sym = symresolver_get_symbol(current_process, name);
if (!sym)
{
return NULL;
}
return node_from_sym(sym);
}
struct node* struct_node_for_name(struct compile_process* current_process, const char* name)
{
struct node* node = node_from_symbol(current_process, name);
if (!node)
return NULL;
if (node->type != NODE_TYPE_STRUCT)
return NULL;
return node;
}
struct node* union_node_for_name(struct compile_process* current_process, const char* name)
{
struct node* node = node_from_symbol(current_process, name);
if (!node)
return NULL;
if (node->type != NODE_TYPE_UNION)
return NULL;
return node;
}