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

📄 manual.html

📁 一种小型的脚本开发语言Lua,Lua参考手册,中文的资料.
💻 HTML
📖 第 1 页 / 共 5 页
字号:
has its own reference to an environment,
so that all global variables in that function
will refer to that environment table.
When a function is created,
it inherits the environment from the function that created it.
To change or get the environment table of a Lua function,
you call <code>setfenv</code> or <code>getfenv</code> (see <a href="#setfenv">5.1</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.
(The <code>_env</code> variable is not defined in Lua.
We use it here only for explanatory purposes.)

<p><a name="stats"><h2>2.4 - Statements</h2></a>

<p>Lua supports an almost conventional set of statements,
similar to those in Pascal or C.
This set includes
assignment, control structures, procedure calls,
table constructors, and variable declarations.

<p><a name="chunks"><h3>2.4.1 - Chunks</h3></a>

<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>

<p>Lua handles a chunk as the body of an anonymous function (see <a href="#func-def">2.5.8</a>).
As such, chunks can define local variables 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 opcodes for
a virtual machine,
and then the compiled code is executed
by 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="blocks"><h3>2.4.2 - Blocks</h3></a>
A block is a list of statements;
syntactically, a block is equal to 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 useful
to control the scope of variable declarations.
Explicit blocks are also sometimes used to
add a <b>return</b> or <b>break</b> statement in the middle
of another block (see <a href="#control">2.4.4</a>).


<p><a name="assignment"><h3>2.4.3 - Assignment</h3></a>

<p>Lua allows multiple assignment.
Therefore, the syntax for assignment
defines a list of variables on the left side
and 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 of
the 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 that function 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 expressions
and 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 variables
and 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"><h3>2.4.4 - Control Structures</h3></a>
The control structures
<b>if</b>, <b>while</b>, and <b>repeat</b> have the usual meaning and
familiar 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 <em>exp</em> of a
control 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>The <b>return</b> statement is used to return values
from a function or from a chunk.

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 can be used to terminate the execution of a
<b>while</b>, <b>repeat</b>, or <b>for</b> loop,
skiping to the next statement after the loop:

<pre>
	stat ::= <b>break</b>
</pre>
A <b>break</b> ends the innermost enclosing loop.

<p>For syntactic reasons, <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 the
middle 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 in
their (inner) blocks.
In practice,
those idioms are only used during debugging.

<p><a name="for"><h3>2.4.5 - For Statement</h3></a>

<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 a
control 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 of
the first <em>exp</em>, until it passes the second <em>exp</em> by steps of the
third <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
           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>_limit</code> and <code>_step</code> are invisible variables.
The names are here for explanatory purposes only.
<li> The behavior is <em>undefined</em> if you assign to <code>var</code> inside
the block.
<li> If the third expression (the step) is absent, then a step of&nbsp;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 statement;
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>.
For each iteration, it calls its iterator function to produce a new value,
stopping when the new value is <B>nil</B>.
The generic <b>for</b> loop has the following syntax:
<pre>
	stat ::= <b>for</b> Name {`<b>,</b>&acute; Name} <b>in</b> explist1 <b>do</b> block <b>end</b>
</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_1 = explist
         local var_2, ... , var_n
         while true do
           var_1, ..., var_n = _f(_s, var_1)
           if var_1 == 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> and <code>_s</code> are invisible variables.
The names are here for explanatory purposes only.
<li> The behavior is <em>undefined</em> if you assign to
<code>var_1</code> inside the block.
<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 statement;
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"><h3>2.4.6 - Function Calls as Statements</h3></a>
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.7</a>.

<p><a name="localvar"><h3>2.4.7 - Local Declarations</h3></a>
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]
	namelist ::= Name {`<b>,</b>&acute; Name}
</pre>
If present, an initial assignment has the same semantics
of 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>),
so local variables can be declared in a chunk outside any explicit block.
Such local variables die when the chunk ends.

<p>The visibility rules for local variables are explained in <a href="#visibility">2.6</a>.

<p><a name="expressions"><h2>2.5 - Expressions</h2></a>

<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 ::= Literal
	exp ::= function
	exp ::= tableconstructor
	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.8</a>;
function calls are explained in <a href="#functioncall">2.5.7</a>;
table constructors are explained in <a href="#tableconstructor">2.5.6</a>.


<p>An expression enclosed in parentheses always results in only one value.
Thus,
<code>(f(x,y,z))</code> is always a single value,
even if <code>f</code> returns several values.
(The value of <code>(f(x,y,z))</code> is the first value returned by <code>f</code>
or <B>nil</B> if <code>f</code> does not return any values.)

<p>Expressions can also be built with arithmetic operators, relational operators,
and logical operators, all of which are explained below.

<p><a name="arith-ops"><h3>2.5.1 - Arithmetic Operators</h3></a>
Lua supports the usual arithmetic operators:
the binary <code>+</code> (addition),
<code>-</code> (subtraction), <code>*</code> (multiplication),
<code>/</code> (division), and <code>^</code> (exponentiation);
and unary <code>-</code> (negation).
If the operands are numbers, or strings that can be converted to
numbers (see <a href="#coercion">2.2.1</a>),
then all operations except exponentiation have the usual meaning.
Exponentiation calls a global function <code>__pow</code>;
otherwise, an appropriate metamethod is called (see <a href="#metatable">2.8</a>).
The standard mathematical library defines function <code>__pow</code>,
giving the expected meaning to exponentiation
(see <a href="#mathlib">5.5</a>).

<p><a name="rel-ops"><h3>2.5.2 - Relational Operators</h3></a>
The relational operators in Lua are
<PRE>
       ==    ~=    &#060;     >     &#060;=    >=
</PRE>
These operators always result in <B>false</B> or <B>true</B>.

<p>Equality (<code>==</code>) first compares the type of its operands.
If the types are different, then the result is <B>false</B>.
Otherwise, the values of the operands are compared.
Numbers and strings are compared in the usual way.
Objects (tables, userdata, threads, and functions)
are compared by <em>reference</em>:
Two objects are considered equal only if they are the <em>same</em> object.
Every time you create a new object (a table, userdata, or function),

⌨️ 快捷键说明

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