📄 manual.html
字号:
If both arguments are numbers, then they are compared as such.Otherwise, if both arguments are strings,then their values are compared according to the current locale.Otherwise, Lua tries to call the "lt" or the "le"metamethod (see <a href="#metatable">2.8</a>).<p><a name="2.5.3"><h3>2.5.3 - Logical Operators</h3></a>The logical operators in Lua are<PRE> and or not</PRE>Like the control structures (see <a href="#control">2.4.4</a>),all logical operators consider both <B>false</B> and <B>nil</B> as falseand anything else as true.<p>The operator <b>not</b> always return <B>false</B> or <B>true</B>.<p>The conjunction operator <b>and</b> returns its first argumentif this value is <B>false</B> or <B>nil</B>;otherwise, <b>and</b> returns its second argument.The disjunction operator <b>or</b> returns its first argumentif this value is different from <B>nil</B> and <B>false</B>;otherwise, <b>or</b> returns its second argument.Both <b>and</b> and <b>or</b> use short-cut evaluation,that is,the second operand is evaluated only if necessary.For example,<PRE> 10 or error() -> 10 nil or "a" -> "a" nil and 10 -> nil false and error() -> false false and nil -> false false or nil -> nil 10 and 20 -> 20</PRE><p><a name="concat"><a name="2.5.4"><h3>2.5.4 - Concatenation</h3></a></a>The string concatenation operator in Lua isdenoted by two dots (`<code>..</code>´).If both operands are strings or numbers, then they are converted tostrings according to the rules mentioned in <a href="#coercion">2.2.1</a>.Otherwise, the "concat" metamethod is called (see <a href="#metatable">2.8</a>).<p><a name="2.5.5"><h3>2.5.5 - Precedence</h3></a>Operator precedence in Lua follows the table below,from lower to higher priority:<PRE> or and < > <= >= ~= == .. + - * / not - (unary) ^</PRE>You can use parentheses to change the precedences in an expression.The concatenation (`<code>..</code>´) and exponentiation (`<code>^</code>´)operators are right associative.All other binary operators are left associative.<p><a name="tableconstructor"><a name="2.5.6"><h3>2.5.6 - Table Constructors</h3></a></a>Table constructors are expressions that create tables.Every time a constructor is evaluated, a new table is created.Constructors can be used to create empty tables,or to create a table and initialize some of its fields.The general syntax for constructors is<pre> tableconstructor ::= `<b>{</b>´ [fieldlist] `<b>}</b>´ fieldlist ::= field {fieldsep field} [fieldsep] field ::= `<b>[</b>´ exp `<b>]</b>´ `<b>=</b>´ exp | Name `<b>=</b>´ exp | exp fieldsep ::= `<b>,</b>´ | `<b>;</b>´</pre><p>Each field of the form <code>[exp1] = exp2</code> adds to the new table an entrywith key <code>exp1</code> and value <code>exp2</code>.A field of the form <code>name = exp</code> is equivalent to<code>["name"] = exp</code>.Finally, fields of the form <code>exp</code> are equivalent to<code>[i] = exp</code>, where <code>i</code> are consecutive numerical integers,starting with 1.Fields in the other formats do not affect this counting.For example,<PRE> a = {[f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45}</PRE>is equivalent to<PRE> do local temp = {} temp[f(1)] = g temp[1] = "x" -- 1st exp temp[2] = "y" -- 2nd exp temp.x = 1 -- temp["x"] = 1 temp[3] = f(x) -- 3rd exp temp[30] = 23 temp[4] = 45 -- 4th exp a = temp end</PRE><p>If the last field in the list has the form <code>exp</code>and the expression is a function call,then all values returned by the call enter the list consecutively(see <a href="#functioncall">2.5.7</a>).To avoid this,enclose the function call in parentheses (see <a href="#expressions">2.5</a>).<p>The field list may have an optional trailing separator,as a convenience for machine-generated code.<p><a name="functioncall"><a name="2.5.7"><h3>2.5.7 - Function Calls</h3></a></a>A function call in Lua has the following syntax:<pre> functioncall ::= prefixexp args</pre>In a function call,first <em>prefixexp</em> and <em>args</em> are evaluated.If the value of <em>prefixexp</em> has type <em>function</em>,then that function is calledwith the given arguments.Otherwise, its "call" metamethod is called,having as first parameter the value of <em>prefixexp</em>,followed by the original call arguments(see <a href="#metatable">2.8</a>).<p>The form<pre> functioncall ::= prefixexp `<b>:</b>´ Name args</pre>can be used to call "methods".A call <code>v:name(...)</code>is syntactic sugar for <code>v.name(v,...)</code>,except that <code>v</code> is evaluated only once.<p>Arguments have the following syntax:<pre> args ::= `<b>(</b>´ [explist1] `<b>)</b>´ args ::= tableconstructor args ::= Literal</pre>All argument expressions are evaluated before the call.A call of the form <code>f{...}</code> is syntactic sugar for<code>f({...})</code>, that is,the argument list is a single new table.A call of the form <code>f'...'</code>(or <code>f"..."</code> or <code>f[[...]]</code>) is syntactic sugar for<code>f('...')</code>, that is,the argument list is a single literal string.<p>Because a function can return any number of results(see <a href="#control">2.4.4</a>),the number of results must be adjusted before they are used.If the function is called as a statement (see <a href="#funcstat">2.4.6</a>),then its return list is adjusted to zero elements,thus discarding all returned values.If the function is called inside another expressionor in the middle of a list of expressions,then its return list is adjusted to one element,thus discarding all returned values except the first one.If the function is called as the last element of a list of expressions,then no adjustment is made(unless the call is enclosed in parentheses).<p>Here are some examples:<PRE> f() -- adjusted to 0 results g(f(), x) -- f() is adjusted to 1 result g(x, f()) -- g gets x plus all values returned by f() a,b,c = f(), x -- f() is adjusted to 1 result (and c gets nil) 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 values returned by f() return x,y,f() -- returns x, y, and all values returned by f() {f()} -- creates a list with all values returned by f() {f(), nil} -- f() is adjusted to 1 result</PRE><p>If you enclose a function call in parentheses,then it is adjusted to return exactly one value:<PRE> return x,y,(f()) -- returns x, y, and the first value from f() {(f())} -- creates a table with exactly one element</PRE><p>As an exception to the free-format syntax of Lua,you cannot put a line break before the `<code>(</code>´ in a function call.That restriction avoids some ambiguities in the language.If you write<PRE> a = f (g).x(a)</PRE>Lua would read that as <code>a = f(g).x(a)</code>.So, if you want two statements, you must add a semi-colon between them.If you actually want to call <code>f</code>,you must remove the line break before <code>(g)</code>.<p>A call of the form <code>return</code> <em>functioncall</em> is calleda <em>tail call</em>.Lua implements <em>proper tail calls</em>(or <em>proper tail recursion</em>):In a tail call,the called function reuses the stack entry of the calling function.Therefore, there is no limit on the number of nested tail calls thata program can execute.However, a tail call erases any debug information about thecalling function.Note that a tail call only happens with a particular syntax,where the <b>return</b> has one single function call as argument;this syntax makes the calling function returns exactlythe returns of the called function.So, all the following examples are not tails calls:<PRE> return (f(x)) -- results adjusted to 1 return 2 * f(x) return x, f(x) -- additional results f(x); return -- results discarded return x or f(x) -- results adjusted to 1</PRE><p><a name="func-def"><a name="2.5.8"><h3>2.5.8 - Function Definitions</h3></a></a><p>The syntax for function definition is<pre> function ::= <b>function</b> funcbody funcbody ::= `<b>(</b>´ [parlist1] `<b>)</b>´ block <b>end</b></pre><p>The following syntactic sugar simplifies function definitions:<pre> stat ::= <b>function</b> funcname funcbody stat ::= <b>local</b> <b>function</b> Name funcbody funcname ::= Name {`<b>.</b>´ Name} [`<b>:</b>´ Name]</pre>The statement<PRE> function f () ... end</PRE>translates to<PRE> f = function () ... end</PRE>The statement<PRE> function t.a.b.c.f () ... end</PRE>translates to<PRE> t.a.b.c.f = function () ... end</PRE>The statement<PRE> local function f () ... end</PRE>translates to<PRE> local f; f = function () ... end</PRE><p>A function definition is an executable expression,whose value has type <em>function</em>.When Lua pre-compiles a chunk,all its function bodies are pre-compiled too.Then, whenever Lua executes the function definition,the function is <em>instantiated</em> (or <em>closed</em>).This function instance (or <em>closure</em>)is the final value of the expression.Different instances of the same functionmay refer to different external local variablesand may have different environment tables.<p>Parameters act as local variables that areinitialized with the argument values:<pre> parlist1 ::= namelist [`<b>,</b>´ `<b>...</b>´] parlist1 ::= `<b>...</b>´</pre>When a function is called,the list of arguments is adjusted tothe length of the list of parameters,unless the function is a variadic or <em>vararg function</em>,which isindicated by three dots (`<code>...</code>´) at the end of its parameter list.A vararg function does not adjust its argument list;instead, it collects all extra arguments into an implicit parameter,called <code>arg</code>.The value of <a name="vararg"><code>arg</code></a> is a table,with a field <code>n</code> that holds the number of extra argumentsand with the extra arguments at positions 1, 2, ..., <code>n</code>.<p>As an example, consider the following definitions:<PRE> function f(a, b) end function g(a, b, ...) end function r() return 1,2,3 end</PRE>Then, we have the following mapping from arguments to parameters:<PRE> 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}</PRE><p>Results are returned using the <b>return</b> statement (see <a href="#control">2.4.4</a>).If control reaches the end of a functionwithout encountering a <b>return</b> statement,then the function returns with no results.<p>The <em>colon</em> syntaxis used for defining <em>methods</em>,that is, functions that have an implicit extra parameter <code>self</code>.Thus, the statement<PRE> function t.a.b.c:f (...) ... end</PRE>is syntactic sugar for<PRE> t.a.b.c.f = function (self, ...) ... end</PRE><p><a name="visibility"><a name="2.6"><h2>2.6 - Visibility Rules</h2></a></a><p>Lua is a lexically scoped language.The scope of variables begins at the first statement <em>after</em>their declaration and lasts until the end of the innermost block thatincludes the declaration.For instance:<PRE> x = 10 -- global variable do -- new block local x = x -- new `x', with value 10 print(x) --> 10 x = x+1 do -- another block local x = x+1 -- another `x' print(x) --> 12 end print(x) --> 11 end print(x) --> 10 (the global one)</PRE>Notice that, in a declaration like <code>local x = x</code>,the new <code>x</code> being declared is not in scope yet,and so the second <code>x</code> refers to the outside variable.<p>Because of the lexical scoping rules,local variables can be freely accessed by functionsdefined inside their scope.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -