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")
27 /* I sense a Big Blue pattern here... */
28 #if !defined(OEMVS) && !defined(__OPEN_VM) && !defined(POSIX_BC)
39 #endif /* !OEMVS && !__OPEN_VM && !POSIX_BC */
42 #define YYLEX_PARAM (&yychar)
48 %token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
49 %token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
51 %token <ival> FORMAT SUB ANONSUB PACKAGE USE
52 %token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
53 %token <ival> LOOPEX DOTDOT
54 %token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP
55 %token <ival> RELOP EQOP MULOP ADDOP
56 %token <ival> DOLSHARP DO HASHBRACK NOAMP
59 %type <ival> prog decl local format startsub startanonsub startformsub
60 %type <ival> remember mremember '&'
61 %type <opval> block mblock lineseq line loop cond else
62 %type <opval> expr term scalar ary hsh arylen star amper sideff
63 %type <opval> argexpr nexpr texpr iexpr mexpr mnexpr mtexpr miexpr
64 %type <opval> listexpr listexprcom indirob listop method
65 %type <opval> formname subname proto subbody cont my_scalar
71 %nonassoc LSTOP LSTOPSUB
73 %right <ival> ASSIGNOP
82 %nonassoc UNIOP UNIOPSUB
87 %right '!' '~' UMINUS REFGEN
89 %nonassoc PREINC PREDEC POSTINC POSTDEC
97 #if defined(YYDEBUG) && defined(DEBUGGING)
98 yydebug = (PL_debug & 1);
102 /*CONTINUED*/ lineseq
106 block : '{' remember lineseq '}'
107 { if (PL_copline > (line_t)$1)
109 $$ = block_end($2, $3); }
112 remember: /* NULL */ /* start a full lexical scope */
113 { $$ = block_start(TRUE); }
116 mblock : '{' mremember lineseq '}'
117 { if (PL_copline > (line_t)$1)
119 $$ = block_end($2, $3); }
122 mremember: /* NULL */ /* start a partial lexical scope */
123 { $$ = block_start(FALSE); }
131 { $$ = append_list(OP_LINESEQ,
132 (LISTOP*)$1, (LISTOP*)$2);
133 PL_pad_reset_pending = TRUE;
134 if ($1 && $2) PL_hints |= HINT_BLOCK_SCOPE; }
138 { $$ = newSTATEOP(0, $1, $2); }
139 | loop /* loops add their own labels */
141 { if ($1 != Nullch) {
142 $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0));
148 PL_expect = XSTATE; }
150 { $$ = newSTATEOP(0, $1, $2);
151 PL_expect = XSTATE; }
159 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
161 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
163 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
165 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);}
167 { $$ = newFOROP(0, Nullch, $2,
168 Nullop, $3, $1, Nullop); }
175 | ELSIF '(' mexpr ')' mblock else
177 $$ = newCONDOP(0, $3, scope($5), $6);
178 PL_hints |= HINT_BLOCK_SCOPE; }
181 cond : IF '(' remember mexpr ')' mblock else
184 newCONDOP(0, $4, scope($6), $7)); }
185 | UNLESS '(' remember miexpr ')' mblock else
188 newCONDOP(0, $4, scope($6), $7)); }
197 loop : label WHILE '(' remember mtexpr ')' mblock cont
201 newWHILEOP(0, 1, (LOOP*)Nullop,
203 | label UNTIL '(' remember miexpr ')' mblock cont
207 newWHILEOP(0, 1, (LOOP*)Nullop,
209 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
211 newFOROP(0, $1, $2, $5, $7, $9, $10)); }
212 | label FOR scalar '(' remember mexpr ')' mblock cont
214 newFOROP(0, $1, $2, mod($3, OP_ENTERLOOP),
216 | label FOR '(' remember mexpr ')' mblock cont
218 newFOROP(0, $1, $2, Nullop, $5, $7, $8)); }
219 | label FOR '(' remember mnexpr ';' mtexpr ';' mnexpr ')' mblock
220 /* basically fake up an initialize-while lineseq */
221 { OP *forop = append_elem(OP_LINESEQ,
223 newWHILEOP(0, 1, (LOOP*)Nullop,
227 $$ = block_end($4, newSTATEOP(0, $1, forop)); }
228 | label block cont /* a block is a loop that happens once */
229 { $$ = newSTATEOP(0, $1,
230 newWHILEOP(0, 1, (LOOP*)Nullop,
231 NOLINE, Nullop, $2, $3)); }
239 texpr : /* NULL means true */
240 { (void)scan_num("1"); $$ = yylval.opval; }
245 { $$ = invert(scalar($1)); }
249 { $$ = $1; intro_my(); }
253 { $$ = $1; intro_my(); }
257 { $$ = $1; intro_my(); }
261 { $$ = $1; intro_my(); }
279 format : FORMAT startformsub formname block
280 { newFORM($2, $3, $4); }
283 formname: WORD { $$ = $1; }
284 | /* NULL */ { $$ = Nullop; }
287 subrout : SUB startsub subname proto subbody
288 { newSUB($2, $3, $4, $5); }
291 startsub: /* NULL */ /* start a regular subroutine scope */
292 { $$ = start_subparse(FALSE, 0); }
295 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
296 { $$ = start_subparse(FALSE, CVf_ANON); }
299 startformsub: /* NULL */ /* start a format subroutine scope */
300 { $$ = start_subparse(TRUE, 0); }
303 subname : WORD { STRLEN n_a; char *name = SvPV(((SVOP*)$1)->op_sv,n_a);
304 if (strEQ(name, "BEGIN") || strEQ(name, "END")
305 || strEQ(name, "INIT"))
306 CvSPECIAL_on(PL_compcv);
315 subbody : block { $$ = $1; }
316 | ';' { $$ = Nullop; PL_expect = XSTATE; }
319 package : PACKAGE WORD ';'
326 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
327 WORD WORD listexpr ';'
328 { utilize($1, $2, $4, $5, $6); }
331 expr : expr ANDOP expr
332 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
334 { $$ = newLOGOP($2, 0, $1, $3); }
338 argexpr : argexpr ','
341 { $$ = append_elem(OP_LIST, $1, $3); }
345 listop : LSTOP indirob argexpr
346 { $$ = convert($1, OPf_STACKED,
347 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
348 | FUNC '(' indirob expr ')'
349 { $$ = convert($1, OPf_STACKED,
350 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
351 | term ARROW method '(' listexprcom ')'
352 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
354 prepend_elem(OP_LIST, scalar($1), $5),
355 newUNOP(OP_METHOD, 0, $3))); }
357 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
358 append_elem(OP_LIST, scalar($1),
359 newUNOP(OP_METHOD, 0, $3))); }
360 | METHOD indirob listexpr
361 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
363 prepend_elem(OP_LIST, $2, $3),
364 newUNOP(OP_METHOD, 0, $1))); }
365 | FUNCMETH indirob '(' listexprcom ')'
366 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
368 prepend_elem(OP_LIST, $2, $4),
369 newUNOP(OP_METHOD, 0, $1))); }
371 { $$ = convert($1, 0, $2); }
372 | FUNC '(' listexprcom ')'
373 { $$ = convert($1, 0, $3); }
374 | LSTOPSUB startanonsub block
375 { $3 = newANONSUB($2, 0, $3); }
377 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
379 prepend_elem(OP_LIST, $3, $5), $1)); }
386 term : term ASSIGNOP term
387 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
389 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
391 { if ($2 != OP_REPEAT)
393 $$ = newBINOP($2, 0, $1, scalar($3)); }
395 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
397 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
399 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
401 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
403 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
405 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
407 { $$ = newRANGE($2, scalar($1), scalar($3));}
409 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
411 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
412 | term '?' term ':' term
413 { $$ = newCONDOP(0, $1, $3, $5); }
415 { $$ = bind_match($2, $1, $3); }
417 | '-' term %prec UMINUS
418 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
419 | '+' term %prec UMINUS
422 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
424 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
426 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
428 { $$ = newUNOP(OP_POSTINC, 0,
429 mod(scalar($1), OP_POSTINC)); }
431 { $$ = newUNOP(OP_POSTDEC, 0,
432 mod(scalar($1), OP_POSTDEC)); }
434 { $$ = newUNOP(OP_PREINC, 0,
435 mod(scalar($2), OP_PREINC)); }
437 { $$ = newUNOP(OP_PREDEC, 0,
438 mod(scalar($2), OP_PREDEC)); }
439 | local term %prec UNIOP
440 { $$ = localize($2,$1); }
442 { $$ = sawparens($2); }
444 { $$ = sawparens(newNULLLIST()); }
445 | '[' expr ']' %prec '('
446 { $$ = newANONLIST($2); }
448 { $$ = newANONLIST(Nullop); }
449 | HASHBRACK expr ';' '}' %prec '('
450 { $$ = newANONHASH($2); }
451 | HASHBRACK ';' '}' %prec '('
452 { $$ = newANONHASH(Nullop); }
453 | ANONSUB startanonsub proto block %prec '('
454 { $$ = newANONSUB($2, $3, $4); }
457 | star '{' expr ';' '}'
458 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
461 | scalar '[' expr ']' %prec '('
462 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
463 | term ARROW '[' expr ']' %prec '('
464 { $$ = newBINOP(OP_AELEM, 0,
465 ref(newAVREF($1),OP_RV2AV),
467 | term '[' expr ']' %prec '('
468 { assertref($1); $$ = newBINOP(OP_AELEM, 0,
469 ref(newAVREF($1),OP_RV2AV),
476 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
477 | scalar '{' expr ';' '}' %prec '('
478 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
479 PL_expect = XOPERATOR; }
480 | term ARROW '{' expr ';' '}' %prec '('
481 { $$ = newBINOP(OP_HELEM, 0,
482 ref(newHVREF($1),OP_RV2HV),
484 PL_expect = XOPERATOR; }
485 | term '{' expr ';' '}' %prec '('
486 { assertref($1); $$ = newBINOP(OP_HELEM, 0,
487 ref(newHVREF($1),OP_RV2HV),
489 PL_expect = XOPERATOR; }
490 | '(' expr ')' '[' expr ']' %prec '('
491 { $$ = newSLICEOP(0, $5, $2); }
492 | '(' ')' '[' expr ']' %prec '('
493 { $$ = newSLICEOP(0, $4, Nullop); }
494 | ary '[' expr ']' %prec '('
495 { $$ = prepend_elem(OP_ASLICE,
496 newOP(OP_PUSHMARK, 0),
497 newLISTOP(OP_ASLICE, 0,
499 ref($1, OP_ASLICE))); }
500 | ary '{' expr ';' '}' %prec '('
501 { $$ = prepend_elem(OP_HSLICE,
502 newOP(OP_PUSHMARK, 0),
503 newLISTOP(OP_HSLICE, 0,
505 ref(oopsHV($1), OP_HSLICE)));
506 PL_expect = XOPERATOR; }
510 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
512 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
514 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
515 append_elem(OP_LIST, $3, scalar($1))); }
516 | NOAMP WORD listexpr
517 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
518 append_elem(OP_LIST, $3, scalar($2))); }
519 | DO term %prec UNIOP
522 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
524 { $$ = newUNOP(OP_ENTERSUB,
525 OPf_SPECIAL|OPf_STACKED,
526 prepend_elem(OP_LIST,
528 (OPpENTERSUB_AMPER<<8),
531 | DO WORD '(' expr ')'
532 { $$ = newUNOP(OP_ENTERSUB,
533 OPf_SPECIAL|OPf_STACKED,
537 (OPpENTERSUB_AMPER<<8),
541 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
542 prepend_elem(OP_LIST,
543 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
544 | DO scalar '(' expr ')'
545 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
546 prepend_elem(OP_LIST,
548 scalar(newCVREF(0,scalar($2))))); dep();}
549 | term ARROW '(' ')' %prec '('
550 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
551 newCVREF(0, scalar($1))); }
552 | term ARROW '(' expr ')' %prec '('
553 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
554 append_elem(OP_LIST, $4,
555 newCVREF(0, scalar($1)))); }
557 { $$ = newOP($1, OPf_SPECIAL);
558 PL_hints |= HINT_BLOCK_SCOPE; }
560 { $$ = newLOOPEX($1,$2); }
562 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
564 { $$ = newOP($1, 0); }
566 { $$ = newUNOP($1, 0, $2); }
568 { $$ = newUNOP($1, 0, $2); }
570 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
571 append_elem(OP_LIST, $2, scalar($1))); }
573 { $$ = newOP($1, 0); }
575 { $$ = newOP($1, 0); }
577 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
580 { $$ = newOP($1, OPf_SPECIAL); }
582 { $$ = newUNOP($1, 0, $3); }
583 | PMFUNC '(' term ')'
584 { $$ = pmruntime($1, $3, Nullop); }
585 | PMFUNC '(' term ',' term ')'
586 { $$ = pmruntime($1, $3, $5); }
597 listexprcom: /* NULL */
605 local : LOCAL { $$ = 0; }
610 { PL_in_my = 0; $$ = my($1); }
614 { $$ = newCVREF($1,$2); }
618 { $$ = newSVREF($2); }
622 { $$ = newAVREF($2); }
626 { $$ = newHVREF($2); }
629 arylen : DOLSHARP indirob
630 { $$ = newAVREF($2); }
634 { $$ = newGVREF(0,$2); }