aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-06-18 17:14:52 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-06-18 17:14:52 -0300
commit6b78040840bbd457cc325f7a31bb5fbaf549f23f (patch)
tree3bf0bf4c7d52e8bf2559cdda15b98b472520c0b1
parente931c7c0f605f140fa60d0b676fe7caf12a6f410 (diff)
downloadlua-6b78040840bbd457cc325f7a31bb5fbaf549f23f.tar.gz
lua-6b78040840bbd457cc325f7a31bb5fbaf549f23f.tar.bz2
lua-6b78040840bbd457cc325f7a31bb5fbaf549f23f.zip
lhf corrections + new gsub
-rw-r--r--manual.tex474
1 files changed, 254 insertions, 220 deletions
diff --git a/manual.tex b/manual.tex
index 8c63f576..43ce4f3d 100644
--- a/manual.tex
+++ b/manual.tex
@@ -1,4 +1,4 @@
1% $Id: manual.tex,v 1.30 1997/06/09 18:16:33 roberto Exp roberto $ 1% $Id: manual.tex,v 2.0 1997/06/17 18:45:16 roberto Exp roberto $
2 2
3\documentstyle[fullpage,11pt,bnf]{article} 3\documentstyle[fullpage,11pt,bnf]{article}
4 4
@@ -31,13 +31,13 @@ Luiz Henrique de Figueiredo\quad
31Waldemar Celes 31Waldemar Celes
32\vspace{1.0ex}\\ 32\vspace{1.0ex}\\
33\smallskip 33\smallskip
34\small\tt lua@icad.puc-rio.br 34\small\tt lua@tecgraf.puc-rio.br
35\vspace{2.0ex}\\ 35\vspace{2.0ex}\\
36%MCC 08/95 --- 36%MCC 08/95 ---
37\tecgraf\ --- Departamento de Inform\'atica --- PUC-Rio 37\tecgraf\ --- Computer Science Department --- PUC-Rio
38} 38}
39 39
40\date{\small \verb$Date: 1997/06/09 18:16:33 $} 40\date{\small \verb$Date: 1997/06/17 18:45:16 $}
41 41
42\maketitle 42\maketitle
43 43
@@ -71,8 +71,10 @@ a intera\c{c}\~ao entre programas Lua e programas C hospedeiros.
71\begin{quotation} 71\begin{quotation}
72\noindent 72\noindent
73\footnotesize 73\footnotesize
74Copyright (c) 1994--1997 TeCGraf, PUC-Rio. Written by Waldemar Celes Filho, 74Copyright \copyright\ 1994--1997 TeCGraf, PUC-Rio.
75Roberto Ierusalimschy, Luiz Henrique de Figueiredo. All rights reserved. 75Written by Waldemar Celes Filho,
76Roberto Ierusalimschy, Luiz Henrique de Figueiredo.
77All rights reserved.
76% 78%
77Permission is hereby granted, without written agreement and without license or 79Permission is hereby granted, without written agreement and without license or
78royalty fees, to use, copy, modify, and distribute this software and its 80royalty fees, to use, copy, modify, and distribute this software and its
@@ -81,17 +83,20 @@ documentation for any purpose, subject to the following conditions:
81The above copyright notice and this permission notice shall appear in all 83The above copyright notice and this permission notice shall appear in all
82copies or substantial portions of this software. 84copies or substantial portions of this software.
83% 85%
84The name "Lua" cannot be used for any modified form of this software that does 86The name ``Lua'' cannot be used for any modified form
85not originate from the authors. Nevertheless, the name "Lua" may and should be 87of this software that does not originate from the authors.
88Nevertheless, the name ``Lua'' may and should be
86used to designate the language implemented and described in this package, 89used to designate the language implemented and described in this package,
87even if embedded in any other system, as long as its syntax and semantics 90even if embedded in any other system, as long as its syntax and semantics
88remain unchanged. 91remain unchanged.
89% 92%
90The authors specifically disclaim any warranties, including, but not limited 93The authors specifically disclaim any warranties, including,
91to, the implied warranties of merchantability and fitness for a particular 94but not limited to, the implied warranties of merchantability
92purpose. The software provided hereunder is on an "as is" basis, and the 95and fitness for a particular purpose.
96The software provided hereunder is on an ``as is'' basis, and the
93authors have no obligation to provide maintenance, support, updates, 97authors have no obligation to provide maintenance, support, updates,
94enhancements, or modifications. In no event shall TeCGraf, PUC-Rio, or the 98enhancements, or modifications.
99In no event shall TeCGraf, PUC-Rio, or the
95authors be liable to any party for direct, indirect, special, incidental, or 100authors be liable to any party for direct, indirect, special, incidental, or
96consequential damages arising out of the use of this software and its 101consequential damages arising out of the use of this software and its
97documentation. 102documentation.
@@ -110,7 +115,7 @@ documentation.
110\section{Introduction} 115\section{Introduction}
111 116
112Lua is an extension programming language designed to support 117Lua is an extension programming language designed to support
113general procedural programming features with data description 118general procedural programming with data description
114facilities. 119facilities.
115It is intended to be used as a light-weight, but powerful, 120It is intended to be used as a light-weight, but powerful,
116configuration language for any program that needs one. 121configuration language for any program that needs one.
@@ -127,11 +132,12 @@ This host program can invoke functions to execute a piece of
127code in Lua, can write and read Lua variables, 132code in Lua, can write and read Lua variables,
128and can register C functions to be called by Lua code. 133and can register C functions to be called by Lua code.
129Through the use of C functions, Lua can be augmented to cope with 134Through the use of C functions, Lua can be augmented to cope with
130many, completely different domains, 135a wide range of different domains,
131thus creating customized programming languages sharing a syntactical framework. 136thus creating customized programming languages sharing a syntactical framework.
132 137
133Lua is free-distribution software, 138Lua is free-distribution software,
134and provided as usual with no guarantees. 139and provided as usual with no guarantees,
140as stated in the copyright notice in the front page of this manual.
135The implementation described in this manual is available 141The implementation described in this manual is available
136at the following URL's: 142at the following URL's:
137\begin{verbatim} 143\begin{verbatim}
@@ -155,7 +161,8 @@ using functions in the library that implements Lua.
155\Index{Global variables} do not need declaration. 161\Index{Global variables} do not need declaration.
156Any variable is assumed to be global unless explicitly declared local 162Any variable is assumed to be global unless explicitly declared local
157\see{localvar}. 163\see{localvar}.
158Before the first assignment, the value of a global variable is \nil. 164Before the first assignment, the value of a global variable is \nil;
165this default can be changed \see{tag-method}.
159 166
160The unit of execution of Lua is called a \Def{chunk}. 167The unit of execution of Lua is called a \Def{chunk}.
161The syntax% 168The syntax%
@@ -178,7 +185,8 @@ of new functions%
178\footnote{Actually, a function definition is an 185\footnote{Actually, a function definition is an
179assignment to a global variable \see{TypesSec}.}. 186assignment to a global variable \see{TypesSec}.}.
180 187
181Chunks may be pre-compiled; see program \IndexVerb{luac} for details. 188Chunks may be pre-compiled into binary form;
189see program \IndexVerb{luac} for details.
182Text files with chunks and their binary pre-compiled forms 190Text files with chunks and their binary pre-compiled forms
183are interchangeable. 191are interchangeable.
184Lua automatically detects the file type and acts accordingly. 192Lua automatically detects the file type and acts accordingly.
@@ -196,10 +204,10 @@ There are six \Index{basic types} in Lua: \Def{nil}, \Def{number},
196\Def{string}, \Def{function}, \Def{userdata}, and \Def{table}. 204\Def{string}, \Def{function}, \Def{userdata}, and \Def{table}.
197{\em Nil\/} is the type of the value \nil, 205{\em Nil\/} is the type of the value \nil,
198whose main property is to be different from any other value. 206whose main property is to be different from any other value.
199{\em Number\/} represents real (floating point) numbers, 207{\em Number\/} represents real (floating-point) numbers,
200while {\em string\/} has the usual meaning. 208while {\em string\/} has the usual meaning.
201The function \verb|type| returns a string describing the type 209The function \verb|type| returns a string describing the type
202of a given value. 210of a given value \see{pdf-type}.
203 211
204Functions are considered first-class values in Lua. 212Functions are considered first-class values in Lua.
205This means that functions can be stored in variables, 213This means that functions can be stored in variables,
@@ -210,7 +218,8 @@ Lua can call (and manipulate) functions written in Lua and
210functions written in C. 218functions written in C.
211They can be distinguished by their tags: 219They can be distinguished by their tags:
212all Lua functions have the same tag, 220all Lua functions have the same tag,
213which is different from the tag for all C functions. 221and all C functions have the same tag,
222which is different from the tag of a Lua function.
214 223
215The type {\em userdata\/} is provided to allow 224The type {\em userdata\/} is provided to allow
216arbitrary \Index{C pointers} to be stored in Lua variables. 225arbitrary \Index{C pointers} to be stored in Lua variables.
@@ -233,7 +242,7 @@ Because functions are first class values,
233table fields may contain functions. 242table fields may contain functions.
234The form \verb|t:f(x)| is syntactic sugar for \verb|t.f(t,x)|, 243The form \verb|t:f(x)| is syntactic sugar for \verb|t.f(t,x)|,
235which calls the method \verb|f| from the table \verb|t| passing 244which calls the method \verb|f| from the table \verb|t| passing
236itself as the first parameter. 245itself as the first parameter \see{func-def}.
237 246
238It is important to notice that tables are {\em objects}, and not values. 247It is important to notice that tables are {\em objects}, and not values.
239Variables cannot contain tables, only {\em references\/} to them. 248Variables cannot contain tables, only {\em references\/} to them.
@@ -244,10 +253,10 @@ Moreover, tables must be explicitly created before used
244 253
245Tags are mainly used to select tag methods when 254Tags are mainly used to select tag methods when
246some events occur \see{tag-method}. 255some events occur \see{tag-method}.
247Each of the types nil, number and string have a different tag. 256Each of the types nil, number and string has a different tag.
248All values of each of these types have this same pre-defined tag. 257All values of each of these types have this same pre-defined tag.
249Values of type function may have two different tags, 258Values of type function can have two different tags,
250depending whether they are Lua or C functions. 259depending on whether they are Lua or C functions.
251Finally, 260Finally,
252values of type userdata and table can have 261values of type userdata and table can have
253as many different tags as needed \see{tag-method}. 262as many different tags as needed \see{tag-method}.
@@ -264,7 +273,7 @@ This section describes the lexis, the syntax and the semantics of Lua.
264 273
265\subsection{Lexical Conventions} \label{lexical} 274\subsection{Lexical Conventions} \label{lexical}
266 275
267Lua is a case sensitive language. 276Lua is a case-sensitive language.
268\Index{Identifiers} can be any string of letters, digits, and underscores, 277\Index{Identifiers} can be any string of letters, digits, and underscores,
269not beginning with a digit. 278not beginning with a digit.
270The following words are reserved, and cannot be used as identifiers: 279The following words are reserved, and cannot be used as identifiers:
@@ -295,8 +304,8 @@ other quoted strings.
295 304
296\Index{Comments} start anywhere outside a string with a 305\Index{Comments} start anywhere outside a string with a
297double hyphen (\verb|--|) and run until the end of the line. 306double hyphen (\verb|--|) and run until the end of the line.
298Moreover, if the first line of a chunk file starts with \verb|#|, 307Moreover,
299this line is skipped% 308the first line of a chunk file is skipped if it starts with \verb|#|%
300\footnote{This facility allows the use of Lua as a script interpreter 309\footnote{This facility allows the use of Lua as a script interpreter
301in Unix systems \see{lua-sa}.}. 310in Unix systems \see{lua-sa}.}.
302 311
@@ -314,10 +323,10 @@ The \verb|$| can be followed by any of the following directives:
314\begin{description} 323\begin{description}
315\item[{\tt debug}] --- turn on some debugging facilities \see{pragma}. 324\item[{\tt debug}] --- turn on some debugging facilities \see{pragma}.
316\item[{\tt nodebug}] --- turn off some debugging facilities \see{pragma}. 325\item[{\tt nodebug}] --- turn off some debugging facilities \see{pragma}.
317\item[{\tt if \M{cond}}] --- starts a condicional part. 326\item[{\tt if \M{cond}}] --- starts a conditional part.
318If \M{cond} is false, this part is skiped by the lexical analizer. 327If \M{cond} is false, then this part is skipped by the lexical analyzer.
319\item[{\tt ifnot \M{cond}}] --- starts a condicional part. 328\item[{\tt ifnot \M{cond}}] --- starts a conditional part.
320If \M{cond} is true, this part is skiped by the lexical analizer. 329If \M{cond} is true, then this part is skipped by the lexical analyzer.
321\item[{\tt end}] --- ends a conditional part. 330\item[{\tt end}] --- ends a conditional part.
322\item[{\tt else}] --- starts an ``else'' conditional part, 331\item[{\tt else}] --- starts an ``else'' conditional part,
323switching the ``skip'' status. 332switching the ``skip'' status.
@@ -325,15 +334,16 @@ switching the ``skip'' status.
325\end{description} 334\end{description}
326 335
327Directives can be freely nested. 336Directives can be freely nested.
328Particularlly, a \verb|$endinput| may ocurr inside a \verb|$if|; 337Particularly, a \verb|$endinput| may occur inside a \verb|$if|;
329in that case, even the matching \verb|$end| is not parsed. 338in that case, even the matching \verb|$end| is not parsed.
330 339
331A \M{cond} part may be: 340A \M{cond} part may be:
332\begin{description} 341\begin{description}
333\item[{\tt nil}] --- always false. 342\item[{\tt nil}] --- always false.
334\item[{\tt 1}] --- always true. 343\item[{\tt 1}] --- always true.
335\item[\M{name}] --- true if global variable \M{name} is different from \nil. 344\item[\M{name}] --- true if the value of the
336Notice that this is evaluated before the chunk starts its execution. 345global variable \M{name} is different from \nil.
346Notice that \M{name} is evaluated before the chunk starts its execution.
337Therefore, actions in a chunk do not affect its own conditional directives. 347Therefore, actions in a chunk do not affect its own conditional directives.
338\end{description} 348\end{description}
339 349
@@ -389,7 +399,8 @@ This restriction also avoids some ``statement not reached'' conditions.
389 399
390\subsubsection{\Index{Assignment}} \label{assignment} 400\subsubsection{\Index{Assignment}} \label{assignment}
391The language allows \Index{multiple assignment}. 401The language allows \Index{multiple assignment}.
392Therefore, the syntax defines a list of variables on the left side, 402Therefore, the syntax for assignment
403defines a list of variables on the left side,
393and a list of expressions on the right side. 404and a list of expressions on the right side.
394Both lists have their elements separated by commas: 405Both lists have their elements separated by commas:
395\begin{Produc} 406\begin{Produc}
@@ -403,6 +414,7 @@ Therefore, it can be used to exchange two values, as in
403\begin{verbatim} 414\begin{verbatim}
404 x, y = y, x 415 x, y = y, x
405\end{verbatim} 416\end{verbatim}
417The two lists may have different lengths.
406Before the assignment, the list of values is {\em adjusted\/} to 418Before the assignment, the list of values is {\em adjusted\/} to
407the length of the list of variables \see{adjust}. 419the length of the list of variables \see{adjust}.
408 420
@@ -422,10 +434,12 @@ The meaning of assignments and evaluations of global variables and
422indexed variables can be changed by tag methods \see{tag-method}. 434indexed variables can be changed by tag methods \see{tag-method}.
423Actually, 435Actually,
424an assignment \verb|x = val|, where \verb|x| is a global variable, 436an assignment \verb|x = val|, where \verb|x| is a global variable,
425is equivalent to a call \verb|setglobal('x', val)|, 437is equivalent to a call \verb|setglobal('x', val)|;
426and an assignment \verb|t[i] = val| is equivalent to 438an assignment \verb|t[i] = val| is equivalent to
427\verb|settable(t, i, val)|. 439\verb|settable_event(t, i, val)|.
428See \See{tag-method} for a description of these ``functions''. 440See \See{tag-method} for a description of these functions%
441\footnote{Function \verb|setglobal| is pre-defined in Lua.
442Function {\tt settable\_event} is used only for explanation purposes.}.
429 443
430The syntax \verb|var.NAME| is just syntactic sugar for 444The syntax \verb|var.NAME| is just syntactic sugar for
431\verb|var["NAME"]|: 445\verb|var["NAME"]|:
@@ -498,27 +512,27 @@ Variables are explained in Section~\ref{assignment}.
498An access to a global variable \verb|x| is equivalent to a 512An access to a global variable \verb|x| is equivalent to a
499call \verb|getglobal('x')|; 513call \verb|getglobal('x')|;
500an access to an indexed variable \verb|t[i]| is equivalent to 514an access to an indexed variable \verb|t[i]| is equivalent to
501a call \verb|gettable(t, i)|. 515a call \verb|gettable_event(t, i)|.
502See \See{tag-method} for a description of these ``functions''. 516See \See{tag-method} for a description of these functions%
517\footnote{Function \verb|getglobal| is pre-defined in Lua.
518Function {\tt gettable\_event} is used only for explanation purposes.}.
503 519
504The non-terminal \verb|exp1| is used to indicate that the values 520The non-terminal \M{exp1} is used to indicate that the values
505returned by an expression must be adjusted to one single value: 521returned by an expression must be adjusted to one single value:
506\begin{Produc} 522\begin{Produc}
507\produc{exp1}{exp} 523\produc{exp1}{exp}
508\end{Produc} 524\end{Produc}
509 525
510\subsubsection{Arithmetic Operators} 526\subsubsection{Arithmetic Operators}
511Lua supports the usual \Index{arithmetic operators}. 527Lua supports the usual \Index{arithmetic operators}:
512These operators are the binary 528the binary \verb|+| (addition),
513\verb|+| (addition), 529\verb|-| (subtraction), \verb|*| (multiplication),
514\verb|-| (subtraction),
515\verb|*| (multiplication),
516\verb|/| (division) and \verb|^| (exponentiation), 530\verb|/| (division) and \verb|^| (exponentiation),
517and the unary \verb|-| (negation). 531and unary \verb|-| (negation).
518If the operands are numbers, or strings that can be converted to 532If the operands are numbers, or strings that can be converted to
519numbers, according to the rules given in Section~\ref{coercion}, 533numbers, according to the rules given in Section~\ref{coercion},
520then all operations except exponentiation have the usual meaning. 534then all operations except exponentiation have the usual meaning.
521Otherwise, a tag method is called \see{tag-method}. 535Otherwise, an appropriate tag method is called \see{tag-method}.
522An exponentiation always calls a tag method. 536An exponentiation always calls a tag method.
523The standard mathematical library redefines this method for numbers, 537The standard mathematical library redefines this method for numbers,
524giving the expected meaning to \Index{exponentiation} 538giving the expected meaning to \Index{exponentiation}
@@ -535,7 +549,7 @@ Equality first compares the types of its operands.
535If they are different, then the result is \nil. 549If they are different, then the result is \nil.
536Otherwise, their values are compared. 550Otherwise, their values are compared.
537Numbers and strings are compared in the usual way. 551Numbers and strings are compared in the usual way.
538Tables, userdatas and functions are compared by reference, 552Tables, userdata and functions are compared by reference,
539that is, two tables are considered equal only if they are the same table. 553that is, two tables are considered equal only if they are the same table.
540The operator \verb|~=| is exactly the negation of equality (\verb|==|). 554The operator \verb|~=| is exactly the negation of equality (\verb|==|).
541Note that the conversion rules of Section~\ref{coercion} 555Note that the conversion rules of Section~\ref{coercion}
@@ -560,10 +574,10 @@ The \Index{logical operators} are:
560 and or not 574 and or not
561\end{verbatim} 575\end{verbatim}
562The operator \verb|and| returns \nil\ if its first argument is \nil; 576The operator \verb|and| returns \nil\ if its first argument is \nil;
563otherwise it returns its second argument. 577otherwise, it returns its second argument.
564The operator \verb|or| returns its first argument 578The operator \verb|or| returns its first argument
565if it is different from \nil; 579if it is different from \nil;
566otherwise it returns its second argument. 580otherwise, it returns its second argument.
567Both \verb|and| and \verb|or| use \Index{short-cut evaluation}, 581Both \verb|and| and \verb|or| use \Index{short-cut evaluation},
568that is, 582that is,
569the second operand is evaluated only when necessary. 583the second operand is evaluated only when necessary.
@@ -573,7 +587,7 @@ Lua offers a string \Index{concatenation} operator,
573denoted by ``\IndexVerb{..}''. 587denoted by ``\IndexVerb{..}''.
574If operands are strings or numbers, then they are converted to 588If operands are strings or numbers, then they are converted to
575strings according to the rules in Section~\ref{coercion}. 589strings according to the rules in Section~\ref{coercion}.
576Otherwise, the tag method ``concat'' is called \see{tag-method}. 590Otherwise, the ``concat'' tag method is called \see{tag-method}.
577 591
578\subsubsection{Precedence} 592\subsubsection{Precedence}
579\Index{Operator precedence} follows the table below, 593\Index{Operator precedence} follows the table below,
@@ -615,7 +629,7 @@ For example:
615\begin{verbatim} 629\begin{verbatim}
616 a = {"v1", "v2", 34} 630 a = {"v1", "v2", 34}
617\end{verbatim} 631\end{verbatim}
618is essentialy equivalent to: 632is essentially equivalent to:
619\begin{verbatim} 633\begin{verbatim}
620 temp = {} 634 temp = {}
621 temp[1] = "v1" 635 temp[1] = "v1"
@@ -633,7 +647,7 @@ For example:
633\begin{verbatim} 647\begin{verbatim}
634 a = {[f(k)] = g(y), x = 1, y = 3, [0] = b+c} 648 a = {[f(k)] = g(y), x = 1, y = 3, [0] = b+c}
635\end{verbatim} 649\end{verbatim}
636is essentialy equivalent to: 650is essentially equivalent to:
637\begin{verbatim} 651\begin{verbatim}
638 temp = {} 652 temp = {}
639 temp[f(k)] = g(y) 653 temp[f(k)] = g(y)
@@ -642,22 +656,19 @@ is essentialy equivalent to:
642 temp[0] = b+c 656 temp[0] = b+c
643 a = temp 657 a = temp
644\end{verbatim} 658\end{verbatim}
645Notice that an expression like \verb|{x = 1, y = 4}| is 659An expression like \verb|{x = 1, y = 4}| is
646in fact a syntactic sugar for 660in fact syntactic sugar for \verb|{["x"] = 1, ["y"] = 4}|.
647\begin{verbatim}
648 {["x"] = 1, ["y"] = 4}
649\end{verbatim}
650 661
651\subsubsection{Function Calls} \label{functioncall} 662\subsubsection{Function Calls} \label{functioncall}
652A \Index{function call} has the following syntax: 663A \Index{function call} has the following syntax:
653\begin{Produc} 664\begin{Produc}
654\produc{functioncall}{var realParams} 665\produc{functioncall}{var realParams}
655\end{Produc}% 666\end{Produc}%
656Here, \verb|var| can be any variable (global, local, indexed, etc). 667Here, \M{var} can be any variable (global, local, indexed, etc).
657If its value has type {\em function\/}, 668If its value has type {\em function\/},
658then this function is called. 669then this function is called.
659Otherwise, the ``function'' tag method is called, 670Otherwise, the ``function'' tag method is called,
660having as first parameter the value of \verb|var|, 671having as first parameter the value of \M{var},
661and then the original call parameters. 672and then the original call parameters.
662 673
663The form: 674The form:
@@ -686,18 +697,18 @@ Because a function can return any number of results
686\see{return}, 697\see{return},
687the number of results must be adjusted before used. 698the number of results must be adjusted before used.
688If the function is called as a statement \see{funcstat}, 699If the function is called as a statement \see{funcstat},
689its return list is adjusted to 0, 700then its return list is adjusted to~0,
690thus discarding all returned values. 701thus discarding all returned values.
691If the function is called in a place that needs a single value 702If the function is called in a place that needs a single value
692(syntactically denoted by the non-terminal \verb|exp1|), 703(syntactically denoted by the non-terminal \M{exp1}),
693then its return list is adjusted to 1, 704then its return list is adjusted to~1,
694thus discarding all returned values but the first one. 705thus discarding all returned values but the first one.
695If the function is called in a place that can hold many values 706If the function is called in a place that can hold many values
696(syntactically denoted by the non-terminal \verb|exp|), 707(syntactically denoted by the non-terminal \M{exp}),
697then no adjustment is made. 708then no adjustment is made.
698 709
699 710
700\subsection{\Index{Function Definitions}} 711\subsection{\Index{Function Definitions}} \label{func-def}
701 712
702Functions in Lua can be defined anywhere in the global level of a chunk. 713Functions in Lua can be defined anywhere in the global level of a chunk.
703The syntax for function definition is: 714The syntax for function definition is:
@@ -731,7 +742,7 @@ instead, it collects any extra arguments in an implicit parameter,
731called \Def{arg}. 742called \Def{arg}.
732This parameter is always initialized as a table, 743This parameter is always initialized as a table,
733with a field \verb|n| with the number of extra arguments, 744with a field \verb|n| with the number of extra arguments,
734and the extra arguments at positions 1, 2, \ldots. 745and the extra arguments at positions 1, 2, \ldots
735 746
736As an example, suppose definitions like: 747As an example, suppose definitions like:
737\begin{verbatim} 748\begin{verbatim}
@@ -783,14 +794,14 @@ previously initialized with a table value.
783 794
784Lua provides a powerful mechanism to extend its semantics, 795Lua provides a powerful mechanism to extend its semantics,
785called \Def{Tag Methods}. 796called \Def{Tag Methods}.
786A tag method (TM) is a programmer defined function 797A tag method (TM) is a programmer-defined function
787that can be called in many key points of the evaluation of a program, 798that can be called at many key points of the evaluation of a program,
788allowing a programmer to change the standard Lua behavior at these points. 799allowing a programmer to change the standard Lua behavior at these points.
789Each of these points is called an \Def{event}. 800Each of these points is called an \Def{event}.
790 801
791The tag method called for any specific event is selected 802The tag method called for any specific event is selected
792accordingly with the tag~\see{TypesSec} of the values involved 803according to the tag of the values involved
793in the event. 804in the event \see{TypesSec}.
794The function \IndexVerb{settagmethod} changes the tag method 805The function \IndexVerb{settagmethod} changes the tag method
795associated with a given pair $<tag, event>$. 806associated with a given pair $<tag, event>$.
796Its first parameter is the tag, the second the event name 807Its first parameter is the tag, the second the event name
@@ -810,7 +821,7 @@ The function not only shows when a tag method is called,
810but also its arguments, its results and the default behavior. 821but also its arguments, its results and the default behavior.
811Please notice that the code shown here is only illustrative; 822Please notice that the code shown here is only illustrative;
812the real behavior is hard coded in the interpreter, 823the real behavior is hard coded in the interpreter,
813and it is much more eficient than this simulation. 824and it is much more efficient than this simulation.
814All functions used in these descriptions 825All functions used in these descriptions
815(\verb|rawgetglobal|, \verb|tonumber|, \verb|call|, etc) 826(\verb|rawgetglobal|, \verb|tonumber|, \verb|call|, etc)
816are described in \See{predefined}. 827are described in \See{predefined}.
@@ -822,10 +833,10 @@ called when a \verb|+| operation is applied to non numerical operands.
822 833
823The function \verb|getbinmethod| defines how Lua chooses a tag method 834The function \verb|getbinmethod| defines how Lua chooses a tag method
824for a binary operation. 835for a binary operation.
825First Lua trys the first operand. 836First Lua tries the first operand.
826If its tag does not define a tag method for the operation, 837If its tag does not define a tag method for the operation,
827Lua trys the second operand. 838then Lua tries the second operand.
828If it also fails, it gets a tag method from tag 0: 839If it also fails, then it gets a tag method from tag~0:
829\begin{verbatim} 840\begin{verbatim}
830 function getbinmethod (op1, op2, event) 841 function getbinmethod (op1, op2, event)
831 return gettagmethod(tag(op1), event) or 842 return gettagmethod(tag(op1), event) or
@@ -853,18 +864,18 @@ If it also fails, it gets a tag method from tag 0:
853 864
854\item[``sub'':]\index{sub event} 865\item[``sub'':]\index{sub event}
855called when a \verb|-| operation is applied to non numerical operands. 866called when a \verb|-| operation is applied to non numerical operands.
856Behavior is similar to \verb|"add"| event. 867Behavior similar to \verb|"add"| event.
857 868
858\item[``mul'':]\index{mul event} 869\item[``mul'':]\index{mul event}
859called when a \verb|*| operation is applied to non numerical operands. 870called when a \verb|*| operation is applied to non numerical operands.
860Behavior is similar to \verb|"add"| event. 871Behavior similar to \verb|"add"| event.
861 872
862\item[``div'':]\index{div event} 873\item[``div'':]\index{div event}
863called when a \verb|/| operation is applied to non numerical operands. 874called when a \verb|/| operation is applied to non numerical operands.
864Behavior is similar to \verb|"add"| event. 875Behavior similar to \verb|"add"| event.
865 876
866\item[``pow'':]\index{pow event} 877\item[``pow'':]\index{pow event}
867called whenenver a \verb|^| operation is applied. 878called when a \verb|^| operation is applied.
868\begin{verbatim} 879\begin{verbatim}
869 function pow_event (op1, op2) 880 function pow_event (op1, op2)
870 local tm = getbinmethod(op1, op2, "pow") 881 local tm = getbinmethod(op1, op2, "pow")
@@ -924,17 +935,17 @@ or non string operands.
924\item[``gt'':]\index{gt event} 935\item[``gt'':]\index{gt event}
925called when a \verb|>| operation is applied to non numerical 936called when a \verb|>| operation is applied to non numerical
926or non string operands. 937or non string operands.
927Behavior is similar to \verb|"lt"| event. 938Behavior similar to \verb|"lt"| event.
928 939
929\item[``le'':]\index{le event} 940\item[``le'':]\index{le event}
930called when a \verb|<=| operation is applied to non numerical 941called when a \verb|<=| operation is applied to non numerical
931or non string operands. 942or non string operands.
932Behavior is similar to \verb|"lt"| event. 943Behavior similar to \verb|"lt"| event.
933 944
934\item[``ge'':]\index{ge event} 945\item[``ge'':]\index{ge event}
935called when a \verb|>=| operation is applied to non numerical 946called when a \verb|>=| operation is applied to non numerical
936or non string operands. 947or non string operands.
937Behavior is similar to \verb|"lt"| event. 948Behavior similar to \verb|"lt"| event.
938 949
939\item[``concat'':]\index{concatenation event} 950\item[``concat'':]\index{concatenation event}
940called when a concatenation is applied to non string operands. 951called when a concatenation is applied to non string operands.
@@ -1011,8 +1022,7 @@ called whenever Lua accesses an indexed variable.
1011\end{verbatim} 1022\end{verbatim}
1012 1023
1013\item[``settable'':]\index{settable event} 1024\item[``settable'':]\index{settable event}
1014called whenever Lua assigns to an indexed variable. 1025called when Lua assigns to an indexed variable.
1015in a table.
1016\begin{verbatim} 1026\begin{verbatim}
1017 function settable_event (table, index, value) 1027 function settable_event (table, index, value)
1018 local tm = gettagmethod(tag(table), "settable") 1028 local tm = gettagmethod(tag(table), "settable")
@@ -1062,8 +1072,8 @@ Lua does the equivalent of the following function:
1062 end 1072 end
1063 end 1073 end
1064\end{verbatim} 1074\end{verbatim}
1065Moreover, at the end of a garbage collection cicle, 1075Moreover, at the end of a garbage collection cycle,
1066Lua does the equivalent to the call \verb|gc_event(nil)|. 1076Lua does the equivalent of the call \verb|gc_event(nil)|.
1067 1077
1068\end{description} 1078\end{description}
1069 1079
@@ -1086,14 +1096,15 @@ using the debug facilities \see{debugI},
1086in order to print some extra information, 1096in order to print some extra information,
1087like the call stack. 1097like the call stack.
1088To provide more information about errors, 1098To provide more information about errors,
1089Lua programs can include the compilation pragma \verb|$debug|. 1099Lua programs should include the compilation pragma \verb|$debug|.
1090\index{debug pragma}\label{pragma} 1100\index{debug pragma}\label{pragma}
1091When an error occurs in a program compiled with this option, 1101When an error occurs in a program compiled with this option,
1092the error routine is able to print the number of the lines where the calls 1102the error routine is able to print the number of the lines where the calls
1093(and the error) were made. 1103(and the error) were made.
1094If needed, it is possible to change the error method with the 1104If needed, it is possible to change the error method with the
1095function \verb|seterrormethod|, 1105function \verb|seterrormethod|,
1096which gets the new error handler as its only parameter. 1106which gets the new error handler as its only parameter
1107\see{pdf-seterrormethod}.
1097 1108
1098Lua code can explicitly generate an error by calling the built-in 1109Lua code can explicitly generate an error by calling the built-in
1099function \verb|error| \see{pdf-error}. 1110function \verb|error| \see{pdf-error}.
@@ -1103,7 +1114,7 @@ function \verb|error| \see{pdf-error}.
1103 1114
1104This section describes the API for Lua, that is, 1115This section describes the API for Lua, that is,
1105the set of C functions available to the host program to communicate 1116the set of C functions available to the host program to communicate
1106with the library. 1117with the Lua library.
1107The API functions can be classified in the following categories: 1118The API functions can be classified in the following categories:
1108\begin{enumerate} 1119\begin{enumerate}
1109\item exchanging values between C and Lua; 1120\item exchanging values between C and Lua;
@@ -1123,7 +1134,7 @@ all values passed between Lua and C have type
1123which works like an abstract type in C that can hold any Lua value. 1134which works like an abstract type in C that can hold any Lua value.
1124Values of type \verb|lua_Object| have no meaning outside Lua; 1135Values of type \verb|lua_Object| have no meaning outside Lua;
1125for instance, 1136for instance,
1126the comparisson of two \verb|lua_Object's| is undefined. 1137the comparison of two \verb|lua_Object's| is undefined.
1127 1138
1128To check the type of a \verb|lua_Object|, 1139To check the type of a \verb|lua_Object|,
1129the following functions are available: 1140the following functions are available:
@@ -1163,17 +1174,19 @@ char *lua_getstring (lua_Object object);
1163lua_CFunction lua_getcfunction (lua_Object object); 1174lua_CFunction lua_getcfunction (lua_Object object);
1164void *lua_getuserdata (lua_Object object); 1175void *lua_getuserdata (lua_Object object);
1165\end{verbatim} 1176\end{verbatim}
1177
1166\verb|lua_getnumber| converts a \verb|lua_Object| to a floating-point number. 1178\verb|lua_getnumber| converts a \verb|lua_Object| to a floating-point number.
1167This \verb|lua_Object| must be a number or a string convertible to number 1179This \verb|lua_Object| must be a number or a string convertible to number
1168\see{coercion}; otherwise, the function returns 0. 1180\see{coercion}; otherwise, the function returns~0.
1169 1181
1170\verb|lua_getstring| converts a \verb|lua_Object| to a string (\verb|char *|). 1182\verb|lua_getstring| converts a \verb|lua_Object| to a string (\verb|char*|).
1171This \verb|lua_Object| must be a string or a number; 1183This \verb|lua_Object| must be a string or a number;
1172otherwise, the function returns 0 (the \verb|NULL| pointer). 1184otherwise, the function returns~0 (the \verb|NULL| pointer).
1173This function does not create a new string, 1185This function does not create a new string,
1174but returns a pointer to a string inside the Lua environment. 1186but returns a pointer to a string inside the Lua environment.
1175Because Lua has garbage collection, 1187Because Lua has garbage collection,
1176there is no guarantee that such pointer will be valid after the block ends. 1188there is no guarantee that such pointer will be valid after the block ends
1189(see below).
1177 1190
1178\verb|lua_getcfunction| converts a \verb|lua_Object| to a C function. 1191\verb|lua_getcfunction| converts a \verb|lua_Object| to a C function.
1179This \verb|lua_Object| must have type {\em CFunction\/}; 1192This \verb|lua_Object| must have type {\em CFunction\/};
@@ -1193,18 +1206,18 @@ It is good programming practice to convert Lua objects to C values
1193as soon as they are available, 1206as soon as they are available,
1194and never to store \verb|lua_Object|s in C global variables. 1207and never to store \verb|lua_Object|s in C global variables.
1195 1208
1196A garbage collection cicle can be forced by: 1209A garbage collection cycle can be forced by:
1197\Deffunc{lua_collectgarbage} 1210\Deffunc{lua_collectgarbage}
1198\begin{verbatim} 1211\begin{verbatim}
1199long lua_collectgarbage (long limit); 1212long lua_collectgarbage (long limit);
1200\end{verbatim} 1213\end{verbatim}
1201This function returns the number of objects collected. 1214This function returns the number of objects collected.
1202The argument \verb|limit| makes the next cicle occur when that number 1215The argument \verb|limit| makes the next cycle occur only
1203of new objects have been created. 1216when that number of new objects have been created.
1204If \verb|limit|=0, Lua uses an adaptative algorithm to set this limit. 1217If \verb|limit|=0, then Lua uses an adaptable heuristics to set this limit.
1205 1218
1206 1219
1207All comunication between Lua and C is done through two 1220All communication between Lua and C is done through two
1208abstract data types, called \Def{lua2C} and \Def{C2lua}. 1221abstract data types, called \Def{lua2C} and \Def{C2lua}.
1209The first one, as the name implies, is used to pass values 1222The first one, as the name implies, is used to pass values
1210from Lua to C: parameters when Lua calls C and results when C calls Lua. 1223from Lua to C: parameters when Lua calls C and results when C calls Lua.
@@ -1227,7 +1240,7 @@ Notice that the structure lua2C cannot be directly modified by C code.
1227 1240
1228The second structure, C2lua, is a stack. 1241The second structure, C2lua, is a stack.
1229Pushing elements into this stack 1242Pushing elements into this stack
1230is done by using the following functions: 1243is done with the following functions:
1231\Deffunc{lua_pushnumber}\Deffunc{lua_pushstring} 1244\Deffunc{lua_pushnumber}\Deffunc{lua_pushstring}
1232\Deffunc{lua_pushcfunction}\Deffunc{lua_pushusertag} 1245\Deffunc{lua_pushcfunction}\Deffunc{lua_pushusertag}
1233\Deffunc{lua_pushnil}\Deffunc{lua_pushobject} 1246\Deffunc{lua_pushnil}\Deffunc{lua_pushobject}
@@ -1256,17 +1269,17 @@ all elements that they use.
1256 1269
1257Because userdata are objects, 1270Because userdata are objects,
1258the function \verb|lua_pushusertag| may create a new userdata. 1271the function \verb|lua_pushusertag| may create a new userdata.
1259If Lua has a userdata with the given value (\verb|void *|) and tag, 1272If Lua has a userdata with the given value (\verb|void*|) and tag,
1260that userdata is pushed. 1273that userdata is pushed.
1261Otherwise, a new userdata is created, with the given value and tag. 1274Otherwise, a new userdata is created, with the given value and tag.
1262If this function is called with 1275If this function is called with
1263\verb|tag|=\verb|LUA_ANYTAG|\Deffunc{LUA_ANYTAG}, 1276\verb|tag|=\verb|LUA_ANYTAG|\Deffunc{LUA_ANYTAG},
1264then Lua will try to find any userdata with the given value, 1277then Lua will try to find any userdata with the given value,
1265no matter its tag. 1278no matter its tag.
1266If there is no userdata with that value, a new one is created, 1279If there is no userdata with that value, then a new one is created,
1267with tag=0. 1280with tag=0.
1268 1281
1269User data can have different tags, 1282Userdata can have different tags,
1270whose semantics are only known to the host program. 1283whose semantics are only known to the host program.
1271Tags are created with the function: 1284Tags are created with the function:
1272\Deffunc{lua_newtag} 1285\Deffunc{lua_newtag}
@@ -1283,8 +1296,8 @@ void lua_settag (int tag);
1283\verb|tag| must be a value created with \verb|lua_newtag|. 1296\verb|tag| must be a value created with \verb|lua_newtag|.
1284 1297
1285When C code calls Lua repeatedly, as in a loop, 1298When C code calls Lua repeatedly, as in a loop,
1286objects returned by these calls accumulate, 1299objects returned by these calls can accumulate,
1287and may create a memory problem. 1300and may cause a stack overflow.
1288To avoid this, 1301To avoid this,
1289nested blocks can be defined with the functions: 1302nested blocks can be defined with the functions:
1290\begin{verbatim} 1303\begin{verbatim}
@@ -1297,7 +1310,7 @@ The use of explicit nested blocks is strongly encouraged.
1297 1310
1298\subsection{Executing Lua Code} 1311\subsection{Executing Lua Code}
1299A host program can execute Lua chunks written in a file or in a string 1312A host program can execute Lua chunks written in a file or in a string
1300using the following functions: 1313using the following functions:%
1301\Deffunc{lua_dofile}\Deffunc{lua_dostring} 1314\Deffunc{lua_dofile}\Deffunc{lua_dostring}
1302\begin{verbatim} 1315\begin{verbatim}
1303int lua_dofile (char *filename); 1316int lua_dofile (char *filename);
@@ -1325,7 +1338,7 @@ one uses the function:
1325\begin{verbatim} 1338\begin{verbatim}
1326lua_Object lua_getglobal (char *varname); 1339lua_Object lua_getglobal (char *varname);
1327\end{verbatim} 1340\end{verbatim}
1328As in Lua, this function may call a tag method. 1341As in Lua, this function may trigger a tag method.
1329To read the real value of any global variable, 1342To read the real value of any global variable,
1330without invoking any tag method, 1343without invoking any tag method,
1331this function has a {\em raw\/} version: 1344this function has a {\em raw\/} version:
@@ -1336,11 +1349,11 @@ lua_Object lua_rawgetglobal (char *varname);
1336 1349
1337To store a value previously pushed onto C2lua in a global variable, 1350To store a value previously pushed onto C2lua in a global variable,
1338there is the function: 1351there is the function:
1339\Deffunc{lua_storeglobal} 1352\Deffunc{lua_setglobal}
1340\begin{verbatim} 1353\begin{verbatim}
1341void lua_setglobal (char *varname); 1354void lua_setglobal (char *varname);
1342\end{verbatim} 1355\end{verbatim}
1343As in Lua, this function may call a tag method. 1356As in Lua, this function may trigger a tag method.
1344To set the real value of any global variable, 1357To set the real value of any global variable,
1345without invoking any tag method, 1358without invoking any tag method,
1346this function has a {\em raw\/} version: 1359this function has a {\em raw\/} version:
@@ -1357,7 +1370,7 @@ lua_Object lua_gettable (void);
1357\end{verbatim} 1370\end{verbatim}
1358pops from the stack C2lua a table and an index, 1371pops from the stack C2lua a table and an index,
1359and returns the contents of the table at that index. 1372and returns the contents of the table at that index.
1360As in Lua, this operation may call a tag method. 1373As in Lua, this operation may trigger a tag method.
1361To get the real value of any table index, 1374To get the real value of any table index,
1362without invoking any tag method, 1375without invoking any tag method,
1363this function has a {\em raw\/} version: 1376this function has a {\em raw\/} version:
@@ -1419,13 +1432,13 @@ equivalent to the Lua code:
1419 a = f(t.x, 4) 1432 a = f(t.x, 4)
1420\end{verbatim} 1433\end{verbatim}
1421\begin{verbatim} 1434\begin{verbatim}
1422 lua_pushobject(lua_getglobal("t")); /* push value of global 't' */ 1435 lua_pushobject(lua_getglobal("t")); /* push value of global 't' */
1423 lua_pushstring("x"); /* push the string 'x' */ 1436 lua_pushstring("x"); /* push the string 'x' */
1424 lua_pushobject(lua_gettable()); /* push result of t.x (= t['x']) */ 1437 lua_pushobject(lua_gettable()); /* push result of t.x (= t['x']) */
1425 lua_pushnumber(4); /* 2nd argument */ 1438 lua_pushnumber(4); /* 2nd argument */
1426 lua_callfunction(lua_getglobal("f")); /* call Lua function */ 1439 lua_callfunction(lua_getglobal("f")); /* call Lua function */
1427 lua_pushobject(lua_getresult(1)); /* push first result of the call */ 1440 lua_pushobject(lua_getresult(1)); /* push first result of the call */
1428 lua_setglobal("a"); /* sets global variable 'a' */ 1441 lua_setglobal("a"); /* sets global variable 'a' */
1429\end{verbatim} 1442\end{verbatim}
1430 1443
1431Some special Lua functions have exclusive interfaces. 1444Some special Lua functions have exclusive interfaces.
@@ -1458,7 +1471,7 @@ the second is the event name \see{tag-method},
1458and the third is a CFunction to be used as the new method. 1471and the third is a CFunction to be used as the new method.
1459This function returns a \verb|lua_Object|, 1472This function returns a \verb|lua_Object|,
1460which is the old tag method value. 1473which is the old tag method value.
1461To only get the current value of a tag method, 1474To get just the current value of a tag method,
1462there is the function 1475there is the function
1463\Deffunc{lua_gettagmethod} 1476\Deffunc{lua_gettagmethod}
1464\begin{verbatim} 1477\begin{verbatim}
@@ -1471,7 +1484,7 @@ To register a C function to Lua,
1471there is the following macro: 1484there is the following macro:
1472\Deffunc{lua_register} 1485\Deffunc{lua_register}
1473\begin{verbatim} 1486\begin{verbatim}
1474#define lua_register(n,f) (lua_pushcfunction(f), lua_storeglobal(n)) 1487#define lua_register(n,f) (lua_pushcfunction(f), lua_setglobal(n))
1475/* char *n; */ 1488/* char *n; */
1476/* lua_CFunction f; */ 1489/* lua_CFunction f; */
1477\end{verbatim} 1490\end{verbatim}
@@ -1505,7 +1518,7 @@ For some examples, see files \verb|strlib.c|,
1505As noted in Section~\ref{LuacallC}, \verb|lua_Object|s are volatile. 1518As noted in Section~\ref{LuacallC}, \verb|lua_Object|s are volatile.
1506If the C code needs to keep a \verb|lua_Object| 1519If the C code needs to keep a \verb|lua_Object|
1507outside block boundaries, 1520outside block boundaries,
1508it must create a \Def{reference} to the object. 1521then it must create a \Def{reference} to the object.
1509The routines to manipulate references are the following: 1522The routines to manipulate references are the following:
1510\Deffunc{lua_ref}\Deffunc{lua_getref} 1523\Deffunc{lua_ref}\Deffunc{lua_getref}
1511\Deffunc{lua_unref} 1524\Deffunc{lua_unref}
@@ -1537,7 +1550,7 @@ The set of \Index{predefined functions} in Lua is small but powerful.
1537Most of them provide features that allow some degree of 1550Most of them provide features that allow some degree of
1538\Index{reflexivity} in the language. 1551\Index{reflexivity} in the language.
1539Some of these features cannot be simulated with the rest of the 1552Some of these features cannot be simulated with the rest of the
1540Language nor with the standard Lua API. 1553language nor with the standard Lua API.
1541Others are just convenient interfaces to common API functions. 1554Others are just convenient interfaces to common API functions.
1542 1555
1543The libraries, on the other hand, provide useful routines 1556The libraries, on the other hand, provide useful routines
@@ -1558,7 +1571,7 @@ declared in \verb|lualib.h|.
1558 1571
1559\subsection{Predefined Functions} \label{predefined} 1572\subsection{Predefined Functions} \label{predefined}
1560 1573
1561\subsection*{\ff{\tt call (func, arg, [retmode])}}\Deffunc{call} 1574\subsubsection*{\ff {\tt call (func, arg, [retmode])}}\Deffunc{call}
1562This function calls function \verb|func| with 1575This function calls function \verb|func| with
1563the arguments given by the table \verb|arg|. 1576the arguments given by the table \verb|arg|.
1564The call is equivalent to 1577The call is equivalent to
@@ -1566,59 +1579,61 @@ The call is equivalent to
1566 func(arg[1], arg[2], ..., arg[arg.n]) 1579 func(arg[1], arg[2], ..., arg[arg.n])
1567\end{verbatim} 1580\end{verbatim}
1568If \verb|arg.n| is not defined, 1581If \verb|arg.n| is not defined,
1569Lua gets the arguments from \verb|arg[1]| until the first nil value. 1582then Lua stops getting arguments at the first nil value.
1570 1583
1571If \verb|retmode| is equal to \verb|"plain"| or is absent, 1584If \verb|retmode| is absent,
1572all results from \verb|func| are just returned by the call. 1585all results from \verb|func| are just returned by the call.
1573If \verb|retmode| is equal to \verb|"pack"|, 1586If \verb|retmode| is equal to \verb|"pack"|,
1574the results are {\em packed\/} in a single table.\index{packed results} 1587the results are {\em packed\/} in a single table.\index{packed results}
1575That is, \verb|call| returns just one table. 1588That is, \verb|call| returns just one table.
1576At index \verb|n| the table has the total number of results 1589At index \verb|n|, the table has the total number of results
1577from the call; 1590from the call;
1578the first result is at index 1, etc. 1591the first result is at index 1, etc.
1579 1592
1580For instance, the following calls produce the following results: 1593For instance, the following calls produce the following results:
1581\begin{verbatim} 1594\begin{verbatim}
1582a = call(sin, {5}) -- a = 0.0871557 = sin(5) 1595a = call(sin, {5}) --> a = 0.0871557 = sin(5)
1583a = call(max, {1,4,5; n=2}) -- a = 4 (only 1 and 4 are arguments) 1596a = call(max, {1,4,5; n=2}) --> a = 4 (only 1 and 4 are arguments)
1584t = {x=1} 1597t = {x=1}
1585a = call(next, {t,nil;n=2}, "pack") -- a={"x", 1; n=2} 1598a = call(next, {t,nil;n=2}, "pack") --> a={"x", 1; n=2}
1586\end{verbatim} 1599\end{verbatim}
1587 1600
1588\subsection*{\ff{\tt callgc ([nextgc])}}\Deffunc{callgc} 1601\subsubsection*{\ff {\tt collectgarbage ([limit])}}\Deffunc{collectgarbage}
1589Forces a garbage collection cicle. 1602Forces a garbage collection cycle.
1590Returns the number of objects collected. 1603Returns the number of objects collected.
1591An optional argument, \verb|nextgc|, is a number that 1604An optional argument, \verb|limit|, is a number that
1592makes the next cicle occur when that number of new 1605makes the next cycle occur when that number of new
1593objects have been created. 1606objects have been created.
1594If absent, Lua uses an adaptative algorithm to set 1607If absent, Lua uses an adaptable algorithm to set
1595this limit. 1608this limit.
1596\verb|nextgc| is simply an interface to \verb|lua_collectgarbage|. 1609\verb|collectgarbage| is equivalent to
1610the API function \verb|lua_collectgarbage|.
1597 1611
1598\subsubsection*{\ff{\tt dofile (filename)}}\Deffunc{dofile} 1612\subsubsection*{\ff {\tt dofile (filename)}}\Deffunc{dofile}
1599This function receives a file name, 1613This function receives a file name,
1600opens it, and executes its contents as a Lua chunk, 1614opens it, and executes its contents as a Lua chunk,
1601or as pre-compiled chunks. 1615or as pre-compiled chunks.
1602When called without arguments, 1616When called without arguments,
1603it executes the contents of the standard input (\verb|stdin|). 1617it executes the contents of the standard input (\verb|stdin|).
1604If there is any error executing the file, it returns \nil. 1618If there is any error executing the file,
1619then \verb|dofile| returns \nil.
1605Otherwise, it returns the values returned by the chunk, 1620Otherwise, it returns the values returned by the chunk,
1606or a non \nil\ value if the chunk returns no values. 1621or a non \nil\ value if the chunk returns no values.
1607It issues an error when called with a non string argument. 1622It issues an error when called with a non string argument.
1608\verb|dofile| is simply an interface to \verb|lua_dofile|. 1623\verb|dofile| is equivalent to the API function \verb|lua_dofile|.
1609 1624
1610\subsubsection*{\ff{\tt dostring (string)}}\Deffunc{dostring} 1625\subsubsection*{\ff {\tt dostring (string)}}\Deffunc{dostring}
1611This function executes a given string as a Lua chunk. 1626This function executes a given string as a Lua chunk.
1612If there is any error executing the string, it returns \nil. 1627If there is any error executing the string, it returns \nil.
1613Otherwise, it returns the values returned by the chunk, 1628Otherwise, it returns the values returned by the chunk,
1614or a non \nil\ value if the chunk returns no values. 1629or a non \nil\ value if the chunk returns no values.
1615\verb|dostring| is simply an interface to \verb|lua_dostring|. 1630\verb|dostring| is equivalent to the API function \verb|lua_dostring|.
1616 1631
1617\subsubsection*{\ff{\tt newtag ()}}\Deffunc{newtag}\label{pdf-newtag} 1632\subsubsection*{\ff {\tt newtag ()}}\Deffunc{newtag}\label{pdf-newtag}
1618Returns a new tag. 1633Returns a new tag.
1619\verb|newtag| is simply an interface to \verb|lua_newtag|. 1634\verb|newtag| is equivalent to the API function \verb|lua_newtag|.
1620 1635
1621\subsubsection*{\ff{\tt next (table, index)}}\Deffunc{next} 1636\subsubsection*{\ff {\tt next (table, index)}}\Deffunc{next}
1622This function allows a program to traverse all fields of a table. 1637This function allows a program to traverse all fields of a table.
1623Its first argument is a table and its second argument 1638Its first argument is a table and its second argument
1624is an index in this table. 1639is an index in this table.
@@ -1635,13 +1650,13 @@ semantically, there is no difference between a
1635field not present in a table or a field with value \nil. 1650field not present in a table or a field with value \nil.
1636Therefore, the function only considers fields with non \nil\ values. 1651Therefore, the function only considers fields with non \nil\ values.
1637The order in which the indices are enumerated is not specified, 1652The order in which the indices are enumerated is not specified,
1638{\em even for numeric indices}. 1653{\em not even for numeric indices}.
1639If the table is modified in any way during a traversal, 1654If the table is modified in any way during a traversal,
1640the semantics of \verb|next| is undefined. 1655the semantics of \verb|next| is undefined.
1641 1656
1642This function cannot be written with the standard API. 1657This function cannot be written with the standard API.
1643 1658
1644\subsubsection*{\ff{\tt nextvar (name)}}\Deffunc{nextvar} 1659\subsubsection*{\ff {\tt nextvar (name)}}\Deffunc{nextvar}
1645This function is similar to the function \verb|next|, 1660This function is similar to the function \verb|next|,
1646but iterates over the global variables. 1661but iterates over the global variables.
1647Its single argument is the name of a global variable, 1662Its single argument is the name of a global variable,
@@ -1654,11 +1669,11 @@ otherwise the semantics of \verb|nextvar| is undefined.
1654 1669
1655This function cannot be written with the standard API. 1670This function cannot be written with the standard API.
1656 1671
1657\subsubsection*{\ff{\tt tostring (e)}}\Deffunc{tostring} 1672\subsubsection*{\ff {\tt tostring (e)}}\Deffunc{tostring}
1658This function receives an argument of any type and 1673This function receives an argument of any type and
1659converts it to a string in a reasonable format. 1674converts it to a string in a reasonable format.
1660 1675
1661\subsubsection*{\ff{\tt print (e1, e2, ...)}}\Deffunc{print} 1676\subsubsection*{\ff {\tt print (e1, e2, ...)}}\Deffunc{print}
1662This function receives any number of arguments, 1677This function receives any number of arguments,
1663and prints their values in a reasonable format. 1678and prints their values in a reasonable format.
1664Each value is printed in a new line. 1679Each value is printed in a new line.
@@ -1667,14 +1682,14 @@ but as a quick way to show a value,
1667for instance for error messages or debugging. 1682for instance for error messages or debugging.
1668See Section~\ref{libio} for functions for formatted output. 1683See Section~\ref{libio} for functions for formatted output.
1669 1684
1670\subsubsection*{\ff{\tt tonumber (e)}}\Deffunc{tonumber} 1685\subsubsection*{\ff {\tt tonumber (e)}}\Deffunc{tonumber}
1671This function receives one argument, 1686This function receives one argument,
1672and tries to convert it to a number. 1687and tries to convert it to a number.
1673If the argument is already a number or a string convertible 1688If the argument is already a number or a string convertible
1674to a number \see{coercion}, then it returns that number; 1689to a number \see{coercion}, then it returns that number;
1675otherwise, it returns \nil. 1690otherwise, it returns \nil.
1676 1691
1677\subsubsection*{\ff{\tt type (v)}}\Deffunc{type} 1692\subsubsection*{\ff {\tt type (v)}}\Deffunc{type}\label{pdf-type}
1678This function allows Lua to test the type of a value. 1693This function allows Lua to test the type of a value.
1679It receives one argument, and returns its type, coded as a string. 1694It receives one argument, and returns its type, coded as a string.
1680The possible results of this function are 1695The possible results of this function are
@@ -1684,79 +1699,81 @@ The possible results of this function are
1684\verb|"table"|, 1699\verb|"table"|,
1685\verb|"function"|, 1700\verb|"function"|,
1686and \verb|"userdata"|. 1701and \verb|"userdata"|.
1702\verb|type| is equivalent to the API function \verb|lua_type|.
1687 1703
1688\verb|type| is simply an interface to \verb|lua_type|. 1704\subsubsection*{\ff {\tt tag (v)}}\Deffunc{tag}
1689
1690\subsubsection*{\ff{\tt tag (v)}}\Deffunc{tag}
1691This function allows Lua to test the tag of a value \see{TypesSec}. 1705This function allows Lua to test the tag of a value \see{TypesSec}.
1692It receives one argument, and returns its tag (a number). 1706It receives one argument, and returns its tag (a number).
1707\verb|tag| is equivalent to the API function \verb|lua_tag|.
1693 1708
1694\subsubsection*{\ff{\tt settag (o, tag)}}\Deffunc{settag} 1709\subsubsection*{\ff {\tt settag (o, tag)}}\Deffunc{settag}
1695This function sets the tag of a given object \see{TypesSec}. 1710This function sets the tag of a given object \see{TypesSec}.
1696The object \verb|o| must be a userdata or a table. 1711The object \verb|o| must be a userdata or a table.
1697\verb|tag| must be a value created with \verb|newtag| 1712\verb|tag| must be a value created with \verb|newtag|
1698\see{pdf-newtag}. 1713\see{pdf-newtag}.
1714\verb|settag| is equivalent to the API function \verb|lua_settag|.
1699 1715
1700\subsubsection*{\ff{\tt assert (v)}}\Deffunc{assert} 1716\subsubsection*{\ff {\tt assert (v)}}\Deffunc{assert}
1701This function issues an {\em ``assertion failed!''} error 1717This function issues an {\em ``assertion failed!''} error
1702when its argument is \nil. 1718when its argument is \nil.
1703 1719
1704\subsubsection*{\ff{\tt error (message)}}\Deffunc{error}\label{pdf-error} 1720\subsubsection*{\ff {\tt error (message)}}\Deffunc{error}\label{pdf-error}
1705This function issues an error message and terminates 1721This function issues an error message and terminates
1706the last called function from the library 1722the last called function from the library
1707(\verb|lua_dofile|, \verb|lua_dostring|, or \verb|lua_callfunction|). 1723(\verb|lua_dofile|, \verb|lua_dostring|, or \verb|lua_callfunction|).
1708It never returns. 1724It never returns.
1709\verb|error| is simply an interface to \verb|lua_error|. 1725\verb|error| is equivalent to the API function \verb|lua_error|.
1710 1726
1711\subsubsection*{\ff{\tt rawgettable (table, index)}}\Deffunc{rawgettable} 1727\subsubsection*{\ff {\tt rawgettable (table, index)}}\Deffunc{rawgettable}
1712Gets the real value of \verb|table[index]|, 1728Gets the real value of \verb|table[index]|,
1713without invoking any tag method. 1729without invoking any tag method.
1714\verb|table| must be a table, 1730\verb|table| must be a table,
1715and \verb|index| is any value different from \nil. 1731and \verb|index| is any value different from \nil.
1716 1732
1717\subsubsection*{\ff{\tt rawsettable (table, index, value)}}\Deffunc{rawsettable} 1733\subsubsection*{\ff {\tt rawsettable (table, index, value)}}\Deffunc{rawsettable}
1718Sets the real value \verb|table[index]=value|, 1734Sets the real value \verb|table[index]=value|,
1719without invoking any tag method. 1735without invoking any tag method.
1720\verb|table| must be a table, 1736\verb|table| must be a table,
1721\verb|index| is any value different from \nil, 1737\verb|index| is any value different from \nil,
1722and \verb|value| is any Lua value. 1738and \verb|value| is any Lua value.
1723 1739
1724\subsubsection*{\ff{\tt rawsetglobal (name, value)}}\Deffunc{rawsetglobal} 1740\subsubsection*{\ff {\tt rawsetglobal (name, value)}}\Deffunc{rawsetglobal}
1725This function assigns the given value to a global variable. 1741This function assigns the given value to a global variable.
1726The string \verb|name| does not need to be a syntactically valid variable name. 1742The string \verb|name| does not need to be a syntactically valid variable name.
1727Therefore, this function can set global variables with strange names like 1743Therefore, this function can set global variables with strange names like
1728\verb|`m v 1'| or \verb|34|. 1744\verb|"m v 1"| or \verb|34|.
1729It returns the value of its second argument. 1745It returns the value of its second argument.
1730 1746
1731\subsubsection*{\ff{\tt setglobal (name, value)}}\Deffunc{setglobal} 1747\subsubsection*{\ff {\tt setglobal (name, value)}}\Deffunc{setglobal}
1732This function assigns the given value to a global variable, 1748This function assigns the given value to a global variable,
1733or calls a tag method. 1749or calls a tag method.
1734Its full semantics is explained in \See{tag-method}. 1750Its full semantics is explained in \See{tag-method}.
1735 1751
1736\subsubsection*{\ff{\tt rawgetglobal (name)}}\Deffunc{rawgetglobal} 1752\subsubsection*{\ff {\tt rawgetglobal (name)}}\Deffunc{rawgetglobal}
1737This function retrieves the value of a global variable. 1753This function retrieves the value of a global variable.
1738The string \verb|name| does not need to be a 1754The string \verb|name| does not need to be a
1739syntactically valid variable name. 1755syntactically valid variable name.
1740 1756
1741\subsubsection*{\ff{\tt getglobal (name)}}\Deffunc{getglobal} 1757\subsubsection*{\ff {\tt getglobal (name)}}\Deffunc{getglobal}
1742This function retrieves the value of a global variable, 1758This function retrieves the value of a global variable,
1743or calls a tag method. 1759or calls a tag method.
1744Its full semantics is explained in \See{tag-method}. 1760Its full semantics is explained in \See{tag-method}.
1745 1761
1746\subsubsection*{\ff{\tt seterrormethod (newmethod)}} 1762\subsubsection*{\ff {\tt seterrormethod (newmethod)}}
1763\label{pdf-seterrormethod}
1747Sets the error handler \see{error}. 1764Sets the error handler \see{error}.
1748\verb|newmethod| must be a function or \nil, 1765\verb|newmethod| must be a function or \nil,
1749in which case the error handler does nothing. 1766in which case the error handler does nothing.
1750Returns the old handler. 1767Returns the old error handler.
1751 1768
1752\subsubsection*{\ff{\tt settagmethod (tag, event, newmethod)}} 1769\subsubsection*{\ff {\tt settagmethod (tag, event, newmethod)}}
1753\Deffunc{settagmethod} 1770\Deffunc{settagmethod}
1754This function sets a new tag method to the given pair $<tag, event>$. 1771This function sets a new tag method to the given pair $<tag, event>$.
1755It returns the old method. 1772It returns the old method.
1756If \verb|newmethod| is \nil, 1773If \verb|newmethod| is \nil,
1757it restores the default behavior for the given event. 1774it restores the default behavior for the given event.
1758 1775
1759\subsubsection*{\ff{\tt gettagmethod (tag, event)}} 1776\subsubsection*{\ff {\tt gettagmethod (tag, event)}}
1760\Deffunc{gettagmethod} 1777\Deffunc{gettagmethod}
1761This function returns the current tag method 1778This function returns the current tag method
1762for a given pair $<tag, event>$. 1779for a given pair $<tag, event>$.
@@ -1765,30 +1782,31 @@ for a given pair $<tag, event>$.
1765\subsection{String Manipulation} 1782\subsection{String Manipulation}
1766This library provides generic functions for string manipulation, 1783This library provides generic functions for string manipulation,
1767such as finding and extracting substrings and pattern matching. 1784such as finding and extracting substrings and pattern matching.
1768When indexing a string, the first character is at position 1, 1785When indexing a string, the first character is at position~1,
1769not 0, as in C. 1786not~0, as in C.
1770 1787
1771\subsubsection*{\ff{\tt strfind (str, pattern [, init [, plain]])}} 1788\subsubsection*{\ff {\tt strfind (str, pattern [, init [, plain]])}}
1772\Deffunc{strfind} 1789\Deffunc{strfind}
1773This function looks for the first {\em match\/} of 1790This function looks for the first {\em match\/} of
1774\verb|pattern| in \verb|str|. 1791\verb|pattern| in \verb|str|.
1775If it finds one, then it returns the indices on \verb|str| 1792If it finds one, then it returns the indices on \verb|str|
1776where this occurence starts and ends; 1793where this occurrence starts and ends;
1777otherwise, it returns \nil. 1794otherwise, it returns \nil.
1778If the pattern specifies captures, 1795If the pattern specifies captures,
1779the captured strings are returned as extra results. 1796the captured strings are returned as extra results.
1780A third optional numerical argument specifies where to start the search; 1797A third optional numerical argument specifies where to start the search;
1781its default value is 1. 1798its default value is 1.
1782A value of 1 as a forth optional argument 1799A value of 1 as a fourth optional argument
1783turns off the pattern matching facilities, 1800turns off the pattern matching facilities,
1784so the function does a plain ``find substring'' operation. 1801so the function does a plain ``find substring'' operation,
1802with no characters in \verb|pattern| being considered ``magic''.
1785 1803
1786\subsubsection*{\ff{\tt strlen (s)}}\Deffunc{strlen} 1804\subsubsection*{\ff {\tt strlen (s)}}\Deffunc{strlen}
1787Receives a string and returns its length. 1805Receives a string and returns its length.
1788 1806
1789\subsubsection*{\ff{\tt strsub (s, i [, j])}}\Deffunc{strsub} 1807\subsubsection*{\ff {\tt strsub (s, i [, j])}}\Deffunc{strsub}
1790Returns another string, which is a substring of \verb|s|, 1808Returns another string, which is a substring of \verb|s|,
1791starting at \verb|i| and runing until \verb|j|. 1809starting at \verb|i| and running until \verb|j|.
1792If \verb|i| or \verb|j| are negative, 1810If \verb|i| or \verb|j| are negative,
1793they are replaced by the length of the string minus their 1811they are replaced by the length of the string minus their
1794absolute value plus 1. 1812absolute value plus 1.
@@ -1802,25 +1820,25 @@ with length \verb|j|,
1802and the call \verb|strsub(s, -i)| returns a suffix of \verb|s| 1820and the call \verb|strsub(s, -i)| returns a suffix of \verb|s|
1803with length \verb|i|. 1821with length \verb|i|.
1804 1822
1805\subsubsection*{\ff{\tt strlower (s)}}\Deffunc{strlower} 1823\subsubsection*{\ff {\tt strlower (s)}}\Deffunc{strlower}
1806Receives a string and returns a copy of that string with all 1824Receives a string and returns a copy of that string with all
1807upper case letters changed to lower case. 1825upper case letters changed to lower case.
1808All other characters are left unchanged. 1826All other characters are left unchanged.
1809 1827
1810\subsubsection*{\ff{\tt strupper (s)}}\Deffunc{strupper} 1828\subsubsection*{\ff {\tt strupper (s)}}\Deffunc{strupper}
1811Receives a string and returns a copy of that string with all 1829Receives a string and returns a copy of that string with all
1812lower case letters changed to upper case. 1830lower case letters changed to upper case.
1813All other characters are left unchanged. 1831All other characters are left unchanged.
1814 1832
1815\subsubsection*{\ff{\tt strrep (s, n)}}\Deffunc{strrep} 1833\subsubsection*{\ff {\tt strrep (s, n)}}\Deffunc{strrep}
1816Returns a string which is the concatenation of \verb|n| copies of 1834Returns a string which is the concatenation of \verb|n| copies of
1817the string \verb|s|. 1835the string \verb|s|.
1818 1836
1819\subsubsection*{\ff{\tt ascii (s [, i])}}\Deffunc{ascii} 1837\subsubsection*{\ff {\tt ascii (s [, i])}}\Deffunc{ascii}
1820Returns the ASCII code of the character \verb|s[i]|. 1838Returns the ASCII code of the character \verb|s[i]|.
1821If \verb|i| is absent, then it is assumed to be 1. 1839If \verb|i| is absent, then it is assumed to be 1.
1822 1840
1823\subsubsection*{\ff{\tt format (formatstring, e1, e2, \ldots)}}\Deffunc{format} 1841\subsubsection*{\ff {\tt format (formatstring, e1, e2, \ldots)}}\Deffunc{format}
1824\label{format} 1842\label{format}
1825This function returns a formated version of its variable number of arguments 1843This function returns a formated version of its variable number of arguments
1826following the description given in its first argument (which must be a string). 1844following the description given in its first argument (which must be a string).
@@ -1855,7 +1873,8 @@ the appropriate format string.
1855For example, \verb|"%*g"| can be simulated with 1873For example, \verb|"%*g"| can be simulated with
1856\verb|"%"..width.."g"|. 1874\verb|"%"..width.."g"|.
1857 1875
1858\subsubsection*{\ff{\tt gsub (s, pat, repl [, n])}}\Deffunc{gsub} 1876\subsubsection*{\ff {\tt gsub (s, pat, repl [, table] [, n])}}
1877\Deffunc{gsub}
1859Returns a copy of \verb|s|, 1878Returns a copy of \verb|s|,
1860where all occurrences of the pattern \verb|pat| have been 1879where all occurrences of the pattern \verb|pat| have been
1861replaced by a replacement string specified by \verb|repl|. 1880replaced by a replacement string specified by \verb|repl|.
@@ -1868,38 +1887,53 @@ with \verb|n| between 1 and 9
1868stands for the value of the n-th captured substring. 1887stands for the value of the n-th captured substring.
1869 1888
1870If \verb|repl| is a function, then this function is called every time a 1889If \verb|repl| is a function, then this function is called every time a
1871match occurs, with all captured substrings as parameters 1890match occurs, with the following arguments:
1872(see below). 1891If \verb|table| is present, it is the first argument;
1892then all captured substrings, in order (see below);
1893finally, the last argument is a match counter
1894(1 for the first call).
1873If the value returned by this function is a string, 1895If the value returned by this function is a string,
1874then it is used as the replacement string; 1896then it is used as the replacement string;
1875otherwise, the replacement string is the empty string. 1897otherwise, the replacement string is the empty string.
1876 1898
1877An optional parameter \verb|n| limits 1899A last optional parameter \verb|n| limits
1878the maximum number of substitutions to occur. 1900the maximum number of substitutions to occur.
1879For instance, when \verb|n| is 1 only the first occurrence of 1901For instance, when \verb|n| is 1 only the first occurrence of
1880\verb|pat| is replaced. 1902\verb|pat| is replaced.
1881 1903
1882As an example, in the following expression each occurrence of the form 1904See some examples below:
1883\verb|$name| calls the function \verb|getenv|,
1884passing \verb|name| as argument
1885(because only this part of the pattern is captured).
1886The value returned by \verb|getenv| will replace the pattern.
1887Therefore, the whole expression:
1888\begin{verbatim} 1905\begin{verbatim}
1889 gsub("home = $HOME, user = $USER", "$(%w%w*)", getenv) 1906 x = gsub("hello world", "(%w%w*)", "%1 %1", 1)
1890\end{verbatim} 1907 --> x="hello hello world"
1891returns a string like: 1908
1892\begin{verbatim} 1909 x = gsub("home = $HOME, user = $USER", "$(%w%w*)", getenv)
1893home = /home/roberto, user = roberto 1910 --> x="home = /home/roberto, user = roberto" (for instance)
1911
1912 x = gsub("4+5 = $return 4+5$", "$(.-)%$", dostring)
1913 --> x="4+5 = 9"
1914
1915 t = {name="lua", version="3.0"}
1916 x = gsub("$name - $version", "$(%w%w*)", rawgettable, t)
1917 --> x="lua - 3.0"
1918
1919 t = {"apple", "orange", "lime"}
1920 x = gsub("x and x and x", "x", rawgettable, t)
1921 --> x="apple and orange and lime"
1922
1923 function f(t,v,i) t[i]=v end
1924 t = {}
1925 gsub("first second word", "(%w%w*)", f, t)
1926 --> t={"first", "second", "word"}
1894\end{verbatim} 1927\end{verbatim}
1895 1928
1929
1896\subsubsection*{Patterns} \label{pm} 1930\subsubsection*{Patterns} \label{pm}
1897 1931
1898\paragraph{Character Class:} 1932\paragraph{Character Class:}
1899a \Def{character class} is used to represent a set of characters. 1933a \Def{character class} is used to represent a set of characters.
1900The following combinations are allowed in describing a character class: 1934The following combinations are allowed in describing a character class:
1901\begin{description} 1935\begin{description}
1902\item[{\em x}] (where {\em x} is any character not in the list \verb|()%.[*?|) 1936\item[{\em x}] (where {\em x} is any character not in the list \verb|()%.[*-?|)
1903--- represents the character {\em x} itself. 1937--- represents the character {\em x} itself.
1904\item[{\tt .}] --- represents all characters. 1938\item[{\tt .}] --- represents all characters.
1905\item[{\tt \%a}] --- represents all letters. 1939\item[{\tt \%a}] --- represents all letters.
@@ -1916,7 +1950,7 @@ The following combinations are allowed in describing a character class:
1916\item[{\tt \%W}] --- represents all non alphanumeric characters. 1950\item[{\tt \%W}] --- represents all non alphanumeric characters.
1917\item[{\tt \%\M{x}}] (where \M{x} is any non alphanumeric character) --- 1951\item[{\tt \%\M{x}}] (where \M{x} is any non alphanumeric character) ---
1918represents the character \M{x}. 1952represents the character \M{x}.
1919This is the standard way to escape the magic characters \verb|()%.[*?|. 1953This is the standard way to escape the magic characters \verb|()%.[*-?|.
1920\item[{\tt [char-set]}] --- 1954\item[{\tt [char-set]}] ---
1921Represents the class which is the union of all 1955Represents the class which is the union of all
1922characters in char-set. 1956characters in char-set.
@@ -1943,12 +1977,12 @@ which matches any single character in the class;
1943\item 1977\item
1944a single character class followed by \verb|*|, 1978a single character class followed by \verb|*|,
1945which matches 0 or more repetitions of characters in the class. 1979which matches 0 or more repetitions of characters in the class.
1946These repetition itens will always match the longest possible sequence. 1980These repetition items will always match the longest possible sequence.
1947\item 1981\item
1948a single character class followed by \verb|-|, 1982a single character class followed by \verb|-|,
1949which also matches 0 or more repetitions of characters in the class. 1983which also matches 0 or more repetitions of characters in the class.
1950Unlike \verb|*|, 1984Unlike \verb|*|,
1951these repetition itens will always match the shortest possible sequence. 1985these repetition items will always match the shortest possible sequence.
1952\item 1986\item
1953a single character class followed by \verb|?|, 1987a single character class followed by \verb|?|,
1954which matches 0 or 1 occurrence of a character in the class; 1988which matches 0 or 1 occurrence of a character in the class;
@@ -1958,12 +1992,12 @@ such item matches a sub-string equal to the n-th captured string
1958(see below); 1992(see below);
1959\item 1993\item
1960{\tt \%b\M{xy}}, where \M{x} and \M{y} are two distinct characters; 1994{\tt \%b\M{xy}}, where \M{x} and \M{y} are two distinct characters;
1961such item mathes strings that start with \M{x}, end with \M{y}, 1995such item matches strings that start with \M{x}, end with \M{y},
1962and where the \M{x} and \M{y} are {\em balanced}. 1996and where the \M{x} and \M{y} are {\em balanced}.
1963That means that, if one reads the string from left to write, 1997That means that, if one reads the string from left to write,
1964counting plus 1 for an \M{x} and minus 1 for a \M{y}, 1998counting plus 1 for an \M{x} and minus 1 for a \M{y},
1965the ending \M{y} is the first where the count reaches 0. 1999the ending \M{y} is the first where the count reaches 0.
1966For instance, the item \verb|%()| matches expressions with 2000For instance, the item \verb|%b()| matches expressions with
1967balanced parentheses. 2001balanced parentheses.
1968\end{itemize} 2002\end{itemize}
1969 2003
@@ -2021,7 +2055,7 @@ range \M{[0,1)}.
2021 2055
2022\subsection{I/O Facilities} \label{libio} 2056\subsection{I/O Facilities} \label{libio}
2023 2057
2024All input and outpu operations in Lua are done over two {\em current\/} files: 2058All input and output operations in Lua are done over two {\em current\/} files:
2025one for reading and one for writing. 2059one for reading and one for writing.
2026Initially, the current input file is \verb|stdin|, 2060Initially, the current input file is \verb|stdin|,
2027and the current output file is \verb|stdout|. 2061and the current output file is \verb|stdout|.
@@ -2030,16 +2064,16 @@ Unless otherwise stated,
2030all I/O functions return \nil\ on failure and 2064all I/O functions return \nil\ on failure and
2031some value different from \nil\ on success. 2065some value different from \nil\ on success.
2032 2066
2033\subsubsection*{\ff{\tt readfrom (filename)}}\Deffunc{readfrom} 2067\subsubsection*{\ff {\tt readfrom (filename)}}\Deffunc{readfrom}
2034 2068
2035This function may be called in three ways. 2069This function may be called in three ways.
2036When called with a file name, 2070When called with a file name,
2037it opens the named file, 2071it opens the named file,
2038sets it as the {\em current\/} input file, 2072sets it as the {\em current\/} input file,
2039and returns a {\em handle\/} to the file 2073and returns a {\em handle\/} to the file
2040(this handle is a user data containing the file stream \verb|FILE*|). 2074(this handle is a userdata containing the file stream \verb|FILE*|).
2041It does not close the current input file. 2075It does not close the current input file.
2042When called with a file handle, returned by a previous call, 2076When called with a file handle returned by a previous call,
2043it restores the file as the current input. 2077it restores the file as the current input.
2044When called without parameters, 2078When called without parameters,
2045it closes the current input file, 2079it closes the current input file,
@@ -2058,7 +2092,7 @@ the number of files that can be open at the same time is usually limited and
2058depends on the system. 2092depends on the system.
2059\end{quotation} 2093\end{quotation}
2060 2094
2061\subsubsection*{\ff{\tt writeto (filename)}}\Deffunc{writeto} 2095\subsubsection*{\ff {\tt writeto (filename)}}\Deffunc{writeto}
2062 2096
2063This function may be called in three ways. 2097This function may be called in three ways.
2064When called with a file name, 2098When called with a file name,
@@ -2068,8 +2102,8 @@ and returns a {\em handle\/} to the file
2068(this handle is a user data containing the file stream \verb|FILE*|). 2102(this handle is a user data containing the file stream \verb|FILE*|).
2069It does not close the current output file. 2103It does not close the current output file.
2070Notice that, if the file already exists, 2104Notice that, if the file already exists,
2071it will be {\em completely erased\/} with this operation. 2105then it will be {\em completely erased\/} with this operation.
2072When called with a file handle, returned by a previous call, 2106When called with a file handle returned by a previous call,
2073it restores the file as the current output. 2107it restores the file as the current output.
2074When called without parameters, 2108When called without parameters,
2075this function closes the current output file, 2109this function closes the current output file,
@@ -2090,7 +2124,7 @@ the number of files that can be open at the same time is usually limited and
2090depends on the system. 2124depends on the system.
2091\end{quotation} 2125\end{quotation}
2092 2126
2093\subsubsection*{\ff{\tt appendto (filename)}}\Deffunc{appendto} 2127\subsubsection*{\ff {\tt appendto (filename)}}\Deffunc{appendto}
2094 2128
2095This function opens a file named \verb|filename| and sets it as the 2129This function opens a file named \verb|filename| and sets it as the
2096{\em current\/} output file. 2130{\em current\/} output file.
@@ -2103,24 +2137,25 @@ plus a string describing the error.
2103 2137
2104Notice that function \verb|writeto| is available to close an output file. 2138Notice that function \verb|writeto| is available to close an output file.
2105 2139
2106\subsubsection*{\ff{\tt remove (filename)}}\Deffunc{remove} 2140\subsubsection*{\ff {\tt remove (filename)}}\Deffunc{remove}
2107 2141
2108This function deletes the file with the given name. 2142This function deletes the file with the given name.
2109If this function fails, it returns \nil, 2143If this function fails, it returns \nil,
2110plus a string describing the error. 2144plus a string describing the error.
2111 2145
2112\subsubsection*{\ff{\tt rename (name1, name2)}}\Deffunc{rename} 2146\subsubsection*{\ff {\tt rename (name1, name2)}}\Deffunc{rename}
2113 2147
2114This function renames file named \verb|name1| to \verb|name2|. 2148This function renames file named \verb|name1| to \verb|name2|.
2115If this function fails, it returns \nil, 2149If this function fails, it returns \nil,
2116plus a string describing the error. 2150plus a string describing the error.
2117 2151
2118\subsubsection*{\ff{\tt tmpname ()}}\Deffunc{tmpname} 2152\subsubsection*{\ff {\tt tmpname ()}}\Deffunc{tmpname}
2119 2153
2120This function returns a string with a file name that can safely 2154This function returns a string with a file name that can safely
2121be used for a temporary file. 2155be used for a temporary file.
2156The file must be explicitly removed when no longer needed.
2122 2157
2123\subsubsection*{\ff{\tt read ([readpattern])}}\Deffunc{read} 2158\subsubsection*{\ff {\tt read ([readpattern])}}\Deffunc{read}
2124 2159
2125This function reads the current input 2160This function reads the current input
2126according to a read pattern, that specifies how much to read; 2161according to a read pattern, that specifies how much to read;
@@ -2173,7 +2208,7 @@ or \nil\ on end of file.
2173or \nil\ if the next characters do not conform to an integer format. 2208or \nil\ if the next characters do not conform to an integer format.
2174\end{itemize} 2209\end{itemize}
2175 2210
2176\subsubsection*{\ff{\tt write (value1, ...)}}\Deffunc{write} 2211\subsubsection*{\ff {\tt write (value1, ...)}}\Deffunc{write}
2177 2212
2178This function writes the value of each of its arguments to the 2213This function writes the value of each of its arguments to the
2179current output file. 2214current output file.
@@ -2183,7 +2218,7 @@ use \verb|tostring| before \verb|write|.
2183If this function fails, it returns \nil, 2218If this function fails, it returns \nil,
2184plus a string describing the error. 2219plus a string describing the error.
2185 2220
2186\subsubsection*{\ff{\tt date ([format])}}\Deffunc{date} 2221\subsubsection*{\ff {\tt date ([format])}}\Deffunc{date}
2187 2222
2188This function returns a string containing date and time 2223This function returns a string containing date and time
2189formatted according to the given string \verb|format|, 2224formatted according to the given string \verb|format|,
@@ -2192,19 +2227,19 @@ When called without arguments,
2192it returns a reasonable date and time representation that depends on 2227it returns a reasonable date and time representation that depends on
2193the host system. 2228the host system.
2194 2229
2195\subsubsection*{\ff{\tt exit ([code])}}\Deffunc{exit} 2230\subsubsection*{\ff {\tt exit ([code])}}\Deffunc{exit}
2196 2231
2197This function calls the C function \verb|exit|, 2232This function calls the C function \verb|exit|,
2198with an optional \verb|code|, 2233with an optional \verb|code|,
2199to terminate the program. 2234to terminate the program.
2200The default value for \verb|code| is 1. 2235The default value for \verb|code| is 1.
2201 2236
2202\subsubsection*{\ff{\tt getenv (varname)}}\Deffunc{getenv} 2237\subsubsection*{\ff {\tt getenv (varname)}}\Deffunc{getenv}
2203 2238
2204Returns the value of the environment variable \verb|varname|, 2239Returns the value of the environment variable \verb|varname|,
2205or \nil\ if the variable is not defined. 2240or \nil\ if the variable is not defined.
2206 2241
2207\subsubsection*{\ff{\tt execute (command)}}\Deffunc{execute} 2242\subsubsection*{\ff {\tt execute (command)}}\Deffunc{execute}
2208 2243
2209This function is equivalent to the C function \verb|system|. 2244This function is equivalent to the C function \verb|system|.
2210It passes \verb|command| to be executed by an operating system shell. 2245It passes \verb|command| to be executed by an operating system shell.
@@ -2301,7 +2336,6 @@ or if the activation record has no debug information,
2301Formal parameters are the first local variables. 2336Formal parameters are the first local variables.
2302 2337
2303The function \verb|lua_setlocal| sets the local variable 2338The function \verb|lua_setlocal| sets the local variable
2304%%LHF: please, lua_setglobal!
2305\verb|local_number| to the value previously pushed on the stack 2339\verb|local_number| to the value previously pushed on the stack
2306\see{valuesCLua}. 2340\see{valuesCLua}.
2307If the function succeeds, then it returns 1. 2341If the function succeeds, then it returns 1.