3 * Copyright (c) 1991-1997, 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)
70 %token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
71 %token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
73 %token <ival> FORMAT SUB ANONSUB PACKAGE USE
74 %token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
75 %token <ival> LOOPEX DOTDOT
76 %token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP
77 %token <ival> RELOP EQOP MULOP ADDOP
78 %token <ival> DOLSHARP DO HASHBRACK NOAMP
79 %token <ival> LOCAL MY MYSUB
82 %type <ival> prog decl format startsub startanonsub startformsub
83 %type <ival> remember mremember '&'
84 %type <opval> block mblock lineseq line loop cond else
85 %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
86 %type <opval> argexpr nexpr texpr iexpr mexpr mnexpr mtexpr miexpr
87 %type <opval> listexpr listexprcom indirob listop method
88 %type <opval> formname subname proto subbody cont my_scalar
89 %type <opval> subattrlist myattrlist mysubrout myattrterm myterm
98 %nonassoc LSTOP LSTOPSUB
100 %right <ival> ASSIGNOP
106 %left <ival> BITANDOP
109 %nonassoc UNIOP UNIOPSUB
114 %right '!' '~' UMINUS REFGEN
116 %nonassoc PREINC PREDEC POSTINC POSTDEC
126 #if defined(YYDEBUG) && defined(DEBUGGING)
127 yydebug = (PL_debug & 1);
131 /*CONTINUED*/ lineseq
135 block : '{' remember lineseq '}'
136 { if (PL_copline > (line_t)$1)
138 $$ = block_end($2, $3); }
141 remember: /* NULL */ /* start a full lexical scope */
142 { $$ = block_start(TRUE); }
145 mblock : '{' mremember lineseq '}'
146 { if (PL_copline > (line_t)$1)
148 $$ = block_end($2, $3); }
151 mremember: /* NULL */ /* start a partial lexical scope */
152 { $$ = block_start(FALSE); }
160 { $$ = append_list(OP_LINESEQ,
161 (LISTOP*)$1, (LISTOP*)$2);
162 PL_pad_reset_pending = TRUE;
163 if ($1 && $2) PL_hints |= HINT_BLOCK_SCOPE; }
167 { $$ = newSTATEOP(0, $1, $2); }
168 | loop /* loops add their own labels */
170 { if ($1 != Nullch) {
171 $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0));
177 PL_expect = XSTATE; }
179 { $$ = newSTATEOP(0, $1, $2);
180 PL_expect = XSTATE; }
188 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
190 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
192 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
194 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);}
196 { $$ = newFOROP(0, Nullch, $2,
197 Nullop, $3, $1, Nullop); }
203 { ($2)->op_flags |= OPf_PARENS; $$ = scope($2); }
204 | ELSIF '(' mexpr ')' mblock else
206 $$ = newCONDOP(0, $3, scope($5), $6);
207 PL_hints |= HINT_BLOCK_SCOPE; }
210 cond : IF '(' remember mexpr ')' mblock else
213 newCONDOP(0, $4, scope($6), $7)); }
214 | UNLESS '(' remember miexpr ')' mblock else
217 newCONDOP(0, $4, scope($6), $7)); }
226 loop : label WHILE '(' remember mtexpr ')' mblock cont
230 newWHILEOP(0, 1, (LOOP*)Nullop,
232 | label UNTIL '(' remember miexpr ')' mblock cont
236 newWHILEOP(0, 1, (LOOP*)Nullop,
238 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
240 newFOROP(0, $1, $2, $5, $7, $9, $10)); }
241 | label FOR scalar '(' remember mexpr ')' mblock cont
243 newFOROP(0, $1, $2, mod($3, OP_ENTERLOOP),
245 | label FOR '(' remember mexpr ')' mblock cont
247 newFOROP(0, $1, $2, Nullop, $5, $7, $8)); }
248 | label FOR '(' remember mnexpr ';' mtexpr ';' mnexpr ')' mblock
249 /* basically fake up an initialize-while lineseq */
250 { OP *forop = append_elem(OP_LINESEQ,
252 newWHILEOP(0, 1, (LOOP*)Nullop,
256 $$ = block_end($4, newSTATEOP(0, $1, forop)); }
257 | label block cont /* a block is a loop that happens once */
258 { $$ = newSTATEOP(0, $1,
259 newWHILEOP(0, 1, (LOOP*)Nullop,
260 NOLINE, Nullop, $2, $3)); }
268 texpr : /* NULL means true */
269 { (void)scan_num("1"); $$ = yylval.opval; }
274 { $$ = invert(scalar($1)); }
278 { $$ = $1; intro_my(); }
282 { $$ = $1; intro_my(); }
286 { $$ = $1; intro_my(); }
290 { $$ = $1; intro_my(); }
310 format : FORMAT startformsub formname block
311 { newFORM($2, $3, $4); }
314 formname: WORD { $$ = $1; }
315 | /* NULL */ { $$ = Nullop; }
318 mysubrout: MYSUB startsub subname proto subattrlist subbody
319 { newMYSUB($2, $3, $4, $5, $6); }
322 subrout : SUB startsub subname proto subattrlist subbody
323 { newATTRSUB($2, $3, $4, $5, $6); }
326 startsub: /* NULL */ /* start a regular subroutine scope */
327 { $$ = start_subparse(FALSE, 0); }
330 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
331 { $$ = start_subparse(FALSE, CVf_ANON); }
334 startformsub: /* NULL */ /* start a format subroutine scope */
335 { $$ = start_subparse(TRUE, 0); }
338 subname : WORD { STRLEN n_a; char *name = SvPV(((SVOP*)$1)->op_sv,n_a);
339 if (strEQ(name, "BEGIN") || strEQ(name, "END")
340 || strEQ(name, "STOP") || strEQ(name, "INIT"))
341 CvSPECIAL_on(PL_compcv);
350 subattrlist: /* NULL */
358 myattrlist: COLONATTR THING
364 subbody : block { $$ = $1; }
365 | ';' { $$ = Nullop; PL_expect = XSTATE; }
368 package : PACKAGE WORD ';'
375 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
376 WORD WORD listexpr ';'
377 { utilize($1, $2, $4, $5, $6); }
380 expr : expr ANDOP expr
381 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
383 { $$ = newLOGOP($2, 0, $1, $3); }
384 | argexpr %prec PREC_LOW
387 argexpr : argexpr ','
390 { $$ = append_elem(OP_LIST, $1, $3); }
391 | term %prec PREC_LOW
394 listop : LSTOP indirob argexpr
395 { $$ = convert($1, OPf_STACKED,
396 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
397 | FUNC '(' indirob expr ')'
398 { $$ = convert($1, OPf_STACKED,
399 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
400 | term ARROW method '(' listexprcom ')'
401 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
403 prepend_elem(OP_LIST, scalar($1), $5),
404 newUNOP(OP_METHOD, 0, $3))); }
406 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
407 append_elem(OP_LIST, scalar($1),
408 newUNOP(OP_METHOD, 0, $3))); }
409 | METHOD indirob listexpr
410 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
412 prepend_elem(OP_LIST, $2, $3),
413 newUNOP(OP_METHOD, 0, $1))); }
414 | FUNCMETH indirob '(' listexprcom ')'
415 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
417 prepend_elem(OP_LIST, $2, $4),
418 newUNOP(OP_METHOD, 0, $1))); }
420 { $$ = convert($1, 0, $2); }
421 | FUNC '(' listexprcom ')'
422 { $$ = convert($1, 0, $3); }
423 | LSTOPSUB startanonsub block
424 { $3 = newANONATTRSUB($2, 0, Nullop, $3); }
426 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
428 prepend_elem(OP_LIST, $3, $5), $1)); }
435 subscripted: star '{' expr ';' '}'
436 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
437 | scalar '[' expr ']'
438 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
439 | term ARROW '[' expr ']'
440 { $$ = newBINOP(OP_AELEM, 0,
441 ref(newAVREF($1),OP_RV2AV),
443 | subscripted '[' expr ']'
444 { $$ = newBINOP(OP_AELEM, 0,
445 ref(newAVREF($1),OP_RV2AV),
447 | scalar '{' expr ';' '}'
448 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
449 PL_expect = XOPERATOR; }
450 | term ARROW '{' expr ';' '}'
451 { $$ = newBINOP(OP_HELEM, 0,
452 ref(newHVREF($1),OP_RV2HV),
454 PL_expect = XOPERATOR; }
455 | subscripted '{' expr ';' '}'
456 { $$ = newBINOP(OP_HELEM, 0,
457 ref(newHVREF($1),OP_RV2HV),
459 PL_expect = XOPERATOR; }
461 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
462 newCVREF(0, scalar($1))); }
463 | term ARROW '(' expr ')'
464 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
465 append_elem(OP_LIST, $4,
466 newCVREF(0, scalar($1)))); }
468 | subscripted '(' expr ')'
469 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
470 append_elem(OP_LIST, $3,
471 newCVREF(0, scalar($1)))); }
472 | subscripted '(' ')'
473 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
474 newCVREF(0, scalar($1))); }
478 term : term ASSIGNOP term
479 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
481 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
483 { if ($2 != OP_REPEAT)
485 $$ = newBINOP($2, 0, $1, scalar($3)); }
487 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
489 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
491 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
493 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
495 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
497 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
499 { $$ = newRANGE($2, scalar($1), scalar($3));}
501 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
503 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
504 | term '?' term ':' term
505 { $$ = newCONDOP(0, $1, $3, $5); }
507 { $$ = bind_match($2, $1, $3); }
509 | '-' term %prec UMINUS
510 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
511 | '+' term %prec UMINUS
514 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
516 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
518 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
520 { $$ = newUNOP(OP_POSTINC, 0,
521 mod(scalar($1), OP_POSTINC)); }
523 { $$ = newUNOP(OP_POSTDEC, 0,
524 mod(scalar($1), OP_POSTDEC)); }
526 { $$ = newUNOP(OP_PREINC, 0,
527 mod(scalar($2), OP_PREINC)); }
529 { $$ = newUNOP(OP_PREDEC, 0,
530 mod(scalar($2), OP_PREDEC)); }
531 | myattrterm %prec UNIOP
533 | LOCAL term %prec UNIOP
534 { $$ = localize($2,$1); }
536 { $$ = sawparens($2); }
538 { $$ = sawparens(newNULLLIST()); }
540 { $$ = newANONLIST($2); }
542 { $$ = newANONLIST(Nullop); }
543 | HASHBRACK expr ';' '}' %prec '('
544 { $$ = newANONHASH($2); }
545 | HASHBRACK ';' '}' %prec '('
546 { $$ = newANONHASH(Nullop); }
547 | ANONSUB startanonsub proto subattrlist block %prec '('
548 { $$ = newANONATTRSUB($2, $3, $4, $5); }
558 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
561 | '(' expr ')' '[' expr ']'
562 { $$ = newSLICEOP(0, $5, $2); }
563 | '(' ')' '[' expr ']'
564 { $$ = newSLICEOP(0, $4, Nullop); }
566 { $$ = prepend_elem(OP_ASLICE,
567 newOP(OP_PUSHMARK, 0),
568 newLISTOP(OP_ASLICE, 0,
570 ref($1, OP_ASLICE))); }
571 | ary '{' expr ';' '}'
572 { $$ = prepend_elem(OP_HSLICE,
573 newOP(OP_PUSHMARK, 0),
574 newLISTOP(OP_HSLICE, 0,
576 ref(oopsHV($1), OP_HSLICE)));
577 PL_expect = XOPERATOR; }
581 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
583 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
585 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
586 append_elem(OP_LIST, $3, scalar($1))); }
587 | NOAMP WORD listexpr
588 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
589 append_elem(OP_LIST, $3, scalar($2))); }
590 | DO term %prec UNIOP
593 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
595 { $$ = newUNOP(OP_ENTERSUB,
596 OPf_SPECIAL|OPf_STACKED,
597 prepend_elem(OP_LIST,
599 (OPpENTERSUB_AMPER<<8),
602 | DO WORD '(' expr ')'
603 { $$ = newUNOP(OP_ENTERSUB,
604 OPf_SPECIAL|OPf_STACKED,
608 (OPpENTERSUB_AMPER<<8),
612 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
613 prepend_elem(OP_LIST,
614 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
615 | DO scalar '(' expr ')'
616 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
617 prepend_elem(OP_LIST,
619 scalar(newCVREF(0,scalar($2))))); dep();}
621 { $$ = newOP($1, OPf_SPECIAL);
622 PL_hints |= HINT_BLOCK_SCOPE; }
624 { $$ = newLOOPEX($1,$2); }
626 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
628 { $$ = newOP($1, 0); }
630 { $$ = newUNOP($1, 0, $2); }
632 { $$ = newUNOP($1, 0, $2); }
634 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
635 append_elem(OP_LIST, $2, scalar($1))); }
637 { $$ = newOP($1, 0); }
639 { $$ = newOP($1, 0); }
641 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
644 { $$ = newOP($1, OPf_SPECIAL); }
646 { $$ = newUNOP($1, 0, $3); }
647 | PMFUNC '(' term ')'
648 { $$ = pmruntime($1, $3, Nullop); }
649 | PMFUNC '(' term ',' term ')'
650 { $$ = pmruntime($1, $3, $5); }
655 myattrterm: MY myterm myattrlist
656 { $$ = my_attrs($2,$3); }
658 { $$ = localize($2,$1); }
661 myterm : '(' expr ')'
662 { $$ = sawparens($2); }
664 { $$ = sawparens(newNULLLIST()); }
673 listexpr: /* NULL */ %prec PREC_LOW
675 | argexpr %prec PREC_LOW
679 listexprcom: /* NULL */
688 { PL_in_my = 0; $$ = my($1); }
692 { $$ = newCVREF($1,$2); }
696 { $$ = newSVREF($2); }
700 { $$ = newAVREF($2); }
704 { $$ = newHVREF($2); }
707 arylen : DOLSHARP indirob
708 { $$ = newAVREF($2); }
712 { $$ = newGVREF(0,$2); }
717 | scalar %prec PREC_LOW
728 /* more stuff added to make perly_c.diff easier to apply */
733 #define yyparse() Perl_yyparse(pTHX)