-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastree.c
361 lines (340 loc) · 9.29 KB
/
astree.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
#include "astree.h"
#include "symbol_table.h"
#include <stdlib.h>
#include <stdio.h>
/*REMOVE!!!*/
static const char *data_type_to_string[] = {
"", "double", "", "float", "", "long", "", "",
"", "", "", "", "", "", "", "short",
"", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "byte",
"boolean"
};
struct astree *ast_create(int type, struct hm_item *symbol,
struct astree *c0,
struct astree *c1,
struct astree *c2,
struct astree *c3)
{
struct astree *node = calloc(1, sizeof(struct astree));
if (!node) {
perror("astree: node alocation");
return NULL;
}
node->type = type;
node->symbol = symbol;
node->children[0] = c0;
node->children[1] = c1;
node->children[2] = c2;
node->children[3] = c3;
return node;
}
void ast_terminate(struct astree *tree) {
if (!tree) {
return;
}
if(tree->type == AST_RETURN){
free(tree->symbol);
}
for (int i = 0; i < AST_MAXCHILDREN; i++) {
ast_terminate(tree->children[i]);
}
free(tree);
}
static const char *type_to_string[] = {
"symbol",
"+",
"-",
"*",
"/",
"prog",
"byte",
"short",
"long",
"float",
"double",
"decl_list",
"var",
"vec",
"vec_init",
"func",
"fheader",
"params",
"cmd_list",
"var_attr",
"vec_attr",
"read",
"print",
"print_list",
"return",
"vec_sub",
"call",
"args",
"<",
">",
"!",
"<=",
">=",
"==",
"!=",
"&&",
"||",
"when",
"when_else",
"while",
"for",
"block",
"expr_block"
};
void ast_fprint(FILE *stream, int level, struct astree *tree) {
if (!tree) {
return;
}
int i;
for (i = 0; i < level; i++) {
fprintf(stream, "| ");
}
fprintf(stream, "%s", type_to_string[tree->type]);
if (tree->type == AST_SYM) {
fprintf(stream, ": %s, ", tree->symbol->key);
symtab_fprint_item(stream, tree->symbol->value);
}
fprintf(stream, "\n");
for (i = 0; i < AST_MAXCHILDREN; i++) {
ast_fprint(stream, level + 1, tree->children[i]);
}
}
static void print_identation(FILE* stream, int level){
int i;
for(i = 0; i < level; i++)
fprintf(stream, " ");
}
void ast_make_source(FILE* stream, struct astree* tree, int level){
if (tree == NULL)
return;
switch (tree->type) {
case AST_VAR_ATTR: case AST_VEC_ATTR: case AST_READ:
case AST_PRINT: case AST_RETURN: case AST_BLOCK:
case AST_WHEN: case AST_WHEN_ELSE: case AST_WHILE: case AST_FOR:
print_identation(stream, level);
break;
default:
break;
}
switch(tree->type){
/* Init program*/
case AST_PROG:
ast_make_source(stream, tree->children[0], level);
break;
/* Lists*/
case AST_CMD_LIST: case AST_DECL_LIST:
ast_make_source(stream, tree->children[0], level);
ast_make_source(stream, tree->children[1], level);
if(tree->children[1] == NULL)
print_identation(stream, level);
fprintf(stream, ";\n");
break;
/* All types of symbols*/
case AST_SYM:
fprintf(stream, "%s", tree->symbol->key);
break;
/* Types*/
case AST_KW_BYTE: case AST_KW_SHORT: case AST_KW_LONG:
case AST_KW_FLOAT: case AST_KW_DOUBLE:
fprintf(stream, "%s", type_to_string[tree->type]);
break;
/* Variable declarition*/
case AST_VAR:
ast_make_source(stream, tree->children[0], level);
fprintf(stream, " : ");
ast_make_source(stream, tree->children[1], level);
fprintf(stream, " ");
ast_make_source(stream, tree->children[2], level);
break;
/* Vector declarition*/
case AST_VEC:
ast_make_source(stream, tree->children[0], level);
fprintf(stream, " : ");
ast_make_source(stream, tree->children[1], level);
fprintf(stream, " [");
ast_make_source(stream, tree->children[2], level);
fprintf(stream, "]");
ast_make_source(stream, tree->children[3], level);
break;
/* Vector inicialization*/
case AST_VEC_INIT:
ast_make_source(stream, tree->children[0], level);
fprintf(stream, " ");
ast_make_source(stream, tree->children[1], level);
break;
/* Function declarition*/
case AST_FUNC:
ast_make_source(stream, tree->children[0], level);
if(tree->children[1] != NULL && tree->children[1]->type == AST_BLOCK)
ast_make_source(stream, tree->children[1], level);
else
ast_make_source(stream, tree->children[1], level + 1);
break;
/* Function Header declarition*/
case AST_FHEADER:
ast_make_source(stream, tree->children[0], level);
fprintf(stream, " ");
ast_make_source(stream, tree->children[1], level);
fprintf(stream, "(");
ast_make_source(stream, tree->children[2], level);
fprintf(stream, ")\n");
break;
/* Parameters*/
case AST_PARAMS:
if(tree->children[0] != NULL) {
ast_make_source(stream, tree->children[0], level);
fprintf(stream, ", ");
}
ast_make_source(stream, tree->children[1], level);
fprintf(stream, " ");
ast_make_source(stream, tree->children[2], level);
break;
/* Block of commands*/
case AST_BLOCK:
fprintf(stream, "{\n");
ast_make_source(stream, tree->children[0], level + 1);
print_identation(stream, level);
fprintf(stream, "}");
break;
/* Attribution command*/
case AST_VAR_ATTR:
ast_make_source(stream, tree->children[0], level);
fprintf(stream, " = ");
ast_make_source(stream, tree->children[1], level);
break;
/* Attribution command*/
case AST_VEC_ATTR:
ast_make_source(stream, tree->children[0], level);
fprintf(stream, "#");
ast_make_source(stream, tree->children[1], level);
fprintf(stream, " = ");
ast_make_source(stream, tree->children[2], level);
break;
/* Read command*/
case AST_READ:
fprintf(stream, "%s ", type_to_string[tree->type]);
ast_make_source(stream, tree->children[0], level);
break;
/* Print command*/
case AST_PRINT:
fprintf(stream, "%s ", type_to_string[tree->type]);
ast_make_source(stream, tree->children[0], level);
break;
case AST_PRINT_LIST:
ast_make_source(stream, tree->children[0], level);
ast_make_source(stream, tree->children[1], level);
//if(tree->children[0] != NULL)
fprintf(stream, " ");
break;
/* Return statement*/
case AST_RETURN:
fprintf(stream, "%s ", type_to_string[tree->type]);
ast_make_source(stream, tree->children[0], level);
break;
/* Expressions*/
/* Unary operatiors*/
case AST_NOT:
fprintf(stream, "%s ", type_to_string[tree->type]);
ast_make_source(stream, tree->children[0], level);
break;
/* Operators*/
case AST_ADD: case AST_SUB: case AST_MUL: case AST_DIV:
case AST_LT: case AST_GT: case AST_LE: case AST_GE:
case AST_EQ: case AST_NE: case AST_AND: case AST_OR:
ast_make_source(stream, tree->children[0], level);
fprintf(stream, " %s ", type_to_string[tree->type]);
ast_make_source(stream, tree->children[1], level);
break;
/* Call operatior*/
case AST_CALL:
ast_make_source(stream, tree->children[0], level);
fprintf(stream, "(");
ast_make_source(stream, tree->children[1], level);
fprintf(stream, ")");
break;
/* Vector as expression*/
case AST_VEC_SUB:
ast_make_source(stream, tree->children[0], level);
fprintf(stream, "[");
ast_make_source(stream, tree->children[1], level);
fprintf(stream, "]");
break;
case AST_EXP_BLOCK:
fprintf(stream, "(");
ast_make_source(stream, tree->children[0], level);
fprintf(stream, ")");
break;
/* Arguments*/
case AST_ARGS:
ast_make_source(stream, tree->children[0], level);
if(tree->children[0] != NULL)
fprintf(stream, ", ");
ast_make_source(stream, tree->children[1], level);
break;
/* Control statements*/
case AST_WHEN:
fprintf(stream, "when (");
ast_make_source(stream, tree->children[0], level);
fprintf(stream, ") then\n");
if(tree->children[1] == NULL)
print_identation(stream, level + 1);
else if(tree->children[1]->type == AST_BLOCK)
ast_make_source(stream, tree->children[1], level);
else
ast_make_source(stream, tree->children[1], level + 1);
break;
case AST_WHEN_ELSE:
fprintf(stream, "when (");
ast_make_source(stream, tree->children[0], level);
fprintf(stream, ") then\n");
if(tree->children[1] == NULL)
print_identation(stream, level + 1);
else if(tree->children[1]->type == AST_BLOCK)
ast_make_source(stream, tree->children[1], level);
else
ast_make_source(stream, tree->children[1], level + 1);
fprintf(stream, "\n");
print_identation(stream, level);
fprintf(stream, "else\n");
if(tree->children[2] == NULL)
print_identation(stream, level + 1);
else if(tree->children[2]->type == AST_BLOCK)
ast_make_source(stream, tree->children[2], level);
else
ast_make_source(stream, tree->children[2], level + 1);
break;
case AST_WHILE:
fprintf(stream, "while (");
ast_make_source(stream, tree->children[0], level);
fprintf(stream, ")\n");
if(tree->children[1] == NULL)
print_identation(stream, level + 1);
else if(tree->children[1]->type == AST_BLOCK)
ast_make_source(stream, tree->children[1], level);
else
ast_make_source(stream, tree->children[1], level + 1);
break;
case AST_FOR:
fprintf(stream, "for (");
ast_make_source(stream, tree->children[0], level);
fprintf(stream, " = ");
ast_make_source(stream, tree->children[1], level);
fprintf(stream, " to ");
ast_make_source(stream, tree->children[2], level);
fprintf(stream, ")\n");
if(tree->children[3] == NULL)
print_identation(stream, level + 1);
else if(tree->children[3]->type == AST_BLOCK)
ast_make_source(stream, tree->children[3], level);
else
ast_make_source(stream, tree->children[3], level + 1);
break;
default:
break;
}
}