From d63779ec0a6f00854f53b48b0f6a67707879b6d1 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 12 Feb 2026 10:29:07 +0800 Subject: Updated docs. --- doc/docs/.vitepress/config.mts | 7 +- doc/docs/de/doc/advanced/do.md | 22 ++- doc/docs/de/doc/advanced/module.md | 253 -------------------------- doc/docs/de/doc/control-flow/for-loop.md | 16 +- doc/docs/de/doc/control-flow/while-loop.md | 42 ++++- doc/docs/de/doc/language-basics/module.md | 253 ++++++++++++++++++++++++++ doc/docs/de/doc/objects/with-statement.md | 44 +++++ doc/docs/doc/advanced/do.md | 22 +++ doc/docs/doc/advanced/module.md | 253 -------------------------- doc/docs/doc/control-flow/for-loop.md | 18 +- doc/docs/doc/control-flow/while-loop.md | 42 ++++- doc/docs/doc/language-basics/module.md | 253 ++++++++++++++++++++++++++ doc/docs/doc/objects/with-statement.md | 44 +++++ doc/docs/id-id/doc/advanced/do.md | 22 ++- doc/docs/id-id/doc/advanced/module.md | 253 -------------------------- doc/docs/id-id/doc/control-flow/for-loop.md | 16 +- doc/docs/id-id/doc/control-flow/while-loop.md | 42 ++++- doc/docs/id-id/doc/language-basics/module.md | 253 ++++++++++++++++++++++++++ doc/docs/id-id/doc/objects/with-statement.md | 44 +++++ doc/docs/pt-br/doc/advanced/do.md | 22 ++- doc/docs/pt-br/doc/advanced/module.md | 253 -------------------------- doc/docs/pt-br/doc/control-flow/for-loop.md | 16 +- doc/docs/pt-br/doc/control-flow/while-loop.md | 42 ++++- doc/docs/pt-br/doc/language-basics/module.md | 253 ++++++++++++++++++++++++++ doc/docs/pt-br/doc/objects/with-statement.md | 44 +++++ doc/docs/zh/doc/advanced/do.md | 22 ++- doc/docs/zh/doc/advanced/module.md | 253 -------------------------- doc/docs/zh/doc/control-flow/for-loop.md | 16 +- doc/docs/zh/doc/control-flow/while-loop.md | 42 ++++- doc/docs/zh/doc/language-basics/module.md | 253 ++++++++++++++++++++++++++ doc/docs/zh/doc/objects/with-statement.md | 44 +++++ 31 files changed, 1879 insertions(+), 1280 deletions(-) delete mode 100644 doc/docs/de/doc/advanced/module.md create mode 100644 doc/docs/de/doc/language-basics/module.md delete mode 100644 doc/docs/doc/advanced/module.md create mode 100644 doc/docs/doc/language-basics/module.md delete mode 100644 doc/docs/id-id/doc/advanced/module.md create mode 100644 doc/docs/id-id/doc/language-basics/module.md delete mode 100644 doc/docs/pt-br/doc/advanced/module.md create mode 100644 doc/docs/pt-br/doc/language-basics/module.md delete mode 100644 doc/docs/zh/doc/advanced/module.md create mode 100644 doc/docs/zh/doc/language-basics/module.md (limited to 'doc') diff --git a/doc/docs/.vitepress/config.mts b/doc/docs/.vitepress/config.mts index cae1d89..c23301f 100644 --- a/doc/docs/.vitepress/config.mts +++ b/doc/docs/.vitepress/config.mts @@ -299,6 +299,7 @@ function createSidebar(basePath: string, locale: SidebarLocale) { text: text.attributes, link: `${basePath}/language-basics/attributes`, }, + { text: text.module, link: `${basePath}/language-basics/module` }, ], }, { @@ -389,7 +390,6 @@ function createSidebar(basePath: string, locale: SidebarLocale) { collapsed: true, items: [ { text: text.macro, link: `${basePath}/advanced/macro` }, - { text: text.module, link: `${basePath}/advanced/module` }, { text: text.lineDecorators, link: `${basePath}/advanced/line-decorators`, @@ -510,6 +510,11 @@ export default defineConfig({ document.head.appendChild(s); })();`, ], + [ + "style", + {}, + ".dark .vp-code span{color:var(--shiki-dark,inherit)}html:not(.dark) .vp-code span{color:var(--shiki-light,inherit)}", + ], ], vite: { publicDir: resolve(__dirname, "public"), diff --git a/doc/docs/de/doc/advanced/do.md b/doc/docs/de/doc/advanced/do.md index 265a5ba..c6c41cf 100644 --- a/doc/docs/de/doc/advanced/do.md +++ b/doc/docs/de/doc/advanced/do.md @@ -20,7 +20,27 @@ print var -- nil hier -YueScripts **do** kann auch als Ausdruck verwendet werden. So kannst du mehrere Zeilen in einem Ausdruck kombinieren. Das Ergebnis des `do`-Ausdrucks ist die letzte Anweisung im Block. +YueScripts **do** kann auch als Ausdruck verwendet werden. So kannst du mehrere Zeilen in einem Ausdruck kombinieren. Das Ergebnis des `do`-Ausdrucks ist die letzte Anweisung im Block. `do`-Ausdrücke unterstützen die Verwendung von `break`, um den Kontrollfluss zu unterbrechen und mehrere Rückgabewerte vorzeitig zurückzugeben. + +```yuescript +status, value = do + n = 12 + if n > 10 + break "large", n + break "small", n +``` + + + +```yue +status, value = do + n = 12 + if n > 10 + break "large", n + break "small", n +``` + + ```yuescript counter = do diff --git a/doc/docs/de/doc/advanced/module.md b/doc/docs/de/doc/advanced/module.md deleted file mode 100644 index f20deec..0000000 --- a/doc/docs/de/doc/advanced/module.md +++ /dev/null @@ -1,253 +0,0 @@ -# Module - -## Import - -Die `import`-Anweisung ist syntaktischer Zucker für `require` und hilft beim Extrahieren von Einträgen aus importierten Modulen. Importierte Elemente sind standardmäßig `const`. - -```yuescript --- als Tabellen-Destrukturierung -do - import insert, concat from table - -- Fehler beim Zuweisen zu insert, concat - import C, Ct, Cmt from require "lpeg" - -- Kurzform für implizites Require - import x, y, z from 'mymodule' - -- Import im Python-Stil - from 'module' import a, b, c - --- Kurzform zum Laden eines Moduls -do - import 'module' - import 'module_x' - import "d-a-s-h-e-s" - import "module.part" - --- Modul mit Alias oder Tabellen-Destrukturierung laden -do - import "player" as PlayerModule - import "lpeg" as :C, :Ct, :Cmt - import "export" as {one, two, Something:{umm:{ch}}} -``` - - - -```yue --- als Tabellen-Destrukturierung -do - import insert, concat from table - -- Fehler beim Zuweisen zu insert, concat - import C, Ct, Cmt from require "lpeg" - -- Kurzform für implizites Require - import x, y, z from 'mymodule' - -- Import im Python-Stil - from 'module' import a, b, c - --- Kurzform zum Laden eines Moduls -do - import 'module' - import 'module_x' - import "d-a-s-h-e-s" - import "module.part" - --- Modul mit Alias oder Tabellen-Destrukturierung laden -do - import "player" as PlayerModule - import "lpeg" as :C, :Ct, :Cmt - import "export" as {one, two, Something:{umm:{ch}}} -``` - - - -## Import von Globals - -Du kannst mit `import` bestimmte Globals in lokale Variablen importieren. Wenn du eine Kette von Globalzugriffen importierst, wird das letzte Feld der lokalen Variable zugewiesen. - -```yuescript -do - import tostring - import table.concat - print concat ["a", tostring 1] -``` - - - -```yue -do - import tostring - import table.concat - print concat ["a", tostring 1] -``` - - - -### Automatischer Global-Import - -Du kannst `import global` am Anfang eines Blocks platzieren, um automatisch alle Namen zu importieren, die im aktuellen Scope nicht explizit deklariert oder zugewiesen sind. Diese impliziten Importe werden als lokale `const` behandelt, die an die entsprechenden Globals zum Zeitpunkt der Anweisung gebunden sind. - -Namen, die im selben Scope explizit als `global` deklariert werden, werden nicht importiert, sodass du sie weiterhin zuweisen kannst. - -```yuescript -do - import global - print "hallo" - math.random 3 - -- print = nil -- Fehler: importierte Globals sind const - -do - -- explizite globale Variable wird nicht importiert - import global - global FLAG - print FLAG - FLAG = 123 -``` - - - -```yue -do - import global - print "hallo" - math.random 3 - -- print = nil -- Fehler: importierte Globals sind const - -do - -- explizite globale Variable wird nicht importiert - import global - global FLAG - print FLAG - FLAG = 123 -``` - - - -## Export - -Die `export`-Anweisung bietet eine knappe Möglichkeit, Module zu definieren. - -### Benannter Export - -Benannter Export definiert eine lokale Variable und fügt ein Feld in die exportierte Tabelle ein. - -```yuescript -export a, b, c = 1, 2, 3 -export cool = "Katze" - -export What = if this - "abc" -else - "def" - -export y = -> - hallo = 3434 - -export class Something - umm: "cool" -``` - - - -```yue -export a, b, c = 1, 2, 3 -export cool = "Katze" - -export What = if this - "abc" -else - "def" - -export y = -> - hallo = 3434 - -export class Something - umm: "cool" -``` - - - -Benannter Export mit Destructuring. - -```yuescript -export :loadstring, to_lua: tolua = yue -export {itemA: {:fieldA = 'default'}} = tb -``` - - - -```yue -export :loadstring, to_lua: tolua = yue -export {itemA: {:fieldA = 'default'}} = tb -``` - - - -Benannte Elemente aus dem Modul exportieren, ohne lokale Variablen zu erstellen. - -```yuescript -export.itemA = tb -export. = items -export["a-b-c"] = 123 -``` - - - -```yue -export.itemA = tb -export. = items -export["a-b-c"] = 123 -``` - - - -### Unbenannter Export - -Unbenannter Export fügt das Ziel-Element in den Array-Teil der exportierten Tabelle ein. - -```yuescript -d, e, f = 3, 2, 1 -export d, e, f - -export if this - 123 -else - 456 - -export with tmp - j = 2000 -``` - - - -```yue -d, e, f = 3, 2, 1 -export d, e, f - -export if this - 123 -else - 456 - -export with tmp - j = 2000 -``` - - - -### Default-Export - -Mit dem Schlüsselwort **default** in einer `export`-Anweisung wird die exportierte Tabelle durch ein beliebiges Objekt ersetzt. - -```yuescript -export default -> - print "hallo" - 123 -``` - - - -```yue -export default -> - print "hallo" - 123 -``` - - diff --git a/doc/docs/de/doc/control-flow/for-loop.md b/doc/docs/de/doc/control-flow/for-loop.md index 827d983..16b5418 100644 --- a/doc/docs/de/doc/control-flow/for-loop.md +++ b/doc/docs/de/doc/control-flow/for-loop.md @@ -86,7 +86,7 @@ doubled_evens = for i = 1, 20 -Zusätzlich unterstützen `for`-Schleifen `break` mit Rückgabewert, sodass die Schleife selbst als Ausdruck verwendet werden kann, der früh mit einem sinnvollen Ergebnis endet. +Zusätzlich unterstützen `for`-Schleifen `break` mit Rückgabewerten, sodass die Schleife selbst als Ausdruck verwendet werden kann, der früh mit einem sinnvollen Ergebnis endet. `for`-Ausdrücke unterstützen mehrere `break`-Werte. Beispiel: die erste Zahl größer als 10 finden: @@ -106,6 +106,20 @@ first_large = for n in *numbers Diese `break`-mit-Wert-Syntax ermöglicht knappe und ausdrucksstarke Such- bzw. Early-Exit-Muster direkt in Schleifenausdrücken. +```yuescript +key, score = for k, v in pairs data + break k, v * 10 if k == "target" +``` + + + +```yue +key, score = for k, v in pairs data + break k, v * 10 if k == "target" +``` + + + Du kannst Werte auch filtern, indem du den `for`-Ausdruck mit `continue` kombinierst. `for`-Schleifen am Ende eines Funktionsrumpfs werden nicht in eine Tabelle für einen Rückgabewert gesammelt (stattdessen gibt die Funktion `nil` zurück). Du kannst entweder explizit `return` verwenden oder die Schleife in eine Listen-Comprehension umwandeln. diff --git a/doc/docs/de/doc/control-flow/while-loop.md b/doc/docs/de/doc/control-flow/while-loop.md index 6d9f089..98776ec 100644 --- a/doc/docs/de/doc/control-flow/while-loop.md +++ b/doc/docs/de/doc/control-flow/while-loop.md @@ -45,7 +45,25 @@ until running == false do my_function! -Wie bei `for`-Schleifen kann die `while`-Schleife auch als Ausdruck verwendet werden. Damit eine Funktion den akkumulierten Wert einer `while`-Schleife zurückgibt, muss die Anweisung explizit mit `return` zurückgegeben werden. +Wie bei `for`-Schleifen kann die `while`-Schleife auch als Ausdruck verwendet werden. `while`- und `until`-Ausdrücke unterstützen `break` mit mehreren Rückgabewerten. + +```yuescript +value, doubled = while true + n = get_next! + break n, n * 2 if n > 10 +``` + + + +```yue +value, doubled = while true + n = get_next! + break n, n * 2 if n > 10 +``` + + + +Damit eine Funktion den akkumulierten Wert einer `while`-Schleife zurückgibt, muss die Anweisung explizit mit `return` zurückgegeben werden. ## Repeat-Schleife @@ -70,3 +88,25 @@ until i == 0 ``` + +`repeat`-Ausdrücke unterstützen ebenfalls `break` mit mehreren Rückgabewerten: + +```yuescript +i = 1 +value, scaled = repeat + break i, i * 100 if i > 3 + i += 1 +until false +``` + + + +```yue +i = 1 +value, scaled = repeat + break i, i * 100 if i > 3 + i += 1 +until false +``` + + diff --git a/doc/docs/de/doc/language-basics/module.md b/doc/docs/de/doc/language-basics/module.md new file mode 100644 index 0000000..f20deec --- /dev/null +++ b/doc/docs/de/doc/language-basics/module.md @@ -0,0 +1,253 @@ +# Module + +## Import + +Die `import`-Anweisung ist syntaktischer Zucker für `require` und hilft beim Extrahieren von Einträgen aus importierten Modulen. Importierte Elemente sind standardmäßig `const`. + +```yuescript +-- als Tabellen-Destrukturierung +do + import insert, concat from table + -- Fehler beim Zuweisen zu insert, concat + import C, Ct, Cmt from require "lpeg" + -- Kurzform für implizites Require + import x, y, z from 'mymodule' + -- Import im Python-Stil + from 'module' import a, b, c + +-- Kurzform zum Laden eines Moduls +do + import 'module' + import 'module_x' + import "d-a-s-h-e-s" + import "module.part" + +-- Modul mit Alias oder Tabellen-Destrukturierung laden +do + import "player" as PlayerModule + import "lpeg" as :C, :Ct, :Cmt + import "export" as {one, two, Something:{umm:{ch}}} +``` + + + +```yue +-- als Tabellen-Destrukturierung +do + import insert, concat from table + -- Fehler beim Zuweisen zu insert, concat + import C, Ct, Cmt from require "lpeg" + -- Kurzform für implizites Require + import x, y, z from 'mymodule' + -- Import im Python-Stil + from 'module' import a, b, c + +-- Kurzform zum Laden eines Moduls +do + import 'module' + import 'module_x' + import "d-a-s-h-e-s" + import "module.part" + +-- Modul mit Alias oder Tabellen-Destrukturierung laden +do + import "player" as PlayerModule + import "lpeg" as :C, :Ct, :Cmt + import "export" as {one, two, Something:{umm:{ch}}} +``` + + + +## Import von Globals + +Du kannst mit `import` bestimmte Globals in lokale Variablen importieren. Wenn du eine Kette von Globalzugriffen importierst, wird das letzte Feld der lokalen Variable zugewiesen. + +```yuescript +do + import tostring + import table.concat + print concat ["a", tostring 1] +``` + + + +```yue +do + import tostring + import table.concat + print concat ["a", tostring 1] +``` + + + +### Automatischer Global-Import + +Du kannst `import global` am Anfang eines Blocks platzieren, um automatisch alle Namen zu importieren, die im aktuellen Scope nicht explizit deklariert oder zugewiesen sind. Diese impliziten Importe werden als lokale `const` behandelt, die an die entsprechenden Globals zum Zeitpunkt der Anweisung gebunden sind. + +Namen, die im selben Scope explizit als `global` deklariert werden, werden nicht importiert, sodass du sie weiterhin zuweisen kannst. + +```yuescript +do + import global + print "hallo" + math.random 3 + -- print = nil -- Fehler: importierte Globals sind const + +do + -- explizite globale Variable wird nicht importiert + import global + global FLAG + print FLAG + FLAG = 123 +``` + + + +```yue +do + import global + print "hallo" + math.random 3 + -- print = nil -- Fehler: importierte Globals sind const + +do + -- explizite globale Variable wird nicht importiert + import global + global FLAG + print FLAG + FLAG = 123 +``` + + + +## Export + +Die `export`-Anweisung bietet eine knappe Möglichkeit, Module zu definieren. + +### Benannter Export + +Benannter Export definiert eine lokale Variable und fügt ein Feld in die exportierte Tabelle ein. + +```yuescript +export a, b, c = 1, 2, 3 +export cool = "Katze" + +export What = if this + "abc" +else + "def" + +export y = -> + hallo = 3434 + +export class Something + umm: "cool" +``` + + + +```yue +export a, b, c = 1, 2, 3 +export cool = "Katze" + +export What = if this + "abc" +else + "def" + +export y = -> + hallo = 3434 + +export class Something + umm: "cool" +``` + + + +Benannter Export mit Destructuring. + +```yuescript +export :loadstring, to_lua: tolua = yue +export {itemA: {:fieldA = 'default'}} = tb +``` + + + +```yue +export :loadstring, to_lua: tolua = yue +export {itemA: {:fieldA = 'default'}} = tb +``` + + + +Benannte Elemente aus dem Modul exportieren, ohne lokale Variablen zu erstellen. + +```yuescript +export.itemA = tb +export. = items +export["a-b-c"] = 123 +``` + + + +```yue +export.itemA = tb +export. = items +export["a-b-c"] = 123 +``` + + + +### Unbenannter Export + +Unbenannter Export fügt das Ziel-Element in den Array-Teil der exportierten Tabelle ein. + +```yuescript +d, e, f = 3, 2, 1 +export d, e, f + +export if this + 123 +else + 456 + +export with tmp + j = 2000 +``` + + + +```yue +d, e, f = 3, 2, 1 +export d, e, f + +export if this + 123 +else + 456 + +export with tmp + j = 2000 +``` + + + +### Default-Export + +Mit dem Schlüsselwort **default** in einer `export`-Anweisung wird die exportierte Tabelle durch ein beliebiges Objekt ersetzt. + +```yuescript +export default -> + print "hallo" + 123 +``` + + + +```yue +export default -> + print "hallo" + 123 +``` + + diff --git a/doc/docs/de/doc/objects/with-statement.md b/doc/docs/de/doc/objects/with-statement.md index adeeda6..f299902 100644 --- a/doc/docs/de/doc/objects/with-statement.md +++ b/doc/docs/de/doc/objects/with-statement.md @@ -44,6 +44,50 @@ file = with File "Lieblingsessen.txt" +`with`-Ausdrücke unterstützen `break` mit genau einem Wert: + +```yuescript +result = with obj + break .value +``` + + + +```yue +result = with obj + break .value +``` + + + +Sobald `break value` in `with` verwendet wird, gibt der `with`-Ausdruck nicht mehr sein Zielobjekt zurück, sondern den von `break` gelieferten Wert. + +```yuescript +a = with obj + .x = 1 +-- a ist obj + +b = with obj + break .x +-- b ist .x, nicht obj +``` + + + +```yue +a = with obj + .x = 1 +-- a ist obj + +b = with obj + break .x +-- b ist .x, nicht obj +``` + + + +Im Unterschied zu `for` / `while` / `repeat` / `do` unterstützt `with` nur einen `break`-Wert. + Oder … ```yuescript diff --git a/doc/docs/doc/advanced/do.md b/doc/docs/doc/advanced/do.md index c4b3e30..e13b025 100644 --- a/doc/docs/doc/advanced/do.md +++ b/doc/docs/doc/advanced/do.md @@ -22,6 +22,28 @@ print var -- nil here YueScript's **do** can also be used an expression . Allowing you to combine multiple lines into one. The result of the do expression is the last statement in its body. +`do` expressions also support using `break` to interrupt control flow and return multiple values early: + +```yuescript +status, value = do + n = 12 + if n > 10 + break "large", n + break "small", n +``` + + + +```yue +status, value = do + n = 12 + if n > 10 + break "large", n + break "small", n +``` + + + ```yuescript counter = do i = 0 diff --git a/doc/docs/doc/advanced/module.md b/doc/docs/doc/advanced/module.md deleted file mode 100644 index 0ba2d90..0000000 --- a/doc/docs/doc/advanced/module.md +++ /dev/null @@ -1,253 +0,0 @@ -# Module - -## Import - -The import statement is a syntax sugar for requiring a module or help extracting items from an imported module. The imported items are const by default. - -```yuescript --- used as table destructuring -do - import insert, concat from table - -- report error when assigning to insert, concat - import C, Ct, Cmt from require "lpeg" - -- shortcut for implicit requiring - import x, y, z from 'mymodule' - -- import with Python style - from 'module' import a, b, c - --- shortcut for requring a module -do - import 'module' - import 'module_x' - import "d-a-s-h-e-s" - import "module.part" - --- requring module with aliasing or table destructuring -do - import "player" as PlayerModule - import "lpeg" as :C, :Ct, :Cmt - import "export" as {one, two, Something:{umm:{ch}}} -``` - - - -```yue --- used as table destructuring -do - import insert, concat from table - -- report error when assigning to insert, concat - import C, Ct, Cmt from require "lpeg" - -- shortcut for implicit requiring - import x, y, z from 'mymodule' - -- import with Python style - from 'module' import a, b, c - --- shortcut for requring a module -do - import 'module' - import 'module_x' - import "d-a-s-h-e-s" - import "module.part" - --- requring module with aliasing or table destructuring -do - import "player" as PlayerModule - import "lpeg" as :C, :Ct, :Cmt - import "export" as {one, two, Something:{umm:{ch}}} -``` - - - -## Import Global - -You can import specific globals into local variables with `import`. When importing a chain of global variable accessings, the last field will be assigned to the local variable. - -```yuescript -do - import tostring - import table.concat - print concat ["a", tostring 1] -``` - - - -```yue -do - import tostring - import table.concat - print concat ["a", tostring 1] -``` - - - -### Automatic Global Variable Import - -You can place `import global` at the top of a block to automatically import all names that have not been explicitly declared or assigned in the current scope as globals. These implicit imports are treated as local consts that reference the corresponding globals at the position of the statement. - -Names that are explicitly declared as globals in the same scope will not be imported, so you can still assign to them. - -```yuescript -do - import global - print "hello" - math.random 3 - -- print = nil -- error: imported globals are const - -do - -- explicit global variable will not be imported - import global - global FLAG - print FLAG - FLAG = 123 -``` - - - -```yue -do - import global - print "hello" - math.random 3 - -- print = nil -- error: imported globals are const - -do - -- explicit global variable will not be imported - import global - global FLAG - print FLAG - FLAG = 123 -``` - - - -## Export - -The export statement offers a concise way to define modules. - -### Named Export - -Named export will define a local variable as well as adding a field in the exported table. - -```yuescript -export a, b, c = 1, 2, 3 -export cool = "cat" - -export What = if this - "abc" -else - "def" - -export y = -> - hallo = 3434 - -export class Something - umm: "cool" -``` - - - -```yue -export a, b, c = 1, 2, 3 -export cool = "cat" - -export What = if this - "abc" -else - "def" - -export y = -> - hallo = 3434 - -export class Something - umm: "cool" -``` - - - -Doing named export with destructuring. - -```yuescript -export :loadstring, to_lua: tolua = yue -export {itemA: {:fieldA = 'default'}} = tb -``` - - - -```yue -export :loadstring, to_lua: tolua = yue -export {itemA: {:fieldA = 'default'}} = tb -``` - - - -Export named items from module without creating local variables. - -```yuescript -export.itemA = tb -export. = items -export["a-b-c"] = 123 -``` - - - -```yue -export.itemA = tb -export. = items -export["a-b-c"] = 123 -``` - - - -### Unnamed Export - -Unnamed export will add the target item into the array part of the exported table. - -```yuescript -d, e, f = 3, 2, 1 -export d, e, f - -export if this - 123 -else - 456 - -export with tmp - j = 2000 -``` - - - -```yue -d, e, f = 3, 2, 1 -export d, e, f - -export if this - 123 -else - 456 - -export with tmp - j = 2000 -``` - - - -### Default Export - -Using the **default** keyword in export statement to replace the exported table with any thing. - -```yuescript -export default -> - print "hello" - 123 -``` - - - -```yue -export default -> - print "hello" - 123 -``` - - diff --git a/doc/docs/doc/control-flow/for-loop.md b/doc/docs/doc/control-flow/for-loop.md index b92e94e..763e6a7 100644 --- a/doc/docs/doc/control-flow/for-loop.md +++ b/doc/docs/doc/control-flow/for-loop.md @@ -86,7 +86,7 @@ doubled_evens = for i = 1, 20 -In addition, for loops support break with a return value, allowing the loop itself to be used as an expression that exits early with a meaningful result. +In addition, for loops support break with return values, allowing the loop itself to be used as an expression that exits early with meaningful results. For example, to find the first number greater than 10: @@ -106,6 +106,22 @@ first_large = for n in *numbers This break-with-value syntax enables concise and expressive search or early-exit patterns directly within loop expressions. +For loop expressions can break with multiple values: + +```yuescript +key, score = for k, v in pairs data + break k, v * 10 if k == "target" +``` + + + +```yue +key, score = for k, v in pairs data + break k, v * 10 if k == "target" +``` + + + You can also filter values by combining the for loop expression with the continue statement. For loops at the end of a function body are not accumulated into a table for a return value (Instead the function will return nil). Either an explicit return statement can be used, or the loop can be converted into a list comprehension. diff --git a/doc/docs/doc/control-flow/while-loop.md b/doc/docs/doc/control-flow/while-loop.md index 02e82d6..0dff342 100644 --- a/doc/docs/doc/control-flow/while-loop.md +++ b/doc/docs/doc/control-flow/while-loop.md @@ -45,7 +45,25 @@ until running == false do my_function! -Like for loops, the while loop can also be used an expression. Additionally, for a function to return the accumulated value of a while loop, the statement must be explicitly returned. +Like for loops, the while loop can also be used as an expression. While and until loop expressions support `break` with multiple return values. + +```yuescript +value, doubled = while true + n = get_next! + break n, n * 2 if n > 10 +``` + + + +```yue +value, doubled = while true + n = get_next! + break n, n * 2 if n > 10 +``` + + + +Additionally, for a function to return the accumulated value of a while loop, the statement must be explicitly returned. ## Repeat Loop @@ -70,3 +88,25 @@ until i == 0 ``` + +Repeat loop expressions also support `break` with multiple return values: + +```yuescript +i = 1 +value, scaled = repeat + break i, i * 100 if i > 3 + i += 1 +until false +``` + + + +```yue +i = 1 +value, scaled = repeat + break i, i * 100 if i > 3 + i += 1 +until false +``` + + diff --git a/doc/docs/doc/language-basics/module.md b/doc/docs/doc/language-basics/module.md new file mode 100644 index 0000000..0ba2d90 --- /dev/null +++ b/doc/docs/doc/language-basics/module.md @@ -0,0 +1,253 @@ +# Module + +## Import + +The import statement is a syntax sugar for requiring a module or help extracting items from an imported module. The imported items are const by default. + +```yuescript +-- used as table destructuring +do + import insert, concat from table + -- report error when assigning to insert, concat + import C, Ct, Cmt from require "lpeg" + -- shortcut for implicit requiring + import x, y, z from 'mymodule' + -- import with Python style + from 'module' import a, b, c + +-- shortcut for requring a module +do + import 'module' + import 'module_x' + import "d-a-s-h-e-s" + import "module.part" + +-- requring module with aliasing or table destructuring +do + import "player" as PlayerModule + import "lpeg" as :C, :Ct, :Cmt + import "export" as {one, two, Something:{umm:{ch}}} +``` + + + +```yue +-- used as table destructuring +do + import insert, concat from table + -- report error when assigning to insert, concat + import C, Ct, Cmt from require "lpeg" + -- shortcut for implicit requiring + import x, y, z from 'mymodule' + -- import with Python style + from 'module' import a, b, c + +-- shortcut for requring a module +do + import 'module' + import 'module_x' + import "d-a-s-h-e-s" + import "module.part" + +-- requring module with aliasing or table destructuring +do + import "player" as PlayerModule + import "lpeg" as :C, :Ct, :Cmt + import "export" as {one, two, Something:{umm:{ch}}} +``` + + + +## Import Global + +You can import specific globals into local variables with `import`. When importing a chain of global variable accessings, the last field will be assigned to the local variable. + +```yuescript +do + import tostring + import table.concat + print concat ["a", tostring 1] +``` + + + +```yue +do + import tostring + import table.concat + print concat ["a", tostring 1] +``` + + + +### Automatic Global Variable Import + +You can place `import global` at the top of a block to automatically import all names that have not been explicitly declared or assigned in the current scope as globals. These implicit imports are treated as local consts that reference the corresponding globals at the position of the statement. + +Names that are explicitly declared as globals in the same scope will not be imported, so you can still assign to them. + +```yuescript +do + import global + print "hello" + math.random 3 + -- print = nil -- error: imported globals are const + +do + -- explicit global variable will not be imported + import global + global FLAG + print FLAG + FLAG = 123 +``` + + + +```yue +do + import global + print "hello" + math.random 3 + -- print = nil -- error: imported globals are const + +do + -- explicit global variable will not be imported + import global + global FLAG + print FLAG + FLAG = 123 +``` + + + +## Export + +The export statement offers a concise way to define modules. + +### Named Export + +Named export will define a local variable as well as adding a field in the exported table. + +```yuescript +export a, b, c = 1, 2, 3 +export cool = "cat" + +export What = if this + "abc" +else + "def" + +export y = -> + hallo = 3434 + +export class Something + umm: "cool" +``` + + + +```yue +export a, b, c = 1, 2, 3 +export cool = "cat" + +export What = if this + "abc" +else + "def" + +export y = -> + hallo = 3434 + +export class Something + umm: "cool" +``` + + + +Doing named export with destructuring. + +```yuescript +export :loadstring, to_lua: tolua = yue +export {itemA: {:fieldA = 'default'}} = tb +``` + + + +```yue +export :loadstring, to_lua: tolua = yue +export {itemA: {:fieldA = 'default'}} = tb +``` + + + +Export named items from module without creating local variables. + +```yuescript +export.itemA = tb +export. = items +export["a-b-c"] = 123 +``` + + + +```yue +export.itemA = tb +export. = items +export["a-b-c"] = 123 +``` + + + +### Unnamed Export + +Unnamed export will add the target item into the array part of the exported table. + +```yuescript +d, e, f = 3, 2, 1 +export d, e, f + +export if this + 123 +else + 456 + +export with tmp + j = 2000 +``` + + + +```yue +d, e, f = 3, 2, 1 +export d, e, f + +export if this + 123 +else + 456 + +export with tmp + j = 2000 +``` + + + +### Default Export + +Using the **default** keyword in export statement to replace the exported table with any thing. + +```yuescript +export default -> + print "hello" + 123 +``` + + + +```yue +export default -> + print "hello" + 123 +``` + + diff --git a/doc/docs/doc/objects/with-statement.md b/doc/docs/doc/objects/with-statement.md index 5d7fea3..9173f9c 100644 --- a/doc/docs/doc/objects/with-statement.md +++ b/doc/docs/doc/objects/with-statement.md @@ -44,6 +44,50 @@ file = with File "favorite_foods.txt" +`with` expressions support `break` with one value: + +```yuescript +result = with obj + break .value +``` + + + +```yue +result = with obj + break .value +``` + + + +After `break value` is used inside `with`, the `with` expression no longer returns its target object. Instead, it returns the value from `break`. + +```yuescript +a = with obj + .x = 1 +-- a is obj + +b = with obj + break .x +-- b is .x, not obj +``` + + + +```yue +a = with obj + .x = 1 +-- a is obj + +b = with obj + break .x +-- b is .x, not obj +``` + + + +Unlike `for` / `while` / `repeat` / `do`, `with` only supports one break value. + Or… ```yuescript diff --git a/doc/docs/id-id/doc/advanced/do.md b/doc/docs/id-id/doc/advanced/do.md index ac75531..52ef5dc 100644 --- a/doc/docs/id-id/doc/advanced/do.md +++ b/doc/docs/id-id/doc/advanced/do.md @@ -20,7 +20,27 @@ print var -- nil di sini -`do` di YueScript juga bisa digunakan sebagai ekspresi, memungkinkan Anda menggabungkan beberapa baris menjadi satu. Hasil ekspresi `do` adalah pernyataan terakhir di badannya. +`do` di YueScript juga bisa digunakan sebagai ekspresi, memungkinkan Anda menggabungkan beberapa baris menjadi satu. Hasil ekspresi `do` adalah pernyataan terakhir di badannya. Ekspresi `do` mendukung penggunaan `break` untuk memutus alur eksekusi dan mengembalikan banyak nilai lebih awal. + +```yuescript +status, value = do + n = 12 + if n > 10 + break "large", n + break "small", n +``` + + + +```yue +status, value = do + n = 12 + if n > 10 + break "large", n + break "small", n +``` + + ```yuescript counter = do diff --git a/doc/docs/id-id/doc/advanced/module.md b/doc/docs/id-id/doc/advanced/module.md deleted file mode 100644 index 103e3f6..0000000 --- a/doc/docs/id-id/doc/advanced/module.md +++ /dev/null @@ -1,253 +0,0 @@ -# Modul - -## Import - -Pernyataan `import` adalah sintaks sugar untuk me-require modul atau membantu mengekstrak item dari modul yang diimpor. Item yang diimpor bersifat `const` secara default. - -```yuescript --- digunakan sebagai destrukturisasi tabel -do - import insert, concat from table - -- akan error saat meng-assign ke insert, concat - import C, Ct, Cmt from require "lpeg" - -- shortcut untuk require implisit - import x, y, z from 'mymodule' - -- import gaya Python - from 'module' import a, b, c - --- shortcut untuk require modul -do - import 'module' - import 'module_x' - import "d-a-s-h-e-s" - import "module.part" - --- require modul dengan aliasing atau destrukturisasi tabel -do - import "player" as PlayerModule - import "lpeg" as :C, :Ct, :Cmt - import "export" as {one, two, Something:{umm:{ch}}} -``` - - - -```yue --- digunakan sebagai destrukturisasi tabel -do - import insert, concat from table - -- akan error saat meng-assign ke insert, concat - import C, Ct, Cmt from require "lpeg" - -- shortcut untuk require implisit - import x, y, z from 'mymodule' - -- import gaya Python - from 'module' import a, b, c - --- shortcut untuk require modul -do - import 'module' - import 'module_x' - import "d-a-s-h-e-s" - import "module.part" - --- require modul dengan aliasing atau destrukturisasi tabel -do - import "player" as PlayerModule - import "lpeg" as :C, :Ct, :Cmt - import "export" as {one, two, Something:{umm:{ch}}} -``` - - - -## Import Global - -Anda dapat mengimpor global tertentu ke variabel local dengan `import`. Saat mengimpor rangkaian akses variabel global, field terakhir akan di-assign ke variabel local. - -```yuescript -do - import tostring - import table.concat - print concat ["a", tostring 1] -``` - - - -```yue -do - import tostring - import table.concat - print concat ["a", tostring 1] -``` - - - -### Import Variabel Global Otomatis - -Anda dapat menempatkan `import global` di awal blok untuk mengimpor secara otomatis semua nama yang belum dideklarasikan atau di-assign secara eksplisit di scope saat ini sebagai global. Import implisit ini diperlakukan sebagai local const yang mereferensikan global terkait pada posisi pernyataan tersebut. - -Nama yang secara eksplisit dideklarasikan sebagai global di scope yang sama tidak akan diimpor, sehingga Anda masih bisa meng-assign ke mereka. - -```yuescript -do - import global - print "hello" - math.random 3 - -- print = nil -- error: imported globals are const - -do - -- variabel global eksplisit tidak akan diimpor - import global - global FLAG - print FLAG - FLAG = 123 -``` - - - -```yue -do - import global - print "hello" - math.random 3 - -- print = nil -- error: imported globals are const - -do - -- variabel global eksplisit tidak akan diimpor - import global - global FLAG - print FLAG - FLAG = 123 -``` - - - -## Export - -Pernyataan `export` menawarkan cara ringkas untuk mendefinisikan modul. - -### Export Bernama - -Export bernama akan mendefinisikan variabel local sekaligus menambahkan field di tabel export. - -```yuescript -export a, b, c = 1, 2, 3 -export cool = "cat" - -export What = if this - "abc" -else - "def" - -export y = -> - hallo = 3434 - -export class Something - umm: "cool" -``` - - - -```yue -export a, b, c = 1, 2, 3 -export cool = "cat" - -export What = if this - "abc" -else - "def" - -export y = -> - hallo = 3434 - -export class Something - umm: "cool" -``` - - - -Melakukan export bernama dengan destrukturisasi. - -```yuescript -export :loadstring, to_lua: tolua = yue -export {itemA: {:fieldA = 'default'}} = tb -``` - - - -```yue -export :loadstring, to_lua: tolua = yue -export {itemA: {:fieldA = 'default'}} = tb -``` - - - -Export item bernama dari modul tanpa membuat variabel local. - -```yuescript -export.itemA = tb -export. = items -export["a-b-c"] = 123 -``` - - - -```yue -export.itemA = tb -export. = items -export["a-b-c"] = 123 -``` - - - -### Export Tanpa Nama - -Export tanpa nama akan menambahkan item target ke bagian array dari tabel export. - -```yuescript -d, e, f = 3, 2, 1 -export d, e, f - -export if this - 123 -else - 456 - -export with tmp - j = 2000 -``` - - - -```yue -d, e, f = 3, 2, 1 -export d, e, f - -export if this - 123 -else - 456 - -export with tmp - j = 2000 -``` - - - -### Export Default - -Gunakan kata kunci **default** dalam pernyataan export untuk mengganti tabel export dengan apa pun. - -```yuescript -export default -> - print "hello" - 123 -``` - - - -```yue -export default -> - print "hello" - 123 -``` - - diff --git a/doc/docs/id-id/doc/control-flow/for-loop.md b/doc/docs/id-id/doc/control-flow/for-loop.md index 65386f7..3835cce 100644 --- a/doc/docs/id-id/doc/control-flow/for-loop.md +++ b/doc/docs/id-id/doc/control-flow/for-loop.md @@ -86,7 +86,7 @@ doubled_evens = for i = 1, 20 -Selain itu, loop for mendukung break dengan nilai kembalian, sehingga loop itu sendiri bisa dipakai sebagai ekspresi yang keluar lebih awal dengan hasil bermakna. +Selain itu, loop for mendukung break dengan nilai kembalian, sehingga loop itu sendiri bisa dipakai sebagai ekspresi yang keluar lebih awal dengan hasil bermakna. Ekspresi `for` mendukung `break` dengan banyak nilai. Contohnya, untuk menemukan angka pertama yang lebih besar dari 10: @@ -106,6 +106,20 @@ first_large = for n in *numbers Sintaks break-dengan-nilai ini memungkinkan pola pencarian atau keluar-lebih-awal yang ringkas langsung di dalam ekspresi loop. +```yuescript +key, score = for k, v in pairs data + break k, v * 10 if k == "target" +``` + + + +```yue +key, score = for k, v in pairs data + break k, v * 10 if k == "target" +``` + + + Anda juga bisa memfilter nilai dengan menggabungkan ekspresi for dengan pernyataan continue. Loop for di akhir badan fungsi tidak diakumulasikan menjadi tabel untuk nilai kembalian (sebaliknya fungsi akan mengembalikan nil). Gunakan pernyataan return eksplisit, atau ubah loop menjadi list comprehension. diff --git a/doc/docs/id-id/doc/control-flow/while-loop.md b/doc/docs/id-id/doc/control-flow/while-loop.md index 3e302cc..0c3a28d 100644 --- a/doc/docs/id-id/doc/control-flow/while-loop.md +++ b/doc/docs/id-id/doc/control-flow/while-loop.md @@ -45,7 +45,25 @@ until running == false do my_function! -Seperti loop for, loop while juga bisa digunakan sebagai ekspresi. Selain itu, agar sebuah fungsi mengembalikan nilai akumulasi dari loop while, pernyataannya harus di-return secara eksplisit. +Seperti loop for, loop while juga bisa digunakan sebagai ekspresi. Ekspresi `while` dan `until` mendukung `break` dengan banyak nilai. + +```yuescript +value, doubled = while true + n = get_next! + break n, n * 2 if n > 10 +``` + + + +```yue +value, doubled = while true + n = get_next! + break n, n * 2 if n > 10 +``` + + + +Selain itu, agar sebuah fungsi mengembalikan nilai akumulasi dari loop while, pernyataannya harus di-return secara eksplisit. ## Repeat Loop @@ -70,3 +88,25 @@ until i == 0 ``` + +Ekspresi `repeat` juga mendukung `break` dengan banyak nilai: + +```yuescript +i = 1 +value, scaled = repeat + break i, i * 100 if i > 3 + i += 1 +until false +``` + + + +```yue +i = 1 +value, scaled = repeat + break i, i * 100 if i > 3 + i += 1 +until false +``` + + diff --git a/doc/docs/id-id/doc/language-basics/module.md b/doc/docs/id-id/doc/language-basics/module.md new file mode 100644 index 0000000..103e3f6 --- /dev/null +++ b/doc/docs/id-id/doc/language-basics/module.md @@ -0,0 +1,253 @@ +# Modul + +## Import + +Pernyataan `import` adalah sintaks sugar untuk me-require modul atau membantu mengekstrak item dari modul yang diimpor. Item yang diimpor bersifat `const` secara default. + +```yuescript +-- digunakan sebagai destrukturisasi tabel +do + import insert, concat from table + -- akan error saat meng-assign ke insert, concat + import C, Ct, Cmt from require "lpeg" + -- shortcut untuk require implisit + import x, y, z from 'mymodule' + -- import gaya Python + from 'module' import a, b, c + +-- shortcut untuk require modul +do + import 'module' + import 'module_x' + import "d-a-s-h-e-s" + import "module.part" + +-- require modul dengan aliasing atau destrukturisasi tabel +do + import "player" as PlayerModule + import "lpeg" as :C, :Ct, :Cmt + import "export" as {one, two, Something:{umm:{ch}}} +``` + + + +```yue +-- digunakan sebagai destrukturisasi tabel +do + import insert, concat from table + -- akan error saat meng-assign ke insert, concat + import C, Ct, Cmt from require "lpeg" + -- shortcut untuk require implisit + import x, y, z from 'mymodule' + -- import gaya Python + from 'module' import a, b, c + +-- shortcut untuk require modul +do + import 'module' + import 'module_x' + import "d-a-s-h-e-s" + import "module.part" + +-- require modul dengan aliasing atau destrukturisasi tabel +do + import "player" as PlayerModule + import "lpeg" as :C, :Ct, :Cmt + import "export" as {one, two, Something:{umm:{ch}}} +``` + + + +## Import Global + +Anda dapat mengimpor global tertentu ke variabel local dengan `import`. Saat mengimpor rangkaian akses variabel global, field terakhir akan di-assign ke variabel local. + +```yuescript +do + import tostring + import table.concat + print concat ["a", tostring 1] +``` + + + +```yue +do + import tostring + import table.concat + print concat ["a", tostring 1] +``` + + + +### Import Variabel Global Otomatis + +Anda dapat menempatkan `import global` di awal blok untuk mengimpor secara otomatis semua nama yang belum dideklarasikan atau di-assign secara eksplisit di scope saat ini sebagai global. Import implisit ini diperlakukan sebagai local const yang mereferensikan global terkait pada posisi pernyataan tersebut. + +Nama yang secara eksplisit dideklarasikan sebagai global di scope yang sama tidak akan diimpor, sehingga Anda masih bisa meng-assign ke mereka. + +```yuescript +do + import global + print "hello" + math.random 3 + -- print = nil -- error: imported globals are const + +do + -- variabel global eksplisit tidak akan diimpor + import global + global FLAG + print FLAG + FLAG = 123 +``` + + + +```yue +do + import global + print "hello" + math.random 3 + -- print = nil -- error: imported globals are const + +do + -- variabel global eksplisit tidak akan diimpor + import global + global FLAG + print FLAG + FLAG = 123 +``` + + + +## Export + +Pernyataan `export` menawarkan cara ringkas untuk mendefinisikan modul. + +### Export Bernama + +Export bernama akan mendefinisikan variabel local sekaligus menambahkan field di tabel export. + +```yuescript +export a, b, c = 1, 2, 3 +export cool = "cat" + +export What = if this + "abc" +else + "def" + +export y = -> + hallo = 3434 + +export class Something + umm: "cool" +``` + + + +```yue +export a, b, c = 1, 2, 3 +export cool = "cat" + +export What = if this + "abc" +else + "def" + +export y = -> + hallo = 3434 + +export class Something + umm: "cool" +``` + + + +Melakukan export bernama dengan destrukturisasi. + +```yuescript +export :loadstring, to_lua: tolua = yue +export {itemA: {:fieldA = 'default'}} = tb +``` + + + +```yue +export :loadstring, to_lua: tolua = yue +export {itemA: {:fieldA = 'default'}} = tb +``` + + + +Export item bernama dari modul tanpa membuat variabel local. + +```yuescript +export.itemA = tb +export. = items +export["a-b-c"] = 123 +``` + + + +```yue +export.itemA = tb +export. = items +export["a-b-c"] = 123 +``` + + + +### Export Tanpa Nama + +Export tanpa nama akan menambahkan item target ke bagian array dari tabel export. + +```yuescript +d, e, f = 3, 2, 1 +export d, e, f + +export if this + 123 +else + 456 + +export with tmp + j = 2000 +``` + + + +```yue +d, e, f = 3, 2, 1 +export d, e, f + +export if this + 123 +else + 456 + +export with tmp + j = 2000 +``` + + + +### Export Default + +Gunakan kata kunci **default** dalam pernyataan export untuk mengganti tabel export dengan apa pun. + +```yuescript +export default -> + print "hello" + 123 +``` + + + +```yue +export default -> + print "hello" + 123 +``` + + diff --git a/doc/docs/id-id/doc/objects/with-statement.md b/doc/docs/id-id/doc/objects/with-statement.md index 96a3efd..b3dd520 100644 --- a/doc/docs/id-id/doc/objects/with-statement.md +++ b/doc/docs/id-id/doc/objects/with-statement.md @@ -44,6 +44,50 @@ file = with File "favorite_foods.txt" +Ekspresi `with` mendukung `break` dengan satu nilai: + +```yuescript +result = with obj + break .value +``` + + + +```yue +result = with obj + break .value +``` + + + +Setelah `break value` digunakan di dalam `with`, ekspresi `with` tidak lagi mengembalikan objek targetnya, melainkan mengembalikan nilai dari `break`. + +```yuescript +a = with obj + .x = 1 +-- a adalah obj + +b = with obj + break .x +-- b adalah .x, bukan obj +``` + + + +```yue +a = with obj + .x = 1 +-- a adalah obj + +b = with obj + break .x +-- b adalah .x, bukan obj +``` + + + +Berbeda dari `for` / `while` / `repeat` / `do`, `with` hanya mendukung satu nilai `break`. + Atau… ```yuescript diff --git a/doc/docs/pt-br/doc/advanced/do.md b/doc/docs/pt-br/doc/advanced/do.md index 503c551..aaf2c69 100644 --- a/doc/docs/pt-br/doc/advanced/do.md +++ b/doc/docs/pt-br/doc/advanced/do.md @@ -20,7 +20,27 @@ print var -- nil aqui -O **do** do YueScript também pode ser usado como expressão. Permitindo combinar múltiplas linhas em uma. O resultado da expressão do é a última instrução em seu corpo. +O **do** do YueScript também pode ser usado como expressão. Permitindo combinar múltiplas linhas em uma. O resultado da expressão do é a última instrução em seu corpo. Expressões `do` suportam usar `break` para interromper o fluxo de execução e retornar múltiplos valores antecipadamente. + +```yuescript +status, value = do + n = 12 + if n > 10 + break "large", n + break "small", n +``` + + + +```yue +status, value = do + n = 12 + if n > 10 + break "large", n + break "small", n +``` + + ```yuescript counter = do diff --git a/doc/docs/pt-br/doc/advanced/module.md b/doc/docs/pt-br/doc/advanced/module.md deleted file mode 100644 index ed13107..0000000 --- a/doc/docs/pt-br/doc/advanced/module.md +++ /dev/null @@ -1,253 +0,0 @@ -# Módulo - -## Import - -A instrução import é um açúcar sintático para requerer um módulo ou ajudar a extrair itens de um módulo importado. Os itens importados são const por padrão. - -```yuescript --- usado como desestruturação de tabela -do - import insert, concat from table - -- reporta erro ao atribuir a insert, concat - import C, Ct, Cmt from require "lpeg" - -- atalho para require implícito - import x, y, z from 'mymodule' - -- import com estilo Python - from 'module' import a, b, c - --- atalho para requerer um módulo -do - import 'module' - import 'module_x' - import "d-a-s-h-e-s" - import "module.part" - --- requerer módulo com aliasing ou desestruturação de tabela -do - import "player" as PlayerModule - import "lpeg" as :C, :Ct, :Cmt - import "export" as {one, two, Something:{umm:{ch}}} -``` - - - -```yue --- usado como desestruturação de tabela -do - import insert, concat from table - -- reporta erro ao atribuir a insert, concat - import C, Ct, Cmt from require "lpeg" - -- atalho para require implícito - import x, y, z from 'mymodule' - -- import com estilo Python - from 'module' import a, b, c - --- atalho para requerer um módulo -do - import 'module' - import 'module_x' - import "d-a-s-h-e-s" - import "module.part" - --- requerer módulo com aliasing ou desestruturação de tabela -do - import "player" as PlayerModule - import "lpeg" as :C, :Ct, :Cmt - import "export" as {one, two, Something:{umm:{ch}}} -``` - - - -## Import Global - -Você pode importar globais específicos para variáveis locais com `import`. Ao importar uma cadeia de acessos a variáveis globais, o último campo será atribuído à variável local. - -```yuescript -do - import tostring - import table.concat - print concat ["a", tostring 1] -``` - - - -```yue -do - import tostring - import table.concat - print concat ["a", tostring 1] -``` - - - -### Importação automática de variável global - -Você pode colocar `import global` no topo de um bloco para importar automaticamente todos os nomes que não foram explicitamente declarados ou atribuídos no escopo atual como globais. Essas importações implícitas são tratadas como consts locais que referenciam os globais correspondentes na posição da instrução. - -Nomes que foram explicitamente declarados como globais no mesmo escopo não serão importados, então você ainda pode atribuir a eles. - -```yuescript -do - import global - print "hello" - math.random 3 - -- print = nil -- erro: globais importados são const - -do - -- variável global explícita não será importada - import global - global FLAG - print FLAG - FLAG = 123 -``` - - - -```yue -do - import global - print "hello" - math.random 3 - -- print = nil -- erro: globais importados são const - -do - -- variável global explícita não será importada - import global - global FLAG - print FLAG - FLAG = 123 -``` - - - -## Export - -A instrução export oferece uma forma concisa de definir módulos. - -### Export nomeado - -Export nomeado definirá uma variável local e também adicionará um campo na tabela exportada. - -```yuescript -export a, b, c = 1, 2, 3 -export cool = "cat" - -export What = if this - "abc" -else - "def" - -export y = -> - hallo = 3434 - -export class Something - umm: "cool" -``` - - - -```yue -export a, b, c = 1, 2, 3 -export cool = "cat" - -export What = if this - "abc" -else - "def" - -export y = -> - hallo = 3434 - -export class Something - umm: "cool" -``` - - - -Fazendo export nomeado com desestruturação. - -```yuescript -export :loadstring, to_lua: tolua = yue -export {itemA: {:fieldA = 'default'}} = tb -``` - - - -```yue -export :loadstring, to_lua: tolua = yue -export {itemA: {:fieldA = 'default'}} = tb -``` - - - -Exportar itens nomeados do módulo sem criar variáveis locais. - -```yuescript -export.itemA = tb -export. = items -export["a-b-c"] = 123 -``` - - - -```yue -export.itemA = tb -export. = items -export["a-b-c"] = 123 -``` - - - -### Export sem nome - -Export sem nome adicionará o item alvo na parte array da tabela exportada. - -```yuescript -d, e, f = 3, 2, 1 -export d, e, f - -export if this - 123 -else - 456 - -export with tmp - j = 2000 -``` - - - -```yue -d, e, f = 3, 2, 1 -export d, e, f - -export if this - 123 -else - 456 - -export with tmp - j = 2000 -``` - - - -### Export padrão - -Usar a palavra-chave **default** na instrução export para substituir a tabela exportada por qualquer coisa. - -```yuescript -export default -> - print "hello" - 123 -``` - - - -```yue -export default -> - print "hello" - 123 -``` - - diff --git a/doc/docs/pt-br/doc/control-flow/for-loop.md b/doc/docs/pt-br/doc/control-flow/for-loop.md index 4c99e6d..de5f9c2 100644 --- a/doc/docs/pt-br/doc/control-flow/for-loop.md +++ b/doc/docs/pt-br/doc/control-flow/for-loop.md @@ -86,7 +86,7 @@ doubled_evens = for i = 1, 20 -Além disso, os loops for suportam break com valor de retorno, permitindo que o próprio loop seja usado como expressão que sai antecipadamente com um resultado significativo. +Além disso, os loops for suportam break com valores de retorno, permitindo que o próprio loop seja usado como expressão que sai antecipadamente com um resultado significativo. Expressões `for` suportam `break` com múltiplos valores. Por exemplo, para encontrar o primeiro número maior que 10: @@ -106,6 +106,20 @@ first_large = for n in *numbers Esta sintaxe de break-com-valor permite padrões concisos e expressivos de busca ou saída antecipada diretamente em expressões de loop. +```yuescript +key, score = for k, v in pairs data + break k, v * 10 if k == "target" +``` + + + +```yue +key, score = for k, v in pairs data + break k, v * 10 if k == "target" +``` + + + Você também pode filtrar valores combinando a expressão do loop for com a instrução continue. Loops for no final do corpo de uma função não são acumulados em uma tabela para valor de retorno (em vez disso, a função retornará nil). Uma instrução return explícita pode ser usada, ou o loop pode ser convertido em compreensão de lista. diff --git a/doc/docs/pt-br/doc/control-flow/while-loop.md b/doc/docs/pt-br/doc/control-flow/while-loop.md index 2deb5aa..38df05a 100644 --- a/doc/docs/pt-br/doc/control-flow/while-loop.md +++ b/doc/docs/pt-br/doc/control-flow/while-loop.md @@ -45,7 +45,25 @@ until running == false do my_function! -Como os loops for, o loop while também pode ser usado como expressão. Além disso, para uma função retornar o valor acumulado de um loop while, a instrução deve ser explicitamente retornada. +Como os loops for, o loop while também pode ser usado como expressão. Expressões `while` e `until` suportam `break` com múltiplos valores. + +```yuescript +value, doubled = while true + n = get_next! + break n, n * 2 if n > 10 +``` + + + +```yue +value, doubled = while true + n = get_next! + break n, n * 2 if n > 10 +``` + + + +Além disso, para uma função retornar o valor acumulado de um loop while, a instrução deve ser explicitamente retornada. ## Loop Repeat @@ -70,3 +88,25 @@ until i == 0 ``` + +Expressões `repeat` também suportam `break` com múltiplos valores: + +```yuescript +i = 1 +value, scaled = repeat + break i, i * 100 if i > 3 + i += 1 +until false +``` + + + +```yue +i = 1 +value, scaled = repeat + break i, i * 100 if i > 3 + i += 1 +until false +``` + + diff --git a/doc/docs/pt-br/doc/language-basics/module.md b/doc/docs/pt-br/doc/language-basics/module.md new file mode 100644 index 0000000..ed13107 --- /dev/null +++ b/doc/docs/pt-br/doc/language-basics/module.md @@ -0,0 +1,253 @@ +# Módulo + +## Import + +A instrução import é um açúcar sintático para requerer um módulo ou ajudar a extrair itens de um módulo importado. Os itens importados são const por padrão. + +```yuescript +-- usado como desestruturação de tabela +do + import insert, concat from table + -- reporta erro ao atribuir a insert, concat + import C, Ct, Cmt from require "lpeg" + -- atalho para require implícito + import x, y, z from 'mymodule' + -- import com estilo Python + from 'module' import a, b, c + +-- atalho para requerer um módulo +do + import 'module' + import 'module_x' + import "d-a-s-h-e-s" + import "module.part" + +-- requerer módulo com aliasing ou desestruturação de tabela +do + import "player" as PlayerModule + import "lpeg" as :C, :Ct, :Cmt + import "export" as {one, two, Something:{umm:{ch}}} +``` + + + +```yue +-- usado como desestruturação de tabela +do + import insert, concat from table + -- reporta erro ao atribuir a insert, concat + import C, Ct, Cmt from require "lpeg" + -- atalho para require implícito + import x, y, z from 'mymodule' + -- import com estilo Python + from 'module' import a, b, c + +-- atalho para requerer um módulo +do + import 'module' + import 'module_x' + import "d-a-s-h-e-s" + import "module.part" + +-- requerer módulo com aliasing ou desestruturação de tabela +do + import "player" as PlayerModule + import "lpeg" as :C, :Ct, :Cmt + import "export" as {one, two, Something:{umm:{ch}}} +``` + + + +## Import Global + +Você pode importar globais específicos para variáveis locais com `import`. Ao importar uma cadeia de acessos a variáveis globais, o último campo será atribuído à variável local. + +```yuescript +do + import tostring + import table.concat + print concat ["a", tostring 1] +``` + + + +```yue +do + import tostring + import table.concat + print concat ["a", tostring 1] +``` + + + +### Importação automática de variável global + +Você pode colocar `import global` no topo de um bloco para importar automaticamente todos os nomes que não foram explicitamente declarados ou atribuídos no escopo atual como globais. Essas importações implícitas são tratadas como consts locais que referenciam os globais correspondentes na posição da instrução. + +Nomes que foram explicitamente declarados como globais no mesmo escopo não serão importados, então você ainda pode atribuir a eles. + +```yuescript +do + import global + print "hello" + math.random 3 + -- print = nil -- erro: globais importados são const + +do + -- variável global explícita não será importada + import global + global FLAG + print FLAG + FLAG = 123 +``` + + + +```yue +do + import global + print "hello" + math.random 3 + -- print = nil -- erro: globais importados são const + +do + -- variável global explícita não será importada + import global + global FLAG + print FLAG + FLAG = 123 +``` + + + +## Export + +A instrução export oferece uma forma concisa de definir módulos. + +### Export nomeado + +Export nomeado definirá uma variável local e também adicionará um campo na tabela exportada. + +```yuescript +export a, b, c = 1, 2, 3 +export cool = "cat" + +export What = if this + "abc" +else + "def" + +export y = -> + hallo = 3434 + +export class Something + umm: "cool" +``` + + + +```yue +export a, b, c = 1, 2, 3 +export cool = "cat" + +export What = if this + "abc" +else + "def" + +export y = -> + hallo = 3434 + +export class Something + umm: "cool" +``` + + + +Fazendo export nomeado com desestruturação. + +```yuescript +export :loadstring, to_lua: tolua = yue +export {itemA: {:fieldA = 'default'}} = tb +``` + + + +```yue +export :loadstring, to_lua: tolua = yue +export {itemA: {:fieldA = 'default'}} = tb +``` + + + +Exportar itens nomeados do módulo sem criar variáveis locais. + +```yuescript +export.itemA = tb +export. = items +export["a-b-c"] = 123 +``` + + + +```yue +export.itemA = tb +export. = items +export["a-b-c"] = 123 +``` + + + +### Export sem nome + +Export sem nome adicionará o item alvo na parte array da tabela exportada. + +```yuescript +d, e, f = 3, 2, 1 +export d, e, f + +export if this + 123 +else + 456 + +export with tmp + j = 2000 +``` + + + +```yue +d, e, f = 3, 2, 1 +export d, e, f + +export if this + 123 +else + 456 + +export with tmp + j = 2000 +``` + + + +### Export padrão + +Usar a palavra-chave **default** na instrução export para substituir a tabela exportada por qualquer coisa. + +```yuescript +export default -> + print "hello" + 123 +``` + + + +```yue +export default -> + print "hello" + 123 +``` + + diff --git a/doc/docs/pt-br/doc/objects/with-statement.md b/doc/docs/pt-br/doc/objects/with-statement.md index e5a56e5..38efee7 100644 --- a/doc/docs/pt-br/doc/objects/with-statement.md +++ b/doc/docs/pt-br/doc/objects/with-statement.md @@ -44,6 +44,50 @@ file = with File "favorite_foods.txt" +Expressões `with` suportam `break` com um valor: + +```yuescript +result = with obj + break .value +``` + + + +```yue +result = with obj + break .value +``` + + + +Depois que `break value` é usado dentro de `with`, a expressão `with` deixa de retornar seu objeto-alvo e passa a retornar o valor de `break`. + +```yuescript +a = with obj + .x = 1 +-- a é obj + +b = with obj + break .x +-- b é .x, não obj +``` + + + +```yue +a = with obj + .x = 1 +-- a é obj + +b = with obj + break .x +-- b é .x, não obj +``` + + + +Diferente de `for` / `while` / `repeat` / `do`, `with` suporta apenas um valor de `break`. + Ou… ```yuescript diff --git a/doc/docs/zh/doc/advanced/do.md b/doc/docs/zh/doc/advanced/do.md index a2d5d19..b3f61af 100644 --- a/doc/docs/zh/doc/advanced/do.md +++ b/doc/docs/zh/doc/advanced/do.md @@ -20,7 +20,27 @@ print var -- 这里是nil -  月之脚本的 **do** 也可以用作表达式。允许你将多行代码的处理合并为一个表达式,并将 do 语句代码块的最后一个语句作为表达式返回的结果。 +  月之脚本的 **do** 也可以用作表达式。允许你将多行代码的处理合并为一个表达式,并将 do 语句代码块的最后一个语句作为表达式返回的结果。`do` 表达式支持通过 `break` 打断执行流并提前返回多个值。 + +```yuescript +status, value = do + n = 12 + if n > 10 + break "large", n + break "small", n +``` + + + +```yue +status, value = do + n = 12 + if n > 10 + break "large", n + break "small", n +``` + + ```yuescript counter = do diff --git a/doc/docs/zh/doc/advanced/module.md b/doc/docs/zh/doc/advanced/module.md deleted file mode 100644 index 6c90f0e..0000000 --- a/doc/docs/zh/doc/advanced/module.md +++ /dev/null @@ -1,253 +0,0 @@ -# 模块 - -## 导入 - -  导入语句是一个语法糖,用于需要引入一个模块或者从已导入的模块中提取子项目。从模块导入的变量默认为不可修改的常量。 - -```yuescript --- 用作表解构 -do - import insert, concat from table - -- 当给 insert, concat 变量赋值时,编译器会报告错误 - import C, Ct, Cmt from require "lpeg" - -- 快捷写法引入模块的子项 - import x, y, z from 'mymodule' - -- 使用Python风格的导入 - from 'module' import a, b, c - --- 快捷地导入一个模块 -do - import 'module' - import 'module_x' - import "d-a-s-h-e-s" - import "module.part" - --- 导入模块后起一个别名使用,或是进行导入模块表的解构 -do - import "player" as PlayerModule - import "lpeg" as :C, :Ct, :Cmt - import "export" as {one, two, Something:{umm:{ch}}} -``` - - - -```yue --- 用作表解构 -do - import insert, concat from table - -- 当给 insert, concat 变量赋值时,编译器会报告错误 - import C, Ct, Cmt from require "lpeg" - -- 快捷写法引入模块的子项 - import x, y, z from 'mymodule' - -- 使用Python风格的导入 - from 'module' import a, b, c - --- 快捷地导入一个模块 -do - import 'module' - import 'module_x' - import "d-a-s-h-e-s" - import "module.part" - --- 导入模块后起一个别名使用,或是进行导入模块表的解构 -do - import "player" as PlayerModule - import "lpeg" as :C, :Ct, :Cmt - import "export" as {one, two, Something:{umm:{ch}}} -``` - - - -## 导入全局变量 - -  你可以使用 `import` 将指定的全局变量导入到本地变量中。当导入一系列对全局变量的链式访问时,最后一个访问的字段将被赋值给本地变量。 - -```yuescript -do - import tostring - import table.concat - print concat ["a", tostring 1] -``` - - - -```yue -do - import tostring - import table.concat - print concat ["a", tostring 1] -``` - - - -### 自动全局变量导入 - -  在一个代码块的顶部写 `import global`,会将当前作用域中尚未显式声明或赋值过的变量名,自动导入为本地常量,并在该语句的位置绑定到同名的全局变量。 - -  但是在同一作用域中被显式声明为全局的变量不会被自动导入,因此可以继续进行赋值操作。 - -```yuescript -do - import global - print "hello" - math.random 3 - -- print = nil -- 报错:自动导入的全局变量为常量 - -do - -- 被显式声明为全局的变量不会被自动导入 - import global - global FLAG - print FLAG - FLAG = 123 -``` - - - -```yue -do - import global - print "hello" - math.random 3 - -- print = nil -- 报错:自动导入的全局变量是常量 - -do - -- 被显式声明为全局的变量不会被自动导入 - import global - global FLAG - print FLAG - FLAG = 123 -``` - - - -## 导出 - -  导出语句提供了一种简洁的方式来定义当前的模块。 - -### 命名导出 - -  带命名的导出将定义一个局部变量,并在导出的表中添加一个同名的字段。 - -```yuescript -export a, b, c = 1, 2, 3 -export cool = "cat" - -export What = if this - "abc" -else - "def" - -export y = -> - hallo = 3434 - -export class Something - umm: "cool" -``` - - - -```yue -export a, b, c = 1, 2, 3 -export cool = "cat" - -export What = if this - "abc" -else - "def" - -export y = -> - hallo = 3434 - -export class Something - umm: "cool" -``` - - - -  使用解构进行命名导出。 - -```yuescript -export :loadstring, to_lua: tolua = yue -export {itemA: {:fieldA = '默认值'}} = tb -``` - - - -```yue -export :loadstring, to_lua: tolua = yue -export {itemA: {:fieldA = '默认值'}} = tb -``` - - - -  从模块导出命名项目时,可以不用创建局部变量。 - -```yuescript -export.itemA = tb -export. = items -export["a-b-c"] = 123 -``` - - - -```yue -export.itemA = tb -export. = items -export["a-b-c"] = 123 -``` - - - -### 未命名导出 - -  未命名导出会将要导出的目标项目添加到导出表的数组部分。 - -```yuescript -d, e, f = 3, 2, 1 -export d, e, f - -export if this - 123 -else - 456 - -export with tmp - j = 2000 -``` - - - -```yue -d, e, f = 3, 2, 1 -export d, e, f - -export if this - 123 -else - 456 - -export with tmp - j = 2000 -``` - - - -### 默认导出 - -  在导出语句中使用 **default** 关键字,来替换导出的表为一个目标的对象。 - -```yuescript -export default -> - print "你好" - 123 -``` - - - -```yue -export default -> - print "你好" - 123 -``` - - diff --git a/doc/docs/zh/doc/control-flow/for-loop.md b/doc/docs/zh/doc/control-flow/for-loop.md index 52180b7..97e66f9 100644 --- a/doc/docs/zh/doc/control-flow/for-loop.md +++ b/doc/docs/zh/doc/control-flow/for-loop.md @@ -86,7 +86,7 @@ doubled_evens = for i = 1, 20 -  此外,for 循环还支持带返回值的 break 语句,这样循环本身就可以作为一个表达式,在满足条件时提前退出并返回有意义的结果。 +  此外,for 循环还支持带返回值的 break 语句,这样循环本身就可以作为一个表达式,在满足条件时提前退出并返回有意义的结果。for 循环表达式支持 `break` 返回多个值。   例如,查找第一个大于 10 的数字: @@ -104,6 +104,20 @@ first_large = for n in *numbers +```yuescript +key, score = for k, v in pairs data + break k, v * 10 if k == "target" +``` + + + +```yue +key, score = for k, v in pairs data + break k, v * 10 if k == "target" +``` + + +   你还可以结合 for 循环表达式与 continue 语句来过滤值。   注意出现在函数体末尾的 for 循环,不会被当作是一个表达式并将循环结果累积到一个列表中作为返回值(相反,函数将返回 nil)。如果要函数末尾的循环转换为列表表达式,可以显式地使用返回语句加 for 循环表达式。 diff --git a/doc/docs/zh/doc/control-flow/while-loop.md b/doc/docs/zh/doc/control-flow/while-loop.md index 3c624fe..0138d85 100644 --- a/doc/docs/zh/doc/control-flow/while-loop.md +++ b/doc/docs/zh/doc/control-flow/while-loop.md @@ -45,7 +45,25 @@ until running == false do my_function! -  像 for 循环的语法一样,while 循环也可以作为一个表达式使用。为了使函数返回 while 循环的累积列表值,必须明确使用返回语句返回 while 循环表达式。 +  像 for 循环的语法一样,while 循环也可以作为一个表达式使用。while / until 循环表达式支持 `break` 返回多个值。 + +```yuescript +value, doubled = while true + n = get_next! + break n, n * 2 if n > 10 +``` + + + +```yue +value, doubled = while true + n = get_next! + break n, n * 2 if n > 10 +``` + + + +  为了使函数返回 while 循环的累积列表值,必须明确使用返回语句返回 while 循环表达式。 ## repeat 循环 @@ -70,3 +88,25 @@ until i == 0 ``` + +  repeat 循环表达式同样支持 `break` 返回多个值: + +```yuescript +i = 1 +value, scaled = repeat + break i, i * 100 if i > 3 + i += 1 +until false +``` + + + +```yue +i = 1 +value, scaled = repeat + break i, i * 100 if i > 3 + i += 1 +until false +``` + + diff --git a/doc/docs/zh/doc/language-basics/module.md b/doc/docs/zh/doc/language-basics/module.md new file mode 100644 index 0000000..6c90f0e --- /dev/null +++ b/doc/docs/zh/doc/language-basics/module.md @@ -0,0 +1,253 @@ +# 模块 + +## 导入 + +  导入语句是一个语法糖,用于需要引入一个模块或者从已导入的模块中提取子项目。从模块导入的变量默认为不可修改的常量。 + +```yuescript +-- 用作表解构 +do + import insert, concat from table + -- 当给 insert, concat 变量赋值时,编译器会报告错误 + import C, Ct, Cmt from require "lpeg" + -- 快捷写法引入模块的子项 + import x, y, z from 'mymodule' + -- 使用Python风格的导入 + from 'module' import a, b, c + +-- 快捷地导入一个模块 +do + import 'module' + import 'module_x' + import "d-a-s-h-e-s" + import "module.part" + +-- 导入模块后起一个别名使用,或是进行导入模块表的解构 +do + import "player" as PlayerModule + import "lpeg" as :C, :Ct, :Cmt + import "export" as {one, two, Something:{umm:{ch}}} +``` + + + +```yue +-- 用作表解构 +do + import insert, concat from table + -- 当给 insert, concat 变量赋值时,编译器会报告错误 + import C, Ct, Cmt from require "lpeg" + -- 快捷写法引入模块的子项 + import x, y, z from 'mymodule' + -- 使用Python风格的导入 + from 'module' import a, b, c + +-- 快捷地导入一个模块 +do + import 'module' + import 'module_x' + import "d-a-s-h-e-s" + import "module.part" + +-- 导入模块后起一个别名使用,或是进行导入模块表的解构 +do + import "player" as PlayerModule + import "lpeg" as :C, :Ct, :Cmt + import "export" as {one, two, Something:{umm:{ch}}} +``` + + + +## 导入全局变量 + +  你可以使用 `import` 将指定的全局变量导入到本地变量中。当导入一系列对全局变量的链式访问时,最后一个访问的字段将被赋值给本地变量。 + +```yuescript +do + import tostring + import table.concat + print concat ["a", tostring 1] +``` + + + +```yue +do + import tostring + import table.concat + print concat ["a", tostring 1] +``` + + + +### 自动全局变量导入 + +  在一个代码块的顶部写 `import global`,会将当前作用域中尚未显式声明或赋值过的变量名,自动导入为本地常量,并在该语句的位置绑定到同名的全局变量。 + +  但是在同一作用域中被显式声明为全局的变量不会被自动导入,因此可以继续进行赋值操作。 + +```yuescript +do + import global + print "hello" + math.random 3 + -- print = nil -- 报错:自动导入的全局变量为常量 + +do + -- 被显式声明为全局的变量不会被自动导入 + import global + global FLAG + print FLAG + FLAG = 123 +``` + + + +```yue +do + import global + print "hello" + math.random 3 + -- print = nil -- 报错:自动导入的全局变量是常量 + +do + -- 被显式声明为全局的变量不会被自动导入 + import global + global FLAG + print FLAG + FLAG = 123 +``` + + + +## 导出 + +  导出语句提供了一种简洁的方式来定义当前的模块。 + +### 命名导出 + +  带命名的导出将定义一个局部变量,并在导出的表中添加一个同名的字段。 + +```yuescript +export a, b, c = 1, 2, 3 +export cool = "cat" + +export What = if this + "abc" +else + "def" + +export y = -> + hallo = 3434 + +export class Something + umm: "cool" +``` + + + +```yue +export a, b, c = 1, 2, 3 +export cool = "cat" + +export What = if this + "abc" +else + "def" + +export y = -> + hallo = 3434 + +export class Something + umm: "cool" +``` + + + +  使用解构进行命名导出。 + +```yuescript +export :loadstring, to_lua: tolua = yue +export {itemA: {:fieldA = '默认值'}} = tb +``` + + + +```yue +export :loadstring, to_lua: tolua = yue +export {itemA: {:fieldA = '默认值'}} = tb +``` + + + +  从模块导出命名项目时,可以不用创建局部变量。 + +```yuescript +export.itemA = tb +export. = items +export["a-b-c"] = 123 +``` + + + +```yue +export.itemA = tb +export. = items +export["a-b-c"] = 123 +``` + + + +### 未命名导出 + +  未命名导出会将要导出的目标项目添加到导出表的数组部分。 + +```yuescript +d, e, f = 3, 2, 1 +export d, e, f + +export if this + 123 +else + 456 + +export with tmp + j = 2000 +``` + + + +```yue +d, e, f = 3, 2, 1 +export d, e, f + +export if this + 123 +else + 456 + +export with tmp + j = 2000 +``` + + + +### 默认导出 + +  在导出语句中使用 **default** 关键字,来替换导出的表为一个目标的对象。 + +```yuescript +export default -> + print "你好" + 123 +``` + + + +```yue +export default -> + print "你好" + 123 +``` + + diff --git a/doc/docs/zh/doc/objects/with-statement.md b/doc/docs/zh/doc/objects/with-statement.md index 0925050..338e2cc 100644 --- a/doc/docs/zh/doc/objects/with-statement.md +++ b/doc/docs/zh/doc/objects/with-statement.md @@ -44,6 +44,50 @@ file = with File "favorite_foods.txt" +`with` 表达式支持 `break` 返回一个值: + +```yuescript +result = with obj + break .value +``` + + + +```yue +result = with obj + break .value +``` + + + +在 `with` 中使用 `break value` 后,`with` 表达式将不再返回其目标对象,而是返回 `break` 给出的值。 + +```yuescript +a = with obj + .x = 1 +-- a 是 obj + +b = with obj + break .x +-- b 是 .x,不是 obj +``` + + + +```yue +a = with obj + .x = 1 +-- a 是 obj + +b = with obj + break .x +-- b 是 .x,不是 obj +``` + + + +与 `for` / `while` / `repeat` / `do` 不同,`with` 只支持一个 break 返回值。 + 或者… ```yuescript -- cgit v1.2.3-55-g6feb