diff --git a/examples/ex_codegen.erl b/examples/ex_codegen.erl index 0f81772..da97b69 100644 --- a/examples/ex_codegen.erl +++ b/examples/ex_codegen.erl @@ -2,7 +2,7 @@ -compile({parse_transform, parse_trans_codegen}). --export([f/1, g/2]). +-export([f/1, g/2, gen/2]). f(Name) -> @@ -23,3 +23,6 @@ g(Name, V) -> member({'$var',V}, L) end). + +gen(Name, X) -> + codegen:gen_function(Name, fun(L) -> lists:member({'$var',X}, L) end). diff --git a/src/parse_trans_codegen.erl b/src/parse_trans_codegen.erl index 339ea8a..1857e36 100644 --- a/src/parse_trans_codegen.erl +++ b/src/parse_trans_codegen.erl @@ -34,6 +34,31 @@ -export([parse_transform/2]). +%% @spec (Forms, Options) -> NewForms +%% +%% @doc +%% Searches for calls to `codegen:gen_function(Name, Fun)' and +%% substitutes the abstract code for a function with name `Name' +%% and the same behaviour as `Fun'. +%% +%% It is possible to do some limited expansion (importing a value +%% bound at compile-time), using the construct {'$var', V}, where +%% `V' is a bound variable in the scope of the call to `gen_function/2'. +%% +%% Example: +%%
+%% gen(Name, X) ->
+%%    codegen:gen_function(Name, fun(L) -> lists:member({'$var',X}, L) end).
+%% 
+%% +%% After transformation, calling `gen(contains_17, 17)' will yield the +%% abstract form corresponding to: +%%
+%% contains_17(L) ->
+%%    lists:member(17, L).
+%% 
+%% @end +%% parse_transform(Forms, Options) -> {NewForms, _} = parse_trans:depth_first(fun xform_fun/4, [], Forms, Options),