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
20 #define dep() deprecate("\"do\" to call subroutines")
22 /* stuff included here to make perly_c.diff apply better */
24 #define yydebug PL_yydebug
25 #define yynerrs PL_yynerrs
26 #define yyerrflag PL_yyerrflag
27 #define yychar PL_yychar
28 #define yyval PL_yyval
29 #define yylval PL_yylval
42 static void yydestruct(pTHXo_ void *ptr);
49 #if 0 /* get this from perly.h instead */
63 #define YYLEX_PARAM (&yychar)
71 %token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
72 %token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
74 %token <ival> FORMAT SUB ANONSUB PACKAGE USE
75 %token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
76 %token <ival> LOOPEX DOTDOT
77 %token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP
78 %token <ival> RELOP EQOP MULOP ADDOP
79 %token <ival> DOLSHARP DO HASHBRACK NOAMP
80 %token <ival> LOCAL MY MYSUB
83 %type <ival> prog decl format startsub startanonsub startformsub
84 %type <ival> remember mremember '&'
85 %type <opval> block mblock lineseq line loop cond else
86 %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
87 %type <opval> argexpr nexpr texpr iexpr mexpr mnexpr mtexpr miexpr
88 %type <opval> listexpr listexprcom indirob listop method
89 %type <opval> formname subname proto subbody cont my_scalar
90 %type <opval> subattrlist myattrlist mysubrout myattrterm myterm
99 %nonassoc LSTOP LSTOPSUB
101 %right <ival> ASSIGNOP
107 %left <ival> BITANDOP
110 %nonassoc UNIOP UNIOPSUB
115 %right '!' '~' UMINUS REFGEN
117 %nonassoc PREINC PREDEC POSTINC POSTDEC
127 #if defined(YYDEBUG) && defined(DEBUGGING)
128 yydebug = (DEBUG_p_TEST);
132 /*CONTINUED*/ lineseq
136 block : '{' remember lineseq '}'
137 { if (PL_copline > (line_t)$1)
139 $$ = block_end($2, $3); }
142 remember: /* NULL */ /* start a full lexical scope */
143 { $$ = block_start(TRUE); }
146 mblock : '{' mremember lineseq '}'
147 { if (PL_copline > (line_t)$1)
149 $$ = block_end($2, $3); }
152 mremember: /* NULL */ /* start a partial lexical scope */
153 { $$ = block_start(FALSE); }
161 { $$ = append_list(OP_LINESEQ,
162 (LISTOP*)$1, (LISTOP*)$2);
163 PL_pad_reset_pending = TRUE;
164 if ($1 && $2) PL_hints |= HINT_BLOCK_SCOPE; }
168 { $$ = newSTATEOP(0, $1, $2); }
169 | loop /* loops add their own labels */
171 { if ($1 != Nullch) {
172 $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0));
178 PL_expect = XSTATE; }
180 { $$ = newSTATEOP(0, $1, $2);
181 PL_expect = XSTATE; }
189 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
191 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
193 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
195 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);}
197 { $$ = newFOROP(0, Nullch, $2,
198 Nullop, $3, $1, Nullop); }
204 { ($2)->op_flags |= OPf_PARENS; $$ = scope($2); }
205 | ELSIF '(' mexpr ')' mblock else
207 $$ = newCONDOP(0, $3, scope($5), $6);
208 PL_hints |= HINT_BLOCK_SCOPE; }
211 cond : IF '(' remember mexpr ')' mblock else
214 newCONDOP(0, $4, scope($6), $7)); }
215 | UNLESS '(' remember miexpr ')' mblock else
218 newCONDOP(0, $4, scope($6), $7)); }
227 loop : label WHILE '(' remember mtexpr ')' mblock cont
231 newWHILEOP(0, 1, (LOOP*)Nullop,
233 | label UNTIL '(' remember miexpr ')' mblock cont
237 newWHILEOP(0, 1, (LOOP*)Nullop,
239 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
241 newFOROP(0, $1, $2, $5, $7, $9, $10)); }
242 | label FOR scalar '(' remember mexpr ')' mblock cont
244 newFOROP(0, $1, $2, mod($3, OP_ENTERLOOP),
246 | label FOR '(' remember mexpr ')' mblock cont
248 newFOROP(0, $1, $2, Nullop, $5, $7, $8)); }
249 | label FOR '(' remember mnexpr ';' mtexpr ';' mnexpr ')' mblock
250 /* basically fake up an initialize-while lineseq */
253 forop = newSTATEOP(0, $1,
254 newWHILEOP(0, 1, (LOOP*)Nullop,
258 forop = append_elem(OP_LINESEQ,
259 newSTATEOP(0, ($1?savepv($1):Nullch),
264 $$ = block_end($4, forop); }
265 | label block cont /* a block is a loop that happens once */
266 { $$ = newSTATEOP(0, $1,
267 newWHILEOP(0, 1, (LOOP*)Nullop,
268 NOLINE, Nullop, $2, $3)); }
276 texpr : /* NULL means true */
277 { (void)scan_num("1", &yylval); $$ = yylval.opval; }
282 { $$ = invert(scalar($1)); }
286 { $$ = $1; intro_my(); }
290 { $$ = $1; intro_my(); }
294 { $$ = $1; intro_my(); }
298 { $$ = $1; intro_my(); }
318 format : FORMAT startformsub formname block
319 { newFORM($2, $3, $4); }
322 formname: WORD { $$ = $1; }
323 | /* NULL */ { $$ = Nullop; }
326 mysubrout: MYSUB startsub subname proto subattrlist subbody
327 { newMYSUB($2, $3, $4, $5, $6); }
330 subrout : SUB startsub subname proto subattrlist subbody
331 { newATTRSUB($2, $3, $4, $5, $6); }
334 startsub: /* NULL */ /* start a regular subroutine scope */
335 { $$ = start_subparse(FALSE, 0); }
338 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
339 { $$ = start_subparse(FALSE, CVf_ANON); }
342 startformsub: /* NULL */ /* start a format subroutine scope */
343 { $$ = start_subparse(TRUE, 0); }
346 subname : WORD { STRLEN n_a; char *name = SvPV(((SVOP*)$1)->op_sv,n_a);
347 if (strEQ(name, "BEGIN") || strEQ(name, "END")
348 || strEQ(name, "INIT") || strEQ(name, "CHECK"))
349 CvSPECIAL_on(PL_compcv);
358 subattrlist: /* NULL */
366 myattrlist: COLONATTR THING
372 subbody : block { $$ = $1; }
373 | ';' { $$ = Nullop; PL_expect = XSTATE; }
376 package : PACKAGE WORD ';'
383 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
384 WORD WORD listexpr ';'
385 { utilize($1, $2, $4, $5, $6); }
388 expr : expr ANDOP expr
389 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
391 { $$ = newLOGOP($2, 0, $1, $3); }
392 | argexpr %prec PREC_LOW
395 argexpr : argexpr ','
398 { $$ = append_elem(OP_LIST, $1, $3); }
399 | term %prec PREC_LOW
402 listop : LSTOP indirob argexpr
403 { $$ = convert($1, OPf_STACKED,
404 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
405 | FUNC '(' indirob expr ')'
406 { $$ = convert($1, OPf_STACKED,
407 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
408 | term ARROW method '(' listexprcom ')'
409 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
411 prepend_elem(OP_LIST, scalar($1), $5),
412 newUNOP(OP_METHOD, 0, $3))); }
414 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
415 append_elem(OP_LIST, scalar($1),
416 newUNOP(OP_METHOD, 0, $3))); }
417 | METHOD indirob listexpr
418 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
420 prepend_elem(OP_LIST, $2, $3),
421 newUNOP(OP_METHOD, 0, $1))); }
422 | FUNCMETH indirob '(' listexprcom ')'
423 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
425 prepend_elem(OP_LIST, $2, $4),
426 newUNOP(OP_METHOD, 0, $1))); }
428 { $$ = convert($1, 0, $2); }
429 | FUNC '(' listexprcom ')'
430 { $$ = convert($1, 0, $3); }
431 | LSTOPSUB startanonsub block
432 { $3 = newANONATTRSUB($2, 0, Nullop, $3); }
434 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
436 prepend_elem(OP_LIST, $3, $5), $1)); }
443 subscripted: star '{' expr ';' '}'
444 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
445 | scalar '[' expr ']'
446 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
447 | term ARROW '[' expr ']'
448 { $$ = newBINOP(OP_AELEM, 0,
449 ref(newAVREF($1),OP_RV2AV),
451 | subscripted '[' expr ']'
452 { $$ = newBINOP(OP_AELEM, 0,
453 ref(newAVREF($1),OP_RV2AV),
455 | scalar '{' expr ';' '}'
456 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
457 PL_expect = XOPERATOR; }
458 | term ARROW '{' expr ';' '}'
459 { $$ = newBINOP(OP_HELEM, 0,
460 ref(newHVREF($1),OP_RV2HV),
462 PL_expect = XOPERATOR; }
463 | subscripted '{' expr ';' '}'
464 { $$ = newBINOP(OP_HELEM, 0,
465 ref(newHVREF($1),OP_RV2HV),
467 PL_expect = XOPERATOR; }
469 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
470 newCVREF(0, scalar($1))); }
471 | term ARROW '(' expr ')'
472 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
473 append_elem(OP_LIST, $4,
474 newCVREF(0, scalar($1)))); }
476 | subscripted '(' expr ')'
477 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
478 append_elem(OP_LIST, $3,
479 newCVREF(0, scalar($1)))); }
480 | subscripted '(' ')'
481 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
482 newCVREF(0, scalar($1))); }
486 term : term ASSIGNOP term
487 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
489 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
491 { if ($2 != OP_REPEAT)
493 $$ = newBINOP($2, 0, $1, scalar($3)); }
495 { $$ = newBINOP($2, 0, scalar($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 { $$ = newRANGE($2, scalar($1), scalar($3));}
509 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
511 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
512 | term '?' term ':' term
513 { $$ = newCONDOP(0, $1, $3, $5); }
515 { $$ = bind_match($2, $1, $3); }
517 | '-' term %prec UMINUS
518 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
519 | '+' term %prec UMINUS
522 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
524 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
526 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
528 { $$ = newUNOP(OP_POSTINC, 0,
529 mod(scalar($1), OP_POSTINC)); }
531 { $$ = newUNOP(OP_POSTDEC, 0,
532 mod(scalar($1), OP_POSTDEC)); }
534 { $$ = newUNOP(OP_PREINC, 0,
535 mod(scalar($2), OP_PREINC)); }
537 { $$ = newUNOP(OP_PREDEC, 0,
538 mod(scalar($2), OP_PREDEC)); }
539 | myattrterm %prec UNIOP
541 | LOCAL term %prec UNIOP
542 { $$ = localize($2,$1); }
544 { $$ = sawparens($2); }
546 { $$ = sawparens(newNULLLIST()); }
548 { $$ = newANONLIST($2); }
550 { $$ = newANONLIST(Nullop); }
551 | HASHBRACK expr ';' '}' %prec '('
552 { $$ = newANONHASH($2); }
553 | HASHBRACK ';' '}' %prec '('
554 { $$ = newANONHASH(Nullop); }
555 | ANONSUB startanonsub proto subattrlist block %prec '('
556 { $$ = newANONATTRSUB($2, $3, $4, $5); }
566 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
569 | '(' expr ')' '[' expr ']'
570 { $$ = newSLICEOP(0, $5, $2); }
571 | '(' ')' '[' expr ']'
572 { $$ = newSLICEOP(0, $4, Nullop); }
574 { $$ = prepend_elem(OP_ASLICE,
575 newOP(OP_PUSHMARK, 0),
576 newLISTOP(OP_ASLICE, 0,
578 ref($1, OP_ASLICE))); }
579 | ary '{' expr ';' '}'
580 { $$ = prepend_elem(OP_HSLICE,
581 newOP(OP_PUSHMARK, 0),
582 newLISTOP(OP_HSLICE, 0,
584 ref(oopsHV($1), OP_HSLICE)));
585 PL_expect = XOPERATOR; }
589 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
591 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
593 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
594 append_elem(OP_LIST, $3, scalar($1))); }
595 | NOAMP WORD listexpr
596 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
597 append_elem(OP_LIST, $3, scalar($2))); }
598 | DO term %prec UNIOP
601 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
603 { $$ = newUNOP(OP_ENTERSUB,
604 OPf_SPECIAL|OPf_STACKED,
605 prepend_elem(OP_LIST,
607 (OPpENTERSUB_AMPER<<8),
610 | DO WORD '(' expr ')'
611 { $$ = newUNOP(OP_ENTERSUB,
612 OPf_SPECIAL|OPf_STACKED,
616 (OPpENTERSUB_AMPER<<8),
620 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
621 prepend_elem(OP_LIST,
622 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
623 | DO scalar '(' expr ')'
624 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
625 prepend_elem(OP_LIST,
627 scalar(newCVREF(0,scalar($2))))); dep();}
629 { $$ = newOP($1, OPf_SPECIAL);
630 PL_hints |= HINT_BLOCK_SCOPE; }
632 { $$ = newLOOPEX($1,$2); }
634 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
636 { $$ = newOP($1, 0); }
638 { $$ = newUNOP($1, 0, $2); }
640 { $$ = newUNOP($1, 0, $2); }
642 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
643 append_elem(OP_LIST, $2, scalar($1))); }
645 { $$ = newOP($1, 0); }
647 { $$ = newOP($1, 0); }
649 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
652 { $$ = newOP($1, OPf_SPECIAL); }
654 { $$ = newUNOP($1, 0, $3); }
655 | PMFUNC '(' term ')'
656 { $$ = pmruntime($1, $3, Nullop); }
657 | PMFUNC '(' term ',' term ')'
658 { $$ = pmruntime($1, $3, $5); }
663 myattrterm: MY myterm myattrlist
664 { $$ = my_attrs($2,$3); }
666 { $$ = localize($2,$1); }
669 myterm : '(' expr ')'
670 { $$ = sawparens($2); }
672 { $$ = sawparens(newNULLLIST()); }
681 listexpr: /* NULL */ %prec PREC_LOW
683 | argexpr %prec PREC_LOW
687 listexprcom: /* NULL */
696 { PL_in_my = 0; $$ = my($1); }
700 { $$ = newCVREF($1,$2); }
704 { $$ = newSVREF($2); }
708 { $$ = newAVREF($2); }
712 { $$ = newHVREF($2); }
715 arylen : DOLSHARP indirob
716 { $$ = newAVREF($2); }
720 { $$ = newGVREF(0,$2); }
725 | scalar %prec PREC_LOW
736 /* more stuff added to make perly_c.diff easier to apply */
741 #define yyparse() Perl_yyparse(pTHX)