aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2022-04-19 11:06:08 +0800
committerLi Jin <dragon-fly@qq.com>2022-04-19 11:06:08 +0800
commitba1081a851dd6b3c76be5ad8bb9cb7fd643fc792 (patch)
tree86175282e630c3b73eea0aa0712b90c4f672a6c7
parente235b0503532918fd984e1bb3d13f6350281fa31 (diff)
downloadyuescript-ba1081a851dd6b3c76be5ad8bb9cb7fd643fc792.tar.gz
yuescript-ba1081a851dd6b3c76be5ad8bb9cb7fd643fc792.tar.bz2
yuescript-ba1081a851dd6b3c76be5ad8bb9cb7fd643fc792.zip
update doc.
-rw-r--r--CMakeLists.txt10
-rwxr-xr-xdoc/docs/doc/README.md637
-rw-r--r--yuescript-dev-1.rockspec4
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")
24ENDIF(LUA_FOUND) 24ENDIF(LUA_FOUND)
25 25
26MESSAGE(STATUS "Lua: " ${LUA}) 26MESSAGE(STATUS "Lua: " ${LUA})
@@ -39,12 +39,12 @@ target_link_libraries(libyue ${LUA_LIBRARIES})
39find_package(Threads REQUIRED) 39find_package(Threads REQUIRED)
40add_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) 40add_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)
41target_link_libraries(yue ${LUA_LIBRARIES} Threads::Threads) 41target_link_libraries(yue ${LUA_LIBRARIES} Threads::Threads)
42if(CMAKE_DL_LIBS) 42IF(CMAKE_DL_LIBS)
43 target_link_libraries(yue ${CMAKE_DL_LIBS}) 43 target_link_libraries(yue ${CMAKE_DL_LIBS})
44endif() 44ENDIF(CMAKE_DL_LIBS)
45if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 45IF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
46 target_link_options(yue PRIVATE -lstdc++fs) 46 target_link_options(yue PRIVATE -lstdc++fs)
47endif() 47ENDIF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
48 48
49install(CODE "") 49install(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
365Yuescript adds the **+=** operator for incrementing and **-=** operator for decrementing values.
366
367```moonscript
368i = 0
369i += 1
370i -= 1
371```
372<YueDisplay>
373<pre>
374i = 0
375i += 1
376i -= 1
377</pre>
378</YueDisplay>
379
380### Appending to tables
381The **[] =** operator is used to append values to tables. 364The **[] =** operator is used to append values to tables.
382 365
383```moonscript 366```moonscript
384table = {} 367tab = {}
385table[] = "Value" 368tab[] = "Value"
386``` 369```
387<YueDisplay> 370<YueDisplay>
388<pre> 371<pre>
389table = {} 372tab = {}
390table[] = "Value" 373tab[] = "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
761The syntax for Lua error handling in a common form.
762
763```moonscript
764try
765 func 1, 2, 3
766catch err
767 print yue.traceback err
768
769success, result = try
770 func 1, 2, 3
771catch err
772 yue.traceback err
773
774try func 1, 2, 3
775catch err
776 print yue.traceback err
777
778success, result = try func 1, 2, 3
779
780try
781 print "trying"
782 func 1, 2, 3
783```
784<YueDisplay>
785<pre>
786try
787 func 1, 2, 3
788catch err
789 print yue.traceback err
790
791success, result = try
792 func 1, 2, 3
793catch err
794 yue.traceback err
795
796try func 1, 2, 3
797catch err
798 print yue.traceback err
799
800success, result = try func 1, 2, 3
801
802try
803 print "trying"
804 func 1, 2, 3
805</pre>
806</YueDisplay>
807
808## Whitespace
809
810Yuescript 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
814You can write multi-line chaining function calls with a same indent.
815```moonscript
816Rx.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>
825Rx.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
836The variable is dynamic typed and is defined as local by default. But you can change the scope of declaration by **local** and **global** statement. 744The 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
840hello = "world" 747hello = "world"
841a, b, c = 1, 2, 3 748a, b, c = 1, 2, 3
842hello = 123 -- uses the existing variable 749hello = 123 -- uses the existing variable
843
844-- update Assignment
845x = 0
846x += 10
847
848s = "hello "
849s ..= "world"
850
851arg or= "default value"
852``` 750```
853<YueDisplay> 751<YueDisplay>
854
855<pre> 752<pre>
856hello = "world" 753hello = "world"
857a, b, c = 1, 2, 3 754a, b, c = 1, 2, 3
858hello = 123 -- uses the existing variable 755hello = 123 -- uses the existing variable
756</pre>
757</YueDisplay>
859 758
860-- update Assignment 759### Perform Update
861x = 0
862x += 10
863
864s = "hello "
865s ..= "world"
866 760
761You can perform update assignment with many binary operators.
762```moonscript
763x = 1
764x += 1
765x -= 1
766x *= 10
767x /= 10
768x %= 10
769s ..= "world" -- will add a new local if local variable is not exist
770arg or= "default value"
771```
772<YueDisplay>
773<pre>
774x = 1
775x += 1
776x -= 1
777x *= 10
778x /= 10
779x %= 10
780s ..= "world" -- will add a new local if local variable is not exist
867arg or= "default value" 781arg or= "default value"
868</pre> 782</pre>
869</YueDisplay> 783</YueDisplay>
870 784
871* **Explicit Locals** 785### Explicit Locals
872```moonscript 786```moonscript
873do 787do
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>
889do 802do
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
906do 819do
907 global * 820 global *
@@ -933,6 +846,242 @@ do
933</pre> 846</pre>
934</YueDisplay> 847</YueDisplay>
935 848
849## Destructuring Assignment
850
851Destructuring assignment is a way to quickly extract values from a table by their name or position in array based tables.
852
853Typically 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
855This is best explained with examples. Here is how you would unpack the first two values from a table:
856
857```moonscript
858thing = {1, 2}
859
860{a, b} = thing
861print a, b
862```
863<YueDisplay>
864<pre>
865thing = {1, 2}
866
867{a, b} = thing
868print a, b
869</pre>
870</YueDisplay>
871
872In 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
875obj = {
876 hello: "world"
877 day: "tuesday"
878 length: 20
879}
880
881{hello: hello, day: the_day} = obj
882print hello, the_day
883
884:day = obj -- OK to do simple destructuring without braces
885```
886<YueDisplay>
887<pre>
888obj = {
889 hello: "world"
890 day: "tuesday"
891 length: 20
892}
893
894{hello: hello, day: the_day} = obj
895print hello, the_day
896
897:day = obj -- OK to do simple destructuring without braces
898</pre>
899</YueDisplay>
900
901This also works with nested data structures as well:
902
903```moonscript
904obj2 = {
905 numbers: {1,2,3,4}
906 properties: {
907 color: "green"
908 height: 13.5
909 }
910}
911
912{numbers: {first, second}} = obj2
913print first, second, color
914```
915<YueDisplay>
916<pre>
917obj2 = {
918 numbers: {1,2,3,4}
919 properties: {
920 color: "green"
921 height: 13.5
922 }
923}
924
925{numbers: {first, second}} = obj2
926print first, second, color
927</pre>
928</YueDisplay>
929
930If 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
951It’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
962This 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
973You 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
986Destructuring can also show up in places where an assignment implicitly takes place. An example of this is a for loop:
987
988```moonscript
989tuples = {
990 {"hello", "world"}
991 {"egg", "head"}
992}
993
994for {left, right} in *tuples
995 print left, right
996```
997<YueDisplay>
998<pre>
999tuples = {
1000 {"hello", "world"}
1001 {"egg", "head"}
1002}
1003
1004for {left, right} in *tuples
1005 print left, right
1006</pre>
1007</YueDisplay>
1008
1009We 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
1013if 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
1016if user = database.find_user "moon"
1017 print user.name
1018```
1019<YueDisplay>
1020<pre>
1021if user = database.find_user "moon"
1022 print user.name
1023</pre>
1024</YueDisplay>
1025
1026```moonscript
1027if hello = os.getenv "hello"
1028 print "You have hello", hello
1029elseif world = os.getenv "world"
1030 print "you have world", world
1031else
1032 print "nothing :("
1033```
1034<YueDisplay>
1035<pre>
1036if hello = os.getenv "hello"
1037 print "You have hello", hello
1038elseif world = os.getenv "world"
1039 print "you have world", world
1040else
1041 print "nothing :("
1042</pre>
1043</YueDisplay>
1044
1045If assignment with extra return values.
1046```moonscript
1047if success, result = pcall -> "get result without problems"
1048 print result -- variable result is scoped
1049print "OK"
1050```
1051<YueDisplay>
1052<pre>
1053if success, result = pcall -> "get result without problems"
1054 print result -- variable result is scoped
1055print "OK"
1056</pre>
1057</YueDisplay>
1058
1059## Whitespace
1060
1061Yuescript 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
1065You can write multi-line chaining function calls with a same indent.
1066```moonscript
1067Rx.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>
1076Rx.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
1116The syntax for Lua error handling in a common form.
1117
1118```moonscript
1119try
1120 func 1, 2, 3
1121catch err
1122 print yue.traceback err
1123
1124success, result = try
1125 func 1, 2, 3
1126catch err
1127 yue.traceback err
1128
1129try func 1, 2, 3
1130catch err
1131 print yue.traceback err
1132
1133success, result = try func 1, 2, 3
1134
1135try
1136 print "trying"
1137 func 1, 2, 3
1138
1139-- working with if assignment pattern
1140if success, result = try func 1, 2, 3
1141catch err
1142 print yue.traceback err
1143 print result
1144```
1145<YueDisplay>
1146<pre>
1147try
1148 func 1, 2, 3
1149catch err
1150 print yue.traceback err
1151
1152success, result = try
1153 func 1, 2, 3
1154catch err
1155 yue.traceback err
1156
1157try func 1, 2, 3
1158catch err
1159 print yue.traceback err
1160
1161success, result = try func 1, 2, 3
1162
1163try
1164 print "trying"
1165 func 1, 2, 3
1166
1167-- working with if assignment pattern
1168if success, result = try func 1, 2, 3
1169catch err
1170 print yue.traceback err
1171 print result
1172</pre>
1173</YueDisplay>
1174
965::: warning NOTICE 1175::: warning NOTICE
966The 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. 1176The 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
1887if 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
1890if user = database.find_user "moon"
1891 print user.name
1892```
1893<YueDisplay>
1894<pre>
1895if user = database.find_user "moon"
1896 print user.name
1897</pre>
1898</YueDisplay>
1899
1900```moonscript
1901if hello = os.getenv "hello"
1902 print "You have hello", hello
1903elseif world = os.getenv "world"
1904 print "you have world", world
1905else
1906 print "nothing :("
1907```
1908<YueDisplay>
1909<pre>
1910if hello = os.getenv "hello"
1911 print "You have hello", hello
1912elseif world = os.getenv "world"
1913 print "you have world", world
1914else
1915 print "nothing :("
1916</pre>
1917</YueDisplay>
1918
1919## Line Decorators 2095## Line Decorators
1920 2096
1921For convenience, the for loop and if statement can be applied to single statements at the end of the line: 2097For 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
2580Destructuring assignment is a way to quickly extract values from a table by their name or position in array based tables.
2581
2582Typically 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
2584This is best explained with examples. Here is how you would unpack the first two values from a table:
2585
2586```moonscript
2587thing = {1, 2}
2588
2589{a, b} = thing
2590print a, b
2591```
2592<YueDisplay>
2593<pre>
2594thing = {1, 2}
2595
2596{a, b} = thing
2597print a, b
2598</pre>
2599</YueDisplay>
2600
2601In 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
2604obj = {
2605 hello: "world"
2606 day: "tuesday"
2607 length: 20
2608}
2609
2610{hello: hello, day: the_day} = obj
2611print hello, the_day
2612```
2613<YueDisplay>
2614<pre>
2615obj = {
2616 hello: "world"
2617 day: "tuesday"
2618 length: 20
2619}
2620
2621{hello: hello, day: the_day} = obj
2622print hello, the_day
2623</pre>
2624</YueDisplay>
2625
2626This also works with nested data structures as well:
2627
2628```moonscript
2629obj2 = {
2630 numbers: {1,2,3,4}
2631 properties: {
2632 color: "green"
2633 height: 13.5
2634 }
2635}
2636
2637{numbers: {first, second}} = obj2
2638print first, second, color
2639```
2640<YueDisplay>
2641<pre>
2642obj2 = {
2643 numbers: {1,2,3,4}
2644 properties: {
2645 color: "green"
2646 height: 13.5
2647 }
2648}
2649
2650{numbers: {first, second}} = obj2
2651print first, second, color
2652</pre>
2653</YueDisplay>
2654
2655If 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
2676It’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
2687This 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
2698You 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
2712Destructuring can also show up in places where an assignment implicitly takes place. An example of this is a for loop:
2713
2714```moonscript
2715tuples = {
2716 {"hello", "world"}
2717 {"egg", "head"}
2718}
2719
2720for {left, right} in *tuples
2721 print left, right
2722```
2723<YueDisplay>
2724<pre>
2725tuples = {
2726 {"hello", "world"}
2727 {"egg", "head"}
2728}
2729
2730for {left, right} in *tuples
2731 print left, right
2732</pre>
2733</YueDisplay>
2734
2735We 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
2739It 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. 2756It 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 = {
19build = { 19build = {
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 = {