Erlang Abstract Form--Module声明和Form

来源:互联网 发布:淘宝视频直播怎么开通 编辑:程序博客网 时间:2024/06/08 06:01

前面已经介绍了,ErlyWeb中的Smerl已经具备了和Ruby样的元组编程,本文将会介绍实现元组编程的基本原理,在Smerl中,主要是用到了Erlang的Abstract Form来实现的,我们将具体考察Erlang Abstract Form的组成。方法很简单,参考Eralng文档的Abstract Form一节,用实际的例子加以验证。

Abstract Form

Abstract Form文档中,用函数Rep表示从Erlang源代码C到abstract form形式R的映射。简单地说,如果源代码C解析成为Abstract Form R,那么写成R = Rep(C)。另外文档中LINE表示源代码的行号。下面是module声明的描述:
    A module declaration consists of a sequence of forms that are either function declarations or attributes.
        * If D is a module declaration consisting of the forms F_1, ..., F_k, then Rep(D) = [Rep(F_1), ..., Rep(F_k)].
        * If F is an attribute -module(Mod), then Rep(F) = {attribute,LINE,module,Mod}.
        * If F is an attribute -export([Fun_1/A_1, ..., Fun_k/A_k]), then Rep(F) = {attribute,LINE,export,[{Fun_1,A_1}, ..., {Fun_k,A_k}]}.
        * If F is an attribute -import(Mod,[Fun_1/A_1, ..., Fun_k/A_k]), then Rep(F) = {attribute,LINE,import,{Mod,[{Fun_1,A_1}, ..., {Fun_k,A_k}]}}.
        * If F is an attribute -compile(Options), then Rep(F) = {attribute,LINE,compile,Options}.
        * If F is an attribute -file(File,Line), then Rep(F) = {attribute,LINE,file,{File,Line}}.
        * If F is a record declaration -record(Name,{V_1, ..., V_k}), then Rep(F) = {attribute,LINE,record,{Name,[Rep(V_1), ..., Rep(V_k)]}}. For Rep(V), see below.
        * If F is a wild attribute -A(T), then Rep(F) = {attribute,LINE,A,T}.
        * If F is a function declaration Name Fc_1 ; ... ; Name Fc_k, where each Fc_i is a function clause with a pattern sequence of the same length Arity, then Rep(F) = {function,LINE,Name,Arity,[Rep(Fc_1), ...,Rep(Fc_k)]}.
模块声明由一系列Form组成,这些Form要么是函数声明,要么是属性(attribute)。

Simplest
考察我们最简单的模块 simplest。

-module(simplest).

我们对它进行编译,然后获取它的abstract_code:

Eshell V5.5  (abort with ^G)
1> c(simplest,[debug_info]).
{ok,simplest}
2> beam_lib:chunks(simplest, [abstract_code]).
{ok,{simplest,[{abstract_code,{raw_abstract_v1,
                  [{attribute,1,file,{"./simplest.erl",1}},
                   {attribute,1,module,simplest},
                   {eof,1}]}}]}}

beam_lib:chunks返回的abstract_code定义如下:

    {ChunkName, DataT} =
    {abstract_code, AbstractCode}

    AbstractCode = {AbstVersion, Forms} | no_abstract_code
    AbstVersion = atom()

如果无法在beam文件中找到abstract form,那么将返回no_abstract_code。如果找到的话,则是一个tuple, tuple的第一项是版本,即我们上面例子中的raw_abstract_v1,tuple的第2项就是真正的form。因此,最简单的simplest beam文件中包含的Form如下:

  [{attribute,1,file,{"./simplest.erl",1}},
 {attribute,1,module,simplest},
 {eof,1}]

Abstract Form关于module声明Form的第一条说:

    If D is a module declaration consisting of the forms F_1, ..., F_k, then Rep(D) = [Rep(F_1), ..., Rep(F_k)].

这可以解释Form为什么是一个列表。

    If F is an attribute -module(Mod), then Rep(F) = {attribute,LINE,module,Mod}.

因此,例子中Form的第2行是:

 {attribute,1,module,simplest}

还有:

    If F is an attribute -file(File,Line), then Rep(F) = {attribute,LINE,file,{File,Line}}.

这也说明了为什么会出现:

 {attribute,1,file,{"./simplest.erl",1}}

尽管我们没有在源代码中编写-file属性,但是编译器还是在abstract code中加入了这个属性。

最后,由于文件在第一行结束,因此还包含

 {eof,1}

这是在Abstract Form文档的其中一节提到的:

    4.1.2 Representation of parse errors and end of file
    In addition to the representations of forms, the list that represents a module declaration (as returned by functions in erl_parse and epp) may contain tuples {error,E}, denoting syntactically incorrect forms, and {eof,LINE}, denoting an end of stream encountered before a complete form had been parsed.

加入一个方法
接下去,我们在simplest.erl加入一个新的函数test,并export:

-module(simplest). %1
-export([test/0]). %2
test() ->  %3
ok.   %4

重新编译simplest,并获取abstract code 如下:
5> c(simplest,[debug_info]).      
{ok,simplest}
6> beam_lib:chunks(simplest, [abstract_code]).
{ok,{simplest,[{abstract_code,{raw_abstract_v1,
                       [{attribute,1,file,{"./simplest.erl",1}},
                        {attribute,1,module,simplest},
                        {attribute,2,export,[{test,0}]},
                        {function,
                            3,
                            test,
                            0,
                            [{clause,3,[],[],[{atom,4|...}]}]},
                        {eof,5}]}}]}}


首先,我们看到新增加了export属性,出现在代码的第2行,其中包括0个参数的test这个tuple。最主要的变化是一个新的function Form:

    If F is a function declaration Name Fc_1 ; ... ; Name Fc_k, where each Fc_i is a function clause with a pattern sequence of the same length Arity, then Rep(F) = {function,LINE,Name,Arity,[Rep(Fc_1), ...,Rep(Fc_k)]}.

function的Form共有5项,第一项是function这个atom,第二项是行号,第3项是函数的名字,第4项是函数参数的个数。最后一项是一个列表,包含每个子句的Rep。

我们可以深入到function的每一个子句中去,但是探索Erlang Abstract Form的目的是为了能够理解metaprogramming的原理。而在实际编程时,很少有人会用Form来动态生成一个新的函数。通常使用的方法是提供一个函数的源代码,或者直接使用函数参数,关于直接对Form更详细的操纵,我们放到后面再说

原创粉丝点击