-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemantic_analysis.c
More file actions
405 lines (374 loc) · 10.5 KB
/
semantic_analysis.c
File metadata and controls
405 lines (374 loc) · 10.5 KB
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
/* IFJ20 - Semantic analysis
* Authors:
* Mario Harvan, xharva03
*/
#include "semantic_analysis.h"
bool isVarDefined(htab_t *symTable, htab_key_t name)
{
if (symTable != NULL)
{
htab_iterator_t tmp = htab_find(symTable, name);
if (tmp.ptr != NULL)
{
if (tmp.ptr->isVar == true)
return true;
}
}
return false;
}
varDataType getVarType(htab_t *symTable, htab_key_t name)
{
htab_iterator_t tmp = htab_find(symTable, name);
if (tmp.ptr != NULL)
{
if (tmp.ptr->isVar == true)
return tmp.ptr->varDataType;
}
return NONE;
}
void setVarType(htab_t *symTable, htab_key_t name, varDataType varDataType)
{
htab_iterator_t tmp = htab_find(symTable, name);
if (tmp.ptr != NULL)
{
if (tmp.ptr->isVar == true)
tmp.ptr->varDataType = varDataType;
}
}
TokenValue getVarValue(htab_t *symTable, htab_key_t name)
{
TokenValue emptyVal;
emptyVal.i = 0;
htab_iterator_t tmp = htab_find(symTable, name);
if (tmp.ptr != NULL)
{
if (tmp.ptr->isVar == true)
return tmp.ptr->varValue;
}
return emptyVal;
}
void setVarValue(htab_t *symTable, htab_key_t name, TokenValue value)
{
htab_iterator_t tmp = htab_find(symTable, name);
if (tmp.ptr != NULL)
{
if (tmp.ptr->isVar == true)
tmp.ptr->varValue = value;
}
}
bool isVarConst(htab_t *symTable, htab_key_t name)
{
htab_iterator_t tmp = htab_find(symTable, name);
if (tmp.ptr != NULL)
{
if (tmp.ptr->isVar == true)
return tmp.ptr->isConst;
}
return false;
}
void setVarConst(htab_t *symTable, htab_key_t name, bool isConst)
{
htab_iterator_t tmp = htab_find(symTable, name);
if (tmp.ptr != NULL)
{
if (tmp.ptr->isVar == true)
tmp.ptr->isConst = isConst;
}
}
unsigned getVarCnt(htab_t *symTable, htab_key_t name)
{
htab_iterator_t tmp = htab_find(symTable, name);
if (tmp.ptr != NULL)
{
if (tmp.ptr->isVar == true)
return tmp.ptr->varCnt;
}
return 0;
}
varDataType checkOperationTypes(Vector *tableVector, Token var1, Token var2)
{
htab_iterator_t operator1;
htab_iterator_t operator2;
operator1.ptr = NULL;
operator2.ptr = NULL;
htab_t *tableForOp1 = getSymTableForVar(tableVector, var1.value.s.ptr);
if (tableForOp1 == NULL)
throw_error_fatal(DEFINITION_ERROR, "%s", "Variable is undefined");
operator1 = htab_find(tableForOp1, var1.value.s.ptr);
htab_t *tableForOp2 = getSymTableForVar(tableVector, var2.value.s.ptr);
if (tableForOp1 == NULL)
throw_error_fatal(DEFINITION_ERROR, "%s", "Variable is undefined");
operator2 = htab_find(tableForOp2, var2.value.s.ptr);
//check data types of both operators
if (operator1.ptr != NULL && operator2.ptr != NULL)
{
if (operator1.ptr->tokenType == TOKEN_IDENTIFIER && operator2.ptr->tokenType == TOKEN_IDENTIFIER)
{
if (operator1.ptr->varDataType == operator2.ptr->varDataType)
return operator1.ptr->varDataType;
}
}
return NONE;
}
String defineCompilerVar(htab_t *symTable, varDataType varDataType, TokenValue varValue, bool isConst)
{
//temporary var idetificator
static unsigned cnt = 0;
String varName;
string_init(&varName);
string_append_string(&varName, "Expression$");
//count length of number
int length = snprintf(NULL, 0, "%u", cnt);
char *tmpCnt = malloc(sizeof(char) * length + 1);
if (tmpCnt == NULL)
throw_error_fatal(INTERNAL_ERROR, "%s", "Memory allocation error");
sprintf(tmpCnt, "%u", cnt);
string_append_string(&varName, tmpCnt);
free(tmpCnt);
//insert tmp var into symtable
htab_iterator_t tmp = htab_insert(symTable, varName.ptr);
if (tmp.ptr != NULL)
{
tmp.ptr->tokenType = TOKEN_IDENTIFIER;
tmp.ptr->varDataType = varDataType;
tmp.ptr->isVarUsedDefined = false;
if (isConst == true)
tmp.ptr->varValue = varValue;
tmp.ptr->isConst = isConst;
tmp.ptr->isVar = true;
tmp.ptr->varCnt = 0;
cnt++;
return varName;
}
else
throw_error_fatal(INTERNAL_ERROR, "%s", "Inconsistency in compiler variables");
}
void defineUserVar(htab_t *symTable, htab_key_t name, varDataType varDataType, TokenValue varValue, bool isConst)
{
//variable identificator
static unsigned varCnt = 0;
htab_iterator_t tmp = htab_insert(symTable, name);
if (tmp.ptr != NULL)
{
tmp.ptr->tokenType = TOKEN_IDENTIFIER;
tmp.ptr->varDataType = varDataType;
tmp.ptr->isVarUsedDefined = true;
if (isConst == true)
tmp.ptr->varValue = varValue;
tmp.ptr->isConst = isConst;
tmp.ptr->isVar = true;
tmp.ptr->varCnt = varCnt++;
}
else
throw_error_fatal(DEFINITION_ERROR, "%s %s", "Multiple definitions of variable", name);
}
bool isVarUserDefined(htab_t *symTable, htab_key_t name)
{
htab_iterator_t tmp = htab_find(symTable, name);
if (tmp.ptr != NULL)
{
if (tmp.ptr->isVar == true)
return tmp.ptr->isVarUsedDefined;
}
return false;
}
void checkZeroDivision(htab_t *symTable, htab_key_t name)
{
if (isVarConst(symTable, name) == true)
{
htab_iterator_t tmp = htab_find(symTable, name);
if (tmp.ptr != NULL)
{
switch (tmp.ptr->varDataType)
{
case INTEGER:
if (tmp.ptr->varValue.i == 0)
throw_error_fatal(ZERO_DIVISION_ERROR, "%s", "Can't divide by zero");
break;
case FLOAT:
if (tmp.ptr->varValue.d == 0)
throw_error_fatal(ZERO_DIVISION_ERROR, "%s", "Can't divide by zero");
default:
break;
}
}
}
}
void removeVar(htab_t *symTable, htab_key_t name)
{
htab_iterator_t tmp = htab_find(symTable, name);
if (tmp.ptr != NULL)
{
if (tmp.ptr->isVar == true)
{
if (tmp.ptr->varDataType == STRING)
if (tmp.ptr->isConst == true)
string_free(&tmp.ptr->varValue.s);
htab_erase(symTable, tmp);
}
}
}
htab_t *getSymTableForVar(Vector *tableVector, htab_key_t name)
{
htab_iterator_t tmp;
unsigned int vecLength = vectorLength(tableVector);
for (unsigned int i = 1; i <= vecLength; i++)
{
htab_t *tmpTable = (htab_t *)vectorGet(tableVector, vecLength - i);
tmp = htab_find(tmpTable, name);
if (tmp.ptr != NULL)
{
if (tmp.ptr->isVar == true)
return (htab_t *)tmp.t;
}
}
return NULL;
}
int getSymtableIdForVar(Vector *tableVector, htab_key_t name)
{
htab_iterator_t tmp;
unsigned int vecLength = vectorLength(tableVector);
for (unsigned int i = 1; i <= vecLength; i++)
{
htab_t *tmpTable = (htab_t *)vectorGet(tableVector, vecLength - i);
tmp = htab_find(tmpTable, name);
if (tmp.ptr != NULL)
{
if (tmp.ptr->isVar == true)
return vecLength - i;
}
}
return EMPTY_SYMTABLE_ID;
}
htab_t *getLocalSymTable(Vector *tableVector)
{
if (tableVector != NULL)
{
if (vectorLength(tableVector) != 0)
{
return (htab_t *)vectorGet(tableVector, vectorLength(tableVector) - 1);
}
}
return NULL;
}
void insertLocalSymTable(Vector *tableVector)
{
if (tableVector != NULL)
{
htab_t *tmp = htab_init(SYM_TABLE_SIZE);
if (tmp == NULL)
throw_error_fatal(INTERNAL_ERROR, "%s", "Memory allocation error");
vectorPush(tableVector, (void *)tmp);
}
}
void removeLocalSymTable(Vector *tableVector)
{
if (tableVector != NULL)
{
if (vectorLength(tableVector) != 0)
{
htab_free((htab_t *)vectorPop(tableVector));
}
}
}
bool isFuncDefined(htab_t *symTable, htab_key_t name)
{
if (symTable != NULL)
{
htab_iterator_t tmp = htab_find(symTable, name);
if (tmp.ptr != NULL)
{
if (tmp.ptr->isVar == false)
return true;
}
}
return false;
}
void addFuncType(Vector *types, varDataType varType)
{
if (types != NULL)
{
varDataType *tmp = malloc(sizeof(varDataType));
if (tmp == NULL)
throw_error_fatal(INTERNAL_ERROR, "%s", "Memory allocation error");
*tmp = varType;
vectorPush(types, (void *)tmp);
}
}
void removeFuncTypes(Vector *types)
{
if (types != NULL)
{
while (vectorLength(types) != 0)
{
varDataType *tmp = (varDataType *)vectorPop(types);
free(tmp);
}
}
}
bool checkTypes(Vector *types1, Vector *types2)
{
if (types1 != NULL && types2 != NULL)
{
if (vectorLength(types1) == vectorLength(types2))
{
for (unsigned int i = 0; i < vectorLength(types1); i++)
{
if (*(varDataType *)vectorGet(types1, i) != NONE && *(varDataType *)vectorGet(types2, i) != NONE)
{
if (*(varDataType *)vectorGet(types1, i) != *(varDataType *)vectorGet(types2, i))
return false;
}
}
return true;
}
else
{
return false;
}
}
return false;
}
void defineFunc(htab_t *symTable, htab_key_t name, Vector *paramTypes, Vector *returnTypes)
{
if (symTable != NULL)
{
htab_iterator_t tmp = htab_insert(symTable, name);
if (tmp.ptr == NULL)
throw_error_fatal(DEFINITION_ERROR, "%s", "Function is already definded");
tmp.ptr->isConst = false;
tmp.ptr->isVar = false;
tmp.ptr->paramTypes = paramTypes;
tmp.ptr->returnTypes = returnTypes;
}
}
Vector *getFuncParamTypes(htab_t *symTable, htab_key_t name)
{
if (symTable != NULL)
{
if (isFuncDefined(symTable, name) == true)
{
htab_iterator_t tmp = htab_find(symTable, name);
if (tmp.ptr != NULL)
{
return tmp.ptr->paramTypes;
}
}
}
return NULL;
}
Vector *getFuncReturnTypes(htab_t *symTable, htab_key_t name)
{
if (symTable != NULL)
{
if (isFuncDefined(symTable, name) == true)
{
htab_iterator_t tmp = htab_find(symTable, name);
if (tmp.ptr != NULL)
{
return tmp.ptr->returnTypes;
}
}
}
return NULL;
}