Mercurial Hosting > luan
comparison website/src/m.html.luan @ 1667:c55373c3a0ce
manual work
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 08 May 2022 22:50:48 -0600 |
parents | 2968e43cdd44 |
children | ef75d9ad5ce9 |
comparison
equal
deleted
inserted
replaced
1666:8f38abaf779f | 1667:c55373c3a0ce |
---|---|
1370 then their values are compared according to the current locale. | 1370 then their values are compared according to the current locale. |
1371 Otherwise, Luan tries to call the "lt" or the "le" | 1371 Otherwise, Luan tries to call the "lt" or the "le" |
1372 metamethod (see <a href="#meta">Metatables and Metamethods</a>). | 1372 metamethod (see <a href="#meta">Metatables and Metamethods</a>). |
1373 A comparison <code>a > b</code> is translated to <code>b < a</code> | 1373 A comparison <code>a > b</code> is translated to <code>b < a</code> |
1374 and <code>a >= b</code> is translated to <code>b <= a</code>. | 1374 and <code>a >= b</code> is translated to <code>b <= a</code>. |
1375 </p> | |
1376 <% | |
1377 end | |
1378 } | |
1379 logical_ops = { | |
1380 title = "Logical Operators" | |
1381 content = function() | |
1382 %> | |
1383 <p> | |
1384 The logical operators in Luan are | |
1385 <b>and</b>, <b>or</b>, and <b>not</b>. | |
1386 The <b>and</b> and <b>or</b> operators consider both <b>false</b> and <b>nil</b> as false | |
1387 and anything else as true. | |
1388 Like the control structures (see <a href="#control">Control Structures</a>), | |
1389 the <b>not</b> operator requires a boolean value. | |
1390 </p> | |
1391 | |
1392 <p> | |
1393 The negation operator <b>not</b> always returns <b>false</b> or <b>true</b>. | |
1394 The conjunction operator <b>and</b> returns its first argument | |
1395 if this value is <b>false</b> or <b>nil</b>; | |
1396 otherwise, <b>and</b> returns its second argument. | |
1397 The disjunction operator <b>or</b> returns its first argument | |
1398 if this value is different from <b>nil</b> and <b>false</b>; | |
1399 otherwise, <b>or</b> returns its second argument. | |
1400 Both <b>and</b> and <b>or</b> use short-circuit evaluation; | |
1401 that is, | |
1402 the second operand is evaluated only if necessary. | |
1403 Here are some examples: | |
1404 </p> | |
1405 | |
1406 <pre> | |
1407 10 or 20 --> 10 | |
1408 10 or error() --> 10 | |
1409 nil or "a" --> "a" | |
1410 nil and 10 --> nil | |
1411 false and error() --> false | |
1412 false and nil --> false | |
1413 false or nil --> nil | |
1414 10 and 20 --> 20 | |
1415 </pre> | |
1416 | |
1417 <p> | |
1418 (In this manual, | |
1419 <code>--></code> indicates the result of the preceding expression.) | |
1420 </p> | |
1421 <% | |
1422 end | |
1423 } | |
1424 concatenation = { | |
1425 title = "Concatenation" | |
1426 content = function() | |
1427 %> | |
1428 <p> | |
1429 The string concatenation operator in Luan is | |
1430 denoted by two dots ('<code>..</code>'). | |
1431 All operands are converted to strings. | |
1432 </p> | |
1433 <% | |
1434 end | |
1435 } | |
1436 length = { | |
1437 title = "The Length Operator" | |
1438 content = function() | |
1439 %> | |
1440 <p> | |
1441 The length operator is denoted by the unary prefix operator <code>#</code>. | |
1442 The length of a string is its number of characters. | |
1443 The length of a binary is its number of bytes. | |
1444 </p> | |
1445 | |
1446 <p> | |
1447 A program can modify the behavior of the length operator for | |
1448 any table through the <code>__len</code> metamethod (see <a href="#meta">Metatables and Metamethods</a>). | |
1449 </p> | |
1450 | |
1451 <p> | |
1452 Unless a <code>__len</code> metamethod is given, | |
1453 the length of a table <code>t</code> is defined | |
1454 as the number of elements in <em>sequence</em>, | |
1455 that is, | |
1456 the size of the set of its positive numeric keys is equal to <em>{1..n}</em> | |
1457 for some non-negative integer <em>n</em>. | |
1458 In that case, <em>n</em> is its length. | |
1459 Note that a table like | |
1460 </p> | |
1461 | |
1462 <pre> | |
1463 {10, 20, nil, 40} | |
1464 </pre> | |
1465 | |
1466 <p> | |
1467 has a length of <code>2</code>, because that is the last key in sequence. | |
1468 </p> | |
1469 <% | |
1470 end | |
1471 } | |
1472 precedence = { | |
1473 title = "Precedence" | |
1474 content = function() | |
1475 %> | |
1476 <p> | |
1477 Operator precedence in Luan follows the table below, | |
1478 from lower to higher priority: | |
1479 </p> | |
1480 | |
1481 <pre> | |
1482 or | |
1483 and | |
1484 < > <= >= ~= == | |
1485 .. | |
1486 + - | |
1487 * / % | |
1488 unary operators (not # -) | |
1489 ^ | |
1490 </pre> | |
1491 | |
1492 <p> | |
1493 As usual, | |
1494 you can use parentheses to change the precedences of an expression. | |
1495 The concatenation ('<code>..</code>') and exponentiation ('<code>^</code>') | |
1496 operators are right associative. | |
1497 All other binary operators are left associative. | |
1498 </p> | |
1499 <% | |
1500 end | |
1501 } | |
1502 constructors = { | |
1503 title = "Table Constructors" | |
1504 content = function() | |
1505 %> | |
1506 <p> | |
1507 Table constructors are expressions that create tables. | |
1508 Every time a constructor is evaluated, a new table is created. | |
1509 A constructor can be used to create an empty table | |
1510 or to create a table and initialize some of its fields. | |
1511 The general syntax for constructors is | |
1512 </p> | |
1513 | |
1514 <pre> | |
1515 tableconstructor ::= ‘<b>{</b>’ fieldlist ‘<b>}</b>’ | |
1516 fieldlist ::= [field] {fieldsep [field]} | |
1517 field ::= ‘<b>[</b>’ exp ‘<b>]</b>’ ‘<b>=</b>’ exp | Name ‘<b>=</b>’ exp | exp | |
1518 fieldsep ::= ‘<b>,</b>’ | ‘<b>;</b>’ | <b>end_of_line</b> | |
1519 </pre> | |
1520 | |
1521 <p> | |
1522 Each field of the form <code>[exp1] = exp2</code> adds to the new table an entry | |
1523 with key <code>exp1</code> and value <code>exp2</code>. | |
1524 A field of the form <code>name = exp</code> is equivalent to | |
1525 <code>["name"] = exp</code>. | |
1526 Finally, fields of the form <code>exp</code> are equivalent to | |
1527 <code>[i] = exp</code>, where <code>i</code> are consecutive integers | |
1528 starting with 1. | |
1529 Fields in the other formats do not affect this counting. | |
1530 For example, | |
1531 </p> | |
1532 | |
1533 <pre> | |
1534 a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 } | |
1535 </pre> | |
1536 | |
1537 <p> | |
1538 is equivalent to | |
1539 </p> | |
1540 | |
1541 <pre> | |
1542 do | |
1543 local t = {} | |
1544 t[f(1)] = g | |
1545 t[1] = "x" -- 1st exp | |
1546 t[2] = "y" -- 2nd exp | |
1547 t.x = 1 -- t["x"] = 1 | |
1548 t[3] = f(x) -- 3rd exp | |
1549 t[30] = 23 | |
1550 t[4] = 45 -- 4th exp | |
1551 a = t | |
1552 end | |
1553 </pre> | |
1554 | |
1555 <p> | |
1556 The order of the assignments in a constructor is undefined. | |
1557 (This order would be relevant only when there are repeated keys.) | |
1558 </p> | |
1559 | |
1560 <p> | |
1561 If the last field in the list has the form <code>exp</code> | |
1562 and the expression is a function call or a vararg expression, | |
1563 then all values returned by this expression enter the list consecutively | |
1564 (see <a href="#fn_calls">Function Calls</a>). | |
1565 </p> | |
1566 | |
1567 <p> | |
1568 The field list can have an optional trailing separator, | |
1569 as a convenience for machine-generated code. | |
1570 </p> | |
1571 <% | |
1572 end | |
1573 } | |
1574 fn_calls = { | |
1575 title = "Function Calls" | |
1576 content = function() | |
1577 %> | |
1578 <p> | |
1579 A function call in Luan has the following syntax: | |
1580 </p> | |
1581 | |
1582 <pre> | |
1583 functioncall ::= prefixexp args | |
1584 </pre> | |
1585 | |
1586 <p> | |
1587 In a function call, | |
1588 first prefixexp and args are evaluated. | |
1589 The value of prefixexp must have type <em>function</em>. | |
1590 This function is called | |
1591 with the given arguments. | |
1592 </p> | |
1593 | |
1594 <p> | |
1595 Arguments have the following syntax: | |
1596 </p> | |
1597 | |
1598 <pre> | |
1599 args ::= ‘<b>(</b>’ [explist] ‘<b>)</b>’ | |
1600 args ::= tableconstructor | |
1601 args ::= LiteralString | |
1602 </pre> | |
1603 | |
1604 <p> | |
1605 All argument expressions are evaluated before the call. | |
1606 A call of the form <code>f{<em>fields</em>}</code> is | |
1607 syntactic sugar for <code>f({<em>fields</em>})</code>; | |
1608 that is, the argument list is a single new table. | |
1609 A call of the form <code>f'<em>string</em>'</code> | |
1610 (or <code>f"<em>string</em>"</code> or <code>f[[<em>string</em>]]</code>) | |
1611 is syntactic sugar for <code>f('<em>string</em>')</code>; | |
1612 that is, the argument list is a single literal string. | |
1613 </p> | |
1614 <% | |
1615 end | |
1616 } | |
1617 fn_def = { | |
1618 title = "Function Definitions" | |
1619 content = function() | |
1620 %> | |
1621 <p> | |
1622 The syntax for function definition is | |
1623 </p> | |
1624 | |
1625 <pre> | |
1626 functiondef ::= <b>function</b> funcbody | |
1627 funcbody ::= ‘<b>(</b>’ [parlist] ‘<b>)</b>’ block end_function | |
1628 end_function ::= <b>end_function</b> | <b>end</b> | |
1629 </pre> | |
1630 | |
1631 <p> | |
1632 The following syntactic sugar simplifies function definitions: | |
1633 </p> | |
1634 | |
1635 <pre> | |
1636 stat ::= <b>function</b> funcname funcbody | |
1637 stat ::= <b>local</b> <b>function</b> Name funcbody | |
1638 funcname ::= Name {‘<b>.</b>’ Name} [‘<b>:</b>’ Name] | |
1639 </pre> | |
1640 | |
1641 <p> | |
1642 The statement | |
1643 </p> | |
1644 | |
1645 <pre> | |
1646 function f () <em>body</em> end | |
1647 </pre> | |
1648 | |
1649 <p> | |
1650 translates to | |
1651 </p> | |
1652 | |
1653 <pre> | |
1654 f = function () <em>body</em> end | |
1655 </pre> | |
1656 | |
1657 <p> | |
1658 The statement | |
1659 <p> | |
1660 | |
1661 <pre> | |
1662 function t.a.b.c.f () <em>body</em> end | |
1663 </pre> | |
1664 | |
1665 <p> | |
1666 translates to | |
1667 </p> | |
1668 | |
1669 <pre> | |
1670 t.a.b.c.f = function () <em>body</em> end | |
1671 </pre> | |
1672 | |
1673 <p> | |
1674 The statement | |
1675 </p> | |
1676 | |
1677 <pre> | |
1678 local function f () <em>body</em> end | |
1679 </pre> | |
1680 | |
1681 <p> | |
1682 translates to | |
1683 </p> | |
1684 | |
1685 <pre> | |
1686 local f; f = function () <em>body</em> end | |
1687 </pre> | |
1688 | |
1689 <p> | |
1690 not to | |
1691 </p> | |
1692 | |
1693 <pre> | |
1694 local f = function () <em>body</em> end | |
1695 </pre> | |
1696 | |
1697 <p> | |
1698 (This only makes a difference when the body of the function | |
1699 contains references to <code>f</code>.) | |
1700 </p> | |
1701 | |
1702 <p> | |
1703 A function definition is an executable expression, | |
1704 whose value has type <em>function</em>. | |
1705 When Luan precompiles a chunk, | |
1706 all its function bodies are precompiled too. | |
1707 Then, whenever Luan executes the function definition, | |
1708 the function is <em>instantiated</em> (or <em>closed</em>). | |
1709 This function instance (or <em>closure</em>) | |
1710 is the final value of the expression. | |
1711 </p> | |
1712 | |
1713 <p> | |
1714 Parameters act as local variables that are | |
1715 initialized with the argument values: | |
1716 </p> | |
1717 | |
1718 <pre> | |
1719 parlist ::= namelist [‘<b>,</b>’ ‘<b>...</b>’] | ‘<b>...</b>’ | |
1720 </pre> | |
1721 | |
1722 <p> | |
1723 When a function is called, | |
1724 the list of arguments is adjusted to | |
1725 the length of the list of parameters if the list is too short, | |
1726 unless the function is a <em>vararg function</em>, | |
1727 which is indicated by three dots ('<code>...</code>') | |
1728 at the end of its parameter list. | |
1729 A vararg function does not adjust its argument list; | |
1730 instead, it collects all extra arguments and supplies them | |
1731 to the function through a <em>vararg expression</em>, | |
1732 which is also written as three dots. | |
1733 The value of this expression is a list of all actual extra arguments, | |
1734 similar to a function with multiple results. | |
1735 If a vararg expression is used inside another expression | |
1736 or in the middle of a list of expressions, | |
1737 then its return list is adjusted to one element. | |
1738 If the expression is used as the last element of a list of expressions, | |
1739 then no adjustment is made | |
1740 (unless that last expression is enclosed in parentheses). | |
1741 </p> | |
1742 | |
1743 <p> | |
1744 As an example, consider the following definitions: | |
1745 </p> | |
1746 <pre> | |
1747 function f(a, b) end | |
1748 function g(a, b, ...) end | |
1749 function r() return 1,2,3 end | |
1750 </pre> | |
1751 | |
1752 <p> | |
1753 Then, we have the following mapping from arguments to parameters and | |
1754 to the vararg expression: | |
1755 </p> | |
1756 <pre> | |
1757 CALL PARAMETERS | |
1758 | |
1759 f(3) a=3, b=nil | |
1760 f(3, 4) a=3, b=4 | |
1761 f(3, 4, 5) runtime error | |
1762 f(r(), 10) runtime error | |
1763 f(r()) runtime error | |
1764 | |
1765 g(3) a=3, b=nil, ... --> (nothing) | |
1766 g(3, 4) a=3, b=4, ... --> (nothing) | |
1767 g(3, 4, 5, 8) a=3, b=4, ... --> 5 8 | |
1768 g(5, r()) a=5, b=1, ... --> 2 3 | |
1769 </pre> | |
1770 | |
1771 <p> | |
1772 Results are returned using the <b>return</b> statement (see <a href="#control">Control Structures</a>). | |
1773 If control reaches the end of a function | |
1774 without encountering a <b>return</b> statement, | |
1775 then the function returns with no results. | |
1776 </p> | |
1777 <% | |
1778 end | |
1779 } | |
1780 } | |
1781 } | |
1782 visibility = { | |
1783 title = "Visibility Rules" | |
1784 content = function() | |
1785 %> | |
1786 <p> | |
1787 Luan is a lexically scoped language. | |
1788 The scope of a local variable begins at the first statement after | |
1789 its declaration and lasts until the last non-void statement | |
1790 of the innermost block that includes the declaration. | |
1791 Consider the following example: | |
1792 </p> | |
1793 <pre> | |
1794 x = 10 -- global variable | |
1795 do -- new block | |
1796 local x = x -- new 'x', with value 10 | |
1797 print(x) --> 10 | |
1798 x = x+1 | |
1799 do -- another block | |
1800 local x = x+1 -- another 'x' | |
1801 print(x) --> 12 | |
1802 end | |
1803 print(x) --> 11 | |
1804 end | |
1805 print(x) --> 10 (the global one) | |
1806 </pre> | |
1807 | |
1808 <p> | |
1809 Notice that, in a declaration like <code>local x = x</code>, | |
1810 the new <code>x</code> being declared is not in scope yet, | |
1811 and so the second <code>x</code> refers to the outside variable. | |
1812 </p> | |
1813 | |
1814 <p> | |
1815 Because of the lexical scoping rules, | |
1816 local variables can be freely accessed by functions | |
1817 defined inside their scope. | |
1818 A local variable used by an inner function is called | |
1819 an <em>upvalue</em>, or <em>external local variable</em>, | |
1820 inside the inner function. | |
1821 </p> | |
1822 | |
1823 <p> | |
1824 Notice that each execution of a <b>local</b> statement | |
1825 defines new local variables. | |
1826 Consider the following example: | |
1827 </p> | |
1828 <pre> | |
1829 a = {} | |
1830 local x = 20 | |
1831 for i=1,10 do | |
1832 local y = 0 | |
1833 a[i] = function () y=y+1; return x+y end | |
1834 end | |
1835 </pre> | |
1836 | |
1837 <p> | |
1838 The loop creates ten closures | |
1839 (that is, ten instances of the anonymous function). | |
1840 Each of these closures uses a different <code>y</code> variable, | |
1841 while all of them share the same <code>x</code>. | |
1842 </p> | |
1843 <% | |
1844 end | |
1845 } | |
1846 } | |
1847 } | |
1848 libs = { | |
1849 title = "Standard Libraries" | |
1850 content = function() | |
1851 %> | |
1852 <p> | |
1853 The standard Luan libraries provide useful functions | |
1854 that are implemented both in Java and in Luan itself. | |
1855 How each function is implemented shouldn't matter to the user. | |
1856 Some of these functions provide essential services to the language | |
1857 (e.g., <a href="#Luan.type"><code>type</code></a> and <a href="#Luan.get_metatable"><code>get_metatable</code></a>); | |
1858 others provide access to "outside" services (e.g., I/O). | |
1859 </p> | |
1860 <% | |
1861 end | |
1862 subs = { | |
1863 default_lib = { | |
1864 title = "Default Environment" | |
1865 content = function() | |
1866 %> | |
1867 <p> | |
1868 This is provided by default as a local variable for any Luan code as described in <a href="#env">Environments</a>. | |
1869 </p> | |
1870 <% | |
1871 end | |
1872 subs = { | |
1873 require = { | |
1874 title = "<code>require (mod_uri)</code>" | |
1875 content = function() | |
1876 %> | |
1877 <p> | |
1878 Example use: | |
1879 </p> | |
1880 <pre> | |
1881 local Table = require "luan:Table.luan" | |
1882 </pre> | |
1883 | |
1884 <p> | |
1885 Could be defined as: | |
1886 </p> | |
1887 <pre> | |
1888 local function require(mod_name) | |
1889 return <a href="#Package.load">Package.load</a>(mod_name) or <a href="#Luan.error">Luan.error</a>("module '"..mod_name.."' not found") | |
1890 end | |
1891 </pre> | |
1892 | |
1893 <p> | |
1894 A special case is: | |
1895 </p> | |
1896 <pre> | |
1897 require "java" | |
1898 </pre> | |
1899 | |
1900 <p> | |
1901 This enables Java in the current chunk if that chunk has permission to use Java. If the chunk doesn't have permission to use Java, then an error is thrown. | |
1902 </p> | |
1903 <% | |
1904 end | |
1905 } | |
1906 } | |
1907 } | |
1908 luan_lib = { | |
1909 title = "Basic Functions" | |
1910 content = function() | |
1911 %> | |
1912 <p> | |
1913 Include this library by: | |
1914 </p> | |
1915 <pre> | |
1916 local Luan = require "luan:Luan.luan" | |
1917 </pre> | |
1918 | |
1919 <p> | |
1920 The basic library provides basic functions to Luan that don't depend on other libaries. | |
1921 </p> | |
1922 <% | |
1923 end | |
1924 subs = { | |
1925 ["Luan.do_file"] = { | |
1926 title = "<code>Luan.do_file ([uri])</code>" | |
1927 content = function() | |
1928 %> | |
1929 <p> | |
1930 Could be defined as: | |
1931 </p> | |
1932 <pre> | |
1933 function Luan.do_file(uri) | |
1934 local fn = <a href="#Luan.load_file">Luan.load_file</a>(uri) or <a href="#Luan.error">Luan.error</a>("file '"..uri.."' not found") | |
1935 return fn() | |
1936 end | |
1937 </pre> | |
1938 <% | |
1939 end | |
1940 } | |
1941 ["Luan.error"] = { | |
1942 title = "<code>Luan.error (message)</code>" | |
1943 content = function() | |
1944 %> | |
1945 <p> | |
1946 Throws an error containing the message. | |
1947 </p> | |
1948 | |
1949 <p> | |
1950 Could be defined as: | |
1951 </p> | |
1952 <pre> | |
1953 function Luan.error(message) | |
1954 <a href="#Luan.new_error">Luan.new_error</a>(message).throw() | |
1955 end | |
1956 </pre> | |
1957 <% | |
1958 end | |
1959 } | |
1960 ["Luan.eval"] = { | |
1961 title = "<code>Luan.eval (text [, source_name [, env]])</code>" | |
1962 content = function() | |
1963 %> | |
1964 <p> | |
1965 Evaluates <code>text</code> as a Luan expression. | |
1966 </p> | |
1967 | |
1968 <p> | |
1969 Could be defined as: | |
1970 </p> | |
1971 <pre> | |
1972 function Luan.eval(text,source_name, env) | |
1973 return <a href="#Luan.load">Luan.load</a>( "return "..text, source_name or "eval", env )() | |
1974 end | |
1975 </pre> | |
1976 <% | |
1977 end | |
1978 } | |
1979 ["Luan.get_metatable"] = { | |
1980 title = "<code>Luan.get_metatable (table)</code>" | |
1981 content = function() | |
1982 %> | |
1983 <p> | |
1984 If <code>table</code> does not have a metatable, returns <b>nil</b>. | |
1985 Otherwise, | |
1986 if the table's metatable has a <code>"__metatable"</code> field, | |
1987 returns the associated value. | |
1988 Otherwise, returns the metatable of the given table. | |
1989 </p> | |
1990 <% | |
1991 end | |
1992 } | |
1993 ["Luan.hash_code"] = { | |
1994 title = "<code>Luan.hash_code (v)</code>" | |
1995 content = function() | |
1996 %> | |
1997 <p> | |
1998 Returns the hash code of <code>v</code>. | |
1999 </p> | |
2000 <% | |
2001 end | |
2002 } | |
2003 ["Luan.ipairs"] = { | |
2004 title = "<code>Luan.ipairs (t)</code>" | |
2005 content = function() | |
2006 %> | |
2007 <p> | |
2008 Returns an iterator function | |
2009 so that the construction | |
2010 </p> | |
2011 <pre> | |
2012 for i,v in ipairs(t) do <em>body</em> end | |
2013 </pre> | |
2014 | |
2015 <p> | |
2016 will iterate over the key–value pairs | |
2017 (<code>1,t[1]</code>), (<code>2,t[2]</code>), ..., | |
2018 up to the first nil value. | |
2019 </p> | |
2020 | |
2021 <p> | |
2022 Could be defined as: | |
2023 </p> | |
2024 <pre> | |
2025 function Luan.ipairs(t) | |
2026 local i = 0 | |
2027 return function() | |
2028 if i < #t then | |
2029 i = i + 1 | |
2030 return i, t[i] | |
2031 end | |
2032 end | |
2033 end | |
2034 </pre> | |
2035 <% | |
2036 end | |
2037 } | |
2038 ["Luan.load"] = { | |
2039 title = "<code>Luan.load (text, [source_name [, env [, persist]]])</code>" | |
2040 content = function() | |
2041 %> | |
2042 <p> | |
2043 Loads a chunk. | |
2044 </p> | |
2045 | |
2046 <p> | |
2047 The <code>text</code> is compiled. | |
2048 If there are no syntactic errors, | |
2049 returns the compiled chunk as a function; | |
2050 otherwise, throws an error. | |
2051 </p> | |
2052 | |
2053 <p> | |
2054 The <code>source_name</code> parameter is a string saying where the text came from. It is used to produce error messages. Defaults to "load". | |
2055 </p> | |
2056 | |
2057 <p> | |
2058 If the <code>env</code> parameter is supplied, it becomes the <code>_ENV</code> of the chunk. | |
2059 </p> | |
2060 | |
2061 <p> | |
2062 The <code>persist</code> parameter is a boolean which determines if the compiled code is persistently cached to a temporary file. Defaults to <code>false</code>. | |
2063 </p> | |
2064 <% | |
2065 end | |
2066 } | |
2067 ["Luan.load_file"] = { | |
2068 title = "<code>Luan.load_file (file_uri)</code>" | |
2069 content = function() | |
2070 %> | |
2071 <p> | |
2072 Similar to <a href="#Luan.load"><code>load</code></a>, | |
2073 but gets the chunk from file <code>file_uri</code>. | |
2074 <code>file_uri</code> can be a string or a uri table. | |
2075 </p> | |
2076 <% | |
2077 end | |
2078 } | |
2079 ["Luan.new_error"] = { | |
2080 title = "<code>Luan.new_error (message)</code>" | |
2081 content = function() | |
2082 %> | |
2083 <p> | |
2084 Creates a new error table containing the message assigned to "<code>message</code>". The error table also contains a <code>throw</code> function which throws the error. The table also contains a list of stack trace elements where each stack trace element is a table containing "<code>source</code>", "<code>line</code>", and possible "<code>call_to</code>". The table also has a metatable containing "<code>__to_string</code>" to render the error. | |
2085 </p> | |
2086 | |
2087 <p> | |
2088 To print the current stack trace, you could do: | |
2089 </p> | |
2090 <pre> | |
2091 Io.print( Luan.new_error "stack" ) | |
2092 </pre> | |
2093 <% | |
2094 end | |
2095 } | |
2096 ["Luan.pairs"] = { | |
2097 title = "<code>Luan.pairs (t)</code>" | |
2098 content = function() | |
2099 %> | |
2100 <p> | |
2101 If <code>t</code> has a metamethod <code>__pairs</code>, | |
2102 calls it with <code>t</code> as argument and returns the | |
2103 result from the call. | |
2104 </p> | |
2105 | |
2106 <p> | |
2107 Otherwise, | |
2108 returns a function | |
2109 so that the construction | |
2110 </p> | |
2111 <pre> | |
2112 for k,v in pairs(t) do <em>body</em> end | |
2113 </pre> | |
2114 | |
2115 <p> | |
2116 will iterate over all key–value pairs of table <code>t</code>. | |
2117 </p> | |
2118 <% | |
2119 end | |
2120 } | |
2121 ["Luan.range"] = { | |
2122 title = "<code>Luan.range (start, stop [, step])</code>" | |
2123 content = function() | |
2124 %> | |
2125 <p> | |
2126 Based on <a href="https://docs.python.org/2/library/functions.html#range">the Python range() function</a>, this lets one iterate through a sequence of numbers. | |
2127 </p> | |
2128 | |
2129 <p> | |
2130 Example use: | |
2131 </p> | |
2132 <pre> | |
2133 for i in range(1,10) do | |
2134 Io.print("count up:",i) | |
2135 end | |
2136 for i in range(10,0,-1) do | |
2137 Io.print("count down:",i) | |
2138 end | |
2139 </pre> | |
2140 | |
2141 <p> | |
2142 Could be defined as: | |
2143 </p> | |
2144 <pre> | |
2145 function Luan.range(start, stop, step) | |
2146 step = step or 1 | |
2147 step == 0 and <a href="#Luan.error">Luan.error</a> "bad argument #3 (step may not be zero)" | |
2148 local i = start | |
2149 return function() | |
2150 if step > 0 and i <= stop or step < 0 and i >= stop then | |
2151 local rtn = i | |
2152 i = i + step | |
2153 return rtn | |
2154 end | |
2155 end | |
2156 end | |
2157 </pre> | |
2158 <% | |
2159 end | |
2160 } | |
2161 ["Luan.raw_equal"] = { | |
2162 title = "<code>Luan.raw_equal (v1, v2)</code>" | |
2163 content = function() | |
2164 %> | |
2165 <p> | |
2166 Checks whether <code>v1</code> is equal to <code>v2</code>, | |
2167 without invoking any metamethod. | |
2168 Returns a boolean. | |
2169 </p> | |
2170 <% | |
2171 end | |
2172 } | |
2173 ["Luan.raw_get"] = { | |
2174 title = "<code>Luan.raw_get (table, index)</code>" | |
2175 content = function() | |
2176 %> | |
2177 <p> | |
2178 Gets the real value of <code>table[index]</code>, | |
2179 without invoking any metamethod. | |
2180 <code>table</code> must be a table; | |
2181 <code>index</code> may be any value. | |
2182 </p> | |
2183 <% | |
2184 end | |
2185 } | |
2186 ["Luan.raw_len"] = { | |
2187 title = "<code>Luan.raw_len (v)</code>" | |
2188 content = function() | |
2189 %> | |
2190 <p> | |
2191 Returns the length of the object <code>v</code>, | |
2192 which must be a table or a string, | |
2193 without invoking any metamethod. | |
2194 Returns an integer. | |
2195 </p> | |
2196 <% | |
2197 end | |
2198 } | |
2199 ["Luan.raw_set"] = { | |
2200 title = "<code>Luan.raw_set (table, index, value)</code>" | |
2201 content = function() | |
2202 %> | |
2203 <p> | |
2204 Sets the real value of <code>table[index]</code> to <code>value</code>, | |
2205 without invoking any metamethod. | |
2206 <code>table</code> must be a table, | |
2207 <code>index</code> any value different from <b>nil</b>, | |
2208 and <code>value</code> any Luan value. | |
2209 </p> | |
2210 <% | |
2211 end | |
2212 } | |
2213 ["Luan.set_metatable"] = { | |
2214 title = "<code>Luan.set_metatable (table, metatable)</code>" | |
2215 content = function() | |
2216 %> | |
2217 <p> | |
2218 Sets the metatable for the given table. | |
2219 If <code>metatable</code> is <b>nil</b>, | |
2220 removes the metatable of the given table. | |
2221 If the original metatable has a <code>"__metatable"</code> field, | |
2222 raises an error. | |
2223 </p> | |
2224 <% | |
2225 end | |
2226 } | |
2227 ["Luan.stringify"] = { | |
2228 title = "<code>Luan.stringify (v [,options])</code>" | |
2229 content = function() | |
2230 %> | |
2231 <p> | |
2232 Receives a value of any type and converts it to a string that is a Luan expression. <code>options</code> is a table. If <code>options.strict==true</code> then invalid types throw an error. Otherwise invalid types are represented but the resulting expression is invalid. If <code>options.number_types==true</code> then numbers will be wrapped in functions for their type. | |
2233 </p> | |
2234 <% | |
2235 end | |
2236 } | |
2237 ["Luan.to_string"] = { | |
2238 title = "<code>Luan.to_string (v)</code>" | |
2239 content = function() | |
2240 %> | |
2241 <p> | |
2242 Receives a value of any type and | |
2243 converts it to a string in a human-readable format. | |
2244 </p> | |
2245 | |
2246 <p> | |
2247 If the metatable of <code>v</code> has a <code>"__to_string"</code> field, | |
2248 then <code>to_string</code> calls the corresponding value | |
2249 with <code>v</code> as argument, | |
2250 and uses the result of the call as its result. | |
2251 </p> | |
2252 <% | |
2253 end | |
2254 } | |
2255 ["Luan.type"] = { | |
2256 title = "<code>Luan.type (v)</code>" | |
2257 content = function() | |
2258 %> | |
2259 <p> | |
2260 Returns the type of its only argument, coded as a string. | |
2261 The possible results of this function are | |
2262 "<code>nil</code>" (a string, not the value <b>nil</b>), | |
2263 "<code>number</code>", | |
2264 "<code>string</code>", | |
2265 "<code>binary</code>", | |
2266 "<code>boolean</code>", | |
2267 "<code>table</code>", | |
2268 "<code>function</code>", | |
2269 and "<code>java</code>". | |
2270 </p> | |
2271 <% | |
2272 end | |
2273 } | |
2274 ["Luan.values"] = { | |
2275 title = "<code>Luan.values (···)</code>" | |
2276 content = function() | |
2277 %> | |
2278 <p> | |
2279 Returns a function so that the construction | |
2280 </p> | |
2281 <pre> | |
2282 for i, v in Luan.values(···) do <em>body</em> end | |
2283 </pre> | |
2284 | |
2285 <p> | |
2286 will iterate over all values of <code>···</code>. | |
2287 </p> | |
2288 <% | |
2289 end | |
2290 } | |
2291 ["Luan.VERSION"] = { | |
2292 title = "<code>Luan.VERSION</code>" | |
2293 content = function() | |
2294 %> | |
2295 <p> | |
2296 A global variable (not a function) that | |
2297 holds a string containing the current Luan version. | |
2298 </p> | |
2299 <% | |
2300 end | |
2301 } | |
2302 } | |
2303 } | |
2304 package_lib = { | |
2305 title = "Modules" | |
2306 content = function() | |
2307 %> | |
2308 <p> | |
2309 Include this library by: | |
2310 </p> | |
2311 <pre> | |
2312 local Package = require "luan:Package.luan" | |
2313 </pre> | |
2314 | |
2315 <p> | |
2316 The package library provides basic | |
2317 facilities for loading modules in Luan. | |
2318 </p> | |
2319 <% | |
2320 end | |
2321 subs = { | |
2322 ["Package.load"] = { | |
2323 title = "<code>Package.load (mod_uri)</code>" | |
2324 content = function() | |
2325 %> | |
2326 <p> | |
2327 Loads the given module. | |
2328 The function starts by looking into the <a href="#Package.loaded"><code>Package.loaded</code></a> table | |
2329 to determine whether <code>mod_uri</code> is already loaded. | |
2330 If it is, then <code>Package.load</code> returns the value stored | |
2331 at <code>Package.loaded[mod_uri]</code>. | |
2332 Otherwise, it tries to load a new value for the module. | |
2333 </p> | |
2334 | |
2335 <p> | |
2336 To load a new value, <code>Package.load</code> first checks if <code>mod_uri</code> starts with "<b>java:</b>". If yes, then this is a Java class which is loaded by special Java code. | |
2337 </p> | |
2338 | |
2339 <p> | |
2340 Otherwise <code>Package.load</code> tries to read the text of the file referred to by <code>mod_uri</code>. If the file doesn't exist, then <code>Package.load</code> returns <b>false</b>. If the file exists, then its content is compiled into a chunk by calling <a href="#Luan.load"><code>Luan.load</code></a>. This chunk is run passing in <code>mod_uri</code> as an argument. The value returned by the chunk must not be <b>nil</b> and is loaded. | |
2341 </p> | |
2342 | |
2343 <p> | |
2344 If a new value for the module successful loaded, then it is stored in <code>Package.loaded[mod_uri]</code>. The value is returned. | |
2345 </p> | |
2346 <% | |
2347 end | |
2348 } | |
2349 ["Package.loaded"] = { | |
2350 title = "<code>Package.loaded</code>" | |
2351 content = function() | |
2352 %> | |
2353 <p> | |
2354 A table used by <a href="#Package.load"><code>Package.load</code></a> to control which | |
2355 modules are already loaded. | |
2356 When you load a module <code>mod_uri</code> and | |
2357 <code>Package.loaded[mod_uri]</code> is not <b>nil</b>, | |
2358 <a href="#Package.load"><code>Package.load</code></a> simply returns the value stored there. | |
2359 </p> | |
2360 | |
2361 <p> | |
2362 This variable is only a reference to the real table; | |
2363 assignments to this variable do not change the | |
2364 table used by <a href="#Package.load"><code>Package.load</code></a>. | |
1375 </p> | 2365 </p> |
1376 <% | 2366 <% |
1377 end | 2367 end |
1378 } | 2368 } |
1379 } | 2369 } |