annotate website/src/m.html.luan @ 1661:08177ced7fa0

add file.copy_to
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 02 May 2022 18:56:55 -0600
parents 2968e43cdd44
children c55373c3a0ce
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1656
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local Io = require "luan:Io.luan"
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local Http = require "luan:http/Http.luan"
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local Shared = require "site:/lib/Shared.luan"
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local head = Shared.head or error()
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local docs_header = Shared.docs_header or error()
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local show_toc = Shared.show_toc or error()
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local show_content = Shared.show_content or error()
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local content = {
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 intro = {
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 title = "Introduction"
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 content = function()
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 %>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 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.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 Luan is implemented in Java and is tightly coupled with Java. So it makes a great scripting language for Java programmers.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 Unlike Lua which is meant to be embedded, Luan is meant to be a full scripting language. This done not by adding features to Luan, but rather by providing a complete set of libraries.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 <%
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 end
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 }
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 basic = {
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 title = "Basic Concepts"
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 content = function()
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 %>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 This section describes the basic concepts of the language.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 <%
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 end
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 subs = {
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 types = {
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 title = "Values and Types"
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 content = function()
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 %>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 Luan is a <em>dynamically typed language</em>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 This means that
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 variables do not have types; only values do.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 There are no type definitions in the language.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 All values carry their own type.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 All values in Luan are <em>first-class values</em>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 This means that all values can be stored in variables,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 passed as arguments to other functions, and returned as results.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 There are eight basic types in Luan:
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 <em>nil</em>, <em>boolean</em>, <em>number</em>,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 <em>string</em>, <em>binary</em>, <em>function</em>, <em>java</em>,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 and <em>table</em>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 <em>Nil</em> is the type of the value <b>nil</b>,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 whose main property is to be different from any other value;
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 it usually represents the absence of a useful value.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 <em>Nil</em> is implemented as the Java value <em>null</em>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 <em>Boolean</em> is the type of the values <b>false</b> and <b>true</b>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 <em>Boolean</em> is implemented as the Java class <em>Boolean</em>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 <em>Number</em> represents both
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 integer numbers and real (floating-point) numbers.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 <em>Number</em> is implemented as the Java class <em>Number</em>. Any Java subclass of <em>Number</em> is allowed and this is invisible to the Luan user. Operations on numbers follow the same rules of
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 the underlying Java implementation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 <em>String</em> is implemented as the Java class <em>String</em>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 <em>Binary</em> is implemented as the Java type <em>byte[]</em>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 Luan can call (and manipulate) functions written in Luan and
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 functions written in Java (see <a href="#fn_calls">Function Calls</a>).
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 Both are represented by the type <em>function</em>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 The type <em>java</em> is provided to allow arbitrary Java objects to
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 be stored in Luan variables.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 A <em>java</em> value is a Java object that isn't one of the standard Luan types.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 Java values have no predefined operations in Luan,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 except assignment and identity test.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 Java values are useful when Java access is enabled in Luan.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94 The type <em>table</em> implements associative arrays,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 that is, arrays that can be indexed not only with numbers,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 but with any Luan value except <b>nil</b>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97 Tables can be <em>heterogeneous</em>;
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98 that is, they can contain values of all types (except <b>nil</b>).
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99 Any key with value <b>nil</b> is not considered part of the table.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100 Conversely, any key that is not part of a table has
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 an associated value <b>nil</b>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 Tables are the sole data-structuring mechanism in Luan;
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 they can be used to represent ordinary arrays, sequences,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 symbol tables, sets, records, graphs, trees, etc.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108 To represent records, Luan uses the field name as an index.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109 The language supports this representation by
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 providing <code>a.name</code> as syntactic sugar for <code>a["name"]</code>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 There are several convenient ways to create tables in Luan
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 (see <a href="#constructors">Table Constructors</a>).
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 We use the term <em>sequence</em> to denote a table where
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117 the set of all positive numeric keys is equal to {1..<em>n</em>}
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 for some non-negative integer <em>n</em>,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119 which is called the length of the sequence (see <a href="#length">The Length Operator</a>).
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
121
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123 Like indices,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
124 the values of table fields can be of any type.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 In particular,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 because functions are first-class values,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
127 table fields can contain functions.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
128 Thus tables can also carry <em>methods</em> (see <a href="#fn_def">Function Definitions</a>).
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
132 The indexing of tables follows
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
133 the definition of raw equality in the language.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
134 The expressions <code>a[i]</code> and <code>a[j]</code>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
135 denote the same table element
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
136 if and only if <code>i</code> and <code>j</code> are raw equal
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
137 (that is, equal without metamethods).
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
138 In particular, floats with integral values
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
139 are equal to their respective integers
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
140 (e.g., <code>1.0 == 1</code>).
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
141 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
142
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
143 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
144 Luan values are <em>objects</em>:
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
145 variables do not actually <em>contain</em> values,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
146 only <em>references</em> to them.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
147 Assignment, parameter passing, and function returns
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
148 always manipulate references to values;
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
149 these operations do not imply any kind of copy.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
150 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
151
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
152 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
153 The library function <a href="#Luan.type"><code>Luan.type</code></a> returns a string describing the type
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
154 of a given value.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
155 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
156 <%
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
157 end
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
158 }
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
159 env = {
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
160 title = "Environments"
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
161 content = function()
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
162 %>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
163 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
164 The environment of a chunk starts with only one local variable: <code><a href="#require">require</a></code>. This function is used to load and access libraries and other modules. All other variables must be added to the environment using <a href="http://localhost:8080/manual.html#local_stmt">local declarations</a>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
165 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
166
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
167 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
168 As will be discussed in <a href="#vars">Variables</a> and <a href=#assignment">Assignment</a>,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
169 any reference to a free name
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
170 (that is, a name not bound to any declaration) <code>var</code>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
171 can be syntactically translated to <code>_ENV.var</code> if <code>_ENV</code> is defined.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
172 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
173 <%
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
174 end
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
175 }
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
176 error = {
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
177 title = "Error Handling"
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
178 content = function()
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
179 %>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
180 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
181 Luan code can explicitly generate an error by calling the
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
182 <a href="#Luan.error"><code>error</code></a> function.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
183 If you need to catch errors in Luan,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
184 you can use the <a href="#try">Try Statement</code></a>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
185 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
186
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
187 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
188 Whenever there is an error,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
189 an <em>error table</em>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
190 is propagated with information about the error.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
191 See <a href="#Luan.new_error"><code>Luan.new_error</code></a>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
192 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
193 <%
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
194 end
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
195 }
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
196 meta = {
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
197 title = "Metatables and Metamethods"
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
198 content = function()
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
199 %>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
200 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
201 Every table in Luan can have a <em>metatable</em>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
202 This <em>metatable</em> is an ordinary Luan table
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
203 that defines the behavior of the original value
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
204 under certain special operations.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
205 You can change several aspects of the behavior
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
206 of operations over a value by setting specific fields in its metatable.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
207 For instance, when a table is the operand of an addition,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
208 Luan checks for a function in the field "<code>__add</code>" of the table's metatable.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
209 If it finds one,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
210 Luan calls this function to perform the addition.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
211 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
212
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
213 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
214 The keys in a metatable are derived from the <em>event</em> names;
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
215 the corresponding values are called <ii>metamethods</em>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
216 In the previous example, the event is <code>"add"</code>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
217 and the metamethod is the function that performs the addition.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
218 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
219
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
220 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
221 You can query the metatable of any table
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
222 using the <a href="#Luan.get_metatable"><code>get_metatable</code></a> function.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
223 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
224
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
225 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
226 You can replace the metatable of tables
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
227 using the <a href="#Luan.set_metatable"><code>set_metatable</code></a> function.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
228 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
229
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
230 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
231 A metatable controls how a table behaves in
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
232 arithmetic operations, bitwise operations,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
233 order comparisons, concatenation, length operation, calls, and indexing.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
234 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
235
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
236 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
237 A detailed list of events controlled by metatables is given next.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
238 Each operation is identified by its corresponding event name.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
239 The key for each event is a string with its name prefixed by
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
240 two underscores, '<code>__</code>';
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
241 for instance, the key for operation "add" is the
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
242 string "<code>__add</code>".
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
243 Note that queries for metamethods are always raw;
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
244 the access to a metamethod does not invoke other metamethods.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
245 You can emulate how Luan queries a metamethod for an object <code>obj</code>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
246 with the following code:
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
247 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
248
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
249 <pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
250 raw_get(get_metatable(obj) or {}, "__" .. event_name)
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
251 </pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
252
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
253 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
254 Here are the events:
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
255 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
256
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
257 <ul>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
258
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
259 <li><p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
260 <b>"add": </b>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
261 the <code>+</code> operation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
262
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
263 If any operand for an addition is a table,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
264 Luan will try to call a metamethod.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
265 First, Luan will check the first operand (even if it is valid).
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
266 If that operand does not define a metamethod for the "<code>__add</code>" event,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
267 then Luan will check the second operand.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
268 If Luan can find a metamethod,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
269 it calls the metamethod with the two operands as arguments,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
270 and the result of the call
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
271 (adjusted to one value)
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
272 is the result of the operation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
273 Otherwise,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
274 it raises an error.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
275 </p></li>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
276
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
277 <li><p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
278 <b>"sub": </b>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
279 the <code>-</code> operation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
280 Behavior similar to the "add" operation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
281 </li>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
282
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
283 <li><p><b>"mul": </b>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
284 the <code>*</code> operation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
285 Behavior similar to the "add" operation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
286 </p></li>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
287
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
288 <li><p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
289 <b>"div": </b>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
290 the <code>/</code> operation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
291 Behavior similar to the "add" operation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
292 </p></li>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
293
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
294 <li><p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
295 <b>"mod": </b>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
296 the <code>%</code> operation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
297 Behavior similar to the "add" operation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
298 </p></li>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
299
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
300 <li><p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
301 <b>"pow": </b>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
302 the <code>^</code> (exponentiation) operation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
303 Behavior similar to the "add" operation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
304 </p></li>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
305
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
306 <li><p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
307 <b>"unm": </b>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
308 the <code>-</code> (unary minus) operation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
309 Behavior similar to the "add" operation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
310 </p></li>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
311
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
312 <li><p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
313 <b>"concat": </b>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
314 the <code>..</code> (concatenation) operation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
315 Behavior similar to the "add" operation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
316 </p></li>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
317
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
318 <li><p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
319 <b>"len": </b>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
320 the <code>#</code> (length) operation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
321 If there is a metamethod,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
322 Luan calls it with the object as argument,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
323 and the result of the call
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
324 (always adjusted to one value)
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
325 is the result of the operation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
326 If there is no metamethod but the object is a table,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
327 then Luan uses the table length operation (see <a href="#length">The Length Operator</a>).
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
328 Otherwise, Luan raises an error.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
329 </p></li>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
330
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
331 <li><p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
332 <b>"eq": </b>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
333 the <code>==</code> (equal) operation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
334 Behavior similar to the "add" operation,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
335 except that Luan will try a metamethod only when the values
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
336 being compared are both tables
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
337 and they are not primitively equal.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
338 The result of the call is always converted to a boolean.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
339 </p></li>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
340
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
341 <li><p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
342 <b>"lt": </b>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
343 the <code>&lt;</code> (less than) operation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
344 Behavior similar to the "add" operation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
345 The result of the call is always converted to a boolean.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
346 </p></li>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
347
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
348 <li><p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
349 <b>"le": </b>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
350 the <code>&lt;=</code> (less equal) operation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
351 Unlike other operations,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
352 The less-equal operation can use two different events.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
353 First, Luan looks for the "<code>__le</code>" metamethod in both operands,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
354 like in the "lt" operation.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
355 If it cannot find such a metamethod,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
356 then it will try the "<code>__lt</code>" event,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
357 assuming that <code>a &lt;= b</code> is equivalent to <code>not (b &lt; a)</code>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
358 As with the other comparison operators,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
359 the result is always a boolean.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
360 </p></li>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
361
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
362 <li>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
363 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
364 <b>"index": </b>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
365 The indexing access <code>table[key]</code>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
366 This event happens
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
367 when <code>key</code> is not present in <code>table</code>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
368 The metamethod is looked up in <code>table</code>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
369 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
370
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
371 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
372 Despite the name,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
373 the metamethod for this event can be any type.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
374 If it is a function,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
375 it is called with <code>table</code> and <code>key</code> as arguments.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
376 Otherwise
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
377 the final result is the result of indexing this metamethod object with <code>key</code>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
378 (This indexing is regular, not raw,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
379 and therefore can trigger another metamethod if the metamethod object is a table.)
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
380 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
381 </li>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
382
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
383 <li>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
384 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
385 <b>"new_index": </b>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
386 The indexing assignment <code>table[key] = value</code>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
387 Like the index event,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
388 this event happens when
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
389 when <code>key</code> is not present in <code>table</code>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
390 The metamethod is looked up in <code>table</code>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
391 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
392
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
393 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
394 Like with indexing,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
395 the metamethod for this event can be either a function or a table.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
396 If it is a function,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
397 it is called with <code>table</code>, <code>key</code>, and <code>value</code> as arguments.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
398 If it is a table,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
399 Luan does an indexing assignment to this table with the same key and value.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
400 (This assignment is regular, not raw,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
401 and therefore can trigger another metamethod.)
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
402 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
403
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
404 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
405 Whenever there is a "new_index" metamethod,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
406 Luan does not perform the primitive assignment.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
407 (If necessary,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
408 the metamethod itself can call <a href="#Luan.raw_set"><code>raw_set</code></a>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
409 to do the assignment.)
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
410 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
411 </li>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
412
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
413 <li><p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
414 <b>"gc":</b>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
415 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.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
416 </p></li>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
417
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
418 </ul>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
419 <%
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
420 end
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
421 }
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
422 gc = {
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
423 title = "Garbage Collection"
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
424 content = function()
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
425 %>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
426 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
427 Luan uses Java's garbage collection.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
428 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
429 <%
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
430 end
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
431 }
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
432 }
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
433 }
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
434 lang = {
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
435 title = "The Language"
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
436 content = function()
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
437 %>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
438 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
439 This section describes the lexis, the syntax, and the semantics of Luan.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
440 In other words,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
441 this section describes
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
442 which tokens are valid,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
443 how they can be combined,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
444 and what their combinations mean.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
445 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
446
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
447 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
448 Language constructs will be explained using the usual extended BNF notation,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
449 in which
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
450 {<em>a</em>}&nbsp;means&nbsp;0 or more <em>a</em>'s, and
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
451 [<em>a</em>]&nbsp;means an optional <em>a</em>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
452 Non-terminals are shown like non-terminal,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
453 keywords are shown like <b>kword</b>,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
454 and other terminal symbols are shown like &lsquo;<b>=</b>&rsquo;.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
455 The complete syntax of Luan can be found in <a href="#9">&sect;9</a>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
456 at the end of this manual.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
457 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
458 <%
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
459 end
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
460 subs = {
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
461 lex = {
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
462 title = "Lexical Conventions"
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
463 content = function()
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
464 %>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
465 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
466 Luan ignores spaces and comments
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
467 between lexical elements (tokens),
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
468 except as delimiters between names and keywords.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
469 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.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
470 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
471
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
472 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
473 <em>Names</em>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
474 (also called <em>identifiers</em>)
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
475 in Luan can be any string of letters,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
476 digits, and underscores,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
477 not beginning with a digit.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
478 Identifiers are used to name variables, table fields, and labels.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
479 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
480
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
481 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
482 The following <em>keywords</em> are reserved
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
483 and cannot be used as names:
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
484 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
485
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
486 <p keywords>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
487 <span>and</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
488 <span>break</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
489 <span>catch</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
490 <span>continue</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
491 <span>do</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
492 <span>else</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
493 <span>elseif</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
494 <span>end_do</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
495 <span>end_for</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
496 <span>end_function</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
497 <span>end_if</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
498 <span>end_try</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
499 <span>end_while</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
500 <span>false</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
501 <span>finally</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
502 <span>for</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
503 <span>function</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
504 <span>if</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
505 <span>in</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
506 <span>local</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
507 <span>nil</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
508 <span>not</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
509 <span>or</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
510 <span>repeat</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
511 <span>return</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
512 <span>then</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
513 <span>true</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
514 <span>try</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
515 <span>until</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
516 <span>while</span>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
517 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
518
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
519 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
520 Luan is a case-sensitive language:
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
521 <code>and</code> is a reserved word, but <code>And</code> and <code>AND</code>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
522 are two different, valid names.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
523 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
524
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
525 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
526 The following strings denote other tokens:
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
527 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
528
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
529 <pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
530 + - * / % ^ #
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
531 &amp; ~ | &lt;&lt; &gt;&gt; //
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
532 == ~= &lt;= &gt;= &lt; &gt; =
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
533 ( ) { } [ ] ::
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
534 ; : , . .. ...
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
535 </pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
536
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
537 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
538 <em>Literal strings</em>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
539 can be delimited by matching single or double quotes,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
540 and can contain the following C-like escape sequences:
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
541 '<code>\a</code>' (bell),
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
542 '<code>\b</code>' (backspace),
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
543 '<code>\f</code>' (form feed),
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
544 '<code>\n</code>' (newline),
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
545 '<code>\r</code>' (carriage return),
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
546 '<code>\t</code>' (horizontal tab),
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
547 '<code>\v</code>' (vertical tab),
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
548 '<code>\\</code>' (backslash),
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
549 '<code>\"</code>' (quotation mark [double quote]),
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
550 and '<code>\'</code>' (apostrophe [single quote]).
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
551 A backslash followed by a real newline
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
552 results in a newline in the string.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
553 The escape sequence '<code>\z</code>' skips the following span
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
554 of white-space characters,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
555 including line breaks;
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
556 it is particularly useful to break and indent a long literal string
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
557 into multiple lines without adding the newlines and spaces
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
558 into the string contents.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
559 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
560
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
561 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
562 Luan can specify any character in a literal string by its numerical value.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
563 This can be done
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
564 with the escape sequence <code>\x<em>XX</em></code>,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
565 where <em>XX</em> is a sequence of exactly two hexadecimal digits,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
566 or with the escape sequence <code>\u<em>XXXX</em></code>,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
567 where <em>XXXX</em> is a sequence of exactly four hexadecimal digits,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
568 or with the escape sequence <code>\<em>ddd</em></code>,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
569 where <em>ddd</em> is a sequence of up to three decimal digits.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
570 (Note that if a decimal escape sequence is to be followed by a digit,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
571 it must be expressed using exactly three digits.)
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
572 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
573
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
574 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
575 Literal strings can also be defined using a long format
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
576 enclosed by <em>long brackets</em>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
577 We define an <em>opening long bracket of level <em>n</em></em> as an opening
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
578 square bracket followed by <em>n</em> equal signs followed by another
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
579 opening square bracket.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
580 So, an opening long bracket of level 0 is written as <code>[[</code>,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
581 an opening long bracket of level 1 is written as <code>[=[</code>,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
582 and so on.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
583 A <em>closing long bracket</em> is defined similarly;
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
584 for instance,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
585 a closing long bracket of level 4 is written as <code>]====]</code>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
586 A <em>long literal</em> starts with an opening long bracket of any level and
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
587 ends at the first closing long bracket of the same level.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
588 It can contain any text except a closing bracket of the same level.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
589 Literals in this bracketed form can run for several lines,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
590 do not interpret any escape sequences,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
591 and ignore long brackets of any other level.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
592 Any kind of end-of-line sequence
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
593 (carriage return, newline, carriage return followed by newline,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
594 or newline followed by carriage return)
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
595 is converted to a simple newline.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
596 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
597
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
598 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
599 Any character in a literal string not
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
600 explicitly affected by the previous rules represents itself.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
601 However, Luan opens files for parsing in text mode,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
602 and the system file functions may have problems with
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
603 some control characters.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
604 So, it is safer to represent
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
605 non-text data as a quoted literal with
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
606 explicit escape sequences for non-text characters.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
607 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
608
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
609 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
610 For convenience,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
611 when the opening long bracket is immediately followed by a newline,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
612 the newline is not included in the string.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
613 As an example
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
614 the five literal strings below denote the same string:
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
615 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
616
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
617 <pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
618 a = 'alo\n123"'
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
619 a = "alo\n123\""
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
620 a = '\97lo\10\04923"'
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
621 a = [[alo
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
622 123"]]
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
623 a = [==[
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
624 alo
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
625 123"]==]
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
626 </pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
627
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
628 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
629 A <em>numerical constant</em> (or <em>numeral</em>)
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
630 can be written with an optional fractional part
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
631 and an optional decimal exponent,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
632 marked by a letter '<code>e</code>' or '<code>E</code>'.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
633 Luan also accepts hexadecimal constants,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
634 which start with <code>0x</code> or <code>0X</code>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
635 Hexadecimal constants also accept an optional fractional part
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
636 plus an optional binary exponent,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
637 marked by a letter '<code>p</code>' or '<code>P</code>'.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
638 A numeric constant with a fractional dot or an exponent
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
639 denotes a float;
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
640 otherwise it denotes an integer.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
641 Examples of valid integer constants are
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
642 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
643
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
644 <pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
645 3 345 0xff 0xBEBADA
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
646 </pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
647
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
648 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
649 Examples of valid float constants are
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
650 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
651
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
652 <pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
653 3.0 3.1416 314.16e-2 0.31416E1 34e1
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
654 0x0.1E 0xA23p-4 0X1.921FB54442D18P+1
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
655 </pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
656
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
657 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
658 A <em>comment</em> starts with a double hyphen (<code>--</code>)
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
659 anywhere outside a string.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
660 If the text immediately after <code>--</code> is not an opening long bracket,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
661 the comment is a <em>short comment</em>,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
662 which runs until the end of the line.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
663 Otherwise, it is a <em>long comment</em>,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
664 which runs until the corresponding closing long bracket.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
665 Long comments are frequently used to disable code temporarily.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
666 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
667 <%
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
668 end
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
669 }
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
670 vars = {
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
671 title = "Variables"
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
672 content = function()
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
673 %>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
674 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
675 Variables are places that store values.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
676 There are three kinds of variables in Luan:
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
677 global variables, local variables, and table fields.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
678 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
679
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
680 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
681 A single name can denote a global variable or a local variable
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
682 (or a function's formal parameter,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
683 which is a particular kind of local variable):
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
684 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
685
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
686 <pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
687 var ::= Name
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
688 </pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
689
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
690 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
691 Name denotes identifiers, as defined in <a href="#lex">Lexical Conventions</a>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
692 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
693
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
694 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
695 Local variables are <em>lexically scoped</em>:
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
696 local variables can be freely accessed by functions
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
697 defined inside their scope (see <a href="#visibility">Visibility Rules</a>).
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
698 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
699
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
700 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
701 Before the first assignment to a variable, its value is <b>nil</b>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
702 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
703
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
704 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
705 Square brackets are used to index a table:
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
706 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
707
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
708 <pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
709 var ::= prefixexp &lsquo;<b>[</b>&rsquo; exp &lsquo;<b>]</b>&rsquo;
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
710 </pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
711
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
712 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
713 The meaning of accesses to table fields can be changed via metatables.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
714 An access to an indexed variable <code>t[i]</code> is equivalent to
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
715 a call <code>gettable_event(t,i)</code>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
716 (See <a href="#meta">Metatables and Metamethods</a> for a complete description of the
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
717 <code>gettable_event</code> function.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
718 This function is not defined or callable in Luan.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
719 We use it here only for explanatory purposes.)
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
720 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
721
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
722 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
723 The syntax <code>var.Name</code> is just syntactic sugar for
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
724 <code>var["Name"]</code>:
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
725 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
726
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
727 <pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
728 var ::= prefixexp &lsquo;<b>.</b>&rsquo; Name
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
729 </pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
730
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
731 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
732 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>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
733 is equivalent to <code>_ENV.x</code>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
734 Due to the way that chunks are compiled,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
735 <code>_ENV</code> is never a global name (see <a href="#env">Environments</a>).
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
736 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
737 <%
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
738 end
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
739 }
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
740 stmt = {
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
741 title = "Statements"
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
742 content = function()
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
743 %>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
744 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
745 Luan supports an almost conventional set of statements,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
746 similar to those in Pascal or C.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
747 This set includes
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
748 assignments, control structures, function calls,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
749 and variable declarations.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
750 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
751 <%
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
752 end
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
753 subs = {
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
754 blocks = {
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
755 title = "Blocks"
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
756 content = function()
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
757 %>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
758 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
759 A block is a list of statements,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
760 which are executed sequentially:
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
761 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
762
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
763 <pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
764 block ::= {stat}
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
765 </pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
766
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
767 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
768 Luan has <em>empty statements</em>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
769 that allow you to separate statements with semicolons,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
770 start a block with a semicolon
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
771 or write two semicolons in sequence:
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
772 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
773
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
774 <pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
775 stat ::= &lsquo;<b>;</b>&rsquo;
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
776 </pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
777
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
778 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
779 A block can be explicitly delimited to produce a single statement:
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
780 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
781
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
782 <pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
783 stat ::= <b>do</b> block end_do
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
784 end_do ::= <b>end_do</b> | <b>end</b>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
785 </pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
786
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
787 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
788 Explicit blocks are useful
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
789 to control the scope of variable declarations.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
790 Explicit blocks are also sometimes used to
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
791 add a <b>return</b> statement in the middle
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
792 of another block (see <a href="#control">Control Structures</a>).
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
793 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
794 <%
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
795 end
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
796 }
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
797 chunks = {
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
798 title = "Chunks"
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
799 content = function()
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
800 %>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
801 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
802 The unit of compilation of Luan is called a <em>chunk</em>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
803 Syntactically,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
804 a chunk is simply a block:
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
805 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
806
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
807 <pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
808 chunk ::= block
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
809 </pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
810
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
811 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
812 Luan handles a chunk as the body of an anonymous function
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
813 with a variable number of arguments
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
814 (see <a href="#fn_def">Function Definitions</a>).
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
815 As such, chunks can define local variables,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
816 receive arguments, and return values.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
817 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
818
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
819 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
820 A chunk can be stored in a file or in a string inside the host program.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
821 To execute a chunk,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
822 Luan first <em>loads</em> it,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
823 compiling the chunk's code,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
824 and then Luan executes the compiled code.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
825 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
826 <%
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
827 end
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
828 }
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
829 assignment = {
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
830 title = "Assignment"
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
831 content = function()
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
832 %>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
833 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
834 Luan allows multiple assignments.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
835 Therefore, the syntax for assignment
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
836 defines a list of variables on the left side
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
837 and a list of expressions on the right side.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
838 The elements in both lists are separated by commas:
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
839 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
840
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
841 <pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
842 stat ::= varlist &lsquo;<b>=</b>&rsquo; explist
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
843 varlist ::= var {&lsquo;<b>,</b>&rsquo; var}
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
844 explist ::= exp {&lsquo;<b>,</b>&rsquo; exp}
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
845 </pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
846
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
847 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
848 Expressions are discussed in <a href="#expressions">Expressions</a>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
849 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
850
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
851 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
852 Before the assignment,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
853 the list of values is <em>adjusted</em> to the length of
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
854 the list of variables.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
855 If there are more values than needed,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
856 the excess values are thrown away.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
857 If there are fewer values than needed,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
858 the list is extended with as many <b>nil</b>'s as needed.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
859 If the list of expressions ends with a function call,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
860 then all values returned by that call enter the list of values,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
861 before the adjustment
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
862 (except when the call is enclosed in parentheses; see <a href="#expressions">Expressions</a>).
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
863 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
864
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
865 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
866 The assignment statement first evaluates all its expressions
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
867 and only then the assignments are performed.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
868 Thus the code
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
869 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
870
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
871 <pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
872 i = 3
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
873 i, a[i] = i+1, 20
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
874 </pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
875
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
876 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
877 sets <code>a[3]</code> to 20, without affecting <code>a[4]</code>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
878 because the <code>i</code> in <code>a[i]</code> is evaluated (to 3)
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
879 before it is assigned&nbsp;4.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
880 Similarly, the line
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
881 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
882
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
883 <pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
884 x, y = y, x
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
885 </pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
886
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
887 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
888 exchanges the values of <code>x</code> and <code>y</code>,
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
889 and
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
890 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
891
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
892 <pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
893 x, y, z = y, z, x
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
894 </pre>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
895
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
896 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
897 cyclically permutes the values of <code>x</code>, <code>y</code>, and <code>z</code>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
898 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
899
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
900 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
901 The meaning of assignments to global variables
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
902 and table fields can be changed via metatables.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
903 An assignment to an indexed variable <code>t[i] = val</code> is equivalent to
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
904 <code>settable_event(t,i,val)</code>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
905 (See <a href="#meta">Metatables and Metamethods</a> for a complete description of the
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
906 <code>settable_event</code> function.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
907 This function is not defined or callable in Luan.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
908 We use it here only for explanatory purposes.)
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
909 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
910
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
911 <p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
912 An assignment to a global name <code>x = val</code>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
913 is equivalent to the assignment
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
914 <code>_ENV.x = val</code> (see <a href="#env">Environments</a>).
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
915 Global names are only available when <code>_ENV</code> is defined.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
916 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
917 <%
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
918 end
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
919 }
1660
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
920 control = {
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
921 title = "Control Structures"
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
922 content = function()
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
923 %>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
924 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
925 The control structures
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
926 <b>if</b>, <b>while</b>, and <b>repeat</b> have the usual meaning and
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
927 familiar syntax:
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
928 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
929
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
930 <pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
931 stat ::= <b>while</b> exp <b>do</b> block end_while
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
932 stat ::= <b>repeat</b> block <b>until</b> exp
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
933 stat ::= <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] end_if
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
934 end_while ::= <b>end_while</b> | <b>end</b>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
935 end_if ::= <b>end_if</b> | <b>end</b>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
936 </pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
937
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
938 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
939 Luan also has a <b>for</b> statement (see <a href="#for">For Statement</a>).
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
940 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
941
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
942 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
943 The condition expression of a
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
944 control structure must be a boolean.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
945 Any other value type will produce an error.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
946 This helps catch errors and makes code more readable.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
947 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
948
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
949 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
950 In the <b>repeat</b>&ndash;<b>until</b> loop,
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
951 the inner block does not end at the <b>until</b> keyword,
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
952 but only after the condition.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
953 So, the condition can refer to local variables
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
954 declared inside the loop block.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
955 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
956
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
957 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
958 The <b>break</b> statement terminates the execution of a
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
959 <b>while</b>, <b>repeat</b>, or <b>for</b> loop,
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
960 skipping to the next statement after the loop:
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
961 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
962
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
963 <pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
964 stat ::= <b>break</b>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
965 </pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
966
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
967 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
968 A <b>break</b> ends the innermost enclosing loop.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
969 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
970
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
971 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
972 The <b>continue</b> statement jumps to the beginning of a
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
973 <b>while</b>, <b>repeat</b>, or <b>for</b> loop for next iteration,
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
974 skipping the execution of statements inside the body of loop for the current iteration:
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
975 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
976
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
977 <pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
978 stat ::= <b>continue</b>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
979 </pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
980
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
981 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
982 The <b>return</b> statement is used to return values
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
983 from a function or a chunk
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
984 (which is an anonymous function).
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
985 Functions can return more than one value,
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
986 so the syntax for the <b>return</b> statement is
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
987 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
988
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
989 <pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
990 stat ::= <b>return</b> [explist] [&lsquo;<b>;</b>&rsquo;]
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
991 </pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
992 <%
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
993 end
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
994 }
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
995 ["for"] = {
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
996 title = "For Statement"
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
997 content = function()
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
998 %>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
999 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1000 The <b>for</b> statement works over functions,
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1001 called <em>iterators</em>.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1002 On each iteration, the iterator function is called to produce a new value,
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1003 stopping when this new value is <b>nil</b>.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1004 The <b>for</b> loop has the following syntax:
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1005 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1006
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1007 <pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1008 stat ::= <b>for</b> namelist <b>in</b> exp <b>do</b> block end_for
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1009 namelist ::= Name {&lsquo;<b>,</b>&rsquo; Name}
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1010 end_for ::= <b>end_for</b> | <b>end</b>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1011 </pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1012
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1013 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1014 A <b>for</b> statement like
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1015 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1016
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1017 <pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1018 for <em>var_1</em>, &middot;&middot;&middot;, <em>var_n</em> in <em>exp</em> do <em>block</em> end
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1019 </pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1020
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1021 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1022 is equivalent to the code:
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1023 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1024
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1025 <pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1026 do
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1027 local <em>f</em> = <em>exp</em>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1028 while true do
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1029 local <em>var_1</em>, &middot;&middot;&middot;, <em>var_n</em> = <em>f</em>()
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1030 if <em>var_1</em> == nil then break end
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1031 <em>block</em>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1032 end
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1033 end
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1034 </pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1035
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1036 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1037 Note the following:
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1038 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1039
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1040 <ul>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1041 <li>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1042 <code><em>exp</em></code> is evaluated only once.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1043 Its result is an <em>iterator</em> function.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1044 </li>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1045 <li>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1046 <code><em>f</em></code> is an invisible variable.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1047 The name is here for explanatory purposes only.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1048 </li>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1049 <li>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1050 You can use <b>break</b> to exit a <b>for</b> loop.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1051 </li>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1052 <li>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1053 The loop variables <code><em>var_i</em></code> are local to the loop;
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1054 you cannot use their values after the <b>for</b> ends.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1055 If you need these values,
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1056 then assign them to other variables before breaking or exiting the loop.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1057 </li>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1058 </ul>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1059 <%
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1060 end
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1061 }
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1062 ["try"] = {
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1063 title = "Try Statement"
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1064 content = function()
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1065 %>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1066 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1067 The <b>try</b> statement has the same semantics as in Java.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1068 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1069
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1070 <pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1071 stat ::= <b>try</b> block [<b>catch</b> Name block] [<b>finally</b> block] end_try
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1072 end_try ::= <b>end_try</b> | <b>end</b>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1073 </pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1074 <%
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1075 end
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1076 }
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1077 fn_stmt = {
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1078 title = "Function Calls as Statements"
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1079 content = function()
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1080 %>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1081 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1082 To allow possible side-effects,
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1083 function calls can be executed as statements:
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1084 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1085
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1086 <pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1087 stat ::= functioncall
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1088 </pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1089
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1090 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1091 In this case, all returned values are thrown away.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1092 Function calls are explained in <a href="#fn_calls">Function Calls</a>.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1093 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1094 <%
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1095 end
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1096 }
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1097 logical_stmt = {
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1098 title = "Logical Statement"
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1099 content = function()
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1100 %>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1101 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1102 <a href="#logical_ops">Logical expressions</a> can be statements.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1103 This is useful in cases like this:
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1104 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1105
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1106 <pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1107 x==5 or error "x should be 5"
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1108 </pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1109 <%
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1110 end
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1111 }
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1112 local_stmt = {
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1113 title = "Local Declarations"
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1114 content = function()
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1115 %>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1116 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1117 Local variables can be declared anywhere inside a block.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1118 The declaration can include an initial assignment:
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1119 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1120
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1121 <pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1122 stat ::= <b>local</b> namelist [&lsquo;<b>=</b>&rsquo; explist]
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1123 </pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1124
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1125 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1126 If present, an initial assignment has the same semantics
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1127 of a multiple assignment (see <a href="#assignment">Assignment</a>).
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1128 Otherwise, all variables are initialized with <b>nil</b>.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1129 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1130
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1131 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1132 A chunk is also a block (see <a href="#chunks">Chunks</a>),
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1133 and so local variables can be declared in a chunk outside any explicit block.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1134 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1135
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1136 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1137 The visibility rules for local variables are explained in <a href="#visibility">Visibility Rules</a>.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1138 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1139 <%
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1140 end
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1141 }
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1142 template_stmt = {
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1143 title = "Template Statements"
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1144 content = function()
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1145 %>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1146 <p>Template statements provide the full equivalent of <a href="http://en.wikipedia.org/wiki/JavaServer_Pages">JSP</a> but in a general way. Template statements write to standard output. For example:</p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1147 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1148
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1149 <pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1150 local name = "Bob"
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1151 %&gt;
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1152 Hello &lt;%= name %&gt;!
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1153 Bye &lt;%= name %&gt;.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1154 &lt;%
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1155 </pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1156
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1157 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1158 is equivalent to the code:
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1159 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1160
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1161 <pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1162 local name = "Bob"
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1163 require("luan:Io.luan").stdout.write( "Hello ", name , "!\nBye ", name , ".\n" )
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1164 </pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1165 <%
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1166 end
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1167 }
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1168 }
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1169 }
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1170 expressions = {
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1171 title = "Expressions"
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1172 content = function()
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1173 %>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1174 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1175 The basic expressions in Luan are the following:
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1176 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1177
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1178 <pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1179 exp ::= prefixexp
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1180 exp ::= <b>nil</b> | <b>false</b> | <b>true</b>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1181 exp ::= Numeral
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1182 exp ::= LiteralString
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1183 exp ::= functiondef
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1184 exp ::= tableconstructor
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1185 exp ::= &lsquo;<b>...</b>&rsquo;
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1186 exp ::= exp binop exp
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1187 exp ::= unop exp
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1188 prefixexp ::= var | functioncall | &lsquo;<b>(</b>&rsquo; exp &lsquo;<b>)</b>&rsquo;
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1189 </pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1190
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1191 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1192 Numerals and literal strings are explained in <a href="#lex">Lexical Conventions</a>;
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1193 variables are explained in <a href="#vars">Variables</a>;
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1194 function definitions are explained in <a href="#fn_def">Function Definitions</a>;
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1195 function calls are explained in <a href="#fn_calls">Function Calls</a>;
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1196 table constructors are explained in <a href="#constructors">Table Constructors</a>.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1197 Vararg expressions,
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1198 denoted by three dots ('<code>...</code>'), can only be used when
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1199 directly inside a vararg function;
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1200 they are explained in <a href="#fn_def">Function Definitions</a>.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1201 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1202
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1203 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1204 Binary operators comprise arithmetic operators (see <a href="#arithmetic">Arithmetic Operators</a>),
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1205 relational operators (see <a href="#relational">Relational Operators</a>), logical operators (see <a href="#logical_ops">Logical Operators</a>),
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1206 and the concatenation operator (see <a href="#concatenation">Concatenation</a>).
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1207 Unary operators comprise the unary minus (see <a href="#arithmetic">Arithmetic Operators</a>),
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1208 the unary logical <b>not</b> (see <a href="#logical_ops">Logical Operators</a>),
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1209 and the unary <em>length operator</em> (see <a href="#length">The Length Operator</a>).
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1210 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1211
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1212 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1213 Both function calls and vararg expressions can result in multiple values.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1214 If a function call is used as a statement (see <a href="#fn_stmt">Function Calls as Statements</a>),
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1215 then its return list is adjusted to zero elements,
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1216 thus discarding all returned values.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1217 If an expression is used as the last (or the only) element
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1218 of a list of expressions,
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1219 then no adjustment is made
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1220 (unless the expression is enclosed in parentheses).
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1221 In all other contexts,
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1222 Luan adjusts the result list to one element,
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1223 either discarding all values except the first one
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1224 or adding a single <b>nil</b> if there are no values.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1225 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1226
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1227 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1228 Here are some examples:
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1229 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1230
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1231 <pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1232 f() -- adjusted to 0 results
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1233 g(f(), x) -- f() is adjusted to 1 result
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1234 g(x, f()) -- g gets x plus all results from f()
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1235 a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil)
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1236 a,b = ... -- a gets the first vararg parameter, b gets
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1237 -- the second (both a and b can get nil if there
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1238 -- is no corresponding vararg parameter)
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1239
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1240 a,b,c = x, f() -- f() is adjusted to 2 results
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1241 a,b,c = f() -- f() is adjusted to 3 results
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1242 return f() -- returns all results from f()
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1243 return ... -- returns all received vararg parameters
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1244 return x,y,f() -- returns x, y, and all results from f()
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1245 {f()} -- creates a list with all results from f()
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1246 {...} -- creates a list with all vararg parameters
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1247 {f(), nil} -- f() is adjusted to 1 result
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1248 </pre>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1249
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1250 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1251 Any expression enclosed in parentheses always results in only one value.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1252 Thus,
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1253 <code>(f(x,y,z))</code> is always a single value,
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1254 even if <code>f</code> returns several values.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1255 (The value of <code>(f(x,y,z))</code> is the first value returned by <code>f</code>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1256 or <b>nil</b> if <code>f</code> does not return any values.)
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1257 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1258 <%
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1259 end
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1260 subs = {
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1261 arithmetic = {
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1262 title = "Arithmetic Operators"
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1263 content = function()
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1264 %>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1265 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1266 Luan supports the following arithmetic operators:
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1267 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1268
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1269 <ul>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1270 <li><b><code>+</code>: </b>addition</li>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1271 <li><b><code>-</code>: </b>subtraction</li>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1272 <li><b><code>*</code>: </b>multiplication</li>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1273 <li><b><code>/</code>: </b>division</li>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1274 <li><b><code>%</code>: </b>modulo</li>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1275 <li><b><code>^</code>: </b>exponentiation</li>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1276 <li><b><code>-</code>: </b>unary minus</li>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1277 </ul>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1278
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1279 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1280 Addition, subtraction, multiplication, division, and unary minus are the same as these operators in Java. Exponentiation uses Java's <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#pow(double,%20double)">Math.pow</a> function.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1281 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1282
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1283 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1284 Modulo is defined as the remainder of a division
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1285 that rounds the quotient towards minus infinite (floor division).
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1286 (The Java modulo operator is not used.)
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1287 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1288 <%
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1289 end
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1290 }
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1291 conversions = {
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1292 title = "Coercions and Conversions"
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1293 content = function()
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1294 %>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1295 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1296 Luan generally avoids automatic conversions.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1297 String concatenation automatically converts all of its arguments to strings.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1298 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1299
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1300 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1301 Luan provides library functions for explicit type conversions.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1302 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1303 <%
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1304 end
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1305 }
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1306 relational = {
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1307 title = "Relational Operators"
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1308 content = function()
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1309 %>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1310 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1311 Luan supports the following relational operators:
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1312 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1313
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1314 <ul>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1315 <li><b><code>==</code>: </b>equality</li>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1316 <li><b><code>~=</code>: </b>inequality</li>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1317 <li><b><code>&lt;</code>: </b>less than</li>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1318 <li><b><code>&gt;</code>: </b>greater than</li>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1319 <li><b><code>&lt;=</code>: </b>less or equal</li>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1320 <li><b><code>&gt;=</code>: </b>greater or equal</li>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1321 </ul>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1322
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1323 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1324 These operators always result in <b>false</b> or <b>true</b>.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1325 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1326
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1327 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1328 Equality (<code>==</code>) first compares the type of its operands.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1329 If the types are different, then the result is <b>false</b>.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1330 Otherwise, the values of the operands are compared.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1331 Strings, numbers, and binary values are compared in the obvious way (by value).
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1332 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1333
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1334 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1335 Tables
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1336 are compared by reference:
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1337 two objects are considered equal only if they are the same object.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1338 Every time you create a new table,
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1339 it is different from any previously existing table.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1340 Closures are also compared by reference.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1341 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1342
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1343 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1344 You can change the way that Luan compares tables
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1345 by using the "eq" metamethod (see <a href="#meta">Metatables and Metamethods</a>).
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1346 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1347
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1348 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1349 Java values are compared for equality with the Java <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)"><code>equals</code></a> method.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1350 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1351
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1352 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1353 Equality comparisons do not convert strings to numbers
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1354 or vice versa.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1355 Thus, <code>"0"==0</code> evaluates to <b>false</b>,
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1356 and <code>t[0]</code> and <code>t["0"]</code> denote different
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1357 entries in a table.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1358 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1359
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1360 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1361 The operator <code>~=</code> is exactly the negation of equality (<code>==</code>).
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1362 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1363
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1364 <p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1365 The order operators work as follows.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1366 If both arguments are numbers,
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1367 then they are compared following
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1368 the usual rule for binary operations.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1369 Otherwise, if both arguments are strings,
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1370 then their values are compared according to the current locale.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1371 Otherwise, Luan tries to call the "lt" or the "le"
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1372 metamethod (see <a href="#meta">Metatables and Metamethods</a>).
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1373 A comparison <code>a &gt; b</code> is translated to <code>b &lt; a</code>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1374 and <code>a &gt;= b</code> is translated to <code>b &lt;= a</code>.
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1375 </p>
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1376 <%
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1377 end
2968e43cdd44 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1656
diff changeset
1378 }
1656
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1379 }
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1380 }
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1381 }
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1382 }
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1383 }
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1384
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1385
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1386 return function()
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1387 Io.stdout = Http.response.text_writer()
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1388 %>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1389 <!doctype html>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1390 <html>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1391 <head>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1392 <% head() %>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1393 <title>Luan Reference Manual</title>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1394 <style>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1395 p[keywords] {
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1396 font-family: monospace;
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1397 margin-left: 40px;
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1398 max-width: 700px;
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1399 }
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1400 p[keywords] span {
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1401 display: inline-block;
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1402 width: 100px;
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1403 }
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1404 </style>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1405 </head>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1406 <body>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1407 <% docs_header() %>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1408 <div content>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1409 <h1><a href="manual.html">Luan Reference Manual</a></h1>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1410 <p small>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1411 Original copyright &copy; 2015 Lua.org, PUC-Rio.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1412 Freely available under the terms of the
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1413 <a href="http://www.lua.org/license.html">Lua license</a>.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1414 Modified for Luan.
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1415 </p>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1416 <hr>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1417 <h2>Contents</h2>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1418 <div toc>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1419 <% show_toc(content) %>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1420 </div>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1421 <hr>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1422 <% show_content(content,2) %>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1423 </div>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1424 </body>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1425 </html>
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1426 <%
540bf2343078 manual work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1427 end