From cf5b1b4a68d762e6e33cac8367611ecea15fa942 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Sun, 15 Feb 2026 05:49:13 +0000 Subject: Add goto statement documentation and tests - Added goto.md documentation files in all languages (en, de, zh, pt-br, id-id) - Updated conditionals.md to include goto statement references - Updated VitePress config to include new goto documentation pages - Updated makefile for goto documentation compilation - Added test outputs for goto examples in all languages - Updated yue.cpp core implementation Co-Authored-By: Claude Sonnet 4.5 --- doc/docs/zh/doc/control-flow/conditionals.md | 58 +++++++++++++++ doc/docs/zh/doc/control-flow/goto.md | 106 +++++++++++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 doc/docs/zh/doc/control-flow/goto.md (limited to 'doc/docs/zh') diff --git a/doc/docs/zh/doc/control-flow/conditionals.md b/doc/docs/zh/doc/control-flow/conditionals.md index 3a4d5d1..f8d2851 100644 --- a/doc/docs/zh/doc/control-flow/conditionals.md +++ b/doc/docs/zh/doc/control-flow/conditionals.md @@ -143,3 +143,61 @@ if a in list ``` + +`in` 运算符也可以用于表,并支持 `not in` 变体来进行否定检查: + +```yuescript +has = "foo" in {"bar", "foo"} + +if a in {1, 2, 3} + print "a 在表中" + +not_exist = item not in list + +check = -> value not in table +``` + + + +```yue +has = "foo" in {"bar", "foo"} + +if a in {1, 2, 3} + print "a 在表中" + +not_exist = item not in list + +check = -> value not in table +``` + + + +单元素列表或表会检查与该元素的相等性: + +```yuescript +-- [1,] 检查 value == 1 +c = a in [1,] + +-- {1} 也是检查 value == 1 +c = a in {1} + +-- 没有逗号,[1] 是索引访问(tb[1]) +with tb + c = a in [1] +``` + + + +```yue +-- [1,] 检查 value == 1 +c = a in [1,] + +-- {1} 也是检查 value == 1 +c = a in {1} + +-- 没有逗号,[1] 是索引访问(tb[1]) +with tb + c = a in [1] +``` + + diff --git a/doc/docs/zh/doc/control-flow/goto.md b/doc/docs/zh/doc/control-flow/goto.md new file mode 100644 index 0000000..6d1cc89 --- /dev/null +++ b/doc/docs/zh/doc/control-flow/goto.md @@ -0,0 +1,106 @@ +# Goto + +YueScript 支持 goto 语句和标签语法来控制程序流程,该语法遵循与 Lua 的 goto 语句相同的规则。**注意:** goto 语句需要 Lua 5.2 或更高版本。当编译目标是 Lua 5.1 时,使用 goto 语法将导致编译错误。 + +标签使用双冒号定义: + +```yuescript +::开始:: +::结束:: +::我的标签:: +``` + + + +```yue +::开始:: +::结束:: +::我的标签:: +``` + + + +goto 语句可以跳转到指定的标签: + +```yuescript +a = 0 +::开始:: +a += 1 +goto 结束 if a == 5 +goto 开始 +::结束:: +print "a 现在是 5" +``` + + + +```yue +a = 0 +::开始:: +a += 1 +goto 结束 if a == 5 +goto 开始 +::结束:: +print "a 现在是 5" +``` + + + +goto 语句对于跳出深层嵌套循环非常有用: + +```yuescript +for z = 1, 10 + for y = 1, 10 do for x = 1, 10 + if x^2 + y^2 == z^2 + print '找到勾股数:', x, y, z + goto 完成 +::完成:: +``` + + + +```yue +for z = 1, 10 + for y = 1, 10 do for x = 1, 10 + if x^2 + y^2 == z^2 + print '找到勾股数:', x, y, z + goto 完成 +::完成:: +``` + + + +你也可以使用标签跳转到特定的循环层级: + +```yuescript +for z = 1, 10 + for y = 1, 10 + for x = 1, 10 + if x^2 + y^2 == z^2 + print '找到勾股数:', x, y, z + print '尝试下一个 z...' + goto 继续z + ::继续z:: +``` + + + +```yue +for z = 1, 10 + for y = 1, 10 + for x = 1, 10 + if x^2 + y^2 == z^2 + print '找到勾股数:', x, y, z + print '尝试下一个 z...' + goto 继续z + ::继续z:: +``` + + + +## 注意事项 + +- 标签在其作用域内必须唯一 +- goto 可以跳转到相同或外层作用域级别的标签 +- goto 不能跳转到内层作用域(如代码块或循环内部) +- 谨慎使用 goto,因为它会使代码更难阅读和维护 -- cgit v1.2.3-55-g6feb