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");
30 /* I sense a Big Blue pattern here... */
31 #if !defined(OEMVS) && !defined(__OPEN_VM) && !defined(POSIX_BC)
42 #endif /* !OEMVS && !__OPEN_VM && !POSIX_BC */
45 #define YYLEX_PARAM (&yychar)
51 %token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
52 %token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
54 %token <ival> FORMAT SUB ANONSUB PACKAGE USE
55 %token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
56 %token <ival> LOOPEX DOTDOT
57 %token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP
58 %token <ival> RELOP EQOP MULOP ADDOP
59 %token <ival> DOLSHARP DO HASHBRACK NOAMP
62 %type <ival> prog decl local format startsub startanonsub startformsub
63 %type <ival> remember mremember '&'
64 %type <opval> block mblock lineseq line loop cond else
65 %type <opval> expr term scalar ary hsh arylen star amper sideff
66 %type <opval> argexpr nexpr texpr iexpr mexpr mnexpr mtexpr miexpr
67 %type <opval> listexpr listexprcom indirob listop method
68 %type <opval> formname subname proto subbody cont my_scalar
74 %nonassoc LSTOP LSTOPSUB
76 %right <ival> ASSIGNOP
85 %nonassoc UNIOP UNIOPSUB
90 %right '!' '~' UMINUS REFGEN
92 %nonassoc PREINC PREDEC POSTINC POSTDEC
100 #if defined(YYDEBUG) && defined(DEBUGGING)
101 yydebug = (PL_debug & 1);
105 /*CONTINUED*/ lineseq
109 block : '{' remember lineseq '}'
110 { if (PL_copline > (line_t)$1)
112 $$ = block_end($2, $3); }
115 remember: /* NULL */ /* start a full lexical scope */
116 { $$ = block_start(TRUE); }
119 mblock : '{' mremember lineseq '}'
120 { if (PL_copline > (line_t)$1)
122 $$ = block_end($2, $3); }
125 mremember: /* NULL */ /* start a partial lexical scope */
126 { $$ = block_start(FALSE); }
134 { $$ = append_list(OP_LINESEQ,
135 (LISTOP*)$1, (LISTOP*)$2);
136 PL_pad_reset_pending = TRUE;
137 if ($1 && $2) PL_hints |= HINT_BLOCK_SCOPE; }
141 { $$ = newSTATEOP(0, $1, $2); }
142 | loop /* loops add their own labels */
144 { if ($1 != Nullch) {
145 $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0));
151 PL_expect = XSTATE; }
153 { $$ = newSTATEOP(0, $1, $2);
154 PL_expect = XSTATE; }
162 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
164 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
166 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
168 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);}
170 { $$ = newFOROP(0, Nullch, $2,
171 Nullop, $3, $1, Nullop); }
178 | ELSIF '(' mexpr ')' mblock else
180 $$ = newSTATEOP(0, Nullch,
181 newCONDOP(0, $3, scope($5), $6));
182 PL_hints |= HINT_BLOCK_SCOPE; }
185 cond : IF '(' remember mexpr ')' mblock else
188 newCONDOP(0, $4, scope($6), $7)); }
189 | UNLESS '(' remember miexpr ')' mblock else
192 newCONDOP(0, $4, scope($6), $7)); }
201 loop : label WHILE '(' remember mtexpr ')' mblock cont
205 newWHILEOP(0, 1, (LOOP*)Nullop,
207 | label UNTIL '(' remember miexpr ')' mblock cont
211 newWHILEOP(0, 1, (LOOP*)Nullop,
213 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
215 newFOROP(0, $1, $2, $5, $7, $9, $10)); }
216 | label FOR scalar '(' remember mexpr ')' mblock cont
218 newFOROP(0, $1, $2, mod($3, OP_ENTERLOOP),
220 | label FOR '(' remember mexpr ')' mblock cont
222 newFOROP(0, $1, $2, Nullop, $5, $7, $8)); }
223 | label FOR '(' remember mnexpr ';' mtexpr ';' mnexpr ')' mblock
224 /* basically fake up an initialize-while lineseq */
225 { OP *forop = append_elem(OP_LINESEQ,
227 newWHILEOP(0, 1, (LOOP*)Nullop,
231 $$ = block_end($4, newSTATEOP(0, $1, forop)); }
232 | label block cont /* a block is a loop that happens once */
233 { $$ = newSTATEOP(0, $1,
234 newWHILEOP(0, 1, (LOOP*)Nullop,
235 NOLINE, Nullop, $2, $3)); }
243 texpr : /* NULL means true */
244 { (void)scan_num("1"); $$ = yylval.opval; }
249 { $$ = invert(scalar($1)); }
253 { $$ = $1; intro_my(); }
257 { $$ = $1; intro_my(); }
261 { $$ = $1; intro_my(); }
265 { $$ = $1; intro_my(); }
283 format : FORMAT startformsub formname block
284 { newFORM($2, $3, $4); }
287 formname: WORD { $$ = $1; }
288 | /* NULL */ { $$ = Nullop; }
291 subrout : SUB startsub subname proto subbody
292 { newSUB($2, $3, $4, $5); }
295 startsub: /* NULL */ /* start a regular subroutine scope */
296 { $$ = start_subparse(FALSE, 0); }
299 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
300 { $$ = start_subparse(FALSE, CVf_ANON); }
303 startformsub: /* NULL */ /* start a format subroutine scope */
304 { $$ = start_subparse(TRUE, 0); }
307 subname : WORD { STRLEN n_a; char *name = SvPV(((SVOP*)$1)->op_sv,n_a);
308 if (strEQ(name, "BEGIN") || strEQ(name, "END")
309 || strEQ(name, "INIT"))
310 CvUNIQUE_on(PL_compcv);
319 subbody : block { $$ = $1; }
320 | ';' { $$ = Nullop; PL_expect = XSTATE; }
323 package : PACKAGE WORD ';'
330 { CvUNIQUE_on(PL_compcv); /* It's a BEGIN {} */ }
331 WORD WORD listexpr ';'
332 { utilize($1, $2, $4, $5, $6); }
335 expr : expr ANDOP expr
336 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
338 { $$ = newLOGOP($2, 0, $1, $3); }
342 argexpr : argexpr ','
345 { $$ = append_elem(OP_LIST, $1, $3); }
349 listop : LSTOP indirob argexpr
350 { $$ = convert($1, OPf_STACKED,
351 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
352 | FUNC '(' indirob expr ')'
353 { $$ = convert($1, OPf_STACKED,
354 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
355 | term ARROW method '(' listexprcom ')'
356 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
358 prepend_elem(OP_LIST, scalar($1), $5),
359 newUNOP(OP_METHOD, 0, $3))); }
361 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
362 append_elem(OP_LIST, scalar($1),
363 newUNOP(OP_METHOD, 0, $3))); }
364 | METHOD indirob listexpr
365 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
367 prepend_elem(OP_LIST, $2, $3),
368 newUNOP(OP_METHOD, 0, $1))); }
369 | FUNCMETH indirob '(' listexprcom ')'
370 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
372 prepend_elem(OP_LIST, $2, $4),
373 newUNOP(OP_METHOD, 0, $1))); }
375 { $$ = convert($1, 0, $2); }
376 | FUNC '(' listexprcom ')'
377 { $$ = convert($1, 0, $3); }
378 | LSTOPSUB startanonsub block
379 { $3 = newANONSUB($2, 0, $3); }
381 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
383 prepend_elem(OP_LIST, $3, $5), $1)); }
390 term : term ASSIGNOP term
391 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
393 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
395 { if ($2 != OP_REPEAT)
397 $$ = newBINOP($2, 0, $1, scalar($3)); }
399 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
401 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
403 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
405 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
407 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
409 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
411 { $$ = newRANGE($2, scalar($1), scalar($3));}
413 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
415 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
416 | term '?' term ':' term
417 { $$ = newCONDOP(0, $1, $3, $5); }
419 { $$ = bind_match($2, $1, $3); }
421 | '-' term %prec UMINUS
422 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
423 | '+' term %prec UMINUS
426 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
428 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
430 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
432 { $$ = newUNOP(OP_POSTINC, 0,
433 mod(scalar($1), OP_POSTINC)); }
435 { $$ = newUNOP(OP_POSTDEC, 0,
436 mod(scalar($1), OP_POSTDEC)); }
438 { $$ = newUNOP(OP_PREINC, 0,
439 mod(scalar($2), OP_PREINC)); }
441 { $$ = newUNOP(OP_PREDEC, 0,
442 mod(scalar($2), OP_PREDEC)); }
443 | local term %prec UNIOP
444 { $$ = localize($2,$1); }
446 { $$ = sawparens($2); }
448 { $$ = sawparens(newNULLLIST()); }
449 | '[' expr ']' %prec '('
450 { $$ = newANONLIST($2); }
452 { $$ = newANONLIST(Nullop); }
453 | HASHBRACK expr ';' '}' %prec '('
454 { $$ = newANONHASH($2); }
455 | HASHBRACK ';' '}' %prec '('
456 { $$ = newANONHASH(Nullop); }
457 | ANONSUB startanonsub proto block %prec '('
458 { $$ = newANONSUB($2, $3, $4); }
461 | star '{' expr ';' '}'
462 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
465 | scalar '[' expr ']' %prec '('
466 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
467 | term ARROW '[' expr ']' %prec '('
468 { $$ = newBINOP(OP_AELEM, 0,
469 ref(newAVREF($1),OP_RV2AV),
471 | term '[' expr ']' %prec '('
472 { assertref($1); $$ = newBINOP(OP_AELEM, 0,
473 ref(newAVREF($1),OP_RV2AV),
480 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
481 | scalar '{' expr ';' '}' %prec '('
482 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
483 PL_expect = XOPERATOR; }
484 | term ARROW '{' expr ';' '}' %prec '('
485 { $$ = newBINOP(OP_HELEM, 0,
486 ref(newHVREF($1),OP_RV2HV),
488 PL_expect = XOPERATOR; }
489 | term '{' expr ';' '}' %prec '('
490 { assertref($1); $$ = newBINOP(OP_HELEM, 0,
491 ref(newHVREF($1),OP_RV2HV),
493 PL_expect = XOPERATOR; }
494 | '(' expr ')' '[' expr ']' %prec '('
495 { $$ = newSLICEOP(0, $5, $2); }
496 | '(' ')' '[' expr ']' %prec '('
497 { $$ = newSLICEOP(0, $4, Nullop); }
498 | ary '[' expr ']' %prec '('
499 { $$ = prepend_elem(OP_ASLICE,
500 newOP(OP_PUSHMARK, 0),
501 newLISTOP(OP_ASLICE, 0,
503 ref($1, OP_ASLICE))); }
504 | ary '{' expr ';' '}' %prec '('
505 { $$ = prepend_elem(OP_HSLICE,
506 newOP(OP_PUSHMARK, 0),
507 newLISTOP(OP_HSLICE, 0,
509 ref(oopsHV($1), OP_HSLICE)));
510 PL_expect = XOPERATOR; }
514 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
516 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
518 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
519 append_elem(OP_LIST, $3, scalar($1))); }
520 | NOAMP WORD listexpr
521 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
522 append_elem(OP_LIST, $3, scalar($2))); }
523 | DO term %prec UNIOP
526 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
528 { $$ = newUNOP(OP_ENTERSUB,
529 OPf_SPECIAL|OPf_STACKED,
530 prepend_elem(OP_LIST,
532 (OPpENTERSUB_AMPER<<8),
535 | DO WORD '(' expr ')'
536 { $$ = newUNOP(OP_ENTERSUB,
537 OPf_SPECIAL|OPf_STACKED,
541 (OPpENTERSUB_AMPER<<8),
545 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
546 prepend_elem(OP_LIST,
547 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
548 | DO scalar '(' expr ')'
549 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
550 prepend_elem(OP_LIST,
552 scalar(newCVREF(0,scalar($2))))); dep();}
553 | term ARROW '(' ')' %prec '('
554 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
555 newCVREF(0, scalar($1))); }
556 | term ARROW '(' expr ')' %prec '('
557 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
558 append_elem(OP_LIST, $4,
559 newCVREF(0, scalar($1)))); }
561 { $$ = newOP($1, OPf_SPECIAL);
562 PL_hints |= HINT_BLOCK_SCOPE; }
564 { $$ = newLOOPEX($1,$2); }
566 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
568 { $$ = newOP($1, 0); }
570 { $$ = newUNOP($1, 0, $2); }
572 { $$ = newUNOP($1, 0, $2); }
574 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
575 append_elem(OP_LIST, $2, scalar($1))); }
577 { $$ = newOP($1, 0); }
579 { $$ = newOP($1, 0); }
581 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
584 { $$ = newOP($1, OPf_SPECIAL); }
586 { $$ = newUNOP($1, 0, $3); }
587 | PMFUNC '(' term ')'
588 { $$ = pmruntime($1, $3, Nullop); }
589 | PMFUNC '(' term ',' term ')'
590 { $$ = pmruntime($1, $3, $5); }
601 listexprcom: /* NULL */
609 local : LOCAL { $$ = 0; }
614 { PL_in_my = 0; $$ = my($1); }
618 { $$ = newCVREF($1,$2); }
622 { $$ = newSVREF($2); }
626 { $$ = newAVREF($2); }
630 { $$ = newHVREF($2); }
633 arylen : DOLSHARP indirob
634 { $$ = newAVREF($2); }
638 { $$ = newGVREF(0,$2); }