Skip to content

Commit

Permalink
Excise last use of brittle extract_vars function
Browse files Browse the repository at this point in the history
  • Loading branch information
ashton314 committed Jan 22, 2025
1 parent 87e2a2e commit f208581
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 55 deletions.
26 changes: 2 additions & 24 deletions lib/chorex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -325,15 +325,7 @@ defmodule Chorex do
| # params_ might have "_" in it when parameter not for
# this label; do not add _ to ctx.vars
vars:
(
params_
|> FreeVarAnalysis.free_vars()
|> Enum.map(&elem(&1, 0))
|> Enum.filter(fn
:_ -> false
_ -> true
end)
) ++ ctx.vars
FreeVarAnalysis.extract_new_pattern_var_names(params_) ++ ctx.vars
})

# no return value from a function *definition*
Expand Down Expand Up @@ -673,8 +665,7 @@ defmodule Chorex do
| vars:
if(match?(^zero, match_expr_),
do: ctx.vars,
# FIXME: use FreeVarAnalysis
else: extract_vars(match_expr_) ++ ctx.vars
else: FreeVarAnalysis.extract_new_pattern_var_names(match_expr_) ++ ctx.vars
)
})

Expand Down Expand Up @@ -968,19 +959,6 @@ defmodule Chorex do

def metadata({_, m, _}) when is_list(m), do: m

@doc """
Walk an expression and pull out all the names of all the variables embedded in it.
"""
def extract_vars(expr) do
Macro.postwalk(expr, [], &grab_var/2)
|> elem(1)
end

def grab_var({var_name, _meta, ctx} = e, acc) when is_atom(var_name) and is_atom(ctx),
do: {e, [var_name | acc]}

def grab_var(node, acc), do: {node, acc}

@doc """
Walks a local expression to pull out/convert function calls.
Expand Down
5 changes: 5 additions & 0 deletions lib/free_var_analysis.ex
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ defmodule FreeVarAnalysis do
{free, MapSet.to_list(bound_set)}
end

def extract_new_pattern_var_names(expr, bound \\ MapSet.new()) do
{_, newly_bound} = expr |> extract_pattern_vars(bound)
newly_bound |> Enum.map(&var_name/1)
end

defp strip_meta({a, _, b}), do: {a, [], b}

defp bound?(var, binds) when is_var(var), do: MapSet.member?(binds, strip_meta(var))
Expand Down
31 changes: 0 additions & 31 deletions test/chorex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -515,35 +515,4 @@ defmodule ChorexTest do
Chorex.project_local_expr(stx, __ENV__, Alice, Chorex.empty_ctx(__ENV__))
end
end

describe "extract_vars" do
test "basic variables" do
vars =
quote do
foo
end
|> extract_vars()

assert [:foo] = vars

vars =
quote do
bar
baz
end
|> extract_vars()

assert [:baz, :bar] = vars
end

test "tuples" do
vars =
quote do
{thing1, thing2}
end
|> extract_vars()

assert [:thing2, :thing1] = vars
end
end
end

0 comments on commit f208581

Please sign in to comment.