前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Core Erlang:Erlang的Core中间表示

Core Erlang:Erlang的Core中间表示

作者头像
racaljk
发布2018-08-31 10:59:04
9130
发布2018-08-31 10:59:04
举报
文章被收录于专栏:racaljkracaljkracaljk

随着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/`

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-11-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云代码分析
腾讯云代码分析(内部代号CodeDog)是集众多代码分析工具的云原生、分布式、高性能的代码综合分析跟踪管理平台,其主要功能是持续跟踪分析代码,观测项目代码质量,支撑团队传承代码文化。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档