Skip to content

Commit

Permalink
Implement postfix if/while/unless/until
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Jul 28, 2010
1 parent 02a9152 commit d4465fa
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
56 changes: 54 additions & 2 deletions Niecza/Actions.pm
Expand Up @@ -1001,6 +1001,10 @@ sub terminator__S_Thesis {}
sub terminator__S_Semi {}
sub terminator__S_Ket {}
sub terminator__S_Ly {}
sub terminator__S_if {}
sub terminator__S_unless {}
sub terminator__S_for {}
sub terminator__S_until {}
sub terminator__S_then {}
sub terminator__S_again {}
sub terminator__S_repeat {}
Expand Down Expand Up @@ -1169,6 +1173,9 @@ sub nulltermish { my ($cl, $M) = @_; # for 1,2,3,
$M->{term}{_ast} = $M->{term}{term}{_ast} if $M->{term};
}
sub EXPR {}
sub modifier_expr { my ($cl, $M) = @_;
$M->{_ast} = $M->{EXPR}{_ast};
}

sub arglist { my ($cl, $M) = @_;
$M->sorry("Invocant handling is NYI") if $::INVOCANT_IS;
Expand Down Expand Up @@ -1198,15 +1205,60 @@ sub args { my ($cl, $M) = @_;
}

sub statement { my ($cl, $M) = @_;
if ($M->{label} || $M->{statement_mod_cond}[0] || $M->{statement_mod_loop}[0]) {
$M->sorry("Control is NYI");
if ($M->{label}) {
$M->sorry("Labels are NYI");
return;
}

$M->{_ast} = $M->{statement_control} ? $M->{statement_control}{_ast} :
$M->{EXPR} ? $M->{EXPR}{_ast} : undef;

if ($M->{statement_mod_cond}[0]) {
my ($sym, $exp) = @{ $M->{statement_mod_cond}[0]{_ast} };

if ($sym eq 'if') {
$M->{_ast} = Op::Conditional->new(node($M), check => $exp,
true => $M->{_ast}, false => undef);
} elsif ($sym eq 'unless') {
$M->{_ast} = Op::Conditional->new(node($M), check => $exp,
false => $M->{_ast}, true => undef);
} else {
$M->sorry("Unhandled statement modifier $sym");
return;
}
}

if ($M->{statement_mod_loop}[0]) {
my ($sym, $exp) = @{ $M->{statement_mod_loop}[0]{_ast} };

if ($sym eq 'while') {
$M->{_ast} = Op::WhileLoop->new(node($M), check => $exp,
body => $M->{_ast}, until => 0, once => 0);
} elsif ($sym eq 'until') {
$M->{_ast} = Op::WhileLoop->new(node($M), check => $exp,
body => $M->{_ast}, until => 1, once => 0);
} else {
$M->sorry("Unhandled statement modifier $sym");
return;
}
}
}

sub statement_mod_cond { my ($cl, $M) = @_;
$M->{_ast} = [ $M->{sym}, $M->{modifier_expr}{_ast} ];
}
sub statement_mod_loop { my ($cl, $M) = @_;
$M->{_ast} = [ $M->{sym}, $M->{modifier_expr}{_ast} ];
}

sub statement_mod_cond__S_if {}
sub statement_mod_cond__S_unless {}
sub statement_mod_cond__S_when {}
sub statement_mod_loop__S_while {}
sub statement_mod_loop__S_until {}
sub statement_mod_loop__S_for {}
sub statement_mod_loop__S_given {}

sub statementlist { my ($cl, $M) = @_;
$M->{_ast} = Op::StatementList->new(node($M), children =>
[ map { $_->statement_level } grep { defined }
Expand Down
14 changes: 13 additions & 1 deletion test.pl
Expand Up @@ -2,7 +2,7 @@

use Test;

plan 137;
plan 141;

ok 1, "one is true";
ok 2, "two is also true";
Expand Down Expand Up @@ -403,3 +403,15 @@
ok Foo::Bar.bar == 51, "can call through nested methods";
ok GLOBAL::Foo::Bar.bar == 51, "can call through GLOBAL nested";
}

{
my $x1; my $x2; my $x3; my $x4;
$x1 = 1 if 0;
$x2 = 1 if 1;
$x3 = 1 unless 0;
$x4 = 1 unless 1;
ok !$x1, "if 0 doesn't execute";
ok $x2, "if 1 does execute";
ok $x3, "unless 0 does execute";
ok !$x4, "unless 1 doesn't execute";
}

0 comments on commit d4465fa

Please sign in to comment.