diff options
| -rw-r--r-- | manual.tex | 55 |
1 files changed, 50 insertions, 5 deletions
| @@ -1,4 +1,4 @@ | |||
| 1 | % $Id: manual.tex,v 1.2 1996/01/29 17:08:23 roberto Exp roberto $ | 1 | % $Id: manual.tex,v 1.3 1996/01/30 12:55:10 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/29 17:08:23 $} | 35 | \date{\small \verb$Date: 1996/01/30 12:55:10 $} |
| 36 | 36 | ||
| 37 | \maketitle | 37 | \maketitle |
| 38 | 38 | ||
| @@ -100,7 +100,7 @@ or by WWW (World Wide Web) from | |||
| 100 | \end{verbatim} | 100 | \end{verbatim} |
| 101 | 101 | ||
| 102 | 102 | ||
| 103 | \section{Environment and Modules} | 103 | \section{Environment and Chunks} |
| 104 | 104 | ||
| 105 | All statements in Lua are executed in a \Def{global environment}. | 105 | All statements in Lua are executed in a \Def{global environment}. |
| 106 | This environment, which keeps all global variables and functions, | 106 | This environment, which keeps all global variables and functions, |
| @@ -558,7 +558,7 @@ no adjustment is done. | |||
| 558 | 558 | ||
| 559 | \subsection{\Index{Function Definitions}} | 559 | \subsection{\Index{Function Definitions}} |
| 560 | 560 | ||
| 561 | Functions in Lua can be defined anywhere in the global level of a module. | 561 | Functions in Lua can be defined anywhere in the global level of a chunk. |
| 562 | The syntax for function definition is: | 562 | The syntax for function definition is: |
| 563 | \begin{Produc} | 563 | \begin{Produc} |
| 564 | \produc{function}{\rwd{function} var \ter{(} \opt{parlist1} \ter{)} | 564 | \produc{function}{\rwd{function} var \ter{(} \opt{parlist1} \ter{)} |
| @@ -1460,7 +1460,7 @@ over the fields of a table. | |||
| 1460 | Function \Def{clone} receives any table and returns a clone of it. | 1460 | Function \Def{clone} receives any table and returns a clone of it. |
| 1461 | \begin{verbatim} | 1461 | \begin{verbatim} |
| 1462 | function clone (t) -- t is a table | 1462 | function clone (t) -- t is a table |
| 1463 | local new_t = {} -- creates a new table | 1463 | local new_t = {} -- create a new table |
| 1464 | local i, v = next(t, nil) -- i is an index of t, v = t[i] | 1464 | local i, v = next(t, nil) -- i is an index of t, v = t[i] |
| 1465 | while i do | 1465 | while i do |
| 1466 | new_t[i] = v | 1466 | new_t[i] = v |
| @@ -1654,6 +1654,51 @@ This code must be registered with: | |||
| 1654 | Notice how the string \verb'"parent"' is kept | 1654 | Notice how the string \verb'"parent"' is kept |
| 1655 | locked in Lua for optimal performance. | 1655 | locked in Lua for optimal performance. |
| 1656 | 1656 | ||
| 1657 | \subsection{\Index{Modules}} | ||
| 1658 | 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. | ||
| 1660 | |||
| 1661 | A module should be written as a separate chunk, starting with: | ||
| 1662 | \begin{verbatim} | ||
| 1663 | if modulename then return end -- avoid loading twice the same module | ||
| 1664 | modulename = {} -- create a table to represent the module | ||
| 1665 | \end{verbatim} | ||
| 1666 | After that, functions can be directly defined with the syntax | ||
| 1667 | \begin{verbatim} | ||
| 1668 | function modulename.foo (...) | ||
| 1669 | ... | ||
| 1670 | end | ||
| 1671 | \end{verbatim} | ||
| 1672 | |||
| 1673 | Any code that needs this module has only to execute | ||
| 1674 | \verb'dofile("filename")', where \verb'filename' is the file | ||
| 1675 | where the module is written. | ||
| 1676 | After this, any function can be called with \verb'modulename.foo(...)'. | ||
| 1677 | |||
| 1678 | If a module function is going to be used many times, | ||
| 1679 | the program can give a local name to it. | ||
| 1680 | Because functions are values, it is enough to write | ||
| 1681 | \begin{verbatim} | ||
| 1682 | localname = modulename.foo | ||
| 1683 | \end{verbatim} | ||
| 1684 | Finally, a module may be {\em opened}, | ||
| 1685 | giving direct access to all its functions, | ||
| 1686 | as shown in the code in Figure~\ref{openmod}. | ||
| 1687 | \begin{figure} | ||
| 1688 | \Line | ||
| 1689 | \begin{verbatim} | ||
| 1690 | function open (mod) | ||
| 1691 | local n, f = next(mod, nil) | ||
| 1692 | while n do | ||
| 1693 | setglobal(n, f) | ||
| 1694 | n, f = next(mod, n) | ||
| 1695 | end | ||
| 1696 | end | ||
| 1697 | \end{verbatim} | ||
| 1698 | \caption{Opening a module.\label{openmod}} | ||
| 1699 | \Line | ||
| 1700 | \end{figure} | ||
| 1701 | |||
| 1657 | \subsection{A CFunction} \label{exCFunction}\index{functions in C} | 1702 | \subsection{A CFunction} \label{exCFunction}\index{functions in C} |
| 1658 | A CFunction to compute the maximum of a variable number of arguments | 1703 | A CFunction to compute the maximum of a variable number of arguments |
| 1659 | is shown in Figure~\ref{Cmax}. | 1704 | is shown in Figure~\ref{Cmax}. |
