Lua4.0 Reference Manual (1) 1-3

Source: Internet
Author: User

Note:
This document is the manual.html file in the doc directory.
The copyright of the original article belongs to the original author. This translation is only used for learning. If the translation is inappropriate, refer to the original article.
------------------- The following is the body -------------------
Reference manual for programming language lua4.0
--------------------------------------
1 Overview
--------------------------------------
Lua is an extended programming language that supports general programming and data description functions. As a powerful and lightweight configuration language, it is designed as a configuration language for other programs.

Lua is implemented as a library written in C language. As an extension language, Lua does not have the concept of "Main" function; it can only be embedded into the host for running, called an embedded program. The host can call a function to execute a Lua code, read and write the Lua variable, and register the C function called by the Lua code. With the use of C functions, Lua can expand itself to deal with different fields, and thus create a programming language that can be customized and shared with the syntax framework.

Lua is a freely distributed software that provides no guarantee, as stated in its copyright statement. The implementation described in this document can be obtained from the following address:

Http://www.lua.org/
(The original address has expired. Here is the current official website address)

Like other reference manuals, this document is also boring. For the decision-making discussions behind some Lua designs, refer to the following papers, which can be found on the official website.
> R. ierusalimschy, L. H. de Figueiredo, and W. Celes. Lua-an extensible extension language. Software: Practice & experience 26 #6 (1996) 635--652.
> L. h. de Figueiredo, R. ierusalimschy, and W. celes. the Design and Implementation of a language for extending applications. proceedings of XXI extends Ilian Seminar on software and hardware (1994) 273--283.
> L. H. de Figueiredo, R. ierusalimschy, and W. Celes. Lua: An extensible embedded language. dr. Dobb's journal 21 #12 (Dec 1996) 26--33.

--------------------------------------
2. Environment and block
--------------------------------------
All statements in Lua are executed in a global environment. This environment is initialized by the embedded Program Calling lua_open and continues until the call lua_close ends, or ends when the embedded program ends. If necessary, the host programmer can establish multiple independent global environments and switch between them freely (see section 5.1 ).

This global environment can be managed using Lua code or embedded programs. You can use the Lua implementation library API function to read and write global variables.

Global variables in Lua do not need to be declared. All variables are considered global unless explicitly declared as local (see section 4.4.6 ). Before the first assignment, the global variable value is nil (you can modify this default value, see section 4.8 ). A table stores all global names and values. For more information, see section 3 ).

The execution unit of Lua is called chunk ). Simply put, a block is a sequence of statements executed in sequence. Each statement can be optional followed by a semicolon:

Chunk: = {stat [';']}

The statement is described in section 4.4. (The above symbolic markup method is a general extension of BNF, {A} indicates 0 or multiple A, [a] indicates an optional, {A} + (the plus sign is in the upper right corner) indicates one or more. The complete syntax description is at the end of this document .)

A module can be stored in a file or a Host Program string. When a module is executed, it is first pre-compiled into the bytecode of the virtual machine, and then its statements are executed sequentially by the Simulated Virtual Machine. All modifications made by the module to the global environment are persistent. These modifications are still visible after the completion of the module.

Blocks can be pre-compiled into binary format and saved in files. For more information, see program luac. Text files containing block code are equivalent to their binary pre-compiled bytecode format. Lua can automatically detect the file type and perform corresponding operations.

--------------------------------------
3. Type and label (Types and tags)
--------------------------------------
Lua is a dynamic language. This means that the variable has no type; only the value has a type. Therefore, there is no type definition in Lua. All values contain their own types. In addition to the type, all values also have a tag ).

Lua has six basic data types: nil, number, String, function, userdata, and table. Nil is a type with a value of nil. Its main property is different from other values. Number indicates a real number (double precision floating point type), and string indicates a string. Lua is an 8-Bit String. Therefore, a string can contain all 8 characters, including internal 0 ('\ 0') (see section 4.1 ). The type function returns a string describing the type of the given value (see section 6.1 ).

In Lua, the function is the first-class values ). This means they can be stored in variables and passed as parameters to other functions or returned as results. Lua can call (and manipulate) functions written in Lua or C. These two functions can be distinguished by their labels: All Lua functions have the same tag, and all C functions have the same tag, and they are different from those of Lua functions. The tag function can return the tag of a given value (see section 6.1 ).

The userdata type allows the Lua variable to save any c pointer (void *). It is equivalent to the void * pointer in C language. Therefore, in Lua, there are no predefined operations except assignment and equality test operations. However, by using tag methods, programmers can define operations for userdata values (see section 4.8 ).

The table type is implemented as an associated array, that is, the array can be indexed not only by digits, but also by any value (except nil ). Therefore, this type can be used not only to represent normal arrays, but also to represent symbol tables, sets, and records. A table is the main data type in Lua. To indicate a record, Lua uses the field name as the subscript. The language uses the expression A. name as the syntactic sugar of a ["name. The table can also contain methods. Because the function is the first type, table fields can contain functions. T: f (x) is T. the syntax sugar of f (t, x). It calls the method F in table t and uses it as the first parameter (see section 4.5.9 ).

Note that table is an object rather than a value. A variable cannot contain a table and can only be referenced. Assign values, PASS Parameters, and return operations will always be performed on the reference of the table, instead of copying the table. In addition, the table must be explicitly created before use (see section 4.5.7 ).

Each type of nil, number, and string has different labels. All these types of values have the same predefined labels. As explained above, the values of function types can have two different labels, depending on the Lua function or C function. Finally, the types of userdata and table can have variable labels specified by programmers (see section 4.8 ). The tag function returns the tag of the given value. User tags are generated by the newtag function. The settag function is used to change the table labels (see section 6.1 ). Tags of userdata values can only be set in C (see section 5.7 ). When an event occurs, tags are mainly used to select tag methods ). The label method is the main mechanism for expanding Lua semantics (see section 4.8 ).
(To be continued)

Lua4.0 Reference Manual (1) 1-3

Related Article

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.