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