Erlang abstract form-module declaration and form

Source: Internet
Author: User

As mentioned above, the smerl in erlyweb already has a ruby-like tuples for programming. This article will introduce the basic principle of implementing tuples for programming. In smerl, the abstract form of Erlang is mainly used for implementation. We will examine the composition of Erlang abstract form in detail. The method is very simple. Refer to the abstract form section in the eralng document and use actual examples for verification.

Abstract form

In abstract form, the function rep is used to indicate the ing from Erlang source code C to abstract form Form R. Simply put, if source code C is parsed as abstract form r, it is written as R = rep (c ). In addition, line indicates the source code line number. The following is the description of the module declaration:
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)]}.
A module Declaration consists of a series of forms, which are either function declaration or attribute ).

Simplest
Test our simplest module simplest.

-Module (simplest ).

We compile it and then obtain 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}]}

Beam_lib: The abstract_code returned by chunks is defined 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, no_effecact_code is returned. If it is found, it is a tuple. the first item of the tuple is the version, that is, raw_javasact_v1 in the above example, and the fifth item of the tuple is the real form. Therefore, the simplest form contained in the simplest beam file is as follows:

[{Attribute, 1, file, {"./simplest. erl", 1 }},
{Attribute, 1, module, simplest },
{EOF, 1}]

Abstract form: Article 1 of the module Declaration Form:

If D is a module Declaration consisting of the forms F_1,..., f_k, then rep (d) = [rep (F_1),..., Rep (f_k)].

This explains why form is a list.

If F is an attribute-module (MOD), then rep (f) = {attribute, line, module, MOD }.

In this example, the first line of form is:

{Attribute, 1, module, simplest}

Also:

If F is an attribute-file (file, line), then rep (f) = {attribute, line, file, {file, line }}.

This also explains why:

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

Although the-file attribute is not written in the source code, the compiler still adds this attribute to the abstract code.

Finally, because the file ends in the first line, it also contains

{EOF, 1}

This is mentioned in one of the abstract form documents:

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 in 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 simplest and obtain 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 can see that the new export attribute appears in line 1 of the Code, including the tuple of the test with 0 parameters. The major change is a new 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)]}.

There are five form items in the function. The first item is the function atom, the second item is the row number, the second item is the function name, and the second item is the number of function parameters. The last item is a list containing the rep of each clause.

We can go deep into every clause of the function, but the objective of exploring Erlang abstract form is to understand the principles of metaprogramming. In actual programming, few people use form to dynamically generate a new function. The common method is to provide the source code of a function, or directly use the function parameters. For more detailed operations on the form directly, let's talk about it later.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/minyskirt/archive/2009/12/11/4988531.aspx

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.