diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-07-19 10:36:18 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-07-19 10:36:18 -0300 |
commit | a94cba4b88a940cba7a35244b8a1b393f33a905c (patch) | |
tree | 6828eb0e630b492a9d62022bb82819172a06afa9 | |
parent | f9f355221fc41b5db5e6bc7b3e369c4d074003d5 (diff) | |
download | lua-a94cba4b88a940cba7a35244b8a1b393f33a905c.tar.gz lua-a94cba4b88a940cba7a35244b8a1b393f33a905c.tar.bz2 lua-a94cba4b88a940cba7a35244b8a1b393f33a905c.zip |
ready for 4.1 alpha?
-rw-r--r-- | manual.tex | 790 |
1 files changed, 487 insertions, 303 deletions
@@ -1,4 +1,4 @@ | |||
1 | % $Id: manual.tex,v 1.48 2001/01/29 19:33:55 roberto Exp roberto $ | 1 | % $Id: manual.tex,v 1.49 2001/01/31 19:53:01 roberto Exp roberto $ |
2 | 2 | ||
3 | \documentclass[11pt]{article} | 3 | \documentclass[11pt]{article} |
4 | \usepackage{fullpage} | 4 | \usepackage{fullpage} |
@@ -29,7 +29,7 @@ | |||
29 | 29 | ||
30 | \newcommand{\ff}{$\bullet$\ } | 30 | \newcommand{\ff}{$\bullet$\ } |
31 | 31 | ||
32 | \newcommand{\Version}{4.0} | 32 | \newcommand{\Version}{4.1 (alpha)} |
33 | 33 | ||
34 | % LHF | 34 | % LHF |
35 | \renewcommand{\ter}[1]{{\rm`{\tt#1}'}} | 35 | \renewcommand{\ter}[1]{{\rm`{\tt#1}'}} |
@@ -71,7 +71,7 @@ Last revised on \today | |||
71 | \null\vfill | 71 | \null\vfill |
72 | 72 | ||
73 | \noindent | 73 | \noindent |
74 | Copyright \copyright\ 1994--2000 TeCGraf, PUC-Rio. All rights reserved. | 74 | Copyright \copyright\ 1994--2001 TeCGraf, PUC-Rio. All rights reserved. |
75 | 75 | ||
76 | \noindent | 76 | \noindent |
77 | Permission is hereby granted, without written agreement and without license | 77 | Permission is hereby granted, without written agreement and without license |
@@ -110,7 +110,7 @@ This implementation contains no third-party code. | |||
110 | 110 | ||
111 | \noindent | 111 | \noindent |
112 | Copies of this manual can be obtained at | 112 | Copies of this manual can be obtained at |
113 | \verb|http://www.tecgraf.puc-rio.br/lua/|. | 113 | \verb|http://www.lua.org|. |
114 | 114 | ||
115 | \bigskip | 115 | \bigskip |
116 | \noindent | 116 | \noindent |
@@ -134,7 +134,7 @@ Waldemar Celes | |||
134 | \tecgraf\ --- Computer Science Department --- PUC-Rio | 134 | \tecgraf\ --- Computer Science Department --- PUC-Rio |
135 | } | 135 | } |
136 | 136 | ||
137 | \date{{\small \tt\$Date: 2001/01/29 19:33:55 $ $}} | 137 | \date{{\small \tt\$Date: 2001/01/31 19:53:01 $ $}} |
138 | 138 | ||
139 | \maketitle | 139 | \maketitle |
140 | 140 | ||
@@ -228,8 +228,8 @@ as stated in its copyright notice. | |||
228 | The implementation described in this manual is available | 228 | The implementation described in this manual is available |
229 | at the following URL's: | 229 | at the following URL's: |
230 | \begin{verbatim} | 230 | \begin{verbatim} |
231 | http://www.tecgraf.puc-rio.br/lua/ | 231 | http://www.lua.org |
232 | ftp://ftp.tecgraf.puc-rio.br/pub/lua/ | 232 | ftp://ftp.lua.org |
233 | \end{verbatim} | 233 | \end{verbatim} |
234 | 234 | ||
235 | Like any other reference manual, | 235 | Like any other reference manual, |
@@ -305,6 +305,7 @@ are interchangeable. | |||
305 | Lua automatically detects the file type and acts accordingly. | 305 | Lua automatically detects the file type and acts accordingly. |
306 | \index{pre-compilation} | 306 | \index{pre-compilation} |
307 | 307 | ||
308 | |||
308 | \section{\Index{Types and Tags}} \label{TypesSec} | 309 | \section{\Index{Types and Tags}} \label{TypesSec} |
309 | 310 | ||
310 | Lua is a \emph{dynamically typed language}. | 311 | Lua is a \emph{dynamically typed language}. |
@@ -365,23 +366,92 @@ to tables, and do not imply any kind of copy. | |||
365 | Moreover, tables must be explicitly created before used | 366 | Moreover, tables must be explicitly created before used |
366 | \see{tableconstructor}. | 367 | \see{tableconstructor}. |
367 | 368 | ||
368 | Each of the types \M{nil}, \M{number}, and \M{string} has a different tag. | 369 | |
369 | All values of each of these types have the same pre-defined tag. | 370 | \subsection{Tags} |
370 | As explained above, | 371 | |
371 | values of type \M{function} can have two different tags, | 372 | Each type has a \emph{name}, |
372 | depending on whether they are Lua functions or C~functions. | 373 | and a numerical identifier, |
373 | Finally, | 374 | called a \Index{tag}. |
374 | values of type \M{userdata} and \M{table} can have variable tags, | 375 | Tags are mainly used by C code, |
375 | assigned by the programmer \see{tag-method}. | 376 | to avoid the manipulation of strings. |
376 | The \verb|tag| function returns the tag of a given value. | 377 | Most operations over types, in the C API, |
377 | User tags are created with the function \verb|newtag|. | 378 | require a tag to identify the type. |
378 | The \verb|settag| function | 379 | In Lua, all operations over types work |
379 | is used to change the tag of a table \see{pdf-newtag}. | 380 | both with type names or tags. |
380 | The tag of userdata values can only be set from~C \see{C-tags}. | 381 | |
381 | Tags are mainly used to select \emph{tag methods} when | 382 | |
382 | some events occur. | 383 | \subsection{User-defined Types} |
383 | Tag methods are the main mechanism for extending the | 384 | |
384 | semantics of Lua \see{tag-method}. | 385 | Lua programs can create new types, |
386 | called \Index{User-defined Types}. | ||
387 | A user-defined type is always based on a base type, | ||
388 | either a table or a userdata. | ||
389 | Objects of an extended type have an internal structure | ||
390 | identical to the corresponding base type, | ||
391 | but may have diferent semantics for each operation. | ||
392 | |||
393 | The \verb|newtype| function creates a new type \see{pdf-newtype}. | ||
394 | Types created by Lua programs are always based upon tables; | ||
395 | types created by C can be based upon tables or upon userdata. | ||
396 | The \verb|settagmethod| function defines new semantics for | ||
397 | the operations of this new type \see{tag-method}. | ||
398 | The \verb|settype| function changes the type of a given object | ||
399 | \see{pdf-settype}. | ||
400 | |||
401 | |||
402 | \section{Garbage Collection}\label{GC} | ||
403 | |||
404 | Lua does automatic memory management. | ||
405 | To do that, | ||
406 | Lua runs a \Index{garbage collector} from time to time. | ||
407 | All objects in Lua are subjected to automatic management: | ||
408 | tables, userdata, functions, and strings. | ||
409 | |||
410 | Lua uses two numbers to control its garbage-collection cycles. | ||
411 | One number counts how many bytes of dynamic memory Lua is using, | ||
412 | and the other is a threshold. | ||
413 | When the number of bytes crosses the threshold, | ||
414 | Lua runs the garbage collector, | ||
415 | which reclaims the memory of all ``dead'' objects | ||
416 | (that is, objects no longer accessible from Lua). | ||
417 | The byte counter is corrected, | ||
418 | and then the threshold is reset to twice the value of the byte counter. | ||
419 | |||
420 | Through the C API, you can consult those numbers, | ||
421 | and change the threshold \see{GC-API}. | ||
422 | Setting the threshold to zero actually forces an immediate | ||
423 | garbage-collection cycle, | ||
424 | while setting it to a huge number stops the garbage collector. | ||
425 | Using Lua code you have a more limited control of memory management, | ||
426 | through functions \verb|gcinfo| and \verb|collectgarbage|. | ||
427 | |||
428 | |||
429 | You can set garbage-collector tag methods for user-defined | ||
430 | types based on userdata \see{tag-method}. | ||
431 | Lua calls those functions when it is about to free a userdata | ||
432 | of the corresponding type. | ||
433 | Using this facility, you can coordinate Lua's garbage collection | ||
434 | with external resourse management | ||
435 | (such as closing files or freeing your own memory). | ||
436 | |||
437 | |||
438 | \subsection{Weak Tables}\label{weak-table} | ||
439 | |||
440 | A \IndexEmph{weak table} is a table whose elements are | ||
441 | \IndexEmph{weak references}. | ||
442 | A weak reference is ignored by the garbage collector, | ||
443 | so that if the only references to an object are weak references, | ||
444 | the garbage collector will collect that object. | ||
445 | |||
446 | A weak table can have weak keys, weak values, or both. | ||
447 | A table with weak keys allows the collection of its keys, | ||
448 | but avoids the collection of its values. | ||
449 | A table with both weak keys and weak values allow the collection of both. | ||
450 | In any case, if either the key or the value is collected, | ||
451 | the whole pair is removed from the table. | ||
452 | The weakness of a table is controled by the | ||
453 | function \verb|weakmode| \see{weakmode}. | ||
454 | |||
385 | 455 | ||
386 | \section{The Language} | 456 | \section{The Language} |
387 | 457 | ||
@@ -398,14 +468,16 @@ except that | |||
398 | the definition of letter depends on the current locale: | 468 | the definition of letter depends on the current locale: |
399 | Any character considered alphabetic by the current locale | 469 | Any character considered alphabetic by the current locale |
400 | can be used in an identifier. | 470 | can be used in an identifier. |
401 | The following words are \emph{reserved}, and cannot be used as identifiers: | 471 | The following words are \emph{reserved}, |
472 | and cannot be used as identifiers: | ||
402 | \index{reserved words} | 473 | \index{reserved words} |
403 | \begin{verbatim} | 474 | \begin{verbatim} |
404 | and break do else elseif | 475 | and break do else elseif |
405 | end for function if in | 476 | end for function global if |
406 | local nil not or repeat | 477 | in local nil not or |
407 | return then until while | 478 | repeat return then until while |
408 | \end{verbatim} | 479 | \end{verbatim} |
480 | (\rwd{global} is reserved for future use.) | ||
409 | 481 | ||
410 | Lua is a case-sensitive language: | 482 | Lua is a case-sensitive language: |
411 | \T{and} is a reserved word, but \T{And} and \T{\'and} | 483 | \T{and} is a reserved word, but \T{And} and \T{\'and} |
@@ -447,6 +519,8 @@ Literal strings can also be delimited by matching \verb|[[| \dots\ \verb|]]|. | |||
447 | Literals in this bracketed form may run for several lines, | 519 | Literals in this bracketed form may run for several lines, |
448 | may contain nested \verb|[[| \dots\ \verb|]]| pairs, | 520 | may contain nested \verb|[[| \dots\ \verb|]]| pairs, |
449 | and do not interpret escape sequences. | 521 | and do not interpret escape sequences. |
522 | When the \verb|[[| is immediatly followed by a newline, | ||
523 | this newline is not included in the string. | ||
450 | This form is specially convenient for | 524 | This form is specially convenient for |
451 | writing strings that contain program pieces or | 525 | writing strings that contain program pieces or |
452 | other quoted strings. | 526 | other quoted strings. |
@@ -457,6 +531,9 @@ the following three literals are equivalent: | |||
457 | 2) '\97lo\10\04923"' | 531 | 2) '\97lo\10\04923"' |
458 | 3) [[alo | 532 | 3) [[alo |
459 | 123"]] | 533 | 123"]] |
534 | 4) [[ | ||
535 | alo | ||
536 | 123"]] | ||
460 | \end{verbatim} | 537 | \end{verbatim} |
461 | 538 | ||
462 | \IndexEmph{Comments} start anywhere outside a string with a | 539 | \IndexEmph{Comments} start anywhere outside a string with a |
@@ -489,22 +566,6 @@ For complete control of how numbers are converted to strings, | |||
489 | use the \verb|format| function \see{format}. | 566 | use the \verb|format| function \see{format}. |
490 | 567 | ||
491 | 568 | ||
492 | \subsection{\Index{Adjustment}} \label{adjust} | ||
493 | |||
494 | Functions in Lua can return many values. | ||
495 | Because there are no type declarations, | ||
496 | when a function is called | ||
497 | the system does not know how many values the function will return, | ||
498 | or how many parameters it needs. | ||
499 | Therefore, sometimes, a list of values must be \emph{adjusted}, at run time, | ||
500 | to a given length. | ||
501 | If there are more values than are needed, | ||
502 | then the excess values are thrown away. | ||
503 | If there are less values than are needed, | ||
504 | then the list is extended with as many \nil's as needed. | ||
505 | This adjustment occurs in multiple assignments \see{assignment} | ||
506 | and in function calls \see{functioncall}. | ||
507 | |||
508 | 569 | ||
509 | \subsection{Statements}\label{stats} | 570 | \subsection{Statements}\label{stats} |
510 | 571 | ||
@@ -560,9 +621,15 @@ Multiple assignment can be used to exchange two values, as in | |||
560 | x, y = y, x | 621 | x, y = y, x |
561 | \end{verbatim} | 622 | \end{verbatim} |
562 | 623 | ||
563 | The two lists in a multiple assignment may have different lengths. | ||
564 | Before the assignment, the list of values is adjusted to | 624 | Before the assignment, the list of values is adjusted to |
565 | the length of the list of variables \see{adjust}. | 625 | the length of the list of variables. |
626 | If there are more values than are needed, | ||
627 | the excess values are thrown away. | ||
628 | If there are less values than are needed, | ||
629 | the list is extended with as many \nil's as needed. | ||
630 | If the list of expressions (\M{explist1}) ends with a function call, | ||
631 | all values returned by the function call enter in the list of values, | ||
632 | before the adjust. | ||
566 | 633 | ||
567 | A single name can denote a global variable, a local variable, | 634 | A single name can denote a global variable, a local variable, |
568 | or a formal parameter: | 635 | or a formal parameter: |
@@ -572,17 +639,16 @@ or a formal parameter: | |||
572 | 639 | ||
573 | Square brackets are used to index a table: | 640 | Square brackets are used to index a table: |
574 | \begin{Produc} | 641 | \begin{Produc} |
575 | \produc{var}{varorfunc \ter{[} exp1 \ter{]}} | 642 | \produc{var}{exp \ter{[} exp \ter{]}} |
576 | \produc{varorfunc}{var \Or functioncall} | ||
577 | \end{Produc}% | 643 | \end{Produc}% |
578 | The \M{varorfunc} should result in a table value, | 644 | The first expression (\M{exp}) should result in a table value, |
579 | from where the field indexed by the expression \M{exp1} | 645 | from where the field indexed by the expression \M{exp} |
580 | value gets the assigned value. | 646 | value gets the assigned value. |
581 | 647 | ||
582 | The syntax \verb|var.NAME| is just syntactic sugar for | 648 | The syntax \verb|var.NAME| is just syntactic sugar for |
583 | \verb|var["NAME"]|: | 649 | \verb|var["NAME"]|: |
584 | \begin{Produc} | 650 | \begin{Produc} |
585 | \produc{var}{varorfunc \ter{.} name} | 651 | \produc{var}{exp \ter{.} name} |
586 | \end{Produc}% | 652 | \end{Produc}% |
587 | 653 | ||
588 | The meaning of assignments and evaluations of global variables and | 654 | The meaning of assignments and evaluations of global variables and |
@@ -605,13 +671,14 @@ familiar syntax | |||
605 | \index{repeat-until statement} | 671 | \index{repeat-until statement} |
606 | \index{if-then-else statement} | 672 | \index{if-then-else statement} |
607 | \begin{Produc} | 673 | \begin{Produc} |
608 | \produc{stat}{\rwd{while} exp1 \rwd{do} block \rwd{end}} | 674 | \produc{stat}{\rwd{while} exp \rwd{do} block \rwd{end}} |
609 | \produc{stat}{\rwd{repeat} block \rwd{until} exp1} | 675 | \produc{stat}{\rwd{repeat} block \rwd{until} exp} |
610 | \produc{stat}{\rwd{if} exp1 \rwd{then} block | 676 | \produc{stat}{\rwd{if} exp \rwd{then} block |
611 | \rep{\rwd{elseif} exp1 \rwd{then} block} | 677 | \rep{\rwd{elseif} exp \rwd{then} block} |
612 | \opt{\rwd{else} block} \rwd{end}} | 678 | \opt{\rwd{else} block} \rwd{end}} |
613 | \end{Produc}% | 679 | \end{Produc}% |
614 | The \Index{condition expression} \M{exp1} of a control structure may return any value. | 680 | The \Index{condition expression} \M{exp} of a |
681 | control structure may return any value. | ||
615 | All values different from \nil\ are considered true; | 682 | All values different from \nil\ are considered true; |
616 | only \nil\ is considered false. | 683 | only \nil\ is considered false. |
617 | 684 | ||
@@ -650,7 +717,7 @@ one for numbers and one for tables. | |||
650 | \newpage | 717 | \newpage |
651 | The numerical \rwd{for} loop has the following syntax: | 718 | The numerical \rwd{for} loop has the following syntax: |
652 | \begin{Produc} | 719 | \begin{Produc} |
653 | \produc{stat}{\rwd{for} name \ter{=} exp1 \ter{,} exp1 \opt{\ter{,} exp1} | 720 | \produc{stat}{\rwd{for} name \ter{=} exp \ter{,} exp \opt{\ter{,} exp} |
654 | \rwd{do} block \rwd{end}} | 721 | \rwd{do} block \rwd{end}} |
655 | \end{Produc}% | 722 | \end{Produc}% |
656 | A \rwd{for} statement like | 723 | A \rwd{for} statement like |
@@ -688,7 +755,7 @@ The table \rwd{for} statement traverses all pairs | |||
688 | (index,value) of a given table. | 755 | (index,value) of a given table. |
689 | It has the following syntax: | 756 | It has the following syntax: |
690 | \begin{Produc} | 757 | \begin{Produc} |
691 | \produc{stat}{\rwd{for} name \ter{,} name \rwd{in} exp1 | 758 | \produc{stat}{\rwd{for} name \ter{,} name \rwd{in} exp |
692 | \rwd{do} block \rwd{end}} | 759 | \rwd{do} block \rwd{end}} |
693 | \end{Produc}% | 760 | \end{Produc}% |
694 | A \rwd{for} statement like | 761 | A \rwd{for} statement like |
@@ -699,10 +766,10 @@ is equivalent to the code: | |||
699 | \begin{verbatim} | 766 | \begin{verbatim} |
700 | do | 767 | do |
701 | local _t = exp | 768 | local _t = exp |
702 | local index, value = next(t, nil) | 769 | local index, value = next(_t, nil) |
703 | while index do | 770 | while index do |
704 | block | 771 | block |
705 | index, value = next(t, index) | 772 | index, value = next(_t, index) |
706 | end | 773 | end |
707 | end | 774 | end |
708 | \end{verbatim} | 775 | \end{verbatim} |
@@ -774,6 +841,9 @@ The basic expressions in Lua are | |||
774 | \produc{exp}{tableconstructor} | 841 | \produc{exp}{tableconstructor} |
775 | \end{Produc}% | 842 | \end{Produc}% |
776 | 843 | ||
844 | An expression enclosed in parentheses always results | ||
845 | in only one value. | ||
846 | |||
777 | Numbers (numerical constants) and | 847 | Numbers (numerical constants) and |
778 | literal strings are explained in \See{lexical}; | 848 | literal strings are explained in \See{lexical}; |
779 | variables are explained in \See{assignment}; | 849 | variables are explained in \See{assignment}; |
@@ -790,11 +860,6 @@ See \See{tag-method} for a description of these functions | |||
790 | (\verb|getglobal| is in the basic library; | 860 | (\verb|getglobal| is in the basic library; |
791 | \T{gettable\_event} is used for explanatory purposes only). | 861 | \T{gettable\_event} is used for explanatory purposes only). |
792 | 862 | ||
793 | The non-terminal \M{exp1} is used to indicate that the values | ||
794 | returned by an expression must be adjusted to one single value: | ||
795 | \begin{Produc} | ||
796 | \produc{exp1}{exp} | ||
797 | \end{Produc}% | ||
798 | 863 | ||
799 | \subsubsection{Arithmetic Operators} | 864 | \subsubsection{Arithmetic Operators} |
800 | Lua supports the usual \Index{arithmetic operators}: | 865 | Lua supports the usual \Index{arithmetic operators}: |
@@ -823,7 +888,10 @@ If they are different, then the result is \nil. | |||
823 | Otherwise, their values are compared. | 888 | Otherwise, their values are compared. |
824 | Numbers and strings are compared in the usual way. | 889 | Numbers and strings are compared in the usual way. |
825 | Tables, userdata, and functions are compared by reference, | 890 | Tables, userdata, and functions are compared by reference, |
826 | that is, two tables are considered equal only if they are the \emph{same} table. | 891 | that is, |
892 | two tables are considered equal only if they are the \emph{same} table. | ||
893 | Every time you create a new table (or userdata, or function) this | ||
894 | new value is different from any previously existing value. | ||
827 | The operator \verb|~=| is exactly the negation of equality (\verb|==|). | 895 | The operator \verb|~=| is exactly the negation of equality (\verb|==|). |
828 | 896 | ||
829 | \NOTE | 897 | \NOTE |
@@ -904,10 +972,10 @@ except for \verb|^| (exponentiation), | |||
904 | which is right associative. | 972 | which is right associative. |
905 | \NOTE | 973 | \NOTE |
906 | The pre-compiler may rearrange the order of evaluation of | 974 | The pre-compiler may rearrange the order of evaluation of |
907 | associative operators (such as~\verb|..| or~\verb|+|), | 975 | associative or commutative operators, |
908 | as long as these optimizations do not change normal results. | 976 | as long as these optimizations do not change normal results. |
909 | However, these optimizations may change some results | 977 | However, these optimizations may change some results |
910 | if you define non-associative | 978 | if you define non-associative (or non-commutative) |
911 | tag methods for these operators. | 979 | tag methods for these operators. |
912 | 980 | ||
913 | \subsubsection{Table Constructors} \label{tableconstructor} | 981 | \subsubsection{Table Constructors} \label{tableconstructor} |
@@ -920,14 +988,11 @@ The general syntax for constructors is | |||
920 | \produc{tableconstructor}{\ter{\{} fieldlist \ter{\}}} | 988 | \produc{tableconstructor}{\ter{\{} fieldlist \ter{\}}} |
921 | \produc{fieldlist}{lfieldlist \Or ffieldlist \Or lfieldlist \ter{;} ffieldlist | 989 | \produc{fieldlist}{lfieldlist \Or ffieldlist \Or lfieldlist \ter{;} ffieldlist |
922 | \Or ffieldlist \ter{;} lfieldlist} | 990 | \Or ffieldlist \ter{;} lfieldlist} |
923 | \produc{lfieldlist}{\opt{lfieldlist1}} | 991 | \produc{lfieldlist}{\opt{explist1 \opt{\ter{,}}}} |
924 | \produc{ffieldlist}{\opt{ffieldlist1}} | 992 | \produc{ffieldlist}{\opt{ffieldlist1}} |
925 | \end{Produc}% | 993 | \end{Produc}% |
926 | 994 | ||
927 | The form \emph{lfieldlist1} is used to initialize lists: | 995 | The form \emph{explist1} is used to initialize lists. |
928 | \begin{Produc} | ||
929 | \produc{lfieldlist1}{exp \rep{\ter{,} exp} \opt{\ter{,}}} | ||
930 | \end{Produc}% | ||
931 | The expressions in the list are assigned to consecutive numerical indices, | 996 | The expressions in the list are assigned to consecutive numerical indices, |
932 | starting with~1. | 997 | starting with~1. |
933 | For example, | 998 | For example, |
@@ -944,6 +1009,8 @@ is equivalent to | |||
944 | a = temp | 1009 | a = temp |
945 | end | 1010 | end |
946 | \end{verbatim} | 1011 | \end{verbatim} |
1012 | If the last expression in the list is a function call, | ||
1013 | all values returned by the call enter the list \see{functioncall}. | ||
947 | 1014 | ||
948 | The form \emph{ffieldlist1} initializes other fields in a table: | 1015 | The form \emph{ffieldlist1} initializes other fields in a table: |
949 | \begin{Produc} | 1016 | \begin{Produc} |
@@ -982,20 +1049,20 @@ For example, all forms below are correct. | |||
982 | \subsubsection{Function Calls} \label{functioncall} | 1049 | \subsubsection{Function Calls} \label{functioncall} |
983 | A \Index{function call} in Lua has the following syntax: | 1050 | A \Index{function call} in Lua has the following syntax: |
984 | \begin{Produc} | 1051 | \begin{Produc} |
985 | \produc{functioncall}{varorfunc args} | 1052 | \produc{functioncall}{exp args} |
986 | \end{Produc}% | 1053 | \end{Produc}% |
987 | First, \M{varorfunc} is evaluated. | 1054 | First, \M{exp} and \M{args} are evaluated. |
988 | If its value has type \emph{function}, | 1055 | If the value of \M{exp} has type \emph{function}, |
989 | then this function is called, | 1056 | then this function is called, |
990 | with the given arguments. | 1057 | with the given arguments. |
991 | Otherwise, the ``function'' tag method is called, | 1058 | Otherwise, the ``function'' tag method is called, |
992 | having as first parameter the value of \M{varorfunc}, | 1059 | having as first parameter the value of \M{exp}, |
993 | and then the original call arguments | 1060 | followed by the original call arguments |
994 | \see{tag-method}. | 1061 | \see{tag-method}. |
995 | 1062 | ||
996 | The form | 1063 | The form |
997 | \begin{Produc} | 1064 | \begin{Produc} |
998 | \produc{functioncall}{varorfunc \ter{:} name args} | 1065 | \produc{functioncall}{exp \ter{:} name args} |
999 | \end{Produc}% | 1066 | \end{Produc}% |
1000 | can be used to call ``methods''. | 1067 | can be used to call ``methods''. |
1001 | A call \verb|v:name(...)| | 1068 | A call \verb|v:name(...)| |
@@ -1005,9 +1072,9 @@ except that \verb|v| is evaluated only once. | |||
1005 | Arguments have the following syntax: | 1072 | Arguments have the following syntax: |
1006 | \begin{Produc} | 1073 | \begin{Produc} |
1007 | \produc{args}{\ter{(} \opt{explist1} \ter{)}} | 1074 | \produc{args}{\ter{(} \opt{explist1} \ter{)}} |
1075 | \produc{explist1}{\rep{exp \ter{,}} exp} | ||
1008 | \produc{args}{tableconstructor} | 1076 | \produc{args}{tableconstructor} |
1009 | \produc{args}{literal} | 1077 | \produc{args}{literal} |
1010 | \produc{explist1}{\rep{exp1 \ter{,}} exp} | ||
1011 | \end{Produc}% | 1078 | \end{Produc}% |
1012 | All argument expressions are evaluated before the call. | 1079 | All argument expressions are evaluated before the call. |
1013 | A call of the form \verb|f{...}| is syntactic sugar for | 1080 | A call of the form \verb|f{...}| is syntactic sugar for |
@@ -1020,20 +1087,16 @@ the argument list is a single literal string. | |||
1020 | 1087 | ||
1021 | Because a function can return any number of results | 1088 | Because a function can return any number of results |
1022 | \see{return}, | 1089 | \see{return}, |
1023 | the number of results must be adjusted before they are used \see{adjust}. | 1090 | the number of results must be adjusted before they are used. |
1024 | If the function is called as a statement \see{funcstat}, | 1091 | If the function is called as a statement \see{funcstat}, |
1025 | then its return list is adjusted to~0, | 1092 | then its return list is adjusted to~0, |
1026 | thus discarding all returned values. | 1093 | thus discarding all returned values. |
1027 | If the function is called in a place that needs a single value | 1094 | If the function is called inside another expression, |
1028 | (syntactically denoted by the non-terminal \M{exp1}), | 1095 | or in the middle of a list of expressions, |
1029 | then its return list is adjusted to~1, | 1096 | then its return list is adjusted to~1, |
1030 | thus discarding all returned values but the first one. | 1097 | thus discarding all returned values but the first one. |
1031 | If the function is called in a place that can hold many values | 1098 | If the function is called as the last element of a list of expressions, |
1032 | (syntactically denoted by the non-terminal \M{exp}), | ||
1033 | then no adjustment is made. | 1099 | then no adjustment is made. |
1034 | The only places that can hold many values | ||
1035 | is the last (or the only) expression in an assignment, | ||
1036 | in an argument list, or in the \rwd{return} statement. | ||
1037 | Here are some examples: | 1100 | Here are some examples: |
1038 | \begin{verbatim} | 1101 | \begin{verbatim} |
1039 | f() -- adjusted to 0 results | 1102 | f() -- adjusted to 0 results |
@@ -1043,7 +1106,16 @@ Here are some examples: | |||
1043 | a,b,c = x, f() -- f() is adjusted to 2 | 1106 | a,b,c = x, f() -- f() is adjusted to 2 |
1044 | a,b,c = f() -- f() is adjusted to 3 | 1107 | a,b,c = f() -- f() is adjusted to 3 |
1045 | return f() -- returns all values returned by f() | 1108 | return f() -- returns all values returned by f() |
1046 | return x,y,f() -- returns a, b, and all values returned by f() | 1109 | return x,y,f() -- returns x, y, and all values returned by f() |
1110 | {f()} -- creates a list with all values returned by f() | ||
1111 | {f(), nil} -- f() is adjusted to 1 result | ||
1112 | \end{verbatim} | ||
1113 | |||
1114 | If you embrace a function call in parentheses, | ||
1115 | then it is adjusted to return exactly one value: | ||
1116 | \begin{verbatim} | ||
1117 | return x, y, (f()) -- returns x, y, and one value from f() | ||
1118 | {(f())} -- create a table with exactly one element | ||
1047 | \end{verbatim} | 1119 | \end{verbatim} |
1048 | 1120 | ||
1049 | \subsubsection{\Index{Function Definitions}} \label{func-def} | 1121 | \subsubsection{\Index{Function Definitions}} \label{func-def} |
@@ -1054,7 +1126,7 @@ The syntax for function definition is | |||
1054 | block \rwd{end}} | 1126 | block \rwd{end}} |
1055 | \produc{stat}{\rwd{function} funcname \ter{(} \opt{parlist1} \ter{)} | 1127 | \produc{stat}{\rwd{function} funcname \ter{(} \opt{parlist1} \ter{)} |
1056 | block \rwd{end}} | 1128 | block \rwd{end}} |
1057 | \produc{funcname}{name \Or name \ter{.} name \Or name \ter{:} name} | 1129 | \produc{funcname}{name \rep{\ter{.} name} \opt{\ter{:} name}} |
1058 | \end{Produc}% | 1130 | \end{Produc}% |
1059 | The statement | 1131 | The statement |
1060 | \begin{verbatim} | 1132 | \begin{verbatim} |
@@ -1066,11 +1138,11 @@ is just syntactic sugar for | |||
1066 | \end{verbatim} | 1138 | \end{verbatim} |
1067 | and the statement | 1139 | and the statement |
1068 | \begin{verbatim} | 1140 | \begin{verbatim} |
1069 | function v.f () ... end | 1141 | function v.c.f () ... end |
1070 | \end{verbatim} | 1142 | \end{verbatim} |
1071 | is syntactic sugar for | 1143 | is syntactic sugar for |
1072 | \begin{verbatim} | 1144 | \begin{verbatim} |
1073 | v.f = function () ... end | 1145 | v.c.f = function () ... end |
1074 | \end{verbatim} | 1146 | \end{verbatim} |
1075 | 1147 | ||
1076 | A function definition is an executable expression, | 1148 | A function definition is an executable expression, |
@@ -1094,7 +1166,7 @@ initialized with the argument values: | |||
1094 | \label{vararg}% | 1166 | \label{vararg}% |
1095 | When a function is called, | 1167 | When a function is called, |
1096 | the list of \Index{arguments} is adjusted to | 1168 | the list of \Index{arguments} is adjusted to |
1097 | the length of the list of parameters \see{adjust}, | 1169 | the length of the list of parameters, |
1098 | unless the function is a \Def{vararg function}, | 1170 | unless the function is a \Def{vararg function}, |
1099 | which is | 1171 | which is |
1100 | indicated by three dots (`\verb|...|') at the end of its parameter list. | 1172 | indicated by three dots (`\verb|...|') at the end of its parameter list. |
@@ -1132,20 +1204,17 @@ If control reaches the end of a function | |||
1132 | without encountering a \rwd{return} statement, | 1204 | without encountering a \rwd{return} statement, |
1133 | then the function returns with no results. | 1205 | then the function returns with no results. |
1134 | 1206 | ||
1135 | The syntax | 1207 | The \emph{colon} syntax |
1136 | \begin{Produc} | ||
1137 | \produc{funcname}{name \ter{:} name} | ||
1138 | \end{Produc}% | ||
1139 | is used for defining \IndexEmph{methods}, | 1208 | is used for defining \IndexEmph{methods}, |
1140 | that is, functions that have an implicit extra parameter \IndexVerb{self}. | 1209 | that is, functions that have an implicit extra parameter \IndexVerb{self}. |
1141 | 1210 | ||
1142 | The statement | 1211 | The statement |
1143 | \begin{verbatim} | 1212 | \begin{verbatim} |
1144 | function v:f (...) ... end | 1213 | function v.c:f (...) ... end |
1145 | \end{verbatim} | 1214 | \end{verbatim} |
1146 | is just syntactic sugar for | 1215 | is just syntactic sugar for |
1147 | \begin{verbatim} | 1216 | \begin{verbatim} |
1148 | v.f = function (self, ...) ... end | 1217 | v.c.f = function (self, ...) ... end |
1149 | \end{verbatim} | 1218 | \end{verbatim} |
1150 | Note that the function gets an extra formal parameter called \verb|self|. | 1219 | Note that the function gets an extra formal parameter called \verb|self|. |
1151 | 1220 | ||
@@ -1192,7 +1261,7 @@ Here are some examples: | |||
1192 | local y -- a and y are local to g | 1261 | local y -- a and y are local to g |
1193 | p = a -- OK, access local `a' | 1262 | p = a -- OK, access local `a' |
1194 | p = c -- OK, access global `c' | 1263 | p = c -- OK, access global `c' |
1195 | p = b -- ERROR: cannot access a variable in outer scope | 1264 | p = b -- ERROR: cannot access a variable in outer function |
1196 | p = %b -- OK, access frozen value of `b' (local to `f') | 1265 | p = %b -- OK, access frozen value of `b' (local to `f') |
1197 | %b = 3 -- ERROR: cannot change an upvalue | 1266 | %b = 3 -- ERROR: cannot change an upvalue |
1198 | %b.x = 3 -- OK, change the table contents | 1267 | %b.x = 3 -- OK, change the table contents |
@@ -1245,34 +1314,33 @@ Lua code can ``catch'' an error using the function | |||
1245 | 1314 | ||
1246 | \subsection{Tag Methods} \label{tag-method}\index{tag method} | 1315 | \subsection{Tag Methods} \label{tag-method}\index{tag method} |
1247 | 1316 | ||
1248 | Lua provides a powerful mechanism to extend its semantics, | ||
1249 | called \emph{tag methods}. | ||
1250 | A tag method is a programmer-defined function | 1317 | A tag method is a programmer-defined function |
1251 | that is called at specific key points during the execution of a Lua program, | 1318 | that defines how Lua operations act over user-defined types |
1252 | allowing the programmer to change the standard Lua behavior at these points. | 1319 | (and, sometimes, over basic types as well). |
1253 | Each of these points is called an \Def{event}. | 1320 | An \Def{event} is any operation that may invoke a tag method. |
1254 | 1321 | ||
1255 | The tag method called for any specific event is selected | 1322 | Lua selects the tag method called for any specific event |
1256 | according to the tag of the values involved | 1323 | according to the types of the values involved |
1257 | in the event \see{TypesSec}. | 1324 | in the event \see{TypesSec}. |
1258 | The function \IndexLIB{settagmethod} changes the tag method | 1325 | The function \IndexLIB{settagmethod} changes the tag method |
1259 | associated with a given pair \M{(tag, event)}. | 1326 | associated with a given pair \M{(type, event)}. |
1260 | Its first parameter is the tag, the second parameter is the event name | 1327 | Its first parameter is the type (its name or its tag), |
1261 | (a string; see below), | 1328 | the second parameter is the event name (a string; see below), |
1262 | and the third parameter is the new method (a function), | 1329 | and the third parameter is the new method (a function), |
1263 | or \nil\ to restore the default behavior for the pair. | 1330 | or \nil\ to restore the default behavior for the pair. |
1264 | The \verb|settagmethod| function returns the previous tag method for that pair. | ||
1265 | A companion function \IndexLIB{gettagmethod} | 1331 | A companion function \IndexLIB{gettagmethod} |
1266 | receives a tag and an event name and returns the | 1332 | receives a type and an event name and returns the |
1267 | current method associated with the pair. | 1333 | current method associated with the pair. |
1268 | 1334 | ||
1269 | Tag methods are called in the following events, | 1335 | Tag methods are called in the following events, |
1270 | identified by the given names. | 1336 | identified by the given names. |
1271 | The semantics of tag methods is better explained by a Lua function | 1337 | The semantics of tag methods is better explained by a Lua function |
1272 | describing the behavior of the interpreter at each event. | 1338 | describing the behavior of the interpreter at each event. |
1273 | This function not only shows when a tag method is called, | 1339 | Each event-handler function shows how a tag method is called, |
1274 | but also its arguments, its results, and the default behavior. | 1340 | its arguments (that is, its signature), |
1275 | The code shown here is only \emph{illustrative}; | 1341 | its results, |
1342 | and the default behavior in the absence of a tag method. | ||
1343 | The code shown here in Lua is only illustrative; | ||
1276 | the real behavior is hard coded in the interpreter, | 1344 | the real behavior is hard coded in the interpreter, |
1277 | and it is much more efficient than this simulation. | 1345 | and it is much more efficient than this simulation. |
1278 | All functions used in these descriptions | 1346 | All functions used in these descriptions |
@@ -1287,7 +1355,7 @@ called when a \verb|+| operation is applied to non-numerical operands. | |||
1287 | The function \verb|getbinmethod| below defines how Lua chooses a tag method | 1355 | The function \verb|getbinmethod| below defines how Lua chooses a tag method |
1288 | for a binary operation. | 1356 | for a binary operation. |
1289 | First, Lua tries the first operand. | 1357 | First, Lua tries the first operand. |
1290 | If its tag does not define a tag method for the operation, | 1358 | If its type does not define a tag method for the operation, |
1291 | then Lua tries the second operand. | 1359 | then Lua tries the second operand. |
1292 | If it also fails, then it gets a tag method from tag~0. | 1360 | If it also fails, then it gets a tag method from tag~0. |
1293 | \begin{verbatim} | 1361 | \begin{verbatim} |
@@ -1307,9 +1375,8 @@ the tag method for the ``add'' event is | |||
1307 | else -- at least one of the operands is not numeric | 1375 | else -- at least one of the operands is not numeric |
1308 | local tm = getbinmethod(op1, op2, "add") | 1376 | local tm = getbinmethod(op1, op2, "add") |
1309 | if tm then | 1377 | if tm then |
1310 | -- call the method with both operands and an extra | 1378 | -- call the method with both operands |
1311 | -- argument with the event name | 1379 | return tm(op1, op2) |
1312 | return tm(op1, op2, "add") | ||
1313 | else -- no tag method available: default behavior | 1380 | else -- no tag method available: default behavior |
1314 | error("unexpected type at arithmetic operation") | 1381 | error("unexpected type at arithmetic operation") |
1315 | end | 1382 | end |
@@ -1336,9 +1403,8 @@ even for numerical operands. | |||
1336 | function pow_event (op1, op2) | 1403 | function pow_event (op1, op2) |
1337 | local tm = getbinmethod(op1, op2, "pow") | 1404 | local tm = getbinmethod(op1, op2, "pow") |
1338 | if tm then | 1405 | if tm then |
1339 | -- call the method with both operands and an extra | 1406 | -- call the method with both operands |
1340 | -- argument with the event name | 1407 | return tm(op1, op2) |
1341 | return tm(op1, op2, "pow") | ||
1342 | else -- no tag method available: default behavior | 1408 | else -- no tag method available: default behavior |
1343 | error("unexpected type at arithmetic operation") | 1409 | error("unexpected type at arithmetic operation") |
1344 | end | 1410 | end |
@@ -1358,9 +1424,8 @@ called when a unary \verb|-| operation is applied to a non-numerical operand. | |||
1358 | local tm = gettagmethod(tag(op), "unm") or | 1424 | local tm = gettagmethod(tag(op), "unm") or |
1359 | gettagmethod(0, "unm") | 1425 | gettagmethod(0, "unm") |
1360 | if tm then | 1426 | if tm then |
1361 | -- call the method with the operand, nil, and an extra | 1427 | -- call the method with the operand and nil |
1362 | -- argument with the event name | 1428 | return tm(op, nil) |
1363 | return tm(op, nil, "unm") | ||
1364 | else -- no tag method available: default behavior | 1429 | else -- no tag method available: default behavior |
1365 | error("unexpected type at arithmetic operation") | 1430 | error("unexpected type at arithmetic operation") |
1366 | end | 1431 | end |
@@ -1381,15 +1446,15 @@ It corresponds to the \verb|<| operator. | |||
1381 | else | 1446 | else |
1382 | local tm = getbinmethod(op1, op2, "lt") | 1447 | local tm = getbinmethod(op1, op2, "lt") |
1383 | if tm then | 1448 | if tm then |
1384 | return tm(op1, op2, "lt") | 1449 | return tm(op1, op2) |
1385 | else | 1450 | else |
1386 | error("unexpected type at comparison"); | 1451 | error("unexpected type at comparison"); |
1387 | end | 1452 | end |
1388 | end | 1453 | end |
1389 | end | 1454 | end |
1390 | \end{verbatim} | 1455 | \end{verbatim} |
1391 | The other order operators use this tag method according to the | 1456 | The other order operators use the \verb|"lt"| tag method |
1392 | usual equivalences: | 1457 | according to the usual equivalences: |
1393 | \begin{verbatim} | 1458 | \begin{verbatim} |
1394 | a>b <=> b<a | 1459 | a>b <=> b<a |
1395 | a<=b <=> not (b<a) | 1460 | a<=b <=> not (b<a) |
@@ -1406,7 +1471,7 @@ called when a concatenation is applied to non-string operands. | |||
1406 | else | 1471 | else |
1407 | local tm = getbinmethod(op1, op2, "concat") | 1472 | local tm = getbinmethod(op1, op2, "concat") |
1408 | if tm then | 1473 | if tm then |
1409 | return tm(op1, op2, "concat") | 1474 | return tm(op1, op2) |
1410 | else | 1475 | else |
1411 | error("unexpected type for concatenation") | 1476 | error("unexpected type for concatenation") |
1412 | end | 1477 | end |
@@ -1421,8 +1486,7 @@ See the ``gettable'' event for its semantics. | |||
1421 | 1486 | ||
1422 | \item[``getglobal'':]\IndexTM{getglobal} | 1487 | \item[``getglobal'':]\IndexTM{getglobal} |
1423 | called whenever Lua needs the value of a global variable. | 1488 | called whenever Lua needs the value of a global variable. |
1424 | This method can only be set for \nil\ and for tags | 1489 | This method can only be set for \nil\ and for user-defined types. |
1425 | created by \verb|newtag|. | ||
1426 | Note that | 1490 | Note that |
1427 | the tag is that of the \emph{current value} of the global variable. | 1491 | the tag is that of the \emph{current value} of the global variable. |
1428 | \begin{verbatim} | 1492 | \begin{verbatim} |
@@ -1438,6 +1502,12 @@ the tag is that of the \emph{current value} of the global variable. | |||
1438 | end | 1502 | end |
1439 | \end{verbatim} | 1503 | \end{verbatim} |
1440 | The function \verb|getglobal| is defined in the basic library~\see{predefined}. | 1504 | The function \verb|getglobal| is defined in the basic library~\see{predefined}. |
1505 | \NOTE | ||
1506 | \verb|getglobal| is ``overloaded'' here. | ||
1507 | It is the name both of the event and | ||
1508 | of the function that handles the event | ||
1509 | to call an eventual tag method | ||
1510 | (called \verb|tm| in the above code). | ||
1441 | 1511 | ||
1442 | \item[``setglobal'':]\IndexTM{setglobal} | 1512 | \item[``setglobal'':]\IndexTM{setglobal} |
1443 | called whenever Lua assigns to a global variable. | 1513 | called whenever Lua assigns to a global variable. |
@@ -1455,6 +1525,8 @@ userdata with the default tag. | |||
1455 | end | 1525 | end |
1456 | \end{verbatim} | 1526 | \end{verbatim} |
1457 | The function \verb|setglobal| is defined in the basic library~\see{predefined}. | 1527 | The function \verb|setglobal| is defined in the basic library~\see{predefined}. |
1528 | \NOTE | ||
1529 | See previous note. | ||
1458 | 1530 | ||
1459 | \item[``gettable'':]\IndexTM{gettable} | 1531 | \item[``gettable'':]\IndexTM{gettable} |
1460 | called whenever Lua accesses an indexed variable. | 1532 | called whenever Lua accesses an indexed variable. |
@@ -1531,9 +1603,10 @@ Lua does the equivalent of the following function: | |||
1531 | end | 1603 | end |
1532 | \end{verbatim} | 1604 | \end{verbatim} |
1533 | In a garbage-collection cycle, | 1605 | In a garbage-collection cycle, |
1534 | the tag methods for userdata are called in \emph{reverse} order of tag creation, | 1606 | the tag methods for userdata are called in \emph{reverse} |
1607 | order of type creation, | ||
1535 | that is, the first tag methods to be called are those associated | 1608 | that is, the first tag methods to be called are those associated |
1536 | with the last tag created in the program. | 1609 | with the last type created in the program. |
1537 | Moreover, at the end of the cycle, | 1610 | Moreover, at the end of the cycle, |
1538 | Lua does the equivalent of the call \verb|gc_event(nil)|. | 1611 | Lua does the equivalent of the call \verb|gc_event(nil)|. |
1539 | 1612 | ||
@@ -1553,7 +1626,8 @@ are declared in the header file \verb|lua.h|. | |||
1553 | \NOTE | 1626 | \NOTE |
1554 | Even when we use the term ``function'', | 1627 | Even when we use the term ``function'', |
1555 | any facility in the API may be provided as a \emph{macro} instead. | 1628 | any facility in the API may be provided as a \emph{macro} instead. |
1556 | All such macros use each of its arguments exactly once, | 1629 | All such macros use each of its arguments exactly once |
1630 | (except for the first argument, which is always a state), | ||
1557 | and so do not generate hidden side-effects. | 1631 | and so do not generate hidden side-effects. |
1558 | 1632 | ||
1559 | 1633 | ||
@@ -1604,6 +1678,31 @@ With the exception of \verb|lua_open|, | |||
1604 | all functions in the Lua API need a state as their first argument. | 1678 | all functions in the Lua API need a state as their first argument. |
1605 | 1679 | ||
1606 | 1680 | ||
1681 | \subsection{Threads} | ||
1682 | |||
1683 | Lua offers a partial support for multiple threads. | ||
1684 | If you have a C library that offers multi-threading or co-routines, | ||
1685 | Lua can cooperate with it to implement the equivalent facility in Lua. | ||
1686 | The following function creates a new ``thread'' in Lua: | ||
1687 | \begin{verbatim} | ||
1688 | lua_State *lua_newthread (lua_State *L, int stacksize); | ||
1689 | \end{verbatim} | ||
1690 | \DefAPI{lua_newthread} | ||
1691 | The new state returned by this function shares with the original state | ||
1692 | all global environment (such as tables, tag methods, etc.), | ||
1693 | but has an independent stack. | ||
1694 | (The use of these multiple stacks must be ``syncronized'' with C. | ||
1695 | How to explain that? TO BE WRITTEN.) | ||
1696 | |||
1697 | Each thread has an independent table for global variables. | ||
1698 | When you create a thread this table is the same as of the given state, | ||
1699 | but you can change each one independently. | ||
1700 | |||
1701 | You destroy threads with \verb|lua_close|. | ||
1702 | When you destroy the last thread of a global state, | ||
1703 | the state itself is also destroyed. | ||
1704 | |||
1705 | |||
1607 | \subsection{The Stack and Indices} | 1706 | \subsection{The Stack and Indices} |
1608 | 1707 | ||
1609 | Lua uses a \emph{stack} to pass values to and from C. | 1708 | Lua uses a \emph{stack} to pass values to and from C. |
@@ -1650,7 +1749,7 @@ Whenever Lua calls C, \DefAPI{LUA_MINSTACK} | |||
1650 | it ensures that | 1749 | it ensures that |
1651 | at least \verb|LUA_MINSTACK| positions are still available. | 1750 | at least \verb|LUA_MINSTACK| positions are still available. |
1652 | \verb|LUA_MINSTACK| is defined in \verb|lua.h| and is at least~16, | 1751 | \verb|LUA_MINSTACK| is defined in \verb|lua.h| and is at least~16, |
1653 | and so you have to worry about stack space only | 1752 | so that usually you have to worry about stack space only |
1654 | when your code has loops pushing elements onto the stack. | 1753 | when your code has loops pushing elements onto the stack. |
1655 | 1754 | ||
1656 | Most query functions accept as indices any value inside the | 1755 | Most query functions accept as indices any value inside the |
@@ -1714,8 +1813,9 @@ then | |||
1714 | To check the type of a stack element, | 1813 | To check the type of a stack element, |
1715 | the following functions are available: | 1814 | the following functions are available: |
1716 | \begin{verbatim} | 1815 | \begin{verbatim} |
1717 | int lua_type (lua_State *L, int index); | ||
1718 | int lua_tag (lua_State *L, int index); | 1816 | int lua_tag (lua_State *L, int index); |
1817 | int lua_rawtag (lua_State *L, int index); | ||
1818 | const char *lua_type (lua_State *L, int index); | ||
1719 | int lua_isnil (lua_State *L, int index); | 1819 | int lua_isnil (lua_State *L, int index); |
1720 | int lua_isnumber (lua_State *L, int index); | 1820 | int lua_isnumber (lua_State *L, int index); |
1721 | int lua_isstring (lua_State *L, int index); | 1821 | int lua_isstring (lua_State *L, int index); |
@@ -1729,34 +1829,21 @@ the following functions are available: | |||
1729 | \DefAPI{lua_istable} | 1829 | \DefAPI{lua_istable} |
1730 | \DefAPI{lua_isfunction}\DefAPI{lua_iscfunction}\DefAPI{lua_isuserdata} | 1830 | \DefAPI{lua_isfunction}\DefAPI{lua_iscfunction}\DefAPI{lua_isuserdata} |
1731 | These functions can be called with any acceptable index. | 1831 | These functions can be called with any acceptable index. |
1732 | The \verb|lua_isnumber| function may have a side effect of changing the | ||
1733 | actual value in the stack from a string to a number. | ||
1734 | 1832 | ||
1735 | \verb|lua_type| returns one of the following constants, | 1833 | \verb|lua_tag| returns the tag of a value in the stack, |
1736 | according to the type of the given object: | 1834 | or \verb|LUA_TNONE| for a non-valid index |
1835 | (that is, if that stack position is ``empty''). | ||
1836 | The tags for the basic types are the following constants: | ||
1737 | \verb|LUA_TNIL|, | 1837 | \verb|LUA_TNIL|, |
1738 | \verb|LUA_TNUMBER|, | 1838 | \verb|LUA_TNUMBER|, |
1739 | \verb|LUA_TSTRING|, | 1839 | \verb|LUA_TSTRING|, |
1740 | \verb|LUA_TTABLE|, | 1840 | \verb|LUA_TTABLE|, |
1741 | \verb|LUA_TFUNCTION|, | 1841 | \verb|LUA_TFUNCTION|, |
1742 | \verb|LUA_TUSERDATA|. | 1842 | \verb|LUA_TUSERDATA|. |
1743 | If the index is non-valid | 1843 | \verb|lua_rawtag| is similar to \verb|lua_tag|, |
1744 | (that is, if that stack position is ``empty''), | 1844 | but it returns the tag of the basic (raw) type of a value. |
1745 | then \verb|lua_type| returns \verb|LUA_TNONE|. | 1845 | \verb|lua_type| is similar to \verb|lua_tag|, |
1746 | These constants can be converted to strings with | 1846 | but it returns the type name of the given value. |
1747 | \begin{verbatim} | ||
1748 | const char *lua_typename (lua_State *L, int t); | ||
1749 | \end{verbatim} | ||
1750 | \DefAPI{lua_typename} | ||
1751 | where \verb|t| is a type returned by \verb|lua_type|. | ||
1752 | The strings returned by \verb|lua_typename| are | ||
1753 | \verb|"nil"|, \verb|"number"|, \verb|"string"|, \verb|"table"|, | ||
1754 | \verb|"function"|, \verb|"userdata"|, and \verb|"no value"|, | ||
1755 | |||
1756 | \verb|lua_tag| returns the tag of a value, | ||
1757 | or \verb|LUA_NOTAG| for a non-valid index. | ||
1758 | The default tag value for all types is equal to the value | ||
1759 | returned by \verb|lua_type|. | ||
1760 | 1847 | ||
1761 | The \verb|lua_is*| functions return~1 if the object is compatible | 1848 | The \verb|lua_is*| functions return~1 if the object is compatible |
1762 | with the given type, and 0 otherwise. | 1849 | with the given type, and 0 otherwise. |
@@ -1767,7 +1854,7 @@ and \verb|lua_isfunction| accepts both Lua functions and C~functions. | |||
1767 | To distinguish between Lua functions and C~functions, | 1854 | To distinguish between Lua functions and C~functions, |
1768 | you should use \verb|lua_iscfunction|. | 1855 | you should use \verb|lua_iscfunction|. |
1769 | To distinguish between numbers and numerical strings, | 1856 | To distinguish between numbers and numerical strings, |
1770 | you can use \verb|lua_type|. | 1857 | you can use \verb|lua_rawtag| (or \verb|lua_tag|). |
1771 | 1858 | ||
1772 | The API also has functions to compare two values in the stack: | 1859 | The API also has functions to compare two values in the stack: |
1773 | \begin{verbatim} | 1860 | \begin{verbatim} |
@@ -1799,9 +1886,6 @@ they act as if the given value had an incorrect type. | |||
1799 | to a floating-point number. | 1886 | to a floating-point number. |
1800 | This value must be a number or a string convertible to number | 1887 | This value must be a number or a string convertible to number |
1801 | \see{coercion}; otherwise, \verb|lua_tonumber| returns~0. | 1888 | \see{coercion}; otherwise, \verb|lua_tonumber| returns~0. |
1802 | If the value is a string, | ||
1803 | \verb|lua_tonumber| also changes the | ||
1804 | actual value in the stack to a number. | ||
1805 | 1889 | ||
1806 | \verb|lua_tostring| converts a Lua value to a string | 1890 | \verb|lua_tostring| converts a Lua value to a string |
1807 | (\verb|const char*|). | 1891 | (\verb|const char*|). |
@@ -1811,11 +1895,12 @@ If the value is a number, | |||
1811 | \verb|lua_tostring| also changes the | 1895 | \verb|lua_tostring| also changes the |
1812 | actual value in the stack to a string. | 1896 | actual value in the stack to a string. |
1813 | This function returns a pointer to a string inside the Lua environment. | 1897 | This function returns a pointer to a string inside the Lua environment. |
1814 | Those strings always have a zero (\verb|'\0'|) | 1898 | This pointer is always fully aligned. |
1899 | The strings always have a zero (\verb|'\0'|) | ||
1815 | after their last character (as in C), | 1900 | after their last character (as in C), |
1816 | but may contain other zeros in their body. | 1901 | but may contain other zeros in their body. |
1817 | If you do not know whether a string may contain zeros, | 1902 | If you do not know whether a string may contain zeros, |
1818 | you should use \verb|lua_strlen| to get its actual length. | 1903 | you can use \verb|lua_strlen| to get its actual length. |
1819 | Because Lua has garbage collection, | 1904 | Because Lua has garbage collection, |
1820 | there is no guarantee that the pointer returned by \verb|lua_tostring| | 1905 | there is no guarantee that the pointer returned by \verb|lua_tostring| |
1821 | will be valid after the respective value is removed from the stack. | 1906 | will be valid after the respective value is removed from the stack. |
@@ -1839,13 +1924,12 @@ push C~values onto the stack: | |||
1839 | void lua_pushnumber (lua_State *L, double n); | 1924 | void lua_pushnumber (lua_State *L, double n); |
1840 | void lua_pushlstring (lua_State *L, const char *s, size_t len); | 1925 | void lua_pushlstring (lua_State *L, const char *s, size_t len); |
1841 | void lua_pushstring (lua_State *L, const char *s); | 1926 | void lua_pushstring (lua_State *L, const char *s); |
1842 | void lua_pushusertag (lua_State *L, void *u, int tag); | ||
1843 | void lua_pushnil (lua_State *L); | 1927 | void lua_pushnil (lua_State *L); |
1844 | void lua_pushcfunction (lua_State *L, lua_CFunction f); | 1928 | void lua_pushcfunction (lua_State *L, lua_CFunction f); |
1845 | \end{verbatim} | 1929 | \end{verbatim} |
1846 | \DefAPI{lua_pushnumber}\DefAPI{lua_pushlstring}\DefAPI{lua_pushstring} | 1930 | \DefAPI{lua_pushnumber}\DefAPI{lua_pushlstring}\DefAPI{lua_pushstring} |
1847 | \DefAPI{lua_pushcfunction}\DefAPI{lua_pushusertag} | 1931 | \DefAPI{lua_pushcfunction}\DefAPI{lua_pushusertag} |
1848 | \DefAPI{lua_pushnil}\DefAPI{lua_pushuserdata}\label{pushing} | 1932 | \DefAPI{lua_pushnil}\label{pushing} |
1849 | These functions receive a C~value, | 1933 | These functions receive a C~value, |
1850 | convert it to a corresponding Lua value, | 1934 | convert it to a corresponding Lua value, |
1851 | and push the result onto the stack. | 1935 | and push the result onto the stack. |
@@ -1857,18 +1941,9 @@ otherwise you should use the more general \verb|lua_pushlstring|, | |||
1857 | which accepts an explicit size. | 1941 | which accepts an explicit size. |
1858 | 1942 | ||
1859 | 1943 | ||
1860 | \subsection{Garbage Collection}\label{GC} | 1944 | \subsection{Garbage Collection API}\label{GC-API} |
1861 | |||
1862 | Lua uses two numbers to control its garbage collection. | ||
1863 | One number counts how many bytes of dynamic memory Lua is using, | ||
1864 | and the other is a threshold. | ||
1865 | When the number of bytes crosses the threshold, | ||
1866 | Lua runs a garbage-collection cycle, | ||
1867 | which reclaims the memory of all ``dead'' objects | ||
1868 | (that is, objects no longer accessible from Lua). | ||
1869 | The byte counter is corrected, | ||
1870 | and then the threshold is reset to twice the value of the byte counter. | ||
1871 | 1945 | ||
1946 | Lua uses two numbers to control its garbage collection \see{GC}. | ||
1872 | You can access the current values of these two numbers through the | 1947 | You can access the current values of these two numbers through the |
1873 | following functions: | 1948 | following functions: |
1874 | \begin{verbatim} | 1949 | \begin{verbatim} |
@@ -1896,35 +1971,62 @@ to set your own threshold | |||
1896 | (the tag method is called after Lua resets the threshold). | 1971 | (the tag method is called after Lua resets the threshold). |
1897 | 1972 | ||
1898 | 1973 | ||
1899 | \subsection{Userdata and Tags}\label{C-tags} | 1974 | \subsection{Userdata} |
1975 | |||
1976 | You can create new userdata with the following functions: | ||
1977 | \begin{verbatim} | ||
1978 | void *lua_newuserdata (lua_State *L, size_t size); | ||
1979 | void lua_newuserdatabox (lua_State *L, void *u); | ||
1980 | \end{verbatim} | ||
1981 | \DefAPI{lua_newuserdata}\DefAPI{lua_newuserdatabox} | ||
1982 | The first function, \verb|lua_newuserdata|, | ||
1983 | allocates a new block of memory with the given size, | ||
1984 | pushes on the stack a new userdata with the block address, | ||
1985 | and returns this address. | ||
1986 | The second function, \verb|lua_newuserdatabox|, | ||
1987 | gets a pointer and pushes on the stack a new userdata | ||
1988 | with that pointer. | ||
1989 | In this case, Lua does not care about the pointer's value. | ||
1990 | By default, all userdata are created with a standard tag, | ||
1991 | \verb|LUA_TUSERDATA|. | ||
1992 | |||
1993 | When Lua collects a userdata created by \verb|lua_newuserdata|, | ||
1994 | it automatically frees its corresponding memory. | ||
1995 | On the other hand, Lua never uses pointers in | ||
1996 | userdata created with \verb|lua_newuserdatabox|; | ||
1997 | it is up to you to free any associated memory, | ||
1998 | setting a garbage-collection tag method, for instance. | ||
1999 | |||
1900 | 2000 | ||
1901 | Because userdata are objects, | 2001 | \subsection{Types and Tags} |
1902 | the function \verb|lua_pushusertag| may create a new userdata. | ||
1903 | If Lua has a userdata with the given value (\verb|void*|) and tag, | ||
1904 | then that userdata is pushed. | ||
1905 | Otherwise, a new userdata is created, with the given value and tag. | ||
1906 | If this function is called with | ||
1907 | \verb|tag| equal to \verb|LUA_ANYTAG|\DefAPI{LUA_ANYTAG}, | ||
1908 | then Lua will try to find any userdata with the given value, | ||
1909 | regardless of its tag. | ||
1910 | If there is no userdata with that value, then a new one is created, | ||
1911 | with tag equal to 0. | ||
1912 | 2002 | ||
1913 | Userdata can have different tags, | 2003 | User-defined types are created with the function |
1914 | whose semantics are only known to the host program. | ||
1915 | Tags are created with the function | ||
1916 | \begin{verbatim} | 2004 | \begin{verbatim} |
1917 | int lua_newtag (lua_State *L); | 2005 | int lua_newtype (lua_State *L, const char *name, int basictype); |
1918 | \end{verbatim} | 2006 | \end{verbatim} |
1919 | \DefAPI{lua_newtag} | 2007 | \DefAPI{lua_newtype} |
1920 | The function \verb|lua_settag| changes the tag of | 2008 | \verb|name| is the name of the new type, |
2009 | and \verb|basictype| is the basic type for objects with this new type, | ||
2010 | which can be \verb|LUA_TUSERDATA| or \verb|LUA_TTABLE|. | ||
2011 | |||
2012 | The function \verb|lua_settag| changes the tag (i.e., the type) of | ||
1921 | the object on top of the stack (without popping it): | 2013 | the object on top of the stack (without popping it): |
1922 | \begin{verbatim} | 2014 | \begin{verbatim} |
1923 | void lua_settag (lua_State *L, int tag); | 2015 | void lua_settag (lua_State *L, int tag); |
1924 | \end{verbatim} | 2016 | \end{verbatim} |
1925 | \DefAPI{lua_settag} | 2017 | \DefAPI{lua_settag} |
1926 | The object must be a userdata or a table; | 2018 | The given \verb|tag| must be a user-defined tag, |
1927 | the given \verb|tag| must be a value created with \verb|lua_newtag|. | 2019 | and the basic type of the object must be the basic type for that |
2020 | tag (userdata or table). | ||
2021 | |||
2022 | The following functions allow you to translate a tag to a type name | ||
2023 | and a type name to a tag: | ||
2024 | \begin{verbatim} | ||
2025 | int lua_name2tag (lua_State *L, const char *name); | ||
2026 | const char *lua_tag2name (lua_State *L, int tag); | ||
2027 | \end{verbatim} | ||
2028 | \DefAPI{lua_name2tag}\DefAPI{lua_tag2name} | ||
2029 | |||
1928 | 2030 | ||
1929 | \subsection{Executing Lua Code}\label{luado} | 2031 | \subsection{Executing Lua Code}\label{luado} |
1930 | A host program can execute Lua chunks written in a file or in a string | 2032 | A host program can execute Lua chunks written in a file or in a string |
@@ -2144,7 +2246,7 @@ In both functions, | |||
2144 | \verb|nargs| is the number of arguments that you pushed onto the stack. | 2246 | \verb|nargs| is the number of arguments that you pushed onto the stack. |
2145 | All arguments and the function value are popped from the stack, | 2247 | All arguments and the function value are popped from the stack, |
2146 | and the function results are pushed. | 2248 | and the function results are pushed. |
2147 | The number of results are adjusted \see{adjust} to \verb|nresults|, | 2249 | The number of results are adjusted to \verb|nresults|, |
2148 | unless \verb|nresults| is \IndexAPI{LUA_MULTRET}. | 2250 | unless \verb|nresults| is \IndexAPI{LUA_MULTRET}. |
2149 | In that case, \emph{all} results from the function are pushed. | 2251 | In that case, \emph{all} results from the function are pushed. |
2150 | The function results are pushed in direct order | 2252 | The function results are pushed in direct order |
@@ -2392,6 +2494,27 @@ Any C~library can store data into this table, | |||
2392 | as long as it chooses a key different from other libraries. | 2494 | as long as it chooses a key different from other libraries. |
2393 | 2495 | ||
2394 | 2496 | ||
2497 | |||
2498 | \subsection{Weak Tables} | ||
2499 | |||
2500 | The following constants and functions control the weak mode of a table: | ||
2501 | \begin{verbatim} | ||
2502 | #define LUA_WEAK_KEY ... | ||
2503 | #define LUA_WEAK_VALUE ... | ||
2504 | \end{verbatim} | ||
2505 | \begin{verbatim} | ||
2506 | void lua_setweakmode (lua_State *L, int mode); | ||
2507 | int lua_getweakmode (lua_State *L, int index); | ||
2508 | \end{verbatim} | ||
2509 | \DefAPI{lua_setweakmode}\DefAPI{lua_getweakmode} | ||
2510 | Both functions operate over the table at the top of the stack. | ||
2511 | Modes are described as bit sets, so that | ||
2512 | \verb|LUA_WEAK_KEY| means weak keys, | ||
2513 | \verb|LUA_WEAK_VALUE| means weak values, | ||
2514 | \verb|LUA_WEAK_KEY | LUA_WEAK_VALUE| means both, | ||
2515 | and zero means none. | ||
2516 | |||
2517 | |||
2395 | \section{Standard Libraries} | 2518 | \section{Standard Libraries} |
2396 | 2519 | ||
2397 | The standard libraries provide useful functions | 2520 | The standard libraries provide useful functions |
@@ -2499,22 +2622,25 @@ or as pre-compiled chunks. | |||
2499 | When called without arguments, | 2622 | When called without arguments, |
2500 | \verb|dofile| executes the contents of the standard input (\verb|stdin|). | 2623 | \verb|dofile| executes the contents of the standard input (\verb|stdin|). |
2501 | If there is any error executing the file, | 2624 | If there is any error executing the file, |
2502 | then \verb|dofile| returns \nil. | 2625 | then \verb|dofile| returns \nil{} plus one of the following strings |
2626 | describing the error: | ||
2627 | \verb|"file error"|, \verb|"run-time error"|, | ||
2628 | \verb|"syntax error"|, \verb|"memory error"|, or | ||
2629 | \verb|"error in error handling"|. | ||
2503 | Otherwise, it returns the values returned by the chunk, | 2630 | Otherwise, it returns the values returned by the chunk, |
2504 | or a non-\nil\ value if the chunk returns no values. | 2631 | or a non-\nil\ value if the chunk returns no values. |
2505 | It issues an error when called with a non-string argument. | 2632 | It issues an error when called with a non-string argument. |
2506 | %\verb|dofile| is equivalent to the API function \verb|lua_dofile|. | ||
2507 | 2633 | ||
2508 | \subsubsection*{\ff \T{dostring (string [, chunkname])}}\DefLIB{dostring} | 2634 | \subsubsection*{\ff \T{dostring (string [, chunkname])}}\DefLIB{dostring} |
2509 | Executes a given string as a Lua chunk. | 2635 | Executes a given string as a Lua chunk. |
2510 | If there is any error executing the string, | 2636 | If there is any error executing the string, |
2511 | then \verb|dostring| returns \nil. | 2637 | then \verb|dostring| returns \nil plus a string describing |
2638 | the error (see \verb|dofile|). | ||
2512 | Otherwise, it returns the values returned by the chunk, | 2639 | Otherwise, it returns the values returned by the chunk, |
2513 | or a non-\nil\ value if the chunk returns no values. | 2640 | or a non-\nil\ value if the chunk returns no values. |
2514 | The optional parameter \verb|chunkname| | 2641 | The optional parameter \verb|chunkname| |
2515 | is the ``name of the chunk'', | 2642 | is the ``name of the chunk'', |
2516 | used in error messages and debug information. | 2643 | used in error messages and debug information. |
2517 | %\verb|dostring| is equivalent to the API function \verb|lua_dostring|. | ||
2518 | 2644 | ||
2519 | \subsubsection*{\ff \T{error (message)}}\DefLIB{error}\label{pdf-error} | 2645 | \subsubsection*{\ff \T{error (message)}}\DefLIB{error}\label{pdf-error} |
2520 | Calls the error handler \see{error} and then terminates | 2646 | Calls the error handler \see{error} and then terminates |
@@ -2554,7 +2680,7 @@ For each index, the function is called with the index and | |||
2554 | respective value as arguments. | 2680 | respective value as arguments. |
2555 | Indices are visited in sequential order, | 2681 | Indices are visited in sequential order, |
2556 | from~1 to \verb|n|, | 2682 | from~1 to \verb|n|, |
2557 | where \verb|n| is the result of \verb|getn(table)| \see{getn}. | 2683 | where \verb|n| is the result of \verb|getn(table)| (see below). |
2558 | If the function returns any non-\nil\ value, | 2684 | If the function returns any non-\nil\ value, |
2559 | then the loop is broken, and this value is returned | 2685 | then the loop is broken, and this value is returned |
2560 | as the final value of \verb|foreachi|. | 2686 | as the final value of \verb|foreachi|. |
@@ -2568,6 +2694,10 @@ This function could be defined in Lua: | |||
2568 | end | 2694 | end |
2569 | \end{verbatim} | 2695 | \end{verbatim} |
2570 | 2696 | ||
2697 | \subsubsection*{\ff \T{gcinfo ()}}\DefLIB{gcinfo} | ||
2698 | Returns the number of Kbytes of dynamic memory Lua is using, | ||
2699 | and (as a second result) the | ||
2700 | current garbage collector threshold (also in Kbytes). | ||
2571 | 2701 | ||
2572 | \subsubsection*{\ff \T{getglobal (name)}}\DefLIB{getglobal} | 2702 | \subsubsection*{\ff \T{getglobal (name)}}\DefLIB{getglobal} |
2573 | Gets the value of a global variable, | 2703 | Gets the value of a global variable, |
@@ -2606,9 +2736,10 @@ Returns the current table of globals. | |||
2606 | If the argument \verb|table| is given, | 2736 | If the argument \verb|table| is given, |
2607 | then it also sets this table as the table of globals. | 2737 | then it also sets this table as the table of globals. |
2608 | 2738 | ||
2609 | \subsubsection*{\ff \T{newtag ()}}\DefLIB{newtag}\label{pdf-newtag} | 2739 | \subsubsection*{\ff \T{newtype (name)}}\DefLIB{newtype}\label{pdf-newtype} |
2610 | Returns a new tag. | 2740 | Creates a new type with the given name |
2611 | %\verb|newtag| is equivalent to the API function \verb|lua_newtag|. | 2741 | (which can be used only for table objects). |
2742 | Returns the tag of the new type. | ||
2612 | 2743 | ||
2613 | \subsubsection*{\ff \T{next (table, [index])}}\DefLIB{next} | 2744 | \subsubsection*{\ff \T{next (table, [index])}}\DefLIB{next} |
2614 | Allows a program to traverse all fields of a table. | 2745 | Allows a program to traverse all fields of a table. |
@@ -2658,6 +2789,20 @@ without invoking any tag method. | |||
2658 | \verb|index| is any value different from \nil, | 2789 | \verb|index| is any value different from \nil, |
2659 | and \verb|value| is any Lua value. | 2790 | and \verb|value| is any Lua value. |
2660 | 2791 | ||
2792 | \subsubsection*{\ff \T{rawtype (v)}}\DefLIB{rawtype} | ||
2793 | Returns the basic (raw) type of its only argument, coded as a string. | ||
2794 | The possible results of this function are | ||
2795 | \verb|"nil"| (a string, not the value \nil), | ||
2796 | \verb|"number"|, | ||
2797 | \verb|"string"|, | ||
2798 | \verb|"table"|, | ||
2799 | \verb|"function"|, | ||
2800 | and \verb|"userdata"|. | ||
2801 | |||
2802 | \subsubsection*{\ff \T{require (module)}}\DefLIB{require} | ||
2803 | |||
2804 | TO BE WRITTEN. | ||
2805 | |||
2661 | \subsubsection*{\ff \T{setglobal (name, value)}}\DefLIB{setglobal} | 2806 | \subsubsection*{\ff \T{setglobal (name, value)}}\DefLIB{setglobal} |
2662 | Sets the named global variable to the given value, | 2807 | Sets the named global variable to the given value, |
2663 | or calls a tag method for ``setglobal''. | 2808 | or calls a tag method for ``setglobal''. |
@@ -2665,10 +2810,9 @@ Its full semantics is explained in \See{tag-method}. | |||
2665 | The string \verb|name| does not need to be a | 2810 | The string \verb|name| does not need to be a |
2666 | syntactically valid variable name. | 2811 | syntactically valid variable name. |
2667 | 2812 | ||
2668 | \subsubsection*{\ff \T{settag (t, tag)}}\DefLIB{settag} | 2813 | \subsubsection*{\ff \T{settype (t, type)}}\DefLIB{settype}\label{pdf-settype} |
2669 | Sets the tag of a given table \see{TypesSec}. | 2814 | Sets the type of a given table \see{TypesSec}. |
2670 | \verb|tag| must be a value created with \verb|newtag| | 2815 | \verb|type| must be the name or the tag of a user-defined type. |
2671 | \see{pdf-newtag}. | ||
2672 | \verb|settag| returns the value of its first argument (the table). | 2816 | \verb|settag| returns the value of its first argument (the table). |
2673 | For the safety of host programs, | 2817 | For the safety of host programs, |
2674 | it is impossible to change the tag of a userdata from Lua. | 2818 | it is impossible to change the tag of a userdata from Lua. |
@@ -2724,7 +2868,6 @@ For complete control of how numbers are converted, | |||
2724 | use function \verb|format|. | 2868 | use function \verb|format|. |
2725 | 2869 | ||
2726 | 2870 | ||
2727 | |||
2728 | \subsubsection*{\ff \T{tinsert (table, [pos,] value)}}\DefLIB{tinsert} | 2871 | \subsubsection*{\ff \T{tinsert (table, [pos,] value)}}\DefLIB{tinsert} |
2729 | 2872 | ||
2730 | Inserts element \verb|value| at table position \verb|pos|, | 2873 | Inserts element \verb|value| at table position \verb|pos|, |
@@ -2786,15 +2929,28 @@ except that the table accesses are all \emph{raw} | |||
2786 | \end{verbatim} | 2929 | \end{verbatim} |
2787 | 2930 | ||
2788 | \subsubsection*{\ff \T{type (v)}}\DefLIB{type}\label{pdf-type} | 2931 | \subsubsection*{\ff \T{type (v)}}\DefLIB{type}\label{pdf-type} |
2789 | Allows Lua programs to test the type of a value. | 2932 | Returns the type name of its only argument. |
2790 | It receives one argument, and returns its type, coded as a string. | 2933 | |
2791 | The possible results of this function are | 2934 | \subsubsection*{\ff \T{unpack (list)}}\DefLIB{unpack} |
2792 | \verb|"nil"| (a string, not the value \nil), | 2935 | Returns all elements from the given list. |
2793 | \verb|"number"|, | 2936 | This function is equivalent to |
2794 | \verb|"string"|, | 2937 | \begin{verbatim} |
2795 | \verb|"table"|, | 2938 | return list[1], list[2], ..., list[n] |
2796 | \verb|"function"|, | 2939 | \end{verbatim} |
2797 | and \verb|"userdata"|. | 2940 | except that the above code can be valid only for a fixed \M{n}. |
2941 | The number of returned values, \M{n}, | ||
2942 | is the result of \verb|getn(list)| \see{getn}, | ||
2943 | |||
2944 | \subsubsection*{\ff \T{weakmode (table, mode)}}\DefLIB{weakmode}\label{weakmode} | ||
2945 | |||
2946 | Controls the weakness of a table. | ||
2947 | When \verb|mode| is \verb|"?"|, | ||
2948 | returns the current mode of the table, as a string; | ||
2949 | otherwise, sets the weakmode of the table to the given mode (also a string). | ||
2950 | Valid mode strings are \verb|"k"| for weak keys, | ||
2951 | \verb|"v"| for weak values, | ||
2952 | \verb|"kv"| for both, | ||
2953 | and \verb|""| for none (that is, for ``normal'' tables). | ||
2798 | 2954 | ||
2799 | 2955 | ||
2800 | \subsection{String Manipulation} | 2956 | \subsection{String Manipulation} |
@@ -2913,9 +3069,8 @@ For example, \verb|"%*g"| can be simulated with | |||
2913 | \verb|"%"..width.."g"|. | 3069 | \verb|"%"..width.."g"|. |
2914 | 3070 | ||
2915 | \NOTE | 3071 | \NOTE |
2916 | Neither the format string nor the string values to be formatted with | 3072 | String values to be formatted with |
2917 | \verb|%s| can contain embedded zeros. | 3073 | \verb|%s| cannot contain embedded zeros. |
2918 | \verb|%q| handles string values with embedded zeros. | ||
2919 | 3074 | ||
2920 | \subsubsection*{\ff \T{gsub (s, pat, repl [, n])}} | 3075 | \subsubsection*{\ff \T{gsub (s, pat, repl [, n])}} |
2921 | \DefLIB{gsub} | 3076 | \DefLIB{gsub} |
@@ -2975,7 +3130,7 @@ Here are some examples: | |||
2975 | a \Def{character class} is used to represent a set of characters. | 3130 | a \Def{character class} is used to represent a set of characters. |
2976 | The following combinations are allowed in describing a character class: | 3131 | The following combinations are allowed in describing a character class: |
2977 | \begin{description} | 3132 | \begin{description} |
2978 | \item[\emph{x}] (where \emph{x} is any magic characters | 3133 | \item[\emph{x}] (where \emph{x} is not one of the magic characters |
2979 | \verb|^$()%.[]*+-?|) | 3134 | \verb|^$()%.[]*+-?|) |
2980 | --- represents the character \emph{x} itself. | 3135 | --- represents the character \emph{x} itself. |
2981 | \item[\T{.}] --- (a dot) represents all characters. | 3136 | \item[\T{.}] --- (a dot) represents all characters. |
@@ -3170,7 +3325,7 @@ The \verb|mode| string can be any of the following: | |||
3170 | \end{description} | 3325 | \end{description} |
3171 | The \verb|mode| string may also have a \verb|b| at the end, | 3326 | The \verb|mode| string may also have a \verb|b| at the end, |
3172 | which is needed in some systems to open the file in binary mode. | 3327 | which is needed in some systems to open the file in binary mode. |
3173 | This string is exactlty what is used in the standard~C function \verb|fopen|. | 3328 | This string is exactly what is used in the standard~C function \verb|fopen|. |
3174 | 3329 | ||
3175 | \subsubsection*{\ff \T{closefile (handle)}}\DefLIB{closefile} | 3330 | \subsubsection*{\ff \T{closefile (handle)}}\DefLIB{closefile} |
3176 | 3331 | ||
@@ -3180,7 +3335,7 @@ It does not modify either \verb|_INPUT| or \verb|_OUTPUT|. | |||
3180 | \subsubsection*{\ff \T{readfrom (filename)}}\DefLIB{readfrom} | 3335 | \subsubsection*{\ff \T{readfrom (filename)}}\DefLIB{readfrom} |
3181 | 3336 | ||
3182 | This function may be called in two ways. | 3337 | This function may be called in two ways. |
3183 | When called with a file name, it opens the named file, | 3338 | When called with a file name, it opens the named file (in text mode), |
3184 | sets its handle as the value of \verb|_INPUT|, | 3339 | sets its handle as the value of \verb|_INPUT|, |
3185 | and returns this value. | 3340 | and returns this value. |
3186 | It does not close the current input file. | 3341 | It does not close the current input file. |
@@ -3202,7 +3357,7 @@ usually limited and depends on the system. | |||
3202 | 3357 | ||
3203 | This function may be called in two ways. | 3358 | This function may be called in two ways. |
3204 | When called with a file name, | 3359 | When called with a file name, |
3205 | it opens the named file, | 3360 | it opens the named file (in text mode), |
3206 | sets its handle as the value of \verb|_OUTPUT|, | 3361 | sets its handle as the value of \verb|_OUTPUT|, |
3207 | and returns this value. | 3362 | and returns this value. |
3208 | It does not close the current output file. | 3363 | It does not close the current output file. |
@@ -3225,8 +3380,8 @@ usually limited and depends on the system. | |||
3225 | 3380 | ||
3226 | \subsubsection*{\ff \T{appendto (filename)}}\DefLIB{appendto} | 3381 | \subsubsection*{\ff \T{appendto (filename)}}\DefLIB{appendto} |
3227 | 3382 | ||
3228 | Opens a file named \verb|filename| and sets its handle as the | 3383 | Opens a file named \verb|filename| (in text mode) |
3229 | value of \verb|_OUTPUT|. | 3384 | and sets its handle as the value of \verb|_OUTPUT|. |
3230 | Unlike the \verb|writeto| operation, | 3385 | Unlike the \verb|writeto| operation, |
3231 | this function does not erase any previous contents of the file; | 3386 | this function does not erase any previous contents of the file; |
3232 | instead, anything written to the file is appended to its end. | 3387 | instead, anything written to the file is appended to its end. |
@@ -3278,13 +3433,26 @@ beginning of the file (and returns 0); | |||
3278 | and the call \verb|seek(file, "end")| sets the position to the | 3433 | and the call \verb|seek(file, "end")| sets the position to the |
3279 | end of the file, and returns its size. | 3434 | end of the file, and returns its size. |
3280 | 3435 | ||
3436 | \subsubsection*{\ff \T{tmpfile ()}}\DefLIB{tmpfile} | ||
3437 | |||
3438 | Returns a handle for a temporary file. | ||
3439 | This file is open in read/write mode, | ||
3440 | and it is automatically removed when the program ends. | ||
3441 | |||
3281 | \subsubsection*{\ff \T{tmpname ()}}\DefLIB{tmpname} | 3442 | \subsubsection*{\ff \T{tmpname ()}}\DefLIB{tmpname} |
3282 | 3443 | ||
3283 | Returns a string with a file name that can safely | 3444 | Returns a string with a file name that can |
3284 | be used for a temporary file. | 3445 | be used for a temporary file. |
3285 | The file must be explicitly opened before its use | 3446 | The file must be explicitly opened before its use |
3286 | and removed when no longer needed. | 3447 | and removed when no longer needed. |
3287 | 3448 | ||
3449 | This function is equivalent to the \verb|tmpnam| C function, | ||
3450 | and many people advise against its use, | ||
3451 | because between the time you call the function | ||
3452 | and the time you open the file, | ||
3453 | it is possible for another process | ||
3454 | to create a file with the same name. | ||
3455 | |||
3288 | \subsubsection*{\ff \T{read ([filehandle,] format1, ...)}}\DefLIB{read} | 3456 | \subsubsection*{\ff \T{read ([filehandle,] format1, ...)}}\DefLIB{read} |
3289 | 3457 | ||
3290 | Reads file \verb|_INPUT|, | 3458 | Reads file \verb|_INPUT|, |
@@ -3301,16 +3469,23 @@ The available formats are | |||
3301 | \begin{description} | 3469 | \begin{description} |
3302 | \item[``*n''] reads a number; | 3470 | \item[``*n''] reads a number; |
3303 | this is the only format that returns a number instead of a string. | 3471 | this is the only format that returns a number instead of a string. |
3304 | \item[``*l''] reads the next line | ||
3305 | (skipping the end of line), or \nil\ on end of file. | ||
3306 | This is the default format. | ||
3307 | \item[``*a''] reads the whole file, starting at the current position. | 3472 | \item[``*a''] reads the whole file, starting at the current position. |
3308 | On end of file, it returns the empty string. | 3473 | On end of file, it returns the empty string. |
3309 | \item[``*w''] reads the next word | 3474 | \item[``*u\emph{string}''] reads until the first occurence of |
3310 | (maximal sequence of non--white-space characters), | 3475 | \emph{string} in the file. |
3311 | skipping spaces if necessary, or \nil\ on end of file. | 3476 | The string itself is read, but it is not included in the result. |
3477 | If it cannot finds the string, | ||
3478 | reads (and returns) the file until its end, | ||
3479 | or \nil\ if the file was already on its end. | ||
3480 | \item[``*l''] equivalent to \verb|"*u\n"|. | ||
3481 | Reads the next line (skipping the end of line), | ||
3482 | returning \nil\ on end of file. | ||
3483 | This is the default format. | ||
3312 | \item[\emph{number}] reads a string with up to that number of characters, | 3484 | \item[\emph{number}] reads a string with up to that number of characters, |
3313 | or \nil\ on end of file. | 3485 | or \nil\ on end of file. |
3486 | Particularly, if number is zero, | ||
3487 | reads nothing and returns an empty string, | ||
3488 | or \nil\ on end of file. | ||
3314 | \end{description} | 3489 | \end{description} |
3315 | 3490 | ||
3316 | \subsubsection*{\ff \T{write ([filehandle, ] value1, ...)}}\DefLIB{write} | 3491 | \subsubsection*{\ff \T{write ([filehandle, ] value1, ...)}}\DefLIB{write} |
@@ -3331,15 +3506,41 @@ plus a string describing the error. | |||
3331 | Returns an approximation of the amount of CPU time | 3506 | Returns an approximation of the amount of CPU time |
3332 | used by the program, in seconds. | 3507 | used by the program, in seconds. |
3333 | 3508 | ||
3334 | \subsubsection*{\ff \T{date ([format])}}\DefLIB{date} | 3509 | \subsubsection*{\ff \T{date ([format [, time]])}}\DefLIB{date} |
3510 | |||
3511 | Returns a string or a table containing date and time, | ||
3512 | formatted according to the given string \verb|format|. | ||
3513 | |||
3514 | If the \verb|time| argument is present, | ||
3515 | this is the time to be formatted | ||
3516 | (see the \verb|time| function for a description of this value). | ||
3517 | Otherwise, \verb|date| formats the current time. | ||
3518 | |||
3519 | If \verb|format| starts with \verb|!|, | ||
3520 | the date is formatted in Coordinated Universal Time. | ||
3335 | 3521 | ||
3336 | Returns a string containing date and time | 3522 | After that optional character, |
3337 | formatted according to the given string \verb|format|, | 3523 | if \verb|format| is \verb|*t|, |
3338 | following the same rules of the ANSI~C function \verb|strftime|. | 3524 | the function returns a table with the following fields: |
3525 | \verb|year|, \verb|month| (1-12), \verb|day| (1-31), | ||
3526 | \verb|hour| (0-23), \verb|min| (0-59), \verb|sec| (0-59), | ||
3527 | \verb|wday| (weekday, Sunday is 1), | ||
3528 | \verb|yday| (day of the year), | ||
3529 | and \verb|isdst| (daylight saving flag). | ||
3530 | |||
3531 | If format is not \verb|*t|, the function returns the date | ||
3532 | as a string, formatted according with the | ||
3533 | same rules of the ANSI~C function \verb|strftime|. | ||
3339 | When called without arguments, | 3534 | When called without arguments, |
3340 | it returns a reasonable date and time representation that depends on | 3535 | it returns a reasonable date and time representation that depends on |
3341 | the host system and on the current locale. | 3536 | the host system and on the current locale. |
3342 | 3537 | ||
3538 | \subsubsection*{\ff \T{difftime (t1, t2)}}\DefLIB{difftime} | ||
3539 | |||
3540 | Returns the number of seconds from time \verb|t1| to time \verb|t2|. | ||
3541 | In Posix, Windows, and some other systems, | ||
3542 | this value is exactly \Math{t1-t2}. | ||
3543 | |||
3343 | \subsubsection*{\ff \T{execute (command)}}\DefLIB{execute} | 3544 | \subsubsection*{\ff \T{execute (command)}}\DefLIB{execute} |
3344 | 3545 | ||
3345 | This function is equivalent to the C~function \verb|system|. | 3546 | This function is equivalent to the C~function \verb|system|. |
@@ -3369,6 +3570,21 @@ the default category is \verb|"all"|. | |||
3369 | The function returns the name of the new locale, | 3570 | The function returns the name of the new locale, |
3370 | or \nil\ if the request cannot be honored. | 3571 | or \nil\ if the request cannot be honored. |
3371 | 3572 | ||
3573 | \subsubsection*{\ff \T{time ([table])}}\DefLIB{time} | ||
3574 | |||
3575 | Returns the current time (when called without arguments), | ||
3576 | or a time representing the date/time specified by the given table. | ||
3577 | This table must have fields \verb|year|, \verb|month|, and \verb|day|, | ||
3578 | and may have fields \verb|hour|, \verb|min|, \verb|sec|, and \verb|isdst| | ||
3579 | (for a description of these fields, see the \verb|date| function). | ||
3580 | |||
3581 | The returned value is a number, whose meaning depends on your system. | ||
3582 | In Posix, Windows, and some other systems, this number counts the number | ||
3583 | of seconds since some given start time (the ``epoch''). | ||
3584 | In other systems, the meaning is not specified, | ||
3585 | and such number can be used only as an argument to | ||
3586 | functions \verb|date| and \verb|difftime|. | ||
3587 | |||
3372 | 3588 | ||
3373 | \section{The Debug Interface} \label{debugI} | 3589 | \section{The Debug Interface} \label{debugI} |
3374 | 3590 | ||
@@ -3597,11 +3813,11 @@ You should exert great care when using this library. | |||
3597 | The functions provided here should be used exclusively for debugging | 3813 | The functions provided here should be used exclusively for debugging |
3598 | and similar tasks (e.g., profiling). | 3814 | and similar tasks (e.g., profiling). |
3599 | Please resist the temptation to use them as a | 3815 | Please resist the temptation to use them as a |
3600 | usual programming tool. | 3816 | usual programming tool: |
3601 | They are slow and violate some (otherwise) secure aspects of the | 3817 | They can be \emph{very} slow. |
3602 | language (e.g., privacy of local variables). | 3818 | Moreover, \verb|setlocal| and \verb|getlocal| |
3603 | As a general rule, if your program does not need this library, | 3819 | violate the privacy of local variables, |
3604 | do not open it. | 3820 | and therefore can compromise some (otherwise) secure code. |
3605 | 3821 | ||
3606 | 3822 | ||
3607 | \subsubsection*{\ff \T{getinfo (function, [what])}}\DefLIB{getinfo} | 3823 | \subsubsection*{\ff \T{getinfo (function, [what])}}\DefLIB{getinfo} |
@@ -3619,6 +3835,8 @@ then \verb|getinfo| returns \nil. | |||
3619 | The returned table contains all the fields returned by \verb|lua_getinfo|, | 3835 | The returned table contains all the fields returned by \verb|lua_getinfo|, |
3620 | with the string \verb|what| describing what to get. | 3836 | with the string \verb|what| describing what to get. |
3621 | The default for \verb|what| is to get all information available. | 3837 | The default for \verb|what| is to get all information available. |
3838 | The option \verb|f|, if present, | ||
3839 | adds a field named \verb|func| with the function itself. | ||
3622 | 3840 | ||
3623 | For instance, the expression \verb|getinfo(1,"n").name| returns | 3841 | For instance, the expression \verb|getinfo(1,"n").name| returns |
3624 | the name of the current function, if a reasonable name can be found, | 3842 | the name of the current function, if a reasonable name can be found, |
@@ -3770,41 +3988,30 @@ Lua means ``moon'' in Portuguese. | |||
3770 | \section*{Incompatibilities with Previous Versions} | 3988 | \section*{Incompatibilities with Previous Versions} |
3771 | \addcontentsline{toc}{section}{Incompatibilities with Previous Versions} | 3989 | \addcontentsline{toc}{section}{Incompatibilities with Previous Versions} |
3772 | 3990 | ||
3773 | Lua 4.0 is a major revision of the language. | ||
3774 | We took a great care to avoid incompatibilities with | 3991 | We took a great care to avoid incompatibilities with |
3775 | the previous public versions of Lua, | 3992 | the previous public versions of Lua, |
3776 | but some differences had to be introduced. | 3993 | but some differences had to be introduced. |
3777 | Here is a list of all these incompatibilities. | 3994 | Here is a list of all these incompatibilities. |
3778 | 3995 | ||
3779 | 3996 | ||
3780 | \subsection*{Incompatibilities with \Index{version 3.2}} | 3997 | \subsection*{Incompatibilities with \Index{version 4.0}} |
3781 | 3998 | ||
3782 | \subsubsection*{Changes in the Language} | 3999 | \subsubsection*{Changes in the Language} |
3783 | \begin{itemize} | 4000 | \begin{itemize} |
3784 | 4001 | ||
3785 | \item | 4002 | \item |
3786 | All pragmas (\verb|$debug|, \verb|$if|, \ldots) have been removed. | 4003 | Function calls written between parentheses result in exactly one value. |
3787 | 4004 | ||
3788 | \item | 4005 | \item |
3789 | \rwd{for}, \rwd{break}, and \rwd{in} are now reserved words. | 4006 | A function call as the last expression in a list constructor |
4007 | (like \verb|{a,b,f()}}|) has all its return values inserted in the list. | ||
3790 | 4008 | ||
3791 | \item | 4009 | \item |
3792 | Garbage-collection tag methods for tables is now obsolete. | 4010 | \rwd{global} and \rwd{in} are reserved words. |
3793 | 4011 | ||
3794 | \item | 4012 | \item |
3795 | There is now only one tag method for order operators. | 4013 | When a literal string of the form \verb|[[...]]| starts with a newline, |
3796 | 4014 | this newline is ignored. | |
3797 | \item | ||
3798 | In nested function calls like \verb|f(g(x))|, | ||
3799 | \emph{all} return values from \verb|g| are passed as arguments to \verb|f|. | ||
3800 | This only happens when \verb|g| is the last | ||
3801 | or the only argument to \verb|f|. | ||
3802 | |||
3803 | \item | ||
3804 | The pre-compiler may assume that some operators are associative, | ||
3805 | for optimizations. | ||
3806 | This may cause problems if these operators | ||
3807 | have non-associative tag methods. | ||
3808 | 4015 | ||
3809 | \item Old pre-compiled code is obsolete, and must be re-compiled. | 4016 | \item Old pre-compiled code is obsolete, and must be re-compiled. |
3810 | 4017 | ||
@@ -3815,29 +4022,15 @@ have non-associative tag methods. | |||
3815 | \begin{itemize} | 4022 | \begin{itemize} |
3816 | 4023 | ||
3817 | \item | 4024 | \item |
3818 | When traversing a table with \verb|next| or \verb|foreach|, | 4025 | The \verb|read| option \verb|*w| is obsolete. |
3819 | the table cannot be modified in any way. | ||
3820 | |||
3821 | \item | ||
3822 | General read patterns are now obsolete. | ||
3823 | 4026 | ||
3824 | \item | 4027 | \item |
3825 | The functions \verb|rawgettable| and \verb|rawsettable| | 4028 | The \verb|format| option \verb|%n$| is obsolete. |
3826 | have been renamed to \verb|rawget| and \verb|rawset|. | ||
3827 | 4029 | ||
3828 | \item | 4030 | \item |
3829 | The functions \verb|foreachvar|, \verb|nextvar|, | 4031 | \verb|newtag| is deprecated, being replaced by \verb|newtype|. |
3830 | \verb|rawsetglobal|, and \verb|rawgetglobal| are obsolete. | 4032 | Tags created in Lua with \verb|newtype| (or \verb|newtag|) can only |
3831 | You can get their functionality using table operations | 4033 | be used for tables. |
3832 | over the table of globals, | ||
3833 | which is returned by \verb|globals|. | ||
3834 | |||
3835 | \item | ||
3836 | \verb|setglobal| and \verb|sort| no longer return a value; | ||
3837 | \verb|type| no longer returns a second value. | ||
3838 | |||
3839 | \item | ||
3840 | The \verb|p| option in function \verb|call| is now obsolete. | ||
3841 | 4034 | ||
3842 | \end{itemize} | 4035 | \end{itemize} |
3843 | 4036 | ||
@@ -3846,11 +4039,8 @@ The \verb|p| option in function \verb|call| is now obsolete. | |||
3846 | \begin{itemize} | 4039 | \begin{itemize} |
3847 | 4040 | ||
3848 | \item | 4041 | \item |
3849 | The API has been completely rewritten: | 4042 | The \verb|lua_pushuserdata| function has been replaced by |
3850 | It is now fully reentrant and much clearer. | 4043 | \verb|lua_newuserdatabox|. |
3851 | |||
3852 | \item | ||
3853 | The debug API has been completely rewritten. | ||
3854 | 4044 | ||
3855 | \end{itemize} | 4045 | \end{itemize} |
3856 | 4046 | ||
@@ -3879,32 +4069,29 @@ The debug API has been completely rewritten. | |||
3879 | varlist1 \ter{=} explist1 | 4069 | varlist1 \ter{=} explist1 |
3880 | \OrNL functioncall | 4070 | \OrNL functioncall |
3881 | \OrNL \rwd{do} block \rwd{end} | 4071 | \OrNL \rwd{do} block \rwd{end} |
3882 | \OrNL \rwd{while} exp1 \rwd{do} block \rwd{end} | 4072 | \OrNL \rwd{while} exp \rwd{do} block \rwd{end} |
3883 | \OrNL \rwd{repeat} block \rwd{until} exp1 | 4073 | \OrNL \rwd{repeat} block \rwd{until} exp |
3884 | \OrNL \rwd{if} exp1 \rwd{then} block | 4074 | \OrNL \rwd{if} exp \rwd{then} block |
3885 | \rep{\rwd{elseif} exp1 \rwd{then} block} | 4075 | \rep{\rwd{elseif} exp \rwd{then} block} |
3886 | \opt{\rwd{else} block} \rwd{end} | 4076 | \opt{\rwd{else} block} \rwd{end} |
3887 | \OrNL \rwd{return} \opt{explist1} | 4077 | \OrNL \rwd{return} \opt{explist1} |
3888 | \OrNL \rwd{break} | 4078 | \OrNL \rwd{break} |
3889 | \OrNL \rwd{for} \Nter{name} \ter{=} exp1 \ter{,} exp1 \opt{\ter{,} exp1} | 4079 | \OrNL \rwd{for} \Nter{name} \ter{=} exp \ter{,} exp \opt{\ter{,} exp} |
3890 | \rwd{do} block \rwd{end} | 4080 | \rwd{do} block \rwd{end} |
3891 | \OrNL \rwd{for} \Nter{name} \ter{,} \Nter{name} \rwd{in} exp1 | 4081 | \OrNL \rwd{for} \Nter{name} \ter{,} \Nter{name} \rwd{in} exp |
3892 | \rwd{do} block \rwd{end} | 4082 | \rwd{do} block \rwd{end} |
3893 | \OrNL \rwd{function} funcname \ter{(} \opt{parlist1} \ter{)} block \rwd{end} | 4083 | \OrNL \rwd{function} funcname \ter{(} \opt{parlist1} \ter{)} block \rwd{end} |
3894 | \OrNL \rwd{local} declist \opt{init} | 4084 | \OrNL \rwd{local} declist \opt{init} |
3895 | } | 4085 | } |
3896 | 4086 | ||
3897 | \produc{funcname}{% | 4087 | \produc{funcname}{\Nter{name} \rep{\ter{.} \Nter{name}} |
3898 | \Nter{name} | 4088 | \opt{\ter{:} \Nter{name}}} |
3899 | \Or \Nter{name} \ter{.} \Nter{name} | ||
3900 | \Or \Nter{name} \ter{:} \Nter{name} | ||
3901 | } | ||
3902 | 4089 | ||
3903 | \produc{varlist1}{var \rep{\ter{,} var}} | 4090 | \produc{varlist1}{var \rep{\ter{,} var}} |
3904 | 4091 | ||
3905 | \produc{var}{% | 4092 | \produc{var}{% |
3906 | \Nter{name} | 4093 | \Nter{name} |
3907 | \Or varorfunc \ter{[} exp1 \ter{]} | 4094 | \Or varorfunc \ter{[} exp \ter{]} |
3908 | \Or varorfunc \ter{.} \Nter{name} | 4095 | \Or varorfunc \ter{.} \Nter{name} |
3909 | } | 4096 | } |
3910 | 4097 | ||
@@ -3914,9 +4101,7 @@ The debug API has been completely rewritten. | |||
3914 | 4101 | ||
3915 | \produc{init}{\ter{=} explist1} | 4102 | \produc{init}{\ter{=} explist1} |
3916 | 4103 | ||
3917 | \produc{explist1}{\rep{exp1 \ter{,}} exp} | 4104 | \produc{explist1}{\rep{exp \ter{,}} exp} |
3918 | |||
3919 | \produc{exp1}{exp} | ||
3920 | 4105 | ||
3921 | \produc{exp}{% | 4106 | \produc{exp}{% |
3922 | \rwd{nil} | 4107 | \rwd{nil} |
@@ -3960,9 +4145,8 @@ The debug API has been completely rewritten. | |||
3960 | \Or lfieldlist \ter{;} ffieldlist | 4145 | \Or lfieldlist \ter{;} ffieldlist |
3961 | \Or ffieldlist \ter{;} lfieldlist | 4146 | \Or ffieldlist \ter{;} lfieldlist |
3962 | } | 4147 | } |
3963 | \produc{lfieldlist}{\opt{lfieldlist1}} | 4148 | \produc{lfieldlist}{\opt{explist1 \opt{\ter{,}}}} |
3964 | \produc{ffieldlist}{\opt{ffieldlist1}} | 4149 | \produc{ffieldlist}{\opt{ffieldlist1}} |
3965 | \produc{lfieldlist1}{exp \rep{\ter{,} exp} \opt{\ter{,}}} | ||
3966 | \produc{ffieldlist1}{ffield \rep{\ter{,} ffield} \opt{\ter{,}}} | 4150 | \produc{ffieldlist1}{ffield \rep{\ter{,} ffield} \opt{\ter{,}}} |
3967 | \produc{ffield}{% | 4151 | \produc{ffield}{% |
3968 | \ter{[} exp \ter{]} \ter{=} exp | 4152 | \ter{[} exp \ter{]} \ter{=} exp |