forked from redis/redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanual.html
8804 lines (5912 loc) · 249 KB
/
manual.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Lua 5.1 Reference Manual</title>
<link rel="stylesheet" type="text/css" href="lua.css">
<link rel="stylesheet" type="text/css" href="manual.css">
<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=iso-8859-1">
</head>
<body>
<hr>
<h1>
<a href="http://www.lua.org/"><img src="logo.gif" alt="" border="0"></a>
Lua 5.1 Reference Manual
</h1>
by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes
<p>
<small>
Copyright © 2006–2012 Lua.org, PUC-Rio.
Freely available under the terms of the
<a href="http://www.lua.org/license.html">Lua license</a>.
</small>
<hr>
<p>
<a href="contents.html#contents">contents</A>
·
<a href="contents.html#index">index</A>
·
<A HREF="http://www.lua.org/manual/">other versions</A>
<!-- ====================================================================== -->
<p>
<!-- $Id: manual.of,v 1.49.1.2 2012/01/13 20:23:26 roberto Exp $ -->
<h1>1 - <a name="1">Introduction</a></h1>
<p>
Lua is an extension programming language designed to support
general procedural programming with data description
facilities.
It also offers good support for object-oriented programming,
functional programming, and data-driven programming.
Lua is intended to be used as a powerful, light-weight
scripting language for any program that needs one.
Lua is implemented as a library, written in <em>clean</em> C
(that is, in the common subset of ANSI C and C++).
<p>
Being an extension language, Lua has no notion of a "main" program:
it only works <em>embedded</em> in a host client,
called the <em>embedding program</em> or simply the <em>host</em>.
This host program can invoke functions to execute a piece of Lua code,
can write and read Lua variables,
and can register C functions to be called by Lua code.
Through the use of C functions, Lua can be augmented to cope with
a wide range of different domains,
thus creating customized programming languages sharing a syntactical framework.
The Lua distribution includes a sample host program called <code>lua</code>,
which uses the Lua library to offer a complete, stand-alone Lua interpreter.
<p>
Lua is free software,
and is provided as usual with no guarantees,
as stated in its license.
The implementation described in this manual is available
at Lua's official web site, <code>www.lua.org</code>.
<p>
Like any other reference manual,
this document is dry in places.
For a discussion of the decisions behind the design of Lua,
see the technical papers available at Lua's web site.
For a detailed introduction to programming in Lua,
see Roberto's book, <em>Programming in Lua (Second Edition)</em>.
<h1>2 - <a name="2">The Language</a></h1>
<p>
This section describes the lexis, the syntax, and the semantics of Lua.
In other words,
this section describes
which tokens are valid,
how they can be combined,
and what their combinations mean.
<p>
The language constructs will be explained using the usual extended BNF notation,
in which
{<em>a</em>} means 0 or more <em>a</em>'s, and
[<em>a</em>] means an optional <em>a</em>.
Non-terminals are shown like non-terminal,
keywords are shown like <b>kword</b>,
and other terminal symbols are shown like `<b>=</b>´.
The complete syntax of Lua can be found in <a href="#8">§8</a>
at the end of this manual.
<h2>2.1 - <a name="2.1">Lexical Conventions</a></h2>
<p>
<em>Names</em>
(also called <em>identifiers</em>)
in Lua can be any string of letters,
digits, and underscores,
not beginning with a digit.
This coincides with the definition of names in most languages.
(The definition of letter depends on the current locale:
any character considered alphabetic by the current locale
can be used in an identifier.)
Identifiers are used to name variables and table fields.
<p>
The following <em>keywords</em> are reserved
and cannot be used as names:
<pre>
and break do else elseif
end false for function if
in local nil not or
repeat return then true until while
</pre>
<p>
Lua is a case-sensitive language:
<code>and</code> is a reserved word, but <code>And</code> and <code>AND</code>
are two different, valid names.
As a convention, names starting with an underscore followed by
uppercase letters (such as <a href="#pdf-_VERSION"><code>_VERSION</code></a>)
are reserved for internal global variables used by Lua.
<p>
The following strings denote other tokens:
<pre>
+ - * / % ^ #
== ~= <= >= < > =
( ) { } [ ]
; : , . .. ...
</pre>
<p>
<em>Literal strings</em>
can be delimited by matching single or double quotes,
and can contain the following C-like escape sequences:
'<code>\a</code>' (bell),
'<code>\b</code>' (backspace),
'<code>\f</code>' (form feed),
'<code>\n</code>' (newline),
'<code>\r</code>' (carriage return),
'<code>\t</code>' (horizontal tab),
'<code>\v</code>' (vertical tab),
'<code>\\</code>' (backslash),
'<code>\"</code>' (quotation mark [double quote]),
and '<code>\'</code>' (apostrophe [single quote]).
Moreover, a backslash followed by a real newline
results in a newline in the string.
A character in a string can also be specified by its numerical value
using the escape sequence <code>\<em>ddd</em></code>,
where <em>ddd</em> is a sequence of up to three decimal digits.
(Note that if a numerical escape is to be followed by a digit,
it must be expressed using exactly three digits.)
Strings in Lua can contain any 8-bit value, including embedded zeros,
which can be specified as '<code>\0</code>'.
<p>
Literal strings can also be defined using a long format
enclosed by <em>long brackets</em>.
We define an <em>opening long bracket of level <em>n</em></em> as an opening
square bracket followed by <em>n</em> equal signs followed by another
opening square bracket.
So, an opening long bracket of level 0 is written as <code>[[</code>,
an opening long bracket of level 1 is written as <code>[=[</code>,
and so on.
A <em>closing long bracket</em> is defined similarly;
for instance, a closing long bracket of level 4 is written as <code>]====]</code>.
A long string starts with an opening long bracket of any level and
ends at the first closing long bracket of the same level.
Literals in this bracketed form can run for several lines,
do not interpret any escape sequences,
and ignore long brackets of any other level.
They can contain anything except a closing bracket of the proper level.
<p>
For convenience,
when the opening long bracket is immediately followed by a newline,
the newline is not included in the string.
As an example, in a system using ASCII
(in which '<code>a</code>' is coded as 97,
newline is coded as 10, and '<code>1</code>' is coded as 49),
the five literal strings below denote the same string:
<pre>
a = 'alo\n123"'
a = "alo\n123\""
a = '\97lo\10\04923"'
a = [[alo
123"]]
a = [==[
alo
123"]==]
</pre>
<p>
A <em>numerical constant</em> can be written with an optional decimal part
and an optional decimal exponent.
Lua also accepts integer hexadecimal constants,
by prefixing them with <code>0x</code>.
Examples of valid numerical constants are
<pre>
3 3.0 3.1416 314.16e-2 0.31416E1 0xff 0x56
</pre>
<p>
A <em>comment</em> starts with a double hyphen (<code>--</code>)
anywhere outside a string.
If the text immediately after <code>--</code> is not an opening long bracket,
the comment is a <em>short comment</em>,
which runs until the end of the line.
Otherwise, it is a <em>long comment</em>,
which runs until the corresponding closing long bracket.
Long comments are frequently used to disable code temporarily.
<h2>2.2 - <a name="2.2">Values and Types</a></h2>
<p>
Lua is a <em>dynamically typed language</em>.
This means that
variables do not have types; only values do.
There are no type definitions in the language.
All values carry their own type.
<p>
All values in Lua are <em>first-class values</em>.
This means that all values can be stored in variables,
passed as arguments to other functions, and returned as results.
<p>
There are eight basic types in Lua:
<em>nil</em>, <em>boolean</em>, <em>number</em>,
<em>string</em>, <em>function</em>, <em>userdata</em>,
<em>thread</em>, and <em>table</em>.
<em>Nil</em> is the type of the value <b>nil</b>,
whose main property is to be different from any other value;
it usually represents the absence of a useful value.
<em>Boolean</em> is the type of the values <b>false</b> and <b>true</b>.
Both <b>nil</b> and <b>false</b> make a condition false;
any other value makes it true.
<em>Number</em> represents real (double-precision floating-point) numbers.
(It is easy to build Lua interpreters that use other
internal representations for numbers,
such as single-precision float or long integers;
see file <code>luaconf.h</code>.)
<em>String</em> represents arrays of characters.
Lua is 8-bit clean:
strings can contain any 8-bit character,
including embedded zeros ('<code>\0</code>') (see <a href="#2.1">§2.1</a>).
<p>
Lua can call (and manipulate) functions written in Lua and
functions written in C
(see <a href="#2.5.8">§2.5.8</a>).
<p>
The type <em>userdata</em> is provided to allow arbitrary C data to
be stored in Lua variables.
This type corresponds to a block of raw memory
and has no pre-defined operations in Lua,
except assignment and identity test.
However, by using <em>metatables</em>,
the programmer can define operations for userdata values
(see <a href="#2.8">§2.8</a>).
Userdata values cannot be created or modified in Lua,
only through the C API.
This guarantees the integrity of data owned by the host program.
<p>
The type <em>thread</em> represents independent threads of execution
and it is used to implement coroutines (see <a href="#2.11">§2.11</a>).
Do not confuse Lua threads with operating-system threads.
Lua supports coroutines on all systems,
even those that do not support threads.
<p>
The type <em>table</em> implements associative arrays,
that is, arrays that can be indexed not only with numbers,
but with any value (except <b>nil</b>).
Tables can be <em>heterogeneous</em>;
that is, they can contain values of all types (except <b>nil</b>).
Tables are the sole data structuring mechanism in Lua;
they can be used to represent ordinary arrays,
symbol tables, sets, records, graphs, trees, etc.
To represent records, Lua uses the field name as an index.
The language supports this representation by
providing <code>a.name</code> as syntactic sugar for <code>a["name"]</code>.
There are several convenient ways to create tables in Lua
(see <a href="#2.5.7">§2.5.7</a>).
<p>
Like indices,
the value of a table field can be of any type (except <b>nil</b>).
In particular,
because functions are first-class values,
table fields can contain functions.
Thus tables can also carry <em>methods</em> (see <a href="#2.5.9">§2.5.9</a>).
<p>
Tables, functions, threads, and (full) userdata values are <em>objects</em>:
variables do not actually <em>contain</em> these values,
only <em>references</em> to them.
Assignment, parameter passing, and function returns
always manipulate references to such values;
these operations do not imply any kind of copy.
<p>
The library function <a href="#pdf-type"><code>type</code></a> returns a string describing the type
of a given value.
<h3>2.2.1 - <a name="2.2.1">Coercion</a></h3>
<p>
Lua provides automatic conversion between
string and number values at run time.
Any arithmetic operation applied to a string tries to convert
this string to a number, following the usual conversion rules.
Conversely, whenever a number is used where a string is expected,
the number is converted to a string, in a reasonable format.
For complete control over how numbers are converted to strings,
use the <code>format</code> function from the string library
(see <a href="#pdf-string.format"><code>string.format</code></a>).
<h2>2.3 - <a name="2.3">Variables</a></h2>
<p>
Variables are places that store values.
There are three kinds of variables in Lua:
global variables, local variables, and table fields.
<p>
A single name can denote a global variable or a local variable
(or a function's formal parameter,
which is a particular kind of local variable):
<pre>
var ::= Name
</pre><p>
Name denotes identifiers, as defined in <a href="#2.1">§2.1</a>.
<p>
Any variable is assumed to be global unless explicitly declared
as a local (see <a href="#2.4.7">§2.4.7</a>).
Local variables are <em>lexically scoped</em>:
local variables can be freely accessed by functions
defined inside their scope (see <a href="#2.6">§2.6</a>).
<p>
Before the first assignment to a variable, its value is <b>nil</b>.
<p>
Square brackets are used to index a table:
<pre>
var ::= prefixexp `<b>[</b>´ exp `<b>]</b>´
</pre><p>
The meaning of accesses to global variables
and table fields can be changed via metatables.
An access to an indexed variable <code>t[i]</code> is equivalent to
a call <code>gettable_event(t,i)</code>.
(See <a href="#2.8">§2.8</a> for a complete description of the
<code>gettable_event</code> function.
This function is not defined or callable in Lua.
We use it here only for explanatory purposes.)
<p>
The syntax <code>var.Name</code> is just syntactic sugar for
<code>var["Name"]</code>:
<pre>
var ::= prefixexp `<b>.</b>´ Name
</pre>
<p>
All global variables live as fields in ordinary Lua tables,
called <em>environment tables</em> or simply
<em>environments</em> (see <a href="#2.9">§2.9</a>).
Each function has its own reference to an environment,
so that all global variables in this function
will refer to this environment table.
When a function is created,
it inherits the environment from the function that created it.
To get the environment table of a Lua function,
you call <a href="#pdf-getfenv"><code>getfenv</code></a>.
To replace it,
you call <a href="#pdf-setfenv"><code>setfenv</code></a>.
(You can only manipulate the environment of C functions
through the debug library; (see <a href="#5.9">§5.9</a>).)
<p>
An access to a global variable <code>x</code>
is equivalent to <code>_env.x</code>,
which in turn is equivalent to
<pre>
gettable_event(_env, "x")
</pre><p>
where <code>_env</code> is the environment of the running function.
(See <a href="#2.8">§2.8</a> for a complete description of the
<code>gettable_event</code> function.
This function is not defined or callable in Lua.
Similarly, the <code>_env</code> variable is not defined in Lua.
We use them here only for explanatory purposes.)
<h2>2.4 - <a name="2.4">Statements</a></h2>
<p>
Lua supports an almost conventional set of statements,
similar to those in Pascal or C.
This set includes
assignments, control structures, function calls,
and variable declarations.
<h3>2.4.1 - <a name="2.4.1">Chunks</a></h3>
<p>
The unit of execution of Lua is called a <em>chunk</em>.
A chunk is simply a sequence of statements,
which are executed sequentially.
Each statement can be optionally followed by a semicolon:
<pre>
chunk ::= {stat [`<b>;</b>´]}
</pre><p>
There are no empty statements and thus '<code>;;</code>' is not legal.
<p>
Lua handles a chunk as the body of an anonymous function
with a variable number of arguments
(see <a href="#2.5.9">§2.5.9</a>).
As such, chunks can define local variables,
receive arguments, and return values.
<p>
A chunk can be stored in a file or in a string inside the host program.
To execute a chunk,
Lua first pre-compiles the chunk into instructions for a virtual machine,
and then it executes the compiled code
with an interpreter for the virtual machine.
<p>
Chunks can also be pre-compiled into binary form;
see program <code>luac</code> for details.
Programs in source and compiled forms are interchangeable;
Lua automatically detects the file type and acts accordingly.
<h3>2.4.2 - <a name="2.4.2">Blocks</a></h3><p>
A block is a list of statements;
syntactically, a block is the same as a chunk:
<pre>
block ::= chunk
</pre>
<p>
A block can be explicitly delimited to produce a single statement:
<pre>
stat ::= <b>do</b> block <b>end</b>
</pre><p>
Explicit blocks are useful
to control the scope of variable declarations.
Explicit blocks are also sometimes used to
add a <b>return</b> or <b>break</b> statement in the middle
of another block (see <a href="#2.4.4">§2.4.4</a>).
<h3>2.4.3 - <a name="2.4.3">Assignment</a></h3>
<p>
Lua allows multiple assignments.
Therefore, the syntax for assignment
defines a list of variables on the left side
and a list of expressions on the right side.
The elements in both lists are separated by commas:
<pre>
stat ::= varlist `<b>=</b>´ explist
varlist ::= var {`<b>,</b>´ var}
explist ::= exp {`<b>,</b>´ exp}
</pre><p>
Expressions are discussed in <a href="#2.5">§2.5</a>.
<p>
Before the assignment,
the list of values is <em>adjusted</em> to the length of
the list of variables.
If there are more values than needed,
the excess values are thrown away.
If there are fewer values than needed,
the list is extended with as many <b>nil</b>'s as needed.
If the list of expressions ends with a function call,
then all values returned by that call enter the list of values,
before the adjustment
(except when the call is enclosed in parentheses; see <a href="#2.5">§2.5</a>).
<p>
The assignment statement first evaluates all its expressions
and only then are the assignments performed.
Thus the code
<pre>
i = 3
i, a[i] = i+1, 20
</pre><p>
sets <code>a[3]</code> to 20, without affecting <code>a[4]</code>
because the <code>i</code> in <code>a[i]</code> is evaluated (to 3)
before it is assigned 4.
Similarly, the line
<pre>
x, y = y, x
</pre><p>
exchanges the values of <code>x</code> and <code>y</code>,
and
<pre>
x, y, z = y, z, x
</pre><p>
cyclically permutes the values of <code>x</code>, <code>y</code>, and <code>z</code>.
<p>
The meaning of assignments to global variables
and table fields can be changed via metatables.
An assignment to an indexed variable <code>t[i] = val</code> is equivalent to
<code>settable_event(t,i,val)</code>.
(See <a href="#2.8">§2.8</a> for a complete description of the
<code>settable_event</code> function.
This function is not defined or callable in Lua.
We use it here only for explanatory purposes.)
<p>
An assignment to a global variable <code>x = val</code>
is equivalent to the assignment
<code>_env.x = val</code>,
which in turn is equivalent to
<pre>
settable_event(_env, "x", val)
</pre><p>
where <code>_env</code> is the environment of the running function.
(The <code>_env</code> variable is not defined in Lua.
We use it here only for explanatory purposes.)
<h3>2.4.4 - <a name="2.4.4">Control Structures</a></h3><p>
The control structures
<b>if</b>, <b>while</b>, and <b>repeat</b> have the usual meaning and
familiar syntax:
<pre>
stat ::= <b>while</b> exp <b>do</b> block <b>end</b>
stat ::= <b>repeat</b> block <b>until</b> exp
stat ::= <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b>
</pre><p>
Lua also has a <b>for</b> statement, in two flavors (see <a href="#2.4.5">§2.4.5</a>).
<p>
The condition expression of a
control structure can return any value.
Both <b>false</b> and <b>nil</b> are considered false.
All values different from <b>nil</b> and <b>false</b> are considered true
(in particular, the number 0 and the empty string are also true).
<p>
In the <b>repeat</b>–<b>until</b> loop,
the inner block does not end at the <b>until</b> keyword,
but only after the condition.
So, the condition can refer to local variables
declared inside the loop block.
<p>
The <b>return</b> statement is used to return values
from a function or a chunk (which is just a function).
Functions and chunks can return more than one value,
and so the syntax for the <b>return</b> statement is
<pre>
stat ::= <b>return</b> [explist]
</pre>
<p>
The <b>break</b> statement is used to terminate the execution of a
<b>while</b>, <b>repeat</b>, or <b>for</b> loop,
skipping to the next statement after the loop:
<pre>
stat ::= <b>break</b>
</pre><p>
A <b>break</b> ends the innermost enclosing loop.
<p>
The <b>return</b> and <b>break</b>
statements can only be written as the <em>last</em> statement of a block.
If it is really necessary to <b>return</b> or <b>break</b> in the
middle of a block,
then an explicit inner block can be used,
as in the idioms
<code>do return end</code> and <code>do break end</code>,
because now <b>return</b> and <b>break</b> are the last statements in
their (inner) blocks.
<h3>2.4.5 - <a name="2.4.5">For Statement</a></h3>
<p>
The <b>for</b> statement has two forms:
one numeric and one generic.
<p>
The numeric <b>for</b> loop repeats a block of code while a
control variable runs through an arithmetic progression.
It has the following syntax:
<pre>
stat ::= <b>for</b> Name `<b>=</b>´ exp `<b>,</b>´ exp [`<b>,</b>´ exp] <b>do</b> block <b>end</b>
</pre><p>
The <em>block</em> is repeated for <em>name</em> starting at the value of
the first <em>exp</em>, until it passes the second <em>exp</em> by steps of the
third <em>exp</em>.
More precisely, a <b>for</b> statement like
<pre>
for v = <em>e1</em>, <em>e2</em>, <em>e3</em> do <em>block</em> end
</pre><p>
is equivalent to the code:
<pre>
do
local <em>var</em>, <em>limit</em>, <em>step</em> = tonumber(<em>e1</em>), tonumber(<em>e2</em>), tonumber(<em>e3</em>)
if not (<em>var</em> and <em>limit</em> and <em>step</em>) then error() end
while (<em>step</em> > 0 and <em>var</em> <= <em>limit</em>) or (<em>step</em> <= 0 and <em>var</em> >= <em>limit</em>) do
local v = <em>var</em>
<em>block</em>
<em>var</em> = <em>var</em> + <em>step</em>
end
end
</pre><p>
Note the following:
<ul>
<li>
All three control expressions are evaluated only once,
before the loop starts.
They must all result in numbers.
</li>
<li>
<code><em>var</em></code>, <code><em>limit</em></code>, and <code><em>step</em></code> are invisible variables.
The names shown here are for explanatory purposes only.
</li>
<li>
If the third expression (the step) is absent,
then a step of 1 is used.
</li>
<li>
You can use <b>break</b> to exit a <b>for</b> loop.
</li>
<li>
The loop variable <code>v</code> is local to the loop;
you cannot use its value after the <b>for</b> ends or is broken.
If you need this value,
assign it to another variable before breaking or exiting the loop.
</li>
</ul>
<p>
The generic <b>for</b> statement works over functions,
called <em>iterators</em>.
On each iteration, the iterator function is called to produce a new value,
stopping when this new value is <b>nil</b>.
The generic <b>for</b> loop has the following syntax:
<pre>
stat ::= <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b>
namelist ::= Name {`<b>,</b>´ Name}
</pre><p>
A <b>for</b> statement like
<pre>
for <em>var_1</em>, ···, <em>var_n</em> in <em>explist</em> do <em>block</em> end
</pre><p>
is equivalent to the code:
<pre>
do
local <em>f</em>, <em>s</em>, <em>var</em> = <em>explist</em>
while true do
local <em>var_1</em>, ···, <em>var_n</em> = <em>f</em>(<em>s</em>, <em>var</em>)
<em>var</em> = <em>var_1</em>
if <em>var</em> == nil then break end
<em>block</em>
end
end
</pre><p>
Note the following:
<ul>
<li>
<code><em>explist</em></code> is evaluated only once.
Its results are an <em>iterator</em> function,
a <em>state</em>,
and an initial value for the first <em>iterator variable</em>.
</li>
<li>
<code><em>f</em></code>, <code><em>s</em></code>, and <code><em>var</em></code> are invisible variables.
The names are here for explanatory purposes only.
</li>
<li>
You can use <b>break</b> to exit a <b>for</b> loop.
</li>
<li>
The loop variables <code><em>var_i</em></code> are local to the loop;
you cannot use their values after the <b>for</b> ends.
If you need these values,
then assign them to other variables before breaking or exiting the loop.
</li>
</ul>
<h3>2.4.6 - <a name="2.4.6">Function Calls as Statements</a></h3><p>
To allow possible side-effects,
function calls can be executed as statements:
<pre>
stat ::= functioncall
</pre><p>
In this case, all returned values are thrown away.
Function calls are explained in <a href="#2.5.8">§2.5.8</a>.
<h3>2.4.7 - <a name="2.4.7">Local Declarations</a></h3><p>
Local variables can be declared anywhere inside a block.
The declaration can include an initial assignment:
<pre>
stat ::= <b>local</b> namelist [`<b>=</b>´ explist]
</pre><p>
If present, an initial assignment has the same semantics
of a multiple assignment (see <a href="#2.4.3">§2.4.3</a>).
Otherwise, all variables are initialized with <b>nil</b>.
<p>
A chunk is also a block (see <a href="#2.4.1">§2.4.1</a>),
and so local variables can be declared in a chunk outside any explicit block.
The scope of such local variables extends until the end of the chunk.
<p>
The visibility rules for local variables are explained in <a href="#2.6">§2.6</a>.
<h2>2.5 - <a name="2.5">Expressions</a></h2>
<p>
The basic expressions in Lua are the following:
<pre>
exp ::= prefixexp
exp ::= <b>nil</b> | <b>false</b> | <b>true</b>
exp ::= Number
exp ::= String
exp ::= function
exp ::= tableconstructor
exp ::= `<b>...</b>´
exp ::= exp binop exp
exp ::= unop exp
prefixexp ::= var | functioncall | `<b>(</b>´ exp `<b>)</b>´
</pre>
<p>
Numbers and literal strings are explained in <a href="#2.1">§2.1</a>;
variables are explained in <a href="#2.3">§2.3</a>;
function definitions are explained in <a href="#2.5.9">§2.5.9</a>;
function calls are explained in <a href="#2.5.8">§2.5.8</a>;
table constructors are explained in <a href="#2.5.7">§2.5.7</a>.
Vararg expressions,
denoted by three dots ('<code>...</code>'), can only be used when
directly inside a vararg function;
they are explained in <a href="#2.5.9">§2.5.9</a>.
<p>
Binary operators comprise arithmetic operators (see <a href="#2.5.1">§2.5.1</a>),
relational operators (see <a href="#2.5.2">§2.5.2</a>), logical operators (see <a href="#2.5.3">§2.5.3</a>),
and the concatenation operator (see <a href="#2.5.4">§2.5.4</a>).
Unary operators comprise the unary minus (see <a href="#2.5.1">§2.5.1</a>),
the unary <b>not</b> (see <a href="#2.5.3">§2.5.3</a>),
and the unary <em>length operator</em> (see <a href="#2.5.5">§2.5.5</a>).
<p>
Both function calls and vararg expressions can result in multiple values.
If an expression is used as a statement
(only possible for function calls (see <a href="#2.4.6">§2.4.6</a>)),
then its return list is adjusted to zero elements,
thus discarding all returned values.
If an expression is used as the last (or the only) element
of a list of expressions,
then no adjustment is made
(unless the call is enclosed in parentheses).
In all other contexts,
Lua adjusts the result list to one element,
discarding all values except the first one.
<p>
Here are some examples:
<pre>
f() -- adjusted to 0 results
g(f(), x) -- f() is adjusted to 1 result
g(x, f()) -- g gets x plus all results from f()
a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil)
a,b = ... -- a gets the first vararg parameter, b gets
-- the second (both a and b can get nil if there
-- is no corresponding vararg parameter)
a,b,c = x, f() -- f() is adjusted to 2 results
a,b,c = f() -- f() is adjusted to 3 results
return f() -- returns all results from f()
return ... -- returns all received vararg parameters
return x,y,f() -- returns x, y, and all results from f()
{f()} -- creates a list with all results from f()
{...} -- creates a list with all vararg parameters
{f(), nil} -- f() is adjusted to 1 result
</pre>
<p>
Any expression enclosed in parentheses always results in only one value.
Thus,
<code>(f(x,y,z))</code> is always a single value,
even if <code>f</code> returns several values.
(The value of <code>(f(x,y,z))</code> is the first value returned by <code>f</code>
or <b>nil</b> if <code>f</code> does not return any values.)
<h3>2.5.1 - <a name="2.5.1">Arithmetic Operators</a></h3><p>
Lua supports the usual arithmetic operators:
the binary <code>+</code> (addition),
<code>-</code> (subtraction), <code>*</code> (multiplication),
<code>/</code> (division), <code>%</code> (modulo), and <code>^</code> (exponentiation);
and unary <code>-</code> (negation).
If the operands are numbers, or strings that can be converted to
numbers (see <a href="#2.2.1">§2.2.1</a>),
then all operations have the usual meaning.
Exponentiation works for any exponent.
For instance, <code>x^(-0.5)</code> computes the inverse of the square root of <code>x</code>.
Modulo is defined as
<pre>
a % b == a - math.floor(a/b)*b
</pre><p>
That is, it is the remainder of a division that rounds
the quotient towards minus infinity.
<h3>2.5.2 - <a name="2.5.2">Relational Operators</a></h3><p>
The relational operators in Lua are
<pre>
== ~= < > <= >=
</pre><p>
These operators always result in <b>false</b> or <b>true</b>.
<p>
Equality (<code>==</code>) first compares the type of its operands.
If the types are different, then the result is <b>false</b>.
Otherwise, the values of the operands are compared.
Numbers and strings are compared in the usual way.
Objects (tables, userdata, threads, and functions)
are compared by <em>reference</em>:
two objects are considered equal only if they are the <em>same</em> object.
Every time you create a new object
(a table, userdata, thread, or function),
this new object is different from any previously existing object.
<p>