-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.y
More file actions
2132 lines (1989 loc) · 61.9 KB
/
Copy pathparser.y
File metadata and controls
2132 lines (1989 loc) · 61.9 KB
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
%{
/******************************************************************************
* File : parser.y
* Project : Pazcal Compiler
* Written by : Lolos Konstantinos, Podimata Charikleia
* (lolos.kostis@gmail.com, charapod@gmail.com)
* Date : 2014-2015
* Description : Parser definition to be used by bison
******************************************************************************/
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <string.h>
#include "general.h"
#include "error.h"
#include "symbol.h"
#include "final.h"
#include "opt.h"
#include "gc/include/gc.h"
#include <list>
using namespace std;
enum WRITE_TYPE {WRITE, WRITELN, WRITESP, WRITESPLN};
/* external functions and variables */
extern int lineno;
extern int yylex(void);
extern vector<quad> quads;
/* global variables used by the parser for semantics checking */
list<char*> n_list;
list<const char*> p_list;
list<Type> t_list;
list<const char*> sw_list;
list<array_decl> a_list;
list<arg_decl> arg_list;
Type currRetType;
int program = 0;
unsigned int currentOffset;
unsigned int funcOffset;
unsigned int maxFuncOffset;
WRITE_TYPE write_type;
%}
%union {
char *n; /* for string literals */
char c; /* for char literals -single characters */
int i; /* for integer literals */
long double r; /* for real literals */
/* Typed types that have a type */
struct {
Type type;
} t;
/* function calls */
struct {
Type type;
const char *place;
} call;
/* ALL expressions */
struct {
Type type;
bool isConst;
VALUE value;
const char *place;
list<int> *TRUE;
list<int> *FALSE;
list<const char *> *places;
int i;
} e;
/* lvalues */
struct {
Type type;
char * name;
bool isConst;
VALUE value;
const char * place;
} lv;
/* functions */
struct {
char *name;
Type type;
bool byRef;
SymbolEntry *entry;
} f;
/* statements */
struct {
int i;
list<int> *NEXT;
list<int> *BREAK;
list<int> *CONT;
} s;
type_node *t_n;
/* range */
struct {
const char *expr1_place;
const char *expr2_place;
const char *step_place;
bool isUp;
} rng;
/* two integers! */
struct {
int i1;
int i2;
} ii;
/* FOR */
struct {
int i;
const char * c;
} forr;
}
%token T_bool "bool"
%token T_and "and"
%token T_break "break"
%token T_case "case"
%token T_char "char"
%token T_const "const"
%token T_cont "continue"
%token T_default "default"
%token T_do "do"
%token T_downto "DOWNTO"
%token T_else "else"
%token T_false "false"
%token T_for "FOR"
%token T_form "FORM"
%token T_func "FUNC"
%token T_if "if"
%token T_int "int"
%token T_mod "MOD"
%token T_next "NEXT"
%token T_not "not"
%token T_or "or"
%token T_proc "PROC"
%token T_program "PROGRAM"
%token T_real "REAL"
%token T_return "return"
%token T_step "STEP"
%token T_switch "switch"
%token T_to "TO"
%token T_true "true"
%token T_while "while"
%token T_write "WRITE"
%token T_writeln "WRITELN"
%token T_writesp "WRITESP"
%token T_writespln "WRITESPLN"
%token T_eqeq "=="
%token T_noteq "!="
%token T_geq ">="
%token T_leq "<="
%token T_andand "&&"
%token T_oror "||"
%token T_plusplus "++"
%token T_minmin "--"
%token T_pluseq "+="
%token T_mineq "-="
%token T_muleq "*="
%token T_diveq "/="
%token T_modeq "%="
%token<n> T_identifier
%token<i> T_int_lit
%token<r> T_real_lit
%token<c> T_char_lit
%token<n> T_string_lit
%nonassoc "then"
%nonassoc "else"
%left "||" "or"
%left "&&" "and"
%left "==" "!="
%left '>' '<' ">=" "<="
%left '+' '-'
%left '*' '/' '%' "MOD" /* operators infix */
%left UNOP /* unary operators (prefix) */
%type<t> type c_def c_def_list v_init_list init_a_lst func_type c_expr_list
%type<call> call
%type<n> ID
%type<e> expr un_expr bin_expr const_expr init_value expr_list opt_c_expr switch_case
%type<e> case_list cases T_case switch_list "&&" "and" "||" "or"
%type<lv> l_value var_init
%type<c> assign
%type<f> formal routine_hd
%type<t_n> call_args c_arg_list
%type<s> stmt block block_body switch_body clause stmt_list default_case else_clause "else"
%type<i> "do" "if"
%type<forr> "FOR"
%type<ii> "while"
%type<rng> range
%%
module:
decl_list
{
if (program != 1)
error("? Where is the program???");
}
;
decl_list:
/* nothing */
| declaration decl_list
;
declaration:
const_def
| var_def
| routine
| program
;
const_def:
"const" type ID '=' const_expr c_def_list ';'
{
SymbolEntry *e;
switch ($5.type->kind) {
case TYPE_INTEGER:
e = newConstant($3,$2.type,$5.value.vInteger);
break;
case TYPE_BOOLEAN:
e = newConstant($3,$2.type,$5.value.vBoolean);
break;
case TYPE_CHAR:
e = newConstant($3,$2.type,$5.value.vChar);
break;
case TYPE_REAL:
e = newConstant($3,$2.type,$5.value.vReal);
break;
default:
internal("Unknown constant type.");
}
if ((($6.type->kind != TYPE_VOID) && ($2.type->kind != $6.type->kind)) ||
($2.type->kind != $5.type->kind))
error("Declaration and value types do not match.");
e->isGlobal = !currentScope->nestingLevel;
}
;
ID:
T_identifier
{
$$ = $1;
}
;
c_def_list:
/* nothing */
{
$$.type = typeVoid;
}
| c_def c_def_list
{
if (($2.type->kind != TYPE_VOID) && ($1.type->kind != $2.type->kind)) {
$$.type = typeVoid;
error("Declaration and value types do not match.");
} else
$$.type = $1.type;
}
;
c_def:
',' ID '=' const_expr
{
SymbolEntry *e;
$$.type = $4.type;
switch ($4.type->kind) {
case TYPE_INTEGER:
e = newConstant($2,$4.type,$4.value.vInteger);
break;
case TYPE_BOOLEAN:
e = newConstant($2,$4.type,$4.value.vBoolean);
break;
case TYPE_CHAR:
e = newConstant($2,$4.type,$4.value.vChar);
break;
case TYPE_REAL:
e = newConstant($2,$4.type,$4.value.vReal);
break;
default:
internal("Unknown constant type.");
}
e->isGlobal = !currentScope->nestingLevel;
}
;
var_def:
type var_init v_init_list ';'
{
list<char*>::iterator names = n_list.begin();
list<const char*>::iterator places = p_list.begin();
list<Type>::iterator types = t_list.begin();
list<array_decl>::iterator itt;
Type type;
SymbolEntry *e;
// the names of the declared variables are stored in n_list
// the types of the declared variables are stored in t_list
// create the uninitialized variables in the symbol table
for (; types != t_list.end(); names++, types++, places++) {
e = newVariable(*names, $1.type);
if ((*types)->kind != TYPE_VOID)
genQuad(":=", *places, "-", *names);
if (!assignable_var_def(*types, $1.type))
error("Declaration and initialization types are incompatible.");
e->isGlobal = !currentScope->nestingLevel;
}
n_list.clear();
t_list.clear();
p_list.clear();
// set the types of the declared variables
for (itt = a_list.begin(); itt != a_list.end(); ++itt) {
type = itt->type;
while (type->refType->kind != TYPE_VOID) {
type = type->refType;
}
type->refType = $1.type;
e = newVariable(itt->name, itt->type);
e->isGlobal = !currentScope->nestingLevel;
}
a_list.clear();
}
;
v_init_list:
/* nothing */ {}
| ',' var_init v_init_list {}
;
var_init:
ID init_value
{
$$.type = $2.type;
n_list.push_back($1);
t_list.push_back($2.type);
if ($2.type->kind != TYPE_VOID)
p_list.push_back($2.place);
else
p_list.push_back("dummy");
}
| ID '[' const_expr ']' init_a_lst
{
int size;
if (!$3.isConst) {
error("Array size needs to be constant");
size = 1;
}
else {
if ($3.type->kind == TYPE_INTEGER)
size = $3.value.vInteger;
else if ($3.type->kind == TYPE_CHAR)
size = $3.value.vChar;
else {
size = 1;
error("Array size must be an integer value");
}
}
if (size <= 0) {
error("Array size must be positive");
size = 1;
}
$$.type = typeVoid;
array_decl arr = {$1, typeArray(size, $5.type)};
a_list.push_back(arr);
}
;
init_value:
/* nothing */
{
$$.type = typeVoid;
}
| '=' expr
{
$$.type = $2.type;
if ($2.isConst) {
$$.value = $2.value;
$$.isConst = true;
}
$$.place = $2.place;
}
;
init_a_lst :
/* nothing */
{
$$.type = typeVoid;
}
| '[' const_expr ']' init_a_lst
{
int size;
if (!$2.isConst) {
error("Array size needs to be constant");
size = 1;
}
else {
if ($2.type->kind == TYPE_INTEGER)
size = $2.value.vInteger;
else if ($2.type->kind == TYPE_CHAR)
size = $2.value.vChar;
else {
error("Array size must be an integer value");
size = 1;
}
}
if (size <= 0) {
error("Array size must be positive");
size = 1;
}
$$.type = typeArray(size, $4.type);
}
;
routine_hd:
func_type ID '(' args ')'
{
list<arg_decl>::iterator it;
SymbolEntry *p;
PassMode pm;
p = newFunction($2);
forwardFunction(p);
openScope();
arg_list.reverse();
/* add function parameters stored in arg_list */
for (it = arg_list.begin(); it != arg_list.end(); ++it) {
pm = it->byRef ? PASS_BY_REFERENCE : PASS_BY_VALUE;
newParameter(it->name, it->type, pm, p);
}
arg_list.clear();
endFunctionHeader(p, $1.type);
currRetType = $1.type;
$$.type = $1.type;
$$.name = $2;
$$.entry = p;
}
;
func_type:
"PROC"
{
$$.type = typeVoid;
}
| "FUNC" type
{
$$.type = $2.type;
}
;
args:
/* nothing */
| type formal arg_list
{
Type argType = $2.type;
arg_decl *arg = (arg_decl *) newAlloc(sizeof(* arg));
arg->name = $2.name;
if (argType->kind != TYPE_ARRAY && argType->kind != TYPE_IARRAY)
arg->type = $1.type; /* if formal parameter is not an ARRAY */
else { /* set type of last array to the declared type */
while (argType->refType->kind == TYPE_ARRAY || argType->refType->kind == TYPE_IARRAY) {
argType = argType->refType;
}
argType->refType = $1.type;
arg->type = $2.type;
}
arg->byRef = $2.byRef;
arg_list.push_back(*arg);
}
;
arg_list:
/* nothing */
| ',' type formal arg_list
{
Type argType = $3.type;
arg_decl *arg = (arg_decl *) newAlloc(sizeof(* arg));
arg->name = $3.name;
if (argType->kind != TYPE_ARRAY && argType->kind != TYPE_IARRAY)
arg->type = $2.type; /* if formal parameter is not an ARRAY */
else { /* set type of last array to the declared type */
while (argType->refType->kind == TYPE_ARRAY || argType->refType->kind == TYPE_IARRAY) {
argType = argType->refType;
}
argType->refType = $2.type;
arg->type = $3.type;
}
arg->byRef = $3.byRef;
arg_list.push_back(*arg);
}
;
formal:
ID
{
$$.name = $1;
$$.byRef = false;
$$.type = typeVoid;
}
| '&' ID
{
$$.name = $2;
$$.byRef = true;
$$.type = typeVoid;
}
| ID '[' opt_c_expr ']' c_expr_list
{
$$.name = $1;
$$.byRef = true; /* Arrays ALWAYS by Reference */
$$.type = typeVoid;
if ($3.type->kind == TYPE_VOID)
$$.type = typeIArray($5.type);
else if ($3.type->kind != TYPE_INTEGER && $3.type->kind != TYPE_CHAR){
error("Array dimensions need to be integers");
$$.type = typeArray(1,$5.type);
}
else if ($3.type->kind == TYPE_CHAR) {
if ($3.value.vChar <= 0)
error("Array dimensions need to be positive integers");
$$.type = typeArray($3.value.vChar, $5.type);
}
else if ($3.type->kind == TYPE_INTEGER) {
if ($3.value.vInteger <= 0)
error("Array dimensions need to be positive integers");
$$.type = typeArray($3.value.vInteger, $5.type);
}
}
;
const_expr:
expr
{
$$ = $1;
if (!$$.isConst)
error("Right value needs to be a constant expression.");
}
;
opt_c_expr:
/* nothing */
{
$$.type = typeVoid;
}
| const_expr
{
$$ = $1;
}
;
c_expr_list:
/* nothing */
{
$$.type = typeVoid;
}
| '[' const_expr ']' c_expr_list
{
if ($2.type->kind != TYPE_INTEGER && $2.type->kind != TYPE_CHAR){
error("Array dimensions need to be integers");
$$.type = typeArray(1,$4.type);
}
else if ($2.type->kind == TYPE_CHAR) {
if ($2.value.vChar <= 0)
error("Array dimensions need to be positive integers");
$$.type = typeArray($2.value.vChar, $4.type);
}
else if ($2.type->kind == TYPE_INTEGER) {
if ($2.value.vInteger <= 0)
error("Array dimensions need to be positive integers");
$$.type = typeArray($2.value.vInteger, $4.type);
}
}
;
routine:
routine_hd ';'
{
closeScope();
}
| routine_hd { genQuad("unit", $1.name, "-", "-"); } block
{
SymbolEntry *entry;
entry = lookupEntry($1.name, LOOKUP_ALL_SCOPES,false);
if (!entry || entry->entryType != ENTRY_FUNCTION)
internal("Routine_hd did not register to the function name");
else {
if (!entry->u.eFunction.isForward)
error("Duplicate function definition");
}
$1.entry->u.eFunction.negOffset = currentScope->negOffset;
closeScope();
genQuad("endu", $1.name, "-", "-");
}
;
type:
"int" { $$.type = typeInteger; }
| "bool" { $$.type = typeBoolean; }
| "char" { $$.type = typeChar; }
| "REAL" { $$.type = typeReal; }
;
program:
"PROGRAM" ID '(' ')'
{
genQuad("unit", $2, "-", "-");
openScope();
currRetType = typeVoid;
}
block
{
program++;
program_offset = currentOffset;
genQuad("endu", $2, "-", "-");
closeScope();
currRetType = NULL;
}
;
expr:
T_int_lit
{
$$.value.vInteger = $1;
$$.isConst = true;
$$.type = typeInteger;
/* Intermediate Code */
SymbolEntry *entry = newTemporary($$.type, &$$.value);
$$.place = entry->u.eTemporary.name;
$$.TRUE = new list<int>();
$$.FALSE = new list<int>();
}
| T_real_lit
{
$$.isConst = true;
$$.value.vReal = $1;
$$.type = typeReal;
/* Intermediate Code */
SymbolEntry *entry = newTemporary($$.type, &$$.value);
$$.place = entry->u.eTemporary.name;
$$.TRUE = new list<int>();
$$.FALSE = new list<int>();
}
| T_char_lit
{
$$.isConst = true;
$$.value.vChar = $1;
$$.type = typeChar;
/* Intermediate Code */
SymbolEntry *entry = newTemporary($$.type, &$$.value);
$$.place = entry->u.eTemporary.name;
$$.TRUE = new list<int>();
$$.FALSE = new list<int>();
}
| T_string_lit
{
$$.isConst = true;
$$.value.vString = $1;
$$.type = typeArray(strlen($1)+1,typeChar);
/* Intermediate Code */
SymbolEntry *entry = newTemporary($$.type, &$$.value);
$$.place = entry->u.eTemporary.name;
$$.TRUE = new list<int>();
$$.FALSE = new list<int>();
}
| "true"
{
$$.value.vBoolean = 1;
$$.isConst = true;
$$.type = typeBoolean;
/* Intermediate Code */
SymbolEntry *entry = newTemporary($$.type, &$$.value);
$$.place = entry->u.eTemporary.name;
$$.TRUE = new list<int>();
$$.FALSE = new list<int>();
}
| "false"
{
$$.value.vBoolean = 0;
$$.isConst = true;
$$.type = typeBoolean;
/* Intermediate Code */
SymbolEntry *entry = newTemporary($$.type, &$$.value);
$$.place = entry->u.eTemporary.name;
$$.TRUE = new list<int>();
$$.FALSE = new list<int>();
}
| '(' expr ')'
{
$$ = $2;
$$.TRUE = $2.TRUE;
$$.FALSE = $2.FALSE;
}
| l_value
{
$$.type = $1.type;
$$.isConst = $1.isConst;
$$.value = $1.value;
$$.place = $1.place;
$$.TRUE = new list<int>();
$$.FALSE = new list<int>();
}
| call
{
$$.type = $1.type;
$$.place = $1.place;
$$.isConst = false;
$$.TRUE = new list<int>();
$$.FALSE = new list<int>();
}
| un_expr
{
$$ = $1;
$$.TRUE = new list<int>();
$$.FALSE = new list<int>();
}
| bin_expr
{
$$ = $1;
$$.TRUE = $1.TRUE;
$$.FALSE = $1.FALSE;
}
;
l_value:
ID expr_list
{
Type IDtype,exprType;
exprType = $2.type;
IDtype = typeInteger; /* Default value to avoid typeVoid l_value */
SymbolEntry * entry = lookupEntry($1,LOOKUP_ALL_SCOPES,true);
if (!entry)
error("unknown variable %s", $1);
switch (entry->entryType) {
case ENTRY_VARIABLE:
IDtype = entry->u.eVariable.type;
$$.isConst = false;
break;
case ENTRY_CONSTANT:
IDtype = entry->u.eConstant.type;
$$.isConst = true;
$$.value = entry->u.eConstant.value;
break;
case ENTRY_FUNCTION:
error("Invalid function call");
break;
case ENTRY_PARAMETER:
IDtype = entry->u.eParameter.type;
$$.isConst = false;
break;
case ENTRY_TEMPORARY:
IDtype = entry->u.eTemporary.type;
$$.isConst = entry->u.eTemporary.isConst;
$$.value = entry->u.eTemporary.value;
break;
}
list<const char *>::iterator it = (*$2.places).begin();
const char * last_place = $1;
while (exprType->kind == TYPE_IARRAY) {
if (IDtype->kind != TYPE_ARRAY && IDtype->kind != TYPE_IARRAY) {
error("Variable is not an array.");
break;
}
/* Intermediate Code */
IDtype = IDtype->refType;
SymbolEntry *newTemp = newTemporaryRef(IDtype);
newTemp->u.eTemporary.isArrayElement = true;
genQuad("array", last_place, *it, newTemp->id);
it++;
exprType = exprType->refType;
last_place = newTemp->id;
}
$$.place = last_place;
$$.type = IDtype;
if ($2.type->kind == TYPE_VOID)
$$.place = $1;
}
;
expr_list:
/* nothing */
{
$$.type = typeVoid;
$$.places = new list<const char *>();
}
| '[' expr ']' expr_list
{
if ($2.type->kind != TYPE_INTEGER && $2.type->kind != TYPE_CHAR)
error("Array pointer must be of integer type.");
$$.type = typeIArray($4.type);
$$.places = $4.places;
(*$$.places).push_front($2.place);
}
;
un_expr:
'+' expr %prec UNOP
{
if ($2.type->kind != TYPE_REAL && $2.type->kind != TYPE_INTEGER && $2.type->kind != TYPE_CHAR)
error("Unary operator '+' only applies to numeric values");
$$ = $2;
/* Intermediate Code */
$$.place = $2.place;
}
| '-' expr %prec UNOP
{
$$ = $2;
if ($2.type->kind == TYPE_REAL)
$$.value.vReal *= -1;
else if ($2.type->kind == TYPE_INTEGER)
$$.value.vInteger *= -1;
else if ($2.type->kind == TYPE_CHAR)
$$.value.vChar *= -1;
else
error("Unary operator '-' only applies to numeric values");
/* Intermediate Code */
SymbolEntry *entry;
if ($$.isConst)
entry = newTemporary($$.type, &$$.value);
else {
entry = newTemporary($$.type);
genQuad("-", zeroConst, $2.place, entry->u.eTemporary.name);
}
$$.place = entry->u.eTemporary.name;
}
| '!' expr %prec UNOP
{
$$ = $2;
$$.value.vBoolean = !$2.value.vBoolean;
if ($2.type->kind != TYPE_BOOLEAN)
error("Unary operator '!' only applies to boolean expressions");
/* Intermediate Code */
SymbolEntry *entry;
if ($2.isConst)
entry = newTemporary($$.type, &($$.value));
else {
entry = newTemporary($$.type);
genQuad("not", $2.place, "-", entry->u.eTemporary.name);
}
$$.place = entry->u.eTemporary.name;
}
| "not" expr %prec UNOP
{
$$ = $2;
$$.value.vBoolean = !$2.value.vBoolean;
if ($2.type->kind != TYPE_BOOLEAN)
error("Unary operator '!' only applies to boolean expressions");
/* Intermediate Code */
SymbolEntry *entry;
if ($$.isConst)
entry = newTemporary($$.type, &$$.value);
else {
entry = newTemporary($$.type);
genQuad("not", $2.place, " ", entry->u.eTemporary.name);
}
$$.place = entry->u.eTemporary.name;
}
;
bin_expr:
expr '+' expr
{
$$.isConst = false;
if (is_numeric_type($1.type) && is_numeric_type($3.type)) {
$$.type = calc_type($1.type, $3.type);
if ($1.isConst && $3.isConst) {
$$.isConst = true;
$$.value = eval_bin_expr($1.value, $3.value, $1.type->kind, $3.type->kind, "+");
}
} else
error("Operator '+' applies only to numeric values.");
// Intermediate Code
SymbolEntry *entry;
if ($$.isConst)
entry = newTemporary($$.type, &$$.value);
else {
entry = newTemporary($$.type);
genQuad("+", $1.place, $3.place, entry->u.eTemporary.name);
}
$$.place = entry->u.eTemporary.name;
$$.TRUE = new list<int>();
$$.FALSE = new list<int>();
}
| expr '-' expr
{
$$.isConst = false;
if (is_numeric_type($1.type) && is_numeric_type($3.type)) {
$$.type = calc_type($1.type, $3.type);
if ($1.isConst && $3.isConst) {
$$.isConst = true;
$$.value = eval_bin_expr($1.value, $3.value, $1.type->kind, $3.type->kind, "-");
}
} else
error("Operator '-' applies only to numeric values.");
/* Intermediate Code */
SymbolEntry *entry;
if ($$.isConst)
entry = newTemporary($$.type, &$$.value);
else {
entry = newTemporary($$.type);
genQuad("-", $1.place, $3.place, entry->u.eTemporary.name);
}
$$.place = entry->u.eTemporary.name;
$$.TRUE = new list<int>();
$$.FALSE = new list<int>();
}
| expr '*' expr
{
$$.isConst = false;
if (is_numeric_type($1.type) && is_numeric_type($3.type)) {
$$.type = calc_type($1.type, $3.type);
if ($1.isConst && $3.isConst) {
$$.isConst = true;
$$.value = eval_bin_expr($1.value, $3.value, $1.type->kind, $3.type->kind, "*");
}
} else
error("Operator '*' applies only to numeric values.");
/* Intermediate Code */
SymbolEntry *entry;
if ($$.isConst)
entry = newTemporary($$.type, &$$.value);
else {
entry = newTemporary($$.type);
genQuad("*", $1.place, $3.place, entry->u.eTemporary.name);
}
$$.place = entry->u.eTemporary.name;
$$.TRUE = new list<int>();
$$.FALSE = new list<int>();
}
| expr '/' expr
{
$$.isConst = false;
if (is_numeric_type($1.type) && is_numeric_type($3.type)) {
$$.type = calc_type($1.type, $3.type);
if ($1.type->kind == TYPE_CHAR && $3.type->kind == TYPE_CHAR)
$$.type = typeInteger;
if ($1.isConst && $3.isConst) {
$$.isConst = true;
$$.value = eval_bin_expr($1.value, $3.value, $1.type->kind, $3.type->kind, "/");
if ($1.type->kind == TYPE_CHAR && $3.type->kind == TYPE_CHAR)
$$.value.vInteger = $1.value.vChar / $3.value.vChar;
}
} else
error("Operator '/' applies only to numeric values.");
/* Intermediate Code */
SymbolEntry *entry;
if ($$.isConst)
entry = newTemporary($$.type, &$$.value);
else {
entry = newTemporary($$.type);
genQuad("/", $1.place, $3.place, entry->u.eTemporary.name);
}
$$.place = entry->u.eTemporary.name;
$$.TRUE = new list<int>();
$$.FALSE = new list<int>();
}
| expr '%' expr
{
$$.isConst = false;
$$.type = typeInteger;
if (($1.type->kind != TYPE_INTEGER && $1.type->kind != TYPE_CHAR) ||
($3.type->kind != TYPE_INTEGER && $3.type->kind != TYPE_CHAR))
error("Operator '%%' applies only to numeric values.");
if ($1.isConst && $3.isConst) {
$$.isConst = true;
$$.value = eval_bin_expr($1.value, $3.value, $1.type->kind, $3.type->kind, "%");
}
/* Intermediate Code */