-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemantic.c
1932 lines (1805 loc) · 63.6 KB
/
semantic.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
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "semantic.h"
#include <string.h>
#include "semantic_struct.h"
#include "helpers.h"
char* CLASS_NAME = "Go";
int scope = 0;
struct Constant* constantCode;
struct Constant* constantClassString;
struct Constant* objectConstructorMethodRef;
struct Constant* constantObjectClass;
struct Constant* constantSourceFile;
struct Constant* constantSourceFileName;
struct Constant* constantClassRuntimeLib;
struct Constant* printStringMethodRef;
struct Constant* printIntegerMethodRef;
struct Constant* printFloatMethodRef;
struct Constant* scanStringMethodRef;
struct Constant* scanIntegerMethodRef;
struct Constant* scanFloatMethodRef;
struct Constant* openSquareParenthesis;
struct Constant* closeSquareParenthesis;
struct Constant* space;
struct Constant* addConstantMethodRefToConstantTable(struct Constant* clazz, struct Constant* nameAndType) {
struct Constant* constant = (struct Constant*) malloc(sizeof(struct Constant));
constant->const1 = clazz;
constant->const2 = nameAndType;
constant->id = list_size(constantsTable) + 1;
constant->type = CONSTANT_Methodref;
list_add(constantsTable, constant);
return constant;
}
void addRuntimeLibConstant() {
constantClassRuntimeLib = addClassToConstantsTable("RuntimeLib");
struct Constant* printStringNameAndType = addNameAndTypeToConstantsTable("printString", "(Ljava/lang/String;)V");
struct Constant* printIntegerNameAndType = addNameAndTypeToConstantsTable("printInteger", "(I)V");
struct Constant* printFloatNameAndType = addNameAndTypeToConstantsTable("printFloat", "(F)V");
struct Constant* scanStringNameAndType = addNameAndTypeToConstantsTable("scanString", "()Ljava/lang/String;");
struct Constant* scanIntegerNameAndType = addNameAndTypeToConstantsTable("scanInteger", "()I");
struct Constant* scanFloatNameAndType = addNameAndTypeToConstantsTable("scanFloat", "()F");
printStringMethodRef = addConstantMethodRefToConstantTable(constantClassRuntimeLib, printStringNameAndType);
printIntegerMethodRef = addConstantMethodRefToConstantTable(constantClassRuntimeLib, printIntegerNameAndType);
printFloatMethodRef = addConstantMethodRefToConstantTable(constantClassRuntimeLib, printFloatNameAndType);
scanStringMethodRef = addConstantMethodRefToConstantTable(constantClassRuntimeLib, scanStringNameAndType);
scanIntegerMethodRef = addConstantMethodRefToConstantTable(constantClassRuntimeLib, scanIntegerNameAndType);
scanFloatMethodRef = addConstantMethodRefToConstantTable(constantClassRuntimeLib, scanFloatNameAndType);
}
struct SemanticType* checkExpressionType(struct Expression* expr, struct Method* method) {
struct SemanticType* type = (struct SemanticType*) malloc(sizeof(struct SemanticType));
switch (expr->exprType)
{
case PRIMARY: {
type = checkPrimaryExpressionType(expr->primaryExpr, method);
break;
}
case NOT_UNARY_EXPR:
{
printf("Semantic error. Not operation not supported\n");
type->typeName = UNKNOWN_TYPE;
break;
}
case PLUS_UNARY_EXPR:
case MINUS_UNARY_EXPR:
{
type = checkPrimaryExpressionType(expr->primaryExpr, method);
if(type->typeName != FLOAT32_TYPE_NAME && type->typeName != INT_TYPE_NAME){
printf("Semantic error. Type of plus/minus unary expression should be int or float \n");
type->typeName = UNKNOWN_TYPE;
}
if (type->arrayType == ARRAY) {
printf("Semantic error. Array use as operand of PLUS/MINUS operator\n");
type->typeName = UNKNOWN_TYPE;
}
break;
}
case AND_EXPRESSION:
case OR_EXPRESSION:
{
printf("Semantic error. AND/OR operation not supported\n");
type->typeName = UNKNOWN_TYPE;
break;
}
case EQU_EXPRESSION:
case NE_EXPRESSION:
{
struct SemanticType* leftType = checkExpressionType(expr->leftExpr, method);
struct SemanticType* rightType = checkExpressionType(expr->rightExpr, method);
if (leftType->typeName != rightType->typeName || leftType->typeName == UNKNOWN_TYPE)
{
printf("Semantic error. Left and right operands of comparation should be same type\n");
type->typeName = UNKNOWN_TYPE;
}
else if (leftType->arrayType != NONE_ARRAY || rightType->arrayType != NONE_ARRAY) {
printf("Semantic error. Left and right operands of comparation can not be array\n");
type->typeName = UNKNOWN_TYPE;
}
else {
type->typeName = BOOL_TYPE_NAME;
type->arrayType = NONE_ARRAY;
}
break;
}
case GT_EXPRESSION:
case GTE_EXPRESSION:
case LT_EXPRESSION:
case LTE_EXPRESSION:
{
struct SemanticType* leftType = checkExpressionType(expr->leftExpr, method);
struct SemanticType* rightType = checkExpressionType(expr->rightExpr, method);
if (leftType->typeName == INT_TYPE_NAME && rightType->typeName == INT_TYPE_NAME ||
leftType->typeName == FLOAT32_TYPE_NAME && rightType->typeName == FLOAT32_TYPE_NAME||
leftType->typeName == BOOL_TYPE_NAME && rightType->typeName == BOOL_TYPE_NAME
&& leftType->arrayType == NONE_ARRAY && rightType->arrayType == NONE_ARRAY) {
type->typeName = BOOL_TYPE_NAME;
type->arrayType = NONE_ARRAY;
}
else {
printf("Semantic error. Left and right operands of comparation should be same type\n");
type->typeName = UNKNOWN_TYPE;
}
break;
}
case PLUS_EXPRESSION:
case MINUS_EXPRESSION:
case MUL_EXPRESSION:
case DIV_EXPRESSION:
case MOD_EXPRESSION:
{
struct SemanticType* leftType = checkExpressionType(expr->leftExpr, method);
struct SemanticType* rightType = checkExpressionType(expr->rightExpr, method);
if (leftType->arrayType != NONE_ARRAY || rightType->arrayType != NONE_ARRAY) {
printf("Semantic error. Array used as operand of arithmetic operator\n");
}
else {
if (leftType->typeName == rightType->typeName && leftType->typeName != UNKNOWN_TYPE){
if (expr->exprType == PLUS_EXPRESSION) {
type->typeName = leftType->typeName;
type->arrayType = NONE_ARRAY;
}
if (expr->exprType == MINUS_EXPRESSION) {
if (leftType->typeName == STRING_TYPE_NAME || rightType->typeName == STRING_TYPE_NAME) {
printf("Semantic error. Operands of subtract cannot be string\n");
type->typeName = UNKNOWN_TYPE;
}
else {
type->typeName = leftType->typeName;
type->arrayType = NONE_ARRAY;
}
}
else if (expr->exprType == MUL_EXPRESSION) {
if (leftType->typeName == STRING_TYPE_NAME || rightType->typeName == STRING_TYPE_NAME) {
printf("Semantic error. Operands of multiple cannot be string\n");
type->typeName = UNKNOWN_TYPE;
}
else {
type->typeName = leftType->typeName;
type->arrayType = NONE_ARRAY;
}
}
else if (expr->exprType == DIV_EXPRESSION) {
if (leftType->typeName == STRING_TYPE_NAME || rightType->typeName == STRING_TYPE_NAME) {
printf("Semantic error. Operands of division cannot be string\n");
type->typeName = UNKNOWN_TYPE;
}
else {
type->typeName = leftType->typeName;
type->arrayType = NONE_ARRAY;
}
}
else if (expr->exprType == MOD_EXPRESSION) {
if (leftType->typeName != INT_TYPE_NAME || rightType->typeName != INT_TYPE_NAME) {
printf("Semantic error. Incompatible types of modulo operation\n");
}
else {
type->typeName = INT_TYPE_NAME;
type->arrayType = NONE_ARRAY;
}
}
}
else {
printf("Semantic error. Left and right operands should be same type\n");
type->typeName = UNKNOWN_TYPE;
}
}
break;
}
}
expr->semanticType = type;
return type;
}
//pass the method and program to this function
struct SemanticType* checkPrimaryExpressionType(struct PrimaryExpression* primaryExpr, struct Method* method) {
struct SemanticType* type = (struct SemanticType*)malloc(sizeof(struct SemanticType));
//type->IS_ARRAY_ACCESS = false;
switch (primaryExpr->exprType) {
case BOOL_TRUE_EXPRESSION:
case BOOL_FALSE_EXPRESSION: {
printf("Boolean primary expressions are not supported\n");
type ->typeName = UNKNOWN_TYPE;
break;
}
case DECIMAL_EXPR: {
type->typeName = INT_TYPE_NAME;
type->arrayType = NONE_ARRAY;
struct Constant* constant = addIntegerToConstantsTable(primaryExpr->decNumber);
type->constantExpressionNum = constant->id;
break;
}
case FLOAT_EXPR: {
type->typeName = FLOAT32_TYPE_NAME;
type->arrayType = NONE_ARRAY;
struct Constant* constant = addFloatToConstantsTable(primaryExpr->floatNumber);
type->constantExpressionNum = constant->id;
break;
}
case STRING_EXPR: {
type->typeName = STRING_TYPE_NAME;
type->arrayType = NONE_ARRAY;
struct Constant* constant = addStringToConstantsTable(primaryExpr->stringLiteral);
type->constantExpressionNum = constant->id;
break;
}
case ID_EXPRESSION: {
//find type and idNum of identifier
struct LocalVariable* variable = findActiveLocalVariableByName(method->localVariablesTable, primaryExpr->identifier);
//addUtf8ToConstantsTable(primaryExpr->identifier);
if (variable == NULL) {
struct Field* field = getField(semanticClass, primaryExpr->identifier);
if (field == NULL) {
printf("Semantic error. Identifier %s not declared\n", primaryExpr->identifier);
type ->typeName = UNKNOWN_TYPE;
}
else {
type->typeName = field->type->typeName;
type->arrayType = field->type->arrayType;
type->idNum = field->id;
}
}
else {
type = variable->semanticType;
}
break;
}
case EXPRESSION: {
//how to eliminate multilevel expression ???
struct SemanticType* nestedType = checkExpressionType(primaryExpr->expr, method);
type = nestedType;
break;
}
case PE_COMPOSITE: {
if (primaryExpr->exprType != IDENTIFIER_TYPE_NAME) {
struct SemanticType* arrayType = checkPrimaryExpressionType(primaryExpr->primaryExpr, method);
struct SemanticType* indexType = checkExpressionType(primaryExpr->expr, method);
if (arrayType->typeName != FLOAT32_TYPE_NAME &&
arrayType->typeName != INT_TYPE_NAME &&
arrayType->typeName != STRING_TYPE_NAME) {
printf("Semantic error. Access to unknown array in function %s\n", method->constMethodref->const2->const1->utf8);
type->typeName = UNKNOWN_TYPE;
}
else {
if (indexType->typeName != INT_TYPE_NAME) {
printf("Semantic error. Array's index must be integer-type\n");
type->typeName = UNKNOWN_TYPE;
}
else {
//or type->arrayType = ARRAY_ELEMENT ???
type->typeName = arrayType->typeName;
type->arrayType = NONE_ARRAY;
}
}
}
break;
}
case FUNCTION_CALL:{
//search the definition of function in constant table to find return type
struct FunctionCall* functionCall = primaryExpr->funcCall;
char* methodName = functionCall->primaryExpr->identifier;
struct Method* calledMethod = getMethod(methodName);
if (calledMethod != NULL) {
bool isOk = checkSemanticFunctionCall(functionCall->exprList, calledMethod->paramList, method);
if (isOk) {
type = calledMethod->returnType;
type->typeName = calledMethod->returnType->typeName;
type->arrayType = calledMethod->returnType->arrayType;
}
}
else {
printf("Semantic error. Method is %s not declared\n", methodName);
type->typeName = UNKNOWN_TYPE;
}
break;
}
}
primaryExpr->semanticType = type;
return type;
}
bool doSemantic(struct Program* program) {
//create class to wrap programm
semanticClass = (struct Class*)malloc(sizeof(struct Class));
semanticClass->className = CLASS_NAME;
//Initialize constantsTable
list_new(&constantsTable);
semanticClass->constantsTable = constantsTable;
//Initialize fieldsTable
hashtable_new(&fieldsTable);
semanticClass->fieldsTable = fieldsTable;
//Initialize methodsTable
hashtable_new(&methodsTable);
semanticClass->methodsTable = methodsTable;
//add object class and it's init method to constants table
constantObjectClass = addObjectClassToConstantsTable();
//add Code constants table
constantCode = addUtf8ToConstantsTable("Code");
//add CLASS's name to constantsTAble
struct Constant* constClassName = addUtf8ToConstantsTable(CLASS_NAME);
//add class to constants table
constantClass = addClassToConstantsTable(CLASS_NAME);
//add constant type string
constantClassString = addClassToConstantsTable("java/lang/String");
//add addition constants to print string
openSquareParenthesis = addStringToConstantsTable("[");
closeSquareParenthesis = addStringToConstantsTable("]");
space = addStringToConstantsTable(" ");
addRuntimeLibConstant();
//
constantSourceFile = addUtf8ToConstantsTable("SourceFile");
constantSourceFileName = addUtf8ToConstantsTable("Go.java");
bool isOk = true;
struct DeclarationList* declList = program->declList;
if (declList != NULL) {
struct Declaration* decl = declList->firstDecl;
while (decl != NULL && isOk) {
if (decl->declType == CONST_DECL) {
isOk = checkSemanticConstDecl(decl->constDecl, NULL);
}
else if (decl->declType == VAR_DECL) {
isOk = checkSemanticVarDecl(decl->varDecl, NULL);
}
else if (decl->declType == FUNC_DECL) {
isOk = checkSemanticFunctionDecl(decl->funcDecl);
}
decl = decl->nextDecl;
}
}
//check semantic main function (function without parameters)
return isOk;
}
struct Constant* addObjectClassToConstantsTable() {
//Add class's metadata to constants table
char* constructorName = "<init>";
struct Constant* constructorConstant = addUtf8ToConstantsTable(constructorName);
char* constructorTypeStr = "()V";
struct Constant* constructorType = addUtf8ToConstantsTable(constructorTypeStr);
struct Constant* constructorNameAndType =
addNameAndTypeToConstantsTable(constructorName, constructorTypeStr);
char* objectClassName = "java/lang/Object";
struct Constant* objectClassUtf8 = addUtf8ToConstantsTable(objectClassName);
struct Constant* constantObjectClass = addClassToConstantsTable(objectClassName);
objectConstructorMethodRef = (struct Constant*) malloc(sizeof(struct Constant));
int size = list_size(constantsTable);
//add method ref to constants table
objectConstructorMethodRef->type = CONSTANT_Methodref;
objectConstructorMethodRef->const1 = constantObjectClass;
objectConstructorMethodRef->const2 = constructorNameAndType;
objectConstructorMethodRef->id = size + 1;
list_add(constantsTable, objectConstructorMethodRef);
return constantObjectClass;
}
//check and add function to method table
bool checkSemanticFunctionDecl(struct FunctionDecl* functionDecl) {
bool isOk = true;
struct Method* method = NULL; // (struct Method*)malloc(sizeof(struct Method));
//check for existing of method reference and add method to methods table + constants table if it does not exist
if (hashtable_get(semanticClass->methodsTable, functionDecl->identifier, &method) != CC_OK) {
//check semantic of parameter list
struct ParameterList* paramList = functionDecl->signature->paramInParen->paramList;
if (paramList != NULL) {
isOk = checkSemanticParamList(paramList, functionDecl->identifier);
if (!isOk) {
printf("Semantic error. Unsupport types in parameter declaration in function %s\n", functionDecl->identifier);
return false;
}
}
struct SemanticType* semanticReturnType = getFunctionReturnType(functionDecl);
if (semanticReturnType->typeName == UNKNOWN_TYPE) {
printf("Semantic error. Unsupport return type in function %s\n", functionDecl->identifier);
return false;
}
if (functionDecl->block == NULL) {
printf("Body of function %s not found", functionDecl->identifier);
return false;
}
//add return type to constant table
char* returnTypeStr = convertTypeToString(semanticReturnType);
///addUtf8ToConstantsTable(returnTypeStr);
//add name and type to constants table
char* methodDescriptor = createMethodDescriptor(paramList, returnTypeStr);
//add method ref to constants table
struct Constant* constMethodRef = addMethodRefToConstantsTable(functionDecl->identifier, methodDescriptor);
//create method
method = (struct Method*)malloc(sizeof(struct Method));
method->constMethodref = constMethodRef;
method->returnType = semanticReturnType;
method->paramList = paramList;
method->functionDecl = functionDecl;
method->isStatic = true;
char* methodName = method->constMethodref->const2->const1->utf8;
int isMethodMain = strcmp(methodName, "main") == 0;
/*-----------------*/
//add method to methodsTable of class
hashtable_add(methodsTable, functionDecl->identifier, method);
Array* out;
array_new(&out);
hashtable_get_keys(methodsTable, &out);
int size = hashtable_size(methodsTable);
//add variable to local variables table
list_new(&method->localVariablesTable);
if (paramList != NULL) {
struct ParameterDeclare* param = paramList->firstParamDecl;
while (param != NULL && isOk) {
//addUtf8ToConstantsTable(param->identifier);
struct SemanticType* paramSemanticType = (struct SemanticType*) malloc(sizeof(struct SemanticType));
paramSemanticType->typeName = param->type->typeName;
if (param->type->expr != NULL) {
paramSemanticType->arrayType = ARRAY;
}
else {
paramSemanticType->arrayType = NONE_ARRAY;
}
char* paramTypeStr = convertTypeToString(paramSemanticType);
//addUtf8ToConstantsTable(paramTypeStr);
isOk = addParamToLocalVarsTable(param->identifier, paramSemanticType, method);
if (isOk) {
param = param->nextParamDecl;
}
else {
printf("Variable with the same name in function %s has been defined\n", functionDecl->identifier);
}
}
}
if (isContainStatementType(functionDecl->block->stmtList, BREAK_STMT)) {
printf("Semantic Error. Invalid break statement in function definition\n");
return false;
}
else if (isContainStatementType(functionDecl->block->stmtList, CONTINUE_STMT)) {
printf("Semantic Error. Invalid continue statement in function definition\n");
return false;
}
//check semantic of body
isOk = checkSemanticBlock(functionDecl->block, method);
if (!isOk) {
return false;
}
if (semanticReturnType->typeName != UNKNOWN_TYPE && semanticReturnType->typeName != VOID_TYPE_NAME) {
/*isOk = isContainStatementType(functionDecl->block->stmtList, RETURN_STMT);
if (! isOk ){
printf("Semantic error. Function %s should return a value\n", functionDecl->identifier);
}*/
isOk = true;
}
}
else {
printf("Function with the same name as %s has been define\n", functionDecl->identifier);
isOk = false;
}
//TODO:check for compatible of return type in the body and in signature
return isOk;
}
//Here, propose that the semantic of param list has been check
char* createMethodDescriptor(struct ParameterList* paramList, char* returnTypeStr) {
char* result = (char*)malloc(100 * sizeof(char));
if (paramList == NULL) {
strcpy(result, "()");
strcpy(result + 2, returnTypeStr);
}
else {
struct ParameterDeclare* param = paramList->firstParamDecl;
char* ptr = result;
strcpy(ptr, "(");
ptr += 1;
while (param != NULL) {
struct Type* type = param->type;
struct SemanticType* semanticType = (struct SemanticType*)malloc(sizeof(struct SemanticType));
semanticType->typeName = type->typeName;
if (type->expr != NULL) {
semanticType->arrayType = ARRAY;
}
else {
semanticType->arrayType = NONE_ARRAY;
}
char* typeNameStr = convertTypeToString(semanticType);
strcpy(ptr, typeNameStr);
ptr += strlen(typeNameStr);
param = param->nextParamDecl;
}
strcpy(ptr, ")");
ptr += 1;
strcpy(ptr, returnTypeStr);
}
return result;
}
bool checkSemanticParamList(struct ParameterList* paramList, char* functionName) {
struct ParameterDeclare* param = paramList->firstParamDecl;
bool isOk = true;
while (param != NULL && isOk) {
//check for existing of param name
if (param->identifier == NULL) {
printf("Semantic error. Parameter name not given. Function %s\n", functionName);
isOk = false;
}
//check type of param
struct Type* type = param->type;
if (type->expr != NULL) {
//primitive type
if (type->typeName == INT_TYPE_NAME || type->typeName == FLOAT32_TYPE_NAME || type->typeName == STRING_TYPE_NAME) {
if (type->expr->exprType != PRIMARY || type->expr->primaryExpr->exprType != DECIMAL_EXPR) {
printf("Semantic error. Array index should be integer type %s \n", functionName);
isOk = false;
}
else {
addIntegerToConstantsTable(type->expr->primaryExpr->decNumber);
param = param->nextParamDecl;
}
}
else {
printf("Semantic error. Unsupport type in param list of function %s \n", functionName);
isOk = false;
}
}
else {
//primitive type
if (type->typeName == INT_TYPE_NAME || type->typeName == FLOAT32_TYPE_NAME || type->typeName == STRING_TYPE_NAME) {
param = param->nextParamDecl;
}
else {
printf("Semantic error. Unsupport type in param list of function %s \n", functionName);
isOk = false;
}
}
}
return isOk;
}
//return type should be one of the following: INT, FLOAT, STRING, VOID,// INT[], FLOAT[], STRING[]
//the format is (), (INT|FLOAT|STRING), INT, FLOAT, STRING
struct SemanticType* getFunctionReturnType(struct FunctionDecl* functionDecl) {
struct SemanticType* semanticType = (struct SemanticType*) malloc(sizeof(struct SemanticType));
semanticType->typeName = UNKNOWN_TYPE;
if (functionDecl->signature->result != NULL) {
struct Result* returnType = functionDecl->signature->result;
if (returnType->type != NULL) {
//INT, FLOAT, STRING, VOID,// INT[], FLOAT[], STRING[]
struct Type* type = returnType->type;
semanticType->typeName = type->typeName;
if (type->expr != NULL) {
if (type->expr->exprType != PRIMARY || type->expr->primaryExpr->exprType != DECIMAL_EXPR) {
semanticType->typeName = UNKNOWN_TYPE;
}
else {
semanticType->arrayType = ARRAY;
}
}
else {
semanticType->arrayType = NONE_ARRAY;
}
}
else {
//returnType->paramInParent != NULL
if (returnType->paramInParen->paramList != NULL) {
struct ParameterList* paramList = returnType->paramInParen->paramList;
//(int),(float), (string), ([4]int), ([4]float), ([5]string)
//(a int) will not be accepted
if (paramList->size == 1 && paramList->firstParamDecl->identifier == NULL) {
struct Type* type = paramList->firstParamDecl->type;
if (type->expr != NULL ) {
if (type->expr->exprType == PRIMARY && type->expr->primaryExpr->exprType == DECIMAL_EXPR) {
semanticType->typeName = type->typeName;
semanticType->arrayType = ARRAY;
}
else {
semanticType->typeName = UNKNOWN_TYPE;
}
}
else {
semanticType->typeName = type->typeName;
semanticType->arrayType = NONE_ARRAY;
}
}
}
else {
semanticType->typeName = VOID_TYPE_NAME;
}
}
}
else {
//void type
semanticType->typeName = VOID_TYPE_NAME;
}
return semanticType;
}
bool checkSemanticBlock(struct Block* block, struct Method* method) {
//increase scope
scope++;
bool isOk = true;
if (block->stmtList == NULL) {
return true;
}
struct Statement* stmt = block->stmtList->firstStmt;
while (stmt != NULL && isOk ) {
isOk = checkSemanticStmt(stmt,method);
if (isOk) {
stmt = stmt->nextStatement;
}
}
deactivateLocalVariablesByScope(method->localVariablesTable, scope);
//decrease scope
scope--;
return isOk;
}
void deactivateLocalVariablesByScope(List* localVariablesTable, int scope) {
struct LocalVariable* variable = NULL;
int size = list_size(localVariablesTable);
for (int i = 0; i < size; ++i) {
list_get_at(localVariablesTable, i, &variable);
if (variable->scope == scope && variable->isActive) {
variable->isActive = false;
}
}
}
bool checkSemanticStmt(struct Statement* statement, struct Method* method) {
switch (statement->stmtType) {
case SIMPLE_STMT: {
return checkSemanticSimpleStmt(statement->simpleStmt, method);
}
case VAR_DECL_STMT: {
return checkSemanticVarDecl(statement->varDecl, method);
}
case CONST_DECL_STMT: {
return checkSemanticConstDecl(statement->constDecl, method);
}
case BLOCK: {
return checkSemanticBlock(statement->block, method);
}
case IF_STMT: {
return checkSemanticIfStmt(statement->ifStmt, method);
}
case SWITCH_STMT: {
return checkSemanticSwitchStmt(statement->switchStmt, method);
}
case FOR_STMT: {
return checkSemanticForStmt(statement->forStmt, method);
}
case PRINT_STMT: {
return checkSemanticPrintStmt(statement->printStatement, method);
}
case SCAN_STMT: {
return checkSemanticScanStmt(statement->scanStatement, method);
}
case RETURN_STMT: {
return checkSemanticReturnStmt(statement->returnStmt, method);
}
}
return true;
}
bool checkSemanticSimpleStmt(struct SimpleStmt* simpleStmt, struct Method* method) {
switch(simpleStmt->stmtType)
{
case EXPR_SIMPLE_STMT:
{
if (simpleStmt->expr != NULL && simpleStmt->expr->primaryExpr != NULL && simpleStmt->expr->primaryExpr->funcCall != NULL) {
struct FunctionCall* functionCall = simpleStmt->expr->primaryExpr->funcCall;
char* methodName = functionCall->primaryExpr->identifier;
struct Method* calledMethod = getMethod(methodName);
if (calledMethod != NULL) {
if (checkSemanticFunctionCall(functionCall->exprList, calledMethod->paramList, method)) {
if (calledMethod->returnType->typeName != VOID_TYPE_NAME) {
printf("Semantic error. Expression evaluated but not used\n");
return false;
}
else {
struct SemanticType* semanticType = (struct SemanticType*)malloc(sizeof(struct SemanticType));
semanticType->typeName = VOID_TYPE_NAME;
simpleStmt->expr->semanticType = semanticType;
}
}
else {
return false;
}
}
else {
printf("Semantic error. Method %s not declared\n", methodName);
return false;
}
}
else {
printf("Semantic error. Expression evaluated but not used\n");
return false;
}
return true;
}
case INC_SIMPLE_STMT:
case DEC_SIMPLE_STMT:
{
if (simpleStmt->expr->exprType == PRIMARY) {
struct SemanticType * semanticType = checkExpressionType(simpleStmt->expr, method);
if (semanticType->typeName != INT_TYPE_NAME) {
printf("Semantic error. Increase or decrease statement can only perform in INT-TYPE variable\n");
return false;
}
}
else {
printf("Semantic error. Increase or decrease statement can only perform on variable\n");
return false;
}
return true;
}
case ASSIGN_STMT:
{
return checkSemanticAssignStmtList(simpleStmt->exprListLeft, simpleStmt->exprListRight, method);
}
case PLUS_ASSIGN_STMT:
case MINUS_ASSIGN_STMT:
case MUL_ASSIGN_STMT:
case DIV_ASSIGN_STMT: {
if (exprListSize(simpleStmt->exprListLeft) == 1 && exprListSize(simpleStmt->exprListRight) == 1)
{
return checkSemanticAssignStmt(simpleStmt->exprListLeft->firstExpression, simpleStmt->exprListRight->firstExpression, method);
}
else {
printf("Semantic error. Multiple arithmetic assign is not supported. \n");
return false;
}
}
default:
return true;
}
}
//not supported
bool checkSemanticAssignStmtList(struct ExpressionList* leftExprList, struct ExpressionList* rightExprList, struct Method* method)
{
bool isOk = true;
if (exprListSize(leftExprList) == exprListSize(rightExprList))
{
struct Expression *leftExpr = leftExprList->firstExpression;
struct Expression *rightExpr = rightExprList->firstExpression;
//Dont need to check for isOk
while(leftExpr != NULL && isOk)
{
isOk = checkSemanticAssignStmt(leftExpr, rightExpr, method);
if (isOk) {
leftExpr = leftExpr->nextExpr;
rightExpr = rightExpr->nextExpr;
}
}
}
else
{
printf("Assignment count mismatch \n");
isOk = false;
}
return isOk;
}
bool checkSemanticAssignStmt(struct Expression* leftExpr, struct Expression* rightExpr, struct Method* method){
bool isOk = true;
struct SemanticType* leftType = checkExpressionType(leftExpr, method);
struct SemanticType* rightType = checkExpressionType(rightExpr, method);
isOk = (leftType->typeName == rightType->typeName && leftType->typeName != UNKNOWN_TYPE);
if (!isOk) {
printf("Semantic error. Type mismatch in assign statement\n");
}
isOk = (leftExpr->exprType == PRIMARY && leftExpr->primaryExpr->exprType == ID_EXPRESSION ||
leftExpr->exprType == PRIMARY && leftExpr->primaryExpr->exprType == PE_COMPOSITE
);
if (!isOk) {
printf("Semantic error. Left expression of assignment statement must be an identifier or array access\n");
}
return isOk;
}
bool checkSemanticIfStmt(struct IfStmt* ifStmt, struct Method* method) {
bool isOk = true;
if (ifStmt->ifStmtExpr->simpleStmt != NULL) {
printf("Semantic error. Simple statement before expression of if is not accepted. \n");
isOk = false;
}
else {
struct SemanticType* semanticType = checkExpressionType(ifStmt->ifStmtExpr->expr, method);
if (semanticType->typeName != BOOL_TYPE_NAME) {
if (semanticType->typeName != UNKNOWN_TYPE) {
printf("Semantic error. Non bool used as if condition\n");
}
isOk = false;
}
/*else {
if (isContainStatementType(ifStmt->block->stmtList, BREAK_STMT)) {
printf("Semantic Error. Invalid break statement inside if statement\n");
isOk = false;
}
else if (isContainStatementType(ifStmt->block->stmtList, CONTINUE_STMT)) {
printf("Semantic Error. Invalid continue statement inside if statement\n");
isOk = false;
}
}*/
//check semantic block of if
if (isOk) {
isOk = checkSemanticBlock(ifStmt->block, method);
if (isOk && ifStmt->elseBlock != NULL) {
isOk = checkSemanticElseBlock(ifStmt->elseBlock, method);
}
}
}
return isOk;
}
bool checkSemanticElseBlock(struct ElseBlock* elseBlock, struct Method* method) {
bool isOk = true;
if(elseBlock->ifStmt != NULL){
isOk = checkSemanticIfStmt(elseBlock->ifStmt, method);
}
else {
if (isContainStatementType(elseBlock->block->stmtList, BREAK_STMT)) {
printf("Semantic Error. Invalid break statement inside else statement\n");
return false;
}
else if (isContainStatementType(elseBlock->block->stmtList, CONTINUE_STMT)) {
printf("Semantic Error. Invalid continue statement inside else statement\n");
return false;
}
if (isOk) {
isOk = checkSemanticBlock(elseBlock->block, method);
}
}
return isOk;
}
bool checkSemanticSwitchStmt(struct SwitchStmt* switchStmt, struct Method* method) {
bool isOk = true;
struct SemanticType* initExprSemanticType = (struct SemanticType*) malloc(sizeof(struct SemanticType));
initExprSemanticType->typeName = UNKNOWN_TYPE;
if (switchStmt->initialAndExpression->switchType == WITH_INITIAL_STMT) {
printf("Semantic error. Switch can not start with initial statement without expression\n");
return false;
}
if (switchStmt->initialAndExpression != NULL)
{
if (switchStmt->initialAndExpression->initialStmt != NULL) {
struct SimpleStmt* initStmt = switchStmt->initialAndExpression->initialStmt;
if (initStmt->stmtType == EXPR_SIMPLE_STMT) {
printf("Semantic error. Initial expression of switch statement cannot be expression\n");
isOk = false;
}
else {
isOk = checkSemanticSimpleStmt(switchStmt->initialAndExpression->initialStmt, method);
}
}
if (isOk && switchStmt->initialAndExpression->expression != NULL) {
struct PrimaryExpression* primaryExpr = switchStmt->initialAndExpression->expression->primaryExpr;
if (primaryExpr != NULL && primaryExpr->identifier != 0) {
//what do to with semanticType (probably nothing :v)
initExprSemanticType = checkExpressionType(switchStmt->initialAndExpression->expression, method);
if (initExprSemanticType->typeName != INT_TYPE_NAME) {
printf("Semantic error. Non-integer value used as index in expession of switch statement\n");
isOk = false;
}
}
else {
printf("Semantic error. Expression of switch statement should be an identifier\n");
isOk = false;
}
}
}
//empty switch initial statement and expression -> nothing to do :v
//check semantic of switch's body
if (switchStmt->switchBody == NULL) {
printf("Semantic error. Body of switch statement is empty\n");
isOk = false;
}
if (isOk) {
if (switchStmt->switchBody->eccl != NULL) {
scope++;
struct ExpressionCaseClauseList* exprCaseClauseList = switchStmt->switchBody->eccl;
struct ExpressionCaseClause* ecc = exprCaseClauseList->firstExprCaseClause;
while (ecc != NULL && isOk) {
isOk = checkSemanticExpressionCaseClause(ecc, initExprSemanticType->typeName, method);
ecc = ecc->nextExprCaseClause;
}
scope--;
if (exprCaseClauseList->defaultCount > 1) {
printf("Semantic error. Multiple default in switch\n");
isOk = false;
}
}
}
return isOk;
}
bool checkSemanticExpressionCaseClause(struct ExpressionCaseClause *ecc, enum TypeNames identifierType, struct Method* method)
{
bool isOk = true;
if (ecc->expreSwitchCase->exprList != NULL) {
//check semantic for expression list in case
struct ExpressionList* exprList = ecc->expreSwitchCase->exprList;
struct Expression* expr = exprList->firstExpression;
struct SemanticType* semanticType = NULL;
while (expr != NULL && isOk) {
semanticType = checkExpressionType(expr, method);
if (expr->exprType != PRIMARY || expr->primaryExpr->exprType != DECIMAL_EXPR) {
printf("Semantic error. Value of expression in case is non-integer type\n");
isOk = false;
}
if (semanticType->typeName != INT_TYPE_NAME) {
printf("Semantic error. Value of expression in case is non-integer type\n");
isOk = false;
}
if (isOk) {
expr = expr->nextExpr;
}
}
}
if (isOk) {
//check semantic body of case
//check whether this is case or default
if (ecc->expreSwitchCase->exprList != NULL) {
//add break statement to end of list
struct Statement* lastStmt = ecc->stmtList->lastStmt;
struct Statement* firstStmt = ecc->stmtList->firstStmt;
struct Statement* breakStmt = (struct Statement*) malloc(sizeof(struct Statement));
breakStmt->stmtType = BREAK_STMT;
breakStmt->nextStatement = NULL;
if (lastStmt == NULL) {
ecc->stmtList->firstStmt = breakStmt;
ecc->stmtList->lastStmt = breakStmt;
}
else {
ecc->stmtList->lastStmt->nextStatement = breakStmt;
ecc->stmtList->lastStmt = breakStmt;
}
ecc->stmtList->size += 1;
}
struct Statement* stmt = ecc->stmtList->firstStmt;
if (isContainStatementType(ecc->stmtList, CONTINUE_STMT)) {
printf("Semantic error. Invalid continue statement in body of switch statement\n");
isOk = false;