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