Let's not assume how much memory has been allocated
[p5sagit/p5-mst-13.2.git] / perly.y
CommitLineData
a0d0e21e 1/* perly.y
a687059c 2 *
bc89e66f 3 * Copyright (c) 1991-2001, Larry Wall
a687059c 4 *
9ef589d8 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 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 13 */
14
15%{
79072805 16#include "EXTERN.h"
864dbfa3 17#define PERL_IN_PERLY_C
8d063cd8 18#include "perl.h"
1e0e26cb 19#ifdef EBCDIC
20#undef YYDEBUG
21#endif
864dbfa3 22#define dep() deprecate("\"do\" to call subroutines")
f0fcb552 23
09bef843 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 46%}
47
48%start prog
49
9d116dd7 50%{
09bef843 51#if 0 /* get this from perly.h instead */
9d116dd7 52%}
53
8d063cd8 54%union {
79072805 55 I32 ival;
56 char *pval;
57 OP *opval;
58 GV *gvval;
8d063cd8 59}
60
9d116dd7 61%{
09bef843 62#endif /* 0 */
a1a0e61e 63
64#ifdef USE_PURE_BISON
65#define YYLEX_PARAM (&yychar)
48cf72c8 66#define yylex yylex_r
a1a0e61e 67#endif
09bef843 68
9d116dd7 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 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 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 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 105%right '?' ':'
106%nonassoc DOTDOT
107%left OROR
108%left ANDAND
79072805 109%left <ival> BITOROP
110%left <ival> BITANDOP
a687059c 111%nonassoc EQOP
112%nonassoc RELOP
36477c24 113%nonassoc UNIOP UNIOPSUB
79072805 114%left <ival> SHIFTOP
a687059c 115%left ADDOP
116%left MULOP
8990e307 117%left <ival> MATCHOP
79072805 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 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 132 ;
133
891be019 134/* An ordinary block */
a687059c 135block : '{' remember lineseq '}'
3280af22 136 { if (PL_copline > (line_t)$1)
137 PL_copline = $1;
36477c24 138 $$ = block_end($2, $3); }
a0d0e21e 139 ;
140
55497cff 141remember: /* NULL */ /* start a full lexical scope */
142 { $$ = block_start(TRUE); }
143 ;
144
ecb2f335 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)
157 PL_copline = $1;
36477c24 158 $$ = block_end($2, $3); }
55497cff 159 ;
160
161mremember: /* NULL */ /* start a partial lexical scope */
162 { $$ = block_start(FALSE); }
8d063cd8 163 ;
164
891be019 165/* A collection of "lines" in the program */
8d063cd8 166lineseq : /* NULL */
79072805 167 { $$ = Nullop; }
168 | lineseq decl
169 { $$ = $1; }
8d063cd8 170 | lineseq line
463ee0b2 171 { $$ = append_list(OP_LINESEQ,
a0d0e21e 172 (LISTOP*)$1, (LISTOP*)$2);
3280af22 173 PL_pad_reset_pending = TRUE;
174 if ($1 && $2) PL_hints |= HINT_BLOCK_SCOPE; }
8d063cd8 175 ;
176
891be019 177/* A "line" in the program */
79072805 178line : label cond
179 { $$ = newSTATEOP(0, $1, $2); }
8d063cd8 180 | loop /* loops add their own labels */
181 | label ';'
182 { if ($1 != Nullch) {
79072805 183 $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0));
450a55e4 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 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
209 { $$ = newFOROP(0, Nullch, $2,
210 Nullop, $3, $1, Nullop); }
79072805 211 ;
212
891be019 213/* else and elsif blocks */
79072805 214else : /* NULL */
215 { $$ = Nullop; }
55497cff 216 | ELSE mblock
38a230cb 217 { ($2)->op_flags |= OPf_PARENS; $$ = scope($2); }
55497cff 218 | ELSIF '(' mexpr ')' mblock else
3280af22 219 { PL_copline = $1;
2c15bef3 220 $$ = newCONDOP(0, $3, scope($5), $6);
3280af22 221 PL_hints |= HINT_BLOCK_SCOPE; }
79072805 222 ;
223
891be019 224/* Real conditional expressions */
55497cff 225cond : IF '(' remember mexpr ')' mblock else
3280af22 226 { PL_copline = $1;
36477c24 227 $$ = block_end($3,
bbce6d69 228 newCONDOP(0, $4, scope($6), $7)); }
55497cff 229 | UNLESS '(' remember miexpr ')' mblock else
3280af22 230 { PL_copline = $1;
36477c24 231 $$ = block_end($3,
bbce6d69 232 newCONDOP(0, $4, scope($6), $7)); }
79072805 233 ;
234
891be019 235/* Continue blocks */
79072805 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
3280af22 244 { PL_copline = $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
3280af22 250 { PL_copline = $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,
bbce6d69 257 newFOROP(0, $1, $2, $5, $7, $9, $10)); }
258 | label FOR scalar '(' remember mexpr ')' mblock cont
36477c24 259 { $$ = block_end($5,
bbce6d69 260 newFOROP(0, $1, $2, mod($3, OP_ENTERLOOP),
261 $6, $8, $9)); }
262 | label FOR '(' remember mexpr ')' mblock cont
36477c24 263 { $$ = block_end($4,
55497cff 264 newFOROP(0, $1, $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;
3280af22 268 PL_copline = $2;
36c66720 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 285 ;
286
891be019 287/* Normal expression */
8d063cd8 288nexpr : /* NULL */
79072805 289 { $$ = Nullop; }
8d063cd8 290 | sideff
291 ;
292
891be019 293/* Boolean expression */
8d063cd8 294texpr : /* NULL means true */
b73d6f50 295 { (void)scan_num("1", &yylval); $$ = yylval.opval; }
8d063cd8 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 322label : /* empty */
323 { $$ = Nullch; }
32c2e4fb 324 | LABEL
8d063cd8 325 ;
326
891be019 327/* Some kind of declaration - does not take part in the parse tree */
8d063cd8 328decl : format
329 { $$ = 0; }
330 | subrout
331 { $$ = 0; }
09bef843 332 | mysubrout
333 { $$ = 0; }
a687059c 334 | package
335 { $$ = 0; }
a0d0e21e 336 | use
85e6fe83 337 { $$ = 0; }
8d063cd8 338 ;
339
44a8e56a 340format : FORMAT startformsub formname block
a0d0e21e 341 { newFORM($2, $3, $4); }
44a8e56a 342 ;
343
344formname: WORD { $$ = $1; }
345 | /* NULL */ { $$ = Nullop; }
8d063cd8 346 ;
347
891be019 348/* Unimplemented "my sub foo { }" */
09bef843 349mysubrout: MYSUB startsub subname proto subattrlist subbody
350 { newMYSUB($2, $3, $4, $5, $6); }
351 ;
352
891be019 353/* Subroutine definition */
09bef843 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 376 ;
377
891be019 378/* Subroutine prototype */
4633a7c4 379proto : /* NULL */
380 { $$ = Nullop; }
381 | THING
382 ;
28757baa 383
891be019 384/* Optional list of subroutine attributes */
09bef843 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 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 403 ;
404
a687059c 405package : PACKAGE WORD ';'
79072805 406 { package($2); }
93a17b20 407 | PACKAGE ';'
408 { package(Nullop); }
a687059c 409 ;
410
28757baa 411use : USE startsub
1aff0e91 412 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
28757baa 413 WORD WORD listexpr ';'
414 { utilize($1, $2, $4, $5, $6); }
85e6fe83 415 ;
416
891be019 417/* Ordinary expressions; logical combinations */
a0d0e21e 418expr : expr ANDOP expr
419 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
420 | expr OROP expr
421 { $$ = newLOGOP($2, 0, $1, $3); }
fad39ff1 422 | argexpr %prec PREC_LOW
a0d0e21e 423 ;
424
891be019 425/* Expressions are a list of terms joined by commas */
a0d0e21e 426argexpr : argexpr ','
427 { $$ = $1; }
428 | argexpr ',' term
79072805 429 { $$ = append_elem(OP_LIST, $1, $3); }
fad39ff1 430 | term %prec PREC_LOW
8d063cd8 431 ;
432
891be019 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 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 469 ;
470
891be019 471/* Names of methods. May use $object->$methodname */
a0d0e21e 472method : METHOD
473 | scalar
474 ;
475
891be019 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 484 { $$ = newBINOP(OP_AELEM, 0,
485 ref(newAVREF($1),OP_RV2AV),
486 scalar($4));}
891be019 487 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
fad39ff1 488 { $$ = newBINOP(OP_AELEM, 0,
489 ref(newAVREF($1),OP_RV2AV),
490 scalar($3));}
891be019 491 | scalar '{' expr ';' '}' /* $foo->{bar();} */
fad39ff1 492 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
493 PL_expect = XOPERATOR; }
891be019 494 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
fad39ff1 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 500 { $$ = newBINOP(OP_HELEM, 0,
501 ref(newHVREF($1),OP_RV2HV),
502 jmaybe($3));
503 PL_expect = XOPERATOR; }
891be019 504 | term ARROW '(' ')' /* $subref->() */
fad39ff1 505 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
506 newCVREF(0, scalar($1))); }
891be019 507 | term ARROW '(' expr ')' /* $subref->(@args) */
fad39ff1 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 513 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
514 append_elem(OP_LIST, $3,
515 newCVREF(0, scalar($1)))); }
891be019 516 | subscripted '(' ')' /* $foo->{bar}->() */
fad39ff1 517 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
518 newCVREF(0, scalar($1))); }
891be019 519 ;
fad39ff1 520
891be019 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 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); }
891be019 548 | term MATCHOP term /* $x =~ /$y/ */
79072805 549 { $$ = bind_match($2, $1, $3); }
891be019 550 ;
8d063cd8 551
891be019 552/* Unary operators and terms */
553termunop : '-' term %prec UMINUS /* -$x */
79072805 554 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
891be019 555 | '+' term %prec UMINUS /* +$x */
a687059c 556 { $$ = $2; }
891be019 557 | '!' term /* !$x */
79072805 558 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
891be019 559 | '~' term /* ~$x */
79072805 560 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
891be019 561 | term POSTINC /* $x++ */
79072805 562 { $$ = newUNOP(OP_POSTINC, 0,
463ee0b2 563 mod(scalar($1), OP_POSTINC)); }
891be019 564 | term POSTDEC /* $x-- */
79072805 565 { $$ = newUNOP(OP_POSTDEC, 0,
463ee0b2 566 mod(scalar($1), OP_POSTDEC)); }
891be019 567 | PREINC term /* ++$x */
79072805 568 { $$ = newUNOP(OP_PREINC, 0,
463ee0b2 569 mod(scalar($2), OP_PREINC)); }
891be019 570 | PREDEC term /* --$x */
79072805 571 { $$ = newUNOP(OP_PREDEC, 0,
463ee0b2 572 mod(scalar($2), OP_PREDEC)); }
891be019 573
574 ;
575
576/* Constructors for anonymous data */
577anonymous: '[' expr ']'
578 { $$ = newANONLIST($2); }
579 | '[' ']'
580 { $$ = newANONLIST(Nullop); }
581 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
ecb2f335 582 { $$ = newANONHASH($2); }
891be019 583 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
584 { $$ = newANONHASH(Nullop); }
585 | ANONSUB startanonsub proto subattrlist block %prec '('
586 { $$ = newANONATTRSUB($2, $3, $4, $5); }
587
588 ;
589
590/* Things called with "do" */
591termdo : DO term %prec UNIOP /* do $filename */
592 { $$ = dofile($2); }
593 | DO block %prec '(' /* do { code */
594 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
595 | DO WORD '(' ')' /* do somesub() */
596 { $$ = newUNOP(OP_ENTERSUB,
597 OPf_SPECIAL|OPf_STACKED,
598 prepend_elem(OP_LIST,
599 scalar(newCVREF(
600 (OPpENTERSUB_AMPER<<8),
601 scalar($2)
602 )),Nullop)); dep();}
603 | DO WORD '(' expr ')' /* do somesub(@args) */
604 { $$ = newUNOP(OP_ENTERSUB,
605 OPf_SPECIAL|OPf_STACKED,
606 append_elem(OP_LIST,
607 $4,
608 scalar(newCVREF(
609 (OPpENTERSUB_AMPER<<8),
610 scalar($2)
611 )))); dep();}
612 | DO scalar '(' ')' /* do $subref () */
613 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
614 prepend_elem(OP_LIST,
615 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
616 | DO scalar '(' expr ')' /* do $subref (@args) */
617 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
618 prepend_elem(OP_LIST,
619 $4,
620 scalar(newCVREF(0,scalar($2))))); dep();}
621
622 ;
623
624term : termbinop
625 | termunop
626 | anonymous
627 | termdo
628 | term '?' term ':' term
629 { $$ = newCONDOP(0, $1, $3, $5); }
630 | REFGEN term /* \$x, \@y, \%z */
631 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
09bef843 632 | myattrterm %prec UNIOP
633 { $$ = $1; }
634 | LOCAL term %prec UNIOP
93a17b20 635 { $$ = localize($2,$1); }
a0d0e21e 636 | '(' expr ')'
79072805 637 { $$ = sawparens($2); }
8d063cd8 638 | '(' ')'
8990e307 639 { $$ = sawparens(newNULLLIST()); }
79072805 640 | scalar %prec '('
8d063cd8 641 { $$ = $1; }
79072805 642 | star %prec '('
8d063cd8 643 { $$ = $1; }
79072805 644 | hsh %prec '('
8d063cd8 645 { $$ = $1; }
79072805 646 | ary %prec '('
8d063cd8 647 { $$ = $1; }
891be019 648 | arylen %prec '(' /* $#x, $#{ something } */
79072805 649 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
fad39ff1 650 | subscripted
651 { $$ = $1; }
891be019 652 | '(' expr ')' '[' expr ']' /* list slice */
79072805 653 { $$ = newSLICEOP(0, $5, $2); }
891be019 654 | '(' ')' '[' expr ']' /* empty list slice! */
79072805 655 { $$ = newSLICEOP(0, $4, Nullop); }
891be019 656 | ary '[' expr ']' /* array slice */
79072805 657 { $$ = prepend_elem(OP_ASLICE,
658 newOP(OP_PUSHMARK, 0),
79072805 659 newLISTOP(OP_ASLICE, 0,
660 list($3),
a0d0e21e 661 ref($1, OP_ASLICE))); }
891be019 662 | ary '{' expr ';' '}' /* @hash{@keys} */
79072805 663 { $$ = prepend_elem(OP_HSLICE,
664 newOP(OP_PUSHMARK, 0),
79072805 665 newLISTOP(OP_HSLICE, 0,
666 list($3),
a0d0e21e 667 ref(oopsHV($1), OP_HSLICE)));
3280af22 668 PL_expect = XOPERATOR; }
79072805 669 | THING %prec '('
670 { $$ = $1; }
891be019 671 | amper /* &foo; */
c07a80fd 672 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
ecb2f335 673 | amper '(' ')' /* &foo() */
a0d0e21e 674 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
891be019 675 | amper '(' expr ')' /* &foo(@args) */
a0d0e21e 676 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
4633a7c4 677 append_elem(OP_LIST, $3, scalar($1))); }
891be019 678 | NOAMP WORD listexpr /* foo(@args) */
a0d0e21e 679 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
a5f75d66 680 append_elem(OP_LIST, $3, scalar($2))); }
891be019 681 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
85e6fe83 682 { $$ = newOP($1, OPf_SPECIAL);
3280af22 683 PL_hints |= HINT_BLOCK_SCOPE; }
a0d0e21e 684 | LOOPEX term
8990e307 685 { $$ = newLOOPEX($1,$2); }
891be019 686 | NOTOP argexpr /* not $foo */
c07a80fd 687 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
891be019 688 | UNIOP /* Unary op, $_ implied */
79072805 689 { $$ = newOP($1, 0); }
891be019 690 | UNIOP block /* eval { foo }, I *think* */
79072805 691 { $$ = newUNOP($1, 0, $2); }
891be019 692 | UNIOP term /* Unary op */
79072805 693 { $$ = newUNOP($1, 0, $2); }
891be019 694 | UNIOPSUB term /* Sub treated as unop */
4633a7c4 695 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
696 append_elem(OP_LIST, $2, scalar($1))); }
891be019 697 | FUNC0 /* Nullary operator */
79072805 698 { $$ = newOP($1, 0); }
ae986130 699 | FUNC0 '(' ')'
79072805 700 { $$ = newOP($1, 0); }
891be019 701 | FUNC0SUB /* Sub treated as nullop */
28757baa 702 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
4633a7c4 703 scalar($1)); }
891be019 704 | FUNC1 '(' ')' /* not () */
79072805 705 { $$ = newOP($1, OPf_SPECIAL); }
891be019 706 | FUNC1 '(' expr ')' /* not($foo) */
79072805 707 { $$ = newUNOP($1, 0, $3); }
891be019 708 | PMFUNC '(' term ')' /* split (/foo/) */
79072805 709 { $$ = pmruntime($1, $3, Nullop); }
891be019 710 | PMFUNC '(' term ',' term ')' /* split (/foo/,$bar) */
79072805 711 { $$ = pmruntime($1, $3, $5); }
712 | WORD
378cc40b 713 | listop
8d063cd8 714 ;
715
891be019 716/* "my" declarations, with optional attributes */
09bef843 717myattrterm: MY myterm myattrlist
718 { $$ = my_attrs($2,$3); }
719 | MY myterm
720 { $$ = localize($2,$1); }
721 ;
722
891be019 723/* Things that can be "my"'d */
09bef843 724myterm : '(' expr ')'
725 { $$ = sawparens($2); }
726 | '(' ')'
727 { $$ = sawparens(newNULLLIST()); }
728 | scalar %prec '('
729 { $$ = $1; }
730 | hsh %prec '('
731 { $$ = $1; }
732 | ary %prec '('
733 { $$ = $1; }
734 ;
735
891be019 736/* Basic list expressions */
fad39ff1 737listexpr: /* NULL */ %prec PREC_LOW
8990e307 738 { $$ = Nullop; }
fad39ff1 739 | argexpr %prec PREC_LOW
a0d0e21e 740 { $$ = $1; }
741 ;
742
743listexprcom: /* NULL */
744 { $$ = Nullop; }
79072805 745 | expr
746 { $$ = $1; }
a0d0e21e 747 | expr ','
748 { $$ = $1; }
79072805 749 ;
750
891be019 751/* A little bit of trickery to make "for my $foo (@bar)" actually be
752 lexical */
55497cff 753my_scalar: scalar
3280af22 754 { PL_in_my = 0; $$ = my($1); }
55497cff 755 ;
756
79072805 757amper : '&' indirob
c07a80fd 758 { $$ = newCVREF($1,$2); }
a687059c 759 ;
760
79072805 761scalar : '$' indirob
762 { $$ = newSVREF($2); }
a687059c 763 ;
764
79072805 765ary : '@' indirob
766 { $$ = newAVREF($2); }
767 ;
768
769hsh : '%' indirob
770 { $$ = newHVREF($2); }
771 ;
772
773arylen : DOLSHARP indirob
774 { $$ = newAVREF($2); }
775 ;
776
777star : '*' indirob
a0d0e21e 778 { $$ = newGVREF(0,$2); }
79072805 779 ;
780
891be019 781/* Indirect objects */
79072805 782indirob : WORD
783 { $$ = scalar($1); }
fad39ff1 784 | scalar %prec PREC_LOW
463ee0b2 785 { $$ = scalar($1); }
79072805 786 | block
a0d0e21e 787 { $$ = scope($1); }
79072805 788
93a17b20 789 | PRIVATEREF
790 { $$ = $1; }
8d063cd8 791 ;
792
793%% /* PROGRAM */
09bef843 794
795/* more stuff added to make perly_c.diff easier to apply */
796
797#ifdef yyparse
798#undef yyparse
799#endif
800#define yyparse() Perl_yyparse(pTHX)
801