diff options
author | Nicholas Clark <nick@ccl4.org> | 2021-04-25 09:20:51 +0000 |
---|---|---|
committer | Ricardo Signes <rjbs@semiotic.systems> | 2021-10-15 09:28:26 -0400 |
commit | e92ce056f2022a3f96487b1b5a1862a3bf9c159c (patch) | |
tree | 5d14a9931bd14077170525a66122cd9a67bf47fc /perly.y | |
parent | 6ce22ce7e7abeb2ba69129f645e82f16d77fbd89 (diff) | |
download | perl-e92ce056f2022a3f96487b1b5a1862a3bf9c159c.tar.gz |
Implement n-at-a-time for loops.
For example, this now works:
for my ($key, $value) (%hash) { ... }
Only for scalars declared with my as a list in the for loop statement.
As many as you want (unless you want more than 4294967296).
Diffstat (limited to 'perly.y')
-rw-r--r-- | perly.y | 21 |
1 files changed, 20 insertions, 1 deletions
@@ -91,7 +91,7 @@ %type <opval> listexpr nexpr texpr iexpr mexpr mnexpr %type <opval> optlistexpr optexpr optrepl indirob listop method %type <opval> formname subname proto cont my_scalar my_var -%type <opval> refgen_topic formblock +%type <opval> list_of_scalars my_list_of_scalars refgen_topic formblock %type <opval> subattrlist myattrlist myattrterm myterm %type <opval> termbinop termunop anonymous termdo %type <opval> termrelop relopchain termeqop eqopchain @@ -426,6 +426,11 @@ barestmt: PLUGSTMT $$ = block_end($remember, newFOROP(0, $my_scalar, $mexpr, $mblock, $cont)); parser->copline = (line_t)$FOR; } + | FOR MY remember PERLY_PAREN_OPEN my_list_of_scalars PERLY_PAREN_CLOSE PERLY_PAREN_OPEN mexpr PERLY_PAREN_CLOSE mblock cont + { + $$ = block_end($remember, newFOROP(0, $my_list_of_scalars, $mexpr, $mblock, $cont)); + parser->copline = (line_t)$FOR; + } | FOR scalar PERLY_PAREN_OPEN remember mexpr PERLY_PAREN_CLOSE mblock cont { $$ = block_end($remember, newFOROP(0, @@ -1377,6 +1382,20 @@ my_scalar: scalar { parser->in_my = 0; $$ = my($scalar); } ; +/* A list of scalars for "for my ($foo, $bar) (@baz)" */ +list_of_scalars: list_of_scalars[list] PERLY_COMMA + { parser->in_my = 0; $$ = $list; } + | list_of_scalars[list] PERLY_COMMA scalar + { + $$ = op_append_elem(OP_LIST, $list, $scalar); + } + | scalar %prec PREC_LOW + ; + +my_list_of_scalars: list_of_scalars + { parser->in_my = 0; $$ = $list_of_scalars; } + ; + my_var : scalar | ary | hsh |