This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Enable v (verbose) switch on -Dp to display the top 8 elements
[perl5.git] / perly.y
CommitLineData
a0d0e21e 1/* perly.y
a687059c 2 *
0de566d7 3 * Copyright (c) 1991-2002, 2003, 2004 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.'
8d063cd8
LW
13 */
14
0de566d7 15/* Make the parser re-entrant. */
8d063cd8 16
0de566d7 17%pure_parser
8d063cd8 18
0de566d7 19%start prog
9d116dd7 20
8d063cd8 21%union {
79072805
LW
22 I32 ival;
23 char *pval;
24 OP *opval;
25 GV *gvval;
8d063cd8
LW
26}
27
fad39ff1 28%token <ival> '{'
f0fcb552 29
a0d0e21e 30%token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
4633a7c4 31%token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
79072805 32%token <pval> LABEL
a0d0e21e 33%token <ival> FORMAT SUB ANONSUB PACKAGE USE
79072805
LW
34%token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
35%token <ival> LOOPEX DOTDOT
36477c24 36%token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP
79072805 37%token <ival> RELOP EQOP MULOP ADDOP
55497cff 38%token <ival> DOLSHARP DO HASHBRACK NOAMP
09bef843
SB
39%token <ival> LOCAL MY MYSUB
40%token COLONATTR
79072805 41
09bef843 42%type <ival> prog decl format startsub startanonsub startformsub
ecb2f335 43%type <ival> progstart remember mremember '&'
bbce6d69 44%type <opval> block mblock lineseq line loop cond else
fad39ff1 45%type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
bbce6d69 46%type <opval> argexpr nexpr texpr iexpr mexpr mnexpr mtexpr miexpr
44a8e56a
PP
47%type <opval> listexpr listexprcom indirob listop method
48%type <opval> formname subname proto subbody cont my_scalar
09bef843 49%type <opval> subattrlist myattrlist mysubrout myattrterm myterm
891be019 50%type <opval> termbinop termunop anonymous termdo
79072805 51%type <pval> label
79072805 52
fad39ff1
SM
53%nonassoc PREC_LOW
54%nonassoc LOOPEX
55
c963b151 56%left <ival> OROP DOROP
463ee0b2 57%left ANDOP
c07a80fd 58%right NOTOP
36477c24 59%nonassoc LSTOP LSTOPSUB
8d063cd8 60%left ','
a0d0e21e 61%right <ival> ASSIGNOP
8d063cd8
LW
62%right '?' ':'
63%nonassoc DOTDOT
c963b151 64%left OROR DORDOR
8d063cd8 65%left ANDAND
79072805
LW
66%left <ival> BITOROP
67%left <ival> BITANDOP
a687059c
LW
68%nonassoc EQOP
69%nonassoc RELOP
36477c24 70%nonassoc UNIOP UNIOPSUB
79072805 71%left <ival> SHIFTOP
a687059c
LW
72%left ADDOP
73%left MULOP
8990e307 74%left <ival> MATCHOP
79072805
LW
75%right '!' '~' UMINUS REFGEN
76%right <ival> POWOP
77%nonassoc PREINC PREDEC POSTINC POSTDEC
8990e307 78%left ARROW
fad39ff1 79%nonassoc <ival> ')'
8d063cd8 80%left '('
fad39ff1 81%left '[' '{'
8d063cd8
LW
82
83%% /* RULES */
84
891be019 85/* The whole program */
ecb2f335 86prog : progstart
ae986130 87 /*CONTINUED*/ lineseq
ecb2f335 88 { $$ = $1; newPROG(block_end($1,$2)); }
8d063cd8
LW
89 ;
90
891be019 91/* An ordinary block */
a687059c 92block : '{' remember lineseq '}'
3280af22 93 { if (PL_copline > (line_t)$1)
eb160463 94 PL_copline = (line_t)$1;
36477c24 95 $$ = block_end($2, $3); }
a0d0e21e
LW
96 ;
97
55497cff
PP
98remember: /* NULL */ /* start a full lexical scope */
99 { $$ = block_start(TRUE); }
100 ;
101
ecb2f335
NIS
102progstart:
103 {
ecb2f335
NIS
104 PL_expect = XSTATE; $$ = block_start(TRUE);
105 }
106 ;
107
108
bbce6d69 109mblock : '{' mremember lineseq '}'
3280af22 110 { if (PL_copline > (line_t)$1)
eb160463 111 PL_copline = (line_t)$1;
36477c24 112 $$ = block_end($2, $3); }
55497cff
PP
113 ;
114
115mremember: /* NULL */ /* start a partial lexical scope */
116 { $$ = block_start(FALSE); }
8d063cd8
LW
117 ;
118
891be019 119/* A collection of "lines" in the program */
8d063cd8 120lineseq : /* NULL */
79072805
LW
121 { $$ = Nullop; }
122 | lineseq decl
123 { $$ = $1; }
8d063cd8 124 | lineseq line
463ee0b2 125 { $$ = append_list(OP_LINESEQ,
a0d0e21e 126 (LISTOP*)$1, (LISTOP*)$2);
3280af22
NIS
127 PL_pad_reset_pending = TRUE;
128 if ($1 && $2) PL_hints |= HINT_BLOCK_SCOPE; }
8d063cd8
LW
129 ;
130
891be019 131/* A "line" in the program */
79072805
LW
132line : label cond
133 { $$ = newSTATEOP(0, $1, $2); }
8d063cd8
LW
134 | loop /* loops add their own labels */
135 | label ';'
136 { if ($1 != Nullch) {
79072805 137 $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0));
450a55e4
LW
138 }
139 else {
79072805 140 $$ = Nullop;
3280af22 141 PL_copline = NOLINE;
32c2e4fb 142 }
3280af22 143 PL_expect = XSTATE; }
8d063cd8 144 | label sideff ';'
79072805 145 { $$ = newSTATEOP(0, $1, $2);
3280af22 146 PL_expect = XSTATE; }
8d063cd8
LW
147 ;
148
891be019 149/* An expression which may have a side-effect */
a687059c 150sideff : error
79072805 151 { $$ = Nullop; }
a687059c 152 | expr
79072805 153 { $$ = $1; }
a687059c 154 | expr IF expr
79072805 155 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
a687059c 156 | expr UNLESS expr
79072805 157 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
a687059c 158 | expr WHILE expr
8990e307 159 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
55497cff
PP
160 | expr UNTIL iexpr
161 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);}
ecca16b0 162 | expr FOR expr
eb160463 163 { $$ = newFOROP(0, Nullch, (line_t)$2,
ecca16b0 164 Nullop, $3, $1, Nullop); }
79072805
LW
165 ;
166
891be019 167/* else and elsif blocks */
79072805
LW
168else : /* NULL */
169 { $$ = Nullop; }
55497cff 170 | ELSE mblock
38a230cb 171 { ($2)->op_flags |= OPf_PARENS; $$ = scope($2); }
55497cff 172 | ELSIF '(' mexpr ')' mblock else
eb160463 173 { PL_copline = (line_t)$1;
2c15bef3 174 $$ = newCONDOP(0, $3, scope($5), $6);
3280af22 175 PL_hints |= HINT_BLOCK_SCOPE; }
79072805
LW
176 ;
177
891be019 178/* Real conditional expressions */
55497cff 179cond : IF '(' remember mexpr ')' mblock else
eb160463 180 { PL_copline = (line_t)$1;
36477c24 181 $$ = block_end($3,
bbce6d69 182 newCONDOP(0, $4, scope($6), $7)); }
55497cff 183 | UNLESS '(' remember miexpr ')' mblock else
eb160463 184 { PL_copline = (line_t)$1;
36477c24 185 $$ = block_end($3,
bbce6d69 186 newCONDOP(0, $4, scope($6), $7)); }
79072805
LW
187 ;
188
891be019 189/* Continue blocks */
79072805
LW
190cont : /* NULL */
191 { $$ = Nullop; }
192 | CONTINUE block
193 { $$ = scope($2); }
194 ;
195
891be019 196/* Loops: while, until, for, and a bare block */
55497cff 197loop : label WHILE '(' remember mtexpr ')' mblock cont
eb160463 198 { PL_copline = (line_t)$2;
36477c24 199 $$ = block_end($4,
55497cff 200 newSTATEOP(0, $1,
bbce6d69 201 newWHILEOP(0, 1, (LOOP*)Nullop,
fb73857a 202 $2, $5, $7, $8))); }
55497cff 203 | label UNTIL '(' remember miexpr ')' mblock cont
eb160463 204 { PL_copline = (line_t)$2;
36477c24 205 $$ = block_end($4,
55497cff 206 newSTATEOP(0, $1,
bbce6d69 207 newWHILEOP(0, 1, (LOOP*)Nullop,
fb73857a 208 $2, $5, $7, $8))); }
bbce6d69 209 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
36477c24 210 { $$ = block_end($4,
eb160463 211 newFOROP(0, $1, (line_t)$2, $5, $7, $9, $10)); }
bbce6d69 212 | label FOR scalar '(' remember mexpr ')' mblock cont
36477c24 213 { $$ = block_end($5,
eb160463 214 newFOROP(0, $1, (line_t)$2, mod($3, OP_ENTERLOOP),
bbce6d69
PP
215 $6, $8, $9)); }
216 | label FOR '(' remember mexpr ')' mblock cont
36477c24 217 { $$ = block_end($4,
eb160463 218 newFOROP(0, $1, (line_t)$2, Nullop, $5, $7, $8)); }
bbce6d69 219 | label FOR '(' remember mnexpr ';' mtexpr ';' mnexpr ')' mblock
8d063cd8 220 /* basically fake up an initialize-while lineseq */
36c66720 221 { OP *forop;
eb160463 222 PL_copline = (line_t)$2;
36c66720
RH
223 forop = newSTATEOP(0, $1,
224 newWHILEOP(0, 1, (LOOP*)Nullop,
225 $2, scalar($7),
226 $11, $9));
227 if ($5) {
228 forop = append_elem(OP_LINESEQ,
229 newSTATEOP(0, ($1?savepv($1):Nullch),
230 $5),
231 forop);
232 }
233
234 $$ = block_end($4, forop); }
79072805 235 | label block cont /* a block is a loop that happens once */
fb73857a
PP
236 { $$ = newSTATEOP(0, $1,
237 newWHILEOP(0, 1, (LOOP*)Nullop,
238 NOLINE, Nullop, $2, $3)); }
8d063cd8
LW
239 ;
240
891be019 241/* Normal expression */
8d063cd8 242nexpr : /* NULL */
79072805 243 { $$ = Nullop; }
8d063cd8
LW
244 | sideff
245 ;
246
891be019 247/* Boolean expression */
8d063cd8 248texpr : /* NULL means true */
b73d6f50 249 { (void)scan_num("1", &yylval); $$ = yylval.opval; }
8d063cd8
LW
250 | expr
251 ;
252
891be019 253/* Inverted boolean expression */
55497cff
PP
254iexpr : expr
255 { $$ = invert(scalar($1)); }
256 ;
257
891be019 258/* Expression with its own lexical scope */
55497cff 259mexpr : expr
bbce6d69
PP
260 { $$ = $1; intro_my(); }
261 ;
262
263mnexpr : nexpr
264 { $$ = $1; intro_my(); }
55497cff
PP
265 ;
266
267mtexpr : texpr
bbce6d69 268 { $$ = $1; intro_my(); }
55497cff
PP
269 ;
270
271miexpr : iexpr
bbce6d69 272 { $$ = $1; intro_my(); }
55497cff
PP
273 ;
274
891be019 275/* Optional "MAIN:"-style loop labels */
8d063cd8
LW
276label : /* empty */
277 { $$ = Nullch; }
32c2e4fb 278 | LABEL
8d063cd8
LW
279 ;
280
891be019 281/* Some kind of declaration - does not take part in the parse tree */
8d063cd8
LW
282decl : format
283 { $$ = 0; }
284 | subrout
285 { $$ = 0; }
09bef843
SB
286 | mysubrout
287 { $$ = 0; }
a687059c
LW
288 | package
289 { $$ = 0; }
a0d0e21e 290 | use
85e6fe83 291 { $$ = 0; }
8d063cd8
LW
292 ;
293
44a8e56a 294format : FORMAT startformsub formname block
a0d0e21e 295 { newFORM($2, $3, $4); }
44a8e56a
PP
296 ;
297
298formname: WORD { $$ = $1; }
299 | /* NULL */ { $$ = Nullop; }
8d063cd8
LW
300 ;
301
891be019 302/* Unimplemented "my sub foo { }" */
09bef843
SB
303mysubrout: MYSUB startsub subname proto subattrlist subbody
304 { newMYSUB($2, $3, $4, $5, $6); }
305 ;
306
891be019 307/* Subroutine definition */
09bef843
SB
308subrout : SUB startsub subname proto subattrlist subbody
309 { newATTRSUB($2, $3, $4, $5, $6); }
28757baa
PP
310 ;
311
fa83b5b6 312startsub: /* NULL */ /* start a regular subroutine scope */
774d564b 313 { $$ = start_subparse(FALSE, 0); }
28757baa
PP
314 ;
315
316startanonsub: /* NULL */ /* start an anonymous subroutine scope */
774d564b 317 { $$ = start_subparse(FALSE, CVf_ANON); }
28757baa
PP
318 ;
319
44a8e56a 320startformsub: /* NULL */ /* start a format subroutine scope */
774d564b 321 { $$ = start_subparse(TRUE, 0); }
44a8e56a
PP
322 ;
323
891be019 324/* Name of a subroutine - must be a bareword, could be special */
2d8e6c8d 325subname : WORD { STRLEN n_a; char *name = SvPV(((SVOP*)$1)->op_sv,n_a);
e858de61 326 if (strEQ(name, "BEGIN") || strEQ(name, "END")
7d30b5c4 327 || strEQ(name, "INIT") || strEQ(name, "CHECK"))
1aff0e91 328 CvSPECIAL_on(PL_compcv);
28757baa 329 $$ = $1; }
a0d0e21e
LW
330 ;
331
891be019 332/* Subroutine prototype */
4633a7c4
LW
333proto : /* NULL */
334 { $$ = Nullop; }
335 | THING
336 ;
28757baa 337
891be019 338/* Optional list of subroutine attributes */
09bef843
SB
339subattrlist: /* NULL */
340 { $$ = Nullop; }
341 | COLONATTR THING
342 { $$ = $2; }
343 | COLONATTR
344 { $$ = Nullop; }
345 ;
346
891be019 347/* List of attributes for a "my" variable declaration */
09bef843
SB
348myattrlist: COLONATTR THING
349 { $$ = $2; }
350 | COLONATTR
351 { $$ = Nullop; }
352 ;
353
891be019 354/* Subroutine body - either null or a block */
28757baa 355subbody : block { $$ = $1; }
3280af22 356 | ';' { $$ = Nullop; PL_expect = XSTATE; }
8d063cd8
LW
357 ;
358
a687059c 359package : PACKAGE WORD ';'
79072805 360 { package($2); }
a687059c
LW
361 ;
362
28757baa 363use : USE startsub
1aff0e91 364 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
28757baa
PP
365 WORD WORD listexpr ';'
366 { utilize($1, $2, $4, $5, $6); }
85e6fe83
LW
367 ;
368
891be019 369/* Ordinary expressions; logical combinations */
a0d0e21e
LW
370expr : expr ANDOP expr
371 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
372 | expr OROP expr
373 { $$ = newLOGOP($2, 0, $1, $3); }
c963b151
BD
374 | expr DOROP expr
375 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
fad39ff1 376 | argexpr %prec PREC_LOW
a0d0e21e
LW
377 ;
378
891be019 379/* Expressions are a list of terms joined by commas */
a0d0e21e
LW
380argexpr : argexpr ','
381 { $$ = $1; }
382 | argexpr ',' term
79072805 383 { $$ = append_elem(OP_LIST, $1, $3); }
fad39ff1 384 | term %prec PREC_LOW
8d063cd8
LW
385 ;
386
891be019
SC
387/* List operators */
388listop : LSTOP indirob argexpr /* print $fh @args */
79072805 389 { $$ = convert($1, OPf_STACKED,
a0d0e21e 390 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
891be019 391 | FUNC '(' indirob expr ')' /* print ($fh @args */
79072805 392 { $$ = convert($1, OPf_STACKED,
a0d0e21e 393 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
891be019 394 | term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
4633a7c4 395 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
a0d0e21e 396 append_elem(OP_LIST,
55497cff 397 prepend_elem(OP_LIST, scalar($1), $5),
a0d0e21e 398 newUNOP(OP_METHOD, 0, $3))); }
891be019 399 | term ARROW method /* $foo->bar */
b1524f17
SM
400 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
401 append_elem(OP_LIST, scalar($1),
402 newUNOP(OP_METHOD, 0, $3))); }
891be019 403 | METHOD indirob listexpr /* new Class @args */
4633a7c4 404 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
a0d0e21e 405 append_elem(OP_LIST,
4633a7c4 406 prepend_elem(OP_LIST, $2, $3),
a0d0e21e 407 newUNOP(OP_METHOD, 0, $1))); }
891be019 408 | FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
4633a7c4 409 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
a0d0e21e 410 append_elem(OP_LIST,
4633a7c4 411 prepend_elem(OP_LIST, $2, $4),
a0d0e21e 412 newUNOP(OP_METHOD, 0, $1))); }
891be019 413 | LSTOP listexpr /* print @args */
79072805 414 { $$ = convert($1, 0, $2); }
891be019 415 | FUNC '(' listexprcom ')' /* print (@args) */
79072805 416 { $$ = convert($1, 0, $3); }
ecb2f335 417 | LSTOPSUB startanonsub block /* map { foo } ... */
09bef843 418 { $3 = newANONATTRSUB($2, 0, Nullop, $3); }
891be019 419 listexpr %prec LSTOP /* ... @bar */
4633a7c4 420 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
28757baa
PP
421 append_elem(OP_LIST,
422 prepend_elem(OP_LIST, $3, $5), $1)); }
a687059c
LW
423 ;
424
891be019 425/* Names of methods. May use $object->$methodname */
a0d0e21e
LW
426method : METHOD
427 | scalar
428 ;
429
891be019
SC
430/* Some kind of subscripted expression */
431subscripted: star '{' expr ';' '}' /* *main::{something} */
432 /* In this and all the hash accessors, ';' is
433 * provided by the tokeniser */
264e1af3
RGS
434 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
435 PL_expect = XOPERATOR; }
891be019 436 | scalar '[' expr ']' /* $array[$element] */
fad39ff1 437 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
891be019 438 | term ARROW '[' expr ']' /* somearef->[$element] */
fad39ff1
SM
439 { $$ = newBINOP(OP_AELEM, 0,
440 ref(newAVREF($1),OP_RV2AV),
441 scalar($4));}
891be019 442 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
fad39ff1
SM
443 { $$ = newBINOP(OP_AELEM, 0,
444 ref(newAVREF($1),OP_RV2AV),
445 scalar($3));}
891be019 446 | scalar '{' expr ';' '}' /* $foo->{bar();} */
fad39ff1
SM
447 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
448 PL_expect = XOPERATOR; }
891be019 449 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
fad39ff1
SM
450 { $$ = newBINOP(OP_HELEM, 0,
451 ref(newHVREF($1),OP_RV2HV),
452 jmaybe($4));
453 PL_expect = XOPERATOR; }
891be019 454 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
fad39ff1
SM
455 { $$ = newBINOP(OP_HELEM, 0,
456 ref(newHVREF($1),OP_RV2HV),
457 jmaybe($3));
458 PL_expect = XOPERATOR; }
891be019 459 | term ARROW '(' ')' /* $subref->() */
fad39ff1
SM
460 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
461 newCVREF(0, scalar($1))); }
891be019 462 | term ARROW '(' expr ')' /* $subref->(@args) */
fad39ff1
SM
463 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
464 append_elem(OP_LIST, $4,
465 newCVREF(0, scalar($1)))); }
466
891be019 467 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
fad39ff1
SM
468 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
469 append_elem(OP_LIST, $3,
470 newCVREF(0, scalar($1)))); }
891be019 471 | subscripted '(' ')' /* $foo->{bar}->() */
fad39ff1
SM
472 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
473 newCVREF(0, scalar($1))); }
891be019 474 ;
fad39ff1 475
891be019
SC
476/* Binary operators between terms */
477termbinop : term ASSIGNOP term /* $x = $y */
a0d0e21e 478 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
891be019 479 | term POWOP term /* $x ** $y */
79072805 480 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 481 | term MULOP term /* $x * $y, $x x $y */
79072805
LW
482 { if ($2 != OP_REPEAT)
483 scalar($1);
484 $$ = newBINOP($2, 0, $1, scalar($3)); }
891be019 485 | term ADDOP term /* $x + $y */
79072805 486 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 487 | term SHIFTOP term /* $x >> $y, $x << $y */
79072805 488 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 489 | term RELOP term /* $x > $y, etc. */
79072805 490 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 491 | term EQOP term /* $x == $y, $x eq $y */
79072805 492 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 493 | term BITANDOP term /* $x & $y */
79072805 494 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 495 | term BITOROP term /* $x | $y */
79072805 496 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 497 | term DOTDOT term /* $x..$y, $x...$y */
79072805 498 { $$ = newRANGE($2, scalar($1), scalar($3));}
891be019 499 | term ANDAND term /* $x && $y */
463ee0b2 500 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
891be019 501 | term OROR term /* $x || $y */
463ee0b2 502 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
c963b151
BD
503 | term DORDOR term /* $x // $y */
504 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
891be019 505 | term MATCHOP term /* $x =~ /$y/ */
79072805 506 { $$ = bind_match($2, $1, $3); }
891be019 507 ;
8d063cd8 508
891be019
SC
509/* Unary operators and terms */
510termunop : '-' term %prec UMINUS /* -$x */
79072805 511 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
891be019 512 | '+' term %prec UMINUS /* +$x */
a687059c 513 { $$ = $2; }
891be019 514 | '!' term /* !$x */
79072805 515 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
891be019 516 | '~' term /* ~$x */
79072805 517 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
891be019 518 | term POSTINC /* $x++ */
79072805 519 { $$ = newUNOP(OP_POSTINC, 0,
463ee0b2 520 mod(scalar($1), OP_POSTINC)); }
891be019 521 | term POSTDEC /* $x-- */
79072805 522 { $$ = newUNOP(OP_POSTDEC, 0,
463ee0b2 523 mod(scalar($1), OP_POSTDEC)); }
891be019 524 | PREINC term /* ++$x */
79072805 525 { $$ = newUNOP(OP_PREINC, 0,
463ee0b2 526 mod(scalar($2), OP_PREINC)); }
891be019 527 | PREDEC term /* --$x */
79072805 528 { $$ = newUNOP(OP_PREDEC, 0,
463ee0b2 529 mod(scalar($2), OP_PREDEC)); }
891be019
SC
530
531 ;
532
533/* Constructors for anonymous data */
534anonymous: '[' expr ']'
535 { $$ = newANONLIST($2); }
536 | '[' ']'
537 { $$ = newANONLIST(Nullop); }
538 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
ecb2f335 539 { $$ = newANONHASH($2); }
891be019
SC
540 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
541 { $$ = newANONHASH(Nullop); }
542 | ANONSUB startanonsub proto subattrlist block %prec '('
543 { $$ = newANONATTRSUB($2, $3, $4, $5); }
544
545 ;
546
547/* Things called with "do" */
548termdo : DO term %prec UNIOP /* do $filename */
549 { $$ = dofile($2); }
550 | DO block %prec '(' /* do { code */
551 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
552 | DO WORD '(' ')' /* do somesub() */
553 { $$ = newUNOP(OP_ENTERSUB,
554 OPf_SPECIAL|OPf_STACKED,
555 prepend_elem(OP_LIST,
556 scalar(newCVREF(
557 (OPpENTERSUB_AMPER<<8),
558 scalar($2)
559 )),Nullop)); dep();}
560 | DO WORD '(' expr ')' /* do somesub(@args) */
561 { $$ = newUNOP(OP_ENTERSUB,
562 OPf_SPECIAL|OPf_STACKED,
563 append_elem(OP_LIST,
564 $4,
565 scalar(newCVREF(
566 (OPpENTERSUB_AMPER<<8),
567 scalar($2)
568 )))); dep();}
569 | DO scalar '(' ')' /* do $subref () */
570 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
571 prepend_elem(OP_LIST,
572 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
573 | DO scalar '(' expr ')' /* do $subref (@args) */
574 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
575 prepend_elem(OP_LIST,
576 $4,
577 scalar(newCVREF(0,scalar($2))))); dep();}
578
579 ;
580
581term : termbinop
582 | termunop
583 | anonymous
584 | termdo
e9bdcc27 585 | term '?' term ':' term
891be019
SC
586 { $$ = newCONDOP(0, $1, $3, $5); }
587 | REFGEN term /* \$x, \@y, \%z */
588 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
09bef843
SB
589 | myattrterm %prec UNIOP
590 { $$ = $1; }
591 | LOCAL term %prec UNIOP
93a17b20 592 { $$ = localize($2,$1); }
a0d0e21e 593 | '(' expr ')'
79072805 594 { $$ = sawparens($2); }
8d063cd8 595 | '(' ')'
8990e307 596 { $$ = sawparens(newNULLLIST()); }
79072805 597 | scalar %prec '('
8d063cd8 598 { $$ = $1; }
79072805 599 | star %prec '('
8d063cd8 600 { $$ = $1; }
79072805 601 | hsh %prec '('
8d063cd8 602 { $$ = $1; }
79072805 603 | ary %prec '('
8d063cd8 604 { $$ = $1; }
891be019 605 | arylen %prec '(' /* $#x, $#{ something } */
79072805 606 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
fad39ff1
SM
607 | subscripted
608 { $$ = $1; }
891be019 609 | '(' expr ')' '[' expr ']' /* list slice */
79072805 610 { $$ = newSLICEOP(0, $5, $2); }
891be019 611 | '(' ')' '[' expr ']' /* empty list slice! */
79072805 612 { $$ = newSLICEOP(0, $4, Nullop); }
891be019 613 | ary '[' expr ']' /* array slice */
79072805
LW
614 { $$ = prepend_elem(OP_ASLICE,
615 newOP(OP_PUSHMARK, 0),
79072805
LW
616 newLISTOP(OP_ASLICE, 0,
617 list($3),
a0d0e21e 618 ref($1, OP_ASLICE))); }
891be019 619 | ary '{' expr ';' '}' /* @hash{@keys} */
79072805
LW
620 { $$ = prepend_elem(OP_HSLICE,
621 newOP(OP_PUSHMARK, 0),
79072805
LW
622 newLISTOP(OP_HSLICE, 0,
623 list($3),
a0d0e21e 624 ref(oopsHV($1), OP_HSLICE)));
3280af22 625 PL_expect = XOPERATOR; }
79072805
LW
626 | THING %prec '('
627 { $$ = $1; }
891be019 628 | amper /* &foo; */
c07a80fd 629 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
ecb2f335 630 | amper '(' ')' /* &foo() */
a0d0e21e 631 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
891be019 632 | amper '(' expr ')' /* &foo(@args) */
a0d0e21e 633 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
4633a7c4 634 append_elem(OP_LIST, $3, scalar($1))); }
891be019 635 | NOAMP WORD listexpr /* foo(@args) */
a0d0e21e 636 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
a5f75d66 637 append_elem(OP_LIST, $3, scalar($2))); }
891be019 638 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
85e6fe83 639 { $$ = newOP($1, OPf_SPECIAL);
3280af22 640 PL_hints |= HINT_BLOCK_SCOPE; }
a0d0e21e 641 | LOOPEX term
8990e307 642 { $$ = newLOOPEX($1,$2); }
891be019 643 | NOTOP argexpr /* not $foo */
c07a80fd 644 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
891be019 645 | UNIOP /* Unary op, $_ implied */
79072805 646 { $$ = newOP($1, 0); }
891be019 647 | UNIOP block /* eval { foo }, I *think* */
79072805 648 { $$ = newUNOP($1, 0, $2); }
891be019 649 | UNIOP term /* Unary op */
79072805 650 { $$ = newUNOP($1, 0, $2); }
891be019 651 | UNIOPSUB term /* Sub treated as unop */
4633a7c4
LW
652 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
653 append_elem(OP_LIST, $2, scalar($1))); }
891be019 654 | FUNC0 /* Nullary operator */
79072805 655 { $$ = newOP($1, 0); }
ae986130 656 | FUNC0 '(' ')'
79072805 657 { $$ = newOP($1, 0); }
891be019 658 | FUNC0SUB /* Sub treated as nullop */
28757baa 659 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
4633a7c4 660 scalar($1)); }
891be019 661 | FUNC1 '(' ')' /* not () */
79072805 662 { $$ = newOP($1, OPf_SPECIAL); }
891be019 663 | FUNC1 '(' expr ')' /* not($foo) */
79072805 664 { $$ = newUNOP($1, 0, $3); }
891be019 665 | PMFUNC '(' term ')' /* split (/foo/) */
79072805 666 { $$ = pmruntime($1, $3, Nullop); }
891be019 667 | PMFUNC '(' term ',' term ')' /* split (/foo/,$bar) */
79072805
LW
668 { $$ = pmruntime($1, $3, $5); }
669 | WORD
378cc40b 670 | listop
8d063cd8
LW
671 ;
672
891be019 673/* "my" declarations, with optional attributes */
09bef843
SB
674myattrterm: MY myterm myattrlist
675 { $$ = my_attrs($2,$3); }
676 | MY myterm
677 { $$ = localize($2,$1); }
678 ;
679
891be019 680/* Things that can be "my"'d */
09bef843
SB
681myterm : '(' expr ')'
682 { $$ = sawparens($2); }
683 | '(' ')'
684 { $$ = sawparens(newNULLLIST()); }
685 | scalar %prec '('
686 { $$ = $1; }
687 | hsh %prec '('
688 { $$ = $1; }
689 | ary %prec '('
690 { $$ = $1; }
691 ;
692
891be019 693/* Basic list expressions */
fad39ff1 694listexpr: /* NULL */ %prec PREC_LOW
8990e307 695 { $$ = Nullop; }
fad39ff1 696 | argexpr %prec PREC_LOW
a0d0e21e
LW
697 { $$ = $1; }
698 ;
699
700listexprcom: /* NULL */
701 { $$ = Nullop; }
79072805
LW
702 | expr
703 { $$ = $1; }
a0d0e21e
LW
704 | expr ','
705 { $$ = $1; }
79072805
LW
706 ;
707
891be019
SC
708/* A little bit of trickery to make "for my $foo (@bar)" actually be
709 lexical */
55497cff 710my_scalar: scalar
3280af22 711 { PL_in_my = 0; $$ = my($1); }
55497cff
PP
712 ;
713
79072805 714amper : '&' indirob
c07a80fd 715 { $$ = newCVREF($1,$2); }
a687059c
LW
716 ;
717
79072805
LW
718scalar : '$' indirob
719 { $$ = newSVREF($2); }
a687059c
LW
720 ;
721
79072805
LW
722ary : '@' indirob
723 { $$ = newAVREF($2); }
724 ;
725
726hsh : '%' indirob
727 { $$ = newHVREF($2); }
728 ;
729
730arylen : DOLSHARP indirob
731 { $$ = newAVREF($2); }
732 ;
733
734star : '*' indirob
a0d0e21e 735 { $$ = newGVREF(0,$2); }
79072805
LW
736 ;
737
891be019 738/* Indirect objects */
79072805
LW
739indirob : WORD
740 { $$ = scalar($1); }
fad39ff1 741 | scalar %prec PREC_LOW
463ee0b2 742 { $$ = scalar($1); }
79072805 743 | block
a0d0e21e 744 { $$ = scope($1); }
79072805 745
93a17b20
LW
746 | PRIVATEREF
747 { $$ = $1; }
8d063cd8 748 ;