diff options
Diffstat (limited to 'doc/docs')
| -rwxr-xr-x | doc/docs/doc/README.md | 164 | ||||
| -rwxr-xr-x | doc/docs/zh/doc/README.md | 164 |
2 files changed, 170 insertions, 158 deletions
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md index 814593b..e268dc8 100755 --- a/doc/docs/doc/README.md +++ b/doc/docs/doc/README.md | |||
| @@ -31,16 +31,19 @@ inventory = | |||
| 31 | 31 | ||
| 32 | -- pipe operator | 32 | -- pipe operator |
| 33 | [1, 2, 3] | 33 | [1, 2, 3] |
| 34 | |> map (x)-> x * 2 | 34 | |> map (x) -> x * 2 |
| 35 | |> filter (x)-> x > 4 | 35 | |> filter (x) -> x > 4 |
| 36 | |> reduce 0, (a, b)-> a + b | 36 | |> reduce 0, (a, b) -> a + b |
| 37 | 37 | ||
| 38 | 38 | ||
| 39 | -- metatable manipulation | 39 | -- metatable manipulation |
| 40 | apple = | 40 | apple = |
| 41 | size: 15 | 41 | size: 15 |
| 42 | <index>: {color: 0x00ffff} | 42 | <index>: |
| 43 | p apple.color, apple.<index> if apple.<>? | 43 | color: 0x00ffff |
| 44 | |||
| 45 | with apple | ||
| 46 | p .size, .color, .<index> if .<>? | ||
| 44 | 47 | ||
| 45 | -- js-like export syntax | 48 | -- js-like export syntax |
| 46 | export 🌛 = "月之脚本" | 49 | export 🌛 = "月之脚本" |
| @@ -63,16 +66,19 @@ inventory = | |||
| 63 | 66 | ||
| 64 | -- pipe operator | 67 | -- pipe operator |
| 65 | [1, 2, 3] | 68 | [1, 2, 3] |
| 66 | |> map (x)-> x * 2 | 69 | |> map (x) -> x * 2 |
| 67 | |> filter (x)-> x > 4 | 70 | |> filter (x) -> x > 4 |
| 68 | |> reduce 0, (a, b)-> a + b | 71 | |> reduce 0, (a, b) -> a + b |
| 69 | 72 | ||
| 70 | 73 | ||
| 71 | -- metatable manipulation | 74 | -- metatable manipulation |
| 72 | apple = | 75 | apple = |
| 73 | size: 15 | 76 | size: 15 |
| 74 | <index>: {color: 0x00ffff} | 77 | <index>: |
| 75 | p apple.color, apple.<index> if apple.<>? | 78 | color: 0x00ffff |
| 79 | |||
| 80 | with apple | ||
| 81 | p .size, .color, .<index> if .<>? | ||
| 76 | 82 | ||
| 77 | -- js-like export syntax | 83 | -- js-like export syntax |
| 78 | export 🌛 = "月之脚本" | 84 | export 🌛 = "月之脚本" |
| @@ -210,14 +216,14 @@ area = $PI2 * 5 | |||
| 210 | macro HELLO = -> "'hello world'" | 216 | macro HELLO = -> "'hello world'" |
| 211 | print $HELLO | 217 | print $HELLO |
| 212 | 218 | ||
| 213 | macro config = (debugging)-> | 219 | macro config = (debugging) -> |
| 214 | global debugMode = debugging == "true" | 220 | global debugMode = debugging == "true" |
| 215 | "" | 221 | "" |
| 216 | 222 | ||
| 217 | macro asserts = (cond)-> | 223 | macro asserts = (cond) -> |
| 218 | debugMode and "assert #{cond}" or "" | 224 | debugMode and "assert #{cond}" or "" |
| 219 | 225 | ||
| 220 | macro assert = (cond)-> | 226 | macro assert = (cond) -> |
| 221 | debugMode and "assert #{cond}" or "#{cond}" | 227 | debugMode and "assert #{cond}" or "#{cond}" |
| 222 | 228 | ||
| 223 | $config true | 229 | $config true |
| @@ -227,7 +233,7 @@ $config false | |||
| 227 | value = $assert item | 233 | value = $assert item |
| 228 | 234 | ||
| 229 | -- the passed expressions are treated as strings | 235 | -- the passed expressions are treated as strings |
| 230 | macro and = (...)-> "#{ table.concat {...}, ' and ' }" | 236 | macro and = (...) -> "#{ table.concat {...}, ' and ' }" |
| 231 | if $and f1!, f2!, f3! | 237 | if $and f1!, f2!, f3! |
| 232 | print "OK" | 238 | print "OK" |
| 233 | ``` | 239 | ``` |
| @@ -239,14 +245,14 @@ area = $PI2 * 5 | |||
| 239 | macro HELLO = -> "'hello world'" | 245 | macro HELLO = -> "'hello world'" |
| 240 | print $HELLO | 246 | print $HELLO |
| 241 | 247 | ||
| 242 | macro config = (debugging)-> | 248 | macro config = (debugging) -> |
| 243 | global debugMode = debugging == "true" | 249 | global debugMode = debugging == "true" |
| 244 | "" | 250 | "" |
| 245 | 251 | ||
| 246 | macro asserts = (cond)-> | 252 | macro asserts = (cond) -> |
| 247 | debugMode and "assert #{cond}" or "" | 253 | debugMode and "assert #{cond}" or "" |
| 248 | 254 | ||
| 249 | macro assert = (cond)-> | 255 | macro assert = (cond) -> |
| 250 | debugMode and "assert #{cond}" or "#{cond}" | 256 | debugMode and "assert #{cond}" or "#{cond}" |
| 251 | 257 | ||
| 252 | $config true | 258 | $config true |
| @@ -256,7 +262,7 @@ $config false | |||
| 256 | value = $assert item | 262 | value = $assert item |
| 257 | 263 | ||
| 258 | -- the passed expressions are treated as strings | 264 | -- the passed expressions are treated as strings |
| 259 | macro and = (...)-> "#{ table.concat {...}, ' and ' }" | 265 | macro and = (...) -> "#{ table.concat {...}, ' and ' }" |
| 260 | if $and f1!, f2!, f3! | 266 | if $and f1!, f2!, f3! |
| 261 | print "OK" | 267 | print "OK" |
| 262 | </pre> | 268 | </pre> |
| @@ -266,21 +272,21 @@ if $and f1!, f2!, f3! | |||
| 266 | 272 | ||
| 267 | A macro function can either return a YueScript string or a config table containing Lua codes. | 273 | A macro function can either return a YueScript string or a config table containing Lua codes. |
| 268 | ```moonscript | 274 | ```moonscript |
| 269 | macro yueFunc = (var)-> "local #{var} = ->" | 275 | macro yueFunc = (var) -> "local #{var} = ->" |
| 270 | $yueFunc funcA | 276 | $yueFunc funcA |
| 271 | funcA = -> "assign the Yue defined variable" | 277 | funcA = -> "fail to assign to the Yue defined variable" |
| 272 | 278 | ||
| 273 | -- take care and let YueScript know the | 279 | -- take care and let YueScript know the |
| 274 | -- local variables you declared in Lua code | 280 | -- local variables you declared in Lua code |
| 275 | macro luaFunc = (var)-> { | 281 | macro luaFunc = (var) -> { |
| 276 | code: "local function #{var}() end" | 282 | code: "local function #{var}() end" |
| 277 | type: "lua" | 283 | type: "lua" |
| 278 | locals: {var} | 284 | locals: {var} |
| 279 | } | 285 | } |
| 280 | $luaFunc funcB | 286 | $luaFunc funcB |
| 281 | funcB = -> "assign the Lua defined variable" | 287 | funcB = -> "assign to the Lua defined variable" |
| 282 | 288 | ||
| 283 | macro lua = (code)-> { | 289 | macro lua = (code) -> { |
| 284 | :code | 290 | :code |
| 285 | type: "lua" | 291 | type: "lua" |
| 286 | } | 292 | } |
| @@ -295,21 +301,21 @@ end | |||
| 295 | ``` | 301 | ``` |
| 296 | <YueDisplay> | 302 | <YueDisplay> |
| 297 | <pre> | 303 | <pre> |
| 298 | macro yueFunc = (var)-> "local #{var} = ->" | 304 | macro yueFunc = (var) -> "local #{var} = ->" |
| 299 | $yueFunc funcA | 305 | $yueFunc funcA |
| 300 | funcA = -> "assign the Yue defined variable" | 306 | funcA = -> "fail to assign to the Yue defined variable" |
| 301 | 307 | ||
| 302 | -- take care and let YueScript know the | 308 | -- take care and let YueScript know the |
| 303 | -- local variables you declared in Lua codes | 309 | -- local variables you declared in Lua codes |
| 304 | macro luaFunc = (var)-> { | 310 | macro luaFunc = (var) -> { |
| 305 | code: "local function #{var}() end" | 311 | code: "local function #{var}() end" |
| 306 | type: "lua" | 312 | type: "lua" |
| 307 | locals: {var} | 313 | locals: {var} |
| 308 | } | 314 | } |
| 309 | $luaFunc funcB | 315 | $luaFunc funcB |
| 310 | funcB = -> "assign the Lua defined variable" | 316 | funcB = -> "assign to the Lua defined variable" |
| 311 | 317 | ||
| 312 | macro lua = (code)-> { | 318 | macro lua = (code) -> { |
| 313 | :code | 319 | :code |
| 314 | type: "lua" | 320 | type: "lua" |
| 315 | } | 321 | } |
| @@ -329,9 +335,9 @@ end | |||
| 329 | Macro functions can be exported from a module and get imported in another module. You have to put export macro functions in a single file to be used, and only macro definition, macro importing and macro expansion in place can be put into the macro exporting module. | 335 | Macro functions can be exported from a module and get imported in another module. You have to put export macro functions in a single file to be used, and only macro definition, macro importing and macro expansion in place can be put into the macro exporting module. |
| 330 | ```moonscript | 336 | ```moonscript |
| 331 | -- file: utils.yue | 337 | -- file: utils.yue |
| 332 | export macro map = (items, action)-> "[#{action} for _ in *#{items}]" | 338 | export macro map = (items, action) -> "[#{action} for _ in *#{items}]" |
| 333 | export macro filter = (items, action)-> "[_ for _ in *#{items} when #{action}]" | 339 | export macro filter = (items, action) -> "[_ for _ in *#{items} when #{action}]" |
| 334 | export macro foreach = (items, action)-> "for _ in *#{items} | 340 | export macro foreach = (items, action) -> "for _ in *#{items} |
| 335 | #{action}" | 341 | #{action}" |
| 336 | 342 | ||
| 337 | -- file main.yue | 343 | -- file main.yue |
| @@ -344,9 +350,9 @@ import "utils" as { | |||
| 344 | <YueDisplay> | 350 | <YueDisplay> |
| 345 | <pre> | 351 | <pre> |
| 346 | -- file: utils.yue | 352 | -- file: utils.yue |
| 347 | export macro map = (items, action)-> "[#{action} for _ in *#{items}]" | 353 | export macro map = (items, action) -> "[#{action} for _ in *#{items}]" |
| 348 | export macro filter = (items, action)-> "[_ for _ in *#{items} when #{action}]" | 354 | export macro filter = (items, action) -> "[_ for _ in *#{items} when #{action}]" |
| 349 | export macro foreach = (items, action)-> "for _ in *#{items} | 355 | export macro foreach = (items, action) -> "for _ in *#{items} |
| 350 | #{action}" | 356 | #{action}" |
| 351 | -- file main.yue | 357 | -- file main.yue |
| 352 | -- import function is not available in browser, try it in a real environment | 358 | -- import function is not available in browser, try it in a real environment |
| @@ -457,7 +463,7 @@ print 1 <= a <= 10 | |||
| 457 | Note the evaluation behavior of chained comparisons: | 463 | Note the evaluation behavior of chained comparisons: |
| 458 | 464 | ||
| 459 | ```moonscript | 465 | ```moonscript |
| 460 | v = (x)-> | 466 | v = (x) -> |
| 461 | print x | 467 | print x |
| 462 | x | 468 | x |
| 463 | 469 | ||
| @@ -480,7 +486,7 @@ print v(1) > v(2) <= v(3) | |||
| 480 | ``` | 486 | ``` |
| 481 | <YueDisplay> | 487 | <YueDisplay> |
| 482 | <pre> | 488 | <pre> |
| 483 | v = (x)-> | 489 | v = (x) -> |
| 484 | print x | 490 | print x |
| 485 | x | 491 | x |
| 486 | 492 | ||
| @@ -567,7 +573,7 @@ Create normal table with empty bracekets **<>** or metamethod key which is surro | |||
| 567 | 573 | ||
| 568 | ```moonscript | 574 | ```moonscript |
| 569 | mt = {} | 575 | mt = {} |
| 570 | add = (right)=> <>: mt, value: @value + right.value | 576 | add = (right) => <>: mt, value: @value + right.value |
| 571 | mt.__add = add | 577 | mt.__add = add |
| 572 | 578 | ||
| 573 | a = <>: mt, value: 1 | 579 | a = <>: mt, value: 1 |
| @@ -583,7 +589,7 @@ close _ = <close>: -> print "out of scope" | |||
| 583 | <YueDisplay> | 589 | <YueDisplay> |
| 584 | <pre> | 590 | <pre> |
| 585 | mt = {} | 591 | mt = {} |
| 586 | add = (right)=> <>: mt, value: @value + right.value | 592 | add = (right) => <>: mt, value: @value + right.value |
| 587 | mt.__add = add | 593 | mt.__add = add |
| 588 | 594 | ||
| 589 | a = <>: mt, value: 1 | 595 | a = <>: mt, value: 1 |
| @@ -645,7 +651,7 @@ func?! | |||
| 645 | print abc?["hello world"]?.xyz | 651 | print abc?["hello world"]?.xyz |
| 646 | 652 | ||
| 647 | x = tab?.value | 653 | x = tab?.value |
| 648 | len = utf8?.len or string?.len or (o)-> #o | 654 | len = utf8?.len or string?.len or (o) -> #o |
| 649 | 655 | ||
| 650 | if print and x? | 656 | if print and x? |
| 651 | print x | 657 | print x |
| @@ -660,7 +666,7 @@ func?! | |||
| 660 | print abc?["hello world"]?.xyz | 666 | print abc?["hello world"]?.xyz |
| 661 | 667 | ||
| 662 | x = tab?.value | 668 | x = tab?.value |
| 663 | len = utf8?.len or string?.len or (o)-> #o | 669 | len = utf8?.len or string?.len or (o) -> #o |
| 664 | 670 | ||
| 665 | if print and x? | 671 | if print and x? |
| 666 | print x | 672 | print x |
| @@ -1361,18 +1367,18 @@ You can write multi-line chaining function calls with a same indent. | |||
| 1361 | ```moonscript | 1367 | ```moonscript |
| 1362 | Rx.Observable | 1368 | Rx.Observable |
| 1363 | .fromRange 1, 8 | 1369 | .fromRange 1, 8 |
| 1364 | \filter (x)-> x % 2 == 0 | 1370 | \filter (x) -> x % 2 == 0 |
| 1365 | \concat Rx.Observable.of 'who do we appreciate' | 1371 | \concat Rx.Observable.of 'who do we appreciate' |
| 1366 | \map (value)-> value .. '!' | 1372 | \map (value) -> value .. '!' |
| 1367 | \subscribe print | 1373 | \subscribe print |
| 1368 | ``` | 1374 | ``` |
| 1369 | <YueDisplay> | 1375 | <YueDisplay> |
| 1370 | <pre> | 1376 | <pre> |
| 1371 | Rx.Observable | 1377 | Rx.Observable |
| 1372 | .fromRange 1, 8 | 1378 | .fromRange 1, 8 |
| 1373 | \filter (x)-> x % 2 == 0 | 1379 | \filter (x) -> x % 2 == 0 |
| 1374 | \concat Rx.Observable.of 'who do we appreciate' | 1380 | \concat Rx.Observable.of 'who do we appreciate' |
| 1375 | \map (value)-> value .. '!' | 1381 | \map (value) -> value .. '!' |
| 1376 | \subscribe print | 1382 | \subscribe print |
| 1377 | </pre> | 1383 | </pre> |
| 1378 | </YueDisplay> | 1384 | </YueDisplay> |
| @@ -1586,11 +1592,11 @@ func_b() | |||
| 1586 | Functions with arguments can be created by preceding the arrow with a list of argument names in parentheses: | 1592 | Functions with arguments can be created by preceding the arrow with a list of argument names in parentheses: |
| 1587 | 1593 | ||
| 1588 | ```moonscript | 1594 | ```moonscript |
| 1589 | sum = (x, y)-> print "sum", x + y | 1595 | sum = (x, y) -> print "sum", x + y |
| 1590 | ``` | 1596 | ``` |
| 1591 | <YueDisplay> | 1597 | <YueDisplay> |
| 1592 | <pre> | 1598 | <pre> |
| 1593 | sum = (x, y)-> print "sum", x + y | 1599 | sum = (x, y) -> print "sum", x + y |
| 1594 | </pre> | 1600 | </pre> |
| 1595 | </YueDisplay> | 1601 | </YueDisplay> |
| 1596 | 1602 | ||
| @@ -1627,7 +1633,7 @@ There must not be any space between the opening parenthesis and the function. | |||
| 1627 | Functions will coerce the last statement in their body into a return statement, this is called implicit return: | 1633 | Functions will coerce the last statement in their body into a return statement, this is called implicit return: |
| 1628 | 1634 | ||
| 1629 | ```moonscript | 1635 | ```moonscript |
| 1630 | sum = (x, y)-> x + y | 1636 | sum = (x, y) -> x + y |
| 1631 | print "The sum is ", sum 10, 20 | 1637 | print "The sum is ", sum 10, 20 |
| 1632 | ``` | 1638 | ``` |
| 1633 | <YueDisplay> | 1639 | <YueDisplay> |
| @@ -1640,23 +1646,23 @@ print "The sum is ", sum 10, 20 | |||
| 1640 | And if you need to explicitly return, you can use the return keyword: | 1646 | And if you need to explicitly return, you can use the return keyword: |
| 1641 | 1647 | ||
| 1642 | ```moonscript | 1648 | ```moonscript |
| 1643 | sum = (x, y)-> return x + y | 1649 | sum = (x, y) -> return x + y |
| 1644 | ``` | 1650 | ``` |
| 1645 | <YueDisplay> | 1651 | <YueDisplay> |
| 1646 | <pre> | 1652 | <pre> |
| 1647 | sum = (x, y)-> return x + y | 1653 | sum = (x, y) -> return x + y |
| 1648 | </pre> | 1654 | </pre> |
| 1649 | </YueDisplay> | 1655 | </YueDisplay> |
| 1650 | 1656 | ||
| 1651 | Just like in Lua, functions can return multiple values. The last statement must be a list of values separated by commas: | 1657 | Just like in Lua, functions can return multiple values. The last statement must be a list of values separated by commas: |
| 1652 | 1658 | ||
| 1653 | ```moonscript | 1659 | ```moonscript |
| 1654 | mystery = (x, y)-> x + y, x - y | 1660 | mystery = (x, y) -> x + y, x - y |
| 1655 | a, b = mystery 10, 20 | 1661 | a, b = mystery 10, 20 |
| 1656 | ``` | 1662 | ``` |
| 1657 | <YueDisplay> | 1663 | <YueDisplay> |
| 1658 | <pre> | 1664 | <pre> |
| 1659 | mystery = (x, y)-> x + y, x - y | 1665 | mystery = (x, y) -> x + y, x - y |
| 1660 | a, b = mystery 10, 20 | 1666 | a, b = mystery 10, 20 |
| 1661 | </pre> | 1667 | </pre> |
| 1662 | </YueDisplay> | 1668 | </YueDisplay> |
| @@ -1666,11 +1672,11 @@ a, b = mystery 10, 20 | |||
| 1666 | Because it is an idiom in Lua to send an object as the first argument when calling a method, a special syntax is provided for creating functions which automatically includes a self argument. | 1672 | Because it is an idiom in Lua to send an object as the first argument when calling a method, a special syntax is provided for creating functions which automatically includes a self argument. |
| 1667 | 1673 | ||
| 1668 | ```moonscript | 1674 | ```moonscript |
| 1669 | func = (num)=> @value + num | 1675 | func = (num) => @value + num |
| 1670 | ``` | 1676 | ``` |
| 1671 | <YueDisplay> | 1677 | <YueDisplay> |
| 1672 | <pre> | 1678 | <pre> |
| 1673 | func = (num)=> @value + num | 1679 | func = (num) => @value + num |
| 1674 | </pre> | 1680 | </pre> |
| 1675 | </YueDisplay> | 1681 | </YueDisplay> |
| 1676 | 1682 | ||
| @@ -1679,13 +1685,13 @@ func = (num)=> @value + num | |||
| 1679 | It is possible to provide default values for the arguments of a function. An argument is determined to be empty if its value is nil. Any nil arguments that have a default value will be replace before the body of the function is run. | 1685 | It is possible to provide default values for the arguments of a function. An argument is determined to be empty if its value is nil. Any nil arguments that have a default value will be replace before the body of the function is run. |
| 1680 | 1686 | ||
| 1681 | ```moonscript | 1687 | ```moonscript |
| 1682 | my_function = (name = "something", height = 100)-> | 1688 | my_function = (name = "something", height = 100) -> |
| 1683 | print "Hello I am", name | 1689 | print "Hello I am", name |
| 1684 | print "My height is", height | 1690 | print "My height is", height |
| 1685 | ``` | 1691 | ``` |
| 1686 | <YueDisplay> | 1692 | <YueDisplay> |
| 1687 | <pre> | 1693 | <pre> |
| 1688 | my_function = (name = "something", height = 100)-> | 1694 | my_function = (name = "something", height = 100) -> |
| 1689 | print "Hello I am", name | 1695 | print "Hello I am", name |
| 1690 | print "My height is", height | 1696 | print "My height is", height |
| 1691 | </pre> | 1697 | </pre> |
| @@ -1694,12 +1700,12 @@ my_function = (name = "something", height = 100)-> | |||
| 1694 | An argument default value expression is evaluated in the body of the function in the order of the argument declarations. For this reason default values have access to previously declared arguments. | 1700 | An argument default value expression is evaluated in the body of the function in the order of the argument declarations. For this reason default values have access to previously declared arguments. |
| 1695 | 1701 | ||
| 1696 | ```moonscript | 1702 | ```moonscript |
| 1697 | some_args = (x = 100, y = x + 1000)-> | 1703 | some_args = (x = 100, y = x + 1000) -> |
| 1698 | print x + y | 1704 | print x + y |
| 1699 | ``` | 1705 | ``` |
| 1700 | <YueDisplay> | 1706 | <YueDisplay> |
| 1701 | <pre> | 1707 | <pre> |
| 1702 | some_args = (x = 100, y = x + 1000)-> | 1708 | some_args = (x = 100, y = x + 1000) -> |
| 1703 | print x + y | 1709 | print x + y |
| 1704 | </pre> | 1710 | </pre> |
| 1705 | </YueDisplay> | 1711 | </YueDisplay> |
| @@ -2787,7 +2793,7 @@ class Inventory | |||
| 2787 | new: => | 2793 | new: => |
| 2788 | @items = {} | 2794 | @items = {} |
| 2789 | 2795 | ||
| 2790 | add_item: (name)=> | 2796 | add_item: (name) => |
| 2791 | if @items[name] | 2797 | if @items[name] |
| 2792 | @items[name] += 1 | 2798 | @items[name] += 1 |
| 2793 | else | 2799 | else |
| @@ -2799,7 +2805,7 @@ class Inventory | |||
| 2799 | new: => | 2805 | new: => |
| 2800 | @items = {} | 2806 | @items = {} |
| 2801 | 2807 | ||
| 2802 | add_item: (name)=> | 2808 | add_item: (name) => |
| 2803 | if @items[name] | 2809 | if @items[name] |
| 2804 | @items[name] += 1 | 2810 | @items[name] += 1 |
| 2805 | else | 2811 | else |
| @@ -2839,7 +2845,7 @@ Consider the example below, the clothes property is shared amongst all instances | |||
| 2839 | ```moonscript | 2845 | ```moonscript |
| 2840 | class Person | 2846 | class Person |
| 2841 | clothes: [] | 2847 | clothes: [] |
| 2842 | give_item: (name)=> | 2848 | give_item: (name) => |
| 2843 | table.insert @clothes, name | 2849 | table.insert @clothes, name |
| 2844 | 2850 | ||
| 2845 | a = Person! | 2851 | a = Person! |
| @@ -2855,7 +2861,7 @@ print item for item in *a.clothes | |||
| 2855 | <pre> | 2861 | <pre> |
| 2856 | class Person | 2862 | class Person |
| 2857 | clothes: [] | 2863 | clothes: [] |
| 2858 | give_item: (name)=> | 2864 | give_item: (name) => |
| 2859 | table.insert @clothes, name | 2865 | table.insert @clothes, name |
| 2860 | 2866 | ||
| 2861 | a = Person! | 2867 | a = Person! |
| @@ -2891,7 +2897,7 @@ The extends keyword can be used in a class declaration to inherit the properties | |||
| 2891 | ```moonscript | 2897 | ```moonscript |
| 2892 | class BackPack extends Inventory | 2898 | class BackPack extends Inventory |
| 2893 | size: 10 | 2899 | size: 10 |
| 2894 | add_item: (name)=> | 2900 | add_item: (name) => |
| 2895 | if #@items > size then error "backpack is full" | 2901 | if #@items > size then error "backpack is full" |
| 2896 | super name | 2902 | super name |
| 2897 | ``` | 2903 | ``` |
| @@ -2899,7 +2905,7 @@ class BackPack extends Inventory | |||
| 2899 | <pre> | 2905 | <pre> |
| 2900 | class BackPack extends Inventory | 2906 | class BackPack extends Inventory |
| 2901 | size: 10 | 2907 | size: 10 |
| 2902 | add_item: (name)=> | 2908 | add_item: (name) => |
| 2903 | if #@items > size then error "backpack is full" | 2909 | if #@items > size then error "backpack is full" |
| 2904 | super name | 2910 | super name |
| 2905 | </pre> | 2911 | </pre> |
| @@ -2913,7 +2919,7 @@ Whenever a class inherits from another, it sends a message to the parent class b | |||
| 2913 | 2919 | ||
| 2914 | ```moonscript | 2920 | ```moonscript |
| 2915 | class Shelf | 2921 | class Shelf |
| 2916 | @__inherited: (child)=> | 2922 | @__inherited: (child) => |
| 2917 | print @__name, "was inherited by", child.__name | 2923 | print @__name, "was inherited by", child.__name |
| 2918 | 2924 | ||
| 2919 | -- will print: Shelf was inherited by Cupboard | 2925 | -- will print: Shelf was inherited by Cupboard |
| @@ -2922,7 +2928,7 @@ class Cupboard extends Shelf | |||
| 2922 | <YueDisplay> | 2928 | <YueDisplay> |
| 2923 | <pre> | 2929 | <pre> |
| 2924 | class Shelf | 2930 | class Shelf |
| 2925 | @__inherited: (child)=> | 2931 | @__inherited: (child) => |
| 2926 | print @__name, "was inherited by", child.__name | 2932 | print @__name, "was inherited by", child.__name |
| 2927 | 2933 | ||
| 2928 | -- will print: Shelf was inherited by Cupboard | 2934 | -- will print: Shelf was inherited by Cupboard |
| @@ -3108,7 +3114,7 @@ All variables declared in the body of the class are local to the classes propert | |||
| 3108 | ```moonscript | 3114 | ```moonscript |
| 3109 | class MoreThings | 3115 | class MoreThings |
| 3110 | secret = 123 | 3116 | secret = 123 |
| 3111 | log = (msg)-> print "LOG:", msg | 3117 | log = (msg) -> print "LOG:", msg |
| 3112 | 3118 | ||
| 3113 | some_method: => | 3119 | some_method: => |
| 3114 | log "hello world: " .. secret | 3120 | log "hello world: " .. secret |
| @@ -3117,7 +3123,7 @@ class MoreThings | |||
| 3117 | <pre> | 3123 | <pre> |
| 3118 | class MoreThings | 3124 | class MoreThings |
| 3119 | secret = 123 | 3125 | secret = 123 |
| 3120 | log = (msg)-> print "LOG:", msg | 3126 | log = (msg) -> print "LOG:", msg |
| 3121 | 3127 | ||
| 3122 | some_method: => | 3128 | some_method: => |
| 3123 | log "hello world: " .. secret | 3129 | log "hello world: " .. secret |
| @@ -3144,11 +3150,11 @@ assert @@ == self.__class | |||
| 3144 | For example, a quick way to create a new instance of the same class from an instance method using @@: | 3150 | For example, a quick way to create a new instance of the same class from an instance method using @@: |
| 3145 | 3151 | ||
| 3146 | ```moonscript | 3152 | ```moonscript |
| 3147 | some_instance_method = (...)=> @@ ... | 3153 | some_instance_method = (...) => @@ ... |
| 3148 | ``` | 3154 | ``` |
| 3149 | <YueDisplay> | 3155 | <YueDisplay> |
| 3150 | <pre> | 3156 | <pre> |
| 3151 | some_instance_method = (...)=> @@ ... | 3157 | some_instance_method = (...) => @@ ... |
| 3152 | </pre> | 3158 | </pre> |
| 3153 | </YueDisplay> | 3159 | </YueDisplay> |
| 3154 | 3160 | ||
| @@ -3188,13 +3194,13 @@ class Something | |||
| 3188 | You can also use this syntax for a common function to initialize a object's fields. | 3194 | You can also use this syntax for a common function to initialize a object's fields. |
| 3189 | 3195 | ||
| 3190 | ```moonscript | 3196 | ```moonscript |
| 3191 | new = (@fieldA, @fieldB)=> @ | 3197 | new = (@fieldA, @fieldB) => @ |
| 3192 | obj = new {}, 123, "abc" | 3198 | obj = new {}, 123, "abc" |
| 3193 | print obj | 3199 | print obj |
| 3194 | ``` | 3200 | ``` |
| 3195 | <YueDisplay> | 3201 | <YueDisplay> |
| 3196 | <pre> | 3202 | <pre> |
| 3197 | new = (@fieldA, @fieldB)=> @ | 3203 | new = (@fieldA, @fieldB) => @ |
| 3198 | obj = new {}, 123, "abc" | 3204 | obj = new {}, 123, "abc" |
| 3199 | print obj | 3205 | print obj |
| 3200 | </pre> | 3206 | </pre> |
| @@ -3331,7 +3337,7 @@ file = with File "favorite_foods.txt" | |||
| 3331 | Or… | 3337 | Or… |
| 3332 | 3338 | ||
| 3333 | ```moonscript | 3339 | ```moonscript |
| 3334 | create_person = (name, relatives)-> | 3340 | create_person = (name, relatives) -> |
| 3335 | with Person! | 3341 | with Person! |
| 3336 | .name = name | 3342 | .name = name |
| 3337 | \add_relative relative for relative in *relatives | 3343 | \add_relative relative for relative in *relatives |
| @@ -3340,7 +3346,7 @@ me = create_person "Leaf", [dad, mother, sister] | |||
| 3340 | ``` | 3346 | ``` |
| 3341 | <YueDisplay> | 3347 | <YueDisplay> |
| 3342 | <pre> | 3348 | <pre> |
| 3343 | create_person = (name, relatives)-> | 3349 | create_person = (name, relatives) -> |
| 3344 | with Person! | 3350 | with Person! |
| 3345 | .name = name | 3351 | .name = name |
| 3346 | \add_relative relative for relative in *relatives | 3352 | \add_relative relative for relative in *relatives |
| @@ -3465,7 +3471,7 @@ my_object = { | |||
| 3465 | write: => print "the value:", @value | 3471 | write: => print "the value:", @value |
| 3466 | } | 3472 | } |
| 3467 | 3473 | ||
| 3468 | run_callback = (func)-> | 3474 | run_callback = (func) -> |
| 3469 | print "running callback..." | 3475 | print "running callback..." |
| 3470 | func! | 3476 | func! |
| 3471 | 3477 | ||
| @@ -3484,7 +3490,7 @@ my_object = { | |||
| 3484 | write: => print "the value:", @value | 3490 | write: => print "the value:", @value |
| 3485 | } | 3491 | } |
| 3486 | 3492 | ||
| 3487 | run_callback = (func)-> | 3493 | run_callback = (func) -> |
| 3488 | print "running callback..." | 3494 | print "running callback..." |
| 3489 | func! | 3495 | func! |
| 3490 | 3496 | ||
| @@ -3544,7 +3550,7 @@ The using keyword lets us do that. using nil makes sure that no closed variables | |||
| 3544 | ```moonscript | 3550 | ```moonscript |
| 3545 | i = 100 | 3551 | i = 100 |
| 3546 | 3552 | ||
| 3547 | my_func = (using nil)-> | 3553 | my_func = (using nil) -> |
| 3548 | i = "hello" -- a new local variable is created here | 3554 | i = "hello" -- a new local variable is created here |
| 3549 | 3555 | ||
| 3550 | my_func! | 3556 | my_func! |
| @@ -3554,7 +3560,7 @@ print i -- prints 100, i is unaffected | |||
| 3554 | <pre> | 3560 | <pre> |
| 3555 | i = 100 | 3561 | i = 100 |
| 3556 | 3562 | ||
| 3557 | my_func = (using nil)-> | 3563 | my_func = (using nil) -> |
| 3558 | i = "hello" -- a new local variable is created here | 3564 | i = "hello" -- a new local variable is created here |
| 3559 | 3565 | ||
| 3560 | my_func! | 3566 | my_func! |
| @@ -3568,7 +3574,7 @@ Multiple names can be separated by commas. Closure values can still be accessed, | |||
| 3568 | tmp = 1213 | 3574 | tmp = 1213 |
| 3569 | i, k = 100, 50 | 3575 | i, k = 100, 50 |
| 3570 | 3576 | ||
| 3571 | my_func = (add using k, i)-> | 3577 | my_func = (add using k, i) -> |
| 3572 | tmp = tmp + add -- a new local tmp is created | 3578 | tmp = tmp + add -- a new local tmp is created |
| 3573 | i += tmp | 3579 | i += tmp |
| 3574 | k += tmp | 3580 | k += tmp |
| @@ -3581,7 +3587,7 @@ print i, k -- these have been updated | |||
| 3581 | tmp = 1213 | 3587 | tmp = 1213 |
| 3582 | i, k = 100, 50 | 3588 | i, k = 100, 50 |
| 3583 | 3589 | ||
| 3584 | my_func = (add using k, i)-> | 3590 | my_func = (add using k, i) -> |
| 3585 | tmp = tmp + add -- a new local tmp is created | 3591 | tmp = tmp + add -- a new local tmp is created |
| 3586 | i += tmp | 3592 | i += tmp |
| 3587 | k += tmp | 3593 | k += tmp |
diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md index 355e468..f34fe58 100755 --- a/doc/docs/zh/doc/README.md +++ b/doc/docs/zh/doc/README.md | |||
| @@ -31,16 +31,19 @@ inventory = | |||
| 31 | 31 | ||
| 32 | -- 管道操作符 | 32 | -- 管道操作符 |
| 33 | [1, 2, 3] | 33 | [1, 2, 3] |
| 34 | |> map (x)-> x * 2 | 34 | |> map (x) -> x * 2 |
| 35 | |> filter (x)-> x > 4 | 35 | |> filter (x) -> x > 4 |
| 36 | |> reduce 0, (a, b)-> a + b | 36 | |> reduce 0, (a, b) -> a + b |
| 37 | 37 | ||
| 38 | 38 | ||
| 39 | -- 元表操作 | 39 | -- 元表操作 |
| 40 | apple = | 40 | apple = |
| 41 | size: 15 | 41 | size: 15 |
| 42 | <index>: {color: 0x00ffff} | 42 | <index>: |
| 43 | p apple.color, apple.<index> if apple.<>? | 43 | color: 0x00ffff |
| 44 | |||
| 45 | with apple | ||
| 46 | p .size, .color, .<index> if .<>? | ||
| 44 | 47 | ||
| 45 | -- 类似js的导出语法 | 48 | -- 类似js的导出语法 |
| 46 | export 🌛 = "月之脚本" | 49 | export 🌛 = "月之脚本" |
| @@ -63,16 +66,19 @@ inventory = | |||
| 63 | 66 | ||
| 64 | -- 管道操作符 | 67 | -- 管道操作符 |
| 65 | [1, 2, 3] | 68 | [1, 2, 3] |
| 66 | |> map (x)-> x * 2 | 69 | |> map (x) -> x * 2 |
| 67 | |> filter (x)-> x > 4 | 70 | |> filter (x) -> x > 4 |
| 68 | |> reduce 0, (a, b)-> a + b | 71 | |> reduce 0, (a, b) -> a + b |
| 69 | 72 | ||
| 70 | 73 | ||
| 71 | -- 元表操作 | 74 | -- 元表操作 |
| 72 | apple = | 75 | apple = |
| 73 | size: 15 | 76 | size: 15 |
| 74 | <index>: {color: 0x00ffff} | 77 | <index>: |
| 75 | p apple.color, apple.<index> if apple.<>? | 78 | color: 0x00ffff |
| 79 | |||
| 80 | with apple | ||
| 81 | p .size, .color, .<index> if .<>? | ||
| 76 | 82 | ||
| 77 | -- 类似js的导出语法 | 83 | -- 类似js的导出语法 |
| 78 | export 🌛 = "月之脚本" | 84 | export 🌛 = "月之脚本" |
| @@ -209,14 +215,14 @@ area = $PI2 * 5 | |||
| 209 | macro HELLO = -> "'你好 世界'" | 215 | macro HELLO = -> "'你好 世界'" |
| 210 | print $HELLO | 216 | print $HELLO |
| 211 | 217 | ||
| 212 | macro config = (debugging)-> | 218 | macro config = (debugging) -> |
| 213 | global debugMode = debugging == "true" | 219 | global debugMode = debugging == "true" |
| 214 | "" | 220 | "" |
| 215 | 221 | ||
| 216 | macro asserts = (cond)-> | 222 | macro asserts = (cond) -> |
| 217 | debugMode and "assert #{cond}" or "" | 223 | debugMode and "assert #{cond}" or "" |
| 218 | 224 | ||
| 219 | macro assert = (cond)-> | 225 | macro assert = (cond) -> |
| 220 | debugMode and "assert #{cond}" or "#{cond}" | 226 | debugMode and "assert #{cond}" or "#{cond}" |
| 221 | 227 | ||
| 222 | $config true | 228 | $config true |
| @@ -226,7 +232,7 @@ $config false | |||
| 226 | value = $assert item | 232 | value = $assert item |
| 227 | 233 | ||
| 228 | -- 宏函数参数传递的表达式会被转换为字符串 | 234 | -- 宏函数参数传递的表达式会被转换为字符串 |
| 229 | macro and = (...)-> "#{ table.concat {...}, ' and ' }" | 235 | macro and = (...) -> "#{ table.concat {...}, ' and ' }" |
| 230 | if $and f1!, f2!, f3! | 236 | if $and f1!, f2!, f3! |
| 231 | print "OK" | 237 | print "OK" |
| 232 | ``` | 238 | ``` |
| @@ -238,14 +244,14 @@ area = $PI2 * 5 | |||
| 238 | macro HELLO = -> "'你好 世界'" | 244 | macro HELLO = -> "'你好 世界'" |
| 239 | print $HELLO | 245 | print $HELLO |
| 240 | 246 | ||
| 241 | macro config = (debugging)-> | 247 | macro config = (debugging) -> |
| 242 | global debugMode = debugging == "true" | 248 | global debugMode = debugging == "true" |
| 243 | "" | 249 | "" |
| 244 | 250 | ||
| 245 | macro asserts = (cond)-> | 251 | macro asserts = (cond) -> |
| 246 | debugMode and "assert #{cond}" or "" | 252 | debugMode and "assert #{cond}" or "" |
| 247 | 253 | ||
| 248 | macro assert = (cond)-> | 254 | macro assert = (cond) -> |
| 249 | debugMode and "assert #{cond}" or "#{cond}" | 255 | debugMode and "assert #{cond}" or "#{cond}" |
| 250 | 256 | ||
| 251 | $config true | 257 | $config true |
| @@ -255,7 +261,7 @@ $config false | |||
| 255 | value = $assert item | 261 | value = $assert item |
| 256 | 262 | ||
| 257 | -- 宏函数参数传递的表达式会被转换为字符串 | 263 | -- 宏函数参数传递的表达式会被转换为字符串 |
| 258 | macro and = (...)-> "#{ table.concat {...}, ' and ' }" | 264 | macro and = (...) -> "#{ table.concat {...}, ' and ' }" |
| 259 | if $and f1!, f2!, f3! | 265 | if $and f1!, f2!, f3! |
| 260 | print "OK" | 266 | print "OK" |
| 261 | </pre> | 267 | </pre> |
| @@ -265,20 +271,20 @@ if $and f1!, f2!, f3! | |||
| 265 | 271 | ||
| 266 | 宏函数可以返回一个包含月之脚本代码的字符串,或是一个包含Lua代码字符串的配置表。 | 272 | 宏函数可以返回一个包含月之脚本代码的字符串,或是一个包含Lua代码字符串的配置表。 |
| 267 | ```moonscript | 273 | ```moonscript |
| 268 | macro yueFunc = (var)-> "local #{var} = ->" | 274 | macro yueFunc = (var) -> "local #{var} = ->" |
| 269 | $yueFunc funcA | 275 | $yueFunc funcA |
| 270 | funcA = -> "访问月之脚本定义的变量" | 276 | funcA = -> "无法访问宏生之脚本里定义的变量" |
| 271 | 277 | ||
| 272 | -- 让月之脚本知道你在Lua代码中声明的局部变量 | 278 | -- 让月之脚本知道你在Lua代码中声明的局部变量 |
| 273 | macro luaFunc = (var)-> { | 279 | macro luaFunc = (var) -> { |
| 274 | code: "local function #{var}() end" | 280 | code: "local function #{var}() end" |
| 275 | type: "lua" | 281 | type: "lua" |
| 276 | locals: {var} | 282 | locals: {var} |
| 277 | } | 283 | } |
| 278 | $luaFunc funcB | 284 | $luaFunc funcB |
| 279 | funcB = -> "访问Lua代码里定义的变量" | 285 | funcB = -> "访问宏生成Lua代码里定义的变量" |
| 280 | 286 | ||
| 281 | macro lua = (code)-> { | 287 | macro lua = (code) -> { |
| 282 | :code | 288 | :code |
| 283 | type: "lua" | 289 | type: "lua" |
| 284 | } | 290 | } |
| @@ -293,20 +299,20 @@ end | |||
| 293 | ``` | 299 | ``` |
| 294 | <YueDisplay> | 300 | <YueDisplay> |
| 295 | <pre> | 301 | <pre> |
| 296 | macro yueFunc = (var)-> "local #{var} = ->" | 302 | macro yueFunc = (var) -> "local #{var} = ->" |
| 297 | $yueFunc funcA | 303 | $yueFunc funcA |
| 298 | funcA = -> "访问月之脚本定义的变量" | 304 | funcA = -> "无法访问宏生之脚本里定义的变量" |
| 299 | 305 | ||
| 300 | -- 让月之脚本知道你在Lua代码中声明的局部变量 | 306 | -- 让月之脚本知道你在Lua代码中声明的局部变量 |
| 301 | macro luaFunc = (var)-> { | 307 | macro luaFunc = (var) -> { |
| 302 | code: "local function #{var}() end" | 308 | code: "local function #{var}() end" |
| 303 | type: "lua" | 309 | type: "lua" |
| 304 | locals: {var} | 310 | locals: {var} |
| 305 | } | 311 | } |
| 306 | $luaFunc funcB | 312 | $luaFunc funcB |
| 307 | funcB = -> "访问Lua代码里定义的变量" | 313 | funcB = -> "访问宏生成Lua代码里定义的变量" |
| 308 | 314 | ||
| 309 | macro lua = (code)-> { | 315 | macro lua = (code) -> { |
| 310 | :code | 316 | :code |
| 311 | type: "lua" | 317 | type: "lua" |
| 312 | } | 318 | } |
| @@ -326,9 +332,9 @@ end | |||
| 326 | 宏函数可以从一个模块中导出,并在另一个模块中导入。您必须将导出的宏函数放在一个单独的文件中使用,而且只有宏定义、宏导入和宏展开可以放入这个宏导出模块中。 | 332 | 宏函数可以从一个模块中导出,并在另一个模块中导入。您必须将导出的宏函数放在一个单独的文件中使用,而且只有宏定义、宏导入和宏展开可以放入这个宏导出模块中。 |
| 327 | ```moonscript | 333 | ```moonscript |
| 328 | -- 文件: utils.yue | 334 | -- 文件: utils.yue |
| 329 | export macro map = (items, action)-> "[#{action} for _ in *#{items}]" | 335 | export macro map = (items, action) -> "[#{action} for _ in *#{items}]" |
| 330 | export macro filter = (items, action)-> "[_ for _ in *#{items} when #{action}]" | 336 | export macro filter = (items, action) -> "[_ for _ in *#{items} when #{action}]" |
| 331 | export macro foreach = (items, action)-> "for _ in *#{items} | 337 | export macro foreach = (items, action) -> "for _ in *#{items} |
| 332 | #{action}" | 338 | #{action}" |
| 333 | 339 | ||
| 334 | -- 文件 main.yue | 340 | -- 文件 main.yue |
| @@ -341,9 +347,9 @@ import "utils" as { | |||
| 341 | <YueDisplay> | 347 | <YueDisplay> |
| 342 | <pre> | 348 | <pre> |
| 343 | -- 文件: utils.yue | 349 | -- 文件: utils.yue |
| 344 | export macro map = (items, action)-> "[#{action} for _ in *#{items}]" | 350 | export macro map = (items, action) -> "[#{action} for _ in *#{items}]" |
| 345 | export macro filter = (items, action)-> "[_ for _ in *#{items} when #{action}]" | 351 | export macro filter = (items, action) -> "[_ for _ in *#{items} when #{action}]" |
| 346 | export macro foreach = (items, action)-> "for _ in *#{items} | 352 | export macro foreach = (items, action) -> "for _ in *#{items} |
| 347 | #{action}" | 353 | #{action}" |
| 348 | -- 文件 main.yue | 354 | -- 文件 main.yue |
| 349 | -- 在浏览器中不支持import函数,请在真实环境中尝试 | 355 | -- 在浏览器中不支持import函数,请在真实环境中尝试 |
| @@ -453,7 +459,7 @@ print 1 <= a <= 10 | |||
| 453 | 可以注意一下链式比较表达式的求值行为: | 459 | 可以注意一下链式比较表达式的求值行为: |
| 454 | 460 | ||
| 455 | ```moonscript | 461 | ```moonscript |
| 456 | v = (x)-> | 462 | v = (x) -> |
| 457 | print x | 463 | print x |
| 458 | x | 464 | x |
| 459 | 465 | ||
| @@ -476,7 +482,7 @@ print v(1) > v(2) <= v(3) | |||
| 476 | ``` | 482 | ``` |
| 477 | <YueDisplay> | 483 | <YueDisplay> |
| 478 | <pre> | 484 | <pre> |
| 479 | v = (x)-> | 485 | v = (x) -> |
| 480 | print x | 486 | print x |
| 481 | x | 487 | x |
| 482 | 488 | ||
| @@ -564,7 +570,7 @@ merge = {...a, ...b} | |||
| 564 | 570 | ||
| 565 | ```moonscript | 571 | ```moonscript |
| 566 | mt = {} | 572 | mt = {} |
| 567 | add = (right)=> <>: mt, value: @value + right.value | 573 | add = (right) => <>: mt, value: @value + right.value |
| 568 | mt.__add = add | 574 | mt.__add = add |
| 569 | 575 | ||
| 570 | a = <>: mt, value: 1 | 576 | a = <>: mt, value: 1 |
| @@ -580,7 +586,7 @@ close _ = <close>: -> print "超出范围" | |||
| 580 | <YueDisplay> | 586 | <YueDisplay> |
| 581 | <pre> | 587 | <pre> |
| 582 | mt = {} | 588 | mt = {} |
| 583 | add = (right)=> <>: mt, value: @value + right.value | 589 | add = (right) => <>: mt, value: @value + right.value |
| 584 | mt.__add = add | 590 | mt.__add = add |
| 585 | 591 | ||
| 586 | a = <>: mt, value: 1 | 592 | a = <>: mt, value: 1 |
| @@ -642,7 +648,7 @@ func?! | |||
| 642 | print abc?["你好 世界"]?.xyz | 648 | print abc?["你好 世界"]?.xyz |
| 643 | 649 | ||
| 644 | x = tab?.value | 650 | x = tab?.value |
| 645 | len = utf8?.len or string?.len or (o)-> #o | 651 | len = utf8?.len or string?.len or (o) -> #o |
| 646 | 652 | ||
| 647 | if print and x? | 653 | if print and x? |
| 648 | print x | 654 | print x |
| @@ -657,7 +663,7 @@ func?! | |||
| 657 | print abc?["你好 世界"]?.xyz | 663 | print abc?["你好 世界"]?.xyz |
| 658 | 664 | ||
| 659 | x = tab?.value | 665 | x = tab?.value |
| 660 | len = utf8?.len or string?.len or (o)-> #o | 666 | len = utf8?.len or string?.len or (o) -> #o |
| 661 | 667 | ||
| 662 | if print and x? | 668 | if print and x? |
| 663 | print x | 669 | print x |
| @@ -1358,18 +1364,18 @@ print ok, count, first | |||
| 1358 | ```moonscript | 1364 | ```moonscript |
| 1359 | Rx.Observable | 1365 | Rx.Observable |
| 1360 | .fromRange 1, 8 | 1366 | .fromRange 1, 8 |
| 1361 | \filter (x)-> x % 2 == 0 | 1367 | \filter (x) -> x % 2 == 0 |
| 1362 | \concat Rx.Observable.of 'who do we appreciate' | 1368 | \concat Rx.Observable.of 'who do we appreciate' |
| 1363 | \map (value)-> value .. '!' | 1369 | \map (value) -> value .. '!' |
| 1364 | \subscribe print | 1370 | \subscribe print |
| 1365 | ``` | 1371 | ``` |
| 1366 | <YueDisplay> | 1372 | <YueDisplay> |
| 1367 | <pre> | 1373 | <pre> |
| 1368 | Rx.Observable | 1374 | Rx.Observable |
| 1369 | .fromRange 1, 8 | 1375 | .fromRange 1, 8 |
| 1370 | \filter (x)-> x % 2 == 0 | 1376 | \filter (x) -> x % 2 == 0 |
| 1371 | \concat Rx.Observable.of 'who do we appreciate' | 1377 | \concat Rx.Observable.of 'who do we appreciate' |
| 1372 | \map (value)-> value .. '!' | 1378 | \map (value) -> value .. '!' |
| 1373 | \subscribe print | 1379 | \subscribe print |
| 1374 | </pre> | 1380 | </pre> |
| 1375 | </YueDisplay> | 1381 | </YueDisplay> |
| @@ -1583,11 +1589,11 @@ func_b() | |||
| 1583 | 带有参数的函数可以通过在箭头前加上括号中的参数名列表来进行创建: | 1589 | 带有参数的函数可以通过在箭头前加上括号中的参数名列表来进行创建: |
| 1584 | 1590 | ||
| 1585 | ```moonscript | 1591 | ```moonscript |
| 1586 | sum = (x, y)-> print "数字的和", x + y | 1592 | sum = (x, y) -> print "数字的和", x + y |
| 1587 | ``` | 1593 | ``` |
| 1588 | <YueDisplay> | 1594 | <YueDisplay> |
| 1589 | <pre> | 1595 | <pre> |
| 1590 | sum = (x, y)-> print "数字的和", x + y | 1596 | sum = (x, y) -> print "数字的和", x + y |
| 1591 | </pre> | 1597 | </pre> |
| 1592 | </YueDisplay> | 1598 | </YueDisplay> |
| 1593 | 1599 | ||
| @@ -1624,7 +1630,7 @@ print "x:", sum(10, 20), "y:", sum(30, 40) | |||
| 1624 | 函数会将函数体中的最后一个语句强制转换为返回语句,这被称作隐式返回: | 1630 | 函数会将函数体中的最后一个语句强制转换为返回语句,这被称作隐式返回: |
| 1625 | 1631 | ||
| 1626 | ```moonscript | 1632 | ```moonscript |
| 1627 | sum = (x, y)-> x + y | 1633 | sum = (x, y) -> x + y |
| 1628 | print "数字的和是", sum 10, 20 | 1634 | print "数字的和是", sum 10, 20 |
| 1629 | ``` | 1635 | ``` |
| 1630 | <YueDisplay> | 1636 | <YueDisplay> |
| @@ -1637,23 +1643,23 @@ print "数字的和是", sum 10, 20 | |||
| 1637 | 如果您需要做显式返回,可以使用return关键字: | 1643 | 如果您需要做显式返回,可以使用return关键字: |
| 1638 | 1644 | ||
| 1639 | ```moonscript | 1645 | ```moonscript |
| 1640 | sum = (x, y)-> return x + y | 1646 | sum = (x, y) -> return x + y |
| 1641 | ``` | 1647 | ``` |
| 1642 | <YueDisplay> | 1648 | <YueDisplay> |
| 1643 | <pre> | 1649 | <pre> |
| 1644 | sum = (x, y)-> return x + y | 1650 | sum = (x, y) -> return x + y |
| 1645 | </pre> | 1651 | </pre> |
| 1646 | </YueDisplay> | 1652 | </YueDisplay> |
| 1647 | 1653 | ||
| 1648 | 就像在Lua中一样,函数可以返回多个值。最后一个语句必须是由逗号分隔的值列表: | 1654 | 就像在Lua中一样,函数可以返回多个值。最后一个语句必须是由逗号分隔的值列表: |
| 1649 | 1655 | ||
| 1650 | ```moonscript | 1656 | ```moonscript |
| 1651 | mystery = (x, y)-> x + y, x - y | 1657 | mystery = (x, y) -> x + y, x - y |
| 1652 | a, b = mystery 10, 20 | 1658 | a, b = mystery 10, 20 |
| 1653 | ``` | 1659 | ``` |
| 1654 | <YueDisplay> | 1660 | <YueDisplay> |
| 1655 | <pre> | 1661 | <pre> |
| 1656 | mystery = (x, y)-> x + y, x - y | 1662 | mystery = (x, y) -> x + y, x - y |
| 1657 | a, b = mystery 10, 20 | 1663 | a, b = mystery 10, 20 |
| 1658 | </pre> | 1664 | </pre> |
| 1659 | </YueDisplay> | 1665 | </YueDisplay> |
| @@ -1663,11 +1669,11 @@ a, b = mystery 10, 20 | |||
| 1663 | 因为在Lua中调用方法时,经常习惯将对象作为第一个参数传入,所以月之脚本提供了一种特殊的语法来创建自动包含self参数的函数。 | 1669 | 因为在Lua中调用方法时,经常习惯将对象作为第一个参数传入,所以月之脚本提供了一种特殊的语法来创建自动包含self参数的函数。 |
| 1664 | 1670 | ||
| 1665 | ```moonscript | 1671 | ```moonscript |
| 1666 | func = (num)=> @value + num | 1672 | func = (num) => @value + num |
| 1667 | ``` | 1673 | ``` |
| 1668 | <YueDisplay> | 1674 | <YueDisplay> |
| 1669 | <pre> | 1675 | <pre> |
| 1670 | func = (num)=> @value + num | 1676 | func = (num) => @value + num |
| 1671 | </pre> | 1677 | </pre> |
| 1672 | </YueDisplay> | 1678 | </YueDisplay> |
| 1673 | 1679 | ||
| @@ -1676,13 +1682,13 @@ func = (num)=> @value + num | |||
| 1676 | 可以为函数的参数提供默认值。如果参数的值为nil,则确定该参数为空。任何具有默认值的nil参数在函数体运行之前都会被替换。 | 1682 | 可以为函数的参数提供默认值。如果参数的值为nil,则确定该参数为空。任何具有默认值的nil参数在函数体运行之前都会被替换。 |
| 1677 | 1683 | ||
| 1678 | ```moonscript | 1684 | ```moonscript |
| 1679 | my_function = (name = "某物", height = 100)-> | 1685 | my_function = (name = "某物", height = 100) -> |
| 1680 | print "你好,我是", name | 1686 | print "你好,我是", name |
| 1681 | print "我的高度是", height | 1687 | print "我的高度是", height |
| 1682 | ``` | 1688 | ``` |
| 1683 | <YueDisplay> | 1689 | <YueDisplay> |
| 1684 | <pre> | 1690 | <pre> |
| 1685 | my_function = (name = "某物", height = 100)-> | 1691 | my_function = (name = "某物", height = 100) -> |
| 1686 | print "你好,我是", name | 1692 | print "你好,我是", name |
| 1687 | print "我的高度是", height | 1693 | print "我的高度是", height |
| 1688 | </pre> | 1694 | </pre> |
| @@ -1691,12 +1697,12 @@ my_function = (name = "某物", height = 100)-> | |||
| 1691 | 函数参数的默认值表达式在函数体中会按参数声明的顺序进行计算。因此,在默认值的表达式中可以访问先前声明的参数。 | 1697 | 函数参数的默认值表达式在函数体中会按参数声明的顺序进行计算。因此,在默认值的表达式中可以访问先前声明的参数。 |
| 1692 | 1698 | ||
| 1693 | ```moonscript | 1699 | ```moonscript |
| 1694 | some_args = (x = 100, y = x + 1000)-> | 1700 | some_args = (x = 100, y = x + 1000) -> |
| 1695 | print x + y | 1701 | print x + y |
| 1696 | ``` | 1702 | ``` |
| 1697 | <YueDisplay> | 1703 | <YueDisplay> |
| 1698 | <pre> | 1704 | <pre> |
| 1699 | some_args = (x = 100, y = x + 1000)-> | 1705 | some_args = (x = 100, y = x + 1000) -> |
| 1700 | print x + y | 1706 | print x + y |
| 1701 | </pre> | 1707 | </pre> |
| 1702 | </YueDisplay> | 1708 | </YueDisplay> |
| @@ -2747,7 +2753,7 @@ class Inventory | |||
| 2747 | new: => | 2753 | new: => |
| 2748 | @items = {} | 2754 | @items = {} |
| 2749 | 2755 | ||
| 2750 | add_item: (name)=> | 2756 | add_item: (name) => |
| 2751 | if @items[name] | 2757 | if @items[name] |
| 2752 | @items[name] += 1 | 2758 | @items[name] += 1 |
| 2753 | else | 2759 | else |
| @@ -2759,7 +2765,7 @@ class Inventory | |||
| 2759 | new: => | 2765 | new: => |
| 2760 | @items = {} | 2766 | @items = {} |
| 2761 | 2767 | ||
| 2762 | add_item: (name)=> | 2768 | add_item: (name) => |
| 2763 | if @items[name] | 2769 | if @items[name] |
| 2764 | @items[name] += 1 | 2770 | @items[name] += 1 |
| 2765 | else | 2771 | else |
| @@ -2798,7 +2804,7 @@ inv\add_item "pants" | |||
| 2798 | ```moonscript | 2804 | ```moonscript |
| 2799 | class Person | 2805 | class Person |
| 2800 | clothes: [] | 2806 | clothes: [] |
| 2801 | give_item: (name)=> | 2807 | give_item: (name) => |
| 2802 | table.insert @clothes, name | 2808 | table.insert @clothes, name |
| 2803 | 2809 | ||
| 2804 | a = Person! | 2810 | a = Person! |
| @@ -2814,7 +2820,7 @@ print item for item in *a.clothes | |||
| 2814 | <pre> | 2820 | <pre> |
| 2815 | class Person | 2821 | class Person |
| 2816 | clothes: [] | 2822 | clothes: [] |
| 2817 | give_item: (name)=> | 2823 | give_item: (name) => |
| 2818 | table.insert @clothes, name | 2824 | table.insert @clothes, name |
| 2819 | 2825 | ||
| 2820 | a = Person! | 2826 | a = Person! |
| @@ -2850,7 +2856,7 @@ class Person | |||
| 2850 | ```moonscript | 2856 | ```moonscript |
| 2851 | class BackPack extends Inventory | 2857 | class BackPack extends Inventory |
| 2852 | size: 10 | 2858 | size: 10 |
| 2853 | add_item: (name)=> | 2859 | add_item: (name) => |
| 2854 | if #@items > size then error "背包已满" | 2860 | if #@items > size then error "背包已满" |
| 2855 | super name | 2861 | super name |
| 2856 | ``` | 2862 | ``` |
| @@ -2858,7 +2864,7 @@ class BackPack extends Inventory | |||
| 2858 | <pre> | 2864 | <pre> |
| 2859 | class BackPack extends Inventory | 2865 | class BackPack extends Inventory |
| 2860 | size: 10 | 2866 | size: 10 |
| 2861 | add_item: (name)=> | 2867 | add_item: (name) => |
| 2862 | if #@items > size then error "背包已满" | 2868 | if #@items > size then error "背包已满" |
| 2863 | super name | 2869 | super name |
| 2864 | </pre> | 2870 | </pre> |
| @@ -2873,7 +2879,7 @@ class BackPack extends Inventory | |||
| 2873 | 2879 | ||
| 2874 | ```moonscript | 2880 | ```moonscript |
| 2875 | class Shelf | 2881 | class Shelf |
| 2876 | @__inherited: (child)=> | 2882 | @__inherited: (child) => |
| 2877 | print @__name, "被", child.__name, "继承" | 2883 | print @__name, "被", child.__name, "继承" |
| 2878 | 2884 | ||
| 2879 | -- 将打印: Shelf 被 Cupboard 继承 | 2885 | -- 将打印: Shelf 被 Cupboard 继承 |
| @@ -2882,7 +2888,7 @@ class Cupboard extends Shelf | |||
| 2882 | <YueDisplay> | 2888 | <YueDisplay> |
| 2883 | <pre> | 2889 | <pre> |
| 2884 | class Shelf | 2890 | class Shelf |
| 2885 | @__inherited: (child)=> | 2891 | @__inherited: (child) => |
| 2886 | print @__name, "被", child.__name, "继承" | 2892 | print @__name, "被", child.__name, "继承" |
| 2887 | 2893 | ||
| 2888 | -- 将打印: Shelf 被 Cupboard 继承 | 2894 | -- 将打印: Shelf 被 Cupboard 继承 |
| @@ -3065,7 +3071,7 @@ class Things | |||
| 3065 | ```moonscript | 3071 | ```moonscript |
| 3066 | class MoreThings | 3072 | class MoreThings |
| 3067 | secret = 123 | 3073 | secret = 123 |
| 3068 | log = (msg)-> print "LOG:", msg | 3074 | log = (msg) -> print "LOG:", msg |
| 3069 | 3075 | ||
| 3070 | some_method: => | 3076 | some_method: => |
| 3071 | log "hello world: " .. secret | 3077 | log "hello world: " .. secret |
| @@ -3074,7 +3080,7 @@ class MoreThings | |||
| 3074 | <pre> | 3080 | <pre> |
| 3075 | class MoreThings | 3081 | class MoreThings |
| 3076 | secret = 123 | 3082 | secret = 123 |
| 3077 | log = (msg)-> print "LOG:", msg | 3083 | log = (msg) -> print "LOG:", msg |
| 3078 | 3084 | ||
| 3079 | some_method: => | 3085 | some_method: => |
| 3080 | log "hello world: " .. secret | 3086 | log "hello world: " .. secret |
| @@ -3101,11 +3107,11 @@ assert @@ == self.__class | |||
| 3101 | 例如,使用@@从实例方法快速创建同一类的新实例的方法: | 3107 | 例如,使用@@从实例方法快速创建同一类的新实例的方法: |
| 3102 | 3108 | ||
| 3103 | ```moonscript | 3109 | ```moonscript |
| 3104 | some_instance_method = (...)=> @@ ... | 3110 | some_instance_method = (...) => @@ ... |
| 3105 | ``` | 3111 | ``` |
| 3106 | <YueDisplay> | 3112 | <YueDisplay> |
| 3107 | <pre> | 3113 | <pre> |
| 3108 | some_instance_method = (...)=> @@ ... | 3114 | some_instance_method = (...) => @@ ... |
| 3109 | </pre> | 3115 | </pre> |
| 3110 | </YueDisplay> | 3116 | </YueDisplay> |
| 3111 | 3117 | ||
| @@ -3145,13 +3151,13 @@ class Something | |||
| 3145 | 你也可以使用这种语法为一个函数初始化传入对象的字段。 | 3151 | 你也可以使用这种语法为一个函数初始化传入对象的字段。 |
| 3146 | 3152 | ||
| 3147 | ```moonscript | 3153 | ```moonscript |
| 3148 | new = (@fieldA, @fieldB)=> @ | 3154 | new = (@fieldA, @fieldB) => @ |
| 3149 | obj = new {}, 123, "abc" | 3155 | obj = new {}, 123, "abc" |
| 3150 | print obj | 3156 | print obj |
| 3151 | ``` | 3157 | ``` |
| 3152 | <YueDisplay> | 3158 | <YueDisplay> |
| 3153 | <pre> | 3159 | <pre> |
| 3154 | new = (@fieldA, @fieldB)=> @ | 3160 | new = (@fieldA, @fieldB) => @ |
| 3155 | obj = new {}, 123, "abc" | 3161 | obj = new {}, 123, "abc" |
| 3156 | print obj | 3162 | print obj |
| 3157 | </pre> | 3163 | </pre> |
| @@ -3288,7 +3294,7 @@ file = with File "favorite_foods.txt" | |||
| 3288 | 或者… | 3294 | 或者… |
| 3289 | 3295 | ||
| 3290 | ```moonscript | 3296 | ```moonscript |
| 3291 | create_person = (name, relatives)-> | 3297 | create_person = (name, relatives) -> |
| 3292 | with Person! | 3298 | with Person! |
| 3293 | .name = name | 3299 | .name = name |
| 3294 | \add_relative relative for relative in *relatives | 3300 | \add_relative relative for relative in *relatives |
| @@ -3297,7 +3303,7 @@ me = create_person "Leaf", [dad, mother, sister] | |||
| 3297 | ``` | 3303 | ``` |
| 3298 | <YueDisplay> | 3304 | <YueDisplay> |
| 3299 | <pre> | 3305 | <pre> |
| 3300 | create_person = (name, relatives)-> | 3306 | create_person = (name, relatives) -> |
| 3301 | with Person! | 3307 | with Person! |
| 3302 | .name = name | 3308 | .name = name |
| 3303 | \add_relative relative for relative in *relatives | 3309 | \add_relative relative for relative in *relatives |
| @@ -3421,7 +3427,7 @@ my_object = { | |||
| 3421 | write: => print "值为:", @value | 3427 | write: => print "值为:", @value |
| 3422 | } | 3428 | } |
| 3423 | 3429 | ||
| 3424 | run_callback = (func)-> | 3430 | run_callback = (func) -> |
| 3425 | print "运行回调..." | 3431 | print "运行回调..." |
| 3426 | func! | 3432 | func! |
| 3427 | 3433 | ||
| @@ -3440,7 +3446,7 @@ my_object = { | |||
| 3440 | write: => print "值为:", @value | 3446 | write: => print "值为:", @value |
| 3441 | } | 3447 | } |
| 3442 | 3448 | ||
| 3443 | run_callback = (func)-> | 3449 | run_callback = (func) -> |
| 3444 | print "运行回调..." | 3450 | print "运行回调..." |
| 3445 | func! | 3451 | func! |
| 3446 | 3452 | ||
| @@ -3500,7 +3506,7 @@ print i -- 将打印 0 | |||
| 3500 | ```moonscript | 3506 | ```moonscript |
| 3501 | i = 100 | 3507 | i = 100 |
| 3502 | 3508 | ||
| 3503 | my_func = (using nil)-> | 3509 | my_func = (using nil) -> |
| 3504 | i = "hello" -- 这里创建了一个新的局部变量 | 3510 | i = "hello" -- 这里创建了一个新的局部变量 |
| 3505 | 3511 | ||
| 3506 | my_func! | 3512 | my_func! |
| @@ -3510,7 +3516,7 @@ print i -- 打印 100,i 没有受到影响 | |||
| 3510 | <pre> | 3516 | <pre> |
| 3511 | i = 100 | 3517 | i = 100 |
| 3512 | 3518 | ||
| 3513 | my_func = (using nil)-> | 3519 | my_func = (using nil) -> |
| 3514 | i = "hello" -- 这里创建了一个新的局部变量 | 3520 | i = "hello" -- 这里创建了一个新的局部变量 |
| 3515 | 3521 | ||
| 3516 | my_func! | 3522 | my_func! |
| @@ -3524,7 +3530,7 @@ using子句中可以填写多个用逗号分隔名称。指定可以访问和修 | |||
| 3524 | tmp = 1213 | 3530 | tmp = 1213 |
| 3525 | i, k = 100, 50 | 3531 | i, k = 100, 50 |
| 3526 | 3532 | ||
| 3527 | my_func = (add using k, i)-> | 3533 | my_func = (add using k, i) -> |
| 3528 | tmp = tmp + add -- 创建了一个新的局部tmp | 3534 | tmp = tmp + add -- 创建了一个新的局部tmp |
| 3529 | i += tmp | 3535 | i += tmp |
| 3530 | k += tmp | 3536 | k += tmp |
| @@ -3537,7 +3543,7 @@ print i, k -- 这些已经被更新 | |||
| 3537 | tmp = 1213 | 3543 | tmp = 1213 |
| 3538 | i, k = 100, 50 | 3544 | i, k = 100, 50 |
| 3539 | 3545 | ||
| 3540 | my_func = (add using k, i)-> | 3546 | my_func = (add using k, i) -> |
| 3541 | tmp = tmp + add -- 创建了一个新的局部tmp | 3547 | tmp = tmp + add -- 创建了一个新的局部tmp |
| 3542 | i += tmp | 3548 | i += tmp |
| 3543 | k += tmp | 3549 | k += tmp |
