-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymtable.c
442 lines (407 loc) · 13.2 KB
/
symtable.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/**
* @file symtable.c
* @author Jan Brudný ([email protected])
* @author Jakub Vlk ([email protected])
* @author Antonín Jarolím ([email protected])
* @brief Tabulka symbolů
* Implementation IFJ22 compiler
*
* The implementation used the hash table solution from the 2nd homework of IAL 2021/Z
*/
#include "symtable.h"
// region DTList
DTList_T *createDTL(int count, ...) {
DTList_T *DTL = (DTList_T *) malloc(sizeof(DTList_T));
initDTList(DTL);
if (count == 0)
return DTL;
va_list args;
va_start(args, count);
for (int i = 0; i < count; ++i) {
insDTList(DTL, va_arg(args, symbolDataType_t), NULL);
}
va_end(args);
return DTL;
}
/**
* @brief vkládá prvek na konec seznamu
*
* @param list DTL seznam
* @param typ typ symbolu
*/
void insDTList(DTList_T *list, symbolDataType_t typ, char* identifier) {
DTListMem_T *actualMem = list->first;
DTListMem_T *newMember = malloc(sizeof(DTListMem_T));
checkNullPointer(newMember);
newMember->type = typ;
newMember->next = NULL;
newMember->identifier = identifier;
if (list->first != NULL) {
while (actualMem->next != NULL)
actualMem = actualMem->next;
actualMem->next = newMember;
} else {
list->first = newMember;
}
list->len++;
}
/**
* @brief inicializuje nový seznam
*
* @param list odkaz na nový seznam
*/
void initDTList(DTList_T *list) {
list->first = NULL;
list->len = 0;
}
// endregion
// region HashTable
// Return 64-bit FNV-1a hash for key (NUL-terminated). See description:
// https://en.wikipedia.org/wiki/Fowler–Noll–Vo_hash_function
size_t ht_get_hash(const char *key) {
uint64_t hash = FNV_OFFSET;
for (const char *p = key; *p; p++) {
hash ^= (uint64_t) (unsigned char) (*p);
hash *= FNV_PRIME;
}
return (size_t) (hash & (uint64_t) (MAX_HTSIZE - 1));
}
void htInit(htTable_t *table) {
for (int i = 0; i < MAX_HTSIZE; ++i) {
(*table)[i] = NULL;
}
}
htItem_t *htSearch(htTable_t *table, const char *key) {
htItem_t *item = (*table)[ht_get_hash(key)];
while (item != NULL) {
if (strcmp(item->key, key) == 0) {
return item;
}
item = item->next;
}
return NULL;
}
void htInsertItem(htTable_t *table, const char *key, symbol_t value) {
htItem_t *item = htSearch(table, key);
if (item != NULL) {
if (debug) printHashtable(table, "Original");
item->value = value;
loging("Symbol %s already exists, overwriting", key);
if (debug) printHashtable(table, "Overwritten");
return;
}
size_t hash = ht_get_hash(key);
item = malloc(sizeof(struct htItem));
if (item == NULL) {
fprintf(stderr, "Malloc fail in htInsertItem, key '%s'.\n", key);
return;
}
item->value = value;
item->key = key;
item->next = (*table)[hash];
(*table)[hash] = item;
}
symbol_t *htGetItem(htTable_t *table, const char *key) {
htItem_t *item = htSearch(table, key);
if (item == NULL) {
return NULL;
}
return &item->value;
}
void htDeleteItem(htTable_t *table, const char *key) {
htItem_t *item = (*table)[ht_get_hash(key)];
htItem_t *prevItem = NULL;
while (item != NULL) {
if (strcmp(item->key, key) == 0) {
if (prevItem != NULL) {
prevItem->next = item->next;
}
free(item);
return;
}
prevItem = item;
item = item->next;
}
return;
}
void htDestroy(htTable_t *table) {
for (int i = 0; i < MAX_HTSIZE; ++i) {
htItem_t *item = (*table)[i];
htItem_t *prevItem = NULL;
while (item != NULL) {
prevItem = item;
item = item->next;
free(prevItem);
}
(*table)[i] = NULL;
}
}
// endregion
// region SymTable
symbol_t createSymbolVarLit(const char *name,
symbolType_t type,
symbolDataType_t dataType,
token_t token) {
symbol_t newSymbol;
newSymbol.identifier = name;
newSymbol.type = type;
newSymbol.dataType = dataType;
newSymbol.token = token;
newSymbol.firstParam = NULL;
newSymbol.returnType = undefinedDataType;
return newSymbol;
}
symbol_t *createSymbolFunction(const char *name,
symbolType_t type,
DTList_T *paramList,
symbolDataType_t returnType) {
symbol_t *newSymbol = (symbol_t *) malloc(sizeof(symbol_t));
if (newSymbol == NULL) {
InternalError("Malloc failed.");
}
newSymbol->type = type;
newSymbol->dataType = undefinedDataType;
newSymbol->identifier = name;
newSymbol->returnType = returnType;
DTList_T *newPList = createDTL(0);
if (paramList != NULL) {
DTListMem_T *tmp = paramList->first;
while (tmp != NULL) {
insDTList(newPList, tmp->type, NULL);
tmp = tmp->next;
}
}
newSymbol->firstParam = newPList;
return newSymbol;
}
void saveBuildInFunctions(symtable_t *symtable) {
symInsertFunction(symtable, *createSymbolFunction("reads", function,
createDTL(0),
stringNullable));
symInsertFunction(symtable, *createSymbolFunction("readi", function,
createDTL(0),
integerNullable));
symInsertFunction(symtable, *createSymbolFunction("readf", function,
createDTL(0),
floatingNullable));
symInsertFunction(symtable, *createSymbolFunction("write", function,
createDTL(0),
undefinedDataType));
symInsertFunction(symtable, *createSymbolFunction("intval", function,
createDTL(1, floatingNullable),
integer));
symInsertFunction(symtable, *createSymbolFunction("floatval", function,
createDTL(1, integerNullable),
floating));
symInsertFunction(symtable, *createSymbolFunction("strval", function,
createDTL(1, stringNullable),
string));
symInsertFunction(symtable, *createSymbolFunction("substring", function,
createDTL(3, string, integer, integer),
stringNullable));
symInsertFunction(symtable, *createSymbolFunction("strlen", function,
createDTL(1, string),
integer));
symInsertFunction(symtable, *createSymbolFunction("ord", function,
createDTL(1, string),
integer));
symInsertFunction(symtable, *createSymbolFunction("chr", function,
createDTL(1, integer),
string));
}
void symInit(symtable_t *symtable) {
symtable->current = symtable->main;
symtable->isInFunction = false;
htInit(&(symtable->functions));
for (int i = 0; i < MAX_SYMTABLES; i++) {
htInit(&(symtable->main[i]));
htInit(&(symtable->infunc[i]));
}
symtable->last = 0;
symtable->lastMain = 0;
saveBuildInFunctions(symtable);
}
void symDestroy(symtable_t *symtable) {
htDestroy(&(symtable->functions));
for (int i = 0; i < MAX_SYMTABLES; i++) {
htDestroy(&(symtable->main[i]));
htDestroy(&(symtable->infunc[i]));
}
symtable->last = -1;
}
void symNewLocal(symtable_t *symtable) {
if (symtable->last == MAX_SYMTABLES - 2) {
fprintf(stderr, "ERROR: max number of local symtables exeeded.\n");
return;
}
symtable->last++;
}
bool symDelLocal(symtable_t *symtable) {
htDestroy(&(symtable->current[symtable->last]));
if (symtable->last == 0) {
if (symtable->isInFunction) {
loging("INFO: Switching back to main symtable.");
printSymtable(symtable);
symSwitchBack(symtable);
return true;
}
InternalError("ERROR: attempted to delete non existent symtable");
return false;
}
symtable->last--;
return false;
}
void symInsert(symtable_t *symtable, symbol_t symbol) {
symbol.symtablePos = -1;
htInsertItem(&(symtable->current[symtable->last]), symbol.identifier, symbol);
}
void symInsertFunction(symtable_t *symtable, symbol_t symbol) {
htInsertItem(&(symtable->functions), symbol.identifier, symbol);
}
symbol_t *symSearchVar(symtable_t *symtable, const char *identifier) {
htItem_t *found;
for (int i = 0; i <= symtable->last; i++) {
found = htSearch(&(symtable->current[i]), identifier);
if (found) {
found->value.symtablePos = symtable->last - i;
return &(found->value);
}
}
return NULL;
}
symbol_t *symSearchFunc(symtable_t *symtable, const char *identifier) {
htItem_t *found;
found = htSearch(&(symtable->functions), identifier);
if (found) {
return &(found->value);
}
return NULL;
}
void symSwitch(symtable_t *symtable) {
if (symtable->isInFunction) {
InternalError("Attempted to switch to infunc while in infunc.");
}
if (symtable->last > 0) {
InternalError("Attempted to switch to infunc while in a local symtable.");
}
symtable->current = symtable->infunc;
symtable->lastMain = symtable->last;
symtable->last = 0;
symtable->isInFunction = true;
return;
}
void symSwitchBack(symtable_t *symtable) {
if (!symtable->isInFunction) {
InternalError("Attempted to switch to main while in main.");
}
symtable->current = symtable->main;
symtable->last = symtable->lastMain;
symtable->lastMain = 0;
symtable->isInFunction = false;
return;
}
void printHashtable(htTable_t *table, const char *index) {
htItem_t *item;
bool hashTableNotEmpty = false;
for (int i = 0; i < MAX_HTSIZE; ++i) {
bool printLineEnd = false;
item = (*table)[i];
while (item != NULL) {
if (!hashTableNotEmpty) {
hashTableNotEmpty = true;
printlog("---- HT %s ----\n", index);
}
printSymbol(&(item->value));
printLineEnd = true;
item = item->next;
}
if (printLineEnd) {
printlog("%s", "\n");
}
}
}
void printSymbol(symbol_t *symbol) {
if (!symbol) {
printlog("(NULL)");
return;
}
char *dataType;
char *type;
switch (symbol->type) {
case function:
type = "function";
break;
case variable:
type = "variable";
break;
case literal:
type = "literal";
break;
case undefinedType:
type = "undefinedType";
break;
default:
InternalError("Type of symbol (%d) is unknown", symbol->type);
}
switch (symbol->dataType) {
case string:
dataType = "string";
break;
case stringNullable:
dataType = "?string";
break;
case floating:
dataType = "floating";
break;
case floatingNullable:
dataType = "?floating";
break;
case integer:
dataType = "integer";
break;
case integerNullable:
dataType = "?integer";
break;
case nil:
dataType = "nil";
break;
case undefinedDataType:
dataType = "undefined";
break;
default:
InternalError("DataType of symbol (%d) is unknown", symbol->dataType);
break;
}
printlog("(%s, %s, %s, %d:%d)",
symbol->identifier,
type,
dataType,
symbol->token.rowNumber,
symbol->token.rowPosNumber);
}
void printSymtable(symtable_t *symtable) {
if (!debug) { return; }
printHashtable(&(symtable->functions), "functions");
for (int i = 0; i < MAX_SYMTABLES; i++) {
char number[128];
sprintf(number, "MAINBODY %d", i);
printHashtable(&(symtable->main[i]), number);
}
if (symtable->isInFunction) {
for (int i = 0; i < MAX_SYMTABLES; i++) {
char number[128];
sprintf(number, "INFUNC %d", i);
printHashtable(&(symtable->infunc[i]), number);
}
}
}
size_t countDtList(DTList_T *list) {
DTListMem_T *param = list->first;
size_t count = 0;
while (param != NULL) {
count++;
param = param->next;
}
return count;
}
// endregion