forked from cotillion/cd-gamedriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postlang.y
3264 lines (2931 loc) · 85.5 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
%{
#line 3 "postlang.y"
/*
* These are token values that needn't have an associated code for the
* compiled file
*/
%}
%right F_ELSE
%token F_WHILE F_DO F_FOR F_STORE F_LAND F_LOR F_STATUS F_IF F_COMMA F_INT
%token F_CONTINUE F_INHERIT F_COLON_COLON F_VARARGS F_VARARG F_SUBSCRIPT
%token F_BREAK
%token F_OBJECT F_VOID F_MIXED F_PRIVATE F_NO_MASK F_MAPPING F_FLOAT
%token F_PUBLIC F_FUNCTION F_OPERATOR
%token F_CASE F_DEFAULT
%union
{
long long number;
double real;
unsigned int address; /* Address of an instruction */
char *string;
short type;
struct { long long key; char block; } case_label;
struct function *funp;
}
%type <number> assign F_NUMBER constant F_LOCAL_NAME expr_list funop
%type <number> oexpr_list oexpr_list1
%type <number> const1 const2 const3 const4 const5 const6 const7 const8 const9
%type <number> lvalue_list fun_args 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 m_expr_list m_expr_list2
%type <number> new_local_name2
%type <string> identifier identifier2 fun_identifier opt_inherit_name
%type <string> F_IDENTIFIER F_STRING string_con1 string_constant
%type <real> F_FLOATC
%type <case_label> case_label
/* The following symbols return type information */
%type <type> lvalue lvaluec string cast expr01 c_expr
%type <type> comma_expr rvalue funident funexpr opt_expr01
%type <type> expr2 expr211 expr1 expr212 expr213 expr24 expr22 expr23 expr25
%type <type> expr27 expr28 expr3 expr31 expr4 number expr0 real
/* %expect 2 */ /* You can uncomment previous statement if you use bison */
%{
/* has to be here so that F_EXT is defined */
static void store_reloc_data (char, unsigned short, char *name, int, char);
static void ins_f_byte (unsigned int);
static void start_block(void);
static void end_block(void);
static int first_default_arg, current_arg;
static int is_ctor, is_dtor;
static int block_depth;
static int return_label;
#define CTOR_FUNC ((char *)1)
#define DTOR_FUNC ((char *)2)
%}
%%
all: { block_depth = 0; } program ;
program: program def possible_semi_colon
| /* empty */ ;
possible_semi_colon: /* empty */
| ';' { yyerror("Extra ';'. Ignored."); };
inheritance: type_modifier_list F_INHERIT string_constant opt_inherit_name ';'
{
struct object *ob;
if (mem_block[A_VARIABLES].current_size ||
mem_block[A_FUNCTIONS].current_size)
yyerror("Inherit after other definitions\n");
ob = find_object2($3);
if (ob == 0) {
if (inherit_file)
free(inherit_file);
inherit_file = string_copy($3);
/* Return back to load_object() */
YYACCEPT;
}
if (ob->prog->flags & PRAGMA_NO_INHERIT)
{
yyerror("Inheriting bad object.\n");
}
if (!check_inherits(ob->prog))
{
if (inherit_file)
free(inherit_file);
inherit_file = string_copy($3);
/* Return back to load_object() */
YYACCEPT;
}
copy_inherits(ob->prog, $1, $4);
};
opt_inherit_name: /* empty */
{
char *n = strrchr($<string>0, '/');
if (n)
{
char *s = tmpstring_copy(n + 1);
char *p = strchr(s, '.');
if (p)
{
*p = '\0';
$$ = tmpstring_copy(s);
}
$$ = s;
}
else
$$ = tmpstring_copy($<string>0);
}
| F_IDENTIFIER
{
$$ = $1;
} ;
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_mem(&$1, sizeof($1)); $$ = TYPE_NUMBER;
}
} ;
optional_star: /* empty */ { $$ = 0; } | '*' { $$ = TYPE_MOD_POINTER; } ;
block_or_semi: block { $$ = 0; }
| ';' { $$ = ';'; }
| '=' string_con1 ';' { $<string>$ = $<string>2; }
| '=' F_NUMBER ';'
{
if ($2 != 0)
{
yyerror("Syntax error");
}
$$ = 1;
} ;
def: type optional_star fun_identifier
{
/* Save start of function. */
clear_init_arg_stack();
deref_index = 0;
push_address();
try_level = break_try_level = continue_try_level = 0;
true_varargs = 0;
first_default_arg = current_arg = 0;
is_ctor = is_dtor = 0;
return_label = make_label();
if ($3 == CTOR_FUNC)
{
$3 = tmpstring_copy(".CTOR");
is_ctor = 1;
}
else if ($3 == DTOR_FUNC)
{
$3 = tmpstring_copy(".DTOR");
is_dtor = 1;
}
if (is_ctor | is_dtor)
{
if ($1 | $2)
{
yyerror("Constructors and destructors don't have a type");
$1 = $2 = 0;
}
if (pragma_strict_types)
{
$1 = exact_types = TYPE_VOID;
}
else
exact_types = 0;
}
else if ($1 & TYPE_MASK)
{
exact_types = $1 | $2;
}
else
{
if (pragma_strict_types)
yyerror("\"#pragma strict_types\" requires type of function");
exact_types = 0;
}
}
fun_args
{
/*
* Define a prototype. If it is a real function, then the
* prototype will be replaced below.
*/
if ($5 && is_ctor | is_dtor)
{
yyerror("Constructors and destructors don't take arguments");
}
ins_f_byte(F_JUMP);
push_address();
ins_short(0);
{
int i;
unsigned short j;
i = pop_address();
push_address();
dump_init_arg_table($5);
j = mem_block[A_PROGRAM].current_size;
upd_short(i, (short)mem_block[A_PROGRAM].current_size);
((char *)mem_block[A_PROGRAM].block)[i] = ((char *)&j)[0];
((char *)mem_block[A_PROGRAM].block)[i + 1] = ((char *)&j)[1];
define_new_function($3, (char)$5, 0, 0,
NAME_PROTOTYPE | $1 | $2 | true_varargs,
(char)first_default_arg);
}
}
block_or_semi
{
/* Either a prototype or a block */
if ($7 == ';') /* its only a prototype */
{
(void)pop_address(); /* Not used here */
mem_block[A_PROGRAM].current_size =
pop_address();
}
else if ($7 == 0)
{
define_new_function($3, (char)$5,
(unsigned char)(max_number_of_locals - $5),
pop_address(), $1 | $2 | true_varargs,
(char)first_default_arg);
ins_f_byte(F_CONST0);
set_label(return_label, (short)mem_block[A_PROGRAM].current_size);
ins_f_byte(F_RETURN);
(void)pop_address(); /* Not used here */
}
else if ($7 == 1)
{
if (is_ctor | is_dtor)
yyerror("Constructors and destructors can't be abstract");
define_new_function($3, (char)$5, 0, 0,
NAME_ABSTRACT | $1 | $2 | true_varargs,
(char)first_default_arg);
yyerror("Abstract functions aren't implemented yet");
}
else
{
extern void (* get_C_fun_address(char *, char *))(struct svalue *);
void (*funca)(struct svalue *);
define_new_function($3, (char)$5,
(unsigned char)(max_number_of_locals - $5),
pop_address(), $1 | $2 | true_varargs,
(char)first_default_arg);
ins_f_byte(F_CALL_C);
funca = get_C_fun_address(current_file, $<string>7);
if (!funca)
{
char buf[256];
(void)snprintf(buf, sizeof(buf), "Undefined interface %s() = \"%s\".\n",
$3, $<string>7);
yyerror(buf);
}
ins_mem(&funca, sizeof(funca));
ins_f_byte(F_RETURN);
(void)pop_address(); /* Not used here */
}
free_all_local_names();
is_ctor = is_dtor = 0;
}
| 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");
(void)add_local_name($3, TYPE_ANY); /* Supress more errors */
} else {
(void)add_local_name($3, $1 | $2);
}
if (first_default_arg != current_arg)
yyerror("non default argument defined after default one");
else
first_default_arg++;
push_init_arg_address(mem_block[A_PROGRAM].current_size);
}
| type optional_star F_IDENTIFIER
{
int var_num;
if (exact_types && $1 == 0) {
yyerror("Missing type for argument");
var_num = add_local_name($3, TYPE_ANY); /* Supress more errors */
} else {
var_num = add_local_name($3, $1 | $2);
}
push_init_arg_address(mem_block[A_PROGRAM].current_size);
ins_f_byte(F_PUSH_LOCAL_VARIABLE_LVALUE);
ins_byte((char)var_num);
}
'=' expr0
{
if (!compatible_types((($1 ? $1 : TYPE_ANY) | $2) & TYPE_MASK, $6)){
char buff[100];
(void)snprintf(buff, sizeof(buff), "Type mismatch %s when initializing %s",
get_two_types($1 | $2, $6), $3);
yyerror(buff);
}
ins_f_byte(F_ASSIGN);
ins_f_byte(F_POP_VALUE);
}
fun_args: '(' /* empty */ ')' { $$ = 0; }
| '(' argument ')' { $$ = $2; }
| '(' F_VOID ')' { $$ = 0; };
argument: argument_list
| argument_list ',' F_VARARG
{
int var_num = add_local_name(tmpstring_copy("argv"), TYPE_ANY | TYPE_MOD_POINTER);
$$ = $1 + 1;
true_varargs = TYPE_MOD_TRUE_VARARGS;
push_init_arg_address(mem_block[A_PROGRAM].current_size);
ins_f_byte(F_PUSH_LOCAL_VARIABLE_LVALUE);
ins_byte((char)var_num);
ins_f_byte(F_AGGREGATE);
ins_short(0);
ins_f_byte(F_ASSIGN);
ins_f_byte(F_POP_VALUE);
current_arg++;
}
| F_VARARG
{
int var_num = add_local_name(tmpstring_copy("argv"), TYPE_ANY | TYPE_MOD_POINTER);
$$ = 1;
true_varargs = TYPE_MOD_TRUE_VARARGS;
push_init_arg_address(mem_block[A_PROGRAM].current_size);
ins_f_byte(F_PUSH_LOCAL_VARIABLE_LVALUE);
ins_byte((char)var_num);
ins_f_byte(F_AGGREGATE);
ins_short(0);
ins_f_byte(F_ASSIGN);
ins_f_byte(F_POP_VALUE);
current_arg++;
};
argument_list: new_arg_name { $$ = 1; current_arg++; }
| argument_list ',' new_arg_name { $$ = $1 + 1; current_arg++; } ;
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; } ;
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 = $$; }
| F_MAPPING { $$ = TYPE_MAPPING; current_type = $$; }
| F_FLOAT { $$ = TYPE_FLOAT; current_type = $$; }
| F_FUNCTION { $$ = TYPE_FUNCTION; current_type = $$; };
name_list: new_name
| new_name ',' name_list;
new_name: optional_star F_IDENTIFIER
{
define_variable($2, current_type | $1);
}
| optional_star F_IDENTIFIER
{
define_variable($2, current_type | $1);
transfer_init_control();
ins_f_byte(F_PUSH_IDENTIFIER_LVALUE);
ins_byte((char)variable_inherit_found);
ins_byte((char)variable_index_found);
}
'=' expr0
{
if (pragma_strict_types &&
!compatible_types((current_type | $1) & TYPE_MASK, $5)){
char buff[100];
(void)snprintf(buff, sizeof(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();
} ;
block: '{'
{
start_block();
}
statements '}'
{
end_block();
};
local_declaration: basic_type local_name_list ';' { ;} ;
new_local_name2: optional_star F_IDENTIFIER
{
$$ = add_local_name($2, current_type | $1);
}
new_local_name: new_local_name2
{ /* To shut bison up */ }
| new_local_name2
{
ins_f_byte(F_PUSH_LOCAL_VARIABLE_LVALUE);
ins_byte((char)$1);
}
'=' expr0
{
if (exact_types &&
!compatible_types(type_of_locals[$1] & TYPE_MASK, $4))
{
char buff[100];
(void)snprintf(buff, sizeof(buff),
"Type mismatch %s when initializing %s",
get_two_types(type_of_locals[$1], $4),
local_names[$1]);
yyerror(buff);
}
ins_f_byte(F_ASSIGN);
ins_f_byte(F_POP_VALUE);
};
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 (exact_types && !TYPE($1,TYPE_VOID))
yyerror("Value thrown away"); */
}
| cond | try | while | do | for | switch | case | default | return ';'
| foreach | foreach_m | block | local_declaration
| /* empty */ ';'
| F_BREAK ';' /* This code is a jump to a jump */
{
if (current_break_address == 0)
yyerror("break statement outside loop");
for (int i = try_level; i > break_try_level; i--)
ins_f_byte(F_END_TRY);
ins_f_byte(F_JUMP);
add_jump();
ins_label(current_break_address);
}
| F_CONTINUE ';' /* This code is a jump to a jump */
{
if (current_continue_address == 0)
yyerror("continue statement outside loop");
for (int i = try_level; i > continue_try_level; i--)
ins_f_byte(F_END_TRY);
ins_f_byte(F_JUMP);
add_jump();
ins_label(current_continue_address);
}
;
try: F_TRY
{
ins_f_byte(F_TRY);
push_address();
ins_short(0);
ins_byte(0);
try_level++;
} statement F_CATCH {start_block();} '(' basic_type new_local_name2 ')'
{
short try_start = pop_address();
try_level--;
ins_f_byte(F_END_TRY);
ins_f_byte(F_JUMP);
push_address();
ins_short(0);
upd_short(try_start,
(short)mem_block[A_PROGRAM].current_size);
upd_byte(try_start + 2, $8);
} statement
{
end_block();
upd_short(pop_address(),
(short)mem_block[A_PROGRAM].current_size);
};
while: { push_explicit(current_continue_address);
push_explicit(continue_try_level);
push_explicit(current_break_address);
push_explicit(break_try_level);
current_continue_address = make_label();
current_break_address = make_label();
continue_try_level = break_try_level = try_level;
set_label(current_continue_address, (short)mem_block[A_PROGRAM].current_size);
} F_WHILE '(' comma_expr ')'
{
ins_f_byte(F_JUMP_WHEN_ZERO);
add_jump();
ins_label(current_break_address);
}
statement
{
ins_f_byte(F_JUMP);
add_jump();
ins_label(current_continue_address);
set_label(current_break_address, (short)mem_block[A_PROGRAM].current_size);
break_try_level = pop_address();
current_break_address = pop_address();
continue_try_level = pop_address();
current_continue_address = pop_address();
};
do: {
push_explicit(current_continue_address);
push_explicit(continue_try_level);
push_explicit(current_break_address);
push_explicit(break_try_level);
current_continue_address = make_label();
current_break_address = make_label();
continue_try_level = break_try_level = try_level;
push_address();
}
F_DO statement
{
set_label(current_continue_address, (short)mem_block[A_PROGRAM].current_size);
}
F_WHILE '(' comma_expr ')' ';'
{
ins_f_byte(F_JUMP_WHEN_NON_ZERO);
add_jump();
ins_short((short)pop_address());
set_label(current_break_address, (short)mem_block[A_PROGRAM].current_size);
break_try_level = pop_address();
current_break_address = pop_address();
continue_try_level = pop_address();
current_continue_address = pop_address();
};
start_block:
{ start_block();} ;
foreach_var: basic_type new_local_name2 {
ins_f_byte(F_PUSH_LOCAL_VARIABLE_LVALUE);
ins_byte((char)$2);
} ;
foreach_m: F_FOREACH start_block '(' foreach_var ',' foreach_var ':' comma_expr ')' {
ins_f_byte(F_DUP);
ins_f_byte(F_M_INDEXES);
ins_f_byte(F_CONST0);
push_explicit(current_continue_address);
push_explicit(continue_try_level);
push_explicit(current_break_address);
push_explicit(break_try_level);
current_continue_address = make_label();
current_break_address = make_label();
continue_try_level = break_try_level = try_level;
set_label(current_continue_address, get_address());
ins_f_byte(F_FOREACH_M);
add_jump();
ins_label(current_break_address);
}
statement
{
ins_f_byte(F_JUMP);
add_jump();
ins_label(current_continue_address);
set_label(current_break_address, get_address());
ins_f_byte(F_POP_VALUE);
ins_f_byte(F_POP_VALUE);
ins_f_byte(F_POP_VALUE);
ins_f_byte(F_POP_VALUE);
ins_f_byte(F_POP_VALUE);
end_block();
break_try_level = pop_address();
current_break_address = pop_address();
continue_try_level = pop_address();
current_continue_address = pop_address();
};
foreach: F_FOREACH start_block '(' foreach_var ':' comma_expr ')' {
ins_f_byte(F_CONST0);
push_explicit(current_continue_address);
push_explicit(continue_try_level);
push_explicit(current_break_address);
push_explicit(break_try_level);
current_continue_address = make_label();
current_break_address = make_label();
continue_try_level = break_try_level = try_level;
set_label(current_continue_address, get_address());
ins_f_byte(F_FOREACH);
add_jump();
ins_label(current_break_address);
}
statement
{
ins_f_byte(F_JUMP);
add_jump();
ins_label(current_continue_address);
set_label(current_break_address, get_address());
ins_f_byte(F_POP_VALUE);
ins_f_byte(F_POP_VALUE);
ins_f_byte(F_POP_VALUE);
end_block();
break_try_level = pop_address();
current_break_address = pop_address();
continue_try_level = pop_address();
current_continue_address = pop_address();
};
for_init_expr: for_expr
| basic_type local_name_list {;};
for: F_FOR '(' start_block {
push_explicit(current_continue_address);
push_explicit(continue_try_level);
push_explicit(current_break_address);
push_explicit(break_try_level);
continue_try_level = break_try_level = try_level;
} for_init_expr ';' {
push_address();
}
for_guard ';' {
push_address();
ins_short(0);
current_continue_address = make_label();
current_break_address = make_label();
ins_f_byte(F_JUMP);
add_jump();
ins_label(current_break_address);
set_label(current_continue_address, (short)mem_block[A_PROGRAM].current_size);
}
for_expr ')' {
upd_short(pop_address(), (short)(mem_block[A_PROGRAM].current_size + 3));
ins_f_byte(F_JUMP);
add_jump();
ins_short((short)pop_address());
}
statement
{
ins_f_byte(F_JUMP);
add_jump();
ins_label(current_continue_address);
set_label(current_break_address, (short)mem_block[A_PROGRAM].current_size);
end_block();
break_try_level = pop_address();
current_break_address = pop_address();
continue_try_level = pop_address();
current_continue_address = pop_address();
};
for_guard: /* EMPTY */
{
ins_f_byte(F_JUMP);
add_jump();
}
| comma_expr
{
ins_f_byte(F_JUMP_WHEN_NON_ZERO);
add_jump();
}
;
for_expr: /* EMPTY */
| comma_expr { ins_f_byte(F_POP_VALUE); } ;
switch: F_SWITCH '(' comma_expr ')'
{
/* initialize switch */
push_explicit(current_case_number_heap);
push_explicit(current_case_string_heap);
push_explicit(zero_case_label);
push_explicit(current_break_address);
push_explicit(break_try_level);
break_try_level = try_level;
push_explicit(current_case_address);
ins_f_byte(F_SWITCH);
current_case_address = mem_block[A_PROGRAM].current_size;
ins_byte((char)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 */
ins_short(0); /* break address to push, table is entered before */
ins_short(0); /* default address */
current_break_address = make_label();
}
statement
{
int block_index;
int current_case_heap;
short default_addr;
/* Wrap up switch */
/* It is not unusual that the last case/default has no break
*/
/* Default address */
if (!(default_addr = read_short(current_case_address + 5)))
{
upd_short(current_case_address + 5, /* no default given -> */
(short)mem_block[A_PROGRAM].current_size); /* create one */
default_addr = mem_block[A_PROGRAM].current_size;
}
ins_f_byte(F_JUMP);
add_jump();
ins_label(current_break_address);
/* Table address */
upd_short(current_case_address + 1,
(short)mem_block[A_PROGRAM].current_size);
/* What type of table? */
if (zero_case_label & (NO_STRING_CASE_LABELS|SOME_NUMERIC_CASE_LABELS))
{
block_index = A_CASE_NUMBERS;
current_case_heap = current_case_number_heap;
mem_block[A_PROGRAM].block[current_case_address] = 0;
}
else
{
block_index = A_CASE_STRINGS;
current_case_heap = current_case_string_heap;
mem_block[A_PROGRAM].block[current_case_address] = 1;
if (zero_case_label & 0xffff)
{
ins_long_long(0); /* not used */
ins_short((short)zero_case_label); /* the address */
}
else
{
ins_long_long(0); /* not used */
ins_short(default_addr); /* the address */
}
}
/* Dump the table */
{
struct case_heap_entry *ent, *end;
ent = (struct case_heap_entry *)
(mem_block[block_index].block + current_case_heap);
end = (struct case_heap_entry *)
(mem_block[block_index].block +
mem_block[block_index].current_size);
for (; ent < end; ent++)
{
ins_long_long(ent->key);
ins_short(ent->addr);
}
}
/* Break address */
upd_short(current_case_address + 3,
(short)mem_block[A_PROGRAM].current_size);
set_label(current_break_address,
(short)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_case_address = pop_address();
break_try_level = pop_address();
current_break_address = pop_address();
zero_case_label = pop_address();
current_case_string_heap = pop_address();
current_case_number_heap = pop_address();
};
case: F_CASE case_label ':'
{
/* Register label */
struct case_heap_entry temp;
if ( !current_case_address )
{
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, NULL);
}
| F_CASE case_label F_RANGE case_label ':'
{
/* Register range label */
struct case_heap_entry temp1, temp2;
if ( !current_case_address)
{
yyerror("Case outside switch");
break;
}
if ($2.block != $4.block)
yyerror("Incompatible types in case label range bounds");
else
{
temp1.key = $2.key;
temp1.addr = -1;
temp1.line = current_line;
temp2.key = $4.key;
temp2.addr = mem_block[A_PROGRAM].current_size;
temp2.line = current_line;
add_to_case_heap($2.block, &temp1, &temp2);
}
};
case_label: constant
{
if ( !(zero_case_label & NO_STRING_CASE_LABELS) )
yyerror("Mixed case label list not allowed");
if ( ($$.key = $1) != 0 )
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;
$$.key = (unsigned short)store_prog_string($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 { $$ = (unsigned)$1 >> $3; } ;
const6: const7
| const6 '+' const7 { $$ = $1 + $3; }
| const6 '-' const7 { $$ = $1 - $3; } ;
const7: const8
| const7 '*' const8 { $$ = $1 * $3; }
| const7 F_MOD 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_case_address)
{
yyerror("Default outside switch");
break;
}
if ( read_short(current_case_address + 5 ) )
yyerror("Duplicate default");
upd_short(current_case_address + 5,
(short)mem_block[A_PROGRAM].current_size);
} ;
comma_expr: c_expr
| F_THROW comma_expr { ins_f_byte(F_THROW); $$ = TYPE_NONE; } ;
c_expr: expr0 { $$ = $1; }
| c_expr { ins_f_byte(F_POP_VALUE); }
',' expr0
{ $$ = $4; } ;
expr0: expr01
| lvalue assign expr0
{
deref_index--;
if (!($1 & TYPE_LVALUE))
yyerror("Illegal LHS");
$1 &= ~TYPE_LVALUE;
if (exact_types && !compatible_types($1, $3) &&
!($1 == TYPE_STRING && $3 == TYPE_NUMBER && $2 == F_ADD_EQ) &&
!(($1 == TYPE_STRING || ($1 & TYPE_MOD_POINTER)) &&
$3 == TYPE_NUMBER && $2 == F_MULT_EQ))
type_error("Bad assignment. Rhs", $3);
ins_f_byte((unsigned)$2);
$$ = $3;
}
| error assign expr01 { yyerror("Illegal LHS"); $$ = TYPE_ANY; };
opt_expr01: { $$ = TYPE_NONE;}
| expr01 ;
expr01: expr1 { $$ = $1; }
| expr1 '?'
{
push_address();
ins_f_byte(F_JUMP_WHEN_ZERO);
add_jump();
push_address();
ins_short(0);
}
opt_expr01
{
int i;
i = pop_address();
if ($4 != TYPE_NONE)
{
(void)pop_address();
ins_f_byte(F_JUMP);
add_jump();
push_address();
ins_short(0);
upd_short(i, (short)mem_block[A_PROGRAM].current_size);
}
else
{
mem_block[A_PROGRAM].current_size = pop_address();
ins_f_byte(F_SKIP_NZ);
push_address();
ins_short(0);
$4 = $1;
}
}
':' expr01
{
upd_short(pop_address(), (short)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; }