Core Erlang:Erlang的Core中间表示

来源:互联网 发布:阿里云终端管理工具 编辑:程序博客网 时间:2024/05/22 16:43

随着erlang的不断发展,它的语法越来越复杂,不便于诸如分析器,调试器此类程序在源码层次直接进行解析,而CORE Erlang旨在为Erlang提供一个人类可读可改的中间表示(Intermediate representation),这样就能方便面向程序源码的工具的开发。
千言万语不如代码一句。这里我们通过一个例子来直观的了解究竟什么是CORE Erlang。在helloworld.erl中输入如下代码:

-module (helloworld).-export([simple/0, complicated/2]).simple() -> atom_to_list(hello_world).complicated(Type,List) -> List = [{Type,Elem} || Elem<-List,is_atom(Elem)], case Type of  bool when bool =:= true -> R1 = bool_true;  bool -> R1 = bool_false;  integer -> R1 = 1+2*3/4 end.

Erlang/OTP R10以及之后发行的版本在编译helloworld.erl时传入to_core标志:
c(helloworld,to_core).
将会生成CORE Erlang文件helloworld.core:

module 'helloworld' ['complicated'/2,           'module_info'/0,           'module_info'/1,           'simple'/0]    attributes [%% Line 1      'file' =          %% Line 1          [{[104|[101|[108|[108|[111|[119|[111|[114|[108|[100|[46|[101|[114|[108]]]]]]]]]]]]]],1}]]'simple'/0 =    %% Line 4    fun () ->   %% Line 5   [104|[101|[108|[108|[111|[95|[119|[111|[114|[108|[100]]]]]]]]]]]

可以看到,模块头部信息和helloworld:simple()函数在CORE Erlang中还是很清楚的,但是对于helloworld:complicated(Type,List)它就完全背离了自己的初衷:

'complicated'/2 =    %% Line 7    fun (_@c1,_@c0) ->   let <_@c6> =       letrec      'lc$^0'/1 =          %% Line 8          fun (_@c4) ->         case _@c4 of           <[Elem|_@c3]>               when call 'erlang':'is_atom'                (Elem) ->               let <_@c5> =              apply 'lc$^0'/1                  (_@c3)               in  ( [{_@c1,Elem}|_@c5]                -| ['compiler_generated'] )           ( <[Elem|_@c3]> when 'true' ->            apply 'lc$^0'/1                (_@c3)             -| ['compiler_generated'] )           <[]> when 'true' ->               []           ( <_@c4> when 'true' ->            ( primop 'match_fail'                  ({'function_clause',_@c4})              -| [{'function_name',{'lc$^0',1}}] )             -| ['compiler_generated'] )         end       in  %% Line 8      apply 'lc$^0'/1          (_@c0)   in  %% Line 8       case _@c6 of         <_@c16>        when call 'erlang':'=:='         (_@c6,          _@c0) ->        %% Line 9        case _@c1 of          %% Line 11          <'bool'> when 'true' ->         'bool_false'          %% Line 12          <'integer'> when 'true' ->         2.50000000000000000000e+00          ( <_@c13> when 'true' ->           primop 'match_fail'               ({'case_clause',_@c13})            -| ['compiler_generated'] )        end         ( <_@c7> when 'true' ->          primop 'match_fail'         ({'badmatch',_@c7})      -| ['compiler_generated'] )       end'module_info'/0 =    fun () ->   call 'erlang':'get_module_info'       ('helloworld')'module_info'/1 =    fun (_@c0) ->   call 'erlang':'get_module_info'       ('helloworld', _@c0)end

不过话又说回来,CORE Erlang的受众终究是代码分析器,调试器之类的工具,人类可读可改更像是一个崇高追求,如果我们观察会发现CORE Erlang的中间表示的确降低了语法的解析难度,它将源码的各种语法用let in,call ,case of,fun等几个简单的结构来表示,从这点来看CORE Erlang算是到达了它的初衷。

[+] core erlang project index : http://www.it.uu.se/research/group/hipe/cerl/`