-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.y
1392 lines (1327 loc) · 82.8 KB
/
parse.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
/********************************************
parse.y
copyright 1991-94, 2014 Michael D. Brennan
This is a source file for mawk, an implementation of
the AWK programming language.
Mawk is distributed without warranty under the terms of
the GNU General Public License, version 3, 2007.
If you import elements of this code into another product, you agree to
not name that product mawk.
********************************************/
/* make parser reentrant */
%define api.pure full
// %param {mawk_state_t * state}
// %param {environment_type *env}
// If ‘%define api.pure full’ is added:
// int yylex (YYSTYPE *lvalp, scanner_mode *mode, environment_type *env);
// int yyparse (parser_mode *mode, environment_type *env);
// %parse-param {int *nastiness} {int *randomness}
// %define api.push-pull push
%{
#include <stdio.h>
#include "mawk.h"
#include "table.h"
#include "code.h"
#include "types_string.h"
#include "bi_funct.h"
#include "bi_vars.h"
#include "field.h"
#include "files.h"
#include "scan.h"
#include "printf.h"
#define YYMAXDEPTH 200
extern void eat_nl(YYSTYPE *yylval) ;
static void resize_fblock(FBLOCK *) ;
static void code_array(SYMTAB *) ;
static void code_call_id(CA_REC *, SYMTAB *) ;
static void field_A2I(void) ;
static void check_var(SYMTAB *) ;
static void check_array(SYMTAB *) ;
static void RE_as_arg(void) ;
int REempty(void *) ;
static int scope ;
static FBLOCK * active_funct ;
/* when scope is SCOPE_FUNCT */
#define code_address(x) if ( is_local(x) ) \
code2op(L_PUSHA, (x)->offset) ;\
else \
code2(_PUSHA, (x)->stval.cp)
#define CDP(x) (code_base+(x))
/* WARNING: These CDP() calculations become invalid after calls
that might change code_base. Which are: code2(), code2op(),
code_jmp() and code_pop().
*/
/* this nonsense caters to MSDOS large model */
#define CODE_FE_PUSHA() code_ptr->ptr = (void *) 0 ; \
code1(FE_PUSHA)
/* moves the active_code from MAIN to a BEGIN or END */
#define CODE_OPEN_BEGIN _code_open_BEGIN(&scope)
#define CODE_OPEN_END _code_open_END(&scope)
#define CODE_CLOSE_ACTIVE _code_close_active(&scope)
#define CODE_CLOSE_BEGIN _code_close_BEGIN(&scope)
#define CODE_CLOSE_END _code_close_END(&scope)
#define CODE_CLOSE_ACTIVE _code_close_active(&scope)
#define CODE_NOT_SCOPE(SCOPE, MSG) if ( scope != SCOPE ) \
compile_error("MSG") ;
static void
_code_open_BEGIN( int * scope ) {
*scope = SCOPE_BEGIN ;
*main_code_p = active_code;
if ( !begin_code_p )
begin_code_p = code_create_block();
active_code = *begin_code_p;
}
static void
_code_open_END( int * scope ) {
*scope = SCOPE_END ;
*main_code_p = active_code;
if ( !end_code_p )
end_code_p = code_create_block();
active_code = *end_code_p;
}
static inline void
__code_close_active( void ) {
active_code = *main_code_p ;
active_funct = (FBLOCK*) 0 ;
}
static void
_code_close_BEGIN(
int * scope
// CODEBLOCK * active_code_p
) {
// *begin_code_p = *active_code_p ;
*begin_code_p = active_code ;
__code_close_active();
*scope = SCOPE_MAIN ;
}
static void
_code_close_END(
int * scope
// CODEBLOCK * active_code_p
) {
// *end_code_p = *active_code_p ;
*end_code_p = active_code ;
__code_close_active();
*scope = SCOPE_MAIN ;
}
static void
_code_close_FUNCT(
int * scope
) {
__code_close_active();
*scope = SCOPE_MAIN ;
}
/* reset the active_code back to the MAIN block */
static void
_code_close_active( int * scope ) // switch_code_to_main(void)
{
switch (*scope) {
case SCOPE_MAIN :
__code_close_active();
break ;
case SCOPE_BEGIN :
_code_close_BEGIN(scope);
break ;
case SCOPE_END :
_code_close_END(scope);
break ;
case SCOPE_FUNCT :
_code_close_FUNCT(scope);
break ;
}
// case SCOPE_FUNCT :
// active_code = *main_code_p ;
// break ;
// case SCOPE_MAIN :
// break ;
// }
// active_funct = (FBLOCK*) 0 ;
// scope = SCOPE_MAIN ;
}
%}
%union {
CELL * cp ;
SYMTAB * stp ;
int start ; /* code starting address as offset from code_base */
PF_CP fp ; /* ptr to a (print/printf) or (sub/gsub) function */
BI_REC * bip ; /* ptr to info about a builtin */
FBLOCK * fbp ; /* ptr to a function block */
ARG2_REC * arg2p ;
CA_REC * ca_p ;
int ival ;
void * ptr ;
}
/* two tokens to help with errors */
%token UNEXPECTED /* unexpected character */
%token BAD_DECIMAL
%token NL
%token SEMI_COLON
%token LBRACE RBRACE
%token LBOX RBOX
%token COMMA
%token <ival> IO_OUT /* > or output pipe */
%right ASSIGN ADD_ASG SUB_ASG MUL_ASG DIV_ASG MOD_ASG POW_ASG
%right QMARK COLON
%left OR
%left AND
%left IN
%left <ival> MATCH /* ~ or !~ */
%left EQ NEQ LT LTE GT GTE
%left CAT
%left GETLINE
%left PLUS MINUS
%left MUL DIV MOD
%left NOT UMINUS
%nonassoc IO_IN PIPE
%right POW
%left <ival> INC_or_DEC
%left DOLLAR FIELD /* last to remove a SR conflict with getline */
%right LPAREN RPAREN /* removes some SR conflicts */
%token <ptr> DOUBLE STRING_ RE
%token <stp> ID D_ID
%token <fbp> FUNCT_ID
%token <bip> BUILTIN LENGTH
%token <cp> FIELD
%token PRINT PRINTF SPLIT MATCH_FUNC SUB GSUB SPRINTF
/* keywords */
%token DO WHILE FOR BREAK CONTINUE IF ELSE IN
%token DELETE BEGIN END EXIT NEXT NEXTFILE RETURN FUNCTION
%type <start> block block_or_separator
%type <start> stmt_list statement mark pmark
%type <ival> pr_args printf_args
%type <arg2p> arg2
%type <start> builtin
%type <start> getline_file
%type <start> lvalue field fvalue
%type <start> expr cat_expr p_expr
%type <start> while_front if_front
%type <start> for1 for2
%type <start> array_loop_front
%type <start> return_stmt
%type <start> split_front re_arg sub_back
%type <ival> arglist args
%type <fp> sub_or_gsub
%type <fbp> funct_start funct_head
%type <ca_p> call_args ca_front ca_back
%type <ival> f_arglist f_args
%type <ptr> string_comma
%%
/* productions */
start : program
| { CODE_OPEN_BEGIN;
} // TODO: refactor midrule issues? https://www.gnu.org/software/bison/manual/bison.html#Midrule-Actions
stmt_list { CODE_CLOSE_BEGIN;
}
;
program : program_block
| program program_block
;
program_block : PA_block /* pattern-action */
| function_def
| outside_error block
;
PA_block : block { /* this do nothing removes a vacuous warning from Bison */
}
| BEGIN { CODE_OPEN_BEGIN;
}
block { CODE_CLOSE_BEGIN;
}
| END { CODE_OPEN_END;
}
block { CODE_CLOSE_END;
}
/* this works just like an if statement */
| expr { code_jmp(_JZ, (INST*)0);
}
block_or_separator { patch_jmp( code_ptr ) ;
}
/* range pattern, see comment in execute.c near _RANGE */
| expr COMMA { INST *p1 = CDP($1) ;
int len ;
code_push(p1, code_ptr - p1, scope, active_funct) ;
code_ptr = p1 ;
code2op(_RANGE, 1) ;
code_ptr += 3 ;
len = code_pop(code_ptr) ;
code_ptr += len ;
code1(_STOP) ;
p1 = CDP($1) ;
p1[2].op = code_ptr - (p1+1) ;
}
expr { code1(_STOP);
}
block_or_separator { INST *p1 = CDP($1) ;
p1[3].op = CDP($6) - (p1+1) ;
p1[4].op = code_ptr - (p1+1) ;
}
;
block : LBRACE stmt_list RBRACE { $$ = $2;
}
| LBRACE error RBRACE { $$ = code_offset ; /* does nothing won't be executed */
print_flag = getline_flag = paren_cnt = 0 ;
yyerrok ;
}
;
block_or_separator : block
| separator { /* default print action */
$$ = code_offset ;
code1(_PUSHINT) ; code1(0) ;
code2(_PRINT, bi_print) ;
}
stmt_list : statement
| stmt_list statement
;
statement : block
| expr separator { code1(_POP) ;
}
| /*empty*/ separator { $$ = code_offset ;
}
| error separator { $$ = code_offset ;
print_flag = getline_flag = 0 ;
paren_cnt = 0 ;
yyerrok ;
}
| BREAK separator { $$ = code_offset ;
BC_insert('B', code_ptr+1) ;
code2(_JMP, 0) ; /* don't use code_jmp ! */
}
| CONTINUE separator { $$ = code_offset ;
BC_insert('C', code_ptr+1) ;
code2(_JMP, 0) ;
}
| return_stmt { CODE_NOT_SCOPE( SCOPE_FUNCT, "return outside function body") ;
}
| NEXT separator { CODE_NOT_SCOPE( SCOPE_MAIN, "improper use of next") ;
$$ = code_offset ;
code1(_NEXT) ;
}
| NEXTFILE separator { CODE_NOT_SCOPE( SCOPE_MAIN, "improper use of nextfile" ) ;
$$ = code_offset ;
code1(_NEXTFILE) ;
}
;
separator : NL
| SEMI_COLON
;
expr : cat_expr
| lvalue ASSIGN expr { code1(_ASSIGN) ;
}
| lvalue ADD_ASG expr { code1(_ADD_ASG) ;
}
| lvalue SUB_ASG expr { code1(_SUB_ASG) ;
}
| lvalue MUL_ASG expr { code1(_MUL_ASG) ;
}
| lvalue DIV_ASG expr { code1(_DIV_ASG) ;
}
| lvalue MOD_ASG expr { code1(_MOD_ASG) ;
}
| lvalue POW_ASG expr { code1(_POW_ASG) ;
}
| expr EQ expr { code1(_EQ) ;
}
| expr NEQ expr { code1(_NEQ) ;
}
| expr LT expr { code1(_LT) ;
}
| expr LTE expr { code1(_LTE) ;
}
| expr GT expr { code1(_GT) ;
}
| expr GTE expr { code1(_GTE) ;
}
| expr MATCH expr { INST *p3 = CDP($3) ;
if ( p3 == code_ptr - 2 ) {
if ( p3->op == _MATCH0 )
p3->op = _MATCH1 ;
else /* check for string */
if ( p3->op == _PUSHS ) {
CELL *cp = ZMALLOC(CELL) ;
cp->type = C_STRING ;
cp->ptr = p3[1].ptr ;
cast_to_RE(cp) ;
code_ptr -= 2 ;
code2(_MATCH1, cp->ptr) ;
ZFREE(cp) ;
}
else
code1(_MATCH2) ;
}
else
code1(_MATCH2) ;
if ( !$2 )
code1(_NOT) ;
}
/* short circuit boolean evaluation */
| expr OR { code1(_TEST) ;
code_jmp(_LJNZ, (INST*)0) ;
}
expr { code1(_TEST) ;
patch_jmp(code_ptr) ;
}
| expr AND { code1(_TEST) ;
code_jmp(_LJZ, (INST*)0) ;
}
expr { code1(_TEST) ;
patch_jmp(code_ptr) ;
}
| expr QMARK { code_jmp(_JZ, (INST*)0) ;
}
expr COLON { code_jmp(_JMP, (INST*)0) ;
}
expr { patch_jmp(code_ptr) ;
patch_jmp(CDP($7)) ;
}
;
cat_expr : p_expr %prec CAT
| cat_expr p_expr %prec CAT { code1(_CAT) ;
}
;
p_expr : DOUBLE { $$ = code_offset ;
code2(_PUSHD, $1) ;
}
| STRING_ { $$ = code_offset ;
code2(_PUSHS, $1) ;
}
| ID %prec AND { /* anything less than IN */
check_var($1) ;
$$ = code_offset ;
if ( is_local($1) ) {
code2op(L_PUSHI, $1->offset) ;
}
else
code2(_PUSHI, $1->stval.cp) ;
}
| LPAREN expr RPAREN { $$ = $2 ;
}
;
p_expr : RE { $$ = code_offset ;
code2(_MATCH0, $1) ;
}
;
p_expr : p_expr PLUS p_expr { code1(_ADD) ;
}
| p_expr MINUS p_expr { code1(_SUB) ;
}
| p_expr MUL p_expr { code1(_MUL) ;
}
| p_expr DIV p_expr { code1(_DIV) ;
}
| p_expr MOD p_expr { code1(_MOD) ;
}
| p_expr POW p_expr { code1(_POW) ;
}
| NOT p_expr { $$ = $2 ;
code1(_NOT) ;
}
| PLUS p_expr %prec UMINUS { $$ = $2 ;
code1(_UPLUS) ;
}
| MINUS p_expr %prec UMINUS { $$ = $2 ;
code1(_UMINUS) ;
}
| builtin
;
p_expr : ID INC_or_DEC { check_var($1) ;
$$ = code_offset ;
code_address($1) ;
if ( $2 == '+' )
code1(_POST_INC) ;
else
code1(_POST_DEC) ;
}
| INC_or_DEC lvalue { $$ = $2 ;
if ( $1 == '+' )
code1(_PRE_INC) ;
else
code1(_PRE_DEC) ;
}
;
p_expr : field INC_or_DEC { if ($2 == '+' )
code1(F_POST_INC ) ;
else
code1(F_POST_DEC) ;
}
| INC_or_DEC field { $$ = $2 ;
if ( $1 == '+' )
code1(F_PRE_INC) ;
else
code1( F_PRE_DEC) ;
}
;
lvalue : ID { $$ = code_offset ;
check_var($1) ;
code_address($1) ;
}
;
arglist : /* empty */ { $$ = 0 ;
}
| args
;
args : expr %prec LPAREN { $$ = 1 ;
}
| args COMMA expr { $$ = $1 + 1 ;
}
;
builtin : BUILTIN mark LPAREN arglist RPAREN { BI_REC *p = $1 ;
$$ = $2 ;
if ( (int)p->min_args > $4 || (int)p->max_args < $4 )
compile_error(
"wrong number of arguments in call to %s" ,
p->name
) ;
/* variable args */
if ( p->min_args != p->max_args ) {
code1(_PUSHINT) ;
code1($4) ;
}
code2(_BUILTIN , p->fp) ;
}
;
/*
one shift/reduce conflict
SPRINTF mark LPAREN STRING_ . COMMA
reduce STRING_ --> expr or shift COMMA
shift wins and is what we want
*/
builtin : SPRINTF mark LPAREN RPAREN { $$ = $2 ;
compile_error("no argments in call to sprintf()") ;
}
| SPRINTF mark LPAREN string_comma args RPAREN
{ /* the usual case */
const Form* form = (Form*) $4 ;
$$ = $2 ;
if (form && form->num_args != $5) {
compile_error("wrong number of arguments to sprintf, needs %d, has %d",
form->num_args+1, $5+1) ;
}
code2op(_PUSHINT, $5 + 1) ;
code2(_BUILTIN, bi_sprintf) ;
}
| SPRINTF mark LPAREN args RPAREN { $$ = $2 ;
code2op(_PUSHINT, $4) ;
code2(_BUILTIN, bi_sprintf1) ;
}
;
string_comma : STRING_ COMMA { STRING* str = (STRING*) $1 ;
const Form* form = parse_form(str) ;
free_STRING(str) ;
$$ = (void *) form ;
code2(PUSHFM, form) ;
}
;
;
// empty production to store the code_ptr
mark : /* empty */ { $$ = code_offset ;
}
// print_statement
statement : PRINT pmark pr_args pr_direction separator
{ code2(_PRINT, bi_print) ;
print_flag = 0 ;
$$ = $2 ;
}
;
/* printf statment
- first case is same as print statment
- second case is first arg is const string no braces, at least one more arg
- third case is first arg is const string inside braces, at least one more arg
last two cases are the usual
*/
statement : PRINTF pmark pr_args pr_direction separator
{ code2(_PRINT, bi_printf1) ;
print_flag = 0 ;
$$ = $2 ;
if ($3 == 0) {
compile_error("no arguments in call to printf") ;
}
}
| PRINTF pmark string_comma printf_args pr_direction separator
{ const Form* form = (Form*) $3 ;
if (form && form->num_args != $4) {
compile_error(
"wrong number of arguments to printf, needs %d, has %d",
form->num_args+1, $4+1
) ;
}
code2(_PRINT, bi_printf) ;
print_flag = 0 ;
$$ = $2 ;
}
| PRINTF pmark LPAREN string_comma printf_args RPAREN pr_direction separator
{ const Form* form = (Form*) $4 ;
if (form && form->num_args != $5) {
compile_error(
"wrong number of arguments to printf, needs %d, has %d",
form->num_args+1, $5+1
) ;
}
code2(_PRINT, bi_printf) ;
print_flag = 0 ;
$$ = $2 ;
}
;
pmark : /* empty */ { $$ = code_offset ;
print_flag = 1 ;
}
;
printf_args : args { code2op(_PUSHINT, $1 + 1) ;
}
;
pr_args : arglist { code2op(_PUSHINT, $1) ;
}
| LPAREN arg2 RPAREN { $$ = $2->cnt ;
zfree($2,sizeof(ARG2_REC)) ;
code2op(_PUSHINT, $$) ;
}
| LPAREN RPAREN { $$=0 ;
code2op(_PUSHINT, 0) ;
}
;
arg2 : expr COMMA expr { $$ = (ARG2_REC*) zmalloc(sizeof(ARG2_REC)) ;
$$->start = $1 ;
$$->cnt = 2 ;
}
| arg2 COMMA expr { $$ = $1 ;
$$->cnt++ ;
}
;
pr_direction : /* empty */
| IO_OUT expr { code2op(_PUSHINT, $1) ;
}
;
/* IF and IF-ELSE */
if_front : IF LPAREN expr RPAREN { $$ = $3 ;
EAT_NL_ ;
code_jmp(_JZ, (INST*)0) ;
}
;
/* if_statement */
statement : if_front statement { patch_jmp( code_ptr ) ;
}
;
else : ELSE { EAT_NL_ ;
code_jmp(_JMP, (INST*)0) ;
}
;
/* if_else_statement */
statement : if_front statement else statement { patch_jmp(code_ptr) ;
patch_jmp(CDP($4)) ;
}
/* LOOPS */
do : DO { EAT_NL_ ;
BC_new() ;
}
;
/* do_statement */
statement : do statement WHILE LPAREN expr RPAREN separator
{ $$ = $2 ;
code_jmp(_JNZ, CDP($2)) ;
BC_clear(code_ptr, CDP($5)) ;
}
;
while_front : WHILE LPAREN expr RPAREN { EAT_NL_ ;
BC_new() ;
$$ = $3 ;
/* check if const expression */
if ( code_ptr - 2 == CDP($3) &&
code_ptr[-2].op == _PUSHD &&
*(double*)code_ptr[-1].ptr != 0.0
)
code_ptr -= 2 ;
else {
INST *p3 = CDP($3) ;
code_push(p3, code_ptr-p3, scope, active_funct) ;
code_ptr = p3 ;
code2(_JMP, (INST*)0) ; /* code2() not code_jmp() */
}
}
;
/* while_statement */
statement : while_front statement { int saved_offset ;
int len ;
INST *p1 = CDP($1) ;
INST *p2 = CDP($2) ;
if ( p1 != p2 ) { /* real test in loop */
p1[1].op = code_ptr-(p1+1) ;
saved_offset = code_offset ;
len = code_pop(code_ptr) ;
code_ptr += len ;
code_jmp(_JNZ, CDP($2)) ;
BC_clear(code_ptr, CDP(saved_offset)) ;
}
else { /* while(1) */
code_jmp(_JMP, p1) ;
BC_clear(code_ptr, CDP($2)) ;
}
}
;
/* for_statement */
statement : for1 for2 for3 statement { int cont_offset = code_offset ;
unsigned len = code_pop(code_ptr) ;
INST * p2 = CDP($2) ;
INST * p4 = CDP($4) ;
code_ptr += len ;
if ( p2 != p4 ) { /* real test in for2 */
p4[-1].op = code_ptr - p4 + 1 ;
len = code_pop(code_ptr) ;
code_ptr += len ;
code_jmp(_JNZ, CDP($4)) ;
}
else /* for(;;) */
code_jmp(_JMP, p4) ;
BC_clear(code_ptr, CDP(cont_offset)) ;
}
;
for1 : FOR LPAREN SEMI_COLON { $$ = code_offset ;
}
| FOR LPAREN expr SEMI_COLON { $$ = $3 ;
code1(_POP) ;
}
;
for2 : SEMI_COLON { $$ = code_offset ;
}
| expr SEMI_COLON { if ( code_ptr - 2 == CDP($1) &&
code_ptr[-2].op == _PUSHD &&
* (double*) code_ptr[-1].ptr != 0.0
) {
code_ptr -= 2 ;
}
else {
INST *p1 = CDP($1) ;
code_push(p1, code_ptr-p1, scope, active_funct) ;
code_ptr = p1 ;
code2(_JMP, (INST*)0) ;
}
}
;
for3 : RPAREN { EAT_NL_ ;
BC_new() ;
code_push((INST*)0,0, scope, active_funct) ;
}
| expr RPAREN { INST *p1 = CDP($1) ;
EAT_NL_ ; BC_new() ;
code1(_POP) ;
code_push(p1, code_ptr - p1, scope, active_funct) ;
code_ptr -= code_ptr - p1 ;
}
;
/* arrays */
expr : expr IN ID { check_array($3) ;
code_array($3) ;
code1(A_TEST) ;
}
| LPAREN arg2 RPAREN IN ID { $$ = $2->start ;
code2op(A_CAT, $2->cnt) ;
zfree($2, sizeof(ARG2_REC)) ;
check_array($5) ;
code_array($5) ;
code1(A_TEST) ;
}
;
lvalue : ID mark LBOX args RBOX { if ( $4 > 1 ) {
code2op(A_CAT, $4) ;
}
check_array($1) ;
if ( is_local($1) ) {
code2op(LAE_PUSHA, $1->offset) ;
}
else
code2(AE_PUSHA, $1->stval.array) ;
$$ = $2 ;
}
;
p_expr : ID mark LBOX args RBOX %prec AND { if ( $4 > 1 ) {
code2op(A_CAT, $4) ;
}
check_array($1) ;
if( is_local($1) ) {
code2op(LAE_PUSHI, $1->offset) ;
}
else
code2(AE_PUSHI, $1->stval.array) ;
$$ = $2 ;
}
| ID mark LBOX args RBOX INC_or_DEC { if ( $4 > 1 ) {
code2op(A_CAT,$4) ;
}
check_array($1) ;
if ( is_local($1) ) {
code2op(LAE_PUSHA, $1->offset) ;
}
else
code2(AE_PUSHA, $1->stval.array) ;
if ( $6 == '+' )
code1(_POST_INC) ;
else
code1(_POST_DEC) ;
$$ = $2 ;
}
;
/* delete A[i] or delete A */
statement : DELETE ID mark LBOX args RBOX separator
{ $$ = $3 ;
if ( $5 > 1 ) { code2op(A_CAT, $5) ; }
check_array($2) ;
code_array($2) ;
code1(A_DEL) ;
}
| DELETE ID separator { $$ = code_offset ;
check_array($2) ;
code_array($2) ;
code1(DEL_A) ;
}
;
/* for ( i in A ) statement */
array_loop_front : FOR LPAREN ID IN ID RPAREN { EAT_NL_ ;
BC_new() ;
$$ = code_offset ;
check_var($3) ;
code_address($3) ;
check_array($5) ;
code_array($5) ;
code2(SET_ALOOP, (INST*)0) ;
}
;
/* array_loop */
statement : array_loop_front statement { INST *p2 = CDP($2) ;
p2[-1].op = code_ptr - p2 + 1 ;
BC_clear( code_ptr+2 , code_ptr) ;
code_jmp(ALOOP, p2) ;
code1(POP_AL) ;
}
;
/* fields
D_ID is a special token , same as an ID, but yylex()
only returns it after a '$'. In essense,
DOLLAR D_ID is really one token.
*/
field : FIELD { $$ = code_offset ;
code2(F_PUSHA, $1) ;
}
| DOLLAR D_ID { check_var($2) ;
$$ = code_offset ;
if ( is_local($2) )
code2op(L_PUSHI, $2->offset) ;
else
code2(_PUSHI, $2->stval.cp) ;
CODE_FE_PUSHA() ;
}
| DOLLAR D_ID mark LBOX args RBOX { if ( $5 > 1 )
code2op(A_CAT, $5) ;
check_array($2) ;
if ( is_local($2) )
code2op(LAE_PUSHI, $2->offset) ;
else
code2(AE_PUSHI, $2->stval.array) ;
CODE_FE_PUSHA() ;
$$ = $3 ;
}
| DOLLAR p_expr { $$ = $2 ;
CODE_FE_PUSHA() ;
}
| LPAREN field RPAREN { $$ = $2 ;
}
;
/* removes field (++|--) sr conflict */
p_expr : field %prec CAT { field_A2I() ;
}
;
expr : field ASSIGN expr { code1(F_ASSIGN) ; }
| field ADD_ASG expr { code1(F_ADD_ASG) ; }
| field SUB_ASG expr { code1(F_SUB_ASG) ; }
| field MUL_ASG expr { code1(F_MUL_ASG) ; }
| field DIV_ASG expr { code1(F_DIV_ASG) ; }
| field MOD_ASG expr { code1(F_MOD_ASG) ; }
| field POW_ASG expr { code1(F_POW_ASG) ; }
;
/*
split is handled different than a builtin because
it takes an array and optionally a regular expression as args
*/
p_expr : split_front split_back { code2(_BUILTIN, bi_split) ;
}
;
split_front : SPLIT LPAREN expr COMMA ID { $$ = $3 ;
check_array($5) ;
code_array($5) ;
}
;
split_back : RPAREN { code2(_PUSHI, &fs_shadow) ;
}
| COMMA expr RPAREN { if ( CDP($2) == code_ptr - 2 ) {
if (code_ptr[-2].op == _MATCH0) {
RE_as_arg() ;
{
/* see if // needs conversion */
CELL* cp = (CELL*) code_ptr[-1].ptr ;
if (REempty(cp->ptr)) {
cp->type = C_SNULL ;
cp->ptr = 0 ;
}
}
}
else {
if ( code_ptr[-2].op == _PUSHS ) {
CELL *cp = ZMALLOC(CELL) ;
cp->type = C_STRING ;
cp->ptr = code_ptr[-1].ptr ;
cast_for_split(cp) ;
code_ptr[-2].op = _PUSHC ;
code_ptr[-1].ptr = (void *) cp ;
}
}
}
}
;
/*
sprintf -- try to parse form at compile time
- length is now overloaded to return size of an array
- note: first two rules give SR conflict, but S is correct so it's ok
*/
p_expr : LENGTH { $$ = code_offset ;
code2(_PUSHI,field) ;
code2(_BUILTIN,bi_length) ;
}
| LENGTH LPAREN RPAREN { $$ = code_offset ;
code2(_PUSHI,field) ;
code2(_BUILTIN,bi_length) ;
}
| LENGTH LPAREN expr RPAREN { $$ = $3 ;
code2(_BUILTIN,bi_length) ;
}
| LENGTH LPAREN ID RPAREN { SYMTAB* stp = $3 ;
$$ = code_offset ;
switch(stp->type) {
case ST_VAR:
code2(_PUSHI, stp->stval.cp) ;
code2(_BUILTIN, bi_length) ;
break ;
case ST_ARRAY:
code2(A_PUSHA, stp->stval.array) ;
code2(_BUILTIN, bi_alength) ;
break ;
case ST_LOCAL_VAR:
code2op(L_PUSHI, stp->offset) ;
code2(_BUILTIN, bi_length) ;
break ;
case ST_LOCAL_ARRAY:
code2op(LA_PUSHA, stp->offset) ;
code2(_BUILTIN, bi_alength) ;
break ;
case ST_NONE:
/* could go on modified resolve list, but too much work to
figure that out. Will be patched at run time */
code2(PI_LOAD, stp) ;
code2(_BUILTIN, bi_length) ;
break ;
case ST_LOCAL_NONE:
{ /* ditto, patched at run-time */
Local_PI* pi = (Local_PI *)zmalloc(sizeof(Local_PI)) ;
pi->fbp = active_funct ;
pi->offset = stp->offset ;
code2(LPI_LOAD, pi) ;
code2(_BUILTIN, bi_length) ;
}
break ;
default:
type_error(stp) ;
break ;
}
}
;
/* match(expr, RE) */
p_expr : MATCH_FUNC LPAREN expr COMMA re_arg RPAREN
{ $$ = $3 ;
code2(_BUILTIN, bi_match) ;
}
;
re_arg : expr { INST *p1 = CDP($1) ;
if ( p1 == code_ptr - 2 ) {
if ( p1->op == _MATCH0 )
RE_as_arg() ;
else
if ( p1->op == _PUSHS ) {
CELL *cp = ZMALLOC(CELL) ;
cp->type = C_STRING ;
cp->ptr = p1[1].ptr ;
cast_to_RE(cp) ;
p1->op = _PUSHC ;
p1[1].ptr = (void *) cp ;
}
}
}
/* exit_statement */
statement : EXIT separator { $$ = code_offset ;
code1(_EXIT0) ;
}
| EXIT expr separator { $$ = $2 ;