summaryrefslogtreecommitdiff
path: root/doc/docs
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2021-11-02 16:12:03 +0800
committerLi Jin <dragon-fly@qq.com>2021-11-02 16:12:03 +0800
commita8e5aaf64969792741f3a094fe0070ddb5e3bc7d (patch)
tree2b006e98a96b52f0bed87cb717ba6012e474b839 /doc/docs
parent827c3736f357e09168fc108e8e740c6425d37d9b (diff)
downloadyuescript-0.8.5.tar.gz
yuescript-0.8.5.tar.bz2
yuescript-0.8.5.zip
update docs.v0.8.5
Diffstat (limited to '')
-rwxr-xr-xdoc/docs/doc/README.md80
1 files changed, 75 insertions, 5 deletions
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md
index 138d035..b4ae85f 100755
--- a/doc/docs/doc/README.md
+++ b/doc/docs/doc/README.md
@@ -38,7 +38,7 @@ inventory =
38apple = 38apple =
39 size: 15 39 size: 15
40 index#: {color: 0x00ffff} 40 index#: {color: 0x00ffff}
41p apple.color, apple.#?, apple.index# 41p apple.color, apple.index# if apple.#?
42 42
43-- js-like export syntax 43-- js-like export syntax
44export yuescript = "月之脚本" 44export yuescript = "月之脚本"
@@ -70,7 +70,7 @@ inventory =
70apple = 70apple =
71 size: 15 71 size: 15
72 index#: {color: 0x00ffff} 72 index#: {color: 0x00ffff}
73p apple.color, apple.#?, apple.index# 73p apple.color, apple.index# if apple.#?
74 74
75-- js-like export syntax 75-- js-like export syntax
76export yuescript = "月之脚本" 76export yuescript = "月之脚本"
@@ -160,9 +160,11 @@ Usage: yue [options|files|directories] ...
160&emsp;&emsp;Execute raw codes: **yue -e 'print 123'** 160&emsp;&emsp;Execute raw codes: **yue -e 'print 123'**
161&emsp;&emsp;Execute a Yuescript file: **yue -e main.yue** 161&emsp;&emsp;Execute a Yuescript file: **yue -e main.yue**
162 162
163
163## Macro 164## Macro
164 165
165### Common Usage 166### Common Usage
167
166Macro function is used for evaluating a string in the compile time and insert the generated codes into final compilation. 168Macro function is used for evaluating a string in the compile time and insert the generated codes into final compilation.
167 169
168```moonscript 170```moonscript
@@ -298,7 +300,6 @@ export macro map = (items, action)-> "[#{action} for _ in *#{items}]"
298export macro filter = (items, action)-> "[_ for _ in *#{items} when #{action}]" 300export macro filter = (items, action)-> "[_ for _ in *#{items} when #{action}]"
299export macro foreach = (items, action)-> "for _ in *#{items} 301export macro foreach = (items, action)-> "for _ in *#{items}
300 #{action}" 302 #{action}"
301
302-- file main.yue 303-- file main.yue
303-- import function is not available in browser, try it in a real environment 304-- import function is not available in browser, try it in a real environment
304--[[ 305--[[
@@ -311,9 +312,21 @@ import "utils" as {
311</pre> 312</pre>
312</YueDisplay> 313</YueDisplay>
313 314
315
314## Operator 316## Operator
315 317
316All of Lua’s binary and unary operators are available. Additionally **!=** is as an alias for **~=**. And Yuescipt offers some special operator to write more expressive codes. 318All of Lua's binary and unary operators are available. Additionally **!=** is as an alias for **~=**, and either **\\** or **::** can be used to write a chaining function call like `tb\func!` or `tb::func!`. And Yuescipt offers some other special operators to write more expressive codes.
319
320```moonscript
321tb\func! if tb ~= nil
322tb::func! if tb != nil
323```
324<YueDisplay>
325<pre>
326tb\func! if tb ~= nil
327tb::func! if tb != nil
328</pre>
329</YueDisplay>
317 330
318### Metatable 331### Metatable
319 332
@@ -373,7 +386,6 @@ print tb.item
373tb = ["value"]#: 123 386tb = ["value"]#: 123
374tb.index# = tb.# 387tb.index# = tb.#
375print tb.value 388print tb.value
376
377tb.# = __index: {item: "hello"} 389tb.# = __index: {item: "hello"}
378print tb.item 390print tb.item
379</pre> 391</pre>
@@ -460,6 +472,27 @@ readFile "example.txt"
460</pre> 472</pre>
461</YueDisplay> 473</YueDisplay>
462 474
475### Nil Coalescing
476
477The nil-coalescing operator **??** returns the value of its left-hand operand if it isn't **nil**; otherwise, it evaluates the right-hand operand and returns its result. The **??** operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-nil.
478```moonscript
479local a, b, c, d
480a = b ?? c ?? d
481func a ?? {}
482
483a ??= false
484```
485<YueDisplay>
486<pre>
487local a, b, c, d
488a = b ?? c ?? d
489func a ?? {}
490
491a ??= false
492</pre>
493</YueDisplay>
494
495
463## Module 496## Module
464 497
465### Import 498### Import
@@ -593,10 +626,33 @@ export default ->
593</pre> 626</pre>
594</YueDisplay> 627</YueDisplay>
595 628
629
596## Whitespace 630## Whitespace
597 631
598Yuescript 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. 632Yuescript 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.
599 633
634### Multiline Chaining
635
636You can write multi-line chaining function calls with a same indent.
637```moonscript
638Rx.Observable
639 .fromRange 1, 8
640 \filter (x)-> x % 2 == 0
641 \concat Rx.Observable.of 'who do we appreciate'
642 \map (value)-> value .. '!'
643 \subscribe print
644```
645<YueDisplay>
646<pre>
647Rx.Observable
648 .fromRange 1, 8
649 \filter (x)-> x % 2 == 0
650 \concat Rx.Observable.of 'who do we appreciate'
651 \map (value)-> value .. '!'
652 \subscribe print
653</pre>
654</YueDisplay>
655
600## Assignment 656## Assignment
601 657
602The variable is dynamic typed and is defined as local by default. But you can change the scope of declaration by **local** and **global** statement. 658The variable is dynamic typed and is defined as local by default. But you can change the scope of declaration by **local** and **global** statement.
@@ -617,6 +673,7 @@ s ..= "world"
617arg or= "default value" 673arg or= "default value"
618``` 674```
619<YueDisplay> 675<YueDisplay>
676
620<pre> 677<pre>
621hello = "world" 678hello = "world"
622a, b, c = 1, 2, 3 679a, b, c = 1, 2, 3
@@ -649,6 +706,7 @@ do
649 B = 2 706 B = 2
650``` 707```
651<YueDisplay> 708<YueDisplay>
709
652<pre> 710<pre>
653do 711do
654 local * 712 local *
@@ -2458,6 +2516,18 @@ This is effectively the same as import, but we can rename fields we want to extr
2458</pre> 2516</pre>
2459</YueDisplay> 2517</YueDisplay>
2460 2518
2519You can write default values while doing destructuring like:
2520
2521```moonscript
2522{:name = "nameless", :job = "jobless"} = person
2523```
2524<YueDisplay>
2525<pre>
2526{:name = "nameless", :job = "jobless"} = person
2527</pre>
2528</YueDisplay>
2529
2530
2461### Destructuring In Other Places 2531### Destructuring In Other Places
2462 2532
2463Destructuring can also show up in places where an assignment implicitly takes place. An example of this is a for loop: 2533Destructuring can also show up in places where an assignment implicitly takes place. An example of this is a for loop: