Last active
April 1, 2021 07:26
-
-
Save hamidreza-s/10704087 to your computer and use it in GitHub Desktop.
How to use Parse Transform feature in Erlang for meta-programming.
This file contains hidden or 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(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"). |
This file contains hidden or 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(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