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.'
17 #define PERL_IN_PERLY_C
20 #define dep() deprecate("\"do\" to call subroutines")
27 /* I sense a Big Blue pattern here... */
28 #if !defined(OEMVS) && !defined(__OPEN_VM) && !defined(POSIX_BC)
39 #endif /* !OEMVS && !__OPEN_VM && !POSIX_BC */
42 #define YYLEX_PARAM (&yychar)
48 %token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
49 %token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
51 %token <ival> FORMAT SUB ANONSUB PACKAGE USE
52 %token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
53 %token <ival> LOOPEX DOTDOT
54 %token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP
55 %token <ival> RELOP EQOP MULOP ADDOP
56 %token <ival> DOLSHARP DO HASHBRACK NOAMP
59 %type <ival> prog decl local format startsub startanonsub startformsub
60 %type <ival> remember mremember '&'
61 %type <opval> block mblock lineseq line loop cond else
62 %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
63 %type <opval> argexpr nexpr texpr iexpr mexpr mnexpr mtexpr miexpr
64 %type <opval> listexpr listexprcom indirob listop method
65 %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
102 #if defined(YYDEBUG) && defined(DEBUGGING)
103 yydebug = (PL_debug & 1);
107 /*CONTINUED*/ lineseq
111 block : '{' remember lineseq '}'
112 { if (PL_copline > (line_t)$1)
114 $$ = block_end($2, $3); }
117 remember: /* NULL */ /* start a full lexical scope */
118 { $$ = block_start(TRUE); }
121 mblock : '{' mremember lineseq '}'
122 { if (PL_copline > (line_t)$1)
124 $$ = block_end($2, $3); }
127 mremember: /* NULL */ /* start a partial lexical scope */
128 { $$ = block_start(FALSE); }
136 { $$ = append_list(OP_LINESEQ,
137 (LISTOP*)$1, (LISTOP*)$2);
138 PL_pad_reset_pending = TRUE;
139 if ($1 && $2) PL_hints |= HINT_BLOCK_SCOPE; }
143 { $$ = newSTATEOP(0, $1, $2); }
144 | loop /* loops add their own labels */
146 { if ($1 != Nullch) {
147 $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0));
153 PL_expect = XSTATE; }
155 { $$ = newSTATEOP(0, $1, $2);
156 PL_expect = XSTATE; }
164 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
166 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
168 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
170 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);}
172 { $$ = newFOROP(0, Nullch, $2,
173 Nullop, $3, $1, Nullop); }
180 | ELSIF '(' mexpr ')' mblock else
182 $$ = newCONDOP(0, $3, scope($5), $6);
183 PL_hints |= HINT_BLOCK_SCOPE; }
186 cond : IF '(' remember mexpr ')' mblock else
189 newCONDOP(0, $4, scope($6), $7)); }
190 | UNLESS '(' remember miexpr ')' mblock else
193 newCONDOP(0, $4, scope($6), $7)); }
202 loop : label WHILE '(' remember mtexpr ')' mblock cont
206 newWHILEOP(0, 1, (LOOP*)Nullop,
208 | label UNTIL '(' remember miexpr ')' mblock cont
212 newWHILEOP(0, 1, (LOOP*)Nullop,
214 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
216 newFOROP(0, $1, $2, $5, $7, $9, $10)); }
217 | label FOR scalar '(' remember mexpr ')' mblock cont
219 newFOROP(0, $1, $2, mod($3, OP_ENTERLOOP),
221 | label FOR '(' remember mexpr ')' mblock cont
223 newFOROP(0, $1, $2, Nullop, $5, $7, $8)); }
224 | label FOR '(' remember mnexpr ';' mtexpr ';' mnexpr ')' mblock
225 /* basically fake up an initialize-while lineseq */
226 { OP *forop = append_elem(OP_LINESEQ,
228 newWHILEOP(0, 1, (LOOP*)Nullop,
232 $$ = block_end($4, newSTATEOP(0, $1, forop)); }
233 | label block cont /* a block is a loop that happens once */
234 { $$ = newSTATEOP(0, $1,
235 newWHILEOP(0, 1, (LOOP*)Nullop,
236 NOLINE, Nullop, $2, $3)); }
244 texpr : /* NULL means true */
245 { (void)scan_num("1"); $$ = yylval.opval; }
250 { $$ = invert(scalar($1)); }
254 { $$ = $1; intro_my(); }
258 { $$ = $1; intro_my(); }
262 { $$ = $1; intro_my(); }
266 { $$ = $1; intro_my(); }
284 format : FORMAT startformsub formname block
285 { newFORM($2, $3, $4); }
288 formname: WORD { $$ = $1; }
289 | /* NULL */ { $$ = Nullop; }
292 subrout : SUB startsub subname proto subbody
293 { newSUB($2, $3, $4, $5); }
296 startsub: /* NULL */ /* start a regular subroutine scope */
297 { $$ = start_subparse(FALSE, 0); }
300 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
301 { $$ = start_subparse(FALSE, CVf_ANON); }
304 startformsub: /* NULL */ /* start a format subroutine scope */
305 { $$ = start_subparse(TRUE, 0); }
308 subname : WORD { STRLEN n_a; char *name = SvPV(((SVOP*)$1)->op_sv,n_a);
309 if (strEQ(name, "BEGIN") || strEQ(name, "END")
310 || strEQ(name, "INIT"))
311 CvSPECIAL_on(PL_compcv);
320 subbody : block { $$ = $1; }
321 | ';' { $$ = Nullop; PL_expect = XSTATE; }
324 package : PACKAGE WORD ';'
331 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
332 WORD WORD listexpr ';'
333 { utilize($1, $2, $4, $5, $6); }
336 expr : expr ANDOP expr
337 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
339 { $$ = newLOGOP($2, 0, $1, $3); }
340 | argexpr %prec PREC_LOW
343 argexpr : argexpr ','
346 { $$ = append_elem(OP_LIST, $1, $3); }
347 | term %prec PREC_LOW
350 listop : LSTOP indirob argexpr
351 { $$ = convert($1, OPf_STACKED,
352 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
353 | FUNC '(' indirob expr ')'
354 { $$ = convert($1, OPf_STACKED,
355 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
356 | term ARROW method '(' listexprcom ')'
357 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
359 prepend_elem(OP_LIST, scalar($1), $5),
360 newUNOP(OP_METHOD, 0, $3))); }
362 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
363 append_elem(OP_LIST, scalar($1),
364 newUNOP(OP_METHOD, 0, $3))); }
365 | METHOD indirob listexpr
366 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
368 prepend_elem(OP_LIST, $2, $3),
369 newUNOP(OP_METHOD, 0, $1))); }
370 | FUNCMETH indirob '(' listexprcom ')'
371 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
373 prepend_elem(OP_LIST, $2, $4),
374 newUNOP(OP_METHOD, 0, $1))); }
376 { $$ = convert($1, 0, $2); }
377 | FUNC '(' listexprcom ')'
378 { $$ = convert($1, 0, $3); }
379 | LSTOPSUB startanonsub block
380 { $3 = newANONSUB($2, 0, $3); }
382 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
384 prepend_elem(OP_LIST, $3, $5), $1)); }
391 subscripted: star '{' expr ';' '}'
392 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
393 | scalar '[' expr ']'
394 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
395 | term ARROW '[' expr ']'
396 { $$ = newBINOP(OP_AELEM, 0,
397 ref(newAVREF($1),OP_RV2AV),
399 | subscripted '[' expr ']'
400 { $$ = newBINOP(OP_AELEM, 0,
401 ref(newAVREF($1),OP_RV2AV),
403 | scalar '{' expr ';' '}'
404 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
405 PL_expect = XOPERATOR; }
406 | term ARROW '{' expr ';' '}'
407 { $$ = newBINOP(OP_HELEM, 0,
408 ref(newHVREF($1),OP_RV2HV),
410 PL_expect = XOPERATOR; }
411 | subscripted '{' expr ';' '}'
412 { $$ = newBINOP(OP_HELEM, 0,
413 ref(newHVREF($1),OP_RV2HV),
415 PL_expect = XOPERATOR; }
417 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
418 newCVREF(0, scalar($1))); }
419 | term ARROW '(' expr ')'
420 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
421 append_elem(OP_LIST, $4,
422 newCVREF(0, scalar($1)))); }
424 | subscripted '(' expr ')'
425 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
426 append_elem(OP_LIST, $3,
427 newCVREF(0, scalar($1)))); }
428 | subscripted '(' ')'
429 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
430 newCVREF(0, scalar($1))); }
434 term : term ASSIGNOP term
435 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
437 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
439 { if ($2 != OP_REPEAT)
441 $$ = newBINOP($2, 0, $1, scalar($3)); }
443 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
445 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
447 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
449 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
451 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
453 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
455 { $$ = newRANGE($2, scalar($1), scalar($3));}
457 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
459 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
460 | term '?' term ':' term
461 { $$ = newCONDOP(0, $1, $3, $5); }
463 { $$ = bind_match($2, $1, $3); }
465 | '-' term %prec UMINUS
466 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
467 | '+' term %prec UMINUS
470 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
472 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
474 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
476 { $$ = newUNOP(OP_POSTINC, 0,
477 mod(scalar($1), OP_POSTINC)); }
479 { $$ = newUNOP(OP_POSTDEC, 0,
480 mod(scalar($1), OP_POSTDEC)); }
482 { $$ = newUNOP(OP_PREINC, 0,
483 mod(scalar($2), OP_PREINC)); }
485 { $$ = newUNOP(OP_PREDEC, 0,
486 mod(scalar($2), OP_PREDEC)); }
487 | local term %prec UNIOP
488 { $$ = localize($2,$1); }
490 { $$ = sawparens($2); }
492 { $$ = sawparens(newNULLLIST()); }
494 { $$ = newANONLIST($2); }
496 { $$ = newANONLIST(Nullop); }
497 | HASHBRACK expr ';' '}' %prec '('
498 { $$ = newANONHASH($2); }
499 | HASHBRACK ';' '}' %prec '('
500 { $$ = newANONHASH(Nullop); }
501 | ANONSUB startanonsub proto block %prec '('
502 { $$ = newANONSUB($2, $3, $4); }
512 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
515 | '(' expr ')' '[' expr ']'
516 { $$ = newSLICEOP(0, $5, $2); }
517 | '(' ')' '[' expr ']'
518 { $$ = newSLICEOP(0, $4, Nullop); }
520 { $$ = prepend_elem(OP_ASLICE,
521 newOP(OP_PUSHMARK, 0),
522 newLISTOP(OP_ASLICE, 0,
524 ref($1, OP_ASLICE))); }
525 | ary '{' expr ';' '}'
526 { $$ = prepend_elem(OP_HSLICE,
527 newOP(OP_PUSHMARK, 0),
528 newLISTOP(OP_HSLICE, 0,
530 ref(oopsHV($1), OP_HSLICE)));
531 PL_expect = XOPERATOR; }
535 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
537 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
539 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
540 append_elem(OP_LIST, $3, scalar($1))); }
541 | NOAMP WORD listexpr
542 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
543 append_elem(OP_LIST, $3, scalar($2))); }
544 | DO term %prec UNIOP
547 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
549 { $$ = newUNOP(OP_ENTERSUB,
550 OPf_SPECIAL|OPf_STACKED,
551 prepend_elem(OP_LIST,
553 (OPpENTERSUB_AMPER<<8),
556 | DO WORD '(' expr ')'
557 { $$ = newUNOP(OP_ENTERSUB,
558 OPf_SPECIAL|OPf_STACKED,
562 (OPpENTERSUB_AMPER<<8),
566 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
567 prepend_elem(OP_LIST,
568 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
569 | DO scalar '(' expr ')'
570 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
571 prepend_elem(OP_LIST,
573 scalar(newCVREF(0,scalar($2))))); dep();}
575 { $$ = newOP($1, OPf_SPECIAL);
576 PL_hints |= HINT_BLOCK_SCOPE; }
578 { $$ = newLOOPEX($1,$2); }
580 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
582 { $$ = newOP($1, 0); }
584 { $$ = newUNOP($1, 0, $2); }
586 { $$ = newUNOP($1, 0, $2); }
588 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
589 append_elem(OP_LIST, $2, scalar($1))); }
591 { $$ = newOP($1, 0); }
593 { $$ = newOP($1, 0); }
595 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
598 { $$ = newOP($1, OPf_SPECIAL); }
600 { $$ = newUNOP($1, 0, $3); }
601 | PMFUNC '(' term ')'
602 { $$ = pmruntime($1, $3, Nullop); }
603 | PMFUNC '(' term ',' term ')'
604 { $$ = pmruntime($1, $3, $5); }
609 listexpr: /* NULL */ %prec PREC_LOW
611 | argexpr %prec PREC_LOW
615 listexprcom: /* NULL */
623 local : LOCAL { $$ = 0; }
628 { PL_in_my = 0; $$ = my($1); }
632 { $$ = newCVREF($1,$2); }
636 { $$ = newSVREF($2); }
640 { $$ = newAVREF($2); }
644 { $$ = newHVREF($2); }
647 arylen : DOLSHARP indirob
648 { $$ = newAVREF($2); }
652 { $$ = newGVREF(0,$2); }
657 | scalar %prec PREC_LOW