This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #123286] Lone C-style for in a block
[perl5.git] / perly.y
diff --git a/perly.y b/perly.y
index 77773fd..6ba61ae 100644 (file)
--- a/perly.y
+++ b/perly.y
@@ -71,7 +71,8 @@
 %type <opval> sliceme kvslice gelem
 %type <opval> listexpr nexpr texpr iexpr mexpr mnexpr miexpr
 %type <opval> optlistexpr optexpr indirob listop method
-%type <opval> formname subname proto optsubbody cont my_scalar formblock
+%type <opval> formname subname proto optsubbody cont my_scalar my_var
+%type <opval> refgen_topic formblock
 %type <opval> subattrlist myattrlist myattrterm myterm
 %type <opval> realsubbody subsignature termbinop termunop anonymous termdo
 %type <opval> formstmtseq formline formarg
@@ -117,6 +118,7 @@ grammar     :       GRAMPROG
                remember stmtseq
                        {
                          newPROG(block_end($3,$4));
+                         PL_compiling.cop_seq = 0;
                          $$ = 0;
                        }
        |       GRAMEXPR
@@ -192,7 +194,8 @@ formblock:  '=' remember ';' FORMRBRACK formstmtseq ';' '.'
        ;
 
 remember:      /* NULL */      /* start a full lexical scope */
-                       { $$ = block_start(TRUE); }
+                       { $$ = block_start(TRUE);
+                         parser->parsed_sub = 0; }
        ;
 
 mblock :       '{' mremember stmtseq '}'
@@ -203,7 +206,8 @@ mblock      :       '{' mremember stmtseq '}'
        ;
 
 mremember:     /* NULL */      /* start a partial lexical scope */
-                       { $$ = block_start(FALSE); }
+                       { $$ = block_start(FALSE);
+                         parser->parsed_sub = 0; }
        ;
 
 /* A sequence of statements in the program */
@@ -291,6 +295,7 @@ barestmt:   PLUGSTMT
                          ;
                          $$ = (OP*)NULL;
                          intro_my();
+                         parser->parsed_sub = 1;
                        }
        |       PACKAGE WORD WORD ';'
                        {
@@ -305,6 +310,7 @@ barestmt:   PLUGSTMT
                        {
                          SvREFCNT_inc_simple_void(PL_compcv);
                          utilize($1, $2, $4, $5, $6);
+                         parser->parsed_sub = 1;
                          $$ = (OP*)NULL;
                        }
        |       IF '(' remember mexpr ')' mblock else
@@ -364,6 +370,7 @@ barestmt:   PLUGSTMT
                                      newOP(OP_UNSTACK, OPf_SPECIAL),
                                      forop));
                          }
+                         PL_hints |= HINT_BLOCK_SCOPE;
                          $$ = block_end($3, forop);
                          parser->copline = (line_t)$1;
                        }
@@ -378,6 +385,29 @@ barestmt:  PLUGSTMT
                                      op_lvalue($2, OP_ENTERLOOP), $5, $7, $8));
                          parser->copline = (line_t)$1;
                        }
+       |       FOR REFGEN MY remember my_var
+                       { parser->in_my = 0; $<opval>$ = my($5); }
+               '(' mexpr ')' mblock cont
+                       {
+                         $$ = block_end(
+                               $4,
+                               newFOROP(0,
+                                        op_lvalue(
+                                           newUNOP(OP_REFGEN, 0,
+                                                   $<opval>6),
+                                           OP_ENTERLOOP),
+                                        $8, $10, $11)
+                         );
+                         parser->copline = (line_t)$1;
+                       }
+       |       FOR REFGEN refgen_topic '(' remember mexpr ')' mblock cont
+                       {
+                         $$ = block_end($5, newFOROP(
+                               0, op_lvalue(newUNOP(OP_REFGEN, 0,
+                                                    $3),
+                                            OP_ENTERLOOP), $6, $8, $9));
+                         parser->copline = (line_t)$1;
+                       }
        |       FOR '(' remember mexpr ')' mblock cont
                        {
                          $$ = block_end($3,
@@ -430,7 +460,7 @@ formline:   THING formarg
                               parser->copline = CopLINE(PL_curcop)-1;
                          else parser->copline--;
                          $$ = newSTATEOP(0, NULL,
-                                         convert(OP_FORMLINE, 0, list));
+                                         op_convert_list(OP_FORMLINE, 0, list));
                        }
        ;
 
