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 WORD listexpr ';'
271 { utilize($1, $2, $3); }
274 expr : expr ANDOP expr
275 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
277 { $$ = newLOGOP($2, 0, $1, $3); }
279 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
283 argexpr : argexpr ','
286 { $$ = append_elem(OP_LIST, $1, $3); }
290 listop : LSTOP indirob argexpr
291 { $$ = convert($1, OPf_STACKED,
292 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
293 | FUNC '(' indirob expr ')'
294 { $$ = convert($1, OPf_STACKED,
295 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
296 | term ARROW method '(' listexprcom ')'
297 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
299 prepend_elem(OP_LIST, $1, $5),
300 newUNOP(OP_METHOD, 0, $3))); }
301 | METHOD indirob listexpr
302 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
304 prepend_elem(OP_LIST, $2, $3),
305 newUNOP(OP_METHOD, 0, $1))); }
306 | FUNCMETH indirob '(' listexprcom ')'
307 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
309 prepend_elem(OP_LIST, $2, $4),
310 newUNOP(OP_METHOD, 0, $1))); }
312 { $$ = convert($1, 0, $2); }
313 | FUNC '(' listexprcom ')'
314 { $$ = convert($1, 0, $3); }
315 | LSTOPSUB startsub block listexpr %prec LSTOP
316 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
318 prepend_elem(OP_LIST, newANONSUB($2, 0, $3), $4),
326 term : term ASSIGNOP term
327 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
329 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
331 { if ($2 != OP_REPEAT)
333 $$ = newBINOP($2, 0, $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 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
347 { $$ = newRANGE($2, scalar($1), scalar($3));}
349 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
351 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
352 | term '?' term ':' term
353 { $$ = newCONDOP(0, $1, $3, $5); }
355 { $$ = bind_match($2, $1, $3); }
357 | '-' term %prec UMINUS
358 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
359 | '+' term %prec UMINUS
362 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
364 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
366 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
368 { $$ = newUNOP(OP_POSTINC, 0,
369 mod(scalar($1), OP_POSTINC)); }
371 { $$ = newUNOP(OP_POSTDEC, 0,
372 mod(scalar($1), OP_POSTDEC)); }
374 { $$ = newUNOP(OP_PREINC, 0,
375 mod(scalar($2), OP_PREINC)); }
377 { $$ = newUNOP(OP_PREDEC, 0,
378 mod(scalar($2), OP_PREDEC)); }
379 | LOCAL term %prec UNIOP
380 { $$ = localize($2,$1); }
382 { $$ = sawparens($2); }
384 { $$ = sawparens(newNULLLIST()); }
385 | '[' expr ']' %prec '('
386 { $$ = newANONLIST($2); }
388 { $$ = newANONLIST(Nullop); }
389 | HASHBRACK expr ';' '}' %prec '('
390 { $$ = newANONHASH($2); }
391 | HASHBRACK ';' '}' %prec '('
392 { $$ = newANONHASH(Nullop); }
393 | ANONSUB startsub proto block %prec '('
394 { $$ = newANONSUB($2, $3, $4); }
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,
451 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
453 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
454 append_elem(OP_LIST, $3, scalar($1))); }
455 | NOAMP WORD listexpr
456 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
458 $3, newCVREF(scalar($2)))); }
459 | DO term %prec UNIOP
460 { $$ = newUNOP(OP_DOFILE, 0, scalar($2)); }
462 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
464 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
465 prepend_elem(OP_LIST,
466 scalar(newCVREF(scalar($2))), Nullop)); dep();}
467 | DO WORD '(' expr ')'
468 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
471 scalar(newCVREF(scalar($2))))); dep();}
473 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
474 prepend_elem(OP_LIST,
475 scalar(newCVREF(scalar($2))), Nullop)); dep();}
476 | DO scalar '(' expr ')'
477 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
478 prepend_elem(OP_LIST,
480 scalar(newCVREF(scalar($2))))); dep();}
482 { $$ = newOP($1, OPf_SPECIAL);
483 hints |= HINT_BLOCK_SCOPE; }
485 { $$ = newLOOPEX($1,$2); }
487 { $$ = newOP($1, 0); }
489 { $$ = newUNOP($1, 0, $2); }
491 { $$ = newUNOP($1, 0, $2); }
493 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
494 append_elem(OP_LIST, $2, scalar($1))); }
496 { $$ = newOP($1, 0); }
498 { $$ = newOP($1, 0); }
500 { $$ = newUNOP(OP_ENTERSUB, 0,
503 { $$ = newOP($1, OPf_SPECIAL); }
505 { $$ = newUNOP($1, 0, $3); }
506 | PMFUNC '(' term ')'
507 { $$ = pmruntime($1, $3, Nullop); }
508 | PMFUNC '(' term ',' term ')'
509 { $$ = pmruntime($1, $3, $5); }
520 listexprcom: /* NULL */
529 { $$ = newCVREF($2); }
533 { $$ = newSVREF($2); }
537 { $$ = newAVREF($2); }
541 { $$ = newHVREF($2); }
544 arylen : DOLSHARP indirob
545 { $$ = newAVREF($2); }
549 { $$ = newGVREF(0,$2); }