-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.c
407 lines (353 loc) · 9.04 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#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 ()
{
/* Get the last node of the vector. */
struct node *last_node = vector_back_ptr(node_vector);
/* If the node vector is not empty, get the last node on the root. */
struct node *last_node_root = vector_empty(node_vector) ? NULL : vector_back_ptr_or_null(node_vector_root);
/* Pop off the last node of the vector. */
vector_pop(node_vector);
if (last_node == last_node_root)
{
/*
* Pop off from the root vector as well.
* This ensures there are no duplicates.
*/
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;
}
/*
* Peek to the last node on the stack, if its something we
* can put in an expression, return it. Otherwise return null.
* If we peek and its function node or something, we cannot put
* a function into an expression. So, we return 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;
}
void
make_case_node (struct node* exp_node)
{
node_create ( & (struct node)
{
.type=NODE_TYPE_STATEMENT_CASE,
.stmt._case.exp_node=exp_node,
});
}
void
make_goto_node (struct node* name_node)
{
node_create (& (struct node)
{
.type=NODE_TYPE_STATEMENT_GOTO,
.stmt._goto.label=name_node
});
}
void
make_label_node (struct node* name_node)
{
node_create (& (struct node)
{
.type=NODE_TYPE_LABEL,
.label.name=name_node,
});
}
void
make_continue_node ()
{
node_create (& (struct node)
{
.type=NODE_TYPE_STATEMENT_CONTINUE
});
}
void
make_break_node ()
{
node_create (& (struct node)
{
.type=NODE_TYPE_STATEMENT_BREAK
});
}
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_parentheses_node (struct node* exp_node)
{
node_create (&(struct node)
{
.type=NODE_TYPE_EXPRESSION_PARENTHESES,
.parenthesis.exp=exp_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_function_node (struct datatype* ret_type, const char* name,
struct vector* arguments, struct node* body_node)
{
node_create (&(struct node)
{
.type=NODE_TYPE_FUNCTION,
.func.name=name,
.func.args.vector=arguments,
.func.body_n=body_node,
.func.rtype=*ret_type,
.func.args.stack_addition=DATA_SIZE_DDWORD
});
#warning "Dont forget to build the frame elementss"
}
void
make_switch_node (struct node* exp_node, struct node* body_node,
struct vector* cases, bool has_default_cases)
{
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_cases
});
}
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_node.body_node=body_node,
.stmt.do_while_node.exp_node=exp_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_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_return_node (struct node* exp_node)
{
node_create (&(struct node)
{
.type=NODE_TYPE_STATEMENT_RETURN,
.stmt.return_stmt.exp=exp_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_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
});
}
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 = symbol_resolver_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
*node_create (struct node *_node)
{
struct node *node = malloc(sizeof(struct node));
memcpy(node, _node, sizeof(struct node));
#warning "Should set the binded owner and binded function here."
node->binded.owner = parser_current_body;
node->binded.function = parser_current_function;
node_push(node);
return node;
}
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);
}
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; */
assert(1 == 0 && "Unions are not yet supported.\n");
break;
}
return var_node;
}
bool
variable_node_is_primitive (struct node* node)
{
assert(node->type == NODE_TYPE_VARIABLE);
return datatype_is_primitive(&node->var.type);
}
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;
}
bool
node_is_expression_or_parentheses (struct node* node)
{
return node->type == NODE_TYPE_EXPRESSION_PARENTHESES || \
node->type == NODE_TYPE_EXPRESSION;
}
bool
node_is_value_type (struct node* node)
{
/* They all return a value that can be passed into a variable. */
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;
}