This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix double free introduced by #29543 (spotted by Nicholas)
[perl5.git] / perly.y
CommitLineData
a0d0e21e 1/* perly.y
a687059c 2 *
f05e27e5 3 * Copyright (c) 1991-2002, 2003, 2004, 2005, 2006 Larry Wall
a687059c 4 *
9ef589d8
LW
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
8d063cd8 7 *
a0d0e21e
LW
8 */
9
10/*
11 * 'I see,' laughed Strider. 'I look foul and feel fair. Is that it?
14a38fd5 12 * All that is gold does not glitter, not all those who wander are lost.'
f05e27e5
DM
13 *
14 * This file holds the grammar for the Perl language. If edited, you need
166f8a29
DM
15 * to run regen_perly.pl, which re-creates the files perly.h, perly.tab
16 * and perly.act which are derived from this.
17 *
f05e27e5
DM
18 * Note that these derived files are included and compiled twice; once
19 * from perly.c, and once from madly.c. The second time, a number of MAD
20 * macros are defined, which compile in extra code that allows the parse
21 * tree to be accurately dumped. In particular:
22 *
23 * MAD defined if compiling madly.c
24 * DO_MAD(A) expands to A under madly.c, to null otherwise
25 * IF_MAD(a,b) expands to A under madly.c, to B otherwise
26 * TOKEN_GETMAD() expands to token_getmad() under madly.c, to null otherwise
27 * TOKEN_FREE() similarly
28 * OP_GETMAD() similarly
29 * IVAL(i) expands to (i)->tk_lval.ival or (i)
30 * PVAL(p) expands to (p)->tk_lval.pval or (p)
31 *
166f8a29
DM
32 * The main job of of this grammar is to call the various newFOO()
33 * functions in op.c to build a syntax tree of OP structs.
61296642 34 * It relies on the lexer in toke.c to do the tokenizing.
166f8a29
DM
35 */
36
0de566d7 37/* Make the parser re-entrant. */
8d063cd8 38
0de566d7 39%pure_parser
8d063cd8 40
f05e27e5
DM
41/* FIXME for MAD - is the new mintro on while and until important? */
42
0de566d7 43%start prog
9d116dd7 44
8d063cd8 45%union {
d5c6462e
DM
46 I32 ival; /* __DEFAULT__ (marker for regen_perly.pl;
47 must always be 1st union member) */
79072805
LW
48 char *pval;
49 OP *opval;
50 GV *gvval;
f05e27e5
DM
51#ifdef PERL_IN_MADLY_C
52 TOKEN* p_tkval;
123d08c9 53 TOKEN* i_tkval;
f05e27e5
DM
54#else
55 char *p_tkval;
123d08c9
DM
56 I32 i_tkval;
57#endif
58#ifdef PERL_MAD
59 TOKEN* tkval;
f05e27e5 60#endif
8d063cd8
LW
61}
62
123d08c9 63%token <i_tkval> '{' '}' '[' ']' '-' '+' '$' '@' '%' '*' '&' ';'
f0fcb552 64
a0d0e21e 65%token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
4633a7c4 66%token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
f05e27e5 67%token <p_tkval> LABEL
123d08c9
DM
68%token <i_tkval> FORMAT SUB ANONSUB PACKAGE USE
69%token <i_tkval> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
70%token <i_tkval> GIVEN WHEN DEFAULT
71%token <i_tkval> LOOPEX DOTDOT
72%token <i_tkval> FUNC0 FUNC1 FUNC UNIOP LSTOP
73%token <i_tkval> RELOP EQOP MULOP ADDOP
74%token <i_tkval> DOLSHARP DO HASHBRACK NOAMP
75%token <i_tkval> LOCAL MY MYSUB REQUIRE
76%token <i_tkval> COLONATTR
f05e27e5
DM
77
78%type <ival> prog progstart remember mremember savescope
79%type <ival> startsub startanonsub startformsub
80/* FIXME for MAD - are these two ival? */
81%type <ival> mydefsv mintro
82
83%type <opval> decl format subrout mysubrout package use peg
84
bbce6d69 85%type <opval> block mblock lineseq line loop cond else
fad39ff1 86%type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
a034e688 87%type <opval> argexpr nexpr texpr iexpr mexpr mnexpr miexpr
44a8e56a
PP
88%type <opval> listexpr listexprcom indirob listop method
89%type <opval> formname subname proto subbody cont my_scalar
f05e27e5 90%type <opval> subattrlist myattrlist myattrterm myterm
891be019 91%type <opval> termbinop termunop anonymous termdo
0d863452 92%type <opval> switch case
f05e27e5 93%type <p_tkval> label
79072805 94
123d08c9 95%nonassoc <i_tkval> PREC_LOW
fad39ff1
SM
96%nonassoc LOOPEX
97
123d08c9
DM
98%left <i_tkval> OROP DOROP
99%left <i_tkval> ANDOP
100%right <i_tkval> NOTOP
36477c24 101%nonassoc LSTOP LSTOPSUB
123d08c9
DM
102%left <i_tkval> ','
103%right <i_tkval> ASSIGNOP
104%right <i_tkval> '?' ':'
8d063cd8 105%nonassoc DOTDOT
123d08c9
DM
106%left <i_tkval> OROR DORDOR
107%left <i_tkval> ANDAND
108%left <i_tkval> BITOROP
109%left <i_tkval> BITANDOP
a687059c
LW
110%nonassoc EQOP
111%nonassoc RELOP
36477c24 112%nonassoc UNIOP UNIOPSUB
20515881 113%nonassoc REQUIRE
123d08c9 114%left <i_tkval> SHIFTOP
a687059c
LW
115%left ADDOP
116%left MULOP
123d08c9
DM
117%left <i_tkval> MATCHOP
118%right <i_tkval> '!' '~' UMINUS REFGEN
119%right <i_tkval> POWOP
120%nonassoc <i_tkval> PREINC PREDEC POSTINC POSTDEC
121%left <i_tkval> ARROW
122%nonassoc <i_tkval> ')'
123%left <i_tkval> '('
fad39ff1 124%left '[' '{'
8d063cd8 125
123d08c9 126%token <i_tkval> PEG
7f46837f 127
8d063cd8
LW
128%% /* RULES */
129
891be019 130/* The whole program */
ecb2f335 131prog : progstart
ae986130 132 /*CONTINUED*/ lineseq
ecb2f335 133 { $$ = $1; newPROG(block_end($1,$2)); }
8d063cd8
LW
134 ;
135
891be019 136/* An ordinary block */
a687059c 137block : '{' remember lineseq '}'
f05e27e5
DM
138 { if (PL_copline > (line_t)IVAL($1))
139 PL_copline = (line_t)IVAL($1);
140 $$ = block_end($2, $3);
141 TOKEN_GETMAD($1,$$,'{');
142 TOKEN_GETMAD($4,$$,'}');
143 }
a0d0e21e
LW
144 ;
145
55497cff
PP
146remember: /* NULL */ /* start a full lexical scope */
147 { $$ = block_start(TRUE); }
148 ;
149
0d863452
RH
150mydefsv: /* NULL */ /* lexicalize $_ */
151 { $$ = (I32) allocmy("$_"); }
152 ;
153
ecb2f335
NIS
154progstart:
155 {
ecb2f335
NIS
156 PL_expect = XSTATE; $$ = block_start(TRUE);
157 }
158 ;
159
160
bbce6d69 161mblock : '{' mremember lineseq '}'
f05e27e5
DM
162 { if (PL_copline > (line_t)IVAL($1))
163 PL_copline = (line_t)IVAL($1);
164 $$ = block_end($2, $3);
165 TOKEN_GETMAD($1,$$,'{');
166 TOKEN_GETMAD($4,$$,'}');
167 }
55497cff
PP
168 ;
169
170mremember: /* NULL */ /* start a partial lexical scope */
171 { $$ = block_start(FALSE); }
8d063cd8
LW
172 ;
173
500bedb6
DM
174savescope: /* NULL */ /* remember stack pos in case of error */
175 { $$ = PL_savestack_ix; }
176
891be019 177/* A collection of "lines" in the program */
8d063cd8 178lineseq : /* NULL */
79072805
LW
179 { $$ = Nullop; }
180 | lineseq decl
f05e27e5
DM
181 {
182 $$ = IF_MAD(
183 append_list(OP_LINESEQ,
184 (LISTOP*)$1, (LISTOP*)$2),
185 $1);
186 }
500bedb6
DM
187 | lineseq savescope line
188 { LEAVE_SCOPE($2);
189 $$ = append_list(OP_LINESEQ,
190 (LISTOP*)$1, (LISTOP*)$3);
3280af22 191 PL_pad_reset_pending = TRUE;
500bedb6 192 if ($1 && $3) PL_hints |= HINT_BLOCK_SCOPE; }
8d063cd8
LW
193 ;
194
891be019 195/* A "line" in the program */
79072805 196line : label cond
f05e27e5
DM
197 { $$ = newSTATEOP(0, PVAL($1), $2);
198 TOKEN_GETMAD($1,((LISTOP*)$$)->op_first,'L'); }
8d063cd8 199 | loop /* loops add their own labels */
0d863452
RH
200 | switch /* ... and so do switches */
201 { $$ = $1; }
202 | label case
f05e27e5 203 { $$ = newSTATEOP(0, PVAL($1), $2); }
8d063cd8 204 | label ';'
f05e27e5
DM
205 {
206 if (PVAL($1)) {
207 $$ = newSTATEOP(0, PVAL($1), newOP(OP_NULL, 0));
208 TOKEN_GETMAD($1,$$,'L');
209 TOKEN_GETMAD($2,((LISTOP*)$$)->op_first,';');
210 }
211 else {
212 $$ = IF_MAD(
213 newOP(OP_NULL, 0),
214 Nullop);
215 PL_copline = NOLINE;
216 TOKEN_FREE($1);
217 TOKEN_GETMAD($2,$$,';');
218 }
219 PL_expect = XSTATE;
220 }
8d063cd8 221 | label sideff ';'
f05e27e5
DM
222 {
223 $$ = newSTATEOP(0, PVAL($1), $2);
224 PL_expect = XSTATE;
225 DO_MAD(
226 /* sideff might already have a nexstate */
227 OP* op = ((LISTOP*)$$)->op_first;
228 if (op) {
229 while (op->op_sibling &&
230 op->op_sibling->op_type == OP_NEXTSTATE)
231 op = op->op_sibling;
232 token_getmad($1,op,'L');
233 token_getmad($3,op,';');
234 }
235 )
236 }
8d063cd8
LW
237 ;
238
891be019 239/* An expression which may have a side-effect */
a687059c 240sideff : error
79072805 241 { $$ = Nullop; }
a687059c 242 | expr
79072805 243 { $$ = $1; }
a687059c 244 | expr IF expr
f05e27e5
DM
245 { $$ = newLOGOP(OP_AND, 0, $3, $1);
246 TOKEN_GETMAD($2,$$,'i');
247 }
a687059c 248 | expr UNLESS expr
f05e27e5
DM
249 { $$ = newLOGOP(OP_OR, 0, $3, $1);
250 TOKEN_GETMAD($2,$$,'i');
251 }
a687059c 252 | expr WHILE expr
f05e27e5
DM
253 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1);
254 TOKEN_GETMAD($2,$$,'w');
255 }
55497cff 256 | expr UNTIL iexpr
f05e27e5
DM
257 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);
258 TOKEN_GETMAD($2,$$,'w');
259 }
ecca16b0 260 | expr FOR expr
f05e27e5
DM
261 { $$ = newFOROP(0, Nullch, (line_t)IVAL($2),
262 Nullop, $3, $1, Nullop);
263 TOKEN_GETMAD($2,((LISTOP*)$$)->op_first->op_sibling,'w');
264 }
79072805
LW
265 ;
266
891be019 267/* else and elsif blocks */
79072805
LW
268else : /* NULL */
269 { $$ = Nullop; }
55497cff 270 | ELSE mblock
f05e27e5
DM
271 { ($2)->op_flags |= OPf_PARENS; $$ = scope($2);
272 TOKEN_GETMAD($1,$$,'o');
273 }
55497cff 274 | ELSIF '(' mexpr ')' mblock else
f05e27e5 275 { PL_copline = (line_t)IVAL($1);
2c15bef3 276 $$ = newCONDOP(0, $3, scope($5), $6);
f05e27e5
DM
277 PL_hints |= HINT_BLOCK_SCOPE;
278 TOKEN_GETMAD($1,$$,'I');
279 TOKEN_GETMAD($2,$$,'(');
280 TOKEN_GETMAD($4,$$,')');
281 }
79072805
LW
282 ;
283
891be019 284/* Real conditional expressions */
55497cff 285cond : IF '(' remember mexpr ')' mblock else
f05e27e5 286 { PL_copline = (line_t)IVAL($1);
36477c24 287 $$ = block_end($3,
f05e27e5
DM
288 newCONDOP(0, $4, scope($6), $7));
289 TOKEN_GETMAD($1,$$,'I');
290 TOKEN_GETMAD($2,$$,'(');
291 TOKEN_GETMAD($5,$$,')');
292 }
55497cff 293 | UNLESS '(' remember miexpr ')' mblock else
f05e27e5 294 { PL_copline = (line_t)IVAL($1);
36477c24 295 $$ = block_end($3,
f05e27e5
DM
296 newCONDOP(0, $4, scope($6), $7));
297 TOKEN_GETMAD($1,$$,'I');
298 TOKEN_GETMAD($2,$$,'(');
299 TOKEN_GETMAD($5,$$,')');
300 }
79072805
LW
301 ;
302
0d863452
RH
303/* Cases for a switch statement */
304case : WHEN '(' remember mexpr ')' mblock
305 { $$ = block_end($3,
306 newWHENOP($4, scope($6))); }
307 | DEFAULT block
308 { $$ = newWHENOP(0, scope($2)); }
309 ;
310
891be019 311/* Continue blocks */
79072805
LW
312cont : /* NULL */
313 { $$ = Nullop; }
314 | CONTINUE block
f05e27e5
DM
315 { $$ = scope($2);
316 TOKEN_GETMAD($1,$$,'o');
317 }
79072805
LW
318 ;
319
891be019 320/* Loops: while, until, for, and a bare block */
a034e688 321loop : label WHILE '(' remember texpr ')' mintro mblock cont
f05e27e5
DM
322 { OP *innerop;
323 PL_copline = (line_t)$2;
36477c24 324 $$ = block_end($4,
f05e27e5
DM
325 newSTATEOP(0, PVAL($1),
326 innerop = newWHILEOP(0, 1, (LOOP*)Nullop,
327 IVAL($2), $5, $8, $9, $7)));
328 TOKEN_GETMAD($1,innerop,'L');
329 TOKEN_GETMAD($2,innerop,'W');
330 TOKEN_GETMAD($3,innerop,'(');
331 TOKEN_GETMAD($6,innerop,')');
332 }
333
a034e688 334 | label UNTIL '(' remember iexpr ')' mintro mblock cont
f05e27e5
DM
335 { OP *innerop;
336 PL_copline = (line_t)$2;
36477c24 337 $$ = block_end($4,
f05e27e5
DM
338 newSTATEOP(0, PVAL($1),
339 innerop = newWHILEOP(0, 1, (LOOP*)Nullop,
340 IVAL($2), $5, $8, $9, $7)));
341 TOKEN_GETMAD($1,innerop,'L');
342 TOKEN_GETMAD($2,innerop,'W');
343 TOKEN_GETMAD($3,innerop,'(');
344 TOKEN_GETMAD($6,innerop,')');
345 }
bbce6d69 346 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
f05e27e5
DM
347 { OP *innerop;
348 $$ = block_end($4,
349 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
350 $5, $7, $9, $10));
351 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
352 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
353 TOKEN_GETMAD($3,((LISTOP*)innerop)->op_first->op_sibling,'d');
354 TOKEN_GETMAD($6,((LISTOP*)innerop)->op_first->op_sibling,'(');
355 TOKEN_GETMAD($8,((LISTOP*)innerop)->op_first->op_sibling,')');
356 }
bbce6d69 357 | label FOR scalar '(' remember mexpr ')' mblock cont
f05e27e5
DM
358 { OP *innerop;
359 $$ = block_end($5,
360 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
361 mod($3, OP_ENTERLOOP), $6, $8, $9));
362 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
363 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
364 TOKEN_GETMAD($4,((LISTOP*)innerop)->op_first->op_sibling,'(');
365 TOKEN_GETMAD($7,((LISTOP*)innerop)->op_first->op_sibling,')');
366 }
bbce6d69 367 | label FOR '(' remember mexpr ')' mblock cont
f05e27e5
DM
368 { OP *innerop;
369 $$ = block_end($4,
370 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
371 Nullop, $5, $7, $8));
372 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
373 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
374 TOKEN_GETMAD($3,((LISTOP*)innerop)->op_first->op_sibling,'(');
375 TOKEN_GETMAD($6,((LISTOP*)innerop)->op_first->op_sibling,')');
376 }
a034e688
DM
377 | label FOR '(' remember mnexpr ';' texpr ';' mintro mnexpr ')'
378 mblock
8d063cd8 379 /* basically fake up an initialize-while lineseq */
36c66720 380 { OP *forop;
f05e27e5
DM
381 PL_copline = (line_t)IVAL($2);
382 forop = newSTATEOP(0, PVAL($1),
36c66720 383 newWHILEOP(0, 1, (LOOP*)Nullop,
f05e27e5 384 IVAL($2), scalar($7),
a034e688 385 $12, $10, $9));
f05e27e5
DM
386#ifdef MAD
387 if (!$5)
388 $5 = newOP(OP_NULL, 0);
389 forop = newUNOP(OP_NULL, 0, append_elem(OP_LINESEQ,
390 newSTATEOP(0,
391 (($1)->tk_lval.pval
392 ?savepv(($1)->tk_lval.pval):Nullch),
393 $5),
394 forop));
395
396 token_getmad($2,forop,'3');
397 token_getmad($3,forop,'(');
398 token_getmad($6,forop,'1');
399 token_getmad($8,forop,'2');
400 token_getmad($11,forop,')');
401 token_getmad($1,forop,'L');
402#else
36c66720
RH
403 if ($5) {
404 forop = append_elem(OP_LINESEQ,
405 newSTATEOP(0, ($1?savepv($1):Nullch),
406 $5),
407 forop);
408 }
409
f05e27e5
DM
410
411#endif
36c66720 412 $$ = block_end($4, forop); }
79072805 413 | label block cont /* a block is a loop that happens once */
f05e27e5 414 { $$ = newSTATEOP(0, PVAL($1),
fb73857a 415 newWHILEOP(0, 1, (LOOP*)Nullop,
f05e27e5
DM
416 NOLINE, Nullop, $2, $3, 0));
417 TOKEN_GETMAD($1,((LISTOP*)$$)->op_first,'L'); }
8d063cd8
LW
418 ;
419
0d863452
RH
420/* Switch blocks */
421switch : label GIVEN '(' remember mydefsv mexpr ')' mblock
422 { PL_copline = (line_t) $2;
423 $$ = block_end($4,
f05e27e5 424 newSTATEOP(0, PVAL($1),
0d863452
RH
425 newGIVENOP($6, scope($8),
426 (PADOFFSET) $5) )); }
427 ;
428
a034e688
DM
429/* determine whether there are any new my declarations */
430mintro : /* NULL */
431 { $$ = (PL_min_intro_pending &&
432 PL_max_intro_pending >= PL_min_intro_pending);
433 intro_my(); }
434
891be019 435/* Normal expression */
8d063cd8 436nexpr : /* NULL */
79072805 437 { $$ = Nullop; }
8d063cd8
LW
438 | sideff
439 ;
440
891be019 441/* Boolean expression */
8d063cd8 442texpr : /* NULL means true */
f05e27e5
DM
443 { YYSTYPE tmplval;
444 (void)scan_num("1", &tmplval);
445 $$ = tmplval.opval; }
8d063cd8
LW
446 | expr
447 ;
448
891be019 449/* Inverted boolean expression */
55497cff
PP
450iexpr : expr
451 { $$ = invert(scalar($1)); }
452 ;
453
891be019 454/* Expression with its own lexical scope */
55497cff 455mexpr : expr
bbce6d69
PP
456 { $$ = $1; intro_my(); }
457 ;
458
459mnexpr : nexpr
460 { $$ = $1; intro_my(); }
55497cff
PP
461 ;
462
55497cff 463miexpr : iexpr
bbce6d69 464 { $$ = $1; intro_my(); }
55497cff
PP
465 ;
466
891be019 467/* Optional "MAIN:"-style loop labels */
8d063cd8 468label : /* empty */
f05e27e5
DM
469 {
470#ifdef MAD
471 YYSTYPE tmplval;
472 tmplval.pval = Nullch;
473 $$ = newTOKEN(OP_NULL, tmplval, 0);
474#else
475 $$ = Nullch;
476#endif
477 }
32c2e4fb 478 | LABEL
8d063cd8
LW
479 ;
480
f05e27e5 481/* Some kind of declaration - just hang on peg in the parse tree */
8d063cd8 482decl : format
f05e27e5 483 { $$ = $1; }
8d063cd8 484 | subrout
f05e27e5 485 { $$ = $1; }
09bef843 486 | mysubrout
f05e27e5 487 { $$ = $1; }
a687059c 488 | package
f05e27e5 489 { $$ = $1; }
a0d0e21e 490 | use
f05e27e5
DM
491 { $$ = $1; }
492
493 /* these two are only used by MAD */
494
495 | peg
496 { $$ = $1; }
497 ;
498
499peg : PEG
500 { $$ = newOP(OP_NULL,0);
501 TOKEN_GETMAD($1,$$,'p');
502 }
8d063cd8
LW
503 ;
504
718a7425 505format : FORMAT startformsub formname block
a8ff2fa6 506 { SvREFCNT_inc(PL_compcv);
f05e27e5 507#ifdef MAD
718a7425 508 $$ = newFORM($2, $3, $4);
f05e27e5
DM
509 prepend_madprops($1->tk_mad, $$, 'F');
510 $1->tk_mad = 0;
511 token_free($1);
512#else
718a7425 513 newFORM($2, $3, $4);
30994c59 514 $$ = Nullop;
f05e27e5
DM
515#endif
516 }
44a8e56a
PP
517 ;
518
519formname: WORD { $$ = $1; }
520 | /* NULL */ { $$ = Nullop; }
8d063cd8
LW
521 ;
522
891be019 523/* Unimplemented "my sub foo { }" */
718a7425 524mysubrout: MYSUB startsub subname proto subattrlist subbody
a8ff2fa6 525 { SvREFCNT_inc(PL_compcv);
f05e27e5 526#ifdef MAD
718a7425 527 $$ = newMYSUB($2, $3, $4, $5, $6);
f05e27e5
DM
528 token_getmad($1,$$,'d');
529#else
718a7425 530 newMYSUB($2, $3, $4, $5, $6);
30994c59 531 $$ = Nullop;
f05e27e5
DM
532#endif
533 }
09bef843
SB
534 ;
535
891be019 536/* Subroutine definition */
718a7425 537subrout : SUB startsub subname proto subattrlist subbody
a8ff2fa6 538 { SvREFCNT_inc(PL_compcv);
f05e27e5
DM
539#ifdef MAD
540 OP* o = newSVOP(OP_ANONCODE, 0,
718a7425 541 (SV*)newATTRSUB($2, $3, $4, $5, $6));
f05e27e5
DM
542 $$ = newOP(OP_NULL,0);
543 op_getmad(o,$$,'&');
718a7425
DM
544 op_getmad($3,$$,'n');
545 op_getmad($4,$$,'s');
546 op_getmad($5,$$,'a');
f05e27e5 547 token_getmad($1,$$,'d');
718a7425
DM
548 append_madprops($6->op_madprop, $$, 0);
549 $6->op_madprop = 0;
f05e27e5 550#else
718a7425 551 newATTRSUB($2, $3, $4, $5, $6);
f05e27e5
DM
552 $$ = Nullop;
553#endif
554 }
28757baa
PP
555 ;
556
fa83b5b6 557startsub: /* NULL */ /* start a regular subroutine scope */
a8ff2fa6
DM
558 { $$ = start_subparse(FALSE, 0);
559 SAVEFREESV(PL_compcv); }
f05e27e5 560
28757baa
PP
561 ;
562
563startanonsub: /* NULL */ /* start an anonymous subroutine scope */
a8ff2fa6
DM
564 { $$ = start_subparse(FALSE, CVf_ANON);
565 SAVEFREESV(PL_compcv); }
28757baa
PP
566 ;
567
44a8e56a 568startformsub: /* NULL */ /* start a format subroutine scope */
a8ff2fa6
DM
569 { $$ = start_subparse(TRUE, 0);
570 SAVEFREESV(PL_compcv); }
44a8e56a
PP
571 ;
572
891be019 573/* Name of a subroutine - must be a bareword, could be special */
2596d9fe 574subname : WORD { const char *const name = SvPV_nolen_const(((SVOP*)$1)->op_sv);
e858de61 575 if (strEQ(name, "BEGIN") || strEQ(name, "END")
3c10abe3
AG
576 || strEQ(name, "INIT") || strEQ(name, "CHECK")
577 || strEQ(name, "UNITCHECK"))
1aff0e91 578 CvSPECIAL_on(PL_compcv);
28757baa 579 $$ = $1; }
a0d0e21e
LW
580 ;
581
891be019 582/* Subroutine prototype */
4633a7c4
LW
583proto : /* NULL */
584 { $$ = Nullop; }
585 | THING
586 ;
28757baa 587
891be019 588/* Optional list of subroutine attributes */
09bef843
SB
589subattrlist: /* NULL */
590 { $$ = Nullop; }
591 | COLONATTR THING
f05e27e5
DM
592 { $$ = $2;
593 TOKEN_GETMAD($1,$$,':');
594 }
09bef843 595 | COLONATTR
f05e27e5
DM
596 { $$ = IF_MAD(
597 newOP(OP_NULL, 0),
598 Nullop
599 );
600 TOKEN_GETMAD($1,$$,':');
601 }
09bef843
SB
602 ;
603
891be019 604/* List of attributes for a "my" variable declaration */
09bef843 605myattrlist: COLONATTR THING
f05e27e5
DM
606 { $$ = $2;
607 TOKEN_GETMAD($1,$$,':');
608 }
09bef843 609 | COLONATTR
f05e27e5
DM
610 { $$ = IF_MAD(
611 newOP(OP_NULL, 0),
612 Nullop
613 );
614 TOKEN_GETMAD($1,$$,':');
615 }
09bef843
SB
616 ;
617
891be019 618/* Subroutine body - either null or a block */
28757baa 619subbody : block { $$ = $1; }
f05e27e5
DM
620 | ';' { $$ = IF_MAD(
621 newOP(OP_NULL,0),
622 Nullop
623 );
624 PL_expect = XSTATE;
625 TOKEN_GETMAD($1,$$,';');
626 }
8d063cd8
LW
627 ;
628
a687059c 629package : PACKAGE WORD ';'
f05e27e5
DM
630 {
631#ifdef MAD
632 $$ = package($2);
633 token_getmad($1,$$,'o');
634 token_getmad($3,$$,';');
635#else
636 package($2);
30994c59 637 $$ = Nullop;
f05e27e5
DM
638#endif
639 }
a687059c
LW
640 ;
641
718a7425 642use : USE startsub
1aff0e91 643 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
28757baa 644 WORD WORD listexpr ';'
f05e27e5
DM
645 { SvREFCNT_inc(PL_compcv);
646#ifdef MAD
718a7425 647 $$ = utilize(IVAL($1), $2, $4, $5, $6);
f05e27e5 648 token_getmad($1,$$,'o');
718a7425 649 token_getmad($7,$$,';');
f05e27e5
DM
650 if (PL_rsfp_filters && AvFILLp(PL_rsfp_filters) >= 0)
651 append_madprops(newMADPROP('!', MAD_PV, "", 0), $$, 0);
652#else
718a7425 653 utilize(IVAL($1), $2, $4, $5, $6);
30994c59 654 $$ = Nullop;
f05e27e5
DM
655#endif
656 }
85e6fe83
LW
657 ;
658
891be019 659/* Ordinary expressions; logical combinations */
a0d0e21e 660expr : expr ANDOP expr
f05e27e5
DM
661 { $$ = newLOGOP(OP_AND, 0, $1, $3);
662 TOKEN_GETMAD($2,$$,'o');
663 }
a0d0e21e 664 | expr OROP expr
f05e27e5
DM
665 { $$ = newLOGOP(IVAL($2), 0, $1, $3);
666 TOKEN_GETMAD($2,$$,'o');
667 }
c963b151 668 | expr DOROP expr
f05e27e5
DM
669 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
670 TOKEN_GETMAD($2,$$,'o');
671 }
fad39ff1 672 | argexpr %prec PREC_LOW
a0d0e21e
LW
673 ;
674
891be019 675/* Expressions are a list of terms joined by commas */
a0d0e21e 676argexpr : argexpr ','
f05e27e5
DM
677 {
678#ifdef MAD
679 OP* op = newNULLLIST();
680 token_getmad($2,op,',');
681 $$ = append_elem(OP_LIST, $1, op);
682#else
683 $$ = $1;
684#endif
685 }
a0d0e21e 686 | argexpr ',' term
f05e27e5
DM
687 {
688 DO_MAD(
689 $3 = newUNOP(OP_NULL, 0, $3);
690 token_getmad($2,$3,',');
691 )
692 $$ = append_elem(OP_LIST, $1, $3);
693 }
fad39ff1 694 | term %prec PREC_LOW
8d063cd8
LW
695 ;
696
891be019 697/* List operators */
ad4673e5 698listop : LSTOP indirob argexpr /* map {...} @args or print $fh @args */
f05e27e5
DM
699 { $$ = convert(IVAL($1), OPf_STACKED,
700 prepend_elem(OP_LIST, newGVREF(IVAL($1),$2), $3) );
701 TOKEN_GETMAD($1,$$,'o');
702 }
891be019 703 | FUNC '(' indirob expr ')' /* print ($fh @args */
f05e27e5
DM
704 { $$ = convert(IVAL($1), OPf_STACKED,
705 prepend_elem(OP_LIST, newGVREF(IVAL($1),$3), $4) );
706 TOKEN_GETMAD($1,$$,'o');
707 TOKEN_GETMAD($2,$$,'(');
708 TOKEN_GETMAD($5,$$,')');
709 }
891be019 710 | term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
4633a7c4 711 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
a0d0e21e 712 append_elem(OP_LIST,
55497cff 713 prepend_elem(OP_LIST, scalar($1), $5),
f05e27e5
DM
714 newUNOP(OP_METHOD, 0, $3)));
715 TOKEN_GETMAD($2,$$,'A');
716 TOKEN_GETMAD($4,$$,'(');
717 TOKEN_GETMAD($6,$$,')');
718 }
891be019 719 | term ARROW method /* $foo->bar */
b1524f17
SM
720 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
721 append_elem(OP_LIST, scalar($1),
f05e27e5
DM
722 newUNOP(OP_METHOD, 0, $3)));
723 TOKEN_GETMAD($2,$$,'A');
724 }
891be019 725 | METHOD indirob listexpr /* new Class @args */
4633a7c4 726 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
a0d0e21e 727 append_elem(OP_LIST,
4633a7c4 728 prepend_elem(OP_LIST, $2, $3),
f05e27e5
DM
729 newUNOP(OP_METHOD, 0, $1)));
730 }
891be019 731 | FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
4633a7c4 732 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
a0d0e21e 733 append_elem(OP_LIST,
4633a7c4 734 prepend_elem(OP_LIST, $2, $4),
f05e27e5
DM
735 newUNOP(OP_METHOD, 0, $1)));
736 TOKEN_GETMAD($3,$$,'(');
737 TOKEN_GETMAD($5,$$,')');
738 }
891be019 739 | LSTOP listexpr /* print @args */
f05e27e5
DM
740 { $$ = convert(IVAL($1), 0, $2);
741 TOKEN_GETMAD($1,$$,'o');
742 }
891be019 743 | FUNC '(' listexprcom ')' /* print (@args) */
f05e27e5
DM
744 { $$ = convert(IVAL($1), 0, $3);
745 TOKEN_GETMAD($1,$$,'o');
746 TOKEN_GETMAD($2,$$,'(');
747 TOKEN_GETMAD($4,$$,')');
748 }
718a7425 749 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
a8ff2fa6 750 { SvREFCNT_inc(PL_compcv);
718a7425 751 $3 = newANONATTRSUB($2, 0, Nullop, $3); }
891be019 752 listexpr %prec LSTOP /* ... @bar */
4633a7c4 753 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
28757baa 754 append_elem(OP_LIST,
718a7425 755 prepend_elem(OP_LIST, $3, $5), $1));
f05e27e5 756 }
a687059c
LW
757 ;
758
891be019 759/* Names of methods. May use $object->$methodname */
a0d0e21e
LW
760method : METHOD
761 | scalar
762 ;
763
891be019
SC
764/* Some kind of subscripted expression */
765subscripted: star '{' expr ';' '}' /* *main::{something} */
766 /* In this and all the hash accessors, ';' is
767 * provided by the tokeniser */
264e1af3 768 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
f05e27e5
DM
769 PL_expect = XOPERATOR;
770 TOKEN_GETMAD($2,$$,'{');
771 TOKEN_GETMAD($4,$$,';');
772 TOKEN_GETMAD($5,$$,'}');
773 }
891be019 774 | scalar '[' expr ']' /* $array[$element] */
f05e27e5
DM
775 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
776 TOKEN_GETMAD($2,$$,'[');
777 TOKEN_GETMAD($4,$$,']');
778 }
891be019 779 | term ARROW '[' expr ']' /* somearef->[$element] */
fad39ff1
SM
780 { $$ = newBINOP(OP_AELEM, 0,
781 ref(newAVREF($1),OP_RV2AV),
f05e27e5
DM
782 scalar($4));
783 TOKEN_GETMAD($2,$$,'a');
784 TOKEN_GETMAD($3,$$,'[');
785 TOKEN_GETMAD($5,$$,']');
786 }
891be019 787 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
fad39ff1
SM
788 { $$ = newBINOP(OP_AELEM, 0,
789 ref(newAVREF($1),OP_RV2AV),
f05e27e5
DM
790 scalar($3));
791 TOKEN_GETMAD($2,$$,'[');
792 TOKEN_GETMAD($4,$$,']');
793 }
891be019 794 | scalar '{' expr ';' '}' /* $foo->{bar();} */
fad39ff1 795 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
f05e27e5
DM
796 PL_expect = XOPERATOR;
797 TOKEN_GETMAD($2,$$,'{');
798 TOKEN_GETMAD($4,$$,';');
799 TOKEN_GETMAD($5,$$,'}');
800 }
891be019 801 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
fad39ff1
SM
802 { $$ = newBINOP(OP_HELEM, 0,
803 ref(newHVREF($1),OP_RV2HV),
804 jmaybe($4));
f05e27e5
DM
805 PL_expect = XOPERATOR;
806 TOKEN_GETMAD($2,$$,'a');
807 TOKEN_GETMAD($3,$$,'{');
808 TOKEN_GETMAD($5,$$,';');
809 TOKEN_GETMAD($6,$$,'}');
810 }
891be019 811 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
fad39ff1
SM
812 { $$ = newBINOP(OP_HELEM, 0,
813 ref(newHVREF($1),OP_RV2HV),
814 jmaybe($3));
f05e27e5
DM
815 PL_expect = XOPERATOR;
816 TOKEN_GETMAD($2,$$,'{');
817 TOKEN_GETMAD($4,$$,';');
818 TOKEN_GETMAD($5,$$,'}');
819 }
891be019 820 | term ARROW '(' ')' /* $subref->() */
fad39ff1 821 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
f05e27e5
DM
822 newCVREF(0, scalar($1)));
823 TOKEN_GETMAD($2,$$,'a');
824 TOKEN_GETMAD($3,$$,'(');
825 TOKEN_GETMAD($4,$$,')');
826 }
891be019 827 | term ARROW '(' expr ')' /* $subref->(@args) */
fad39ff1
SM
828 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
829 append_elem(OP_LIST, $4,
f05e27e5
DM
830 newCVREF(0, scalar($1))));
831 TOKEN_GETMAD($2,$$,'a');
832 TOKEN_GETMAD($3,$$,'(');
833 TOKEN_GETMAD($5,$$,')');
834 }
fad39ff1 835
891be019 836 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
fad39ff1
SM
837 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
838 append_elem(OP_LIST, $3,
f05e27e5
DM
839 newCVREF(0, scalar($1))));
840 TOKEN_GETMAD($2,$$,'(');
841 TOKEN_GETMAD($4,$$,')');
842 }
891be019 843 | subscripted '(' ')' /* $foo->{bar}->() */
fad39ff1 844 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
f05e27e5
DM
845 newCVREF(0, scalar($1)));
846 TOKEN_GETMAD($2,$$,'(');
847 TOKEN_GETMAD($3,$$,')');
848 }
9a9798c2 849 | '(' expr ')' '[' expr ']' /* list slice */
f05e27e5
DM
850 { $$ = newSLICEOP(0, $5, $2);
851 TOKEN_GETMAD($1,$$,'(');
852 TOKEN_GETMAD($3,$$,')');
853 TOKEN_GETMAD($4,$$,'[');
854 TOKEN_GETMAD($6,$$,']');
855 }
9a9798c2 856 | '(' ')' '[' expr ']' /* empty list slice! */
f05e27e5
DM
857 { $$ = newSLICEOP(0, $4, Nullop);
858 TOKEN_GETMAD($1,$$,'(');
859 TOKEN_GETMAD($2,$$,')');
860 TOKEN_GETMAD($3,$$,'[');
861 TOKEN_GETMAD($5,$$,']');
862 }
891be019 863 ;
fad39ff1 864
891be019 865/* Binary operators between terms */
f05e27e5
DM
866termbinop: term ASSIGNOP term /* $x = $y */
867 { $$ = newASSIGNOP(OPf_STACKED, $1, IVAL($2), $3);
868 TOKEN_GETMAD($2,$$,'o');
869 }
891be019 870 | term POWOP term /* $x ** $y */
f05e27e5
DM
871 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
872 TOKEN_GETMAD($2,$$,'o');
873 }
891be019 874 | term MULOP term /* $x * $y, $x x $y */
f05e27e5 875 { if (IVAL($2) != OP_REPEAT)
79072805 876 scalar($1);
f05e27e5
DM
877 $$ = newBINOP(IVAL($2), 0, $1, scalar($3));
878 TOKEN_GETMAD($2,$$,'o');
879 }
891be019 880 | term ADDOP term /* $x + $y */
f05e27e5
DM
881 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
882 TOKEN_GETMAD($2,$$,'o');
883 }
891be019 884 | term SHIFTOP term /* $x >> $y, $x << $y */
f05e27e5
DM
885 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
886 TOKEN_GETMAD($2,$$,'o');
887 }
891be019 888 | term RELOP term /* $x > $y, etc. */
f05e27e5
DM
889 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
890 TOKEN_GETMAD($2,$$,'o');
891 }
891be019 892 | term EQOP term /* $x == $y, $x eq $y */
f05e27e5
DM
893 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
894 TOKEN_GETMAD($2,$$,'o');
895 }
891be019 896 | term BITANDOP term /* $x & $y */
f05e27e5
DM
897 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
898 TOKEN_GETMAD($2,$$,'o');
899 }
891be019 900 | term BITOROP term /* $x | $y */
f05e27e5
DM
901 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
902 TOKEN_GETMAD($2,$$,'o');
903 }
891be019 904 | term DOTDOT term /* $x..$y, $x...$y */
f05e27e5
DM
905 {
906 $$ = newRANGE(IVAL($2), scalar($1), scalar($3));
907 DO_MAD(
908 UNOP *op;
909 op = (UNOP*)$$;
910 op = (UNOP*)op->op_first; /* get to flop */
911 op = (UNOP*)op->op_first; /* get to flip */
912 op = (UNOP*)op->op_first; /* get to range */
913 token_getmad($2,(OP*)op,'o');
914 )
915 }
891be019 916 | term ANDAND term /* $x && $y */
f05e27e5
DM
917 { $$ = newLOGOP(OP_AND, 0, $1, $3);
918 TOKEN_GETMAD($2,$$,'o');
919 }
891be019 920 | term OROR term /* $x || $y */
f05e27e5
DM
921 { $$ = newLOGOP(OP_OR, 0, $1, $3);
922 TOKEN_GETMAD($2,$$,'o');
923 }
c963b151 924 | term DORDOR term /* $x // $y */
f05e27e5
DM
925 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
926 TOKEN_GETMAD($2,$$,'o');
927 }
891be019 928 | term MATCHOP term /* $x =~ /$y/ */
f05e27e5
DM
929 { $$ = bind_match(IVAL($2), $1, $3);
930 TOKEN_GETMAD($2,
931 ($$->op_type == OP_NOT
932 ? ((UNOP*)$$)->op_first : $$),
933 '~');
934 }
891be019 935 ;
8d063cd8 936
891be019
SC
937/* Unary operators and terms */
938termunop : '-' term %prec UMINUS /* -$x */
f05e27e5
DM
939 { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
940 TOKEN_GETMAD($1,$$,'o');
941 }
891be019 942 | '+' term %prec UMINUS /* +$x */
f05e27e5
DM
943 { $$ = IF_MAD(
944 newUNOP(OP_NULL, 0, $2),
945 $2
946 );
947 TOKEN_GETMAD($1,$$,'+');
948 }
891be019 949 | '!' term /* !$x */
f05e27e5
DM
950 { $$ = newUNOP(OP_NOT, 0, scalar($2));
951 TOKEN_GETMAD($1,$$,'o');
952 }
891be019 953 | '~' term /* ~$x */
f05e27e5
DM
954 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
955 TOKEN_GETMAD($1,$$,'o');
956 }
891be019 957 | term POSTINC /* $x++ */
79072805 958 { $$ = newUNOP(OP_POSTINC, 0,
f05e27e5
DM
959 mod(scalar($1), OP_POSTINC));
960 TOKEN_GETMAD($2,$$,'o');
961 }
891be019 962 | term POSTDEC /* $x-- */
79072805 963 { $$ = newUNOP(OP_POSTDEC, 0,
f05e27e5
DM
964 mod(scalar($1), OP_POSTDEC));
965 TOKEN_GETMAD($2,$$,'o');
966 }
891be019 967 | PREINC term /* ++$x */
79072805 968 { $$ = newUNOP(OP_PREINC, 0,
f05e27e5
DM
969 mod(scalar($2), OP_PREINC));
970 TOKEN_GETMAD($1,$$,'o');
971 }
891be019 972 | PREDEC term /* --$x */
79072805 973 { $$ = newUNOP(OP_PREDEC, 0,
f05e27e5
DM
974 mod(scalar($2), OP_PREDEC));
975 TOKEN_GETMAD($1,$$,'o');
976 }
891be019
SC
977
978 ;
979
980/* Constructors for anonymous data */
981anonymous: '[' expr ']'
f05e27e5
DM
982 { $$ = newANONLIST($2);
983 TOKEN_GETMAD($1,$$,'[');
984 TOKEN_GETMAD($3,$$,']');
985 }
891be019 986 | '[' ']'
f05e27e5
DM
987 { $$ = newANONLIST(Nullop);
988 TOKEN_GETMAD($1,$$,'[');
989 TOKEN_GETMAD($2,$$,']');
990 }
891be019 991 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
f05e27e5
DM
992 { $$ = newANONHASH($2);
993 TOKEN_GETMAD($1,$$,'{');
994 TOKEN_GETMAD($3,$$,';');
995 TOKEN_GETMAD($4,$$,'}');
996 }
891be019 997 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
f05e27e5
DM
998 { $$ = newANONHASH(Nullop);
999 TOKEN_GETMAD($1,$$,'{');
1000 TOKEN_GETMAD($2,$$,';');
1001 TOKEN_GETMAD($3,$$,'}');
1002 }
718a7425 1003 | ANONSUB startanonsub proto subattrlist block %prec '('
a8ff2fa6 1004 { SvREFCNT_inc(PL_compcv);
718a7425 1005 $$ = newANONATTRSUB($2, $3, $4, $5);
f05e27e5 1006 TOKEN_GETMAD($1,$$,'o');
718a7425
DM
1007 OP_GETMAD($3,$$,'s');
1008 OP_GETMAD($4,$$,'a');
f05e27e5 1009 }
891be019
SC
1010
1011 ;
1012
1013/* Things called with "do" */
1014termdo : DO term %prec UNIOP /* do $filename */
123d08c9 1015 { $$ = dofile($2, IVAL($1));
f05e27e5
DM
1016 TOKEN_GETMAD($1,$$,'o');
1017 }
891be019 1018 | DO block %prec '(' /* do { code */
f05e27e5
DM
1019 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2));
1020 TOKEN_GETMAD($1,$$,'D');
1021 }
891be019
SC
1022 | DO WORD '(' ')' /* do somesub() */
1023 { $$ = newUNOP(OP_ENTERSUB,
1024 OPf_SPECIAL|OPf_STACKED,
1025 prepend_elem(OP_LIST,
1026 scalar(newCVREF(
1027 (OPpENTERSUB_AMPER<<8),
1028 scalar($2)
f05e27e5
DM
1029 )),Nullop)); dep();
1030 TOKEN_GETMAD($1,$$,'o');
1031 TOKEN_GETMAD($3,$$,'(');
1032 TOKEN_GETMAD($4,$$,')');
1033 }
891be019
SC
1034 | DO WORD '(' expr ')' /* do somesub(@args) */
1035 { $$ = newUNOP(OP_ENTERSUB,
1036 OPf_SPECIAL|OPf_STACKED,
1037 append_elem(OP_LIST,
1038 $4,
1039 scalar(newCVREF(
1040 (OPpENTERSUB_AMPER<<8),
1041 scalar($2)
f05e27e5
DM
1042 )))); dep();
1043 TOKEN_GETMAD($1,$$,'o');
1044 TOKEN_GETMAD($3,$$,'(');
1045 TOKEN_GETMAD($5,$$,')');
1046 }
891be019
SC
1047 | DO scalar '(' ')' /* do $subref () */
1048 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1049 prepend_elem(OP_LIST,
f05e27e5
DM
1050 scalar(newCVREF(0,scalar($2))), Nullop)); dep();
1051 TOKEN_GETMAD($1,$$,'o');
1052 TOKEN_GETMAD($3,$$,'(');
1053 TOKEN_GETMAD($4,$$,')');
1054 }
891be019
SC
1055 | DO scalar '(' expr ')' /* do $subref (@args) */
1056 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1057 prepend_elem(OP_LIST,
1058 $4,
f05e27e5
DM
1059 scalar(newCVREF(0,scalar($2))))); dep();
1060 TOKEN_GETMAD($1,$$,'o');
1061 TOKEN_GETMAD($3,$$,'(');
1062 TOKEN_GETMAD($5,$$,')');
1063 }
891be019
SC
1064
1065 ;
1066
1067term : termbinop
1068 | termunop
1069 | anonymous
1070 | termdo
e9bdcc27 1071 | term '?' term ':' term
f05e27e5
DM
1072 { $$ = newCONDOP(0, $1, $3, $5);
1073 TOKEN_GETMAD($2,$$,'?');
1074 TOKEN_GETMAD($4,$$,':');
1075 }
891be019 1076 | REFGEN term /* \$x, \@y, \%z */
f05e27e5
DM
1077 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN));
1078 TOKEN_GETMAD($1,$$,'o');
1079 }
09bef843
SB
1080 | myattrterm %prec UNIOP
1081 { $$ = $1; }
1082 | LOCAL term %prec UNIOP
f05e27e5
DM
1083 { $$ = localize($2,IVAL($1));
1084 TOKEN_GETMAD($1,$$,'d');
1085 }
a0d0e21e 1086 | '(' expr ')'
f05e27e5
DM
1087 { $$ = sawparens(IF_MAD(newUNOP(OP_NULL,0,$2), $2));
1088 TOKEN_GETMAD($1,$$,'(');
1089 TOKEN_GETMAD($3,$$,')');
1090 }
8d063cd8 1091 | '(' ')'
f05e27e5
DM
1092 { $$ = sawparens(newNULLLIST());
1093 TOKEN_GETMAD($1,$$,'(');
1094 TOKEN_GETMAD($2,$$,')');
1095 }
79072805 1096 | scalar %prec '('
8d063cd8 1097 { $$ = $1; }
79072805 1098 | star %prec '('
8d063cd8 1099 { $$ = $1; }
79072805 1100 | hsh %prec '('
8d063cd8 1101 { $$ = $1; }
79072805 1102 | ary %prec '('
8d063cd8 1103 { $$ = $1; }
891be019 1104 | arylen %prec '(' /* $#x, $#{ something } */
79072805 1105 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
fad39ff1
SM
1106 | subscripted
1107 { $$ = $1; }
891be019 1108 | ary '[' expr ']' /* array slice */
79072805
LW
1109 { $$ = prepend_elem(OP_ASLICE,
1110 newOP(OP_PUSHMARK, 0),
79072805
LW
1111 newLISTOP(OP_ASLICE, 0,
1112 list($3),
f05e27e5
DM
1113 ref($1, OP_ASLICE)));
1114 TOKEN_GETMAD($2,$$,'[');
1115 TOKEN_GETMAD($4,$$,']');
1116 }
891be019 1117 | ary '{' expr ';' '}' /* @hash{@keys} */
79072805
LW
1118 { $$ = prepend_elem(OP_HSLICE,
1119 newOP(OP_PUSHMARK, 0),
79072805
LW
1120 newLISTOP(OP_HSLICE, 0,
1121 list($3),
a0d0e21e 1122 ref(oopsHV($1), OP_HSLICE)));
f05e27e5
DM
1123 PL_expect = XOPERATOR;
1124 TOKEN_GETMAD($2,$$,'{');
1125 TOKEN_GETMAD($4,$$,';');
1126 TOKEN_GETMAD($5,$$,'}');
1127 }
79072805
LW
1128 | THING %prec '('
1129 { $$ = $1; }
891be019 1130 | amper /* &foo; */
c07a80fd 1131 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
ecb2f335 1132 | amper '(' ')' /* &foo() */
f05e27e5
DM
1133 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1134 TOKEN_GETMAD($2,$$,'(');
1135 TOKEN_GETMAD($3,$$,')');
1136 }
891be019 1137 | amper '(' expr ')' /* &foo(@args) */
f05e27e5
DM
1138 {
1139 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1140 append_elem(OP_LIST, $3, scalar($1)));
1141 DO_MAD(
1142 OP* op = $$;
1143 if (op->op_type == OP_CONST) { /* defeat const fold */
1144 op = (OP*)op->op_madprop->mad_val;
1145 }
1146 token_getmad($2,op,'(');
1147 token_getmad($4,op,')');
1148 )
1149 }
891be019 1150 | NOAMP WORD listexpr /* foo(@args) */
a0d0e21e 1151 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
f05e27e5
DM
1152 append_elem(OP_LIST, $3, scalar($2)));
1153 TOKEN_GETMAD($1,$$,'o');
1154 }
891be019 1155 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
f05e27e5
DM
1156 { $$ = newOP(IVAL($1), OPf_SPECIAL);
1157 PL_hints |= HINT_BLOCK_SCOPE;
1158 TOKEN_GETMAD($1,$$,'o');
1159 }
a0d0e21e 1160 | LOOPEX term
f05e27e5
DM
1161 { $$ = newLOOPEX(IVAL($1),$2);
1162 TOKEN_GETMAD($1,$$,'o');
1163 }
891be019 1164 | NOTOP argexpr /* not $foo */
f05e27e5
DM
1165 { $$ = newUNOP(OP_NOT, 0, scalar($2));
1166 TOKEN_GETMAD($1,$$,'o');
1167 }
891be019 1168 | UNIOP /* Unary op, $_ implied */
f05e27e5
DM
1169 { $$ = newOP(IVAL($1), 0);
1170 TOKEN_GETMAD($1,$$,'o');
1171 }
1172 | UNIOP block /* eval { foo }* */
1173 { $$ = newUNOP(IVAL($1), 0, $2);
1174 TOKEN_GETMAD($1,$$,'o');
1175 }
891be019 1176 | UNIOP term /* Unary op */
f05e27e5
DM
1177 { $$ = newUNOP(IVAL($1), 0, $2);
1178 TOKEN_GETMAD($1,$$,'o');
1179 }
1180 | REQUIRE /* require, $_ implied *//* FIMXE for MAD needed? */
a72a1c8b 1181 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); }
f05e27e5 1182 | REQUIRE term /* require Foo *//* FIMXE for MAD needed? */
a72a1c8b 1183 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); }
3cd0a11a
RGS
1184 | UNIOPSUB
1185 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
891be019 1186 | UNIOPSUB term /* Sub treated as unop */
4633a7c4
LW
1187 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1188 append_elem(OP_LIST, $2, scalar($1))); }
891be019 1189 | FUNC0 /* Nullary operator */
f05e27e5
DM
1190 { $$ = newOP(IVAL($1), 0);
1191 TOKEN_GETMAD($1,$$,'o');
1192 }
ae986130 1193 | FUNC0 '(' ')'
f05e27e5
DM
1194 { $$ = newOP(IVAL($1), 0);
1195 TOKEN_GETMAD($1,$$,'o');
1196 TOKEN_GETMAD($2,$$,'(');
1197 TOKEN_GETMAD($3,$$,')');
1198 }
891be019 1199 | FUNC0SUB /* Sub treated as nullop */
28757baa 1200 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
4633a7c4 1201 scalar($1)); }
891be019 1202 | FUNC1 '(' ')' /* not () */
f05e27e5
DM
1203 { $$ = (IVAL($1) == OP_NOT)
1204 ? newUNOP(IVAL($1), 0, newSVOP(OP_CONST, 0, newSViv(0)))
1205 : newOP(IVAL($1), OPf_SPECIAL);
1206
1207 TOKEN_GETMAD($1,$$,'o');
1208 TOKEN_GETMAD($2,$$,'(');
1209 TOKEN_GETMAD($3,$$,')');
1210 }
891be019 1211 | FUNC1 '(' expr ')' /* not($foo) */
f05e27e5
DM
1212 { $$ = newUNOP(IVAL($1), 0, $3);
1213 TOKEN_GETMAD($1,$$,'o');
1214 TOKEN_GETMAD($2,$$,'(');
1215 TOKEN_GETMAD($4,$$,')');
1216 }
1217 | PMFUNC '(' argexpr ')' /* m//, s///, tr/// */
1218 { $$ = pmruntime($1, $3, 1);
1219 TOKEN_GETMAD($2,$$,'(');
1220 TOKEN_GETMAD($4,$$,')');
1221 }
79072805 1222 | WORD
378cc40b 1223 | listop
8d063cd8
LW
1224 ;
1225
891be019 1226/* "my" declarations, with optional attributes */
09bef843 1227myattrterm: MY myterm myattrlist
f05e27e5
DM
1228 { $$ = my_attrs($2,$3);
1229 DO_MAD(
1230 token_getmad($1,$$,'d');
1231 append_madprops($3->op_madprop, $$, 'a');
1232 $3->op_madprop = 0;
1233 )
1234 }
09bef843 1235 | MY myterm
f05e27e5
DM
1236 { $$ = localize($2,IVAL($1));
1237 TOKEN_GETMAD($1,$$,'d');
1238 }
09bef843
SB
1239 ;
1240
891be019 1241/* Things that can be "my"'d */
09bef843 1242myterm : '(' expr ')'
f05e27e5
DM
1243 { $$ = sawparens($2);
1244 TOKEN_GETMAD($1,$$,'(');
1245 TOKEN_GETMAD($3,$$,')');
1246 }
09bef843 1247 | '(' ')'
f05e27e5
DM
1248 { $$ = sawparens(newNULLLIST());
1249 TOKEN_GETMAD($1,$$,'(');
1250 TOKEN_GETMAD($2,$$,')');
1251 }
09bef843
SB
1252 | scalar %prec '('
1253 { $$ = $1; }
1254 | hsh %prec '('
1255 { $$ = $1; }
1256 | ary %prec '('
1257 { $$ = $1; }
1258 ;
1259
891be019 1260/* Basic list expressions */
fad39ff1 1261listexpr: /* NULL */ %prec PREC_LOW
8990e307 1262 { $$ = Nullop; }
fad39ff1 1263 | argexpr %prec PREC_LOW
a0d0e21e
LW
1264 { $$ = $1; }
1265 ;
1266
1267listexprcom: /* NULL */
1268 { $$ = Nullop; }
79072805
LW
1269 | expr
1270 { $$ = $1; }
a0d0e21e 1271 | expr ','
f05e27e5
DM
1272 {
1273#ifdef MAD
1274 OP* op = newNULLLIST();
1275 token_getmad($2,op,',');
1276 $$ = append_elem(OP_LIST, $1, op);
1277#else
1278 $$ = $1;
1279#endif
1280
1281 }
79072805
LW
1282 ;
1283
891be019
SC
1284/* A little bit of trickery to make "for my $foo (@bar)" actually be
1285 lexical */
55497cff 1286my_scalar: scalar
3280af22 1287 { PL_in_my = 0; $$ = my($1); }
55497cff
PP
1288 ;
1289
79072805 1290amper : '&' indirob
f05e27e5
DM
1291 { $$ = newCVREF(IVAL($1),$2);
1292 TOKEN_GETMAD($1,$$,'&');
1293 }
a687059c
LW
1294 ;
1295
79072805 1296scalar : '$' indirob
f05e27e5
DM
1297 { $$ = newSVREF($2);
1298 TOKEN_GETMAD($1,$$,'$');
1299 }
a687059c
LW
1300 ;
1301
79072805 1302ary : '@' indirob
f05e27e5
DM
1303 { $$ = newAVREF($2);
1304 TOKEN_GETMAD($1,$$,'@');
1305 }
79072805
LW
1306 ;
1307
1308hsh : '%' indirob
f05e27e5
DM
1309 { $$ = newHVREF($2);
1310 TOKEN_GETMAD($1,$$,'%');
1311 }
79072805
LW
1312 ;
1313
1314arylen : DOLSHARP indirob
f05e27e5
DM
1315 { $$ = newAVREF($2);
1316 TOKEN_GETMAD($1,$$,'l');
1317 }
79072805
LW
1318 ;
1319
1320star : '*' indirob
f05e27e5
DM
1321 { $$ = newGVREF(0,$2);
1322 TOKEN_GETMAD($1,$$,'*');
1323 }
79072805
LW
1324 ;
1325
891be019 1326/* Indirect objects */
79072805
LW
1327indirob : WORD
1328 { $$ = scalar($1); }
fad39ff1 1329 | scalar %prec PREC_LOW
f05e27e5 1330 { $$ = scalar($1); }
79072805 1331 | block
a0d0e21e 1332 { $$ = scope($1); }
79072805 1333
93a17b20
LW
1334 | PRIVATEREF
1335 { $$ = $1; }
8d063cd8 1336 ;