forked from Shivankit-Gaind/Compiler-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypeExtractor.c
454 lines (319 loc) · 9.75 KB
/
typeExtractor.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
/****
Name: Shivankit Gaind
ID: 2015A7PS0076P
****/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "semanticAnalyzer.h"
#include "typeExtractor.h"
#include "symbolTable.h"
#include "ast.h"
/*** Extracts the type of an arithmetic expression ****/
void get_type_arithmetic_exp(ASTNode* node, int* undeclared_variable, ErrorList* errors){
if(node==NULL){
return;
}
astChildren* children;
ASTNode* temp1, *temp2;
Tokentype operator;
if(node->label==OPERATOR_NODE){
//Process Children First
children = node->children;
temp1 = children->head->next;
temp2 = temp1->next;
operator = children->head->lexicalTOKEN->type;
get_type_arithmetic_exp(temp1, undeclared_variable, errors);
get_type_arithmetic_exp(temp2, undeclared_variable, errors);
if(temp1->nodeType==ERROR || temp2->nodeType==ERROR){
node->nodeType = ERROR;
return;
}
if(temp1->nodeType==INT && temp2->nodeType==INT && (operator==PLUS || operator==MINUS || operator==MUL)){
node->nodeType = INT;
return;
}
if(temp1->nodeType==REAL && temp2->nodeType==REAL && (operator==PLUS || operator==MINUS || operator==MUL)){
node->nodeType = REAL;
return;
}
if(temp1->nodeType==INT && temp2->nodeType==INT && (operator==DIV)){
node->nodeType = REAL;
return;
}
if(temp1->nodeType==REAL && temp2->nodeType==REAL && (operator==DIV)){
node->nodeType = REAL;
return;
}
if(temp1->nodeType==STRING && temp2->nodeType==STRING && (operator==PLUS)){
node->str_length = temp1->str_length + temp2->str_length;
node->nodeType = STRING;
return;
}
if(temp1->nodeType==MATRIX && temp2->nodeType==MATRIX && (operator==PLUS || operator==MINUS)){
node->nodeType = MATRIX;
//Beginning of rows
int check_size=1;
//Check Compatibility of matrices
if(temp1-> dimension1==0 || temp2->dimension1==0 || temp1->dimension2 ==0 || temp2->dimension2==0 || temp1->dimension1 != temp2->dimension1 || temp1->dimension2 != temp2->dimension2){
check_size = 0;
}
if(check_size!=1){
node->nodeType = ERROR;
char* str = (char*)malloc(sizeof(str)*ERROR_STRING_SIZE);
sprintf(str,"Error: Adding Matrices of incompatibe sizes");
add_error(errors,str,node->line_no);
//REPORT ERROR
}
else{
node->dimension1 = temp1->dimension1;
node->dimension2 = temp1->dimension2;
}
return;
}
node->nodeType = ERROR;
char* str = (char*)malloc(sizeof(str)*ERROR_STRING_SIZE);
sprintf(str,"Error: Invalid operand types for operator '%s'",node->children->head->lexicalTOKEN->lexeme);
add_error(errors,str,node->line_no);
//REPORT ERROR
return;
}
//Otherwise it's a variable node
else{
get_type_var_node(node, undeclared_variable, errors);
}
}
void get_type_boolean_exp(ASTNode* node, int* undeclared_variable, ErrorList* errors){
//<booleanExpression> ===> OP <booleanExpression> CL <logicalOp1> OP <booleanExpression> CL
if(node->label==BOOLEANEXPRESSION1_NODE){
astChildren* children = node->children;
ASTNode* temp1 = children->head;
ASTNode* temp2 = children->head->next->next;
get_type_boolean_exp(temp1,undeclared_variable, errors);
get_type_boolean_exp(temp2, undeclared_variable, errors);
if(temp1->nodeType==ERROR || temp2->nodeType==ERROR){
node->nodeType = ERROR;
}
else if(temp1->nodeType==BOOLEAN && temp2->nodeType==BOOLEAN){
node->nodeType = BOOLEAN;
}
else{
char* str = (char*)malloc(sizeof(str)*ERROR_STRING_SIZE);
sprintf(str,"Error: Ivalid boolean expression as operand for '%s'",node->children->head->next->lexicalTOKEN->lexeme);
add_error(errors,str,node->line_no);
//REPORT ERROR
node->nodeType = ERROR;
}
return;
}
//<booleanExpression> ===> NOT OP <booleanExpression> CL
if(node->label==BOOLEANEXPRESSION2_NODE){
ASTNode* temp = node->children->head;
get_type_boolean_exp(temp,undeclared_variable, errors);
if(temp->nodeType==BOOLEAN){
node->nodeType = BOOLEAN;
}
else{
node->nodeType = ERROR;
}
return;
}
//<booleanExpression> ===> <constrainedVars> <relationalOp> <constrainedVars>
if(node->label==BOOLEANEXPRESSION3_NODE){
astChildren* children = node->children;
//2 constrained variables nodes
ASTNode* var_node1 = children->head;
ASTNode* var_node2 = children->head->next->next;
//Get their types
get_type_var_node(var_node1,undeclared_variable, errors);
get_type_var_node(var_node2,undeclared_variable, errors);
if(var_node1->nodeType==ERROR || var_node2->nodeType==ERROR){
node->nodeType = ERROR;
}
//<constrainedVars> ===> NUM
else if(var_node1->nodeType==INT && var_node2->nodeType==INT){
node->nodeType = BOOLEAN;
}
//<constrainedVars> ===> RNUM
else if(var_node1->nodeType==REAL && var_node2->nodeType==REAL){
node->nodeType = BOOLEAN;
}
else{
node->nodeType = ERROR;
char* str = (char*)malloc(sizeof(str)*ERROR_STRING_SIZE);
sprintf(str,"Error: Invalid operand types for relational operator '%s'",node->children->head->next->lexicalTOKEN->lexeme);
add_error(errors,str,node->line_no);
//REPORT ERROR
}
return;
}
}
//Gives a type to a variable Node
void get_type_var_node(ASTNode* node, int* undeclared_variable, ErrorList* errors){
astChildren* children = node->children;
ASTNode* temp = children->head;
//Leaf Node
if(temp->leaf==1){
//<var> ===> NUM
if(temp->lexicalTOKEN->type == NUM){
node->nodeType = INT;
}
//<var> ===> RNUM
else if(temp->lexicalTOKEN->type == RNUM){
node->nodeType = REAL;
}
//<var> ===> STR
else if(temp->lexicalTOKEN->type == STR){
node->nodeType = STRING;
node->str_length = strlen(temp->lexicalTOKEN->lexeme) - 2;
}
//<var> ===> ID <matrix_may_be>
else if(temp->lexicalTOKEN->type == ID){
//If undeclared variable
if(temp->nodeType==ERROR){
*undeclared_variable =1;
node->nodeType = ERROR;
}
else{
//Check whether the variable is initialized or not
check_assigned(temp, errors);
//Matrix Element
if(temp->concat!=NULL){
node->nodeType = INT;
}
//Only Id
else{
node->nodeType = temp->nodeType;
SymbolNode* info = findInfo(temp);
if(temp->nodeType==MATRIX){
node->dimension1 = info->symbol->dimension1;
node->dimension2 = info->symbol->dimension2;
}
if(temp->nodeType==STRING){
node->str_length = info->symbol->width;
}
}
}
}
}
//Non Leaf Node
else{
//<var> ===> <matrix_actual>
if(temp->label==ROW){
node->nodeType = MATRIX;
get_dimensions_matrix(node, errors);
if(node->dimension1==0 || node->dimension2==0){
node->nodeType = ERROR;
char* str = (char*)malloc(sizeof(str)*ERROR_STRING_SIZE);
sprintf(str,"Error: Invalid Matrix");
add_error(errors,str,node->line_no);
//REPORT ERROR
}
}
//<var> ===> <functionCall>
else if(temp->label==FUNCTIONCALL_NODE){
get_type_function_call(temp,undeclared_variable,errors);
if(temp->nodeType==ERROR){
//Undefined Function
node->nodeType = ERROR;
}
else{
//Only one parameter is returned
if(temp->return_parameters!=NULL && temp->return_parameters->head->next==NULL){
node->nodeType = temp->return_parameters->head->type;
}
else{
char* str = (char*)malloc(sizeof(str)*ERROR_STRING_SIZE);
sprintf(str,"Error: Inappropriate no. of return parameters");
add_error(errors,str,node->line_no);
//REPORT ERROR
node->nodeType = ERROR; //This var node is error node
}
}
}
}
}
//Set the return parameters of the function call
void get_type_function_call(ASTNode* node, int* undeclared_variable, ErrorList* errors){
node->return_parameters = initialize_type_list();
//The FUNID Node
ASTNode* temp = node->children->head;
//If the corresponding definition doesn't exist or recursive call
if(temp->nodeType==ERROR){
node->nodeType = ERROR;
*undeclared_variable = 1;
return;
}
//Find the corresponding definition
SymbolNode* info = getSymbolInfo(temp->lexicalTOKEN->lexeme, node->current_scope->table);
//The funid corresponding to definition
ASTNode* def_node = info->symbol->astNode;
//Go to the first output parameter
temp = def_node->parent->children->head;
while(temp!=NULL){
Tokentype datatype = temp->children->head->lexicalTOKEN->type;
add_to_return_parameters(node->return_parameters,datatype);
temp = temp->concat;
}
check_input_parameters(node,errors);
return;
}
void get_type_sizeexp(ASTNode* node, int* undeclared_variable, ErrorList* errors){
ASTNode* id = node->children->head;
//If the variable is undeclared
if(id->nodeType==ERROR){
node->nodeType = ERROR;
*undeclared_variable = 1;
}
else{
//Check whether the variable is initialized or not
check_assigned(id, errors);
SymbolNode* info = findInfo(id);
//The type is INT
if(id->nodeType==STRING){
node->nodeType = INT;
}
//Else the type is INT x INT
else if(id->nodeType==MATRIX){
node->nodeType = -1;
node->return_parameters = initialize_type_list();
add_to_return_parameters(node->return_parameters,INT);
add_to_return_parameters(node->return_parameters,INT);
}
else{
char* str = (char*)malloc(sizeof(str)*ERROR_STRING_SIZE);
sprintf(str,"Error: Size exp can only have string or matrix as input");
add_error(errors,str,node->line_no);
//REPORT ERROR
node->nodeType = ERROR;
}
}
}
void get_dimensions_matrix(ASTNode* node, ErrorList* errors){
int i = 0;
int j = 0;
ASTNode* row = node->children->head;
ASTNode* col;
while(row!=NULL){
i++;
col = row->children->head;
int q = 0;
while(col!=NULL){
q++;
col = col->concat;
}
if(j==0)
j = q;
else{
//Invalid Matrix
if(j!=q){
node->nodeType = ERROR;
return;
}
}
row = row->concat;
}
node->dimension1 = i;
node->dimension2 = j;
}