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