johnbender / unraverl

An erlang module built to provide language features and other operations through manipulation of the abstract form.

johnbender (author)
Sat Aug 08 18:38:57 -0700 2009
commit  cc8310fab4538cefd035502f95327bf894130292
tree    6c650a303b615b99e0220b92d9bc8459be6dbc10
parent  f66c0ed3801b3aa8aa34eaaefee06f06086f0744
unraverl / unraverl.erl
100644 36 lines (26 sloc) 1.246 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
-module(unraverl).
-export([parse_transform/2]).
 
-define(MODS, [filters, partial_application]).
-define(ATTR_ALTERATIONS, [before_exec, after_exec]).
-define(LANG_ALTERATIONS, [partial_application]).
 
parse_transform(Form, _options) ->
    AltAttr = attribute_alterations(Form, ?ATTR_ALTERATIONS),
    AltLang = lang_alterations(AltAttr, ?LANG_ALTERATIONS),
    io:format("~p", [AltLang]),
    AltLang.
 
attribute_alterations(Form, []) -> Form;
attribute_alterations(Form, [Alteration|T]) ->
    Altered = per_attribute_alter(Form, util:find_attribute(Form, Alteration)),
    attribute_alterations(Altered, T).
 
per_attribute_alter(Form, []) -> Form;
per_attribute_alter(Form, [{attribute,_,Alteration,{ToBeAltered, With}}|T]) ->
    AlteredForm = module_call(Alteration, [Form, ToBeAltered, With]),
    per_attribute_alter(AlteredForm, T).
 
lang_alterations(Form, []) -> Form;
lang_alterations(Form, [Alteration|T]) ->
    lang_alterations(module_call(Alteration, [Form]), T).
 
module_call(Alteration, Args) when is_list(Args) ->
    apply(get_module_for_alteration(Alteration), Alteration, Args).
 
get_module_for_alteration(Function) ->
    [Module] = [X || X<-?MODS, lists:keymember(Function, 1, X:module_info(exports)) ],
    Module.