@@ -627,40 +657,40 @@ listexpr: listexpr ','
 
 /* List operators */
 listop :       LSTOP indirob listexpr /* map {...} @args or print $fh @args */
-                       { $$ = convert($1, OPf_STACKED,
+                       { $$ = op_convert_list($1, OPf_STACKED,
                                op_prepend_elem(OP_LIST, newGVREF($1,$2), $3) );
                        }
        |       FUNC '(' indirob expr ')'      /* print ($fh @args */
-                       { $$ = convert($1, OPf_STACKED,
+                       { $$ = op_convert_list($1, OPf_STACKED,
                                op_prepend_elem(OP_LIST, newGVREF($1,$3), $4) );
                        }
        |       term ARROW method '(' optexpr ')' /* $foo->bar(list) */
-                       { $$ = convert(OP_ENTERSUB, OPf_STACKED,
+                       { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
                                op_append_elem(OP_LIST,
                                    op_prepend_elem(OP_LIST, scalar($1), $5),
-                                   newUNOP(OP_METHOD, 0, $3)));
+                                   newMETHOP(OP_METHOD, 0, $3)));
                        }
        |       term ARROW method                     /* $foo->bar */
-                       { $$ = convert(OP_ENTERSUB, OPf_STACKED,
+                       { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
                                op_append_elem(OP_LIST, scalar($1),
-                                   newUNOP(OP_METHOD, 0, $3)));
+                                   newMETHOP(OP_METHOD, 0, $3)));
                        }
        |       METHOD indirob optlistexpr           /* new Class @args */
-                       { $$ = convert(OP_ENTERSUB, OPf_STACKED,
+                       { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
                                op_append_elem(OP_LIST,
                                    op_prepend_elem(OP_LIST, $2, $3),
-                                   newUNOP(OP_METHOD, 0, $1)));
+                                   newMETHOP(OP_METHOD, 0, $1)));
                        }
        |       FUNCMETH indirob '(' optexpr ')'    /* method $object (@args) */
-                       { $$ = convert(OP_ENTERSUB, OPf_STACKED,
+                       { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
                                op_append_elem(OP_LIST,
                                    op_prepend_elem(OP_LIST, $2, $4),
-                                   newUNOP(OP_METHOD, 0, $1)));
+                                   newMETHOP(OP_METHOD, 0, $1)));
                        }
        |       LSTOP optlistexpr                    /* print @args */
-                       { $$ = convert($1, 0, $2); }
+                       { $$ = op_convert_list($1, 0, $2); }
        |       FUNC '(' optexpr ')'                 /* print (@args) */
-                       { $$ = convert($1, 0, $3); }
+                       { $$ = op_convert_list($1, 0, $3); }
        |       LSTOPSUB startanonsub block /* sub f(&@);   f { foo } ... */
                        { SvREFCNT_inc_simple_void(PL_compcv);
                          $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); }
@@ -779,7 +809,7 @@ termunop : '-' term %prec UMINUS                       /* -$x */
                        { $$ = newUNOP(OP_POSTDEC, 0,
                                        op_lvalue(scalar($1), OP_POSTDEC));}
        |       term POSTJOIN    /* implicit join after interpolated ->@ */
-                       { $$ = convert(OP_JOIN, 0,
+                       { $$ = op_convert_list(OP_JOIN, 0,
                                       op_append_elem(
                                        OP_LIST,
                                        newSVREF(scalar(
@@ -1018,6 +1048,15 @@ my_scalar:       scalar
                        { parser->in_my = 0; $$ = my($1); }
        ;
 
+my_var :       scalar
+       |       ary
+       |       hsh
+       ;
+
+refgen_topic:  my_var
+       |       amper
+       ;
+
 amper  :       '&' indirob
                        { $$ = newCVREF($1,$2); }
        ;