aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-08-28 17:46:26 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-08-28 17:46:26 -0300
commit15791f93fed507f73ae58aa89b3e8770b4d69406 (patch)
treeb0df8cead7535d86b69c3a94e55bc0863845fc34
parentd763b69740653e68aaddd72edf0bc0a34544415a (diff)
downloadlua-15791f93fed507f73ae58aa89b3e8770b4d69406.tar.gz
lua-15791f93fed507f73ae58aa89b3e8770b4d69406.tar.bz2
lua-15791f93fed507f73ae58aa89b3e8770b4d69406.zip
lhf's corrections.
-rw-r--r--manual.tex332
1 files changed, 166 insertions, 166 deletions
diff --git a/manual.tex b/manual.tex
index b634171b..b69805ae 100644
--- a/manual.tex
+++ b/manual.tex
@@ -1,4 +1,4 @@
1% $Id: manual.tex,v 1.17 1996/04/29 18:50:08 roberto Exp roberto $ 1% $Id: manual.tex,v 1.18 1996/06/18 20:08:40 roberto Exp roberto $
2 2
3\documentstyle[fullpage,11pt,bnf]{article} 3\documentstyle[fullpage,11pt,bnf]{article}
4 4
@@ -28,13 +28,13 @@ Waldemar Celes Filho
28\vspace{1.0ex}\\ 28\vspace{1.0ex}\\
29%\small \tecgraf \ --- PUC-Rio\\ 29%\small \tecgraf \ --- PUC-Rio\\
30\smallskip 30\smallskip
31\small\tt roberto,lhf,celes@icad.puc-rio.br 31\small\tt lua@icad.puc-rio.br
32\vspace{2.0ex}\\ 32\vspace{2.0ex}\\
33%MCC 08/95 --- 33%MCC 08/95 ---
34\tecgraf\ --- Departamento de Inform\'atica --- PUC-Rio 34\tecgraf\ --- Departamento de Inform\'atica --- PUC-Rio
35} 35}
36 36
37\date{\small \verb$Date: 1996/04/29 18:50:08 $} 37\date{\small \verb$Date: 1996/06/18 20:08:40 $}
38 38
39\maketitle 39\maketitle
40 40
@@ -42,9 +42,10 @@ Waldemar Celes Filho
42\noindent 42\noindent
43Lua is an extension programming language designed to be used 43Lua is an extension programming language designed to be used
44as a configuration language for any program that needs one. 44as a configuration language for any program that needs one.
45This document describes Version \Version\ of the Lua programming language and 45This document describes version \Version\ of the Lua programming language and
46the API that allows interaction between Lua programs and its host C program. 46the API that allows interaction between Lua programs and their host C programs.
47It also presents some examples of using the main features of the system. 47The document also presents some examples of using the main
48features of the system.
48\end{abstract} 49\end{abstract}
49 50
50\vspace{4ex} 51\vspace{4ex}
@@ -57,8 +58,8 @@ Lua \'e uma linguagem de extens\~ao projetada para ser usada como
57linguagem de configura\c{c}\~ao em qualquer programa que precise de 58linguagem de configura\c{c}\~ao em qualquer programa que precise de
58uma. 59uma.
59Este documento descreve a vers\~ao \Version\ da linguagem de 60Este documento descreve a vers\~ao \Version\ da linguagem de
60programa\c{c}\~ao Lua e a Interface de Programa\c{c}\~ao que permite 61programa\c{c}\~ao Lua e a Interface de Programa\c{c}\~ao (API) que permite
61a intera\c{c}\~ao entre programas Lua e o programa C hospedeiro. 62a intera\c{c}\~ao entre programas Lua e programas C hospedeiros.
62O documento tamb\'em apresenta alguns exemplos de uso das principais 63O documento tamb\'em apresenta alguns exemplos de uso das principais
63ca\-racte\-r\'{\i}sticas do sistema. 64ca\-racte\-r\'{\i}sticas do sistema.
64\end{quotation} 65\end{quotation}
@@ -69,7 +70,7 @@ ca\-racte\-r\'{\i}sticas do sistema.
69Lua is an extension programming language designed to support 70Lua is an extension programming language designed to support
70general procedural programming features with data description 71general procedural programming features with data description
71facilities. 72facilities.
72It is supposed to be used as a configuration language for any 73It is intended to be used as a configuration language for any
73program that needs one. 74program that needs one.
74%Its main extensions are related to object-oriented facilities, 75%Its main extensions are related to object-oriented facilities,
75%and fallbacks, 76%and fallbacks,
@@ -88,16 +89,13 @@ Through the use of C functions, Lua can be augmented to cope with
88rather different domains, 89rather different domains,
89thus creating customized programming languages sharing a syntactical framework. 90thus creating customized programming languages sharing a syntactical framework.
90 91
91Lua is free distribution software, 92Lua is free-distribution software,
92and provided as usual with no guarantees. 93and provided as usual with no guarantees.
93The implementation described in this manual is available 94The implementation described in this manual is available
94by anonymous ftp from 95at the following URL's:
95\begin{verbatim}
96 ftp.icad.puc-rio.br:/pub/lua/lua.tar.gz
97\end{verbatim}
98or by WWW (World Wide Web) from
99\begin{verbatim} 96\begin{verbatim}
100 http://www.inf.puc-rio.br/~roberto/lua.html 97 http://www.inf.puc-rio.br/~roberto/lua.html
98 ftp://ftp.icad.puc-rio.br/pub/lua/lua.tar.gz
101\end{verbatim} 99\end{verbatim}
102 100
103 101
@@ -115,14 +113,15 @@ using functions in the library that implements Lua.
115 113
116\Index{Global variables} do not need declaration. 114\Index{Global variables} do not need declaration.
117Any variable is assumed to be global unless explicitly declared local 115Any variable is assumed to be global unless explicitly declared local
118(see local declarations, Section~\ref{localvar}). 116(\see{localvar}).
119Before the first assignment, the value of a global variable is \nil. 117Before the first assignment, the value of a global variable is \nil.
120 118
121The unit of execution of Lua is called a \Def{chunk}. 119The unit of execution of Lua is called a \Def{chunk}.
122The syntax for chunks is:% 120The syntax%
123\footnote{As usual, \rep{{\em a}} means 0 or more {\em a\/}'s, 121\footnote{As usual, \rep{{\em a}} means 0 or more {\em a\/}'s,
124\opt{{\em a}} means an optional {\em a} and \oneormore{{\em a}} means 122\opt{{\em a}} means an optional {\em a} and \oneormore{{\em a}} means
125one or more {\em a\/}'s.} 123one or more {\em a\/}'s.}
124for chunks is:
126\begin{Produc} 125\begin{Produc}
127\produc{chunk}{\rep{statement \Or function}} 126\produc{chunk}{\rep{statement \Or function}}
128\end{Produc}% 127\end{Produc}%
@@ -138,7 +137,7 @@ of new functions%
138assignment to a global variable; \see{TypesSec}.}. 137assignment to a global variable; \see{TypesSec}.}.
139 138
140Chunks may be pre-compiled; see program \IndexVerb{luac} for details. 139Chunks may be pre-compiled; see program \IndexVerb{luac} for details.
141Ascii files with chunk code and their binary pre-compiled forms 140Text files with chunks and their binary pre-compiled forms
142are interchangeable. 141are interchangeable.
143Lua automatically detects the file type and acts accordingly. 142Lua automatically detects the file type and acts accordingly.
144\index{pre-compilation} 143\index{pre-compilation}
@@ -174,7 +173,7 @@ However, by using fallbacks, the programmer may define operations
174for {\em userdata} values; \see{fallback}. 173for {\em userdata} values; \see{fallback}.
175 174
176The type {\em table} implements \Index{associative arrays}, 175The type {\em table} implements \Index{associative arrays},
177that is, \Index{arrays} which can be indexed not only with numbers, 176that is, \Index{arrays} that can be indexed not only with numbers,
178but with any value (except \nil). 177but with any value (except \nil).
179Therefore, this type may be used not only to represent ordinary arrays, 178Therefore, this type may be used not only to represent ordinary arrays,
180but also symbol tables, sets, records, etc. 179but also symbol tables, sets, records, etc.
@@ -192,14 +191,14 @@ It is important to notice that tables are objects, and not values.
192Variables cannot contain tables, only references to them. 191Variables cannot contain tables, only references to them.
193Assignment, parameter passing and returns always manipulate references 192Assignment, parameter passing and returns always manipulate references
194to tables, and do not imply any kind of copy. 193to tables, and do not imply any kind of copy.
195Moreover, tables must be explicitly created before used; 194Moreover, tables must be explicitly created before used
196\see{tableconstructor}. 195(\see{tableconstructor}).
197 196
198 197
199 198
200\section{The Language} 199\section{The Language}
201 200
202This section describes the lexis, syntax and semantics of Lua. 201This section describes the lexis, the syntax and the semantics of Lua.
203 202
204 203
205\subsection{Lexical Conventions} \label{lexical} 204\subsection{Lexical Conventions} \label{lexical}
@@ -210,9 +209,10 @@ not beginning with a digit.
210The following words are reserved, and cannot be used as identifiers: 209The following words are reserved, and cannot be used as identifiers:
211\index{reserved words} 210\index{reserved words}
212\begin{verbatim} 211\begin{verbatim}
213 and do else elseif end 212 and do else elseif
214 function if local nil not 213 end function if local
215 or repeat return until then while 214 nil not or repeat
215 return then until while
216\end{verbatim} 216\end{verbatim}
217 217
218The following strings denote other \Index{tokens}: 218The following strings denote other \Index{tokens}:
@@ -225,8 +225,8 @@ The following strings denote other \Index{tokens}:
225and can contain the C-like escape sequences 225and can contain the C-like escape sequences
226\verb-'\n'-, \verb-'\t'- and \verb-'\r'-. 226\verb-'\n'-, \verb-'\t'- and \verb-'\r'-.
227Literal strings can also be delimited by matching \verb'[[ ... ]]'. 227Literal strings can also be delimited by matching \verb'[[ ... ]]'.
228Literals in this last form may run for several lines, 228Literals in this bracketed form may run for several lines,
229may contain nested \verb'[[ ... ]]', 229may contain nested \verb'[[ ... ]]' pairs,
230and do not interpret escape sequences. 230and do not interpret escape sequences.
231 231
232\Index{Comments} start anywhere outside a string with a 232\Index{Comments} start anywhere outside a string with a
@@ -248,8 +248,9 @@ that string to a number, following the usual rules.
248Conversely, whenever a number is used when a string is expected, 248Conversely, whenever a number is used when a string is expected,
249that number is converted to a string, according to the following rule: 249that number is converted to a string, according to the following rule:
250if the number is an integer, it is written without exponent or decimal point; 250if the number is an integer, it is written without exponent or decimal point;
251otherwise, it is formatted following the ``\verb'%g''' 251otherwise, it is formatted following the \verb'%g'
252conversion specification of the standard \verb'printf' C function. 252conversion specification of the \verb'printf' function in the
253standard C library.
253 254
254 255
255 256
@@ -261,8 +262,8 @@ the system does not know how many values a function will return,
261or how many parameters it needs. 262or how many parameters it needs.
262Therefore, sometimes, a list of values must be {\em adjusted\/}, at run time, 263Therefore, sometimes, a list of values must be {\em adjusted\/}, at run time,
263to a given length. 264to a given length.
264If there are more values than are needed, the last values are thrown away. 265If there are more values than are needed, then the last values are thrown away.
265If there are more needs than values, the list is extended with as 266If there are more needs than values, then the list is extended with as
266many \nil's as needed. 267many \nil's as needed.
267Adjustment occurs in multiple assignment and function calls. 268Adjustment occurs in multiple assignment and function calls.
268 269
@@ -272,18 +273,18 @@ Adjustment occurs in multiple assignment and function calls.
272Lua supports an almost conventional set of \Index{statements}. 273Lua supports an almost conventional set of \Index{statements}.
273The conventional commands include 274The conventional commands include
274assignment, control structures and procedure calls. 275assignment, control structures and procedure calls.
275Non-conventional commands include table constructors, 276Non-conventional commands include table constructors
276explained in Section \ref{tableconstructor}, 277(Section~\ref{tableconstructor}),
277and local variable declarations. 278and local variable declarations (Section~\ref{localvar}).
278 279
279\subsubsection{Blocks} 280\subsubsection{Blocks}
280A \Index{block} is a list of statements, executed sequentially. 281A \Index{block} is a list of statements, which is executed sequentially.
281Any statement can be optionally followed by a semicolon. 282Any statement can be optionally followed by a semicolon:
282\begin{Produc} 283\begin{Produc}
283\produc{block}{\rep{stat sc} \opt{ret sc}} 284\produc{block}{\rep{stat sc} \opt{ret sc}}
284\produc{sc}{\opt{\ter{;}}} 285\produc{sc}{\opt{\ter{;}}}
285\end{Produc}% 286\end{Produc}%
286For syntactic reasons, a \Index{return statement} can only be written 287For syntactic reasons, a \IndexVerb{return} statement can only be written
287as the last statement of a block. 288as the last statement of a block.
288This restriction also avoids some ``statement not reached'' errors. 289This restriction also avoids some ``statement not reached'' errors.
289 290
@@ -291,7 +292,7 @@ This restriction also avoids some ``statement not reached'' errors.
291The language allows \Index{multiple assignment}. 292The language allows \Index{multiple assignment}.
292Therefore, the syntax defines a list of variables on the left side, 293Therefore, the syntax defines a list of variables on the left side,
293and a list of expressions on the right side. 294and a list of expressions on the right side.
294Both lists have their elements separated by commas. 295Both lists have their elements separated by commas:
295\begin{Produc} 296\begin{Produc}
296\produc{stat}{varlist1 \ter{=} explist1} 297\produc{stat}{varlist1 \ter{=} explist1}
297\produc{varlist1}{var \rep{\ter{,} var}} 298\produc{varlist1}{var \rep{\ter{,} var}}
@@ -304,33 +305,34 @@ Therefore, it can be used to exchange two values, as in
304 x, y = y, x 305 x, y = y, x
305\end{verbatim} 306\end{verbatim}
306Before the assignment, the list of values is {\em adjusted} to 307Before the assignment, the list of values is {\em adjusted} to
307the length of the list of variables; \see{adjust}. 308the length of the list of variables (\see{adjust}).
308 309
310A single name can denote a global or a local variable,
311or a formal parameter:
309\begin{Produc} 312\begin{Produc}
310\produc{var}{name} 313\produc{var}{name}
311\end{Produc}% 314\end{Produc}%
312A single name can denote a global or a local variable, 315Square brackets are used to index a table:
313or a formal parameter.
314\begin{Produc} 316\begin{Produc}
315\produc{var}{var \ter{[} exp1 \ter{]}} 317\produc{var}{var \ter{[} exp1 \ter{]}}
316\end{Produc}% 318\end{Produc}%
317Square brackets are used to index a table.
318If \verb'var' results in a table value, 319If \verb'var' results in a table value,
319the field indexed by the expression value gets the assigned value. 320the field indexed by the expression value gets the assigned value.
320Otherwise, the fallback {\em settable} is called, 321Otherwise, the fallback {\em settable} is called,
321with three parameters: the value of \verb'var', 322with three parameters: the value of \verb'var',
322the value of expression, and the value being assigned to it; 323the value of expression, and the value being assigned to it;
323\see{fallback}. 324\see{fallback}.
325
326The syntax \verb'var.NAME' is just syntactic sugar for
327\verb'var["NAME"]'.
324\begin{Produc} 328\begin{Produc}
325\produc{var}{var \ter{.} name} 329\produc{var}{var \ter{.} name}
326\end{Produc}% 330\end{Produc}%
327The syntax \verb'var.NAME' is just syntactic sugar for
328\verb'var["NAME"]'.
329 331
330\subsubsection{Control Structures} 332\subsubsection{Control Structures}
331The \Index{condition expression} of a control structure can return any value. 333The \Index{condition expression} of a control structure can return any value.
332All values different from \nil\ are considered true, 334All values different from \nil\ are considered true;
333while \nil\ is considered false. 335\nil\ is considered false.
334{\tt if}'s, {\tt while}'s and {\tt repeat}'s have the usual meaning. 336{\tt if}'s, {\tt while}'s and {\tt repeat}'s have the usual meaning.
335 337
336\index{while-do}\index{repeat-until}\index{if-then-else} 338\index{while-do}\index{repeat-until}\index{if-then-else}
@@ -351,7 +353,7 @@ the syntax for a \Index{return statement} is:
351 353
352\subsubsection{Function Calls as Statements} \label{funcstat} 354\subsubsection{Function Calls as Statements} \label{funcstat}
353Because of possible side-effects, 355Because of possible side-effects,
354function calls can be executed as statements. 356function calls can be executed as statements:
355\begin{Produc} 357\begin{Produc}
356\produc{stat}{functioncall} 358\produc{stat}{functioncall}
357\end{Produc}% 359\end{Produc}%
@@ -368,7 +370,7 @@ The declaration may include an initial assignment:
368\produc{declist}{name \rep{\ter{,} name}} 370\produc{declist}{name \rep{\ter{,} name}}
369\produc{init}{\ter{=} explist1} 371\produc{init}{\ter{=} explist1}
370\end{Produc}% 372\end{Produc}%
371If there is an initial assignment, it has the same semantics 373If present, an initial assignment has the same semantics
372of a multiple assignment. 374of a multiple assignment.
373Otherwise, all variables are initialized with \nil. 375Otherwise, all variables are initialized with \nil.
374 376
@@ -395,23 +397,23 @@ These operators are the binary
395and the unary \verb'-'. 397and the unary \verb'-'.
396If the operands are numbers, or strings that can be converted to 398If the operands are numbers, or strings that can be converted to
397numbers, according to the rules given in Section \ref{coercion}, 399numbers, according to the rules given in Section \ref{coercion},
398all operations but exponentiation have the usual meaning. 400then all operations but exponentiation have the usual meaning.
399Otherwise, the fallback ``arith'' is called; \see{fallback}. 401Otherwise, the fallback ``arith'' is called (\see{fallback}).
400An exponentiation always calls this fallback. 402An exponentiation always calls this fallback.
401The standard mathematical library redefines this fallback, 403The standard mathematical library redefines this fallback,
402giving the expected meaning to \Index{exponentiation}; 404giving the expected meaning to \Index{exponentiation}
403\see{mathlib}. 405(\see{mathlib}).
404 406
405\subsubsection{Relational Operators} 407\subsubsection{Relational Operators}
406Lua offers the following \Index{relational operators}: 408Lua provides the following \Index{relational operators}:
407\begin{verbatim} 409\begin{verbatim}
408 < > <= >= ~= == 410 < > <= >= ~= ==
409\end{verbatim} 411\end{verbatim}
410All return \nil\ as false and a value different from \nil\ 412All these return \nil\ as false and a value different from \nil\
411(actually the number 1) as true. 413(actually the number 1) as true.
412 414
413Equality first compares the types of its operands. 415Equality first compares the types of its operands.
414If they are different, the result is \nil. 416If they are different, then the result is \nil.
415Otherwise, their values are compared. 417Otherwise, their values are compared.
416Numbers and strings are compared in the usual way. 418Numbers and strings are compared in the usual way.
417Tables, CFunctions, and functions are compared by reference, 419Tables, CFunctions, and functions are compared by reference,
@@ -419,13 +421,13 @@ that is, two tables are considered equal only if they are the same table.
419The operator \verb'~=' is exactly the negation of equality (\verb'=='). 421The operator \verb'~=' is exactly the negation of equality (\verb'==').
420 422
421The other operators work as follows. 423The other operators work as follows.
422If both arguments are numbers, they are compared as such. 424If both arguments are numbers, then they are compared as such.
423Otherwise, if both arguments can be converted to strings, 425Otherwise, if both arguments can be converted to strings,
424their values are compared using lexicographical order. 426their values are compared using lexicographical order.
425Otherwise, the fallback ``order'' is called; \see{fallback}. 427Otherwise, the ``order'' fallback is called (\see{fallback}).
426 428
427\subsubsection{Logical Operators} 429\subsubsection{Logical Operators}
428All logical operators, like control structures, 430Like control structures, all logical operators
429consider \nil\ as false and anything else as true. 431consider \nil\ as false and anything else as true.
430The \Index{logical operators} are: 432The \Index{logical operators} are:
431\index{and}\index{or}\index{not} 433\index{and}\index{or}\index{not}
@@ -439,9 +441,9 @@ the second operand is evaluated only if necessary.
439\subsubsection{Concatenation} 441\subsubsection{Concatenation}
440Lua offers a string \Index{concatenation} operator, 442Lua offers a string \Index{concatenation} operator,
441denoted by ``\IndexVerb{..}''. 443denoted by ``\IndexVerb{..}''.
442If operands are strings or numbers, they are converted to 444If operands are strings or numbers, then they are converted to
443strings according to the rules in Section \ref{coercion}. 445strings according to the rules in Section \ref{coercion}.
444Otherwise, the fallback ``concat'' is called; \see{fallback}. 446Otherwise, the fallback ``concat'' is called (\see{fallback}).
445 447
446\subsubsection{Precedence} 448\subsubsection{Precedence}
447\Index{Operator precedence} follows the table below, 449\Index{Operator precedence} follows the table below,
@@ -455,7 +457,8 @@ from the lower to the higher priority:
455 not - (unary) 457 not - (unary)
456 ^ 458 ^
457\end{verbatim} 459\end{verbatim}
458All binary operators are left associative, except for \verb'^', 460All binary operators are left associative,
461except for \verb'^' (exponentiation),
459which is right associative. 462which is right associative.
460 463
461\subsubsection{Table Constructors} \label{tableconstructor} 464\subsubsection{Table Constructors} \label{tableconstructor}
@@ -478,7 +481,7 @@ The form {\em lfieldlist1} is used to initialize lists.
478\end{Produc}% 481\end{Produc}%
479The expressions in the list are assigned to consecutive numerical indexes, 482The expressions in the list are assigned to consecutive numerical indexes,
480starting with 1. 483starting with 1.
481As an example: 484For example:
482\begin{verbatim} 485\begin{verbatim}
483 a = {"v1", "v2", 34} 486 a = {"v1", "v2", 34}
484\end{verbatim} 487\end{verbatim}
@@ -491,12 +494,12 @@ is equivalent to:
491 a = temp 494 a = temp
492\end{verbatim} 495\end{verbatim}
493 496
494The next form initializes named fields in a table. 497The next form initializes named fields in a table:
495\begin{Produc} 498\begin{Produc}
496\produc{ffieldlist1}{ffield \rep{\ter{,} ffield} \opt{\ter{,}}} 499\produc{ffieldlist1}{ffield \rep{\ter{,} ffield} \opt{\ter{,}}}
497\produc{ffield}{name \ter{=} exp} 500\produc{ffield}{name \ter{=} exp}
498\end{Produc}% 501\end{Produc}%
499As an example: 502For example:
500\begin{verbatim} 503\begin{verbatim}
501 a = {x = 1, y = 3} 504 a = {x = 1, y = 3}
502\end{verbatim} 505\end{verbatim}
@@ -516,8 +519,8 @@ A \Index{function call} has the following syntax:
516\end{Produc}% 519\end{Produc}%
517Here, \verb'var' can be any variable (global, local, indexed, etc). 520Here, \verb'var' can be any variable (global, local, indexed, etc).
518If its type is {\em function\/} or {\em CFunction\/}, 521If its type is {\em function\/} or {\em CFunction\/},
519this function is called. 522then this function is called.
520Otherwise, the fallback ``function'' is called, 523Otherwise, the ``function'' fallback is called,
521having as first parameter the value of \verb'var', 524having as first parameter the value of \verb'var',
522and then the original call parameters. 525and then the original call parameters.
523 526
@@ -553,10 +556,10 @@ If the function is called as a statement (\see{funcstat}),
553its return list is adjusted to 0. 556its return list is adjusted to 0.
554If the function is called in a place that needs a single value 557If the function is called in a place that needs a single value
555(syntactically denoted by the non-terminal \verb'exp1'), 558(syntactically denoted by the non-terminal \verb'exp1'),
556its return list is adjusted to 1. 559then its return list is adjusted to 1.
557If the function is called in a place that can hold many values 560If the function is called in a place that can hold many values
558(syntactically denoted by the non-terminal \verb'exp'), 561(syntactically denoted by the non-terminal \verb'exp'),
559no adjustment is done. 562then no adjustment is made.
560 563
561 564
562\subsection{\Index{Function Definitions}} 565\subsection{\Index{Function Definitions}}
@@ -582,15 +585,15 @@ initialized with the argument values.
582 585
583Results are returned using the \verb'return' statement (\see{return}). 586Results are returned using the \verb'return' statement (\see{return}).
584If control reaches the end of a function without a return instruction, 587If control reaches the end of a function without a return instruction,
585the function returns with no results. 588then the function returns with no results.
586 589
587There is a special syntax for definition of \Index{methods}, 590There is a special syntax for defining \Index{methods},
588that is, functions which have an extra parameter \Def{self}. 591that is, functions that have an extra parameter \Def{self}.
589\begin{Produc} 592\begin{Produc}
590\produc{function}{\rwd{function} var \ter{:} name \ter{(} \opt{parlist1} 593\produc{function}{\rwd{function} var \ter{:} name \ter{(} \opt{parlist1}
591 \ter{)} block \rwd{end}} 594 \ter{)} block \rwd{end}}
592\end{Produc}% 595\end{Produc}%
593A declaration like 596Thus, a declaration like
594\begin{verbatim} 597\begin{verbatim}
595function v:f (...) 598function v:f (...)
596 ... 599 ...
@@ -611,13 +614,13 @@ the variable \verb'v' must have been previously initialized with a table value.
611 614
612Lua provides a powerful mechanism to extend its semantics, 615Lua provides a powerful mechanism to extend its semantics,
613called \Def{fallbacks}. 616called \Def{fallbacks}.
614Basically, a fallback is a programmer defined function 617A fallback is a programmer defined function
615which is called whenever Lua does not know how to proceed. 618that is called whenever Lua does not know how to proceed.
616 619
617Lua supports the following fallbacks, 620Lua supports the following fallbacks,
618identified by the given strings: 621identified by the given strings:
619\begin{description} 622\begin{description}
620\item[``arith'']\index{arithmetic fallback} 623\item[``arith'':]\index{arithmetic fallback}
621called when an arithmetic operation is applied to non numerical operands, 624called when an arithmetic operation is applied to non numerical operands,
622or when the binary \verb'^' operation is called. 625or when the binary \verb'^' operation is called.
623It receives three arguments: 626It receives three arguments:
@@ -628,7 +631,7 @@ and one of the following strings describing the offended operator:
628\end{verbatim} 631\end{verbatim}
629Its return value is the final result of the arithmetic operation. 632Its return value is the final result of the arithmetic operation.
630The default function issues an error. 633The default function issues an error.
631\item[``order'']\index{order fallback} 634\item[``order'':]\index{order fallback}
632called when an order comparison is applied to non numerical or 635called when an order comparison is applied to non numerical or
633non string operands. 636non string operands.
634It receives three arguments: 637It receives three arguments:
@@ -639,40 +642,40 @@ one of the following strings describing the offended operator:
639\end{verbatim} 642\end{verbatim}
640Its return value is the final result of the comparison operation. 643Its return value is the final result of the comparison operation.
641The default function issues an error. 644The default function issues an error.
642\item[``concat'']\index{concatenation fallback} 645\item[``concat'':]\index{concatenation fallback}
643called when a concatenation is applied to non string operands. 646called when a concatenation is applied to non string operands.
644It receives the two operands as arguments. 647It receives the two operands as arguments.
645Its return value is the final result of the concatenation operation. 648Its return value is the final result of the concatenation operation.
646The default function issues an error. 649The default function issues an error.
647\item[``index'']\index{index fallback} 650\item[``index'':]\index{index fallback}
648called when Lua tries to retrieve the value of an index 651called when Lua tries to retrieve the value of an index
649not present in a table. 652not present in a table.
650It receives as arguments the table and the index. 653It receives as arguments the table and the index.
651Its return value is the final result of the indexing operation. 654Its return value is the final result of the indexing operation.
652The default function returns nil. 655The default function returns nil.
653\item[``getglobal'']\index{index getglobal} 656\item[``getglobal'':]\index{index getglobal}
654called when Lua tries to retrieve the value of a global variable 657called when Lua tries to retrieve the value of a global variable
655which has a nil value (or which has not been initialized). 658which has a nil value (or which has not been initialized).
656It receives as argument the name of the variable. 659It receives as argument the name of the variable.
657Its return value is the final result of the expression. 660Its return value is the final result of the expression.
658The default function returns nil. 661The default function returns nil.
659\item[``gettable'']\index{gettable fallback} 662\item[``gettable'':]\index{gettable fallback}
660called when Lua tries to index a non table value. 663called when Lua tries to index a non table value.
661It receives as arguments the non table value and the index. 664It receives as arguments the non table value and the index.
662Its return value is the final result of the indexing operation. 665Its return value is the final result of the indexing operation.
663The default function issues an error. 666The default function issues an error.
664\item[``settable'']\index{settable fallback} 667\item[``settable'':]\index{settable fallback}
665called when Lua tries to assign indexed a non table value. 668called when Lua tries to assign indexed a non table value.
666It receives as arguments the non table value, 669It receives as arguments the non table value,
667the index, and the assigned value. 670the index, and the assigned value.
668The default function issues an error. 671The default function issues an error.
669\item[``function'']\index{function falback} 672\item[``function'':]\index{function falback}
670called when Lua tries to call a non function value. 673called when Lua tries to call a non function value.
671It receives as arguments the non function value and the 674It receives as arguments the non function value and the
672arguments given in the original call. 675arguments given in the original call.
673Its return values are the final results of the call operation. 676Its return values are the final results of the call operation.
674The default function issues an error. 677The default function issues an error.
675\item[``gc''] 678\item[``gc'':]
676called during garbage collection. 679called during garbage collection.
677It receives as argument the table being collected. 680It receives as argument the table being collected.
678After each run of the collector this function is called with argument nil. 681After each run of the collector this function is called with argument nil.
@@ -681,15 +684,15 @@ it must be used with great care,
681and programmers should avoid the creation of new objects 684and programmers should avoid the creation of new objects
682(tables or strings) in this function. 685(tables or strings) in this function.
683The default function does nothing. 686The default function does nothing.
684\item[``error'']\index{error fallback} 687\item[``error'':]\index{error fallback}
685called when an error occurs. 688called when an error occurs.
686It receives as argument a string describing the error. 689It receives as argument a string describing the error.
687The default function prints the message on the standard error output. 690The default function prints the message on the standard error output.
688\end{description} 691\end{description}
689 692
690The function \IndexVerb{setfallback} is used to change a fallback action. 693The function \IndexVerb{setfallback} is used to change a fallback function.
691Its first argument is a string describing the fallback, 694Its first argument is the name of a fallback condition,
692and the second the new function to be called. 695and the second argument is the new function to be called.
693It returns the old function for the given fallback. 696It returns the old function for the given fallback.
694 697
695Section \ref{exfallback} shows an example of the use of fallbacks. 698Section \ref{exfallback} shows an example of the use of fallbacks.
@@ -700,18 +703,18 @@ Section \ref{exfallback} shows an example of the use of fallbacks.
700Because Lua is an extension language, 703Because Lua is an extension language,
701all Lua actions start from C code calling a function from the Lua library. 704all Lua actions start from C code calling a function from the Lua library.
702Whenever an error occurs during Lua compilation or execution, 705Whenever an error occurs during Lua compilation or execution,
703an error fallback function is called, 706an ``error'' fallback function is called,
704and then the corresponding function from the library 707and then the corresponding function from the library
705(\verb'lua_dofile', \verb'lua_dostring', 708(\verb'lua_dofile', \verb'lua_dostring',
706\verb'lua_call', and \verb'lua_callfunction') 709\verb'lua_call', and \verb'lua_callfunction')
707is terminated returning an error condition. 710is terminated returning an error condition.
708 711
709The only argument to the error fallback function is a string describing 712The only argument to the ``error'' fallback function is a string
710the error. 713describing the error.
711The standard I/O library redefines this fallback, 714The standard I/O library redefines this fallback,
712using the debug facilities (\see{debugI}, 715using the debug facilities (\see{debugI}),
713in order to print some extra informations, 716in order to print some extra information,
714like the stack of calls. 717like the call stack.
715For more information about an error, 718For more information about an error,
716the Lua program can include the compilation pragma \verb'$debug'. 719the Lua program can include the compilation pragma \verb'$debug'.
717\index{debug pragma}\label{pragma} 720\index{debug pragma}\label{pragma}
@@ -719,12 +722,11 @@ This pragma must be written in a line by itself.
719When an error occurs in a program compiled with this option, 722When an error occurs in a program compiled with this option,
720the error routine is able to print also the lines where the calls 723the error routine is able to print also the lines where the calls
721(and the error) were made. 724(and the error) were made.
722If needed, it is possible to change the error fallback routine; 725If needed, it is possible to change the ``error'' fallback routine
723\see{fallback}. 726(\see{fallback}).
724 727
725Lua code can generate an error by calling the function \verb'error'. 728Lua code can generate an error by calling the built-in
726Its optional parameter is a string, 729function \verb'error' (\see{pdf-error}).
727which is used as the error message.
728 730
729 731
730\section{The Application Program Interface} 732\section{The Application Program Interface}
@@ -741,24 +743,24 @@ The API functions can be classified in the following categories:
741\item C functions to be called by Lua; 743\item C functions to be called by Lua;
742\item references to Lua Objects. 744\item references to Lua Objects.
743\end{enumerate} 745\end{enumerate}
744All API functions are declared in the file \verb'lua.h'. 746All API functions are declared in the header file \verb'lua.h'.
745 747
746\subsection{Executing Lua Code} 748\subsection{Executing Lua Code}
747A host program can execute Lua chunks written in a file or in a string, 749A host program can execute Lua chunks written in a file or in a string,
748using the following functions: 750using the following functions:
749\Deffunc{lua_dofile}\Deffunc{lua_dostring} 751\Deffunc{lua_dofile}\Deffunc{lua_dostring}
750\begin{verbatim} 752\begin{verbatim}
751int lua_dofile (char *filename); 753int lua_dofile (char *filename);
752int lua_dostring (char *string); 754int lua_dostring (char *string);
753\end{verbatim} 755\end{verbatim}
754Both functions return an error code: 756Both functions return an error code:
7550, in case of success; non zero, in case of errors. 7570, in case of success; non zero, in case of errors.
756More specifically, \verb'lua_dofile' returns 2 if for any reason 758More specifically, \verb'lua_dofile' returns 2 if for any reason
757it could not open the file. 759it could not open the file.
758The function \verb'lua_dofile', if called with argument NULL (0), 760The function \verb'lua_dofile', if called with argument \verb'NULL' (0),
759executes the ``file'' {\tt stdin}. 761executes the {\tt stdin} stream.
760Function \verb'lua_dofile' is also able to execute pre-compiled chunks. 762Function \verb'lua_dofile' is also able to execute pre-compiled chunks.
761It detects whether the file is text or not, 763It automatically detects whether the file is text or binary,
762and loads it accordingly (see program \IndexVerb{luac}). 764and loads it accordingly (see program \IndexVerb{luac}).
763 765
764\subsection{Converting Values between C and Lua} \label{valuesCLua} 766\subsection{Converting Values between C and Lua} \label{valuesCLua}
@@ -770,8 +772,8 @@ Values of type \verb'lua_Object' have no meaning outside Lua;
770for instance, 772for instance,
771the comparisson of two \verb"lua_Object's" is of no significance. 773the comparisson of two \verb"lua_Object's" is of no significance.
772 774
773Lua has automatic memory management, and garbage collection. 775Because Lua has automatic memory management and garbage collection,
774Because of that, a \verb'lua_Object' has a limited scope, 776a \verb'lua_Object' has a limited scope,
775and is only valid inside the {\em block\/} where it was created. 777and is only valid inside the {\em block\/} where it was created.
776A C function called from Lua is a block, 778A C function called from Lua is a block,
777and its parameters are valid only until its end. 779and its parameters are valid only until its end.
@@ -815,9 +817,8 @@ and 0 otherwise.
815The function \verb'lua_isnumber' accepts numbers and numerical strings, 817The function \verb'lua_isnumber' accepts numbers and numerical strings,
816\verb'lua_isstring' accepts strings and numbers (\see{coercion}), 818\verb'lua_isstring' accepts strings and numbers (\see{coercion}),
817and \verb'lua_isfunction' accepts Lua and C functions. 819and \verb'lua_isfunction' accepts Lua and C functions.
818
819The function \verb'lua_type' can be used to distinguish between 820The function \verb'lua_type' can be used to distinguish between
820different kinds of user data; see below. 821different kinds of user data.
821 822
822To translate a value from type \verb'lua_Object' to a specific C type, 823To translate a value from type \verb'lua_Object' to a specific C type,
823the programmer can use: 824the programmer can use:
@@ -866,15 +867,15 @@ plus the macro:
866void lua_pushuserdata (void *u); 867void lua_pushuserdata (void *u);
867\end{verbatim} 868\end{verbatim}
868All of them receive a C value, 869All of them receive a C value,
869convert it to a correspondent \verb'lua_Object', 870convert it to a corresponding \verb'lua_Object',
870and leave the result on the top of the Lua stack, 871and leave the result on the top of the Lua stack,
871where it can be assigned to a Lua variable, 872where it can be assigned to a Lua variable,
872passed as paramenter to a Lua function, etc (see below). \label{pushing} 873passed as parameter to a Lua function, etc. \label{pushing}
873 874
874User data can have different tags, 875User data can have different tags,
875whose semantics are defined by the host program. 876whose semantics are defined by the host program.
876Any positive integer can be used to tag a user data. 877Any positive integer can be used to tag a user datum.
877When a user data is retrieved, 878When a user datum is retrieved,
878the function \verb'lua_type' can be used to get its tag. 879the function \verb'lua_type' can be used to get its tag.
879 880
880To complete the set, 881To complete the set,
@@ -889,13 +890,13 @@ void lua_pushobject (lua_Object object);
889 890
890\subsection{Manipulating Lua Objects} 891\subsection{Manipulating Lua Objects}
891To read the value of any global Lua variable, 892To read the value of any global Lua variable,
892one can use the function: 893one uses the function:
893\Deffunc{lua_getglobal} 894\Deffunc{lua_getglobal}
894\begin{verbatim} 895\begin{verbatim}
895lua_Object lua_getglobal (char *varname); 896lua_Object lua_getglobal (char *varname);
896\end{verbatim} 897\end{verbatim}
897As in Lua, if the value of the global is \nil, 898As in Lua, if the value of the global is \nil,
898the \verb'"getglobal"' fallback is called. 899then the ``getglobal'' fallback is called.
899 900
900To store a value previously pushed onto the stack in a global variable, 901To store a value previously pushed onto the stack in a global variable,
901there is the function: 902there is the function:
@@ -914,7 +915,7 @@ expects on the stack a table and an index,
914and returns the contents of the table at that index. 915and returns the contents of the table at that index.
915As in Lua, if the first object is not a table, 916As in Lua, if the first object is not a table,
916or the index is not present in the table, 917or the index is not present in the table,
917the correspondent fallback is called. 918the corresponding fallback is called.
918 919
919%For compatibility with previous versions of the API, 920%For compatibility with previous versions of the API,
920%the following macros are supported: 921%the following macros are supported:
@@ -934,20 +935,20 @@ and then call the function:
934\begin{verbatim} 935\begin{verbatim}
935void lua_storesubscript (void); 936void lua_storesubscript (void);
936\end{verbatim} 937\end{verbatim}
937Again, the correspondent fallback is called if needed. 938Again, the corresponding fallback is called if needed.
938 939
939Finally, the function 940Finally, the function
940\Deffunc{lua_createtable} 941\Deffunc{lua_createtable}
941\begin{verbatim} 942\begin{verbatim}
942lua_Object lua_createtable (void); 943lua_Object lua_createtable (void);
943\end{verbatim} 944\end{verbatim}
944creates a new table. 945creates and returns a new table.
945 946
946{\em Please Notice:\/} 947{\em Please Notice:\/}
947Most functions from the Lua library receive parameters through the stack. 948Most functions from the Lua library receive parameters through Lua's stack.
948Because other functions also use the stack, 949Because other functions also use this stack,
949it is important that these 950it is important that these
950parameters be pushed just before the correspondent call, 951parameters be pushed just before the corresponding call,
951without intermediate calls to the Lua library. 952without intermediate calls to the Lua library.
952For instance, suppose the user wants the value of \verb'a[i]'. 953For instance, suppose the user wants the value of \verb'a[i]'.
953A simplistic solution would be: 954A simplistic solution would be:
@@ -1064,7 +1065,7 @@ In this way, it is possible to write functions that work with
1064a variable number of parameters. 1065a variable number of parameters.
1065 1066
1066To return values, a C function just pushes them onto the stack, 1067To return values, a C function just pushes them onto the stack,
1067in direct order; \see{valuesCLua}. 1068in direct order (\see{valuesCLua}).
1068Like a Lua function, a C function called by Lua can also return 1069Like a Lua function, a C function called by Lua can also return
1069many results. 1070many results.
1070 1071
@@ -1073,7 +1074,7 @@ Section~\ref{exCFunction} presents an example of a CFunction.
1073 1074
1074\subsection{References to Lua Objects} 1075\subsection{References to Lua Objects}
1075 1076
1076As already noted, \verb'lua_Object's are volatile. 1077As noted in Section~\ref{LuacallC}, \verb'lua_Object's are volatile.
1077If the C code needs to keep a \verb'lua_Object' 1078If the C code needs to keep a \verb'lua_Object'
1078outside block boundaries, 1079outside block boundaries,
1079it must create a \Def{reference} to the object. 1080it must create a \Def{reference} to the object.
@@ -1087,7 +1088,7 @@ void lua_pushref (int ref);
1087void lua_unref (int ref); 1088void lua_unref (int ref);
1088\end{verbatim} 1089\end{verbatim}
1089The function \verb'lua_ref' creates a reference 1090The function \verb'lua_ref' creates a reference
1090to the object which is on the top of the stack, 1091to the object that is on the top of the stack,
1091and returns this reference. 1092and returns this reference.
1092If \verb'lock' is true, the object is {\em locked}: 1093If \verb'lock' is true, the object is {\em locked}:
1093that means the object will not be garbage collected. 1094that means the object will not be garbage collected.
@@ -1095,9 +1096,9 @@ Notice that an unlocked reference may be garbage collected.
1095Whenever the referenced object is needed, 1096Whenever the referenced object is needed,
1096a call to \verb'lua_getref' 1097a call to \verb'lua_getref'
1097returns a handle to it, 1098returns a handle to it,
1098while \verb'lua_pushref' pushes the object on the stack. 1099whereas \verb'lua_pushref' pushes the object on the stack.
1099If the object has been collected, 1100If the object has been collected,
1100\verb'lua_getref' returns \verb'LUA_NOOBJECT', 1101then \verb'lua_getref' returns \verb'LUA_NOOBJECT',
1101and \verb'lua_pushobject' issues an error. 1102and \verb'lua_pushobject' issues an error.
1102 1103
1103When a reference is no longer needed, 1104When a reference is no longer needed,
@@ -1112,7 +1113,7 @@ Most of them provide features that allows some degree of
1112\Index{reflexivity} in the language. 1113\Index{reflexivity} in the language.
1113Some of these features cannot be simulated with the rest of the 1114Some of these features cannot be simulated with the rest of the
1114Language nor with the standard Lua API. 1115Language nor with the standard Lua API.
1115Others are just helpful interfaces to common API functions. 1116Others are just convenient interfaces to common API functions.
1116 1117
1117The libraries, on the other hand, provide useful routines 1118The libraries, on the other hand, provide useful routines
1118that are implemented directly through the standard API. 1119that are implemented directly through the standard API.
@@ -1134,10 +1135,10 @@ declared in \verb-lualib.h-.
1134 1135
1135\subsubsection*{{\tt dofile (filename)}}\Deffunc{dofile} 1136\subsubsection*{{\tt dofile (filename)}}\Deffunc{dofile}
1136This function receives a file name, 1137This function receives a file name,
1137opens it and executes its contents as a Lua chunk, 1138opens it, and executes its contents as a Lua chunk,
1138or as pre-compiled chunks. 1139or as pre-compiled chunks.
1139When called without arguments, 1140When called without arguments,
1140it executes the contents of the standard input. 1141it executes the contents of the standard input (\verb'stdin').
1141It returns 1 if there are no errors, \nil\ otherwise. 1142It returns 1 if there are no errors, \nil\ otherwise.
1142It issues an error when called with a non string argument. 1143It issues an error when called with a non string argument.
1143 1144
@@ -1168,7 +1169,7 @@ See Section \ref{exnext} for an example of the use of this function.
1168 1169
1169\subsubsection*{{\tt nextvar (name)}}\Deffunc{nextvar} 1170\subsubsection*{{\tt nextvar (name)}}\Deffunc{nextvar}
1170This function is similar to the function \verb'next', 1171This function is similar to the function \verb'next',
1171but it iterates over the global variables. 1172but iterates over the global variables.
1172Its single argument is the name of a global variable, 1173Its single argument is the name of a global variable,
1173or \nil\ to get a first name. 1174or \nil\ to get a first name.
1174Similarly to \verb'next', it returns the name of another variable 1175Similarly to \verb'next', it returns the name of another variable
@@ -1193,7 +1194,7 @@ See Section~\ref{libio} for functions for formatted output.
1193This function receives one argument, 1194This function receives one argument,
1194and tries to convert it to a number. 1195and tries to convert it to a number.
1195If the argument is already a number or a string convertible 1196If the argument is already a number or a string convertible
1196to a number (\see{coercion}), it returns that number; 1197to a number (\see{coercion}), then it returns that number;
1197otherwise, it returns \nil. 1198otherwise, it returns \nil.
1198 1199
1199\subsubsection*{{\tt type (v)}}\Deffunc{type} 1200\subsubsection*{{\tt type (v)}}\Deffunc{type}
@@ -1217,7 +1218,7 @@ and between C functions and Lua functions.
1217This function issues an {\em ``assertion failed!''} error 1218This function issues an {\em ``assertion failed!''} error
1218when its argument is \nil. 1219when its argument is \nil.
1219 1220
1220\subsubsection*{{\tt error (message)}}\Deffunc{error} 1221\subsubsection*{{\tt error (message)}}\Deffunc{error}\label{pdf-error}
1221This function issues an error message and terminates 1222This function issues an error message and terminates
1222the last called function from the library 1223the last called function from the library
1223(\verb'lua_dofile', \verb'lua_dostring', \ldots). 1224(\verb'lua_dofile', \verb'lua_dostring', \ldots).
@@ -1265,9 +1266,9 @@ Returns another string, which is a substring of \verb's',
1265starting at \verb'i' and runing until \verb'j'. 1266starting at \verb'i' and runing until \verb'j'.
1266If \verb'j' is absent, 1267If \verb'j' is absent,
1267it is assumed to be equal to the length of \verb's'. 1268it is assumed to be equal to the length of \verb's'.
1268Particularly, the call \verb'strsub(s,1,j)' returns a prefix of \verb's' 1269In particular, the call \verb'strsub(s,1,j)' returns a prefix of \verb's'
1269with length \verb'j', 1270with length \verb'j',
1270while the call \verb'strsub(s,i)' returns a suffix of \verb's', 1271whereas the call \verb'strsub(s,i)' returns a suffix of \verb's',
1271starting at \verb'i'. 1272starting at \verb'i'.
1272 1273
1273\subsubsection*{{\tt strlower (s)}}\Deffunc{strlower} 1274\subsubsection*{{\tt strlower (s)}}\Deffunc{strlower}
@@ -1282,7 +1283,7 @@ All other characters are left unchanged.
1282 1283
1283\subsubsection*{{\tt ascii (s, [i])}}\Deffunc{ascii} 1284\subsubsection*{{\tt ascii (s, [i])}}\Deffunc{ascii}
1284Returns the ascii code of the character \verb's[i]'. 1285Returns the ascii code of the character \verb's[i]'.
1285If \verb'i' is absent, it is assumed to be 1. 1286If \verb'i' is absent, then it is assumed to be 1.
1286 1287
1287\subsubsection*{{\tt format (formatstring, e1, e2, \ldots)}}\Deffunc{format} 1288\subsubsection*{{\tt format (formatstring, e1, e2, \ldots)}}\Deffunc{format}
1288\label{format} 1289\label{format}
@@ -1290,7 +1291,7 @@ This function returns a formated version of its variable number of arguments
1290following the description given in its first argument (which must be a string). 1291following the description given in its first argument (which must be a string).
1291The format string follows the same rules as the \verb'printf' family of 1292The format string follows the same rules as the \verb'printf' family of
1292standard C functions. 1293standard C functions.
1293The only differencies are that the options/modifiers 1294The only differences are that the options/modifiers
1294\verb'*', \verb'l', \verb'L', \verb'n', \verb'p', 1295\verb'*', \verb'l', \verb'L', \verb'n', \verb'p',
1295and \verb'h' are not supported, 1296and \verb'h' are not supported,
1296and there is an extra option, \verb'q'. 1297and there is an extra option, \verb'q'.
@@ -1303,7 +1304,7 @@ are correctly escaped when written.
1303The options \verb'c', \verb'd', \verb'E', \verb'e', \verb'f', 1304The options \verb'c', \verb'd', \verb'E', \verb'e', \verb'f',
1304\verb'g' \verb'i', \verb'o', \verb'u', \verb'X', and \verb'x' all 1305\verb'g' \verb'i', \verb'o', \verb'u', \verb'X', and \verb'x' all
1305expect a number argument, 1306expect a number argument,
1306while \verb'q' and \verb's' expect a string. 1307whereas \verb'q' and \verb's' expect a string.
1307 1308
1308 1309
1309\subsection{Mathematical Functions} \label{mathlib} 1310\subsection{Mathematical Functions} \label{mathlib}
@@ -1325,7 +1326,7 @@ max min mod sin sqrt tan random randomseed
1325Most of them 1326Most of them
1326are only interfaces to the homonymous functions in the C library, 1327are only interfaces to the homonymous functions in the C library,
1327except that, for the trigonometric functions, 1328except that, for the trigonometric functions,
1328all angles are expressed in degrees. 1329all angles are expressed in degrees, not radians.
1329 1330
1330The function \verb'max' returns the maximum 1331The function \verb'max' returns the maximum
1331value of its numeric arguments. 1332value of its numeric arguments.
@@ -1343,7 +1344,7 @@ $[0,1)$.
1343 1344
1344\subsection{I/O Facilities} \label{libio} 1345\subsection{I/O Facilities} \label{libio}
1345 1346
1346All I/O operations in Lua are done over two {\em current} files, 1347All I/O operations in Lua are done over two {\em current} files:
1347one for reading and one for writing. 1348one for reading and one for writing.
1348Initially, the current input file is \verb'stdin', 1349Initially, the current input file is \verb'stdin',
1349and the current output file is \verb'stdout'. 1350and the current output file is \verb'stdout'.
@@ -1489,15 +1490,15 @@ with an optional \verb-code-,
1489to terminate the program. 1490to terminate the program.
1490 1491
1491 1492
1492\section{The Debuger Interface} \label{debugI} 1493\section{The Debugger Interface} \label{debugI}
1493 1494
1494Lua has no built in debuger facilities. 1495Lua has no built-in debugger facilities.
1495Instead, it offers a special interface, 1496Instead, it offers a special interface,
1496by means of functions and {\em hooks}, 1497by means of functions and {\em hooks},
1497which allows the construction of different 1498which allows the construction of different
1498kinds of debugers, profiles, and other tools 1499kinds of debugers, profilers, and other tools
1499that need ``inside'' information from the interpreter. 1500that need ``inside information'' from the interpreter.
1500This interface is declared in the file \verb'luadebug.h'. 1501This interface is declared in the header file \verb'luadebug.h'.
1501 1502
1502\subsection{Stack and Function Information} 1503\subsection{Stack and Function Information}
1503 1504
@@ -1529,9 +1530,9 @@ char *lua_getobjname (lua_Object o, char **name);
1529\verb'lua_funcinfo' gives the file name and the line where the 1530\verb'lua_funcinfo' gives the file name and the line where the
1530given function has been defined. 1531given function has been defined.
1531If the ``function'' is in fact the main code of a chunk, 1532If the ``function'' is in fact the main code of a chunk,
1532\verb'linedefined' is 0. 1533then \verb'linedefined' is 0.
1533If the function is a C function, 1534If the function is a C function,
1534\verb'linedefined' is -1, and \verb'filename' is \verb'"(C)"'. 1535then \verb'linedefined' is -1, and \verb'filename' is \verb'"(C)"'.
1535 1536
1536The function \verb'lua_currentline' gives the current line where 1537The function \verb'lua_currentline' gives the current line where
1537a given function is executing. 1538a given function is executing.
@@ -1550,10 +1551,10 @@ function is a fallback.
1550If so, it returns the string \verb'"fallback"', 1551If so, it returns the string \verb'"fallback"',
1551and \verb'name' is set to point to the fallback name. 1552and \verb'name' is set to point to the fallback name.
1552Otherwise, if the given function is the value of a global variable, 1553Otherwise, if the given function is the value of a global variable,
1553\verb'lua_getobjname' returns the string \verb'"global"', 1554then \verb'lua_getobjname' returns the string \verb'"global"',
1554while \verb'name' points to the variable name. 1555while \verb'name' points to the variable name.
1555If the given function is neither a fallback nor a global variable, 1556If the given function is neither a fallback nor a global variable,
1556\verb'lua_getobjname' returns the empty string, 1557then \verb'lua_getobjname' returns the empty string,
1557and \verb'name' is set to \verb'NULL'. 1558and \verb'name' is set to \verb'NULL'.
1558 1559
1559\subsection{Manipulating Local Variables} 1560\subsection{Manipulating Local Variables}
@@ -1579,15 +1580,15 @@ or if the activation record has no debug information,
1579The function \verb'lua_setlocal' sets the local variable 1580The function \verb'lua_setlocal' sets the local variable
1580\verb'local_number' to the value previously pushed on the stack 1581\verb'local_number' to the value previously pushed on the stack
1581(\see{valuesCLua}). 1582(\see{valuesCLua}).
1582If the function succeeds it returns 1. 1583If the function succeeds, then it returns 1.
1583If \verb'local_number' is greater than the number 1584If \verb'local_number' is greater than the number
1584of active local variables, 1585of active local variables,
1585or if the activation record has no debug information, 1586or if the activation record has no debug information,
1586this function fails and returns 0. 1587then this function fails and returns 0.
1587 1588
1588\subsection{Hooks} 1589\subsection{Hooks}
1589 1590
1590The Lua interpreter offers two hooks for debug purposes: 1591The Lua interpreter offers two hooks for debugging purposes:
1591\begin{verbatim} 1592\begin{verbatim}
1592typedef void (*lua_CHFunction) (lua_Function func, char *file, int line); 1593typedef void (*lua_CHFunction) (lua_Function func, char *file, int line);
1593extern lua_CHFunction lua_callhook; 1594extern lua_CHFunction lua_callhook;
@@ -1631,7 +1632,7 @@ like ordinary arrays, records, sets, bags, and lists.
1631Arrays need no explanations. 1632Arrays need no explanations.
1632In Lua, it is conventional to start indices from 1, 1633In Lua, it is conventional to start indices from 1,
1633but this is only a convention. 1634but this is only a convention.
1634Arrays can be indexed by 0, negative numbers, or any other value (but \nil). 1635Arrays can be indexed by 0, negative numbers, or any other value (except \nil).
1635Records are also trivially implemented by the syntactic sugar 1636Records are also trivially implemented by the syntactic sugar
1636\verb'a.x'. 1637\verb'a.x'.
1637 1638
@@ -1642,15 +1643,14 @@ The statement \verb's[x] = 1' inserts the value of \verb'x' into
1642the set \verb's'. 1643the set \verb's'.
1643The expression \verb's[x]' is true if and only if 1644The expression \verb's[x]' is true if and only if
1644\verb'x' belongs to \verb's'. 1645\verb'x' belongs to \verb's'.
1645Finally, the statement \verb's[x] = nil' erases \verb'x' from \verb's'. 1646Finally, the statement \verb's[x] = nil' removes \verb'x' from \verb's'.
1646 1647
1647Bags can be implemented similarly to sets, 1648Bags can be implemented similarly to sets,
1648but using the value associated to an element as its counter. 1649but using the value associated to an element as its counter.
1649So, to insert an element, 1650So, to insert an element,
1650the following code is enough: 1651the following code is enough:
1651\begin{verbatim} 1652\begin{verbatim}
1652if s[x] then s[x] = s[x]+1 1653if s[x] then s[x] = s[x]+1 else s[x] = 1 end
1653else s[x] = 1 end
1654\end{verbatim} 1654\end{verbatim}
1655and to remove an element: 1655and to remove an element:
1656\begin{verbatim} 1656\begin{verbatim}
@@ -1671,7 +1671,7 @@ the tail with \verb'l[2]'.
1671\Deffunc{next}\Deffunc{nextvar} 1671\Deffunc{next}\Deffunc{nextvar}
1672This example shows how to use the function \verb'next' to iterate 1672This example shows how to use the function \verb'next' to iterate
1673over the fields of a table. 1673over the fields of a table.
1674Function \Def{clone} receives any table and returns a clone of it. 1674Function \IndexVerb{clone} receives any table and returns a clone of it.
1675\begin{verbatim} 1675\begin{verbatim}
1676function clone (t) -- t is a table 1676function clone (t) -- t is a table
1677 local new_t = {} -- create a new table 1677 local new_t = {} -- create a new table
@@ -1731,7 +1731,7 @@ end
1731 1731
1732\subsection{\Index{Variable number of arguments}} 1732\subsection{\Index{Variable number of arguments}}
1733Lua does not provide any explicit mechanism to deal with 1733Lua does not provide any explicit mechanism to deal with
1734variable number of arguments. 1734variable number of arguments in function calls.
1735However, one can use table constructors to simulate this mechanism. 1735However, one can use table constructors to simulate this mechanism.
1736As an example, suppose a function to concatenate all its arguments. 1736As an example, suppose a function to concatenate all its arguments.
1737It could be written like 1737It could be written like
@@ -1778,9 +1778,9 @@ In order to restore this value, a \verb'lua_dofile' suffices.
1778 1778
1779Storing tables is a little more complex. 1779Storing tables is a little more complex.
1780Assuming that the table is a tree, 1780Assuming that the table is a tree,
1781and all indices are identifiers 1781and that all indices are identifiers
1782(that is, the tables are being used as records), 1782(that is, the tables are being used as records),
1783its value can be written directly with table constructors. 1783then its value can be written directly with table constructors.
1784First, the function \verb'write_value' is changed to 1784First, the function \verb'write_value' is changed to
1785\begin{verbatim} 1785\begin{verbatim}
1786function write_value (value) 1786function write_value (value)
@@ -1902,9 +1902,9 @@ with the index fallback redefined according to
1902Section~\ref{exfallback}}. 1902Section~\ref{exfallback}}.
1903 1903
1904As one could expect, a good way to represent a class is 1904As one could expect, a good way to represent a class is
1905as a table. 1905with a table.
1906This table will contain all instance methods of the class, 1906This table will contain all instance methods of the class,
1907plus eventual default values for instance variables. 1907plus optional default values for instance variables.
1908An instance of a class has its \verb'parent' field pointing to 1908An instance of a class has its \verb'parent' field pointing to
1909the class, 1909the class,
1910and so it ``inherits'' all methods. 1910and so it ``inherits'' all methods.
@@ -1942,7 +1942,7 @@ p2 = Point:create{x = 10} -- y will be inherited until it is set
1942-- 1942--
1943p1:move(p2) 1943p1:move(p2)
1944\end{verbatim} 1944\end{verbatim}
1945\caption{A Class Point.\label{Point}} 1945\caption{A Class {\tt Point}.\label{Point}}
1946\Line 1946\Line
1947\end{figure} 1947\end{figure}
1948Finally, a subclass can be created as a new table, 1948Finally, a subclass can be created as a new table,
@@ -2071,7 +2071,7 @@ Lua means {\em moon} in Portuguese.
2071 2071
2072\appendix 2072\appendix
2073 2073
2074\section{Incompatibilities with Previous Versions} 2074\section*{Incompatibilities with Previous Versions}
2075 2075
2076Although great care has been taken to avoid incompatibilities with 2076Although great care has been taken to avoid incompatibilities with
2077the previous public versions of Lua, 2077the previous public versions of Lua,
@@ -2082,7 +2082,7 @@ Here is a list of all these differences.
2082\begin{itemize} 2082\begin{itemize}
2083\item 2083\item
2084Functions \verb'date' and \verb'time' (from \verb'iolib') 2084Functions \verb'date' and \verb'time' (from \verb'iolib')
2085have been superseded by the new version of function \verb'date'. 2085have been superseded by the new, more powerful version of function \verb'date'.
2086\item 2086\item
2087Function \verb'append' (from \verb'iolib') now returns 1 whenever it succeeds, 2087Function \verb'append' (from \verb'iolib') now returns 1 whenever it succeeds,
2088whether the file is new or not. 2088whether the file is new or not.
@@ -2094,7 +2094,7 @@ The API lock mechanism has been superseded by the reference mechanism.
2094However, \verb-lua.h- provides compatibility macros, 2094However, \verb-lua.h- provides compatibility macros,
2095so there is no need to change programs. 2095so there is no need to change programs.
2096\item 2096\item
2097API function \verb'lua_pushliteral' now is just a macro to 2097The API function \verb'lua_pushliteral' now is just a macro to
2098\verb'lua_pushstring'. 2098\verb'lua_pushstring'.
2099\end{itemize} 2099\end{itemize}
2100 2100
@@ -2145,11 +2145,11 @@ int lua_storesubscript (void);
2145with the parameters explicitly pushed on the stack. 2145with the parameters explicitly pushed on the stack.
2146\item 2146\item
2147The functionality of the function \verb'lua_errorfunction' has been 2147The functionality of the function \verb'lua_errorfunction' has been
2148replaced by the {\em fallback} mechanism; \see{error}. 2148replaced by the {\em fallback} mechanism (\see{error}).
2149\item 2149\item
2150When calling a function from the Lua library, 2150When calling a function from the Lua library,
2151parameters passed through the stack 2151parameters passed through the stack
2152must be pushed just before the correspondent call, 2152must be pushed just before the corresponding call,
2153with no intermediate calls to Lua. 2153with no intermediate calls to Lua.
2154Special care should be taken with macros like 2154Special care should be taken with macros like
2155\verb'lua_getindexed' and \verb'lua_getfield'. 2155\verb'lua_getindexed' and \verb'lua_getfield'.