Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement elif.
  • Loading branch information
arnsholt committed Oct 20, 2014
1 parent 2f600b0 commit af5f6f7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/Snake/Actions.nqp
Expand Up @@ -62,8 +62,15 @@ method simple-statement:sym<expr>($/) { make $<EXPR>.ast; }

# 8: Compound statements
method compound-statement:sym<if>($/) {
my $ast := QAST::Op.new(:op<if>, $<EXPR>.ast, $<suite>[0].ast);
$ast.push($<suite>[1].ast) if +$<suite> > 1;
my $ast := QAST::Op.new(:op<if>, $<EXPR>.ast, $<suite>.ast);
my $cur := $ast;
while nqp::elems($<elif>) > 0 {
my $new := QAST::Op.new(:op<if>, nqp::shift($<elif>).ast, nqp::shift($<elif>).ast);
$cur.push: $new;
$cur := $new;
}
$cur.push($<else>.ast) if $<else>;

make $ast;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Snake/Grammar.nqp
Expand Up @@ -194,7 +194,8 @@ proto token compound-statement {*}
rule compound-statement:sym<if> {
:my int $indent := nqp::atpos_i(@*INDENT, 0);
<sym> <EXPR> ':' <suite>
[<.INDENT: $indent> 'else' ':' <suite>]?
[<.INDENT: $indent> 'elif' <elif=.EXPR> ':' <elif=.suite>]?
[<.INDENT: $indent> 'else' ':' <else=.suite>]?
}

proto token suite {*}
Expand Down
10 changes: 9 additions & 1 deletion t/sanity/if.t
@@ -1,8 +1,16 @@
nqp::say('1..2')

if 1:
nqp::say('1..1')
if 0:
nqp::say("BAIL OUT!")
else:
nqp::print('not ')

nqp::say('ok 1 - else attachment')

if 0:
nqp::say("BAIL OUT!")
elif 1:
nqp::say("ok 2 - elif")
else:
nqp::say("BAIL OUT!")

0 comments on commit af5f6f7

Please sign in to comment.