forked from redox-os/gawk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
awkgram.y
6337 lines (5618 loc) · 149 KB
/
awkgram.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
/*
* awkgram.y --- yacc/bison parser
*/
/*
* Copyright (C) 1986, 1988, 1989, 1991-2018 the Free Software Foundation, Inc.
*
* This file is part of GAWK, the GNU implementation of the
* AWK Programming Language.
*
* GAWK is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GAWK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
%{
#ifdef GAWKDEBUG
#define YYDEBUG 12
#endif
#include "awk.h"
#if defined(__STDC__) && __STDC__ < 1 /* VMS weirdness, maybe elsewhere */
#define signed /**/
#endif
static void yyerror(const char *m, ...) ATTRIBUTE_PRINTF_1;
static void error_ln(int line, const char *m, ...) ATTRIBUTE_PRINTF_2;
static void lintwarn_ln(int line, const char *m, ...) ATTRIBUTE_PRINTF_2;
static void warning_ln(int line, const char *m, ...) ATTRIBUTE_PRINTF_2;
static char *get_src_buf(void);
static int yylex(void);
int yyparse(void);
static INSTRUCTION *snode(INSTRUCTION *subn, INSTRUCTION *op);
static char **check_params(char *fname, int pcount, INSTRUCTION *list);
static int install_function(char *fname, INSTRUCTION *fi, INSTRUCTION *plist);
static NODE *mk_rexp(INSTRUCTION *exp);
static void param_sanity(INSTRUCTION *arglist);
static int parms_shadow(INSTRUCTION *pc, bool *shadow);
#ifndef NO_LINT
static int isnoeffect(OPCODE type);
#endif
static INSTRUCTION *make_assignable(INSTRUCTION *ip);
static void dumpintlstr(const char *str, size_t len);
static void dumpintlstr2(const char *str1, size_t len1, const char *str2, size_t len2);
static int include_source(INSTRUCTION *file);
static int load_library(INSTRUCTION *file);
static void next_sourcefile(void);
static char *tokexpand(void);
static NODE *set_profile_text(NODE *n, const char *str, size_t len);
#define instruction(t) bcalloc(t, 1, 0)
static INSTRUCTION *mk_program(void);
static INSTRUCTION *append_rule(INSTRUCTION *pattern, INSTRUCTION *action);
static INSTRUCTION *mk_function(INSTRUCTION *fi, INSTRUCTION *def);
static INSTRUCTION *mk_condition(INSTRUCTION *cond, INSTRUCTION *ifp, INSTRUCTION *true_branch,
INSTRUCTION *elsep, INSTRUCTION *false_branch);
static INSTRUCTION *mk_expression_list(INSTRUCTION *list, INSTRUCTION *s1);
static INSTRUCTION *mk_for_loop(INSTRUCTION *forp, INSTRUCTION *init, INSTRUCTION *cond,
INSTRUCTION *incr, INSTRUCTION *body);
static void fix_break_continue(INSTRUCTION *list, INSTRUCTION *b_target, INSTRUCTION *c_target);
static INSTRUCTION *mk_binary(INSTRUCTION *s1, INSTRUCTION *s2, INSTRUCTION *op);
static INSTRUCTION *mk_boolean(INSTRUCTION *left, INSTRUCTION *right, INSTRUCTION *op);
static INSTRUCTION *mk_assignment(INSTRUCTION *lhs, INSTRUCTION *rhs, INSTRUCTION *op);
static INSTRUCTION *mk_getline(INSTRUCTION *op, INSTRUCTION *opt_var, INSTRUCTION *redir, int redirtype);
static int count_expressions(INSTRUCTION **list, bool isarg);
static INSTRUCTION *optimize_assignment(INSTRUCTION *exp);
static void add_lint(INSTRUCTION *list, LINTTYPE linttype);
enum defref { FUNC_DEFINE, FUNC_USE, FUNC_EXT };
static void func_use(const char *name, enum defref how);
static void check_funcs(void);
static ssize_t read_one_line(int fd, void *buffer, size_t count);
static int one_line_close(int fd);
static void split_comment(void);
static void check_comment(void);
static void add_sign_to_num(NODE *n, char sign);
static bool at_seen = false;
static bool want_source = false;
static bool want_regexp = false; /* lexical scanning kludge */
static enum {
FUNC_HEADER,
FUNC_BODY,
DONT_CHECK
} want_param_names = DONT_CHECK; /* ditto */
static char *in_function; /* parsing kludge */
static int rule = 0;
const char *const ruletab[] = {
"?",
"BEGIN",
"Rule",
"END",
"BEGINFILE",
"ENDFILE",
};
static bool in_print = false; /* lexical scanning kludge for print */
static int in_parens = 0; /* lexical scanning kludge for print */
static int sub_counter = 0; /* array dimension counter for use in delete */
static char *lexptr; /* pointer to next char during parsing */
static char *lexend; /* end of buffer */
static char *lexptr_begin; /* keep track of where we were for error msgs */
static char *lexeme; /* beginning of lexeme for debugging */
static bool lexeof; /* seen EOF for current source? */
static char *thisline = NULL;
static int in_braces = 0; /* count braces for firstline, lastline in an 'action' */
static int lastline = 0;
static int firstline = 0;
static SRCFILE *sourcefile = NULL; /* current program source */
static int lasttok = 0;
static bool eof_warned = false; /* GLOBAL: want warning for each file */
static int break_allowed; /* kludge for break */
static int continue_allowed; /* kludge for continue */
#define END_FILE -1000
#define END_SRC -2000
#define YYDEBUG_LEXER_TEXT (lexeme)
static char *tokstart = NULL;
static char *tok = NULL;
static char *tokend;
static int errcount = 0;
extern char *source;
extern int sourceline;
extern SRCFILE *srcfiles;
extern INSTRUCTION *rule_list;
extern int max_args;
extern NODE **args_array;
static INSTRUCTION *rule_block[sizeof(ruletab)];
static INSTRUCTION *ip_rec;
static INSTRUCTION *ip_newfile;
static INSTRUCTION *ip_atexit = NULL;
static INSTRUCTION *ip_end;
static INSTRUCTION *ip_endfile;
static INSTRUCTION *ip_beginfile;
INSTRUCTION *main_beginfile;
static INSTRUCTION *comment = NULL;
static INSTRUCTION *prior_comment = NULL;
static INSTRUCTION *comment_to_save = NULL;
static INSTRUCTION *program_comment = NULL;
static INSTRUCTION *function_comment = NULL;
static INSTRUCTION *block_comment = NULL;
static bool func_first = true;
static bool first_rule = true;
static inline INSTRUCTION *list_create(INSTRUCTION *x);
static inline INSTRUCTION *list_append(INSTRUCTION *l, INSTRUCTION *x);
static inline INSTRUCTION *list_prepend(INSTRUCTION *l, INSTRUCTION *x);
static inline INSTRUCTION *list_merge(INSTRUCTION *l1, INSTRUCTION *l2);
static inline INSTRUCTION *add_pending_comment(INSTRUCTION *stmt);
extern double fmod(double x, double y);
#define YYSTYPE INSTRUCTION *
%}
%token FUNC_CALL NAME REGEXP FILENAME
%token YNUMBER YSTRING TYPED_REGEXP
%token RELOP IO_OUT IO_IN
%token ASSIGNOP ASSIGN MATCHOP CONCAT_OP
%token SUBSCRIPT
%token LEX_BEGIN LEX_END LEX_IF LEX_ELSE LEX_RETURN LEX_DELETE
%token LEX_SWITCH LEX_CASE LEX_DEFAULT LEX_WHILE LEX_DO LEX_FOR LEX_BREAK LEX_CONTINUE
%token LEX_PRINT LEX_PRINTF LEX_NEXT LEX_EXIT LEX_FUNCTION
%token LEX_BEGINFILE LEX_ENDFILE
%token LEX_GETLINE LEX_NEXTFILE
%token LEX_IN
%token LEX_AND LEX_OR INCREMENT DECREMENT
%token LEX_BUILTIN LEX_LENGTH
%token LEX_EOF
%token LEX_INCLUDE LEX_EVAL LEX_LOAD
%token NEWLINE
/* Lowest to highest */
%right ASSIGNOP ASSIGN SLASH_BEFORE_EQUAL
%right '?' ':'
%left LEX_OR
%left LEX_AND
%left LEX_GETLINE
%nonassoc LEX_IN
%left FUNC_CALL LEX_BUILTIN LEX_LENGTH
%nonassoc ','
%left MATCHOP
%nonassoc RELOP '<' '>' IO_IN IO_OUT
%left CONCAT_OP
%left YSTRING YNUMBER TYPED_REGEXP
%left '+' '-'
%left '*' '/' '%'
%right '!' UNARY
%right '^'
%left INCREMENT DECREMENT
%left '$'
%left '(' ')'
%%
program
: /* empty */
| program rule
{
rule = 0;
yyerrok;
}
| program nls
| program LEX_EOF
{
next_sourcefile();
}
| program error
{
rule = 0;
/*
* If errors, give up, don't produce an infinite
* stream of syntax error messages.
*/
/* yyerrok; */
}
;
rule
: pattern action
{
(void) append_rule($1, $2);
first_rule = false;
}
| pattern statement_term
{
if (rule != Rule) {
msg(_("%s blocks must have an action part"), ruletab[rule]);
errcount++;
} else if ($1 == NULL) {
msg(_("each rule must have a pattern or an action part"));
errcount++;
} else /* pattern rule with non-empty pattern */
(void) append_rule($1, NULL);
}
| function_prologue action
{
in_function = NULL;
(void) mk_function($1, $2);
want_param_names = DONT_CHECK;
yyerrok;
}
| '@' LEX_INCLUDE source statement_term
{
want_source = false;
at_seen = false;
yyerrok;
}
| '@' LEX_LOAD library statement_term
{
want_source = false;
at_seen = false;
yyerrok;
}
;
source
: FILENAME
{
if (include_source($1) < 0)
YYABORT;
efree($1->lextok);
bcfree($1);
$$ = NULL;
}
| FILENAME error
{ $$ = NULL; }
| error
{ $$ = NULL; }
;
library
: FILENAME
{
if (load_library($1) < 0)
YYABORT;
efree($1->lextok);
bcfree($1);
$$ = NULL;
}
| FILENAME error
{ $$ = NULL; }
| error
{ $$ = NULL; }
;
pattern
: /* empty */
{
rule = Rule;
if (comment != NULL) {
$$ = list_create(comment);
comment = NULL;
} else
$$ = NULL;
}
| exp
{
rule = Rule;
if (comment != NULL) {
$$ = list_prepend($1, comment);
comment = NULL;
} else
$$ = $1;
}
| exp ',' opt_nls exp
{
INSTRUCTION *tp;
add_lint($1, LINT_assign_in_cond);
add_lint($4, LINT_assign_in_cond);
tp = instruction(Op_no_op);
list_prepend($1, bcalloc(Op_line_range, !!do_pretty_print + 1, 0));
$1->nexti->triggered = false;
$1->nexti->target_jmp = $4->nexti;
list_append($1, instruction(Op_cond_pair));
$1->lasti->line_range = $1->nexti;
$1->lasti->target_jmp = tp;
list_append($4, instruction(Op_cond_pair));
$4->lasti->line_range = $1->nexti;
$4->lasti->target_jmp = tp;
if (do_pretty_print) {
($1->nexti + 1)->condpair_left = $1->lasti;
($1->nexti + 1)->condpair_right = $4->lasti;
}
if (comment != NULL) {
$$ = list_append(list_merge(list_prepend($1, comment), $4), tp);
comment = NULL;
} else
$$ = list_append(list_merge($1, $4), tp);
rule = Rule;
}
| LEX_BEGIN
{
static int begin_seen = 0;
func_first = false;
if (do_lint_old && ++begin_seen == 2)
warning_ln($1->source_line,
_("old awk does not support multiple `BEGIN' or `END' rules"));
$1->in_rule = rule = BEGIN;
$1->source_file = source;
check_comment();
$$ = $1;
}
| LEX_END
{
static int end_seen = 0;
func_first = false;
if (do_lint_old && ++end_seen == 2)
warning_ln($1->source_line,
_("old awk does not support multiple `BEGIN' or `END' rules"));
$1->in_rule = rule = END;
$1->source_file = source;
check_comment();
$$ = $1;
}
| LEX_BEGINFILE
{
func_first = false;
$1->in_rule = rule = BEGINFILE;
$1->source_file = source;
check_comment();
$$ = $1;
}
| LEX_ENDFILE
{
func_first = false;
$1->in_rule = rule = ENDFILE;
$1->source_file = source;
check_comment();
$$ = $1;
}
;
action
: l_brace statements r_brace opt_semi opt_nls
{
INSTRUCTION *ip;
if ($2 == NULL)
ip = list_create(instruction(Op_no_op));
else
ip = $2;
$$ = ip;
}
;
func_name
: NAME
{ $$ = $1; }
| FUNC_CALL
{ $$ = $1; }
| lex_builtin
{
yyerror(_("`%s' is a built-in function, it cannot be redefined"),
tokstart);
YYABORT;
}
| '@' LEX_EVAL
{
$$ = $2;
at_seen = false;
}
;
lex_builtin
: LEX_BUILTIN
| LEX_LENGTH
;
function_prologue
: LEX_FUNCTION func_name '(' { want_param_names = FUNC_HEADER; } opt_param_list r_paren opt_nls
{
/*
* treat any comments between BOF and the first function
* definition (with no intervening BEGIN etc block) as
* program comments. Special kludge: iff there are more
* than one such comments, treat the last as a function
* comment.
*/
if (prior_comment != NULL) {
comment_to_save = prior_comment;
prior_comment = NULL;
} else if (comment != NULL) {
comment_to_save = comment;
comment = NULL;
} else
comment_to_save = NULL;
if (comment_to_save != NULL && func_first
&& strstr(comment_to_save->memory->stptr, "\n\n") != NULL)
split_comment();
/* save any other pre-function comment as function comment */
if (comment_to_save != NULL) {
function_comment = comment_to_save;
comment_to_save = NULL;
}
func_first = false;
$1->source_file = source;
if (install_function($2->lextok, $1, $5) < 0)
YYABORT;
in_function = $2->lextok;
$2->lextok = NULL;
bcfree($2);
/* $5 already free'd in install_function */
$$ = $1;
want_param_names = FUNC_BODY;
}
;
regexp
/*
* In this rule, want_regexp tells yylex that the next thing
* is a regexp so it should read up to the closing slash.
*/
: a_slash
{ want_regexp = true; }
REGEXP /* The terminating '/' is consumed by yylex(). */
{
NODE *n, *exp;
char *re;
size_t len;
re = $3->lextok;
$3->lextok = NULL;
len = strlen(re);
if (do_lint) {
if (len == 0)
lintwarn_ln($3->source_line,
_("regexp constant `//' looks like a C++ comment, but is not"));
else if (re[0] == '*' && re[len-1] == '*')
/* possible C comment */
lintwarn_ln($3->source_line,
_("regexp constant `/%s/' looks like a C comment, but is not"), re);
}
exp = make_str_node(re, len, ALREADY_MALLOCED);
n = make_regnode(Node_regex, exp);
if (n == NULL) {
unref(exp);
YYABORT;
}
$$ = $3;
$$->opcode = Op_match_rec;
$$->memory = n;
}
;
typed_regexp
: TYPED_REGEXP
{
char *re;
size_t len;
re = $1->lextok;
$1->lextok = NULL;
len = strlen(re);
$$ = $1;
$$->opcode = Op_push_re;
$$->memory = make_typed_regex(re, len);
}
a_slash
: '/'
{ bcfree($1); }
| SLASH_BEFORE_EQUAL
;
statements
: /* empty */
{
if (prior_comment != NULL) {
$$ = list_create(prior_comment);
prior_comment = NULL;
} else if (comment != NULL) {
$$ = list_create(comment);
comment = NULL;
} else
$$ = NULL;
}
| statements statement
{
if ($2 == NULL) {
if (prior_comment != NULL) {
$$ = list_append($1, prior_comment);
prior_comment = NULL;
if (comment != NULL) {
$$ = list_append($$, comment);
comment = NULL;
}
} else if (comment != NULL) {
$$ = list_append($1, comment);
comment = NULL;
} else
$$ = $1;
} else {
add_lint($2, LINT_no_effect);
if ($1 == NULL) {
if (prior_comment != NULL) {
$$ = list_append($2, prior_comment);
prior_comment = NULL;
if (comment != NULL) {
$$ = list_append($$, comment);
comment = NULL;
}
} else if (comment != NULL) {
$$ = list_append($2, comment);
comment = NULL;
} else
$$ = $2;
} else {
if (prior_comment != NULL) {
list_append($2, prior_comment);
prior_comment = NULL;
if (comment != NULL) {
list_append($2, comment);
comment = NULL;
}
} else if (comment != NULL) {
list_append($2, comment);
comment = NULL;
}
$$ = list_merge($1, $2);
}
}
yyerrok;
}
| statements error
{ $$ = NULL; }
;
statement_term
: nls
| semi opt_nls
;
statement
: semi opt_nls
{ $$ = NULL; }
| l_brace statements r_brace
{ $$ = $2; }
| if_statement
{
if (do_pretty_print)
$$ = list_prepend($1, instruction(Op_exec_count));
else
$$ = $1;
}
| LEX_SWITCH '(' exp r_paren opt_nls l_brace case_statements opt_nls r_brace
{
INSTRUCTION *dflt, *curr = NULL, *cexp, *cstmt;
INSTRUCTION *ip, *nextc, *tbreak;
const char **case_values = NULL;
int maxcount = 128;
int case_count = 0;
int i;
tbreak = instruction(Op_no_op);
cstmt = list_create(tbreak);
cexp = list_create(instruction(Op_pop));
dflt = instruction(Op_jmp);
dflt->target_jmp = tbreak; /* if no case match and no explicit default */
if ($7 != NULL) {
curr = $7->nexti;
bcfree($7); /* Op_list */
} /* else
curr = NULL; */
for (; curr != NULL; curr = nextc) {
INSTRUCTION *caseexp = curr->case_exp;
INSTRUCTION *casestmt = curr->case_stmt;
nextc = curr->nexti;
if (curr->opcode == Op_K_case) {
if (caseexp->opcode == Op_push_i) {
/* a constant scalar */
char *caseval;
caseval = force_string(caseexp->memory)->stptr;
for (i = 0; i < case_count; i++) {
if (strcmp(caseval, case_values[i]) == 0)
error_ln(curr->source_line,
_("duplicate case values in switch body: %s"), caseval);
}
if (case_values == NULL)
emalloc(case_values, const char **, sizeof(char *) * maxcount, "statement");
else if (case_count >= maxcount) {
maxcount += 128;
erealloc(case_values, const char **, sizeof(char*) * maxcount, "statement");
}
case_values[case_count++] = caseval;
} else {
/* match a constant regex against switch expression. */
(curr + 1)->match_exp = true;
}
curr->stmt_start = casestmt->nexti;
curr->stmt_end = casestmt->lasti;
(void) list_prepend(cexp, curr);
(void) list_prepend(cexp, caseexp);
} else {
if (dflt->target_jmp != tbreak)
error_ln(curr->source_line,
_("duplicate `default' detected in switch body"));
else
dflt->target_jmp = casestmt->nexti;
if (do_pretty_print) {
curr->stmt_start = casestmt->nexti;
curr->stmt_end = casestmt->lasti;
(void) list_prepend(cexp, curr);
} else
bcfree(curr);
}
cstmt = list_merge(casestmt, cstmt);
}
if (case_values != NULL)
efree(case_values);
ip = $3;
if (do_pretty_print) {
(void) list_prepend(ip, $1);
(void) list_prepend(ip, instruction(Op_exec_count));
$1->target_break = tbreak;
($1 + 1)->switch_start = cexp->nexti;
($1 + 1)->switch_end = cexp->lasti;
}/* else
$1 is NULL */
(void) list_append(cexp, dflt);
(void) list_merge(ip, cexp);
$$ = list_merge(ip, cstmt);
break_allowed--;
fix_break_continue(ip, tbreak, NULL);
}
| LEX_WHILE '(' exp r_paren opt_nls statement
{
/*
* -----------------
* tc:
* cond
* -----------------
* [Op_jmp_false tb ]
* -----------------
* body
* -----------------
* [Op_jmp tc ]
* tb:[Op_no_op ]
*/
INSTRUCTION *ip, *tbreak, *tcont;
tbreak = instruction(Op_no_op);
add_lint($3, LINT_assign_in_cond);
tcont = $3->nexti;
ip = list_append($3, instruction(Op_jmp_false));
ip->lasti->target_jmp = tbreak;
if (do_pretty_print) {
(void) list_append(ip, instruction(Op_exec_count));
$1->target_break = tbreak;
$1->target_continue = tcont;
($1 + 1)->while_body = ip->lasti;
(void) list_prepend(ip, $1);
}/* else
$1 is NULL */
if ($6 != NULL)
(void) list_merge(ip, $6);
(void) list_append(ip, instruction(Op_jmp));
ip->lasti->target_jmp = tcont;
$$ = list_append(ip, tbreak);
break_allowed--;
continue_allowed--;
fix_break_continue(ip, tbreak, tcont);
}
| LEX_DO opt_nls statement LEX_WHILE '(' exp r_paren opt_nls
{
/*
* -----------------
* z:
* body
* -----------------
* tc:
* cond
* -----------------
* [Op_jmp_true | z ]
* tb:[Op_no_op ]
*/
INSTRUCTION *ip, *tbreak, *tcont;
tbreak = instruction(Op_no_op);
tcont = $6->nexti;
add_lint($6, LINT_assign_in_cond);
if ($3 != NULL)
ip = list_merge($3, $6);
else
ip = list_prepend($6, instruction(Op_no_op));
if (do_pretty_print)
(void) list_prepend(ip, instruction(Op_exec_count));
(void) list_append(ip, instruction(Op_jmp_true));
ip->lasti->target_jmp = ip->nexti;
$$ = list_append(ip, tbreak);
break_allowed--;
continue_allowed--;
fix_break_continue(ip, tbreak, tcont);
if (do_pretty_print) {
$1->target_break = tbreak;
$1->target_continue = tcont;
($1 + 1)->doloop_cond = tcont;
$$ = list_prepend(ip, $1);
bcfree($4);
} /* else
$1 and $4 are NULLs */
}
| LEX_FOR '(' NAME LEX_IN simple_variable r_paren opt_nls statement
{
INSTRUCTION *ip;
char *var_name = $3->lextok;
if ($8 != NULL
&& $8->lasti->opcode == Op_K_delete
&& $8->lasti->expr_count == 1
&& $8->nexti->opcode == Op_push
&& ($8->nexti->memory->type != Node_var || !($8->nexti->memory->var_update))
&& strcmp($8->nexti->memory->vname, var_name) == 0
) {
/* Efficiency hack. Recognize the special case of
*
* for (iggy in foo)
* delete foo[iggy]
*
* and treat it as if it were
*
* delete foo
*
* Check that the body is a `delete a[i]' statement,
* and that both the loop var and array names match.
*/
NODE *arr = NULL;
ip = $8->nexti->nexti;
if ($5->nexti->opcode == Op_push && $5->lasti == $5->nexti)
arr = $5->nexti->memory;
if (arr != NULL
&& ip->opcode == Op_no_op
&& ip->nexti->opcode == Op_push_array
&& strcmp(ip->nexti->memory->vname, arr->vname) == 0
&& ip->nexti->nexti == $8->lasti
) {
(void) make_assignable($8->nexti);
$8->lasti->opcode = Op_K_delete_loop;
$8->lasti->expr_count = 0;
if ($1 != NULL)
bcfree($1);
efree(var_name);
bcfree($3);
bcfree($4);
bcfree($5);
$$ = $8;
} else
goto regular_loop;
} else {
INSTRUCTION *tbreak, *tcont;
/* [ Op_push_array a ]
* [ Op_arrayfor_init | ib ]
* ic:[ Op_arrayfor_incr | ib ]
* [ Op_var_assign if any ]
*
* body
*
* [Op_jmp | ic ]
* ib:[Op_arrayfor_final ]
*/
regular_loop:
ip = $5;
ip->nexti->opcode = Op_push_array;
tbreak = instruction(Op_arrayfor_final);
$4->opcode = Op_arrayfor_incr;
$4->array_var = variable($3->source_line, var_name, Node_var);
$4->target_jmp = tbreak;
tcont = $4;
$3->opcode = Op_arrayfor_init;
$3->target_jmp = tbreak;
(void) list_append(ip, $3);
if (do_pretty_print) {
$1->opcode = Op_K_arrayfor;
$1->target_continue = tcont;
$1->target_break = tbreak;
(void) list_append(ip, $1);
} /* else
$1 is NULL */
/* add update_FOO instruction if necessary */
if ($4->array_var->type == Node_var && $4->array_var->var_update) {
(void) list_append(ip, instruction(Op_var_update));
ip->lasti->update_var = $4->array_var->var_update;
}
(void) list_append(ip, $4);
/* add set_FOO instruction if necessary */
if ($4->array_var->type == Node_var && $4->array_var->var_assign) {
(void) list_append(ip, instruction(Op_var_assign));
ip->lasti->assign_var = $4->array_var->var_assign;
}
if (do_pretty_print) {
(void) list_append(ip, instruction(Op_exec_count));
($1 + 1)->forloop_cond = $4;
($1 + 1)->forloop_body = ip->lasti;
}
if ($8 != NULL)
(void) list_merge(ip, $8);
(void) list_append(ip, instruction(Op_jmp));
ip->lasti->target_jmp = $4;
$$ = list_append(ip, tbreak);
fix_break_continue(ip, tbreak, tcont);
}
break_allowed--;
continue_allowed--;
}
| LEX_FOR '(' opt_simple_stmt semi opt_nls exp semi opt_nls opt_simple_stmt r_paren opt_nls statement
{
$$ = mk_for_loop($1, $3, $6, $9, $12);
break_allowed--;
continue_allowed--;
}
| LEX_FOR '(' opt_simple_stmt semi opt_nls semi opt_nls opt_simple_stmt r_paren opt_nls statement
{
$$ = mk_for_loop($1, $3, (INSTRUCTION *) NULL, $8, $11);
break_allowed--;
continue_allowed--;
}
| non_compound_stmt
{
if (do_pretty_print)
$$ = list_prepend($1, instruction(Op_exec_count));
else
$$ = $1;
$$ = add_pending_comment($$);
}
;
non_compound_stmt
: LEX_BREAK statement_term
{
if (! break_allowed)
error_ln($1->source_line,
_("`break' is not allowed outside a loop or switch"));
$1->target_jmp = NULL;
$$ = list_create($1);
$$ = add_pending_comment($$);
}
| LEX_CONTINUE statement_term
{
if (! continue_allowed)
error_ln($1->source_line,
_("`continue' is not allowed outside a loop"));
$1->target_jmp = NULL;
$$ = list_create($1);
$$ = add_pending_comment($$);
}
| LEX_NEXT statement_term
{
/* if inside function (rule = 0), resolve context at run-time */
if (rule && rule != Rule)
error_ln($1->source_line,
_("`next' used in %s action"), ruletab[rule]);
$1->target_jmp = ip_rec;
$$ = list_create($1);
$$ = add_pending_comment($$);
}
| LEX_NEXTFILE statement_term
{
/* if inside function (rule = 0), resolve context at run-time */
if (rule == BEGIN || rule == END || rule == ENDFILE)
error_ln($1->source_line,
_("`nextfile' used in %s action"), ruletab[rule]);
$1->target_newfile = ip_newfile;
$1->target_endfile = ip_endfile;
$$ = list_create($1);
$$ = add_pending_comment($$);
}
| LEX_EXIT opt_exp statement_term
{
/* Initialize the two possible jump targets, the actual target
* is resolved at run-time.
*/
$1->target_end = ip_end; /* first instruction in end_block */
$1->target_atexit = ip_atexit; /* cleanup and go home */
if ($2 == NULL) {
$$ = list_create($1);
(void) list_prepend($$, instruction(Op_push_i));
$$->nexti->memory = dupnode(Nnull_string);
} else
$$ = list_append($2, $1);
$$ = add_pending_comment($$);
}
| LEX_RETURN
{
if (! in_function)
yyerror(_("`return' used outside function context"));
} opt_exp statement_term {
if ($3 == NULL) {
$$ = list_create($1);
(void) list_prepend($$, instruction(Op_push_i));
$$->nexti->memory = dupnode(Nnull_string);
} else
$$ = list_append($3, $1);
$$ = add_pending_comment($$);
}