[erlang-questions] clarify: how to traverse AST with erl_syntax functions ?

Roberto Saccon rsaccon@REDACTED
Mon Dec 17 11:21:38 CET 2007


is there a good example anywhere ?

I think I am doing something wrong, e.g.: I want to traverse the AST
of the string module and access some of the functions.

1> {_, Beam, _} = code:get_object_code(list_to_atom("string")).
{string,<<70,79,82,49,0,0,45,40,66,69,65,77,65,116,111,109,0,0,2,15,0,0,0,69,6,
          115,116,...>>,
        "/Users/rsaccon/R11B/erlang/lib/stdlib-1.14.5/ebin/string.beam"}
2> {ok,{_,[{abstract_code,{_,Ac}}]}} = beam_lib:chunks(Beam,[abstract_code]).
...
3>T= erl_syntax:abstract(Ac).
...
4> erl_syntax:type(T).
list
5> Ts=erl_syntax:list_elements(T).


now Ii grab one element form the middle of the  list, which
encapsulates a function of the string module:


11> T_in_the_middle.
{tree,tuple,
      {attr,0,[],none},
      [{tree,atom,{attr,0,[],none},function},
       {tree,integer,{attr,0,[],none},38},
       {tree,atom,{attr,0,[],none},len},
       {tree,integer,{attr,0,[],none},1},
       {tree,list,
             {attr,0,[],none},
             {list,[{tree,tuple,
                          {attr,0,[],none},
                          [{tree,atom,{attr,0,[],none},clause},
                           {tree,integer,{attr,0,[],none},38},
                           {tree,list,{attr,0,[],...},{list,[...],...}},
                           {tree,nil,{attr,0,...},[]},
                           {tree,list,{attr,...},{...}}]}],
                   none}}]}
12> erl_syntax:type(T_in_the_middle).
tuple


And now how do I get to the function inside ? Is there a "direct way"
to get there or do I need to unpack the tuple ?

regards
-- 
Roberto Saccon
http://rsaccon.com



More information about the erlang-questions mailing list