Skip to content

Instantly share code, notes, and snippets.

@hamidreza-s
Last active April 1, 2021 07:26
Show Gist options
  • Save hamidreza-s/10704087 to your computer and use it in GitHub Desktop.
Save hamidreza-s/10704087 to your computer and use it in GitHub Desktop.
How to use Parse Transform feature in Erlang for meta-programming.
-module(main).
-export([hi/0, bye/0, hey/1]).
-compile({parse_transform, main_pt}).
hi() -> io:format("Hi there!~n").
bye() -> io:format("Bye there!~n").
hey(foo) -> io:format("Hey foo!~n");
hey(bar) -> io:format("Hey bar!~n").
-module(main_pt).
-export([parse_transform/2]).
parse_transform(Ast, _Opt) ->
io:format("AST:~p~n",[Ast]),
[parse(X) || X <- Ast],
Ast.
parse({function, _Line, _FunName, _ArgNum, Clause}) ->
[parse(X) || X <- Clause];
parse({clause, _Line, _Vars, _Gaurd, ExprCalls}) ->
[erl_eval:expr(X, erl_eval:new_bindings()) || X <- ExprCalls];
parse(_) -> ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment