diff options
| author | Li Jin <dragon-fly@qq.com> | 2025-05-26 15:52:07 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2025-05-26 15:52:07 +0800 |
| commit | a9d28cb615d2bdc09d2482d5262951f2afc8d6e1 (patch) | |
| tree | 0567dd183518ce5df596875b8267215ecf5a25ce | |
| parent | 4f301523563e80e838490cc6216a16bb3b8d6122 (diff) | |
| download | yuescript-a9d28cb615d2bdc09d2482d5262951f2afc8d6e1.tar.gz yuescript-a9d28cb615d2bdc09d2482d5262951f2afc8d6e1.tar.bz2 yuescript-a9d28cb615d2bdc09d2482d5262951f2afc8d6e1.zip | |
Added doc for `with?` syntax.
| -rwxr-xr-x | doc/docs/doc/README.md | 14 | ||||
| -rwxr-xr-x | doc/docs/zh/doc/README.md | 15 | ||||
| -rw-r--r-- | spec/inputs/with.yue | 5 | ||||
| -rw-r--r-- | spec/outputs/codes_from_doc.lua | 8 | ||||
| -rw-r--r-- | spec/outputs/codes_from_doc_zh.lua | 8 | ||||
| -rw-r--r-- | spec/outputs/with.lua | 16 |
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 | ||
| 3541 | Accessing special keys with `[]` in a `with` statement. | 3541 | You can access special keys with `[]` in a `with` statement. |
| 3542 | 3542 | ||
| 3543 | ```moonscript | 3543 | ```moonscript |
| 3544 | with tb | 3544 | with 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 | ||
| 3567 | with? obj | ||
| 3568 | print obj.name | ||
| 3569 | ``` | ||
| 3570 | <YueDisplay> | ||
| 3571 | <pre> | ||
| 3572 | with? 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 |
| 3500 | with tb | 3500 | with tb |
| @@ -3517,6 +3517,19 @@ with tb | |||
| 3517 | </pre> | 3517 | </pre> |
| 3518 | </YueDisplay> | 3518 | </YueDisplay> |
| 3519 | 3519 | ||
| 3520 | `with?` 是 `with` 语法的一个增强版本,引入了存在性检查,用于在不显式判空的情况下安全访问可能为 nil 的对象。 | ||
| 3521 | |||
| 3522 | ```moonscript | ||
| 3523 | with? obj | ||
| 3524 | print obj.name | ||
| 3525 | ``` | ||
| 3526 | <YueDisplay> | ||
| 3527 | <pre> | ||
| 3528 | with? 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 | ||
| 168 | do | ||
| 169 | a = for i = 1, 100 | ||
| 170 | with? x := tb[i] | ||
| 171 | break x if .id := 1 | ||
| 172 | |||
| 168 | nil | 173 | nil |
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 |
| 2148 | end | 2148 | end |
| 2149 | _with_0[#_with_0 + 1] = "abc" | 2149 | _with_0[#_with_0 + 1] = "abc" |
| 2150 | local _with_0 = obj | ||
| 2151 | if _with_0 ~= nil then | ||
| 2152 | print(obj.name) | ||
| 2153 | end | ||
| 2150 | do | 2154 | do |
| 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 |
| 4364 | end | 4368 | end |
| 4365 | _with_0[#_with_0 + 1] = "abc" | 4369 | _with_0[#_with_0 + 1] = "abc" |
| 4370 | local _with_0 = obj | ||
| 4371 | if _with_0 ~= nil then | ||
| 4372 | print(obj.name) | ||
| 4373 | end | ||
| 4366 | do | 4374 | do |
| 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 |
| 2142 | end | 2142 | end |
| 2143 | _with_0[#_with_0 + 1] = "abc" | 2143 | _with_0[#_with_0 + 1] = "abc" |
| 2144 | local _with_0 = obj | ||
| 2145 | if _with_0 ~= nil then | ||
| 2146 | print(obj.name) | ||
| 2147 | end | ||
| 2144 | do | 2148 | do |
| 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 |
| 4352 | end | 4356 | end |
| 4353 | _with_0[#_with_0 + 1] = "abc" | 4357 | _with_0[#_with_0 + 1] = "abc" |
| 4358 | local _with_0 = obj | ||
| 4359 | if _with_0 ~= nil then | ||
| 4360 | print(obj.name) | ||
| 4361 | end | ||
| 4354 | do | 4362 | do |
| 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 |
| 236 | end | 236 | end |
| 237 | do | ||
| 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 | ||
| 252 | end | ||
| 237 | return nil | 253 | return nil |
