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