aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-01-30 13:24:49 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-01-30 13:24:49 -0200
commitabfebf1e2110eb661e146f01ea82836727b3e794 (patch)
tree749fd699946bbec230027bfe6218e95f5ab1e6b9
parentb1c02c7f00eeffbb16a87f95ab8ef83116b97bad (diff)
downloadlua-abfebf1e2110eb661e146f01ea82836727b3e794.tar.gz
lua-abfebf1e2110eb661e146f01ea82836727b3e794.tar.bz2
lua-abfebf1e2110eb661e146f01ea82836727b3e794.zip
new example showing how to build modules.
-rw-r--r--manual.tex55
1 files changed, 50 insertions, 5 deletions
diff --git a/manual.tex b/manual.tex
index ceb1e0bb..d0556c02 100644
--- a/manual.tex
+++ b/manual.tex
@@ -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
32Departamento de Inform\'atica --- PUC-Rio 32Departamento 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
105All statements in Lua are executed in a \Def{global environment}. 105All statements in Lua are executed in a \Def{global environment}.
106This environment, which keeps all global variables and functions, 106This 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
561Functions in Lua can be defined anywhere in the global level of a module. 561Functions in Lua can be defined anywhere in the global level of a chunk.
562The syntax for function definition is: 562The 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.
1460Function \Def{clone} receives any table and returns a clone of it. 1460Function \Def{clone} receives any table and returns a clone of it.
1461\begin{verbatim} 1461\begin{verbatim}
1462function clone (t) -- t is a table 1462function 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:
1654Notice how the string \verb'"parent"' is kept 1654Notice how the string \verb'"parent"' is kept
1655locked in Lua for optimal performance. 1655locked in Lua for optimal performance.
1656 1656
1657\subsection{\Index{Modules}}
1658Here we explain one possible way to simulate modules in Lua.
1659The main idea is to use a table to store the module functions.
1660
1661A module should be written as a separate chunk, starting with:
1662\begin{verbatim}
1663if modulename then return end -- avoid loading twice the same module
1664modulename = {} -- create a table to represent the module
1665\end{verbatim}
1666After that, functions can be directly defined with the syntax
1667\begin{verbatim}
1668function modulename.foo (...)
1669 ...
1670end
1671\end{verbatim}
1672
1673Any code that needs this module has only to execute
1674\verb'dofile("filename")', where \verb'filename' is the file
1675where the module is written.
1676After this, any function can be called with \verb'modulename.foo(...)'.
1677
1678If a module function is going to be used many times,
1679the program can give a local name to it.
1680Because functions are values, it is enough to write
1681\begin{verbatim}
1682localname = modulename.foo
1683\end{verbatim}
1684Finally, a module may be {\em opened},
1685giving direct access to all its functions,
1686as shown in the code in Figure~\ref{openmod}.
1687\begin{figure}
1688\Line
1689\begin{verbatim}
1690function 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
1696end
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}
1658A CFunction to compute the maximum of a variable number of arguments 1703A CFunction to compute the maximum of a variable number of arguments
1659is shown in Figure~\ref{Cmax}. 1704is shown in Figure~\ref{Cmax}.