3 * Copyright (c) 1991-2002, Larry Wall
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.
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.'
17 #define PERL_IN_PERLY_C
22 #define dep() deprecate("\"do\" to call subroutines")
24 /* stuff included here to make perly_c.diff apply better */
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
44 static void yydestruct(pTHX_ void *ptr);
51 #if 0 /* get this from perly.h instead */
65 #define YYLEX_PARAM (&yychar)
73 %token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
74 %token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
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
85 %type <ival> prog decl format startsub startanonsub startformsub
86 %type <ival> progstart 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
99 %left <ival> OROP DOROP
102 %nonassoc LSTOP LSTOPSUB
104 %right <ival> ASSIGNOP
110 %left <ival> BITANDOP
113 %nonassoc UNIOP UNIOPSUB
118 %right '!' '~' UMINUS REFGEN
120 %nonassoc PREINC PREDEC POSTINC POSTDEC
128 /* The whole program */
130 /*CONTINUED*/ lineseq
131 { $$ = $1; newPROG(block_end($1,$2)); }
134 /* An ordinary block */
135 block : '{' remember lineseq '}'
136 { if (PL_copline > (line_t)$1)
137 PL_copline = (line_t)$1;
138 $$ = block_end($2, $3); }
141 remember: /* NULL */ /* start a full lexical scope */
142 { $$ = block_start(TRUE); }
147 #if defined(YYDEBUG) && defined(DEBUGGING)
148 yydebug = (DEBUG_p_TEST);
150 PL_expect = XSTATE; $$ = block_start(TRUE);
155 mblock : '{' mremember lineseq '}'
156 { if (PL_copline > (line_t)$1)
157 PL_copline = (line_t)$1;
158 $$ = block_end($2, $3); }
161 mremember: /* NULL */ /* start a partial lexical scope */
162 { $$ = block_start(FALSE); }
165 /* A collection of "lines" in the program */
171 { $$ = append_list(OP_LINESEQ,
172 (LISTOP*)$1, (LISTOP*)$2);
173 PL_pad_reset_pending = TRUE;
174 if ($1 && $2) PL_hints |= HINT_BLOCK_SCOPE; }
177 /* A "line" in the program */
179 { $$ = newSTATEOP(0, $1, $2); }
180 | loop /* loops add their own labels */
182 { if ($1 != Nullch) {
183 $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0));
189 PL_expect = XSTATE; }
191 { $$ = newSTATEOP(0, $1, $2);
192 PL_expect = XSTATE; }
195 /* An expression which may have a side-effect */
201 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
203 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
205 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
207 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);}
209 { $$ = newFOROP(0, Nullch, (line_t)$2,
210 Nullop, $3, $1, Nullop); }
213 /* else and elsif blocks */
217 { ($2)->op_flags |= OPf_PARENS; $$ = scope($2); }
218 | ELSIF '(' mexpr ')' mblock else
219 { PL_copline = (line_t)$1;
220 $$ = newCONDOP(0, $3, scope($5), $6);
221 PL_hints |= HINT_BLOCK_SCOPE; }
224 /* Real conditional expressions */
225 cond : IF '(' remember mexpr ')' mblock else
226 { PL_copline = (line_t)$1;
228 newCONDOP(0, $4, scope($6), $7)); }
229 | UNLESS '(' remember miexpr ')' mblock else
230 { PL_copline = (line_t)$1;
232 newCONDOP(0, $4, scope($6), $7)); }
235 /* Continue blocks */
242 /* Loops: while, until, for, and a bare block */
243 loop : label WHILE '(' remember mtexpr ')' mblock cont
244 { PL_copline = (line_t)$2;
247 newWHILEOP(0, 1, (LOOP*)Nullop,
249 | label UNTIL '(' remember miexpr ')' mblock cont
250 { PL_copline = (line_t)$2;
253 newWHILEOP(0, 1, (LOOP*)Nullop,
255 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
257 newFOROP(0, $1, (line_t)$2, $5, $7, $9, $10)); }
258 | label FOR scalar '(' remember mexpr ')' mblock cont
260 newFOROP(0, $1, (line_t)$2, mod($3, OP_ENTERLOOP),
262 | label FOR '(' remember mexpr ')' mblock cont
264 newFOROP(0, $1, (line_t)$2, Nullop, $5, $7, $8)); }
265 | label FOR '(' remember mnexpr ';' mtexpr ';' mnexpr ')' mblock
266 /* basically fake up an initialize-while lineseq */
268 PL_copline = (line_t)$2;
269 forop = newSTATEOP(0, $1,
270 newWHILEOP(0, 1, (LOOP*)Nullop,
274 forop = append_elem(OP_LINESEQ,
275 newSTATEOP(0, ($1?savepv($1):Nullch),
280 $$ = block_end($4, forop); }
281 | label block cont /* a block is a loop that happens once */
282 { $$ = newSTATEOP(0, $1,
283 newWHILEOP(0, 1, (LOOP*)Nullop,
284 NOLINE, Nullop, $2, $3)); }
287 /* Normal expression */
293 /* Boolean expression */
294 texpr : /* NULL means true */
295 { (void)scan_num("1", &yylval); $$ = yylval.opval; }
299 /* Inverted boolean expression */
301 { $$ = invert(scalar($1)); }
304 /* Expression with its own lexical scope */
306 { $$ = $1; intro_my(); }
310 { $$ = $1; intro_my(); }
314 { $$ = $1; intro_my(); }
318 { $$ = $1; intro_my(); }
321 /* Optional "MAIN:"-style loop labels */
327 /* Some kind of declaration - does not take part in the parse tree */
340 format : FORMAT startformsub formname block
341 { newFORM($2, $3, $4); }
344 formname: WORD { $$ = $1; }
345 | /* NULL */ { $$ = Nullop; }
348 /* Unimplemented "my sub foo { }" */
349 mysubrout: MYSUB startsub subname proto subattrlist subbody
350 { newMYSUB($2, $3, $4, $5, $6); }
353 /* Subroutine definition */
354 subrout : SUB startsub subname proto subattrlist subbody
355 { newATTRSUB($2, $3, $4, $5, $6); }
358 startsub: /* NULL */ /* start a regular subroutine scope */
359 { $$ = start_subparse(FALSE, 0); }
362 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
363 { $$ = start_subparse(FALSE, CVf_ANON); }
366 startformsub: /* NULL */ /* start a format subroutine scope */
367 { $$ = start_subparse(TRUE, 0); }
370 /* Name of a subroutine - must be a bareword, could be special */
371 subname : WORD { STRLEN n_a; char *name = SvPV(((SVOP*)$1)->op_sv,n_a);
372 if (strEQ(name, "BEGIN") || strEQ(name, "END")
373 || strEQ(name, "INIT") || strEQ(name, "CHECK"))
374 CvSPECIAL_on(PL_compcv);
378 /* Subroutine prototype */
384 /* Optional list of subroutine attributes */
385 subattrlist: /* NULL */
393 /* List of attributes for a "my" variable declaration */
394 myattrlist: COLONATTR THING
400 /* Subroutine body - either null or a block */
401 subbody : block { $$ = $1; }
402 | ';' { $$ = Nullop; PL_expect = XSTATE; }
405 package : PACKAGE WORD ';'
412 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
413 WORD WORD listexpr ';'
414 { utilize($1, $2, $4, $5, $6); }
417 /* Ordinary expressions; logical combinations */
418 expr : expr ANDOP expr
419 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
421 { $$ = newLOGOP($2, 0, $1, $3); }
423 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
424 | argexpr %prec PREC_LOW
427 /* Expressions are a list of terms joined by commas */
428 argexpr : argexpr ','
431 { $$ = append_elem(OP_LIST, $1, $3); }
432 | term %prec PREC_LOW
436 listop : LSTOP indirob argexpr /* print $fh @args */
437 { $$ = convert($1, OPf_STACKED,
438 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
439 | FUNC '(' indirob expr ')' /* print ($fh @args */
440 { $$ = convert($1, OPf_STACKED,
441 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
442 | term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
443 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
445 prepend_elem(OP_LIST, scalar($1), $5),
446 newUNOP(OP_METHOD, 0, $3))); }
447 | term ARROW method /* $foo->bar */
448 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
449 append_elem(OP_LIST, scalar($1),
450 newUNOP(OP_METHOD, 0, $3))); }
451 | METHOD indirob listexpr /* new Class @args */
452 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
454 prepend_elem(OP_LIST, $2, $3),
455 newUNOP(OP_METHOD, 0, $1))); }
456 | FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
457 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
459 prepend_elem(OP_LIST, $2, $4),
460 newUNOP(OP_METHOD, 0, $1))); }
461 | LSTOP listexpr /* print @args */
462 { $$ = convert($1, 0, $2); }
463 | FUNC '(' listexprcom ')' /* print (@args) */
464 { $$ = convert($1, 0, $3); }
465 | LSTOPSUB startanonsub block /* map { foo } ... */
466 { $3 = newANONATTRSUB($2, 0, Nullop, $3); }
467 listexpr %prec LSTOP /* ... @bar */
468 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
470 prepend_elem(OP_LIST, $3, $5), $1)); }
473 /* Names of methods. May use $object->$methodname */
478 /* Some kind of subscripted expression */
479 subscripted: star '{' expr ';' '}' /* *main::{something} */
480 /* In this and all the hash accessors, ';' is
481 * provided by the tokeniser */
482 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
483 | scalar '[' expr ']' /* $array[$element] */
484 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
485 | term ARROW '[' expr ']' /* somearef->[$element] */
486 { $$ = newBINOP(OP_AELEM, 0,
487 ref(newAVREF($1),OP_RV2AV),
489 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
490 { $$ = newBINOP(OP_AELEM, 0,
491 ref(newAVREF($1),OP_RV2AV),
493 | scalar '{' expr ';' '}' /* $foo->{bar();} */
494 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
495 PL_expect = XOPERATOR; }
496 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
497 { $$ = newBINOP(OP_HELEM, 0,
498 ref(newHVREF($1),OP_RV2HV),
500 PL_expect = XOPERATOR; }
501 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
502 { $$ = newBINOP(OP_HELEM, 0,
503 ref(newHVREF($1),OP_RV2HV),
505 PL_expect = XOPERATOR; }
506 | term ARROW '(' ')' /* $subref->() */
507 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
508 newCVREF(0, scalar($1))); }
509 | term ARROW '(' expr ')' /* $subref->(@args) */
510 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
511 append_elem(OP_LIST, $4,
512 newCVREF(0, scalar($1)))); }
514 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
515 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
516 append_elem(OP_LIST, $3,
517 newCVREF(0, scalar($1)))); }
518 | subscripted '(' ')' /* $foo->{bar}->() */
519 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
520 newCVREF(0, scalar($1))); }
523 /* Binary operators between terms */
524 termbinop : term ASSIGNOP term /* $x = $y */
525 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
526 | term POWOP term /* $x ** $y */
527 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
528 | term MULOP term /* $x * $y, $x x $y */
529 { if ($2 != OP_REPEAT)
531 $$ = newBINOP($2, 0, $1, scalar($3)); }
532 | term ADDOP term /* $x + $y */
533 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
534 | term SHIFTOP term /* $x >> $y, $x << $y */
535 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
536 | term RELOP term /* $x > $y, etc. */
537 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
538 | term EQOP term /* $x == $y, $x eq $y */
539 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
540 | term BITANDOP term /* $x & $y */
541 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
542 | term BITOROP term /* $x | $y */
543 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
544 | term DOTDOT term /* $x..$y, $x...$y */
545 { $$ = newRANGE($2, scalar($1), scalar($3));}
546 | term ANDAND term /* $x && $y */
547 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
548 | term OROR term /* $x || $y */
549 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
550 | term DORDOR term /* $x // $y */
551 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
552 | term MATCHOP term /* $x =~ /$y/ */
553 { $$ = bind_match($2, $1, $3); }
556 /* Unary operators and terms */
557 termunop : '-' term %prec UMINUS /* -$x */
558 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
559 | '+' term %prec UMINUS /* +$x */
562 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
564 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
565 | term POSTINC /* $x++ */
566 { $$ = newUNOP(OP_POSTINC, 0,
567 mod(scalar($1), OP_POSTINC)); }
568 | term POSTDEC /* $x-- */
569 { $$ = newUNOP(OP_POSTDEC, 0,
570 mod(scalar($1), OP_POSTDEC)); }
571 | PREINC term /* ++$x */
572 { $$ = newUNOP(OP_PREINC, 0,
573 mod(scalar($2), OP_PREINC)); }
574 | PREDEC term /* --$x */
575 { $$ = newUNOP(OP_PREDEC, 0,
576 mod(scalar($2), OP_PREDEC)); }
580 /* Constructors for anonymous data */
581 anonymous: '[' expr ']'
582 { $$ = newANONLIST($2); }
584 { $$ = newANONLIST(Nullop); }
585 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
586 { $$ = newANONHASH($2); }
587 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
588 { $$ = newANONHASH(Nullop); }
589 | ANONSUB startanonsub proto subattrlist block %prec '('
590 { $$ = newANONATTRSUB($2, $3, $4, $5); }
594 /* Things called with "do" */
595 termdo : DO term %prec UNIOP /* do $filename */
597 | DO block %prec '(' /* do { code */
598 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
599 | DO WORD '(' ')' /* do somesub() */
600 { $$ = newUNOP(OP_ENTERSUB,
601 OPf_SPECIAL|OPf_STACKED,
602 prepend_elem(OP_LIST,
604 (OPpENTERSUB_AMPER<<8),
607 | DO WORD '(' expr ')' /* do somesub(@args) */
608 { $$ = newUNOP(OP_ENTERSUB,
609 OPf_SPECIAL|OPf_STACKED,
613 (OPpENTERSUB_AMPER<<8),
616 | DO scalar '(' ')' /* do $subref () */
617 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
618 prepend_elem(OP_LIST,
619 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
620 | DO scalar '(' expr ')' /* do $subref (@args) */
621 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
622 prepend_elem(OP_LIST,
624 scalar(newCVREF(0,scalar($2))))); dep();}
632 | term '?' term ':' term
633 { $$ = newCONDOP(0, $1, $3, $5); }
634 | REFGEN term /* \$x, \@y, \%z */
635 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
636 | myattrterm %prec UNIOP
638 | LOCAL term %prec UNIOP
639 { $$ = localize($2,$1); }
641 { $$ = sawparens($2); }
643 { $$ = sawparens(newNULLLIST()); }
652 | arylen %prec '(' /* $#x, $#{ something } */
653 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
656 | '(' expr ')' '[' expr ']' /* list slice */
657 { $$ = newSLICEOP(0, $5, $2); }
658 | '(' ')' '[' expr ']' /* empty list slice! */
659 { $$ = newSLICEOP(0, $4, Nullop); }
660 | ary '[' expr ']' /* array slice */
661 { $$ = prepend_elem(OP_ASLICE,
662 newOP(OP_PUSHMARK, 0),
663 newLISTOP(OP_ASLICE, 0,
665 ref($1, OP_ASLICE))); }
666 | ary '{' expr ';' '}' /* @hash{@keys} */
667 { $$ = prepend_elem(OP_HSLICE,
668 newOP(OP_PUSHMARK, 0),
669 newLISTOP(OP_HSLICE, 0,
671 ref(oopsHV($1), OP_HSLICE)));
672 PL_expect = XOPERATOR; }
676 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
677 | amper '(' ')' /* &foo() */
678 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
679 | amper '(' expr ')' /* &foo(@args) */
680 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
681 append_elem(OP_LIST, $3, scalar($1))); }
682 | NOAMP WORD listexpr /* foo(@args) */
683 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
684 append_elem(OP_LIST, $3, scalar($2))); }
685 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
686 { $$ = newOP($1, OPf_SPECIAL);
687 PL_hints |= HINT_BLOCK_SCOPE; }
689 { $$ = newLOOPEX($1,$2); }
690 | NOTOP argexpr /* not $foo */
691 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
692 | UNIOP /* Unary op, $_ implied */
693 { $$ = newOP($1, 0); }
694 | UNIOP block /* eval { foo }, I *think* */
695 { $$ = newUNOP($1, 0, $2); }
696 | UNIOP term /* Unary op */
697 { $$ = newUNOP($1, 0, $2); }
698 | UNIOPSUB term /* Sub treated as unop */
699 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
700 append_elem(OP_LIST, $2, scalar($1))); }
701 | FUNC0 /* Nullary operator */
702 { $$ = newOP($1, 0); }
704 { $$ = newOP($1, 0); }
705 | FUNC0SUB /* Sub treated as nullop */
706 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
708 | FUNC1 '(' ')' /* not () */
709 { $$ = newOP($1, OPf_SPECIAL); }
710 | FUNC1 '(' expr ')' /* not($foo) */
711 { $$ = newUNOP($1, 0, $3); }
712 | PMFUNC '(' term ')' /* split (/foo/) */
713 { $$ = pmruntime($1, $3, Nullop); }
714 | PMFUNC '(' term ',' term ')' /* split (/foo/,$bar) */
715 { $$ = pmruntime($1, $3, $5); }
720 /* "my" declarations, with optional attributes */
721 myattrterm: MY myterm myattrlist
722 { $$ = my_attrs($2,$3); }
724 { $$ = localize($2,$1); }
727 /* Things that can be "my"'d */
728 myterm : '(' expr ')'
729 { $$ = sawparens($2); }
731 { $$ = sawparens(newNULLLIST()); }
740 /* Basic list expressions */
741 listexpr: /* NULL */ %prec PREC_LOW
743 | argexpr %prec PREC_LOW
747 listexprcom: /* NULL */
755 /* A little bit of trickery to make "for my $foo (@bar)" actually be
758 { PL_in_my = 0; $$ = my($1); }
762 { $$ = newCVREF($1,$2); }
766 { $$ = newSVREF($2); }
770 { $$ = newAVREF($2); }
774 { $$ = newHVREF($2); }
777 arylen : DOLSHARP indirob
778 { $$ = newAVREF($2); }
782 { $$ = newGVREF(0,$2); }
785 /* Indirect objects */
788 | scalar %prec PREC_LOW
799 /* more stuff added to make perly_c.diff easier to apply */
804 #define yyparse() Perl_yyparse(pTHX)