Skip to content

Commit 9d52012

Browse files
committed
Mark y as freevar in x@y
Revert f48f6e0. For records, y is not free in x@y; it is a field name. For lists, y *is* free in x@y; it is an index expression (could be a var). For now, we'll assume it might be an expression and mark it as a (possibly extra) freevar.
1 parent 504ccc7 commit 9d52012

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

scrapscript.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,12 @@ def free_in(exp: Object) -> Set[str]:
11881188
if isinstance(exp, Apply):
11891189
return free_in(exp.func) | free_in(exp.arg)
11901190
if isinstance(exp, Access):
1191-
return free_in(exp.obj)
1191+
# For records, y is not free in x@y; it is a field name.
1192+
# For lists, y *is* free in x@y; it is an index expression (could be a
1193+
# var).
1194+
# For now, we'll assume it might be an expression and mark it as a
1195+
# (possibly extra) freevar.
1196+
return free_in(exp.obj) | free_in(exp.at)
11921197
if isinstance(exp, Where):
11931198
assert isinstance(exp.binding, Assign)
11941199
return (free_in(exp.body) - {exp.binding.name.name}) | free_in(exp.binding)
@@ -3138,6 +3143,12 @@ def test_access_list_var(self) -> None:
31383143
def test_access_list_expr(self) -> None:
31393144
self.assertEqual(self._run("xs@(1+1) . xs = [1, 2, 3]"), Int(3))
31403145

3146+
def test_access_list_closure_var(self) -> None:
3147+
self.assertEqual(
3148+
self._run("list_at 1 [1,2,3] . list_at = idx -> ls -> ls@idx"),
3149+
Int(2),
3150+
)
3151+
31413152
def test_functions_eval_arguments(self) -> None:
31423153
self.assertEqual(self._run("(x -> x) c . c = 1"), Int(1))
31433154

@@ -3582,7 +3593,7 @@ def test_apply(self) -> None:
35823593
self.assertEqual(free_in(Apply(Var("x"), Var("y"))), {"x", "y"})
35833594

35843595
def test_access(self) -> None:
3585-
self.assertEqual(free_in(Access(Var("x"), Var("y"))), {"x"})
3596+
self.assertEqual(free_in(Access(Var("x"), Var("y"))), {"x", "y"})
35863597

35873598
def test_where(self) -> None:
35883599
exp = parse(tokenize("x . x = 1"))

0 commit comments

Comments
 (0)