This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
SysV IPC
[perl5.git] / madly.y
CommitLineData
00e74f14
NC
1/* perly.y
2 *
3 * Copyright (c) 1991-2002, 2003, 2004 Larry Wall
4 *
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.
7 *
8 */
9
10/*
11 * 'I see,' laughed Strider. 'I look foul and feel fair. Is that it?
12 * All that is gold does not glitter, not all those who wander are lost.'
13 */
14
15/* Make the parser re-entrant. */
16
733849fa 17/* FIXME for MAD - is the new mintro on while and until important? */
00e74f14
NC
18%pure_parser
19
20%start prog
21
22%union {
23 I32 ival;
24 char *pval;
25 TOKEN* tkval;
26 OP *opval;
27 GV *gvval;
28}
29
30%token <tkval> '{' '}' '[' ']' '-' '+' '$' '@' '%' '*'
31
32%token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
33%token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
34%token <tkval> LABEL
35%token <tkval> FORMAT SUB ANONSUB PACKAGE USE
36%token <tkval> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
733849fa 37%token <tkval> GIVEN WHEN DEFAULT
00e74f14
NC
38%token <tkval> LOOPEX DOTDOT
39%token <tkval> FUNC0 FUNC1 FUNC UNIOP LSTOP
40%token <tkval> RELOP EQOP MULOP ADDOP
41%token <tkval> DOLSHARP DO HASHBRACK NOAMP
733849fa 42%token <tkval> LOCAL MY MYSUB REQUIRE
00e74f14
NC
43%token <tkval> COLONATTR
44
45%type <ival> prog progstart remember mremember savescope
733849fa
NC
46%type <ival> startsub startanonsub startformsub
47/* FIXME for MAD - are these two ival? */
48%type <ival> mydefsv mintro
00e74f14
NC
49
50%type <tkval> '&' ';'
51
52%type <opval> decl format subrout mysubrout package use peg
53
54%type <opval> block mblock lineseq line loop cond else
55%type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
56%type <opval> argexpr nexpr texpr iexpr mexpr mnexpr miexpr
57%type <opval> listexpr listexprcom indirob listop method
58%type <opval> formname subname proto subbody cont my_scalar
59%type <opval> subattrlist myattrlist myattrterm myterm
60%type <opval> termbinop termunop anonymous termdo
733849fa 61%type <opval> switch case
00e74f14
NC
62%type <tkval> label
63
64%nonassoc <tkval> PREC_LOW
65%nonassoc LOOPEX
66
67%left <tkval> OROP DOROP
68%left <tkval> ANDOP
69%right <tkval> NOTOP
70%nonassoc LSTOP LSTOPSUB
71%left <tkval> ','
72%right <tkval> ASSIGNOP
73%right <tkval> '?' ':'
74%nonassoc DOTDOT
75%left <tkval> OROR DORDOR
76%left <tkval> ANDAND
77%left <tkval> BITOROP
78%left <tkval> BITANDOP
79%nonassoc EQOP
80%nonassoc RELOP
81%nonassoc UNIOP UNIOPSUB
733849fa 82%nonassoc REQUIRE
00e74f14
NC
83%left <tkval> SHIFTOP
84%left ADDOP
85%left MULOP
86%left <tkval> MATCHOP
87%right <tkval> '!' '~' UMINUS REFGEN
88%right <tkval> POWOP
89%nonassoc <tkval> PREINC PREDEC POSTINC POSTDEC
90%left <tkval> ARROW
91%nonassoc <tkval> ')'
92%left <tkval> '('
93%left '[' '{'
94
95%token <tkval> PEG
96
97%% /* RULES */
98
99/* The whole program */
100prog : progstart
101 /*CONTINUED*/ lineseq
102 { $$ = $1; newPROG(block_end($1,$2)); }
103 ;
104
105/* An ordinary block */
106block : '{' remember lineseq '}'
107 { if (PL_copline > (line_t)($1)->tk_lval.ival)
108 PL_copline = (line_t)($1)->tk_lval.ival;
109 $$ = block_end($2, $3);
110 token_getmad($1,$$,'{');
111 token_getmad($4,$$,'}');
112 }
113 ;
114
115remember: /* NULL */ /* start a full lexical scope */
116 { $$ = block_start(TRUE); }
117 ;
118
733849fa
NC
119mydefsv: /* NULL */ /* lexicalize $_ */
120 { $$ = (I32) allocmy("$_"); }
121 ;
122
00e74f14
NC
123progstart:
124 {
125 PL_expect = XSTATE; $$ = block_start(TRUE);
126 }
127 ;
128
129
130mblock : '{' mremember lineseq '}'
131 { if (PL_copline > (line_t)($1)->tk_lval.ival)
132 PL_copline = (line_t)($1)->tk_lval.ival;
133 $$ = block_end($2, $3);
134 token_getmad($1,$$,'{');
135 token_getmad($4,$$,'}');
136 }
137 ;
138
139mremember: /* NULL */ /* start a partial lexical scope */
140 { $$ = block_start(FALSE); }
141 ;
142
143savescope: /* NULL */ /* remember stack pos in case of error */
144 { $$ = PL_savestack_ix; }
145
146/* A collection of "lines" in the program */
147lineseq : /* NULL */
148 { $$ = Nullop; }
149 | lineseq decl
150/* { $$ = $1 } */
151 { $$ = append_list(OP_LINESEQ,
152 (LISTOP*)$1, (LISTOP*)$2); }
153 | lineseq savescope line
154 { LEAVE_SCOPE($2);
155 $$ = append_list(OP_LINESEQ,
156 (LISTOP*)$1, (LISTOP*)$3);
157 PL_pad_reset_pending = TRUE;
158 if ($1 && $3) PL_hints |= HINT_BLOCK_SCOPE; }
159 ;
160
161/* A "line" in the program */
162line : label cond
163 { $$ = newSTATEOP(0, ($1)->tk_lval.pval, $2);
164 token_getmad($1,((LISTOP*)$$)->op_first,'L'); }
165 | loop /* loops add their own labels */
733849fa
NC
166 | switch /* ... and so do switches */
167 { $$ = $1; }
168 | label case
169 { $$ = newSTATEOP(0, ($1)->tk_lval.pval, $2); }
00e74f14
NC
170 | label ';'
171 {
172 if (($1)->tk_lval.pval) {
173 $$ = newSTATEOP(0, ($1)->tk_lval.pval, newOP(OP_NULL, 0));
174 token_getmad($1,$$,'L');
175 token_getmad($2,((LISTOP*)$$)->op_first,';');
176 }
177 else {
178 $$ = newOP(OP_NULL, 0);
179 PL_copline = NOLINE;
180 token_free($1);
181 token_getmad($2,$$,';');
182 }
183 PL_expect = XSTATE;
184 }
185 | label sideff ';'
186 { OP* op;
187 $$ = newSTATEOP(0, ($1)->tk_lval.pval, $2);
188 PL_expect = XSTATE;
189 /* sideff might already have a nexstate */
190 op = ((LISTOP*)$$)->op_first;
191 if (op) {
192 while (op->op_sibling &&
193 op->op_sibling->op_type == OP_NEXTSTATE)
194 op = op->op_sibling;
195 token_getmad($1,op,'L');
196 token_getmad($3,op,';');
197 }
198 }
199 ;
200
201/* An expression which may have a side-effect */
202sideff : error
203 { $$ = Nullop; }
204 | expr
205 { $$ = $1; }
206 | expr IF expr
207 { $$ = newLOGOP(OP_AND, 0, $3, $1);
208 token_getmad($2,$$,'i');
209 }
210 | expr UNLESS expr
211 { $$ = newLOGOP(OP_OR, 0, $3, $1);
212 token_getmad($2,$$,'i');
213 }
214 | expr WHILE expr
215 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1);
216 token_getmad($2,$$,'w');
217 }
218 | expr UNTIL iexpr
219 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);
220 token_getmad($2,$$,'w');
221 }
222 | expr FOR expr
223 { $$ = newFOROP(0, Nullch, (line_t)($2)->tk_lval.ival,
224 Nullop, $3, $1, Nullop);
225 token_getmad($2,((LISTOP*)$$)->op_first->op_sibling,'w');
226 }
227 ;
228
229/* else and elsif blocks */
230else : /* NULL */
231 { $$ = Nullop; }
232 | ELSE mblock
233 { ($2)->op_flags |= OPf_PARENS; $$ = scope($2);
234 token_getmad($1,$$,'o');
235 }
236 | ELSIF '(' mexpr ')' mblock else
237 { PL_copline = (line_t)($1)->tk_lval.ival;
238 $$ = newCONDOP(0, $3, scope($5), $6);
239 PL_hints |= HINT_BLOCK_SCOPE;
240 token_getmad($1,$$,'I');
241 token_getmad($2,$$,'(');
242 token_getmad($4,$$,')');
243 }
244 ;
245
246/* Real conditional expressions */
247cond : IF '(' remember mexpr ')' mblock else
248 { PL_copline = (line_t)($1)->tk_lval.ival;
249 $$ = block_end($3,
250 newCONDOP(0, $4, scope($6), $7));
251 token_getmad($1,$$,'I');
252 token_getmad($2,$$,'(');
253 token_getmad($5,$$,')');
254 }
255 | UNLESS '(' remember miexpr ')' mblock else
256 { PL_copline = (line_t)($1)->tk_lval.ival;
257 $$ = block_end($3,
258 newCONDOP(0, $4, scope($6), $7));
259 token_getmad($1,$$,'I');
260 token_getmad($2,$$,'(');
261 token_getmad($5,$$,')');
262 }
263 ;
264
733849fa
NC
265/* Cases for a switch statement */
266case : WHEN '(' remember mexpr ')' mblock
267 { $$ = block_end($3,
268 newWHENOP($4, scope($6))); }
269 | DEFAULT block
270 { $$ = newWHENOP(0, scope($2)); }
271 ;
272
00e74f14
NC
273/* Continue blocks */
274cont : /* NULL */
275 { $$ = Nullop; }
276 | CONTINUE block
277 { $$ = scope($2);
278 token_getmad($1,$$,'o');
279 }
280 ;
281
282/* Loops: while, until, for, and a bare block */
283loop : label WHILE '(' remember texpr ')' mintro mblock cont
284 { OP *innerop;
285 PL_copline = (line_t)$2;
733849fa 286 $$ = block_end($4,
00e74f14
NC
287 newSTATEOP(0, ($1)->tk_lval.pval,
288 innerop = newWHILEOP(0, 1, (LOOP*)Nullop,
289 ($2)->tk_lval.ival, $5, $8, $9, $7)));
290 token_getmad($1,innerop,'L');
291 token_getmad($2,innerop,'W');
292 token_getmad($3,innerop,'(');
293 token_getmad($6,innerop,')');
294 }
733849fa 295
00e74f14
NC
296 | label UNTIL '(' remember iexpr ')' mintro mblock cont
297 { OP *innerop;
298 PL_copline = (line_t)$2;
733849fa 299 $$ = block_end($4,
00e74f14 300 newSTATEOP(0, ($1)->tk_lval.pval,
733849fa 301 newWHILEOP(0, 1, (LOOP*)Nullop,
00e74f14
NC
302 ($2)->tk_lval.ival, $5, $8, $9, $7)));
303 token_getmad($1,innerop,'L');
304 token_getmad($2,innerop,'W');
305 token_getmad($3,innerop,'(');
306 token_getmad($6,innerop,')');
307 }
308 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
309 { OP *innerop;
310 $$ = block_end($4,
311 innerop = newFOROP(0, ($1)->tk_lval.pval, (line_t)($2)->tk_lval.ival, $5, $7, $9, $10));
312 token_getmad($1,((LISTOP*)innerop)->op_first,'L');
313 token_getmad($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
314 token_getmad($3,((LISTOP*)innerop)->op_first->op_sibling,'d');
315 token_getmad($6,((LISTOP*)innerop)->op_first->op_sibling,'(');
316 token_getmad($8,((LISTOP*)innerop)->op_first->op_sibling,')');
317 }
318 | label FOR scalar '(' remember mexpr ')' mblock cont
319 { OP *innerop;
320 $$ = block_end($5,
321 innerop = newFOROP(0, ($1)->tk_lval.pval, (line_t)($2)->tk_lval.ival, mod($3, OP_ENTERLOOP),
322 $6, $8, $9));
323 token_getmad($1,((LISTOP*)innerop)->op_first,'L');
324 token_getmad($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
325 token_getmad($4,((LISTOP*)innerop)->op_first->op_sibling,'(');
326 token_getmad($7,((LISTOP*)innerop)->op_first->op_sibling,')');
327 }
328 | label FOR '(' remember mexpr ')' mblock cont
329 { OP *innerop;
330 $$ = block_end($4,
331 innerop = newFOROP(0, ($1)->tk_lval.pval, (line_t)($2)->tk_lval.ival, Nullop, $5, $7, $8));
332 token_getmad($1,((LISTOP*)innerop)->op_first,'L');
333 token_getmad($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
334 token_getmad($3,((LISTOP*)innerop)->op_first->op_sibling,'(');
335 token_getmad($6,((LISTOP*)innerop)->op_first->op_sibling,')');
336 }
733849fa
NC
337 | label FOR '(' remember mnexpr ';' texpr ';' mintro mnexpr ')'
338 mblock
00e74f14
NC
339 /* basically fake up an initialize-while lineseq */
340 { OP *forop;
341 PL_copline = (line_t)($2)->tk_lval.ival;
342 forop = newSTATEOP(0, ($1)->tk_lval.pval,
343 newWHILEOP(0, 1, (LOOP*)Nullop,
344 ($2)->tk_lval.ival, scalar($7),
345 $12, $10, $9));
346 if (!$5)
347 $5 = newOP(OP_NULL, 0);
348 forop = newUNOP(OP_NULL, 0, append_elem(OP_LINESEQ,
349 newSTATEOP(0,
350 (($1)->tk_lval.pval
351 ?savepv(($1)->tk_lval.pval):Nullch),
352 $5),
353 forop));
354
355 token_getmad($2,forop,'3');
356 token_getmad($3,forop,'(');
357 token_getmad($6,forop,'1');
358 token_getmad($8,forop,'2');
359 token_getmad($11,forop,')');
360 token_getmad($1,forop,'L');
733849fa 361 $$ = block_end($4, forop); }
00e74f14
NC
362 | label block cont /* a block is a loop that happens once */
363 { $$ = newSTATEOP(0, ($1)->tk_lval.pval,
364 newWHILEOP(0, 1, (LOOP*)Nullop,
365 NOLINE, Nullop, $2, $3, 0));
366 token_getmad($1,((LISTOP*)$$)->op_first,'L'); }
367 ;
368
733849fa
NC
369/* Switch blocks */
370switch : label GIVEN '(' remember mydefsv mexpr ')' mblock
371 { PL_copline = (line_t) $2;
372 $$ = block_end($4,
373 newSTATEOP(0, ($1)->tk_lval.pval,
374 newGIVENOP($6, scope($8),
375 (PADOFFSET) $5) )); }
376 ;
377
00e74f14
NC
378/* determine whether there are any new my declarations */
379mintro : /* NULL */
380 { $$ = (PL_min_intro_pending &&
381 PL_max_intro_pending >= PL_min_intro_pending);
382 intro_my(); }
383
00e74f14
NC
384/* Normal expression */
385nexpr : /* NULL */
386 { $$ = Nullop; }
387 | sideff
388 ;
389
390/* Boolean expression */
391texpr : /* NULL means true */
392 { YYSTYPE tmplval;
393 (void)scan_num("1", &tmplval);
394 $$ = tmplval.opval; }
395 | expr
396 ;
397
398/* Inverted boolean expression */
399iexpr : expr
400 { $$ = invert(scalar($1)); }
401 ;
402
403/* Expression with its own lexical scope */
404mexpr : expr
405 { $$ = $1; intro_my(); }
406 ;
407
408mnexpr : nexpr
409 { $$ = $1; intro_my(); }
410 ;
411
412miexpr : iexpr
413 { $$ = $1; intro_my(); }
414 ;
415
416/* Optional "MAIN:"-style loop labels */
417label : /* empty */
418 { YYSTYPE tmplval;
419 tmplval.pval = Nullch;
420 $$ = newTOKEN(OP_NULL, tmplval, 0); }
421 | LABEL
422 ;
423
424/* Some kind of declaration - just hang on peg in the parse tree */
425decl : format
426 { $$ = $1; }
427 | subrout
428 { $$ = $1; }
429 | mysubrout
430 { $$ = $1; }
431 | package
432 { $$ = $1; }
433 | use
434 { $$ = $1; }
435 | peg
436 { $$ = $1; }
437 ;
438
439peg : PEG
440 { $$ = newOP(OP_NULL,0);
441 token_getmad($1,$$,'p');
442 }
443 ;
444
445format : FORMAT startformsub formname block
446 { $$ = newFORM($2, $3, $4);
447 prepend_madprops($1->tk_mad, $$, 'F');
448 $1->tk_mad = 0;
449 token_free($1);
450 }
451 ;
452
453formname: WORD { $$ = $1; }
454 | /* NULL */ { $$ = Nullop; }
455 ;
456
457/* Unimplemented "my sub foo { }" */
458mysubrout: MYSUB startsub subname proto subattrlist subbody
459 { $$ = newMYSUB($2, $3, $4, $5, $6);
460 token_getmad($1,$$,'d');
461 }
462 ;
463
464/* Subroutine definition */
465subrout : SUB startsub subname proto subattrlist subbody
466 { OP* o = newSVOP(OP_ANONCODE, 0,
467 (SV*)newATTRSUB($2, $3, $4, $5, $6));
468 $$ = newOP(OP_NULL,0);
469 op_getmad(o,$$,'&');
470 op_getmad($3,$$,'n');
471 op_getmad($4,$$,'s');
472 op_getmad($5,$$,'a');
473 token_getmad($1,$$,'d');
474 append_madprops($6->op_madprop, $$, 0);
475 $6->op_madprop = 0;
476 }
477 ;
478
479startsub: /* NULL */ /* start a regular subroutine scope */
480 { $$ = start_subparse(FALSE, 0); }
481 ;
482
483startanonsub: /* NULL */ /* start an anonymous subroutine scope */
484 { $$ = start_subparse(FALSE, CVf_ANON); }
485 ;
486
487startformsub: /* NULL */ /* start a format subroutine scope */
488 { $$ = start_subparse(TRUE, 0); }
489 ;
490
491/* Name of a subroutine - must be a bareword, could be special */
733849fa 492subname : WORD { const char *const name = SvPV_nolen_const(((SVOP*)$1)->op_sv);
00e74f14
NC
493 if (strEQ(name, "BEGIN") || strEQ(name, "END")
494 || strEQ(name, "INIT") || strEQ(name, "CHECK"))
495 CvSPECIAL_on(PL_compcv);
496 $$ = $1; }
497 ;
498
499/* Subroutine prototype */
500proto : /* NULL */
501 { $$ = Nullop; }
502 | THING
503 ;
504
505/* Optional list of subroutine attributes */
506subattrlist: /* NULL */
507 { $$ = Nullop; }
508 | COLONATTR THING
509 { $$ = $2;
510 token_getmad($1,$$,':');
511 }
512 | COLONATTR
513 { $$ = newOP(OP_NULL, 0);
514 token_getmad($1,$$,':');
515 }
516 ;
517
518/* List of attributes for a "my" variable declaration */
519myattrlist: COLONATTR THING
520 { $$ = $2;
521 token_getmad($1,$$,':');
522 }
523 | COLONATTR
524 { $$ = newOP(OP_NULL, 0);
525 token_getmad($1,$$,':');
526 }
527 ;
528
529/* Subroutine body - either null or a block */
530subbody : block { $$ = $1; }
531 | ';' { $$ = newOP(OP_NULL,0); PL_expect = XSTATE;
532 token_getmad($1,$$,';');
533 }
534 ;
535
536package : PACKAGE WORD ';'
537 { $$ = package($2);
538 token_getmad($1,$$,'o');
539 token_getmad($3,$$,';');
540 }
541 ;
542
543use : USE startsub
544 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
545 WORD WORD listexpr ';'
546 { $$ = utilize(($1)->tk_lval.ival, $2, $4, $5, $6);
547 token_getmad($1,$$,'o');
548 token_getmad($7,$$,';');
549 if (PL_rsfp_filters && AvFILLp(PL_rsfp_filters) >= 0)
550 append_madprops(newMADPROP('!', MAD_PV, "", 0), $$, 0);
551 }
552 ;
553
554/* Ordinary expressions; logical combinations */
555expr : expr ANDOP expr
556 { $$ = newLOGOP(OP_AND, 0, $1, $3);
557 token_getmad($2,$$,'o');
558 }
559 | expr OROP expr
560 { $$ = newLOGOP(($2)->tk_lval.ival, 0, $1, $3);
561 token_getmad($2,$$,'o');
562 }
563 | expr DOROP expr
564 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
565 token_getmad($2,$$,'o');
566 }
567 | argexpr %prec PREC_LOW
568 ;
569
570/* Expressions are a list of terms joined by commas */
571argexpr : argexpr ','
572 { OP* op = newNULLLIST();
573 token_getmad($2,op,',');
574 $$ = append_elem(OP_LIST, $1, op);
575 }
576 | argexpr ',' term
577 {
578 $3 = newUNOP(OP_NULL, 0, $3);
579 token_getmad($2,$3,',');
580 $$ = append_elem(OP_LIST, $1, $3);
581 }
582 | term %prec PREC_LOW
583 ;
584
585/* List operators */
586listop : LSTOP indirob argexpr /* print $fh @args */
587 { $$ = convert(($1)->tk_lval.ival, OPf_STACKED,
588 prepend_elem(OP_LIST, newGVREF(($1)->tk_lval.ival,$2), $3) );
589 token_getmad($1,$$,'o');
590 }
591 | FUNC '(' indirob expr ')' /* print ($fh @args */
592 { $$ = convert(($1)->tk_lval.ival, OPf_STACKED,
593 prepend_elem(OP_LIST, newGVREF(($1)->tk_lval.ival,$3), $4) );
594 token_getmad($1,$$,'o');
595 token_getmad($2,$$,'(');
596 token_getmad($5,$$,')');
597 }
598 | term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
599 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
600 append_elem(OP_LIST,
601 prepend_elem(OP_LIST, scalar($1), $5),
602 newUNOP(OP_METHOD, 0, $3)));
603 token_getmad($2,$$,'A');
604 token_getmad($4,$$,'(');
605 token_getmad($6,$$,')');
606 }
607 | term ARROW method /* $foo->bar */
608 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
609 append_elem(OP_LIST, scalar($1),
610 newUNOP(OP_METHOD, 0, $3)));
611 token_getmad($2,$$,'A');
612 }
613 | METHOD indirob listexpr /* new Class @args */
614 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
615 append_elem(OP_LIST,
616 prepend_elem(OP_LIST, $2, $3),
617 newUNOP(OP_METHOD, 0, $1)));
618 }
619 | FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
620 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
621 append_elem(OP_LIST,
622 prepend_elem(OP_LIST, $2, $4),
623 newUNOP(OP_METHOD, 0, $1)));
624 token_getmad($3,$$,'(');
625 token_getmad($5,$$,')');
626 }
627 | LSTOP listexpr /* print @args */
628 { $$ = convert(($1)->tk_lval.ival, 0, $2);
629 token_getmad($1,$$,'o');
630 }
631 | FUNC '(' listexprcom ')' /* print (@args) */
632 { $$ = convert(($1)->tk_lval.ival, 0, $3);
633 token_getmad($1,$$,'o');
634 token_getmad($2,$$,'(');
635 token_getmad($4,$$,')');
636 }
637 | LSTOPSUB startanonsub block /* map { foo } ... */
638 { $3 = newANONATTRSUB($2, 0, Nullop, $3); }
639 listexpr %prec LSTOP /* ... @bar */
640 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
641 append_elem(OP_LIST,
642 prepend_elem(OP_LIST, $3, $5), $1));
643 }
644 ;
645
646/* Names of methods. May use $object->$methodname */
647method : METHOD
648 | scalar
649 ;
650
651/* Some kind of subscripted expression */
652subscripted: star '{' expr ';' '}' /* *main::{something} */
653 /* In this and all the hash accessors, ';' is
654 * provided by the tokeniser */
655 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
656 PL_expect = XOPERATOR;
657 token_getmad($2,$$,'{');
658 token_getmad($4,$$,';');
659 token_getmad($5,$$,'}');
660 }
661 | scalar '[' expr ']' /* $array[$element] */
662 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
663 token_getmad($2,$$,'[');
664 token_getmad($4,$$,']');
665 }
666 | term ARROW '[' expr ']' /* somearef->[$element] */
667 { $$ = newBINOP(OP_AELEM, 0,
668 ref(newAVREF($1),OP_RV2AV),
669 scalar($4));
670 token_getmad($2,$$,'a');
671 token_getmad($3,$$,'[');
672 token_getmad($5,$$,']');
673 }
674 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
675 { $$ = newBINOP(OP_AELEM, 0,
676 ref(newAVREF($1),OP_RV2AV),
677 scalar($3));
678 token_getmad($2,$$,'[');
679 token_getmad($4,$$,']');
680 }
681 | scalar '{' expr ';' '}' /* $foo->{bar();} */
682 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
683 PL_expect = XOPERATOR;
684 token_getmad($2,$$,'{');
685 token_getmad($4,$$,';');
686 token_getmad($5,$$,'}');
687 }
688 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
689 { $$ = newBINOP(OP_HELEM, 0,
690 ref(newHVREF($1),OP_RV2HV),
691 jmaybe($4));
692 PL_expect = XOPERATOR;
693 token_getmad($2,$$,'a');
694 token_getmad($3,$$,'{');
695 token_getmad($5,$$,';');
696 token_getmad($6,$$,'}');
697 }
698 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
699 { $$ = newBINOP(OP_HELEM, 0,
700 ref(newHVREF($1),OP_RV2HV),
701 jmaybe($3));
702 PL_expect = XOPERATOR;
703 token_getmad($2,$$,'{');
704 token_getmad($4,$$,';');
705 token_getmad($5,$$,'}');
706 }
707 | term ARROW '(' ')' /* $subref->() */
708 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
709 newCVREF(0, scalar($1)));
710 token_getmad($2,$$,'a');
711 token_getmad($3,$$,'(');
712 token_getmad($4,$$,')');
713 }
714 | term ARROW '(' expr ')' /* $subref->(@args) */
715 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
716 append_elem(OP_LIST, $4,
717 newCVREF(0, scalar($1))));
718 token_getmad($2,$$,'a');
719 token_getmad($3,$$,'(');
720 token_getmad($5,$$,')');
721 }
722
723 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
724 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
725 append_elem(OP_LIST, $3,
726 newCVREF(0, scalar($1))));
727 token_getmad($2,$$,'(');
728 token_getmad($4,$$,')');
729 }
730 | subscripted '(' ')' /* $foo->{bar}->() */
731 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
732 newCVREF(0, scalar($1)));
733 token_getmad($2,$$,'(');
734 token_getmad($3,$$,')');
735 }
733849fa
NC
736 | '(' expr ')' '[' expr ']' /* list slice */
737 { $$ = newSLICEOP(0, $5, $2);
738 token_getmad($1,$$,'(');
739 token_getmad($3,$$,')');
740 token_getmad($4,$$,'[');
741 token_getmad($6,$$,']');
742 }
743 | '(' ')' '[' expr ']' /* empty list slice! */
744 { $$ = newSLICEOP(0, $4, Nullop);
745 token_getmad($1,$$,'(');
746 token_getmad($2,$$,')');
747 token_getmad($3,$$,'[');
748 token_getmad($5,$$,']');
749 }
00e74f14
NC
750 ;
751
752/* Binary operators between terms */
753termbinop: term ASSIGNOP term /* $x = $y */
754 { $$ = newASSIGNOP(OPf_STACKED, $1, ($2)->tk_lval.ival, $3);
755 token_getmad($2,$$,'o');
756 }
757 | term POWOP term /* $x ** $y */
758 { $$ = newBINOP(($2)->tk_lval.ival, 0, scalar($1), scalar($3));
759 token_getmad($2,$$,'o');
760 }
761 | term MULOP term /* $x * $y, $x x $y */
762 { if (($2)->tk_lval.ival != OP_REPEAT)
763 scalar($1);
764 $$ = newBINOP(($2)->tk_lval.ival, 0, $1, scalar($3));
765 token_getmad($2,$$,'o');
766 }
767 | term ADDOP term /* $x + $y */
768 { $$ = newBINOP(($2)->tk_lval.ival, 0, scalar($1), scalar($3));
769 token_getmad($2,$$,'o');
770 }
771 | term SHIFTOP term /* $x >> $y, $x << $y */
772 { $$ = newBINOP(($2)->tk_lval.ival, 0, scalar($1), scalar($3));
773 token_getmad($2,$$,'o');
774 }
775 | term RELOP term /* $x > $y, etc. */
776 { $$ = newBINOP(($2)->tk_lval.ival, 0, scalar($1), scalar($3));
777 token_getmad($2,$$,'o');
778 }
779 | term EQOP term /* $x == $y, $x eq $y */
780 { $$ = newBINOP(($2)->tk_lval.ival, 0, scalar($1), scalar($3));
781 token_getmad($2,$$,'o');
782 }
783 | term BITANDOP term /* $x & $y */
784 { $$ = newBINOP(($2)->tk_lval.ival, 0, scalar($1), scalar($3));
785 token_getmad($2,$$,'o');
786 }
787 | term BITOROP term /* $x | $y */
788 { $$ = newBINOP(($2)->tk_lval.ival, 0, scalar($1), scalar($3));
789 token_getmad($2,$$,'o');
790 }
791 | term DOTDOT term /* $x..$y, $x...$y */
792 { UNOP *op;
793 $$ = newRANGE(($2)->tk_lval.ival, scalar($1), scalar($3));
794 op = (UNOP*)$$;
795 op = (UNOP*)op->op_first; /* get to flop */
796 op = (UNOP*)op->op_first; /* get to flip */
797 op = (UNOP*)op->op_first; /* get to range */
798 token_getmad($2,(OP*)op,'o');
799 }
800 | term ANDAND term /* $x && $y */
801 { $$ = newLOGOP(OP_AND, 0, $1, $3);
802 token_getmad($2,$$,'o');
803 }
804 | term OROR term /* $x || $y */
805 { $$ = newLOGOP(OP_OR, 0, $1, $3);
806 token_getmad($2,$$,'o');
807 }
808 | term DORDOR term /* $x // $y */
809 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
810 token_getmad($2,$$,'o');
811 }
812 | term MATCHOP term /* $x =~ /$y/ */
813 { $$ = bind_match(($2)->tk_lval.ival, $1, $3);
814 if ($$->op_type == OP_NOT)
815 token_getmad($2,((UNOP*)$$)->op_first,'~');
816 else
817 token_getmad($2,$$,'~');
818 }
819 ;
820
821/* Unary operators and terms */
822termunop : '-' term %prec UMINUS /* -$x */
823 { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
824 token_getmad($1,$$,'o');
825 }
826 | '+' term %prec UMINUS /* +$x */
827 { $$ = newUNOP(OP_NULL, 0, $2);
828 token_getmad($1,$$,'+');
829 }
830 | '!' term /* !$x */
831 { $$ = newUNOP(OP_NOT, 0, scalar($2));
832 token_getmad($1,$$,'o');
833 }
834 | '~' term /* ~$x */
835 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
836 token_getmad($1,$$,'o');
837 }
838 | term POSTINC /* $x++ */
839 { $$ = newUNOP(OP_POSTINC, 0,
840 mod(scalar($1), OP_POSTINC));
841 token_getmad($2,$$,'o');
842 }
843 | term POSTDEC /* $x-- */
844 { $$ = newUNOP(OP_POSTDEC, 0,
845 mod(scalar($1), OP_POSTDEC));
846 token_getmad($2,$$,'o');
847 }
848 | PREINC term /* ++$x */
849 { $$ = newUNOP(OP_PREINC, 0,
850 mod(scalar($2), OP_PREINC));
851 token_getmad($1,$$,'o');
852 }
853 | PREDEC term /* --$x */
854 { $$ = newUNOP(OP_PREDEC, 0,
855 mod(scalar($2), OP_PREDEC));
856 token_getmad($1,$$,'o');
857 }
858
859 ;
860
861/* Constructors for anonymous data */
862anonymous: '[' expr ']'
863 { $$ = newANONLIST($2);
864 token_getmad($1,$$,'[');
865 token_getmad($3,$$,']');
866 }
867 | '[' ']'
868 { $$ = newANONLIST(Nullop);
869 token_getmad($1,$$,'[');
870 token_getmad($2,$$,']');
871 }
872 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
873 { $$ = newANONHASH($2);
874 token_getmad($1,$$,'{');
875 token_getmad($3,$$,';');
876 token_getmad($4,$$,'}');
877 }
878 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
879 { $$ = newANONHASH(Nullop);
880 token_getmad($1,$$,'{');
881 token_getmad($2,$$,';');
882 token_getmad($3,$$,'}');
883 }
884 | ANONSUB startanonsub proto subattrlist block %prec '('
885 { $$ = newANONATTRSUB($2, $3, $4, $5);
886 token_getmad($1,$$,'o');
887 op_getmad($3,$$,'s');
888 op_getmad($4,$$,'a');
889 }
890
891 ;
892
893/* Things called with "do" */
894termdo : DO term %prec UNIOP /* do $filename */
895 { $$ = dofile($2, $1);
896 token_getmad($1,$$,'o');
897 }
898 | DO block %prec '(' /* do { code */
899 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2));
900 token_getmad($1,$$,'D');
901 }
902 | DO WORD '(' ')' /* do somesub() */
903 { $$ = newUNOP(OP_ENTERSUB,
904 OPf_SPECIAL|OPf_STACKED,
905 prepend_elem(OP_LIST,
906 scalar(newCVREF(
907 (OPpENTERSUB_AMPER<<8),
908 scalar($2)
909 )),Nullop)); dep();
910 token_getmad($1,$$,'o');
911 token_getmad($3,$$,'(');
912 token_getmad($4,$$,')');
913 }
914 | DO WORD '(' expr ')' /* do somesub(@args) */
915 { $$ = newUNOP(OP_ENTERSUB,
916 OPf_SPECIAL|OPf_STACKED,
917 append_elem(OP_LIST,
918 $4,
919 scalar(newCVREF(
920 (OPpENTERSUB_AMPER<<8),
921 scalar($2)
922 )))); dep();
923 token_getmad($1,$$,'o');
924 token_getmad($3,$$,'(');
925 token_getmad($5,$$,')');
926 }
927 | DO scalar '(' ')' /* do $subref () */
928 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
929 prepend_elem(OP_LIST,
930 scalar(newCVREF(0,scalar($2))), Nullop)); dep();
931 token_getmad($1,$$,'o');
932 token_getmad($3,$$,'(');
933 token_getmad($4,$$,')');
934 }
935 | DO scalar '(' expr ')' /* do $subref (@args) */
936 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
937 prepend_elem(OP_LIST,
938 $4,
939 scalar(newCVREF(0,scalar($2))))); dep();
940 token_getmad($1,$$,'o');
941 token_getmad($3,$$,'(');
942 token_getmad($5,$$,')');
943 }
944
945 ;
946
947term : termbinop
948 | termunop
949 | anonymous
950 | termdo
951 | term '?' term ':' term
952 { $$ = newCONDOP(0, $1, $3, $5);
953 token_getmad($2,$$,'?');
954 token_getmad($4,$$,':');
955 }
956 | REFGEN term /* \$x, \@y, \%z */
957 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN));
958 token_getmad($1,$$,'o');
959 }
960 | myattrterm %prec UNIOP
961 { $$ = $1; }
962 | LOCAL term %prec UNIOP
963 { $$ = localize($2,($1)->tk_lval.ival);
964 token_getmad($1,$$,'d');
965 }
966 | '(' expr ')'
967 { $$ = sawparens(newUNOP(OP_NULL,0,$2));
968 token_getmad($1,$$,'(');
969 token_getmad($3,$$,')');
970 }
971 | '(' ')'
972 { $$ = sawparens(newNULLLIST());
973 token_getmad($1,$$,'(');
974 token_getmad($2,$$,')');
975 }
976 | scalar %prec '('
977 { $$ = $1; }
978 | star %prec '('
979 { $$ = $1; }
980 | hsh %prec '('
981 { $$ = $1; }
982 | ary %prec '('
983 { $$ = $1; }
984 | arylen %prec '(' /* $#x, $#{ something } */
985 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
986 | subscripted
987 { $$ = $1; }
00e74f14
NC
988 | ary '[' expr ']' /* array slice */
989 { $$ = prepend_elem(OP_ASLICE,
990 newOP(OP_PUSHMARK, 0),
991 newLISTOP(OP_ASLICE, 0,
992 list($3),
993 ref($1, OP_ASLICE)));
994 token_getmad($2,$$,'[');
995 token_getmad($4,$$,']');
996 }
997 | ary '{' expr ';' '}' /* @hash{@keys} */
998 { $$ = prepend_elem(OP_HSLICE,
999 newOP(OP_PUSHMARK, 0),
1000 newLISTOP(OP_HSLICE, 0,
1001 list($3),
1002 ref(oopsHV($1), OP_HSLICE)));
1003 PL_expect = XOPERATOR;
1004 token_getmad($2,$$,'{');
1005 token_getmad($4,$$,';');
1006 token_getmad($5,$$,'}');
1007 }
1008 | THING %prec '('
1009 { $$ = $1; }
1010 | amper /* &foo; */
1011 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1012 | amper '(' ')' /* &foo() */
1013 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1014 token_getmad($2,$$,'(');
1015 token_getmad($3,$$,')');
1016 }
1017 | amper '(' expr ')' /* &foo(@args) */
1018 { OP* op;
1019 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1020 append_elem(OP_LIST, $3, scalar($1)));
1021 op = $$;
1022 if (op->op_type == OP_CONST) { /* defeat const fold */
1023 op = (OP*)op->op_madprop->mad_val;
1024 }
1025 token_getmad($2,op,'(');
1026 token_getmad($4,op,')');
1027 }
1028 | NOAMP WORD listexpr /* foo(@args) */
1029 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1030 append_elem(OP_LIST, $3, scalar($2)));
1031 token_getmad($1,$$,'o');
1032 }
1033 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
1034 { $$ = newOP(($1)->tk_lval.ival, OPf_SPECIAL);
1035 PL_hints |= HINT_BLOCK_SCOPE;
1036 token_getmad($1,$$,'o');
1037 }
1038 | LOOPEX term
1039 { $$ = newLOOPEX(($1)->tk_lval.ival,$2);
1040 token_getmad($1,$$,'o');
1041 }
1042 | NOTOP argexpr /* not $foo */
1043 { $$ = newUNOP(OP_NOT, 0, scalar($2));
1044 token_getmad($1,$$,'o');
1045 }
1046 | UNIOP /* Unary op, $_ implied */
1047 { $$ = newOP(($1)->tk_lval.ival, 0);
1048 token_getmad($1,$$,'o');
1049 }
1050 | UNIOP block /* eval { foo }, I *think* */
1051 { $$ = newUNOP(($1)->tk_lval.ival, 0, $2);
1052 token_getmad($1,$$,'o');
1053 }
1054 | UNIOP term /* Unary op */
1055 { $$ = newUNOP(($1)->tk_lval.ival, 0, $2);
1056 token_getmad($1,$$,'o');
1057 }
733849fa
NC
1058 | REQUIRE /* require, $_ implied *//* FIMXE for MAD needed? */
1059 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); }
1060 | REQUIRE term /* require Foo *//* FIMXE for MAD needed? */
1061 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); }
00e74f14
NC
1062 | UNIOPSUB term /* Sub treated as unop */
1063 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
733849fa 1064 append_elem(OP_LIST, $2, scalar($1))); }
00e74f14
NC
1065 | FUNC0 /* Nullary operator */
1066 { $$ = newOP(($1)->tk_lval.ival, 0);
1067 token_getmad($1,$$,'o');
1068 }
1069 | FUNC0 '(' ')'
1070 { $$ = newOP(($1)->tk_lval.ival, 0);
1071 token_getmad($1,$$,'o');
1072 token_getmad($2,$$,'(');
1073 token_getmad($3,$$,')');
1074 }
1075 | FUNC0SUB /* Sub treated as nullop */
1076 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1077 scalar($1)); }
1078 | FUNC1 '(' ')' /* not () */
1079 { $$ = newOP(($1)->tk_lval.ival, OPf_SPECIAL);
1080 token_getmad($1,$$,'o');
1081 token_getmad($2,$$,'(');
1082 token_getmad($3,$$,')');
1083 }
1084 | FUNC1 '(' expr ')' /* not($foo) */
1085 { $$ = newUNOP(($1)->tk_lval.ival, 0, $3);
1086 token_getmad($1,$$,'o');
1087 token_getmad($2,$$,'(');
1088 token_getmad($4,$$,')');
1089 }
1090 | PMFUNC '(' argexpr ')' /* m//, s///, tr/// */
1091 { $$ = pmruntime($1, $3, 1);
1092 token_getmad($2,$$,'(');
1093 token_getmad($4,$$,')');
1094 }
1095 | WORD
1096 | listop
1097 ;
1098
1099/* "my" declarations, with optional attributes */
1100myattrterm: MY myterm myattrlist
1101 { $$ = my_attrs($2,$3);
1102 token_getmad($1,$$,'d');
1103 append_madprops($3->op_madprop, $$, 'a');
1104 $3->op_madprop = 0;
1105 }
1106 | MY myterm
1107 { $$ = localize($2,($1)->tk_lval.ival);
1108 token_getmad($1,$$,'d');
1109 }
1110 ;
1111
1112/* Things that can be "my"'d */
1113myterm : '(' expr ')'
1114 { $$ = sawparens($2);
1115 token_getmad($1,$$,'(');
1116 token_getmad($3,$$,')');
1117 }
1118 | '(' ')'
1119 { $$ = sawparens(newNULLLIST());
1120 token_getmad($1,$$,'(');
1121 token_getmad($2,$$,')');
1122 }
1123 | scalar %prec '('
1124 { $$ = $1; }
1125 | hsh %prec '('
1126 { $$ = $1; }
1127 | ary %prec '('
1128 { $$ = $1; }
1129 ;
1130
1131/* Basic list expressions */
1132listexpr: /* NULL */ %prec PREC_LOW
1133 { $$ = Nullop; }
1134 | argexpr %prec PREC_LOW
1135 { $$ = $1; }
1136 ;
1137
1138listexprcom: /* NULL */
1139 { $$ = Nullop; }
1140 | expr
1141 { $$ = $1; }
1142 | expr ','
1143 { OP* op = newNULLLIST();
1144 token_getmad($2,op,',');
1145 $$ = append_elem(OP_LIST, $1, op);
1146 }
1147 ;
1148
1149/* A little bit of trickery to make "for my $foo (@bar)" actually be
1150 lexical */
1151my_scalar: scalar
1152 { PL_in_my = 0; $$ = my($1); }
1153 ;
1154
1155amper : '&' indirob
1156 { $$ = newCVREF(($1)->tk_lval.ival,$2);
1157 token_getmad($1,$$,'&');
1158 }
1159 ;
1160
1161scalar : '$' indirob
1162 { $$ = newSVREF($2);
1163 token_getmad($1,$$,'$');
1164 }
1165 ;
1166
1167ary : '@' indirob
1168 { $$ = newAVREF($2);
1169 token_getmad($1,$$,'@');
1170 }
1171 ;
1172
1173hsh : '%' indirob
1174 { $$ = newHVREF($2);
1175 token_getmad($1,$$,'%');
1176 }
1177 ;
1178
1179arylen : DOLSHARP indirob
1180 { $$ = newAVREF($2);
1181 token_getmad($1,$$,'l');
1182 }
1183 ;
1184
1185star : '*' indirob
1186 { $$ = newGVREF(0,$2);
1187 token_getmad($1,$$,'*');
1188 }
1189 ;
1190
1191/* Indirect objects */
1192indirob : WORD
1193 { $$ = scalar($1); }
1194 | scalar %prec PREC_LOW
1195 { $$ = scalar($1); }
1196 | block
1197 { $$ = scope($1); }
1198
1199 | PRIVATEREF
1200 { $$ = $1; }
1201 ;