comparison website/src/manual.html @ 1325:28c1fc6d9d29

website - make docs html
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 07 Feb 2019 23:00:20 -0700
parents website/src/manual.html.luan@1660136ac451
children 29d6d7d79c41
comparison
equal deleted inserted replaced
1324:1660136ac451 1325:28c1fc6d9d29
1 <!doctype html>
2 <html>
3 <head>
4 <title>Luan Reference Manual</title>
5 <style>
6 @import "/site.css";
7 </style>
8 </head>
9 <body>
10
11 <div small><a href="/">Luan</a></div>
12
13 <h1>Luan Reference Manual</h1>
14
15 <p small>
16 Original copyright &copy; 2015 Lua.org, PUC-Rio.
17 Freely available under the terms of the
18 <a href="http://www.lua.org/license.html">Lua license</a>.
19 Modified for Luan.
20 </p>
21
22 <hr/>
23
24 <h2>Contents</h2>
25
26 <div contents><a href="#intro">Introduction</a></div>
27
28 <div contents>
29 <a href="#basic">Basic Concepts</a>
30 <ul>
31 <li><a href="#types">Values and Types</a></li>
32 <li><a href="#env">Environments</a></li>
33 <li><a href="#error">Error Handling</a></li>
34 <li><a href="#meta">Metatables and Metamethods</a></li>
35 <li><a href="#gc">Garbage Collection</a></li>
36 </ul>
37 </div>
38
39 <div contents>
40 <a href="#lang">The Language</a>
41 <ul>
42 <li><a href="#lex">Lexical Conventions</a></li>
43 <li><a href="#vars">Variables</a></li>
44 <li>
45 <a href="#stmts">Statements</a>
46 <ul>
47 <li><a href="#blocks">Blocks</a></li>
48 <li><a href="#chunks">Chunks</a></li>
49 <li><a href="#assignment">Assignment</a></li>
50 <li><a href="#control">Control Structures</a></li>
51 <li><a href="#for">For Statement</a></li>
52 <li><a href="#fn_stmt">Function Calls as Statements</a></li>
53 <li><a href="#local_stmt">Local Declarations</a></li>
54 <li><a href="#template_stmt">Template Statements</a></li>
55 </ul>
56 </li>
57 <li>
58 <a href="#expressions">Expressions</a>
59 <ul>
60 <li><a href="#arithmetic">Arithmetic Operators</a></li>
61 <li><a href="#conversions">Coercions and Conversions</a></li>
62 <li><a href="#relational">Relational Operators</a></li>
63 <li><a href="#logical_ops">Logical Operators</a></li>
64 <li><a href="#concatenation">Concatenation</a></li>
65 <li><a href="#length">The Length Operator</a></li>
66 <li><a href="#precedence">Precedence</a></li>
67 <li><a href="#constructors">Table Constructors</a></li>
68 <li><a href="#fn_calls">Function Calls</a></li>
69 <li><a href="#fn_def">Function Definitions</a></li>
70 <li><a href="#template_expr">Template Expressions</a></li>
71 </ul>
72 </li>
73 <li><a href="#visibility">Visibility Rules</a></li>
74 </ul>
75 </div>
76
77 <div contents>
78 <a href="#libs">Standard Libraries</a>
79 <ul>
80 <li><a href="#default_lib">Default Environment</a></li>
81 <li><a href="#luan_lib">Basic Functions</a></li>
82 <li><a href="#package_lib">Modules</a></li>
83 <li><a href="#string_lib">String Manipulation</a></li>
84 <li><a href="#binary_lib">Binary Manipulation</a></li>
85 <li><a href="#table_lib">Table Manipulation</a></li>
86 <li><a href="#number_lib">Number Manipulation</a></li>
87 <li><a href="#math_lib">Mathematical Functions</a></li>
88 </ul>
89 </div>
90
91 <hr/>
92
93
94 <h2 heading><a name="intro">Introduction</a></h2>
95
96 <p>Luan is a high level programming language based on <a href="http://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.</p>
97
98 <p>Luan is implemented in Java and is tightly coupled with Java. So it makes a great scripting language for Java programmers.</p>
99
100 <p>Unlike Lua which is meant to be embedded, Luan is meant to be a full scripting language. This done not by adding feature to Luan, but rather by providing a complete set of libraries.</p>
101
102
103 <h2 heading><a name="basic">Basic Concepts</a></h2>
104
105 <p>This section describes the basic concepts of the language.</p>
106
107 <h3 heading><a name="types">Values and Types</a></h3>
108
109 <p>
110 Luan is a <em>dynamically typed language</em>.
111 This means that
112 variables do not have types; only values do.
113 There are no type definitions in the language.
114 All values carry their own type.
115
116
117 <p>
118 All values in Luan are <em>first-class values</em>.
119 This means that all values can be stored in variables,
120 passed as arguments to other functions, and returned as results.
121
122
123 <p>
124 There are eight basic types in Luan:
125 <em>nil</em>, <em>boolean</em>, <em>number</em>,
126 <em>string</em>, <em>binary</em>, <em>function</em>, <em>java</em>,
127 and <em>table</em>.
128 <em>Nil</em> is the type of the value <b>nil</b>,
129 whose main property is to be different from any other value;
130 it usually represents the absence of a useful value.
131 <em>Nil</em> is implemented as the Java value <em>null</em>.
132 <em>Boolean</em> is the type of the values <b>false</b> and <b>true</b>.
133 <em>Boolean</em> is implemented as the Java class <em>Boolean</em>.
134 <em>Number</em> represents both
135 integer numbers and real (floating-point) numbers.
136 <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
137 the underlying Java implementation.
138
139 <em>String</em> is implemented as the Java class <em>String</em>.
140 <em>Binary</em> is implemented as the Java type <em>byte[]</em>.
141
142
143 <p>
144 Luan can call (and manipulate) functions written in Luan and
145 functions written in Java (see <a href="#fn_calls">Function Calls</a>).
146 Both are represented by the type <em>function</em>.
147
148
149 <p>
150 The type <em>java</em> is provided to allow arbitrary Java objects to
151 be stored in Luan variables.
152 A <em>java</em> value is a Java object that isn't one of the standard Luan types.
153 Java values have no predefined operations in Luan,
154 except assignment and identity test.
155 Java values are useful when Java access is enabled in Luan
156
157
158
159 <p>
160 The type <em>table</em> implements associative arrays,
161 that is, arrays that can be indexed not only with numbers,
162 but with any Luan value except <b>nil</b>.
163 Tables can be <em>heterogeneous</em>;
164 that is, they can contain values of all types (except <b>nil</b>).
165 Any key with value <b>nil</b> is not considered part of the table.
166 Conversely, any key that is not part of a table has
167 an associated value <b>nil</b>.
168
169
170 <p>
171 Tables are the sole data-structuring mechanism in Luan;
172 they can be used to represent ordinary arrays, sequences,
173 symbol tables, sets, records, graphs, trees, etc.
174 To represent records, Luan uses the field name as an index.
175 The language supports this representation by
176 providing <code>a.name</code> as syntactic sugar for <code>a["name"]</code>.
177 There are several convenient ways to create tables in Luan
178 (see <a href="#constructors">Table Constructors</a>).
179
180
181 <p>
182 We use the term <em>sequence</em> to denote a table where
183 the set of all positive numeric keys is equal to {1..<em>n</em>}
184 for some non-negative integer <em>n</em>,
185 which is called the length of the sequence (see <a href="#length">The Length Operator</a>).
186
187
188 <p>
189 Like indices,
190 the values of table fields can be of any type.
191 In particular,
192 because functions are first-class values,
193 table fields can contain functions.
194 Thus tables can also carry <em>methods</em> (see <a href="#fn_def">Function Definitions</a>).
195
196
197 <p>
198 The indexing of tables follows
199 the definition of raw equality in the language.
200 The expressions <code>a[i]</code> and <code>a[j]</code>
201 denote the same table element
202 if and only if <code>i</code> and <code>j</code> are raw equal
203 (that is, equal without metamethods).
204 In particular, floats with integral values
205 are equal to their respective integers
206 (e.g., <code>1.0 == 1</code>).
207
208
209 <p>
210 Luan values are <em>objects</em>:
211 variables do not actually <em>contain</em> values,
212 only <em>references</em> to them.
213 Assignment, parameter passing, and function returns
214 always manipulate references to values;
215 these operations do not imply any kind of copy.
216
217
218 <p>
219 The library function <a href="#Luan.type"><code>Luan.type</code></a> returns a string describing the type
220 of a given value.
221
222
223
224
225
226 <h3 heading><a name="env">Environments</a></h3>
227
228 <p>
229 The environment of a chunk starts with only two local variables: <code><a href="#require">require</a></code> and <code><a href="#java">java</a></code>. These are functions are used to load and access libraries and other modules. All other variables must be added to the environment using <a href="http://localhost:8080/manual.html#local_stmt">local declarations</a>.
230
231 <p>
232 As will be discussed in <a href="#vars">Variables</a> and <a href=#assignment">Assignment</a>,
233 any reference to a free name
234 (that is, a name not bound to any declaration) <code>var</code>
235 can be syntactically translated to <code>_ENV.var</code> if <code>_ENV</code> is defined.
236
237
238 <h3 heading><a name="error">Error Handling</a></h3>
239
240 <p>
241 Luan code can explicitly generate an error by calling the
242 <a href="#Luan.error"><code>error</code></a> function.
243 If you need to catch errors in Luan,
244 you can use <a href="#Luan.pcall"><code>pcall</code></a> or <a href="#Luan.try"><code>try</code></a>
245 to call a given function in <em>protected mode</em>.
246
247
248 <p>
249 Whenever there is an error,
250 an <em>error table</em>
251 is propagated with information about the error.
252 See <a href="#Luan.new_error"><code>Luan.new_error</code></a>.
253
254
255
256 <h3 heading><a name="meta">Metatables and Metamethods</a></h3>
257
258 <p>
259 Every table in Luan can have a <em>metatable</em>.
260 This <em>metatable</em> is an ordinary Luan table
261 that defines the behavior of the original value
262 under certain special operations.
263 You can change several aspects of the behavior
264 of operations over a value by setting specific fields in its metatable.
265 For instance, when a table is the operand of an addition,
266 Luan checks for a function in the field "<code>__add</code>" of the table's metatable.
267 If it finds one,
268 Luan calls this function to perform the addition.
269
270
271 <p>
272 The keys in a metatable are derived from the <em>event</em> names;
273 the corresponding values are called <ii>metamethods</em>.
274 In the previous example, the event is <code>"add"</code>
275 and the metamethod is the function that performs the addition.
276
277
278 <p>
279 You can query the metatable of any table
280 using the <a href="#Luan.get_metatable"><code>get_metatable</code></a> function.
281
282
283 <p>
284 You can replace the metatable of tables
285 using the <a href="#Luan.set_metatable"><code>set_metatable</code></a> function.
286
287
288 <p>
289 A metatable controls how a table behaves in
290 arithmetic operations, bitwise operations,
291 order comparisons, concatenation, length operation, calls, and indexing.
292
293
294 <p>
295 A detailed list of events controlled by metatables is given next.
296 Each operation is identified by its corresponding event name.
297 The key for each event is a string with its name prefixed by
298 two underscores, '<code>__</code>';
299 for instance, the key for operation "add" is the
300 string "<code>__add</code>".
301 Note that queries for metamethods are always raw;
302 the access to a metamethod does not invoke other metamethods.
303 You can emulate how Luan queries a metamethod for an object <code>obj</code>
304 with the following code:
305
306 <pre>
307 raw_get(get_metatable(obj) or {}, "__" .. event_name)
308 </pre>
309
310 <p>
311 Here are the events:
312
313 <ul>
314
315 <li><p><b>"add": </b>
316 the <code>+</code> operation.
317
318 If any operand for an addition is a table,
319 Luan will try to call a metamethod.
320 First, Luan will check the first operand (even if it is valid).
321 If that operand does not define a metamethod for the "<code>__add</code>" event,
322 then Luan will check the second operand.
323 If Luan can find a metamethod,
324 it calls the metamethod with the two operands as arguments,
325 and the result of the call
326 (adjusted to one value)
327 is the result of the operation.
328 Otherwise,
329 it raises an error.
330 </li>
331
332 <li><p><b>"sub": </b>
333 the <code>-</code> operation.
334
335 Behavior similar to the "add" operation.
336 </li>
337
338 <li><p><b>"mul": </b>
339 the <code>*</code> operation.
340
341 Behavior similar to the "add" operation.
342 </li>
343
344 <li><p><b>"div": </b>
345 the <code>/</code> operation.
346
347 Behavior similar to the "add" operation.
348 </li>
349
350 <li><p><b>"mod": </b>
351 the <code>%</code> operation.
352
353 Behavior similar to the "add" operation.
354 </li>
355
356 <li><p><b>"pow": </b>
357 the <code>^</code> (exponentiation) operation.
358
359 Behavior similar to the "add" operation.
360 </li>
361
362 <li><p><b>"unm": </b>
363 the <code>-</code> (unary minus) operation.
364
365 Behavior similar to the "add" operation.
366 </li>
367
368 <li><p><b>"concat": </b>
369 the <code>..</code> (concatenation) operation.
370
371 Behavior similar to the "add" operation.
372 </li>
373
374 <li><p><b>"len": </b>
375 the <code>#</code> (length) operation.
376
377 If there is a metamethod,
378 Luan calls it with the object as argument,
379 and the result of the call
380 (always adjusted to one value)
381 is the result of the operation.
382 If there is no metamethod but the object is a table,
383 then Luan uses the table length operation (see <a href="#length">The Length Operator</a>).
384 Otherwise, Luan raises an error.
385 </li>
386
387 <li><p><b>"eq": </b>
388 the <code>==</code> (equal) operation.
389
390 Behavior similar to the "add" operation,
391 except that Luan will try a metamethod only when the values
392 being compared are both tables
393 and they are not primitively equal.
394 The result of the call is always converted to a boolean.
395 </li>
396
397 <li><p><b>"lt": </b>
398 the <code>&lt;</code> (less than) operation.
399
400 Behavior similar to the "add" operation.
401 The result of the call is always converted to a boolean.
402 </li>
403
404 <li><p><b>"le": </b>
405 the <code>&lt;=</code> (less equal) operation.
406
407 Unlike other operations,
408 The less-equal operation can use two different events.
409 First, Luan looks for the "<code>__le</code>" metamethod in both operands,
410 like in the "lt" operation.
411 If it cannot find such a metamethod,
412 then it will try the "<code>__lt</code>" event,
413 assuming that <code>a &lt;= b</code> is equivalent to <code>not (b &lt; a)</code>.
414 As with the other comparison operators,
415 the result is always a boolean.
416 </li>
417
418 <li><p><b>"index": </b>
419 The indexing access <code>table[key]</code>.
420
421 This event happens
422 when <code>key</code> is not present in <code>table</code>.
423 The metamethod is looked up in <code>table</code>.
424
425
426 <p>
427 Despite the name,
428 the metamethod for this event can be any type.
429 If it is a function,
430 it is called with <code>table</code> and <code>key</code> as arguments.
431 Otherwise
432 the final result is the result of indexing this metamethod object with <code>key</code>.
433 (This indexing is regular, not raw,
434 and therefore can trigger another metamethod if the metamethod object is a table.)
435 </li>
436
437 <li><p><b>"new_index": </b>
438 The indexing assignment <code>table[key] = value</code>.
439
440 Like the index event,
441 this event happens when
442 when <code>key</code> is not present in <code>table</code>.
443 The metamethod is looked up in <code>table</code>.
444
445
446 <p>
447 Like with indexing,
448 the metamethod for this event can be either a function or a table.
449 If it is a function,
450 it is called with <code>table</code>, <code>key</code>, and <code>value</code> as arguments.
451 If it is a table,
452 Luan does an indexing assignment to this table with the same key and value.
453 (This assignment is regular, not raw,
454 and therefore can trigger another metamethod.)
455
456
457 <p>
458 Whenever there is a "new_index" metamethod,
459 Luan does not perform the primitive assignment.
460 (If necessary,
461 the metamethod itself can call <a href="#Luan.raw_set"><code>raw_set</code></a>
462 to do the assignment.)
463 </li>
464
465 <li><p><b>"gc":</b>
466 This is when a table is garbage collected. When the table's <a href="https://docs.oracle.com/javase/7/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.
467
468 </li>
469
470 </ul>
471
472
473
474
475 <h3 heading><a name="gc">Garbage Collection</a></h3>
476
477 <p>
478 Luan uses Java's garbage collection.
479
480
481
482
483
484 <h2 heading><a name="lang">The Language</a></h2>
485
486 <p>
487 This section describes the lexis, the syntax, and the semantics of Luan.
488 In other words,
489 this section describes
490 which tokens are valid,
491 how they can be combined,
492 and what their combinations mean.
493
494
495 <p>
496 Language constructs will be explained using the usual extended BNF notation,
497 in which
498 {<em>a</em>}&nbsp;means&nbsp;0 or more <em>a</em>'s, and
499 [<em>a</em>]&nbsp;means an optional <em>a</em>.
500 Non-terminals are shown like non-terminal,
501 keywords are shown like <b>kword</b>,
502 and other terminal symbols are shown like &lsquo;<b>=</b>&rsquo;.
503 The complete syntax of Luan can be found in <a href="#9">&sect;9</a>
504 at the end of this manual.
505
506
507
508 <h3 heading><a name="lex">Lexical Conventions</a></h3>
509
510 <p>
511 Luan ignores spaces and comments
512 between lexical elements (tokens),
513 except as delimiters between names and keywords.
514 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.
515
516 <p>
517 <em>Names</em>
518 (also called <em>identifiers</em>)
519 in Luan can be any string of letters,
520 digits, and underscores,
521 not beginning with a digit.
522 Identifiers are used to name variables, table fields, and labels.
523
524
525 <p>
526 The following <em>keywords</em> are reserved
527 and cannot be used as names:
528
529
530 <pre>
531 and break do else elseif end
532 end_do end_for end_function end_if end_while
533 false for function goto if in
534 local nil not or repeat return
535 then true until while
536 </pre>
537
538 <p>
539 Luan is a case-sensitive language:
540 <code>and</code> is a reserved word, but <code>And</code> and <code>AND</code>
541 are two different, valid names.
542
543
544 <p>
545 The following strings denote other tokens:
546
547 <pre>
548 + - * / % ^ #
549 &amp; ~ | &lt;&lt; &gt;&gt; //
550 == ~= &lt;= &gt;= &lt; &gt; =
551 ( ) { } [ ] ::
552 ; : , . .. ...
553 </pre>
554
555 <p>
556 <em>Literal strings</em>
557 can be delimited by matching single or double quotes,
558 and can contain the following C-like escape sequences:
559 '<code>\a</code>' (bell),
560 '<code>\b</code>' (backspace),
561 '<code>\f</code>' (form feed),
562 '<code>\n</code>' (newline),
563 '<code>\r</code>' (carriage return),
564 '<code>\t</code>' (horizontal tab),
565 '<code>\v</code>' (vertical tab),
566 '<code>\\</code>' (backslash),
567 '<code>\"</code>' (quotation mark [double quote]),
568 and '<code>\'</code>' (apostrophe [single quote]).
569 A backslash followed by a real newline
570 results in a newline in the string.
571 The escape sequence '<code>\z</code>' skips the following span
572 of white-space characters,
573 including line breaks;
574 it is particularly useful to break and indent a long literal string
575 into multiple lines without adding the newlines and spaces
576 into the string contents.
577
578
579 <p>
580 Luan can specify any character in a literal string by its numerical value.
581 This can be done
582 with the escape sequence <code>\x<em>XX</em></code>,
583 where <em>XX</em> is a sequence of exactly two hexadecimal digits,
584 or with the escape sequence <code>\u<em>XXXX</em></code>,
585 where <em>XXXX</em> is a sequence of exactly four hexadecimal digits,
586 or with the escape sequence <code>\<em>ddd</em></code>,
587 where <em>ddd</em> is a sequence of up to three decimal digits.
588 (Note that if a decimal escape sequence is to be followed by a digit,
589 it must be expressed using exactly three digits.)
590
591
592 <p>
593 Literal strings can also be defined using a long format
594 enclosed by <em>long brackets</em>.
595 We define an <em>opening long bracket of level <em>n</em></em> as an opening
596 square bracket followed by <em>n</em> equal signs followed by another
597 opening square bracket.
598 So, an opening long bracket of level 0 is written as <code>[[</code>,
599 an opening long bracket of level 1 is written as <code>[=[</code>,
600 and so on.
601 A <em>closing long bracket</em> is defined similarly;
602 for instance,
603 a closing long bracket of level 4 is written as <code>]====]</code>.
604 A <em>long literal</em> starts with an opening long bracket of any level and
605 ends at the first closing long bracket of the same level.
606 It can contain any text except a closing bracket of the same level.
607 Literals in this bracketed form can run for several lines,
608 do not interpret any escape sequences,
609 and ignore long brackets of any other level.
610 Any kind of end-of-line sequence
611 (carriage return, newline, carriage return followed by newline,
612 or newline followed by carriage return)
613 is converted to a simple newline.
614
615
616 <p>
617 Any character in a literal string not
618 explicitly affected by the previous rules represents itself.
619 However, Luan opens files for parsing in text mode,
620 and the system file functions may have problems with
621 some control characters.
622 So, it is safer to represent
623 non-text data as a quoted literal with
624 explicit escape sequences for non-text characters.
625
626
627 <p>
628 For convenience,
629 when the opening long bracket is immediately followed by a newline,
630 the newline is not included in the string.
631 As an example
632 the five literal strings below denote the same string:
633
634 <pre>
635 a = 'alo\n123"'
636 a = "alo\n123\""
637 a = '\97lo\10\04923"'
638 a = [[alo
639 123"]]
640 a = [==[
641 alo
642 123"]==]
643 </pre>
644
645 <p>
646 A <em>numerical constant</em> (or <em>numeral</em>)
647 can be written with an optional fractional part
648 and an optional decimal exponent,
649 marked by a letter '<code>e</code>' or '<code>E</code>'.
650 Luan also accepts hexadecimal constants,
651 which start with <code>0x</code> or <code>0X</code>.
652 Hexadecimal constants also accept an optional fractional part
653 plus an optional binary exponent,
654 marked by a letter '<code>p</code>' or '<code>P</code>'.
655 A numeric constant with a fractional dot or an exponent
656 denotes a float;
657 otherwise it denotes an integer.
658 Examples of valid integer constants are
659
660 <pre>
661 3 345 0xff 0xBEBADA
662 </pre>
663
664 <p>
665 Examples of valid float constants are
666
667 <pre>
668 3.0 3.1416 314.16e-2 0.31416E1 34e1
669 0x0.1E 0xA23p-4 0X1.921FB54442D18P+1
670 </pre>
671
672 <p>
673 A <em>comment</em> starts with a double hyphen (<code>--</code>)
674 anywhere outside a string.
675 If the text immediately after <code>--</code> is not an opening long bracket,
676 the comment is a <em>short comment</em>,
677 which runs until the end of the line.
678 Otherwise, it is a <em>long comment</em>,
679 which runs until the corresponding closing long bracket.
680 Long comments are frequently used to disable code temporarily.
681
682
683
684
685
686 <h3 heading><a name="vars">Variables</a></h3>
687
688 <p>
689 Variables are places that store values.
690 There are three kinds of variables in Luan:
691 global variables, local variables, and table fields.
692
693 <p>
694 A single name can denote a global variable or a local variable
695 (or a function's formal parameter,
696 which is a particular kind of local variable):
697
698 <pre>
699 var ::= Name
700 </pre>
701
702 <p>
703 Name denotes identifiers, as defined in <a href="#lex">Lexical Conventions</a>.
704
705 <p>
706 Local variables are <em>lexically scoped</em>:
707 local variables can be freely accessed by functions
708 defined inside their scope (see <a href="#visibility">Visibility Rules</a>).
709
710
711 <p>
712 Before the first assignment to a variable, its value is <b>nil</b>.
713
714 <p>
715 Square brackets are used to index a table:
716
717 <pre>
718 var ::= prefixexp &lsquo;<b>[</b>&rsquo; exp &lsquo;<b>]</b>&rsquo;
719 </pre>
720
721 <p>
722 The meaning of accesses to table fields can be changed via metatables.
723 An access to an indexed variable <code>t[i]</code> is equivalent to
724 a call <code>gettable_event(t,i)</code>.
725 (See <a href="#meta">Metatables and Metamethods</a> for a complete description of the
726 <code>gettable_event</code> function.
727 This function is not defined or callable in Luan.
728 We use it here only for explanatory purposes.)
729
730
731 <p>
732 The syntax <code>var.Name</code> is just syntactic sugar for
733 <code>var["Name"]</code>:
734
735 <pre>
736 var ::= prefixexp &lsquo;<b>.</b>&rsquo; Name
737 </pre>
738
739 <p>
740 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>
741 is equivalent to <code>_ENV.x</code>.
742 Due to the way that chunks are compiled,
743 <code>_ENV</code> is never a global name (see <a href="#env">Environments</a>).
744
745
746
747
748
749 <h3 heading><a name="stmts">Statements</a></h3>
750
751 <p>
752 Luan supports an almost conventional set of statements,
753 similar to those in Pascal or C.
754 This set includes
755 assignments, control structures, function calls,
756 and variable declarations.
757
758
759
760 <h4 heading><a name="blocks">Blocks</a></h4>
761
762 <p>
763 A block is a list of statements,
764 which are executed sequentially:
765
766 <pre>
767 block ::= {stat}
768 </pre>
769
770 <p>
771 Luan has <em>empty statements</em>
772 that allow you to separate statements with semicolons,
773 start a block with a semicolon
774 or write two semicolons in sequence:
775
776 <pre>
777 stat ::= &lsquo;<b>;</b>&rsquo;
778 </pre>
779
780 <p>
781 A block can be explicitly delimited to produce a single statement:
782
783 <pre>
784 stat ::= <b>do</b> block end_do
785 end_do ::= <b>end_do</b> | <b>end</b>
786 </pre>
787
788 <p>
789 Explicit blocks are useful
790 to control the scope of variable declarations.
791 Explicit blocks are also sometimes used to
792 add a <b>return</b> statement in the middle
793 of another block (see <a href="#control">Control Structures</a>).
794
795
796
797
798
799 <h4 heading><a name="chunks">Chunks</a></h4>
800
801 <p>
802 The unit of compilation of Luan is called a <em>chunk</em>.
803 Syntactically,
804 a chunk is simply a block:
805
806 <pre>
807 chunk ::= block
808 </pre>
809
810 <p>
811 Luan handles a chunk as the body of an anonymous function
812 with a variable number of arguments
813 (see <a href="#fn_def">Function Definitions</a>).
814 As such, chunks can define local variables,
815 receive arguments, and return values.
816
817
818 <p>
819 A chunk can be stored in a file or in a string inside the host program.
820 To execute a chunk,
821 Luan first <em>loads</em> it,
822 compiling the chunk's code,
823 and then Luan executes the compiled code.
824
825
826
827
828
829 <h4 heading><a name="assignment">Assignment</a></h4>
830
831 <p>
832 Luan allows multiple assignments.
833 Therefore, the syntax for assignment
834 defines a list of variables on the left side
835 and a list of expressions on the right side.
836 The elements in both lists are separated by commas:
837
838 <pre>
839 stat ::= varlist &lsquo;<b>=</b>&rsquo; explist
840 varlist ::= var {&lsquo;<b>,</b>&rsquo; var}
841 explist ::= exp {&lsquo;<b>,</b>&rsquo; exp}
842 </pre>
843
844 <p>
845 Expressions are discussed in <a href="#expressions">Expressions</a>.
846
847
848 <p>
849 Before the assignment,
850 the list of values is <em>adjusted</em> to the length of
851 the list of variables.
852 If there are more values than needed,
853 the excess values are thrown away.
854 If there are fewer values than needed,
855 the list is extended with as many <b>nil</b>'s as needed.
856 If the list of expressions ends with a function call,
857 then all values returned by that call enter the list of values,
858 before the adjustment
859 (except when the call is enclosed in parentheses; see <a href="#expressions">Expressions</a>).
860
861
862 <p>
863 The assignment statement first evaluates all its expressions
864 and only then the assignments are performed.
865 Thus the code
866
867 <pre>
868 i = 3
869 i, a[i] = i+1, 20
870 </pre>
871
872 <p>
873 sets <code>a[3]</code> to 20, without affecting <code>a[4]</code>
874 because the <code>i</code> in <code>a[i]</code> is evaluated (to 3)
875 before it is assigned&nbsp;4.
876 Similarly, the line
877
878 <pre>
879 x, y = y, x
880 </pre>
881
882 <p>
883 exchanges the values of <code>x</code> and <code>y</code>,
884 and
885
886 <pre>
887 x, y, z = y, z, x
888 </pre>
889
890 <p>
891 cyclically permutes the values of <code>x</code>, <code>y</code>, and <code>z</code>.
892
893
894 <p>
895 The meaning of assignments to global variables
896 and table fields can be changed via metatables.
897 An assignment to an indexed variable <code>t[i] = val</code> is equivalent to
898 <code>settable_event(t,i,val)</code>.
899 (See <a href="#meta">Metatables and Metamethods</a> for a complete description of the
900 <code>settable_event</code> function.
901 This function is not defined or callable in Luan.
902 We use it here only for explanatory purposes.)
903
904
905 <p>
906 An assignment to a global name <code>x = val</code>
907 is equivalent to the assignment
908 <code>_ENV.x = val</code> (see <a href="#env">Environments</a>).
909 Global names are only available when <code>_ENV</code> is defined.
910
911
912
913 <h4 heading><a name="control">Control Structures</a></h4>
914
915 <p>
916 The control structures
917 <b>if</b>, <b>while</b>, and <b>repeat</b> have the usual meaning and
918 familiar syntax:
919
920 <pre>
921 stat ::= <b>while</b> exp <b>do</b> block end_while
922 stat ::= <b>repeat</b> block <b>until</b> exp
923 stat ::= <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] end_if
924 end_while ::= <b>end_while</b> | <b>end</b>
925 end_if ::= <b>end_if</b> | <b>end</b>
926 </pre>
927
928 <p>
929 Luan also has a <b>for</b> statement (see <a href="#for">For Statement</a>).
930
931
932 <p>
933 The condition expression of a
934 control structure must be a boolean.
935 Any other value type will produce an error.
936 This helps catch errors and makes code more readable.
937
938
939 <p>
940 In the <b>repeat</b>&ndash;<b>until</b> loop,
941 the inner block does not end at the <b>until</b> keyword,
942 but only after the condition.
943 So, the condition can refer to local variables
944 declared inside the loop block.
945
946
947 <p>
948 The <b>break</b> statement terminates the execution of a
949 <b>while</b>, <b>repeat</b>, or <b>for</b> loop,
950 skipping to the next statement after the loop:
951
952
953 <pre>
954 stat ::= <b>break</b>
955 </pre>
956
957 <p>
958 A <b>break</b> ends the innermost enclosing loop.
959
960
961 <p>
962 The <b>return</b> statement is used to return values
963 from a function or a chunk
964 (which is an anonymous function).
965
966 Functions can return more than one value,
967 so the syntax for the <b>return</b> statement is
968
969 <pre>
970 stat ::= <b>return</b> [explist] [&lsquo;<b>;</b>&rsquo;]
971 </pre>
972
973
974
975
976 <h4 heading><a name="for">For Statement</a></h4>
977
978 <p>
979 The <b>for</b> statement works over functions,
980 called <em>iterators</em>.
981 On each iteration, the iterator function is called to produce a new value,
982 stopping when this new value is <b>nil</b>.
983 The <b>for</b> loop has the following syntax:
984
985 <pre>
986 stat ::= <b>for</b> namelist <b>in</b> exp <b>do</b> block end_for
987 namelist ::= Name {&lsquo;<b>,</b>&rsquo; Name}
988 end_for ::= <b>end_for</b> | <b>end</b>
989 </pre>
990
991 <p>
992 A <b>for</b> statement like
993
994 <pre>
995 for <em>var_1</em>, &middot;&middot;&middot;, <em>var_n</em> in <em>exp</em> do <em>block</em> end
996 </pre>
997
998 <p>
999 is equivalent to the code:
1000
1001 <pre>
1002 do
1003 local <em>f</em> = <em>exp</em>
1004 while true do
1005 local <em>var_1</em>, &middot;&middot;&middot;, <em>var_n</em> = <em>f</em>()
1006 if <em>var_1</em> == nil then break end
1007 <em>block</em>
1008 end
1009 end
1010 </pre>
1011
1012 <p>
1013 Note the following:
1014
1015 <ul>
1016
1017 <li>
1018 <code><em>exp</em></code> is evaluated only once.
1019 Its result is an <em>iterator</em> function.
1020 </li>
1021
1022 <li>
1023 <code><em>f</em></code> is an invisible variable.
1024 The name is here for explanatory purposes only.
1025 </li>
1026
1027 <li>
1028 You can use <b>break</b> to exit a <b>for</b> loop.
1029 </li>
1030
1031 <li>
1032 The loop variables <code><em>var_i</em></code> are local to the loop;
1033 you cannot use their values after the <b>for</b> ends.
1034 If you need these values,
1035 then assign them to other variables before breaking or exiting the loop.
1036 </li>
1037
1038 </ul>
1039
1040
1041
1042
1043 <h4 heading><a name="fn_stmt">Function Calls as Statements</a></h4>
1044
1045 <p>
1046 To allow possible side-effects,
1047 function calls can be executed as statements:
1048
1049 <pre>
1050 stat ::= functioncall
1051 </pre>
1052
1053 <p>
1054 In this case, all returned values are thrown away.
1055 Function calls are explained in <a href="#fn_calls">Function Calls</a>.
1056
1057
1058
1059 <h4 heading><a name="local_stmt">Local Declarations</a></h4>
1060
1061 <p>
1062 Local variables can be declared anywhere inside a block.
1063 The declaration can include an initial assignment:
1064
1065 <pre>
1066 stat ::= <b>local</b> namelist [&lsquo;<b>=</b>&rsquo; explist]
1067 </pre>
1068
1069 <p>
1070 If present, an initial assignment has the same semantics
1071 of a multiple assignment (see <a href="#assignment">Assignment</a>).
1072 Otherwise, all variables are initialized with <b>nil</b>.
1073
1074
1075 <p>
1076 A chunk is also a block (see <a href="#chunks">Chunks</a>),
1077 and so local variables can be declared in a chunk outside any explicit block.
1078
1079
1080 <p>
1081 The visibility rules for local variables are explained in <a href="#visibility">Visibility Rules</a>.
1082
1083
1084 <h4 heading><a name="template_stmt">Template Statements</a></h4>
1085
1086 <p>Template statements are based on <a href="#template_expr">template exressions</a> and provide the full equivalent of <a href="http://en.wikipedia.org/wiki/JavaServer_Pages">JSP</a> but in a general way. Template statements write the equivalent template exression to standard output. For example:</p>
1087
1088 <pre>
1089 local name = "Bob"
1090 %>
1091 Hello <%= name %>!
1092 Bye <%= name %>.
1093 <%
1094 </pre>
1095
1096 <p>is equivalent to the code:</p>
1097
1098 <pre>
1099 local name = "Bob"
1100 require("luan:Io.luan").stdout.write( %>
1101 Hello <%= name %>!
1102 Bye <%= name %>.
1103 <% )
1104 </pre>
1105
1106
1107
1108 <h3 heading><a name="expressions">Expressions</a></h3>
1109
1110 <p>
1111 The basic expressions in Luan are the following:
1112
1113 <pre>
1114 exp ::= prefixexp
1115 exp ::= <b>nil</b> | <b>false</b> | <b>true</b>
1116 exp ::= Numeral
1117 exp ::= LiteralString
1118 exp ::= functiondef
1119 exp ::= tableconstructor
1120 exp ::= &lsquo;<b>...</b>&rsquo;
1121 exp ::= exp binop exp
1122 exp ::= unop exp
1123 prefixexp ::= var | functioncall | &lsquo;<b>(</b>&rsquo; exp &lsquo;<b>)</b>&rsquo;
1124 </pre>
1125
1126 <p>
1127 Numerals and literal strings are explained in <a href="#lex">Lexical Conventions</a>;
1128 variables are explained in <a href="#vars">Variables</a>;
1129 function definitions are explained in <a href="#fn_def">Function Definitions</a>;
1130 function calls are explained in <a href="#fn_calls">Function Calls</a>;
1131 table constructors are explained in <a href="#constructors">Table Constructors</a>.
1132 Vararg expressions,
1133 denoted by three dots ('<code>...</code>'), can only be used when
1134 directly inside a vararg function;
1135 they are explained in <a href="#fn_def">Function Definitions</a>.
1136
1137
1138 <p>
1139 Binary operators comprise arithmetic operators (see <a href="#arithmetic">Arithmetic Operators</a>),
1140 relational operators (see <a href="#relational">Relational Operators</a>), logical operators (see <a href="#logical_ops">Logical Operators</a>),
1141 and the concatenation operator (see <a href="#concatenation">Concatenation</a>).
1142 Unary operators comprise the unary minus (see <a href="#arithmetic">Arithmetic Operators</a>),
1143 the unary logical <b>not</b> (see <a href="#logical_ops">Logical Operators</a>),
1144 and the unary <em>length operator</em> (see <a href="#length">The Length Operator</a>).
1145
1146
1147 <p>
1148 Both function calls and vararg expressions can result in multiple values.
1149 If a function call is used as a statement (see <a href="#fn_stmt">Function Calls as Statements</a>),
1150 then its return list is adjusted to zero elements,
1151 thus discarding all returned values.
1152 If an expression is used as the last (or the only) element
1153 of a list of expressions,
1154 then no adjustment is made
1155 (unless the expression is enclosed in parentheses).
1156 In all other contexts,
1157 Luan adjusts the result list to one element,
1158 either discarding all values except the first one
1159 or adding a single <b>nil</b> if there are no values.
1160
1161
1162 <p>
1163 Here are some examples:
1164
1165 <pre>
1166 f() -- adjusted to 0 results
1167 g(f(), x) -- f() is adjusted to 1 result
1168 g(x, f()) -- g gets x plus all results from f()
1169 a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil)
1170 a,b = ... -- a gets the first vararg parameter, b gets
1171 -- the second (both a and b can get nil if there
1172 -- is no corresponding vararg parameter)
1173
1174 a,b,c = x, f() -- f() is adjusted to 2 results
1175 a,b,c = f() -- f() is adjusted to 3 results
1176 return f() -- returns all results from f()
1177 return ... -- returns all received vararg parameters
1178 return x,y,f() -- returns x, y, and all results from f()
1179 {f()} -- creates a list with all results from f()
1180 {...} -- creates a list with all vararg parameters
1181 {f(), nil} -- f() is adjusted to 1 result
1182 </pre>
1183
1184 <p>
1185 Any expression enclosed in parentheses always results in only one value.
1186 Thus,
1187 <code>(f(x,y,z))</code> is always a single value,
1188 even if <code>f</code> returns several values.
1189 (The value of <code>(f(x,y,z))</code> is the first value returned by <code>f</code>
1190 or <b>nil</b> if <code>f</code> does not return any values.)
1191
1192
1193
1194 <h4 heading><a name="arithmetic">Arithmetic Operators</a></h4>
1195
1196 <p>
1197 Luan supports the following arithmetic operators:
1198
1199 <ul>
1200 <li><b><code>+</code>: </b>addition</li>
1201 <li><b><code>-</code>: </b>subtraction</li>
1202 <li><b><code>*</code>: </b>multiplication</li>
1203 <li><b><code>/</code>: </b>division</li>
1204 <li><b><code>%</code>: </b>modulo</li>
1205 <li><b><code>^</code>: </b>exponentiation</li>
1206 <li><b><code>-</code>: </b>unary minus</li>
1207 </ul>
1208
1209 <p>
1210 Addition, subtraction, multiplication, division, and unary minus are the same as these operators in Java. Exponentiation uses Java's <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#pow(double,%20double)">Math.pow</a> function.
1211
1212 <p>
1213 Modulo is defined as the remainder of a division
1214 that rounds the quotient towards minus infinite (floor division).
1215 (The Java modulo operator is not used.)
1216
1217
1218
1219 <h4 heading><a name="conversions">Coercions and Conversions</a></h4>
1220
1221 <p>
1222 Luan generally avoids automatic conversions.
1223 String concatenation automatically converts all of its arguments to strings.
1224
1225 <p>
1226 Luan provides library functions for explicit type conversions.
1227
1228
1229
1230
1231 <h4 heading><a name="relational">Relational Operators</a></h4>
1232
1233 <p>
1234 Luan supports the following relational operators:
1235
1236 <ul>
1237 <li><b><code>==</code>: </b>equality</li>
1238 <li><b><code>~=</code>: </b>inequality</li>
1239 <li><b><code>&lt;</code>: </b>less than</li>
1240 <li><b><code>&gt;</code>: </b>greater than</li>
1241 <li><b><code>&lt;=</code>: </b>less or equal</li>
1242 <li><b><code>&gt;=</code>: </b>greater or equal</li>
1243 </ul><p>
1244 These operators always result in <b>false</b> or <b>true</b>.
1245
1246
1247 <p>
1248 Equality (<code>==</code>) first compares the type of its operands.
1249 If the types are different, then the result is <b>false</b>.
1250 Otherwise, the values of the operands are compared.
1251 Strings, numbers, and binary values are compared in the obvious way (by value).
1252
1253 <p>
1254 Tables
1255 are compared by reference:
1256 two objects are considered equal only if they are the same object.
1257 Every time you create a new table,
1258 it is different from any previously existing table.
1259 Closures are also compared by reference.
1260
1261 <p>
1262 You can change the way that Luan compares tables
1263 by using the "eq" metamethod (see <a href="#meta">Metatables and Metamethods</a>).
1264
1265 <p>
1266 Java values are compared for equality with the Java <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)"><code>equals</code></a> method.
1267
1268 <p>
1269 Equality comparisons do not convert strings to numbers
1270 or vice versa.
1271 Thus, <code>"0"==0</code> evaluates to <b>false</b>,
1272 and <code>t[0]</code> and <code>t["0"]</code> denote different
1273 entries in a table.
1274
1275
1276 <p>
1277 The operator <code>~=</code> is exactly the negation of equality (<code>==</code>).
1278
1279
1280 <p>
1281 The order operators work as follows.
1282
1283 If both arguments are numbers,
1284 then they are compared following
1285 the usual rule for binary operations.
1286 Otherwise, if both arguments are strings,
1287 then their values are compared according to the current locale.
1288 Otherwise, Luan tries to call the "lt" or the "le"
1289 metamethod (see <a href="#meta">Metatables and Metamethods</a>).
1290 A comparison <code>a &gt; b</code> is translated to <code>b &lt; a</code>
1291 and <code>a &gt;= b</code> is translated to <code>b &lt;= a</code>.
1292
1293
1294
1295
1296
1297 <h4 heading><a name="logical_ops">Logical Operators</a></h4>
1298
1299 <p>
1300 The logical operators in Luan are
1301 <b>and</b>, <b>or</b>, and <b>not</b>.
1302 The <b>and</b> and <b>or</b> operators consider both <b>false</b> and <b>nil</b> as false
1303 and anything else as true.
1304 Like the control structures (see <a href="#control">Control Structures</a>),
1305 the <b>not</b> operator requires a boolean value.
1306
1307 <p>
1308 The negation operator <b>not</b> always returns <b>false</b> or <b>true</b>.
1309 The conjunction operator <b>and</b> returns its first argument
1310 if this value is <b>false</b> or <b>nil</b>;
1311 otherwise, <b>and</b> returns its second argument.
1312 The disjunction operator <b>or</b> returns its first argument
1313 if this value is different from <b>nil</b> and <b>false</b>;
1314 otherwise, <b>or</b> returns its second argument.
1315 Both <b>and</b> and <b>or</b> use short-circuit evaluation;
1316 that is,
1317 the second operand is evaluated only if necessary.
1318 Here are some examples:
1319
1320 <pre>
1321 10 or 20 --&gt; 10
1322 10 or error() --&gt; 10
1323 nil or "a" --&gt; "a"
1324 nil and 10 --&gt; nil
1325 false and error() --&gt; false
1326 false and nil --&gt; false
1327 false or nil --&gt; nil
1328 10 and 20 --&gt; 20
1329 </pre>
1330
1331 <p>
1332 (In this manual,
1333 <code>--&gt;</code> indicates the result of the preceding expression.)
1334
1335
1336
1337 <h4 heading><a name="concatenation">Concatenation</a></h4>
1338
1339 <p>
1340 The string concatenation operator in Luan is
1341 denoted by two dots ('<code>..</code>').
1342 All operands are converted to strings.
1343
1344
1345
1346 <h4 heading><a name="length">The Length Operator</a></h4>
1347
1348 <p>
1349 The length operator is denoted by the unary prefix operator <code>#</code>.
1350 The length of a string is its number of characters.
1351 The length of a binary is its number of bytes.
1352
1353
1354 <p>
1355 A program can modify the behavior of the length operator for
1356 any table through the <code>__len</code> metamethod (see <a href="#meta">Metatables and Metamethods</a>).
1357
1358
1359 <p>
1360 Unless a <code>__len</code> metamethod is given,
1361 the length of a table <code>t</code> is defined
1362 as the number of elements in <em>sequence</em>,
1363 that is,
1364 the size of the set of its positive numeric keys is equal to <em>{1..n}</em>
1365 for some non-negative integer <em>n</em>.
1366 In that case, <em>n</em> is its length.
1367 Note that a table like
1368
1369 <pre>
1370 {10, 20, nil, 40}
1371 </pre>
1372
1373 <p>
1374 has a length of <code>2</code>, because that is the last key in sequence.
1375
1376
1377
1378
1379
1380 <h4 heading><a name="precedence">Precedence</a></h4>
1381
1382 <p>
1383 Operator precedence in Luan follows the table below,
1384 from lower to higher priority:
1385
1386 <pre>
1387 or
1388 and
1389 &lt; &gt; &lt;= &gt;= ~= ==
1390 ..
1391 + -
1392 * / %
1393 unary operators (not # -)
1394 ^
1395 </pre>
1396
1397 <p>
1398 As usual,
1399 you can use parentheses to change the precedences of an expression.
1400 The concatenation ('<code>..</code>') and exponentiation ('<code>^</code>')
1401 operators are right associative.
1402 All other binary operators are left associative.
1403
1404
1405
1406
1407
1408 <h4 heading><a name="constructors">Table Constructors</a></h4>
1409
1410 <p>
1411 Table constructors are expressions that create tables.
1412 Every time a constructor is evaluated, a new table is created.
1413 A constructor can be used to create an empty table
1414 or to create a table and initialize some of its fields.
1415 The general syntax for constructors is
1416
1417 <pre>
1418 tableconstructor ::= &lsquo;<b>{</b>&rsquo; fieldlist &lsquo;<b>}</b>&rsquo;
1419 fieldlist ::= [field] {fieldsep [field]}
1420 field ::= &lsquo;<b>[</b>&rsquo; exp &lsquo;<b>]</b>&rsquo; &lsquo;<b>=</b>&rsquo; exp | Name &lsquo;<b>=</b>&rsquo; exp | exp
1421 fieldsep ::= &lsquo;<b>,</b>&rsquo; | &lsquo;<b>;</b>&rsquo; | <b>end_of_line</b>
1422 </pre>
1423
1424 <p>
1425 Each field of the form <code>[exp1] = exp2</code> adds to the new table an entry
1426 with key <code>exp1</code> and value <code>exp2</code>.
1427 A field of the form <code>name = exp</code> is equivalent to
1428 <code>["name"] = exp</code>.
1429 Finally, fields of the form <code>exp</code> are equivalent to
1430 <code>[i] = exp</code>, where <code>i</code> are consecutive integers
1431 starting with 1.
1432 Fields in the other formats do not affect this counting.
1433 For example,
1434
1435 <pre>
1436 a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 }
1437 </pre>
1438
1439 <p>
1440 is equivalent to
1441
1442 <pre>
1443 do
1444 local t = {}
1445 t[f(1)] = g
1446 t[1] = "x" -- 1st exp
1447 t[2] = "y" -- 2nd exp
1448 t.x = 1 -- t["x"] = 1
1449 t[3] = f(x) -- 3rd exp
1450 t[30] = 23
1451 t[4] = 45 -- 4th exp
1452 a = t
1453 end
1454 </pre>
1455
1456 <p>
1457 The order of the assignments in a constructor is undefined.
1458 (This order would be relevant only when there are repeated keys.)
1459
1460
1461 <p>
1462 If the last field in the list has the form <code>exp</code>
1463 and the expression is a function call or a vararg expression,
1464 then all values returned by this expression enter the list consecutively
1465 (see <a href="#fn_calls">Function Calls</a>).
1466
1467
1468 <p>
1469 The field list can have an optional trailing separator,
1470 as a convenience for machine-generated code.
1471
1472
1473
1474
1475
1476 <h4 heading><a name="fn_calls">Function Calls</a></h4>
1477
1478 <p>
1479 A function call in Luan has the following syntax:
1480
1481 <pre>
1482 functioncall ::= prefixexp args
1483 </pre>
1484
1485 <p>
1486 In a function call,
1487 first prefixexp and args are evaluated.
1488 The value of prefixexp must have type <em>function</em>.
1489 This function is called
1490 with the given arguments.
1491
1492
1493 <p>
1494 Arguments have the following syntax:
1495
1496 <pre>
1497 args ::= &lsquo;<b>(</b>&rsquo; [explist] &lsquo;<b>)</b>&rsquo;
1498 args ::= tableconstructor
1499 args ::= LiteralString
1500 </pre>
1501
1502 <p>
1503 All argument expressions are evaluated before the call.
1504 A call of the form <code>f{<em>fields</em>}</code> is
1505 syntactic sugar for <code>f({<em>fields</em>})</code>;
1506 that is, the argument list is a single new table.
1507 A call of the form <code>f'<em>string</em>'</code>
1508 (or <code>f"<em>string</em>"</code> or <code>f[[<em>string</em>]]</code>)
1509 is syntactic sugar for <code>f('<em>string</em>')</code>;
1510 that is, the argument list is a single literal string.
1511
1512
1513
1514
1515 <h4 heading><a name="fn_def">Function Definitions</a></h4>
1516
1517 <p>
1518 The syntax for function definition is
1519
1520 <pre>
1521 functiondef ::= <b>function</b> funcbody
1522 funcbody ::= &lsquo;<b>(</b>&rsquo; [parlist] &lsquo;<b>)</b>&rsquo; block end_function
1523 end_function ::= <b>end_function</b> | <b>end</b>
1524 </pre>
1525
1526 <p>
1527 The following syntactic sugar simplifies function definitions:
1528
1529 <pre>
1530 stat ::= <b>function</b> funcname funcbody
1531 stat ::= <b>local</b> <b>function</b> Name funcbody
1532 funcname ::= Name {&lsquo;<b>.</b>&rsquo; Name} [&lsquo;<b>:</b>&rsquo; Name]
1533 </pre>
1534
1535 <p>
1536 The statement
1537
1538 <pre>
1539 function f () <em>body</em> end
1540 </pre>
1541
1542 <p>
1543 translates to
1544
1545 <pre>
1546 f = function () <em>body</em> end
1547 </pre>
1548
1549 <p>
1550 The statement
1551
1552 <pre>
1553 function t.a.b.c.f () <em>body</em> end
1554 </pre>
1555
1556 <p>
1557 translates to
1558
1559 <pre>
1560 t.a.b.c.f = function () <em>body</em> end
1561 </pre>
1562
1563 <p>
1564 The statement
1565
1566 <pre>
1567 local function f () <em>body</em> end
1568 </pre>
1569
1570 <p>
1571 translates to
1572
1573 <pre>
1574 local f; f = function () <em>body</em> end
1575 </pre>
1576
1577 <p>
1578 not to
1579
1580 <pre>
1581 local f = function () <em>body</em> end
1582 </pre>
1583
1584 <p>
1585 (This only makes a difference when the body of the function
1586 contains references to <code>f</code>.)
1587
1588
1589 <p>
1590 A function definition is an executable expression,
1591 whose value has type <em>function</em>.
1592 When Luan precompiles a chunk,
1593 all its function bodies are precompiled too.
1594 Then, whenever Luan executes the function definition,
1595 the function is <em>instantiated</em> (or <em>closed</em>).
1596 This function instance (or <em>closure</em>)
1597 is the final value of the expression.
1598
1599
1600 <p>
1601 Parameters act as local variables that are
1602 initialized with the argument values:
1603
1604 <pre>
1605 parlist ::= namelist [&lsquo;<b>,</b>&rsquo; &lsquo;<b>...</b>&rsquo;] | &lsquo;<b>...</b>&rsquo;
1606 </pre>
1607
1608 <p>
1609 When a function is called,
1610 the list of arguments is adjusted to
1611 the length of the list of parameters if the list is too short,
1612 unless the function is a <em>vararg function</em>,
1613 which is indicated by three dots ('<code>...</code>')
1614 at the end of its parameter list.
1615 A vararg function does not adjust its argument list;
1616 instead, it collects all extra arguments and supplies them
1617 to the function through a <em>vararg expression</em>,
1618 which is also written as three dots.
1619 The value of this expression is a list of all actual extra arguments,
1620 similar to a function with multiple results.
1621 If a vararg expression is used inside another expression
1622 or in the middle of a list of expressions,
1623 then its return list is adjusted to one element.
1624 If the expression is used as the last element of a list of expressions,
1625 then no adjustment is made
1626 (unless that last expression is enclosed in parentheses).
1627
1628
1629 <p>
1630 As an example, consider the following definitions:
1631
1632 <pre>
1633 function f(a, b) end
1634 function g(a, b, ...) end
1635 function r() return 1,2,3 end
1636 </pre>
1637
1638 <p>
1639 Then, we have the following mapping from arguments to parameters and
1640 to the vararg expression:
1641
1642 <pre>
1643 CALL PARAMETERS
1644
1645 f(3) a=3, b=nil
1646 f(3, 4) a=3, b=4
1647 f(3, 4, 5) runtime error
1648 f(r(), 10) runtime error
1649 f(r()) runtime error
1650
1651 g(3) a=3, b=nil, ... --&gt; (nothing)
1652 g(3, 4) a=3, b=4, ... --&gt; (nothing)
1653 g(3, 4, 5, 8) a=3, b=4, ... --&gt; 5 8
1654 g(5, r()) a=5, b=1, ... --&gt; 2 3
1655 </pre>
1656
1657 <p>
1658 Results are returned using the <b>return</b> statement (see <a href="#control">Control Structures</a>).
1659 If control reaches the end of a function
1660 without encountering a <b>return</b> statement,
1661 then the function returns with no results.
1662
1663
1664 <h4 heading><a name="template_expr">Template Expressions</a></h4>
1665
1666 <p>Luan template expression are based on <a href="http://en.wikipedia.org/wiki/JavaServer_Pages">JSP</a>. Template expressions return multiple values. Here is an example:</p>
1667
1668 <pre>
1669 local name = "Bob"
1670 write( %>Hello <%= name %>!<% )
1671 </pre>
1672
1673 <p>This is equivalent to the code:</p>
1674
1675 <pre>
1676 local name = "Bob"
1677 write( "Hello ", name, "!" )
1678 </pre>
1679
1680 <p>The strings in template expressions may be multiple lines.</p>
1681
1682
1683
1684 <h3 heading><a name="visibility">Visibility Rules</a></h3>
1685
1686 <p>
1687 Luan is a lexically scoped language.
1688 The scope of a local variable begins at the first statement after
1689 its declaration and lasts until the last non-void statement
1690 of the innermost block that includes the declaration.
1691 Consider the following example:
1692
1693 <pre>
1694 x = 10 -- global variable
1695 do -- new block
1696 local x = x -- new 'x', with value 10
1697 print(x) --&gt; 10
1698 x = x+1
1699 do -- another block
1700 local x = x+1 -- another 'x'
1701 print(x) --&gt; 12
1702 end
1703 print(x) --&gt; 11
1704 end
1705 print(x) --&gt; 10 (the global one)
1706 </pre>
1707
1708 <p>
1709 Notice that, in a declaration like <code>local x = x</code>,
1710 the new <code>x</code> being declared is not in scope yet,
1711 and so the second <code>x</code> refers to the outside variable.
1712
1713
1714 <p>
1715 Because of the lexical scoping rules,
1716 local variables can be freely accessed by functions
1717 defined inside their scope.
1718 A local variable used by an inner function is called
1719 an <em>upvalue</em>, or <em>external local variable</em>,
1720 inside the inner function.
1721
1722
1723 <p>
1724 Notice that each execution of a <b>local</b> statement
1725 defines new local variables.
1726 Consider the following example:
1727
1728 <pre>
1729 a = {}
1730 local x = 20
1731 for i=1,10 do
1732 local y = 0
1733 a[i] = function () y=y+1; return x+y end
1734 end
1735 </pre>
1736
1737 <p>
1738 The loop creates ten closures
1739 (that is, ten instances of the anonymous function).
1740 Each of these closures uses a different <code>y</code> variable,
1741 while all of them share the same <code>x</code>.
1742
1743
1744
1745
1746
1747 <h2 heading><a name="libs">Standard Libraries</a></h2>
1748
1749 <p>
1750 The standard Luan libraries provide useful functions
1751 that are implemented both in Java and in Luan itself.
1752 How each function is implemented shouldn't matter to the user.
1753 Some of these functions provide essential services to the language
1754 (e.g., <a href="#Luan.type"><code>type</code></a> and <a href="#Luan.get_metatable"><code>get_metatable</code></a>);
1755 others provide access to "outside" services (e.g., I/O).
1756
1757
1758 <h3 heading><a name="default_lib">Default Environment</a></h3>
1759
1760 <p>
1761 These are provided by default as local variables for any Luan code as described in <a href="#env">Environments</a>.
1762
1763
1764 <h4 heading><a name="require"><code>java ()</code></a></h4>
1765
1766 <p>
1767 This function 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.
1768
1769
1770 <h4 heading><a name="require"><code>require (mod_uri)</code></a></h4>
1771
1772 <p>
1773 Example use:
1774
1775 <pre>
1776 local Table = require "luan:Table.luan"
1777 </pre>
1778
1779 <p>
1780 Could be defined as:
1781
1782 <pre>
1783 local function require(mod_name)
1784 return <a href="#Package.load">Package.load</a>(mod_name) or <a href="#Luan.error">Luan.error</a>("module '"..mod_name.."' not found")
1785 end
1786 </pre>
1787
1788
1789 <h3 heading><a name="luan_lib">Basic Functions</a></h3>
1790
1791 <p>
1792 Include this library by:
1793
1794 <pre>
1795 local Luan = require "luan:Luan.luan"
1796 </pre>
1797
1798 <p>
1799 The basic library provides basic functions to Luan that don't depend on other libaries.
1800
1801
1802 <h4 heading><a name="Luan.do_file" href="#Luan.do_file"><code>Luan.do_file ([uri])</code></a></h4>
1803
1804 <p>
1805 Could be defined as:
1806
1807 <pre>
1808 function Luan.do_file(uri)
1809 local fn = <a href="#Luan.load_file">Luan.load_file</a>(uri) or <a href="#Luan.error">Luan.error</a>("file '"..uri.."' not found")
1810 return fn()
1811 end
1812 </pre>
1813
1814
1815
1816 <h4 heading><a name="Luan.error" href="#Luan.error"><code>Luan.error (message)</code></a></h4>
1817
1818 <p>
1819 Throws an error containing the message.
1820
1821 <p>
1822 Could be defined as:
1823
1824 <pre>
1825 function Luan.error(message)
1826 <a href="#Luan.new_error">Luan.new_error</a>(message).throw()
1827 end
1828 </pre>
1829
1830
1831
1832 <h4 heading><a name="Luan.eval" href="#Luan.eval"><code>Luan.eval (text [, source_name])</code></a></h4>
1833
1834 <p>
1835 Evaluates <code>text</code> as a Luan expression.
1836
1837 <p>
1838 Could be defined as:
1839
1840 <pre>
1841 function Luan.eval(text,source_name)
1842 return Luan.load( "return "..text, source_name or "eval" )()
1843 end
1844 </pre>
1845
1846
1847
1848 <h4 heading><a name="Luan.get_metatable" href="#Luan.get_metatable"><code>Luan.get_metatable (table)</code></a></h4>
1849
1850 <p>
1851 If <code>table</code> does not have a metatable, returns <b>nil</b>.
1852 Otherwise,
1853 if the table's metatable has a <code>"__metatable"</code> field,
1854 returns the associated value.
1855 Otherwise, returns the metatable of the given table.
1856
1857
1858 <h4 heading><a name="Luan.ipairs" href="#Luan.ipairs"><code>Luan.ipairs (t)</code></a></h4>
1859
1860 <p>
1861 Returns an iterator function
1862 so that the construction
1863
1864 <pre>
1865 for i,v in ipairs(t) do <em>body</em> end
1866 </pre>
1867
1868 <p>
1869 will iterate over the key&ndash;value pairs
1870 (<code>1,t[1]</code>), (<code>2,t[2]</code>), ...,
1871 up to the first nil value.
1872
1873 <p>
1874 Could be defined as:
1875
1876 <pre>
1877 function Luan.ipairs(t)
1878 local i = 0
1879 return function()
1880 if i < #t then
1881 i = i + 1
1882 return i, t[i]
1883 end
1884 end
1885 end
1886 </pre>
1887
1888
1889
1890 <h4 heading><a name="Luan.load" href="#Luan.load"><code>Luan.load (text, source_name [, env])</code></a></h4>
1891
1892 <p>
1893 Loads a chunk.
1894
1895 <p>
1896 The <code>text</code> is compiled.
1897 If there are no syntactic errors,
1898 returns the compiled chunk as a function;
1899 otherwise, throws an error.
1900
1901 <p>
1902 The <code>source_name</code> parameter is a string saying where the text came from. It is used to produce error messages.
1903
1904 <p>
1905 If the <code>env</code> parameter is supplied, it becomes the <code>_ENV</code> of the chunk.
1906
1907
1908 <h4 heading><a name="Luan.load_file" href="#Luan.load_file"><code>Luan.load_file ([file_uri])</code></a></h4>
1909
1910 <p>
1911 Similar to <a href="#Luan.load"><code>load</code></a>,
1912 but gets the chunk from file <code>file_uri</code>
1913 or from the standard input,
1914 if no file uri is given.
1915
1916 <p>
1917 Could be defined as:
1918
1919 <pre>
1920 function Luan.load_file(file_uri)
1921 file_uri = file_uri or "stdin:"
1922 local f = Io.uri(file_uri)
1923 if not f.exists() then
1924 return nil
1925 end
1926 return <a href="#Luan.load">Luan.load</a>( f.read_text(), file_uri )
1927 end
1928 </pre>
1929
1930
1931 <h4 heading><a name="Luan.new_error" href="#Luan.new_error"><code>Luan.new_error (message)</code></a></h4>
1932
1933 <p>
1934 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.
1935
1936 <p>
1937 To print the current stack trace, you could do:
1938
1939 <pre>
1940 Io.print( Luan.new_error "stack" )
1941 </pre>
1942
1943
1944 <h4 heading><a name="Luan.pairs" href="#Luan.pairs"><code>Luan.pairs (t)</code></a></h4>
1945
1946 <p>
1947 If <code>t</code> has a metamethod <code>__pairs</code>,
1948 calls it with <code>t</code> as argument and returns the
1949 result from the call.
1950
1951
1952 <p>
1953 Otherwise,
1954 returns a function
1955 so that the construction
1956
1957 <pre>
1958 for k,v in pairs(t) do <em>body</em> end
1959 </pre>
1960
1961 <p>
1962 will iterate over all key&ndash;value pairs of table <code>t</code>.
1963
1964
1965
1966 <h4 heading><a name="Luan.pcall" href="#Luan.pcall"><code>Luan.pcall (f [, arg1, &middot;&middot;&middot;])</code></a></h4>
1967
1968 <p>
1969 Calls function <code>f</code> with
1970 the given arguments in <em>protected mode</em>.
1971 This means that any error inside&nbsp;<code>f</code> is not propagated;
1972 instead, <code>pcall</code> catches the error
1973 and returns a status code.
1974 Its first result is the status code (a boolean),
1975 which is true if the call succeeds without errors.
1976 In such case, <code>pcall</code> also returns all results from the call,
1977 after this first result.
1978 In case of any error, <code>pcall</code> returns <b>false</b> plus the error.
1979
1980
1981
1982
1983 <p>
1984 <hr><h3><a name="pdf-print"><code>print (&middot;&middot;&middot;)</code></a></h3>
1985 Receives any number of arguments
1986 and prints their values to <code>stdout</code>,
1987 using the <a href="#pdf-tostring"><code>tostring</code></a> function to convert each argument to a string.
1988 <code>print</code> is not intended for formatted output,
1989 but only as a quick way to show a value,
1990 for instance for debugging.
1991 For complete control over the output,
1992 use <a href="#pdf-string.format"><code>string.format</code></a> and <a href="#pdf-io.write"><code>io.write</code></a>.
1993
1994
1995
1996 <h4 heading><a name="Luan.range" href="#Luan.range"><code>Luan.range (start, stop [, step])</code></a></h4>
1997
1998 <p>
1999 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.
2000
2001 <p>
2002 Example use:
2003
2004 <pre>
2005 for i in range(1,10) do
2006 Io.print("count up:",i)
2007 end
2008 for i in range(10,0,-1) do
2009 Io.print("count down:",i)
2010 end
2011 </pre>
2012
2013 <p>
2014 Could be defined as:
2015
2016 <pre>
2017 function Luan.range(start, stop, step)
2018 step = step or 1
2019 step == 0 and <a href="#Luan.error">Luan.error</a> "bad argument #3 (step may not be zero)"
2020 local i = start
2021 return function()
2022 if step > 0 and i <= stop or step < 0 and i >= stop then
2023 local rtn = i
2024 i = i + step
2025 return rtn
2026 end
2027 end
2028 end
2029 </pre>
2030
2031
2032
2033 <h4 heading><a name="Luan.raw_equal" href="#Luan.raw_equal"><code>Luan.raw_equal (v1, v2)</code></a></h4>
2034
2035 <p>
2036 Checks whether <code>v1</code> is equal to <code>v2</code>,
2037 without invoking any metamethod.
2038 Returns a boolean.
2039
2040
2041
2042 <h4 heading><a name="Luan.raw_get" href="#Luan.raw_get"><code>Luan.raw_get (table, index)</code></a></h4>
2043
2044 <p>
2045 Gets the real value of <code>table[index]</code>,
2046 without invoking any metamethod.
2047 <code>table</code> must be a table;
2048 <code>index</code> may be any value.
2049
2050
2051
2052 <h4 heading><a name="Luan.raw_len" href="#Luan.raw_len"><code>Luan.raw_len (v)</code></a></h4>
2053
2054 <p>
2055 Returns the length of the object <code>v</code>,
2056 which must be a table or a string,
2057 without invoking any metamethod.
2058 Returns an integer.
2059
2060
2061
2062 <h4 heading><a name="Luan.raw_set" href="#Luan.raw_set"><code>Luan.raw_set (table, index, value)</code></a></h4>
2063
2064 <p>
2065 Sets the real value of <code>table[index]</code> to <code>value</code>,
2066 without invoking any metamethod.
2067 <code>table</code> must be a table,
2068 <code>index</code> any value different from <b>nil</b>,
2069 and <code>value</code> any Lua value.
2070
2071
2072 <h4 heading><a name="Luan.set_metatable" href="#Luan.set_metatable"><code>Luan.set_metatable (table, metatable)</code></a></h4>
2073
2074 <p>
2075 Sets the metatable for the given table.
2076 If <code>metatable</code> is <b>nil</b>,
2077 removes the metatable of the given table.
2078 If the original metatable has a <code>"__metatable"</code> field,
2079 raises an error.
2080
2081
2082 <h4 heading><a name="Luan.stringify" href="#Luan.stringify"><code>Luan.stringify (v [,strict])</code></a></h4>
2083
2084 <p>
2085 Receives a value of any type and converts it to a string that is a Luan expression. <code>strict</code> is a string. If <code>strict</code> is 'strict' then invalid types throw an error. If <code>strict</code> is nil then invalid types are represented but the resulting expression is invalid. <code>strict</code> defaults to <b>nil</b>.
2086
2087
2088 <h4 heading><a name="Luan.to_string" href="#Luan.to_string"><code>Luan.to_string (v)</code></a></h4>
2089
2090 <p>
2091 Receives a value of any type and
2092 converts it to a string in a human-readable format.
2093
2094 <p>
2095 If the metatable of <code>v</code> has a <code>"__to_string"</code> field,
2096 then <code>to_string</code> calls the corresponding value
2097 with <code>v</code> as argument,
2098 and uses the result of the call as its result.
2099
2100
2101
2102 <h4 heading><a name="Luan.try" href="#Luan.try"><code>Luan.try (t, &middot;&middot;&middot;)</code></a></h4>
2103
2104 <p>
2105 Implements try-catch as found in other languages where each block is in table <code>t</code>. <code>t[1]</code> is the "try" block. The <code>t.catch</code> and <code>t.finally</code> blocks are optional. Any extra arguments are passed to the "try" function. Returns the result of the "try" block or the "catch" block.
2106
2107 <p>
2108 Example use:
2109
2110 <pre>
2111 try {
2112 function()
2113 a_dangerous_fn()
2114 end
2115 catch = function(e)
2116 -- handle error
2117 end
2118 finally = function()
2119 -- clean up
2120 end
2121 }
2122 </pre>
2123
2124 <p>
2125 Could be defined as:
2126
2127 <pre>
2128 function Luan.try(t)
2129 local r = { <a href="#Luan.pcall">Luan.pcall</a>(t[1]) }
2130 if r[1] then
2131 Table.remove(r,1)
2132 elseif t.catch ~= nil then
2133 r = { t.catch(r[2]) }
2134 else
2135 t.finally and t.finally()
2136 r[2].throw()
2137 end
2138 t.finally and t.finally()
2139 return Table.unpack(r)
2140 end
2141 </pre>
2142
2143
2144 <h4 heading><a name="Luan.type" href="#Luan.type"><code>Luan.type (v)</code></a></h4>
2145
2146 <p>
2147 Returns the type of its only argument, coded as a string.
2148 The possible results of this function are
2149 "<code>nil</code>" (a string, not the value <b>nil</b>),
2150 "<code>number</code>",
2151 "<code>string</code>",
2152 "<code>binary</code>",
2153 "<code>boolean</code>",
2154 "<code>table</code>",
2155 "<code>function</code>",
2156 and "<code>java</code>".
2157
2158
2159 <h4 heading><a name="Luan.values" href="#Luan.values"><code>Luan.values (&middot;&middot;&middot;)</code></a></h4>
2160
2161 <p>
2162 Returns a function so that the construction
2163
2164 <pre>
2165 for i, v in Luan.values(&middot;&middot;&middot;) do <em>body</em> end
2166 </pre>
2167
2168 <p>
2169 will iterate over all values of <code>&middot;&middot;&middot;</code>.
2170
2171
2172
2173 <h4 heading><a name="Luan.VERSION" href="#Luan.VERSION"><code>Luan.VERSION</code></a></h4>
2174
2175 <p>
2176 A global variable (not a function) that
2177 holds a string containing the current Luan version.
2178
2179
2180
2181
2182
2183
2184 <h3 heading><a name="package_lib">Modules</a></h3>
2185
2186 <p>
2187 Include this library by:
2188
2189 <pre>
2190 local Package = require "luan:Package.luan"
2191 </pre>
2192
2193 <p>
2194 The package library provides basic
2195 facilities for loading modules in Luan.
2196
2197
2198 <h4 heading><a name="Package.load" href="#Package.load"><code>Package.load (mod_uri)</code></a></h4>
2199
2200 <p>
2201 Loads the given module.
2202 The function starts by looking into the <a href="#Package.loaded"><code>Package.loaded</code></a> table
2203 to determine whether <code>mod_uri</code> is already loaded.
2204 If it is, then <code>Package.load</code> returns the value stored
2205 at <code>Package.loaded[mod_uri]</code>.
2206 Otherwise, it tries to load a new value for the module.
2207
2208 <p>
2209 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.
2210
2211 <p>
2212 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.
2213
2214 <p>
2215 If a new value for the module successful loaded, then it is stored in <code>Package.loaded[mod_uri]</code>. The value is returned.
2216
2217
2218
2219
2220 <h4 heading><a name="Package.loaded" href="#Package.loaded"><code>Package.loaded</code></a></h4>
2221
2222
2223 <p>
2224 A table used by <a href="#Package.load"><code>Package.load</code></a> to control which
2225 modules are already loaded.
2226 When you load a module <code>mod_uri</code> and
2227 <code>Package.loaded[mod_uri]</code> is not <b>nil</b>,
2228 <a href="#Package.load"><code>Package.load</code></a> simply returns the value stored there.
2229
2230
2231 <p>
2232 This variable is only a reference to the real table;
2233 assignments to this variable do not change the
2234 table used by <a href="#Package.load"><code>Package.load</code></a>.
2235
2236
2237
2238
2239
2240
2241 <h3 heading><a name="string_lib">String Manipulation</a></h3>
2242
2243 <p>
2244 Include this library by:
2245
2246 <pre>
2247 local String = require "luan:String.luan"
2248 </pre>
2249
2250 <p>
2251 This library provides generic functions for string manipulation,
2252 such as finding and extracting substrings, and pattern matching.
2253 When indexing a string in Luan, the first character is at position&nbsp;1
2254 (not at&nbsp;0, as in Java).
2255 Indices are allowed to be negative and are interpreted as indexing backwards,
2256 from the end of the string.
2257 Thus, the last character is at position -1, and so on.
2258
2259
2260
2261 <h4 heading><a name="String.char" href="#String.char"><code>String.char (&middot;&middot;&middot;)</code></a></h4>
2262
2263 <p>
2264 Receives zero or more integers.
2265 Returns a string with length equal to the number of arguments,
2266 in which each character has the internal numerical code equal
2267 to its corresponding argument.
2268
2269
2270 <h4 heading><a name="String.concat" href="#String.concat"><code>String.concat (&middot;&middot;&middot;)</code></a></h4>
2271
2272 <p>
2273 Concatenates the <a href="#Luan.to_string"><code>to_string</code></a> value of all arguments.
2274
2275
2276
2277 <h4 heading><a name="String.encode" href="#String.encode"><code>String.encode (s)</code></a></h4>
2278
2279 <p>
2280 Encodes argument <code>s</code> into a string that can be placed in quotes so as to return the original value of the string.
2281
2282
2283
2284
2285 <h4 heading><a name="String.find" href="#String.find"><code>String.find (s, pattern [, init [, plain]])</code></a></h4>
2286
2287 <p>
2288 Looks for the first match of
2289 <code>pattern</code> (see <a href="http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html">Pattern</a>) in the string <code>s</code>.
2290 If it finds a match, then <code>find</code> returns the indices of&nbsp;<code>s</code>
2291 where this occurrence starts and ends;
2292 otherwise, it returns <b>nil</b>.
2293 A third, optional numerical argument <code>init</code> specifies
2294 where to start the search;
2295 its default value is&nbsp;1 and can be negative.
2296 A value of <b>true</b> as a fourth, optional argument <code>plain</code>
2297 turns off the pattern matching facilities,
2298 so the function does a plain "find substring" operation,
2299 with no characters in <code>pattern</code> being considered magic.
2300 Note that if <code>plain</code> is given, then <code>init</code> must be given as well.
2301
2302 <p>
2303 If the pattern has captures,
2304 then in a successful match
2305 the captured values are also returned,
2306 after the two indices.
2307
2308
2309
2310
2311 <h4 heading><a name="String.format" href="#String.format"><code>String.format (formatstring, &middot;&middot;&middot;)</code></a></h4>
2312
2313
2314 <p>
2315 Returns a formatted version of its variable number of arguments
2316 following the description given in its first argument (which must be a string).
2317 The format string follows the same rules as the Java function <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#format(java.lang.String,%20java.lang.Object...)"><code>String.format</code></a> because Luan calls this internally.
2318
2319 <p>
2320 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.
2321
2322
2323
2324 <h4 heading><a name="String.gmatch" href="#String.gmatch"><code>String.gmatch (s, pattern)</code></a></h4>
2325
2326 <p>
2327 Returns an iterator function that,
2328 each time it is called,
2329 returns the next captures from <code>pattern</code> (see <a href="http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html">Pattern</a>)
2330 over the string <code>s</code>.
2331 If <code>pattern</code> specifies no captures,
2332 then the whole match is produced in each call.
2333
2334
2335 <p>
2336 As an example, the following loop
2337 will iterate over all the words from string <code>s</code>,
2338 printing one per line:
2339
2340 <pre>
2341 local s = "hello world from Lua"
2342 for w in String.gmatch(s, [[\w+]]) do
2343 print(w)
2344 end
2345 </pre>
2346
2347 <p>
2348 The next example collects all pairs <code>key=value</code> from the
2349 given string into a table:
2350
2351 <pre>
2352 local t = {}
2353 local s = "from=world, to=Lua"
2354 for k, v in String.gmatch(s, [[(\w+)=(\w+)]]) do
2355 t[k] = v
2356 end
2357 </pre>
2358
2359 <p>
2360 For this function, a caret '<code>^</code>' at the start of a pattern does not
2361 work as an anchor, as this would prevent the iteration.
2362
2363
2364
2365 <h4 heading><a name="String.gsub" href="#String.gsub"><code>String.gsub (s, pattern, repl [, n])</code></a></h4>
2366
2367 <p>
2368 Returns a copy of <code>s</code>
2369 in which all (or the first <code>n</code>, if given)
2370 occurrences of the <code>pattern</code> (see <a href="http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html">Pattern</a>) have been
2371 replaced by a replacement string specified by <code>repl</code>,
2372 which can be a string, a table, or a function.
2373 <code>gsub</code> also returns, as its second value,
2374 the total number of matches that occurred.
2375 The name <code>gsub</code> comes from <em>Global SUBstitution</em>.
2376
2377
2378 <p>
2379 If <code>repl</code> is a string, then its value is used for replacement.
2380 The character&nbsp;<code>\</code> works as an escape character.
2381 Any sequence in <code>repl</code> of the form <code>$<em>d</em></code>,
2382 with <em>d</em> between 1 and 9,
2383 stands for the value of the <em>d</em>-th captured substring.
2384 The sequence <code>$0</code> stands for the whole match.
2385
2386
2387 <p>
2388 If <code>repl</code> is a table, then the table is queried for every match,
2389 using the first capture as the key.
2390
2391
2392 <p>
2393 If <code>repl</code> is a function, then this function is called every time a
2394 match occurs, with all captured substrings passed as arguments,
2395 in order.
2396
2397
2398 <p>
2399 In any case,
2400 if the pattern specifies no captures,
2401 then it behaves as if the whole pattern was inside a capture.
2402
2403
2404 <p>
2405 If the value returned by the table query or by the function call
2406 is not <b>nil</b>,
2407 then it is used as the replacement string;
2408 otherwise, if it is <b>nil</b>,
2409 then there is no replacement
2410 (that is, the original match is kept in the string).
2411
2412
2413 <p>
2414 Here are some examples:
2415
2416 <pre>
2417 x = String.gsub("hello world", [[(\w+)]], "$1 $1")
2418 --&gt; x="hello hello world world"
2419
2420 x = String.gsub("hello world", [[\w+]], "$0 $0", 1)
2421 --&gt; x="hello hello world"
2422
2423 x = String.gsub("hello world from Luan", [[(\w+)\s*(\w+)]], "$2 $1")
2424 --&gt; x="world hello Luan from"
2425
2426 x = String.gsub("4+5 = $return 4+5$", [[\$(.*?)\$]], function (s)
2427 return load(s)()
2428 end)
2429 --&gt; x="4+5 = 9"
2430
2431 local t = {name="lua", version="5.3"}
2432 x = String.gsub("$name-$version.tar.gz", [[\$(\w+)]], t)
2433 --&gt; x="lua-5.3.tar.gz"
2434 </pre>
2435
2436
2437
2438 <h4 heading><a name="String.literal" href="#String.literal"><code>String.literal (s)</code></a></h4>
2439 <p>
2440 Returns a string which matches the literal string <code>s</code> in a regular expression. This function is simply the Java method <a href="http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#quote(java.lang.String)"><code>Pattern.quote</code></a>.
2441
2442
2443 <h4 heading><a name="String.lower" href="#String.lower"><code>String.lower (s)</code></a></h4>
2444 <p>
2445 Receives a string and returns a copy of this string with all
2446 uppercase letters changed to lowercase.
2447 All other characters are left unchanged.
2448
2449
2450
2451
2452 <h4 heading><a name="String.match" href="#String.match"><code>String.match (s, pattern [, init])</code></a></h4>
2453
2454 <p>
2455 Looks for the first <em>match</em> of
2456 <code>pattern</code> (see <a href="http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html">Pattern</a>) in the string <code>s</code>.
2457 If it finds one, then <code>match</code> returns
2458 the captures from the pattern;
2459 otherwise it returns <b>nil</b>.
2460 If <code>pattern</code> specifies no captures,
2461 then the whole match is returned.
2462 A third, optional numerical argument <code>init</code> specifies
2463 where to start the search;
2464 its default value is&nbsp;1 and can be negative.
2465
2466
2467 <h4 heading><a name="String.matches" href="#String.matches"><code>String.matches (s, pattern)</code></a></h4>
2468 <p>
2469 Returns a boolean indicating whether the <code>pattern</code> can be found in string <code>s</code>.
2470 This function is equivalent to
2471
2472 <pre>
2473 return String.match(s,pattern) ~= nil
2474 </pre>
2475
2476
2477 <h4 heading><a name="String.rep" href="#String.rep"><code>String.rep (s, n [, sep])</code></a></h4>
2478 <p>
2479 Returns a string that is the concatenation of <code>n</code> copies of
2480 the string <code>s</code> separated by the string <code>sep</code>.
2481 The default value for <code>sep</code> is the empty string
2482 (that is, no separator).
2483 Returns the empty string if <code>n</code> is not positive.
2484
2485
2486
2487
2488 <h4 heading><a name="String.reverse" href="#String.reverse"><code>String.reverse (s)</code></a></h4>
2489 <p>
2490 Returns a string that is the string <code>s</code> reversed.
2491
2492
2493
2494 <h4 heading><a name="String.split" href="#String.match"><code>String.split (s, pattern [, limit])</code></a></h4>
2495
2496 <p>
2497 Splits <code>s</code> using regex <code>pattern</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.
2498
2499
2500
2501 <h4 heading><a name="String.sub" href="#String.sub"><code>String.sub (s, i [, j])</code></a></h4>
2502
2503 <p>
2504 Returns the substring of <code>s</code> that
2505 starts at <code>i</code> and continues until <code>j</code>;
2506 <code>i</code> and <code>j</code> can be negative.
2507 If <code>j</code> is absent, then it is assumed to be equal to -1
2508 (which is the same as the string length).
2509 In particular,
2510 the call <code>string.sub(s,1,j)</code> returns a prefix of <code>s</code>
2511 with length <code>j</code>,
2512 and <code>string.sub(s, -i)</code> returns a suffix of <code>s</code>
2513 with length <code>i</code>.
2514
2515
2516 <p>
2517 If, after the translation of negative indices,
2518 <code>i</code> is less than 1,
2519 it is corrected to 1.
2520 If <code>j</code> is greater than the string length,
2521 it is corrected to that length.
2522 If, after these corrections,
2523 <code>i</code> is greater than <code>j</code>,
2524 the function returns the empty string.
2525
2526
2527
2528 <h4 heading><a name="String.to_binary" href="#String.to_binary"><code>String.to_binary (s)</code></a></h4>
2529
2530 <p>
2531 Converts a string to a binary by calling the Java method <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#getBytes()"><code>String.getBytes</code></a>.
2532
2533
2534
2535 <h4 heading><a name="String.to_number" href="#String.to_number"><code>String.to_number (s [, base])</code></a></h4>
2536
2537 <p>
2538 When called with no <code>base</code>,
2539 <code>to_number</code> tries to convert its argument to a number.
2540 If the argument is
2541 a string convertible to a number,
2542 then <code>to_number</code> returns this number;
2543 otherwise, it returns <b>nil</b>.
2544
2545 The conversion of strings can result in integers or floats.
2546
2547
2548 <p>
2549 When called with <code>base</code>,
2550 then <code>s</code> must be a string to be interpreted as
2551 an integer numeral in that base.
2552 In bases above&nbsp;10, the letter '<code>A</code>' (in either upper or lower case)
2553 represents&nbsp;10, '<code>B</code>' represents&nbsp;11, and so forth,
2554 with '<code>Z</code>' representing 35.
2555 If the string <code>s</code> is not a valid numeral in the given base,
2556 the function returns <b>nil</b>.
2557
2558
2559
2560 <h4 heading><a name="String.trim" href="#String.trim"><code>String.trim (s)</code></a></h4>
2561
2562 <p>
2563 Removes the leading and trailing whitespace by calling the Java method <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim()"><code>String.trim</code></a>.
2564
2565
2566
2567
2568 <h4 heading><a name="String.unicode" href="#String.unicode"><code>String.unicode (s [, i [, j]])</code></a></h4>
2569
2570 <p>
2571 Returns the internal numerical codes of the characters <code>s[i]</code>,
2572 <code>s[i+1]</code>, ..., <code>s[j]</code>.
2573 The default value for <code>i</code> is&nbsp;1;
2574 the default value for <code>j</code> is&nbsp;<code>i</code>.
2575 These indices are corrected
2576 following the same rules of function <a href="#String.sub"><code>String.sub</code></a>.
2577
2578
2579
2580
2581
2582 <h4 heading><a name="String.upper" href="#String.upper"><code>String.upper (s)</code></a></h4>
2583 <p>
2584 Receives a string and returns a copy of this string with all
2585 lowercase letters changed to uppercase.
2586 All other characters are left unchanged.
2587 The definition of what a lowercase letter is depends on the current locale.
2588
2589
2590
2591
2592
2593 <h3 heading><a name="binary_lib">Binary Manipulation</a></h3>
2594
2595 <p>
2596 Include this library by:
2597
2598 <pre>
2599 local Binary = require "luan:Binary.luan"
2600 </pre>
2601
2602
2603 <h4 heading><a name="Binary.binary" href="#Binary.binary"><code>Binary.binary (&middot;&middot;&middot;)</code></a></h4>
2604
2605 <p>
2606 Receives zero or more bytes (as integers).
2607 Returns a binary with length equal to the number of arguments,
2608 in which each byte has the internal numerical code equal
2609 to its corresponding argument.
2610
2611
2612 <h4 heading><a name="Binary.byte" href="#Binary.byte"><code>Binary.byte (b [, i [, j]])</code></a></h4>
2613
2614 <p>
2615 Returns the internal numerical codes of the bytes <code>b[i]</code>,
2616 <code>b[i+1]</code>, ..., <code>b[j]</code>.
2617 The default value for <code>i</code> is&nbsp;1;
2618 the default value for <code>j</code> is&nbsp;<code>i</code>.
2619 These indices are corrected
2620 following the same rules of function <a href="#String.sub"><code>String.sub</code></a>.
2621
2622
2623 <h4 heading><a name="Binary.to_string" href="#Binary.to_string"><code>Binary.to_string (b [,charset])</code></a></h4>
2624 <p>
2625 If <code>charset</code> is not nil then converts the binary <code>b</code> to a string using the Java <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#String(byte[],%20java.lang.String)">String constructor</a>, else makes each byte a char.
2626
2627
2628
2629
2630 <h3 heading><a name="table_lib">Table Manipulation</a></h3>
2631
2632 <p>
2633 Include this library by:
2634
2635 <pre>
2636 local Table = require "luan:Table.luan"
2637 </pre>
2638
2639 <p>
2640 This library provides generic functions for table manipulation.
2641 It provides all its functions inside the table <code>Table</code>.
2642
2643
2644
2645 <h4 heading><a name="Table.clear" href="#Table.clear"><code>Table.clear (tbl)</code></a></h4>
2646
2647 <p>
2648 Clears the table.
2649
2650
2651 <h4 heading><a name="Table.concat" href="#Table.concat"><code>Table.concat (list [, sep [, i [, j]]])</code></a></h4>
2652
2653 <p>
2654 Given a list,
2655 returns the string <code>list[i]..sep..list[i+1] &middot;&middot;&middot; sep..list[j]</code>.
2656 The default value for <code>sep</code> is the empty string,
2657 the default for <code>i</code> is 1,
2658 and the default for <code>j</code> is <code>#list</code>.
2659 If <code>i</code> is greater than <code>j</code>, returns the empty string.
2660
2661
2662 <h4 heading><a name="Table.copy" href="#Table.copy"><code>Table.copy (tbl [, i [, j]])</code></a></h4>
2663
2664 <p>
2665 If <code>i</code> is <code>nil</code>, returns a shallow copy of <code>tbl</code>.
2666 Otherwise returns a new table which is a list of the elements <code>tbl[i] &middot;&middot;&middot; tbl[j]</code>.
2667 By default, <code>j</code> is <code>#tbl</code>.
2668
2669
2670
2671 <h4 heading><a name="Table.insert" href="#Table.insert"><code>Table.insert (list, pos, value)</code></a></h4>
2672
2673 <p>
2674 Inserts element <code>value</code> at position <code>pos</code> in <code>list</code>,
2675 shifting up the elements
2676 <code>list[pos], list[pos+1], &middot;&middot;&middot;, list[#list]</code>.
2677
2678
2679
2680 <h4 heading><a name="Table.is_empty" href="#Table.is_empty"><code>Table.is_empty (tbl)</code></a></h4>
2681
2682
2683
2684 <h4 heading><a name="Table.pack" href="#Table.pack"><code>Table.pack (&middot;&middot;&middot;)</code></a></h4>
2685
2686 <p>
2687 Returns a new table with all parameters stored into keys 1, 2, etc.
2688 and with a field "<code>n</code>" with the total number of parameters.
2689 Note that the resulting table may not be a sequence.
2690
2691
2692
2693
2694 <h4 heading><a name="Table.remove" href="#Table.remove"><code>Table.remove (list, pos)</code></a></h4>
2695
2696
2697 <p>
2698 Removes from <code>list</code> the element at position <code>pos</code>,
2699 returning the value of the removed element.
2700 When <code>pos</code> is an integer between 1 and <code>#list</code>,
2701 it shifts down the elements
2702 <code>list[pos+1], list[pos+2], &middot;&middot;&middot;, list[#list]</code>
2703 and erases element <code>list[#list]</code>;
2704 The index <code>pos</code> can also be 0 when <code>#list</code> is 0,
2705 or <code>#list + 1</code>;
2706 in those cases, the function erases the element <code>list[pos]</code>.
2707
2708
2709
2710 <h4 heading><a name="Table.size" href="#Table.size"><code>Table.size (tbl)</code></a></h4>
2711
2712
2713
2714 <h4 heading><a name="Table.sort" href="#Table.sort"><code>Table.sort (list [, comp])</code></a></h4>
2715
2716 <p>
2717 Sorts list elements in a given order, <em>in-place</em>,
2718 from <code>list[1]</code> to <code>list[#list]</code>.
2719 If <code>comp</code> is given,
2720 then it must be a function that receives two list elements
2721 and returns true when the first element must come
2722 before the second in the final order
2723 (so that <code>not comp(list[i+1],list[i])</code> will be true after the sort).
2724 If <code>comp</code> is not given,
2725 then the standard Lua operator <code>&lt;</code> is used instead.
2726
2727 <p>
2728 The sort algorithm is not stable;
2729 that is, elements considered equal by the given order
2730 may have their relative positions changed by the sort.
2731
2732
2733
2734 <h4 heading><a name="Table.unpack" href="#Table.unpack"><code>Table.unpack (list [, i [, j]])</code></a></h4>
2735
2736 <p>
2737 Returns the elements from the given list.
2738 This function is equivalent to
2739
2740 <pre>
2741 return list[i], list[i+1], &middot;&middot;&middot;, list[j]
2742 </pre>
2743
2744 <p>
2745 By default, <code>i</code> is 1 and <code>j</code> is <code>list.n or #list</code>.
2746
2747
2748
2749
2750 <h3 heading><a name="number_lib">Number Manipulation</a></h3>
2751
2752 <p>
2753 Include this library by:
2754
2755 <pre>
2756 local Number = require "luan:Number.luan"
2757 </pre>
2758
2759
2760 <h4 heading><a name="Number.double" href="#Number.double"><code>Number.double (x)</code></a></h4>
2761 <p>
2762 Returns <code>x</code> as a double.
2763
2764
2765 <h4 heading><a name="Number.integer" href="#Number.integer"><code>Number.integer (x)</code></a></h4>
2766 <p>
2767 If the value <code>x</code> is convertible to an integer,
2768 returns that integer.
2769 Otherwise throws an error.
2770
2771
2772 <h4 heading><a name="Number.long" href="#Number.long"><code>Number.long (x)</code></a></h4>
2773 <p>
2774 If the value <code>x</code> is convertible to an long,
2775 returns that long.
2776 Otherwise throws an error.
2777
2778
2779 <h4 heading><a name="Number.long_to_string" href="#Number.long_to_string"><code>Number.long_to_string (i, radix)</code></a></h4>
2780 <p>
2781 Converts long value <code>i</code> to a string by calling <code><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Long.html#toString(long,%20int)">Long.toString</a></code>.
2782
2783
2784 <h4 heading><a name="Number.type" href="#Number.type"><code>Number.type (x)</code></a></h4>
2785 <p>
2786 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>".
2787
2788
2789
2790
2791 <h3 heading><a name="math_lib">Mathematical Functions</a></h3>
2792
2793 <p>
2794 Include this library by:
2795
2796 <pre>
2797 local Math = require "luan:Math.luan"
2798 </pre>
2799
2800 <p>
2801 This library provides basic mathematical functions.
2802 It provides all its functions and constants inside the table <code>Math</code>.
2803
2804
2805 <h4 heading><a name="Math.abs" href="#Math.abs"><code>Math.abs (x)</code></a></h4>
2806
2807 <p>
2808 Returns the absolute value of <code>x</code>.
2809
2810
2811
2812 <h4 heading><a name="Math.acos" href="#Math.acos"><code>Math.acos (x)</code></a></h4>
2813
2814 <p>
2815 Returns the arc cosine of <code>x</code> (in radians).
2816
2817
2818
2819
2820 <h4 heading><a name="Math.asin" href="#Math.asin"><code>Math.asin (x)</code></a></h4>
2821
2822 <p>
2823 Returns the arc sine of <code>x</code> (in radians).
2824
2825
2826
2827
2828 <h4 heading><a name="Math.atan" href="#Math.atan"><code>Math.atan (y, x)</code></a></h4>
2829
2830 <p>
2831 Returns the arc tangent of <code>y/x</code> (in radians),
2832 but uses the signs of both parameters to find the
2833 quadrant of the result.
2834 (It also handles correctly the case of <code>x</code> being zero.)
2835
2836
2837
2838
2839 <h4 heading><a name="Math.ceil" href="#Math.ceil"><code>Math.ceil (x)</code></a></h4>
2840
2841 <p>
2842 Returns the smallest integral value larger than or equal to <code>x</code>.
2843
2844
2845
2846
2847 <h4 heading><a name="Math.cos" href="#Math.cos"><code>Math.cos (x)</code></a></h4>
2848
2849 <p>
2850 Returns the cosine of <code>x</code> (assumed to be in radians).
2851
2852
2853
2854
2855 <h4 heading><a name="Math.deg" href="#Math.deg"><code>Math.deg (x)</code></a></h4>
2856
2857 <p>
2858 Converts the angle <code>x</code> from radians to degrees.
2859
2860
2861
2862
2863 <h4 heading><a name="Math.exp" href="#Math.exp"><code>Math.exp (x)</code></a></h4>
2864
2865 <p>
2866 Returns the value <em>e<sup>x</sup></em>
2867 (where <code>e</code> is the base of natural logarithms).
2868
2869
2870
2871
2872 <h4 heading><a name="Math.floor" href="#Math.floor"><code>Math.floor (x)</code></a></h4>
2873
2874 <p>
2875 Returns the largest integral value smaller than or equal to <code>x</code>.
2876
2877
2878
2879
2880 <h4 heading><a name="Math.fmod" href="#Math.fmod"><code>Math.fmod (x, y)</code></a></h4>
2881
2882 <p>
2883 Returns the remainder of the division of <code>x</code> by <code>y</code>
2884 that rounds the quotient towards zero.
2885
2886
2887
2888
2889 <h4 heading><a name="Math.huge" href="#Math.huge"><code>Math.huge</code></a></h4>
2890
2891 <p>
2892 A value larger than any other numerical value.
2893
2894
2895
2896
2897 <h4 heading><a name="Math.log" href="#Math.log"><code>Math.log (x [, base])</code></a></h4>
2898
2899 <p>
2900 Returns the logarithm of <code>x</code> in the given base.
2901 The default for <code>base</code> is <em>e</em>
2902 (so that the function returns the natural logarithm of <code>x</code>).
2903
2904
2905
2906
2907 <h4 heading><a name="Math.max" href="#Math.max"><code>Math.max (x, &middot;&middot;&middot;)</code></a></h4>
2908
2909 <p>
2910 Returns the argument with the maximum value,
2911 according to the Lua operator <code>&lt;</code>.
2912
2913
2914
2915
2916 <h4 heading><a name="Math.max_integer" href="#Math.max_integer"><code>Math.max_integer</code></a></h4>
2917 <p>
2918 An integer with the maximum value for an integer.
2919
2920
2921
2922
2923 <h4 heading><a name="Math.min" href="#Math.min"><code>Math.min (x, &middot;&middot;&middot;)</code></a></h4>
2924
2925 <p>
2926 Returns the argument with the minimum value,
2927 according to the Lua operator <code>&lt;</code>.
2928
2929
2930
2931
2932 <h4 heading><a name="Math.min_integer" href="#Math.min_integer"><code>Math.min_integer</code></a></h4>
2933 <p>
2934 An integer with the minimum value for an integer.
2935
2936
2937
2938
2939 <h4 heading><a name="Math.modf" href="#Math.modf"><code>Math.modf (x)</code></a></h4>
2940
2941 <p>
2942 Returns the integral part of <code>x</code> and the fractional part of <code>x</code>.
2943
2944
2945
2946
2947 <h4 heading><a name="Math.pi" href="#Math.pi"><code>Math.pi</code></a></h4>
2948
2949 <p>
2950 The value of <em>&pi;</em>.
2951
2952
2953
2954
2955 <h4 heading><a name="Math.rad" href="#Math.rad"><code>Math.rad (x)</code></a></h4>
2956
2957 <p>
2958 Converts the angle <code>x</code> from degrees to radians.
2959
2960
2961
2962
2963 <h4 heading><a name="Math.random" href="#Math.random"><code>Math.random ([m [, n])</code></a></h4>
2964
2965
2966 <p>
2967 When called without arguments,
2968 returns a pseudo-random float with uniform distribution
2969 in the range <em>[0,1)</em>.
2970 When called with two integers <code>m</code> and <code>n</code>,
2971 <code>Math.random</code> returns a pseudo-random integer
2972 with uniform distribution in the range <em>[m, n]</em>.
2973 (The value <em>m-n</em> cannot be negative and must fit in a Luan integer.)
2974 The call <code>Math.random(n)</code> is equivalent to <code>Math.random(1,n)</code>.
2975
2976
2977 <p>
2978 This function is an interface to the underling
2979 pseudo-random generator function provided by Java.
2980 No guarantees can be given for its statistical properties.
2981
2982
2983
2984
2985 <h4 heading><a name="Math.sin" href="#Math.sin"><code>Math.sin (x)</code></a></h4>
2986
2987 <p>
2988 Returns the sine of <code>x</code> (assumed to be in radians).
2989
2990
2991
2992
2993 <h4 heading><a name="Math.sqrt" href="#Math.sqrt"><code>Math.sqrt (x)</code></a></h4>
2994
2995 <p>
2996 Returns the square root of <code>x</code>.
2997 (You can also use the expression <code>x^0.5</code> to compute this value.)
2998
2999
3000
3001
3002 <h4 heading><a name="Math.tan" href="#Math.tan"><code>Math.tan (x)</code></a></h4>
3003
3004 <p>
3005 Returns the tangent of <code>x</code> (assumed to be in radians).
3006
3007
3008
3009
3010
3011
3012
3013
3014 <h2>6.8 &ndash; <a name="6.8">Input and Output Facilities</a></h2>
3015
3016 <p>
3017 The I/O library provides two different styles for file manipulation.
3018 The first one uses implicit file handles;
3019 that is, there are operations to set a default input file and a
3020 default output file,
3021 and all input/output operations are over these default files.
3022 The second style uses explicit file handles.
3023
3024
3025 <p>
3026 When using implicit file handles,
3027 all operations are supplied by table <a name="pdf-io"><code>io</code></a>.
3028 When using explicit file handles,
3029 the operation <a href="#pdf-io.open"><code>io.open</code></a> returns a file handle
3030 and then all operations are supplied as methods of the file handle.
3031
3032
3033 <p>
3034 The table <code>io</code> also provides
3035 three predefined file handles with their usual meanings from C:
3036 <a name="pdf-io.stdin"><code>io.stdin</code></a>, <a name="pdf-io.stdout"><code>io.stdout</code></a>, and <a name="pdf-io.stderr"><code>io.stderr</code></a>.
3037 The I/O library never closes these files.
3038
3039
3040 <p>
3041 Unless otherwise stated,
3042 all I/O functions return <b>nil</b> on failure
3043 (plus an error message as a second result and
3044 a system-dependent error code as a third result)
3045 and some value different from <b>nil</b> on success.
3046 On non-POSIX systems,
3047 the computation of the error message and error code
3048 in case of errors
3049 may be not thread safe,
3050 because they rely on the global C variable <code>errno</code>.
3051
3052
3053 <p>
3054 <hr><h3><a name="pdf-io.close"><code>io.close ([file])</code></a></h3>
3055
3056
3057 <p>
3058 Equivalent to <code>file:close()</code>.
3059 Without a <code>file</code>, closes the default output file.
3060
3061
3062
3063
3064 <p>
3065 <hr><h3><a name="pdf-io.flush"><code>io.flush ()</code></a></h3>
3066
3067
3068 <p>
3069 Equivalent to <code>io.output():flush()</code>.
3070
3071
3072
3073
3074 <p>
3075 <hr><h3><a name="pdf-io.input"><code>io.input ([file])</code></a></h3>
3076
3077
3078 <p>
3079 When called with a file name, it opens the named file (in text mode),
3080 and sets its handle as the default input file.
3081 When called with a file handle,
3082 it simply sets this file handle as the default input file.
3083 When called without parameters,
3084 it returns the current default input file.
3085
3086
3087 <p>
3088 In case of errors this function raises the error,
3089 instead of returning an error code.
3090
3091
3092
3093
3094 <p>
3095 <hr><h3><a name="pdf-io.lines"><code>io.lines ([filename &middot;&middot;&middot;])</code></a></h3>
3096
3097
3098 <p>
3099 Opens the given file name in read mode
3100 and returns an iterator function that
3101 works like <code>file:lines(&middot;&middot;&middot;)</code> over the opened file.
3102 When the iterator function detects the end of file,
3103 it returns no values (to finish the loop) and automatically closes the file.
3104
3105
3106 <p>
3107 The call <code>io.lines()</code> (with no file name) is equivalent
3108 to <code>io.input():lines("*l")</code>;
3109 that is, it iterates over the lines of the default input file.
3110 In this case it does not close the file when the loop ends.
3111
3112
3113 <p>
3114 In case of errors this function raises the error,
3115 instead of returning an error code.
3116
3117
3118
3119
3120 <p>
3121 <hr><h3><a name="pdf-io.open"><code>io.open (filename [, mode])</code></a></h3>
3122
3123
3124 <p>
3125 This function opens a file,
3126 in the mode specified in the string <code>mode</code>.
3127 It returns a new file handle,
3128 or, in case of errors, <b>nil</b> plus an error message.
3129
3130
3131 <p>
3132 The <code>mode</code> string can be any of the following:
3133
3134 <ul>
3135 <li><b>"<code>r</code>": </b> read mode (the default);</li>
3136 <li><b>"<code>w</code>": </b> write mode;</li>
3137 <li><b>"<code>a</code>": </b> append mode;</li>
3138 <li><b>"<code>r+</code>": </b> update mode, all previous data is preserved;</li>
3139 <li><b>"<code>w+</code>": </b> update mode, all previous data is erased;</li>
3140 <li><b>"<code>a+</code>": </b> append update mode, previous data is preserved,
3141 writing is only allowed at the end of file.</li>
3142 </ul><p>
3143 The <code>mode</code> string can also have a '<code>b</code>' at the end,
3144 which is needed in some systems to open the file in binary mode.
3145
3146
3147
3148
3149 <p>
3150 <hr><h3><a name="pdf-io.output"><code>io.output ([file])</code></a></h3>
3151
3152
3153 <p>
3154 Similar to <a href="#pdf-io.input"><code>io.input</code></a>, but operates over the default output file.
3155
3156
3157
3158
3159 <p>
3160 <hr><h3><a name="pdf-io.popen"><code>io.popen (prog [, mode])</code></a></h3>
3161
3162
3163 <p>
3164 This function is system dependent and is not available
3165 on all platforms.
3166
3167
3168 <p>
3169 Starts program <code>prog</code> in a separated process and returns
3170 a file handle that you can use to read data from this program
3171 (if <code>mode</code> is <code>"r"</code>, the default)
3172 or to write data to this program
3173 (if <code>mode</code> is <code>"w"</code>).
3174
3175
3176
3177
3178 <p>
3179 <hr><h3><a name="pdf-io.read"><code>io.read (&middot;&middot;&middot;)</code></a></h3>
3180
3181
3182 <p>
3183 Equivalent to <code>io.input():read(&middot;&middot;&middot;)</code>.
3184
3185
3186
3187
3188 <p>
3189 <hr><h3><a name="pdf-io.tmpfile"><code>io.tmpfile ()</code></a></h3>
3190
3191
3192 <p>
3193 Returns a handle for a temporary file.
3194 This file is opened in update mode
3195 and it is automatically removed when the program ends.
3196
3197
3198
3199
3200 <p>
3201 <hr><h3><a name="pdf-io.type"><code>io.type (obj)</code></a></h3>
3202
3203
3204 <p>
3205 Checks whether <code>obj</code> is a valid file handle.
3206 Returns the string <code>"file"</code> if <code>obj</code> is an open file handle,
3207 <code>"closed file"</code> if <code>obj</code> is a closed file handle,
3208 or <b>nil</b> if <code>obj</code> is not a file handle.
3209
3210
3211
3212
3213 <p>
3214 <hr><h3><a name="pdf-io.write"><code>io.write (&middot;&middot;&middot;)</code></a></h3>
3215
3216
3217 <p>
3218 Equivalent to <code>io.output():write(&middot;&middot;&middot;)</code>.
3219
3220
3221
3222
3223 <p>
3224 <hr><h3><a name="pdf-file:close"><code>file:close ()</code></a></h3>
3225
3226
3227 <p>
3228 Closes <code>file</code>.
3229 Note that files are automatically closed when
3230 their handles are garbage collected,
3231 but that takes an unpredictable amount of time to happen.
3232
3233
3234 <p>
3235 When closing a file handle created with <a href="#pdf-io.popen"><code>io.popen</code></a>,
3236 <a href="#pdf-file:close"><code>file:close</code></a> returns the same values
3237 returned by <a href="#pdf-os.execute"><code>os.execute</code></a>.
3238
3239
3240
3241
3242 <p>
3243 <hr><h3><a name="pdf-file:flush"><code>file:flush ()</code></a></h3>
3244
3245
3246 <p>
3247 Saves any written data to <code>file</code>.
3248
3249
3250
3251
3252 <p>
3253 <hr><h3><a name="pdf-file:lines"><code>file:lines (&middot;&middot;&middot;)</code></a></h3>
3254
3255
3256 <p>
3257 Returns an iterator function that,
3258 each time it is called,
3259 reads the file according to the given formats.
3260 When no format is given,
3261 uses "<code>l</code>" as a default.
3262 As an example, the construction
3263
3264 <pre>
3265 for c in file:lines(1) do <em>body</em> end
3266 </pre><p>
3267 will iterate over all characters of the file,
3268 starting at the current position.
3269 Unlike <a href="#pdf-io.lines"><code>io.lines</code></a>, this function does not close the file
3270 when the loop ends.
3271
3272
3273 <p>
3274 In case of errors this function raises the error,
3275 instead of returning an error code.
3276
3277
3278
3279
3280 <p>
3281 <hr><h3><a name="pdf-file:read"><code>file:read (&middot;&middot;&middot;)</code></a></h3>
3282
3283
3284 <p>
3285 Reads the file <code>file</code>,
3286 according to the given formats, which specify what to read.
3287 For each format,
3288 the function returns a string or a number with the characters read,
3289 or <b>nil</b> if it cannot read data with the specified format.
3290 (In this latter case,
3291 the function does not read subsequent formats.)
3292 When called without formats,
3293 it uses a default format that reads the next line
3294 (see below).
3295
3296
3297 <p>
3298 The available formats are
3299
3300 <ul>
3301
3302 <li><b>"<code>n</code>": </b>
3303 reads a numeral and returns it as a float or an integer,
3304 following the lexical conventions of Lua.
3305 (The numeral may have leading spaces and a sign.)
3306 This format always reads the longest input sequence that
3307 is a valid prefix for a number;
3308 if that prefix does not form a valid number
3309 (e.g., an empty string, "<code>0x</code>", or "<code>3.4e-</code>"),
3310 it is discarded and the function returns <b>nil</b>.
3311 </li>
3312
3313 <li><b>"<code>i</code>": </b>
3314 reads an integral number and returns it as an integer.
3315 </li>
3316
3317 <li><b>"<code>a</code>": </b>
3318 reads the whole file, starting at the current position.
3319 On end of file, it returns the empty string.
3320 </li>
3321
3322 <li><b>"<code>l</code>": </b>
3323 reads the next line skipping the end of line,
3324 returning <b>nil</b> on end of file.
3325 This is the default format.
3326 </li>
3327
3328 <li><b>"<code>L</code>": </b>
3329 reads the next line keeping the end-of-line character (if present),
3330 returning <b>nil</b> on end of file.
3331 </li>
3332
3333 <li><b><em>number</em>: </b>
3334 reads a string with up to this number of bytes,
3335 returning <b>nil</b> on end of file.
3336 If <code>number</code> is zero,
3337 it reads nothing and returns an empty string,
3338 or <b>nil</b> on end of file.
3339 </li>
3340
3341 </ul><p>
3342 The formats "<code>l</code>" and "<code>L</code>" should be used only for text files.
3343
3344
3345
3346
3347 <p>
3348 <hr><h3><a name="pdf-file:seek"><code>file:seek ([whence [, offset]])</code></a></h3>
3349
3350
3351 <p>
3352 Sets and gets the file position,
3353 measured from the beginning of the file,
3354 to the position given by <code>offset</code> plus a base
3355 specified by the string <code>whence</code>, as follows:
3356
3357 <ul>
3358 <li><b>"<code>set</code>": </b> base is position 0 (beginning of the file);</li>
3359 <li><b>"<code>cur</code>": </b> base is current position;</li>
3360 <li><b>"<code>end</code>": </b> base is end of file;</li>
3361 </ul><p>
3362 In case of success, <code>seek</code> returns the final file position,
3363 measured in bytes from the beginning of the file.
3364 If <code>seek</code> fails, it returns <b>nil</b>,
3365 plus a string describing the error.
3366
3367
3368 <p>
3369 The default value for <code>whence</code> is <code>"cur"</code>,
3370 and for <code>offset</code> is 0.
3371 Therefore, the call <code>file:seek()</code> returns the current
3372 file position, without changing it;
3373 the call <code>file:seek("set")</code> sets the position to the
3374 beginning of the file (and returns 0);
3375 and the call <code>file:seek("end")</code> sets the position to the
3376 end of the file, and returns its size.
3377
3378
3379
3380
3381 <p>
3382 <hr><h3><a name="pdf-file:setvbuf"><code>file:setvbuf (mode [, size])</code></a></h3>
3383
3384
3385 <p>
3386 Sets the buffering mode for an output file.
3387 There are three available modes:
3388
3389 <ul>
3390
3391 <li><b>"<code>no</code>": </b>
3392 no buffering; the result of any output operation appears immediately.
3393 </li>
3394
3395 <li><b>"<code>full</code>": </b>
3396 full buffering; output operation is performed only
3397 when the buffer is full or when
3398 you explicitly <code>flush</code> the file (see <a href="#pdf-io.flush"><code>io.flush</code></a>).
3399 </li>
3400
3401 <li><b>"<code>line</code>": </b>
3402 line buffering; output is buffered until a newline is output
3403 or there is any input from some special files
3404 (such as a terminal device).
3405 </li>
3406
3407 </ul><p>
3408 For the last two cases, <code>size</code>
3409 specifies the size of the buffer, in bytes.
3410 The default is an appropriate size.
3411
3412
3413
3414
3415 <p>
3416 <hr><h3><a name="pdf-file:write"><code>file:write (&middot;&middot;&middot;)</code></a></h3>
3417
3418
3419 <p>
3420 Writes the value of each of its arguments to <code>file</code>.
3421 The arguments must be strings or numbers.
3422
3423
3424 <p>
3425 In case of success, this function returns <code>file</code>.
3426 Otherwise it returns <b>nil</b> plus a string describing the error.
3427
3428
3429
3430
3431
3432
3433
3434 <h2>6.9 &ndash; <a name="6.9">Operating System Facilities</a></h2>
3435
3436 <p>
3437 This library is implemented through table <a name="pdf-os"><code>os</code></a>.
3438
3439
3440 <p>
3441 <hr><h3><a name="pdf-os.clock"><code>os.clock ()</code></a></h3>
3442
3443
3444 <p>
3445 Returns an approximation of the amount in seconds of CPU time
3446 used by the program.
3447
3448
3449
3450
3451 <p>
3452 <hr><h3><a name="pdf-os.date"><code>os.date ([format [, time]])</code></a></h3>
3453
3454
3455 <p>
3456 Returns a string or a table containing date and time,
3457 formatted according to the given string <code>format</code>.
3458
3459
3460 <p>
3461 If the <code>time</code> argument is present,
3462 this is the time to be formatted
3463 (see the <a href="#pdf-os.time"><code>os.time</code></a> function for a description of this value).
3464 Otherwise, <code>date</code> formats the current time.
3465
3466
3467 <p>
3468 If <code>format</code> starts with '<code>!</code>',
3469 then the date is formatted in Coordinated Universal Time.
3470 After this optional character,
3471 if <code>format</code> is the string "<code>*t</code>",
3472 then <code>date</code> returns a table with the following fields:
3473 <code>year</code> (four digits), <code>month</code> (1&ndash;12), <code>day</code> (1&ndash;31),
3474 <code>hour</code> (0&ndash;23), <code>min</code> (0&ndash;59), <code>sec</code> (0&ndash;61),
3475 <code>wday</code> (weekday, Sunday is&nbsp;1),
3476 <code>yday</code> (day of the year),
3477 and <code>isdst</code> (daylight saving flag, a boolean).
3478 This last field may be absent
3479 if the information is not available.
3480
3481
3482 <p>
3483 If <code>format</code> is not "<code>*t</code>",
3484 then <code>date</code> returns the date as a string,
3485 formatted according to the same rules as the ISO&nbsp;C function <code>strftime</code>.
3486
3487
3488 <p>
3489 When called without arguments,
3490 <code>date</code> returns a reasonable date and time representation that depends on
3491 the host system and on the current locale
3492 (that is, <code>os.date()</code> is equivalent to <code>os.date("%c")</code>).
3493
3494
3495 <p>
3496 On non-POSIX systems,
3497 this function may be not thread safe
3498 because of its reliance on C&nbsp;function <code>gmtime</code> and C&nbsp;function <code>localtime</code>.
3499
3500
3501
3502
3503 <p>
3504 <hr><h3><a name="pdf-os.difftime"><code>os.difftime (t2, t1)</code></a></h3>
3505
3506
3507 <p>
3508 Returns the difference, in seconds,
3509 from time <code>t1</code> to time <code>t2</code>
3510 (where the times are values returned by <a href="#pdf-os.time"><code>os.time</code></a>).
3511 In POSIX, Windows, and some other systems,
3512 this value is exactly <code>t2</code><em>-</em><code>t1</code>.
3513
3514
3515
3516
3517 <p>
3518 <hr><h3><a name="pdf-os.execute"><code>os.execute ([command])</code></a></h3>
3519
3520
3521 <p>
3522 This function is equivalent to the ISO&nbsp;C function <code>system</code>.
3523 It passes <code>command</code> to be executed by an operating system shell.
3524 Its first result is <b>true</b>
3525 if the command terminated successfully,
3526 or <b>nil</b> otherwise.
3527 After this first result
3528 the function returns a string plus a number,
3529 as follows:
3530
3531 <ul>
3532
3533 <li><b>"<code>exit</code>": </b>
3534 the command terminated normally;
3535 the following number is the exit status of the command.
3536 </li>
3537
3538 <li><b>"<code>signal</code>": </b>
3539 the command was terminated by a signal;
3540 the following number is the signal that terminated the command.
3541 </li>
3542
3543 </ul>
3544
3545 <p>
3546 When called without a <code>command</code>,
3547 <code>os.execute</code> returns a boolean that is true if a shell is available.
3548
3549
3550
3551
3552 <p>
3553 <hr><h3><a name="pdf-os.exit"><code>os.exit ([code [, close]])</code></a></h3>
3554
3555
3556 <p>
3557 Calls the ISO&nbsp;C function <code>exit</code> to terminate the host program.
3558 If <code>code</code> is <b>true</b>,
3559 the returned status is <code>EXIT_SUCCESS</code>;
3560 if <code>code</code> is <b>false</b>,
3561 the returned status is <code>EXIT_FAILURE</code>;
3562 if <code>code</code> is a number,
3563 the returned status is this number.
3564 The default value for <code>code</code> is <b>true</b>.
3565
3566
3567 <p>
3568 If the optional second argument <code>close</code> is true,
3569 closes the Lua state before exiting.
3570
3571
3572
3573
3574 <p>
3575 <hr><h3><a name="pdf-os.getenv"><code>os.getenv (varname)</code></a></h3>
3576
3577
3578 <p>
3579 Returns the value of the process environment variable <code>varname</code>,
3580 or <b>nil</b> if the variable is not defined.
3581
3582
3583
3584
3585 <p>
3586 <hr><h3><a name="pdf-os.remove"><code>os.remove (filename)</code></a></h3>
3587
3588
3589 <p>
3590 Deletes the file (or empty directory, on POSIX systems)
3591 with the given name.
3592 If this function fails, it returns <b>nil</b>,
3593 plus a string describing the error and the error code.
3594
3595
3596
3597
3598 <p>
3599 <hr><h3><a name="pdf-os.rename"><code>os.rename (oldname, newname)</code></a></h3>
3600
3601
3602 <p>
3603 Renames file or directory named <code>oldname</code> to <code>newname</code>.
3604 If this function fails, it returns <b>nil</b>,
3605 plus a string describing the error and the error code.
3606
3607
3608
3609
3610 <p>
3611 <hr><h3><a name="pdf-os.setlocale"><code>os.setlocale (locale [, category])</code></a></h3>
3612
3613
3614 <p>
3615 Sets the current locale of the program.
3616 <code>locale</code> is a system-dependent string specifying a locale;
3617 <code>category</code> is an optional string describing which category to change:
3618 <code>"all"</code>, <code>"collate"</code>, <code>"ctype"</code>,
3619 <code>"monetary"</code>, <code>"numeric"</code>, or <code>"time"</code>;
3620 the default category is <code>"all"</code>.
3621 The function returns the name of the new locale,
3622 or <b>nil</b> if the request cannot be honored.
3623
3624
3625 <p>
3626 If <code>locale</code> is the empty string,
3627 the current locale is set to an implementation-defined native locale.
3628 If <code>locale</code> is the string "<code>C</code>",
3629 the current locale is set to the standard C locale.
3630
3631
3632 <p>
3633 When called with <b>nil</b> as the first argument,
3634 this function only returns the name of the current locale
3635 for the given category.
3636
3637
3638 <p>
3639 This function may be not thread safe
3640 because of its reliance on C&nbsp;function <code>setlocale</code>.
3641
3642
3643
3644
3645 <p>
3646 <hr><h3><a name="pdf-os.time"><code>os.time ([table])</code></a></h3>
3647
3648
3649 <p>
3650 Returns the current time when called without arguments,
3651 or a time representing the date and time specified by the given table.
3652 This table must have fields <code>year</code>, <code>month</code>, and <code>day</code>,
3653 and may have fields
3654 <code>hour</code> (default is 12),
3655 <code>min</code> (default is 0),
3656 <code>sec</code> (default is 0),
3657 and <code>isdst</code> (default is <b>nil</b>).
3658 For a description of these fields, see the <a href="#pdf-os.date"><code>os.date</code></a> function.
3659
3660
3661 <p>
3662 The returned value is a number, whose meaning depends on your system.
3663 In POSIX, Windows, and some other systems,
3664 this number counts the number
3665 of seconds since some given start time (the "epoch").
3666 In other systems, the meaning is not specified,
3667 and the number returned by <code>time</code> can be used only as an argument to
3668 <a href="#pdf-os.date"><code>os.date</code></a> and <a href="#pdf-os.difftime"><code>os.difftime</code></a>.
3669
3670
3671
3672
3673 <p>
3674 <hr><h3><a name="pdf-os.tmpname"><code>os.tmpname ()</code></a></h3>
3675
3676
3677 <p>
3678 Returns a string with a file name that can
3679 be used for a temporary file.
3680 The file must be explicitly opened before its use
3681 and explicitly removed when no longer needed.
3682
3683
3684 <p>
3685 On POSIX systems,
3686 this function also creates a file with that name,
3687 to avoid security risks.
3688 (Someone else might create the file with wrong permissions
3689 in the time between getting the name and creating the file.)
3690 You still have to open the file to use it
3691 and to remove it (even if you do not use it).
3692
3693
3694 <p>
3695 When possible,
3696 you may prefer to use <a href="#pdf-io.tmpfile"><code>io.tmpfile</code></a>,
3697 which automatically removes the file when the program ends.
3698
3699
3700
3701
3702
3703
3704
3705 <h2>6.10 &ndash; <a name="6.10">The Debug Library</a></h2>
3706
3707 <p>
3708 This library provides
3709 the functionality of the debug interface (<a href="#4.9">&sect;4.9</a>) to Lua programs.
3710 You should exert care when using this library.
3711 Several of its functions
3712 violate basic assumptions about Lua code
3713 (e.g., that variables local to a function
3714 cannot be accessed from outside;
3715 that userdata metatables cannot be changed by Lua code;
3716 that Lua programs do not crash)
3717 and therefore can compromise otherwise secure code.
3718 Moreover, some functions in this library may be slow.
3719
3720
3721 <p>
3722 All functions in this library are provided
3723 inside the <a name="pdf-debug"><code>debug</code></a> table.
3724 All functions that operate over a thread
3725 have an optional first argument which is the
3726 thread to operate over.
3727 The default is always the current thread.
3728
3729
3730 <p>
3731 <hr><h3><a name="pdf-debug.debug"><code>debug.debug ()</code></a></h3>
3732
3733
3734 <p>
3735 Enters an interactive mode with the user,
3736 running each string that the user enters.
3737 Using simple commands and other debug facilities,
3738 the user can inspect global and local variables,
3739 change their values, evaluate expressions, and so on.
3740 A line containing only the word <code>cont</code> finishes this function,
3741 so that the caller continues its execution.
3742
3743
3744 <p>
3745 Note that commands for <code>debug.debug</code> are not lexically nested
3746 within any function and so have no direct access to local variables.
3747
3748
3749
3750
3751 <p>
3752 <hr><h3><a name="pdf-debug.gethook"><code>debug.gethook ([thread])</code></a></h3>
3753
3754
3755 <p>
3756 Returns the current hook settings of the thread, as three values:
3757 the current hook function, the current hook mask,
3758 and the current hook count
3759 (as set by the <a href="#pdf-debug.sethook"><code>debug.sethook</code></a> function).
3760
3761
3762
3763
3764 <p>
3765 <hr><h3><a name="pdf-debug.getinfo"><code>debug.getinfo ([thread,] f [, what])</code></a></h3>
3766
3767
3768 <p>
3769 Returns a table with information about a function.
3770 You can give the function directly
3771 or you can give a number as the value of <code>f</code>,
3772 which means the function running at level <code>f</code> of the call stack
3773 of the given thread:
3774 level&nbsp;0 is the current function (<code>getinfo</code> itself);
3775 level&nbsp;1 is the function that called <code>getinfo</code>
3776 (except for tail calls, which do not count on the stack);
3777 and so on.
3778 If <code>f</code> is a number larger than the number of active functions,
3779 then <code>getinfo</code> returns <b>nil</b>.
3780
3781
3782 <p>
3783 The returned table can contain all the fields returned by <a href="#lua_getinfo"><code>lua_getinfo</code></a>,
3784 with the string <code>what</code> describing which fields to fill in.
3785 The default for <code>what</code> is to get all information available,
3786 except the table of valid lines.
3787 If present,
3788 the option '<code>f</code>'
3789 adds a field named <code>func</code> with the function itself.
3790 If present,
3791 the option '<code>L</code>'
3792 adds a field named <code>activelines</code> with the table of
3793 valid lines.
3794
3795
3796 <p>
3797 For instance, the expression <code>debug.getinfo(1,"n").name</code> returns
3798 a table with a name for the current function,
3799 if a reasonable name can be found,
3800 and the expression <code>debug.getinfo(print)</code>
3801 returns a table with all available information
3802 about the <a href="#pdf-print"><code>print</code></a> function.
3803
3804
3805
3806
3807 <p>
3808 <hr><h3><a name="pdf-debug.getlocal"><code>debug.getlocal ([thread,] f, local)</code></a></h3>
3809
3810
3811 <p>
3812 This function returns the name and the value of the local variable
3813 with index <code>local</code> of the function at level <code>f</code> of the stack.
3814 This function accesses not only explicit local variables,
3815 but also parameters, temporaries, etc.
3816
3817
3818 <p>
3819 The first parameter or local variable has index&nbsp;1, and so on,
3820 following the order that they are declared in the code,
3821 counting only the variables that are active
3822 in the current scope of the function.
3823 Negative indices refer to vararg parameters;
3824 -1 is the first vararg parameter.
3825 The function returns <b>nil</b> if there is no variable with the given index,
3826 and raises an error when called with a level out of range.
3827 (You can call <a href="#pdf-debug.getinfo"><code>debug.getinfo</code></a> to check whether the level is valid.)
3828
3829
3830 <p>
3831 Variable names starting with '<code>(</code>' (open parenthesis)
3832 represent variables with no known names
3833 (internal variables such as loop control variables,
3834 and variables from chunks saved without debug information).
3835
3836
3837 <p>
3838 The parameter <code>f</code> may also be a function.
3839 In that case, <code>getlocal</code> returns only the name of function parameters.
3840
3841
3842
3843
3844 <p>
3845 <hr><h3><a name="pdf-debug.getmetatable"><code>debug.getmetatable (value)</code></a></h3>
3846
3847
3848 <p>
3849 Returns the metatable of the given <code>value</code>
3850 or <b>nil</b> if it does not have a metatable.
3851
3852
3853
3854
3855 <p>
3856 <hr><h3><a name="pdf-debug.getregistry"><code>debug.getregistry ()</code></a></h3>
3857
3858
3859 <p>
3860 Returns the registry table (see <a href="#4.5">&sect;4.5</a>).
3861
3862
3863
3864
3865 <p>
3866 <hr><h3><a name="pdf-debug.getupvalue"><code>debug.getupvalue (f, up)</code></a></h3>
3867
3868
3869 <p>
3870 This function returns the name and the value of the upvalue
3871 with index <code>up</code> of the function <code>f</code>.
3872 The function returns <b>nil</b> if there is no upvalue with the given index.
3873
3874
3875 <p>
3876 Variable names starting with '<code>(</code>' (open parenthesis)
3877 represent variables with no known names
3878 (variables from chunks saved without debug information).
3879
3880
3881
3882
3883 <p>
3884 <hr><h3><a name="pdf-debug.getuservalue"><code>debug.getuservalue (u)</code></a></h3>
3885
3886
3887 <p>
3888 Returns the Lua value associated to <code>u</code>.
3889 If <code>u</code> is not a userdata,
3890 returns <b>nil</b>.
3891
3892
3893
3894
3895 <p>
3896 <hr><h3><a name="pdf-debug.sethook"><code>debug.sethook ([thread,] hook, mask [, count])</code></a></h3>
3897
3898
3899 <p>
3900 Sets the given function as a hook.
3901 The string <code>mask</code> and the number <code>count</code> describe
3902 when the hook will be called.
3903 The string mask may have any combination of the following characters,
3904 with the given meaning:
3905
3906 <ul>
3907 <li><b>'<code>c</code>': </b> the hook is called every time Lua calls a function;</li>
3908 <li><b>'<code>r</code>': </b> the hook is called every time Lua returns from a function;</li>
3909 <li><b>'<code>l</code>': </b> the hook is called every time Lua enters a new line of code.</li>
3910 </ul><p>
3911 Moreover,
3912 with a <code>count</code> different from zero,
3913 the hook is called also after every <code>count</code> instructions.
3914
3915
3916 <p>
3917 When called without arguments,
3918 <a href="#pdf-debug.sethook"><code>debug.sethook</code></a> turns off the hook.
3919
3920
3921 <p>
3922 When the hook is called, its first parameter is a string
3923 describing the event that has triggered its call:
3924 <code>"call"</code> (or <code>"tail call"</code>),
3925 <code>"return"</code>,
3926 <code>"line"</code>, and <code>"count"</code>.
3927 For line events,
3928 the hook also gets the new line number as its second parameter.
3929 Inside a hook,
3930 you can call <code>getinfo</code> with level&nbsp;2 to get more information about
3931 the running function
3932 (level&nbsp;0 is the <code>getinfo</code> function,
3933 and level&nbsp;1 is the hook function).
3934
3935
3936
3937
3938 <p>
3939 <hr><h3><a name="pdf-debug.setlocal"><code>debug.setlocal ([thread,] level, local, value)</code></a></h3>
3940
3941
3942 <p>
3943 This function assigns the value <code>value</code> to the local variable
3944 with index <code>local</code> of the function at level <code>level</code> of the stack.
3945 The function returns <b>nil</b> if there is no local
3946 variable with the given index,
3947 and raises an error when called with a <code>level</code> out of range.
3948 (You can call <code>getinfo</code> to check whether the level is valid.)
3949 Otherwise, it returns the name of the local variable.
3950
3951
3952 <p>
3953 See <a href="#pdf-debug.getlocal"><code>debug.getlocal</code></a> for more information about
3954 variable indices and names.
3955
3956
3957
3958
3959 <p>
3960 <hr><h3><a name="pdf-debug.setmetatable"><code>debug.setmetatable (value, table)</code></a></h3>
3961
3962
3963 <p>
3964 Sets the metatable for the given <code>value</code> to the given <code>table</code>
3965 (which can be <b>nil</b>).
3966 Returns <code>value</code>.
3967
3968
3969
3970
3971 <p>
3972 <hr><h3><a name="pdf-debug.setupvalue"><code>debug.setupvalue (f, up, value)</code></a></h3>
3973
3974
3975 <p>
3976 This function assigns the value <code>value</code> to the upvalue
3977 with index <code>up</code> of the function <code>f</code>.
3978 The function returns <b>nil</b> if there is no upvalue
3979 with the given index.
3980 Otherwise, it returns the name of the upvalue.
3981
3982
3983
3984
3985 <p>
3986 <hr><h3><a name="pdf-debug.setuservalue"><code>debug.setuservalue (udata, value)</code></a></h3>
3987
3988
3989 <p>
3990 Sets the given <code>value</code> as
3991 the Lua value associated to the given <code>udata</code>.
3992 <code>udata</code> must be a full userdata.
3993
3994
3995 <p>
3996 Returns <code>udata</code>.
3997
3998
3999
4000
4001 <p>
4002 <hr><h3><a name="pdf-debug.traceback"><code>debug.traceback ([thread,] [message [, level]])</code></a></h3>
4003
4004
4005 <p>
4006 If <code>message</code> is present but is neither a string nor <b>nil</b>,
4007 this function returns <code>message</code> without further processing.
4008 Otherwise,
4009 it returns a string with a traceback of the call stack.
4010 The optional <code>message</code> string is appended
4011 at the beginning of the traceback.
4012 An optional <code>level</code> number tells at which level
4013 to start the traceback
4014 (default is 1, the function calling <code>traceback</code>).
4015
4016
4017
4018
4019 <p>
4020 <hr><h3><a name="pdf-debug.upvalueid"><code>debug.upvalueid (f, n)</code></a></h3>
4021
4022
4023 <p>
4024 Returns a unique identifier (as a light userdata)
4025 for the upvalue numbered <code>n</code>
4026 from the given function.
4027
4028
4029 <p>
4030 These unique identifiers allow a program to check whether different
4031 closures share upvalues.
4032 Lua closures that share an upvalue
4033 (that is, that access a same external local variable)
4034 will return identical ids for those upvalue indices.
4035
4036
4037
4038
4039 <p>
4040 <hr><h3><a name="pdf-debug.upvaluejoin"><code>debug.upvaluejoin (f1, n1, f2, n2)</code></a></h3>
4041
4042
4043 <p>
4044 Make the <code>n1</code>-th upvalue of the Lua closure <code>f1</code>
4045 refer to the <code>n2</code>-th upvalue of the Lua closure <code>f2</code>.
4046
4047
4048
4049
4050
4051
4052
4053 <h1>7 &ndash; <a name="7">Lua Standalone</a></h1>
4054
4055 <p>
4056 Although Lua has been designed as an extension language,
4057 to be embedded in a host C&nbsp;program,
4058 it is also frequently used as a standalone language.
4059 An interpreter for Lua as a standalone language,
4060 called simply <code>lua</code>,
4061 is provided with the standard distribution.
4062 The standalone interpreter includes
4063 all standard libraries, including the debug library.
4064 Its usage is:
4065
4066 <pre>
4067 lua [options] [script [args]]
4068 </pre><p>
4069 The options are:
4070
4071 <ul>
4072 <li><b><code>-e <em>stat</em></code>: </b> executes string <em>stat</em>;</li>
4073 <li><b><code>-l <em>mod</em></code>: </b> "requires" <em>mod</em>;</li>
4074 <li><b><code>-i</code>: </b> enters interactive mode after running <em>script</em>;</li>
4075 <li><b><code>-v</code>: </b> prints version information;</li>
4076 <li><b><code>-E</code>: </b> ignores environment variables;</li>
4077 <li><b><code>--</code>: </b> stops handling options;</li>
4078 <li><b><code>-</code>: </b> executes <code>stdin</code> as a file and stops handling options.</li>
4079 </ul><p>
4080 After handling its options, <code>lua</code> runs the given <em>script</em>.
4081 When called without arguments,
4082 <code>lua</code> behaves as <code>lua -v -i</code>
4083 when the standard input (<code>stdin</code>) is a terminal,
4084 and as <code>lua -</code> otherwise.
4085
4086
4087 <p>
4088 When called without option <code>-E</code>,
4089 the interpreter checks for an environment variable <a name="pdf-LUA_INIT_5_3"><code>LUA_INIT_5_3</code></a>
4090 (or <a name="pdf-LUA_INIT"><code>LUA_INIT</code></a> if the versioned name is not defined)
4091 before running any argument.
4092 If the variable content has the format <code>@<em>filename</em></code>,
4093 then <code>lua</code> executes the file.
4094 Otherwise, <code>lua</code> executes the string itself.
4095
4096
4097 <p>
4098 When called with option <code>-E</code>,
4099 besides ignoring <code>LUA_INIT</code>,
4100 Lua also ignores
4101 the values of <code>LUA_PATH</code> and <code>LUA_CPATH</code>,
4102 setting the values of
4103 <a href="#pdf-package.path"><code>package.path</code></a> and <a href="#pdf-package.cpath"><code>package.cpath</code></a>
4104 with the default paths defined in <code>luaconf.h</code>.
4105
4106
4107 <p>
4108 All options are handled in order, except <code>-i</code> and <code>-E</code>.
4109 For instance, an invocation like
4110
4111 <pre>
4112 $ lua -e'a=1' -e 'print(a)' script.lua
4113 </pre><p>
4114 will first set <code>a</code> to 1, then print the value of <code>a</code>,
4115 and finally run the file <code>script.lua</code> with no arguments.
4116 (Here <code>$</code> is the shell prompt. Your prompt may be different.)
4117
4118
4119 <p>
4120 Before running any code,
4121 <code>lua</code> collects all command-line arguments
4122 in a global table called <code>arg</code>.
4123 The script name goes to index 0,
4124 the first argument after the script name goes to index 1,
4125 and so on.
4126 Any arguments before the script name
4127 (that is, the interpreter name plus its options)
4128 go to negative indices.
4129 For instance, in the call
4130
4131 <pre>
4132 $ lua -la b.lua t1 t2
4133 </pre><p>
4134 the table is like this:
4135
4136 <pre>
4137 arg = { [-2] = "lua", [-1] = "-la",
4138 [0] = "b.lua",
4139 [1] = "t1", [2] = "t2" }
4140 </pre><p>
4141 If there is no script in the call,
4142 the interpreter name goes to index 0,
4143 followed by the other arguments.
4144 For instance, the call
4145
4146 <pre>
4147 $ lua -e "print(arg[1])"
4148 </pre><p>
4149 will print "<code>-e</code>".
4150 If there is a script,
4151 the script is called with parameters
4152 <code>arg[1]</code>, &middot;&middot;&middot;, <code>arg[#arg]</code>.
4153 (Like all chunks in Lua,
4154 the script is compiled as a vararg function.)
4155
4156
4157 <p>
4158 In interactive mode,
4159 Lua repeatedly prompts and waits for a line.
4160 After reading a line,
4161 Lua first try to interpret the line as an expression.
4162 If it succeeds, it prints its value.
4163 Otherwise, it interprets the line as a statement.
4164 If you write an incomplete statement,
4165 the interpreter waits for its completion
4166 by issuing a different prompt.
4167
4168
4169 <p>
4170 In case of unprotected errors in the script,
4171 the interpreter reports the error to the standard error stream.
4172 If the error object is not a string but
4173 has a metamethod <code>__to_string</code>,
4174 the interpreter calls this metamethod to produce the final message.
4175 Otherwise, the interpreter converts the error object to a string
4176 and adds a stack traceback to it.
4177
4178
4179 <p>
4180 When finishing normally,
4181 the interpreter closes its main Lua state
4182 (see <a href="#lua_close"><code>lua_close</code></a>).
4183 The script can avoid this step by
4184 calling <a href="#pdf-os.exit"><code>os.exit</code></a> to terminate.
4185
4186
4187 <p>
4188 To allow the use of Lua as a
4189 script interpreter in Unix systems,
4190 the standalone interpreter skips
4191 the first line of a chunk if it starts with <code>#</code>.
4192 Therefore, Lua scripts can be made into executable programs
4193 by using <code>chmod +x</code> and the&nbsp;<code>#!</code> form,
4194 as in
4195
4196 <pre>
4197 #!/usr/local/bin/lua
4198 </pre><p>
4199 (Of course,
4200 the location of the Lua interpreter may be different in your machine.
4201 If <code>lua</code> is in your <code>PATH</code>,
4202 then
4203
4204 <pre>
4205 #!/usr/bin/env lua
4206 </pre><p>
4207 is a more portable solution.)
4208
4209
4210
4211 <h1>8 &ndash; <a name="8">Incompatibilities with the Previous Version</a></h1>
4212
4213 <p>
4214 Here we list the incompatibilities that you may find when moving a program
4215 from Lua&nbsp;5.2 to Lua&nbsp;5.3.
4216 You can avoid some incompatibilities by compiling Lua with
4217 appropriate options (see file <code>luaconf.h</code>).
4218 However,
4219 all these compatibility options will be removed in the future.
4220
4221
4222 <p>
4223 Lua versions can always change the C API in ways that
4224 do not imply source-code changes in a program,
4225 such as the numeric values for constants
4226 or the implementation of functions as macros.
4227 Therefore,
4228 you should not assume that binaries are compatible between
4229 different Lua versions.
4230 Always recompile clients of the Lua API when
4231 using a new version.
4232
4233
4234 <p>
4235 Similarly, Lua versions can always change the internal representation
4236 of precompiled chunks;
4237 precompiled chunks are not compatible between different Lua versions.
4238
4239
4240 <p>
4241 The standard paths in the official distribution may
4242 change between versions.
4243
4244
4245
4246 <h2>8.1 &ndash; <a name="8.1">Changes in the Language</a></h2>
4247 <ul>
4248
4249 <li>
4250 The main difference between Lua&nbsp;5.2 and Lua&nbsp;5.3 is the
4251 introduction of an integer subtype for numbers.
4252 Although this change should not affect "normal" computations,
4253 some computations
4254 (mainly those that involve some kind of overflow)
4255 can give different results.
4256
4257
4258 <p>
4259 You can fix these differences by forcing a number to be a float
4260 (in Lua&nbsp;5.2 all numbers were float),
4261 in particular writing constants with an ending <code>.0</code>
4262 or using <code>x = x + 0.0</code> to convert a variable.
4263 (This recommendation is only for a quick fix
4264 for an occasional incompatibility;
4265 it is not a general guideline for good programming.
4266 For good programming,
4267 use floats where you need floats
4268 and integers where you need integers.)
4269 </li>
4270
4271 <li>
4272 The conversion of a float to a string now adds a <code>.0</code> suffix
4273 to the result if it looks like an integer.
4274 (For instance, the float 2.0 will be printed as <code>2.0</code>,
4275 not as <code>2</code>.)
4276 You should always use an explicit format
4277 when you need a specific format for numbers.
4278
4279
4280 <p>
4281 (Formally this is not an incompatibility,
4282 because Lua does not specify how numbers are formatted as strings,
4283 but some programs assumed a specific format.)
4284 </li>
4285
4286 <li>
4287 The generational mode for the garbage collector was removed.
4288 (It was an experimental feature in Lua&nbsp;5.2.)
4289 </li>
4290
4291 </ul>
4292
4293
4294
4295
4296 <h2>8.2 &ndash; <a name="8.2">Changes in the Libraries</a></h2>
4297 <ul>
4298
4299 <li>
4300 The <code>bit32</code> library has been deprecated.
4301 It is easy to require a compatible external library or,
4302 better yet, to replace its functions with appropriate bitwise operations.
4303 (Keep in mind that <code>bit32</code> operates on 32-bit integers,
4304 while the bitwise operators in standard Lua operate on 64-bit integers.)
4305 </li>
4306
4307 <li>
4308 The Table library now respects metamethods
4309 for setting and getting elements.
4310 </li>
4311
4312 <li>
4313 The <a href="#pdf-ipairs"><code>ipairs</code></a> iterator now respects metamethods and
4314 its <code>__ipairs</code> metamethod has been deprecated.
4315 </li>
4316
4317 <li>
4318 Option names in <a href="#pdf-io.read"><code>io.read</code></a> do not have a starting '<code>*</code>' anymore.
4319 For compatibility, Lua will continue to ignore this character.
4320 </li>
4321
4322 <li>
4323 The following functions were deprecated in the mathematical library:
4324 <code>atan2</code>, <code>cosh</code>, <code>sinh</code>, <code>tanh</code>, <code>pow</code>,
4325 <code>frexp</code>, and <code>ldexp</code>.
4326 You can replace <code>math.pow(x,y)</code> with <code>x^y</code>;
4327 you can replace <code>math.atan2</code> with <code>math.atan</code>,
4328 which now accepts one or two parameters;
4329 you can replace <code>math.ldexp(x,exp)</code> with <code>x * 2.0^exp</code>.
4330 For the other operations,
4331 you can either use an external library or
4332 implement them in Lua.
4333 </li>
4334
4335 <li>
4336 The searcher for C loaders used by <a href="#pdf-require"><code>require</code></a>
4337 changed the way it handles versioned names.
4338 Now, the version should come after the module name
4339 (as is usual in most other tools).
4340 For compatibility, that searcher still tries the old format
4341 if it cannot find an open function according to the new style.
4342 (Lua&nbsp;5.2 already worked that way,
4343 but it did not document the change.)
4344 </li>
4345
4346 </ul>
4347
4348
4349
4350
4351 <h2>8.3 &ndash; <a name="8.3">Changes in the API</a></h2>
4352
4353
4354 <ul>
4355
4356 <li>
4357 Continuation functions now receive as parameters what they needed
4358 to get through <code>lua_getctx</code>,
4359 so <code>lua_getctx</code> has been removed.
4360 Adapt your code accordingly.
4361 </li>
4362
4363 <li>
4364 Function <a href="#lua_dump"><code>lua_dump</code></a> has an extra parameter, <code>strip</code>.
4365 Use 0 as the value of this parameter to get the old behavior.
4366 </li>
4367
4368 <li>
4369 Functions to inject/project unsigned integers
4370 (<code>lua_pushunsigned</code>, <code>lua_tounsigned</code>, <code>lua_tounsignedx</code>,
4371 <code>luaL_checkunsigned</code>, <code>luaL_optunsigned</code>)
4372 were deprecated.
4373 Use their signed equivalents with a type cast.
4374 </li>
4375
4376 <li>
4377 Macros to project non-default integer types
4378 (<code>luaL_checkint</code>, <code>luaL_optint</code>, <code>luaL_checklong</code>, <code>luaL_optlong</code>)
4379 were deprecated.
4380 Use their equivalent over <a href="#lua_Integer"><code>lua_Integer</code></a> with a type cast
4381 (or, when possible, use <a href="#lua_Integer"><code>lua_Integer</code></a> in your code).
4382 </li>
4383
4384 </ul>
4385
4386
4387
4388
4389 <h1>9 &ndash; <a name="9">The Complete Syntax of Lua</a></h1>
4390
4391 <p>
4392 Here is the complete syntax of Lua in extended BNF.
4393 As usual in extended BNF,
4394 {A} means 0 or more As,
4395 and [A] means an optional A.
4396 (For operator precedences, see <a href="#3.4.8">&sect;3.4.8</a>;
4397 for a description of the terminals
4398 Name, Numeral,
4399 and LiteralString, see <a href="#3.1">&sect;3.1</a>.)
4400
4401
4402
4403
4404 <pre>
4405
4406 chunk ::= block
4407
4408 block ::= {stat} [retstat]
4409
4410 stat ::= &lsquo;<b>;</b>&rsquo; |
4411 varlist &lsquo;<b>=</b>&rsquo; explist |
4412 functioncall |
4413 label |
4414 <b>break</b> |
4415 <b>goto</b> Name |
4416 <b>do</b> block <b>end</b> |
4417 <b>while</b> exp <b>do</b> block <b>end</b> |
4418 <b>repeat</b> block <b>until</b> exp |
4419 <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b> |
4420 <b>for</b> Name &lsquo;<b>=</b>&rsquo; exp &lsquo;<b>,</b>&rsquo; exp [&lsquo;<b>,</b>&rsquo; exp] <b>do</b> block <b>end</b> |
4421 <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b> |
4422 <b>function</b> funcname funcbody |
4423 <b>local</b> <b>function</b> Name funcbody |
4424 <b>local</b> namelist [&lsquo;<b>=</b>&rsquo; explist]
4425
4426 retstat ::= <b>return</b> [explist] [&lsquo;<b>;</b>&rsquo;]
4427
4428 label ::= &lsquo;<b>::</b>&rsquo; Name &lsquo;<b>::</b>&rsquo;
4429
4430 funcname ::= Name {&lsquo;<b>.</b>&rsquo; Name} [&lsquo;<b>:</b>&rsquo; Name]
4431
4432 varlist ::= var {&lsquo;<b>,</b>&rsquo; var}
4433
4434 var ::= Name | prefixexp &lsquo;<b>[</b>&rsquo; exp &lsquo;<b>]</b>&rsquo; | prefixexp &lsquo;<b>.</b>&rsquo; Name
4435
4436 namelist ::= Name {&lsquo;<b>,</b>&rsquo; Name}
4437
4438 explist ::= exp {&lsquo;<b>,</b>&rsquo; exp}
4439
4440 exp ::= <b>nil</b> | <b>false</b> | <b>true</b> | Numeral | LiteralString | &lsquo;<b>...</b>&rsquo; | functiondef |
4441 prefixexp | tableconstructor | exp binop exp | unop exp
4442
4443 prefixexp ::= var | functioncall | &lsquo;<b>(</b>&rsquo; exp &lsquo;<b>)</b>&rsquo;
4444
4445 functioncall ::= prefixexp args | prefixexp &lsquo;<b>:</b>&rsquo; Name args
4446
4447 args ::= &lsquo;<b>(</b>&rsquo; [explist] &lsquo;<b>)</b>&rsquo; | tableconstructor | LiteralString
4448
4449 functiondef ::= <b>function</b> funcbody
4450
4451 funcbody ::= &lsquo;<b>(</b>&rsquo; [parlist] &lsquo;<b>)</b>&rsquo; block <b>end</b>
4452
4453 parlist ::= namelist [&lsquo;<b>,</b>&rsquo; &lsquo;<b>...</b>&rsquo;] | &lsquo;<b>...</b>&rsquo;
4454
4455 tableconstructor ::= &lsquo;<b>{</b>&rsquo; [fieldlist] &lsquo;<b>}</b>&rsquo;
4456
4457 fieldlist ::= field {fieldsep field} [fieldsep]
4458
4459 field ::= &lsquo;<b>[</b>&rsquo; exp &lsquo;<b>]</b>&rsquo; &lsquo;<b>=</b>&rsquo; exp | Name &lsquo;<b>=</b>&rsquo; exp | exp
4460
4461 fieldsep ::= &lsquo;<b>,</b>&rsquo; | &lsquo;<b>;</b>&rsquo;
4462
4463 binop ::= &lsquo;<b>+</b>&rsquo; | &lsquo;<b>-</b>&rsquo; | &lsquo;<b>*</b>&rsquo; | &lsquo;<b>/</b>&rsquo; | &lsquo;<b>//</b>&rsquo; | &lsquo;<b>^</b>&rsquo; | &lsquo;<b>%</b>&rsquo; |
4464 &lsquo;<b>&amp;</b>&rsquo; | &lsquo;<b>~</b>&rsquo; | &lsquo;<b>|</b>&rsquo; | &lsquo;<b>&gt;&gt;</b>&rsquo; | &lsquo;<b>&lt;&lt;</b>&rsquo; | &lsquo;<b>..</b>&rsquo; |
4465 &lsquo;<b>&lt;</b>&rsquo; | &lsquo;<b>&lt;=</b>&rsquo; | &lsquo;<b>&gt;</b>&rsquo; | &lsquo;<b>&gt;=</b>&rsquo; | &lsquo;<b>==</b>&rsquo; | &lsquo;<b>~=</b>&rsquo; |
4466 <b>and</b> | <b>or</b>
4467
4468 unop ::= &lsquo;<b>-</b>&rsquo; | <b>not</b> | &lsquo;<b>#</b>&rsquo; | &lsquo;<b>~</b>&rsquo;
4469
4470 </pre>
4471
4472 <p>
4473
4474
4475
4476
4477
4478
4479
4480
4481 <HR>
4482 <SMALL CLASS="footer">
4483 Last update:
4484 Fri Jan 16 00:58:20 BRST 2015
4485 </SMALL>
4486 <!--
4487 Last change: minor edit
4488 -->
4489
4490 </body>
4491 </html>