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 = (PL_debug & 1);
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 */
251 { OP *forop = append_elem(OP_LINESEQ,
253 newWHILEOP(0, 1, (LOOP*)Nullop,
257 $$ = block_end($4, newSTATEOP(0, $1, forop)); }
258 | label block cont /* a block is a loop that happens once */
259 { $$ = newSTATEOP(0, $1,
260 newWHILEOP(0, 1, (LOOP*)Nullop,
261 NOLINE, Nullop, $2, $3)); }
269 texpr : /* NULL means true */
270 { (void)scan_num("1", &yylval); $$ = yylval.opval; }
275 { $$ = invert(scalar($1)); }
279 { $$ = $1; intro_my(); }
283 { $$ = $1; intro_my(); }
287 { $$ = $1; intro_my(); }
291 { $$ = $1; intro_my(); }
311 format : FORMAT startformsub formname block
312 { newFORM($2, $3, $4); }
315 formname: WORD { $$ = $1; }
316 | /* NULL */ { $$ = Nullop; }
319 mysubrout: MYSUB startsub subname proto subattrlist subbody
320 { newMYSUB($2, $3, $4, $5, $6); }
323 subrout : SUB startsub subname proto subattrlist subbody
324 { newATTRSUB($2, $3, $4, $5, $6); }
327 startsub: /* NULL */ /* start a regular subroutine scope */
328 { $$ = start_subparse(FALSE, 0); }
331 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
332 { $$ = start_subparse(FALSE, CVf_ANON); }
335 startformsub: /* NULL */ /* start a format subroutine scope */
336 { $$ = start_subparse(TRUE, 0); }
339 subname : WORD { STRLEN n_a; char *name = SvPV(((SVOP*)$1)->op_sv,n_a);
340 if (strEQ(name, "BEGIN") || strEQ(name, "END")
341 || strEQ(name, "INIT") || strEQ(name, "CHECK"))
342 CvSPECIAL_on(PL_compcv);
351 subattrlist: /* NULL */
359 myattrlist: COLONATTR THING
365 subbody : block { $$ = $1; }
366 | ';' { $$ = Nullop; PL_expect = XSTATE; }
369 package : PACKAGE WORD ';'
376 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
377 WORD WORD listexpr ';'
378 { utilize($1, $2, $4, $5, $6); }
381 expr : expr ANDOP expr
382 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
384 { $$ = newLOGOP($2, 0, $1, $3); }
385 | argexpr %prec PREC_LOW
388 argexpr : argexpr ','
391 { $$ = append_elem(OP_LIST, $1, $3); }
392 | term %prec PREC_LOW
395 listop : LSTOP indirob argexpr
396 { $$ = convert($1, OPf_STACKED,
397 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
398 | FUNC '(' indirob expr ')'
399 { $$ = convert($1, OPf_STACKED,
400 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
401 | term ARROW method '(' listexprcom ')'
402 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
404 prepend_elem(OP_LIST, scalar($1), $5),
405 newUNOP(OP_METHOD, 0, $3))); }
407 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
408 append_elem(OP_LIST, scalar($1),
409 newUNOP(OP_METHOD, 0, $3))); }
410 | METHOD indirob listexpr
411 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
413 prepend_elem(OP_LIST, $2, $3),
414 newUNOP(OP_METHOD, 0, $1))); }
415 | FUNCMETH indirob '(' listexprcom ')'
416 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
418 prepend_elem(OP_LIST, $2, $4),
419 newUNOP(OP_METHOD, 0, $1))); }
421 { $$ = convert($1, 0, $2); }
422 | FUNC '(' listexprcom ')'
423 { $$ = convert($1, 0, $3); }
424 | LSTOPSUB startanonsub block
425 { $3 = newANONATTRSUB($2, 0, Nullop, $3); }
427 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
429 prepend_elem(OP_LIST, $3, $5), $1)); }
436 subscripted: star '{' expr ';' '}'
437 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
438 | scalar '[' expr ']'
439 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
440 | term ARROW '[' expr ']'
441 { $$ = newBINOP(OP_AELEM, 0,
442 ref(newAVREF($1),OP_RV2AV),
444 | subscripted '[' expr ']'
445 { $$ = newBINOP(OP_AELEM, 0,
446 ref(newAVREF($1),OP_RV2AV),
448 | scalar '{' expr ';' '}'
449 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
450 PL_expect = XOPERATOR; }
451 | term ARROW '{' expr ';' '}'
452 { $$ = newBINOP(OP_HELEM, 0,
453 ref(newHVREF($1),OP_RV2HV),
455 PL_expect = XOPERATOR; }
456 | subscripted '{' expr ';' '}'
457 { $$ = newBINOP(OP_HELEM, 0,
458 ref(newHVREF($1),OP_RV2HV),
460 PL_expect = XOPERATOR; }
462 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
463 newCVREF(0, scalar($1))); }
464 | term ARROW '(' expr ')'
465 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
466 append_elem(OP_LIST, $4,
467 newCVREF(0, scalar($1)))); }
469 | subscripted '(' expr ')'
470 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
471 append_elem(OP_LIST, $3,
472 newCVREF(0, scalar($1)))); }
473 | subscripted '(' ')'
474 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
475 newCVREF(0, scalar($1))); }
479 term : term ASSIGNOP term
480 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
482 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
484 { if ($2 != OP_REPEAT)
486 $$ = newBINOP($2, 0, $1, scalar($3)); }
488 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
490 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
492 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
494 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
496 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
498 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
500 { $$ = newRANGE($2, scalar($1), scalar($3));}
502 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
504 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
505 | term '?' term ':' term
506 { $$ = newCONDOP(0, $1, $3, $5); }
508 { $$ = bind_match($2, $1, $3); }
510 | '-' term %prec UMINUS
511 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
512 | '+' term %prec UMINUS
515 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
517 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
519 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
521 { $$ = newUNOP(OP_POSTINC, 0,
522 mod(scalar($1), OP_POSTINC)); }
524 { $$ = newUNOP(OP_POSTDEC, 0,
525 mod(scalar($1), OP_POSTDEC)); }
527 { $$ = newUNOP(OP_PREINC, 0,
528 mod(scalar($2), OP_PREINC)); }
530 { $$ = newUNOP(OP_PREDEC, 0,
531 mod(scalar($2), OP_PREDEC)); }
532 | myattrterm %prec UNIOP
534 | LOCAL term %prec UNIOP
535 { $$ = localize($2,$1); }
537 { $$ = sawparens($2); }
539 { $$ = sawparens(newNULLLIST()); }
541 { $$ = newANONLIST($2); }
543 { $$ = newANONLIST(Nullop); }
544 | HASHBRACK expr ';' '}' %prec '('
545 { $$ = newANONHASH($2); }
546 | HASHBRACK ';' '}' %prec '('
547 { $$ = newANONHASH(Nullop); }
548 | ANONSUB startanonsub proto subattrlist block %prec '('
549 { $$ = newANONATTRSUB($2, $3, $4, $5); }
559 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
562 | '(' expr ')' '[' expr ']'
563 { $$ = newSLICEOP(0, $5, $2); }
564 | '(' ')' '[' expr ']'
565 { $$ = newSLICEOP(0, $4, Nullop); }
567 { $$ = prepend_elem(OP_ASLICE,
568 newOP(OP_PUSHMARK, 0),
569 newLISTOP(OP_ASLICE, 0,
571 ref($1, OP_ASLICE))); }
572 | ary '{' expr ';' '}'
573 { $$ = prepend_elem(OP_HSLICE,
574 newOP(OP_PUSHMARK, 0),
575 newLISTOP(OP_HSLICE, 0,
577 ref(oopsHV($1), OP_HSLICE)));
578 PL_expect = XOPERATOR; }
582 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
584 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
586 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
587 append_elem(OP_LIST, $3, scalar($1))); }
588 | NOAMP WORD listexpr
589 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
590 append_elem(OP_LIST, $3, scalar($2))); }
591 | DO term %prec UNIOP
594 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
596 { $$ = newUNOP(OP_ENTERSUB,
597 OPf_SPECIAL|OPf_STACKED,
598 prepend_elem(OP_LIST,
600 (OPpENTERSUB_AMPER<<8),
603 | DO WORD '(' expr ')'
604 { $$ = newUNOP(OP_ENTERSUB,
605 OPf_SPECIAL|OPf_STACKED,
609 (OPpENTERSUB_AMPER<<8),
613 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
614 prepend_elem(OP_LIST,
615 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
616 | DO scalar '(' expr ')'
617 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
618 prepend_elem(OP_LIST,
620 scalar(newCVREF(0,scalar($2))))); dep();}
622 { $$ = newOP($1, OPf_SPECIAL);
623 PL_hints |= HINT_BLOCK_SCOPE; }
625 { $$ = newLOOPEX($1,$2); }
627 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
629 { $$ = newOP($1, 0); }
631 { $$ = newUNOP($1, 0, $2); }
633 { $$ = newUNOP($1, 0, $2); }
635 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
636 append_elem(OP_LIST, $2, scalar($1))); }
638 { $$ = newOP($1, 0); }
640 { $$ = newOP($1, 0); }
642 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
645 { $$ = newOP($1, OPf_SPECIAL); }
647 { $$ = newUNOP($1, 0, $3); }
648 | PMFUNC '(' term ')'
649 { $$ = pmruntime($1, $3, Nullop); }
650 | PMFUNC '(' term ',' term ')'
651 { $$ = pmruntime($1, $3, $5); }
656 myattrterm: MY myterm myattrlist
657 { $$ = my_attrs($2,$3); }
659 { $$ = localize($2,$1); }
662 myterm : '(' expr ')'
663 { $$ = sawparens($2); }
665 { $$ = sawparens(newNULLLIST()); }
674 listexpr: /* NULL */ %prec PREC_LOW
676 | argexpr %prec PREC_LOW
680 listexprcom: /* NULL */
689 { PL_in_my = 0; $$ = my($1); }
693 { $$ = newCVREF($1,$2); }
697 { $$ = newSVREF($2); }
701 { $$ = newAVREF($2); }
705 { $$ = newHVREF($2); }
708 arylen : DOLSHARP indirob
709 { $$ = newAVREF($2); }
713 { $$ = newGVREF(0,$2); }
718 | scalar %prec PREC_LOW
729 /* more stuff added to make perly_c.diff easier to apply */
734 #define yyparse() Perl_yyparse(pTHX)