Skip to content

Commit

Permalink
[Parser] error out if there is no block after 'else'
Browse files Browse the repository at this point in the history
  • Loading branch information
isuckatcs committed Jun 30, 2024
1 parent d801d71 commit 39b9764
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ std::unique_ptr<IfStmt> TheParser::parseIfStmt() {
std::move(trueBranch), std::move(elseIf));
}

if (nextToken.kind != TokenKind::Lbrace)
return error(nextToken.location, "expected 'else' body");

varOrReturn(falseBlock, parseBlock());

return std::make_unique<IfStmt>(location, std::move(condition),
Expand Down
45 changes: 45 additions & 0 deletions test/parser/if.al
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// RUN: compiler %s -ast-dump 2>&1 | filecheck %s
fn main(): void {
// CHECK: [[# @LINE + 1 ]]:8: error: expected expression
if {}

// CHECK: [[# @LINE + 1 ]]:12: error: expected 'if' body
if 0.0 ({}

if 0.0 {}
// CHECK: [[# @LINE + 1 ]]:10: error: expected 'else' body
else ({}

if 0.0 {}
// CHECK: [[# @LINE + 1 ]]:13: error: expected expression
else if {}
// CHECK: [[# @LINE + 1 ]]:5: error: expected expression
else {}

if 0.0 {}
// CHECK: IfStmt
// CHECK-NEXT: NumberLiteral: '0.0'
// CHECK-NEXT: Block

if 0.0 {}
else {}
// CHECK: IfStmt
// CHECK-NEXT: NumberLiteral: '0.0'
// CHECK-NEXT: Block
// CHECK-NEXT: Block

if 0.0 {}
else if 1.0 {}
else if 2.0 {}
else {}
// CHECK: IfStmt
// CHECK-NEXT: NumberLiteral: '0.0'
// CHECK-NEXT: Block
// CHECK-NEXT: IfStmt
// CHECK-NEXT: NumberLiteral: '1.0'
// CHECK-NEXT: Block
// CHECK-NEXT: IfStmt
// CHECK-NEXT: NumberLiteral: '2.0'
// CHECK-NEXT: Block
// CHECK-NEXT: Block
}

0 comments on commit 39b9764

Please sign in to comment.