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