diff options
author | Li Jin <dragon-fly@qq.com> | 2021-11-02 16:12:03 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2021-11-02 16:12:03 +0800 |
commit | a8e5aaf64969792741f3a094fe0070ddb5e3bc7d (patch) | |
tree | 2b006e98a96b52f0bed87cb717ba6012e474b839 /doc/docs | |
parent | 827c3736f357e09168fc108e8e740c6425d37d9b (diff) | |
download | yuescript-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-x | doc/docs/doc/README.md | 80 |
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 = | |||
38 | apple = | 38 | apple = |
39 | size: 15 | 39 | size: 15 |
40 | index#: {color: 0x00ffff} | 40 | index#: {color: 0x00ffff} |
41 | p apple.color, apple.#?, apple.index# | 41 | p apple.color, apple.index# if apple.#? |
42 | 42 | ||
43 | -- js-like export syntax | 43 | -- js-like export syntax |
44 | export yuescript = "月之脚本" | 44 | export yuescript = "月之脚本" |
@@ -70,7 +70,7 @@ inventory = | |||
70 | apple = | 70 | apple = |
71 | size: 15 | 71 | size: 15 |
72 | index#: {color: 0x00ffff} | 72 | index#: {color: 0x00ffff} |
73 | p apple.color, apple.#?, apple.index# | 73 | p apple.color, apple.index# if apple.#? |
74 | 74 | ||
75 | -- js-like export syntax | 75 | -- js-like export syntax |
76 | export yuescript = "月之脚本" | 76 | export yuescript = "月之脚本" |
@@ -160,9 +160,11 @@ Usage: yue [options|files|directories] ... | |||
160 |   Execute raw codes: **yue -e 'print 123'** | 160 |   Execute raw codes: **yue -e 'print 123'** |
161 |   Execute a Yuescript file: **yue -e main.yue** | 161 |   Execute a Yuescript file: **yue -e main.yue** |
162 | 162 | ||
163 | |||
163 | ## Macro | 164 | ## Macro |
164 | 165 | ||
165 | ### Common Usage | 166 | ### Common Usage |
167 | |||
166 | Macro function is used for evaluating a string in the compile time and insert the generated codes into final compilation. | 168 | Macro 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}]" | |||
298 | export macro filter = (items, action)-> "[_ for _ in *#{items} when #{action}]" | 300 | export macro filter = (items, action)-> "[_ for _ in *#{items} when #{action}]" |
299 | export macro foreach = (items, action)-> "for _ in *#{items} | 301 | export 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 | ||
316 | All 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. | 318 | All 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 | ||
321 | tb\func! if tb ~= nil | ||
322 | tb::func! if tb != nil | ||
323 | ``` | ||
324 | <YueDisplay> | ||
325 | <pre> | ||
326 | tb\func! if tb ~= nil | ||
327 | tb::func! if tb != nil | ||
328 | </pre> | ||
329 | </YueDisplay> | ||
317 | 330 | ||
318 | ### Metatable | 331 | ### Metatable |
319 | 332 | ||
@@ -373,7 +386,6 @@ print tb.item | |||
373 | tb = ["value"]#: 123 | 386 | tb = ["value"]#: 123 |
374 | tb.index# = tb.# | 387 | tb.index# = tb.# |
375 | print tb.value | 388 | print tb.value |
376 | |||
377 | tb.# = __index: {item: "hello"} | 389 | tb.# = __index: {item: "hello"} |
378 | print tb.item | 390 | print 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 | |||
477 | The 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 | ||
479 | local a, b, c, d | ||
480 | a = b ?? c ?? d | ||
481 | func a ?? {} | ||
482 | |||
483 | a ??= false | ||
484 | ``` | ||
485 | <YueDisplay> | ||
486 | <pre> | ||
487 | local a, b, c, d | ||
488 | a = b ?? c ?? d | ||
489 | func a ?? {} | ||
490 | |||
491 | a ??= 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 | ||
598 | 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. | 632 | 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. |
599 | 633 | ||
634 | ### Multiline Chaining | ||
635 | |||
636 | You can write multi-line chaining function calls with a same indent. | ||
637 | ```moonscript | ||
638 | Rx.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> | ||
647 | Rx.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 | ||
602 | 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. | 658 | 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. |
@@ -617,6 +673,7 @@ s ..= "world" | |||
617 | arg or= "default value" | 673 | arg or= "default value" |
618 | ``` | 674 | ``` |
619 | <YueDisplay> | 675 | <YueDisplay> |
676 | |||
620 | <pre> | 677 | <pre> |
621 | hello = "world" | 678 | hello = "world" |
622 | a, b, c = 1, 2, 3 | 679 | a, 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> |
653 | do | 711 | do |
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 | ||
2519 | You 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 | ||
2463 | Destructuring can also show up in places where an assignment implicitly takes place. An example of this is a for loop: | 2533 | Destructuring can also show up in places where an assignment implicitly takes place. An example of this is a for loop: |