comparison website/src/manual.html.luan @ 417:a40e99cf0b0b

work on manual
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 30 Apr 2015 21:30:32 -0600
parents ba8b0aae6453
children 455784e2227d
comparison
equal deleted inserted replaced
416:91af5337b9ae 417:a40e99cf0b0b
71 <li><a href="#logical_ops">Logical Operators</a></li> 71 <li><a href="#logical_ops">Logical Operators</a></li>
72 <li><a href="#concatenation">Concatenation</a></li> 72 <li><a href="#concatenation">Concatenation</a></li>
73 <li><a href="#length">The Length Operator</a></li> 73 <li><a href="#length">The Length Operator</a></li>
74 <li><a href="#precedence">Precedence</a></li> 74 <li><a href="#precedence">Precedence</a></li>
75 <li><a href="#constructors">Table Constructors</a></li> 75 <li><a href="#constructors">Table Constructors</a></li>
76 <li><a href="#fn_calls">Function Calls</a></li>
77 <li><a href="#fn_def">Function Definitions</a></li>
76 </ul> 78 </ul>
77 </li> 79 </li>
80 <li><a href="#visibility">>Visibility Rules</a></li>
78 </ul> 81 </ul>
79 </div> 82 </div>
80 83
81 <hr/> 84 <hr/>
82 85
1260 Every time you create a new table, 1263 Every time you create a new table,
1261 it is different from any previously existing table. 1264 it is different from any previously existing table.
1262 Closures are also compared by reference. 1265 Closures are also compared by reference.
1263 1266
1264 <p> 1267 <p>
1265 You can change the way that Lua compares tables 1268 You can change the way that Luan compares tables
1266 by using the "eq" metamethod (see <a href="#2.4">&sect;2.4</a>). 1269 by using the "eq" metamethod (see <a href="#2.4">&sect;2.4</a>).
1267 1270
1268 <p> 1271 <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. 1272 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.
1270 1273
1474 1477
1475 1478
1476 1479
1477 1480
1478 1481
1479 <h3>3.4.10 &ndash; <a name="3.4.10">Function Calls</a></h3><p> 1482 <h4 margin-top="1em"><a name="fn_calls">Function Calls</a></h4>
1480 A function call in Lua has the following syntax: 1483
1481 1484 <p>
1482 <pre> 1485 A function call in Luan has the following syntax:
1486
1487 <p><tt><pre>
1483 functioncall ::= prefixexp args 1488 functioncall ::= prefixexp args
1484 </pre><p> 1489 </pre></tt></p>
1490
1491 <p>
1485 In a function call, 1492 In a function call,
1486 first prefixexp and args are evaluated. 1493 first prefixexp and args are evaluated.
1487 If the value of prefixexp has type <em>function</em>, 1494 If the value of prefixexp has type <i>function</i>,
1488 then this function is called 1495 then this function is called
1489 with the given arguments. 1496 with the given arguments.
1490 Otherwise, the prefixexp "call" metamethod is called, 1497 Otherwise, the prefixexp "call" metamethod is called,
1491 having as first parameter the value of prefixexp, 1498 having as first parameter the value of prefixexp,
1492 followed by the original call arguments 1499 followed by the original call arguments
1493 (see <a href="#2.4">&sect;2.4</a>). 1500 (see <a href="#2.4">&sect;2.4</a>).
1494 1501
1495 1502
1496 <p> 1503 <p>
1497 The form
1498
1499 <pre>
1500 functioncall ::= prefixexp &lsquo;<b>:</b>&rsquo; Name args
1501 </pre><p>
1502 can be used to call "methods".
1503 A call <code>v:name(<em>args</em>)</code>
1504 is syntactic sugar for <code>v.name(v,<em>args</em>)</code>,
1505 except that <code>v</code> is evaluated only once.
1506
1507
1508 <p>
1509 Arguments have the following syntax: 1504 Arguments have the following syntax:
1510 1505
1511 <pre> 1506 <p><tt><pre>
1512 args ::= &lsquo;<b>(</b>&rsquo; [explist] &lsquo;<b>)</b>&rsquo; 1507 args ::= &lsquo;<b>(</b>&rsquo; [explist] &lsquo;<b>)</b>&rsquo;
1513 args ::= tableconstructor 1508 args ::= tableconstructor
1514 args ::= LiteralString 1509 args ::= LiteralString
1515 </pre><p> 1510 </pre></tt></p>
1511
1512 <p>
1516 All argument expressions are evaluated before the call. 1513 All argument expressions are evaluated before the call.
1517 A call of the form <code>f{<em>fields</em>}</code> is 1514 A call of the form <tt>f{<i>fields</i>}</tt> is
1518 syntactic sugar for <code>f({<em>fields</em>})</code>; 1515 syntactic sugar for <tt>f({<i>fields</i>})</tt>;
1519 that is, the argument list is a single new table. 1516 that is, the argument list is a single new table.
1520 A call of the form <code>f'<em>string</em>'</code> 1517 A call of the form <tt>f'<i>string</i>'</tt>
1521 (or <code>f"<em>string</em>"</code> or <code>f[[<em>string</em>]]</code>) 1518 (or <tt>f"<i>string</i>"</tt> or <tt>f[[<i>string</i>]]</tt>)
1522 is syntactic sugar for <code>f('<em>string</em>')</code>; 1519 is syntactic sugar for <tt>f('<i>string</i>')</tt>;
1523 that is, the argument list is a single literal string. 1520 that is, the argument list is a single literal string.
1524 1521
1525 1522
1526 <p> 1523 <p>
1527 A call of the form <code>return <em>functioncall</em></code> is called 1524 A call of the form <tt>return <i>functioncall</i></tt> is called
1528 a <em>tail call</em>. 1525 a <i>tail call</i>.
1529 Lua implements <em>proper tail calls</em> 1526 Luan implements <i>proper tail calls</i>
1530 (or <em>proper tail recursion</em>): 1527 (or <i>proper tail recursion</i>):
1531 in a tail call, 1528 in a tail call,
1532 the called function reuses the stack entry of the calling function. 1529 the called function reuses the stack entry of the calling function.
1533 Therefore, there is no limit on the number of nested tail calls that 1530 Therefore, there is no limit on the number of nested tail calls that
1534 a program can execute. 1531 a program can execute.
1535 However, a tail call erases any debug information about the 1532 However, a tail call erases any debug information about the
1538 where the <b>return</b> has one single function call as argument; 1535 where the <b>return</b> has one single function call as argument;
1539 this syntax makes the calling function return exactly 1536 this syntax makes the calling function return exactly
1540 the returns of the called function. 1537 the returns of the called function.
1541 So, none of the following examples are tail calls: 1538 So, none of the following examples are tail calls:
1542 1539
1543 <pre> 1540 <p><tt><pre>
1544 return (f(x)) -- results adjusted to 1 1541 return (f(x)) -- results adjusted to 1
1545 return 2 * f(x) 1542 return 2 * f(x)
1546 return x, f(x) -- additional results 1543 return x, f(x) -- additional results
1547 f(x); return -- results discarded 1544 f(x); return -- results discarded
1548 return x or f(x) -- results adjusted to 1 1545 return x or f(x) -- results adjusted to 1
1549 </pre> 1546 </pre></tt></p>
1550 1547
1551 1548
1552 1549
1553 1550
1554 <h3>3.4.11 &ndash; <a name="3.4.11">Function Definitions</a></h3> 1551 <h4 margin-top="1em"><a name="fn_def">Function Definitions</a></h4>
1555 1552
1556 <p> 1553 <p>
1557 The syntax for function definition is 1554 The syntax for function definition is
1558 1555
1559 <pre> 1556 <p><tt><pre>
1560 functiondef ::= <b>function</b> funcbody 1557 functiondef ::= <b>function</b> funcbody
1561 funcbody ::= &lsquo;<b>(</b>&rsquo; [parlist] &lsquo;<b>)</b>&rsquo; block <b>end</b> 1558 funcbody ::= &lsquo;<b>(</b>&rsquo; [parlist] &lsquo;<b>)</b>&rsquo; block <b>end</b>
1562 </pre> 1559 </pre></tt></p>
1563 1560
1564 <p> 1561 <p>
1565 The following syntactic sugar simplifies function definitions: 1562 The following syntactic sugar simplifies function definitions:
1566 1563
1567 <pre> 1564 <p><tt><pre>
1568 stat ::= <b>function</b> funcname funcbody 1565 stat ::= <b>function</b> funcname funcbody
1569 stat ::= <b>local</b> <b>function</b> Name funcbody 1566 stat ::= <b>local</b> <b>function</b> Name funcbody
1570 funcname ::= Name {&lsquo;<b>.</b>&rsquo; Name} [&lsquo;<b>:</b>&rsquo; Name] 1567 funcname ::= Name {&lsquo;<b>.</b>&rsquo; Name} [&lsquo;<b>:</b>&rsquo; Name]
1571 </pre><p> 1568 </pre></tt></p>
1569
1570 <p>
1572 The statement 1571 The statement
1573 1572
1574 <pre> 1573 <p><tt><pre>
1575 function f () <em>body</em> end 1574 function f () <i>body</i> end
1576 </pre><p> 1575 </pre></tt></p>
1576
1577 <p>
1577 translates to 1578 translates to
1578 1579
1579 <pre> 1580 <p><tt><pre>
1580 f = function () <em>body</em> end 1581 f = function () <i>body</i> end
1581 </pre><p> 1582 </pre></tt></p>
1583
1584 <p>
1582 The statement 1585 The statement
1583 1586
1584 <pre> 1587 <p><tt><pre>
1585 function t.a.b.c.f () <em>body</em> end 1588 function t.a.b.c.f () <i>body</i> end
1586 </pre><p> 1589 </pre></tt></p>
1590
1591 <p>
1587 translates to 1592 translates to
1588 1593
1589 <pre> 1594 <p><tt><pre>
1590 t.a.b.c.f = function () <em>body</em> end 1595 t.a.b.c.f = function () <i>body</i> end
1591 </pre><p> 1596 </pre></tt></p>
1597
1598 <p>
1592 The statement 1599 The statement
1593 1600
1594 <pre> 1601 <p><tt><pre>
1595 local function f () <em>body</em> end 1602 local function f () <i>body</i> end
1596 </pre><p> 1603 </pre></tt></p>
1604
1605 <p>
1597 translates to 1606 translates to
1598 1607
1599 <pre> 1608 <p><tt><pre>
1600 local f; f = function () <em>body</em> end 1609 local f; f = function () <em>body</em> end
1601 </pre><p> 1610 </pre></tt></p>
1611
1612 <p>
1602 not to 1613 not to
1603 1614
1604 <pre> 1615 <p><tt><pre>
1605 local f = function () <em>body</em> end 1616 local f = function () <em>body</em> end
1606 </pre><p> 1617 </pre></tt></p>
1618
1619 <p>
1607 (This only makes a difference when the body of the function 1620 (This only makes a difference when the body of the function
1608 contains references to <code>f</code>.) 1621 contains references to <tt>f</tt>.)
1609 1622
1610 1623
1611 <p> 1624 <p>
1612 A function definition is an executable expression, 1625 A function definition is an executable expression,
1613 whose value has type <em>function</em>. 1626 whose value has type <i>function</i>.
1614 When Lua precompiles a chunk, 1627 When Luan precompiles a chunk,
1615 all its function bodies are precompiled too. 1628 all its function bodies are precompiled too.
1616 Then, whenever Lua executes the function definition, 1629 Then, whenever Luan executes the function definition,
1617 the function is <em>instantiated</em> (or <em>closed</em>). 1630 the function is <i>instantiated</i> (or <i>closed</i>).
1618 This function instance (or <em>closure</em>) 1631 This function instance (or <i>closure</i>)
1619 is the final value of the expression. 1632 is the final value of the expression.
1620 1633
1621 1634
1622 <p> 1635 <p>
1623 Parameters act as local variables that are 1636 Parameters act as local variables that are
1624 initialized with the argument values: 1637 initialized with the argument values:
1625 1638
1626 <pre> 1639 <p><tt><pre>
1627 parlist ::= namelist [&lsquo;<b>,</b>&rsquo; &lsquo;<b>...</b>&rsquo;] | &lsquo;<b>...</b>&rsquo; 1640 parlist ::= namelist [&lsquo;<b>,</b>&rsquo; &lsquo;<b>...</b>&rsquo;] | &lsquo;<b>...</b>&rsquo;
1628 </pre><p> 1641 </pre></tt></p>
1642
1643 <p>
1629 When a function is called, 1644 When a function is called,
1630 the list of arguments is adjusted to 1645 the list of arguments is adjusted to
1631 the length of the list of parameters, 1646 the length of the list of parameters,
1632 unless the function is a <em>vararg function</em>, 1647 unless the function is a <i>vararg function</i>,
1633 which is indicated by three dots ('<code>...</code>') 1648 which is indicated by three dots ('<tt>...</tt>')
1634 at the end of its parameter list. 1649 at the end of its parameter list.
1635 A vararg function does not adjust its argument list; 1650 A vararg function does not adjust its argument list;
1636 instead, it collects all extra arguments and supplies them 1651 instead, it collects all extra arguments and supplies them
1637 to the function through a <em>vararg expression</em>, 1652 to the function through a <i>vararg expression</i>,
1638 which is also written as three dots. 1653 which is also written as three dots.
1639 The value of this expression is a list of all actual extra arguments, 1654 The value of this expression is a list of all actual extra arguments,
1640 similar to a function with multiple results. 1655 similar to a function with multiple results.
1641 If a vararg expression is used inside another expression 1656 If a vararg expression is used inside another expression
1642 or in the middle of a list of expressions, 1657 or in the middle of a list of expressions,
1647 1662
1648 1663
1649 <p> 1664 <p>
1650 As an example, consider the following definitions: 1665 As an example, consider the following definitions:
1651 1666
1652 <pre> 1667 <p><tt><pre>
1653 function f(a, b) end 1668 function f(a, b) end
1654 function g(a, b, ...) end 1669 function g(a, b, ...) end
1655 function r() return 1,2,3 end 1670 function r() return 1,2,3 end
1656 </pre><p> 1671 </pre></tt></p>
1672
1673 <p>
1657 Then, we have the following mapping from arguments to parameters and 1674 Then, we have the following mapping from arguments to parameters and
1658 to the vararg expression: 1675 to the vararg expression:
1659 1676
1660 <pre> 1677 <p><tt><pre>
1661 CALL PARAMETERS 1678 CALL PARAMETERS
1662 1679
1663 f(3) a=3, b=nil 1680 f(3) a=3, b=nil
1664 f(3, 4) a=3, b=4 1681 f(3, 4) a=3, b=4
1665 f(3, 4, 5) a=3, b=4 1682 f(3, 4, 5) a=3, b=4
1668 1685
1669 g(3) a=3, b=nil, ... --&gt; (nothing) 1686 g(3) a=3, b=nil, ... --&gt; (nothing)
1670 g(3, 4) a=3, b=4, ... --&gt; (nothing) 1687 g(3, 4) a=3, b=4, ... --&gt; (nothing)
1671 g(3, 4, 5, 8) a=3, b=4, ... --&gt; 5 8 1688 g(3, 4, 5, 8) a=3, b=4, ... --&gt; 5 8
1672 g(5, r()) a=5, b=1, ... --&gt; 2 3 1689 g(5, r()) a=5, b=1, ... --&gt; 2 3
1673 </pre> 1690 </pre></tt></p>
1674 1691
1675 <p> 1692 <p>
1676 Results are returned using the <b>return</b> statement (see <a href="#3.3.4">&sect;3.3.4</a>). 1693 Results are returned using the <b>return</b> statement (see <a href="#3.3.4">&sect;3.3.4</a>).
1677 If control reaches the end of a function 1694 If control reaches the end of a function
1678 without encountering a <b>return</b> statement, 1695 without encountering a <b>return</b> statement,
1679 then the function returns with no results. 1696 then the function returns with no results.
1680 1697
1681 1698
1682 <p> 1699
1683 1700
1684 There is a system-dependent limit on the number of values 1701 <h3 margin-top="1em"><a name="visibility">Visibility Rules</a></h3>
1685 that a function may return. 1702
1686 This limit is guaranteed to be larger than 1000. 1703 <p>
1687 1704 Luan is a lexically scoped language.
1688
1689 <p>
1690 The <em>colon</em> syntax
1691 is used for defining <em>methods</em>,
1692 that is, functions that have an implicit extra parameter <code>self</code>.
1693 Thus, the statement
1694
1695 <pre>
1696 function t.a.b.c:f (<em>params</em>) <em>body</em> end
1697 </pre><p>
1698 is syntactic sugar for
1699
1700 <pre>
1701 t.a.b.c.f = function (self, <em>params</em>) <em>body</em> end
1702 </pre>
1703
1704
1705
1706
1707
1708
1709 <h2>3.5 &ndash; <a name="3.5">Visibility Rules</a></h2>
1710
1711 <p>
1712
1713 Lua is a lexically scoped language.
1714 The scope of a local variable begins at the first statement after 1705 The scope of a local variable begins at the first statement after
1715 its declaration and lasts until the last non-void statement 1706 its declaration and lasts until the last non-void statement
1716 of the innermost block that includes the declaration. 1707 of the innermost block that includes the declaration.
1717 Consider the following example: 1708 Consider the following example:
1718 1709
1719 <pre> 1710 <p><tt><pre>
1720 x = 10 -- global variable 1711 x = 10 -- global variable
1721 do -- new block 1712 do -- new block
1722 local x = x -- new 'x', with value 10 1713 local x = x -- new 'x', with value 10
1723 print(x) --&gt; 10 1714 print(x) --&gt; 10
1724 x = x+1 1715 x = x+1
1727 print(x) --&gt; 12 1718 print(x) --&gt; 12
1728 end 1719 end
1729 print(x) --&gt; 11 1720 print(x) --&gt; 11
1730 end 1721 end
1731 print(x) --&gt; 10 (the global one) 1722 print(x) --&gt; 10 (the global one)
1732 </pre> 1723 </pre></tt></p>
1733 1724
1734 <p> 1725 <p>
1735 Notice that, in a declaration like <code>local x = x</code>, 1726 Notice that, in a declaration like <tt>local x = x</tt>,
1736 the new <code>x</code> being declared is not in scope yet, 1727 the new <tt>x</tt> being declared is not in scope yet,
1737 and so the second <code>x</code> refers to the outside variable. 1728 and so the second <tt>x</tt> refers to the outside variable.
1738 1729
1739 1730
1740 <p> 1731 <p>
1741 Because of the lexical scoping rules, 1732 Because of the lexical scoping rules,
1742 local variables can be freely accessed by functions 1733 local variables can be freely accessed by functions
1743 defined inside their scope. 1734 defined inside their scope.
1744 A local variable used by an inner function is called 1735 A local variable used by an inner function is called
1745 an <em>upvalue</em>, or <em>external local variable</em>, 1736 an <i>upvalue</i>, or <i>external local variable</i>,
1746 inside the inner function. 1737 inside the inner function.
1747 1738
1748 1739
1749 <p> 1740 <p>
1750 Notice that each execution of a <b>local</b> statement 1741 Notice that each execution of a <b>local</b> statement
1751 defines new local variables. 1742 defines new local variables.
1752 Consider the following example: 1743 Consider the following example:
1753 1744
1754 <pre> 1745 <p><tt><pre>
1755 a = {} 1746 a = {}
1756 local x = 20 1747 local x = 20
1757 for i=1,10 do 1748 for i=1,10 do
1758 local y = 0 1749 local y = 0
1759 a[i] = function () y=y+1; return x+y end 1750 a[i] = function () y=y+1; return x+y end
1760 end 1751 end
1761 </pre><p> 1752 </pre></tt></p>
1753
1754 <p>
1762 The loop creates ten closures 1755 The loop creates ten closures
1763 (that is, ten instances of the anonymous function). 1756 (that is, ten instances of the anonymous function).
1764 Each of these closures uses a different <code>y</code> variable, 1757 Each of these closures uses a different <tt>y</tt> variable,
1765 while all of them share the same <code>x</code>. 1758 while all of them share the same <tt>x</tt>.
1766 1759
1767 1760
1768 1761
1769 1762
1770 1763