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))); }
360 | METHOD indirob listexpr
361 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
363 prepend_elem(OP_LIST, $2, $3),
364 newUNOP(OP_METHOD, 0, $1))); }
365 | FUNCMETH indirob '(' listexprcom ')'
366 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
368 prepend_elem(OP_LIST, $2, $4),
369 newUNOP(OP_METHOD, 0, $1))); }
371 { $$ = convert($1, 0, $2); }
372 | FUNC '(' listexprcom ')'
373 { $$ = convert($1, 0, $3); }
374 | LSTOPSUB startanonsub block
375 { $3 = newANONSUB($2, 0, $3); }
377 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
379 prepend_elem(OP_LIST, $3, $5), $1)); }
386 term : term ASSIGNOP term
387 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
389 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
391 { if ($2 != OP_REPEAT)
393 $$ = newBINOP($2, 0, $1, scalar($3)); }
395 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
397 { $$ = newBINOP($2, 0, scalar($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 { $$ = newRANGE($2, scalar($1), scalar($3));}
409 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
411 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
412 | term '?' term ':' term
413 { $$ = newCONDOP(0, $1, $3, $5); }
415 { $$ = bind_match($2, $1, $3); }
417 | '-' term %prec UMINUS
418 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
419 | '+' term %prec UMINUS
422 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
424 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
426 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
428 { $$ = newUNOP(OP_POSTINC, 0,
429 mod(scalar($1), OP_POSTINC)); }
431 { $$ = newUNOP(OP_POSTDEC, 0,
432 mod(scalar($1), OP_POSTDEC)); }
434 { $$ = newUNOP(OP_PREINC, 0,
435 mod(scalar($2), OP_PREINC)); }
437 { $$ = newUNOP(OP_PREDEC, 0,
438 mod(scalar($2), OP_PREDEC)); }
439 | local term %prec UNIOP
440 { $$ = localize($2,$1); }
442 { $$ = sawparens($2); }
444 { $$ = sawparens(newNULLLIST()); }
445 | '[' expr ']' %prec '('
446 { $$ = newANONLIST($2); }
448 { $$ = newANONLIST(Nullop); }
449 | HASHBRACK expr ';' '}' %prec '('
450 { $$ = newANONHASH($2); }
451 | HASHBRACK ';' '}' %prec '('
452 { $$ = newANONHASH(Nullop); }
453 | ANONSUB startanonsub proto block %prec '('
454 { $$ = newANONSUB($2, $3, $4); }
457 | star '{' expr ';' '}'
458 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
461 | scalar '[' expr ']' %prec '('
462 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
463 | term ARROW '[' expr ']' %prec '('
464 { $$ = newBINOP(OP_AELEM, 0,
465 ref(newAVREF($1),OP_RV2AV),
467 | term '[' expr ']' %prec '('
468 { assertref($1); $$ = newBINOP(OP_AELEM, 0,
469 ref(newAVREF($1),OP_RV2AV),
476 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
477 | scalar '{' expr ';' '}' %prec '('
478 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
479 PL_expect = XOPERATOR; }
480 | term ARROW '{' expr ';' '}' %prec '('
481 { $$ = newBINOP(OP_HELEM, 0,
482 ref(newHVREF($1),OP_RV2HV),
484 PL_expect = XOPERATOR; }
485 | term '{' expr ';' '}' %prec '('
486 { assertref($1); $$ = newBINOP(OP_HELEM, 0,
487 ref(newHVREF($1),OP_RV2HV),
489 PL_expect = XOPERATOR; }
490 | '(' expr ')' '[' expr ']' %prec '('
491 { $$ = newSLICEOP(0, $5, $2); }
492 | '(' ')' '[' expr ']' %prec '('
493 { $$ = newSLICEOP(0, $4, Nullop); }
494 | ary '[' expr ']' %prec '('
495 { $$ = prepend_elem(OP_ASLICE,
496 newOP(OP_PUSHMARK, 0),
497 newLISTOP(OP_ASLICE, 0,
499 ref($1, OP_ASLICE))); }
500 | ary '{' expr ';' '}' %prec '('
501 { $$ = prepend_elem(OP_HSLICE,
502 newOP(OP_PUSHMARK, 0),
503 newLISTOP(OP_HSLICE, 0,
505 ref(oopsHV($1), OP_HSLICE)));
506 PL_expect = XOPERATOR; }
510 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
512 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
514 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
515 append_elem(OP_LIST, $3, scalar($1))); }
516 | NOAMP WORD listexpr
517 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
518 append_elem(OP_LIST, $3, scalar($2))); }
519 | DO term %prec UNIOP
522 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
524 { $$ = newUNOP(OP_ENTERSUB,
525 OPf_SPECIAL|OPf_STACKED,
526 prepend_elem(OP_LIST,
528 (OPpENTERSUB_AMPER<<8),
531 | DO WORD '(' expr ')'
532 { $$ = newUNOP(OP_ENTERSUB,
533 OPf_SPECIAL|OPf_STACKED,
537 (OPpENTERSUB_AMPER<<8),
541 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
542 prepend_elem(OP_LIST,
543 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
544 | DO scalar '(' expr ')'
545 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
546 prepend_elem(OP_LIST,
548 scalar(newCVREF(0,scalar($2))))); dep();}
549 | term ARROW '(' ')' %prec '('
550 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
551 newCVREF(0, scalar($1))); }
552 | term ARROW '(' expr ')' %prec '('
553 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
554 append_elem(OP_LIST, $4,
555 newCVREF(0, scalar($1)))); }
557 { $$ = newOP($1, OPf_SPECIAL);
558 PL_hints |= HINT_BLOCK_SCOPE; }
560 { $$ = newLOOPEX($1,$2); }
562 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
564 { $$ = newOP($1, 0); }
566 { $$ = newUNOP($1, 0, $2); }
568 { $$ = newUNOP($1, 0, $2); }
570 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
571 append_elem(OP_LIST, $2, scalar($1))); }
573 { $$ = newOP($1, 0); }
575 { $$ = newOP($1, 0); }
577 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
580 { $$ = newOP($1, OPf_SPECIAL); }
582 { $$ = newUNOP($1, 0, $3); }
583 | PMFUNC '(' term ')'
584 { $$ = pmruntime($1, $3, Nullop); }
585 | PMFUNC '(' term ',' term ')'
586 { $$ = pmruntime($1, $3, $5); }
597 listexprcom: /* NULL */
605 local : LOCAL { $$ = 0; }
610 { PL_in_my = 0; $$ = my($1); }
614 { $$ = newCVREF($1,$2); }
618 { $$ = newSVREF($2); }
622 { $$ = newAVREF($2); }
626 { $$ = newHVREF($2); }
629 arylen : DOLSHARP indirob
630 { $$ = newAVREF($2); }
634 { $$ = newGVREF(0,$2); }