Mercurial Hosting > luan
changeset 417:a40e99cf0b0b
work on manual
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 30 Apr 2015 21:30:32 -0600 |
parents | 91af5337b9ae |
children | 455784e2227d |
files | website/src/manual.html.luan |
diffstat | 1 files changed, 112 insertions(+), 119 deletions(-) [+] |
line wrap: on
line diff
diff -r 91af5337b9ae -r a40e99cf0b0b website/src/manual.html.luan --- a/website/src/manual.html.luan Thu Apr 30 06:28:25 2015 -0600 +++ b/website/src/manual.html.luan Thu Apr 30 21:30:32 2015 -0600 @@ -73,8 +73,11 @@ <li><a href="#length">The Length Operator</a></li> <li><a href="#precedence">Precedence</a></li> <li><a href="#constructors">Table Constructors</a></li> + <li><a href="#fn_calls">Function Calls</a></li> + <li><a href="#fn_def">Function Definitions</a></li> </ul> </li> + <li><a href="#visibility">>Visibility Rules</a></li> </ul> </div> @@ -1262,7 +1265,7 @@ Closures are also compared by reference. <p> -You can change the way that Lua compares tables +You can change the way that Luan compares tables by using the "eq" metamethod (see <a href="#2.4">§2.4</a>). <p> @@ -1476,15 +1479,19 @@ -<h3>3.4.10 – <a name="3.4.10">Function Calls</a></h3><p> -A function call in Lua has the following syntax: - -<pre> +<h4 margin-top="1em"><a name="fn_calls">Function Calls</a></h4> + +<p> +A function call in Luan has the following syntax: + +<p><tt><pre> functioncall ::= prefixexp args -</pre><p> +</pre></tt></p> + +<p> In a function call, first prefixexp and args are evaluated. -If the value of prefixexp has type <em>function</em>, +If the value of prefixexp has type <i>function</i>, then this function is called with the given arguments. Otherwise, the prefixexp "call" metamethod is called, @@ -1494,40 +1501,30 @@ <p> -The form - -<pre> - functioncall ::= prefixexp ‘<b>:</b>’ 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> +<p><tt><pre> args ::= ‘<b>(</b>’ [explist] ‘<b>)</b>’ args ::= tableconstructor args ::= LiteralString -</pre><p> +</pre></tt></p> + +<p> All argument expressions are evaluated before the call. -A call of the form <code>f{<em>fields</em>}</code> is -syntactic sugar for <code>f({<em>fields</em>})</code>; +A call of the form <tt>f{<i>fields</i>}</tt> is +syntactic sugar for <tt>f({<i>fields</i>})</tt>; 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>; +A call of the form <tt>f'<i>string</i>'</tt> +(or <tt>f"<i>string</i>"</tt> or <tt>f[[<i>string</i>]]</tt>) +is syntactic sugar for <tt>f('<i>string</i>')</tt>; that is, the argument list is a single literal string. <p> -A call of the form <code>return <em>functioncall</em></code> is called -a <em>tail call</em>. -Lua implements <em>proper tail calls</em> -(or <em>proper tail recursion</em>): +A call of the form <tt>return <i>functioncall</i></tt> is called +a <i>tail call</i>. +Luan implements <i>proper tail calls</i> +(or <i>proper tail recursion</i>): 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 that @@ -1540,82 +1537,98 @@ the returns of the called function. So, none of the following examples are tail calls: -<pre> +<p><tt><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>3.4.11 – <a name="3.4.11">Function Definitions</a></h3> +</pre></tt></p> + + + + +<h4 margin-top="1em"><a name="fn_def">Function Definitions</a></h4> <p> The syntax for function definition is -<pre> +<p><tt><pre> functiondef ::= <b>function</b> funcbody funcbody ::= ‘<b>(</b>’ [parlist] ‘<b>)</b>’ block <b>end</b> -</pre> +</pre></tt></p> <p> The following syntactic sugar simplifies function definitions: -<pre> +<p><tt><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><p> +</pre></tt></p> + +<p> The statement -<pre> - function f () <em>body</em> end -</pre><p> +<p><tt><pre> + function f () <i>body</i> end +</pre></tt></p> + +<p> translates to -<pre> - f = function () <em>body</em> end -</pre><p> +<p><tt><pre> + f = function () <i>body</i> end +</pre></tt></p> + +<p> The statement -<pre> - function t.a.b.c.f () <em>body</em> end -</pre><p> +<p><tt><pre> + function t.a.b.c.f () <i>body</i> end +</pre></tt></p> + +<p> translates to -<pre> - t.a.b.c.f = function () <em>body</em> end -</pre><p> +<p><tt><pre> + t.a.b.c.f = function () <i>body</i> end +</pre></tt></p> + +<p> The statement -<pre> - local function f () <em>body</em> end -</pre><p> +<p><tt><pre> + local function f () <i>body</i> end +</pre></tt></p> + +<p> translates to -<pre> +<p><tt><pre> local f; f = function () <em>body</em> end -</pre><p> +</pre></tt></p> + +<p> not to -<pre> +<p><tt><pre> local f = function () <em>body</em> end -</pre><p> +</pre></tt></p> + +<p> (This only makes a difference when the body of the function -contains references to <code>f</code>.) +contains references to <tt>f</tt>.) <p> A function definition is an executable expression, -whose value has type <em>function</em>. -When Lua precompiles a chunk, +whose value has type <i>function</i>. +When Luan precompiles a chunk, all its function bodies are precompiled 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>) +Then, whenever Luan executes the function definition, +the function is <i>instantiated</i> (or <i>closed</i>). +This function instance (or <i>closure</i>) is the final value of the expression. @@ -1623,18 +1636,20 @@ Parameters act as local variables that are initialized with the argument values: -<pre> +<p><tt><pre> parlist ::= namelist [‘<b>,</b>’ ‘<b>...</b>’] | ‘<b>...</b>’ -</pre><p> +</pre></tt></p> + +<p> When a function is called, the list of arguments is adjusted to the length of the list of parameters, -unless the function is a <em>vararg function</em>, -which is indicated by three dots ('<code>...</code>') +unless the function is a <i>vararg function</i>, +which is indicated by three dots ('<tt>...</tt>') at the end of its parameter list. A vararg function does not adjust its argument list; instead, it collects all extra arguments and supplies them -to the function through a <em>vararg expression</em>, +to the function through a <i>vararg expression</i>, which is also written as three dots. The value of this expression is a list of all actual extra arguments, similar to a function with multiple results. @@ -1649,15 +1664,17 @@ <p> As an example, consider the following definitions: -<pre> +<p><tt><pre> function f(a, b) end function g(a, b, ...) end function r() return 1,2,3 end -</pre><p> +</pre></tt></p> + +<p> Then, we have the following mapping from arguments to parameters and to the vararg expression: -<pre> +<p><tt><pre> CALL PARAMETERS f(3) a=3, b=nil @@ -1670,7 +1687,7 @@ g(3, 4) a=3, b=4, ... --> (nothing) g(3, 4, 5, 8) a=3, b=4, ... --> 5 8 g(5, r()) a=5, b=1, ... --> 2 3 -</pre> +</pre></tt></p> <p> Results are returned using the <b>return</b> statement (see <a href="#3.3.4">§3.3.4</a>). @@ -1679,44 +1696,18 @@ then the function returns with no results. -<p> - -There is a system-dependent limit on the number of values -that a function may return. -This limit is guaranteed to be larger than 1000. - - -<p> -The <em>colon</em> syntax -is 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 (<em>params</em>) <em>body</em> end -</pre><p> -is syntactic sugar for - -<pre> - t.a.b.c.f = function (self, <em>params</em>) <em>body</em> end -</pre> - - - - - - -<h2>3.5 – <a name="3.5">Visibility Rules</a></h2> - -<p> - -Lua is a lexically scoped language. + + +<h3 margin-top="1em"><a name="visibility">Visibility Rules</a></h3> + +<p> +Luan is a lexically scoped language. The scope of a local variable begins at the first statement after its declaration and lasts until the last non-void statement of the innermost block that includes the declaration. Consider the following example: -<pre> +<p><tt><pre> x = 10 -- global variable do -- new block local x = x -- new 'x', with value 10 @@ -1729,12 +1720,12 @@ print(x) --> 11 end print(x) --> 10 (the global one) -</pre> - -<p> -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. +</pre></tt></p> + +<p> +Notice that, in a declaration like <tt>local x = x</tt>, +the new <tt>x</tt> being declared is not in scope yet, +and so the second <tt>x</tt> refers to the outside variable. <p> @@ -1742,7 +1733,7 @@ local variables can be freely accessed by functions defined inside their scope. A local variable used by an inner function is called -an <em>upvalue</em>, or <em>external local variable</em>, +an <i>upvalue</i>, or <i>external local variable</i>, inside the inner function. @@ -1751,18 +1742,20 @@ defines new local variables. Consider the following example: -<pre> +<p><tt><pre> a = {} local x = 20 for i=1,10 do local y = 0 a[i] = function () y=y+1; return x+y end end -</pre><p> +</pre></tt></p> + +<p> The loop creates ten closures (that is, ten instances of the anonymous function). -Each of these closures uses a different <code>y</code> variable, -while all of them share the same <code>x</code>. +Each of these closures uses a different <tt>y</tt> variable, +while all of them share the same <tt>x</tt>.