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