Mercurial Hosting > luan
comparison website/src/manual.html.luan @ 512:d96944467ffc
update documentation for luan changes
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Fri, 22 May 2015 16:08:23 -0600 |
| parents | 7bc63886d4f2 |
| children | 0dfc01d8d42d |
comparison
equal
deleted
inserted
replaced
| 511:e3fb9768dbb3 | 512:d96944467ffc |
|---|---|
| 230 | 230 |
| 231 | 231 |
| 232 <h3 <%=heading_options%> ><a name="env">Environments</a></h3> | 232 <h3 <%=heading_options%> ><a name="env">Environments</a></h3> |
| 233 | 233 |
| 234 <p> | 234 <p> |
| 235 The environment of a chunk starts with only two local variables: <tt><a href="#require">require</a></tt> and <tt><a href="#java">java</a></tt>. These are functions are used to load and access libraries and other modules. All other variables must be added to the environment using <a href="http://localhost:8080/manual.html#local_stmt">local declarations</a>. | |
| 236 | |
| 237 <p> | |
| 235 As will be discussed in <a href="#vars">Variables</a> and <a href=#assignment">Assignment</a>, | 238 As will be discussed in <a href="#vars">Variables</a> and <a href=#assignment">Assignment</a>, |
| 236 any reference to a free name | 239 any reference to a free name |
| 237 (that is, a name not bound to any declaration) <tt>var</tt> | 240 (that is, a name not bound to any declaration) <tt>var</tt> |
| 238 is syntactically translated to <tt>_ENV.var</tt>. | 241 can be syntactically translated to <tt>_ENV.var</tt> if <tt>_ENV</tt> is defined. |
| 239 Moreover, every chunk is compiled in the scope of | |
| 240 an external local variable named <tt>_ENV</tt> (see <a href="#chunks">Chunks</a>), | |
| 241 so <tt>_ENV</tt> itself is never a free name in a chunk. | |
| 242 | |
| 243 | |
| 244 <p> | |
| 245 Despite the existence of this external <tt>_ENV</tt> variable and | |
| 246 the translation of free names, | |
| 247 <tt>_ENV</tt> is a completely regular name. | |
| 248 In particular, | |
| 249 you can define new variables and parameters with that name. | |
| 250 Each reference to a free name uses the <tt>_ENV</tt> that is | |
| 251 visible at that point in the program, | |
| 252 following the usual visibility rules of Luan (see <a href="#visibility">Visibility Rules</a>). | |
| 253 | |
| 254 | |
| 255 <p> | |
| 256 Any table used as the value of <tt>_ENV</tt> is called an <i>environment</i>. | |
| 257 | |
| 258 | |
| 259 <p> | |
| 260 When Luan loads a chunk, | |
| 261 the default value for its <tt>_ENV</tt> is an empty table. | |
| 262 | |
| 263 <p> | |
| 264 Luan also provides all chunks with two other local values: <tt>require</tt> and <tt>java</tt>. These are functions used to load and access libraries and other modules. | |
| 265 | |
| 266 | |
| 267 | 242 |
| 268 | 243 |
| 269 <h3 <%=heading_options%> ><a name="error">Error Handling</a></h3> | 244 <h3 <%=heading_options%> ><a name="error">Error Handling</a></h3> |
| 270 | 245 |
| 271 <p> | 246 <p> |
| 721 <p> | 696 <p> |
| 722 Variables are places that store values. | 697 Variables are places that store values. |
| 723 There are three kinds of variables in Luan: | 698 There are three kinds of variables in Luan: |
| 724 global variables, local variables, and table fields. | 699 global variables, local variables, and table fields. |
| 725 | 700 |
| 726 | |
| 727 <p> | 701 <p> |
| 728 A single name can denote a global variable or a local variable | 702 A single name can denote a global variable or a local variable |
| 729 (or a function's formal parameter, | 703 (or a function's formal parameter, |
| 730 which is a particular kind of local variable): | 704 which is a particular kind of local variable): |
| 731 | 705 |
| 734 </pre></tt></p> | 708 </pre></tt></p> |
| 735 | 709 |
| 736 <p> | 710 <p> |
| 737 Name denotes identifiers, as defined in <a href="#lex">Lexical Conventions</a>. | 711 Name denotes identifiers, as defined in <a href="#lex">Lexical Conventions</a>. |
| 738 | 712 |
| 739 | 713 <p> |
| 740 <p> | |
| 741 Any variable name is assumed to be global unless explicitly declared | |
| 742 as a local (see <a href="#local_stmt">Local Declarations</a>). | |
| 743 Local variables are <i>lexically scoped</i>: | 714 Local variables are <i>lexically scoped</i>: |
| 744 local variables can be freely accessed by functions | 715 local variables can be freely accessed by functions |
| 745 defined inside their scope (see <a href="#visibility">Visibility Rules</a>). | 716 defined inside their scope (see <a href="#visibility">Visibility Rules</a>). |
| 746 | 717 |
| 747 | 718 |
| 748 <p> | 719 <p> |
| 749 Before the first assignment to a variable, its value is <b>nil</b>. | 720 Before the first assignment to a variable, its value is <b>nil</b>. |
| 750 | |
| 751 | 721 |
| 752 <p> | 722 <p> |
| 753 Square brackets are used to index a table: | 723 Square brackets are used to index a table: |
| 754 | 724 |
| 755 <p><tt><pre> | 725 <p><tt><pre> |
| 773 <p><tt><pre> | 743 <p><tt><pre> |
| 774 var ::= prefixexp ‘<b>.</b>’ Name | 744 var ::= prefixexp ‘<b>.</b>’ Name |
| 775 </pre></tt></p> | 745 </pre></tt></p> |
| 776 | 746 |
| 777 <p> | 747 <p> |
| 778 An access to a global variable <tt>x</tt> | 748 Global variables are not available by default. To enable global variable, you must define <tt>_ENV</tt> as a local variable whose value is a table. If <tt>_ENV</tt> is not defined, then an unrecognized variable name will produce a compile error. If <tt>_ENV</tt> is defined then an access to an unrecognized variable name will be consider a global variable. So then an acces to global variable <tt>x</tt> |
| 779 is equivalent to <tt>_ENV.x</tt>. | 749 is equivalent to <tt>_ENV.x</tt>. |
| 780 Due to the way that chunks are compiled, | 750 Due to the way that chunks are compiled, |
| 781 <tt>_ENV</tt> is never a global name (see <a href="#env">Environments</a>). | 751 <tt>_ENV</tt> is never a global name (see <a href="#env">Environments</a>). |
| 782 | 752 |
| 783 | 753 |
| 941 | 911 |
| 942 <p> | 912 <p> |
| 943 An assignment to a global name <tt>x = val</tt> | 913 An assignment to a global name <tt>x = val</tt> |
| 944 is equivalent to the assignment | 914 is equivalent to the assignment |
| 945 <tt>_ENV.x = val</tt> (see <a href="#env">Environments</a>). | 915 <tt>_ENV.x = val</tt> (see <a href="#env">Environments</a>). |
| 946 | 916 Global names are only available when <tt>_ENV</tt> is defined. |
| 947 | 917 |
| 948 | 918 |
| 949 | 919 |
| 950 <h4 <%=heading_options%> ><a name="control">Control Structures</a></h4> | 920 <h4 <%=heading_options%> ><a name="control">Control Structures</a></h4> |
| 951 | 921 |
| 1820 | 1790 |
| 1821 <h3 <%=heading_options%> ><a name="default_lib">Default Environment</a></h3> | 1791 <h3 <%=heading_options%> ><a name="default_lib">Default Environment</a></h3> |
| 1822 | 1792 |
| 1823 <p> | 1793 <p> |
| 1824 These are provided by default as local variables for any Luan code as described in <a href="#env">Environments</a>. | 1794 These are provided by default as local variables for any Luan code as described in <a href="#env">Environments</a>. |
| 1825 | |
| 1826 | |
| 1827 <h4 <%=heading_options%> ><a name="_ENV"><tt>_ENV</tt></a></h4> | |
| 1828 | |
| 1829 <p> | |
| 1830 This is a table that holds the global variables of a module as described in <a href="#env">Environments</a>. | |
| 1831 | 1795 |
| 1832 | 1796 |
| 1833 <h4 <%=heading_options%> ><a name="require"><tt>java ()</tt></a></h4> | 1797 <h4 <%=heading_options%> ><a name="require"><tt>java ()</tt></a></h4> |
| 1834 | 1798 |
| 1835 <p> | 1799 <p> |
