Skip to content

Commit

Permalink
Create all and any operators
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
leonerd committed Nov 26, 2024
1 parent 3ff76ac commit e9f1634
Show file tree
Hide file tree
Showing 29 changed files with 2,081 additions and 1,790 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -6256,6 +6256,7 @@ t/op/aassign.t test list assign
t/op/alarm.t See if alarm works
t/op/anonconst.t See if :const works
t/op/anonsub.t See if anonymous subroutines work
t/op/any_all.t See if feature 'any_all' works
t/op/append.t See if . works
t/op/args.t See if operations on @_ work
t/op/arith2.t See if arithmetic works
Expand Down
3 changes: 2 additions & 1 deletion embed.fnc
Original file line number Diff line number Diff line change
Expand Up @@ -5897,7 +5897,8 @@ S |int |intuit_method |NN char *s \
|NULLOK NOCHECK CV *cv
S |int |intuit_more |NN char *s \
|NN char *e
S |I32 |lop |I32 f \
S |I32 |lop |enum yytokentype t \
|I32 f \
|U8 x \
|NN char *s
Sr |void |missingterm |NULLOK char *s \
Expand Down
2 changes: 1 addition & 1 deletion embed.h
Original file line number Diff line number Diff line change
Expand Up @@ -1700,7 +1700,7 @@
# define incline(a,b) S_incline(aTHX_ a,b)
# define intuit_method(a,b,c) S_intuit_method(aTHX_ a,b,c)
# define intuit_more(a,b) S_intuit_more(aTHX_ a,b)
# define lop(a,b,c) S_lop(aTHX_ a,b,c)
# define lop(a,b,c,d) S_lop(aTHX_ a,b,c,d)
# define missingterm(a,b) S_missingterm(aTHX_ a,b)
# define parse_ident(a,b,c,d,e,f) S_parse_ident(aTHX_ a,b,c,d,e,f)
# define pending_ident() S_pending_ident(aTHX)
Expand Down
3 changes: 2 additions & 1 deletion ext/Opcode/Opcode.pm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Opcode 1.66;
package Opcode 1.67;

use strict;

Expand Down Expand Up @@ -377,6 +377,7 @@ used to implement a resource attack (e.g., consume all available CPU time).
grepstart grepwhile
mapstart mapwhile
anystart allstart anywhile
enteriter iter
enterloop leaveloop unstack
last next redo
Expand Down
3 changes: 2 additions & 1 deletion gv.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,8 @@ S_maybe_add_coresub(pTHX_ HV * const stash, GV *gv,
case KEY___DATA__: case KEY___END__ :
case KEY_ADJUST : case KEY_AUTOLOAD: case KEY_BEGIN : case KEY_CHECK :
case KEY_DESTROY : case KEY_END : case KEY_INIT : case KEY_UNITCHECK:
case KEY_and : case KEY_catch : case KEY_class :
case KEY_all : case KEY_and : case KEY_any :
case KEY_catch : case KEY_class :
case KEY_continue: case KEY_cmp : case KEY_defer :
case KEY_do : case KEY_dump : case KEY_else : case KEY_elsif :
case KEY_eq : case KEY_eval : case KEY_field :
Expand Down
4 changes: 4 additions & 0 deletions lib/B/Deparse-core.t
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ sub testit {
else {
package test;
use subs ();
no warnings 'experimental::any_all';
import subs $keyword;
$code = "no warnings 'syntax'; no strict 'vars'; sub { ${vars}() = $expr }";
$code = "use feature 'isa';\n$code" if $keyword eq "isa";
Expand Down Expand Up @@ -232,6 +233,9 @@ while (<DATA>) {

# Special cases

testit any => 'CORE::any { $a } $b, $c', 'CORE::any({$a;} $b, $c);';
testit all => 'CORE::all { $a } $b, $c', 'CORE::all({$a;} $b, $c);';

testit dbmopen => 'CORE::dbmopen(%foo, $bar, $baz);';
testit dbmclose => 'CORE::dbmclose %foo;';

Expand Down
14 changes: 9 additions & 5 deletions lib/B/Deparse.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# This is based on the module of the same name by Malcolm Beattie,
# but essentially none of his code remains.

package B::Deparse 1.80;
package B::Deparse 1.81;
use strict;
use Carp;
use B qw(class main_root main_start main_cv svref_2object opnumber perlstring
Expand Down Expand Up @@ -2334,6 +2334,8 @@ my %feature_keywords = (
finally => 'try',
defer => 'defer',
signatures => 'signatures',
any => 'any_all',
all => 'any_all',
);

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

sub mapop {
my $self = shift;
my($op, $cx, $name) = @_;
my($op, $cx) = @_;
my($expr, @exprs);
my $kid = $op->first; # this is the (map|grep)start
my $kid = $op->first; # this is the (map|grep\any|all)start
my ( $name ) = $kid->name =~ m/(.*)start/; # anystart and allstart share anywhile
$kid = $kid->first->sibling; # skip a pushmark
my $code = $kid->first; # skip a null
if (is_scope $code) {
Expand All @@ -3657,8 +3660,9 @@ sub mapop {
$code . join(", ", @exprs), $cx, 5);
}

sub pp_mapwhile { mapop(@_, "map") }
sub pp_grepwhile { mapop(@_, "grep") }
sub pp_mapwhile { mapop(@_) }
sub pp_grepwhile { mapop(@_) }
sub pp_anywhile { mapop(@_) }
sub pp_mapstart { baseop(@_, "map") }
sub pp_grepstart { baseop(@_, "grep") }

Expand Down
1 change: 1 addition & 0 deletions lib/B/Op_private.pm

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 12 additions & 5 deletions lib/warnings.pm

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions op.c
Original file line number Diff line number Diff line change
Expand Up @@ -12060,8 +12060,16 @@ Constructs, checks, and returns a glob reference op.
OP *
Perl_newGVREF(pTHX_ I32 type, OP *o)
{
if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
return newUNOP(OP_NULL, 0, o);
switch(type) {
/* The thing that looks like a GVREF at the start of these operators
* isn't really */
case OP_MAPSTART:
case OP_GREPSTART:
case OP_SORT:
case OP_ANYSTART:
case OP_ALLSTART:
return newUNOP(OP_NULL, 0, o);
}

if (!FEATURE_BAREWORD_FILEHANDLES_IS_ENABLED &&
((PL_opargs[type] >> OASHIFT) & 7) == OA_FILEREF &&
Expand Down Expand Up @@ -13202,7 +13210,10 @@ Perl_ck_grep(pTHX_ OP *o)
{
LOGOP *gwop;
OP *kid;
const OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
const OPCODE type =
o->op_type == OP_GREPSTART ? OP_GREPWHILE :
o->op_type == OP_MAPSTART ? OP_MAPWHILE :
OP_ANYWHILE; /* any and all both share this */

PERL_ARGS_ASSERT_CK_GREP;

Expand Down
25 changes: 24 additions & 1 deletion opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e9f1634

Please sign in to comment.