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