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 $$ = newSTATEOP(0, Nullch,
178 newCONDOP(0, $3, scope($5), $6));
179 PL_hints |= HINT_BLOCK_SCOPE; }
182 cond : IF '(' remember mexpr ')' mblock else
185 newCONDOP(0, $4, scope($6), $7)); }
186 | UNLESS '(' remember miexpr ')' mblock else
189 newCONDOP(0, $4, scope($6), $7)); }
198 loop : label WHILE '(' remember mtexpr ')' mblock cont
202 newWHILEOP(0, 1, (LOOP*)Nullop,
204 | label UNTIL '(' remember miexpr ')' mblock cont
208 newWHILEOP(0, 1, (LOOP*)Nullop,
210 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
212 newFOROP(0, $1, $2, $5, $7, $9, $10)); }
213 | label FOR scalar '(' remember mexpr ')' mblock cont
215 newFOROP(0, $1, $2, mod($3, OP_ENTERLOOP),
217 | label FOR '(' remember mexpr ')' mblock cont
219 newFOROP(0, $1, $2, Nullop, $5, $7, $8)); }
220 | label FOR '(' remember mnexpr ';' mtexpr ';' mnexpr ')' mblock
221 /* basically fake up an initialize-while lineseq */
222 { OP *forop = append_elem(OP_LINESEQ,
224 newWHILEOP(0, 1, (LOOP*)Nullop,
228 $$ = block_end($4, newSTATEOP(0, $1, forop)); }
229 | label block cont /* a block is a loop that happens once */
230 { $$ = newSTATEOP(0, $1,
231 newWHILEOP(0, 1, (LOOP*)Nullop,
232 NOLINE, Nullop, $2, $3)); }
240 texpr : /* NULL means true */
241 { (void)scan_num("1"); $$ = yylval.opval; }
246 { $$ = invert(scalar($1)); }
250 { $$ = $1; intro_my(); }
254 { $$ = $1; intro_my(); }
258 { $$ = $1; intro_my(); }
262 { $$ = $1; intro_my(); }
280 format : FORMAT startformsub formname block
281 { newFORM($2, $3, $4); }
284 formname: WORD { $$ = $1; }
285 | /* NULL */ { $$ = Nullop; }
288 subrout : SUB startsub subname proto subbody
289 { newSUB($2, $3, $4, $5); }
292 startsub: /* NULL */ /* start a regular subroutine scope */
293 { $$ = start_subparse(FALSE, 0); }
296 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
297 { $$ = start_subparse(FALSE, CVf_ANON); }
300 startformsub: /* NULL */ /* start a format subroutine scope */
301 { $$ = start_subparse(TRUE, 0); }
304 subname : WORD { STRLEN n_a; char *name = SvPV(((SVOP*)$1)->op_sv,n_a);
305 if (strEQ(name, "BEGIN") || strEQ(name, "END")
306 || strEQ(name, "INIT"))
307 CvSPECIAL_on(PL_compcv);
316 subbody : block { $$ = $1; }
317 | ';' { $$ = Nullop; PL_expect = XSTATE; }
320 package : PACKAGE WORD ';'
327 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
328 WORD WORD listexpr ';'
329 { utilize($1, $2, $4, $5, $6); }
332 expr : expr ANDOP expr
333 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
335 { $$ = newLOGOP($2, 0, $1, $3); }
339 argexpr : argexpr ','
342 { $$ = append_elem(OP_LIST, $1, $3); }
346 listop : LSTOP indirob argexpr
347 { $$ = convert($1, OPf_STACKED,
348 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
349 | FUNC '(' indirob expr ')'
350 { $$ = convert($1, OPf_STACKED,
351 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
352 | term ARROW method '(' listexprcom ')'
353 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
355 prepend_elem(OP_LIST, scalar($1), $5),
356 newUNOP(OP_METHOD, 0, $3))); }
358 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
359 append_elem(OP_LIST, scalar($1),
360 newUNOP(OP_METHOD, 0, $3))); }
361 | METHOD indirob listexpr
362 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
364 prepend_elem(OP_LIST, $2, $3),
365 newUNOP(OP_METHOD, 0, $1))); }
366 | FUNCMETH indirob '(' listexprcom ')'
367 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
369 prepend_elem(OP_LIST, $2, $4),
370 newUNOP(OP_METHOD, 0, $1))); }
372 { $$ = convert($1, 0, $2); }
373 | FUNC '(' listexprcom ')'
374 { $$ = convert($1, 0, $3); }
375 | LSTOPSUB startanonsub block
376 { $3 = newANONSUB($2, 0, $3); }
378 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
380 prepend_elem(OP_LIST, $3, $5), $1)); }
387 term : term ASSIGNOP term
388 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
390 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
392 { if ($2 != OP_REPEAT)
394 $$ = newBINOP($2, 0, $1, scalar($3)); }
396 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
398 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
400 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
402 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
404 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
406 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
408 { $$ = newRANGE($2, scalar($1), scalar($3));}
410 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
412 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
413 | term '?' term ':' term
414 { $$ = newCONDOP(0, $1, $3, $5); }
416 { $$ = bind_match($2, $1, $3); }
418 | '-' term %prec UMINUS
419 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
420 | '+' term %prec UMINUS
423 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
425 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
427 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
429 { $$ = newUNOP(OP_POSTINC, 0,
430 mod(scalar($1), OP_POSTINC)); }
432 { $$ = newUNOP(OP_POSTDEC, 0,
433 mod(scalar($1), OP_POSTDEC)); }
435 { $$ = newUNOP(OP_PREINC, 0,
436 mod(scalar($2), OP_PREINC)); }
438 { $$ = newUNOP(OP_PREDEC, 0,
439 mod(scalar($2), OP_PREDEC)); }
440 | local term %prec UNIOP
441 { $$ = localize($2,$1); }
443 { $$ = sawparens($2); }
445 { $$ = sawparens(newNULLLIST()); }
446 | '[' expr ']' %prec '('
447 { $$ = newANONLIST($2); }
449 { $$ = newANONLIST(Nullop); }
450 | HASHBRACK expr ';' '}' %prec '('
451 { $$ = newANONHASH($2); }
452 | HASHBRACK ';' '}' %prec '('
453 { $$ = newANONHASH(Nullop); }
454 | ANONSUB startanonsub proto block %prec '('
455 { $$ = newANONSUB($2, $3, $4); }
458 | star '{' expr ';' '}'
459 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
462 | scalar '[' expr ']' %prec '('
463 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
464 | term ARROW '[' expr ']' %prec '('
465 { $$ = newBINOP(OP_AELEM, 0,
466 ref(newAVREF($1),OP_RV2AV),
468 | term '[' expr ']' %prec '('
469 { assertref($1); $$ = newBINOP(OP_AELEM, 0,
470 ref(newAVREF($1),OP_RV2AV),
477 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
478 | scalar '{' expr ';' '}' %prec '('
479 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
480 PL_expect = XOPERATOR; }
481 | term ARROW '{' expr ';' '}' %prec '('
482 { $$ = newBINOP(OP_HELEM, 0,
483 ref(newHVREF($1),OP_RV2HV),
485 PL_expect = XOPERATOR; }
486 | term '{' expr ';' '}' %prec '('
487 { assertref($1); $$ = newBINOP(OP_HELEM, 0,
488 ref(newHVREF($1),OP_RV2HV),
490 PL_expect = XOPERATOR; }
491 | '(' expr ')' '[' expr ']' %prec '('
492 { $$ = newSLICEOP(0, $5, $2); }
493 | '(' ')' '[' expr ']' %prec '('
494 { $$ = newSLICEOP(0, $4, Nullop); }
495 | ary '[' expr ']' %prec '('
496 { $$ = prepend_elem(OP_ASLICE,
497 newOP(OP_PUSHMARK, 0),
498 newLISTOP(OP_ASLICE, 0,
500 ref($1, OP_ASLICE))); }
501 | ary '{' expr ';' '}' %prec '('
502 { $$ = prepend_elem(OP_HSLICE,
503 newOP(OP_PUSHMARK, 0),
504 newLISTOP(OP_HSLICE, 0,
506 ref(oopsHV($1), OP_HSLICE)));
507 PL_expect = XOPERATOR; }
511 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
513 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
515 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
516 append_elem(OP_LIST, $3, scalar($1))); }
517 | NOAMP WORD listexpr
518 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
519 append_elem(OP_LIST, $3, scalar($2))); }
520 | DO term %prec UNIOP
523 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
525 { $$ = newUNOP(OP_ENTERSUB,
526 OPf_SPECIAL|OPf_STACKED,
527 prepend_elem(OP_LIST,
529 (OPpENTERSUB_AMPER<<8),
532 | DO WORD '(' expr ')'
533 { $$ = newUNOP(OP_ENTERSUB,
534 OPf_SPECIAL|OPf_STACKED,
538 (OPpENTERSUB_AMPER<<8),
542 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
543 prepend_elem(OP_LIST,
544 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
545 | DO scalar '(' expr ')'
546 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
547 prepend_elem(OP_LIST,
549 scalar(newCVREF(0,scalar($2))))); dep();}
550 | term ARROW '(' ')' %prec '('
551 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
552 newCVREF(0, scalar($1))); }
553 | term ARROW '(' expr ')' %prec '('
554 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
555 append_elem(OP_LIST, $4,
556 newCVREF(0, scalar($1)))); }
558 { $$ = newOP($1, OPf_SPECIAL);
559 PL_hints |= HINT_BLOCK_SCOPE; }
561 { $$ = newLOOPEX($1,$2); }
563 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
565 { $$ = newOP($1, 0); }
567 { $$ = newUNOP($1, 0, $2); }
569 { $$ = newUNOP($1, 0, $2); }
571 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
572 append_elem(OP_LIST, $2, scalar($1))); }
574 { $$ = newOP($1, 0); }
576 { $$ = newOP($1, 0); }
578 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
581 { $$ = newOP($1, OPf_SPECIAL); }
583 { $$ = newUNOP($1, 0, $3); }
584 | PMFUNC '(' term ')'
585 { $$ = pmruntime($1, $3, Nullop); }
586 | PMFUNC '(' term ',' term ')'
587 { $$ = pmruntime($1, $3, $5); }
598 listexprcom: /* NULL */
606 local : LOCAL { $$ = 0; }
611 { PL_in_my = 0; $$ = my($1); }
615 { $$ = newCVREF($1,$2); }
619 { $$ = newSVREF($2); }
623 { $$ = newAVREF($2); }
627 { $$ = newHVREF($2); }
630 arylen : DOLSHARP indirob
631 { $$ = newAVREF($2); }
635 { $$ = newGVREF(0,$2); }