Skip to content

Commit

Permalink
Implement syntax sugar for optional else, closes #11
Browse files Browse the repository at this point in the history
  • Loading branch information
sparkprime committed Aug 12, 2014
1 parent 291c3e6 commit 73ce797
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 7 additions & 2 deletions parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,8 +578,13 @@ namespace {
AST *cond = parse(MAX_PRECEDENCE, obj_level);
popExpect(Token::THEN);
AST *branch_true = parse(MAX_PRECEDENCE, obj_level);
popExpect(Token::ELSE);
AST *branch_false = parse(MAX_PRECEDENCE, obj_level);
AST *branch_false;
if (peek().kind == Token::ELSE) {
pop();
branch_false = parse(MAX_PRECEDENCE, obj_level);
} else {
branch_false = alloc.make<LiteralNull>(span(begin, branch_true));
}
return alloc.make<Conditional>(span(begin, branch_false),
cond, branch_true, branch_false);
}
Expand Down
2 changes: 2 additions & 0 deletions test_suite/condition.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ std.assertEqual(f(0), "zero") &&
std.assertEqual(f(3), "small") &&
std.assertEqual(f(8), "large") &&
std.assertEqual(f(100), "huge") &&
std.assertEqual(if 1 > 0 then 1, 1) &&
std.assertEqual(if 1 > 2 then 1, null) &&

true

0 comments on commit 73ce797

Please sign in to comment.