diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-07-03 14:18:07 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-07-03 14:18:07 -0300 |
commit | 4d46289331395a845c5de1f6c0e0fe873c50db4f (patch) | |
tree | 96649174d3c13830549ad026af0133e0b789d411 /manual | |
parent | 8eca21c2e85625390a2a3b08c231e75e315980b0 (diff) | |
download | lua-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.of | 60 |
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. | |||
1399 | Their values are called respectively | 1399 | Their values are called respectively |
1400 | the @emph{initial value}, the @emph{limit}, and the @emph{step}. | 1400 | the @emph{initial value}, the @emph{limit}, and the @emph{step}. |
1401 | If the step is absent, it defaults @N{to 1}. | 1401 | If the step is absent, it defaults @N{to 1}. |
1402 | Then the loop body is repeated with the value of the control variable | 1402 | |
1403 | If both the initial value and the step are integers, | ||
1404 | the loop is done with integers; | ||
1405 | note that the limit may not be an integer. | ||
1406 | Otherwise, the loop is done with floats. | ||
1407 | (Beware of floating-point accuracy in this case.) | ||
1408 | |||
1409 | After that initialization, | ||
1410 | the loop body is repeated with the value of the control variable | ||
1403 | going through an arithmetic progression, | 1411 | going through an arithmetic progression, |
1404 | starting at the initial value, | 1412 | starting at the initial value, |
1405 | with a common difference given by the step, | 1413 | with a common difference given by the step. |
1406 | until that value passes the limit. | ||
1407 | A negative step makes a decreasing sequence; | 1414 | A negative step makes a decreasing sequence; |
1408 | a step equal to zero raises an error. | 1415 | a step equal to zero raises an error. |
1416 | The loop continues while the value is less than | ||
1417 | or equal to the limit | ||
1418 | (greater than or equal to for a negative step). | ||
1409 | If the initial value is already greater than the limit | 1419 | If 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), |
1411 | the body is not executed. | 1421 | the body is not executed. |
1412 | 1422 | ||
1413 | If both the initial value and the step are integers, | 1423 | For integer loops, |
1414 | the loop is done with integers; | 1424 | the control variable never wraps around; |
1415 | in this case, the range of the control variable is clipped | 1425 | instead, the loop ends in case of an overflow. |
1416 | by the range of integers. | ||
1417 | Otherwise, the loop is done with floats. | ||
1418 | (Beware of floating-point accuracy in this case.) | ||
1419 | 1426 | ||
1420 | You should not change the value of the control variable | 1427 | You should not change the value of the control variable |
1421 | during the loop. | 1428 | during 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. |
1491 | The declaration can include an initialization: | 1498 | The 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 | } |
1497 | If present, an initial assignment has the same semantics | 1504 | If present, an initial assignment has the same semantics |
1498 | of a multiple assignment @see{assignment}. | 1505 | of a multiple assignment @see{assignment}. |
1499 | Otherwise, all variables are initialized with @nil. | 1506 | Otherwise, all variables are initialized with @nil. |
1500 | The second syntax declares a local with a given attribute, | 1507 | |
1501 | which is the name between the angle brackets. | 1508 | Each variable name may be preceded by an attribute |
1502 | In 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 | } | ||
1503 | There are two possible attributes: | 1513 | There are two possible attributes: |
1504 | @id{const}, which declares a @x{constant variable}, | 1514 | @id{const}, which declares a @x{constant variable}, |
1505 | that is, a variable that cannot be assigned to | 1515 | that is, a variable that cannot be assigned to |
1506 | after its initialization; | 1516 | after its initialization; |
1507 | and @id{toclose}, which declares a to-be-closed variable @see{to-be-closed}. | 1517 | and @id{toclose}, which declares a to-be-closed variable @see{to-be-closed}. |
1508 | 1518 | A list of variables can contain at most one to-be-closed variable. | |
1509 | 1519 | ||
1510 | A chunk is also a block @see{chunks}, | 1520 | A chunk is also a block @see{chunks}, |
1511 | and so local variables can be declared in a chunk outside any explicit block. | 1521 | and 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 | ||
1519 | A local variable can be declared as a @def{to-be-closed} variable, | ||
1520 | using the identifier @id{toclose} as its attribute: | ||
1521 | @Produc{ | ||
1522 | @producname{stat}@producbody{ | ||
1523 | @Rw{local} @bnfter{<} @id{toclose} @bnfter{>} Name @bnfter{=} exp | ||
1524 | }} | ||
1525 | A to-be-closed variable behaves like a constant local variable, | 1529 | A to-be-closed variable behaves like a constant local variable, |
1526 | except that its value is @emph{closed} whenever the variable | 1530 | except that its value is @emph{closed} whenever the variable |
1527 | goes out of scope, including normal block termination, | 1531 | goes out of scope, including normal block termination, |
@@ -8215,7 +8219,7 @@ then @id{date} returns the date as a string, | |||
8215 | formatted according to the same rules as the @ANSI{strftime}. | 8219 | formatted according to the same rules as the @ANSI{strftime}. |
8216 | 8220 | ||
8217 | If @id{format} is absent, it defaults to @St{%c}, | 8221 | If @id{format} is absent, it defaults to @St{%c}, |
8218 | which gives a reasonable date and time representation | 8222 | which gives a human-readable date and time representation |
8219 | using the current locale. | 8223 | using the current locale. |
8220 | 8224 | ||
8221 | On non-POSIX systems, | 8225 | On 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 | ||