RE: [PATCH] compress 2.018
[p5sagit/p5-mst-13.2.git] / perly.y
CommitLineData
a0d0e21e 1/* perly.y
a687059c 2 *
f05e27e5 3 * Copyright (c) 1991-2002, 2003, 2004, 2005, 2006 Larry Wall
1129b882 4 * Copyright (c) 2007, 2008 by Larry Wall and others
a687059c 5 *
9ef589d8 6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8d063cd8 8 *
a0d0e21e 9 */
10
11/*
12 * 'I see,' laughed Strider. 'I look foul and feel fair. Is that it?
4ac71550 13 * All that is gold does not glitter, not all those who wander are lost.'
f05e27e5 14 *
4ac71550 15 * [p.171 of _The Lord of the Rings_, I/x: "Strider"]
16 */
17
18/*
f05e27e5 19 * This file holds the grammar for the Perl language. If edited, you need
166f8a29 20 * to run regen_perly.pl, which re-creates the files perly.h, perly.tab
21 * and perly.act which are derived from this.
22 *
f05e27e5 23 * Note that these derived files are included and compiled twice; once
24 * from perly.c, and once from madly.c. The second time, a number of MAD
25 * macros are defined, which compile in extra code that allows the parse
26 * tree to be accurately dumped. In particular:
27 *
28 * MAD defined if compiling madly.c
29 * DO_MAD(A) expands to A under madly.c, to null otherwise
30 * IF_MAD(a,b) expands to A under madly.c, to B otherwise
31 * TOKEN_GETMAD() expands to token_getmad() under madly.c, to null otherwise
32 * TOKEN_FREE() similarly
33 * OP_GETMAD() similarly
34 * IVAL(i) expands to (i)->tk_lval.ival or (i)
35 * PVAL(p) expands to (p)->tk_lval.pval or (p)
36 *
166f8a29 37 * The main job of of this grammar is to call the various newFOO()
38 * functions in op.c to build a syntax tree of OP structs.
61296642 39 * It relies on the lexer in toke.c to do the tokenizing.
29522234 40 *
41 * Note: due to the way that the cleanup code works WRT to freeing ops on
42 * the parse stack, it is dangerous to assign to the $n variables within
43 * an action.
166f8a29 44 */
45
0de566d7 46/* Make the parser re-entrant. */
8d063cd8 47
0de566d7 48%pure_parser
8d063cd8 49
f05e27e5 50/* FIXME for MAD - is the new mintro on while and until important? */
51
0de566d7 52%start prog
9d116dd7 53
8d063cd8 54%union {
d5c6462e 55 I32 ival; /* __DEFAULT__ (marker for regen_perly.pl;
56 must always be 1st union member) */
79072805 57 char *pval;
58 OP *opval;
59 GV *gvval;
f05e27e5 60#ifdef PERL_IN_MADLY_C
61 TOKEN* p_tkval;
123d08c9 62 TOKEN* i_tkval;
f05e27e5 63#else
64 char *p_tkval;
123d08c9 65 I32 i_tkval;
66#endif
67#ifdef PERL_MAD
68 TOKEN* tkval;
f05e27e5 69#endif
8d063cd8 70}
71
123d08c9 72%token <i_tkval> '{' '}' '[' ']' '-' '+' '$' '@' '%' '*' '&' ';'
f0fcb552 73
a0d0e21e 74%token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
4633a7c4 75%token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
f05e27e5 76%token <p_tkval> LABEL
123d08c9 77%token <i_tkval> FORMAT SUB ANONSUB PACKAGE USE
78%token <i_tkval> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
79%token <i_tkval> GIVEN WHEN DEFAULT
be25f609 80%token <i_tkval> LOOPEX DOTDOT YADAYADA
123d08c9 81%token <i_tkval> FUNC0 FUNC1 FUNC UNIOP LSTOP
82%token <i_tkval> RELOP EQOP MULOP ADDOP
83%token <i_tkval> DOLSHARP DO HASHBRACK NOAMP
84%token <i_tkval> LOCAL MY MYSUB REQUIRE
85%token <i_tkval> COLONATTR
f05e27e5 86
503de470 87%type <ival> prog progstart remember mremember
f05e27e5 88%type <ival> startsub startanonsub startformsub
89/* FIXME for MAD - are these two ival? */
90%type <ival> mydefsv mintro
91
92%type <opval> decl format subrout mysubrout package use peg
93
bbce6d69 94%type <opval> block mblock lineseq line loop cond else
fad39ff1 95%type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
a034e688 96%type <opval> argexpr nexpr texpr iexpr mexpr mnexpr miexpr
44a8e56a 97%type <opval> listexpr listexprcom indirob listop method
98%type <opval> formname subname proto subbody cont my_scalar
f05e27e5 99%type <opval> subattrlist myattrlist myattrterm myterm
891be019 100%type <opval> termbinop termunop anonymous termdo
0d863452 101%type <opval> switch case
f05e27e5 102%type <p_tkval> label
79072805 103
123d08c9 104%nonassoc <i_tkval> PREC_LOW
fad39ff1 105%nonassoc LOOPEX
106
123d08c9 107%left <i_tkval> OROP DOROP
108%left <i_tkval> ANDOP
109%right <i_tkval> NOTOP
36477c24 110%nonassoc LSTOP LSTOPSUB
123d08c9 111%left <i_tkval> ','
112%right <i_tkval> ASSIGNOP
113%right <i_tkval> '?' ':'
be25f609 114%nonassoc DOTDOT YADAYADA
123d08c9 115%left <i_tkval> OROR DORDOR
116%left <i_tkval> ANDAND
117%left <i_tkval> BITOROP
118%left <i_tkval> BITANDOP
a687059c 119%nonassoc EQOP
120%nonassoc RELOP
36477c24 121%nonassoc UNIOP UNIOPSUB
20515881 122%nonassoc REQUIRE
123d08c9 123%left <i_tkval> SHIFTOP
a687059c 124%left ADDOP
125%left MULOP
123d08c9 126%left <i_tkval> MATCHOP
127%right <i_tkval> '!' '~' UMINUS REFGEN
128%right <i_tkval> POWOP
129%nonassoc <i_tkval> PREINC PREDEC POSTINC POSTDEC
130%left <i_tkval> ARROW
131%nonassoc <i_tkval> ')'
132%left <i_tkval> '('
fad39ff1 133%left '[' '{'
8d063cd8 134
123d08c9 135%token <i_tkval> PEG
7f46837f 136
8d063cd8 137%% /* RULES */
138
891be019 139/* The whole program */
ecb2f335 140prog : progstart
ae986130 141 /*CONTINUED*/ lineseq
ecb2f335 142 { $$ = $1; newPROG(block_end($1,$2)); }
8d063cd8 143 ;
144
891be019 145/* An ordinary block */
a687059c 146block : '{' remember lineseq '}'
53a7735b 147 { if (PL_parser->copline > (line_t)IVAL($1))
148 PL_parser->copline = (line_t)IVAL($1);
f05e27e5 149 $$ = block_end($2, $3);
150 TOKEN_GETMAD($1,$$,'{');
151 TOKEN_GETMAD($4,$$,'}');
152 }
a0d0e21e 153 ;
154
55497cff 155remember: /* NULL */ /* start a full lexical scope */
156 { $$ = block_start(TRUE); }
157 ;
158
0d863452 159mydefsv: /* NULL */ /* lexicalize $_ */
160 { $$ = (I32) allocmy("$_"); }
161 ;
162
ecb2f335 163progstart:
164 {
53a7735b 165 PL_parser->expect = XSTATE; $$ = block_start(TRUE);
ecb2f335 166 }
167 ;
168
169
bbce6d69 170mblock : '{' mremember lineseq '}'
53a7735b 171 { if (PL_parser->copline > (line_t)IVAL($1))
172 PL_parser->copline = (line_t)IVAL($1);
f05e27e5 173 $$ = block_end($2, $3);
174 TOKEN_GETMAD($1,$$,'{');
175 TOKEN_GETMAD($4,$$,'}');
176 }
55497cff 177 ;
178
179mremember: /* NULL */ /* start a partial lexical scope */
180 { $$ = block_start(FALSE); }
8d063cd8 181 ;
182
891be019 183/* A collection of "lines" in the program */
8d063cd8 184lineseq : /* NULL */
bcabcc50 185 { $$ = (OP*)NULL; }
79072805 186 | lineseq decl
f05e27e5 187 {
188 $$ = IF_MAD(
189 append_list(OP_LINESEQ,
190 (LISTOP*)$1, (LISTOP*)$2),
191 $1);
192 }
503de470 193 | lineseq line
194 { $$ = append_list(OP_LINESEQ,
195 (LISTOP*)$1, (LISTOP*)$2);
3280af22 196 PL_pad_reset_pending = TRUE;
503de470 197 if ($1 && $2)
198 PL_hints |= HINT_BLOCK_SCOPE;
199 }
8d063cd8 200 ;
201
891be019 202/* A "line" in the program */
79072805 203line : label cond
f05e27e5 204 { $$ = newSTATEOP(0, PVAL($1), $2);
205 TOKEN_GETMAD($1,((LISTOP*)$$)->op_first,'L'); }
8d063cd8 206 | loop /* loops add their own labels */
0d863452 207 | switch /* ... and so do switches */
208 { $$ = $1; }
209 | label case
f05e27e5 210 { $$ = newSTATEOP(0, PVAL($1), $2); }
8d063cd8 211 | label ';'
f05e27e5 212 {
213 if (PVAL($1)) {
214 $$ = newSTATEOP(0, PVAL($1), newOP(OP_NULL, 0));
215 TOKEN_GETMAD($1,$$,'L');
216 TOKEN_GETMAD($2,((LISTOP*)$$)->op_first,';');
217 }
218 else {
219 $$ = IF_MAD(
220 newOP(OP_NULL, 0),
bcabcc50 221 (OP*)NULL);
53a7735b 222 PL_parser->copline = NOLINE;
f05e27e5 223 TOKEN_FREE($1);
224 TOKEN_GETMAD($2,$$,';');
225 }
53a7735b 226 PL_parser->expect = XSTATE;
f05e27e5 227 }
8d063cd8 228 | label sideff ';'
f05e27e5 229 {
230 $$ = newSTATEOP(0, PVAL($1), $2);
53a7735b 231 PL_parser->expect = XSTATE;
035e2bcc 232 DO_MAD({
f05e27e5 233 /* sideff might already have a nexstate */
234 OP* op = ((LISTOP*)$$)->op_first;
235 if (op) {
236 while (op->op_sibling &&
237 op->op_sibling->op_type == OP_NEXTSTATE)
238 op = op->op_sibling;
239 token_getmad($1,op,'L');
240 token_getmad($3,op,';');
241 }
035e2bcc 242 })
f05e27e5 243 }
8d063cd8 244 ;
245
891be019 246/* An expression which may have a side-effect */
a687059c 247sideff : error
bcabcc50 248 { $$ = (OP*)NULL; }
a687059c 249 | expr
79072805 250 { $$ = $1; }
a687059c 251 | expr IF expr
f05e27e5 252 { $$ = newLOGOP(OP_AND, 0, $3, $1);
253 TOKEN_GETMAD($2,$$,'i');
254 }
a687059c 255 | expr UNLESS expr
f05e27e5 256 { $$ = newLOGOP(OP_OR, 0, $3, $1);
257 TOKEN_GETMAD($2,$$,'i');
258 }
a687059c 259 | expr WHILE expr
f05e27e5 260 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1);
261 TOKEN_GETMAD($2,$$,'w');
262 }
55497cff 263 | expr UNTIL iexpr
f05e27e5 264 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);
265 TOKEN_GETMAD($2,$$,'w');
266 }
ecca16b0 267 | expr FOR expr
1a9a51d4 268 { $$ = newFOROP(0, NULL, (line_t)IVAL($2),
bcabcc50 269 (OP*)NULL, $3, $1, (OP*)NULL);
f05e27e5 270 TOKEN_GETMAD($2,((LISTOP*)$$)->op_first->op_sibling,'w');
271 }
f20dcd76 272 | expr WHEN expr
273 { $$ = newWHENOP($3, scope($1)); }
79072805 274 ;
275
891be019 276/* else and elsif blocks */
79072805 277else : /* NULL */
bcabcc50 278 { $$ = (OP*)NULL; }
55497cff 279 | ELSE mblock
f05e27e5 280 { ($2)->op_flags |= OPf_PARENS; $$ = scope($2);
281 TOKEN_GETMAD($1,$$,'o');
282 }
55497cff 283 | ELSIF '(' mexpr ')' mblock else
53a7735b 284 { PL_parser->copline = (line_t)IVAL($1);
f6f3a1fe 285 $$ = newCONDOP(0, newSTATEOP(OPf_SPECIAL,NULL,$3), scope($5), $6);
f05e27e5 286 PL_hints |= HINT_BLOCK_SCOPE;
287 TOKEN_GETMAD($1,$$,'I');
288 TOKEN_GETMAD($2,$$,'(');
289 TOKEN_GETMAD($4,$$,')');
290 }
79072805 291 ;
292
891be019 293/* Real conditional expressions */
55497cff 294cond : IF '(' remember mexpr ')' mblock else
53a7735b 295 { PL_parser->copline = (line_t)IVAL($1);
36477c24 296 $$ = block_end($3,
f05e27e5 297 newCONDOP(0, $4, scope($6), $7));
298 TOKEN_GETMAD($1,$$,'I');
299 TOKEN_GETMAD($2,$$,'(');
300 TOKEN_GETMAD($5,$$,')');
301 }
55497cff 302 | UNLESS '(' remember miexpr ')' mblock else
53a7735b 303 { PL_parser->copline = (line_t)IVAL($1);
36477c24 304 $$ = block_end($3,
f05e27e5 305 newCONDOP(0, $4, scope($6), $7));
306 TOKEN_GETMAD($1,$$,'I');
307 TOKEN_GETMAD($2,$$,'(');
308 TOKEN_GETMAD($5,$$,')');
309 }
79072805 310 ;
311
0d863452 312/* Cases for a switch statement */
313case : WHEN '(' remember mexpr ')' mblock
314 { $$ = block_end($3,
315 newWHENOP($4, scope($6))); }
316 | DEFAULT block
317 { $$ = newWHENOP(0, scope($2)); }
318 ;
319
891be019 320/* Continue blocks */
79072805 321cont : /* NULL */
bcabcc50 322 { $$ = (OP*)NULL; }
79072805 323 | CONTINUE block
f05e27e5 324 { $$ = scope($2);
325 TOKEN_GETMAD($1,$$,'o');
326 }
79072805 327 ;
328
891be019 329/* Loops: while, until, for, and a bare block */
a034e688 330loop : label WHILE '(' remember texpr ')' mintro mblock cont
f05e27e5 331 { OP *innerop;
53a7735b 332 PL_parser->copline = (line_t)$2;
36477c24 333 $$ = block_end($4,
f05e27e5 334 newSTATEOP(0, PVAL($1),
bcabcc50 335 innerop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
f05e27e5 336 IVAL($2), $5, $8, $9, $7)));
337 TOKEN_GETMAD($1,innerop,'L');
338 TOKEN_GETMAD($2,innerop,'W');
339 TOKEN_GETMAD($3,innerop,'(');
340 TOKEN_GETMAD($6,innerop,')');
341 }
342
a034e688 343 | label UNTIL '(' remember iexpr ')' mintro mblock cont
f05e27e5 344 { OP *innerop;
53a7735b 345 PL_parser->copline = (line_t)$2;
36477c24 346 $$ = block_end($4,
f05e27e5 347 newSTATEOP(0, PVAL($1),
bcabcc50 348 innerop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
f05e27e5 349 IVAL($2), $5, $8, $9, $7)));
350 TOKEN_GETMAD($1,innerop,'L');
351 TOKEN_GETMAD($2,innerop,'W');
352 TOKEN_GETMAD($3,innerop,'(');
353 TOKEN_GETMAD($6,innerop,')');
354 }
bbce6d69 355 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
f05e27e5 356 { OP *innerop;
357 $$ = block_end($4,
358 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
359 $5, $7, $9, $10));
360 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
361 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
362 TOKEN_GETMAD($3,((LISTOP*)innerop)->op_first->op_sibling,'d');
363 TOKEN_GETMAD($6,((LISTOP*)innerop)->op_first->op_sibling,'(');
364 TOKEN_GETMAD($8,((LISTOP*)innerop)->op_first->op_sibling,')');
365 }
bbce6d69 366 | label FOR scalar '(' remember mexpr ')' mblock cont
f05e27e5 367 { OP *innerop;
368 $$ = block_end($5,
369 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
370 mod($3, OP_ENTERLOOP), $6, $8, $9));
371 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
372 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
373 TOKEN_GETMAD($4,((LISTOP*)innerop)->op_first->op_sibling,'(');
374 TOKEN_GETMAD($7,((LISTOP*)innerop)->op_first->op_sibling,')');
375 }
bbce6d69 376 | label FOR '(' remember mexpr ')' mblock cont
f05e27e5 377 { OP *innerop;
378 $$ = block_end($4,
379 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
bcabcc50 380 (OP*)NULL, $5, $7, $8));
f05e27e5 381 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
382 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
383 TOKEN_GETMAD($3,((LISTOP*)innerop)->op_first->op_sibling,'(');
384 TOKEN_GETMAD($6,((LISTOP*)innerop)->op_first->op_sibling,')');
385 }
a034e688 386 | label FOR '(' remember mnexpr ';' texpr ';' mintro mnexpr ')'
387 mblock
8d063cd8 388 /* basically fake up an initialize-while lineseq */
36c66720 389 { OP *forop;
53a7735b 390 PL_parser->copline = (line_t)IVAL($2);
f05e27e5 391 forop = newSTATEOP(0, PVAL($1),
bcabcc50 392 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
f05e27e5 393 IVAL($2), scalar($7),
a034e688 394 $12, $10, $9));
f05e27e5 395#ifdef MAD
f05e27e5 396 forop = newUNOP(OP_NULL, 0, append_elem(OP_LINESEQ,
397 newSTATEOP(0,
63031daf 398 CopLABEL_alloc(($1)->tk_lval.pval),
700f8fa5 399 ($5 ? $5 : newOP(OP_NULL, 0)) ),
f05e27e5 400 forop));
401
402 token_getmad($2,forop,'3');
403 token_getmad($3,forop,'(');
404 token_getmad($6,forop,'1');
405 token_getmad($8,forop,'2');
406 token_getmad($11,forop,')');
407 token_getmad($1,forop,'L');
408#else
36c66720 409 if ($5) {
410 forop = append_elem(OP_LINESEQ,
63031daf 411 newSTATEOP(0, CopLABEL_alloc($1), $5),
36c66720 412 forop);
413 }
414
f05e27e5 415
416#endif
36c66720 417 $$ = block_end($4, forop); }
79072805 418 | label block cont /* a block is a loop that happens once */
f05e27e5 419 { $$ = newSTATEOP(0, PVAL($1),
bcabcc50 420 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
421 NOLINE, (OP*)NULL, $2, $3, 0));
f05e27e5 422 TOKEN_GETMAD($1,((LISTOP*)$$)->op_first,'L'); }
8d063cd8 423 ;
424
0d863452 425/* Switch blocks */
426switch : label GIVEN '(' remember mydefsv mexpr ')' mblock
53a7735b 427 { PL_parser->copline = (line_t) $2;
0d863452 428 $$ = block_end($4,
f05e27e5 429 newSTATEOP(0, PVAL($1),
0d863452 430 newGIVENOP($6, scope($8),
431 (PADOFFSET) $5) )); }
432 ;
433
a034e688 434/* determine whether there are any new my declarations */
435mintro : /* NULL */
436 { $$ = (PL_min_intro_pending &&
437 PL_max_intro_pending >= PL_min_intro_pending);
438 intro_my(); }
439
891be019 440/* Normal expression */
8d063cd8 441nexpr : /* NULL */
bcabcc50 442 { $$ = (OP*)NULL; }
8d063cd8 443 | sideff
444 ;
445
891be019 446/* Boolean expression */
8d063cd8 447texpr : /* NULL means true */
f05e27e5 448 { YYSTYPE tmplval;
449 (void)scan_num("1", &tmplval);
450 $$ = tmplval.opval; }
8d063cd8 451 | expr
452 ;
453
891be019 454/* Inverted boolean expression */
55497cff 455iexpr : expr
456 { $$ = invert(scalar($1)); }
457 ;
458
891be019 459/* Expression with its own lexical scope */
55497cff 460mexpr : expr
bbce6d69 461 { $$ = $1; intro_my(); }
462 ;
463
464mnexpr : nexpr
465 { $$ = $1; intro_my(); }
55497cff 466 ;
467
55497cff 468miexpr : iexpr
bbce6d69 469 { $$ = $1; intro_my(); }
55497cff 470 ;
471
891be019 472/* Optional "MAIN:"-style loop labels */
8d063cd8 473label : /* empty */
f05e27e5 474 {
475#ifdef MAD
476 YYSTYPE tmplval;
1a9a51d4 477 tmplval.pval = NULL;
f05e27e5 478 $$ = newTOKEN(OP_NULL, tmplval, 0);
479#else
1a9a51d4 480 $$ = NULL;
f05e27e5 481#endif
482 }
32c2e4fb 483 | LABEL
8d063cd8 484 ;
485
f05e27e5 486/* Some kind of declaration - just hang on peg in the parse tree */
8d063cd8 487decl : format
f05e27e5 488 { $$ = $1; }
8d063cd8 489 | subrout
f05e27e5 490 { $$ = $1; }
09bef843 491 | mysubrout
f05e27e5 492 { $$ = $1; }
a687059c 493 | package
f05e27e5 494 { $$ = $1; }
a0d0e21e 495 | use
f05e27e5 496 { $$ = $1; }
497
498 /* these two are only used by MAD */
499
500 | peg
501 { $$ = $1; }
502 ;
503
504peg : PEG
505 { $$ = newOP(OP_NULL,0);
506 TOKEN_GETMAD($1,$$,'p');
507 }
8d063cd8 508 ;
509
718a7425 510format : FORMAT startformsub formname block
5a5094bd 511 { SvREFCNT_inc_simple_void(PL_compcv);
f05e27e5 512#ifdef MAD
718a7425 513 $$ = newFORM($2, $3, $4);
f05e27e5 514 prepend_madprops($1->tk_mad, $$, 'F');
515 $1->tk_mad = 0;
516 token_free($1);
517#else
718a7425 518 newFORM($2, $3, $4);
bcabcc50 519 $$ = (OP*)NULL;
f05e27e5 520#endif
521 }
44a8e56a 522 ;
523
524formname: WORD { $$ = $1; }
bcabcc50 525 | /* NULL */ { $$ = (OP*)NULL; }
8d063cd8 526 ;
527
891be019 528/* Unimplemented "my sub foo { }" */
718a7425 529mysubrout: MYSUB startsub subname proto subattrlist subbody
5a5094bd 530 { SvREFCNT_inc_simple_void(PL_compcv);
f05e27e5 531#ifdef MAD
718a7425 532 $$ = newMYSUB($2, $3, $4, $5, $6);
f05e27e5 533 token_getmad($1,$$,'d');
534#else
718a7425 535 newMYSUB($2, $3, $4, $5, $6);
bcabcc50 536 $$ = (OP*)NULL;
f05e27e5 537#endif
538 }
09bef843 539 ;
540
891be019 541/* Subroutine definition */
718a7425 542subrout : SUB startsub subname proto subattrlist subbody
5a5094bd 543 { SvREFCNT_inc_simple_void(PL_compcv);
f05e27e5 544#ifdef MAD
035e2bcc 545 {
546 OP* o = newSVOP(OP_ANONCODE, 0,
547 (SV*)newATTRSUB($2, $3, $4, $5, $6));
548 $$ = newOP(OP_NULL,0);
549 op_getmad(o,$$,'&');
550 op_getmad($3,$$,'n');
551 op_getmad($4,$$,'s');
552 op_getmad($5,$$,'a');
553 token_getmad($1,$$,'d');
554 append_madprops($6->op_madprop, $$, 0);
555 $6->op_madprop = 0;
556 }
f05e27e5 557#else
718a7425 558 newATTRSUB($2, $3, $4, $5, $6);
bcabcc50 559 $$ = (OP*)NULL;
f05e27e5 560#endif
561 }
28757baa 562 ;
563
fa83b5b6 564startsub: /* NULL */ /* start a regular subroutine scope */
a8ff2fa6 565 { $$ = start_subparse(FALSE, 0);
566 SAVEFREESV(PL_compcv); }
f05e27e5 567
28757baa 568 ;
569
570startanonsub: /* NULL */ /* start an anonymous subroutine scope */
a8ff2fa6 571 { $$ = start_subparse(FALSE, CVf_ANON);
572 SAVEFREESV(PL_compcv); }
28757baa 573 ;
574
44a8e56a 575startformsub: /* NULL */ /* start a format subroutine scope */
a8ff2fa6 576 { $$ = start_subparse(TRUE, 0);
577 SAVEFREESV(PL_compcv); }
44a8e56a 578 ;
579
891be019 580/* Name of a subroutine - must be a bareword, could be special */
2596d9fe 581subname : WORD { const char *const name = SvPV_nolen_const(((SVOP*)$1)->op_sv);
e858de61 582 if (strEQ(name, "BEGIN") || strEQ(name, "END")
3c10abe3 583 || strEQ(name, "INIT") || strEQ(name, "CHECK")
584 || strEQ(name, "UNITCHECK"))
1aff0e91 585 CvSPECIAL_on(PL_compcv);
28757baa 586 $$ = $1; }
a0d0e21e 587 ;
588
891be019 589/* Subroutine prototype */
4633a7c4 590proto : /* NULL */
bcabcc50 591 { $$ = (OP*)NULL; }
4633a7c4 592 | THING
593 ;
28757baa 594
891be019 595/* Optional list of subroutine attributes */
09bef843 596subattrlist: /* NULL */
bcabcc50 597 { $$ = (OP*)NULL; }
09bef843 598 | COLONATTR THING
f05e27e5 599 { $$ = $2;
600 TOKEN_GETMAD($1,$$,':');
601 }
09bef843 602 | COLONATTR
f05e27e5 603 { $$ = IF_MAD(
604 newOP(OP_NULL, 0),
bcabcc50 605 (OP*)NULL
f05e27e5 606 );
607 TOKEN_GETMAD($1,$$,':');
608 }
09bef843 609 ;
610
891be019 611/* List of attributes for a "my" variable declaration */
09bef843 612myattrlist: COLONATTR THING
f05e27e5 613 { $$ = $2;
614 TOKEN_GETMAD($1,$$,':');
615 }
09bef843 616 | COLONATTR
f05e27e5 617 { $$ = IF_MAD(
618 newOP(OP_NULL, 0),
bcabcc50 619 (OP*)NULL
f05e27e5 620 );
621 TOKEN_GETMAD($1,$$,':');
622 }
09bef843 623 ;
624
891be019 625/* Subroutine body - either null or a block */
28757baa 626subbody : block { $$ = $1; }
f05e27e5 627 | ';' { $$ = IF_MAD(
628 newOP(OP_NULL,0),
bcabcc50 629 (OP*)NULL
f05e27e5 630 );
53a7735b 631 PL_parser->expect = XSTATE;
f05e27e5 632 TOKEN_GETMAD($1,$$,';');
633 }
8d063cd8 634 ;
635
a687059c 636package : PACKAGE WORD ';'
f05e27e5 637 {
638#ifdef MAD
639 $$ = package($2);
640 token_getmad($1,$$,'o');
641 token_getmad($3,$$,';');
642#else
643 package($2);
bcabcc50 644 $$ = (OP*)NULL;
f05e27e5 645#endif
646 }
a687059c 647 ;
648
718a7425 649use : USE startsub
1aff0e91 650 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
28757baa 651 WORD WORD listexpr ';'
5a5094bd 652 { SvREFCNT_inc_simple_void(PL_compcv);
f05e27e5 653#ifdef MAD
718a7425 654 $$ = utilize(IVAL($1), $2, $4, $5, $6);
f05e27e5 655 token_getmad($1,$$,'o');
718a7425 656 token_getmad($7,$$,';');
5486870f 657 if (PL_parser->rsfp_filters &&
658 AvFILLp(PL_parser->rsfp_filters) >= 0)
f05e27e5 659 append_madprops(newMADPROP('!', MAD_PV, "", 0), $$, 0);
660#else
718a7425 661 utilize(IVAL($1), $2, $4, $5, $6);
bcabcc50 662 $$ = (OP*)NULL;
f05e27e5 663#endif
664 }
85e6fe83 665 ;
666
891be019 667/* Ordinary expressions; logical combinations */
a0d0e21e 668expr : expr ANDOP expr
f05e27e5 669 { $$ = newLOGOP(OP_AND, 0, $1, $3);
670 TOKEN_GETMAD($2,$$,'o');
671 }
a0d0e21e 672 | expr OROP expr
f05e27e5 673 { $$ = newLOGOP(IVAL($2), 0, $1, $3);
674 TOKEN_GETMAD($2,$$,'o');
675 }
c963b151 676 | expr DOROP expr
f05e27e5 677 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
678 TOKEN_GETMAD($2,$$,'o');
679 }
fad39ff1 680 | argexpr %prec PREC_LOW
a0d0e21e 681 ;
682
891be019 683/* Expressions are a list of terms joined by commas */
a0d0e21e 684argexpr : argexpr ','
f05e27e5 685 {
686#ifdef MAD
687 OP* op = newNULLLIST();
688 token_getmad($2,op,',');
689 $$ = append_elem(OP_LIST, $1, op);
690#else
691 $$ = $1;
692#endif
693 }
a0d0e21e 694 | argexpr ',' term
f05e27e5 695 {
29522234 696 OP* term = $3;
f05e27e5 697 DO_MAD(
29522234 698 term = newUNOP(OP_NULL, 0, term);
699 token_getmad($2,term,',');
f05e27e5 700 )
29522234 701 $$ = append_elem(OP_LIST, $1, term);
f05e27e5 702 }
fad39ff1 703 | term %prec PREC_LOW
8d063cd8 704 ;
705
891be019 706/* List operators */
ad4673e5 707listop : LSTOP indirob argexpr /* map {...} @args or print $fh @args */
f05e27e5 708 { $$ = convert(IVAL($1), OPf_STACKED,
709 prepend_elem(OP_LIST, newGVREF(IVAL($1),$2), $3) );
710 TOKEN_GETMAD($1,$$,'o');
711 }
891be019 712 | FUNC '(' indirob expr ')' /* print ($fh @args */
f05e27e5 713 { $$ = convert(IVAL($1), OPf_STACKED,
714 prepend_elem(OP_LIST, newGVREF(IVAL($1),$3), $4) );
715 TOKEN_GETMAD($1,$$,'o');
716 TOKEN_GETMAD($2,$$,'(');
717 TOKEN_GETMAD($5,$$,')');
718 }
891be019 719 | term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
4633a7c4 720 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
a0d0e21e 721 append_elem(OP_LIST,
55497cff 722 prepend_elem(OP_LIST, scalar($1), $5),
f05e27e5 723 newUNOP(OP_METHOD, 0, $3)));
724 TOKEN_GETMAD($2,$$,'A');
725 TOKEN_GETMAD($4,$$,'(');
726 TOKEN_GETMAD($6,$$,')');
727 }
891be019 728 | term ARROW method /* $foo->bar */
b1524f17 729 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
730 append_elem(OP_LIST, scalar($1),
f05e27e5 731 newUNOP(OP_METHOD, 0, $3)));
732 TOKEN_GETMAD($2,$$,'A');
733 }
891be019 734 | METHOD indirob listexpr /* new Class @args */
4633a7c4 735 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
a0d0e21e 736 append_elem(OP_LIST,
4633a7c4 737 prepend_elem(OP_LIST, $2, $3),
f05e27e5 738 newUNOP(OP_METHOD, 0, $1)));
739 }
891be019 740 | FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
4633a7c4 741 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
a0d0e21e 742 append_elem(OP_LIST,
4633a7c4 743 prepend_elem(OP_LIST, $2, $4),
f05e27e5 744 newUNOP(OP_METHOD, 0, $1)));
745 TOKEN_GETMAD($3,$$,'(');
746 TOKEN_GETMAD($5,$$,')');
747 }
891be019 748 | LSTOP listexpr /* print @args */
f05e27e5 749 { $$ = convert(IVAL($1), 0, $2);
750 TOKEN_GETMAD($1,$$,'o');
751 }
891be019 752 | FUNC '(' listexprcom ')' /* print (@args) */
f05e27e5 753 { $$ = convert(IVAL($1), 0, $3);
754 TOKEN_GETMAD($1,$$,'o');
755 TOKEN_GETMAD($2,$$,'(');
756 TOKEN_GETMAD($4,$$,')');
757 }
718a7425 758 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
5a5094bd 759 { SvREFCNT_inc_simple_void(PL_compcv);
bcabcc50 760 $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); }
891be019 761 listexpr %prec LSTOP /* ... @bar */
4633a7c4 762 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
28757baa 763 append_elem(OP_LIST,
29522234 764 prepend_elem(OP_LIST, $<opval>4, $5), $1));
f05e27e5 765 }
a687059c 766 ;
767
891be019 768/* Names of methods. May use $object->$methodname */
a0d0e21e 769method : METHOD
770 | scalar
771 ;
772
891be019 773/* Some kind of subscripted expression */
774subscripted: star '{' expr ';' '}' /* *main::{something} */
775 /* In this and all the hash accessors, ';' is
776 * provided by the tokeniser */
264e1af3 777 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
53a7735b 778 PL_parser->expect = XOPERATOR;
f05e27e5 779 TOKEN_GETMAD($2,$$,'{');
780 TOKEN_GETMAD($4,$$,';');
781 TOKEN_GETMAD($5,$$,'}');
782 }
891be019 783 | scalar '[' expr ']' /* $array[$element] */
f05e27e5 784 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
785 TOKEN_GETMAD($2,$$,'[');
786 TOKEN_GETMAD($4,$$,']');
787 }
891be019 788 | term ARROW '[' expr ']' /* somearef->[$element] */
fad39ff1 789 { $$ = newBINOP(OP_AELEM, 0,
790 ref(newAVREF($1),OP_RV2AV),
f05e27e5 791 scalar($4));
792 TOKEN_GETMAD($2,$$,'a');
793 TOKEN_GETMAD($3,$$,'[');
794 TOKEN_GETMAD($5,$$,']');
795 }
891be019 796 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
fad39ff1 797 { $$ = newBINOP(OP_AELEM, 0,
798 ref(newAVREF($1),OP_RV2AV),
f05e27e5 799 scalar($3));
800 TOKEN_GETMAD($2,$$,'[');
801 TOKEN_GETMAD($4,$$,']');
802 }
891be019 803 | scalar '{' expr ';' '}' /* $foo->{bar();} */
fad39ff1 804 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
53a7735b 805 PL_parser->expect = XOPERATOR;
f05e27e5 806 TOKEN_GETMAD($2,$$,'{');
807 TOKEN_GETMAD($4,$$,';');
808 TOKEN_GETMAD($5,$$,'}');
809 }
891be019 810 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
fad39ff1 811 { $$ = newBINOP(OP_HELEM, 0,
812 ref(newHVREF($1),OP_RV2HV),
813 jmaybe($4));
53a7735b 814 PL_parser->expect = XOPERATOR;
f05e27e5 815 TOKEN_GETMAD($2,$$,'a');
816 TOKEN_GETMAD($3,$$,'{');
817 TOKEN_GETMAD($5,$$,';');
818 TOKEN_GETMAD($6,$$,'}');
819 }
891be019 820 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
fad39ff1 821 { $$ = newBINOP(OP_HELEM, 0,
822 ref(newHVREF($1),OP_RV2HV),
823 jmaybe($3));
53a7735b 824 PL_parser->expect = XOPERATOR;
f05e27e5 825 TOKEN_GETMAD($2,$$,'{');
826 TOKEN_GETMAD($4,$$,';');
827 TOKEN_GETMAD($5,$$,'}');
828 }
891be019 829 | term ARROW '(' ')' /* $subref->() */
fad39ff1 830 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
f05e27e5 831 newCVREF(0, scalar($1)));
832 TOKEN_GETMAD($2,$$,'a');
833 TOKEN_GETMAD($3,$$,'(');
834 TOKEN_GETMAD($4,$$,')');
835 }
891be019 836 | term ARROW '(' expr ')' /* $subref->(@args) */
fad39ff1 837 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
838 append_elem(OP_LIST, $4,
f05e27e5 839 newCVREF(0, scalar($1))));
840 TOKEN_GETMAD($2,$$,'a');
841 TOKEN_GETMAD($3,$$,'(');
842 TOKEN_GETMAD($5,$$,')');
843 }
fad39ff1 844
891be019 845 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
fad39ff1 846 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
847 append_elem(OP_LIST, $3,
f05e27e5 848 newCVREF(0, scalar($1))));
849 TOKEN_GETMAD($2,$$,'(');
850 TOKEN_GETMAD($4,$$,')');
851 }
891be019 852 | subscripted '(' ')' /* $foo->{bar}->() */
fad39ff1 853 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
f05e27e5 854 newCVREF(0, scalar($1)));
855 TOKEN_GETMAD($2,$$,'(');
856 TOKEN_GETMAD($3,$$,')');
857 }
9a9798c2 858 | '(' expr ')' '[' expr ']' /* list slice */
f05e27e5 859 { $$ = newSLICEOP(0, $5, $2);
860 TOKEN_GETMAD($1,$$,'(');
861 TOKEN_GETMAD($3,$$,')');
862 TOKEN_GETMAD($4,$$,'[');
863 TOKEN_GETMAD($6,$$,']');
864 }
9a9798c2 865 | '(' ')' '[' expr ']' /* empty list slice! */
bcabcc50 866 { $$ = newSLICEOP(0, $4, (OP*)NULL);
f05e27e5 867 TOKEN_GETMAD($1,$$,'(');
868 TOKEN_GETMAD($2,$$,')');
869 TOKEN_GETMAD($3,$$,'[');
870 TOKEN_GETMAD($5,$$,']');
871 }
891be019 872 ;
fad39ff1 873
891be019 874/* Binary operators between terms */
f05e27e5 875termbinop: term ASSIGNOP term /* $x = $y */
876 { $$ = newASSIGNOP(OPf_STACKED, $1, IVAL($2), $3);
877 TOKEN_GETMAD($2,$$,'o');
878 }
891be019 879 | term POWOP term /* $x ** $y */
f05e27e5 880 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
881 TOKEN_GETMAD($2,$$,'o');
882 }
891be019 883 | term MULOP term /* $x * $y, $x x $y */
f05e27e5 884 { if (IVAL($2) != OP_REPEAT)
79072805 885 scalar($1);
f05e27e5 886 $$ = newBINOP(IVAL($2), 0, $1, scalar($3));
887 TOKEN_GETMAD($2,$$,'o');
888 }
891be019 889 | term ADDOP term /* $x + $y */
f05e27e5 890 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
891 TOKEN_GETMAD($2,$$,'o');
892 }
891be019 893 | term SHIFTOP term /* $x >> $y, $x << $y */
f05e27e5 894 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
895 TOKEN_GETMAD($2,$$,'o');
896 }
891be019 897 | term RELOP term /* $x > $y, etc. */
f05e27e5 898 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
899 TOKEN_GETMAD($2,$$,'o');
900 }
891be019 901 | term EQOP term /* $x == $y, $x eq $y */
f05e27e5 902 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
903 TOKEN_GETMAD($2,$$,'o');
904 }
891be019 905 | term BITANDOP term /* $x & $y */
f05e27e5 906 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
907 TOKEN_GETMAD($2,$$,'o');
908 }
891be019 909 | term BITOROP term /* $x | $y */
f05e27e5 910 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
911 TOKEN_GETMAD($2,$$,'o');
912 }
891be019 913 | term DOTDOT term /* $x..$y, $x...$y */
f05e27e5 914 {
915 $$ = newRANGE(IVAL($2), scalar($1), scalar($3));
035e2bcc 916 DO_MAD({
f05e27e5 917 UNOP *op;
918 op = (UNOP*)$$;
919 op = (UNOP*)op->op_first; /* get to flop */
920 op = (UNOP*)op->op_first; /* get to flip */
921 op = (UNOP*)op->op_first; /* get to range */
922 token_getmad($2,(OP*)op,'o');
035e2bcc 923 })
f05e27e5 924 }
891be019 925 | term ANDAND term /* $x && $y */
f05e27e5 926 { $$ = newLOGOP(OP_AND, 0, $1, $3);
927 TOKEN_GETMAD($2,$$,'o');
928 }
891be019 929 | term OROR term /* $x || $y */
f05e27e5 930 { $$ = newLOGOP(OP_OR, 0, $1, $3);
931 TOKEN_GETMAD($2,$$,'o');
932 }
c963b151 933 | term DORDOR term /* $x // $y */
f05e27e5 934 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
935 TOKEN_GETMAD($2,$$,'o');
936 }
891be019 937 | term MATCHOP term /* $x =~ /$y/ */
f05e27e5 938 { $$ = bind_match(IVAL($2), $1, $3);
939 TOKEN_GETMAD($2,
940 ($$->op_type == OP_NOT
941 ? ((UNOP*)$$)->op_first : $$),
942 '~');
943 }
891be019 944 ;
8d063cd8 945
891be019 946/* Unary operators and terms */
947termunop : '-' term %prec UMINUS /* -$x */
f05e27e5 948 { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
949 TOKEN_GETMAD($1,$$,'o');
950 }
891be019 951 | '+' term %prec UMINUS /* +$x */
f05e27e5 952 { $$ = IF_MAD(
953 newUNOP(OP_NULL, 0, $2),
954 $2
955 );
956 TOKEN_GETMAD($1,$$,'+');
957 }
891be019 958 | '!' term /* !$x */
f05e27e5 959 { $$ = newUNOP(OP_NOT, 0, scalar($2));
960 TOKEN_GETMAD($1,$$,'o');
961 }
891be019 962 | '~' term /* ~$x */
f05e27e5 963 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
964 TOKEN_GETMAD($1,$$,'o');
965 }
891be019 966 | term POSTINC /* $x++ */
79072805 967 { $$ = newUNOP(OP_POSTINC, 0,
f05e27e5 968 mod(scalar($1), OP_POSTINC));
969 TOKEN_GETMAD($2,$$,'o');
970 }
891be019 971 | term POSTDEC /* $x-- */
79072805 972 { $$ = newUNOP(OP_POSTDEC, 0,
f05e27e5 973 mod(scalar($1), OP_POSTDEC));
974 TOKEN_GETMAD($2,$$,'o');
975 }
891be019 976 | PREINC term /* ++$x */
79072805 977 { $$ = newUNOP(OP_PREINC, 0,
f05e27e5 978 mod(scalar($2), OP_PREINC));
979 TOKEN_GETMAD($1,$$,'o');
980 }
891be019 981 | PREDEC term /* --$x */
79072805 982 { $$ = newUNOP(OP_PREDEC, 0,
f05e27e5 983 mod(scalar($2), OP_PREDEC));
984 TOKEN_GETMAD($1,$$,'o');
985 }
891be019 986
987 ;
988
989/* Constructors for anonymous data */
990anonymous: '[' expr ']'
f05e27e5 991 { $$ = newANONLIST($2);
992 TOKEN_GETMAD($1,$$,'[');
993 TOKEN_GETMAD($3,$$,']');
994 }
891be019 995 | '[' ']'
bcabcc50 996 { $$ = newANONLIST((OP*)NULL);
f05e27e5 997 TOKEN_GETMAD($1,$$,'[');
998 TOKEN_GETMAD($2,$$,']');
999 }
891be019 1000 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
f05e27e5 1001 { $$ = newANONHASH($2);
1002 TOKEN_GETMAD($1,$$,'{');
1003 TOKEN_GETMAD($3,$$,';');
1004 TOKEN_GETMAD($4,$$,'}');
1005 }
891be019 1006 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
bcabcc50 1007 { $$ = newANONHASH((OP*)NULL);
f05e27e5 1008 TOKEN_GETMAD($1,$$,'{');
1009 TOKEN_GETMAD($2,$$,';');
1010 TOKEN_GETMAD($3,$$,'}');
1011 }
718a7425 1012 | ANONSUB startanonsub proto subattrlist block %prec '('
5a5094bd 1013 { SvREFCNT_inc_simple_void(PL_compcv);
718a7425 1014 $$ = newANONATTRSUB($2, $3, $4, $5);
f05e27e5 1015 TOKEN_GETMAD($1,$$,'o');
718a7425 1016 OP_GETMAD($3,$$,'s');
1017 OP_GETMAD($4,$$,'a');
f05e27e5 1018 }
891be019 1019
1020 ;
1021
1022/* Things called with "do" */
1023termdo : DO term %prec UNIOP /* do $filename */
123d08c9 1024 { $$ = dofile($2, IVAL($1));
f05e27e5 1025 TOKEN_GETMAD($1,$$,'o');
1026 }
891be019 1027 | DO block %prec '(' /* do { code */
f05e27e5 1028 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2));
1029 TOKEN_GETMAD($1,$$,'D');
1030 }
891be019 1031 | DO WORD '(' ')' /* do somesub() */
1032 { $$ = newUNOP(OP_ENTERSUB,
1033 OPf_SPECIAL|OPf_STACKED,
1034 prepend_elem(OP_LIST,
1035 scalar(newCVREF(
1036 (OPpENTERSUB_AMPER<<8),
1037 scalar($2)
bcabcc50 1038 )),(OP*)NULL)); dep();
f05e27e5 1039 TOKEN_GETMAD($1,$$,'o');
1040 TOKEN_GETMAD($3,$$,'(');
1041 TOKEN_GETMAD($4,$$,')');
1042 }
891be019 1043 | DO WORD '(' expr ')' /* do somesub(@args) */
1044 { $$ = newUNOP(OP_ENTERSUB,
1045 OPf_SPECIAL|OPf_STACKED,
1046 append_elem(OP_LIST,
1047 $4,
1048 scalar(newCVREF(
1049 (OPpENTERSUB_AMPER<<8),
1050 scalar($2)
f05e27e5 1051 )))); dep();
1052 TOKEN_GETMAD($1,$$,'o');
1053 TOKEN_GETMAD($3,$$,'(');
1054 TOKEN_GETMAD($5,$$,')');
1055 }
891be019 1056 | DO scalar '(' ')' /* do $subref () */
1057 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1058 prepend_elem(OP_LIST,
bcabcc50 1059 scalar(newCVREF(0,scalar($2))), (OP*)NULL)); dep();
f05e27e5 1060 TOKEN_GETMAD($1,$$,'o');
1061 TOKEN_GETMAD($3,$$,'(');
1062 TOKEN_GETMAD($4,$$,')');
1063 }
891be019 1064 | DO scalar '(' expr ')' /* do $subref (@args) */
1065 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1066 prepend_elem(OP_LIST,
1067 $4,
f05e27e5 1068 scalar(newCVREF(0,scalar($2))))); dep();
1069 TOKEN_GETMAD($1,$$,'o');
1070 TOKEN_GETMAD($3,$$,'(');
1071 TOKEN_GETMAD($5,$$,')');
1072 }
891be019 1073
1074 ;
1075
1076term : termbinop
1077 | termunop
1078 | anonymous
1079 | termdo
e9bdcc27 1080 | term '?' term ':' term
f05e27e5 1081 { $$ = newCONDOP(0, $1, $3, $5);
1082 TOKEN_GETMAD($2,$$,'?');
1083 TOKEN_GETMAD($4,$$,':');
1084 }
891be019 1085 | REFGEN term /* \$x, \@y, \%z */
f05e27e5 1086 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN));
1087 TOKEN_GETMAD($1,$$,'o');
1088 }
09bef843 1089 | myattrterm %prec UNIOP
1090 { $$ = $1; }
1091 | LOCAL term %prec UNIOP
f05e27e5 1092 { $$ = localize($2,IVAL($1));
dde83331 1093 TOKEN_GETMAD($1,$$,'k');
f05e27e5 1094 }
a0d0e21e 1095 | '(' expr ')'
f05e27e5 1096 { $$ = sawparens(IF_MAD(newUNOP(OP_NULL,0,$2), $2));
1097 TOKEN_GETMAD($1,$$,'(');
1098 TOKEN_GETMAD($3,$$,')');
1099 }
8d063cd8 1100 | '(' ')'
f05e27e5 1101 { $$ = sawparens(newNULLLIST());
1102 TOKEN_GETMAD($1,$$,'(');
1103 TOKEN_GETMAD($2,$$,')');
1104 }
79072805 1105 | scalar %prec '('
8d063cd8 1106 { $$ = $1; }
79072805 1107 | star %prec '('
8d063cd8 1108 { $$ = $1; }
79072805 1109 | hsh %prec '('
8d063cd8 1110 { $$ = $1; }
79072805 1111 | ary %prec '('
8d063cd8 1112 { $$ = $1; }
891be019 1113 | arylen %prec '(' /* $#x, $#{ something } */
79072805 1114 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
fad39ff1 1115 | subscripted
1116 { $$ = $1; }
891be019 1117 | ary '[' expr ']' /* array slice */
79072805 1118 { $$ = prepend_elem(OP_ASLICE,
1119 newOP(OP_PUSHMARK, 0),
79072805 1120 newLISTOP(OP_ASLICE, 0,
1121 list($3),
f05e27e5 1122 ref($1, OP_ASLICE)));
1123 TOKEN_GETMAD($2,$$,'[');
1124 TOKEN_GETMAD($4,$$,']');
1125 }
891be019 1126 | ary '{' expr ';' '}' /* @hash{@keys} */
79072805 1127 { $$ = prepend_elem(OP_HSLICE,
1128 newOP(OP_PUSHMARK, 0),
79072805 1129 newLISTOP(OP_HSLICE, 0,
1130 list($3),
a0d0e21e 1131 ref(oopsHV($1), OP_HSLICE)));
53a7735b 1132 PL_parser->expect = XOPERATOR;
f05e27e5 1133 TOKEN_GETMAD($2,$$,'{');
1134 TOKEN_GETMAD($4,$$,';');
1135 TOKEN_GETMAD($5,$$,'}');
1136 }
79072805 1137 | THING %prec '('
1138 { $$ = $1; }
891be019 1139 | amper /* &foo; */
c07a80fd 1140 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
ecb2f335 1141 | amper '(' ')' /* &foo() */
f05e27e5 1142 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1143 TOKEN_GETMAD($2,$$,'(');
1144 TOKEN_GETMAD($3,$$,')');
1145 }
891be019 1146 | amper '(' expr ')' /* &foo(@args) */
f05e27e5 1147 {
1148 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1149 append_elem(OP_LIST, $3, scalar($1)));
035e2bcc 1150 DO_MAD({
f05e27e5 1151 OP* op = $$;
1152 if (op->op_type == OP_CONST) { /* defeat const fold */
1153 op = (OP*)op->op_madprop->mad_val;
1154 }
1155 token_getmad($2,op,'(');
1156 token_getmad($4,op,')');
035e2bcc 1157 })
f05e27e5 1158 }
891be019 1159 | NOAMP WORD listexpr /* foo(@args) */
a0d0e21e 1160 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
f05e27e5 1161 append_elem(OP_LIST, $3, scalar($2)));
1162 TOKEN_GETMAD($1,$$,'o');
1163 }
891be019 1164 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
f05e27e5 1165 { $$ = newOP(IVAL($1), OPf_SPECIAL);
1166 PL_hints |= HINT_BLOCK_SCOPE;
1167 TOKEN_GETMAD($1,$$,'o');
1168 }
a0d0e21e 1169 | LOOPEX term
f05e27e5 1170 { $$ = newLOOPEX(IVAL($1),$2);
1171 TOKEN_GETMAD($1,$$,'o');
1172 }
891be019 1173 | NOTOP argexpr /* not $foo */
f05e27e5 1174 { $$ = newUNOP(OP_NOT, 0, scalar($2));
1175 TOKEN_GETMAD($1,$$,'o');
1176 }
891be019 1177 | UNIOP /* Unary op, $_ implied */
f05e27e5 1178 { $$ = newOP(IVAL($1), 0);
1179 TOKEN_GETMAD($1,$$,'o');
1180 }
1181 | UNIOP block /* eval { foo }* */
1182 { $$ = newUNOP(IVAL($1), 0, $2);
1183 TOKEN_GETMAD($1,$$,'o');
1184 }
891be019 1185 | UNIOP term /* Unary op */
f05e27e5 1186 { $$ = newUNOP(IVAL($1), 0, $2);
1187 TOKEN_GETMAD($1,$$,'o');
1188 }
d2fdf8fd 1189 | REQUIRE /* require, $_ implied */
1190 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0);
1191 TOKEN_GETMAD($1,$$,'o');
1192 }
1193 | REQUIRE term /* require Foo */
1194 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2);
1195 TOKEN_GETMAD($1,$$,'o');
1196 }
3cd0a11a 1197 | UNIOPSUB
1198 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
891be019 1199 | UNIOPSUB term /* Sub treated as unop */
4633a7c4 1200 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1201 append_elem(OP_LIST, $2, scalar($1))); }
891be019 1202 | FUNC0 /* Nullary operator */
f05e27e5 1203 { $$ = newOP(IVAL($1), 0);
1204 TOKEN_GETMAD($1,$$,'o');
1205 }
ae986130 1206 | FUNC0 '(' ')'
f05e27e5 1207 { $$ = newOP(IVAL($1), 0);
1208 TOKEN_GETMAD($1,$$,'o');
1209 TOKEN_GETMAD($2,$$,'(');
1210 TOKEN_GETMAD($3,$$,')');
1211 }
891be019 1212 | FUNC0SUB /* Sub treated as nullop */
28757baa 1213 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
4633a7c4 1214 scalar($1)); }
891be019 1215 | FUNC1 '(' ')' /* not () */
f05e27e5 1216 { $$ = (IVAL($1) == OP_NOT)
1217 ? newUNOP(IVAL($1), 0, newSVOP(OP_CONST, 0, newSViv(0)))
1218 : newOP(IVAL($1), OPf_SPECIAL);
1219
1220 TOKEN_GETMAD($1,$$,'o');
1221 TOKEN_GETMAD($2,$$,'(');
1222 TOKEN_GETMAD($3,$$,')');
1223 }
891be019 1224 | FUNC1 '(' expr ')' /* not($foo) */
f05e27e5 1225 { $$ = newUNOP(IVAL($1), 0, $3);
1226 TOKEN_GETMAD($1,$$,'o');
1227 TOKEN_GETMAD($2,$$,'(');
1228 TOKEN_GETMAD($4,$$,')');
1229 }
1230 | PMFUNC '(' argexpr ')' /* m//, s///, tr/// */
1231 { $$ = pmruntime($1, $3, 1);
1232 TOKEN_GETMAD($2,$$,'(');
1233 TOKEN_GETMAD($4,$$,')');
1234 }
79072805 1235 | WORD
378cc40b 1236 | listop
be25f609 1237 | YADAYADA
1238 {
1239 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
1240 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
1241 }
8d063cd8 1242 ;
1243
891be019 1244/* "my" declarations, with optional attributes */
09bef843 1245myattrterm: MY myterm myattrlist
f05e27e5 1246 { $$ = my_attrs($2,$3);
1247 DO_MAD(
1248 token_getmad($1,$$,'d');
1249 append_madprops($3->op_madprop, $$, 'a');
1250 $3->op_madprop = 0;
1251 )
1252 }
09bef843 1253 | MY myterm
f05e27e5 1254 { $$ = localize($2,IVAL($1));
1255 TOKEN_GETMAD($1,$$,'d');
1256 }
09bef843 1257 ;
1258
891be019 1259/* Things that can be "my"'d */
09bef843 1260myterm : '(' expr ')'
f05e27e5 1261 { $$ = sawparens($2);
1262 TOKEN_GETMAD($1,$$,'(');
1263 TOKEN_GETMAD($3,$$,')');
1264 }
09bef843 1265 | '(' ')'
f05e27e5 1266 { $$ = sawparens(newNULLLIST());
1267 TOKEN_GETMAD($1,$$,'(');
1268 TOKEN_GETMAD($2,$$,')');
1269 }
09bef843 1270 | scalar %prec '('
1271 { $$ = $1; }
1272 | hsh %prec '('
1273 { $$ = $1; }
1274 | ary %prec '('
1275 { $$ = $1; }
1276 ;
1277
891be019 1278/* Basic list expressions */
fad39ff1 1279listexpr: /* NULL */ %prec PREC_LOW
bcabcc50 1280 { $$ = (OP*)NULL; }
fad39ff1 1281 | argexpr %prec PREC_LOW
a0d0e21e 1282 { $$ = $1; }
1283 ;
1284
1285listexprcom: /* NULL */
bcabcc50 1286 { $$ = (OP*)NULL; }
79072805 1287 | expr
1288 { $$ = $1; }
a0d0e21e 1289 | expr ','
f05e27e5 1290 {
1291#ifdef MAD
1292 OP* op = newNULLLIST();
1293 token_getmad($2,op,',');
1294 $$ = append_elem(OP_LIST, $1, op);
1295#else
1296 $$ = $1;
1297#endif
1298
1299 }
79072805 1300 ;
1301
891be019 1302/* A little bit of trickery to make "for my $foo (@bar)" actually be
1303 lexical */
55497cff 1304my_scalar: scalar
12bd6ede 1305 { PL_parser->in_my = 0; $$ = my($1); }
55497cff 1306 ;
1307
79072805 1308amper : '&' indirob
f05e27e5 1309 { $$ = newCVREF(IVAL($1),$2);
1310 TOKEN_GETMAD($1,$$,'&');
1311 }
a687059c 1312 ;
1313
79072805 1314scalar : '$' indirob
f05e27e5 1315 { $$ = newSVREF($2);
1316 TOKEN_GETMAD($1,$$,'$');
1317 }
a687059c 1318 ;
1319
79072805 1320ary : '@' indirob
f05e27e5 1321 { $$ = newAVREF($2);
1322 TOKEN_GETMAD($1,$$,'@');
1323 }
79072805 1324 ;
1325
1326hsh : '%' indirob
f05e27e5 1327 { $$ = newHVREF($2);
1328 TOKEN_GETMAD($1,$$,'%');
1329 }
79072805 1330 ;
1331
1332arylen : DOLSHARP indirob
f05e27e5 1333 { $$ = newAVREF($2);
1334 TOKEN_GETMAD($1,$$,'l');
1335 }
79072805 1336 ;
1337
1338star : '*' indirob
f05e27e5 1339 { $$ = newGVREF(0,$2);
1340 TOKEN_GETMAD($1,$$,'*');
1341 }
79072805 1342 ;
1343
891be019 1344/* Indirect objects */
79072805 1345indirob : WORD
1346 { $$ = scalar($1); }
fad39ff1 1347 | scalar %prec PREC_LOW
f05e27e5 1348 { $$ = scalar($1); }
79072805 1349 | block
a0d0e21e 1350 { $$ = scope($1); }
79072805 1351
93a17b20 1352 | PRIVATEREF
1353 { $$ = $1; }
8d063cd8 1354 ;