Skip to content

Instantly share code, notes, and snippets.

@ricston-git
Created February 11, 2013 11:02
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 ricston-git/4753865 to your computer and use it in GitHub Desktop.
Save ricston-git/4753865 to your computer and use it in GitHub Desktop.
-module(blog).
-compile(export_all).
transform(Forms) ->
Tree = erl_syntax:form_list(Forms),
ModifiedTree = postorder(Fun, Tree),
erl_syntax:revert_forms(ModifiedTree).
parse_transform(Forms, Options) ->
TransFun =
case lists:keysearch(trans_fun, 1, Options) of
{value, {trans_fun, Fun}} ->
Fun;
false ->
fun blog:replaceSend/1
end,
transform(Forms, Fun).
replaceSend(Node) ->
case erl_syntax:type(Node) of
infix_expr ->
OpNode = erl_syntax:infix_expr_operator(Node),
case erl_syntax:operator_name(OpNode) of
'!' ->
erl_syntax:block_expr([
erl_syntax:application(
erl_syntax:module_qualifier(erl_syntax:atom(io), erl_syntax:atom(format)),[erl_syntax:string("About to send:~n")]),
Node
]);
_ ->
Node
end;
_ ->
Node
end.
postorder(F, Tree) ->
F(case erl_syntax:subtrees(Tree) of
[] -> Tree;
List -> erl_syntax:update_tree(Tree,
[[postorder(F, Subtree)
|| Subtree <- Group]
|| Group <- List])
end).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment