aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-02-05 10:15:07 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-02-05 10:15:07 -0200
commit8fdd06ba3c92c7ca7fbf25748bfa32635e4c003a (patch)
tree7608125651bdcbe991577c206b1796ac1423c05c
parent028ec00ab947e06b99c64dbd721205f1f714e5b8 (diff)
downloadlua-8fdd06ba3c92c7ca7fbf25748bfa32635e4c003a.tar.gz
lua-8fdd06ba3c92c7ca7fbf25748bfa32635e4c003a.tar.bz2
lua-8fdd06ba3c92c7ca7fbf25748bfa32635e4c003a.zip
lots of new stuff from 3.2
-rw-r--r--manual.tex207
1 files changed, 138 insertions, 69 deletions
diff --git a/manual.tex b/manual.tex
index 8e6e4786..20420570 100644
--- a/manual.tex
+++ b/manual.tex
@@ -1,4 +1,4 @@
1% $Id: manual.tex,v 1.20 1998/11/13 16:48:48 roberto Exp roberto $ 1% $Id: manual.tex,v 1.21 1998/11/20 15:41:43 roberto Exp roberto $
2 2
3\documentclass[11pt]{article} 3\documentclass[11pt]{article}
4\usepackage{fullpage,bnf} 4\usepackage{fullpage,bnf}
@@ -41,7 +41,7 @@ Waldemar Celes
41\tecgraf\ --- Computer Science Department --- PUC-Rio 41\tecgraf\ --- Computer Science Department --- PUC-Rio
42} 42}
43 43
44%\date{\small \verb$Date: 1998/11/13 16:48:48 $} 44%\date{\small \verb$Date: 1998/11/20 15:41:43 $}
45 45
46\maketitle 46\maketitle
47 47
@@ -410,11 +410,7 @@ Lua provides some automatic conversions between values at run time.
410Any arithmetic operation applied to a string tries to convert 410Any arithmetic operation applied to a string tries to convert
411that string to a number, following the usual rules. 411that string to a number, following the usual rules.
412Conversely, whenever a number is used when a string is expected, 412Conversely, whenever a number is used when a string is expected,
413that number is converted to a string, according to the following rule: 413that number is converted to a string, in a reasonable format.
414if the number is an integer, it is written without exponent or decimal point;
415otherwise, it is formatted following the \verb|%g|
416conversion specification of the \verb|printf| function in the
417standard C library.
418For complete control on how numbers are converted to strings, 414For complete control on how numbers are converted to strings,
419use the \verb|format| function \see{format}. 415use the \verb|format| function \see{format}.
420 416
@@ -523,9 +519,9 @@ only \nil\ is considered false.
523\begin{Produc} 519\begin{Produc}
524\produc{stat}{\rwd{while} exp1 \rwd{do} block \rwd{end} \OrNL 520\produc{stat}{\rwd{while} exp1 \rwd{do} block \rwd{end} \OrNL
525\rwd{repeat} block \rwd{until} exp1 \OrNL 521\rwd{repeat} block \rwd{until} exp1 \OrNL
526\rwd{if} exp1 \rwd{then} block \rep{elseif} 522\rwd{if} exp1 \rwd{then} block
523 \rep{\rwd{elseif} exp1 \rwd{then} block}
527 \opt{\rwd{else} block} \rwd{end}} 524 \opt{\rwd{else} block} \rwd{end}}
528\produc{elseif}{\rwd{elseif} exp1 \rwd{then} block}
529\end{Produc} 525\end{Produc}
530 526
531A \T{return} is used to return values from a function or from a chunk. 527A \T{return} is used to return values from a function or from a chunk.
@@ -688,6 +684,24 @@ All binary operators are left associative,
688except for \verb|^| (exponentiation), 684except for \verb|^| (exponentiation),
689which is right associative. 685which is right associative.
690 686
687\subsubsection{If Expressions}
688\begin{Produc}
689\produc{exp}{\rwd{if} exp1 \rwd{then} exp1
690 \rep{\rwd{elseif} exp1 \rwd{then} exp1}
691 \opt{\rwd{else} exp1} \rwd{end}}
692\end{Produc}%
693An \Index{if expression} chooses an expression to evaluate
694according to its condition.
695Its final value is the value of the chosen expression.
696An absent else-part is equivalent to \verb|else nil|.
697
698\subsubsection{Assignment Expressions}
699\begin{Produc}
700\produc{exp}{\ter{(} var \ter{=} exp1 \ter{)}}
701\end{Produc}%
702An \Index{assignment expression} executes a regular assignment,
703and results in the final value of its right hand expression.
704
691\subsubsection{Table Constructors} \label{tableconstructor} 705\subsubsection{Table Constructors} \label{tableconstructor}
692Table \Index{constructors} are expressions that create tables; 706Table \Index{constructors} are expressions that create tables;
693every time a constructor is evaluated, a new table is created. 707every time a constructor is evaluated, a new table is created.
@@ -1978,8 +1992,9 @@ field not present in a table or a field with value \nil.
1978Therefore, the function only considers fields with non \nil\ values. 1992Therefore, the function only considers fields with non \nil\ values.
1979The order in which the indices are enumerated is not specified, 1993The order in which the indices are enumerated is not specified,
1980\emph{even for numeric indices} 1994\emph{even for numeric indices}
1981(to traverse a table in numeric order, use a counter). 1995(to traverse a table in numeric order,
1982If the table is modified in any way during a traversal, 1996use a counter or function \verb|foreachi|).
1997If the table indices are modified in any way during a traversal,
1983the semantics of \verb|next| is undefined. 1998the semantics of \verb|next| is undefined.
1984 1999
1985This function cannot be written with the standard API. 2000This function cannot be written with the standard API.
@@ -1992,7 +2007,7 @@ or \nil\ to get a first name.
1992Similarly to \verb|next|, it returns the name of another variable 2007Similarly to \verb|next|, it returns the name of another variable
1993and its value, 2008and its value,
1994or \nil\ if there are no more variables. 2009or \nil\ if there are no more variables.
1995There can be no assignments to global variables during the traversal; 2010There can be no creation of new global variables during the traversal;
1996otherwise the semantics of \verb|nextvar| is undefined. 2011otherwise the semantics of \verb|nextvar| is undefined.
1997 2012
1998This function cannot be written with the standard API. 2013This function cannot be written with the standard API.
@@ -2150,14 +2165,13 @@ value in the table.
2150This function could be defined in Lua: 2165This function could be defined in Lua:
2151\begin{verbatim} 2166\begin{verbatim}
2152 function getn (t) 2167 function getn (t)
2153 if type(t.n) == 'number' then return floor(t.n) end 2168 if type(t.n) == 'number' then return t.n end
2154 local i = next(t, nil) 2169 local i = nil
2155 local max = 0 2170 local max = 0
2156 while i do 2171 while (i=next(t, i)) do
2157 if type(i) == 'number' and i>max then max=i end 2172 if type(i) == 'number' and i>max then max=i end
2158 i = next(t, i)
2159 end 2173 end
2160 return floor(max) 2174 return max
2161 end 2175 end
2162\end{verbatim} 2176\end{verbatim}
2163 2177
@@ -2198,11 +2212,10 @@ as the final value of \verb|foreachi|.
2198This function could be defined in Lua: 2212This function could be defined in Lua:
2199\begin{verbatim} 2213\begin{verbatim}
2200 function foreachi (t, f) 2214 function foreachi (t, f)
2201 local i, n = 1, getn(t) 2215 local i, n = 0, getn(t)
2202 while i<=n do 2216 while (i=i+1)<=n do
2203 local res = f(i, t[i]) 2217 local res = f(i, t[i])
2204 if res then return res end 2218 if res then return res end
2205 i = i+1
2206 end 2219 end
2207 end 2220 end
2208\end{verbatim} 2221\end{verbatim}
@@ -2227,50 +2240,80 @@ This function could be defined in Lua:
2227 end 2240 end
2228\end{verbatim} 2241\end{verbatim}
2229 2242
2243\subsubsection*{\ff \T{tinsert (table [, pos] , value)}}\Deffunc{tinsert}
2244
2245Inserts element \verb|value| at table position \verb|pos|,
2246shifting other elements to open space.
2247The default value for \verb|pos| is \verb|n+1|
2248(where \verb|n| is the result of \verb|getn(table)| \see{getn})
2249so that a call \verb|tinsert(t,x)| inserts \verb|x| at the end
2250of table \verb|t|.
2251
2252This function also sets or increments the field \verb|n| of the table,
2253to \verb|n+1|.
2254
2255This function is equivalent to the following Lua function,
2256except that the table accesses are all raw (that is, without tag methods):
2257\begin{verbatim}
2258 function tinsert (t, ...)
2259 local pos, value
2260 local n = getn(t)
2261 if arg.n == 1 then
2262 pos = n+1; value = arg[1]
2263 else
2264 pos = arg[1]; value = arg[2]
2265 end
2266 t.n = n+1; n=n+1
2267 while (n=n-1)>=pos do
2268 t[n+1] = t[n]
2269 end
2270 t[pos] = value
2271 end
2272\end{verbatim}
2273
2274\subsubsection*{\ff \T{tremove (table [, pos])}}\Deffunc{tremove}
2275
2276Removes from \verb|table| the element at position \verb|pos|,
2277shifting other elements to close the space.
2278Returns the value of the removed element.
2279The default value for \verb|pos| is \verb|n|
2280(where \verb|n| is the result of \verb|getn(table)| \see{getn}),
2281so that a call \verb|tremove(t)| removes the last element
2282of table \verb|t|.
2283
2284This function also sets or decrements the field \verb|n| of the table,
2285to \verb|n-1|.
2286
2287This function is equivalent to the following Lua function,
2288except that the table accesses are all raw (that is, without tag methods):
2289\begin{verbatim}
2290 function tremove (t, pos)
2291 local n = getn(t)
2292 pos = pos or n
2293 local value = t[pos]
2294 if n<=0 then return end
2295 t.n = n-1
2296 pos = pos-1
2297 while (pos=pos+1)<n do
2298 t[pos] = t[pos+1]
2299 end
2300 return value
2301 end
2302\end{verbatim}
2303
2230\subsubsection*{\ff \T{sort (table [, comp])}}\Deffunc{sort} 2304\subsubsection*{\ff \T{sort (table [, comp])}}\Deffunc{sort}
2231Sorts table elements in ascending order, \emph{in-place}, 2305Sorts table elements in a given order, \emph{in-place},
2232from \verb|table[1]| to \verb|table[n]|, 2306from \verb|table[1]| to \verb|table[n]|,
2233where \verb|n| is the result of \verb|getn(table)| \see{getn}. 2307where \verb|n| is the result of \verb|getn(table)| \see{getn}.
2234If \verb|comp| is given, 2308If \verb|comp| is given,
2235it must be a function that compares two table elements, 2309it must be a function that receives two table elements,
2236and returns true when the first is less than the second 2310and returns true when the first is less than the second
2237(that is, \verb|not comp(a[i+1], a[i])| will be true after the sort). 2311(so that \verb|not comp(a[i+1], a[i])| will be true after the sort).
2238If \verb|comp| is not given, 2312If \verb|comp| is not given,
2239the standard \verb|<| Lua operator is used instead. 2313the standard \verb|<| Lua operator is used instead.
2240 2314
2241Function \verb|sort| returns the (sorted) table. 2315Function \verb|sort| returns the (sorted) table.
2242 2316
2243This function could be defined in Lua:
2244\begin{verbatim}
2245 function aux_qsort (a, l, u, leq)
2246 if l < u then
2247 local m = floor((l+u)/2) -- choose middle element as pivot
2248 a[l], a[m] = a[m], a[l] -- swap pivot to first position
2249 local t = a[l] -- pivot value
2250 m = l
2251 local i = l+1
2252 while i <= u do
2253 -- invariant: a[l+1..m] < t <= a[m+1..i-1]
2254 if leq(a[i], t) then
2255 m = m+1
2256 a[m], a[i] = a[i], a[m] -- swap
2257 end
2258 i = i+1
2259 end
2260 a[l], a[m] = a[m], a[l] -- swap pivot to a valid place
2261 -- a[l+1..m-1] < a[m] <= a[m+1..u]
2262 aux_qsort(a, l, m-1, leq)
2263 aux_qsort(a, m+1, u, leq)
2264 end
2265 return a -- return the table
2266 end
2267
2268 function sort (a, f)
2269 f = f or function (a,b) return a<b end
2270 return aux_qsort(a, 1, getn(a), f)
2271 end
2272\end{verbatim}
2273
2274 2317
2275\subsection{String Manipulation} 2318\subsection{String Manipulation}
2276This library provides generic functions for string manipulation, 2319This library provides generic functions for string manipulation,
@@ -2364,9 +2407,8 @@ The only differences are that the options/modifiers
2364and \verb|h| are not supported, 2407and \verb|h| are not supported,
2365and there is an extra option, \verb|q|. 2408and there is an extra option, \verb|q|.
2366This option formats a string in a form suitable to be safely read 2409This option formats a string in a form suitable to be safely read
2367back by the Lua interpreter; 2410back by the Lua interpreter:
2368that is, 2411The string is written between double quotes,
2369the string is written between double quotes,
2370and all double quotes, returns and backslashes in the string 2412and all double quotes, returns and backslashes in the string
2371are correctly escaped when written. 2413are correctly escaped when written.
2372For instance, the call 2414For instance, the call
@@ -2393,7 +2435,7 @@ The options \verb|c|, \verb|d|, \verb|E|, \verb|e|, \verb|f|,
2393\verb|g|, \verb|G|, \verb|i|, \verb|o|, \verb|u|, \verb|X|, and \verb|x| all 2435\verb|g|, \verb|G|, \verb|i|, \verb|o|, \verb|u|, \verb|X|, and \verb|x| all
2394expect a number as argument, 2436expect a number as argument,
2395whereas \verb|q| and \verb|s| expect a string. 2437whereas \verb|q| and \verb|s| expect a string.
2396Note that the \verb|*| modifier can be simulated by building 2438The \verb|*| modifier can be simulated by building
2397the appropriate format string. 2439the appropriate format string.
2398For example, \verb|"%*g"| can be simulated with 2440For example, \verb|"%*g"| can be simulated with
2399\verb|"%"..width.."g"|. 2441\verb|"%"..width.."g"|.
@@ -2464,17 +2506,15 @@ The following combinations are allowed in describing a character class:
2464--- represents the character \emph{x} itself. 2506--- represents the character \emph{x} itself.
2465\item[\T{.}] --- (a dot) represents all characters. 2507\item[\T{.}] --- (a dot) represents all characters.
2466\item[\T{\%a}] --- represents all letters. 2508\item[\T{\%a}] --- represents all letters.
2467\item[\T{\%A}] --- represents all non letter characters. 2509\item[\T{\%c}] --- represents all control characters.
2468\item[\T{\%d}] --- represents all digits. 2510\item[\T{\%d}] --- represents all digits.
2469\item[\T{\%D}] --- represents all non digits.
2470\item[\T{\%l}] --- represents all lower case letters. 2511\item[\T{\%l}] --- represents all lower case letters.
2471\item[\T{\%L}] --- represents all non lower case letter characters. 2512\item[\T{\%p}] --- represents all punctuation characters.
2472\item[\T{\%s}] --- represents all space characters. 2513\item[\T{\%s}] --- represents all space characters.
2473\item[\T{\%S}] --- represents all non space characters.
2474\item[\T{\%u}] --- represents all upper case letters. 2514\item[\T{\%u}] --- represents all upper case letters.
2475\item[\T{\%U}] --- represents all non upper case letter characters.
2476\item[\T{\%w}] --- represents all alphanumeric characters. 2515\item[\T{\%w}] --- represents all alphanumeric characters.
2477\item[\T{\%W}] --- represents all non alphanumeric characters. 2516\item[\T{\%x}] --- represents all hexa-decimal digits.
2517\item[\T{\%z}] --- represents the character with representation 0.
2478\item[\T{\%\M{x}}] (where \M{x} is any non alphanumeric character) --- 2518\item[\T{\%\M{x}}] (where \M{x} is any non alphanumeric character) ---
2479represents the character \M{x}. 2519represents the character \M{x}.
2480This is the standard way to escape the magic characters \verb|()%.[*-?|. 2520This is the standard way to escape the magic characters \verb|()%.[*-?|.
@@ -2495,6 +2535,9 @@ E.g., assuming an \emph{ascii} character set,
2495represents the complement of char-set, 2535represents the complement of char-set,
2496where char-set is interpreted as above. 2536where char-set is interpreted as above.
2497\end{description} 2537\end{description}
2538For all classes represented by single letters (\verb|%a|, \verb|%c|, \ldots),
2539the correspondent upper-case letter represents the complement of the class.
2540For instance, \verb|%S| represents all non-space characters.
2498 2541
2499The definitions of letter, space, etc. depend on the current locale. 2542The definitions of letter, space, etc. depend on the current locale.
2500In particular, the class \verb|[a-z]| may not be equivalent to \verb|%l|. 2543In particular, the class \verb|[a-z]| may not be equivalent to \verb|%l|.
@@ -2591,6 +2634,8 @@ The function \verb|random|, when called without arguments,
2591returns a pseudo-random real number in the range \Math{[0,1)}. 2634returns a pseudo-random real number in the range \Math{[0,1)}.
2592When called with a number \Math{n}, 2635When called with a number \Math{n},
2593\verb|random| returns a pseudo-random integer in the range \Math{[1,n]}. 2636\verb|random| returns a pseudo-random integer in the range \Math{[1,n]}.
2637When called with two arguments, \Math{l} and \Math{u},
2638\verb|random| returns a pseudo-random integer in the range \Math{[l,u]}.
2594 2639
2595 2640
2596\subsection{I/O Facilities} \label{libio} 2641\subsection{I/O Facilities} \label{libio}
@@ -2891,10 +2936,18 @@ then \verb|linedefined| is \Math{-1}, and \verb|filename| is \verb|"(C)"|.
2891The function \verb|lua_currentline| gives the current line where 2936The function \verb|lua_currentline| gives the current line where
2892a given function is executing. 2937a given function is executing.
2893It only works if the function has been compiled with debug 2938It only works if the function has been compiled with debug
2894information \see{pragma}. 2939information.
2895When no line information is available, 2940When no line information is available,
2896\verb|lua_currentline| returns \Math{-1}. 2941\verb|lua_currentline| returns \Math{-1}.
2897 2942
2943The generation of debug information is controled by an internal flag,
2944which can be switched with
2945\begin{verbatim}
2946int lua_setdebug (int debug);
2947\end{verbatim}
2948This function sets the flag and returns its previous value.
2949This flag can also be set from Lua~\see{pragma}.
2950
2898Function \verb|lua_getobjname| tries to find a reasonable name for 2951Function \verb|lua_getobjname| tries to find a reasonable name for
2899a given function. 2952a given function.
2900Because functions in Lua are first class values, 2953Because functions in Lua are first class values,
@@ -2918,6 +2971,8 @@ The following functions allow the manipulation of the
2918local variables of a given activation record. 2971local variables of a given activation record.
2919They only work if the function has been compiled with debug 2972They only work if the function has been compiled with debug
2920information \see{pragma}. 2973information \see{pragma}.
2974Moreover, for these functions, a local variable becomes
2975visible in the line after its definition.
2921\begin{verbatim} 2976\begin{verbatim}
2922lua_Object lua_getlocal (lua_Function func, int local_number, char **name); 2977lua_Object lua_getlocal (lua_Function func, int local_number, char **name);
2923int lua_setlocal (lua_Function func, int local_number); 2978int lua_setlocal (lua_Function func, int local_number);
@@ -2947,17 +3002,17 @@ then this function fails and returns 0.
2947The Lua interpreter offers two hooks for debugging purposes: 3002The Lua interpreter offers two hooks for debugging purposes:
2948\begin{verbatim} 3003\begin{verbatim}
2949typedef void (*lua_CHFunction) (lua_Function func, char *file, int line); 3004typedef void (*lua_CHFunction) (lua_Function func, char *file, int line);
2950extern lua_CHFunction lua_callhook; 3005lua_CHFunction lua_setcallhook (lua_CHFunction func);
2951 3006
2952typedef void (*lua_LHFunction) (int line); 3007typedef void (*lua_LHFunction) (int line);
2953extern lua_LHFunction lua_linehook; 3008lua_LHFunction lua_setlinehook (lua_LHFunction func);
2954\end{verbatim} 3009\end{verbatim}
2955The first one is called whenever the interpreter enters or leaves a 3010The first one is called whenever the interpreter enters or leaves a
2956function. 3011function.
2957When entering a function, 3012When entering a function,
2958its parameters are a handle to the function activation record, 3013its parameters are a handle to the function activation record,
2959plus the file and the line where the function is defined (the same 3014plus the file and the line where the function is defined
2960information which is provided by \verb|lua_funcinfo|); 3015(the same information which is provided by \verb|lua_funcinfo|);
2961when leaving a function, \verb|func| is \verb|LUA_NOOBJECT|, 3016when leaving a function, \verb|func| is \verb|LUA_NOOBJECT|,
2962\verb|file| is \verb|"(return)"|, and \verb|line| is 0. 3017\verb|file| is \verb|"(return)"|, and \verb|line| is 0.
2963 3018
@@ -2971,6 +3026,8 @@ has been compiled with debug information \see{pragma}.
2971 3026
2972A hook is disabled when its value is \verb|NULL|, 3027A hook is disabled when its value is \verb|NULL|,
2973which is the initial value of both hooks. 3028which is the initial value of both hooks.
3029Both \verb|lua_setcallhook| and \verb|lua_setlinehook|
3030set their corresponding hooks and return their previous values.
2974 3031
2975 3032
2976 3033
@@ -3040,6 +3097,18 @@ the previous public versions of Lua,
3040some differences had to be introduced. 3097some differences had to be introduced.
3041Here is a list of all these incompatibilities. 3098Here is a list of all these incompatibilities.
3042 3099
3100\subsection*{Incompatibilities with \Index{version 3.1}}
3101\begin{itemize}
3102\item
3103In the debug API, the old variables \verb|lua_debug|,
3104\verb|lua_callhook| and \verb|lua_linehook| now live inside \verb|lua_state|.
3105Therefore, they are no more directly accessible, and must be
3106manipulated through the new functions \verb|lua_setdebug|,
3107\verb|lua_setcallhook| and \verb|lua_setlinehook|.
3108
3109\item Old pre-compiled code is obsolete, and must be re-compiled.
3110\end{itemize}
3111
3043\subsection*{Incompatibilities with \Index{version 3.0}} 3112\subsection*{Incompatibilities with \Index{version 3.0}}
3044\begin{itemize} 3113\begin{itemize}
3045 3114