This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
flock() constants
[perl5.git] / perly.y
... / ...
CommitLineData
1/* perly.y
2 *
3 * Copyright (c) 1991-1994, 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 that wander are lost.'
13 */
14
15%{
16#include "EXTERN.h"
17#include "perl.h"
18
19static void
20dep()
21{
22 deprecate("\"do\" to call subroutines");
23}
24
25%}
26
27%start prog
28
29%union {
30 I32 ival;
31 char *pval;
32 OP *opval;
33 GV *gvval;
34}
35
36%token <ival> '{' ')'
37
38%token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
39%token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
40%token <pval> LABEL
41%token <ival> FORMAT SUB ANONSUB PACKAGE USE
42%token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
43%token <ival> LOOPEX DOTDOT
44%token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP
45%token <ival> RELOP EQOP MULOP ADDOP
46%token <ival> DOLSHARP DO HASHBRACK NOAMP
47%token LOCAL MY
48
49%type <ival> prog decl local format startsub remember mremember '&'
50%type <opval> block mblock lineseq line loop cond else
51%type <opval> expr term scalar ary hsh arylen star amper sideff
52%type <opval> argexpr nexpr texpr iexpr mexpr mnexpr mtexpr miexpr
53%type <opval> listexpr listexprcom indirob
54%type <opval> listop method proto cont my_scalar
55%type <pval> label
56
57%left <ival> OROP
58%left ANDOP
59%right NOTOP
60%nonassoc LSTOP LSTOPSUB
61%left ','
62%right <ival> ASSIGNOP
63%right '?' ':'
64%nonassoc DOTDOT
65%left OROR
66%left ANDAND
67%left <ival> BITOROP
68%left <ival> BITANDOP
69%nonassoc EQOP
70%nonassoc RELOP
71%nonassoc UNIOP UNIOPSUB
72%left <ival> SHIFTOP
73%left ADDOP
74%left MULOP
75%left <ival> MATCHOP
76%right '!' '~' UMINUS REFGEN
77%right <ival> POWOP
78%nonassoc PREINC PREDEC POSTINC POSTDEC
79%left ARROW
80%left '('
81
82%% /* RULES */
83
84prog : /* NULL */
85 {
86#if defined(YYDEBUG) && defined(DEBUGGING)
87 yydebug = (debug & 1);
88#endif
89 expect = XSTATE;
90 }
91 /*CONTINUED*/ lineseq
92 { newPROG($2); }
93 ;
94
95block : '{' remember lineseq '}'
96 { if (copline > (line_t)$1)
97 copline = $1;
98 $$ = block_end($2, $3); }
99 ;
100
101remember: /* NULL */ /* start a full lexical scope */
102 { $$ = block_start(TRUE); }
103 ;
104
105mblock : '{' mremember lineseq '}'
106 { if (copline > (line_t)$1)
107 copline = $1;
108 $$ = block_end($2, $3); }
109 ;
110
111mremember: /* NULL */ /* start a partial lexical scope */
112 { $$ = block_start(FALSE); }
113 ;
114
115lineseq : /* NULL */
116 { $$ = Nullop; }
117 | lineseq decl
118 { $$ = $1; }
119 | lineseq line
120 { $$ = append_list(OP_LINESEQ,
121 (LISTOP*)$1, (LISTOP*)$2);
122 pad_reset_pending = TRUE;
123 if ($1 && $2) hints |= HINT_BLOCK_SCOPE; }
124 ;
125
126line : label cond
127 { $$ = newSTATEOP(0, $1, $2); }
128 | loop /* loops add their own labels */
129 | label ';'
130 { if ($1 != Nullch) {
131 $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0));
132 }
133 else {
134 $$ = Nullop;
135 copline = NOLINE;
136 }
137 expect = XSTATE; }
138 | label sideff ';'
139 { $$ = newSTATEOP(0, $1, $2);
140 expect = XSTATE; }
141 ;
142
143sideff : error
144 { $$ = Nullop; }
145 | expr
146 { $$ = $1; }
147 | expr IF expr
148 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
149 | expr UNLESS expr
150 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
151 | expr WHILE expr
152 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
153 | expr UNTIL iexpr
154 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);}
155 ;
156
157else : /* NULL */
158 { $$ = Nullop; }
159 | ELSE mblock
160 { $$ = scope($2); }
161 | ELSIF '(' mexpr ')' mblock else
162 { copline = $1;
163 $$ = newSTATEOP(0, Nullch,
164 newCONDOP(0, $3, scope($5), $6));
165 hints |= HINT_BLOCK_SCOPE; }
166 ;
167
168cond : IF '(' remember mexpr ')' mblock else
169 { copline = $1;
170 $$ = block_end($3,
171 newCONDOP(0, $4, scope($6), $7)); }
172 | UNLESS '(' remember miexpr ')' mblock else
173 { copline = $1;
174 $$ = block_end($3,
175 newCONDOP(0, $4, scope($6), $7)); }
176 | IF block block else
177 { copline = $1;
178 deprecate("if BLOCK BLOCK");
179 $$ = newCONDOP(0, scope($2), scope($3), $4); }
180 | UNLESS block block else
181 { copline = $1;
182 deprecate("unless BLOCK BLOCK");
183 $$ = newCONDOP(0, invert(scalar(scope($2))),
184 scope($3), $4); }
185 ;
186
187cont : /* NULL */
188 { $$ = Nullop; }
189 | CONTINUE block
190 { $$ = scope($2); }
191 ;
192
193loop : label WHILE '(' remember mtexpr ')' mblock cont
194 { copline = $2;
195 $$ = block_end($4,
196 newSTATEOP(0, $1,
197 newWHILEOP(0, 1, (LOOP*)Nullop,
198 $5, $7, $8))); }
199 | label UNTIL '(' remember miexpr ')' mblock cont
200 { copline = $2;
201 $$ = block_end($4,
202 newSTATEOP(0, $1,
203 newWHILEOP(0, 1, (LOOP*)Nullop,
204 $5, $7, $8))); }
205 | label WHILE block block cont
206 { copline = $2;
207 deprecate("while BLOCK BLOCK");
208 $$ = newSTATEOP(0, $1,
209 newWHILEOP(0, 1, (LOOP*)Nullop,
210 scope($3), $4, $5)); }
211 | label UNTIL block block cont
212 { copline = $2;
213 deprecate("until BLOCK BLOCK");
214 $$ = newSTATEOP(0, $1,
215 newWHILEOP(0, 1, (LOOP*)Nullop,
216 invert(scalar(scope($3))),
217 $4, $5)); }
218 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
219 { $$ = block_end($4,
220 newFOROP(0, $1, $2, $5, $7, $9, $10)); }
221 | label FOR scalar '(' remember mexpr ')' mblock cont
222 { $$ = block_end($5,
223 newFOROP(0, $1, $2, mod($3, OP_ENTERLOOP),
224 $6, $8, $9)); }
225 | label FOR '(' remember mexpr ')' mblock cont
226 { $$ = block_end($4,
227 newFOROP(0, $1, $2, Nullop, $5, $7, $8)); }
228 | label FOR '(' remember mnexpr ';' mtexpr ';' mnexpr ')' mblock
229 /* basically fake up an initialize-while lineseq */
230 { copline = $2;
231 $$ = block_end($4,
232 append_elem(OP_LINESEQ, scalar($5),
233 newSTATEOP(0, $1,
234 newWHILEOP(0, 1, (LOOP*)Nullop,
235 scalar($7),
236 $11, scalar($9))))); }
237 | label block cont /* a block is a loop that happens once */
238 { $$ = newSTATEOP(0,
239 $1, newWHILEOP(0, 1, (LOOP*)Nullop,
240 Nullop, $2, $3)); }
241 ;
242
243nexpr : /* NULL */
244 { $$ = Nullop; }
245 | sideff
246 ;
247
248texpr : /* NULL means true */
249 { (void)scan_num("1"); $$ = yylval.opval; }
250 | expr
251 ;
252
253iexpr : expr
254 { $$ = invert(scalar($1)); }
255 ;
256
257mexpr : expr
258 { $$ = $1; intro_my(); }
259 ;
260
261mnexpr : nexpr
262 { $$ = $1; intro_my(); }
263 ;
264
265mtexpr : texpr
266 { $$ = $1; intro_my(); }
267 ;
268
269miexpr : iexpr
270 { $$ = $1; intro_my(); }
271 ;
272
273label : /* empty */
274 { $$ = Nullch; }
275 | LABEL
276 ;
277
278decl : format
279 { $$ = 0; }
280 | subrout
281 { $$ = 0; }
282 | package
283 { $$ = 0; }
284 | use
285 { $$ = 0; }
286 ;
287
288format : FORMAT startsub WORD block
289 { newFORM($2, $3, $4); }
290 | FORMAT startsub block
291 { newFORM($2, Nullop, $3); }
292 ;
293
294subrout : SUB startsub WORD proto block
295 { newSUB($2, $3, $4, $5); }
296 | SUB startsub WORD proto ';'
297 { newSUB($2, $3, $4, Nullop); expect = XSTATE; }
298 ;
299
300proto : /* NULL */
301 { $$ = Nullop; }
302 | THING
303 ;
304
305startsub: /* NULL */ /* start a subroutine scope */
306 { $$ = start_subparse(); }
307 ;
308
309package : PACKAGE WORD ';'
310 { package($2); }
311 | PACKAGE ';'
312 { package(Nullop); }
313 ;
314
315use : USE startsub WORD WORD listexpr ';'
316 { utilize($1, $2, $3, $4, $5); }
317 ;
318
319expr : expr ANDOP expr
320 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
321 | expr OROP expr
322 { $$ = newLOGOP($2, 0, $1, $3); }
323 | argexpr
324 ;
325
326argexpr : argexpr ','
327 { $$ = $1; }
328 | argexpr ',' term
329 { $$ = append_elem(OP_LIST, $1, $3); }
330 | term
331 ;
332
333listop : LSTOP indirob argexpr
334 { $$ = convert($1, OPf_STACKED,
335 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
336 | FUNC '(' indirob expr ')'
337 { $$ = convert($1, OPf_STACKED,
338 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
339 | term ARROW method '(' listexprcom ')'
340 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
341 append_elem(OP_LIST,
342 prepend_elem(OP_LIST, scalar($1), $5),
343 newUNOP(OP_METHOD, 0, $3))); }
344 | METHOD indirob listexpr
345 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
346 append_elem(OP_LIST,
347 prepend_elem(OP_LIST, $2, $3),
348 newUNOP(OP_METHOD, 0, $1))); }
349 | FUNCMETH indirob '(' listexprcom ')'
350 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
351 append_elem(OP_LIST,
352 prepend_elem(OP_LIST, $2, $4),
353 newUNOP(OP_METHOD, 0, $1))); }
354 | LSTOP listexpr
355 { $$ = convert($1, 0, $2); }
356 | FUNC '(' listexprcom ')'
357 { $$ = convert($1, 0, $3); }
358 | LSTOPSUB startsub block listexpr %prec LSTOP
359 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
360 append_elem(OP_LIST,
361 prepend_elem(OP_LIST, newANONSUB($2, 0, $3), $4),
362 $1)); }
363 ;
364
365method : METHOD
366 | scalar
367 ;
368
369term : term ASSIGNOP term
370 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
371 | term POWOP term
372 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
373 | term MULOP term
374 { if ($2 != OP_REPEAT)
375 scalar($1);
376 $$ = newBINOP($2, 0, $1, scalar($3)); }
377 | term ADDOP term
378 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
379 | term SHIFTOP term
380 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
381 | term RELOP term
382 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
383 | term EQOP term
384 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
385 | term BITANDOP term
386 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
387 | term BITOROP term
388 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
389 | term DOTDOT term
390 { $$ = newRANGE($2, scalar($1), scalar($3));}
391 | term ANDAND term
392 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
393 | term OROR term
394 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
395 | term '?' term ':' term
396 { $$ = newCONDOP(0, $1, $3, $5); }
397 | term MATCHOP term
398 { $$ = bind_match($2, $1, $3); }
399
400 | '-' term %prec UMINUS
401 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
402 | '+' term %prec UMINUS
403 { $$ = $2; }
404 | '!' term
405 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
406 | '~' term
407 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
408 | REFGEN term
409 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
410 | term POSTINC
411 { $$ = newUNOP(OP_POSTINC, 0,
412 mod(scalar($1), OP_POSTINC)); }
413 | term POSTDEC
414 { $$ = newUNOP(OP_POSTDEC, 0,
415 mod(scalar($1), OP_POSTDEC)); }
416 | PREINC term
417 { $$ = newUNOP(OP_PREINC, 0,
418 mod(scalar($2), OP_PREINC)); }
419 | PREDEC term
420 { $$ = newUNOP(OP_PREDEC, 0,
421 mod(scalar($2), OP_PREDEC)); }
422 | local term %prec UNIOP
423 { $$ = localize($2,$1); }
424 | '(' expr ')'
425 { $$ = sawparens($2); }
426 | '(' ')'
427 { $$ = sawparens(newNULLLIST()); }
428 | '[' expr ']' %prec '('
429 { $$ = newANONLIST($2); }
430 | '[' ']' %prec '('
431 { $$ = newANONLIST(Nullop); }
432 | HASHBRACK expr ';' '}' %prec '('
433 { $$ = newANONHASH($2); }
434 | HASHBRACK ';' '}' %prec '('
435 { $$ = newANONHASH(Nullop); }
436 | ANONSUB startsub proto block %prec '('
437 { $$ = newANONSUB($2, $3, $4); }
438 | scalar %prec '('
439 { $$ = $1; }
440 | star '{' expr ';' '}'
441 { $$ = newBINOP(OP_GELEM, 0, newGVREF(0,$1), $3); }
442 | star %prec '('
443 { $$ = $1; }
444 | scalar '[' expr ']' %prec '('
445 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
446 | term ARROW '[' expr ']' %prec '('
447 { $$ = newBINOP(OP_AELEM, 0,
448 ref(newAVREF($1),OP_RV2AV),
449 scalar($4));}
450 | term '[' expr ']' %prec '('
451 { assertref($1); $$ = newBINOP(OP_AELEM, 0,
452 ref(newAVREF($1),OP_RV2AV),
453 scalar($3));}
454 | hsh %prec '('
455 { $$ = $1; }
456 | ary %prec '('
457 { $$ = $1; }
458 | arylen %prec '('
459 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
460 | scalar '{' expr ';' '}' %prec '('
461 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
462 expect = XOPERATOR; }
463 | term ARROW '{' expr ';' '}' %prec '('
464 { $$ = newBINOP(OP_HELEM, 0,
465 ref(newHVREF($1),OP_RV2HV),
466 jmaybe($4));
467 expect = XOPERATOR; }
468 | term '{' expr ';' '}' %prec '('
469 { assertref($1); $$ = newBINOP(OP_HELEM, 0,
470 ref(newHVREF($1),OP_RV2HV),
471 jmaybe($3));
472 expect = XOPERATOR; }
473 | '(' expr ')' '[' expr ']' %prec '('
474 { $$ = newSLICEOP(0, $5, $2); }
475 | '(' ')' '[' expr ']' %prec '('
476 { $$ = newSLICEOP(0, $4, Nullop); }
477 | ary '[' expr ']' %prec '('
478 { $$ = prepend_elem(OP_ASLICE,
479 newOP(OP_PUSHMARK, 0),
480 newLISTOP(OP_ASLICE, 0,
481 list($3),
482 ref($1, OP_ASLICE))); }
483 | ary '{' expr ';' '}' %prec '('
484 { $$ = prepend_elem(OP_HSLICE,
485 newOP(OP_PUSHMARK, 0),
486 newLISTOP(OP_HSLICE, 0,
487 list($3),
488 ref(oopsHV($1), OP_HSLICE)));
489 expect = XOPERATOR; }
490 | THING %prec '('
491 { $$ = $1; }
492 | amper
493 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
494 | amper '(' ')'
495 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
496 | amper '(' expr ')'
497 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
498 append_elem(OP_LIST, $3, scalar($1))); }
499 | NOAMP WORD listexpr
500 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
501 append_elem(OP_LIST, $3, scalar($2))); }
502 | DO term %prec UNIOP
503 { $$ = newUNOP(OP_DOFILE, 0, scalar($2)); }
504 | DO block %prec '('
505 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
506 | DO WORD '(' ')'
507 { $$ = newUNOP(OP_ENTERSUB,
508 OPf_SPECIAL|OPf_STACKED,
509 prepend_elem(OP_LIST,
510 scalar(newCVREF(
511 (OPpENTERSUB_AMPER<<8),
512 scalar($2)
513 )),Nullop)); dep();}
514 | DO WORD '(' expr ')'
515 { $$ = newUNOP(OP_ENTERSUB,
516 OPf_SPECIAL|OPf_STACKED,
517 append_elem(OP_LIST,
518 $4,
519 scalar(newCVREF(
520 (OPpENTERSUB_AMPER<<8),
521 scalar($2)
522 )))); dep();}
523 | DO scalar '(' ')'
524 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
525 prepend_elem(OP_LIST,
526 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
527 | DO scalar '(' expr ')'
528 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
529 prepend_elem(OP_LIST,
530 $4,
531 scalar(newCVREF(0,scalar($2))))); dep();}
532 | LOOPEX
533 { $$ = newOP($1, OPf_SPECIAL);
534 hints |= HINT_BLOCK_SCOPE; }
535 | LOOPEX term
536 { $$ = newLOOPEX($1,$2); }
537 | NOTOP argexpr
538 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
539 | UNIOP
540 { $$ = newOP($1, 0); }
541 | UNIOP block
542 { $$ = newUNOP($1, 0, $2); }
543 | UNIOP term
544 { $$ = newUNOP($1, 0, $2); }
545 | UNIOPSUB term
546 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
547 append_elem(OP_LIST, $2, scalar($1))); }
548 | FUNC0
549 { $$ = newOP($1, 0); }
550 | FUNC0 '(' ')'
551 { $$ = newOP($1, 0); }
552 | FUNC0SUB
553 { $$ = newUNOP(OP_ENTERSUB, 0,
554 scalar($1)); }
555 | FUNC1 '(' ')'
556 { $$ = newOP($1, OPf_SPECIAL); }
557 | FUNC1 '(' expr ')'
558 { $$ = newUNOP($1, 0, $3); }
559 | PMFUNC '(' term ')'
560 { $$ = pmruntime($1, $3, Nullop); }
561 | PMFUNC '(' term ',' term ')'
562 { $$ = pmruntime($1, $3, $5); }
563 | WORD
564 | listop
565 ;
566
567listexpr: /* NULL */
568 { $$ = Nullop; }
569 | argexpr
570 { $$ = $1; }
571 ;
572
573listexprcom: /* NULL */
574 { $$ = Nullop; }
575 | expr
576 { $$ = $1; }
577 | expr ','
578 { $$ = $1; }
579 ;
580
581local : LOCAL { $$ = 0; }
582 | MY { $$ = 1; }
583 ;
584
585my_scalar: scalar
586 { in_my = 0; $$ = my($1); }
587 ;
588
589amper : '&' indirob
590 { $$ = newCVREF($1,$2); }
591 ;
592
593scalar : '$' indirob
594 { $$ = newSVREF($2); }
595 ;
596
597ary : '@' indirob
598 { $$ = newAVREF($2); }
599 ;
600
601hsh : '%' indirob
602 { $$ = newHVREF($2); }
603 ;
604
605arylen : DOLSHARP indirob
606 { $$ = newAVREF($2); }
607 ;
608
609star : '*' indirob
610 { $$ = newGVREF(0,$2); }
611 ;
612
613indirob : WORD
614 { $$ = scalar($1); }
615 | scalar
616 { $$ = scalar($1); }
617 | block
618 { $$ = scope($1); }
619
620 | PRIVATEREF
621 { $$ = $1; }
622 ;
623
624%% /* PROGRAM */