Skip to content

Commit

Permalink
Merge pull request #217 from nbuilding/match-fix
Browse files Browse the repository at this point in the history
Stop double defaults in `match`
  • Loading branch information
SheepTester authored Jul 10, 2021
2 parents 5b65fe6 + 109d6d2 commit 34bd5b6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
8 changes: 7 additions & 1 deletion python/run.n
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
print(["12", "1", "2"][1][1])
print(match ("Why are you?") {
"Who are you?" -> "I am an example."
"How are you?" -> "I am good."
"Why are you?" -> "Why are *you*?"
"Why are you?" -> "Why are *youu*?"
_ -> "I don't understand."
})
17 changes: 17 additions & 0 deletions python/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,23 @@ def type_check_expr(self, expr):
first_value_type = self.type_check_expr(first_value)
for i, match_value in enumerate(match_block.children):
match, value = match_value.children

if (
i != len(match_block.children) - 1
and isinstance(match, lark.Tree)
and len(match.children) == 1
and isinstance(match.children[0], lark.Token)
and match.children[0].type == "NAME"
and match.children[0].value == "_"
):
self.errors.append(
TypeCheckError(
match,
"You cannot have more than one default in a match statement"
)
)
continue

if (
i != len(match_block.children) - 1
and self.type_check_expr(match) != first_match_type
Expand Down

0 comments on commit 34bd5b6

Please sign in to comment.