diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-08-19 14:10:18 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-08-19 14:10:18 -0300 |
commit | c6cea857a4845940c833e39a149d20bb64a9af85 (patch) | |
tree | 9cae6b73bcd5be5fa7feb01130d903cb4dbf6d4e | |
parent | d61b0c60287c38008d312ddd11724a15b1737f7b (diff) | |
download | lua-c6cea857a4845940c833e39a149d20bb64a9af85.tar.gz lua-c6cea857a4845940c833e39a149d20bb64a9af85.tar.bz2 lua-c6cea857a4845940c833e39a149d20bb64a9af85.zip |
Better documentation for 'multires' expressions
Manual has a new section explaining multires expressions, lists of
expressions, and adjustments. This commit also corrects some comments
in the code.
-rw-r--r-- | lauxlib.c | 5 | ||||
-rw-r--r-- | lfunc.c | 2 | ||||
-rw-r--r-- | manual/manual.of | 188 |
3 files changed, 120 insertions, 75 deletions
@@ -526,7 +526,8 @@ static void newbox (lua_State *L) { | |||
526 | 526 | ||
527 | /* | 527 | /* |
528 | ** Compute new size for buffer 'B', enough to accommodate extra 'sz' | 528 | ** Compute new size for buffer 'B', enough to accommodate extra 'sz' |
529 | ** bytes. | 529 | ** bytes. (The test for "double is not big enough" also gets the |
530 | ** case when the multiplication by 2 overflows.) | ||
530 | */ | 531 | */ |
531 | static size_t newbuffsize (luaL_Buffer *B, size_t sz) { | 532 | static size_t newbuffsize (luaL_Buffer *B, size_t sz) { |
532 | size_t newsize = B->size * 2; /* double buffer size */ | 533 | size_t newsize = B->size * 2; /* double buffer size */ |
@@ -611,7 +612,7 @@ LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) { | |||
611 | ** box (if existent) is not on the top of the stack. So, instead of | 612 | ** box (if existent) is not on the top of the stack. So, instead of |
612 | ** calling 'luaL_addlstring', it replicates the code using -2 as the | 613 | ** calling 'luaL_addlstring', it replicates the code using -2 as the |
613 | ** last argument to 'prepbuffsize', signaling that the box is (or will | 614 | ** last argument to 'prepbuffsize', signaling that the box is (or will |
614 | ** be) bellow the string being added to the buffer. (Box creation can | 615 | ** be) below the string being added to the buffer. (Box creation can |
615 | ** trigger an emergency GC, so we should not remove the string from the | 616 | ** trigger an emergency GC, so we should not remove the string from the |
616 | ** stack before we have the space guaranteed.) | 617 | ** stack before we have the space guaranteed.) |
617 | */ | 618 | */ |
@@ -209,7 +209,7 @@ void luaF_closeupval (lua_State *L, StkId level) { | |||
209 | 209 | ||
210 | 210 | ||
211 | /* | 211 | /* |
212 | ** Remove firt element from the tbclist plus its dummy nodes. | 212 | ** Remove first element from the tbclist plus its dummy nodes. |
213 | */ | 213 | */ |
214 | static void poptbclist (lua_State *L) { | 214 | static void poptbclist (lua_State *L) { |
215 | StkId tbc = L->tbclist; | 215 | StkId tbc = L->tbclist; |
diff --git a/manual/manual.of b/manual/manual.of index 30f92d60..ca7f9933 100644 --- a/manual/manual.of +++ b/manual/manual.of | |||
@@ -1333,19 +1333,11 @@ Expressions are discussed in @See{expressions}. | |||
1333 | 1333 | ||
1334 | Before the assignment, | 1334 | Before the assignment, |
1335 | the list of values is @emph{adjusted} to the length of | 1335 | the list of values is @emph{adjusted} to the length of |
1336 | the list of variables.@index{adjustment} | 1336 | the list of variables @see{multires}. |
1337 | If there are more values than needed, | ||
1338 | the excess values are thrown away. | ||
1339 | If there are fewer values than needed, | ||
1340 | the list is extended with @nil's. | ||
1341 | If the list of expressions ends with a function call, | ||
1342 | then all values returned by that call enter the list of values, | ||
1343 | before the adjustment | ||
1344 | (except when the call is enclosed in parentheses; see @See{expressions}). | ||
1345 | 1337 | ||
1346 | If a variable is both assigned and read | 1338 | If a variable is both assigned and read |
1347 | inside a multiple assignment, | 1339 | inside a multiple assignment, |
1348 | Lua ensures all reads get the value of the variable | 1340 | Lua ensures that all reads get the value of the variable |
1349 | before the assignment. | 1341 | before the assignment. |
1350 | Thus the code | 1342 | Thus the code |
1351 | @verbatim{ | 1343 | @verbatim{ |
@@ -1684,9 +1676,10 @@ function calls are explained in @See{functioncall}; | |||
1684 | table constructors are explained in @See{tableconstructor}. | 1676 | table constructors are explained in @See{tableconstructor}. |
1685 | Vararg expressions, | 1677 | Vararg expressions, |
1686 | denoted by three dots (@Char{...}), can only be used when | 1678 | denoted by three dots (@Char{...}), can only be used when |
1687 | directly inside a vararg function; | 1679 | directly inside a variadic function; |
1688 | they are explained in @See{func-def}. | 1680 | they are explained in @See{func-def}. |
1689 | 1681 | ||
1682 | |||
1690 | Binary operators comprise arithmetic operators @see{arith}, | 1683 | Binary operators comprise arithmetic operators @see{arith}, |
1691 | bitwise operators @see{bitwise}, | 1684 | bitwise operators @see{bitwise}, |
1692 | relational operators @see{rel-ops}, logical operators @see{logic}, | 1685 | relational operators @see{rel-ops}, logical operators @see{logic}, |
@@ -1696,47 +1689,8 @@ the unary bitwise NOT @see{bitwise}, | |||
1696 | the unary logical @Rw{not} @see{logic}, | 1689 | the unary logical @Rw{not} @see{logic}, |
1697 | and the unary @def{length operator} @see{len-op}. | 1690 | and the unary @def{length operator} @see{len-op}. |
1698 | 1691 | ||
1699 | Both function calls and vararg expressions can result in multiple values. | ||
1700 | If a function call is used as a statement @see{funcstat}, | ||
1701 | then its return list is adjusted to zero elements, | ||
1702 | thus discarding all returned values. | ||
1703 | If an expression is used as the last (or the only) element | ||
1704 | of a list of expressions, | ||
1705 | then no adjustment is made | ||
1706 | (unless the expression is enclosed in parentheses). | ||
1707 | In all other contexts, | ||
1708 | Lua adjusts the result list to one element, | ||
1709 | either discarding all values except the first one | ||
1710 | or adding a single @nil if there are no values. | ||
1711 | |||
1712 | Here are some examples: | ||
1713 | @verbatim{ | ||
1714 | f() -- adjusted to 0 results | ||
1715 | g(f(), x) -- f() is adjusted to 1 result | ||
1716 | g(x, f()) -- g gets x plus all results from f() | ||
1717 | a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil) | ||
1718 | a,b = ... -- a gets the first vararg argument, b gets | ||
1719 | -- the second (both a and b can get nil if there | ||
1720 | -- is no corresponding vararg argument) | ||
1721 | |||
1722 | a,b,c = x, f() -- f() is adjusted to 2 results | ||
1723 | a,b,c = f() -- f() is adjusted to 3 results | ||
1724 | return f() -- returns all results from f() | ||
1725 | return ... -- returns all received vararg arguments | ||
1726 | return x,y,f() -- returns x, y, and all results from f() | ||
1727 | {f()} -- creates a list with all results from f() | ||
1728 | {...} -- creates a list with all vararg arguments | ||
1729 | {f(), nil} -- f() is adjusted to 1 result | ||
1730 | } | 1692 | } |
1731 | 1693 | ||
1732 | Any expression enclosed in parentheses always results in only one value. | ||
1733 | Thus, | ||
1734 | @T{(f(x,y,z))} is always a single value, | ||
1735 | even if @id{f} returns several values. | ||
1736 | (The value of @T{(f(x,y,z))} is the first value returned by @id{f} | ||
1737 | or @nil if @id{f} does not return any values.) | ||
1738 | |||
1739 | } | ||
1740 | 1694 | ||
1741 | 1695 | ||
1742 | @sect3{arith| @title{Arithmetic Operators} | 1696 | @sect3{arith| @title{Arithmetic Operators} |
@@ -1843,8 +1797,9 @@ the library calls the metamethod of the other operand | |||
1843 | (if present) or it raises an error. | 1797 | (if present) or it raises an error. |
1844 | Note that bitwise operators do not do this coercion. | 1798 | Note that bitwise operators do not do this coercion. |
1845 | 1799 | ||
1846 | Nonetheless, it is always a good practice not to rely on these | 1800 | It is always a good practice not to rely on the |
1847 | implicit coercions, as they are not always applied; | 1801 | implicit coercions from strings to numbers, |
1802 | as they are not always applied; | ||
1848 | in particular, @T{"1"==1} is false and @T{"1"<1} raises an error | 1803 | in particular, @T{"1"==1} is false and @T{"1"<1} raises an error |
1849 | @see{rel-ops}. | 1804 | @see{rel-ops}. |
1850 | These coercions exist mainly for compatibility and may be removed | 1805 | These coercions exist mainly for compatibility and may be removed |
@@ -2095,9 +2050,9 @@ The order of the assignments in a constructor is undefined. | |||
2095 | (This order would be relevant only when there are repeated keys.) | 2050 | (This order would be relevant only when there are repeated keys.) |
2096 | 2051 | ||
2097 | If the last field in the list has the form @id{exp} | 2052 | If the last field in the list has the form @id{exp} |
2098 | and the expression is a function call or a vararg expression, | 2053 | and the expression is a multires expression, |
2099 | then all values returned by this expression enter the list consecutively | 2054 | then all values returned by this expression enter the list consecutively |
2100 | @see{functioncall}. | 2055 | @see{multires}. |
2101 | 2056 | ||
2102 | The field list can have an optional trailing separator, | 2057 | The field list can have an optional trailing separator, |
2103 | as a convenience for machine-generated code. | 2058 | as a convenience for machine-generated code. |
@@ -2148,7 +2103,7 @@ A call of the form @T{return @rep{functioncall}} not in the | |||
2148 | scope of a to-be-closed variable is called a @def{tail call}. | 2103 | scope of a to-be-closed variable is called a @def{tail call}. |
2149 | Lua implements @def{proper tail calls} | 2104 | Lua implements @def{proper tail calls} |
2150 | (or @def{proper tail recursion}): | 2105 | (or @def{proper tail recursion}): |
2151 | in a tail call, | 2106 | In a tail call, |
2152 | the called function reuses the stack entry of the calling function. | 2107 | the called function reuses the stack entry of the calling function. |
2153 | Therefore, there is no limit on the number of nested tail calls that | 2108 | Therefore, there is no limit on the number of nested tail calls that |
2154 | a program can execute. | 2109 | a program can execute. |
@@ -2234,22 +2189,16 @@ initialized with the argument values: | |||
2234 | } | 2189 | } |
2235 | When a Lua function is called, | 2190 | When a Lua function is called, |
2236 | it adjusts its list of @x{arguments} to | 2191 | it adjusts its list of @x{arguments} to |
2237 | the length of its list of parameters, | 2192 | the length of its list of parameters @see{multires}, |
2238 | unless the function is a @def{vararg function}, | 2193 | unless the function is a @def{variadic function}, |
2239 | which is indicated by three dots (@Char{...}) | 2194 | which is indicated by three dots (@Char{...}) |
2240 | at the end of its parameter list. | 2195 | at the end of its parameter list. |
2241 | A vararg function does not adjust its argument list; | 2196 | A variadic function does not adjust its argument list; |
2242 | instead, it collects all extra arguments and supplies them | 2197 | instead, it collects all extra arguments and supplies them |
2243 | to the function through a @def{vararg expression}, | 2198 | to the function through a @def{vararg expression}, |
2244 | which is also written as three dots. | 2199 | which is also written as three dots. |
2245 | The value of this expression is a list of all actual extra arguments, | 2200 | The value of this expression is a list of all actual extra arguments, |
2246 | similar to a function with multiple results. | 2201 | similar to a function with multiple results @see{multires}. |
2247 | If a vararg expression is used inside another expression | ||
2248 | or in the middle of a list of expressions, | ||
2249 | then its return list is adjusted to one element. | ||
2250 | If the expression is used as the last element of a list of expressions, | ||
2251 | then no adjustment is made | ||
2252 | (unless that last expression is enclosed in parentheses). | ||
2253 | 2202 | ||
2254 | 2203 | ||
2255 | As an example, consider the following definitions: | 2204 | As an example, consider the following definitions: |
@@ -2299,6 +2248,99 @@ t.a.b.c.f = function (self, @rep{params}) @rep{body} end | |||
2299 | 2248 | ||
2300 | } | 2249 | } |
2301 | 2250 | ||
2251 | @sect3{multires| @title{Lists of expressions, multiple results, | ||
2252 | and adjustment} | ||
2253 | |||
2254 | Both function calls and vararg expressions can result in multiple values. | ||
2255 | These expressions are called @def{multires expressions}. | ||
2256 | |||
2257 | When a multires expression is used as the last element | ||
2258 | of a list of expressions, | ||
2259 | all results from the expression are added to the | ||
2260 | list of values produced by the list of expressions. | ||
2261 | Note that a single expression | ||
2262 | in a place that expects a list of expressions | ||
2263 | is the last expression in that (singleton) list. | ||
2264 | |||
2265 | These are the places where Lua expects a list of expressions: | ||
2266 | @description{ | ||
2267 | |||
2268 | @item{A @rw{return} statement, | ||
2269 | for instance @T{return e1, e2, e3} @see{control}.} | ||
2270 | |||
2271 | @item{A table constructor, | ||
2272 | for instance @T{{e1, e2, e3}} @see{tableconstructor}.} | ||
2273 | |||
2274 | @item{The arguments of a function call, | ||
2275 | for instance @T{foo(e1, e2, e3)} @see{functioncall}.} | ||
2276 | |||
2277 | @item{A multiple assignment, | ||
2278 | for instance @T{a , b, c = e1, e2, e3} @see{assignment}.} | ||
2279 | |||
2280 | @item{A local declaration, | ||
2281 | for instance @T{local a , b, c = e1, e2, e3} @see{localvar}.} | ||
2282 | |||
2283 | @item{The initial values in a generic @rw{for} loop, | ||
2284 | for instance @T{for k in e1, e2, e3 do ... end} @see{for}.} | ||
2285 | |||
2286 | } | ||
2287 | In the last four cases, | ||
2288 | the list of values from the list of expressions | ||
2289 | must be @emph{adjusted} to a specific length: | ||
2290 | the number of parameters in a call to a non-variadic function | ||
2291 | @see{func-def}, | ||
2292 | the number of variables in a multiple assignment or | ||
2293 | a local declaration, | ||
2294 | and exactly four for a generic @rw{for} loop. | ||
2295 | The @def{adjustment} follows these rules: | ||
2296 | If there are more values than needed, | ||
2297 | the extra values are thrown away; | ||
2298 | if there are fewer values than needed, | ||
2299 | the list is extended with @nil's. | ||
2300 | When the list of expressions ends with a multires expression, | ||
2301 | all results from that expression enter the list of values | ||
2302 | before the adjustment. | ||
2303 | |||
2304 | When a multires expression is used | ||
2305 | in a list of expressions without being the last element, | ||
2306 | or in a place where the syntax expects a single expression, | ||
2307 | Lua adjusts the result list of that expression to one element. | ||
2308 | As a particular case, | ||
2309 | the syntax expects a single expression inside a parenthesized expression; | ||
2310 | therefore, adding parentheses around a multires expression | ||
2311 | forces it to produce exactly one result. | ||
2312 | |||
2313 | Here are some examples. | ||
2314 | In all cases, when the construction needs | ||
2315 | @Q{the n-th result} and there is no such result, | ||
2316 | it uses a @nil. | ||
2317 | @verbatim{ | ||
2318 | print(x, f()) -- prints x and all results from f(). | ||
2319 | print(x, (f())) -- prints x and the first result from f(). | ||
2320 | print(f(), x) -- prints the first result from f() and x. | ||
2321 | print(1 + f()) -- prints 1 added to the first result from f(). | ||
2322 | x,y = ... -- x gets the first vararg argument, | ||
2323 | -- y gets the second vararg argument. | ||
2324 | x,y,z = w, f() -- x gets w, y gets the first result from f(), | ||
2325 | -- z gets the second result from f(). | ||
2326 | x,y,z = f() -- x gets the first result from f(), | ||
2327 | -- y gets the second result from f(), | ||
2328 | -- z gets the third result from f(). | ||
2329 | x,y,z = f(), g() -- x gets the first result from f(), | ||
2330 | -- y gets the first result from g(), | ||
2331 | -- z gets the second result from g(). | ||
2332 | x,y,z = (f()) -- x gets the first result from f(), y and z get nil. | ||
2333 | return f() -- returns all results from f(). | ||
2334 | return ... -- returns all received vararg arguments. | ||
2335 | return (...) -- returns the first received vararg argument. | ||
2336 | return x,y,f() -- returns x, y, and all results from f(). | ||
2337 | {f()} -- creates a list with all results from f(). | ||
2338 | {...} -- creates a list with all vararg arguments. | ||
2339 | {f(), 5} -- creates a list with the first result from f() and 5. | ||
2340 | } | ||
2341 | |||
2342 | } | ||
2343 | |||
2302 | } | 2344 | } |
2303 | 2345 | ||
2304 | @sect2{visibility| @title{Visibility Rules} | 2346 | @sect2{visibility| @title{Visibility Rules} |
@@ -4780,7 +4822,7 @@ the number of parameters of the function | |||
4780 | } | 4822 | } |
4781 | 4823 | ||
4782 | @item{@id{isvararg}| | 4824 | @item{@id{isvararg}| |
4783 | true if the function is a vararg function | 4825 | true if the function is a variadic function |
4784 | (always true for @N{C functions}). | 4826 | (always true for @N{C functions}). |
4785 | } | 4827 | } |
4786 | 4828 | ||
@@ -6017,9 +6059,7 @@ to start the traceback. | |||
6017 | 6059 | ||
6018 | } | 6060 | } |
6019 | 6061 | ||
6020 | @APIEntry{const char *luaL_typeerror (lua_State *L, | 6062 | @APIEntry{int luaL_typeerror (lua_State *L, int arg, const char *tname);| |
6021 | int arg, | ||
6022 | const char *tname);| | ||
6023 | @apii{0,0,v} | 6063 | @apii{0,0,v} |
6024 | 6064 | ||
6025 | Raises a type error for the argument @id{arg} | 6065 | Raises a type error for the argument @id{arg} |
@@ -6816,6 +6856,8 @@ When you require a module @id{modname} and | |||
6816 | This variable is only a reference to the real table; | 6856 | This variable is only a reference to the real table; |
6817 | assignments to this variable do not change the | 6857 | assignments to this variable do not change the |
6818 | table used by @Lid{require}. | 6858 | table used by @Lid{require}. |
6859 | The real table is stored in the C registry @see{registry}, | ||
6860 | indexed by the key @defid{LUA_LOADED_TABLE}, a string. | ||
6819 | 6861 | ||
6820 | } | 6862 | } |
6821 | 6863 | ||
@@ -6883,6 +6925,8 @@ A table to store loaders for specific modules | |||
6883 | This variable is only a reference to the real table; | 6925 | This variable is only a reference to the real table; |
6884 | assignments to this variable do not change the | 6926 | assignments to this variable do not change the |
6885 | table used by @Lid{require}. | 6927 | table used by @Lid{require}. |
6928 | The real table is stored in the C registry @see{registry}, | ||
6929 | indexed by the key @defid{LUA_PRELOAD_TABLE}, a string. | ||
6886 | 6930 | ||
6887 | } | 6931 | } |
6888 | 6932 | ||
@@ -7904,9 +7948,9 @@ Returns the arc sine of @id{x} (in radians). | |||
7904 | 7948 | ||
7905 | @LibEntry{math.atan (y [, x])| | 7949 | @LibEntry{math.atan (y [, x])| |
7906 | 7950 | ||
7907 | @index{atan2} | 7951 | @index{atan} @index{atan2} |
7908 | Returns the arc tangent of @T{y/x} (in radians), | 7952 | Returns the arc tangent of @T{y/x} (in radians), |
7909 | but uses the signs of both arguments to find the | 7953 | using the signs of both arguments to find the |
7910 | quadrant of the result. | 7954 | quadrant of the result. |
7911 | It also handles correctly the case of @id{x} being zero. | 7955 | It also handles correctly the case of @id{x} being zero. |
7912 | 7956 | ||
@@ -8997,7 +9041,7 @@ If there is a script, | |||
8997 | the script is called with arguments | 9041 | the script is called with arguments |
8998 | @T{arg[1]}, @Cdots, @T{arg[#arg]}. | 9042 | @T{arg[1]}, @Cdots, @T{arg[#arg]}. |
8999 | Like all chunks in Lua, | 9043 | Like all chunks in Lua, |
9000 | the script is compiled as a vararg function. | 9044 | the script is compiled as a variadic function. |
9001 | 9045 | ||
9002 | In interactive mode, | 9046 | In interactive mode, |
9003 | Lua repeatedly prompts and waits for a line. | 9047 | Lua repeatedly prompts and waits for a line. |