Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
Merge branch 'master' of git@github.com:perl6/nqp-rx
Browse files Browse the repository at this point in the history
Conflicts:
	src/NQP/Grammar.pm
  • Loading branch information
pmichaud committed Nov 14, 2009
2 parents cffd7d9 + 8d6dfe2 commit eb37eeb
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 9 deletions.
20 changes: 15 additions & 5 deletions src/NQP/Actions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,17 @@ method statement($/, $key?) {
my $past;
if $<EXPR> {
my $mc := $<statement_mod_cond>[0];
$past := $mc
?? PAST::Op.new($mc.ast, $<EXPR>.ast, :pasttype(~$mc<sym>), :node($/) )
!! $<EXPR>.ast;
my $ml := $<statement_mod_loop>[0];
if $mc {
$past := PAST::Op.new($mc<cond>.ast, $<EXPR>.ast, :pasttype(~$mc<sym>), :node($/) );
if $ml {
$past := PAST::Op.new($ml<cond>.ast, $past, :pasttype(~$ml<sym>), :node($/) );
}
} elsif $ml {
$past := PAST::Op.new($ml<cond>.ast, $<EXPR>.ast, :pasttype(~$ml<sym>), :node($/) );
} else {
$past := $<EXPR>.ast;
}
}
elsif $<statement_control> { $past := $<statement_control>.ast; }
else { $past := 0; }
Expand Down Expand Up @@ -177,9 +185,11 @@ method blorst($/) {

# Statement modifiers

method statement_mod_cond:sym<if>($/) { make $<mod_expr>.ast; }
method statement_mod_cond:sym<unless>($/) { make $<mod_expr>.ast; }
method statement_mod_cond:sym<if>($/) { make $<cond>.ast; }
method statement_mod_cond:sym<unless>($/) { make $<cond>.ast; }

method statement_mod_loop:sym<while>($/) { make $<cond>.ast; }
method statement_mod_loop:sym<until>($/) { make $<cond>.ast; }

## Terms

Expand Down
12 changes: 9 additions & 3 deletions src/NQP/Grammar.pm
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ token statement {
| <statement_control>
| <EXPR> <.ws>
[
| <statement_mod_cond>
| <statement_mod_cond> <statement_mod_loop>?
| <statement_mod_loop>
]?
]
}
Expand Down Expand Up @@ -185,8 +186,13 @@ token blorst {

proto token statement_mod_cond { <...> }

token statement_mod_cond:sym<if> { <sym> :s <mod_expr=.EXPR> }
token statement_mod_cond:sym<unless> { <sym> :s <mod_expr=.EXPR> }
token statement_mod_cond:sym<if> { <sym> :s <cond=.EXPR> }
token statement_mod_cond:sym<unless> { <sym> :s <cond=.EXPR> }

proto token statement_mod_loop { <...> }

token statement_mod_loop:sym<while> { <sym> :s <cond=.EXPR> }
token statement_mod_loop:sym<until> { <sym> :s <cond=.EXPR> }

## Terms

Expand Down
10 changes: 9 additions & 1 deletion t/nqp/14-while.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# while, until statements

plan(12);
plan(14);

my $a; my $sum;

Expand All @@ -13,13 +13,21 @@ while $a != 10 {
}
ok($sum == 45, 'basic while loop test');

$a := 1; $sum := 0;
$sum := $sum + $a++ while $a < 10;
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');

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

$a := 1; $sum := 0;
while $a != 1 {
$sum := 99;
Expand Down
40 changes: 40 additions & 0 deletions t/nqp/42-cond-loop.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!./parrot nqp.pbc

# combination of conditional modifier and loop modifier

plan(8);

my $a; my $s;


$a := 0; $s := 0;
$s := 5 if $a > 7 while $a++ < 9;
ok( $s == 5 && $a == 10, 'true if + while');

$a := 0; $s := 0;
$s := 5 if $a > 17 while $a++ < 9;
ok( $s == 0 && $a == 10, 'false if + while');

$a := 0; $s := 0;
$s := 5 if $a > 7 until $a++ > 9;
ok( $s == 5 && $a == 11, 'true if + until');

$a := 0; $s := 0;
$s := 5 if $a > 17 until $a++ > 9;
ok( $s == 0 && $a == 11, 'false if + until');

$a := 0; $s := 0;
$s := 5 unless $a > 0 while $a++ < 9;
ok( $s == 0 && $a == 10, 'true unless + while');

$a := 0; $s := 0;
$s := 5 unless $a < 0 while $a++ < 9;
ok( $s == 5 && $a == 10, 'false unless + while');

$a := 0; $s := 0;
$s := 5 if $a > 0 until $a++ > 9;
ok( $s == 5 && $a == 11, 'true unless + until');

$a := 0; $s := 0;
$s := 5 if $a < 0 until $a++ > 9;
ok( $s == 0 && $a == 11, 'false unless + until');

0 comments on commit eb37eeb

Please sign in to comment.