Skip to content

Commit 410cecc

Browse files
committed
Add free_in(MatchCase(Record))
1 parent 6c6f8ac commit 410cecc

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

scrapscript.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,12 +1164,7 @@ def free_in(exp: Object) -> Set[str]:
11641164
return set()
11651165
return set.union(*(free_in(case) for case in exp.cases))
11661166
if isinstance(exp, MatchCase):
1167-
if isinstance(exp.pattern, Var):
1168-
return free_in(exp.body) - {exp.pattern.name}
1169-
if isinstance(exp.pattern, List):
1170-
return free_in(exp.body) - free_in(exp.pattern)
1171-
# TODO(max): Record destructuring
1172-
return free_in(exp.body)
1167+
return free_in(exp.body) - free_in(exp.pattern)
11731168
if isinstance(exp, Apply):
11741169
return free_in(exp.func) | free_in(exp.arg)
11751170
if isinstance(exp, Where):
@@ -3573,6 +3568,25 @@ def test_match_case_list_spread(self) -> None:
35733568
exp = MatchCase(List([Spread()]), Binop(BinopKind.ADD, Var("xs"), Var("y")))
35743569
self.assertEqual(free_in(exp), {"xs", "y"})
35753570

3571+
def test_match_case_list_spread_name(self) -> None:
3572+
exp = MatchCase(List([Spread("xs")]), Binop(BinopKind.ADD, Var("xs"), Var("y")))
3573+
self.assertEqual(free_in(exp), {"y"})
3574+
3575+
def test_match_case_record(self) -> None:
3576+
exp = MatchCase(
3577+
Record({"x": Int(1), "y": Var("y"), "a": Var("z")}),
3578+
Binop(BinopKind.ADD, Binop(BinopKind.ADD, Var("x"), Var("y")), Var("z")),
3579+
)
3580+
self.assertEqual(free_in(exp), {"x"})
3581+
3582+
def test_match_case_record_spread(self) -> None:
3583+
exp = MatchCase(Record({"...": Spread()}), Binop(BinopKind.ADD, Var("x"), Var("y")))
3584+
self.assertEqual(free_in(exp), {"x", "y"})
3585+
3586+
def test_match_case_record_spread_name(self) -> None:
3587+
exp = MatchCase(Record({"...": Spread("x")}), Binop(BinopKind.ADD, Var("x"), Var("y")))
3588+
self.assertEqual(free_in(exp), {"y"})
3589+
35763590
def test_apply(self) -> None:
35773591
self.assertEqual(free_in(Apply(Var("x"), Var("y"))), {"x", "y"})
35783592

0 commit comments

Comments
 (0)