-
Notifications
You must be signed in to change notification settings - Fork 0
/
tables.c
338 lines (279 loc) · 8.05 KB
/
tables.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "tables.h"
#include "types.h"
// Strings Table
// ----------------------------------------------------------------------------
#define STRING_MAX_SIZE 128
#define STRINGS_TABLE_MAX_SIZE 100
#define GLOBAL_SCOPE 0
struct str_table {
char t[STRINGS_TABLE_MAX_SIZE][STRING_MAX_SIZE];
int size;
};
int add_string(StrTable* st, char* s) {
for (int i = 0; i < st->size; i++) {
if (strcmp(st->t[i], s) == 0) {
return i;
}
}
strcpy(st->t[st->size], s);
int idx_added = st->size;
st->size++;
return idx_added;
}
char* get_string(StrTable* st, int i) {
return st->t[i];
}
void print_str_table(StrTable* st) {
printf("Strings table:\n");
for (int i = 0; i < st->size; i++) {
printf("Entry %d -- %s\n", i, get_string(st, i));
}
}
int get_str_table_size(StrTable* st) {
return st->size;
}
StrTable* create_str_table() {
StrTable *st = malloc(sizeof * st);
st->size = 0;
add_string(st, "%s\0");
add_string(st, "%d\n\0");
add_string(st, "%f\n\0");
add_string(st, "%d\0");
add_string(st, "%f\0");
return st;
}
void free_str_table(StrTable* st) {
free(st);
}
// Variables Table
// ----------------------------------------------------------------------------
#define VARIABLE_MAX_SIZE 128
#define VARIABLES_TABLE_MAX_SIZE 100
typedef struct {
char name[VARIABLE_MAX_SIZE];
int scope;
int line;
Type type;
Type array_type;
int array_size;
int array_dimension;
int relative_pos;
} Entry;
struct var_table {
Entry t[VARIABLES_TABLE_MAX_SIZE];
int size;
};
int get_var_table_size(VarTable* vt) {
return vt->size;
}
int lookup_for_create_var(VarTable* vt, char* s, int scope) {
for (int i = 0; i < vt->size; i++) {
if (strcmp(vt->t[i].name, s) == 0 && scope == get_scope(vt, i))/*variable in the same scope passed or global scope*/ {
return i;
}
}
return -1;
}
int get_var_is_global_scope(VarTable* vt, int idx) {
return get_scope(vt, idx) == GLOBAL_SCOPE;
}
int lookup_var(VarTable* vt, char* s, int scope) {
// try to find local variable else try to get global one
int global = -1;
for (int i = 0; i < vt->size; i++) {
int var_scope = get_scope(vt, i);
int comp_res = strcmp(vt->t[i].name, s) == 0;
if (comp_res && scope == var_scope) {
return i;
} else if (comp_res && get_var_is_global_scope(vt, i)) {
global = i;
}
}
return global;
}
int add_to_var_table(VarTable* vt, char* s, int line, Type type, Type array_type, int scope, int dimension, int relative_pos) {
strcpy(vt->t[vt->size].name, s);
vt->t[vt->size].line = line;
vt->t[vt->size].type = type;
vt->t[vt->size].array_type = array_type;
vt->t[vt->size].scope = scope;
vt->t[vt->size].array_dimension = dimension;
vt->t[vt->size].relative_pos = relative_pos;
int idx_added = vt->size;
vt->size++;
return idx_added;
}
int add_var(VarTable* vt, char* s, int line, Type type, int scope, int relative_pos) {
return add_to_var_table(vt, s, line, type, type, scope, 0, relative_pos);
}
int add_array(VarTable* vt, char* s, int line, Type type, int scope, int dimension, int relative_pos, int size) {
int pos = add_to_var_table(vt, s, line, ARRAY, type, scope, dimension, relative_pos);
set_array_size(vt, pos, size);
return pos;
}
char* get_name(VarTable* vt, int i) {
return vt->t[i].name;
}
int get_line(VarTable* vt, int i) {
return vt->t[i].line;
}
Type get_array_type(VarTable* vt, int i) {
return vt->t[i].array_type;
}
Type get_type(VarTable* vt, int i) {
return vt->t[i].type;
}
int get_scope(VarTable* vt, int i) {
return vt->t[i].scope;
}
int get_var_offset(VarTable* vt, int i) {
return vt->t[i].relative_pos;
}
void set_array_size(VarTable* vt, int i, int size) {
vt->t[i].array_size = size;
}
int get_array_size(VarTable* vt, int i) {
return vt->t[i].array_size;
}
VarTable* create_var_table() {
VarTable *vt = malloc(sizeof * vt);
vt->size = 0;
add_var(vt, "elem", 0, STR_TYPE, 1, 0);
return vt;
}
void print_var_table(char* name, VarTable* vt) {
printf("%s table:\n", name);
for (int i = 0; i < vt->size; i++) {
printf("Entry %d -- name: %s, line: %d, type: %s, scope: %d, relative: %d\n", i,
get_name(vt, i), get_line(vt, i), get_text(get_type(vt, i)), get_scope(vt, i), get_var_offset(vt, i));
}
}
void free_var_table(VarTable* vt) {
free(vt);
}
// Function Table
// ----------------------------------------------------------------------------
#define FUNCTION_MAX_SIZE 128
#define FUNCTION_TABLE_MAX_SIZE 100
typedef struct {
char name[FUNCTION_MAX_SIZE];
int scope;
int line;
int builtin;
Type type;
Type *param_types;
AST* ast_start;
int num_param;
} FuncEntry;
struct func_table {
FuncEntry t[FUNCTION_TABLE_MAX_SIZE];
int num_vars[FUNCTION_TABLE_MAX_SIZE];
int size;
};
int get_func_table_size(FuncTable* ft) {
return ft->size;
}
int get_func_num_vars(FuncTable* ft, int i) {
return ft->num_vars[i];
}
void add_var_to_func(FuncTable* ft, int i) {
ft->num_vars[i]++;
}
void add_array_to_func(FuncTable* ft, int i, int array_size) {
ft->num_vars[i] += array_size;
}
int lookup_for_create_func(FuncTable* vt, char* s, int scope) {
for (int i = 0; i < vt->size; i++) {
if (strcmp(vt->t[i].name, s) == 0 && scope == get_func_scope(vt, i))/*variable in the same scope passed or global scope*/ {
return i;
}
}
return -1;
}
int lookup_func(FuncTable* vt, char* s, int scope) {
for (int i = 0; i < vt->size; i++) {
if (strcmp(vt->t[i].name, s) == 0 &&
(scope == get_func_scope(vt, i) || get_func_scope(vt,i) == GLOBAL_SCOPE))/*variable in the same scope passed or global scope*/ {
return i;
}
}
return -1;
}
int add_func_builtin(FuncTable* vt, char* s, int line, Type type, int scope) {
int pos = add_func(vt, s, line, type, scope);
vt->t[pos].builtin = 1;
return pos;
}
int add_func(FuncTable* vt, char* s, int line, Type type, int scope) {
strcpy(vt->t[vt->size].name, s);
vt->t[vt->size].line = line;
vt->t[vt->size].type = type;
vt->t[vt->size].scope = scope;
int idx_added = vt->size;
vt->t[vt->size].builtin = 0;
vt->size++;
return idx_added;
}
void add_func_params(FuncTable* vt, int i, Type *param_types, int num_param) {
vt->t[i].num_param = num_param;
vt->t[i].param_types = malloc(num_param * sizeof(Type));
for(int i = 0; i < num_param; i++) {
vt->t[i].param_types[i] = param_types[i];
}
}
char* get_func_name(FuncTable* vt, int i) {
return vt->t[i].name;
}
int get_func_line(FuncTable* vt, int i) {
return vt->t[i].line;
}
Type get_func_type(FuncTable* vt, int i) {
return vt->t[i].type;
}
int get_func_scope(FuncTable* vt, int i) {
return vt->t[i].scope;
}
int get_func_num_params(FuncTable* vt, int i) {
return vt->t[i].num_param;
}
void set_func_ast_start(FuncTable* ft, int i, AST* ast) {
ft->t[i].ast_start = ast;
}
AST* get_func_ast_start(FuncTable* ft, int i) {
return ft->t[i].ast_start;
}
int get_func_is_builtin(FuncTable* ft, int i) {
return ft->t[i].builtin;
}
void add_builtin_functions(FuncTable* ft) {
/*add_func_builtin(ft, "global", 0, VOID_TYPE, 0);*/
int pos = add_func_builtin(ft, "printf", 0, VOID_TYPE, 0);
Type args[] = {STR_TYPE};
add_func_params(ft, pos, args, 1);
pos = add_func_builtin(ft, "scanf", 0, VOID_TYPE, 0);
args[0] = INT_TYPE;
add_func_params(ft, pos, args, 1);
}
FuncTable* create_func_table() {
FuncTable *ft = malloc(sizeof * ft);
ft->size = 0;
add_builtin_functions(ft);
return ft;
}
void print_func_table(char* name, FuncTable* vt) {
printf("%s table:\n", name);
for (int i = 0; i < vt->size; i++) {
printf("Entry %d -- name: %s, line: %d, return type: %s, scope: %d, num params: %d\n",
i, get_func_name(vt, i), get_func_line(vt, i), get_text(get_func_type(vt, i)),
get_func_scope(vt, i), get_func_num_params(vt, i));
}
}
void free_func_table(FuncTable* vt) {
for(int i = 0; i < vt->size; i++) {
free(vt->t[i].param_types);
}
free(vt);
}