Skip to content

Commit

Permalink
[dotnet] finish implementing pasttype while; implement until, repeat_…
Browse files Browse the repository at this point in the history
…while, repeat_until; add 14-while.t (all pass); add 05-comments.t b/c it passes.
  • Loading branch information
diakopter committed Nov 1, 2010
1 parent e7e4edb commit ad6fd43
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 5 deletions.
60 changes: 55 additions & 5 deletions dotnet/compiler/PAST2DNSTCompiler.pm
Expand Up @@ -681,19 +681,26 @@ our multi sub dnst_for(PAST::Op $op) {
return $result;
}

elsif $op.pasttype eq 'while' {
elsif $op.pasttype eq 'while' || $op.pasttype eq 'until' {
# Need labels for start and end.
my $test_label := get_unique_id('while_lab');
my $end_label := get_unique_id('while_end_lab');
my $cond_result := get_unique_id('cond');

# Compile the condition.
my $cond := DNST::Temp.new(
:name($cond_result), :type('RakudoObject'),
dnst_for(PAST::Op.new(

my $cop := $op.pasttype eq 'until'
?? PAST::Op.new(
:pasttype('call'), :name('&prefix:<!>'),
(@($op))[0]
)
!! PAST::Op.new(
:pasttype('callmethod'), :name('Bool'),
(@($op))[0]
))
);
my $cond := DNST::Temp.new(
:name($cond_result), :type('RakudoObject'),
dnst_for($cop)
);

# Compile the body.
Expand All @@ -719,6 +726,49 @@ our multi sub dnst_for(PAST::Op $op) {
);
}

elsif $op.pasttype eq 'repeat_while' || $op.pasttype eq 'repeat_until' {
# Need labels for start and end.
my $test_label := get_unique_id('while_lab');
my $block_label := get_unique_id('block_lab');
my $cond_result := get_unique_id('cond');

# Compile the condition.

my $cop := $op.pasttype eq 'repeat_until'
?? PAST::Op.new(
:pasttype('call'), :name('&prefix:<!>'),
(@($op))[0]
)
!! PAST::Op.new(
:pasttype('callmethod'), :name('Bool'),
(@($op))[0]
);
my $cond := DNST::Temp.new(
:name($cond_result), :type('RakudoObject'),
dnst_for($cop)
);

# Compile the body.
my $body := dnst_for((@($op))[1]);

# Build up result.
return DNST::Stmts.new(
DNST::Label.new( :name($block_label) ),
$body,
$cond,
DNST::If.new(
DNST::MethodCall.new(
:on('Ops'), :name('unbox_int'), :type('int'),
'TC', $cond_result
),
DNST::Stmts.new(
$cond_result,
DNST::Goto.new( :label($block_label) )
)
)
);
}

elsif $op.pasttype eq 'list' {
my $tmp_name := get_unique_id('list_');
my $result := DNST::Stmts.new(
Expand Down
57 changes: 57 additions & 0 deletions t/nqp/05-comments.t
@@ -0,0 +1,57 @@
#! nqp

# check comments

say('1..8');

#Comment preceding
say("ok 1");

say("ok 2"); #Comment following

#say("not ok 3");
# say("not ok 4");

{ say('ok 3'); } # comment
{ say('ok 4'); }

=for comment
say("not ok 5");
=for comment say("not ok 6");
=begin comment
say("not ok 7");
say("not ok 8");
=end comment

=comment say("not ok 9");
say("not ok 10");
=for comment blah
say("ok 5");

=begin comment
=end comment
say("ok 6");

# This doesn't quite work right... but it doesn't work in STD either
#=for comment
#=begin comment
#=end comment
#=say("ok 7");

=comment

say("ok 7");

=begin comment indented pod
this is indented pod
say("not ok 8");
=end comment

say("ok 8");


104 changes: 104 additions & 0 deletions t/nqp/14-while.t
@@ -0,0 +1,104 @@
#!./parrot nqp.pbc

# while, until statements

plan(14);

my $a; my $sum;

$a := 1; $sum := 0;
while $a != 10 {
$sum := $sum + $a;
$a := $a + 1;
}
ok($sum == 45, 'basic while loop test');

$sum := 0;
$sum := $sum + 1 while $sum < 45;
ok($sum == 45, 'basic while statement modifier');

$a := 1; $sum := 0;
until $a == 10 {
$sum := $sum + $a;
$a := $a + 1;
}
ok($sum == 45, 'basic until loop test');

$sum := 0;
$sum := $sum + 1 until $sum > 44;
ok($sum == 45, 'basic until statement modifier');

$a := 1; $sum := 0;
while $a != 1 {
$sum := 99;
$a := 1;
}
ok($sum == 0, 'while loop exits initial false immediately');

$a := 1; $sum := 0;
until $a == 1 {
$sum := 99;
$a := 1;
}
ok($sum == 0, 'until loop exits initial true immediately');

$a := 1; $sum := 0;
repeat {
$sum := $sum + $a;
$a := $a + 1;
} while $a != 10;
ok($sum == 45, 'basic repeat_while loop');

$a := 1; $sum := 0;
repeat {
$sum := $sum + $a;
$a := $a + 1;
} until $a == 10;
ok($sum == 45, 'basic repeat_until loop');

$a := 1; $sum := 0;
repeat while $a != 10 {
$sum := $sum + $a;
$a := $a + 1;
};
ok($sum == 45, 'basic repeat_while loop');

$a := 1; $sum := 0;
repeat until $a == 10 {
$sum := $sum + $a;
$a := $a + 1;
};
ok($sum == 45, 'basic repeat_until loop');

$a := 1; $sum := 0;
repeat {
$sum := 99;
} while $a != 1;
ok($sum == 99, 'repeat_while always executes at least once');

$a := 1; $sum := 0;
repeat {
$sum := 99;
} until $a == 1;
ok($sum == 99, 'repeat_until always executes at least once');

$a := 1; $sum := 0;
repeat while $a != 1 {
$sum := 99;
};
ok($sum == 99, 'repeat_while always executes at least once');

$a := 1; $sum := 0;
repeat until $a == 1 {
$sum := 99;
};
ok($sum == 99, 'repeat_until always executes at least once');









0 comments on commit ad6fd43

Please sign in to comment.