diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-01-24 07:14:54 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-01-24 07:14:54 -0200 |
commit | 3088ebd95b7c509ff1a049ef4a10a1c016883490 (patch) | |
tree | 372176a783a834d79268c979c441c426ab105a3c | |
parent | 797d964df9c0b1bf09338cea3d578f1985fe2ee0 (diff) | |
download | lua-3088ebd95b7c509ff1a049ef4a10a1c016883490.tar.gz lua-3088ebd95b7c509ff1a049ef4a10a1c016883490.tar.bz2 lua-3088ebd95b7c509ff1a049ef4a10a1c016883490.zip |
last changes before leaving Latex
-rw-r--r-- | manual.tex | 446 |
1 files changed, 241 insertions, 205 deletions
@@ -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, | |||
166 | and | 198 | and |
167 | rapid prototyping. | 199 | rapid prototyping. |
168 | 200 | ||
169 | This document describes version \Version\ of the Lua programming language | 201 | This document describes version \Version{} of the Lua programming language |
170 | and the Application Program Interface (API) | 202 | and the Application Program Interface (API) |
171 | that allows interaction between Lua programs and their host C~programs. | 203 | that 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 |
177 | Lua \'e uma linguagem de programa\c{c}\~ao | 209 | Lua é uma linguagem de programação |
178 | poderosa e leve, | 210 | poderosa e leve, |
179 | projetada para estender aplica\c{c}\~oes. | 211 | projetada para estender aplicações. |
180 | Lua tamb\'em \'e frequentemente usada como uma linguagem de prop\'osito geral. | 212 | Lua também é frequentemente usada como uma linguagem de propósito geral. |
181 | Lua combina programa\c{c}\~ao procedural | 213 | Lua combina programação procedural |
182 | (com sintaxe semelhante \`a de Pascal) | 214 | (com sintaxe semelhante à de Pascal) |
183 | com | 215 | com |
184 | poderosas constru\c{c}\~oes para descri\c{c}\~ao de dados, | 216 | poderosas construções para descrição de dados, |
185 | baseadas em tabelas associativas e sem\^antica extens\'\i vel. | 217 | baseadas em tabelas associativas e semântica extensível. |
186 | Lua \'e | 218 | Lua é |
187 | tipada dinamicamente, | 219 | tipada dinamicamente, |
188 | interpretada a partir de \emph{opcodes}, | 220 | interpretada a partir de \emph{opcodes}, |
189 | e tem gerenciamento autom\'atico de mem\'oria com coleta de lixo. | 221 | e tem gerenciamento automático de memória com coleta de lixo. |
190 | Essas caracter\'{\i}sticas fazem de Lua uma linguagem ideal para | 222 | Essas características fazem de Lua uma linguagem ideal para |
191 | configura\c{c}\~ao, | 223 | configuração, |
192 | automa\c{c}\~ao (\emph{scripting}) | 224 | automação (\emph{scripting}) |
193 | e prototipagem r\'apida. | 225 | e prototipagem rápida. |
194 | 226 | ||
195 | Este documento descreve a vers\~ao \Version\ da linguagem de | 227 | Este documento descreve a versão \Version{} da linguagem de |
196 | programa\c{c}\~ao Lua e a Interface de Programa\c{c}\~ao (API) que permite | 228 | programação Lua e a Interface de Programação (API) que permite |
197 | a intera\c{c}\~ao entre programas Lua e programas C~hospedeiros. | 229 | a 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 | ||
214 | Lua is an extension programming language designed to support | 255 | Lua is an extension programming language designed to support |
@@ -223,8 +264,8 @@ it only works \emph{embedded} in a host client, | |||
223 | called the \emph{embedding program} or simply the \emph{host}. | 264 | called the \emph{embedding program} or simply the \emph{host}. |
224 | This host program can invoke functions to execute a piece of Lua code, | 265 | This host program can invoke functions to execute a piece of Lua code, |
225 | can write and read Lua variables, | 266 | can write and read Lua variables, |
226 | and can register C~functions to be called by Lua code. | 267 | and can register C\Nb{}functions to be called by Lua code. |
227 | Through the use of C~functions, Lua can be augmented to cope with | 268 | Through the use of C\Nb{}functions, Lua can be augmented to cope with |
228 | a wide range of different domains, | 269 | a wide range of different domains, |
229 | thus creating customized programming languages sharing a syntactical framework. | 270 | thus creating customized programming languages sharing a syntactical framework. |
230 | 271 | ||
@@ -241,32 +282,32 @@ see the papers below, | |||
241 | which are available at Lua's web site. | 282 | which are available at Lua's web site. |
242 | \begin{itemize} | 283 | \begin{itemize} |
243 | \item | 284 | \item |
244 | R.~Ierusalimschy, L.~H.~de Figueiredo, and W.~Celes. | 285 | R.\Nb{}Ierusalimschy, L.\Nb{}H.\Nb{}de Figueiredo, and W.\Nb{}Celes. |
245 | Lua\Em{}an extensible extension language. | 286 | Lua\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 |
248 | L.~H.~de Figueiredo, R.~Ierusalimschy, and W.~Celes. | 289 | L.\Nb{}H.\Nb{}de Figueiredo, R.\Nb{}Ierusalimschy, and W.\Nb{}Celes. |
249 | The design and implementation of a language for extending applications. | 290 | The 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 |
252 | L.~H.~de Figueiredo, R.~Ierusalimschy, and W.~Celes. | 293 | L.\Nb{}H.\Nb{}de Figueiredo, R.\Nb{}Ierusalimschy, and W.\Nb{}Celes. |
253 | Lua: an extensible embedded language. | 294 | Lua: 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 |
256 | R.~Ierusalimschy, L.~H.~de Figueiredo, and W.~Celes. | 297 | R.\Nb{}Ierusalimschy, L.\Nb{}H.\Nb{}de Figueiredo, and W.\Nb{}Celes. |
257 | The evolution of an extension language: a history of Lua, | 298 | The 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 | ||
261 | Lua means \Q{moon} in Portuguese. | 302 | Lua 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 | ||
266 | This section describes the main concepts of Lua as a language. | 307 | This section describes the main concepts of Lua as a language. |
267 | The syntax and semantics of Lua are described in \See{language}. | 308 | The syntax and semantics of Lua are described in \See{language}. |
268 | The discussion below is not purely conceptual; | 309 | The discussion below is not purely conceptual; |
269 | it includes references to the C~API \see{API}, | 310 | it includes references to the C\Nb{}API \see{API}, |
270 | because Lua is designed to be embedded in host programs. | 311 | because Lua is designed to be embedded in host programs. |
271 | It also includes references to the standard libraries \see{libraries}. | 312 | It also includes references to the standard libraries \see{libraries}. |
272 | 313 | ||
@@ -315,10 +356,10 @@ All values carry their own type. | |||
315 | There are eight \Index{basic types} in Lua: | 356 | There 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{}, |
319 | whose main property is to be different from any other value; | 360 | whose main property is to be different from any other value; |
320 | usually it represents the absence of a useful value. | 361 | usually 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{}. |
322 | In Lua, both \nil{} and \False{} make a condition false, | 363 | In Lua, both \nil{} and \False{} make a condition false, |
323 | and any other value makes it true. | 364 | and 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}, | |||
347 | the programmer can define operations for userdata values | 388 | the programmer can define operations for userdata values |
348 | \see{metatable}. | 389 | \see{metatable}. |
349 | Userdata values cannot be created or modified in Lua, | 390 | Userdata values cannot be created or modified in Lua, |
350 | only through the C~API. | 391 | only through the C\Nb{}API. |
351 | This guarantees the integrity of data owned by the host program. | 392 | This guarantees the integrity of data owned by the host program. |
352 | 393 | ||
353 | The type \Def{thread} represents independent threads of execution, | 394 | The type \Def{thread} represents independent threads of execution, |
@@ -357,10 +398,10 @@ and is subject to changes in the future.) | |||
357 | 398 | ||
358 | The type \emph{table} implements \Index{associative arrays}, | 399 | The type \emph{table} implements \Index{associative arrays}, |
359 | that is, \Index{arrays} that can be indexed not only with numbers, | 400 | that is, \Index{arrays} that can be indexed not only with numbers, |
360 | but with any value (except \nil). | 401 | but with any value (except \nil{}). |
361 | Moreover, | 402 | Moreover, |
362 | tables can be \emph{heterogeneous}, | 403 | tables can be \emph{heterogeneous}, |
363 | that is, they can contain values of all types (except \nil). | 404 | that is, they can contain values of all types (except \nil{}). |
364 | Tables are the sole data structuring mechanism in Lua; | 405 | Tables are the sole data structuring mechanism in Lua; |
365 | they may be used to represent not only ordinary arrays, | 406 | they may be used to represent not only ordinary arrays, |
366 | but also symbol tables, sets, records, graphs, trees, etc. | 407 | but also symbol tables, sets, records, graphs, trees, etc. |
@@ -438,7 +479,7 @@ global variables | |||
438 | and local variables. | 479 | and local variables. |
439 | Variables are assumed to be global unless explicitly declared local | 480 | Variables are assumed to be global unless explicitly declared local |
440 | \see{localvar}. | 481 | \see{localvar}. |
441 | Before the first assignment to a variable, its value is \nil. | 482 | Before the first assignment to a variable, its value is \nil{}. |
442 | 483 | ||
443 | All global variables live as fields in ordinary Lua tables. | 484 | All global variables live as fields in ordinary Lua tables. |
444 | Usually, globals live in a table called \Index{table of globals}. | 485 | Usually, globals live in a table called \Index{table of globals}. |
@@ -466,7 +507,7 @@ collecting all dead objects | |||
466 | All objects in Lua are subject to automatic management: | 507 | All objects in Lua are subject to automatic management: |
467 | tables, userdata, functions, and strings. | 508 | tables, userdata, functions, and strings. |
468 | 509 | ||
469 | Using the C~API, | 510 | Using the C\Nb{}API, |
470 | you can set garbage-collector metamethods for userdata \see{metatable}. | 511 | you can set garbage-collector metamethods for userdata \see{metatable}. |
471 | When it is about to free a userdata, | 512 | When it is about to free a userdata, |
472 | Lua calls the metamethod associated with event \verb|gc| in the | 513 | Lua calls the metamethod associated with event \verb|gc| in the |
@@ -485,7 +526,7 @@ which reclaims the memory of all dead objects. | |||
485 | The byte counter is adjusted, | 526 | The byte counter is adjusted, |
486 | and then the threshold is reset to twice the new value of the byte counter. | 527 | and then the threshold is reset to twice the new value of the byte counter. |
487 | 528 | ||
488 | Through the C~API, you can query those numbers, | 529 | Through the C\Nb{}API, you can query those numbers, |
489 | and change the threshold \see{GC-API}. | 530 | and change the threshold \see{GC-API}. |
490 | Setting the threshold to zero actually forces an immediate | 531 | Setting the threshold to zero actually forces an immediate |
491 | garbage-collection cycle, | 532 | garbage-collection cycle, |
@@ -519,7 +560,7 @@ If \verb|__mode| contains \verb|v|, | |||
519 | the values in the table are weak. | 560 | the 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 | ||
525 | This section describes the lexis, the syntax, and the semantics of Lua. | 566 | This 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 | ||
553 | Lua is a case-sensitive language: | 594 | Lua 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. | 596 | are two different, valid identifiers. |
556 | As a convention, identifiers starting with an underscore followed by | 597 | As a convention, identifiers starting with an underscore followed by |
557 | uppercase letters (such as \verb|_VERSION|) | 598 | uppercase letters (such as \verb|_VERSION|) |
558 | are reserved for internal variables used by Lua. | 599 | are reserved for internal variables used by Lua. |
@@ -588,9 +629,10 @@ where \emph{ddd} is a sequence of up to three \emph{decimal} digits. | |||
588 | Strings in Lua may contain any 8-bit value, including embedded zeros, | 629 | Strings in Lua may contain any 8-bit value, including embedded zeros, |
589 | which can be specified as `\verb|\0|'. | 630 | which can be specified as `\verb|\0|'. |
590 | 631 | ||
591 | Literal strings can also be delimited by matching \verb|[[| $\ldots$ \verb|]]|. | 632 | Literal strings can also be delimited by matching |
633 | \verb|[[| \Math{\ldots} \verb|]]|. | ||
592 | Literals in this bracketed form may run for several lines, | 634 | Literals in this bracketed form may run for several lines, |
593 | may contain nested \verb|[[| $\ldots$ \verb|]]| pairs, | 635 | may contain nested \verb|[[| \Math{\ldots} \verb|]]| pairs, |
594 | and do not interpret escape sequences. | 636 | and do not interpret escape sequences. |
595 | For convenience, | 637 | For convenience, |
596 | when the opening \verb|[[| is immediately followed by a newline, | 638 | when the opening \verb|[[| is immediately followed by a newline, |
@@ -599,8 +641,8 @@ That form is specially convenient for | |||
599 | writing strings that contain program pieces or | 641 | writing strings that contain program pieces or |
600 | other quoted strings. | 642 | other quoted strings. |
601 | As an example, in a system using ASCII | 643 | As 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, |
603 | newline is coded as~10, and `\verb|1|' is coded as~49), | 645 | newline is coded as\Nb{}10, and `\verb|1|' is coded as\Nb{}49), |
604 | the four literals below denote the same string: | 646 | the 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. | |||
627 | Otherwise, it is a long comment, | 669 | Otherwise, it is a long comment, |
628 | that runs until the corresponding \verb|]]|. | 670 | that runs until the corresponding \verb|]]|. |
629 | Long comments may run for several lines, | 671 | Long comments may run for several lines, |
630 | and may contain nested \verb|[[| $\ldots$ \verb|]]| pairs. | 672 | and may contain nested \verb|[[| \Math{\ldots} \verb|]]| pairs. |
631 | For convenience, | 673 | For convenience, |
632 | the first line of a chunk is skipped if it starts with \verb|#|. | 674 | the first line of a chunk is skipped if it starts with \verb|#|. |
633 | This facility allows the use of Lua as a script interpreter | 675 | This facility allows the use of Lua as a script interpreter |
@@ -736,7 +778,7 @@ the list of variables.\index{adjustment} | |||
736 | If there are more values than needed, | 778 | If there are more values than needed, |
737 | the excess values are thrown away. | 779 | the excess values are thrown away. |
738 | If there are fewer values than needed, | 780 | If there are fewer values than needed, |
739 | the list is extended with as many \nil's as needed. | 781 | the list is extended with as many \nil{}'s as needed. |
740 | If the list of expressions ends with a function call, | 782 | If the list of expressions ends with a function call, |
741 | then all values returned by that function call enter in the list of values, | 783 | then all values returned by that function call enter in the list of values, |
742 | before the adjustment | 784 | before the adjustment |
@@ -854,7 +896,7 @@ before the loop starts. | |||
854 | The names are here for explanatory purposes only. | 896 | The 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 |
856 | the block. | 898 | the 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; |
860 | you cannot use its value after the \rwd{for} ends or is broken. | 902 | you 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. | |||
865 | The generic \rwd{for} statement works over functions, | 907 | The generic \rwd{for} statement works over functions, |
866 | called \Index{generators}. | 908 | called \Index{generators}. |
867 | It calls its generator to produce a new value for each iteration, | 909 | It calls its generator to produce a new value for each iteration, |
868 | stopping when the new value is \nil. | 910 | stopping when the new value is \nil{}. |
869 | It has the following syntax: | 911 | It 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} |
922 | If present, an initial assignment has the same semantics | 964 | If present, an initial assignment has the same semantics |
923 | of a multiple assignment \see{assignment}. | 965 | of a multiple assignment \see{assignment}. |
924 | Otherwise, all variables are initialized with \nil. | 966 | Otherwise, all variables are initialized with \nil{}. |
925 | 967 | ||
926 | A chunk is also a block \see{chunks}, | 968 | A chunk is also a block \see{chunks}, |
927 | so local variables can be declared outside any explicit block. | 969 | so 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} |
984 | These operators always result in \False{} or \True. | 1026 | These operators always result in \False{} or \True{}. |
985 | 1027 | ||
986 | Equality (\verb|==|) first compares the type of its operands. | 1028 | Equality (\verb|==|) first compares the type of its operands. |
987 | If the types are different, then the result is \False. | 1029 | If the types are different, then the result is \False{}. |
988 | Otherwise, the values of the operands are compared. | 1030 | Otherwise, the values of the operands are compared. |
989 | Numbers and strings are compared in the usual way. | 1031 | Numbers and strings are compared in the usual way. |
990 | Tables, userdata, and functions are compared \emph{by reference}, | 1032 | Tables, userdata, and functions are compared \emph{by reference}, |
@@ -1024,13 +1066,13 @@ all logical operators consider both \False{} and \nil{} as false | |||
1024 | and anything else as true. | 1066 | and anything else as true. |
1025 | \IndexKW{and}\IndexKW{or}\IndexKW{not} | 1067 | \IndexKW{and}\IndexKW{or}\IndexKW{not} |
1026 | 1068 | ||
1027 | The operator \rwd{not} always return \False{} or \True. | 1069 | The operator \rwd{not} always return \False{} or \True{}. |
1028 | 1070 | ||
1029 | The conjunction operator \rwd{and} returns its first argument | 1071 | The conjunction operator \rwd{and} returns its first argument |
1030 | if its value is \False{} or \nil; | 1072 | if its value is \False{} or \nil{}; |
1031 | otherwise, \rwd{and} returns its second argument. | 1073 | otherwise, \rwd{and} returns its second argument. |
1032 | The disjunction operator \rwd{or} returns its first argument | 1074 | The disjunction operator \rwd{or} returns its first argument |
1033 | if it is different from \nil{} and \False; | 1075 | if it is different from \nil{} and \False{}; |
1034 | otherwise, \rwd{or} returns its second argument. | 1076 | otherwise, \rwd{or} returns its second argument. |
1035 | Both \rwd{and} and \rwd{or} use \Index{short-cut evaluation}, | 1077 | Both \rwd{and} and \rwd{or} use \Index{short-cut evaluation}, |
1036 | that is, | 1078 | that is, |
@@ -1164,11 +1206,11 @@ Because a function can return any number of results | |||
1164 | \see{return}, | 1206 | \see{return}, |
1165 | the number of results must be adjusted before they are used. | 1207 | the number of results must be adjusted before they are used. |
1166 | If the function is called as a statement \see{funcstat}, | 1208 | If the function is called as a statement \see{funcstat}, |
1167 | then its return list is adjusted to~0 elements, | 1209 | then its return list is adjusted to\Nb{}0 elements, |
1168 | thus discarding all returned values. | 1210 | thus discarding all returned values. |
1169 | If the function is called inside another expression, | 1211 | If the function is called inside another expression, |
1170 | or in the middle of a list of expressions, | 1212 | or in the middle of a list of expressions, |
1171 | then its return list is adjusted to~1 element, | 1213 | then its return list is adjusted to\Nb{}1 element, |
1172 | thus discarding all returned values but the first one. | 1214 | thus discarding all returned values but the first one. |
1173 | If the function is called as the last element of a list of expressions, | 1215 | If the function is called as the last element of a list of expressions, |
1174 | then no adjustment is made | 1216 | then no adjustment is made |
@@ -1277,8 +1319,8 @@ A vararg function does not adjust its argument list; | |||
1277 | instead, it collects all extra arguments into an implicit parameter, | 1319 | instead, it collects all extra arguments into an implicit parameter, |
1278 | called \IndexLIB{arg}. | 1320 | called \IndexLIB{arg}. |
1279 | The value of \verb|arg| is a table, | 1321 | The value of \verb|arg| is a table, |
1280 | with a field~\verb|n| whose value is the number of extra arguments, | 1322 | with a field\Nb{}\verb|n| whose value is the number of extra arguments, |
1281 | and with the extra arguments at positions 1,~2,~\ldots,~\verb|n|. | 1323 | and with the extra arguments at positions 1,\Nb{}2,\Nb{}\ldots,\Nb{}\verb|n|. |
1282 | 1324 | ||
1283 | As an example, consider the following definitions: | 1325 | As 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 | ||
1379 | Because Lua is an extension language, | 1421 | Because Lua is an extension language, |
1380 | all Lua actions start from C~code in the host program | 1422 | all Lua actions start from C\Nb{}code in the host program |
1381 | calling a function from the Lua library \see{pcall}. | 1423 | calling a function from the Lua library \see{pcall}. |
1382 | Whenever an error occurs during Lua compilation or execution, | 1424 | Whenever an error occurs during Lua compilation or execution, |
1383 | control returns to C, | 1425 | control returns to C, |
@@ -1689,7 +1731,7 @@ A coroutine can terminate its execution in two ways: | |||
1689 | Normally, when its main function returns | 1731 | Normally, when its main function returns |
1690 | (explicitly or implicitly, after the last instruction); | 1732 | (explicitly or implicitly, after the last instruction); |
1691 | and abnormally, if there is an unprotected error. | 1733 | and abnormally, if there is an unprotected error. |
1692 | In the first case, \verb|coroutine.resume| returns \True, | 1734 | In the first case, \verb|coroutine.resume| returns \True{}, |
1693 | plus any values returned by the coroutine main function. | 1735 | plus any values returned by the coroutine main function. |
1694 | In case of errors, \verb|coroutine.resume| returns \False{} | 1736 | In case of errors, \verb|coroutine.resume| returns \False{} |
1695 | plus an error message. | 1737 | plus an error message. |
@@ -1700,7 +1742,7 @@ the corresponding \verb|coroutine.resume| returns immediately, | |||
1700 | even if the yield happens inside nested function calls | 1742 | even if the yield happens inside nested function calls |
1701 | (that is, not in the main function, | 1743 | (that is, not in the main function, |
1702 | but in a function directly or indirectly called by the main function). | 1744 | but in a function directly or indirectly called by the main function). |
1703 | In the case of a yield, \verb|coroutine.resume| also returns \True, | 1745 | In the case of a yield, \verb|coroutine.resume| also returns \True{}, |
1704 | plus any values passed to \verb|coroutine.yield|. | 1746 | plus any values passed to \verb|coroutine.yield|. |
1705 | The next time you resume the same coroutine, | 1747 | The next time you resume the same coroutine, |
1706 | it continues its execution from the point where it yielded, | 1748 | it 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 | ||
1766 | This section describes the API for Lua, that is, | 1808 | This section describes the API for Lua, that is, |
1767 | the set of C~functions available to the host program to communicate | 1809 | the set of C\Nb{}functions available to the host program to communicate |
1768 | with Lua. | 1810 | with Lua. |
1769 | All API functions and related types and constants | 1811 | All API functions and related types and constants |
1770 | are declared in the header file \verb|lua.h|. | 1812 | are 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 | ||
1818 | Lua offers partial support for multiple threads of execution. | 1860 | Lua offers partial support for multiple threads of execution. |
1819 | If you have a C~library that offers multi-threading, | 1861 | If you have a C\Nb{}library that offers multi-threading, |
1820 | then Lua can cooperate with it to implement the equivalent facility in Lua. | 1862 | then Lua can cooperate with it to implement the equivalent facility in Lua. |
1821 | Also, Lua implements its own coroutine system on top of threads. | 1863 | Also, Lua implements its own coroutine system on top of threads. |
1822 | The following function creates a new \Q{thread} in Lua: | 1864 | The following function creates a new \Q{thread} in Lua: |
@@ -1846,7 +1888,7 @@ Instead, you must close the state itself. | |||
1846 | 1888 | ||
1847 | Lua uses a virtual \emph{stack} to pass values to and from C. | 1889 | Lua uses a virtual \emph{stack} to pass values to and from C. |
1848 | Each element in this stack represents a Lua value | 1890 | Each element in this stack represents a Lua value |
1849 | (\nil, number, string, etc.). | 1891 | (\nil{}, number, string, etc.). |
1850 | 1892 | ||
1851 | Each C invocation has its own stack. | 1893 | Each C invocation has its own stack. |
1852 | Whenever Lua calls C, the called function gets a new stack, | 1894 | Whenever Lua calls C, the called function gets a new stack, |
@@ -1859,18 +1901,18 @@ For convenience, | |||
1859 | most query operations in the API do not follow a strict stack discipline. | 1901 | most query operations in the API do not follow a strict stack discipline. |
1860 | Instead, they can refer to any element in the stack by using an \emph{index}: | 1902 | Instead, they can refer to any element in the stack by using an \emph{index}: |
1861 | A positive index represents an \emph{absolute} stack position | 1903 | A positive index represents an \emph{absolute} stack position |
1862 | (starting at~1); | 1904 | (starting at\Nb{}1); |
1863 | a negative index represents an \emph{offset} from the top of the stack. | 1905 | a negative index represents an \emph{offset} from the top of the stack. |
1864 | More specifically, if the stack has \M{n} elements, | 1906 | More specifically, if the stack has \M{n} elements, |
1865 | then index~1 represents the first element | 1907 | then 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), |
1867 | and | 1909 | and |
1868 | index~\M{n} represents the last element; | 1910 | index\Nb{}\M{n} represents the last element; |
1869 | index~\Math{-1} also represents the last element | 1911 | index\Nb{}\Math{-1} also represents the last element |
1870 | (that is, the element at the top), | 1912 | (that is, the element at the top), |
1871 | and index \Math{-n} represents the first element. | 1913 | and index \Math{-n} represents the first element. |
1872 | We say that an index is \emph{valid} | 1914 | We say that an index is \emph{valid} |
1873 | if it lies between~1 and the stack top | 1915 | if 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} |
1882 | Because indices start at~1, | 1924 | Because indices start at\Nb{}1, |
1883 | the result of \verb|lua_gettop| is equal to the number of elements in the stack | 1925 | the 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 | ||
1886 | When you interact with Lua API, | 1928 | When 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. | |||
1918 | Unless otherwise noted, | 1960 | Unless otherwise noted, |
1919 | any function that accepts valid indices can also be called with | 1961 | any function that accepts valid indices can also be called with |
1920 | \Index{pseudo-indices}, | 1962 | \Index{pseudo-indices}, |
1921 | which represent some Lua values that are accessible to the C~code | 1963 | which represent some Lua values that are accessible to the C\Nb{}code |
1922 | but are not in the stack. | 1964 | but are not in the stack. |
1923 | Pseudo-indices are used to access the table of globals \see{globals}, | 1965 | Pseudo-indices are used to access the table of globals \see{globals}, |
1924 | the registry, and the upvalues of a C function \see{c-closure}. | 1966 | the 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: | |||
1939 | or 0, | 1981 | or 0, |
1940 | and sets the stack top to that index. | 1982 | and sets the stack top to that index. |
1941 | If the new top is larger than the old one, | 1983 | If the new top is larger than the old one, |
1942 | then the new elements are filled with \nil. | 1984 | then the new elements are filled with \nil{}. |
1943 | If \verb|index| is 0, then all stack elements are removed. | 1985 | If \verb|index| is 0, then all stack elements are removed. |
1944 | A useful macro defined in the \verb|lua.h| is | 1986 | A 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 | ||
2024 | The \verb|lua_is*| functions return~1 if the object is compatible | 2066 | The \verb|lua_is*| functions return\Nb{}1 if the object is compatible |
2025 | with the given type, and 0 otherwise. | 2067 | with 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, |
2027 | and it succeeds only for boolean values | 2069 | and it succeeds only for boolean values |
@@ -2030,9 +2072,9 @@ as any value has a boolean value). | |||
2030 | They always return 0 for a non-valid index. | 2072 | They 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, |
2034 | and \verb|lua_isuserdata| accepts both full and light userdata. | 2076 | and \verb|lua_isuserdata| accepts both full and light userdata. |
2035 | To distinguish between Lua functions and C~functions, | 2077 | To distinguish between Lua functions and C\Nb{}functions, |
2036 | you should use \verb|lua_iscfunction|. | 2078 | you should use \verb|lua_iscfunction|. |
2037 | To distinguish between full and light userdata, | 2079 | To distinguish between full and light userdata, |
2038 | you can use \verb|lua_islightuserdata|. | 2080 | you 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 | ||
2053 | To translate a value in the stack to a specific C~type, | 2095 | To translate a value in the stack to a specific C\Nb{}type, |
2054 | you can use the following conversion functions: | 2096 | you 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 |
2070 | to a C \Q{boolean} value (that is, 0 or 1). | 2112 | to a C \Q{boolean} value (that is, 0 or 1). |
2071 | Like all tests in Lua, it returns 1 for any Lua value different from | 2113 | Like all tests in Lua, it returns 1 for any Lua value different from |
2072 | \False{} and \nil; | 2114 | \False{} and \nil{}; |
2073 | otherwise it returns 0. | 2115 | otherwise it returns 0. |
2074 | It also returns 0 when called with a non-valid index. | 2116 | It 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.) | |||
2079 | to a number (by default, \verb|lua_Number| is \verb|double|). | 2121 | to a number (by default, \verb|lua_Number| is \verb|double|). |
2080 | \DefAPI{lua_Number} | 2122 | \DefAPI{lua_Number} |
2081 | The Lua value must be a number or a string convertible to number | 2123 | The 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 |
2094 | to a string inside the Lua environment. | 2136 | to a string inside the Lua environment. |
2095 | This string always has a zero (\verb|'\0'|) | 2137 | This string always has a zero (\verb|'\0'|) |
2096 | after its last character (as in~C), | 2138 | after its last character (as in\Nb{}C), |
2097 | but may contain other zeros in its body. | 2139 | but may contain other zeros in its body. |
2098 | If you do not know whether a string may contain zeros, | 2140 | If you do not know whether a string may contain zeros, |
2099 | you can use \verb|lua_strlen| to get its actual length. | 2141 | you 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. | |||
2103 | If you need the string after the current function returns, | 2145 | If you need the string after the current function returns, |
2104 | then you should duplicate it (or put it into the registry \see{registry}). | 2146 | then 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. |
2107 | This value must be a C~function; | 2149 | This value must be a C\Nb{}function; |
2108 | otherwise, \verb|lua_tocfunction| returns \verb|NULL|. | 2150 | otherwise, \verb|lua_tocfunction| returns \verb|NULL|. |
2109 | The type \verb|lua_CFunction| is explained in \See{LuacallC}. | 2151 | The 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 | ||
2116 | The API has the following functions to | 2158 | The API has the following functions to |
2117 | push C~values onto the stack: | 2159 | push 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} |
2131 | These functions receive a C~value, | 2173 | These functions receive a C\Nb{}value, |
2132 | convert it to a corresponding Lua value, | 2174 | convert it to a corresponding Lua value, |
2133 | and push the result onto the stack. | 2175 | and push the result onto the stack. |
2134 | In particular, \verb|lua_pushlstring| and \verb|lua_pushstring| | 2176 | In particular, \verb|lua_pushlstring| and \verb|lua_pushstring| |
2135 | make an internal copy of the given string. | 2177 | make 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); |
2138 | otherwise, you should use the more general \verb|lua_pushlstring|, | 2180 | otherwise, you should use the more general \verb|lua_pushlstring|, |
2139 | which accepts an explicit size. | 2181 | which accepts an explicit size. |
@@ -2444,7 +2486,7 @@ removing this value from the stack. | |||
2444 | \subsection{Calling Functions} | 2486 | \subsection{Calling Functions} |
2445 | 2487 | ||
2446 | Functions defined in Lua | 2488 | Functions defined in Lua |
2447 | and C~functions registered in Lua | 2489 | and C\Nb{}functions registered in Lua |
2448 | can be called from the host program. | 2490 | can be called from the host program. |
2449 | This is done using the following protocol: | 2491 | This is done using the following protocol: |
2450 | First, the function to be called is pushed onto the stack; | 2492 | First, 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} |
2474 | Here it is in~C: | 2516 | Here 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.} |
2551 | Some special Lua functions have their own C~interfaces. | 2593 | Some special Lua functions have their own C\Nb{}interfaces. |
2552 | The host program can generate a Lua error calling the function | 2594 | The 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 | |||
2557 | The error message (which actually can be any type of object) | 2599 | The error message (which actually can be any type of object) |
2558 | is popped from the stack. | 2600 | is popped from the stack. |
2559 | This function never returns. | 2601 | This function never returns. |
2560 | If \verb|lua_error| is called from a C~function that | 2602 | If \verb|lua_error| is called from a C\Nb{}function that |
2561 | has been called from Lua, | 2603 | has been called from Lua, |
2562 | then the corresponding Lua execution terminates, | 2604 | then the corresponding Lua execution terminates, |
2563 | as if an error had occurred inside Lua code. | 2605 | as 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 | ||
2584 | Lua can be extended with functions written in~C. | 2626 | Lua can be extended with functions written in\Nb{}C. |
2585 | These functions must be of type \verb|lua_CFunction|, | 2627 | These functions must be of type \verb|lua_CFunction|, |
2586 | which is defined as | 2628 | which 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} |
2591 | A C~function receives a Lua environment and returns an integer, | 2633 | A C\Nb{}function receives a Lua environment and returns an integer, |
2592 | the number of values it has returned to Lua. | 2634 | the number of values it has returned to Lua. |
2593 | 2635 | ||
2594 | In order to communicate properly with Lua, | 2636 | In order to communicate properly with Lua, |
2595 | a C~function must follow the following protocol, | 2637 | a C\Nb{}function must follow the following protocol, |
2596 | which defines the way parameters and results are passed: | 2638 | which defines the way parameters and results are passed: |
2597 | A C~function receives its arguments from Lua in its stack, | 2639 | A C\Nb{}function receives its arguments from Lua in its stack, |
2598 | in direct order (the first argument is pushed first). | 2640 | in direct order (the first argument is pushed first). |
2599 | So, when the function starts, | 2641 | So, when the function starts, |
2600 | its first argument (if any) is at index 1. | 2642 | its first argument (if any) is at index 1. |
2601 | To return values to Lua, a C~function just pushes them onto the stack, | 2643 | To return values to Lua, a C\Nb{}function just pushes them onto the stack, |
2602 | in direct order (the first result is pushed first), | 2644 | in direct order (the first result is pushed first), |
2603 | and returns the number of results. | 2645 | and returns the number of results. |
2604 | Any other value in the stack below the results will be properly | 2646 | Any other value in the stack below the results will be properly |
2605 | discharged by Lua. | 2647 | discharged by Lua. |
2606 | Like a Lua function, a C~function called by Lua can also return | 2648 | Like a Lua function, a C\Nb{}function called by Lua can also return |
2607 | many results. | 2649 | many results. |
2608 | 2650 | ||
2609 | As an example, the following function receives a variable number | 2651 | As 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 | ||
2629 | To register a C~function to Lua, | 2671 | To register a C\Nb{}function to Lua, |
2630 | there is the following convenience macro: | 2672 | there 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: | |||
2640 | which receives the name the function will have in Lua, | 2682 | which receives the name the function will have in Lua, |
2641 | and a pointer to the function. | 2683 | and a pointer to the function. |
2642 | Thus, | 2684 | Thus, |
2643 | the C~function `\verb|foo|' above may be registered in Lua as `\verb|average|' | 2685 | the C\Nb{}function `\verb|foo|' above may be registered in Lua as `\verb|average|' |
2644 | by calling | 2686 | by 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 | ||
2651 | When a C~function is created, | 2693 | When a C\Nb{}function is created, |
2652 | it is possible to associate some values with it, | 2694 | it is possible to associate some values with it, |
2653 | thus creating a \IndexEmph{C~closure}; | 2695 | thus creating a \IndexEmph{C\Nb{}closure}; |
2654 | these values are then accessible to the function whenever it is called. | 2696 | these values are then accessible to the function whenever it is called. |
2655 | To associate values with a C~function, | 2697 | To associate values with a C\Nb{}function, |
2656 | first these values should be pushed onto the stack | 2698 | first 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). |
2658 | Then the function | 2700 | Then 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} |
2663 | is used to push the C~function onto the stack, | 2705 | is used to push the C\Nb{}function onto the stack, |
2664 | with the argument \verb|n| telling how many values should be | 2706 | with the argument \verb|n| telling how many values should be |
2665 | associated with the function | 2707 | associated 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); |
2667 | in fact, the macro \verb|lua_pushcfunction| is defined as | 2709 | in 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 | ||
2670 | Then, whenever the C~function is called, | 2712 | Then, whenever the C\Nb{}function is called, |
2671 | those values are located at specific pseudo-indices. | 2713 | those values are located at specific pseudo-indices. |
2672 | Those pseudo-indices are produced by a macro \IndexAPI{lua_upvalueindex}. | 2714 | Those pseudo-indices are produced by a macro \IndexAPI{lua_upvalueindex}. |
2673 | The first value associated with a function is at position | 2715 | The 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 | ||
2676 | For examples of C~functions and closures, see files | 2718 | For 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| |
2678 | in the official Lua distribution. | 2720 | in the official Lua distribution. |
2679 | 2721 | ||
2680 | 2722 | ||
2681 | \subsubsection*{Registry} \label{registry} | 2723 | \subsubsection*{Registry} \label{registry} |
2682 | 2724 | ||
2683 | Lua provides a pre-defined table that can be used by any C~code to | 2725 | Lua provides a pre-defined table that can be used by any C\Nb{}code to |
2684 | store whatever Lua value it needs to store, | 2726 | store whatever Lua value it needs to store, |
2685 | especially if the C~code needs to keep that Lua value | 2727 | especially if the C\Nb{}code needs to keep that Lua value |
2686 | outside the life span of a C~function. | 2728 | outside the life span of a C\Nb{}function. |
2687 | This table is always located at pseudo-index | 2729 | This table is always located at pseudo-index |
2688 | \IndexAPI{LUA_REGISTRYINDEX}. | 2730 | \IndexAPI{LUA_REGISTRYINDEX}. |
2689 | Any C~library can store data into this table, | 2731 | Any C\Nb{}library can store data into this table, |
2690 | as long as it chooses keys different from other libraries. | 2732 | as long as it chooses keys different from other libraries. |
2691 | Typically, you should use as key a string containing your library name, | 2733 | Typically, you should use as key a string containing your library name, |
2692 | or a light userdata with the address of a C object in your code. | 2734 | or a light userdata with the address of a C object in your code. |
@@ -2696,7 +2738,7 @@ implemented by the auxiliary library, | |||
2696 | and therefore should not be used by other purposes. | 2738 | and 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 | ||
2702 | Lua has no built-in debugging facilities. | 2744 | Lua has no built-in debugging facilities. |
@@ -2716,7 +2758,7 @@ The main function to get information about the interpreter stack is | |||
2716 | This function fills parts of a \verb|lua_Debug| structure with | 2758 | This function fills parts of a \verb|lua_Debug| structure with |
2717 | an identification of the \emph{activation record} | 2759 | an identification of the \emph{activation record} |
2718 | of the function executing at a given level. | 2760 | of the function executing at a given level. |
2719 | Level~0 is the current running function, | 2761 | Level\Nb{}0 is the current running function, |
2720 | whereas level \Math{n+1} is the function that has called level \Math{n}. | 2762 | whereas level \Math{n+1} is the function that has called level \Math{n}. |
2721 | Usually, \verb|lua_getstack| returns 1; | 2763 | Usually, \verb|lua_getstack| returns 1; |
2722 | when called with a level greater than the stack depth, | 2764 | when called with a level greater than the stack depth, |
@@ -2781,16 +2823,16 @@ The fields of \verb|lua_Debug| have the following meaning: | |||
2781 | If the function was defined in a string, | 2823 | If the function was defined in a string, |
2782 | then \verb|source| is that string; | 2824 | then \verb|source| is that string; |
2783 | if the function was defined in a file, | 2825 | if the function was defined in a file, |
2784 | then \verb|source| starts with a \verb|@| followed by the file name. | 2826 | then \verb|source| starts with a \At{} followed by the file name. |
2785 | 2827 | ||
2786 | \item[short\_src] | 2828 | \item[short_src] |
2787 | A \Q{printable} version of \verb|source|, to be used in error messages. | 2829 | A \Q{printable} version of \verb|source|, to be used in error messages. |
2788 | 2830 | ||
2789 | \item[linedefined] | 2831 | \item[linedefined] |
2790 | the line number where the definition of the function starts. | 2832 | the 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, |
2794 | or \verb|"main"| if this is the main part of a chunk. | 2836 | or \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 | ||
2828 | For the manipulation of local variables, | 2870 | For the manipulation of local variables, |
2829 | \verb|luadebug.h| uses indices: | 2871 | \verb|luadebug.h| uses indices: |
2830 | The first parameter or local variable has index~1, and so on, | 2872 | The first parameter or local variable has index\Nb{}1, and so on, |
2831 | until the last active local variable. | 2873 | until the last active local variable. |
2832 | 2874 | ||
2833 | The following functions allow the manipulation of the | 2875 | The following functions allow the manipulation of the |
@@ -2936,11 +2978,11 @@ Therefore, if a hook calls Lua to execute a function or a chunk, | |||
2936 | that execution occurs without any calls to hooks. | 2978 | that 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 | ||
2942 | The standard libraries provide useful functions | 2984 | The standard libraries provide useful functions |
2943 | that are implemented directly through the standard C~API. | 2985 | that are implemented directly through the standard C\Nb{}API. |
2944 | Some of these functions provide essential services to the language | 2986 | Some 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|); |
2946 | others provide access to \Q{outside} services (e.g. I/O); | 2988 | others provide access to \Q{outside} services (e.g. I/O); |
@@ -2949,7 +2991,7 @@ but are quite useful or have critical performance to | |||
2949 | deserve an implementation in C (e.g. \verb|sort|). | 2991 | deserve an implementation in C (e.g. \verb|sort|). |
2950 | 2992 | ||
2951 | All libraries are implemented through the official C API, | 2993 | All libraries are implemented through the official C API, |
2952 | and are provided as separate C~modules. | 2994 | and are provided as separate C\Nb{}modules. |
2953 | Currently, Lua has the following standard libraries: | 2995 | Currently, 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 | |||
2965 | or as methods of its objects. | 3007 | or as methods of its objects. |
2966 | 3008 | ||
2967 | To have access to these libraries, | 3009 | To have access to these libraries, |
2968 | the C~host program must call the functions | 3010 | the 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} |
2996 | Issues an \emph{\Q{assertion failed!}} error | 3038 | Issues an \emph{\Q{assertion failed!}} error |
2997 | when its argument \verb|v| is \nil{} or \False; | 3039 | when its argument \verb|v| is \nil{} or \False{}; |
2998 | otherwise, returns this argument. | 3040 | otherwise, returns this argument. |
2999 | This function is equivalent to the following Lua function: | 3041 | This 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 | ||
3050 | Returns the metatable of the given object. | 3092 | Returns the metatable of the given object. |
3051 | If the object does not have a metatable, returns \nil. | 3093 | If 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} |
3054 | Returns the number of Kbytes of dynamic memory Lua is using, | 3096 | Returns the number of Kbytes of dynamic memory Lua is using, |
@@ -3097,12 +3139,12 @@ When called with \nil{} as its second argument, | |||
3097 | of the table and its associated value. | 3139 | of the table and its associated value. |
3098 | When called with the last index, | 3140 | When called with the last index, |
3099 | or with \nil{} in an empty table, | 3141 | or with \nil{} in an empty table, |
3100 | \verb|next| returns \nil. | 3142 | \verb|next| returns \nil{}. |
3101 | If the second argument is absent, then it is interpreted as \nil. | 3143 | If the second argument is absent, then it is interpreted as \nil{}. |
3102 | 3144 | ||
3103 | Lua has no declaration of fields; | 3145 | Lua has no declaration of fields; |
3104 | semantically, there is no difference between a | 3146 | semantically, there is no difference between a |
3105 | field not present in a table or a field with value \nil. | 3147 | field not present in a table or a field with value \nil{}. |
3106 | Therefore, \verb|next| only considers fields with non-\nil{} values. | 3148 | Therefore, \verb|next| only considers fields with non-\nil{} values. |
3107 | The order in which the indices are enumerated is not specified, | 3149 | The 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 | ||
3117 | Returns the function \verb|next| and the table \verb|t| (plus a \nil), | 3159 | Returns the function \verb|next| and the table \verb|t| (plus a \nil{}), |
3118 | so that the construction | 3160 | so 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}. | |||
3147 | Gets the real value of \verb|table[index]|, | 3189 | Gets the real value of \verb|table[index]|, |
3148 | without invoking any metamethod. | 3190 | without 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} |
3153 | Sets the real value of \verb|table[index]| to \verb|value|, | 3195 | Sets the real value of \verb|table[index]| to \verb|value|, |
3154 | without invoking any metamethod. | 3196 | without 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{}; |
3157 | and \verb|value| is any Lua value. | 3199 | and \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 | ||
3206 | Sets the metatable for the given table. | 3248 | Sets 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.) |
3208 | If \verb|metatable| is \nil, removes the metatable of the given table. | 3250 | If \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} |
3211 | Tries to convert its argument to a number. | 3253 | Tries to convert its argument to a number. |
3212 | If the argument is already a number or a string convertible | 3254 | If the argument is already a number or a string convertible |
3213 | to a number, then \verb|tonumber| returns that number; | 3255 | to a number, then \verb|tonumber| returns that number; |
3214 | otherwise, it returns \nil. | 3256 | otherwise, it returns \nil{}. |
3215 | 3257 | ||
3216 | An optional argument specifies the base to interpret the numeral. | 3258 | An optional argument specifies the base to interpret the numeral. |
3217 | The base may be any integer between 2 and 36, inclusive. | 3259 | The base may be any integer between 2 and 36, inclusive. |
3218 | In bases above~10, the letter `A' (in either upper or lower case) | 3260 | In bases above\Nb{}10, the letter `A' (in either upper or lower case) |
3219 | represents~10, `B' represents~11, and so forth, with `Z' representing 35. | 3261 | represents\Nb{}10, `B' represents\Nb{}11, and so forth, with `Z' representing 35. |
3220 | In base 10 (the default), the number may have a decimal part, | 3262 | In base 10 (the default), the number may have a decimal part, |
3221 | as well as an optional exponent part \see{coercion}. | 3263 | as well as an optional exponent part \see{coercion}. |
3222 | In other bases, only unsigned integers are accepted. | 3264 | In 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} |
3231 | Returns the type of its only argument, coded as a string. | 3273 | Returns the type of its only argument, coded as a string. |
3232 | The possible results of this function are | 3274 | The 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 | |||
3246 | except that the above code can be valid only for a fixed \M{n}. | 3288 | except that the above code can be valid only for a fixed \M{n}. |
3247 | The number \M{n} of returned values | 3289 | The number \M{n} of returned values |
3248 | is either the value of \verb|list.n|, if it is a number, | 3290 | is either the value of \verb|list.n|, if it is a number, |
3249 | or one less the index of the first absent (\nil) value. | 3291 | or one less the index of the first absent (\nil{}) value. |
3250 | 3292 | ||
3251 | \subsection{String Manipulation} | 3293 | \subsection{String Manipulation} |
3252 | This library provides generic functions for string manipulation, | 3294 | This library provides generic functions for string manipulation, |
3253 | such as finding and extracting substrings and pattern matching. | 3295 | such as finding and extracting substrings and pattern matching. |
3254 | When indexing a string in Lua, the first character is at position~1 | 3296 | When 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). |
3256 | Indices are allowed to be negative and are interpreted as indexing backwards, | 3298 | Indices are allowed to be negative and are interpreted as indexing backwards, |
3257 | from the end of the string. | 3299 | from the end of the string. |
3258 | Thus, the last character is at position \Math{-1}, and so on. | 3300 | Thus, 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} |
3264 | Returns the internal numerical code of the \M{i}-th character of \verb|s|. | 3306 | Returns the internal numerical code of the \M{i}-th character of \verb|s|. |
3265 | If \verb|i| is absent, then it is assumed to be~1. | 3307 | If \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|. |
3284 | If it finds one, then \verb|find| returns the indices of \verb|s| | 3326 | If it finds one, then \verb|find| returns the indices of \verb|s| |
3285 | where this occurrence starts and ends; | 3327 | where this occurrence starts and ends; |
3286 | otherwise, it returns \nil. | 3328 | otherwise, it returns \nil{}. |
3287 | If the pattern specifies captures (see \verb|string.gsub| below), | 3329 | If the pattern specifies captures (see \verb|string.gsub| below), |
3288 | the captured strings are returned as extra results. | 3330 | the captured strings are returned as extra results. |
3289 | A third, optional numerical argument \verb|init| specifies | 3331 | A third, optional numerical argument \verb|init| specifies |
3290 | where to start the search; | 3332 | where to start the search; |
3291 | its default value is~1, and may be negative. | 3333 | its default value is\Nb{}1, and may be negative. |
3292 | A value of \True{} as a fourth, optional argument \verb|plain| | 3334 | A value of \True{} as a fourth, optional argument \verb|plain| |
3293 | turns off the pattern matching facilities, | 3335 | turns off the pattern matching facilities, |
3294 | so the function does a plain \Q{find substring} operation, | 3336 | so 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. | |||
3334 | Returns a formatted version of its variable number of arguments | 3376 | Returns a formatted version of its variable number of arguments |
3335 | following the description given in its first argument (which must be a string). | 3377 | following the description given in its first argument (which must be a string). |
3336 | The format string follows the same rules as the \verb|printf| family of | 3378 | The format string follows the same rules as the \verb|printf| family of |
3337 | standard C~functions. | 3379 | standard C\Nb{}functions. |
3338 | The only differences are that the options/modifiers | 3380 | The 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|, |
3340 | and \verb|h| are not supported, | 3382 | and \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{} |
3473 | represents the character \M{x}. | 3515 | represents the character \M{x}. |
3474 | This is the standard way to escape the magic characters. | 3516 | This is the standard way to escape the magic characters. |
3475 | We recommend that any punctuation character (even the non magic) | 3517 | We recommend that any punctuation character (even the non magic) |
@@ -3494,7 +3536,7 @@ The interaction between ranges and classes is not defined. | |||
3494 | Therefore, patterns like \verb|[%a-z]| or \verb|[a-%%]| | 3536 | Therefore, patterns like \verb|[%a-z]| or \verb|[a-%%]| |
3495 | have no meaning. | 3537 | have no meaning. |
3496 | 3538 | ||
3497 | \item[\T{[\^\null\M{set}]}] \Em{} | 3539 | \item[\T{[^\M{set}]}] \Em{} |
3498 | represents the complement of \M{set}, | 3540 | represents the complement of \M{set}, |
3499 | where \M{set} is interpreted as above. | 3541 | where \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), | |||
3502 | the corresponding uppercase letter represents the complement of the class. | 3544 | the corresponding uppercase letter represents the complement of the class. |
3503 | For instance, \verb|%S| represents all non-space characters. | 3545 | For instance, \verb|%S| represents all non-space characters. |
3504 | 3546 | ||
3505 | The definitions of letter, space, etc.\ depend on the current locale. | 3547 | The definitions of letter, space, etc.{} depend on the current locale. |
3506 | In particular, the class \verb|[a-z]| may not be equivalent to \verb|%l|. | 3548 | In particular, the class \verb|[a-z]| may not be equivalent to \verb|%l|. |
3507 | The second form should be preferred for portability. | 3549 | The second form should be preferred for portability. |
3508 | 3550 | ||
@@ -3529,12 +3571,12 @@ these repetition items will always match the \emph{shortest} possible sequence; | |||
3529 | a single character class followed by \verb|?|, | 3571 | a single character class followed by \verb|?|, |
3530 | which matches 0 or 1 occurrence of a character in the class; | 3572 | which 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; |
3533 | such item matches a substring equal to the \M{n}-th captured string | 3575 | such 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; |
3537 | such item matches strings that start with~\M{x}, end with~\M{y}, | 3579 | such item matches strings that start with\Nb{}\M{x}, end with\Nb{}\M{y}, |
3538 | and where the \M{x} and \M{y} are \emph{balanced}. | 3580 | and where the \M{x} and \M{y} are \emph{balanced}. |
3539 | This means that, if one reads the string from left to right, | 3581 | This means that, if one reads the string from left to right, |
3540 | counting \Math{+1} for an \M{x} and \Math{-1} for a \M{y}, | 3582 | counting \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. | |||
3560 | Captures are numbered according to their left parentheses. | 3602 | Captures are numbered according to their left parentheses. |
3561 | For instance, in the pattern \verb|"(a*(.)%w(%s*))"|, | 3603 | For instance, in the pattern \verb|"(a*(.)%w(%s*))"|, |
3562 | the part of the string matching \verb|"a*(.)%w(%s*)"| is | 3604 | the part of the string matching \verb|"a*(.)%w(%s*)"| is |
3563 | stored as the first capture (and therefore has number~1); | 3605 | stored as the first capture (and therefore has number\Nb{}1); |
3564 | the character matching \verb|.| is captured with number~2, | 3606 | the character matching \verb|.| is captured with number\Nb{}2, |
3565 | and the part matching \verb|%s*| has number~3. | 3607 | and the part matching \verb|%s*| has number\Nb{}3. |
3566 | 3608 | ||
3567 | As a special case, the empty capture \verb|()| captures | 3609 | As a special case, the empty capture \verb|()| captures |
3568 | the current string position (a number). | 3610 | the current string position (a number). |
@@ -3621,7 +3663,7 @@ numerical indices of \verb|table|. | |||
3621 | For each index, \verb|func| is called with the index and | 3663 | For each index, \verb|func| is called with the index and |
3622 | respective value as arguments. | 3664 | respective value as arguments. |
3623 | Indices are visited in sequential order, | 3665 | Indices are visited in sequential order, |
3624 | from~1 to \verb|n|, | 3666 | from\Nb{}1 to \verb|n|, |
3625 | where \verb|n| is the size of the table \see{getn}. | 3667 | where \verb|n| is the size of the table \see{getn}. |
3626 | If \verb|func| returns a non-\nil{} value, | 3668 | If \verb|func| returns a non-\nil{} value, |
3627 | then the loop is broken, and this value is returned | 3669 | then 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 | ||
3697 | This library is an interface to most of the functions of the | 3739 | This library is an interface to most of the functions of the |
3698 | standard C~math library. | 3740 | standard C\Nb{}math library. |
3699 | (Some have slightly different names.) | 3741 | (Some have slightly different names.) |
3700 | It provides all its functions inside the table \IndexLIB{math}. | 3742 | It provides all its functions inside the table \IndexLIB{math}. |
3701 | In addition, | 3743 | In addition, |
3702 | it registers a ??tag method for the binary exponentiation operator \verb|^| | 3744 | it registers a ??tag method for the binary exponentiation operator \verb|^| |
3703 | that returns \Math{x^y} when applied to numbers \verb|x| and \verb|y|. | 3745 | that returns \Math{x\sp{y}} when applied to numbers \verb|x| and \verb|y|. |
3704 | 3746 | ||
3705 | The library provides the following functions: | 3747 | The 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} |
3722 | plus a variable \IndexLIB{math.pi}. | 3764 | plus a variable \IndexLIB{math.pi}. |
3723 | Most of them | 3765 | Most of them |
3724 | are only interfaces to the corresponding functions in the C~library. | 3766 | are only interfaces to the corresponding functions in the C\Nb{}library. |
3725 | All trigonometric functions work in radians. | 3767 | All trigonometric functions work in radians. |
3726 | The functions \verb|math.deg| and \verb|math.rad| convert | 3768 | The functions \verb|math.deg| and \verb|math.rad| convert |
3727 | between radians and degrees. | 3769 | between radians and degrees. |
@@ -3733,7 +3775,7 @@ Both can be used with 1, 2, or more arguments. | |||
3733 | 3775 | ||
3734 | The functions \verb|math.random| and \verb|math.randomseed| | 3776 | The functions \verb|math.random| and \verb|math.randomseed| |
3735 | are interfaces to the simple random generator functions | 3777 | are 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.) |
3738 | When called without arguments, | 3780 | When 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} |
3834 | The \verb|mode| string may also have a \verb|b| at the end, | 3876 | The \verb|mode| string may also have a \verb|b| at the end, |
3835 | which is needed in some systems to open the file in binary mode. | 3877 | which is needed in some systems to open the file in binary mode. |
3836 | This string is exactly what is used in the standard~C function \verb|fopen|. | 3878 | This 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} |
3917 | In case of success, function \verb|seek| returns the final file position, | 3959 | In case of success, function \verb|seek| returns the final file position, |
3918 | measured in bytes from the beginning of the file. | 3960 | measured in bytes from the beginning of the file. |
3919 | If this function fails, it returns \nil, | 3961 | If this function fails, it returns \nil{}, |
3920 | plus a string describing the error. | 3962 | plus a string describing the error. |
3921 | 3963 | ||
3922 | The default value for \verb|whence| is \verb|"cur"|, | 3964 | The default value for \verb|whence| is \verb|"cur"|, |
@@ -3935,7 +3977,7 @@ the filehandle \verb|file|. | |||
3935 | The arguments must be strings or numbers. | 3977 | The arguments must be strings or numbers. |
3936 | To write other values, | 3978 | To write other values, |
3937 | use \verb|tostring| or \verb|format| before \verb|write|. | 3979 | use \verb|tostring| or \verb|format| before \verb|write|. |
3938 | If this function fails, it returns \nil, | 3980 | If this function fails, it returns \nil{}, |
3939 | plus a string describing the error. | 3981 | plus a string describing the error. |
3940 | 3982 | ||
3941 | 3983 | ||
@@ -3972,7 +4014,7 @@ and \verb|isdst| (daylight saving flag, a boolean). | |||
3972 | 4014 | ||
3973 | If format is not \verb|*t|, | 4015 | If format is not \verb|*t|, |
3974 | then \verb|date| returns the date as a string, | 4016 | then \verb|date| returns the date as a string, |
3975 | formatted according with the same rules as the C~function \verb|strftime|. | 4017 | formatted according with the same rules as the C\Nb{}function \verb|strftime|. |
3976 | When called without arguments, | 4018 | When 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 |
3978 | the host system and on the current locale | 4020 | the 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 | ||
3989 | This function is equivalent to the C~function \verb|system|. | 4031 | This function is equivalent to the C\Nb{}function \verb|system|. |
3990 | It passes \verb|command| to be executed by an operating system shell. | 4032 | It passes \verb|command| to be executed by an operating system shell. |
3991 | It returns a status code, which is system-dependent. | 4033 | It 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 | ||
3995 | Calls the C~function \verb|exit|, | 4037 | Calls the C\Nb{}function \verb|exit|, |
3996 | with an optional \verb|code|, | 4038 | with an optional \verb|code|, |
3997 | to terminate the host program. | 4039 | to terminate the host program. |
3998 | The default value for \verb|code| is the success code. | 4040 | The 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 | ||
4007 | Deletes the file with the given name. | 4049 | Deletes the file with the given name. |
4008 | If this function fails, it returns \nil, | 4050 | If this function fails, it returns \nil{}, |
4009 | plus a string describing the error. | 4051 | plus 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 | ||
4013 | Renames file named \verb|name1| to \verb|name2|. | 4055 | Renames file named \verb|name1| to \verb|name2|. |
4014 | If this function fails, it returns \nil, | 4056 | If this function fails, it returns \nil{}, |
4015 | plus a string describing the error. | 4057 | plus 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 | ||
4019 | This function is an interface to the C~function \verb|setlocale|. | 4061 | This 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. | |||
4047 | The file must be explicitly opened before its use | 4089 | The file must be explicitly opened before its use |
4048 | and removed when no longer needed. | 4090 | and removed when no longer needed. |
4049 | 4091 | ||
4050 | This function is equivalent to the \verb|tmpnam| C~function, | 4092 | This function is equivalent to the \verb|tmpnam| C\Nb{}function, |
4051 | and many people (and even some compilers!) advise against its use, | 4093 | and many people (and even some compilers!) advise against its use, |
4052 | because between the time you call this function | 4094 | because between the time you call this function |
4053 | and the time you open the file, | 4095 | and the time you open the file, |
@@ -4083,7 +4125,7 @@ Level 0 is the current function (\verb|getinfo| itself); | |||
4083 | level 1 is the function that called \verb|getinfo|; | 4125 | level 1 is the function that called \verb|getinfo|; |
4084 | and so on. | 4126 | and so on. |
4085 | If \verb|function| is a number larger than the number of active functions, | 4127 | If \verb|function| is a number larger than the number of active functions, |
4086 | then \verb|getinfo| returns \nil. | 4128 | then \verb|getinfo| returns \nil{}. |
4087 | 4129 | ||
4088 | The returned table contains all the fields returned by \verb|lua_getinfo|, | 4130 | The returned table contains all the fields returned by \verb|lua_getinfo|, |
4089 | with the string \verb|what| describing which fields to fill in. | 4131 | with the string \verb|what| describing which fields to fill in. |
@@ -4102,7 +4144,7 @@ about the \verb|print| function. | |||
4102 | 4144 | ||
4103 | This function returns the name and the value of the local variable | 4145 | This function returns the name and the value of the local variable |
4104 | with index \verb|local| of the function at level \verb|level| of the stack. | 4146 | with 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, |
4106 | until the last active local variable.) | 4148 | until the last active local variable.) |
4107 | The function returns \nil{} if there is no local | 4149 | The function returns \nil{} if there is no local |
4108 | variable with the given index, | 4150 | variable with the given index, |
@@ -4146,8 +4188,8 @@ it also gets as its second parameter the new line number. | |||
4146 | Inside a hook, | 4188 | Inside a hook, |
4147 | you can call \verb|getinfo| with level 2 to get more information about | 4189 | you can call \verb|getinfo| with level 2 to get more information about |
4148 | the running function | 4190 | the running function |
4149 | (level~0 is the \verb|getinfo| function, | 4191 | (level\Nb{}0 is the \verb|getinfo| function, |
4150 | and level~1 is the hook function). | 4192 | and 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, | |||
4156 | and the current hook count (as set by the \verb|debug.sethook| function). | 4198 | and 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 | ||
4162 | Although Lua has been designed as an extension language, | 4204 | Although Lua has been designed as an extension language, |
4163 | to be embedded in a host C~program, | 4205 | to be embedded in a host C\Nb{}program, |
4164 | it is also frequently used as a stand-alone language. | 4206 | it is also frequently used as a stand-alone language. |
4165 | An interpreter for Lua as a stand-alone language, | 4207 | An interpreter for Lua as a stand-alone language, |
4166 | called simply \verb|lua|, | 4208 | called simply \verb|lua|, |
@@ -4188,7 +4230,7 @@ and as \verb|lua -| otherwise. | |||
4188 | 4230 | ||
4189 | Before running any argument, | 4231 | Before running any argument, |
4190 | the interpreter checks for an environment variable \IndexVerb{LUA_INIT}. | 4232 | the interpreter checks for an environment variable \IndexVerb{LUA_INIT}. |
4191 | If its format is \verb|@|\emph{filename}, | 4233 | If its format is \At{}\emph{filename}, |
4192 | then lua executes the file. | 4234 | then lua executes the file. |
4193 | Otherwise, lua executes the string itself. | 4235 | Otherwise, lua executes the string itself. |
4194 | 4236 | ||
@@ -4240,7 +4282,7 @@ Note the use of \verb|-i| to enter interactive mode; otherwise, | |||
4240 | the program would end just after the assignment to \verb|_PROMPT|. | 4282 | the program would end just after the assignment to \verb|_PROMPT|. |
4241 | 4283 | ||
4242 | In Unix systems, Lua scripts can be made into executable programs | 4284 | In Unix systems, Lua scripts can be made into executable programs |
4243 | by using \verb|chmod +x| and the~\verb|#!| form, | 4285 | by using \verb|chmod +x| and the\Nb{}\verb|#!| form, |
4244 | as in | 4286 | as 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} |
4255 | is a more portable solution.) | 4297 | is a more portable solution.) |
4256 | 4298 | ||
4257 | \C{------------------------------------------------------------------------------} | 4299 | \C{-------------------------------------------------------------------------} |
4258 | \section*{Acknowledgments} | 4300 | \section*{Acknowledgments} |
4259 | 4301 | ||
4260 | The Lua team is grateful to \tecgraf{} for its continued support to Lua. | 4302 | The Lua team is grateful to \tecgraf{} for its continued support to Lua. |
@@ -4264,8 +4306,8 @@ At the risk of omitting several names, | |||
4264 | we also thank the following individuals for supporting, | 4306 | we also thank the following individuals for supporting, |
4265 | contributing to, and spreading the word about Lua: | 4307 | contributing to, and spreading the word about Lua: |
4266 | Alan Watson. | 4308 | Alan Watson. |
4267 | Andr\'e Clinio, | 4309 | André Clinio, |
4268 | Andr\'e Costa, | 4310 | André Costa, |
4269 | Antonio Scuri, | 4311 | Antonio Scuri, |
4270 | Bret Mogilefsky, | 4312 | Bret Mogilefsky, |
4271 | Cameron Laird, | 4313 | Cameron Laird, |
@@ -4293,7 +4335,7 @@ Reuben Thomas, | |||
4293 | Stephan Herrmann, | 4335 | Stephan Herrmann, |
4294 | Steve Dekorte, | 4336 | Steve Dekorte, |
4295 | Thatcher Ulrich, | 4337 | Thatcher Ulrich, |
4296 | Tom\'as Gorham, | 4338 | Tomás Gorham, |
4297 | Vincent Penquerc'h, | 4339 | Vincent Penquerc'h, |
4298 | Thank you! | 4340 | Thank you! |
4299 | 4341 | ||
@@ -4383,20 +4425,14 @@ Userdata!! | |||
4383 | 4425 | ||
4384 | The notation used here is the usual extended BNF, | 4426 | The notation used here is the usual extended BNF, |
4385 | in which | 4427 | in 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}. |
4388 | Non-terminals are shown in \emph{italics}, | 4430 | Non-terminals are shown in \emph{italics}, |
4389 | keywords are shown in {\bf bold}, | 4431 | keywords are shown in {\bf bold}, |
4390 | and other terminal symbols are shown in {\tt typewriter} font, | 4432 | and other terminal symbols are shown in {\tt typewriter} font, |
4391 | enclosed in single quotes. | 4433 | enclosed 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 | ||