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