This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: install misses Compress/IO/{Base,Zlib}
[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 500 if (strEQ(name, "BEGIN") || strEQ(name, "END")
3c10abe3
AG
501 || strEQ(name, "INIT") || strEQ(name, "CHECK")
502 || strEQ(name, "UNITCHECK"))
00e74f14
NC
503 CvSPECIAL_on(PL_compcv);
504 $$ = $1; }
505 ;
506
507/* Subroutine prototype */
508proto : /* NULL */
509 { $$ = Nullop; }
510 | THING
511 ;
512
513/* Optional list of subroutine attributes */
514subattrlist: /* NULL */
515 { $$ = Nullop; }
516 | COLONATTR THING
517 { $$ = $2;
518 token_getmad($1,$$,':');
519 }
520 | COLONATTR
521 { $$ = newOP(OP_NULL, 0);
522 token_getmad($1,$$,':');
523 }
524 ;
525
526/* List of attributes for a "my" variable declaration */
527myattrlist: COLONATTR THING
528 { $$ = $2;
529 token_getmad($1,$$,':');
530 }
531 | COLONATTR
532 { $$ = newOP(OP_NULL, 0);
533 token_getmad($1,$$,':');
534 }
535 ;
536
537/* Subroutine body - either null or a block */
538subbody : block { $$ = $1; }
539 | ';' { $$ = newOP(OP_NULL,0); PL_expect = XSTATE;
540 token_getmad($1,$$,';');
541 }
542 ;
543
544package : PACKAGE WORD ';'
545 { $$ = package($2);
546 token_getmad($1,$$,'o');
547 token_getmad($3,$$,';');
548 }
549 ;
550
551use : USE startsub
552 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
553 WORD WORD listexpr ';'
a8ff2fa6
DM
554 { SvREFCNT_inc(PL_compcv);
555 $$ = utilize(($1)->tk_lval.ival, $2, $4, $5, $6);
00e74f14
NC
556 token_getmad($1,$$,'o');
557 token_getmad($7,$$,';');
558 if (PL_rsfp_filters && AvFILLp(PL_rsfp_filters) >= 0)
559 append_madprops(newMADPROP('!', MAD_PV, "", 0), $$, 0);
560 }
561 ;
562
563/* Ordinary expressions; logical combinations */
564expr : expr ANDOP expr
565 { $$ = newLOGOP(OP_AND, 0, $1, $3);
566 token_getmad($2,$$,'o');
567 }
568 | expr OROP expr
569 { $$ = newLOGOP(($2)->tk_lval.ival, 0, $1, $3);
570 token_getmad($2,$$,'o');
571 }
572 | expr DOROP expr
573 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
574 token_getmad($2,$$,'o');
575 }
576 | argexpr %prec PREC_LOW
577 ;
578
579/* Expressions are a list of terms joined by commas */
580argexpr : argexpr ','
581 { OP* op = newNULLLIST();
582 token_getmad($2,op,',');
583 $$ = append_elem(OP_LIST, $1, op);
584 }
585 | argexpr ',' term
586 {
587 $3 = newUNOP(OP_NULL, 0, $3);
588 token_getmad($2,$3,',');
589 $$ = append_elem(OP_LIST, $1, $3);
590 }
591 | term %prec PREC_LOW
592 ;
593
594/* List operators */
595listop : LSTOP indirob argexpr /* print $fh @args */
596 { $$ = convert(($1)->tk_lval.ival, OPf_STACKED,
597 prepend_elem(OP_LIST, newGVREF(($1)->tk_lval.ival,$2), $3) );
598 token_getmad($1,$$,'o');
599 }
600 | FUNC '(' indirob expr ')' /* print ($fh @args */
601 { $$ = convert(($1)->tk_lval.ival, OPf_STACKED,
602 prepend_elem(OP_LIST, newGVREF(($1)->tk_lval.ival,$3), $4) );
603 token_getmad($1,$$,'o');
604 token_getmad($2,$$,'(');
605 token_getmad($5,$$,')');
606 }
607 | term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
608 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
609 append_elem(OP_LIST,
610 prepend_elem(OP_LIST, scalar($1), $5),
611 newUNOP(OP_METHOD, 0, $3)));
612 token_getmad($2,$$,'A');
613 token_getmad($4,$$,'(');
614 token_getmad($6,$$,')');
615 }
616 | term ARROW method /* $foo->bar */
617 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
618 append_elem(OP_LIST, scalar($1),
619 newUNOP(OP_METHOD, 0, $3)));
620 token_getmad($2,$$,'A');
621 }
622 | METHOD indirob listexpr /* new Class @args */
623 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
624 append_elem(OP_LIST,
625 prepend_elem(OP_LIST, $2, $3),
626 newUNOP(OP_METHOD, 0, $1)));
627 }
628 | FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
629 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
630 append_elem(OP_LIST,
631 prepend_elem(OP_LIST, $2, $4),
632 newUNOP(OP_METHOD, 0, $1)));
633 token_getmad($3,$$,'(');
634 token_getmad($5,$$,')');
635 }
636 | LSTOP listexpr /* print @args */
637 { $$ = convert(($1)->tk_lval.ival, 0, $2);
638 token_getmad($1,$$,'o');
639 }
640 | FUNC '(' listexprcom ')' /* print (@args) */
641 { $$ = convert(($1)->tk_lval.ival, 0, $3);
642 token_getmad($1,$$,'o');
643 token_getmad($2,$$,'(');
644 token_getmad($4,$$,')');
645 }
646 | LSTOPSUB startanonsub block /* map { foo } ... */
a8ff2fa6
DM
647 { SvREFCNT_inc(PL_compcv);
648 $3 = newANONATTRSUB($2, 0, Nullop, $3); }
00e74f14
NC
649 listexpr %prec LSTOP /* ... @bar */
650 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
651 append_elem(OP_LIST,
652 prepend_elem(OP_LIST, $3, $5), $1));
653 }
654 ;
655
656/* Names of methods. May use $object->$methodname */
657method : METHOD
658 | scalar
659 ;
660
661/* Some kind of subscripted expression */
662subscripted: star '{' expr ';' '}' /* *main::{something} */
663 /* In this and all the hash accessors, ';' is
664 * provided by the tokeniser */
665 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
666 PL_expect = XOPERATOR;
667 token_getmad($2,$$,'{');
668 token_getmad($4,$$,';');
669 token_getmad($5,$$,'}');
670 }
671 | scalar '[' expr ']' /* $array[$element] */
672 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
673 token_getmad($2,$$,'[');
674 token_getmad($4,$$,']');
675 }
676 | term ARROW '[' expr ']' /* somearef->[$element] */
677 { $$ = newBINOP(OP_AELEM, 0,
678 ref(newAVREF($1),OP_RV2AV),
679 scalar($4));
680 token_getmad($2,$$,'a');
681 token_getmad($3,$$,'[');
682 token_getmad($5,$$,']');
683 }
684 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
685 { $$ = newBINOP(OP_AELEM, 0,
686 ref(newAVREF($1),OP_RV2AV),
687 scalar($3));
688 token_getmad($2,$$,'[');
689 token_getmad($4,$$,']');
690 }
691 | scalar '{' expr ';' '}' /* $foo->{bar();} */
692 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
693 PL_expect = XOPERATOR;
694 token_getmad($2,$$,'{');
695 token_getmad($4,$$,';');
696 token_getmad($5,$$,'}');
697 }
698 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
699 { $$ = newBINOP(OP_HELEM, 0,
700 ref(newHVREF($1),OP_RV2HV),
701 jmaybe($4));
702 PL_expect = XOPERATOR;
703 token_getmad($2,$$,'a');
704 token_getmad($3,$$,'{');
705 token_getmad($5,$$,';');
706 token_getmad($6,$$,'}');
707 }
708 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
709 { $$ = newBINOP(OP_HELEM, 0,
710 ref(newHVREF($1),OP_RV2HV),
711 jmaybe($3));
712 PL_expect = XOPERATOR;
713 token_getmad($2,$$,'{');
714 token_getmad($4,$$,';');
715 token_getmad($5,$$,'}');
716 }
717 | term ARROW '(' ')' /* $subref->() */
718 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
719 newCVREF(0, scalar($1)));
720 token_getmad($2,$$,'a');
721 token_getmad($3,$$,'(');
722 token_getmad($4,$$,')');
723 }
724 | term ARROW '(' expr ')' /* $subref->(@args) */
725 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
726 append_elem(OP_LIST, $4,
727 newCVREF(0, scalar($1))));
728 token_getmad($2,$$,'a');
729 token_getmad($3,$$,'(');
730 token_getmad($5,$$,')');
731 }
732
733 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
734 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
735 append_elem(OP_LIST, $3,
736 newCVREF(0, scalar($1))));
737 token_getmad($2,$$,'(');
738 token_getmad($4,$$,')');
739 }
740 | subscripted '(' ')' /* $foo->{bar}->() */
741 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
742 newCVREF(0, scalar($1)));
743 token_getmad($2,$$,'(');
744 token_getmad($3,$$,')');
745 }
733849fa
NC
746 | '(' expr ')' '[' expr ']' /* list slice */
747 { $$ = newSLICEOP(0, $5, $2);
748 token_getmad($1,$$,'(');
749 token_getmad($3,$$,')');
750 token_getmad($4,$$,'[');
751 token_getmad($6,$$,']');
752 }
753 | '(' ')' '[' expr ']' /* empty list slice! */
754 { $$ = newSLICEOP(0, $4, Nullop);
755 token_getmad($1,$$,'(');
756 token_getmad($2,$$,')');
757 token_getmad($3,$$,'[');
758 token_getmad($5,$$,']');
759 }
00e74f14
NC
760 ;
761
762/* Binary operators between terms */
763termbinop: term ASSIGNOP term /* $x = $y */
764 { $$ = newASSIGNOP(OPf_STACKED, $1, ($2)->tk_lval.ival, $3);
765 token_getmad($2,$$,'o');
766 }
767 | term POWOP term /* $x ** $y */
768 { $$ = newBINOP(($2)->tk_lval.ival, 0, scalar($1), scalar($3));
769 token_getmad($2,$$,'o');
770 }
771 | term MULOP term /* $x * $y, $x x $y */
772 { if (($2)->tk_lval.ival != OP_REPEAT)
773 scalar($1);
774 $$ = newBINOP(($2)->tk_lval.ival, 0, $1, scalar($3));
775 token_getmad($2,$$,'o');
776 }
777 | term ADDOP term /* $x + $y */
778 { $$ = newBINOP(($2)->tk_lval.ival, 0, scalar($1), scalar($3));
779 token_getmad($2,$$,'o');
780 }
781 | term SHIFTOP term /* $x >> $y, $x << $y */
782 { $$ = newBINOP(($2)->tk_lval.ival, 0, scalar($1), scalar($3));
783 token_getmad($2,$$,'o');
784 }
785 | term RELOP term /* $x > $y, etc. */
786 { $$ = newBINOP(($2)->tk_lval.ival, 0, scalar($1), scalar($3));
787 token_getmad($2,$$,'o');
788 }
789 | term EQOP term /* $x == $y, $x eq $y */
790 { $$ = newBINOP(($2)->tk_lval.ival, 0, scalar($1), scalar($3));
791 token_getmad($2,$$,'o');
792 }
793 | term BITANDOP term /* $x & $y */
794 { $$ = newBINOP(($2)->tk_lval.ival, 0, scalar($1), scalar($3));
795 token_getmad($2,$$,'o');
796 }
797 | term BITOROP term /* $x | $y */
798 { $$ = newBINOP(($2)->tk_lval.ival, 0, scalar($1), scalar($3));
799 token_getmad($2,$$,'o');
800 }
801 | term DOTDOT term /* $x..$y, $x...$y */
802 { UNOP *op;
803 $$ = newRANGE(($2)->tk_lval.ival, scalar($1), scalar($3));
804 op = (UNOP*)$$;
805 op = (UNOP*)op->op_first; /* get to flop */
806 op = (UNOP*)op->op_first; /* get to flip */
807 op = (UNOP*)op->op_first; /* get to range */
808 token_getmad($2,(OP*)op,'o');
809 }
810 | term ANDAND term /* $x && $y */
811 { $$ = newLOGOP(OP_AND, 0, $1, $3);
812 token_getmad($2,$$,'o');
813 }
814 | term OROR term /* $x || $y */
815 { $$ = newLOGOP(OP_OR, 0, $1, $3);
816 token_getmad($2,$$,'o');
817 }
818 | term DORDOR term /* $x // $y */
819 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
820 token_getmad($2,$$,'o');
821 }
822 | term MATCHOP term /* $x =~ /$y/ */
823 { $$ = bind_match(($2)->tk_lval.ival, $1, $3);
824 if ($$->op_type == OP_NOT)
825 token_getmad($2,((UNOP*)$$)->op_first,'~');
826 else
827 token_getmad($2,$$,'~');
828 }
829 ;
830
831/* Unary operators and terms */
832termunop : '-' term %prec UMINUS /* -$x */
833 { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
834 token_getmad($1,$$,'o');
835 }
836 | '+' term %prec UMINUS /* +$x */
837 { $$ = newUNOP(OP_NULL, 0, $2);
838 token_getmad($1,$$,'+');
839 }
840 | '!' term /* !$x */
841 { $$ = newUNOP(OP_NOT, 0, scalar($2));
842 token_getmad($1,$$,'o');
843 }
844 | '~' term /* ~$x */
845 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
846 token_getmad($1,$$,'o');
847 }
848 | term POSTINC /* $x++ */
849 { $$ = newUNOP(OP_POSTINC, 0,
850 mod(scalar($1), OP_POSTINC));
851 token_getmad($2,$$,'o');
852 }
853 | term POSTDEC /* $x-- */
854 { $$ = newUNOP(OP_POSTDEC, 0,
855 mod(scalar($1), OP_POSTDEC));
856 token_getmad($2,$$,'o');
857 }
858 | PREINC term /* ++$x */
859 { $$ = newUNOP(OP_PREINC, 0,
860 mod(scalar($2), OP_PREINC));
861 token_getmad($1,$$,'o');
862 }
863 | PREDEC term /* --$x */
864 { $$ = newUNOP(OP_PREDEC, 0,
865 mod(scalar($2), OP_PREDEC));
866 token_getmad($1,$$,'o');
867 }
868
869 ;
870
871/* Constructors for anonymous data */
872anonymous: '[' expr ']'
873 { $$ = newANONLIST($2);
874 token_getmad($1,$$,'[');
875 token_getmad($3,$$,']');
876 }
877 | '[' ']'
878 { $$ = newANONLIST(Nullop);
879 token_getmad($1,$$,'[');
880 token_getmad($2,$$,']');
881 }
882 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
883 { $$ = newANONHASH($2);
884 token_getmad($1,$$,'{');
885 token_getmad($3,$$,';');
886 token_getmad($4,$$,'}');
887 }
888 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
889 { $$ = newANONHASH(Nullop);
890 token_getmad($1,$$,'{');
891 token_getmad($2,$$,';');
892 token_getmad($3,$$,'}');
893 }
894 | ANONSUB startanonsub proto subattrlist block %prec '('
a8ff2fa6
DM
895 { SvREFCNT_inc(PL_compcv);
896 $$ = newANONATTRSUB($2, $3, $4, $5);
00e74f14
NC
897 token_getmad($1,$$,'o');
898 op_getmad($3,$$,'s');
899 op_getmad($4,$$,'a');
900 }
901
902 ;
903
904/* Things called with "do" */
905termdo : DO term %prec UNIOP /* do $filename */
906 { $$ = dofile($2, $1);
907 token_getmad($1,$$,'o');
908 }
909 | DO block %prec '(' /* do { code */
910 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2));
911 token_getmad($1,$$,'D');
912 }
913 | DO WORD '(' ')' /* do somesub() */
914 { $$ = newUNOP(OP_ENTERSUB,
915 OPf_SPECIAL|OPf_STACKED,
916 prepend_elem(OP_LIST,
917 scalar(newCVREF(
918 (OPpENTERSUB_AMPER<<8),
919 scalar($2)
920 )),Nullop)); dep();
921 token_getmad($1,$$,'o');
922 token_getmad($3,$$,'(');
923 token_getmad($4,$$,')');
924 }
925 | DO WORD '(' expr ')' /* do somesub(@args) */
926 { $$ = newUNOP(OP_ENTERSUB,
927 OPf_SPECIAL|OPf_STACKED,
928 append_elem(OP_LIST,
929 $4,
930 scalar(newCVREF(
931 (OPpENTERSUB_AMPER<<8),
932 scalar($2)
933 )))); dep();
934 token_getmad($1,$$,'o');
935 token_getmad($3,$$,'(');
936 token_getmad($5,$$,')');
937 }
938 | DO scalar '(' ')' /* do $subref () */
939 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
940 prepend_elem(OP_LIST,
941 scalar(newCVREF(0,scalar($2))), Nullop)); dep();
942 token_getmad($1,$$,'o');
943 token_getmad($3,$$,'(');
944 token_getmad($4,$$,')');
945 }
946 | DO scalar '(' expr ')' /* do $subref (@args) */
947 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
948 prepend_elem(OP_LIST,
949 $4,
950 scalar(newCVREF(0,scalar($2))))); dep();
951 token_getmad($1,$$,'o');
952 token_getmad($3,$$,'(');
953 token_getmad($5,$$,')');
954 }
955
956 ;
957
958term : termbinop
959 | termunop
960 | anonymous
961 | termdo
962 | term '?' term ':' term
963 { $$ = newCONDOP(0, $1, $3, $5);
964 token_getmad($2,$$,'?');
965 token_getmad($4,$$,':');
966 }
967 | REFGEN term /* \$x, \@y, \%z */
968 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN));
969 token_getmad($1,$$,'o');
970 }
971 | myattrterm %prec UNIOP
972 { $$ = $1; }
973 | LOCAL term %prec UNIOP
974 { $$ = localize($2,($1)->tk_lval.ival);
975 token_getmad($1,$$,'d');
976 }
977 | '(' expr ')'
978 { $$ = sawparens(newUNOP(OP_NULL,0,$2));
979 token_getmad($1,$$,'(');
980 token_getmad($3,$$,')');
981 }
982 | '(' ')'
983 { $$ = sawparens(newNULLLIST());
984 token_getmad($1,$$,'(');
985 token_getmad($2,$$,')');
986 }
987 | scalar %prec '('
988 { $$ = $1; }
989 | star %prec '('
990 { $$ = $1; }
991 | hsh %prec '('
992 { $$ = $1; }
993 | ary %prec '('
994 { $$ = $1; }
995 | arylen %prec '(' /* $#x, $#{ something } */
996 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
997 | subscripted
998 { $$ = $1; }
00e74f14
NC
999 | ary '[' expr ']' /* array slice */
1000 { $$ = prepend_elem(OP_ASLICE,
1001 newOP(OP_PUSHMARK, 0),
1002 newLISTOP(OP_ASLICE, 0,
1003 list($3),
1004 ref($1, OP_ASLICE)));
1005 token_getmad($2,$$,'[');
1006 token_getmad($4,$$,']');
1007 }
1008 | ary '{' expr ';' '}' /* @hash{@keys} */
1009 { $$ = prepend_elem(OP_HSLICE,
1010 newOP(OP_PUSHMARK, 0),
1011 newLISTOP(OP_HSLICE, 0,
1012 list($3),
1013 ref(oopsHV($1), OP_HSLICE)));
1014 PL_expect = XOPERATOR;
1015 token_getmad($2,$$,'{');
1016 token_getmad($4,$$,';');
1017 token_getmad($5,$$,'}');
1018 }
1019 | THING %prec '('
1020 { $$ = $1; }
1021 | amper /* &foo; */
1022 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1023 | amper '(' ')' /* &foo() */
1024 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1025 token_getmad($2,$$,'(');
1026 token_getmad($3,$$,')');
1027 }
1028 | amper '(' expr ')' /* &foo(@args) */
1029 { OP* op;
1030 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1031 append_elem(OP_LIST, $3, scalar($1)));
1032 op = $$;
1033 if (op->op_type == OP_CONST) { /* defeat const fold */
1034 op = (OP*)op->op_madprop->mad_val;
1035 }
1036 token_getmad($2,op,'(');
1037 token_getmad($4,op,')');
1038 }
1039 | NOAMP WORD listexpr /* foo(@args) */
1040 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1041 append_elem(OP_LIST, $3, scalar($2)));
1042 token_getmad($1,$$,'o');
1043 }
1044 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
1045 { $$ = newOP(($1)->tk_lval.ival, OPf_SPECIAL);
1046 PL_hints |= HINT_BLOCK_SCOPE;
1047 token_getmad($1,$$,'o');
1048 }
1049 | LOOPEX term
1050 { $$ = newLOOPEX(($1)->tk_lval.ival,$2);
1051 token_getmad($1,$$,'o');
1052 }
1053 | NOTOP argexpr /* not $foo */
1054 { $$ = newUNOP(OP_NOT, 0, scalar($2));
1055 token_getmad($1,$$,'o');
1056 }
1057 | UNIOP /* Unary op, $_ implied */
1058 { $$ = newOP(($1)->tk_lval.ival, 0);
1059 token_getmad($1,$$,'o');
1060 }
1061 | UNIOP block /* eval { foo }, I *think* */
1062 { $$ = newUNOP(($1)->tk_lval.ival, 0, $2);
1063 token_getmad($1,$$,'o');
1064 }
1065 | UNIOP term /* Unary op */
1066 { $$ = newUNOP(($1)->tk_lval.ival, 0, $2);
1067 token_getmad($1,$$,'o');
1068 }
733849fa
NC
1069 | REQUIRE /* require, $_ implied *//* FIMXE for MAD needed? */
1070 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); }
1071 | REQUIRE term /* require Foo *//* FIMXE for MAD needed? */
1072 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); }
3cd0a11a
RGS
1073 | UNIOPSUB
1074 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
00e74f14
NC
1075 | UNIOPSUB term /* Sub treated as unop */
1076 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
733849fa 1077 append_elem(OP_LIST, $2, scalar($1))); }
00e74f14
NC
1078 | FUNC0 /* Nullary operator */
1079 { $$ = newOP(($1)->tk_lval.ival, 0);
1080 token_getmad($1,$$,'o');
1081 }
1082 | FUNC0 '(' ')'
1083 { $$ = newOP(($1)->tk_lval.ival, 0);
1084 token_getmad($1,$$,'o');
1085 token_getmad($2,$$,'(');
1086 token_getmad($3,$$,')');
1087 }
1088 | FUNC0SUB /* Sub treated as nullop */
1089 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1090 scalar($1)); }
1091 | FUNC1 '(' ')' /* not () */
1092 { $$ = newOP(($1)->tk_lval.ival, OPf_SPECIAL);
1093 token_getmad($1,$$,'o');
1094 token_getmad($2,$$,'(');
1095 token_getmad($3,$$,')');
1096 }
1097 | FUNC1 '(' expr ')' /* not($foo) */
1098 { $$ = newUNOP(($1)->tk_lval.ival, 0, $3);
1099 token_getmad($1,$$,'o');
1100 token_getmad($2,$$,'(');
1101 token_getmad($4,$$,')');
1102 }
1103 | PMFUNC '(' argexpr ')' /* m//, s///, tr/// */
1104 { $$ = pmruntime($1, $3, 1);
1105 token_getmad($2,$$,'(');
1106 token_getmad($4,$$,')');
1107 }
1108 | WORD
1109 | listop
1110 ;
1111
1112/* "my" declarations, with optional attributes */
1113myattrterm: MY myterm myattrlist
1114 { $$ = my_attrs($2,$3);
1115 token_getmad($1,$$,'d');
1116 append_madprops($3->op_madprop, $$, 'a');
1117 $3->op_madprop = 0;
1118 }
1119 | MY myterm
1120 { $$ = localize($2,($1)->tk_lval.ival);
1121 token_getmad($1,$$,'d');
1122 }
1123 ;
1124
1125/* Things that can be "my"'d */
1126myterm : '(' expr ')'
1127 { $$ = sawparens($2);
1128 token_getmad($1,$$,'(');
1129 token_getmad($3,$$,')');
1130 }
1131 | '(' ')'
1132 { $$ = sawparens(newNULLLIST());
1133 token_getmad($1,$$,'(');
1134 token_getmad($2,$$,')');
1135 }
1136 | scalar %prec '('
1137 { $$ = $1; }
1138 | hsh %prec '('
1139 { $$ = $1; }
1140 | ary %prec '('
1141 { $$ = $1; }
1142 ;
1143
1144/* Basic list expressions */
1145listexpr: /* NULL */ %prec PREC_LOW
1146 { $$ = Nullop; }
1147 | argexpr %prec PREC_LOW
1148 { $$ = $1; }
1149 ;
1150
1151listexprcom: /* NULL */
1152 { $$ = Nullop; }
1153 | expr
1154 { $$ = $1; }
1155 | expr ','
1156 { OP* op = newNULLLIST();
1157 token_getmad($2,op,',');
1158 $$ = append_elem(OP_LIST, $1, op);
1159 }
1160 ;
1161
1162/* A little bit of trickery to make "for my $foo (@bar)" actually be
1163 lexical */
1164my_scalar: scalar
1165 { PL_in_my = 0; $$ = my($1); }
1166 ;
1167
1168amper : '&' indirob
1169 { $$ = newCVREF(($1)->tk_lval.ival,$2);
1170 token_getmad($1,$$,'&');
1171 }
1172 ;
1173
1174scalar : '$' indirob
1175 { $$ = newSVREF($2);
1176 token_getmad($1,$$,'$');
1177 }
1178 ;
1179
1180ary : '@' indirob
1181 { $$ = newAVREF($2);
1182 token_getmad($1,$$,'@');
1183 }
1184 ;
1185
1186hsh : '%' indirob
1187 { $$ = newHVREF($2);
1188 token_getmad($1,$$,'%');
1189 }
1190 ;
1191
1192arylen : DOLSHARP indirob
1193 { $$ = newAVREF($2);
1194 token_getmad($1,$$,'l');
1195 }
1196 ;
1197
1198star : '*' indirob
1199 { $$ = newGVREF(0,$2);
1200 token_getmad($1,$$,'*');
1201 }
1202 ;
1203
1204/* Indirect objects */
1205indirob : WORD
1206 { $$ = scalar($1); }
1207 | scalar %prec PREC_LOW
1208 { $$ = scalar($1); }
1209 | block
1210 { $$ = scope($1); }
1211
1212 | PRIVATEREF
1213 { $$ = $1; }
1214 ;