comparison website/src/manual.html.luan @ 1651:5b8f056527a3

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