diff options
author | Li Jin <dragon-fly@qq.com> | 2022-11-09 11:30:17 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2022-11-09 11:30:17 +0800 |
commit | 6db82a69096a48c8b348217b0db6e06b297218ca (patch) | |
tree | 58dda2d771e508c417017d649707e8198f481a55 /doc | |
parent | b041d365b88b76418def86d13a8f946dd8a6db73 (diff) | |
download | yuescript-6db82a69096a48c8b348217b0db6e06b297218ca.tar.gz yuescript-6db82a69096a48c8b348217b0db6e06b297218ca.tar.bz2 yuescript-6db82a69096a48c8b348217b0db6e06b297218ca.zip |
refactor and update readme and changelog.
Diffstat (limited to 'doc')
-rwxr-xr-x | doc/docs/doc/README.md | 89 |
1 files changed, 80 insertions, 9 deletions
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md index f23a407..df9002d 100755 --- a/doc/docs/doc/README.md +++ b/doc/docs/doc/README.md | |||
@@ -671,13 +671,14 @@ tb = | |||
671 | 671 | ||
672 | ### Import | 672 | ### Import |
673 | 673 | ||
674 | The import statement is a syntax sugar for requiring a module or help extracting items from an imported module. | 674 | The import statement is a syntax sugar for requiring a module or help extracting items from an imported module. The imported items are const by default. |
675 | 675 | ||
676 | ```moonscript | 676 | ```moonscript |
677 | -- used as table destructure | 677 | -- used as table destructuring |
678 | do | 678 | do |
679 | import C, Ct, Cmt from require "lpeg" | ||
680 | import insert, concat from table | 679 | import insert, concat from table |
680 | -- report error when assigning to insert, concat | ||
681 | import C, Ct, Cmt from require "lpeg" | ||
681 | 682 | ||
682 | -- shortcut for requring a module | 683 | -- shortcut for requring a module |
683 | do | 684 | do |
@@ -686,7 +687,7 @@ do | |||
686 | import "d-a-s-h-e-s" | 687 | import "d-a-s-h-e-s" |
687 | import "module.part" | 688 | import "module.part" |
688 | 689 | ||
689 | -- requring module with aliasing or table destruction | 690 | -- requring module with aliasing or table destructuring |
690 | do | 691 | do |
691 | import "player" as PlayerModule | 692 | import "player" as PlayerModule |
692 | import "lpeg" as :C, :Ct, :Cmt | 693 | import "lpeg" as :C, :Ct, :Cmt |
@@ -694,10 +695,11 @@ do | |||
694 | ``` | 695 | ``` |
695 | <YueDisplay> | 696 | <YueDisplay> |
696 | <pre> | 697 | <pre> |
697 | -- used as table destruction | 698 | -- used as table destructuring |
698 | do | 699 | do |
699 | import C, Ct, Cmt from require "lpeg" | ||
700 | import insert, concat from table | 700 | import insert, concat from table |
701 | -- report error when assigning to insert, concat | ||
702 | import C, Ct, Cmt from require "lpeg" | ||
701 | 703 | ||
702 | -- shortcut for requring a module | 704 | -- shortcut for requring a module |
703 | do | 705 | do |
@@ -706,7 +708,7 @@ do | |||
706 | import "d-a-s-h-e-s" | 708 | import "d-a-s-h-e-s" |
707 | import "module.part" | 709 | import "module.part" |
708 | 710 | ||
709 | -- requring module with aliasing or table destruction | 711 | -- requring module with aliasing or table destructuring |
710 | do | 712 | do |
711 | import "player" as PlayerModule | 713 | import "player" as PlayerModule |
712 | import "lpeg" as :C, :Ct, :Cmt | 714 | import "lpeg" as :C, :Ct, :Cmt |
@@ -843,6 +845,20 @@ arg or= "default value" | |||
843 | </pre> | 845 | </pre> |
844 | </YueDisplay> | 846 | </YueDisplay> |
845 | 847 | ||
848 | ### Chaining Assignment | ||
849 | |||
850 | You can do chaining assignment to assign multiple items to hold the same value. | ||
851 | ```moonscript | ||
852 | a = b = c = d = e = 0 | ||
853 | x = y = z = f! | ||
854 | ``` | ||
855 | <YueDisplay> | ||
856 | <pre> | ||
857 | a = b = c = d = e = 0 | ||
858 | x = y = z = f! | ||
859 | </pre> | ||
860 | </YueDisplay> | ||
861 | |||
846 | ### Explicit Locals | 862 | ### Explicit Locals |
847 | ```moonscript | 863 | ```moonscript |
848 | do | 864 | do |
@@ -1114,7 +1130,7 @@ else | |||
1114 | </pre> | 1130 | </pre> |
1115 | </YueDisplay> | 1131 | </YueDisplay> |
1116 | 1132 | ||
1117 | If assignment with extra return values. | 1133 | If assignment with multiple return values. Only the first value is getting checked, other values are scoped. |
1118 | ```moonscript | 1134 | ```moonscript |
1119 | if success, result = pcall -> "get result without problems" | 1135 | if success, result = pcall -> "get result without problems" |
1120 | print result -- variable result is scoped | 1136 | print result -- variable result is scoped |
@@ -1246,7 +1262,7 @@ catch err | |||
1246 | 1262 | ||
1247 | ## Attributes | 1263 | ## Attributes |
1248 | 1264 | ||
1249 | The syntax support for Lua 5.4 attributes. | 1265 | Syntax support for Lua 5.4 attributes. But you can use still use `const` declaration and get constant check functioning when targeting Lua versions below 5.4. |
1250 | 1266 | ||
1251 | ```moonscript | 1267 | ```moonscript |
1252 | const a = 123 | 1268 | const a = 123 |
@@ -1284,6 +1300,22 @@ print "I am #{math.random! * 100}% sure." | |||
1284 | </pre> | 1300 | </pre> |
1285 | </YueDisplay> | 1301 | </YueDisplay> |
1286 | 1302 | ||
1303 | ### Number Literals | ||
1304 | |||
1305 | You can use underscores in a number literal to increase readability. | ||
1306 | |||
1307 | ```moonscript | ||
1308 | integer = 1_000_000 | ||
1309 | hex = 0xEF_BB_BF | ||
1310 | ``` | ||
1311 | <YueDisplay> | ||
1312 | |||
1313 | <pre> | ||
1314 | integer = 1_000_000 | ||
1315 | hex = 0xEF_BB_BF | ||
1316 | </pre> | ||
1317 | </YueDisplay> | ||
1318 | |||
1287 | ## Function Literals | 1319 | ## Function Literals |
1288 | 1320 | ||
1289 | All functions are created using a function expression. A simple function is denoted using the arrow: **->**. | 1321 | All functions are created using a function expression. A simple function is denoted using the arrow: **->**. |
@@ -2302,6 +2334,21 @@ print "item: ", item for item in *items | |||
2302 | </pre> | 2334 | </pre> |
2303 | </YueDisplay> | 2335 | </YueDisplay> |
2304 | 2336 | ||
2337 | And with while loops: | ||
2338 | |||
2339 | ```moonscript | ||
2340 | game\update! while game\isRunning! | ||
2341 | |||
2342 | reader\parse_line! until reader\eof! | ||
2343 | ``` | ||
2344 | <YueDisplay> | ||
2345 | <pre> | ||
2346 | game\update! while game\isRunning! | ||
2347 | |||
2348 | reader\parse_line! until reader\eof! | ||
2349 | </pre> | ||
2350 | </YueDisplay> | ||
2351 | |||
2305 | ## Switch | 2352 | ## Switch |
2306 | 2353 | ||
2307 | The switch statement is shorthand for writing a series of if statements that check against the same value. Note that the value is only evaluated once. Like if statements, switches can have an else block to handle no matches. Comparison is done with the == operator. | 2354 | The switch statement is shorthand for writing a series of if statements that check against the same value. Note that the value is only evaluated once. Like if statements, switches can have an else block to handle no matches. Comparison is done with the == operator. |
@@ -2975,6 +3022,30 @@ with str = "Hello" | |||
2975 | </pre> | 3022 | </pre> |
2976 | </YueDisplay> | 3023 | </YueDisplay> |
2977 | 3024 | ||
3025 | Accessing special keys with `[]` in a `with` statement. | ||
3026 | |||
3027 | ```moonscript | ||
3028 | with tb | ||
3029 | [1] = 1 | ||
3030 | print [2] | ||
3031 | with [abc] | ||
3032 | [3] = [2]\func! | ||
3033 | ["key-name"] = value | ||
3034 | [] = "abc" -- appending to "tb" | ||
3035 | ``` | ||
3036 | <YueDisplay> | ||
3037 | <pre> | ||
3038 | with tb | ||
3039 | [1] = 1 | ||
3040 | print [2] | ||
3041 | with [abc] | ||
3042 | [3] = [2]\func! | ||
3043 | ["key-name"] = value | ||
3044 | [] = "abc" -- appending to "tb" | ||
3045 | </pre> | ||
3046 | </YueDisplay> | ||
3047 | |||
3048 | |||
2978 | ## Do | 3049 | ## Do |
2979 | 3050 | ||
2980 | When used as a statement, do works just like it does in Lua. | 3051 | When used as a statement, do works just like it does in Lua. |