aboutsummaryrefslogtreecommitdiff
path: root/manual
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-07-03 14:18:07 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-07-03 14:18:07 -0300
commit4d46289331395a845c5de1f6c0e0fe873c50db4f (patch)
tree96649174d3c13830549ad026af0133e0b789d411 /manual
parent8eca21c2e85625390a2a3b08c231e75e315980b0 (diff)
downloadlua-4d46289331395a845c5de1f6c0e0fe873c50db4f.tar.gz
lua-4d46289331395a845c5de1f6c0e0fe873c50db4f.tar.bz2
lua-4d46289331395a845c5de1f6c0e0fe873c50db4f.zip
Local attributes can be used in list of local variables
The syntax for local attributes ('const'/'toclose') was unified with the regular syntax for local variables, so that we can have variables with attributes in local definitions with multiple names; for instance: local <toclose> f, <const> err = io.open(fname) This new syntax does not implement constant propagation, yet. This commit also has some small improvements to the manual.
Diffstat (limited to 'manual')
-rw-r--r--manual/manual.of60
1 files changed, 34 insertions, 26 deletions
diff --git a/manual/manual.of b/manual/manual.of
index e9416956..136e9022 100644
--- a/manual/manual.of
+++ b/manual/manual.of
@@ -1399,23 +1399,30 @@ they must all result in numbers.
1399Their values are called respectively 1399Their values are called respectively
1400the @emph{initial value}, the @emph{limit}, and the @emph{step}. 1400the @emph{initial value}, the @emph{limit}, and the @emph{step}.
1401If the step is absent, it defaults @N{to 1}. 1401If the step is absent, it defaults @N{to 1}.
1402Then the loop body is repeated with the value of the control variable 1402
1403If both the initial value and the step are integers,
1404the loop is done with integers;
1405note that the limit may not be an integer.
1406Otherwise, the loop is done with floats.
1407(Beware of floating-point accuracy in this case.)
1408
1409After that initialization,
1410the loop body is repeated with the value of the control variable
1403going through an arithmetic progression, 1411going through an arithmetic progression,
1404starting at the initial value, 1412starting at the initial value,
1405with a common difference given by the step, 1413with a common difference given by the step.
1406until that value passes the limit.
1407A negative step makes a decreasing sequence; 1414A negative step makes a decreasing sequence;
1408a step equal to zero raises an error. 1415a step equal to zero raises an error.
1416The loop continues while the value is less than
1417or equal to the limit
1418(greater than or equal to for a negative step).
1409If the initial value is already greater than the limit 1419If the initial value is already greater than the limit
1410(or less than, if the step is negative), 1420(or less than, if the step is negative),
1411the body is not executed. 1421the body is not executed.
1412 1422
1413If both the initial value and the step are integers, 1423For integer loops,
1414the loop is done with integers; 1424the control variable never wraps around;
1415in this case, the range of the control variable is clipped 1425instead, the loop ends in case of an overflow.
1416by the range of integers.
1417Otherwise, the loop is done with floats.
1418(Beware of floating-point accuracy in this case.)
1419 1426
1420You should not change the value of the control variable 1427You should not change the value of the control variable
1421during the loop. 1428during the loop.
@@ -1490,22 +1497,25 @@ Function calls are explained in @See{functioncall}.
1490@x{Local variables} can be declared anywhere inside a block. 1497@x{Local variables} can be declared anywhere inside a block.
1491The declaration can include an initialization: 1498The declaration can include an initialization:
1492@Produc{ 1499@Produc{
1493@producname{stat}@producbody{@Rw{local} namelist @bnfopt{@bnfter{=} explist}} 1500@producname{stat}@producbody{@Rw{local} attnamelist @bnfopt{@bnfter{=} explist}}
1494@producname{stat}@producbody{ 1501@producname{attnamelist}@producbody{
1495 @Rw{local} @bnfter{<} Name @bnfter{>} Name @bnfter{=} exp 1502 attrib @bnfNter{Name} @bnfrep{@bnfter{,} attrib @bnfNter{Name}}}
1496}} 1503}
1497If present, an initial assignment has the same semantics 1504If present, an initial assignment has the same semantics
1498of a multiple assignment @see{assignment}. 1505of a multiple assignment @see{assignment}.
1499Otherwise, all variables are initialized with @nil. 1506Otherwise, all variables are initialized with @nil.
1500The second syntax declares a local with a given attribute, 1507
1501which is the name between the angle brackets. 1508Each variable name may be preceded by an attribute
1502In this case, there must be an initialization. 1509(a name between angle brackets):
1510@Produc{
1511@producname{attrib}@producbody{@bnfopt{@bnfter{<} @bnfNter{Name} @bnfter{>}}}
1512}
1503There are two possible attributes: 1513There are two possible attributes:
1504@id{const}, which declares a @x{constant variable}, 1514@id{const}, which declares a @x{constant variable},
1505that is, a variable that cannot be assigned to 1515that is, a variable that cannot be assigned to
1506after its initialization; 1516after its initialization;
1507and @id{toclose}, which declares a to-be-closed variable @see{to-be-closed}. 1517and @id{toclose}, which declares a to-be-closed variable @see{to-be-closed}.
1508 1518A list of variables can contain at most one to-be-closed variable.
1509 1519
1510A chunk is also a block @see{chunks}, 1520A chunk is also a block @see{chunks},
1511and so local variables can be declared in a chunk outside any explicit block. 1521and so local variables can be declared in a chunk outside any explicit block.
@@ -1516,12 +1526,6 @@ The visibility rules for local variables are explained in @See{visibility}.
1516 1526
1517@sect3{to-be-closed| @title{To-be-closed Variables} 1527@sect3{to-be-closed| @title{To-be-closed Variables}
1518 1528
1519A local variable can be declared as a @def{to-be-closed} variable,
1520using the identifier @id{toclose} as its attribute:
1521@Produc{
1522@producname{stat}@producbody{
1523 @Rw{local} @bnfter{<} @id{toclose} @bnfter{>} Name @bnfter{=} exp
1524}}
1525A to-be-closed variable behaves like a constant local variable, 1529A to-be-closed variable behaves like a constant local variable,
1526except that its value is @emph{closed} whenever the variable 1530except that its value is @emph{closed} whenever the variable
1527goes out of scope, including normal block termination, 1531goes out of scope, including normal block termination,
@@ -8215,7 +8219,7 @@ then @id{date} returns the date as a string,
8215formatted according to the same rules as the @ANSI{strftime}. 8219formatted according to the same rules as the @ANSI{strftime}.
8216 8220
8217If @id{format} is absent, it defaults to @St{%c}, 8221If @id{format} is absent, it defaults to @St{%c},
8218which gives a reasonable date and time representation 8222which gives a human-readable date and time representation
8219using the current locale. 8223using the current locale.
8220 8224
8221On non-POSIX systems, 8225On non-POSIX systems,
@@ -9022,10 +9026,14 @@ and @bnfNter{LiteralString}, see @See{lexical}.)
9022@OrNL @Rw{for} namelist @Rw{in} explist @Rw{do} block @Rw{end} 9026@OrNL @Rw{for} namelist @Rw{in} explist @Rw{do} block @Rw{end}
9023@OrNL @Rw{function} funcname funcbody 9027@OrNL @Rw{function} funcname funcbody
9024@OrNL @Rw{local} @Rw{function} @bnfNter{Name} funcbody 9028@OrNL @Rw{local} @Rw{function} @bnfNter{Name} funcbody
9025@OrNL @Rw{local} namelist @bnfopt{@bnfter{=} explist} 9029@OrNL @Rw{local} attnamelist @bnfopt{@bnfter{=} explist}
9026@OrNL @Rw{local} @bnfter{<} Name @bnfter{>} Name @bnfter{=} exp
9027} 9030}
9028 9031
9032@producname{attnamelist}@producbody{
9033 attrib @bnfNter{Name} @bnfrep{@bnfter{,} attrib @bnfNter{Name}}}
9034
9035@producname{attrib}@producbody{@bnfopt{@bnfter{<} @bnfNter{Name} @bnfter{>}}}
9036
9029@producname{retstat}@producbody{@Rw{return} 9037@producname{retstat}@producbody{@Rw{return}
9030 @bnfopt{explist} @bnfopt{@bnfter{;}}} 9038 @bnfopt{explist} @bnfopt{@bnfter{;}}}
9031 9039