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 ';'
410 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
411 WORD WORD listexpr ';'
412 { utilize($1, $2, $4, $5, $6); }
415 /* Ordinary expressions; logical combinations */
416 expr : expr ANDOP expr
417 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
419 { $$ = newLOGOP($2, 0, $1, $3); }
421 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
422 | argexpr %prec PREC_LOW
425 /* Expressions are a list of terms joined by commas */
426 argexpr : argexpr ','
429 { $$ = append_elem(OP_LIST, $1, $3); }
430 | term %prec PREC_LOW
434 listop : LSTOP indirob argexpr /* print $fh @args */
435 { $$ = convert($1, OPf_STACKED,
436 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
437 | FUNC '(' indirob expr ')' /* print ($fh @args */
438 { $$ = convert($1, OPf_STACKED,
439 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
440 | term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
441 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
443 prepend_elem(OP_LIST, scalar($1), $5),
444 newUNOP(OP_METHOD, 0, $3))); }
445 | term ARROW method /* $foo->bar */
446 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
447 append_elem(OP_LIST, scalar($1),
448 newUNOP(OP_METHOD, 0, $3))); }
449 | METHOD indirob listexpr /* new Class @args */
450 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
452 prepend_elem(OP_LIST, $2, $3),
453 newUNOP(OP_METHOD, 0, $1))); }
454 | FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
455 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
457 prepend_elem(OP_LIST, $2, $4),
458 newUNOP(OP_METHOD, 0, $1))); }
459 | LSTOP listexpr /* print @args */
460 { $$ = convert($1, 0, $2); }
461 | FUNC '(' listexprcom ')' /* print (@args) */
462 { $$ = convert($1, 0, $3); }
463 | LSTOPSUB startanonsub block /* map { foo } ... */
464 { $3 = newANONATTRSUB($2, 0, Nullop, $3); }
465 listexpr %prec LSTOP /* ... @bar */
466 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
468 prepend_elem(OP_LIST, $3, $5), $1)); }
471 /* Names of methods. May use $object->$methodname */
476 /* Some kind of subscripted expression */
477 subscripted: star '{' expr ';' '}' /* *main::{something} */
478 /* In this and all the hash accessors, ';' is
479 * provided by the tokeniser */
480 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
481 PL_expect = XOPERATOR; }
482 | scalar '[' expr ']' /* $array[$element] */
483 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
484 | term ARROW '[' expr ']' /* somearef->[$element] */
485 { $$ = newBINOP(OP_AELEM, 0,
486 ref(newAVREF($1),OP_RV2AV),
488 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
489 { $$ = newBINOP(OP_AELEM, 0,
490 ref(newAVREF($1),OP_RV2AV),
492 | scalar '{' expr ';' '}' /* $foo->{bar();} */
493 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
494 PL_expect = XOPERATOR; }
495 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
496 { $$ = newBINOP(OP_HELEM, 0,
497 ref(newHVREF($1),OP_RV2HV),
499 PL_expect = XOPERATOR; }
500 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
501 { $$ = newBINOP(OP_HELEM, 0,
502 ref(newHVREF($1),OP_RV2HV),
504 PL_expect = XOPERATOR; }
505 | term ARROW '(' ')' /* $subref->() */
506 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
507 newCVREF(0, scalar($1))); }
508 | term ARROW '(' expr ')' /* $subref->(@args) */
509 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
510 append_elem(OP_LIST, $4,
511 newCVREF(0, scalar($1)))); }
513 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
514 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
515 append_elem(OP_LIST, $3,
516 newCVREF(0, scalar($1)))); }
517 | subscripted '(' ')' /* $foo->{bar}->() */
518 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
519 newCVREF(0, scalar($1))); }
522 /* Binary operators between terms */
523 termbinop : term ASSIGNOP term /* $x = $y */
524 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
525 | term POWOP term /* $x ** $y */
526 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
527 | term MULOP term /* $x * $y, $x x $y */
528 { if ($2 != OP_REPEAT)
530 $$ = newBINOP($2, 0, $1, scalar($3)); }
531 | term ADDOP term /* $x + $y */
532 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
533 | term SHIFTOP term /* $x >> $y, $x << $y */
534 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
535 | term RELOP term /* $x > $y, etc. */
536 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
537 | term EQOP term /* $x == $y, $x eq $y */
538 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
539 | term BITANDOP term /* $x & $y */
540 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
541 | term BITOROP term /* $x | $y */
542 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
543 | term DOTDOT term /* $x..$y, $x...$y */
544 { $$ = newRANGE($2, scalar($1), scalar($3));}
545 | term ANDAND term /* $x && $y */
546 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
547 | term OROR term /* $x || $y */
548 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
549 | term DORDOR term /* $x // $y */
550 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
551 | term MATCHOP term /* $x =~ /$y/ */
552 { $$ = bind_match($2, $1, $3); }
555 /* Unary operators and terms */
556 termunop : '-' term %prec UMINUS /* -$x */
557 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
558 | '+' term %prec UMINUS /* +$x */
561 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
563 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
564 | term POSTINC /* $x++ */
565 { $$ = newUNOP(OP_POSTINC, 0,
566 mod(scalar($1), OP_POSTINC)); }
567 | term POSTDEC /* $x-- */
568 { $$ = newUNOP(OP_POSTDEC, 0,
569 mod(scalar($1), OP_POSTDEC)); }
570 | PREINC term /* ++$x */
571 { $$ = newUNOP(OP_PREINC, 0,
572 mod(scalar($2), OP_PREINC)); }
573 | PREDEC term /* --$x */
574 { $$ = newUNOP(OP_PREDEC, 0,
575 mod(scalar($2), OP_PREDEC)); }
579 /* Constructors for anonymous data */
580 anonymous: '[' expr ']'
581 { $$ = newANONLIST($2); }
583 { $$ = newANONLIST(Nullop); }
584 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
585 { $$ = newANONHASH($2); }
586 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
587 { $$ = newANONHASH(Nullop); }
588 | ANONSUB startanonsub proto subattrlist block %prec '('
589 { $$ = newANONATTRSUB($2, $3, $4, $5); }
593 /* Things called with "do" */
594 termdo : DO term %prec UNIOP /* do $filename */
596 | DO block %prec '(' /* do { code */
597 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
598 | DO WORD '(' ')' /* do somesub() */
599 { $$ = newUNOP(OP_ENTERSUB,
600 OPf_SPECIAL|OPf_STACKED,
601 prepend_elem(OP_LIST,
603 (OPpENTERSUB_AMPER<<8),
606 | DO WORD '(' expr ')' /* do somesub(@args) */
607 { $$ = newUNOP(OP_ENTERSUB,
608 OPf_SPECIAL|OPf_STACKED,
612 (OPpENTERSUB_AMPER<<8),
615 | DO scalar '(' ')' /* do $subref () */
616 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
617 prepend_elem(OP_LIST,
618 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
619 | DO scalar '(' expr ')' /* do $subref (@args) */
620 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
621 prepend_elem(OP_LIST,
623 scalar(newCVREF(0,scalar($2))))); dep();}
631 | term '?' term ':' term
632 { $$ = newCONDOP(0, $1, $3, $5); }
633 | REFGEN term /* \$x, \@y, \%z */
634 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
635 | myattrterm %prec UNIOP
637 | LOCAL term %prec UNIOP
638 { $$ = localize($2,$1); }
640 { $$ = sawparens($2); }
642 { $$ = sawparens(newNULLLIST()); }
651 | arylen %prec '(' /* $#x, $#{ something } */
652 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
655 | '(' expr ')' '[' expr ']' /* list slice */
656 { $$ = newSLICEOP(0, $5, $2); }
657 | '(' ')' '[' expr ']' /* empty list slice! */
658 { $$ = newSLICEOP(0, $4, Nullop); }
659 | ary '[' expr ']' /* array slice */
660 { $$ = prepend_elem(OP_ASLICE,
661 newOP(OP_PUSHMARK, 0),
662 newLISTOP(OP_ASLICE, 0,
664 ref($1, OP_ASLICE))); }
665 | ary '{' expr ';' '}' /* @hash{@keys} */
666 { $$ = prepend_elem(OP_HSLICE,
667 newOP(OP_PUSHMARK, 0),
668 newLISTOP(OP_HSLICE, 0,
670 ref(oopsHV($1), OP_HSLICE)));
671 PL_expect = XOPERATOR; }
675 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
676 | amper '(' ')' /* &foo() */
677 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
678 | amper '(' expr ')' /* &foo(@args) */
679 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
680 append_elem(OP_LIST, $3, scalar($1))); }
681 | NOAMP WORD listexpr /* foo(@args) */
682 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
683 append_elem(OP_LIST, $3, scalar($2))); }
684 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
685 { $$ = newOP($1, OPf_SPECIAL);
686 PL_hints |= HINT_BLOCK_SCOPE; }
688 { $$ = newLOOPEX($1,$2); }
689 | NOTOP argexpr /* not $foo */
690 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
691 | UNIOP /* Unary op, $_ implied */
692 { $$ = newOP($1, 0); }
693 | UNIOP block /* eval { foo }, I *think* */
694 { $$ = newUNOP($1, 0, $2); }
695 | UNIOP term /* Unary op */
696 { $$ = newUNOP($1, 0, $2); }
697 | UNIOPSUB term /* Sub treated as unop */
698 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
699 append_elem(OP_LIST, $2, scalar($1))); }
700 | FUNC0 /* Nullary operator */
701 { $$ = newOP($1, 0); }
703 { $$ = newOP($1, 0); }
704 | FUNC0SUB /* Sub treated as nullop */
705 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
707 | FUNC1 '(' ')' /* not () */
708 { $$ = newOP($1, OPf_SPECIAL); }
709 | FUNC1 '(' expr ')' /* not($foo) */
710 { $$ = newUNOP($1, 0, $3); }
711 | PMFUNC '(' term ')' /* split (/foo/) */
712 { $$ = pmruntime($1, $3, Nullop); }
713 | PMFUNC '(' term ',' term ')' /* split (/foo/,$bar) */
714 { $$ = pmruntime($1, $3, $5); }
719 /* "my" declarations, with optional attributes */
720 myattrterm: MY myterm myattrlist
721 { $$ = my_attrs($2,$3); }
723 { $$ = localize($2,$1); }
726 /* Things that can be "my"'d */
727 myterm : '(' expr ')'
728 { $$ = sawparens($2); }
730 { $$ = sawparens(newNULLLIST()); }
739 /* Basic list expressions */
740 listexpr: /* NULL */ %prec PREC_LOW
742 | argexpr %prec PREC_LOW
746 listexprcom: /* NULL */
754 /* A little bit of trickery to make "for my $foo (@bar)" actually be
757 { PL_in_my = 0; $$ = my($1); }
761 { $$ = newCVREF($1,$2); }
765 { $$ = newSVREF($2); }
769 { $$ = newAVREF($2); }
773 { $$ = newHVREF($2); }
776 arylen : DOLSHARP indirob
777 { $$ = newAVREF($2); }
781 { $$ = newGVREF(0,$2); }
784 /* Indirect objects */
787 | scalar %prec PREC_LOW
798 /* more stuff added to make perly_c.diff easier to apply */
803 #define yyparse() Perl_yyparse(pTHX)