Skip to content

Commit 001dc07

Browse files
committed
compiler: Support pattern matching on record patterns
1 parent d049541 commit 001dc07

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

compiler.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,15 @@ def try_match(self, env: Env, arg: str, pattern: Object, fallthrough: str) -> En
195195
# Too many elements
196196
self._emit(f"if (!is_empty_list({the_list})) {{ goto {fallthrough}; }}")
197197
return updates
198+
if isinstance(pattern, Record):
199+
self._emit(f"if (!is_record({arg})) {{ goto {fallthrough}; }}")
200+
updates = {}
201+
for key, pattern_value in pattern.data.items():
202+
key_idx = self.record_key(key)
203+
record_value = self._mktemp(f"record_get({arg}, {key_idx})")
204+
self._emit(f"if ({record_value} == NULL) {{ goto {fallthrough}; }}")
205+
updates.update(self.try_match(env, record_value, pattern_value, fallthrough))
206+
return updates
198207
raise NotImplementedError("try_match", pattern)
199208

200209
def compile_match_function(self, env: Env, exp: MatchFunction, name: Optional[str]) -> str:

compiler_tests.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ def test_match_int(self) -> None:
4949
def test_match_list(self) -> None:
5050
self.assertEqual(self._run("f [4, 5] . f = | [1, 2] -> 3 | [4, 5] -> 6"), "6\n")
5151

52-
@unittest.skip("TODO")
5352
def test_match_record(self) -> None:
5453
self.assertEqual(self._run("f {a = 4, b = 5} . f = | {a = 1, b = 2} -> 3 | {a = 4, b = 5} -> 6"), "6\n")
5554

0 commit comments

Comments
 (0)