aboutsummaryrefslogtreecommitdiff
path: root/doc/docs/zh
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2023-10-22 00:55:02 +0800
committerLi Jin <dragon-fly@qq.com>2023-10-22 00:55:02 +0800
commit652a8839f885b73fff57942a9db8b26e9cb5233b (patch)
tree0b24bb5610823a831ec0addbba37cb6200203b27 /doc/docs/zh
parentf61a4a1d9a1b979b8a0c2e8a9c194a284f42220f (diff)
downloadyuescript-652a8839f885b73fff57942a9db8b26e9cb5233b.tar.gz
yuescript-652a8839f885b73fff57942a9db8b26e9cb5233b.tar.bz2
yuescript-652a8839f885b73fff57942a9db8b26e9cb5233b.zip
fixing issues from #152.
Diffstat (limited to 'doc/docs/zh')
-rwxr-xr-xdoc/docs/zh/doc/README.md182
1 files changed, 102 insertions, 80 deletions
diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md
index 1d234a0..8a236c1 100755
--- a/doc/docs/zh/doc/README.md
+++ b/doc/docs/zh/doc/README.md
@@ -386,6 +386,80 @@ tb::func! if tb != nil
386</pre> 386</pre>
387</YueDisplay> 387</YueDisplay>
388 388
389### 链式比较
390
391您可以在月之脚本中进行比较表达式的链式书写:
392
393```moonscript
394print 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5
395-- 输出:true
396
397a = 5
398print 1 <= a <= 10
399-- 输出:true
400```
401<YueDisplay>
402<pre>
403print 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5
404-- 输出:true
405
406a = 5
407print 1 <= a <= 10
408-- 输出:true
409</pre>
410</YueDisplay>
411
412可以注意一下链式比较表达式的求值行为:
413
414```moonscript
415v = (x)->
416 print x
417 x
418
419print v(1) < v(2) <= v(3)
420--[[
421 输出:
422 2
423 1
424 3
425 true
426]]
427
428print v(1) > v(2) <= v(3)
429--[[
430 输出:
431 2
432 1
433 false
434]]
435```
436<YueDisplay>
437<pre>
438v = (x)->
439 print x
440 x
441
442print v(1) < v(2) <= v(3)
443--[[
444 输出:
445 2
446 1
447 3
448 true
449]]
450
451print v(1) > v(2) <= v(3)
452--[[
453 输出:
454 2
455 1
456 false
457]]
458</pre>
459</YueDisplay>
460
461在上面的例子里,中间的表达式`v(2)`仅被计算一次,如果把表达式写成`v(1) < v(2) and v(2) <= v(3)`的方式,中间的`v(2)`才会被计算两次。在链式比较中,求值的顺序往往是未定义的。所以强烈建议不要在链式比较中使用具有副作用(比如做打印操作)的表达式。如果需要使用有副作用的函数,应明确使用短路 `and` 运算符来做连接。
462
389### 表追加 463### 表追加
390 464
391**[] =** 操作符用于向Lua表的最后插入值。 465**[] =** 操作符用于向Lua表的最后插入值。
@@ -982,17 +1056,17 @@ do
982最好是通过示例来解释。以下是如何从表格中解包前两个值的方法: 1056最好是通过示例来解释。以下是如何从表格中解包前两个值的方法:
983 1057
984```moonscript 1058```moonscript
985thing = {1, 2} 1059thing = [1, 2]
986 1060
987{a, b} = thing 1061[a, b] = thing
988print a, b 1062print a, b
989``` 1063```
990<YueDisplay> 1064<YueDisplay>
991 1065
992<pre> 1066<pre>
993thing = {1, 2} 1067thing = [1, 2]
994 1068
995{a, b} = thing 1069[a, b] = thing
996print a, b 1070print a, b
997</pre> 1071</pre>
998</YueDisplay> 1072</YueDisplay>
@@ -1881,6 +1955,19 @@ t = {
1881</pre> 1955</pre>
1882</YueDisplay> 1956</YueDisplay>
1883 1957
1958Lua的表同时具有数组部分和哈希部分,但有时候你会希望在书写Lua表时,对Lua表做数组和哈希不同用法的语义区分。然后你可以用 **[ ]** 而不是 **{ }** 来编写表示数组的 Lua 表,并且不允许在数组 Lua 表中写入任何键值对。
1959
1960```moonscript
1961some_values = [ 1, 2, 3, 4 ]
1962list_with_one_element = [ 1, ]
1963```
1964<YueDisplay>
1965<pre>
1966some_values = [ 1, 2, 3, 4 ]
1967list_with_one_element = [ 1, ]
1968</pre>
1969</YueDisplay>
1970
1884## 推导式 1971## 推导式
1885 1972
1886推导式为我们提供了一种便捷的语法,通过遍历现有对象并对其值应用表达式来构造出新的表格。月之脚本有两种推导式:列表推导式和表格推导式。它们最终都是产生Lua表格;列表推导式将值累积到类似数组的表格中,而表格推导式允许您在每次遍历时设置新表格的键和值。 1973推导式为我们提供了一种便捷的语法,通过遍历现有对象并对其值应用表达式来构造出新的表格。月之脚本有两种推导式:列表推导式和表格推导式。它们最终都是产生Lua表格;列表推导式将值累积到类似数组的表格中,而表格推导式允许您在每次遍历时设置新表格的键和值。
@@ -1890,12 +1977,12 @@ t = {
1890以下操作创建了一个items表的副本,但所有包含的值都翻倍了。 1977以下操作创建了一个items表的副本,但所有包含的值都翻倍了。
1891 1978
1892```moonscript 1979```moonscript
1893items = { 1, 2, 3, 4 } 1980items = [ 1, 2, 3, 4 ]
1894doubled = [item * 2 for i, item in ipairs items] 1981doubled = [item * 2 for i, item in ipairs items]
1895``` 1982```
1896<YueDisplay> 1983<YueDisplay>
1897<pre> 1984<pre>
1898items = { 1, 2, 3, 4 } 1985items = [ 1, 2, 3, 4 ]
1899doubled = [item * 2 for i, item in ipairs items] 1986doubled = [item * 2 for i, item in ipairs items]
1900</pre> 1987</pre>
1901</YueDisplay> 1988</YueDisplay>
@@ -1929,18 +2016,18 @@ for和when子句可以根据需要进行链式操作。唯一的要求是推导
1929使用多个for子句与使用多重循环的效果相同: 2016使用多个for子句与使用多重循环的效果相同:
1930 2017
1931```moonscript 2018```moonscript
1932x_coords = {4, 5, 6, 7} 2019x_coords = [4, 5, 6, 7]
1933y_coords = {9, 2, 3} 2020y_coords = [9, 2, 3]
1934 2021
1935points = [{x, y} for x in *x_coords \ 2022points = [ [x, y] for x in *x_coords \
1936for y in *y_coords] 2023for y in *y_coords]
1937``` 2024```
1938<YueDisplay> 2025<YueDisplay>
1939<pre> 2026<pre>
1940x_coords = {4, 5, 6, 7} 2027x_coords = [4, 5, 6, 7]
1941y_coords = {9, 2, 3} 2028y_coords = [9, 2, 3]
1942 2029
1943points = [{x, y} for x in *x_coords \ 2030points = [ [x, y] for x in *x_coords \
1944for y in *y_coords] 2031for y in *y_coords]
1945</pre> 2032</pre>
1946</YueDisplay> 2033</YueDisplay>
@@ -1995,12 +2082,12 @@ no_color = {k, v for k, v in pairs thing when k != "color"}
1995**\***操作符在表格推导式中能使用。在下面的例子里,我们为几个数字创建了一个平方根查找表。 2082**\***操作符在表格推导式中能使用。在下面的例子里,我们为几个数字创建了一个平方根查找表。
1996 2083
1997```moonscript 2084```moonscript
1998numbers = {1, 2, 3, 4} 2085numbers = [1, 2, 3, 4]
1999sqrts = {i, math.sqrt i for i in *numbers} 2086sqrts = {i, math.sqrt i for i in *numbers}
2000``` 2087```
2001<YueDisplay> 2088<YueDisplay>
2002<pre> 2089<pre>
2003numbers = {1, 2, 3, 4} 2090numbers = [1, 2, 3, 4]
2004sqrts = {i, math.sqrt i for i in *numbers} 2091sqrts = {i, math.sqrt i for i in *numbers}
2005</pre> 2092</pre>
2006</YueDisplay> 2093</YueDisplay>
@@ -2361,20 +2448,11 @@ print "你真幸运!" unless math.random! > 0.1
2361 2448
2362### 范围表达式 2449### 范围表达式
2363 2450
2364您可以使用范围表达式来编写进行围检查的代码。 2451您可以使用范围表达式来编写进行范围检查的代码。
2365 2452
2366```moonscript 2453```moonscript
2367a = 5 2454a = 5
2368 2455
2369if a in [1, 10]
2370 print "a在1到10的范围内"
2371
2372if a not in [1, 10]
2373 print "a不在1到10的范围内"
2374
2375if a in (0, 11)
2376 print "a在0到11的开放区间内"
2377
2378if a in {1, 3, 5, 7} 2456if a in {1, 3, 5, 7}
2379 print "检查离散值的相等性" 2457 print "检查离散值的相等性"
2380 2458
@@ -2385,15 +2463,6 @@ if a in list
2385<pre> 2463<pre>
2386a = 5 2464a = 5
2387 2465
2388if a in [1, 10]
2389 print "a在1到10的范围内"
2390
2391if a not in [1, 10]
2392 print "a不在1到10的范围内"
2393
2394if a in (0, 11)
2395 print "a在0到11的开放区间内"
2396
2397if a in {1, 3, 5, 7} 2466if a in {1, 3, 5, 7}
2398 print "检查离散值的相等性" 2467 print "检查离散值的相等性"
2399 2468
@@ -2610,53 +2679,6 @@ switch item
2610</pre> 2679</pre>
2611</YueDisplay> 2680</YueDisplay>
2612 2681
2613### 范围匹配
2614
2615使用`in`范围匹配表达式,你可以在switch的when子句中进行范围匹配的检查处理。
2616
2617```moonscript
2618value = 5
2619
2620switch item
2621 -- 使用闭区间进行范围检查
2622 when in [1, 3]
2623 print "1 <= value <= 3"
2624
2625 -- 使用开闭区间进行范围检查
2626 when in (6, 8]
2627 print "6 < value <= 8"
2628
2629 -- 检查不在范围内
2630 when not in [1, 10)
2631 print "不是 (1 <= value < 10)"
2632
2633 -- 检查离散值
2634 when in {11, 21, 99}
2635 print "值是 11, 21 或 99"
2636```
2637<YueDisplay>
2638<pre>
2639value = 5
2640
2641switch item
2642 -- 使用闭区间进行范围检查
2643 when in [1, 3]
2644 print "1 <= value <= 3"
2645
2646 -- 使用开闭区间进行范围检查
2647 when in (6, 8]
2648 print "6 < value <= 8"
2649
2650 -- 检查不在范围内
2651 when not in [1, 10)
2652 print "不是 (1 <= value < 10)"
2653
2654 -- 检查离散值
2655 when in {11, 21, 99}
2656 print "值是 11, 21 或 99"
2657</pre>
2658</YueDisplay>
2659
2660## 面向对象编程 2682## 面向对象编程
2661 2683
2662在以下的示例中,月之脚本生成的Lua代码可能看起来会很复杂。所以最好主要关注月之脚本代码层面的意义,然后如果您想知道关于面向对象功能的实现细节,再查看Lua代码。 2684在以下的示例中,月之脚本生成的Lua代码可能看起来会很复杂。所以最好主要关注月之脚本代码层面的意义,然后如果您想知道关于面向对象功能的实现细节,再查看Lua代码。