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);}
157 { $$ = newFOROP(0, Nullch, $2,
158 Nullop, $3, $1, Nullop); }
165 | ELSIF '(' mexpr ')' mblock else
167 $$ = newSTATEOP(0, Nullch,
168 newCONDOP(0, $3, scope($5), $6));
169 hints |= HINT_BLOCK_SCOPE; }
172 cond : IF '(' remember mexpr ')' mblock else
175 newCONDOP(0, $4, scope($6), $7)); }
176 | UNLESS '(' remember miexpr ')' mblock else
179 newCONDOP(0, $4, scope($6), $7)); }
188 loop : label WHILE '(' remember mtexpr ')' mblock cont
192 newWHILEOP(0, 1, (LOOP*)Nullop,
194 | label UNTIL '(' remember miexpr ')' mblock cont
198 newWHILEOP(0, 1, (LOOP*)Nullop,
200 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
202 newFOROP(0, $1, $2, $5, $7, $9, $10)); }
203 | label FOR scalar '(' remember mexpr ')' mblock cont
205 newFOROP(0, $1, $2, mod($3, OP_ENTERLOOP),
207 | label FOR '(' remember mexpr ')' mblock cont
209 newFOROP(0, $1, $2, Nullop, $5, $7, $8)); }
210 | label FOR '(' remember mnexpr ';' mtexpr ';' mnexpr ')' mblock
211 /* basically fake up an initialize-while lineseq */
212 { OP *forop = append_elem(OP_LINESEQ,
214 newWHILEOP(0, 1, (LOOP*)Nullop,
218 $$ = block_end($4, newSTATEOP(0, $1, forop)); }
219 | label block cont /* a block is a loop that happens once */
220 { $$ = newSTATEOP(0, $1,
221 newWHILEOP(0, 1, (LOOP*)Nullop,
222 NOLINE, Nullop, $2, $3)); }
230 texpr : /* NULL means true */
231 { (void)scan_num("1"); $$ = yylval.opval; }
236 { $$ = invert(scalar($1)); }
240 { $$ = $1; intro_my(); }
244 { $$ = $1; intro_my(); }
248 { $$ = $1; intro_my(); }
252 { $$ = $1; intro_my(); }
270 format : FORMAT startformsub formname block
271 { newFORM($2, $3, $4); }
274 formname: WORD { $$ = $1; }
275 | /* NULL */ { $$ = Nullop; }
278 subrout : SUB startsub subname proto subbody
279 { newSUB($2, $3, $4, $5); }
282 startsub: /* NULL */ /* start a regular subroutine scope */
283 { $$ = start_subparse(FALSE, 0); }
286 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
287 { $$ = start_subparse(FALSE, CVf_ANON); }
290 startformsub: /* NULL */ /* start a format subroutine scope */
291 { $$ = start_subparse(TRUE, 0); }
294 subname : WORD { char *name = SvPV(((SVOP*)$1)->op_sv, na);
295 if (strEQ(name, "BEGIN") || strEQ(name, "END")
296 || strEQ(name, "INIT"))
306 subbody : block { $$ = $1; }
307 | ';' { $$ = Nullop; expect = XSTATE; }
310 package : PACKAGE WORD ';'
317 { CvUNIQUE_on(compcv); /* It's a BEGIN {} */ }
318 WORD WORD listexpr ';'
319 { utilize($1, $2, $4, $5, $6); }
322 expr : expr ANDOP expr
323 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
325 { $$ = newLOGOP($2, 0, $1, $3); }
329 argexpr : argexpr ','
332 { $$ = append_elem(OP_LIST, $1, $3); }
336 listop : LSTOP indirob argexpr
337 { $$ = convert($1, OPf_STACKED,
338 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
339 | FUNC '(' indirob expr ')'
340 { $$ = convert($1, OPf_STACKED,
341 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
342 | term ARROW method '(' listexprcom ')'
343 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
345 prepend_elem(OP_LIST, scalar($1), $5),
346 newUNOP(OP_METHOD, 0, $3))); }
347 | METHOD indirob listexpr
348 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
350 prepend_elem(OP_LIST, $2, $3),
351 newUNOP(OP_METHOD, 0, $1))); }
352 | FUNCMETH indirob '(' listexprcom ')'
353 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
355 prepend_elem(OP_LIST, $2, $4),
356 newUNOP(OP_METHOD, 0, $1))); }
358 { $$ = convert($1, 0, $2); }
359 | FUNC '(' listexprcom ')'
360 { $$ = convert($1, 0, $3); }
361 | LSTOPSUB startanonsub block
362 { $3 = newANONSUB($2, 0, $3); }
364 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
366 prepend_elem(OP_LIST, $3, $5), $1)); }
373 term : term ASSIGNOP term
374 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
376 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
378 { if ($2 != OP_REPEAT)
380 $$ = newBINOP($2, 0, $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 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
392 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
394 { $$ = newRANGE($2, scalar($1), scalar($3));}
396 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
398 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
399 | term '?' term ':' term
400 { $$ = newCONDOP(0, $1, $3, $5); }
402 { $$ = bind_match($2, $1, $3); }
404 | '-' term %prec UMINUS
405 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
406 | '+' term %prec UMINUS
409 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
411 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
413 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
415 { $$ = newUNOP(OP_POSTINC, 0,
416 mod(scalar($1), OP_POSTINC)); }
418 { $$ = newUNOP(OP_POSTDEC, 0,
419 mod(scalar($1), OP_POSTDEC)); }
421 { $$ = newUNOP(OP_PREINC, 0,
422 mod(scalar($2), OP_PREINC)); }
424 { $$ = newUNOP(OP_PREDEC, 0,
425 mod(scalar($2), OP_PREDEC)); }
426 | local term %prec UNIOP
427 { $$ = localize($2,$1); }
429 { $$ = sawparens($2); }
431 { $$ = sawparens(newNULLLIST()); }
432 | '[' expr ']' %prec '('
433 { $$ = newANONLIST($2); }
435 { $$ = newANONLIST(Nullop); }
436 | HASHBRACK expr ';' '}' %prec '('
437 { $$ = newANONHASH($2); }
438 | HASHBRACK ';' '}' %prec '('
439 { $$ = newANONHASH(Nullop); }
440 | ANONSUB startanonsub proto block %prec '('
441 { $$ = newANONSUB($2, $3, $4); }
444 | star '{' expr ';' '}'
445 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
448 | scalar '[' expr ']' %prec '('
449 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
450 | term ARROW '[' expr ']' %prec '('
451 { $$ = newBINOP(OP_AELEM, 0,
452 ref(newAVREF($1),OP_RV2AV),
454 | term '[' expr ']' %prec '('
455 { assertref($1); $$ = newBINOP(OP_AELEM, 0,
456 ref(newAVREF($1),OP_RV2AV),
463 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
464 | scalar '{' expr ';' '}' %prec '('
465 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
466 expect = XOPERATOR; }
467 | term ARROW '{' expr ';' '}' %prec '('
468 { $$ = newBINOP(OP_HELEM, 0,
469 ref(newHVREF($1),OP_RV2HV),
471 expect = XOPERATOR; }
472 | term '{' expr ';' '}' %prec '('
473 { assertref($1); $$ = newBINOP(OP_HELEM, 0,
474 ref(newHVREF($1),OP_RV2HV),
476 expect = XOPERATOR; }
477 | '(' expr ')' '[' expr ']' %prec '('
478 { $$ = newSLICEOP(0, $5, $2); }
479 | '(' ')' '[' expr ']' %prec '('
480 { $$ = newSLICEOP(0, $4, Nullop); }
481 | ary '[' expr ']' %prec '('
482 { $$ = prepend_elem(OP_ASLICE,
483 newOP(OP_PUSHMARK, 0),
484 newLISTOP(OP_ASLICE, 0,
486 ref($1, OP_ASLICE))); }
487 | ary '{' expr ';' '}' %prec '('
488 { $$ = prepend_elem(OP_HSLICE,
489 newOP(OP_PUSHMARK, 0),
490 newLISTOP(OP_HSLICE, 0,
492 ref(oopsHV($1), OP_HSLICE)));
493 expect = XOPERATOR; }
497 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
499 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
501 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
502 append_elem(OP_LIST, $3, scalar($1))); }
503 | NOAMP WORD listexpr
504 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
505 append_elem(OP_LIST, $3, scalar($2))); }
506 | DO term %prec UNIOP
507 { $$ = newUNOP(OP_DOFILE, 0, scalar($2)); }
509 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
511 { $$ = newUNOP(OP_ENTERSUB,
512 OPf_SPECIAL|OPf_STACKED,
513 prepend_elem(OP_LIST,
515 (OPpENTERSUB_AMPER<<8),
518 | DO WORD '(' expr ')'
519 { $$ = newUNOP(OP_ENTERSUB,
520 OPf_SPECIAL|OPf_STACKED,
524 (OPpENTERSUB_AMPER<<8),
528 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
529 prepend_elem(OP_LIST,
530 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
531 | DO scalar '(' expr ')'
532 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
533 prepend_elem(OP_LIST,
535 scalar(newCVREF(0,scalar($2))))); dep();}
536 | term ARROW '(' ')' %prec '('
537 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
538 newCVREF(0, scalar($1))); }
539 | term ARROW '(' expr ')' %prec '('
540 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
541 append_elem(OP_LIST, $4,
542 newCVREF(0, scalar($1)))); }
544 { $$ = newOP($1, OPf_SPECIAL);
545 hints |= HINT_BLOCK_SCOPE; }
547 { $$ = newLOOPEX($1,$2); }
549 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
551 { $$ = newOP($1, 0); }
553 { $$ = newUNOP($1, 0, $2); }
555 { $$ = newUNOP($1, 0, $2); }
557 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
558 append_elem(OP_LIST, $2, scalar($1))); }
560 { $$ = newOP($1, 0); }
562 { $$ = newOP($1, 0); }
564 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
567 { $$ = newOP($1, OPf_SPECIAL); }
569 { $$ = newUNOP($1, 0, $3); }
570 | PMFUNC '(' term ')'
571 { $$ = pmruntime($1, $3, Nullop); }
572 | PMFUNC '(' term ',' term ')'
573 { $$ = pmruntime($1, $3, $5); }
584 listexprcom: /* NULL */
592 local : LOCAL { $$ = 0; }
597 { in_my = 0; $$ = my($1); }
601 { $$ = newCVREF($1,$2); }
605 { $$ = newSVREF($2); }
609 { $$ = newAVREF($2); }
613 { $$ = newHVREF($2); }
616 arylen : DOLSHARP indirob
617 { $$ = newAVREF($2); }
621 { $$ = newGVREF(0,$2); }