-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsyntacticalanalyzer.c
1236 lines (1056 loc) · 46.2 KB
/
syntacticalanalyzer.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
/**
* Implementace interpretu imperativního jazyka IFJ16.
*
* Jakub Fajkus
* Richard Bureš
* Andrej Hučko
* David Czernin
*/
#include "syntacticalanalyzer.h"
#include "ifj16.h"
#include "debug.h"
#include "semanticalanalyzer.h"
#include "interpret.h"
tDLList *globalTokens;
struct SYMBOL_TABLE_NODE *globalSymbolTable;
struct SYMBOL_TABLE_FUNCTION_STR *actualFunction;
struct tDLListStruct *mainInstructionList;
struct tDLListStruct *actualInstructionList;
char* actualClass;
bool firstPass = true;
/**
* Return 'count' number of tokens.
* If the count equals to one, the actual token is returned and the next token returned by funtion getCachedToken() is the same token as it was returned the last time.
*
* Example: tokens are:
* tok1 -> tok2 -> tok3
* last returned token was "tok2". you call the function returnCachedTokens() with count= 1 and the next token retunred by function getCachedToken() will be again "tok2"
* @param count
*/
void returnCachedTokens(unsigned int count);
/**
* Get all tokend from the lexical analyser
*
* @param fileName
* @return
*/
tDLList* getAllTokens(char *fileName);
void makeFirstPass();
void makeSecondPass();
void addIfj16Functions();
void printAll();
TOKEN *getCachedToken() {
LIST_ELEMENT *element = malloc(sizeof(LIST_ELEMENT));
TOKEN *token;
//copy the active element
ListElementCopy(globalTokens, element);
token = element->data.token;
//move the activity to the next element
ListSuccessor(globalTokens);
//free the container
free(element);
return token;
}
void returnCachedTokens(unsigned int count) {
for (int i = 0; i < count; ++i) {
ListPredcessor(globalTokens);
}
}
tDLList* getAllTokens(char *fileName) {
tDLList *listOfTokens = malloc(sizeof(tDLList));
ListInit(listOfTokens);
initializeStream(fileName);
TOKEN *token;
do {
token = getToken();
LIST_ELEMENT_DATA *data = malloc(sizeof(LIST_ELEMENT_DATA));
data->token = token;
struct LIST_ELEMENT *listElement = malloc(sizeof(LIST_ELEMENT));
listElement->data = *data;
listElement->type = LIST_ELEMENT_TYPE_TOKEN;
ListInsertLast (listOfTokens, *listElement);
} while(token->type != END_OF_FILE);
ListFirst(listOfTokens);
return listOfTokens;
}
/**
* no instruction to generate
*/
bool ruleId(char **name) {
TOKEN *token = getCachedToken();
if (token->type == IDENTIFIER) {
*name = token->data.identifier.name;
debugPrintf("id: %s\n", *name);
return true;
}
if(token->type == IDENTIFIER_FULL) {
char *classWithDot = stringConcat(token->data.identifierFull.class, ".");
char *fullName = stringConcat(classWithDot, token->data.identifierFull.name);
free(classWithDot);
*name = fullName;
debugPrintf("id: %s\n", *name);
return true;
} else {
returnCachedTokens(1);
return false;
}
}
/**
* no instruction to generate
*/
bool ruleTypeString(DATA_TYPE *type) {
TOKEN *token = getCachedToken();
if (token->type == KEYWORD && stringEquals(token->data.keyword.name, "String")) {
*type = TYPE_STRING;
return true;
} else {
returnCachedTokens(1);
return false;
}
}
/**
* no instruction to generate
*/
bool ruleTypeDouble(DATA_TYPE *type) {
TOKEN *token = getCachedToken();
if (token->type == KEYWORD && stringEquals(token->data.keyword.name, "double")) {
*type = TYPE_DOUBLE;
return true;
} else {
returnCachedTokens(1);
return false;
}
}
/**
* no instruction to generate
*/
bool ruleTypeInt(DATA_TYPE *type) {
TOKEN *token = getCachedToken();
if (token->type == KEYWORD && stringEquals(token->data.keyword.name, "int")) {
*type = TYPE_INT;
return true;
} else {
returnCachedTokens(1);
return false;
}
}
/**
* Instruction_Create_Global_Variable for CLASS.*
*/
bool ruleProg(){
tDLElemPtr activeElementRuleApplication = globalTokens->Act;
TOKEN *token = getCachedToken();
char *className;
//<PROG> -> class <ID> {<CLASS_DEFINITION> } <PROG>
if (token->type == KEYWORD && stringEquals(token->data.keyword.name, "class")) {
if (ruleId(&className)) {
actualClass = className;
if(firstPass) {
//insert a record for the class itself
createAndInsertIntVariable(&globalSymbolTable, stringConcat(className, ".*"), false);
//create instruction to insert global variable
ListInsertLast(mainInstructionList, wrapInstructionIntoListElement(createGlobalVariable(stringConcat(className, ".*"), TYPE_INT)));
}
token = getCachedToken();
if (token->type == BRACKET && token->data.bracket.name == '{') {
if (ruleClassDefinition(className)) {
token = getCachedToken();
if (token->type == BRACKET && token->data.bracket.name == '}') {
if (ruleProg()) {
return true;
} else {
//rule prog was not applied, but it has an epsilon rule... so check if the token is EOF(in this particular case...)
//if so, the source file ended and the syntax analysis is successful
token = getCachedToken();
if (token->type == END_OF_FILE) {
//no need to return any token, the source file is over
return true;
} else {
//the token was not an EOF so there were some other tokens but they did not match the rule prog
//so the syntax analysis is over because of syntax error
globalTokens->Act = activeElementRuleApplication;
return false;
}
}
}
}
}
}
} else if (token->type == END_OF_FILE) {
return true;
}
globalTokens->Act = activeElementRuleApplication;
return false;
}
/**
* no instruction to generate
*/
bool ruleClassDefinition(char *className){
tDLElemPtr activeElementRuleApplication = globalTokens->Act;
TOKEN *token = getCachedToken();
actualFunction = NULL;
//<CLASS_DEFINITION> -> static <DEFINITION_START> <CLASS_DEFINITION>
if (token->type == KEYWORD && stringEquals(token->data.keyword.name, "static")) {
if (ruleDefinitionStart(className)) {
//recursive call
if (ruleClassDefinition(className)) {
return true;
} else {
globalTokens->Act = activeElementRuleApplication;
return false;
}
} else {
globalTokens->Act = activeElementRuleApplication;
return false;
}
} else {
//rule prog was not applied, but it has an epsilon rule... so check if the token is '}'(in this particular case...)
//if so, the class definition ended and rule is applied
if (token->type == BRACKET && token->data.bracket.name == '}') {
returnCachedTokens(1);
return true;
} else {
//the token was not an '}' so there were some other tokens but they did not match the rule
//so the syntax analysis is over because of syntax error
globalTokens->Act = activeElementRuleApplication;
return false;
}
}
}
bool ruleDefinition(char *className, DATA_TYPE type, char *functionOrPropertyName, bool *variableInitialized, bool *isFunction){
//<DEFINITION> -> <PROP_DEF>
//<DEFINITION> -> <FUNC_DEF>
if (rulePropDef(variableInitialized, type, functionOrPropertyName, className)) {
*isFunction = false;
return true;
} else {
char *classNameWithDot = stringConcat(className, ".");
struct SYMBOL_TABLE_FUNCTION_STR *function = NULL;
if(firstPass) {
function = semanticCreateAndInsertFunction(&globalSymbolTable, stringConcat(classNameWithDot, functionOrPropertyName), type, 0, NULL, NULL, 0);
} else {
function = semantic_getFunction(stringConcat(classNameWithDot, functionOrPropertyName));
}
actualFunction = function;
actualInstructionList = function->instructions;
if(ruleFuncDef()) {
*isFunction = true;
return true;
} else {
*isFunction = false;
return false;
}
}
}
//only for static variables
bool rulePropDef(bool *variableInitialized, DATA_TYPE variableType, char *variableName, char *className) {
tDLElemPtr activeElementRuleApplication = globalTokens->Act;
TOKEN *token = getCachedToken();
*variableInitialized = false;
char *fullyQualifiedVariableName = stringConcat(stringConcat(className, "."), variableName);
if (token->type == SEMICOLON) {
if (firstPass) {
createAndInsertVariable(&globalSymbolTable, fullyQualifiedVariableName, variableType, false);
ListInsertLast(mainInstructionList, wrapInstructionIntoListElement(createGlobalVariable(fullyQualifiedVariableName, variableType)));
// noo need to check type
}
return true;
} else if (token->type == OPERATOR_ASSIGN) {
//<PROP_DEF> -> = <EXP>;
//the check should return true if the expression was successfully parsed, false otherwise
//the check should set the active token to the token which is right after the expression(example: tokens:"3+4)", the active token should be ')'
debugPrintf("calling analyzeExpression from rulePropDef\n");
char* resultVariableName = NULL;
DATA_TYPE resultVariableType;
debugPrintf("calling analyzeExpression from rulePropDef\n");
if (analyzeExpression(mainInstructionList, &resultVariableName, &resultVariableType)) {
token = getCachedToken();
if (token->type == SEMICOLON) {
*variableInitialized = true;
if(firstPass) {
ListInsertLast(mainInstructionList, wrapInstructionIntoListElement(createGlobalVariable(fullyQualifiedVariableName, variableType)));
semanticCreateAndInsertVariable(&globalSymbolTable, fullyQualifiedVariableName, variableType, true);
} else{
if(!canConvertTypes(variableType, resultVariableType)) {
fprintf(stderr, "Incompatible types for assign");
exit(4);
}
ListInsertLast(mainInstructionList, wrapInstructionIntoListElement(createInstrAssign(fullyQualifiedVariableName, resultVariableName)));
}
return true;
} else {
//the rule application was unsuccessful, so return the token list to the state in which it was before this function call
globalTokens->Act = activeElementRuleApplication;
return false;
}
} else {
globalTokens->Act = activeElementRuleApplication;
return false;
}
} else {
returnCachedTokens(1);
return false;
}
}
/**
* here the whole function definition takes place
* the actualFunction pointer is already set to the function which is beeing processed
*
*/
bool ruleFuncDef(){
tDLElemPtr activeElementRuleApplication = globalTokens->Act;
TOKEN *token = getCachedToken();
//<FUNC_DEF> -> ( <FUNC_DEF_PARAMS> { <ST_LIST_DECL> }
if (token->type == BRACKET && token->data.bracket.name == '(') {
if (ruleFuncDefParams()) {
token = getCachedToken();
if (token->type == BRACKET && token->data.bracket.name == '{') {
if (ruleStListDecl()) {
token = getCachedToken();
if (token->type == BRACKET && token->data.bracket.name == '}') {
return true;
}
} else {
//rule stListDeclaration was not applied, but it has an epsilon rule... so check if the token is '}'(in this particular case...)
//if so, the source the rule is applied
//note tha the condition with the bracket is not duplicated as it seems... the condition below works with different tokens
//the bracket below is the only token after the stListDecl which is allowed and represents the rule
token = getCachedToken();
if (token->type == BRACKET && token->data.bracket.name == '}') {
//return the bracket
returnCachedTokens(1);
return true;
} else {
//the token was not an '}' so there were some other tokens
//so the syntax analysis is over because of syntax error
globalTokens->Act = activeElementRuleApplication;
return false;
}
}
}
}
}
globalTokens->Act = activeElementRuleApplication;
return false;
}
/**
* the body of each function
* actualFunction is set already
*
* there will be plenty of instructions!
*/
bool ruleStListDecl(){
//<ST_LIST_DECL> -> <TYPE><ID><DECL><ST_LIST_DECL>
DATA_TYPE type;
char *name;
if (ruleTypeDouble(&type) || ruleTypeInt(&type) || ruleTypeString(&type)) {
if (ruleId(&name)) {
if(ifj16_find(name, ".") != -1) {
exit(3);
}
if (ruleDecl(type, name)) {
//recursive call
if (ruleStListDecl()) {
return true;
}
}
}
//<ST_LIST_DECL> -> <STAT> <ST_LIST_DECL>
} else if (ruleStat()) {
if (ruleStListDecl()) {
return true;
}
} else {
TOKEN *token = getCachedToken();
//<ST_LIST_DECL> -> { <ST_LIST> }
if (token->type == BRACKET && token->data.bracket.name == '{') {
if (ruleStList()) {
token = getCachedToken();
if (token->type == BRACKET && token->data.bracket.name == '}') {
return true;
}
}
//<ST_LIST> -> EPSILON
} else if(token->type == BRACKET && token->data.bracket.name == '}') {
returnCachedTokens(1);
return true;
// FAIL
} else {
returnCachedTokens(1);
return false;
}
}
return false;
}
bool ruleStList(){
//<ST_LIST> -> <STAT> <ST_LIST>
if (ruleStat()) {
if (ruleStList()) {
return true;
}
} else {
TOKEN *token = getCachedToken();
//<ST_LIST>-> { <ST_LIST> }
if (token->type == BRACKET && token->data.bracket.name == '{') {
if (ruleStList()) {
if (token->type == BRACKET && token->data.bracket.name == '}') {
return true;
}
}
//<ST_LIST> -> EPSILON
} else if(token->type == BRACKET && token->data.bracket.name == '}') {
returnCachedTokens(1);
return true;
// FAIL
}
}
returnCachedTokens(1);
return false;
}
/**
* function for declaration of local variable
* actualFunction is already set
*
* Instruction_Create_Local_Variable
* Instruction_Assign
*/
bool ruleDecl(DATA_TYPE declaredType, char *variableName){
tDLElemPtr activeElementRuleApplication = globalTokens->Act;
TOKEN *token = getCachedToken();
if (token->type == SEMICOLON) {
if (!firstPass) {
semanticCreateAndInsertVariable(&actualFunction->localSymbolTable, variableName, declaredType, false);
//no need to check types
ListInsertLast(actualInstructionList, wrapInstructionIntoListElement(createActualLocalVariable(variableName, declaredType)));
}
return true;
} else if (token->type == OPERATOR_ASSIGN) {
//<DECL> -> = <EXP>;
//the check should return true if the expression was successfully parsed, false otherwise
//the check should set the active token to the token which is right after the expression(example: tokens:"3+4)", the active token should be ')'
char* resultVariableName = NULL;
DATA_TYPE resultVariableType;
debugPrintf("calling analyzeExpression from ruleDecl\n");
//no function call allowed! thank god...
if (analyzeExpression(actualInstructionList, &resultVariableName, &resultVariableType)) {
//the resultVariableName contains the variable name which contains the result of the expression
token = getCachedToken();
//<DECL> -> ;
if (token->type == SEMICOLON) {
if (!firstPass) {
//insert the local variable to the symbol table
semanticCreateAndInsertVariable(&actualFunction->localSymbolTable, variableName, declaredType, true);
ListInsertLast(actualInstructionList, wrapInstructionIntoListElement(createActualLocalVariable(variableName, declaredType)));
if(!canConvertTypes(declaredType, resultVariableType)) {
fprintf(stderr, "Incompatible types for assign");
exit(4);
}
//create instruction to assign the temporal variable created by expAnalyzer to the local variable which was defined right now
ListInsertLast(actualInstructionList, wrapInstructionIntoListElement(createInstrAssign(variableName, resultVariableName)));
}
return true;
} else {
//the rule application was unsuccessful, so return the token list to the state in which it was before this function call
globalTokens->Act = activeElementRuleApplication;
return false;
}
} else {
globalTokens->Act = activeElementRuleApplication;
return false;
}
} else {
returnCachedTokens(1);
return false;
}
}
/**
* one statement in a function body
*/
bool ruleStat(){
tDLElemPtr activeElementRuleApplication = globalTokens->Act;
char *functionOrVariableName = NULL;
//<STAT> -> <ID><STAT_BEGINNING_ID>;
if (ruleId(&functionOrVariableName)) {
//neco?
//char *calle
if(ruleStatBeginningId(functionOrVariableName)){
TOKEN *token = getCachedToken();
if(token->type == SEMICOLON) {
return true;
}
}
} else {
TOKEN *token = getCachedToken();
//<STAT> -> while ( <EXP> ) { <ST_LIST> }
if (token->type == KEYWORD && stringEquals(token->data.keyword.name, "while")) {
tDLList *actualInstructionListBackup = actualInstructionList;
tDLList *whileBodyList = malloc(sizeof(tDLList));
ListInit(whileBodyList);
tDLList *whileConditionList = malloc(sizeof(tDLList));
ListInit(whileConditionList);
token = getCachedToken();
if (token->type == BRACKET && token->data.bracket.name == '(') {
char* resultVariableName = NULL;
DATA_TYPE resultVariableType = TYPE_INT; //just some default value
actualInstructionList = whileConditionList;
debugPrintf("calling analyzeExpression from ruleStat <STAT> -> while ( <EXP> ) { <ST_LIST> }\n");
if (analyzeExpression(actualInstructionList, &resultVariableName, &resultVariableType)) {
if(!firstPass && resultVariableType != TYPE_BOOL) {
fprintf(stderr, "variable used in the confition of an while statement must be of type bool, type: %d given", resultVariableType);
exit(4);
}
token = getCachedToken();
actualInstructionList = actualInstructionListBackup;
if (token->type == BRACKET && token->data.bracket.name == ')') {
token = getCachedToken();
if (token->type == BRACKET && token->data.bracket.name == '{'){
actualInstructionList = whileBodyList;
if (ruleStList()) {
token = getCachedToken();
if (token->type == BRACKET && token->data.bracket.name == '}') {
if(!firstPass){
ListInsertLast(actualInstructionListBackup, wrapInstructionIntoListElement(createInstrWhile(resultVariableName, whileConditionList, whileBodyList)));
}
actualInstructionList = actualInstructionListBackup;
return true;
}
}
actualInstructionList = actualInstructionListBackup;
}
}
}
actualInstructionList = actualInstructionListBackup;
}
//<STAT> -> if ( <EXP> ) { <ST_LIST> } else { <ST_LIST> }
} else if (token->type == KEYWORD && stringEquals(token->data.keyword.name, "if")) {
tDLList *actualInstructionListBackup = actualInstructionList;
tDLList *trueList = malloc(sizeof(tDLList));
ListInit(trueList);
tDLList *falseList = malloc(sizeof(tDLList));
ListInit(falseList);
token = getCachedToken();
if (token->type == BRACKET && token->data.bracket.name == '(') {
char* resultVariableName = NULL;
DATA_TYPE resultVariableType = TYPE_STRING; //just default value
debugPrintf("calling analyzeExpression from ruleStat <STAT> -> if ( <EXP> ) { <ST_LIST> } else { <ST_LIST> }\n");
if (analyzeExpression(actualInstructionList, &resultVariableName, &resultVariableType)) {
//result of expression, temp which would be used in createInsIf
if(!firstPass){
if(resultVariableType != TYPE_BOOL) {
fprintf(stderr, "variable used in the confition of an if statement must be of type bool, type: %d given", resultVariableType);
exit(4);
}
ListInsertLast(actualInstructionList, wrapInstructionIntoListElement(createActualLocalVariable(resultVariableName, resultVariableType)));
}
token = getCachedToken();
if (token->type == BRACKET && token->data.bracket.name == ')') {
token = getCachedToken();
if (token->type == BRACKET && token->data.bracket.name == '{'){
actualInstructionList = trueList;
//list of instructions for true
if (ruleStList()) {
token = getCachedToken();
if (token->type == BRACKET && token->data.bracket.name == '}') {
actualInstructionList = actualInstructionListBackup;
token = getCachedToken();
if (token->type == KEYWORD && stringEquals(token->data.keyword.name, "else")) {
token = getCachedToken();
if (token->type == BRACKET && token->data.bracket.name == '{'){
actualInstructionList = falseList;
//list of instructions for false
if (ruleStList()) {
token = getCachedToken();
if (token->type == BRACKET && token->data.bracket.name == '}') {
if(!firstPass){
ListInsertLast(actualInstructionListBackup, wrapInstructionIntoListElement(createInstrIf(resultVariableName, trueList, falseList)));
}
actualInstructionList = actualInstructionListBackup;
return true;
}
}
actualInstructionList = actualInstructionListBackup;
}
}
}
}
actualInstructionList = actualInstructionListBackup;
}
}
}
}
//<STAT> -> return <EXP_SEMICOLON>
} else if (token->type == KEYWORD && stringEquals(token->data.keyword.name, "return")) {
char* tempVariableName = NULL;
DATA_TYPE tempVariableType;
if (ruleExpSemicolon(&tempVariableName, &tempVariableType)) {
if(!firstPass) {
if(tempVariableName != NULL) {
if (actualFunction->type != tempVariableType) {
fprintf(stderr, "incompatible return type of function: %s", actualFunction->name);
exit(4);
}
//the ruleExpSemicolon returned a variable name. in this case the statement was: return <EXP>; and we want to generate the assign instruction
ListInsertLast(actualInstructionList, wrapInstructionIntoListElement(createActualLocalVariable(stringConcat("#", actualFunction->name), actualFunction->type)));
ListInsertLast(actualInstructionList, wrapInstructionIntoListElement(createInstrAssign(stringConcat("#", actualFunction->name), tempVariableName)));
}
ListInsertLast(actualInstructionList, wrapInstructionIntoListElement(createInstrReturnFunction()));
}
return true;
}
}
}
globalTokens->Act = activeElementRuleApplication;
return false;
}
bool ruleExpSemicolon(char **tempVariableName, DATA_TYPE *tempVariableType) {
tDLElemPtr activeElementRuleApplication = globalTokens->Act;
TOKEN *token = getCachedToken();
if (token->type == SEMICOLON) {
return true;
} else {
returnCachedTokens(1);
debugPrintf("calling analyzeExpression from ruleExpSemicolon\n");
if (analyzeExpression(actualInstructionList, tempVariableName, tempVariableType)) {
token = getCachedToken();
if (token->type == SEMICOLON){
//check return type
if(actualFunction->type == *tempVariableType){
actualFunction->hasReturn = true;
}
return true;
}
}
}
globalTokens->Act = activeElementRuleApplication;
return false;
}
bool ruleFuncCall(char *calledFunctionName, char *assignReturnValueToVariable){
tDLElemPtr activeElementRuleApplication = globalTokens->Act;
struct SYMBOL_TABLE_FUNCTION_STR *functionToCall = NULL;
//<FUNC_CALL> -> ( <FUNC_PARAMS>
TOKEN *token = getCachedToken();
if (token->type == BRACKET && token->data.bracket.name == '('){
if (!firstPass) {
//set activity to the first parameter
functionToCall = semantic_getFunction(calledFunctionName);
ListFirst(functionToCall->parameters);
} else {
functionToCall = getFunctionFromTable(&globalSymbolTable, calledFunctionName);
}
tDLList *parameters = malloc(sizeof(tDLList));
ListInit(parameters);
if (ruleFuncParams(functionToCall, parameters)) {
if(!firstPass) {
//if the called function is ifj16.print
//the function can accept int, double, string which is all datatypes
if(!(stringEquals(functionToCall->name, "ifj16.print") && DLSize(parameters) == 1)) {
semantical_checkFunctionCall(functionToCall->parameters, parameters);
}
//you have a list of PARAM *
if(isFunctionFromIfj16(calledFunctionName)) {
createInstructionsToCallIfj16Function(calledFunctionName, actualInstructionList, parameters, assignReturnValueToVariable);
} else {
ListFirst(parameters);
ListFirst(functionToCall->parameters);
ListInsertLast(actualInstructionList, wrapInstructionIntoListElement(createInstrFillLocalFrame()));
//create local variables for arguments
while(DLActive(parameters) ) {
FUNCTION_PARAMETER *param = functionToCall->parameters->Act->element.data.parameter;
ListInsertLast(actualInstructionList, wrapInstructionIntoListElement(createLocalVariable(param->name, param->type)));
ListInsertLast(actualInstructionList, wrapInstructionIntoListElement(createInstrCopyFromActualToUpcomingFrame(functionToCall->parameters->Act->element.data.parameter->name, parameters->Act->element.data.parameter->name)));
//move to the next parameter
ListSuccessor(parameters);
ListSuccessor(functionToCall->parameters);
}
//do not create local variable for void function
if(functionToCall->type != TYPE_VOID) {
ListInsertLast(actualInstructionList, wrapInstructionIntoListElement(createActualLocalVariable(stringConcat("#"/*"#"*/, functionToCall->name), functionToCall->type)));
}
ListInsertLast(actualInstructionList, wrapInstructionIntoListElement(createInstrCallFunction(functionToCall->instructions, functionToCall)));
//do not assign to local variable for void function
if(functionToCall->type != TYPE_VOID) {
//tady!
ListInsertLast(actualInstructionList, wrapInstructionIntoListElement(createInstrAssign(assignReturnValueToVariable, stringConcat("#", functionToCall->name))));
}
}
}
return true;
}
}
globalTokens->Act = activeElementRuleApplication;
return false;
}
/**
* generates
*/
bool ruleFuncParams(struct SYMBOL_TABLE_FUNCTION_STR *functionToCall, tDLList *parameters){
//<FUNC_PARAMS> -> <PARAM>
//<FUNC_PARAMS> -> <FUNCTION_CALL_END>
//create local frame of the function
if (ruleFunctionCallEnd()) {
return true;
} else if (ruleParam(functionToCall, parameters)) {
return true;
}
return false;
}
//1,4,14,15
bool ruleParam(struct SYMBOL_TABLE_FUNCTION_STR *functionToCall, tDLList *parameters){
tDLElemPtr activeElementRuleApplication = globalTokens->Act;
//<PARAM> -> <EXP> <AFTER_FUNCTION_CALL_EXP>
char* resultVariableName = NULL;
DATA_TYPE resultVariableType;
debugPrintf("calling analyzeExpression from ruleParam\n");
if (analyzeExpression(actualInstructionList, &resultVariableName, &resultVariableType)) {
if (ruleAfterFunctionCallExp(functionToCall, parameters)) {
if(!firstPass) {
char *newResultVariableName = malloc(strlen(resultVariableName) + 1* sizeof(char));
strcpy(newResultVariableName, resultVariableName);
//if the paramseters is empty
debugPrintf("insert parameter with name: %s\n", resultVariableName);
if(parameters->Act == NULL){
InsertFirst(parameters, createListElementWithFunctionParamameter(newResultVariableName, resultVariableType));
} else {
ListInsertLast(parameters, createListElementWithFunctionParamameter(newResultVariableName, resultVariableType));
}
}
return true;
}
}
globalTokens->Act = activeElementRuleApplication;
return false;
}
bool ruleFunctionCallEnd(){
// <FUNCTION_CALL_END> -> )
TOKEN *token = getCachedToken();
if (token->type == BRACKET && token->data.bracket.name == ')') {
return true;
}
returnCachedTokens(1);
return false;
}
bool ruleAfterFunctionCallExp(struct SYMBOL_TABLE_FUNCTION_STR *functionToCall, tDLList *parameters){
tDLElemPtr activeElementRuleApplication = globalTokens->Act;
//<AFTER_FUNCTION_CALL_EXP> -> <FUNCTION_CALL_END>
//<AFTER_FUNCTION_CALL_EXP> -> , <PARAM>
if (ruleFunctionCallEnd()) {
return true;
} else {
TOKEN *token = getCachedToken();
if (token->type == SEPARATOR) {
if (ruleParam(functionToCall, parameters)) {
return true;
}
}
}
globalTokens->Act = activeElementRuleApplication;
return false;
}
/**
* no instruction is generated
*/
bool ruleFuncDefParams(){
//<FUNC_DEF_PARAMS> -> <DEF_PARAM>
//<FUNC_DEF_PARAMS> -> <FUNCTION_DEF_END>
return ruleDefParam() || ruleFunctionDefEnd();
}
/**
* no instruction is generated
*/
bool ruleDefParam(){
DATA_TYPE type;
char *name;
//<DEF_PARAM> -> <TYPE> <ID> <DEF_PARAM_BEGIN_TI>
if (ruleTypeInt(&type) || ruleTypeDouble(&type) || ruleTypeString(&type)) {
if (ruleId(&name)) {
//insert function parameter
if (firstPass) {
ListInsertLast(actualFunction->parameters, *createFunctionParamListElement(type, name));
createAndInsertVariable(&actualFunction->localSymbolTable, name, type, true);
}
if (ruleDefParamBeginTi()) {
return true;
}
}
}
//no need to return tokens
return false;
}
/**
* no instruction is generated
*/
bool ruleDefParamBeginTi(){
tDLElemPtr activeElementRuleApplication = globalTokens->Act;
//<DEF_PARAM_BEGIN_TI> -> <FUNCTION_DEF_END>
//<DEF_PARAM_BEGIN_TI> -> , <DEF_PARAM>
if (ruleFunctionDefEnd()) {
return true;
} else {
TOKEN *token = getCachedToken();
if(token->type == SEPARATOR) {
if (ruleDefParam()) {
return true;
}
}
}
globalTokens->Act = activeElementRuleApplication;
return false;
}
/**
* no instruction is generated
*/
bool ruleFunctionDefEnd(){
//<FUNCTION_DEF_END> -> ) <ST_LIST_DECL>
TOKEN *token = getCachedToken();
if (token->type == BRACKET && token->data.bracket.name == ')') {
return true;
}
returnCachedTokens(1);
return false;
}
/**
* ???? first if branch does not generate instructions(nested rules do!)
*/
bool ruleDefinitionStart(char *className) {
tDLElemPtr activeElementRuleApplication = globalTokens->Act;
TOKEN *token = getCachedToken();
char *functionOrPropertyName;
DATA_TYPE type;
//<DEFINITION_START> -> void <ID><FUNC_DEF>
if (token->type == KEYWORD && stringEquals(token->data.keyword.name, "void")) {
if (ruleId(&functionOrPropertyName)) {
char *classNameWithDot = stringConcat(className, ".");
tDLList *params = malloc(sizeof(tDLList));
ListInit(params);
struct SYMBOL_TABLE_FUNCTION_STR *function = NULL;
if(firstPass) {
function = semanticCreateAndInsertFunction(&globalSymbolTable, stringConcat(classNameWithDot, functionOrPropertyName), TYPE_VOID, 0, NULL, NULL, 0);
} else {
function = semantic_getFunction(stringConcat(classNameWithDot, functionOrPropertyName));
}
actualFunction = function;
actualInstructionList = function->instructions;
if (ruleFuncDef()) {
return true;
}
}
//<DEFINITION_START> -> string <ID><DEFINITION>
} else if (token->type == KEYWORD && stringEquals(token->data.keyword.name, "String")) {
if (ruleId(&functionOrPropertyName)) {
bool propertyInitialized = false;
bool isFunction = false;
type = TYPE_STRING;
if (ruleDefinition(className, type, functionOrPropertyName, &propertyInitialized, &isFunction)) {
return true;
}
}
//<DEFINITION_START> -> int <ID><DEFINITION>
} else if (token->type == KEYWORD && stringEquals(token->data.keyword.name, "int")) {
if (ruleId(&functionOrPropertyName)) {
bool propertyInitialized = false;
bool isFunction = false;
type = TYPE_INT;
if (ruleDefinition(className, type, functionOrPropertyName, &propertyInitialized, &isFunction)) {
return true;
}
}
//<DEFINITION_START> -> double <ID><DEFINITION>
} else if (token->type == KEYWORD && stringEquals(token->data.keyword.name, "double")) {
if (ruleId(&functionOrPropertyName)) {
bool propertyInitialized = false;
bool isFunction = false;
type = TYPE_DOUBLE;
if (ruleDefinition(className, type, functionOrPropertyName, &propertyInitialized, &isFunction)) {
return true;
}
}
}
globalTokens->Act = activeElementRuleApplication;
return false;
}