1. 程式人生 > >[Erlang 0111] Erlang Abstract Format , Part 2

[Erlang 0111] Erlang Abstract Format , Part 2

Eshell V5.10.2  (abort with ^G)
1> M  = erl_syntax:attribute(erl_syntax:atom(module),[erl_syntax:atom(t)]).
{tree,attribute,
      {attr,0,[],none},
      {attribute,{tree,atom,{attr,0,[],none},module},
                 [{tree,atom,{attr,0,[],none},t}]}}
2> MF =  erl_syntax:revert(M).
{attribute,0,module,t}
3> E = erl_syntax:attribute(erl_syntax:atom(export),[erl_syntax:list([erl_syntax:arity_qualifier(erl_syntax:atom(say),erl_syntax:integer(0))])]).
{tree,attribute,
    {attr,0,[],none},
    {attribute,
        {tree,atom,{attr,0,[],none},export},
        [{tree,list,
             {attr,0,[],none},
             {list,
                 [{tree,arity_qualifier,
                      {attr,0,[],none},
                      {arity_qualifier,
                          {tree,atom,{attr,0,[],none},say},
                          {tree,integer,{attr,0,[],none},0}}}],
                 none}}]}}
4> ExportForm = erl_syntax:revert(E).
{attribute,0,export,[{say,0}]}
5> 
5> 
5> Clause= erl_syntax:clause([],[],[erl_syntax:atom(hello_world)]).
{tree,clause,
      {attr,0,[],none},
      {clause,[],none,[{tree,atom,{attr,0,[],none},hello_world}]}}
6> 
6> Function =  erl_syntax:function(erl_syntax:atom(say),[Clause]).
{tree,function,
      {attr,0,[],none},
      {func,{tree,atom,{attr,0,[],none},say},
            [{tree,clause,
                   {attr,0,[],none},
                   {clause,[],none,
                           [{tree,atom,{attr,0,[],none},hello_world}]}}]}}
7> FunctionForm = erl_syntax:revert(Function).
{function,0,say,0,[{clause,0,[],[],[{atom,0,hello_world}]}]}
8>  {ok, Mod, Bin1} = compile:forms([MF,ExportForm, FunctionForm]).
{ok,t,
    <<70,79,82,49,0,0,1,208,66,69,65,77,65,116,111,109,0,0,0,
      57,0,0,0,6,1,116,...>>}
9> t:say().
** exception error: undefined function t:say/0
10> code:load_binary(Mod, [], Bin1).
{module,t}
11> t:say().
hello_world
12>