This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
make op.c:S_alloc_LOGOP() non-static
[perl5.git] / perly.y
CommitLineData
a0d0e21e 1/* perly.y
a687059c 2 *
f05e27e5 3 * Copyright (c) 1991-2002, 2003, 2004, 2005, 2006 Larry Wall
2eee27d7 4 * Copyright (c) 2007, 2008, 2009, 2010, 2011 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 *
23 * The main job of of this grammar is to call the various newFOO()
24 * functions in op.c to build a syntax tree of OP structs.
61296642 25 * It relies on the lexer in toke.c to do the tokenizing.
29522234
DM
26 *
27 * Note: due to the way that the cleanup code works WRT to freeing ops on
28 * the parse stack, it is dangerous to assign to the $n variables within
29 * an action.
166f8a29
DM
30 */
31
0de566d7 32/* Make the parser re-entrant. */
8d063cd8 33
f39ff1f3 34%pure-parser
8d063cd8 35
28ac2b49 36%start grammar
9d116dd7 37
8d063cd8 38%union {
d5c6462e
DM
39 I32 ival; /* __DEFAULT__ (marker for regen_perly.pl;
40 must always be 1st union member) */
79072805
LW
41 char *pval;
42 OP *opval;
43 GV *gvval;
8d063cd8
LW
44}
45
78cdf107 46%token <ival> GRAMPROG GRAMEXPR GRAMBLOCK GRAMBARESTMT GRAMFULLSTMT GRAMSTMTSEQ
28ac2b49 47
f10ebf6c 48%token <ival> '{' '}' '[' ']' '-' '+' '@' '%' '&' '=' '.'
f0fcb552 49
185c2e96 50%token <opval> BAREWORD METHOD FUNCMETH THING PMFUNC PRIVATEREF QWLIST
7eb971ee 51%token <opval> FUNC0OP FUNC0SUB UNIOPSUB LSTOPSUB
88e1f1a2 52%token <opval> PLUGEXPR PLUGSTMT
b5bbe64a
JH
53%token <pval> LABEL
54%token <ival> FORMAT SUB ANONSUB PACKAGE USE
55%token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
56%token <ival> GIVEN WHEN DEFAULT
57%token <ival> LOOPEX DOTDOT YADAYADA
58%token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP
59%token <ival> RELOP EQOP MULOP ADDOP
60%token <ival> DOLSHARP DO HASHBRACK NOAMP
61%token <ival> LOCAL MY REQUIRE
62%token <ival> COLONATTR FORMLBRACK FORMRBRACK
f05e27e5 63
727a8fe5 64%type <ival> grammar remember mremember
f05e27e5 65%type <ival> startsub startanonsub startformsub
b5bbe64a 66
b5a64814 67%type <ival> mintro
f05e27e5 68
8e720305 69%type <opval> stmtseq fullstmt labfullstmt barestmt block mblock else
fad39ff1 70%type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
76eba8ab 71%type <opval> sliceme kvslice gelem
08b3e84f 72%type <opval> listexpr nexpr texpr iexpr mexpr mnexpr
9b6b7be8 73%type <opval> optlistexpr optexpr optrepl indirob listop method
d39c26a6
FC
74%type <opval> formname subname proto optsubbody cont my_scalar my_var
75%type <opval> refgen_topic formblock
f05e27e5 76%type <opval> subattrlist myattrlist myattrterm myterm
d3d9da4a
DM
77%type <opval> termbinop termunop anonymous termdo
78%type <ival> sigslurpsigil
79%type <opval> sigvarname sigdefault sigscalarelem sigslurpelem
80%type <opval> sigelem siglist siglistornull subsignature
705fe0e5 81%type <opval> formstmtseq formline formarg
79072805 82
b5bbe64a 83%nonassoc <ival> PREC_LOW
fad39ff1
SM
84%nonassoc LOOPEX
85
b5bbe64a
JH
86%left <ival> OROP DOROP
87%left <ival> ANDOP
88%right <ival> NOTOP
36477c24 89%nonassoc LSTOP LSTOPSUB
b5bbe64a
JH
90%left <ival> ','
91%right <ival> ASSIGNOP
92%right <ival> '?' ':'
be25f609 93%nonassoc DOTDOT YADAYADA
b5bbe64a
JH
94%left <ival> OROR DORDOR
95%left <ival> ANDAND
96%left <ival> BITOROP
97%left <ival> BITANDOP
a687059c
LW
98%nonassoc EQOP
99%nonassoc RELOP
36477c24 100%nonassoc UNIOP UNIOPSUB
20515881 101%nonassoc REQUIRE
b5bbe64a 102%left <ival> SHIFTOP
a687059c
LW
103%left ADDOP
104%left MULOP
b5bbe64a
JH
105%left <ival> MATCHOP
106%right <ival> '!' '~' UMINUS REFGEN
107%right <ival> POWOP
108%nonassoc <ival> PREINC PREDEC POSTINC POSTDEC POSTJOIN
109%left <ival> ARROW
110%nonassoc <ival> ')'
111%left <ival> '('
fad39ff1 112%left '[' '{'
8d063cd8
LW
113
114%% /* RULES */
115
28ac2b49 116/* Top-level choice of what kind of thing yyparse was called to parse */
727a8fe5
Z
117grammar : GRAMPROG
118 {
624fa8bd 119 parser->expect = XSTATE;
727a8fe5
Z
120 }
121 remember stmtseq
122 {
123 newPROG(block_end($3,$4));
8635e3c2 124 PL_compiling.cop_seq = 0;
727a8fe5
Z
125 $$ = 0;
126 }
78cdf107
Z
127 | GRAMEXPR
128 {
129 parser->expect = XTERM;
130 }
09b1cffe 131 optexpr
78cdf107
Z
132 {
133 PL_eval_root = $3;
134 $$ = 0;
135 }
e53d8f76
Z
136 | GRAMBLOCK
137 {
138 parser->expect = XBLOCK;
139 }
140 block
141 {
5f211341 142 PL_pad_reset_pending = TRUE;
8359b381
Z
143 PL_eval_root = $3;
144 $$ = 0;
145 yyunlex();
146 parser->yychar = YYEOF;
147 }
148 | GRAMBARESTMT
149 {
150 parser->expect = XSTATE;
151 }
152 barestmt
153 {
154 PL_pad_reset_pending = TRUE;
e53d8f76
Z
155 PL_eval_root = $3;
156 $$ = 0;
157 yyunlex();
158 parser->yychar = YYEOF;
159 }
9eb5c532 160 | GRAMFULLSTMT
28ac2b49 161 {
9eb5c532
Z
162 parser->expect = XSTATE;
163 }
164 fullstmt
165 {
5f211341 166 PL_pad_reset_pending = TRUE;
9eb5c532 167 PL_eval_root = $3;
28ac2b49
Z
168 $$ = 0;
169 yyunlex();
170 parser->yychar = YYEOF;
171 }
07ffcb73
Z
172 | GRAMSTMTSEQ
173 {
174 parser->expect = XSTATE;
175 }
5f211341 176 stmtseq
07ffcb73
Z
177 {
178 PL_eval_root = $3;
179 $$ = 0;
180 }
181 ;
182
891be019 183/* An ordinary block */
5f211341 184block : '{' remember stmtseq '}'
624fa8bd
FC
185 { if (parser->copline > (line_t)$1)
186 parser->copline = (line_t)$1;
f05e27e5 187 $$ = block_end($2, $3);
f05e27e5 188 }
a0d0e21e
LW
189 ;
190
7c70caa5 191/* format body */
705fe0e5 192formblock: '=' remember ';' FORMRBRACK formstmtseq ';' '.'
624fa8bd
FC
193 { if (parser->copline > (line_t)$1)
194 parser->copline = (line_t)$1;
705fe0e5 195 $$ = block_end($2, $5);
7c70caa5
FC
196 }
197 ;
198
55497cff 199remember: /* NULL */ /* start a full lexical scope */
34b54951
FC
200 { $$ = block_start(TRUE);
201 parser->parsed_sub = 0; }
55497cff
PP
202 ;
203
5f211341 204mblock : '{' mremember stmtseq '}'
624fa8bd
FC
205 { if (parser->copline > (line_t)$1)
206 parser->copline = (line_t)$1;
f05e27e5 207 $$ = block_end($2, $3);
f05e27e5 208 }
55497cff
PP
209 ;
210
211mremember: /* NULL */ /* start a partial lexical scope */
34b54951
FC
212 { $$ = block_start(FALSE);
213 parser->parsed_sub = 0; }
8d063cd8
LW
214 ;
215
eae48c89 216/* A sequence of statements in the program */
5f211341 217stmtseq : /* NULL */
bcabcc50 218 { $$ = (OP*)NULL; }
5f211341 219 | stmtseq fullstmt
2fcb4757 220 { $$ = op_append_list(OP_LINESEQ, $1, $2);
3280af22 221 PL_pad_reset_pending = TRUE;
503de470
DM
222 if ($1 && $2)
223 PL_hints |= HINT_BLOCK_SCOPE;
224 }
8d063cd8
LW
225 ;
226
705fe0e5
FC
227/* A sequence of format lines */
228formstmtseq: /* NULL */
229 { $$ = (OP*)NULL; }
230 | formstmtseq formline
231 { $$ = op_append_list(OP_LINESEQ, $1, $2);
232 PL_pad_reset_pending = TRUE;
233 if ($1 && $2)
234 PL_hints |= HINT_BLOCK_SCOPE;
235 }
236 ;
237
8e720305
Z
238/* A statement in the program, including optional labels */
239fullstmt: barestmt
eae48c89 240 {
b5bbe64a 241 $$ = $1 ? newSTATEOP(0, NULL, $1) : NULL;
eae48c89 242 }
8e720305
Z
243 | labfullstmt
244 { $$ = $1; }
245 ;
246
247labfullstmt: LABEL barestmt
248 {
b5bbe64a 249 $$ = newSTATEOP(SVf_UTF8 * $1[strlen($1)+1], $1, $2);
8e720305
Z
250 }
251 | LABEL labfullstmt
252 {
b5bbe64a 253 $$ = newSTATEOP(SVf_UTF8 * $1[strlen($1)+1], $1, $2);
8e720305 254 }
eae48c89
Z
255 ;
256
257/* A bare statement, lacking label and other aspects of state op */
258barestmt: PLUGSTMT
0d863452 259 { $$ = $1; }
7c70caa5 260 | FORMAT startformsub formname formblock
f05e27e5 261 {
eae48c89 262 CV *fmtcv = PL_compcv;
eae48c89
Z
263 newFORM($2, $3, $4);
264 $$ = (OP*)NULL;
4a273b91 265 if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) {
74a9453a 266 pad_add_weakref(fmtcv);
f05e27e5 267 }
8b9fb2f9 268 parser->parsed_sub = 1;
eae48c89 269 }
50278755
FC
270 | SUB subname startsub
271 {
272 if ($2->op_type == OP_CONST) {
273 const char *const name =
274 SvPV_nolen_const(((SVOP*)$2)->op_sv);
275 if (strEQ(name, "BEGIN") || strEQ(name, "END")
764212cf
FC
276 || strEQ(name, "INIT") || strEQ(name, "CHECK")
277 || strEQ(name, "UNITCHECK"))
278 CvSPECIAL_on(PL_compcv);
50278755 279 }
e07561e6
FC
280 else
281 /* State subs inside anonymous subs need to be
282 clonable themselves. */
6d5c2147
FC
283 if (CvANON(CvOUTSIDE(PL_compcv))
284 || CvCLONE(CvOUTSIDE(PL_compcv))
285 || !PadnameIsSTATE(PadlistNAMESARRAY(CvPADLIST(
286 CvOUTSIDE(PL_compcv)
287 ))[$2->op_targ]))
e07561e6 288 CvCLONE_on(PL_compcv);
624fa8bd
FC
289 parser->in_my = 0;
290 parser->in_my_stash = NULL;
764212cf 291 }
30d9c59b 292 proto subattrlist optsubbody
eae48c89
Z
293 {
294 SvREFCNT_inc_simple_void(PL_compcv);
50278755
FC
295 $2->op_type == OP_CONST
296 ? newATTRSUB($3, $2, $5, $6, $7)
297 : newMYSUB($3, $2, $5, $6, $7)
50278755 298 ;
eae48c89 299 $$ = (OP*)NULL;
764212cf 300 intro_my();
34b54951 301 parser->parsed_sub = 1;
f05e27e5 302 }
abcf453d
PM
303 | SUB subname startsub
304 {
305 if ($2->op_type == OP_CONST) {
306 const char *const name =
307 SvPV_nolen_const(((SVOP*)$2)->op_sv);
308 if (strEQ(name, "BEGIN") || strEQ(name, "END")
309 || strEQ(name, "INIT") || strEQ(name, "CHECK")
310 || strEQ(name, "UNITCHECK"))
311 CvSPECIAL_on(PL_compcv);
312 }
313 else
314 /* State subs inside anonymous subs need to be
315 clonable themselves. */
316 if (CvANON(CvOUTSIDE(PL_compcv))
317 || CvCLONE(CvOUTSIDE(PL_compcv))
318 || !PadnameIsSTATE(PadlistNAMESARRAY(CvPADLIST(
319 CvOUTSIDE(PL_compcv)
320 ))[$2->op_targ]))
321 CvCLONE_on(PL_compcv);
322 parser->in_my = 0;
323 parser->in_my_stash = NULL;
324 }
325 remember subsignature subattrlist '{' stmtseq '}'
326 {
327 OP *body;
328 if (parser->copline > (line_t)$8)
329 parser->copline = (line_t)$8;
330 body = block_end($5,
331 op_append_list(OP_LINESEQ, $6, $9));
332
333 SvREFCNT_inc_simple_void(PL_compcv);
334 $2->op_type == OP_CONST
335 ? newATTRSUB($3, $2, NULL, $7, body)
336 : newMYSUB($3, $2, NULL, $7, body)
337 ;
338 $$ = (OP*)NULL;
339 intro_my();
340 parser->parsed_sub = 1;
341 }
185c2e96 342 | PACKAGE BAREWORD BAREWORD ';'
5f211341 343 {
eae48c89
Z
344 package($3);
345 if ($2)
346 package_version($2);
347 $$ = (OP*)NULL;
eae48c89
Z
348 }
349 | USE startsub
350 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
185c2e96 351 BAREWORD BAREWORD optlistexpr ';'
eae48c89
Z
352 {
353 SvREFCNT_inc_simple_void(PL_compcv);
b5bbe64a 354 utilize($1, $2, $4, $5, $6);
34b54951 355 parser->parsed_sub = 1;
eae48c89 356 $$ = (OP*)NULL;
eae48c89 357 }
417a992d 358 | IF '(' remember mexpr ')' mblock else
eae48c89 359 {
3ad73efd
Z
360 $$ = block_end($3,
361 newCONDOP(0, $4, op_scope($6), $7));
624fa8bd 362 parser->copline = (line_t)$1;
eae48c89 363 }
08b3e84f 364 | UNLESS '(' remember mexpr ')' mblock else
eae48c89 365 {
3ad73efd 366 $$ = block_end($3,
08b3e84f 367 newCONDOP(0, $4, $7, op_scope($6)));
624fa8bd 368 parser->copline = (line_t)$1;
eae48c89 369 }
b5a64814 370 | GIVEN '(' remember mexpr ')' mblock
eae48c89 371 {
5d051ee0 372 $$ = block_end($3, newGIVENOP($4, op_scope($6), 0));
624fa8bd 373 parser->copline = (line_t)$1;
eae48c89 374 }
417a992d 375 | WHEN '(' remember mexpr ')' mblock
3ad73efd 376 { $$ = block_end($3, newWHENOP($4, op_scope($6))); }
eae48c89 377 | DEFAULT block
3ad73efd 378 { $$ = newWHENOP(0, op_scope($2)); }
417a992d 379 | WHILE '(' remember texpr ')' mintro mblock cont
eae48c89
Z
380 {
381 $$ = block_end($3,
382 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
94bf0465 383 $4, $7, $8, $6));
624fa8bd 384 parser->copline = (line_t)$1;
eae48c89 385 }
417a992d 386 | UNTIL '(' remember iexpr ')' mintro mblock cont
eae48c89
Z
387 {
388 $$ = block_end($3,
94bf0465
Z
389 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
390 $4, $7, $8, $6));
624fa8bd 391 parser->copline = (line_t)$1;
eae48c89 392 }
2d0e3c96
FC
393 | FOR '(' remember mnexpr ';'
394 { parser->expect = XTERM; }
395 texpr ';'
396 { parser->expect = XTERM; }
397 mintro mnexpr ')'
eae48c89
Z
398 mblock
399 {
b5bbe64a 400 OP *initop = $4;
eae48c89 401 OP *forop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
2d0e3c96 402 scalar($7), $13, $11, $10);
eae48c89
Z
403 if (initop) {
404 forop = op_prepend_elem(OP_LINESEQ, initop,
405 op_append_elem(OP_LINESEQ,
406 newOP(OP_UNSTACK, OPf_SPECIAL),
407 forop));
5f211341 408 }
0f602692 409 PL_hints |= HINT_BLOCK_SCOPE;
eae48c89 410 $$ = block_end($3, forop);
624fa8bd 411 parser->copline = (line_t)$1;
eae48c89 412 }
417a992d 413 | FOR MY remember my_scalar '(' mexpr ')' mblock cont
eae48c89 414 {
94bf0465 415 $$ = block_end($3, newFOROP(0, $4, $6, $8, $9));
624fa8bd 416 parser->copline = (line_t)$1;
eae48c89 417 }
417a992d 418 | FOR scalar '(' remember mexpr ')' mblock cont
eae48c89 419 {
94bf0465 420 $$ = block_end($4, newFOROP(0,
3ad73efd 421 op_lvalue($2, OP_ENTERLOOP), $5, $7, $8));
624fa8bd 422 parser->copline = (line_t)$1;
eae48c89 423 }
e118fea3
FC
424 | FOR my_refgen remember my_var
425 { parser->in_my = 0; $<opval>$ = my($4); }
d39c26a6
FC
426 '(' mexpr ')' mblock cont
427 {
428 $$ = block_end(
e118fea3 429 $3,
d39c26a6
FC
430 newFOROP(0,
431 op_lvalue(
432 newUNOP(OP_REFGEN, 0,
e118fea3 433 $<opval>5),
d39c26a6 434 OP_ENTERLOOP),
e118fea3 435 $7, $9, $10)
d39c26a6
FC
436 );
437 parser->copline = (line_t)$1;
438 }
439 | FOR REFGEN refgen_topic '(' remember mexpr ')' mblock cont
440 {
441 $$ = block_end($5, newFOROP(
442 0, op_lvalue(newUNOP(OP_REFGEN, 0,
96801525 443 $3),
d39c26a6
FC
444 OP_ENTERLOOP), $6, $8, $9));
445 parser->copline = (line_t)$1;
446 }
417a992d 447 | FOR '(' remember mexpr ')' mblock cont
eae48c89
Z
448 {
449 $$ = block_end($3,
94bf0465 450 newFOROP(0, (OP*)NULL, $4, $6, $7));
624fa8bd 451 parser->copline = (line_t)$1;
eae48c89
Z
452 }
453 | block cont
454 {
455 /* a block is a loop that happens once */
456 $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
94bf0465 457 (OP*)NULL, $1, $2, 0);
eae48c89 458 }
185c2e96 459 | PACKAGE BAREWORD BAREWORD '{' remember
eae48c89 460 {
eae48c89 461 package($3);
eae48c89 462 if ($2) {
eae48c89 463 package_version($2);
eae48c89
Z
464 }
465 }
466 stmtseq '}'
467 {
468 /* a block is a loop that happens once */
94bf0465 469 $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
eae48c89 470 (OP*)NULL, block_end($5, $7), (OP*)NULL, 0);
624fa8bd
FC
471 if (parser->copline > (line_t)$4)
472 parser->copline = (line_t)$4;
eae48c89
Z
473 }
474 | sideff ';'
475 {
eae48c89 476 $$ = $1;
eae48c89
Z
477 }
478 | ';'
479 {
b5bbe64a 480 $$ = (OP*)NULL;
624fa8bd 481 parser->copline = NOLINE;
5f211341 482 }
8d063cd8
LW
483 ;
484
705fe0e5
FC
485/* Format line */
486formline: THING formarg
487 { OP *list;
488 if ($2) {
489 OP *term = $2;
705fe0e5
FC
490 list = op_append_elem(OP_LIST, $1, term);
491 }
492 else {
705fe0e5 493 list = $1;
705fe0e5 494 }
624fa8bd
FC
495 if (parser->copline == NOLINE)
496 parser->copline = CopLINE(PL_curcop)-1;
497 else parser->copline--;
705fe0e5 498 $$ = newSTATEOP(0, NULL,
03d05f6e 499 op_convert_list(OP_FORMLINE, 0, list));
705fe0e5
FC
500 }
501 ;
502
503formarg : /* NULL */
504 { $$ = NULL; }
505 | FORMLBRACK stmtseq FORMRBRACK
506 { $$ = op_unscope($2); }
507 ;
508
891be019 509/* An expression which may have a side-effect */
a687059c 510sideff : error
bcabcc50 511 { $$ = (OP*)NULL; }
a687059c 512 | expr
79072805 513 { $$ = $1; }
a687059c 514 | expr IF expr
b5bbe64a 515 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
a687059c 516 | expr UNLESS expr
b5bbe64a 517 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
a687059c 518 | expr WHILE expr
b5bbe64a 519 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
55497cff 520 | expr UNTIL iexpr
b5bbe64a 521 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1); }
ecca16b0 522 | expr FOR expr
94bf0465 523 { $$ = newFOROP(0, (OP*)NULL, $3, $1, (OP*)NULL);
624fa8bd 524 parser->copline = (line_t)$2; }
f20dcd76 525 | expr WHEN expr
3ad73efd 526 { $$ = newWHENOP($3, op_scope($1)); }
79072805
LW
527 ;
528
891be019 529/* else and elsif blocks */
79072805 530else : /* NULL */
bcabcc50 531 { $$ = (OP*)NULL; }
55497cff 532 | ELSE mblock
3ad73efd
Z
533 {
534 ($2)->op_flags |= OPf_PARENS;
535 $$ = op_scope($2);
f05e27e5 536 }
417a992d 537 | ELSIF '(' mexpr ')' mblock else
624fa8bd 538 { parser->copline = (line_t)$1;
3ad73efd
Z
539 $$ = newCONDOP(0,
540 newSTATEOP(OPf_SPECIAL,NULL,$3),
541 op_scope($5), $6);
542 PL_hints |= HINT_BLOCK_SCOPE;
f05e27e5 543 }
79072805
LW
544 ;
545
891be019 546/* Continue blocks */
79072805 547cont : /* NULL */
bcabcc50 548 { $$ = (OP*)NULL; }
79072805 549 | CONTINUE block
b5bbe64a 550 { $$ = op_scope($2); }
79072805
LW
551 ;
552
a034e688
DM
553/* determine whether there are any new my declarations */
554mintro : /* NULL */
555 { $$ = (PL_min_intro_pending &&
556 PL_max_intro_pending >= PL_min_intro_pending);
557 intro_my(); }
558
891be019 559/* Normal expression */
8d063cd8 560nexpr : /* NULL */
bcabcc50 561 { $$ = (OP*)NULL; }
8d063cd8
LW
562 | sideff
563 ;
564
891be019 565/* Boolean expression */
8d063cd8 566texpr : /* NULL means true */
f05e27e5
DM
567 { YYSTYPE tmplval;
568 (void)scan_num("1", &tmplval);
569 $$ = tmplval.opval; }
8d063cd8
LW
570 | expr
571 ;
572
891be019 573/* Inverted boolean expression */
55497cff
PP
574iexpr : expr
575 { $$ = invert(scalar($1)); }
576 ;
577
891be019 578/* Expression with its own lexical scope */
55497cff 579mexpr : expr
bbce6d69
PP
580 { $$ = $1; intro_my(); }
581 ;
582
583mnexpr : nexpr
584 { $$ = $1; intro_my(); }
55497cff
PP
585 ;
586
185c2e96 587formname: BAREWORD { $$ = $1; }
bcabcc50 588 | /* NULL */ { $$ = (OP*)NULL; }
8d063cd8
LW
589 ;
590
fa83b5b6 591startsub: /* NULL */ /* start a regular subroutine scope */
a8ff2fa6
DM
592 { $$ = start_subparse(FALSE, 0);
593 SAVEFREESV(PL_compcv); }
f05e27e5 594
28757baa
PP
595 ;
596
597startanonsub: /* NULL */ /* start an anonymous subroutine scope */
a8ff2fa6
DM
598 { $$ = start_subparse(FALSE, CVf_ANON);
599 SAVEFREESV(PL_compcv); }
28757baa
PP
600 ;
601
44a8e56a 602startformsub: /* NULL */ /* start a format subroutine scope */
a8ff2fa6
DM
603 { $$ = start_subparse(TRUE, 0);
604 SAVEFREESV(PL_compcv); }
44a8e56a
PP
605 ;
606
891be019 607/* Name of a subroutine - must be a bareword, could be special */
185c2e96 608subname : BAREWORD
4210d3f1 609 | PRIVATEREF
a0d0e21e
LW
610 ;
611
891be019 612/* Subroutine prototype */
4633a7c4 613proto : /* NULL */
bcabcc50 614 { $$ = (OP*)NULL; }
4633a7c4
LW
615 | THING
616 ;
28757baa 617
891be019 618/* Optional list of subroutine attributes */
09bef843 619subattrlist: /* NULL */
bcabcc50 620 { $$ = (OP*)NULL; }
09bef843 621 | COLONATTR THING
b5bbe64a 622 { $$ = $2; }
09bef843 623 | COLONATTR
b5bbe64a 624 { $$ = (OP*)NULL; }
09bef843
SB
625 ;
626
891be019 627/* List of attributes for a "my" variable declaration */
09bef843 628myattrlist: COLONATTR THING
b5bbe64a 629 { $$ = $2; }
09bef843 630 | COLONATTR
b5bbe64a 631 { $$ = (OP*)NULL; }
09bef843
SB
632 ;
633
abcf453d 634
d3d9da4a
DM
635
636/* --------------------------------------
637 * subroutine signature parsing
638 */
639
640/* the '' or 'foo' part of a '$' or '@foo' etc signature variable */
641sigvarname: /* NULL */
642 { $$ = (OP*)NULL; }
643 | PRIVATEREF
644 {
645 $$ = $1;
646 PL_parser->in_my = 0;
647 }
648 ;
649
650sigslurpsigil:
651 '@'
652 { $$ = '@'; }
653 | '%'
654 { $$ = '%'; }
655
656/* @, %, @foo, %foo */
657sigslurpelem: sigslurpsigil sigvarname sigdefault/* def only to catch errors */
658 {
659 I32 sigil = $1;
660 OP *var = $2;
661 OP *defexpr = $3;
662 int type = (sigil == '@' ? OP_PADAV : OP_PADHV);
663
664 if (PL_parser->sig_slurpy)
665 yyerror("Multiple slurpy parameters not allowed");
666 PL_parser->sig_slurpy = sigil;
667
668 if (defexpr)
669 yyerror("a slurpy parameter may not have "
670 "a default value");
671
672 if (var) {
673 OP *slice;
674
675 var->op_type = type;
676 var->op_ppaddr = PL_ppaddr[type];
677 var->op_flags = (OPf_WANT_LIST | OPf_MOD);
678 var->op_private = OPpLVAL_INTRO;
679
680 slice = PL_parser->sig_elems
681 ? op_prepend_elem(OP_ASLICE,
682 newOP(OP_PUSHMARK, 0),
683 newLISTOP(OP_ASLICE, 0,
684 list(newRANGE(0,
685 newSVOP(OP_CONST, 0,
686 newSViv(PL_parser->sig_elems)),
687 newUNOP(OP_AV2ARYLEN, 0,
688 ref(newUNOP(OP_RV2AV, 0,
689 newGVOP(OP_GV, 0, PL_defgv)),
690 OP_AV2ARYLEN)))),
691 ref(newUNOP(OP_RV2AV, 0,
692 newGVOP(OP_GV, 0, PL_defgv)),
693 OP_ASLICE)))
694 : newUNOP(OP_RV2AV, 0, newGVOP(OP_GV, 0, PL_defgv));
695 $$ = newSTATEOP(0, NULL,
696 newASSIGNOP(OPf_STACKED, var, 0, slice));
697 }
698 else
699 $$ = (OP*)NULL;
700 }
701 ;
702
703/* default part of sub signature scalar element: i.e. '= default_expr' */
704sigdefault: /* NULL */
705 { $$ = (OP*)NULL; }
706 | ASSIGNOP
707 { $$ = newOP(OP_NULL, 0); }
708 | ASSIGNOP term
709 { $$ = $2; }
710
711
712/* subroutine signature scalar element: e.g. '$x', '$=', '$x = $default' */
713sigscalarelem:
714 '$' sigvarname sigdefault
715 {
716 OP *var = $2;
717 OP *defexpr = $3;
718 OP *argn = NULL;
719 OP *expr = NULL;
720
721 if (PL_parser->sig_slurpy)
722 yyerror("Slurpy parameter not last");
723
724 PL_parser->sig_elems++;
725
726 if (var) {
727 var->op_type = OP_PADSV;
728 var->op_ppaddr = PL_ppaddr[OP_PADSV];
729 var->op_flags = (OPf_WANT_SCALAR | OPf_MOD);
730 var->op_private = OPpLVAL_INTRO;
731 }
732
733 /* $_[N] */
734 argn = newBINOP(OP_AELEM, 0,
735 ref(newUNOP(OP_RV2AV, 0,
736 newGVOP(OP_GV, 0, PL_defgv)),
737 OP_RV2AV),
738 newSVOP(OP_CONST, 0,
739 newSViv(PL_parser->sig_elems - 1)));
740
741 if (defexpr) {
742 PL_parser->sig_optelems++;
743 /* is it '$var=undef', '$=' ? */
744 if ( ( defexpr->op_type == OP_NULL
745 || defexpr->op_type == OP_UNDEF)
746 && !(defexpr->op_flags & OPf_KIDS))
747 {
748 if (var) {
749 /* '$=' is legal, '$var=' isn't */
750 if (defexpr->op_type == OP_NULL)
751 yyerror("Optional parameter "
752 "lacks default expression");
753 else
754 expr = argn;
755 }
756 op_free(defexpr);
757 }
758 else {
759 /* @_ >= N */
760 OP *ge_op =
761 newBINOP(OP_GE, 0,
762 scalar(newUNOP(OP_RV2AV, 0,
763 newGVOP(OP_GV, 0, PL_defgv))),
764 newSVOP(OP_CONST, 0,
765 newSViv(PL_parser->sig_elems)));
766
767 expr = var
768 ? newCONDOP(0, ge_op, argn, defexpr)
769 : newLOGOP(OP_OR, 0, ge_op, defexpr);
770 }
771 }
772 else {
773 if (PL_parser->sig_optelems)
774 yyerror("Mandatory parameter "
775 "follows optional parameter");
776 expr = argn;
777 }
778
779 if (var)
780 expr = newASSIGNOP(OPf_STACKED, var, 0, expr);
781 if (expr)
782 $$ = op_prepend_elem(OP_LINESEQ,
783 newSTATEOP(0, NULL, NULL),
784 expr);
785 else
786 $$ = (OP*)NULL;
787 }
788 ;
789
790
791/* subroutine signature element: e.g. '$x = $default' or '%h' */
792sigelem: sigscalarelem
793 { parser->expect = XSIGVAR; $$ = $1; }
794 | sigslurpelem
795 { parser->expect = XSIGVAR; $$ = $1; }
796 ;
797
798/* list of subroutine signature elements */
799siglist:
800 siglist ','
801 { $$ = $1; }
802 | siglist ',' sigelem
30d9c59b 803 {
d3d9da4a 804 $$ = op_append_list(OP_LINESEQ, $1, $3);
30d9c59b 805 }
d3d9da4a
DM
806 | sigelem %prec PREC_LOW
807 { $$ = $1; }
30d9c59b
Z
808 ;
809
d3d9da4a
DM
810/* () or (....) */
811siglistornull: /* NULL */
812 { $$ = (OP*)NULL; }
813 | siglist
814 { $$ = $1; }
815
816/* Subroutine signature */
817subsignature: '('
818 {
819 ENTER;
820 SAVEINT(PL_parser->sig_elems);
821 SAVEINT(PL_parser->sig_optelems);
822 SAVEI8(PL_parser->sig_slurpy);
823 PL_parser->sig_elems = 0;
824 PL_parser->sig_optelems = 0;
825 PL_parser->sig_slurpy = 0;
826 parser->expect = XSIGVAR;
827 }
828 siglistornull
829 ')'
830 {
831 OP *sigops = $3;
832 int min_arity =
833 PL_parser->sig_elems - PL_parser->sig_optelems;
834
835 assert(FEATURE_SIGNATURES_IS_ENABLED);
836
837 /* We shouldn't get here otherwise */
838 Perl_ck_warner_d(aTHX_
839 packWARN(WARN_EXPERIMENTAL__SIGNATURES),
840 "The signatures feature is experimental");
841
842 /* handle odd/even for %foo */
843 if (PL_parser->sig_slurpy == '%') {
844 OP *chkop =
845 newLOGOP(
846 (PL_parser->sig_elems & 1)
847 ? OP_OR : OP_AND,
848 0,
849 newBINOP(OP_BIT_AND, 0,
850 scalar(newUNOP(OP_RV2AV, 0,
851 newGVOP(OP_GV, 0, PL_defgv))),
852 newSVOP(OP_CONST, 0, newSViv(1))),
853 op_convert_list(OP_DIE, 0,
854 op_convert_list(OP_SPRINTF, 0,
855 op_append_list(OP_LIST,
856 newSVOP(OP_CONST, 0,
857 newSVpvs("Odd name/value argument for subroutine at %s line %d.\n")),
858 newSLICEOP(0,
859 op_append_list(OP_LIST,
860 newSVOP(OP_CONST, 0, newSViv(1)),
861 newSVOP(OP_CONST, 0, newSViv(2))),
862 newOP(OP_CALLER, 0))))));
863 if (PL_parser->sig_optelems)
864 chkop = newLOGOP(OP_AND, 0,
865 newBINOP(OP_GT, 0,
866 scalar(newUNOP(OP_RV2AV, 0,
867 newGVOP(OP_GV, 0, PL_defgv))),
868 newSVOP(OP_CONST, 0,
869 newSViv(PL_parser->sig_elems))),
870 chkop);
871 sigops = op_prepend_elem(OP_LINESEQ,
872 chkop, sigops);
873
874 }
875 if (min_arity)
876 sigops = op_prepend_elem(OP_LINESEQ,
877 Perl_check_arity(aTHX_ min_arity,
878 FALSE),
879 sigops);
880 if (!PL_parser->sig_slurpy)
881 sigops = op_prepend_elem(OP_LINESEQ,
882 Perl_check_arity(aTHX_
883 PL_parser->sig_elems, TRUE),
884 sigops);
885
886 $$ = op_append_elem(OP_LINESEQ, sigops,
887 newSTATEOP(0, NULL, NULL));
888
889 parser->expect = XATTRBLOCK;
890 LEAVE;
891 }
892 ;
893
894
895
30d9c59b 896/* Optional subroutine body, for named subroutine declaration */
abcf453d 897optsubbody: block
2d0e3c96 898 | ';' { $$ = (OP*)NULL; }
8d063cd8
LW
899 ;
900
891be019 901/* Ordinary expressions; logical combinations */
a0d0e21e 902expr : expr ANDOP expr
b5bbe64a 903 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
a0d0e21e 904 | expr OROP expr
b5bbe64a 905 { $$ = newLOGOP($2, 0, $1, $3); }
c963b151 906 | expr DOROP expr
b5bbe64a 907 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
09b1cffe 908 | listexpr %prec PREC_LOW
a0d0e21e
LW
909 ;
910
891be019 911/* Expressions are a list of terms joined by commas */
09b1cffe 912listexpr: listexpr ','
b5bbe64a 913 { $$ = $1; }
09b1cffe 914 | listexpr ',' term
abcf453d 915 {
29522234 916 OP* term = $3;
2fcb4757 917 $$ = op_append_elem(OP_LIST, $1, term);
f05e27e5 918 }
fad39ff1 919 | term %prec PREC_LOW
8d063cd8
LW
920 ;
921
891be019 922/* List operators */
09b1cffe 923listop : LSTOP indirob listexpr /* map {...} @args or print $fh @args */
03d05f6e 924 { $$ = op_convert_list($1, OPf_STACKED,
b5bbe64a 925 op_prepend_elem(OP_LIST, newGVREF($1,$2), $3) );
f05e27e5 926 }
891be019 927 | FUNC '(' indirob expr ')' /* print ($fh @args */
03d05f6e 928 { $$ = op_convert_list($1, OPf_STACKED,
b5bbe64a 929 op_prepend_elem(OP_LIST, newGVREF($1,$3), $4) );
f05e27e5 930 }
417a992d 931 | term ARROW method '(' optexpr ')' /* $foo->bar(list) */
03d05f6e 932 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
933 op_append_elem(OP_LIST,
934 op_prepend_elem(OP_LIST, scalar($1), $5),
b46e009d 935 newMETHOP(OP_METHOD, 0, $3)));
f05e27e5 936 }
891be019 937 | term ARROW method /* $foo->bar */
03d05f6e 938 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
2fcb4757 939 op_append_elem(OP_LIST, scalar($1),
b46e009d 940 newMETHOP(OP_METHOD, 0, $3)));
f05e27e5 941 }
09b1cffe 942 | METHOD indirob optlistexpr /* new Class @args */
03d05f6e 943 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
944 op_append_elem(OP_LIST,
945 op_prepend_elem(OP_LIST, $2, $3),
b46e009d 946 newMETHOP(OP_METHOD, 0, $1)));
f05e27e5 947 }
09b1cffe 948 | FUNCMETH indirob '(' optexpr ')' /* method $object (@args) */
03d05f6e 949 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
950 op_append_elem(OP_LIST,
951 op_prepend_elem(OP_LIST, $2, $4),
b46e009d 952 newMETHOP(OP_METHOD, 0, $1)));
f05e27e5 953 }
09b1cffe 954 | LSTOP optlistexpr /* print @args */
03d05f6e 955 { $$ = op_convert_list($1, 0, $2); }
09b1cffe 956 | FUNC '(' optexpr ')' /* print (@args) */
03d05f6e 957 { $$ = op_convert_list($1, 0, $3); }
718a7425 958 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
5a5094bd 959 { SvREFCNT_inc_simple_void(PL_compcv);
bcabcc50 960 $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); }
09b1cffe 961 optlistexpr %prec LSTOP /* ... @bar */
4633a7c4 962 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
963 op_append_elem(OP_LIST,
964 op_prepend_elem(OP_LIST, $<opval>4, $5), $1));
f05e27e5 965 }
a687059c
LW
966 ;
967
891be019 968/* Names of methods. May use $object->$methodname */
a0d0e21e
LW
969method : METHOD
970 | scalar
971 ;
972
891be019 973/* Some kind of subscripted expression */
89f35911 974subscripted: gelem '{' expr ';' '}' /* *main::{something} */
891be019
SC
975 /* In this and all the hash accessors, ';' is
976 * provided by the tokeniser */
5c86b6df 977 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
891be019 978 | scalar '[' expr ']' /* $array[$element] */
f05e27e5 979 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
f05e27e5 980 }
891be019 981 | term ARROW '[' expr ']' /* somearef->[$element] */
fad39ff1
SM
982 { $$ = newBINOP(OP_AELEM, 0,
983 ref(newAVREF($1),OP_RV2AV),
f05e27e5 984 scalar($4));
f05e27e5 985 }
891be019 986 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
fad39ff1
SM
987 { $$ = newBINOP(OP_AELEM, 0,
988 ref(newAVREF($1),OP_RV2AV),
f05e27e5 989 scalar($3));
f05e27e5 990 }
5a63ceef 991 | scalar '{' expr ';' '}' /* $foo{bar();} */
fad39ff1 992 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
f05e27e5 993 }
891be019 994 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
fad39ff1
SM
995 { $$ = newBINOP(OP_HELEM, 0,
996 ref(newHVREF($1),OP_RV2HV),
5c86b6df 997 jmaybe($4)); }
891be019 998 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
fad39ff1
SM
999 { $$ = newBINOP(OP_HELEM, 0,
1000 ref(newHVREF($1),OP_RV2HV),
5c86b6df 1001 jmaybe($3)); }
891be019 1002 | term ARROW '(' ')' /* $subref->() */
fad39ff1 1003 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
b5bbe64a 1004 newCVREF(0, scalar($1))); }
891be019 1005 | term ARROW '(' expr ')' /* $subref->(@args) */
fad39ff1 1006 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 1007 op_append_elem(OP_LIST, $4,
b5bbe64a 1008 newCVREF(0, scalar($1)))); }
fad39ff1 1009
417a992d 1010 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
fad39ff1 1011 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 1012 op_append_elem(OP_LIST, $3,
b5bbe64a 1013 newCVREF(0, scalar($1)))); }
417a992d 1014 | subscripted '(' ')' /* $foo->{bar}->() */
fad39ff1 1015 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
b5bbe64a 1016 newCVREF(0, scalar($1))); }
9a9798c2 1017 | '(' expr ')' '[' expr ']' /* list slice */
b5bbe64a 1018 { $$ = newSLICEOP(0, $5, $2); }
ea25a9b2 1019 | QWLIST '[' expr ']' /* list literal slice */
b5bbe64a 1020 { $$ = newSLICEOP(0, $3, $1); }
9a9798c2 1021 | '(' ')' '[' expr ']' /* empty list slice! */
b5bbe64a 1022 { $$ = newSLICEOP(0, $4, (OP*)NULL); }
891be019 1023 ;
fad39ff1 1024
891be019 1025/* Binary operators between terms */
f05e27e5 1026termbinop: term ASSIGNOP term /* $x = $y */
b5bbe64a 1027 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
891be019 1028 | term POWOP term /* $x ** $y */
b5bbe64a 1029 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 1030 | term MULOP term /* $x * $y, $x x $y */
b5bbe64a 1031 { if ($2 != OP_REPEAT)
79072805 1032 scalar($1);
b5bbe64a 1033 $$ = newBINOP($2, 0, $1, scalar($3));
f05e27e5 1034 }
891be019 1035 | term ADDOP term /* $x + $y */
b5bbe64a 1036 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 1037 | term SHIFTOP term /* $x >> $y, $x << $y */
b5bbe64a 1038 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 1039 | term RELOP term /* $x > $y, etc. */
b5bbe64a 1040 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 1041 | term EQOP term /* $x == $y, $x eq $y */
b5bbe64a 1042 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 1043 | term BITANDOP term /* $x & $y */
b5bbe64a 1044 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 1045 | term BITOROP term /* $x | $y */
b5bbe64a 1046 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 1047 | term DOTDOT term /* $x..$y, $x...$y */
b5bbe64a 1048 { $$ = newRANGE($2, scalar($1), scalar($3)); }
891be019 1049 | term ANDAND term /* $x && $y */
b5bbe64a 1050 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
891be019 1051 | term OROR term /* $x || $y */
b5bbe64a 1052 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
c963b151 1053 | term DORDOR term /* $x // $y */
b5bbe64a 1054 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
891be019 1055 | term MATCHOP term /* $x =~ /$y/ */
b5bbe64a 1056 { $$ = bind_match($2, $1, $3); }
891be019 1057 ;
8d063cd8 1058
891be019
SC
1059/* Unary operators and terms */
1060termunop : '-' term %prec UMINUS /* -$x */
b5bbe64a 1061 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
891be019 1062 | '+' term %prec UMINUS /* +$x */
b5bbe64a
JH
1063 { $$ = $2; }
1064
891be019 1065 | '!' term /* !$x */
b5bbe64a 1066 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
891be019 1067 | '~' term /* ~$x */
8823cb89 1068 { $$ = newUNOP($1, 0, scalar($2)); }
891be019 1069 | term POSTINC /* $x++ */
79072805 1070 { $$ = newUNOP(OP_POSTINC, 0,
b5bbe64a 1071 op_lvalue(scalar($1), OP_POSTINC)); }
891be019 1072 | term POSTDEC /* $x-- */
79072805 1073 { $$ = newUNOP(OP_POSTDEC, 0,
b5bbe64a 1074 op_lvalue(scalar($1), OP_POSTDEC));}
cc624add 1075 | term POSTJOIN /* implicit join after interpolated ->@ */
03d05f6e 1076 { $$ = op_convert_list(OP_JOIN, 0,
cc624add
FC
1077 op_append_elem(
1078 OP_LIST,
1079 newSVREF(scalar(
1080 newSVOP(OP_CONST,0,
1081 newSVpvs("\""))
1082 )),
1083 $1
1084 ));
cc624add 1085 }
891be019 1086 | PREINC term /* ++$x */
79072805 1087 { $$ = newUNOP(OP_PREINC, 0,
b5bbe64a 1088 op_lvalue(scalar($2), OP_PREINC)); }
891be019 1089 | PREDEC term /* --$x */
79072805 1090 { $$ = newUNOP(OP_PREDEC, 0,
b5bbe64a 1091 op_lvalue(scalar($2), OP_PREDEC)); }
891be019
SC
1092
1093 ;
1094
1095/* Constructors for anonymous data */
1096anonymous: '[' expr ']'
b5bbe64a 1097 { $$ = newANONLIST($2); }
891be019 1098 | '[' ']'
b5bbe64a 1099 { $$ = newANONLIST((OP*)NULL);}
891be019 1100 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
b5bbe64a 1101 { $$ = newANONHASH($2); }
891be019 1102 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
b5bbe64a 1103 { $$ = newANONHASH((OP*)NULL); }
abcf453d 1104 | ANONSUB startanonsub proto subattrlist block %prec '('
5a5094bd 1105 { SvREFCNT_inc_simple_void(PL_compcv);
b5bbe64a 1106 $$ = newANONATTRSUB($2, $3, $4, $5); }
abcf453d
PM
1107 | ANONSUB startanonsub remember subsignature subattrlist '{' stmtseq '}' %prec '('
1108 {
1109 OP *body;
1110 if (parser->copline > (line_t)$6)
1111 parser->copline = (line_t)$6;
1112 body = block_end($3,
1113 op_append_list(OP_LINESEQ, $4, $7));
1114 SvREFCNT_inc_simple_void(PL_compcv);
1115 $$ = newANONATTRSUB($2, NULL, $5, body);
1116 }
891be019
SC
1117
1118 ;
1119
1120/* Things called with "do" */
1121termdo : DO term %prec UNIOP /* do $filename */
b5bbe64a 1122 { $$ = dofile($2, $1);}
891be019 1123 | DO block %prec '(' /* do { code */
b5bbe64a 1124 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));}
891be019
SC
1125 ;
1126
1127term : termbinop
1128 | termunop
1129 | anonymous
1130 | termdo
e9bdcc27 1131 | term '?' term ':' term
b5bbe64a 1132 { $$ = newCONDOP(0, $1, $3, $5); }
891be019 1133 | REFGEN term /* \$x, \@y, \%z */
070d3cb6 1134 { $$ = newUNOP(OP_REFGEN, 0, $2); }
e118fea3
FC
1135 | MY REFGEN term
1136 { $$ = newUNOP(OP_REFGEN, 0, localize($3,1)); }
09bef843
SB
1137 | myattrterm %prec UNIOP
1138 { $$ = $1; }
1139 | LOCAL term %prec UNIOP
28383d1a 1140 { $$ = localize($2,0); }
a0d0e21e 1141 | '(' expr ')'
b5bbe64a 1142 { $$ = sawparens($2); }
ea25a9b2 1143 | QWLIST
b5bbe64a 1144 { $$ = $1; }
8d063cd8 1145 | '(' ')'
b5bbe64a 1146 { $$ = sawparens(newNULLLIST()); }
79072805 1147 | scalar %prec '('
8d063cd8 1148 { $$ = $1; }
79072805 1149 | star %prec '('
8d063cd8 1150 { $$ = $1; }
79072805 1151 | hsh %prec '('
8d063cd8 1152 { $$ = $1; }
79072805 1153 | ary %prec '('
8d063cd8 1154 { $$ = $1; }
891be019 1155 | arylen %prec '(' /* $#x, $#{ something } */
79072805 1156 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
fad39ff1
SM
1157 | subscripted
1158 { $$ = $1; }
89f35911 1159 | sliceme '[' expr ']' /* array slice */
2fcb4757 1160 { $$ = op_prepend_elem(OP_ASLICE,
79072805 1161 newOP(OP_PUSHMARK, 0),
79072805
LW
1162 newLISTOP(OP_ASLICE, 0,
1163 list($3),
f05e27e5 1164 ref($1, OP_ASLICE)));
429a2555
FC
1165 if ($$ && $1)
1166 $$->op_private |=
1167 $1->op_private & OPpSLICEWARNING;
f05e27e5 1168 }
76eba8ab 1169 | kvslice '[' expr ']' /* array key/value slice */
6dd3e0f2
RZ
1170 { $$ = op_prepend_elem(OP_KVASLICE,
1171 newOP(OP_PUSHMARK, 0),
1172 newLISTOP(OP_KVASLICE, 0,
1173 list($3),
1174 ref(oopsAV($1), OP_KVASLICE)));
95a31aad
FC
1175 if ($$ && $1)
1176 $$->op_private |=
1177 $1->op_private & OPpSLICEWARNING;
6dd3e0f2 1178 }
89f35911 1179 | sliceme '{' expr ';' '}' /* @hash{@keys} */
2fcb4757 1180 { $$ = op_prepend_elem(OP_HSLICE,
79072805 1181 newOP(OP_PUSHMARK, 0),
79072805
LW
1182 newLISTOP(OP_HSLICE, 0,
1183 list($3),
a0d0e21e 1184 ref(oopsHV($1), OP_HSLICE)));
429a2555
FC
1185 if ($$ && $1)
1186 $$->op_private |=
1187 $1->op_private & OPpSLICEWARNING;
f05e27e5 1188 }
76eba8ab 1189 | kvslice '{' expr ';' '}' /* %hash{@keys} */
5cae3edb
RZ
1190 { $$ = op_prepend_elem(OP_KVHSLICE,
1191 newOP(OP_PUSHMARK, 0),
1192 newLISTOP(OP_KVHSLICE, 0,
1193 list($3),
1194 ref($1, OP_KVHSLICE)));
95a31aad
FC
1195 if ($$ && $1)
1196 $$->op_private |=
1197 $1->op_private & OPpSLICEWARNING;
5cae3edb 1198 }
79072805
LW
1199 | THING %prec '('
1200 { $$ = $1; }
891be019 1201 | amper /* &foo; */
c07a80fd 1202 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
fb602e32 1203 | amper '(' ')' /* &foo() or foo() */
f05e27e5 1204 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
f05e27e5 1205 }
fb602e32 1206 | amper '(' expr ')' /* &foo(@args) or foo(@args) */
f05e27e5
DM
1207 {
1208 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 1209 op_append_elem(OP_LIST, $3, scalar($1)));
f05e27e5 1210 }
fb602e32 1211 | NOAMP subname optlistexpr /* foo @args (no parens) */
a0d0e21e 1212 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 1213 op_append_elem(OP_LIST, $3, scalar($2)));
f05e27e5 1214 }
89f35911 1215 | term ARROW '$' '*'
b5bbe64a 1216 { $$ = newSVREF($1); }
89f35911 1217 | term ARROW '@' '*'
b5bbe64a 1218 { $$ = newAVREF($1); }
89f35911 1219 | term ARROW '%' '*'
b5bbe64a 1220 { $$ = newHVREF($1); }
89f35911
FC
1221 | term ARROW '&' '*'
1222 { $$ = newUNOP(OP_ENTERSUB, 0,
b5bbe64a 1223 scalar(newCVREF($3,$1))); }
89f35911 1224 | term ARROW '*' '*' %prec '('
b5bbe64a 1225 { $$ = newGVREF(0,$1); }
891be019 1226 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
b5bbe64a
JH
1227 { $$ = newOP($1, OPf_SPECIAL);
1228 PL_hints |= HINT_BLOCK_SCOPE; }
a0d0e21e 1229 | LOOPEX term
b5bbe64a 1230 { $$ = newLOOPEX($1,$2); }
09b1cffe 1231 | NOTOP listexpr /* not $foo */
b5bbe64a 1232 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
891be019 1233 | UNIOP /* Unary op, $_ implied */
b5bbe64a 1234 { $$ = newOP($1, 0); }
f05e27e5 1235 | UNIOP block /* eval { foo }* */
b5bbe64a 1236 { $$ = newUNOP($1, 0, $2); }
891be019 1237 | UNIOP term /* Unary op */
b5bbe64a 1238 { $$ = newUNOP($1, 0, $2); }
d2fdf8fd 1239 | REQUIRE /* require, $_ implied */
b5bbe64a 1240 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); }
d2fdf8fd 1241 | REQUIRE term /* require Foo */
b5bbe64a 1242 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); }
3cd0a11a
RGS
1243 | UNIOPSUB
1244 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
891be019 1245 | UNIOPSUB term /* Sub treated as unop */
4633a7c4 1246 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 1247 op_append_elem(OP_LIST, $2, scalar($1))); }
891be019 1248 | FUNC0 /* Nullary operator */
b5bbe64a 1249 { $$ = newOP($1, 0); }
ae986130 1250 | FUNC0 '(' ')'
b5bbe64a 1251 { $$ = newOP($1, 0);}
7eb971ee
FC
1252 | FUNC0OP /* Same as above, but op created in toke.c */
1253 { $$ = $1; }
1254 | FUNC0OP '(' ')'
b5bbe64a 1255 { $$ = $1; }
891be019 1256 | FUNC0SUB /* Sub treated as nullop */
b5bbe64a 1257 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
891be019 1258 | FUNC1 '(' ')' /* not () */
b5bbe64a
JH
1259 { $$ = ($1 == OP_NOT)
1260 ? newUNOP($1, 0, newSVOP(OP_CONST, 0, newSViv(0)))
1261 : newOP($1, OPf_SPECIAL); }
891be019 1262 | FUNC1 '(' expr ')' /* not($foo) */
b5bbe64a 1263 { $$ = newUNOP($1, 0, $3); }
d63c20f2
DM
1264 | PMFUNC /* m//, s///, qr//, tr/// */
1265 {
1266 if ( $1->op_type != OP_TRANS
1267 && $1->op_type != OP_TRANSR
1268 && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV))
1269 {
1270 $<ival>$ = start_subparse(FALSE, CVf_ANON);
1271 SAVEFREESV(PL_compcv);
1272 } else
1273 $<ival>$ = 0;
1274 }
9b6b7be8
FC
1275 '(' listexpr optrepl ')'
1276 { $$ = pmruntime($1, $4, $5, 1, $<ival>2); }
185c2e96 1277 | BAREWORD
378cc40b 1278 | listop
be25f609 1279 | YADAYADA
1280 {
1281 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
1282 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
1283 }
88e1f1a2 1284 | PLUGEXPR
8d063cd8
LW
1285 ;
1286
891be019 1287/* "my" declarations, with optional attributes */
09bef843 1288myattrterm: MY myterm myattrlist
b5bbe64a 1289 { $$ = my_attrs($2,$3); }
09bef843 1290 | MY myterm
28383d1a 1291 { $$ = localize($2,1); }
e118fea3
FC
1292 | MY REFGEN myterm myattrlist
1293 { $$ = newUNOP(OP_REFGEN, 0, my_attrs($3,$4)); }
09bef843
SB
1294 ;
1295
891be019 1296/* Things that can be "my"'d */
09bef843 1297myterm : '(' expr ')'
b5bbe64a 1298 { $$ = sawparens($2); }
09bef843 1299 | '(' ')'
b5bbe64a
JH
1300 { $$ = sawparens(newNULLLIST()); }
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
9b6b7be8
FC
1323optrepl: /* NULL */
1324 { $$ = (OP*)NULL; }
1325 | '/' expr
1326 { $$ = $2; }
1327 ;
1328
891be019
SC
1329/* A little bit of trickery to make "for my $foo (@bar)" actually be
1330 lexical */
55497cff 1331my_scalar: scalar
624fa8bd 1332 { parser->in_my = 0; $$ = my($1); }
55497cff
PP
1333 ;
1334
d39c26a6
FC
1335my_var : scalar
1336 | ary
1337 | hsh
1338 ;
1339
1340refgen_topic: my_var
1341 | amper
1342 ;
1343
e118fea3
FC
1344my_refgen: MY REFGEN
1345 | REFGEN MY
1346 ;
1347
79072805 1348amper : '&' indirob
b5bbe64a 1349 { $$ = newCVREF($1,$2); }
a687059c
LW
1350 ;
1351
79072805 1352scalar : '$' indirob
b5bbe64a 1353 { $$ = newSVREF($2); }
a687059c
LW
1354 ;
1355
79072805 1356ary : '@' indirob
f05e27e5 1357 { $$ = newAVREF($2);
b5bbe64a 1358 if ($$) $$->op_private |= $1;
f05e27e5 1359 }
79072805
LW
1360 ;
1361
1362hsh : '%' indirob
f05e27e5 1363 { $$ = newHVREF($2);
b5bbe64a 1364 if ($$) $$->op_private |= $1;
f05e27e5 1365 }
79072805
LW
1366 ;
1367
1368arylen : DOLSHARP indirob
b5bbe64a 1369 { $$ = newAVREF($2); }
ff25e5db 1370 | term ARROW DOLSHARP '*'
b5bbe64a 1371 { $$ = newAVREF($1); }
79072805
LW
1372 ;
1373
1374star : '*' indirob
b5bbe64a 1375 { $$ = newGVREF(0,$2); }
79072805
LW
1376 ;
1377
89f35911
FC
1378sliceme : ary
1379 | term ARROW '@'
b5bbe64a 1380 { $$ = newAVREF($1); }
89f35911
FC
1381 ;
1382
76eba8ab
FC
1383kvslice : hsh
1384 | term ARROW '%'
b5bbe64a 1385 { $$ = newHVREF($1); }
76eba8ab
FC
1386 ;
1387
89f35911
FC
1388gelem : star
1389 | term ARROW '*'
b5bbe64a 1390 { $$ = newGVREF(0,$1); }
89f35911
FC
1391 ;
1392
891be019 1393/* Indirect objects */
185c2e96 1394indirob : BAREWORD
79072805 1395 { $$ = scalar($1); }
fad39ff1 1396 | scalar %prec PREC_LOW
f05e27e5 1397 { $$ = scalar($1); }
79072805 1398 | block
3ad73efd 1399 { $$ = op_scope($1); }
79072805 1400
93a17b20
LW
1401 | PRIVATEREF
1402 { $$ = $1; }
8d063cd8 1403 ;