Mercurial Hosting > luan
comparison website/src/manual.html.luan @ 389:497d4ef0a89f
documentation work
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 24 Apr 2015 12:25:50 -0600 |
parents | 23d075ce1e48 |
children | 2f5cc9c2cbf0 |
comparison
equal
deleted
inserted
replaced
388:12ee9a336b95 | 389:497d4ef0a89f |
---|---|
46 <div margin-bottom="1em"> | 46 <div margin-bottom="1em"> |
47 <a href="#lang">The Language</a> | 47 <a href="#lang">The Language</a> |
48 <ul> | 48 <ul> |
49 <li><a href="#lex">Lexical Conventions</a></li> | 49 <li><a href="#lex">Lexical Conventions</a></li> |
50 <li><a href="#vars">Variables</a></li> | 50 <li><a href="#vars">Variables</a></li> |
51 <li> | |
52 <a href="#stmts">Statements</a> | |
53 <ul> | |
54 <li><a href="#blocks">Blocks</a></li> | |
55 </ul> | |
56 </li> | |
51 </ul> | 57 </ul> |
52 </div> | 58 </div> |
53 | 59 |
54 <hr/> | 60 <hr/> |
55 | 61 |
523 <p> | 529 <p> |
524 The following <i>keywords</i> are reserved | 530 The following <i>keywords</i> are reserved |
525 and cannot be used as names: | 531 and cannot be used as names: |
526 | 532 |
527 | 533 |
528 <p><pre> | 534 <p><tt><pre> |
529 and break do else elseif end | 535 and break do else elseif end |
530 false for function goto if in | 536 false for function goto if in |
531 local nil not or repeat return | 537 local nil not or repeat return |
532 then true until while | 538 then true until while |
533 </pre></p> | 539 </pre></tt></p> |
534 | 540 |
535 <p> | 541 <p> |
536 Luan is a case-sensitive language: | 542 Luan is a case-sensitive language: |
537 <tt>and</tt> is a reserved word, but <tt>And</tt> and <tt>AND</tt> | 543 <tt>and</tt> is a reserved word, but <tt>And</tt> and <tt>AND</tt> |
538 are two different, valid names. | 544 are two different, valid names. |
539 | 545 |
540 | 546 |
541 <p> | 547 <p> |
542 The following strings denote other tokens: | 548 The following strings denote other tokens: |
543 | 549 |
544 <p><pre> | 550 <p><tt><pre> |
545 + - * / % ^ # | 551 + - * / % ^ # |
546 & ~ | << >> // | 552 & ~ | << >> // |
547 == ~= <= >= < > = | 553 == ~= <= >= < > = |
548 ( ) { } [ ] :: | 554 ( ) { } [ ] :: |
549 ; : , . .. ... | 555 ; : , . .. ... |
550 </pre></p> | 556 </pre></tt></p> |
551 | 557 |
552 <p> | 558 <p> |
553 <i>Literal strings</i> | 559 <i>Literal strings</i> |
554 can be delimited by matching single or double quotes, | 560 can be delimited by matching single or double quotes, |
555 and can contain the following C-like escape sequences: | 561 and can contain the following C-like escape sequences: |
624 when the opening long bracket is immediately followed by a newline, | 630 when the opening long bracket is immediately followed by a newline, |
625 the newline is not included in the string. | 631 the newline is not included in the string. |
626 As an example | 632 As an example |
627 the five literal strings below denote the same string: | 633 the five literal strings below denote the same string: |
628 | 634 |
629 <p><pre> | 635 <p><tt><pre> |
630 a = 'alo\n123"' | 636 a = 'alo\n123"' |
631 a = "alo\n123\"" | 637 a = "alo\n123\"" |
632 a = '\97lo\10\04923"' | 638 a = '\97lo\10\04923"' |
633 a = [[alo | 639 a = [[alo |
634 123"]] | 640 123"]] |
635 a = [==[ | 641 a = [==[ |
636 alo | 642 alo |
637 123"]==] | 643 123"]==] |
638 </pre></p> | 644 </pre></tt></p> |
639 | 645 |
640 <p> | 646 <p> |
641 A <i>numerical constant</i> (or <i>numeral</i>) | 647 A <i>numerical constant</i> (or <i>numeral</i>) |
642 can be written with an optional fractional part | 648 can be written with an optional fractional part |
643 and an optional decimal exponent, | 649 and an optional decimal exponent, |
650 A numeric constant with a fractional dot or an exponent | 656 A numeric constant with a fractional dot or an exponent |
651 denotes a float; | 657 denotes a float; |
652 otherwise it denotes an integer. | 658 otherwise it denotes an integer. |
653 Examples of valid integer constants are | 659 Examples of valid integer constants are |
654 | 660 |
655 <p><pre> | 661 <p><tt><pre> |
656 3 345 0xff 0xBEBADA | 662 3 345 0xff 0xBEBADA |
657 </pre></p> | 663 </pre></tt></p> |
658 | 664 |
659 <p> | 665 <p> |
660 Examples of valid float constants are | 666 Examples of valid float constants are |
661 | 667 |
662 <p><pre> | 668 <p><tt><pre> |
663 3.0 3.1416 314.16e-2 0.31416E1 34e1 | 669 3.0 3.1416 314.16e-2 0.31416E1 34e1 |
664 0x0.1E 0xA23p-4 0X1.921FB54442D18P+1 | 670 0x0.1E 0xA23p-4 0X1.921FB54442D18P+1 |
665 </pre></p> | 671 </pre></tt></p> |
666 | 672 |
667 <p> | 673 <p> |
668 A <i>comment</i> starts with a double hyphen (<tt>--</tt>) | 674 A <i>comment</i> starts with a double hyphen (<tt>--</tt>) |
669 anywhere outside a string. | 675 anywhere outside a string. |
670 If the text immediately after <tt>--</tt> is not an opening long bracket, | 676 If the text immediately after <tt>--</tt> is not an opening long bracket, |
689 <p> | 695 <p> |
690 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 |
691 (or a function's formal parameter, | 697 (or a function's formal parameter, |
692 which is a particular kind of local variable): | 698 which is a particular kind of local variable): |
693 | 699 |
694 <p><pre> | 700 <p><tt><pre> |
695 var ::= Name | 701 var ::= Name |
696 </pre></p> | 702 </pre></tt></p> |
697 | 703 |
698 <p> | 704 <p> |
699 Name denotes identifiers, as defined in <a href="#3.1">§3.1</a>. | 705 Name denotes identifiers, as defined in <a href="#3.1">§3.1</a>. |
700 | 706 |
701 | 707 |
712 | 718 |
713 | 719 |
714 <p> | 720 <p> |
715 Square brackets are used to index a table: | 721 Square brackets are used to index a table: |
716 | 722 |
717 <p><pre> | 723 <p><tt><pre> |
718 var ::= prefixexp ‘<b>[</b>’ exp ‘<b>]</b>’ | 724 var ::= prefixexp ‘<b>[</b>’ exp ‘<b>]</b>’ |
719 </pre></p> | 725 </pre></tt></p> |
720 | 726 |
721 <p> | 727 <p> |
722 The meaning of accesses to table fields can be changed via metatables. | 728 The meaning of accesses to table fields can be changed via metatables. |
723 An access to an indexed variable <tt>t[i]</tt> is equivalent to | 729 An access to an indexed variable <tt>t[i]</tt> is equivalent to |
724 a call <tt>gettable_event(t,i)</tt>. | 730 a call <tt>gettable_event(t,i)</tt>. |
730 | 736 |
731 <p> | 737 <p> |
732 The syntax <tt>var.Name</tt> is just syntactic sugar for | 738 The syntax <tt>var.Name</tt> is just syntactic sugar for |
733 <tt>var["Name"]</tt>: | 739 <tt>var["Name"]</tt>: |
734 | 740 |
735 <p><pre> | 741 <p><tt><pre> |
736 var ::= prefixexp ‘<b>.</b>’ Name | 742 var ::= prefixexp ‘<b>.</b>’ Name |
737 </pre></p> | 743 </pre></tt></p> |
738 | 744 |
739 <p> | 745 <p> |
740 An access to a global variable <tt>x</tt> | 746 An access to a global variable <tt>x</tt> |
741 is equivalent to <tt>_ENV.x</tt>. | 747 is equivalent to <tt>_ENV.x</tt>. |
742 Due to the way that chunks are compiled, | 748 Due to the way that chunks are compiled, |
744 | 750 |
745 | 751 |
746 | 752 |
747 | 753 |
748 | 754 |
749 <h2>3.3 – <a name="3.3">Statements</a></h2> | 755 <h3 margin-top="1em"><a name="stmts">Statements</a></h3> |
750 | 756 |
751 <p> | 757 <p> |
752 Lua supports an almost conventional set of statements, | 758 Luan supports an almost conventional set of statements, |
753 similar to those in Pascal or C. | 759 similar to those in Pascal or C. |
754 This set includes | 760 This set includes |
755 assignments, control structures, function calls, | 761 assignments, control structures, function calls, |
756 and variable declarations. | 762 and variable declarations. |
757 | 763 |
758 | 764 |
759 | 765 |
760 <h3>3.3.1 – <a name="3.3.1">Blocks</a></h3> | 766 <h3 margin-top="1em"><a name="blocks">Blocks</a></h3> |
761 | 767 |
762 <p> | 768 <p> |
763 A block is a list of statements, | 769 A block is a list of statements, |
764 which are executed sequentially: | 770 which are executed sequentially: |
765 | 771 |
766 <pre> | 772 <p><tt><pre> |
767 block ::= {stat} | 773 block ::= {stat} |
768 </pre><p> | 774 </pre></tt></p> |
769 Lua has <em>empty statements</em> | 775 |
776 <p> | |
777 Luan has <i>empty statements</i> | |
770 that allow you to separate statements with semicolons, | 778 that allow you to separate statements with semicolons, |
771 start a block with a semicolon | 779 start a block with a semicolon |
772 or write two semicolons in sequence: | 780 or write two semicolons in sequence: |
773 | 781 |
774 <pre> | 782 <p><tt><pre> |
775 stat ::= ‘<b>;</b>’ | 783 stat ::= ‘<b>;</b>’ |
776 </pre> | 784 </pre></tt></p> |
777 | |
778 <p> | |
779 Function calls and assignments | |
780 can start with an open parenthesis. | |
781 This possibility leads to an ambiguity in Lua's grammar. | |
782 Consider the following fragment: | |
783 | |
784 <pre> | |
785 a = b + c | |
786 (print or io.write)('done') | |
787 </pre><p> | |
788 The grammar could see it in two ways: | |
789 | |
790 <pre> | |
791 a = b + c(print or io.write)('done') | |
792 | |
793 a = b + c; (print or io.write)('done') | |
794 </pre><p> | |
795 The current parser always sees such constructions | |
796 in the first way, | |
797 interpreting the open parenthesis | |
798 as the start of the arguments to a call. | |
799 To avoid this ambiguity, | |
800 it is a good practice to always precede with a semicolon | |
801 statements that start with a parenthesis: | |
802 | |
803 <pre> | |
804 ;(print or io.write)('done') | |
805 </pre> | |
806 | 785 |
807 <p> | 786 <p> |
808 A block can be explicitly delimited to produce a single statement: | 787 A block can be explicitly delimited to produce a single statement: |
809 | 788 |
810 <pre> | 789 <p><tt><pre> |
811 stat ::= <b>do</b> block <b>end</b> | 790 stat ::= <b>do</b> block <b>end</b> |
812 </pre><p> | 791 </pre></tt></p> |
792 | |
793 <p> | |
813 Explicit blocks are useful | 794 Explicit blocks are useful |
814 to control the scope of variable declarations. | 795 to control the scope of variable declarations. |
815 Explicit blocks are also sometimes used to | 796 Explicit blocks are also sometimes used to |
816 add a <b>return</b> statement in the middle | 797 add a <b>return</b> statement in the middle |
817 of another block (see <a href="#3.3.4">§3.3.4</a>). | 798 of another block (see <a href="#3.3.4">§3.3.4</a>). |