Skip to content

Commit 997461e

Browse files
committed
Perlito5 - js - implement special syntax "PATTERN" for split() - #22
1 parent ba4e482 commit 997461e

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

perlito5.pl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10192,6 +10192,17 @@ package Perlito5::AST::Apply;
1019210192
my $wantarray = shift;
1019310193
my $arg = $self->{'arguments'}->[0];
1019410194
return 'p5sub_prototype(' . $arg->emit_javascript2() . ', ' . Perlito5::Javascript2::escape_string($Perlito5::PKG_NAME) . ')'
10195+
}, 'split' => sub {
10196+
my $self = shift;
10197+
my $level = shift;
10198+
my $wantarray = shift;
10199+
my @js;
10200+
my $arg = $self->{'arguments'}->[0];
10201+
if ($arg && $arg->isa('Perlito5::AST::Apply') && $arg->{'code'} eq 'p5:m') {
10202+
push(@js, 'new RegExp(' . $arg->{'arguments'}->[0]->emit_javascript2() . ', ' . '"' . $arg->{'arguments'}->[1] . '"' . ')');
10203+
shift(@{$self->{'arguments'}})
10204+
}
10205+
return 'CORE.split(' . '[' . join(', ', @js, map($_->emit_javascript2(), @{$self->{'arguments'}})) . '], ' . Perlito5::Javascript2::to_context($wantarray) . ')'
1019510206
});
1019610207
sub Perlito5::AST::Apply::emit_javascript2 {
1019710208
my $self = shift;

src5/lib/Perlito5/Javascript2/Emitter.pm

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2571,6 +2571,31 @@ package Perlito5::AST::Apply;
25712571
return 'p5sub_prototype(' . $arg->emit_javascript2() . ', ' . Perlito5::Javascript2::escape_string($Perlito5::PKG_NAME) . ')';
25722572
},
25732573

2574+
'split' => sub {
2575+
my $self = shift;
2576+
my $level = shift;
2577+
my $wantarray = shift;
2578+
my @js;
2579+
my $arg = $self->{arguments}->[0];
2580+
if ( $arg
2581+
&& $arg->isa('Perlito5::AST::Apply')
2582+
&& $arg->{code} eq 'p5:m'
2583+
) {
2584+
# first argument of split() is a regex
2585+
push @js, 'new RegExp('
2586+
. $arg->{arguments}->[0]->emit_javascript2() . ', '
2587+
. '"' . $arg->{arguments}->[1] . '"'
2588+
. ')';
2589+
shift @{ $self->{arguments} };
2590+
}
2591+
return 'CORE.split('
2592+
. '[' . join( ', ',
2593+
@js,
2594+
map( $_->emit_javascript2, @{ $self->{arguments} } ) )
2595+
. '], '
2596+
. Perlito5::Javascript2::to_context($wantarray)
2597+
. ')';
2598+
},
25742599
);
25752600

25762601
sub emit_javascript2 {

0 commit comments

Comments
 (0)