Mercurial Hosting > luan
comparison website/src/manual.html.luan @ 474:00646edc9d92
documentation
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 10 May 2015 16:09:45 -0600 |
parents | f4aca5a5346a |
children | 7ac0891718eb |
comparison
equal
deleted
inserted
replaced
473:bab5607a0eed | 474:00646edc9d92 |
---|---|
1935 <p> | 1935 <p> |
1936 Could be defined as: | 1936 Could be defined as: |
1937 | 1937 |
1938 <p><tt><pre> | 1938 <p><tt><pre> |
1939 function Luan.do_file(uri) | 1939 function Luan.do_file(uri) |
1940 return Luan.load_file(uri)() | 1940 return <a href="#Luan.load_file">Luan.load_file</a>(uri)() |
1941 end | 1941 end |
1942 </pre></tt></p> | 1942 </pre></tt></p> |
1943 | 1943 |
1944 | 1944 |
1945 | 1945 |
1957 if the table's metatable has a <tt>"__metatable"</tt> field, | 1957 if the table's metatable has a <tt>"__metatable"</tt> field, |
1958 returns the associated value. | 1958 returns the associated value. |
1959 Otherwise, returns the metatable of the given table. | 1959 Otherwise, returns the metatable of the given table. |
1960 | 1960 |
1961 | 1961 |
1962 | 1962 <h4 margin-top="1em"><a name="Luan.ipairs"><tt>Luan.ipairs (t)</tt></a></h4> |
1963 | 1963 |
1964 <p> | 1964 <p> |
1965 <hr><h3><a name="pdf-ipairs"><code>ipairs (t)</code></a></h3> | 1965 Returns an iterator function |
1966 | |
1967 | |
1968 <p> | |
1969 Returns three values (an iterator function, the table <code>t</code>, and 0) | |
1970 so that the construction | 1966 so that the construction |
1971 | 1967 |
1972 <pre> | 1968 <p><tt><pre> |
1973 for i,v in ipairs(t) do <em>body</em> end | 1969 for i,v in ipairs(t) do <i>body</i> end |
1974 </pre><p> | 1970 </pre></tt></p> |
1971 | |
1972 <p> | |
1975 will iterate over the key–value pairs | 1973 will iterate over the key–value pairs |
1976 (<code>1,t[1]</code>), (<code>2,t[2]</code>), ..., | 1974 (<tt>1,t[1]</tt>), (<tt>2,t[2]</tt>), ..., |
1977 up to the first nil value. | 1975 up to the first nil value. |
1978 | 1976 |
1979 | 1977 <p> |
1980 | 1978 Could be defined as: |
1981 | 1979 |
1982 <p> | 1980 <p><tt><pre> |
1983 <hr><h3><a name="pdf-load"><code>load (chunk [, chunkname [, mode [, env]]])</code></a></h3> | 1981 function Luan.ipairs(t) |
1984 | 1982 local i = 0 |
1983 return function() | |
1984 if i < #t then | |
1985 i = i + 1 | |
1986 return i, t[i] | |
1987 end | |
1988 end | |
1989 end | |
1990 </pre></tt></p> | |
1991 | |
1992 | |
1993 | |
1994 <h4 margin-top="1em"><a name="Luan.load"><tt>Luan.load (text, source_name [env, [, allow_expression]])</tt></a></h4> | |
1985 | 1995 |
1986 <p> | 1996 <p> |
1987 Loads a chunk. | 1997 Loads a chunk. |
1988 | 1998 |
1989 | 1999 <p> |
1990 <p> | 2000 The <tt>text</tt> is compiled. |
1991 If <code>chunk</code> is a string, the chunk is this string. | |
1992 If <code>chunk</code> is a function, | |
1993 <code>load</code> calls it repeatedly to get the chunk pieces. | |
1994 Each call to <code>chunk</code> must return a string that concatenates | |
1995 with previous results. | |
1996 A return of an empty string, <b>nil</b>, or no value signals the end of the chunk. | |
1997 | |
1998 | |
1999 <p> | |
2000 If there are no syntactic errors, | 2001 If there are no syntactic errors, |
2001 returns the compiled chunk as a function; | 2002 returns the compiled chunk as a function; |
2002 otherwise, returns <b>nil</b> plus the error message. | 2003 otherwise, throws an error. |
2003 | 2004 |
2004 | 2005 <p> |
2005 <p> | 2006 The <tt>source_name</tt> parameter is a string saying where the text came from. It is used to produce error messages. |
2006 If the resulting function has upvalues, | 2007 |
2007 the first upvalue is set to the value of <code>env</code>, | 2008 <p> |
2008 if that parameter is given, | 2009 If the <tt>env</tt> parameter is supplied, it becomes the <tt>_ENV</tt> of the chunk. |
2009 or to the value of the global environment. | 2010 |
2010 Other upvalues are initialized with <b>nil</b>. | 2011 <p> |
2011 (When you load a main chunk, | 2012 If the <tt>allow_expression</tt> parameter is <tt>true</tt> then the entire text can be nothing more than an expression in which case the chunk returns the value of this expression. |
2012 the resulting function will always have exactly one upvalue, | 2013 |
2013 the <code>_ENV</code> variable (see <a href="#2.2">§2.2</a>). | 2014 |
2014 However, | 2015 <h4 margin-top="1em"><a name="Luan.load_file"><tt>Luan.load_file (file_uri [, add_extension])</tt></a></h4> |
2015 when you load a binary chunk created from a function (see <a href="#pdf-string.dump"><code>string.dump</code></a>), | 2016 |
2016 the resulting function can have an arbitrary number of upvalues.) | 2017 <p> |
2017 All upvalues are fresh, that is, | 2018 Similar to <a href="#Luan.load"><tt>load</tt></a>, |
2018 they are not shared with any other function. | 2019 but gets the chunk from file <tt>file_uri</tt> |
2019 | |
2020 | |
2021 <p> | |
2022 <code>chunkname</code> is used as the name of the chunk for error messages | |
2023 and debug information (see <a href="#4.9">§4.9</a>). | |
2024 When absent, | |
2025 it defaults to <code>chunk</code>, if <code>chunk</code> is a string, | |
2026 or to "<code>=(load)</code>" otherwise. | |
2027 | |
2028 | |
2029 <p> | |
2030 The string <code>mode</code> controls whether the chunk can be text or binary | |
2031 (that is, a precompiled chunk). | |
2032 It may be the string "<code>b</code>" (only binary chunks), | |
2033 "<code>t</code>" (only text chunks), | |
2034 or "<code>bt</code>" (both binary and text). | |
2035 The default is "<code>bt</code>". | |
2036 | |
2037 | |
2038 <p> | |
2039 Lua does not check the consistency of binary chunks. | |
2040 Maliciously crafted binary chunks can crash | |
2041 the interpreter. | |
2042 | |
2043 | |
2044 | |
2045 | |
2046 <p> | |
2047 <hr><h3><a name="pdf-loadfile"><code>loadfile ([filename [, mode [, env]]])</code></a></h3> | |
2048 | |
2049 | |
2050 <p> | |
2051 Similar to <a href="#pdf-load"><code>load</code></a>, | |
2052 but gets the chunk from file <code>filename</code> | |
2053 or from the standard input, | 2020 or from the standard input, |
2054 if no file name is given. | 2021 if no file uri is given. |
2055 | 2022 |
2056 | 2023 <p> |
2057 | 2024 Could be defined as: |
2058 | 2025 |
2059 <p> | 2026 <p><tt><pre> |
2060 <hr><h3><a name="pdf-next"><code>next (table [, index])</code></a></h3> | 2027 function Luan.load_file(file_uri,add_extension) |
2061 | 2028 file_uri = file_uri or "stdin:" |
2062 | 2029 local f = Io.uri(file_uri,add_extension) |
2063 <p> | 2030 f.exists() or <a href="#Luan.error">Luan.error</a>("file '"..file_uri.."' not found") |
2064 Allows a program to traverse all fields of a table. | 2031 return <a href="#Luan.load">Luan.load</a>( f.read_text(), file_uri ) |
2065 Its first argument is a table and its second argument | 2032 end |
2066 is an index in this table. | 2033 </pre></tt></p> |
2067 <code>next</code> returns the next index of the table | |
2068 and its associated value. | |
2069 When called with <b>nil</b> as its second argument, | |
2070 <code>next</code> returns an initial index | |
2071 and its associated value. | |
2072 When called with the last index, | |
2073 or with <b>nil</b> in an empty table, | |
2074 <code>next</code> returns <b>nil</b>. | |
2075 If the second argument is absent, then it is interpreted as <b>nil</b>. | |
2076 In particular, | |
2077 you can use <code>next(t)</code> to check whether a table is empty. | |
2078 | |
2079 | |
2080 <p> | |
2081 The order in which the indices are enumerated is not specified, | |
2082 <em>even for numeric indices</em>. | |
2083 (To traverse a table in numeric order, | |
2084 use a numerical <b>for</b>.) | |
2085 | |
2086 | |
2087 <p> | |
2088 The behavior of <code>next</code> is undefined if, | |
2089 during the traversal, | |
2090 you assign any value to a non-existent field in the table. | |
2091 You may however modify existing fields. | |
2092 In particular, you may clear existing fields. | |
2093 | 2034 |
2094 | 2035 |
2095 | 2036 |
2096 | 2037 |
2097 <p> | 2038 <p> |