3 * Copyright (c) 1991-2001, 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(pTHXo_ 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> 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
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 */
131 #if defined(YYDEBUG) && defined(DEBUGGING)
132 yydebug = (DEBUG_p_TEST);
134 PL_expect = XSTATE; $$ = block_start(TRUE);
136 /*CONTINUED*/ lineseq
137 { newPROG(block_end($1,$2)); }
140 /* An ordinary block */
141 block : '{' remember lineseq '}'
142 { if (PL_copline > (line_t)$1)
144 $$ = block_end($2, $3); }
147 remember: /* NULL */ /* start a full lexical scope */
148 { $$ = block_start(TRUE); }
151 mblock : '{' mremember lineseq '}'
152 { if (PL_copline > (line_t)$1)
154 $$ = block_end($2, $3); }
157 mremember: /* NULL */ /* start a partial lexical scope */
158 { $$ = block_start(FALSE); }
161 /* A collection of "lines" in the program */
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; }
173 /* A "line" in the program */
175 { $$ = newSTATEOP(0, $1, $2); }
176 | loop /* loops add their own labels */
178 { if ($1 != Nullch) {
179 $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0));
185 PL_expect = XSTATE; }
187 { $$ = newSTATEOP(0, $1, $2);
188 PL_expect = XSTATE; }
191 /* An expression which may have a side-effect */
197 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
199 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
201 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
203 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);}
205 { $$ = newFOROP(0, Nullch, $2,
206 Nullop, $3, $1, Nullop); }
209 /* else and elsif blocks */
213 { ($2)->op_flags |= OPf_PARENS; $$ = scope($2); }
214 | ELSIF '(' mexpr ')' mblock else
216 $$ = newCONDOP(0, $3, scope($5), $6);
217 PL_hints |= HINT_BLOCK_SCOPE; }
220 /* Real conditional expressions */
221 cond : IF '(' remember mexpr ')' mblock else
224 newCONDOP(0, $4, scope($6), $7)); }
225 | UNLESS '(' remember miexpr ')' mblock else
228 newCONDOP(0, $4, scope($6), $7)); }
231 /* Continue blocks */
238 /* Loops: while, until, for, and a bare block */
239 loop : label WHILE '(' remember mtexpr ')' mblock cont
243 newWHILEOP(0, 1, (LOOP*)Nullop,
245 | label UNTIL '(' remember miexpr ')' mblock cont
249 newWHILEOP(0, 1, (LOOP*)Nullop,
251 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
253 newFOROP(0, $1, $2, $5, $7, $9, $10)); }
254 | label FOR scalar '(' remember mexpr ')' mblock cont
256 newFOROP(0, $1, $2, mod($3, OP_ENTERLOOP),
258 | label FOR '(' remember mexpr ')' mblock cont
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 */
265 forop = newSTATEOP(0, $1,
266 newWHILEOP(0, 1, (LOOP*)Nullop,
270 forop = append_elem(OP_LINESEQ,
271 newSTATEOP(0, ($1?savepv($1):Nullch),
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)); }
283 /* Normal expression */
289 /* Boolean expression */
290 texpr : /* NULL means true */
291 { (void)scan_num("1", &yylval); $$ = yylval.opval; }
295 /* Inverted boolean expression */
297 { $$ = invert(scalar($1)); }
300 /* Expression with its own lexical scope */
302 { $$ = $1; intro_my(); }
306 { $$ = $1; intro_my(); }
310 { $$ = $1; intro_my(); }
314 { $$ = $1; intro_my(); }
317 /* Optional "MAIN:"-style loop labels */
323 /* Some kind of declaration - does not take part in the parse tree */
336 format : FORMAT startformsub formname block
337 { newFORM($2, $3, $4); }
340 formname: WORD { $$ = $1; }
341 | /* NULL */ { $$ = Nullop; }
344 /* Unimplemented "my sub foo { }" */
345 mysubrout: MYSUB startsub subname proto subattrlist subbody
346 { newMYSUB($2, $3, $4, $5, $6); }
349 /* Subroutine definition */
350 subrout : SUB startsub subname proto subattrlist subbody
351 { newATTRSUB($2, $3, $4, $5, $6); }
354 startsub: /* NULL */ /* start a regular subroutine scope */
355 { $$ = start_subparse(FALSE, 0); }
358 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
359 { $$ = start_subparse(FALSE, CVf_ANON); }
362 startformsub: /* NULL */ /* start a format subroutine scope */
363 { $$ = start_subparse(TRUE, 0); }
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);
374 /* Subroutine prototype */
380 /* Optional list of subroutine attributes */
381 subattrlist: /* NULL */
389 /* List of attributes for a "my" variable declaration */
390 myattrlist: COLONATTR THING
396 /* Subroutine body - either null or a block */
397 subbody : block { $$ = $1; }
398 | ';' { $$ = Nullop; PL_expect = XSTATE; }
401 package : PACKAGE WORD ';'
408 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
409 WORD WORD listexpr ';'
410 { utilize($1, $2, $4, $5, $6); }
413 /* Ordinary expressions; logical combinations */
414 expr : expr ANDOP expr
415 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
417 { $$ = newLOGOP($2, 0, $1, $3); }
418 | argexpr %prec PREC_LOW
421 /* Expressions are a list of terms joined by commas */
422 argexpr : argexpr ','
425 { $$ = append_elem(OP_LIST, $1, $3); }
426 | term %prec PREC_LOW
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,
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,
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,
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,
464 prepend_elem(OP_LIST, $3, $5), $1)); }
467 /* Names of methods. May use $object->$methodname */
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),
483 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
484 { $$ = newBINOP(OP_AELEM, 0,
485 ref(newAVREF($1),OP_RV2AV),
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),
494 PL_expect = XOPERATOR; }
495 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
496 { $$ = newBINOP(OP_HELEM, 0,
497 ref(newHVREF($1),OP_RV2HV),
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)))); }
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))); }
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)
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); }
548 /* Unary operators and terms */
549 termunop : '-' term %prec UMINUS /* -$x */
550 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
551 | '+' term %prec UMINUS /* +$x */
554 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
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)); }
572 /* Constructors for anonymous data */
573 anonymous: '[' expr ']'
574 { $$ = newANONLIST($2); }
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); }
586 /* Things called with "do" */
587 termdo : DO term %prec UNIOP /* do $filename */
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,
596 (OPpENTERSUB_AMPER<<8),
599 | DO WORD '(' expr ')' /* do somesub(@args) */
600 { $$ = newUNOP(OP_ENTERSUB,
601 OPf_SPECIAL|OPf_STACKED,
605 (OPpENTERSUB_AMPER<<8),
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,
616 scalar(newCVREF(0,scalar($2))))); dep();}
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
630 | LOCAL term %prec UNIOP
631 { $$ = localize($2,$1); }
633 { $$ = sawparens($2); }
635 { $$ = sawparens(newNULLLIST()); }
644 | arylen %prec '(' /* $#x, $#{ something } */
645 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
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,
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,
663 ref(oopsHV($1), OP_HSLICE)));
664 PL_expect = XOPERATOR; }
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; }
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); }
696 { $$ = newOP($1, 0); }
697 | FUNC0SUB /* Sub treated as nullop */
698 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
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); }
712 /* "my" declarations, with optional attributes */
713 myattrterm: MY myterm myattrlist
714 { $$ = my_attrs($2,$3); }
716 { $$ = localize($2,$1); }
719 /* Things that can be "my"'d */
720 myterm : '(' expr ')'
721 { $$ = sawparens($2); }
723 { $$ = sawparens(newNULLLIST()); }
732 /* Basic list expressions */
733 listexpr: /* NULL */ %prec PREC_LOW
735 | argexpr %prec PREC_LOW
739 listexprcom: /* NULL */
747 /* A little bit of trickery to make "for my $foo (@bar)" actually be
750 { PL_in_my = 0; $$ = my($1); }
754 { $$ = newCVREF($1,$2); }
758 { $$ = newSVREF($2); }
762 { $$ = newAVREF($2); }
766 { $$ = newHVREF($2); }
769 arylen : DOLSHARP indirob
770 { $$ = newAVREF($2); }
774 { $$ = newGVREF(0,$2); }
777 /* Indirect objects */
780 | scalar %prec PREC_LOW
791 /* more stuff added to make perly_c.diff easier to apply */
796 #define yyparse() Perl_yyparse(pTHX)