From 8fdd06ba3c92c7ca7fbf25748bfa32635e4c003a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 5 Feb 1999 10:15:07 -0200 Subject: [PATCH] lots of new stuff from 3.2 --- manual.tex | 207 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 138 insertions(+), 69 deletions(-) diff --git a/manual.tex b/manual.tex index 8e6e4786..20420570 100644 --- a/manual.tex +++ b/manual.tex @@ -1,4 +1,4 @@ -% $Id: manual.tex,v 1.20 1998/11/13 16:48:48 roberto Exp roberto $ +% $Id: manual.tex,v 1.21 1998/11/20 15:41:43 roberto Exp roberto $ \documentclass[11pt]{article} \usepackage{fullpage,bnf} @@ -41,7 +41,7 @@ Waldemar Celes \tecgraf\ --- Computer Science Department --- PUC-Rio } -%\date{\small \verb$Date: 1998/11/13 16:48:48 $} +%\date{\small \verb$Date: 1998/11/20 15:41:43 $} \maketitle @@ -410,11 +410,7 @@ Lua provides some automatic conversions between values at run time. Any arithmetic operation applied to a string tries to convert that string to a number, following the usual rules. Conversely, whenever a number is used when a string is expected, -that number is converted to a string, according to the following rule: -if the number is an integer, it is written without exponent or decimal point; -otherwise, it is formatted following the \verb|%g| -conversion specification of the \verb|printf| function in the -standard C library. +that number is converted to a string, in a reasonable format. For complete control on how numbers are converted to strings, use the \verb|format| function \see{format}. @@ -523,9 +519,9 @@ only \nil\ is considered false. \begin{Produc} \produc{stat}{\rwd{while} exp1 \rwd{do} block \rwd{end} \OrNL \rwd{repeat} block \rwd{until} exp1 \OrNL -\rwd{if} exp1 \rwd{then} block \rep{elseif} +\rwd{if} exp1 \rwd{then} block + \rep{\rwd{elseif} exp1 \rwd{then} block} \opt{\rwd{else} block} \rwd{end}} -\produc{elseif}{\rwd{elseif} exp1 \rwd{then} block} \end{Produc} A \T{return} is used to return values from a function or from a chunk. @@ -688,6 +684,24 @@ All binary operators are left associative, except for \verb|^| (exponentiation), which is right associative. +\subsubsection{If Expressions} +\begin{Produc} +\produc{exp}{\rwd{if} exp1 \rwd{then} exp1 + \rep{\rwd{elseif} exp1 \rwd{then} exp1} + \opt{\rwd{else} exp1} \rwd{end}} +\end{Produc}% +An \Index{if expression} chooses an expression to evaluate +according to its condition. +Its final value is the value of the chosen expression. +An absent else-part is equivalent to \verb|else nil|. + +\subsubsection{Assignment Expressions} +\begin{Produc} +\produc{exp}{\ter{(} var \ter{=} exp1 \ter{)}} +\end{Produc}% +An \Index{assignment expression} executes a regular assignment, +and results in the final value of its right hand expression. + \subsubsection{Table Constructors} \label{tableconstructor} Table \Index{constructors} are expressions that create tables; every time a constructor is evaluated, a new table is created. @@ -1978,8 +1992,9 @@ field not present in a table or a field with value \nil. Therefore, the function only considers fields with non \nil\ values. The order in which the indices are enumerated is not specified, \emph{even for numeric indices} -(to traverse a table in numeric order, use a counter). -If the table is modified in any way during a traversal, +(to traverse a table in numeric order, +use a counter or function \verb|foreachi|). +If the table indices are modified in any way during a traversal, the semantics of \verb|next| is undefined. This function cannot be written with the standard API. @@ -1992,7 +2007,7 @@ or \nil\ to get a first name. Similarly to \verb|next|, it returns the name of another variable and its value, or \nil\ if there are no more variables. -There can be no assignments to global variables during the traversal; +There can be no creation of new global variables during the traversal; otherwise the semantics of \verb|nextvar| is undefined. This function cannot be written with the standard API. @@ -2150,14 +2165,13 @@ value in the table. This function could be defined in Lua: \begin{verbatim} function getn (t) - if type(t.n) == 'number' then return floor(t.n) end - local i = next(t, nil) + if type(t.n) == 'number' then return t.n end + local i = nil local max = 0 - while i do + while (i=next(t, i)) do if type(i) == 'number' and i>max then max=i end - i = next(t, i) end - return floor(max) + return max end \end{verbatim} @@ -2198,11 +2212,10 @@ as the final value of \verb|foreachi|. This function could be defined in Lua: \begin{verbatim} function foreachi (t, f) - local i, n = 1, getn(t) - while i<=n do + local i, n = 0, getn(t) + while (i=i+1)<=n do local res = f(i, t[i]) if res then return res end - i = i+1 end end \end{verbatim} @@ -2227,50 +2240,80 @@ This function could be defined in Lua: end \end{verbatim} +\subsubsection*{\ff \T{tinsert (table [, pos] , value)}}\Deffunc{tinsert} + +Inserts element \verb|value| at table position \verb|pos|, +shifting other elements to open space. +The default value for \verb|pos| is \verb|n+1| +(where \verb|n| is the result of \verb|getn(table)| \see{getn}) +so that a call \verb|tinsert(t,x)| inserts \verb|x| at the end +of table \verb|t|. + +This function also sets or increments the field \verb|n| of the table, +to \verb|n+1|. + +This function is equivalent to the following Lua function, +except that the table accesses are all raw (that is, without tag methods): +\begin{verbatim} + function tinsert (t, ...) + local pos, value + local n = getn(t) + if arg.n == 1 then + pos = n+1; value = arg[1] + else + pos = arg[1]; value = arg[2] + end + t.n = n+1; n=n+1 + while (n=n-1)>=pos do + t[n+1] = t[n] + end + t[pos] = value + end +\end{verbatim} + +\subsubsection*{\ff \T{tremove (table [, pos])}}\Deffunc{tremove} + +Removes from \verb|table| the element at position \verb|pos|, +shifting other elements to close the space. +Returns the value of the removed element. +The default value for \verb|pos| is \verb|n| +(where \verb|n| is the result of \verb|getn(table)| \see{getn}), +so that a call \verb|tremove(t)| removes the last element +of table \verb|t|. + +This function also sets or decrements the field \verb|n| of the table, +to \verb|n-1|. + +This function is equivalent to the following Lua function, +except that the table accesses are all raw (that is, without tag methods): +\begin{verbatim} + function tremove (t, pos) + local n = getn(t) + pos = pos or n + local value = t[pos] + if n<=0 then return end + t.n = n-1 + pos = pos-1 + while (pos=pos+1)