Generate more readable code for run_fold hooks

This commit is contained in:
Evgeniy Khramtsov 2017-10-02 10:47:03 +03:00
parent 77a1f728cf
commit 6658806820
1 changed files with 11 additions and 14 deletions

View File

@ -273,8 +273,6 @@ emit_module(RunDeps, RunFoldDeps, Specs, Dir, Module) ->
emit_export(Fd, RunFoldDeps, "run_fold hooks"),
emit_run_hooks(Fd, RunDeps, Specs),
emit_run_fold_hooks(Fd, RunFoldDeps, Specs),
write(Fd, "bypass_stop({stop, Acc}) -> Acc;~n"
"bypass_stop(Acc) -> Acc.~n", []),
file:close(Fd),
log("Module written to file ~s~n", [File])
catch _:{badmatch, {error, Reason}} ->
@ -311,20 +309,19 @@ emit_run_fold_hooks(Fd, Deps, Specs) ->
emit_specs(Fd, Funs, Specs),
write(Fd, "%% called at ~s:~p~n", [File, LineNo]),
Args = [[N] || N <- lists:sublist(lists:seq($A, $Z), Arity - 1)],
write(Fd, "~s(~s) ->", [Hook, string:join(["Acc"|Args], ", ")]),
FunsCascade = make_funs_cascade(
lists:reverse(lists:keysort(2, Funs)),
1, Args),
write(Fd, "~s.~n~n", [FunsCascade])
write(Fd, "~s(~s) ->~n ", [Hook, string:join(["Acc0"|Args], ", ")]),
{Calls, _} = lists:mapfoldl(
fun({{Mod, Fun, _}, _Seq, _}, N) ->
Args1 = ["Acc" ++ integer_to_list(N)|Args],
{io_lib:format("Acc~p = ~s:~s(~s)",
[N+1, Mod, Fun,
string:join(Args1, ", ")]),
N + 1}
end, 0, lists:keysort(2, Funs)),
write(Fd, "~s,~n", [string:join(Calls, ",\n ")]),
write(Fd, " Acc~p.~n~n", [length(Funs)])
end, DepsList).
make_funs_cascade([{{Mod, Fun, _}, _Seq, _}|Funs], N, Args) ->
io_lib:format("~n~sbypass_stop(~s:~s(~s))",
[lists:duplicate(N, " "),
Mod, Fun, string:join([make_funs_cascade(Funs, N+1, Args)|Args], ", ")]);
make_funs_cascade([], _N, _Args) ->
"Acc".
emit_export(Fd, Deps, Comment) ->
DepsList = lists:sort(dict:to_list(Deps)),
Exports = lists:map(