diff options
-rw-r--r-- | CHANGELOG.md | 34 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rwxr-xr-x | doc/docs/doc/README.md | 80 |
3 files changed, 109 insertions, 7 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index b60bf8a..1c87ff7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md | |||
@@ -2,7 +2,7 @@ | |||
2 | 2 | ||
3 | The implementation for the original Moonscript language 0.5.0 can be found in the `0.5.0` branch of Yuescript. The Moonscript with fixes and new features is in the main branch of Yuescript. Here are the changelogs for each Yuescript version. | 3 | The implementation for the original Moonscript language 0.5.0 can be found in the `0.5.0` branch of Yuescript. The Moonscript with fixes and new features is in the main branch of Yuescript. Here are the changelogs for each Yuescript version. |
4 | 4 | ||
5 | ## v0.8.2 | 5 | ## v0.8.5 |
6 | 6 | ||
7 | ### Fixed Issues | 7 | ### Fixed Issues |
8 | 8 | ||
@@ -11,6 +11,38 @@ The implementation for the original Moonscript language 0.5.0 can be found in th | |||
11 | 11 | ||
12 | ### Added Features | 12 | ### Added Features |
13 | 13 | ||
14 | * Nil coalescing operator. | ||
15 | ```moonscript | ||
16 | local a, b, c, d | ||
17 | a = b ?? c ?? d | ||
18 | func a ?? {} | ||
19 | |||
20 | a ??= false | ||
21 | ``` | ||
22 | Compiles to: | ||
23 | ```lua | ||
24 | local a, b, c, d | ||
25 | if b ~= nil then | ||
26 | a = b | ||
27 | else | ||
28 | if c ~= nil then | ||
29 | a = c | ||
30 | else | ||
31 | a = d | ||
32 | end | ||
33 | end | ||
34 | func((function() | ||
35 | if a ~= nil then | ||
36 | return a | ||
37 | else | ||
38 | return { } | ||
39 | end | ||
40 | end)()) | ||
41 | if a == nil then | ||
42 | a = false | ||
43 | end | ||
44 | ``` | ||
45 | |||
14 | * New metatable syntax. | 46 | * New metatable syntax. |
15 | ```moonscript | 47 | ```moonscript |
16 | #: mt = tb | 48 | #: mt = tb |
@@ -21,7 +21,7 @@ Yue (月) is the name of moon in Chinese and it's pronounced as [jyɛ]. | |||
21 | * Support most of the features from Moonscript language. Generate Lua codes in the same way like the original compiler. | 21 | * Support most of the features from Moonscript language. Generate Lua codes in the same way like the original compiler. |
22 | * Reserve line numbers from source file in the compiled Lua codes to help debugging. | 22 | * Reserve line numbers from source file in the compiled Lua codes to help debugging. |
23 | * More features like macro, existential operator, pipe operator, Javascript-like export syntax and etc. | 23 | * More features like macro, existential operator, pipe operator, Javascript-like export syntax and etc. |
24 | * See other details in the [changelog](./CHANGELOG.md). Find document from [here](http://yuescript.org). | 24 | * See other details in the [changelog](./CHANGELOG.md). Find document [here](http://yuescript.org). |
25 | 25 | ||
26 | 26 | ||
27 | 27 | ||
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: |