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

📄 manual.html

📁 采用C语言写的Lua的解释器的代码!Lua不用介绍了吧
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<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="#2.1">&sect;2.1</a>;variables are explained in <a href="#2.3">&sect;2.3</a>;function definitions are explained in <a href="#2.5.9">&sect;2.5.9</a>;function calls are explained in <a href="#2.5.8">&sect;2.5.8</a>;table constructors are explained in <a href="#2.5.7">&sect;2.5.7</a>.Vararg expressions,denoted by three dots ('<code>...</code>'), can only be used whendirectly inside a vararg function;they are explained in <a href="#2.5.9">&sect;2.5.9</a>.<p>Binary operators comprise arithmetic operators (see <a href="#2.5.1">&sect;2.5.1</a>),relational operators (see <a href="#2.5.2">&sect;2.5.2</a>), logical operators (see <a href="#2.5.3">&sect;2.5.3</a>),and the concatenation operator (see <a href="#2.5.4">&sect;2.5.4</a>).Unary operators comprise the unary minus (see <a href="#2.5.1">&sect;2.5.1</a>),the unary <b>not</b> (see <a href="#2.5.3">&sect;2.5.3</a>),and the unary <em>length operator</em> (see <a href="#2.5.5">&sect;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="#2.4.6">&sect;2.4.6</a>)(only possible for function calls),then its return list is adjusted to zero elements,thus discarding all returned values.If the expression is used as the last (or the only) elementof 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.<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 results from f()     a,b,c = f(), x     -- f() is adjusted to 1 result (c gets nil)     a,b = ...          -- a gets the first vararg parameter, b gets                        -- the second (both a and b may 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</pre><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.)<h3>2.5.1 - <a name="2.5.1">Arithmetic Operators</a></h3><p>Lua supports the usual arithmetic operators:the binary <code>+</code> (addition),<code>-</code> (subtraction), <code>*</code> (multiplication),<code>/</code> (division), <code>%</code> (modulo), and <code>^</code> (exponentiation);and unary <code>-</code> (negation).If the operands are numbers, or strings that can be converted tonumbers (see <a href="#2.2.1">&sect;2.2.1</a>),then all operations have the usual meaning.Exponentiation works for any exponent.For instance, <code>x^(-0.5)</code> computes the inverse of the square root of <code>x</code>.Modulo is defined as<pre>     a % b == a - math.floor(a/b)*b</pre><p>That is, it is the remainder of a division that roundsthe quotient towards minus infinity.<h3>2.5.2 - <a name="2.5.2">Relational Operators</a></h3><p>The relational operators in Lua are<pre>     ==    ~=    &lt;     &gt;     &lt;=    &gt;=</pre><p>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, thread, or function),this new object is different from any previously existing object.<p>You can change the way that Lua compares tables and userdata by using the "eq" metamethod (see <a href="#2.8">&sect;2.8</a>).<p>The conversion rules of <a href="#2.2.1">&sect;2.2.1</a><em>do not</em> apply to equality comparisons.Thus, <code>"0"==0</code> evaluates to <b>false</b>,and <code>t[0]</code> and <code>t["0"]</code> denote differententries in a table.<p>The operator <code>~=</code> is exactly the negation of equality (<code>==</code>).<p>The order operators work as follows.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="#2.8">&sect;2.8</a>).<h3>2.5.3 - <a name="2.5.3">Logical Operators</a></h3><p>The logical operators in Lua are<b>and</b>, <b>or</b>, and <b>not</b>.Like the control structures (see <a href="#2.4.4">&sect;2.4.4</a>),all logical operators consider both <b>false</b> and <b>nil</b> as falseand anything else as true.<p>The negation operator <b>not</b> always returns <b>false</b> or <b>true</b>.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.Here are some examples:<pre>     10 or 20            --&gt; 10     10 or error()       --&gt; 10     nil or "a"          --&gt; "a"     nil and 10          --&gt; nil     false and error()   --&gt; false     false and nil       --&gt; false     false or nil        --&gt; nil     10 and 20           --&gt; 20</pre><p>(In this manual,--> indicates the result of the preceding expression.)<h3>2.5.4 - <a name="2.5.4">Concatenation</a></h3><p>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="#2.2.1">&sect;2.2.1</a>.Otherwise, the "concat" metamethod is called (see <a href="#2.8">&sect;2.8</a>).<h3>2.5.5 - <a name="2.5.5">The Length Operator</a></h3><p>The length operator is denoted by the unary operator <code>#</code>.The length of a string is its number of bytes(that is, the usual meaning of string length when eachcharacter is one byte).<p>The length of a table <code>t</code> is defined to be anyinteger index <code>n</code>such that <code>t[n]</code> is not <b>nil</b> and <code>t[n+1]</code> is <b>nil</b>;moreover, if <code>t[1]</code> is <b>nil</b>, <code>n</code> may be zero.For a regular array, with non-nil values from 1 to a given <code>n</code>,its length is exactly that <code>n</code>,the index of its last value.If the array has "holes"(that is, <b>nil</b> values between other non-nil values),then <code>#t</code> may be any of the indices thatdirectly precedes a <b>nil</b> value(that is, it may consider any such <b>nil</b> value as the end ofthe array). <h3>2.5.6 - <a name="2.5.6">Precedence</a></h3><p>Operator precedence in Lua follows the table below,from lower to higher priority:<pre>     or     and     &lt;     &gt;     &lt;=    &gt;=    ~=    ==     ..     +     -     *     /     %     not   #     - (unary)     ^</pre><p>As usual,you can use parentheses to change the precedences of an expression.The concatenation ('<code>..</code>') and exponentiation ('<code>^</code>')operators are right associative.All other binary operators are left associative.<h3>2.5.7 - <a name="2.5.7">Table Constructors</a></h3><p>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>&acute; [fieldlist] `<b>}</b>&acute;	fieldlist ::= field {fieldsep field} [fieldsep]	field ::= `<b>[</b>&acute; exp `<b>]</b>&acute; `<b>=</b>&acute; exp | Name `<b>=</b>&acute; exp | exp	fieldsep ::= `<b>,</b>&acute; | `<b>;</b>&acute;</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><p>is equivalent to<pre>     do       local t = {}       t[f(1)] = g       t[1] = "x"         -- 1st exp       t[2] = "y"         -- 2nd exp       t.x = 1            -- t["x"] = 1       t[3] = f(x)        -- 3rd exp       t[30] = 23       t[4] = 45          -- 4th exp       a = t     end</pre><p>If the last field in the list has the form <code>exp</code>and the expression is a function call or a vararg expression,then all values returned by this expression enter the list consecutively(see <a href="#2.5.8">&sect;2.5.8</a>).To avoid this,enclose the function call (or the vararg expression)in parentheses (see <a href="#2.5">&sect;2.5</a>).<p>The field list may have an optional trailing separator,as a convenience for machine-generated code.<h3>2.5.8 - <a name="2.5.8">Function Calls</a></h3><p>A function call in Lua has the following syntax:<pre>	functioncall ::= prefixexp args</pre><p>In a function call,first prefixexp and args are evaluated.If the value of prefixexp has type <em>function</em>,then this function is calledwith the given arguments.Otherwise, the prefixexp "call" metamethod is called,having as first parameter the value of prefixexp,followed by the original call arguments(see <a href="#2.8">&sect;2.8</a>).<p>The form<pre>	functioncall ::= prefixexp `<b>:</b>&acute; Name args</pre><p>can be used to call "methods".A call <code>v:name(<em>args</em>)</code>is syntactic sugar for <code>v.name(v,<em>args</em>)</code>,except that <code>v</code> is evaluated only once.<p>Arguments have the following syntax:<pre>	args ::= `<b>(</b>&acute; [explist] `<b>)</b>&acute;	args ::= tableconstructor	args ::= String</pre><p>All argument expressions are evaluated before the call.A call of the form <code>f{<em>fields</em>}</code> issyntactic sugar for <code>f({<em>fields</em>})</code>;that is, the argument list is a single new table.A call of the form <code>f'<em>string</em>'</code>(or <code>f"<em>string</em>"</code> or <code>f[[<em>string</em>]]</code>)is syntactic sugar for <code>f('<em>string</em>')</code>;that is, the argument list is a single literal string.<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.This restriction avoids some ambiguities in the language.If you write<pre>     a = f     (g).x(a)</pre><p>Lua would see that as a single statement, <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 return exactlythe returns of the called function.So, none of the following examples are tail 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><h3>2.5.9 - <a name="2.5.9">Function Definitions</a></h3><p>The syntax for function definition is<pre>	function ::= <b>function</b> funcbody	funcbody ::= `<b>(</b>&acute; [parlist] `<b>)</b>&acute; 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>&acute; Name} [`<b>:</b>&acute; Name]</pre><p>The statement<pre>     function f () <em>body</em> end</pre><p>translates to

⌨️ 快捷键说明

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