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