-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgrammar.y
More file actions
3197 lines (2932 loc) · 109 KB
/
Copy pathgrammar.y
File metadata and controls
3197 lines (2932 loc) · 109 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
%{
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#include "src/std.h"
#include "src/simul_efun.h"
#include "rc/rc.h"
#include "misc/scratchpad.h"
#include "lpc/object.h"
#include "lpc/otable.h"
#include "lpc/program/parse_trees.h"
#include "lpc/program/generate.h"
#include "lpc/include/function.h"
#include "lpc/include/runtime_config.h"
#define YYMALLOC(sz) DXALLOC(sz, TAG_GRAMMAR, "grammar.y")
#define YYFREE(p) FREE(p)
/*
* 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
*/
int64_t context;
/*
* bison & yacc don't prototype this in y.tab.h
*/
int yyparse(void);
#ifdef _MSC_VER
#pragma warning(disable: 4244 4702)
#endif
%}
/*
* Token definitions.
*
* Appearing in the precedence declarations are:
* '+' '-' '/' '*' '%'
* '&' '|' '<' '>' '^'
* '~' '?'
*
* Other single character tokens recognized in this grammar:
* '{' '}' ',' ';' ':'
* '(' ')' '[' ']' '$'
*/
%token <number> L_NUMBER
%token <real> L_REAL
%token <string_span> L_STRING
%token <type> L_BASIC_TYPE L_TYPE_MODIFIER
%token <ihe> L_DEFINED_NAME
%token <string> L_IDENTIFIER L_EFUN
%token L_INC L_DEC
%token <number> L_ASSIGN
%token L_LAND L_LOR
%token L_LSH L_RSH
%token <number> L_ORDER
%token L_NOT
%token L_IF L_ELSE
%token L_SWITCH L_CASE L_DEFAULT L_RANGE L_DOT 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
%token <number> L_NEW_FUNCTION_OPEN
%token L_SSCANF L_CATCH
%token L_PARSE_COMMAND L_TIME_EXPRESSION
%token L_CLASS L_NEW
%token <number> L_PARAMETER
/*
* '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
*
* This size of the largest element of this union should be kept as small
* as possible to optimize copying of compiler stack elements.
*/
%union
{
intptr_t pointer_int;
int64_t number;
double real;
char *string;
struct {
char *str;
unsigned int len;
} string_span;
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
/* Holds a variable index */
%type <number> single_new_local_def
/* These hold numbers that are going to be stuffed into pointers :)
* Don't ask :)
*/
%type <pointer_int> constant
/* holds a string constant */
%type <string_span> 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 type */
%type <type> type optional_star type_modifier_list
%type <type> opt_basic_type basic_type atomic_type
%type <type> cast
/* holds an identifier or some sort */
%type <string> 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 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
/* The following hold information about blocks and local vars */
%type <decl> local_declarations local_name_list block decl_block
%type <decl> block_items block_item local_declaration_stmt
%type <decl> foreach_var foreach_vars first_for_expr foreach for
/* This holds a flag */
%type <number> new_arg
%%
all
: program
;
program
: program def possible_semi_colon
| /* empty */
;
possible_semi_colon
: /* empty */
| ';'
{
yywarn("Extra ';' ignored.");
}
;
inheritance
: type_modifier_list L_INHERIT string_con1 ';'
{
object_t *ob;
inherit_t inherit;
int initializer;
$1 |= global_modifiers;
if (var_defined)
yyerror("Invalid inherit clause after variables declarations.");
if (memchr($3.str, '\0', $3.len) != NULL) {
yyerror("Inherited file path must not contain embedded null bytes.");
scratch_free($3.str);
break;
}
ob = find_object_by_name($3.str);
if (ob == 0) {
inherit_file = alloc_cstring($3.str, "inherit");
/* Return back to load_object() */
YYACCEPT;
}
scratch_free($3.str);
inherit.prog = ob->prog;
inherit.function_index_offset = (function_index_t)(mem_block[A_RUNTIME_FUNCTIONS].current_size / sizeof (runtime_function_u));
inherit.variable_index_offset = (unsigned short)(mem_block[A_VAR_TEMP].current_size / sizeof (variable_t));
inherit.type_mod = (unsigned short)$1;
add_to_mem_block(A_INHERITS, (char *)&inherit, sizeof inherit);
copy_variables(ob->prog, $1);
copy_structures(ob->prog);
initializer = copy_functions(ob->prog, $1);
if (initializer >= 0)
{
/* 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
*/
int inherit_index = (int)(mem_block[A_INHERITS].current_size/sizeof(inherit_t) - 1);
switch_to_block(A_INITIALIZER);
generate_inherited_init_call(inherit_index, initializer);
switch_to_block(A_PROGRAM);
}
}
;
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
{
$1 |= global_modifiers;
/* Handle type checking here so we know whether to typecheck
'argument' */
if ($1 & ~NAME_TYPE_MOD) {
exact_types = $1 | $2;
} 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, NULL);
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>$ = NAME_UNDEFINED | NAME_PROTOTYPE;
if ($6.flags & ARG_IS_VARARGS) {
$<number>$ |= NAME_TRUE_VARARGS;
$1 |= NAME_VARARGS;
}
define_new_function($3, $6.num_arg, 0, $<number>$, $1 | $2);
/* This is safe since it is guaranteed to be in the
function table, so it can't be dangling */
free_string(to_shared_str($3));
context = 0;
}
block_or_semi
{
/* Either a prototype or a block */
if ($9) {
int fun;
$<number>8 &= ~(NAME_UNDEFINED | NAME_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;
}
if ($6.flags & ARG_IS_PROTO) {
yyerror("Missing name for function argument");
}
fun = define_new_function($3, $6.num_arg,
max_num_locals - $6.num_arg,
$<number>8, $1 | $2);
if (fun != -1)
COMPILER_FUNC(fun)->address =
generate_function(COMPILER_FUNC(fun), $9, max_num_locals);
}
free_all_local_names();
}
| type name_list ';'
{
if (!$1) yyerror("Missing type for global variable declaration");
}
| inheritance
| type_decl
| modifier_change
;
modifier_change
: type_modifier_list ':' { global_modifiers = $1; }
;
member_name
: optional_star identifier
{
if ((current_type & ~NAME_TYPE_MOD) == 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 type 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_len($3, strlen($3))),
FOA_GLOBAL_SCOPE);
if (ihe->dn.class_num == -1)
ihe->sem_value++;
else {
/* Possibly, this should check if the definitions are
consistent */
char buf[256];
char *end = EndOf(buf);
char *p;
p = strput(buf, end, "Illegal to redefine class ");
p = strput(p, end, $3);
yyerror(buf);
}
ihe->dn.class_num = (short)(mem_block[A_CLASS_DEF].current_size / sizeof(class_def_t));
}
member_list '}'
{
class_def_t *sd;
class_member_entry_t *sme;
int i;
sd = (class_def_t *)allocate_in_mem_block(A_CLASS_DEF, sizeof(class_def_t));
i = sd->size = (unsigned short)current_number_of_locals;
sd->index = (unsigned short)(mem_block[A_CLASS_MEMBER].current_size / sizeof(class_member_entry_t));
sd->name = (unsigned short)$<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_len(locals_ptr[i]->name, strlen(locals_ptr[i]->name));
sme[i].type = type_of_locals_ptr[i];
}
free_all_local_names();
scratch_free($3);
}
;
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;
}
;
basic_type
: atomic_type
;
new_arg
: basic_type optional_star
{
if ($1 == TYPE_VOID)
yyerror("Illegal to declare argument of type void.");
$$ = ARG_IS_PROTO;
add_local_name("", $1 | $2);
}
| basic_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);
$$ = 0;
}
| new_local_name
{
if (exact_types) yyerror("Missing type for argument");
add_local_name($1, TYPE_ANY);
scratch_free($1);
$$ = 0;
}
;
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];
$$ = $1;
$$.flags |= ARG_IS_VARARGS;
if (x != TYPE_ANY && !(x & TYPE_MOD_ARRAY))
yywarn("Variable to hold remainder of arguments should be an array.");
}
;
argument_list
: new_arg
{
$$.num_arg = 1;
$$.flags = (char)$1;
}
| argument_list ',' new_arg
{
$$ = $1;
$$.num_arg++;
$$.flags |= $3;
}
;
type_modifier_list
: /* empty */ { $$ = 0; }
| L_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
: /* empty */ { $$ = TYPE_UNKNOWN; }
| basic_type
;
name_list
: new_name
| new_name ',' name_list
;
new_name:
optional_star identifier
{
if ((current_type & ~NAME_TYPE_MOD) == TYPE_VOID)
yyerror("Illegal to declare global variable of type void.");
define_new_variable($2, current_type | $1 | global_modifiers);
scratch_free($2);
}
| optional_star identifier L_ASSIGN expr0
{
parse_node_t *expr;
int type = 0;
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 | global_modifiers) & ~NAME_TYPE_MOD;
if ((current_type & ~NAME_TYPE_MOD) == 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);
}
}
switch_to_block(A_INITIALIZER);
$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 | global_modifiers));
generate(expr);
switch_to_block(A_PROGRAM);
scratch_free($2);
}
;
block:
'{' block_items '}'
{
$$ = $2;
}
;
block_items
: /* empty */
{
$$.node = 0;
$$.num = 0;
}
| block_items block_item
{
if ($1.node && $2.node) {
CREATE_STATEMENTS($$.node, $1.node, $2.node);
} else $$.node = ($1.node ? $1.node : $2.node);
$$.num = $1.num + $2.num;
}
;
block_item
: statement
{
$$.node = $1;
$$.num = 0;
}
| local_declaration_stmt
;
local_declaration_stmt
: basic_type
{
if ($1 == 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 = $1;
}
local_name_list ';'
{
$$.node = $3.node;
$$.num = $3.num;
}
;
decl_block
: block
| for
| foreach
;
local_declarations
: /* empty */
{
$$.node = 0;
$$.num = 0;
}
| local_declarations local_declaration_stmt
{
if ($1.node && $2.node) {
CREATE_STATEMENTS($$.node, $1.node, $2.node);
} else {
$$.node = ($1.node ? $1.node : $2.node);
}
$$.num = $1.num + $2.num;
}
;
new_local_def
: optional_star new_local_name
{
add_local_name($2, current_type | $1);
scratch_free($2);
$$ = 0;
}
| optional_star new_local_name L_ASSIGN expr0
{
int type = (current_type | $1) & ~NAME_TYPE_MOD;
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,
add_local_name($2, current_type | $1));
scratch_free($2);
}
;
single_new_local_def:
basic_type optional_star new_local_name
{
if ($1 == TYPE_VOID)
yyerror("Illegal to declare local variable of type void.");
$$ = add_local_name($3, $1 | $2);
scratch_free($3);
}
;
single_new_local_def_with_init:
single_new_local_def L_ASSIGN expr0
{
int type = type_of_locals_ptr[$1];
if ($2 != F_ASSIGN)
yyerror("Only '=' is allowed in initializers.");
if (!compatible_types($3->type, type)) {
char buff[256];
char *end = EndOf(buff);
char *p;
p = strput(buff, end, "Type mismatch ");
p = get_two_types(p, end, type, $3->type);
p = strput(p, end, " when initializing.");
yyerror(buff);
}
$3 = do_promotions($3, type);
/* this is an expression */
CREATE_BINARY_OP($$, F_ASSIGN, 0, $3, 0);
CREATE_OPCODE_1($$->r.expr, F_LOCAL_LVALUE, 0, $1);
}
;
local_name_list:
new_local_def
{
$$.node = $1;
$$.num = 1;
}
| new_local_def ',' local_name_list
{
if ($1 && $3.node) {
CREATE_STATEMENTS($$.node, $1, $3.node);
} else $$.node = ($1 ? $1 : $3.node);
$$.num = 1 + $3.num;
}
;
statement:
comma_expr ';' { $$ = insert_pop_value($1); }
| cond
| while
| do
| switch
| return
| decl_block
{
$$ = $1.node;
pop_n_locals($1.num);
}
| /* empty */ ';'
{
$$ = 0;
}
| L_BREAK ';'
{
if (context & SPECIAL_CONTEXT) {
yyerror("Cannot break out of catch { } or time_expression { }");
$$ = 0;
} else
if (context & SWITCH_CONTEXT) {
CREATE_CONTROL_JUMP($$, CJ_BREAK_SWITCH);
} else
if (context & LOOP_CONTEXT) {
CREATE_CONTROL_JUMP($$, CJ_BREAK);
if (context & LOOP_FOREACH) {
parse_node_t *replace;
CREATE_STATEMENTS(replace, 0, $$);
CREATE_OPCODE(replace->l.expr, F_EXIT_FOREACH, 0);
$$ = replace;
}
} else {
yyerror("break statement outside loop");
$$ = 0;
}
}
| L_CONTINUE ';'
{
if (context & SPECIAL_CONTEXT)
yyerror("Cannot continue out of catch { } or time_expression { }");
else
if (!(context & LOOP_CONTEXT))
yyerror("continue statement outside loop");
CREATE_CONTROL_JUMP($$, CJ_CONTINUE);
}
;
while:
L_WHILE '(' comma_expr ')'
{
$<number>1 = context;
context = LOOP_CONTEXT;
}
statement
{
CREATE_LOOP($$, 1, $6, 0, optimize_loop_test($3));
context = $<number>1;
}
;
do:
L_DO
{
$<number>1 = context;
context = LOOP_CONTEXT;
}
statement L_WHILE '(' comma_expr ')' ';'
{
CREATE_LOOP($$, 0, $3, 0, optimize_loop_test($6));
context = $<number>1;
}
;
for:
L_FOR '(' first_for_expr ';' for_expr ';' for_expr ')'
{
$<number>1 = context;
context = LOOP_CONTEXT;
}
statement
{
$$.num = $3.num; /* number of declarations (0/1) */
$3.node = insert_pop_value($3.node);
$7 = insert_pop_value($7);
if ($7 && IS_NODE($7, NODE_UNARY_OP, F_INC)
&& IS_NODE($7->r.expr, NODE_OPCODE_1, F_LOCAL_LVALUE)) {
int64_t lvar = $7->r.expr->l.number;
CREATE_OPCODE_1($7, F_LOOP_INCR, 0, lvar);
}
CREATE_STATEMENTS($$.node, $3.node, 0);
CREATE_LOOP($$.node->r.expr, 1, $10, $7, optimize_loop_test($5));
context = $<number>1;
}
;
foreach_var: L_DEFINED_NAME
{
if ($1->dn.local_num != -1) {
CREATE_OPCODE_1($$.node, F_LOCAL_LVALUE, 0, $1->dn.local_num);
} else
if ($1->dn.global_num != -1) {
CREATE_OPCODE_1($$.node, F_GLOBAL_LVALUE, 0, $1->dn.global_num);
} else {
char buf[256];
char *end = EndOf(buf);
char *p;
p = strput(buf, end, "'");
p = strput(p, end, $1->name);
p = strput(p, end, "' is not a local or a global variable.");
yyerror(buf);
CREATE_OPCODE_1($$.node, F_GLOBAL_LVALUE, 0, 0);
}
$$.num = 0;
}
| single_new_local_def
{
CREATE_OPCODE_1($$.node, F_LOCAL_LVALUE, 0, $1);
$$.num = 1;
}
| L_IDENTIFIER
{
char buf[256];
char *end = EndOf(buf);
char *p;
p = strput(buf, end, "'");
p = strput(p, end, $1);
p = strput(p, end, "' is not a local or a global variable.");
yyerror(buf);
CREATE_OPCODE_1($$.node, F_GLOBAL_LVALUE, 0, 0);
scratch_free($1);
$$.num = 0;
}
;
foreach_vars:
foreach_var
{
CREATE_FOREACH($$.node, $1.node, 0);
$$.num = $1.num;
}
| foreach_var ',' foreach_var
{
CREATE_FOREACH($$.node, $1.node, $3.node);
$$.num = $1.num + $3.num;
}
;
foreach:
L_FOREACH '(' foreach_vars L_IN expr0 ')'
{
$3.node->v.expr = $5;
$<number>1 = context;
context = LOOP_CONTEXT | LOOP_FOREACH;
}
statement
{
$$.num = $3.num;
CREATE_STATEMENTS($$.node, $3.node, 0);
CREATE_LOOP($$.node->r.expr, 2, $8, 0, 0);
CREATE_OPCODE($$.node->r.expr->r.expr, F_NEXT_FOREACH, 0);
context = $<number>1;
}
;
for_expr:
/* EMPTY */
{
CREATE_NUMBER($$, 1);
}
| comma_expr
;
first_for_expr:
for_expr
{
$$.node = $1;
$$.num = 0;
}
| single_new_local_def_with_init
{
$$.node = $1;
$$.num = 1;
}
;
switch:
L_SWITCH '(' comma_expr ')'
{
$<number>1 = context;
context &= LOOP_CONTEXT;
context |= SWITCH_CONTEXT;
$<number>2 = mem_block[A_CASES].current_size;
}
'{' local_declarations case switch_block '}'
{
parse_node_t *node1, *node2;
if ($9) {
CREATE_STATEMENTS(node1, $8, $9);
} else node1 = $8;
if (context & SWITCH_STRINGS) {
NODE_NO_LINE(node2, NODE_SWITCH_STRINGS);
} else if (context & SWITCH_RANGES) {
NODE_NO_LINE(node2, NODE_SWITCH_RANGES);
} else {
NODE_NO_LINE(node2, NODE_SWITCH_NUMBERS);
}
node2->l.expr = $3;
node2->r.expr = node1;
prepare_cases(node2, (size_t)$<number>2);
context = $<number>1;
$$ = node2;
pop_n_locals($7.num);
}
;
switch_block: