diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-04-07 13:40:04 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-04-07 13:40:04 -0300 |
| commit | 4eb67aa7107e495bc756f37b7dcc936ee113afd7 (patch) | |
| tree | 584b84c6b1d829d2dd5bb35149fc1a1239ad10dc | |
| parent | 0133610315f42f9ec35acacd028f31e4a95a16b5 (diff) | |
| download | lua-4eb67aa7107e495bc756f37b7dcc936ee113afd7.tar.gz lua-4eb67aa7107e495bc756f37b7dcc936ee113afd7.tar.bz2 lua-4eb67aa7107e495bc756f37b7dcc936ee113afd7.zip | |
changes from lhf.
| -rw-r--r-- | manual.tex | 236 |
1 files changed, 120 insertions, 116 deletions
| @@ -1,4 +1,4 @@ | |||
| 1 | % $Id: manual.tex,v 1.27 1999/03/11 19:00:12 roberto Exp roberto $ | 1 | % $Id: manual.tex,v 1.28 1999/03/29 14:21:37 roberto Exp roberto $ |
| 2 | 2 | ||
| 3 | \documentclass[11pt]{article} | 3 | \documentclass[11pt]{article} |
| 4 | \usepackage{fullpage,bnf} | 4 | \usepackage{fullpage,bnf} |
| @@ -21,7 +21,7 @@ | |||
| 21 | 21 | ||
| 22 | \newcommand{\ff}{$\bullet$\ } | 22 | \newcommand{\ff}{$\bullet$\ } |
| 23 | 23 | ||
| 24 | \newcommand{\Version}{3.2 (alpha)} | 24 | \newcommand{\Version}{3.2 (beta)} |
| 25 | 25 | ||
| 26 | \makeindex | 26 | \makeindex |
| 27 | 27 | ||
| @@ -41,7 +41,7 @@ Waldemar Celes | |||
| 41 | \tecgraf\ --- Computer Science Department --- PUC-Rio | 41 | \tecgraf\ --- Computer Science Department --- PUC-Rio |
| 42 | } | 42 | } |
| 43 | 43 | ||
| 44 | %\date{\small \verb$Date: 1999/03/11 19:00:12 $} | 44 | \date{{\small \tt\$Date: 1999/03/29 14:21:37 $ $}} |
| 45 | 45 | ||
| 46 | \maketitle | 46 | \maketitle |
| 47 | 47 | ||
| @@ -89,6 +89,7 @@ a intera\c{c}\~ao entre programas Lua e programas C hospedeiros. | |||
| 89 | 89 | ||
| 90 | \newpage | 90 | \newpage |
| 91 | \begin{quotation} | 91 | \begin{quotation} |
| 92 | \parskip=10pt | ||
| 92 | \noindent | 93 | \noindent |
| 93 | \footnotesize | 94 | \footnotesize |
| 94 | Copyright \copyright\ 1994--1999 TeCGraf, PUC-Rio. All rights reserved. | 95 | Copyright \copyright\ 1994--1999 TeCGraf, PUC-Rio. All rights reserved. |
| @@ -144,10 +145,6 @@ general procedural programming with data description | |||
| 144 | facilities. | 145 | facilities. |
| 145 | Lua is intended to be used as a light-weight, but powerful, | 146 | Lua is intended to be used as a light-weight, but powerful, |
| 146 | configuration language for any program that needs one. | 147 | configuration language for any program that needs one. |
| 147 | Lua has been designed and implemented by | ||
| 148 | W.~Celes, | ||
| 149 | R.~Ierusalimschy and | ||
| 150 | L.~H.~de Figueiredo. | ||
| 151 | 148 | ||
| 152 | Lua is implemented as a library, written in C. | 149 | Lua is implemented as a library, written in C. |
| 153 | Being an extension language, Lua has no notion of a ``main'' program: | 150 | Being an extension language, Lua has no notion of a ``main'' program: |
| @@ -175,15 +172,17 @@ at the following URL's: | |||
| 175 | 172 | ||
| 176 | All statements in Lua are executed in a \Def{global environment}. | 173 | All statements in Lua are executed in a \Def{global environment}. |
| 177 | This environment, which keeps all global variables, | 174 | This environment, which keeps all global variables, |
| 178 | is initialized at the beginning of the embedding program and | 175 | is initialized with a call from the embedding program to |
| 179 | persists until its end. | 176 | \verb|lua_open| and |
| 177 | persists until a call to \verb|lua_close|, | ||
| 178 | or the end of the embedding program. | ||
| 180 | Optionally, a user can create multiple independent global | 179 | Optionally, a user can create multiple independent global |
| 181 | environments \see{mangstate}. | 180 | environments \see{mangstate}. |
| 182 | 181 | ||
| 183 | The global environment can be manipulated by Lua code or | 182 | The global environment can be manipulated by Lua code or |
| 184 | by the embedding program, | 183 | by the embedding program, |
| 185 | which can read and write global variables | 184 | which can read and write global variables |
| 186 | using functions from the API library that implements Lua. | 185 | using API functions from the library that implements Lua. |
| 187 | 186 | ||
| 188 | \Index{Global variables} do not need declaration. | 187 | \Index{Global variables} do not need declaration. |
| 189 | Any variable is assumed to be global unless explicitly declared local | 188 | Any variable is assumed to be global unless explicitly declared local |
| @@ -206,7 +205,7 @@ A chunk may optionally end with a \verb|return| statement \see{return}. | |||
| 206 | When a chunk is executed, first all its code is pre-compiled, | 205 | When a chunk is executed, first all its code is pre-compiled, |
| 207 | then the statements are executed in sequential order. | 206 | then the statements are executed in sequential order. |
| 208 | All modifications a chunk effects on the global environment persist | 207 | All modifications a chunk effects on the global environment persist |
| 209 | after its end. | 208 | after the chunk end. |
| 210 | 209 | ||
| 211 | Chunks may also be pre-compiled into binary form; | 210 | Chunks may also be pre-compiled into binary form; |
| 212 | see program \IndexVerb{luac} for details. | 211 | see program \IndexVerb{luac} for details. |
| @@ -227,7 +226,7 @@ There are six \Index{basic types} in Lua: \Def{nil}, \Def{number}, | |||
| 227 | \Def{string}, \Def{function}, \Def{userdata}, and \Def{table}. | 226 | \Def{string}, \Def{function}, \Def{userdata}, and \Def{table}. |
| 228 | \emph{Nil} is the type of the value \nil, | 227 | \emph{Nil} is the type of the value \nil, |
| 229 | whose main property is to be different from any other value. | 228 | whose main property is to be different from any other value. |
| 230 | \emph{Number} represents real (double precision floating point) numbers, | 229 | \emph{Number} represents real (double-precision floating-point) numbers, |
| 231 | while \emph{string} has the usual meaning. | 230 | while \emph{string} has the usual meaning. |
| 232 | Lua is \Index{eight-bit clean}, | 231 | Lua is \Index{eight-bit clean}, |
| 233 | and so strings may contain any 8-bit character, | 232 | and so strings may contain any 8-bit character, |
| @@ -283,7 +282,7 @@ semantics of Lua \see{tag-method}. | |||
| 283 | Each of the types \M{nil}, \M{number} and \M{string} has a different tag. | 282 | Each of the types \M{nil}, \M{number} and \M{string} has a different tag. |
| 284 | All values of each of these types have this same pre-defined tag. | 283 | All values of each of these types have this same pre-defined tag. |
| 285 | Values of type \M{function} can have two different tags, | 284 | Values of type \M{function} can have two different tags, |
| 286 | depending on whether they are Lua or C functions. | 285 | depending on whether they are Lua functions or C functions. |
| 287 | Finally, | 286 | Finally, |
| 288 | values of type \M{userdata} and \M{table} can have | 287 | values of type \M{userdata} and \M{table} can have |
| 289 | as many different tags as needed \see{tag-method}. | 288 | as many different tags as needed \see{tag-method}. |
| @@ -318,7 +317,7 @@ Lua is a case-sensitive language: | |||
| 318 | \T{and} is a reserved word, but \T{And} and \T{\'and} | 317 | \T{and} is a reserved word, but \T{And} and \T{\'and} |
| 319 | (if the locale permits) are two other different identifiers. | 318 | (if the locale permits) are two other different identifiers. |
| 320 | As a convention, identifiers starting with underscore followed by | 319 | As a convention, identifiers starting with underscore followed by |
| 321 | uppercase letters should not be used in regular programs. | 320 | uppercase letters are reserved for internal variables. |
| 322 | 321 | ||
| 323 | The following strings denote other \Index{tokens}: | 322 | The following strings denote other \Index{tokens}: |
| 324 | \begin{verbatim} | 323 | \begin{verbatim} |
| @@ -329,7 +328,7 @@ The following strings denote other \Index{tokens}: | |||
| 329 | \Index{Literal strings} can be delimited by matching single or double quotes, | 328 | \Index{Literal strings} can be delimited by matching single or double quotes, |
| 330 | and can contain the C-like escape sequences | 329 | and can contain the C-like escape sequences |
| 331 | \verb|'\a'| (bell), | 330 | \verb|'\a'| (bell), |
| 332 | \verb|'\b'| (back space), | 331 | \verb|'\b'| (backspace), |
| 333 | \verb|'\f'| (form feed), | 332 | \verb|'\f'| (form feed), |
| 334 | \verb|'\n'| (new line), | 333 | \verb|'\n'| (new line), |
| 335 | \verb|'\r'| (carriage return), | 334 | \verb|'\r'| (carriage return), |
| @@ -369,9 +368,9 @@ in Unix systems \see{lua-sa}. | |||
| 369 | 368 | ||
| 370 | \Index{Numerical constants} may be written with an optional decimal part, | 369 | \Index{Numerical constants} may be written with an optional decimal part, |
| 371 | and an optional decimal exponent. | 370 | and an optional decimal exponent. |
| 372 | Examples of valid numerical constants are: | 371 | Examples of valid numerical constants are |
| 373 | \begin{verbatim} | 372 | \begin{verbatim} |
| 374 | 4 4.0 0.4 4.57e-3 0.3e12 | 373 | 3 3.0 3.1416 314.16e-2 0.31416E1 |
| 375 | \end{verbatim} | 374 | \end{verbatim} |
| 376 | 375 | ||
| 377 | \subsection{The \Index{Pre-processor}} \label{pre-processor} | 376 | \subsection{The \Index{Pre-processor}} \label{pre-processor} |
| @@ -396,7 +395,7 @@ Directives may be freely nested. | |||
| 396 | Particularly, a \verb|$endinput| may occur inside a \verb|$if|; | 395 | Particularly, a \verb|$endinput| may occur inside a \verb|$if|; |
| 397 | in that case, even the matching \verb|$end| is not parsed. | 396 | in that case, even the matching \verb|$end| is not parsed. |
| 398 | 397 | ||
| 399 | A \M{cond} part may be: | 398 | A \M{cond} part may be |
| 400 | \begin{description} | 399 | \begin{description} |
| 401 | \item[\T{nil}] --- always false. | 400 | \item[\T{nil}] --- always false. |
| 402 | \item[\T{1}] --- always true. | 401 | \item[\T{1}] --- always true. |
| @@ -421,12 +420,13 @@ use the \verb|format| function \see{format}. | |||
| 421 | 420 | ||
| 422 | Functions in Lua can return many values. | 421 | Functions in Lua can return many values. |
| 423 | Because there are no type declarations, | 422 | Because there are no type declarations, |
| 423 | when a function is called | ||
| 424 | the system does not know how many values a function will return, | 424 | the system does not know how many values a function will return, |
| 425 | or how many parameters it needs. | 425 | or how many parameters it needs. |
| 426 | Therefore, sometimes, a list of values must be \emph{adjusted}, at run time, | 426 | Therefore, sometimes, a list of values must be \emph{adjusted}, at run time, |
| 427 | to a given length. | 427 | to a given length. |
| 428 | If there are more values than are needed, | 428 | If there are more values than are needed, |
| 429 | then the last values are thrown away. | 429 | then the excess values are thrown away. |
| 430 | If there are more needs than values, | 430 | If there are more needs than values, |
| 431 | then the list is extended with as many \nil's as needed. | 431 | then the list is extended with as many \nil's as needed. |
| 432 | Adjustment occurs in multiple assignment \see{assignment} | 432 | Adjustment occurs in multiple assignment \see{assignment} |
| @@ -481,7 +481,7 @@ The two lists may have different lengths. | |||
| 481 | Before the assignment, the list of values is \emph{adjusted} to | 481 | Before the assignment, the list of values is \emph{adjusted} to |
| 482 | the length of the list of variables \see{adjust}. | 482 | the length of the list of variables \see{adjust}. |
| 483 | 483 | ||
| 484 | A single name can denote a global or a local variable, | 484 | A single name can denote a global variable, a local variable, |
| 485 | or a formal parameter: | 485 | or a formal parameter: |
| 486 | \begin{Produc} | 486 | \begin{Produc} |
| 487 | \produc{var}{name} | 487 | \produc{var}{name} |
| @@ -529,7 +529,7 @@ only \nil\ is considered false. | |||
| 529 | A \T{return} is used to return values from a function or from a chunk. | 529 | A \T{return} is used to return values from a function or from a chunk. |
| 530 | \label{return} | 530 | \label{return} |
| 531 | Because they may return more than one value, | 531 | Because they may return more than one value, |
| 532 | the syntax for a \Index{return statement} is: | 532 | the syntax for a \Index{return statement} is |
| 533 | \begin{Produc} | 533 | \begin{Produc} |
| 534 | \produc{ret}{\rwd{return} \opt{explist1} \opt{sc}} | 534 | \produc{ret}{\rwd{return} \opt{explist1} \opt{sc}} |
| 535 | \end{Produc} | 535 | \end{Produc} |
| @@ -561,7 +561,7 @@ Otherwise, all variables are initialized with \nil. | |||
| 561 | \subsection{\Index{Expressions}} | 561 | \subsection{\Index{Expressions}} |
| 562 | 562 | ||
| 563 | \subsubsection{\Index{Basic Expressions}} | 563 | \subsubsection{\Index{Basic Expressions}} |
| 564 | Basic expressions are: | 564 | Basic expressions are |
| 565 | \begin{Produc} | 565 | \begin{Produc} |
| 566 | \produc{exp}{\ter{(} exp \ter{)}} | 566 | \produc{exp}{\ter{(} exp \ter{)}} |
| 567 | \produc{exp}{\rwd{nil}} | 567 | \produc{exp}{\rwd{nil}} |
| @@ -581,7 +581,7 @@ string literals are explained in \See{lexical}; | |||
| 581 | variables are explained in \See{assignment}; | 581 | variables are explained in \See{assignment}; |
| 582 | upvalues are explained in \See{upvalue}; | 582 | upvalues are explained in \See{upvalue}; |
| 583 | function definitions (\M{function}) are explained in \See{func-def}; | 583 | function definitions (\M{function}) are explained in \See{func-def}; |
| 584 | function call are explained in \See{functioncall}. | 584 | function calls are explained in \See{functioncall}. |
| 585 | 585 | ||
| 586 | An access to a global variable \verb|x| is equivalent to a | 586 | An access to a global variable \verb|x| is equivalent to a |
| 587 | call \verb|getglobal('x')|; | 587 | call \verb|getglobal('x')|; |
| @@ -639,7 +639,7 @@ then their values are compared using lexicographical order. | |||
| 639 | Otherwise, the ``order'' tag method is called \see{tag-method}. | 639 | Otherwise, the ``order'' tag method is called \see{tag-method}. |
| 640 | 640 | ||
| 641 | \subsubsection{Logical Operators} | 641 | \subsubsection{Logical Operators} |
| 642 | The \Index{logical operators} are: | 642 | The \Index{logical operators} are |
| 643 | \index{and}\index{or}\index{not} | 643 | \index{and}\index{or}\index{not} |
| 644 | \begin{verbatim} | 644 | \begin{verbatim} |
| 645 | and or not | 645 | and or not |
| @@ -692,7 +692,7 @@ every time a constructor is evaluated, a new table is created. | |||
| 692 | Constructors can be used to create empty tables, | 692 | Constructors can be used to create empty tables, |
| 693 | or to create a table and initialize some fields. | 693 | or to create a table and initialize some fields. |
| 694 | 694 | ||
| 695 | The general syntax for constructors is: | 695 | The general syntax for constructors is |
| 696 | \begin{Produc} | 696 | \begin{Produc} |
| 697 | \produc{tableconstructor}{\ter{\{} fieldlist \ter{\}}} | 697 | \produc{tableconstructor}{\ter{\{} fieldlist \ter{\}}} |
| 698 | \produc{fieldlist}{lfieldlist \Or ffieldlist \Or lfieldlist \ter{;} ffieldlist | 698 | \produc{fieldlist}{lfieldlist \Or ffieldlist \Or lfieldlist \ter{;} ffieldlist |
| @@ -701,17 +701,17 @@ The general syntax for constructors is: | |||
| 701 | \produc{ffieldlist}{\opt{ffieldlist1}} | 701 | \produc{ffieldlist}{\opt{ffieldlist1}} |
| 702 | \end{Produc} | 702 | \end{Produc} |
| 703 | 703 | ||
| 704 | The form \emph{lfieldlist1} is used to initialize lists. | 704 | The form \emph{lfieldlist1} is used to initialize lists: |
| 705 | \begin{Produc} | 705 | \begin{Produc} |
| 706 | \produc{lfieldlist1}{exp \rep{\ter{,} exp} \opt{\ter{,}}} | 706 | \produc{lfieldlist1}{exp \rep{\ter{,} exp} \opt{\ter{,}}} |
| 707 | \end{Produc}% | 707 | \end{Produc}% |
| 708 | The expressions in the list are assigned to consecutive numerical indices, | 708 | The expressions in the list are assigned to consecutive numerical indices, |
| 709 | starting with 1. | 709 | starting with 1. |
| 710 | For example: | 710 | For example, |
| 711 | \begin{verbatim} | 711 | \begin{verbatim} |
| 712 | a = {"v1", "v2", 34} | 712 | a = {"v1", "v2", 34} |
| 713 | \end{verbatim} | 713 | \end{verbatim} |
| 714 | is equivalent to: | 714 | is equivalent to |
| 715 | \begin{verbatim} | 715 | \begin{verbatim} |
| 716 | do | 716 | do |
| 717 | local temp = {} | 717 | local temp = {} |
| @@ -727,11 +727,11 @@ The form \emph{ffieldlist1} initializes other fields in a table: | |||
| 727 | \produc{ffieldlist1}{ffield \rep{\ter{,} ffield} \opt{\ter{,}}} | 727 | \produc{ffieldlist1}{ffield \rep{\ter{,} ffield} \opt{\ter{,}}} |
| 728 | \produc{ffield}{\ter{[} exp \ter{]} \ter{=} exp \Or name \ter{=} exp} | 728 | \produc{ffield}{\ter{[} exp \ter{]} \ter{=} exp \Or name \ter{=} exp} |
| 729 | \end{Produc}% | 729 | \end{Produc}% |
| 730 | For example: | 730 | For example, |
| 731 | \begin{verbatim} | 731 | \begin{verbatim} |
| 732 | a = {[f(k)] = g(y), x = 1, y = 3, [0] = b+c} | 732 | a = {[f(k)] = g(y), x = 1, y = 3, [0] = b+c} |
| 733 | \end{verbatim} | 733 | \end{verbatim} |
| 734 | is equivalent to: | 734 | is equivalent to |
| 735 | \begin{verbatim} | 735 | \begin{verbatim} |
| 736 | do | 736 | do |
| 737 | local temp = {} | 737 | local temp = {} |
| @@ -753,7 +753,7 @@ For example, all forms below are correct: | |||
| 753 | x = {;} | 753 | x = {;} |
| 754 | x = {'a', 'b',} | 754 | x = {'a', 'b',} |
| 755 | x = {type='list'; 'a', 'b'} | 755 | x = {type='list'; 'a', 'b'} |
| 756 | x = {f(0), f(1), f(2),; n=3} | 756 | x = {f(0), f(1), f(2),; n=3,} |
| 757 | \end{verbatim} | 757 | \end{verbatim} |
| 758 | 758 | ||
| 759 | \subsubsection{Function Calls} \label{functioncall} | 759 | \subsubsection{Function Calls} \label{functioncall} |
| @@ -781,6 +781,7 @@ is syntactic sugar for | |||
| 781 | \end{verbatim} | 781 | \end{verbatim} |
| 782 | except that \verb|simpleexp| is evaluated only once. | 782 | except that \verb|simpleexp| is evaluated only once. |
| 783 | 783 | ||
| 784 | Arguments have the following syntax: | ||
| 784 | \begin{Produc} | 785 | \begin{Produc} |
| 785 | \produc{args}{\ter{(} \opt{explist1} \ter{)}} | 786 | \produc{args}{\ter{(} \opt{explist1} \ter{)}} |
| 786 | \produc{args}{tableconstructor} | 787 | \produc{args}{tableconstructor} |
| @@ -813,17 +814,17 @@ Note that the only place that can hold many values | |||
| 813 | is the last (or the only) expression in an assignment | 814 | is the last (or the only) expression in an assignment |
| 814 | or in a return statement; see examples below. | 815 | or in a return statement; see examples below. |
| 815 | \begin{verbatim} | 816 | \begin{verbatim} |
| 816 | f(); -- adjusted to 0 | 817 | f(); -- adjusted to 0 |
| 817 | g(x, f()); -- f() is adjusted to 1 | 818 | g(x, f()); -- f() is adjusted to 1 |
| 818 | a,b,c = f(), x; -- f() is adjusted to 1 result (and c gets nil) | 819 | a,b,c = f(), x; -- f() is adjusted to 1 result (and c gets nil) |
| 819 | a,b,c = x, f(); -- f() is adjusted to 2 | 820 | a,b,c = x, f(); -- f() is adjusted to 2 |
| 820 | a,b,c = f(); -- f() is adjusted to 3 | 821 | a,b,c = f(); -- f() is adjusted to 3 |
| 821 | return f(); -- returns all values returned by f() | 822 | return f(); -- returns all values returned by f() |
| 822 | \end{verbatim} | 823 | \end{verbatim} |
| 823 | 824 | ||
| 824 | \subsubsection{\Index{Function Definitions}} \label{func-def} | 825 | \subsubsection{\Index{Function Definitions}} \label{func-def} |
| 825 | 826 | ||
| 826 | The syntax for function definition is: | 827 | The syntax for function definition is |
| 827 | \begin{Produc} | 828 | \begin{Produc} |
| 828 | \produc{function}{\rwd{function} \ter{(} \opt{parlist1} \ter{)} | 829 | \produc{function}{\rwd{function} \ter{(} \opt{parlist1} \ter{)} |
| 829 | block \rwd{end}} | 830 | block \rwd{end}} |
| @@ -831,13 +832,13 @@ The syntax for function definition is: | |||
| 831 | block \rwd{end}} | 832 | block \rwd{end}} |
| 832 | \produc{funcname}{name \Or name \ter{.} name} | 833 | \produc{funcname}{name \Or name \ter{.} name} |
| 833 | \end{Produc} | 834 | \end{Produc} |
| 834 | The statement: | 835 | The statement |
| 835 | \begin{verbatim} | 836 | \begin{verbatim} |
| 836 | function f (...) | 837 | function f (...) |
| 837 | ... | 838 | ... |
| 838 | end | 839 | end |
| 839 | \end{verbatim} | 840 | \end{verbatim} |
| 840 | is just syntactic sugar for: | 841 | is just syntactic sugar for |
| 841 | \begin{verbatim} | 842 | \begin{verbatim} |
| 842 | f = function (...) | 843 | f = function (...) |
| 843 | ... | 844 | ... |
| @@ -872,8 +873,8 @@ A vararg function does not adjust its argument list; | |||
| 872 | instead, it collects any extra arguments into an implicit parameter, | 873 | instead, it collects any extra arguments into an implicit parameter, |
| 873 | called \IndexVerb{arg}. | 874 | called \IndexVerb{arg}. |
| 874 | This parameter is always initialized as a table, | 875 | This parameter is always initialized as a table, |
| 875 | with a field \verb|n| with the number of extra arguments, | 876 | with a field \verb|n| whose value is the number of extra arguments, |
| 876 | and the extra arguments at positions 1, 2, \ldots | 877 | and the extra arguments at positions 1,~2,~\ldots |
| 877 | 878 | ||
| 878 | As an example, suppose definitions like: | 879 | As an example, suppose definitions like: |
| 879 | \begin{verbatim} | 880 | \begin{verbatim} |
| @@ -898,22 +899,22 @@ If control reaches the end of a function without a return instruction, | |||
| 898 | then the function returns with no results. | 899 | then the function returns with no results. |
| 899 | 900 | ||
| 900 | There is a special syntax for defining \Index{methods}, | 901 | There is a special syntax for defining \Index{methods}, |
| 901 | that is, functions that have an implicit extra parameter \IndexVerb{self}. | 902 | that is, functions that have an implicit extra parameter \IndexVerb{self}: |
| 902 | \begin{Produc} | 903 | \begin{Produc} |
| 903 | \produc{function}{\rwd{function} name \ter{:} name \ter{(} \opt{parlist1} | 904 | \produc{function}{\rwd{function} name \ter{:} name \ter{(} \opt{parlist1} |
| 904 | \ter{)} block \rwd{end}} | 905 | \ter{)} block \rwd{end}} |
| 905 | \end{Produc}% | 906 | \end{Produc}% |
| 906 | Thus, a declaration like | 907 | Thus, a declaration like |
| 907 | \begin{verbatim} | 908 | \begin{verbatim} |
| 908 | function v:f (...) | 909 | function v:f (...) |
| 909 | ... | 910 | ... |
| 910 | end | 911 | end |
| 911 | \end{verbatim} | 912 | \end{verbatim} |
| 912 | is equivalent to | 913 | is equivalent to |
| 913 | \begin{verbatim} | 914 | \begin{verbatim} |
| 914 | v.f = function (self, ...) | 915 | v.f = function (self, ...) |
| 915 | ... | 916 | ... |
| 916 | end | 917 | end |
| 917 | \end{verbatim} | 918 | \end{verbatim} |
| 918 | that is, the function gets an extra formal parameter called \verb|self|. | 919 | that is, the function gets an extra formal parameter called \verb|self|. |
| 919 | Note that the variable \verb|v| must have been | 920 | Note that the variable \verb|v| must have been |
| @@ -944,26 +945,26 @@ at the point where the function is defined. | |||
| 944 | 945 | ||
| 945 | Here are some examples: | 946 | Here are some examples: |
| 946 | \begin{verbatim} | 947 | \begin{verbatim} |
| 947 | a,b,c = 1,2,3 -- global variables | 948 | a,b,c = 1,2,3 -- global variables |
| 948 | function f (x) | 949 | function f (x) |
| 949 | local b -- x and b are local to f | 950 | local b -- x and b are local to f |
| 950 | local g = function (a) | 951 | local g = function (a) |
| 951 | local y -- a and y are local to g | 952 | local y -- a and y are local to g |
| 952 | p = a -- OK, access local 'a' | 953 | p = a -- OK, access local 'a' |
| 953 | p = c -- OK, access global 'c' | 954 | p = c -- OK, access global 'c' |
| 954 | p = b -- ERROR: cannot access a variable in outer scope | 955 | p = b -- ERROR: cannot access a variable in outer scope |
| 955 | p = %b -- OK, access frozen value of 'b' (local to 'f') | 956 | p = %b -- OK, access frozen value of 'b' (local to 'f') |
| 956 | p = %c -- OK, access frozen value of global 'c' | 957 | p = %c -- OK, access frozen value of global 'c' |
| 957 | p = %y -- ERROR: 'y' is not visible where 'g' is defined | 958 | p = %y -- ERROR: 'y' is not visible where 'g' is defined |
| 958 | end -- g | 959 | end -- g |
| 959 | end -- f | 960 | end -- f |
| 960 | \end{verbatim} | 961 | \end{verbatim} |
| 961 | 962 | ||
| 962 | 963 | ||
| 963 | \subsection{Tag Methods} \label{tag-method} | 964 | \subsection{Tag Methods} \label{tag-method} |
| 964 | 965 | ||
| 965 | Lua provides a powerful mechanism to extend its semantics, | 966 | Lua provides a powerful mechanism to extend its semantics, |
| 966 | called \Def{Tag Methods}. | 967 | called \Def{tag methods}. |
| 967 | A tag method is a programmer-defined function | 968 | A tag method is a programmer-defined function |
| 968 | that is called at specific key points during the evaluation of a program, | 969 | that is called at specific key points during the evaluation of a program, |
| 969 | allowing the programmer to change the standard Lua behavior at these points. | 970 | allowing the programmer to change the standard Lua behavior at these points. |
| @@ -974,10 +975,10 @@ according to the tag of the values involved | |||
| 974 | in the event \see{TypesSec}. | 975 | in the event \see{TypesSec}. |
| 975 | The function \IndexVerb{settagmethod} changes the tag method | 976 | The function \IndexVerb{settagmethod} changes the tag method |
| 976 | associated with a given pair \M{(tag, event)}. | 977 | associated with a given pair \M{(tag, event)}. |
| 977 | Its first parameter is the tag, the second is the event name | 978 | Its first parameter is the tag, the second parameter is the event name |
| 978 | (a string, see below), | 979 | (a string; see below), |
| 979 | and the third parameter is the new method (a function), | 980 | and the third parameter is the new method (a function), |
| 980 | or \nil\ to restore the default behavior. | 981 | or \nil\ to restore the default behavior for the pair. |
| 981 | The function returns the previous tag method for that pair. | 982 | The function returns the previous tag method for that pair. |
| 982 | Another function, \IndexVerb{gettagmethod}, | 983 | Another function, \IndexVerb{gettagmethod}, |
| 983 | receives a tag and an event name and returns the | 984 | receives a tag and an event name and returns the |
| @@ -993,7 +994,7 @@ Please notice that the code shown here is only illustrative; | |||
| 993 | the real behavior is hard coded in the interpreter, | 994 | the real behavior is hard coded in the interpreter, |
| 994 | and it is much more efficient than this simulation. | 995 | and it is much more efficient than this simulation. |
| 995 | All functions used in these descriptions | 996 | All functions used in these descriptions |
| 996 | (\verb|rawgetglobal|, \verb|tonumber|, \verb|call|, etc) | 997 | (\verb|rawgetglobal|, \verb|tonumber|, \verb|call|, etc.) |
| 997 | are described in \See{predefined}. | 998 | are described in \See{predefined}. |
| 998 | 999 | ||
| 999 | \begin{description} | 1000 | \begin{description} |
| @@ -1237,7 +1238,7 @@ called when Lua tries to call a non function value. | |||
| 1237 | \end{verbatim} | 1238 | \end{verbatim} |
| 1238 | 1239 | ||
| 1239 | \item[``gc'':]\index{gc event} | 1240 | \item[``gc'':]\index{gc event} |
| 1240 | called when Lua is garbage collecting an object. | 1241 | called when Lua is ``garbage collecting'' an object. |
| 1241 | This method cannot be set for strings, numbers, functions, | 1242 | This method cannot be set for strings, numbers, functions, |
| 1242 | and userdata with default tag. | 1243 | and userdata with default tag. |
| 1243 | For each object to be collected, | 1244 | For each object to be collected, |
| @@ -1282,7 +1283,7 @@ such as the call stack. | |||
| 1282 | To provide more information about errors, | 1283 | To provide more information about errors, |
| 1283 | Lua programs should include the compilation pragma \verb|$debug|. | 1284 | Lua programs should include the compilation pragma \verb|$debug|. |
| 1284 | \index{debug pragma}\label{pragma} | 1285 | \index{debug pragma}\label{pragma} |
| 1285 | When an error occurs in a program compiled with this option, | 1286 | When an error occurs in a chunk compiled with this option, |
| 1286 | the I/O error routine is able to print the number of the | 1287 | the I/O error routine is able to print the number of the |
| 1287 | lines where the calls (and the error) were made. | 1288 | lines where the calls (and the error) were made. |
| 1288 | 1289 | ||
| @@ -1319,6 +1320,8 @@ is stored in a dynamic structure pointed by\Deffunc{lua_state} | |||
| 1319 | typedef struct lua_State lua_State; | 1320 | typedef struct lua_State lua_State; |
| 1320 | extern lua_State *lua_state; | 1321 | extern lua_State *lua_state; |
| 1321 | \end{verbatim} | 1322 | \end{verbatim} |
| 1323 | The variable \verb|lua_state| is the only C global variable in | ||
| 1324 | the Lua library. | ||
| 1322 | 1325 | ||
| 1323 | Before calling any API function, | 1326 | Before calling any API function, |
| 1324 | this state must be initialized. | 1327 | this state must be initialized. |
| @@ -1353,11 +1356,11 @@ An easy way to do that is defining an auxiliary function: | |||
| 1353 | \end{verbatim} | 1356 | \end{verbatim} |
| 1354 | This function creates a new state without changing the current state | 1357 | This function creates a new state without changing the current state |
| 1355 | of the interpreter. | 1358 | of the interpreter. |
| 1356 | Note that any new state is built with all predefined functions, | 1359 | Note that any new state is created with all predefined functions, |
| 1357 | but any additional library (such as the standard libraries) must be | 1360 | but any additional library (such as the standard libraries) must be |
| 1358 | explicitly open in the new state, if needed. | 1361 | explicitly open in the new state, if needed. |
| 1359 | 1362 | ||
| 1360 | If necessary, a state may be released:\Deffunc{lua_close} | 1363 | If necessary, a state may be released by calling\Deffunc{lua_close} |
| 1361 | \begin{verbatim} | 1364 | \begin{verbatim} |
| 1362 | void lua_close (void); | 1365 | void lua_close (void); |
| 1363 | \end{verbatim} | 1366 | \end{verbatim} |
| @@ -1404,7 +1407,7 @@ int lua_isfunction (lua_Object object); | |||
| 1404 | int lua_iscfunction (lua_Object object); | 1407 | int lua_iscfunction (lua_Object object); |
| 1405 | int lua_isuserdata (lua_Object object); | 1408 | int lua_isuserdata (lua_Object object); |
| 1406 | \end{verbatim} | 1409 | \end{verbatim} |
| 1407 | All macros return 1 if the object is compatible with the given type, | 1410 | These functions return 1 if the object is compatible with the given type, |
| 1408 | and 0 otherwise. | 1411 | and 0 otherwise. |
| 1409 | The function \verb|lua_isnumber| accepts numbers and numerical strings, | 1412 | The function \verb|lua_isnumber| accepts numbers and numerical strings, |
| 1410 | whereas | 1413 | whereas |
| @@ -1444,7 +1447,8 @@ but may contain other zeros in their body. | |||
| 1444 | If you do not know whether a string may contain zeros, | 1447 | If you do not know whether a string may contain zeros, |
| 1445 | you can use \verb|lua_strlen| to get the actual length. | 1448 | you can use \verb|lua_strlen| to get the actual length. |
| 1446 | Because Lua has garbage collection, | 1449 | Because Lua has garbage collection, |
| 1447 | there is no guarantee that such pointer will be valid after the block ends | 1450 | there is no guarantee that the pointer returned by \verb|lua_getstring| |
| 1451 | will be valid after the block ends | ||
| 1448 | \see{GC}. | 1452 | \see{GC}. |
| 1449 | 1453 | ||
| 1450 | \verb|lua_getcfunction| converts a \verb|lua_Object| to a C function. | 1454 | \verb|lua_getcfunction| converts a \verb|lua_Object| to a C function. |
| @@ -1501,7 +1505,7 @@ Note that the structure lua2C cannot be directly modified by C code. | |||
| 1501 | 1505 | ||
| 1502 | The second structure, C2lua, is an abstract stack. | 1506 | The second structure, C2lua, is an abstract stack. |
| 1503 | Pushing elements into this stack | 1507 | Pushing elements into this stack |
| 1504 | is done with the following functions and macros: | 1508 | is done with the following functions: |
| 1505 | \Deffunc{lua_pushnumber}\Deffunc{lua_pushlstring}\Deffunc{lua_pushstring} | 1509 | \Deffunc{lua_pushnumber}\Deffunc{lua_pushlstring}\Deffunc{lua_pushstring} |
| 1506 | \Deffunc{lua_pushcfunction}\Deffunc{lua_pushusertag} | 1510 | \Deffunc{lua_pushcfunction}\Deffunc{lua_pushusertag} |
| 1507 | \Deffunc{lua_pushnil}\Deffunc{lua_pushobject} | 1511 | \Deffunc{lua_pushnil}\Deffunc{lua_pushobject} |
| @@ -1518,7 +1522,7 @@ void lua_pushcfunction (lua_CFunction f); /* macro */ | |||
| 1518 | All of them receive a C value, | 1522 | All of them receive a C value, |
| 1519 | convert it to a corresponding \verb|lua_Object|, | 1523 | convert it to a corresponding \verb|lua_Object|, |
| 1520 | and leave the result on the top of C2lua. | 1524 | and leave the result on the top of C2lua. |
| 1521 | Particularly, functions \verb|lua_pushlstring| and \verb|lua_pushstring| | 1525 | In particular, functions \verb|lua_pushlstring| and \verb|lua_pushstring| |
| 1522 | make an internal copy of the given string. | 1526 | make an internal copy of the given string. |
| 1523 | Function \verb|lua_pushstring| can only be used to push proper C strings | 1527 | Function \verb|lua_pushstring| can only be used to push proper C strings |
| 1524 | (that is, strings that do not contain zeros and end with a zero); | 1528 | (that is, strings that do not contain zeros and end with a zero); |
| @@ -1602,9 +1606,6 @@ is the ``name of the chunk'', | |||
| 1602 | used in error messages and debug information. | 1606 | used in error messages and debug information. |
| 1603 | If \verb|name| is \verb|NULL|, | 1607 | If \verb|name| is \verb|NULL|, |
| 1604 | Lua gives a default name to the chunk. | 1608 | Lua gives a default name to the chunk. |
| 1605 | In files this name is the file name, | ||
| 1606 | and \verb|lua_dostring| uses a small prefix | ||
| 1607 | of the string as the chunk name. | ||
| 1608 | 1609 | ||
| 1609 | These functions return, in structure lua2C, | 1610 | These functions return, in structure lua2C, |
| 1610 | any values eventually returned by the chunks. | 1611 | any values eventually returned by the chunks. |
| @@ -1648,7 +1649,7 @@ The function | |||
| 1648 | \begin{verbatim} | 1649 | \begin{verbatim} |
| 1649 | lua_Object lua_gettable (void); | 1650 | lua_Object lua_gettable (void); |
| 1650 | \end{verbatim} | 1651 | \end{verbatim} |
| 1651 | pops from the stack C2lua a table and an index, | 1652 | pops a table and an index from the stack C2lua, |
| 1652 | and returns the contents of the table at that index. | 1653 | and returns the contents of the table at that index. |
| 1653 | As in Lua, this operation may trigger a tag method. | 1654 | As in Lua, this operation may trigger a tag method. |
| 1654 | To get the real value of any table index, | 1655 | To get the real value of any table index, |
| @@ -1662,7 +1663,7 @@ lua_Object lua_rawgettable (void); | |||
| 1662 | To store a value in an index, | 1663 | To store a value in an index, |
| 1663 | the program must push the table, the index, | 1664 | the program must push the table, the index, |
| 1664 | and the value onto C2lua, | 1665 | and the value onto C2lua, |
| 1665 | and then call the function: | 1666 | and then call the function |
| 1666 | \Deffunc{lua_settable} | 1667 | \Deffunc{lua_settable} |
| 1667 | \begin{verbatim} | 1668 | \begin{verbatim} |
| 1668 | void lua_settable (void); | 1669 | void lua_settable (void); |
| @@ -1733,11 +1734,11 @@ This function never returns. | |||
| 1733 | If the C function has been called from Lua, | 1734 | If the C function has been called from Lua, |
| 1734 | then the corresponding Lua execution terminates, | 1735 | then the corresponding Lua execution terminates, |
| 1735 | as if an error had occurred inside Lua code. | 1736 | as if an error had occurred inside Lua code. |
| 1736 | Otherwise, the whole program terminates with a call to \verb|exit(1)|. | 1737 | Otherwise, the whole host program terminates with a call to \verb|exit(1)|. |
| 1737 | The \verb|message| is passed to the error handler function, | 1738 | The \verb|message| is passed to the error handler function, |
| 1738 | \verb|_ERRORMESSAGE|. | 1739 | \verb|_ERRORMESSAGE|. |
| 1739 | If \verb|message| is \verb|NULL|, | 1740 | If \verb|message| is \verb|NULL|, |
| 1740 | \verb|_ERRORMESSAGE| is not called. | 1741 | then \verb|_ERRORMESSAGE| is not called. |
| 1741 | 1742 | ||
| 1742 | Tag methods can be changed with: \Deffunc{lua_settagmethod} | 1743 | Tag methods can be changed with: \Deffunc{lua_settagmethod} |
| 1743 | \begin{verbatim} | 1744 | \begin{verbatim} |
| @@ -1794,12 +1795,13 @@ Like a Lua function, a C function called by Lua can also return | |||
| 1794 | many results. | 1795 | many results. |
| 1795 | 1796 | ||
| 1796 | When a C function is created, | 1797 | When a C function is created, |
| 1797 | it is possible to associate some \emph{upvalues} to it; | 1798 | it is possible to associate some \emph{upvalues} to it, |
| 1799 | thus creating a C closure; | ||
| 1798 | then these values are passed to the function whenever it is called, | 1800 | then these values are passed to the function whenever it is called, |
| 1799 | as common arguments. | 1801 | as common arguments. |
| 1800 | To associate upvalues to a function, | 1802 | To associate upvalues to a function, |
| 1801 | first these values must be pushed on C2lua. | 1803 | first these values must be pushed on C2lua. |
| 1802 | Then the function: | 1804 | Then the function |
| 1803 | \Deffunc{lua_pushcclosure} | 1805 | \Deffunc{lua_pushcclosure} |
| 1804 | \begin{verbatim} | 1806 | \begin{verbatim} |
| 1805 | void lua_pushcclosure (lua_CFunction fn, int n); | 1807 | void lua_pushcclosure (lua_CFunction fn, int n); |
| @@ -1860,7 +1862,7 @@ The libraries, on the other hand, provide useful routines | |||
| 1860 | that are implemented directly through the standard API. | 1862 | that are implemented directly through the standard API. |
| 1861 | Therefore, they are not necessary to the language, | 1863 | Therefore, they are not necessary to the language, |
| 1862 | and are provided as separate C modules. | 1864 | and are provided as separate C modules. |
| 1863 | Currently there are three standard libraries: | 1865 | Currently, there are three standard libraries: |
| 1864 | \begin{itemize} | 1866 | \begin{itemize} |
| 1865 | \item string manipulation; | 1867 | \item string manipulation; |
| 1866 | \item mathematical functions (sin, log, etc); | 1868 | \item mathematical functions (sin, log, etc); |
| @@ -1908,14 +1910,14 @@ the error is propagated. | |||
| 1908 | If the string \verb|mode| contains \verb|"x"|, | 1910 | If the string \verb|mode| contains \verb|"x"|, |
| 1909 | then the call is \emph{protected}.\index{protected calls} | 1911 | then the call is \emph{protected}.\index{protected calls} |
| 1910 | In this mode, function \verb|call| does not propagate an error, | 1912 | In this mode, function \verb|call| does not propagate an error, |
| 1911 | whatever happens during the call. | 1913 | regardless of what happens during the call. |
| 1912 | Instead, it returns \nil\ to signal the error | 1914 | Instead, it returns \nil\ to signal the error |
| 1913 | (besides calling the appropriated error handler). | 1915 | (besides calling the appropriated error handler). |
| 1914 | 1916 | ||
| 1915 | If provided, | 1917 | If provided, |
| 1916 | \verb|errhandler| is temporarily set as the error function | 1918 | \verb|errhandler| is temporarily set as the error function |
| 1917 | \verb|_ERRORMESSAGE|, while \verb|func| runs. | 1919 | \verb|_ERRORMESSAGE|, while \verb|func| runs. |
| 1918 | As a particular example, if \verb|errhandler| is \nil, | 1920 | In particular, if \verb|errhandler| is \nil, |
| 1919 | no error messages will be issued during the execution of the called function. | 1921 | no error messages will be issued during the execution of the called function. |
| 1920 | 1922 | ||
| 1921 | \subsubsection*{\ff \T{collectgarbage ([limit])}}\Deffunc{collectgarbage} | 1923 | \subsubsection*{\ff \T{collectgarbage ([limit])}}\Deffunc{collectgarbage} |
| @@ -1977,7 +1979,7 @@ Therefore, the function only considers fields with non \nil\ values. | |||
| 1977 | The order in which the indices are enumerated is not specified, | 1979 | The order in which the indices are enumerated is not specified, |
| 1978 | \emph{even for numeric indices} | 1980 | \emph{even for numeric indices} |
| 1979 | (to traverse a table in numeric order, | 1981 | (to traverse a table in numeric order, |
| 1980 | use a counter or function \verb|foreachi|). | 1982 | use a counter or the function \verb|foreachi|). |
| 1981 | If the table indices are modified in any way during a traversal, | 1983 | If the table indices are modified in any way during a traversal, |
| 1982 | the semantics of \verb|next| is undefined. | 1984 | the semantics of \verb|next| is undefined. |
| 1983 | 1985 | ||
| @@ -2247,9 +2249,10 @@ except that the table accesses are all raw (that is, without tag methods): | |||
| 2247 | else | 2249 | else |
| 2248 | pos = arg[1]; value = arg[2] | 2250 | pos = arg[1]; value = arg[2] |
| 2249 | end | 2251 | end |
| 2250 | t.n = n+1; n=n+1 | 2252 | t.n = n+1; |
| 2251 | while (n=n-1)>=pos do | 2253 | while n >= pos do |
| 2252 | t[n+1] = t[n] | 2254 | t[n+1] = t[n] |
| 2255 | n = n-1 | ||
| 2253 | end | 2256 | end |
| 2254 | t[pos] = value | 2257 | t[pos] = value |
| 2255 | end | 2258 | end |
| @@ -2277,9 +2280,9 @@ except that the table accesses are all raw (that is, without tag methods): | |||
| 2277 | local value = t[pos] | 2280 | local value = t[pos] |
| 2278 | if n<=0 then return end | 2281 | if n<=0 then return end |
| 2279 | t.n = n-1 | 2282 | t.n = n-1 |
| 2280 | pos = pos-1 | 2283 | while pos < n do |
| 2281 | while (pos=pos+1)<n do | ||
| 2282 | t[pos] = t[pos+1] | 2284 | t[pos] = t[pos+1] |
| 2285 | pos = pos+1 | ||
| 2283 | end | 2286 | end |
| 2284 | return value | 2287 | return value |
| 2285 | end | 2288 | end |
| @@ -2452,7 +2455,7 @@ the maximum number of substitutions to occur. | |||
| 2452 | For instance, when \verb|n| is 1 only the first occurrence of | 2455 | For instance, when \verb|n| is 1 only the first occurrence of |
| 2453 | \verb|pat| is replaced. | 2456 | \verb|pat| is replaced. |
| 2454 | 2457 | ||
| 2455 | See some examples below: | 2458 | Here are some examples: |
| 2456 | \begin{verbatim} | 2459 | \begin{verbatim} |
| 2457 | x = gsub("hello world", "(%w%w*)", "%1 %1") | 2460 | x = gsub("hello world", "(%w%w*)", "%1 %1") |
| 2458 | --> x="hello hello world world" | 2461 | --> x="hello hello world world" |
| @@ -2474,8 +2477,7 @@ See some examples below: | |||
| 2474 | --> x="lua - 3.1" | 2477 | --> x="lua - 3.1" |
| 2475 | 2478 | ||
| 2476 | t = {n=0} | 2479 | t = {n=0} |
| 2477 | gsub("first second word", "(%w%w*)", | 2480 | gsub("first second word", "(%w%w*)", function (w) tinsert(%t, w) end) |
| 2478 | function (w) %t.n = %t.n+1; %t[%t.n] = w end) | ||
| 2479 | --> t={"first", "second", "word"; n=3} | 2481 | --> t={"first", "second", "word"; n=3} |
| 2480 | \end{verbatim} | 2482 | \end{verbatim} |
| 2481 | 2483 | ||
| @@ -2486,7 +2488,8 @@ See some examples below: | |||
| 2486 | a \Def{character class} is used to represent a set of characters. | 2488 | a \Def{character class} is used to represent a set of characters. |
| 2487 | The following combinations are allowed in describing a character class: | 2489 | The following combinations are allowed in describing a character class: |
| 2488 | \begin{description} | 2490 | \begin{description} |
| 2489 | \item[\emph{x}] (where \emph{x} is any character not in the list \verb|()%.[*-?|) | 2491 | \item[\emph{x}] (where \emph{x} is any character not in the list |
| 2492 | \verb|()%.[]*-?|) | ||
| 2490 | --- represents the character \emph{x} itself. | 2493 | --- represents the character \emph{x} itself. |
| 2491 | \item[\T{.}] --- (a dot) represents all characters. | 2494 | \item[\T{.}] --- (a dot) represents all characters. |
| 2492 | \item[\T{\%a}] --- represents all letters. | 2495 | \item[\T{\%a}] --- represents all letters. |
| @@ -2501,7 +2504,7 @@ The following combinations are allowed in describing a character class: | |||
| 2501 | \item[\T{\%z}] --- represents the character with representation 0. | 2504 | \item[\T{\%z}] --- represents the character with representation 0. |
| 2502 | \item[\T{\%\M{x}}] (where \M{x} is any non alphanumeric character) --- | 2505 | \item[\T{\%\M{x}}] (where \M{x} is any non alphanumeric character) --- |
| 2503 | represents the character \M{x}. | 2506 | represents the character \M{x}. |
| 2504 | This is the standard way to escape the magic characters \verb|()%.[*-?|. | 2507 | This is the standard way to escape the magic characters \verb|()%.[]*-?|. |
| 2505 | \item[\T{[char-set]}] --- | 2508 | \item[\T{[char-set]}] --- |
| 2506 | Represents the class which is the union of all | 2509 | Represents the class which is the union of all |
| 2507 | characters in char-set. | 2510 | characters in char-set. |
| @@ -2651,7 +2654,7 @@ This function opens a file, | |||
| 2651 | in the mode specified in the string \verb|mode|. | 2654 | in the mode specified in the string \verb|mode|. |
| 2652 | It returns a new file handle, | 2655 | It returns a new file handle, |
| 2653 | or, in case of errors, \nil\ plus a string describing the error. | 2656 | or, in case of errors, \nil\ plus a string describing the error. |
| 2654 | This function does not modify neither \verb|_INPUT| nor \verb|_OUTPUT|. | 2657 | This function does not modify either \verb|_INPUT| or \verb|_OUTPUT|. |
| 2655 | 2658 | ||
| 2656 | The string mode can be any of the following: | 2659 | The string mode can be any of the following: |
| 2657 | \begin{description} | 2660 | \begin{description} |
| @@ -2669,7 +2672,7 @@ which is needed in some systems to open the file in binary mode. | |||
| 2669 | \subsubsection*{\ff \T{closefile (handle)}}\Deffunc{closefile} | 2672 | \subsubsection*{\ff \T{closefile (handle)}}\Deffunc{closefile} |
| 2670 | 2673 | ||
| 2671 | This function closes the given file. | 2674 | This function closes the given file. |
| 2672 | It does not modify neither \verb|_INPUT| nor \verb|_OUTPUT|. | 2675 | It does not modify either \verb|_INPUT| or \verb|_OUTPUT|. |
| 2673 | 2676 | ||
| 2674 | \subsubsection*{\ff \T{readfrom (filename)}}\Deffunc{readfrom} | 2677 | \subsubsection*{\ff \T{readfrom (filename)}}\Deffunc{readfrom} |
| 2675 | 2678 | ||
| @@ -2783,7 +2786,7 @@ Returns a string with a file name that can safely | |||
| 2783 | be used for a temporary file. | 2786 | be used for a temporary file. |
| 2784 | The file must be explicitly removed when no longer needed. | 2787 | The file must be explicitly removed when no longer needed. |
| 2785 | 2788 | ||
| 2786 | \subsubsection*{\ff \T{read ([filehandle] {readpattern})}}\Deffunc{read} | 2789 | \subsubsection*{\ff \T{read ([filehandle,] readpattern1, ...)}}\Deffunc{read} |
| 2787 | 2790 | ||
| 2788 | Reads file \verb|_INPUT|, | 2791 | Reads file \verb|_INPUT|, |
| 2789 | or \verb|filehandle| if this argument is given, | 2792 | or \verb|filehandle| if this argument is given, |
| @@ -2822,15 +2825,15 @@ but are not included in the resulting string. | |||
| 2822 | 2825 | ||
| 2823 | There are some predefined patterns, as follows: | 2826 | There are some predefined patterns, as follows: |
| 2824 | \begin{description} | 2827 | \begin{description} |
| 2825 | \item["*n"] reads a number; | 2828 | \item[``*n''] reads a number; |
| 2826 | this is the only pattern that returns a number instead of a string. | 2829 | this is the only pattern that returns a number instead of a string. |
| 2827 | \item["*l"] returns the next line | 2830 | \item[``*l''] returns the next line |
| 2828 | (skipping the end of line), or \nil\ on end of file. | 2831 | (skipping the end of line), or \nil\ on end of file. |
| 2829 | This is the default pattern. | 2832 | This is the default pattern. |
| 2830 | It is equivalent to the pattern \verb|"[^\n]*{\n}"|. | 2833 | It is equivalent to the pattern \verb|"[^\n]*{\n}"|. |
| 2831 | \item["*a"] reads the whole file. | 2834 | \item[``*a''] reads the whole file. |
| 2832 | It is equivalent to the pattern \verb|".*"|. | 2835 | It is equivalent to the pattern \verb|".*"|. |
| 2833 | \item["*w"] returns the next word | 2836 | \item[``*w''] returns the next word |
| 2834 | (maximal sequence of non white-space characters), | 2837 | (maximal sequence of non white-space characters), |
| 2835 | skipping spaces if necessary, or \nil\ on end of file. | 2838 | skipping spaces if necessary, or \nil\ on end of file. |
| 2836 | It is equivalent to the pattern \verb|"{%s*}%S%S*"|. | 2839 | It is equivalent to the pattern \verb|"{%s*}%S%S*"|. |
| @@ -2854,7 +2857,7 @@ formatted according to the given string \verb|format|, | |||
| 2854 | following the same rules of the ANSI C function \verb|strftime|. | 2857 | following the same rules of the ANSI C function \verb|strftime|. |
| 2855 | When called without arguments, | 2858 | When called without arguments, |
| 2856 | it returns a reasonable date and time representation that depends on | 2859 | it returns a reasonable date and time representation that depends on |
| 2857 | the host system and the locale. | 2860 | the host system and on the locale. |
| 2858 | 2861 | ||
| 2859 | \subsubsection*{\ff \T{clock ()}}\Deffunc{clock} | 2862 | \subsubsection*{\ff \T{clock ()}}\Deffunc{clock} |
| 2860 | 2863 | ||
| @@ -2961,11 +2964,12 @@ Because functions in Lua are first class values, | |||
| 2961 | they do not have a fixed name: | 2964 | they do not have a fixed name: |
| 2962 | Some functions may be the value of many global variables, | 2965 | Some functions may be the value of many global variables, |
| 2963 | while others may be stored only in a table field. | 2966 | while others may be stored only in a table field. |
| 2964 | Function \verb|lua_getobjname| first checks whether the given | 2967 | Function \verb|lua_getobjname| checks whether the given |
| 2965 | function is a tag method. | 2968 | function is a tag method or the value of a global variable. |
| 2966 | If so, it returns the string \verb|"tag-method"|, | 2969 | If the given function is a tag method, then \verb|lua_getobjname| |
| 2970 | returns the string \verb|"tag-method"|, | ||
| 2967 | and \verb|name| is set to point to the event name. | 2971 | and \verb|name| is set to point to the event name. |
| 2968 | Otherwise, if the given function is the value of a global variable, | 2972 | If the given function is the value of a global variable, |
| 2969 | then \verb|lua_getobjname| returns the string \verb|"global"|, | 2973 | then \verb|lua_getobjname| returns the string \verb|"global"|, |
| 2970 | and \verb|name| points to the variable name. | 2974 | and \verb|name| points to the variable name. |
| 2971 | If the given function is neither a tag method nor a global variable, | 2975 | If the given function is neither a tag method nor a global variable, |
| @@ -3014,7 +3018,7 @@ lua_CHFunction lua_setcallhook (lua_CHFunction func); | |||
| 3014 | typedef void (*lua_LHFunction) (int line); | 3018 | typedef void (*lua_LHFunction) (int line); |
| 3015 | lua_LHFunction lua_setlinehook (lua_LHFunction func); | 3019 | lua_LHFunction lua_setlinehook (lua_LHFunction func); |
| 3016 | \end{verbatim} | 3020 | \end{verbatim} |
| 3017 | The first one is called whenever the interpreter enters or leaves a | 3021 | The first hook is called whenever the interpreter enters or leaves a |
| 3018 | function. | 3022 | function. |
| 3019 | When entering a function, | 3023 | When entering a function, |
| 3020 | its parameters are a handle to the function activation record, | 3024 | its parameters are a handle to the function activation record, |
| @@ -3043,12 +3047,12 @@ set their corresponding hooks and return their previous values. | |||
| 3043 | The library \verb|ldblib| provides | 3047 | The library \verb|ldblib| provides |
| 3044 | the functionallity of the debugger interface to Lua programs. | 3048 | the functionallity of the debugger interface to Lua programs. |
| 3045 | If you want to use this library, | 3049 | If you want to use this library, |
| 3046 | your host application must open it first, | 3050 | your host application must open it, |
| 3047 | calling \verb|lua_dblibopen|. | 3051 | calling \verb|lua_dblibopen|. |
| 3048 | 3052 | ||
| 3049 | You should exert great care when using this library. | 3053 | You should exert great care when using this library. |
| 3050 | Functions provided here should be used exclusively for debugging | 3054 | The functions provided here should be used exclusively for debugging |
| 3051 | and similar tasks (e.g. profiling): | 3055 | and similar tasks (e.g. profiling). |
| 3052 | Please resist the temptation to use them as a | 3056 | Please resist the temptation to use them as a |
| 3053 | usual programming tool. | 3057 | usual programming tool. |
| 3054 | They are slow and violate some (otherwise) secure aspects of the | 3058 | They are slow and violate some (otherwise) secure aspects of the |
| @@ -3072,7 +3076,7 @@ If the function was defined in a string, | |||
| 3072 | If the function was defined in a file, | 3076 | If the function was defined in a file, |
| 3073 | \verb|source| starts with a \verb|@| followed by the file name. | 3077 | \verb|source| starts with a \verb|@| followed by the file name. |
| 3074 | 3078 | ||
| 3075 | \item[def_line] the line where the function was defined in the source | 3079 | \item[def\_line] the line where the function was defined in the source |
| 3076 | (only valid if this is a Lua function). | 3080 | (only valid if this is a Lua function). |
| 3077 | 3081 | ||
| 3078 | \item[where] can be \verb|"global"| if this function has a global name, | 3082 | \item[where] can be \verb|"global"| if this function has a global name, |
| @@ -3225,8 +3229,8 @@ Here is a list of all these incompatibilities. | |||
| 3225 | \item | 3229 | \item |
| 3226 | In the debug API, the old variables \verb|lua_debug|, | 3230 | In the debug API, the old variables \verb|lua_debug|, |
| 3227 | \verb|lua_callhook| and \verb|lua_linehook| now live inside \verb|lua_state|. | 3231 | \verb|lua_callhook| and \verb|lua_linehook| now live inside \verb|lua_state|. |
| 3228 | Therefore, they are no more directly accessible, and must be | 3232 | Therefore, they are no longer directly accessible, and must be |
| 3229 | manipulated through the new functions \verb|lua_setdebug|, | 3233 | manipulated only through the new functions \verb|lua_setdebug|, |
| 3230 | \verb|lua_setcallhook| and \verb|lua_setlinehook|. | 3234 | \verb|lua_setcallhook| and \verb|lua_setlinehook|. |
| 3231 | 3235 | ||
| 3232 | \item Old pre-compiled code is obsolete, and must be re-compiled. | 3236 | \item Old pre-compiled code is obsolete, and must be re-compiled. |
