Lua4.0 Reference Manual (ii) 4.1-4.4

Source: Internet
Author: User

(Part 1)
--------------------------------------
4 languages
--------------------------------------
This section describes Lua's lexical, syntax, and semantics.

-------------------
4.1 lexical conventions
-------------------
The identifier in Lua can be a string consisting of any letters, numbers, and underscores. The first letter cannot be a number. This complies with the definition of identifiers in most languages, except that the meaning of letters depends on the current region settings: All characters that are considered to be letters in the current region settings can be used in identifiers. The following are reserved keywords that cannot be used as identifiers:
And break do else elseif
End for function if in
Local nil not or repeat
Return then until while

Lua is a case-sensitive language: And is a reserved word, but and α Nd (if allowed by the region settings) are different and valid identifiers. As a convention, the identifier (such as _ INPUT) Starting with an underscore and followed by an upper-case letter is reserved as an internal variable.

The following string indicates other tags:
~ = <= >=< >== + -*/
() {} [];,...

String constants can be defined by pair single quotes or double quotation marks, and can contain C-language escape sequences '\ A' (Bell),' \ B '(return ), '\ f' (Form feed),' \ n' (line feed), '\ R' (Press ENTER),' \ t' (horizontal tab ), '\ V' (vertical tab),' \ '(backslash),' \ "'(double nickname),' \ '(single quotes ), and '\ newline' (that is, a backslash is followed by a newline, which generates a new line in the string ). The character in the string can be specified by its value. The Escape Sequence '\ DDD' is used. Here, DDD is a three-digit number. The string in Lua can contain any 8-bit value, including embedded 0, which can be specified by '\ 000.

