-
Notifications
You must be signed in to change notification settings - Fork 1
/
postlang.y
1788 lines (1642 loc) · 50.7 KB
/
postlang.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
/*
* These are token values that needn't have an associated code for the
* compiled file
*/
%token F_CASE F_DEFAULT F_RANGE
%union
{
int number;
unsigned int address; /* Address of an instruction */
char *string;
short type;
struct { int key; char block; } case_label;
struct function *funp;
}
%type <number> assign F_NUMBER constant F_LOCAL_NAME expr_list
%type <number> const1 const2 const3 const4 const5 const6 const7 const8 const9
%type <number> lvalue_list argument type basic_type optional_star expr_list2
%type <number> type_modifier type_modifier_list opt_basic_type block_or_semi
%type <number> argument_list
%type <string> F_IDENTIFIER F_STRING string_con1 string_constant function_name
%type <case_label> case_label
/* The following symbos return type information */
%type <type> function_call lvalue string cast expr28 expr01 comma_expr
%type <type> expr2 expr211 expr1 expr212 expr213 expr24 expr22 expr23 expr25
%type <type> expr27 expr28 expr24 expr3 expr31 expr4 number expr0
%%
all: program;
program: program def possible_semi_colon
| /* empty */ ;
possible_semi_colon: /* empty */
| ';' { yyerror("Extra ';'. Ignored."); };
inheritance: type_modifier_list F_INHERIT F_STRING ';'
{
struct object *ob;
struct inherit inherit;
int initializer;
ob = find_object2($3);
if (ob == 0) {
inherit_file = $3;
/* Return back to load_object() */
YYACCEPT;
}
free($3);
if (ob->flags & O_APPROVED)
approved_object = 1;
inherit.prog = ob->prog;
inherit.function_index_offset =
mem_block[A_FUNCTIONS].current_size /
sizeof (struct function);
inherit.variable_index_offset =
mem_block[A_VARIABLES].current_size /
sizeof (struct variable);
add_to_mem_block(A_INHERITS, &inherit, sizeof inherit);
copy_variables(ob->prog, $1);
initializer = copy_functions(ob->prog, $1);
if (initializer > 0) {
struct function *funp;
int f;
f = define_new_function("::__INIT", 0, 0, 0, 0, 0);
funp = FUNCTION(f);
funp->offset = mem_block[A_INHERITS].current_size /
sizeof (struct inherit) - 1;
funp->flags = NAME_STRICT_TYPES |
NAME_INHERITED | NAME_HIDDEN;
funp->type = TYPE_VOID;
funp->function_index_offset = initializer;
transfer_init_control();
ins_f_byte(F_CALL_FUNCTION_BY_ADDRESS);
ins_short(f);
ins_byte(0); /* Actual number of arguments */
ins_f_byte(F_POP_VALUE);
add_new_init_jump();
}
}
number: F_NUMBER
{
if ( $1 == 0 ) {
ins_f_byte(F_CONST0); $$ = TYPE_ANY;
} else if ( $1 == 1 ) {
ins_f_byte(F_CONST1); $$ = TYPE_NUMBER;
} else {
ins_f_byte(F_NUMBER); ins_long($1); $$ = TYPE_NUMBER;
}
} ;
optional_star: /* empty */ { $$ = 0; } | '*' { $$ = TYPE_MOD_POINTER; } ;
block_or_semi: block { $$ = 0; } | ';' { $$ = ';'; } ;
def: type optional_star F_IDENTIFIER
{
/* Save start of function. */
push_explicit(mem_block[A_PROGRAM].current_size);
if ($1 & TYPE_MOD_MASK) {
exact_types = $1 | $2;
} else {
if (pragma_strict_types)
yyerror("\"#pragma strict_types\" requires type of function");
exact_types = 0;
}
}
'(' argument ')'
{
/*
* Define a prototype. If it is a real function, then the
* prototype will be replaced below.
*/
define_new_function($3, $6, 0, 0,
NAME_UNDEFINED|NAME_PROTOTYPE, $1 | $2);
}
block_or_semi
{
/* Either a prototype or a block */
if ($9 == ';') {
(void)pop_address(); /* Not used here */
} else {
define_new_function($3, $6, current_number_of_locals - $6+
( max_break_stack_need -1 ) / sizeof(struct svalue) +1,
pop_address(), 0, $1 | $2);
ins_f_byte(F_CONST0); ins_f_byte(F_RETURN);
}
free_all_local_names();
free($3); /* Value was copied above */
}
| type name_list ';' { if ($1 == 0) yyerror("Missing type"); }
| inheritance ;
new_arg_name: type optional_star F_IDENTIFIER
{
if (exact_types && $1 == 0) {
yyerror("Missing type for argument");
add_local_name($3, TYPE_ANY); /* Supress more errors */
} else {
add_local_name($3, $1 | $2);
}
}
| type F_LOCAL_NAME
{yyerror("Illegal to redeclare local name"); } ;
argument: /* empty */ { $$ = 0; }
| argument_list ;
argument_list: new_arg_name { $$ = 1; }
| argument_list ',' new_arg_name { $$ = $1 + 1; } ;
type_modifier: F_NO_MASK { $$ = TYPE_MOD_NO_MASK; }
| F_STATIC { $$ = TYPE_MOD_STATIC; }
| F_PRIVATE { $$ = TYPE_MOD_PRIVATE; }
| F_PUBLIC { $$ = TYPE_MOD_PUBLIC; }
| F_VARARGS { $$ = TYPE_MOD_VARARGS; }
| F_PROTECTED { $$ = TYPE_MOD_PROTECTED; } ;
type_modifier_list: /* empty */ { $$ = 0; }
| type_modifier type_modifier_list { $$ = $1 | $2; } ;
type: type_modifier_list opt_basic_type { $$ = $1 | $2; current_type = $$; } ;
cast: '(' basic_type optional_star ')'
{
$$ = $2 | $3;
} ;
opt_basic_type: basic_type | /* empty */ { $$ = TYPE_UNKNOWN; } ;
basic_type: F_STATUS { $$ = TYPE_NUMBER; current_type = $$; }
| F_INT { $$ = TYPE_NUMBER; current_type = $$; }
| F_STRING_DECL { $$ = TYPE_STRING; current_type = $$; }
| F_OBJECT { $$ = TYPE_OBJECT; current_type = $$; }
| F_VOID {$$ = TYPE_VOID; current_type = $$; }
| F_MIXED { $$ = TYPE_ANY; current_type = $$; } ;
name_list: new_name
| new_name ',' name_list;
new_name: optional_star F_IDENTIFIER
{
define_variable($2, current_type | $1, 0);
free($2);
}
| optional_star F_IDENTIFIER
{
int var_num;
define_variable($2, current_type | $1, 0);
var_num = verify_declared($2);
transfer_init_control();
ins_f_byte(F_PUSH_IDENTIFIER_LVALUE);
ins_byte(var_num);
}
'=' expr0
{
if (!compatible_types((current_type | $1) & TYPE_MOD_MASK, $5)){
char buff[100];
sprintf(buff, "Type mismatch %s when initializing %s",
get_two_types(current_type | $1, $5), $2);
yyerror(buff);
}
ins_f_byte(F_ASSIGN);
ins_f_byte(F_POP_VALUE);
add_new_init_jump();
free($2);
} ;
block: '{' local_declarations statements '}'
{ ; };
local_declarations: /* empty */
| local_declarations basic_type local_name_list ';' ;
new_local_name: optional_star F_IDENTIFIER
{
add_local_name($2, current_type | $1);
} ;
local_name_list: new_local_name
| new_local_name ',' local_name_list ;
statements: /* empty */
| statement statements
| error ';' ;
statement: comma_expr ';'
{
ins_f_byte(F_POP_VALUE);
if (d_flag)
ins_f_byte(F_BREAK_POINT);
/* if (exact_types && !TYPE($1,TYPE_VOID))
yyerror("Value thrown away"); */
}
| cond | while | do | for | switch | case | default | return ';'
| block
| /* empty */ ';'
| F_BREAK ';' /* This code is a jump to a jump */
{
if (current_break_address == 0)
yyerror("break statement outside loop");
if (current_break_address & BREAK_ON_STACK) {
ins_f_byte(F_BREAK);
} else {
ins_f_byte(F_JUMP); ins_short(current_break_address);
}
}
| F_CONTINUE ';' /* This code is a jump to a jump */
{
if (current_continue_address == 0)
yyerror("continue statement outside loop");
ins_f_byte(F_JUMP); ins_short(current_continue_address);
}
;
while: { push_explicit(current_continue_address);
push_explicit(current_break_address);
current_continue_address = mem_block[A_PROGRAM].current_size;
} F_WHILE '(' comma_expr ')'
{
ins_f_byte(F_JUMP_WHEN_NON_ZERO); ins_short(0); /* to block */
current_break_address = mem_block[A_PROGRAM].current_size;
ins_f_byte(F_JUMP); ins_short(0); /* Exit loop */
upd_short(current_break_address-2,
mem_block[A_PROGRAM].current_size);
}
statement
{
ins_f_byte(F_JUMP); ins_short(current_continue_address);
upd_short(current_break_address+1,
mem_block[A_PROGRAM].current_size);
current_break_address = pop_address();
current_continue_address = pop_address();
}
do: {
int tmp_save;
push_explicit(current_continue_address);
push_explicit(current_break_address);
/* Jump to start of loop. */
ins_f_byte(F_JUMP); tmp_save = mem_block[A_PROGRAM].current_size;
ins_short(0);
current_break_address = mem_block[A_PROGRAM].current_size;
/* Jump to end of loop. All breaks go through this one. */
ins_f_byte(F_JUMP); push_address(); ins_short(0);
current_continue_address = mem_block[A_PROGRAM].current_size;
upd_short(tmp_save, current_continue_address);
push_address();
} F_DO statement F_WHILE '(' comma_expr ')' ';'
{
ins_f_byte(F_JUMP_WHEN_NON_ZERO); ins_short(pop_address());
/* Fill in the break jump address in the beginning of the loop. */
upd_short(pop_address(), mem_block[A_PROGRAM].current_size);
current_break_address = pop_address();
current_continue_address = pop_address();
}
for: F_FOR '(' { push_explicit(current_continue_address);
push_explicit(current_break_address); }
for_expr ';' { ins_f_byte(F_POP_VALUE);
push_address();
}
for_expr ';' {
ins_f_byte(F_JUMP_WHEN_NON_ZERO);
ins_short(0); /* Jump to block of block */
current_break_address = mem_block[A_PROGRAM].current_size;
ins_f_byte(F_JUMP); ins_short(0); /* Out of loop */
current_continue_address =
mem_block[A_PROGRAM].current_size;
}
for_expr ')' {
ins_f_byte(F_POP_VALUE);
ins_f_byte(F_JUMP); ins_short(pop_address());
/* Here starts the block. */
upd_short(current_break_address-2,
mem_block[A_PROGRAM].current_size);
}
statement
{
ins_f_byte(F_JUMP); ins_short(current_continue_address);
/* Now, the address of the end of the block is known. */
upd_short(current_break_address+1, mem_block[A_PROGRAM].current_size);
current_break_address = pop_address();
current_continue_address = pop_address();
}
for_expr: /* EMPTY */ { ins_f_byte(F_CONST1); }
| comma_expr;
switch: F_SWITCH '(' comma_expr ')'
{
current_break_stack_need += sizeof(short);
if ( current_break_stack_need > max_break_stack_need )
max_break_stack_need = current_break_stack_need;
push_explicit(current_case_number_heap);
push_explicit(current_case_string_heap);
push_explicit(zero_case_label);
push_explicit(current_break_address);
ins_f_byte(F_SWITCH);
ins_byte(0xff); /* kind of table */
current_case_number_heap = mem_block[A_CASE_NUMBERS].current_size;
current_case_string_heap = mem_block[A_CASE_STRINGS].current_size;
zero_case_label = NO_STRING_CASE_LABELS;
ins_short(0); /* address of table */
current_break_address = mem_block[A_PROGRAM].current_size |
BREAK_ON_STACK | BREAK_FROM_CASE ;
ins_short(0); /* break address to push, table is entered before */
ins_short(0); /* default address */
}
statement
{
char *heap_start;
int heap_end_offs;
int i,o;
int current_key,last_key;
/* int size_without_table; */
int block_index;
int current_case_heap;
int lookup_start;
int lookup_start_key;
current_break_address &= ~(BREAK_ON_STACK|BREAK_FROM_CASE);
if ( !read_short(current_break_address+2 ) )
upd_short(current_break_address+2, /* no default given -> */
mem_block[A_PROGRAM].current_size); /* create one */
/* it isn't unusual that the last case/default has no break */
ins_f_byte(F_BREAK);
if(zero_case_label & (NO_STRING_CASE_LABELS|SOME_NUMERIC_CASE_LABELS)){
block_index = A_CASE_NUMBERS;
current_case_heap = current_case_number_heap;
} else {
block_index = A_CASE_STRINGS;
current_case_heap = current_case_string_heap;
if (zero_case_label&0xffff) {
struct case_heap_entry temp;
temp.key = ZERO_AS_STR_CASE_LABEL;
temp.addr = zero_case_label;
temp.line = 0; /* if this is accessed later, something is
* really wrong */
add_to_case_heap(A_CASE_STRINGS,&temp);
}
}
heap_start = mem_block[block_index].block + current_case_heap ;
heap_end_offs = mem_block[block_index].current_size -current_case_heap;
if (!heap_end_offs) yyerror("switch without case not supported");
/* add a dummy entry so that we can always
* assume we have no or two childs
*/
add_to_mem_block(block_index, "\0\0\0\0\0\0\0\0",
sizeof(struct case_heap_entry) );
/* read out the heap and build a sorted table */
/* the table could be optimized better, but let's first see
* how much switch is used at all when it is full-featured...
*/
mem_block[A_CASE_LABELS].current_size = 0;
lookup_start = 0;
lookup_start_key = ((struct case_heap_entry*)heap_start)->key;
for( ; ((struct case_heap_entry*)heap_start)->addr; )
{
int offset;
int curr_line,last_line;
unsigned short current_addr,last_addr = 0xffff;
int range_start;
current_key = ((struct case_heap_entry*)heap_start)->key ;
curr_line = ((struct case_heap_entry*)heap_start)->line ;
current_addr = ((struct case_heap_entry*)heap_start)->addr ;
if ( current_key == last_key &&
mem_block[A_CASE_LABELS].current_size )
{
char buf[90];
sprintf(buf,"Duplicate case in line %d and %d",
last_line, curr_line);
yyerror(buf);
}
if (curr_line) {
if (last_addr == 1) {
char buf[120];
sprintf(buf,
"Discontinued case label list range, line %d by line %d",
last_line, curr_line);
yyerror(buf);
}
else if (current_key == last_key + 1
&& current_addr == last_addr) {
if (mem_block[A_CASE_LABELS].current_size
!= range_start + 6) {
*(short*)(mem_block[A_CASE_LABELS].block+range_start+4)
=1;
mem_block[A_CASE_LABELS].current_size = range_start + 6;
}
} else {
range_start = mem_block[A_CASE_LABELS].current_size;
}
}
last_key = current_key;
last_line = curr_line;
last_addr = current_addr;
add_to_mem_block(A_CASE_LABELS,
(char *)¤t_key, sizeof(long) );
add_to_mem_block(A_CASE_LABELS,
(char *)¤t_addr, sizeof(short) );
for ( offset = 0; ; )
{
int child1,child2;
child1 = ( offset << 1 ) + sizeof(struct case_heap_entry);
child2 = child1 + sizeof(struct case_heap_entry);
if ( child1 >= heap_end_offs ) break;
if ( ((struct case_heap_entry*)(heap_start+child1))->addr &&
( !((struct case_heap_entry*)(heap_start+child2))->addr ||
((struct case_heap_entry*)(heap_start+child1))->key <=
((struct case_heap_entry*)(heap_start+child2))->key ) )
{
*(struct case_heap_entry*)(heap_start+offset) =
*(struct case_heap_entry*)(heap_start+child1);
offset = child1;
} else
if (((struct case_heap_entry*)(heap_start+child2))->addr ) {
*(struct case_heap_entry*)(heap_start+offset) =
*(struct case_heap_entry*)(heap_start+child2);
offset = child2;
} else break;
}
((struct case_heap_entry*)(heap_start+offset))->addr = 0;
}
/* write start of table */
upd_short(current_break_address-2,
mem_block[A_PROGRAM].current_size);
add_to_mem_block(A_PROGRAM, mem_block[A_CASE_LABELS].block,
mem_block[A_CASE_LABELS].current_size );
/* calculate starting index for itarative search at execution time */
for(i=0xf0,o=6; o<<1 <= mem_block[A_CASE_LABELS].current_size; )
i++,o<<=1;
if (block_index == A_CASE_STRINGS) i = ( i << 4 ) | 0xf;
/* and store it */
mem_block[A_PROGRAM].block[current_break_address-3] &= i;
#if 0 /* neither the code for ordinary switch is fully debugged now,
* nor is the code for packed switch tables complete */
d = ((struct case_heap_entry*)heap_start)->key;
if ( (r-d)*sizeof(short) < heap_end_offs ) {
mem_block[A_PROGRAM].block[current_break_address-3] &= 0xfe;
upd_short(current_break_address-2, mem_block[A_PROGRAM].current_size);
size_without_table = mem_block[A_PROGRAM].current_size;
r = heap_end_offs / sizeof(struct case_heap_entry);
add_to_mem_block(A_PROGRAM,mem_block[A_PROGRAM]->block,
r * sizeof(short) );
memset(mem_block[A_PROGRAM]->block+size_without_table,
'\0',r * sizeof(short) );
ins_long( d );
for(; --r; heap_start += sizeof(struct case_heap_entry) )
{
upd_short(size_without_table + sizeof(short)*
( ((struct case_heap_entry*)heap_start)->key - d )
, ((struct case_heap_entry*)heap_start)->addr );
}
}
#endif /* 0 */
upd_short(current_break_address, mem_block[A_PROGRAM].current_size);
mem_block[A_CASE_NUMBERS].current_size = current_case_number_heap;
mem_block[A_CASE_STRINGS].current_size = current_case_string_heap;
current_break_address = pop_address();
zero_case_label = pop_address();
current_case_string_heap = pop_address();
current_case_number_heap = pop_address();
current_break_stack_need -= sizeof(short);
} ;
case: F_CASE case_label ':'
{
struct case_heap_entry temp;
if ( !( current_break_address & BREAK_FROM_CASE ) ) {
yyerror("Case outside switch");
break;
}
temp.key = $2.key;
temp.addr = mem_block[A_PROGRAM].current_size;
temp.line = current_line;
add_to_case_heap($2.block,&temp);
}
| F_CASE case_label F_RANGE case_label ':'
{
struct case_heap_entry temp;
if ( $2.block != A_CASE_NUMBERS || $4.block != A_CASE_NUMBERS )
yyerror("String case labels not allowed as range bounds");
if ($2.key > $4.key) break;
temp.key = $2.key;
temp.addr = 1;
temp.line = current_line;
add_to_case_heap(A_CASE_NUMBERS,&temp);
temp.key = $4.key;
temp.addr = mem_block[A_PROGRAM].current_size;
temp.line = 0;
add_to_case_heap(A_CASE_NUMBERS,&temp);
} ;
case_label: constant
{
if ( !(zero_case_label & NO_STRING_CASE_LABELS) )
yyerror("Mixed case label list not allowed");
if ( $$.key = $1 )
zero_case_label |= SOME_NUMERIC_CASE_LABELS;
else
zero_case_label |= mem_block[A_PROGRAM].current_size;
$$.block = A_CASE_NUMBERS;
}
| string_constant
{
if ( zero_case_label & SOME_NUMERIC_CASE_LABELS )
yyerror("Mixed case label list not allowed");
zero_case_label &= ~NO_STRING_CASE_LABELS;
store_prog_string($1);
$$.key = (int)$1;
$$.block = A_CASE_STRINGS;
}
;
constant: const1
| constant '|' const1 { $$ = $1 | $3; } ;
const1: const2
| const1 '^' const2 { $$ = $1 ^ $3; } ;
const2: const3
| const2 '&' const3 { $$ = $1 & $3; } ;
const3: const4
| const3 F_EQ const4 { $$ = $1 == $3; }
| const3 F_NE const4 { $$ = $1 != $3; } ;
const4: const5
| const4 '>' const5 { $$ = $1 > $3; }
| const4 F_GE const5 { $$ = $1 >= $3; }
| const4 '<' const5 { $$ = $1 < $3; }
| const4 F_LE const5 { $$ = $1 <= $3; } ;
const5: const6
| const5 F_LSH const6 { $$ = $1 << $3; }
| const5 F_RSH const6 { $$ = $1 >> $3; } ;
const6: const7
| const6 '+' const7 { $$ = $1 + $3; }
| const6 '-' const7 { $$ = $1 - $3; } ;
const7: const8
| const7 '*' const8 { $$ = $1 * $3; }
| const7 '%' const8 { $$ = $1 % $3; }
| const7 '/' const8 { $$ = $1 / $3; } ;
const8: const9
| '(' constant ')' { $$ = $2; } ;
const9: F_NUMBER
| '-' F_NUMBER { $$ = -$2; }
| F_NOT F_NUMBER { $$ = !$2; }
| '~' F_NUMBER { $$ = ~$2; } ;
default: F_DEFAULT ':'
{
if ( !( current_break_address & BREAK_FROM_CASE ) ) {
yyerror("Default outside switch");
break;
}
current_break_address &= ~(BREAK_ON_STACK|BREAK_FROM_CASE);
if ( read_short(current_break_address+2 ) )
yyerror("Duplicate default");
upd_short(current_break_address+2, mem_block[A_PROGRAM].current_size);
current_break_address |= (BREAK_ON_STACK|BREAK_FROM_CASE);
} ;
comma_expr: expr0 { $$ = $1; }
| comma_expr { ins_f_byte(F_POP_VALUE); }
',' expr0
{ $$ = $4; } ;
expr0: expr01
| lvalue assign expr0
{
if (exact_types && !compatible_types($1, $3) &&
!($1 == TYPE_STRING && $3 == TYPE_NUMBER && $2 == F_ADD_EQ))
{
type_error("Bad assignment. Rhs", $3);
}
ins_f_byte($2);
$$ = $3;
}
| error assign expr01 { yyerror("Illegal LHS"); $$ = TYPE_ANY; };
expr01: expr1 { $$ = $1; }
| expr1 '?'
{
ins_f_byte(F_JUMP_WHEN_ZERO);
push_address();
ins_short(0);
}
expr01
{
int i;
i = pop_address();
ins_f_byte(F_JUMP); push_address(); ins_short(0);
upd_short(i, mem_block[A_PROGRAM].current_size);
}
':' expr01
{
upd_short(pop_address(), mem_block[A_PROGRAM].current_size);
if (exact_types && !compatible_types($4, $7)) {
type_error("Different types in ?: expr", $4);
type_error(" and ", $7);
}
if ($4 == TYPE_ANY) $$ = $7;
else if ($7 == TYPE_ANY) $$ = $4;
else if (TYPE($4, TYPE_MOD_POINTER|TYPE_ANY)) $$ = $7;
else if (TYPE($7, TYPE_MOD_POINTER|TYPE_ANY)) $$ = $4;
else $$ = $4;
};
assign: '=' { $$ = F_ASSIGN; }
| F_AND_EQ { $$ = F_AND_EQ; }
| F_OR_EQ { $$ = F_OR_EQ; }
| F_XOR_EQ { $$ = F_XOR_EQ; }
| F_LSH_EQ { $$ = F_LSH_EQ; }
| F_RSH_EQ { $$ = F_RSH_EQ; }
| F_ADD_EQ { $$ = F_ADD_EQ; }
| F_SUB_EQ { $$ = F_SUB_EQ; }
| F_MULT_EQ { $$ = F_MULT_EQ; }
| F_MOD_EQ { $$ = F_MOD_EQ; }
| F_DIV_EQ { $$ = F_DIV_EQ; };
return: F_RETURN
{
if (exact_types && !TYPE(exact_types, TYPE_VOID))
type_error("Must return a value for a function declared",
exact_types);
ins_f_byte(F_CONST0);
ins_f_byte(F_RETURN);
}
| F_RETURN comma_expr
{
if (exact_types && !TYPE($2, exact_types & TYPE_MOD_MASK))
type_error("Return type not matching", exact_types);
ins_f_byte(F_RETURN);
};
expr_list: /* empty */ { $$ = 0; }
| expr_list2 { $$ = $1; }
| expr_list2 ',' { $$ = $1; } ; /* Allow a terminating comma */
expr_list2: expr0 { $$ = 1; add_arg_type($1); }
| expr_list2 ',' expr0 { $$ = $1 + 1; add_arg_type($3); } ;
expr1: expr2 { $$ = $1; }
| expr2 F_LOR
{
ins_f_byte(F_DUP); ins_f_byte(F_JUMP_WHEN_NON_ZERO);
push_address();
ins_short(0);
ins_f_byte(F_POP_VALUE);
}
expr1
{
upd_short(pop_address(), mem_block[A_PROGRAM].current_size);
if ($1 == $4)
$$ = $1;
else
$$ = TYPE_ANY; /* Return type can't be known */
};
expr2: expr211 { $$ = $1; }
| expr211 F_LAND
{
ins_f_byte(F_DUP); ins_f_byte(F_JUMP_WHEN_ZERO);
push_address();
ins_short(0);
ins_f_byte(F_POP_VALUE);
}
expr2
{
upd_short(pop_address(), mem_block[A_PROGRAM].current_size);
if ($1 == $4)
$$ = $1;
else
$$ = TYPE_ANY; /* Return type can't be known */
} ;
expr211: expr212
| expr211 '|' expr212
{
if (exact_types && !TYPE($1,TYPE_NUMBER))
type_error("Bad argument 1 to |", $1);
if (exact_types && !TYPE($3,TYPE_NUMBER))
type_error("Bad argument 2 to |", $3);
$$ = TYPE_NUMBER;
ins_f_byte(F_OR);
};
expr212: expr213
| expr212 '^' expr213
{
if (exact_types && !TYPE($1,TYPE_NUMBER))
type_error("Bad argument 1 to ^", $1);
if (exact_types && !TYPE($3,TYPE_NUMBER))
type_error("Bad argument 2 to ^", $3);
$$ = TYPE_NUMBER;
ins_f_byte(F_XOR);
};
expr213: expr22
| expr213 '&' expr22
{
ins_f_byte(F_AND);
if ( !TYPE($1,TYPE_MOD_POINTER) || !TYPE($3,TYPE_MOD_POINTER) ) {
if (exact_types && !TYPE($1,TYPE_NUMBER))
type_error("Bad argument 1 to &", $1);
if (exact_types && !TYPE($3,TYPE_NUMBER))
type_error("Bad argument 2 to &", $3);
}
$$ = TYPE_NUMBER;
};
expr22: expr23
| expr24 F_EQ expr24
{
int t1 = $1 & TYPE_MOD_MASK, t2 = $3 & TYPE_MOD_MASK;
if (exact_types && t1 != t2 && t1 != TYPE_ANY && t2 != TYPE_ANY) {
type_error("== always false because of different types", $1);
type_error(" compared to", $3);
}
ins_f_byte(F_EQ);
$$ = TYPE_NUMBER;
};
| expr24 F_NE expr24
{
int t1 = $1 & TYPE_MOD_MASK, t2 = $3 & TYPE_MOD_MASK;
if (exact_types && t1 != t2 && t1 != TYPE_ANY && t2 != TYPE_ANY) {
type_error("!= always true because of different types", $1);
type_error(" compared to", $3);
}
ins_f_byte(F_NE);
$$ = TYPE_NUMBER;
};
expr23: expr24
| expr24 '>' expr24
{ $$ = TYPE_NUMBER; ins_f_byte(F_GT); };
| expr24 F_GE expr24
{ $$ = TYPE_NUMBER; ins_f_byte(F_GE); };
| expr24 '<' expr24
{ $$ = TYPE_NUMBER; ins_f_byte(F_LT); };
| expr24 F_LE expr24
{ $$ = TYPE_NUMBER; ins_f_byte(F_LE); };
expr24: expr25
| expr24 F_LSH expr25
{
ins_f_byte(F_LSH);
$$ = TYPE_NUMBER;
if (exact_types && !TYPE($1, TYPE_NUMBER))
type_error("Bad argument number 1 to '<<'", $1);
if (exact_types && !TYPE($3, TYPE_NUMBER))
type_error("Bad argument number 2 to '<<'", $3);
};
| expr24 F_RSH expr25
{
ins_f_byte(F_RSH);
$$ = TYPE_NUMBER;
if (exact_types && !TYPE($1, TYPE_NUMBER))
type_error("Bad argument number 1 to '>>'", $1);
if (exact_types && !TYPE($3, TYPE_NUMBER))
type_error("Bad argument number 2 to '>>'", $3);
};
expr25: expr27
| expr25 '+' expr27 /* Type checks of this case is complicated */
{ ins_f_byte(F_ADD); $$ = TYPE_ANY; };
| expr25 '-' expr27
{
int bad_arg = 0;
if (exact_types) {
if (!TYPE($1, TYPE_NUMBER) && !($1 & TYPE_MOD_POINTER) ) {
type_error("Bad argument number 1 to '-'", $1);
bad_arg++;
}
if (!TYPE($3, TYPE_NUMBER) && !($3 & TYPE_MOD_POINTER) ) {
type_error("Bad argument number 2 to '-'", $3);
bad_arg++;
}
}
$$ = TYPE_ANY;
if (($1 & TYPE_MOD_POINTER) || ($3 & TYPE_MOD_POINTER))
$$ = TYPE_MOD_POINTER | TYPE_ANY;
if (!($1 & TYPE_MOD_POINTER) || !($3 & TYPE_MOD_POINTER)) {
if (exact_types && $$ != TYPE_ANY && !bad_arg)
yyerror("Arguments to '-' don't match");
$$ = TYPE_NUMBER;
}
ins_f_byte(F_SUBTRACT);
};
expr27: expr28
| expr27 '*' expr3
{
if (exact_types && !TYPE($1, TYPE_NUMBER))
type_error("Bad argument number 1 to '*'", $1);
if (exact_types && !TYPE($3, TYPE_NUMBER))
type_error("Bad argument number 2 to '*'", $3);
ins_f_byte(F_MULTIPLY);
$$ = TYPE_NUMBER;
};
| expr27 '%' expr3
{
if (exact_types && !TYPE($1, TYPE_NUMBER))
type_error("Bad argument number 1 to '%'", $1);
if (exact_types && !TYPE($3, TYPE_NUMBER))
type_error("Bad argument number 2 to '%'", $3);
ins_f_byte(F_MOD);
$$ = TYPE_NUMBER;
};
| expr27 '/' expr3
{
if (exact_types && !TYPE($1, TYPE_NUMBER))
type_error("Bad argument number 1 to '/'", $1);
if (exact_types && !TYPE($3, TYPE_NUMBER))
type_error("Bad argument number 2 to '/'", $3);
ins_f_byte(F_DIVIDE);
$$ = TYPE_NUMBER;
};
expr28: expr3
| cast expr3
{
$$ = $1;
if (exact_types && $2 != TYPE_ANY && $2 != TYPE_UNKNOWN &&
$1 != TYPE_VOID)
type_error("Casts are only legal for type mixed, or when unknown", $2);
} ;
expr3: expr31
| F_INC lvalue
{
ins_f_byte(F_INC);
if (exact_types && !TYPE($2, TYPE_NUMBER))
type_error("Bad argument to ++", $2);
$$ = TYPE_NUMBER;
};
| F_DEC lvalue
{
ins_f_byte(F_DEC);
if (exact_types && !TYPE($2, TYPE_NUMBER))
type_error("Bad argument to --", $2);
$$ = TYPE_NUMBER;
};
| F_NOT expr3
{
ins_f_byte(F_NOT); /* Any type is valid here. */
$$ = TYPE_NUMBER;
};
| '~' expr3
{
ins_f_byte(F_COMPL);
if (exact_types && !TYPE($2, TYPE_NUMBER))
type_error("Bad argument to ~", $2);
$$ = TYPE_NUMBER;
};
| '-' expr3
{
ins_f_byte(F_NEGATE);
if (exact_types && !TYPE($2, TYPE_NUMBER))
type_error("Bad argument to unary '-'", $2);
$$ = TYPE_NUMBER;
};
expr31: expr4
| lvalue F_INC
{
ins_f_byte(F_POST_INC);
if (exact_types && !TYPE($1, TYPE_NUMBER))
type_error("Bad argument to ++", $1);
$$ = TYPE_NUMBER;
};
| lvalue F_DEC
{
ins_f_byte(F_POST_DEC);
if (exact_types && !TYPE($1, TYPE_NUMBER))
type_error("Bad argument to --", $1);
$$ = TYPE_NUMBER;
};
expr4: function_call
| lvalue
{
int pos = mem_block[A_PROGRAM].current_size;
/* Some optimization. Replace the push-lvalue with push-value */
if (last_push_identifier == pos-2)
mem_block[A_PROGRAM].block[last_push_identifier] =
F_IDENTIFIER - F_OFFSET;
else if (last_push_local == pos-2)
mem_block[A_PROGRAM].block[last_push_local] =
F_LOCAL_NAME - F_OFFSET;
else if (last_push_indexed == pos-1)
mem_block[A_PROGRAM].block[last_push_indexed] =
F_INDEX - F_OFFSET;
else if (last_push_indexed != 0)
fatal("Should be a push at this point !\n");
$$ = $1;
}
| string | number
| '(' comma_expr ')' { $$ = $2; }
| catch { $$ = TYPE_ANY; }
| sscanf { $$ = TYPE_NUMBER; }
| parse_command { $$ = TYPE_NUMBER; }
| '(' '{' expr_list '}' ')'
{
pop_arg_stack($3); /* We don't care about these types */
ins_f_byte(F_AGGREGATE);
ins_short($3);
$$ = TYPE_MOD_POINTER | TYPE_ANY;
};
catch: F_CATCH { ins_f_byte(F_CATCH); push_address(); ins_short(0);}
'(' comma_expr ')'
{
ins_f_byte(F_POP_VALUE);
#if 1
ins_f_byte(F_CONST0);
ins_f_byte(F_THROW);
#else
ins_f_byte(F_RETURN);
#endif
upd_short(pop_address(),
mem_block[A_PROGRAM].current_size);
};
sscanf: F_SSCANF '(' expr0 ',' expr0 lvalue_list ')'
{
ins_f_byte(F_SSCANF); ins_byte($6 + 2);
}
parse_command: F_PARSE_COMMAND '(' expr0 ',' expr0 ',' expr0 lvalue_list ')'