Mercurial Hosting > luan
annotate website/src/manual.html.luan @ 1829:0eb615de1f80
add mail helo
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 22 Sep 2024 20:05:54 -0600 |
parents | 50e570b598b2 |
children |
rev | line source |
---|---|
1656 | 1 local Luan = require "luan:Luan.luan" |
2 local error = Luan.error | |
3 local Io = require "luan:Io.luan" | |
4 local Http = require "luan:http/Http.luan" | |
5 local Shared = require "site:/lib/Shared.luan" | |
6 local head = Shared.head or error() | |
7 local docs_header = Shared.docs_header or error() | |
8 local show_toc = Shared.show_toc or error() | |
9 local show_content = Shared.show_content or error() | |
10 | |
11 | |
12 local content = { | |
13 intro = { | |
14 title = "Introduction" | |
15 content = function() | |
16 %> | |
17 <p> | |
1827 | 18 Luan is a high level programming language based on <a href="https://www.lua.org/">Lua</a>. A great strength of Lua is its simplicity and Luan takes this even further, being even simpler than Lua. The goal is to provide a simple programming language for the casual programmer with as few concepts as possible so that one can quickly learn the language and then easily understand any code written in Luan. |
1656 | 19 </p> |
20 | |
21 <p> | |
22 Luan is implemented in Java and is tightly coupled with Java. So it makes a great scripting language for Java programmers. | |
23 </p> | |
24 | |
25 <p> | |
26 Unlike Lua which is meant to be embedded, Luan is meant to be a full scripting language. This done not by adding features to Luan, but rather by providing a complete set of libraries. | |
27 </p> | |
28 <% | |
29 end | |
30 } | |
31 basic = { | |
32 title = "Basic Concepts" | |
33 content = function() | |
34 %> | |
35 <p> | |
36 This section describes the basic concepts of the language. | |
37 </p> | |
38 <% | |
39 end | |
40 subs = { | |
41 types = { | |
42 title = "Values and Types" | |
43 content = function() | |
44 %> | |
45 <p> | |
46 Luan is a <em>dynamically typed language</em>. | |
47 This means that | |
48 variables do not have types; only values do. | |
49 There are no type definitions in the language. | |
50 All values carry their own type. | |
51 </p> | |
52 | |
53 <p> | |
54 All values in Luan are <em>first-class values</em>. | |
55 This means that all values can be stored in variables, | |
56 passed as arguments to other functions, and returned as results. | |
57 </p> | |
58 | |
59 <p> | |
60 There are eight basic types in Luan: | |
61 <em>nil</em>, <em>boolean</em>, <em>number</em>, | |
62 <em>string</em>, <em>binary</em>, <em>function</em>, <em>java</em>, | |
63 and <em>table</em>. | |
64 <em>Nil</em> is the type of the value <b>nil</b>, | |
65 whose main property is to be different from any other value; | |
66 it usually represents the absence of a useful value. | |
67 <em>Nil</em> is implemented as the Java value <em>null</em>. | |
68 <em>Boolean</em> is the type of the values <b>false</b> and <b>true</b>. | |
69 <em>Boolean</em> is implemented as the Java class <em>Boolean</em>. | |
70 <em>Number</em> represents both | |
71 integer numbers and real (floating-point) numbers. | |
72 <em>Number</em> is implemented as the Java class <em>Number</em>. Any Java subclass of <em>Number</em> is allowed and this is invisible to the Luan user. Operations on numbers follow the same rules of | |
73 the underlying Java implementation. | |
74 <em>String</em> is implemented as the Java class <em>String</em>. | |
75 <em>Binary</em> is implemented as the Java type <em>byte[]</em>. | |
76 </p> | |
77 | |
78 <p> | |
79 Luan can call (and manipulate) functions written in Luan and | |
80 functions written in Java (see <a href="#fn_calls">Function Calls</a>). | |
81 Both are represented by the type <em>function</em>. | |
82 </p> | |
83 | |
84 <p> | |
85 The type <em>java</em> is provided to allow arbitrary Java objects to | |
86 be stored in Luan variables. | |
87 A <em>java</em> value is a Java object that isn't one of the standard Luan types. | |
88 Java values have no predefined operations in Luan, | |
89 except assignment and identity test. | |
90 Java values are useful when Java access is enabled in Luan. | |
91 </p> | |
92 | |
93 <p> | |
94 The type <em>table</em> implements associative arrays, | |
95 that is, arrays that can be indexed not only with numbers, | |
96 but with any Luan value except <b>nil</b>. | |
97 Tables can be <em>heterogeneous</em>; | |
98 that is, they can contain values of all types (except <b>nil</b>). | |
99 Any key with value <b>nil</b> is not considered part of the table. | |
100 Conversely, any key that is not part of a table has | |
101 an associated value <b>nil</b>. | |
102 </p> | |
103 | |
104 <p> | |
105 Tables are the sole data-structuring mechanism in Luan; | |
106 they can be used to represent ordinary arrays, sequences, | |
107 symbol tables, sets, records, graphs, trees, etc. | |
108 To represent records, Luan uses the field name as an index. | |
109 The language supports this representation by | |
110 providing <code>a.name</code> as syntactic sugar for <code>a["name"]</code>. | |
111 There are several convenient ways to create tables in Luan | |
112 (see <a href="#constructors">Table Constructors</a>). | |
113 </p> | |
114 | |
115 <p> | |
116 We use the term <em>sequence</em> to denote a table where | |
117 the set of all positive numeric keys is equal to {1..<em>n</em>} | |
118 for some non-negative integer <em>n</em>, | |
119 which is called the length of the sequence (see <a href="#length">The Length Operator</a>). | |
120 </p> | |
121 | |
122 <p> | |
123 Like indices, | |
124 the values of table fields can be of any type. | |
125 In particular, | |
126 because functions are first-class values, | |
127 table fields can contain functions. | |
128 Thus tables can also carry <em>methods</em> (see <a href="#fn_def">Function Definitions</a>). | |
129 </p> | |
130 | |
131 <p> | |
132 The indexing of tables follows | |
133 the definition of raw equality in the language. | |
134 The expressions <code>a[i]</code> and <code>a[j]</code> | |
135 denote the same table element | |
136 if and only if <code>i</code> and <code>j</code> are raw equal | |
137 (that is, equal without metamethods). | |
138 In particular, floats with integral values | |
139 are equal to their respective integers | |
140 (e.g., <code>1.0 == 1</code>). | |
141 </p> | |
142 | |
143 <p> | |
144 Luan values are <em>objects</em>: | |
145 variables do not actually <em>contain</em> values, | |
146 only <em>references</em> to them. | |
147 Assignment, parameter passing, and function returns | |
148 always manipulate references to values; | |
149 these operations do not imply any kind of copy. | |
150 </p> | |
151 | |
152 <p> | |
153 The library function <a href="#Luan.type"><code>Luan.type</code></a> returns a string describing the type | |
154 of a given value. | |
155 </p> | |
156 <% | |
157 end | |
158 } | |
159 env = { | |
160 title = "Environments" | |
161 content = function() | |
162 %> | |
163 <p> | |
1827 | 164 The environment of a chunk starts with only one local variable: <code><a href="#require">require</a></code>. This function is used to load and access libraries and other modules. All other variables must be added to the environment using <a href="/manual.html#local_stmt">local declarations</a>. |
1656 | 165 </p> |
166 | |
167 <p> | |
168 As will be discussed in <a href="#vars">Variables</a> and <a href=#assignment">Assignment</a>, | |
169 any reference to a free name | |
170 (that is, a name not bound to any declaration) <code>var</code> | |
171 can be syntactically translated to <code>_ENV.var</code> if <code>_ENV</code> is defined. | |
172 </p> | |
173 <% | |
174 end | |
175 } | |
176 error = { | |
177 title = "Error Handling" | |
178 content = function() | |
179 %> | |
180 <p> | |
181 Luan code can explicitly generate an error by calling the | |
182 <a href="#Luan.error"><code>error</code></a> function. | |
183 If you need to catch errors in Luan, | |
184 you can use the <a href="#try">Try Statement</code></a>. | |
185 </p> | |
186 | |
187 <p> | |
188 Whenever there is an error, | |
189 an <em>error table</em> | |
190 is propagated with information about the error. | |
191 See <a href="#Luan.new_error"><code>Luan.new_error</code></a>. | |
192 </p> | |
193 <% | |
194 end | |
195 } | |
196 meta = { | |
197 title = "Metatables and Metamethods" | |
198 content = function() | |
199 %> | |
200 <p> | |
201 Every table in Luan can have a <em>metatable</em>. | |
202 This <em>metatable</em> is an ordinary Luan table | |
203 that defines the behavior of the original value | |
204 under certain special operations. | |
205 You can change several aspects of the behavior | |
206 of operations over a value by setting specific fields in its metatable. | |
207 For instance, when a table is the operand of an addition, | |
208 Luan checks for a function in the field "<code>__add</code>" of the table's metatable. | |
209 If it finds one, | |
210 Luan calls this function to perform the addition. | |
211 </p> | |
212 | |
213 <p> | |
214 The keys in a metatable are derived from the <em>event</em> names; | |
215 the corresponding values are called <ii>metamethods</em>. | |
216 In the previous example, the event is <code>"add"</code> | |
217 and the metamethod is the function that performs the addition. | |
218 </p> | |
219 | |
220 <p> | |
221 You can query the metatable of any table | |
222 using the <a href="#Luan.get_metatable"><code>get_metatable</code></a> function. | |
223 </p> | |
224 | |
225 <p> | |
226 You can replace the metatable of tables | |
227 using the <a href="#Luan.set_metatable"><code>set_metatable</code></a> function. | |
228 </p> | |
229 | |
230 <p> | |
231 A metatable controls how a table behaves in | |
232 arithmetic operations, bitwise operations, | |
233 order comparisons, concatenation, length operation, calls, and indexing. | |
234 </p> | |
235 | |
236 <p> | |
237 A detailed list of events controlled by metatables is given next. | |
238 Each operation is identified by its corresponding event name. | |
239 The key for each event is a string with its name prefixed by | |
240 two underscores, '<code>__</code>'; | |
241 for instance, the key for operation "add" is the | |
242 string "<code>__add</code>". | |
243 Note that queries for metamethods are always raw; | |
244 the access to a metamethod does not invoke other metamethods. | |
245 You can emulate how Luan queries a metamethod for an object <code>obj</code> | |
246 with the following code: | |
247 </p> | |
248 | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
249 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
250 raw_get(get_metatable(obj) or {}, "__" .. event_name) |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
251 </code> |
1656 | 252 |
253 <p> | |
254 Here are the events: | |
255 </p> | |
256 | |
257 <ul> | |
258 | |
259 <li><p> | |
260 <b>"add": </b> | |
261 the <code>+</code> operation. | |
262 | |
263 If any operand for an addition is a table, | |
264 Luan will try to call a metamethod. | |
265 First, Luan will check the first operand (even if it is valid). | |
266 If that operand does not define a metamethod for the "<code>__add</code>" event, | |
267 then Luan will check the second operand. | |
268 If Luan can find a metamethod, | |
269 it calls the metamethod with the two operands as arguments, | |
270 and the result of the call | |
271 (adjusted to one value) | |
272 is the result of the operation. | |
273 Otherwise, | |
274 it raises an error. | |
275 </p></li> | |
276 | |
277 <li><p> | |
278 <b>"sub": </b> | |
279 the <code>-</code> operation. | |
280 Behavior similar to the "add" operation. | |
281 </li> | |
282 | |
283 <li><p><b>"mul": </b> | |
284 the <code>*</code> operation. | |
285 Behavior similar to the "add" operation. | |
286 </p></li> | |
287 | |
288 <li><p> | |
289 <b>"div": </b> | |
290 the <code>/</code> operation. | |
291 Behavior similar to the "add" operation. | |
292 </p></li> | |
293 | |
294 <li><p> | |
1680 | 295 <b>"idiv": </b> |
296 the <code>//</code> operation. | |
297 Behavior similar to the "add" operation. | |
298 </p></li> | |
299 | |
300 <li><p> | |
1656 | 301 <b>"mod": </b> |
302 the <code>%</code> operation. | |
303 Behavior similar to the "add" operation. | |
304 </p></li> | |
305 | |
306 <li><p> | |
307 <b>"pow": </b> | |
308 the <code>^</code> (exponentiation) operation. | |
309 Behavior similar to the "add" operation. | |
310 </p></li> | |
311 | |
312 <li><p> | |
313 <b>"unm": </b> | |
314 the <code>-</code> (unary minus) operation. | |
315 Behavior similar to the "add" operation. | |
316 </p></li> | |
317 | |
318 <li><p> | |
319 <b>"concat": </b> | |
320 the <code>..</code> (concatenation) operation. | |
321 Behavior similar to the "add" operation. | |
322 </p></li> | |
323 | |
324 <li><p> | |
325 <b>"len": </b> | |
326 the <code>#</code> (length) operation. | |
327 If there is a metamethod, | |
328 Luan calls it with the object as argument, | |
329 and the result of the call | |
330 (always adjusted to one value) | |
331 is the result of the operation. | |
332 If there is no metamethod but the object is a table, | |
333 then Luan uses the table length operation (see <a href="#length">The Length Operator</a>). | |
334 Otherwise, Luan raises an error. | |
335 </p></li> | |
336 | |
337 <li><p> | |
338 <b>"eq": </b> | |
339 the <code>==</code> (equal) operation. | |
340 Behavior similar to the "add" operation, | |
341 except that Luan will try a metamethod only when the values | |
342 being compared are both tables | |
343 and they are not primitively equal. | |
344 The result of the call is always converted to a boolean. | |
345 </p></li> | |
346 | |
347 <li><p> | |
348 <b>"lt": </b> | |
349 the <code><</code> (less than) operation. | |
350 Behavior similar to the "add" operation. | |
351 The result of the call is always converted to a boolean. | |
352 </p></li> | |
353 | |
354 <li><p> | |
355 <b>"le": </b> | |
356 the <code><=</code> (less equal) operation. | |
357 Unlike other operations, | |
358 The less-equal operation can use two different events. | |
359 First, Luan looks for the "<code>__le</code>" metamethod in both operands, | |
360 like in the "lt" operation. | |
361 If it cannot find such a metamethod, | |
362 then it will try the "<code>__lt</code>" event, | |
363 assuming that <code>a <= b</code> is equivalent to <code>not (b < a)</code>. | |
364 As with the other comparison operators, | |
365 the result is always a boolean. | |
366 </p></li> | |
367 | |
368 <li> | |
369 <p> | |
370 <b>"index": </b> | |
371 The indexing access <code>table[key]</code>. | |
372 This event happens | |
373 when <code>key</code> is not present in <code>table</code>. | |
374 The metamethod is looked up in <code>table</code>. | |
375 </p> | |
376 | |
377 <p> | |
378 Despite the name, | |
379 the metamethod for this event can be any type. | |
380 If it is a function, | |
381 it is called with <code>table</code> and <code>key</code> as arguments. | |
382 Otherwise | |
383 the final result is the result of indexing this metamethod object with <code>key</code>. | |
384 (This indexing is regular, not raw, | |
385 and therefore can trigger another metamethod if the metamethod object is a table.) | |
386 </p> | |
387 </li> | |
388 | |
389 <li> | |
390 <p> | |
391 <b>"new_index": </b> | |
392 The indexing assignment <code>table[key] = value</code>. | |
393 Like the index event, | |
394 this event happens when | |
395 when <code>key</code> is not present in <code>table</code>. | |
396 The metamethod is looked up in <code>table</code>. | |
397 </p> | |
398 | |
399 <p> | |
400 Like with indexing, | |
401 the metamethod for this event can be either a function or a table. | |
402 If it is a function, | |
403 it is called with <code>table</code>, <code>key</code>, and <code>value</code> as arguments. | |
404 If it is a table, | |
405 Luan does an indexing assignment to this table with the same key and value. | |
406 (This assignment is regular, not raw, | |
407 and therefore can trigger another metamethod.) | |
408 </p> | |
409 | |
410 <p> | |
411 Whenever there is a "new_index" metamethod, | |
412 Luan does not perform the primitive assignment. | |
413 (If necessary, | |
414 the metamethod itself can call <a href="#Luan.raw_set"><code>raw_set</code></a> | |
415 to do the assignment.) | |
416 </p> | |
417 </li> | |
418 | |
419 <li><p> | |
420 <b>"gc":</b> | |
1816 | 421 This is when a table is garbage collected. When the table's <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--">finalize</a> method is called by the Java garbage collector, if there is a "<code>__gc</code>" metamethod then it is called with the table as a parameter. |
1656 | 422 </p></li> |
423 | |
424 </ul> | |
425 <% | |
426 end | |
427 } | |
428 gc = { | |
429 title = "Garbage Collection" | |
430 content = function() | |
431 %> | |
432 <p> | |
433 Luan uses Java's garbage collection. | |
434 </p> | |
435 <% | |
436 end | |
437 } | |
438 } | |
439 } | |
440 lang = { | |
441 title = "The Language" | |
442 content = function() | |
443 %> | |
444 <p> | |
445 This section describes the lexis, the syntax, and the semantics of Luan. | |
446 In other words, | |
447 this section describes | |
448 which tokens are valid, | |
449 how they can be combined, | |
450 and what their combinations mean. | |
451 </p> | |
452 | |
453 <p> | |
454 Language constructs will be explained using the usual extended BNF notation, | |
455 in which | |
456 {<em>a</em>} means 0 or more <em>a</em>'s, and | |
457 [<em>a</em>] means an optional <em>a</em>. | |
458 Non-terminals are shown like non-terminal, | |
459 keywords are shown like <b>kword</b>, | |
460 and other terminal symbols are shown like ‘<b>=</b>’. | |
461 The complete syntax of Luan can be found in <a href="#9">§9</a> | |
462 at the end of this manual. | |
463 </p> | |
464 <% | |
465 end | |
466 subs = { | |
467 lex = { | |
468 title = "Lexical Conventions" | |
469 content = function() | |
470 %> | |
471 <p> | |
472 Luan ignores spaces and comments | |
473 between lexical elements (tokens), | |
474 except as delimiters between names and keywords. | |
475 Luan considers the end of a line to be the end of a statement. This catches errors and encourages readability. If you want to continue a statement on another line, you can use a backslash followed by a newline which will be treated as white space. | |
476 </p> | |
477 | |
478 <p> | |
479 <em>Names</em> | |
480 (also called <em>identifiers</em>) | |
481 in Luan can be any string of letters, | |
482 digits, and underscores, | |
483 not beginning with a digit. | |
484 Identifiers are used to name variables, table fields, and labels. | |
485 </p> | |
486 | |
487 <p> | |
488 The following <em>keywords</em> are reserved | |
489 and cannot be used as names: | |
490 </p> | |
491 | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
492 <p list=keywords> |
1656 | 493 <span>and</span> |
494 <span>break</span> | |
495 <span>catch</span> | |
496 <span>continue</span> | |
497 <span>do</span> | |
498 <span>else</span> | |
499 <span>elseif</span> | |
500 <span>end_do</span> | |
501 <span>end_for</span> | |
502 <span>end_function</span> | |
503 <span>end_if</span> | |
504 <span>end_try</span> | |
505 <span>end_while</span> | |
506 <span>false</span> | |
507 <span>finally</span> | |
508 <span>for</span> | |
509 <span>function</span> | |
510 <span>if</span> | |
511 <span>in</span> | |
512 <span>local</span> | |
513 <span>nil</span> | |
514 <span>not</span> | |
515 <span>or</span> | |
516 <span>repeat</span> | |
517 <span>return</span> | |
518 <span>then</span> | |
519 <span>true</span> | |
520 <span>try</span> | |
521 <span>until</span> | |
522 <span>while</span> | |
523 </p> | |
524 | |
525 <p> | |
526 Luan is a case-sensitive language: | |
527 <code>and</code> is a reserved word, but <code>And</code> and <code>AND</code> | |
528 are two different, valid names. | |
529 </p> | |
530 | |
531 <p> | |
532 The following strings denote other tokens: | |
533 </p> | |
534 | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
535 <p list=tokens> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
536 <span>+</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
537 <span>-</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
538 <span>*</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
539 <span>/</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
540 <span>//</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
541 <span>%</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
542 <span>^</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
543 <span>#</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
544 <span>&</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
545 <span>~</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
546 <span>|</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
547 <span>==</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
548 <span>~=</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
549 <span><=</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
550 <span>>=</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
551 <span><</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
552 <span>></span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
553 <span>=</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
554 <span>(</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
555 <span>)</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
556 <span>{</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
557 <span>}</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
558 <span>[</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
559 <span>]</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
560 <span>;</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
561 <span>,</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
562 <span>.</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
563 <span>..</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
564 <span>...</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
565 <span>%></span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
566 <span><%</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
567 <span><%=</span> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
568 </p> |
1656 | 569 |
570 <p> | |
571 <em>Literal strings</em> | |
572 can be delimited by matching single or double quotes, | |
573 and can contain the following C-like escape sequences: | |
574 '<code>\a</code>' (bell), | |
575 '<code>\b</code>' (backspace), | |
576 '<code>\f</code>' (form feed), | |
577 '<code>\n</code>' (newline), | |
578 '<code>\r</code>' (carriage return), | |
579 '<code>\t</code>' (horizontal tab), | |
580 '<code>\v</code>' (vertical tab), | |
581 '<code>\\</code>' (backslash), | |
582 '<code>\"</code>' (quotation mark [double quote]), | |
583 and '<code>\'</code>' (apostrophe [single quote]). | |
584 A backslash followed by a real newline | |
585 results in a newline in the string. | |
586 The escape sequence '<code>\z</code>' skips the following span | |
587 of white-space characters, | |
588 including line breaks; | |
589 it is particularly useful to break and indent a long literal string | |
590 into multiple lines without adding the newlines and spaces | |
591 into the string contents. | |
592 </p> | |
593 | |
594 <p> | |
595 Luan can specify any character in a literal string by its numerical value. | |
596 This can be done | |
597 with the escape sequence <code>\x<em>XX</em></code>, | |
598 where <em>XX</em> is a sequence of exactly two hexadecimal digits, | |
599 or with the escape sequence <code>\u<em>XXXX</em></code>, | |
600 where <em>XXXX</em> is a sequence of exactly four hexadecimal digits, | |
601 or with the escape sequence <code>\<em>ddd</em></code>, | |
602 where <em>ddd</em> is a sequence of up to three decimal digits. | |
603 (Note that if a decimal escape sequence is to be followed by a digit, | |
604 it must be expressed using exactly three digits.) | |
605 </p> | |
606 | |
607 <p> | |
608 Literal strings can also be defined using a long format | |
609 enclosed by <em>long brackets</em>. | |
610 We define an <em>opening long bracket of level <em>n</em></em> as an opening | |
611 square bracket followed by <em>n</em> equal signs followed by another | |
612 opening square bracket. | |
613 So, an opening long bracket of level 0 is written as <code>[[</code>, | |
614 an opening long bracket of level 1 is written as <code>[=[</code>, | |
615 and so on. | |
616 A <em>closing long bracket</em> is defined similarly; | |
617 for instance, | |
618 a closing long bracket of level 4 is written as <code>]====]</code>. | |
619 A <em>long literal</em> starts with an opening long bracket of any level and | |
620 ends at the first closing long bracket of the same level. | |
621 It can contain any text except a closing bracket of the same level. | |
622 Literals in this bracketed form can run for several lines, | |
623 do not interpret any escape sequences, | |
624 and ignore long brackets of any other level. | |
625 Any kind of end-of-line sequence | |
626 (carriage return, newline, carriage return followed by newline, | |
627 or newline followed by carriage return) | |
628 is converted to a simple newline. | |
629 </p> | |
630 | |
631 <p> | |
632 Any character in a literal string not | |
633 explicitly affected by the previous rules represents itself. | |
634 However, Luan opens files for parsing in text mode, | |
635 and the system file functions may have problems with | |
636 some control characters. | |
637 So, it is safer to represent | |
638 non-text data as a quoted literal with | |
639 explicit escape sequences for non-text characters. | |
640 </p> | |
641 | |
642 <p> | |
643 For convenience, | |
644 when the opening long bracket is immediately followed by a newline, | |
645 the newline is not included in the string. | |
646 As an example | |
647 the five literal strings below denote the same string: | |
648 </p> | |
649 | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
650 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
651 a = 'alo\n123"' |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
652 a = "alo\n123\"" |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
653 a = '\97lo\10\04923"' |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
654 a = [[alo |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
655 123"]] |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
656 a = [==[ |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
657 alo |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
658 123"]==] |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
659 </code> |
1656 | 660 |
661 <p> | |
662 A <em>numerical constant</em> (or <em>numeral</em>) | |
663 can be written with an optional fractional part | |
664 and an optional decimal exponent, | |
665 marked by a letter '<code>e</code>' or '<code>E</code>'. | |
666 Luan also accepts hexadecimal constants, | |
667 which start with <code>0x</code> or <code>0X</code>. | |
668 Hexadecimal constants also accept an optional fractional part | |
669 plus an optional binary exponent, | |
670 marked by a letter '<code>p</code>' or '<code>P</code>'. | |
671 A numeric constant with a fractional dot or an exponent | |
672 denotes a float; | |
673 otherwise it denotes an integer. | |
674 Examples of valid integer constants are | |
675 </p> | |
676 | |
677 <pre> | |
678 3 345 0xff 0xBEBADA | |
679 </pre> | |
680 | |
681 <p> | |
682 Examples of valid float constants are | |
683 </p> | |
684 | |
685 <pre> | |
686 3.0 3.1416 314.16e-2 0.31416E1 34e1 | |
687 0x0.1E 0xA23p-4 0X1.921FB54442D18P+1 | |
688 </pre> | |
689 | |
690 <p> | |
691 A <em>comment</em> starts with a double hyphen (<code>--</code>) | |
692 anywhere outside a string. | |
693 If the text immediately after <code>--</code> is not an opening long bracket, | |
694 the comment is a <em>short comment</em>, | |
695 which runs until the end of the line. | |
696 Otherwise, it is a <em>long comment</em>, | |
697 which runs until the corresponding closing long bracket. | |
698 Long comments are frequently used to disable code temporarily. | |
699 </p> | |
700 <% | |
701 end | |
702 } | |
703 vars = { | |
704 title = "Variables" | |
705 content = function() | |
706 %> | |
707 <p> | |
708 Variables are places that store values. | |
709 There are three kinds of variables in Luan: | |
710 global variables, local variables, and table fields. | |
711 </p> | |
712 | |
713 <p> | |
714 A single name can denote a global variable or a local variable | |
715 (or a function's formal parameter, | |
716 which is a particular kind of local variable): | |
717 </p> | |
718 | |
719 <pre> | |
720 var ::= Name | |
721 </pre> | |
722 | |
723 <p> | |
724 Name denotes identifiers, as defined in <a href="#lex">Lexical Conventions</a>. | |
725 </p> | |
726 | |
727 <p> | |
728 Local variables are <em>lexically scoped</em>: | |
729 local variables can be freely accessed by functions | |
730 defined inside their scope (see <a href="#visibility">Visibility Rules</a>). | |
731 </p> | |
732 | |
733 <p> | |
734 Before the first assignment to a variable, its value is <b>nil</b>. | |
735 </p> | |
736 | |
737 <p> | |
738 Square brackets are used to index a table: | |
739 </p> | |
740 | |
741 <pre> | |
742 var ::= prefixexp ‘<b>[</b>’ exp ‘<b>]</b>’ | |
743 </pre> | |
744 | |
745 <p> | |
746 The meaning of accesses to table fields can be changed via metatables. | |
747 An access to an indexed variable <code>t[i]</code> is equivalent to | |
748 a call <code>gettable_event(t,i)</code>. | |
749 (See <a href="#meta">Metatables and Metamethods</a> for a complete description of the | |
750 <code>gettable_event</code> function. | |
751 This function is not defined or callable in Luan. | |
752 We use it here only for explanatory purposes.) | |
753 </p> | |
754 | |
755 <p> | |
756 The syntax <code>var.Name</code> is just syntactic sugar for | |
757 <code>var["Name"]</code>: | |
758 </p> | |
759 | |
760 <pre> | |
761 var ::= prefixexp ‘<b>.</b>’ Name | |
762 </pre> | |
763 | |
764 <p> | |
765 Global variables are not available by default. To enable global variable, you must define <code>_ENV</code> as a local variable whose value is a table. If <code>_ENV</code> is not defined, then an unrecognized variable name will produce a compile error. If <code>_ENV</code> is defined then an access to an unrecognized variable name will be consider a global variable. So then an acces to global variable <code>x</code> | |
766 is equivalent to <code>_ENV.x</code>. | |
767 Due to the way that chunks are compiled, | |
768 <code>_ENV</code> is never a global name (see <a href="#env">Environments</a>). | |
769 </p> | |
770 <% | |
771 end | |
772 } | |
773 stmt = { | |
774 title = "Statements" | |
775 content = function() | |
776 %> | |
777 <p> | |
778 Luan supports an almost conventional set of statements, | |
779 similar to those in Pascal or C. | |
780 This set includes | |
781 assignments, control structures, function calls, | |
782 and variable declarations. | |
783 </p> | |
784 <% | |
785 end | |
786 subs = { | |
787 blocks = { | |
788 title = "Blocks" | |
789 content = function() | |
790 %> | |
791 <p> | |
792 A block is a list of statements, | |
793 which are executed sequentially: | |
794 </p> | |
795 | |
796 <pre> | |
797 block ::= {stat} | |
798 </pre> | |
799 | |
800 <p> | |
801 Luan has <em>empty statements</em> | |
802 that allow you to separate statements with semicolons, | |
803 start a block with a semicolon | |
804 or write two semicolons in sequence: | |
805 </p> | |
806 | |
807 <pre> | |
808 stat ::= ‘<b>;</b>’ | |
809 </pre> | |
810 | |
811 <p> | |
812 A block can be explicitly delimited to produce a single statement: | |
813 </p> | |
814 | |
815 <pre> | |
816 stat ::= <b>do</b> block end_do | |
817 end_do ::= <b>end_do</b> | <b>end</b> | |
818 </pre> | |
819 | |
820 <p> | |
821 Explicit blocks are useful | |
822 to control the scope of variable declarations. | |
823 Explicit blocks are also sometimes used to | |
824 add a <b>return</b> statement in the middle | |
825 of another block (see <a href="#control">Control Structures</a>). | |
826 </p> | |
827 <% | |
828 end | |
829 } | |
830 chunks = { | |
831 title = "Chunks" | |
832 content = function() | |
833 %> | |
834 <p> | |
835 The unit of compilation of Luan is called a <em>chunk</em>. | |
836 Syntactically, | |
837 a chunk is simply a block: | |
838 </p> | |
839 | |
840 <pre> | |
841 chunk ::= block | |
842 </pre> | |
843 | |
844 <p> | |
845 Luan handles a chunk as the body of an anonymous function | |
846 with a variable number of arguments | |
847 (see <a href="#fn_def">Function Definitions</a>). | |
848 As such, chunks can define local variables, | |
849 receive arguments, and return values. | |
850 </p> | |
851 | |
852 <p> | |
853 A chunk can be stored in a file or in a string inside the host program. | |
854 To execute a chunk, | |
855 Luan first <em>loads</em> it, | |
856 compiling the chunk's code, | |
857 and then Luan executes the compiled code. | |
858 </p> | |
859 <% | |
860 end | |
861 } | |
862 assignment = { | |
863 title = "Assignment" | |
864 content = function() | |
865 %> | |
866 <p> | |
867 Luan allows multiple assignments. | |
868 Therefore, the syntax for assignment | |
869 defines a list of variables on the left side | |
870 and a list of expressions on the right side. | |
871 The elements in both lists are separated by commas: | |
872 </p> | |
873 | |
874 <pre> | |
875 stat ::= varlist ‘<b>=</b>’ explist | |
876 varlist ::= var {‘<b>,</b>’ var} | |
877 explist ::= exp {‘<b>,</b>’ exp} | |
878 </pre> | |
879 | |
880 <p> | |
881 Expressions are discussed in <a href="#expressions">Expressions</a>. | |
882 </p> | |
883 | |
884 <p> | |
885 Before the assignment, | |
886 the list of values is <em>adjusted</em> to the length of | |
887 the list of variables. | |
888 If there are more values than needed, | |
889 the excess values are thrown away. | |
890 If there are fewer values than needed, | |
891 the list is extended with as many <b>nil</b>'s as needed. | |
892 If the list of expressions ends with a function call, | |
893 then all values returned by that call enter the list of values, | |
894 before the adjustment | |
895 (except when the call is enclosed in parentheses; see <a href="#expressions">Expressions</a>). | |
896 </p> | |
897 | |
898 <p> | |
899 The assignment statement first evaluates all its expressions | |
900 and only then the assignments are performed. | |
901 Thus the code | |
902 </p> | |
903 | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
904 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
905 i = 3 |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
906 i, a[i] = i+1, 20 |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
907 </code> |
1656 | 908 |
909 <p> | |
910 sets <code>a[3]</code> to 20, without affecting <code>a[4]</code> | |
911 because the <code>i</code> in <code>a[i]</code> is evaluated (to 3) | |
912 before it is assigned 4. | |
913 Similarly, the line | |
914 </p> | |
915 | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
916 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
917 x, y = y, x |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
918 </code> |
1656 | 919 |
920 <p> | |
921 exchanges the values of <code>x</code> and <code>y</code>, | |
922 and | |
923 </p> | |
924 | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
925 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
926 x, y, z = y, z, x |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
927 </code> |
1656 | 928 |
929 <p> | |
930 cyclically permutes the values of <code>x</code>, <code>y</code>, and <code>z</code>. | |
931 </p> | |
932 | |
933 <p> | |
934 The meaning of assignments to global variables | |
935 and table fields can be changed via metatables. | |
936 An assignment to an indexed variable <code>t[i] = val</code> is equivalent to | |
937 <code>settable_event(t,i,val)</code>. | |
938 (See <a href="#meta">Metatables and Metamethods</a> for a complete description of the | |
939 <code>settable_event</code> function. | |
940 This function is not defined or callable in Luan. | |
941 We use it here only for explanatory purposes.) | |
942 </p> | |
943 | |
944 <p> | |
945 An assignment to a global name <code>x = val</code> | |
946 is equivalent to the assignment | |
947 <code>_ENV.x = val</code> (see <a href="#env">Environments</a>). | |
948 Global names are only available when <code>_ENV</code> is defined. | |
949 </p> | |
950 <% | |
951 end | |
952 } | |
1660 | 953 control = { |
954 title = "Control Structures" | |
955 content = function() | |
956 %> | |
957 <p> | |
958 The control structures | |
959 <b>if</b>, <b>while</b>, and <b>repeat</b> have the usual meaning and | |
960 familiar syntax: | |
961 </p> | |
962 | |
963 <pre> | |
964 stat ::= <b>while</b> exp <b>do</b> block end_while | |
965 stat ::= <b>repeat</b> block <b>until</b> exp | |
966 stat ::= <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] end_if | |
967 end_while ::= <b>end_while</b> | <b>end</b> | |
968 end_if ::= <b>end_if</b> | <b>end</b> | |
969 </pre> | |
970 | |
971 <p> | |
972 Luan also has a <b>for</b> statement (see <a href="#for">For Statement</a>). | |
973 </p> | |
974 | |
975 <p> | |
976 The condition expression of a | |
977 control structure must be a boolean. | |
978 Any other value type will produce an error. | |
979 This helps catch errors and makes code more readable. | |
980 </p> | |
981 | |
982 <p> | |
983 In the <b>repeat</b>–<b>until</b> loop, | |
984 the inner block does not end at the <b>until</b> keyword, | |
985 but only after the condition. | |
986 So, the condition can refer to local variables | |
987 declared inside the loop block. | |
988 </p> | |
989 | |
990 <p> | |
991 The <b>break</b> statement terminates the execution of a | |
992 <b>while</b>, <b>repeat</b>, or <b>for</b> loop, | |
993 skipping to the next statement after the loop: | |
994 </p> | |
995 | |
996 <pre> | |
997 stat ::= <b>break</b> | |
998 </pre> | |
999 | |
1000 <p> | |
1001 A <b>break</b> ends the innermost enclosing loop. | |
1002 </p> | |
1003 | |
1004 <p> | |
1005 The <b>continue</b> statement jumps to the beginning of a | |
1006 <b>while</b>, <b>repeat</b>, or <b>for</b> loop for next iteration, | |
1007 skipping the execution of statements inside the body of loop for the current iteration: | |
1008 </p> | |
1009 | |
1010 <pre> | |
1011 stat ::= <b>continue</b> | |
1012 </pre> | |
1013 | |
1014 <p> | |
1015 The <b>return</b> statement is used to return values | |
1016 from a function or a chunk | |
1017 (which is an anonymous function). | |
1018 Functions can return more than one value, | |
1019 so the syntax for the <b>return</b> statement is | |
1020 </p> | |
1021 | |
1022 <pre> | |
1023 stat ::= <b>return</b> [explist] [‘<b>;</b>’] | |
1024 </pre> | |
1025 <% | |
1026 end | |
1027 } | |
1028 ["for"] = { | |
1029 title = "For Statement" | |
1030 content = function() | |
1031 %> | |
1032 <p> | |
1033 The <b>for</b> statement works over functions, | |
1034 called <em>iterators</em>. | |
1035 On each iteration, the iterator function is called to produce a new value, | |
1036 stopping when this new value is <b>nil</b>. | |
1037 The <b>for</b> loop has the following syntax: | |
1038 </p> | |
1039 | |
1040 <pre> | |
1041 stat ::= <b>for</b> namelist <b>in</b> exp <b>do</b> block end_for | |
1042 namelist ::= Name {‘<b>,</b>’ Name} | |
1043 end_for ::= <b>end_for</b> | <b>end</b> | |
1044 </pre> | |
1045 | |
1046 <p> | |
1047 A <b>for</b> statement like | |
1048 </p> | |
1049 | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1050 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1051 for <em>var_1</em>, ···, <em>var_n</em> in <em>exp</em> do <em>block</em> end |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1052 </code> |
1660 | 1053 |
1054 <p> | |
1055 is equivalent to the code: | |
1056 </p> | |
1057 | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1058 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1059 do |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1060 local <em>f</em> = <em>exp</em> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1061 while true do |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1062 local <em>var_1</em>, ···, <em>var_n</em> = <em>f</em>() |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1063 if <em>var_1</em> == nil then break end |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1064 <em>block</em> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1065 end |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1066 end |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1067 </code> |
1660 | 1068 |
1069 <p> | |
1070 Note the following: | |
1071 </p> | |
1072 | |
1073 <ul> | |
1074 <li> | |
1075 <code><em>exp</em></code> is evaluated only once. | |
1076 Its result is an <em>iterator</em> function. | |
1077 </li> | |
1078 <li> | |
1079 <code><em>f</em></code> is an invisible variable. | |
1080 The name is here for explanatory purposes only. | |
1081 </li> | |
1082 <li> | |
1083 You can use <b>break</b> to exit a <b>for</b> loop. | |
1084 </li> | |
1085 <li> | |
1086 The loop variables <code><em>var_i</em></code> are local to the loop; | |
1087 you cannot use their values after the <b>for</b> ends. | |
1088 If you need these values, | |
1089 then assign them to other variables before breaking or exiting the loop. | |
1090 </li> | |
1091 </ul> | |
1092 <% | |
1093 end | |
1094 } | |
1095 ["try"] = { | |
1096 title = "Try Statement" | |
1097 content = function() | |
1098 %> | |
1099 <p> | |
1100 The <b>try</b> statement has the same semantics as in Java. | |
1101 </p> | |
1102 | |
1103 <pre> | |
1104 stat ::= <b>try</b> block [<b>catch</b> Name block] [<b>finally</b> block] end_try | |
1105 end_try ::= <b>end_try</b> | <b>end</b> | |
1106 </pre> | |
1107 <% | |
1108 end | |
1109 } | |
1110 fn_stmt = { | |
1111 title = "Function Calls as Statements" | |
1112 content = function() | |
1113 %> | |
1114 <p> | |
1115 To allow possible side-effects, | |
1116 function calls can be executed as statements: | |
1117 </p> | |
1118 | |
1119 <pre> | |
1120 stat ::= functioncall | |
1121 </pre> | |
1122 | |
1123 <p> | |
1124 In this case, all returned values are thrown away. | |
1125 Function calls are explained in <a href="#fn_calls">Function Calls</a>. | |
1126 </p> | |
1127 <% | |
1128 end | |
1129 } | |
1130 logical_stmt = { | |
1131 title = "Logical Statement" | |
1132 content = function() | |
1133 %> | |
1134 <p> | |
1135 <a href="#logical_ops">Logical expressions</a> can be statements. | |
1136 This is useful in cases like this: | |
1137 </p> | |
1138 | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1139 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1140 x==5 or error "x should be 5" |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1141 </code> |
1660 | 1142 <% |
1143 end | |
1144 } | |
1145 local_stmt = { | |
1146 title = "Local Declarations" | |
1147 content = function() | |
1148 %> | |
1149 <p> | |
1150 Local variables can be declared anywhere inside a block. | |
1151 The declaration can include an initial assignment: | |
1152 </p> | |
1153 | |
1154 <pre> | |
1155 stat ::= <b>local</b> namelist [‘<b>=</b>’ explist] | |
1156 </pre> | |
1157 | |
1158 <p> | |
1159 If present, an initial assignment has the same semantics | |
1160 of a multiple assignment (see <a href="#assignment">Assignment</a>). | |
1161 Otherwise, all variables are initialized with <b>nil</b>. | |
1162 </p> | |
1163 | |
1164 <p> | |
1165 A chunk is also a block (see <a href="#chunks">Chunks</a>), | |
1166 and so local variables can be declared in a chunk outside any explicit block. | |
1167 </p> | |
1168 | |
1169 <p> | |
1170 The visibility rules for local variables are explained in <a href="#visibility">Visibility Rules</a>. | |
1171 </p> | |
1172 <% | |
1173 end | |
1174 } | |
1175 template_stmt = { | |
1176 title = "Template Statements" | |
1177 content = function() | |
1178 %> | |
1827 | 1179 <p>Template statements provide the full equivalent of <a href="https://en.wikipedia.org/wiki/Jakarta_Server_Pages">JSP</a> but in a general way. Template statements write to standard output. For example:</p> |
1660 | 1180 </p> |
1181 | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1182 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1183 local name = "Bob" |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1184 %> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1185 Hello <%= name %>! |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1186 Bye <%= name %>. |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1187 <% |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1188 </code> |
1660 | 1189 |
1190 <p> | |
1191 is equivalent to the code: | |
1192 </p> | |
1193 | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1194 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1195 local name = "Bob" |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1196 require("luan:Io.luan").stdout.write( "Hello ", name , "!\nBye ", name , ".\n" ) |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1197 </code> |
1660 | 1198 <% |
1199 end | |
1200 } | |
1201 } | |
1202 } | |
1203 expressions = { | |
1204 title = "Expressions" | |
1205 content = function() | |
1206 %> | |
1207 <p> | |
1208 The basic expressions in Luan are the following: | |
1209 </p> | |
1210 | |
1211 <pre> | |
1212 exp ::= prefixexp | |
1213 exp ::= <b>nil</b> | <b>false</b> | <b>true</b> | |
1214 exp ::= Numeral | |
1215 exp ::= LiteralString | |
1216 exp ::= functiondef | |
1217 exp ::= tableconstructor | |
1218 exp ::= ‘<b>...</b>’ | |
1219 exp ::= exp binop exp | |
1220 exp ::= unop exp | |
1221 prefixexp ::= var | functioncall | ‘<b>(</b>’ exp ‘<b>)</b>’ | |
1222 </pre> | |
1223 | |
1224 <p> | |
1225 Numerals and literal strings are explained in <a href="#lex">Lexical Conventions</a>; | |
1226 variables are explained in <a href="#vars">Variables</a>; | |
1227 function definitions are explained in <a href="#fn_def">Function Definitions</a>; | |
1228 function calls are explained in <a href="#fn_calls">Function Calls</a>; | |
1229 table constructors are explained in <a href="#constructors">Table Constructors</a>. | |
1230 Vararg expressions, | |
1231 denoted by three dots ('<code>...</code>'), can only be used when | |
1232 directly inside a vararg function; | |
1233 they are explained in <a href="#fn_def">Function Definitions</a>. | |
1234 </p> | |
1235 | |
1236 <p> | |
1237 Binary operators comprise arithmetic operators (see <a href="#arithmetic">Arithmetic Operators</a>), | |
1238 relational operators (see <a href="#relational">Relational Operators</a>), logical operators (see <a href="#logical_ops">Logical Operators</a>), | |
1239 and the concatenation operator (see <a href="#concatenation">Concatenation</a>). | |
1240 Unary operators comprise the unary minus (see <a href="#arithmetic">Arithmetic Operators</a>), | |
1241 the unary logical <b>not</b> (see <a href="#logical_ops">Logical Operators</a>), | |
1242 and the unary <em>length operator</em> (see <a href="#length">The Length Operator</a>). | |
1243 </p> | |
1244 | |
1245 <p> | |
1246 Both function calls and vararg expressions can result in multiple values. | |
1247 If a function call is used as a statement (see <a href="#fn_stmt">Function Calls as Statements</a>), | |
1248 then its return list is adjusted to zero elements, | |
1249 thus discarding all returned values. | |
1250 If an expression is used as the last (or the only) element | |
1251 of a list of expressions, | |
1252 then no adjustment is made | |
1253 (unless the expression is enclosed in parentheses). | |
1254 In all other contexts, | |
1255 Luan adjusts the result list to one element, | |
1256 either discarding all values except the first one | |
1257 or adding a single <b>nil</b> if there are no values. | |
1258 </p> | |
1259 | |
1260 <p> | |
1261 Here are some examples: | |
1262 </p> | |
1263 | |
1264 <pre> | |
1265 f() -- adjusted to 0 results | |
1266 g(f(), x) -- f() is adjusted to 1 result | |
1267 g(x, f()) -- g gets x plus all results from f() | |
1268 a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil) | |
1269 a,b = ... -- a gets the first vararg parameter, b gets | |
1270 -- the second (both a and b can get nil if there | |
1271 -- is no corresponding vararg parameter) | |
1272 | |
1273 a,b,c = x, f() -- f() is adjusted to 2 results | |
1274 a,b,c = f() -- f() is adjusted to 3 results | |
1275 return f() -- returns all results from f() | |
1276 return ... -- returns all received vararg parameters | |
1277 return x,y,f() -- returns x, y, and all results from f() | |
1278 {f()} -- creates a list with all results from f() | |
1279 {...} -- creates a list with all vararg parameters | |
1280 {f(), nil} -- f() is adjusted to 1 result | |
1281 </pre> | |
1282 | |
1283 <p> | |
1284 Any expression enclosed in parentheses always results in only one value. | |
1285 Thus, | |
1286 <code>(f(x,y,z))</code> is always a single value, | |
1287 even if <code>f</code> returns several values. | |
1288 (The value of <code>(f(x,y,z))</code> is the first value returned by <code>f</code> | |
1289 or <b>nil</b> if <code>f</code> does not return any values.) | |
1290 </p> | |
1291 <% | |
1292 end | |
1293 subs = { | |
1294 arithmetic = { | |
1295 title = "Arithmetic Operators" | |
1296 content = function() | |
1297 %> | |
1298 <p> | |
1299 Luan supports the following arithmetic operators: | |
1300 </p> | |
1301 | |
1302 <ul> | |
1303 <li><b><code>+</code>: </b>addition</li> | |
1304 <li><b><code>-</code>: </b>subtraction</li> | |
1305 <li><b><code>*</code>: </b>multiplication</li> | |
1680 | 1306 <li><b><code>/</code>: </b>float division</li> |
1307 <li><b><code>//</code>: </b>floor division</li> | |
1660 | 1308 <li><b><code>%</code>: </b>modulo</li> |
1309 <li><b><code>^</code>: </b>exponentiation</li> | |
1310 <li><b><code>-</code>: </b>unary minus</li> | |
1311 </ul> | |
1312 | |
1313 <p> | |
1816 | 1314 Addition, subtraction, multiplication, division, and unary minus are the same as these operators in Java. Exponentiation uses Java's <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#pow-double-double-">Math.pow</a> function. |
1660 | 1315 </p> |
1316 | |
1317 <p> | |
1680 | 1318 Floor division (//) is a division that rounds the quotient towards minus infinity, that is, the floor of the division of its operands. |
1319 </p> | |
1320 | |
1321 <p> | |
1660 | 1322 Modulo is defined as the remainder of a division |
1323 that rounds the quotient towards minus infinite (floor division). | |
1324 (The Java modulo operator is not used.) | |
1325 </p> | |
1326 <% | |
1327 end | |
1328 } | |
1329 conversions = { | |
1330 title = "Coercions and Conversions" | |
1331 content = function() | |
1332 %> | |
1333 <p> | |
1334 Luan generally avoids automatic conversions. | |
1335 String concatenation automatically converts all of its arguments to strings. | |
1336 </p> | |
1337 | |
1338 <p> | |
1339 Luan provides library functions for explicit type conversions. | |
1340 </p> | |
1341 <% | |
1342 end | |
1343 } | |
1344 relational = { | |
1345 title = "Relational Operators" | |
1346 content = function() | |
1347 %> | |
1348 <p> | |
1349 Luan supports the following relational operators: | |
1350 </p> | |
1351 | |
1352 <ul> | |
1353 <li><b><code>==</code>: </b>equality</li> | |
1354 <li><b><code>~=</code>: </b>inequality</li> | |
1355 <li><b><code><</code>: </b>less than</li> | |
1356 <li><b><code>></code>: </b>greater than</li> | |
1357 <li><b><code><=</code>: </b>less or equal</li> | |
1358 <li><b><code>>=</code>: </b>greater or equal</li> | |
1359 </ul> | |
1360 | |
1361 <p> | |
1362 These operators always result in <b>false</b> or <b>true</b>. | |
1363 </p> | |
1364 | |
1365 <p> | |
1366 Equality (<code>==</code>) first compares the type of its operands. | |
1367 If the types are different, then the result is <b>false</b>. | |
1368 Otherwise, the values of the operands are compared. | |
1369 Strings, numbers, and binary values are compared in the obvious way (by value). | |
1370 </p> | |
1371 | |
1372 <p> | |
1373 Tables | |
1374 are compared by reference: | |
1375 two objects are considered equal only if they are the same object. | |
1376 Every time you create a new table, | |
1377 it is different from any previously existing table. | |
1378 Closures are also compared by reference. | |
1379 </p> | |
1380 | |
1381 <p> | |
1382 You can change the way that Luan compares tables | |
1383 by using the "eq" metamethod (see <a href="#meta">Metatables and Metamethods</a>). | |
1384 </p> | |
1385 | |
1386 <p> | |
1816 | 1387 Java values are compared for equality with the Java <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-"><code>equals</code></a> method. |
1660 | 1388 </p> |
1389 | |
1390 <p> | |
1391 Equality comparisons do not convert strings to numbers | |
1392 or vice versa. | |
1393 Thus, <code>"0"==0</code> evaluates to <b>false</b>, | |
1394 and <code>t[0]</code> and <code>t["0"]</code> denote different | |
1395 entries in a table. | |
1396 </p> | |
1397 | |
1398 <p> | |
1399 The operator <code>~=</code> is exactly the negation of equality (<code>==</code>). | |
1400 </p> | |
1401 | |
1402 <p> | |
1403 The order operators work as follows. | |
1404 If both arguments are numbers, | |
1405 then they are compared following | |
1406 the usual rule for binary operations. | |
1407 Otherwise, if both arguments are strings, | |
1408 then their values are compared according to the current locale. | |
1409 Otherwise, Luan tries to call the "lt" or the "le" | |
1410 metamethod (see <a href="#meta">Metatables and Metamethods</a>). | |
1411 A comparison <code>a > b</code> is translated to <code>b < a</code> | |
1412 and <code>a >= b</code> is translated to <code>b <= a</code>. | |
1413 </p> | |
1414 <% | |
1415 end | |
1416 } | |
1667 | 1417 logical_ops = { |
1418 title = "Logical Operators" | |
1419 content = function() | |
1420 %> | |
1421 <p> | |
1422 The logical operators in Luan are | |
1423 <b>and</b>, <b>or</b>, and <b>not</b>. | |
1424 The <b>and</b> and <b>or</b> operators consider both <b>false</b> and <b>nil</b> as false | |
1425 and anything else as true. | |
1426 Like the control structures (see <a href="#control">Control Structures</a>), | |
1427 the <b>not</b> operator requires a boolean value. | |
1428 </p> | |
1429 | |
1430 <p> | |
1431 The negation operator <b>not</b> always returns <b>false</b> or <b>true</b>. | |
1432 The conjunction operator <b>and</b> returns its first argument | |
1433 if this value is <b>false</b> or <b>nil</b>; | |
1434 otherwise, <b>and</b> returns its second argument. | |
1435 The disjunction operator <b>or</b> returns its first argument | |
1436 if this value is different from <b>nil</b> and <b>false</b>; | |
1437 otherwise, <b>or</b> returns its second argument. | |
1438 Both <b>and</b> and <b>or</b> use short-circuit evaluation; | |
1439 that is, | |
1440 the second operand is evaluated only if necessary. | |
1441 Here are some examples: | |
1442 </p> | |
1443 | |
1444 <pre> | |
1445 10 or 20 --> 10 | |
1446 10 or error() --> 10 | |
1447 nil or "a" --> "a" | |
1448 nil and 10 --> nil | |
1449 false and error() --> false | |
1450 false and nil --> false | |
1451 false or nil --> nil | |
1452 10 and 20 --> 20 | |
1453 </pre> | |
1454 | |
1455 <p> | |
1456 (In this manual, | |
1457 <code>--></code> indicates the result of the preceding expression.) | |
1458 </p> | |
1459 <% | |
1460 end | |
1461 } | |
1462 concatenation = { | |
1463 title = "Concatenation" | |
1464 content = function() | |
1465 %> | |
1466 <p> | |
1467 The string concatenation operator in Luan is | |
1468 denoted by two dots ('<code>..</code>'). | |
1469 All operands are converted to strings. | |
1470 </p> | |
1471 <% | |
1472 end | |
1473 } | |
1474 length = { | |
1475 title = "The Length Operator" | |
1476 content = function() | |
1477 %> | |
1478 <p> | |
1479 The length operator is denoted by the unary prefix operator <code>#</code>. | |
1480 The length of a string is its number of characters. | |
1481 The length of a binary is its number of bytes. | |
1482 </p> | |
1483 | |
1484 <p> | |
1485 A program can modify the behavior of the length operator for | |
1486 any table through the <code>__len</code> metamethod (see <a href="#meta">Metatables and Metamethods</a>). | |
1487 </p> | |
1488 | |
1489 <p> | |
1490 Unless a <code>__len</code> metamethod is given, | |
1491 the length of a table <code>t</code> is defined | |
1492 as the number of elements in <em>sequence</em>, | |
1493 that is, | |
1494 the size of the set of its positive numeric keys is equal to <em>{1..n}</em> | |
1495 for some non-negative integer <em>n</em>. | |
1496 In that case, <em>n</em> is its length. | |
1497 Note that a table like | |
1498 </p> | |
1499 | |
1500 <pre> | |
1501 {10, 20, nil, 40} | |
1502 </pre> | |
1503 | |
1504 <p> | |
1505 has a length of <code>2</code>, because that is the last key in sequence. | |
1506 </p> | |
1507 <% | |
1508 end | |
1509 } | |
1510 precedence = { | |
1511 title = "Precedence" | |
1512 content = function() | |
1513 %> | |
1514 <p> | |
1515 Operator precedence in Luan follows the table below, | |
1516 from lower to higher priority: | |
1517 </p> | |
1518 | |
1519 <pre> | |
1520 or | |
1521 and | |
1522 < > <= >= ~= == | |
1523 .. | |
1524 + - | |
1525 * / % | |
1526 unary operators (not # -) | |
1527 ^ | |
1528 </pre> | |
1529 | |
1530 <p> | |
1531 As usual, | |
1532 you can use parentheses to change the precedences of an expression. | |
1533 The concatenation ('<code>..</code>') and exponentiation ('<code>^</code>') | |
1534 operators are right associative. | |
1535 All other binary operators are left associative. | |
1536 </p> | |
1537 <% | |
1538 end | |
1539 } | |
1540 constructors = { | |
1541 title = "Table Constructors" | |
1542 content = function() | |
1543 %> | |
1544 <p> | |
1545 Table constructors are expressions that create tables. | |
1546 Every time a constructor is evaluated, a new table is created. | |
1547 A constructor can be used to create an empty table | |
1548 or to create a table and initialize some of its fields. | |
1549 The general syntax for constructors is | |
1550 </p> | |
1551 | |
1552 <pre> | |
1553 tableconstructor ::= ‘<b>{</b>’ fieldlist ‘<b>}</b>’ | |
1554 fieldlist ::= [field] {fieldsep [field]} | |
1555 field ::= ‘<b>[</b>’ exp ‘<b>]</b>’ ‘<b>=</b>’ exp | Name ‘<b>=</b>’ exp | exp | |
1556 fieldsep ::= ‘<b>,</b>’ | ‘<b>;</b>’ | <b>end_of_line</b> | |
1557 </pre> | |
1558 | |
1559 <p> | |
1560 Each field of the form <code>[exp1] = exp2</code> adds to the new table an entry | |
1561 with key <code>exp1</code> and value <code>exp2</code>. | |
1562 A field of the form <code>name = exp</code> is equivalent to | |
1563 <code>["name"] = exp</code>. | |
1564 Finally, fields of the form <code>exp</code> are equivalent to | |
1565 <code>[i] = exp</code>, where <code>i</code> are consecutive integers | |
1566 starting with 1. | |
1567 Fields in the other formats do not affect this counting. | |
1568 For example, | |
1569 </p> | |
1570 | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1571 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1572 a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 } |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1573 </code> |
1667 | 1574 |
1575 <p> | |
1576 is equivalent to | |
1577 </p> | |
1578 | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1579 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1580 do |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1581 local t = {} |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1582 t[f(1)] = g |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1583 t[1] = "x" -- 1st exp |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1584 t[2] = "y" -- 2nd exp |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1585 t.x = 1 -- t["x"] = 1 |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1586 t[3] = f(x) -- 3rd exp |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1587 t[30] = 23 |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1588 t[4] = 45 -- 4th exp |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1589 a = t |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1590 end |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1591 </code> |
1667 | 1592 |
1593 <p> | |
1594 The order of the assignments in a constructor is undefined. | |
1595 (This order would be relevant only when there are repeated keys.) | |
1596 </p> | |
1597 | |
1598 <p> | |
1599 If the last field in the list has the form <code>exp</code> | |
1600 and the expression is a function call or a vararg expression, | |
1601 then all values returned by this expression enter the list consecutively | |
1602 (see <a href="#fn_calls">Function Calls</a>). | |
1603 </p> | |
1604 | |
1605 <p> | |
1606 The field list can have an optional trailing separator, | |
1607 as a convenience for machine-generated code. | |
1608 </p> | |
1609 <% | |
1610 end | |
1611 } | |
1612 fn_calls = { | |
1613 title = "Function Calls" | |
1614 content = function() | |
1615 %> | |
1616 <p> | |
1617 A function call in Luan has the following syntax: | |
1618 </p> | |
1619 | |
1620 <pre> | |
1621 functioncall ::= prefixexp args | |
1622 </pre> | |
1623 | |
1624 <p> | |
1625 In a function call, | |
1626 first prefixexp and args are evaluated. | |
1627 The value of prefixexp must have type <em>function</em>. | |
1628 This function is called | |
1629 with the given arguments. | |
1630 </p> | |
1631 | |
1632 <p> | |
1633 Arguments have the following syntax: | |
1634 </p> | |
1635 | |
1636 <pre> | |
1637 args ::= ‘<b>(</b>’ [explist] ‘<b>)</b>’ | |
1638 args ::= tableconstructor | |
1639 args ::= LiteralString | |
1640 </pre> | |
1641 | |
1642 <p> | |
1643 All argument expressions are evaluated before the call. | |
1644 A call of the form <code>f{<em>fields</em>}</code> is | |
1645 syntactic sugar for <code>f({<em>fields</em>})</code>; | |
1646 that is, the argument list is a single new table. | |
1647 A call of the form <code>f'<em>string</em>'</code> | |
1648 (or <code>f"<em>string</em>"</code> or <code>f[[<em>string</em>]]</code>) | |
1649 is syntactic sugar for <code>f('<em>string</em>')</code>; | |
1650 that is, the argument list is a single literal string. | |
1651 </p> | |
1652 <% | |
1653 end | |
1654 } | |
1655 fn_def = { | |
1656 title = "Function Definitions" | |
1657 content = function() | |
1658 %> | |
1659 <p> | |
1660 The syntax for function definition is | |
1661 </p> | |
1662 | |
1663 <pre> | |
1664 functiondef ::= <b>function</b> funcbody | |
1665 funcbody ::= ‘<b>(</b>’ [parlist] ‘<b>)</b>’ block end_function | |
1666 end_function ::= <b>end_function</b> | <b>end</b> | |
1667 </pre> | |
1668 | |
1669 <p> | |
1670 The following syntactic sugar simplifies function definitions: | |
1671 </p> | |
1672 | |
1673 <pre> | |
1674 stat ::= <b>function</b> funcname funcbody | |
1675 stat ::= <b>local</b> <b>function</b> Name funcbody | |
1676 funcname ::= Name {‘<b>.</b>’ Name} [‘<b>:</b>’ Name] | |
1677 </pre> | |
1678 | |
1679 <p> | |
1680 The statement | |
1681 </p> | |
1682 | |
1683 <pre> | |
1684 function f () <em>body</em> end | |
1685 </pre> | |
1686 | |
1687 <p> | |
1688 translates to | |
1689 </p> | |
1690 | |
1691 <pre> | |
1692 f = function () <em>body</em> end | |
1693 </pre> | |
1694 | |
1695 <p> | |
1696 The statement | |
1697 <p> | |
1698 | |
1699 <pre> | |
1700 function t.a.b.c.f () <em>body</em> end | |
1701 </pre> | |
1702 | |
1703 <p> | |
1704 translates to | |
1705 </p> | |
1706 | |
1707 <pre> | |
1708 t.a.b.c.f = function () <em>body</em> end | |
1709 </pre> | |
1710 | |
1711 <p> | |
1712 The statement | |
1713 </p> | |
1714 | |
1715 <pre> | |
1716 local function f () <em>body</em> end | |
1717 </pre> | |
1718 | |
1719 <p> | |
1720 translates to | |
1721 </p> | |
1722 | |
1723 <pre> | |
1724 local f; f = function () <em>body</em> end | |
1725 </pre> | |
1726 | |
1727 <p> | |
1728 not to | |
1729 </p> | |
1730 | |
1731 <pre> | |
1732 local f = function () <em>body</em> end | |
1733 </pre> | |
1734 | |
1735 <p> | |
1736 (This only makes a difference when the body of the function | |
1737 contains references to <code>f</code>.) | |
1738 </p> | |
1739 | |
1740 <p> | |
1741 A function definition is an executable expression, | |
1742 whose value has type <em>function</em>. | |
1743 When Luan precompiles a chunk, | |
1744 all its function bodies are precompiled too. | |
1745 Then, whenever Luan executes the function definition, | |
1746 the function is <em>instantiated</em> (or <em>closed</em>). | |
1747 This function instance (or <em>closure</em>) | |
1748 is the final value of the expression. | |
1749 </p> | |
1750 | |
1751 <p> | |
1752 Parameters act as local variables that are | |
1753 initialized with the argument values: | |
1754 </p> | |
1755 | |
1756 <pre> | |
1757 parlist ::= namelist [‘<b>,</b>’ ‘<b>...</b>’] | ‘<b>...</b>’ | |
1758 </pre> | |
1759 | |
1760 <p> | |
1761 When a function is called, | |
1762 the list of arguments is adjusted to | |
1763 the length of the list of parameters if the list is too short, | |
1764 unless the function is a <em>vararg function</em>, | |
1765 which is indicated by three dots ('<code>...</code>') | |
1766 at the end of its parameter list. | |
1767 A vararg function does not adjust its argument list; | |
1768 instead, it collects all extra arguments and supplies them | |
1769 to the function through a <em>vararg expression</em>, | |
1770 which is also written as three dots. | |
1771 The value of this expression is a list of all actual extra arguments, | |
1772 similar to a function with multiple results. | |
1773 If a vararg expression is used inside another expression | |
1774 or in the middle of a list of expressions, | |
1775 then its return list is adjusted to one element. | |
1776 If the expression is used as the last element of a list of expressions, | |
1777 then no adjustment is made | |
1778 (unless that last expression is enclosed in parentheses). | |
1779 </p> | |
1780 | |
1781 <p> | |
1782 As an example, consider the following definitions: | |
1783 </p> | |
1784 <pre> | |
1785 function f(a, b) end | |
1786 function g(a, b, ...) end | |
1787 function r() return 1,2,3 end | |
1788 </pre> | |
1789 | |
1790 <p> | |
1791 Then, we have the following mapping from arguments to parameters and | |
1792 to the vararg expression: | |
1793 </p> | |
1794 <pre> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1795 CALL PARAMETERS |
1667 | 1796 |
1797 f(3) a=3, b=nil | |
1798 f(3, 4) a=3, b=4 | |
1799 f(3, 4, 5) runtime error | |
1800 f(r(), 10) runtime error | |
1801 f(r()) runtime error | |
1802 | |
1803 g(3) a=3, b=nil, ... --> (nothing) | |
1804 g(3, 4) a=3, b=4, ... --> (nothing) | |
1805 g(3, 4, 5, 8) a=3, b=4, ... --> 5 8 | |
1806 g(5, r()) a=5, b=1, ... --> 2 3 | |
1807 </pre> | |
1808 | |
1809 <p> | |
1810 Results are returned using the <b>return</b> statement (see <a href="#control">Control Structures</a>). | |
1811 If control reaches the end of a function | |
1812 without encountering a <b>return</b> statement, | |
1813 then the function returns with no results. | |
1814 </p> | |
1815 <% | |
1816 end | |
1817 } | |
1797 | 1818 backticks = { |
1819 title = "Backticks" | |
1820 content = function() | |
1821 %> | |
1822 <p> | |
1823 A block between backticks is run and then whatever was sent to standard output is returned as a string. Examples: | |
1824 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1825 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1826 local s = `%>1 + 1 = <%=1+1%><%` |
1797 | 1827 |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1828 local s = ` fn(whatever) ` |
1797 | 1829 |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1830 local s = `%> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1831 ... |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1832 <%` |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1833 </code> |
1797 | 1834 <p> |
1835 Backticks complement <a href="#template_stmt">template statements</a>. | |
1836 </p> | |
1837 <% | |
1838 end | |
1839 } | |
1667 | 1840 } |
1841 } | |
1842 visibility = { | |
1843 title = "Visibility Rules" | |
1844 content = function() | |
1845 %> | |
1846 <p> | |
1847 Luan is a lexically scoped language. | |
1848 The scope of a local variable begins at the first statement after | |
1849 its declaration and lasts until the last non-void statement | |
1850 of the innermost block that includes the declaration. | |
1851 Consider the following example: | |
1852 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1853 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1854 local x = 10 -- global to module |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1855 do -- new block |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1856 local x = x -- new 'x', with value 10 |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1857 print(x) --> 10 |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1858 x = x+1 |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1859 do -- another block |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1860 local x = x+1 -- another 'x' |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1861 print(x) --> 12 |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1862 end |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1863 print(x) --> 11 |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1864 end |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1865 print(x) --> 10 (the global one) |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1866 </code> |
1667 | 1867 |
1868 <p> | |
1869 Notice that, in a declaration like <code>local x = x</code>, | |
1870 the new <code>x</code> being declared is not in scope yet, | |
1871 and so the second <code>x</code> refers to the outside variable. | |
1872 </p> | |
1873 | |
1874 <p> | |
1875 Because of the lexical scoping rules, | |
1876 local variables can be freely accessed by functions | |
1877 defined inside their scope. | |
1878 A local variable used by an inner function is called | |
1879 an <em>upvalue</em>, or <em>external local variable</em>, | |
1880 inside the inner function. | |
1881 </p> | |
1882 | |
1883 <p> | |
1884 Notice that each execution of a <b>local</b> statement | |
1885 defines new local variables. | |
1886 Consider the following example: | |
1887 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1888 |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1889 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1890 local a = {} |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1891 local x = 20 |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1892 for i=1,10 do |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1893 local y = 0 |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1894 a[i] = function () y=y+1; return x+y end |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1895 end |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1896 </code> |
1667 | 1897 |
1898 <p> | |
1899 The loop creates ten closures | |
1900 (that is, ten instances of the anonymous function). | |
1901 Each of these closures uses a different <code>y</code> variable, | |
1902 while all of them share the same <code>x</code>. | |
1903 </p> | |
1904 <% | |
1905 end | |
1906 } | |
1907 } | |
1908 } | |
1909 libs = { | |
1910 title = "Standard Libraries" | |
1911 content = function() | |
1912 %> | |
1913 <p> | |
1914 The standard Luan libraries provide useful functions | |
1915 that are implemented both in Java and in Luan itself. | |
1916 How each function is implemented shouldn't matter to the user. | |
1917 Some of these functions provide essential services to the language | |
1918 (e.g., <a href="#Luan.type"><code>type</code></a> and <a href="#Luan.get_metatable"><code>get_metatable</code></a>); | |
1919 others provide access to "outside" services (e.g., I/O). | |
1920 </p> | |
1921 <% | |
1922 end | |
1923 subs = { | |
1924 default_lib = { | |
1925 title = "Default Environment" | |
1926 content = function() | |
1927 %> | |
1928 <p> | |
1929 This is provided by default as a local variable for any Luan code as described in <a href="#env">Environments</a>. | |
1930 </p> | |
1931 <% | |
1932 end | |
1933 subs = { | |
1934 require = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1935 title = "require (mod_uri)" |
1667 | 1936 content = function() |
1937 %> | |
1938 <p> | |
1939 Example use: | |
1940 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1941 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1942 local Table = require "luan:Table.luan" |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1943 </code> |
1667 | 1944 |
1945 <p> | |
1946 Could be defined as: | |
1947 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1948 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1949 local function require(mod_name) |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1950 return <a href="#Package.load">Package.load</a>(mod_name) or <a href="#Luan.error">Luan.error</a>("module '"..mod_name.."' not found") |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1951 end |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1952 </code> |
1667 | 1953 |
1954 <p> | |
1955 A special case is: | |
1956 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1957 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1958 require "java" |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1959 </code> |
1667 | 1960 |
1961 <p> | |
1962 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. | |
1963 </p> | |
1964 <% | |
1965 end | |
1966 } | |
1967 } | |
1968 } | |
1969 luan_lib = { | |
1970 title = "Basic Functions" | |
1971 content = function() | |
1972 %> | |
1973 <p> | |
1974 Include this library by: | |
1975 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1976 |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1977 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1978 local Luan = require "luan:Luan.luan" |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
1979 </code> |
1667 | 1980 |
1981 <p> | |
1982 The basic library provides basic functions to Luan that don't depend on other libaries. | |
1983 </p> | |
1984 <% | |
1985 end | |
1986 subs = { | |
1813 | 1987 ["Luan.arg"] = { |
1988 title = "Luan.arg" | |
1989 content = function() | |
1990 %> | |
1991 <p>If Luan was run from the command line then this is a list of the command line arguments. For example if one runs Luan like this:</p> | |
1992 | |
1993 <code block> | |
1994 luan t.luan a b c | |
1995 </code> | |
1996 | |
1997 <p>Then Luan.arg will contain:</p> | |
1998 | |
1999 <code block> | |
2000 { | |
2001 [0] = "t.luan" | |
2002 [1] = "a" | |
2003 [2] = "b" | |
2004 [3] = "c" | |
2005 } | |
2006 </code> | |
2007 | |
2008 <p>And of course <code>#Luan.arg</code> will be <code>3</code>.</p> | |
2009 <% | |
2010 end | |
2011 } | |
1667 | 2012 ["Luan.do_file"] = { |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2013 title = "Luan.do_file ([uri])" |
1667 | 2014 content = function() |
2015 %> | |
2016 <p> | |
2017 Could be defined as: | |
2018 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2019 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2020 function Luan.do_file(uri) |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2021 local fn = <a href="#Luan.load_file">Luan.load_file</a>(uri) or <a href="#Luan.error">Luan.error</a>("file '"..uri.."' not found") |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2022 return fn() |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2023 end |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2024 </code> |
1667 | 2025 <% |
2026 end | |
2027 } | |
2028 ["Luan.error"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2029 title = "Luan.error (message)" |
1667 | 2030 content = function() |
2031 %> | |
2032 <p> | |
2033 Throws an error containing the message. | |
2034 </p> | |
2035 | |
2036 <p> | |
2037 Could be defined as: | |
2038 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2039 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2040 function Luan.error(message) |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2041 <a href="#Luan.new_error">Luan.new_error</a>(message).throw() |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2042 end |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2043 </code> |
1667 | 2044 <% |
2045 end | |
2046 } | |
2047 ["Luan.eval"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2048 title = "Luan.eval (text [, source_name [, env]])" |
1667 | 2049 content = function() |
2050 %> | |
2051 <p> | |
2052 Evaluates <code>text</code> as a Luan expression. | |
2053 </p> | |
2054 | |
2055 <p> | |
2056 Could be defined as: | |
2057 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2058 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2059 function Luan.eval(text,source_name, env) |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2060 return <a href="#Luan.load">Luan.load</a>( "return "..text, source_name or "eval", env )() |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2061 end |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2062 </code> |
1667 | 2063 <% |
2064 end | |
2065 } | |
2066 ["Luan.get_metatable"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2067 title = "Luan.get_metatable (table)" |
1667 | 2068 content = function() |
2069 %> | |
2070 <p> | |
2071 If <code>table</code> does not have a metatable, returns <b>nil</b>. | |
2072 Otherwise, | |
2073 if the table's metatable has a <code>"__metatable"</code> field, | |
2074 returns the associated value. | |
2075 Otherwise, returns the metatable of the given table. | |
2076 </p> | |
2077 <% | |
2078 end | |
2079 } | |
2080 ["Luan.hash_code"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2081 title = "Luan.hash_code (v)" |
1667 | 2082 content = function() |
2083 %> | |
2084 <p> | |
2085 Returns the hash code of <code>v</code>. | |
2086 </p> | |
2087 <% | |
2088 end | |
2089 } | |
2090 ["Luan.ipairs"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2091 title = "Luan.ipairs (t)" |
1667 | 2092 content = function() |
2093 %> | |
2094 <p> | |
2095 Returns an iterator function | |
2096 so that the construction | |
2097 </p> | |
2098 <pre> | |
2099 for i,v in ipairs(t) do <em>body</em> end | |
2100 </pre> | |
2101 | |
2102 <p> | |
2103 will iterate over the key–value pairs | |
2104 (<code>1,t[1]</code>), (<code>2,t[2]</code>), ..., | |
2105 up to the first nil value. | |
2106 </p> | |
2107 | |
2108 <p> | |
2109 Could be defined as: | |
2110 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2111 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2112 function Luan.ipairs(t) |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2113 local i = 0 |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2114 return function() |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2115 if i < #t then |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2116 i = i + 1 |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2117 return i, t[i] |
1667 | 2118 end |
2119 end | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2120 end |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2121 </code> |
1667 | 2122 <% |
2123 end | |
2124 } | |
2125 ["Luan.load"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2126 title = "Luan.load (text, [source_name [, env [, persist]]])" |
1667 | 2127 content = function() |
2128 %> | |
2129 <p> | |
2130 Loads a chunk. | |
2131 </p> | |
2132 | |
2133 <p> | |
2134 The <code>text</code> is compiled. | |
2135 If there are no syntactic errors, | |
2136 returns the compiled chunk as a function; | |
2137 otherwise, throws an error. | |
2138 </p> | |
2139 | |
2140 <p> | |
2141 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". | |
2142 </p> | |
2143 | |
2144 <p> | |
2145 If the <code>env</code> parameter is supplied, it becomes the <code>_ENV</code> of the chunk. | |
2146 </p> | |
2147 | |
2148 <p> | |
2149 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>. | |
2150 </p> | |
2151 <% | |
2152 end | |
2153 } | |
2154 ["Luan.load_file"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2155 title = "Luan.load_file (file_uri)" |
1667 | 2156 content = function() |
2157 %> | |
2158 <p> | |
2159 Similar to <a href="#Luan.load"><code>load</code></a>, | |
2160 but gets the chunk from file <code>file_uri</code>. | |
2161 <code>file_uri</code> can be a string or a uri table. | |
2162 </p> | |
2163 <% | |
2164 end | |
2165 } | |
2166 ["Luan.new_error"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2167 title = "Luan.new_error (message)" |
1667 | 2168 content = function() |
2169 %> | |
2170 <p> | |
2171 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. | |
2172 </p> | |
2173 | |
2174 <p> | |
2175 To print the current stack trace, you could do: | |
2176 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2177 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2178 Io.print( Luan.new_error "stack" ) |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2179 </code> |
1667 | 2180 <% |
2181 end | |
2182 } | |
2183 ["Luan.pairs"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2184 title = "Luan.pairs (t)" |
1667 | 2185 content = function() |
2186 %> | |
2187 <p> | |
2188 If <code>t</code> has a metamethod <code>__pairs</code>, | |
2189 calls it with <code>t</code> as argument and returns the | |
2190 result from the call. | |
2191 </p> | |
2192 | |
2193 <p> | |
2194 Otherwise, | |
2195 returns a function | |
2196 so that the construction | |
2197 </p> | |
2198 <pre> | |
2199 for k,v in pairs(t) do <em>body</em> end | |
2200 </pre> | |
2201 | |
2202 <p> | |
2203 will iterate over all key–value pairs of table <code>t</code>. | |
2204 </p> | |
2205 <% | |
2206 end | |
2207 } | |
1813 | 2208 ["Luan.parse"] = { |
2209 title = "Luan.parse (s)" | |
2210 content = function() | |
2211 %> | |
2212 <p>This Luan's equivalent to Javascript's JSON.parse(), but for a Luan value. In addition to the usual JSON values, Luan.parse allows long strings and allows specifying numeric types of <i>double</i>, <i>float</i>, <i>integer</i>, and <i>long</i>. For example:</p> | |
2213 | |
2214 <code block> | |
2215 local t = Luan.parse[=[ | |
2216 { | |
2217 nothing = nil | |
2218 t = true | |
2219 f = false | |
2220 s = "string" | |
2221 ls = [[long string]] | |
2222 n = 3 | |
2223 d = double(3) | |
2224 f = float(3) | |
2225 i = integer(3) | |
2226 l = long(3) | |
2227 list = { 1, 2, 3 } | |
2228 table = { | |
2229 one = 1 | |
2230 two = 2 | |
2231 three = 3 | |
2232 } | |
2233 ["ugly-key"] = "something" | |
2234 } | |
2235 ]=] | |
2236 </code> | |
2237 <% | |
2238 end | |
2239 } | |
1667 | 2240 ["Luan.range"] = { |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2241 title = "Luan.range (start, stop [, step])" |
1667 | 2242 content = function() |
2243 %> | |
2244 <p> | |
2245 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. | |
2246 </p> | |
2247 | |
2248 <p> | |
2249 Example use: | |
2250 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2251 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2252 for i in range(1,10) do |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2253 Io.print("count up:",i) |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2254 end |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2255 for i in range(10,0,-1) do |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2256 Io.print("count down:",i) |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2257 end |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2258 </code> |
1667 | 2259 |
2260 <p> | |
2261 Could be defined as: | |
2262 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2263 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2264 function Luan.range(start, stop, step) |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2265 step = step or 1 |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2266 step == 0 and <a href="#Luan.error">Luan.error</a> "bad argument #3 (step may not be zero)" |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2267 local i = start |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2268 return function() |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2269 if step > 0 and i <= stop or step < 0 and i >= stop then |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2270 local rtn = i |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2271 i = i + step |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2272 return rtn |
1667 | 2273 end |
2274 end | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2275 end |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2276 </code> |
1667 | 2277 <% |
2278 end | |
2279 } | |
2280 ["Luan.raw_equal"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2281 title = "Luan.raw_equal (v1, v2)" |
1667 | 2282 content = function() |
2283 %> | |
2284 <p> | |
2285 Checks whether <code>v1</code> is equal to <code>v2</code>, | |
2286 without invoking any metamethod. | |
2287 Returns a boolean. | |
2288 </p> | |
2289 <% | |
2290 end | |
2291 } | |
2292 ["Luan.raw_get"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2293 title = "Luan.raw_get (table, index)" |
1667 | 2294 content = function() |
2295 %> | |
2296 <p> | |
2297 Gets the real value of <code>table[index]</code>, | |
2298 without invoking any metamethod. | |
2299 <code>table</code> must be a table; | |
2300 <code>index</code> may be any value. | |
2301 </p> | |
2302 <% | |
2303 end | |
2304 } | |
2305 ["Luan.raw_len"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2306 title = "Luan.raw_len (v)" |
1667 | 2307 content = function() |
2308 %> | |
2309 <p> | |
2310 Returns the length of the object <code>v</code>, | |
2311 which must be a table or a string, | |
2312 without invoking any metamethod. | |
2313 Returns an integer. | |
2314 </p> | |
2315 <% | |
2316 end | |
2317 } | |
2318 ["Luan.raw_set"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2319 title = "Luan.raw_set (table, index, value)" |
1667 | 2320 content = function() |
2321 %> | |
2322 <p> | |
2323 Sets the real value of <code>table[index]</code> to <code>value</code>, | |
2324 without invoking any metamethod. | |
2325 <code>table</code> must be a table, | |
2326 <code>index</code> any value different from <b>nil</b>, | |
2327 and <code>value</code> any Luan value. | |
2328 </p> | |
2329 <% | |
2330 end | |
2331 } | |
2332 ["Luan.set_metatable"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2333 title = "Luan.set_metatable (table, metatable)" |
1667 | 2334 content = function() |
2335 %> | |
2336 <p> | |
2337 Sets the metatable for the given table. | |
2338 If <code>metatable</code> is <b>nil</b>, | |
2339 removes the metatable of the given table. | |
2340 If the original metatable has a <code>"__metatable"</code> field, | |
2341 raises an error. | |
2342 </p> | |
2343 <% | |
2344 end | |
2345 } | |
2346 ["Luan.stringify"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2347 title = "Luan.stringify (v [,options])" |
1667 | 2348 content = function() |
2349 %> | |
2350 <p> | |
1813 | 2351 This Luan's equivalent to Javascript's JSON.stringify(), but for a Luan value. |
2352 <code>v</code> is a value of any type which is converted to a string that is a Luan expression. <code>options</code> may be a table or a function. If <code>options</code> is a table, it may contain the following flags whose <code>true</code> value means: | |
2353 </p> | |
2354 | |
2355 <ul> | |
2356 <li><b>strict</b> - invalid types throw an error</li> | |
2357 <li><b>number_types</b> - numbers will be wrapped in functions for their type</li> | |
2358 <li><b>compressed</b> - eliminates white space</li> | |
2359 <li><b>inline</b> - on one line</li> | |
2360 <li><b>no_name_keys</b> - forces all keys to be of the form <code>["key"]</code></li> | |
2361 </ul> | |
2362 | |
2363 <p>If <code>options</code> is a function then this function should take an argument <code>stack</code> and return an <code>options</code> table. The <code>stack</code> will be a list of keys indicating where stringify is currently processing. This allows different options to be applied at different places in a data structure.</p> | |
1667 | 2364 <% |
2365 end | |
2366 } | |
2367 ["Luan.to_string"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2368 title = "Luan.to_string (v)" |
1667 | 2369 content = function() |
2370 %> | |
2371 <p> | |
2372 Receives a value of any type and | |
2373 converts it to a string in a human-readable format. | |
2374 </p> | |
2375 | |
2376 <p> | |
2377 If the metatable of <code>v</code> has a <code>"__to_string"</code> field, | |
2378 then <code>to_string</code> calls the corresponding value | |
2379 with <code>v</code> as argument, | |
2380 and uses the result of the call as its result. | |
2381 </p> | |
2382 <% | |
2383 end | |
2384 } | |
2385 ["Luan.type"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2386 title = "Luan.type (v)" |
1667 | 2387 content = function() |
2388 %> | |
2389 <p> | |
2390 Returns the type of its only argument, coded as a string. | |
2391 The possible results of this function are | |
2392 "<code>nil</code>" (a string, not the value <b>nil</b>), | |
2393 "<code>number</code>", | |
2394 "<code>string</code>", | |
2395 "<code>binary</code>", | |
2396 "<code>boolean</code>", | |
2397 "<code>table</code>", | |
2398 "<code>function</code>", | |
2399 and "<code>java</code>". | |
2400 </p> | |
2401 <% | |
2402 end | |
2403 } | |
2404 ["Luan.values"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2405 title = "Luan.values (···)" |
1667 | 2406 content = function() |
2407 %> | |
2408 <p> | |
2409 Returns a function so that the construction | |
2410 </p> | |
2411 <pre> | |
2412 for i, v in Luan.values(···) do <em>body</em> end | |
2413 </pre> | |
2414 | |
2415 <p> | |
2416 will iterate over all values of <code>···</code>. | |
2417 </p> | |
2418 <% | |
2419 end | |
2420 } | |
2421 ["Luan.VERSION"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2422 title = "Luan.VERSION" |
1667 | 2423 content = function() |
2424 %> | |
2425 <p> | |
2426 A global variable (not a function) that | |
2427 holds a string containing the current Luan version. | |
2428 </p> | |
2429 <% | |
2430 end | |
2431 } | |
2432 } | |
2433 } | |
2434 package_lib = { | |
2435 title = "Modules" | |
2436 content = function() | |
2437 %> | |
2438 <p> | |
2439 Include this library by: | |
2440 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2441 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2442 local Package = require "luan:Package.luan" |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2443 </code> |
1667 | 2444 |
2445 <p> | |
2446 The package library provides basic | |
2447 facilities for loading modules in Luan. | |
2448 </p> | |
2449 <% | |
2450 end | |
2451 subs = { | |
2452 ["Package.load"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2453 title = "Package.load (mod_uri)" |
1667 | 2454 content = function() |
2455 %> | |
2456 <p> | |
2457 Loads the given module. | |
2458 The function starts by looking into the <a href="#Package.loaded"><code>Package.loaded</code></a> table | |
2459 to determine whether <code>mod_uri</code> is already loaded. | |
2460 If it is, then <code>Package.load</code> returns the value stored | |
2461 at <code>Package.loaded[mod_uri]</code>. | |
2462 Otherwise, it tries to load a new value for the module. | |
2463 </p> | |
2464 | |
2465 <p> | |
2466 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. | |
2467 </p> | |
2468 | |
2469 <p> | |
2470 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. | |
2471 </p> | |
2472 | |
2473 <p> | |
2474 If a new value for the module successful loaded, then it is stored in <code>Package.loaded[mod_uri]</code>. The value is returned. | |
2475 </p> | |
2476 <% | |
2477 end | |
2478 } | |
2479 ["Package.loaded"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2480 title = "Package.loaded" |
1667 | 2481 content = function() |
2482 %> | |
2483 <p> | |
2484 A table used by <a href="#Package.load"><code>Package.load</code></a> to control which | |
2485 modules are already loaded. | |
2486 When you load a module <code>mod_uri</code> and | |
2487 <code>Package.loaded[mod_uri]</code> is not <b>nil</b>, | |
2488 <a href="#Package.load"><code>Package.load</code></a> simply returns the value stored there. | |
2489 </p> | |
2490 | |
2491 <p> | |
2492 This variable is only a reference to the real table; | |
2493 assignments to this variable do not change the | |
2494 table used by <a href="#Package.load"><code>Package.load</code></a>. | |
2495 </p> | |
2496 <% | |
2497 end | |
2498 } | |
1656 | 2499 } |
2500 } | |
1668 | 2501 string_lib = { |
2502 title = "String Manipulation" | |
2503 content = function() | |
2504 %> | |
2505 <p> | |
2506 Include this library by: | |
2507 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2508 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2509 local String = require "luan:String.luan" |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2510 </code> |
1668 | 2511 |
2512 <p> | |
2513 This library provides generic functions for string manipulation, | |
2514 such as finding and extracting substrings, and pattern matching. | |
2515 When indexing a string in Luan, the first character is at position 1 | |
2516 (not at 0, as in Java). | |
2517 Indices are allowed to be negative and are interpreted as indexing backwards, | |
2518 from the end of the string. | |
2519 Thus, the last character is at position -1, and so on. | |
2520 </p> | |
2521 <% | |
2522 end | |
2523 subs = { | |
2524 ["String.char"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2525 title = "String.char (···)" |
1668 | 2526 content = function() |
2527 %> | |
2528 <p> | |
2529 Receives zero or more integers. | |
2530 Returns a string with length equal to the number of arguments, | |
2531 in which each character has the internal numerical code equal | |
2532 to its corresponding argument. | |
2533 </p> | |
2534 <% | |
2535 end | |
2536 } | |
1716 | 2537 ["String.contains"] = { |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2538 title = "String.contains (s, s2)" |
1716 | 2539 content = function() |
2540 %> | |
2541 <p> | |
2542 Returns a boolean indicating whether the <code>s</code> contains <code>s2</code>. | |
2543 </p> | |
2544 <% | |
2545 end | |
2546 } | |
1816 | 2547 ["String.digest_message"] = { |
2548 title = "String.digest_message (algorithm, s)" | |
2549 content = function() | |
2550 %> | |
2551 <p>Returns a hex digest string of <code>s</code>. Could be defined as:</p> | |
2552 | |
2553 <code block> | |
2554 function String.digest_message(algorithm,s) | |
1827 | 2555 return <a href="/manual.html#Binary.to_hex">Binary.to_hex</a>( <a href="/manual.html#Binary.digest_message">Binary.digest_message</a>( algorithm, <a href="/manual.html#String.to_binary">String.to_binary(s)</a> ) ) |
1816 | 2556 end |
2557 </code> | |
2558 <% | |
2559 end | |
2560 } | |
1668 | 2561 ["String.encode"] = { |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2562 title = "String.encode (s)" |
1668 | 2563 content = function() |
2564 %> | |
2565 <p> | |
2566 Encodes argument <code>s</code> into a string that can be placed in quotes so as to return the original value of the string. | |
2567 </p> | |
2568 <% | |
2569 end | |
2570 } | |
1716 | 2571 ["String.ends_with"] = { |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2572 title = "String.ends_with (s, s2)" |
1716 | 2573 content = function() |
2574 %> | |
2575 <p> | |
2576 Returns a boolean indicating whether the <code>s</code> ends with <code>s2</code>. | |
2577 </p> | |
2578 <% | |
2579 end | |
2580 } | |
1668 | 2581 ["String.find"] = { |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2582 title = "String.find (s, s2 [, init])" |
1668 | 2583 content = function() |
2584 %> | |
2585 <p> | |
1721
5c69d2e8bd75
no regex in String.find and String.split
Franklin Schmidt <fschmidt@gmail.com>
parents:
1719
diff
changeset
|
2586 Looks for the first substring |
5c69d2e8bd75
no regex in String.find and String.split
Franklin Schmidt <fschmidt@gmail.com>
parents:
1719
diff
changeset
|
2587 <code>s2</code> in the string <code>s</code>. |
1668 | 2588 If it finds a match, then <code>find</code> returns the indices of <code>s</code> |
2589 where this occurrence starts and ends; | |
2590 otherwise, it returns <b>nil</b>. | |
2591 A third, optional numerical argument <code>init</code> specifies | |
2592 where to start the search; | |
2593 its default value is 1 and can be negative. | |
2594 </p> | |
2595 | |
2596 <p> | |
2597 If the pattern has captures, | |
2598 then in a successful match | |
2599 the captured values are also returned, | |
2600 after the two indices. | |
2601 </p> | |
2602 <% | |
2603 end | |
2604 } | |
2605 ["String.format"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2606 title = "String.format (formatstring, ···)" |
1668 | 2607 content = function() |
2608 %> | |
2609 <p> | |
2610 Returns a formatted version of its variable number of arguments | |
2611 following the description given in its first argument (which must be a string). | |
1816 | 2612 The format string follows the same rules as the Java function <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#format-java.lang.String-java.lang.Object...-"><code>String.format</code></a> because Luan calls this internally. |
1668 | 2613 </p> |
2614 | |
2615 <p> | |
2616 Note that Java's <code>String.format</code> is too stupid to convert between ints and floats, so you must provide the right kind of number. | |
2617 </p> | |
2618 <% | |
2619 end | |
2620 } | |
2621 ["String.lower"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2622 title = "String.lower (s)" |
1668 | 2623 content = function() |
2624 %> | |
2625 <p> | |
2626 Receives a string and returns a copy of this string with all | |
2627 uppercase letters changed to lowercase. | |
2628 All other characters are left unchanged. | |
2629 </p> | |
2630 <% | |
2631 end | |
2632 } | |
1716 | 2633 ["String.regex"] = { |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2634 title = "String.regex (s)" |
1716 | 2635 content = function() |
2636 %> | |
2637 <p> | |
2638 Returns a <a href="#regex_table">regex</a> table for the pattern <code>s</code>. | |
2639 </p> | |
2640 <% | |
2641 end | |
2642 } | |
1668 | 2643 ["String.regex_quote"] = { |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2644 title = "String.regex_quote (s)" |
1668 | 2645 content = function() |
2646 %> | |
2647 <p> | |
1816 | 2648 Returns a string which matches the literal string <code>s</code> in a regular expression. This function is simply the Java method <a href="https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#quote-java.lang.String-"><code>Pattern.quote</code></a>. |
1668 | 2649 </p> |
2650 <% | |
2651 end | |
2652 } | |
1718
5603ee8e2a71
rename String.rep to String.repeated
Franklin Schmidt <fschmidt@gmail.com>
parents:
1717
diff
changeset
|
2653 ["String.repeated"] = { |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2654 title = "String.repeated (s, n [, sep])" |
1668 | 2655 content = function() |
2656 %> | |
2657 <p> | |
2658 Returns a string that is the concatenation of <code>n</code> copies of | |
2659 the string <code>s</code> separated by the string <code>sep</code>. | |
2660 The default value for <code>sep</code> is the empty string | |
2661 (that is, no separator). | |
2662 Returns the empty string if <code>n</code> is not positive. | |
2663 </p> | |
2664 <% | |
2665 end | |
2666 } | |
1717 | 2667 ["String.replace"] = { |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2668 title = "String.replace (s, target, replacement)" |
1717 | 2669 content = function() |
2670 %> | |
2671 <p> | |
2672 Returns a string where each substring <code>target</code> in <code>s</code> is replaced by <code>replacement</code>. | |
2673 </p> | |
2674 <% | |
2675 end | |
2676 } | |
1668 | 2677 ["String.reverse"] = { |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2678 title = "String.reverse (s)" |
1668 | 2679 content = function() |
2680 %> | |
2681 <p> | |
2682 Returns a string that is the string <code>s</code> reversed. | |
2683 </p> | |
2684 <% | |
2685 end | |
2686 } | |
2687 ["String.split"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2688 title = "String.split (s, s2 [, limit])" |
1668 | 2689 content = function() |
2690 %> | |
2691 <p> | |
1721
5c69d2e8bd75
no regex in String.find and String.split
Franklin Schmidt <fschmidt@gmail.com>
parents:
1719
diff
changeset
|
2692 Splits <code>s</code> using substring <code>s2</code> and returns the results. If <code>limit</code> is positive, then only returns at most that many results. If <code>limit</code> is zero, then remove trailing empty results. |
1668 | 2693 </p> |
2694 <% | |
2695 end | |
2696 } | |
1716 | 2697 ["String.starts_with"] = { |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2698 title = "String.starts_with (s, s2)" |
1716 | 2699 content = function() |
2700 %> | |
2701 <p> | |
2702 Returns a boolean indicating whether the <code>s</code> starts with <code>s2</code>. | |
2703 </p> | |
2704 <% | |
2705 end | |
2706 } | |
1668 | 2707 ["String.sub"] = { |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2708 title = "String.sub (s, i [, j])" |
1668 | 2709 content = function() |
2710 %> | |
2711 <p> | |
2712 Returns the substring of <code>s</code> that | |
2713 starts at <code>i</code> and continues until <code>j</code>; | |
2714 <code>i</code> and <code>j</code> can be negative. | |
2715 If <code>j</code> is absent, then it is assumed to be equal to -1 | |
2716 (which is the same as the string length). | |
2717 In particular, | |
2718 the call <code>string.sub(s,1,j)</code> returns a prefix of <code>s</code> | |
2719 with length <code>j</code>, | |
2720 and <code>string.sub(s, -i)</code> returns a suffix of <code>s</code> | |
2721 with length <code>i</code>. | |
2722 </p> | |
2723 | |
2724 <p> | |
2725 If, after the translation of negative indices, | |
2726 <code>i</code> is less than 1, | |
2727 it is corrected to 1. | |
2728 If <code>j</code> is greater than the string length, | |
2729 it is corrected to that length. | |
2730 If, after these corrections, | |
2731 <code>i</code> is greater than <code>j</code>, | |
2732 the function returns the empty string. | |
2733 </p> | |
2734 <% | |
2735 end | |
2736 } | |
2737 ["String.to_binary"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2738 title = "String.to_binary (s)" |
1668 | 2739 content = function() |
2740 %> | |
2741 <p> | |
1816 | 2742 Converts a string to a binary by calling the Java method <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#getBytes--"><code>String.getBytes</code></a>. |
1668 | 2743 </p> |
2744 <% | |
2745 end | |
2746 } | |
2747 ["String.to_number"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2748 title = "String.to_number (s [, base])" |
1668 | 2749 content = function() |
2750 %> | |
2751 <p> | |
2752 When called with no <code>base</code>, | |
2753 <code>to_number</code> tries to convert its argument to a number. | |
2754 If the argument is | |
2755 a string convertible to a number, | |
2756 then <code>to_number</code> returns this number; | |
2757 otherwise, it returns <b>nil</b>. | |
2758 The conversion of strings can result in integers or floats. | |
2759 </p> | |
2760 | |
2761 <p> | |
2762 When called with <code>base</code>, | |
2763 then <code>s</code> must be a string to be interpreted as | |
2764 an integer numeral in that base. | |
2765 In bases above 10, the letter '<code>A</code>' (in either upper or lower case) | |
2766 represents 10, '<code>B</code>' represents 11, and so forth, | |
2767 with '<code>Z</code>' representing 35. | |
2768 If the string <code>s</code> is not a valid numeral in the given base, | |
2769 the function returns <b>nil</b>. | |
2770 </p> | |
2771 <% | |
2772 end | |
2773 } | |
2774 ["String.trim"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2775 title = "String.trim (s)" |
1668 | 2776 content = function() |
2777 %> | |
2778 <p> | |
1816 | 2779 Removes the leading and trailing whitespace by calling the Java method <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#trim--"><code>String.trim</code></a>. |
1668 | 2780 </p> |
2781 <% | |
2782 end | |
2783 } | |
2784 ["String.unicode"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2785 title = "String.unicode (s [, i [, j]])" |
1668 | 2786 content = function() |
2787 %> | |
2788 <p> | |
2789 Returns the internal numerical codes of the characters <code>s[i]</code>, | |
2790 <code>s[i+1]</code>, ..., <code>s[j]</code>. | |
2791 The default value for <code>i</code> is 1; | |
2792 the default value for <code>j</code> is <code>i</code>. | |
2793 These indices are corrected | |
2794 following the same rules of function <a href="#String.sub"><code>String.sub</code></a>. | |
2795 </p> | |
2796 <% | |
2797 end | |
2798 } | |
2799 ["String.upper"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2800 title = "String.upper (s)" |
1668 | 2801 content = function() |
2802 %> | |
2803 <p> | |
2804 Receives a string and returns a copy of this string with all | |
2805 lowercase letters changed to uppercase. | |
2806 All other characters are left unchanged. | |
2807 The definition of what a lowercase letter is depends on the current locale. | |
2808 </p> | |
2809 <% | |
2810 end | |
2811 } | |
2812 } | |
2813 } | |
1716 | 2814 regex_table = { |
2815 title = "Regular Expressions" | |
2816 content = function() | |
2817 %> | |
2818 <p> | |
2819 Regular expressions are handled using a regex table generated by <a href="#String.regex">String.regex</a>. | |
2820 </p> | |
2821 | |
2822 <p> | |
1816 | 2823 Pattern matching is based on the Java <a href="https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html">Pattern</a> class. |
1716 | 2824 </p> |
2825 <% | |
2826 end | |
2827 subs = { | |
2828 ["regex.find"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2829 title = "regex.find (s [, init])" |
1716 | 2830 content = function() |
2831 %> | |
2832 <p> | |
2833 Looks for the first match of | |
2834 the regex in the string <code>s</code>. | |
2835 If it finds a match, then <code>find</code> returns the indices of <code>s</code> | |
2836 where this occurrence starts and ends; | |
2837 otherwise, it returns <b>nil</b>. | |
2838 A third, optional numerical argument <code>init</code> specifies | |
2839 where to start the search; | |
2840 its default value is 1 and can be negative. | |
2841 </p> | |
2842 | |
2843 <p> | |
2844 If the regex has captures, | |
2845 then in a successful match | |
2846 the captured values are also returned, | |
2847 after the two indices. | |
2848 </p> | |
2849 <% | |
2850 end | |
2851 } | |
2852 ["regex.gmatch"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2853 title = "regex.gmatch (s)" |
1716 | 2854 content = function() |
2855 %> | |
2856 <p> | |
2857 Returns an iterator function that, | |
2858 each time it is called, | |
2859 returns the next captures from the regex | |
2860 over the string <code>s</code>. | |
2861 If the regex specifies no captures, | |
2862 then the whole match is produced in each call. | |
2863 </p> | |
2864 | |
2865 <p> | |
2866 As an example, the following loop | |
2867 will iterate over all the words from string <code>s</code>, | |
2868 printing one per line: | |
2869 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2870 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2871 local r = String.regex[[\w+]] |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2872 local s = "hello world from Lua" |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2873 for w in r.gmatch(s) do |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2874 print(w) |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2875 end |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2876 </code> |
1716 | 2877 |
2878 <p> | |
2879 The next example collects all pairs <code>key=value</code> from the | |
2880 given string into a table: | |
2881 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2882 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2883 local t = {} |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2884 local r = String.regex[[(\w+)=(\w+)]] |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2885 local s = "from=world, to=Lua" |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2886 for k, v in r.gmatch(s) do |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2887 t[k] = v |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2888 end |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2889 </code> |
1716 | 2890 |
2891 <p> | |
2892 For this function, a caret '<code>^</code>' at the start of a pattern does not | |
2893 work as an anchor, as this would prevent the iteration. | |
2894 </p> | |
2895 <% | |
2896 end | |
2897 } | |
2898 ["regex.gsub"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2899 title = "regex.gsub (s, repl [, n])" |
1716 | 2900 content = function() |
2901 %> | |
2902 <p> | |
2903 Returns a copy of <code>s</code> | |
2904 in which all (or the first <code>n</code>, if given) | |
2905 occurrences of the regex have been | |
2906 replaced by a replacement string specified by <code>repl</code>, | |
2907 which can be a string, a table, or a function. | |
2908 <code>gsub</code> also returns, as its second value, | |
2909 the total number of matches that occurred. | |
2910 The name <code>gsub</code> comes from <em>Global SUBstitution</em>. | |
2911 </p> | |
2912 | |
2913 <p> | |
2914 If <code>repl</code> is a string, then its value is used for replacement. | |
2915 The character <code>\</code> works as an escape character. | |
2916 Any sequence in <code>repl</code> of the form <code>$<em>d</em></code>, | |
2917 with <em>d</em> between 1 and 9, | |
2918 stands for the value of the <em>d</em>-th captured substring. | |
2919 The sequence <code>$0</code> stands for the whole match. | |
2920 </p> | |
2921 | |
2922 <p> | |
2923 If <code>repl</code> is a table, then the table is queried for every match, | |
2924 using the first capture as the key. | |
2925 </p> | |
2926 | |
2927 <p> | |
2928 If <code>repl</code> is a function, then this function is called every time a | |
2929 match occurs, with all captured substrings passed as arguments, | |
2930 in order. | |
2931 </p> | |
2932 | |
2933 <p> | |
2934 In any case, | |
2935 if the regex specifies no captures, | |
2936 then it behaves as if the whole regex was inside a capture. | |
2937 </p> | |
2938 | |
2939 <p> | |
2940 If the value returned by the table query or by the function call | |
2941 is not <b>nil</b>, | |
2942 then it is used as the replacement string; | |
2943 otherwise, if it is <b>nil</b>, | |
2944 then there is no replacement | |
2945 (that is, the original match is kept in the string). | |
2946 </p> | |
2947 | |
2948 <p> | |
2949 Here are some examples: | |
2950 </p> | |
2951 | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2952 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2953 local r = String.regex[[(\w+)]] |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2954 local x = r.gsub("hello world", "$1 $1") |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2955 --> x="hello hello world world" |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2956 |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2957 local r = String.regex[[(\w+)]] |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2958 local x = r.gsub("hello world", "$0 $0", 1) |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2959 --> x="hello hello world" |
1716 | 2960 |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2961 local r = String.regex[[(\w+)\s*(\w+)]] |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2962 local x = r.gsub("hello world from Luan", "$2 $1") |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2963 --> x="world hello Luan from" |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2964 |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2965 local r = String.regex[[\$(.*?)\$]] |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2966 local x = r.gsub("4+5 = $return 4+5$", function(s) |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2967 return load(s)() |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2968 end) |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2969 --> x="4+5 = 9" |
1716 | 2970 |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2971 local r = String.regex[[\$(\w+)]] |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2972 local t = {name="lua", version="5.3"} |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2973 local x = r.gsub("$name-$version.tar.gz", t) |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2974 --> x="lua-5.3.tar.gz" |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2975 </code> |
1716 | 2976 <% |
2977 end | |
2978 } | |
2979 ["regex.match"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2980 title = "regex.match (s [, init])" |
1716 | 2981 content = function() |
2982 %> | |
2983 <p> | |
2984 Looks for the first <em>match</em> of | |
2985 the regex in the string <code>s</code>. | |
2986 If it finds one, then <code>match</code> returns | |
2987 the captures from the regex; | |
2988 otherwise it returns <b>nil</b>. | |
2989 If the regex specifies no captures, | |
2990 then the whole match is returned. | |
2991 A third, optional numerical argument <code>init</code> specifies | |
2992 where to start the search; | |
2993 its default value is 1 and can be negative. | |
2994 </p> | |
2995 <% | |
2996 end | |
2997 } | |
2998 ["regex.matches"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
2999 title = "regex.matches (s)" |
1716 | 3000 content = function() |
3001 %> | |
3002 <p> | |
3003 Returns a boolean indicating whether the regex can be found in string <code>s</code>. | |
3004 This function is equivalent to | |
3005 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3006 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3007 return regex.match(s) ~= nil |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3008 </code> |
1716 | 3009 <% |
3010 end | |
3011 } | |
1719 | 3012 ["regex.set"] = { |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3013 title = "regex.set (pattern)" |
1719 | 3014 content = function() |
3015 %> | |
3016 <p> | |
3017 Changes the regex pattern to <code>pattern</code>. | |
3018 </p> | |
3019 <% | |
3020 end | |
3021 } | |
3022 ["regex.split"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3023 title = "regex.split (s [, limit])" |
1719 | 3024 content = function() |
3025 %> | |
3026 <p> | |
3027 Splits <code>s</code> using the regex and returns the results. If <code>limit</code> is positive, then only returns at most that many results. If <code>limit</code> is zero, then remove trailing empty results. | |
3028 </p> | |
3029 <% | |
3030 end | |
3031 } | |
1716 | 3032 } |
3033 } | |
1668 | 3034 binary_lib = { |
3035 title = "Binary Manipulation" | |
3036 content = function() | |
3037 %> | |
3038 <p> | |
3039 Include this library by: | |
3040 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3041 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3042 local Binary = require "luan:Binary.luan" |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3043 </code> |
1668 | 3044 <% |
3045 end | |
3046 subs = { | |
1816 | 3047 ["Binary.base64_decode"] = { |
3048 title = "Binary.base64_decode (s)" | |
3049 content = function() | |
3050 %> | |
3051 <p>Same as Java's <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Base64.Decoder.html#decode-java.lang.String-">Base64.Decoder.decode</a>.</p> | |
3052 <% | |
3053 end | |
3054 } | |
3055 ["Binary.base64_encode"] = { | |
3056 title = "Binary.base64_encode (b)" | |
3057 content = function() | |
3058 %> | |
3059 <p>Same as Java's <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Base64.Encoder.html#encodeToString-byte:A-">Base64.Encoder.encodeToString</a>.</p> | |
3060 <% | |
3061 end | |
3062 } | |
1668 | 3063 ["Binary.binary"] = { |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3064 title = "Binary.binary (···)" |
1668 | 3065 content = function() |
3066 %> | |
3067 <p> | |
3068 Receives zero or more bytes (as integers). | |
3069 Returns a binary with length equal to the number of arguments, | |
3070 in which each byte has the internal numerical code equal | |
3071 to its corresponding argument. | |
3072 </p> | |
3073 <% | |
3074 end | |
3075 } | |
3076 ["Binary.byte"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3077 title = "Binary.byte (b [, i [, j]])" |
1668 | 3078 content = function() |
3079 %> | |
3080 <p> | |
3081 Returns the internal numerical codes of the bytes <code>b[i]</code>, | |
3082 <code>b[i+1]</code>, ..., <code>b[j]</code>. | |
3083 The default value for <code>i</code> is 1; | |
3084 the default value for <code>j</code> is <code>i</code>. | |
3085 These indices are corrected | |
3086 following the same rules of function <a href="#String.sub"><code>String.sub</code></a>. | |
3087 </p> | |
3088 <% | |
3089 end | |
3090 } | |
1816 | 3091 ["Binary.digest_message"] = { |
3092 title = "Binary.digest_message (algorithm, b)" | |
3093 content = function() | |
3094 %> | |
3095 <p>Implemented in Java as:</p> | |
3096 | |
3097 <code block> | |
3098 return <a href="https://docs.oracle.com/javase/8/docs/api/java/security/MessageDigest.html">MessageDigest</a>.<a href="https://docs.oracle.com/javase/8/docs/api/java/security/MessageDigest.html#getInstance-java.lang.String-">getInstance</a>(algorithm).<a href="https://docs.oracle.com/javase/8/docs/api/java/security/MessageDigest.html#digest-byte:A-">digest</a>(b); | |
3099 </code> | |
3100 <% | |
3101 end | |
3102 } | |
3103 ["Binary.to_hex"] = { | |
3104 title = "Binary.to_hex (b)" | |
3105 content = function() | |
3106 %> | |
3107 <p>Converts a binary to a hex string.</p> | |
3108 <% | |
3109 end | |
3110 } | |
1668 | 3111 ["Binary.to_string"] = { |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3112 title = "Binary.to_string (b [,charset])" |
1668 | 3113 content = function() |
3114 %> | |
3115 <p> | |
1816 | 3116 If <code>charset</code> is not nil then converts the binary <code>b</code> to a string using the Java <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#String-byte:A-java.lang.String-">String constructor</a>, else makes each byte a char. |
1668 | 3117 </p> |
3118 <% | |
3119 end | |
3120 } | |
3121 } | |
3122 } | |
3123 table_lib = { | |
3124 title = "Table Manipulation" | |
3125 content = function() | |
3126 %> | |
3127 <p> | |
3128 Include this library by: | |
3129 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3130 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3131 local Table = require "luan:Table.luan" |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3132 </code> |
1668 | 3133 |
3134 <p> | |
3135 This library provides generic functions for table manipulation. | |
3136 It provides all its functions inside the table <code>Table</code>. | |
3137 </p> | |
3138 <% | |
3139 end | |
3140 subs = { | |
1817 | 3141 ["Table.case_insensitive"] = { |
3142 title = "Table.case_insensitive ([tbl])" | |
3143 content = function() | |
3144 %> | |
3145 <p>Returns a table with case-insensitive string keys. Copies <code>tbl</code> or is empty.</p> | |
3146 <% | |
3147 end | |
3148 } | |
1668 | 3149 ["Table.clear"] = { |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3150 title = "Table.clear (tbl)" |
1668 | 3151 content = function() |
3152 %> | |
3153 <p> | |
3154 Clears the table. | |
3155 </p> | |
3156 <% | |
3157 end | |
3158 } | |
3159 ["Table.concat"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3160 title = "Table.concat (list [, sep [, i [, j]]])" |
1668 | 3161 content = function() |
3162 %> | |
3163 <p> | |
3164 Given a list, | |
3165 returns the string <code>list[i]..sep..list[i+1] ··· sep..list[j]</code>. | |
3166 The default value for <code>sep</code> is the empty string, | |
3167 the default for <code>i</code> is 1, | |
3168 and the default for <code>j</code> is <code>#list</code>. | |
3169 If <code>i</code> is greater than <code>j</code>, returns the empty string. | |
3170 </p> | |
3171 <% | |
3172 end | |
3173 } | |
3174 ["Table.copy"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3175 title = "Table.copy (tbl [, i [, j]])" |
1668 | 3176 content = function() |
3177 %> | |
3178 <p> | |
3179 If <code>i</code> is <code>nil</code>, returns a shallow copy of <code>tbl</code>. | |
3180 Otherwise returns a new table which is a list of the elements <code>tbl[i] ··· tbl[j]</code>. | |
3181 By default, <code>j</code> is <code>#tbl</code>. | |
3182 </p> | |
3183 <% | |
3184 end | |
3185 } | |
3186 ["Table.insert"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3187 title = "Table.insert (list, pos, value)" |
1668 | 3188 content = function() |
3189 %> | |
3190 <p> | |
3191 Inserts element <code>value</code> at position <code>pos</code> in <code>list</code>, | |
3192 shifting up the elements | |
3193 <code>list[pos], list[pos+1], ···, list[#list]</code>. | |
3194 </p> | |
3195 <% | |
3196 end | |
3197 } | |
3198 ["Table.is_empty"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3199 title = "Table.is_empty (tbl)" |
1668 | 3200 content = function() |
3201 %> | |
3202 <% | |
3203 end | |
3204 } | |
1704 | 3205 ["Table.is_list"] = { |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3206 title = "Table.is_list (tbl)" |
1704 | 3207 content = function() |
3208 %> | |
3209 <% | |
3210 end | |
3211 } | |
1817 | 3212 ["Table.java_to_table_deep"] = { |
3213 title = "Table.java_to_table_deep (obj [, java_to_table_shallow])" | |
3214 content = function() | |
3215 %> | |
3216 <p>Recursively applies <code>java_to_table_shallow</code> to convert a Java object to nested tables. <code>java_to_table_shallow</code> defaults to <a href="#Table.java_to_table_shallow">Table.java_to_table_shallow</a>.</p> | |
3217 <% | |
3218 end | |
3219 } | |
3220 ["Table.java_to_table_shallow"] = { | |
3221 title = "Table.java_to_table_shallow (obj)" | |
3222 content = function() | |
3223 %> | |
3224 <p>Converts a Java object to a table. Works for collection types List, Map, Set, and Java arrays.</p> | |
3225 <% | |
3226 end | |
3227 } | |
1668 | 3228 ["Table.pack"] = { |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3229 title = "Table.pack (···)" |
1668 | 3230 content = function() |
3231 %> | |
3232 <p> | |
3233 Returns a new table with all parameters stored into keys 1, 2, etc. | |
3234 and with a field "<code>n</code>" with the total number of parameters. | |
3235 Note that the resulting table may not be a sequence. | |
3236 </p> | |
3237 <% | |
3238 end | |
3239 } | |
3240 ["Table.remove"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3241 title = "Table.remove (list, pos)" |
1668 | 3242 content = function() |
3243 %> | |
3244 <p> | |
3245 Removes from <code>list</code> the element at position <code>pos</code>, | |
3246 returning the value of the removed element. | |
3247 When <code>pos</code> is an integer between 1 and <code>#list</code>, | |
3248 it shifts down the elements | |
3249 <code>list[pos+1], list[pos+2], ···, list[#list]</code> | |
3250 and erases element <code>list[#list]</code>; | |
3251 The index <code>pos</code> can also be 0 when <code>#list</code> is 0, | |
3252 or <code>#list + 1</code>; | |
3253 in those cases, the function erases the element <code>list[pos]</code>. | |
3254 </p> | |
3255 <% | |
3256 end | |
3257 } | |
3258 ["Table.size"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3259 title = "Table.size (tbl)" |
1668 | 3260 content = function() |
3261 %> | |
3262 <% | |
3263 end | |
3264 } | |
3265 ["Table.sort"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3266 title = "Table.sort (list [, comp])" |
1668 | 3267 content = function() |
3268 %> | |
3269 <p> | |
3270 Sorts list elements in a given order, <em>in-place</em>, | |
3271 from <code>list[1]</code> to <code>list[#list]</code>. | |
3272 If <code>comp</code> is given, | |
3273 then it must be a function that receives two list elements | |
3274 and returns true when the first element must come | |
3275 before the second in the final order | |
3276 (so that <code>not comp(list[i+1],list[i])</code> will be true after the sort). | |
3277 If <code>comp</code> is not given, | |
3278 then the standard Lua operator <code><</code> is used instead. | |
3279 </p> | |
3280 | |
3281 <p> | |
3282 The sort algorithm is not stable; | |
3283 that is, elements considered equal by the given order | |
3284 may have their relative positions changed by the sort. | |
3285 </p> | |
3286 <% | |
3287 end | |
3288 } | |
3289 ["Table.unpack"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3290 title = "Table.unpack (list [, i [, j]])" |
1668 | 3291 content = function() |
3292 %> | |
3293 <p> | |
3294 Returns the elements from the given list. | |
3295 This function is equivalent to | |
3296 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3297 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3298 return list[i], list[i+1], ···, list[j] |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3299 </code> |
1668 | 3300 |
3301 <p> | |
3302 By default, <code>i</code> is 1 and <code>j</code> is <code>list.n or #list</code>. | |
3303 </p> | |
3304 <% | |
3305 end | |
3306 } | |
3307 } | |
3308 } | |
3309 number_lib = { | |
3310 title = "Number Manipulation" | |
3311 content = function() | |
3312 %> | |
3313 <p> | |
3314 Include this library by: | |
3315 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3316 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3317 local Number = require "luan:Number.luan" |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3318 </code> |
1668 | 3319 <% |
3320 end | |
3321 subs = { | |
3322 ["Number.double"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3323 title = "Number.double (x)" |
1668 | 3324 content = function() |
3325 %> | |
3326 <p> | |
3327 Returns <code>x</code> as a double. | |
3328 </p> | |
3329 <% | |
3330 end | |
3331 } | |
3332 ["Number.float"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3333 title = "Number.float (x)" |
1668 | 3334 content = function() |
3335 %> | |
3336 <p> | |
3337 Returns <code>x</code> as a float. | |
3338 </p> | |
3339 <% | |
3340 end | |
3341 } | |
3342 ["Number.integer"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3343 title = "Number.integer (x)" |
1668 | 3344 content = function() |
3345 %> | |
3346 <p> | |
3347 If the value <code>x</code> is convertible to an integer, | |
3348 returns that integer. | |
3349 Otherwise throws an error. | |
3350 </p> | |
3351 <% | |
3352 end | |
3353 } | |
3354 ["Number.long"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3355 title = "Number.long (x)" |
1668 | 3356 content = function() |
3357 %> | |
3358 <p> | |
3359 If the value <code>x</code> is convertible to an long, | |
3360 returns that long. | |
3361 Otherwise throws an error. | |
3362 </p> | |
3363 <% | |
3364 end | |
3365 } | |
3366 ["Number.long_to_string"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3367 title = "Number.long_to_string (i, radix)" |
1668 | 3368 content = function() |
3369 %> | |
3370 <p> | |
1816 | 3371 Converts long value <code>i</code> to a string by calling <code><a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html#toString-long-int-">Long.toString</a></code>. |
1668 | 3372 </p> |
3373 <% | |
3374 end | |
3375 } | |
3376 ["Number.type"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3377 title = "Number.type (x)" |
1668 | 3378 content = function() |
3379 %> | |
3380 <p> | |
3381 Returns a string for the numeric type of <code>x</code>. Possible return values include "<code>integer</code>", "<code>long</code>", "<code>double</code>", and "<code>float</code>". | |
3382 </p> | |
3383 <% | |
3384 end | |
3385 } | |
3386 } | |
3387 } | |
1669 | 3388 math_lib = { |
3389 title = "Mathematical Functions" | |
3390 content = function() | |
3391 %> | |
3392 <p> | |
3393 Include this library by: | |
3394 </p> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3395 <code block> |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3396 local Math = require "luan:Math.luan" |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3397 </code> |
1669 | 3398 |
3399 <p> | |
3400 This library provides basic mathematical functions. | |
3401 It provides all its functions and constants inside the table <code>Math</code>. | |
3402 </p> | |
3403 <% | |
3404 end | |
3405 subs = { | |
3406 ["Math.abs"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3407 title = "Math.abs (x)" |
1669 | 3408 content = function() |
3409 %> | |
3410 <p> | |
3411 Returns the absolute value of <code>x</code>. | |
3412 </p> | |
3413 <% | |
3414 end | |
3415 } | |
3416 ["Math.acos"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3417 title = "Math.acos (x)" |
1669 | 3418 content = function() |
3419 %> | |
3420 <p> | |
3421 Returns the arc cosine of <code>x</code> (in radians). | |
3422 </p> | |
3423 <% | |
3424 end | |
3425 } | |
3426 ["Math.asin"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3427 title = "Math.asin (x)" |
1669 | 3428 content = function() |
3429 %> | |
3430 <p> | |
3431 Returns the arc sine of <code>x</code> (in radians). | |
3432 </p> | |
3433 <% | |
3434 end | |
3435 } | |
3436 ["Math.atan"] = { | |
1818 | 3437 title = "Math.atan (a)" |
3438 content = function() | |
3439 %> | |
3440 <p> | |
3441 Returns the arc tangent of a value; the returned angle is in the range -pi/2 through pi/2.</p> | |
3442 <% | |
3443 end | |
3444 } | |
3445 ["Math.atan2"] = { | |
3446 title = "Math.atan2 (y, x)" | |
1669 | 3447 content = function() |
3448 %> | |
3449 <p> | |
3450 Returns the arc tangent of <code>y/x</code> (in radians), | |
3451 but uses the signs of both parameters to find the | |
3452 quadrant of the result. | |
3453 (It also handles correctly the case of <code>x</code> being zero.) | |
3454 </p> | |
3455 <% | |
3456 end | |
3457 } | |
3458 ["Math.ceil"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3459 title = "Math.ceil (x)" |
1669 | 3460 content = function() |
3461 %> | |
3462 <p> | |
3463 Returns the smallest integral value larger than or equal to <code>x</code>. | |
3464 </p> | |
3465 <% | |
3466 end | |
3467 } | |
3468 ["Math.cos"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3469 title = "Math.cos (x)" |
1669 | 3470 content = function() |
3471 %> | |
3472 <p> | |
3473 Returns the cosine of <code>x</code> (assumed to be in radians). | |
3474 </p> | |
3475 <% | |
3476 end | |
3477 } | |
3478 ["Math.deg"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3479 title = "Math.deg (x)" |
1669 | 3480 content = function() |
3481 %> | |
3482 <p> | |
3483 Converts the angle <code>x</code> from radians to degrees. | |
3484 </p> | |
3485 <% | |
3486 end | |
3487 } | |
3488 ["Math.exp"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3489 title = "Math.exp (x)" |
1669 | 3490 content = function() |
3491 %> | |
3492 <p> | |
3493 Returns the value <em>e<sup>x</sup></em> | |
3494 (where <code>e</code> is the base of natural logarithms). | |
3495 </p> | |
3496 <% | |
3497 end | |
3498 } | |
3499 ["Math.floor"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3500 title = "Math.floor (x)" |
1669 | 3501 content = function() |
3502 %> | |
3503 <p> | |
3504 Returns the largest integral value smaller than or equal to <code>x</code>. | |
3505 </p> | |
3506 <% | |
3507 end | |
3508 } | |
3509 ["Math.fmod"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3510 title = "Math.fmod (x, y)" |
1669 | 3511 content = function() |
3512 %> | |
3513 <p> | |
3514 Returns the remainder of the division of <code>x</code> by <code>y</code> | |
3515 that rounds the quotient towards zero. | |
3516 </p> | |
3517 <% | |
3518 end | |
3519 } | |
3520 ["Math.huge"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3521 title = "Math.huge" |
1669 | 3522 content = function() |
3523 %> | |
3524 <p> | |
3525 A value larger than any other numerical value. | |
3526 </p> | |
3527 <% | |
3528 end | |
3529 } | |
3530 ["Math.log"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3531 title = "Math.log (x [, base])" |
1669 | 3532 content = function() |
3533 %> | |
3534 <p> | |
3535 Returns the logarithm of <code>x</code> in the given base. | |
3536 The default for <code>base</code> is <em>e</em> | |
3537 (so that the function returns the natural logarithm of <code>x</code>). | |
3538 </p> | |
3539 <% | |
3540 end | |
3541 } | |
3542 ["Math.max"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3543 title = "Math.max (x, ···)" |
1669 | 3544 content = function() |
3545 %> | |
3546 <p> | |
3547 Returns the argument with the maximum value, | |
3548 according to the Lua operator <code><</code>. | |
3549 </p> | |
3550 <% | |
3551 end | |
3552 } | |
3553 ["Math.max_integer"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3554 title = "Math.max_integer" |
1669 | 3555 content = function() |
3556 %> | |
3557 <p> | |
3558 An integer with the maximum value for an integer. | |
3559 </p> | |
3560 <% | |
3561 end | |
3562 } | |
3563 ["Math.min"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3564 title = "Math.min (x, ···)" |
1669 | 3565 content = function() |
3566 %> | |
3567 <p> | |
3568 Returns the argument with the minimum value, | |
3569 according to the Lua operator <code><</code>. | |
3570 </p> | |
3571 <% | |
3572 end | |
3573 } | |
3574 ["Math.min_integer"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3575 title = "Math.min_integer" |
1669 | 3576 content = function() |
3577 %> | |
3578 <p> | |
3579 An integer with the minimum value for an integer. | |
3580 </p> | |
3581 <% | |
3582 end | |
3583 } | |
3584 ["Math.modf"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3585 title = "Math.modf (x)" |
1669 | 3586 content = function() |
3587 %> | |
3588 <p> | |
3589 Returns the integral part of <code>x</code> and the fractional part of <code>x</code>. | |
3590 </p> | |
3591 <% | |
3592 end | |
3593 } | |
3594 ["Math.pi"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3595 title = "Math.pi" |
1669 | 3596 content = function() |
3597 %> | |
3598 <p> | |
3599 The value of <em>π</em>. | |
3600 </p> | |
3601 <% | |
3602 end | |
3603 } | |
3604 ["Math.rad"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3605 title = "Math.rad (x)" |
1669 | 3606 content = function() |
3607 %> | |
3608 <p> | |
3609 Converts the angle <code>x</code> from degrees to radians. | |
3610 </p> | |
3611 <% | |
3612 end | |
3613 } | |
3614 ["Math.random"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3615 title = "Math.random ([m [, n])" |
1669 | 3616 content = function() |
3617 %> | |
3618 <p> | |
3619 When called without arguments, | |
3620 returns a pseudo-random float with uniform distribution | |
3621 in the range <em>[0,1)</em>. | |
3622 When called with two integers <code>m</code> and <code>n</code>, | |
3623 <code>Math.random</code> returns a pseudo-random integer | |
3624 with uniform distribution in the range <em>[m, n]</em>. | |
3625 (The value <em>m-n</em> cannot be negative and must fit in a Luan integer.) | |
3626 The call <code>Math.random(n)</code> is equivalent to <code>Math.random(1,n)</code>. | |
3627 </p> | |
3628 | |
3629 <p> | |
3630 This function is an interface to the underling | |
3631 pseudo-random generator function provided by Java. | |
3632 No guarantees can be given for its statistical properties. | |
3633 </p> | |
3634 <% | |
3635 end | |
3636 } | |
3637 ["Math.sin"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3638 title = "Math.sin (x)" |
1669 | 3639 content = function() |
3640 %> | |
3641 <p> | |
3642 Returns the sine of <code>x</code> (assumed to be in radians). | |
3643 </p> | |
3644 <% | |
3645 end | |
3646 } | |
3647 ["Math.sqrt"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3648 title = "Math.sqrt (x)" |
1669 | 3649 content = function() |
3650 %> | |
3651 <p> | |
3652 Returns the square root of <code>x</code>. | |
3653 (You can also use the expression <code>x^0.5</code> to compute this value.) | |
3654 </p> | |
3655 <% | |
3656 end | |
3657 } | |
3658 ["Math.tan"] = { | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3659 title = "Math.tan (x)" |
1669 | 3660 content = function() |
3661 %> | |
3662 <p> | |
3663 Returns the tangent of <code>x</code> (assumed to be in radians). | |
3664 </p> | |
3665 <% | |
3666 end | |
3667 } | |
3668 } | |
3669 } | |
1656 | 3670 } |
3671 } | |
3672 } | |
3673 | |
3674 | |
3675 return function() | |
3676 Io.stdout = Http.response.text_writer() | |
3677 %> | |
3678 <!doctype html> | |
3679 <html> | |
3680 <head> | |
3681 <% head() %> | |
3682 <title>Luan Reference Manual</title> | |
3683 <style> | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3684 p[list] { |
1656 | 3685 font-family: monospace; |
3686 margin-left: 40px; | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3687 } |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3688 p[list] span { |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3689 display: inline-block; |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3690 } |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3691 p[list=keywords] { |
1656 | 3692 max-width: 700px; |
3693 } | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3694 p[list=keywords] span { |
1656 | 3695 width: 100px; |
3696 } | |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3697 p[list=tokens] { |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3698 max-width: 400px; |
1716 | 3699 } |
1812
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3700 p[list=tokens] span { |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3701 width: 50px; |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3702 } |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3703 li[c_libs] li li > a { |
f44dcb3fedf7
docs - add code block
Franklin Schmidt <fschmidt@gmail.com>
parents:
1797
diff
changeset
|
3704 font-family: monospace; |
1716 | 3705 } |
1656 | 3706 </style> |
3707 </head> | |
3708 <body> | |
3709 <% docs_header() %> | |
3710 <div content> | |
3711 <h1><a href="manual.html">Luan Reference Manual</a></h1> | |
3712 <p small> | |
3713 Original copyright © 2015 Lua.org, PUC-Rio. | |
3714 Freely available under the terms of the | |
1827 | 3715 <a href="https://www.lua.org/license.html">Lua license</a>. |
1656 | 3716 Modified for Luan. |
3717 </p> | |
3718 <hr> | |
3719 <h2>Contents</h2> | |
3720 <div toc> | |
3721 <% show_toc(content) %> | |
3722 </div> | |
3723 <hr> | |
3724 <% show_content(content,2) %> | |
3725 </div> | |
3726 </body> | |
3727 </html> | |
3728 <% | |
3729 end |