aboutsummaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2022-11-09 11:30:17 +0800
committerLi Jin <dragon-fly@qq.com>2022-11-09 11:30:17 +0800
commit6db82a69096a48c8b348217b0db6e06b297218ca (patch)
tree58dda2d771e508c417017d649707e8198f481a55 /doc
parentb041d365b88b76418def86d13a8f946dd8a6db73 (diff)
downloadyuescript-6db82a69096a48c8b348217b0db6e06b297218ca.tar.gz
yuescript-6db82a69096a48c8b348217b0db6e06b297218ca.tar.bz2
yuescript-6db82a69096a48c8b348217b0db6e06b297218ca.zip
refactor and update readme and changelog.
Diffstat (limited to 'doc')
-rwxr-xr-xdoc/docs/doc/README.md89
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
674The import statement is a syntax sugar for requiring a module or help extracting items from an imported module. 674The 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
678do 678do
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
683do 684do
@@ -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
690do 691do
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
698do 699do
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
703do 705do
@@ -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
710do 712do
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
850You can do chaining assignment to assign multiple items to hold the same value.
851```moonscript
852a = b = c = d = e = 0
853x = y = z = f!
854```
855<YueDisplay>
856<pre>
857a = b = c = d = e = 0
858x = y = z = f!
859</pre>
860</YueDisplay>
861
846### Explicit Locals 862### Explicit Locals
847```moonscript 863```moonscript
848do 864do
@@ -1114,7 +1130,7 @@ else
1114</pre> 1130</pre>
1115</YueDisplay> 1131</YueDisplay>
1116 1132
1117If assignment with extra return values. 1133If assignment with multiple return values. Only the first value is getting checked, other values are scoped.
1118```moonscript 1134```moonscript
1119if success, result = pcall -> "get result without problems" 1135if 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
1249The syntax support for Lua 5.4 attributes. 1265Syntax 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
1252const a = 123 1268const 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
1305You can use underscores in a number literal to increase readability.
1306
1307```moonscript
1308integer = 1_000_000
1309hex = 0xEF_BB_BF
1310```
1311<YueDisplay>
1312
1313<pre>
1314integer = 1_000_000
1315hex = 0xEF_BB_BF
1316</pre>
1317</YueDisplay>
1318
1287## Function Literals 1319## Function Literals
1288 1320
1289All functions are created using a function expression. A simple function is denoted using the arrow: **->**. 1321All 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
2337And with while loops:
2338
2339```moonscript
2340game\update! while game\isRunning!
2341
2342reader\parse_line! until reader\eof!
2343```
2344<YueDisplay>
2345<pre>
2346game\update! while game\isRunning!
2347
2348reader\parse_line! until reader\eof!
2349</pre>
2350</YueDisplay>
2351
2305## Switch 2352## Switch
2306 2353
2307The 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. 2354The 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
3025Accessing special keys with `[]` in a `with` statement.
3026
3027```moonscript
3028with 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>
3038with 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
2980When used as a statement, do works just like it does in Lua. 3051When used as a statement, do works just like it does in Lua.