⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 manual.html

📁 这是快速高效的脚本语言 LUA 的 win 移植到 ce 的版本. 这是 5.12的修改版
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<code>gettable_event</code> function.This function is not defined or callable in Lua.We use it here only for explanatory purposes.)<p>All global variables live as fields in ordinary Lua tables,called <em>environment tables</em> or simply<em>environments</em> (see <a href="#environ">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="#libdebug">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>where <code>_env</code> is the environment of the running function.(See <a href="#metatable">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.)<p><a name="stats"></a><a name="2.4"></a><h2>2.4 - Statements</h2><p>Lua supports an almost conventional set of statements,similar to those in Pascal or C.This set includesassignment, control structures, function calls,table constructors, and variable declarations.<p><a name="chunks"></a><a name="2.4.1"></a><h3>2.4.1 - Chunks</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>&acute;]}</pre>There are no empty statements and thus `<code>;;</code>&acute; is not legal.<p>Lua handles a chunk as the body of an anonymous function with a variable number of arguments(see <a href="#func-def">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.<p><a name="2.4.2"></a><h3>2.4.2 - Blocks</h3>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>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="#control">2.4.4</a>).<p><a name="assignment"></a><a name="2.4.3"></a><h3>2.4.3 - Assignment</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 ::= varlist1 `<b>=</b>&acute; explist1	varlist1 ::= var {`<b>,</b>&acute; var}	explist1 ::= exp {`<b>,</b>&acute; exp}</pre>Expressions are discussed in <a href="#expressions">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="#expressions">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>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>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="#metatable">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>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.)<p><a name="control"></a><a name="2.4.4"></a><h3>2.4.4 - Control Structures</h3>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>Lua also has a <b>for</b> statement, in two flavors (see <a href="#for">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> [explist1]</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>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>&acute; and`<code>do break end</code>&acute;,because now <b>return</b> and <b>break</b> are the last statements intheir (inner) blocks.<p><a name="for"></a><a name="2.4.5"></a><h3>2.4.5 - For Statement</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>&acute; exp `<b>,</b>&acute; exp [`<b>,</b>&acute; exp] <b>do</b> block <b>end</b></pre>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 var = e1, e2, e3 do block end</pre>is equivalent to the code:<pre>       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&#060;=_limit) or (_step&#060;=0 and _var>=_limit) do           local var = _var           block           _var = _var + _step         end       end</pre>Note the following:<ul><li> All three control expressions are evaluated only once,before the loop starts.They must all result in numbers.<li> <code>_var</code>, <code>_limit</code>, and <code>_step</code> are invisible variables.The names are here for explanatory purposes only.<li> If the third expression (the step) is absent,then a step of 1 is used.<li> You can use <b>break</b> to exit a <b>for</b> loop.<li> The loop variable <code>var</code> is local to the loop;you cannot use its value after the <b>for</b> ends or is broken.If you need the value of the loop variable <code>var</code>,then assign it to another variable before breaking or exiting the loop.</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> explist1 <b>do</b> block <b>end</b>	namelist ::= Name {`<b>,</b>&acute; Name}</pre>A <b>for</b> statement like<pre>       for var_1, ..., var_n in explist do block end</pre>is equivalent to the code:<pre>       do         local _f, _s, _var = explist         while true do           local var_1, ... , var_n = _f(_s, _var)           _var = var_1           if _var == nil then break end           block         end       end</pre>Note the following:<ul><li> <code>explist</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> <code>_f</code>, <code>_s</code>, and <code>_var</code> are invisible variables.The names are here for explanatory purposes only.<li> You can use <b>break</b> to exit a <b>for</b> loop.<li> The loop variables <code>var_i</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.</ul><p><a name="funcstat"></a><a name="2.4.6"></a><h3>2.4.6 - Function Calls as Statements</h3>To allow possible side-effects,function calls can be executed as statements:<pre>	stat ::= functioncall</pre>In this case, all returned values are thrown away.Function calls are explained in <a href="#functioncall">2.5.8</a>.<p><a name="localvar"></a><a name="2.4.7"></a><h3>2.4.7 - Local Declarations</h3>Local variables may be declared anywhere inside a block.The declaration may include an initial assignment:<pre>	stat ::= <b>local</b> namelist [`<b>=</b>&acute; explist1]</pre>If present, an initial assignment has the same semanticsof a multiple assignment (see <a href="#assignment">2.4.3</a>).Otherwise, all variables are initialized with <b>nil</b>.<p>A chunk is also a block (see <a href="#chunks">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="#visibility">2.6</a>.<p><a name="expressions"></a><a name="2.5"></a><h2>2.5 - Expressions</h2><p>The basic expressions in Lua are the following:<pre>	exp ::= prefixexp	exp ::= <b>nil</b>  |  <b>false</b>  |  <b>true</b>	exp ::= Number	exp ::= String	exp ::= function	exp ::= tableconstructor	exp ::= `<b>...</b>&acute;	exp ::= exp binop exp	exp ::= unop exp	prefixexp ::= var  |  functioncall  |  `<b>(</b>&acute; exp `<b>)</b>&acute;</pre><p>Numbers and literal strings are explained in <a href="#lexical">2.1</a>;variables are explained in <a href="#variables">2.3</a>;function definitions are explained in <a href="#func-def">2.5.9</a>;function calls are explained in <a href="#functioncall">2.5.8</a>;table constructors are explained in <a href="#tableconstructor">2.5.7</a>.Vararg expressions,denoted by three dots (`<code>...</code>&acute;), can only be used insidevararg functions;they are explained in <a href="#func-def">2.5.9</a>.<p>Binary operators comprise arithmetic operators (see <a href="#arith">2.5.1</a>),relational operators (see <a href="#rel-ops">2.5.2</a>), and logical operators (see <a href="#logic">2.5.3</a>).Unary operators comprise the unary minus (see <a href="#arith">2.5.1</a>),the unary <b>not</b> (see <a href="#logic">2.5.3</a>),and the unary <em>length operator</em> (see <a href="#len-op">2.5.5</a>).<p>Both function calls and vararg expressions may result in multiple values.If the expression is used as a statement (see <a href="#funcstat">2.4.6</a>)(only possible for function calls),then its return list is adjusted to zero elements,thus discarding all returned values.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -