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
40 %token <ival> FORMAT SUB ANONSUB PACKAGE USE
41 %token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
42 %token <ival> LOOPEX DOTDOT
43 %token <ival> FUNC0 FUNC1 FUNC
44 %token <ival> RELOP EQOP MULOP ADDOP
45 %token <ival> DOLSHARP DO LOCAL HASHBRACK NOAMP
47 %type <ival> prog decl format remember startsub
48 %type <opval> block lineseq line loop cond nexpr else argexpr
49 %type <opval> expr term scalar ary hsh arylen star amper sideff
50 %type <opval> listexpr listexprcom indirob
51 %type <opval> texpr listop method
58 %nonassoc <ival> LSTOP
60 %right <ival> ASSIGNOP
69 %nonassoc <ival> UNIOP
74 %right '!' '~' UMINUS REFGEN
76 %nonassoc PREINC PREDEC POSTINC POSTDEC
84 #if defined(YYDEBUG) && defined(DEBUGGING)
85 yydebug = (debug & 1);
93 block : '{' remember lineseq '}'
94 { $$ = block_end($1,$2,$3); }
97 remember: /* NULL */ /* start a lexical scope */
98 { $$ = block_start(); }
106 { $$ = append_list(OP_LINESEQ,
107 (LISTOP*)$1, (LISTOP*)$2);
108 pad_reset_pending = TRUE;
109 if ($1 && $2) hints |= HINT_BLOCK_SCOPE; }
113 { $$ = newSTATEOP(0, $1, $2); }
114 | loop /* loops add their own labels */
116 { if ($1 != Nullch) {
117 $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0));
125 { $$ = newSTATEOP(0, $1, $2);
134 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
136 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
138 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
140 { $$ = newLOOPOP(OPf_PARENS, 1, invert(scalar($3)), $1);}
147 | ELSIF '(' expr ')' block else
149 $$ = newSTATEOP(0, 0,
150 newCONDOP(0, $3, scope($5), $6)); }
153 cond : IF '(' expr ')' block else
155 $$ = newCONDOP(0, $3, scope($5), $6); }
156 | UNLESS '(' expr ')' block else
159 invert(scalar($3)), scope($5), $6); }
160 | IF block block else
162 deprecate("if BLOCK BLOCK");
163 $$ = newCONDOP(0, scope($2), scope($3), $4); }
164 | UNLESS block block else
166 deprecate("unless BLOCK BLOCK");
167 $$ = newCONDOP(0, invert(scalar(scope($2))),
177 loop : label WHILE '(' texpr ')' block cont
179 $$ = newSTATEOP(0, $1,
180 newWHILEOP(0, 1, (LOOP*)Nullop,
182 | label UNTIL '(' expr ')' block cont
184 $$ = newSTATEOP(0, $1,
185 newWHILEOP(0, 1, (LOOP*)Nullop,
186 invert(scalar($4)), $6, $7) ); }
187 | label WHILE block block cont
189 $$ = newSTATEOP(0, $1,
190 newWHILEOP(0, 1, (LOOP*)Nullop,
191 scope($3), $4, $5) ); }
192 | label UNTIL block block cont
194 $$ = newSTATEOP(0, $1,
195 newWHILEOP(0, 1, (LOOP*)Nullop,
196 invert(scalar(scope($3))), $4, $5)); }
197 | label FOR scalar '(' expr ')' block cont
198 { $$ = newFOROP(0, $1, $2, mod($3, OP_ENTERLOOP),
200 | label FOR '(' expr ')' block cont
201 { $$ = newFOROP(0, $1, $2, Nullop, $4, $6, $7); }
202 | label FOR '(' nexpr ';' texpr ';' nexpr ')' block
203 /* basically fake up an initialize-while lineseq */
205 $$ = append_elem(OP_LINESEQ,
206 newSTATEOP(0, $1, scalar($4)),
208 newWHILEOP(0, 1, (LOOP*)Nullop,
209 scalar($6), $10, scalar($8)) )); }
210 | label block cont /* a block is a loop that happens once */
212 $1, newWHILEOP(0, 1, (LOOP*)Nullop,
221 texpr : /* NULL means true */
222 { (void)scan_num("1"); $$ = yylval.opval; }
241 format : FORMAT startsub WORD block
242 { newFORM($2, $3, $4); }
243 | FORMAT startsub block
244 { newFORM($2, Nullop, $3); }
247 subrout : SUB startsub WORD block
248 { newSUB($2, $3, $4); }
249 | SUB startsub WORD ';'
250 { newSUB($2, $3, Nullop); expect = XSTATE; }
253 startsub: /* NULL */ /* start a subroutine scope */
254 { $$ = start_subparse(); }
257 package : PACKAGE WORD ';'
263 use : USE WORD listexpr ';'
264 { utilize($1, $2, $3); }
267 expr : expr ANDOP expr
268 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
270 { $$ = newLOGOP($2, 0, $1, $3); }
272 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
276 argexpr : argexpr ','
279 { $$ = append_elem(OP_LIST, $1, $3); }
283 listop : LSTOP indirob argexpr
284 { $$ = convert($1, OPf_STACKED,
285 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
286 | FUNC '(' indirob expr ')'
287 { $$ = convert($1, OPf_STACKED,
288 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
289 | term ARROW method '(' listexprcom ')'
290 { $$ = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
292 prepend_elem(OP_LIST, $1, list($5)),
293 newUNOP(OP_METHOD, 0, $3))); }
294 | METHOD indirob listexpr
295 { $$ = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
297 prepend_elem(OP_LIST, $2, list($3)),
298 newUNOP(OP_METHOD, 0, $1))); }
299 | FUNCMETH indirob '(' listexprcom ')'
300 { $$ = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
302 prepend_elem(OP_LIST, $2, list($4)),
303 newUNOP(OP_METHOD, 0, $1))); }
305 { $$ = convert($1, 0, $2); }
306 | FUNC '(' listexprcom ')'
307 { $$ = convert($1, 0, $3); }
314 term : term ASSIGNOP term
315 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
317 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
319 { if ($2 != OP_REPEAT)
321 $$ = newBINOP($2, 0, $1, scalar($3)); }
323 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
325 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
327 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
329 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
331 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
333 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
335 { $$ = newRANGE($2, scalar($1), scalar($3));}
337 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
339 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
340 | term '?' term ':' term
341 { $$ = newCONDOP(0, $1, $3, $5); }
343 { $$ = bind_match($2, $1, $3); }
345 | '-' term %prec UMINUS
346 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
347 | '+' term %prec UMINUS
350 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
352 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
354 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
356 { $$ = newUNOP(OP_POSTINC, 0,
357 mod(scalar($1), OP_POSTINC)); }
359 { $$ = newUNOP(OP_POSTDEC, 0,
360 mod(scalar($1), OP_POSTDEC)); }
362 { $$ = newUNOP(OP_PREINC, 0,
363 mod(scalar($2), OP_PREINC)); }
365 { $$ = newUNOP(OP_PREDEC, 0,
366 mod(scalar($2), OP_PREDEC)); }
367 | LOCAL term %prec UNIOP
368 { $$ = localize($2,$1); }
370 { $$ = sawparens($2); }
372 { $$ = sawparens(newNULLLIST()); }
373 | '[' expr ']' %prec '('
374 { $$ = newANONLIST($2); }
376 { $$ = newANONLIST(Nullop); }
377 | HASHBRACK expr ';' '}' %prec '('
378 { $$ = newANONHASH($2); }
379 | HASHBRACK ';' '}' %prec '('
380 { $$ = newANONHASH(Nullop); }
381 | ANONSUB startsub block %prec '('
382 { $$ = newANONSUB($2, $3); }
387 | scalar '[' expr ']' %prec '('
388 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
389 | term ARROW '[' expr ']' %prec '('
390 { $$ = newBINOP(OP_AELEM, 0,
391 ref(newAVREF($1),OP_RV2AV),
393 | term '[' expr ']' %prec '('
394 { assertref($1); $$ = newBINOP(OP_AELEM, 0,
395 ref(newAVREF($1),OP_RV2AV),
402 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
403 | scalar '{' expr ';' '}' %prec '('
404 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
405 expect = XOPERATOR; }
406 | term ARROW '{' expr ';' '}' %prec '('
407 { $$ = newBINOP(OP_HELEM, 0,
408 ref(newHVREF($1),OP_RV2HV),
410 expect = XOPERATOR; }
411 | term '{' expr ';' '}' %prec '('
412 { assertref($1); $$ = newBINOP(OP_HELEM, 0,
413 ref(newHVREF($1),OP_RV2HV),
415 expect = XOPERATOR; }
416 | '(' expr ')' '[' expr ']' %prec '('
417 { $$ = newSLICEOP(0, $5, $2); }
418 | '(' ')' '[' expr ']' %prec '('
419 { $$ = newSLICEOP(0, $4, Nullop); }
420 | ary '[' expr ']' %prec '('
421 { $$ = prepend_elem(OP_ASLICE,
422 newOP(OP_PUSHMARK, 0),
423 newLISTOP(OP_ASLICE, 0,
425 ref($1, OP_ASLICE))); }
426 | ary '{' expr ';' '}' %prec '('
427 { $$ = prepend_elem(OP_HSLICE,
428 newOP(OP_PUSHMARK, 0),
429 newLISTOP(OP_HSLICE, 0,
431 ref(oopsHV($1), OP_HSLICE)));
432 expect = XOPERATOR; }
436 { $$ = newUNOP(OP_ENTERSUB, 0,
439 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
441 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
442 list(append_elem(OP_LIST, $3, scalar($1)))); }
443 | NOAMP WORD listexpr
444 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
445 list(append_elem(OP_LIST,
446 $3, newCVREF(scalar($2))))); }
447 | DO term %prec UNIOP
448 { $$ = newUNOP(OP_DOFILE, 0, scalar($2)); }
450 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
452 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
453 list(prepend_elem(OP_LIST,
454 scalar(newCVREF(scalar($2))), Nullop))); dep();}
455 | DO WORD '(' expr ')'
456 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
457 list(append_elem(OP_LIST,
459 scalar(newCVREF(scalar($2)))))); dep();}
461 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
462 list(prepend_elem(OP_LIST,
463 scalar(newCVREF(scalar($2))), Nullop))); dep();}
464 | DO scalar '(' expr ')'
465 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
466 list(prepend_elem(OP_LIST,
468 scalar(newCVREF(scalar($2)))))); dep();}
470 { $$ = newOP($1, OPf_SPECIAL);
471 hints |= HINT_BLOCK_SCOPE; }
473 { $$ = newLOOPEX($1,$2); }
475 { $$ = newOP($1, 0); }
477 { $$ = newUNOP($1, 0, $2); }
479 { $$ = newUNOP($1, 0, $2); }
481 { $$ = newOP($1, 0); }
483 { $$ = newOP($1, 0); }
485 { $$ = newOP($1, OPf_SPECIAL); }
487 { $$ = newUNOP($1, 0, $3); }
488 | PMFUNC '(' term ')'
489 { $$ = pmruntime($1, $3, Nullop); }
490 | PMFUNC '(' term ',' term ')'
491 { $$ = pmruntime($1, $3, $5); }
502 listexprcom: /* NULL */
511 { $$ = newCVREF($2); }
515 { $$ = newSVREF($2); }
519 { $$ = newAVREF($2); }
523 { $$ = newHVREF($2); }
526 arylen : DOLSHARP indirob
527 { $$ = newAVREF($2); }
531 { $$ = newGVREF(0,$2); }