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