This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
stop eval "sub{" leaking
[perl5.git] / madly.y
CommitLineData
00e74f14
NC
1/* perly.y
2 *
3 * Copyright (c) 1991-2002, 2003, 2004 Larry Wall
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
8 */
9
10/*
11 * 'I see,' laughed Strider. 'I look foul and feel fair. Is that it?
12 * All that is gold does not glitter, not all those who wander are lost.'
13 */
14
15/* Make the parser re-entrant. */
16
733849fa 17/* FIXME for MAD - is the new mintro on while and until important? */
00e74f14
NC
18%pure_parser
19
20%start prog
21
22%union {
23 I32 ival;
24 char *pval;
25 TOKEN* tkval;
26 OP *opval;
27 GV *gvval;
28}
29
30%token <tkval> '{' '}' '[' ']' '-' '+' '$' '@' '%' '*'
31
32%token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
33%token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
34%token <tkval> LABEL
35%token <tkval> FORMAT SUB ANONSUB PACKAGE USE
36%token <tkval> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
733849fa 37%token <tkval> GIVEN WHEN DEFAULT
00e74f14
NC
38%token <tkval> LOOPEX DOTDOT
39%token <tkval> FUNC0 FUNC1 FUNC UNIOP LSTOP
40%token <tkval> RELOP EQOP MULOP ADDOP
41%token <tkval> DOLSHARP DO HASHBRACK NOAMP
733849fa 42%token <tkval> LOCAL MY MYSUB REQUIRE
00e74f14
NC
43%token <tkval> COLONATTR
44
45%type <ival> prog progstart remember mremember savescope
733849fa
NC
46%type <ival> startsub startanonsub startformsub
47/* FIXME for MAD - are these two ival? */
48%type <ival> mydefsv mintro
00e74f14
NC
49
50%type <tkval> '&' ';'
51
52%type <opval> decl format subrout mysubrout package use peg
53
54%type <opval> block mblock lineseq line loop cond else
55%type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
56%type <opval> argexpr nexpr texpr iexpr mexpr mnexpr miexpr
57%type <opval> listexpr listexprcom indirob listop method
58%type <opval> formname subname proto subbody cont my_scalar
59%type <opval> subattrlist myattrlist myattrterm myterm
60%type <opval> termbinop termunop anonymous termdo
733849fa 61%type <opval> switch case
00e74f14
NC
62%type <tkval> label
63
64%nonassoc <tkval> PREC_LOW
65%nonassoc LOOPEX
66
67%left <tkval> OROP DOROP
68%left <tkval> ANDOP
69%right <tkval> NOTOP
70%nonassoc LSTOP LSTOPSUB
71%left <tkval> ','
72%right <tkval> ASSIGNOP
73%right <tkval> '?' ':'
74%nonassoc DOTDOT
75%left <tkval> OROR DORDOR
76%left <tkval> ANDAND
77%left <tkval> BITOROP
78%left <tkval> BITANDOP
79%nonassoc EQOP
80%nonassoc RELOP
81%nonassoc UNIOP UNIOPSUB
733849fa 82%nonassoc REQUIRE
00e74f14
NC
83%left <tkval> SHIFTOP
84%left ADDOP
85%left MULOP
86%left <tkval> MATCHOP
87%right <tkval> '!' '~' UMINUS REFGEN
88%right <tkval> POWOP
89%nonassoc <tkval> PREINC PREDEC POSTINC POSTDEC
90%left <tkval> ARROW
91%nonassoc <tkval> ')'
92%left <tkval> '('
93%left '[' '{'
94
95%token <tkval> PEG
96
97%% /* RULES */
98
99/* The whole program */
100prog : progstart
101 /*CONTINUED*/ lineseq
102 { $$ = $1; newPROG(block_end($1,$2)); }
103 ;
104
105/* An ordinary block */
106block : '{' remember lineseq '}'
107 { if (PL_copline > (line_t)($1)->tk_lval.ival)
108 PL_copline = (line_t)($1)->tk_lval.ival;
109 $$ = block_end($2, $3);
110 token_getmad($1,$$,'{');
111 token_getmad($4,$$,'}');
112 }
113 ;
114
115remember: /* NULL */ /* start a full lexical scope */
116 { $$ = block_start(TRUE); }
117 ;
118
733849fa
NC
119mydefsv: /* NULL */ /* lexicalize $_ */
120 { $$ = (I32) allocmy("$_"); }
121 ;
122
00e74f14
NC
123progstart:
124 {
125 PL_expect = XSTATE; $$ = block_start(TRUE);
126 }
127 ;
128
129
130mblock : '{' mremember lineseq '}'
131 { if (PL_copline > (line_t)($1)->tk_lval.ival)
132 PL_copline = (line_t)($1)->tk_lval.ival;
133 $$ = block_end($2, $3);
134 token_getmad($1,$$,'{');
135 token_getmad($4,$$,'}');
136 }
137 ;
138
139mremember: /* NULL */ /* start a partial lexical scope */
140 { $$ = block_start(FALSE); }
141 ;
142
143savescope: /* NULL */ /* remember stack pos in case of error */
144 { $$ = PL_savestack_ix; }
145
146/* A collection of "lines" in the program */
147lineseq : /* NULL */
148 { $$ = Nullop; }
149 | lineseq decl
150/* { $$ = $1 } */
151 { $$ = append_list(OP_LINESEQ,
152 (LISTOP*)$1, (LISTOP*)$2); }
153 | lineseq savescope line
154 { LEAVE_SCOPE($2);
155 $$ = append_list(OP_LINESEQ,
156 (LISTOP*)$1, (LISTOP*)$3);
157 PL_pad_reset_pending = TRUE;
158 if ($1 && $3) PL_hints |= HINT_BLOCK_SCOPE; }
159 ;
160
161/* A "line" in the program */
162line : label cond
163 { $$ = newSTATEOP(0, ($1)->tk_lval.pval, $2);
164 token_getmad($1,((LISTOP*)$$)->op_first,'L'); }
165 | loop /* loops add their own labels */
733849fa
NC
166 | switch /* ... and so do switches */
167 { $$ = $1; }
168 | label case
169 { $$ = newSTATEOP(0, ($1)->tk_lval.pval, $2); }
00e74f14
NC
170 | label ';'
171 {
172 if (($1)->tk_lval.pval) {
173 $$ = newSTATEOP(0, ($1)->tk_lval.pval, newOP(OP_NULL, 0));
174 token_getmad($1,$$,'L');
175 token_getmad($2,((LISTOP*)$$)->op_first,';');
176 }
177 else {
178 $$ = newOP(OP_NULL, 0);
179 PL_copline = NOLINE;
180 token_free($1);
181 token_getmad($2,$$,';');
182 }
183 PL_expect = XSTATE;
184 }
185 | label sideff ';'
186 { OP* op;
187 $$ = newSTATEOP(0, ($1)->tk_lval.pval, $2);
188 PL_expect = XSTATE;
189 /* sideff might already have a nexstate */
190 op = ((LISTOP*)$$)->op_first;
191 if (op) {
192 while (op->op_sibling &&
193 op->op_sibling->op_type == OP_NEXTSTATE)
194 op = op->op_sibling;
195 token_getmad($1,op,'L');
196 token_getmad($3,op,';');
197 }
198 }
199 ;
200
201/* An expression which may have a side-effect */
202sideff : error
203 { $$ = Nullop; }
204 | expr
205 { $$ = $1; }
206 | expr IF expr
207 { $$ = newLOGOP(OP_AND, 0, $3, $1);
208 token_getmad($2,$$,'i');
209 }
210 | expr UNLESS expr
211 { $$ = newLOGOP(OP_OR, 0, $3, $1);
212 token_getmad($2,$$,'i');
213 }
214 | expr WHILE expr
215 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1);
216 token_getmad($2,$$,'w');
217 }
218 | expr UNTIL iexpr
219 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);
220 token_getmad($2,$$,'w');
221 }
222 | expr FOR expr
223 { $$ = newFOROP(0, Nullch, (line_t)($2)->tk_lval.ival,
224 Nullop, $3, $1, Nullop);
225 token_getmad($2,((LISTOP*)$$)->op_first->op_sibling,'w');
226 }
227 ;
228
229/* else and elsif blocks */
230else : /* NULL */
231 { $$ = Nullop; }
232 | ELSE mblock
233 { ($2)->op_flags |= OPf_PARENS; $$ = scope($2);
234 token_getmad($1,$$,'o');
235 }
236 | ELSIF '(' mexpr ')' mblock else
237 { PL_copline = (line_t)($1)->tk_lval.ival;
238 $$ = newCONDOP(0, $3, scope($5), $6);
239 PL_hints |= HINT_BLOCK_SCOPE;
240 token_getmad($1,$$,'I');
241 token_getmad($2,$$,'(');
242 token_getmad($4,$$,')');
243 }
244 ;
245
246/* Real conditional expressions */
247cond : IF '(' remember mexpr ')' mblock else
248 { PL_copline = (line_t)($1)->tk_lval.ival;
249 $$ = block_end($3,
250 newCONDOP(0, $4, scope($6), $7));
251 token_getmad($1,$$,'I');
252 token_getmad($2,$$,'(');
253 token_getmad($5,$$,')');
254 }
255 | UNLESS '(' remember miexpr ')' mblock else
256 { PL_copline = (line_t)($1)->tk_lval.ival;
257 $$ = block_end($3,
258 newCONDOP(0, $4, scope($6), $7));
259 token_getmad($1,$$,'I');
260 token_getmad($2,$$,'(');
261 token_getmad($5,$$,')');
262 }
263 ;
264
733849fa
NC
265/* Cases for a switch statement */
266case : WHEN '(' remember mexpr ')' mblock
267 { $$ = block_end($3,
268 newWHENOP($4, scope($6))); }
269 | DEFAULT block
270 { $$ = newWHENOP(0, scope($2)); }
271 ;
272
00e74f14
NC
273/* Continue blocks */
274cont : /* NULL */
275 { $$ = Nullop; }
276 | CONTINUE block
277 { $$ = scope($2);
278 token_getmad($1,$$,'o');
279 }
280 ;
281
282/* Loops: while, until, for, and a bare block */
283loop : label WHILE '(' remember texpr ')' mintro mblock cont
284 { OP *innerop;
285 PL_copline = (line_t)$2;
733849fa 286 $$ = block_end($4,
00e74f14
NC
287 newSTATEOP(0, ($1)->tk_lval.pval,
288 innerop = newWHILEOP(0, 1, (LOOP*)Nullop,
289 ($2)->tk_lval.ival, $5, $8, $9, $7)));
290 token_getmad($1,innerop,'L');
291 token_getmad($2,innerop,'W');
292 token_getmad($3,innerop,'(');
293 token_getmad($6,innerop,')');
294 }
733849fa 295
00e74f14
NC
296 | label UNTIL '(' remember iexpr ')' mintro mblock cont
297 { OP *innerop;
298 PL_copline = (line_t)$2;
733849fa 299 $$ = block_end($4,
00e74f14 300 newSTATEOP(0, ($1)->tk_lval.pval,
733849fa 301 newWHILEOP(0, 1, (LOOP*)Nullop,
00e74f14
NC
302 ($2)->tk_lval.ival, $5, $8, $9, $7)));
303 token_getmad($1,innerop,'L');
304 token_getmad($2,innerop,'W');
305 token_getmad($3,innerop,'(');
306 token_getmad($6,innerop,')');
307 }
308 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
309 { OP *innerop;
310 $$ = block_end($4,
311 innerop = newFOROP(0, ($1)->tk_lval.pval, (line_t)($2)->tk_lval.ival, $5, $7, $9, $10));
312 token_getmad($1,((LISTOP*)innerop)->op_first,'L');
313 token_getmad($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
314 token_getmad($3,((LISTOP*)innerop)->op_first->op_sibling,'d');
315 token_getmad($6,((LISTOP*)innerop)->op_first->op_sibling,'(');
316 token_getmad($8,((LISTOP*)innerop)->op_first->op_sibling,')');
317 }
318 | label FOR scalar '(' remember mexpr ')' mblock cont
319 { OP *innerop;
320 $$ = block_end($5,
321 innerop = newFOROP(0, ($1)->tk_lval.pval, (line_t)($2)->tk_lval.ival, mod($3, OP_ENTERLOOP),
322 $6, $8, $9));
323 token_getmad($1,((LISTOP*)innerop)->op_first,'L');
324 token_getmad($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
325 token_getmad($4,((LISTOP*)innerop)->op_first->op_sibling,'(');
326 token_getmad($7,((LISTOP*)innerop)->op_first->op_sibling,')');
327 }
328 | label FOR '(' remember mexpr ')' mblock cont
329 { OP *innerop;
330 $$ = block_end($4,
331 innerop = newFOROP(0, ($1)->tk_lval.pval, (line_t)($2)->tk_lval.ival, Nullop, $5, $7, $8));
332 token_getmad($1,((LISTOP*)innerop)->op_first,'L');
333 token_getmad($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
334 token_getmad($3,((LISTOP*)innerop)->op_first->op_sibling,'(');
335 token_getmad($6,((LISTOP*)innerop)->op_first->op_sibling,')');
336 }
733849fa
NC
337 | label FOR '(' remember mnexpr ';' texpr ';' mintro mnexpr ')'
338 mblock
00e74f14
NC
339 /* basically fake up an initialize-while lineseq */
340 { OP *forop;
341 PL_copline = (line_t)($2)->tk_lval.ival;
342 forop = newSTATEOP(0, ($1)->tk_lval.pval,
343 newWHILEOP(0, 1, (LOOP*)Nullop,
344 ($2)->tk_lval.ival, scalar($7),
345 $12, $10, $9));
346 if (!$5)
347 $5 = newOP(OP_NULL, 0);
348 forop = newUNOP(OP_NULL, 0, append_elem(OP_LINESEQ,
349 newSTATEOP(0,
350 (($1)->tk_lval.pval
351 ?savepv(($1)->tk_lval.pval):Nullch),
352 $5),
353 forop));
354
355 token_getmad($2,forop,'3');
356 token_getmad($3,forop,'(');
357 token_getmad($6,forop,'1');
358 token_getmad($8,forop,'2');
359 token_getmad($11,forop,')');
360 token_getmad($1,forop,'L');
733849fa 361 $$ = block_end($4, forop); }
00e74f14
NC
362 | label block cont /* a block is a loop that happens once */
363 { $$ = newSTATEOP(0, ($1)->tk_lval.pval,
364 newWHILEOP(0, 1, (LOOP*)Nullop,
365 NOLINE, Nullop, $2, $3, 0));
366 token_getmad($1,((LISTOP*)$$)->op_first,'L'); }
367 ;
368
733849fa
NC
369/* Switch blocks */
370switch : label GIVEN '(' remember mydefsv mexpr ')' mblock
371 { PL_copline = (line_t) $2;
372 $$ = block_end($4,
373 newSTATEOP(0, ($1)->tk_lval.pval,
374 newGIVENOP($6, scope($8),
375 (PADOFFSET) $5) )); }
376 ;
377
00e74f14
NC
378/* determine whether there are any new my declarations */
379mintro : /* NULL */
380 { $$ = (PL_min_intro_pending &&
381 PL_max_intro_pending >= PL_min_intro_pending);
382 intro_my(); }
383
00e74f14
NC
384/* Normal expression */
385nexpr : /* NULL */
386 { $$ = Nullop; }
387 | sideff
388 ;
389
390/* Boolean expression */
391texpr : /* NULL means true */
392 { YYSTYPE tmplval;
393 (void)scan_num("1", &tmplval);
394 $$ = tmplval.opval; }
395 | expr
396 ;
397
398/* Inverted boolean expression */
399iexpr : expr
400 { $$ = invert(scalar($1)); }
401 ;
402
403/* Expression with its own lexical scope */
404mexpr : expr
405 { $$ = $1; intro_my(); }
406 ;
407
408mnexpr : nexpr
409 { $$ = $1; intro_my(); }
410 ;
411
412miexpr : iexpr
413 { $$ = $1; intro_my(); }
414 ;
415
416/* Optional "MAIN:"-style loop labels */
417label : /* empty */
418 { YYSTYPE tmplval;
419 tmplval.pval = Nullch;
420 $$ = newTOKEN(OP_NULL, tmplval, 0); }
421 | LABEL
422 ;
423
424/* Some kind of declaration - just hang on peg in the parse tree */
425decl : format
426 { $$ = $1; }
427 | subrout
428 { $$ = $1; }
429 | mysubrout
430 { $$ = $1; }
431 | package
432 { $$ = $1; }
433 | use
434 { $$ = $1; }
435 | peg
436 { $$ = $1; }
437 ;
438
439peg : PEG
440 { $$ = newOP(OP_NULL,0);
441 token_getmad($1,$$,'p');
442 }
443 ;
444
445format : FORMAT startformsub formname block
a8ff2fa6
DM
446 { SvREFCNT_inc(PL_compcv);
447 $$ = newFORM($2, $3, $4);
00e74f14
NC
448 prepend_madprops($1->tk_mad, $$, 'F');
449 $1->tk_mad = 0;
450 token_free($1);
451 }
452 ;
453
454formname: WORD { $$ = $1; }
455 | /* NULL */ { $$ = Nullop; }
456 ;
457
458/* Unimplemented "my sub foo { }" */
459mysubrout: MYSUB startsub subname proto subattrlist subbody
a8ff2fa6
DM
460 { SvREFCNT_inc(PL_compcv);
461 $$ = newMYSUB($2, $3, $4, $5, $6);
00e74f14
NC
462 token_getmad($1,$$,'d');
463 }
464 ;
465
466/* Subroutine definition */
467subrout : SUB startsub subname proto subattrlist subbody
a8ff2fa6
DM
468 { SvREFCNT_inc(PL_compcv);
469 OP* o = newSVOP(OP_ANONCODE, 0,
00e74f14
NC
470 (SV*)newATTRSUB($2, $3, $4, $5, $6));
471 $$ = newOP(OP_NULL,0);
472 op_getmad(o,$$,'&');
473 op_getmad($3,$$,'n');
474 op_getmad($4,$$,'s');
475 op_getmad($5,$$,'a');
476 token_getmad($1,$$,'d');
477 append_madprops($6->op_madprop, $$, 0);
478 $6->op_madprop = 0;
479 }
480 ;
481
482startsub: /* NULL */ /* start a regular subroutine scope */
a8ff2fa6
DM
483 { $$ = start_subparse(FALSE, 0);
484 SAVEFREESV(PL_compcv); }
485
00e74f14
NC
486 ;
487
488startanonsub: /* NULL */ /* start an anonymous subroutine scope */
a8ff2fa6
DM
489 { $$ = start_subparse(FALSE, CVf_ANON);
490 SAVEFREESV(PL_compcv); }
00e74f14
NC
491 ;
492
493startformsub: /* NULL */ /* start a format subroutine scope */
a8ff2fa6
DM
494 { $$ = start_subparse(TRUE, 0);
495 SAVEFREESV(PL_compcv); }
00e74f14
NC
496 ;
497
498/* Name of a subroutine - must be a bareword, could be special */
733849fa 499subname : WORD { const char *const name = SvPV_nolen_const(((SVOP*)$1)->op_sv);
00e74f14
NC
500 if (strEQ(name, "BEGIN") || strEQ(name, "END")
501 || strEQ(name, "INIT") || strEQ(name, "CHECK"))
502 CvSPECIAL_on(PL_compcv);
503 $$ = $1; }
504 ;
505
506/* Subroutine prototype */
507proto : /* NULL */
508 { $$ = Nullop; }
509 | THING
510 ;
511
512/* Optional list of subroutine attributes */
513subattrlist: /* NULL */
514 { $$ = Nullop; }
515 | COLONATTR THING
516 { $$ = $2;
517 token_getmad($1,$$,':');
518 }
519 | COLONATTR
520 { $$ = newOP(OP_NULL, 0);
521 token_getmad($1,$$,':');
522 }
523 ;
524
525/* List of attributes for a "my" variable declaration */
526myattrlist: COLONATTR THING
527 { $$ = $2;
528 token_getmad($1,$$,':');
529 }
530 | COLONATTR
531 { $$ = newOP(OP_NULL, 0);
532 token_getmad($1,$$,':');
533 }
534 ;
535
536/* Subroutine body - either null or a block */
537subbody : block { $$ = $1; }
538 | ';' { $$ = newOP(OP_NULL,0); PL_expect = XSTATE;
539 token_getmad($1,$$,';');
540 }
541 ;
542
543package : PACKAGE WORD ';'
544 { $$ = package($2);
545 token_getmad($1,$$,'o');
546 token_getmad($3,$$,';');
547 }
548 ;
549
550use : USE startsub
551 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
552 WORD WORD listexpr ';'
a8ff2fa6
DM
553 { SvREFCNT_inc(PL_compcv);
554 $$ = utilize(($1)->tk_lval.ival, $2, $4, $5, $6);
00e74f14
NC
555 token_getmad($1,$$,'o');
556 token_getmad($7,$$,';');
557 if (PL_rsfp_filters && AvFILLp(PL_rsfp_filters) >= 0)
558 append_madprops(newMADPROP('!', MAD_PV, "", 0), $$, 0);
559 }
560 ;
561
562/* Ordinary expressions; logical combinations */
563expr : expr ANDOP expr
564 { $$ = newLOGOP(OP_AND, 0, $1, $3);
565 token_getmad($2,$$,'o');
566 }
567 | expr OROP expr
568 { $$ = newLOGOP(($2)->tk_lval.ival, 0, $1, $3);
569 token_getmad($2,$$,'o');
570 }
571 | expr DOROP expr
572 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
573 token_getmad($2,$$,'o');
574 }
575 | argexpr %prec PREC_LOW
576 ;
577
578/* Expressions are a list of terms joined by commas */
579argexpr : argexpr ','
580 { OP* op = newNULLLIST();
581 token_getmad($2,op,',');
582 $$ = append_elem(OP_LIST, $1, op);
583 }
584 | argexpr ',' term
585 {
586 $3 = newUNOP(OP_NULL, 0, $3);
587 token_getmad($2,$3,',');
588 $$ = append_elem(OP_LIST, $1, $3);
589 }
590 | term %prec PREC_LOW
591 ;
592
593/* List operators */
594listop : LSTOP indirob argexpr /* print $fh @args */
595 { $$ = convert(($1)->tk_lval.ival, OPf_STACKED,
596 prepend_elem(OP_LIST, newGVREF(($1)->tk_lval.ival,$2), $3) );
597 token_getmad($1,$$,'o');
598 }
599 | FUNC '(' indirob expr ')' /* print ($fh @args */
600 { $$ = convert(($1)->tk_lval.ival, OPf_STACKED,
601 prepend_elem(OP_LIST, newGVREF(($1)->tk_lval.ival,$3), $4) );
602 token_getmad($1,$$,'o');
603 token_getmad($2,$$,'(');
604 token_getmad($5,$$,')');
605 }
606 | term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
607 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
608 append_elem(OP_LIST,
609 prepend_elem(OP_LIST, scalar($1), $5),
610 newUNOP(OP_METHOD, 0, $3)));
611 token_getmad($2,$$,'A');
612 token_getmad($4,$$,'(');
613 token_getmad($6,$$,')');
614 }
615 | term ARROW method /* $foo->bar */
616 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
617 append_elem(OP_LIST, scalar($1),
618 newUNOP(OP_METHOD, 0, $3)));
619 token_getmad($2,$$,'A');
620 }
621 | METHOD indirob listexpr /* new Class @args */
622 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
623 append_elem(OP_LIST,
624 prepend_elem(OP_LIST, $2, $3),
625 newUNOP(OP_METHOD, 0, $1)));
626 }
627 | FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
628 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
629 append_elem(OP_LIST,
630 prepend_elem(OP_LIST, $2, $4),
631 newUNOP(OP_METHOD, 0, $1)));
632 token_getmad($3,$$,'(');
633 token_getmad($5,$$,')');
634 }
635 | LSTOP listexpr /* print @args */
636 { $$ = convert(($1)->tk_lval.ival, 0, $2);
637 token_getmad($1,$$,'o');
638 }
639 | FUNC '(' listexprcom ')' /* print (@args) */
640 { $$ = convert(($1)->tk_lval.ival, 0, $3);
641 token_getmad($1,$$,'o');
642 token_getmad($2,$$,'(');
643 token_getmad($4,$$,')');
644 }
645 | LSTOPSUB startanonsub block /* map { foo } ... */
a8ff2fa6
DM
646 { SvREFCNT_inc(PL_compcv);
647 $3 = newANONATTRSUB($2, 0, Nullop, $3); }
00e74f14
NC
648 listexpr %prec LSTOP /* ... @bar */
649 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
650 append_elem(OP_LIST,
651 prepend_elem(OP_LIST, $3, $5), $1));
652 }
653 ;
654
655/* Names of methods. May use $object->$methodname */
656method : METHOD
657 | scalar
658 ;
659
660/* Some kind of subscripted expression */
661subscripted: star '{' expr ';' '}' /* *main::{something} */
662 /* In this and all the hash accessors, ';' is
663 * provided by the tokeniser */
664 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
665 PL_expect = XOPERATOR;
666 token_getmad($2,$$,'{');
667 token_getmad($4,$$,';');
668 token_getmad($5,$$,'}');
669 }
670 | scalar '[' expr ']' /* $array[$element] */
671 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
672 token_getmad($2,$$,'[');
673 token_getmad($4,$$,']');
674 }
675 | term ARROW '[' expr ']' /* somearef->[$element] */
676 { $$ = newBINOP(OP_AELEM, 0,
677 ref(newAVREF($1),OP_RV2AV),
678 scalar($4));
679 token_getmad($2,$$,'a');
680 token_getmad($3,$$,'[');
681 token_getmad($5,$$,']');
682 }
683 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
684 { $$ = newBINOP(OP_AELEM, 0,
685 ref(newAVREF($1),OP_RV2AV),
686 scalar($3));
687 token_getmad($2,$$,'[');
688 token_getmad($4,$$,']');
689 }
690 | scalar '{' expr ';' '}' /* $foo->{bar();} */
691 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
692 PL_expect = XOPERATOR;
693 token_getmad($2,$$,'{');
694 token_getmad($4,$$,';');
695 token_getmad($5,$$,'}');
696 }
697 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
698 { $$ = newBINOP(OP_HELEM, 0,
699 ref(newHVREF($1),OP_RV2HV),
700 jmaybe($4));
701 PL_expect = XOPERATOR;
702 token_getmad($2,$$,'a');
703 token_getmad($3,$$,'{');
704 token_getmad($5,$$,';');
705 token_getmad($6,$$,'}');
706 }
707 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
708 { $$ = newBINOP(OP_HELEM, 0,
709 ref(newHVREF($1),OP_RV2HV),
710 jmaybe($3));
711 PL_expect = XOPERATOR;
712 token_getmad($2,$$,'{');
713 token_getmad($4,$$,';');
714 token_getmad($5,$$,'}');
715 }
716 | term ARROW '(' ')' /* $subref->() */
717 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
718 newCVREF(0, scalar($1)));
719 token_getmad($2,$$,'a');
720 token_getmad($3,$$,'(');
721 token_getmad($4,$$,')');
722 }
723 | term ARROW '(' expr ')' /* $subref->(@args) */
724 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
725 append_elem(OP_LIST, $4,
726 newCVREF(0, scalar($1))));
727 token_getmad($2,$$,'a');
728 token_getmad($3,$$,'(');
729 token_getmad($5,$$,')');
730 }
731
732 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
733 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
734 append_elem(OP_LIST, $3,
735 newCVREF(0, scalar($1))));
736 token_getmad($2,$$,'(');
737 token_getmad($4,$$,')');
738 }
739 | subscripted '(' ')' /* $foo->{bar}->() */
740 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
741 newCVREF(0, scalar($1)));
742 token_getmad($2,$$,'(');
743 token_getmad($3,$$,')');
744 }
733849fa
NC
745 | '(' expr ')' '[' expr ']' /* list slice */
746 { $$ = newSLICEOP(0, $5, $2);
747 token_getmad($1,$$,'(');
748 token_getmad($3,$$,')');
749 token_getmad($4,$$,'[');
750 token_getmad($6,$$,']');
751 }
752 | '(' ')' '[' expr ']' /* empty list slice! */
753 { $$ = newSLICEOP(0, $4, Nullop);
754 token_getmad($1,$$,'(');
755 token_getmad($2,$$,')');
756 token_getmad($3,$$,'[');
757 token_getmad($5,$$,']');
758 }
00e74f14
NC
759 ;
760
761/* Binary operators between terms */
762termbinop: term ASSIGNOP term /* $x = $y */
763 { $$ = newASSIGNOP(OPf_STACKED, $1, ($2)->tk_lval.ival, $3);
764 token_getmad($2,$$,'o');
765 }
766 | term POWOP term /* $x ** $y */
767 { $$ = newBINOP(($2)->tk_lval.ival, 0, scalar($1), scalar($3));
768 token_getmad($2,$$,'o');
769 }
770 | term MULOP term /* $x * $y, $x x $y */
771 { if (($2)->tk_lval.ival != OP_REPEAT)
772 scalar($1);
773 $$ = newBINOP(($2)->tk_lval.ival, 0, $1, scalar($3));
774 token_getmad($2,$$,'o');
775 }
776 | term ADDOP term /* $x + $y */
777 { $$ = newBINOP(($2)->tk_lval.ival, 0, scalar($1), scalar($3));
778 token_getmad($2,$$,'o');
779 }
780 | term SHIFTOP term /* $x >> $y, $x << $y */
781 { $$ = newBINOP(($2)->tk_lval.ival, 0, scalar($1), scalar($3));
782 token_getmad($2,$$,'o');
783 }
784 | term RELOP term /* $x > $y, etc. */
785 { $$ = newBINOP(($2)->tk_lval.ival, 0, scalar($1), scalar($3));
786 token_getmad($2,$$,'o');
787 }
788 | term EQOP term /* $x == $y, $x eq $y */
789 { $$ = newBINOP(($2)->tk_lval.ival, 0, scalar($1), scalar($3));
790 token_getmad($2,$$,'o');
791 }
792 | term BITANDOP term /* $x & $y */
793 { $$ = newBINOP(($2)->tk_lval.ival, 0, scalar($1), scalar($3));
794 token_getmad($2,$$,'o');
795 }
796 | term BITOROP term /* $x | $y */
797 { $$ = newBINOP(($2)->tk_lval.ival, 0, scalar($1), scalar($3));
798 token_getmad($2,$$,'o');
799 }
800 | term DOTDOT term /* $x..$y, $x...$y */
801 { UNOP *op;
802 $$ = newRANGE(($2)->tk_lval.ival, scalar($1), scalar($3));
803 op = (UNOP*)$$;
804 op = (UNOP*)op->op_first; /* get to flop */
805 op = (UNOP*)op->op_first; /* get to flip */
806 op = (UNOP*)op->op_first; /* get to range */
807 token_getmad($2,(OP*)op,'o');
808 }
809 | term ANDAND term /* $x && $y */
810 { $$ = newLOGOP(OP_AND, 0, $1, $3);
811 token_getmad($2,$$,'o');
812 }
813 | term OROR term /* $x || $y */
814 { $$ = newLOGOP(OP_OR, 0, $1, $3);
815 token_getmad($2,$$,'o');
816 }
817 | term DORDOR term /* $x // $y */
818 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
819 token_getmad($2,$$,'o');
820 }
821 | term MATCHOP term /* $x =~ /$y/ */
822 { $$ = bind_match(($2)->tk_lval.ival, $1, $3);
823 if ($$->op_type == OP_NOT)
824 token_getmad($2,((UNOP*)$$)->op_first,'~');
825 else
826 token_getmad($2,$$,'~');
827 }
828 ;
829
830/* Unary operators and terms */
831termunop : '-' term %prec UMINUS /* -$x */
832 { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
833 token_getmad($1,$$,'o');
834 }
835 | '+' term %prec UMINUS /* +$x */
836 { $$ = newUNOP(OP_NULL, 0, $2);
837 token_getmad($1,$$,'+');
838 }
839 | '!' term /* !$x */
840 { $$ = newUNOP(OP_NOT, 0, scalar($2));
841 token_getmad($1,$$,'o');
842 }
843 | '~' term /* ~$x */
844 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
845 token_getmad($1,$$,'o');
846 }
847 | term POSTINC /* $x++ */
848 { $$ = newUNOP(OP_POSTINC, 0,
849 mod(scalar($1), OP_POSTINC));
850 token_getmad($2,$$,'o');
851 }
852 | term POSTDEC /* $x-- */
853 { $$ = newUNOP(OP_POSTDEC, 0,
854 mod(scalar($1), OP_POSTDEC));
855 token_getmad($2,$$,'o');
856 }
857 | PREINC term /* ++$x */
858 { $$ = newUNOP(OP_PREINC, 0,
859 mod(scalar($2), OP_PREINC));
860 token_getmad($1,$$,'o');
861 }
862 | PREDEC term /* --$x */
863 { $$ = newUNOP(OP_PREDEC, 0,
864 mod(scalar($2), OP_PREDEC));
865 token_getmad($1,$$,'o');
866 }
867
868 ;
869
870/* Constructors for anonymous data */
871anonymous: '[' expr ']'
872 { $$ = newANONLIST($2);
873 token_getmad($1,$$,'[');
874 token_getmad($3,$$,']');
875 }
876 | '[' ']'
877 { $$ = newANONLIST(Nullop);
878 token_getmad($1,$$,'[');
879 token_getmad($2,$$,']');
880 }
881 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
882 { $$ = newANONHASH($2);
883 token_getmad($1,$$,'{');
884 token_getmad($3,$$,';');
885 token_getmad($4,$$,'}');
886 }
887 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
888 { $$ = newANONHASH(Nullop);
889 token_getmad($1,$$,'{');
890 token_getmad($2,$$,';');
891 token_getmad($3,$$,'}');
892 }
893 | ANONSUB startanonsub proto subattrlist block %prec '('
a8ff2fa6
DM
894 { SvREFCNT_inc(PL_compcv);
895 $$ = newANONATTRSUB($2, $3, $4, $5);
00e74f14
NC
896 token_getmad($1,$$,'o');
897 op_getmad($3,$$,'s');
898 op_getmad($4,$$,'a');
899 }
900
901 ;
902
903/* Things called with "do" */
904termdo : DO term %prec UNIOP /* do $filename */
905 { $$ = dofile($2, $1);
906 token_getmad($1,$$,'o');
907 }
908 | DO block %prec '(' /* do { code */
909 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2));
910 token_getmad($1,$$,'D');
911 }
912 | DO WORD '(' ')' /* do somesub() */
913 { $$ = newUNOP(OP_ENTERSUB,
914 OPf_SPECIAL|OPf_STACKED,
915 prepend_elem(OP_LIST,
916 scalar(newCVREF(
917 (OPpENTERSUB_AMPER<<8),
918 scalar($2)
919 )),Nullop)); dep();
920 token_getmad($1,$$,'o');
921 token_getmad($3,$$,'(');
922 token_getmad($4,$$,')');
923 }
924 | DO WORD '(' expr ')' /* do somesub(@args) */
925 { $$ = newUNOP(OP_ENTERSUB,
926 OPf_SPECIAL|OPf_STACKED,
927 append_elem(OP_LIST,
928 $4,
929 scalar(newCVREF(
930 (OPpENTERSUB_AMPER<<8),
931 scalar($2)
932 )))); dep();
933 token_getmad($1,$$,'o');
934 token_getmad($3,$$,'(');
935 token_getmad($5,$$,')');
936 }
937 | DO scalar '(' ')' /* do $subref () */
938 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
939 prepend_elem(OP_LIST,
940 scalar(newCVREF(0,scalar($2))), Nullop)); dep();
941 token_getmad($1,$$,'o');
942 token_getmad($3,$$,'(');
943 token_getmad($4,$$,')');
944 }
945 | DO scalar '(' expr ')' /* do $subref (@args) */
946 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
947 prepend_elem(OP_LIST,
948 $4,
949 scalar(newCVREF(0,scalar($2))))); dep();
950 token_getmad($1,$$,'o');
951 token_getmad($3,$$,'(');
952 token_getmad($5,$$,')');
953 }
954
955 ;
956
957term : termbinop
958 | termunop
959 | anonymous
960 | termdo
961 | term '?' term ':' term
962 { $$ = newCONDOP(0, $1, $3, $5);
963 token_getmad($2,$$,'?');
964 token_getmad($4,$$,':');
965 }
966 | REFGEN term /* \$x, \@y, \%z */
967 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN));
968 token_getmad($1,$$,'o');
969 }
970 | myattrterm %prec UNIOP
971 { $$ = $1; }
972 | LOCAL term %prec UNIOP
973 { $$ = localize($2,($1)->tk_lval.ival);
974 token_getmad($1,$$,'d');
975 }
976 | '(' expr ')'
977 { $$ = sawparens(newUNOP(OP_NULL,0,$2));
978 token_getmad($1,$$,'(');
979 token_getmad($3,$$,')');
980 }
981 | '(' ')'
982 { $$ = sawparens(newNULLLIST());
983 token_getmad($1,$$,'(');
984 token_getmad($2,$$,')');
985 }
986 | scalar %prec '('
987 { $$ = $1; }
988 | star %prec '('
989 { $$ = $1; }
990 | hsh %prec '('
991 { $$ = $1; }
992 | ary %prec '('
993 { $$ = $1; }
994 | arylen %prec '(' /* $#x, $#{ something } */
995 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
996 | subscripted
997 { $$ = $1; }
00e74f14
NC
998 | ary '[' expr ']' /* array slice */
999 { $$ = prepend_elem(OP_ASLICE,
1000 newOP(OP_PUSHMARK, 0),
1001 newLISTOP(OP_ASLICE, 0,
1002 list($3),
1003 ref($1, OP_ASLICE)));
1004 token_getmad($2,$$,'[');
1005 token_getmad($4,$$,']');
1006 }
1007 | ary '{' expr ';' '}' /* @hash{@keys} */
1008 { $$ = prepend_elem(OP_HSLICE,
1009 newOP(OP_PUSHMARK, 0),
1010 newLISTOP(OP_HSLICE, 0,
1011 list($3),
1012 ref(oopsHV($1), OP_HSLICE)));
1013 PL_expect = XOPERATOR;
1014 token_getmad($2,$$,'{');
1015 token_getmad($4,$$,';');
1016 token_getmad($5,$$,'}');
1017 }
1018 | THING %prec '('
1019 { $$ = $1; }
1020 | amper /* &foo; */
1021 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1022 | amper '(' ')' /* &foo() */
1023 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1024 token_getmad($2,$$,'(');
1025 token_getmad($3,$$,')');
1026 }
1027 | amper '(' expr ')' /* &foo(@args) */
1028 { OP* op;
1029 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1030 append_elem(OP_LIST, $3, scalar($1)));
1031 op = $$;
1032 if (op->op_type == OP_CONST) { /* defeat const fold */
1033 op = (OP*)op->op_madprop->mad_val;
1034 }
1035 token_getmad($2,op,'(');
1036 token_getmad($4,op,')');
1037 }
1038 | NOAMP WORD listexpr /* foo(@args) */
1039 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1040 append_elem(OP_LIST, $3, scalar($2)));
1041 token_getmad($1,$$,'o');
1042 }
1043 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
1044 { $$ = newOP(($1)->tk_lval.ival, OPf_SPECIAL);
1045 PL_hints |= HINT_BLOCK_SCOPE;
1046 token_getmad($1,$$,'o');
1047 }
1048 | LOOPEX term
1049 { $$ = newLOOPEX(($1)->tk_lval.ival,$2);
1050 token_getmad($1,$$,'o');
1051 }
1052 | NOTOP argexpr /* not $foo */
1053 { $$ = newUNOP(OP_NOT, 0, scalar($2));
1054 token_getmad($1,$$,'o');
1055 }
1056 | UNIOP /* Unary op, $_ implied */
1057 { $$ = newOP(($1)->tk_lval.ival, 0);
1058 token_getmad($1,$$,'o');
1059 }
1060 | UNIOP block /* eval { foo }, I *think* */
1061 { $$ = newUNOP(($1)->tk_lval.ival, 0, $2);
1062 token_getmad($1,$$,'o');
1063 }
1064 | UNIOP term /* Unary op */
1065 { $$ = newUNOP(($1)->tk_lval.ival, 0, $2);
1066 token_getmad($1,$$,'o');
1067 }
733849fa
NC
1068 | REQUIRE /* require, $_ implied *//* FIMXE for MAD needed? */
1069 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); }
1070 | REQUIRE term /* require Foo *//* FIMXE for MAD needed? */
1071 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); }
00e74f14
NC
1072 | UNIOPSUB term /* Sub treated as unop */
1073 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
733849fa 1074 append_elem(OP_LIST, $2, scalar($1))); }
00e74f14
NC
1075 | FUNC0 /* Nullary operator */
1076 { $$ = newOP(($1)->tk_lval.ival, 0);
1077 token_getmad($1,$$,'o');
1078 }
1079 | FUNC0 '(' ')'
1080 { $$ = newOP(($1)->tk_lval.ival, 0);
1081 token_getmad($1,$$,'o');
1082 token_getmad($2,$$,'(');
1083 token_getmad($3,$$,')');
1084 }
1085 | FUNC0SUB /* Sub treated as nullop */
1086 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1087 scalar($1)); }
1088 | FUNC1 '(' ')' /* not () */
1089 { $$ = newOP(($1)->tk_lval.ival, OPf_SPECIAL);
1090 token_getmad($1,$$,'o');
1091 token_getmad($2,$$,'(');
1092 token_getmad($3,$$,')');
1093 }
1094 | FUNC1 '(' expr ')' /* not($foo) */
1095 { $$ = newUNOP(($1)->tk_lval.ival, 0, $3);
1096 token_getmad($1,$$,'o');
1097 token_getmad($2,$$,'(');
1098 token_getmad($4,$$,')');
1099 }
1100 | PMFUNC '(' argexpr ')' /* m//, s///, tr/// */
1101 { $$ = pmruntime($1, $3, 1);
1102 token_getmad($2,$$,'(');
1103 token_getmad($4,$$,')');
1104 }
1105 | WORD
1106 | listop
1107 ;
1108
1109/* "my" declarations, with optional attributes */
1110myattrterm: MY myterm myattrlist
1111 { $$ = my_attrs($2,$3);
1112 token_getmad($1,$$,'d');
1113 append_madprops($3->op_madprop, $$, 'a');
1114 $3->op_madprop = 0;
1115 }
1116 | MY myterm
1117 { $$ = localize($2,($1)->tk_lval.ival);
1118 token_getmad($1,$$,'d');
1119 }
1120 ;
1121
1122/* Things that can be "my"'d */
1123myterm : '(' expr ')'
1124 { $$ = sawparens($2);
1125 token_getmad($1,$$,'(');
1126 token_getmad($3,$$,')');
1127 }
1128 | '(' ')'
1129 { $$ = sawparens(newNULLLIST());
1130 token_getmad($1,$$,'(');
1131 token_getmad($2,$$,')');
1132 }
1133 | scalar %prec '('
1134 { $$ = $1; }
1135 | hsh %prec '('
1136 { $$ = $1; }
1137 | ary %prec '('
1138 { $$ = $1; }
1139 ;
1140
1141/* Basic list expressions */
1142listexpr: /* NULL */ %prec PREC_LOW
1143 { $$ = Nullop; }
1144 | argexpr %prec PREC_LOW
1145 { $$ = $1; }
1146 ;
1147
1148listexprcom: /* NULL */
1149 { $$ = Nullop; }
1150 | expr
1151 { $$ = $1; }
1152 | expr ','
1153 { OP* op = newNULLLIST();
1154 token_getmad($2,op,',');
1155 $$ = append_elem(OP_LIST, $1, op);
1156 }
1157 ;
1158
1159/* A little bit of trickery to make "for my $foo (@bar)" actually be
1160 lexical */
1161my_scalar: scalar
1162 { PL_in_my = 0; $$ = my($1); }
1163 ;
1164
1165amper : '&' indirob
1166 { $$ = newCVREF(($1)->tk_lval.ival,$2);
1167 token_getmad($1,$$,'&');
1168 }
1169 ;
1170
1171scalar : '$' indirob
1172 { $$ = newSVREF($2);
1173 token_getmad($1,$$,'$');
1174 }
1175 ;
1176
1177ary : '@' indirob
1178 { $$ = newAVREF($2);
1179 token_getmad($1,$$,'@');
1180 }
1181 ;
1182
1183hsh : '%' indirob
1184 { $$ = newHVREF($2);
1185 token_getmad($1,$$,'%');
1186 }
1187 ;
1188
1189arylen : DOLSHARP indirob
1190 { $$ = newAVREF($2);
1191 token_getmad($1,$$,'l');
1192 }
1193 ;
1194
1195star : '*' indirob
1196 { $$ = newGVREF(0,$2);
1197 token_getmad($1,$$,'*');
1198 }
1199 ;
1200
1201/* Indirect objects */
1202indirob : WORD
1203 { $$ = scalar($1); }
1204 | scalar %prec PREC_LOW
1205 { $$ = scalar($1); }
1206 | block
1207 { $$ = scope($1); }
1208
1209 | PRIVATEREF
1210 { $$ = $1; }
1211 ;