This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate Memoize 0.64. Few tweaks were required in
[perl5.git] / perly.y
1 /*    perly.y
2  *
3  *    Copyright (c) 1991-2001, 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 %{
16 #include "EXTERN.h"
17 #define PERL_IN_PERLY_C
18 #include "perl.h"
19 #ifdef EBCDIC
20 #undef YYDEBUG
21 #endif
22 #define dep() deprecate("\"do\" to call subroutines")
23
24 /* stuff included here to make perly_c.diff apply better */
25
26 #define yydebug     PL_yydebug
27 #define yynerrs     PL_yynerrs
28 #define yyerrflag   PL_yyerrflag
29 #define yychar      PL_yychar
30 #define yyval       PL_yyval
31 #define yylval      PL_yylval
32
33 struct ysv {
34     short* yyss;
35     YYSTYPE* yyvs;
36     int oldyydebug;
37     int oldyynerrs;
38     int oldyyerrflag;
39     int oldyychar;
40     YYSTYPE oldyyval;
41     YYSTYPE oldyylval;
42 };
43
44 static void yydestruct(pTHXo_ void *ptr);
45
46 %}
47
48 %start prog
49
50 %{
51 #if 0 /* get this from perly.h instead */
52 %}
53
54 %union {
55     I32 ival;
56     char *pval;
57     OP *opval;
58     GV *gvval;
59 }
60
61 %{
62 #endif /* 0 */
63
64 #ifdef USE_PURE_BISON
65 #define YYLEX_PARAM (&yychar)
66 #define yylex yylex_r
67 #endif
68
69 %}
70
71 %token <ival> '{'
72
73 %token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
74 %token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
75 %token <pval> LABEL
76 %token <ival> FORMAT SUB ANONSUB PACKAGE USE
77 %token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
78 %token <ival> LOOPEX DOTDOT
79 %token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP
80 %token <ival> RELOP EQOP MULOP ADDOP
81 %token <ival> DOLSHARP DO HASHBRACK NOAMP
82 %token <ival> LOCAL MY MYSUB
83 %token COLONATTR
84
85 %type <ival> prog decl format startsub startanonsub startformsub
86 %type <ival> remember mremember '&'
87 %type <opval> block mblock lineseq line loop cond else
88 %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
89 %type <opval> argexpr nexpr texpr iexpr mexpr mnexpr mtexpr miexpr
90 %type <opval> listexpr listexprcom indirob listop method
91 %type <opval> formname subname proto subbody cont my_scalar
92 %type <opval> subattrlist myattrlist mysubrout myattrterm myterm
93 %type <opval> termbinop termunop anonymous termdo
94 %type <pval> label
95
96 %nonassoc PREC_LOW
97 %nonassoc LOOPEX
98
99 %left <ival> OROP
100 %left ANDOP
101 %right NOTOP
102 %nonassoc LSTOP LSTOPSUB
103 %left ','
104 %right <ival> ASSIGNOP
105 %right '?' ':'
106 %nonassoc DOTDOT
107 %left OROR
108 %left ANDAND
109 %left <ival> BITOROP
110 %left <ival> BITANDOP
111 %nonassoc EQOP
112 %nonassoc RELOP
113 %nonassoc UNIOP UNIOPSUB
114 %left <ival> SHIFTOP
115 %left ADDOP
116 %left MULOP
117 %left <ival> MATCHOP
118 %right '!' '~' UMINUS REFGEN
119 %right <ival> POWOP
120 %nonassoc PREINC PREDEC POSTINC POSTDEC
121 %left ARROW
122 %nonassoc <ival> ')'
123 %left '('
124 %left '[' '{'
125
126 %% /* RULES */
127
128 /* The whole program */
129 prog    :       /* NULL */
130                 {
131 #if defined(YYDEBUG) && defined(DEBUGGING)
132                     yydebug = (DEBUG_p_TEST);
133 #endif
134                     PL_expect = XSTATE; $$ = block_start(TRUE);
135                 }
136         /*CONTINUED*/   lineseq
137                         { newPROG(block_end($1,$2)); }
138         ;
139
140 /* An ordinary block */
141 block   :       '{' remember lineseq '}'
142                         { if (PL_copline > (line_t)$1)
143                               PL_copline = $1;
144                           $$ = block_end($2, $3); }
145         ;
146
147 remember:       /* NULL */      /* start a full lexical scope */
148                         { $$ = block_start(TRUE); }
149         ;
150
151 mblock  :       '{' mremember lineseq '}'
152                         { if (PL_copline > (line_t)$1)
153                               PL_copline = $1;
154                           $$ = block_end($2, $3); }
155         ;
156
157 mremember:      /* NULL */      /* start a partial lexical scope */
158                         { $$ = block_start(FALSE); }
159         ;
160
161 /* A collection of "lines" in the program */
162 lineseq :       /* NULL */
163                         { $$ = Nullop; }
164         |       lineseq decl
165                         { $$ = $1; }
166         |       lineseq line
167                         {   $$ = append_list(OP_LINESEQ,
168                                 (LISTOP*)$1, (LISTOP*)$2);
169                             PL_pad_reset_pending = TRUE;
170                             if ($1 && $2) PL_hints |= HINT_BLOCK_SCOPE; }
171         ;
172
173 /* A "line" in the program */
174 line    :       label cond
175                         { $$ = newSTATEOP(0, $1, $2); }
176         |       loop    /* loops add their own labels */
177         |       label ';'
178                         { if ($1 != Nullch) {
179                               $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0));
180                             }
181                             else {
182                               $$ = Nullop;
183                               PL_copline = NOLINE;
184                             }
185                             PL_expect = XSTATE; }
186         |       label sideff ';'
187                         { $$ = newSTATEOP(0, $1, $2);
188                           PL_expect = XSTATE; }
189         ;
190
191 /* An expression which may have a side-effect */
192 sideff  :       error
193                         { $$ = Nullop; }
194         |       expr
195                         { $$ = $1; }
196         |       expr IF expr
197                         { $$ = newLOGOP(OP_AND, 0, $3, $1); }
198         |       expr UNLESS expr
199                         { $$ = newLOGOP(OP_OR, 0, $3, $1); }
200         |       expr WHILE expr
201                         { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
202         |       expr UNTIL iexpr
203                         { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);}
204         |       expr FOR expr
205                         { $$ = newFOROP(0, Nullch, $2,
206                                         Nullop, $3, $1, Nullop); }
207         ;
208
209 /* else and elsif blocks */
210 else    :       /* NULL */
211                         { $$ = Nullop; }
212         |       ELSE mblock
213                         { ($2)->op_flags |= OPf_PARENS; $$ = scope($2); }
214         |       ELSIF '(' mexpr ')' mblock else
215                         { PL_copline = $1;
216                             $$ = newCONDOP(0, $3, scope($5), $6);
217                             PL_hints |= HINT_BLOCK_SCOPE; }
218         ;
219
220 /* Real conditional expressions */
221 cond    :       IF '(' remember mexpr ')' mblock else
222                         { PL_copline = $1;
223                             $$ = block_end($3,
224                                    newCONDOP(0, $4, scope($6), $7)); }
225         |       UNLESS '(' remember miexpr ')' mblock else
226                         { PL_copline = $1;
227                             $$ = block_end($3,
228                                    newCONDOP(0, $4, scope($6), $7)); }
229         ;
230
231 /* Continue blocks */
232 cont    :       /* NULL */
233                         { $$ = Nullop; }
234         |       CONTINUE block
235                         { $$ = scope($2); }
236         ;
237
238 /* Loops: while, until, for, and a bare block */
239 loop    :       label WHILE '(' remember mtexpr ')' mblock cont
240                         { PL_copline = $2;
241                             $$ = block_end($4,
242                                    newSTATEOP(0, $1,
243                                      newWHILEOP(0, 1, (LOOP*)Nullop,
244                                                 $2, $5, $7, $8))); }
245         |       label UNTIL '(' remember miexpr ')' mblock cont
246                         { PL_copline = $2;
247                             $$ = block_end($4,
248                                    newSTATEOP(0, $1,
249                                      newWHILEOP(0, 1, (LOOP*)Nullop,
250                                                 $2, $5, $7, $8))); }
251         |       label FOR MY remember my_scalar '(' mexpr ')' mblock cont
252                         { $$ = block_end($4,
253                                  newFOROP(0, $1, $2, $5, $7, $9, $10)); }
254         |       label FOR scalar '(' remember mexpr ')' mblock cont
255                         { $$ = block_end($5,
256                                  newFOROP(0, $1, $2, mod($3, OP_ENTERLOOP),
257                                           $6, $8, $9)); }
258         |       label FOR '(' remember mexpr ')' mblock cont
259                         { $$ = block_end($4,
260                                  newFOROP(0, $1, $2, Nullop, $5, $7, $8)); }
261         |       label FOR '(' remember mnexpr ';' mtexpr ';' mnexpr ')' mblock
262                         /* basically fake up an initialize-while lineseq */
263                         { OP *forop;
264                           PL_copline = $2;
265                           forop = newSTATEOP(0, $1,
266                                             newWHILEOP(0, 1, (LOOP*)Nullop,
267                                                 $2, scalar($7),
268                                                 $11, $9));
269                           if ($5) {
270                                 forop = append_elem(OP_LINESEQ,
271                                         newSTATEOP(0, ($1?savepv($1):Nullch),
272                                                    $5),
273                                         forop);
274                           }
275
276                           $$ = block_end($4, forop); }
277         |       label block cont  /* a block is a loop that happens once */
278                         { $$ = newSTATEOP(0, $1,
279                                  newWHILEOP(0, 1, (LOOP*)Nullop,
280                                             NOLINE, Nullop, $2, $3)); }
281         ;
282
283 /* Normal expression */
284 nexpr   :       /* NULL */
285                         { $$ = Nullop; }
286         |       sideff
287         ;
288
289 /* Boolean expression */
290 texpr   :       /* NULL means true */
291                         { (void)scan_num("1", &yylval); $$ = yylval.opval; }
292         |       expr
293         ;
294
295 /* Inverted boolean expression */
296 iexpr   :       expr
297                         { $$ = invert(scalar($1)); }
298         ;
299
300 /* Expression with its own lexical scope */
301 mexpr   :       expr
302                         { $$ = $1; intro_my(); }
303         ;
304
305 mnexpr  :       nexpr
306                         { $$ = $1; intro_my(); }
307         ;
308
309 mtexpr  :       texpr
310                         { $$ = $1; intro_my(); }
311         ;
312
313 miexpr  :       iexpr
314                         { $$ = $1; intro_my(); }
315         ;
316
317 /* Optional "MAIN:"-style loop labels */
318 label   :       /* empty */
319                         { $$ = Nullch; }
320         |       LABEL
321         ;
322
323 /* Some kind of declaration - does not take part in the parse tree */
324 decl    :       format
325                         { $$ = 0; }
326         |       subrout
327                         { $$ = 0; }
328         |       mysubrout
329                         { $$ = 0; }
330         |       package
331                         { $$ = 0; }
332         |       use
333                         { $$ = 0; }
334         ;
335
336 format  :       FORMAT startformsub formname block
337                         { newFORM($2, $3, $4); }
338         ;
339
340 formname:       WORD            { $$ = $1; }
341         |       /* NULL */      { $$ = Nullop; }
342         ;
343
344 /* Unimplemented "my sub foo { }" */
345 mysubrout:      MYSUB startsub subname proto subattrlist subbody
346                         { newMYSUB($2, $3, $4, $5, $6); }
347         ;
348
349 /* Subroutine definition */
350 subrout :       SUB startsub subname proto subattrlist subbody
351                         { newATTRSUB($2, $3, $4, $5, $6); }
352         ;
353
354 startsub:       /* NULL */      /* start a regular subroutine scope */
355                         { $$ = start_subparse(FALSE, 0); }
356         ;
357
358 startanonsub:   /* NULL */      /* start an anonymous subroutine scope */
359                         { $$ = start_subparse(FALSE, CVf_ANON); }
360         ;
361
362 startformsub:   /* NULL */      /* start a format subroutine scope */
363                         { $$ = start_subparse(TRUE, 0); }
364         ;
365
366 /* Name of a subroutine - must be a bareword, could be special */
367 subname :       WORD    { STRLEN n_a; char *name = SvPV(((SVOP*)$1)->op_sv,n_a);
368                           if (strEQ(name, "BEGIN") || strEQ(name, "END")
369                               || strEQ(name, "INIT") || strEQ(name, "CHECK"))
370                               CvSPECIAL_on(PL_compcv);
371                           $$ = $1; }
372         ;
373
374 /* Subroutine prototype */
375 proto   :       /* NULL */
376                         { $$ = Nullop; }
377         |       THING
378         ;
379
380 /* Optional list of subroutine attributes */
381 subattrlist:    /* NULL */
382                         { $$ = Nullop; }
383         |       COLONATTR THING
384                         { $$ = $2; }
385         |       COLONATTR
386                         { $$ = Nullop; }
387         ;
388
389 /* List of attributes for a "my" variable declaration */
390 myattrlist:     COLONATTR THING
391                         { $$ = $2; }
392         |       COLONATTR
393                         { $$ = Nullop; }
394         ;
395
396 /* Subroutine body - either null or a block */
397 subbody :       block   { $$ = $1; }
398         |       ';'     { $$ = Nullop; PL_expect = XSTATE; }
399         ;
400
401 package :       PACKAGE WORD ';'
402                         { package($2); }
403         |       PACKAGE ';'
404                         { package(Nullop); }
405         ;
406
407 use     :       USE startsub
408                         { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
409                     WORD WORD listexpr ';'
410                         { utilize($1, $2, $4, $5, $6); }
411         ;
412
413 /* Ordinary expressions; logical combinations */
414 expr    :       expr ANDOP expr
415                         { $$ = newLOGOP(OP_AND, 0, $1, $3); }
416         |       expr OROP expr
417                         { $$ = newLOGOP($2, 0, $1, $3); }
418         |       argexpr %prec PREC_LOW
419         ;
420
421 /* Expressions are a list of terms joined by commas */
422 argexpr :       argexpr ','
423                         { $$ = $1; }
424         |       argexpr ',' term
425                         { $$ = append_elem(OP_LIST, $1, $3); }
426         |       term %prec PREC_LOW
427         ;
428
429 /* List operators */
430 listop  :       LSTOP indirob argexpr          /* print $fh @args */
431                         { $$ = convert($1, OPf_STACKED,
432                                 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
433         |       FUNC '(' indirob expr ')'      /* print ($fh @args */
434                         { $$ = convert($1, OPf_STACKED,
435                                 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
436         |       term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
437                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
438                                 append_elem(OP_LIST,
439                                     prepend_elem(OP_LIST, scalar($1), $5),
440                                     newUNOP(OP_METHOD, 0, $3))); }
441         |       term ARROW method                     /* $foo->bar */
442                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
443                                 append_elem(OP_LIST, scalar($1),
444                                     newUNOP(OP_METHOD, 0, $3))); }
445         |       METHOD indirob listexpr              /* new Class @args */
446                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
447                                 append_elem(OP_LIST,
448                                     prepend_elem(OP_LIST, $2, $3),
449                                     newUNOP(OP_METHOD, 0, $1))); }
450         |       FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
451                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
452                                 append_elem(OP_LIST,
453                                     prepend_elem(OP_LIST, $2, $4),
454                                     newUNOP(OP_METHOD, 0, $1))); }
455         |       LSTOP listexpr                       /* print @args */
456                         { $$ = convert($1, 0, $2); }
457         |       FUNC '(' listexprcom ')'             /* print (@args) */
458                         { $$ = convert($1, 0, $3); }
459         |       LSTOPSUB startanonsub block          /* map { foo } ... */ 
460                         { $3 = newANONATTRSUB($2, 0, Nullop, $3); }
461                     listexpr            %prec LSTOP  /* ... @bar */
462                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
463                                  append_elem(OP_LIST,
464                                    prepend_elem(OP_LIST, $3, $5), $1)); }
465         ;
466
467 /* Names of methods. May use $object->$methodname */
468 method  :       METHOD
469         |       scalar
470         ;
471
472 /* Some kind of subscripted expression */
473 subscripted:    star '{' expr ';' '}'        /* *main::{something} */
474                         /* In this and all the hash accessors, ';' is
475                          * provided by the tokeniser */
476                         { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
477         |       scalar '[' expr ']'          /* $array[$element] */
478                         { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
479         |       term ARROW '[' expr ']'      /* somearef->[$element] */
480                         { $$ = newBINOP(OP_AELEM, 0,
481                                         ref(newAVREF($1),OP_RV2AV),
482                                         scalar($4));}
483         |       subscripted '[' expr ']'    /* $foo->[$bar]->[$baz] */
484                         { $$ = newBINOP(OP_AELEM, 0,
485                                         ref(newAVREF($1),OP_RV2AV),
486                                         scalar($3));}
487         |       scalar '{' expr ';' '}'    /* $foo->{bar();} */
488                         { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
489                             PL_expect = XOPERATOR; }
490         |       term ARROW '{' expr ';' '}' /* somehref->{bar();} */
491                         { $$ = newBINOP(OP_HELEM, 0,
492                                         ref(newHVREF($1),OP_RV2HV),
493                                         jmaybe($4));
494                             PL_expect = XOPERATOR; }
495         |       subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
496                         { $$ = newBINOP(OP_HELEM, 0,
497                                         ref(newHVREF($1),OP_RV2HV),
498                                         jmaybe($3));
499                             PL_expect = XOPERATOR; }
500         |       term ARROW '(' ')'          /* $subref->() */
501                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
502                                    newCVREF(0, scalar($1))); }
503         |       term ARROW '(' expr ')'     /* $subref->(@args) */
504                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
505                                    append_elem(OP_LIST, $4,
506                                        newCVREF(0, scalar($1)))); }
507
508         |       subscripted '(' expr ')'   /* $foo->{bar}->(@args) */
509                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
510                                    append_elem(OP_LIST, $3,
511                                                newCVREF(0, scalar($1)))); }
512         |       subscripted '(' ')'        /* $foo->{bar}->() */
513                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
514                                    newCVREF(0, scalar($1))); }
515     ;
516
517 /* Binary operators between terms */
518 termbinop       :       term ASSIGNOP term             /* $x = $y */
519                         { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
520         |       term POWOP term                        /* $x ** $y */
521                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
522         |       term MULOP term                        /* $x * $y, $x x $y */
523                         {   if ($2 != OP_REPEAT)
524                                 scalar($1);
525                             $$ = newBINOP($2, 0, $1, scalar($3)); }
526         |       term ADDOP term                        /* $x + $y */
527                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
528         |       term SHIFTOP term                      /* $x >> $y, $x << $y */
529                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
530         |       term RELOP term                        /* $x > $y, etc. */
531                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
532         |       term EQOP term                         /* $x == $y, $x eq $y */
533                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
534         |       term BITANDOP term                     /* $x & $y */
535                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
536         |       term BITOROP term                      /* $x | $y */
537                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
538         |       term DOTDOT term                       /* $x..$y, $x...$y */
539                         { $$ = newRANGE($2, scalar($1), scalar($3));}
540         |       term ANDAND term                       /* $x && $y */
541                         { $$ = newLOGOP(OP_AND, 0, $1, $3); }
542         |       term OROR term                         /* $x || $y */
543                         { $$ = newLOGOP(OP_OR, 0, $1, $3); }
544         |       term MATCHOP term                      /* $x =~ /$y/ */
545                         { $$ = bind_match($2, $1, $3); }
546     ;
547
548 /* Unary operators and terms */
549 termunop : '-' term %prec UMINUS                       /* -$x */
550                         { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
551         |       '+' term %prec UMINUS                  /* +$x */
552                         { $$ = $2; }
553         |       '!' term                               /* !$x */
554                         { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
555         |       '~' term                               /* ~$x */
556                         { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
557         |       term POSTINC                           /* $x++ */
558                         { $$ = newUNOP(OP_POSTINC, 0,
559                                         mod(scalar($1), OP_POSTINC)); }
560         |       term POSTDEC                           /* $x-- */
561                         { $$ = newUNOP(OP_POSTDEC, 0,
562                                         mod(scalar($1), OP_POSTDEC)); }
563         |       PREINC term                            /* ++$x */
564                         { $$ = newUNOP(OP_PREINC, 0,
565                                         mod(scalar($2), OP_PREINC)); }
566         |       PREDEC term                            /* --$x */
567                         { $$ = newUNOP(OP_PREDEC, 0,
568                                         mod(scalar($2), OP_PREDEC)); }
569
570     ;
571
572 /* Constructors for anonymous data */
573 anonymous:      '[' expr ']'
574                         { $$ = newANONLIST($2); }
575         |       '[' ']'
576                         { $$ = newANONLIST(Nullop); }
577         |       HASHBRACK expr ';' '}'  %prec '(' /* { foo => "Bar" } */
578                         { $$ = newANONHASH($2); } 
579         |       HASHBRACK ';' '}'       %prec '(' /* { } (';' by tokener) */
580                         { $$ = newANONHASH(Nullop); }
581         |       ANONSUB startanonsub proto subattrlist block    %prec '('
582                         { $$ = newANONATTRSUB($2, $3, $4, $5); }
583
584     ;
585
586 /* Things called with "do" */
587 termdo  :       DO term %prec UNIOP                     /* do $filename */
588                         { $$ = dofile($2); }
589         |       DO block        %prec '('               /* do { code */
590                         { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
591         |       DO WORD '(' ')'                         /* do somesub() */
592                         { $$ = newUNOP(OP_ENTERSUB,
593                             OPf_SPECIAL|OPf_STACKED,
594                             prepend_elem(OP_LIST,
595                                 scalar(newCVREF(
596                                     (OPpENTERSUB_AMPER<<8),
597                                     scalar($2)
598                                 )),Nullop)); dep();}
599         |       DO WORD '(' expr ')'                    /* do somesub(@args) */
600                         { $$ = newUNOP(OP_ENTERSUB,
601                             OPf_SPECIAL|OPf_STACKED,
602                             append_elem(OP_LIST,
603                                 $4,
604                                 scalar(newCVREF(
605                                     (OPpENTERSUB_AMPER<<8),
606                                     scalar($2)
607                                 )))); dep();}
608         |       DO scalar '(' ')'                      /* do $subref () */
609                         { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
610                             prepend_elem(OP_LIST,
611                                 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
612         |       DO scalar '(' expr ')'                 /* do $subref (@args) */
613                         { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
614                             prepend_elem(OP_LIST,
615                                 $4,
616                                 scalar(newCVREF(0,scalar($2))))); dep();}
617
618         ;
619
620 term    :       termbinop
621         |       termunop
622         |       anonymous
623         |       termdo
624         |       term '?' term ':' term
625                         { $$ = newCONDOP(0, $1, $3, $5); }
626         |       REFGEN term                          /* \$x, \@y, \%z */
627                         { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
628         |       myattrterm      %prec UNIOP
629                         { $$ = $1; }
630         |       LOCAL term      %prec UNIOP
631                         { $$ = localize($2,$1); }
632         |       '(' expr ')'
633                         { $$ = sawparens($2); }
634         |       '(' ')'
635                         { $$ = sawparens(newNULLLIST()); }
636         |       scalar  %prec '('
637                         { $$ = $1; }
638         |       star    %prec '('
639                         { $$ = $1; }
640         |       hsh     %prec '('
641                         { $$ = $1; }
642         |       ary     %prec '('
643                         { $$ = $1; }
644         |       arylen  %prec '('                    /* $#x, $#{ something } */
645                         { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
646         |       subscripted
647                         { $$ = $1; }
648         |       '(' expr ')' '[' expr ']'            /* list slice */
649                         { $$ = newSLICEOP(0, $5, $2); }
650         |       '(' ')' '[' expr ']'                 /* empty list slice! */
651                         { $$ = newSLICEOP(0, $4, Nullop); }
652         |       ary '[' expr ']'                     /* array slice */
653                         { $$ = prepend_elem(OP_ASLICE,
654                                 newOP(OP_PUSHMARK, 0),
655                                     newLISTOP(OP_ASLICE, 0,
656                                         list($3),
657                                         ref($1, OP_ASLICE))); }
658         |       ary '{' expr ';' '}'                 /* @hash{@keys} */
659                         { $$ = prepend_elem(OP_HSLICE,
660                                 newOP(OP_PUSHMARK, 0),
661                                     newLISTOP(OP_HSLICE, 0,
662                                         list($3),
663                                         ref(oopsHV($1), OP_HSLICE)));
664                             PL_expect = XOPERATOR; }
665         |       THING   %prec '('
666                         { $$ = $1; }
667         |       amper                                /* &foo; */
668                         { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
669         |       amper '(' ')'                        /* &foo() */ 
670                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
671         |       amper '(' expr ')'                   /* &foo(@args) */
672                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
673                             append_elem(OP_LIST, $3, scalar($1))); }
674         |       NOAMP WORD listexpr                  /* foo(@args) */
675                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
676                             append_elem(OP_LIST, $3, scalar($2))); }
677         |       LOOPEX  /* loop exiting command (goto, last, dump, etc) */
678                         { $$ = newOP($1, OPf_SPECIAL);
679                             PL_hints |= HINT_BLOCK_SCOPE; }
680         |       LOOPEX term
681                         { $$ = newLOOPEX($1,$2); }
682         |       NOTOP argexpr                        /* not $foo */
683                         { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
684         |       UNIOP                                /* Unary op, $_ implied */
685                         { $$ = newOP($1, 0); }
686         |       UNIOP block                          /* eval { foo }, I *think* */
687                         { $$ = newUNOP($1, 0, $2); }
688         |       UNIOP term                           /* Unary op */
689                         { $$ = newUNOP($1, 0, $2); }
690         |       UNIOPSUB term                        /* Sub treated as unop */
691                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
692                             append_elem(OP_LIST, $2, scalar($1))); }
693         |       FUNC0                                /* Nullary operator */
694                         { $$ = newOP($1, 0); }
695         |       FUNC0 '(' ')'
696                         { $$ = newOP($1, 0); }
697         |       FUNC0SUB                             /* Sub treated as nullop */
698                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
699                                 scalar($1)); }
700         |       FUNC1 '(' ')'                        /* not () */
701                         { $$ = newOP($1, OPf_SPECIAL); }
702         |       FUNC1 '(' expr ')'                   /* not($foo) */
703                         { $$ = newUNOP($1, 0, $3); }
704         |       PMFUNC '(' term ')'                  /* split (/foo/) */
705                         { $$ = pmruntime($1, $3, Nullop); }
706         |       PMFUNC '(' term ',' term ')'         /* split (/foo/,$bar) */
707                         { $$ = pmruntime($1, $3, $5); }
708         |       WORD
709         |       listop
710         ;
711
712 /* "my" declarations, with optional attributes */
713 myattrterm:     MY myterm myattrlist
714                         { $$ = my_attrs($2,$3); }
715         |       MY myterm
716                         { $$ = localize($2,$1); }
717         ;
718
719 /* Things that can be "my"'d */
720 myterm  :       '(' expr ')'
721                         { $$ = sawparens($2); }
722         |       '(' ')'
723                         { $$ = sawparens(newNULLLIST()); }
724         |       scalar  %prec '('
725                         { $$ = $1; }
726         |       hsh     %prec '('
727                         { $$ = $1; }
728         |       ary     %prec '('
729                         { $$ = $1; }
730         ;
731
732 /* Basic list expressions */
733 listexpr:       /* NULL */ %prec PREC_LOW
734                         { $$ = Nullop; }
735         |       argexpr    %prec PREC_LOW
736                         { $$ = $1; }
737         ;
738
739 listexprcom:    /* NULL */
740                         { $$ = Nullop; }
741         |       expr
742                         { $$ = $1; }
743         |       expr ','
744                         { $$ = $1; }
745         ;
746
747 /* A little bit of trickery to make "for my $foo (@bar)" actually be
748    lexical */
749 my_scalar:      scalar
750                         { PL_in_my = 0; $$ = my($1); }
751         ;
752
753 amper   :       '&' indirob
754                         { $$ = newCVREF($1,$2); }
755         ;
756
757 scalar  :       '$' indirob
758                         { $$ = newSVREF($2); }
759         ;
760
761 ary     :       '@' indirob
762                         { $$ = newAVREF($2); }
763         ;
764
765 hsh     :       '%' indirob
766                         { $$ = newHVREF($2); }
767         ;
768
769 arylen  :       DOLSHARP indirob
770                         { $$ = newAVREF($2); }
771         ;
772
773 star    :       '*' indirob
774                         { $$ = newGVREF(0,$2); }
775         ;
776
777 /* Indirect objects */
778 indirob :       WORD
779                         { $$ = scalar($1); }
780         |       scalar %prec PREC_LOW
781                         { $$ = scalar($1);  }
782         |       block
783                         { $$ = scope($1); }
784
785         |       PRIVATEREF
786                         { $$ = $1; }
787         ;
788
789 %% /* PROGRAM */
790
791 /* more stuff added to make perly_c.diff easier to apply */
792
793 #ifdef yyparse
794 #undef yyparse
795 #endif
796 #define yyparse() Perl_yyparse(pTHX)
797