aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--manual.tex459
1 files changed, 288 insertions, 171 deletions
diff --git a/manual.tex b/manual.tex
index 6d52cb7e..45fc1d5e 100644
--- a/manual.tex
+++ b/manual.tex
@@ -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
73Copyright (c) 1994--1996 TeCGraf, PUC-Rio. Written by Waldemar Celes Filho,
74Roberto Ierusalimschy, Luiz Henrique de Figueiredo. All rights reserved.
75%
76Permission is hereby granted, without written agreement and without license or
77royalty fees, to use, copy, modify, and distribute this software and its
78documentation for any purpose, subject to the following conditions:
79%
80The above copyright notice and this permission notice shall appear in all
81copies or substantial portions of this software.
82%
83The name "Lua" cannot be used for any modified form of this software that does
84not originate from the authors. Nevertheless, the name "Lua" may and should be
85used to designate the language implemented and described in this package,
86even if embedded in any other system, as long as its syntax and semantics
87remain unchanged.
88%
89The authors specifically disclaim any warranties, including, but not limited
90to, the implied warranties of merchantability and fitness for a particular
91purpose. The software provided hereunder is on an "as is" basis, and the
92authors have no obligation to provide maintenance, support, updates,
93enhancements, or modifications. In no event shall TeCGraf, PUC-Rio, or the
94authors be liable to any party for direct, indirect, special, incidental, or
95consequential damages arising out of the use of this software and its
96documentation.
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
71Lua is an extension programming language designed to support 107Lua is an extension programming language designed to support
72general procedural programming features with data description 108general procedural programming features with data description
73facilities. 109facilities.
74It is intended to be used as a configuration language for any 110It is intended to be used as a
111light-weight, but powerful, configuration language for any
75program that needs one. 112program that needs one.
76Lua has been designed and implemented by 113Lua has been designed and implemented by
77W.~Celes, L.~H.~de Figueiredo and R.~Ierusalimschy. 114W.~Celes,
115R.~Ierusalimschy and
116L.~H.~de Figueiredo.
78 117
79Lua is implemented as a library, written in C. 118Lua is implemented as a library, written in C.
80Being an extension language, Lua has no notion of a ``main'' program: 119Being an extension language, Lua has no notion of a ``main'' program:
81it only works {\em embedded} in a host client, 120it only works {\em embedded\/} in a host client,
82called the {\em embedding} program. 121called the {\em embedding\/} program.
83This host program can invoke functions to execute a piece of 122This host program can invoke functions to execute a piece of
84code in Lua, can write and read Lua variables, 123code in Lua, can write and read Lua variables,
85and can register C functions to be called by Lua code. 124and can register C functions to be called by Lua code.
86Through the use of C functions, Lua can be augmented to cope with 125Through the use of C functions, Lua can be augmented to cope with
87rather different domains, 126many, completely different domains,
88thus creating customized programming languages sharing a syntactical framework. 127thus creating customized programming languages sharing a syntactical framework.
89 128
90Lua is free-distribution software, 129Lua 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.
113Any variable is assumed to be global unless explicitly declared local 152Any variable is assumed to be global unless explicitly declared local
114(\see{localvar}). 153\see{localvar}.
115Before the first assignment, the value of a global variable is \nil. 154Before the first assignment, the value of a global variable is \nil.
116 155
117The unit of execution of Lua is called a \Def{chunk}. 156The unit of execution of Lua is called a \Def{chunk}.
@@ -125,7 +164,7 @@ for chunks is:
125\end{Produc}% 164\end{Produc}%
126A chunk may contain statements and function definitions, 165A chunk may contain statements and function definitions,
127and may be in a file or in a string inside the host program. 166and may be in a file or in a string inside the host program.
128A chunk may optionally ends with a return statement (\see{return}). 167A chunk may optionally end with a \verb|return| statement \see{return}.
129When a chunk is executed, first all its functions and statements are compiled, 168When a chunk is executed, first all its functions and statements are compiled,
130then the statements are executed in sequential order. 169then the statements are executed in sequential order.
131All modifications a chunk effects on the global environment persist 170All modifications a chunk effects on the global environment persist
@@ -133,7 +172,7 @@ after its end.
133Those include modifications to global variables and definitions 172Those include modifications to global variables and definitions
134of new functions% 173of new functions%
135\footnote{Actually, a function definition is an 174\footnote{Actually, a function definition is an
136assignment to a global variable; \see{TypesSec}.}. 175assignment to a global variable \see{TypesSec}.}.
137 176
138Chunks may be pre-compiled; see program \IndexVerb{luac} for details. 177Chunks may be pre-compiled; see program \IndexVerb{luac} for details.
139Text files with chunks and their binary pre-compiled forms 178Text files with chunks and their binary pre-compiled forms
@@ -151,10 +190,10 @@ Therefore, there are no type definitions in the language.
151There are seven \Index{basic types} in Lua: \Def{nil}, \Def{number}, 190There 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},
153and \Def{table}. 192and \Def{table}.
154{\em Nil} is the type of the value \nil, 193{\em Nil\/} is the type of the value \nil,
155whose main property is to be different from any other value. 194whose 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,
157while {\em string} has the usual meaning. 196while {\em string\/} has the usual meaning.
158 197
159Functions are considered first-class values in Lua. 198Functions are considered first-class values in Lua.
160This means that functions can be stored in variables, 199This means that functions can be stored in variables,
@@ -162,16 +201,16 @@ passed as arguments to other functions and returned as results.
162When a function is defined in Lua, its body is compiled and stored 201When a function is defined in Lua, its body is compiled and stored
163in a given variable. 202in a given variable.
164Lua can call (and manipulate) functions written in Lua and 203Lua can call (and manipulate) functions written in Lua and
165functions written in C; the latter have type {\em CFunction\/}. 204functions written in C; the latter have type {\em CFunction}.
166 205
167The type {\em userdata} is provided to allow 206The type {\em userdata\/} is provided to allow
168arbitrary \Index{C pointers} to be stored in Lua variables. 207arbitrary \Index{C pointers} to be stored in Lua variables.
169It corresponds to \verb'void*' and has no pre-defined operations in Lua, 208It corresponds to \verb'void*' and has no pre-defined operations in Lua,
170besides assignment and equality test. 209besides assignment and equality test.
171However, by using fallbacks, the programmer may define operations 210However, by using fallbacks, the programmer may define operations
172for {\em userdata} values; \see{fallback}. 211for {\em userdata\/} values; \see{fallback}.
173 212
174The type {\em table} implements \Index{associative arrays}, 213The type {\em table\/} implements \Index{associative arrays},
175that is, \Index{arrays} that can be indexed not only with numbers, 214that is, \Index{arrays} that can be indexed not only with numbers,
176but with any value (except \nil). 215but with any value (except \nil).
177Therefore, this type may be used not only to represent ordinary arrays, 216Therefore, 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)',
186which calls the method \verb'f' from the table \verb't' passing 225which calls the method \verb'f' from the table \verb't' passing
187itself as the first parameter. 226itself as the first parameter.
188 227
189It is important to notice that tables are objects, and not values. 228It is important to notice that tables are {\em objects}, and not values.
190Variables cannot contain tables, only references to them. 229Variables cannot contain tables, only {\em references\/} to them.
191Assignment, parameter passing and returns always manipulate references 230Assignment, parameter passing and returns always manipulate references
192to tables, and do not imply any kind of copy. 231to tables, and do not imply any kind of copy.
193Moreover, tables must be explicitly created before used 232Moreover, 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'[[ ... ]]'.
227Literals in this bracketed form may run for several lines, 266Literals in this bracketed form may run for several lines,
228may contain nested \verb'[[ ... ]]' pairs, 267may contain nested \verb'[[ ... ]]' pairs,
229and do not interpret escape sequences. 268and do not interpret escape sequences.
269This form is specially convenient for
270handling 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
232double hyphen (\verb'--') and run until the end of the line. 273double hyphen (\verb'--') and run until the end of the line.
233Moreover, if the first line of a chunk file starts with \verb'#', 274Moreover, if the first line of a chunk file starts with \verb'#',
234this line is skipped% 275this 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
236in Unix systems.}. 277in 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,
239and an optional decimal exponent. 280and 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
248Lua provides some automatic conversions. 289Lua provides some automatic conversions between values.
249Any arithmetic operation applied to a string tries to convert 290Any arithmetic operation applied to a string tries to convert
250that string to a number, following the usual rules. 291that string to a number, following the usual rules.
251Conversely, whenever a number is used when a string is expected, 292Conversely, 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;
254otherwise, it is formatted following the \verb'%g' 295otherwise, it is formatted following the \verb'%g'
255conversion specification of the \verb'printf' function in the 296conversion specification of the \verb'printf' function in the
256standard C library. 297standard C library.
257 298For complete control on how numbers are converted to strings,
299use 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
276Lua supports an almost conventional set of \Index{statements}. 318Lua supports an almost conventional set of \Index{statements},
319similar to those in Pascal or C.
277The conventional commands include 320The conventional commands include
278assignment, control structures and procedure calls. 321assignment, control structures and procedure calls.
279Non-conventional commands include table constructors 322Non-conventional commands include table constructors
280(Section~\ref{tableconstructor}), 323\see{tableconstructor},
281and local variable declarations (Section~\ref{localvar}). 324and local variable declarations \see{localvar}.
282 325
283\subsubsection{Blocks} 326\subsubsection{Blocks}
284A \Index{block} is a list of statements, which is executed sequentially. 327A \Index{block} is a list of statements, which are executed sequentially.
285Any statement can be optionally followed by a semicolon: 328Any 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}
310Before the assignment, the list of values is {\em adjusted} to 353Before the assignment, the list of values is {\em adjusted\/} to
311the length of the list of variables (\see{adjust}). 354the length of the list of variables \see{adjust}.
312 355
313A single name can denote a global or a local variable, 356A single name can denote a global or a local variable,
314or a formal parameter: 357or a formal parameter:
@@ -321,21 +364,21 @@ Square brackets are used to index a table:
321\end{Produc}% 364\end{Produc}%
322If \verb'var' results in a table value, 365If \verb'var' results in a table value,
323the field indexed by the expression value gets the assigned value. 366the field indexed by the expression value gets the assigned value.
324Otherwise, the fallback {\em settable} is called, 367Otherwise, the fallback {\em settable\/} is called,
325with three parameters: the value of \verb'var', 368with three parameters: the value of \verb'var',
326the value of expression, and the value being assigned to it; 369the value of expression, and the value being assigned to it;
327\see{fallback}. 370\see{fallback}.
328 371
329The syntax \verb'var.NAME' is just syntactic sugar for 372The 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}
336The \Index{condition expression} of a control structure can return any value. 379The \Index{condition expression} of a control structure may return any value.
337All values different from \nil\ are considered true; 380All values different from \nil\ are considered true;
338\nil\ is considered false. 381only \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}%
364Eventual returned values are thrown away. 407In this case, returned values are thrown away.
365Function calls are explained in Section \ref{functioncall}. 408Function 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.
369Their scope begins after the declaration and lasts until the 412Their scope begins after the declaration and lasts until the
370end of the block. 413end of the block.
371The declaration may include an initial assignment: 414The 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}
398Lua supports the usual \Index{arithmetic operators}. 441Lua supports the usual \Index{arithmetic operators}.
399These operators are the binary 442These operators are the binary
400\verb'+', \verb'-', \verb'*', \verb'/' and \verb'^' (exponentiation), 443\verb'+' (addition),
401and the unary \verb'-'. 444\verb'-' (subtraction),
445\verb'*' (multiplication),
446\verb'/' (division) and \verb'^' (exponentiation),
447and the unary \verb'-' (negation).
402If the operands are numbers, or strings that can be converted to 448If the operands are numbers, or strings that can be converted to
403numbers, according to the rules given in Section \ref{coercion}, 449numbers, according to the rules given in Section~\ref{coercion},
404then all operations but exponentiation have the usual meaning. 450then all operations except exponentiation have the usual meaning.
405Otherwise, the fallback ``arith'' is called (\see{fallback}). 451Otherwise, the fallback ``arith'' is called \see{fallback}.
406An exponentiation always calls this fallback. 452An exponentiation always calls this fallback.
407The standard mathematical library redefines this fallback, 453The standard mathematical library redefines this fallback,
408giving the expected meaning to \Index{exponentiation} 454giving the expected meaning to \Index{exponentiation}
409(\see{mathlib}). 455\see{mathlib}.
410 456
411\subsubsection{Relational Operators} 457\subsubsection{Relational Operators}
412Lua provides the following \Index{relational operators}: 458Lua provides the following \Index{relational operators}:
@@ -423,12 +469,18 @@ Numbers and strings are compared in the usual way.
423Tables, CFunctions, and functions are compared by reference, 469Tables, CFunctions, and functions are compared by reference,
424that is, two tables are considered equal only if they are the same table. 470that is, two tables are considered equal only if they are the same table.
425The operator \verb'~=' is exactly the negation of equality (\verb'=='). 471The operator \verb'~=' is exactly the negation of equality (\verb'==').
472Note that the conversion rules of Section~\ref{coercion}
473do not apply to equality comparisons.
474Thus, \verb|"0"==0| evaluates to false.
426 475
427The other operators work as follows. 476The other operators work as follows.
428If both arguments are numbers, then they are compared as such. 477If both arguments are numbers, then they are compared as such.
429Otherwise, if both arguments can be converted to strings, 478Otherwise, if both arguments can be converted to strings,
430their values are compared using lexicographical order. 479their values are compared using lexicographical order.
431Otherwise, the ``order'' fallback is called (\see{fallback}). 480Otherwise, 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}
434Like control structures, all logical operators 486Like control structures, all logical operators
@@ -451,8 +503,8 @@ the second operand is evaluated only if necessary.
451Lua offers a string \Index{concatenation} operator, 503Lua offers a string \Index{concatenation} operator,
452denoted by ``\IndexVerb{..}''. 504denoted by ``\IndexVerb{..}''.
453If operands are strings or numbers, then they are converted to 505If operands are strings or numbers, then they are converted to
454strings according to the rules in Section \ref{coercion}. 506strings according to the rules in Section~\ref{coercion}.
455Otherwise, the fallback ``concat'' is called (\see{fallback}). 507Otherwise, 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
487The form {\em lfieldlist1} is used to initialize lists. 539The 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}%
491The expressions in the list are assigned to consecutive numerical indexes, 543The expressions in the list are assigned to consecutive numerical indices,
492starting with 1. 544starting with 1.
493For example: 545For example:
494\begin{verbatim} 546\begin{verbatim}
495 a = {"v1", "v2", 34} 547 a = {"v1", "v2", 34}
496\end{verbatim} 548\end{verbatim}
497is roughly equivalent to: 549is 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}
515is roughly equivalent to: 567is 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}%
529Here, \verb'var' can be any variable (global, local, indexed, etc). 581Here, \verb'var' can be any variable (global, local, indexed, etc).
530If its value has type {\em function\/} or {\em CFunction\/}, 582If its value has type {\em function\/} or {\em CFunction},
531then this function is called. 583then this function is called.
532Otherwise, the ``function'' fallback is called, 584Otherwise, the ``function'' fallback is called,
533having as first parameter the value of \verb'var', 585having 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}%
553All argument expressions are evaluated before the call; 605All argument expressions are evaluated before the call;
554then the list of \Index{arguments} is adjusted to 606then the list of \Index{arguments} is adjusted to
555the length of the list of parameters (\see{adjust}); 607the length of the list of parameters \see{adjust};
556finally, this list is assigned to the formal parameters. 608finally, this list is assigned to the formal parameters.
557A call of the form \verb'f{...}' is syntactic sugar for 609A call of the form \verb'f{...}' is syntactic sugar for
558\verb'f({...})', that is, 610\verb'f({...})', that is,
559the parameter list is a single new table. 611the parameter list is a single new table.
560 612
561Because a function can return any number of results 613Because a function can return any number of results
562(\see{return}), 614\see{return},
563the number of results must be adjusted before used. 615the number of results must be adjusted before used.
564If the function is called as a statement (\see{funcstat}), 616If the function is called as a statement \see{funcstat},
565its return list is adjusted to 0. 617its return list is adjusted to 0,
618thus discarding all returned values.
566If the function is called in a place that needs a single value 619If 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'),
568then its return list is adjusted to 1. 621then its return list is adjusted to 1,
622thus discarding all returned values,
623except the first one.
569If the function is called in a place that can hold many values 624If 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'),
571then no adjustment is made. 626then no adjustment is made.
@@ -585,6 +640,8 @@ all its function bodies are pre-compiled, too.
585Then, when Lua ``executes'' the function definition, 640Then, when Lua ``executes'' the function definition,
586its body is stored, with type {\em function}, 641its body is stored, with type {\em function},
587into the variable \verb'var'. 642into the variable \verb'var'.
643It is in this sense that
644a function definition is an assignment to a global variable.
588 645
589Parameters act as local variables, 646Parameters act as local variables,
590initialized with the argument values. 647initialized 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
595Results are returned using the \verb'return' statement (\see{return}). 652Results are returned using the \verb'return' statement \see{return}.
596If control reaches the end of a function without a return instruction, 653If control reaches the end of a function without a return instruction,
597then the function returns with no results. 654then 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}
633called when an arithmetic operation is applied to non numerical operands, 690called when an arithmetic operation is applied to non numerical operands,
634or when the binary \verb'^' operation is called. 691or when the binary \verb'^' operation (exponentiation) is called.
635It receives three arguments: 692It receives three arguments:
636the two operands (the second one is nil when the operation is unary minus) 693the two operands (the second one is \nil\ when the operation is unary minus)
637and one of the following strings describing the offended operator: 694and 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
661not present in a table. 718not present in a table.
662It receives as arguments the table and the index. 719It receives as arguments the table and the index.
663Its return value is the final result of the indexing operation. 720Its return value is the final result of the indexing operation.
664The default handler returns nil. 721The default handler returns \nil.
665\item[``getglobal'':]\index{index getglobal} 722\item[``getglobal'':]\index{index getglobal}
666called when Lua tries to retrieve the value of a global variable 723called when Lua tries to retrieve the value of a global variable
667which has a nil value (or which has not been initialized). 724which has a \nil\ value (or which has not been initialized).
668It receives as argument the name of the variable. 725It receives as argument the name of the variable.
669Its return value is the final result of the expression. 726Its return value is the final result of the expression.
670The default handler returns nil. 727The default handler returns \nil.
671\item[``gettable'':]\index{gettable fallback} 728\item[``gettable'':]\index{gettable fallback}
672called when Lua tries to index a non table value. 729called when Lua tries to index a non table value.
673It receives as arguments the non table value and the index. 730It receives as arguments the non table value and the index.
674Its return value is the final result of the indexing operation. 731Its return value is the final result of the indexing operation.
675The default handler issues an error. 732The default handler issues an error.
676\item[``settable'':]\index{settable fallback} 733\item[``settable'':]\index{settable fallback}
677called when Lua tries to assign indexed a non table value. 734called when Lua tries to assign to an index in a non table value.
678It receives as arguments the non table value, 735It receives as arguments the non table value,
679the index, and the assigned value. 736the index, and the assigned value.
680The default handler issues an error. 737The default handler issues an error.
@@ -687,7 +744,8 @@ The default handler issues an error.
687\item[``gc'':] 744\item[``gc'':]
688called during garbage collection. 745called during garbage collection.
689It receives as argument the table being collected. 746It receives as argument the table being collected.
690After each run of the collector this function is called with argument nil. 747After each run of the collector this function is called with argument \nil,
748to signal the completion of the garbage collection.
691Because this function operates during garbage collection, 749Because this function operates during garbage collection,
692it must be used with great care, 750it must be used with great care,
693and programmers should avoid the creation of new objects 751and 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}
697called when an error occurs. 755called when an error occurs.
698It receives as argument a string describing the error. 756It receives as argument a string describing the error.
699The default handler prints the message on the standard error output. 757The default handler prints the message on the standard error output
758(\verb|stderr|).
700\end{description} 759\end{description}
701 760
702The function \IndexVerb{setfallback} is used to change a fallback handler. 761The function \IndexVerb{setfallback} is used to change a fallback handler.
@@ -704,7 +763,7 @@ Its first argument is the name of a fallback condition,
704and the second argument is the new function to be called. 763and the second argument is the new function to be called.
705It returns the old handler function for the given fallback. 764It returns the old handler function for the given fallback.
706 765
707Section \ref{exfallback} shows an example of the use of fallbacks. 766Section~\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.
712Because Lua is an extension language, 771Because Lua is an extension language,
713all Lua actions start from C code calling a function from the Lua library. 772all Lua actions start from C code calling a function from the Lua library.
714Whenever an error occurs during Lua compilation or execution, 773Whenever an error occurs during Lua compilation or execution,
715an ``error'' fallback function is called, 774the ``error'' fallback function is called,
716and then the corresponding function from the library 775and 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.
721The only argument to the ``error'' fallback function is a string 780The only argument to the ``error'' fallback function is a string
722describing the error. 781describing the error.
723The standard I/O library redefines this fallback, 782The standard I/O library redefines this fallback,
724using the debug facilities (\see{debugI}), 783using the debug facilities \see{debugI},
725in order to print some extra information, 784in order to print some extra information,
726like the call stack. 785like the call stack.
727For more information about an error, 786To provide more information about errors,
728the Lua program can include the compilation pragma \verb'$debug'. 787Lua programs can include the compilation pragma \verb'$debug'.
729\index{debug pragma}\label{pragma} 788\index{debug pragma}\label{pragma}
730This pragma must be written in a line by itself. 789This pragma must be written in a line by itself.
731When an error occurs in a program compiled with this option, 790When an error occurs in a program compiled with this option,
732the error routine is able to print also the lines where the calls 791the 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.
734If needed, it is possible to change the ``error'' fallback handler 793If needed, it is possible to change the ``error'' fallback handler
735(\see{fallback}). 794\see{fallback}.
736 795
737Lua code can explicitly generate an error by calling the built-in 796Lua code can explicitly generate an error by calling the built-in
738function \verb'error' (\see{pdf-error}). 797function \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}
755All API functions are declared in the header file \verb'lua.h'. 814All API functions and related types and constants
815are declared in the header file \verb'lua.h'.
756 816
757\subsection{Executing Lua Code} 817\subsection{Executing Lua Code}
758A host program can execute Lua chunks written in a file or in a string, 818A host program can execute Lua chunks written in a file or in a string
759using the following functions: 819using 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:
7660, in case of success; non zero, in case of errors. 8260, in case of success; non zero, in case of errors.
767More specifically, \verb'lua_dofile' returns 2 if for any reason 827More specifically, \verb'lua_dofile' returns 2 if for any reason
768it could not open the file. 828it could not open the file.
769The function \verb'lua_dofile', if called with argument \verb'NULL' (0), 829The function \verb'lua_dofile', if called with argument \verb'NULL',
770executes the {\tt stdin} stream. 830executes the \verb|stdin| stream.
771Function \verb'lua_dofile' is also able to execute pre-compiled chunks. 831Function \verb'lua_dofile' is also able to execute pre-compiled chunks.
772It automatically detects whether the file is text or binary, 832It automatically detects whether the file is text or binary,
773and loads it accordingly (see program \IndexVerb{luac}). 833and loads it accordingly (see program \IndexVerb{luac}).
@@ -779,14 +839,14 @@ all values passed between Lua and C have type
779which works like an abstract type in C that can hold any Lua value. 839which works like an abstract type in C that can hold any Lua value.
780Values of type \verb'lua_Object' have no meaning outside Lua; 840Values of type \verb'lua_Object' have no meaning outside Lua;
781for instance, 841for instance,
782the comparisson of two \verb"lua_Object's" is of no significance. 842the comparisson of two \verb"lua_Object's" is undefined.
783 843
784Because Lua has automatic memory management and garbage collection, 844Because Lua has automatic memory management and garbage collection,
785a \verb'lua_Object' has a limited scope, 845a \verb'lua_Object' has a limited scope,
786and is only valid inside the {\em block\/} where it was created. 846and is only valid inside the {\em block\/} where it was created.
787A C function called from Lua is a block, 847A C function called from Lua is a block,
788and its parameters are valid only until its end. 848and its parameters are valid only until its end.
789A good programming practice is to convert Lua objects to C values 849It is good programming practice to convert Lua objects to C values
790as soon as they are available, 850as soon as they are available,
791and never to store \verb'lua_Object's in C global variables. 851and 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}
802After the end of the block, 862After the end of the block,
803all \verb'lua_Object''s created inside it are released. 863all \verb'lua_Object''s created inside it are released.
864The use of explicit nested blocks is encouraged.
804 865
805To check the type of a \verb'lua_Object', 866To check the type of a \verb'lua_Object',
806the following function is available: 867the following function is available:
@@ -824,7 +885,8 @@ int lua_isuserdata (lua_Object object);
824All macros return 1 if the object is compatible with the given type, 885All macros return 1 if the object is compatible with the given type,
825and 0 otherwise. 886and 0 otherwise.
826The function \verb'lua_isnumber' accepts numbers and numerical strings, 887The function \verb'lua_isnumber' accepts numbers and numerical strings,
827\verb'lua_isstring' accepts strings and numbers (\see{coercion}), 888whereas
889\verb'lua_isstring' accepts strings and numbers \see{coercion},
828and \verb'lua_isfunction' accepts Lua and C functions. 890and \verb'lua_isfunction' accepts Lua and C functions.
829The function \verb'lua_type' can be used to distinguish between 891The function \verb'lua_type' can be used to distinguish between
830different kinds of user data. 892different kinds of user data.
@@ -839,13 +901,13 @@ char *lua_getstring (lua_Object object);
839lua_CFunction lua_getcfunction (lua_Object object); 901lua_CFunction lua_getcfunction (lua_Object object);
840void *lua_getuserdata (lua_Object object); 902void *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.
843This \verb'lua_Object' must be a number or a string convertible to number 905This \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 *').
847This \verb'lua_Object' must be a string or a number; 909This \verb'lua_Object' must be a string or a number;
848otherwise, the function returns 0 (the null pointer). 910otherwise, the function returns 0 (the \verb|NULL| pointer).
849This function does not create a new string, but returns a pointer to 911This function does not create a new string, but returns a pointer to
850a string inside the Lua environment. 912a string inside the Lua environment.
851Because Lua has garbage collection, there is no guarantee that such 913Because 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.
855This \verb'lua_Object' must have type {\em CFunction\/}; 917This \verb'lua_Object' must have type {\em CFunction\/};
856otherwise, the function returns 0 (the null pointer). 918otherwise, the function returns 0 (the \verb|NULL| pointer).
857The type \verb'lua_CFunction' is explained in Section~\ref{LuacallC}. 919The 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*'.
860This \verb'lua_Object' must have type {\em userdata\/}; 922This \verb'lua_Object' must have type {\em userdata\/};
861otherwise, the function returns 0 (the null pointer). 923otherwise, the function returns 0 (the \verb|NULL| pointer).
862 924
863The reverse process, that is, passing a specific C value to Lua, 925The reverse process, that is, passing a specific C value to Lua,
864is done by using the following functions: 926is done by using the following functions:
@@ -882,7 +944,7 @@ where it can be assigned to a Lua variable,
882passed as parameter to a Lua function, etc. \label{pushing} 944passed as parameter to a Lua function, etc. \label{pushing}
883 945
884User data can have different tags, 946User data can have different tags,
885whose semantics are defined by the host program. 947whose semantics are only known to the host program.
886Any positive integer can be used to tag a user datum. 948Any positive integer can be used to tag a user datum.
887When a user datum is retrieved, 949When a user datum is retrieved,
888the function \verb'lua_type' can be used to get its tag. 950the function \verb'lua_type' can be used to get its tag.
@@ -927,29 +989,31 @@ or the index is not present in the table,
927the corresponding fallback is called. 989the corresponding fallback is called.
928 990
929To store a value in an index, 991To store a value in an index,
930the program must push onto the stack the table, the index, 992the program must push the table, the index, and the value onto the stack,
931and the value,
932and then call the function: 993and then call the function:
933\Deffunc{lua_storesubscript} 994\Deffunc{lua_storesubscript}
934\begin{verbatim} 995\begin{verbatim}
935void lua_storesubscript (void); 996void lua_storesubscript (void);
936\end{verbatim} 997\end{verbatim}
937Again, the corresponding fallback is called if needed. 998Again, the ``settable'' fallback is called if a non-table value is used.
938 999
939Finally, the function 1000Finally, the function
940\Deffunc{lua_createtable} 1001\Deffunc{lua_createtable}
941\begin{verbatim} 1002\begin{verbatim}
942lua_Object lua_createtable (void); 1003lua_Object lua_createtable (void);
943\end{verbatim} 1004\end{verbatim}
944creates and returns a new table. 1005creates and returns a new, empty table.
945 1006
946{\em Please Notice:\/} 1007\begin{quotation}
1008\noindent
1009{\em Please note\/}:
947Most functions from the Lua library receive parameters through Lua's stack. 1010Most functions from the Lua library receive parameters through Lua's stack.
948Because other functions also use this stack, 1011Because other functions also use this stack,
949it is important that these 1012it is important that these
950parameters be pushed just before the corresponding call, 1013parameters be pushed just before the corresponding call,
951without intermediate calls to the Lua library. 1014without intermediate calls to the Lua library.
952For instance, suppose the user wants the value of \verb'a[i]'. 1015For instance, suppose the user wants the value of \verb'a[i]',
1016where \verb'a' and \verb'i' are global Lua variables.
953A simplistic solution would be: 1017A 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}
961However, the call \verb'lua_getglobal("i")' modifies the stack, 1025This code is incorrect because
1026the call \verb'lua_getglobal("i")' modifies the stack,
962and invalidates the previous pushed value. 1027and invalidates the previous pushed value.
963A correct solution could be: 1028A 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}
971The functions \verb|lua_getnumber|, \verb|lua_getstring|, 1036The functions {\em lua\_getnumber}, {\em lua\_getstring},
972 \verb|lua_getuserdata|, and \verb|lua_getcfunction|, 1037{\em lua\_getuserdata}, and {\em lua\_getcfunction},
973plus the family \verb|lua_is*|, 1038plus the family \verb|lua_is*|,
974are safe to be called without modifying the stack. 1039are safe to be called without modifying the stack.
1040\end{quotation}
975 1041
976\subsection{Calling Lua Functions} 1042\subsection{Calling Lua Functions}
977Functions defined in Lua by a chunk executed with 1043Functions 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.
979This is done using the following protocol: 1045This is done using the following protocol:
980first, the arguments to the function are pushed onto the Lua stack 1046first, 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.
982Again, it is important to emphasize that, during this phase, 1048Again, it is important to emphasize that, during this phase,
983no other Lua function can be called. 1049no other Lua function can be called.
984 1050
@@ -1012,20 +1078,21 @@ void lua_error (char *message);
1012\end{verbatim} 1078\end{verbatim}
1013This function never returns. 1079This function never returns.
1014If the C function has been called from Lua, 1080If the C function has been called from Lua,
1015the corresponding Lua execution terminates, 1081then the corresponding Lua execution terminates,
1016as if an error had occurred inside Lua code. 1082as if an error had occurred inside Lua code.
1017Otherwise, the whole program terminates. 1083Otherwise, 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
1019Fallbacks can be changed with: 1086Fallbacks can be changed with:
1020\Deffunc{lua_setfallback} 1087\Deffunc{lua_setfallback}
1021\begin{verbatim} 1088\begin{verbatim}
1022lua_Object lua_setfallback (char *name, lua_CFunction fallback); 1089lua_Object lua_setfallback (char *name, lua_CFunction fallback);
1023\end{verbatim} 1090\end{verbatim}
1024The first parameter is the fallback name, 1091The first parameter is the fallback name \see{fallback},
1025and the second a CFunction to be used as the new fallback. 1092and the second is a CFunction to be used as the new fallback.
1026This function returns a \verb'lua_Object', 1093This function returns a \verb'lua_Object',
1027which is the old fallback value, 1094which is the old fallback value,
1028or \nil\ on fail (invalid fallback name). 1095or \nil\ on failure (invalid fallback name).
1029This old value can be used for chaining fallbacks. 1096This old value can be used for chaining fallbacks.
1030 1097
1031An example of C code calling a Lua function is shown in 1098An 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}.
1067In this way, it is possible to write functions that work with 1134In this way, it is possible to write functions that work with
1068a variable number of parameters. 1135a variable number of parameters.
1136The funcion \verb|lua_getparam| can be called in any order,
1137and many times for the same index.
1069 1138
1070To return values, a C function just pushes them onto the stack, 1139To return values, a C function just pushes them onto the stack,
1071in direct order (\see{valuesCLua}). 1140in direct order \see{valuesCLua}.
1072Like a Lua function, a C function called by Lua can also return 1141Like a Lua function, a C function called by Lua can also return
1073many results. 1142many results.
1074 1143
@@ -1093,8 +1162,8 @@ void lua_unref (int ref);
1093The function \verb'lua_ref' creates a reference 1162The function \verb'lua_ref' creates a reference
1094to the object that is on the top of the stack, 1163to the object that is on the top of the stack,
1095and returns this reference. 1164and returns this reference.
1096If \verb'lock' is true, the object is {\em locked}: 1165If \verb'lock' is true, the object is {\em locked\/}:
1097that means the object will not be garbage collected. 1166this means the object will not be garbage collected.
1098Notice that an unlocked reference may be garbage collected. 1167Notice that an unlocked reference may be garbage collected.
1099Whenever the referenced object is needed, 1168Whenever the referenced object is needed,
1100a call to \verb'lua_getref' 1169a 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
1114The set of \Index{predefined functions} in Lua is small but powerful. 1183The set of \Index{predefined functions} in Lua is small but powerful.
1115Most of them provide features that allows some degree of 1184Most of them provide features that allow some degree of
1116\Index{reflexivity} in the language. 1185\Index{reflexivity} in the language.
1117Some of these features cannot be simulated with the rest of the 1186Some of these features cannot be simulated with the rest of the
1118Language nor with the standard Lua API. 1187Language nor with the standard Lua API.
@@ -1121,11 +1190,11 @@ Others are just convenient interfaces to common API functions.
1121The libraries, on the other hand, provide useful routines 1190The libraries, on the other hand, provide useful routines
1122that are implemented directly through the standard API. 1191that are implemented directly through the standard API.
1123Therefore, they are not necessary to the language, 1192Therefore, they are not necessary to the language,
1124and are provided as separated C modules. 1193and are provided as separate C modules.
1125Currently there are three standard libraries: 1194Currently 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}
1131In order to have access to these libraries, 1200In order to have access to these libraries,
@@ -1146,12 +1215,14 @@ If there is any error executing the file, it returns \nil.
1146Otherwise, it returns the values returned by the chunk, 1215Otherwise, it returns the values returned by the chunk,
1147or a non \nil\ value if the chunk returns no values. 1216or a non \nil\ value if the chunk returns no values.
1148It issues an error when called with a non string argument. 1217It 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}
1151This function executes a given string as a Lua chunk. 1221This function executes a given string as a Lua chunk.
1152If there is any error executing the string, it returns \nil. 1222If there is any error executing the string, it returns \nil.
1153Otherwise, it returns the values returned by the chunk, 1223Otherwise, it returns the values returned by the chunk,
1154or a non \nil\ value if the chunk returns no values. 1224or 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}
1157This function allows a program to traverse all fields of a table. 1228This function allows a program to traverse all fields of a table.
@@ -1164,15 +1235,16 @@ the function returns the first index
1164of the table (and its associated value). 1235of the table (and its associated value).
1165When called with the last index, or with \nil\ in an empty table, 1236When called with the last index, or with \nil\ in an empty table,
1166it returns \nil. 1237it returns \nil.
1238This function cannot be written with the standard API.
1167 1239
1168In Lua there is no declaration of fields; 1240In Lua there is no declaration of fields;
1169semantically, there is no difference between a 1241semantically, there is no difference between a
1170field not present in a table or a field with value \nil. 1242field not present in a table or a field with value \nil.
1171Therefore, the function only considers fields with non nil values. 1243Therefore, the function only considers fields with non \nil\ values.
1172The order the indices are enumerated is not specified, 1244The order in which the indices are enumerated is not specified,
1173{\em even for numeric indices}. 1245{\em even for numeric indices}.
1174 1246
1175See Section \ref{exnext} for an example of the use of this function. 1247See 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}
1178This function is similar to the function \verb'next', 1250This function is similar to the function \verb'next',
@@ -1182,7 +1254,8 @@ or \nil\ to get a first name.
1182Similarly to \verb'next', it returns the name of another variable 1254Similarly to \verb'next', it returns the name of another variable
1183and its value, 1255and its value,
1184or \nil\ if there are no more variables. 1256or \nil\ if there are no more variables.
1185See Section \ref{exnext} for an example of the use of this function. 1257See Section~\ref{exnext} for an example of the use of this function.
1258This 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}
1188This function receives an argument of any type and 1261This function receives an argument of any type and
@@ -1201,7 +1274,7 @@ See Section~\ref{libio} for functions for formatted output.
1201This function receives one argument, 1274This function receives one argument,
1202and tries to convert it to a number. 1275and tries to convert it to a number.
1203If the argument is already a number or a string convertible 1276If the argument is already a number or a string convertible
1204to a number (\see{coercion}), then it returns that number; 1277to a number \see{coercion}, then it returns that number;
1205otherwise, it returns \nil. 1278otherwise, 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
1221data with different tags, 1294data with different tags,
1222and between C functions and Lua functions. 1295and 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}
1225This function issues an {\em ``assertion failed!''} error 1300This function issues an {\em ``assertion failed!''} error
1226when its argument is \nil. 1301when its argument is \nil.
@@ -1230,6 +1305,7 @@ This function issues an error message and terminates
1230the last called function from the library 1305the last called function from the library
1231(\verb'lua_dofile', \verb'lua_dostring', \ldots). 1306(\verb'lua_dofile', \verb'lua_dostring', \ldots).
1232It never returns. 1307It 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}
1235This function assigns the given value to a global variable. 1311This 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.
1237Therefore, this function can set global variables with strange names like 1313Therefore, 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'.
1239It returns the value of its second argument. 1315It 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}
1242This function retrieves the value of a global variable. 1319This 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}
1247This function sets a new fallback function to the given fallback. 1324This function sets a new fallback function to the given fallback.
1248It returns the old fallback function. 1325It 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}
1251This library provides generic functions for string manipulation, 1329This library provides generic functions for string manipulation,
1252such as finding and extracting substrings and pattern matching. 1330such as finding and extracting substrings and pattern matching.
1253When indexing a string, the first character has position 1. 1331When indexing a string, the first character is at position 1,
1254See Page~\pageref{pm} for an explanation about patterns, 1332not 0, as in C.
1333See page~\pageref{pm} for an explanation about patterns,
1255and Section~\ref{exstring} for some examples on string manipulation 1334and Section~\ref{exstring} for some examples on string manipulation
1256in Lua. 1335in 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}
1260This function looks for the first {\em match} of 1339This function looks for the first {\em match\/} of
1261\verb-pattern- in \verb-str-. 1340\verb-pattern- in \verb-str-.
1262If it finds one, it returns the indexes on \verb-str- 1341If it finds one, then it returns the indices on \verb-str-
1263where this occurence starts and ends; 1342where this occurence starts and ends;
1264otherwise, it returns \nil. 1343otherwise, it returns \nil.
1265If the pattern specifies captures, 1344If the pattern specifies captures,
@@ -1298,7 +1377,7 @@ Returns a string which is the concatenation of \verb-n- copies of
1298the string \verb-s-. 1377the string \verb-s-.
1299 1378
1300\subsubsection*{\ff{\tt ascii (s [, i])}}\Deffunc{ascii} 1379\subsubsection*{\ff{\tt ascii (s [, i])}}\Deffunc{ascii}
1301Returns the ascii code of the character \verb's[i]'. 1380Returns the ASCII code of the character \verb's[i]'.
1302If \verb'i' is absent, then it is assumed to be 1. 1381If \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
1332expect a number as argument, 1411expect a number as argument,
1333whereas \verb'q' and \verb's' expect a string. 1412whereas \verb'q' and \verb's' expect a string.
1413Note that the \verb'*' modifier can be simulated by building
1414the appropriate format string.
1415For 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}
1336Returns a copy of \verb-s-, 1419Returns a copy of \verb-s-,
@@ -1339,15 +1422,16 @@ replaced by a replacement string specified by \verb-repl-.
1339This function also returns, as a second value, 1422This function also returns, as a second value,
1340the total number of substitutions made. 1423the total number of substitutions made.
1341 1424
1342If \verb-repl- is a string, its value is used for replacement. 1425If \verb-repl- is a string, then its value is used for replacement.
1343Any sequence in \verb-repl- of the form \verb-%n- 1426Any sequence in \verb-repl- of the form \verb-%n-
1344with \verb-n- between 1 and 9 1427with \verb-n- between 1 and 9
1345stands for the value of the n-th captured substring. 1428stands for the value of the n-th captured substring.
1346 1429
1347If \verb-repl- is a function, this function is called every time a 1430If \verb-repl- is a function, then this function is called every time a
1348match occurs, with all captured substrings as parameters. 1431match occurs, with all captured substrings as parameters
1432(see below).
1349If the value returned by this function is a string, 1433If the value returned by this function is a string,
1350it is used as the replacement string; 1434then it is used as the replacement string;
1351otherwise, the replacement string is the empty string. 1435otherwise, the replacement string is the empty string.
1352 1436
1353An optional parameter \verb-n- limits 1437An 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
1358As an example, in the following expression each occurrence of the form 1442As 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|,
1360passing \verb|name| as argument 1444passing \verb|name| as argument
1361(because only this part of the pattern is captured). 1445(because only this part of the pattern is captured).
1362The value returned by \verb|getenv| will replace the pattern. 1446The value returned by \verb|getenv| will replace the pattern.
1363Therefore, the whole expression: 1447Therefore, 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}
1367may return the string: 1451may 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) ---
1394represents the character {\em x}. 1478represents the character {\em x}.
1479This is the standard way to escape the magic characters \verb'()%.[*?'.
1395\item[{\tt [char-set]}] --- 1480\item[{\tt [char-set]}] ---
1396Represents the class which is the union of all 1481Represents the class which is the union of all
1397characters in char-set. 1482characters in char-set.
@@ -1419,7 +1504,8 @@ A character class followed by \verb'?' matches 0 or one occurrence
1419of a character in the class. 1504of a character in the class.
1420A pattern item may also has the form \verb'%n', 1505A pattern item may also has the form \verb'%n',
1421for \verb-n- between 1 and 9; 1506for \verb-n- between 1 and 9;
1422such item matches a sub-string equal to the n-th captured string. 1507such item matches a sub-string equal to the n-th captured string
1508(see below).
1423 1509
1424\paragraph{Pattern:} 1510\paragraph{Pattern:}
1425a \Def{pattern} is a sequence of pattern items. 1511a \Def{pattern} is a sequence of pattern items.
@@ -1433,14 +1519,18 @@ end of the subject string.
1433A pattern may contain sub-patterns enclosed in parentheses, 1519A pattern may contain sub-patterns enclosed in parentheses,
1434that describe \Def{captures}. 1520that describe \Def{captures}.
1435When a match succeeds, the sub-strings of the subject string 1521When a match succeeds, the sub-strings of the subject string
1436that match captures are {\em captured} for future use. 1522that match captures are {\em captured\/} for future use.
1437Captures are numbered according to their left parentheses. 1523Captures are numbered according to their left parentheses.
1524For instance, in the pattern \verb|"(a*(.)%w(%s*))"|,
1525the 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
1441This library is an interface to some functions of the standard C math library. 1531This library is an interface to some functions of the standard C math library.
1442Moreover, it registers a fallback for the binary operator \verb'^' which, 1532In addition, it registers a fallback for the binary operator \verb'^' that,
1443when applied to numbers \verb'x^y', returns $x^y$. 1533returns $x^y$ when applied to numbers \verb'x^y'.
1444 1534
1445The library provides the following functions: 1535The 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}
1452abs acos asin atan atan2 ceil cos floor log log10 1542abs acos asin atan atan2 ceil cos floor log log10
1453max min mod sin sqrt tan random randomseed 1543max min mod sin sqrt tan random randomseed
1454\end{verbatim} 1544\end{verbatim}
1455Most of them 1545Most of them
1456are only interfaces to the homonymous functions in the C library, 1546are only interfaces to the homonymous functions in the C library,
1457except that, for the trigonometric functions, 1547except that, for the trigonometric functions,
1458all angles are expressed in degrees, not radians. 1548all angles are expressed in {\em degrees}, not radians.
1459 1549
1460The function \verb'max' returns the maximum 1550The function \verb'max' returns the maximum
1461value of its numeric arguments. 1551value 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
1474All I/O operations in Lua are done over two {\em current} files: 1564All input and outpu operations in Lua are done over two {\em current\/} files:
1475one for reading and one for writing. 1565one for reading and one for writing.
1476Initially, the current input file is \verb'stdin', 1566Initially, the current input file is \verb'stdin',
1477and the current output file is \verb'stdout'. 1567and the current output file is \verb'stdout'.
@@ -1485,9 +1575,10 @@ some value different from \nil\ on success.
1485This function may be called in three ways. 1575This function may be called in three ways.
1486When called with a file name, 1576When called with a file name,
1487it opens the named file, 1577it opens the named file,
1488sets it as the {\em current} input file, 1578sets it as the {\em current\/} input file,
1489and returns a {\em handle} to the file 1579and 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*|).
1581It does not close the current input file.
1491When called with a file handle, returned by a previous call, 1582When called with a file handle, returned by a previous call,
1492it restores the file as the current input. 1583it restores the file as the current input.
1493When called without parameters, 1584When called without parameters,
@@ -1497,36 +1588,52 @@ and restores \verb'stdin' as the current input file.
1497If this function fails, it returns \nil, 1588If this function fails, it returns \nil,
1498plus a string describing the error. 1589plus 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'|',
1501then a \Index{piped input} is open, via function \IndexVerb{popen}. 1594then a \Index{piped input} is open, via function \IndexVerb{popen}.
1595Not all systems implement pipes.
1596Moreover,
1597the number of files that can be open at the same time is usually limited and
1598depends 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
1505This function may be called in three ways. 1603This function may be called in three ways.
1506When called with a file name, 1604When called with a file name,
1507it opens the named file, 1605it opens the named file,
1508sets it as the {\em current} output file, 1606sets it as the {\em current\/} output file,
1509and returns a {\em handle} to the file 1607and 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*|).
1609It does not close the current output file.
1511Notice that, if the file already exists, 1610Notice that, if the file already exists,
1512it will be {\em completely erased} with this operation. 1611it will be {\em completely erased\/} with this operation.
1513When called with a file handle, returned by a previous call, 1612When called with a file handle, returned by a previous call,
1514it restores the file as the current output. 1613it restores the file as the current output.
1515When called without parameters, 1614When called without parameters,
1516this function closes the current output file, 1615this function closes the current output file,
1517and restores \verb'stdout' as the current output file. 1616and 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
1520If this function fails, it returns \nil, 1620If this function fails, it returns \nil,
1521plus a string describing the error. 1621plus 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'|',
1524then a \Index{piped output} is open, via function \IndexVerb{popen}. 1626then a \Index{piped output} is open, via function \IndexVerb{popen}.
1627Not all systems implement pipes.
1628Moreover,
1629the number of files that can be open at the same time is usually limited and
1630depends 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
1528This function opens a file named \verb'filename' and sets it as the 1635This function opens a file named \verb'filename' and sets it as the
1529{\em current} output file. 1636{\em current\/} output file.
1530It returns the file handle, 1637It returns the file handle,
1531or \nil\ in case of error. 1638or \nil\ in case of error.
1532Unlike the \verb'writeto' operation, 1639Unlike the \verb'writeto' operation,
@@ -1534,7 +1641,7 @@ this function does not erase any previous content of the file.
1534If this function fails, it returns \nil, 1641If this function fails, it returns \nil,
1535plus a string describing the error. 1642plus a string describing the error.
1536 1643
1537Notice that function \verb|writeto| is available to close a file. 1644Notice 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
1547This function renames file \verb'name1' to \verb'name2'. 1654This function renames file named \verb'name1' to \verb'name2'.
1548If this function fails, it returns \nil, 1655If this function fails, it returns \nil,
1549plus a string describing the error. 1656plus a string describing the error.
1550 1657
@@ -1560,6 +1667,7 @@ according to a read pattern, that specifies how much to read;
1560characters are read from the current input file until 1667characters are read from the current input file until
1561the read pattern fails or ends. 1668the read pattern fails or ends.
1562The function \verb|read| returns a string with the characters read, 1669The function \verb|read| returns a string with the characters read,
1670even if the pattern succeeds only partially,
1563or \nil\ if the read pattern fails {\em and\/} 1671or \nil\ if the read pattern fails {\em and\/}
1564the result string would be empty. 1672the result string would be empty.
1565When called without parameters, 1673When called without parameters,
@@ -1580,7 +1688,9 @@ since it can match a sequence of zero characteres, it never fails.%
1580\footnote{ 1688\footnote{
1581Notice that this behaviour is different from regular pattern matching, 1689Notice that this behaviour is different from regular pattern matching,
1582where a \verb'*' expands to the maximum length {\em such that\/} 1690where a \verb'*' expands to the maximum length {\em such that\/}
1583the rest of the pattern does not fail.} 1691the rest of the pattern does not fail.
1692Therefore, there is no need for backtracking the reading.
1693}
1584 1694
1585A pattern item may contain sub-patterns enclosed in curly brackets, 1695A pattern item may contain sub-patterns enclosed in curly brackets,
1586that describe \Def{skips}. 1696that describe \Def{skips}.
@@ -1606,6 +1716,8 @@ or \nil\ if the next characters do not conform to an integer format.
1606This function writes the value of each of its arguments to the 1716This function writes the value of each of its arguments to the
1607current output file. 1717current output file.
1608The arguments must be strings or numbers. 1718The arguments must be strings or numbers.
1719To write other values,
1720use \verb|tostring| before \verb|write|.
1609If this function fails, it returns \nil, 1721If this function fails, it returns \nil,
1610plus a string describing the error. 1722plus a string describing the error.
1611 1723
@@ -1615,13 +1727,15 @@ This function returns a string containing date and time
1615formatted according to the given string \verb'format', 1727formatted according to the given string \verb'format',
1616following the same rules of the ANSI C function \verb'strftime'. 1728following the same rules of the ANSI C function \verb'strftime'.
1617When called without arguments, 1729When called without arguments,
1618it returns a reasonable date and time representation. 1730it returns a reasonable date and time representation that depends on
1731the host system.
1619 1732
1620\subsubsection*{\ff{\tt exit ([code])}}\Deffunc{exit} 1733\subsubsection*{\ff{\tt exit ([code])}}\Deffunc{exit}
1621 1734
1622This function calls the C function \verb-exit-, 1735This function calls the C function \verb-exit-,
1623with an optional \verb-code-, 1736with an optional \verb-code-,
1624to terminate the program. 1737to terminate the program.
1738The 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
1633This function is equivalent to the C function \verb|system|. 1747This function is equivalent to the C function \verb|system|.
1634It passes \verb|command| to be executed by an Operating System Shell. 1748It passes \verb|command| to be executed by an operating system shell.
1635It returns an error code, which is implementation-defined. 1749It 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
1640Lua has no built-in debugger facilities. 1754Lua has no built-in debugging facilities.
1641Instead, it offers a special interface, 1755Instead, it offers a special interface,
1642by means of functions and {\em hooks}, 1756by means of functions and {\em hooks},
1643which allows the construction of different 1757which allows the construction of different
@@ -1663,7 +1777,8 @@ The type \verb'lua_Function' is just another name
1663to \verb'lua_Object'. 1777to \verb'lua_Object'.
1664Although, in this library, 1778Although, in this library,
1665a \verb'lua_Function' can be used wherever a \verb'lua_Object' is required, 1779a \verb'lua_Function' can be used wherever a \verb'lua_Object' is required,
1666a parameter \verb'lua_Function' accepts only a handle returned by 1780when a parameter has type \verb'lua_Function'
1781it accepts only a handle returned by
1667\verb'lua_stackedfunction'. 1782\verb'lua_stackedfunction'.
1668 1783
1669Three other functions produce extra information about a function: 1784Three 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
1682The function \verb'lua_currentline' gives the current line where 1797The function \verb'lua_currentline' gives the current line where
1683a given function is executing. 1798a given function is executing.
1684It only works if the function has been pre-compiled with debug 1799It only works if the function has been compiled with debug
1685information (\see{pragma}). 1800information \see{pragma}.
1686When no line information is available, it returns -1. 1801When no line information is available, it returns -1.
1687 1802
1688Function \verb'lua_getobjname' tries to find a reasonable name for 1803Function \verb'lua_getobjname' tries to find a reasonable name for
1689a given function. 1804a given function.
1690Because functions in Lua are first class values, 1805Because functions in Lua are first class values,
1691they do not have a fixed name. 1806they do not have a fixed name:
1692Some functions may be the value of many global variables, 1807Some functions may be the value of many global variables,
1693while others may be stored only in a table field. 1808while others may be stored only in a table field.
1694Function \verb'lua_getobjname' first checks whether the given 1809Function \verb'lua_getobjname' first checks whether the given
@@ -1697,7 +1812,7 @@ If so, it returns the string \verb'"fallback"',
1697and \verb'name' is set to point to the fallback name. 1812and \verb'name' is set to point to the fallback name.
1698Otherwise, if the given function is the value of a global variable, 1813Otherwise, if the given function is the value of a global variable,
1699then \verb'lua_getobjname' returns the string \verb'"global"', 1814then \verb'lua_getobjname' returns the string \verb'"global"',
1700while \verb'name' points to the variable name. 1815and \verb'name' points to the variable name.
1701If the given function is neither a fallback nor a global variable, 1816If the given function is neither a fallback nor a global variable,
1702then \verb'lua_getobjname' returns the empty string, 1817then \verb'lua_getobjname' returns the empty string,
1703and \verb'name' is set to \verb'NULL'. 1818and \verb'name' is set to \verb'NULL'.
@@ -1706,13 +1821,13 @@ and \verb'name' is set to \verb'NULL'.
1706 1821
1707The following functions allow the manipulation of the 1822The following functions allow the manipulation of the
1708local variables of a given activation record. 1823local variables of a given activation record.
1709They only work if the function has been pre-compiled with debug 1824They only work if the function has been compiled with debug
1710information (\see{pragma}). 1825information \see{pragma}.
1711\begin{verbatim} 1826\begin{verbatim}
1712lua_Object lua_getlocal (lua_Function func, int local_number, char **name); 1827lua_Object lua_getlocal (lua_Function func, int local_number, char **name);
1713int lua_setlocal (lua_Function func, int local_number); 1828int lua_setlocal (lua_Function func, int local_number);
1714\end{verbatim} 1829\end{verbatim}
1715The first one returns the value of a local variable, 1830\verb|lua_getlocal| returns the value of a local variable,
1716and sets \verb'name' to point to the variable name. 1831and 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.
1718The first parameter has index 1, and so on, until the 1833The first parameter has index 1, and so on, until the
@@ -1721,10 +1836,12 @@ When called with a \verb'local_number' greater than the
1721number of active local variables, 1836number of active local variables,
1722or if the activation record has no debug information, 1837or if the activation record has no debug information,
1723\verb'lua_getlocal' returns \verb'LUA_NOOBJECT'. 1838\verb'lua_getlocal' returns \verb'LUA_NOOBJECT'.
1839Formal parameters are the first local variables.
1724 1840
1725The function \verb'lua_setlocal' sets the local variable 1841The 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}.
1728If the function succeeds, then it returns 1. 1845If the function succeeds, then it returns 1.
1729If \verb'local_number' is greater than the number 1846If \verb'local_number' is greater than the number
1730of active local variables, 1847of 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))').
1758This second hook is only called if the active function 1875This second hook is only called if the active function
1759has been pre-compiled with debug information (\see{pragma}). 1876has been compiled with debug information \see{pragma}.
1760 1877
1761A hook is disabled when its value is NULL (0), 1878A hook is disabled when its value is \verb|NULL|,
1762which is the initial value of both hooks. 1879which 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.
2033This section presents one possible way to 2149This section presents one possible way to
2034implement classes, 2150implement classes,
2035using the inheritance mechanism presented above. 2151using the inheritance mechanism presented above.
2036{\em Please notice: the following examples only work 2152{\em Please note: the following examples only work
2037with the index fallback redefined according to 2153with the index fallback redefined according to
2038Section~\ref{exfallback}}. 2154Section~\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
2199Although Lua has been designed as an extension language, 2315Although Lua has been designed as an extension language,
2200the language can also be used as a stand-alone interpreter. 2316the 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}
2213All arguments are handle in order. 2329All arguments are handled in order.
2214For instance, an invocation like 2330For 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}
2228will {\em not} set \verb|a| to the string \verb|"name"|. 2344will {\em not\/} set \verb|a| to the string \verb|"name"|.
2229Instead, the quotes will be handled by the shell, 2345Instead, the quotes will be handled by the shell,
2230lua will get only \verb'a=name' to run, 2346lua will get only \verb'a=name' to run,
2231and \verb'a' will finish with \nil. 2347and \verb'a' will finish with \nil,
2348because the global variable \verb|name| has not been initialized.
2232Instead, one should write 2349Instead, 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
2241this system and gave valuable comments. 2358this system and gave valuable comments.
2242The authors would also like to thank Carlos Henrique Levy, 2359The authors would also like to thank Carlos Henrique Levy,
2243who found the name of the game. 2360who found the name of the game.
2244Lua means {\em moon} in Portuguese. 2361Lua 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}}
2258The whole I/O facilities have been rewritten. 2375The whole I/O facilities have been rewritten.
2259We strongly encourage programmers to addapt their code 2376We strongly encourage programmers to adapt their code
2260to this new version. 2377to this new version.
2261However, we are keeping the old version of the libraries 2378However, we are keeping the old version of the libraries
2262in the distribution, 2379in 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
2267function \verb'format'; 2384function \verb'format';
2268therefore this facility has been dropped. 2385therefore 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
2270what to read; 2387what to read;
2271this is incompatible with the old format options. 2388this is incompatible with the old format options.
2272\item Function \verb'strfind' now accepts patterns, 2389\item Function \verb'strfind' now accepts patterns,
2273so it may have a different behavior when the pattern includes 2390so it may have a different behavior when the pattern includes
2274special characteres. 2391special 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);
2341with the parameters explicitly pushed on the stack. 2458with the parameters explicitly pushed on the stack.
2342\item 2459\item
2343The functionality of the function \verb'lua_errorfunction' has been 2460The functionality of the function \verb'lua_errorfunction' has been
2344replaced by the {\em fallback} mechanism (\see{error}). 2461replaced by the {\em fallback\/} mechanism \see{error}.
2345\item 2462\item
2346When calling a function from the Lua library, 2463When calling a function from the Lua library,
2347parameters passed through the stack 2464parameters passed through the stack