-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathgrammar.y.pre
3683 lines (3369 loc) · 88.7 KB
/
grammar.y.pre
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
/* This is to make emacs edit this in C mode: -*-C-*- */
%{
#include "std.h"
#include "compiler.h"
#include "lex.h"
#include "scratchpad.h"
#include "lpc_incl.h"
#include "simul_efun.h"
#include "generate.h"
#include "master.h"
/* gross. Necessary? - Beek */
#ifdef WIN32
#define MSDOS
#endif
%line
/*
* This is the grammar definition of LPC, and its parse tree generator.
*/
/* down to one global :)
bits:
SWITCH_CONTEXT - we're inside a switch
LOOP_CONTEXT - we're inside a loop
SWITCH_STRINGS - a string case has been found
SWITCH_NUMBERS - a non-zero numeric case has been found
SWITCH_RANGES - a range has been found
SWITCH_DEFAULT - a default has been found
*/
int context;
int num_refs;
/*
* bison & yacc don't prototype this in y.tab.h
*/
int yyparse PROT((void));
%}
/*
* Token definitions.
*
* Appearing in the precedence declarations are:
* '+' '-' '/' '*' '%'
* '&' '|' '<' '>' '^'
* '~' '?'
*
* Other single character tokens recognized in this grammar:
* '{' '}' ',' ';' ':'
* '(' ')' '[' ']' '$'
*/
%token L_STRING L_NUMBER L_REAL
%token L_BASIC_TYPE L_TYPE_MODIFIER
%token L_DEFINED_NAME L_IDENTIFIER
%token L_EFUN
%token L_INC L_DEC
%token L_ASSIGN
%token L_LAND L_LOR
%token L_LSH L_RSH
%token L_ORDER
%token L_NOT
%token L_IF L_ELSE
%token L_SWITCH L_CASE L_DEFAULT L_RANGE L_DOT_DOT_DOT
%token L_WHILE L_DO L_FOR L_FOREACH L_IN
%token L_BREAK L_CONTINUE
%token L_RETURN
%token L_ARROW L_INHERIT L_COLON_COLON
%token L_ARRAY_OPEN L_MAPPING_OPEN L_FUNCTION_OPEN L_NEW_FUNCTION_OPEN
%token L_SSCANF L_CATCH
%ifdef DEBUG
%token L_TREE
%endif
%ifdef ARRAY_RESERVED_WORD
%token L_ARRAY
%endif
%ifdef REF_RESERVED_WORD
%token L_REF
%endif
%token L_PARSE_COMMAND L_TIME_EXPRESSION
%token L_CLASS L_NEW
%token L_PARAMETER
%ifdef COMPAT_32
%token L_LAMBDA
%endif
/*
* 'Dangling else' shift/reduce conflict is well known...
* define these precedences to shut yacc up.
*/
%nonassoc LOWER_THAN_ELSE
%nonassoc L_ELSE
/*
* Operator precedence and associativity...
* greatly simplify the grammar.
*/
%right L_ASSIGN
%right '?'
%left L_LOR
%left L_LAND
%left '|'
%left '^'
%left '&'
%left L_EQ L_NE
%left L_ORDER '<'
%left L_LSH L_RSH
%left '+' '-'
%left '*' '%' '/'
%right L_NOT '~'
%nonassoc L_INC L_DEC
/*
* YYTYPE
*
* Anything with size > 4 is commented. Sizes assume typical 32 bit
* architecture. This size of the largest element of this union should
* be kept as small as possible to optimize copying of compiler stack
* elements.
*/
%union
{
POINTER_INT pointer_int;
int number;
float real;
char *string;
int type;
struct { short num_arg; char flags; } argument;
ident_hash_elem_t *ihe;
parse_node_t *node;
function_context_t *contextp;
struct {
parse_node_t *node;
char num;
} decl; /* 5 */
struct {
char num_local;
char max_num_locals;
short context;
short save_current_type;
short save_exact_types;
} func_block; /* 8 */
}
/*
* Type declarations.
*/
/* These hold opcodes */
%type <number> efun_override L_ASSIGN L_ORDER
/* Holds a variable index */
%type <number> L_PARAMETER single_new_local_def
/* These hold arbitrary numbers */
%type <number> L_NUMBER
/* These hold numbers that are going to be stuffed into pointers :)
* Don't ask :)
*/
%type <pointer_int> constant
/* These hold a real number */
%type <real> L_REAL
/* holds a string constant */
%type <string> L_STRING string_con1 string_con2
/* Holds the number of elements in a list and whether it must be a prototype */
%type <argument> argument_list argument
/* These hold a list of possible interpretations of an identifier */
%type <ihe> L_DEFINED_NAME
/* These hold a type */
%type <type> type optional_star type_modifier_list
%type <type> opt_basic_type L_TYPE_MODIFIER L_BASIC_TYPE basic_type atomic_type
%type <type> cast arg_type
%ifdef ARRAY_RESERVED_WORD
%type <type> opt_atomic_type
%endif
/* This holds compressed and less flexible def_name information */
%type <number> L_NEW_FUNCTION_OPEN l_new_function_open
%ifdef COMPAT_32
%type <number> simple_function_pointer
%endif
/* holds an identifier or some sort */
%type <string> L_IDENTIFIER L_EFUN function_name identifier
%type <string> new_local_name
/* The following return a parse node */
%type <node> number real string expr0 comma_expr for_expr sscanf catch
%type <node> parse_command time_expression expr_list expr_list2 expr_list3
%type <node> expr_list4 assoc_pair expr4 lvalue function_call lvalue_list
%type <node> new_local_def statement while cond do switch case
%type <node> return optional_else_part block_or_semi
%type <node> case_label statements switch_block
%type <node> expr_list_node expr_or_block
%type <node> single_new_local_def_with_init
%type <node> class_init opt_class_init all def
%type <node> program modifier_change inheritance type_decl
%ifdef DEBUG
%type <node> tree
%endif
/* The following hold information about blocks and local vars */
%type <decl> local_declarations local_name_list block decl_block
%type <decl> foreach_var foreach_vars first_for_expr foreach for
/* This holds a flag */
%type <number> new_arg
%%
%pragma auto_note_compiler_case_start
all:
program
{
comp_trees[TREE_MAIN] = $$;
}
;
program:
program def possible_semi_colon
{
CREATE_TWO_VALUES($$, 0, $1, $2);
}
| /* empty */
{
$$ = 0;
}
;
possible_semi_colon:
/* empty */
| ';'
{
yywarn("Extra ';'. Ignored.");
}
;
inheritance:
type_modifier_list L_INHERIT string_con1 ';'
{
object_t *ob;
inherit_t inherit;
int initializer;
%ifdef SENSIBLE_MODIFIERS
int acc_mod;
%endif
$1 |= global_modifiers;
%ifdef SENSIBLE_MODIFIERS
acc_mod = ($1 & DECL_ACCESS) & ~global_modifiers;
if (acc_mod & (acc_mod - 1)) {
char buf[256];
char *end = EndOf(buf);
char *p;
p = strput(buf, end, "Multiple access modifiers (");
p = get_type_modifiers(p, end, $1);
p = strput(p, end, ") for inheritance");
yyerror(buf);
}
%endif
if (!($1 & DECL_ACCESS)) $1 |= DECL_PUBLIC;
if (var_defined)
yyerror("Illegal to inherit after defining global variables.");
ob = find_object2($3);
if (ob == 0) {
inherit_file = alloc_cstring($3, "inherit");
/* Return back to load_object() */
YYACCEPT;
}
scratch_free($3);
inherit.prog = ob->prog;
if (mem_block[A_INHERITS].current_size){
inherit_t *prev_inherit = INHERIT(NUM_INHERITS - 1);
inherit.function_index_offset
= prev_inherit->function_index_offset
+ prev_inherit->prog->num_functions_defined
+ prev_inherit->prog->last_inherited;
if (prev_inherit->prog->num_functions_defined &&
prev_inherit->prog->function_table[prev_inherit->prog->num_functions_defined - 1].name[0] == '#')
inherit.function_index_offset--;
} else inherit.function_index_offset = 0;
inherit.variable_index_offset =
mem_block[A_VAR_TEMP].current_size /
sizeof (variable_t);
inherit.type_mod = $1;
add_to_mem_block(A_INHERITS, (char *)&inherit, sizeof inherit);
/* The following has to come before copy_vars - Sym */
copy_structures(ob->prog);
copy_variables(ob->prog, $1);
initializer = copy_functions(ob->prog, $1);
if (initializer >= 0) {
parse_node_t *node, *newnode;
/* initializer is an index into the object we're
inheriting's function table; this finds the
appropriate entry in our table and generates
a call to it */
node = new_node_no_line();
node->kind = NODE_CALL_2;
node->r.expr = 0;
node->v.number = F_CALL_INHERITED;
node->l.number = initializer | ((NUM_INHERITS - 1) << 16);
node->type = TYPE_ANY;
/* The following illustrates a distinction between */
/* macros and funcs...newnode is needed here - Sym */
newnode = comp_trees[TREE_INIT];
CREATE_TWO_VALUES(comp_trees[TREE_INIT],0, newnode, node);
comp_trees[TREE_INIT] = pop_value(comp_trees[TREE_INIT]);
}
$$ = 0;
}
;
real:
L_REAL
{
CREATE_REAL($$, $1);
}
;
number:
L_NUMBER
{
CREATE_NUMBER($$, $1);
}
;
optional_star:
/* empty */
{
$$ = 0;
}
| '*'
{
$$ = TYPE_MOD_ARRAY;
}
;
block_or_semi:
block
{
$$ = $1.node;
if (!$$) {
CREATE_RETURN($$, 0);
}
}
| ';'
{
$$ = 0;
}
| error
{
$$ = 0;
}
;
identifier:
L_DEFINED_NAME
{
$$ = scratch_copy($1->name);
}
| L_IDENTIFIER
;
def:
type optional_star identifier
{
int flags;
%ifdef SENSIBLE_MODIFIERS
int acc_mod;
%endif
flags = ($1 >> 16);
flags |= global_modifiers;
%ifdef SENSIBLE_MODIFIERS
acc_mod = (flags & DECL_ACCESS) & ~global_modifiers;
if (acc_mod & (acc_mod - 1)) {
char buf[256];
char *end = EndOf(buf);
char *p;
p = strput(buf, end, "Multiple access modifiers (");
p = get_type_modifiers(p, end, flags);
p = strput(p, end, ") for function");
yyerror(buf);
}
%endif
if (!(flags & DECL_ACCESS)) flags |= DECL_PUBLIC;
%ifdef SENSIBLE_MODIFIERS
if (flags & DECL_NOSAVE) {
yywarn("Illegal to declare nosave function.");
flags &= ~DECL_NOSAVE;
}
%endif
$1 = (flags << 16) | ($1 & 0xffff);
/* Handle type checking here so we know whether to typecheck
'argument' */
if ($1 & 0xffff) {
%ifdef OLD_TYPE_BEHAVIOR
exact_types = 0;
%else
exact_types = ($1& 0xffff) | $2;
%endif
} else {
if (pragmas & PRAGMA_STRICT_TYPES) {
if (strcmp($3, "create") != 0)
yyerror("\"#pragma strict_types\" requires type of function");
else
exact_types = TYPE_VOID; /* default for create() */
} else
exact_types = 0;
}
}
'(' argument ')'
{
char *p = $3;
$3 = make_shared_string($3);
scratch_free(p);
/* If we had nested functions, we would need to check */
/* here if we have enough space for locals */
/*
* Define a prototype. If it is a real function, then the
* prototype will be replaced below.
*/
$<number>$ = FUNC_PROTOTYPE;
if ($6.flags & ARG_IS_VARARGS) {
$<number>$ |= (FUNC_TRUE_VARARGS | FUNC_VARARGS);
}
$<number>$ |= ($1 >> 16);
define_new_function($3, $6.num_arg, 0, $<number>$, ($1 & 0xffff)| $2);
/* This is safe since it is guaranteed to be in the
function table, so it can't be dangling */
free_string($3);
context = 0;
}
block_or_semi
{
/* Either a prototype or a block */
if ($9) {
int fun;
$<number>8 &= ~FUNC_PROTOTYPE;
if ($9->kind != NODE_RETURN &&
($9->kind != NODE_TWO_VALUES
|| $9->r.expr->kind != NODE_RETURN)) {
parse_node_t *replacement;
CREATE_STATEMENTS(replacement, $9, 0);
CREATE_RETURN(replacement->r.expr, 0);
$9 = replacement;
}
fun = define_new_function($3, $6.num_arg,
max_num_locals - $6.num_arg,
$<number>8, ($1 & 0xffff) | $2);
if (fun != -1) {
$$ = new_node_no_line();
$$->kind = NODE_FUNCTION;
$$->v.number = fun;
$$->l.number = max_num_locals;
$$->r.expr = $9;
} else
$$ = 0;
} else
$$ = 0;
free_all_local_names(!!$9);
}
| type name_list ';'
{
if (!($1 & ~(DECL_MODS)) && (pragmas & PRAGMA_STRICT_TYPES))
yyerror("Missing type for global variable declaration");
$$ = 0;
}
| inheritance
| type_decl
| modifier_change
;
modifier_change: type_modifier_list ':'
{
if (!$1)
yyerror("modifier list may not be empty.");
if ($1 & FUNC_VARARGS) {
yyerror("Illegal modifier 'varargs' in global modifier list.");
$1 &= ~FUNC_VARARGS;
}
if (!($1 & DECL_ACCESS)) $1 |= DECL_PUBLIC;
global_modifiers = $1;
$$ = 0;
}
;
member_name:
optional_star identifier
{
/* At this point, the current_type here is only a basic_type */
/* and cannot be unused yet - Sym */
if (current_type == TYPE_VOID)
yyerror("Illegal to declare class member of type void.");
add_local_name($2, current_type | $1);
scratch_free($2);
}
;
member_name_list:
member_name
| member_name ',' member_name_list
;
member_list:
/* empty */
| member_list basic_type
{
current_type = $2;
}
member_name_list ';'
;
type_decl:
type_modifier_list L_CLASS identifier '{'
{
ident_hash_elem_t *ihe;
ihe = find_or_add_ident(
PROG_STRING($<number>$ = store_prog_string($3)),
FOA_GLOBAL_SCOPE);
if (ihe->dn.class_num == -1) {
ihe->sem_value++;
ihe->dn.class_num = mem_block[A_CLASS_DEF].current_size / sizeof(class_def_t);
if (ihe->dn.class_num > CLASS_NUM_MASK){
char buf[256];
char *p;
p = buf;
sprintf(p, "Too many classes, max is %d.\n", CLASS_NUM_MASK + 1);
yyerror(buf);
}
scratch_free($3);
$<ihe>2 = 0;
}
else {
$<ihe>2 = ihe;
}
}
member_list '}'
{
class_def_t *sd;
class_member_entry_t *sme;
int i, raise_error = 0;
/* check for a redefinition */
if ($<ihe>2 != 0) {
sd = CLASS($<ihe>2->dn.class_num);
if (sd->size != current_number_of_locals)
raise_error = 1;
else {
i = sd->size;
sme = (class_member_entry_t *)mem_block[A_CLASS_MEMBER].block + sd->index;
while (i--) {
/* check for matching names and types */
if (strcmp(PROG_STRING(sme[i].name), locals_ptr[i].ihe->name) != 0 ||
sme[i].type != (type_of_locals_ptr[i] & ~LOCAL_MODS)) {
raise_error = 1;
break;
}
}
}
}
if (raise_error) {
char buf[256];
char *end = EndOf(buf);
char *p;
p = strput(buf, end, "Illegal to redefine class ");
p = strput(p, end, PROG_STRING($<number>$));
yyerror(buf);
} else {
sd = (class_def_t *)allocate_in_mem_block(A_CLASS_DEF, sizeof(class_def_t));
i = sd->size = current_number_of_locals;
sd->index = mem_block[A_CLASS_MEMBER].current_size / sizeof(class_member_entry_t);
sd->name = $<number>5;
sme = (class_member_entry_t *)allocate_in_mem_block(A_CLASS_MEMBER, sizeof(class_member_entry_t) * current_number_of_locals);
while (i--) {
sme[i].name = store_prog_string(locals_ptr[i].ihe->name);
sme[i].type = type_of_locals_ptr[i] & ~LOCAL_MODS;
}
}
free_all_local_names(0);
$$ = 0;
}
;
new_local_name:
L_IDENTIFIER
| L_DEFINED_NAME
{
if ($1->dn.local_num != -1) {
char buff[256];
char *end = EndOf(buff);
char *p;
p = strput(buff, end, "Illegal to redeclare local name '");
p = strput(p, end, $1->name);
p = strput(p, end, "'");
yyerror(buff);
}
$$ = scratch_copy($1->name);
}
;
atomic_type:
L_BASIC_TYPE
| L_CLASS L_DEFINED_NAME
{
if ($2->dn.class_num == -1) {
char buf[256];
char *end = EndOf(buf);
char *p;
p = strput(buf, end, "Undefined class '");
p = strput(p, end, $2->name);
p = strput(p, end, "'");
yyerror(buf);
$$ = TYPE_ANY;
} else {
$$ = $2->dn.class_num | TYPE_MOD_CLASS;
}
}
| L_CLASS L_IDENTIFIER
{
char buf[256];
char *end = EndOf(buf);
char *p;
p = strput(buf, end, "Undefined class '");
p = strput(p, end, $2);
p = strput(p, end, "'");
yyerror(buf);
$$ = TYPE_ANY;
}
;
%ifdef ARRAY_RESERVED_WORD
opt_atomic_type:
atomic_type
| /* empty */
{
$$ = TYPE_ANY;
}
;
%endif
basic_type:
atomic_type
%ifdef ARRAY_RESERVED_WORD
| opt_atomic_type L_ARRAY
{
$$ = $1 | TYPE_MOD_ARRAY;
}
%endif
;
arg_type:
basic_type
%ifdef REF_RESERVED_WORD
| basic_type ref
{
$$ = $1 | LOCAL_MOD_REF;
}
%endif
;
new_arg:
arg_type optional_star
{
$$ = $1 | $2;
if ($1 != TYPE_VOID)
add_local_name("", $1 | $2);
}
| arg_type optional_star new_local_name
{
if ($1 == TYPE_VOID)
yyerror("Illegal to declare argument of type void.");
add_local_name($3, $1 | $2);
scratch_free($3);
$$ = $1 | $2;
}
| new_local_name
{
if (exact_types) {
yyerror("Missing type for argument");
}
add_local_name($1, TYPE_ANY);
scratch_free($1);
$$ = TYPE_ANY;
}
;
argument:
/* empty */
{
$$.num_arg = 0;
$$.flags = 0;
}
| argument_list
| argument_list L_DOT_DOT_DOT
{
int x = type_of_locals_ptr[max_num_locals-1];
int lt = x & ~LOCAL_MODS;
$$ = $1;
$$.flags |= ARG_IS_VARARGS;
if (x & LOCAL_MOD_REF) {
yyerror("Variable to hold remainder of args may not be a reference");
x &= ~LOCAL_MOD_REF;
}
if (lt != TYPE_ANY && !(lt & TYPE_MOD_ARRAY))
yywarn("Variable to hold remainder of arguments should be an array.");
}
;
argument_list:
new_arg
{
if (($1 & TYPE_MASK) == TYPE_VOID && !($1 & TYPE_MOD_CLASS)) {
if ($1 & ~TYPE_MASK)
yyerror("Illegal to declare argument of type void.");
$$.num_arg = 0;
} else {
$$.num_arg = 1;
}
$$.flags = 0;
}
| argument_list ',' new_arg
{
if (!$$.num_arg) /* first arg was void w/no name */
yyerror("argument of type void must be the only argument.");
if (($3 & TYPE_MASK) == TYPE_VOID && !($3 & TYPE_MOD_CLASS))
yyerror("Illegal to declare argument of type void.");
$$ = $1;
$$.num_arg++;
}
;
type_modifier_list:
/* empty */
{
$$ = 0;
}
| L_TYPE_MODIFIER type_modifier_list
{
%ifdef SENSIBLE_MODIFIERS
int acc_mod;
%endif
$$ = $1 | $2;
%ifdef SENSIBLE_MODIFIERS
acc_mod = ($$ & DECL_ACCESS) & ~global_modifiers;
if (acc_mod & (acc_mod - 1)) {
char buf[256];
char *end = EndOf(buf);
char *p;
p = strput(buf, end, "Multiple access modifiers (");
p = get_type_modifiers(p, end, $$);
p = strput(p, end, ") ");
yyerror(buf);
$$ = DECL_PUBLIC;
}
%endif
}
;
type:
type_modifier_list opt_basic_type
{
$$ = ($1 << 16) | $2;
current_type = $$;
}
;
cast:
'(' basic_type optional_star ')'
{
$$ = $2 | $3;
}
;
opt_basic_type:
basic_type
| /* empty */
{
$$ = TYPE_UNKNOWN;
}
;
name_list:
new_name
| new_name ',' name_list
;
new_name:
optional_star identifier
{
if (current_type & (FUNC_VARARGS << 16)){
yyerror("Illegal to declare varargs variable.");
current_type &= ~(FUNC_VARARGS << 16);
}
/* Now it is ok to merge the two
* remember that class_num and varargs was the reason for above
* Do the merging once only per row of decls
*/
if (current_type & 0xffff0000){
current_type = (current_type >> 16) | (current_type & 0xffff);
}
current_type |= global_modifiers;
if (!(current_type & DECL_ACCESS)) current_type |= DECL_PUBLIC;
if ((current_type & ~DECL_MODS) == TYPE_VOID)
yyerror("Illegal to declare global variable of type void.");
define_new_variable($2, current_type | $1);
scratch_free($2);
}
| optional_star identifier L_ASSIGN expr0
{
parse_node_t *expr, *newnode;
int type;
if (current_type & (FUNC_VARARGS << 16)){
yyerror("Illegal to declare varargs variable.");
current_type &= ~(FUNC_VARARGS << 16);
}
if (current_type & 0xffff0000){
current_type = (current_type >> 16) | (current_type & 0xffff);
}
current_type |= global_modifiers;
if (!(current_type & DECL_ACCESS)) current_type |= DECL_PUBLIC;
if ((current_type & ~DECL_MODS) == TYPE_VOID)
yyerror("Illegal to declare global variable of type void.");
if ($3 != F_ASSIGN)
yyerror("Only '=' is legal in initializers.");
/* ignore current_type == 0, which gets a missing type error
later anyway */
if (current_type) {
type = (current_type | $1) & ~DECL_MODS;
if ((current_type & ~DECL_MODS) == TYPE_VOID)
yyerror("Illegal to declare global variable of type void.");
if (!compatible_types(type, $4->type)) {
char buff[256];
char *end = EndOf(buff);
char *p;
p = strput(buff, end, "Type mismatch ");
p = get_two_types(p, end, type, $4->type);
p = strput(p, end, " when initializing ");
p = strput(p, end, $2);
yyerror(buff);
}
}
$4 = do_promotions($4, type);
CREATE_BINARY_OP(expr, F_VOID_ASSIGN, 0, $4, 0);
CREATE_OPCODE_1(expr->r.expr, F_GLOBAL_LVALUE, 0,
define_new_variable($2, current_type | $1));
newnode = comp_trees[TREE_INIT];
CREATE_TWO_VALUES(comp_trees[TREE_INIT], 0,
newnode, expr);
scratch_free($2);
}
;
block:
'{' local_declarations statements '}'
{
if ($2.node && $3) {
CREATE_STATEMENTS($$.node, $2.node, $3);
} else $$.node = ($2.node ? $2.node : $3);
$$.num = $2.num;
}
;
decl_block: block | for | foreach ;
local_declarations:
/* empty */
{
$$.node = 0;
$$.num = 0;
}
| local_declarations basic_type
{
if ($2 == TYPE_VOID)
yyerror("Illegal to declare local variable of type void.");
/* can't do this in basic_type b/c local_name_list contains
* expr0 which contains cast which contains basic_type
*/
current_type = $2;
}
local_name_list ';'
{
if ($1.node && $4.node) {
CREATE_STATEMENTS($$.node, $1.node, $4.node);
} else $$.node = ($1.node ? $1.node : $4.node);
$$.num = $1.num + $4.num;
}
;
new_local_def:
optional_star new_local_name
{
if (current_type & LOCAL_MOD_REF) {
yyerror("Illegal to declare local variable as reference");
current_type &= ~LOCAL_MOD_REF;
}
add_local_name($2, current_type | $1 | LOCAL_MOD_UNUSED);
scratch_free($2);
$$ = 0;
}
| optional_star new_local_name L_ASSIGN expr0
{
int type = (current_type | $1) & ~DECL_MODS;
if (current_type & LOCAL_MOD_REF) {
yyerror("Illegal to declare local variable as reference");
current_type &= ~LOCAL_MOD_REF;
type &= ~LOCAL_MOD_REF;
}
if ($3 != F_ASSIGN)
yyerror("Only '=' is allowed in initializers.");
if (!compatible_types($4->type, type)) {
char buff[256];
char *end = EndOf(buff);
char *p;
p = strput(buff, end, "Type mismatch ");
p = get_two_types(p, end, type, $4->type);
p = strput(p, end, " when initializing ");
p = strput(p, end, $2);
yyerror(buff);
}
$4 = do_promotions($4, type);
CREATE_UNARY_OP_1($$, F_VOID_ASSIGN_LOCAL, 0, $4,