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
101 %nonassoc LSTOP LSTOPSUB
103 %right <ival> ASSIGNOP
109 %left <ival> BITANDOP
112 %nonassoc UNIOP UNIOPSUB
117 %right '!' '~' UMINUS REFGEN
119 %nonassoc PREINC PREDEC POSTINC POSTDEC
129 #if defined(YYDEBUG) && defined(DEBUGGING)
130 yydebug = (DEBUG_p_TEST);
134 /*CONTINUED*/ lineseq
138 block : '{' remember lineseq '}'
139 { if (PL_copline > (line_t)$1)
141 $$ = block_end($2, $3); }
144 remember: /* NULL */ /* start a full lexical scope */
145 { $$ = block_start(TRUE); }
148 mblock : '{' mremember lineseq '}'
149 { if (PL_copline > (line_t)$1)
151 $$ = block_end($2, $3); }
154 mremember: /* NULL */ /* start a partial lexical scope */
155 { $$ = block_start(FALSE); }
163 { $$ = append_list(OP_LINESEQ,
164 (LISTOP*)$1, (LISTOP*)$2);
165 PL_pad_reset_pending = TRUE;
166 if ($1 && $2) PL_hints |= HINT_BLOCK_SCOPE; }
170 { $$ = newSTATEOP(0, $1, $2); }
171 | loop /* loops add their own labels */
173 { if ($1 != Nullch) {
174 $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0));
180 PL_expect = XSTATE; }
182 { $$ = newSTATEOP(0, $1, $2);
183 PL_expect = XSTATE; }
191 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
193 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
195 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
197 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);}
199 { $$ = newFOROP(0, Nullch, $2,
200 Nullop, $3, $1, Nullop); }
206 { ($2)->op_flags |= OPf_PARENS; $$ = scope($2); }
207 | ELSIF '(' mexpr ')' mblock else
209 $$ = newCONDOP(0, $3, scope($5), $6);
210 PL_hints |= HINT_BLOCK_SCOPE; }
213 cond : IF '(' remember mexpr ')' mblock else
216 newCONDOP(0, $4, scope($6), $7)); }
217 | UNLESS '(' remember miexpr ')' mblock else
220 newCONDOP(0, $4, scope($6), $7)); }
229 loop : label WHILE '(' remember mtexpr ')' mblock cont
233 newWHILEOP(0, 1, (LOOP*)Nullop,
235 | label UNTIL '(' remember miexpr ')' mblock cont
239 newWHILEOP(0, 1, (LOOP*)Nullop,
241 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
243 newFOROP(0, $1, $2, $5, $7, $9, $10)); }
244 | label FOR scalar '(' remember mexpr ')' mblock cont
246 newFOROP(0, $1, $2, mod($3, OP_ENTERLOOP),
248 | label FOR '(' remember mexpr ')' mblock cont
250 newFOROP(0, $1, $2, Nullop, $5, $7, $8)); }
251 | label FOR '(' remember mnexpr ';' mtexpr ';' mnexpr ')' mblock
252 /* basically fake up an initialize-while lineseq */
255 forop = newSTATEOP(0, $1,
256 newWHILEOP(0, 1, (LOOP*)Nullop,
260 forop = append_elem(OP_LINESEQ,
261 newSTATEOP(0, ($1?savepv($1):Nullch),
266 $$ = block_end($4, forop); }
267 | label block cont /* a block is a loop that happens once */
268 { $$ = newSTATEOP(0, $1,
269 newWHILEOP(0, 1, (LOOP*)Nullop,
270 NOLINE, Nullop, $2, $3)); }
278 texpr : /* NULL means true */
279 { (void)scan_num("1", &yylval); $$ = yylval.opval; }
284 { $$ = invert(scalar($1)); }
288 { $$ = $1; intro_my(); }
292 { $$ = $1; intro_my(); }
296 { $$ = $1; intro_my(); }
300 { $$ = $1; intro_my(); }
320 format : FORMAT startformsub formname block
321 { newFORM($2, $3, $4); }
324 formname: WORD { $$ = $1; }
325 | /* NULL */ { $$ = Nullop; }
328 mysubrout: MYSUB startsub subname proto subattrlist subbody
329 { newMYSUB($2, $3, $4, $5, $6); }
332 subrout : SUB startsub subname proto subattrlist subbody
333 { newATTRSUB($2, $3, $4, $5, $6); }
336 startsub: /* NULL */ /* start a regular subroutine scope */
337 { $$ = start_subparse(FALSE, 0); }
340 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
341 { $$ = start_subparse(FALSE, CVf_ANON); }
344 startformsub: /* NULL */ /* start a format subroutine scope */
345 { $$ = start_subparse(TRUE, 0); }
348 subname : WORD { STRLEN n_a; char *name = SvPV(((SVOP*)$1)->op_sv,n_a);
349 if (strEQ(name, "BEGIN") || strEQ(name, "END")
350 || strEQ(name, "INIT") || strEQ(name, "CHECK"))
351 CvSPECIAL_on(PL_compcv);
360 subattrlist: /* NULL */
368 myattrlist: COLONATTR THING
374 subbody : block { $$ = $1; }
375 | ';' { $$ = Nullop; PL_expect = XSTATE; }
378 package : PACKAGE WORD ';'
385 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
386 WORD WORD listexpr ';'
387 { utilize($1, $2, $4, $5, $6); }
390 expr : expr ANDOP expr
391 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
393 { $$ = newLOGOP($2, 0, $1, $3); }
394 | argexpr %prec PREC_LOW
397 argexpr : argexpr ','
400 { $$ = append_elem(OP_LIST, $1, $3); }
401 | term %prec PREC_LOW
404 listop : LSTOP indirob argexpr
405 { $$ = convert($1, OPf_STACKED,
406 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
407 | FUNC '(' indirob expr ')'
408 { $$ = convert($1, OPf_STACKED,
409 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
410 | term ARROW method '(' listexprcom ')'
411 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
413 prepend_elem(OP_LIST, scalar($1), $5),
414 newUNOP(OP_METHOD, 0, $3))); }
416 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
417 append_elem(OP_LIST, scalar($1),
418 newUNOP(OP_METHOD, 0, $3))); }
419 | METHOD indirob listexpr
420 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
422 prepend_elem(OP_LIST, $2, $3),
423 newUNOP(OP_METHOD, 0, $1))); }
424 | FUNCMETH indirob '(' listexprcom ')'
425 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
427 prepend_elem(OP_LIST, $2, $4),
428 newUNOP(OP_METHOD, 0, $1))); }
430 { $$ = convert($1, 0, $2); }
431 | FUNC '(' listexprcom ')'
432 { $$ = convert($1, 0, $3); }
433 | LSTOPSUB startanonsub block
434 { $3 = newANONATTRSUB($2, 0, Nullop, $3); }
436 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
438 prepend_elem(OP_LIST, $3, $5), $1)); }
445 subscripted: star '{' expr ';' '}'
446 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
447 | scalar '[' expr ']'
448 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
449 | term ARROW '[' expr ']'
450 { $$ = newBINOP(OP_AELEM, 0,
451 ref(newAVREF($1),OP_RV2AV),
453 | subscripted '[' expr ']'
454 { $$ = newBINOP(OP_AELEM, 0,
455 ref(newAVREF($1),OP_RV2AV),
457 | scalar '{' expr ';' '}'
458 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
459 PL_expect = XOPERATOR; }
460 | term ARROW '{' expr ';' '}'
461 { $$ = newBINOP(OP_HELEM, 0,
462 ref(newHVREF($1),OP_RV2HV),
464 PL_expect = XOPERATOR; }
465 | subscripted '{' expr ';' '}'
466 { $$ = newBINOP(OP_HELEM, 0,
467 ref(newHVREF($1),OP_RV2HV),
469 PL_expect = XOPERATOR; }
471 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
472 newCVREF(0, scalar($1))); }
473 | term ARROW '(' expr ')'
474 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
475 append_elem(OP_LIST, $4,
476 newCVREF(0, scalar($1)))); }
478 | subscripted '(' expr ')'
479 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
480 append_elem(OP_LIST, $3,
481 newCVREF(0, scalar($1)))); }
482 | subscripted '(' ')'
483 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
484 newCVREF(0, scalar($1))); }
488 term : term ASSIGNOP term
489 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
491 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
493 { if ($2 != OP_REPEAT)
495 $$ = newBINOP($2, 0, $1, scalar($3)); }
497 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
499 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
501 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
503 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
505 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
507 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
509 { $$ = newRANGE($2, scalar($1), scalar($3));}
511 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
513 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
514 | term '?' term ':' term
515 { $$ = newCONDOP(0, $1, $3, $5); }
517 { $$ = bind_match($2, $1, $3); }
519 | '-' term %prec UMINUS
520 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
521 | '+' term %prec UMINUS
524 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
526 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
528 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
530 { $$ = newUNOP(OP_POSTINC, 0,
531 mod(scalar($1), OP_POSTINC)); }
533 { $$ = newUNOP(OP_POSTDEC, 0,
534 mod(scalar($1), OP_POSTDEC)); }
536 { $$ = newUNOP(OP_PREINC, 0,
537 mod(scalar($2), OP_PREINC)); }
539 { $$ = newUNOP(OP_PREDEC, 0,
540 mod(scalar($2), OP_PREDEC)); }
541 | myattrterm %prec UNIOP
543 | LOCAL term %prec UNIOP
544 { $$ = localize($2,$1); }
546 { $$ = sawparens($2); }
548 { $$ = sawparens(newNULLLIST()); }
550 { $$ = newANONLIST($2); }
552 { $$ = newANONLIST(Nullop); }
553 | HASHBRACK expr ';' '}' %prec '('
554 { $$ = newANONHASH($2); }
555 | HASHBRACK ';' '}' %prec '('
556 { $$ = newANONHASH(Nullop); }
557 | ANONSUB startanonsub proto subattrlist block %prec '('
558 { $$ = newANONATTRSUB($2, $3, $4, $5); }
568 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
571 | '(' expr ')' '[' expr ']'
572 { $$ = newSLICEOP(0, $5, $2); }
573 | '(' ')' '[' expr ']'
574 { $$ = newSLICEOP(0, $4, Nullop); }
576 { $$ = prepend_elem(OP_ASLICE,
577 newOP(OP_PUSHMARK, 0),
578 newLISTOP(OP_ASLICE, 0,
580 ref($1, OP_ASLICE))); }
581 | ary '{' expr ';' '}'
582 { $$ = prepend_elem(OP_HSLICE,
583 newOP(OP_PUSHMARK, 0),
584 newLISTOP(OP_HSLICE, 0,
586 ref(oopsHV($1), OP_HSLICE)));
587 PL_expect = XOPERATOR; }
591 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
593 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
595 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
596 append_elem(OP_LIST, $3, scalar($1))); }
597 | NOAMP WORD listexpr
598 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
599 append_elem(OP_LIST, $3, scalar($2))); }
600 | DO term %prec UNIOP
603 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
605 { $$ = newUNOP(OP_ENTERSUB,
606 OPf_SPECIAL|OPf_STACKED,
607 prepend_elem(OP_LIST,
609 (OPpENTERSUB_AMPER<<8),
612 | DO WORD '(' expr ')'
613 { $$ = newUNOP(OP_ENTERSUB,
614 OPf_SPECIAL|OPf_STACKED,
618 (OPpENTERSUB_AMPER<<8),
622 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
623 prepend_elem(OP_LIST,
624 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
625 | DO scalar '(' expr ')'
626 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
627 prepend_elem(OP_LIST,
629 scalar(newCVREF(0,scalar($2))))); dep();}
631 { $$ = newOP($1, OPf_SPECIAL);
632 PL_hints |= HINT_BLOCK_SCOPE; }
634 { $$ = newLOOPEX($1,$2); }
636 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
638 { $$ = newOP($1, 0); }
640 { $$ = newUNOP($1, 0, $2); }
642 { $$ = newUNOP($1, 0, $2); }
644 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
645 append_elem(OP_LIST, $2, scalar($1))); }
647 { $$ = newOP($1, 0); }
649 { $$ = newOP($1, 0); }
651 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
654 { $$ = newOP($1, OPf_SPECIAL); }
656 { $$ = newUNOP($1, 0, $3); }
657 | PMFUNC '(' term ')'
658 { $$ = pmruntime($1, $3, Nullop); }
659 | PMFUNC '(' term ',' term ')'
660 { $$ = pmruntime($1, $3, $5); }
665 myattrterm: MY myterm myattrlist
666 { $$ = my_attrs($2,$3); }
668 { $$ = localize($2,$1); }
671 myterm : '(' expr ')'
672 { $$ = sawparens($2); }
674 { $$ = sawparens(newNULLLIST()); }
683 listexpr: /* NULL */ %prec PREC_LOW
685 | argexpr %prec PREC_LOW
689 listexprcom: /* NULL */
698 { PL_in_my = 0; $$ = my($1); }
702 { $$ = newCVREF($1,$2); }
706 { $$ = newSVREF($2); }
710 { $$ = newAVREF($2); }
714 { $$ = newHVREF($2); }
717 arylen : DOLSHARP indirob
718 { $$ = newAVREF($2); }
722 { $$ = newGVREF(0,$2); }
727 | scalar %prec PREC_LOW
738 /* more stuff added to make perly_c.diff easier to apply */
743 #define yyparse() Perl_yyparse(pTHX)