This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perlpod.pod patch for _16
[perl5.git] / perly.y
1 /*    perly.y
2  *
3  *    Copyright (c) 1991-1994, 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 that wander are lost.'
13  */
14
15 %{
16 #include "EXTERN.h"
17 #include "perl.h"
18
19 static void
20 dep()
21 {
22     deprecate("\"do\" to call subroutines");
23 }
24
25 %}
26
27 %start prog
28
29 %union {
30     I32 ival;
31     char *pval;
32     OP *opval;
33     GV *gvval;
34 }
35
36 %token <ival> '{' ')'
37
38 %token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
39 %token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
40 %token <pval> LABEL
41 %token <ival> FORMAT SUB ANONSUB PACKAGE USE
42 %token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
43 %token <ival> LOOPEX DOTDOT
44 %token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP
45 %token <ival> RELOP EQOP MULOP ADDOP
46 %token <ival> DOLSHARP DO HASHBRACK NOAMP
47 %token LOCAL MY
48
49 %type <ival> prog decl local format startsub remember mremember '&'
50 %type <opval> block mblock lineseq line loop cond else
51 %type <opval> expr term scalar ary hsh arylen star amper sideff
52 %type <opval> argexpr nexpr texpr iexpr mexpr mnexpr mtexpr miexpr
53 %type <opval> listexpr listexprcom indirob
54 %type <opval> listop method proto cont my_scalar
55 %type <pval> label
56
57 %left <ival> OROP
58 %left ANDOP
59 %right NOTOP
60 %nonassoc LSTOP LSTOPSUB
61 %left ','
62 %right <ival> ASSIGNOP
63 %right '?' ':'
64 %nonassoc DOTDOT
65 %left OROR
66 %left ANDAND
67 %left <ival> BITOROP
68 %left <ival> BITANDOP
69 %nonassoc EQOP
70 %nonassoc RELOP
71 %nonassoc UNIOP UNIOPSUB
72 %left <ival> SHIFTOP
73 %left ADDOP
74 %left MULOP
75 %left <ival> MATCHOP
76 %right '!' '~' UMINUS REFGEN
77 %right <ival> POWOP
78 %nonassoc PREINC PREDEC POSTINC POSTDEC
79 %left ARROW
80 %left '('
81
82 %% /* RULES */
83
84 prog    :       /* NULL */
85                 {
86 #if defined(YYDEBUG) && defined(DEBUGGING)
87                     yydebug = (debug & 1);
88 #endif
89                     expect = XSTATE;
90                 }
91         /*CONTINUED*/   lineseq
92                         { newPROG($2); }
93         ;
94
95 block   :       '{' remember lineseq '}'
96                         { if (copline > (line_t)$1)
97                               copline = $1;
98                           $$ = block_end($2, $3); }
99         ;
100
101 remember:       /* NULL */      /* start a full lexical scope */
102                         { $$ = block_start(TRUE); }
103         ;
104
105 mblock  :       '{' mremember lineseq '}'
106                         { if (copline > (line_t)$1)
107                               copline = $1;
108                           $$ = block_end($2, $3); }
109         ;
110
111 mremember:      /* NULL */      /* start a partial lexical scope */
112                         { $$ = block_start(FALSE); }
113         ;
114
115 lineseq :       /* NULL */
116                         { $$ = Nullop; }
117         |       lineseq decl
118                         { $$ = $1; }
119         |       lineseq line
120                         {   $$ = append_list(OP_LINESEQ,
121                                 (LISTOP*)$1, (LISTOP*)$2);
122                             pad_reset_pending = TRUE;
123                             if ($1 && $2) hints |= HINT_BLOCK_SCOPE; }
124         ;
125
126 line    :       label cond
127                         { $$ = newSTATEOP(0, $1, $2); }
128         |       loop    /* loops add their own labels */
129         |       label ';'
130                         { if ($1 != Nullch) {
131                               $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0));
132                             }
133                             else {
134                               $$ = Nullop;
135                               copline = NOLINE;
136                             }
137                             expect = XSTATE; }
138         |       label sideff ';'
139                         { $$ = newSTATEOP(0, $1, $2);
140                           expect = XSTATE; }
141         ;
142
143 sideff  :       error
144                         { $$ = Nullop; }
145         |       expr
146                         { $$ = $1; }
147         |       expr IF expr
148                         { $$ = newLOGOP(OP_AND, 0, $3, $1); }
149         |       expr UNLESS expr
150                         { $$ = newLOGOP(OP_OR, 0, $3, $1); }
151         |       expr WHILE expr
152                         { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
153         |       expr UNTIL iexpr
154                         { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);}
155         ;
156
157 else    :       /* NULL */
158                         { $$ = Nullop; }
159         |       ELSE mblock
160                         { $$ = scope($2); }
161         |       ELSIF '(' mexpr ')' mblock else
162                         { copline = $1;
163                             $$ = newSTATEOP(0, Nullch,
164                                    newCONDOP(0, $3, scope($5), $6));
165                             hints |= HINT_BLOCK_SCOPE; }
166         ;
167
168 cond    :       IF '(' remember mexpr ')' mblock else
169                         { copline = $1;
170                             $$ = block_end($3,
171                                    newCONDOP(0, $4, scope($6), $7)); }
172         |       UNLESS '(' remember miexpr ')' mblock else
173                         { copline = $1;
174                             $$ = block_end($3,
175                                    newCONDOP(0, $4, scope($6), $7)); }
176         ;
177
178 cont    :       /* NULL */
179                         { $$ = Nullop; }
180         |       CONTINUE block
181                         { $$ = scope($2); }
182         ;
183
184 loop    :       label WHILE '(' remember mtexpr ')' mblock cont
185                         { copline = $2;
186                             $$ = block_end($4,
187                                    newSTATEOP(0, $1,
188                                      newWHILEOP(0, 1, (LOOP*)Nullop,
189                                                 $5, $7, $8))); }
190         |       label UNTIL '(' remember miexpr ')' mblock cont
191                         { copline = $2;
192                             $$ = block_end($4,
193                                    newSTATEOP(0, $1,
194                                      newWHILEOP(0, 1, (LOOP*)Nullop,
195                                                 $5, $7, $8))); }
196         |       label FOR MY remember my_scalar '(' mexpr ')' mblock cont
197                         { $$ = block_end($4,
198                                  newFOROP(0, $1, $2, $5, $7, $9, $10)); }
199         |       label FOR scalar '(' remember mexpr ')' mblock cont
200                         { $$ = block_end($5,
201                                  newFOROP(0, $1, $2, mod($3, OP_ENTERLOOP),
202                                           $6, $8, $9)); }
203         |       label FOR '(' remember mexpr ')' mblock cont
204                         { $$ = block_end($4,
205                                  newFOROP(0, $1, $2, Nullop, $5, $7, $8)); }
206         |       label FOR '(' remember mnexpr ';' mtexpr ';' mnexpr ')' mblock
207                         /* basically fake up an initialize-while lineseq */
208                         { copline = $2;
209                             $$ = block_end($4,
210                                    append_elem(OP_LINESEQ, scalar($5),
211                                      newSTATEOP(0, $1,
212                                        newWHILEOP(0, 1, (LOOP*)Nullop,
213                                                   scalar($7),
214                                                   $11, scalar($9))))); }
215         |       label block cont  /* a block is a loop that happens once */
216                         { $$ = newSTATEOP(0,
217                                 $1, newWHILEOP(0, 1, (LOOP*)Nullop,
218                                         Nullop, $2, $3)); }
219         ;
220
221 nexpr   :       /* NULL */
222                         { $$ = Nullop; }
223         |       sideff
224         ;
225
226 texpr   :       /* NULL means true */
227                         { (void)scan_num("1"); $$ = yylval.opval; }
228         |       expr
229         ;
230
231 iexpr   :       expr
232                         { $$ = invert(scalar($1)); }
233         ;
234
235 mexpr   :       expr
236                         { $$ = $1; intro_my(); }
237         ;
238
239 mnexpr  :       nexpr
240                         { $$ = $1; intro_my(); }
241         ;
242
243 mtexpr  :       texpr
244                         { $$ = $1; intro_my(); }
245         ;
246
247 miexpr  :       iexpr
248                         { $$ = $1; intro_my(); }
249         ;
250
251 label   :       /* empty */
252                         { $$ = Nullch; }
253         |       LABEL
254         ;
255
256 decl    :       format
257                         { $$ = 0; }
258         |       subrout
259                         { $$ = 0; }
260         |       package
261                         { $$ = 0; }
262         |       use
263                         { $$ = 0; }
264         ;
265
266 format  :       FORMAT startsub WORD block
267                         { newFORM($2, $3, $4); }
268         |       FORMAT startsub block
269                         { newFORM($2, Nullop, $3); }
270         ;
271
272 subrout :       SUB startsub WORD proto block
273                         { newSUB($2, $3, $4, $5); }
274         |       SUB startsub WORD proto ';'
275                         { newSUB($2, $3, $4, Nullop); expect = XSTATE; }
276         ;
277
278 proto   :       /* NULL */
279                         { $$ = Nullop; }
280         |       THING
281         ;
282                 
283 startsub:       /* NULL */      /* start a subroutine scope */
284                         { $$ = start_subparse(); }
285         ;
286
287 package :       PACKAGE WORD ';'
288                         { package($2); }
289         |       PACKAGE ';'
290                         { package(Nullop); }
291         ;
292
293 use     :       USE startsub WORD WORD listexpr ';'
294                         { utilize($1, $2, $3, $4, $5); }
295         ;
296
297 expr    :       expr ANDOP expr
298                         { $$ = newLOGOP(OP_AND, 0, $1, $3); }
299         |       expr OROP expr
300                         { $$ = newLOGOP($2, 0, $1, $3); }
301         |       argexpr
302         ;
303
304 argexpr :       argexpr ','
305                         { $$ = $1; }
306         |       argexpr ',' term
307                         { $$ = append_elem(OP_LIST, $1, $3); }
308         |       term
309         ;
310
311 listop  :       LSTOP indirob argexpr
312                         { $$ = convert($1, OPf_STACKED,
313                                 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
314         |       FUNC '(' indirob expr ')'
315                         { $$ = convert($1, OPf_STACKED,
316                                 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
317         |       term ARROW method '(' listexprcom ')'
318                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
319                                 append_elem(OP_LIST,
320                                     prepend_elem(OP_LIST, scalar($1), $5),
321                                     newUNOP(OP_METHOD, 0, $3))); }
322         |       METHOD indirob listexpr
323                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
324                                 append_elem(OP_LIST,
325                                     prepend_elem(OP_LIST, $2, $3),
326                                     newUNOP(OP_METHOD, 0, $1))); }
327         |       FUNCMETH indirob '(' listexprcom ')'
328                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
329                                 append_elem(OP_LIST,
330                                     prepend_elem(OP_LIST, $2, $4),
331                                     newUNOP(OP_METHOD, 0, $1))); }
332         |       LSTOP listexpr
333                         { $$ = convert($1, 0, $2); }
334         |       FUNC '(' listexprcom ')'
335                         { $$ = convert($1, 0, $3); }
336         |       LSTOPSUB startsub block listexpr        %prec LSTOP
337                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
338                             append_elem(OP_LIST,
339                               prepend_elem(OP_LIST, newANONSUB($2, 0, $3), $4),
340                               $1)); }
341         ;
342
343 method  :       METHOD
344         |       scalar
345         ;
346
347 term    :       term ASSIGNOP term
348                         { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
349         |       term POWOP term
350                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
351         |       term MULOP term
352                         {   if ($2 != OP_REPEAT)
353                                 scalar($1);
354                             $$ = newBINOP($2, 0, $1, scalar($3)); }
355         |       term ADDOP term
356                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
357         |       term SHIFTOP term
358                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
359         |       term RELOP term
360                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
361         |       term EQOP term
362                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
363         |       term BITANDOP term
364                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
365         |       term BITOROP term
366                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
367         |       term DOTDOT term
368                         { $$ = newRANGE($2, scalar($1), scalar($3));}
369         |       term ANDAND term
370                         { $$ = newLOGOP(OP_AND, 0, $1, $3); }
371         |       term OROR term
372                         { $$ = newLOGOP(OP_OR, 0, $1, $3); }
373         |       term '?' term ':' term
374                         { $$ = newCONDOP(0, $1, $3, $5); }
375         |       term MATCHOP term
376                         { $$ = bind_match($2, $1, $3); }
377
378         |       '-' term %prec UMINUS
379                         { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
380         |       '+' term %prec UMINUS
381                         { $$ = $2; }
382         |       '!' term
383                         { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
384         |       '~' term
385                         { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
386         |       REFGEN term
387                         { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
388         |       term POSTINC
389                         { $$ = newUNOP(OP_POSTINC, 0,
390                                         mod(scalar($1), OP_POSTINC)); }
391         |       term POSTDEC
392                         { $$ = newUNOP(OP_POSTDEC, 0,
393                                         mod(scalar($1), OP_POSTDEC)); }
394         |       PREINC term
395                         { $$ = newUNOP(OP_PREINC, 0,
396                                         mod(scalar($2), OP_PREINC)); }
397         |       PREDEC term
398                         { $$ = newUNOP(OP_PREDEC, 0,
399                                         mod(scalar($2), OP_PREDEC)); }
400         |       local term      %prec UNIOP
401                         { $$ = localize($2,$1); }
402         |       '(' expr ')'
403                         { $$ = sawparens($2); }
404         |       '(' ')'
405                         { $$ = sawparens(newNULLLIST()); }
406         |       '[' expr ']'                            %prec '('
407                         { $$ = newANONLIST($2); }
408         |       '[' ']'                                 %prec '('
409                         { $$ = newANONLIST(Nullop); }
410         |       HASHBRACK expr ';' '}'                  %prec '('
411                         { $$ = newANONHASH($2); }
412         |       HASHBRACK ';' '}'                               %prec '('
413                         { $$ = newANONHASH(Nullop); }
414         |       ANONSUB startsub proto block                    %prec '('
415                         { $$ = newANONSUB($2, $3, $4); }
416         |       scalar  %prec '('
417                         { $$ = $1; }
418         |       star '{' expr ';' '}'
419                         { $$ = newBINOP(OP_GELEM, 0, newGVREF(0,$1), $3); }
420         |       star    %prec '('
421                         { $$ = $1; }
422         |       scalar '[' expr ']'     %prec '('
423                         { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
424         |       term ARROW '[' expr ']' %prec '('
425                         { $$ = newBINOP(OP_AELEM, 0,
426                                         ref(newAVREF($1),OP_RV2AV),
427                                         scalar($4));}
428         |       term '[' expr ']'       %prec '('
429                         { assertref($1); $$ = newBINOP(OP_AELEM, 0,
430                                         ref(newAVREF($1),OP_RV2AV),
431                                         scalar($3));}
432         |       hsh     %prec '('
433                         { $$ = $1; }
434         |       ary     %prec '('
435                         { $$ = $1; }
436         |       arylen  %prec '('
437                         { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
438         |       scalar '{' expr ';' '}' %prec '('
439                         { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
440                             expect = XOPERATOR; }
441         |       term ARROW '{' expr ';' '}'     %prec '('
442                         { $$ = newBINOP(OP_HELEM, 0,
443                                         ref(newHVREF($1),OP_RV2HV),
444                                         jmaybe($4));
445                             expect = XOPERATOR; }
446         |       term '{' expr ';' '}'   %prec '('
447                         { assertref($1); $$ = newBINOP(OP_HELEM, 0,
448                                         ref(newHVREF($1),OP_RV2HV),
449                                         jmaybe($3));
450                             expect = XOPERATOR; }
451         |       '(' expr ')' '[' expr ']'       %prec '('
452                         { $$ = newSLICEOP(0, $5, $2); }
453         |       '(' ')' '[' expr ']'    %prec '('
454                         { $$ = newSLICEOP(0, $4, Nullop); }
455         |       ary '[' expr ']'        %prec '('
456                         { $$ = prepend_elem(OP_ASLICE,
457                                 newOP(OP_PUSHMARK, 0),
458                                     newLISTOP(OP_ASLICE, 0,
459                                         list($3),
460                                         ref($1, OP_ASLICE))); }
461         |       ary '{' expr ';' '}'    %prec '('
462                         { $$ = prepend_elem(OP_HSLICE,
463                                 newOP(OP_PUSHMARK, 0),
464                                     newLISTOP(OP_HSLICE, 0,
465                                         list($3),
466                                         ref(oopsHV($1), OP_HSLICE)));
467                             expect = XOPERATOR; }
468         |       THING   %prec '('
469                         { $$ = $1; }
470         |       amper
471                         { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
472         |       amper '(' ')'
473                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
474         |       amper '(' expr ')'
475                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
476                             append_elem(OP_LIST, $3, scalar($1))); }
477         |       NOAMP WORD listexpr
478                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
479                             append_elem(OP_LIST, $3, scalar($2))); }
480         |       DO term %prec UNIOP
481                         { $$ = newUNOP(OP_DOFILE, 0, scalar($2)); }
482         |       DO block        %prec '('
483                         { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
484         |       DO WORD '(' ')'
485                         { $$ = newUNOP(OP_ENTERSUB,
486                             OPf_SPECIAL|OPf_STACKED,
487                             prepend_elem(OP_LIST,
488                                 scalar(newCVREF(
489                                     (OPpENTERSUB_AMPER<<8),
490                                     scalar($2)
491                                 )),Nullop)); dep();}
492         |       DO WORD '(' expr ')'
493                         { $$ = newUNOP(OP_ENTERSUB,
494                             OPf_SPECIAL|OPf_STACKED,
495                             append_elem(OP_LIST,
496                                 $4,
497                                 scalar(newCVREF(
498                                     (OPpENTERSUB_AMPER<<8),
499                                     scalar($2)
500                                 )))); dep();}
501         |       DO scalar '(' ')'
502                         { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
503                             prepend_elem(OP_LIST,
504                                 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
505         |       DO scalar '(' expr ')'
506                         { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
507                             prepend_elem(OP_LIST,
508                                 $4,
509                                 scalar(newCVREF(0,scalar($2))))); dep();}
510         |       LOOPEX
511                         { $$ = newOP($1, OPf_SPECIAL);
512                             hints |= HINT_BLOCK_SCOPE; }
513         |       LOOPEX term
514                         { $$ = newLOOPEX($1,$2); }
515         |       NOTOP argexpr
516                         { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
517         |       UNIOP
518                         { $$ = newOP($1, 0); }
519         |       UNIOP block
520                         { $$ = newUNOP($1, 0, $2); }
521         |       UNIOP term
522                         { $$ = newUNOP($1, 0, $2); }
523         |       UNIOPSUB term
524                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
525                             append_elem(OP_LIST, $2, scalar($1))); }
526         |       FUNC0
527                         { $$ = newOP($1, 0); }
528         |       FUNC0 '(' ')'
529                         { $$ = newOP($1, 0); }
530         |       FUNC0SUB
531                         { $$ = newUNOP(OP_ENTERSUB, 0,
532                                 scalar($1)); }
533         |       FUNC1 '(' ')'
534                         { $$ = newOP($1, OPf_SPECIAL); }
535         |       FUNC1 '(' expr ')'
536                         { $$ = newUNOP($1, 0, $3); }
537         |       PMFUNC '(' term ')'
538                         { $$ = pmruntime($1, $3, Nullop); }
539         |       PMFUNC '(' term ',' term ')'
540                         { $$ = pmruntime($1, $3, $5); }
541         |       WORD
542         |       listop
543         ;
544
545 listexpr:       /* NULL */
546                         { $$ = Nullop; }
547         |       argexpr
548                         { $$ = $1; }
549         ;
550
551 listexprcom:    /* NULL */
552                         { $$ = Nullop; }
553         |       expr
554                         { $$ = $1; }
555         |       expr ','
556                         { $$ = $1; }
557         ;
558
559 local   :       LOCAL   { $$ = 0; }
560         |       MY      { $$ = 1; }
561         ;
562
563 my_scalar:      scalar
564                         { in_my = 0; $$ = my($1); }
565         ;
566
567 amper   :       '&' indirob
568                         { $$ = newCVREF($1,$2); }
569         ;
570
571 scalar  :       '$' indirob
572                         { $$ = newSVREF($2); }
573         ;
574
575 ary     :       '@' indirob
576                         { $$ = newAVREF($2); }
577         ;
578
579 hsh     :       '%' indirob
580                         { $$ = newHVREF($2); }
581         ;
582
583 arylen  :       DOLSHARP indirob
584                         { $$ = newAVREF($2); }
585         ;
586
587 star    :       '*' indirob
588                         { $$ = newGVREF(0,$2); }
589         ;
590
591 indirob :       WORD
592                         { $$ = scalar($1); }
593         |       scalar
594                         { $$ = scalar($1);  }
595         |       block
596                         { $$ = scope($1); }
597
598         |       PRIVATEREF
599                         { $$ = $1; }
600         ;
601
602 %% /* PROGRAM */