-
Notifications
You must be signed in to change notification settings - Fork 1
/
ast.c
201 lines (168 loc) · 4.51 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
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
#include "ast.h"
char *stringFromNodeType(ast_node_type f)
{
char *strings[] = {
"AST_BLOC",
"AST_STATEMENT",
"AST_EXPRESSION",
"AST_IF",
"AST_ELSE",
"AST_ELIF",
"AST_FOR_LOOP",
"AST_WHILE_LOOP",
"AST_BREAK",
"AST_CONTINUE",
"AST_TRUE",
"AST_FALSE",
"AST_ID",
"AST_OPENPARENTHESIS",
"AST_CLOSEPARENTHESIS",
// logical operations
"AST_EQUAL",
"AST_NONEQUAL",
"AST_AND",
"AST_OR",
"AST_NON",
"AST_INFERIOR",
"AST_SUPERIOR",
"AST_INFERIOREQUAL",
"AST_SUPERIOREQUAL",
// Arithmetic operation
"AST_ADD",
"AST_SUB",
"AST_MULT",
"AST_DIV",
"AST_MOD",
"AST_POWER",
"AST_ASSIGNMENT",
"AST_RETURN",
// constants
"AST_INTEGER",
"AST_REAL",
"AST_STRING",
// For functions
"AST_FUNCTION",
"AST_FUNCPARAM",
"AST_FUNCCALL",
"AST_POINTERVALUE",
"AST_ADDRESSVALUE",
};
return strings[f];
}
ast *build_ast(ast_node_type node_type)
{
ast *tree = (ast *)malloc(sizeof(ast));
ast_node *root = (ast_node *)malloc(sizeof(ast_node));
tree->root = root;
tree->root->parent = NULL;
tree->root->node_type = node_type;
tree->root->index = -1; // initially it's a leaf node
tree->root->label = stringFromNodeType(node_type);
for (int i = 0; i < MAX_CHILDREN; i++)
{
tree->root->children[i] = NULL;
}
tree->number_of_nodes = 1;
tree->root->id = 1;
return tree;
}
ast_node *create_node(ast_node_type node_type)
{
ast_node *new_node = (ast_node *)malloc(sizeof(ast_node));
for (int i = 0; i < MAX_CHILDREN; i++)
{
new_node->children[i] = NULL;
}
new_node->index = -1; // added as a leaf node
new_node->node_type = node_type;
new_node->label = stringFromNodeType(node_type);
return new_node;
}
ast_node *add_child(ast *tree, ast_node *parent, ast_node *node)
{
int where = parent->index + 1;
parent->index = parent->index + 1;
parent->children[where] = node; // linking
tree->number_of_nodes += 1;
node->id = tree->number_of_nodes;
node->parent = parent;
return node;
}
// ast_node *add_child(ast *tree, ast_node *node, ast_node_type node_type)
// {
// int where = node->index + 1;
// ast_node *new_node = (ast_node *)malloc(sizeof(ast_node));
// // increase the number of nodes
// tree->number_of_nodes += 1;
// new_node->index = -1; // added as a leaf node
// new_node->node_type = node_type;
// new_node->label = stringFromNodeType(node_type);
// for (int i = 0; i < MAX_CHILDREN; i++)
// {
// new_node->children[i] = NULL;
// }
// node->index = node->index + 1;
// node->children[where] = new_node; // linking
// new_node->id = tree->number_of_nodes;
// new_node->parent = node;
// return new_node;
// }
void set_val(ast_node *node, ast_node_val val)
{
// node values are only for string, int and reals
node->node_val = val;
}
ast_node *get_child(ast_node *node, int index)
{
return node->children[index];
}
size_t number_of_children(ast_node *node)
{
return node->index + 1;
}
bool is_leaf(ast_node *node)
{
return node->index == -1 ? true : false;
}
void destroy_ast(ast_node *node, int num_of_children)
{
if (is_leaf(node))
{
free(node);
return;
}
// recursive node destruction
for (int i = 0; i < num_of_children; i++)
{
destroy_ast(node->children[i], node->children[i]->index + 1);
}
}
void aux_ast_print(ast_node *node, FILE *stream)
{
if (is_leaf(node))
{
fprintf(stream, " %d[label=%s];\n", node->id, node->label);
return;
}
for (int i = 0; i < node->index + 1; i++)
{
fprintf(stream, " %d[label=%s];\n", node->id, node->label);
fprintf(stream, " %d -> %d;\n", node->id, node->children[i]->id);
aux_ast_print(node->children[i], stream);
}
}
void main_ast_print(ast *tree, FILE *stream)
{
fprintf(stream, "digraph AST {\n");
fprintf(stream, " node [fontname=\"Arial\"];\n");
if (!tree)
fprintf(stream, "\n");
else if (is_leaf(tree->root))
{
fprintf(stream, " %d[label=%s];\n", tree->root->id, tree->root->label);
fprintf(stream, " %d;\n", tree->root->id);
}
else
aux_ast_print(tree->root, stream);
fprintf(stream, "}\n");
}