-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymbol_Tables.c
435 lines (365 loc) · 12.5 KB
/
Symbol_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
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
/*
* Symbol_Tables.c
*
* Created on: Dec 29, 2017
* Author: Josip
*/
#include "System.h"
#include "Symbol_Tables.h"
uint8_t SymbolCategoryString[SYMBOL_CATEGORY_NUM][MAX_SYMBOL_NAME_LENGHT] =
{
"BUILT-IN TYPE",
"VARIABLE",
"COMPOUND",
"PARAMETER"
};
s_symbol_symTable_link Symbol_Table_HeadLink;
s_symbol_symbol** Symbol_Table_CreateHash(uint16_t HashSize)
{
s_symbol_symbol** HashPtr;
/* Init */
HashPtr = NULL;
/* Allocate Hash Table */
HashPtr = (s_symbol_symbol**) malloc(sizeof(s_symbol_symbol*) * HashSize);
/* Init to NULL pointers */
memset(HashPtr, 0, sizeof(s_symbol_symbol*) * HashSize);
return HashPtr;
}
s_symbol_symbol_table* Symbol_Table_CreateTable(uint8_t* name, uint16_t hashSize)
{
s_symbol_symbol_table* SymbolTable;
SymbolTable = (s_symbol_symbol_table*) malloc(sizeof(s_symbol_symbol_table));
/* Init */
memset(SymbolTable->scope, 0, MAX_SYMBOL_NAME_LENGHT);
strcpy( (char*)SymbolTable->scope, (const char*)name );
SymbolTable->hash_info.hashSize = hashSize;
/* Create Hash */
SymbolTable->symbolHash = Symbol_Table_CreateHash( SymbolTable->hash_info.hashSize );
/* Debug Printout */
System_Print(" \n\tCreated Symbol Table: %s\n", name);
return SymbolTable;
}
s_symbol_symbol_table* Symbol_Table_GetTable( uint8_t* name )
{
s_symbol_symbol_table* SymbolTable;
s_symbol_symTable_link* TempSymbolTableLinkPtr;
/* Init */
SymbolTable = NULL;
TempSymbolTableLinkPtr = &Symbol_Table_HeadLink;
do
{
/* Check scope */
if(strcmp((const char*)TempSymbolTableLinkPtr->symbolTable->scope, (const char*)name) == 0)
{
/* Symbol table found */
SymbolTable = TempSymbolTableLinkPtr->symbolTable;
break;
}
TempSymbolTableLinkPtr = TempSymbolTableLinkPtr->next_symbol_table;
}while( TempSymbolTableLinkPtr != NULL );
return SymbolTable;
}
uint16_t Symbol_Table_Hash(uint8_t* key, uint16_t hashSize)
{
uint16_t Index;
uint16_t Sum;
uint16_t Lenght;
uint8_t Idx;
/* Init */
Index = 0;
Sum = 0;
Lenght = strlen((const char*)key);
/* Hash Function */
for( Idx = 0; Idx < Lenght; Idx++ )
{
Sum += *key;
key++;
}
Index = Sum % hashSize;
return Index;
}
void Symbol_Table_InsertSymbol( s_symbol_symbol* symbol, s_symbol_symbol_table* symbol_table )
{
uint16_t Index;
uint16_t Idx;
s_symbol_symbol** TempSymbolPtr;
enum e_symbol_table_insertSymbol
{
INSERTED,
REDECLARED,
HASH_FULL
} Status;
/* Init */
TempSymbolPtr = NULL;
/* Find hash index */
Index = Symbol_Table_Hash(symbol->name, symbol_table->hash_info.hashSize);
/* Try to insert the symbol into symbol table */
TempSymbolPtr = ((s_symbol_symbol**)((uint32_t*)symbol_table->symbolHash + Index));
if( *TempSymbolPtr == NULL )
{
*TempSymbolPtr = symbol;
Status = INSERTED;
}
else if( strcmp((const char*)(*TempSymbolPtr)->name, (const char*)symbol->name) == 0 )
{
/* Found Variable already declared */
Status = REDECLARED;
}
else
{
/* Find the first free slot or if the variable is already declared */
for(Idx = Index + 1; Idx < Index + symbol_table->hash_info.hashSize; Idx++)
{
TempSymbolPtr = ((s_symbol_symbol**)((uint32_t*)symbol_table->symbolHash + (Idx % symbol_table->hash_info.hashSize)));
if(*TempSymbolPtr == NULL)
{
/* Found Free Slot */
*TempSymbolPtr = symbol;
Status = INSERTED;
break;
}
else if(strcmp((const char*)(*TempSymbolPtr)->name, (const char*)symbol->name) == 0)
{
/* Found Variable already declared */
Status = REDECLARED;
}
}
/* Check if there was no space */
if(Idx == Index + symbol_table->hash_info.hashSize)
{
Status = HASH_FULL;
}
}
/* Debug Printout */
switch(Status)
{
case INSERTED:
System_Print("\t\tINSERT: %s \t->\t %s\n", symbol->name, symbol_table->scope);
break;
case REDECLARED:
System_Print("Semantic Error: Symbol %s redeclared in Symbol Table %s!\n", symbol->name, symbol_table->scope);
exit(1);
break;
case HASH_FULL:
System_Print("Semantic Error: Hash Table to small!\n");
exit(1);
break;
}
}
s_symbol_symbol* Symbol_Table_GetSymbol( uint8_t* name, s_symbol_symbol_table* symbol_table )
{
uint16_t Index;
uint16_t Idx;
s_symbol_symbol* Symbol;
enum e_symbol_table_insertSymbol
{
FOUND,
NOT_FOUND
} Status;
/* Debug Printout */
System_Print("\t\tLOOKUP: %s \t->\t %s\n", name, symbol_table->scope);
/* Init */
Symbol = NULL;
/* Try and find the symbol in local/current scope */
/* Hash the name */
Index = Symbol_Table_Hash( name, symbol_table->hash_info.hashSize );
/* Find if the symbol exists */
for(Idx = Index; Idx < Index + symbol_table->hash_info.hashSize; Idx++)
{
Symbol = *((s_symbol_symbol**)((uint32_t*)symbol_table->symbolHash + (Idx % symbol_table->hash_info.hashSize)));
if( Symbol != NULL )
{
/* Check if the symbol is correct */
if( strcmp((const char*)name, (const char*)Symbol->name) == 0 )
{
/* Symbol found */
Status = FOUND;
break;
}
}
else
{
/* Symbol Not Found */
Status = NOT_FOUND;
break;
}
}
if(Idx == Index + symbol_table->hash_info.hashSize)
{
/* Symbol Not Found */
Status = NOT_FOUND;
}
/* Try and find the Variable in global scope */
if( Status == NOT_FOUND && symbol_table != Symbol_Table_HeadLink.symbolTable )
{
/* ReInit */
symbol_table = Symbol_Table_HeadLink.symbolTable;
Symbol = NULL;
/* Call Get Symbol for global scope */
Symbol = Symbol_Table_GetSymbol( name, symbol_table );
}
return Symbol;
}
s_symbol_symbol* Symbol_Table_CreateSymbol( e_symbol_category category, uint8_t* name, s_symbol_symbol* type )
{
s_symbol_symbol* Symbol;
Symbol = (s_symbol_symbol*) malloc(sizeof(s_symbol_symbol));
/* Init */
Symbol->category = category;
memset(Symbol->name, 0, MAX_SYMBOL_NAME_LENGHT);
strcpy( (char*)Symbol->name, (const char*)name );
switch(category)
{
case SYMBOL_BUILTIN_TYPE:
/* Init Built-In Type */
Symbol->type = (e_symbol_builtin_types*) malloc(sizeof(e_symbol_builtin_types));
if(strcmp((const char*)name, (const char*)"int8") == 0)
{
*(e_symbol_builtin_types*)(Symbol->type) = BIT_INT8;
}
else if(strcmp((const char*)name, (const char*)"int16") == 0)
{
*(e_symbol_builtin_types*)(Symbol->type) = BIT_INT16;
}
else if(strcmp((const char*)name, (const char*)"int32") == 0)
{
*(e_symbol_builtin_types*)(Symbol->type) = BIT_INT32;
}
else if(strcmp((const char*)name, (const char*)"uint8") == 0)
{
*(e_symbol_builtin_types*)(Symbol->type) = BIT_UINT8;
}
else if(strcmp((const char*)name, (const char*)"uint16") == 0)
{
*(e_symbol_builtin_types*)(Symbol->type) = BIT_UINT16;
}
else if(strcmp((const char*)name, (const char*)"uint32") == 0)
{
*(e_symbol_builtin_types*)(Symbol->type) = BIT_UINT32;
}
else if(strcmp((const char*)name, (const char*)"float") == 0)
{
*(e_symbol_builtin_types*)(Symbol->type) = BIT_FLOAT;
}
Symbol->value = NULL;
Symbol->info = NULL;
break;
case SYMBOL_VARIABLE:
/* Init Variable */
Symbol->type = type;
Symbol->value = NULL;
Symbol->info = NULL;
break;
case SYMBOL_COMPOUND:
/* Init Compound */
Symbol->type = type;
Symbol->value = NULL;
/* Fill compound specific info */
Symbol->info = malloc(sizeof(s_symbol_compoundStatementInfo));
((s_symbol_compoundStatementInfo*)Symbol->info)->parameterNum = 0;
((s_symbol_compoundStatementInfo*)Symbol->info)->compoundNode = NULL;
((s_symbol_compoundStatementInfo*)Symbol->info)->returnNode = NULL;
((s_symbol_compoundStatementInfo*)Symbol->info)->returnExecuted = FALSE;
((s_symbol_compoundStatementInfo*)Symbol->info)->breakExecuted = FALSE;
break;
case SYMBOL_PARAMETER:
/* Init Parameter */
Symbol->type = type;
Symbol->value = NULL;
break;
case SYMBOL_CATEGORY_NUM:
break;
}
return Symbol;
}
void Symbol_Table_MemAllocVar(s_symbol_symbol_table* symbol_table)
{
uint16_t Idx;
s_symbol_symbol* TempSymbolPtr;
/* Allocate memory for all variables in symbol_table */
for(Idx = 0; Idx < symbol_table->hash_info.hashSize; Idx++)
{
TempSymbolPtr = *((s_symbol_symbol**)((uint32_t*)symbol_table->symbolHash + Idx));
if( TempSymbolPtr != NULL )
{
if( TempSymbolPtr->category != SYMBOL_BUILTIN_TYPE )
{
TempSymbolPtr->value = calloc(1,sizeof(uint32_t));
}
}
}
}
void Symbol_Table_MemFreeVar(s_symbol_symbol_table* symbol_table)
{
uint16_t Idx;
s_symbol_symbol* TempSymbolPtr;
/* Free memory for all variables in symbol_table */
for(Idx = 0; Idx < symbol_table->hash_info.hashSize; Idx++)
{
TempSymbolPtr = *((s_symbol_symbol**)((uint32_t*)symbol_table->symbolHash + Idx));
if( TempSymbolPtr != NULL )
{
if( TempSymbolPtr->category == SYMBOL_VARIABLE )
{
free(TempSymbolPtr->value);
}
}
}
}
void Symbol_Table_PrintoutSymbolTable(s_symbol_symbol_table* symbol_table)
{
uint16_t Idx;
s_symbol_symbol* TempSymbolPtr;
/* Print Scope */
System_Print(" \n \n");
System_Print("\t\t\t\t\tSymbol Table: %s\n", symbol_table->scope);
System_Print("\t-------------------------------------------------------------------------------------------\n");
System_Print("\t\tCategory\t|\tName\t|\tType\t|\tValue\t|\tIndex\n");
System_Print("\t-------------------------------------------------------------------------------------------\n");
/* Print Symbol Entries */
for(Idx = 0; Idx < symbol_table->hash_info.hashSize; Idx++)
{
TempSymbolPtr = *((s_symbol_symbol**)((uint32_t*)symbol_table->symbolHash + Idx));
if( TempSymbolPtr != NULL )
{
if( TempSymbolPtr->category == SYMBOL_BUILTIN_TYPE )
{
System_Print("\t\t%s\t|\t%s\t|\tn/a\t|\tNULL\t|\t%d\n", SymbolCategoryString[TempSymbolPtr->category], TempSymbolPtr->name, Idx);
}
else
{
if( TempSymbolPtr->value == NULL )
{
System_Print("\t\t%s\t|\t%s\t|\t%s\t|\t%s\t|\t%d\n"
, SymbolCategoryString[TempSymbolPtr->category]
, TempSymbolPtr->name
, ((s_symbol_symbol*)(TempSymbolPtr->type))->name
, "NULL"
, Idx
);
}
else
{
System_Print("\t\t%s\t|\t%s\t|\t%s\t|\t%d\t|\t%d\n"
, SymbolCategoryString[TempSymbolPtr->category]
, TempSymbolPtr->name
, ((s_symbol_symbol*)(TempSymbolPtr->type))->name
, *(uint32_t*)TempSymbolPtr->value
, Idx
);
}
}
System_Print("\t-------------------------------------------------------------------------------------------\n");
}
}
}
void Symbol_Table_PrintoutSymbolTableAll()
{
s_symbol_symTable_link* TempSymbolTablePtr;
TempSymbolTablePtr = &Symbol_Table_HeadLink;
while(TempSymbolTablePtr != NULL)
{
Symbol_Table_PrintoutSymbolTable( TempSymbolTablePtr->symbolTable );
TempSymbolTablePtr = TempSymbolTablePtr->next_symbol_table;
}
}