Explore Erlang Abstract form--module declarations and Form

Source: Internet
Author: User
Tags abstract

Reprint: http://agileprogrammer.blogspot.com/2006/08/erlang-abstract-form-moduleform.html


We will examine the composition of the Erlang Abstract form in detail. The method is simple, refer to the Abstract form section of the ERALNG document and validate it with practical examples.
Abstract Form
In the Abstract form document, a function rep is used to represent a mapping from the Erlang source code c to the abstract form R. Simply put, if source code C resolves to be an abstract Form R, then write r = Rep (c).
In addition, line in the document indicates the row number of the source code.
The following is a description of the module declaration:

A module declaration consists of a sequence of forms that is 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 a 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 a attribute-compile (Options), then Rep (F) = {attribute,line,compile,options}. If F is a 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) and then Rep (F) = {attribute,line,a,t}.
If F is a function declaration Name fc_1; ... ; Name Fc_k, where each fc_i are 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)]}. A module declaration consists of a series of form, which is either a function declaration or a property (attribute).

simplestExplore our simplest module simplest.

-module (simplest).

We compile it, and then get its 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}]}}}}


The Abstract_code definition returned by Beam_lib:chunks is as follows:
{chunkname, Datat} = {abstract_code, abstractcode} abstractcode = {abstversion, Forms} | No_abstract_code abstversion = Atom () If the abstract form cannot be found in the beam file, the No_abstract_code is returned. If found, it is a tuple, the first item of the tuple is the version, that is, the 2nd of the raw_abstract_v1,tuple in our example is the real form. Therefore, the simplest simplest beam file contains the following form:
  [{attribute,1,file,{"./simplest.erl", 1}},
 {Attribute,1,module,simplest},
 {eof,1}]
Abstract form the first article on module declaration form says:
If D is a module declaration consisting of the forms f_1, ..., F_k, then Rep (D) = [Rep (f_1), ..., Rep (F_k)]. This can explain why a form is a list.
If F is a attribute-module (Mod), then Rep (F) = {Attribute,line,module,mod}.
So the 2nd line of the form in the example is:
{Attribute,1,module,simplest}
And also:
If F is a attribute-file (file,line), then Rep (F) = {Attribute,line,file,{file,line}}. This also explains why it appears:
{attribute,1,file,{"./simplest.erl", 1}}
Although we did not write the-file property in the source code, the compiler added this attribute to the abstract code.

Finally, because the file ends at the first line, it also contains
{eof,1}


This is mentioned in one of the sections of the abstract form document:
4.1.2 Representation of parse errors and end of fileIn addition to the representations of forms, the list is represents a module declaration (as returned by functions in ER L_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.
Add a method
Next, we add a new function test in Simplest.erl, and export:
-module (simplest). %1
-export ([test/0]).%2
test ()-  %3
OK.   %4


Recompile the simplest and get the abstract code as follows:
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}]}}}
                        }


First, we see that the newly added export property appears in line 2nd of the code, which includes the 0 parameter of the test tuple. The main change is a new function Form:

If F is a function declaration Name fc_1; ... ; Name Fc_k, where each fc_i are 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)]}. There are 5 functions of the form, the first is the function of the atom, the second is the line number, the 3rd is the name of the function, and the 4th item is the number of function arguments. The last item is a list that contains the rep for each clause.

We can go deep into every sentence of function, but the purpose of exploring Erlang abstract form is to be able to understand the principle of metaprogramming. In the actual programming, few people will use form to dynamically generate a new function. The usual method is to provide the source code of a function, or directly use the function parameters, about the direct to the form of more detailed manipulation, we put in the back.

I'll describe in the next article how to parse the source code into a form, compile the form, and dynamically load the successful binary data to achieve the metaprogramming capability at run time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.