Mercurial Hosting > luan
comparison website/src/manual.html.luan @ 395:a7cb58532846
work on manual
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Fri, 24 Apr 2015 17:42:22 -0600 |
| parents | 2f5cc9c2cbf0 |
| children | ba8b0aae6453 |
comparison
equal
deleted
inserted
replaced
| 394:c3d21bdc83b5 | 395:a7cb58532846 |
|---|---|
| 52 <li><a href="#vars">Variables</a></li> | 52 <li><a href="#vars">Variables</a></li> |
| 53 <li> | 53 <li> |
| 54 <a href="#stmts">Statements</a> | 54 <a href="#stmts">Statements</a> |
| 55 <ul> | 55 <ul> |
| 56 <li><a href="#blocks">Blocks</a></li> | 56 <li><a href="#blocks">Blocks</a></li> |
| 57 <li><a href="#chunks">Chunks</a></li> | |
| 58 <li><a href="#assignment">Assignment</a></li> | |
| 59 <li><a href="#control">Control Structures</a></li> | |
| 60 <li><a href="#for">For Statement</a></li> | |
| 61 <li><a href="#fn_stmt">Function Calls as Statements</a></li> | |
| 62 <li><a href="#local_stmt">Local Declarations</a></li> | |
| 63 </ul> | |
| 64 </li> | |
| 65 <li> | |
| 66 <a href="#expressions">Expressions</a> | |
| 67 <ul> | |
| 68 <li><a href="#arithmetic">Arithmetic Operators</a></li> | |
| 57 </ul> | 69 </ul> |
| 58 </li> | 70 </li> |
| 59 </ul> | 71 </ul> |
| 60 </div> | 72 </div> |
| 61 | 73 |
| 763 assignments, control structures, function calls, | 775 assignments, control structures, function calls, |
| 764 and variable declarations. | 776 and variable declarations. |
| 765 | 777 |
| 766 | 778 |
| 767 | 779 |
| 768 <h3 margin-top="1em"><a name="blocks">Blocks</a></h3> | 780 <h4 margin-top="1em"><a name="blocks">Blocks</a></h4> |
| 769 | 781 |
| 770 <p> | 782 <p> |
| 771 A block is a list of statements, | 783 A block is a list of statements, |
| 772 which are executed sequentially: | 784 which are executed sequentially: |
| 773 | 785 |
| 801 | 813 |
| 802 | 814 |
| 803 | 815 |
| 804 | 816 |
| 805 | 817 |
| 806 <h3>3.3.2 – <a name="3.3.2">Chunks</a></h3> | 818 <h4 margin-top="1em"><a name="chunks">Chunks</a></h4> |
| 807 | 819 |
| 808 <p> | 820 <p> |
| 809 The unit of compilation of Lua is called a <em>chunk</em>. | 821 The unit of compilation of Luan is called a <i>chunk</i>. |
| 810 Syntactically, | 822 Syntactically, |
| 811 a chunk is simply a block: | 823 a chunk is simply a block: |
| 812 | 824 |
| 813 <pre> | 825 <p><tt><pre> |
| 814 chunk ::= block | 826 chunk ::= block |
| 815 </pre> | 827 </pre></tt></p> |
| 816 | 828 |
| 817 <p> | 829 <p> |
| 818 Lua handles a chunk as the body of an anonymous function | 830 Luan handles a chunk as the body of an anonymous function |
| 819 with a variable number of arguments | 831 with a variable number of arguments |
| 820 (see <a href="#3.4.11">§3.4.11</a>). | 832 (see <a href="#3.4.11">§3.4.11</a>). |
| 821 As such, chunks can define local variables, | 833 As such, chunks can define local variables, |
| 822 receive arguments, and return values. | 834 receive arguments, and return values. |
| 823 Moreover, such anonymous function is compiled as in the | |
| 824 scope of an external local variable called <code>_ENV</code> (see <a href="#2.2">§2.2</a>). | |
| 825 The resulting function always has <code>_ENV</code> as its only upvalue, | |
| 826 even if it does not use that variable. | |
| 827 | 835 |
| 828 | 836 |
| 829 <p> | 837 <p> |
| 830 A chunk can be stored in a file or in a string inside the host program. | 838 A chunk can be stored in a file or in a string inside the host program. |
| 831 To execute a chunk, | 839 To execute a chunk, |
| 832 Lua first <em>loads</em> it, | 840 Luan first <i>loads</i> it, |
| 833 precompiling the chunk's code into instructions for a virtual machine, | 841 compiling the chunk's code, |
| 834 and then Lua executes the compiled code | 842 and then Luan executes the compiled code. |
| 835 with an interpreter for the virtual machine. | 843 |
| 836 | 844 |
| 837 | 845 |
| 838 <p> | 846 |
| 839 Chunks can also be precompiled into binary form; | 847 |
| 840 see program <code>luac</code> and function <a href="#pdf-string.dump"><code>string.dump</code></a> for details. | 848 <h4 margin-top="1em"><a name="assignment">Assignment</a></h4> |
| 841 Programs in source and compiled forms are interchangeable; | 849 |
| 842 Lua automatically detects the file type and acts accordingly (see <a href="#pdf-load"><code>load</code></a>). | 850 <p> |
| 843 | 851 Luan allows multiple assignments. |
| 844 | |
| 845 | |
| 846 | |
| 847 | |
| 848 <h3>3.3.3 – <a name="3.3.3">Assignment</a></h3> | |
| 849 | |
| 850 <p> | |
| 851 Lua allows multiple assignments. | |
| 852 Therefore, the syntax for assignment | 852 Therefore, the syntax for assignment |
| 853 defines a list of variables on the left side | 853 defines a list of variables on the left side |
| 854 and a list of expressions on the right side. | 854 and a list of expressions on the right side. |
| 855 The elements in both lists are separated by commas: | 855 The elements in both lists are separated by commas: |
| 856 | 856 |
| 857 <pre> | 857 <p><tt><pre> |
| 858 stat ::= varlist ‘<b>=</b>’ explist | 858 stat ::= varlist ‘<b>=</b>’ explist |
| 859 varlist ::= var {‘<b>,</b>’ var} | 859 varlist ::= var {‘<b>,</b>’ var} |
| 860 explist ::= exp {‘<b>,</b>’ exp} | 860 explist ::= exp {‘<b>,</b>’ exp} |
| 861 </pre><p> | 861 </pre></tt></p> |
| 862 | |
| 863 <p> | |
| 862 Expressions are discussed in <a href="#3.4">§3.4</a>. | 864 Expressions are discussed in <a href="#3.4">§3.4</a>. |
| 863 | 865 |
| 864 | 866 |
| 865 <p> | 867 <p> |
| 866 Before the assignment, | 868 Before the assignment, |
| 867 the list of values is <em>adjusted</em> to the length of | 869 the list of values is <i>adjusted</i> to the length of |
| 868 the list of variables. | 870 the list of variables. |
| 869 If there are more values than needed, | 871 If there are more values than needed, |
| 870 the excess values are thrown away. | 872 the excess values are thrown away. |
| 871 If there are fewer values than needed, | 873 If there are fewer values than needed, |
| 872 the list is extended with as many <b>nil</b>'s as needed. | 874 the list is extended with as many <b>nil</b>'s as needed. |
| 879 <p> | 881 <p> |
| 880 The assignment statement first evaluates all its expressions | 882 The assignment statement first evaluates all its expressions |
| 881 and only then the assignments are performed. | 883 and only then the assignments are performed. |
| 882 Thus the code | 884 Thus the code |
| 883 | 885 |
| 884 <pre> | 886 <p><tt><pre> |
| 885 i = 3 | 887 i = 3 |
| 886 i, a[i] = i+1, 20 | 888 i, a[i] = i+1, 20 |
| 887 </pre><p> | 889 </pre></tt></p> |
| 888 sets <code>a[3]</code> to 20, without affecting <code>a[4]</code> | 890 |
| 889 because the <code>i</code> in <code>a[i]</code> is evaluated (to 3) | 891 <p> |
| 892 sets <tt>a[3]</tt> to 20, without affecting <tt>a[4]</tt> | |
| 893 because the <tt>i</tt> in <tt>a[i]</tt> is evaluated (to 3) | |
| 890 before it is assigned 4. | 894 before it is assigned 4. |
| 891 Similarly, the line | 895 Similarly, the line |
| 892 | 896 |
| 893 <pre> | 897 <p><tt><pre> |
| 894 x, y = y, x | 898 x, y = y, x |
| 895 </pre><p> | 899 </pre></tt></p> |
| 896 exchanges the values of <code>x</code> and <code>y</code>, | 900 |
| 901 <p> | |
| 902 exchanges the values of <tt>x</tt> and <tt>y</tt>, | |
| 897 and | 903 and |
| 898 | 904 |
| 899 <pre> | 905 <p><tt><pre> |
| 900 x, y, z = y, z, x | 906 x, y, z = y, z, x |
| 901 </pre><p> | 907 </pre></tt></p> |
| 902 cyclically permutes the values of <code>x</code>, <code>y</code>, and <code>z</code>. | 908 |
| 909 <p> | |
| 910 cyclically permutes the values of <tt>x</tt>, <tt>y</tt>, and <tt>z</tt>. | |
| 903 | 911 |
| 904 | 912 |
| 905 <p> | 913 <p> |
| 906 The meaning of assignments to global variables | 914 The meaning of assignments to global variables |
| 907 and table fields can be changed via metatables. | 915 and table fields can be changed via metatables. |
| 908 An assignment to an indexed variable <code>t[i] = val</code> is equivalent to | 916 An assignment to an indexed variable <tt>t[i] = val</tt> is equivalent to |
| 909 <code>settable_event(t,i,val)</code>. | 917 <tt>settable_event(t,i,val)</tt>. |
| 910 (See <a href="#2.4">§2.4</a> for a complete description of the | 918 (See <a href="#2.4">§2.4</a> for a complete description of the |
| 911 <code>settable_event</code> function. | 919 <tt>settable_event</tt> function. |
| 912 This function is not defined or callable in Lua. | 920 This function is not defined or callable in Luan. |
| 913 We use it here only for explanatory purposes.) | 921 We use it here only for explanatory purposes.) |
| 914 | 922 |
| 915 | 923 |
| 916 <p> | 924 <p> |
| 917 An assignment to a global name <code>x = val</code> | 925 An assignment to a global name <tt>x = val</tt> |
| 918 is equivalent to the assignment | 926 is equivalent to the assignment |
| 919 <code>_ENV.x = val</code> (see <a href="#2.2">§2.2</a>). | 927 <tt>_ENV.x = val</tt> (see <a href="#2.2">§2.2</a>). |
| 920 | 928 |
| 921 | 929 |
| 922 | 930 |
| 923 | 931 |
| 924 | 932 <h4 margin-top="1em"><a name="control">Control Structures</a></h4> |
| 925 <h3>3.3.4 – <a name="3.3.4">Control Structures</a></h3><p> | 933 |
| 934 <p> | |
| 926 The control structures | 935 The control structures |
| 927 <b>if</b>, <b>while</b>, and <b>repeat</b> have the usual meaning and | 936 <b>if</b>, <b>while</b>, and <b>repeat</b> have the usual meaning and |
| 928 familiar syntax: | 937 familiar syntax: |
| 929 | 938 |
| 930 | 939 <p><tt><pre> |
| 931 | |
| 932 | |
| 933 <pre> | |
| 934 stat ::= <b>while</b> exp <b>do</b> block <b>end</b> | 940 stat ::= <b>while</b> exp <b>do</b> block <b>end</b> |
| 935 stat ::= <b>repeat</b> block <b>until</b> exp | 941 stat ::= <b>repeat</b> block <b>until</b> exp |
| 936 stat ::= <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b> | 942 stat ::= <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b> |
| 937 </pre><p> | 943 </pre></tt></p> |
| 938 Lua also has a <b>for</b> statement, in two flavors (see <a href="#3.3.5">§3.3.5</a>). | 944 |
| 945 <p> | |
| 946 Luan also has a <b>for</b> statement (see <a href="#3.3.5">§3.3.5</a>). | |
| 939 | 947 |
| 940 | 948 |
| 941 <p> | 949 <p> |
| 942 The condition expression of a | 950 The condition expression of a |
| 943 control structure can return any value. | 951 control structure must be a boolean. |
| 944 Both <b>false</b> and <b>nil</b> are considered false. | 952 Any other value type will produce an error. |
| 945 All values different from <b>nil</b> and <b>false</b> are considered true | 953 This helps catch errors and makes code more readable. |
| 946 (in particular, the number 0 and the empty string are also true). | |
| 947 | 954 |
| 948 | 955 |
| 949 <p> | 956 <p> |
| 950 In the <b>repeat</b>–<b>until</b> loop, | 957 In the <b>repeat</b>–<b>until</b> loop, |
| 951 the inner block does not end at the <b>until</b> keyword, | 958 the inner block does not end at the <b>until</b> keyword, |
| 953 So, the condition can refer to local variables | 960 So, the condition can refer to local variables |
| 954 declared inside the loop block. | 961 declared inside the loop block. |
| 955 | 962 |
| 956 | 963 |
| 957 <p> | 964 <p> |
| 958 The <b>goto</b> statement transfers the program control to a label. | |
| 959 For syntactical reasons, | |
| 960 labels in Lua are considered statements too: | |
| 961 | |
| 962 | |
| 963 | |
| 964 <pre> | |
| 965 stat ::= <b>goto</b> Name | |
| 966 stat ::= label | |
| 967 label ::= ‘<b>::</b>’ Name ‘<b>::</b>’ | |
| 968 </pre> | |
| 969 | |
| 970 <p> | |
| 971 A label is visible in the entire block where it is defined, | |
| 972 except | |
| 973 inside nested blocks where a label with the same name is defined and | |
| 974 inside nested functions. | |
| 975 A goto may jump to any visible label as long as it does not | |
| 976 enter into the scope of a local variable. | |
| 977 | |
| 978 | |
| 979 <p> | |
| 980 Labels and empty statements are called <em>void statements</em>, | |
| 981 as they perform no actions. | |
| 982 | |
| 983 | |
| 984 <p> | |
| 985 The <b>break</b> statement terminates the execution of a | 965 The <b>break</b> statement terminates the execution of a |
| 986 <b>while</b>, <b>repeat</b>, or <b>for</b> loop, | 966 <b>while</b>, <b>repeat</b>, or <b>for</b> loop, |
| 987 skipping to the next statement after the loop: | 967 skipping to the next statement after the loop: |
| 988 | 968 |
| 989 | 969 |
| 990 <pre> | 970 <p><tt><pre> |
| 991 stat ::= <b>break</b> | 971 stat ::= <b>break</b> |
| 992 </pre><p> | 972 </pre></tt></p> |
| 973 | |
| 974 <p> | |
| 993 A <b>break</b> ends the innermost enclosing loop. | 975 A <b>break</b> ends the innermost enclosing loop. |
| 994 | 976 |
| 995 | 977 |
| 996 <p> | 978 <p> |
| 997 The <b>return</b> statement is used to return values | 979 The <b>return</b> statement is used to return values |
| 999 (which is an anonymous function). | 981 (which is an anonymous function). |
| 1000 | 982 |
| 1001 Functions can return more than one value, | 983 Functions can return more than one value, |
| 1002 so the syntax for the <b>return</b> statement is | 984 so the syntax for the <b>return</b> statement is |
| 1003 | 985 |
| 1004 <pre> | 986 <p><tt><pre> |
| 1005 stat ::= <b>return</b> [explist] [‘<b>;</b>’] | 987 stat ::= <b>return</b> [explist] [‘<b>;</b>’] |
| 1006 </pre> | 988 </pre></tt></p> |
| 1007 | 989 |
| 1008 <p> | 990 |
| 1009 The <b>return</b> statement can only be written | 991 |
| 1010 as the last statement of a block. | 992 |
| 1011 If it is really necessary to <b>return</b> in the middle of a block, | 993 <h4 margin-top="1em"><a name="for">For Statement</a></h4> |
| 1012 then an explicit inner block can be used, | 994 |
| 1013 as in the idiom <code>do return end</code>, | 995 <p> |
| 1014 because now <b>return</b> is the last statement in its (inner) block. | 996 The <b>for</b> statement works over functions, |
| 1015 | 997 called <i>iterators</i>. |
| 1016 | 998 On each iteration, the iterator function is called to produce a new value, |
| 1017 | 999 stopping when this new value is <b>nil</b>. |
| 1018 | 1000 The <b>for</b> loop has the following syntax: |
| 1019 | 1001 |
| 1020 <h3>3.3.5 – <a name="3.3.5">For Statement</a></h3> | 1002 <p><tt><pre> |
| 1021 | 1003 stat ::= <b>for</b> namelist <b>in</b> exp <b>do</b> block <b>end</b> |
| 1022 <p> | 1004 namelist ::= Name {‘<b>,</b>’ Name} |
| 1023 | 1005 </pre></tt></p> |
| 1024 The <b>for</b> statement has two forms: | 1006 |
| 1025 one numeric and one generic. | 1007 <p> |
| 1026 | 1008 A <b>for</b> statement like |
| 1027 | 1009 |
| 1028 <p> | 1010 <p><tt><pre> |
| 1029 The numeric <b>for</b> loop repeats a block of code while a | 1011 for <i>var_1</i>, ···, <i>var_n</i> in <i>exp</i> do <i>block</i> end |
| 1030 control variable runs through an arithmetic progression. | 1012 </pre></tt></p> |
| 1031 It has the following syntax: | 1013 |
| 1032 | 1014 <p> |
| 1033 <pre> | |
| 1034 stat ::= <b>for</b> Name ‘<b>=</b>’ exp ‘<b>,</b>’ exp [‘<b>,</b>’ exp] <b>do</b> block <b>end</b> | |
| 1035 </pre><p> | |
| 1036 The <em>block</em> is repeated for <em>name</em> starting at the value of | |
| 1037 the first <em>exp</em>, until it passes the second <em>exp</em> by steps of the | |
| 1038 third <em>exp</em>. | |
| 1039 More precisely, a <b>for</b> statement like | |
| 1040 | |
| 1041 <pre> | |
| 1042 for v = <em>e1</em>, <em>e2</em>, <em>e3</em> do <em>block</em> end | |
| 1043 </pre><p> | |
| 1044 is equivalent to the code: | 1015 is equivalent to the code: |
| 1045 | 1016 |
| 1046 <pre> | 1017 <p><tt><pre> |
| 1047 do | 1018 do |
| 1048 local <em>var</em>, <em>limit</em>, <em>step</em> = tonumber(<em>e1</em>), tonumber(<em>e2</em>), tonumber(<em>e3</em>) | 1019 local <i>f</i> = <i>exp</i> |
| 1049 if not (<em>var</em> and <em>limit</em> and <em>step</em>) then error() end | |
| 1050 <em>var</em> = <em>var</em> - <em>step</em> | |
| 1051 while true do | 1020 while true do |
| 1052 <em>var</em> = <em>var</em> + <em>step</em> | 1021 local <i>var_1</i>, ···, <i>var_n</i> = <i>f</i>() |
| 1053 if (<em>step</em> >= 0 and <em>var</em> > <em>limit</em>) or (<em>step</em> < 0 and <em>var</em> < <em>limit</em>) then | 1022 if <i>var_1</i> == nil then break end |
| 1054 break | 1023 <i>block</i> |
| 1055 end | |
| 1056 local v = <em>var</em> | |
| 1057 <em>block</em> | |
| 1058 end | 1024 end |
| 1059 end | 1025 end |
| 1060 </pre> | 1026 </pre></tt></p> |
| 1061 | 1027 |
| 1062 <p> | 1028 <p> |
| 1063 Note the following: | 1029 Note the following: |
| 1064 | 1030 |
| 1065 <ul> | 1031 <ul> |
| 1066 | 1032 |
| 1067 <li> | 1033 <li> |
| 1068 All three control expressions are evaluated only once, | 1034 <tt><i>exp</i></tt> is evaluated only once. |
| 1069 before the loop starts. | 1035 Its result is an <i>iterator</i> function. |
| 1070 They must all result in numbers. | |
| 1071 </li> | 1036 </li> |
| 1072 | 1037 |
| 1073 <li> | 1038 <li> |
| 1074 <code><em>var</em></code>, <code><em>limit</em></code>, and <code><em>step</em></code> are invisible variables. | 1039 <tt><i>f</i></tt> is an invisible variable. |
| 1075 The names shown here are for explanatory purposes only. | 1040 The name is here for explanatory purposes only. |
| 1076 </li> | |
| 1077 | |
| 1078 <li> | |
| 1079 If the third expression (the step) is absent, | |
| 1080 then a step of 1 is used. | |
| 1081 </li> | |
| 1082 | |
| 1083 <li> | |
| 1084 You can use <b>break</b> and <b>goto</b> to exit a <b>for</b> loop. | |
| 1085 </li> | |
| 1086 | |
| 1087 <li> | |
| 1088 The loop variable <code>v</code> is local to the loop body. | |
| 1089 If you need its value after the loop, | |
| 1090 assign it to another variable before exiting the loop. | |
| 1091 </li> | |
| 1092 | |
| 1093 </ul> | |
| 1094 | |
| 1095 <p> | |
| 1096 The generic <b>for</b> statement works over functions, | |
| 1097 called <em>iterators</em>. | |
| 1098 On each iteration, the iterator function is called to produce a new value, | |
| 1099 stopping when this new value is <b>nil</b>. | |
| 1100 The generic <b>for</b> loop has the following syntax: | |
| 1101 | |
| 1102 <pre> | |
| 1103 stat ::= <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b> | |
| 1104 namelist ::= Name {‘<b>,</b>’ Name} | |
| 1105 </pre><p> | |
| 1106 A <b>for</b> statement like | |
| 1107 | |
| 1108 <pre> | |
| 1109 for <em>var_1</em>, ···, <em>var_n</em> in <em>explist</em> do <em>block</em> end | |
| 1110 </pre><p> | |
| 1111 is equivalent to the code: | |
| 1112 | |
| 1113 <pre> | |
| 1114 do | |
| 1115 local <em>f</em>, <em>s</em>, <em>var</em> = <em>explist</em> | |
| 1116 while true do | |
| 1117 local <em>var_1</em>, ···, <em>var_n</em> = <em>f</em>(<em>s</em>, <em>var</em>) | |
| 1118 if <em>var_1</em> == nil then break end | |
| 1119 <em>var</em> = <em>var_1</em> | |
| 1120 <em>block</em> | |
| 1121 end | |
| 1122 end | |
| 1123 </pre><p> | |
| 1124 Note the following: | |
| 1125 | |
| 1126 <ul> | |
| 1127 | |
| 1128 <li> | |
| 1129 <code><em>explist</em></code> is evaluated only once. | |
| 1130 Its results are an <em>iterator</em> function, | |
| 1131 a <em>state</em>, | |
| 1132 and an initial value for the first <em>iterator variable</em>. | |
| 1133 </li> | |
| 1134 | |
| 1135 <li> | |
| 1136 <code><em>f</em></code>, <code><em>s</em></code>, and <code><em>var</em></code> are invisible variables. | |
| 1137 The names are here for explanatory purposes only. | |
| 1138 </li> | 1041 </li> |
| 1139 | 1042 |
| 1140 <li> | 1043 <li> |
| 1141 You can use <b>break</b> to exit a <b>for</b> loop. | 1044 You can use <b>break</b> to exit a <b>for</b> loop. |
| 1142 </li> | 1045 </li> |
| 1143 | 1046 |
| 1144 <li> | 1047 <li> |
| 1145 The loop variables <code><em>var_i</em></code> are local to the loop; | 1048 The loop variables <tt><i>var_i</i></tt> are local to the loop; |
| 1146 you cannot use their values after the <b>for</b> ends. | 1049 you cannot use their values after the <b>for</b> ends. |
| 1147 If you need these values, | 1050 If you need these values, |
| 1148 then assign them to other variables before breaking or exiting the loop. | 1051 then assign them to other variables before breaking or exiting the loop. |
| 1149 </li> | 1052 </li> |
| 1150 | 1053 |
| 1151 </ul> | 1054 </ul> |
| 1152 | 1055 |
| 1153 | 1056 |
| 1154 | 1057 |
| 1155 | 1058 |
| 1156 <h3>3.3.6 – <a name="3.3.6">Function Calls as Statements</a></h3><p> | 1059 <h4 margin-top="1em"><a name="fn_stmt">Function Calls as Statements</a></h4> |
| 1060 | |
| 1061 <p> | |
| 1157 To allow possible side-effects, | 1062 To allow possible side-effects, |
| 1158 function calls can be executed as statements: | 1063 function calls can be executed as statements: |
| 1159 | 1064 |
| 1160 <pre> | 1065 <p><tt><pre> |
| 1161 stat ::= functioncall | 1066 stat ::= functioncall |
| 1162 </pre><p> | 1067 </pre></tt></p> |
| 1068 | |
| 1069 <p> | |
| 1163 In this case, all returned values are thrown away. | 1070 In this case, all returned values are thrown away. |
| 1164 Function calls are explained in <a href="#3.4.10">§3.4.10</a>. | 1071 Function calls are explained in <a href="#3.4.10">§3.4.10</a>. |
| 1165 | 1072 |
| 1166 | 1073 |
| 1167 | 1074 |
| 1168 | 1075 <h4 margin-top="1em"><a name="local_stmt">Local Declarations</a></h4> |
| 1169 | 1076 |
| 1170 <h3>3.3.7 – <a name="3.3.7">Local Declarations</a></h3><p> | 1077 <p> |
| 1171 Local variables can be declared anywhere inside a block. | 1078 Local variables can be declared anywhere inside a block. |
| 1172 The declaration can include an initial assignment: | 1079 The declaration can include an initial assignment: |
| 1173 | 1080 |
| 1174 <pre> | 1081 <p><tt><pre> |
| 1175 stat ::= <b>local</b> namelist [‘<b>=</b>’ explist] | 1082 stat ::= <b>local</b> namelist [‘<b>=</b>’ explist] |
| 1176 </pre><p> | 1083 </pre></tt></p> |
| 1084 | |
| 1085 <p> | |
| 1177 If present, an initial assignment has the same semantics | 1086 If present, an initial assignment has the same semantics |
| 1178 of a multiple assignment (see <a href="#3.3.3">§3.3.3</a>). | 1087 of a multiple assignment (see <a href="#3.3.3">§3.3.3</a>). |
| 1179 Otherwise, all variables are initialized with <b>nil</b>. | 1088 Otherwise, all variables are initialized with <b>nil</b>. |
| 1180 | 1089 |
| 1181 | 1090 |
| 1188 The visibility rules for local variables are explained in <a href="#3.5">§3.5</a>. | 1097 The visibility rules for local variables are explained in <a href="#3.5">§3.5</a>. |
| 1189 | 1098 |
| 1190 | 1099 |
| 1191 | 1100 |
| 1192 | 1101 |
| 1193 | 1102 <h3 margin-top="1em"><a name="expressions">Expressions</a></h3> |
| 1194 | 1103 |
| 1195 | 1104 <p> |
| 1196 <h2>3.4 – <a name="3.4">Expressions</a></h2> | 1105 The basic expressions in Luan are the following: |
| 1197 | 1106 |
| 1198 <p> | 1107 <p><tt><pre> |
| 1199 The basic expressions in Lua are the following: | |
| 1200 | |
| 1201 <pre> | |
| 1202 exp ::= prefixexp | 1108 exp ::= prefixexp |
| 1203 exp ::= <b>nil</b> | <b>false</b> | <b>true</b> | 1109 exp ::= <b>nil</b> | <b>false</b> | <b>true</b> |
| 1204 exp ::= Numeral | 1110 exp ::= Numeral |
| 1205 exp ::= LiteralString | 1111 exp ::= LiteralString |
| 1206 exp ::= functiondef | 1112 exp ::= functiondef |
| 1207 exp ::= tableconstructor | 1113 exp ::= tableconstructor |
| 1208 exp ::= ‘<b>...</b>’ | 1114 exp ::= ‘<b>...</b>’ |
| 1209 exp ::= exp binop exp | 1115 exp ::= exp binop exp |
| 1210 exp ::= unop exp | 1116 exp ::= unop exp |
| 1211 prefixexp ::= var | functioncall | ‘<b>(</b>’ exp ‘<b>)</b>’ | 1117 prefixexp ::= var | functioncall | ‘<b>(</b>’ exp ‘<b>)</b>’ |
| 1212 </pre> | 1118 </pre></tt></p> |
| 1213 | 1119 |
| 1214 <p> | 1120 <p> |
| 1215 Numerals and literal strings are explained in <a href="#3.1">§3.1</a>; | 1121 Numerals and literal strings are explained in <a href="#3.1">§3.1</a>; |
| 1216 variables are explained in <a href="#3.2">§3.2</a>; | 1122 variables are explained in <a href="#3.2">§3.2</a>; |
| 1217 function definitions are explained in <a href="#3.4.11">§3.4.11</a>; | 1123 function definitions are explained in <a href="#3.4.11">§3.4.11</a>; |
| 1218 function calls are explained in <a href="#3.4.10">§3.4.10</a>; | 1124 function calls are explained in <a href="#3.4.10">§3.4.10</a>; |
| 1219 table constructors are explained in <a href="#3.4.9">§3.4.9</a>. | 1125 table constructors are explained in <a href="#3.4.9">§3.4.9</a>. |
| 1220 Vararg expressions, | 1126 Vararg expressions, |
| 1221 denoted by three dots ('<code>...</code>'), can only be used when | 1127 denoted by three dots ('<tt>...</tt>'), can only be used when |
| 1222 directly inside a vararg function; | 1128 directly inside a vararg function; |
| 1223 they are explained in <a href="#3.4.11">§3.4.11</a>. | 1129 they are explained in <a href="#3.4.11">§3.4.11</a>. |
| 1224 | 1130 |
| 1225 | 1131 |
| 1226 <p> | 1132 <p> |
| 1227 Binary operators comprise arithmetic operators (see <a href="#3.4.1">§3.4.1</a>), | 1133 Binary operators comprise arithmetic operators (see <a href="#3.4.1">§3.4.1</a>), |
| 1228 bitwise operators (see <a href="#3.4.2">§3.4.2</a>), | |
| 1229 relational operators (see <a href="#3.4.4">§3.4.4</a>), logical operators (see <a href="#3.4.5">§3.4.5</a>), | 1134 relational operators (see <a href="#3.4.4">§3.4.4</a>), logical operators (see <a href="#3.4.5">§3.4.5</a>), |
| 1230 and the concatenation operator (see <a href="#3.4.6">§3.4.6</a>). | 1135 and the concatenation operator (see <a href="#3.4.6">§3.4.6</a>). |
| 1231 Unary operators comprise the unary minus (see <a href="#3.4.1">§3.4.1</a>), | 1136 Unary operators comprise the unary minus (see <a href="#3.4.1">§3.4.1</a>), |
| 1232 the unary bitwise not (see <a href="#3.4.2">§3.4.2</a>), | |
| 1233 the unary logical <b>not</b> (see <a href="#3.4.5">§3.4.5</a>), | 1137 the unary logical <b>not</b> (see <a href="#3.4.5">§3.4.5</a>), |
| 1234 and the unary <em>length operator</em> (see <a href="#3.4.7">§3.4.7</a>). | 1138 and the unary <em>length operator</em> (see <a href="#3.4.7">§3.4.7</a>). |
| 1235 | 1139 |
| 1236 | 1140 |
| 1237 <p> | 1141 <p> |
| 1242 If an expression is used as the last (or the only) element | 1146 If an expression is used as the last (or the only) element |
| 1243 of a list of expressions, | 1147 of a list of expressions, |
| 1244 then no adjustment is made | 1148 then no adjustment is made |
| 1245 (unless the expression is enclosed in parentheses). | 1149 (unless the expression is enclosed in parentheses). |
| 1246 In all other contexts, | 1150 In all other contexts, |
| 1247 Lua adjusts the result list to one element, | 1151 Luan adjusts the result list to one element, |
| 1248 either discarding all values except the first one | 1152 either discarding all values except the first one |
| 1249 or adding a single <b>nil</b> if there are no values. | 1153 or adding a single <b>nil</b> if there are no values. |
| 1250 | 1154 |
| 1251 | 1155 |
| 1252 <p> | 1156 <p> |
| 1253 Here are some examples: | 1157 Here are some examples: |
| 1254 | 1158 |
| 1255 <pre> | 1159 <p><tt><pre> |
| 1256 f() -- adjusted to 0 results | 1160 f() -- adjusted to 0 results |
| 1257 g(f(), x) -- f() is adjusted to 1 result | 1161 g(f(), x) -- f() is adjusted to 1 result |
| 1258 g(x, f()) -- g gets x plus all results from f() | 1162 g(x, f()) -- g gets x plus all results from f() |
| 1259 a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil) | 1163 a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil) |
| 1260 a,b = ... -- a gets the first vararg parameter, b gets | 1164 a,b = ... -- a gets the first vararg parameter, b gets |
| 1267 return ... -- returns all received vararg parameters | 1171 return ... -- returns all received vararg parameters |
| 1268 return x,y,f() -- returns x, y, and all results from f() | 1172 return x,y,f() -- returns x, y, and all results from f() |
| 1269 {f()} -- creates a list with all results from f() | 1173 {f()} -- creates a list with all results from f() |
| 1270 {...} -- creates a list with all vararg parameters | 1174 {...} -- creates a list with all vararg parameters |
| 1271 {f(), nil} -- f() is adjusted to 1 result | 1175 {f(), nil} -- f() is adjusted to 1 result |
| 1272 </pre> | 1176 </pre></tt></p> |
| 1273 | 1177 |
| 1274 <p> | 1178 <p> |
| 1275 Any expression enclosed in parentheses always results in only one value. | 1179 Any expression enclosed in parentheses always results in only one value. |
| 1276 Thus, | 1180 Thus, |
| 1277 <code>(f(x,y,z))</code> is always a single value, | 1181 <tt>(f(x,y,z))</tt> is always a single value, |
| 1278 even if <code>f</code> returns several values. | 1182 even if <tt>f</tt> returns several values. |
| 1279 (The value of <code>(f(x,y,z))</code> is the first value returned by <code>f</code> | 1183 (The value of <tt>(f(x,y,z))</tt> is the first value returned by <tt>f</tt> |
| 1280 or <b>nil</b> if <code>f</code> does not return any values.) | 1184 or <b>nil</b> if <tt>f</tt> does not return any values.) |
| 1281 | 1185 |
| 1282 | 1186 |
| 1283 | 1187 |
| 1284 <h3>3.4.1 – <a name="3.4.1">Arithmetic Operators</a></h3><p> | 1188 <h4 margin-top="1em"><a name="arithmetic">Arithmetic Operators</a></h4> |
| 1285 Lua supports the following arithmetic operators: | 1189 |
| 1190 <p> | |
| 1191 Luan supports the following arithmetic operators: | |
| 1286 | 1192 |
| 1287 <ul> | 1193 <ul> |
| 1288 <li><b><code>+</code>: </b>addition</li> | 1194 <li><b><tt>+</tt>: </b>addition</li> |
| 1289 <li><b><code>-</code>: </b>subtraction</li> | 1195 <li><b><tt>-</tt>: </b>subtraction</li> |
| 1290 <li><b><code>*</code>: </b>multiplication</li> | 1196 <li><b><tt>*</tt>: </b>multiplication</li> |
| 1291 <li><b><code>/</code>: </b>float division</li> | 1197 <li><b><tt>/</tt>: </b>division</li> |
| 1292 <li><b><code>//</code>: </b>floor division</li> | 1198 <li><b><tt>%</tt>: </b>modulo</li> |
| 1293 <li><b><code>%</code>: </b>modulo</li> | 1199 <li><b><tt>^</tt>: </b>exponentiation</li> |
| 1294 <li><b><code>^</code>: </b>exponentiation</li> | 1200 <li><b><tt>-</tt>: </b>unary minus</li> |
| 1295 <li><b><code>-</code>: </b>unary minus</li> | |
| 1296 </ul> | 1201 </ul> |
| 1297 | 1202 |
| 1298 <p> | 1203 <p> |
| 1299 With the exception of exponentiation and float division, | 1204 Addition, subtraction, multiplication, division, and unary minus are the same as these operators in Java. Exponentiation uses Java's <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#pow(double,%20double)">Math.pow</a> function. |
| 1300 the arithmetic operators work as follows: | |
| 1301 If both operands are integers, | |
| 1302 the operation is performed over integers and the result is an integer. | |
| 1303 Otherwise, if both operands are numbers | |
| 1304 or strings that can be converted to | |
| 1305 numbers (see <a href="#3.4.3">§3.4.3</a>), | |
| 1306 then they are converted to floats, | |
| 1307 the operation is performed following the usual rules | |
| 1308 for floating-point arithmetic | |
| 1309 (usually the IEEE 754 standard), | |
| 1310 and the result is a float. | |
| 1311 | |
| 1312 | |
| 1313 <p> | |
| 1314 Exponentiation and float division (<code>/</code>) | |
| 1315 always convert their operands to floats | |
| 1316 and the result is always a float. | |
| 1317 Exponentiation uses the ISO C function <code>pow</code>, | |
| 1318 so that it works for non-integer exponents too. | |
| 1319 | |
| 1320 | |
| 1321 <p> | |
| 1322 Floor division (<code>//</code>) is a division | |
| 1323 that rounds the quotient towards minus infinite, | |
| 1324 that is, the floor of the division of its operands. | |
| 1325 | |
| 1326 | 1205 |
| 1327 <p> | 1206 <p> |
| 1328 Modulo is defined as the remainder of a division | 1207 Modulo is defined as the remainder of a division |
| 1329 that rounds the quotient towards minus infinite (floor division). | 1208 that rounds the quotient towards minus infinite (floor division). |
| 1330 | 1209 (The Java modulo operator is not used.) |
| 1331 | |
| 1332 <p> | |
| 1333 In case of overflows in integer arithmetic, | |
| 1334 all operations <em>wrap around</em>, | |
| 1335 according to the usual rules of two-complement arithmetic. | |
| 1336 (In other words, | |
| 1337 they return the unique representable integer | |
| 1338 that is equal modulo <em>2<sup>64</sup></em> to the mathematical result.) | |
| 1339 | |
| 1340 | |
| 1341 | |
| 1342 <h3>3.4.2 – <a name="3.4.2">Bitwise Operators</a></h3><p> | |
| 1343 Lua supports the following bitwise operators: | |
| 1344 | |
| 1345 <ul> | |
| 1346 <li><b><code>&</code>: </b>bitwise and</li> | |
| 1347 <li><b><code>|</code>: </b>bitwise or</li> | |
| 1348 <li><b><code>~</code>: </b>bitwise exclusive or</li> | |
| 1349 <li><b><code>>></code>: </b>right shift</li> | |
| 1350 <li><b><code><<</code>: </b>left shift</li> | |
| 1351 <li><b><code>~</code>: </b>unary bitwise not</li> | |
| 1352 </ul> | |
| 1353 | |
| 1354 <p> | |
| 1355 All bitwise operations convert its operands to integers | |
| 1356 (see <a href="#3.4.3">§3.4.3</a>), | |
| 1357 operate on all bits of those integers, | |
| 1358 and result in an integer. | |
| 1359 | |
| 1360 | |
| 1361 <p> | |
| 1362 Both right and left shifts fill the vacant bits with zeros. | |
| 1363 Negative displacements shift to the other direction; | |
| 1364 displacements with absolute values equal to or higher than | |
| 1365 the number of bits in an integer | |
| 1366 result in zero (as all bits are shifted out). | |
| 1367 | |
| 1368 | |
| 1369 | 1210 |
| 1370 | 1211 |
| 1371 | 1212 |
| 1372 <h3>3.4.3 – <a name="3.4.3">Coercions and Conversions</a></h3><p> | 1213 <h3>3.4.3 – <a name="3.4.3">Coercions and Conversions</a></h3><p> |
| 1373 Lua provides some automatic conversions between some | 1214 Lua provides some automatic conversions between some |
