Skip to content

Commit bbf13b0

Browse files
committed
compiler: Match hole and add test for matching variants
1 parent a375085 commit bbf13b0

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

compiler.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ def try_match(self, env: Env, arg: str, pattern: Object, fallthrough: str) -> En
160160
self._emit(f"if (!is_num({arg})) {{ goto {fallthrough}; }}")
161161
self._emit(f"if (num_value({arg}) != {pattern.value}) {{ goto {fallthrough}; }}")
162162
return {}
163+
if isinstance(pattern, Hole):
164+
self._emit(f"if (!is_hole({arg})) {{ goto {fallthrough}; }}")
165+
return {}
163166
if isinstance(pattern, Variant):
164167
self.variant_tag(pattern.tag) # register it for the big enum
165168
self._emit(f"if (!is_variant({arg})) {{ goto {fallthrough}; }}")

compiler_tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ def test_match_list(self) -> None:
5252
def test_match_record(self) -> None:
5353
self.assertEqual(self._run("f {a = 4, b = 5} . f = | {a = 1, b = 2} -> 3 | {a = 4, b = 5} -> 6"), "6\n")
5454

55+
def test_match_hole(self) -> None:
56+
self.assertEqual(self._run("f () . f = | 1 -> 3 | () -> 4"), "4\n")
57+
58+
def test_match_variant(self) -> None:
59+
self.assertEqual(self._run("f #foo () . f = | # bar 1 -> 3 | # foo () -> 4"), "4\n")
60+
5561

5662
if __name__ == "__main__":
5763
unittest.main()

0 commit comments

Comments
 (0)