Mercurial Hosting > luan
comparison website/src/m.html.luan @ 1660:2968e43cdd44
manual work
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 19 Apr 2022 23:46:06 -0600 |
parents | 540bf2343078 |
children | c55373c3a0ce |
comparison
equal
deleted
inserted
replaced
1659:500c706ed4ea | 1660:2968e43cdd44 |
---|---|
911 <p> | 911 <p> |
912 An assignment to a global name <code>x = val</code> | 912 An assignment to a global name <code>x = val</code> |
913 is equivalent to the assignment | 913 is equivalent to the assignment |
914 <code>_ENV.x = val</code> (see <a href="#env">Environments</a>). | 914 <code>_ENV.x = val</code> (see <a href="#env">Environments</a>). |
915 Global names are only available when <code>_ENV</code> is defined. | 915 Global names are only available when <code>_ENV</code> is defined. |
916 </p> | |
917 <% | |
918 end | |
919 } | |
920 control = { | |
921 title = "Control Structures" | |
922 content = function() | |
923 %> | |
924 <p> | |
925 The control structures | |
926 <b>if</b>, <b>while</b>, and <b>repeat</b> have the usual meaning and | |
927 familiar syntax: | |
928 </p> | |
929 | |
930 <pre> | |
931 stat ::= <b>while</b> exp <b>do</b> block end_while | |
932 stat ::= <b>repeat</b> block <b>until</b> exp | |
933 stat ::= <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] end_if | |
934 end_while ::= <b>end_while</b> | <b>end</b> | |
935 end_if ::= <b>end_if</b> | <b>end</b> | |
936 </pre> | |
937 | |
938 <p> | |
939 Luan also has a <b>for</b> statement (see <a href="#for">For Statement</a>). | |
940 </p> | |
941 | |
942 <p> | |
943 The condition expression of a | |
944 control structure must be a boolean. | |
945 Any other value type will produce an error. | |
946 This helps catch errors and makes code more readable. | |
947 </p> | |
948 | |
949 <p> | |
950 In the <b>repeat</b>–<b>until</b> loop, | |
951 the inner block does not end at the <b>until</b> keyword, | |
952 but only after the condition. | |
953 So, the condition can refer to local variables | |
954 declared inside the loop block. | |
955 </p> | |
956 | |
957 <p> | |
958 The <b>break</b> statement terminates the execution of a | |
959 <b>while</b>, <b>repeat</b>, or <b>for</b> loop, | |
960 skipping to the next statement after the loop: | |
961 </p> | |
962 | |
963 <pre> | |
964 stat ::= <b>break</b> | |
965 </pre> | |
966 | |
967 <p> | |
968 A <b>break</b> ends the innermost enclosing loop. | |
969 </p> | |
970 | |
971 <p> | |
972 The <b>continue</b> statement jumps to the beginning of a | |
973 <b>while</b>, <b>repeat</b>, or <b>for</b> loop for next iteration, | |
974 skipping the execution of statements inside the body of loop for the current iteration: | |
975 </p> | |
976 | |
977 <pre> | |
978 stat ::= <b>continue</b> | |
979 </pre> | |
980 | |
981 <p> | |
982 The <b>return</b> statement is used to return values | |
983 from a function or a chunk | |
984 (which is an anonymous function). | |
985 Functions can return more than one value, | |
986 so the syntax for the <b>return</b> statement is | |
987 </p> | |
988 | |
989 <pre> | |
990 stat ::= <b>return</b> [explist] [‘<b>;</b>’] | |
991 </pre> | |
992 <% | |
993 end | |
994 } | |
995 ["for"] = { | |
996 title = "For Statement" | |
997 content = function() | |
998 %> | |
999 <p> | |
1000 The <b>for</b> statement works over functions, | |
1001 called <em>iterators</em>. | |
1002 On each iteration, the iterator function is called to produce a new value, | |
1003 stopping when this new value is <b>nil</b>. | |
1004 The <b>for</b> loop has the following syntax: | |
1005 </p> | |
1006 | |
1007 <pre> | |
1008 stat ::= <b>for</b> namelist <b>in</b> exp <b>do</b> block end_for | |
1009 namelist ::= Name {‘<b>,</b>’ Name} | |
1010 end_for ::= <b>end_for</b> | <b>end</b> | |
1011 </pre> | |
1012 | |
1013 <p> | |
1014 A <b>for</b> statement like | |
1015 </p> | |
1016 | |
1017 <pre> | |
1018 for <em>var_1</em>, ···, <em>var_n</em> in <em>exp</em> do <em>block</em> end | |
1019 </pre> | |
1020 | |
1021 <p> | |
1022 is equivalent to the code: | |
1023 </p> | |
1024 | |
1025 <pre> | |
1026 do | |
1027 local <em>f</em> = <em>exp</em> | |
1028 while true do | |
1029 local <em>var_1</em>, ···, <em>var_n</em> = <em>f</em>() | |
1030 if <em>var_1</em> == nil then break end | |
1031 <em>block</em> | |
1032 end | |
1033 end | |
1034 </pre> | |
1035 | |
1036 <p> | |
1037 Note the following: | |
1038 </p> | |
1039 | |
1040 <ul> | |
1041 <li> | |
1042 <code><em>exp</em></code> is evaluated only once. | |
1043 Its result is an <em>iterator</em> function. | |
1044 </li> | |
1045 <li> | |
1046 <code><em>f</em></code> is an invisible variable. | |
1047 The name is here for explanatory purposes only. | |
1048 </li> | |
1049 <li> | |
1050 You can use <b>break</b> to exit a <b>for</b> loop. | |
1051 </li> | |
1052 <li> | |
1053 The loop variables <code><em>var_i</em></code> are local to the loop; | |
1054 you cannot use their values after the <b>for</b> ends. | |
1055 If you need these values, | |
1056 then assign them to other variables before breaking or exiting the loop. | |
1057 </li> | |
1058 </ul> | |
1059 <% | |
1060 end | |
1061 } | |
1062 ["try"] = { | |
1063 title = "Try Statement" | |
1064 content = function() | |
1065 %> | |
1066 <p> | |
1067 The <b>try</b> statement has the same semantics as in Java. | |
1068 </p> | |
1069 | |
1070 <pre> | |
1071 stat ::= <b>try</b> block [<b>catch</b> Name block] [<b>finally</b> block] end_try | |
1072 end_try ::= <b>end_try</b> | <b>end</b> | |
1073 </pre> | |
1074 <% | |
1075 end | |
1076 } | |
1077 fn_stmt = { | |
1078 title = "Function Calls as Statements" | |
1079 content = function() | |
1080 %> | |
1081 <p> | |
1082 To allow possible side-effects, | |
1083 function calls can be executed as statements: | |
1084 </p> | |
1085 | |
1086 <pre> | |
1087 stat ::= functioncall | |
1088 </pre> | |
1089 | |
1090 <p> | |
1091 In this case, all returned values are thrown away. | |
1092 Function calls are explained in <a href="#fn_calls">Function Calls</a>. | |
1093 </p> | |
1094 <% | |
1095 end | |
1096 } | |
1097 logical_stmt = { | |
1098 title = "Logical Statement" | |
1099 content = function() | |
1100 %> | |
1101 <p> | |
1102 <a href="#logical_ops">Logical expressions</a> can be statements. | |
1103 This is useful in cases like this: | |
1104 </p> | |
1105 | |
1106 <pre> | |
1107 x==5 or error "x should be 5" | |
1108 </pre> | |
1109 <% | |
1110 end | |
1111 } | |
1112 local_stmt = { | |
1113 title = "Local Declarations" | |
1114 content = function() | |
1115 %> | |
1116 <p> | |
1117 Local variables can be declared anywhere inside a block. | |
1118 The declaration can include an initial assignment: | |
1119 </p> | |
1120 | |
1121 <pre> | |
1122 stat ::= <b>local</b> namelist [‘<b>=</b>’ explist] | |
1123 </pre> | |
1124 | |
1125 <p> | |
1126 If present, an initial assignment has the same semantics | |
1127 of a multiple assignment (see <a href="#assignment">Assignment</a>). | |
1128 Otherwise, all variables are initialized with <b>nil</b>. | |
1129 </p> | |
1130 | |
1131 <p> | |
1132 A chunk is also a block (see <a href="#chunks">Chunks</a>), | |
1133 and so local variables can be declared in a chunk outside any explicit block. | |
1134 </p> | |
1135 | |
1136 <p> | |
1137 The visibility rules for local variables are explained in <a href="#visibility">Visibility Rules</a>. | |
1138 </p> | |
1139 <% | |
1140 end | |
1141 } | |
1142 template_stmt = { | |
1143 title = "Template Statements" | |
1144 content = function() | |
1145 %> | |
1146 <p>Template statements provide the full equivalent of <a href="http://en.wikipedia.org/wiki/JavaServer_Pages">JSP</a> but in a general way. Template statements write to standard output. For example:</p> | |
1147 </p> | |
1148 | |
1149 <pre> | |
1150 local name = "Bob" | |
1151 %> | |
1152 Hello <%= name %>! | |
1153 Bye <%= name %>. | |
1154 <% | |
1155 </pre> | |
1156 | |
1157 <p> | |
1158 is equivalent to the code: | |
1159 </p> | |
1160 | |
1161 <pre> | |
1162 local name = "Bob" | |
1163 require("luan:Io.luan").stdout.write( "Hello ", name , "!\nBye ", name , ".\n" ) | |
1164 </pre> | |
1165 <% | |
1166 end | |
1167 } | |
1168 } | |
1169 } | |
1170 expressions = { | |
1171 title = "Expressions" | |
1172 content = function() | |
1173 %> | |
1174 <p> | |
1175 The basic expressions in Luan are the following: | |
1176 </p> | |
1177 | |
1178 <pre> | |
1179 exp ::= prefixexp | |
1180 exp ::= <b>nil</b> | <b>false</b> | <b>true</b> | |
1181 exp ::= Numeral | |
1182 exp ::= LiteralString | |
1183 exp ::= functiondef | |
1184 exp ::= tableconstructor | |
1185 exp ::= ‘<b>...</b>’ | |
1186 exp ::= exp binop exp | |
1187 exp ::= unop exp | |
1188 prefixexp ::= var | functioncall | ‘<b>(</b>’ exp ‘<b>)</b>’ | |
1189 </pre> | |
1190 | |
1191 <p> | |
1192 Numerals and literal strings are explained in <a href="#lex">Lexical Conventions</a>; | |
1193 variables are explained in <a href="#vars">Variables</a>; | |
1194 function definitions are explained in <a href="#fn_def">Function Definitions</a>; | |
1195 function calls are explained in <a href="#fn_calls">Function Calls</a>; | |
1196 table constructors are explained in <a href="#constructors">Table Constructors</a>. | |
1197 Vararg expressions, | |
1198 denoted by three dots ('<code>...</code>'), can only be used when | |
1199 directly inside a vararg function; | |
1200 they are explained in <a href="#fn_def">Function Definitions</a>. | |
1201 </p> | |
1202 | |
1203 <p> | |
1204 Binary operators comprise arithmetic operators (see <a href="#arithmetic">Arithmetic Operators</a>), | |
1205 relational operators (see <a href="#relational">Relational Operators</a>), logical operators (see <a href="#logical_ops">Logical Operators</a>), | |
1206 and the concatenation operator (see <a href="#concatenation">Concatenation</a>). | |
1207 Unary operators comprise the unary minus (see <a href="#arithmetic">Arithmetic Operators</a>), | |
1208 the unary logical <b>not</b> (see <a href="#logical_ops">Logical Operators</a>), | |
1209 and the unary <em>length operator</em> (see <a href="#length">The Length Operator</a>). | |
1210 </p> | |
1211 | |
1212 <p> | |
1213 Both function calls and vararg expressions can result in multiple values. | |
1214 If a function call is used as a statement (see <a href="#fn_stmt">Function Calls as Statements</a>), | |
1215 then its return list is adjusted to zero elements, | |
1216 thus discarding all returned values. | |
1217 If an expression is used as the last (or the only) element | |
1218 of a list of expressions, | |
1219 then no adjustment is made | |
1220 (unless the expression is enclosed in parentheses). | |
1221 In all other contexts, | |
1222 Luan adjusts the result list to one element, | |
1223 either discarding all values except the first one | |
1224 or adding a single <b>nil</b> if there are no values. | |
1225 </p> | |
1226 | |
1227 <p> | |
1228 Here are some examples: | |
1229 </p> | |
1230 | |
1231 <pre> | |
1232 f() -- adjusted to 0 results | |
1233 g(f(), x) -- f() is adjusted to 1 result | |
1234 g(x, f()) -- g gets x plus all results from f() | |
1235 a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil) | |
1236 a,b = ... -- a gets the first vararg parameter, b gets | |
1237 -- the second (both a and b can get nil if there | |
1238 -- is no corresponding vararg parameter) | |
1239 | |
1240 a,b,c = x, f() -- f() is adjusted to 2 results | |
1241 a,b,c = f() -- f() is adjusted to 3 results | |
1242 return f() -- returns all results from f() | |
1243 return ... -- returns all received vararg parameters | |
1244 return x,y,f() -- returns x, y, and all results from f() | |
1245 {f()} -- creates a list with all results from f() | |
1246 {...} -- creates a list with all vararg parameters | |
1247 {f(), nil} -- f() is adjusted to 1 result | |
1248 </pre> | |
1249 | |
1250 <p> | |
1251 Any expression enclosed in parentheses always results in only one value. | |
1252 Thus, | |
1253 <code>(f(x,y,z))</code> is always a single value, | |
1254 even if <code>f</code> returns several values. | |
1255 (The value of <code>(f(x,y,z))</code> is the first value returned by <code>f</code> | |
1256 or <b>nil</b> if <code>f</code> does not return any values.) | |
1257 </p> | |
1258 <% | |
1259 end | |
1260 subs = { | |
1261 arithmetic = { | |
1262 title = "Arithmetic Operators" | |
1263 content = function() | |
1264 %> | |
1265 <p> | |
1266 Luan supports the following arithmetic operators: | |
1267 </p> | |
1268 | |
1269 <ul> | |
1270 <li><b><code>+</code>: </b>addition</li> | |
1271 <li><b><code>-</code>: </b>subtraction</li> | |
1272 <li><b><code>*</code>: </b>multiplication</li> | |
1273 <li><b><code>/</code>: </b>division</li> | |
1274 <li><b><code>%</code>: </b>modulo</li> | |
1275 <li><b><code>^</code>: </b>exponentiation</li> | |
1276 <li><b><code>-</code>: </b>unary minus</li> | |
1277 </ul> | |
1278 | |
1279 <p> | |
1280 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. | |
1281 </p> | |
1282 | |
1283 <p> | |
1284 Modulo is defined as the remainder of a division | |
1285 that rounds the quotient towards minus infinite (floor division). | |
1286 (The Java modulo operator is not used.) | |
1287 </p> | |
1288 <% | |
1289 end | |
1290 } | |
1291 conversions = { | |
1292 title = "Coercions and Conversions" | |
1293 content = function() | |
1294 %> | |
1295 <p> | |
1296 Luan generally avoids automatic conversions. | |
1297 String concatenation automatically converts all of its arguments to strings. | |
1298 </p> | |
1299 | |
1300 <p> | |
1301 Luan provides library functions for explicit type conversions. | |
1302 </p> | |
1303 <% | |
1304 end | |
1305 } | |
1306 relational = { | |
1307 title = "Relational Operators" | |
1308 content = function() | |
1309 %> | |
1310 <p> | |
1311 Luan supports the following relational operators: | |
1312 </p> | |
1313 | |
1314 <ul> | |
1315 <li><b><code>==</code>: </b>equality</li> | |
1316 <li><b><code>~=</code>: </b>inequality</li> | |
1317 <li><b><code><</code>: </b>less than</li> | |
1318 <li><b><code>></code>: </b>greater than</li> | |
1319 <li><b><code><=</code>: </b>less or equal</li> | |
1320 <li><b><code>>=</code>: </b>greater or equal</li> | |
1321 </ul> | |
1322 | |
1323 <p> | |
1324 These operators always result in <b>false</b> or <b>true</b>. | |
1325 </p> | |
1326 | |
1327 <p> | |
1328 Equality (<code>==</code>) first compares the type of its operands. | |
1329 If the types are different, then the result is <b>false</b>. | |
1330 Otherwise, the values of the operands are compared. | |
1331 Strings, numbers, and binary values are compared in the obvious way (by value). | |
1332 </p> | |
1333 | |
1334 <p> | |
1335 Tables | |
1336 are compared by reference: | |
1337 two objects are considered equal only if they are the same object. | |
1338 Every time you create a new table, | |
1339 it is different from any previously existing table. | |
1340 Closures are also compared by reference. | |
1341 </p> | |
1342 | |
1343 <p> | |
1344 You can change the way that Luan compares tables | |
1345 by using the "eq" metamethod (see <a href="#meta">Metatables and Metamethods</a>). | |
1346 </p> | |
1347 | |
1348 <p> | |
1349 Java values 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)"><code>equals</code></a> method. | |
1350 </p> | |
1351 | |
1352 <p> | |
1353 Equality comparisons do not convert strings to numbers | |
1354 or vice versa. | |
1355 Thus, <code>"0"==0</code> evaluates to <b>false</b>, | |
1356 and <code>t[0]</code> and <code>t["0"]</code> denote different | |
1357 entries in a table. | |
1358 </p> | |
1359 | |
1360 <p> | |
1361 The operator <code>~=</code> is exactly the negation of equality (<code>==</code>). | |
1362 </p> | |
1363 | |
1364 <p> | |
1365 The order operators work as follows. | |
1366 If both arguments are numbers, | |
1367 then they are compared following | |
1368 the usual rule for binary operations. | |
1369 Otherwise, if both arguments are strings, | |
1370 then their values are compared according to the current locale. | |
1371 Otherwise, Luan tries to call the "lt" or the "le" | |
1372 metamethod (see <a href="#meta">Metatables and Metamethods</a>). | |
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>. | |
916 </p> | 1375 </p> |
917 <% | 1376 <% |
918 end | 1377 end |
919 } | 1378 } |
920 } | 1379 } |