Skip to content

Commit 2cd9794

Browse files
committed
Create all and any operators
As these only permit `any BLOCK LIST` and not the deferred-expression form like `grep EXPR, LIST`, we create a whole new token type, BLKLSTOP, to represent these. The parser then only accepts the block form and not the expression form when parsing these.
1 parent c1bde84 commit 2cd9794

29 files changed

+2259
-1950
lines changed

MANIFEST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6256,6 +6256,7 @@ t/op/aassign.t test list assign
62566256
t/op/alarm.t See if alarm works
62576257
t/op/anonconst.t See if :const works
62586258
t/op/anonsub.t See if anonymous subroutines work
6259+
t/op/any_all.t See if feature 'any' and 'all' work
62596260
t/op/append.t See if . works
62606261
t/op/args.t See if operations on @_ work
62616262
t/op/arith2.t See if arithmetic works

embed.fnc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5957,7 +5957,8 @@ S |int |intuit_method |NN char *s \
59575957
|NULLOK NOCHECK CV *cv
59585958
S |int |intuit_more |NN char *s \
59595959
|NN char *e
5960-
S |I32 |lop |I32 f \
5960+
S |I32 |lop |enum yytokentype t \
5961+
|I32 f \
59615962
|U8 x \
59625963
|NN char *s
59635964
Sr |void |missingterm |NULLOK char *s \

embed.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1715,7 +1715,7 @@
17151715
# define incline(a,b) S_incline(aTHX_ a,b)
17161716
# define intuit_method(a,b,c) S_intuit_method(aTHX_ a,b,c)
17171717
# define intuit_more(a,b) S_intuit_more(aTHX_ a,b)
1718-
# define lop(a,b,c) S_lop(aTHX_ a,b,c)
1718+
# define lop(a,b,c,d) S_lop(aTHX_ a,b,c,d)
17191719
# define missingterm(a,b) S_missingterm(aTHX_ a,b)
17201720
# define parse_ident(a,b,c,d,e,f) S_parse_ident(aTHX_ a,b,c,d,e,f)
17211721
# define pending_ident() S_pending_ident(aTHX)

ext/Opcode/Opcode.pm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package Opcode 1.66;
1+
package Opcode 1.67;
22

33
use strict;
44

@@ -377,6 +377,7 @@ used to implement a resource attack (e.g., consume all available CPU time).
377377
378378
grepstart grepwhile
379379
mapstart mapwhile
380+
anystart allstart anywhile
380381
enteriter iter
381382
enterloop leaveloop unstack
382383
last next redo

gv.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,8 @@ S_maybe_add_coresub(pTHX_ HV * const stash, GV *gv,
610610
case KEY___DATA__: case KEY___END__ :
611611
case KEY_ADJUST : case KEY_AUTOLOAD: case KEY_BEGIN : case KEY_CHECK :
612612
case KEY_DESTROY : case KEY_END : case KEY_INIT : case KEY_UNITCHECK:
613-
case KEY_and : case KEY_catch : case KEY_class :
613+
case KEY_all : case KEY_and : case KEY_any :
614+
case KEY_catch : case KEY_class :
614615
case KEY_continue: case KEY_cmp : case KEY_defer :
615616
case KEY_do : case KEY_dump : case KEY_else : case KEY_elsif :
616617
case KEY_eq : case KEY_eval : case KEY_field :

lib/B/Deparse-core.t

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ sub testit {
9191
else {
9292
package test;
9393
use subs ();
94+
no warnings qw( experimental::any experimental::all );
9495
import subs $keyword;
9596
$code = "no warnings 'syntax'; no strict 'vars'; sub { ${vars}() = $expr }";
9697
$code = "use feature 'isa';\n$code" if $keyword eq "isa";
@@ -232,6 +233,9 @@ while (<DATA>) {
232233

233234
# Special cases
234235

236+
testit any => 'CORE::any { $a } $b, $c', 'CORE::any({$a;} $b, $c);';
237+
testit all => 'CORE::all { $a } $b, $c', 'CORE::all({$a;} $b, $c);';
238+
235239
testit dbmopen => 'CORE::dbmopen(%foo, $bar, $baz);';
236240
testit dbmclose => 'CORE::dbmclose %foo;';
237241

lib/B/Deparse.pm

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,6 +2348,8 @@ my %feature_keywords = (
23482348
finally => 'try',
23492349
defer => 'defer',
23502350
signatures => 'signatures',
2351+
any => 'any',
2352+
all => 'all',
23512353
);
23522354

23532355
# keywords that are strong and also have a prototype
@@ -3652,9 +3654,10 @@ sub pp_sort { indirop(@_, "sort") }
36523654

36533655
sub mapop {
36543656
my $self = shift;
3655-
my($op, $cx, $name) = @_;
3657+
my($op, $cx) = @_;
36563658
my($expr, @exprs);
3657-
my $kid = $op->first; # this is the (map|grep)start
3659+
my $kid = $op->first; # this is the (map|grep|any|all)start
3660+
my ( $name ) = $kid->name =~ m/(.*)start/; # anystart and allstart share anywhile
36583661
$kid = $kid->first->sibling; # skip a pushmark
36593662
my $code = $kid->first; # skip a null
36603663
if (is_scope $code) {
@@ -3672,8 +3675,9 @@ sub mapop {
36723675
$code . join(", ", @exprs), $cx, 5);
36733676
}
36743677

3675-
sub pp_mapwhile { mapop(@_, "map") }
3676-
sub pp_grepwhile { mapop(@_, "grep") }
3678+
sub pp_mapwhile { mapop(@_) }
3679+
sub pp_grepwhile { mapop(@_) }
3680+
sub pp_anywhile { mapop(@_) }
36773681
sub pp_mapstart { baseop(@_, "map") }
36783682
sub pp_grepstart { baseop(@_, "grep") }
36793683

lib/B/Op_private.pm

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/warnings.pm

Lines changed: 175 additions & 163 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

op.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12089,8 +12089,16 @@ Constructs, checks, and returns a glob reference op.
1208912089
OP *
1209012090
Perl_newGVREF(pTHX_ I32 type, OP *o)
1209112091
{
12092-
if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
12093-
return newUNOP(OP_NULL, 0, o);
12092+
switch(type) {
12093+
/* The thing that looks like a GVREF at the start of these operators
12094+
* isn't really */
12095+
case OP_MAPSTART:
12096+
case OP_GREPSTART:
12097+
case OP_SORT:
12098+
case OP_ANYSTART:
12099+
case OP_ALLSTART:
12100+
return newUNOP(OP_NULL, 0, o);
12101+
}
1209412102

1209512103
if (!FEATURE_BAREWORD_FILEHANDLES_IS_ENABLED &&
1209612104
((PL_opargs[type] >> OASHIFT) & 7) == OA_FILEREF &&
@@ -13231,7 +13239,10 @@ Perl_ck_grep(pTHX_ OP *o)
1323113239
{
1323213240
LOGOP *gwop;
1323313241
OP *kid;
13234-
const OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
13242+
const OPCODE type =
13243+
o->op_type == OP_GREPSTART ? OP_GREPWHILE :
13244+
o->op_type == OP_MAPSTART ? OP_MAPWHILE :
13245+
OP_ANYWHILE; /* any and all both share this */
1323513246

1323613247
PERL_ARGS_ASSERT_CK_GREP;
1323713248

0 commit comments

Comments
 (0)