Skip to content

Commit

Permalink
Normalize captures (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
NickNeck authored Jul 8, 2024
1 parent f1499b5 commit 4924fa3
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
name: Test on Ubuntu (Elixir ${{ matrix.elixir }}, OTP ${{ matrix.otp }})
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
elixir:
- '1.13.4'
Expand Down
7 changes: 7 additions & 0 deletions lib/beam_file/normalizer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ defmodule BeamFile.Normalizer do
end
end

def normalize({:capture, meta, nil} = ast, :code) do
case Keyword.get(meta, :counter) do
{Capture, count} -> {:"capture#{count}", meta, nil}
_else -> ast
end
end

def normalize({:super, meta, args}, target) do
case Keyword.has_key?(meta, :super) do
true ->
Expand Down
5 changes: 5 additions & 0 deletions test/beam_file_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,11 @@ defmodule BeamFileTest do
assert code <> "\n" == TestSupport.fixture("comps.exs")
end

test "returns elixir code for the Capture module" do
assert {:ok, code} = BeamFile.elixir_code(Capture)
assert code <> "\n" == TestSupport.fixture("capture.exs")
end

test "returns an error for invalid binary" do
assert BeamFile.elixir_code(<<0, 0, 7>>) == {:error, {:not_a_beam_file, <<0, 0, 7>>}}
end
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/1.13.4/capture.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Elixir.Capture do
def fun do
fn x1, x2 -> :erlang.+(:erlang.*(x1, x2), x1) end
end

def double do
fn x1 -> :erlang.+(x1, x1) end
end
end
9 changes: 9 additions & 0 deletions test/fixtures/1.14.5/capture.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Elixir.Capture do
def fun do
fn x1, x2 -> :erlang.+(:erlang.*(x1, x2), x1) end
end

def double do
fn x1 -> :erlang.+(x1, x1) end
end
end
9 changes: 9 additions & 0 deletions test/fixtures/1.15.8/capture.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Elixir.Capture do
def fun do
fn x1, x2 -> :erlang.+(:erlang.*(x1, x2), x1) end
end

def double do
fn x1 -> :erlang.+(x1, x1) end
end
end
9 changes: 9 additions & 0 deletions test/fixtures/1.16.3/capture.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Elixir.Capture do
def fun do
fn x1, x2 -> :erlang.+(:erlang.*(x1, x2), x1) end
end

def double do
fn x1 -> :erlang.+(x1, x1) end
end
end
9 changes: 9 additions & 0 deletions test/fixtures/1.17.2/capture.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Elixir.Capture do
def fun do
fn capture3, capture4 -> :erlang.+(:erlang.*(capture3, capture4), capture3) end
end

def double do
fn capture5 -> :erlang.+(capture5, capture5) end
end
end
9 changes: 9 additions & 0 deletions test/fixtures/capture.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Capture do
def fun do
&(&1 * &2 + &1)
end

def double do
&(&1 + &1)
end
end

0 comments on commit 4924fa3

Please sign in to comment.