aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2003-01-24 07:14:54 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2003-01-24 07:14:54 -0200
commit3088ebd95b7c509ff1a049ef4a10a1c016883490 (patch)
tree372176a783a834d79268c979c441c426ab105a3c
parent797d964df9c0b1bf09338cea3d578f1985fe2ee0 (diff)
downloadlua-3088ebd95b7c509ff1a049ef4a10a1c016883490.tar.gz
lua-3088ebd95b7c509ff1a049ef4a10a1c016883490.tar.bz2
lua-3088ebd95b7c509ff1a049ef4a10a1c016883490.zip
last changes before leaving Latex
-rw-r--r--manual.tex446
1 files changed, 241 insertions, 205 deletions
diff --git a/manual.tex b/manual.tex
index e49352be..35984019 100644
--- a/manual.tex
+++ b/manual.tex
@@ -1,13 +1,47 @@
1% $Id: manual.tex,v 1.66 2003/01/21 17:45:11 roberto Exp roberto $ 1% $Id: manual.tex,v 2.1 2003/01/22 16:29:38 roberto Exp roberto $
2%{[( 2%{[(
3 3
4\documentclass[11pt,twoside]{article} 4\documentclass[11pt,twoside]{article}
5\usepackage{fullpage} 5\usepackage{fullpage}
6\usepackage{bnf} 6\usepackage{iso}
7\usepackage{graphicx} 7\usepackage{graphicx}
8 8
9% no need for subscripts... 9
10\catcode`\_=12 10
11% Right arrow (internal use)
12\newcommand{\ra}{\(\rightarrow\)\ }
13
14% Terminal Simbols
15\newcommand{\ter}[1]{{\rm`{\tt#1}'}}
16%reserved words
17\newcommand{\rwd}[1]{{\bf\lowercase{#1}}}
18% empty production
19\newcommand{\emptyprod}{$\epsilon$ }
20
21% repetitions and optionals
22\newcommand{\rep}[1]{{\rm\{}\,#1\,{\rm\}}}
23\newcommand{\opt}[1]{{\rm [}\,#1\,{\,\rm]}}
24\newcommand{\oneormore}[1]{{\rm\{}#1{\/\rm\}$^+$}}
25
26\newcommand{\prg}[1]{{\it #1\/}}
27
28%productions: \produc{non-terminal}{rule}
29\newcommand{\produc}[2]{#1 & \ra & #2\index{grammar!#1}\\}
30
31%new line inside a production
32\newcommand{\NL}{\\ & &}
33%new line indented
34\newcommand{\NLI}{\NL\hspace{2ex}}
35
36% 'or'
37\newcommand{\Or}{$|$ }
38
39% 'or' in a new line
40\newcommand{\OrNL}{\\ & \Or & }
41
42%Environment for productions
43\newenvironment{Produc}{\vspace{0.8ex}\par\noindent\hspace{5ex}\it\begin{tabular}{rrl}}{\end{tabular}\vspace{0.8ex}\par\noindent}
44
11 45
12%\newcommand{\See}[1]{Section~\ref{#1}} 46%\newcommand{\See}[1]{Section~\ref{#1}}
13\newcommand{\See}[1]{\S\ref{#1}} 47\newcommand{\See}[1]{\S\ref{#1}}
@@ -43,14 +77,12 @@
43 77
44\newcommand{\Version}{5.0 (beta)} 78\newcommand{\Version}{5.0 (beta)}
45 79
46% changes to bnf.sty by LHF
47\renewcommand{\Or}{$|$ }
48\renewcommand{\rep}[1]{{\rm\{}\,#1\,{\rm\}}}
49\renewcommand{\opt}[1]{{\rm [}\,#1\,{\,\rm]}}
50\renewcommand{\ter}[1]{{\rm`{\tt#1}'}}
51\newcommand{\Nter}[1]{{\tt#1}} 80\newcommand{\Nter}[1]{{\tt#1}}
52\newcommand{\NOTE}{\par\medskip\noindent\emph{NOTE}: } 81\newcommand{\NOTE}{\par\medskip\noindent\emph{NOTE}: }
53 82
83\newcommand{\At}{{\tt @}} %{\verb|@|}
84\newcommand{\Nb}{~}
85
54\makeindex 86\makeindex
55 87
56\begin{document} 88\begin{document}
@@ -139,7 +171,7 @@ Waldemar Celes
139\tecgraf\ --- Computer Science Department --- PUC-Rio 171\tecgraf\ --- Computer Science Department --- PUC-Rio
140} 172}
141 173
142%\date{{\small \tt\$Date: 2003/01/21 17:45:11 $ $}} 174%\date{{\small \tt\$Date: 2003/01/22 16:29:38 $ $}}
143 175
144\maketitle 176\maketitle
145 177
@@ -166,7 +198,7 @@ scripting,
166and 198and
167rapid prototyping. 199rapid prototyping.
168 200
169This document describes version \Version\ of the Lua programming language 201This document describes version \Version{} of the Lua programming language
170and the Application Program Interface (API) 202and the Application Program Interface (API)
171that allows interaction between Lua programs and their host C~programs. 203that allows interaction between Lua programs and their host C~programs.
172\end{abstract} 204\end{abstract}
@@ -174,27 +206,27 @@ that allows interaction between Lua programs and their host C~programs.
174\def\abstractname{Resumo} 206\def\abstractname{Resumo}
175\begin{abstract} 207\begin{abstract}
176\noindent 208\noindent
177Lua \'e uma linguagem de programa\c{c}\~ao 209Lua é uma linguagem de programação
178poderosa e leve, 210poderosa e leve,
179projetada para estender aplica\c{c}\~oes. 211projetada para estender aplicações.
180Lua tamb\'em \'e frequentemente usada como uma linguagem de prop\'osito geral. 212Lua também é frequentemente usada como uma linguagem de propósito geral.
181Lua combina programa\c{c}\~ao procedural 213Lua combina programação procedural
182(com sintaxe semelhante \`a de Pascal) 214(com sintaxe semelhante à de Pascal)
183com 215com
184poderosas constru\c{c}\~oes para descri\c{c}\~ao de dados, 216poderosas construções para descrição de dados,
185baseadas em tabelas associativas e sem\^antica extens\'\i vel. 217baseadas em tabelas associativas e semântica extensível.
186Lua \'e 218Lua é
187tipada dinamicamente, 219tipada dinamicamente,
188interpretada a partir de \emph{opcodes}, 220interpretada a partir de \emph{opcodes},
189e tem gerenciamento autom\'atico de mem\'oria com coleta de lixo. 221e tem gerenciamento automático de memória com coleta de lixo.
190Essas caracter\'{\i}sticas fazem de Lua uma linguagem ideal para 222Essas características fazem de Lua uma linguagem ideal para
191configura\c{c}\~ao, 223configuração,
192automa\c{c}\~ao (\emph{scripting}) 224automação (\emph{scripting})
193e prototipagem r\'apida. 225e prototipagem rápida.
194 226
195Este documento descreve a vers\~ao \Version\ da linguagem de 227Este documento descreve a versão \Version{} da linguagem de
196programa\c{c}\~ao Lua e a Interface de Programa\c{c}\~ao (API) que permite 228programação Lua e a Interface de Programação (API) que permite
197a intera\c{c}\~ao entre programas Lua e programas C~hospedeiros. 229a interação entre programas Lua e programas C~hospedeiros.
198\end{abstract} 230\end{abstract}
199 231
200\newpage 232\newpage
@@ -207,8 +239,17 @@ a intera\c{c}\~ao entre programas Lua e programas C~hospedeiros.
207\pagestyle{plain} 239\pagestyle{plain}
208\pagenumbering{arabic} 240\pagenumbering{arabic}
209 241
242\catcode`\_=12
243\catcode`\$=12
244\catcode`\#=12
245\catcode`\%=12
246\catcode`\^=12
247\catcode`\~=12
248\catcode`\&=12
249
210 250
211\C{------------------------------------------------------------------------------} 251
252\C{-------------------------------------------------------------------------}
212\section{Introduction} 253\section{Introduction}
213 254
214Lua is an extension programming language designed to support 255Lua is an extension programming language designed to support
@@ -223,8 +264,8 @@ it only works \emph{embedded} in a host client,
223called the \emph{embedding program} or simply the \emph{host}. 264called the \emph{embedding program} or simply the \emph{host}.
224This host program can invoke functions to execute a piece of Lua code, 265This host program can invoke functions to execute a piece of Lua code,
225can write and read Lua variables, 266can write and read Lua variables,
226and can register C~functions to be called by Lua code. 267and can register C\Nb{}functions to be called by Lua code.
227Through the use of C~functions, Lua can be augmented to cope with 268Through the use of C\Nb{}functions, Lua can be augmented to cope with
228a wide range of different domains, 269a wide range of different domains,
229thus creating customized programming languages sharing a syntactical framework. 270thus creating customized programming languages sharing a syntactical framework.
230 271
@@ -241,32 +282,32 @@ see the papers below,
241which are available at Lua's web site. 282which are available at Lua's web site.
242\begin{itemize} 283\begin{itemize}
243\item 284\item
244R.~Ierusalimschy, L.~H.~de Figueiredo, and W.~Celes. 285R.\Nb{}Ierusalimschy, L.\Nb{}H.\Nb{}de Figueiredo, and W.\Nb{}Celes.
245Lua\Em{}an extensible extension language. 286Lua\Em{}an extensible extension language.
246\emph{Software: Practice \& Experience} {\bf 26} \#6 (1996) 635\En{}652. 287\emph{Software: Practice & Experience} {\bf 26} #6 (1996) 635\En{}652.
247\item 288\item
248L.~H.~de Figueiredo, R.~Ierusalimschy, and W.~Celes. 289L.\Nb{}H.\Nb{}de Figueiredo, R.\Nb{}Ierusalimschy, and W.\Nb{}Celes.
249The design and implementation of a language for extending applications. 290The design and implementation of a language for extending applications.
250\emph{Proceedings of XXI Brazilian Seminar on Software and Hardware} (1994) 273\En{}283. 291\emph{Proceedings of XXI Brazilian Seminar on Software and Hardware} (1994) 273\En{}283.
251\item 292\item
252L.~H.~de Figueiredo, R.~Ierusalimschy, and W.~Celes. 293L.\Nb{}H.\Nb{}de Figueiredo, R.\Nb{}Ierusalimschy, and W.\Nb{}Celes.
253Lua: an extensible embedded language. 294Lua: an extensible embedded language.
254\emph{Dr. Dobb's Journal} {\bf 21} \#12 (Dec 1996) 26\En{}33. 295\emph{Dr. Dobb's Journal} {\bf 21} #12 (Dec 1996) 26\En{}33.
255\item 296\item
256R.~Ierusalimschy, L.~H.~de Figueiredo, and W.~Celes. 297R.\Nb{}Ierusalimschy, L.\Nb{}H.\Nb{}de Figueiredo, and W.\Nb{}Celes.
257The evolution of an extension language: a history of Lua, 298The evolution of an extension language: a history of Lua,
258\emph{Proceedings of V Brazilian Symposium on Programming Languages} (2001) B-14\En{}B-28. 299\emph{Proceedings of V Brazilian Symposium on Programming Languages} (2001) B-14\En{}B-28.
259\end{itemize} 300\end{itemize}
260 301
261Lua means \Q{moon} in Portuguese. 302Lua means \Q{moon} in Portuguese.
262 303
263\C{------------------------------------------------------------------------------} 304\C{-------------------------------------------------------------------------}
264\section{Lua Concepts}\label{concepts} 305\section{Lua Concepts}\label{concepts}
265 306
266This section describes the main concepts of Lua as a language. 307This section describes the main concepts of Lua as a language.
267The syntax and semantics of Lua are described in \See{language}. 308The syntax and semantics of Lua are described in \See{language}.
268The discussion below is not purely conceptual; 309The discussion below is not purely conceptual;
269it includes references to the C~API \see{API}, 310it includes references to the C\Nb{}API \see{API},
270because Lua is designed to be embedded in host programs. 311because Lua is designed to be embedded in host programs.
271It also includes references to the standard libraries \see{libraries}. 312It also includes references to the standard libraries \see{libraries}.
272 313
@@ -315,10 +356,10 @@ All values carry their own type.
315There are eight \Index{basic types} in Lua: 356There are eight \Index{basic types} in Lua:
316\Def{nil}, \Def{boolean}, \Def{number}, 357\Def{nil}, \Def{boolean}, \Def{number},
317\Def{string}, \Def{function}, \Def{userdata}, \Def{thread}, and \Def{table}. 358\Def{string}, \Def{function}, \Def{userdata}, \Def{thread}, and \Def{table}.
318\emph{Nil} is the type of the value \nil, 359\emph{Nil} is the type of the value \nil{},
319whose main property is to be different from any other value; 360whose main property is to be different from any other value;
320usually it represents the absence of a useful value. 361usually it represents the absence of a useful value.
321\emph{Boolean} is the type of the values \False{} and \True. 362\emph{Boolean} is the type of the values \False{} and \True{}.
322In Lua, both \nil{} and \False{} make a condition false, 363In Lua, both \nil{} and \False{} make a condition false,
323and any other value makes it true. 364and any other value makes it true.
324\emph{Number} represents real (double-precision floating-point) numbers. 365\emph{Number} represents real (double-precision floating-point) numbers.
@@ -347,7 +388,7 @@ However, by using \emph{metatables},
347the programmer can define operations for userdata values 388the programmer can define operations for userdata values
348\see{metatable}. 389\see{metatable}.
349Userdata values cannot be created or modified in Lua, 390Userdata values cannot be created or modified in Lua,
350only through the C~API. 391only through the C\Nb{}API.
351This guarantees the integrity of data owned by the host program. 392This guarantees the integrity of data owned by the host program.
352 393
353The type \Def{thread} represents independent threads of execution, 394The type \Def{thread} represents independent threads of execution,
@@ -357,10 +398,10 @@ and is subject to changes in the future.)
357 398
358The type \emph{table} implements \Index{associative arrays}, 399The type \emph{table} implements \Index{associative arrays},
359that is, \Index{arrays} that can be indexed not only with numbers, 400that is, \Index{arrays} that can be indexed not only with numbers,
360but with any value (except \nil). 401but with any value (except \nil{}).
361Moreover, 402Moreover,
362tables can be \emph{heterogeneous}, 403tables can be \emph{heterogeneous},
363that is, they can contain values of all types (except \nil). 404that is, they can contain values of all types (except \nil{}).
364Tables are the sole data structuring mechanism in Lua; 405Tables are the sole data structuring mechanism in Lua;
365they may be used to represent not only ordinary arrays, 406they may be used to represent not only ordinary arrays,
366but also symbol tables, sets, records, graphs, trees, etc. 407but also symbol tables, sets, records, graphs, trees, etc.
@@ -438,7 +479,7 @@ global variables
438and local variables. 479and local variables.
439Variables are assumed to be global unless explicitly declared local 480Variables are assumed to be global unless explicitly declared local
440\see{localvar}. 481\see{localvar}.
441Before the first assignment to a variable, its value is \nil. 482Before the first assignment to a variable, its value is \nil{}.
442 483
443All global variables live as fields in ordinary Lua tables. 484All global variables live as fields in ordinary Lua tables.
444Usually, globals live in a table called \Index{table of globals}. 485Usually, globals live in a table called \Index{table of globals}.
@@ -466,7 +507,7 @@ collecting all dead objects
466All objects in Lua are subject to automatic management: 507All objects in Lua are subject to automatic management:
467tables, userdata, functions, and strings. 508tables, userdata, functions, and strings.
468 509
469Using the C~API, 510Using the C\Nb{}API,
470you can set garbage-collector metamethods for userdata \see{metatable}. 511you can set garbage-collector metamethods for userdata \see{metatable}.
471When it is about to free a userdata, 512When it is about to free a userdata,
472Lua calls the metamethod associated with event \verb|gc| in the 513Lua calls the metamethod associated with event \verb|gc| in the
@@ -485,7 +526,7 @@ which reclaims the memory of all dead objects.
485The byte counter is adjusted, 526The byte counter is adjusted,
486and then the threshold is reset to twice the new value of the byte counter. 527and then the threshold is reset to twice the new value of the byte counter.
487 528
488Through the C~API, you can query those numbers, 529Through the C\Nb{}API, you can query those numbers,
489and change the threshold \see{GC-API}. 530and change the threshold \see{GC-API}.
490Setting the threshold to zero actually forces an immediate 531Setting the threshold to zero actually forces an immediate
491garbage-collection cycle, 532garbage-collection cycle,
@@ -519,7 +560,7 @@ If \verb|__mode| contains \verb|v|,
519the values in the table are weak. 560the values in the table are weak.
520 561
521 562
522\C{------------------------------------------------------------------------------} 563\C{-------------------------------------------------------------------------}
523\section{The Language}\label{language} 564\section{The Language}\label{language}
524 565
525This section describes the lexis, the syntax, and the semantics of Lua. 566This section describes the lexis, the syntax, and the semantics of Lua.
@@ -551,8 +592,8 @@ and cannot be used as identifiers:
551\end{verbatim} 592\end{verbatim}
552 593
553Lua is a case-sensitive language: 594Lua is a case-sensitive language:
554\T{and} is a reserved word, but \T{And} and \T{\'and} 595\T{and} is a reserved word, but \T{And} and \T{AND}
555(if the locale permits) are two different, valid identifiers. 596are two different, valid identifiers.
556As a convention, identifiers starting with an underscore followed by 597As a convention, identifiers starting with an underscore followed by
557uppercase letters (such as \verb|_VERSION|) 598uppercase letters (such as \verb|_VERSION|)
558are reserved for internal variables used by Lua. 599are reserved for internal variables used by Lua.
@@ -588,9 +629,10 @@ where \emph{ddd} is a sequence of up to three \emph{decimal} digits.
588Strings in Lua may contain any 8-bit value, including embedded zeros, 629Strings in Lua may contain any 8-bit value, including embedded zeros,
589which can be specified as `\verb|\0|'. 630which can be specified as `\verb|\0|'.
590 631
591Literal strings can also be delimited by matching \verb|[[| $\ldots$ \verb|]]|. 632Literal strings can also be delimited by matching
633\verb|[[| \Math{\ldots} \verb|]]|.
592Literals in this bracketed form may run for several lines, 634Literals in this bracketed form may run for several lines,
593may contain nested \verb|[[| $\ldots$ \verb|]]| pairs, 635may contain nested \verb|[[| \Math{\ldots} \verb|]]| pairs,
594and do not interpret escape sequences. 636and do not interpret escape sequences.
595For convenience, 637For convenience,
596when the opening \verb|[[| is immediately followed by a newline, 638when the opening \verb|[[| is immediately followed by a newline,
@@ -599,8 +641,8 @@ That form is specially convenient for
599writing strings that contain program pieces or 641writing strings that contain program pieces or
600other quoted strings. 642other quoted strings.
601As an example, in a system using ASCII 643As an example, in a system using ASCII
602(in which `\verb|a|' is coded as~97, 644(in which `\verb|a|' is coded as\Nb{}97,
603newline is coded as~10, and `\verb|1|' is coded as~49), 645newline is coded as\Nb{}10, and `\verb|1|' is coded as\Nb{}49),
604the four literals below denote the same string: 646the four literals below denote the same string:
605\begin{verbatim} 647\begin{verbatim}
606 (1) "alo\n123\"" 648 (1) "alo\n123\""
@@ -627,7 +669,7 @@ that runs until the end of the line.
627Otherwise, it is a long comment, 669Otherwise, it is a long comment,
628that runs until the corresponding \verb|]]|. 670that runs until the corresponding \verb|]]|.
629Long comments may run for several lines, 671Long comments may run for several lines,
630and may contain nested \verb|[[| $\ldots$ \verb|]]| pairs. 672and may contain nested \verb|[[| \Math{\ldots} \verb|]]| pairs.
631For convenience, 673For convenience,
632the first line of a chunk is skipped if it starts with \verb|#|. 674the first line of a chunk is skipped if it starts with \verb|#|.
633This facility allows the use of Lua as a script interpreter 675This facility allows the use of Lua as a script interpreter
@@ -736,7 +778,7 @@ the list of variables.\index{adjustment}
736If there are more values than needed, 778If there are more values than needed,
737the excess values are thrown away. 779the excess values are thrown away.
738If there are fewer values than needed, 780If there are fewer values than needed,
739the list is extended with as many \nil's as needed. 781the list is extended with as many \nil{}'s as needed.
740If the list of expressions ends with a function call, 782If the list of expressions ends with a function call,
741then all values returned by that function call enter in the list of values, 783then all values returned by that function call enter in the list of values,
742before the adjustment 784before the adjustment
@@ -854,7 +896,7 @@ before the loop starts.
854The names are here for explanatory purposes only. 896The names are here for explanatory purposes only.
855\item The behavior is \emph{undefined} if you assign to \verb|var| inside 897\item The behavior is \emph{undefined} if you assign to \verb|var| inside
856the block. 898the block.
857\item If the third expression (the step) is absent, then a step of~1 is used. 899\item If the third expression (the step) is absent, then a step of\Nb{}1 is used.
858\item You can use \rwd{break} to exit a \rwd{for} loop. 900\item You can use \rwd{break} to exit a \rwd{for} loop.
859\item The loop variable \verb|var| is local to the statement; 901\item The loop variable \verb|var| is local to the statement;
860you cannot use its value after the \rwd{for} ends or is broken. 902you cannot use its value after the \rwd{for} ends or is broken.
@@ -865,7 +907,7 @@ then assign it to another variable before breaking or exiting the loop.
865The generic \rwd{for} statement works over functions, 907The generic \rwd{for} statement works over functions,
866called \Index{generators}. 908called \Index{generators}.
867It calls its generator to produce a new value for each iteration, 909It calls its generator to produce a new value for each iteration,
868stopping when the new value is \nil. 910stopping when the new value is \nil{}.
869It has the following syntax: 911It has the following syntax:
870\begin{Produc} 912\begin{Produc}
871\produc{stat}{\rwd{for} \Nter{Name} \rep{\ter{,} \Nter{Name}} \rwd{in} explist1 913\produc{stat}{\rwd{for} \Nter{Name} \rep{\ter{,} \Nter{Name}} \rwd{in} explist1
@@ -921,7 +963,7 @@ The declaration may include an initial assignment:\IndexKW{local}
921\end{Produc} 963\end{Produc}
922If present, an initial assignment has the same semantics 964If present, an initial assignment has the same semantics
923of a multiple assignment \see{assignment}. 965of a multiple assignment \see{assignment}.
924Otherwise, all variables are initialized with \nil. 966Otherwise, all variables are initialized with \nil{}.
925 967
926A chunk is also a block \see{chunks}, 968A chunk is also a block \see{chunks},
927so local variables can be declared outside any explicit block. 969so local variables can be declared outside any explicit block.
@@ -981,10 +1023,10 @@ The \Index{relational operators} in Lua are
981\begin{verbatim} 1023\begin{verbatim}
982 == ~= < > <= >= 1024 == ~= < > <= >=
983\end{verbatim} 1025\end{verbatim}
984These operators always result in \False{} or \True. 1026These operators always result in \False{} or \True{}.
985 1027
986Equality (\verb|==|) first compares the type of its operands. 1028Equality (\verb|==|) first compares the type of its operands.
987If the types are different, then the result is \False. 1029If the types are different, then the result is \False{}.
988Otherwise, the values of the operands are compared. 1030Otherwise, the values of the operands are compared.
989Numbers and strings are compared in the usual way. 1031Numbers and strings are compared in the usual way.
990Tables, userdata, and functions are compared \emph{by reference}, 1032Tables, userdata, and functions are compared \emph{by reference},
@@ -1024,13 +1066,13 @@ all logical operators consider both \False{} and \nil{} as false
1024and anything else as true. 1066and anything else as true.
1025\IndexKW{and}\IndexKW{or}\IndexKW{not} 1067\IndexKW{and}\IndexKW{or}\IndexKW{not}
1026 1068
1027The operator \rwd{not} always return \False{} or \True. 1069The operator \rwd{not} always return \False{} or \True{}.
1028 1070
1029The conjunction operator \rwd{and} returns its first argument 1071The conjunction operator \rwd{and} returns its first argument
1030if its value is \False{} or \nil; 1072if its value is \False{} or \nil{};
1031otherwise, \rwd{and} returns its second argument. 1073otherwise, \rwd{and} returns its second argument.
1032The disjunction operator \rwd{or} returns its first argument 1074The disjunction operator \rwd{or} returns its first argument
1033if it is different from \nil{} and \False; 1075if it is different from \nil{} and \False{};
1034otherwise, \rwd{or} returns its second argument. 1076otherwise, \rwd{or} returns its second argument.
1035Both \rwd{and} and \rwd{or} use \Index{short-cut evaluation}, 1077Both \rwd{and} and \rwd{or} use \Index{short-cut evaluation},
1036that is, 1078that is,
@@ -1164,11 +1206,11 @@ Because a function can return any number of results
1164\see{return}, 1206\see{return},
1165the number of results must be adjusted before they are used. 1207the number of results must be adjusted before they are used.
1166If the function is called as a statement \see{funcstat}, 1208If the function is called as a statement \see{funcstat},
1167then its return list is adjusted to~0 elements, 1209then its return list is adjusted to\Nb{}0 elements,
1168thus discarding all returned values. 1210thus discarding all returned values.
1169If the function is called inside another expression, 1211If the function is called inside another expression,
1170or in the middle of a list of expressions, 1212or in the middle of a list of expressions,
1171then its return list is adjusted to~1 element, 1213then its return list is adjusted to\Nb{}1 element,
1172thus discarding all returned values but the first one. 1214thus discarding all returned values but the first one.
1173If the function is called as the last element of a list of expressions, 1215If the function is called as the last element of a list of expressions,
1174then no adjustment is made 1216then no adjustment is made
@@ -1277,8 +1319,8 @@ A vararg function does not adjust its argument list;
1277instead, it collects all extra arguments into an implicit parameter, 1319instead, it collects all extra arguments into an implicit parameter,
1278called \IndexLIB{arg}. 1320called \IndexLIB{arg}.
1279The value of \verb|arg| is a table, 1321The value of \verb|arg| is a table,
1280with a field~\verb|n| whose value is the number of extra arguments, 1322with a field\Nb{}\verb|n| whose value is the number of extra arguments,
1281and with the extra arguments at positions 1,~2,~\ldots,~\verb|n|. 1323and with the extra arguments at positions 1,\Nb{}2,\Nb{}\ldots,\Nb{}\verb|n|.
1282 1324
1283As an example, consider the following definitions: 1325As an example, consider the following definitions:
1284\begin{verbatim} 1326\begin{verbatim}
@@ -1377,7 +1419,7 @@ while all of them share the same \verb|x|.
1377\subsection{Error Handling} \label{error} 1419\subsection{Error Handling} \label{error}
1378 1420
1379Because Lua is an extension language, 1421Because Lua is an extension language,
1380all Lua actions start from C~code in the host program 1422all Lua actions start from C\Nb{}code in the host program
1381calling a function from the Lua library \see{pcall}. 1423calling a function from the Lua library \see{pcall}.
1382Whenever an error occurs during Lua compilation or execution, 1424Whenever an error occurs during Lua compilation or execution,
1383control returns to C, 1425control returns to C,
@@ -1689,7 +1731,7 @@ A coroutine can terminate its execution in two ways:
1689Normally, when its main function returns 1731Normally, when its main function returns
1690(explicitly or implicitly, after the last instruction); 1732(explicitly or implicitly, after the last instruction);
1691and abnormally, if there is an unprotected error. 1733and abnormally, if there is an unprotected error.
1692In the first case, \verb|coroutine.resume| returns \True, 1734In the first case, \verb|coroutine.resume| returns \True{},
1693plus any values returned by the coroutine main function. 1735plus any values returned by the coroutine main function.
1694In case of errors, \verb|coroutine.resume| returns \False{} 1736In case of errors, \verb|coroutine.resume| returns \False{}
1695plus an error message. 1737plus an error message.
@@ -1700,7 +1742,7 @@ the corresponding \verb|coroutine.resume| returns immediately,
1700even if the yield happens inside nested function calls 1742even if the yield happens inside nested function calls
1701(that is, not in the main function, 1743(that is, not in the main function,
1702but in a function directly or indirectly called by the main function). 1744but in a function directly or indirectly called by the main function).
1703In the case of a yield, \verb|coroutine.resume| also returns \True, 1745In the case of a yield, \verb|coroutine.resume| also returns \True{},
1704plus any values passed to \verb|coroutine.yield|. 1746plus any values passed to \verb|coroutine.yield|.
1705The next time you resume the same coroutine, 1747The next time you resume the same coroutine,
1706it continues its execution from the point where it yielded, 1748it continues its execution from the point where it yielded,
@@ -1759,12 +1801,12 @@ main false cannot resume dead coroutine
1759 1801
1760 1802
1761 1803
1762\C{------------------------------------------------------------------------------} 1804\C{-------------------------------------------------------------------------}
1763\section{The Application Program Interface}\label{API} 1805\section{The Application Program Interface}\label{API}
1764\index{C API} 1806\index{C API}
1765 1807
1766This section describes the API for Lua, that is, 1808This section describes the API for Lua, that is,
1767the set of C~functions available to the host program to communicate 1809the set of C\Nb{}functions available to the host program to communicate
1768with Lua. 1810with Lua.
1769All API functions and related types and constants 1811All API functions and related types and constants
1770are declared in the header file \verb|lua.h|. 1812are declared in the header file \verb|lua.h|.
@@ -1816,7 +1858,7 @@ to avoid growing too large.
1816\subsection{Threads} 1858\subsection{Threads}
1817 1859
1818Lua offers partial support for multiple threads of execution. 1860Lua offers partial support for multiple threads of execution.
1819If you have a C~library that offers multi-threading, 1861If you have a C\Nb{}library that offers multi-threading,
1820then Lua can cooperate with it to implement the equivalent facility in Lua. 1862then Lua can cooperate with it to implement the equivalent facility in Lua.
1821Also, Lua implements its own coroutine system on top of threads. 1863Also, Lua implements its own coroutine system on top of threads.
1822The following function creates a new \Q{thread} in Lua: 1864The following function creates a new \Q{thread} in Lua:
@@ -1846,7 +1888,7 @@ Instead, you must close the state itself.
1846 1888
1847Lua uses a virtual \emph{stack} to pass values to and from C. 1889Lua uses a virtual \emph{stack} to pass values to and from C.
1848Each element in this stack represents a Lua value 1890Each element in this stack represents a Lua value
1849(\nil, number, string, etc.). 1891(\nil{}, number, string, etc.).
1850 1892
1851Each C invocation has its own stack. 1893Each C invocation has its own stack.
1852Whenever Lua calls C, the called function gets a new stack, 1894Whenever Lua calls C, the called function gets a new stack,
@@ -1859,18 +1901,18 @@ For convenience,
1859most query operations in the API do not follow a strict stack discipline. 1901most query operations in the API do not follow a strict stack discipline.
1860Instead, they can refer to any element in the stack by using an \emph{index}: 1902Instead, they can refer to any element in the stack by using an \emph{index}:
1861A positive index represents an \emph{absolute} stack position 1903A positive index represents an \emph{absolute} stack position
1862(starting at~1); 1904(starting at\Nb{}1);
1863a negative index represents an \emph{offset} from the top of the stack. 1905a negative index represents an \emph{offset} from the top of the stack.
1864More specifically, if the stack has \M{n} elements, 1906More specifically, if the stack has \M{n} elements,
1865then index~1 represents the first element 1907then index\Nb{}1 represents the first element
1866(that is, the element that was pushed onto the stack first), 1908(that is, the element that was pushed onto the stack first),
1867and 1909and
1868index~\M{n} represents the last element; 1910index\Nb{}\M{n} represents the last element;
1869index~\Math{-1} also represents the last element 1911index\Nb{}\Math{-1} also represents the last element
1870(that is, the element at the top), 1912(that is, the element at the top),
1871and index \Math{-n} represents the first element. 1913and index \Math{-n} represents the first element.
1872We say that an index is \emph{valid} 1914We say that an index is \emph{valid}
1873if it lies between~1 and the stack top 1915if it lies between\Nb{}1 and the stack top
1874(that is, if \verb|1 <= abs(index) <= top|). 1916(that is, if \verb|1 <= abs(index) <= top|).
1875\index{stack index} \index{valid index} 1917\index{stack index} \index{valid index}
1876 1918
@@ -1879,9 +1921,9 @@ At any time, you can get the index of the top element by calling
1879 int lua_gettop (lua_State *L); 1921 int lua_gettop (lua_State *L);
1880\end{verbatim} 1922\end{verbatim}
1881\DefAPI{lua_gettop} 1923\DefAPI{lua_gettop}
1882Because indices start at~1, 1924Because indices start at\Nb{}1,
1883the result of \verb|lua_gettop| is equal to the number of elements in the stack 1925the result of \verb|lua_gettop| is equal to the number of elements in the stack
1884(and so 0~means an empty stack). 1926(and so 0\Nb{}means an empty stack).
1885 1927
1886When you interact with Lua API, 1928When you interact with Lua API,
1887\emph{you are responsible for controlling stack overflow}. 1929\emph{you are responsible for controlling stack overflow}.
@@ -1918,7 +1960,7 @@ Note that 0 is never an acceptable index.
1918Unless otherwise noted, 1960Unless otherwise noted,
1919any function that accepts valid indices can also be called with 1961any function that accepts valid indices can also be called with
1920\Index{pseudo-indices}, 1962\Index{pseudo-indices},
1921which represent some Lua values that are accessible to the C~code 1963which represent some Lua values that are accessible to the C\Nb{}code
1922but are not in the stack. 1964but are not in the stack.
1923Pseudo-indices are used to access the table of globals \see{globals}, 1965Pseudo-indices are used to access the table of globals \see{globals},
1924the registry, and the upvalues of a C function \see{c-closure}. 1966the registry, and the upvalues of a C function \see{c-closure}.
@@ -1939,7 +1981,7 @@ The API offers the following functions for basic stack manipulation:
1939or 0, 1981or 0,
1940and sets the stack top to that index. 1982and sets the stack top to that index.
1941If the new top is larger than the old one, 1983If the new top is larger than the old one,
1942then the new elements are filled with \nil. 1984then the new elements are filled with \nil{}.
1943If \verb|index| is 0, then all stack elements are removed. 1985If \verb|index| is 0, then all stack elements are removed.
1944A useful macro defined in the \verb|lua.h| is 1986A useful macro defined in the \verb|lua.h| is
1945\begin{verbatim} 1987\begin{verbatim}
@@ -2021,7 +2063,7 @@ The following function translates such constants to a type name:
2021\end{verbatim} 2063\end{verbatim}
2022\DefAPI{lua_typename} 2064\DefAPI{lua_typename}
2023 2065
2024The \verb|lua_is*| functions return~1 if the object is compatible 2066The \verb|lua_is*| functions return\Nb{}1 if the object is compatible
2025with the given type, and 0 otherwise. 2067with the given type, and 0 otherwise.
2026\verb|lua_isboolean| is an exception to this rule, 2068\verb|lua_isboolean| is an exception to this rule,
2027and it succeeds only for boolean values 2069and it succeeds only for boolean values
@@ -2030,9 +2072,9 @@ as any value has a boolean value).
2030They always return 0 for a non-valid index. 2072They always return 0 for a non-valid index.
2031\verb|lua_isnumber| accepts numbers and numerical strings, 2073\verb|lua_isnumber| accepts numbers and numerical strings,
2032\verb|lua_isstring| accepts strings and numbers \see{coercion}, 2074\verb|lua_isstring| accepts strings and numbers \see{coercion},
2033\verb|lua_isfunction| accepts both Lua functions and C~functions, 2075\verb|lua_isfunction| accepts both Lua functions and C\Nb{}functions,
2034and \verb|lua_isuserdata| accepts both full and light userdata. 2076and \verb|lua_isuserdata| accepts both full and light userdata.
2035To distinguish between Lua functions and C~functions, 2077To distinguish between Lua functions and C\Nb{}functions,
2036you should use \verb|lua_iscfunction|. 2078you should use \verb|lua_iscfunction|.
2037To distinguish between full and light userdata, 2079To distinguish between full and light userdata,
2038you can use \verb|lua_islightuserdata|. 2080you can use \verb|lua_islightuserdata|.
@@ -2050,7 +2092,7 @@ Both functions return 0 if any of the indices are non-valid.
2050 2092
2051\subsection{Getting Values from the Stack}\label{lua-to} 2093\subsection{Getting Values from the Stack}\label{lua-to}
2052 2094
2053To translate a value in the stack to a specific C~type, 2095To translate a value in the stack to a specific C\Nb{}type,
2054you can use the following conversion functions: 2096you can use the following conversion functions:
2055\begin{verbatim} 2097\begin{verbatim}
2056 int lua_toboolean (lua_State *L, int index); 2098 int lua_toboolean (lua_State *L, int index);
@@ -2069,7 +2111,7 @@ they act as if the given value had an incorrect type.
2069\verb|lua_toboolean| converts the Lua value at the given index 2111\verb|lua_toboolean| converts the Lua value at the given index
2070to a C \Q{boolean} value (that is, 0 or 1). 2112to a C \Q{boolean} value (that is, 0 or 1).
2071Like all tests in Lua, it returns 1 for any Lua value different from 2113Like all tests in Lua, it returns 1 for any Lua value different from
2072\False{} and \nil; 2114\False{} and \nil{};
2073otherwise it returns 0. 2115otherwise it returns 0.
2074It also returns 0 when called with a non-valid index. 2116It also returns 0 when called with a non-valid index.
2075(If you want to accept only real boolean values, 2117(If you want to accept only real boolean values,
@@ -2079,7 +2121,7 @@ use \verb|lua_isboolean| to test the type of the value.)
2079to a number (by default, \verb|lua_Number| is \verb|double|). 2121to a number (by default, \verb|lua_Number| is \verb|double|).
2080\DefAPI{lua_Number} 2122\DefAPI{lua_Number}
2081The Lua value must be a number or a string convertible to number 2123The Lua value must be a number or a string convertible to number
2082\see{coercion}; otherwise, \verb|lua_tonumber| returns~0. 2124\see{coercion}; otherwise, \verb|lua_tonumber| returns\Nb{}0.
2083 2125
2084\verb|lua_tostring| converts the Lua value at the given index to a string 2126\verb|lua_tostring| converts the Lua value at the given index to a string
2085(\verb|const char*|). 2127(\verb|const char*|).
@@ -2093,7 +2135,7 @@ when \verb|lua_tostring| is applied to keys.)
2093\verb|lua_tostring| returns a fully aligned pointer 2135\verb|lua_tostring| returns a fully aligned pointer
2094to a string inside the Lua environment. 2136to a string inside the Lua environment.
2095This string always has a zero (\verb|'\0'|) 2137This string always has a zero (\verb|'\0'|)
2096after its last character (as in~C), 2138after its last character (as in\Nb{}C),
2097but may contain other zeros in its body. 2139but may contain other zeros in its body.
2098If you do not know whether a string may contain zeros, 2140If you do not know whether a string may contain zeros,
2099you can use \verb|lua_strlen| to get its actual length. 2141you can use \verb|lua_strlen| to get its actual length.
@@ -2103,8 +2145,8 @@ will be valid after the corresponding value is removed from the stack.
2103If you need the string after the current function returns, 2145If you need the string after the current function returns,
2104then you should duplicate it (or put it into the registry \see{registry}). 2146then you should duplicate it (or put it into the registry \see{registry}).
2105 2147
2106\verb|lua_tocfunction| converts a value in the stack to a C~function. 2148\verb|lua_tocfunction| converts a value in the stack to a C\Nb{}function.
2107This value must be a C~function; 2149This value must be a C\Nb{}function;
2108otherwise, \verb|lua_tocfunction| returns \verb|NULL|. 2150otherwise, \verb|lua_tocfunction| returns \verb|NULL|.
2109The type \verb|lua_CFunction| is explained in \See{LuacallC}. 2151The type \verb|lua_CFunction| is explained in \See{LuacallC}.
2110 2152
@@ -2114,7 +2156,7 @@ The type \verb|lua_CFunction| is explained in \See{LuacallC}.
2114\subsection{Pushing Values onto the Stack} 2156\subsection{Pushing Values onto the Stack}
2115 2157
2116The API has the following functions to 2158The API has the following functions to
2117push C~values onto the stack: 2159push C\Nb{}values onto the stack:
2118\begin{verbatim} 2160\begin{verbatim}
2119 void lua_pushboolean (lua_State *L, int b); 2161 void lua_pushboolean (lua_State *L, int b);
2120 void lua_pushnumber (lua_State *L, lua_Number n); 2162 void lua_pushnumber (lua_State *L, lua_Number n);
@@ -2128,12 +2170,12 @@ push C~values onto the stack:
2128\DefAPI{lua_pushnumber}\DefAPI{lua_pushlstring}\DefAPI{lua_pushstring} 2170\DefAPI{lua_pushnumber}\DefAPI{lua_pushlstring}\DefAPI{lua_pushstring}
2129\DefAPI{lua_pushcfunction}\DefAPI{lua_pushlightuserdata}\DefAPI{lua_pushboolean} 2171\DefAPI{lua_pushcfunction}\DefAPI{lua_pushlightuserdata}\DefAPI{lua_pushboolean}
2130\DefAPI{lua_pushnil}\label{pushing} 2172\DefAPI{lua_pushnil}\label{pushing}
2131These functions receive a C~value, 2173These functions receive a C\Nb{}value,
2132convert it to a corresponding Lua value, 2174convert it to a corresponding Lua value,
2133and push the result onto the stack. 2175and push the result onto the stack.
2134In particular, \verb|lua_pushlstring| and \verb|lua_pushstring| 2176In particular, \verb|lua_pushlstring| and \verb|lua_pushstring|
2135make an internal copy of the given string. 2177make an internal copy of the given string.
2136\verb|lua_pushstring| can only be used to push proper C~strings 2178\verb|lua_pushstring| can only be used to push proper C\Nb{}strings
2137(that is, strings that end with a zero and do not contain embedded zeros); 2179(that is, strings that end with a zero and do not contain embedded zeros);
2138otherwise, you should use the more general \verb|lua_pushlstring|, 2180otherwise, you should use the more general \verb|lua_pushlstring|,
2139which accepts an explicit size. 2181which accepts an explicit size.
@@ -2444,7 +2486,7 @@ removing this value from the stack.
2444\subsection{Calling Functions} 2486\subsection{Calling Functions}
2445 2487
2446Functions defined in Lua 2488Functions defined in Lua
2447and C~functions registered in Lua 2489and C\Nb{}functions registered in Lua
2448can be called from the host program. 2490can be called from the host program.
2449This is done using the following protocol: 2491This is done using the following protocol:
2450First, the function to be called is pushed onto the stack; 2492First, the function to be called is pushed onto the stack;
@@ -2471,7 +2513,7 @@ equivalent to this Lua code:
2471\begin{verbatim} 2513\begin{verbatim}
2472 a = f("how", t.x, 14) 2514 a = f("how", t.x, 14)
2473\end{verbatim} 2515\end{verbatim}
2474Here it is in~C: 2516Here it is in\Nb{}C:
2475\begin{verbatim} 2517\begin{verbatim}
2476 lua_pushstring(L, "t"); 2518 lua_pushstring(L, "t");
2477 lua_gettable(L, LUA_GLOBALSINDEX); /* global `t' (for later use) */ 2519 lua_gettable(L, LUA_GLOBALSINDEX); /* global `t' (for later use) */
@@ -2548,7 +2590,7 @@ error while running the error handler function.
2548 2590
2549>>>> 2591>>>>
2550\C{ TODO: mover essas 2 para algum lugar melhor.} 2592\C{ TODO: mover essas 2 para algum lugar melhor.}
2551Some special Lua functions have their own C~interfaces. 2593Some special Lua functions have their own C\Nb{}interfaces.
2552The host program can generate a Lua error calling the function 2594The host program can generate a Lua error calling the function
2553\begin{verbatim} 2595\begin{verbatim}
2554 void lua_error (lua_State *L); 2596 void lua_error (lua_State *L);
@@ -2557,7 +2599,7 @@ The host program can generate a Lua error calling the function
2557The error message (which actually can be any type of object) 2599The error message (which actually can be any type of object)
2558is popped from the stack. 2600is popped from the stack.
2559This function never returns. 2601This function never returns.
2560If \verb|lua_error| is called from a C~function that 2602If \verb|lua_error| is called from a C\Nb{}function that
2561has been called from Lua, 2603has been called from Lua,
2562then the corresponding Lua execution terminates, 2604then the corresponding Lua execution terminates,
2563as if an error had occurred inside Lua code. 2605as if an error had occurred inside Lua code.
@@ -2581,29 +2623,29 @@ Concatenation is done following the usual semantics of Lua
2581 2623
2582\subsection{Defining C Functions} \label{LuacallC} 2624\subsection{Defining C Functions} \label{LuacallC}
2583 2625
2584Lua can be extended with functions written in~C. 2626Lua can be extended with functions written in\Nb{}C.
2585These functions must be of type \verb|lua_CFunction|, 2627These functions must be of type \verb|lua_CFunction|,
2586which is defined as 2628which is defined as
2587\begin{verbatim} 2629\begin{verbatim}
2588 typedef int (*lua_CFunction) (lua_State *L); 2630 typedef int (*lua_CFunction) (lua_State *L);
2589\end{verbatim} 2631\end{verbatim}
2590\DefAPI{lua_CFunction} 2632\DefAPI{lua_CFunction}
2591A C~function receives a Lua environment and returns an integer, 2633A C\Nb{}function receives a Lua environment and returns an integer,
2592the number of values it has returned to Lua. 2634the number of values it has returned to Lua.
2593 2635
2594In order to communicate properly with Lua, 2636In order to communicate properly with Lua,
2595a C~function must follow the following protocol, 2637a C\Nb{}function must follow the following protocol,
2596which defines the way parameters and results are passed: 2638which defines the way parameters and results are passed:
2597A C~function receives its arguments from Lua in its stack, 2639A C\Nb{}function receives its arguments from Lua in its stack,
2598in direct order (the first argument is pushed first). 2640in direct order (the first argument is pushed first).
2599So, when the function starts, 2641So, when the function starts,
2600its first argument (if any) is at index 1. 2642its first argument (if any) is at index 1.
2601To return values to Lua, a C~function just pushes them onto the stack, 2643To return values to Lua, a C\Nb{}function just pushes them onto the stack,
2602in direct order (the first result is pushed first), 2644in direct order (the first result is pushed first),
2603and returns the number of results. 2645and returns the number of results.
2604Any other value in the stack below the results will be properly 2646Any other value in the stack below the results will be properly
2605discharged by Lua. 2647discharged by Lua.
2606Like a Lua function, a C~function called by Lua can also return 2648Like a Lua function, a C\Nb{}function called by Lua can also return
2607many results. 2649many results.
2608 2650
2609As an example, the following function receives a variable number 2651As an example, the following function receives a variable number
@@ -2626,7 +2668,7 @@ of numerical arguments and returns their average and sum:
2626 } 2668 }
2627\end{verbatim} 2669\end{verbatim}
2628 2670
2629To register a C~function to Lua, 2671To register a C\Nb{}function to Lua,
2630there is the following convenience macro: 2672there is the following convenience macro:
2631\begin{verbatim} 2673\begin{verbatim}
2632 #define lua_register(L,n,f) \ 2674 #define lua_register(L,n,f) \
@@ -2640,7 +2682,7 @@ there is the following convenience macro:
2640which receives the name the function will have in Lua, 2682which receives the name the function will have in Lua,
2641and a pointer to the function. 2683and a pointer to the function.
2642Thus, 2684Thus,
2643the C~function `\verb|foo|' above may be registered in Lua as `\verb|average|' 2685the C\Nb{}function `\verb|foo|' above may be registered in Lua as `\verb|average|'
2644by calling 2686by calling
2645\begin{verbatim} 2687\begin{verbatim}
2646 lua_register(L, "average", foo); 2688 lua_register(L, "average", foo);
@@ -2648,11 +2690,11 @@ by calling
2648 2690
2649\subsection{Defining C Closures} \label{c-closure} 2691\subsection{Defining C Closures} \label{c-closure}
2650 2692
2651When a C~function is created, 2693When a C\Nb{}function is created,
2652it is possible to associate some values with it, 2694it is possible to associate some values with it,
2653thus creating a \IndexEmph{C~closure}; 2695thus creating a \IndexEmph{C\Nb{}closure};
2654these values are then accessible to the function whenever it is called. 2696these values are then accessible to the function whenever it is called.
2655To associate values with a C~function, 2697To associate values with a C\Nb{}function,
2656first these values should be pushed onto the stack 2698first these values should be pushed onto the stack
2657(when there are multiple values, the first value is pushed first). 2699(when there are multiple values, the first value is pushed first).
2658Then the function 2700Then the function
@@ -2660,33 +2702,33 @@ Then the function
2660 void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n); 2702 void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
2661\end{verbatim} 2703\end{verbatim}
2662\DefAPI{lua_pushcclosure} 2704\DefAPI{lua_pushcclosure}
2663is used to push the C~function onto the stack, 2705is used to push the C\Nb{}function onto the stack,
2664with the argument \verb|n| telling how many values should be 2706with the argument \verb|n| telling how many values should be
2665associated with the function 2707associated with the function
2666(\verb|lua_pushcclosure| also pops these values from the stack); 2708(\verb|lua_pushcclosure| also pops these values from the stack);
2667in fact, the macro \verb|lua_pushcfunction| is defined as 2709in fact, the macro \verb|lua_pushcfunction| is defined as
2668\verb|lua_pushcclosure| with \verb|n| set to 0. 2710\verb|lua_pushcclosure| with \verb|n| set to 0.
2669 2711
2670Then, whenever the C~function is called, 2712Then, whenever the C\Nb{}function is called,
2671those values are located at specific pseudo-indices. 2713those values are located at specific pseudo-indices.
2672Those pseudo-indices are produced by a macro \IndexAPI{lua_upvalueindex}. 2714Those pseudo-indices are produced by a macro \IndexAPI{lua_upvalueindex}.
2673The first value associated with a function is at position 2715The first value associated with a function is at position
2674\verb|lua_upvalueindex(1)|, and so on. 2716\verb|lua_upvalueindex(1)|, and so on.
2675 2717
2676For examples of C~functions and closures, see files 2718For examples of C\Nb{}functions and closures, see files
2677\verb|lbaselib.c|, \verb|liolib.c|, \verb|lmathlib.c|, and \verb|lstrlib.c| 2719\verb|lbaselib.c|, \verb|liolib.c|, \verb|lmathlib.c|, and \verb|lstrlib.c|
2678in the official Lua distribution. 2720in the official Lua distribution.
2679 2721
2680 2722
2681\subsubsection*{Registry} \label{registry} 2723\subsubsection*{Registry} \label{registry}
2682 2724
2683Lua provides a pre-defined table that can be used by any C~code to 2725Lua provides a pre-defined table that can be used by any C\Nb{}code to
2684store whatever Lua value it needs to store, 2726store whatever Lua value it needs to store,
2685especially if the C~code needs to keep that Lua value 2727especially if the C\Nb{}code needs to keep that Lua value
2686outside the life span of a C~function. 2728outside the life span of a C\Nb{}function.
2687This table is always located at pseudo-index 2729This table is always located at pseudo-index
2688\IndexAPI{LUA_REGISTRYINDEX}. 2730\IndexAPI{LUA_REGISTRYINDEX}.
2689Any C~library can store data into this table, 2731Any C\Nb{}library can store data into this table,
2690as long as it chooses keys different from other libraries. 2732as long as it chooses keys different from other libraries.
2691Typically, you should use as key a string containing your library name, 2733Typically, you should use as key a string containing your library name,
2692or a light userdata with the address of a C object in your code. 2734or a light userdata with the address of a C object in your code.
@@ -2696,7 +2738,7 @@ implemented by the auxiliary library,
2696and therefore should not be used by other purposes. 2738and therefore should not be used by other purposes.
2697 2739
2698 2740
2699\C{------------------------------------------------------------------------------} 2741\C{-------------------------------------------------------------------------}
2700\section{The Debug Interface} \label{debugI} 2742\section{The Debug Interface} \label{debugI}
2701 2743
2702Lua has no built-in debugging facilities. 2744Lua has no built-in debugging facilities.
@@ -2716,7 +2758,7 @@ The main function to get information about the interpreter stack is
2716This function fills parts of a \verb|lua_Debug| structure with 2758This function fills parts of a \verb|lua_Debug| structure with
2717an identification of the \emph{activation record} 2759an identification of the \emph{activation record}
2718of the function executing at a given level. 2760of the function executing at a given level.
2719Level~0 is the current running function, 2761Level\Nb{}0 is the current running function,
2720whereas level \Math{n+1} is the function that has called level \Math{n}. 2762whereas level \Math{n+1} is the function that has called level \Math{n}.
2721Usually, \verb|lua_getstack| returns 1; 2763Usually, \verb|lua_getstack| returns 1;
2722when called with a level greater than the stack depth, 2764when called with a level greater than the stack depth,
@@ -2781,16 +2823,16 @@ The fields of \verb|lua_Debug| have the following meaning:
2781If the function was defined in a string, 2823If the function was defined in a string,
2782then \verb|source| is that string; 2824then \verb|source| is that string;
2783if the function was defined in a file, 2825if the function was defined in a file,
2784then \verb|source| starts with a \verb|@| followed by the file name. 2826then \verb|source| starts with a \At{} followed by the file name.
2785 2827
2786\item[short\_src] 2828\item[short_src]
2787A \Q{printable} version of \verb|source|, to be used in error messages. 2829A \Q{printable} version of \verb|source|, to be used in error messages.
2788 2830
2789\item[linedefined] 2831\item[linedefined]
2790the line number where the definition of the function starts. 2832the line number where the definition of the function starts.
2791 2833
2792\item[what] the string \verb|"Lua"| if this is a Lua function, 2834\item[what] the string \verb|"Lua"| if this is a Lua function,
2793\verb|"C"| if this is a C~function, 2835\verb|"C"| if this is a C\Nb{}function,
2794or \verb|"main"| if this is the main part of a chunk. 2836or \verb|"main"| if this is the main part of a chunk.
2795 2837
2796\item[currentline] 2838\item[currentline]
@@ -2827,7 +2869,7 @@ Number of upvalues of the function.
2827 2869
2828For the manipulation of local variables, 2870For the manipulation of local variables,
2829\verb|luadebug.h| uses indices: 2871\verb|luadebug.h| uses indices:
2830The first parameter or local variable has index~1, and so on, 2872The first parameter or local variable has index\Nb{}1, and so on,
2831until the last active local variable. 2873until the last active local variable.
2832 2874
2833The following functions allow the manipulation of the 2875The following functions allow the manipulation of the
@@ -2936,11 +2978,11 @@ Therefore, if a hook calls Lua to execute a function or a chunk,
2936that execution occurs without any calls to hooks. 2978that execution occurs without any calls to hooks.
2937 2979
2938 2980
2939\C{------------------------------------------------------------------------------} 2981\C{-------------------------------------------------------------------------}
2940\section{Standard Libraries}\label{libraries} 2982\section{Standard Libraries}\label{libraries}
2941 2983
2942The standard libraries provide useful functions 2984The standard libraries provide useful functions
2943that are implemented directly through the standard C~API. 2985that are implemented directly through the standard C\Nb{}API.
2944Some of these functions provide essential services to the language 2986Some of these functions provide essential services to the language
2945(e.g. \verb|type| and \verb|getmetatable|); 2987(e.g. \verb|type| and \verb|getmetatable|);
2946others provide access to \Q{outside} services (e.g. I/O); 2988others provide access to \Q{outside} services (e.g. I/O);
@@ -2949,7 +2991,7 @@ but are quite useful or have critical performance to
2949deserve an implementation in C (e.g. \verb|sort|). 2991deserve an implementation in C (e.g. \verb|sort|).
2950 2992
2951All libraries are implemented through the official C API, 2993All libraries are implemented through the official C API,
2952and are provided as separate C~modules. 2994and are provided as separate C\Nb{}modules.
2953Currently, Lua has the following standard libraries: 2995Currently, Lua has the following standard libraries:
2954\begin{itemize} 2996\begin{itemize}
2955\item basic library; 2997\item basic library;
@@ -2965,7 +3007,7 @@ each library provides all its functions as fields of a global table
2965or as methods of its objects. 3007or as methods of its objects.
2966 3008
2967To have access to these libraries, 3009To have access to these libraries,
2968the C~host program must call the functions 3010the C\Nb{}host program must call the functions
2969\verb|lua_baselibopen| (for the basic library), 3011\verb|lua_baselibopen| (for the basic library),
2970\verb|lua_strlibopen| (for the string library), 3012\verb|lua_strlibopen| (for the string library),
2971\verb|lua_tablibopen| (for the table library), 3013\verb|lua_tablibopen| (for the table library),
@@ -2994,7 +3036,7 @@ The current content of this string is {\tt "Lua \Version"}.
2994 3036
2995\subsubsection*{\ff \T{assert (v [, message])}}\DefLIB{assert} 3037\subsubsection*{\ff \T{assert (v [, message])}}\DefLIB{assert}
2996Issues an \emph{\Q{assertion failed!}} error 3038Issues an \emph{\Q{assertion failed!}} error
2997when its argument \verb|v| is \nil{} or \False; 3039when its argument \verb|v| is \nil{} or \False{};
2998otherwise, returns this argument. 3040otherwise, returns this argument.
2999This function is equivalent to the following Lua function: 3041This function is equivalent to the following Lua function:
3000\begin{verbatim} 3042\begin{verbatim}
@@ -3048,7 +3090,7 @@ The default for \verb|function| is 1.
3048\DefLIB{getmetatable}\label{pdf-getmetatable} 3090\DefLIB{getmetatable}\label{pdf-getmetatable}
3049 3091
3050Returns the metatable of the given object. 3092Returns the metatable of the given object.
3051If the object does not have a metatable, returns \nil. 3093If the object does not have a metatable, returns \nil{}.
3052 3094
3053\subsubsection*{\ff \T{gcinfo ()}}\DefLIB{gcinfo} 3095\subsubsection*{\ff \T{gcinfo ()}}\DefLIB{gcinfo}
3054Returns the number of Kbytes of dynamic memory Lua is using, 3096Returns the number of Kbytes of dynamic memory Lua is using,
@@ -3097,12 +3139,12 @@ When called with \nil{} as its second argument,
3097of the table and its associated value. 3139of the table and its associated value.
3098When called with the last index, 3140When called with the last index,
3099or with \nil{} in an empty table, 3141or with \nil{} in an empty table,
3100\verb|next| returns \nil. 3142\verb|next| returns \nil{}.
3101If the second argument is absent, then it is interpreted as \nil. 3143If the second argument is absent, then it is interpreted as \nil{}.
3102 3144
3103Lua has no declaration of fields; 3145Lua has no declaration of fields;
3104semantically, there is no difference between a 3146semantically, there is no difference between a
3105field not present in a table or a field with value \nil. 3147field not present in a table or a field with value \nil{}.
3106Therefore, \verb|next| only considers fields with non-\nil{} values. 3148Therefore, \verb|next| only considers fields with non-\nil{} values.
3107The order in which the indices are enumerated is not specified, 3149The order in which the indices are enumerated is not specified,
3108\emph{even for numeric indices}. 3150\emph{even for numeric indices}.
@@ -3114,7 +3156,7 @@ the table during the traversal.
3114 3156
3115\subsubsection*{\ff \T{pairs (t)}}\DefLIB{pairs} 3157\subsubsection*{\ff \T{pairs (t)}}\DefLIB{pairs}
3116 3158
3117Returns the function \verb|next| and the table \verb|t| (plus a \nil), 3159Returns the function \verb|next| and the table \verb|t| (plus a \nil{}),
3118so that the construction 3160so that the construction
3119\begin{verbatim} 3161\begin{verbatim}
3120 for k,v in pairs(t) do ... end 3162 for k,v in pairs(t) do ... end
@@ -3147,13 +3189,13 @@ For formatted output, see \verb|format| \see{format}.
3147Gets the real value of \verb|table[index]|, 3189Gets the real value of \verb|table[index]|,
3148without invoking any metamethod. 3190without invoking any metamethod.
3149\verb|table| must be a table; 3191\verb|table| must be a table;
3150\verb|index| is any value different from \nil. 3192\verb|index| is any value different from \nil{}.
3151 3193
3152\subsubsection*{\ff \T{rawset (table, index, value)}}\DefLIB{rawset} 3194\subsubsection*{\ff \T{rawset (table, index, value)}}\DefLIB{rawset}
3153Sets the real value of \verb|table[index]| to \verb|value|, 3195Sets the real value of \verb|table[index]| to \verb|value|,
3154without invoking any metamethod. 3196without invoking any metamethod.
3155\verb|table| must be a table; 3197\verb|table| must be a table;
3156\verb|index| is any value different from \nil; 3198\verb|index| is any value different from \nil{};
3157and \verb|value| is any Lua value. 3199and \verb|value| is any Lua value.
3158 3200
3159\subsubsection*{\ff \T{require (packagename)}}\DefLIB{require} 3201\subsubsection*{\ff \T{require (packagename)}}\DefLIB{require}
@@ -3205,18 +3247,18 @@ Level 1 is the function calling \verb|setglobals|.
3205 3247
3206Sets the metatable for the given table. 3248Sets the metatable for the given table.
3207(You cannot change the metatable of a userdata from Lua.) 3249(You cannot change the metatable of a userdata from Lua.)
3208If \verb|metatable| is \nil, removes the metatable of the given table. 3250If \verb|metatable| is \nil{}, removes the metatable of the given table.
3209 3251
3210\subsubsection*{\ff \T{tonumber (e [, base])}}\DefLIB{tonumber} 3252\subsubsection*{\ff \T{tonumber (e [, base])}}\DefLIB{tonumber}
3211Tries to convert its argument to a number. 3253Tries to convert its argument to a number.
3212If the argument is already a number or a string convertible 3254If the argument is already a number or a string convertible
3213to a number, then \verb|tonumber| returns that number; 3255to a number, then \verb|tonumber| returns that number;
3214otherwise, it returns \nil. 3256otherwise, it returns \nil{}.
3215 3257
3216An optional argument specifies the base to interpret the numeral. 3258An optional argument specifies the base to interpret the numeral.
3217The base may be any integer between 2 and 36, inclusive. 3259The base may be any integer between 2 and 36, inclusive.
3218In bases above~10, the letter `A' (in either upper or lower case) 3260In bases above\Nb{}10, the letter `A' (in either upper or lower case)
3219represents~10, `B' represents~11, and so forth, with `Z' representing 35. 3261represents\Nb{}10, `B' represents\Nb{}11, and so forth, with `Z' representing 35.
3220In base 10 (the default), the number may have a decimal part, 3262In base 10 (the default), the number may have a decimal part,
3221as well as an optional exponent part \see{coercion}. 3263as well as an optional exponent part \see{coercion}.
3222In other bases, only unsigned integers are accepted. 3264In other bases, only unsigned integers are accepted.
@@ -3230,7 +3272,7 @@ use \verb|format| \see{format}.
3230\subsubsection*{\ff \T{type (v)}}\DefLIB{type}\label{pdf-type} 3272\subsubsection*{\ff \T{type (v)}}\DefLIB{type}\label{pdf-type}
3231Returns the type of its only argument, coded as a string. 3273Returns the type of its only argument, coded as a string.
3232The possible results of this function are 3274The possible results of this function are
3233\verb|"nil"| (a string, not the value \nil), 3275\verb|"nil"| (a string, not the value \nil{}),
3234\verb|"number"|, 3276\verb|"number"|,
3235\verb|"string"|, 3277\verb|"string"|,
3236\verb|"table"|, 3278\verb|"table"|,
@@ -3246,13 +3288,13 @@ This function is equivalent to
3246except that the above code can be valid only for a fixed \M{n}. 3288except that the above code can be valid only for a fixed \M{n}.
3247The number \M{n} of returned values 3289The number \M{n} of returned values
3248is either the value of \verb|list.n|, if it is a number, 3290is either the value of \verb|list.n|, if it is a number,
3249or one less the index of the first absent (\nil) value. 3291or one less the index of the first absent (\nil{}) value.
3250 3292
3251\subsection{String Manipulation} 3293\subsection{String Manipulation}
3252This library provides generic functions for string manipulation, 3294This library provides generic functions for string manipulation,
3253such as finding and extracting substrings and pattern matching. 3295such as finding and extracting substrings and pattern matching.
3254When indexing a string in Lua, the first character is at position~1 3296When indexing a string in Lua, the first character is at position\Nb{}1
3255(not at~0, as in C). 3297(not at\Nb{}0, as in C).
3256Indices are allowed to be negative and are interpreted as indexing backwards, 3298Indices are allowed to be negative and are interpreted as indexing backwards,
3257from the end of the string. 3299from the end of the string.
3258Thus, the last character is at position \Math{-1}, and so on. 3300Thus, the last character is at position \Math{-1}, and so on.
@@ -3262,7 +3304,7 @@ The string library provides all its functions inside the table
3262 3304
3263\subsubsection*{\ff \T{string.byte (s [, i])}}\DefLIB{string.byte} 3305\subsubsection*{\ff \T{string.byte (s [, i])}}\DefLIB{string.byte}
3264Returns the internal numerical code of the \M{i}-th character of \verb|s|. 3306Returns the internal numerical code of the \M{i}-th character of \verb|s|.
3265If \verb|i| is absent, then it is assumed to be~1. 3307If \verb|i| is absent, then it is assumed to be\Nb{}1.
3266\verb|i| may be negative. 3308\verb|i| may be negative.
3267 3309
3268\NOTE 3310\NOTE
@@ -3283,12 +3325,12 @@ Looks for the first \emph{match} of
3283\verb|pattern| in the string \verb|s|. 3325\verb|pattern| in the string \verb|s|.
3284If it finds one, then \verb|find| returns the indices of \verb|s| 3326If it finds one, then \verb|find| returns the indices of \verb|s|
3285where this occurrence starts and ends; 3327where this occurrence starts and ends;
3286otherwise, it returns \nil. 3328otherwise, it returns \nil{}.
3287If the pattern specifies captures (see \verb|string.gsub| below), 3329If the pattern specifies captures (see \verb|string.gsub| below),
3288the captured strings are returned as extra results. 3330the captured strings are returned as extra results.
3289A third, optional numerical argument \verb|init| specifies 3331A third, optional numerical argument \verb|init| specifies
3290where to start the search; 3332where to start the search;
3291its default value is~1, and may be negative. 3333its default value is\Nb{}1, and may be negative.
3292A value of \True{} as a fourth, optional argument \verb|plain| 3334A value of \True{} as a fourth, optional argument \verb|plain|
3293turns off the pattern matching facilities, 3335turns off the pattern matching facilities,
3294so the function does a plain \Q{find substring} operation, 3336so the function does a plain \Q{find substring} operation,
@@ -3334,7 +3376,7 @@ The definition of what is a lowercase letter depends on the current locale.
3334Returns a formatted version of its variable number of arguments 3376Returns a formatted version of its variable number of arguments
3335following the description given in its first argument (which must be a string). 3377following the description given in its first argument (which must be a string).
3336The format string follows the same rules as the \verb|printf| family of 3378The format string follows the same rules as the \verb|printf| family of
3337standard C~functions. 3379standard C\Nb{}functions.
3338The only differences are that the options/modifiers 3380The only differences are that the options/modifiers
3339\verb|*|, \verb|l|, \verb|L|, \verb|n|, \verb|p|, 3381\verb|*|, \verb|l|, \verb|L|, \verb|n|, \verb|p|,
3340and \verb|h| are not supported, 3382and \verb|h| are not supported,
@@ -3459,17 +3501,17 @@ The following combinations are allowed in describing a character class:
3459\verb|^$()%.[]*+-?|) 3501\verb|^$()%.[]*+-?|)
3460\Em{} represents the character \emph{x} itself. 3502\Em{} represents the character \emph{x} itself.
3461\item[\T{.}] \Em{} (a dot) represents all characters. 3503\item[\T{.}] \Em{} (a dot) represents all characters.
3462\item[\T{\%a}] \Em{} represents all letters. 3504\item[\T{%a}] \Em{} represents all letters.
3463\item[\T{\%c}] \Em{} represents all control characters. 3505\item[\T{%c}] \Em{} represents all control characters.
3464\item[\T{\%d}] \Em{} represents all digits. 3506\item[\T{%d}] \Em{} represents all digits.
3465\item[\T{\%l}] \Em{} represents all lowercase letters. 3507\item[\T{%l}] \Em{} represents all lowercase letters.
3466\item[\T{\%p}] \Em{} represents all punctuation characters. 3508\item[\T{%p}] \Em{} represents all punctuation characters.
3467\item[\T{\%s}] \Em{} represents all space characters. 3509\item[\T{%s}] \Em{} represents all space characters.
3468\item[\T{\%u}] \Em{} represents all uppercase letters. 3510\item[\T{%u}] \Em{} represents all uppercase letters.
3469\item[\T{\%w}] \Em{} represents all alphanumeric characters. 3511\item[\T{%w}] \Em{} represents all alphanumeric characters.
3470\item[\T{\%x}] \Em{} represents all hexadecimal digits. 3512\item[\T{%x}] \Em{} represents all hexadecimal digits.
3471\item[\T{\%z}] \Em{} represents the character with representation 0. 3513\item[\T{%z}] \Em{} represents the character with representation 0.
3472\item[\T{\%\M{x}}] (where \M{x} is any non-alphanumeric character) \Em{} 3514\item[\T{%\M{x}}] (where \M{x} is any non-alphanumeric character) \Em{}
3473represents the character \M{x}. 3515represents the character \M{x}.
3474This is the standard way to escape the magic characters. 3516This is the standard way to escape the magic characters.
3475We recommend that any punctuation character (even the non magic) 3517We recommend that any punctuation character (even the non magic)
@@ -3494,7 +3536,7 @@ The interaction between ranges and classes is not defined.
3494Therefore, patterns like \verb|[%a-z]| or \verb|[a-%%]| 3536Therefore, patterns like \verb|[%a-z]| or \verb|[a-%%]|
3495have no meaning. 3537have no meaning.
3496 3538
3497\item[\T{[\^\null\M{set}]}] \Em{} 3539\item[\T{[^\M{set}]}] \Em{}
3498represents the complement of \M{set}, 3540represents the complement of \M{set},
3499where \M{set} is interpreted as above. 3541where \M{set} is interpreted as above.
3500\end{description} 3542\end{description}
@@ -3502,7 +3544,7 @@ For all classes represented by single letters (\verb|%a|, \verb|%c|, \ldots),
3502the corresponding uppercase letter represents the complement of the class. 3544the corresponding uppercase letter represents the complement of the class.
3503For instance, \verb|%S| represents all non-space characters. 3545For instance, \verb|%S| represents all non-space characters.
3504 3546
3505The definitions of letter, space, etc.\ depend on the current locale. 3547The definitions of letter, space, etc.{} depend on the current locale.
3506In particular, the class \verb|[a-z]| may not be equivalent to \verb|%l|. 3548In particular, the class \verb|[a-z]| may not be equivalent to \verb|%l|.
3507The second form should be preferred for portability. 3549The second form should be preferred for portability.
3508 3550
@@ -3529,12 +3571,12 @@ these repetition items will always match the \emph{shortest} possible sequence;
3529a single character class followed by \verb|?|, 3571a single character class followed by \verb|?|,
3530which matches 0 or 1 occurrence of a character in the class; 3572which matches 0 or 1 occurrence of a character in the class;
3531\item 3573\item
3532\T{\%\M{n}}, for \M{n} between 1 and 9; 3574\T{%\M{n}}, for \M{n} between 1 and 9;
3533such item matches a substring equal to the \M{n}-th captured string 3575such item matches a substring equal to the \M{n}-th captured string
3534(see below); 3576(see below);
3535\item 3577\item
3536\T{\%b\M{xy}}, where \M{x} and \M{y} are two distinct characters; 3578\T{%b\M{xy}}, where \M{x} and \M{y} are two distinct characters;
3537such item matches strings that start with~\M{x}, end with~\M{y}, 3579such item matches strings that start with\Nb{}\M{x}, end with\Nb{}\M{y},
3538and where the \M{x} and \M{y} are \emph{balanced}. 3580and where the \M{x} and \M{y} are \emph{balanced}.
3539This means that, if one reads the string from left to right, 3581This means that, if one reads the string from left to right,
3540counting \Math{+1} for an \M{x} and \Math{-1} for a \M{y}, 3582counting \Math{+1} for an \M{x} and \Math{-1} for a \M{y},
@@ -3560,9 +3602,9 @@ that match captures are stored (\emph{captured}) for future use.
3560Captures are numbered according to their left parentheses. 3602Captures are numbered according to their left parentheses.
3561For instance, in the pattern \verb|"(a*(.)%w(%s*))"|, 3603For instance, in the pattern \verb|"(a*(.)%w(%s*))"|,
3562the part of the string matching \verb|"a*(.)%w(%s*)"| is 3604the part of the string matching \verb|"a*(.)%w(%s*)"| is
3563stored as the first capture (and therefore has number~1); 3605stored as the first capture (and therefore has number\Nb{}1);
3564the character matching \verb|.| is captured with number~2, 3606the character matching \verb|.| is captured with number\Nb{}2,
3565and the part matching \verb|%s*| has number~3. 3607and the part matching \verb|%s*| has number\Nb{}3.
3566 3608
3567As a special case, the empty capture \verb|()| captures 3609As a special case, the empty capture \verb|()| captures
3568the current string position (a number). 3610the current string position (a number).
@@ -3621,7 +3663,7 @@ numerical indices of \verb|table|.
3621For each index, \verb|func| is called with the index and 3663For each index, \verb|func| is called with the index and
3622respective value as arguments. 3664respective value as arguments.
3623Indices are visited in sequential order, 3665Indices are visited in sequential order,
3624from~1 to \verb|n|, 3666from\Nb{}1 to \verb|n|,
3625where \verb|n| is the size of the table \see{getn}. 3667where \verb|n| is the size of the table \see{getn}.
3626If \verb|func| returns a non-\nil{} value, 3668If \verb|func| returns a non-\nil{} value,
3627then the loop is broken, and this value is returned 3669then the loop is broken, and this value is returned
@@ -3695,12 +3737,12 @@ so that subsequent calls to \verb|table.getn(table)| return \verb|n|.
3695\subsection{Mathematical Functions} \label{mathlib} 3737\subsection{Mathematical Functions} \label{mathlib}
3696 3738
3697This library is an interface to most of the functions of the 3739This library is an interface to most of the functions of the
3698standard C~math library. 3740standard C\Nb{}math library.
3699(Some have slightly different names.) 3741(Some have slightly different names.)
3700It provides all its functions inside the table \IndexLIB{math}. 3742It provides all its functions inside the table \IndexLIB{math}.
3701In addition, 3743In addition,
3702it registers a ??tag method for the binary exponentiation operator \verb|^| 3744it registers a ??tag method for the binary exponentiation operator \verb|^|
3703that returns \Math{x^y} when applied to numbers \verb|x| and \verb|y|. 3745that returns \Math{x\sp{y}} when applied to numbers \verb|x| and \verb|y|.
3704 3746
3705The library provides the following functions: 3747The library provides the following functions:
3706\DefLIB{math.abs}\DefLIB{math.acos}\DefLIB{math.asin}\DefLIB{math.atan} 3748\DefLIB{math.abs}\DefLIB{math.acos}\DefLIB{math.asin}\DefLIB{math.atan}
@@ -3721,7 +3763,7 @@ The library provides the following functions:
3721\end{verbatim} 3763\end{verbatim}
3722plus a variable \IndexLIB{math.pi}. 3764plus a variable \IndexLIB{math.pi}.
3723Most of them 3765Most of them
3724are only interfaces to the corresponding functions in the C~library. 3766are only interfaces to the corresponding functions in the C\Nb{}library.
3725All trigonometric functions work in radians. 3767All trigonometric functions work in radians.
3726The functions \verb|math.deg| and \verb|math.rad| convert 3768The functions \verb|math.deg| and \verb|math.rad| convert
3727between radians and degrees. 3769between radians and degrees.
@@ -3733,7 +3775,7 @@ Both can be used with 1, 2, or more arguments.
3733 3775
3734The functions \verb|math.random| and \verb|math.randomseed| 3776The functions \verb|math.random| and \verb|math.randomseed|
3735are interfaces to the simple random generator functions 3777are interfaces to the simple random generator functions
3736\verb|rand| and \verb|srand| that are provided by ANSI~C. 3778\verb|rand| and \verb|srand| that are provided by ANSI\Nb{}C.
3737(No guarantees can be given for their statistical properties.) 3779(No guarantees can be given for their statistical properties.)
3738When called without arguments, 3780When called without arguments,
3739\verb|math.random| returns a pseudo-random real number 3781\verb|math.random| returns a pseudo-random real number
@@ -3833,7 +3875,7 @@ The \verb|mode| string can be any of the following:
3833\end{description} 3875\end{description}
3834The \verb|mode| string may also have a \verb|b| at the end, 3876The \verb|mode| string may also have a \verb|b| at the end,
3835which is needed in some systems to open the file in binary mode. 3877which is needed in some systems to open the file in binary mode.
3836This string is exactly what is used in the standard~C function \verb|fopen|. 3878This string is exactly what is used in the standard\Nb{}C function \verb|fopen|.
3837 3879
3838\subsubsection*{\ff \T{io.output ([file])}}\DefLIB{io.output} 3880\subsubsection*{\ff \T{io.output ([file])}}\DefLIB{io.output}
3839 3881
@@ -3916,7 +3958,7 @@ specified by the string \verb|whence|, as follows:
3916\end{description} 3958\end{description}
3917In case of success, function \verb|seek| returns the final file position, 3959In case of success, function \verb|seek| returns the final file position,
3918measured in bytes from the beginning of the file. 3960measured in bytes from the beginning of the file.
3919If this function fails, it returns \nil, 3961If this function fails, it returns \nil{},
3920plus a string describing the error. 3962plus a string describing the error.
3921 3963
3922The default value for \verb|whence| is \verb|"cur"|, 3964The default value for \verb|whence| is \verb|"cur"|,
@@ -3935,7 +3977,7 @@ the filehandle \verb|file|.
3935The arguments must be strings or numbers. 3977The arguments must be strings or numbers.
3936To write other values, 3978To write other values,
3937use \verb|tostring| or \verb|format| before \verb|write|. 3979use \verb|tostring| or \verb|format| before \verb|write|.
3938If this function fails, it returns \nil, 3980If this function fails, it returns \nil{},
3939plus a string describing the error. 3981plus a string describing the error.
3940 3982
3941 3983
@@ -3972,7 +4014,7 @@ and \verb|isdst| (daylight saving flag, a boolean).
3972 4014
3973If format is not \verb|*t|, 4015If format is not \verb|*t|,
3974then \verb|date| returns the date as a string, 4016then \verb|date| returns the date as a string,
3975formatted according with the same rules as the C~function \verb|strftime|. 4017formatted according with the same rules as the C\Nb{}function \verb|strftime|.
3976When called without arguments, 4018When called without arguments,
3977\verb|date| returns a reasonable date and time representation that depends on 4019\verb|date| returns a reasonable date and time representation that depends on
3978the host system and on the current locale 4020the host system and on the current locale
@@ -3986,13 +4028,13 @@ this value is exactly \verb|t2|\Math{-}\verb|t1|.
3986 4028
3987\subsubsection*{\ff \T{os.execute (command)}}\DefLIB{os.execute} 4029\subsubsection*{\ff \T{os.execute (command)}}\DefLIB{os.execute}
3988 4030
3989This function is equivalent to the C~function \verb|system|. 4031This function is equivalent to the C\Nb{}function \verb|system|.
3990It passes \verb|command| to be executed by an operating system shell. 4032It passes \verb|command| to be executed by an operating system shell.
3991It returns a status code, which is system-dependent. 4033It returns a status code, which is system-dependent.
3992 4034
3993\subsubsection*{\ff \T{os.exit ([code])}}\DefLIB{os.exit} 4035\subsubsection*{\ff \T{os.exit ([code])}}\DefLIB{os.exit}
3994 4036
3995Calls the C~function \verb|exit|, 4037Calls the C\Nb{}function \verb|exit|,
3996with an optional \verb|code|, 4038with an optional \verb|code|,
3997to terminate the host program. 4039to terminate the host program.
3998The default value for \verb|code| is the success code. 4040The default value for \verb|code| is the success code.
@@ -4005,18 +4047,18 @@ or \nil{} if the variable is not defined.
4005\subsubsection*{\ff \T{os.remove (filename)}}\DefLIB{os.remove} 4047\subsubsection*{\ff \T{os.remove (filename)}}\DefLIB{os.remove}
4006 4048
4007Deletes the file with the given name. 4049Deletes the file with the given name.
4008If this function fails, it returns \nil, 4050If this function fails, it returns \nil{},
4009plus a string describing the error. 4051plus a string describing the error.
4010 4052
4011\subsubsection*{\ff \T{os.rename (name1, name2)}}\DefLIB{os.rename} 4053\subsubsection*{\ff \T{os.rename (name1, name2)}}\DefLIB{os.rename}
4012 4054
4013Renames file named \verb|name1| to \verb|name2|. 4055Renames file named \verb|name1| to \verb|name2|.
4014If this function fails, it returns \nil, 4056If this function fails, it returns \nil{},
4015plus a string describing the error. 4057plus a string describing the error.
4016 4058
4017\subsubsection*{\ff \T{os.setlocale (locale [, category])}}\DefLIB{os.setlocale} 4059\subsubsection*{\ff \T{os.setlocale (locale [, category])}}\DefLIB{os.setlocale}
4018 4060
4019This function is an interface to the C~function \verb|setlocale|. 4061This function is an interface to the C\Nb{}function \verb|setlocale|.
4020\verb|locale| is a string specifying a locale; 4062\verb|locale| is a string specifying a locale;
4021\verb|category| is an optional string describing which category to change: 4063\verb|category| is an optional string describing which category to change:
4022\verb|"all"|, \verb|"collate"|, \verb|"ctype"|, 4064\verb|"all"|, \verb|"collate"|, \verb|"ctype"|,
@@ -4047,7 +4089,7 @@ be used for a temporary file.
4047The file must be explicitly opened before its use 4089The file must be explicitly opened before its use
4048and removed when no longer needed. 4090and removed when no longer needed.
4049 4091
4050This function is equivalent to the \verb|tmpnam| C~function, 4092This function is equivalent to the \verb|tmpnam| C\Nb{}function,
4051and many people (and even some compilers!) advise against its use, 4093and many people (and even some compilers!) advise against its use,
4052because between the time you call this function 4094because between the time you call this function
4053and the time you open the file, 4095and the time you open the file,
@@ -4083,7 +4125,7 @@ Level 0 is the current function (\verb|getinfo| itself);
4083level 1 is the function that called \verb|getinfo|; 4125level 1 is the function that called \verb|getinfo|;
4084and so on. 4126and so on.
4085If \verb|function| is a number larger than the number of active functions, 4127If \verb|function| is a number larger than the number of active functions,
4086then \verb|getinfo| returns \nil. 4128then \verb|getinfo| returns \nil{}.
4087 4129
4088The returned table contains all the fields returned by \verb|lua_getinfo|, 4130The returned table contains all the fields returned by \verb|lua_getinfo|,
4089with the string \verb|what| describing which fields to fill in. 4131with the string \verb|what| describing which fields to fill in.
@@ -4102,7 +4144,7 @@ about the \verb|print| function.
4102 4144
4103This function returns the name and the value of the local variable 4145This function returns the name and the value of the local variable
4104with index \verb|local| of the function at level \verb|level| of the stack. 4146with index \verb|local| of the function at level \verb|level| of the stack.
4105(The first parameter or local variable has index~1, and so on, 4147(The first parameter or local variable has index\Nb{}1, and so on,
4106until the last active local variable.) 4148until the last active local variable.)
4107The function returns \nil{} if there is no local 4149The function returns \nil{} if there is no local
4108variable with the given index, 4150variable with the given index,
@@ -4146,8 +4188,8 @@ it also gets as its second parameter the new line number.
4146Inside a hook, 4188Inside a hook,
4147you can call \verb|getinfo| with level 2 to get more information about 4189you can call \verb|getinfo| with level 2 to get more information about
4148the running function 4190the running function
4149(level~0 is the \verb|getinfo| function, 4191(level\Nb{}0 is the \verb|getinfo| function,
4150and level~1 is the hook function). 4192and level\Nb{}1 is the hook function).
4151 4193
4152\subsubsection*{\ff \T{debug.gethook ()}}\DefLIB{debug.gethook} 4194\subsubsection*{\ff \T{debug.gethook ()}}\DefLIB{debug.gethook}
4153 4195
@@ -4156,11 +4198,11 @@ the current hook function, the current hook mask,
4156and the current hook count (as set by the \verb|debug.sethook| function). 4198and the current hook count (as set by the \verb|debug.sethook| function).
4157 4199
4158 4200
4159\C{------------------------------------------------------------------------------} 4201\C{-------------------------------------------------------------------------}
4160\section{\Index{Lua Stand-alone}} \label{lua-sa} 4202\section{\Index{Lua Stand-alone}} \label{lua-sa}
4161 4203
4162Although Lua has been designed as an extension language, 4204Although Lua has been designed as an extension language,
4163to be embedded in a host C~program, 4205to be embedded in a host C\Nb{}program,
4164it is also frequently used as a stand-alone language. 4206it is also frequently used as a stand-alone language.
4165An interpreter for Lua as a stand-alone language, 4207An interpreter for Lua as a stand-alone language,
4166called simply \verb|lua|, 4208called simply \verb|lua|,
@@ -4188,7 +4230,7 @@ and as \verb|lua -| otherwise.
4188 4230
4189Before running any argument, 4231Before running any argument,
4190the interpreter checks for an environment variable \IndexVerb{LUA_INIT}. 4232the interpreter checks for an environment variable \IndexVerb{LUA_INIT}.
4191If its format is \verb|@|\emph{filename}, 4233If its format is \At{}\emph{filename},
4192then lua executes the file. 4234then lua executes the file.
4193Otherwise, lua executes the string itself. 4235Otherwise, lua executes the string itself.
4194 4236
@@ -4240,7 +4282,7 @@ Note the use of \verb|-i| to enter interactive mode; otherwise,
4240the program would end just after the assignment to \verb|_PROMPT|. 4282the program would end just after the assignment to \verb|_PROMPT|.
4241 4283
4242In Unix systems, Lua scripts can be made into executable programs 4284In Unix systems, Lua scripts can be made into executable programs
4243by using \verb|chmod +x| and the~\verb|#!| form, 4285by using \verb|chmod +x| and the\Nb{}\verb|#!| form,
4244as in 4286as in
4245\begin{verbatim} 4287\begin{verbatim}
4246#!/usr/local/bin/lua 4288#!/usr/local/bin/lua
@@ -4254,7 +4296,7 @@ then
4254\end{verbatim} 4296\end{verbatim}
4255is a more portable solution.) 4297is a more portable solution.)
4256 4298
4257\C{------------------------------------------------------------------------------} 4299\C{-------------------------------------------------------------------------}
4258\section*{Acknowledgments} 4300\section*{Acknowledgments}
4259 4301
4260The Lua team is grateful to \tecgraf{} for its continued support to Lua. 4302The Lua team is grateful to \tecgraf{} for its continued support to Lua.
@@ -4264,8 +4306,8 @@ At the risk of omitting several names,
4264we also thank the following individuals for supporting, 4306we also thank the following individuals for supporting,
4265contributing to, and spreading the word about Lua: 4307contributing to, and spreading the word about Lua:
4266Alan Watson. 4308Alan Watson.
4267Andr\'e Clinio, 4309André Clinio,
4268Andr\'e Costa, 4310André Costa,
4269Antonio Scuri, 4311Antonio Scuri,
4270Bret Mogilefsky, 4312Bret Mogilefsky,
4271Cameron Laird, 4313Cameron Laird,
@@ -4293,7 +4335,7 @@ Reuben Thomas,
4293Stephan Herrmann, 4335Stephan Herrmann,
4294Steve Dekorte, 4336Steve Dekorte,
4295Thatcher Ulrich, 4337Thatcher Ulrich,
4296Tom\'as Gorham, 4338Tomás Gorham,
4297Vincent Penquerc'h, 4339Vincent Penquerc'h,
4298Thank you! 4340Thank you!
4299 4341
@@ -4383,20 +4425,14 @@ Userdata!!
4383 4425
4384The notation used here is the usual extended BNF, 4426The notation used here is the usual extended BNF,
4385in which 4427in which
4386\rep{\emph{a}}~means 0 or more \emph{a}'s, and 4428\rep{\emph{a}}\Nb{}means 0 or more \emph{a}'s, and
4387\opt{\emph{a}}~means an optional \emph{a}. 4429\opt{\emph{a}}\Nb{}means an optional \emph{a}.
4388Non-terminals are shown in \emph{italics}, 4430Non-terminals are shown in \emph{italics},
4389keywords are shown in {\bf bold}, 4431keywords are shown in {\bf bold},
4390and other terminal symbols are shown in {\tt typewriter} font, 4432and other terminal symbols are shown in {\tt typewriter} font,
4391enclosed in single quotes. 4433enclosed in single quotes.
4392 4434
4393 4435
4394\renewenvironment{Produc}{\vspace{0.8ex}\par\noindent\hspace{3ex}\it\begin{tabular}{rrl}}{\end{tabular}\vspace{0.8ex}\par\noindent}
4395
4396\renewcommand{\OrNL}{\\ & \Or & }
4397\C{\newcommand{\Nter}[1]{{\rm{\tt#1}}}}
4398\C{\newcommand{\Nter}[1]{\ter{#1}}}
4399
4400\index{grammar} 4436\index{grammar}
4401 4437
4402\begin{Produc} 4438\begin{Produc}
@@ -4482,9 +4518,9 @@ enclosed in single quotes.
4482\produc{field}{\ter{[} exp \ter{]} \ter{=} exp \Or name \ter{=} exp \Or exp} 4518\produc{field}{\ter{[} exp \ter{]} \ter{=} exp \Or name \ter{=} exp \Or exp}
4483\produc{fieldsep}{\ter{,} \Or \ter{;}} 4519\produc{fieldsep}{\ter{,} \Or \ter{;}}
4484 4520
4485\produc{binop}{\ter{+} \Or \ter{-} \Or \ter{*} \Or \ter{/} \Or \ter{\^{ }} \Or 4521\produc{binop}{\ter{+} \Or \ter{-} \Or \ter{*} \Or \ter{/} \Or \ter{^} \Or
4486 \ter{..} \Or \ter{<} \Or \ter{<=} \Or \ter{>} \Or \ter{>=} 4522 \ter{..} \OrNL \ter{<} \Or \ter{<=} \Or \ter{>} \Or \ter{>=}
4487 \Or \ter{==} \Or \ter{\~{ }=} \OrNL \rwd{and} \Or \rwd{or}} 4523 \Or \ter{==} \Or \ter{~=} \OrNL \rwd{and} \Or \rwd{or}}
4488 4524
4489\produc{unop}{\ter{-} \Or \rwd{not}} 4525\produc{unop}{\ter{-} \Or \rwd{not}}
4490 4526