Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
support unless+elsif+else
  • Loading branch information
FROGGS committed Apr 7, 2013
1 parent c1b6b95 commit b9c89f6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
4 changes: 2 additions & 2 deletions TODO_compound_statements.md
Expand Up @@ -11,6 +11,8 @@ Done:
if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
unless (EXPR) BLOCK
unless (EXPR) BLOCK else BLOCK
unless (EXPR) BLOCK elsif (EXPR) BLOCK ...
unless (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
LABEL while (EXPR) BLOCK
LABEL for (EXPR; EXPR; EXPR) BLOCK
LABEL for VAR (LIST) BLOCK
Expand All @@ -20,8 +22,6 @@ Done:
Almost (LHF):

given (EXPR) BLOCK
unless (EXPR) BLOCK elsif (EXPR) BLOCK ...
unless (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
LABEL until (EXPR) BLOCK

Todo:
Expand Down
24 changes: 11 additions & 13 deletions lib/Perl6/P5Actions.pm
Expand Up @@ -822,36 +822,34 @@ class Perl6::P5Actions is HLL::Actions does STDActions {


## Statement control

method statement_control:sym<if>($/) {
$*W.get_env('V5DEBUG') && say("statement_control:sym<if>($/)");
sub if_statement($/) {
my $count := +$<xblock> - 1;
my $past := xblock_immediate( $<xblock>[$count].ast );
# push the else block if any, otherwise 'if' returns C<Nil> (per S04)
# push the else block if any
$past.push( $<else>
?? pblock_immediate( $<else>[0].ast )
!! QAST::Var.new(:name('Nil'), :scope('lexical'))
);
# build if/then/elsif structure
# build if/unless + elsif + else structure
while $count > 0 {
$count--;
my $else := $past;
$past := xblock_immediate( $<xblock>[$count].ast );
$past.push($else);
}
make $past;
$past;
}

method statement_control:sym<if>($/) {
$*W.get_env('V5DEBUG') && say("statement_control:sym<if>($/)");
make if_statement($/)
}

method statement_control:sym<unless>($/) {
$*W.get_env('V5DEBUG') && say("statement_control:sym<unless>($/)");
my $past := xblock_immediate( $<xblock>.ast );
my $past := if_statement($/);
$past.op('unless');
# push the else block if any
$past.push( $<else>
?? pblock_immediate( $<else>[0].ast )
!! QAST::Var.new(:name('Nil'), :scope('lexical'))
);
make $past;
make $past
}

method statement_control:sym<while>($/) {
Expand Down
7 changes: 7 additions & 0 deletions lib/Perl6/P5Grammar.pm
Expand Up @@ -1423,6 +1423,13 @@ grammar Perl6::P5Grammar is HLL::Grammar does STD5 {
rule statement_control:sym<unless> {
<sym>
<xblock>
[
[
| 'else'\h*'if' <.typed_panic: 'X::Syntax::Malformed::Elsif'>
| 'elif' { $/.CURSOR.typed_panic('X::Syntax::Malformed::Elsif', what => "elif") }
| 'elsif' <xblock>
]
]*
[ 'else' <else=.sblock> ]?
}

Expand Down

0 comments on commit b9c89f6

Please sign in to comment.