Skip to content

Instantly share code, notes, and snippets.

@Baha
Created October 22, 2017 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Baha/a667f231292576e00359333f23d2941d to your computer and use it in GitHub Desktop.
Save Baha/a667f231292576e00359333f23d2941d to your computer and use it in GitHub Desktop.
Core Erlang Metaprogramming example
-module(transform).
-export([main/1]).
main(File) ->
case compile:file(File, [to_core, binary, no_copt]) of
{ok, ModuleName, CoreForms} ->
TransForms = cerl_trees:map(fun replace_concat/1, CoreForms),
CoreName = atom_to_list(ModuleName) ++ ".core",
file:write_file(CoreName,
cerl_prettypr:format(TransForms));
_ ->
io:fwrite("Error: Could not compile file.~n"),
exit(file_error)
end.
replace_concat(Node) ->
case cerl:type(Node) of
call ->
ConcCallName = cerl:concrete(cerl:call_name(Node)),
case ConcCallName of
'++' ->
[FstArg, SndArg] = cerl:call_args(Node),
FlatFstArg = cerl:cons_hd(FstArg),
cerl:c_cons(FlatFstArg,
SndArg);
_ -> Node
end;
_ -> Node
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment