aboutsummaryrefslogtreecommitdiff
path: root/doc
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
parentf61a4a1d9a1b979b8a0c2e8a9c194a284f42220f (diff)
downloadyuescript-652a8839f885b73fff57942a9db8b26e9cb5233b.tar.gz
yuescript-652a8839f885b73fff57942a9db8b26e9cb5233b.tar.bz2
yuescript-652a8839f885b73fff57942a9db8b26e9cb5233b.zip
fixing issues from #152.
Diffstat (limited to 'doc')
-rwxr-xr-xdoc/docs/doc/README.md180
-rwxr-xr-xdoc/docs/zh/doc/README.md182
2 files changed, 203 insertions, 159 deletions
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md
index c60330a..6662752 100755
--- a/doc/docs/doc/README.md
+++ b/doc/docs/doc/README.md
@@ -389,6 +389,80 @@ tb::func! if tb != nil
389</pre> 389</pre>
390</YueDisplay> 390</YueDisplay>
391 391
392### Chaining Comparisons
393
394Comparisons can be arbitrarily chained:
395
396```moonscript
397print 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5
398-- output: true
399
400a = 5
401print 1 <= a <= 10
402-- output: true
403```
404<YueDisplay>
405<pre>
406print 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5
407-- output: true
408
409a = 5
410print 1 <= a <= 10
411-- output: true
412</pre>
413</YueDisplay>
414
415Note the evaluation behavior of chained comparisons:
416
417```moonscript
418v = (x)->
419 print x
420 x
421
422print v(1) < v(2) <= v(3)
423--[[
424 output:
425 2
426 1
427 3
428 true
429]]
430
431print v(1) > v(2) <= v(3)
432--[[
433 output:
434 2
435 1
436 false
437]]
438```
439<YueDisplay>
440<pre>
441v = (x)->
442 print x
443 x
444
445print v(1) < v(2) <= v(3)
446--[[
447 output:
448 2
449 1
450 3
451true
452]]
453
454print v(1) > v(2) <= v(3)
455--[[
456 output:
457 2
458 1
459 false
460]]
461</pre>
462</YueDisplay>
463
464The middle expression is only evaluated once, rather than twice as it would be if the expression were written as `v(1) < v(2) and v(2) <= v(3)`. However, the order of evaluations in a chained comparison is undefined. It is strongly recommended not to use expressions with side effects (such as printing) in chained comparisons. If side effects are required, the short-circuit `and` operator should be used explicitly.
465
392### Table Appending 466### Table Appending
393The **[] =** operator is used to append values to tables. 467The **[] =** operator is used to append values to tables.
394 468
@@ -984,17 +1058,17 @@ Typically when you see a table literal, {1,2,3}, it is on the right hand side of
984This is best explained with examples. Here is how you would unpack the first two values from a table: 1058This is best explained with examples. Here is how you would unpack the first two values from a table:
985 1059
986```moonscript 1060```moonscript
987thing = {1, 2} 1061thing = [1, 2]
988 1062
989{a, b} = thing 1063[a, b] = thing
990print a, b 1064print a, b
991``` 1065```
992<YueDisplay> 1066<YueDisplay>
993 1067
994<pre> 1068<pre>
995thing = {1, 2} 1069thing = [1, 2]
996 1070
997{a, b} = thing 1071[a, b] = thing
998print a, b 1072print a, b
999</pre> 1073</pre>
1000</YueDisplay> 1074</YueDisplay>
@@ -1921,6 +1995,19 @@ t = {
1921</pre> 1995</pre>
1922</YueDisplay> 1996</YueDisplay>
1923 1997
1998Lua tables have both an array part and a hash part, but sometimes you want to make a semantic distinction between array and hash usage when writing Lua tables. Then you can write Lua table with **[ ]** instead of **{ }** to represent an array table and writing any key value pair in a list table won't be allowed.
1999
2000```moonscript
2001some_values = [ 1, 2, 3, 4 ]
2002list_with_one_element = [ 1, ]
2003```
2004<YueDisplay>
2005<pre>
2006some_values = [ 1, 2, 3, 4 ]
2007list_with_one_element = [ 1, ]
2008</pre>
2009</YueDisplay>
2010
1924## Comprehensions 2011## Comprehensions
1925 2012
1926Comprehensions provide a convenient syntax for constructing a new table by iterating over some existing object and applying an expression to its values. There are two kinds of comprehensions: list comprehensions and table comprehensions. They both produce Lua tables; list comprehensions accumulate values into an array-like table, and table comprehensions let you set both the key and the value on each iteration. 2013Comprehensions provide a convenient syntax for constructing a new table by iterating over some existing object and applying an expression to its values. There are two kinds of comprehensions: list comprehensions and table comprehensions. They both produce Lua tables; list comprehensions accumulate values into an array-like table, and table comprehensions let you set both the key and the value on each iteration.
@@ -1930,12 +2017,12 @@ Comprehensions provide a convenient syntax for constructing a new table by itera
1930The following creates a copy of the items table but with all the values doubled. 2017The following creates a copy of the items table but with all the values doubled.
1931 2018
1932```moonscript 2019```moonscript
1933items = { 1, 2, 3, 4 } 2020items = [ 1, 2, 3, 4 ]
1934doubled = [item * 2 for i, item in ipairs items] 2021doubled = [item * 2 for i, item in ipairs items]
1935``` 2022```
1936<YueDisplay> 2023<YueDisplay>
1937<pre> 2024<pre>
1938items = { 1, 2, 3, 4 } 2025items = [ 1, 2, 3, 4 ]
1939doubled = [item * 2 for i, item in ipairs items] 2026doubled = [item * 2 for i, item in ipairs items]
1940</pre> 2027</pre>
1941</YueDisplay> 2028</YueDisplay>
@@ -1969,18 +2056,18 @@ The for and when clauses can be chained as much as desired. The only requirement
1969Using multiple for clauses is the same as using nested loops: 2056Using multiple for clauses is the same as using nested loops:
1970 2057
1971```moonscript 2058```moonscript
1972x_coords = {4, 5, 6, 7} 2059x_coords = [4, 5, 6, 7]
1973y_coords = {9, 2, 3} 2060y_coords = [9, 2, 3]
1974 2061
1975points = [{x, y} for x in *x_coords \ 2062points = [ [x, y] for x in *x_coords \
1976for y in *y_coords] 2063for y in *y_coords]
1977``` 2064```
1978<YueDisplay> 2065<YueDisplay>
1979<pre> 2066<pre>
1980x_coords = {4, 5, 6, 7} 2067x_coords = [4, 5, 6, 7]
1981y_coords = {9, 2, 3} 2068y_coords = [9, 2, 3]
1982 2069
1983points = [{x, y} for x in *x_coords \ 2070points = [ [x, y] for x in *x_coords \
1984for y in *y_coords] 2071for y in *y_coords]
1985</pre> 2072</pre>
1986</YueDisplay> 2073</YueDisplay>
@@ -2035,12 +2122,12 @@ no_color = {k, v for k, v in pairs thing when k != "color"}
2035The **\*** operator is also supported. Here we create a square root look up table for a few numbers. 2122The **\*** operator is also supported. Here we create a square root look up table for a few numbers.
2036 2123
2037```moonscript 2124```moonscript
2038numbers = {1, 2, 3, 4} 2125numbers = [1, 2, 3, 4]
2039sqrts = {i, math.sqrt i for i in *numbers} 2126sqrts = {i, math.sqrt i for i in *numbers}
2040``` 2127```
2041<YueDisplay> 2128<YueDisplay>
2042<pre> 2129<pre>
2043numbers = {1, 2, 3, 4} 2130numbers = [1, 2, 3, 4]
2044sqrts = {i, math.sqrt i for i in *numbers} 2131sqrts = {i, math.sqrt i for i in *numbers}
2045</pre> 2132</pre>
2046</YueDisplay> 2133</YueDisplay>
@@ -2405,15 +2492,6 @@ You can write range checking code with an `in-expression`.
2405```moonscript 2492```moonscript
2406a = 5 2493a = 5
2407 2494
2408if a in [1, 10]
2409 print "a is in range from 1 to 10"
2410
2411if a not in [1, 10]
2412 print "a is not in range from 1 to 10"
2413
2414if a in (0, 11)
2415 print "a is between 0 and 11 with open intervals"
2416
2417if a in {1, 3, 5, 7} 2495if a in {1, 3, 5, 7}
2418 print "checking equality with discrete values" 2496 print "checking equality with discrete values"
2419 2497
@@ -2424,15 +2502,6 @@ if a in list
2424<pre> 2502<pre>
2425a = 5 2503a = 5
2426 2504
2427if a in [1, 10]
2428 print "a is in range from 1 to 10"
2429
2430if a not in [1, 10]
2431 print "a is not in range from 1 to 10"
2432
2433if a in (0, 11)
2434 print "a is between 0 and 11 with open intervals"
2435
2436if a in {1, 3, 5, 7} 2505if a in {1, 3, 5, 7}
2437 print "checking equality with discrete values" 2506 print "checking equality with discrete values"
2438 2507
@@ -2649,53 +2718,6 @@ switch item
2649</pre> 2718</pre>
2650</YueDisplay> 2719</YueDisplay>
2651 2720
2652### Range Matching
2653
2654You can do range matching in a switch when clause using `In` expressions.
2655
2656```moonscript
2657value = 5
2658
2659switch item
2660 -- range checking with closed interval
2661 when in [1, 3]
2662 print "1 <= value <= 3"
2663
2664 -- range checking with open and closed interval
2665 when in (6, 8]
2666 print "6 < value <= 8"
2667
2668 -- not in range checking
2669 when not in [1, 10)
2670 print "not (1 <= value < 10)"
2671
2672 -- checking discrete values
2673 when in {11, 21, 99}
2674 print "value is 11, 21 or 99"
2675```
2676<YueDisplay>
2677<pre>
2678value = 5
2679
2680switch item
2681 -- range checking with closed interval
2682 when in [1, 3]
2683 print "1 <= value <= 3"
2684
2685 -- range checking with open and closed interval
2686 when in (6, 8]
2687 print "6 < value <= 8"
2688
2689 -- not in range checking
2690 when not in [1, 10)
2691 print "not (1 <= value < 10)"
2692
2693 -- checking discrete values
2694 when in {11, 21, 99}
2695 print "value is 11, 21 or 99"
2696</pre>
2697</YueDisplay>
2698
2699## Object Oriented Programming 2721## Object Oriented Programming
2700 2722
2701In these examples, the generated Lua code may appear overwhelming. It is best to focus on the meaning of the Yuescript code at first, then look into the Lua code if you wish to know the implementation details. 2723In these examples, the generated Lua code may appear overwhelming. It is best to focus on the meaning of the Yuescript code at first, then look into the Lua code if you wish to know the implementation details.
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代码。