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