summaryrefslogtreecommitdiff
path: root/perly.y
diff options
context:
space:
mode:
authorNicholas Clark <nick@ccl4.org>2021-04-25 09:20:51 +0000
committerRicardo Signes <rjbs@semiotic.systems>2021-10-15 09:28:26 -0400
commite92ce056f2022a3f96487b1b5a1862a3bf9c159c (patch)
tree5d14a9931bd14077170525a66122cd9a67bf47fc /perly.y
parent6ce22ce7e7abeb2ba69129f645e82f16d77fbd89 (diff)
downloadperl-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.y21
1 files changed, 20 insertions, 1 deletions
diff --git a/perly.y b/perly.y
index 18ed6e8fc9..2e2935c60e 100644
--- a/perly.y
+++ b/perly.y
@@ -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