diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-11-01 15:02:10 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-11-01 15:02:10 -0200 |
commit | 2f44cc9f4d62cbdc6e752d5328408fc5123bedcc (patch) | |
tree | 13ad26e096a874166522f1b5bc257298c39288b4 | |
parent | d106f3f43c147b872df7b3304b2e42e92bc1b7bf (diff) | |
download | lua-2f44cc9f4d62cbdc6e752d5328408fc5123bedcc.tar.gz lua-2f44cc9f4d62cbdc6e752d5328408fc5123bedcc.tar.bz2 lua-2f44cc9f4d62cbdc6e752d5328408fc5123bedcc.zip |
new pattern matching facilities; new version of i/o lib;
other small corrections.
-rw-r--r-- | manual.tex | 552 |
1 files changed, 363 insertions, 189 deletions
@@ -1,4 +1,4 @@ | |||
1 | % $Id: manual.tex,v 1.18 1996/06/18 20:08:40 roberto Exp roberto $ | 1 | % $Id: manual.tex,v 1.19 1996/08/28 20:46:26 roberto Exp roberto $ |
2 | 2 | ||
3 | \documentstyle[fullpage,11pt,bnf]{article} | 3 | \documentstyle[fullpage,11pt,bnf]{article} |
4 | 4 | ||
@@ -13,7 +13,9 @@ | |||
13 | \newcommand{\Def}[1]{{\em #1}\index{#1}} | 13 | \newcommand{\Def}[1]{{\em #1}\index{#1}} |
14 | \newcommand{\Deffunc}[1]{\index{#1}} | 14 | \newcommand{\Deffunc}[1]{\index{#1}} |
15 | 15 | ||
16 | \newcommand{\Version}{2.4} | 16 | \newcommand{\ff}{$\bullet$\ } |
17 | |||
18 | \newcommand{\Version}{2.5 (beta)} | ||
17 | 19 | ||
18 | \makeindex | 20 | \makeindex |
19 | 21 | ||
@@ -24,7 +26,7 @@ | |||
24 | \author{% | 26 | \author{% |
25 | Roberto Ierusalimschy\quad | 27 | Roberto Ierusalimschy\quad |
26 | Luiz Henrique de Figueiredo\quad | 28 | Luiz Henrique de Figueiredo\quad |
27 | Waldemar Celes Filho | 29 | Waldemar Celes |
28 | \vspace{1.0ex}\\ | 30 | \vspace{1.0ex}\\ |
29 | %\small \tecgraf \ --- PUC-Rio\\ | 31 | %\small \tecgraf \ --- PUC-Rio\\ |
30 | \smallskip | 32 | \smallskip |
@@ -34,7 +36,7 @@ Waldemar Celes Filho | |||
34 | \tecgraf\ --- Departamento de Inform\'atica --- PUC-Rio | 36 | \tecgraf\ --- Departamento de Inform\'atica --- PUC-Rio |
35 | } | 37 | } |
36 | 38 | ||
37 | \date{\small \verb$Date: 1996/06/18 20:08:40 $} | 39 | \date{\small \verb$Date: 1996/08/28 20:46:26 $} |
38 | 40 | ||
39 | \maketitle | 41 | \maketitle |
40 | 42 | ||
@@ -72,11 +74,8 @@ general procedural programming features with data description | |||
72 | facilities. | 74 | facilities. |
73 | It is intended to be used as a configuration language for any | 75 | It is intended to be used as a configuration language for any |
74 | program that needs one. | 76 | program that needs one. |
75 | %Its main extensions are related to object-oriented facilities, | ||
76 | %and fallbacks, | ||
77 | %but it has some other minor contributions. | ||
78 | Lua has been designed and implemented by | 77 | Lua has been designed and implemented by |
79 | W.~Celes~F., L.~H.~de Figueiredo and R.~Ierusalimschy. | 78 | W.~Celes, L.~H.~de Figueiredo and R.~Ierusalimschy. |
80 | 79 | ||
81 | Lua is implemented as a library, written in C. | 80 | Lua is implemented as a library, written in C. |
82 | Being an extension language, Lua has no notion of a ``main'' program: | 81 | Being an extension language, Lua has no notion of a ``main'' program: |
@@ -123,10 +122,11 @@ The syntax% | |||
123 | one or more {\em a\/}'s.} | 122 | one or more {\em a\/}'s.} |
124 | for chunks is: | 123 | for chunks is: |
125 | \begin{Produc} | 124 | \begin{Produc} |
126 | \produc{chunk}{\rep{statement \Or function}} | 125 | \produc{chunk}{\rep{statement \Or function} \opt{ret}} |
127 | \end{Produc}% | 126 | \end{Produc}% |
128 | A chunk may contain statements and function definitions, | 127 | A chunk may contain statements and function definitions, |
129 | and may be in a file or in a string inside the host program. | 128 | and may be in a file or in a string inside the host program. |
129 | A chunk may optionally ends with a return statement (\see{return}). | ||
130 | When a chunk is executed, first all its functions and statements are compiled, | 130 | When a chunk is executed, first all its functions and statements are compiled, |
131 | then the statements are executed in sequential order. | 131 | then the statements are executed in sequential order. |
132 | All modifications a chunk effects on the global environment persist | 132 | All modifications a chunk effects on the global environment persist |
@@ -231,6 +231,10 @@ and do not interpret escape sequences. | |||
231 | 231 | ||
232 | \Index{Comments} start anywhere outside a string with a | 232 | \Index{Comments} start anywhere outside a string with a |
233 | double hyphen (\verb'--') and run until the end of the line. | 233 | double hyphen (\verb'--') and run until the end of the line. |
234 | Moreover, if the first line of a chunk file starts with \verb'#', | ||
235 | this line is skiped% | ||
236 | \footnote{This facility allows the use of Lua as a script interpreter | ||
237 | in Unix systems.}. | ||
234 | 238 | ||
235 | \Index{Numerical constants} may be written with an optional decimal part, | 239 | \Index{Numerical constants} may be written with an optional decimal part, |
236 | and an optional decimal exponent. | 240 | and an optional decimal exponent. |
@@ -281,7 +285,7 @@ and local variable declarations (Section~\ref{localvar}). | |||
281 | A \Index{block} is a list of statements, which is executed sequentially. | 285 | A \Index{block} is a list of statements, which is executed sequentially. |
282 | Any statement can be optionally followed by a semicolon: | 286 | Any statement can be optionally followed by a semicolon: |
283 | \begin{Produc} | 287 | \begin{Produc} |
284 | \produc{block}{\rep{stat sc} \opt{ret sc}} | 288 | \produc{block}{\rep{stat sc} \opt{ret}} |
285 | \produc{sc}{\opt{\ter{;}}} | 289 | \produc{sc}{\opt{\ter{;}}} |
286 | \end{Produc}% | 290 | \end{Produc}% |
287 | For syntactic reasons, a \IndexVerb{return} statement can only be written | 291 | For syntactic reasons, a \IndexVerb{return} statement can only be written |
@@ -344,11 +348,12 @@ All values different from \nil\ are considered true; | |||
344 | \produc{elseif}{\rwd{elseif} exp1 \rwd{then} block} | 348 | \produc{elseif}{\rwd{elseif} exp1 \rwd{then} block} |
345 | \end{Produc} | 349 | \end{Produc} |
346 | 350 | ||
347 | A {\tt return} is used to return values from a function. \label{return} | 351 | A {\tt return} is used to return values from a function or a chunk. |
348 | Because a function may return more than one value, | 352 | \label{return} |
353 | Because they may return more than one value, | ||
349 | the syntax for a \Index{return statement} is: | 354 | the syntax for a \Index{return statement} is: |
350 | \begin{Produc} | 355 | \begin{Produc} |
351 | \produc{ret}{\rwd{return} explist} | 356 | \produc{ret}{\rwd{return} explist \opt{sc}} |
352 | \end{Produc} | 357 | \end{Produc} |
353 | 358 | ||
354 | \subsubsection{Function Calls as Statements} \label{funcstat} | 359 | \subsubsection{Function Calls as Statements} \label{funcstat} |
@@ -434,7 +439,12 @@ The \Index{logical operators} are: | |||
434 | \begin{verbatim} | 439 | \begin{verbatim} |
435 | and or not | 440 | and or not |
436 | \end{verbatim} | 441 | \end{verbatim} |
437 | The operators \verb'and' and \verb'or' use \Index{short-cut evaluation}, | 442 | The operator \verb'and' returns \nil\ if its first argument is \nil; |
443 | otherwise it returns its second argument. | ||
444 | The operator \verb'or' returns its first argument | ||
445 | if it is different from \nil; | ||
446 | otherwise it returns its second argument. | ||
447 | Both \verb'and' and \verb'or' use \Index{short-cut evaluation}, | ||
438 | that is, | 448 | that is, |
439 | the second operand is evaluated only if necessary. | 449 | the second operand is evaluated only if necessary. |
440 | 450 | ||
@@ -485,7 +495,7 @@ For example: | |||
485 | \begin{verbatim} | 495 | \begin{verbatim} |
486 | a = {"v1", "v2", 34} | 496 | a = {"v1", "v2", 34} |
487 | \end{verbatim} | 497 | \end{verbatim} |
488 | is equivalent to: | 498 | is roughly equivalent to: |
489 | \begin{verbatim} | 499 | \begin{verbatim} |
490 | temp = {} | 500 | temp = {} |
491 | temp[1] = "v1" | 501 | temp[1] = "v1" |
@@ -503,11 +513,11 @@ For example: | |||
503 | \begin{verbatim} | 513 | \begin{verbatim} |
504 | a = {x = 1, y = 3} | 514 | a = {x = 1, y = 3} |
505 | \end{verbatim} | 515 | \end{verbatim} |
506 | is equivalent to: | 516 | is roughly equivalent to: |
507 | \begin{verbatim} | 517 | \begin{verbatim} |
508 | temp = {} | 518 | temp = {} |
509 | temp.x = 1 | 519 | temp.x = 1 -- or temp["x"] = 1 |
510 | temp.y = 3 | 520 | temp.y = 3 -- or temp["y"] = 3 |
511 | a = temp | 521 | a = temp |
512 | \end{verbatim} | 522 | \end{verbatim} |
513 | 523 | ||
@@ -518,7 +528,7 @@ A \Index{function call} has the following syntax: | |||
518 | \produc{functioncall}{var realParams} | 528 | \produc{functioncall}{var realParams} |
519 | \end{Produc}% | 529 | \end{Produc}% |
520 | Here, \verb'var' can be any variable (global, local, indexed, etc). | 530 | Here, \verb'var' can be any variable (global, local, indexed, etc). |
521 | If its type is {\em function\/} or {\em CFunction\/}, | 531 | If its value has type {\em function\/} or {\em CFunction\/}, |
522 | then this function is called. | 532 | then this function is called. |
523 | Otherwise, the ``function'' fallback is called, | 533 | Otherwise, the ``function'' fallback is called, |
524 | having as first parameter the value of \verb'var', | 534 | having as first parameter the value of \verb'var', |
@@ -630,7 +640,7 @@ and one of the following strings describing the offended operator: | |||
630 | add sub mul div pow unm | 640 | add sub mul div pow unm |
631 | \end{verbatim} | 641 | \end{verbatim} |
632 | Its return value is the final result of the arithmetic operation. | 642 | Its return value is the final result of the arithmetic operation. |
633 | The default function issues an error. | 643 | The default handler issues an error. |
634 | \item[``order'':]\index{order fallback} | 644 | \item[``order'':]\index{order fallback} |
635 | called when an order comparison is applied to non numerical or | 645 | called when an order comparison is applied to non numerical or |
636 | non string operands. | 646 | non string operands. |
@@ -641,40 +651,40 @@ one of the following strings describing the offended operator: | |||
641 | lt gt le ge | 651 | lt gt le ge |
642 | \end{verbatim} | 652 | \end{verbatim} |
643 | Its return value is the final result of the comparison operation. | 653 | Its return value is the final result of the comparison operation. |
644 | The default function issues an error. | 654 | The default handler issues an error. |
645 | \item[``concat'':]\index{concatenation fallback} | 655 | \item[``concat'':]\index{concatenation fallback} |
646 | called when a concatenation is applied to non string operands. | 656 | called when a concatenation is applied to non string operands. |
647 | It receives the two operands as arguments. | 657 | It receives the two operands as arguments. |
648 | Its return value is the final result of the concatenation operation. | 658 | Its return value is the final result of the concatenation operation. |
649 | The default function issues an error. | 659 | The default handler issues an error. |
650 | \item[``index'':]\index{index fallback} | 660 | \item[``index'':]\index{index fallback} |
651 | called when Lua tries to retrieve the value of an index | 661 | called when Lua tries to retrieve the value of an index |
652 | not present in a table. | 662 | not present in a table. |
653 | It receives as arguments the table and the index. | 663 | It receives as arguments the table and the index. |
654 | Its return value is the final result of the indexing operation. | 664 | Its return value is the final result of the indexing operation. |
655 | The default function returns nil. | 665 | The default handler returns nil. |
656 | \item[``getglobal'':]\index{index getglobal} | 666 | \item[``getglobal'':]\index{index getglobal} |
657 | called when Lua tries to retrieve the value of a global variable | 667 | called when Lua tries to retrieve the value of a global variable |
658 | which has a nil value (or which has not been initialized). | 668 | which has a nil value (or which has not been initialized). |
659 | It receives as argument the name of the variable. | 669 | It receives as argument the name of the variable. |
660 | Its return value is the final result of the expression. | 670 | Its return value is the final result of the expression. |
661 | The default function returns nil. | 671 | The default handler returns nil. |
662 | \item[``gettable'':]\index{gettable fallback} | 672 | \item[``gettable'':]\index{gettable fallback} |
663 | called when Lua tries to index a non table value. | 673 | called when Lua tries to index a non table value. |
664 | It receives as arguments the non table value and the index. | 674 | It receives as arguments the non table value and the index. |
665 | Its return value is the final result of the indexing operation. | 675 | Its return value is the final result of the indexing operation. |
666 | The default function issues an error. | 676 | The default handler issues an error. |
667 | \item[``settable'':]\index{settable fallback} | 677 | \item[``settable'':]\index{settable fallback} |
668 | called when Lua tries to assign indexed a non table value. | 678 | called when Lua tries to assign indexed a non table value. |
669 | It receives as arguments the non table value, | 679 | It receives as arguments the non table value, |
670 | the index, and the assigned value. | 680 | the index, and the assigned value. |
671 | The default function issues an error. | 681 | The default handler issues an error. |
672 | \item[``function'':]\index{function falback} | 682 | \item[``function'':]\index{function falback} |
673 | called when Lua tries to call a non function value. | 683 | called when Lua tries to call a non function value. |
674 | It receives as arguments the non function value and the | 684 | It receives as arguments the non function value and the |
675 | arguments given in the original call. | 685 | arguments given in the original call. |
676 | Its return values are the final results of the call operation. | 686 | Its return values are the final results of the call operation. |
677 | The default function issues an error. | 687 | The default handler issues an error. |
678 | \item[``gc'':] | 688 | \item[``gc'':] |
679 | called during garbage collection. | 689 | called during garbage collection. |
680 | It receives as argument the table being collected. | 690 | It receives as argument the table being collected. |
@@ -683,17 +693,17 @@ Because this function operates during garbage collection, | |||
683 | it must be used with great care, | 693 | it must be used with great care, |
684 | and programmers should avoid the creation of new objects | 694 | and programmers should avoid the creation of new objects |
685 | (tables or strings) in this function. | 695 | (tables or strings) in this function. |
686 | The default function does nothing. | 696 | The default handler does nothing. |
687 | \item[``error'':]\index{error fallback} | 697 | \item[``error'':]\index{error fallback} |
688 | called when an error occurs. | 698 | called when an error occurs. |
689 | It receives as argument a string describing the error. | 699 | It receives as argument a string describing the error. |
690 | The default function prints the message on the standard error output. | 700 | The default handler prints the message on the standard error output. |
691 | \end{description} | 701 | \end{description} |
692 | 702 | ||
693 | The function \IndexVerb{setfallback} is used to change a fallback function. | 703 | The function \IndexVerb{setfallback} is used to change a fallback handler. |
694 | Its first argument is the name of a fallback condition, | 704 | Its first argument is the name of a fallback condition, |
695 | and the second argument is the new function to be called. | 705 | and the second argument is the new function to be called. |
696 | It returns the old function for the given fallback. | 706 | It returns the old handler function for the given fallback. |
697 | 707 | ||
698 | Section \ref{exfallback} shows an example of the use of fallbacks. | 708 | Section \ref{exfallback} shows an example of the use of fallbacks. |
699 | 709 | ||
@@ -706,7 +716,7 @@ Whenever an error occurs during Lua compilation or execution, | |||
706 | an ``error'' fallback function is called, | 716 | an ``error'' fallback function is called, |
707 | and then the corresponding function from the library | 717 | and then the corresponding function from the library |
708 | (\verb'lua_dofile', \verb'lua_dostring', | 718 | (\verb'lua_dofile', \verb'lua_dostring', |
709 | \verb'lua_call', and \verb'lua_callfunction') | 719 | \verb'lua_call', or \verb'lua_callfunction') |
710 | is terminated returning an error condition. | 720 | is terminated returning an error condition. |
711 | 721 | ||
712 | The only argument to the ``error'' fallback function is a string | 722 | The only argument to the ``error'' fallback function is a string |
@@ -722,10 +732,10 @@ This pragma must be written in a line by itself. | |||
722 | When an error occurs in a program compiled with this option, | 732 | When an error occurs in a program compiled with this option, |
723 | the error routine is able to print also the lines where the calls | 733 | the error routine is able to print also the lines where the calls |
724 | (and the error) were made. | 734 | (and the error) were made. |
725 | If needed, it is possible to change the ``error'' fallback routine | 735 | If needed, it is possible to change the ``error'' fallback handler |
726 | (\see{fallback}). | 736 | (\see{fallback}). |
727 | 737 | ||
728 | Lua code can generate an error by calling the built-in | 738 | Lua code can explicitly generate an error by calling the built-in |
729 | function \verb'error' (\see{pdf-error}). | 739 | function \verb'error' (\see{pdf-error}). |
730 | 740 | ||
731 | 741 | ||
@@ -917,16 +927,6 @@ As in Lua, if the first object is not a table, | |||
917 | or the index is not present in the table, | 927 | or the index is not present in the table, |
918 | the corresponding fallback is called. | 928 | the corresponding fallback is called. |
919 | 929 | ||
920 | %For compatibility with previous versions of the API, | ||
921 | %the following macros are supported: | ||
922 | %\Deffunc{lua_getindexed}\Deffunc{lua_getfield} | ||
923 | %\begin{verbatim} | ||
924 | %lua_Object lua_getindexed (lua_Object table, float index); | ||
925 | %lua_Object lua_getfield (lua_Object table, char *field); | ||
926 | %\end{verbatim} | ||
927 | %The first one is used for numeric indices, | ||
928 | %while the second can be used for any string index. | ||
929 | |||
930 | To store a value in an index, | 930 | To store a value in an index, |
931 | the program must push onto the stack the table, the index, | 931 | the program must push onto the stack the table, the index, |
932 | and the value, | 932 | and the value, |
@@ -1133,20 +1133,24 @@ declared in \verb-lualib.h-. | |||
1133 | 1133 | ||
1134 | \subsection{Predefined Functions} | 1134 | \subsection{Predefined Functions} |
1135 | 1135 | ||
1136 | \subsubsection*{{\tt dofile (filename)}}\Deffunc{dofile} | 1136 | \subsubsection*{\ff{\tt dofile (filename)}}\Deffunc{dofile} |
1137 | This function receives a file name, | 1137 | This function receives a file name, |
1138 | opens it, and executes its contents as a Lua chunk, | 1138 | opens it, and executes its contents as a Lua chunk, |
1139 | or as pre-compiled chunks. | 1139 | or as pre-compiled chunks. |
1140 | When called without arguments, | 1140 | When called without arguments, |
1141 | it executes the contents of the standard input (\verb'stdin'). | 1141 | it executes the contents of the standard input (\verb'stdin'). |
1142 | It returns 1 if there are no errors, \nil\ otherwise. | 1142 | If there is any error executing the file, it returns \nil. |
1143 | Otherwise, it returns the values returned by the chunk, | ||
1144 | or a non \nil\ value if the chunk returns no values. | ||
1143 | It issues an error when called with a non string argument. | 1145 | It issues an error when called with a non string argument. |
1144 | 1146 | ||
1145 | \subsubsection*{{\tt dostring (string)}}\Deffunc{dostring} | 1147 | \subsubsection*{\ff{\tt dostring (string)}}\Deffunc{dostring} |
1146 | This function executes a given string as a Lua chunk. | 1148 | This function executes a given string as a Lua chunk. |
1147 | It returns 1 if there are no errors, \nil\ otherwise. | 1149 | If there is any error executing the string, it returns \nil. |
1150 | Otherwise, it returns the values returned by the chunk, | ||
1151 | or a non \nil\ value if the chunk returns no values. | ||
1148 | 1152 | ||
1149 | \subsubsection*{{\tt next (table, index)}}\Deffunc{next} | 1153 | \subsubsection*{\ff{\tt next (table, index)}}\Deffunc{next} |
1150 | This function allows a program to traverse all fields of a table. | 1154 | This function allows a program to traverse all fields of a table. |
1151 | Its first argument is a table and its second argument | 1155 | Its first argument is a table and its second argument |
1152 | is an index in this table. | 1156 | is an index in this table. |
@@ -1167,7 +1171,7 @@ The order the indices are enumerated is not specified, | |||
1167 | 1171 | ||
1168 | See Section \ref{exnext} for an example of the use of this function. | 1172 | See Section \ref{exnext} for an example of the use of this function. |
1169 | 1173 | ||
1170 | \subsubsection*{{\tt nextvar (name)}}\Deffunc{nextvar} | 1174 | \subsubsection*{\ff{\tt nextvar (name)}}\Deffunc{nextvar} |
1171 | This function is similar to the function \verb'next', | 1175 | This function is similar to the function \verb'next', |
1172 | but iterates over the global variables. | 1176 | but iterates over the global variables. |
1173 | Its single argument is the name of a global variable, | 1177 | Its single argument is the name of a global variable, |
@@ -1177,11 +1181,11 @@ and its value, | |||
1177 | or \nil\ if there are no more variables. | 1181 | or \nil\ if there are no more variables. |
1178 | See Section \ref{exnext} for an example of the use of this function. | 1182 | See Section \ref{exnext} for an example of the use of this function. |
1179 | 1183 | ||
1180 | \subsubsection*{{\tt tostring (e)}}\Deffunc{tostring} | 1184 | \subsubsection*{\ff{\tt tostring (e)}}\Deffunc{tostring} |
1181 | This function receives an argument of any type and | 1185 | This function receives an argument of any type and |
1182 | converts it to a string in a reasonable format. | 1186 | converts it to a string in a reasonable format. |
1183 | 1187 | ||
1184 | \subsubsection*{{\tt print (e1, e2, ...)}}\Deffunc{print} | 1188 | \subsubsection*{\ff{\tt print (e1, e2, ...)}}\Deffunc{print} |
1185 | This function receives any number of arguments, | 1189 | This function receives any number of arguments, |
1186 | and prints their values in a reasonable format. | 1190 | and prints their values in a reasonable format. |
1187 | Each value is printed in a new line. | 1191 | Each value is printed in a new line. |
@@ -1190,14 +1194,14 @@ but as a quick way to show a value, | |||
1190 | for instance for error messages or debugging. | 1194 | for instance for error messages or debugging. |
1191 | See Section~\ref{libio} for functions for formatted output. | 1195 | See Section~\ref{libio} for functions for formatted output. |
1192 | 1196 | ||
1193 | \subsubsection*{{\tt tonumber (e)}}\Deffunc{tonumber} | 1197 | \subsubsection*{\ff{\tt tonumber (e)}}\Deffunc{tonumber} |
1194 | This function receives one argument, | 1198 | This function receives one argument, |
1195 | and tries to convert it to a number. | 1199 | and tries to convert it to a number. |
1196 | If the argument is already a number or a string convertible | 1200 | If the argument is already a number or a string convertible |
1197 | to a number (\see{coercion}), then it returns that number; | 1201 | to a number (\see{coercion}), then it returns that number; |
1198 | otherwise, it returns \nil. | 1202 | otherwise, it returns \nil. |
1199 | 1203 | ||
1200 | \subsubsection*{{\tt type (v)}}\Deffunc{type} | 1204 | \subsubsection*{\ff{\tt type (v)}}\Deffunc{type} |
1201 | This function allows Lua to test the type of a value. | 1205 | This function allows Lua to test the type of a value. |
1202 | It receives one argument, and returns its type, coded as a string. | 1206 | It receives one argument, and returns its type, coded as a string. |
1203 | The possible results of this function are | 1207 | The possible results of this function are |
@@ -1214,54 +1218,59 @@ This tag can be used to distinguish between user | |||
1214 | data with different tags, | 1218 | data with different tags, |
1215 | and between C functions and Lua functions. | 1219 | and between C functions and Lua functions. |
1216 | 1220 | ||
1217 | \subsubsection*{{\tt assert (v)}}\Deffunc{assert} | 1221 | \subsubsection*{\ff{\tt assert (v)}}\Deffunc{assert} |
1218 | This function issues an {\em ``assertion failed!''} error | 1222 | This function issues an {\em ``assertion failed!''} error |
1219 | when its argument is \nil. | 1223 | when its argument is \nil. |
1220 | 1224 | ||
1221 | \subsubsection*{{\tt error (message)}}\Deffunc{error}\label{pdf-error} | 1225 | \subsubsection*{\ff{\tt error (message)}}\Deffunc{error}\label{pdf-error} |
1222 | This function issues an error message and terminates | 1226 | This function issues an error message and terminates |
1223 | the last called function from the library | 1227 | the last called function from the library |
1224 | (\verb'lua_dofile', \verb'lua_dostring', \ldots). | 1228 | (\verb'lua_dofile', \verb'lua_dostring', \ldots). |
1225 | It never returns. | 1229 | It never returns. |
1226 | 1230 | ||
1227 | \subsubsection*{{\tt setglobal (name, value)}}\Deffunc{setglobal} | 1231 | \subsubsection*{\ff{\tt setglobal (name, value)}}\Deffunc{setglobal} |
1228 | This function assigns the given value to a global variable. | 1232 | This function assigns the given value to a global variable. |
1229 | The string \verb'name' does not need to be a syntactically valid variable name. | 1233 | The string \verb'name' does not need to be a syntactically valid variable name. |
1230 | Therefore, this function can set global variables with strange names like | 1234 | Therefore, this function can set global variables with strange names like |
1231 | \verb'm v 1' or \verb'34'. | 1235 | \verb|`m v 1'| or \verb'34'. |
1232 | It returns the value of its second argument. | 1236 | It returns the value of its second argument. |
1233 | 1237 | ||
1234 | \subsubsection*{{\tt getglobal (name)}}\Deffunc{getglobal} | 1238 | \subsubsection*{\ff{\tt getglobal (name)}}\Deffunc{getglobal} |
1235 | This function retrieves the value of a global variable. | 1239 | This function retrieves the value of a global variable. |
1236 | The string \verb'name' does not need to be a syntactically valid variable name. | 1240 | The string \verb'name' does not need to be a syntactically valid variable name. |
1237 | 1241 | ||
1238 | \subsubsection*{{\tt setfallback (fallbackname, newfallback)}} | 1242 | \subsubsection*{\ff{\tt setfallback (fallbackname, newfallback)}} |
1239 | \Deffunc{setfallback} | 1243 | \Deffunc{setfallback} |
1240 | This function sets a new fallback function to the given fallback. | 1244 | This function sets a new fallback function to the given fallback. |
1241 | It returns the old fallback function. | 1245 | It returns the old fallback function. |
1242 | 1246 | ||
1243 | \subsection{String Manipulation} | 1247 | \subsection{String Manipulation} |
1244 | This library provides generic functions for string manipulation, | 1248 | This library provides generic functions for string manipulation, |
1245 | such as finding and extracting substrings. | 1249 | such as finding and extracting substrings and pattern matching. |
1246 | When indexing a string, the first character has position 1. | 1250 | When indexing a string, the first character has position 1. |
1247 | See Section \ref{exstring} for some examples on string manipulation | 1251 | See Page~\pageref{pm} for an explanation about patterns, |
1252 | and Section~\ref{exstring} for some examples on string manipulation | ||
1248 | in Lua. | 1253 | in Lua. |
1249 | 1254 | ||
1250 | \subsubsection*{{\tt strfind (str, substr, [init, [end]])}} | 1255 | \subsubsection*{\ff{\tt strfind (str, pattern [, init [, plain]])}} |
1251 | \Deffunc{strfind} | 1256 | \Deffunc{strfind} |
1252 | Receives two string arguments, | 1257 | This function looks for the first {\em match} of |
1253 | and returns a number. | 1258 | \verb-pattern- in \verb-str-. |
1254 | This number indicates the first position where the second argument appears | 1259 | If it finds one, it returns the indexes on \verb-str- |
1255 | in the first argument. | 1260 | where this occurence starts and ends; |
1256 | If the second argument is not a substring of the first one, | 1261 | otherwise, it returns \nil. |
1257 | then \verb'strfind' returns \nil. | 1262 | If the pattern specifies captures, |
1258 | A third optional numerical argument specifies where to start the search. | 1263 | the captured strings are returned as extra results. |
1259 | Another optional numerical argument specifies where to stop it. | 1264 | A third optional numerical argument specifies where to start the search; |
1260 | 1265 | its default value is 1. | |
1261 | \subsubsection*{{\tt strlen (s)}}\Deffunc{strlen} | 1266 | A value of 1 as a forth optional argument |
1267 | turns off the pattern matching facilities, | ||
1268 | so the function does a plain ``find substring'' operation. | ||
1269 | |||
1270 | \subsubsection*{\ff{\tt strlen (s)}}\Deffunc{strlen} | ||
1262 | Receives a string and returns its length. | 1271 | Receives a string and returns its length. |
1263 | 1272 | ||
1264 | \subsubsection*{{\tt strsub (s, i, [j])}}\Deffunc{strsub} | 1273 | \subsubsection*{\ff{\tt strsub (s, i [, j])}}\Deffunc{strsub} |
1265 | Returns another string, which is a substring of \verb's', | 1274 | Returns another string, which is a substring of \verb's', |
1266 | starting at \verb'i' and runing until \verb'j'. | 1275 | starting at \verb'i' and runing until \verb'j'. |
1267 | If \verb'j' is absent, | 1276 | If \verb'j' is absent, |
@@ -1271,21 +1280,25 @@ with length \verb'j', | |||
1271 | whereas the call \verb'strsub(s,i)' returns a suffix of \verb's', | 1280 | whereas the call \verb'strsub(s,i)' returns a suffix of \verb's', |
1272 | starting at \verb'i'. | 1281 | starting at \verb'i'. |
1273 | 1282 | ||
1274 | \subsubsection*{{\tt strlower (s)}}\Deffunc{strlower} | 1283 | \subsubsection*{\ff{\tt strlower (s)}}\Deffunc{strlower} |
1275 | Receives a string and returns a copy of that string with all | 1284 | Receives a string and returns a copy of that string with all |
1276 | upper case letters changed to lower case. | 1285 | upper case letters changed to lower case. |
1277 | All other characters are left unchanged. | 1286 | All other characters are left unchanged. |
1278 | 1287 | ||
1279 | \subsubsection*{{\tt strupper (s)}}\Deffunc{strupper} | 1288 | \subsubsection*{\ff{\tt strupper (s)}}\Deffunc{strupper} |
1280 | Receives a string and returns a copy of that string with all | 1289 | Receives a string and returns a copy of that string with all |
1281 | lower case letters changed to upper case. | 1290 | lower case letters changed to upper case. |
1282 | All other characters are left unchanged. | 1291 | All other characters are left unchanged. |
1283 | 1292 | ||
1284 | \subsubsection*{{\tt ascii (s, [i])}}\Deffunc{ascii} | 1293 | \subsubsection*{\ff{\tt strrep (s, n)}}\Deffunc{strrep} |
1294 | Returns a string which is the concatenation of \verb-n- copies of | ||
1295 | the string \verb-s-. | ||
1296 | |||
1297 | \subsubsection*{\ff{\tt ascii (s [, i])}}\Deffunc{ascii} | ||
1285 | Returns the ascii code of the character \verb's[i]'. | 1298 | Returns the ascii code of the character \verb's[i]'. |
1286 | If \verb'i' is absent, then it is assumed to be 1. | 1299 | If \verb'i' is absent, then it is assumed to be 1. |
1287 | 1300 | ||
1288 | \subsubsection*{{\tt format (formatstring, e1, e2, \ldots)}}\Deffunc{format} | 1301 | \subsubsection*{\ff{\tt format (formatstring, e1, e2, \ldots)}}\Deffunc{format} |
1289 | \label{format} | 1302 | \label{format} |
1290 | This function returns a formated version of its variable number of arguments | 1303 | This function returns a formated version of its variable number of arguments |
1291 | following the description given in its first argument (which must be a string). | 1304 | following the description given in its first argument (which must be a string). |
@@ -1300,12 +1313,120 @@ back by the Lua interpreter. | |||
1300 | The string is written between double quotes, | 1313 | The string is written between double quotes, |
1301 | and all double quotes, returns and backslashes in the string | 1314 | and all double quotes, returns and backslashes in the string |
1302 | are correctly escaped when written. | 1315 | are correctly escaped when written. |
1316 | For instance, the call | ||
1317 | \begin{verbatim} | ||
1318 | format('%q', 'a string with "quotes" and \n new line') | ||
1319 | \end{verbatim} | ||
1320 | will produce the string: | ||
1321 | \begin{verbatim} | ||
1322 | "a string with \"quotes\" and \ | ||
1323 | new line" | ||
1324 | \end{verbatim} | ||
1303 | 1325 | ||
1304 | The options \verb'c', \verb'd', \verb'E', \verb'e', \verb'f', | 1326 | The options \verb'c', \verb'd', \verb'E', \verb'e', \verb'f', |
1305 | \verb'g' \verb'i', \verb'o', \verb'u', \verb'X', and \verb'x' all | 1327 | \verb'g' \verb'i', \verb'o', \verb'u', \verb'X', and \verb'x' all |
1306 | expect a number argument, | 1328 | expect a number as argument, |
1307 | whereas \verb'q' and \verb's' expect a string. | 1329 | whereas \verb'q' and \verb's' expect a string. |
1308 | 1330 | ||
1331 | \subsubsection*{\ff{\tt gsub (s, from, to [, n])}}\Deffunc{gsub} | ||
1332 | Returns a copy of \verb-s-, | ||
1333 | where all ocurrences of the pattern \verb-from- have been | ||
1334 | replaced by a replacement string specified by \verb-to-. | ||
1335 | This function also returns, as a second value, | ||
1336 | the total number of substitutions made. | ||
1337 | |||
1338 | If \verb-to- is a string, its value is used for replacement. | ||
1339 | Any sequence in \verb-to- of the form \verb-%n- with \verb-n- between 1 and 9 | ||
1340 | stands for the value of the n-th captured substring. | ||
1341 | |||
1342 | If \verb-to- is a function, this function is called every time a | ||
1343 | match occurs, with all captured substrings as parameters. | ||
1344 | If the value returned by this function is a string, | ||
1345 | it is used as the replacement string; | ||
1346 | otherwise, the replacement string is the empty string. | ||
1347 | |||
1348 | An optional parameter \verb-n- limits | ||
1349 | the maximum number of substitutions to occur. | ||
1350 | For instance, when \verb-n- is 1 only the first ocurrence of | ||
1351 | \verb-from- is replaced. | ||
1352 | |||
1353 | As an example, in the following expression each ocurrence of the form | ||
1354 | \verb-$name$- calls the function \verb|getenv|, | ||
1355 | passing \verb|name| as argument | ||
1356 | (because only this part of the pattern is captured). | ||
1357 | The value returned by \verb|getenv| will replace the pattern. | ||
1358 | Therefore, the whole expression: | ||
1359 | \begin{verbatim} | ||
1360 | gsub('home = $HOME$, user = $USER$', "$(%w%w*)$", getenv) | ||
1361 | \end{verbatim} | ||
1362 | may return the string: | ||
1363 | \begin{verbatim} | ||
1364 | home = /home/roberto, user = roberto | ||
1365 | \end{verbatim} | ||
1366 | |||
1367 | \subsubsection*{Patterns} \label{pm} | ||
1368 | |||
1369 | A \Def{character class} is used to represent a set of characters. | ||
1370 | The following combinations are allowed in describing a character class: | ||
1371 | \begin{description} | ||
1372 | \item[{\em x}] (where {\em x} is any character not in the list \verb'()%.[*?') | ||
1373 | --- represents the character {\em x} itself. | ||
1374 | \item[{\tt .}] --- represents all characters. | ||
1375 | \item[{\tt \%a}] --- represents all letters. | ||
1376 | \item[{\tt \%A}] --- represents all non letter characters. | ||
1377 | \item[{\tt \%d}] --- represents all digits. | ||
1378 | \item[{\tt \%D}] --- represents all non digits. | ||
1379 | \item[{\tt \%l}] --- represents all lower case letters. | ||
1380 | \item[{\tt \%L}] --- represents all non lower case letter characters. | ||
1381 | \item[{\tt \%s}] --- represents all space characters. | ||
1382 | \item[{\tt \%S}] --- represents all non space characters. | ||
1383 | \item[{\tt \%u}] --- represents all upper case letters. | ||
1384 | \item[{\tt \%U}] --- represents all non upper case letter characters. | ||
1385 | \item[{\tt \%w}] --- represents all alphanumeric characters. | ||
1386 | \item[{\tt \%W}] --- represents all non alphanumeric characters. | ||
1387 | \item[{\tt \%\em x}] (where {\em x} is any non alphanumeric character) --- | ||
1388 | represents the character {\em x}. | ||
1389 | \item[{\tt [char-set]}] --- | ||
1390 | Represents the class which is the union of all | ||
1391 | characters in char-set. | ||
1392 | To include a \verb']' in char-set, it must be the first character. | ||
1393 | A range of characters may be specified by | ||
1394 | separating the end characters of the range with a \verb'-'; | ||
1395 | e.g., \verb'A-Z' specifies the upper case characters. | ||
1396 | If \verb'-' appears as the first or last character of char-set, | ||
1397 | then it represents itself. | ||
1398 | All classes \verb'%'{\em x} described above can also be used as | ||
1399 | components in a char-set. | ||
1400 | All other characters in char-set represent themselves. | ||
1401 | \item[{\tt [\^{ }char-set]}] --- | ||
1402 | represents the complement of char-set, | ||
1403 | where char-set is interpreted as above. | ||
1404 | \end{description} | ||
1405 | |||
1406 | A \Def{pattern item} may be a single character class, | ||
1407 | or a character class followed by \verb'*' or by \verb'?'. | ||
1408 | A single character class matches any single character in the class. | ||
1409 | A character class followed by \verb'*' matches 0 or more repetitions | ||
1410 | of characters in the class. | ||
1411 | A character class followed by \verb'?' matches 0 or one ocurrence | ||
1412 | of a character in the class. | ||
1413 | A pattern item may also has the form \verb'%n', | ||
1414 | for \verb-n- between 1 and 9. | ||
1415 | Such item matches a sub-string equal to the n-th captured string. | ||
1416 | |||
1417 | A \Def{pattern} is a sequence of pattern items. | ||
1418 | Any repetition item (\verb'*') inside a pattern will always | ||
1419 | match the longest possible sequence. | ||
1420 | A \verb'^' at the beginning of a pattern anchors the match at the | ||
1421 | beginning of the subject string. | ||
1422 | A \verb'$' at the end of a pattern anchors the match at the | ||
1423 | end of the subject string. | ||
1424 | |||
1425 | A pattern may contain sub-patterns enclosed in parentheses, | ||
1426 | that describe \Def{captures}. | ||
1427 | When a match succeeds, the sub-strings of the subject string | ||
1428 | that match captures are {\em captured} for future use. | ||
1429 | Captures are numbered according to their left delimiter. | ||
1309 | 1430 | ||
1310 | \subsection{Mathematical Functions} \label{mathlib} | 1431 | \subsection{Mathematical Functions} \label{mathlib} |
1311 | 1432 | ||
@@ -1333,8 +1454,6 @@ value of its numeric arguments. | |||
1333 | Similarly, \verb'min' computes the minimum. | 1454 | Similarly, \verb'min' computes the minimum. |
1334 | Both can be used with an unlimited number of arguments. | 1455 | Both can be used with an unlimited number of arguments. |
1335 | 1456 | ||
1336 | The function \verb'mod' is equivalent to the \verb'%' operator in C. | ||
1337 | |||
1338 | The functions \verb'random' and \verb'randomseed' are interfaces to | 1457 | The functions \verb'random' and \verb'randomseed' are interfaces to |
1339 | the simple random generator functions \verb'rand' and \verb'srand', | 1458 | the simple random generator functions \verb'rand' and \verb'srand', |
1340 | provided by ANSI C. | 1459 | provided by ANSI C. |
@@ -1350,129 +1469,127 @@ Initially, the current input file is \verb'stdin', | |||
1350 | and the current output file is \verb'stdout'. | 1469 | and the current output file is \verb'stdout'. |
1351 | 1470 | ||
1352 | Unless otherwise stated, | 1471 | Unless otherwise stated, |
1353 | all I/O functions return 1 on success and \nil\ on failure. | 1472 | all I/O functions return \nil\ on failure and |
1354 | 1473 | some value different from \nil\ on success. | |
1355 | \subsubsection*{{\tt readfrom (filename)}}\Deffunc{readfrom} | 1474 | |
1356 | 1475 | \subsubsection*{\ff{\tt readfrom (filename)}}\Deffunc{readfrom} | |
1357 | This function opens a file named \verb'filename' and sets it as the | 1476 | |
1358 | {\em current} input file. | 1477 | This function may be called in three ways. |
1478 | When called with a file name, | ||
1479 | it opens the named file, | ||
1480 | sets it as the {\em current} input file, | ||
1481 | and returns a {\em handle} to the file | ||
1482 | (this handle is a user data containing the file stream \verb|FILE *|). | ||
1483 | When called with a file handle, returned by a previous call, | ||
1484 | it restores the file as the current input. | ||
1359 | When called without parameters, | 1485 | When called without parameters, |
1360 | this function closes the current input file, | 1486 | it closes the current input file, |
1361 | and restores \verb'stdin' as the current input file. | 1487 | and restores \verb'stdin' as the current input file. |
1362 | 1488 | ||
1489 | If this function fails, it returns \nil. | ||
1490 | |||
1363 | {\em System dependent:} if \verb'filename' starts with a \verb'|', | 1491 | {\em System dependent:} if \verb'filename' starts with a \verb'|', |
1364 | then a \Index{piped input} is open, via function \IndexVerb{popen}. | 1492 | then a \Index{piped input} is open, via function \IndexVerb{popen}. |
1365 | 1493 | ||
1366 | \subsubsection*{{\tt writeto (filename)}}\Deffunc{writeto} | 1494 | \subsubsection*{\ff{\tt writeto (filename)}}\Deffunc{writeto} |
1367 | 1495 | ||
1368 | This function opens a file named \verb'filename' and sets it as the | 1496 | This function may be called in three ways. |
1369 | {\em current} output file. | 1497 | When called with a file name, |
1498 | it opens the named file, | ||
1499 | sets it as the {\em current} output file, | ||
1500 | and returns a {\em handle} to the file | ||
1501 | (this handle is a user data containing the file stream \verb|FILE *|). | ||
1370 | Notice that, if the file already exists, | 1502 | Notice that, if the file already exists, |
1371 | it will be {\em completely erased} with this operation. | 1503 | it will be {\em completely erased} with this operation. |
1504 | When called with a file handle, returned by a previous call, | ||
1505 | it restores the file as the current output. | ||
1372 | When called without parameters, | 1506 | When called without parameters, |
1373 | this function closes the current output file, | 1507 | this function closes the current output file, |
1374 | and restores \verb'stdout' as the current output file. | 1508 | and restores \verb'stdout' as the current output file. |
1375 | \index{closing a file} | 1509 | \index{closing a file} |
1376 | 1510 | ||
1511 | If this function fails, it returns \nil. | ||
1512 | |||
1377 | {\em System dependent:} if \verb'filename' starts with a \verb'|', | 1513 | {\em System dependent:} if \verb'filename' starts with a \verb'|', |
1378 | then a \Index{piped output} is open, via function \IndexVerb{popen}. | 1514 | then a \Index{piped output} is open, via function \IndexVerb{popen}. |
1379 | 1515 | ||
1380 | \subsubsection*{{\tt appendto (filename)}}\Deffunc{appendto} | 1516 | \subsubsection*{\ff{\tt appendto (filename)}}\Deffunc{appendto} |
1381 | 1517 | ||
1382 | This function opens a file named \verb'filename' and sets it as the | 1518 | This function opens a file named \verb'filename' and sets it as the |
1383 | {\em current} output file. | 1519 | {\em current} output file. |
1520 | It returns the file handle, | ||
1521 | or \nil\ in case of error. | ||
1384 | Unlike the \verb'writeto' operation, | 1522 | Unlike the \verb'writeto' operation, |
1385 | this function does not erase any previous content of the file. | 1523 | this function does not erase any previous content of the file. |
1386 | 1524 | ||
1387 | \subsubsection*{{\tt remove (filename)}}\Deffunc{remove} | 1525 | \subsubsection*{\ff{\tt remove (filename)}}\Deffunc{remove} |
1388 | 1526 | ||
1389 | This function deletes the file with the given name. | 1527 | This function deletes the file with the given name. |
1390 | 1528 | ||
1391 | \subsubsection*{{\tt rename (name1, name2)}}\Deffunc{rename} | 1529 | \subsubsection*{\ff{\tt rename (name1, name2)}}\Deffunc{rename} |
1392 | 1530 | ||
1393 | This function renames file \verb'name1' to \verb'name2'. | 1531 | This function renames file \verb'name1' to \verb'name2'. |
1394 | 1532 | ||
1395 | \subsubsection*{{\tt tmpname ()}}\Deffunc{tmpname} | 1533 | \subsubsection*{\ff{\tt tmpname ()}}\Deffunc{tmpname} |
1396 | 1534 | ||
1397 | This function returns a string with a file name that can safely | 1535 | This function returns a string with a file name that can safely |
1398 | be used for a temporary file. | 1536 | be used for a temporary file. |
1399 | 1537 | ||
1400 | \subsubsection*{{\tt read ([format])}}\Deffunc{read} | 1538 | \subsubsection*{\ff{\tt read ([readpattern])}}\Deffunc{read} |
1401 | |||
1402 | This function returns a value read from the current input. | ||
1403 | An optional string argument specifies the way the input is interpreted. | ||
1404 | |||
1405 | Without a format argument, {\tt read} first skips blanks, tabs and newlines. | ||
1406 | Then it checks whether the current character is \verb'"' or \verb-'-. | ||
1407 | If so, it reads a string up to the ending quotation mark, | ||
1408 | and returns this string, without the quotation marks. | ||
1409 | Otherwise it reads up to a blank, tab or newline. | ||
1410 | |||
1411 | The format string can have the following format: | ||
1412 | \begin{verbatim} | ||
1413 | ?[n] | ||
1414 | \end{verbatim} | ||
1415 | where \verb'?' can be: | ||
1416 | \begin{description} | ||
1417 | \item['s' or 'S'] to read a string; | ||
1418 | \item['f' or 'F'] to read a real number; | ||
1419 | \item['i' or 'I'] to read an integer. | ||
1420 | \end{description} | ||
1421 | The optional \verb'n' is a number which specifies how many characters | ||
1422 | must be read to compose the input value. | ||
1423 | Particularly, the format \verb'"s1"' reads a single character. | ||
1424 | |||
1425 | \subsubsection*{{\tt readuntil (char)}}\Deffunc{readuntil} | ||
1426 | 1539 | ||
1427 | Reads the current input until the first ocurrence of the given character. | 1540 | This function reads the current input |
1428 | When called with no parameters, | 1541 | according to a read pattern, that specifies how much to read; |
1429 | reads until the end of the current input file. | 1542 | characters are read from the current input file until |
1430 | Returns the string read. | 1543 | the read pattern fails or ends. |
1431 | The character itself is not read. | 1544 | The function \verb|read| returns a string with the characters read, |
1432 | 1545 | or \nil\ if the result string would be empty {\em and\/} | |
1433 | \subsubsection*{{\tt write (value, [format])}}\Deffunc{write} | 1546 | the read pattern fails. |
1434 | 1547 | When called without parameters, | |
1435 | This function writes the value of its first argument to the current output. | 1548 | it uses a default pattern that reads the next line |
1436 | An optional second argument specifies the format to be used. | 1549 | (see below). |
1437 | This format is given as a string, composed of four parts. | 1550 | |
1438 | The first part is the only one not optional, and must be one of the | 1551 | A \Def{read pattern} is a sequence of read pattern items. |
1439 | following characters: | 1552 | An item may be a single character class |
1440 | \begin{description} | 1553 | or a character class followed by \verb'?' or by \verb'*'. |
1441 | \item['s' or 'S'] to write strings; | 1554 | A single character class reads the next character from the input |
1442 | \item['f' or 'F'] to write floats; | 1555 | if it belongs to the class, otherwise it fails. |
1443 | \item['i' or 'I'] to write integers; | 1556 | A character class followed by \verb'?' reads the next character |
1444 | \item['q' or 'Q'] to write quoted strings. | 1557 | from the input if it belongs to the class; |
1445 | This format writes the string in a form suitable to be safely read | 1558 | it never fails. |
1446 | back by the Lua interpreter. | 1559 | A character class followed by \verb'*' reads until a character that |
1447 | The string is written between double quotes, | 1560 | does not belong to the class, or end of file; |
1448 | and all double quotes, returns and backslashes in the string | 1561 | it never fails.% |
1449 | are correctly escaped when written. | 1562 | \footnote{ |
1450 | \end{description} | 1563 | Notice that this behaviour is different from regular pattern matching, |
1451 | These characters can be followed by | 1564 | where a \verb'*' expands to the maximum length {\em such that} |
1452 | \begin{verbatim} | 1565 | the rest of the pattern does not fail.} |
1453 | [?][m][.n] | 1566 | |
1454 | \end{verbatim} | 1567 | A pattern item may contain sub-patterns enclosed in curly brackets, |
1455 | where: | 1568 | that describe \Def{skips}. |
1456 | \begin{description} | 1569 | Characters matching a skip are read, |
1457 | \item[\verb'?'] indicates justification inside the field. | 1570 | but are not included in the resulting string. |
1571 | |||
1572 | Following are some examples of read patterns and their meanings: | ||
1458 | \begin{itemize} | 1573 | \begin{itemize} |
1459 | \item['\verb'<''] right justification (default); | 1574 | \item \verb|"."| returns the next character, or \nil\ on end of file. |
1460 | \item['\verb'>''] left justification; | 1575 | \item \verb|".*"| reads the whole file. |
1461 | \item['\verb'|''] center justification. | 1576 | \item \verb|"[^\n]*{\n}"| returns the next line |
1577 | (skipping the end of line), or \nil\ on end of file. | ||
1578 | This is the default pattern. | ||
1579 | \item \verb|"{%s*}%S%S*"| returns the next word | ||
1580 | (maximal sequence of non white-space characters), | ||
1581 | or \nil\ on end of file. | ||
1582 | \item \verb|"{%s*}[+-]?%d%d*"| returns the next integer | ||
1583 | or \nil\ if the next characters do not conform to an integer format. | ||
1462 | \end{itemize} | 1584 | \end{itemize} |
1463 | \item[\verb'm'] Indicates the field size in characters. | ||
1464 | \item[\verb'.n'] For reals, indicates the number of digital places. | ||
1465 | For integers, it is the minimum number of digits. | ||
1466 | This option has no meaning for strings. | ||
1467 | \end{description} | ||
1468 | 1585 | ||
1469 | When called without a format string, | 1586 | \subsubsection*{\ff{\tt write (value1, ...)}}\Deffunc{write} |
1470 | this function writes numbers using the \verb'%g' format | 1587 | |
1471 | and strings with \verb'%s'. | 1588 | This function writes the value of each of its arguments to the |
1472 | For better format facilities, | 1589 | current output file. |
1473 | the function \verb'format' should be used (\see{format}). | 1590 | The arguments must be strings or numbers. |
1474 | 1591 | ||
1475 | \subsubsection*{{\tt date ([format])}}\Deffunc{date} | 1592 | \subsubsection*{\ff{\tt date ([format])}}\Deffunc{date} |
1476 | 1593 | ||
1477 | This function returns a string containing date and time | 1594 | This function returns a string containing date and time |
1478 | formatted according to the given string \verb'format', | 1595 | formatted according to the given string \verb'format', |
@@ -1480,15 +1597,23 @@ following the same rules of the ANSI C function \verb'strftime'. | |||
1480 | When called without arguments, | 1597 | When called without arguments, |
1481 | it returns a reasonable date and time representation. | 1598 | it returns a reasonable date and time representation. |
1482 | 1599 | ||
1483 | This function replaces functions \verb'date' and \verb'time' from | 1600 | \subsubsection*{\ff{\tt exit ([code])}}\Deffunc{exit} |
1484 | previous Lua versions. | ||
1485 | |||
1486 | \subsubsection*{{\tt exit ([code])}}\Deffunc{exit} | ||
1487 | 1601 | ||
1488 | This function calls the C function \verb-exit-, | 1602 | This function calls the C function \verb-exit-, |
1489 | with an optional \verb-code-, | 1603 | with an optional \verb-code-, |
1490 | to terminate the program. | 1604 | to terminate the program. |
1491 | 1605 | ||
1606 | \subsubsection*{\ff{\tt getenv (varname)}}\Deffunc{getenv} | ||
1607 | |||
1608 | Returns the value of the environment variable \verb|varname|, | ||
1609 | or \nil\ if the variable is not defined. | ||
1610 | |||
1611 | \subsubsection*{\ff{\tt execute (command)}}\Deffunc{execute} | ||
1612 | |||
1613 | This function is equivalent to the C function \verb|system|. | ||
1614 | It passes \verb|command| to be executed by an Operating System Shell. | ||
1615 | It returns an error code, which is implementation-defined. | ||
1616 | |||
1492 | 1617 | ||
1493 | \section{The Debugger Interface} \label{debugI} | 1618 | \section{The Debugger Interface} \label{debugI} |
1494 | 1619 | ||
@@ -1496,7 +1621,7 @@ Lua has no built-in debugger facilities. | |||
1496 | Instead, it offers a special interface, | 1621 | Instead, it offers a special interface, |
1497 | by means of functions and {\em hooks}, | 1622 | by means of functions and {\em hooks}, |
1498 | which allows the construction of different | 1623 | which allows the construction of different |
1499 | kinds of debugers, profilers, and other tools | 1624 | kinds of debuggers, profilers, and other tools |
1500 | that need ``inside information'' from the interpreter. | 1625 | that need ``inside information'' from the interpreter. |
1501 | This interface is declared in the header file \verb'luadebug.h'. | 1626 | This interface is declared in the header file \verb'luadebug.h'. |
1502 | 1627 | ||
@@ -1699,32 +1824,21 @@ end | |||
1699 | 1824 | ||
1700 | \subsection{String Manipulation} \label{exstring} | 1825 | \subsection{String Manipulation} \label{exstring} |
1701 | 1826 | ||
1702 | The first example is a function to trim extra blanks at the beginning | 1827 | The first example is a function to trim extra white-spaces at the beginning |
1703 | and end of a string. | 1828 | and end of a string. |
1704 | \begin{verbatim} | 1829 | \begin{verbatim} |
1705 | function trim(s) | 1830 | function trim(s) |
1706 | local l = 1 | 1831 | local _, i = strfind(s, '^ *') |
1707 | while strsub(s,l,l) == ' ' do | 1832 | local f, __ = strfind(s, ' *$') |
1708 | l = l+1 | 1833 | return strsub(s, i+1, f-1) |
1709 | end | ||
1710 | local r = strlen(s) | ||
1711 | while strsub(s,r,r) == ' ' do | ||
1712 | r = r-1 | ||
1713 | end | ||
1714 | return strsub(s,l,r) | ||
1715 | end | 1834 | end |
1716 | \end{verbatim} | 1835 | \end{verbatim} |
1717 | 1836 | ||
1718 | The second example shows a function that eliminates all blanks | 1837 | The second example shows a function that eliminates all spaces |
1719 | of a string. | 1838 | of a string. |
1720 | \begin{verbatim} | 1839 | \begin{verbatim} |
1721 | function remove_blanks (s) | 1840 | function remove_blanks (s) |
1722 | local b = strfind(s, ' ') | 1841 | return gsub(s, "%s%s*", "") |
1723 | while b do | ||
1724 | s = strsub(s, 1, b-1) .. strsub(s, b+1) | ||
1725 | b = strfind(s, ' ') | ||
1726 | end | ||
1727 | return s | ||
1728 | end | 1842 | end |
1729 | \end{verbatim} | 1843 | \end{verbatim} |
1730 | 1844 | ||
@@ -2058,6 +2172,46 @@ void remove_blanks (char *s) | |||
2058 | \end{verbatim} | 2172 | \end{verbatim} |
2059 | 2173 | ||
2060 | 2174 | ||
2175 | \section{\Index{Lua Stand-alone}} | ||
2176 | |||
2177 | Although Lua has been designed as an extension language, | ||
2178 | the language can also be used as a stand-alone interpreter. | ||
2179 | An implementation of such an interpreter, | ||
2180 | called simply \verb|lua|, | ||
2181 | is provided with the standard distribution. | ||
2182 | This program can be called with any sequence of the following arguments: | ||
2183 | \begin{description} | ||
2184 | \item[{\tt -v}] prints version information. | ||
2185 | \item[{\tt -}] runs interactively, accepting commands from standard input | ||
2186 | until an \verb|EOF|. | ||
2187 | \item[{\tt -e stat}] executes \verb|stat| as a Lua chunck. | ||
2188 | \item[{\tt var=exp}] executes \verb|var=exp| as a Lua chunck. | ||
2189 | \item[{\tt filename}] executes file \verb|filename| as a Lua chunck. | ||
2190 | \end{description} | ||
2191 | All arguments are handle in order. | ||
2192 | For instance, an invocation like | ||
2193 | \begin{verbatim} | ||
2194 | $ lua - a=1 prog.lua | ||
2195 | \end{verbatim} | ||
2196 | will first interact with the user until an \verb|EOF|, | ||
2197 | then will set \verb'a' to 1, | ||
2198 | and finally will run file \verb'prog.lua'. | ||
2199 | |||
2200 | Please notice that the interaction with the shell may lead to | ||
2201 | unintended results. | ||
2202 | For instance, a call like | ||
2203 | \begin{verbatim} | ||
2204 | $ lua a="name" prog.lua | ||
2205 | \end{verbatim} | ||
2206 | will {\em not} set \verb|a| to the string \verb|"name"|. | ||
2207 | Instead, the quotes will be handled by the shell, | ||
2208 | lua will get only \verb'a=name' to run, | ||
2209 | and \verb'a' will finish with \nil. | ||
2210 | Instead, one should write | ||
2211 | \begin{verbatim} | ||
2212 | $ lua 'a="name"' prog.lua | ||
2213 | \end{verbatim} | ||
2214 | |||
2061 | \section*{Acknowledgments} | 2215 | \section*{Acknowledgments} |
2062 | 2216 | ||
2063 | The authors would like to thank CENPES/PETROBR\'AS which, | 2217 | The authors would like to thank CENPES/PETROBR\'AS which, |
@@ -2076,7 +2230,27 @@ Lua means {\em moon} in Portuguese. | |||
2076 | Although great care has been taken to avoid incompatibilities with | 2230 | Although great care has been taken to avoid incompatibilities with |
2077 | the previous public versions of Lua, | 2231 | the previous public versions of Lua, |
2078 | some differences had to be introduced. | 2232 | some differences had to be introduced. |
2079 | Here is a list of all these differences. | 2233 | Here is a list of all these incompatibilities. |
2234 | |||
2235 | \subsection*{Incompatibilities with \Index{version 2.4}} | ||
2236 | The whole I/O facilities have been rewritten. | ||
2237 | We strongly encourage programmers to addapt their code | ||
2238 | to this new version. | ||
2239 | However, we are keeping the old version of the libraries | ||
2240 | in the distribution, | ||
2241 | to allow a smooth transition. | ||
2242 | The incompatibilities between the new and the old libraries are: | ||
2243 | \begin{itemize} | ||
2244 | \item The format facility of function \verb'write' has been supersed by | ||
2245 | function \verb'format'; | ||
2246 | therefore this facility has been dropped. | ||
2247 | \item Function \verb'read' now uses {\em read patterns} to specify | ||
2248 | what to read; | ||
2249 | this is incompatible with the old format options. | ||
2250 | \item Function \verb'strfind' now accepts patterns, | ||
2251 | so it may have a different behavior when the pattern includes | ||
2252 | special characteres. | ||
2253 | \end{itemize} | ||
2080 | 2254 | ||
2081 | \subsection*{Incompatibilities with \Index{version 2.2}} | 2255 | \subsection*{Incompatibilities with \Index{version 2.2}} |
2082 | \begin{itemize} | 2256 | \begin{itemize} |