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