Skip to content

Commit aa505e7

Browse files
committed
Allow if/else if.../else statements
Fix #37.
1 parent fa17ce3 commit aa505e7

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/grammar.rustpeg

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ if_statement -> Stmt
5050
}
5151
)
5252
}
53+
/ IF __ e:expr_node __ lpos1:#position b1:block rpos1:#position __ ELSE __ lpos2:#position elseif:if_statement rpos2:#position __ {
54+
Stmt::IfThen(
55+
IfThenStmt {
56+
cond: e,
57+
then_block: Box::new(StmtNode { pos: (lpos1, rpos1), data: b1 }),
58+
maybe_else_block: Some(Box::new(StmtNode { pos: (lpos2, rpos2), data: elseif })),
59+
}
60+
)
61+
}
5362
/ IF e:expr_node lpos:#position b:block rpos:#position {
5463
Stmt::IfThen(
5564
IfThenStmt {

tests/run-pass/if-else-if.bl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var x = 0;
2+
if false {
3+
x = 1;
4+
} else if false {
5+
x = 2;
6+
} else {
7+
x = 3;
8+
}
9+
assert_eq(x, 3);
10+
11+
var y = 0;
12+
if false {
13+
y = 1;
14+
} else if true {
15+
y = 2;
16+
} else {
17+
y = 3;
18+
}
19+
assert_eq(y, 2);

0 commit comments

Comments
 (0)