aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2025-05-26 15:52:07 +0800
committerLi Jin <dragon-fly@qq.com>2025-05-26 15:52:07 +0800
commita9d28cb615d2bdc09d2482d5262951f2afc8d6e1 (patch)
tree0567dd183518ce5df596875b8267215ecf5a25ce
parent4f301523563e80e838490cc6216a16bb3b8d6122 (diff)
downloadyuescript-a9d28cb615d2bdc09d2482d5262951f2afc8d6e1.tar.gz
yuescript-a9d28cb615d2bdc09d2482d5262951f2afc8d6e1.tar.bz2
yuescript-a9d28cb615d2bdc09d2482d5262951f2afc8d6e1.zip
Added doc for `with?` syntax.
-rwxr-xr-xdoc/docs/doc/README.md14
-rwxr-xr-xdoc/docs/zh/doc/README.md15
-rw-r--r--spec/inputs/with.yue5
-rw-r--r--spec/outputs/codes_from_doc.lua8
-rw-r--r--spec/outputs/codes_from_doc_zh.lua8
-rw-r--r--spec/outputs/with.lua16
6 files changed, 64 insertions, 2 deletions
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md
index 7e9b454..a970baa 100755
--- a/doc/docs/doc/README.md
+++ b/doc/docs/doc/README.md
@@ -3538,7 +3538,7 @@ with str := "Hello"
3538</pre> 3538</pre>
3539</YueDisplay> 3539</YueDisplay>
3540 3540
3541Accessing special keys with `[]` in a `with` statement. 3541You can access special keys with `[]` in a `with` statement.
3542 3542
3543```moonscript 3543```moonscript
3544with tb 3544with tb
@@ -3561,6 +3561,18 @@ with tb
3561</pre> 3561</pre>
3562</YueDisplay> 3562</YueDisplay>
3563 3563
3564`with?` is an enhanced version of `with` syntax, which introduces an existential check to safely access objects that may be nil without explicit null checks.
3565
3566```moonscript
3567with? obj
3568 print obj.name
3569```
3570<YueDisplay>
3571<pre>
3572with? obj
3573 print obj.name
3574</pre>
3575</YueDisplay>
3564 3576
3565## Do 3577## Do
3566 3578
diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md
index da90fa6..2d4af18 100755
--- a/doc/docs/zh/doc/README.md
+++ b/doc/docs/zh/doc/README.md
@@ -3494,7 +3494,7 @@ with str := "你好"
3494</pre> 3494</pre>
3495</YueDisplay> 3495</YueDisplay>
3496 3496
3497在with语句中用`[]`访问特殊键。 3497 `with` 语句中使用 `[]` 访问特殊键。
3498 3498
3499```moonscript 3499```moonscript
3500with tb 3500with tb
@@ -3517,6 +3517,19 @@ with tb
3517</pre> 3517</pre>
3518</YueDisplay> 3518</YueDisplay>
3519 3519
3520`with?` 是 `with` 语法的一个增强版本,引入了存在性检查,用于在不显式判空的情况下安全访问可能为 nil 的对象。
3521
3522```moonscript
3523with? obj
3524 print obj.name
3525```
3526<YueDisplay>
3527<pre>
3528with? obj
3529 print obj.name
3530</pre>
3531</YueDisplay>
3532
3520## do 语句 3533## do 语句
3521 3534
3522当用作语句时,do语句的作用就像在Lua中差不多。 3535当用作语句时,do语句的作用就像在Lua中差不多。
diff --git a/spec/inputs/with.yue b/spec/inputs/with.yue
index c1cf3e3..2256833 100644
--- a/spec/inputs/with.yue
+++ b/spec/inputs/with.yue
@@ -165,4 +165,9 @@ do
165 break with? tb 165 break with? tb
166 break 1 166 break 1
167 167
168do
169 a = for i = 1, 100
170 with? x := tb[i]
171 break x if .id := 1
172
168nil 173nil
diff --git a/spec/outputs/codes_from_doc.lua b/spec/outputs/codes_from_doc.lua
index d857dec..55be7df 100644
--- a/spec/outputs/codes_from_doc.lua
+++ b/spec/outputs/codes_from_doc.lua
@@ -2147,6 +2147,10 @@ do
2147 _with_1["key-name"] = value 2147 _with_1["key-name"] = value
2148end 2148end
2149_with_0[#_with_0 + 1] = "abc" 2149_with_0[#_with_0 + 1] = "abc"
2150local _with_0 = obj
2151if _with_0 ~= nil then
2152 print(obj.name)
2153end
2150do 2154do
2151 local var = "hello" 2155 local var = "hello"
2152 print(var) 2156 print(var)
@@ -4363,6 +4367,10 @@ do
4363 _with_1["key-name"] = value 4367 _with_1["key-name"] = value
4364end 4368end
4365_with_0[#_with_0 + 1] = "abc" 4369_with_0[#_with_0 + 1] = "abc"
4370local _with_0 = obj
4371if _with_0 ~= nil then
4372 print(obj.name)
4373end
4366do 4374do
4367 local var = "hello" 4375 local var = "hello"
4368 print(var) 4376 print(var)
diff --git a/spec/outputs/codes_from_doc_zh.lua b/spec/outputs/codes_from_doc_zh.lua
index ecab077..399f8c1 100644
--- a/spec/outputs/codes_from_doc_zh.lua
+++ b/spec/outputs/codes_from_doc_zh.lua
@@ -2141,6 +2141,10 @@ do
2141 _with_1["key-name"] = value 2141 _with_1["key-name"] = value
2142end 2142end
2143_with_0[#_with_0 + 1] = "abc" 2143_with_0[#_with_0 + 1] = "abc"
2144local _with_0 = obj
2145if _with_0 ~= nil then
2146 print(obj.name)
2147end
2144do 2148do
2145 local var = "hello" 2149 local var = "hello"
2146 print(var) 2150 print(var)
@@ -4351,6 +4355,10 @@ do
4351 _with_1["key-name"] = value 4355 _with_1["key-name"] = value
4352end 4356end
4353_with_0[#_with_0 + 1] = "abc" 4357_with_0[#_with_0 + 1] = "abc"
4358local _with_0 = obj
4359if _with_0 ~= nil then
4360 print(obj.name)
4361end
4354do 4362do
4355 local var = "hello" 4363 local var = "hello"
4356 print(var) 4364 print(var)
diff --git a/spec/outputs/with.lua b/spec/outputs/with.lua
index 867d1b5..338d51f 100644
--- a/spec/outputs/with.lua
+++ b/spec/outputs/with.lua
@@ -234,4 +234,20 @@ do
234 end 234 end
235 a = _accum_0 235 a = _accum_0
236end 236end
237do
238 local a
239 local _accum_0
240 for i = 1, 100 do
241 local x = tb[i]
242 if x ~= nil then
243 local _des_0 = 1
244 if _des_0 then
245 x.id = _des_0
246 _accum_0 = x
247 break
248 end
249 end
250 end
251 a = _accum_0
252end
237return nil 253return nil