-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.c
77 lines (69 loc) · 1.58 KB
/
ast.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
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "ast.h"
ast *new_ast(const char *label, enum types type)
{
ast *ret = NULL;
ret = calloc(1, sizeof(ast));
if (ret != NULL) {
ret->type = type;
ret->label = strdup(label);
ret->number_of_children = 0;
ret->children = NULL;
ret->father = NULL;
}
return ret;
}
void free_ast(ast *tree)
{
if (tree != NULL) {
int i;
for (i = 0; i < tree->number_of_children; i++) {
free_ast(tree->children[i]);
}
free(tree->children);
free(tree->label);
free(tree);
}
}
void add_child(ast *tree, ast *child)
{
if (tree != NULL && child != NULL) {
tree->number_of_children++;
tree->children = realloc(tree->children, tree->number_of_children * sizeof(ast*));
tree->children[tree->number_of_children-1] = child;
child->father = tree;
}
}
ast *get_root(ast *tree)
{
if (tree != NULL) {
if (tree->father != NULL) {
return get_root(tree->father);
}
else {
return tree;
}
}
}
void exporta_ast(ast *tree)
{
int i;
if (tree != NULL) {
printf("%p [label=\"%s\"];\n", tree, tree->label);
for (i = 0; i < tree->number_of_children; i++) {
printf("%p, %p\n", tree, tree->children[i]);
}
for (i = 0; i < tree->number_of_children; i++) {
exporta_ast(tree->children[i]);
}
}
}
void exporta_code(ast *tree)
{
if(tree != NULL)
{
generateAsm(tree->code);
}
}