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/.vitepress/config.mts | 9 ++ doc/docs/de/doc/control-flow/conditionals.md | 58 +++++++++++++ doc/docs/de/doc/control-flow/goto.md | 106 ++++++++++++++++++++++++ doc/docs/doc/control-flow/conditionals.md | 58 +++++++++++++ doc/docs/doc/control-flow/goto.md | 106 ++++++++++++++++++++++++ doc/docs/id-id/doc/control-flow/conditionals.md | 58 +++++++++++++ doc/docs/id-id/doc/control-flow/goto.md | 106 ++++++++++++++++++++++++ doc/docs/pt-br/doc/control-flow/conditionals.md | 58 +++++++++++++ doc/docs/pt-br/doc/control-flow/goto.md | 106 ++++++++++++++++++++++++ doc/docs/zh/doc/control-flow/conditionals.md | 58 +++++++++++++ doc/docs/zh/doc/control-flow/goto.md | 106 ++++++++++++++++++++++++ 11 files changed, 829 insertions(+) create mode 100644 doc/docs/de/doc/control-flow/goto.md create mode 100644 doc/docs/doc/control-flow/goto.md create mode 100644 doc/docs/id-id/doc/control-flow/goto.md create mode 100644 doc/docs/pt-br/doc/control-flow/goto.md create mode 100644 doc/docs/zh/doc/control-flow/goto.md (limited to 'doc/docs') diff --git a/doc/docs/.vitepress/config.mts b/doc/docs/.vitepress/config.mts index 635d087..872791c 100644 --- a/doc/docs/.vitepress/config.mts +++ b/doc/docs/.vitepress/config.mts @@ -55,6 +55,7 @@ const sidebarText = { whileLoop: "While-Schleife", continueStatement: "Continue-Anweisung", conditionals: "Bedingungen", + gotoStatement: "Goto und Labels", lineDecorators: "Zeilen-Dekoratoren", switch: "Switch", objectOrientedProgramming: "Objektorientierte Programmierung", @@ -89,6 +90,7 @@ const sidebarText = { whileLoop: "While Loop", continueStatement: "Continue Statement", conditionals: "Conditionals", + gotoStatement: "Goto and Labels", lineDecorators: "Line Decorators", switch: "Switch", objectOrientedProgramming: "Object Oriented Programming", @@ -123,6 +125,7 @@ const sidebarText = { whileLoop: "Perulangan While", continueStatement: "Pernyataan Lanjutkan", conditionals: "Percabangan", + gotoStatement: "Goto dan Label", lineDecorators: "Dekorator Baris", switch: "Switch", objectOrientedProgramming: "Pemrograman Berorientasi Objek", @@ -157,6 +160,7 @@ const sidebarText = { whileLoop: "Laço while", continueStatement: "Instrução continue", conditionals: "Condicionais", + gotoStatement: "Goto e Rótulos", lineDecorators: "Decoradores de linha", switch: "Switch", objectOrientedProgramming: "Programação orientada a objetos", @@ -191,6 +195,7 @@ const sidebarText = { whileLoop: "while 循环", continueStatement: "continue 语句", conditionals: "条件语句", + gotoStatement: "goto 语句与标签", lineDecorators: "代码行修饰符", switch: "switch 语句", objectOrientedProgramming: "面向对象编程", @@ -363,6 +368,10 @@ function createSidebar(basePath: string, locale: SidebarLocale) { text: text.continueStatement, link: `${basePath}/control-flow/continue`, }, + { + text: text.gotoStatement, + link: `${basePath}/control-flow/goto`, + }, { text: text.switch, link: `${basePath}/control-flow/switch` }, ], }, diff --git a/doc/docs/de/doc/control-flow/conditionals.md b/doc/docs/de/doc/control-flow/conditionals.md index 56663d1..0d2574d 100644 --- a/doc/docs/de/doc/control-flow/conditionals.md +++ b/doc/docs/de/doc/control-flow/conditionals.md @@ -143,3 +143,61 @@ if a in list ``` + +Der `in`-Operator kann auch mit Tabellen verwendet werden und unterstützt die Variante `not in` für Verneinungen: + +```yuescript +has = "foo" in {"bar", "foo"} + +if a in {1, 2, 3} + print "a ist in der Tabelle" + +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 ist in der Tabelle" + +not_exist = item not in list + +check = -> value not in table +``` + + + +Eine Ein-Element-Liste oder Tabelle prüft auf Gleichheit mit diesem Element: + +```yuescript +-- [1,] prüft, ob wert == 1 +c = a in [1,] + +-- {1} prüft auch, ob wert == 1 +c = a in {1} + +-- Ohne Komma ist [1] ein Indexzugriff (tb[1]) +with tb + c = a in [1] +``` + + + +```yue +-- [1,] prüft, ob wert == 1 +c = a in [1,] + +-- {1} prüft auch, ob wert == 1 +c = a in {1} + +-- Ohne Komma ist [1] ein Indexzugriff (tb[1]) +with tb + c = a in [1] +``` + + diff --git a/doc/docs/de/doc/control-flow/goto.md b/doc/docs/de/doc/control-flow/goto.md new file mode 100644 index 0000000..0e2a415 --- /dev/null +++ b/doc/docs/de/doc/control-flow/goto.md @@ -0,0 +1,106 @@ +# Goto + +YueScript unterstützt die goto-Anweisung und die Label-Syntax zur Steuerung des Programmflusses, wobei die gleichen Regeln wie bei Luas goto-Anweisung gelten. **Hinweis:** Die goto-Anweisung erfordert Lua 5.2 oder höher. Beim Kompilieren zu Lua 5.1 führt die Verwendung der goto-Syntax zu einem Kompilierfehler. + +Ein Label wird mit doppelten Doppelpunkten definiert: + +```yuescript +::start:: +::done:: +::mein_label:: +``` + + + +```yue +::start:: +::done:: +::mein_label:: +``` + + + +Die goto-Anweisung springt zu einem angegebenen Label: + +```yuescript +a = 0 +::start:: +a += 1 +goto done if a == 5 +goto start +::done:: +print "a ist jetzt 5" +``` + + + +```yue +a = 0 +::start:: +a += 1 +goto done if a == 5 +goto start +::done:: +print "a ist jetzt 5" +``` + + + +Die goto-Anweisung ist nützlich, um aus tief verschachtelten Schleifen zu springen: + +```yuescript +for z = 1, 10 + for y = 1, 10 do for x = 1, 10 + if x^2 + y^2 == z^2 + print 'Pythagoreisches Tripel gefunden:', x, y, z + goto ok +::ok:: +``` + + + +```yue +for z = 1, 10 + for y = 1, 10 do for x = 1, 10 + if x^2 + y^2 == z^2 + print 'Pythagoreisches Tripel gefunden:', x, y, z + goto ok +::ok:: +``` + + + +Sie können auch Labels verwenden, um zu einer bestimmten Schleifenebene zu springen: + +```yuescript +for z = 1, 10 + for y = 1, 10 + for x = 1, 10 + if x^2 + y^2 == z^2 + print 'Pythagoreisches Tripel gefunden:', x, y, z + print 'versuche nächstes z...' + goto zcontinue + ::zcontinue:: +``` + + + +```yue +for z = 1, 10 + for y = 1, 10 + for x = 1, 10 + if x^2 + y^2 == z^2 + print 'Pythagoreisches Tripel gefunden:', x, y, z + print 'versuche nächstes z...' + goto zcontinue + ::zcontinue:: +``` + + + +## Hinweise + +- Labels müssen innerhalb ihres Geltungsbereichs eindeutig sein +- goto kann zu Labels auf derselben oder äußeren Geltungsbereichsebenen springen +- goto kann nicht in innere Geltungsbereiche springen (wie in Blöcke oder Schleifen) +- Verwenden Sie goto sparsam, da es den Code schwieriger zu lesen und zu warten machen kann diff --git a/doc/docs/doc/control-flow/conditionals.md b/doc/docs/doc/control-flow/conditionals.md index 1bcc838..ca29fda 100644 --- a/doc/docs/doc/control-flow/conditionals.md +++ b/doc/docs/doc/control-flow/conditionals.md @@ -143,3 +143,61 @@ if a in list ``` + +The `in` operator can also be used with tables and supports the `not in` variant for negation: + +```yuescript +has = "foo" in {"bar", "foo"} + +if a in {1, 2, 3} + print "a is in the table" + +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 is in the table" + +not_exist = item not in list + +check = -> value not in table +``` + + + +A single-element list or table checks for equality with that element: + +```yuescript +-- [1,] checks if value == 1 +c = a in [1,] + +-- {1} also checks if value == 1 +c = a in {1} + +-- Without comma, [1] is indexing (tb[1]) +with tb + c = a in [1] +``` + + + +```yue +-- [1,] checks if value == 1 +c = a in [1,] + +-- {1} also checks if value == 1 +c = a in {1} + +-- Without comma, [1] is indexing (tb[1]) +with tb + c = a in [1] +``` + + diff --git a/doc/docs/doc/control-flow/goto.md b/doc/docs/doc/control-flow/goto.md new file mode 100644 index 0000000..00227ba --- /dev/null +++ b/doc/docs/doc/control-flow/goto.md @@ -0,0 +1,106 @@ +# Goto + +YueScript supports goto statement and label syntax for controlling program flow, following the same rules as Lua's goto statement. **Note:** The goto statement requires Lua 5.2 or higher. When compiling to Lua 5.1, using goto syntax will result in a compilation error. + +A label is defined using double colons: + +```yuescript +::start:: +::done:: +::my_label:: +``` + + + +```yue +::start:: +::done:: +::my_label:: +``` + + + +The goto statement jumps to a specified label: + +```yuescript +a = 0 +::start:: +a += 1 +goto done if a == 5 +goto start +::done:: +print "a is now 5" +``` + + + +```yue +a = 0 +::start:: +a += 1 +goto done if a == 5 +goto start +::done:: +print "a is now 5" +``` + + + +The goto statement is useful for breaking out of deeply nested loops: + +```yuescript +for z = 1, 10 + for y = 1, 10 do for x = 1, 10 + if x^2 + y^2 == z^2 + print 'found a Pythagorean triple:', x, y, z + goto ok +::ok:: +``` + + + +```yue +for z = 1, 10 + for y = 1, 10 do for x = 1, 10 + if x^2 + y^2 == z^2 + print 'found a Pythagorean triple:', x, y, z + goto ok +::ok:: +``` + + + +You can also use labels to jump to a specific loop level: + +```yuescript +for z = 1, 10 + for y = 1, 10 + for x = 1, 10 + if x^2 + y^2 == z^2 + print 'found a Pythagorean triple:', x, y, z + print 'now trying next z...' + goto zcontinue + ::zcontinue:: +``` + + + +```yue +for z = 1, 10 + for y = 1, 10 + for x = 1, 10 + if x^2 + y^2 == z^2 + print 'found a Pythagorean triple:', x, y, z + print 'now trying next z...' + goto zcontinue + ::zcontinue:: +``` + + + +## Notes + +- Labels must be unique within their scope +- goto can jump to labels at the same or outer scope levels +- goto cannot jump into inner scopes (like inside blocks or loops) +- Use goto sparingly, as it can make code harder to read and maintain diff --git a/doc/docs/id-id/doc/control-flow/conditionals.md b/doc/docs/id-id/doc/control-flow/conditionals.md index 861eae6..db67a79 100644 --- a/doc/docs/id-id/doc/control-flow/conditionals.md +++ b/doc/docs/id-id/doc/control-flow/conditionals.md @@ -143,3 +143,61 @@ if a in list ``` + +Operator `in` juga dapat digunakan dengan tabel dan mendukung varian `not in` untuk negasi: + +```yuescript +has = "foo" in {"bar", "foo"} + +if a in {1, 2, 3} + print "a ada di dalam tabel" + +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 ada di dalam tabel" + +not_exist = item not in list + +check = -> value not in table +``` + + + +Daftar atau tabel dengan satu elemen memeriksa kesamaan dengan elemen tersebut: + +```yuescript +-- [1,] memeriksa apakah nilai == 1 +c = a in [1,] + +-- {1} juga memeriksa apakah nilai == 1 +c = a in {1} + +-- Tanpa koma, [1] adalah akses indeks (tb[1]) +with tb + c = a in [1] +``` + + + +```yue +-- [1,] memeriksa apakah nilai == 1 +c = a in [1,] + +-- {1} juga memeriksa apakah nilai == 1 +c = a in {1} + +-- Tanpa koma, [1] adalah akses indeks (tb[1]) +with tb + c = a in [1] +``` + + diff --git a/doc/docs/id-id/doc/control-flow/goto.md b/doc/docs/id-id/doc/control-flow/goto.md new file mode 100644 index 0000000..f387f0d --- /dev/null +++ b/doc/docs/id-id/doc/control-flow/goto.md @@ -0,0 +1,106 @@ +# Goto + +YueScript mendukung pernyataan goto dan sintaks label untuk mengontrol alur program, mengikuti aturan yang sama dengan pernyataan goto Lua. **Catatan:** Pernyataan goto memerlukan Lua 5.2 atau lebih tinggi. Saat mengompilasi ke Lua 5.1, penggunaan sintaks goto akan menyebabkan galat kompilasi. + +Label didefinisikan menggunakan dua titik dua: + +```yuescript +::mulai:: +::selesai:: +::label_saya:: +``` + + + +```yue +::mulai:: +::selesai:: +::label_saya:: +``` + + + +Pernyataan goto melompat ke label yang ditentukan: + +```yuescript +a = 0 +::mulai:: +a += 1 +goto selesai if a == 5 +goto mulai +::selesai:: +print "a sekarang 5" +``` + + + +```yue +a = 0 +::mulai:: +a += 1 +goto selesai if a == 5 +goto mulai +::selesai:: +print "a sekarang 5" +``` + + + +Pernyataan goto berguna untuk keluar dari loop yang bersarang dalam: + +```yuescript +for z = 1, 10 + for y = 1, 10 do for x = 1, 10 + if x^2 + y^2 == z^2 + print 'tripel Pythagorean ditemukan:', x, y, z + goto ok +::ok:: +``` + + + +```yue +for z = 1, 10 + for y = 1, 10 do for x = 1, 10 + if x^2 + y^2 == z^2 + print 'tripel Pythagorean ditemukan:', x, y, z + goto ok +::ok:: +``` + + + +Anda juga dapat menggunakan label untuk melompat ke tingkat loop tertentu: + +```yuescript +for z = 1, 10 + for y = 1, 10 + for x = 1, 10 + if x^2 + y^2 == z^2 + print 'tripel Pythagorean ditemukan:', x, y, z + print 'mencoba z berikutnya...' + goto zcontinue + ::zcontinue:: +``` + + + +```yue +for z = 1, 10 + for y = 1, 10 + for x = 1, 10 + if x^2 + y^2 == z^2 + print 'tripel Pythagorean ditemukan:', x, y, z + print 'mencoba z berikutnya...' + goto zcontinue + ::zcontinue:: +``` + + + +## Catatan + +- Label harus unik dalam cakupannya +- goto dapat melompat ke label pada tingkat cakupan yang sama atau luar +- goto tidak dapat melompat ke cakupan dalam (seperti di dalam blok atau loop) +- Gunakan goto dengan hemat, karena dapat membuat kode lebih sulit dibaca dan dipelihara diff --git a/doc/docs/pt-br/doc/control-flow/conditionals.md b/doc/docs/pt-br/doc/control-flow/conditionals.md index bc12d4e..ce32d89 100644 --- a/doc/docs/pt-br/doc/control-flow/conditionals.md +++ b/doc/docs/pt-br/doc/control-flow/conditionals.md @@ -143,3 +143,61 @@ if a in list ``` + +O operador `in` também pode ser usado com tabelas e suporta a variante `not in` para negação: + +```yuescript +has = "foo" in {"bar", "foo"} + +if a in {1, 2, 3} + print "a está na tabela" + +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 está na tabela" + +not_exist = item not in list + +check = -> value not in table +``` + + + +Uma lista ou tabela de único elemento verifica igualdade com esse elemento: + +```yuescript +-- [1,] verifica se valor == 1 +c = a in [1,] + +-- {1} também verifica se valor == 1 +c = a in {1} + +-- Sem vírgula, [1] é acesso por índice (tb[1]) +with tb + c = a in [1] +``` + + + +```yue +-- [1,] verifica se valor == 1 +c = a in [1,] + +-- {1} também verifica se valor == 1 +c = a in {1} + +-- Sem vírgula, [1] é acesso por índice (tb[1]) +with tb + c = a in [1] +``` + + diff --git a/doc/docs/pt-br/doc/control-flow/goto.md b/doc/docs/pt-br/doc/control-flow/goto.md new file mode 100644 index 0000000..f4c85f4 --- /dev/null +++ b/doc/docs/pt-br/doc/control-flow/goto.md @@ -0,0 +1,106 @@ +# Goto + +YueScript suporta a instrução goto e a sintaxe de rótulos para controlar o fluxo do programa, seguindo as mesmas regras da instrução goto do Lua. **Nota:** A instrução goto requer Lua 5.2 ou superior. Ao compilar para Lua 5.1, o uso da sintaxe goto resultará em um erro de compilação. + +Um rótulo é definido usando dois pontos duplos: + +```yuescript +::inicio:: +::fim:: +::meu_rotulo:: +``` + + + +```yue +::inicio:: +::fim:: +::meu_rotulo:: +``` + + + +A instrução goto salta para um rótulo especificado: + +```yuescript +a = 0 +::inicio:: +a += 1 +goto fim if a == 5 +goto inicio +::fim:: +print "a agora é 5" +``` + + + +```yue +a = 0 +::inicio:: +a += 1 +goto fim if a == 5 +goto inicio +::fim:: +print "a agora é 5" +``` + + + +A instrução goto é útil para sair de laços profundamente aninhados: + +```yuescript +for z = 1, 10 + for y = 1, 10 do for x = 1, 10 + if x^2 + y^2 == z^2 + print 'triplo pitagórico encontrado:', x, y, z + goto ok +::ok:: +``` + + + +```yue +for z = 1, 10 + for y = 1, 10 do for x = 1, 10 + if x^2 + y^2 == z^2 + print 'triplo pitagórico encontrado:', x, y, z + goto ok +::ok:: +``` + + + +Você também pode usar rótulos para pular para um nível específico de laço: + +```yuescript +for z = 1, 10 + for y = 1, 10 + for x = 1, 10 + if x^2 + y^2 == z^2 + print 'triplo pitagórico encontrado:', x, y, z + print 'tentando próximo z...' + goto zcontinue + ::zcontinue:: +``` + + + +```yue +for z = 1, 10 + for y = 1, 10 + for x = 1, 10 + if x^2 + y^2 == z^2 + print 'triplo pitagórico encontrado:', x, y, z + print 'tentando próximo z...' + goto zcontinue + ::zcontinue:: +``` + + + +## Notas + +- Rótulos devem ser únicos dentro de seu escopo +- goto pode pular para rótulos nos mesmos níveis de escopo ou externos +- goto não pode pular para escopos internos (como dentro de blocos ou laços) +- Use goto com moderação, pois pode tornar o código mais difícil de ler e manter 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