-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuildsymboltable.c
467 lines (415 loc) · 13.1 KB
/
buildsymboltable.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "data.h"
#include "auxiliary.h"
static Type *processNode(AstNode*);
static void processNodes(AstNode *, int);
static void undeclaredError(AstNode *);
static void updateSymbolId(AstNode *, Symbol *);
static void handleStructLocation(AstNode *);
static void handleDefineFunction(AstNode *function);
static void handleDefineTask(AstNode *function);
static void handleMessageIdentifier(AstNode *function);
static void handleReceiveCase(AstNode *node);
static void handleAssignment(AstNode *node);
static int errors;
static Symbol *currentfunc; /* the symbol of the current function */
int buildSymbolTable(AstNode *tree){
initializeSymbolTables();
processNode(tree);
if(errors)
printf("BuildSymbolTable failed with %d errors\n", errors);
return errors;
}
/* process node CAN return a type if it makes sense */
static Type *processNode(AstNode *node){
int scopeopened = 0;
Symbol *sym;
Type *type = NULL; /* the return value of this function */
Type *vartype;
/* Break recursion */
if(node == NULL)
return NULL;
/* Normally each node is handled by a case, and it's children are then handled after the switch by recursive calls to processNode.
Some cases are special, since they handle their children on their own, and therefore returns before the
recursive call. */
switch(node->tag){
case DefineFunction:
handleDefineFunction(node);
return NULL;
case DefineTask:
handleDefineTask(node);
return NULL;
case DefineStruct:
errors += insertSymbol(node->node.DefineStruct.identifier, 0);
sym = retrieveSymbol(node->node.DefineStruct.identifier);
openScope();
sym->type = mkStructTypeDescriptor(sym->name, getCurrentSymbolTable());
scopeopened = 1;
break;
case MessageIdentifier:
handleMessageIdentifier(node);
return NULL;
case StructMember:
vartype = processNode(node->node.StructMember.type);
errors += insertSymbol(node->node.StructMember.identifier, vartype);
break;
case Parameter:
vartype = processNode(node->node.Parameter.type);
errors += insertSymbol(node->node.Parameter.identifier, vartype);
sym = retrieveSymbol(node->node.Parameter.identifier);
setInitialized(sym->initInfo, sym->type);
break;
case BuiltinType:
return mkBuiltinTypeDescriptor(node->node.BuiltinType.type);
case StructType:
sym = retrieveSymbol(node->node.StructType.identifier);
if(sym == NULL)
undeclaredError(node->node.StructType.identifier);
else{
updateSymbolId(node->node.StructType.identifier, sym);
type = sym->type;
}
break;
case ArrayType:
vartype = processNode(node->node.ArrayType.elementType);
return mkArrayTypeDescriptor(vartype, node->node.ArrayType.dimensions);
case While:
case For: /* fallthrough */
case Switch: /* fallthrough */
case SwitchCase: /* fallthrough */
case Else: /* fallthrough */
case Receive: /* fallthrough */
openScope();
scopeopened = 1;
break;
case ReceiveCase:
if(node->node.ReceiveCase.messageName != NULL)
handleReceiveCase(node);
else{
openScope();
processNodes(node->node.ReceiveCase.statements, 0);
closeScope();
}
return NULL;
case If:
openScope();
processNodes(node->node.If.expression, 0);
processNodes(node->node.If.statements, 0);
closeScope();
processNode(node->node.If.elsePart);
return NULL; /* special case */
case ElseIf:
openScope();
processNodes(node->node.ElseIf.expression, 0);
processNodes(node->node.ElseIf.statements, 0);
closeScope();
processNode(node->node.ElseIf.elsePart);
return NULL; /* special case */
case VarDecl:
vartype = processNode(node->node.VarDecl.type);
processNode(node->node.VarDecl.expression);
errors += insertSymbol(node->node.VarDecl.identifier, vartype);
sym = retrieveSymbol(node->node.VarDecl.identifier);
sym->globalvar = node->node.VarDecl.toplevel;
if(node->node.VarDecl.expression != NULL)
setInitialized(sym->initInfo, sym->type);
return NULL; /* special case */
case VariableLocation:
sym = retrieveSymbol(node->node.VariableLocation.identifier);
if(sym == NULL)
undeclaredError(node->node.VariableLocation.identifier);
else
updateSymbolId(node->node.VariableLocation.identifier, sym);
break;
case StructLocation:
handleStructLocation(node);
return NULL;
case ArrayLocation:
sym = retrieveSymbol(node->node.ArrayLocation.identifier);
if(sym == NULL)
undeclaredError(node->node.ArrayLocation.identifier);
else
updateSymbolId(node->node.ArrayLocation.identifier, sym);
break;
case FunctionCall:
sym = retrieveSymbol(node->node.FunctionCall.identifier);
if(sym == NULL)
undeclaredError(node->node.FunctionCall.identifier);
else
updateSymbolId(node->node.FunctionCall.identifier, sym);
break;
case Identifier:
sym = retrieveSymbol(node);
if(sym == NULL)
undeclaredError(node);
else
updateSymbolId(node, sym);
break;
case MessageLiteral:
sym = retrieveSymbol(node->node.MessageLiteral.identifier);
if(sym == NULL)
undeclaredError(node->node.MessageLiteral.identifier);
else
updateSymbolId(node->node.MessageLiteral.identifier, sym);
break;
case Return:
node->node.Return.functionsym = currentfunc;
break;
case Spawn:
sym = retrieveSymbol(node->node.Spawn.identifier);
if(sym == NULL)
undeclaredError(node->node.Spawn.identifier);
else
updateSymbolId(node->node.Spawn.identifier, sym);
break;
case Assignment:
handleAssignment(node);
return NULL;
}
processNodes(node->children, 1);
if(scopeopened)
closeScope();
return type;
}
static void processNodes(AstNode *nodes, int usechain){
/* Next is used for lists and
chains are used to chain the next sibling such as
Chain = function -> identifier -> int a -> int b -> void -> code */
AstNode *child = nodes;
while(child != NULL){
processNode(child);
if(usechain)
child = child->chain;
else
child = child->next;
}
}
static void undeclaredError(AstNode *node){
char *name = node->node.Identifier.identifier;
errors++;
eprintf(node->linenum, "Undeclared symbol \"%s\"\n", name);
}
static void updateSymbolId(AstNode *node, Symbol *s){
node->node.Identifier.symbol = s;
}
static void handleStructLocation(AstNode *node){
AstNode *n = node;
AstNode *next = n;
/* default is the current table */
SymbolTable *table = NULL;
while(next != NULL){
AstNode *identifier;
Symbol *sym;
switch(n->tag){
case StructLocation:
identifier = n->node.StructLocation.identifier;
next = n->node.StructLocation.location;
break;
case VariableLocation:
identifier = n->node.VariableLocation.identifier;
next = NULL;
break;
case ArrayLocation:
identifier = n->node.ArrayLocation.identifier;
next = NULL;
processNodes(n->node.ArrayLocation.indicies, 0);
break;
}
if(table == NULL)
sym = retrieveSymbol(identifier);
else
sym = retrieveSymbolFromTable(identifier, table);
if(sym == NULL){
undeclaredError(identifier);
return;
}else{
updateSymbolId(identifier, sym);
if(n->tag == StructLocation)
table = sym->type->tags.typeStruct.fields;
n = next;
}
}
}
static void handleDefineFunction(AstNode *function){
AstNode *tmp;
Type **ParaTypes;
Symbol *sym;
int i = 0, parameterLength;
errors += insertSymbol(function->node.DefineFunction.identifier, NULL);
sym = retrieveSymbol(function->node.DefineFunction.identifier);
currentfunc = sym;
openScope();
processNodes(function->node.DefineFunction.parameters, 0);
processNodes(function->node.DefineFunction.statements, 0);
parameterLength = nodeLength(function->node.DefineFunction.parameters);
ParaTypes = malloc(sizeof(Type*)*parameterLength);
for(tmp = function->node.DefineFunction.parameters; tmp != NULL; tmp=tmp->next){
AstNode *identifier = tmp->node.Parameter.identifier;
ParaTypes[i] = identifier->node.Identifier.symbol->type;
i++;
}
sym->type = mkFunctionTypeDescriptor(parameterLength, ParaTypes, processNode(function->node.DefineFunction.type));;
closeScope();
currentfunc = NULL;
}
static void handleDefineTask(AstNode *task){
/* Copy pasted from handleDefineFunction. */
AstNode *tmp;
Type **ParaTypes;
Type *typeDescriptor;
Symbol *sym;
int i = 0, parameterLength;
errors += insertSymbol(task->node.DefineTask.identifier, NULL);
openScope();
processNodes(task->node.DefineTask.parameters, 0);
processNodes(task->node.DefineTask.statements, 0);
parameterLength = nodeLength(task->node.DefineTask.parameters);
ParaTypes = malloc(sizeof(Type*)*parameterLength);
for(tmp = task->node.DefineTask.parameters; tmp != NULL; tmp=tmp->next){
AstNode *identifier = tmp->node.Parameter.identifier;
ParaTypes[i] = identifier->node.Identifier.symbol->type;
i++;
}
typeDescriptor = mkTaskTypeDescriptor(parameterLength, ParaTypes);
closeScope();
sym = retrieveSymbol(task->node.DefineTask.identifier);
sym->type = typeDescriptor;
sym->first = task;
}
static void handleMessageIdentifier(AstNode *messageIdentifier){
/* Copy pasted from handleDefineTask. */
AstNode *tmp;
Type **ParaTypes;
Type *typeDescriptor;
Symbol *sym;
int i = 0, parameterLength;
errors += insertSymbol(messageIdentifier->node.MessageIdentifier.identifier, NULL);
openScope();
processNodes(messageIdentifier->node.MessageIdentifier.parameters, 0);
parameterLength = nodeLength(messageIdentifier->node.MessageIdentifier.parameters);
ParaTypes = malloc(sizeof(Type*)*parameterLength);
for(tmp = messageIdentifier->node.MessageIdentifier.parameters; tmp != NULL; tmp=tmp->next){
AstNode *identifier = tmp->node.Parameter.identifier;
ParaTypes[i] = identifier->node.Identifier.symbol->type;
i++;
}
typeDescriptor = mkMessageTypeDescriptor(parameterLength, ParaTypes);
closeScope();
sym = retrieveSymbol(messageIdentifier->node.MessageIdentifier.identifier);
sym->type = typeDescriptor;
sym->first = messageIdentifier;
}
static void handleReceiveCase(AstNode *node){
Symbol *sym = retrieveSymbol(node->node.ReceiveCase.messageName);
AstNode *arg;
Type **params = NULL;
int paramnr = 0;
int paramcount = 0;
if(sym == NULL){
undeclaredError(node->node.ReceiveCase.messageName);
return;
}else
updateSymbolId(node->node.ReceiveCase.messageName, sym);
if(sym->type->tag == MessageTypeTag){
params = sym->type->tags.typeMessage.parameterTypes;
paramcount = sym->type->tags.typeMessage.arity;
}
openScope();
for(arg = node->node.ReceiveCase.dataNames;
arg != NULL && paramnr < paramcount;
arg = arg->next, paramnr++)
{
Symbol *tmpsym;
errors += insertSymbol(arg, params[paramnr]);
tmpsym = retrieveSymbol(arg);
setInitialized(tmpsym->initInfo, tmpsym->type);
}
if(paramnr != paramcount || arg != NULL){
errors++;
eprintf(node->linenum,"Arity of message in receive case does not match message prototype for %s (should be %d but is %d)\n", sym->name, paramcount, nodeLength(node->node.ReceiveCase.dataNames));
}
processNodes(node->node.ReceiveCase.statements, 0);
closeScope();
}
/* Check if a symbol appears inside the tree */
static int containsSymbol(AstNode *tree, Symbol *sym){
AstNode *n;
for(n = tree; n != NULL; n = n->chain){
if(n->tag == Identifier && n->node.Identifier.symbol == sym)
return 1;
if(containsSymbol(n->children, sym))
return 1;
}
return 0;
}
static void handleAssignment(AstNode *node){
AstNode *loc = node->node.Assignment.location;
AstNode *expr = node->node.Assignment.expression;
AstNode *id = NULL;
Symbol *sym;
Type *type;
Type *elementType;
AstNode *dims;
InitializeInfo *initInfo;
processNode(expr);
processNode(loc);
switch(loc->tag){
case VariableLocation:
id = loc->node.VariableLocation.identifier;
sym = id->node.Identifier.symbol;
if(containsSymbol(expr, sym))
return;
setInitialized(sym->initInfo, sym->type);
break;
case ArrayLocation:
id = loc->node.ArrayLocation.identifier;
sym = id->node.Identifier.symbol;
if(containsSymbol(expr, sym))
return;
initInfo = sym->initInfo;
type = sym->type;
elementType = type->tags.typeArray.elementType;
dims = type->tags.typeArray.dimensions;
loc = loc->node.ArrayLocation.indicies;
while(loc != NULL && loc->tag == IntLiteral){
initInfo = initInfo->arrayInitialized[loc->node.IntLiteral.value];
type = mkArrayTypeDescriptor(elementType, dims->next);
loc = loc->next;
dims = dims->next;
}
if(loc == NULL){
if(dims == NULL)
setInitialized(initInfo, elementType);
else
setInitialized(initInfo, type);
}
break;
case StructLocation:
id = loc->node.StructLocation.identifier;
initInfo = id->node.Identifier.symbol->initInfo;
loc = loc->node.StructLocation.location;
while(loc->tag == StructLocation){
StructInitializeInfo *sinfo;
id = loc->node.StructLocation.identifier;
for(sinfo = initInfo->structInitialized; sinfo != NULL; sinfo = sinfo->next){
char *fieldname = sinfo->symbol->name;
char *idname = id->node.Identifier.symbol->name;
if(strcmp(fieldname, idname) == 0){
initInfo = sinfo->info;
break;
}
}
loc = loc->node.StructLocation.location;
}
if(loc->tag == VariableLocation)
id = loc->node.VariableLocation.identifier;
else if(loc->tag == ArrayLocation)
id = loc->node.ArrayLocation.identifier;
if(containsSymbol(expr, id->node.Identifier.symbol))
return;
setInitializedStructField(initInfo, id->node.Identifier.symbol->name);
break;
}
}