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),
446 | scalar '[' expr ']' %prec '('
447 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
448 | term ARROW '[' expr ']' %prec '('
449 { $$ = newBINOP(OP_AELEM, 0,
450 ref(newAVREF($1),OP_RV2AV),
452 | term '[' expr ']' %prec '('
453 { assertref($1); $$ = newBINOP(OP_AELEM, 0,
454 ref(newAVREF($1),OP_RV2AV),
461 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
462 | scalar '{' expr ';' '}' %prec '('
463 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
464 expect = XOPERATOR; }
465 | term ARROW '{' expr ';' '}' %prec '('
466 { $$ = newBINOP(OP_HELEM, 0,
467 ref(newHVREF($1),OP_RV2HV),
469 expect = XOPERATOR; }
470 | term '{' expr ';' '}' %prec '('
471 { assertref($1); $$ = newBINOP(OP_HELEM, 0,
472 ref(newHVREF($1),OP_RV2HV),
474 expect = XOPERATOR; }
475 | '(' expr ')' '[' expr ']' %prec '('
476 { $$ = newSLICEOP(0, $5, $2); }
477 | '(' ')' '[' expr ']' %prec '('
478 { $$ = newSLICEOP(0, $4, Nullop); }
479 | ary '[' expr ']' %prec '('
480 { $$ = prepend_elem(OP_ASLICE,
481 newOP(OP_PUSHMARK, 0),
482 newLISTOP(OP_ASLICE, 0,
484 ref($1, OP_ASLICE))); }
485 | ary '{' expr ';' '}' %prec '('
486 { $$ = prepend_elem(OP_HSLICE,
487 newOP(OP_PUSHMARK, 0),
488 newLISTOP(OP_HSLICE, 0,
490 ref(oopsHV($1), OP_HSLICE)));
491 expect = XOPERATOR; }
495 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
497 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
499 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
500 append_elem(OP_LIST, $3, scalar($1))); }
501 | NOAMP WORD listexpr
502 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
503 append_elem(OP_LIST, $3, scalar($2))); }
504 | DO term %prec UNIOP
505 { $$ = newUNOP(OP_DOFILE, 0, scalar($2)); }
507 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
509 { $$ = newUNOP(OP_ENTERSUB,
510 OPf_SPECIAL|OPf_STACKED,
511 prepend_elem(OP_LIST,
513 (OPpENTERSUB_AMPER<<8),
516 | DO WORD '(' expr ')'
517 { $$ = newUNOP(OP_ENTERSUB,
518 OPf_SPECIAL|OPf_STACKED,
522 (OPpENTERSUB_AMPER<<8),
526 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
527 prepend_elem(OP_LIST,
528 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
529 | DO scalar '(' expr ')'
530 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
531 prepend_elem(OP_LIST,
533 scalar(newCVREF(0,scalar($2))))); dep();}
534 | term ARROW '(' ')' %prec '('
535 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
536 newCVREF(0, scalar($1))); }
537 | term ARROW '(' expr ')' %prec '('
538 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
539 append_elem(OP_LIST, $4,
540 newCVREF(0, scalar($1)))); }
542 { $$ = newOP($1, OPf_SPECIAL);
543 hints |= HINT_BLOCK_SCOPE; }
545 { $$ = newLOOPEX($1,$2); }
547 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
549 { $$ = newOP($1, 0); }
551 { $$ = newUNOP($1, 0, $2); }
553 { $$ = newUNOP($1, 0, $2); }
555 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
556 append_elem(OP_LIST, $2, scalar($1))); }
558 { $$ = newOP($1, 0); }
560 { $$ = newOP($1, 0); }
562 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
565 { $$ = newOP($1, OPf_SPECIAL); }
567 { $$ = newUNOP($1, 0, $3); }
568 | PMFUNC '(' term ')'
569 { $$ = pmruntime($1, $3, Nullop); }
570 | PMFUNC '(' term ',' term ')'
571 { $$ = pmruntime($1, $3, $5); }
582 listexprcom: /* NULL */
590 local : LOCAL { $$ = 0; }
595 { in_my = 0; $$ = my($1); }
599 { $$ = newCVREF($1,$2); }
603 { $$ = newSVREF($2); }
607 { $$ = newAVREF($2); }
611 { $$ = newHVREF($2); }
614 arylen : DOLSHARP indirob
615 { $$ = newAVREF($2); }
619 { $$ = newGVREF(0,$2); }