Skip to content

Commit df4f5f1

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 aeae5cd commit df4f5f1

29 files changed

+2090
-1790
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_all' works
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
@@ -5897,7 +5897,8 @@ S |int |intuit_method |NN char *s \
58975897
|NULLOK NOCHECK CV *cv
58985898
S |int |intuit_more |NN char *s \
58995899
|NN char *e
5900-
S |I32 |lop |I32 f \
5900+
S |I32 |lop |enum yytokentype t \
5901+
|I32 f \
59015902
|U8 x \
59025903
|NN char *s
59035904
Sr |void |missingterm |NULLOK char *s \

embed.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1700,7 +1700,7 @@
17001700
# define incline(a,b) S_incline(aTHX_ a,b)
17011701
# define intuit_method(a,b,c) S_intuit_method(aTHX_ a,b,c)
17021702
# define intuit_more(a,b) S_intuit_more(aTHX_ a,b)
1703-
# define lop(a,b,c) S_lop(aTHX_ a,b,c)
1703+
# define lop(a,b,c,d) S_lop(aTHX_ a,b,c,d)
17041704
# define missingterm(a,b) S_missingterm(aTHX_ a,b)
17051705
# define parse_ident(a,b,c,d,e,f) S_parse_ident(aTHX_ a,b,c,d,e,f)
17061706
# 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 'experimental::any_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: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# This is based on the module of the same name by Malcolm Beattie,
88
# but essentially none of his code remains.
99

10-
package B::Deparse 1.80;
10+
package B::Deparse 1.81;
1111
use strict;
1212
use Carp;
1313
use B qw(class main_root main_start main_cv svref_2object opnumber perlstring
@@ -2334,6 +2334,8 @@ my %feature_keywords = (
23342334
finally => 'try',
23352335
defer => 'defer',
23362336
signatures => 'signatures',
2337+
any => 'any_all',
2338+
all => 'any_all',
23372339
);
23382340

23392341
# keywords that are strong and also have a prototype
@@ -3637,9 +3639,10 @@ sub pp_sort { indirop(@_, "sort") }
36373639

36383640
sub mapop {
36393641
my $self = shift;
3640-
my($op, $cx, $name) = @_;
3642+
my($op, $cx) = @_;
36413643
my($expr, @exprs);
3642-
my $kid = $op->first; # this is the (map|grep)start
3644+
my $kid = $op->first; # this is the (map|grep|any|all)start
3645+
my ( $name ) = $kid->name =~ m/(.*)start/; # anystart and allstart share anywhile
36433646
$kid = $kid->first->sibling; # skip a pushmark
36443647
my $code = $kid->first; # skip a null
36453648
if (is_scope $code) {
@@ -3657,8 +3660,9 @@ sub mapop {
36573660
$code . join(", ", @exprs), $cx, 5);
36583661
}
36593662

3660-
sub pp_mapwhile { mapop(@_, "map") }
3661-
sub pp_grepwhile { mapop(@_, "grep") }
3663+
sub pp_mapwhile { mapop(@_) }
3664+
sub pp_grepwhile { mapop(@_) }
3665+
sub pp_anywhile { mapop(@_) }
36623666
sub pp_mapstart { baseop(@_, "map") }
36633667
sub pp_grepstart { baseop(@_, "grep") }
36643668

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: 12 additions & 5 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
@@ -12060,8 +12060,16 @@ Constructs, checks, and returns a glob reference op.
1206012060
OP *
1206112061
Perl_newGVREF(pTHX_ I32 type, OP *o)
1206212062
{
12063-
if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
12064-
return newUNOP(OP_NULL, 0, o);
12063+
switch(type) {
12064+
/* The thing that looks like a GVREF at the start of these operators
12065+
* isn't really */
12066+
case OP_MAPSTART:
12067+
case OP_GREPSTART:
12068+
case OP_SORT:
12069+
case OP_ANYSTART:
12070+
case OP_ALLSTART:
12071+
return newUNOP(OP_NULL, 0, o);
12072+
}
1206512073

1206612074
if (!FEATURE_BAREWORD_FILEHANDLES_IS_ENABLED &&
1206712075
((PL_opargs[type] >> OASHIFT) & 7) == OA_FILEREF &&
@@ -13202,7 +13210,10 @@ Perl_ck_grep(pTHX_ OP *o)
1320213210
{
1320313211
LOGOP *gwop;
1320413212
OP *kid;
13205-
const OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
13213+
const OPCODE type =
13214+
o->op_type == OP_GREPSTART ? OP_GREPWHILE :
13215+
o->op_type == OP_MAPSTART ? OP_MAPWHILE :
13216+
OP_ANYWHILE; /* any and all both share this */
1320613217

1320713218
PERL_ARGS_ASSERT_CK_GREP;
1320813219

0 commit comments

Comments
 (0)