3 * Copyright (c) 1991-1994, 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 that 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
45 %token <ival> RELOP EQOP MULOP ADDOP
46 %token <ival> DOLSHARP DO LOCAL HASHBRACK NOAMP
48 %type <ival> prog decl format remember startsub '&'
49 %type <opval> block lineseq line loop cond nexpr else argexpr
50 %type <opval> expr term scalar ary hsh arylen star amper sideff
51 %type <opval> listexpr listexprcom indirob
52 %type <opval> texpr listop method proto
59 %nonassoc <ival> LSTOP
61 %right <ival> ASSIGNOP
70 %nonassoc <ival> UNIOP
75 %right '!' '~' UMINUS REFGEN
77 %nonassoc PREINC PREDEC POSTINC POSTDEC
85 #if defined(YYDEBUG) && defined(DEBUGGING)
86 yydebug = (debug & 1);
94 block : '{' remember lineseq '}'
95 { $$ = block_end($1,$2,$3); }
98 remember: /* NULL */ /* start a lexical scope */
99 { $$ = block_start(); }
107 { $$ = append_list(OP_LINESEQ,
108 (LISTOP*)$1, (LISTOP*)$2);
109 pad_reset_pending = TRUE;
110 if ($1 && $2) hints |= HINT_BLOCK_SCOPE; }
114 { $$ = newSTATEOP(0, $1, $2); }
115 | loop /* loops add their own labels */
117 { if ($1 != Nullch) {
118 $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0));
126 { $$ = newSTATEOP(0, $1, $2);
135 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
137 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
139 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
141 { $$ = newLOOPOP(OPf_PARENS, 1, invert(scalar($3)), $1);}
148 | ELSIF '(' expr ')' block else
150 $$ = newSTATEOP(0, 0,
151 newCONDOP(0, $3, scope($5), $6));
152 hints |= HINT_BLOCK_SCOPE; }
155 cond : IF '(' expr ')' block else
157 $$ = newCONDOP(0, $3, scope($5), $6); }
158 | UNLESS '(' expr ')' block else
161 invert(scalar($3)), scope($5), $6); }
162 | IF block block else
164 deprecate("if BLOCK BLOCK");
165 $$ = newCONDOP(0, scope($2), scope($3), $4); }
166 | UNLESS block block else
168 deprecate("unless BLOCK BLOCK");
169 $$ = newCONDOP(0, invert(scalar(scope($2))),
179 loop : label WHILE '(' texpr ')' block cont
181 $$ = newSTATEOP(0, $1,
182 newWHILEOP(0, 1, (LOOP*)Nullop,
184 | label UNTIL '(' expr ')' block cont
186 $$ = newSTATEOP(0, $1,
187 newWHILEOP(0, 1, (LOOP*)Nullop,
188 invert(scalar($4)), $6, $7) ); }
189 | label WHILE block block cont
191 $$ = newSTATEOP(0, $1,
192 newWHILEOP(0, 1, (LOOP*)Nullop,
193 scope($3), $4, $5) ); }
194 | label UNTIL block block cont
196 $$ = newSTATEOP(0, $1,
197 newWHILEOP(0, 1, (LOOP*)Nullop,
198 invert(scalar(scope($3))), $4, $5)); }
199 | label FOR scalar '(' expr ')' block cont
200 { $$ = newFOROP(0, $1, $2, mod($3, OP_ENTERLOOP),
202 | label FOR '(' expr ')' block cont
203 { $$ = newFOROP(0, $1, $2, Nullop, $4, $6, $7); }
204 | label FOR '(' nexpr ';' texpr ';' nexpr ')' block
205 /* basically fake up an initialize-while lineseq */
207 $$ = append_elem(OP_LINESEQ,
208 newSTATEOP(0, $1, scalar($4)),
210 newWHILEOP(0, 1, (LOOP*)Nullop,
211 scalar($6), $10, scalar($8)) )); }
212 | label block cont /* a block is a loop that happens once */
214 $1, newWHILEOP(0, 1, (LOOP*)Nullop,
223 texpr : /* NULL means true */
224 { (void)scan_num("1"); $$ = yylval.opval; }
243 format : FORMAT startsub WORD block
244 { newFORM($2, $3, $4); }
245 | FORMAT startsub block
246 { newFORM($2, Nullop, $3); }
249 subrout : SUB startsub WORD proto block
250 { newSUB($2, $3, $4, $5); }
251 | SUB startsub WORD proto ';'
252 { newSUB($2, $3, $4, Nullop); expect = XSTATE; }
260 startsub: /* NULL */ /* start a subroutine scope */
261 { $$ = start_subparse(); }
264 package : PACKAGE WORD ';'
270 use : USE startsub WORD WORD listexpr ';'
271 { utilize($1, $2, $3, $4, $5); }
274 expr : expr ANDOP expr
275 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
277 { $$ = newLOGOP($2, 0, $1, $3); }
281 argexpr : argexpr ','
284 { $$ = append_elem(OP_LIST, $1, $3); }
288 listop : LSTOP indirob argexpr
289 { $$ = convert($1, OPf_STACKED,
290 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
291 | FUNC '(' indirob expr ')'
292 { $$ = convert($1, OPf_STACKED,
293 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
294 | term ARROW method '(' listexprcom ')'
295 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
297 prepend_elem(OP_LIST, $1, $5),
298 newUNOP(OP_METHOD, 0, $3))); }
299 | METHOD indirob listexpr
300 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
302 prepend_elem(OP_LIST, $2, $3),
303 newUNOP(OP_METHOD, 0, $1))); }
304 | FUNCMETH indirob '(' listexprcom ')'
305 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
307 prepend_elem(OP_LIST, $2, $4),
308 newUNOP(OP_METHOD, 0, $1))); }
310 { $$ = convert($1, 0, $2); }
311 | FUNC '(' listexprcom ')'
312 { $$ = convert($1, 0, $3); }
313 | LSTOPSUB startsub block listexpr %prec LSTOP
314 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
316 prepend_elem(OP_LIST, newANONSUB($2, 0, $3), $4),
324 term : term ASSIGNOP term
325 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
327 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
329 { if ($2 != OP_REPEAT)
331 $$ = newBINOP($2, 0, $1, scalar($3)); }
333 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
335 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
337 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
339 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
341 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
343 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
345 { $$ = newRANGE($2, scalar($1), scalar($3));}
347 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
349 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
350 | term '?' term ':' term
351 { $$ = newCONDOP(0, $1, $3, $5); }
353 { $$ = bind_match($2, $1, $3); }
355 | '-' term %prec UMINUS
356 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
357 | '+' term %prec UMINUS
360 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
362 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
364 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
366 { $$ = newUNOP(OP_POSTINC, 0,
367 mod(scalar($1), OP_POSTINC)); }
369 { $$ = newUNOP(OP_POSTDEC, 0,
370 mod(scalar($1), OP_POSTDEC)); }
372 { $$ = newUNOP(OP_PREINC, 0,
373 mod(scalar($2), OP_PREINC)); }
375 { $$ = newUNOP(OP_PREDEC, 0,
376 mod(scalar($2), OP_PREDEC)); }
377 | LOCAL term %prec UNIOP
378 { $$ = localize($2,$1); }
380 { $$ = sawparens($2); }
382 { $$ = sawparens(newNULLLIST()); }
383 | '[' expr ']' %prec '('
384 { $$ = newANONLIST($2); }
386 { $$ = newANONLIST(Nullop); }
387 | HASHBRACK expr ';' '}' %prec '('
388 { $$ = newANONHASH($2); }
389 | HASHBRACK ';' '}' %prec '('
390 { $$ = newANONHASH(Nullop); }
391 | ANONSUB startsub proto block %prec '('
392 { $$ = newANONSUB($2, $3, $4); }
395 | star '{' expr ';' '}'
396 { $$ = newBINOP(OP_GELEM, 0, newGVREF(0,$1), $3); }
399 | scalar '[' expr ']' %prec '('
400 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
401 | term ARROW '[' expr ']' %prec '('
402 { $$ = newBINOP(OP_AELEM, 0,
403 ref(newAVREF($1),OP_RV2AV),
405 | term '[' expr ']' %prec '('
406 { assertref($1); $$ = newBINOP(OP_AELEM, 0,
407 ref(newAVREF($1),OP_RV2AV),
414 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
415 | scalar '{' expr ';' '}' %prec '('
416 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
417 expect = XOPERATOR; }
418 | term ARROW '{' expr ';' '}' %prec '('
419 { $$ = newBINOP(OP_HELEM, 0,
420 ref(newHVREF($1),OP_RV2HV),
422 expect = XOPERATOR; }
423 | term '{' expr ';' '}' %prec '('
424 { assertref($1); $$ = newBINOP(OP_HELEM, 0,
425 ref(newHVREF($1),OP_RV2HV),
427 expect = XOPERATOR; }
428 | '(' expr ')' '[' expr ']' %prec '('
429 { $$ = newSLICEOP(0, $5, $2); }
430 | '(' ')' '[' expr ']' %prec '('
431 { $$ = newSLICEOP(0, $4, Nullop); }
432 | ary '[' expr ']' %prec '('
433 { $$ = prepend_elem(OP_ASLICE,
434 newOP(OP_PUSHMARK, 0),
435 newLISTOP(OP_ASLICE, 0,
437 ref($1, OP_ASLICE))); }
438 | ary '{' expr ';' '}' %prec '('
439 { $$ = prepend_elem(OP_HSLICE,
440 newOP(OP_PUSHMARK, 0),
441 newLISTOP(OP_HSLICE, 0,
443 ref(oopsHV($1), OP_HSLICE)));
444 expect = XOPERATOR; }
448 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
450 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
452 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
453 append_elem(OP_LIST, $3, scalar($1))); }
454 | NOAMP WORD listexpr
455 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
456 append_elem(OP_LIST, $3, scalar($2))); }
457 | DO term %prec UNIOP
458 { $$ = newUNOP(OP_DOFILE, 0, scalar($2)); }
460 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
462 { $$ = newUNOP(OP_ENTERSUB,
463 OPf_SPECIAL|OPf_STACKED,
464 prepend_elem(OP_LIST,
466 (OPpENTERSUB_AMPER<<8),
469 | DO WORD '(' expr ')'
470 { $$ = newUNOP(OP_ENTERSUB,
471 OPf_SPECIAL|OPf_STACKED,
475 (OPpENTERSUB_AMPER<<8),
479 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
480 prepend_elem(OP_LIST,
481 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
482 | DO scalar '(' expr ')'
483 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
484 prepend_elem(OP_LIST,
486 scalar(newCVREF(0,scalar($2))))); dep();}
488 { $$ = newOP($1, OPf_SPECIAL);
489 hints |= HINT_BLOCK_SCOPE; }
491 { $$ = newLOOPEX($1,$2); }
493 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
495 { $$ = newOP($1, 0); }
497 { $$ = newUNOP($1, 0, $2); }
499 { $$ = newUNOP($1, 0, $2); }
501 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
502 append_elem(OP_LIST, $2, scalar($1))); }
504 { $$ = newOP($1, 0); }
506 { $$ = newOP($1, 0); }
508 { $$ = newUNOP(OP_ENTERSUB, 0,
511 { $$ = newOP($1, OPf_SPECIAL); }
513 { $$ = newUNOP($1, 0, $3); }
514 | PMFUNC '(' term ')'
515 { $$ = pmruntime($1, $3, Nullop); }
516 | PMFUNC '(' term ',' term ')'
517 { $$ = pmruntime($1, $3, $5); }
528 listexprcom: /* NULL */
537 { $$ = newCVREF($1,$2); }
541 { $$ = newSVREF($2); }
545 { $$ = newAVREF($2); }
549 { $$ = newHVREF($2); }
552 arylen : DOLSHARP indirob
553 { $$ = newAVREF($2); }
557 { $$ = newGVREF(0,$2); }