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