(Part 1)
-------------------
4.5 expression
-------------------
---------
4.5.1 Basic Expressions
---------
The basic expression in Lua is:
Exp: = '('exp ')'
Exp: = Nil
Exp: = Number
Exp: = literal
Exp: = VaR
Exp: = upvalue
Exp: = Function
Exp: = functioncall
Exp: = tableconstructor
The values (numerical constants) and string constants are explained in section 4.1. The variables have been explained in section 4.4.2. Upvalue is explained in section 4.6. Function definitions are described in section 4.5.9. Function calls are explained in section 4.5.8. The table constructor is described in Section 4.5.7.
Calling a global variable X is equivalent to calling getglobal ("X"), and a subscript variable t [I] is equivalent to gettable_event (t, I ). For explanations of these functions, see section 4.8 (getglobal is in the basic library, and gettable_event is only used for interpretation purposes ).
A non-terminated exp1 is used to indicate that the return value of an expression must be adjusted to a value:
Exp1: = exp
---------
4.5.2 Arithmetic Operators
---------
Lua supports common arithmetic operators. These operators are binary operators + (plus),-(minus), * (multiplication),/(Division) and ^ (power), and unary operators-(negative ). If the operand is a numeric value or a string that can be converted to a numeric value (according to the rules given in section 4.2), all operations (except power operations) have a general significance. Otherwise, an applicable tag function (see section 4.8) will be called. The power operation will always call a tag function. The standard math redefines power operations as expected (see section 6.3 ).
---------
4.5.3 Relational operators
---------
The Relational operators in Lua are:
== ~ = <> <=> =
They return nil as false and non-nil as true.
Equal (=) First, compare the labels of the two operands. If they are different, the result is nil. Otherwise, compare their values. The value or string is compared in common mode. Tables, userdata, and functions are compared by reference. That is to say, the two tables are considered equal only when they are the same. Unequal operation ~ = And equal operation (=) have completely opposite results.
The conversion rules in section 4.2 do not apply to equal comparison. Therefore, the result of "0" = 0 "is false. t [0] and T [" 0 "] indicate different elements in the table.
The sequence comparison operator works like this: if both parameters are numerical values, they are compared with numerical values. If both parameters can be converted to strings, they are compared in lexicographically. Otherwise, the label method "LT" will be called (see section 4.8 ).
---------
4.5.4 logical operators
---------
The logical operators in Lua are:
And or not
Like the control structure, all logical operators think that nil is false while others are true.
The Union operator and returns nil if its first parameter is nil; otherwise, it returns its second parameter. The extract operator or returns its first parameter if it is different from nil; otherwise, it returns its second parameter. And and or are short-circuit values, that is, the second operand is evaluated only when needed.
There are two usage habits using logical operators. The first one is:
X = X or V
It is equivalent
If X = nil then x = V end
This method sets a default value for X when X is not set.
The second habit is:
X = A and B or C
It should be read as X = (a and B) or c. It is equivalent
If a then x = B else x = C end
The condition is that B is not nil.
---------
4.5.5 connection
---------
The String concatenation operator in Lua is represented by two points. If the two operands are strings or numbers, they are converted to strings according to the rules in section 4.2. Otherwise, the label method "Concat" will be called (see section 4.8 ).
---------
4.5.6 priority
---------
The operator priority is listed in the following table, in ascending order:
And or
<> <=> = ~ ===
..
+-
*/
Not-(unary)
^
Except for the specific left concatenation of all binary operators ^, power operations have the right concatenation. Pre-compilation may rearrange the order of values of related operators (such as... and +), as long as the optimization does not change the normal results. On the contrary, these optimizations may change some results if you define irrelevant label methods for these operators.
---------
4.5.7 Constructor
---------
The table constructor is the expression used to create a table. When you evaluate the constructor, a new table is generated. The constructor can be used to create an empty table or a new table and initialize some fields.
The constructor syntax is as follows:
Tableconstructor: = '{'fieldlist '}'
Fieldlist: = lfieldlist | ffieldlist | lfieldlist '; 'ffieldlist | ffieldlist'; 'lfieldlist
Lfieldlist: = [lfieldlist1]
Ffieldlist: = [ffieldlist1]
Lfieldlist1 is used to initialize the list.
Lfieldlist1 ::= exp {', 'exp} [', ']
The expression in the list is assigned a continuous value index, which starts from 1. For example:
A = {"V1", "V2", 34}
Equivalent:
Do
Local temp = {}
Temp [1] = "V1"
Temp [2] = "V2"
Temp [3] = 34
A = temp
End
Other fields in the ffieldlist1 initialization table:
Ffieldlist1 ::= Ffield {', 'ffield} [', ']
Ffield: = '['exp'] ''= 'exp | name' = 'exp
For example:
A = {[F (k)] = g (Y), x = 1, y = 3, [0] = B + c}
Equivalent:
Do
Local temp = {}
Temp [F (k)] = g (y)
Temp. x = 1 -- or temp ["X"] = 1
Temp. Y = 3 -- or temp ["Y"] = 3
Temp [0] = B + C
A = temp
End
In fact, expressions such as {x = 1, y = 4} are the syntactic sugar of the {["X"] = 1, ["Y"] = 4} expression.
Either form can have an optional ending comma, and can be separated by semicolons in the same structure. For example, the following forms are correct.
X = {;}
X = {"A", "B ",}
X = {type = "list"; "a", "B "}
X = {f (0), F (1), F (2),; n = 3 ,}
---------
4.5.8 function call
---------
Function calls in Lua have the following syntax:
Functioncall: = varorfunc ARGs
First, varorfunc is evaluated. If its value type is function, this function is called with the given parameter. Otherwise, the label method "function" is called. The first parameter is the value of varorfunc, followed by the original call parameters (see section 4.8 ).
Shape:
Functioncall: = varorfunc ': 'name ARGs
It can be used to call "methods ". V: Name (...) is the syntactic sugar of V. Name (v,...), except that V is evaluated only once.
The parameter syntax is as follows:
ARGs: = '(' [explist1] ')'
ARGs: = tableconstructor
ARGs: = literal
Explist1 ::={ exp1 ','} exp
All parameter expressions are evaluated before function calls. F {...} calls the syntax sugar of F ({...}), that is, the parameter list is a new table. F '... '(or F "... "Or f [[...]) the call is F ('... '), that is, the parameter list is a String constant.
Because a function can return any number of values (see Section 4.4.3), the number of returned values must be adjusted before they are used (see section 4.3 ). If a function call is used as a statement (see section 4.4.5), its return results will be adjusted to 0, and all returned values will be discarded. If a function is called where a value (expressed as a non-final exp1 In the syntax) is required, the returned result is adjusted to one, therefore, all items except the first return value are discarded. If a function is called where multiple values are needed (syntactically expressed as non-ending exp), the number of returned results is not adjusted. The only thing that can hold multiple values is the last (or unique) expression of the value assignment, in the parameter list, or in the return statement. Here are some examples:
F () -- adjusted to 0 return values
G (f (), x) -- F () adjusted to 1 return value
G (x, F () -- g obtains all return values of x and F ().
A, B, c = f (), X -- F () adjusted to 1 return value (C obtains nil)
A, B, c = x, F () -- F () adjusted to two return values
A, B, c = f () -- F () adjusted to three return values
Return F () -- return the return values of all F ()
Return X, Y, F () -- return the values of A, B, and all F ()
-------------------
4.5.9 Function Definition
-------------------
The syntax defined by the function is:
Function: = function' ('[parlist1]') 'block end
Stat: = function funcname '(' [parlist1] ')' block end
Funcname: = Name | Name '. 'name | name': 'name
Statement
Function f ()... end
Yes
F = function ()... end
Syntax sugar.
Statement
Function V. F ()... end
Yes
V. F = function ()... end
Syntax sugar.
A function is an executable Expression and Its Value Type is function. When Lua pre-compiles a chunk, all its function bodies will also be pre-compiled. Then, when Lua executes the function definition, its upvalue is determined (see section 4.6), and the function is instantiated (or closed. This function instance (or closure) is the final value of the expression. Different instances of the same function may have different upvalue.
Parameters are the same as local variables. parameter values are initialized.
Parlist1: = '...'
Parlist1: = Name {', 'name} [', ''... ']
When a function is called, the number of real parameters is adjusted to be the same as that of the form parameter (see section 4.3), unless the function is a Variable Parameter Function (vararg function ), that is, the last three vertices in the parameter list ('... '). A Variable Parameter function does not need to adjust its real parameter list. Instead, it collects all the extra real parameters into the implicit parameters named Arg. The ARG value is a table. Its Field n represents the number of additional parameters, and the additional parameters are located at 1, 2,..., n.
For example, consider the following definition:
Function f (a, B) End
Function g (a, B,...) End
Function R () return 1, 2, 3 end
We have the following ing between real parameters and form parameters:
Call Parameters
F (3) A = 3, B = Nil
F (3, 4) A = 3, B = 4
F (3, 4, 5) A = 3, B = 4
F (R (), 10) A = 1, B = 10
F (R () A = 1, B = 2
G (3) A = 3, B = nil, Arg = {n = 0}
G (3, 4) A = 3, B = 4, Arg = {n = 0}
G (3, 4, 5, 8) A = 3, B = 4, Arg = {5, 8; n = 2}
G (5, R () A = 5, B = 1, Arg = {2, 3; n = 2}
The return result is returned by the Return Statement (see Section 4.4.3 ). If no return command is executed at the end of the function, the function does not return a value.
Syntax
Funcname: = name': 'name
Used to define a function. A function has an implicit parameter self.
Statement
Function V: F (...)... end
Just
V. F = function (self,...)... end
Syntax sugar.
Note that the function obtains an additional form parameter named self.
(To be continued)
Lua4.0 Reference Manual (iii) 4.5