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.'
22 deprecate("\"do\" to call subroutines");
38 %token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
39 %token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
41 %token <ival> FORMAT SUB ANONSUB PACKAGE USE
42 %token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
43 %token <ival> LOOPEX DOTDOT
44 %token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP
45 %token <ival> RELOP EQOP MULOP ADDOP
46 %token <ival> DOLSHARP DO HASHBRACK NOAMP
49 %type <ival> prog decl local format startsub startanonsub startformsub
50 %type <ival> remember mremember '&'
51 %type <opval> block mblock lineseq line loop cond else
52 %type <opval> expr term scalar ary hsh arylen star amper sideff
53 %type <opval> argexpr nexpr texpr iexpr mexpr mnexpr mtexpr miexpr
54 %type <opval> listexpr listexprcom indirob listop method
55 %type <opval> formname subname proto subbody cont my_scalar
61 %nonassoc LSTOP LSTOPSUB
63 %right <ival> ASSIGNOP
72 %nonassoc UNIOP UNIOPSUB
77 %right '!' '~' UMINUS REFGEN
79 %nonassoc PREINC PREDEC POSTINC POSTDEC
87 #if defined(YYDEBUG) && defined(DEBUGGING)
88 yydebug = (debug & 1);
96 block : '{' remember lineseq '}'
97 { if (copline > (line_t)$1)
99 $$ = block_end($2, $3); }
102 remember: /* NULL */ /* start a full lexical scope */
103 { $$ = block_start(TRUE); }
106 mblock : '{' mremember lineseq '}'
107 { if (copline > (line_t)$1)
109 $$ = block_end($2, $3); }
112 mremember: /* NULL */ /* start a partial lexical scope */
113 { $$ = block_start(FALSE); }
121 { $$ = append_list(OP_LINESEQ,
122 (LISTOP*)$1, (LISTOP*)$2);
123 pad_reset_pending = TRUE;
124 if ($1 && $2) hints |= HINT_BLOCK_SCOPE; }
128 { $$ = newSTATEOP(0, $1, $2); }
129 | loop /* loops add their own labels */
131 { if ($1 != Nullch) {
132 $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0));
140 { $$ = newSTATEOP(0, $1, $2);
149 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
151 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
153 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
155 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);}
162 | ELSIF '(' mexpr ')' mblock else
164 $$ = newSTATEOP(0, Nullch,
165 newCONDOP(0, $3, scope($5), $6));
166 hints |= HINT_BLOCK_SCOPE; }
169 cond : IF '(' remember mexpr ')' mblock else
172 newCONDOP(0, $4, scope($6), $7)); }
173 | UNLESS '(' remember miexpr ')' mblock else
176 newCONDOP(0, $4, scope($6), $7)); }
185 loop : label WHILE '(' remember mtexpr ')' mblock cont
189 newWHILEOP(0, 1, (LOOP*)Nullop,
191 | label UNTIL '(' remember miexpr ')' mblock cont
195 newWHILEOP(0, 1, (LOOP*)Nullop,
197 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
199 newFOROP(0, $1, $2, $5, $7, $9, $10)); }
200 | label FOR scalar '(' remember mexpr ')' mblock cont
202 newFOROP(0, $1, $2, mod($3, OP_ENTERLOOP),
204 | label FOR '(' remember mexpr ')' mblock cont
206 newFOROP(0, $1, $2, Nullop, $5, $7, $8)); }
207 | label FOR '(' remember mnexpr ';' mtexpr ';' mnexpr ')' mblock
208 /* basically fake up an initialize-while lineseq */
212 append_elem(OP_LINESEQ, scalar($5),
213 newWHILEOP(0, 1, (LOOP*)Nullop,
215 $11, scalar($9))))); }
216 | label block cont /* a block is a loop that happens once */
218 $1, newWHILEOP(0, 1, (LOOP*)Nullop,
227 texpr : /* NULL means true */
228 { (void)scan_num("1"); $$ = yylval.opval; }
233 { $$ = invert(scalar($1)); }
237 { $$ = $1; intro_my(); }
241 { $$ = $1; intro_my(); }
245 { $$ = $1; intro_my(); }
249 { $$ = $1; intro_my(); }
267 format : FORMAT startformsub formname block
268 { newFORM($2, $3, $4); }
271 formname: WORD { $$ = $1; }
272 | /* NULL */ { $$ = Nullop; }
275 subrout : SUB startsub subname proto subbody
276 { newSUB($2, $3, $4, $5); }
279 startsub: /* NULL */ /* start a regular subroutine scope */
280 { $$ = start_subparse(FALSE, 0); }
283 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
284 { $$ = start_subparse(FALSE, CVf_ANON); }
287 startformsub: /* NULL */ /* start a format subroutine scope */
288 { $$ = start_subparse(TRUE, 0); }
291 subname : WORD { char *name = SvPVx(((SVOP*)$1)->op_sv, na);
292 if (strEQ(name, "BEGIN") || strEQ(name, "END"))
302 subbody : block { $$ = $1; }
303 | ';' { $$ = Nullop; expect = XSTATE; }
306 package : PACKAGE WORD ';'
313 { CvUNIQUE_on(compcv); /* It's a BEGIN {} */ }
314 WORD WORD listexpr ';'
315 { utilize($1, $2, $4, $5, $6); }
318 expr : expr ANDOP expr
319 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
321 { $$ = newLOGOP($2, 0, $1, $3); }
325 argexpr : argexpr ','
328 { $$ = append_elem(OP_LIST, $1, $3); }
332 listop : LSTOP indirob argexpr
333 { $$ = convert($1, OPf_STACKED,
334 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
335 | FUNC '(' indirob expr ')'
336 { $$ = convert($1, OPf_STACKED,
337 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
338 | term ARROW method '(' listexprcom ')'
339 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
341 prepend_elem(OP_LIST, scalar($1), $5),
342 newUNOP(OP_METHOD, 0, $3))); }
343 | METHOD indirob listexpr
344 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
346 prepend_elem(OP_LIST, $2, $3),
347 newUNOP(OP_METHOD, 0, $1))); }
348 | FUNCMETH indirob '(' listexprcom ')'
349 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
351 prepend_elem(OP_LIST, $2, $4),
352 newUNOP(OP_METHOD, 0, $1))); }
354 { $$ = convert($1, 0, $2); }
355 | FUNC '(' listexprcom ')'
356 { $$ = convert($1, 0, $3); }
357 | LSTOPSUB startanonsub block
358 { $3 = newANONSUB($2, 0, $3); }
360 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
362 prepend_elem(OP_LIST, $3, $5), $1)); }
369 term : term ASSIGNOP term
370 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
372 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
374 { if ($2 != OP_REPEAT)
376 $$ = newBINOP($2, 0, $1, scalar($3)); }
378 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
380 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
382 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
384 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
386 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
388 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
390 { $$ = newRANGE($2, scalar($1), scalar($3));}
392 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
394 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
395 | term '?' term ':' term
396 { $$ = newCONDOP(0, $1, $3, $5); }
398 { $$ = bind_match($2, $1, $3); }
400 | '-' term %prec UMINUS
401 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
402 | '+' term %prec UMINUS
405 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
407 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
409 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
411 { $$ = newUNOP(OP_POSTINC, 0,
412 mod(scalar($1), OP_POSTINC)); }
414 { $$ = newUNOP(OP_POSTDEC, 0,
415 mod(scalar($1), OP_POSTDEC)); }
417 { $$ = newUNOP(OP_PREINC, 0,
418 mod(scalar($2), OP_PREINC)); }
420 { $$ = newUNOP(OP_PREDEC, 0,
421 mod(scalar($2), OP_PREDEC)); }
422 | local term %prec UNIOP
423 { $$ = localize($2,$1); }
425 { $$ = sawparens($2); }
427 { $$ = sawparens(newNULLLIST()); }
428 | '[' expr ']' %prec '('
429 { $$ = newANONLIST($2); }
431 { $$ = newANONLIST(Nullop); }
432 | HASHBRACK expr ';' '}' %prec '('
433 { $$ = newANONHASH($2); }
434 | HASHBRACK ';' '}' %prec '('
435 { $$ = newANONHASH(Nullop); }
436 | ANONSUB startanonsub proto block %prec '('
437 { $$ = newANONSUB($2, $3, $4); }
440 | star '{' expr ';' '}'
441 { $$ = newBINOP(OP_GELEM, 0, newGVREF(0,$1), $3); }
444 | scalar '[' expr ']' %prec '('
445 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
446 | term ARROW '[' expr ']' %prec '('
447 { $$ = newBINOP(OP_AELEM, 0,
448 ref(newAVREF($1),OP_RV2AV),
450 | term '[' expr ']' %prec '('
451 { assertref($1); $$ = newBINOP(OP_AELEM, 0,
452 ref(newAVREF($1),OP_RV2AV),
459 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
460 | scalar '{' expr ';' '}' %prec '('
461 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
462 expect = XOPERATOR; }
463 | term ARROW '{' expr ';' '}' %prec '('
464 { $$ = newBINOP(OP_HELEM, 0,
465 ref(newHVREF($1),OP_RV2HV),
467 expect = XOPERATOR; }
468 | term '{' expr ';' '}' %prec '('
469 { assertref($1); $$ = newBINOP(OP_HELEM, 0,
470 ref(newHVREF($1),OP_RV2HV),
472 expect = XOPERATOR; }
473 | '(' expr ')' '[' expr ']' %prec '('
474 { $$ = newSLICEOP(0, $5, $2); }
475 | '(' ')' '[' expr ']' %prec '('
476 { $$ = newSLICEOP(0, $4, Nullop); }
477 | ary '[' expr ']' %prec '('
478 { $$ = prepend_elem(OP_ASLICE,
479 newOP(OP_PUSHMARK, 0),
480 newLISTOP(OP_ASLICE, 0,
482 ref($1, OP_ASLICE))); }
483 | ary '{' expr ';' '}' %prec '('
484 { $$ = prepend_elem(OP_HSLICE,
485 newOP(OP_PUSHMARK, 0),
486 newLISTOP(OP_HSLICE, 0,
488 ref(oopsHV($1), OP_HSLICE)));
489 expect = XOPERATOR; }
493 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
495 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
497 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
498 append_elem(OP_LIST, $3, scalar($1))); }
499 | NOAMP WORD listexpr
500 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
501 append_elem(OP_LIST, $3, scalar($2))); }
502 | DO term %prec UNIOP
503 { $$ = newUNOP(OP_DOFILE, 0, scalar($2)); }
505 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
507 { $$ = newUNOP(OP_ENTERSUB,
508 OPf_SPECIAL|OPf_STACKED,
509 prepend_elem(OP_LIST,
511 (OPpENTERSUB_AMPER<<8),
514 | DO WORD '(' expr ')'
515 { $$ = newUNOP(OP_ENTERSUB,
516 OPf_SPECIAL|OPf_STACKED,
520 (OPpENTERSUB_AMPER<<8),
524 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
525 prepend_elem(OP_LIST,
526 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
527 | DO scalar '(' expr ')'
528 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
529 prepend_elem(OP_LIST,
531 scalar(newCVREF(0,scalar($2))))); dep();}
532 | term ARROW '(' ')' %prec '('
533 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
534 newCVREF(0, scalar($1))); }
535 | term ARROW '(' expr ')' %prec '('
536 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
537 append_elem(OP_LIST, $4,
538 newCVREF(0, scalar($1)))); }
540 { $$ = newOP($1, OPf_SPECIAL);
541 hints |= HINT_BLOCK_SCOPE; }
543 { $$ = newLOOPEX($1,$2); }
545 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
547 { $$ = newOP($1, 0); }
549 { $$ = newUNOP($1, 0, $2); }
551 { $$ = newUNOP($1, 0, $2); }
553 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
554 append_elem(OP_LIST, $2, scalar($1))); }
556 { $$ = newOP($1, 0); }
558 { $$ = newOP($1, 0); }
560 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
563 { $$ = newOP($1, OPf_SPECIAL); }
565 { $$ = newUNOP($1, 0, $3); }
566 | PMFUNC '(' term ')'
567 { $$ = pmruntime($1, $3, Nullop); }
568 | PMFUNC '(' term ',' term ')'
569 { $$ = pmruntime($1, $3, $5); }
580 listexprcom: /* NULL */
588 local : LOCAL { $$ = 0; }
593 { in_my = 0; $$ = my($1); }
597 { $$ = newCVREF($1,$2); }
601 { $$ = newSVREF($2); }
605 { $$ = newAVREF($2); }
609 { $$ = newHVREF($2); }
612 arylen : DOLSHARP indirob
613 { $$ = newAVREF($2); }
617 { $$ = newGVREF(0,$2); }