Skip to content

Commit

Permalink
Slightly refactor map refilling
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorng committed Nov 4, 2024
1 parent 9a0a5e2 commit 4cf83d5
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions lib/compiler/src/v3_core.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1706,8 +1706,7 @@ zip_tq(Line, E, #izip{anno=#a{anno=GA}=GAnno,
%% There are no map generators.
[nomatch];
false ->
RefillBody = [C || C <- RefillAs],
make_refill(lists:enumerate(RefillPats0), {TailVars, RefillBody, LA, [], Sc}, [])
make_refill(RefillPats0, 0, RefillAs, {TailVars, LA, [], Sc})
end,
Cs0 = [AccClause, AccClauseNoGuards, TailClause]++RefillClause,
Cs = [C || C <- Cs0, C =/= nomatch],
Expand Down Expand Up @@ -1871,8 +1870,7 @@ bzip_tq1(Line, E, #izip{anno=#a{anno=_GA}=GAnno,
%% There are no map generators.
[nomatch];
false ->
RefillBody = [C || C <- RefillAs],
make_refill(lists:enumerate(RefillPats0), {TailVars, RefillBody, LA, [Mc], Sc}, [])
make_refill(RefillPats0, 0, RefillAs, {TailVars, LA, [Mc], Sc})
end,
Cs0 = [AccClause, AccClauseNoGuards, TailClause]++RefillClause,
Cs = [C || C <- Cs0, C =/= nomatch],
Expand All @@ -1896,18 +1894,15 @@ mc_tq(Line, {map_field_assoc,Lf,K,V}, Qs, Mc, St0) ->
args=[LcVar]},
{Call,Pre,St2}.


make_refill([{Index, RefillPat}|RefillPats], {TailVars, RefillBody, LA, Mc, Sc}=Args, Acc) ->
case RefillPat of
nomatch -> make_refill(RefillPats, Args, Acc);
_ ->
{H, [_|T]} = lists:split(Index-1, TailVars),
RefillPat1 = H++[RefillPat]++T,
make_refill(RefillPats, Args, [make_clause(LA, RefillPat1++Mc, [], [lists:nth(Index,RefillBody)]++[Sc])|Acc])
end;
make_refill([],_, Acc) ->
reverse(Acc).

make_refill([nomatch|RefillPats], Index, [_|Bodies], Args) ->
make_refill(RefillPats, Index + 1, Bodies, Args);
make_refill([RefillPat0|RefillPats], Index, [RefillBody|Bodies], {TailVars, LA, Mc, Sc}=Args) ->
{H, [_|T]} = lists:split(Index, TailVars),
RefillPat1 = H ++ [RefillPat0|T] ++ Mc,
RefillClause = make_clause(LA, RefillPat1, [], [RefillBody,Sc]),
[RefillClause|make_refill(RefillPats, Index + 1, Bodies, Args)];
make_refill([], _Index, [], _Args) ->
[].

make_clause(Anno, [Pat|PatExtra], Guard, Body) ->
make_clause(Anno, Pat, PatExtra, Guard, Body).
Expand Down

0 comments on commit 4cf83d5

Please sign in to comment.