📄 manual.html
字号:
<p>All global variables live as fields in ordinary Lua tables,called <em>environment tables</em> or simply<em>environments</em> (see <a href="#2.9">§2.9</a>).Each function has its own reference to an environment,so that all global variables in this functionwill refer to this environment table.When a function is created,it inherits the environment from the function that created it.To get the environment table of a Lua function,you call <a href="#pdf-getfenv"><code>getfenv</code></a>.To replace it,you call <a href="#pdf-setfenv"><code>setfenv</code></a>.(You can only manipulate the environment of C functionsthrough the debug library; (see <a href="#5.9">§5.9</a>).)<p>An access to a global variable <code>x</code>is equivalent to <code>_env.x</code>,which in turn is equivalent to<pre> gettable_event(_env, "x")</pre><p>where <code>_env</code> is the environment of the running function.(See <a href="#2.8">§2.8</a> for a complete description of the<code>gettable_event</code> function.This function is not defined or callable in Lua.Similarly, the <code>_env</code> variable is not defined in Lua.We use them here only for explanatory purposes.)<h2>2.4 - <a name="2.4">Statements</a></h2><p>Lua supports an almost conventional set of statements,similar to those in Pascal or C.This set includesassignment, control structures, function calls,and variable declarations.<h3>2.4.1 - <a name="2.4.1">Chunks</a></h3><p>The unit of execution of Lua is called a <em>chunk</em>.A chunk is simply a sequence of statements,which are executed sequentially.Each statement can be optionally followed by a semicolon:<pre> chunk ::= {stat [`<b>;</b>´]}</pre><p>There are no empty statements and thus '<code>;;</code>' is not legal.<p>Lua handles a chunk as the body of an anonymous function with a variable number of arguments(see <a href="#2.5.9">§2.5.9</a>).As such, chunks can define local variables,receive arguments, and return values.<p>A chunk may be stored in a file or in a string inside the host program.When a chunk is executed, first it is pre-compiled into instructions fora virtual machine,and then the compiled code is executedby an interpreter for the virtual machine.<p>Chunks may also be pre-compiled into binary form;see program <code>luac</code> for details.Programs in source and compiled forms are interchangeable;Lua automatically detects the file type and acts accordingly.<h3>2.4.2 - <a name="2.4.2">Blocks</a></h3><p>A block is a list of statements;syntactically, a block is the same as a chunk:<pre> block ::= chunk</pre><p>A block may be explicitly delimited to produce a single statement:<pre> stat ::= <b>do</b> block <b>end</b></pre><p>Explicit blocks are usefulto control the scope of variable declarations.Explicit blocks are also sometimes used toadd a <b>return</b> or <b>break</b> statement in the middleof another block (see <a href="#2.4.4">§2.4.4</a>).<h3>2.4.3 - <a name="2.4.3">Assignment</a></h3><p>Lua allows multiple assignment.Therefore, the syntax for assignmentdefines a list of variables on the left sideand a list of expressions on the right side.The elements in both lists are separated by commas:<pre> stat ::= varlist `<b>=</b>´ explist varlist ::= var {`<b>,</b>´ var} explist ::= exp {`<b>,</b>´ exp}</pre><p>Expressions are discussed in <a href="#2.5">§2.5</a>.<p>Before the assignment,the list of values is <em>adjusted</em> to the length ofthe list of variables.If there are more values than needed,the excess values are thrown away.If there are fewer values than needed,the list is extended with as many <b>nil</b>'s as needed.If the list of expressions ends with a function call,then all values returned by this call enter in the list of values,before the adjustment(except when the call is enclosed in parentheses; see <a href="#2.5">§2.5</a>).<p>The assignment statement first evaluates all its expressionsand only then are the assignments performed.Thus the code<pre> i = 3 i, a[i] = i+1, 20</pre><p>sets <code>a[3]</code> to 20, without affecting <code>a[4]</code>because the <code>i</code> in <code>a[i]</code> is evaluated (to 3)before it is assigned 4.Similarly, the line<pre> x, y = y, x</pre><p>exchanges the values of <code>x</code> and <code>y</code>.<p>The meaning of assignments to global variablesand table fields can be changed via metatables.An assignment to an indexed variable <code>t[i] = val</code> is equivalent to<code>settable_event(t,i,val)</code>.(See <a href="#2.8">§2.8</a> for a complete description of the<code>settable_event</code> function.This function is not defined or callable in Lua.We use it here only for explanatory purposes.)<p>An assignment to a global variable <code>x = val</code>is equivalent to the assignment<code>_env.x = val</code>,which in turn is equivalent to<pre> settable_event(_env, "x", val)</pre><p>where <code>_env</code> is the environment of the running function.(The <code>_env</code> variable is not defined in Lua.We use it here only for explanatory purposes.)<h3>2.4.4 - <a name="2.4.4">Control Structures</a></h3><p>The control structures<b>if</b>, <b>while</b>, and <b>repeat</b> have the usual meaning andfamiliar syntax:<pre> stat ::= <b>while</b> exp <b>do</b> block <b>end</b> stat ::= <b>repeat</b> block <b>until</b> exp stat ::= <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b></pre><p>Lua also has a <b>for</b> statement, in two flavors (see <a href="#2.4.5">§2.4.5</a>).<p>The condition expression of acontrol structure may return any value.Both <b>false</b> and <b>nil</b> are considered false.All values different from <b>nil</b> and <b>false</b> are considered true(in particular, the number 0 and the empty string are also true).<p>In the <b>repeat</b>–<b>until</b> loop,the inner block does not end at the <b>until</b> keyword,but only after the condition.So, the condition can refer to local variablesdeclared inside the loop block.<p>The <b>return</b> statement is used to return valuesfrom a function or a chunk (which is just a function).Functions and chunks may return more than one value,so the syntax for the <b>return</b> statement is<pre> stat ::= <b>return</b> [explist]</pre><p>The <b>break</b> statement is used to terminate the execution of a<b>while</b>, <b>repeat</b>, or <b>for</b> loop,skipping to the next statement after the loop:<pre> stat ::= <b>break</b></pre><p>A <b>break</b> ends the innermost enclosing loop.<p>The <b>return</b> and <b>break</b>statements can only be written as the <em>last</em> statement of a block.If it is really necessary to <b>return</b> or <b>break</b> in themiddle of a block,then an explicit inner block can be used,as in the idioms<code>do return end</code> and <code>do break end</code>,because now <b>return</b> and <b>break</b> are the last statements intheir (inner) blocks.<h3>2.4.5 - <a name="2.4.5">For Statement</a></h3><p>The <b>for</b> statement has two forms:one numeric and one generic.<p>The numeric <b>for</b> loop repeats a block of code while acontrol variable runs through an arithmetic progression.It has the following syntax:<pre> stat ::= <b>for</b> Name `<b>=</b>´ exp `<b>,</b>´ exp [`<b>,</b>´ exp] <b>do</b> block <b>end</b></pre><p>The <em>block</em> is repeated for <em>name</em> starting at the value ofthe first <em>exp</em>, until it passes the second <em>exp</em> by steps of thethird <em>exp</em>.More precisely, a <b>for</b> statement like<pre> for v = <em>e1</em>, <em>e2</em>, <em>e3</em> do <em>block</em> end</pre><p>is equivalent to the code:<pre> do local <em>var</em>, <em>limit</em>, <em>step</em> = tonumber(<em>e1</em>), tonumber(<em>e2</em>), tonumber(<em>e3</em>) if not (<em>var</em> and <em>limit</em> and <em>step</em>) then error() end while (<em>step</em> > 0 and <em>var</em> <= <em>limit</em>) or (<em>step</em> <= 0 and <em>var</em> >= <em>limit</em>) do local v = <em>var</em> <em>block</em> <em>var</em> = <em>var</em> + <em>step</em> end end</pre><p>Note the following:<ul><li>All three control expressions are evaluated only once,before the loop starts.They must all result in numbers.</li><li><code><em>var</em></code>, <code><em>limit</em></code>, and <code><em>step</em></code> are invisible variables.The names are here for explanatory purposes only.</li><li>If the third expression (the step) is absent,then a step of 1 is used.</li><li>You can use <b>break</b> to exit a <b>for</b> loop.</li><li>The loop variable <code>v</code> is local to the loop;you cannot use its value after the <b>for</b> ends or is broken.If you need this value,assign it to another variable before breaking or exiting the loop.</li></ul><p>The generic <b>for</b> statement works over functions,called <em>iterators</em>.On each iteration, the iterator function is called to produce a new value,stopping when this new value is <b>nil</b>.The generic <b>for</b> loop has the following syntax:<pre> stat ::= <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b> namelist ::= Name {`<b>,</b>´ Name}</pre><p>A <b>for</b> statement like<pre> for <em>var_1</em>, ···, <em>var_n</em> in <em>explist</em> do <em>block</em> end</pre><p>is equivalent to the code:<pre> do local <em>f</em>, <em>s</em>, <em>var</em> = <em>explist</em> while true do local <em>var_1</em>, ···, <em>var_n</em> = <em>f</em>(<em>s</em>, <em>var</em>) <em>var</em> = <em>var_1</em> if <em>var</em> == nil then break end <em>block</em> end end</pre><p>Note the following:<ul><li><code><em>explist</em></code> is evaluated only once.Its results are an <em>iterator</em> function,a <em>state</em>,and an initial value for the first <em>iterator variable</em>.</li><li><code><em>f</em></code>, <code><em>s</em></code>, and <code><em>var</em></code> are invisible variables.The names are here for explanatory purposes only.</li><li>You can use <b>break</b> to exit a <b>for</b> loop.</li><li>The loop variables <code><em>var_i</em></code> are local to the loop;you cannot use their values after the <b>for</b> ends.If you need these values,then assign them to other variables before breaking or exiting the loop.</li></ul><h3>2.4.6 - <a name="2.4.6">Function Calls as Statements</a></h3><p>To allow possible side-effects,function calls can be executed as statements:<pre> stat ::= functioncall</pre><p>In this case, all returned values are thrown away.Function calls are explained in <a href="#2.5.8">§2.5.8</a>.<h3>2.4.7 - <a name="2.4.7">Local Declarations</a></h3><p>Local variables may be declared anywhere inside a block.The declaration may include an initial assignment:<pre> stat ::= <b>local</b> namelist [`<b>=</b>´ explist]</pre><p>If present, an initial assignment has the same semanticsof a multiple assignment (see <a href="#2.4.3">§2.4.3</a>).Otherwise, all variables are initialized with <b>nil</b>.<p>A chunk is also a block (see <a href="#2.4.1">§2.4.1</a>),and so local variables can be declared in a chunk outside any explicit block.The scope of such local variables extends until the end of the chunk.<p>The visibility rules for local variables are explained in <a href="#2.6">§2.6</a>.<h2>2.5 - <a name="2.5">Expressions</a></h2>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -