Lua学习--5 表达式
2.5 – Expressions
The basic expressions in Lua are the following:
exp ::= prefixexp
exp ::= nil | false | true
exp ::= Number
exp ::= String
exp ::= function
exp ::= tableconstructor
exp ::= `...´
exp ::= exp binop exp
exp ::= unop exp
prefixexp ::= var | functioncall | `(´ exp `)´
Numbers and literal strings are explained in §2.1; variables are explained in §2.3; function definitions are explained in §2.5.9; function calls are explained in §2.5.8; table constructors are explained in §2.5.7. Vararg expressions, denoted by three dots ('...
'), can only be used when directly inside a vararg function; they are explained in §2.5.9.
Binary operators comprise arithmetic operators (see §2.5.1), relational operators (see §2.5.2), logical operators (see §2.5.3), and the concatenation operator (see §2.5.4). Unary operators comprise the unary minus (see §2.5.1), the unary not (see §2.5.3), and the unary length operator (see §2.5.5).
Both function calls and vararg expressions can result in multiple values. If an expression is used as a statement (only possible for function calls (see §2.4.6)), then its return list is adjusted to zero elements, thus discarding all returned values. If an expression is used as the last (or the only) element of a list of expressions, then no adjustment is made (unless the call is enclosed in parentheses). In all other contexts, Lua adjusts the result list to one element, discarding all values except the first one.
Here are some examples:
f() --f()没有返回值 adjusted to 0 results
g(f(), x) --f()只有一个返回值 f() is adjusted to 1 result
g(x, f()) --f()返回值不做调整 g gets x plus all results from f()
a,b,c = f(), x --f()只返回一个值 f() is adjusted to 1 result (c gets nil)
a,b = ... --a 取第一个值 ,b 取第二个值,如果...没有参数值则a,b 都为nil
--- a gets the first vararg parameter, b gets
-- the second (both a and b can get nil if there
-- is no corresponding vararg parameter)
a,b,c = x, f() -- f() is adjusted to 2 results
a,b,c = f() -- f() is adjusted to 3 results
return f() -- returns all results from f()
return ... -- returns all received vararg parameters
return x,y,f() -- returns x, y, and all results from f()
{f()} -- creates a list with all results from f()
{...} -- creates a list with all vararg parameters
{f(), nil} -- f() is adjusted to 1 result
Any expression enclosed in parentheses always results in only one value. Thus, (f(x,y,z))
is always a single value, even if f
returns several values. (The value of (f(x,y,z))
is the first value returned by f
or nil if f
does not return any values.)
函数和可变参数表达式都可以返回多个值,如果一个表达式只用于执行函数调用,则没有返回值
如果 一个表达式用于一List 表达式的最后一个或只有一个表达式的时候,不对返回结果进行处理。除非 被 括号包裹
其它情况Lua只取返回值 的第一个值 ,其它丢弃
未完待续。。。(计算运算)