Skip to content

Commit

Permalink
Perlito5 - js - do-while rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
fglock committed Oct 1, 2015
1 parent b77a11f commit 2a1bbab
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
10 changes: 7 additions & 3 deletions perlito5.pl
Expand Up @@ -10450,8 +10450,6 @@
sub Perlito5::AST::While::emit_javascript2 {
my($self, $level, $wantarray) = @_;
my $cond = $self->{'cond'};
my $do_at_least_once = ref($self->{'body'}) eq 'Perlito5::AST::Apply' && $self->{'body'}->{'code'} eq 'do' ? 1 : 0;
my $body = ref($self->{'body'}) ne 'Perlito5::AST::Block' ? [$self->{'body'}] : $self->{'body'}->{'stmts'};
my @str;
my $old_level = $level;
if ($cond) {
Expand All @@ -10461,7 +10459,13 @@
push(@str, $arg->emit_javascript2_init($level, $wantarray))
}
}
push(@str, 'p5while(' . 'function () {' . chr(10) . Perlito5::Javascript2::tab($level + 2) . (Perlito5::Javascript2::LexicalBlock::->new('block' => $body))->emit_javascript2($level + 2, $wantarray) . chr(10) . Perlito5::Javascript2::tab($level + 1) . '}, ' . Perlito5::Javascript2::emit_function_javascript2($level + 1, 'scalar', $cond) . ', ' . Perlito5::AST::Block::emit_javascript2_continue($self, $level, $wantarray) . ', ' . Perlito5::Javascript2::escape_string($self->{'label'} || '') . ', ' . $do_at_least_once . ')');
if (ref($self->{'body'}) eq 'Perlito5::AST::Apply' && $self->{'body'}->{'code'} eq 'do') {
push(@str, 'do {' . $self->{'body'}->emit_javascript2($level + 2, $wantarray) . chr(10) . Perlito5::Javascript2::tab($level + 1) . '} while (' . Perlito5::Javascript2::to_bool($cond, $level + 2) . ')')
}
else {
my $body = ref($self->{'body'}) ne 'Perlito5::AST::Block' ? [$self->{'body'}] : $self->{'body'}->{'stmts'};
push(@str, 'p5while(' . 'function () {' . chr(10) . Perlito5::Javascript2::tab($level + 2) . (Perlito5::Javascript2::LexicalBlock::->new('block' => $body))->emit_javascript2($level + 2, $wantarray) . chr(10) . Perlito5::Javascript2::tab($level + 1) . '}, ' . Perlito5::Javascript2::emit_function_javascript2($level + 1, 'scalar', $cond) . ', ' . Perlito5::AST::Block::emit_javascript2_continue($self, $level, $wantarray) . ', ' . Perlito5::Javascript2::escape_string($self->{'label'} || '') . ', ' . 0 . ')')
}
if (@str) {
$level = $old_level;
return Perlito5::Javascript2::emit_wrap_javascript2($level, $wantarray, @str)
Expand Down
29 changes: 18 additions & 11 deletions src5/lib/Perlito5/Javascript2/Emitter.pm
Expand Up @@ -2976,15 +2976,6 @@ package Perlito5::AST::While;
my ($self, $level, $wantarray) = @_;
my $cond = $self->{cond};

# body is 'Perlito5::AST::Apply' in this construct:
# do { ... } while ...;
my $do_at_least_once = ref($self->{body}) eq 'Perlito5::AST::Apply' && $self->{body}{code} eq 'do' ? 1 : 0;

my $body =
ref($self->{body}) ne 'Perlito5::AST::Block'
? [ $self->{body} ]
: $self->{body}{stmts};

# extract declarations from 'cond'
my @str;
my $old_level = $level;
Expand All @@ -2998,15 +2989,31 @@ package Perlito5::AST::While;
}
}

push @str, 'p5while('
# body is 'Perlito5::AST::Apply' in this construct:
# do { ... } while ...;
if ( ref($self->{body}) eq 'Perlito5::AST::Apply' && $self->{body}{code} eq 'do' ) {
push @str,
'do {'
. $self->{body}->emit_javascript2($level + 2, $wantarray) . "\n"
. Perlito5::Javascript2::tab($level + 1) . '} while ('
. Perlito5::Javascript2::to_bool($cond, $level + 2)
. ')';
}
else {
my $body =
ref($self->{body}) ne 'Perlito5::AST::Block'
? [ $self->{body} ]
: $self->{body}{stmts};
push @str, 'p5while('
. "function () {\n"
. Perlito5::Javascript2::tab($level + 2) . (Perlito5::Javascript2::LexicalBlock->new( block => $body ))->emit_javascript2($level + 2, $wantarray) . "\n"
. Perlito5::Javascript2::tab($level + 1) . '}, '
. Perlito5::Javascript2::emit_function_javascript2($level + 1, 'scalar', $cond) . ', '
. Perlito5::AST::Block::emit_javascript2_continue($self, $level, $wantarray) . ', '
. Perlito5::Javascript2::escape_string($self->{label} || "") . ', '
. $do_at_least_once
. '0'
. ')';
}

if (@str) {
$level = $old_level;
Expand Down
23 changes: 23 additions & 0 deletions t5/unit/do_while.t
@@ -0,0 +1,23 @@
use feature 'say';

say '1..5';

$_ = 1;
{
say "ok 1 - inside block";
do {
$_++;
next if $_ == 1 || $_ == 3;
say "ok $_ - next in do-while goes to outer block";
} while ( $_ < 5 );
}

say "ok 3 - outside block";
do {
{
$_++;
next if $_ == 1 || $_ == 3;
say "ok $_ - next in do-while in inner block";
}
} while ( $_ < 5 );

0 comments on commit 2a1bbab

Please sign in to comment.