aboutsummaryrefslogtreecommitdiff
path: root/doc/docs
diff options
context:
space:
mode:
Diffstat (limited to 'doc/docs')
-rwxr-xr-xdoc/docs/doc/README.md126
-rwxr-xr-xdoc/docs/zh/doc/README.md176
2 files changed, 247 insertions, 55 deletions
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md
index c4518bf..c275c52 100755
--- a/doc/docs/doc/README.md
+++ b/doc/docs/doc/README.md
@@ -132,14 +132,14 @@ export 🌛 = "月之脚本"
132 132
133 Use YueScript module in Lua: 133 Use YueScript module in Lua:
134 134
135* **Case 1** 135* **Case 1**
136Require "your_yuescript_entry.yue" in Lua. 136Require "your_yuescript_entry.yue" in Lua.
137```Lua 137```Lua
138require("yue")("your_yuescript_entry") 138require("yue")("your_yuescript_entry")
139``` 139```
140 And this code still works when you compile "your_yuescript_entry.yue" to "your_yuescript_entry.lua" in the same path. In the rest YueScript files just use the normal **require** or **import**. The code line numbers in error messages will also be handled correctly. 140 And this code still works when you compile "your_yuescript_entry.yue" to "your_yuescript_entry.lua" in the same path. In the rest YueScript files just use the normal **require** or **import**. The code line numbers in error messages will also be handled correctly.
141 141
142* **Case 2** 142* **Case 2**
143Require YueScript module and rewite message by hand. 143Require YueScript module and rewite message by hand.
144```lua 144```lua
145local yue = require("yue") 145local yue = require("yue")
@@ -151,7 +151,7 @@ end, function(err)
151end) 151end)
152``` 152```
153 153
154* **Case 3** 154* **Case 3**
155Use the YueScript compiler function in Lua. 155Use the YueScript compiler function in Lua.
156```lua 156```lua
157local yue = require("yue") 157local yue = require("yue")
@@ -203,12 +203,12 @@ Usage: yue [options|files|directories] ...
203 Execute without options to enter REPL, type symbol '$' 203 Execute without options to enter REPL, type symbol '$'
204 in a single line to start/stop multi-line mode 204 in a single line to start/stop multi-line mode
205``` 205```
206  Use cases: 206  Use cases:
207  Recursively compile every YueScript file with extension **.yue** under current path: **yue .** 207  Recursively compile every YueScript file with extension **.yue** under current path: **yue .**
208  Compile and save results to a target path: **yue -t /target/path/ .** 208  Compile and save results to a target path: **yue -t /target/path/ .**
209  Compile and reserve debug info: **yue -l .** 209  Compile and reserve debug info: **yue -l .**
210  Compile and generate minified codes: **yue -m .** 210  Compile and generate minified codes: **yue -m .**
211  Execute raw codes: **yue -e 'print 123'** 211  Execute raw codes: **yue -e 'print 123'**
212  Execute a YueScript file: **yue -e main.yue** 212  Execute a YueScript file: **yue -e main.yue**
213 213
214## Macro 214## Macro
@@ -570,7 +570,7 @@ merge = {...a, ...b}
570 570
571The **<>** operator can be used as a shortcut for metatable manipulation. 571The **<>** operator can be used as a shortcut for metatable manipulation.
572 572
573* **Metatable Creation** 573* **Metatable Creation**
574Create normal table with empty bracekets **<>** or metamethod key which is surrounded by **<>**. 574Create normal table with empty bracekets **<>** or metamethod key which is surrounded by **<>**.
575 575
576```moonscript 576```moonscript
@@ -606,7 +606,7 @@ close _ = &lt;close&gt;: -> print "out of scope"
606</pre> 606</pre>
607</YueDisplay> 607</YueDisplay>
608 608
609* **Metatable Accessing** 609* **Metatable Accessing**
610Accessing metatable with **<>** or metamethod name surrounded by **<>** or writing some expression in **<>**. 610Accessing metatable with **<>** or metamethod name surrounded by **<>** or writing some expression in **<>**.
611 611
612```moonscript 612```moonscript
@@ -630,7 +630,7 @@ print tb.item
630</pre> 630</pre>
631</YueDisplay> 631</YueDisplay>
632 632
633* **Metatable Destructure** 633* **Metatable Destructure**
634Destruct metatable with metamethod key surrounded by **<>**. 634Destruct metatable with metamethod key surrounded by **<>**.
635 635
636```moonscript 636```moonscript
@@ -860,7 +860,7 @@ do
860 860
861The export statement offers a concise way to define modules. 861The export statement offers a concise way to define modules.
862 862
863* **Named Export** 863* **Named Export**
864Named export will define a local variable as well as adding a field in the exported table. 864Named export will define a local variable as well as adding a field in the exported table.
865 865
866```moonscript 866```moonscript
@@ -924,7 +924,7 @@ export["a-b-c"] = 123
924</pre> 924</pre>
925</YueDisplay> 925</YueDisplay>
926 926
927* **Unnamed Export** 927* **Unnamed Export**
928Unnamed export will add the target item into the array part of the exported table. 928Unnamed export will add the target item into the array part of the exported table.
929 929
930```moonscript 930```moonscript
@@ -954,7 +954,7 @@ export with tmp
954</pre> 954</pre>
955</YueDisplay> 955</YueDisplay>
956 956
957* **Default Export** 957* **Default Export**
958Using the **default** keyword in export statement to replace the exported table with any thing. 958Using the **default** keyword in export statement to replace the exported table with any thing.
959 959
960```moonscript 960```moonscript
@@ -2782,6 +2782,102 @@ switch item
2782</pre> 2782</pre>
2783</YueDisplay> 2783</YueDisplay>
2784 2784
2785You can also match against array elements, table fields, and even nested structures with array or table literals.
2786
2787Match against array elements.
2788
2789```moonscript
2790switch tb
2791 when [1, 2, 3]
2792 print "1, 2, 3"
2793 when [1, b, 3]
2794 print "1, #{b}, 3"
2795 when [1, 2, b = 3] -- b has a default value
2796 print "1, 2, #{b}"
2797```
2798<YueDisplay>
2799<pre>
2800switch tb
2801 when [1, 2, 3]
2802 print "1, 2, 3"
2803 when [1, b, 3]
2804 print "1, #{b}, 3"
2805 when [1, 2, b = 3] -- b has a default value
2806 print "1, 2, #{b}"
2807</pre>
2808</YueDisplay>
2809
2810Match against table fields with destructuring.
2811
2812```moonscript
2813switch tb
2814 when success: true, :result
2815 print "success", result
2816 when success: false
2817 print "failed", result
2818 else
2819 print "invalid"
2820```
2821<YueDisplay>
2822<pre>
2823switch tb
2824 when success: true, :result
2825 print "success", result
2826 when success: false
2827 print "failed", result
2828 else
2829 print "invalid"
2830</pre>
2831</YueDisplay>
2832
2833Match against nested table structures.
2834
2835```moonscript
2836switch tb
2837 when data: {type: "success", :content}
2838 print "success", content
2839 when data: {type: "error", :content}
2840 print "failed", content
2841 else
2842 print "invalid"
2843```
2844<YueDisplay>
2845<pre>
2846switch tb
2847 when data: {type: "success", :content}
2848 print "success", content
2849 when data: {type: "error", :content}
2850 print "failed", content
2851 else
2852 print "invalid"
2853</pre>
2854</YueDisplay>
2855
2856Match against array of tables.
2857
2858```moonscript
2859switch tb
2860 when [
2861 {a: 1, b: 2}
2862 {a: 3, b: 4}
2863 {a: 5, b: 6}
2864 fourth
2865 ]
2866 print "matched", fourth
2867```
2868<YueDisplay>
2869<pre>
2870switch tb
2871 when [
2872 {a: 1, b: 2}
2873 {a: 3, b: 4}
2874 {a: 5, b: 6}
2875 fourth
2876 ]
2877 print "matched", fourth
2878</pre>
2879</YueDisplay>
2880
2785## Object Oriented Programming 2881## Object Oriented Programming
2786 2882
2787In 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. 2883In 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 754566c..11dc108 100755
--- a/doc/docs/zh/doc/README.md
+++ b/doc/docs/zh/doc/README.md
@@ -122,7 +122,7 @@ export 🌛 = "月之脚本"
122 122
123* **下载预编译的二进制程序** 123* **下载预编译的二进制程序**
124 124
125&emsp;可以下载预编译的二进制程序,包括兼容不同 Lua 版本的二进制可执行文件和库文件。 125&emsp;可以下载预编译的二进制程序,包括兼容不同 Lua 版本的二进制可执行文件和库文件。
126 126
127&emsp;在[这里](https://github.com/IppClub/YueScript/releases)下载预编译的二进制程序。 127&emsp;在[这里](https://github.com/IppClub/YueScript/releases)下载预编译的二进制程序。
128 128
@@ -132,14 +132,14 @@ export 🌛 = "月之脚本"
132 132
133在Lua中使用月之脚本模块: 133在Lua中使用月之脚本模块:
134 134
135* **用法 1** 135* **用法 1**
136在Lua中引入 "你的脚本入口文件.yue"。 136在Lua中引入 "你的脚本入口文件.yue"。
137```Lua 137```Lua
138require("yue")("你的脚本入口文件") 138require("yue")("你的脚本入口文件")
139``` 139```
140当你在同一路径下把 "你的脚本入口文件.yue" 编译成了 "你的脚本入口文件.lua" 时,仍然可以使用这个代码加载 .lua 代码文件。在其余的月之脚本文件中,只需正常使用 **require** 或 **import**进行脚本引用即可。错误消息中的代码行号也会被正确处理。 140当你在同一路径下把 "你的脚本入口文件.yue" 编译成了 "你的脚本入口文件.lua" 时,仍然可以使用这个代码加载 .lua 代码文件。在其余的月之脚本文件中,只需正常使用 **require** 或 **import**进行脚本引用即可。错误消息中的代码行号也会被正确处理。
141 141
142* **用法 2** 142* **用法 2**
143手动引入月之脚本模块并重写错误消息来帮助调试。 143手动引入月之脚本模块并重写错误消息来帮助调试。
144```lua 144```lua
145local yue = require("yue") 145local yue = require("yue")
@@ -151,7 +151,7 @@ end, function(err)
151end) 151end)
152``` 152```
153 153
154* **用法 3** 154* **用法 3**
155在Lua中使用月之脚本编译器功能。 155在Lua中使用月之脚本编译器功能。
156```lua 156```lua
157local yue = require("yue") 157local yue = require("yue")
@@ -202,12 +202,12 @@ f!
202 不添加任何选项执行命令可以进入REPL模式, 202 不添加任何选项执行命令可以进入REPL模式,
203 在单行输入符号 '$' 并换行后,可以开始或是停止多行输入模式 203 在单行输入符号 '$' 并换行后,可以开始或是停止多行输入模式
204``` 204```
205&emsp;&emsp;使用案例: 205&emsp;&emsp;使用案例:
206&emsp;&emsp;递归编译当前路径下扩展名为 **.yue** 的每个月之脚本文件: **yue .** 206&emsp;&emsp;递归编译当前路径下扩展名为 **.yue** 的每个月之脚本文件: **yue .**
207&emsp;&emsp;编译并将结果保存到目标路径: **yue -t /target/path/ .** 207&emsp;&emsp;编译并将结果保存到目标路径: **yue -t /target/path/ .**
208&emsp;&emsp;编译并保留调试信息: **yue -l .** 208&emsp;&emsp;编译并保留调试信息: **yue -l .**
209&emsp;&emsp;编译并生成压缩代码: **yue -m .** 209&emsp;&emsp;编译并生成压缩代码: **yue -m .**
210&emsp;&emsp;直接执行代码: **yue -e 'print 123'** 210&emsp;&emsp;直接执行代码: **yue -e 'print 123'**
211&emsp;&emsp;执行一个月之脚本文件: **yue -e main.yue** 211&emsp;&emsp;执行一个月之脚本文件: **yue -e main.yue**
212 212
213## 宏 213## 宏
@@ -333,7 +333,7 @@ end
333 333
334### 导出宏 334### 导出宏
335 335
336宏函数可以从一个模块中导出,并在另一个模块中导入。您必须将导出的宏函数放在一个单独的文件中使用,而且只有宏定义、宏导入和宏展开可以放入这个宏导出模块中。 336宏函数可以从一个模块中导出,并在另一个模块中导入。你必须将导出的宏函数放在一个单独的文件中使用,而且只有宏定义、宏导入和宏展开可以放入这个宏导出模块中。
337```moonscript 337```moonscript
338-- 文件: utils.yue 338-- 文件: utils.yue
339export macro map = (items, action) -> "[#{action} for _ in *#{items}]" 339export macro map = (items, action) -> "[#{action} for _ in *#{items}]"
@@ -439,7 +439,7 @@ tb::func! if tb != nil
439 439
440### 链式比较 440### 链式比较
441 441
442可以在月之脚本中进行比较表达式的链式书写: 442可以在月之脚本中进行比较表达式的链式书写:
443 443
444```moonscript 444```moonscript
445print 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5 445print 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5
@@ -528,7 +528,7 @@ tab[] = "Value"
528 528
529### 表扩展 529### 表扩展
530 530
531可以使用前置 `...` 操作符在Lua表中插入数组表或哈希表。 531可以使用前置 `...` 操作符在Lua表中插入数组表或哈希表。
532 532
533```moonscript 533```moonscript
534parts = 534parts =
@@ -569,7 +569,7 @@ merge = {...a, ...b}
569 569
570**<>** 操作符可提供元表操作的快捷方式。 570**<>** 操作符可提供元表操作的快捷方式。
571 571
572* **元表创建** 572* **元表创建**
573使用空括号 **<>** 或被 **<>** 包围的元方法键创建普通的Lua表。 573使用空括号 **<>** 或被 **<>** 包围的元方法键创建普通的Lua表。
574 574
575```moonscript 575```moonscript
@@ -605,7 +605,7 @@ close _ = &lt;close&gt;: -> print "超出范围"
605</pre> 605</pre>
606</YueDisplay> 606</YueDisplay>
607 607
608* **元表访问** 608* **元表访问**
609使用 **<>** 或被 **<>** 包围的元方法名或在 **<>** 中编写某些表达式来访问元表。 609使用 **<>** 或被 **<>** 包围的元方法名或在 **<>** 中编写某些表达式来访问元表。
610 610
611```moonscript 611```moonscript
@@ -629,7 +629,7 @@ print tb.item
629</pre> 629</pre>
630</YueDisplay> 630</YueDisplay>
631 631
632* **元表解构** 632* **元表解构**
633使用被 **<>** 包围的元方法键解构元表。 633使用被 **<>** 包围的元方法键解构元表。
634 634
635```moonscript 635```moonscript
@@ -680,7 +680,7 @@ with? io.open "test.txt", "w"
680 680
681### 管道 681### 管道
682 682
683与其使用一系列嵌套的函数调用,还可以考虑使用运算符 **|>** 来传递值。 683与其使用一系列嵌套的函数调用,还可以考虑使用运算符 **|>** 来传递值。
684 684
685```moonscript 685```moonscript
686"你好" |> print 686"你好" |> print
@@ -731,7 +731,7 @@ a ??= false
731 731
732### 隐式对象 732### 隐式对象
733 733
734您可以在表格块内使用符号 **\*** 开始编写一系列隐式结构。如果您正在创建隐式对象,对象的字段必须具有相同的缩进。 734你可以在表格块内使用符号 **\*** 开始编写一系列隐式结构。如果你正在创建隐式对象,对象的字段必须具有相同的缩进。
735```moonscript 735```moonscript
736list = 736list =
737 * 1 737 * 1
@@ -859,7 +859,7 @@ do
859 859
860导出语句提供了一种简洁的方式来定义当前的模块。 860导出语句提供了一种简洁的方式来定义当前的模块。
861 861
862* **命名导出** 862* **命名导出**
863带命名的导出将定义一个局部变量,并在导出的表中添加一个同名的字段。 863带命名的导出将定义一个局部变量,并在导出的表中添加一个同名的字段。
864 864
865```moonscript 865```moonscript
@@ -923,7 +923,7 @@ export["a-b-c"] = 123
923</pre> 923</pre>
924</YueDisplay> 924</YueDisplay>
925 925
926* **未命名导出** 926* **未命名导出**
927未命名导出会将要导出的目标项目添加到导出表的数组部分。 927未命名导出会将要导出的目标项目添加到导出表的数组部分。
928 928
929```moonscript 929```moonscript
@@ -953,7 +953,7 @@ export with tmp
953</pre> 953</pre>
954</YueDisplay> 954</YueDisplay>
955 955
956* **默认导出** 956* **默认导出**
957在导出语句中使用 **default** 关键字,来替换导出的表为一个目标的对象。 957在导出语句中使用 **default** 关键字,来替换导出的表为一个目标的对象。
958 958
959```moonscript 959```moonscript
@@ -1223,7 +1223,7 @@ print first, second, color
1223</pre> 1223</pre>
1224</YueDisplay> 1224</YueDisplay>
1225 1225
1226在进行解构时,可以指定默认值,如: 1226在进行解构时,可以指定默认值,如:
1227 1227
1228```moonscript 1228```moonscript
1229{:name = "nameless", :job = "jobless"} = person 1229{:name = "nameless", :job = "jobless"} = person
@@ -1234,7 +1234,7 @@ print first, second, color
1234</pre> 1234</pre>
1235</YueDisplay> 1235</YueDisplay>
1236 1236
1237在进行列表解构时,可以使用`_`作为占位符: 1237在进行列表解构时,可以使用`_`作为占位符:
1238 1238
1239```moonscript 1239```moonscript
1240[_, two, _, four] = items 1240[_, two, _, four] = items
@@ -1322,7 +1322,7 @@ print "好的"
1322 1322
1323### While 赋值 1323### While 赋值
1324 1324
1325可以在 while 循环中同样使用赋值来获取循环条件的值。 1325可以在 while 循环中同样使用赋值来获取循环条件的值。
1326```moonscript 1326```moonscript
1327while byte := stream\read_one! 1327while byte := stream\read_one!
1328 -- 对 byte 做一些操作 1328 -- 对 byte 做一些操作
@@ -1338,7 +1338,7 @@ while byte := stream\read_one!
1338 1338
1339## 可变参数赋值 1339## 可变参数赋值
1340 1340
1341可以将函数返回的结果赋值给一个可变参数符号 `...`。然后使用Lua的方式访问其内容。 1341可以将函数返回的结果赋值给一个可变参数符号 `...`。然后使用Lua的方式访问其内容。
1342```moonscript 1342```moonscript
1343list = [1, 2, 3, 4, 5] 1343list = [1, 2, 3, 4, 5]
1344fn = (ok) -> ok, table.unpack list 1344fn = (ok) -> ok, table.unpack list
@@ -1360,7 +1360,7 @@ print ok, count, first
1360 1360
1361## 空白 1361## 空白
1362 1362
1363月之脚本是一个对空白敏感的语言。您必须在相同的缩进中使用空格 **' '** 或制表符 **'\t'** 来编写一些代码块,如函数体、值列表和一些控制块。包含不同空白的表达式可能意味着不同的事情。制表符被视为4个空格,但最好不要混合使用空格和制表符。 1363月之脚本是一个对空白敏感的语言。你必须在相同的缩进中使用空格 **' '** 或制表符 **'\t'** 来编写一些代码块,如函数体、值列表和一些控制块。包含不同空白的表达式可能意味着不同的事情。制表符被视为4个空格,但最好不要混合使用空格和制表符。
1364 1364
1365### 多行链式调用 1365### 多行链式调用
1366 1366
@@ -1529,7 +1529,7 @@ print "我有#{math.random! * 100}%的把握。"
1529 1529
1530### 数字字面量 1530### 数字字面量
1531 1531
1532可以在数字字面量中使用下划线来增加可读性。 1532可以在数字字面量中使用下划线来增加可读性。
1533 1533
1534```moonscript 1534```moonscript
1535integer = 1_000_000 1535integer = 1_000_000
@@ -1644,7 +1644,7 @@ print "数字的和是", sum 10, 20
1644</pre> 1644</pre>
1645</YueDisplay> 1645</YueDisplay>
1646 1646
1647如果需要做显式返回,可以使用return关键字: 1647如果需要做显式返回,可以使用return关键字:
1648 1648
1649```moonscript 1649```moonscript
1650sum = (x, y) -> return x + y 1650sum = (x, y) -> return x + y
@@ -1850,7 +1850,7 @@ print @value
1850</pre> 1850</pre>
1851</YueDisplay> 1851</YueDisplay>
1852 1852
1853可以通过一个占位符指定回调函数的传参位置。 1853可以通过一个占位符指定回调函数的传参位置。
1854 1854
1855```moonscript 1855```moonscript
1856(x) <- map _, [1, 2, 3] 1856(x) <- map _, [1, 2, 3]
@@ -1863,7 +1863,7 @@ x * 2
1863</pre> 1863</pre>
1864</YueDisplay> 1864</YueDisplay>
1865 1865
1866如果您希望在反向回调处理后继续编写更多其它的代码,您可以使用do语句将不归属反向回调的代码分开。 1866如果你希望在反向回调处理后继续编写更多其它的代码,你可以使用do语句将不归属反向回调的代码分开。
1867 1867
1868```moonscript 1868```moonscript
1869result, msg = do 1869result, msg = do
@@ -2037,7 +2037,7 @@ list_with_one_element = [ 1, ]
2037 2037
2038## 推导式 2038## 推导式
2039 2039
2040推导式为我们提供了一种便捷的语法,通过遍历现有对象并对其值应用表达式来构造出新的表格。月之脚本有两种推导式:列表推导式和表格推导式。它们最终都是产生Lua表格;列表推导式将值累积到类似数组的表格中,而表格推导式允许您在每次遍历时设置新表格的键和值。 2040推导式为我们提供了一种便捷的语法,通过遍历现有对象并对其值应用表达式来构造出新的表格。月之脚本有两种推导式:列表推导式和表格推导式。它们最终都是产生Lua表格;列表推导式将值累积到类似数组的表格中,而表格推导式允许你在每次遍历时设置新表格的键和值。
2041 2041
2042### 列表推导式 2042### 列表推导式
2043 2043
@@ -2286,7 +2286,7 @@ doubled_evens = for i = 1, 20
2286</pre> 2286</pre>
2287</YueDisplay> 2287</YueDisplay>
2288 2288
2289还可以结合for循环表达式与continue语句来过滤值。 2289还可以结合for循环表达式与continue语句来过滤值。
2290 2290
2291注意出现在函数体末尾的for循环,不会被当作是一个表达式,并将循环结果累积到一个列表中作为返回值(相反,函数将返回nil)。如果要函数末尾的循环转换为列表表达式,可以使用返回语句加for循环表达式。 2291注意出现在函数体末尾的for循环,不会被当作是一个表达式,并将循环结果累积到一个列表中作为返回值(相反,函数将返回nil)。如果要函数末尾的循环转换为列表表达式,可以使用返回语句加for循环表达式。
2292 2292
@@ -2513,7 +2513,7 @@ print "你真幸运!" unless math.random! > 0.1
2513 2513
2514### 范围表达式 2514### 范围表达式
2515 2515
2516可以使用范围表达式来编写进行范围检查的代码。 2516可以使用范围表达式来编写进行范围检查的代码。
2517 2517
2518```moonscript 2518```moonscript
2519a = 5 2519a = 5
@@ -2684,7 +2684,7 @@ else
2684</pre> 2684</pre>
2685</YueDisplay> 2685</YueDisplay>
2686 2686
2687值得注意的是,在生成Lua代码时,我们要做检查的目标变量会放在==表达式的右侧。当您希望给when子句的比较对象定义一个\_\_eq元方法来重载判断逻辑时,可能会有用。 2687值得注意的是,在生成Lua代码时,我们要做检查的目标变量会放在==表达式的右侧。当你希望给when子句的比较对象定义一个\_\_eq元方法来重载判断逻辑时,可能会有用。
2688 2688
2689### 表格匹配 2689### 表格匹配
2690 2690
@@ -2744,9 +2744,105 @@ switch item
2744</pre> 2744</pre>
2745</YueDisplay> 2745</YueDisplay>
2746 2746
2747你也可以匹配数组元素、表格字段,甚至使用数组或表格字面量来匹配嵌套的结构。
2748
2749匹配数组元素。
2750
2751```moonscript
2752switch tb
2753 when [1, 2, 3]
2754 print "1, 2, 3"
2755 when [1, b, 3]
2756 print "1, #{b}, 3"
2757 when [1, 2, b = 3] -- 变量b有默认值
2758 print "1, 2, #{b}"
2759```
2760<YueDisplay>
2761<pre>
2762switch tb
2763 when [1, 2, 3]
2764 print "1, 2, 3"
2765 when [1, b, 3]
2766 print "1, #{b}, 3"
2767 when [1, 2, b = 3] -- 变量b有默认值
2768 print "1, 2, #{b}"
2769</pre>
2770</YueDisplay>
2771
2772匹配表格字段。
2773
2774```moonscript
2775switch tb
2776 when success: true, :result
2777 print "成功", result
2778 when success: false
2779 print "失败", result
2780 else
2781 print "无效值"
2782```
2783<YueDisplay>
2784<pre>
2785switch tb
2786 when success: true, :result
2787 print "成功", result
2788 when success: false
2789 print "失败", result
2790 else
2791 print "无效值"
2792</pre>
2793</YueDisplay>
2794
2795匹配嵌套的表格结构。
2796
2797```moonscript
2798switch tb
2799 when data: {type: "success", :content}
2800 print "成功", content
2801 when data: {type: "error", :content}
2802 print "失败", content
2803 else
2804 print "无效值"
2805```
2806<YueDisplay>
2807<pre>
2808switch tb
2809 when data: {type: "success", :content}
2810 print "成功", content
2811 when data: {type: "error", :content}
2812 print "失败", content
2813 else
2814 print "无效值"
2815</pre>
2816</YueDisplay>
2817
2818匹配表格数组。
2819
2820```moonscript
2821switch tb
2822 when [
2823 {a: 1, b: 2}
2824 {a: 3, b: 4}
2825 {a: 5, b: 6}
2826 fourth
2827 ]
2828 print "匹配成功", fourth
2829```
2830<YueDisplay>
2831<pre>
2832switch tb
2833 when [
2834 {a: 1, b: 2}
2835 {a: 3, b: 4}
2836 {a: 5, b: 6}
2837 fourth
2838 ]
2839 print "匹配成功", fourth
2840</pre>
2841</YueDisplay>
2842
2747## 面向对象编程 2843## 面向对象编程
2748 2844
2749在以下的示例中,月之脚本生成的Lua代码可能看起来会很复杂。所以最好主要关注月之脚本代码层面的意义,然后如果您想知道关于面向对象功能的实现细节,再查看Lua代码。 2845在以下的示例中,月之脚本生成的Lua代码可能看起来会很复杂。所以最好主要关注月之脚本代码层面的意义,然后如果你想知道关于面向对象功能的实现细节,再查看Lua代码。
2750 2846
2751一个简单的类: 2847一个简单的类:
2752 2848
@@ -3214,7 +3310,7 @@ x = class
3214 3310
3215### 类混合 3311### 类混合
3216 3312
3217您可以通过使用 `using` 关键字来实现类混合。这意味着您可以从一个普通 Lua 表格或已定义的类对象中,复制函数到您创建的新类中。当您使用普通 Lua 表格进行类混合时,您有机会用自己的实现来重写类的索引方法(例如元方法 `__index`)。然而,当您从一个类对象做混合时,需要注意的是该类对象的元方法将不会被复制到新类。 3313你可以通过使用 `using` 关键字来实现类混合。这意味着你可以从一个普通 Lua 表格或已定义的类对象中,复制函数到你创建的新类中。当你使用普通 Lua 表格进行类混合时,你有机会用自己的实现来重写类的索引方法(例如元方法 `__index`)。然而,当你从一个类对象做混合时,需要注意的是该类对象的元方法将不会被复制到新类。
3218 3314
3219```moonscript 3315```moonscript
3220MyIndex = __index: var: 1 3316MyIndex = __index: var: 1
@@ -3316,7 +3412,7 @@ me = create_person "Leaf", [dad, mother, sister]
3316 3412
3317在此用法中,with可以被视为K组合子(k-combinator)的一种特殊形式。 3413在此用法中,with可以被视为K组合子(k-combinator)的一种特殊形式。
3318 3414
3319如果给表达式另外起一个名称的话,with语句中的表达式也可以是一个赋值语句。 3415如果想给表达式另外起一个名称的话,with语句中的表达式也可以是一个赋值语句。
3320 3416
3321```moonscript 3417```moonscript
3322with str = "你好" 3418with str = "你好"
@@ -3373,7 +3469,7 @@ print var -- 这里是nil
3373</pre> 3469</pre>
3374</YueDisplay> 3470</YueDisplay>
3375 3471
3376月之脚本的 **do** 也可以用作表达式。允许您将多行代码的处理合并为一个表达式,并将do语句代码块的最后一个语句作为表达式返回的结果。 3472月之脚本的 **do** 也可以用作表达式。允许你将多行代码的处理合并为一个表达式,并将do语句代码块的最后一个语句作为表达式返回的结果。
3377 3473
3378```moonscript 3474```moonscript
3379counter = do 3475counter = do
@@ -4332,8 +4428,8 @@ simplified: boolean
4332 4428
4333版权 (c) 2017-2025 李瑾 \<dragon-fly@qq.com\> 4429版权 (c) 2017-2025 李瑾 \<dragon-fly@qq.com\>
4334 4430
4335特此免费授予任何获得本软件副本和相关文档文件(下称“软件”)的人不受限制地处置该软件的权利,包括不受限制地使用、复制、修改、合并、发布、分发、转授许可和/或出售该软件副本,以及再授权被配发了本软件的人如上的权利,须在下列条件下: 4431特此免费授予任何获得本软件副本和相关文档文件(下称“软件”)的人不受限制地处置该软件的权利,包括不受限制地使用、复制、修改、合并、发布、分发、转授许可和/或出售该软件副本,以及再授权被配发了本软件的人如上的权利,须在下列条件下:
4336上述版权声明和本许可声明应包含在该软件的所有副本或实质成分中。 4432上述版权声明和本许可声明应包含在该软件的所有副本或实质成分中。
4337本软件是“如此”提供的,没有任何形式的明示或暗示的保证,包括但不限于对适销性、特定用途的适用性和不侵权的保证。在任何情况下,作者或版权持有人都不对任何索赔、损害或其他责任负责,无论这些追责来自合同、侵权或其它行为中,还是产生于、源于或有关于本软件以及本软件的使用或其它处置。 4433本软件是“如此”提供的,没有任何形式的明示或暗示的保证,包括但不限于对适销性、特定用途的适用性和不侵权的保证。在任何情况下,作者或版权持有人都不对任何索赔、损害或其他责任负责,无论这些追责来自合同、侵权或其它行为中,还是产生于、源于或有关于本软件以及本软件的使用或其它处置。
4338 4434
4339<CompilerModal /> 4435<CompilerModal />