String constants can also be defined by [[...] pairs. This form of literal can be cross-row, can contain nested [...], and does not explain the escape sequence. This form is especially suitable for writing strings that contain program blocks or other referenced strings. For example, in an ascii code system, the following three constants are equivalent:
1) "Alo \ n123 \""
2) '\ 97lo \ 10 \ 04923 "'
3) [[AlO
123 "]

A comment can start anywhere outside the string with two hyphens (--) until the end of the line. In addition, the first line of the block is skipped if it starts from. This mechanism is used to allow Lua as a script interpreter in a UNIX system (see section 8 ).

A numerical constant can be written into an optional fractional part and an optional exponent part. The following are examples of valid numerical constants:
3 3.0 3.1416 314.16e-2 0.31416e1

-------------------
4.2 forced conversion
-------------------
Lua provides some automatic conversions between values during runtime. Arithmetic Operations on a string will try to convert the string to a numeric value, following the common rules. On the contrary, when a value is involved in a string operation, the value is converted to a string in a reasonable format. The conversion format requires that a value can be converted back to the original value after being converted to a string. Therefore, for a value, conversion does not need to generate a nice text format. To fully control the conversion from a numeric value to a string, use the format function (see section 6.2 ).

-------------------
4.3 Adjustment
-------------------
The function in Lua can return multiple values. Because there is no type declaration, when a function is called, the system does not know how many values the function will return or how many parameters it needs. Therefore, sometimes, the value list must be adjusted to the given length at runtime. If the actual value is greater than the required value, the extra value will be discarded. If the required value is greater than the actual value, nil expansion will be performed in the list as needed. Multiple values are also adjusted (see section 4.4.2) and function calls (see section 4.5.8.

-------------------
4.4 statement
-------------------
Lua supports almost all common statements, similar to Pascal and C. Common Commands include assignment, control structure, and process call. Unconventional Commands include table Constructors (see section 4.5.7) and local variable declarations (see section 4.4.6 ).

---------
4.4.1 Block
---------
A block is a list of statements. In syntax, a block is equivalent to a chunk:
Block: = chunk
A block can also be explicitly defined:
Stat: = do block end
Explicit block is useful when controlling the scope of local variables (see section 4.4.6. Explicit blocks are sometimes used to add a return or break statement to another block (see Section 4.4.3 ).

---------
4.4.2 value assignment
---------
Lua supports multiple values. Therefore, the syntax defines a variable list on the left and an expression list on the right. Both list elements are separated by commas.
Stat: = varlist1 '= 'explist1
Varlist1 ::= var {', 'var}
This statement first finds all the values on the right, sorts the variables on the left, and assigns values to them. Therefore, the Code:
I = 3
I, a [I] = 4, 20
Setting a [3] to 20 does not affect a [4] Because I is evaluated before being assigned to 4 in a [I.

Multiple values can be used to exchange values of two variables, as shown below:
X, Y = Y, X

The list on both sides of the value assignment may have different lengths. Before assigning values, the Value List is adjusted to be equal to the length of the Variable list (see section 4.3 ).

A name can indicate a global variable, a local variable, or a formal parameter:
VaR: = Name

Square brackets are used to index the table:
VaR: = varorfunc '['exp1']'
Varorfunc: = var | functioncall
The varorfunc result is a table, which is obtained by the field indexed by the value of the expression exp1.

Var. Name is just the syntax sugar of VaR ["name.
VaR: = varorfunc '. 'name

The meaning of global variables and subscript variables assignment and Evaluation (evaluations) can be modified by TAG method (see section 4.8 ). In fact, an X = Val value. Here X is a global variable, which is equivalent to calling setglobal ("X", Val ); and t [I] = Val is equivalent to settable_event (t, I, Val ). For a complete description of these functions, see section 4.8 (setglobal is in the basic library, and settable_event is only used for interpretation purposes ).

---------
4.4.3 Control Structure
---------
The control structure if, while, and repeat have common meanings and common syntax.
Stat: = while exp1 do block end
Stat: = repeat block until exp1
Stat: = If exp1 then block {elseif exp1 then block} [else block] End
The condition expression exp1 of a control structure can return any value. All values that are not nil are considered true, and only NIL is considered false.

Return statements are used to return values from functions or blocks. Because a function or block can return multiple values, the syntax of the return statement is:
Stat: = return [explist1]
The break statement can be used to terminate the execution of a loop. The next statement after the loop is skipped:
Stat ::= break
A break ends a loop (while, repeat, or for) that contains its innermost layer ).

For syntax reasons, the return and break statements can only be written in the last sentence of a block. If you really need to return or break in the middle of a block, you can use an explicit block, as in the 'do return end' statement, at this time, return is the last statement of the inner Block.

---------
4.4.4 for statement
---------
A For statement has two formats: A value and a table. The numeric for loop has the following syntax:
Stat: = for name' = 'exp1', 'exp1 [', 'exp1] Do block end
A For statement is like
For Var = E1, E2, E3 do block end
Equivalent to code:
Do
Local var, _ limit, _ step = tonumber (E1), tonumber (E2), tonumber (E3)
If not (var and _ limit and _ step) then error () End
While (_ Step> 0 and VAR <= _ limit) or (_ Step <= 0 and VAR> = _ limit) Do
Block
Var = var + _ Step
End
End
Note:
> _ Limit and _ step are invisible variables. The name here is only for the purpose of interpretation.
> If you assign values to VaR within a block, the behavior is not defined.
> If there is no third expression (step size), The step size is 1.
> The limit and step are evaluated only once before the loop starts.
> The variable VAR is partial for the statement. Its value cannot be used after the for operation.
> You can use break to Exit A for loop. If you need the index value, assign it to another variable before exiting.

The for statement of the table traverses all key-value pairs (index, value) of the given table ). It has the following syntax:
Stat: = for name', 'name in exp1 do block end
A For statement is like
For index, value in Exp do block end
It is equivalent to the following code:
Do
Local _ t = exp
Local index, value = next (T, nil)
While index do
Block
Index, value = next (T, index)
End
End
Note:
> _ T is an invisible variable. The name here is only used for the purpose of interpretation.
> If an index is assigned to a block, the action is not defined.
> If _ t is changed during the time interval, the behavior is not defined.
> The index and value variables are partial for the statement. Their values cannot be used after the for statement ends.
> You can use break to Exit A for loop. If you need index or value values, assign them to another variable before exiting.
> The Order of element traversal in a table is undefined, even if it is a digital index. If you want to traverse the index in numerical order, use numeric.

---------
4.4.5 function call as a statement
---------
Due to side-effects, function calls can be executed as statements.
Stat: = functioncall
In this case, all returned values are discarded. Function calls are explained in section 4.5.8.

---------
4.4.6 local Declaration
---------
Local variables can be declared anywhere in the block. A statement can contain an initial value.
Stat: = Local declist [init]
Declist: = Name {', 'name}
Init: = '= 'explist1
If an initial value assignment operation is performed, it has the same semantics as multiple values. Otherwise, all variables are initialized as nil.

A chunk is also a block, so local variables can be declared externally in any explicit block.

The scope of the local variable starts from the declared place until the block ends. Therefore, code local print = print creates a local variable named print. The initial value of this local variable is the value of a global variable with the same name.
(To be continued)

Lua4.0 Reference Manual (ii) 4.1-4.4

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.