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