Last active
September 20, 2016 07:45
-
-
Save HernanRivasAcosta/948983b72becbd0ab1ce to your computer and use it in GitHub Desktop.
Parse transform to add partial application to erlang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(pt). | |
-export([parse_transform/2]). | |
parse_transform(ASTIn, _Options) -> | |
format(ASTIn). | |
format({call, Line, {atom, _, FName}, FixedArgs} = Call) -> | |
case handle_partial_application(FName, Line) of | |
undefined -> | |
format_tuple(Call); | |
{FCall, Arity} -> | |
Args = [{var, Line, N} || | |
N <- lists:seq(1, Arity - length(FixedArgs))], | |
R = {'fun', Line, {clauses, [{clause, Line, Args, [], | |
[{call, Line, FCall, | |
format(FixedArgs) ++ Args}]}]}}, | |
format(R); | |
_A -> | |
format_tuple(Call) | |
end; | |
format(Tuple) when is_tuple(Tuple) -> | |
format_tuple(Tuple); | |
format(List) when is_list(List) -> | |
[format(X) || X <- List]; | |
format(Any) -> | |
Any. | |
format_tuple(Tuple) -> | |
list_to_tuple([format(X) || X <- tuple_to_list(Tuple)]). | |
handle_partial_application(FName, Line) -> | |
FNameStr = atom_to_list(FName), | |
case erl_scan:string(FNameStr) of | |
{ok, [{atom, _, OrigFName}, {'/', _}, {integer, _, Arity}], _} -> | |
{{atom, Line, OrigFName}, Arity}; | |
{ok, [{atom, _, Module}, {':', _}, {atom, 1, OrigFName}, | |
{'/', _}, {integer, _, Arity}], _} -> | |
{{remote, Line, {atom, Line, Module}, {atom, Line, OrigFName}}, Arity}; | |
_ -> | |
undefined | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @elbrujohalcon for pointing out I forgot about remote calls.
Now this works: