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