-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSemantic_Handler.h
573 lines (462 loc) · 15 KB
/
Semantic_Handler.h
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#include "Compiler.h"
#include <stdio.h>
#include <stdlib.h>
#include "Enums.h"
struct CHECKS
{
bool check_identifier_name;
bool check_identifier_type;
};
struct SEMANTIC_STACK {
IDENTIFIER_SEMANTIC_TYPE identifier_semantic_type;
struct SEMANTIC_STACK* prev;
};
struct SEMANTIC {
struct SEMANTIC* temp;
struct SEMANTIC* next;
struct SEMANTIC_STACK* args_stack;
IDENTIFIER_SEMANTIC_TYPE identifier_semantic_type;
char* identifier_name;
bool is_function;
bool is_assigned;
bool is_const;
bool is_parameter;
int function_number;
int value;
struct BLOCK* block;
};
struct SEMANTIC* head;
struct BLOCK* newBlockToSemantic(struct BLOCK* current_block,struct BLOCK* parent_block_ptr) {
if (current_block == NULL)
return NULL;
struct BLOCK* node = (struct BLOCK*) malloc (sizeof(struct BLOCK));
node->block_type = current_block->block_type;
node->do_while_count = current_block->do_while_count;
node->while_count = current_block->while_count;
node->for_count = current_block->for_count;
node->if_count = current_block->if_count;
node->stmt_group_count = current_block->stmt_group_count;
//node->name = current_block->name; //only important for function block
if(current_block->block_type == Block_Global_Type)
sprintf(node->name, "Global Variables");
else
sprintf(node->name, "%s",current_block->name);
//node->name_ = malloc(sizeof(char) * (strlen(current_block->name)+1));
//sprintf(node->name_, "%s",current_block->name);
//node->name_ = "hello";
//sprintf(node->name, "bad");
node->function_number = current_block->function_number;
node->parent_block_ptr = parent_block_ptr;
//node->name = (char*)malloc(strlen(current_block->name) + 1);
//strcpy(node->name, current_block->name);
//printf("current_block %s\n",node->name);
node->child_block_ptr = newBlockToSemantic(current_block->child_block_ptr,node);
return node;
}
//return a list of same identifier name
struct SEMANTIC* findIdentifier(char* name,bool is_function)
{
struct SEMANTIC* temp = head;
if (temp == NULL)
{
return NULL;
}
struct SEMANTIC* new_list= NULL;
struct SEMANTIC* new_list_head= NULL;
while(temp != NULL)
{
//printf("Comparison : %s - %s - %d \n",temp->identifier_name, name,strcmp(temp->identifier_name,name));
if(strcmp(temp->identifier_name,name) == 0 && temp->is_function == is_function ) // comapre between two names and if function
{
if (new_list == NULL)
{
//printf("newlist\n");
new_list = temp;
new_list_head = new_list;
}
else
{
//printf("oldlist\n");
new_list->temp = temp;
new_list = new_list->temp;
//printf("oldlist\n");
}
temp = temp->next;
}
else
temp = temp->next;
}
//printf("out\n");
if(new_list != NULL)
new_list->temp = NULL;
return new_list_head;
}
bool checkBlock(struct BLOCK* identifier_block,struct BLOCK* current_block)
{
if(identifier_block->child_block_ptr == NULL)
{
//printf("global_block");
return true;
}
/*struct BLOCK* temp;
while(current_block->parent_block_ptr != NULL)
{
printf("1%s",current_block->name);
temp = current_block;
current_block = current_block->parent_block_ptr;
if (current_block == NULL)
{
current_block = temp;
break;
}
}
while(temp->parent_block_ptr != NULL)
{
printf("%s",temp->name);
temp = temp->parent_block_ptr;
}*/
/*while(identifier_block->do_while_count == current_block->do_while_count
&& identifier_block->while_count == current_block->while_count
&& identifier_block->for_count == current_block->for_count
&& identifier_block->if_count == current_block->if_count
&& identifier_block->stmt_group_count == current_block->stmt_group_count
&& identifier_block->name == current_block->name )
{
printf("hello");
identifier_block = identifier_block->child_block_ptr;
current_block = current_block->child_block_ptr;
printf("%s",identifier_block->name);
if(identifier_block == NULL)
return true;
if(current_block == NULL)
return false;
}*/
printBlockFunctionName(identifier_block->child_block_ptr);
while(1 == 1)
{
//printf("%s",identifier_block->function_number);
identifier_block = identifier_block->child_block_ptr;
current_block = current_block->child_block_ptr;
if(identifier_block == NULL)
{
//printf("same block\n");
return true;
}
if(current_block == NULL)
return false;
if(identifier_block->parent_block_ptr->do_while_count != current_block->parent_block_ptr->do_while_count)
return false;
if(identifier_block->parent_block_ptr->for_count != current_block->parent_block_ptr->for_count)
return false;
if(identifier_block->parent_block_ptr->while_count != current_block->parent_block_ptr->while_count)
return false;
if(identifier_block->parent_block_ptr->if_count != current_block->parent_block_ptr->if_count)
return false;
if(identifier_block->parent_block_ptr->stmt_group_count != current_block->parent_block_ptr->stmt_group_count)
return false;
if(strcmp(identifier_block->name,current_block->name) != 0)
return false;
//printf("%s - %s\n",identifier_block->name,current_block->name);
if(strcmp(identifier_block->name,current_block->name) == 0 && identifier_block->function_number != current_block->function_number)
return false;
//printf("%s\n",identifier_block->name);
}
//printf("sdfsdfsd%s",current_block->name);
/*while(identifier_block->do_while_count - current_block->do_while_count == 0 )
{
identifier_block = identifier_block->child_block_ptr;
current_block = current_block->child_block_ptr;
printf("%s",identifier_block->name);
if(identifier_block == NULL)
return true;
if(current_block == NULL)
return false;
}*/
//printf("two");
return false;
}
/*
bool checkBlock(char* identifier_block,char* current_block)
{
int i = 1;
int size_of_identifier = strlen(identifier_block);
if(size_of_identifier > strlen(current_block))
return false;
while(i <= size_of_identifier)
{
if(strncmp(identifier_block, current_block,i) != 0)
return false;
i+=1;
}
return true;
}
*/
int checkSemantic(char* name, bool is_function,struct BLOCK* current_block,struct CHECKS* checks,IDENTIFIER_SEMANTIC_TYPE identifier_semantic_type)
{
//printf("%s\n",current_block);
//printf("before\n");
struct SEMANTIC* list_of_names = findIdentifier(name,is_function);
int count = 0;
if(is_function == true)
{
while(list_of_names != NULL)
{
if(checks->check_identifier_type)
{
if(list_of_names->identifier_semantic_type == identifier_semantic_type)
{
count +=1;
}
}
else
{
count +=1;
}
list_of_names = list_of_names->temp;
}
return count; //0 = no matching - 1 = one match - more than 1 is wrong
}
while(list_of_names != NULL)
{
printf("%s\n",list_of_names->identifier_name);
bool ret = checkBlock(list_of_names->block,current_block);
if (ret == true)
{
printf("sdfsdfsdf\n");
count +=1;
}
list_of_names = list_of_names->temp;
}
return count; //0 = no matching - 1 = one match - more than 1 is wrong
}
struct SEMANTIC_STACK* semantic_stack_head;
struct SEMANTIC_STACK* newSemanticStack()
{
struct SEMANTIC_STACK* semantics = (struct SEMANTIC_STACK*) malloc (sizeof (struct SEMANTIC_STACK));
semantics->prev = NULL;
return semantics;
}
void addNewSemantic(struct SEMANTIC* semantic)
{
semantic->block = newBlockToSemantic(semantic->block,NULL);
if (head == NULL)
{
head = semantic;
return;
}
struct SEMANTIC* temp = head;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = semantic;
return;
}
void addArgsToSemantic(struct SEMANTIC* semantic, IDENTIFIER_SEMANTIC_TYPE identifier_semantic_type)
{
printf("%dhello\n",semantic->args_stack);
if (semantic->args_stack == NULL)
{
printf("hello\n");
semantic->args_stack = newSemanticStack();
semantic->args_stack->identifier_semantic_type = identifier_semantic_type;
printf("stack: %d\n",semantic->args_stack->prev);
return;
}
struct SEMANTIC_STACK* temp = semantic->args_stack;
while(temp->prev != NULL) //deal with prev as if it's next in this case
{
//printf("%dthere\n",temp->identifier_semantic_type);
temp = temp->prev;
}
temp->prev = newSemanticStack();
temp->prev->identifier_semantic_type = identifier_semantic_type;
printf("stacks: %d\n",temp->prev->prev);
}
void printNumberOfArgs(struct SEMANTIC_STACK* args)
{
while(args != NULL)
{
printf("argtype: %d -> ",args->identifier_semantic_type);
args = args->prev;
}
printf("\n");
}
void printBlockFunctionName(struct BLOCK* block)
{
while(block != NULL)
{
printf("%s -> ",block->name);
block = block->child_block_ptr;
}
printf("\n");
return;
}
struct CHECKS* newCheck()
{
return (struct CHECKS*) malloc (sizeof (struct CHECKS));
}
struct SEMANTIC* newSemantic()
{
struct SEMANTIC* semantics = (struct SEMANTIC*) malloc (sizeof (struct SEMANTIC));
semantics->args_stack = NULL;
semantics->next = NULL;
semantics->temp = NULL;
return semantics;
}
struct SEMANTIC* findSemanticIdentifier(char* identifier_name)
{
struct SEMANTIC* list_of_names = findIdentifier(identifier_name,false);
while(list_of_names != NULL)
{
//printf("%s\n",list_of_names->identifier_name);
bool ret = checkBlock(list_of_names->block,head_block_ptr);
if (ret == true)
{
list_of_names->temp = NULL;
return list_of_names;
}
list_of_names = list_of_names->temp;
}
return NULL;
}
struct SEMANTIC* findSemanticFunction(char* identifier_name,struct SEMANTIC_STACK* semantic_stack)
{
struct SEMANTIC* list_of_names = findIdentifier(identifier_name,true);
struct SEMANTIC_STACK* temp = semantic_stack;
struct SEMANTIC_STACK* temp2;
while(list_of_names != NULL)
{
printNumberOfArgs(list_of_names->args_stack);
printNumberOfArgs(temp);
temp2 = list_of_names->args_stack;
while (true)
{
printf("c\n");
if(temp2 == NULL || temp == NULL)
{
printf("%d-%d\n",temp,temp2);
break;
}
if(temp2->identifier_semantic_type != temp->identifier_semantic_type)
{
printf("%d - %d\n",temp2->identifier_semantic_type ,temp->identifier_semantic_type );
break;
}
temp2 = temp2->prev; //deal with prev as next
temp = temp->prev;
}
if(temp2 == NULL && temp == NULL)
{
return list_of_names;
}
temp = semantic_stack;
list_of_names = list_of_names->temp;
}
return NULL;
}
void pushSemanticStack(struct SEMANTIC_STACK* semantic_stack)
{
if (semantic_stack_head == NULL)
{
//printf("good");
semantic_stack_head = semantic_stack;
semantic_stack_head->prev = NULL;
}
else
{
struct SEMANTIC_STACK* temp2;
temp2 = semantic_stack_head;
semantic_stack_head = semantic_stack;
semantic_stack_head->prev = temp2;
}
};
struct SEMANTIC_STACK* popSemanticStack()
{
if(semantic_stack_head == NULL)
{
//printf("bad");
return NULL;
}
else
{
struct SEMANTIC_STACK* temp2;
temp2 = semantic_stack_head;
semantic_stack_head = semantic_stack_head->prev;
return temp2;
}
}
bool isEmptySemanticStack()
{
if(semantic_stack_head == NULL)
return true;
return false;
}
IDENTIFIER_SEMANTIC_TYPE compareTypes(IDENTIFIER_SEMANTIC_TYPE type1, IDENTIFIER_SEMANTIC_TYPE type2)
{
if(type1 == Float_Semantic_Type)
{
if(type2 == Bool_Semantic_Type)
return Error_Semantic_Type;
return Float_Semantic_Type;
}
if(type2 == Float_Semantic_Type)
{
if(type1 == Bool_Semantic_Type)
return Error_Semantic_Type;
return Float_Semantic_Type;
}
if(type1 == Int_Semantic_Type)
{
if(type2 == Bool_Semantic_Type)
return Error_Semantic_Type;
return Int_Semantic_Type;
}
if(type2 == Int_Semantic_Type)
{
if(type1 == Bool_Semantic_Type)
return Error_Semantic_Type;
return Int_Semantic_Type;
}
if(type1 == Bool_Semantic_Type)
{
if(type2 != Bool_Semantic_Type)
return Error_Semantic_Type;
return Bool_Semantic_Type;
}
if(type2 == Bool_Semantic_Type)
{
if(type1 != Bool_Semantic_Type)
return Error_Semantic_Type;
return Bool_Semantic_Type;
}
return Error_Semantic_Type;
}
extern FILE *semantic_file;
void checkNotAssignedIdentifiers()
{
struct SEMANTIC* temp = head;
while(temp!=NULL)
{
if (temp->is_assigned == false && temp->is_function == false && temp->is_parameter == false)
{
fprintf(semantic_file,"WARNING: Identified a variable %s but not assigend a value to it\n",temp->identifier_name);
}
head = temp;
temp = temp->next;
free(head);
}
}
//if not found a function name matching the calling function - (no function found)
// if Identified a variable but not assigend a value later
// if send arguments with different types - (no function found)
// if return type of function doesn't match with the identifier - (no function found)
// if send arguments with different number of arguments - (no function found)
// can't reassign a constant variable
// can't reassign a constant parameter
// if compare between bool and int/float
// if rhs is different type from lhs
//if rhs identifier is not assigned value before it's used
// If identifier not declared before wether if it's on the rhs or lhs
// tell you how many times the variable declared before in same block
// If you have two names of the same type in the same block