-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcomp.y
executable file
·1217 lines (1079 loc) · 39.5 KB
/
comp.y
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
/**********/
/* HEADER */
/**********/
%code requires{
#include <iostream>
#include <stack>
#include <libgen.h>
#include <string.h>
#include <unordered_map>
#include "front_end/Enumeration.h"
#include "front_end/Genesis.h"
#include "front_end/DeclarationVariable.h"
#include "front_end/Type.h"
#include "front_end/MultipleDeclarationVariable.h"
#include "front_end/Argument.h"
#include "front_end/ArgumentList.h"
#include "front_end/Expression.h"
#include "front_end/DeclarationInitVariable.h"
#include "front_end/DeclarationArrayVariable.h"
#include "front_end/GlobalDeclarationVariable.h"
#include "front_end/FunctionCallExpression.h"
#include "front_end/BinaryOperatorExpression.h"
#include "front_end/UnaryOperatorExpression.h"
#include "front_end/ExpressionInteger.h"
#include "front_end/CrementVariable.h"
#include "front_end/DeclarationFunctionStatement.h"
#include "front_end/DeclarationFunction.h"
#include "front_end/ExpressionVariable.h"
#include "front_end/ExpressionArrayVariable.h"
#include "front_end/AssignmentVariable.h"
#include "front_end/AssignmentOperationVariable.h"
#include "front_end/Return.h"
#include "front_end/SimpleStatement.h"
#include "front_end/ReturnStatement.h"
#include "front_end/UselessStatement.h"
#include "front_end/MultipleStatement.h"
#include "front_end/Statement.h"
#include "front_end/InitFunctionStatement.h"
#include "front_end/BlockDeclarationVariable.h"
#include "front_end/IterationStatement.h"
#include "front_end/SelectionStatement.h"
#include "front_end/ExpressionStatement.h"
#include "front_end/PureDeclarationFunctionStatement.h"
#include "front_end/LoopExpression.h"
#include "front_end/WhileLoop.h"
#include "front_end/ForLoop.h"
#include "front_end/Erreur.h"
Genesis* bison(int argc, char* argv[]);
}
%{
#include "comp.tab.h"
char* filename;
extern int yylex(void);
extern int yylex_destroy(void);
void yyerror(Genesis** g, const char* msg);
void yyerror(const char* msg);
void yywarning(const char* msg);
extern FILE* yyin;
extern int yylineno;
extern int column;
// Utilisé pour les déclarations de variables
int currVariableType;
extern bool hasSyntaxError;
extern std::string syntaxError;
bool variableIsVoid(Genesis** g, Type* type);
bool tryCallFunction(Genesis** g, char* functionName);
bool tryDeclareGlobalVariable(Genesis** g,Type* type, MultipleDeclarationVariable* multDecl);
bool tryDefineFunction(Genesis** g, Type* type, char* name, bool isDeclaration, ArgumentList* args);
bool checkVariableDuplicationInFunction(Genesis** g, char* functionName, ArgumentList* args, DeclarationFunctionStatement* declFunc);
bool checkVariableDuplicationInBlock(Genesis** g, MultipleStatement* multStat);
int getFunctionType(char* name);
// Si on a plusieurs fois une variable de même nom,
// renvoi le type de celle du bloc courant
int getVariableType(char* name);
void pushVariablesInStack(Type* type, MultipleDeclarationVariable* multDecl);
void pushVariablesInStack(Argument* arg);
void popVariablesFromStack(ArgumentList* args, DeclarationFunctionStatement* stat);
void popStatVariablesFromStack(MultipleStatement* stat);
void popSimpleStatVariablesFromStack(SimpleStatement* stat);
std::vector<VariableContainer*> globalVariables;
std::vector<FunctionContainer*> functions;
// Une "pile" des variables locales courantes (rempli et vidé au fur et a mesure)
std::vector<VariableContainer*> currentVars;
// Donne le type primitif correspondant à un type tableau (ex: INT32[] -> INT32)
int primitiveToArrayType(int type);
// Donne le type tableau correspondant à un type primitif (ex: INT32 -> INT32[])
int arrayToPrimitiveType(int type);
bool checkConflictError(Genesis** g, Expression* expr1, Expression* expr2);
void checkAssignmentConstant(int leftExpressionType, Expression* rightExpression);
bool checkArrayTypeConflitError(Genesis** g, int type1, int type2);
std::string getNameOfType(int type);
bool checkVariableExist(Genesis** g, char* name);
struct FunctionCall
{
std::string functionName;
int line;
int column;
FunctionCall(std::string _functionName, int _line, int _column)
: functionName(_functionName), line(_line), column(_column)
{
}
};
std::unordered_map<std::string, FunctionCall*> calledFunctions;
std::unordered_map<std::string, FunctionCall*> undefinedFunctionCall;
bool checkUndeclaredFunctionErrors();
bool checkUndefinedFunctionErrors();
%}
/**************/
/* STRUCTURES */
/**************/
%union {
int i;
char* s;
Genesis* g;
Declaration* d;
MultipleDeclarationVariable* mdv;
DeclarationFunction* df;
DeclarationVariable* dv;
Type* type;
Expression* expr;
ExpressionVariable* exprVar;
AssignmentVariable* assignVar;
DeclarationFunctionStatement* dfs;
ArgumentList* al;
MultipleStatement* ms;
Argument* arg;
SimpleStatement* ss;
IterationStatement* iss;
SelectionStatement* sss;
Return* ret;
Statement* stat;
LoopExpression* loopexpr;
}
/**********/
/* TOKENS */
/**********/
// Liste des tokens issus de Flex (comp.l)
%token <s> ID
%token <i> INT
%token <ival> ELLIPSE
%token <ival> RIGHT_DEC_ASSIGN
%token <ival> LEFT_DEC_ASSIGN
%token <ival> PLUS_ASSIGN
%token <ival> MINUS_ASSIGN
%token <ival> DIV_ASSIGN
%token <ival> MUL_ASSIGN
%token <ival> MOD_ASSIGN
%token <ival> AND_ASSIGN
%token <ival> OR_ASSIGN
%token <ival> OR_EXCL_ASSIGN
%token <ival> INCREMENT
%token <ival> DECREMENT
%token <ival> RIGHT_DEC
%token <ival> LEFT_DEC
%token <ival> MORE_THAN_OR_EQUAL
%token <ival> LESS_THAN_OR_EQUAL
%token <ival> DIFF
%token <ival> EQUAL_EQUAL
%token <ival> AND_AND
%token <ival> OR_OR
%token <ival> EQUAL
%token <ival> PLUS
%token <ival> MINUS
%token <ival> DIV
%token <ival> MUL
%token <ival> MOD
%token <ival> AND
%token <ival> OR
%token <ival> NOT
%token <ival> NOT_BIT
%token <ival> POW
%token <ival> MORE_THAN
%token <ival> LESS_THAN
%token <ival> SEMICOLON
%token <ival> COMMA
%token <ival> OPEN_BRACE
%token <ival> CLOSE_BRACE
%token <ival> OPEN_PARENTHESIS
%token <ival> CLOSE_PARENTHESIS
%token <ival> OPEN_HOOK
%token <ival> CLOSE_HOOK
%token <ival> VOID
%token <ival> CHAR
%token <ival> INT32
%token <ival> INT64
%token <ival> BREAK
%token <ival> RETURN
%token <ival> CONTINUE
%token <ival> WHILE
%token <ival> FOR
%token <ival> IF
%token <ival> ELSE
%token <ival> CHAR_ARRAY
%token <ival> INT32_ARRAY
%token <ival> INT64_ARRAY
// Type permettant la création de la structure de données
%type <g> genesis
%type <d> declaration
%type <mdv> multiple_declaration_variable
%type <df> declaration_function
%type <dv> declaration_variable
%type <type> type
%type <expr> expression
%type <exprVar> expr_var
%type <assignVar> assignment_variable
%type <dfs> declaration_function_statement
%type <al> arguments_list
%type <ms> multiple_statement
%type <arg> argument
%type <ss> simple_statement
%type <iss> iteration_statement
%type <sss> selection_statement
%type <ret> return
%type <stat> statement
%type <loopexpr> loop_expression
/*********/
/* TYPES */
/*********/
/************/
/* PRIORITY */
/************/
// Pour la priorité des opérateurs qui a été définie
// Voir https://c.developpez.com/cours/bernard-cassagne/node101.php#footmp10620
%left COMMA
%right RIGHT_DEC_ASSIGN LEFT_DEC_ASSIGN AND_ASSIGN OR_EXCL_ASSIGN OR_ASSIGN
%right EQUAL PLUS_ASSIGN MINUS_ASSIGN MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN
%left OR_OR
%left AND_AND
%left OR
%left POW
%left EQUAL_EQUAL DIFF
%left MORE_THAN LESS_THAN_OR_EQUAL LESS_THAN MORE_THAN_OR_EQUAL
%left RIGHT_DEC LEFT_DEC
%left PLUS MINUS
%nonassoc NEG
%right MUL DIV MOD
%right NOT NOT_BIT AND
%left INCREMENT DECREMENT
/**************/
/* PARAMETERS */
/**************/
%parse-param { Genesis** g }
/***********/
/* GRAMMAR */
/***********/
%%
program
: genesis {*g = $1;}
;
genesis
: genesis declaration {$$ = $1; $$->addDeclaration($2);}
| declaration {$$ = new Genesis(); $$->addDeclaration($1);}
;
declaration
: type multiple_declaration_variable SEMICOLON
{
GlobalDeclarationVariable* dec = new GlobalDeclarationVariable($2);
$$ = dec;
$2->setType($1);
if(variableIsVoid(g,$1)) YYABORT;
if(!tryDeclareGlobalVariable(g,$1,$2)) YYABORT;
}
| declaration_function {$$ = $1;}
;
type
: VOID {$$ = new Type(VOID); currVariableType = VOID;}
| CHAR {$$ = new Type(CHAR); currVariableType = CHAR;}
| INT32 {$$ = new Type(INT32); currVariableType = INT32;}
| INT64 {$$ = new Type(INT64); currVariableType = INT64;}
;
multiple_declaration_variable
: declaration_variable {$$ = new MultipleDeclarationVariable(); $$->addDeclarationVariable($1);}
| multiple_declaration_variable COMMA declaration_variable {$$ = $1; $1->addDeclarationVariable($3);}
;
declaration_variable
: ID {$$ = new DeclarationVariable($1,false);}
| ID OPEN_HOOK INT CLOSE_HOOK {$$ = new DeclarationArrayVariable($1, $3);}
| ID EQUAL expression
{
$$ = new DeclarationInitVariable($1, $3);
int type1 = currVariableType;
int type2 = $3->getType();
if(checkArrayTypeConflitError(g,type1,type2)) YYABORT;
// Si on a pas une constante à droite
checkAssignmentConstant(type1, $3);
}
;
assignment_variable // utilisé pour affecter une valeur à une variable en dehors de son initialisation (int a; a = 3;)
: expr_var EQUAL expression {$$ = new AssignmentVariable($1,$3);if(checkArrayTypeConflitError(g,$1->getType(),$3->getType())) YYABORT; checkAssignmentConstant($1->getType(), $3);}
| expr_var MUL_ASSIGN expression {$$ = new AssignmentOperationVariable($1,$3,MUL_ASSIGN); if(checkArrayTypeConflitError(g,$1->getType(),$3->getType())) YYABORT; checkAssignmentConstant($1->getType(), $3);}
| expr_var DIV_ASSIGN expression {$$ = new AssignmentOperationVariable($1,$3,DIV_ASSIGN); if(checkArrayTypeConflitError(g,$1->getType(),$3->getType())) YYABORT; checkAssignmentConstant($1->getType(), $3);}
| expr_var MOD_ASSIGN expression {$$ = new AssignmentOperationVariable($1,$3,MOD_ASSIGN); if(checkArrayTypeConflitError(g,$1->getType(),$3->getType())) YYABORT; checkAssignmentConstant($1->getType(), $3);}
| expr_var PLUS_ASSIGN expression {$$ = new AssignmentOperationVariable($1,$3,PLUS_ASSIGN); if(checkArrayTypeConflitError(g,$1->getType(),$3->getType())) YYABORT; checkAssignmentConstant($1->getType(), $3);}
| expr_var MINUS_ASSIGN expression {$$ = new AssignmentOperationVariable($1,$3,MINUS_ASSIGN); if(checkArrayTypeConflitError(g,$1->getType(),$3->getType())) YYABORT; checkAssignmentConstant($1->getType(), $3);}
| expr_var LEFT_DEC_ASSIGN expression {$$ = new AssignmentOperationVariable($1,$3,LEFT_DEC_ASSIGN); if(checkArrayTypeConflitError(g,$1->getType(),$3->getType())) YYABORT; checkAssignmentConstant($1->getType(), $3);}
| expr_var RIGHT_DEC_ASSIGN expression {$$ = new AssignmentOperationVariable($1,$3,RIGHT_DEC_ASSIGN); if(checkArrayTypeConflitError(g,$1->getType(),$3->getType())) YYABORT; checkAssignmentConstant($1->getType(), $3);}
| expr_var AND_ASSIGN expression {$$ = new AssignmentOperationVariable($1,$3,AND_ASSIGN); if(checkArrayTypeConflitError(g,$1->getType(),$3->getType())) YYABORT; checkAssignmentConstant($1->getType(), $3);}
| expr_var OR_ASSIGN expression {$$ = new AssignmentOperationVariable($1,$3,OR_ASSIGN); if(checkArrayTypeConflitError(g,$1->getType(),$3->getType())) YYABORT; checkAssignmentConstant($1->getType(), $3);}
| expr_var OR_EXCL_ASSIGN expression {$$ = new AssignmentOperationVariable($1,$3,OR_EXCL_ASSIGN); if(checkArrayTypeConflitError(g,$1->getType(),$3->getType())) YYABORT; checkAssignmentConstant($1->getType(), $3);}
;
declaration_function
: type ID OPEN_PARENTHESIS CLOSE_PARENTHESIS declaration_function_statement
{
ArgumentList* args = new ArgumentList();
$$ = new DeclarationFunction($1, $2, args, $5);
if(!tryDefineFunction(g,$1,$2,$5->isDeclaration(), args)) YYABORT;
if(checkVariableDuplicationInFunction(g,$2,args,$5)) YYABORT;
if(checkUndeclaredFunctionErrors()) YYABORT;
popVariablesFromStack(nullptr,$5);
}
| type ID OPEN_PARENTHESIS arguments_list CLOSE_PARENTHESIS declaration_function_statement
{
$$ = new DeclarationFunction($1, $2, $4, $6);
if(!tryDefineFunction(g,$1,$2,$6->isDeclaration(),$4)) YYABORT;
if(checkVariableDuplicationInFunction(g,$2,$4,$6)) YYABORT;
popVariablesFromStack($4,$6);
}
;
declaration_function_statement
: SEMICOLON { $$ = new PureDeclarationFunctionStatement(); }
| OPEN_BRACE multiple_statement CLOSE_BRACE { $$ = new InitFunctionStatement($2); }
| OPEN_BRACE CLOSE_BRACE {$$ = new InitFunctionStatement(new MultipleStatement());}
;
argument
: type {
$$=new Argument($1);
}
| type ID {
$$=new Argument($1,$2);
if(variableIsVoid(g, $1)) YYABORT;
}
| type ID OPEN_HOOK CLOSE_HOOK {
$$=new Argument($1,$2,true);
if(variableIsVoid(g, $1)) YYABORT;
}
;
arguments_list
: argument
{
$$ = new ArgumentList(); $$->addArgument($1);
pushVariablesInStack($1);
}
| arguments_list COMMA argument
{
$$ = $1; $1->addArgument($3);
pushVariablesInStack($3);
}
;
simple_statement
: iteration_statement {$$ = $1;}
| selection_statement {$$ = $1;}
| type multiple_declaration_variable SEMICOLON
{
$$ = new BlockDeclarationVariable($2); $2->setType($1);
if(variableIsVoid(g, $1)) YYABORT;
pushVariablesInStack($1,$2);
}
| expression SEMICOLON {$$ = new ExpressionStatement($1);}
| return SEMICOLON {$$ = new ReturnStatement($1);}
| SEMICOLON {$$ = new UselessStatement();}
;
multiple_statement
: multiple_statement simple_statement {$$ = $1; $1->addStatement($2);}
| simple_statement {$$ = new MultipleStatement(); $$->addStatement($1);}
;
return
: RETURN {$$ = new Return(nullptr);}
| RETURN expression {$$ = new Return($2);}
;
iteration_statement
: WHILE OPEN_PARENTHESIS expression CLOSE_PARENTHESIS statement {$$ = new WhileLoop($3,$5);}
| WHILE OPEN_PARENTHESIS expression CLOSE_PARENTHESIS OPEN_BRACE CLOSE_BRACE {$$ = new WhileLoop($3,nullptr);}
| FOR OPEN_PARENTHESIS loop_expression SEMICOLON loop_expression SEMICOLON loop_expression CLOSE_PARENTHESIS statement {$$ = new ForLoop($3,$5,$7,$9);}
| FOR OPEN_PARENTHESIS loop_expression SEMICOLON loop_expression SEMICOLON loop_expression CLOSE_PARENTHESIS OPEN_BRACE CLOSE_BRACE {$$ = new ForLoop($3,$5,$7,nullptr);}
;
statement
: OPEN_BRACE multiple_statement CLOSE_BRACE
{
$$ = new Statement($2);
if(checkVariableDuplicationInBlock(g,$2)) YYABORT;
popStatVariablesFromStack($2);
}
| simple_statement
{
MultipleStatement* mult = new MultipleStatement();
mult->addStatement($1);
$$ = new Statement(mult);
popSimpleStatVariablesFromStack($1);
}
;
loop_expression
: expression {$$ = new LoopExpression($1);}
| {$$ = new LoopExpression(nullptr);}
;
expression
: OPEN_PARENTHESIS expression CLOSE_PARENTHESIS {$$ = $2;}
| expression COMMA expression {$$ = new BinaryOperatorExpression($1,$3,COMMA); }
| expression EQUAL_EQUAL expression {$$ = new BinaryOperatorExpression($1,$3,EQUAL_EQUAL,INT32); if(checkConflictError(g,$1,$3)) YYABORT;}
| expression DIFF expression {$$ = new BinaryOperatorExpression($1,$3,DIFF,INT32); if(checkConflictError(g,$1,$3)) YYABORT;}
| expression LESS_THAN expression {$$ = new BinaryOperatorExpression($1,$3,LESS_THAN,INT32); if(checkConflictError(g,$1,$3)) YYABORT;}
| expression LESS_THAN_OR_EQUAL expression {$$ = new BinaryOperatorExpression($1,$3,LESS_THAN_OR_EQUAL,INT32); if(checkConflictError(g,$1,$3)) YYABORT;}
| expression MORE_THAN expression {$$ = new BinaryOperatorExpression($1,$3,MORE_THAN,INT32); if(checkConflictError(g,$1,$3)) YYABORT;}
| expression MORE_THAN_OR_EQUAL expression {$$ = new BinaryOperatorExpression($1,$3,MORE_THAN_OR_EQUAL,INT32); if(checkConflictError(g,$1,$3)) YYABORT;}
| expression AND_AND expression {$$ = new BinaryOperatorExpression($1,$3,AND_AND,INT32); if(checkConflictError(g,$1,$3)) YYABORT;}
| expression OR_OR expression {$$ = new BinaryOperatorExpression($1,$3,OR_OR,INT32); if(checkConflictError(g,$1,$3)) YYABORT;}
| assignment_variable {$$ = $1;}
| PLUS expression {$$ = new UnaryOperatorExpression($2,PLUS);}
| MINUS expression %prec NEG {$$ = new UnaryOperatorExpression($2,MINUS);}
| NOT_BIT expression {$$ = new UnaryOperatorExpression($2,NOT_BIT);}
| NOT expression {$$ = new UnaryOperatorExpression($2,NOT);}
| INT {$$ = new ExpressionInteger($1,INT32);}
| INCREMENT expr_var {$$ = new CrementVariable($2, true, true);}
| DECREMENT expr_var {$$ = new CrementVariable($2, false, true);}
| expr_var INCREMENT {$$ = new CrementVariable($1, true, false);}
| expr_var DECREMENT {$$ = new CrementVariable($1, false, false);}
| expr_var {$$ = $1;}
| ID OPEN_PARENTHESIS expression CLOSE_PARENTHESIS
{
if(!tryCallFunction(g, $1)) YYABORT;
$$ = new FunctionCallExpression($1, $3, getFunctionType($1));
}
| ID OPEN_PARENTHESIS CLOSE_PARENTHESIS
{
if(!tryCallFunction(g, $1)) YYABORT;
$$ = new FunctionCallExpression($1, nullptr, getFunctionType($1));
}
| expression PLUS expression {$$ = new BinaryOperatorExpression($1,$3,PLUS); if(checkConflictError(g,$1,$3)) YYABORT;}
| expression MINUS expression {$$ = new BinaryOperatorExpression($1,$3,MINUS); if(checkConflictError(g,$1,$3)) YYABORT;}
| expression MUL expression {$$ = new BinaryOperatorExpression($1,$3,MUL); if(checkConflictError(g,$1,$3)) YYABORT;}
| expression DIV expression {$$ = new BinaryOperatorExpression($1,$3,DIV); if(checkConflictError(g,$1,$3)) YYABORT;}
| expression MOD expression {$$ = new BinaryOperatorExpression($1,$3,MOD); if(checkConflictError(g,$1,$3)) YYABORT;}
| expression AND expression {$$ = new BinaryOperatorExpression($1,$3,AND); if(checkConflictError(g,$1,$3)) YYABORT;}
| expression OR expression {$$ = new BinaryOperatorExpression($1,$3,OR); if(checkConflictError(g,$1,$3)) YYABORT;}
| expression POW expression {$$ = new BinaryOperatorExpression($1,$3,POW); if(checkConflictError(g,$1,$3)) YYABORT;}
| expression LEFT_DEC expression {$$ = new BinaryOperatorExpression($1,$3,LEFT_DEC); if(checkConflictError(g,$1,$3)) YYABORT;}
| expression RIGHT_DEC expression {$$ = new BinaryOperatorExpression($1,$3,RIGHT_DEC); if(checkConflictError(g,$1,$3)) YYABORT;}
;
expr_var
: ID OPEN_HOOK expression CLOSE_HOOK
{
if(!checkVariableExist(g,$1)) YYABORT;
if(!isArrayType(getVariableType($1)))
{
yyerror(g, ("tentative d'utilisation de l'opérateur indice de tableau sur la variable non-tableau "+std::string($1)+".").c_str());
YYABORT;
}
$$ = new ExpressionArrayVariable($1, $3, arrayToPrimitiveType(getVariableType($1)));
}
| ID
{
if(!checkVariableExist(g,$1)) YYABORT;
$$ = new ExpressionVariable($1, getVariableType($1));
}
;
selection_statement
: IF OPEN_PARENTHESIS expression CLOSE_PARENTHESIS statement ELSE statement {$$ = new SelectionStatement($3,$5,$7);}
| IF OPEN_PARENTHESIS expression CLOSE_PARENTHESIS statement {$$ = new SelectionStatement($3,$5,nullptr);}
| IF OPEN_PARENTHESIS expression CLOSE_PARENTHESIS OPEN_BRACE CLOSE_BRACE {$$ = new SelectionStatement($3,nullptr,nullptr);}
;
%%
/***********************/
/* PROGRAMME PRINCIPAL */
/***********************/
bool variableIsVoid(Genesis** g, Type* type)
{
if (type->getType() == VOID)
{
yyerror(g, "Une variable ne peut pas être de type void.");
return true;
}
return false;
}
bool checkUndeclaredFunctionErrors()
{
// On retire chaque fonction définie du set des fonctions appellées
for(int i=0;i<functions.size();++i)
{
FunctionContainer* currFunc = functions[i];
auto it = undefinedFunctionCall.find(std::string(currFunc->name));
if (it != undefinedFunctionCall.end())
{
undefinedFunctionCall.erase(std::string(currFunc->name));
if(currFunc->declaration)
{
calledFunctions.insert(std::make_pair(std::string(currFunc->name),it->second));
}
else
{
delete(it->second);
}
}
}
int error = false;
for (auto& pair: undefinedFunctionCall)
{
FunctionCall* func = pair.second;
std::cout << filename << ":" << func->line << ":" << func->column <<" - erreur : "
<< "La fonction " << std::string(func->functionName) << " n'a pas été déclarée." << std::endl;
delete(func);
error = true;
}
return error;
}
bool checkUndefinedFunctionErrors()
{
// On retire chaque fonction définie du set des fonctions appellées
for(int i=0;i<functions.size();++i)
{
FunctionContainer* currFunc = functions[i];
if(!currFunc->declaration)
{
auto it = calledFunctions.find(std::string(currFunc->name));
if (it != calledFunctions.end())
{
delete(it->second);
calledFunctions.erase(std::string(currFunc->name));
}
}
}
int error = false;
for (auto& pair: calledFunctions)
{
FunctionCall* func = pair.second;
std::cout << filename << ":" << func->line << ":" << func->column <<" - erreur : "
<< "Référence indéfinie vers la fonction " << std::string(func->functionName) << "." << std::endl;
delete(func);
error = true;
}
return error;
}
bool tryCallFunction(Genesis** g, char* functionName)
{
int state=0;
if(strcmp(functionName, "putchar")==0
|| strcmp(functionName, "getchar")==0)
{
return true;
}
for(int i=0;i<functions.size();++i)
{
FunctionContainer* currFunc = functions[i];
// 0 = fonction n'existe pas. 1 = seulement le prototype. 2 = correct
if(strcmp(functionName, currFunc->name)==0)
{
if(currFunc->declaration)
{
state = 1;
}
else
{
state = 2;
break;
}
}
}
if(state == 0)
{
// Ajouter à la liste des fonctions non-définies
// (à checker dès qu'on sort de la fonction)
FunctionCall* call = new FunctionCall(functionName, yylineno, column);
undefinedFunctionCall.insert(std::make_pair(std::string(functionName),call));
}
if(state == 1)
{
// Ajouter à la liste des fonctions appellées, mais pas définies
// (à checker à la fin si on a bien défini le corps de la fonction)
FunctionCall* call = new FunctionCall(functionName, yylineno, column);
calledFunctions.insert(std::make_pair(std::string(functionName),call));
}
return true;
}
bool tryDeclareGlobalVariable(Genesis** g, Type* type, MultipleDeclarationVariable* multDecl)
{
for(int i=0;i<multDecl->countDeclaration();++i)
{
DeclarationVariable* decVar = (*multDecl)[i];
VariableContainer* var = new VariableContainer(decVar->getId(), type->getType());
for(int j=0;j<globalVariables.size();j++)
{
if(strcmp(globalVariables[i]->name,var->name)==0)
{
yyerror(g, ("la variable globale "+ std::string(var->name)+" a deja ete declaree.").c_str());
delete var;
return false;
}
}
if(!decVar->isDeclaration())
{
DeclarationInitVariable* initVar = (DeclarationInitVariable*)decVar;
if(initVar->getExpr()->getExpressionType() != EXPRESSION_INTEGER)
{
yyerror(g, "Impossible d'assigner une expression non-constante à une variable globale.");
delete var;
return false;
}
}
globalVariables.push_back(var);
}
return true;
}
bool tryDefineFunction(Genesis** g, Type* type, char* name, bool isDeclaration, ArgumentList* args)
{
FunctionContainer* func;
func = new FunctionContainer(name, type->getType(), isDeclaration, args);
for(int i=0;i<functions.size();++i)
{
FunctionContainer* currFunc = functions[i];
if(strcmp(func->name, currFunc->name)==0)
{
// Check si le type de retour est identique
if(func->type != currFunc->type)
{
yyerror(g, ("La fonction "+std::string(func->name)+" a déjà été déclarée avec un type de retour différent.").c_str());
delete func;
return false;
}
// Check si les paramètres d'appel sont identiques
if(func->args->countArguments() != currFunc->args->countArguments())
{
yyerror(g, ("La fonction "+std::string(func->name)+" a déjà été déclarée avec un nombre de paramètres différent.").c_str());
delete func;
return false;
}
for(int j=0;j<func->args->countArguments();++j)
{
int type1 = (*(func->args))[j]->getType()->getType();
int type2 = (*(currFunc->args))[j]->getType()->getType();
if(type1 != type2)
{
yyerror(g, ("La fonction "+std::string(func->name)+" a déjà été déclarée avec des paramètres différents.").c_str());
yyerror(g, (std::string("(Argument n°")+std::to_string(j)+", type "+getNameOfType(type1)+" au lieu de "+getNameOfType(type2)+".").c_str());
delete func;
return false;
}
}
}
if(strcmp(func->name, currFunc->name)==0 && !(func->declaration) && !(currFunc->declaration))
{
yyerror(g, ("La fonction "+std::string(func->name)+" a déjà été définie.").c_str());
delete func;
return false;
}
}
functions.push_back(func);
return true;
}
bool checkVariableDuplicationInFunction(Genesis** g, char* functionName, ArgumentList* args, DeclarationFunctionStatement* declFunc)
{
std::vector<VariableContainer*> variables;
// Test doublons au sein des paramètres de la fonction
for(int i=0;i<args->countArguments();++i)
{
Argument* arg = (*args)[i];
if(arg->getName() == nullptr)
{
continue;
}
VariableContainer* var = new VariableContainer(arg->getName(), arg->getType()->getType());
for(int j=0;j<variables.size();++j)
{
VariableContainer* currVar = variables[j];
if(strcmp(var->name, currVar->name) == 0)
{
yyerror(g, ("La fonction "+std::string(functionName)+" contient plusieurs paramètres nommés "+std::string(var->name)).c_str());
delete var;
return true;
}
}
variables.push_back(var);
}
// Si on dispose du corps de la fonction
if(!declFunc->isDeclaration())
{
InitFunctionStatement* funcBody = (InitFunctionStatement*)declFunc;
for(int i=0;i<funcBody->countStatements();++i)
{
SimpleStatement* stat = (*funcBody)[i];
if(stat->getType() == BLOCK_DECLARATION_VARIABLE)
{
BlockDeclarationVariable* blockDeclVar = (BlockDeclarationVariable*) stat;
MultipleDeclarationVariable* multDecl = blockDeclVar->getMultipleDeclarationVariable();
for(int j=0;j<multDecl->countDeclaration();++j)
{
DeclarationVariable* decVar = (*multDecl)[j];
VariableContainer* var = new VariableContainer(decVar->getId(), multDecl->getType()->getType());
// Regarder si la variable existe déjà
for(int k=0;k<variables.size();++k)
{
VariableContainer* currVar = variables[k];
if(strcmp(var->name, currVar->name) == 0)
{
yyerror(g, ("La fonction "+std::string(functionName)+" contient plusieurs variables nommé "+std::string(var->name)+" dans la même portée.").c_str());
delete var;
return true;
}
}
variables.push_back(var);
}
}
}
}
return false;
}
bool checkVariableDuplicationInBlock(Genesis** g, MultipleStatement* multStat)
{
std::vector<VariableContainer*> variables;
for(int i=0;i<multStat->countStatements();++i)
{
SimpleStatement* stat = (*multStat)[i];
if(stat->getType() == BLOCK_DECLARATION_VARIABLE)
{
BlockDeclarationVariable* blockDeclVar = (BlockDeclarationVariable*) stat;
MultipleDeclarationVariable* multDecl = blockDeclVar->getMultipleDeclarationVariable();
for(int j=0;j<multDecl->countDeclaration();++j)
{
DeclarationVariable* decVar = (*multDecl)[j];
VariableContainer* var = new VariableContainer(decVar->getId(), multDecl->getType()->getType());
// Regarder si la variable existe déjà
for(int k=0;k<variables.size();++k)
{
VariableContainer* currVar = variables[k];
if(strcmp(var->name, currVar->name) == 0)
{
yyerror(g, ("Plusieurs variables nommé "+std::string(var->name)+" dans la même portée.").c_str());
delete var;
return true;
}
}
variables.push_back(var);
}
}
}
return false;
}
int getFunctionType(char* name)
{
if(strcmp(name,"putchar")==0)
{
return INT32;
}
if(strcmp(name,"getchar")==0)
{
return INT32;
}
for(int i=0;i<functions.size();++i)
{
FunctionContainer* func = functions[i];
if(strcmp(name,func->name)==0)
{
return func->type;
}
}
return INT32;
}
int getVariableType(char* name)
{
// Recherche dans les variables locales
for(int i=currentVars.size()-1;i>=0;--i)
{
VariableContainer* var = currentVars[i];
if(strcmp(name,var->name)==0)
{
return var->type;
}
}
// Recherche dans les variables globales
for(int i=0;i<globalVariables.size();++i)
{
VariableContainer* var = globalVariables[i];
if(strcmp(name,var->name)==0)
{
return var->type;
}
}
return INT32;
}
void pushVariablesInStack(Type* type, MultipleDeclarationVariable* multDecl)
{
int typeVar = type->getType();
for(int i=0;i<multDecl->countDeclaration();++i)
{
DeclarationVariable* decVar = (*multDecl)[i];
if(decVar->isArray())
{
typeVar = primitiveToArrayType(typeVar);
}
VariableContainer* var = new VariableContainer(decVar->getId(), typeVar);
currentVars.push_back(var);
}
}
bool checkVariableExist(Genesis** g, char* name)
{
for(int i=0;i<currentVars.size();++i)
{
if(strcmp(name,currentVars[i]->name)==0)
{
return true;
}
}
for(int i=0;i<globalVariables.size();++i)
{
if(strcmp(name,globalVariables[i]->name)==0)
{
return true;
}
}
yyerror(g, ("La variable "+std::string(name)+" est utilisée mais jamais définie.").c_str());
return false;
}
void pushVariablesInStack(Argument* arg)
{
int typeVar = arg->getType()->getType();
if(arg->isArray())
{
typeVar = primitiveToArrayType(typeVar);
}
VariableContainer* var = new VariableContainer(arg->getName(), typeVar);
currentVars.push_back(var);
}
void popVariablesFromStack(ArgumentList* args, DeclarationFunctionStatement* stat)
{
int countVars = 0;
if(args != nullptr)
{
countVars += args->countArguments();
}
// Si on dispose du corps de la fonction
if(!stat->isDeclaration())
{
InitFunctionStatement* funcBody = (InitFunctionStatement*)stat;
for(int i=0;i<funcBody->countStatements();++i)
{
SimpleStatement* stat = (*funcBody)[i];
if(stat->getType() == BLOCK_DECLARATION_VARIABLE)
{
BlockDeclarationVariable* blockDeclVar = (BlockDeclarationVariable*) stat;
MultipleDeclarationVariable* multDecl = blockDeclVar->getMultipleDeclarationVariable();
countVars += multDecl->countDeclaration();
}
}
}
for(int i=0;i<countVars;++i)
{
VariableContainer* var = currentVars.back();
currentVars.pop_back();
delete var;
}
}
void popSimpleStatVariablesFromStack(SimpleStatement* stat)
{
if(stat->getType() == BLOCK_DECLARATION_VARIABLE)
{
BlockDeclarationVariable* blockDeclVar = (BlockDeclarationVariable*) stat;
MultipleDeclarationVariable* multDecl = blockDeclVar->getMultipleDeclarationVariable();
int countVars = multDecl->countDeclaration();
for(int i=0;i<countVars;++i)
{
VariableContainer* var = currentVars.back();
currentVars.pop_back();
delete var;
}
}
}
void popStatVariablesFromStack(MultipleStatement* stats)
{
int countVars = 0;
for(int i=0;i<stats->countStatements();++i)
{
SimpleStatement* stat = (*stats)[i];
if(stat->getType() == BLOCK_DECLARATION_VARIABLE)
{
BlockDeclarationVariable* blockDeclVar = (BlockDeclarationVariable*) stat;
MultipleDeclarationVariable* multDecl = blockDeclVar->getMultipleDeclarationVariable();
countVars += multDecl->countDeclaration();
}
}
for(int i=0;i<countVars;++i)
{
VariableContainer* var = currentVars.back();
currentVars.pop_back();
delete var;
}
}
int primitiveToArrayType(int type)
{
switch(type)