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 */
209 { OP *forop = append_elem(OP_LINESEQ,
211 newWHILEOP(0, 1, (LOOP*)Nullop,
215 $$ = block_end($4, newSTATEOP(0, $1, forop)); }
216 | label block cont /* a block is a loop that happens once */
217 { $$ = newSTATEOP(0, $1,
218 newWHILEOP(0, 1, (LOOP*)Nullop,
219 NOLINE, Nullop, $2, $3)); }
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 = SvPV(((SVOP*)$1)->op_sv, na);
292 if (strEQ(name, "BEGIN") || strEQ(name, "END")
293 || strEQ(name, "INIT"))
303 subbody : block { $$ = $1; }
304 | ';' { $$ = Nullop; expect = XSTATE; }
307 package : PACKAGE WORD ';'
314 { CvUNIQUE_on(compcv); /* It's a BEGIN {} */ }
315 WORD WORD listexpr ';'
316 { utilize($1, $2, $4, $5, $6); }
319 expr : expr ANDOP expr
320 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
322 { $$ = newLOGOP($2, 0, $1, $3); }
326 argexpr : argexpr ','
329 { $$ = append_elem(OP_LIST, $1, $3); }
333 listop : LSTOP indirob argexpr
334 { $$ = convert($1, OPf_STACKED,
335 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
336 | FUNC '(' indirob expr ')'
337 { $$ = convert($1, OPf_STACKED,
338 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
339 | term ARROW method '(' listexprcom ')'
340 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
342 prepend_elem(OP_LIST, scalar($1), $5),
343 newUNOP(OP_METHOD, 0, $3))); }
344 | METHOD indirob listexpr
345 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
347 prepend_elem(OP_LIST, $2, $3),
348 newUNOP(OP_METHOD, 0, $1))); }
349 | FUNCMETH indirob '(' listexprcom ')'
350 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
352 prepend_elem(OP_LIST, $2, $4),
353 newUNOP(OP_METHOD, 0, $1))); }
355 { $$ = convert($1, 0, $2); }
356 | FUNC '(' listexprcom ')'
357 { $$ = convert($1, 0, $3); }
358 | LSTOPSUB startanonsub block
359 { $3 = newANONSUB($2, 0, $3); }
361 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
363 prepend_elem(OP_LIST, $3, $5), $1)); }
370 term : term ASSIGNOP term
371 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
373 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
375 { if ($2 != OP_REPEAT)
377 $$ = newBINOP($2, 0, $1, scalar($3)); }
379 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
381 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
383 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
385 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
387 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
389 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
391 { $$ = newRANGE($2, scalar($1), scalar($3));}
393 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
395 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
396 | term '?' term ':' term
397 { $$ = newCONDOP(0, $1, $3, $5); }
399 { $$ = bind_match($2, $1, $3); }
401 | '-' term %prec UMINUS
402 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
403 | '+' term %prec UMINUS
406 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
408 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
410 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
412 { $$ = newUNOP(OP_POSTINC, 0,
413 mod(scalar($1), OP_POSTINC)); }
415 { $$ = newUNOP(OP_POSTDEC, 0,
416 mod(scalar($1), OP_POSTDEC)); }
418 { $$ = newUNOP(OP_PREINC, 0,
419 mod(scalar($2), OP_PREINC)); }
421 { $$ = newUNOP(OP_PREDEC, 0,
422 mod(scalar($2), OP_PREDEC)); }
423 | local term %prec UNIOP
424 { $$ = localize($2,$1); }
426 { $$ = sawparens($2); }
428 { $$ = sawparens(newNULLLIST()); }
429 | '[' expr ']' %prec '('
430 { $$ = newANONLIST($2); }
432 { $$ = newANONLIST(Nullop); }
433 | HASHBRACK expr ';' '}' %prec '('
434 { $$ = newANONHASH($2); }
435 | HASHBRACK ';' '}' %prec '('
436 { $$ = newANONHASH(Nullop); }
437 | ANONSUB startanonsub proto block %prec '('
438 { $$ = newANONSUB($2, $3, $4); }
441 | star '{' expr ';' '}'
442 { $$ = newBINOP(OP_GELEM, 0, newGVREF(0,$1), $3); }
445 | scalar '[' expr ']' %prec '('
446 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
447 | term ARROW '[' expr ']' %prec '('
448 { $$ = newBINOP(OP_AELEM, 0,
449 ref(newAVREF($1),OP_RV2AV),
451 | term '[' expr ']' %prec '('
452 { assertref($1); $$ = newBINOP(OP_AELEM, 0,
453 ref(newAVREF($1),OP_RV2AV),
460 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
461 | scalar '{' expr ';' '}' %prec '('
462 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
463 expect = XOPERATOR; }
464 | term ARROW '{' expr ';' '}' %prec '('
465 { $$ = newBINOP(OP_HELEM, 0,
466 ref(newHVREF($1),OP_RV2HV),
468 expect = XOPERATOR; }
469 | term '{' expr ';' '}' %prec '('
470 { assertref($1); $$ = newBINOP(OP_HELEM, 0,
471 ref(newHVREF($1),OP_RV2HV),
473 expect = XOPERATOR; }
474 | '(' expr ')' '[' expr ']' %prec '('
475 { $$ = newSLICEOP(0, $5, $2); }
476 | '(' ')' '[' expr ']' %prec '('
477 { $$ = newSLICEOP(0, $4, Nullop); }
478 | ary '[' expr ']' %prec '('
479 { $$ = prepend_elem(OP_ASLICE,
480 newOP(OP_PUSHMARK, 0),
481 newLISTOP(OP_ASLICE, 0,
483 ref($1, OP_ASLICE))); }
484 | ary '{' expr ';' '}' %prec '('
485 { $$ = prepend_elem(OP_HSLICE,
486 newOP(OP_PUSHMARK, 0),
487 newLISTOP(OP_HSLICE, 0,
489 ref(oopsHV($1), OP_HSLICE)));
490 expect = XOPERATOR; }
494 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
496 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
498 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
499 append_elem(OP_LIST, $3, scalar($1))); }
500 | NOAMP WORD listexpr
501 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
502 append_elem(OP_LIST, $3, scalar($2))); }
503 | DO term %prec UNIOP
504 { $$ = newUNOP(OP_DOFILE, 0, scalar($2)); }
506 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
508 { $$ = newUNOP(OP_ENTERSUB,
509 OPf_SPECIAL|OPf_STACKED,
510 prepend_elem(OP_LIST,
512 (OPpENTERSUB_AMPER<<8),
515 | DO WORD '(' expr ')'
516 { $$ = newUNOP(OP_ENTERSUB,
517 OPf_SPECIAL|OPf_STACKED,
521 (OPpENTERSUB_AMPER<<8),
525 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
526 prepend_elem(OP_LIST,
527 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
528 | DO scalar '(' expr ')'
529 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
530 prepend_elem(OP_LIST,
532 scalar(newCVREF(0,scalar($2))))); dep();}
533 | term ARROW '(' ')' %prec '('
534 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
535 newCVREF(0, scalar($1))); }
536 | term ARROW '(' expr ')' %prec '('
537 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
538 append_elem(OP_LIST, $4,
539 newCVREF(0, scalar($1)))); }
541 { $$ = newOP($1, OPf_SPECIAL);
542 hints |= HINT_BLOCK_SCOPE; }
544 { $$ = newLOOPEX($1,$2); }
546 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
548 { $$ = newOP($1, 0); }
550 { $$ = newUNOP($1, 0, $2); }
552 { $$ = newUNOP($1, 0, $2); }
554 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
555 append_elem(OP_LIST, $2, scalar($1))); }
557 { $$ = newOP($1, 0); }
559 { $$ = newOP($1, 0); }
561 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
564 { $$ = newOP($1, OPf_SPECIAL); }
566 { $$ = newUNOP($1, 0, $3); }
567 | PMFUNC '(' term ')'
568 { $$ = pmruntime($1, $3, Nullop); }
569 | PMFUNC '(' term ',' term ')'
570 { $$ = pmruntime($1, $3, $5); }
581 listexprcom: /* NULL */
589 local : LOCAL { $$ = 0; }
594 { in_my = 0; $$ = my($1); }
598 { $$ = newCVREF($1,$2); }
602 { $$ = newSVREF($2); }
606 { $$ = newAVREF($2); }
610 { $$ = newHVREF($2); }
613 arylen : DOLSHARP indirob
614 { $$ = newAVREF($2); }
618 { $$ = newGVREF(0,$2); }