Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Perlito5 - js - implement special syntax "PATTERN" for split() - #22
  • Loading branch information
fglock committed Oct 20, 2014
1 parent ba4e482 commit 997461e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
11 changes: 11 additions & 0 deletions perlito5.pl
Expand Up @@ -10192,6 +10192,17 @@ package Perlito5::AST::Apply;
my $wantarray = shift;
my $arg = $self->{'arguments'}->[0];
return 'p5sub_prototype(' . $arg->emit_javascript2() . ', ' . Perlito5::Javascript2::escape_string($Perlito5::PKG_NAME) . ')'
}, 'split' => sub {
my $self = shift;
my $level = shift;
my $wantarray = shift;
my @js;
my $arg = $self->{'arguments'}->[0];
if ($arg && $arg->isa('Perlito5::AST::Apply') && $arg->{'code'} eq 'p5:m') {
push(@js, 'new RegExp(' . $arg->{'arguments'}->[0]->emit_javascript2() . ', ' . '"' . $arg->{'arguments'}->[1] . '"' . ')');
shift(@{$self->{'arguments'}})
}
return 'CORE.split(' . '[' . join(', ', @js, map($_->emit_javascript2(), @{$self->{'arguments'}})) . '], ' . Perlito5::Javascript2::to_context($wantarray) . ')'
});
sub Perlito5::AST::Apply::emit_javascript2 {
my $self = shift;
Expand Down
25 changes: 25 additions & 0 deletions src5/lib/Perlito5/Javascript2/Emitter.pm
Expand Up @@ -2571,6 +2571,31 @@ package Perlito5::AST::Apply;
return 'p5sub_prototype(' . $arg->emit_javascript2() . ', ' . Perlito5::Javascript2::escape_string($Perlito5::PKG_NAME) . ')';
},

'split' => sub {
my $self = shift;
my $level = shift;
my $wantarray = shift;
my @js;
my $arg = $self->{arguments}->[0];
if ( $arg
&& $arg->isa('Perlito5::AST::Apply')
&& $arg->{code} eq 'p5:m'
) {
# first argument of split() is a regex
push @js, 'new RegExp('
. $arg->{arguments}->[0]->emit_javascript2() . ', '
. '"' . $arg->{arguments}->[1] . '"'
. ')';
shift @{ $self->{arguments} };
}
return 'CORE.split('
. '[' . join( ', ',
@js,
map( $_->emit_javascript2, @{ $self->{arguments} } ) )
. '], '
. Perlito5::Javascript2::to_context($wantarray)
. ')';
},
);

sub emit_javascript2 {
Expand Down

0 comments on commit 997461e

Please sign in to comment.