diff options
author | Li Jin <dragon-fly@qq.com> | 2022-04-19 11:06:08 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2022-04-19 11:06:08 +0800 |
commit | ba1081a851dd6b3c76be5ad8bb9cb7fd643fc792 (patch) | |
tree | 86175282e630c3b73eea0aa0712b90c4f672a6c7 | |
parent | e235b0503532918fd984e1bb3d13f6350281fa31 (diff) | |
download | yuescript-ba1081a851dd6b3c76be5ad8bb9cb7fd643fc792.tar.gz yuescript-ba1081a851dd6b3c76be5ad8bb9cb7fd643fc792.tar.bz2 yuescript-ba1081a851dd6b3c76be5ad8bb9cb7fd643fc792.zip |
update doc.
-rw-r--r-- | CMakeLists.txt | 10 | ||||
-rwxr-xr-x | doc/docs/doc/README.md | 637 | ||||
-rw-r--r-- | yuescript-dev-1.rockspec | 4 |
3 files changed, 334 insertions, 317 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 995378f..031af18 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
@@ -20,7 +20,7 @@ ELSE(LUA_FOUND) | |||
20 | NAMES lua lua54 lua53 lua52 lua51 liblua liblua54 liblua53 liblua52 liblua51 liblua5.4 liblua5.3 liblua5.2 liblua5.1 | 20 | NAMES lua lua54 lua53 lua52 lua51 liblua liblua54 liblua53 liblua52 liblua51 liblua5.4 liblua5.3 liblua5.2 liblua5.1 |
21 | PATHS ${LUA_LIBDIR} | 21 | PATHS ${LUA_LIBDIR} |
22 | NO_DEFAULT_PATH) | 22 | NO_DEFAULT_PATH) |
23 | ENDIF(LUA_EXEC_NAME) | 23 | ENDIF(LUA_EXEC_NAME STREQUAL "luajit") |
24 | ENDIF(LUA_FOUND) | 24 | ENDIF(LUA_FOUND) |
25 | 25 | ||
26 | MESSAGE(STATUS "Lua: " ${LUA}) | 26 | MESSAGE(STATUS "Lua: " ${LUA}) |
@@ -39,12 +39,12 @@ target_link_libraries(libyue ${LUA_LIBRARIES}) | |||
39 | find_package(Threads REQUIRED) | 39 | find_package(Threads REQUIRED) |
40 | add_executable(yue src/yuescript/ast.cpp src/yuescript/yue_compiler.cpp src/yuescript/yue_parser.cpp src/yuescript/yuescript.cpp src/yuescript/parser.cpp src/yue.cpp) | 40 | add_executable(yue src/yuescript/ast.cpp src/yuescript/yue_compiler.cpp src/yuescript/yue_parser.cpp src/yuescript/yuescript.cpp src/yuescript/parser.cpp src/yue.cpp) |
41 | target_link_libraries(yue ${LUA_LIBRARIES} Threads::Threads) | 41 | target_link_libraries(yue ${LUA_LIBRARIES} Threads::Threads) |
42 | if(CMAKE_DL_LIBS) | 42 | IF(CMAKE_DL_LIBS) |
43 | target_link_libraries(yue ${CMAKE_DL_LIBS}) | 43 | target_link_libraries(yue ${CMAKE_DL_LIBS}) |
44 | endif() | 44 | ENDIF(CMAKE_DL_LIBS) |
45 | if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") | 45 | IF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") |
46 | target_link_options(yue PRIVATE -lstdc++fs) | 46 | target_link_options(yue PRIVATE -lstdc++fs) |
47 | endif() | 47 | ENDIF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") |
48 | 48 | ||
49 | install(CODE "") | 49 | install(CODE "") |
50 | 50 | ||
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md index fcd7bba..5c43917 100755 --- a/doc/docs/doc/README.md +++ b/doc/docs/doc/README.md | |||
@@ -360,34 +360,17 @@ tb::func! if tb != nil | |||
360 | </pre> | 360 | </pre> |
361 | </YueDisplay> | 361 | </YueDisplay> |
362 | 362 | ||
363 | ### Incrementing and Decrementing | 363 | ### Appending to Tables |
364 | |||
365 | Yuescript adds the **+=** operator for incrementing and **-=** operator for decrementing values. | ||
366 | |||
367 | ```moonscript | ||
368 | i = 0 | ||
369 | i += 1 | ||
370 | i -= 1 | ||
371 | ``` | ||
372 | <YueDisplay> | ||
373 | <pre> | ||
374 | i = 0 | ||
375 | i += 1 | ||
376 | i -= 1 | ||
377 | </pre> | ||
378 | </YueDisplay> | ||
379 | |||
380 | ### Appending to tables | ||
381 | The **[] =** operator is used to append values to tables. | 364 | The **[] =** operator is used to append values to tables. |
382 | 365 | ||
383 | ```moonscript | 366 | ```moonscript |
384 | table = {} | 367 | tab = {} |
385 | table[] = "Value" | 368 | tab[] = "Value" |
386 | ``` | 369 | ``` |
387 | <YueDisplay> | 370 | <YueDisplay> |
388 | <pre> | 371 | <pre> |
389 | table = {} | 372 | tab = {} |
390 | table[] = "Value" | 373 | tab[] = "Value" |
391 | </pre> | 374 | </pre> |
392 | </YueDisplay> | 375 | </YueDisplay> |
393 | 376 | ||
@@ -756,119 +739,50 @@ export default -> | |||
756 | </pre> | 739 | </pre> |
757 | </YueDisplay> | 740 | </YueDisplay> |
758 | 741 | ||
759 | ## Try | ||
760 | |||
761 | The syntax for Lua error handling in a common form. | ||
762 | |||
763 | ```moonscript | ||
764 | try | ||
765 | func 1, 2, 3 | ||
766 | catch err | ||
767 | print yue.traceback err | ||
768 | |||
769 | success, result = try | ||
770 | func 1, 2, 3 | ||
771 | catch err | ||
772 | yue.traceback err | ||
773 | |||
774 | try func 1, 2, 3 | ||
775 | catch err | ||
776 | print yue.traceback err | ||
777 | |||
778 | success, result = try func 1, 2, 3 | ||
779 | |||
780 | try | ||
781 | print "trying" | ||
782 | func 1, 2, 3 | ||
783 | ``` | ||
784 | <YueDisplay> | ||
785 | <pre> | ||
786 | try | ||
787 | func 1, 2, 3 | ||
788 | catch err | ||
789 | print yue.traceback err | ||
790 | |||
791 | success, result = try | ||
792 | func 1, 2, 3 | ||
793 | catch err | ||
794 | yue.traceback err | ||
795 | |||
796 | try func 1, 2, 3 | ||
797 | catch err | ||
798 | print yue.traceback err | ||
799 | |||
800 | success, result = try func 1, 2, 3 | ||
801 | |||
802 | try | ||
803 | print "trying" | ||
804 | func 1, 2, 3 | ||
805 | </pre> | ||
806 | </YueDisplay> | ||
807 | |||
808 | ## Whitespace | ||
809 | |||
810 | Yuescript is a whitespace significant language. You have to write some code block in the same indent with space **' '** or tab **'\t'** like function body, value list and some control blocks. And expressions containing different whitespaces might mean different things. Tab is treated like 4 space, but it's better not mix the use of spaces and tabs. | ||
811 | |||
812 | ### Multiline Chaining | ||
813 | |||
814 | You can write multi-line chaining function calls with a same indent. | ||
815 | ```moonscript | ||
816 | Rx.Observable | ||
817 | .fromRange 1, 8 | ||
818 | \filter (x)-> x % 2 == 0 | ||
819 | \concat Rx.Observable.of 'who do we appreciate' | ||
820 | \map (value)-> value .. '!' | ||
821 | \subscribe print | ||
822 | ``` | ||
823 | <YueDisplay> | ||
824 | <pre> | ||
825 | Rx.Observable | ||
826 | .fromRange 1, 8 | ||
827 | \filter (x)-> x % 2 == 0 | ||
828 | \concat Rx.Observable.of 'who do we appreciate' | ||
829 | \map (value)-> value .. '!' | ||
830 | \subscribe print | ||
831 | </pre> | ||
832 | </YueDisplay> | ||
833 | |||
834 | ## Assignment | 742 | ## Assignment |
835 | 743 | ||
836 | The variable is dynamic typed and is defined as local by default. But you can change the scope of declaration by **local** and **global** statement. | 744 | The variable is dynamic typed and is defined as local by default. But you can change the scope of declaration by **local** and **global** statement. |
837 | 745 | ||
838 | * **Common Use** | ||
839 | ```moonscript | 746 | ```moonscript |
840 | hello = "world" | 747 | hello = "world" |
841 | a, b, c = 1, 2, 3 | 748 | a, b, c = 1, 2, 3 |
842 | hello = 123 -- uses the existing variable | 749 | hello = 123 -- uses the existing variable |
843 | |||
844 | -- update Assignment | ||
845 | x = 0 | ||
846 | x += 10 | ||
847 | |||
848 | s = "hello " | ||
849 | s ..= "world" | ||
850 | |||
851 | arg or= "default value" | ||
852 | ``` | 750 | ``` |
853 | <YueDisplay> | 751 | <YueDisplay> |
854 | |||
855 | <pre> | 752 | <pre> |
856 | hello = "world" | 753 | hello = "world" |
857 | a, b, c = 1, 2, 3 | 754 | a, b, c = 1, 2, 3 |
858 | hello = 123 -- uses the existing variable | 755 | hello = 123 -- uses the existing variable |
756 | </pre> | ||
757 | </YueDisplay> | ||
859 | 758 | ||
860 | -- update Assignment | 759 | ### Perform Update |
861 | x = 0 | ||
862 | x += 10 | ||
863 | |||
864 | s = "hello " | ||
865 | s ..= "world" | ||
866 | 760 | ||
761 | You can perform update assignment with many binary operators. | ||
762 | ```moonscript | ||
763 | x = 1 | ||
764 | x += 1 | ||
765 | x -= 1 | ||
766 | x *= 10 | ||
767 | x /= 10 | ||
768 | x %= 10 | ||
769 | s ..= "world" -- will add a new local if local variable is not exist | ||
770 | arg or= "default value" | ||
771 | ``` | ||
772 | <YueDisplay> | ||
773 | <pre> | ||
774 | x = 1 | ||
775 | x += 1 | ||
776 | x -= 1 | ||
777 | x *= 10 | ||
778 | x /= 10 | ||
779 | x %= 10 | ||
780 | s ..= "world" -- will add a new local if local variable is not exist | ||
867 | arg or= "default value" | 781 | arg or= "default value" |
868 | </pre> | 782 | </pre> |
869 | </YueDisplay> | 783 | </YueDisplay> |
870 | 784 | ||
871 | * **Explicit Locals** | 785 | ### Explicit Locals |
872 | ```moonscript | 786 | ```moonscript |
873 | do | 787 | do |
874 | local * | 788 | local * |
@@ -884,7 +798,6 @@ do | |||
884 | B = 2 | 798 | B = 2 |
885 | ``` | 799 | ``` |
886 | <YueDisplay> | 800 | <YueDisplay> |
887 | |||
888 | <pre> | 801 | <pre> |
889 | do | 802 | do |
890 | local * | 803 | local * |
@@ -901,7 +814,7 @@ do | |||
901 | </pre> | 814 | </pre> |
902 | </YueDisplay> | 815 | </YueDisplay> |
903 | 816 | ||
904 | * **Explicit Globals** | 817 | ### Explicit Globals |
905 | ```moonscript | 818 | ```moonscript |
906 | do | 819 | do |
907 | global * | 820 | global * |
@@ -933,6 +846,242 @@ do | |||
933 | </pre> | 846 | </pre> |
934 | </YueDisplay> | 847 | </YueDisplay> |
935 | 848 | ||
849 | ## Destructuring Assignment | ||
850 | |||
851 | Destructuring assignment is a way to quickly extract values from a table by their name or position in array based tables. | ||
852 | |||
853 | Typically when you see a table literal, {1,2,3}, it is on the right hand side of an assignment because it is a value. Destructuring assignment swaps the role of the table literal, and puts it on the left hand side of an assign statement. | ||
854 | |||
855 | This is best explained with examples. Here is how you would unpack the first two values from a table: | ||
856 | |||
857 | ```moonscript | ||
858 | thing = {1, 2} | ||
859 | |||
860 | {a, b} = thing | ||
861 | print a, b | ||
862 | ``` | ||
863 | <YueDisplay> | ||
864 | <pre> | ||
865 | thing = {1, 2} | ||
866 | |||
867 | {a, b} = thing | ||
868 | print a, b | ||
869 | </pre> | ||
870 | </YueDisplay> | ||
871 | |||
872 | In the destructuring table literal, the key represents the key to read from the right hand side, and the value represents the name the read value will be assigned to. | ||
873 | |||
874 | ```moonscript | ||
875 | obj = { | ||
876 | hello: "world" | ||
877 | day: "tuesday" | ||
878 | length: 20 | ||
879 | } | ||
880 | |||
881 | {hello: hello, day: the_day} = obj | ||
882 | print hello, the_day | ||
883 | |||
884 | :day = obj -- OK to do simple destructuring without braces | ||
885 | ``` | ||
886 | <YueDisplay> | ||
887 | <pre> | ||
888 | obj = { | ||
889 | hello: "world" | ||
890 | day: "tuesday" | ||
891 | length: 20 | ||
892 | } | ||
893 | |||
894 | {hello: hello, day: the_day} = obj | ||
895 | print hello, the_day | ||
896 | |||
897 | :day = obj -- OK to do simple destructuring without braces | ||
898 | </pre> | ||
899 | </YueDisplay> | ||
900 | |||
901 | This also works with nested data structures as well: | ||
902 | |||
903 | ```moonscript | ||
904 | obj2 = { | ||
905 | numbers: {1,2,3,4} | ||
906 | properties: { | ||
907 | color: "green" | ||
908 | height: 13.5 | ||
909 | } | ||
910 | } | ||
911 | |||
912 | {numbers: {first, second}} = obj2 | ||
913 | print first, second, color | ||
914 | ``` | ||
915 | <YueDisplay> | ||
916 | <pre> | ||
917 | obj2 = { | ||
918 | numbers: {1,2,3,4} | ||
919 | properties: { | ||
920 | color: "green" | ||
921 | height: 13.5 | ||
922 | } | ||
923 | } | ||
924 | |||
925 | {numbers: {first, second}} = obj2 | ||
926 | print first, second, color | ||
927 | </pre> | ||
928 | </YueDisplay> | ||
929 | |||
930 | If the destructuring statement is complicated, feel free to spread it out over a few lines. A slightly more complicated example: | ||
931 | |||
932 | ```moonscript | ||
933 | { | ||
934 | numbers: {first, second} | ||
935 | properties: { | ||
936 | color: color | ||
937 | } | ||
938 | } = obj2 | ||
939 | ``` | ||
940 | <YueDisplay> | ||
941 | <pre> | ||
942 | { | ||
943 | numbers: {first, second} | ||
944 | properties: { | ||
945 | color: color | ||
946 | } | ||
947 | } = obj2 | ||
948 | </pre> | ||
949 | </YueDisplay> | ||
950 | |||
951 | It’s common to extract values from at table and assign them the local variables that have the same name as the key. In order to avoid repetition we can use the **:** prefix operator: | ||
952 | |||
953 | ```moonscript | ||
954 | {:concat, :insert} = table | ||
955 | ``` | ||
956 | <YueDisplay> | ||
957 | <pre> | ||
958 | {:concat, :insert} = table | ||
959 | </pre> | ||
960 | </YueDisplay> | ||
961 | |||
962 | This is effectively the same as import, but we can rename fields we want to extract by mixing the syntax: | ||
963 | |||
964 | ```moonscript | ||
965 | {:mix, :max, random: rand} = math | ||
966 | ``` | ||
967 | <YueDisplay> | ||
968 | <pre> | ||
969 | {:mix, :max, random: rand} = math | ||
970 | </pre> | ||
971 | </YueDisplay> | ||
972 | |||
973 | You can write default values while doing destructuring like: | ||
974 | |||
975 | ```moonscript | ||
976 | {:name = "nameless", :job = "jobless"} = person | ||
977 | ``` | ||
978 | <YueDisplay> | ||
979 | <pre> | ||
980 | {:name = "nameless", :job = "jobless"} = person | ||
981 | </pre> | ||
982 | </YueDisplay> | ||
983 | |||
984 | ### Destructuring In Other Places | ||
985 | |||
986 | Destructuring can also show up in places where an assignment implicitly takes place. An example of this is a for loop: | ||
987 | |||
988 | ```moonscript | ||
989 | tuples = { | ||
990 | {"hello", "world"} | ||
991 | {"egg", "head"} | ||
992 | } | ||
993 | |||
994 | for {left, right} in *tuples | ||
995 | print left, right | ||
996 | ``` | ||
997 | <YueDisplay> | ||
998 | <pre> | ||
999 | tuples = { | ||
1000 | {"hello", "world"} | ||
1001 | {"egg", "head"} | ||
1002 | } | ||
1003 | |||
1004 | for {left, right} in *tuples | ||
1005 | print left, right | ||
1006 | </pre> | ||
1007 | </YueDisplay> | ||
1008 | |||
1009 | We know each element in the array table is a two item tuple, so we can unpack it directly in the names clause of the for statement using a destructure. | ||
1010 | |||
1011 | ## If Assignment | ||
1012 | |||
1013 | if and elseif blocks can take an assignment in place of a conditional expression. Upon evaluating the conditional, the assignment will take place and the value that was assigned to will be used as the conditional expression. The assigned variable is only in scope for the body of the conditional, meaning it is never available if the value is not truthy. | ||
1014 | |||
1015 | ```moonscript | ||
1016 | if user = database.find_user "moon" | ||
1017 | print user.name | ||
1018 | ``` | ||
1019 | <YueDisplay> | ||
1020 | <pre> | ||
1021 | if user = database.find_user "moon" | ||
1022 | print user.name | ||
1023 | </pre> | ||
1024 | </YueDisplay> | ||
1025 | |||
1026 | ```moonscript | ||
1027 | if hello = os.getenv "hello" | ||
1028 | print "You have hello", hello | ||
1029 | elseif world = os.getenv "world" | ||
1030 | print "you have world", world | ||
1031 | else | ||
1032 | print "nothing :(" | ||
1033 | ``` | ||
1034 | <YueDisplay> | ||
1035 | <pre> | ||
1036 | if hello = os.getenv "hello" | ||
1037 | print "You have hello", hello | ||
1038 | elseif world = os.getenv "world" | ||
1039 | print "you have world", world | ||
1040 | else | ||
1041 | print "nothing :(" | ||
1042 | </pre> | ||
1043 | </YueDisplay> | ||
1044 | |||
1045 | If assignment with extra return values. | ||
1046 | ```moonscript | ||
1047 | if success, result = pcall -> "get result without problems" | ||
1048 | print result -- variable result is scoped | ||
1049 | print "OK" | ||
1050 | ``` | ||
1051 | <YueDisplay> | ||
1052 | <pre> | ||
1053 | if success, result = pcall -> "get result without problems" | ||
1054 | print result -- variable result is scoped | ||
1055 | print "OK" | ||
1056 | </pre> | ||
1057 | </YueDisplay> | ||
1058 | |||
1059 | ## Whitespace | ||
1060 | |||
1061 | Yuescript is a whitespace significant language. You have to write some code block in the same indent with space **' '** or tab **'\t'** like function body, value list and some control blocks. And expressions containing different whitespaces might mean different things. Tab is treated like 4 space, but it's better not mix the use of spaces and tabs. | ||
1062 | |||
1063 | ### Multiline Chaining | ||
1064 | |||
1065 | You can write multi-line chaining function calls with a same indent. | ||
1066 | ```moonscript | ||
1067 | Rx.Observable | ||
1068 | .fromRange 1, 8 | ||
1069 | \filter (x)-> x % 2 == 0 | ||
1070 | \concat Rx.Observable.of 'who do we appreciate' | ||
1071 | \map (value)-> value .. '!' | ||
1072 | \subscribe print | ||
1073 | ``` | ||
1074 | <YueDisplay> | ||
1075 | <pre> | ||
1076 | Rx.Observable | ||
1077 | .fromRange 1, 8 | ||
1078 | \filter (x)-> x % 2 == 0 | ||
1079 | \concat Rx.Observable.of 'who do we appreciate' | ||
1080 | \map (value)-> value .. '!' | ||
1081 | \subscribe print | ||
1082 | </pre> | ||
1083 | </YueDisplay> | ||
1084 | |||
936 | ## Comment | 1085 | ## Comment |
937 | 1086 | ||
938 | ```moonscript | 1087 | ```moonscript |
@@ -962,6 +1111,67 @@ func --[[port]] 3000, --[[ip]] "192.168.1.1" | |||
962 | </pre> | 1111 | </pre> |
963 | </YueDisplay> | 1112 | </YueDisplay> |
964 | 1113 | ||
1114 | ## Try | ||
1115 | |||
1116 | The syntax for Lua error handling in a common form. | ||
1117 | |||
1118 | ```moonscript | ||
1119 | try | ||
1120 | func 1, 2, 3 | ||
1121 | catch err | ||
1122 | print yue.traceback err | ||
1123 | |||
1124 | success, result = try | ||
1125 | func 1, 2, 3 | ||
1126 | catch err | ||
1127 | yue.traceback err | ||
1128 | |||
1129 | try func 1, 2, 3 | ||
1130 | catch err | ||
1131 | print yue.traceback err | ||
1132 | |||
1133 | success, result = try func 1, 2, 3 | ||
1134 | |||
1135 | try | ||
1136 | print "trying" | ||
1137 | func 1, 2, 3 | ||
1138 | |||
1139 | -- working with if assignment pattern | ||
1140 | if success, result = try func 1, 2, 3 | ||
1141 | catch err | ||
1142 | print yue.traceback err | ||
1143 | print result | ||
1144 | ``` | ||
1145 | <YueDisplay> | ||
1146 | <pre> | ||
1147 | try | ||
1148 | func 1, 2, 3 | ||
1149 | catch err | ||
1150 | print yue.traceback err | ||
1151 | |||
1152 | success, result = try | ||
1153 | func 1, 2, 3 | ||
1154 | catch err | ||
1155 | yue.traceback err | ||
1156 | |||
1157 | try func 1, 2, 3 | ||
1158 | catch err | ||
1159 | print yue.traceback err | ||
1160 | |||
1161 | success, result = try func 1, 2, 3 | ||
1162 | |||
1163 | try | ||
1164 | print "trying" | ||
1165 | func 1, 2, 3 | ||
1166 | |||
1167 | -- working with if assignment pattern | ||
1168 | if success, result = try func 1, 2, 3 | ||
1169 | catch err | ||
1170 | print yue.traceback err | ||
1171 | print result | ||
1172 | </pre> | ||
1173 | </YueDisplay> | ||
1174 | |||
965 | ::: warning NOTICE | 1175 | ::: warning NOTICE |
966 | The rest of the document is describing mostly the same syntax taken from Moonscript. So you may as well refer to the [Moonscript Reference](http://moonscript.org/reference) to get the same explanation. | 1176 | The rest of the document is describing mostly the same syntax taken from Moonscript. So you may as well refer to the [Moonscript Reference](http://moonscript.org/reference) to get the same explanation. |
967 | ::: | 1177 | ::: |
@@ -1882,40 +2092,6 @@ print "You're lucky!" unless math.random! > 0.1 | |||
1882 | </pre> | 2092 | </pre> |
1883 | </YueDisplay> | 2093 | </YueDisplay> |
1884 | 2094 | ||
1885 | ## If Assignment | ||
1886 | |||
1887 | if and elseif blocks can take an assignment in place of a conditional expression. Upon evaluating the conditional, the assignment will take place and the value that was assigned to will be used as the conditional expression. The assigned variable is only in scope for the body of the conditional, meaning it is never available if the value is not truthy. | ||
1888 | |||
1889 | ```moonscript | ||
1890 | if user = database.find_user "moon" | ||
1891 | print user.name | ||
1892 | ``` | ||
1893 | <YueDisplay> | ||
1894 | <pre> | ||
1895 | if user = database.find_user "moon" | ||
1896 | print user.name | ||
1897 | </pre> | ||
1898 | </YueDisplay> | ||
1899 | |||
1900 | ```moonscript | ||
1901 | if hello = os.getenv "hello" | ||
1902 | print "You have hello", hello | ||
1903 | elseif world = os.getenv "world" | ||
1904 | print "you have world", world | ||
1905 | else | ||
1906 | print "nothing :(" | ||
1907 | ``` | ||
1908 | <YueDisplay> | ||
1909 | <pre> | ||
1910 | if hello = os.getenv "hello" | ||
1911 | print "You have hello", hello | ||
1912 | elseif world = os.getenv "world" | ||
1913 | print "you have world", world | ||
1914 | else | ||
1915 | print "nothing :(" | ||
1916 | </pre> | ||
1917 | </YueDisplay> | ||
1918 | |||
1919 | ## Line Decorators | 2095 | ## Line Decorators |
1920 | 2096 | ||
1921 | For convenience, the for loop and if statement can be applied to single statements at the end of the line: | 2097 | For convenience, the for loop and if statement can be applied to single statements at the end of the line: |
@@ -2575,165 +2751,6 @@ tbl = { | |||
2575 | </pre> | 2751 | </pre> |
2576 | </YueDisplay> | 2752 | </YueDisplay> |
2577 | 2753 | ||
2578 | ## Destructuring Assignment | ||
2579 | |||
2580 | Destructuring assignment is a way to quickly extract values from a table by their name or position in array based tables. | ||
2581 | |||
2582 | Typically when you see a table literal, {1,2,3}, it is on the right hand side of an assignment because it is a value. Destructuring assignment swaps the role of the table literal, and puts it on the left hand side of an assign statement. | ||
2583 | |||
2584 | This is best explained with examples. Here is how you would unpack the first two values from a table: | ||
2585 | |||
2586 | ```moonscript | ||
2587 | thing = {1, 2} | ||
2588 | |||
2589 | {a, b} = thing | ||
2590 | print a, b | ||
2591 | ``` | ||
2592 | <YueDisplay> | ||
2593 | <pre> | ||
2594 | thing = {1, 2} | ||
2595 | |||
2596 | {a, b} = thing | ||
2597 | print a, b | ||
2598 | </pre> | ||
2599 | </YueDisplay> | ||
2600 | |||
2601 | In the destructuring table literal, the key represents the key to read from the right hand side, and the value represents the name the read value will be assigned to. | ||
2602 | |||
2603 | ```moonscript | ||
2604 | obj = { | ||
2605 | hello: "world" | ||
2606 | day: "tuesday" | ||
2607 | length: 20 | ||
2608 | } | ||
2609 | |||
2610 | {hello: hello, day: the_day} = obj | ||
2611 | print hello, the_day | ||
2612 | ``` | ||
2613 | <YueDisplay> | ||
2614 | <pre> | ||
2615 | obj = { | ||
2616 | hello: "world" | ||
2617 | day: "tuesday" | ||
2618 | length: 20 | ||
2619 | } | ||
2620 | |||
2621 | {hello: hello, day: the_day} = obj | ||
2622 | print hello, the_day | ||
2623 | </pre> | ||
2624 | </YueDisplay> | ||
2625 | |||
2626 | This also works with nested data structures as well: | ||
2627 | |||
2628 | ```moonscript | ||
2629 | obj2 = { | ||
2630 | numbers: {1,2,3,4} | ||
2631 | properties: { | ||
2632 | color: "green" | ||
2633 | height: 13.5 | ||
2634 | } | ||
2635 | } | ||
2636 | |||
2637 | {numbers: {first, second}} = obj2 | ||
2638 | print first, second, color | ||
2639 | ``` | ||
2640 | <YueDisplay> | ||
2641 | <pre> | ||
2642 | obj2 = { | ||
2643 | numbers: {1,2,3,4} | ||
2644 | properties: { | ||
2645 | color: "green" | ||
2646 | height: 13.5 | ||
2647 | } | ||
2648 | } | ||
2649 | |||
2650 | {numbers: {first, second}} = obj2 | ||
2651 | print first, second, color | ||
2652 | </pre> | ||
2653 | </YueDisplay> | ||
2654 | |||
2655 | If the destructuring statement is complicated, feel free to spread it out over a few lines. A slightly more complicated example: | ||
2656 | |||
2657 | ```moonscript | ||
2658 | { | ||
2659 | numbers: {first, second} | ||
2660 | properties: { | ||
2661 | color: color | ||
2662 | } | ||
2663 | } = obj2 | ||
2664 | ``` | ||
2665 | <YueDisplay> | ||
2666 | <pre> | ||
2667 | { | ||
2668 | numbers: {first, second} | ||
2669 | properties: { | ||
2670 | color: color | ||
2671 | } | ||
2672 | } = obj2 | ||
2673 | </pre> | ||
2674 | </YueDisplay> | ||
2675 | |||
2676 | It’s common to extract values from at table and assign them the local variables that have the same name as the key. In order to avoid repetition we can use the **:** prefix operator: | ||
2677 | |||
2678 | ```moonscript | ||
2679 | {:concat, :insert} = table | ||
2680 | ``` | ||
2681 | <YueDisplay> | ||
2682 | <pre> | ||
2683 | {:concat, :insert} = table | ||
2684 | </pre> | ||
2685 | </YueDisplay> | ||
2686 | |||
2687 | This is effectively the same as import, but we can rename fields we want to extract by mixing the syntax: | ||
2688 | |||
2689 | ```moonscript | ||
2690 | {:mix, :max, random: rand} = math | ||
2691 | ``` | ||
2692 | <YueDisplay> | ||
2693 | <pre> | ||
2694 | {:mix, :max, random: rand} = math | ||
2695 | </pre> | ||
2696 | </YueDisplay> | ||
2697 | |||
2698 | You can write default values while doing destructuring like: | ||
2699 | |||
2700 | ```moonscript | ||
2701 | {:name = "nameless", :job = "jobless"} = person | ||
2702 | ``` | ||
2703 | <YueDisplay> | ||
2704 | <pre> | ||
2705 | {:name = "nameless", :job = "jobless"} = person | ||
2706 | </pre> | ||
2707 | </YueDisplay> | ||
2708 | |||
2709 | |||
2710 | ### Destructuring In Other Places | ||
2711 | |||
2712 | Destructuring can also show up in places where an assignment implicitly takes place. An example of this is a for loop: | ||
2713 | |||
2714 | ```moonscript | ||
2715 | tuples = { | ||
2716 | {"hello", "world"} | ||
2717 | {"egg", "head"} | ||
2718 | } | ||
2719 | |||
2720 | for {left, right} in *tuples | ||
2721 | print left, right | ||
2722 | ``` | ||
2723 | <YueDisplay> | ||
2724 | <pre> | ||
2725 | tuples = { | ||
2726 | {"hello", "world"} | ||
2727 | {"egg", "head"} | ||
2728 | } | ||
2729 | |||
2730 | for {left, right} in *tuples | ||
2731 | print left, right | ||
2732 | </pre> | ||
2733 | </YueDisplay> | ||
2734 | |||
2735 | We know each element in the array table is a two item tuple, so we can unpack it directly in the names clause of the for statement using a destructure. | ||
2736 | |||
2737 | ## Function Stubs | 2754 | ## Function Stubs |
2738 | 2755 | ||
2739 | It is common to pass a function from an object around as a value, for example, passing an instance method into a function as a callback. If the function expects the object it is operating on as the first argument then you must somehow bundle that object with the function so it can be called properly. | 2756 | It is common to pass a function from an object around as a value, for example, passing an instance method into a function as a callback. If the function expects the object it is operating on as the first argument then you must somehow bundle that object with the function so it can be called properly. |
diff --git a/yuescript-dev-1.rockspec b/yuescript-dev-1.rockspec index 75835fc..a8d9eea 100644 --- a/yuescript-dev-1.rockspec +++ b/yuescript-dev-1.rockspec | |||
@@ -19,8 +19,8 @@ dependencies = { | |||
19 | build = { | 19 | build = { |
20 | type = "cmake", | 20 | type = "cmake", |
21 | variables = { | 21 | variables = { |
22 | LUA_INCDIR="$(LUA_INCDIR)", | 22 | LUA_INCDIR = "$(LUA_INCDIR)", |
23 | LUA="$(LUA)", | 23 | LUA = "$(LUA)", |
24 | }, | 24 | }, |
25 | install = { | 25 | install = { |
26 | lib = { | 26 | lib = { |