diff options
Diffstat (limited to '')
| -rw-r--r-- | manual.tex | 459 |
1 files changed, 288 insertions, 171 deletions
| @@ -1,9 +1,9 @@ | |||
| 1 | % $Id: manual.tex,v 1.24 1996/11/14 17:45:37 roberto Exp roberto $ | 1 | % $Id: manual.tex,v 1.25 1996/11/18 14:27:42 roberto Exp $ |
| 2 | 2 | ||
| 3 | \documentstyle[fullpage,11pt,bnf]{article} | 3 | \documentstyle[fullpage,11pt,bnf]{article} |
| 4 | 4 | ||
| 5 | \newcommand{\rw}[1]{{\bf #1}} | 5 | \newcommand{\rw}[1]{{\bf #1}} |
| 6 | \newcommand{\see}[1]{see Section~\ref{#1}} | 6 | \newcommand{\see}[1]{(see Section~\ref{#1})} |
| 7 | \newcommand{\nil}{{\bf nil}} | 7 | \newcommand{\nil}{{\bf nil}} |
| 8 | \newcommand{\Line}{\rule{\linewidth}{.5mm}} | 8 | \newcommand{\Line}{\rule{\linewidth}{.5mm}} |
| 9 | \def\tecgraf{{\sf TeC\kern-.21em\lower.7ex\hbox{Graf}}} | 9 | \def\tecgraf{{\sf TeC\kern-.21em\lower.7ex\hbox{Graf}}} |
| @@ -35,7 +35,7 @@ Waldemar Celes | |||
| 35 | \tecgraf\ --- Departamento de Inform\'atica --- PUC-Rio | 35 | \tecgraf\ --- Departamento de Inform\'atica --- PUC-Rio |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | \date{\small \verb$Date: 1996/11/14 17:45:37 $} | 38 | \date{\small \verb$Date: 1996/11/18 14:27:42 $} |
| 39 | 39 | ||
| 40 | \maketitle | 40 | \maketitle |
| 41 | 41 | ||
| @@ -66,25 +66,64 @@ ca\-racte\-r\'{\i}sticas do sistema. | |||
| 66 | \end{quotation} | 66 | \end{quotation} |
| 67 | 67 | ||
| 68 | 68 | ||
| 69 | \vfill | ||
| 70 | \begin{quotation} | ||
| 71 | \noindent | ||
| 72 | \small | ||
| 73 | Copyright (c) 1994--1996 TeCGraf, PUC-Rio. Written by Waldemar Celes Filho, | ||
| 74 | Roberto Ierusalimschy, Luiz Henrique de Figueiredo. All rights reserved. | ||
| 75 | % | ||
| 76 | Permission is hereby granted, without written agreement and without license or | ||
| 77 | royalty fees, to use, copy, modify, and distribute this software and its | ||
| 78 | documentation for any purpose, subject to the following conditions: | ||
| 79 | % | ||
| 80 | The above copyright notice and this permission notice shall appear in all | ||
| 81 | copies or substantial portions of this software. | ||
| 82 | % | ||
| 83 | The name "Lua" cannot be used for any modified form of this software that does | ||
| 84 | not originate from the authors. Nevertheless, the name "Lua" may and should be | ||
| 85 | used to designate the language implemented and described in this package, | ||
| 86 | even if embedded in any other system, as long as its syntax and semantics | ||
| 87 | remain unchanged. | ||
| 88 | % | ||
| 89 | The authors specifically disclaim any warranties, including, but not limited | ||
| 90 | to, the implied warranties of merchantability and fitness for a particular | ||
| 91 | purpose. The software provided hereunder is on an "as is" basis, and the | ||
| 92 | authors have no obligation to provide maintenance, support, updates, | ||
| 93 | enhancements, or modifications. In no event shall TeCGraf, PUC-Rio, or the | ||
| 94 | authors be liable to any party for direct, indirect, special, incidental, or | ||
| 95 | consequential damages arising out of the use of this software and its | ||
| 96 | documentation. | ||
| 97 | \end{quotation} | ||
| 98 | \vfill | ||
| 99 | |||
| 100 | \thispagestyle{empty} | ||
| 101 | \setcounter{page}{0} | ||
| 102 | \newpage | ||
| 103 | |||
| 104 | |||
| 69 | \section{Introduction} | 105 | \section{Introduction} |
| 70 | 106 | ||
| 71 | Lua is an extension programming language designed to support | 107 | Lua is an extension programming language designed to support |
| 72 | general procedural programming features with data description | 108 | general procedural programming features with data description |
| 73 | facilities. | 109 | facilities. |
| 74 | It is intended to be used as a configuration language for any | 110 | It is intended to be used as a |
| 111 | light-weight, but powerful, configuration language for any | ||
| 75 | program that needs one. | 112 | program that needs one. |
| 76 | Lua has been designed and implemented by | 113 | Lua has been designed and implemented by |
| 77 | W.~Celes, L.~H.~de Figueiredo and R.~Ierusalimschy. | 114 | W.~Celes, |
| 115 | R.~Ierusalimschy and | ||
| 116 | L.~H.~de Figueiredo. | ||
| 78 | 117 | ||
| 79 | Lua is implemented as a library, written in C. | 118 | Lua is implemented as a library, written in C. |
| 80 | Being an extension language, Lua has no notion of a ``main'' program: | 119 | Being an extension language, Lua has no notion of a ``main'' program: |
| 81 | it only works {\em embedded} in a host client, | 120 | it only works {\em embedded\/} in a host client, |
| 82 | called the {\em embedding} program. | 121 | called the {\em embedding\/} program. |
| 83 | This host program can invoke functions to execute a piece of | 122 | This host program can invoke functions to execute a piece of |
| 84 | code in Lua, can write and read Lua variables, | 123 | code in Lua, can write and read Lua variables, |
| 85 | and can register C functions to be called by Lua code. | 124 | and can register C functions to be called by Lua code. |
| 86 | Through the use of C functions, Lua can be augmented to cope with | 125 | Through the use of C functions, Lua can be augmented to cope with |
| 87 | rather different domains, | 126 | many, completely different domains, |
| 88 | thus creating customized programming languages sharing a syntactical framework. | 127 | thus creating customized programming languages sharing a syntactical framework. |
| 89 | 128 | ||
| 90 | Lua is free-distribution software, | 129 | Lua is free-distribution software, |
| @@ -111,7 +150,7 @@ using functions in the library that implements Lua. | |||
| 111 | 150 | ||
| 112 | \Index{Global variables} do not need declaration. | 151 | \Index{Global variables} do not need declaration. |
| 113 | Any variable is assumed to be global unless explicitly declared local | 152 | Any variable is assumed to be global unless explicitly declared local |
| 114 | (\see{localvar}). | 153 | \see{localvar}. |
| 115 | Before the first assignment, the value of a global variable is \nil. | 154 | Before the first assignment, the value of a global variable is \nil. |
| 116 | 155 | ||
| 117 | The unit of execution of Lua is called a \Def{chunk}. | 156 | The unit of execution of Lua is called a \Def{chunk}. |
| @@ -125,7 +164,7 @@ for chunks is: | |||
| 125 | \end{Produc}% | 164 | \end{Produc}% |
| 126 | A chunk may contain statements and function definitions, | 165 | A chunk may contain statements and function definitions, |
| 127 | and may be in a file or in a string inside the host program. | 166 | and may be in a file or in a string inside the host program. |
| 128 | A chunk may optionally ends with a return statement (\see{return}). | 167 | A chunk may optionally end with a \verb|return| statement \see{return}. |
| 129 | When a chunk is executed, first all its functions and statements are compiled, | 168 | When a chunk is executed, first all its functions and statements are compiled, |
| 130 | then the statements are executed in sequential order. | 169 | then the statements are executed in sequential order. |
| 131 | All modifications a chunk effects on the global environment persist | 170 | All modifications a chunk effects on the global environment persist |
| @@ -133,7 +172,7 @@ after its end. | |||
| 133 | Those include modifications to global variables and definitions | 172 | Those include modifications to global variables and definitions |
| 134 | of new functions% | 173 | of new functions% |
| 135 | \footnote{Actually, a function definition is an | 174 | \footnote{Actually, a function definition is an |
| 136 | assignment to a global variable; \see{TypesSec}.}. | 175 | assignment to a global variable \see{TypesSec}.}. |
| 137 | 176 | ||
| 138 | Chunks may be pre-compiled; see program \IndexVerb{luac} for details. | 177 | Chunks may be pre-compiled; see program \IndexVerb{luac} for details. |
| 139 | Text files with chunks and their binary pre-compiled forms | 178 | Text files with chunks and their binary pre-compiled forms |
| @@ -151,10 +190,10 @@ Therefore, there are no type definitions in the language. | |||
| 151 | There are seven \Index{basic types} in Lua: \Def{nil}, \Def{number}, | 190 | There are seven \Index{basic types} in Lua: \Def{nil}, \Def{number}, |
| 152 | \Def{string}, \Def{function}, \Def{CFunction}, \Def{userdata}, | 191 | \Def{string}, \Def{function}, \Def{CFunction}, \Def{userdata}, |
| 153 | and \Def{table}. | 192 | and \Def{table}. |
| 154 | {\em Nil} is the type of the value \nil, | 193 | {\em Nil\/} is the type of the value \nil, |
| 155 | whose main property is to be different from any other value. | 194 | whose main property is to be different from any other value. |
| 156 | {\em Number} represents real (floating point) numbers, | 195 | {\em Number\/} represents real (floating point) numbers, |
| 157 | while {\em string} has the usual meaning. | 196 | while {\em string\/} has the usual meaning. |
| 158 | 197 | ||
| 159 | Functions are considered first-class values in Lua. | 198 | Functions are considered first-class values in Lua. |
| 160 | This means that functions can be stored in variables, | 199 | This means that functions can be stored in variables, |
| @@ -162,16 +201,16 @@ passed as arguments to other functions and returned as results. | |||
| 162 | When a function is defined in Lua, its body is compiled and stored | 201 | When a function is defined in Lua, its body is compiled and stored |
| 163 | in a given variable. | 202 | in a given variable. |
| 164 | Lua can call (and manipulate) functions written in Lua and | 203 | Lua can call (and manipulate) functions written in Lua and |
| 165 | functions written in C; the latter have type {\em CFunction\/}. | 204 | functions written in C; the latter have type {\em CFunction}. |
| 166 | 205 | ||
| 167 | The type {\em userdata} is provided to allow | 206 | The type {\em userdata\/} is provided to allow |
| 168 | arbitrary \Index{C pointers} to be stored in Lua variables. | 207 | arbitrary \Index{C pointers} to be stored in Lua variables. |
| 169 | It corresponds to \verb'void*' and has no pre-defined operations in Lua, | 208 | It corresponds to \verb'void*' and has no pre-defined operations in Lua, |
| 170 | besides assignment and equality test. | 209 | besides assignment and equality test. |
| 171 | However, by using fallbacks, the programmer may define operations | 210 | However, by using fallbacks, the programmer may define operations |
| 172 | for {\em userdata} values; \see{fallback}. | 211 | for {\em userdata\/} values; \see{fallback}. |
| 173 | 212 | ||
| 174 | The type {\em table} implements \Index{associative arrays}, | 213 | The type {\em table\/} implements \Index{associative arrays}, |
| 175 | that is, \Index{arrays} that can be indexed not only with numbers, | 214 | that is, \Index{arrays} that can be indexed not only with numbers, |
| 176 | but with any value (except \nil). | 215 | but with any value (except \nil). |
| 177 | Therefore, this type may be used not only to represent ordinary arrays, | 216 | Therefore, this type may be used not only to represent ordinary arrays, |
| @@ -186,12 +225,12 @@ The form \verb't:f(x)' is syntactic sugar for \verb't.f(t,x)', | |||
| 186 | which calls the method \verb'f' from the table \verb't' passing | 225 | which calls the method \verb'f' from the table \verb't' passing |
| 187 | itself as the first parameter. | 226 | itself as the first parameter. |
| 188 | 227 | ||
| 189 | It is important to notice that tables are objects, and not values. | 228 | It is important to notice that tables are {\em objects}, and not values. |
| 190 | Variables cannot contain tables, only references to them. | 229 | Variables cannot contain tables, only {\em references\/} to them. |
| 191 | Assignment, parameter passing and returns always manipulate references | 230 | Assignment, parameter passing and returns always manipulate references |
| 192 | to tables, and do not imply any kind of copy. | 231 | to tables, and do not imply any kind of copy. |
| 193 | Moreover, tables must be explicitly created before used | 232 | Moreover, tables must be explicitly created before used |
| 194 | (\see{tableconstructor}). | 233 | \see{tableconstructor}. |
| 195 | 234 | ||
| 196 | 235 | ||
| 197 | 236 | ||
| @@ -227,13 +266,15 @@ Literal strings can also be delimited by matching \verb'[[ ... ]]'. | |||
| 227 | Literals in this bracketed form may run for several lines, | 266 | Literals in this bracketed form may run for several lines, |
| 228 | may contain nested \verb'[[ ... ]]' pairs, | 267 | may contain nested \verb'[[ ... ]]' pairs, |
| 229 | and do not interpret escape sequences. | 268 | and do not interpret escape sequences. |
| 269 | This form is specially convenient for | ||
| 270 | handling text that has quoted strings in it. | ||
| 230 | 271 | ||
| 231 | \Index{Comments} start anywhere outside a string with a | 272 | \Index{Comments} start anywhere outside a string with a |
| 232 | double hyphen (\verb'--') and run until the end of the line. | 273 | double hyphen (\verb'--') and run until the end of the line. |
| 233 | Moreover, if the first line of a chunk file starts with \verb'#', | 274 | Moreover, if the first line of a chunk file starts with \verb'#', |
| 234 | this line is skipped% | 275 | this line is skipped% |
| 235 | \footnote{This facility allows the use of Lua as a script interpreter | 276 | \footnote{This facility allows the use of Lua as a script interpreter |
| 236 | in Unix systems.}. | 277 | in Unix systems \see{lua-sa}.}. |
| 237 | 278 | ||
| 238 | \Index{Numerical constants} may be written with an optional decimal part, | 279 | \Index{Numerical constants} may be written with an optional decimal part, |
| 239 | and an optional decimal exponent. | 280 | and an optional decimal exponent. |
| @@ -245,7 +286,7 @@ Examples of valid numerical constants are: | |||
| 245 | 286 | ||
| 246 | \subsection{\Index{Coercion}} \label{coercion} | 287 | \subsection{\Index{Coercion}} \label{coercion} |
| 247 | 288 | ||
| 248 | Lua provides some automatic conversions. | 289 | Lua provides some automatic conversions between values. |
| 249 | Any arithmetic operation applied to a string tries to convert | 290 | Any arithmetic operation applied to a string tries to convert |
| 250 | that string to a number, following the usual rules. | 291 | that string to a number, following the usual rules. |
| 251 | Conversely, whenever a number is used when a string is expected, | 292 | Conversely, whenever a number is used when a string is expected, |
| @@ -254,7 +295,8 @@ if the number is an integer, it is written without exponent or decimal point; | |||
| 254 | otherwise, it is formatted following the \verb'%g' | 295 | otherwise, it is formatted following the \verb'%g' |
| 255 | conversion specification of the \verb'printf' function in the | 296 | conversion specification of the \verb'printf' function in the |
| 256 | standard C library. | 297 | standard C library. |
| 257 | 298 | For complete control on how numbers are converted to strings, | |
| 299 | use the \verb|format| function \see{format}. | ||
| 258 | 300 | ||
| 259 | 301 | ||
| 260 | \subsection{\Index{Adjustment}} \label{adjust} | 302 | \subsection{\Index{Adjustment}} \label{adjust} |
| @@ -273,15 +315,16 @@ Adjustment occurs in multiple assignment and function calls. | |||
| 273 | 315 | ||
| 274 | \subsection{Statements} | 316 | \subsection{Statements} |
| 275 | 317 | ||
| 276 | Lua supports an almost conventional set of \Index{statements}. | 318 | Lua supports an almost conventional set of \Index{statements}, |
| 319 | similar to those in Pascal or C. | ||
| 277 | The conventional commands include | 320 | The conventional commands include |
| 278 | assignment, control structures and procedure calls. | 321 | assignment, control structures and procedure calls. |
| 279 | Non-conventional commands include table constructors | 322 | Non-conventional commands include table constructors |
| 280 | (Section~\ref{tableconstructor}), | 323 | \see{tableconstructor}, |
| 281 | and local variable declarations (Section~\ref{localvar}). | 324 | and local variable declarations \see{localvar}. |
| 282 | 325 | ||
| 283 | \subsubsection{Blocks} | 326 | \subsubsection{Blocks} |
| 284 | A \Index{block} is a list of statements, which is executed sequentially. | 327 | A \Index{block} is a list of statements, which are executed sequentially. |
| 285 | Any statement can be optionally followed by a semicolon: | 328 | Any statement can be optionally followed by a semicolon: |
| 286 | \begin{Produc} | 329 | \begin{Produc} |
| 287 | \produc{block}{\rep{stat sc} \opt{ret}} | 330 | \produc{block}{\rep{stat sc} \opt{ret}} |
| @@ -307,8 +350,8 @@ Therefore, it can be used to exchange two values, as in | |||
| 307 | \begin{verbatim} | 350 | \begin{verbatim} |
| 308 | x, y = y, x | 351 | x, y = y, x |
| 309 | \end{verbatim} | 352 | \end{verbatim} |
| 310 | Before the assignment, the list of values is {\em adjusted} to | 353 | Before the assignment, the list of values is {\em adjusted\/} to |
| 311 | the length of the list of variables (\see{adjust}). | 354 | the length of the list of variables \see{adjust}. |
| 312 | 355 | ||
| 313 | A single name can denote a global or a local variable, | 356 | A single name can denote a global or a local variable, |
| 314 | or a formal parameter: | 357 | or a formal parameter: |
| @@ -321,21 +364,21 @@ Square brackets are used to index a table: | |||
| 321 | \end{Produc}% | 364 | \end{Produc}% |
| 322 | If \verb'var' results in a table value, | 365 | If \verb'var' results in a table value, |
| 323 | the field indexed by the expression value gets the assigned value. | 366 | the field indexed by the expression value gets the assigned value. |
| 324 | Otherwise, the fallback {\em settable} is called, | 367 | Otherwise, the fallback {\em settable\/} is called, |
| 325 | with three parameters: the value of \verb'var', | 368 | with three parameters: the value of \verb'var', |
| 326 | the value of expression, and the value being assigned to it; | 369 | the value of expression, and the value being assigned to it; |
| 327 | \see{fallback}. | 370 | \see{fallback}. |
| 328 | 371 | ||
| 329 | The syntax \verb'var.NAME' is just syntactic sugar for | 372 | The syntax \verb'var.NAME' is just syntactic sugar for |
| 330 | \verb'var["NAME"]'. | 373 | \verb'var["NAME"]': |
| 331 | \begin{Produc} | 374 | \begin{Produc} |
| 332 | \produc{var}{var \ter{.} name} | 375 | \produc{var}{var \ter{.} name} |
| 333 | \end{Produc}% | 376 | \end{Produc}% |
| 334 | 377 | ||
| 335 | \subsubsection{Control Structures} | 378 | \subsubsection{Control Structures} |
| 336 | The \Index{condition expression} of a control structure can return any value. | 379 | The \Index{condition expression} of a control structure may return any value. |
| 337 | All values different from \nil\ are considered true; | 380 | All values different from \nil\ are considered true; |
| 338 | \nil\ is considered false. | 381 | only \nil\ is considered false. |
| 339 | {\tt if}'s, {\tt while}'s and {\tt repeat}'s have the usual meaning. | 382 | {\tt if}'s, {\tt while}'s and {\tt repeat}'s have the usual meaning. |
| 340 | 383 | ||
| 341 | \index{while-do}\index{repeat-until}\index{if-then-else} | 384 | \index{while-do}\index{repeat-until}\index{if-then-else} |
| @@ -361,11 +404,11 @@ function calls can be executed as statements: | |||
| 361 | \begin{Produc} | 404 | \begin{Produc} |
| 362 | \produc{stat}{functioncall} | 405 | \produc{stat}{functioncall} |
| 363 | \end{Produc}% | 406 | \end{Produc}% |
| 364 | Eventual returned values are thrown away. | 407 | In this case, returned values are thrown away. |
| 365 | Function calls are explained in Section \ref{functioncall}. | 408 | Function calls are explained in Section~\ref{functioncall}. |
| 366 | 409 | ||
| 367 | \subsubsection{Local Declarations} \label{localvar} | 410 | \subsubsection{Local Declarations} \label{localvar} |
| 368 | \Index{Local variables} can be declared anywhere inside a block. | 411 | \Index{Local variables} may be declared anywhere inside a block. |
| 369 | Their scope begins after the declaration and lasts until the | 412 | Their scope begins after the declaration and lasts until the |
| 370 | end of the block. | 413 | end of the block. |
| 371 | The declaration may include an initial assignment: | 414 | The declaration may include an initial assignment: |
| @@ -397,16 +440,19 @@ Variables are explained in Section~\ref{assignment}. | |||
| 397 | \subsubsection{Arithmetic Operators} | 440 | \subsubsection{Arithmetic Operators} |
| 398 | Lua supports the usual \Index{arithmetic operators}. | 441 | Lua supports the usual \Index{arithmetic operators}. |
| 399 | These operators are the binary | 442 | These operators are the binary |
| 400 | \verb'+', \verb'-', \verb'*', \verb'/' and \verb'^' (exponentiation), | 443 | \verb'+' (addition), |
| 401 | and the unary \verb'-'. | 444 | \verb'-' (subtraction), |
| 445 | \verb'*' (multiplication), | ||
| 446 | \verb'/' (division) and \verb'^' (exponentiation), | ||
| 447 | and the unary \verb'-' (negation). | ||
| 402 | If the operands are numbers, or strings that can be converted to | 448 | If the operands are numbers, or strings that can be converted to |
| 403 | numbers, according to the rules given in Section \ref{coercion}, | 449 | numbers, according to the rules given in Section~\ref{coercion}, |
| 404 | then all operations but exponentiation have the usual meaning. | 450 | then all operations except exponentiation have the usual meaning. |
| 405 | Otherwise, the fallback ``arith'' is called (\see{fallback}). | 451 | Otherwise, the fallback ``arith'' is called \see{fallback}. |
| 406 | An exponentiation always calls this fallback. | 452 | An exponentiation always calls this fallback. |
| 407 | The standard mathematical library redefines this fallback, | 453 | The standard mathematical library redefines this fallback, |
| 408 | giving the expected meaning to \Index{exponentiation} | 454 | giving the expected meaning to \Index{exponentiation} |
| 409 | (\see{mathlib}). | 455 | \see{mathlib}. |
| 410 | 456 | ||
| 411 | \subsubsection{Relational Operators} | 457 | \subsubsection{Relational Operators} |
| 412 | Lua provides the following \Index{relational operators}: | 458 | Lua provides the following \Index{relational operators}: |
| @@ -423,12 +469,18 @@ Numbers and strings are compared in the usual way. | |||
| 423 | Tables, CFunctions, and functions are compared by reference, | 469 | Tables, CFunctions, and functions are compared by reference, |
| 424 | that is, two tables are considered equal only if they are the same table. | 470 | that is, two tables are considered equal only if they are the same table. |
| 425 | The operator \verb'~=' is exactly the negation of equality (\verb'=='). | 471 | The operator \verb'~=' is exactly the negation of equality (\verb'=='). |
| 472 | Note that the conversion rules of Section~\ref{coercion} | ||
| 473 | do not apply to equality comparisons. | ||
| 474 | Thus, \verb|"0"==0| evaluates to false. | ||
| 426 | 475 | ||
| 427 | The other operators work as follows. | 476 | The other operators work as follows. |
| 428 | If both arguments are numbers, then they are compared as such. | 477 | If both arguments are numbers, then they are compared as such. |
| 429 | Otherwise, if both arguments can be converted to strings, | 478 | Otherwise, if both arguments can be converted to strings, |
| 430 | their values are compared using lexicographical order. | 479 | their values are compared using lexicographical order. |
| 431 | Otherwise, the ``order'' fallback is called (\see{fallback}). | 480 | Otherwise, the ``order'' fallback is called \see{fallback}. |
| 481 | %Note that the conversion rules of Section~\ref{coercion} | ||
| 482 | %do apply to order operators. | ||
| 483 | %Thus, \verb|"2">"12"| evaluates to true. | ||
| 432 | 484 | ||
| 433 | \subsubsection{Logical Operators} | 485 | \subsubsection{Logical Operators} |
| 434 | Like control structures, all logical operators | 486 | Like control structures, all logical operators |
| @@ -451,8 +503,8 @@ the second operand is evaluated only if necessary. | |||
| 451 | Lua offers a string \Index{concatenation} operator, | 503 | Lua offers a string \Index{concatenation} operator, |
| 452 | denoted by ``\IndexVerb{..}''. | 504 | denoted by ``\IndexVerb{..}''. |
| 453 | If operands are strings or numbers, then they are converted to | 505 | If operands are strings or numbers, then they are converted to |
| 454 | strings according to the rules in Section \ref{coercion}. | 506 | strings according to the rules in Section~\ref{coercion}. |
| 455 | Otherwise, the fallback ``concat'' is called (\see{fallback}). | 507 | Otherwise, the fallback ``concat'' is called \see{fallback}. |
| 456 | 508 | ||
| 457 | \subsubsection{Precedence} | 509 | \subsubsection{Precedence} |
| 458 | \Index{Operator precedence} follows the table below, | 510 | \Index{Operator precedence} follows the table below, |
| @@ -484,17 +536,17 @@ The general syntax for constructors is: | |||
| 484 | \produc{ffieldlist}{\opt{ffieldlist1}} | 536 | \produc{ffieldlist}{\opt{ffieldlist1}} |
| 485 | \end{Produc} | 537 | \end{Produc} |
| 486 | 538 | ||
| 487 | The form {\em lfieldlist1} is used to initialize lists. | 539 | The form {\em lfieldlist1\/} is used to initialize lists. |
| 488 | \begin{Produc} | 540 | \begin{Produc} |
| 489 | \produc{lfieldlist1}{exp \rep{\ter{,} exp} \opt{\ter{,}}} | 541 | \produc{lfieldlist1}{exp \rep{\ter{,} exp} \opt{\ter{,}}} |
| 490 | \end{Produc}% | 542 | \end{Produc}% |
| 491 | The expressions in the list are assigned to consecutive numerical indexes, | 543 | The expressions in the list are assigned to consecutive numerical indices, |
| 492 | starting with 1. | 544 | starting with 1. |
| 493 | For example: | 545 | For example: |
| 494 | \begin{verbatim} | 546 | \begin{verbatim} |
| 495 | a = {"v1", "v2", 34} | 547 | a = {"v1", "v2", 34} |
| 496 | \end{verbatim} | 548 | \end{verbatim} |
| 497 | is roughly equivalent to: | 549 | is essentialy equivalent to: |
| 498 | \begin{verbatim} | 550 | \begin{verbatim} |
| 499 | temp = {} | 551 | temp = {} |
| 500 | temp[1] = "v1" | 552 | temp[1] = "v1" |
| @@ -512,7 +564,7 @@ For example: | |||
| 512 | \begin{verbatim} | 564 | \begin{verbatim} |
| 513 | a = {x = 1, y = 3} | 565 | a = {x = 1, y = 3} |
| 514 | \end{verbatim} | 566 | \end{verbatim} |
| 515 | is roughly equivalent to: | 567 | is essentialy equivalent to: |
| 516 | \begin{verbatim} | 568 | \begin{verbatim} |
| 517 | temp = {} | 569 | temp = {} |
| 518 | temp.x = 1 -- or temp["x"] = 1 | 570 | temp.x = 1 -- or temp["x"] = 1 |
| @@ -527,7 +579,7 @@ A \Index{function call} has the following syntax: | |||
| 527 | \produc{functioncall}{var realParams} | 579 | \produc{functioncall}{var realParams} |
| 528 | \end{Produc}% | 580 | \end{Produc}% |
| 529 | Here, \verb'var' can be any variable (global, local, indexed, etc). | 581 | Here, \verb'var' can be any variable (global, local, indexed, etc). |
| 530 | If its value has type {\em function\/} or {\em CFunction\/}, | 582 | If its value has type {\em function\/} or {\em CFunction}, |
| 531 | then this function is called. | 583 | then this function is called. |
| 532 | Otherwise, the ``function'' fallback is called, | 584 | Otherwise, the ``function'' fallback is called, |
| 533 | having as first parameter the value of \verb'var', | 585 | having as first parameter the value of \verb'var', |
| @@ -552,20 +604,23 @@ except that \verb'var' is evaluated only once. | |||
| 552 | \end{Produc}% | 604 | \end{Produc}% |
| 553 | All argument expressions are evaluated before the call; | 605 | All argument expressions are evaluated before the call; |
| 554 | then the list of \Index{arguments} is adjusted to | 606 | then the list of \Index{arguments} is adjusted to |
| 555 | the length of the list of parameters (\see{adjust}); | 607 | the length of the list of parameters \see{adjust}; |
| 556 | finally, this list is assigned to the formal parameters. | 608 | finally, this list is assigned to the formal parameters. |
| 557 | A call of the form \verb'f{...}' is syntactic sugar for | 609 | A call of the form \verb'f{...}' is syntactic sugar for |
| 558 | \verb'f({...})', that is, | 610 | \verb'f({...})', that is, |
| 559 | the parameter list is a single new table. | 611 | the parameter list is a single new table. |
| 560 | 612 | ||
| 561 | Because a function can return any number of results | 613 | Because a function can return any number of results |
| 562 | (\see{return}), | 614 | \see{return}, |
| 563 | the number of results must be adjusted before used. | 615 | the number of results must be adjusted before used. |
| 564 | If the function is called as a statement (\see{funcstat}), | 616 | If the function is called as a statement \see{funcstat}, |
| 565 | its return list is adjusted to 0. | 617 | its return list is adjusted to 0, |
| 618 | thus discarding all returned values. | ||
| 566 | If the function is called in a place that needs a single value | 619 | If the function is called in a place that needs a single value |
| 567 | (syntactically denoted by the non-terminal \verb'exp1'), | 620 | (syntactically denoted by the non-terminal \verb'exp1'), |
| 568 | then its return list is adjusted to 1. | 621 | then its return list is adjusted to 1, |
| 622 | thus discarding all returned values, | ||
| 623 | except the first one. | ||
| 569 | If the function is called in a place that can hold many values | 624 | If the function is called in a place that can hold many values |
| 570 | (syntactically denoted by the non-terminal \verb'exp'), | 625 | (syntactically denoted by the non-terminal \verb'exp'), |
| 571 | then no adjustment is made. | 626 | then no adjustment is made. |
| @@ -585,6 +640,8 @@ all its function bodies are pre-compiled, too. | |||
| 585 | Then, when Lua ``executes'' the function definition, | 640 | Then, when Lua ``executes'' the function definition, |
| 586 | its body is stored, with type {\em function}, | 641 | its body is stored, with type {\em function}, |
| 587 | into the variable \verb'var'. | 642 | into the variable \verb'var'. |
| 643 | It is in this sense that | ||
| 644 | a function definition is an assignment to a global variable. | ||
| 588 | 645 | ||
| 589 | Parameters act as local variables, | 646 | Parameters act as local variables, |
| 590 | initialized with the argument values. | 647 | initialized with the argument values. |
| @@ -592,7 +649,7 @@ initialized with the argument values. | |||
| 592 | \produc{parlist1}{name \rep{\ter{,} name}} | 649 | \produc{parlist1}{name \rep{\ter{,} name}} |
| 593 | \end{Produc} | 650 | \end{Produc} |
| 594 | 651 | ||
| 595 | Results are returned using the \verb'return' statement (\see{return}). | 652 | Results are returned using the \verb'return' statement \see{return}. |
| 596 | If control reaches the end of a function without a return instruction, | 653 | If control reaches the end of a function without a return instruction, |
| 597 | then the function returns with no results. | 654 | then the function returns with no results. |
| 598 | 655 | ||
| @@ -631,9 +688,9 @@ identified by the given strings: | |||
| 631 | \begin{description} | 688 | \begin{description} |
| 632 | \item[``arith'':]\index{arithmetic fallback} | 689 | \item[``arith'':]\index{arithmetic fallback} |
| 633 | called when an arithmetic operation is applied to non numerical operands, | 690 | called when an arithmetic operation is applied to non numerical operands, |
| 634 | or when the binary \verb'^' operation is called. | 691 | or when the binary \verb'^' operation (exponentiation) is called. |
| 635 | It receives three arguments: | 692 | It receives three arguments: |
| 636 | the two operands (the second one is nil when the operation is unary minus) | 693 | the two operands (the second one is \nil\ when the operation is unary minus) |
| 637 | and one of the following strings describing the offended operator: | 694 | and one of the following strings describing the offended operator: |
| 638 | \begin{verbatim} | 695 | \begin{verbatim} |
| 639 | add sub mul div pow unm | 696 | add sub mul div pow unm |
| @@ -661,20 +718,20 @@ called when Lua tries to retrieve the value of an index | |||
| 661 | not present in a table. | 718 | not present in a table. |
| 662 | It receives as arguments the table and the index. | 719 | It receives as arguments the table and the index. |
| 663 | Its return value is the final result of the indexing operation. | 720 | Its return value is the final result of the indexing operation. |
| 664 | The default handler returns nil. | 721 | The default handler returns \nil. |
| 665 | \item[``getglobal'':]\index{index getglobal} | 722 | \item[``getglobal'':]\index{index getglobal} |
| 666 | called when Lua tries to retrieve the value of a global variable | 723 | called when Lua tries to retrieve the value of a global variable |
| 667 | which has a nil value (or which has not been initialized). | 724 | which has a \nil\ value (or which has not been initialized). |
| 668 | It receives as argument the name of the variable. | 725 | It receives as argument the name of the variable. |
| 669 | Its return value is the final result of the expression. | 726 | Its return value is the final result of the expression. |
| 670 | The default handler returns nil. | 727 | The default handler returns \nil. |
| 671 | \item[``gettable'':]\index{gettable fallback} | 728 | \item[``gettable'':]\index{gettable fallback} |
| 672 | called when Lua tries to index a non table value. | 729 | called when Lua tries to index a non table value. |
| 673 | It receives as arguments the non table value and the index. | 730 | It receives as arguments the non table value and the index. |
| 674 | Its return value is the final result of the indexing operation. | 731 | Its return value is the final result of the indexing operation. |
| 675 | The default handler issues an error. | 732 | The default handler issues an error. |
| 676 | \item[``settable'':]\index{settable fallback} | 733 | \item[``settable'':]\index{settable fallback} |
| 677 | called when Lua tries to assign indexed a non table value. | 734 | called when Lua tries to assign to an index in a non table value. |
| 678 | It receives as arguments the non table value, | 735 | It receives as arguments the non table value, |
| 679 | the index, and the assigned value. | 736 | the index, and the assigned value. |
| 680 | The default handler issues an error. | 737 | The default handler issues an error. |
| @@ -687,7 +744,8 @@ The default handler issues an error. | |||
| 687 | \item[``gc'':] | 744 | \item[``gc'':] |
| 688 | called during garbage collection. | 745 | called during garbage collection. |
| 689 | It receives as argument the table being collected. | 746 | It receives as argument the table being collected. |
| 690 | After each run of the collector this function is called with argument nil. | 747 | After each run of the collector this function is called with argument \nil, |
| 748 | to signal the completion of the garbage collection. | ||
| 691 | Because this function operates during garbage collection, | 749 | Because this function operates during garbage collection, |
| 692 | it must be used with great care, | 750 | it must be used with great care, |
| 693 | and programmers should avoid the creation of new objects | 751 | and programmers should avoid the creation of new objects |
| @@ -696,7 +754,8 @@ The default handler does nothing. | |||
| 696 | \item[``error'':]\index{error fallback} | 754 | \item[``error'':]\index{error fallback} |
| 697 | called when an error occurs. | 755 | called when an error occurs. |
| 698 | It receives as argument a string describing the error. | 756 | It receives as argument a string describing the error. |
| 699 | The default handler prints the message on the standard error output. | 757 | The default handler prints the message on the standard error output |
| 758 | (\verb|stderr|). | ||
| 700 | \end{description} | 759 | \end{description} |
| 701 | 760 | ||
| 702 | The function \IndexVerb{setfallback} is used to change a fallback handler. | 761 | The function \IndexVerb{setfallback} is used to change a fallback handler. |
| @@ -704,7 +763,7 @@ Its first argument is the name of a fallback condition, | |||
| 704 | and the second argument is the new function to be called. | 763 | and the second argument is the new function to be called. |
| 705 | It returns the old handler function for the given fallback. | 764 | It returns the old handler function for the given fallback. |
| 706 | 765 | ||
| 707 | Section \ref{exfallback} shows an example of the use of fallbacks. | 766 | Section~\ref{exfallback} shows an example of the use of fallbacks. |
| 708 | 767 | ||
| 709 | 768 | ||
| 710 | \subsection{Error Handling} \label{error} | 769 | \subsection{Error Handling} \label{error} |
| @@ -712,7 +771,7 @@ Section \ref{exfallback} shows an example of the use of fallbacks. | |||
| 712 | Because Lua is an extension language, | 771 | Because Lua is an extension language, |
| 713 | all Lua actions start from C code calling a function from the Lua library. | 772 | all Lua actions start from C code calling a function from the Lua library. |
| 714 | Whenever an error occurs during Lua compilation or execution, | 773 | Whenever an error occurs during Lua compilation or execution, |
| 715 | an ``error'' fallback function is called, | 774 | the ``error'' fallback function is called, |
| 716 | and then the corresponding function from the library | 775 | and then the corresponding function from the library |
| 717 | (\verb'lua_dofile', \verb'lua_dostring', | 776 | (\verb'lua_dofile', \verb'lua_dostring', |
| 718 | \verb'lua_call', or \verb'lua_callfunction') | 777 | \verb'lua_call', or \verb'lua_callfunction') |
| @@ -721,21 +780,21 @@ is terminated returning an error condition. | |||
| 721 | The only argument to the ``error'' fallback function is a string | 780 | The only argument to the ``error'' fallback function is a string |
| 722 | describing the error. | 781 | describing the error. |
| 723 | The standard I/O library redefines this fallback, | 782 | The standard I/O library redefines this fallback, |
| 724 | using the debug facilities (\see{debugI}), | 783 | using the debug facilities \see{debugI}, |
| 725 | in order to print some extra information, | 784 | in order to print some extra information, |
| 726 | like the call stack. | 785 | like the call stack. |
| 727 | For more information about an error, | 786 | To provide more information about errors, |
| 728 | the Lua program can include the compilation pragma \verb'$debug'. | 787 | Lua programs can include the compilation pragma \verb'$debug'. |
| 729 | \index{debug pragma}\label{pragma} | 788 | \index{debug pragma}\label{pragma} |
| 730 | This pragma must be written in a line by itself. | 789 | This pragma must be written in a line by itself. |
| 731 | When an error occurs in a program compiled with this option, | 790 | When an error occurs in a program compiled with this option, |
| 732 | the error routine is able to print also the lines where the calls | 791 | the error routine is able to print the number of the lines where the calls |
| 733 | (and the error) were made. | 792 | (and the error) were made. |
| 734 | If needed, it is possible to change the ``error'' fallback handler | 793 | If needed, it is possible to change the ``error'' fallback handler |
| 735 | (\see{fallback}). | 794 | \see{fallback}. |
| 736 | 795 | ||
| 737 | Lua code can explicitly generate an error by calling the built-in | 796 | Lua code can explicitly generate an error by calling the built-in |
| 738 | function \verb'error' (\see{pdf-error}). | 797 | function \verb'error' \see{pdf-error}. |
| 739 | 798 | ||
| 740 | 799 | ||
| 741 | \section{The Application Program Interface} | 800 | \section{The Application Program Interface} |
| @@ -750,12 +809,13 @@ The API functions can be classified in the following categories: | |||
| 750 | \item manipulating (reading and writing) Lua objects; | 809 | \item manipulating (reading and writing) Lua objects; |
| 751 | \item calling Lua functions; | 810 | \item calling Lua functions; |
| 752 | \item C functions to be called by Lua; | 811 | \item C functions to be called by Lua; |
| 753 | \item references to Lua Objects. | 812 | \item manipulating references to Lua Objects. |
| 754 | \end{enumerate} | 813 | \end{enumerate} |
| 755 | All API functions are declared in the header file \verb'lua.h'. | 814 | All API functions and related types and constants |
| 815 | are declared in the header file \verb'lua.h'. | ||
| 756 | 816 | ||
| 757 | \subsection{Executing Lua Code} | 817 | \subsection{Executing Lua Code} |
| 758 | A host program can execute Lua chunks written in a file or in a string, | 818 | A host program can execute Lua chunks written in a file or in a string |
| 759 | using the following functions: | 819 | using the following functions: |
| 760 | \Deffunc{lua_dofile}\Deffunc{lua_dostring} | 820 | \Deffunc{lua_dofile}\Deffunc{lua_dostring} |
| 761 | \begin{verbatim} | 821 | \begin{verbatim} |
| @@ -766,8 +826,8 @@ Both functions return an error code: | |||
| 766 | 0, in case of success; non zero, in case of errors. | 826 | 0, in case of success; non zero, in case of errors. |
| 767 | More specifically, \verb'lua_dofile' returns 2 if for any reason | 827 | More specifically, \verb'lua_dofile' returns 2 if for any reason |
| 768 | it could not open the file. | 828 | it could not open the file. |
| 769 | The function \verb'lua_dofile', if called with argument \verb'NULL' (0), | 829 | The function \verb'lua_dofile', if called with argument \verb'NULL', |
| 770 | executes the {\tt stdin} stream. | 830 | executes the \verb|stdin| stream. |
| 771 | Function \verb'lua_dofile' is also able to execute pre-compiled chunks. | 831 | Function \verb'lua_dofile' is also able to execute pre-compiled chunks. |
| 772 | It automatically detects whether the file is text or binary, | 832 | It automatically detects whether the file is text or binary, |
| 773 | and loads it accordingly (see program \IndexVerb{luac}). | 833 | and loads it accordingly (see program \IndexVerb{luac}). |
| @@ -779,14 +839,14 @@ all values passed between Lua and C have type | |||
| 779 | which works like an abstract type in C that can hold any Lua value. | 839 | which works like an abstract type in C that can hold any Lua value. |
| 780 | Values of type \verb'lua_Object' have no meaning outside Lua; | 840 | Values of type \verb'lua_Object' have no meaning outside Lua; |
| 781 | for instance, | 841 | for instance, |
| 782 | the comparisson of two \verb"lua_Object's" is of no significance. | 842 | the comparisson of two \verb"lua_Object's" is undefined. |
| 783 | 843 | ||
| 784 | Because Lua has automatic memory management and garbage collection, | 844 | Because Lua has automatic memory management and garbage collection, |
| 785 | a \verb'lua_Object' has a limited scope, | 845 | a \verb'lua_Object' has a limited scope, |
| 786 | and is only valid inside the {\em block\/} where it was created. | 846 | and is only valid inside the {\em block\/} where it was created. |
| 787 | A C function called from Lua is a block, | 847 | A C function called from Lua is a block, |
| 788 | and its parameters are valid only until its end. | 848 | and its parameters are valid only until its end. |
| 789 | A good programming practice is to convert Lua objects to C values | 849 | It is good programming practice to convert Lua objects to C values |
| 790 | as soon as they are available, | 850 | as soon as they are available, |
| 791 | and never to store \verb'lua_Object's in C global variables. | 851 | and never to store \verb'lua_Object's in C global variables. |
| 792 | 852 | ||
| @@ -801,6 +861,7 @@ void lua_endblock (void); | |||
| 801 | \end{verbatim} | 861 | \end{verbatim} |
| 802 | After the end of the block, | 862 | After the end of the block, |
| 803 | all \verb'lua_Object''s created inside it are released. | 863 | all \verb'lua_Object''s created inside it are released. |
| 864 | The use of explicit nested blocks is encouraged. | ||
| 804 | 865 | ||
| 805 | To check the type of a \verb'lua_Object', | 866 | To check the type of a \verb'lua_Object', |
| 806 | the following function is available: | 867 | the following function is available: |
| @@ -824,7 +885,8 @@ int lua_isuserdata (lua_Object object); | |||
| 824 | All macros return 1 if the object is compatible with the given type, | 885 | All macros return 1 if the object is compatible with the given type, |
| 825 | and 0 otherwise. | 886 | and 0 otherwise. |
| 826 | The function \verb'lua_isnumber' accepts numbers and numerical strings, | 887 | The function \verb'lua_isnumber' accepts numbers and numerical strings, |
| 827 | \verb'lua_isstring' accepts strings and numbers (\see{coercion}), | 888 | whereas |
| 889 | \verb'lua_isstring' accepts strings and numbers \see{coercion}, | ||
| 828 | and \verb'lua_isfunction' accepts Lua and C functions. | 890 | and \verb'lua_isfunction' accepts Lua and C functions. |
| 829 | The function \verb'lua_type' can be used to distinguish between | 891 | The function \verb'lua_type' can be used to distinguish between |
| 830 | different kinds of user data. | 892 | different kinds of user data. |
| @@ -839,13 +901,13 @@ char *lua_getstring (lua_Object object); | |||
| 839 | lua_CFunction lua_getcfunction (lua_Object object); | 901 | lua_CFunction lua_getcfunction (lua_Object object); |
| 840 | void *lua_getuserdata (lua_Object object); | 902 | void *lua_getuserdata (lua_Object object); |
| 841 | \end{verbatim} | 903 | \end{verbatim} |
| 842 | \verb'lua_getnumber' converts a \verb'lua_Object' to a float. | 904 | \verb'lua_getnumber' converts a \verb'lua_Object' to a floating-point number. |
| 843 | This \verb'lua_Object' must be a number or a string convertible to number | 905 | This \verb'lua_Object' must be a number or a string convertible to number |
| 844 | (\see{coercion}); otherwise, the function returns 0. | 906 | \see{coercion}; otherwise, the function returns 0. |
| 845 | 907 | ||
| 846 | \verb'lua_getstring' converts a \verb'lua_Object' to a string (\verb'char *'). | 908 | \verb'lua_getstring' converts a \verb'lua_Object' to a string (\verb'char *'). |
| 847 | This \verb'lua_Object' must be a string or a number; | 909 | This \verb'lua_Object' must be a string or a number; |
| 848 | otherwise, the function returns 0 (the null pointer). | 910 | otherwise, the function returns 0 (the \verb|NULL| pointer). |
| 849 | This function does not create a new string, but returns a pointer to | 911 | This function does not create a new string, but returns a pointer to |
| 850 | a string inside the Lua environment. | 912 | a string inside the Lua environment. |
| 851 | Because Lua has garbage collection, there is no guarantee that such | 913 | Because Lua has garbage collection, there is no guarantee that such |
| @@ -853,12 +915,12 @@ pointer will be valid after the block ends. | |||
| 853 | 915 | ||
| 854 | \verb'lua_getcfunction' converts a \verb'lua_Object' to a C function. | 916 | \verb'lua_getcfunction' converts a \verb'lua_Object' to a C function. |
| 855 | This \verb'lua_Object' must have type {\em CFunction\/}; | 917 | This \verb'lua_Object' must have type {\em CFunction\/}; |
| 856 | otherwise, the function returns 0 (the null pointer). | 918 | otherwise, the function returns 0 (the \verb|NULL| pointer). |
| 857 | The type \verb'lua_CFunction' is explained in Section~\ref{LuacallC}. | 919 | The type \verb'lua_CFunction' is explained in Section~\ref{LuacallC}. |
| 858 | 920 | ||
| 859 | \verb'lua_getuserdata' converts a \verb'lua_Object' to \verb'void*'. | 921 | \verb'lua_getuserdata' converts a \verb'lua_Object' to \verb'void*'. |
| 860 | This \verb'lua_Object' must have type {\em userdata\/}; | 922 | This \verb'lua_Object' must have type {\em userdata\/}; |
| 861 | otherwise, the function returns 0 (the null pointer). | 923 | otherwise, the function returns 0 (the \verb|NULL| pointer). |
| 862 | 924 | ||
| 863 | The reverse process, that is, passing a specific C value to Lua, | 925 | The reverse process, that is, passing a specific C value to Lua, |
| 864 | is done by using the following functions: | 926 | is done by using the following functions: |
| @@ -882,7 +944,7 @@ where it can be assigned to a Lua variable, | |||
| 882 | passed as parameter to a Lua function, etc. \label{pushing} | 944 | passed as parameter to a Lua function, etc. \label{pushing} |
| 883 | 945 | ||
| 884 | User data can have different tags, | 946 | User data can have different tags, |
| 885 | whose semantics are defined by the host program. | 947 | whose semantics are only known to the host program. |
| 886 | Any positive integer can be used to tag a user datum. | 948 | Any positive integer can be used to tag a user datum. |
| 887 | When a user datum is retrieved, | 949 | When a user datum is retrieved, |
| 888 | the function \verb'lua_type' can be used to get its tag. | 950 | the function \verb'lua_type' can be used to get its tag. |
| @@ -927,29 +989,31 @@ or the index is not present in the table, | |||
| 927 | the corresponding fallback is called. | 989 | the corresponding fallback is called. |
| 928 | 990 | ||
| 929 | To store a value in an index, | 991 | To store a value in an index, |
| 930 | the program must push onto the stack the table, the index, | 992 | the program must push the table, the index, and the value onto the stack, |
| 931 | and the value, | ||
| 932 | and then call the function: | 993 | and then call the function: |
| 933 | \Deffunc{lua_storesubscript} | 994 | \Deffunc{lua_storesubscript} |
| 934 | \begin{verbatim} | 995 | \begin{verbatim} |
| 935 | void lua_storesubscript (void); | 996 | void lua_storesubscript (void); |
| 936 | \end{verbatim} | 997 | \end{verbatim} |
| 937 | Again, the corresponding fallback is called if needed. | 998 | Again, the ``settable'' fallback is called if a non-table value is used. |
| 938 | 999 | ||
| 939 | Finally, the function | 1000 | Finally, the function |
| 940 | \Deffunc{lua_createtable} | 1001 | \Deffunc{lua_createtable} |
| 941 | \begin{verbatim} | 1002 | \begin{verbatim} |
| 942 | lua_Object lua_createtable (void); | 1003 | lua_Object lua_createtable (void); |
| 943 | \end{verbatim} | 1004 | \end{verbatim} |
| 944 | creates and returns a new table. | 1005 | creates and returns a new, empty table. |
| 945 | 1006 | ||
| 946 | {\em Please Notice:\/} | 1007 | \begin{quotation} |
| 1008 | \noindent | ||
| 1009 | {\em Please note\/}: | ||
| 947 | Most functions from the Lua library receive parameters through Lua's stack. | 1010 | Most functions from the Lua library receive parameters through Lua's stack. |
| 948 | Because other functions also use this stack, | 1011 | Because other functions also use this stack, |
| 949 | it is important that these | 1012 | it is important that these |
| 950 | parameters be pushed just before the corresponding call, | 1013 | parameters be pushed just before the corresponding call, |
| 951 | without intermediate calls to the Lua library. | 1014 | without intermediate calls to the Lua library. |
| 952 | For instance, suppose the user wants the value of \verb'a[i]'. | 1015 | For instance, suppose the user wants the value of \verb'a[i]', |
| 1016 | where \verb'a' and \verb'i' are global Lua variables. | ||
| 953 | A simplistic solution would be: | 1017 | A simplistic solution would be: |
| 954 | \begin{verbatim} | 1018 | \begin{verbatim} |
| 955 | /* Warning: WRONG CODE */ | 1019 | /* Warning: WRONG CODE */ |
| @@ -958,7 +1022,8 @@ A simplistic solution would be: | |||
| 958 | lua_pushobject(lua_getglobal("i")); /* push index */ | 1022 | lua_pushobject(lua_getglobal("i")); /* push index */ |
| 959 | result = lua_getsubscript(); | 1023 | result = lua_getsubscript(); |
| 960 | \end{verbatim} | 1024 | \end{verbatim} |
| 961 | However, the call \verb'lua_getglobal("i")' modifies the stack, | 1025 | This code is incorrect because |
| 1026 | the call \verb'lua_getglobal("i")' modifies the stack, | ||
| 962 | and invalidates the previous pushed value. | 1027 | and invalidates the previous pushed value. |
| 963 | A correct solution could be: | 1028 | A correct solution could be: |
| 964 | \begin{verbatim} | 1029 | \begin{verbatim} |
| @@ -968,17 +1033,18 @@ A correct solution could be: | |||
| 968 | lua_pushobject(index); /* push index */ | 1033 | lua_pushobject(index); /* push index */ |
| 969 | result = lua_getsubscript(); | 1034 | result = lua_getsubscript(); |
| 970 | \end{verbatim} | 1035 | \end{verbatim} |
| 971 | The functions \verb|lua_getnumber|, \verb|lua_getstring|, | 1036 | The functions {\em lua\_getnumber}, {\em lua\_getstring}, |
| 972 | \verb|lua_getuserdata|, and \verb|lua_getcfunction|, | 1037 | {\em lua\_getuserdata}, and {\em lua\_getcfunction}, |
| 973 | plus the family \verb|lua_is*|, | 1038 | plus the family \verb|lua_is*|, |
| 974 | are safe to be called without modifying the stack. | 1039 | are safe to be called without modifying the stack. |
| 1040 | \end{quotation} | ||
| 975 | 1041 | ||
| 976 | \subsection{Calling Lua Functions} | 1042 | \subsection{Calling Lua Functions} |
| 977 | Functions defined in Lua by a chunk executed with | 1043 | Functions defined in Lua by a chunk executed with |
| 978 | \verb'dofile' or \verb'dostring' can be called from the host program. | 1044 | \verb'dofile' or \verb'dostring' can be called from the host program. |
| 979 | This is done using the following protocol: | 1045 | This is done using the following protocol: |
| 980 | first, the arguments to the function are pushed onto the Lua stack | 1046 | first, the arguments to the function are pushed onto the Lua stack |
| 981 | (\see{pushing}), in direct order, i.e., the first argument is pushed first. | 1047 | \see{pushing}, in direct order, i.e., the first argument is pushed first. |
| 982 | Again, it is important to emphasize that, during this phase, | 1048 | Again, it is important to emphasize that, during this phase, |
| 983 | no other Lua function can be called. | 1049 | no other Lua function can be called. |
| 984 | 1050 | ||
| @@ -1012,20 +1078,21 @@ void lua_error (char *message); | |||
| 1012 | \end{verbatim} | 1078 | \end{verbatim} |
| 1013 | This function never returns. | 1079 | This function never returns. |
| 1014 | If the C function has been called from Lua, | 1080 | If the C function has been called from Lua, |
| 1015 | the corresponding Lua execution terminates, | 1081 | then the corresponding Lua execution terminates, |
| 1016 | as if an error had occurred inside Lua code. | 1082 | as if an error had occurred inside Lua code. |
| 1017 | Otherwise, the whole program terminates. | 1083 | Otherwise, the whole program terminates with a call to \verb|exit(1)|. |
| 1084 | %%LHF: proponho lua_error(char* m, int rc), gerando exit(rc) | ||
| 1018 | 1085 | ||
| 1019 | Fallbacks can be changed with: | 1086 | Fallbacks can be changed with: |
| 1020 | \Deffunc{lua_setfallback} | 1087 | \Deffunc{lua_setfallback} |
| 1021 | \begin{verbatim} | 1088 | \begin{verbatim} |
| 1022 | lua_Object lua_setfallback (char *name, lua_CFunction fallback); | 1089 | lua_Object lua_setfallback (char *name, lua_CFunction fallback); |
| 1023 | \end{verbatim} | 1090 | \end{verbatim} |
| 1024 | The first parameter is the fallback name, | 1091 | The first parameter is the fallback name \see{fallback}, |
| 1025 | and the second a CFunction to be used as the new fallback. | 1092 | and the second is a CFunction to be used as the new fallback. |
| 1026 | This function returns a \verb'lua_Object', | 1093 | This function returns a \verb'lua_Object', |
| 1027 | which is the old fallback value, | 1094 | which is the old fallback value, |
| 1028 | or \nil\ on fail (invalid fallback name). | 1095 | or \nil\ on failure (invalid fallback name). |
| 1029 | This old value can be used for chaining fallbacks. | 1096 | This old value can be used for chaining fallbacks. |
| 1030 | 1097 | ||
| 1031 | An example of C code calling a Lua function is shown in | 1098 | An example of C code calling a Lua function is shown in |
| @@ -1066,9 +1133,11 @@ this function returns | |||
| 1066 | \verb'LUA_NOOBJECT'\Deffunc{LUA_NOOBJECT}. | 1133 | \verb'LUA_NOOBJECT'\Deffunc{LUA_NOOBJECT}. |
| 1067 | In this way, it is possible to write functions that work with | 1134 | In this way, it is possible to write functions that work with |
| 1068 | a variable number of parameters. | 1135 | a variable number of parameters. |
| 1136 | The funcion \verb|lua_getparam| can be called in any order, | ||
| 1137 | and many times for the same index. | ||
| 1069 | 1138 | ||
| 1070 | To return values, a C function just pushes them onto the stack, | 1139 | To return values, a C function just pushes them onto the stack, |
| 1071 | in direct order (\see{valuesCLua}). | 1140 | in direct order \see{valuesCLua}. |
| 1072 | Like a Lua function, a C function called by Lua can also return | 1141 | Like a Lua function, a C function called by Lua can also return |
| 1073 | many results. | 1142 | many results. |
| 1074 | 1143 | ||
| @@ -1093,8 +1162,8 @@ void lua_unref (int ref); | |||
| 1093 | The function \verb'lua_ref' creates a reference | 1162 | The function \verb'lua_ref' creates a reference |
| 1094 | to the object that is on the top of the stack, | 1163 | to the object that is on the top of the stack, |
| 1095 | and returns this reference. | 1164 | and returns this reference. |
| 1096 | If \verb'lock' is true, the object is {\em locked}: | 1165 | If \verb'lock' is true, the object is {\em locked\/}: |
| 1097 | that means the object will not be garbage collected. | 1166 | this means the object will not be garbage collected. |
| 1098 | Notice that an unlocked reference may be garbage collected. | 1167 | Notice that an unlocked reference may be garbage collected. |
| 1099 | Whenever the referenced object is needed, | 1168 | Whenever the referenced object is needed, |
| 1100 | a call to \verb'lua_getref' | 1169 | a call to \verb'lua_getref' |
| @@ -1112,7 +1181,7 @@ it can be freed with a call to \verb'lua_unref'. | |||
| 1112 | \section{Predefined Functions and Libraries} | 1181 | \section{Predefined Functions and Libraries} |
| 1113 | 1182 | ||
| 1114 | The set of \Index{predefined functions} in Lua is small but powerful. | 1183 | The set of \Index{predefined functions} in Lua is small but powerful. |
| 1115 | Most of them provide features that allows some degree of | 1184 | Most of them provide features that allow some degree of |
| 1116 | \Index{reflexivity} in the language. | 1185 | \Index{reflexivity} in the language. |
| 1117 | Some of these features cannot be simulated with the rest of the | 1186 | Some of these features cannot be simulated with the rest of the |
| 1118 | Language nor with the standard Lua API. | 1187 | Language nor with the standard Lua API. |
| @@ -1121,11 +1190,11 @@ Others are just convenient interfaces to common API functions. | |||
| 1121 | The libraries, on the other hand, provide useful routines | 1190 | The libraries, on the other hand, provide useful routines |
| 1122 | that are implemented directly through the standard API. | 1191 | that are implemented directly through the standard API. |
| 1123 | Therefore, they are not necessary to the language, | 1192 | Therefore, they are not necessary to the language, |
| 1124 | and are provided as separated C modules. | 1193 | and are provided as separate C modules. |
| 1125 | Currently there are three standard libraries: | 1194 | Currently there are three standard libraries: |
| 1126 | \begin{itemize} | 1195 | \begin{itemize} |
| 1127 | \item string manipulation; | 1196 | \item string manipulation; |
| 1128 | \item mathematical functions (sin, cos, etc); | 1197 | \item mathematical functions (sin, log, etc); |
| 1129 | \item input and output (plus some system facilities). | 1198 | \item input and output (plus some system facilities). |
| 1130 | \end{itemize} | 1199 | \end{itemize} |
| 1131 | In order to have access to these libraries, | 1200 | In order to have access to these libraries, |
| @@ -1146,12 +1215,14 @@ If there is any error executing the file, it returns \nil. | |||
| 1146 | Otherwise, it returns the values returned by the chunk, | 1215 | Otherwise, it returns the values returned by the chunk, |
| 1147 | or a non \nil\ value if the chunk returns no values. | 1216 | or a non \nil\ value if the chunk returns no values. |
| 1148 | It issues an error when called with a non string argument. | 1217 | It issues an error when called with a non string argument. |
| 1218 | \verb|dofile| is simply an interface to \verb|lua_dofile|. | ||
| 1149 | 1219 | ||
| 1150 | \subsubsection*{\ff{\tt dostring (string)}}\Deffunc{dostring} | 1220 | \subsubsection*{\ff{\tt dostring (string)}}\Deffunc{dostring} |
| 1151 | This function executes a given string as a Lua chunk. | 1221 | This function executes a given string as a Lua chunk. |
| 1152 | If there is any error executing the string, it returns \nil. | 1222 | If there is any error executing the string, it returns \nil. |
| 1153 | Otherwise, it returns the values returned by the chunk, | 1223 | Otherwise, it returns the values returned by the chunk, |
| 1154 | or a non \nil\ value if the chunk returns no values. | 1224 | or a non \nil\ value if the chunk returns no values. |
| 1225 | \verb|dostring| is simply an interface to \verb|lua_dostring|. | ||
| 1155 | 1226 | ||
| 1156 | \subsubsection*{\ff{\tt next (table, index)}}\Deffunc{next} | 1227 | \subsubsection*{\ff{\tt next (table, index)}}\Deffunc{next} |
| 1157 | This function allows a program to traverse all fields of a table. | 1228 | This function allows a program to traverse all fields of a table. |
| @@ -1164,15 +1235,16 @@ the function returns the first index | |||
| 1164 | of the table (and its associated value). | 1235 | of the table (and its associated value). |
| 1165 | When called with the last index, or with \nil\ in an empty table, | 1236 | When called with the last index, or with \nil\ in an empty table, |
| 1166 | it returns \nil. | 1237 | it returns \nil. |
| 1238 | This function cannot be written with the standard API. | ||
| 1167 | 1239 | ||
| 1168 | In Lua there is no declaration of fields; | 1240 | In Lua there is no declaration of fields; |
| 1169 | semantically, there is no difference between a | 1241 | semantically, there is no difference between a |
| 1170 | field not present in a table or a field with value \nil. | 1242 | field not present in a table or a field with value \nil. |
| 1171 | Therefore, the function only considers fields with non nil values. | 1243 | Therefore, the function only considers fields with non \nil\ values. |
| 1172 | The order the indices are enumerated is not specified, | 1244 | The order in which the indices are enumerated is not specified, |
| 1173 | {\em even for numeric indices}. | 1245 | {\em even for numeric indices}. |
| 1174 | 1246 | ||
| 1175 | See Section \ref{exnext} for an example of the use of this function. | 1247 | See Section~\ref{exnext} for an example of the use of this function. |
| 1176 | 1248 | ||
| 1177 | \subsubsection*{\ff{\tt nextvar (name)}}\Deffunc{nextvar} | 1249 | \subsubsection*{\ff{\tt nextvar (name)}}\Deffunc{nextvar} |
| 1178 | This function is similar to the function \verb'next', | 1250 | This function is similar to the function \verb'next', |
| @@ -1182,7 +1254,8 @@ or \nil\ to get a first name. | |||
| 1182 | Similarly to \verb'next', it returns the name of another variable | 1254 | Similarly to \verb'next', it returns the name of another variable |
| 1183 | and its value, | 1255 | and its value, |
| 1184 | or \nil\ if there are no more variables. | 1256 | or \nil\ if there are no more variables. |
| 1185 | See Section \ref{exnext} for an example of the use of this function. | 1257 | See Section~\ref{exnext} for an example of the use of this function. |
| 1258 | This function cannot be written with the standard API. | ||
| 1186 | 1259 | ||
| 1187 | \subsubsection*{\ff{\tt tostring (e)}}\Deffunc{tostring} | 1260 | \subsubsection*{\ff{\tt tostring (e)}}\Deffunc{tostring} |
| 1188 | This function receives an argument of any type and | 1261 | This function receives an argument of any type and |
| @@ -1201,7 +1274,7 @@ See Section~\ref{libio} for functions for formatted output. | |||
| 1201 | This function receives one argument, | 1274 | This function receives one argument, |
| 1202 | and tries to convert it to a number. | 1275 | and tries to convert it to a number. |
| 1203 | If the argument is already a number or a string convertible | 1276 | If the argument is already a number or a string convertible |
| 1204 | to a number (\see{coercion}), then it returns that number; | 1277 | to a number \see{coercion}, then it returns that number; |
| 1205 | otherwise, it returns \nil. | 1278 | otherwise, it returns \nil. |
| 1206 | 1279 | ||
| 1207 | \subsubsection*{\ff{\tt type (v)}}\Deffunc{type} | 1280 | \subsubsection*{\ff{\tt type (v)}}\Deffunc{type} |
| @@ -1221,6 +1294,8 @@ This tag can be used to distinguish between user | |||
| 1221 | data with different tags, | 1294 | data with different tags, |
| 1222 | and between C functions and Lua functions. | 1295 | and between C functions and Lua functions. |
| 1223 | 1296 | ||
| 1297 | \verb|type| is simply an interface to \verb|lua_type|. | ||
| 1298 | |||
| 1224 | \subsubsection*{\ff{\tt assert (v)}}\Deffunc{assert} | 1299 | \subsubsection*{\ff{\tt assert (v)}}\Deffunc{assert} |
| 1225 | This function issues an {\em ``assertion failed!''} error | 1300 | This function issues an {\em ``assertion failed!''} error |
| 1226 | when its argument is \nil. | 1301 | when its argument is \nil. |
| @@ -1230,6 +1305,7 @@ This function issues an error message and terminates | |||
| 1230 | the last called function from the library | 1305 | the last called function from the library |
| 1231 | (\verb'lua_dofile', \verb'lua_dostring', \ldots). | 1306 | (\verb'lua_dofile', \verb'lua_dostring', \ldots). |
| 1232 | It never returns. | 1307 | It never returns. |
| 1308 | \verb|error| is simply an interface to \verb|lua_error|. | ||
| 1233 | 1309 | ||
| 1234 | \subsubsection*{\ff{\tt setglobal (name, value)}}\Deffunc{setglobal} | 1310 | \subsubsection*{\ff{\tt setglobal (name, value)}}\Deffunc{setglobal} |
| 1235 | This function assigns the given value to a global variable. | 1311 | This function assigns the given value to a global variable. |
| @@ -1237,6 +1313,7 @@ The string \verb'name' does not need to be a syntactically valid variable name. | |||
| 1237 | Therefore, this function can set global variables with strange names like | 1313 | Therefore, this function can set global variables with strange names like |
| 1238 | \verb|`m v 1'| or \verb'34'. | 1314 | \verb|`m v 1'| or \verb'34'. |
| 1239 | It returns the value of its second argument. | 1315 | It returns the value of its second argument. |
| 1316 | \verb|setglobal| is simply an interface to \verb|lua_storeglobal|. | ||
| 1240 | 1317 | ||
| 1241 | \subsubsection*{\ff{\tt getglobal (name)}}\Deffunc{getglobal} | 1318 | \subsubsection*{\ff{\tt getglobal (name)}}\Deffunc{getglobal} |
| 1242 | This function retrieves the value of a global variable. | 1319 | This function retrieves the value of a global variable. |
| @@ -1246,20 +1323,22 @@ The string \verb'name' does not need to be a syntactically valid variable name. | |||
| 1246 | \Deffunc{setfallback} | 1323 | \Deffunc{setfallback} |
| 1247 | This function sets a new fallback function to the given fallback. | 1324 | This function sets a new fallback function to the given fallback. |
| 1248 | It returns the old fallback function. | 1325 | It returns the old fallback function. |
| 1326 | \verb|setfallback| is simply an interface to \verb|lua_setfallback|. | ||
| 1249 | 1327 | ||
| 1250 | \subsection{String Manipulation} | 1328 | \subsection{String Manipulation} |
| 1251 | This library provides generic functions for string manipulation, | 1329 | This library provides generic functions for string manipulation, |
| 1252 | such as finding and extracting substrings and pattern matching. | 1330 | such as finding and extracting substrings and pattern matching. |
| 1253 | When indexing a string, the first character has position 1. | 1331 | When indexing a string, the first character is at position 1, |
| 1254 | See Page~\pageref{pm} for an explanation about patterns, | 1332 | not 0, as in C. |
| 1333 | See page~\pageref{pm} for an explanation about patterns, | ||
| 1255 | and Section~\ref{exstring} for some examples on string manipulation | 1334 | and Section~\ref{exstring} for some examples on string manipulation |
| 1256 | in Lua. | 1335 | in Lua. |
| 1257 | 1336 | ||
| 1258 | \subsubsection*{\ff{\tt strfind (str, pattern [, init [, plain]])}} | 1337 | \subsubsection*{\ff{\tt strfind (str, pattern [, init [, plain]])}} |
| 1259 | \Deffunc{strfind} | 1338 | \Deffunc{strfind} |
| 1260 | This function looks for the first {\em match} of | 1339 | This function looks for the first {\em match\/} of |
| 1261 | \verb-pattern- in \verb-str-. | 1340 | \verb-pattern- in \verb-str-. |
| 1262 | If it finds one, it returns the indexes on \verb-str- | 1341 | If it finds one, then it returns the indices on \verb-str- |
| 1263 | where this occurence starts and ends; | 1342 | where this occurence starts and ends; |
| 1264 | otherwise, it returns \nil. | 1343 | otherwise, it returns \nil. |
| 1265 | If the pattern specifies captures, | 1344 | If the pattern specifies captures, |
| @@ -1298,7 +1377,7 @@ Returns a string which is the concatenation of \verb-n- copies of | |||
| 1298 | the string \verb-s-. | 1377 | the string \verb-s-. |
| 1299 | 1378 | ||
| 1300 | \subsubsection*{\ff{\tt ascii (s [, i])}}\Deffunc{ascii} | 1379 | \subsubsection*{\ff{\tt ascii (s [, i])}}\Deffunc{ascii} |
| 1301 | Returns the ascii code of the character \verb's[i]'. | 1380 | Returns the ASCII code of the character \verb's[i]'. |
| 1302 | If \verb'i' is absent, then it is assumed to be 1. | 1381 | If \verb'i' is absent, then it is assumed to be 1. |
| 1303 | 1382 | ||
| 1304 | \subsubsection*{\ff{\tt format (formatstring, e1, e2, \ldots)}}\Deffunc{format} | 1383 | \subsubsection*{\ff{\tt format (formatstring, e1, e2, \ldots)}}\Deffunc{format} |
| @@ -1331,6 +1410,10 @@ The options \verb'c', \verb'd', \verb'E', \verb'e', \verb'f', | |||
| 1331 | \verb'g' \verb'i', \verb'o', \verb'u', \verb'X', and \verb'x' all | 1410 | \verb'g' \verb'i', \verb'o', \verb'u', \verb'X', and \verb'x' all |
| 1332 | expect a number as argument, | 1411 | expect a number as argument, |
| 1333 | whereas \verb'q' and \verb's' expect a string. | 1412 | whereas \verb'q' and \verb's' expect a string. |
| 1413 | Note that the \verb'*' modifier can be simulated by building | ||
| 1414 | the appropriate format string. | ||
| 1415 | For example, \verb|"%*g"| can be simulated with | ||
| 1416 | \verb|"%"..width.."g"|. | ||
| 1334 | 1417 | ||
| 1335 | \subsubsection*{\ff{\tt gsub (s, pat, repl [, n])}}\Deffunc{gsub} | 1418 | \subsubsection*{\ff{\tt gsub (s, pat, repl [, n])}}\Deffunc{gsub} |
| 1336 | Returns a copy of \verb-s-, | 1419 | Returns a copy of \verb-s-, |
| @@ -1339,15 +1422,16 @@ replaced by a replacement string specified by \verb-repl-. | |||
| 1339 | This function also returns, as a second value, | 1422 | This function also returns, as a second value, |
| 1340 | the total number of substitutions made. | 1423 | the total number of substitutions made. |
| 1341 | 1424 | ||
| 1342 | If \verb-repl- is a string, its value is used for replacement. | 1425 | If \verb-repl- is a string, then its value is used for replacement. |
| 1343 | Any sequence in \verb-repl- of the form \verb-%n- | 1426 | Any sequence in \verb-repl- of the form \verb-%n- |
| 1344 | with \verb-n- between 1 and 9 | 1427 | with \verb-n- between 1 and 9 |
| 1345 | stands for the value of the n-th captured substring. | 1428 | stands for the value of the n-th captured substring. |
| 1346 | 1429 | ||
| 1347 | If \verb-repl- is a function, this function is called every time a | 1430 | If \verb-repl- is a function, then this function is called every time a |
| 1348 | match occurs, with all captured substrings as parameters. | 1431 | match occurs, with all captured substrings as parameters |
| 1432 | (see below). | ||
| 1349 | If the value returned by this function is a string, | 1433 | If the value returned by this function is a string, |
| 1350 | it is used as the replacement string; | 1434 | then it is used as the replacement string; |
| 1351 | otherwise, the replacement string is the empty string. | 1435 | otherwise, the replacement string is the empty string. |
| 1352 | 1436 | ||
| 1353 | An optional parameter \verb-n- limits | 1437 | An optional parameter \verb-n- limits |
| @@ -1356,13 +1440,13 @@ For instance, when \verb-n- is 1 only the first occurrence of | |||
| 1356 | \verb-pat- is replaced. | 1440 | \verb-pat- is replaced. |
| 1357 | 1441 | ||
| 1358 | As an example, in the following expression each occurrence of the form | 1442 | As an example, in the following expression each occurrence of the form |
| 1359 | \verb-$name$- calls the function \verb|getenv|, | 1443 | \verb-$name- calls the function \verb|getenv|, |
| 1360 | passing \verb|name| as argument | 1444 | passing \verb|name| as argument |
| 1361 | (because only this part of the pattern is captured). | 1445 | (because only this part of the pattern is captured). |
| 1362 | The value returned by \verb|getenv| will replace the pattern. | 1446 | The value returned by \verb|getenv| will replace the pattern. |
| 1363 | Therefore, the whole expression: | 1447 | Therefore, the whole expression: |
| 1364 | \begin{verbatim} | 1448 | \begin{verbatim} |
| 1365 | gsub("home = $HOME$, user = $USER$", "$(%w%w*)$", getenv) | 1449 | gsub("home = $HOME, user = $USER", "$(%w%w*)", getenv) |
| 1366 | \end{verbatim} | 1450 | \end{verbatim} |
| 1367 | may return the string: | 1451 | may return the string: |
| 1368 | \begin{verbatim} | 1452 | \begin{verbatim} |
| @@ -1392,6 +1476,7 @@ The following combinations are allowed in describing a character class: | |||
| 1392 | \item[{\tt \%W}] --- represents all non alphanumeric characters. | 1476 | \item[{\tt \%W}] --- represents all non alphanumeric characters. |
| 1393 | \item[{\tt \%\em x}] (where {\em x} is any non alphanumeric character) --- | 1477 | \item[{\tt \%\em x}] (where {\em x} is any non alphanumeric character) --- |
| 1394 | represents the character {\em x}. | 1478 | represents the character {\em x}. |
| 1479 | This is the standard way to escape the magic characters \verb'()%.[*?'. | ||
| 1395 | \item[{\tt [char-set]}] --- | 1480 | \item[{\tt [char-set]}] --- |
| 1396 | Represents the class which is the union of all | 1481 | Represents the class which is the union of all |
| 1397 | characters in char-set. | 1482 | characters in char-set. |
| @@ -1419,7 +1504,8 @@ A character class followed by \verb'?' matches 0 or one occurrence | |||
| 1419 | of a character in the class. | 1504 | of a character in the class. |
| 1420 | A pattern item may also has the form \verb'%n', | 1505 | A pattern item may also has the form \verb'%n', |
| 1421 | for \verb-n- between 1 and 9; | 1506 | for \verb-n- between 1 and 9; |
| 1422 | such item matches a sub-string equal to the n-th captured string. | 1507 | such item matches a sub-string equal to the n-th captured string |
| 1508 | (see below). | ||
| 1423 | 1509 | ||
| 1424 | \paragraph{Pattern:} | 1510 | \paragraph{Pattern:} |
| 1425 | a \Def{pattern} is a sequence of pattern items. | 1511 | a \Def{pattern} is a sequence of pattern items. |
| @@ -1433,14 +1519,18 @@ end of the subject string. | |||
| 1433 | A pattern may contain sub-patterns enclosed in parentheses, | 1519 | A pattern may contain sub-patterns enclosed in parentheses, |
| 1434 | that describe \Def{captures}. | 1520 | that describe \Def{captures}. |
| 1435 | When a match succeeds, the sub-strings of the subject string | 1521 | When a match succeeds, the sub-strings of the subject string |
| 1436 | that match captures are {\em captured} for future use. | 1522 | that match captures are {\em captured\/} for future use. |
| 1437 | Captures are numbered according to their left parentheses. | 1523 | Captures are numbered according to their left parentheses. |
| 1524 | For instance, in the pattern \verb|"(a*(.)%w(%s*))"|, | ||
| 1525 | the capture \verb|"(a*(.)%w(%s*))"| has number 1 | ||
| 1526 | (and therefore is the first capture), | ||
| 1527 | \verb|(.)| has number 2, and \verb|(%s*)| has number 3. | ||
| 1438 | 1528 | ||
| 1439 | \subsection{Mathematical Functions} \label{mathlib} | 1529 | \subsection{Mathematical Functions} \label{mathlib} |
| 1440 | 1530 | ||
| 1441 | This library is an interface to some functions of the standard C math library. | 1531 | This library is an interface to some functions of the standard C math library. |
| 1442 | Moreover, it registers a fallback for the binary operator \verb'^' which, | 1532 | In addition, it registers a fallback for the binary operator \verb'^' that, |
| 1443 | when applied to numbers \verb'x^y', returns $x^y$. | 1533 | returns $x^y$ when applied to numbers \verb'x^y'. |
| 1444 | 1534 | ||
| 1445 | The library provides the following functions: | 1535 | The library provides the following functions: |
| 1446 | \Deffunc{abs}\Deffunc{acos}\Deffunc{asin}\Deffunc{atan} | 1536 | \Deffunc{abs}\Deffunc{acos}\Deffunc{asin}\Deffunc{atan} |
| @@ -1450,12 +1540,12 @@ The library provides the following functions: | |||
| 1450 | \Deffunc{random}\Deffunc{randomseed} | 1540 | \Deffunc{random}\Deffunc{randomseed} |
| 1451 | \begin{verbatim} | 1541 | \begin{verbatim} |
| 1452 | abs acos asin atan atan2 ceil cos floor log log10 | 1542 | abs acos asin atan atan2 ceil cos floor log log10 |
| 1453 | max min mod sin sqrt tan random randomseed | 1543 | max min mod sin sqrt tan random randomseed |
| 1454 | \end{verbatim} | 1544 | \end{verbatim} |
| 1455 | Most of them | 1545 | Most of them |
| 1456 | are only interfaces to the homonymous functions in the C library, | 1546 | are only interfaces to the homonymous functions in the C library, |
| 1457 | except that, for the trigonometric functions, | 1547 | except that, for the trigonometric functions, |
| 1458 | all angles are expressed in degrees, not radians. | 1548 | all angles are expressed in {\em degrees}, not radians. |
| 1459 | 1549 | ||
| 1460 | The function \verb'max' returns the maximum | 1550 | The function \verb'max' returns the maximum |
| 1461 | value of its numeric arguments. | 1551 | value of its numeric arguments. |
| @@ -1471,7 +1561,7 @@ $[0,1)$. | |||
| 1471 | 1561 | ||
| 1472 | \subsection{I/O Facilities} \label{libio} | 1562 | \subsection{I/O Facilities} \label{libio} |
| 1473 | 1563 | ||
| 1474 | All I/O operations in Lua are done over two {\em current} files: | 1564 | All input and outpu operations in Lua are done over two {\em current\/} files: |
| 1475 | one for reading and one for writing. | 1565 | one for reading and one for writing. |
| 1476 | Initially, the current input file is \verb'stdin', | 1566 | Initially, the current input file is \verb'stdin', |
| 1477 | and the current output file is \verb'stdout'. | 1567 | and the current output file is \verb'stdout'. |
| @@ -1485,9 +1575,10 @@ some value different from \nil\ on success. | |||
| 1485 | This function may be called in three ways. | 1575 | This function may be called in three ways. |
| 1486 | When called with a file name, | 1576 | When called with a file name, |
| 1487 | it opens the named file, | 1577 | it opens the named file, |
| 1488 | sets it as the {\em current} input file, | 1578 | sets it as the {\em current\/} input file, |
| 1489 | and returns a {\em handle} to the file | 1579 | and returns a {\em handle\/} to the file |
| 1490 | (this handle is a user data containing the file stream \verb|FILE *|). | 1580 | (this handle is a user data containing the file stream \verb|FILE*|). |
| 1581 | It does not close the current input file. | ||
| 1491 | When called with a file handle, returned by a previous call, | 1582 | When called with a file handle, returned by a previous call, |
| 1492 | it restores the file as the current input. | 1583 | it restores the file as the current input. |
| 1493 | When called without parameters, | 1584 | When called without parameters, |
| @@ -1497,36 +1588,52 @@ and restores \verb'stdin' as the current input file. | |||
| 1497 | If this function fails, it returns \nil, | 1588 | If this function fails, it returns \nil, |
| 1498 | plus a string describing the error. | 1589 | plus a string describing the error. |
| 1499 | 1590 | ||
| 1500 | {\em System dependent:} if \verb'filename' starts with a \verb'|', | 1591 | \begin{quotation} |
| 1592 | \noindent | ||
| 1593 | {\em System dependent\/}: if \verb'filename' starts with a \verb'|', | ||
| 1501 | then a \Index{piped input} is open, via function \IndexVerb{popen}. | 1594 | then a \Index{piped input} is open, via function \IndexVerb{popen}. |
| 1595 | Not all systems implement pipes. | ||
| 1596 | Moreover, | ||
| 1597 | the number of files that can be open at the same time is usually limited and | ||
| 1598 | depends on the system. | ||
| 1599 | \end{quotation} | ||
| 1502 | 1600 | ||
| 1503 | \subsubsection*{\ff{\tt writeto (filename)}}\Deffunc{writeto} | 1601 | \subsubsection*{\ff{\tt writeto (filename)}}\Deffunc{writeto} |
| 1504 | 1602 | ||
| 1505 | This function may be called in three ways. | 1603 | This function may be called in three ways. |
| 1506 | When called with a file name, | 1604 | When called with a file name, |
| 1507 | it opens the named file, | 1605 | it opens the named file, |
| 1508 | sets it as the {\em current} output file, | 1606 | sets it as the {\em current\/} output file, |
| 1509 | and returns a {\em handle} to the file | 1607 | and returns a {\em handle\/} to the file |
| 1510 | (this handle is a user data containing the file stream \verb|FILE *|). | 1608 | (this handle is a user data containing the file stream \verb|FILE*|). |
| 1609 | It does not close the current output file. | ||
| 1511 | Notice that, if the file already exists, | 1610 | Notice that, if the file already exists, |
| 1512 | it will be {\em completely erased} with this operation. | 1611 | it will be {\em completely erased\/} with this operation. |
| 1513 | When called with a file handle, returned by a previous call, | 1612 | When called with a file handle, returned by a previous call, |
| 1514 | it restores the file as the current output. | 1613 | it restores the file as the current output. |
| 1515 | When called without parameters, | 1614 | When called without parameters, |
| 1516 | this function closes the current output file, | 1615 | this function closes the current output file, |
| 1517 | and restores \verb'stdout' as the current output file. | 1616 | and restores \verb'stdout' as the current output file. |
| 1518 | \index{closing a file} | 1617 | \index{closing a file} |
| 1618 | %%LHF: nao tem como escrever em stderr, tem? | ||
| 1519 | 1619 | ||
| 1520 | If this function fails, it returns \nil, | 1620 | If this function fails, it returns \nil, |
| 1521 | plus a string describing the error. | 1621 | plus a string describing the error. |
| 1522 | 1622 | ||
| 1523 | {\em System dependent:} if \verb'filename' starts with a \verb'|', | 1623 | \begin{quotation} |
| 1624 | \noindent | ||
| 1625 | {\em System dependent\/}: if \verb'filename' starts with a \verb'|', | ||
| 1524 | then a \Index{piped output} is open, via function \IndexVerb{popen}. | 1626 | then a \Index{piped output} is open, via function \IndexVerb{popen}. |
| 1627 | Not all systems implement pipes. | ||
| 1628 | Moreover, | ||
| 1629 | the number of files that can be open at the same time is usually limited and | ||
| 1630 | depends on the system. | ||
| 1631 | \end{quotation} | ||
| 1525 | 1632 | ||
| 1526 | \subsubsection*{\ff{\tt appendto (filename)}}\Deffunc{appendto} | 1633 | \subsubsection*{\ff{\tt appendto (filename)}}\Deffunc{appendto} |
| 1527 | 1634 | ||
| 1528 | This function opens a file named \verb'filename' and sets it as the | 1635 | This function opens a file named \verb'filename' and sets it as the |
| 1529 | {\em current} output file. | 1636 | {\em current\/} output file. |
| 1530 | It returns the file handle, | 1637 | It returns the file handle, |
| 1531 | or \nil\ in case of error. | 1638 | or \nil\ in case of error. |
| 1532 | Unlike the \verb'writeto' operation, | 1639 | Unlike the \verb'writeto' operation, |
| @@ -1534,7 +1641,7 @@ this function does not erase any previous content of the file. | |||
| 1534 | If this function fails, it returns \nil, | 1641 | If this function fails, it returns \nil, |
| 1535 | plus a string describing the error. | 1642 | plus a string describing the error. |
| 1536 | 1643 | ||
| 1537 | Notice that function \verb|writeto| is available to close a file. | 1644 | Notice that function \verb|writeto| is available to close an output file. |
| 1538 | 1645 | ||
| 1539 | \subsubsection*{\ff{\tt remove (filename)}}\Deffunc{remove} | 1646 | \subsubsection*{\ff{\tt remove (filename)}}\Deffunc{remove} |
| 1540 | 1647 | ||
| @@ -1544,7 +1651,7 @@ plus a string describing the error. | |||
| 1544 | 1651 | ||
| 1545 | \subsubsection*{\ff{\tt rename (name1, name2)}}\Deffunc{rename} | 1652 | \subsubsection*{\ff{\tt rename (name1, name2)}}\Deffunc{rename} |
| 1546 | 1653 | ||
| 1547 | This function renames file \verb'name1' to \verb'name2'. | 1654 | This function renames file named \verb'name1' to \verb'name2'. |
| 1548 | If this function fails, it returns \nil, | 1655 | If this function fails, it returns \nil, |
| 1549 | plus a string describing the error. | 1656 | plus a string describing the error. |
| 1550 | 1657 | ||
| @@ -1560,6 +1667,7 @@ according to a read pattern, that specifies how much to read; | |||
| 1560 | characters are read from the current input file until | 1667 | characters are read from the current input file until |
| 1561 | the read pattern fails or ends. | 1668 | the read pattern fails or ends. |
| 1562 | The function \verb|read| returns a string with the characters read, | 1669 | The function \verb|read| returns a string with the characters read, |
| 1670 | even if the pattern succeeds only partially, | ||
| 1563 | or \nil\ if the read pattern fails {\em and\/} | 1671 | or \nil\ if the read pattern fails {\em and\/} |
| 1564 | the result string would be empty. | 1672 | the result string would be empty. |
| 1565 | When called without parameters, | 1673 | When called without parameters, |
| @@ -1580,7 +1688,9 @@ since it can match a sequence of zero characteres, it never fails.% | |||
| 1580 | \footnote{ | 1688 | \footnote{ |
| 1581 | Notice that this behaviour is different from regular pattern matching, | 1689 | Notice that this behaviour is different from regular pattern matching, |
| 1582 | where a \verb'*' expands to the maximum length {\em such that\/} | 1690 | where a \verb'*' expands to the maximum length {\em such that\/} |
| 1583 | the rest of the pattern does not fail.} | 1691 | the rest of the pattern does not fail. |
| 1692 | Therefore, there is no need for backtracking the reading. | ||
| 1693 | } | ||
| 1584 | 1694 | ||
| 1585 | A pattern item may contain sub-patterns enclosed in curly brackets, | 1695 | A pattern item may contain sub-patterns enclosed in curly brackets, |
| 1586 | that describe \Def{skips}. | 1696 | that describe \Def{skips}. |
| @@ -1606,6 +1716,8 @@ or \nil\ if the next characters do not conform to an integer format. | |||
| 1606 | This function writes the value of each of its arguments to the | 1716 | This function writes the value of each of its arguments to the |
| 1607 | current output file. | 1717 | current output file. |
| 1608 | The arguments must be strings or numbers. | 1718 | The arguments must be strings or numbers. |
| 1719 | To write other values, | ||
| 1720 | use \verb|tostring| before \verb|write|. | ||
| 1609 | If this function fails, it returns \nil, | 1721 | If this function fails, it returns \nil, |
| 1610 | plus a string describing the error. | 1722 | plus a string describing the error. |
| 1611 | 1723 | ||
| @@ -1615,13 +1727,15 @@ This function returns a string containing date and time | |||
| 1615 | formatted according to the given string \verb'format', | 1727 | formatted according to the given string \verb'format', |
| 1616 | following the same rules of the ANSI C function \verb'strftime'. | 1728 | following the same rules of the ANSI C function \verb'strftime'. |
| 1617 | When called without arguments, | 1729 | When called without arguments, |
| 1618 | it returns a reasonable date and time representation. | 1730 | it returns a reasonable date and time representation that depends on |
| 1731 | the host system. | ||
| 1619 | 1732 | ||
| 1620 | \subsubsection*{\ff{\tt exit ([code])}}\Deffunc{exit} | 1733 | \subsubsection*{\ff{\tt exit ([code])}}\Deffunc{exit} |
| 1621 | 1734 | ||
| 1622 | This function calls the C function \verb-exit-, | 1735 | This function calls the C function \verb-exit-, |
| 1623 | with an optional \verb-code-, | 1736 | with an optional \verb-code-, |
| 1624 | to terminate the program. | 1737 | to terminate the program. |
| 1738 | The default value for \verb-code- is 1. | ||
| 1625 | 1739 | ||
| 1626 | \subsubsection*{\ff{\tt getenv (varname)}}\Deffunc{getenv} | 1740 | \subsubsection*{\ff{\tt getenv (varname)}}\Deffunc{getenv} |
| 1627 | 1741 | ||
| @@ -1631,13 +1745,13 @@ or \nil\ if the variable is not defined. | |||
| 1631 | \subsubsection*{\ff{\tt execute (command)}}\Deffunc{execute} | 1745 | \subsubsection*{\ff{\tt execute (command)}}\Deffunc{execute} |
| 1632 | 1746 | ||
| 1633 | This function is equivalent to the C function \verb|system|. | 1747 | This function is equivalent to the C function \verb|system|. |
| 1634 | It passes \verb|command| to be executed by an Operating System Shell. | 1748 | It passes \verb|command| to be executed by an operating system shell. |
| 1635 | It returns an error code, which is implementation-defined. | 1749 | It returns an error code, which is system-dependent. |
| 1636 | 1750 | ||
| 1637 | 1751 | ||
| 1638 | \section{The Debugger Interface} \label{debugI} | 1752 | \section{The Debugger Interface} \label{debugI} |
| 1639 | 1753 | ||
| 1640 | Lua has no built-in debugger facilities. | 1754 | Lua has no built-in debugging facilities. |
| 1641 | Instead, it offers a special interface, | 1755 | Instead, it offers a special interface, |
| 1642 | by means of functions and {\em hooks}, | 1756 | by means of functions and {\em hooks}, |
| 1643 | which allows the construction of different | 1757 | which allows the construction of different |
| @@ -1663,7 +1777,8 @@ The type \verb'lua_Function' is just another name | |||
| 1663 | to \verb'lua_Object'. | 1777 | to \verb'lua_Object'. |
| 1664 | Although, in this library, | 1778 | Although, in this library, |
| 1665 | a \verb'lua_Function' can be used wherever a \verb'lua_Object' is required, | 1779 | a \verb'lua_Function' can be used wherever a \verb'lua_Object' is required, |
| 1666 | a parameter \verb'lua_Function' accepts only a handle returned by | 1780 | when a parameter has type \verb'lua_Function' |
| 1781 | it accepts only a handle returned by | ||
| 1667 | \verb'lua_stackedfunction'. | 1782 | \verb'lua_stackedfunction'. |
| 1668 | 1783 | ||
| 1669 | Three other functions produce extra information about a function: | 1784 | Three other functions produce extra information about a function: |
| @@ -1681,14 +1796,14 @@ then \verb'linedefined' is -1, and \verb'filename' is \verb'"(C)"'. | |||
| 1681 | 1796 | ||
| 1682 | The function \verb'lua_currentline' gives the current line where | 1797 | The function \verb'lua_currentline' gives the current line where |
| 1683 | a given function is executing. | 1798 | a given function is executing. |
| 1684 | It only works if the function has been pre-compiled with debug | 1799 | It only works if the function has been compiled with debug |
| 1685 | information (\see{pragma}). | 1800 | information \see{pragma}. |
| 1686 | When no line information is available, it returns -1. | 1801 | When no line information is available, it returns -1. |
| 1687 | 1802 | ||
| 1688 | Function \verb'lua_getobjname' tries to find a reasonable name for | 1803 | Function \verb'lua_getobjname' tries to find a reasonable name for |
| 1689 | a given function. | 1804 | a given function. |
| 1690 | Because functions in Lua are first class values, | 1805 | Because functions in Lua are first class values, |
| 1691 | they do not have a fixed name. | 1806 | they do not have a fixed name: |
| 1692 | Some functions may be the value of many global variables, | 1807 | Some functions may be the value of many global variables, |
| 1693 | while others may be stored only in a table field. | 1808 | while others may be stored only in a table field. |
| 1694 | Function \verb'lua_getobjname' first checks whether the given | 1809 | Function \verb'lua_getobjname' first checks whether the given |
| @@ -1697,7 +1812,7 @@ If so, it returns the string \verb'"fallback"', | |||
| 1697 | and \verb'name' is set to point to the fallback name. | 1812 | and \verb'name' is set to point to the fallback name. |
| 1698 | Otherwise, if the given function is the value of a global variable, | 1813 | Otherwise, if the given function is the value of a global variable, |
| 1699 | then \verb'lua_getobjname' returns the string \verb'"global"', | 1814 | then \verb'lua_getobjname' returns the string \verb'"global"', |
| 1700 | while \verb'name' points to the variable name. | 1815 | and \verb'name' points to the variable name. |
| 1701 | If the given function is neither a fallback nor a global variable, | 1816 | If the given function is neither a fallback nor a global variable, |
| 1702 | then \verb'lua_getobjname' returns the empty string, | 1817 | then \verb'lua_getobjname' returns the empty string, |
| 1703 | and \verb'name' is set to \verb'NULL'. | 1818 | and \verb'name' is set to \verb'NULL'. |
| @@ -1706,13 +1821,13 @@ and \verb'name' is set to \verb'NULL'. | |||
| 1706 | 1821 | ||
| 1707 | The following functions allow the manipulation of the | 1822 | The following functions allow the manipulation of the |
| 1708 | local variables of a given activation record. | 1823 | local variables of a given activation record. |
| 1709 | They only work if the function has been pre-compiled with debug | 1824 | They only work if the function has been compiled with debug |
| 1710 | information (\see{pragma}). | 1825 | information \see{pragma}. |
| 1711 | \begin{verbatim} | 1826 | \begin{verbatim} |
| 1712 | lua_Object lua_getlocal (lua_Function func, int local_number, char **name); | 1827 | lua_Object lua_getlocal (lua_Function func, int local_number, char **name); |
| 1713 | int lua_setlocal (lua_Function func, int local_number); | 1828 | int lua_setlocal (lua_Function func, int local_number); |
| 1714 | \end{verbatim} | 1829 | \end{verbatim} |
| 1715 | The first one returns the value of a local variable, | 1830 | \verb|lua_getlocal| returns the value of a local variable, |
| 1716 | and sets \verb'name' to point to the variable name. | 1831 | and sets \verb'name' to point to the variable name. |
| 1717 | \verb'local_number' is an index for local variables. | 1832 | \verb'local_number' is an index for local variables. |
| 1718 | The first parameter has index 1, and so on, until the | 1833 | The first parameter has index 1, and so on, until the |
| @@ -1721,10 +1836,12 @@ When called with a \verb'local_number' greater than the | |||
| 1721 | number of active local variables, | 1836 | number of active local variables, |
| 1722 | or if the activation record has no debug information, | 1837 | or if the activation record has no debug information, |
| 1723 | \verb'lua_getlocal' returns \verb'LUA_NOOBJECT'. | 1838 | \verb'lua_getlocal' returns \verb'LUA_NOOBJECT'. |
| 1839 | Formal parameters are the first local variables. | ||
| 1724 | 1840 | ||
| 1725 | The function \verb'lua_setlocal' sets the local variable | 1841 | The function \verb'lua_setlocal' sets the local variable |
| 1842 | %%LHF: please, lua_setglobal! | ||
| 1726 | \verb'local_number' to the value previously pushed on the stack | 1843 | \verb'local_number' to the value previously pushed on the stack |
| 1727 | (\see{valuesCLua}). | 1844 | \see{valuesCLua}. |
| 1728 | If the function succeeds, then it returns 1. | 1845 | If the function succeeds, then it returns 1. |
| 1729 | If \verb'local_number' is greater than the number | 1846 | If \verb'local_number' is greater than the number |
| 1730 | of active local variables, | 1847 | of active local variables, |
| @@ -1756,9 +1873,9 @@ Its only parameter is the line number | |||
| 1756 | (the same information which is provided by the call | 1873 | (the same information which is provided by the call |
| 1757 | \verb'lua_currentline(lua_stackedfunction(0))'). | 1874 | \verb'lua_currentline(lua_stackedfunction(0))'). |
| 1758 | This second hook is only called if the active function | 1875 | This second hook is only called if the active function |
| 1759 | has been pre-compiled with debug information (\see{pragma}). | 1876 | has been compiled with debug information \see{pragma}. |
| 1760 | 1877 | ||
| 1761 | A hook is disabled when its value is NULL (0), | 1878 | A hook is disabled when its value is \verb|NULL|, |
| 1762 | which is the initial value of both hooks. | 1879 | which is the initial value of both hooks. |
| 1763 | 1880 | ||
| 1764 | 1881 | ||
| @@ -2008,8 +2125,7 @@ void Index (void) | |||
| 2008 | { | 2125 | { |
| 2009 | lua_pushobject(parent); | 2126 | lua_pushobject(parent); |
| 2010 | lua_pushobject(index); | 2127 | lua_pushobject(index); |
| 2011 | /* return result from getsubscript */ | 2128 | lua_pushobject(lua_getsubscript()); /* return result from getsubscript */ |
| 2012 | lua_pushobject(lua_getsubscript()); | ||
| 2013 | } | 2129 | } |
| 2014 | else | 2130 | else |
| 2015 | callOldFallback(table, index); | 2131 | callOldFallback(table, index); |
| @@ -2033,7 +2149,7 @@ There are many different ways to do object-oriented programming in Lua. | |||
| 2033 | This section presents one possible way to | 2149 | This section presents one possible way to |
| 2034 | implement classes, | 2150 | implement classes, |
| 2035 | using the inheritance mechanism presented above. | 2151 | using the inheritance mechanism presented above. |
| 2036 | {\em Please notice: the following examples only work | 2152 | {\em Please note: the following examples only work |
| 2037 | with the index fallback redefined according to | 2153 | with the index fallback redefined according to |
| 2038 | Section~\ref{exfallback}}. | 2154 | Section~\ref{exfallback}}. |
| 2039 | 2155 | ||
| @@ -2194,7 +2310,7 @@ void remove_blanks (char *s) | |||
| 2194 | \end{verbatim} | 2310 | \end{verbatim} |
| 2195 | 2311 | ||
| 2196 | 2312 | ||
| 2197 | \section{\Index{Lua Stand-alone}} | 2313 | \section{\Index{Lua Stand-alone}} \label{lua-sa} |
| 2198 | 2314 | ||
| 2199 | Although Lua has been designed as an extension language, | 2315 | Although Lua has been designed as an extension language, |
| 2200 | the language can also be used as a stand-alone interpreter. | 2316 | the language can also be used as a stand-alone interpreter. |
| @@ -2210,7 +2326,7 @@ until an \verb|EOF|. | |||
| 2210 | \item[{\tt var=exp}] executes \verb|var=exp| as a Lua chunk. | 2326 | \item[{\tt var=exp}] executes \verb|var=exp| as a Lua chunk. |
| 2211 | \item[{\tt filename}] executes file \verb|filename| as a Lua chunk. | 2327 | \item[{\tt filename}] executes file \verb|filename| as a Lua chunk. |
| 2212 | \end{description} | 2328 | \end{description} |
| 2213 | All arguments are handle in order. | 2329 | All arguments are handled in order. |
| 2214 | For instance, an invocation like | 2330 | For instance, an invocation like |
| 2215 | \begin{verbatim} | 2331 | \begin{verbatim} |
| 2216 | $ lua - a=1 prog.lua | 2332 | $ lua - a=1 prog.lua |
| @@ -2225,10 +2341,11 @@ For instance, a call like | |||
| 2225 | \begin{verbatim} | 2341 | \begin{verbatim} |
| 2226 | $ lua a="name" prog.lua | 2342 | $ lua a="name" prog.lua |
| 2227 | \end{verbatim} | 2343 | \end{verbatim} |
| 2228 | will {\em not} set \verb|a| to the string \verb|"name"|. | 2344 | will {\em not\/} set \verb|a| to the string \verb|"name"|. |
| 2229 | Instead, the quotes will be handled by the shell, | 2345 | Instead, the quotes will be handled by the shell, |
| 2230 | lua will get only \verb'a=name' to run, | 2346 | lua will get only \verb'a=name' to run, |
| 2231 | and \verb'a' will finish with \nil. | 2347 | and \verb'a' will finish with \nil, |
| 2348 | because the global variable \verb|name| has not been initialized. | ||
| 2232 | Instead, one should write | 2349 | Instead, one should write |
| 2233 | \begin{verbatim} | 2350 | \begin{verbatim} |
| 2234 | $ lua 'a="name"' prog.lua | 2351 | $ lua 'a="name"' prog.lua |
| @@ -2241,7 +2358,7 @@ jointly with \tecgraf, used extensively early versions of | |||
| 2241 | this system and gave valuable comments. | 2358 | this system and gave valuable comments. |
| 2242 | The authors would also like to thank Carlos Henrique Levy, | 2359 | The authors would also like to thank Carlos Henrique Levy, |
| 2243 | who found the name of the game. | 2360 | who found the name of the game. |
| 2244 | Lua means {\em moon} in Portuguese. | 2361 | Lua means {\em moon\/} in Portuguese. |
| 2245 | 2362 | ||
| 2246 | 2363 | ||
| 2247 | 2364 | ||
| @@ -2256,7 +2373,7 @@ Here is a list of all these incompatibilities. | |||
| 2256 | 2373 | ||
| 2257 | \subsection*{Incompatibilities with \Index{version 2.4}} | 2374 | \subsection*{Incompatibilities with \Index{version 2.4}} |
| 2258 | The whole I/O facilities have been rewritten. | 2375 | The whole I/O facilities have been rewritten. |
| 2259 | We strongly encourage programmers to addapt their code | 2376 | We strongly encourage programmers to adapt their code |
| 2260 | to this new version. | 2377 | to this new version. |
| 2261 | However, we are keeping the old version of the libraries | 2378 | However, we are keeping the old version of the libraries |
| 2262 | in the distribution, | 2379 | in the distribution, |
| @@ -2266,12 +2383,12 @@ The incompatibilities between the new and the old libraries are: | |||
| 2266 | \item The format facility of function \verb'write' has been supersed by | 2383 | \item The format facility of function \verb'write' has been supersed by |
| 2267 | function \verb'format'; | 2384 | function \verb'format'; |
| 2268 | therefore this facility has been dropped. | 2385 | therefore this facility has been dropped. |
| 2269 | \item Function \verb'read' now uses {\em read patterns} to specify | 2386 | \item Function \verb'read' now uses {\em read patterns\/} to specify |
| 2270 | what to read; | 2387 | what to read; |
| 2271 | this is incompatible with the old format options. | 2388 | this is incompatible with the old format options. |
| 2272 | \item Function \verb'strfind' now accepts patterns, | 2389 | \item Function \verb'strfind' now accepts patterns, |
| 2273 | so it may have a different behavior when the pattern includes | 2390 | so it may have a different behavior when the pattern includes |
| 2274 | special characteres. | 2391 | special characters. |
| 2275 | \end{itemize} | 2392 | \end{itemize} |
| 2276 | 2393 | ||
| 2277 | \subsection*{Incompatibilities with \Index{version 2.2}} | 2394 | \subsection*{Incompatibilities with \Index{version 2.2}} |
| @@ -2341,7 +2458,7 @@ int lua_storesubscript (void); | |||
| 2341 | with the parameters explicitly pushed on the stack. | 2458 | with the parameters explicitly pushed on the stack. |
| 2342 | \item | 2459 | \item |
| 2343 | The functionality of the function \verb'lua_errorfunction' has been | 2460 | The functionality of the function \verb'lua_errorfunction' has been |
| 2344 | replaced by the {\em fallback} mechanism (\see{error}). | 2461 | replaced by the {\em fallback\/} mechanism \see{error}. |
| 2345 | \item | 2462 | \item |
| 2346 | When calling a function from the Lua library, | 2463 | When calling a function from the Lua library, |
| 2347 | parameters passed through the stack | 2464 | parameters passed through the stack |
