Mercurial Hosting > luan
comparison website/src/manual.html @ 380:6c6c3537035e
work on manual
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Wed, 22 Apr 2015 13:10:48 -0600 |
| parents | 571057b1666b |
| children | 8557581740db |
comparison
equal
deleted
inserted
replaced
| 379:e9e445e28f0b | 380:6c6c3537035e |
|---|---|
| 40 <ul> | 40 <ul> |
| 41 <li><a href="#types">Values and Types</a></li> | 41 <li><a href="#types">Values and Types</a></li> |
| 42 <li><a href="#env">Environments</a></li> | 42 <li><a href="#env">Environments</a></li> |
| 43 <li><a href="#error">Error Handling</a></li> | 43 <li><a href="#error">Error Handling</a></li> |
| 44 <li><a href="#meta">Metatables and Metamethods</a></li> | 44 <li><a href="#meta">Metatables and Metamethods</a></li> |
| 45 <li><a href="#gc">Garbage Collection</a></li> | |
| 46 </ul> | |
| 47 </div> | |
| 48 | |
| 49 <div margin-bottom="1em"> | |
| 50 <a href="#lang">The Language</a> | |
| 51 <ul> | |
| 52 <li><a href="#lex">Lexical Conventions</a></li> | |
| 53 <li><a href="#vars">Variables</a></li> | |
| 45 </ul> | 54 </ul> |
| 46 </div> | 55 </div> |
| 47 | 56 |
| 48 <hr/> | 57 <hr/> |
| 49 | 58 |
| 463 </ul> | 472 </ul> |
| 464 | 473 |
| 465 | 474 |
| 466 | 475 |
| 467 | 476 |
| 468 <h2>2.5 – <a name="2.5">Garbage Collection</a></h2> | 477 <h3 margin-top="1em"><a name="gc">Garbage Collection</a></h3> |
| 469 | 478 |
| 470 <p> | 479 <p> |
| 471 Lua performs automatic memory management. | 480 Luan uses Java's garbage collection. |
| 472 This means that | 481 |
| 473 you do not have to worry about allocating memory for new objects | 482 |
| 474 or freeing it when the objects are no longer needed. | 483 |
| 475 Lua manages memory automatically by running | 484 |
| 476 a <em>garbage collector</em> to collect all <em>dead objects</em> | 485 |
| 477 (that is, objects that are no longer accessible from Lua). | 486 <h2 margin-top="1em"><a name="lang">The Language</a></h2> |
| 478 All memory used by Lua is subject to automatic management: | 487 |
| 479 strings, tables, userdata, functions, threads, internal structures, etc. | 488 <p> |
| 480 | 489 This section describes the lexis, the syntax, and the semantics of Luan. |
| 481 | |
| 482 <p> | |
| 483 Lua implements an incremental mark-and-sweep collector. | |
| 484 It uses two numbers to control its garbage-collection cycles: | |
| 485 the <em>garbage-collector pause</em> and | |
| 486 the <em>garbage-collector step multiplier</em>. | |
| 487 Both use percentage points as units | |
| 488 (e.g., a value of 100 means an internal value of 1). | |
| 489 | |
| 490 | |
| 491 <p> | |
| 492 The garbage-collector pause | |
| 493 controls how long the collector waits before starting a new cycle. | |
| 494 Larger values make the collector less aggressive. | |
| 495 Values smaller than 100 mean the collector will not wait to | |
| 496 start a new cycle. | |
| 497 A value of 200 means that the collector waits for the total memory in use | |
| 498 to double before starting a new cycle. | |
| 499 | |
| 500 | |
| 501 <p> | |
| 502 The garbage-collector step multiplier | |
| 503 controls the relative speed of the collector relative to | |
| 504 memory allocation. | |
| 505 Larger values make the collector more aggressive but also increase | |
| 506 the size of each incremental step. | |
| 507 You should not use values smaller than 100, | |
| 508 because they make the collector too slow and | |
| 509 can result in the collector never finishing a cycle. | |
| 510 The default is 200, | |
| 511 which means that the collector runs at "twice" | |
| 512 the speed of memory allocation. | |
| 513 | |
| 514 | |
| 515 <p> | |
| 516 If you set the step multiplier to a very large number | |
| 517 (larger than 10% of the maximum number of | |
| 518 bytes that the program may use), | |
| 519 the collector behaves like a stop-the-world collector. | |
| 520 If you then set the pause to 200, | |
| 521 the collector behaves as in old Lua versions, | |
| 522 doing a complete collection every time Lua doubles its | |
| 523 memory usage. | |
| 524 | |
| 525 | |
| 526 <p> | |
| 527 You can change these numbers by calling <a href="#lua_gc"><code>lua_gc</code></a> in C | |
| 528 or <a href="#pdf-collectgarbage"><code>collectgarbage</code></a> in Lua. | |
| 529 You can also use these functions to control | |
| 530 the collector directly (e.g., stop and restart it). | |
| 531 | |
| 532 | |
| 533 | |
| 534 <h3>2.5.1 – <a name="2.5.1">Garbage-Collection Metamethods</a></h3> | |
| 535 | |
| 536 <p> | |
| 537 You can set garbage-collector metamethods for tables | |
| 538 and, using the C API, | |
| 539 for full userdata (see <a href="#2.4">§2.4</a>). | |
| 540 These metamethods are also called <em>finalizers</em>. | |
| 541 Finalizers allow you to coordinate Lua's garbage collection | |
| 542 with external resource management | |
| 543 (such as closing files, network or database connections, | |
| 544 or freeing your own memory). | |
| 545 | |
| 546 | |
| 547 <p> | |
| 548 For an object (table or userdata) to be finalized when collected, | |
| 549 you must <em>mark</em> it for finalization. | |
| 550 | |
| 551 You mark an object for finalization when you set its metatable | |
| 552 and the metatable has a field indexed by the string "<code>__gc</code>". | |
| 553 Note that if you set a metatable without a <code>__gc</code> field | |
| 554 and later create that field in the metatable, | |
| 555 the object will not be marked for finalization. | |
| 556 However, after an object has been marked, | |
| 557 you can freely change the <code>__gc</code> field of its metatable. | |
| 558 | |
| 559 | |
| 560 <p> | |
| 561 When a marked object becomes garbage, | |
| 562 it is not collected immediately by the garbage collector. | |
| 563 Instead, Lua puts it in a list. | |
| 564 After the collection, | |
| 565 Lua goes through that list. | |
| 566 For each object in the list, | |
| 567 it checks the object's <code>__gc</code> metamethod: | |
| 568 If it is a function, | |
| 569 Lua calls it with the object as its single argument; | |
| 570 if the metamethod is not a function, | |
| 571 Lua simply ignores it. | |
| 572 | |
| 573 | |
| 574 <p> | |
| 575 At the end of each garbage-collection cycle, | |
| 576 the finalizers for objects are called in | |
| 577 the reverse order that the objects were marked for finalization, | |
| 578 among those collected in that cycle; | |
| 579 that is, the first finalizer to be called is the one associated | |
| 580 with the object marked last in the program. | |
| 581 The execution of each finalizer may occur at any point during | |
| 582 the execution of the regular code. | |
| 583 | |
| 584 | |
| 585 <p> | |
| 586 Because the object being collected must still be used by the finalizer, | |
| 587 that object (and other objects accessible only through it) | |
| 588 must be <em>resurrected</em> by Lua. | |
| 589 Usually, this resurrection is transient, | |
| 590 and the object memory is freed in the next garbage-collection cycle. | |
| 591 However, if the finalizer stores the object in some global place | |
| 592 (e.g., a global variable), | |
| 593 then the resurrection is permanent. | |
| 594 Moreover, if the finalizer marks a finalizing object for finalization again, | |
| 595 its finalizer will be called again in the next cycle where the | |
| 596 object is unreachable. | |
| 597 In any case, | |
| 598 the object memory is freed only in the GC cycle where | |
| 599 the object is unreachable and not marked for finalization. | |
| 600 | |
| 601 | |
| 602 <p> | |
| 603 When you close a state (see <a href="#lua_close"><code>lua_close</code></a>), | |
| 604 Lua calls the finalizers of all objects marked for finalization, | |
| 605 following the reverse order that they were marked. | |
| 606 If any finalizer marks objects for collection during that phase, | |
| 607 these marks have no effect. | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 <h3>2.5.2 – <a name="2.5.2">Weak Tables</a></h3> | |
| 614 | |
| 615 <p> | |
| 616 A <em>weak table</em> is a table whose elements are | |
| 617 <em>weak references</em>. | |
| 618 A weak reference is ignored by the garbage collector. | |
| 619 In other words, | |
| 620 if the only references to an object are weak references, | |
| 621 then the garbage collector will collect that object. | |
| 622 | |
| 623 | |
| 624 <p> | |
| 625 A weak table can have weak keys, weak values, or both. | |
| 626 A table with weak keys allows the collection of its keys, | |
| 627 but prevents the collection of its values. | |
| 628 A table with both weak keys and weak values allows the collection of | |
| 629 both keys and values. | |
| 630 In any case, if either the key or the value is collected, | |
| 631 the whole pair is removed from the table. | |
| 632 The weakness of a table is controlled by the | |
| 633 <code>__mode</code> field of its metatable. | |
| 634 If the <code>__mode</code> field is a string containing the character '<code>k</code>', | |
| 635 the keys in the table are weak. | |
| 636 If <code>__mode</code> contains '<code>v</code>', | |
| 637 the values in the table are weak. | |
| 638 | |
| 639 | |
| 640 <p> | |
| 641 A table with weak keys and strong values | |
| 642 is also called an <em>ephemeron table</em>. | |
| 643 In an ephemeron table, | |
| 644 a value is considered reachable only if its key is reachable. | |
| 645 In particular, | |
| 646 if the only reference to a key comes through its value, | |
| 647 the pair is removed. | |
| 648 | |
| 649 | |
| 650 <p> | |
| 651 Any change in the weakness of a table may take effect only | |
| 652 at the next collect cycle. | |
| 653 In particular, if you change the weakness to a stronger mode, | |
| 654 Lua may still collect some items from that table | |
| 655 before the change takes effect. | |
| 656 | |
| 657 | |
| 658 <p> | |
| 659 Only objects that have an explicit construction | |
| 660 are removed from weak tables. | |
| 661 Values, such as numbers and light C functions, | |
| 662 are not subject to garbage collection, | |
| 663 and therefore are not removed from weak tables | |
| 664 (unless their associated values are collected). | |
| 665 Although strings are subject to garbage collection, | |
| 666 they do not have an explicit construction, | |
| 667 and therefore are not removed from weak tables. | |
| 668 | |
| 669 | |
| 670 <p> | |
| 671 Resurrected objects | |
| 672 (that is, objects being finalized | |
| 673 and objects accessible only through objects being finalized) | |
| 674 have a special behavior in weak tables. | |
| 675 They are removed from weak values before running their finalizers, | |
| 676 but are removed from weak keys only in the next collection | |
| 677 after running their finalizers, when such objects are actually freed. | |
| 678 This behavior allows the finalizer to access properties | |
| 679 associated with the object through weak tables. | |
| 680 | |
| 681 | |
| 682 <p> | |
| 683 If a weak table is among the resurrected objects in a collection cycle, | |
| 684 it may not be properly cleared until the next cycle. | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 <h2>2.6 – <a name="2.6">Coroutines</a></h2> | |
| 693 | |
| 694 <p> | |
| 695 Lua supports coroutines, | |
| 696 also called <em>collaborative multithreading</em>. | |
| 697 A coroutine in Lua represents an independent thread of execution. | |
| 698 Unlike threads in multithread systems, however, | |
| 699 a coroutine only suspends its execution by explicitly calling | |
| 700 a yield function. | |
| 701 | |
| 702 | |
| 703 <p> | |
| 704 You create a coroutine by calling <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>. | |
| 705 Its sole argument is a function | |
| 706 that is the main function of the coroutine. | |
| 707 The <code>create</code> function only creates a new coroutine and | |
| 708 returns a handle to it (an object of type <em>thread</em>); | |
| 709 it does not start the coroutine. | |
| 710 | |
| 711 | |
| 712 <p> | |
| 713 You execute a coroutine by calling <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>. | |
| 714 When you first call <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>, | |
| 715 passing as its first argument | |
| 716 a thread returned by <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>, | |
| 717 the coroutine starts its execution, | |
| 718 at the first line of its main function. | |
| 719 Extra arguments passed to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> are passed | |
| 720 as arguments to the coroutine's main function. | |
| 721 After the coroutine starts running, | |
| 722 it runs until it terminates or <em>yields</em>. | |
| 723 | |
| 724 | |
| 725 <p> | |
| 726 A coroutine can terminate its execution in two ways: | |
| 727 normally, when its main function returns | |
| 728 (explicitly or implicitly, after the last instruction); | |
| 729 and abnormally, if there is an unprotected error. | |
| 730 In case of normal termination, | |
| 731 <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns <b>true</b>, | |
| 732 plus any values returned by the coroutine main function. | |
| 733 In case of errors, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns <b>false</b> | |
| 734 plus an error message. | |
| 735 | |
| 736 | |
| 737 <p> | |
| 738 A coroutine yields by calling <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a>. | |
| 739 When a coroutine yields, | |
| 740 the corresponding <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns immediately, | |
| 741 even if the yield happens inside nested function calls | |
| 742 (that is, not in the main function, | |
| 743 but in a function directly or indirectly called by the main function). | |
| 744 In the case of a yield, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> also returns <b>true</b>, | |
| 745 plus any values passed to <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a>. | |
| 746 The next time you resume the same coroutine, | |
| 747 it continues its execution from the point where it yielded, | |
| 748 with the call to <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a> returning any extra | |
| 749 arguments passed to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>. | |
| 750 | |
| 751 | |
| 752 <p> | |
| 753 Like <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>, | |
| 754 the <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> function also creates a coroutine, | |
| 755 but instead of returning the coroutine itself, | |
| 756 it returns a function that, when called, resumes the coroutine. | |
| 757 Any arguments passed to this function | |
| 758 go as extra arguments to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>. | |
| 759 <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> returns all the values returned by <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>, | |
| 760 except the first one (the boolean error code). | |
| 761 Unlike <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>, | |
| 762 <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> does not catch errors; | |
| 763 any error is propagated to the caller. | |
| 764 | |
| 765 | |
| 766 <p> | |
| 767 As an example of how coroutines work, | |
| 768 consider the following code: | |
| 769 | |
| 770 <pre> | |
| 771 function foo (a) | |
| 772 print("foo", a) | |
| 773 return coroutine.yield(2*a) | |
| 774 end | |
| 775 | |
| 776 co = coroutine.create(function (a,b) | |
| 777 print("co-body", a, b) | |
| 778 local r = foo(a+1) | |
| 779 print("co-body", r) | |
| 780 local r, s = coroutine.yield(a+b, a-b) | |
| 781 print("co-body", r, s) | |
| 782 return b, "end" | |
| 783 end) | |
| 784 | |
| 785 print("main", coroutine.resume(co, 1, 10)) | |
| 786 print("main", coroutine.resume(co, "r")) | |
| 787 print("main", coroutine.resume(co, "x", "y")) | |
| 788 print("main", coroutine.resume(co, "x", "y")) | |
| 789 </pre><p> | |
| 790 When you run it, it produces the following output: | |
| 791 | |
| 792 <pre> | |
| 793 co-body 1 10 | |
| 794 foo 2 | |
| 795 main true 4 | |
| 796 co-body r | |
| 797 main true 11 -9 | |
| 798 co-body x y | |
| 799 main true 10 end | |
| 800 main false cannot resume dead coroutine | |
| 801 </pre> | |
| 802 | |
| 803 <p> | |
| 804 You can also create and manipulate coroutines through the C API: | |
| 805 see functions <a href="#lua_newthread"><code>lua_newthread</code></a>, <a href="#lua_resume"><code>lua_resume</code></a>, | |
| 806 and <a href="#lua_yield"><code>lua_yield</code></a>. | |
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 <h1>3 – <a name="3">The Language</a></h1> | |
| 813 | |
| 814 <p> | |
| 815 This section describes the lexis, the syntax, and the semantics of Lua. | |
| 816 In other words, | 490 In other words, |
| 817 this section describes | 491 this section describes |
| 818 which tokens are valid, | 492 which tokens are valid, |
| 819 how they can be combined, | 493 how they can be combined, |
| 820 and what their combinations mean. | 494 and what their combinations mean. |
| 821 | 495 |
| 822 | 496 |
| 823 <p> | 497 <p> |
| 824 Language constructs will be explained using the usual extended BNF notation, | 498 Language constructs will be explained using the usual extended BNF notation, |
| 825 in which | 499 in which |
| 826 {<em>a</em>} means 0 or more <em>a</em>'s, and | 500 {<i>a</i>} means 0 or more <i>a</i>'s, and |
| 827 [<em>a</em>] means an optional <em>a</em>. | 501 [<i>a</i>] means an optional <i>a</i>. |
| 828 Non-terminals are shown like non-terminal, | 502 Non-terminals are shown like non-terminal, |
| 829 keywords are shown like <b>kword</b>, | 503 keywords are shown like <b>kword</b>, |
| 830 and other terminal symbols are shown like ‘<b>=</b>’. | 504 and other terminal symbols are shown like ‘<b>=</b>’. |
| 831 The complete syntax of Lua can be found in <a href="#9">§9</a> | 505 The complete syntax of Luan can be found in <a href="#9">§9</a> |
| 832 at the end of this manual. | 506 at the end of this manual. |
| 833 | 507 |
| 834 | 508 |
| 835 | 509 |
| 836 <h2>3.1 – <a name="3.1">Lexical Conventions</a></h2> | 510 <h3 margin-top="1em"><a name="lex">Lexical Conventions</a></h3> |
| 837 | 511 |
| 838 <p> | 512 <p> |
| 839 Lua is a free-form language. | 513 Luan ignores spaces and comments |
| 840 It ignores spaces (including new lines) and comments | |
| 841 between lexical elements (tokens), | 514 between lexical elements (tokens), |
| 842 except as delimiters between names and keywords. | 515 except as delimiters between names and keywords. |
| 843 | 516 Luan generally considers the end of a line to be the end of a statement. This catches errors and encourages readability. The exception to this is in paranthesis ( <i>(...)</i>, <i>[...]</i>, and <i>{...}</i> ) where the end of line is treated as white space. |
| 844 | 517 |
| 845 <p> | 518 <p> |
| 846 <em>Names</em> | 519 <i>Names</i> |
| 847 (also called <em>identifiers</em>) | 520 (also called <i>identifiers</i>) |
| 848 in Lua can be any string of letters, | 521 in Luan can be any string of letters, |
| 849 digits, and underscores, | 522 digits, and underscores, |
| 850 not beginning with a digit. | 523 not beginning with a digit. |
| 851 Identifiers are used to name variables, table fields, and labels. | 524 Identifiers are used to name variables, table fields, and labels. |
| 852 | 525 |
| 853 | 526 |
| 854 <p> | 527 <p> |
| 855 The following <em>keywords</em> are reserved | 528 The following <i>keywords</i> are reserved |
| 856 and cannot be used as names: | 529 and cannot be used as names: |
| 857 | 530 |
| 858 | 531 |
| 859 <pre> | 532 <pre> |
| 860 and break do else elseif end | 533 and break do else elseif end |
| 862 local nil not or repeat return | 535 local nil not or repeat return |
| 863 then true until while | 536 then true until while |
| 864 </pre> | 537 </pre> |
| 865 | 538 |
| 866 <p> | 539 <p> |
| 867 Lua is a case-sensitive language: | 540 Luan is a case-sensitive language: |
| 868 <code>and</code> is a reserved word, but <code>And</code> and <code>AND</code> | 541 <tt>and</tt> is a reserved word, but <tt>And</tt> and <tt>AND</tt> |
| 869 are two different, valid names. | 542 are two different, valid names. |
| 870 As a convention, | |
| 871 programs should avoid creating | |
| 872 names that start with an underscore followed by | |
| 873 one or more uppercase letters (such as <a href="#pdf-_VERSION"><code>_VERSION</code></a>). | |
| 874 | 543 |
| 875 | 544 |
| 876 <p> | 545 <p> |
| 877 The following strings denote other tokens: | 546 The following strings denote other tokens: |
| 878 | 547 |
| 880 + - * / % ^ # | 549 + - * / % ^ # |
| 881 & ~ | << >> // | 550 & ~ | << >> // |
| 882 == ~= <= >= < > = | 551 == ~= <= >= < > = |
| 883 ( ) { } [ ] :: | 552 ( ) { } [ ] :: |
| 884 ; : , . .. ... | 553 ; : , . .. ... |
| 554 | |
| 885 </pre> | 555 </pre> |
| 886 | 556 |
| 887 <p> | 557 <p> |
| 888 <em>Literal strings</em> | 558 <i>Literal strings</i> |
| 889 can be delimited by matching single or double quotes, | 559 can be delimited by matching single or double quotes, |
| 890 and can contain the following C-like escape sequences: | 560 and can contain the following C-like escape sequences: |
| 891 '<code>\a</code>' (bell), | 561 '<tt>\a</tt>' (bell), |
| 892 '<code>\b</code>' (backspace), | 562 '<tt>\b</tt>' (backspace), |
| 893 '<code>\f</code>' (form feed), | 563 '<tt>\f</tt>' (form feed), |
| 894 '<code>\n</code>' (newline), | 564 '<tt>\n</tt>' (newline), |
| 895 '<code>\r</code>' (carriage return), | 565 '<tt>\r</tt>' (carriage return), |
| 896 '<code>\t</code>' (horizontal tab), | 566 '<tt>\t</tt>' (horizontal tab), |
| 897 '<code>\v</code>' (vertical tab), | 567 '<tt>\v</tt>' (vertical tab), |
| 898 '<code>\\</code>' (backslash), | 568 '<tt>\\</tt>' (backslash), |
| 899 '<code>\"</code>' (quotation mark [double quote]), | 569 '<tt>\"</tt>' (quotation mark [double quote]), |
| 900 and '<code>\'</code>' (apostrophe [single quote]). | 570 and '<tt>\'</tt>' (apostrophe [single quote]). |
| 901 A backslash followed by a real newline | 571 A backslash followed by a real newline |
| 902 results in a newline in the string. | 572 results in a newline in the string. |
| 903 The escape sequence '<code>\z</code>' skips the following span | 573 The escape sequence '<tt>\z</tt>' skips the following span |
| 904 of white-space characters, | 574 of white-space characters, |
| 905 including line breaks; | 575 including line breaks; |
| 906 it is particularly useful to break and indent a long literal string | 576 it is particularly useful to break and indent a long literal string |
| 907 into multiple lines without adding the newlines and spaces | 577 into multiple lines without adding the newlines and spaces |
| 908 into the string contents. | 578 into the string contents. |
| 909 | 579 |
| 910 | 580 |
| 911 <p> | 581 <p> |
| 912 Strings in Lua can contain any 8-bit value, including embedded zeros, | 582 Luan can specify any character in a literal string by its numerical value. |
| 913 which can be specified as '<code>\0</code>'. | |
| 914 More generally, | |
| 915 we can specify any byte in a literal string by its numerical value. | |
| 916 This can be done | 583 This can be done |
| 917 with the escape sequence <code>\x<em>XX</em></code>, | 584 with the escape sequence <tt>\x<i>XX</i></tt>, |
| 918 where <em>XX</em> is a sequence of exactly two hexadecimal digits, | 585 where <i>XX</i> is a sequence of exactly two hexadecimal digits, |
| 919 or with the escape sequence <code>\<em>ddd</em></code>, | 586 or with the escape sequence <tt>\<i>ddd</i></tt>, |
| 920 where <em>ddd</em> is a sequence of up to three decimal digits. | 587 where <i>ddd</i> is a sequence of up to three decimal digits. |
| 921 (Note that if a decimal escape sequence is to be followed by a digit, | 588 (Note that if a decimal escape sequence is to be followed by a digit, |
| 922 it must be expressed using exactly three digits.) | 589 it must be expressed using exactly three digits.) |
| 923 | 590 |
| 924 | 591 |
| 925 <p> | 592 <p> |
| 926 The UTF-8 encoding of a Unicode character | |
| 927 can be inserted in a literal string with | |
| 928 the escape sequence <code>\u{<em>XXX</em>}</code> | |
| 929 (note the mandatory enclosing brackets), | |
| 930 where <em>XXX</em> is a sequence of one or more hexadecimal digits | |
| 931 representing the character code point. | |
| 932 | |
| 933 | |
| 934 <p> | |
| 935 Literal strings can also be defined using a long format | 593 Literal strings can also be defined using a long format |
| 936 enclosed by <em>long brackets</em>. | 594 enclosed by <i>long brackets</i>. |
| 937 We define an <em>opening long bracket of level <em>n</em></em> as an opening | 595 We define an <i>opening long bracket of level <i>n</i></i> as an opening |
| 938 square bracket followed by <em>n</em> equal signs followed by another | 596 square bracket followed by <i>n</i> equal signs followed by another |
| 939 opening square bracket. | 597 opening square bracket. |
| 940 So, an opening long bracket of level 0 is written as <code>[[</code>, | 598 So, an opening long bracket of level 0 is written as <tt>[[</tt>, |
| 941 an opening long bracket of level 1 is written as <code>[=[</code>, | 599 an opening long bracket of level 1 is written as <tt>[=[</tt>, |
| 942 and so on. | 600 and so on. |
| 943 A <em>closing long bracket</em> is defined similarly; | 601 A <i>closing long bracket</i> is defined similarly; |
| 944 for instance, | 602 for instance, |
| 945 a closing long bracket of level 4 is written as <code>]====]</code>. | 603 a closing long bracket of level 4 is written as <tt>]====]</tt>. |
| 946 A <em>long literal</em> starts with an opening long bracket of any level and | 604 A <i>long literal</i> starts with an opening long bracket of any level and |
| 947 ends at the first closing long bracket of the same level. | 605 ends at the first closing long bracket of the same level. |
| 948 It can contain any text except a closing bracket of the same level. | 606 It can contain any text except a closing bracket of the same level. |
| 949 Literals in this bracketed form can run for several lines, | 607 Literals in this bracketed form can run for several lines, |
| 950 do not interpret any escape sequences, | 608 do not interpret any escape sequences, |
| 951 and ignore long brackets of any other level. | 609 and ignore long brackets of any other level. |
| 954 or newline followed by carriage return) | 612 or newline followed by carriage return) |
| 955 is converted to a simple newline. | 613 is converted to a simple newline. |
| 956 | 614 |
| 957 | 615 |
| 958 <p> | 616 <p> |
| 959 Any byte in a literal string not | 617 Any character in a literal string not |
| 960 explicitly affected by the previous rules represents itself. | 618 explicitly affected by the previous rules represents itself. |
| 961 However, Lua opens files for parsing in text mode, | 619 However, Luan opens files for parsing in text mode, |
| 962 and the system file functions may have problems with | 620 and the system file functions may have problems with |
| 963 some control characters. | 621 some control characters. |
| 964 So, it is safer to represent | 622 So, it is safer to represent |
| 965 non-text data as a quoted literal with | 623 non-text data as a quoted literal with |
| 966 explicit escape sequences for non-text characters. | 624 explicit escape sequences for non-text characters. |
| 968 | 626 |
| 969 <p> | 627 <p> |
| 970 For convenience, | 628 For convenience, |
| 971 when the opening long bracket is immediately followed by a newline, | 629 when the opening long bracket is immediately followed by a newline, |
| 972 the newline is not included in the string. | 630 the newline is not included in the string. |
| 973 As an example, in a system using ASCII | 631 As an example |
| 974 (in which '<code>a</code>' is coded as 97, | |
| 975 newline is coded as 10, and '<code>1</code>' is coded as 49), | |
| 976 the five literal strings below denote the same string: | 632 the five literal strings below denote the same string: |
| 977 | 633 |
| 978 <pre> | 634 <pre> |
| 979 a = 'alo\n123"' | 635 a = 'alo\n123"' |
| 980 a = "alo\n123\"" | 636 a = "alo\n123\"" |
| 982 a = [[alo | 638 a = [[alo |
| 983 123"]] | 639 123"]] |
| 984 a = [==[ | 640 a = [==[ |
| 985 alo | 641 alo |
| 986 123"]==] | 642 123"]==] |
| 643 | |
| 987 </pre> | 644 </pre> |
| 988 | 645 |
| 989 <p> | 646 <p> |
| 990 A <em>numerical constant</em> (or <em>numeral</em>) | 647 A <i>numerical constant</i> (or <i>numeral</i>) |
| 991 can be written with an optional fractional part | 648 can be written with an optional fractional part |
| 992 and an optional decimal exponent, | 649 and an optional decimal exponent, |
| 993 marked by a letter '<code>e</code>' or '<code>E</code>'. | 650 marked by a letter '<tt>e</tt>' or '<tt>E</tt>'. |
| 994 Lua also accepts hexadecimal constants, | 651 Luan also accepts hexadecimal constants, |
| 995 which start with <code>0x</code> or <code>0X</code>. | 652 which start with <tt>0x</tt> or <tt>0X</tt>. |
| 996 Hexadecimal constants also accept an optional fractional part | 653 Hexadecimal constants also accept an optional fractional part |
| 997 plus an optional binary exponent, | 654 plus an optional binary exponent, |
| 998 marked by a letter '<code>p</code>' or '<code>P</code>'. | 655 marked by a letter '<tt>p</tt>' or '<tt>P</tt>'. |
| 999 A numeric constant with a fractional dot or an exponent | 656 A numeric constant with a fractional dot or an exponent |
| 1000 denotes a float; | 657 denotes a float; |
| 1001 otherwise it denotes an integer. | 658 otherwise it denotes an integer. |
| 1002 Examples of valid integer constants are | 659 Examples of valid integer constants are |
| 1003 | 660 |
| 1004 <pre> | 661 <pre> |
| 1005 3 345 0xff 0xBEBADA | 662 3 345 0xff 0xBEBADA |
| 663 | |
| 1006 </pre><p> | 664 </pre><p> |
| 1007 Examples of valid float constants are | 665 Examples of valid float constants are |
| 1008 | 666 |
| 1009 <pre> | 667 <pre> |
| 1010 3.0 3.1416 314.16e-2 0.31416E1 34e1 | 668 3.0 3.1416 314.16e-2 0.31416E1 34e1 |
| 1011 0x0.1E 0xA23p-4 0X1.921FB54442D18P+1 | 669 0x0.1E 0xA23p-4 0X1.921FB54442D18P+1 |
| 670 | |
| 1012 </pre> | 671 </pre> |
| 1013 | 672 |
| 1014 <p> | 673 <p> |
| 1015 A <em>comment</em> starts with a double hyphen (<code>--</code>) | 674 A <i>comment</i> starts with a double hyphen (<tt>--</tt>) |
| 1016 anywhere outside a string. | 675 anywhere outside a string. |
| 1017 If the text immediately after <code>--</code> is not an opening long bracket, | 676 If the text immediately after <tt>--</tt> is not an opening long bracket, |
| 1018 the comment is a <em>short comment</em>, | 677 the comment is a <i>short comment</i>, |
| 1019 which runs until the end of the line. | 678 which runs until the end of the line. |
| 1020 Otherwise, it is a <em>long comment</em>, | 679 Otherwise, it is a <i>long comment</i>, |
| 1021 which runs until the corresponding closing long bracket. | 680 which runs until the corresponding closing long bracket. |
| 1022 Long comments are frequently used to disable code temporarily. | 681 Long comments are frequently used to disable code temporarily. |
| 1023 | 682 |
| 1024 | 683 |
| 1025 | 684 |
| 1026 | 685 |
| 1027 | 686 |
| 1028 <h2>3.2 – <a name="3.2">Variables</a></h2> | 687 <h3 margin-top="1em"><a name="vars">Variables</a></h3> |
| 1029 | 688 |
| 1030 <p> | 689 <p> |
| 1031 Variables are places that store values. | 690 Variables are places that store values. |
| 1032 There are three kinds of variables in Lua: | 691 There are three kinds of variables in Luan: |
| 1033 global variables, local variables, and table fields. | 692 global variables, local variables, and table fields. |
| 1034 | 693 |
| 1035 | 694 |
| 1036 <p> | 695 <p> |
| 1037 A single name can denote a global variable or a local variable | 696 A single name can denote a global variable or a local variable |
| 1038 (or a function's formal parameter, | 697 (or a function's formal parameter, |
| 1039 which is a particular kind of local variable): | 698 which is a particular kind of local variable): |
| 1040 | 699 |
| 1041 <pre> | 700 <pre> |
| 1042 var ::= Name | 701 var ::= Name |
| 702 | |
| 1043 </pre><p> | 703 </pre><p> |
| 1044 Name denotes identifiers, as defined in <a href="#3.1">§3.1</a>. | 704 Name denotes identifiers, as defined in <a href="#3.1">§3.1</a>. |
| 1045 | 705 |
| 1046 | 706 |
| 1047 <p> | 707 <p> |
| 1048 Any variable name is assumed to be global unless explicitly declared | 708 Any variable name is assumed to be global unless explicitly declared |
| 1049 as a local (see <a href="#3.3.7">§3.3.7</a>). | 709 as a local (see <a href="#3.3.7">§3.3.7</a>). |
| 1050 Local variables are <em>lexically scoped</em>: | 710 Local variables are <i>lexically scoped</i>: |
| 1051 local variables can be freely accessed by functions | 711 local variables can be freely accessed by functions |
| 1052 defined inside their scope (see <a href="#3.5">§3.5</a>). | 712 defined inside their scope (see <a href="#3.5">§3.5</a>). |
| 1053 | 713 |
| 1054 | 714 |
| 1055 <p> | 715 <p> |
| 1059 <p> | 719 <p> |
| 1060 Square brackets are used to index a table: | 720 Square brackets are used to index a table: |
| 1061 | 721 |
| 1062 <pre> | 722 <pre> |
| 1063 var ::= prefixexp ‘<b>[</b>’ exp ‘<b>]</b>’ | 723 var ::= prefixexp ‘<b>[</b>’ exp ‘<b>]</b>’ |
| 724 | |
| 1064 </pre><p> | 725 </pre><p> |
| 1065 The meaning of accesses to table fields can be changed via metatables. | 726 The meaning of accesses to table fields can be changed via metatables. |
| 1066 An access to an indexed variable <code>t[i]</code> is equivalent to | 727 An access to an indexed variable <tt>t[i]</tt> is equivalent to |
| 1067 a call <code>gettable_event(t,i)</code>. | 728 a call <tt>gettable_event(t,i)</tt>. |
| 1068 (See <a href="#2.4">§2.4</a> for a complete description of the | 729 (See <a href="#2.4">§2.4</a> for a complete description of the |
| 1069 <code>gettable_event</code> function. | 730 <tt>gettable_event</tt> function. |
| 1070 This function is not defined or callable in Lua. | 731 This function is not defined or callable in Luan. |
| 1071 We use it here only for explanatory purposes.) | 732 We use it here only for explanatory purposes.) |
| 1072 | 733 |
| 1073 | 734 |
| 1074 <p> | 735 <p> |
| 1075 The syntax <code>var.Name</code> is just syntactic sugar for | 736 The syntax <tt>var.Name</tt> is just syntactic sugar for |
| 1076 <code>var["Name"]</code>: | 737 <tt>var["Name"]</tt>: |
| 1077 | 738 |
| 1078 <pre> | 739 <pre> |
| 1079 var ::= prefixexp ‘<b>.</b>’ Name | 740 var ::= prefixexp ‘<b>.</b>’ Name |
| 741 | |
| 1080 </pre> | 742 </pre> |
| 1081 | 743 |
| 1082 <p> | 744 <p> |
| 1083 An access to a global variable <code>x</code> | 745 An access to a global variable <tt>x</tt> |
| 1084 is equivalent to <code>_ENV.x</code>. | 746 is equivalent to <tt>_ENV.x</tt>. |
| 1085 Due to the way that chunks are compiled, | 747 Due to the way that chunks are compiled, |
| 1086 <code>_ENV</code> is never a global name (see <a href="#2.2">§2.2</a>). | 748 <tt>_ENV</tt> is never a global name (see <a href="#2.2">§2.2</a>). |
| 1087 | 749 |
| 1088 | 750 |
| 1089 | 751 |
| 1090 | 752 |
| 1091 | 753 |
