-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.c
227 lines (200 loc) · 5.42 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
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
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ast.h"
#include "tables.h"
#include "types.h"
#define CHILDREN_LIMIT 20 // Don't try this at home, kids... :P
struct node {
NodeKind kind;
union {
int as_int;
float as_float;
} data;
Type type;
int count;
AST* child[CHILDREN_LIMIT];
};
static const char *KIND_STRING[] = {
"PROGRAM_NODE",
"BLOCK_NODE",
"WHILE_NODE",
"IF_NODE",
"ASSIGN_NODE",
"NOT_NODE",
"EQ_NODE",
"LT_NODE",
"GT_NODE",
"AND_NODE",
"OR_NODE",
"NEG_NODE",
"PLUS_NODE",
"MINUS_NODE",
"TIMES_NODE",
"OVER_NODE",
"FUNC_DECL_NODE",
"FUNC_USE_NODE",
"RETURN_NODE",
"VAR_DECL_NODE",
"VAR_LIST_NODE",
"VAR_USE_NODE",
"ARRAY_USE_NODE",
"INT_VAL_NODE",
"FLOAT_VAL_NODE",
"STRING_VAL_NODE",
"VOID_VAL_NODE",
"I2R_NODE",
"R2I_NODE"
};
const char* get_kind_text(NodeKind kind) {
return KIND_STRING[kind];
}
AST* new_node(NodeKind kind, int data, Type type) {
AST* node = malloc(sizeof * node);
node->kind = kind;
node->data.as_int = data;
node->type = type;
node->count = 0;
for (int i = 0; i < CHILDREN_LIMIT; i++) {
node->child[i] = NULL;
}
return node;
}
void add_child(AST *parent, AST *child) {
if (parent->count == CHILDREN_LIMIT) {
fprintf(stderr, "Cannot add another child!\n");
exit(1);
}
parent->child[parent->count] = child;
parent->count++;
}
AST* get_child(AST *parent, int idx) {
return parent->child[idx];
}
AST* new_subtree(NodeKind kind, Type type, int child_count, ...) {
if (child_count > CHILDREN_LIMIT) {
fprintf(stderr, "Too many children as arguments!\n");
exit(1);
}
AST* node = new_node(kind, 0, type);
va_list ap;
va_start(ap, child_count);
for (int i = 0; i < child_count; i++) {
add_child(node, va_arg(ap, AST*));
}
va_end(ap);
return node;
}
NodeKind get_kind(AST *node) {
return node->kind;
}
int get_data(AST *node) {
return node->data.as_int;
}
void set_float_data(AST *node, float data) {
node->data.as_float = data;
}
float get_float_data(AST *node) {
return node->data.as_float;
}
Type get_node_type(AST *node) {
return node->type;
}
int get_child_count(AST *node) {
return node->count;
}
void free_tree(AST *tree) {
if (tree == NULL) return;
for (int i = 0; i < tree->count; i++) {
free_tree(tree->child[i]);
}
free(tree);
}
// Dot output.
int nr;
extern VarTable *var_table;
extern FuncTable *func_table;
char* kind2str(NodeKind kind) {
switch(kind) {
case ASSIGN_NODE: return ":=";
case EQ_NODE: return "=";
case BLOCK_NODE: return "block";
case IF_NODE: return "if";
case INT_VAL_NODE: return "";
case LT_NODE: return "<";
case GT_NODE: return ">";
case NOT_NODE: return "!";
case NEG_NODE: return "--";
case AND_NODE: return "&&";
case OR_NODE: return "||";
case MINUS_NODE: return "-";
case OVER_NODE: return "/";
case PLUS_NODE: return "+";
case PROGRAM_NODE: return "program";
case FLOAT_VAL_NODE: return "";
case WHILE_NODE: return "repeat";
case STRING_VAL_NODE: return "";
case TIMES_NODE: return "*";
case VOID_VAL_NODE: return "";
case FUNC_DECL_NODE: return "func_decl";
case RETURN_NODE: return "return";
case VAR_DECL_NODE: return "var_decl";
case VAR_LIST_NODE: return "var_list";
case VAR_USE_NODE: return "var_use";
case ARRAY_USE_NODE: return "array_use";
case I2R_NODE: return "I2R";
case R2I_NODE: return "R2I";
default: return "ERROR!!";
}
}
int has_data(NodeKind kind) {
switch(kind) {
case VOID_VAL_NODE:
case INT_VAL_NODE:
case FLOAT_VAL_NODE:
case STRING_VAL_NODE:
case VAR_DECL_NODE:
case VAR_USE_NODE:
case FUNC_DECL_NODE:
case FUNC_USE_NODE:
return 1;
default:
return 0;
}
}
int print_node_dot(AST *node) {
int my_nr = nr++;
fprintf(stderr, "node%d[label=\"", my_nr);
fprintf(stderr, "(%s) ", get_text(node->type));
if (node->kind == VAR_DECL_NODE || node->kind == VAR_USE_NODE) {
fprintf(stderr, "%s@", get_name(var_table, node->data.as_int));
} else if(node->kind == FUNC_DECL_NODE || node->kind == FUNC_USE_NODE) {
fprintf(stderr, "%s@", get_func_name(func_table, node->data.as_int));
} else {
fprintf(stderr, "%s", kind2str(node->kind));
}
if (has_data(node->kind)) {
if (node->kind == FLOAT_VAL_NODE) {
fprintf(stderr, "%.2f", node->data.as_float);
} else if (node->kind == STRING_VAL_NODE) {
fprintf(stderr, "@%d", node->data.as_int);
} else if (node->kind == VOID_VAL_NODE) {
fprintf(stderr, "@VOID");
} else {
fprintf(stderr, "%d", node->data.as_int);
}
}
fprintf(stderr, "\"];\n");
for (int i = 0; i < node->count; i++) {
int child_nr = print_node_dot(node->child[i]);
fprintf(stderr, "node%d -> node%d;\n", my_nr, child_nr);
}
return my_nr;
}
void print_dot(AST *tree) {
nr = 0;
fprintf(stderr, "digraph {\ngraph [ordering=\"out\"];\n");
print_node_dot(tree);
fprintf(stderr, "}\n");
}