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