comparison website/src/manual.html.luan @ 396:ba8b0aae6453

work on manual; add LuanJavaFunction.ARG_COLLECTION;
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 26 Apr 2015 21:19:10 -0600
parents a7cb58532846
children a40e99cf0b0b
comparison
equal deleted inserted replaced
395:a7cb58532846 396:ba8b0aae6453
64 </li> 64 </li>
65 <li> 65 <li>
66 <a href="#expressions">Expressions</a> 66 <a href="#expressions">Expressions</a>
67 <ul> 67 <ul>
68 <li><a href="#arithmetic">Arithmetic Operators</a></li> 68 <li><a href="#arithmetic">Arithmetic Operators</a></li>
69 <li><a href="#conversions">Coercions and Conversions</a></li>
70 <li><a href="#relational">Relational Operators</a></li>
71 <li><a href="#logical_ops">Logical Operators</a></li>
72 <li><a href="#concatenation">Concatenation</a></li>
73 <li><a href="#length">The Length Operator</a></li>
74 <li><a href="#precedence">Precedence</a></li>
75 <li><a href="#constructors">Table Constructors</a></li>
69 </ul> 76 </ul>
70 </li> 77 </li>
71 </ul> 78 </ul>
72 </div> 79 </div>
73 80
1208 that rounds the quotient towards minus infinite (floor division). 1215 that rounds the quotient towards minus infinite (floor division).
1209 (The Java modulo operator is not used.) 1216 (The Java modulo operator is not used.)
1210 1217
1211 1218
1212 1219
1213 <h3>3.4.3 &ndash; <a name="3.4.3">Coercions and Conversions</a></h3><p> 1220 <h4 margin-top="1em"><a name="conversions">Coercions and Conversions</a></h4>
1214 Lua provides some automatic conversions between some 1221
1222 <p>
1223 Luan provides some automatic conversions between some
1215 types and representations at run time. 1224 types and representations at run time.
1216 Bitwise operators always convert float operands to integers. 1225 String concatenation converts all of its arguments to strings.
1217 Exponentiation and float division 1226
1218 always convert integer operands to floats. 1227 Luan also converts strings to numbers
1219 All other arithmetic operations applied to mixed numbers
1220 (integers and floats) convert the integer operand to a float;
1221 this is called the <em>usual rule</em>.
1222 The C API also converts both integers to floats and
1223 floats to integers, as needed.
1224 Moreover, string concatenation accepts numbers as arguments,
1225 besides strings.
1226
1227
1228 <p>
1229 Lua also converts strings to numbers,
1230 whenever a number is expected. 1228 whenever a number is expected.
1231 1229
1232 1230
1233 <p> 1231
1234 In a conversion from integer to float, 1232
1235 if the integer value has an exact representation as a float, 1233
1236 that is the result. 1234 <h4 margin-top="1em"><a name="relational">Relational Operators</a></h4>
1237 Otherwise, 1235
1238 the conversion gets the nearest higher or 1236 <p>
1239 the nearest lower representable value. 1237 Luan supports the following relational operators:
1240 This kind of conversion never fails.
1241
1242
1243 <p>
1244 The conversion from float to integer
1245 checks whether the float has an exact representation as an integer
1246 (that is, the float has an integral value and
1247 it is in the range of integer representation).
1248 If it does, that representation is the result.
1249 Otherwise, the conversion fails.
1250
1251
1252 <p>
1253 The conversion from strings to numbers goes as follows:
1254 First, the string is converted to an integer or a float,
1255 following its syntax and the rules of the Lua lexer.
1256 (The string may have also leading and trailing spaces and a sign.)
1257 Then, the resulting number is converted to the required type
1258 (float or integer) according to the previous rules.
1259
1260
1261 <p>
1262 The conversion from numbers to strings uses a
1263 non-specified human-readable format.
1264 For complete control over how numbers are converted to strings,
1265 use the <code>format</code> function from the string library
1266 (see <a href="#pdf-string.format"><code>string.format</code></a>).
1267
1268
1269
1270
1271
1272 <h3>3.4.4 &ndash; <a name="3.4.4">Relational Operators</a></h3><p>
1273 Lua supports the following relational operators:
1274 1238
1275 <ul> 1239 <ul>
1276 <li><b><code>==</code>: </b>equality</li> 1240 <li><b><tt>==</tt>: </b>equality</li>
1277 <li><b><code>~=</code>: </b>inequality</li> 1241 <li><b><tt>~=</tt>: </b>inequality</li>
1278 <li><b><code>&lt;</code>: </b>less than</li> 1242 <li><b><tt>&lt;</tt>: </b>less than</li>
1279 <li><b><code>&gt;</code>: </b>greater than</li> 1243 <li><b><tt>&gt;</tt>: </b>greater than</li>
1280 <li><b><code>&lt;=</code>: </b>less or equal</li> 1244 <li><b><tt>&lt;=</tt>: </b>less or equal</li>
1281 <li><b><code>&gt;=</code>: </b>greater or equal</li> 1245 <li><b><tt>&gt;=</tt>: </b>greater or equal</li>
1282 </ul><p> 1246 </ul><p>
1283 These operators always result in <b>false</b> or <b>true</b>. 1247 These operators always result in <b>false</b> or <b>true</b>.
1284 1248
1285 1249
1286 <p> 1250 <p>
1287 Equality (<code>==</code>) first compares the type of its operands. 1251 Equality (<tt>==</tt>) first compares the type of its operands.
1288 If the types are different, then the result is <b>false</b>. 1252 If the types are different, then the result is <b>false</b>.
1289 Otherwise, the values of the operands are compared. 1253 Otherwise, the values of the operands are compared.
1290 Strings are compared in the obvious way. 1254 Strings, numbers, and binary values are compared in the obvious way (by value).
1291 Numbers follow the usual rule for binary operations: 1255
1292 if both operands are integers, 1256 <p>
1293 they are compared as integers; 1257 Tables
1294 otherwise, they are converted to floats
1295 and compared as such.
1296
1297
1298 <p>
1299 Tables, userdata, and threads
1300 are compared by reference: 1258 are compared by reference:
1301 two objects are considered equal only if they are the same object. 1259 two objects are considered equal only if they are the same object.
1302 Every time you create a new object 1260 Every time you create a new table,
1303 (a table, userdata, or thread), 1261 it is different from any previously existing table.
1304 this new object is different from any previously existing object. 1262 Closures are also compared by reference.
1305 Closures with the same reference are always equal. 1263
1306 Closures with any detectable difference 1264 <p>
1307 (different behavior, different definition) are always different. 1265 You can change the way that Lua compares tables
1308
1309
1310 <p>
1311 You can change the way that Lua compares tables and userdata
1312 by using the "eq" metamethod (see <a href="#2.4">&sect;2.4</a>). 1266 by using the "eq" metamethod (see <a href="#2.4">&sect;2.4</a>).
1313 1267
1268 <p>
1269 Userdata are Java objects. They are compared for equality with the Java <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)"><tt>equals</tt></a> method.
1314 1270
1315 <p> 1271 <p>
1316 Equality comparisons do not convert strings to numbers 1272 Equality comparisons do not convert strings to numbers
1317 or vice versa. 1273 or vice versa.
1318 Thus, <code>"0"==0</code> evaluates to <b>false</b>, 1274 Thus, <tt>"0"==0</tt> evaluates to <b>false</b>,
1319 and <code>t[0]</code> and <code>t["0"]</code> denote different 1275 and <tt>t[0]</tt> and <tt>t["0"]</tt> denote different
1320 entries in a table. 1276 entries in a table.
1321 1277
1322 1278
1323 <p> 1279 <p>
1324 The operator <code>~=</code> is exactly the negation of equality (<code>==</code>). 1280 The operator <tt>~=</tt> is exactly the negation of equality (<tt>==</tt>).
1325 1281
1326 1282
1327 <p> 1283 <p>
1328 The order operators work as follows. 1284 The order operators work as follows.
1285
1329 If both arguments are numbers, 1286 If both arguments are numbers,
1330 then they are compared following 1287 then they are compared following
1331 the usual rule for binary operations. 1288 the usual rule for binary operations.
1332 Otherwise, if both arguments are strings, 1289 Otherwise, if both arguments are strings,
1333 then their values are compared according to the current locale. 1290 then their values are compared according to the current locale.
1334 Otherwise, Lua tries to call the "lt" or the "le" 1291 Otherwise, Luan tries to call the "lt" or the "le"
1335 metamethod (see <a href="#2.4">&sect;2.4</a>). 1292 metamethod (see <a href="#2.4">&sect;2.4</a>).
1336 A comparison <code>a &gt; b</code> is translated to <code>b &lt; a</code> 1293 A comparison <tt>a &gt; b</tt> is translated to <tt>b &lt; a</tt>
1337 and <code>a &gt;= b</code> is translated to <code>b &lt;= a</code>. 1294 and <tt>a &gt;= b</tt> is translated to <tt>b &lt;= a</tt>.
1338 1295
1339 1296
1340 1297
1341 1298
1342 1299
1343 <h3>3.4.5 &ndash; <a name="3.4.5">Logical Operators</a></h3><p> 1300 <h4 margin-top="1em"><a name="logical_ops">Logical Operators</a></h4>
1344 The logical operators in Lua are 1301
1302 <p>
1303 The logical operators in Luan are
1345 <b>and</b>, <b>or</b>, and <b>not</b>. 1304 <b>and</b>, <b>or</b>, and <b>not</b>.
1305 The <b>and</b> and <b>or</b> operators consider both <b>false</b> and <b>nil</b> as false
1306 and anything else as true.
1346 Like the control structures (see <a href="#3.3.4">&sect;3.3.4</a>), 1307 Like the control structures (see <a href="#3.3.4">&sect;3.3.4</a>),
1347 all logical operators consider both <b>false</b> and <b>nil</b> as false 1308 the <b>not</b> operator requires a boolean value.
1348 and anything else as true.
1349
1350 1309
1351 <p> 1310 <p>
1352 The negation operator <b>not</b> always returns <b>false</b> or <b>true</b>. 1311 The negation operator <b>not</b> always returns <b>false</b> or <b>true</b>.
1353 The conjunction operator <b>and</b> returns its first argument 1312 The conjunction operator <b>and</b> returns its first argument
1354 if this value is <b>false</b> or <b>nil</b>; 1313 if this value is <b>false</b> or <b>nil</b>;
1359 Both <b>and</b> and <b>or</b> use short-circuit evaluation; 1318 Both <b>and</b> and <b>or</b> use short-circuit evaluation;
1360 that is, 1319 that is,
1361 the second operand is evaluated only if necessary. 1320 the second operand is evaluated only if necessary.
1362 Here are some examples: 1321 Here are some examples:
1363 1322
1364 <pre> 1323 <p><tt><pre>
1365 10 or 20 --&gt; 10 1324 10 or 20 --&gt; 10
1366 10 or error() --&gt; 10 1325 10 or error() --&gt; 10
1367 nil or "a" --&gt; "a" 1326 nil or "a" --&gt; "a"
1368 nil and 10 --&gt; nil 1327 nil and 10 --&gt; nil
1369 false and error() --&gt; false 1328 false and error() --&gt; false
1370 false and nil --&gt; false 1329 false and nil --&gt; false
1371 false or nil --&gt; nil 1330 false or nil --&gt; nil
1372 10 and 20 --&gt; 20 1331 10 and 20 --&gt; 20
1373 </pre><p> 1332 </pre></tt></p>
1333
1334 <p>
1374 (In this manual, 1335 (In this manual,
1375 <code>--&gt;</code> indicates the result of the preceding expression.) 1336 <tt>--&gt;</tt> indicates the result of the preceding expression.)
1376 1337
1377 1338
1378 1339
1379 1340 <h4 margin-top="1em"><a name="concatenation">Concatenation</a></h4>
1380 1341
1381 <h3>3.4.6 &ndash; <a name="3.4.6">Concatenation</a></h3><p> 1342 <p>
1382 The string concatenation operator in Lua is 1343 The string concatenation operator in Luan is
1383 denoted by two dots ('<code>..</code>'). 1344 denoted by two dots ('<tt>..</tt>').
1384 If both operands are strings or numbers, then they are converted to 1345 All operands are converted to strings.
1385 strings according to the rules described in <a href="#3.4.3">&sect;3.4.3</a>. 1346
1386 Otherwise, the <code>__concat</code> metamethod is called (see <a href="#2.4">&sect;2.4</a>). 1347
1387 1348
1388 1349 <h4 margin-top="1em"><a name="length">The Length Operator</a></h4>
1389 1350
1390 1351 <p>
1391 1352 The length operator is denoted by the unary prefix operator <tt>#</tt>.
1392 <h3>3.4.7 &ndash; <a name="3.4.7">The Length Operator</a></h3> 1353 The length of a string is its number of characters.
1393 1354 The length of a binary is its number of bytes.
1394 <p>
1395 The length operator is denoted by the unary prefix operator <code>#</code>.
1396 The length of a string is its number of bytes
1397 (that is, the usual meaning of string length when each
1398 character is one byte).
1399 1355
1400 1356
1401 <p> 1357 <p>
1402 A program can modify the behavior of the length operator for 1358 A program can modify the behavior of the length operator for
1403 any value but strings through the <code>__len</code> metamethod (see <a href="#2.4">&sect;2.4</a>). 1359 any table through the <tt>__len</tt> metamethod (see <a href="#2.4">&sect;2.4</a>).
1404 1360
1405 1361
1406 <p> 1362 <p>
1407 Unless a <code>__len</code> metamethod is given, 1363 Unless a <tt>__len</tt> metamethod is given,
1408 the length of a table <code>t</code> is only defined if the 1364 the length of a table <tt>t</tt> is defined
1409 table is a <em>sequence</em>, 1365 as the number of elements in <em>sequence</em>,
1410 that is, 1366 that is,
1411 the set of its positive numeric keys is equal to <em>{1..n}</em> 1367 the size of the set of its positive numeric keys is equal to <em>{1..n}</em>
1412 for some non-negative integer <em>n</em>. 1368 for some non-negative integer <em>n</em>.
1413 In that case, <em>n</em> is its length. 1369 In that case, <em>n</em> is its length.
1414 Note that a table like 1370 Note that a table like
1415 1371
1416 <pre> 1372 <p><tt><pre>
1417 {10, 20, nil, 40} 1373 {10, 20, nil, 40}
1418 </pre><p> 1374 </pre></tt></p>
1419 is not a sequence, because it has the key <code>4</code> 1375
1420 but does not have the key <code>3</code>. 1376 <p>
1421 (So, there is no <em>n</em> such that the set <em>{1..n}</em> is equal 1377 has a length of <tt>2</tt>, because that is the last key in sequence.
1422 to the set of positive numeric keys of that table.) 1378
1423 Note, however, that non-numeric keys do not interfere 1379
1424 with whether a table is a sequence. 1380
1425 1381
1426 1382
1427 1383 <h4 margin-top="1em"><a name="precedence">Precedence</a></h4>
1428 1384
1429 1385 <p>
1430 <h3>3.4.8 &ndash; <a name="3.4.8">Precedence</a></h3><p> 1386 Operator precedence in Luan follows the table below,
1431 Operator precedence in Lua follows the table below,
1432 from lower to higher priority: 1387 from lower to higher priority:
1433 1388
1434 <pre> 1389 <p><tt><pre>
1435 or 1390 or
1436 and 1391 and
1437 &lt; &gt; &lt;= &gt;= ~= == 1392 &lt; &gt; &lt;= &gt;= ~= ==
1438 |
1439 ~
1440 &amp;
1441 &lt;&lt; &gt;&gt;
1442 .. 1393 ..
1443 + - 1394 + -
1444 * / // % 1395 * / %
1445 unary operators (not # - ~) 1396 unary operators (not # -)
1446 ^ 1397 ^
1447 </pre><p> 1398 </pre></tt></p>
1399
1400 <p>
1448 As usual, 1401 As usual,
1449 you can use parentheses to change the precedences of an expression. 1402 you can use parentheses to change the precedences of an expression.
1450 The concatenation ('<code>..</code>') and exponentiation ('<code>^</code>') 1403 The concatenation ('<tt>..</tt>') and exponentiation ('<tt>^</tt>')
1451 operators are right associative. 1404 operators are right associative.
1452 All other binary operators are left associative. 1405 All other binary operators are left associative.
1453 1406
1454 1407
1455 1408
1456 1409
1457 1410
1458 <h3>3.4.9 &ndash; <a name="3.4.9">Table Constructors</a></h3><p> 1411 <h4 margin-top="1em"><a name="constructors">Table Constructors</a></h4>
1412
1413 <p>
1459 Table constructors are expressions that create tables. 1414 Table constructors are expressions that create tables.
1460 Every time a constructor is evaluated, a new table is created. 1415 Every time a constructor is evaluated, a new table is created.
1461 A constructor can be used to create an empty table 1416 A constructor can be used to create an empty table
1462 or to create a table and initialize some of its fields. 1417 or to create a table and initialize some of its fields.
1463 The general syntax for constructors is 1418 The general syntax for constructors is
1464 1419
1465 <pre> 1420 <p><tt><pre>
1466 tableconstructor ::= &lsquo;<b>{</b>&rsquo; [fieldlist] &lsquo;<b>}</b>&rsquo; 1421 tableconstructor ::= &lsquo;<b>{</b>&rsquo; [fieldlist] &lsquo;<b>}</b>&rsquo;
1467 fieldlist ::= field {fieldsep field} [fieldsep] 1422 fieldlist ::= field {fieldsep field} [fieldsep]
1468 field ::= &lsquo;<b>[</b>&rsquo; exp &lsquo;<b>]</b>&rsquo; &lsquo;<b>=</b>&rsquo; exp | Name &lsquo;<b>=</b>&rsquo; exp | exp 1423 field ::= &lsquo;<b>[</b>&rsquo; exp &lsquo;<b>]</b>&rsquo; &lsquo;<b>=</b>&rsquo; exp | Name &lsquo;<b>=</b>&rsquo; exp | exp
1469 fieldsep ::= &lsquo;<b>,</b>&rsquo; | &lsquo;<b>;</b>&rsquo; 1424 fieldsep ::= &lsquo;<b>,</b>&rsquo; | &lsquo;<b>;</b>&rsquo;
1470 </pre> 1425 </pre></tt></p>
1471 1426
1472 <p> 1427 <p>
1473 Each field of the form <code>[exp1] = exp2</code> adds to the new table an entry 1428 Each field of the form <tt>[exp1] = exp2</tt> adds to the new table an entry
1474 with key <code>exp1</code> and value <code>exp2</code>. 1429 with key <tt>exp1</tt> and value <tt>exp2</tt>.
1475 A field of the form <code>name = exp</code> is equivalent to 1430 A field of the form <tt>name = exp</tt> is equivalent to
1476 <code>["name"] = exp</code>. 1431 <tt>["name"] = exp</tt>.
1477 Finally, fields of the form <code>exp</code> are equivalent to 1432 Finally, fields of the form <tt>exp</tt> are equivalent to
1478 <code>[i] = exp</code>, where <code>i</code> are consecutive integers 1433 <tt>[i] = exp</tt>, where <tt>i</tt> are consecutive integers
1479 starting with 1. 1434 starting with 1.
1480 Fields in the other formats do not affect this counting. 1435 Fields in the other formats do not affect this counting.
1481 For example, 1436 For example,
1482 1437
1483 <pre> 1438 <p><tt><pre>
1484 a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 } 1439 a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 }
1485 </pre><p> 1440 </pre></tt></p>
1441
1442 <p>
1486 is equivalent to 1443 is equivalent to
1487 1444
1488 <pre> 1445 <p><tt><pre>
1489 do 1446 do
1490 local t = {} 1447 local t = {}
1491 t[f(1)] = g 1448 t[f(1)] = g
1492 t[1] = "x" -- 1st exp 1449 t[1] = "x" -- 1st exp
1493 t[2] = "y" -- 2nd exp 1450 t[2] = "y" -- 2nd exp
1495 t[3] = f(x) -- 3rd exp 1452 t[3] = f(x) -- 3rd exp
1496 t[30] = 23 1453 t[30] = 23
1497 t[4] = 45 -- 4th exp 1454 t[4] = 45 -- 4th exp
1498 a = t 1455 a = t
1499 end 1456 end
1500 </pre> 1457 </pre></tt></p>
1501 1458
1502 <p> 1459 <p>
1503 The order of the assignments in a constructor is undefined. 1460 The order of the assignments in a constructor is undefined.
1504 (This order would be relevant only when there are repeated keys.) 1461 (This order would be relevant only when there are repeated keys.)
1505 1462
1506 1463
1507 <p> 1464 <p>
1508 If the last field in the list has the form <code>exp</code> 1465 If the last field in the list has the form <tt>exp</tt>
1509 and the expression is a function call or a vararg expression, 1466 and the expression is a function call or a vararg expression,
1510 then all values returned by this expression enter the list consecutively 1467 then all values returned by this expression enter the list consecutively
1511 (see <a href="#3.4.10">&sect;3.4.10</a>). 1468 (see <a href="#3.4.10">&sect;3.4.10</a>).
1512 1469
1513 1470