Created
October 22, 2017 16:56
-
-
Save Baha/a667f231292576e00359333f23d2941d to your computer and use it in GitHub Desktop.
Core Erlang Metaprogramming example
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(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