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