diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-02-05 12:52:47 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-02-05 12:52:47 -0200 |
| commit | 15057aa0a42477a04d718270c9bc3303b4e9b613 (patch) | |
| tree | 681b70790c669e2aac44a24a506c8a5c6593ec9b | |
| parent | 1431b52e769d1dc5f05d9eef10f7ad56bb6c44e6 (diff) | |
| download | lua-15057aa0a42477a04d718270c9bc3303b4e9b613.tar.gz lua-15057aa0a42477a04d718270c9bc3303b4e9b613.tar.bz2 lua-15057aa0a42477a04d718270c9bc3303b4e9b613.zip | |
new examples showing data structures and object-oriented programming.
generic improvements and corrections.
| -rw-r--r-- | manual.tex | 183 |
1 files changed, 145 insertions, 38 deletions
| @@ -1,4 +1,4 @@ | |||
| 1 | % $Id: manual.tex,v 1.3 1996/01/30 12:55:10 roberto Exp roberto $ | 1 | % $Id: manual.tex,v 1.4 1996/01/30 15:24:49 roberto Exp roberto $ |
| 2 | 2 | ||
| 3 | \documentstyle[A4,11pt,bnf]{article} | 3 | \documentstyle[A4,11pt,bnf]{article} |
| 4 | 4 | ||
| @@ -32,7 +32,7 @@ Waldemar Celes Filho | |||
| 32 | Departamento de Inform\'atica --- PUC-Rio | 32 | Departamento de Inform\'atica --- PUC-Rio |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | \date{\small \verb$Date: 1996/01/30 12:55:10 $} | 35 | \date{\small \verb$Date: 1996/01/30 15:24:49 $} |
| 36 | 36 | ||
| 37 | \maketitle | 37 | \maketitle |
| 38 | 38 | ||
| @@ -70,9 +70,9 @@ general procedural programming features with data description | |||
| 70 | facilities. | 70 | facilities. |
| 71 | It is supposed to be used as a configuration language for any | 71 | It is supposed to be used as a configuration language for any |
| 72 | program that needs one. | 72 | program that needs one. |
| 73 | Its main extensions are related to object-oriented facilities, | 73 | %Its main extensions are related to object-oriented facilities, |
| 74 | and fallbacks, | 74 | %and fallbacks, |
| 75 | but it has some other minor contributions. | 75 | %but it has some other minor contributions. |
| 76 | Lua has been designed and implemented by | 76 | Lua has been designed and implemented by |
| 77 | W.~Celes~F., L.~H.~de Figueiredo and R.~Ierusalimschy. | 77 | W.~Celes~F., L.~H.~de Figueiredo and R.~Ierusalimschy. |
| 78 | 78 | ||
| @@ -231,7 +231,7 @@ double hyphen (\verb'--') and run until the end of the line. | |||
| 231 | and an optional decimal exponent. | 231 | and an optional decimal exponent. |
| 232 | Examples of valid numerical constants are: | 232 | Examples of valid numerical constants are: |
| 233 | \begin{verbatim} | 233 | \begin{verbatim} |
| 234 | 4 4. .4 4.57e-3 .3e12 | 234 | 4 4.0 0.4 4.57e-3 0.3e12 |
| 235 | \end{verbatim} | 235 | \end{verbatim} |
| 236 | 236 | ||
| 237 | 237 | ||
| @@ -344,17 +344,14 @@ the syntax for a \Index{return statement} is: | |||
| 344 | \produc{ret}{\rwd{return} explist} | 344 | \produc{ret}{\rwd{return} explist} |
| 345 | \end{Produc} | 345 | \end{Produc} |
| 346 | 346 | ||
| 347 | \subsubsection{Expressions as Statements} \label{statexp} | 347 | \subsubsection{Function Calls as Statements} \label{funcstat} |
| 348 | All expressions with possible side-effects can be | 348 | Because of possible side-effects, |
| 349 | executed as statements. | 349 | function calls can be executed as statements. |
| 350 | These include function calls and table constructors: | ||
| 351 | \begin{Produc} | 350 | \begin{Produc} |
| 352 | \produc{stat}{functioncall} | 351 | \produc{stat}{functioncall} |
| 353 | \produc{stat}{tableconstructor} | ||
| 354 | \end{Produc}% | 352 | \end{Produc}% |
| 355 | Eventual returned values are thrown away. | 353 | Eventual returned values are thrown away. |
| 356 | Function calls are explained in Section \ref{functioncall}; | 354 | Function calls are explained in Section \ref{functioncall}. |
| 357 | constructors are the subject of Section \ref{tableconstructor}. | ||
| 358 | 355 | ||
| 359 | \subsubsection{Local Declarations} \label{localvar} | 356 | \subsubsection{Local Declarations} \label{localvar} |
| 360 | \Index{Local variables} can be declared anywhere inside a block. | 357 | \Index{Local variables} can be declared anywhere inside a block. |
| @@ -405,7 +402,8 @@ Lua offers the following \Index{relational operators}: | |||
| 405 | \begin{verbatim} | 402 | \begin{verbatim} |
| 406 | < > <= >= ~= == | 403 | < > <= >= ~= == |
| 407 | \end{verbatim} | 404 | \end{verbatim} |
| 408 | All return \nil\ as false and 1 as true. | 405 | All return \nil\ as false and a value different from \nil\ |
| 406 | (actually the number 1) as true. | ||
| 409 | 407 | ||
| 410 | Equality first compares the types of its operands. | 408 | Equality first compares the types of its operands. |
| 411 | If they are different, the result is \nil. | 409 | If they are different, the result is \nil. |
| @@ -546,7 +544,7 @@ the parameter list is a single new table. | |||
| 546 | Because a function can return any number of results | 544 | Because a function can return any number of results |
| 547 | (\see{return}), | 545 | (\see{return}), |
| 548 | the number of results must be adjusted before used. | 546 | the number of results must be adjusted before used. |
| 549 | If the function is called as an statement (\see{statexp}), | 547 | If the function is called as a statement (\see{funcstat}), |
| 550 | its return list is adjusted to 0. | 548 | its return list is adjusted to 0. |
| 551 | If the function is called in a place that needs a single value | 549 | If the function is called in a place that needs a single value |
| 552 | (syntactically denoted by the non-terminal \verb'exp1'), | 550 | (syntactically denoted by the non-terminal \verb'exp1'), |
| @@ -601,7 +599,7 @@ end | |||
| 601 | \end{verbatim} | 599 | \end{verbatim} |
| 602 | that is, the function gets an extra formal parameter called \verb'self'. | 600 | that is, the function gets an extra formal parameter called \verb'self'. |
| 603 | Notice that | 601 | Notice that |
| 604 | the variable \verb'v' must be previously initialized with a table value. | 602 | the variable \verb'v' must have been previously initialized with a table value. |
| 605 | 603 | ||
| 606 | 604 | ||
| 607 | \subsection{Fallbacks} \label{fallback} | 605 | \subsection{Fallbacks} \label{fallback} |
| @@ -704,18 +702,17 @@ and then the corresponding function from the library | |||
| 704 | is terminated returning an error condition. | 702 | is terminated returning an error condition. |
| 705 | 703 | ||
| 706 | The only argument to the error fallback function is a string describing | 704 | The only argument to the error fallback function is a string describing |
| 707 | the error and some extra informations, | 705 | the error. |
| 708 | like current line (when the error is at compilation) | 706 | The standard I/O library redefines this fallback, |
| 709 | or current function (when the error is at execution). | 707 | using the debug API, in order to print some extra informations, |
| 708 | like the stack of calls. | ||
| 710 | For more information about an error, | 709 | For more information about an error, |
| 711 | the Lua program can include the compilation pragma \verb'$debug'. | 710 | the Lua program can include the compilation pragma \verb'$debug'. |
| 712 | \index{debug pragma} | 711 | \index{debug pragma} |
| 713 | This pragma must be written in a line by itself. | 712 | This pragma must be written in a line by itself. |
| 714 | When an error occurs in a program compiled with this option, | 713 | When an error occurs in a program compiled with this option, |
| 715 | the error message includes extra information showing the stack of calls. | 714 | the error routine is able to print also the lines where the calls |
| 716 | 715 | (and the error) were made. | |
| 717 | The standard error routine only prints the error message | ||
| 718 | to \verb'stderr'. | ||
| 719 | If needed, it is possible to change the error fallback routine; | 716 | If needed, it is possible to change the error fallback routine; |
| 720 | \see{fallback}. | 717 | \see{fallback}. |
| 721 | 718 | ||
| @@ -758,6 +755,9 @@ Because Lua has no static type system, | |||
| 758 | all values passed between Lua and C have type | 755 | all values passed between Lua and C have type |
| 759 | \verb'lua_Object'\Deffunc{lua_Object}, | 756 | \verb'lua_Object'\Deffunc{lua_Object}, |
| 760 | which works like an abstract type in C that can hold any Lua value. | 757 | which works like an abstract type in C that can hold any Lua value. |
| 758 | Values of type \verb'lua_Object' have no meaning outside Lua; | ||
| 759 | for instance, | ||
| 760 | the comparisson of two \verb"lua_Object's" is of no significance. | ||
| 761 | 761 | ||
| 762 | Lua has automatic memory management, and garbage collection. | 762 | Lua has automatic memory management, and garbage collection. |
| 763 | Because of that, a \verb'lua_Object' has a limited scope, | 763 | Because of that, a \verb'lua_Object' has a limited scope, |
| @@ -856,7 +856,7 @@ and leave the result on the top of the Lua stack, | |||
| 856 | where it can be assigned to a Lua variable, | 856 | where it can be assigned to a Lua variable, |
| 857 | passed as paramenter to a Lua function, etc (see below). \label{pushing} | 857 | passed as paramenter to a Lua function, etc (see below). \label{pushing} |
| 858 | \verb'lua_pushliteral' is like \verb'lua_pushstring', | 858 | \verb'lua_pushliteral' is like \verb'lua_pushstring', |
| 859 | but also puts the string in the Lua literal table. | 859 | but also puts the string in the Lua literal table and merges duplications. |
| 860 | This avoids the string to be garbage collected, | 860 | This avoids the string to be garbage collected, |
| 861 | and therefore has a better overall performance. | 861 | and therefore has a better overall performance. |
| 862 | As a rule, when the string to be pushed is a literal, | 862 | As a rule, when the string to be pushed is a literal, |
| @@ -885,6 +885,9 @@ one can use the function: | |||
| 885 | \begin{verbatim} | 885 | \begin{verbatim} |
| 886 | lua_Object lua_getglobal (char *varname); | 886 | lua_Object lua_getglobal (char *varname); |
| 887 | \end{verbatim} | 887 | \end{verbatim} |
| 888 | As in Lua, if the value of the global is \nil, | ||
| 889 | the \verb'"getglobal"' fallback is called. | ||
| 890 | |||
| 888 | To store a value previously pushed onto the stack in a global variable, | 891 | To store a value previously pushed onto the stack in a global variable, |
| 889 | there is the function: | 892 | there is the function: |
| 890 | \Deffunc{lua_storeglobal} | 893 | \Deffunc{lua_storeglobal} |
| @@ -904,15 +907,15 @@ As in Lua, if the first object is not a table, | |||
| 904 | or the index is not present in the table, | 907 | or the index is not present in the table, |
| 905 | the correspondent fallback is called. | 908 | the correspondent fallback is called. |
| 906 | 909 | ||
| 907 | For compatibility with previous versions of the API, | 910 | %For compatibility with previous versions of the API, |
| 908 | the following macros are supported: | 911 | %the following macros are supported: |
| 909 | \Deffunc{lua_getindexed}\Deffunc{lua_getfield} | 912 | %\Deffunc{lua_getindexed}\Deffunc{lua_getfield} |
| 910 | \begin{verbatim} | 913 | %\begin{verbatim} |
| 911 | lua_Object lua_getindexed (lua_Object table, float index); | 914 | %lua_Object lua_getindexed (lua_Object table, float index); |
| 912 | lua_Object lua_getfield (lua_Object table, char *field); | 915 | %lua_Object lua_getfield (lua_Object table, char *field); |
| 913 | \end{verbatim} | 916 | %\end{verbatim} |
| 914 | The first one is used for numeric indices, | 917 | %The first one is used for numeric indices, |
| 915 | while the second can be used for any string index. | 918 | %while the second can be used for any string index. |
| 916 | 919 | ||
| 917 | To store a value in an index, | 920 | To store a value in an index, |
| 918 | the program must push onto the stack the table, the index, | 921 | the program must push onto the stack the table, the index, |
| @@ -1009,7 +1012,7 @@ The first parameter is the fallback name, | |||
| 1009 | and the second a CFunction to be used as the new fallback. | 1012 | and the second a CFunction to be used as the new fallback. |
| 1010 | This function returns a \verb'lua_Object', | 1013 | This function returns a \verb'lua_Object', |
| 1011 | which is the old fallback value, | 1014 | which is the old fallback value, |
| 1012 | or nil on fail (invalid fallback name). | 1015 | or \nil\ on fail (invalid fallback name). |
| 1013 | This old value can be used for chaining fallbacks. | 1016 | This old value can be used for chaining fallbacks. |
| 1014 | 1017 | ||
| 1015 | An example of C code calling a Lua function is shown in | 1018 | An example of C code calling a Lua function is shown in |
| @@ -1331,8 +1334,8 @@ then a \Index{piped input} is open, via function \IndexVerb{popen}. | |||
| 1331 | 1334 | ||
| 1332 | This function opens a file named \verb'filename' and sets it as the | 1335 | This function opens a file named \verb'filename' and sets it as the |
| 1333 | {\em current} output file. | 1336 | {\em current} output file. |
| 1334 | Notice that, if the file already exists, it is completely erased with this | 1337 | Notice that, if the file already exists, |
| 1335 | operation. | 1338 | it will be {\em completely erased} with this operation. |
| 1336 | When called without parameters, | 1339 | When called without parameters, |
| 1337 | this function closes the current output file, | 1340 | this function closes the current output file, |
| 1338 | and restores \verb'stdout' as the current output file. | 1341 | and restores \verb'stdout' as the current output file. |
| @@ -1382,7 +1385,7 @@ Particularly, the format \verb'"s1"' reads a single character. | |||
| 1382 | \subsubsection*{{\tt readuntil (char)}}\Deffunc{readuntil} | 1385 | \subsubsection*{{\tt readuntil (char)}}\Deffunc{readuntil} |
| 1383 | 1386 | ||
| 1384 | Reads the current input until the first ocurrence of the given character. | 1387 | Reads the current input until the first ocurrence of the given character. |
| 1385 | When called with no parameters or with nil, | 1388 | When called with no parameters, |
| 1386 | reads until the end of the current input file. | 1389 | reads until the end of the current input file. |
| 1387 | Returns the string read. | 1390 | Returns the string read. |
| 1388 | The character itself is not read. | 1391 | The character itself is not read. |
| @@ -1453,6 +1456,49 @@ It does not intend to cover the whole language, | |||
| 1453 | but only to illustrate some interesting uses of the system. | 1456 | but only to illustrate some interesting uses of the system. |
| 1454 | 1457 | ||
| 1455 | 1458 | ||
| 1459 | \subsection{\Index{Data Structures}} | ||
| 1460 | Tables are a strong unifying data constructor. | ||
| 1461 | They directly implement a multitude of data types, | ||
| 1462 | like ordinary arrays, records, sets, bags, and lists. | ||
| 1463 | |||
| 1464 | Arrays need no explanations. | ||
| 1465 | In Lua, it is conventional to start indices from 1, | ||
| 1466 | but this is only a convention. | ||
| 1467 | Arrays can be indexed by 0, negative numbers, or any other value (but \nil). | ||
| 1468 | Records are also trivially implemented by the syntactic sugar | ||
| 1469 | \verb'a.x'. | ||
| 1470 | |||
| 1471 | The best way to implement a set is to use the indices of a table. | ||
| 1472 | The statement \verb's = {}' creates an empty set \verb's'. | ||
| 1473 | The statement \verb's[x] = 1' inserts the value of \verb'x' into | ||
| 1474 | the set \verb's'. | ||
| 1475 | The expression \verb's[x]' is true if and only if | ||
| 1476 | \verb'x' belongs to \verb's'. | ||
| 1477 | Finally, the statement \verb's[x] = nil' erases \verb'x' from \verb's'. | ||
| 1478 | |||
| 1479 | Bags can be implemented similarly to sets, | ||
| 1480 | but using the value associated to an element as its counter. | ||
| 1481 | So, to insert an element, | ||
| 1482 | the following code is enough: | ||
| 1483 | \begin{verbatim} | ||
| 1484 | if s[x] then s[x] = s[x]+1 | ||
| 1485 | else s[x] = 1 end | ||
| 1486 | \end{verbatim} | ||
| 1487 | and to remove an element: | ||
| 1488 | \begin{verbatim} | ||
| 1489 | if s[x] then s[x] = s[x]-1 end | ||
| 1490 | if s[x] == 0 then s[x] = nil end | ||
| 1491 | \end{verbatim} | ||
| 1492 | |||
| 1493 | Lisp-like lists also have an easy implementation. | ||
| 1494 | The ``cons'' of two elements \verb'x' and \verb'y' can be | ||
| 1495 | created with the code \verb'l = {car=x, cdr=y}'. | ||
| 1496 | The expression \verb'l.car' extracts the header, | ||
| 1497 | while \verb'l.cdr' extracts the tail. | ||
| 1498 | An alternative way is to create the list directly with \verb'l={x,y}', | ||
| 1499 | and then to extract the header with \verb'l[1]' and | ||
| 1500 | the tail with \verb'l[2]'. | ||
| 1501 | |||
| 1456 | \subsection{The Functions {\tt next} and {\tt nextvar}} \label{exnext} | 1502 | \subsection{The Functions {\tt next} and {\tt nextvar}} \label{exnext} |
| 1457 | \Deffunc{next}\Deffunc{nextvar} | 1503 | \Deffunc{next}\Deffunc{nextvar} |
| 1458 | This example shows how to use the function \verb'next' to iterate | 1504 | This example shows how to use the function \verb'next' to iterate |
| @@ -1654,6 +1700,64 @@ This code must be registered with: | |||
| 1654 | Notice how the string \verb'"parent"' is kept | 1700 | Notice how the string \verb'"parent"' is kept |
| 1655 | locked in Lua for optimal performance. | 1701 | locked in Lua for optimal performance. |
| 1656 | 1702 | ||
| 1703 | \subsection{\Index{Programming with Classes}} | ||
| 1704 | There are many different ways to do object-oriented programming in Lua. | ||
| 1705 | This section presents one possible way to | ||
| 1706 | implement classes, | ||
| 1707 | using the inheritance mechanism presented above. | ||
| 1708 | |||
| 1709 | As one could expect, a good way to represent a class is | ||
| 1710 | as a table. | ||
| 1711 | This table will contain all instance methods of the class, | ||
| 1712 | plus eventual default values for instance variables. | ||
| 1713 | An instance of a class has its \verb'parent' field pointing to | ||
| 1714 | the class, | ||
| 1715 | and so it ``inherits'' all methods. | ||
| 1716 | |||
| 1717 | For instance, a class \verb'Point' can be described as in | ||
| 1718 | Figure~\ref{Point}. | ||
| 1719 | Function \verb'create' helps the creation of new points, | ||
| 1720 | adding the parent field. | ||
| 1721 | Function \verb'move' is an example of an instance method. | ||
| 1722 | \begin{figure} | ||
| 1723 | \Line | ||
| 1724 | \begin{verbatim} | ||
| 1725 | Point = {x = 0, y = 0} | ||
| 1726 | |||
| 1727 | function Point:create (o) | ||
| 1728 | o.parent = self | ||
| 1729 | return o | ||
| 1730 | end | ||
| 1731 | |||
| 1732 | function Point:move (p) | ||
| 1733 | self.x = self.x + p.x | ||
| 1734 | self.y = self.y + p.y | ||
| 1735 | end | ||
| 1736 | |||
| 1737 | ... | ||
| 1738 | |||
| 1739 | -- | ||
| 1740 | -- creating points | ||
| 1741 | -- | ||
| 1742 | p1 = Point:create{x = 10, y = 20} | ||
| 1743 | p2 = Point:create{x = 10} -- y will be inherited until it is set | ||
| 1744 | |||
| 1745 | -- | ||
| 1746 | -- example of a method invocation | ||
| 1747 | -- | ||
| 1748 | p1:move(p2) | ||
| 1749 | \end{verbatim} | ||
| 1750 | \caption{A Class Point.\label{Point}} | ||
| 1751 | \Line | ||
| 1752 | \end{figure} | ||
| 1753 | Finally, a subclass can be created as a new table, | ||
| 1754 | with the \verb'parent' field pointing to its superclass. | ||
| 1755 | It is interesting to notice how the use of \verb'self' in | ||
| 1756 | method \verb'create' allows this method to work properly even | ||
| 1757 | when inherited by a subclass. | ||
| 1758 | As usual, a subclass may overwrite any inherited method with | ||
| 1759 | its own version. | ||
| 1760 | |||
| 1657 | \subsection{\Index{Modules}} | 1761 | \subsection{\Index{Modules}} |
| 1658 | Here we explain one possible way to simulate modules in Lua. | 1762 | Here we explain one possible way to simulate modules in Lua. |
| 1659 | The main idea is to use a table to store the module functions. | 1763 | The main idea is to use a table to store the module functions. |
| @@ -1673,7 +1777,10 @@ end | |||
| 1673 | Any code that needs this module has only to execute | 1777 | Any code that needs this module has only to execute |
| 1674 | \verb'dofile("filename")', where \verb'filename' is the file | 1778 | \verb'dofile("filename")', where \verb'filename' is the file |
| 1675 | where the module is written. | 1779 | where the module is written. |
| 1676 | After this, any function can be called with \verb'modulename.foo(...)'. | 1780 | After this, any function can be called with |
| 1781 | \begin{verbatim} | ||
| 1782 | modulename.foo(...) | ||
| 1783 | \end{verbatim} | ||
| 1677 | 1784 | ||
| 1678 | If a module function is going to be used many times, | 1785 | If a module function is going to be used many times, |
| 1679 | the program can give a local name to it. | 1786 | the program can give a local name to it. |
