diff options
| author | Li Jin <dragon-fly@qq.com> | 2026-02-06 08:42:20 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2026-02-06 08:42:20 +0800 |
| commit | b350a76827329e05e97c25e07bb8ea2a7efe30b4 (patch) | |
| tree | 81d865d8e5856c4c991385c0d0856522d803f075 /doc/docs | |
| parent | 5d7a9205df3c20f5b6a0464f3e1123394a7ca23a (diff) | |
| download | yuescript-codex/add-portuguese-and-german-language-support.tar.gz yuescript-codex/add-portuguese-and-german-language-support.tar.bz2 yuescript-codex/add-portuguese-and-german-language-support.zip | |
docs: add German and Portuguese (Brazil) localescodex/add-portuguese-and-german-language-support
Diffstat (limited to 'doc/docs')
71 files changed, 11309 insertions, 132 deletions
diff --git a/doc/docs/.vitepress/config.mts b/doc/docs/.vitepress/config.mts index 188c2fa..39ded54 100644 --- a/doc/docs/.vitepress/config.mts +++ b/doc/docs/.vitepress/config.mts | |||
| @@ -19,137 +19,182 @@ const yuescriptLanguage = { | |||
| 19 | aliases: ['yue'], | 19 | aliases: ['yue'], |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | // Generate sidebar configuration function | 22 | const sidebarText = { |
| 23 | function createSidebar(basePath: string, zh: boolean) { | 23 | en: { |
| 24 | introduction: 'Introduction', | ||
| 25 | installation: 'Installation', | ||
| 26 | usage: 'Usage', | ||
| 27 | macro: 'Macro', | ||
| 28 | operator: 'Operator', | ||
| 29 | module: 'Module', | ||
| 30 | assignment: 'Assignment', | ||
| 31 | destructuringAssignment: 'Destructuring Assignment', | ||
| 32 | ifAssignment: 'If Assignment', | ||
| 33 | varargsAssignment: 'Varargs Assignment', | ||
| 34 | whitespace: 'Whitespace', | ||
| 35 | comment: 'Comment', | ||
| 36 | try: 'Try', | ||
| 37 | attributes: 'Attributes', | ||
| 38 | literals: 'Literals', | ||
| 39 | functionLiterals: 'Function Literals', | ||
| 40 | backcalls: 'Backcalls', | ||
| 41 | tableLiterals: 'Table Literals', | ||
| 42 | comprehensions: 'Comprehensions', | ||
| 43 | forLoop: 'For Loop', | ||
| 44 | whileLoop: 'While Loop', | ||
| 45 | continueStatement: 'Continue Statement', | ||
| 46 | conditionals: 'Conditionals', | ||
| 47 | lineDecorators: 'Line Decorators', | ||
| 48 | switch: 'Switch', | ||
| 49 | objectOrientedProgramming: 'Object Oriented Programming', | ||
| 50 | withStatement: 'With Statement', | ||
| 51 | do: 'Do', | ||
| 52 | functionStubs: 'Function Stubs', | ||
| 53 | usingClause: 'The Using Clause; Controlling Destructive Assignment', | ||
| 54 | yuescriptLibrary: 'The YueScript Library', | ||
| 55 | licenseMit: 'License: MIT', | ||
| 56 | }, | ||
| 57 | zh: { | ||
| 58 | introduction: '介绍', | ||
| 59 | installation: '安装', | ||
| 60 | usage: '使用方法', | ||
| 61 | macro: '宏', | ||
| 62 | operator: '操作符', | ||
| 63 | module: '模块', | ||
| 64 | assignment: '赋值', | ||
| 65 | destructuringAssignment: '解构赋值', | ||
| 66 | ifAssignment: 'if 赋值', | ||
| 67 | varargsAssignment: '可变参数赋值', | ||
| 68 | whitespace: '空白', | ||
| 69 | comment: '注释', | ||
| 70 | try: '错误处理', | ||
| 71 | attributes: '属性', | ||
| 72 | literals: '字面量', | ||
| 73 | functionLiterals: '函数字面量', | ||
| 74 | backcalls: '反向回调', | ||
| 75 | tableLiterals: '表格字面量', | ||
| 76 | comprehensions: '推导式', | ||
| 77 | forLoop: 'for 循环', | ||
| 78 | whileLoop: 'while 循环', | ||
| 79 | continueStatement: 'continue 语句', | ||
| 80 | conditionals: '条件语句', | ||
| 81 | lineDecorators: '代码行修饰符', | ||
| 82 | switch: 'switch 语句', | ||
| 83 | objectOrientedProgramming: '面向对象编程', | ||
| 84 | withStatement: 'with 语句', | ||
| 85 | do: 'do 语句', | ||
| 86 | functionStubs: '函数存根', | ||
| 87 | usingClause: '使用 using 语句:防止破坏性赋值', | ||
| 88 | yuescriptLibrary: '月之脚本语言库', | ||
| 89 | licenseMit: 'MIT 许可证', | ||
| 90 | }, | ||
| 91 | de: { | ||
| 92 | introduction: 'Einführung', | ||
| 93 | installation: 'Installation', | ||
| 94 | usage: 'Verwendung', | ||
| 95 | macro: 'Makro', | ||
| 96 | operator: 'Operatoren', | ||
| 97 | module: 'Modul', | ||
| 98 | assignment: 'Zuweisung', | ||
| 99 | destructuringAssignment: 'Destrukturierende Zuweisung', | ||
| 100 | ifAssignment: 'If-Zuweisung', | ||
| 101 | varargsAssignment: 'Varargs-Zuweisung', | ||
| 102 | whitespace: 'Leerraum', | ||
| 103 | comment: 'Kommentare', | ||
| 104 | try: 'Try/Catch', | ||
| 105 | attributes: 'Attribute', | ||
| 106 | literals: 'Literale', | ||
| 107 | functionLiterals: 'Funktionsliterale', | ||
| 108 | backcalls: 'Backcalls', | ||
| 109 | tableLiterals: 'Tabellenliterale', | ||
| 110 | comprehensions: 'Comprehensions', | ||
| 111 | forLoop: 'For-Schleife', | ||
| 112 | whileLoop: 'While-Schleife', | ||
| 113 | continueStatement: 'Continue-Anweisung', | ||
| 114 | conditionals: 'Bedingungen', | ||
| 115 | lineDecorators: 'Zeilen-Dekoratoren', | ||
| 116 | switch: 'Switch', | ||
| 117 | objectOrientedProgramming: 'Objektorientierte Programmierung', | ||
| 118 | withStatement: 'With-Anweisung', | ||
| 119 | do: 'Do', | ||
| 120 | functionStubs: 'Funktions-Stubs', | ||
| 121 | usingClause: 'Die Using-Klausel; Kontrolle destruktiver Zuweisung', | ||
| 122 | yuescriptLibrary: 'Die YueScript-Bibliothek', | ||
| 123 | licenseMit: 'Lizenz: MIT', | ||
| 124 | }, | ||
| 125 | ptBr: { | ||
| 126 | introduction: 'Introdução', | ||
| 127 | installation: 'Instalação', | ||
| 128 | usage: 'Uso', | ||
| 129 | macro: 'Macro', | ||
| 130 | operator: 'Operadores', | ||
| 131 | module: 'Módulo', | ||
| 132 | assignment: 'Atribuição', | ||
| 133 | destructuringAssignment: 'Atribuição com desestruturação', | ||
| 134 | ifAssignment: 'Atribuição com if', | ||
| 135 | varargsAssignment: 'Atribuição de varargs', | ||
| 136 | whitespace: 'Espaços em branco', | ||
| 137 | comment: 'Comentários', | ||
| 138 | try: 'Try/Catch', | ||
| 139 | attributes: 'Atributos', | ||
| 140 | literals: 'Literais', | ||
| 141 | functionLiterals: 'Literais de função', | ||
| 142 | backcalls: 'Backcalls', | ||
| 143 | tableLiterals: 'Literais de tabela', | ||
| 144 | comprehensions: 'Compreensões', | ||
| 145 | forLoop: 'Laço for', | ||
| 146 | whileLoop: 'Laço while', | ||
| 147 | continueStatement: 'Instrução continue', | ||
| 148 | conditionals: 'Condicionais', | ||
| 149 | lineDecorators: 'Decoradores de linha', | ||
| 150 | switch: 'Switch', | ||
| 151 | objectOrientedProgramming: 'Programação orientada a objetos', | ||
| 152 | withStatement: 'Instrução with', | ||
| 153 | do: 'Do', | ||
| 154 | functionStubs: 'Stubs de função', | ||
| 155 | usingClause: 'Cláusula using; controlando atribuição destrutiva', | ||
| 156 | yuescriptLibrary: 'A biblioteca do YueScript', | ||
| 157 | licenseMit: 'Licença: MIT', | ||
| 158 | }, | ||
| 159 | } as const | ||
| 160 | |||
| 161 | type SidebarLocale = keyof typeof sidebarText | ||
| 162 | |||
| 163 | function createSidebar(basePath: string, locale: SidebarLocale) { | ||
| 164 | const text = sidebarText[locale] | ||
| 24 | return [ | 165 | return [ |
| 25 | { | 166 | { text: text.introduction, link: `${basePath}/introduction` }, |
| 26 | text: zh ? '介绍' : 'Introduction', | 167 | { text: text.installation, link: `${basePath}/installation` }, |
| 27 | link: `${basePath}/introduction`, | 168 | { text: text.usage, link: `${basePath}/usage` }, |
| 28 | }, | 169 | { text: text.macro, link: `${basePath}/macro` }, |
| 29 | { | 170 | { text: text.operator, link: `${basePath}/operator` }, |
| 30 | text: zh ? '安装' : 'Installation', | 171 | { text: text.module, link: `${basePath}/module` }, |
| 31 | link: `${basePath}/installation`, | 172 | { text: text.assignment, link: `${basePath}/assignment` }, |
| 32 | }, | 173 | { text: text.destructuringAssignment, link: `${basePath}/destructuring-assignment` }, |
| 33 | { | 174 | { text: text.ifAssignment, link: `${basePath}/if-assignment` }, |
| 34 | text: zh ? '使用方法' : 'Usage', | 175 | { text: text.varargsAssignment, link: `${basePath}/varargs-assignment` }, |
| 35 | link: `${basePath}/usage`, | 176 | { text: text.whitespace, link: `${basePath}/whitespace` }, |
| 36 | }, | 177 | { text: text.comment, link: `${basePath}/comment` }, |
| 37 | { | 178 | { text: text.try, link: `${basePath}/try` }, |
| 38 | text: zh ? '宏' : 'Macro', | 179 | { text: text.attributes, link: `${basePath}/attributes` }, |
| 39 | link: `${basePath}/macro`, | 180 | { text: text.literals, link: `${basePath}/literals` }, |
| 40 | }, | 181 | { text: text.functionLiterals, link: `${basePath}/function-literals` }, |
| 41 | { | 182 | { text: text.backcalls, link: `${basePath}/backcalls` }, |
| 42 | text: zh ? '操作符' : 'Operator', | 183 | { text: text.tableLiterals, link: `${basePath}/table-literals` }, |
| 43 | link: `${basePath}/operator`, | 184 | { text: text.comprehensions, link: `${basePath}/comprehensions` }, |
| 44 | }, | 185 | { text: text.forLoop, link: `${basePath}/for-loop` }, |
| 45 | { | 186 | { text: text.whileLoop, link: `${basePath}/while-loop` }, |
| 46 | text: zh ? '模块' : 'Module', | 187 | { text: text.continueStatement, link: `${basePath}/continue` }, |
| 47 | link: `${basePath}/module`, | 188 | { text: text.conditionals, link: `${basePath}/conditionals` }, |
| 48 | }, | 189 | { text: text.lineDecorators, link: `${basePath}/line-decorators` }, |
| 49 | { | 190 | { text: text.switch, link: `${basePath}/switch` }, |
| 50 | text: zh ? '赋值' : 'Assignment', | 191 | { text: text.objectOrientedProgramming, link: `${basePath}/object-oriented-programming` }, |
| 51 | link: `${basePath}/assignment`, | 192 | { text: text.withStatement, link: `${basePath}/with-statement` }, |
| 52 | }, | 193 | { text: text.do, link: `${basePath}/do` }, |
| 53 | { | 194 | { text: text.functionStubs, link: `${basePath}/function-stubs` }, |
| 54 | text: zh ? '解构赋值' : 'Destructuring Assignment', | 195 | { text: text.usingClause, link: `${basePath}/the-using-clause-controlling-destructive-assignment` }, |
| 55 | link: `${basePath}/destructuring-assignment`, | 196 | { text: text.yuescriptLibrary, link: `${basePath}/the-yuescript-library` }, |
| 56 | }, | 197 | { text: text.licenseMit, link: `${basePath}/licence-mit` }, |
| 57 | { | ||
| 58 | text: zh ? 'if 赋值' : 'If Assignment', | ||
| 59 | link: `${basePath}/if-assignment`, | ||
| 60 | }, | ||
| 61 | { | ||
| 62 | text: zh ? '可变参数赋值' : 'Varargs Assignment', | ||
| 63 | link: `${basePath}/varargs-assignment`, | ||
| 64 | }, | ||
| 65 | { | ||
| 66 | text: zh ? '空白' : 'Whitespace', | ||
| 67 | link: `${basePath}/whitespace`, | ||
| 68 | }, | ||
| 69 | { | ||
| 70 | text: zh ? '注释' : 'Comment', | ||
| 71 | link: `${basePath}/comment`, | ||
| 72 | }, | ||
| 73 | { | ||
| 74 | text: zh ? '错误处理' : 'Try', | ||
| 75 | link: `${basePath}/try`, | ||
| 76 | }, | ||
| 77 | { | ||
| 78 | text: zh ? '属性' : 'Attributes', | ||
| 79 | link: `${basePath}/attributes`, | ||
| 80 | }, | ||
| 81 | { | ||
| 82 | text: zh ? '字面量' : 'Literals', | ||
| 83 | link: `${basePath}/literals`, | ||
| 84 | }, | ||
| 85 | { | ||
| 86 | text: zh ? '函数字面量' : 'Function Literals', | ||
| 87 | link: `${basePath}/function-literals`, | ||
| 88 | }, | ||
| 89 | { | ||
| 90 | text: zh ? '反向回调' : 'Backcalls', | ||
| 91 | link: `${basePath}/backcalls`, | ||
| 92 | }, | ||
| 93 | { | ||
| 94 | text: zh ? '表格字面量' : 'Table Literals', | ||
| 95 | link: `${basePath}/table-literals`, | ||
| 96 | }, | ||
| 97 | { | ||
| 98 | text: zh ? '推导式' : 'Comprehensions', | ||
| 99 | link: `${basePath}/comprehensions`, | ||
| 100 | }, | ||
| 101 | { | ||
| 102 | text: zh ? 'for 循环' : 'For Loop', | ||
| 103 | link: `${basePath}/for-loop`, | ||
| 104 | }, | ||
| 105 | { | ||
| 106 | text: zh ? 'while 循环' : 'While Loop', | ||
| 107 | link: `${basePath}/while-loop`, | ||
| 108 | }, | ||
| 109 | { | ||
| 110 | text: zh ? 'continue 语句' : 'Continue Statement', | ||
| 111 | link: `${basePath}/continue`, | ||
| 112 | }, | ||
| 113 | { | ||
| 114 | text: zh ? '条件语句' : 'Conditionals', | ||
| 115 | link: `${basePath}/conditionals`, | ||
| 116 | }, | ||
| 117 | { | ||
| 118 | text: zh ? '代码行修饰符' : 'Line Decorators', | ||
| 119 | link: `${basePath}/line-decorators`, | ||
| 120 | }, | ||
| 121 | { | ||
| 122 | text: zh ? 'switch 语句' : 'Switch', | ||
| 123 | link: `${basePath}/switch`, | ||
| 124 | }, | ||
| 125 | { | ||
| 126 | text: zh ? '面向对象编程' : 'Object Oriented Programming', | ||
| 127 | link: `${basePath}/object-oriented-programming`, | ||
| 128 | }, | ||
| 129 | { | ||
| 130 | text: zh ? 'with 语句' : 'With Statement', | ||
| 131 | link: `${basePath}/with-statement`, | ||
| 132 | }, | ||
| 133 | { | ||
| 134 | text: zh ? 'do 语句' : 'Do', | ||
| 135 | link: `${basePath}/do`, | ||
| 136 | }, | ||
| 137 | { | ||
| 138 | text: zh ? '函数存根' : 'Function Stubs', | ||
| 139 | link: `${basePath}/function-stubs`, | ||
| 140 | }, | ||
| 141 | { | ||
| 142 | text: zh ? '使用 using 语句:防止破坏性赋值' : 'The Using Clause; Controlling Destructive Assignment', | ||
| 143 | link: `${basePath}/the-using-clause-controlling-destructive-assignment`, | ||
| 144 | }, | ||
| 145 | { | ||
| 146 | text: zh ? '月之脚本语言库' : 'The YueScript Library', | ||
| 147 | link: `${basePath}/the-yuescript-library`, | ||
| 148 | }, | ||
| 149 | { | ||
| 150 | text: zh ? 'MIT 许可证' : 'License: MIT', | ||
| 151 | link: `${basePath}/license-mit`, | ||
| 152 | }, | ||
| 153 | ] | 198 | ] |
| 154 | } | 199 | } |
| 155 | 200 | ||
| @@ -223,7 +268,7 @@ export default defineConfig({ | |||
| 223 | { text: 'Try yue!', link: '/try/' }, | 268 | { text: 'Try yue!', link: '/try/' }, |
| 224 | { text: 'GitHub', link: 'https://github.com/IppClub/Yuescript' } | 269 | { text: 'GitHub', link: 'https://github.com/IppClub/Yuescript' } |
| 225 | ], | 270 | ], |
| 226 | sidebar: createSidebar('/doc', false), | 271 | sidebar: createSidebar('/doc', 'en'), |
| 227 | } | 272 | } |
| 228 | }, | 273 | }, |
| 229 | zh: { | 274 | zh: { |
| @@ -236,7 +281,33 @@ export default defineConfig({ | |||
| 236 | { text: '试一试!', link: '/zh/try/' }, | 281 | { text: '试一试!', link: '/zh/try/' }, |
| 237 | { text: 'GitHub', link: 'https://github.com/IppClub/Yuescript' } | 282 | { text: 'GitHub', link: 'https://github.com/IppClub/Yuescript' } |
| 238 | ], | 283 | ], |
| 239 | sidebar: createSidebar('/zh/doc', true), | 284 | sidebar: createSidebar('/zh/doc', 'zh'), |
| 285 | } | ||
| 286 | }, | ||
| 287 | de: { | ||
| 288 | label: 'Deutsch', | ||
| 289 | lang: 'de-DE', | ||
| 290 | description: 'Eine Sprache, die zu Lua kompiliert', | ||
| 291 | themeConfig: { | ||
| 292 | nav: [ | ||
| 293 | { text: 'Dokumentation', link: '/de/doc/' }, | ||
| 294 | { text: 'Yue ausprobieren!', link: '/de/try/' }, | ||
| 295 | { text: 'GitHub', link: 'https://github.com/IppClub/Yuescript' } | ||
| 296 | ], | ||
| 297 | sidebar: createSidebar('/de/doc', 'de'), | ||
| 298 | } | ||
| 299 | }, | ||
| 300 | 'pt-br': { | ||
| 301 | label: 'Português (Brasil)', | ||
| 302 | lang: 'pt-BR', | ||
| 303 | description: 'Uma linguagem que compila para Lua', | ||
| 304 | themeConfig: { | ||
| 305 | nav: [ | ||
| 306 | { text: 'Documentação', link: '/pt-br/doc/' }, | ||
| 307 | { text: 'Experimente Yue!', link: '/pt-br/try/' }, | ||
| 308 | { text: 'GitHub', link: 'https://github.com/IppClub/Yuescript' } | ||
| 309 | ], | ||
| 310 | sidebar: createSidebar('/pt-br/doc', 'ptBr'), | ||
| 240 | } | 311 | } |
| 241 | } | 312 | } |
| 242 | }, | 313 | }, |
diff --git a/doc/docs/de/doc/assignment.md b/doc/docs/de/doc/assignment.md new file mode 100644 index 0000000..4dac6f4 --- /dev/null +++ b/doc/docs/de/doc/assignment.md | |||
| @@ -0,0 +1,138 @@ | |||
| 1 | # Assignment | ||
| 2 | |||
| 3 | The variable is dynamic typed and is defined as local by default. But you can change the scope of declaration by **local** and **global** statement. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | hello = "world" | ||
| 7 | a, b, c = 1, 2, 3 | ||
| 8 | hello = 123 -- uses the existing variable | ||
| 9 | ``` | ||
| 10 | <YueDisplay> | ||
| 11 | |||
| 12 | ```yue | ||
| 13 | hello = "world" | ||
| 14 | a, b, c = 1, 2, 3 | ||
| 15 | hello = 123 -- uses the existing variable | ||
| 16 | ``` | ||
| 17 | |||
| 18 | </YueDisplay> | ||
| 19 | |||
| 20 | ## Perform Update | ||
| 21 | |||
| 22 | You can perform update assignment with many binary operators. | ||
| 23 | ```yuescript | ||
| 24 | x = 1 | ||
| 25 | x += 1 | ||
| 26 | x -= 1 | ||
| 27 | x *= 10 | ||
| 28 | x /= 10 | ||
| 29 | x %= 10 | ||
| 30 | s ..= "world" -- will add a new local if local variable is not exist | ||
| 31 | arg or= "default value" | ||
| 32 | ``` | ||
| 33 | <YueDisplay> | ||
| 34 | |||
| 35 | ```yue | ||
| 36 | x = 1 | ||
| 37 | x += 1 | ||
| 38 | x -= 1 | ||
| 39 | x *= 10 | ||
| 40 | x /= 10 | ||
| 41 | x %= 10 | ||
| 42 | s ..= "world" -- will add a new local if local variable is not exist | ||
| 43 | arg or= "default value" | ||
| 44 | ``` | ||
| 45 | |||
| 46 | </YueDisplay> | ||
| 47 | |||
| 48 | ## Chaining Assignment | ||
| 49 | |||
| 50 | You can do chaining assignment to assign multiple items to hold the same value. | ||
| 51 | ```yuescript | ||
| 52 | a = b = c = d = e = 0 | ||
| 53 | x = y = z = f! | ||
| 54 | ``` | ||
| 55 | <YueDisplay> | ||
| 56 | |||
| 57 | ```yue | ||
| 58 | a = b = c = d = e = 0 | ||
| 59 | x = y = z = f! | ||
| 60 | ``` | ||
| 61 | |||
| 62 | </YueDisplay> | ||
| 63 | |||
| 64 | ## Explicit Locals | ||
| 65 | ```yuescript | ||
| 66 | do | ||
| 67 | local a = 1 | ||
| 68 | local * | ||
| 69 | print "forward declare all variables as locals" | ||
| 70 | x = -> 1 + y + z | ||
| 71 | y, z = 2, 3 | ||
| 72 | global instance = Item\new! | ||
| 73 | |||
| 74 | do | ||
| 75 | local X = 1 | ||
| 76 | local ^ | ||
| 77 | print "only forward declare upper case variables" | ||
| 78 | a = 1 | ||
| 79 | B = 2 | ||
| 80 | ``` | ||
| 81 | <YueDisplay> | ||
| 82 | |||
| 83 | ```yue | ||
| 84 | do | ||
| 85 | local a = 1 | ||
| 86 | local * | ||
| 87 | print "forward declare all variables as locals" | ||
| 88 | x = -> 1 + y + z | ||
| 89 | y, z = 2, 3 | ||
| 90 | global instance = Item\new! | ||
| 91 | |||
| 92 | do | ||
| 93 | local X = 1 | ||
| 94 | local ^ | ||
| 95 | print "only forward declare upper case variables" | ||
| 96 | a = 1 | ||
| 97 | B = 2 | ||
| 98 | ``` | ||
| 99 | |||
| 100 | </YueDisplay> | ||
| 101 | |||
| 102 | ## Explicit Globals | ||
| 103 | ```yuescript | ||
| 104 | do | ||
| 105 | global a = 1 | ||
| 106 | global * | ||
| 107 | print "declare all variables as globals" | ||
| 108 | x = -> 1 + y + z | ||
| 109 | y, z = 2, 3 | ||
| 110 | |||
| 111 | do | ||
| 112 | global X = 1 | ||
| 113 | global ^ | ||
| 114 | print "only declare upper case variables as globals" | ||
| 115 | a = 1 | ||
| 116 | B = 2 | ||
| 117 | local Temp = "a local value" | ||
| 118 | ``` | ||
| 119 | <YueDisplay> | ||
| 120 | |||
| 121 | ```yue | ||
| 122 | do | ||
| 123 | global a = 1 | ||
| 124 | global * | ||
| 125 | print "declare all variables as globals" | ||
| 126 | x = -> 1 + y + z | ||
| 127 | y, z = 2, 3 | ||
| 128 | |||
| 129 | do | ||
| 130 | global X = 1 | ||
| 131 | global ^ | ||
| 132 | print "only declare upper case variables as globals" | ||
| 133 | a = 1 | ||
| 134 | B = 2 | ||
| 135 | local Temp = "a local value" | ||
| 136 | ``` | ||
| 137 | |||
| 138 | </YueDisplay> | ||
diff --git a/doc/docs/de/doc/attributes.md b/doc/docs/de/doc/attributes.md new file mode 100644 index 0000000..e6fd5a7 --- /dev/null +++ b/doc/docs/de/doc/attributes.md | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | # Attributes | ||
| 2 | |||
| 3 | Syntax support for Lua 5.4 attributes. And you can still use both the `const` and `close` declaration and get constant check and scoped callback working when targeting Lua versions below 5.4. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | const a = 123 | ||
| 7 | close _ = <close>: -> print "Out of scope." | ||
| 8 | ``` | ||
| 9 | <YueDisplay> | ||
| 10 | |||
| 11 | ```yue | ||
| 12 | const a = 123 | ||
| 13 | close _ = <close>: -> print "Out of scope." | ||
| 14 | ``` | ||
| 15 | |||
| 16 | </YueDisplay> | ||
| 17 | |||
| 18 | You can do desctructuring with variables attributed as constant. | ||
| 19 | |||
| 20 | ```yuescript | ||
| 21 | const {:a, :b, c, d} = tb | ||
| 22 | -- a = 1 | ||
| 23 | ``` | ||
| 24 | <YueDisplay> | ||
| 25 | |||
| 26 | ```yue | ||
| 27 | const {:a, :b, c, d} = tb | ||
| 28 | -- a = 1 | ||
| 29 | ``` | ||
| 30 | |||
| 31 | </YueDisplay> | ||
| 32 | |||
| 33 | You can also declare a global variable to be `const`. | ||
| 34 | |||
| 35 | ```yuescript | ||
| 36 | global const Constant = 123 | ||
| 37 | -- Constant = 1 | ||
| 38 | ``` | ||
| 39 | <YueDisplay> | ||
| 40 | |||
| 41 | ```yue | ||
| 42 | global const Constant = 123 | ||
| 43 | -- Constant = 1 | ||
| 44 | ``` | ||
| 45 | |||
| 46 | </YueDisplay> | ||
diff --git a/doc/docs/de/doc/backcalls.md b/doc/docs/de/doc/backcalls.md new file mode 100644 index 0000000..e34331e --- /dev/null +++ b/doc/docs/de/doc/backcalls.md | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | # Backcalls | ||
| 2 | |||
| 3 | Backcalls are used for unnesting callbacks. They are defined using arrows pointed to the left as the last parameter by default filling in a function call. All the syntax is mostly the same as regular arrow functions except that it is just pointing the other way and the function body does not require indent. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | x <- f | ||
| 7 | print "hello" .. x | ||
| 8 | ``` | ||
| 9 | <YueDisplay> | ||
| 10 | |||
| 11 | ```yue | ||
| 12 | x <- f | ||
| 13 | print "hello" .. x | ||
| 14 | ``` | ||
| 15 | |||
| 16 | </YueDisplay> | ||
| 17 | |||
| 18 | Fat arrow functions are also available. | ||
| 19 | |||
| 20 | ```yuescript | ||
| 21 | <= f | ||
| 22 | print @value | ||
| 23 | ``` | ||
| 24 | <YueDisplay> | ||
| 25 | |||
| 26 | ```yue | ||
| 27 | <= f | ||
| 28 | print @value | ||
| 29 | ``` | ||
| 30 | |||
| 31 | </YueDisplay> | ||
| 32 | |||
| 33 | You can specify a placeholder for where you want the backcall function to go as a parameter. | ||
| 34 | |||
| 35 | ```yuescript | ||
| 36 | (x) <- map _, [1, 2, 3] | ||
| 37 | x * 2 | ||
| 38 | ``` | ||
| 39 | <YueDisplay> | ||
| 40 | |||
| 41 | ```yue | ||
| 42 | (x) <- map _, [1, 2, 3] | ||
| 43 | x * 2 | ||
| 44 | ``` | ||
| 45 | |||
| 46 | </YueDisplay> | ||
| 47 | |||
| 48 | If you wish to have further code after your backcalls, you can set them aside with a do statement. And the parentheses can be omitted with non-fat arrow functions. | ||
| 49 | |||
| 50 | ```yuescript | ||
| 51 | result, msg = do | ||
| 52 | data <- readAsync "filename.txt" | ||
| 53 | print data | ||
| 54 | info <- processAsync data | ||
| 55 | check info | ||
| 56 | print result, msg | ||
| 57 | ``` | ||
| 58 | <YueDisplay> | ||
| 59 | |||
| 60 | ```yue | ||
| 61 | result, msg = do | ||
| 62 | data <- readAsync "filename.txt" | ||
| 63 | print data | ||
| 64 | info <- processAsync data | ||
| 65 | check info | ||
| 66 | print result, msg | ||
| 67 | ``` | ||
| 68 | |||
| 69 | </YueDisplay> | ||
diff --git a/doc/docs/de/doc/comment.md b/doc/docs/de/doc/comment.md new file mode 100644 index 0000000..b67c97d --- /dev/null +++ b/doc/docs/de/doc/comment.md | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | # Comment | ||
| 2 | |||
| 3 | ```yuescript | ||
| 4 | -- I am a comment | ||
| 5 | |||
| 6 | str = --[[ | ||
| 7 | This is a multi-line comment. | ||
| 8 | It's OK. | ||
| 9 | ]] strA \ -- comment 1 | ||
| 10 | .. strB \ -- comment 2 | ||
| 11 | .. strC | ||
| 12 | |||
| 13 | func --[[port]] 3000, --[[ip]] "192.168.1.1" | ||
| 14 | ``` | ||
| 15 | <YueDisplay> | ||
| 16 | |||
| 17 | ```yue | ||
| 18 | -- I am a comment | ||
| 19 | |||
| 20 | str = --[[ | ||
| 21 | This is a multi-line comment. | ||
| 22 | It's OK. | ||
| 23 | ]] strA \ -- comment 1 | ||
| 24 | .. strB \ -- comment 2 | ||
| 25 | .. strC | ||
| 26 | |||
| 27 | func --[[port]] 3000, --[[ip]] "192.168.1.1" | ||
| 28 | ``` | ||
| 29 | |||
| 30 | </YueDisplay> | ||
diff --git a/doc/docs/de/doc/comprehensions.md b/doc/docs/de/doc/comprehensions.md new file mode 100644 index 0000000..3a92167 --- /dev/null +++ b/doc/docs/de/doc/comprehensions.md | |||
| @@ -0,0 +1,271 @@ | |||
| 1 | # Comprehensions | ||
| 2 | |||
| 3 | Comprehensions provide a convenient syntax for constructing a new table by iterating over some existing object and applying an expression to its values. There are two kinds of comprehensions: list comprehensions and table comprehensions. They both produce Lua tables; list comprehensions accumulate values into an array-like table, and table comprehensions let you set both the key and the value on each iteration. | ||
| 4 | |||
| 5 | ## List Comprehensions | ||
| 6 | |||
| 7 | The following creates a copy of the items table but with all the values doubled. | ||
| 8 | |||
| 9 | ```yuescript | ||
| 10 | items = [ 1, 2, 3, 4 ] | ||
| 11 | doubled = [item * 2 for i, item in ipairs items] | ||
| 12 | ``` | ||
| 13 | <YueDisplay> | ||
| 14 | |||
| 15 | ```yue | ||
| 16 | items = [ 1, 2, 3, 4 ] | ||
| 17 | doubled = [item * 2 for i, item in ipairs items] | ||
| 18 | ``` | ||
| 19 | |||
| 20 | </YueDisplay> | ||
| 21 | |||
| 22 | The items included in the new table can be restricted with a when clause: | ||
| 23 | |||
| 24 | ```yuescript | ||
| 25 | slice = [item for i, item in ipairs items when i > 1 and i < 3] | ||
| 26 | ``` | ||
| 27 | <YueDisplay> | ||
| 28 | |||
| 29 | ```yue | ||
| 30 | slice = [item for i, item in ipairs items when i > 1 and i < 3] | ||
| 31 | ``` | ||
| 32 | |||
| 33 | </YueDisplay> | ||
| 34 | |||
| 35 | Because it is common to iterate over the values of a numerically indexed table, an **\*** operator is introduced. The doubled example can be rewritten as: | ||
| 36 | |||
| 37 | ```yuescript | ||
| 38 | doubled = [item * 2 for item in *items] | ||
| 39 | ``` | ||
| 40 | <YueDisplay> | ||
| 41 | |||
| 42 | ```yue | ||
| 43 | doubled = [item * 2 for item in *items] | ||
| 44 | ``` | ||
| 45 | |||
| 46 | </YueDisplay> | ||
| 47 | |||
| 48 | In list comprehensions, you can also use the spread operator `...` to flatten nested lists, achieving a flat map effect: | ||
| 49 | |||
| 50 | ```yuescript | ||
| 51 | data = | ||
| 52 | a: [1, 2, 3] | ||
| 53 | b: [4, 5, 6] | ||
| 54 | |||
| 55 | flat = [...v for k,v in pairs data] | ||
| 56 | -- flat is now [1, 2, 3, 4, 5, 6] | ||
| 57 | ``` | ||
| 58 | <YueDisplay> | ||
| 59 | |||
| 60 | ```yue | ||
| 61 | data = | ||
| 62 | a: [1, 2, 3] | ||
| 63 | b: [4, 5, 6] | ||
| 64 | |||
| 65 | flat = [...v for k,v in pairs data] | ||
| 66 | -- flat is now [1, 2, 3, 4, 5, 6] | ||
| 67 | ``` | ||
| 68 | |||
| 69 | </YueDisplay> | ||
| 70 | |||
| 71 | The for and when clauses can be chained as much as desired. The only requirement is that a comprehension has at least one for clause. | ||
| 72 | |||
| 73 | Using multiple for clauses is the same as using nested loops: | ||
| 74 | |||
| 75 | ```yuescript | ||
| 76 | x_coords = [4, 5, 6, 7] | ||
| 77 | y_coords = [9, 2, 3] | ||
| 78 | |||
| 79 | points = [ [x, y] for x in *x_coords \ | ||
| 80 | for y in *y_coords] | ||
| 81 | ``` | ||
| 82 | <YueDisplay> | ||
| 83 | |||
| 84 | ```yue | ||
| 85 | x_coords = [4, 5, 6, 7] | ||
| 86 | y_coords = [9, 2, 3] | ||
| 87 | |||
| 88 | points = [ [x, y] for x in *x_coords \ | ||
| 89 | for y in *y_coords] | ||
| 90 | ``` | ||
| 91 | |||
| 92 | </YueDisplay> | ||
| 93 | |||
| 94 | Numeric for loops can also be used in comprehensions: | ||
| 95 | |||
| 96 | ```yuescript | ||
| 97 | evens = [i for i = 1, 100 when i % 2 == 0] | ||
| 98 | ``` | ||
| 99 | <YueDisplay> | ||
| 100 | |||
| 101 | ```yue | ||
| 102 | evens = [i for i = 1, 100 when i % 2 == 0] | ||
| 103 | ``` | ||
| 104 | |||
| 105 | </YueDisplay> | ||
| 106 | |||
| 107 | ## Table Comprehensions | ||
| 108 | |||
| 109 | The syntax for table comprehensions is very similar, only differing by using **{** and **}** and taking two values from each iteration. | ||
| 110 | |||
| 111 | This example makes a copy of the tablething: | ||
| 112 | |||
| 113 | ```yuescript | ||
| 114 | thing = { | ||
| 115 | color: "red" | ||
| 116 | name: "fast" | ||
| 117 | width: 123 | ||
| 118 | } | ||
| 119 | |||
| 120 | thing_copy = {k, v for k, v in pairs thing} | ||
| 121 | ``` | ||
| 122 | <YueDisplay> | ||
| 123 | |||
| 124 | ```yue | ||
| 125 | thing = { | ||
| 126 | color: "red" | ||
| 127 | name: "fast" | ||
| 128 | width: 123 | ||
| 129 | } | ||
| 130 | |||
| 131 | thing_copy = {k, v for k, v in pairs thing} | ||
| 132 | ``` | ||
| 133 | |||
| 134 | </YueDisplay> | ||
| 135 | |||
| 136 | ```yuescript | ||
| 137 | no_color = {k, v for k, v in pairs thing when k != "color"} | ||
| 138 | ``` | ||
| 139 | <YueDisplay> | ||
| 140 | |||
| 141 | ```yue | ||
| 142 | no_color = {k, v for k, v in pairs thing when k != "color"} | ||
| 143 | ``` | ||
| 144 | |||
| 145 | </YueDisplay> | ||
| 146 | |||
| 147 | The **\*** operator is also supported. Here we create a square root look up table for a few numbers. | ||
| 148 | |||
| 149 | ```yuescript | ||
| 150 | numbers = [1, 2, 3, 4] | ||
| 151 | sqrts = {i, math.sqrt i for i in *numbers} | ||
| 152 | ``` | ||
| 153 | <YueDisplay> | ||
| 154 | |||
| 155 | ```yue | ||
| 156 | numbers = [1, 2, 3, 4] | ||
| 157 | sqrts = {i, math.sqrt i for i in *numbers} | ||
| 158 | ``` | ||
| 159 | |||
| 160 | </YueDisplay> | ||
| 161 | |||
| 162 | The key-value tuple in a table comprehension can also come from a single expression, in which case the expression should return two values. The first is used as the key and the second is used as the value: | ||
| 163 | |||
| 164 | In this example we convert an array of pairs to a table where the first item in the pair is the key and the second is the value. | ||
| 165 | |||
| 166 | ```yuescript | ||
| 167 | tuples = [ ["hello", "world"], ["foo", "bar"]] | ||
| 168 | tbl = {unpack tuple for tuple in *tuples} | ||
| 169 | ``` | ||
| 170 | <YueDisplay> | ||
| 171 | |||
| 172 | ```yue | ||
| 173 | tuples = [ ["hello", "world"], ["foo", "bar"]] | ||
| 174 | tbl = {unpack tuple for tuple in *tuples} | ||
| 175 | ``` | ||
| 176 | |||
| 177 | </YueDisplay> | ||
| 178 | |||
| 179 | ## Slicing | ||
| 180 | |||
| 181 | A special syntax is provided to restrict the items that are iterated over when using the **\*** operator. This is equivalent to setting the iteration bounds and a step size in a for loop. | ||
| 182 | |||
| 183 | Here we can set the minimum and maximum bounds, taking all items with indexes between 1 and 5 inclusive: | ||
| 184 | |||
| 185 | ```yuescript | ||
| 186 | slice = [item for item in *items[1, 5]] | ||
| 187 | ``` | ||
| 188 | <YueDisplay> | ||
| 189 | |||
| 190 | ```yue | ||
| 191 | slice = [item for item in *items[1, 5]] | ||
| 192 | ``` | ||
| 193 | |||
| 194 | </YueDisplay> | ||
| 195 | |||
| 196 | Any of the slice arguments can be left off to use a sensible default. In this example, if the max index is left off it defaults to the length of the table. This will take everything but the first element: | ||
| 197 | |||
| 198 | ```yuescript | ||
| 199 | slice = [item for item in *items[2,]] | ||
| 200 | ``` | ||
| 201 | <YueDisplay> | ||
| 202 | |||
| 203 | ```yue | ||
| 204 | slice = [item for item in *items[2,]] | ||
| 205 | ``` | ||
| 206 | |||
| 207 | </YueDisplay> | ||
| 208 | |||
| 209 | If the minimum bound is left out, it defaults to 1. Here we only provide a step size and leave the other bounds blank. This takes all odd indexed items: (1, 3, 5, …) | ||
| 210 | |||
| 211 | ```yuescript | ||
| 212 | slice = [item for item in *items[,,2]] | ||
| 213 | ``` | ||
| 214 | <YueDisplay> | ||
| 215 | |||
| 216 | ```yue | ||
| 217 | slice = [item for item in *items[,,2]] | ||
| 218 | ``` | ||
| 219 | |||
| 220 | </YueDisplay> | ||
| 221 | |||
| 222 | Both the minimum and maximum bounds can be negative, which means that the bounds are counted from the end of the table. | ||
| 223 | |||
| 224 | ```yuescript | ||
| 225 | -- take the last 4 items | ||
| 226 | slice = [item for item in *items[-4,-1]] | ||
| 227 | ``` | ||
| 228 | <YueDisplay> | ||
| 229 | |||
| 230 | ```yue | ||
| 231 | -- take the last 4 items | ||
| 232 | slice = [item for item in *items[-4,-1]] | ||
| 233 | ``` | ||
| 234 | |||
| 235 | </YueDisplay> | ||
| 236 | |||
| 237 | The step size can also be negative, which means that the items are taken in reverse order. | ||
| 238 | |||
| 239 | ```yuescript | ||
| 240 | reverse_slice = [item for item in *items[-1,1,-1]] | ||
| 241 | ``` | ||
| 242 | <YueDisplay> | ||
| 243 | |||
| 244 | ```yue | ||
| 245 | reverse_slice = [item for item in *items[-1,1,-1]] | ||
| 246 | ``` | ||
| 247 | |||
| 248 | </YueDisplay> | ||
| 249 | |||
| 250 | ### Slicing Expression | ||
| 251 | |||
| 252 | Slicing can also be used as an expression. This is useful for getting a sub-list of a table. | ||
| 253 | |||
| 254 | ```yuescript | ||
| 255 | -- take the 2nd and 4th items as a new list | ||
| 256 | sub_list = items[2, 4] | ||
| 257 | |||
| 258 | -- take the last 4 items | ||
| 259 | last_four_items = items[-4, -1] | ||
| 260 | ``` | ||
| 261 | <YueDisplay> | ||
| 262 | |||
| 263 | ```yue | ||
| 264 | -- take the 2nd and 4th items as a new list | ||
| 265 | sub_list = items[2, 4] | ||
| 266 | |||
| 267 | -- take the last 4 items | ||
| 268 | last_four_items = items[-4, -1] | ||
| 269 | ``` | ||
| 270 | |||
| 271 | </YueDisplay> | ||
diff --git a/doc/docs/de/doc/conditionals.md b/doc/docs/de/doc/conditionals.md new file mode 100644 index 0000000..5ba81cf --- /dev/null +++ b/doc/docs/de/doc/conditionals.md | |||
| @@ -0,0 +1,149 @@ | |||
| 1 | # Conditionals | ||
| 2 | |||
| 3 | ```yuescript | ||
| 4 | have_coins = false | ||
| 5 | if have_coins | ||
| 6 | print "Got coins" | ||
| 7 | else | ||
| 8 | print "No coins" | ||
| 9 | ``` | ||
| 10 | <YueDisplay> | ||
| 11 | |||
| 12 | ```yue | ||
| 13 | have_coins = false | ||
| 14 | if have_coins | ||
| 15 | print "Got coins" | ||
| 16 | else | ||
| 17 | print "No coins" | ||
| 18 | ``` | ||
| 19 | |||
| 20 | </YueDisplay> | ||
| 21 | |||
| 22 | A short syntax for single statements can also be used: | ||
| 23 | |||
| 24 | ```yuescript | ||
| 25 | have_coins = false | ||
| 26 | if have_coins then print "Got coins" else print "No coins" | ||
| 27 | ``` | ||
| 28 | <YueDisplay> | ||
| 29 | |||
| 30 | ```yue | ||
| 31 | have_coins = false | ||
| 32 | if have_coins then print "Got coins" else print "No coins" | ||
| 33 | ``` | ||
| 34 | |||
| 35 | </YueDisplay> | ||
| 36 | |||
| 37 | Because if statements can be used as expressions, this can also be written as: | ||
| 38 | |||
| 39 | ```yuescript | ||
| 40 | have_coins = false | ||
| 41 | print if have_coins then "Got coins" else "No coins" | ||
| 42 | ``` | ||
| 43 | <YueDisplay> | ||
| 44 | |||
| 45 | ```yue | ||
| 46 | have_coins = false | ||
| 47 | print if have_coins then "Got coins" else "No coins" | ||
| 48 | ``` | ||
| 49 | |||
| 50 | </YueDisplay> | ||
| 51 | |||
| 52 | Conditionals can also be used in return statements and assignments: | ||
| 53 | |||
| 54 | ```yuescript | ||
| 55 | is_tall = (name) -> | ||
| 56 | if name == "Rob" | ||
| 57 | true | ||
| 58 | else | ||
| 59 | false | ||
| 60 | |||
| 61 | message = if is_tall "Rob" | ||
| 62 | "I am very tall" | ||
| 63 | else | ||
| 64 | "I am not so tall" | ||
| 65 | |||
| 66 | print message -- prints: I am very tall | ||
| 67 | ``` | ||
| 68 | <YueDisplay> | ||
| 69 | |||
| 70 | ```yue | ||
| 71 | is_tall = (name) -> | ||
| 72 | if name == "Rob" | ||
| 73 | true | ||
| 74 | else | ||
| 75 | false | ||
| 76 | |||
| 77 | message = if is_tall "Rob" | ||
| 78 | "I am very tall" | ||
| 79 | else | ||
| 80 | "I am not so tall" | ||
| 81 | |||
| 82 | print message -- prints: I am very tall | ||
| 83 | ``` | ||
| 84 | |||
| 85 | </YueDisplay> | ||
| 86 | |||
| 87 | The opposite of if is unless: | ||
| 88 | |||
| 89 | ```yuescript | ||
| 90 | unless os.date("%A") == "Monday" | ||
| 91 | print "it is not Monday!" | ||
| 92 | ``` | ||
| 93 | <YueDisplay> | ||
| 94 | |||
| 95 | ```yue | ||
| 96 | unless os.date("%A") == "Monday" | ||
| 97 | print "it is not Monday!" | ||
| 98 | ``` | ||
| 99 | |||
| 100 | </YueDisplay> | ||
| 101 | |||
| 102 | ```yuescript | ||
| 103 | print "You're lucky!" unless math.random! > 0.1 | ||
| 104 | ``` | ||
| 105 | <YueDisplay> | ||
| 106 | |||
| 107 | ```yue | ||
| 108 | print "You're lucky!" unless math.random! > 0.1 | ||
| 109 | ``` | ||
| 110 | |||
| 111 | </YueDisplay> | ||
| 112 | |||
| 113 | ## In Expression | ||
| 114 | |||
| 115 | You can write range checking code with an `in-expression`. | ||
| 116 | |||
| 117 | ```yuescript | ||
| 118 | a = 5 | ||
| 119 | |||
| 120 | if a in [1, 3, 5, 7] | ||
| 121 | print "checking equality with discrete values" | ||
| 122 | |||
| 123 | if a in list | ||
| 124 | print "checking if `a` is in a list" | ||
| 125 | ``` | ||
| 126 | <YueDisplay> | ||
| 127 | |||
| 128 | ```yue | ||
| 129 | a = 5 | ||
| 130 | |||
| 131 | if a in [1, 3, 5, 7] | ||
| 132 | print "checking equality with discrete values" | ||
| 133 | |||
| 134 | if a in list | ||
| 135 | print "checking if `a` is in a list" | ||
| 136 | ``` | ||
| 137 | |||
| 138 | </YueDisplay> | ||
| 139 | |||
| 140 | ```yuescript | ||
| 141 | print "You're lucky!" unless math.random! > 0.1 | ||
| 142 | ``` | ||
| 143 | <YueDisplay> | ||
| 144 | |||
| 145 | ```yue | ||
| 146 | print "You're lucky!" unless math.random! > 0.1 | ||
| 147 | ``` | ||
| 148 | |||
| 149 | </YueDisplay> | ||
diff --git a/doc/docs/de/doc/continue.md b/doc/docs/de/doc/continue.md new file mode 100644 index 0000000..b000765 --- /dev/null +++ b/doc/docs/de/doc/continue.md | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | # Continue | ||
| 2 | |||
| 3 | A continue statement can be used to skip the current iteration in a loop. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | i = 0 | ||
| 7 | while i < 10 | ||
| 8 | i += 1 | ||
| 9 | continue if i % 2 == 0 | ||
| 10 | print i | ||
| 11 | ``` | ||
| 12 | <YueDisplay> | ||
| 13 | |||
| 14 | ```yue | ||
| 15 | i = 0 | ||
| 16 | while i < 10 | ||
| 17 | i += 1 | ||
| 18 | continue if i % 2 == 0 | ||
| 19 | print i | ||
| 20 | ``` | ||
| 21 | |||
| 22 | </YueDisplay> | ||
| 23 | |||
| 24 | continue can also be used with loop expressions to prevent that iteration from accumulating into the result. This examples filters the array table into just even numbers: | ||
| 25 | |||
| 26 | ```yuescript | ||
| 27 | my_numbers = [1, 2, 3, 4, 5, 6] | ||
| 28 | odds = for x in *my_numbers | ||
| 29 | continue if x % 2 == 1 | ||
| 30 | x | ||
| 31 | ``` | ||
| 32 | <YueDisplay> | ||
| 33 | |||
| 34 | ```yue | ||
| 35 | my_numbers = [1, 2, 3, 4, 5, 6] | ||
| 36 | odds = for x in *my_numbers | ||
| 37 | continue if x % 2 == 1 | ||
| 38 | x | ||
| 39 | ``` | ||
| 40 | |||
| 41 | </YueDisplay> | ||
diff --git a/doc/docs/de/doc/destructuring-assignment.md b/doc/docs/de/doc/destructuring-assignment.md new file mode 100644 index 0000000..e7b8046 --- /dev/null +++ b/doc/docs/de/doc/destructuring-assignment.md | |||
| @@ -0,0 +1,240 @@ | |||
| 1 | # Destructuring Assignment | ||
| 2 | |||
| 3 | Destructuring assignment is a way to quickly extract values from a table by their name or position in array based tables. | ||
| 4 | |||
| 5 | Typically when you see a table literal, {1,2,3}, it is on the right hand side of an assignment because it is a value. Destructuring assignment swaps the role of the table literal, and puts it on the left hand side of an assign statement. | ||
| 6 | |||
| 7 | This is best explained with examples. Here is how you would unpack the first two values from a table: | ||
| 8 | |||
| 9 | ```yuescript | ||
| 10 | thing = [1, 2] | ||
| 11 | |||
| 12 | [a, b] = thing | ||
| 13 | print a, b | ||
| 14 | ``` | ||
| 15 | <YueDisplay> | ||
| 16 | |||
| 17 | ```yue | ||
| 18 | thing = [1, 2] | ||
| 19 | |||
| 20 | [a, b] = thing | ||
| 21 | print a, b | ||
| 22 | ``` | ||
| 23 | |||
| 24 | </YueDisplay> | ||
| 25 | |||
| 26 | In the destructuring table literal, the key represents the key to read from the right hand side, and the value represents the name the read value will be assigned to. | ||
| 27 | |||
| 28 | ```yuescript | ||
| 29 | obj = { | ||
| 30 | hello: "world" | ||
| 31 | day: "tuesday" | ||
| 32 | length: 20 | ||
| 33 | } | ||
| 34 | |||
| 35 | {hello: hello, day: the_day} = obj | ||
| 36 | print hello, the_day | ||
| 37 | |||
| 38 | :day = obj -- OK to do simple destructuring without braces | ||
| 39 | ``` | ||
| 40 | <YueDisplay> | ||
| 41 | |||
| 42 | ```yue | ||
| 43 | obj = { | ||
| 44 | hello: "world" | ||
| 45 | day: "tuesday" | ||
| 46 | length: 20 | ||
| 47 | } | ||
| 48 | |||
| 49 | {hello: hello, day: the_day} = obj | ||
| 50 | print hello, the_day | ||
| 51 | |||
| 52 | :day = obj -- OK to do simple destructuring without braces | ||
| 53 | ``` | ||
| 54 | |||
| 55 | </YueDisplay> | ||
| 56 | |||
| 57 | This also works with nested data structures as well: | ||
| 58 | |||
| 59 | ```yuescript | ||
| 60 | obj2 = { | ||
| 61 | numbers: [1, 2, 3, 4] | ||
| 62 | properties: { | ||
| 63 | color: "green" | ||
| 64 | height: 13.5 | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | {numbers: [first, second], properties: {color: color}} = obj2 | ||
| 69 | print first, second, color | ||
| 70 | ``` | ||
| 71 | <YueDisplay> | ||
| 72 | |||
| 73 | ```yue | ||
| 74 | obj2 = { | ||
| 75 | numbers: [1, 2, 3, 4] | ||
| 76 | properties: { | ||
| 77 | color: "green" | ||
| 78 | height: 13.5 | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | {numbers: [first, second], properties: {color: color}} = obj2 | ||
| 83 | print first, second, color | ||
| 84 | ``` | ||
| 85 | |||
| 86 | </YueDisplay> | ||
| 87 | |||
| 88 | If the destructuring statement is complicated, feel free to spread it out over a few lines. A slightly more complicated example: | ||
| 89 | |||
| 90 | ```yuescript | ||
| 91 | { | ||
| 92 | numbers: [first, second] | ||
| 93 | properties: { | ||
| 94 | color: color | ||
| 95 | } | ||
| 96 | } = obj2 | ||
| 97 | ``` | ||
| 98 | <YueDisplay> | ||
| 99 | |||
| 100 | ```yue | ||
| 101 | { | ||
| 102 | numbers: [first, second] | ||
| 103 | properties: { | ||
| 104 | color: color | ||
| 105 | } | ||
| 106 | } = obj2 | ||
| 107 | ``` | ||
| 108 | |||
| 109 | </YueDisplay> | ||
| 110 | |||
| 111 | It's common to extract values from at table and assign them the local variables that have the same name as the key. In order to avoid repetition we can use the **:** prefix operator: | ||
| 112 | |||
| 113 | ```yuescript | ||
| 114 | {:concat, :insert} = table | ||
| 115 | ``` | ||
| 116 | <YueDisplay> | ||
| 117 | |||
| 118 | ```yue | ||
| 119 | {:concat, :insert} = table | ||
| 120 | ``` | ||
| 121 | |||
| 122 | </YueDisplay> | ||
| 123 | |||
| 124 | This is effectively the same as import, but we can rename fields we want to extract by mixing the syntax: | ||
| 125 | |||
| 126 | ```yuescript | ||
| 127 | {:mix, :max, random: rand} = math | ||
| 128 | ``` | ||
| 129 | <YueDisplay> | ||
| 130 | |||
| 131 | ```yue | ||
| 132 | {:mix, :max, random: rand} = math | ||
| 133 | ``` | ||
| 134 | |||
| 135 | </YueDisplay> | ||
| 136 | |||
| 137 | You can write default values while doing destructuring like: | ||
| 138 | |||
| 139 | ```yuescript | ||
| 140 | {:name = "nameless", :job = "jobless"} = person | ||
| 141 | ``` | ||
| 142 | <YueDisplay> | ||
| 143 | |||
| 144 | ```yue | ||
| 145 | {:name = "nameless", :job = "jobless"} = person | ||
| 146 | ``` | ||
| 147 | |||
| 148 | </YueDisplay> | ||
| 149 | |||
| 150 | You can use `_` as placeholder when doing a list destructuring: | ||
| 151 | |||
| 152 | ```yuescript | ||
| 153 | [_, two, _, four] = items | ||
| 154 | ``` | ||
| 155 | <YueDisplay> | ||
| 156 | |||
| 157 | ```yue | ||
| 158 | [_, two, _, four] = items | ||
| 159 | ``` | ||
| 160 | |||
| 161 | </YueDisplay> | ||
| 162 | |||
| 163 | ## Range Destructuring | ||
| 164 | |||
| 165 | You can use the spread operator `...` in list destructuring to capture a range of values. This is useful when you want to extract specific elements from the beginning and end of a list while collecting the rest in between. | ||
| 166 | |||
| 167 | ```yuescript | ||
| 168 | orders = ["first", "second", "third", "fourth", "last"] | ||
| 169 | [first, ...bulk, last] = orders | ||
| 170 | print first -- prints: first | ||
| 171 | print bulk -- prints: {"second", "third", "fourth"} | ||
| 172 | print last -- prints: last | ||
| 173 | ``` | ||
| 174 | <YueDisplay> | ||
| 175 | |||
| 176 | ```yue | ||
| 177 | orders = ["first", "second", "third", "fourth", "last"] | ||
| 178 | [first, ...bulk, last] = orders | ||
| 179 | print first -- prints: first | ||
| 180 | print bulk -- prints: {"second", "third", "fourth"} | ||
| 181 | print last -- prints: last | ||
| 182 | ``` | ||
| 183 | |||
| 184 | </YueDisplay> | ||
| 185 | |||
| 186 | The spread operator can be used in different positions to capture different ranges, and you can use `_` as a placeholder for the values you don't want to capture: | ||
| 187 | |||
| 188 | ```yuescript | ||
| 189 | -- Capture everything after first element | ||
| 190 | [first, ...rest] = orders | ||
| 191 | |||
| 192 | -- Capture everything before last element | ||
| 193 | [...start, last] = orders | ||
| 194 | |||
| 195 | -- Capture things except the middle elements | ||
| 196 | [first, ..._, last] = orders | ||
| 197 | ``` | ||
| 198 | <YueDisplay> | ||
| 199 | |||
| 200 | ```yue | ||
| 201 | -- Capture everything after first element | ||
| 202 | [first, ...rest] = orders | ||
| 203 | |||
| 204 | -- Capture everything before last element | ||
| 205 | [...start, last] = orders | ||
| 206 | |||
| 207 | -- Capture things except the middle elements | ||
| 208 | [first, ..._, last] = orders | ||
| 209 | ``` | ||
| 210 | |||
| 211 | </YueDisplay> | ||
| 212 | |||
| 213 | ## Destructuring In Other Places | ||
| 214 | |||
| 215 | Destructuring can also show up in places where an assignment implicitly takes place. An example of this is a for loop: | ||
| 216 | |||
| 217 | ```yuescript | ||
| 218 | tuples = [ | ||
| 219 | ["hello", "world"] | ||
| 220 | ["egg", "head"] | ||
| 221 | ] | ||
| 222 | |||
| 223 | for [left, right] in *tuples | ||
| 224 | print left, right | ||
| 225 | ``` | ||
| 226 | <YueDisplay> | ||
| 227 | |||
| 228 | ```yue | ||
| 229 | tuples = [ | ||
| 230 | ["hello", "world"] | ||
| 231 | ["egg", "head"] | ||
| 232 | ] | ||
| 233 | |||
| 234 | for [left, right] in *tuples | ||
| 235 | print left, right | ||
| 236 | ``` | ||
| 237 | |||
| 238 | </YueDisplay> | ||
| 239 | |||
| 240 | We know each element in the array table is a two item tuple, so we can unpack it directly in the names clause of the for statement using a destructure. | ||
diff --git a/doc/docs/de/doc/do.md b/doc/docs/de/doc/do.md new file mode 100644 index 0000000..4990d6f --- /dev/null +++ b/doc/docs/de/doc/do.md | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | # Do | ||
| 2 | |||
| 3 | When used as a statement, do works just like it does in Lua. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | do | ||
| 7 | var = "hello" | ||
| 8 | print var | ||
| 9 | print var -- nil here | ||
| 10 | ``` | ||
| 11 | <YueDisplay> | ||
| 12 | |||
| 13 | ```yue | ||
| 14 | do | ||
| 15 | var = "hello" | ||
| 16 | print var | ||
| 17 | print var -- nil here | ||
| 18 | ``` | ||
| 19 | |||
| 20 | </YueDisplay> | ||
| 21 | |||
| 22 | 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. | ||
| 23 | |||
| 24 | ```yuescript | ||
| 25 | counter = do | ||
| 26 | i = 0 | ||
| 27 | -> | ||
| 28 | i += 1 | ||
| 29 | i | ||
| 30 | |||
| 31 | print counter! | ||
| 32 | print counter! | ||
| 33 | ``` | ||
| 34 | <YueDisplay> | ||
| 35 | |||
| 36 | ```yue | ||
| 37 | counter = do | ||
| 38 | i = 0 | ||
| 39 | -> | ||
| 40 | i += 1 | ||
| 41 | i | ||
| 42 | |||
| 43 | print counter! | ||
| 44 | print counter! | ||
| 45 | ``` | ||
| 46 | |||
| 47 | </YueDisplay> | ||
| 48 | |||
| 49 | ```yuescript | ||
| 50 | tbl = { | ||
| 51 | key: do | ||
| 52 | print "assigning key!" | ||
| 53 | 1234 | ||
| 54 | } | ||
| 55 | ``` | ||
| 56 | <YueDisplay> | ||
| 57 | |||
| 58 | ```yue | ||
| 59 | tbl = { | ||
| 60 | key: do | ||
| 61 | print "assigning key!" | ||
| 62 | 1234 | ||
| 63 | } | ||
| 64 | ``` | ||
| 65 | |||
| 66 | </YueDisplay> | ||
diff --git a/doc/docs/de/doc/for-loop.md b/doc/docs/de/doc/for-loop.md new file mode 100644 index 0000000..cabcde5 --- /dev/null +++ b/doc/docs/de/doc/for-loop.md | |||
| @@ -0,0 +1,127 @@ | |||
| 1 | # For Loop | ||
| 2 | |||
| 3 | There are two for loop forms, just like in Lua. A numeric one and a generic one: | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | for i = 10, 20 | ||
| 7 | print i | ||
| 8 | |||
| 9 | for k = 1, 15, 2 -- an optional step provided | ||
| 10 | print k | ||
| 11 | |||
| 12 | for key, value in pairs object | ||
| 13 | print key, value | ||
| 14 | ``` | ||
| 15 | <YueDisplay> | ||
| 16 | |||
| 17 | ```yue | ||
| 18 | for i = 10, 20 | ||
| 19 | print i | ||
| 20 | |||
| 21 | for k = 1, 15, 2 -- an optional step provided | ||
| 22 | print k | ||
| 23 | |||
| 24 | for key, value in pairs object | ||
| 25 | print key, value | ||
| 26 | ``` | ||
| 27 | |||
| 28 | </YueDisplay> | ||
| 29 | |||
| 30 | The slicing and **\*** operators can be used, just like with comprehensions: | ||
| 31 | |||
| 32 | ```yuescript | ||
| 33 | for item in *items[2, 4] | ||
| 34 | print item | ||
| 35 | ``` | ||
| 36 | <YueDisplay> | ||
| 37 | |||
| 38 | ```yue | ||
| 39 | for item in *items[2, 4] | ||
| 40 | print item | ||
| 41 | ``` | ||
| 42 | |||
| 43 | </YueDisplay> | ||
| 44 | |||
| 45 | A shorter syntax is also available for all variations when the body is only a single line: | ||
| 46 | |||
| 47 | ```yuescript | ||
| 48 | for item in *items do print item | ||
| 49 | |||
| 50 | for j = 1, 10, 3 do print j | ||
| 51 | ``` | ||
| 52 | <YueDisplay> | ||
| 53 | |||
| 54 | ```yue | ||
| 55 | for item in *items do print item | ||
| 56 | |||
| 57 | for j = 1, 10, 3 do print j | ||
| 58 | ``` | ||
| 59 | |||
| 60 | </YueDisplay> | ||
| 61 | |||
| 62 | A for loop can also be used as an expression. The last statement in the body of the for loop is coerced into an expression and appended to an accumulating array table. | ||
| 63 | |||
| 64 | Doubling every even number: | ||
| 65 | |||
| 66 | ```yuescript | ||
| 67 | doubled_evens = for i = 1, 20 | ||
| 68 | if i % 2 == 0 | ||
| 69 | i * 2 | ||
| 70 | else | ||
| 71 | i | ||
| 72 | ``` | ||
| 73 | <YueDisplay> | ||
| 74 | |||
| 75 | ```yue | ||
| 76 | doubled_evens = for i = 1, 20 | ||
| 77 | if i % 2 == 0 | ||
| 78 | i * 2 | ||
| 79 | else | ||
| 80 | i | ||
| 81 | ``` | ||
| 82 | |||
| 83 | </YueDisplay> | ||
| 84 | |||
| 85 | 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. | ||
| 86 | |||
| 87 | For example, to find the first number greater than 10: | ||
| 88 | |||
| 89 | ```yuescript | ||
| 90 | first_large = for n in *numbers | ||
| 91 | break n if n > 10 | ||
| 92 | ``` | ||
| 93 | <YueDisplay> | ||
| 94 | |||
| 95 | ```yue | ||
| 96 | first_large = for n in *numbers | ||
| 97 | break n if n > 10 | ||
| 98 | ``` | ||
| 99 | |||
| 100 | </YueDisplay> | ||
| 101 | |||
| 102 | This break-with-value syntax enables concise and expressive search or early-exit patterns directly within loop expressions. | ||
| 103 | |||
| 104 | You can also filter values by combining the for loop expression with the continue statement. | ||
| 105 | |||
| 106 | 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. | ||
| 107 | |||
| 108 | ```yuescript | ||
| 109 | func_a = -> for i = 1, 10 do print i | ||
| 110 | func_b = -> return for i = 1, 10 do i | ||
| 111 | |||
| 112 | print func_a! -- prints nil | ||
| 113 | print func_b! -- prints table object | ||
| 114 | ``` | ||
| 115 | <YueDisplay> | ||
| 116 | |||
| 117 | ```yue | ||
| 118 | func_a = -> for i = 1, 10 do print i | ||
| 119 | func_b = -> return for i = 1, 10 do i | ||
| 120 | |||
| 121 | print func_a! -- prints nil | ||
| 122 | print func_b! -- prints table object | ||
| 123 | ``` | ||
| 124 | |||
| 125 | </YueDisplay> | ||
| 126 | |||
| 127 | This is done to avoid the needless creation of tables for functions that don't need to return the results of the loop. | ||
diff --git a/doc/docs/de/doc/function-literals.md b/doc/docs/de/doc/function-literals.md new file mode 100644 index 0000000..316e07c --- /dev/null +++ b/doc/docs/de/doc/function-literals.md | |||
| @@ -0,0 +1,494 @@ | |||
| 1 | # Function Literals | ||
| 2 | |||
| 3 | All functions are created using a function expression. A simple function is denoted using the arrow: **->**. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | my_function = -> | ||
| 7 | my_function() -- call the empty function | ||
| 8 | ``` | ||
| 9 | <YueDisplay> | ||
| 10 | |||
| 11 | ```yue | ||
| 12 | my_function = -> | ||
| 13 | my_function() -- call the empty function | ||
| 14 | ``` | ||
| 15 | |||
| 16 | </YueDisplay> | ||
| 17 | |||
| 18 | The body of the function can either be one statement placed directly after the arrow, or it can be a series of statements indented on the following lines: | ||
| 19 | |||
| 20 | ```yuescript | ||
| 21 | func_a = -> print "hello world" | ||
| 22 | |||
| 23 | func_b = -> | ||
| 24 | value = 100 | ||
| 25 | print "The value:", value | ||
| 26 | ``` | ||
| 27 | <YueDisplay> | ||
| 28 | |||
| 29 | ```yue | ||
| 30 | func_a = -> print "hello world" | ||
| 31 | |||
| 32 | func_b = -> | ||
| 33 | value = 100 | ||
| 34 | print "The value:", value | ||
| 35 | ``` | ||
| 36 | |||
| 37 | </YueDisplay> | ||
| 38 | |||
| 39 | If a function has no arguments, it can be called using the ! operator, instead of empty parentheses. The ! invocation is the preferred way to call functions with no arguments. | ||
| 40 | |||
| 41 | ```yuescript | ||
| 42 | func_a! | ||
| 43 | func_b() | ||
| 44 | ``` | ||
| 45 | <YueDisplay> | ||
| 46 | |||
| 47 | ```yue | ||
| 48 | func_a! | ||
| 49 | func_b() | ||
| 50 | ``` | ||
| 51 | |||
| 52 | </YueDisplay> | ||
| 53 | |||
| 54 | Functions with arguments can be created by preceding the arrow with a list of argument names in parentheses: | ||
| 55 | |||
| 56 | ```yuescript | ||
| 57 | sum = (x, y) -> print "sum", x + y | ||
| 58 | ``` | ||
| 59 | <YueDisplay> | ||
| 60 | |||
| 61 | ```yue | ||
| 62 | sum = (x, y) -> print "sum", x + y | ||
| 63 | ``` | ||
| 64 | |||
| 65 | </YueDisplay> | ||
| 66 | |||
| 67 | Functions can be called by listing the arguments after the name of an expression that evaluates to a function. When chaining together function calls, the arguments are applied to the closest function to the left. | ||
| 68 | |||
| 69 | ```yuescript | ||
| 70 | sum 10, 20 | ||
| 71 | print sum 10, 20 | ||
| 72 | |||
| 73 | a b c "a", "b", "c" | ||
| 74 | ``` | ||
| 75 | <YueDisplay> | ||
| 76 | |||
| 77 | ```yue | ||
| 78 | sum 10, 20 | ||
| 79 | print sum 10, 20 | ||
| 80 | |||
| 81 | a b c "a", "b", "c" | ||
| 82 | ``` | ||
| 83 | |||
| 84 | </YueDisplay> | ||
| 85 | |||
| 86 | In order to avoid ambiguity in when calling functions, parentheses can also be used to surround the arguments. This is required here in order to make sure the right arguments get sent to the right functions. | ||
| 87 | |||
| 88 | ```yuescript | ||
| 89 | print "x:", sum(10, 20), "y:", sum(30, 40) | ||
| 90 | ``` | ||
| 91 | <YueDisplay> | ||
| 92 | |||
| 93 | ```yue | ||
| 94 | print "x:", sum(10, 20), "y:", sum(30, 40) | ||
| 95 | ``` | ||
| 96 | |||
| 97 | </YueDisplay> | ||
| 98 | |||
| 99 | There must not be any space between the opening parenthesis and the function. | ||
| 100 | |||
| 101 | Functions will coerce the last statement in their body into a return statement, this is called implicit return: | ||
| 102 | |||
| 103 | ```yuescript | ||
| 104 | sum = (x, y) -> x + y | ||
| 105 | print "The sum is ", sum 10, 20 | ||
| 106 | ``` | ||
| 107 | <YueDisplay> | ||
| 108 | |||
| 109 | ```yue | ||
| 110 | sum = (x, y) -> x + y | ||
| 111 | print "The sum is ", sum 10, 20 | ||
| 112 | ``` | ||
| 113 | |||
| 114 | </YueDisplay> | ||
| 115 | |||
| 116 | And if you need to explicitly return, you can use the return keyword: | ||
| 117 | |||
| 118 | ```yuescript | ||
| 119 | sum = (x, y) -> return x + y | ||
| 120 | ``` | ||
| 121 | <YueDisplay> | ||
| 122 | |||
| 123 | ```yue | ||
| 124 | sum = (x, y) -> return x + y | ||
| 125 | ``` | ||
| 126 | |||
| 127 | </YueDisplay> | ||
| 128 | |||
| 129 | Just like in Lua, functions can return multiple values. The last statement must be a list of values separated by commas: | ||
| 130 | |||
| 131 | ```yuescript | ||
| 132 | mystery = (x, y) -> x + y, x - y | ||
| 133 | a, b = mystery 10, 20 | ||
| 134 | ``` | ||
| 135 | <YueDisplay> | ||
| 136 | |||
| 137 | ```yue | ||
| 138 | mystery = (x, y) -> x + y, x - y | ||
| 139 | a, b = mystery 10, 20 | ||
| 140 | ``` | ||
| 141 | |||
| 142 | </YueDisplay> | ||
| 143 | |||
| 144 | ## Fat Arrows | ||
| 145 | |||
| 146 | Because it is an idiom in Lua to send an object as the first argument when calling a method, a special syntax is provided for creating functions which automatically includes a self argument. | ||
| 147 | |||
| 148 | ```yuescript | ||
| 149 | func = (num) => @value + num | ||
| 150 | ``` | ||
| 151 | <YueDisplay> | ||
| 152 | |||
| 153 | ```yue | ||
| 154 | func = (num) => @value + num | ||
| 155 | ``` | ||
| 156 | |||
| 157 | </YueDisplay> | ||
| 158 | |||
| 159 | ## Argument Defaults | ||
| 160 | |||
| 161 | It is possible to provide default values for the arguments of a function. An argument is determined to be empty if its value is nil. Any nil arguments that have a default value will be replace before the body of the function is run. | ||
| 162 | |||
| 163 | ```yuescript | ||
| 164 | my_function = (name = "something", height = 100) -> | ||
| 165 | print "Hello I am", name | ||
| 166 | print "My height is", height | ||
| 167 | ``` | ||
| 168 | <YueDisplay> | ||
| 169 | |||
| 170 | ```yue | ||
| 171 | my_function = (name = "something", height = 100) -> | ||
| 172 | print "Hello I am", name | ||
| 173 | print "My height is", height | ||
| 174 | ``` | ||
| 175 | |||
| 176 | </YueDisplay> | ||
| 177 | |||
| 178 | An argument default value expression is evaluated in the body of the function in the order of the argument declarations. For this reason default values have access to previously declared arguments. | ||
| 179 | |||
| 180 | ```yuescript | ||
| 181 | some_args = (x = 100, y = x + 1000) -> | ||
| 182 | print x + y | ||
| 183 | ``` | ||
| 184 | <YueDisplay> | ||
| 185 | |||
| 186 | ```yue | ||
| 187 | some_args = (x = 100, y = x + 1000) -> | ||
| 188 | print x + y | ||
| 189 | ``` | ||
| 190 | |||
| 191 | </YueDisplay> | ||
| 192 | |||
| 193 | ## Considerations | ||
| 194 | |||
| 195 | Because of the expressive parentheses-less way of calling functions, some restrictions must be put in place to avoid parsing ambiguity involving whitespace. | ||
| 196 | |||
| 197 | The minus sign plays two roles, a unary negation operator and a binary subtraction operator. Consider how the following examples compile: | ||
| 198 | |||
| 199 | ```yuescript | ||
| 200 | a = x - 10 | ||
| 201 | b = x-10 | ||
| 202 | c = x -y | ||
| 203 | d = x- z | ||
| 204 | ``` | ||
| 205 | <YueDisplay> | ||
| 206 | |||
| 207 | ```yue | ||
| 208 | a = x - 10 | ||
| 209 | b = x-10 | ||
| 210 | c = x -y | ||
| 211 | d = x- z | ||
| 212 | ``` | ||
| 213 | |||
| 214 | </YueDisplay> | ||
| 215 | |||
| 216 | The precedence of the first argument of a function call can be controlled using whitespace if the argument is a literal string. In Lua, it is common to leave off parentheses when calling a function with a single string or table literal. | ||
| 217 | |||
| 218 | When there is no space between a variable and a string literal, the function call takes precedence over any following expressions. No other arguments can be passed to the function when it is called this way. | ||
| 219 | |||
| 220 | Where there is a space following a variable and a string literal, the function call acts as show above. The string literal belongs to any following expressions (if they exist), which serves as the argument list. | ||
| 221 | |||
| 222 | ```yuescript | ||
| 223 | x = func"hello" + 100 | ||
| 224 | y = func "hello" + 100 | ||
| 225 | ``` | ||
| 226 | <YueDisplay> | ||
| 227 | |||
| 228 | ```yue | ||
| 229 | x = func"hello" + 100 | ||
| 230 | y = func "hello" + 100 | ||
| 231 | ``` | ||
| 232 | |||
| 233 | </YueDisplay> | ||
| 234 | |||
| 235 | ## Multi-line arguments | ||
| 236 | |||
| 237 | When calling functions that take a large number of arguments, it is convenient to split the argument list over multiple lines. Because of the white-space sensitive nature of the language, care must be taken when splitting up the argument list. | ||
| 238 | |||
| 239 | If an argument list is to be continued onto the next line, the current line must end in a comma. And the following line must be indented more than the current indentation. Once indented, all other argument lines must be at the same level of indentation to be part of the argument list | ||
| 240 | |||
| 241 | ```yuescript | ||
| 242 | my_func 5, 4, 3, | ||
| 243 | 8, 9, 10 | ||
| 244 | |||
| 245 | cool_func 1, 2, | ||
| 246 | 3, 4, | ||
| 247 | 5, 6, | ||
| 248 | 7, 8 | ||
| 249 | ``` | ||
| 250 | <YueDisplay> | ||
| 251 | |||
| 252 | ```yue | ||
| 253 | my_func 5, 4, 3, | ||
| 254 | 8, 9, 10 | ||
| 255 | |||
| 256 | cool_func 1, 2, | ||
| 257 | 3, 4, | ||
| 258 | 5, 6, | ||
| 259 | 7, 8 | ||
| 260 | ``` | ||
| 261 | |||
| 262 | </YueDisplay> | ||
| 263 | |||
| 264 | This type of invocation can be nested. The level of indentation is used to determine to which function the arguments belong to. | ||
| 265 | |||
| 266 | ```yuescript | ||
| 267 | my_func 5, 6, 7, | ||
| 268 | 6, another_func 6, 7, 8, | ||
| 269 | 9, 1, 2, | ||
| 270 | 5, 4 | ||
| 271 | ``` | ||
| 272 | <YueDisplay> | ||
| 273 | |||
| 274 | ```yue | ||
| 275 | my_func 5, 6, 7, | ||
| 276 | 6, another_func 6, 7, 8, | ||
| 277 | 9, 1, 2, | ||
| 278 | 5, 4 | ||
| 279 | ``` | ||
| 280 | |||
| 281 | </YueDisplay> | ||
| 282 | |||
| 283 | Because tables also use the comma as a delimiter, this indentation syntax is helpful for letting values be part of the argument list instead of being part of the table. | ||
| 284 | |||
| 285 | ```yuescript | ||
| 286 | x = [ | ||
| 287 | 1, 2, 3, 4, a_func 4, 5, | ||
| 288 | 5, 6, | ||
| 289 | 8, 9, 10 | ||
| 290 | ] | ||
| 291 | ``` | ||
| 292 | <YueDisplay> | ||
| 293 | |||
| 294 | ```yue | ||
| 295 | x = [ | ||
| 296 | 1, 2, 3, 4, a_func 4, 5, | ||
| 297 | 5, 6, | ||
| 298 | 8, 9, 10 | ||
| 299 | ] | ||
| 300 | ``` | ||
| 301 | |||
| 302 | </YueDisplay> | ||
| 303 | |||
| 304 | Although uncommon, notice how we can give a deeper indentation for function arguments if we know we will be using a lower indentation further on. | ||
| 305 | |||
| 306 | ```yuescript | ||
| 307 | y = [ my_func 1, 2, 3, | ||
| 308 | 4, 5, | ||
| 309 | 5, 6, 7 | ||
| 310 | ] | ||
| 311 | ``` | ||
| 312 | <YueDisplay> | ||
| 313 | |||
| 314 | ```yue | ||
| 315 | y = [ my_func 1, 2, 3, | ||
| 316 | 4, 5, | ||
| 317 | 5, 6, 7 | ||
| 318 | ] | ||
| 319 | ``` | ||
| 320 | |||
| 321 | </YueDisplay> | ||
| 322 | |||
| 323 | The same thing can be done with other block level statements like conditionals. We can use indentation level to determine what statement a value belongs to: | ||
| 324 | |||
| 325 | ```yuescript | ||
| 326 | if func 1, 2, 3, | ||
| 327 | "hello", | ||
| 328 | "world" | ||
| 329 | print "hello" | ||
| 330 | print "I am inside if" | ||
| 331 | |||
| 332 | if func 1, 2, 3, | ||
| 333 | "hello", | ||
| 334 | "world" | ||
| 335 | print "hello" | ||
| 336 | print "I am inside if" | ||
| 337 | ``` | ||
| 338 | <YueDisplay> | ||
| 339 | |||
| 340 | ```yue | ||
| 341 | if func 1, 2, 3, | ||
| 342 | "hello", | ||
| 343 | "world" | ||
| 344 | print "hello" | ||
| 345 | print "I am inside if" | ||
| 346 | |||
| 347 | if func 1, 2, 3, | ||
| 348 | "hello", | ||
| 349 | "world" | ||
| 350 | print "hello" | ||
| 351 | print "I am inside if" | ||
| 352 | ``` | ||
| 353 | |||
| 354 | </YueDisplay> | ||
| 355 | |||
| 356 | ## Parameter Destructuring | ||
| 357 | |||
| 358 | YueScript now supports destructuring function parameters when the argument is an object. Two forms of destructuring table literals are available: | ||
| 359 | |||
| 360 | * **Curly-brace wrapped literals/object parameters**, allowing optional default values when fields are missing (e.g., `{:a, :b}`, `{a: a1 = 123}`). | ||
| 361 | |||
| 362 | * **Unwrapped simple table syntax**, starting with a sequence of key-value or shorthand bindings and continuing until another expression terminates it (e.g., `:a, b: b1, :c`). This form extracts multiple fields from the same object. | ||
| 363 | |||
| 364 | ```yuescript | ||
| 365 | f1 = (:a, :b, :c) -> | ||
| 366 | print a, b, c | ||
| 367 | |||
| 368 | f1 a: 1, b: "2", c: {} | ||
| 369 | |||
| 370 | f2 = ({a: a1 = 123, :b = 'abc'}, c = {}) -> | ||
| 371 | print a1, b, c | ||
| 372 | |||
| 373 | arg1 = {a: 0} | ||
| 374 | f2 arg1, arg2 | ||
| 375 | ``` | ||
| 376 | <YueDisplay> | ||
| 377 | |||
| 378 | ```yue | ||
| 379 | f1 = (:a, :b, :c) -> | ||
| 380 | print a, b, c | ||
| 381 | |||
| 382 | f1 a: 1, b: "2", c: {} | ||
| 383 | |||
| 384 | f2 = ({a: a1 = 123, :b = 'abc'}, c = {}) -> | ||
| 385 | print a1, b, c | ||
| 386 | |||
| 387 | arg1 = {a: 0} | ||
| 388 | f2 arg1, arg2 | ||
| 389 | ``` | ||
| 390 | |||
| 391 | </YueDisplay> | ||
| 392 | |||
| 393 | ## Prefixed Return Expression | ||
| 394 | |||
| 395 | When working with deeply nested function bodies, it can be tedious to maintain readability and consistency of the return value. To address this, YueScript introduces the **Prefixed Return Expression** syntax. Its form is as follows: | ||
| 396 | |||
| 397 | ```yuescript | ||
| 398 | findFirstEven = (list): nil -> | ||
| 399 | for item in *list | ||
| 400 | if type(item) == "table" | ||
| 401 | for sub in *item | ||
| 402 | if sub % 2 == 0 | ||
| 403 | return sub | ||
| 404 | ``` | ||
| 405 | <YueDisplay> | ||
| 406 | |||
| 407 | ```yue | ||
| 408 | findFirstEven = (list): nil -> | ||
| 409 | for item in *list | ||
| 410 | if type(item) == "table" | ||
| 411 | for sub in *item | ||
| 412 | if sub % 2 == 0 | ||
| 413 | return sub | ||
| 414 | ``` | ||
| 415 | |||
| 416 | </YueDisplay> | ||
| 417 | |||
| 418 | This is equivalent to: | ||
| 419 | |||
| 420 | ```yuescript | ||
| 421 | findFirstEven = (list) -> | ||
| 422 | for item in *list | ||
| 423 | if type(item) == "table" | ||
| 424 | for sub in *item | ||
| 425 | if sub % 2 == 0 | ||
| 426 | return sub | ||
| 427 | nil | ||
| 428 | ``` | ||
| 429 | <YueDisplay> | ||
| 430 | |||
| 431 | ```yue | ||
| 432 | findFirstEven = (list) -> | ||
| 433 | for item in *list | ||
| 434 | if type(item) == "table" | ||
| 435 | for sub in *item | ||
| 436 | if sub % 2 == 0 | ||
| 437 | return sub | ||
| 438 | nil | ||
| 439 | ``` | ||
| 440 | |||
| 441 | </YueDisplay> | ||
| 442 | |||
| 443 | The only difference is that you can move the final return expression before the `->` or `=>` token to indicate the function’s implicit return value as the last statement. This way, even in functions with multiple nested loops or conditional branches, you no longer need to write a trailing return expression at the end of the function body, making the logic structure more straightforward and easier to follow. | ||
| 444 | |||
| 445 | ## Named Varargs | ||
| 446 | |||
| 447 | You can use the `(...t) ->` syntax to automatically store varargs into a named table. This table will contain all passed arguments (including `nil` values), and the `n` field of the table will store the actual number of arguments passed (including `nil` values). | ||
| 448 | |||
| 449 | ```yuescript | ||
| 450 | f = (...t) -> | ||
| 451 | print "argument count:", t.n | ||
| 452 | print "table length:", #t | ||
| 453 | for i = 1, t.n | ||
| 454 | print t[i] | ||
| 455 | |||
| 456 | f 1, 2, 3 | ||
| 457 | f "a", "b", "c", "d" | ||
| 458 | f! | ||
| 459 | |||
| 460 | -- Handling cases with nil values | ||
| 461 | process = (...args) -> | ||
| 462 | sum = 0 | ||
| 463 | for i = 1, args.n | ||
| 464 | if args[i] != nil and type(args[i]) == "number" | ||
| 465 | sum += args[i] | ||
| 466 | sum | ||
| 467 | |||
| 468 | process 1, nil, 3, nil, 5 | ||
| 469 | ``` | ||
| 470 | <YueDisplay> | ||
| 471 | |||
| 472 | ```yue | ||
| 473 | f = (...t) -> | ||
| 474 | print "argument count:", t.n | ||
| 475 | print "table length:", #t | ||
| 476 | for i = 1, t.n | ||
| 477 | print t[i] | ||
| 478 | |||
| 479 | f 1, 2, 3 | ||
| 480 | f "a", "b", "c", "d" | ||
| 481 | f! | ||
| 482 | |||
| 483 | -- Handling cases with nil values | ||
| 484 | process = (...args) -> | ||
| 485 | sum = 0 | ||
| 486 | for i = 1, args.n | ||
| 487 | if args[i] != nil and type(args[i]) == "number" | ||
| 488 | sum += args[i] | ||
| 489 | sum | ||
| 490 | |||
| 491 | process 1, nil, 3, nil, 5 | ||
| 492 | ``` | ||
| 493 | |||
| 494 | </YueDisplay> | ||
diff --git a/doc/docs/de/doc/function-stubs.md b/doc/docs/de/doc/function-stubs.md new file mode 100644 index 0000000..57a8b0c --- /dev/null +++ b/doc/docs/de/doc/function-stubs.md | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | # Function Stubs | ||
| 2 | |||
| 3 | It is common to pass a function from an object around as a value, for example, passing an instance method into a function as a callback. If the function expects the object it is operating on as the first argument then you must somehow bundle that object with the function so it can be called properly. | ||
| 4 | |||
| 5 | The function stub syntax is a shorthand for creating a new closure function that bundles both the object and function. This new function calls the wrapped function in the correct context of the object. | ||
| 6 | |||
| 7 | Its syntax is the same as calling an instance method with the \ operator but with no argument list provided. | ||
| 8 | |||
| 9 | ```yuescript | ||
| 10 | my_object = { | ||
| 11 | value: 1000 | ||
| 12 | write: => print "the value:", @value | ||
| 13 | } | ||
| 14 | |||
| 15 | run_callback = (func) -> | ||
| 16 | print "running callback..." | ||
| 17 | func! | ||
| 18 | |||
| 19 | -- this will not work: | ||
| 20 | -- the function has to no reference to my_object | ||
| 21 | run_callback my_object.write | ||
| 22 | |||
| 23 | -- function stub syntax | ||
| 24 | -- lets us bundle the object into a new function | ||
| 25 | run_callback my_object\write | ||
| 26 | ``` | ||
| 27 | <YueDisplay> | ||
| 28 | |||
| 29 | ```yue | ||
| 30 | my_object = { | ||
| 31 | value: 1000 | ||
| 32 | write: => print "the value:", @value | ||
| 33 | } | ||
| 34 | |||
| 35 | run_callback = (func) -> | ||
| 36 | print "running callback..." | ||
| 37 | func! | ||
| 38 | |||
| 39 | -- this will not work: | ||
| 40 | -- the function has to no reference to my_object | ||
| 41 | run_callback my_object.write | ||
| 42 | |||
| 43 | -- function stub syntax | ||
| 44 | -- lets us bundle the object into a new function | ||
| 45 | run_callback my_object\write | ||
| 46 | ``` | ||
| 47 | |||
| 48 | </YueDisplay> | ||
diff --git a/doc/docs/de/doc/if-assignment.md b/doc/docs/de/doc/if-assignment.md new file mode 100644 index 0000000..02984e8 --- /dev/null +++ b/doc/docs/de/doc/if-assignment.md | |||
| @@ -0,0 +1,72 @@ | |||
| 1 | # If Assignment | ||
| 2 | |||
| 3 | `if` and `elseif` blocks can take an assignment in place of a conditional expression. Upon evaluating the conditional, the assignment will take place and the value that was assigned to will be used as the conditional expression. The assigned variable is only in scope for the body of the conditional, meaning it is never available if the value is not truthy. And you have to use "the walrus operator" `:=` instead of `=` to do assignment. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | if user := database.find_user "moon" | ||
| 7 | print user.name | ||
| 8 | ``` | ||
| 9 | <YueDisplay> | ||
| 10 | |||
| 11 | ```yue | ||
| 12 | if user := database.find_user "moon" | ||
| 13 | print user.name | ||
| 14 | ``` | ||
| 15 | |||
| 16 | </YueDisplay> | ||
| 17 | |||
| 18 | ```yuescript | ||
| 19 | if hello := os.getenv "hello" | ||
| 20 | print "You have hello", hello | ||
| 21 | elseif world := os.getenv "world" | ||
| 22 | print "you have world", world | ||
| 23 | else | ||
| 24 | print "nothing :(" | ||
| 25 | ``` | ||
| 26 | <YueDisplay> | ||
| 27 | |||
| 28 | ```yue | ||
| 29 | if hello := os.getenv "hello" | ||
| 30 | print "You have hello", hello | ||
| 31 | elseif world := os.getenv "world" | ||
| 32 | print "you have world", world | ||
| 33 | else | ||
| 34 | print "nothing :(" | ||
| 35 | ``` | ||
| 36 | |||
| 37 | </YueDisplay> | ||
| 38 | |||
| 39 | If assignment with multiple return values. Only the first value is getting checked, other values are scoped. | ||
| 40 | ```yuescript | ||
| 41 | if success, result := pcall -> "get result without problems" | ||
| 42 | print result -- variable result is scoped | ||
| 43 | print "OK" | ||
| 44 | ``` | ||
| 45 | <YueDisplay> | ||
| 46 | |||
| 47 | ```yue | ||
| 48 | if success, result := pcall -> "get result without problems" | ||
| 49 | print result -- variable result is scoped | ||
| 50 | print "OK" | ||
| 51 | ``` | ||
| 52 | |||
| 53 | </YueDisplay> | ||
| 54 | |||
| 55 | ## While Assignment | ||
| 56 | |||
| 57 | You can also use if assignment in a while loop to get the value as the loop condition. | ||
| 58 | |||
| 59 | ```yuescript | ||
| 60 | while byte := stream\read_one! | ||
| 61 | -- do something with the byte | ||
| 62 | print byte | ||
| 63 | ``` | ||
| 64 | <YueDisplay> | ||
| 65 | |||
| 66 | ```yue | ||
| 67 | while byte := stream\read_one! | ||
| 68 | -- do something with the byte | ||
| 69 | print byte | ||
| 70 | ``` | ||
| 71 | |||
| 72 | </YueDisplay> | ||
diff --git a/doc/docs/de/doc/index.md b/doc/docs/de/doc/index.md new file mode 100755 index 0000000..3c4857f --- /dev/null +++ b/doc/docs/de/doc/index.md | |||
| @@ -0,0 +1,11 @@ | |||
| 1 | --- | ||
| 2 | title: Referenz | ||
| 3 | --- | ||
| 4 | |||
| 5 | # YueScript-Dokumentation | ||
| 6 | |||
| 7 | <img src="/image/yuescript.svg" width="250px" height="250px" alt="logo" style="padding-top: 3em; padding-bottom: 2em;"/> | ||
| 8 | |||
| 9 | Willkommen in der offiziellen <b>YueScript</b>-Dokumentation!<br/> | ||
| 10 | Hier findest du Sprachfeatures, Nutzung, Referenzbeispiele und Ressourcen.<br/> | ||
| 11 | Bitte wähle ein Kapitel in der Seitenleiste, um mit YueScript zu beginnen. | ||
diff --git a/doc/docs/de/doc/installation.md b/doc/docs/de/doc/installation.md new file mode 100644 index 0000000..a93ddfd --- /dev/null +++ b/doc/docs/de/doc/installation.md | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | # Installation | ||
| 2 | |||
| 3 | ## Lua Module | ||
| 4 | |||
| 5 | Install [luarocks](https://luarocks.org), a package manager for Lua modules. Then install it as a Lua module and executable with: | ||
| 6 | |||
| 7 | ```shell | ||
| 8 | luarocks install yuescript | ||
| 9 | ``` | ||
| 10 | |||
| 11 | Or you can build `yue.so` file with: | ||
| 12 | |||
| 13 | ```shell | ||
| 14 | make shared LUAI=/usr/local/include/lua LUAL=/usr/local/lib/lua | ||
| 15 | ``` | ||
| 16 | |||
| 17 | Then get the binary file from path **bin/shared/yue.so**. | ||
| 18 | |||
| 19 | ## Build Binary Tool | ||
| 20 | |||
| 21 | Clone this repo, then build and install executable with: | ||
| 22 | |||
| 23 | ```shell | ||
| 24 | make install | ||
| 25 | ``` | ||
| 26 | |||
| 27 | Build YueScript tool without macro feature: | ||
| 28 | |||
| 29 | ```shell | ||
| 30 | make install NO_MACRO=true | ||
| 31 | ``` | ||
| 32 | |||
| 33 | Build YueScript tool without built-in Lua binary: | ||
| 34 | |||
| 35 | ```shell | ||
| 36 | make install NO_LUA=true | ||
| 37 | ``` | ||
| 38 | |||
| 39 | ## Download Precompiled Binary | ||
| 40 | |||
| 41 | You can download precompiled binary files, including binary executable files compatible with different Lua versions and library files. | ||
| 42 | |||
| 43 | Download precompiled binary files from [here](https://github.com/IppClub/YueScript/releases). | ||
diff --git a/doc/docs/de/doc/introduction.md b/doc/docs/de/doc/introduction.md new file mode 100644 index 0000000..a9a9389 --- /dev/null +++ b/doc/docs/de/doc/introduction.md | |||
| @@ -0,0 +1,105 @@ | |||
| 1 | # Introduction | ||
| 2 | |||
| 3 | YueScript is a dynamic language that compiles to Lua. And it's a [MoonScript](https://github.com/leafo/moonscript) dialect. The codes written in YueScript are expressive and extremely concise. And it is suitable for writing some changing application logic with more maintainable codes and runs in a Lua embeded environment such as games or website servers. | ||
| 4 | |||
| 5 | Yue (月) is the name of moon in Chinese and it's pronounced as [jyɛ]. | ||
| 6 | |||
| 7 | ## An Overview of YueScript | ||
| 8 | |||
| 9 | ```yuescript | ||
| 10 | -- import syntax | ||
| 11 | import p, to_lua from "yue" | ||
| 12 | |||
| 13 | -- object literals | ||
| 14 | inventory = | ||
| 15 | equipment: | ||
| 16 | - "sword" | ||
| 17 | - "shield" | ||
| 18 | items: | ||
| 19 | - name: "potion" | ||
| 20 | count: 10 | ||
| 21 | - name: "bread" | ||
| 22 | count: 3 | ||
| 23 | |||
| 24 | -- list comprehension | ||
| 25 | map = (arr, action) -> | ||
| 26 | [action item for item in *arr] | ||
| 27 | |||
| 28 | filter = (arr, cond) -> | ||
| 29 | [item for item in *arr when cond item] | ||
| 30 | |||
| 31 | reduce = (arr, init, action): init -> | ||
| 32 | init = action init, item for item in *arr | ||
| 33 | |||
| 34 | -- pipe operator | ||
| 35 | [1, 2, 3] | ||
| 36 | |> map (x) -> x * 2 | ||
| 37 | |> filter (x) -> x > 4 | ||
| 38 | |> reduce 0, (a, b) -> a + b | ||
| 39 | |||
| 40 | |||
| 41 | -- metatable manipulation | ||
| 42 | apple = | ||
| 43 | size: 15 | ||
| 44 | <index>: | ||
| 45 | color: 0x00ffff | ||
| 46 | |||
| 47 | with apple | ||
| 48 | p .size, .color, .<index> if .<>? | ||
| 49 | |||
| 50 | -- js-like export syntax | ||
| 51 | export 🌛 = "月之脚本" | ||
| 52 | ``` | ||
| 53 | |||
| 54 | <YueDisplay> | ||
| 55 | |||
| 56 | ```yue | ||
| 57 | -- import syntax | ||
| 58 | import p, to_lua from "yue" | ||
| 59 | |||
| 60 | -- object literals | ||
| 61 | inventory = | ||
| 62 | equipment: | ||
| 63 | - "sword" | ||
| 64 | - "shield" | ||
| 65 | items: | ||
| 66 | - name: "potion" | ||
| 67 | count: 10 | ||
| 68 | - name: "bread" | ||
| 69 | count: 3 | ||
| 70 | |||
| 71 | -- list comprehension | ||
| 72 | map = (arr, action) -> | ||
| 73 | [action item for item in *arr] | ||
| 74 | |||
| 75 | filter = (arr, cond) -> | ||
| 76 | [item for item in *arr when cond item] | ||
| 77 | |||
| 78 | reduce = (arr, init, action): init -> | ||
| 79 | init = action init, item for item in *arr | ||
| 80 | |||
| 81 | -- pipe operator | ||
| 82 | [1, 2, 3] | ||
| 83 | |> map (x) -> x * 2 | ||
| 84 | |> filter (x) -> x > 4 | ||
| 85 | |> reduce 0, (a, b) -> a + b | ||
| 86 | |||
| 87 | |||
| 88 | -- metatable manipulation | ||
| 89 | apple = | ||
| 90 | size: 15 | ||
| 91 | <index>: | ||
| 92 | color: 0x00ffff | ||
| 93 | |||
| 94 | with apple | ||
| 95 | p .size, .color, .<index> if .<>? | ||
| 96 | |||
| 97 | -- js-like export syntax | ||
| 98 | export 🌛 = "月之脚本" | ||
| 99 | ``` | ||
| 100 | |||
| 101 | </YueDisplay> | ||
| 102 | |||
| 103 | ## About Dora SSR | ||
| 104 | |||
| 105 | YueScript is being developed and maintained alongside the open-source game engine [Dora SSR](https://github.com/Dora-SSR/Dora-SSR). It has been used to create engine tools, game demos and prototypes, validating its capabilities in real-world scenarios while enhancing the Dora SSR development experience. \ No newline at end of file | ||
diff --git a/doc/docs/de/doc/licence-mit.md b/doc/docs/de/doc/licence-mit.md new file mode 100644 index 0000000..f1d5d60 --- /dev/null +++ b/doc/docs/de/doc/licence-mit.md | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | # License: MIT | ||
| 2 | |||
| 3 | Copyright (c) 2017-2026 Li Jin \<dragon-fly@qq.com\> | ||
| 4 | |||
| 5 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 6 | of this software and associated documentation files (the "Software"), to deal | ||
| 7 | in the Software without restriction, including without limitation the rights | ||
| 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 9 | copies of the Software, and to permit persons to whom the Software is | ||
| 10 | furnished to do so, subject to the following conditions: | ||
| 11 | |||
| 12 | The above copyright notice and this permission notice shall be included in all | ||
| 13 | copies or substantial portions of the Software. | ||
| 14 | |||
| 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 21 | SOFTWARE. | ||
| 22 | |||
| 23 | <CompilerModal /> | ||
diff --git a/doc/docs/de/doc/line-decorators.md b/doc/docs/de/doc/line-decorators.md new file mode 100644 index 0000000..292bc77 --- /dev/null +++ b/doc/docs/de/doc/line-decorators.md | |||
| @@ -0,0 +1,44 @@ | |||
| 1 | # Line Decorators | ||
| 2 | |||
| 3 | For convenience, the for loop and if statement can be applied to single statements at the end of the line: | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | print "hello world" if name == "Rob" | ||
| 7 | ``` | ||
| 8 | <YueDisplay> | ||
| 9 | |||
| 10 | ```yue | ||
| 11 | print "hello world" if name == "Rob" | ||
| 12 | ``` | ||
| 13 | |||
| 14 | </YueDisplay> | ||
| 15 | |||
| 16 | And with basic loops: | ||
| 17 | |||
| 18 | ```yuescript | ||
| 19 | print "item: ", item for item in *items | ||
| 20 | ``` | ||
| 21 | <YueDisplay> | ||
| 22 | |||
| 23 | ```yue | ||
| 24 | print "item: ", item for item in *items | ||
| 25 | ``` | ||
| 26 | |||
| 27 | </YueDisplay> | ||
| 28 | |||
| 29 | And with while loops: | ||
| 30 | |||
| 31 | ```yuescript | ||
| 32 | game\update! while game\isRunning! | ||
| 33 | |||
| 34 | reader\parse_line! until reader\eof! | ||
| 35 | ``` | ||
| 36 | <YueDisplay> | ||
| 37 | |||
| 38 | ```yue | ||
| 39 | game\update! while game\isRunning! | ||
| 40 | |||
| 41 | reader\parse_line! until reader\eof! | ||
| 42 | ``` | ||
| 43 | |||
| 44 | </YueDisplay> | ||
diff --git a/doc/docs/de/doc/literals.md b/doc/docs/de/doc/literals.md new file mode 100644 index 0000000..e097c62 --- /dev/null +++ b/doc/docs/de/doc/literals.md | |||
| @@ -0,0 +1,110 @@ | |||
| 1 | # Literals | ||
| 2 | |||
| 3 | All of the primitive literals in Lua can be used. This applies to numbers, strings, booleans, and **nil**. | ||
| 4 | |||
| 5 | Unlike Lua, Line breaks are allowed inside of single and double quote strings without an escape sequence: | ||
| 6 | |||
| 7 | ```yuescript | ||
| 8 | some_string = "Here is a string | ||
| 9 | that has a line break in it." | ||
| 10 | |||
| 11 | -- You can mix expressions into string literals using #{} syntax. | ||
| 12 | -- String interpolation is only available in double quoted strings. | ||
| 13 | print "I am #{math.random! * 100}% sure." | ||
| 14 | ``` | ||
| 15 | <YueDisplay> | ||
| 16 | |||
| 17 | ```yue | ||
| 18 | some_string = "Here is a string | ||
| 19 | that has a line break in it." | ||
| 20 | |||
| 21 | -- You can mix expressions into string literals using #{} syntax. | ||
| 22 | -- String interpolation is only available in double quoted strings. | ||
| 23 | print "I am #{math.random! * 100}% sure." | ||
| 24 | ``` | ||
| 25 | |||
| 26 | </YueDisplay> | ||
| 27 | |||
| 28 | ## Number Literals | ||
| 29 | |||
| 30 | You can use underscores in a number literal to increase readability. | ||
| 31 | |||
| 32 | ```yuescript | ||
| 33 | integer = 1_000_000 | ||
| 34 | hex = 0xEF_BB_BF | ||
| 35 | binary = 0B10011 | ||
| 36 | ``` | ||
| 37 | <YueDisplay> | ||
| 38 | |||
| 39 | ```yue | ||
| 40 | integer = 1_000_000 | ||
| 41 | hex = 0xEF_BB_BF | ||
| 42 | binary = 0B10011 | ||
| 43 | ``` | ||
| 44 | |||
| 45 | </YueDisplay> | ||
| 46 | |||
| 47 | ## YAML Multiline String | ||
| 48 | |||
| 49 | The `|` prefix introduces a YAML-style multiline string literal: | ||
| 50 | |||
| 51 | ```yuescript | ||
| 52 | str = | | ||
| 53 | key: value | ||
| 54 | list: | ||
| 55 | - item1 | ||
| 56 | - #{expr} | ||
| 57 | ``` | ||
| 58 | <YueDisplay> | ||
| 59 | |||
| 60 | ```yue | ||
| 61 | str = | | ||
| 62 | key: value | ||
| 63 | list: | ||
| 64 | - item1 | ||
| 65 | - #{expr} | ||
| 66 | ``` | ||
| 67 | |||
| 68 | </YueDisplay> | ||
| 69 | |||
| 70 | This allows writing structured multiline text conveniently. All line breaks and indentation are preserved relative to the first non-empty line, and expressions inside `#{...}` are interpolated automatically as `tostring(expr)`. | ||
| 71 | |||
| 72 | YAML Multiline String automatically detects the common leading whitespace prefix (minimum indentation across all non-empty lines) and removes it from all lines. This makes it easy to indent your code visually without affecting the resulting string content. | ||
| 73 | |||
| 74 | ```yuescript | ||
| 75 | fn = -> | ||
| 76 | str = | | ||
| 77 | foo: | ||
| 78 | bar: baz | ||
| 79 | return str | ||
| 80 | ``` | ||
| 81 | <YueDisplay> | ||
| 82 | |||
| 83 | ```yue | ||
| 84 | fn = -> | ||
| 85 | str = | | ||
| 86 | foo: | ||
| 87 | bar: baz | ||
| 88 | return str | ||
| 89 | ``` | ||
| 90 | |||
| 91 | </YueDisplay> | ||
| 92 | |||
| 93 | Internal indentation is preserved relative to the removed common prefix, allowing clean nested structures. | ||
| 94 | |||
| 95 | All special characters like quotes (`"`) and backslashes (`\`) in the YAMLMultiline block are automatically escaped so that the generated Lua string is syntactically valid and behaves as expected. | ||
| 96 | |||
| 97 | ```yuescript | ||
| 98 | str = | | ||
| 99 | path: "C:\Program Files\App" | ||
| 100 | note: 'He said: "#{Hello}!"' | ||
| 101 | ``` | ||
| 102 | <YueDisplay> | ||
| 103 | |||
| 104 | ```yue | ||
| 105 | str = | | ||
| 106 | path: "C:\Program Files\App" | ||
| 107 | note: 'He said: "#{Hello}!"' | ||
| 108 | ``` | ||
| 109 | |||
| 110 | </YueDisplay> | ||
diff --git a/doc/docs/de/doc/macro.md b/doc/docs/de/doc/macro.md new file mode 100644 index 0000000..6d194c3 --- /dev/null +++ b/doc/docs/de/doc/macro.md | |||
| @@ -0,0 +1,275 @@ | |||
| 1 | # Macro | ||
| 2 | |||
| 3 | ## Common Usage | ||
| 4 | |||
| 5 | Macro function is used for evaluating a string in the compile time and insert the generated codes into final compilation. | ||
| 6 | |||
| 7 | ```yuescript | ||
| 8 | macro PI2 = -> math.pi * 2 | ||
| 9 | area = $PI2 * 5 | ||
| 10 | |||
| 11 | macro HELLO = -> "'hello world'" | ||
| 12 | print $HELLO | ||
| 13 | |||
| 14 | macro config = (debugging) -> | ||
| 15 | global debugMode = debugging == "true" | ||
| 16 | "" | ||
| 17 | |||
| 18 | macro asserts = (cond) -> | ||
| 19 | debugMode and "assert #{cond}" or "" | ||
| 20 | |||
| 21 | macro assert = (cond) -> | ||
| 22 | debugMode and "assert #{cond}" or "#{cond}" | ||
| 23 | |||
| 24 | $config true | ||
| 25 | $asserts item ~= nil | ||
| 26 | |||
| 27 | $config false | ||
| 28 | value = $assert item | ||
| 29 | |||
| 30 | -- the passed expressions are treated as strings | ||
| 31 | macro and = (...) -> "#{ table.concat {...}, ' and ' }" | ||
| 32 | if $and f1!, f2!, f3! | ||
| 33 | print "OK" | ||
| 34 | ``` | ||
| 35 | <YueDisplay> | ||
| 36 | |||
| 37 | ```yue | ||
| 38 | macro PI2 = -> math.pi * 2 | ||
| 39 | area = $PI2 * 5 | ||
| 40 | |||
| 41 | macro HELLO = -> "'hello world'" | ||
| 42 | print $HELLO | ||
| 43 | |||
| 44 | macro config = (debugging) -> | ||
| 45 | global debugMode = debugging == "true" | ||
| 46 | "" | ||
| 47 | |||
| 48 | macro asserts = (cond) -> | ||
| 49 | debugMode and "assert #{cond}" or "" | ||
| 50 | |||
| 51 | macro assert = (cond) -> | ||
| 52 | debugMode and "assert #{cond}" or "#{cond}" | ||
| 53 | |||
| 54 | $config true | ||
| 55 | $asserts item ~= nil | ||
| 56 | |||
| 57 | $config false | ||
| 58 | value = $assert item | ||
| 59 | |||
| 60 | -- the passed expressions are treated as strings | ||
| 61 | macro and = (...) -> "#{ table.concat {...}, ' and ' }" | ||
| 62 | if $and f1!, f2!, f3! | ||
| 63 | print "OK" | ||
| 64 | ``` | ||
| 65 | |||
| 66 | </YueDisplay> | ||
| 67 | |||
| 68 | ## Insert Raw Codes | ||
| 69 | |||
| 70 | A macro function can either return a YueScript string or a config table containing Lua codes. | ||
| 71 | ```yuescript | ||
| 72 | macro yueFunc = (var) -> "local #{var} = ->" | ||
| 73 | $yueFunc funcA | ||
| 74 | funcA = -> "fail to assign to the Yue macro defined variable" | ||
| 75 | |||
| 76 | macro luaFunc = (var) -> { | ||
| 77 | code: "local function #{var}() end" | ||
| 78 | type: "lua" | ||
| 79 | } | ||
| 80 | $luaFunc funcB | ||
| 81 | funcB = -> "fail to assign to the Lua macro defined variable" | ||
| 82 | |||
| 83 | macro lua = (code) -> { | ||
| 84 | :code | ||
| 85 | type: "lua" | ||
| 86 | } | ||
| 87 | |||
| 88 | -- the raw string leading and ending symbols are auto trimed | ||
| 89 | $lua[==[ | ||
| 90 | -- raw Lua codes insertion | ||
| 91 | if cond then | ||
| 92 | print("output") | ||
| 93 | end | ||
| 94 | ]==] | ||
| 95 | ``` | ||
| 96 | <YueDisplay> | ||
| 97 | |||
| 98 | ```yue | ||
| 99 | macro yueFunc = (var) -> "local #{var} = ->" | ||
| 100 | $yueFunc funcA | ||
| 101 | funcA = -> "fail to assign to the Yue macro defined variable" | ||
| 102 | |||
| 103 | macro luaFunc = (var) -> { | ||
| 104 | code: "local function #{var}() end" | ||
| 105 | type: "lua" | ||
| 106 | } | ||
| 107 | $luaFunc funcB | ||
| 108 | funcB = -> "fail to assign to the Lua macro defined variable" | ||
| 109 | |||
| 110 | macro lua = (code) -> { | ||
| 111 | :code | ||
| 112 | type: "lua" | ||
| 113 | } | ||
| 114 | |||
| 115 | -- the raw string leading and ending symbols are auto trimed | ||
| 116 | $lua[==[ | ||
| 117 | -- raw Lua codes insertion | ||
| 118 | if cond then | ||
| 119 | print("output") | ||
| 120 | end | ||
| 121 | ]==] | ||
| 122 | ``` | ||
| 123 | |||
| 124 | </YueDisplay> | ||
| 125 | |||
| 126 | ## Export Macro | ||
| 127 | |||
| 128 | Macro functions can be exported from a module and get imported in another module. You have to put export macro functions in a single file to be used, and only macro definition, macro importing and macro expansion in place can be put into the macro exporting module. | ||
| 129 | ```yuescript | ||
| 130 | -- file: utils.yue | ||
| 131 | export macro map = (items, action) -> "[#{action} for _ in *#{items}]" | ||
| 132 | export macro filter = (items, action) -> "[_ for _ in *#{items} when #{action}]" | ||
| 133 | export macro foreach = (items, action) -> "for _ in *#{items} | ||
| 134 | #{action}" | ||
| 135 | |||
| 136 | -- file main.yue | ||
| 137 | import "utils" as { | ||
| 138 | $, -- symbol to import all macros | ||
| 139 | $foreach: $each -- rename macro $foreach to $each | ||
| 140 | } | ||
| 141 | [1, 2, 3] |> $map(_ * 2) |> $filter(_ > 4) |> $each print _ | ||
| 142 | ``` | ||
| 143 | <YueDisplay> | ||
| 144 | |||
| 145 | ```yue | ||
| 146 | -- file: utils.yue | ||
| 147 | export macro map = (items, action) -> "[#{action} for _ in *#{items}]" | ||
| 148 | export macro filter = (items, action) -> "[_ for _ in *#{items} when #{action}]" | ||
| 149 | export macro foreach = (items, action) -> "for _ in *#{items} | ||
| 150 | #{action}" | ||
| 151 | |||
| 152 | -- file main.yue | ||
| 153 | -- import function is not available in browser, try it in a real environment | ||
| 154 | --[[ | ||
| 155 | import "utils" as { | ||
| 156 | $, -- symbol to import all macros | ||
| 157 | $foreach: $each -- rename macro $foreach to $each | ||
| 158 | } | ||
| 159 | [1, 2, 3] |> $map(_ * 2) |> $filter(_ > 4) |> $each print _ | ||
| 160 | ]] | ||
| 161 | ``` | ||
| 162 | |||
| 163 | </YueDisplay> | ||
| 164 | |||
| 165 | ## Builtin Macro | ||
| 166 | |||
| 167 | There are some builtin macros but you can override them by declaring macros with the same names. | ||
| 168 | ```yuescript | ||
| 169 | print $FILE -- get string of current module name | ||
| 170 | print $LINE -- get number 2 | ||
| 171 | ``` | ||
| 172 | <YueDisplay> | ||
| 173 | |||
| 174 | ```yue | ||
| 175 | print $FILE -- get string of current module name | ||
| 176 | print $LINE -- get number 2 | ||
| 177 | ``` | ||
| 178 | |||
| 179 | </YueDisplay> | ||
| 180 | |||
| 181 | ## Generating Macros with Macros | ||
| 182 | |||
| 183 | In YueScript, macro functions allow you to generate code at compile time. By nesting macro functions, you can create more complex generation patterns. This feature enables you to define a macro function that generates another macro function, allowing for more dynamic code generation. | ||
| 184 | |||
| 185 | ```yuescript | ||
| 186 | macro Enum = (...) -> | ||
| 187 | items = {...} | ||
| 188 | itemSet = {item, true for item in *items} | ||
| 189 | (item) -> | ||
| 190 | error "got \"#{item}\", expecting one of #{table.concat items, ', '}" unless itemSet[item] | ||
| 191 | "\"#{item}\"" | ||
| 192 | |||
| 193 | macro BodyType = $Enum( | ||
| 194 | Static | ||
| 195 | Dynamic | ||
| 196 | Kinematic | ||
| 197 | ) | ||
| 198 | |||
| 199 | print "Valid enum type:", $BodyType Static | ||
| 200 | -- print "Compilation error with enum type:", $BodyType Unknown | ||
| 201 | ``` | ||
| 202 | |||
| 203 | <YueDisplay> | ||
| 204 | |||
| 205 | ```yue | ||
| 206 | macro Enum = (...) -> | ||
| 207 | items = {...} | ||
| 208 | itemSet = {item, true for item in *items} | ||
| 209 | (item) -> | ||
| 210 | error "got \"#{item}\", expecting one of #{table.concat items, ', '}" unless itemSet[item] | ||
| 211 | "\"#{item}\"" | ||
| 212 | |||
| 213 | macro BodyType = $Enum( | ||
| 214 | Static | ||
| 215 | Dynamic | ||
| 216 | Kinematic | ||
| 217 | ) | ||
| 218 | |||
| 219 | print "Valid enum type:", $BodyType Static | ||
| 220 | -- print "Compilation error with enum type:", $BodyType Unknown | ||
| 221 | ``` | ||
| 222 | |||
| 223 | </YueDisplay> | ||
| 224 | |||
| 225 | ## Argument Validation | ||
| 226 | |||
| 227 | You can declare the expected AST node types in the argument list, and check whether the incoming macro arguments meet the expectations at compile time. | ||
| 228 | |||
| 229 | ```yuescript | ||
| 230 | macro printNumAndStr = (num `Num, str `String) -> | | ||
| 231 | print( | ||
| 232 | #{num} | ||
| 233 | #{str} | ||
| 234 | ) | ||
| 235 | |||
| 236 | $printNumAndStr 123, "hello" | ||
| 237 | ``` | ||
| 238 | <YueDisplay> | ||
| 239 | |||
| 240 | ```yue | ||
| 241 | macro printNumAndStr = (num `Num, str `String) -> | | ||
| 242 | print( | ||
| 243 | #{num} | ||
| 244 | #{str} | ||
| 245 | ) | ||
| 246 | |||
| 247 | $printNumAndStr 123, "hello" | ||
| 248 | ``` | ||
| 249 | |||
| 250 | </YueDisplay> | ||
| 251 | |||
| 252 | If you need more flexible argument checking, you can use the built-in `$is_ast` macro function to manually check at the appropriate place. | ||
| 253 | |||
| 254 | ```yuescript | ||
| 255 | macro printNumAndStr = (num, str) -> | ||
| 256 | error "expected Num as first argument" unless $is_ast Num, num | ||
| 257 | error "expected String as second argument" unless $is_ast String, str | ||
| 258 | "print(#{num}, #{str})" | ||
| 259 | |||
| 260 | $printNumAndStr 123, "hello" | ||
| 261 | ``` | ||
| 262 | <YueDisplay> | ||
| 263 | |||
| 264 | ```yue | ||
| 265 | macro printNumAndStr = (num, str) -> | ||
| 266 | error "expected Num as first argument" unless $is_ast Num, num | ||
| 267 | error "expected String as second argument" unless $is_ast String, str | ||
| 268 | "print(#{num}, #{str})" | ||
| 269 | |||
| 270 | $printNumAndStr 123, "hello" | ||
| 271 | ``` | ||
| 272 | |||
| 273 | </YueDisplay> | ||
| 274 | |||
| 275 | For more details about available AST nodes, please refer to the uppercased definitions in [yue_parser.cpp](https://github.com/IppClub/YueScript/blob/main/src/yuescript/yue_parser.cpp). | ||
diff --git a/doc/docs/de/doc/module.md b/doc/docs/de/doc/module.md new file mode 100644 index 0000000..c955092 --- /dev/null +++ b/doc/docs/de/doc/module.md | |||
| @@ -0,0 +1,245 @@ | |||
| 1 | # Module | ||
| 2 | |||
| 3 | ## Import | ||
| 4 | |||
| 5 | 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. | ||
| 6 | |||
| 7 | ```yuescript | ||
| 8 | -- used as table destructuring | ||
| 9 | do | ||
| 10 | import insert, concat from table | ||
| 11 | -- report error when assigning to insert, concat | ||
| 12 | import C, Ct, Cmt from require "lpeg" | ||
| 13 | -- shortcut for implicit requiring | ||
| 14 | import x, y, z from 'mymodule' | ||
| 15 | -- import with Python style | ||
| 16 | from 'module' import a, b, c | ||
| 17 | |||
| 18 | -- shortcut for requring a module | ||
| 19 | do | ||
| 20 | import 'module' | ||
| 21 | import 'module_x' | ||
| 22 | import "d-a-s-h-e-s" | ||
| 23 | import "module.part" | ||
| 24 | |||
| 25 | -- requring module with aliasing or table destructuring | ||
| 26 | do | ||
| 27 | import "player" as PlayerModule | ||
| 28 | import "lpeg" as :C, :Ct, :Cmt | ||
| 29 | import "export" as {one, two, Something:{umm:{ch}}} | ||
| 30 | ``` | ||
| 31 | <YueDisplay> | ||
| 32 | |||
| 33 | ```yue | ||
| 34 | -- used as table destructuring | ||
| 35 | do | ||
| 36 | import insert, concat from table | ||
| 37 | -- report error when assigning to insert, concat | ||
| 38 | import C, Ct, Cmt from require "lpeg" | ||
| 39 | -- shortcut for implicit requiring | ||
| 40 | import x, y, z from 'mymodule' | ||
| 41 | -- import with Python style | ||
| 42 | from 'module' import a, b, c | ||
| 43 | |||
| 44 | -- shortcut for requring a module | ||
| 45 | do | ||
| 46 | import 'module' | ||
| 47 | import 'module_x' | ||
| 48 | import "d-a-s-h-e-s" | ||
| 49 | import "module.part" | ||
| 50 | |||
| 51 | -- requring module with aliasing or table destructuring | ||
| 52 | do | ||
| 53 | import "player" as PlayerModule | ||
| 54 | import "lpeg" as :C, :Ct, :Cmt | ||
| 55 | import "export" as {one, two, Something:{umm:{ch}}} | ||
| 56 | ``` | ||
| 57 | |||
| 58 | </YueDisplay> | ||
| 59 | |||
| 60 | ## Import Global | ||
| 61 | |||
| 62 | 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. | ||
| 63 | |||
| 64 | ```yuescript | ||
| 65 | do | ||
| 66 | import tostring | ||
| 67 | import table.concat | ||
| 68 | print concat ["a", tostring 1] | ||
| 69 | ``` | ||
| 70 | <YueDisplay> | ||
| 71 | |||
| 72 | ```yue | ||
| 73 | do | ||
| 74 | import tostring | ||
| 75 | import table.concat | ||
| 76 | print concat ["a", tostring 1] | ||
| 77 | ``` | ||
| 78 | |||
| 79 | </YueDisplay> | ||
| 80 | |||
| 81 | ### Automatic Global Variable Import | ||
| 82 | |||
| 83 | 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. | ||
| 84 | |||
| 85 | Names that are explicitly declared as globals in the same scope will not be imported, so you can still assign to them. | ||
| 86 | |||
| 87 | ```yuescript | ||
| 88 | do | ||
| 89 | import global | ||
| 90 | print "hello" | ||
| 91 | math.random 3 | ||
| 92 | -- print = nil -- error: imported globals are const | ||
| 93 | |||
| 94 | do | ||
| 95 | -- explicit global variable will not be imported | ||
| 96 | import global | ||
| 97 | global FLAG | ||
| 98 | print FLAG | ||
| 99 | FLAG = 123 | ||
| 100 | ``` | ||
| 101 | <YueDisplay> | ||
| 102 | |||
| 103 | ```yue | ||
| 104 | do | ||
| 105 | import global | ||
| 106 | print "hello" | ||
| 107 | math.random 3 | ||
| 108 | -- print = nil -- error: imported globals are const | ||
| 109 | |||
| 110 | do | ||
| 111 | -- explicit global variable will not be imported | ||
| 112 | import global | ||
| 113 | global FLAG | ||
| 114 | print FLAG | ||
| 115 | FLAG = 123 | ||
| 116 | ``` | ||
| 117 | |||
| 118 | </YueDisplay> | ||
| 119 | |||
| 120 | ## Export | ||
| 121 | |||
| 122 | The export statement offers a concise way to define modules. | ||
| 123 | |||
| 124 | ### Named Export | ||
| 125 | |||
| 126 | Named export will define a local variable as well as adding a field in the exported table. | ||
| 127 | |||
| 128 | ```yuescript | ||
| 129 | export a, b, c = 1, 2, 3 | ||
| 130 | export cool = "cat" | ||
| 131 | |||
| 132 | export What = if this | ||
| 133 | "abc" | ||
| 134 | else | ||
| 135 | "def" | ||
| 136 | |||
| 137 | export y = -> | ||
| 138 | hallo = 3434 | ||
| 139 | |||
| 140 | export class Something | ||
| 141 | umm: "cool" | ||
| 142 | ``` | ||
| 143 | <YueDisplay> | ||
| 144 | |||
| 145 | ```yue | ||
| 146 | export a, b, c = 1, 2, 3 | ||
| 147 | export cool = "cat" | ||
| 148 | |||
| 149 | export What = if this | ||
| 150 | "abc" | ||
| 151 | else | ||
| 152 | "def" | ||
| 153 | |||
| 154 | export y = -> | ||
| 155 | hallo = 3434 | ||
| 156 | |||
| 157 | export class Something | ||
| 158 | umm: "cool" | ||
| 159 | ``` | ||
| 160 | |||
| 161 | </YueDisplay> | ||
| 162 | |||
| 163 | Doing named export with destructuring. | ||
| 164 | |||
| 165 | ```yuescript | ||
| 166 | export :loadstring, to_lua: tolua = yue | ||
| 167 | export {itemA: {:fieldA = 'default'}} = tb | ||
| 168 | ``` | ||
| 169 | <YueDisplay> | ||
| 170 | |||
| 171 | ```yue | ||
| 172 | export :loadstring, to_lua: tolua = yue | ||
| 173 | export {itemA: {:fieldA = 'default'}} = tb | ||
| 174 | ``` | ||
| 175 | |||
| 176 | </YueDisplay> | ||
| 177 | |||
| 178 | Export named items from module without creating local variables. | ||
| 179 | |||
| 180 | ```yuescript | ||
| 181 | export.itemA = tb | ||
| 182 | export.<index> = items | ||
| 183 | export["a-b-c"] = 123 | ||
| 184 | ``` | ||
| 185 | <YueDisplay> | ||
| 186 | |||
| 187 | ```yue | ||
| 188 | export.itemA = tb | ||
| 189 | export.<index> = items | ||
| 190 | export["a-b-c"] = 123 | ||
| 191 | ``` | ||
| 192 | |||
| 193 | </YueDisplay> | ||
| 194 | |||
| 195 | ### Unnamed Export | ||
| 196 | |||
| 197 | Unnamed export will add the target item into the array part of the exported table. | ||
| 198 | |||
| 199 | ```yuescript | ||
| 200 | d, e, f = 3, 2, 1 | ||
| 201 | export d, e, f | ||
| 202 | |||
| 203 | export if this | ||
| 204 | 123 | ||
| 205 | else | ||
| 206 | 456 | ||
| 207 | |||
| 208 | export with tmp | ||
| 209 | j = 2000 | ||
| 210 | ``` | ||
| 211 | <YueDisplay> | ||
| 212 | |||
| 213 | ```yue | ||
| 214 | d, e, f = 3, 2, 1 | ||
| 215 | export d, e, f | ||
| 216 | |||
| 217 | export if this | ||
| 218 | 123 | ||
| 219 | else | ||
| 220 | 456 | ||
| 221 | |||
| 222 | export with tmp | ||
| 223 | j = 2000 | ||
| 224 | ``` | ||
| 225 | |||
| 226 | </YueDisplay> | ||
| 227 | |||
| 228 | ### Default Export | ||
| 229 | |||
| 230 | Using the **default** keyword in export statement to replace the exported table with any thing. | ||
| 231 | |||
| 232 | ```yuescript | ||
| 233 | export default -> | ||
| 234 | print "hello" | ||
| 235 | 123 | ||
| 236 | ``` | ||
| 237 | <YueDisplay> | ||
| 238 | |||
| 239 | ```yue | ||
| 240 | export default -> | ||
| 241 | print "hello" | ||
| 242 | 123 | ||
| 243 | ``` | ||
| 244 | |||
| 245 | </YueDisplay> | ||
diff --git a/doc/docs/de/doc/object-oriented-programming.md b/doc/docs/de/doc/object-oriented-programming.md new file mode 100644 index 0000000..6a8559e --- /dev/null +++ b/doc/docs/de/doc/object-oriented-programming.md | |||
| @@ -0,0 +1,555 @@ | |||
| 1 | # Object Oriented Programming | ||
| 2 | |||
| 3 | In these examples, the generated Lua code may appear overwhelming. It is best to focus on the meaning of the YueScript code at first, then look into the Lua code if you wish to know the implementation details. | ||
| 4 | |||
| 5 | A simple class: | ||
| 6 | |||
| 7 | ```yuescript | ||
| 8 | class Inventory | ||
| 9 | new: => | ||
| 10 | @items = {} | ||
| 11 | |||
| 12 | add_item: (name) => | ||
| 13 | if @items[name] | ||
| 14 | @items[name] += 1 | ||
| 15 | else | ||
| 16 | @items[name] = 1 | ||
| 17 | ``` | ||
| 18 | <YueDisplay> | ||
| 19 | |||
| 20 | ```yue | ||
| 21 | class Inventory | ||
| 22 | new: => | ||
| 23 | @items = {} | ||
| 24 | |||
| 25 | add_item: (name) => | ||
| 26 | if @items[name] | ||
| 27 | @items[name] += 1 | ||
| 28 | else | ||
| 29 | @items[name] = 1 | ||
| 30 | ``` | ||
| 31 | |||
| 32 | </YueDisplay> | ||
| 33 | |||
| 34 | A class is declared with a class statement followed by a table-like declaration where all of the methods and properties are listed. | ||
| 35 | |||
| 36 | The new property is special in that it will become the constructor. | ||
| 37 | |||
| 38 | Notice how all the methods in the class use the fat arrow function syntax. When calling methods on a instance, the instance itself is sent in as the first argument. The fat arrow handles the creation of a self argument. | ||
| 39 | |||
| 40 | The @ prefix on a variable name is shorthand for self.. @items becomes self.items. | ||
| 41 | |||
| 42 | Creating an instance of the class is done by calling the name of the class as a function. | ||
| 43 | |||
| 44 | ```yuescript | ||
| 45 | inv = Inventory! | ||
| 46 | inv\add_item "t-shirt" | ||
| 47 | inv\add_item "pants" | ||
| 48 | ``` | ||
| 49 | <YueDisplay> | ||
| 50 | |||
| 51 | ```yue | ||
| 52 | inv = Inventory! | ||
| 53 | inv\add_item "t-shirt" | ||
| 54 | inv\add_item "pants" | ||
| 55 | ``` | ||
| 56 | |||
| 57 | </YueDisplay> | ||
| 58 | |||
| 59 | Because the instance of the class needs to be sent to the methods when they are called, the \ operator is used. | ||
| 60 | |||
| 61 | All properties of a class are shared among the instances. This is fine for functions, but for other types of objects, undesired results may occur. | ||
| 62 | |||
| 63 | Consider the example below, the clothes property is shared amongst all instances, so modifications to it in one instance will show up in another: | ||
| 64 | |||
| 65 | ```yuescript | ||
| 66 | class Person | ||
| 67 | clothes: [] | ||
| 68 | give_item: (name) => | ||
| 69 | table.insert @clothes, name | ||
| 70 | |||
| 71 | a = Person! | ||
| 72 | b = Person! | ||
| 73 | |||
| 74 | a\give_item "pants" | ||
| 75 | b\give_item "shirt" | ||
| 76 | |||
| 77 | -- will print both pants and shirt | ||
| 78 | print item for item in *a.clothes | ||
| 79 | ``` | ||
| 80 | <YueDisplay> | ||
| 81 | |||
| 82 | ```yue | ||
| 83 | class Person | ||
| 84 | clothes: [] | ||
| 85 | give_item: (name) => | ||
| 86 | table.insert @clothes, name | ||
| 87 | |||
| 88 | a = Person! | ||
| 89 | b = Person! | ||
| 90 | |||
| 91 | a\give_item "pants" | ||
| 92 | b\give_item "shirt" | ||
| 93 | |||
| 94 | -- will print both pants and shirt | ||
| 95 | print item for item in *a.clothes | ||
| 96 | ``` | ||
| 97 | |||
| 98 | </YueDisplay> | ||
| 99 | |||
| 100 | The proper way to avoid this problem is to create the mutable state of the object in the constructor: | ||
| 101 | |||
| 102 | ```yuescript | ||
| 103 | class Person | ||
| 104 | new: => | ||
| 105 | @clothes = [] | ||
| 106 | ``` | ||
| 107 | <YueDisplay> | ||
| 108 | |||
| 109 | ```yue | ||
| 110 | class Person | ||
| 111 | new: => | ||
| 112 | @clothes = [] | ||
| 113 | ``` | ||
| 114 | |||
| 115 | </YueDisplay> | ||
| 116 | |||
| 117 | ## Inheritance | ||
| 118 | |||
| 119 | The extends keyword can be used in a class declaration to inherit the properties and methods from another class. | ||
| 120 | |||
| 121 | ```yuescript | ||
| 122 | class BackPack extends Inventory | ||
| 123 | size: 10 | ||
| 124 | add_item: (name) => | ||
| 125 | if #@items > size then error "backpack is full" | ||
| 126 | super name | ||
| 127 | ``` | ||
| 128 | <YueDisplay> | ||
| 129 | |||
| 130 | ```yue | ||
| 131 | class BackPack extends Inventory | ||
| 132 | size: 10 | ||
| 133 | add_item: (name) => | ||
| 134 | if #@items > size then error "backpack is full" | ||
| 135 | super name | ||
| 136 | ``` | ||
| 137 | |||
| 138 | </YueDisplay> | ||
| 139 | |||
| 140 | Here we extend our Inventory class, and limit the amount of items it can carry. | ||
| 141 | |||
| 142 | In this example, we don't define a constructor on the subclass, so the parent class' constructor is called when we make a new instance. If we did define a constructor then we can use the super method to call the parent constructor. | ||
| 143 | |||
| 144 | Whenever a class inherits from another, it sends a message to the parent class by calling the method __inherited on the parent class if it exists. The function receives two arguments, the class that is being inherited and the child class. | ||
| 145 | |||
| 146 | ```yuescript | ||
| 147 | class Shelf | ||
| 148 | @__inherited: (child) => | ||
| 149 | print @__name, "was inherited by", child.__name | ||
| 150 | |||
| 151 | -- will print: Shelf was inherited by Cupboard | ||
| 152 | class Cupboard extends Shelf | ||
| 153 | ``` | ||
| 154 | <YueDisplay> | ||
| 155 | |||
| 156 | ```yue | ||
| 157 | class Shelf | ||
| 158 | @__inherited: (child) => | ||
| 159 | print @__name, "was inherited by", child.__name | ||
| 160 | |||
| 161 | -- will print: Shelf was inherited by Cupboard | ||
| 162 | class Cupboard extends Shelf | ||
| 163 | ``` | ||
| 164 | |||
| 165 | </YueDisplay> | ||
| 166 | |||
| 167 | ## Super | ||
| 168 | |||
| 169 | **super** is a special keyword that can be used in two different ways: It can be treated as an object, or it can be called like a function. It only has special functionality when inside a class. | ||
| 170 | |||
| 171 | When called as a function, it will call the function of the same name in the parent class. The current self will automatically be passed as the first argument. (As seen in the inheritance example above) | ||
| 172 | |||
| 173 | When super is used as a normal value, it is a reference to the parent class object. | ||
| 174 | |||
| 175 | It can be accessed like any of object in order to retrieve values in the parent class that might have been shadowed by the child class. | ||
| 176 | |||
| 177 | When the \ calling operator is used with super, self is inserted as the first argument instead of the value of super itself. When using . to retrieve a function, the raw function is returned. | ||
| 178 | |||
| 179 | A few examples of using super in different ways: | ||
| 180 | |||
| 181 | ```yuescript | ||
| 182 | class MyClass extends ParentClass | ||
| 183 | a_method: => | ||
| 184 | -- the following have the same effect: | ||
| 185 | super "hello", "world" | ||
| 186 | super\a_method "hello", "world" | ||
| 187 | super.a_method self, "hello", "world" | ||
| 188 | |||
| 189 | -- super as a value is equal to the parent class: | ||
| 190 | assert super == ParentClass | ||
| 191 | ``` | ||
| 192 | <YueDisplay> | ||
| 193 | |||
| 194 | ```yue | ||
| 195 | class MyClass extends ParentClass | ||
| 196 | a_method: => | ||
| 197 | -- the following have the same effect: | ||
| 198 | super "hello", "world" | ||
| 199 | super\a_method "hello", "world" | ||
| 200 | super.a_method self, "hello", "world" | ||
| 201 | |||
| 202 | -- super as a value is equal to the parent class: | ||
| 203 | assert super == ParentClass | ||
| 204 | ``` | ||
| 205 | |||
| 206 | </YueDisplay> | ||
| 207 | |||
| 208 | **super** can also be used on left side of a Function Stub. The only major difference is that instead of the resulting function being bound to the value of super, it is bound to self. | ||
| 209 | |||
| 210 | ## Types | ||
| 211 | |||
| 212 | Every instance of a class carries its type with it. This is stored in the special __class property. This property holds the class object. The class object is what we call to build a new instance. We can also index the class object to retrieve class methods and properties. | ||
| 213 | |||
| 214 | ```yuescript | ||
| 215 | b = BackPack! | ||
| 216 | assert b.__class == BackPack | ||
| 217 | |||
| 218 | print BackPack.size -- prints 10 | ||
| 219 | ``` | ||
| 220 | <YueDisplay> | ||
| 221 | |||
| 222 | ```yue | ||
| 223 | b = BackPack! | ||
| 224 | assert b.__class == BackPack | ||
| 225 | |||
| 226 | print BackPack.size -- prints 10 | ||
| 227 | ``` | ||
| 228 | |||
| 229 | </YueDisplay> | ||
| 230 | |||
| 231 | ## Class Objects | ||
| 232 | |||
| 233 | The class object is what we create when we use a class statement. The class object is stored in a variable of the same name of the class. | ||
| 234 | |||
| 235 | The class object can be called like a function in order to create new instances. That's how we created instances of classes in the examples above. | ||
| 236 | |||
| 237 | A class is made up of two tables. The class table itself, and the base table. The base is used as the metatable for all the instances. All properties listed in the class declaration are placed in the base. | ||
| 238 | |||
| 239 | The class object's metatable reads properties from the base if they don't exist in the class object. This means we can access functions and properties directly from the class. | ||
| 240 | |||
| 241 | It is important to note that assigning to the class object does not assign into the base, so it's not a valid way to add new methods to instances. Instead the base must explicitly be changed. See the __base field below. | ||
| 242 | |||
| 243 | The class object has a couple special properties: | ||
| 244 | |||
| 245 | The name of the class as when it was declared is stored as a string in the __name field of the class object. | ||
| 246 | |||
| 247 | ```yuescript | ||
| 248 | print BackPack.__name -- prints Backpack | ||
| 249 | ``` | ||
| 250 | <YueDisplay> | ||
| 251 | |||
| 252 | ```yue | ||
| 253 | print BackPack.__name -- prints Backpack | ||
| 254 | ``` | ||
| 255 | |||
| 256 | </YueDisplay> | ||
| 257 | |||
| 258 | The base object is stored in __base. We can modify this table to add functionality to instances that have already been created and ones that are yet to be created. | ||
| 259 | |||
| 260 | If the class extends from anything, the parent class object is stored in __parent. | ||
| 261 | |||
| 262 | ## Class Variables | ||
| 263 | |||
| 264 | We can create variables directly in the class object instead of in the base by using @ in the front of the property name in a class declaration. | ||
| 265 | |||
| 266 | ```yuescript | ||
| 267 | class Things | ||
| 268 | @some_func: => print "Hello from", @__name | ||
| 269 | |||
| 270 | Things\some_func! | ||
| 271 | |||
| 272 | -- class variables not visible in instances | ||
| 273 | assert Things().some_func == nil | ||
| 274 | ``` | ||
| 275 | <YueDisplay> | ||
| 276 | |||
| 277 | ```yue | ||
| 278 | class Things | ||
| 279 | @some_func: => print "Hello from", @__name | ||
| 280 | |||
| 281 | Things\some_func! | ||
| 282 | |||
| 283 | -- class variables not visible in instances | ||
| 284 | assert Things().some_func == nil | ||
| 285 | ``` | ||
| 286 | |||
| 287 | </YueDisplay> | ||
| 288 | |||
| 289 | In expressions, we can use @@ to access a value that is stored in the __class of self. Thus, @@hello is shorthand for self.__class.hello. | ||
| 290 | |||
| 291 | ```yuescript | ||
| 292 | class Counter | ||
| 293 | @count: 0 | ||
| 294 | |||
| 295 | new: => | ||
| 296 | @@count += 1 | ||
| 297 | |||
| 298 | Counter! | ||
| 299 | Counter! | ||
| 300 | |||
| 301 | print Counter.count -- prints 2 | ||
| 302 | ``` | ||
| 303 | <YueDisplay> | ||
| 304 | |||
| 305 | ```yue | ||
| 306 | class Counter | ||
| 307 | @count: 0 | ||
| 308 | |||
| 309 | new: => | ||
| 310 | @@count += 1 | ||
| 311 | |||
| 312 | Counter! | ||
| 313 | Counter! | ||
| 314 | |||
| 315 | print Counter.count -- prints 2 | ||
| 316 | ``` | ||
| 317 | |||
| 318 | </YueDisplay> | ||
| 319 | |||
| 320 | The calling semantics of @@ are similar to @. Calling a @@ name will pass the class in as the first argument using Lua's colon syntax. | ||
| 321 | |||
| 322 | ```yuescript | ||
| 323 | @@hello 1,2,3,4 | ||
| 324 | ``` | ||
| 325 | <YueDisplay> | ||
| 326 | |||
| 327 | ```yue | ||
| 328 | @@hello 1,2,3,4 | ||
| 329 | ``` | ||
| 330 | |||
| 331 | </YueDisplay> | ||
| 332 | |||
| 333 | ## Class Declaration Statements | ||
| 334 | |||
| 335 | In the body of a class declaration, we can have normal expressions in addition to key/value pairs. In this context, self is equal to the class object. | ||
| 336 | |||
| 337 | Here is an alternative way to create a class variable compared to what's described above: | ||
| 338 | |||
| 339 | ```yuescript | ||
| 340 | class Things | ||
| 341 | @class_var = "hello world" | ||
| 342 | ``` | ||
| 343 | <YueDisplay> | ||
| 344 | |||
| 345 | ```yue | ||
| 346 | class Things | ||
| 347 | @class_var = "hello world" | ||
| 348 | ``` | ||
| 349 | |||
| 350 | </YueDisplay> | ||
| 351 | |||
| 352 | These expressions are executed after all the properties have been added to the base. | ||
| 353 | |||
| 354 | All variables declared in the body of the class are local to the classes properties. This is convenient for placing private values or helper functions that only the class methods can access: | ||
| 355 | |||
| 356 | ```yuescript | ||
| 357 | class MoreThings | ||
| 358 | secret = 123 | ||
| 359 | log = (msg) -> print "LOG:", msg | ||
| 360 | |||
| 361 | some_method: => | ||
| 362 | log "hello world: " .. secret | ||
| 363 | ``` | ||
| 364 | <YueDisplay> | ||
| 365 | |||
| 366 | ```yue | ||
| 367 | class MoreThings | ||
| 368 | secret = 123 | ||
| 369 | log = (msg) -> print "LOG:", msg | ||
| 370 | |||
| 371 | some_method: => | ||
| 372 | log "hello world: " .. secret | ||
| 373 | ``` | ||
| 374 | |||
| 375 | </YueDisplay> | ||
| 376 | |||
| 377 | ## @ and @@ Values | ||
| 378 | |||
| 379 | When @ and @@ are prefixed in front of a name they represent, respectively, that name accessed in self and self.__class. | ||
| 380 | |||
| 381 | If they are used all by themselves, they are aliases for self and self.__class. | ||
| 382 | |||
| 383 | ```yuescript | ||
| 384 | assert @ == self | ||
| 385 | assert @@ == self.__class | ||
| 386 | ``` | ||
| 387 | <YueDisplay> | ||
| 388 | |||
| 389 | ```yue | ||
| 390 | assert @ == self | ||
| 391 | assert @@ == self.__class | ||
| 392 | ``` | ||
| 393 | |||
| 394 | </YueDisplay> | ||
| 395 | |||
| 396 | For example, a quick way to create a new instance of the same class from an instance method using @@: | ||
| 397 | |||
| 398 | ```yuescript | ||
| 399 | some_instance_method = (...) => @@ ... | ||
| 400 | ``` | ||
| 401 | <YueDisplay> | ||
| 402 | |||
| 403 | ```yue | ||
| 404 | some_instance_method = (...) => @@ ... | ||
| 405 | ``` | ||
| 406 | |||
| 407 | </YueDisplay> | ||
| 408 | |||
| 409 | ## Constructor Property Promotion | ||
| 410 | |||
| 411 | To reduce the boilerplate code for definition of simple value objects. You can write a simple class like: | ||
| 412 | |||
| 413 | ```yuescript | ||
| 414 | class Something | ||
| 415 | new: (@foo, @bar, @@biz, @@baz) => | ||
| 416 | |||
| 417 | -- Which is short for | ||
| 418 | |||
| 419 | class Something | ||
| 420 | new: (foo, bar, biz, baz) => | ||
| 421 | @foo = foo | ||
| 422 | @bar = bar | ||
| 423 | @@biz = biz | ||
| 424 | @@baz = baz | ||
| 425 | ``` | ||
| 426 | <YueDisplay> | ||
| 427 | |||
| 428 | ```yue | ||
| 429 | class Something | ||
| 430 | new: (@foo, @bar, @@biz, @@baz) => | ||
| 431 | |||
| 432 | -- Which is short for | ||
| 433 | |||
| 434 | class Something | ||
| 435 | new: (foo, bar, biz, baz) => | ||
| 436 | @foo = foo | ||
| 437 | @bar = bar | ||
| 438 | @@biz = biz | ||
| 439 | @@baz = baz | ||
| 440 | ``` | ||
| 441 | |||
| 442 | </YueDisplay> | ||
| 443 | |||
| 444 | You can also use this syntax for a common function to initialize a object's fields. | ||
| 445 | |||
| 446 | ```yuescript | ||
| 447 | new = (@fieldA, @fieldB) => @ | ||
| 448 | obj = new {}, 123, "abc" | ||
| 449 | print obj | ||
| 450 | ``` | ||
| 451 | <YueDisplay> | ||
| 452 | |||
| 453 | ```yue | ||
| 454 | new = (@fieldA, @fieldB) => @ | ||
| 455 | obj = new {}, 123, "abc" | ||
| 456 | print obj | ||
| 457 | ``` | ||
| 458 | |||
| 459 | </YueDisplay> | ||
| 460 | |||
| 461 | ## Class Expressions | ||
| 462 | |||
| 463 | The class syntax can also be used as an expression which can be assigned to a variable or explicitly returned. | ||
| 464 | |||
| 465 | ```yuescript | ||
| 466 | x = class Bucket | ||
| 467 | drops: 0 | ||
| 468 | add_drop: => @drops += 1 | ||
| 469 | ``` | ||
| 470 | <YueDisplay> | ||
| 471 | |||
| 472 | ```yue | ||
| 473 | x = class Bucket | ||
| 474 | drops: 0 | ||
| 475 | add_drop: => @drops += 1 | ||
| 476 | ``` | ||
| 477 | |||
| 478 | </YueDisplay> | ||
| 479 | |||
| 480 | ## Anonymous classes | ||
| 481 | |||
| 482 | The name can be left out when declaring a class. The __name attribute will be nil, unless the class expression is in an assignment. The name on the left hand side of the assignment is used instead of nil. | ||
| 483 | |||
| 484 | ```yuescript | ||
| 485 | BigBucket = class extends Bucket | ||
| 486 | add_drop: => @drops += 10 | ||
| 487 | |||
| 488 | assert Bucket.__name == "BigBucket" | ||
| 489 | ``` | ||
| 490 | <YueDisplay> | ||
| 491 | |||
| 492 | ```yue | ||
| 493 | BigBucket = class extends Bucket | ||
| 494 | add_drop: => @drops += 10 | ||
| 495 | |||
| 496 | assert Bucket.__name == "BigBucket" | ||
| 497 | ``` | ||
| 498 | |||
| 499 | </YueDisplay> | ||
| 500 | |||
| 501 | You can even leave off the body, meaning you can write a blank anonymous class like this: | ||
| 502 | |||
| 503 | ```yuescript | ||
| 504 | x = class | ||
| 505 | ``` | ||
| 506 | <YueDisplay> | ||
| 507 | |||
| 508 | ```yue | ||
| 509 | x = class | ||
| 510 | ``` | ||
| 511 | |||
| 512 | </YueDisplay> | ||
| 513 | |||
| 514 | ## Class Mixing | ||
| 515 | |||
| 516 | You can do mixing with keyword `using` to copy functions from either a plain table or a predefined class object into your new class. When doing mixing with a plain table, you can override the class indexing function (metamethod `__index`) to your customized implementation. When doing mixing with an existing class object, the class object's metamethods won't be copied. | ||
| 517 | |||
| 518 | ```yuescript | ||
| 519 | MyIndex = __index: var: 1 | ||
| 520 | |||
| 521 | class X using MyIndex | ||
| 522 | func: => | ||
| 523 | print 123 | ||
| 524 | |||
| 525 | x = X! | ||
| 526 | print x.var | ||
| 527 | |||
| 528 | class Y using X | ||
| 529 | |||
| 530 | y = Y! | ||
| 531 | y\func! | ||
| 532 | |||
| 533 | assert y.__class.__parent ~= X -- X is not parent of Y | ||
| 534 | ``` | ||
| 535 | <YueDisplay> | ||
| 536 | |||
| 537 | ```yue | ||
| 538 | MyIndex = __index: var: 1 | ||
| 539 | |||
| 540 | class X using MyIndex | ||
| 541 | func: => | ||
| 542 | print 123 | ||
| 543 | |||
| 544 | x = X! | ||
| 545 | print x.var | ||
| 546 | |||
| 547 | class Y using X | ||
| 548 | |||
| 549 | y = Y! | ||
| 550 | y\func! | ||
| 551 | |||
| 552 | assert y.__class.__parent ~= X -- X is not parent of Y | ||
| 553 | ``` | ||
| 554 | |||
| 555 | </YueDisplay> | ||
diff --git a/doc/docs/de/doc/operator.md b/doc/docs/de/doc/operator.md new file mode 100644 index 0000000..9a2e89b --- /dev/null +++ b/doc/docs/de/doc/operator.md | |||
| @@ -0,0 +1,460 @@ | |||
| 1 | # Operator | ||
| 2 | |||
| 3 | All of Lua's binary and unary operators are available. Additionally **!=** is as an alias for **~=**, and either **\\** or **::** can be used to write a chaining function call like `tb\func!` or `tb::func!`. And Yuescipt offers some other special operators to write more expressive codes. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | tb\func! if tb ~= nil | ||
| 7 | tb::func! if tb != nil | ||
| 8 | ``` | ||
| 9 | <YueDisplay> | ||
| 10 | |||
| 11 | ```yue | ||
| 12 | tb\func! if tb ~= nil | ||
| 13 | tb::func! if tb != nil | ||
| 14 | ``` | ||
| 15 | |||
| 16 | </YueDisplay> | ||
| 17 | |||
| 18 | ## Chaining Comparisons | ||
| 19 | |||
| 20 | Comparisons can be arbitrarily chained: | ||
| 21 | |||
| 22 | ```yuescript | ||
| 23 | print 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5 | ||
| 24 | -- output: true | ||
| 25 | |||
| 26 | a = 5 | ||
| 27 | print 1 <= a <= 10 | ||
| 28 | -- output: true | ||
| 29 | ``` | ||
| 30 | <YueDisplay> | ||
| 31 | |||
| 32 | ```yue | ||
| 33 | print 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5 | ||
| 34 | -- output: true | ||
| 35 | |||
| 36 | a = 5 | ||
| 37 | print 1 <= a <= 10 | ||
| 38 | -- output: true | ||
| 39 | ``` | ||
| 40 | |||
| 41 | </YueDisplay> | ||
| 42 | |||
| 43 | Note the evaluation behavior of chained comparisons: | ||
| 44 | |||
| 45 | ```yuescript | ||
| 46 | v = (x) -> | ||
| 47 | print x | ||
| 48 | x | ||
| 49 | |||
| 50 | print v(1) < v(2) <= v(3) | ||
| 51 | --[[ | ||
| 52 | output: | ||
| 53 | 2 | ||
| 54 | 1 | ||
| 55 | 3 | ||
| 56 | true | ||
| 57 | ]] | ||
| 58 | |||
| 59 | print v(1) > v(2) <= v(3) | ||
| 60 | --[[ | ||
| 61 | output: | ||
| 62 | 2 | ||
| 63 | 1 | ||
| 64 | false | ||
| 65 | ]] | ||
| 66 | ``` | ||
| 67 | <YueDisplay> | ||
| 68 | |||
| 69 | ```yue | ||
| 70 | v = (x) -> | ||
| 71 | print x | ||
| 72 | x | ||
| 73 | |||
| 74 | print v(1) < v(2) <= v(3) | ||
| 75 | --[[ | ||
| 76 | output: | ||
| 77 | 2 | ||
| 78 | 1 | ||
| 79 | 3 | ||
| 80 | true | ||
| 81 | ]] | ||
| 82 | |||
| 83 | print v(1) > v(2) <= v(3) | ||
| 84 | --[[ | ||
| 85 | output: | ||
| 86 | 2 | ||
| 87 | 1 | ||
| 88 | false | ||
| 89 | ]] | ||
| 90 | ``` | ||
| 91 | |||
| 92 | </YueDisplay> | ||
| 93 | |||
| 94 | The middle expression is only evaluated once, rather than twice as it would be if the expression were written as `v(1) < v(2) and v(2) <= v(3)`. However, the order of evaluations in a chained comparison is undefined. It is strongly recommended not to use expressions with side effects (such as printing) in chained comparisons. If side effects are required, the short-circuit `and` operator should be used explicitly. | ||
| 95 | |||
| 96 | ## Table Appending | ||
| 97 | |||
| 98 | The **[] =** operator is used to append values to tables. | ||
| 99 | |||
| 100 | ```yuescript | ||
| 101 | tab = [] | ||
| 102 | tab[] = "Value" | ||
| 103 | ``` | ||
| 104 | <YueDisplay> | ||
| 105 | |||
| 106 | ```yue | ||
| 107 | tab = [] | ||
| 108 | tab[] = "Value" | ||
| 109 | ``` | ||
| 110 | |||
| 111 | </YueDisplay> | ||
| 112 | |||
| 113 | You can also use the spread operator `...` to append all elements from one list to another: | ||
| 114 | |||
| 115 | ```yuescript | ||
| 116 | tbA = [1, 2, 3] | ||
| 117 | tbB = [4, 5, 6] | ||
| 118 | tbA[] = ...tbB | ||
| 119 | -- tbA is now [1, 2, 3, 4, 5, 6] | ||
| 120 | ``` | ||
| 121 | <YueDisplay> | ||
| 122 | |||
| 123 | ```yue | ||
| 124 | tbA = [1, 2, 3] | ||
| 125 | tbB = [4, 5, 6] | ||
| 126 | tbA[] = ...tbB | ||
| 127 | -- tbA is now [1, 2, 3, 4, 5, 6] | ||
| 128 | ``` | ||
| 129 | |||
| 130 | </YueDisplay> | ||
| 131 | |||
| 132 | ## Table Spreading | ||
| 133 | |||
| 134 | You can concatenate array tables or hash tables using spread operator `...` before expressions in table literals. | ||
| 135 | |||
| 136 | ```yuescript | ||
| 137 | parts = | ||
| 138 | * "shoulders" | ||
| 139 | * "knees" | ||
| 140 | lyrics = | ||
| 141 | * "head" | ||
| 142 | * ...parts | ||
| 143 | * "and" | ||
| 144 | * "toes" | ||
| 145 | |||
| 146 | copy = {...other} | ||
| 147 | |||
| 148 | a = {1, 2, 3, x: 1} | ||
| 149 | b = {4, 5, y: 1} | ||
| 150 | merge = {...a, ...b} | ||
| 151 | ``` | ||
| 152 | <YueDisplay> | ||
| 153 | |||
| 154 | ```yue | ||
| 155 | parts = | ||
| 156 | * "shoulders" | ||
| 157 | * "knees" | ||
| 158 | lyrics = | ||
| 159 | * "head" | ||
| 160 | * ...parts | ||
| 161 | * "and" | ||
| 162 | * "toes" | ||
| 163 | |||
| 164 | copy = {...other} | ||
| 165 | |||
| 166 | a = {1, 2, 3, x: 1} | ||
| 167 | b = {4, 5, y: 1} | ||
| 168 | merge = {...a, ...b} | ||
| 169 | ``` | ||
| 170 | |||
| 171 | </YueDisplay> | ||
| 172 | |||
| 173 | ## Table Reversed Indexing | ||
| 174 | |||
| 175 | You can use the **#** operator to get the last elements of a table. | ||
| 176 | |||
| 177 | ```yuescript | ||
| 178 | last = data.items[#] | ||
| 179 | second_last = data.items[#-1] | ||
| 180 | data.items[#] = 1 | ||
| 181 | ``` | ||
| 182 | <YueDisplay> | ||
| 183 | |||
| 184 | ```yue | ||
| 185 | last = data.items[#] | ||
| 186 | second_last = data.items[#-1] | ||
| 187 | data.items[#] = 1 | ||
| 188 | ``` | ||
| 189 | |||
| 190 | </YueDisplay> | ||
| 191 | |||
| 192 | ## Metatable | ||
| 193 | |||
| 194 | The **<>** operator can be used as a shortcut for metatable manipulation. | ||
| 195 | |||
| 196 | ### Metatable Creation | ||
| 197 | |||
| 198 | Create normal table with empty bracekets **<>** or metamethod key which is surrounded by **<>**. | ||
| 199 | |||
| 200 | ```yuescript | ||
| 201 | mt = {} | ||
| 202 | add = (right) => <>: mt, value: @value + right.value | ||
| 203 | mt.__add = add | ||
| 204 | |||
| 205 | a = <>: mt, value: 1 | ||
| 206 | -- set field with variable of the same name | ||
| 207 | b = :<add>, value: 2 | ||
| 208 | c = <add>: mt.__add, value: 3 | ||
| 209 | |||
| 210 | d = a + b + c | ||
| 211 | print d.value | ||
| 212 | |||
| 213 | close _ = <close>: -> print "out of scope" | ||
| 214 | ``` | ||
| 215 | <YueDisplay> | ||
| 216 | |||
| 217 | ```yue | ||
| 218 | mt = {} | ||
| 219 | add = (right) => <>: mt, value: @value + right.value | ||
| 220 | mt.__add = add | ||
| 221 | |||
| 222 | a = <>: mt, value: 1 | ||
| 223 | -- set field with variable of the same name | ||
| 224 | b = :<add>, value: 2 | ||
| 225 | c = <add>: mt.__add, value: 3 | ||
| 226 | |||
| 227 | d = a + b + c | ||
| 228 | print d.value | ||
| 229 | |||
| 230 | close _ = <close>: -> print "out of scope" | ||
| 231 | ``` | ||
| 232 | |||
| 233 | </YueDisplay> | ||
| 234 | |||
| 235 | ### Metatable Accessing | ||
| 236 | |||
| 237 | Accessing metatable with **<>** or metamethod name surrounded by **<>** or writing some expression in **<>**. | ||
| 238 | |||
| 239 | ```yuescript | ||
| 240 | -- create with metatable containing field "value" | ||
| 241 | tb = <"value">: 123 | ||
| 242 | tb.<index> = tb.<> | ||
| 243 | print tb.value | ||
| 244 | |||
| 245 | tb.<> = __index: {item: "hello"} | ||
| 246 | print tb.item | ||
| 247 | ``` | ||
| 248 | <YueDisplay> | ||
| 249 | |||
| 250 | ```yue | ||
| 251 | -- create with metatable containing field "value" | ||
| 252 | tb = <"value">: 123 | ||
| 253 | tb.<index> = tb.<> | ||
| 254 | print tb.value | ||
| 255 | tb.<> = __index: {item: "hello"} | ||
| 256 | print tb.item | ||
| 257 | ``` | ||
| 258 | |||
| 259 | </YueDisplay> | ||
| 260 | |||
| 261 | ### Metatable Destructure | ||
| 262 | |||
| 263 | Destruct metatable with metamethod key surrounded by **<>**. | ||
| 264 | |||
| 265 | ```yuescript | ||
| 266 | {item, :new, :<close>, <index>: getter} = tb | ||
| 267 | print item, new, close, getter | ||
| 268 | ``` | ||
| 269 | <YueDisplay> | ||
| 270 | |||
| 271 | ```yue | ||
| 272 | {item, :new, :<close>, <index>: getter} = tb | ||
| 273 | print item, new, close, getter | ||
| 274 | ``` | ||
| 275 | |||
| 276 | </YueDisplay> | ||
| 277 | |||
| 278 | ## Existence | ||
| 279 | |||
| 280 | The **?** operator can be used in a variety of contexts to check for existence. | ||
| 281 | |||
| 282 | ```yuescript | ||
| 283 | func?! | ||
| 284 | print abc?["hello world"]?.xyz | ||
| 285 | |||
| 286 | x = tab?.value | ||
| 287 | len = utf8?.len or string?.len or (o) -> #o | ||
| 288 | |||
| 289 | if print and x? | ||
| 290 | print x | ||
| 291 | |||
| 292 | with? io.open "test.txt", "w" | ||
| 293 | \write "hello" | ||
| 294 | \close! | ||
| 295 | ``` | ||
| 296 | <YueDisplay> | ||
| 297 | |||
| 298 | ```yue | ||
| 299 | func?! | ||
| 300 | print abc?["hello world"]?.xyz | ||
| 301 | |||
| 302 | x = tab?.value | ||
| 303 | len = utf8?.len or string?.len or (o) -> #o | ||
| 304 | |||
| 305 | if print and x? | ||
| 306 | print x | ||
| 307 | |||
| 308 | with? io.open "test.txt", "w" | ||
| 309 | \write "hello" | ||
| 310 | \close! | ||
| 311 | ``` | ||
| 312 | |||
| 313 | </YueDisplay> | ||
| 314 | |||
| 315 | ## Piping | ||
| 316 | |||
| 317 | Instead of a series of nested function calls, you can pipe values with operator **|>**. | ||
| 318 | |||
| 319 | ```yuescript | ||
| 320 | "hello" |> print | ||
| 321 | 1 |> print 2 -- insert pipe item as the first argument | ||
| 322 | 2 |> print 1, _, 3 -- pipe with a placeholder | ||
| 323 | |||
| 324 | -- pipe expression in multiline | ||
| 325 | readFile "example.txt" | ||
| 326 | |> extract language, {} | ||
| 327 | |> parse language | ||
| 328 | |> emit | ||
| 329 | |> render | ||
| 330 | |||
| 331 | ``` | ||
| 332 | <YueDisplay> | ||
| 333 | |||
| 334 | ```yue | ||
| 335 | "hello" |> print | ||
| 336 | 1 |> print 2 -- insert pipe item as the first argument | ||
| 337 | 2 |> print 1, _, 3 -- pipe with a placeholder | ||
| 338 | -- pipe expression in multiline | ||
| 339 | readFile "example.txt" | ||
| 340 | |> extract language, {} | ||
| 341 | |> parse language | ||
| 342 | |> emit | ||
| 343 | |> render | ||
| 344 | |||
| 345 | ``` | ||
| 346 | |||
| 347 | </YueDisplay> | ||
| 348 | |||
| 349 | ## Nil Coalescing | ||
| 350 | |||
| 351 | The nil-coalescing operator **??** returns the value of its left-hand operand if it isn't **nil**; otherwise, it evaluates the right-hand operand and returns its result. The **??** operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-nil. | ||
| 352 | ```yuescript | ||
| 353 | local a, b, c, d | ||
| 354 | a = b ?? c ?? d | ||
| 355 | func a ?? {} | ||
| 356 | |||
| 357 | a ??= false | ||
| 358 | ``` | ||
| 359 | <YueDisplay> | ||
| 360 | |||
| 361 | ```yue | ||
| 362 | local a, b, c, d | ||
| 363 | a = b ?? c ?? d | ||
| 364 | func a ?? {} | ||
| 365 | a ??= false | ||
| 366 | ``` | ||
| 367 | |||
| 368 | </YueDisplay> | ||
| 369 | |||
| 370 | ## Implicit Object | ||
| 371 | |||
| 372 | You can write a list of implicit structures that starts with the symbol **\*** or **-** inside a table block. If you are creating implicit object, the fields of the object must be with the same indent. | ||
| 373 | |||
| 374 | ```yuescript | ||
| 375 | -- assignment with implicit object | ||
| 376 | list = | ||
| 377 | * 1 | ||
| 378 | * 2 | ||
| 379 | * 3 | ||
| 380 | |||
| 381 | -- function call with implicit object | ||
| 382 | func | ||
| 383 | * 1 | ||
| 384 | * 2 | ||
| 385 | * 3 | ||
| 386 | |||
| 387 | -- return with implicit object | ||
| 388 | f = -> | ||
| 389 | return | ||
| 390 | * 1 | ||
| 391 | * 2 | ||
| 392 | * 3 | ||
| 393 | |||
| 394 | -- table with implicit object | ||
| 395 | tb = | ||
| 396 | name: "abc" | ||
| 397 | |||
| 398 | values: | ||
| 399 | - "a" | ||
| 400 | - "b" | ||
| 401 | - "c" | ||
| 402 | |||
| 403 | objects: | ||
| 404 | - name: "a" | ||
| 405 | value: 1 | ||
| 406 | func: => @value + 1 | ||
| 407 | tb: | ||
| 408 | fieldA: 1 | ||
| 409 | |||
| 410 | - name: "b" | ||
| 411 | value: 2 | ||
| 412 | func: => @value + 2 | ||
| 413 | tb: { } | ||
| 414 | |||
| 415 | ``` | ||
| 416 | <YueDisplay> | ||
| 417 | |||
| 418 | ```yue | ||
| 419 | -- assignment with implicit object | ||
| 420 | list = | ||
| 421 | * 1 | ||
| 422 | * 2 | ||
| 423 | * 3 | ||
| 424 | |||
| 425 | -- function call with implicit object | ||
| 426 | func | ||
| 427 | * 1 | ||
| 428 | * 2 | ||
| 429 | * 3 | ||
| 430 | |||
| 431 | -- return with implicit object | ||
| 432 | f = -> | ||
| 433 | return | ||
| 434 | * 1 | ||
| 435 | * 2 | ||
| 436 | * 3 | ||
| 437 | |||
| 438 | -- table with implicit object | ||
| 439 | tb = | ||
| 440 | name: "abc" | ||
| 441 | |||
| 442 | values: | ||
| 443 | - "a" | ||
| 444 | - "b" | ||
| 445 | - "c" | ||
| 446 | |||
| 447 | objects: | ||
| 448 | - name: "a" | ||
| 449 | value: 1 | ||
| 450 | func: => @value + 1 | ||
| 451 | tb: | ||
| 452 | fieldA: 1 | ||
| 453 | |||
| 454 | - name: "b" | ||
| 455 | value: 2 | ||
| 456 | func: => @value + 2 | ||
| 457 | tb: { } | ||
| 458 | ``` | ||
| 459 | |||
| 460 | </YueDisplay> | ||
diff --git a/doc/docs/de/doc/switch.md b/doc/docs/de/doc/switch.md new file mode 100644 index 0000000..f503a80 --- /dev/null +++ b/doc/docs/de/doc/switch.md | |||
| @@ -0,0 +1,296 @@ | |||
| 1 | # Switch | ||
| 2 | |||
| 3 | The switch statement is shorthand for writing a series of if statements that check against the same value. Note that the value is only evaluated once. Like if statements, switches can have an else block to handle no matches. Comparison is done with the == operator. In switch statement, you can also use assignment expression to store temporary variable value. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | switch name := "Dan" | ||
| 7 | when "Robert" | ||
| 8 | print "You are Robert" | ||
| 9 | when "Dan", "Daniel" | ||
| 10 | print "Your name, it's Dan" | ||
| 11 | else | ||
| 12 | print "I don't know about you with name #{name}" | ||
| 13 | ``` | ||
| 14 | <YueDisplay> | ||
| 15 | |||
| 16 | ```yue | ||
| 17 | switch name := "Dan" | ||
| 18 | when "Robert" | ||
| 19 | print "You are Robert" | ||
| 20 | when "Dan", "Daniel" | ||
| 21 | print "Your name, it's Dan" | ||
| 22 | else | ||
| 23 | print "I don't know about you with name #{name}" | ||
| 24 | ``` | ||
| 25 | |||
| 26 | </YueDisplay> | ||
| 27 | |||
| 28 | A switch when clause can match against multiple values by listing them out comma separated. | ||
| 29 | |||
| 30 | Switches can be used as expressions as well, here we can assign the result of the switch to a variable: | ||
| 31 | |||
| 32 | ```yuescript | ||
| 33 | b = 1 | ||
| 34 | next_number = switch b | ||
| 35 | when 1 | ||
| 36 | 2 | ||
| 37 | when 2 | ||
| 38 | 3 | ||
| 39 | else | ||
| 40 | error "can't count that high!" | ||
| 41 | ``` | ||
| 42 | <YueDisplay> | ||
| 43 | |||
| 44 | ```yue | ||
| 45 | b = 1 | ||
| 46 | next_number = switch b | ||
| 47 | when 1 | ||
| 48 | 2 | ||
| 49 | when 2 | ||
| 50 | 3 | ||
| 51 | else | ||
| 52 | error "can't count that high!" | ||
| 53 | ``` | ||
| 54 | |||
| 55 | </YueDisplay> | ||
| 56 | |||
| 57 | We can use the then keyword to write a switch's when block on a single line. No extra keyword is needed to write the else block on a single line. | ||
| 58 | |||
| 59 | ```yuescript | ||
| 60 | msg = switch math.random(1, 5) | ||
| 61 | when 1 then "you are lucky" | ||
| 62 | when 2 then "you are almost lucky" | ||
| 63 | else "not so lucky" | ||
| 64 | ``` | ||
| 65 | <YueDisplay> | ||
| 66 | |||
| 67 | ```yue | ||
| 68 | msg = switch math.random(1, 5) | ||
| 69 | when 1 then "you are lucky" | ||
| 70 | when 2 then "you are almost lucky" | ||
| 71 | else "not so lucky" | ||
| 72 | ``` | ||
| 73 | |||
| 74 | </YueDisplay> | ||
| 75 | |||
| 76 | If you want to write code with one less indent when writing a switch statement, you can put the first when clause on the statement start line, and then all other clauses can be written with one less indent. | ||
| 77 | |||
| 78 | ```yuescript | ||
| 79 | switch math.random(1, 5) | ||
| 80 | when 1 | ||
| 81 | print "you are lucky" -- two indents | ||
| 82 | else | ||
| 83 | print "not so lucky" | ||
| 84 | |||
| 85 | switch math.random(1, 5) when 1 | ||
| 86 | print "you are lucky" -- one indent | ||
| 87 | else | ||
| 88 | print "not so lucky" | ||
| 89 | ``` | ||
| 90 | <YueDisplay> | ||
| 91 | |||
| 92 | ```yue | ||
| 93 | switch math.random(1, 5) | ||
| 94 | when 1 | ||
| 95 | print "you are lucky" -- two indents | ||
| 96 | else | ||
| 97 | print "not so lucky" | ||
| 98 | |||
| 99 | switch math.random(1, 5) when 1 | ||
| 100 | print "you are lucky" -- one indent | ||
| 101 | else | ||
| 102 | print "not so lucky" | ||
| 103 | ``` | ||
| 104 | |||
| 105 | </YueDisplay> | ||
| 106 | |||
| 107 | It is worth noting the order of the case comparison expression. The case's expression is on the left hand side. This can be useful if the case's expression wants to overwrite how the comparison is done by defining an eq metamethod. | ||
| 108 | |||
| 109 | ## Table Matching | ||
| 110 | |||
| 111 | You can do table matching in a switch when clause, if the table can be destructured by a specific structure and get non-nil values. | ||
| 112 | |||
| 113 | ```yuescript | ||
| 114 | items = | ||
| 115 | * x: 100 | ||
| 116 | y: 200 | ||
| 117 | * width: 300 | ||
| 118 | height: 400 | ||
| 119 | |||
| 120 | for item in *items | ||
| 121 | switch item | ||
| 122 | when :x, :y | ||
| 123 | print "Vec2 #{x}, #{y}" | ||
| 124 | when :width, :height | ||
| 125 | print "size #{width}, #{height}" | ||
| 126 | ``` | ||
| 127 | <YueDisplay> | ||
| 128 | |||
| 129 | ```yue | ||
| 130 | items = | ||
| 131 | * x: 100 | ||
| 132 | y: 200 | ||
| 133 | * width: 300 | ||
| 134 | height: 400 | ||
| 135 | |||
| 136 | for item in *items | ||
| 137 | switch item | ||
| 138 | when :x, :y | ||
| 139 | print "Vec2 #{x}, #{y}" | ||
| 140 | when :width, :height | ||
| 141 | print "size #{width}, #{height}" | ||
| 142 | ``` | ||
| 143 | |||
| 144 | </YueDisplay> | ||
| 145 | |||
| 146 | You can use default values to optionally destructure the table for some fields. | ||
| 147 | |||
| 148 | ```yuescript | ||
| 149 | item = {} | ||
| 150 | |||
| 151 | {pos: {:x = 50, :y = 200}} = item -- get error: attempt to index a nil value (field 'pos') | ||
| 152 | |||
| 153 | switch item | ||
| 154 | when {pos: {:x = 50, :y = 200}} | ||
| 155 | print "Vec2 #{x}, #{y}" -- table destructuring will still pass | ||
| 156 | ``` | ||
| 157 | <YueDisplay> | ||
| 158 | |||
| 159 | ```yue | ||
| 160 | item = {} | ||
| 161 | |||
| 162 | {pos: {:x = 50, :y = 200}} = item -- get error: attempt to index a nil value (field 'pos') | ||
| 163 | |||
| 164 | switch item | ||
| 165 | when {pos: {:x = 50, :y = 200}} | ||
| 166 | print "Vec2 #{x}, #{y}" -- table destructuring will still pass | ||
| 167 | ``` | ||
| 168 | |||
| 169 | </YueDisplay> | ||
| 170 | |||
| 171 | You can also match against array elements, table fields, and even nested structures with array or table literals. | ||
| 172 | |||
| 173 | Match against array elements. | ||
| 174 | |||
| 175 | ```yuescript | ||
| 176 | switch tb | ||
| 177 | when [1, 2, 3] | ||
| 178 | print "1, 2, 3" | ||
| 179 | when [1, b, 3] | ||
| 180 | print "1, #{b}, 3" | ||
| 181 | when [1, 2, b = 3] -- b has a default value | ||
| 182 | print "1, 2, #{b}" | ||
| 183 | ``` | ||
| 184 | <YueDisplay> | ||
| 185 | |||
| 186 | ```yue | ||
| 187 | switch tb | ||
| 188 | when [1, 2, 3] | ||
| 189 | print "1, 2, 3" | ||
| 190 | when [1, b, 3] | ||
| 191 | print "1, #{b}, 3" | ||
| 192 | when [1, 2, b = 3] -- b has a default value | ||
| 193 | print "1, 2, #{b}" | ||
| 194 | ``` | ||
| 195 | |||
| 196 | </YueDisplay> | ||
| 197 | |||
| 198 | Match against table fields with destructuring. | ||
| 199 | |||
| 200 | ```yuescript | ||
| 201 | switch tb | ||
| 202 | when success: true, :result | ||
| 203 | print "success", result | ||
| 204 | when success: false | ||
| 205 | print "failed", result | ||
| 206 | else | ||
| 207 | print "invalid" | ||
| 208 | ``` | ||
| 209 | <YueDisplay> | ||
| 210 | |||
| 211 | ```yue | ||
| 212 | switch tb | ||
| 213 | when success: true, :result | ||
| 214 | print "success", result | ||
| 215 | when success: false | ||
| 216 | print "failed", result | ||
| 217 | else | ||
| 218 | print "invalid" | ||
| 219 | ``` | ||
| 220 | |||
| 221 | </YueDisplay> | ||
| 222 | |||
| 223 | Match against nested table structures. | ||
| 224 | |||
| 225 | ```yuescript | ||
| 226 | switch tb | ||
| 227 | when data: {type: "success", :content} | ||
| 228 | print "success", content | ||
| 229 | when data: {type: "error", :content} | ||
| 230 | print "failed", content | ||
| 231 | else | ||
| 232 | print "invalid" | ||
| 233 | ``` | ||
| 234 | <YueDisplay> | ||
| 235 | |||
| 236 | ```yue | ||
| 237 | switch tb | ||
| 238 | when data: {type: "success", :content} | ||
| 239 | print "success", content | ||
| 240 | when data: {type: "error", :content} | ||
| 241 | print "failed", content | ||
| 242 | else | ||
| 243 | print "invalid" | ||
| 244 | ``` | ||
| 245 | |||
| 246 | </YueDisplay> | ||
| 247 | |||
| 248 | Match against array of tables. | ||
| 249 | |||
| 250 | ```yuescript | ||
| 251 | switch tb | ||
| 252 | when [ | ||
| 253 | {a: 1, b: 2} | ||
| 254 | {a: 3, b: 4} | ||
| 255 | {a: 5, b: 6} | ||
| 256 | fourth | ||
| 257 | ] | ||
| 258 | print "matched", fourth | ||
| 259 | ``` | ||
| 260 | <YueDisplay> | ||
| 261 | |||
| 262 | ```yue | ||
| 263 | switch tb | ||
| 264 | when [ | ||
| 265 | {a: 1, b: 2} | ||
| 266 | {a: 3, b: 4} | ||
| 267 | {a: 5, b: 6} | ||
| 268 | fourth | ||
| 269 | ] | ||
| 270 | print "matched", fourth | ||
| 271 | ``` | ||
| 272 | |||
| 273 | </YueDisplay> | ||
| 274 | |||
| 275 | Match against a list and capture a range of elements. | ||
| 276 | |||
| 277 | ```yuescript | ||
| 278 | segments = ["admin", "users", "logs", "view"] | ||
| 279 | switch segments | ||
| 280 | when [...groups, resource, action] | ||
| 281 | print "Group:", groups -- prints: {"admin", "users"} | ||
| 282 | print "Resource:", resource -- prints: "logs" | ||
| 283 | print "Action:", action -- prints: "view" | ||
| 284 | ``` | ||
| 285 | <YueDisplay> | ||
| 286 | |||
| 287 | ```yue | ||
| 288 | segments = ["admin", "users", "logs", "view"] | ||
| 289 | switch segments | ||
| 290 | when [...groups, resource, action] | ||
| 291 | print "Group:", groups -- prints: {"admin", "users"} | ||
| 292 | print "Resource:", resource -- prints: "logs" | ||
| 293 | print "Action:", action -- prints: "view" | ||
| 294 | ``` | ||
| 295 | |||
| 296 | </YueDisplay> | ||
diff --git a/doc/docs/de/doc/table-literals.md b/doc/docs/de/doc/table-literals.md new file mode 100644 index 0000000..c1adcab --- /dev/null +++ b/doc/docs/de/doc/table-literals.md | |||
| @@ -0,0 +1,168 @@ | |||
| 1 | # Table Literals | ||
| 2 | |||
| 3 | Like in Lua, tables are delimited in curly braces. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | some_values = [1, 2, 3, 4] | ||
| 7 | ``` | ||
| 8 | <YueDisplay> | ||
| 9 | |||
| 10 | ```yue | ||
| 11 | some_values = [1, 2, 3, 4] | ||
| 12 | ``` | ||
| 13 | |||
| 14 | </YueDisplay> | ||
| 15 | |||
| 16 | Unlike Lua, assigning a value to a key in a table is done with **:** (instead of **=**). | ||
| 17 | |||
| 18 | ```yuescript | ||
| 19 | some_values = { | ||
| 20 | name: "Bill", | ||
| 21 | age: 200, | ||
| 22 | ["favorite food"]: "rice" | ||
| 23 | } | ||
| 24 | ``` | ||
| 25 | <YueDisplay> | ||
| 26 | |||
| 27 | ```yue | ||
| 28 | some_values = { | ||
| 29 | name: "Bill", | ||
| 30 | age: 200, | ||
| 31 | ["favorite food"]: "rice" | ||
| 32 | } | ||
| 33 | ``` | ||
| 34 | |||
| 35 | </YueDisplay> | ||
| 36 | |||
| 37 | The curly braces can be left off if a single table of key value pairs is being assigned. | ||
| 38 | |||
| 39 | ```yuescript | ||
| 40 | profile = | ||
| 41 | height: "4 feet", | ||
| 42 | shoe_size: 13, | ||
| 43 | favorite_foods: ["ice cream", "donuts"] | ||
| 44 | ``` | ||
| 45 | <YueDisplay> | ||
| 46 | |||
| 47 | ```yue | ||
| 48 | profile = | ||
| 49 | height: "4 feet", | ||
| 50 | shoe_size: 13, | ||
| 51 | favorite_foods: ["ice cream", "donuts"] | ||
| 52 | ``` | ||
| 53 | |||
| 54 | </YueDisplay> | ||
| 55 | |||
| 56 | Newlines can be used to delimit values instead of a comma (or both): | ||
| 57 | |||
| 58 | ```yuescript | ||
| 59 | values = { | ||
| 60 | 1, 2, 3, 4 | ||
| 61 | 5, 6, 7, 8 | ||
| 62 | name: "superman" | ||
| 63 | occupation: "crime fighting" | ||
| 64 | } | ||
| 65 | ``` | ||
| 66 | <YueDisplay> | ||
| 67 | |||
| 68 | ```yue | ||
| 69 | values = { | ||
| 70 | 1, 2, 3, 4 | ||
| 71 | 5, 6, 7, 8 | ||
| 72 | name: "superman" | ||
| 73 | occupation: "crime fighting" | ||
| 74 | } | ||
| 75 | ``` | ||
| 76 | |||
| 77 | </YueDisplay> | ||
| 78 | |||
| 79 | When creating a single line table literal, the curly braces can also be left off: | ||
| 80 | |||
| 81 | ```yuescript | ||
| 82 | my_function dance: "Tango", partner: "none" | ||
| 83 | |||
| 84 | y = type: "dog", legs: 4, tails: 1 | ||
| 85 | ``` | ||
| 86 | <YueDisplay> | ||
| 87 | |||
| 88 | ```yue | ||
| 89 | my_function dance: "Tango", partner: "none" | ||
| 90 | |||
| 91 | y = type: "dog", legs: 4, tails: 1 | ||
| 92 | ``` | ||
| 93 | |||
| 94 | </YueDisplay> | ||
| 95 | |||
| 96 | The keys of a table literal can be language keywords without being escaped: | ||
| 97 | |||
| 98 | ```yuescript | ||
| 99 | tbl = { | ||
| 100 | do: "something" | ||
| 101 | end: "hunger" | ||
| 102 | } | ||
| 103 | ``` | ||
| 104 | <YueDisplay> | ||
| 105 | |||
| 106 | ```yue | ||
| 107 | tbl = { | ||
| 108 | do: "something" | ||
| 109 | end: "hunger" | ||
| 110 | } | ||
| 111 | ``` | ||
| 112 | |||
| 113 | </YueDisplay> | ||
| 114 | |||
| 115 | If you are constructing a table out of variables and wish the keys to be the same as the variable names, then the **:** prefix operator can be used: | ||
| 116 | |||
| 117 | ```yuescript | ||
| 118 | hair = "golden" | ||
| 119 | height = 200 | ||
| 120 | person = { :hair, :height, shoe_size: 40 } | ||
| 121 | |||
| 122 | print_table :hair, :height | ||
| 123 | ``` | ||
| 124 | <YueDisplay> | ||
| 125 | |||
| 126 | ```yue | ||
| 127 | hair = "golden" | ||
| 128 | height = 200 | ||
| 129 | person = { :hair, :height, shoe_size: 40 } | ||
| 130 | |||
| 131 | print_table :hair, :height | ||
| 132 | ``` | ||
| 133 | |||
| 134 | </YueDisplay> | ||
| 135 | |||
| 136 | If you want the key of a field in the table to to be result of an expression, then you can wrap it in **[ ]**, just like in Lua. You can also use a string literal directly as a key, leaving out the square brackets. This is useful if your key has any special characters. | ||
| 137 | |||
| 138 | ```yuescript | ||
| 139 | t = { | ||
| 140 | [1 + 2]: "hello" | ||
| 141 | "hello world": true | ||
| 142 | } | ||
| 143 | ``` | ||
| 144 | <YueDisplay> | ||
| 145 | |||
| 146 | ```yue | ||
| 147 | t = { | ||
| 148 | [1 + 2]: "hello" | ||
| 149 | "hello world": true | ||
| 150 | } | ||
| 151 | ``` | ||
| 152 | |||
| 153 | </YueDisplay> | ||
| 154 | |||
| 155 | Lua tables have both an array part and a hash part, but sometimes you want to make a semantic distinction between array and hash usage when writing Lua tables. Then you can write Lua table with **[ ]** instead of **{ }** to represent an array table and writing any key value pair in a list table won't be allowed. | ||
| 156 | |||
| 157 | ```yuescript | ||
| 158 | some_values = [1, 2, 3, 4] | ||
| 159 | list_with_one_element = [1, ] | ||
| 160 | ``` | ||
| 161 | <YueDisplay> | ||
| 162 | |||
| 163 | ```yue | ||
| 164 | some_values = [1, 2, 3, 4] | ||
| 165 | list_with_one_element = [1, ] | ||
| 166 | ``` | ||
| 167 | |||
| 168 | </YueDisplay> | ||
diff --git a/doc/docs/de/doc/the-using-clause-controlling-destructive-assignment.md b/doc/docs/de/doc/the-using-clause-controlling-destructive-assignment.md new file mode 100644 index 0000000..fb9b740 --- /dev/null +++ b/doc/docs/de/doc/the-using-clause-controlling-destructive-assignment.md | |||
| @@ -0,0 +1,98 @@ | |||
| 1 | # The Using Clause; Controlling Destructive Assignment | ||
| 2 | |||
| 3 | While lexical scoping can be a great help in reducing the complexity of the code we write, things can get unwieldy as the code size increases. Consider the following snippet: | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | i = 100 | ||
| 7 | |||
| 8 | -- many lines of code... | ||
| 9 | |||
| 10 | my_func = -> | ||
| 11 | i = 10 | ||
| 12 | while i > 0 | ||
| 13 | print i | ||
| 14 | i -= 1 | ||
| 15 | |||
| 16 | my_func! | ||
| 17 | |||
| 18 | print i -- will print 0 | ||
| 19 | ``` | ||
| 20 | <YueDisplay> | ||
| 21 | |||
| 22 | ```yue | ||
| 23 | i = 100 | ||
| 24 | |||
| 25 | -- many lines of code... | ||
| 26 | |||
| 27 | my_func = -> | ||
| 28 | i = 10 | ||
| 29 | while i > 0 | ||
| 30 | print i | ||
| 31 | i -= 1 | ||
| 32 | |||
| 33 | my_func! | ||
| 34 | |||
| 35 | print i -- will print 0 | ||
| 36 | ``` | ||
| 37 | |||
| 38 | </YueDisplay> | ||
| 39 | |||
| 40 | In my_func, we've overwritten the value of i mistakenly. In this example it is quite obvious, but consider a large, or foreign code base where it isn't clear what names have already been declared. | ||
| 41 | |||
| 42 | It would be helpful to say which variables from the enclosing scope we intend on change, in order to prevent us from changing others by accident. | ||
| 43 | |||
| 44 | The using keyword lets us do that. using nil makes sure that no closed variables are overwritten in assignment. The using clause is placed after the argument list in a function, or in place of it if there are no arguments. | ||
| 45 | |||
| 46 | ```yuescript | ||
| 47 | i = 100 | ||
| 48 | |||
| 49 | my_func = (using nil) -> | ||
| 50 | i = "hello" -- a new local variable is created here | ||
| 51 | |||
| 52 | my_func! | ||
| 53 | print i -- prints 100, i is unaffected | ||
| 54 | ``` | ||
| 55 | <YueDisplay> | ||
| 56 | |||
| 57 | ```yue | ||
| 58 | i = 100 | ||
| 59 | |||
| 60 | my_func = (using nil) -> | ||
| 61 | i = "hello" -- a new local variable is created here | ||
| 62 | |||
| 63 | my_func! | ||
| 64 | print i -- prints 100, i is unaffected | ||
| 65 | ``` | ||
| 66 | |||
| 67 | </YueDisplay> | ||
| 68 | |||
| 69 | Multiple names can be separated by commas. Closure values can still be accessed, they just cant be modified: | ||
| 70 | |||
| 71 | ```yuescript | ||
| 72 | tmp = 1213 | ||
| 73 | i, k = 100, 50 | ||
| 74 | |||
| 75 | my_func = (add using k, i) -> | ||
| 76 | tmp = tmp + add -- a new local tmp is created | ||
| 77 | i += tmp | ||
| 78 | k += tmp | ||
| 79 | |||
| 80 | my_func(22) | ||
| 81 | print i, k -- these have been updated | ||
| 82 | ``` | ||
| 83 | <YueDisplay> | ||
| 84 | |||
| 85 | ```yue | ||
| 86 | tmp = 1213 | ||
| 87 | i, k = 100, 50 | ||
| 88 | |||
| 89 | my_func = (add using k, i) -> | ||
| 90 | tmp = tmp + add -- a new local tmp is created | ||
| 91 | i += tmp | ||
| 92 | k += tmp | ||
| 93 | |||
| 94 | my_func(22) | ||
| 95 | print i, k -- these have been updated | ||
| 96 | ``` | ||
| 97 | |||
| 98 | </YueDisplay> | ||
diff --git a/doc/docs/de/doc/the-yuescript-library.md b/doc/docs/de/doc/the-yuescript-library.md new file mode 100644 index 0000000..3761755 --- /dev/null +++ b/doc/docs/de/doc/the-yuescript-library.md | |||
| @@ -0,0 +1,821 @@ | |||
| 1 | # The YueScript Library | ||
| 2 | |||
| 3 | Access it by `local yue = require("yue")` in Lua. | ||
| 4 | |||
| 5 | ## yue | ||
| 6 | |||
| 7 | **Description:** | ||
| 8 | |||
| 9 | The YueScript language library. | ||
| 10 | |||
| 11 | ### version | ||
| 12 | |||
| 13 | **Type:** Field. | ||
| 14 | |||
| 15 | **Description:** | ||
| 16 | |||
| 17 | The YueScript version. | ||
| 18 | |||
| 19 | **Signature:** | ||
| 20 | ```lua | ||
| 21 | version: string | ||
| 22 | ``` | ||
| 23 | |||
| 24 | ### dirsep | ||
| 25 | |||
| 26 | **Type:** Field. | ||
| 27 | |||
| 28 | **Description:** | ||
| 29 | |||
| 30 | The file separator for the current platform. | ||
| 31 | |||
| 32 | **Signature:** | ||
| 33 | ```lua | ||
| 34 | dirsep: string | ||
| 35 | ``` | ||
| 36 | |||
| 37 | ### yue_compiled | ||
| 38 | |||
| 39 | **Type:** Field. | ||
| 40 | |||
| 41 | **Description:** | ||
| 42 | |||
| 43 | The compiled module code cache. | ||
| 44 | |||
| 45 | **Signature:** | ||
| 46 | ```lua | ||
| 47 | yue_compiled: {string: string} | ||
| 48 | ``` | ||
| 49 | |||
| 50 | ### to_lua | ||
| 51 | |||
| 52 | **Type:** Function. | ||
| 53 | |||
| 54 | **Description:** | ||
| 55 | |||
| 56 | The YueScript compiling function. It compiles the YueScript code to Lua code. | ||
| 57 | |||
| 58 | **Signature:** | ||
| 59 | ```lua | ||
| 60 | to_lua: function(code: string, config?: Config): | ||
| 61 | --[[codes]] string | nil, | ||
| 62 | --[[error]] string | nil, | ||
| 63 | --[[globals]] {{string, integer, integer}} | nil | ||
| 64 | ``` | ||
| 65 | |||
| 66 | **Parameters:** | ||
| 67 | |||
| 68 | | Parameter | Type | Description | | ||
| 69 | | --- | --- | --- | | ||
| 70 | | code | string | The YueScript code. | | ||
| 71 | | config | Config | [Optional] The compiler options. | | ||
| 72 | |||
| 73 | **Returns:** | ||
| 74 | |||
| 75 | | Return Type | Description | | ||
| 76 | | --- | --- | | ||
| 77 | | string \| nil | The compiled Lua code, or nil if the compilation failed. | | ||
| 78 | | string \| nil | The error message, or nil if the compilation succeeded. | | ||
| 79 | | {{string, integer, integer}} \| nil | The global variables appearing in the code (with name, row and column), or nil if the compiler option `lint_global` is false. | | ||
| 80 | |||
| 81 | ### file_exist | ||
| 82 | |||
| 83 | **Type:** Function. | ||
| 84 | |||
| 85 | **Description:** | ||
| 86 | |||
| 87 | The source file existence checking function. Can be overridden to customize the behavior. | ||
| 88 | |||
| 89 | **Signature:** | ||
| 90 | ```lua | ||
| 91 | file_exist: function(filename: string): boolean | ||
| 92 | ``` | ||
| 93 | |||
| 94 | **Parameters:** | ||
| 95 | |||
| 96 | | Parameter | Type | Description | | ||
| 97 | | --- | --- | --- | | ||
| 98 | | filename | string | The file name. | | ||
| 99 | |||
| 100 | **Returns:** | ||
| 101 | |||
| 102 | | Return Type | Description | | ||
| 103 | | --- | --- | | ||
| 104 | | boolean | Whether the file exists. | | ||
| 105 | |||
| 106 | ### read_file | ||
| 107 | |||
| 108 | **Type:** Function. | ||
| 109 | |||
| 110 | **Description:** | ||
| 111 | |||
| 112 | The source file reading function. Can be overridden to customize the behavior. | ||
| 113 | |||
| 114 | **Signature:** | ||
| 115 | ```lua | ||
| 116 | read_file: function(filename: string): string | ||
| 117 | ``` | ||
| 118 | |||
| 119 | **Parameters:** | ||
| 120 | |||
| 121 | | Parameter | Type | Description | | ||
| 122 | | --- | --- | --- | | ||
| 123 | | filename | string | The file name. | | ||
| 124 | |||
| 125 | **Returns:** | ||
| 126 | |||
| 127 | | Return Type | Description | | ||
| 128 | | --- | --- | | ||
| 129 | | string | The file content. | | ||
| 130 | |||
| 131 | ### insert_loader | ||
| 132 | |||
| 133 | **Type:** Function. | ||
| 134 | |||
| 135 | **Description:** | ||
| 136 | |||
| 137 | Insert the YueScript loader to the package loaders (searchers). | ||
| 138 | |||
| 139 | **Signature:** | ||
| 140 | ```lua | ||
| 141 | insert_loader: function(pos?: integer): boolean | ||
| 142 | ``` | ||
| 143 | |||
| 144 | **Parameters:** | ||
| 145 | |||
| 146 | | Parameter | Type | Description | | ||
| 147 | | --- | --- | --- | | ||
| 148 | | pos | integer | [Optional] The position to insert the loader. Default is 3. | | ||
| 149 | |||
| 150 | **Returns:** | ||
| 151 | |||
| 152 | | Return Type | Description | | ||
| 153 | | --- | --- | | ||
| 154 | | boolean | Whether the loader is inserted successfully. It will fail if the loader is already inserted. | | ||
| 155 | |||
| 156 | ### remove_loader | ||
| 157 | |||
| 158 | **Type:** Function. | ||
| 159 | |||
| 160 | **Description:** | ||
| 161 | |||
| 162 | Remove the YueScript loader from the package loaders (searchers). | ||
| 163 | |||
| 164 | **Signature:** | ||
| 165 | ```lua | ||
| 166 | remove_loader: function(): boolean | ||
| 167 | ``` | ||
| 168 | |||
| 169 | **Returns:** | ||
| 170 | |||
| 171 | | Return Type | Description | | ||
| 172 | | --- | --- | | ||
| 173 | | boolean | Whether the loader is removed successfully. It will fail if the loader is not inserted. | | ||
| 174 | |||
| 175 | ### loadstring | ||
| 176 | |||
| 177 | **Type:** Function. | ||
| 178 | |||
| 179 | **Description:** | ||
| 180 | |||
| 181 | Loads YueScript code from a string into a function. | ||
| 182 | |||
| 183 | **Signature:** | ||
| 184 | ```lua | ||
| 185 | loadstring: function(input: string, chunkname: string, env: table, config?: Config): | ||
| 186 | --[[loaded function]] nil | function(...: any): (any...), | ||
| 187 | --[[error]] string | nil | ||
| 188 | ``` | ||
| 189 | |||
| 190 | **Parameters:** | ||
| 191 | |||
| 192 | | Parameter | Type | Description | | ||
| 193 | | --- | --- | --- | | ||
| 194 | | input | string | The YueScript code. | | ||
| 195 | | chunkname | string | The name of the code chunk. | | ||
| 196 | | env | table | The environment table. | | ||
| 197 | | config | Config | [Optional] The compiler options. | | ||
| 198 | |||
| 199 | **Returns:** | ||
| 200 | |||
| 201 | | Return Type | Description | | ||
| 202 | | --- | --- | | ||
| 203 | | function \| nil | The loaded function, or nil if the loading failed. | | ||
| 204 | | string \| nil | The error message, or nil if the loading succeeded. | | ||
| 205 | |||
| 206 | ### loadstring | ||
| 207 | |||
| 208 | **Type:** Function. | ||
| 209 | |||
| 210 | **Description:** | ||
| 211 | |||
| 212 | Loads YueScript code from a string into a function. | ||
| 213 | |||
| 214 | **Signature:** | ||
| 215 | ```lua | ||
| 216 | loadstring: function(input: string, chunkname: string, config?: Config): | ||
| 217 | --[[loaded function]] nil | function(...: any): (any...), | ||
| 218 | --[[error]] string | nil | ||
| 219 | ``` | ||
| 220 | |||
| 221 | **Parameters:** | ||
| 222 | |||
| 223 | | Parameter | Type | Description | | ||
| 224 | | --- | --- | --- | | ||
| 225 | | input | string | The YueScript code. | | ||
| 226 | | chunkname | string | The name of the code chunk. | | ||
| 227 | | config | Config | [Optional] The compiler options. | | ||
| 228 | |||
| 229 | **Returns:** | ||
| 230 | |||
| 231 | | Return Type | Description | | ||
| 232 | | --- | --- | | ||
| 233 | | function \| nil | The loaded function, or nil if the loading failed. | | ||
| 234 | | string \| nil | The error message, or nil if the loading succeeded. | | ||
| 235 | |||
| 236 | ### loadstring | ||
| 237 | |||
| 238 | **Type:** Function. | ||
| 239 | |||
| 240 | **Description:** | ||
| 241 | |||
| 242 | Loads YueScript code from a string into a function. | ||
| 243 | |||
| 244 | **Signature:** | ||
| 245 | ```lua | ||
| 246 | loadstring: function(input: string, config?: Config): | ||
| 247 | --[[loaded function]] nil | function(...: any): (any...), | ||
| 248 | --[[error]] string | nil | ||
| 249 | ``` | ||
| 250 | |||
| 251 | **Parameters:** | ||
| 252 | |||
| 253 | | Parameter | Type | Description | | ||
| 254 | | --- | --- | --- | | ||
| 255 | | input | string | The YueScript code. | | ||
| 256 | | config | Config | [Optional] The compiler options. | | ||
| 257 | |||
| 258 | **Returns:** | ||
| 259 | |||
| 260 | | Return Type | Description | | ||
| 261 | | --- | --- | | ||
| 262 | | function \| nil | The loaded function, or nil if the loading failed. | | ||
| 263 | | string \| nil | The error message, or nil if the loading succeeded. | | ||
| 264 | |||
| 265 | ### loadfile | ||
| 266 | |||
| 267 | **Type:** Function. | ||
| 268 | |||
| 269 | **Description:** | ||
| 270 | |||
| 271 | Loads YueScript code from a file into a function. | ||
| 272 | |||
| 273 | **Signature:** | ||
| 274 | ```lua | ||
| 275 | loadfile: function(filename: string, env: table, config?: Config): | ||
| 276 | nil | function(...: any): (any...), | ||
| 277 | string | nil | ||
| 278 | ``` | ||
| 279 | |||
| 280 | **Parameters:** | ||
| 281 | |||
| 282 | | Parameter | Type | Description | | ||
| 283 | | --- | --- | --- | | ||
| 284 | | filename | string | The file name. | | ||
| 285 | | env | table | The environment table. | | ||
| 286 | | config | Config | [Optional] The compiler options. | | ||
| 287 | |||
| 288 | **Returns:** | ||
| 289 | |||
| 290 | | Return Type | Description | | ||
| 291 | | --- | --- | | ||
| 292 | | function \| nil | The loaded function, or nil if the loading failed. | | ||
| 293 | | string \| nil | The error message, or nil if the loading succeeded. | | ||
| 294 | |||
| 295 | ### loadfile | ||
| 296 | |||
| 297 | **Type:** Function. | ||
| 298 | |||
| 299 | **Description:** | ||
| 300 | |||
| 301 | Loads YueScript code from a file into a function. | ||
| 302 | |||
| 303 | **Signature:** | ||
| 304 | ```lua | ||
| 305 | loadfile: function(filename: string, config?: Config): | ||
| 306 | nil | function(...: any): (any...), | ||
| 307 | string | nil | ||
| 308 | ``` | ||
| 309 | |||
| 310 | **Parameters:** | ||
| 311 | |||
| 312 | | Parameter | Type | Description | | ||
| 313 | | --- | --- | --- | | ||
| 314 | | filename | string | The file name. | | ||
| 315 | | config | Config | [Optional] The compiler options. | | ||
| 316 | |||
| 317 | **Returns:** | ||
| 318 | |||
| 319 | | Return Type | Description | | ||
| 320 | | --- | --- | | ||
| 321 | | function \| nil | The loaded function, or nil if the loading failed. | | ||
| 322 | | string \| nil | The error message, or nil if the loading succeeded. | | ||
| 323 | |||
| 324 | ### dofile | ||
| 325 | |||
| 326 | **Type:** Function. | ||
| 327 | |||
| 328 | **Description:** | ||
| 329 | |||
| 330 | Loads YueScript code from a file into a function and executes it. | ||
| 331 | |||
| 332 | **Signature:** | ||
| 333 | ```lua | ||
| 334 | dofile: function(filename: string, env: table, config?: Config): any... | ||
| 335 | ``` | ||
| 336 | |||
| 337 | **Parameters:** | ||
| 338 | |||
| 339 | | Parameter | Type | Description | | ||
| 340 | | --- | --- | --- | | ||
| 341 | | filename | string | The file name. | | ||
| 342 | | env | table | The environment table. | | ||
| 343 | | config | Config | [Optional] The compiler options. | | ||
| 344 | |||
| 345 | **Returns:** | ||
| 346 | |||
| 347 | | Return Type | Description | | ||
| 348 | | --- | --- | | ||
| 349 | | any... | The return values of the loaded function. | | ||
| 350 | |||
| 351 | ### dofile | ||
| 352 | |||
| 353 | **Type:** Function. | ||
| 354 | |||
| 355 | **Description:** | ||
| 356 | |||
| 357 | Loads YueScript code from a file into a function and executes it. | ||
| 358 | |||
| 359 | **Signature:** | ||
| 360 | ```lua | ||
| 361 | dofile: function(filename: string, config?: Config): any... | ||
| 362 | ``` | ||
| 363 | |||
| 364 | **Parameters:** | ||
| 365 | |||
| 366 | | Parameter | Type | Description | | ||
| 367 | | --- | --- | --- | | ||
| 368 | | filename | string | The file name. | | ||
| 369 | | config | Config | [Optional] The compiler options. | | ||
| 370 | |||
| 371 | **Returns:** | ||
| 372 | |||
| 373 | | Return Type | Description | | ||
| 374 | | --- | --- | | ||
| 375 | | any... | The return values of the loaded function. | | ||
| 376 | |||
| 377 | ### find_modulepath | ||
| 378 | |||
| 379 | **Type:** Function. | ||
| 380 | |||
| 381 | **Description:** | ||
| 382 | |||
| 383 | Resolves the YueScript module name to the file path. | ||
| 384 | |||
| 385 | **Signature:** | ||
| 386 | ```lua | ||
| 387 | find_modulepath: function(name: string): string | ||
| 388 | ``` | ||
| 389 | |||
| 390 | **Parameters:** | ||
| 391 | |||
| 392 | | Parameter | Type | Description | | ||
| 393 | | --- | --- | --- | | ||
| 394 | | name | string | The module name. | | ||
| 395 | |||
| 396 | **Returns:** | ||
| 397 | |||
| 398 | | Return Type | Description | | ||
| 399 | | --- | --- | | ||
| 400 | | string | The file path. | | ||
| 401 | |||
| 402 | ### pcall | ||
| 403 | |||
| 404 | **Type:** Function. | ||
| 405 | |||
| 406 | **Description:** | ||
| 407 | |||
| 408 | Calls a function in protected mode. | ||
| 409 | Catches any errors and returns a status code and results or error object. | ||
| 410 | Rewrites the error line number to the original line number in the YueScript code when errors occur. | ||
| 411 | |||
| 412 | **Signature:** | ||
| 413 | ```lua | ||
| 414 | pcall: function(f: function, ...: any): boolean, any... | ||
| 415 | ``` | ||
| 416 | |||
| 417 | **Parameters:** | ||
| 418 | |||
| 419 | | Parameter | Type | Description | | ||
| 420 | | --- | --- | --- | | ||
| 421 | | f | function | The function to call. | | ||
| 422 | | ... | any | Arguments to pass to the function. | | ||
| 423 | |||
| 424 | **Returns:** | ||
| 425 | |||
| 426 | | Return Type | Description | | ||
| 427 | | --- | --- | | ||
| 428 | | boolean, ... | Status code and function results or error object. | | ||
| 429 | |||
| 430 | ### require | ||
| 431 | |||
| 432 | **Type:** Function. | ||
| 433 | |||
| 434 | **Description:** | ||
| 435 | |||
| 436 | Loads a given module. Can be either a Lua module or a YueScript module. | ||
| 437 | Rewrites the error line number to the original line number in the YueScript code if the module is a YueScript module and loading fails. | ||
| 438 | |||
| 439 | **Signature:** | ||
| 440 | ```lua | ||
| 441 | require: function(name: string): any... | ||
| 442 | ``` | ||
| 443 | |||
| 444 | **Parameters:** | ||
| 445 | |||
| 446 | | Parameter | Type | Description | | ||
| 447 | | --- | --- | --- | | ||
| 448 | | modname | string | The name of the module to load. | | ||
| 449 | |||
| 450 | **Returns:** | ||
| 451 | |||
| 452 | | Return Type | Description | | ||
| 453 | | --- | --- | | ||
| 454 | | any | The value stored at package.loaded[modname] if the module is already loaded.Otherwise, tries to find a loader and returns the final value of package.loaded[modname] and a loader data as a second result. | | ||
| 455 | |||
| 456 | ### p | ||
| 457 | |||
| 458 | **Type:** Function. | ||
| 459 | |||
| 460 | **Description:** | ||
| 461 | |||
| 462 | Inspects the structures of the passed values and prints string representations. | ||
| 463 | |||
| 464 | **Signature:** | ||
| 465 | ```lua | ||
| 466 | p: function(...: any) | ||
| 467 | ``` | ||
| 468 | |||
| 469 | **Parameters:** | ||
| 470 | |||
| 471 | | Parameter | Type | Description | | ||
| 472 | | --- | --- | --- | | ||
| 473 | | ... | any | The values to inspect. | | ||
| 474 | |||
| 475 | ### options | ||
| 476 | |||
| 477 | **Type:** Field. | ||
| 478 | |||
| 479 | **Description:** | ||
| 480 | |||
| 481 | The current compiler options. | ||
| 482 | |||
| 483 | **Signature:** | ||
| 484 | ```lua | ||
| 485 | options: Config.Options | ||
| 486 | ``` | ||
| 487 | |||
| 488 | ### traceback | ||
| 489 | |||
| 490 | **Type:** Function. | ||
| 491 | |||
| 492 | **Description:** | ||
| 493 | |||
| 494 | The traceback function that rewrites the stack trace line numbers to the original line numbers in the YueScript code. | ||
| 495 | |||
| 496 | **Signature:** | ||
| 497 | ```lua | ||
| 498 | traceback: function(message: string): string | ||
| 499 | ``` | ||
| 500 | |||
| 501 | **Parameters:** | ||
| 502 | |||
| 503 | | Parameter | Type | Description | | ||
| 504 | | --- | --- | --- | | ||
| 505 | | message | string | The traceback message. | | ||
| 506 | |||
| 507 | **Returns:** | ||
| 508 | |||
| 509 | | Return Type | Description | | ||
| 510 | | --- | --- | | ||
| 511 | | string | The rewritten traceback message. | | ||
| 512 | |||
| 513 | ### is_ast | ||
| 514 | |||
| 515 | **Type:** Function. | ||
| 516 | |||
| 517 | **Description:** | ||
| 518 | |||
| 519 | Checks whether the code matches the specified AST. | ||
| 520 | |||
| 521 | **Signature:** | ||
| 522 | ```lua | ||
| 523 | is_ast: function(astName: string, code: string): boolean | ||
| 524 | ``` | ||
| 525 | |||
| 526 | **Parameters:** | ||
| 527 | |||
| 528 | | Parameter | Type | Description | | ||
| 529 | | --- | --- | --- | | ||
| 530 | | astName | string | The AST name. | | ||
| 531 | | code | string | The code. | | ||
| 532 | |||
| 533 | **Returns:** | ||
| 534 | |||
| 535 | | Return Type | Description | | ||
| 536 | | --- | --- | | ||
| 537 | | boolean | Whether the code matches the AST. | | ||
| 538 | |||
| 539 | ### AST | ||
| 540 | |||
| 541 | **Type:** Field. | ||
| 542 | |||
| 543 | **Description:** | ||
| 544 | |||
| 545 | The AST type definition with name, row, column and sub nodes. | ||
| 546 | |||
| 547 | **Signature:** | ||
| 548 | ```lua | ||
| 549 | type AST = {string, integer, integer, any} | ||
| 550 | ``` | ||
| 551 | |||
| 552 | ### to_ast | ||
| 553 | |||
| 554 | **Type:** Function. | ||
| 555 | |||
| 556 | **Description:** | ||
| 557 | |||
| 558 | Converts the code to the AST. | ||
| 559 | |||
| 560 | **Signature:** | ||
| 561 | ```lua | ||
| 562 | to_ast: function(code: string, flattenLevel?: number, astName?: string, reserveComment?: boolean): | ||
| 563 | --[[AST]] AST | nil, | ||
| 564 | --[[error]] nil | string | ||
| 565 | ``` | ||
| 566 | |||
| 567 | **Parameters:** | ||
| 568 | |||
| 569 | | Parameter | Type | Description | | ||
| 570 | | --- | --- | --- | | ||
| 571 | | code | string | The code. | | ||
| 572 | | flattenLevel | integer | [Optional] The flatten level. Higher level means more flattening. Default is 0. Maximum is 2. | | ||
| 573 | | astName | string | [Optional] The AST name. Default is "File". | | ||
| 574 | | reserveComment | boolean | [Optional] Whether to reserve the original comments. Default is false. | | ||
| 575 | |||
| 576 | **Returns:** | ||
| 577 | |||
| 578 | | Return Type | Description | | ||
| 579 | | --- | --- | | ||
| 580 | | AST \| nil | The AST, or nil if the conversion failed. | | ||
| 581 | | string \| nil | The error message, or nil if the conversion succeeded. | | ||
| 582 | |||
| 583 | ### format | ||
| 584 | |||
| 585 | **Type:** Function. | ||
| 586 | |||
| 587 | **Description:** | ||
| 588 | |||
| 589 | Formats the YueScript code. | ||
| 590 | |||
| 591 | **Signature:** | ||
| 592 | ```lua | ||
| 593 | format: function(code: string, tabSize?: number, reserveComment?: boolean): string | ||
| 594 | ``` | ||
| 595 | |||
| 596 | **Parameters:** | ||
| 597 | |||
| 598 | | Parameter | Type | Description | | ||
| 599 | | --- | --- | --- | | ||
| 600 | | code | string | The code. | | ||
| 601 | | tabSize | integer | [Optional] The tab size. Default is 4. | | ||
| 602 | | reserveComment | boolean | [Optional] Whether to reserve the original comments. Default is true. | | ||
| 603 | |||
| 604 | **Returns:** | ||
| 605 | |||
| 606 | | Return Type | Description | | ||
| 607 | | --- | --- | | ||
| 608 | | string | The formatted code. | | ||
| 609 | |||
| 610 | ### __call | ||
| 611 | |||
| 612 | **Type:** Metamethod. | ||
| 613 | |||
| 614 | **Description:** | ||
| 615 | |||
| 616 | Requires the YueScript module. | ||
| 617 | Rewrites the error line number to the original line number in the YueScript code when loading fails. | ||
| 618 | |||
| 619 | **Signature:** | ||
| 620 | ```lua | ||
| 621 | metamethod __call: function(self: yue, module: string): any... | ||
| 622 | ``` | ||
| 623 | |||
| 624 | **Parameters:** | ||
| 625 | |||
| 626 | | Parameter | Type | Description | | ||
| 627 | | --- | --- | --- | | ||
| 628 | | module | string | The module name. | | ||
| 629 | |||
| 630 | **Returns:** | ||
| 631 | |||
| 632 | | Return Type | Description | | ||
| 633 | | --- | --- | | ||
| 634 | | any | The module value. | | ||
| 635 | |||
| 636 | ## Config | ||
| 637 | |||
| 638 | **Description:** | ||
| 639 | |||
| 640 | The compiler compile options. | ||
| 641 | |||
| 642 | ### lint_global | ||
| 643 | |||
| 644 | **Type:** Field. | ||
| 645 | |||
| 646 | **Description:** | ||
| 647 | |||
| 648 | Whether the compiler should collect the global variables appearing in the code. | ||
| 649 | |||
| 650 | **Signature:** | ||
| 651 | ```lua | ||
| 652 | lint_global: boolean | ||
| 653 | ``` | ||
| 654 | |||
| 655 | ### implicit_return_root | ||
| 656 | |||
| 657 | **Type:** Field. | ||
| 658 | |||
| 659 | **Description:** | ||
| 660 | |||
| 661 | Whether the compiler should do an implicit return for the root code block. | ||
| 662 | |||
| 663 | **Signature:** | ||
| 664 | ```lua | ||
| 665 | implicit_return_root: boolean | ||
| 666 | ``` | ||
| 667 | |||
| 668 | ### reserve_line_number | ||
| 669 | |||
| 670 | **Type:** Field. | ||
| 671 | |||
| 672 | **Description:** | ||
| 673 | |||
| 674 | Whether the compiler should reserve the original line number in the compiled code. | ||
| 675 | |||
| 676 | **Signature:** | ||
| 677 | ```lua | ||
| 678 | reserve_line_number: boolean | ||
| 679 | ``` | ||
| 680 | |||
| 681 | ### reserve_comment | ||
| 682 | |||
| 683 | **Type:** Field. | ||
| 684 | |||
| 685 | **Description:** | ||
| 686 | |||
| 687 | Whether the compiler should reserve the original comments in the compiled code. | ||
| 688 | |||
| 689 | **Signature:** | ||
| 690 | ```lua | ||
| 691 | reserve_comment: boolean | ||
| 692 | ``` | ||
| 693 | |||
| 694 | ### space_over_tab | ||
| 695 | |||
| 696 | **Type:** Field. | ||
| 697 | |||
| 698 | **Description:** | ||
| 699 | |||
| 700 | Whether the compiler should use the space character instead of the tab character in the compiled code. | ||
| 701 | |||
| 702 | **Signature:** | ||
| 703 | ```lua | ||
| 704 | space_over_tab: boolean | ||
| 705 | ``` | ||
| 706 | |||
| 707 | ### same_module | ||
| 708 | |||
| 709 | **Type:** Field. | ||
| 710 | |||
| 711 | **Description:** | ||
| 712 | |||
| 713 | Whether the compiler should treat the code to be compiled as the same currently being compiled module. For internal use only. | ||
| 714 | |||
| 715 | **Signature:** | ||
| 716 | ```lua | ||
| 717 | same_module: boolean | ||
| 718 | ``` | ||
| 719 | |||
| 720 | ### line_offset | ||
| 721 | |||
| 722 | **Type:** Field. | ||
| 723 | |||
| 724 | **Description:** | ||
| 725 | |||
| 726 | Whether the compiler error message should include the line number offset. For internal use only. | ||
| 727 | |||
| 728 | **Signature:** | ||
| 729 | ```lua | ||
| 730 | line_offset: integer | ||
| 731 | ``` | ||
| 732 | |||
| 733 | ### yue.Config.LuaTarget | ||
| 734 | |||
| 735 | **Type:** Enumeration. | ||
| 736 | |||
| 737 | **Description:** | ||
| 738 | |||
| 739 | The target Lua version enumeration. | ||
| 740 | |||
| 741 | **Signature:** | ||
| 742 | ```lua | ||
| 743 | enum LuaTarget | ||
| 744 | "5.1" | ||
| 745 | "5.2" | ||
| 746 | "5.3" | ||
| 747 | "5.4" | ||
| 748 | "5.5" | ||
| 749 | end | ||
| 750 | ``` | ||
| 751 | |||
| 752 | ### options | ||
| 753 | |||
| 754 | **Type:** Field. | ||
| 755 | |||
| 756 | **Description:** | ||
| 757 | |||
| 758 | The extra options to be passed to the compilation function. | ||
| 759 | |||
| 760 | **Signature:** | ||
| 761 | ```lua | ||
| 762 | options: Options | ||
| 763 | ``` | ||
| 764 | |||
| 765 | ## Options | ||
| 766 | |||
| 767 | **Description:** | ||
| 768 | |||
| 769 | The extra compiler options definition. | ||
| 770 | |||
| 771 | ### target | ||
| 772 | |||
| 773 | **Type:** Field. | ||
| 774 | |||
| 775 | **Description:** | ||
| 776 | |||
| 777 | The target Lua version for the compilation. | ||
| 778 | |||
| 779 | **Signature:** | ||
| 780 | ```lua | ||
| 781 | target: LuaTarget | ||
| 782 | ``` | ||
| 783 | |||
| 784 | ### path | ||
| 785 | |||
| 786 | **Type:** Field. | ||
| 787 | |||
| 788 | **Description:** | ||
| 789 | |||
| 790 | The extra module search path. | ||
| 791 | |||
| 792 | **Signature:** | ||
| 793 | ```lua | ||
| 794 | path: string | ||
| 795 | ``` | ||
| 796 | |||
| 797 | ### dump_locals | ||
| 798 | |||
| 799 | **Type:** Field. | ||
| 800 | |||
| 801 | **Description:** | ||
| 802 | |||
| 803 | Whether to dump the local variables in the traceback error message. Default is false. | ||
| 804 | |||
| 805 | **Signature:** | ||
| 806 | ```lua | ||
| 807 | dump_locals: boolean | ||
| 808 | ``` | ||
| 809 | |||
| 810 | ### simplified | ||
| 811 | |||
| 812 | **Type:** Field. | ||
| 813 | |||
| 814 | **Description:** | ||
| 815 | |||
| 816 | Whether to simplify the error message. Default is true. | ||
| 817 | |||
| 818 | **Signature:** | ||
| 819 | ```lua | ||
| 820 | simplified: boolean | ||
| 821 | ``` | ||
diff --git a/doc/docs/de/doc/try.md b/doc/docs/de/doc/try.md new file mode 100644 index 0000000..23c7877 --- /dev/null +++ b/doc/docs/de/doc/try.md | |||
| @@ -0,0 +1,105 @@ | |||
| 1 | # Try | ||
| 2 | |||
| 3 | The syntax for Lua error handling in a common form. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | try | ||
| 7 | func 1, 2, 3 | ||
| 8 | catch err | ||
| 9 | print yue.traceback err | ||
| 10 | |||
| 11 | success, result = try | ||
| 12 | func 1, 2, 3 | ||
| 13 | catch err | ||
| 14 | yue.traceback err | ||
| 15 | |||
| 16 | try func 1, 2, 3 | ||
| 17 | catch err | ||
| 18 | print yue.traceback err | ||
| 19 | |||
| 20 | success, result = try func 1, 2, 3 | ||
| 21 | |||
| 22 | try | ||
| 23 | print "trying" | ||
| 24 | func 1, 2, 3 | ||
| 25 | |||
| 26 | -- working with if assignment pattern | ||
| 27 | if success, result := try func 1, 2, 3 | ||
| 28 | catch err | ||
| 29 | print yue.traceback err | ||
| 30 | print result | ||
| 31 | ``` | ||
| 32 | <YueDisplay> | ||
| 33 | |||
| 34 | ```yue | ||
| 35 | try | ||
| 36 | func 1, 2, 3 | ||
| 37 | catch err | ||
| 38 | print yue.traceback err | ||
| 39 | |||
| 40 | success, result = try | ||
| 41 | func 1, 2, 3 | ||
| 42 | catch err | ||
| 43 | yue.traceback err | ||
| 44 | |||
| 45 | try func 1, 2, 3 | ||
| 46 | catch err | ||
| 47 | print yue.traceback err | ||
| 48 | |||
| 49 | success, result = try func 1, 2, 3 | ||
| 50 | |||
| 51 | try | ||
| 52 | print "trying" | ||
| 53 | func 1, 2, 3 | ||
| 54 | |||
| 55 | -- working with if assignment pattern | ||
| 56 | if success, result := try func 1, 2, 3 | ||
| 57 | catch err | ||
| 58 | print yue.traceback err | ||
| 59 | print result | ||
| 60 | ``` | ||
| 61 | |||
| 62 | </YueDisplay> | ||
| 63 | |||
| 64 | ## Try? | ||
| 65 | |||
| 66 | `try?` is a simplified use for error handling syntax that omit the boolean status from the `try` statement, and it will return the result from the try block when success, return nil instead of error object otherwise. | ||
| 67 | |||
| 68 | ```yuescript | ||
| 69 | a, b, c = try? func! | ||
| 70 | |||
| 71 | -- with nil coalescing operator | ||
| 72 | a = (try? func!) ?? "default" | ||
| 73 | |||
| 74 | -- as function argument | ||
| 75 | f try? func! | ||
| 76 | |||
| 77 | -- with catch block | ||
| 78 | f try? | ||
| 79 | print 123 | ||
| 80 | func! | ||
| 81 | catch e | ||
| 82 | print e | ||
| 83 | e | ||
| 84 | ``` | ||
| 85 | <YueDisplay> | ||
| 86 | |||
| 87 | ```yue | ||
| 88 | a, b, c = try? func! | ||
| 89 | |||
| 90 | -- with nil coalescing operator | ||
| 91 | a = (try? func!) ?? "default" | ||
| 92 | |||
| 93 | -- as function argument | ||
| 94 | f try? func! | ||
| 95 | |||
| 96 | -- with catch block | ||
| 97 | f try? | ||
| 98 | print 123 | ||
| 99 | func! | ||
| 100 | catch e | ||
| 101 | print e | ||
| 102 | e | ||
| 103 | ``` | ||
| 104 | |||
| 105 | </YueDisplay> | ||
diff --git a/doc/docs/de/doc/usage.md b/doc/docs/de/doc/usage.md new file mode 100644 index 0000000..45161c6 --- /dev/null +++ b/doc/docs/de/doc/usage.md | |||
| @@ -0,0 +1,111 @@ | |||
| 1 | # Usage | ||
| 2 | |||
| 3 | ## Lua Module | ||
| 4 | |||
| 5 | Use YueScript module in Lua: | ||
| 6 | |||
| 7 | * **Case 1** | ||
| 8 | |||
| 9 | Require "your_yuescript_entry.yue" in Lua. | ||
| 10 | ```Lua | ||
| 11 | require("yue")("your_yuescript_entry") | ||
| 12 | ``` | ||
| 13 | And this code still works when you compile "your_yuescript_entry.yue" to "your_yuescript_entry.lua" in the same path. In the rest YueScript files just use the normal **require** or **import**. The code line numbers in error messages will also be handled correctly. | ||
| 14 | |||
| 15 | * **Case 2** | ||
| 16 | |||
| 17 | Require YueScript module and rewite message by hand. | ||
| 18 | |||
| 19 | ```lua | ||
| 20 | local yue = require("yue") | ||
| 21 | yue.insert_loader() | ||
| 22 | local success, result = xpcall(function() | ||
| 23 | return require("yuescript_module_name") | ||
| 24 | end, function(err) | ||
| 25 | return yue.traceback(err) | ||
| 26 | end) | ||
| 27 | ``` | ||
| 28 | |||
| 29 | * **Case 3** | ||
| 30 | |||
| 31 | Use the YueScript compiler function in Lua. | ||
| 32 | |||
| 33 | ```lua | ||
| 34 | local yue = require("yue") | ||
| 35 | local codes, err, globals = yue.to_lua([[ | ||
| 36 | f = -> | ||
| 37 | print "hello world" | ||
| 38 | f! | ||
| 39 | ]],{ | ||
| 40 | implicit_return_root = true, | ||
| 41 | reserve_line_number = true, | ||
| 42 | lint_global = true, | ||
| 43 | space_over_tab = false, | ||
| 44 | options = { | ||
| 45 | target = "5.4", | ||
| 46 | path = "/script" | ||
| 47 | } | ||
| 48 | }) | ||
| 49 | ``` | ||
| 50 | |||
| 51 | ## YueScript Tool | ||
| 52 | |||
| 53 | Use YueScript tool with: | ||
| 54 | |||
| 55 | ```shell | ||
| 56 | > yue -h | ||
| 57 | Usage: yue | ||
| 58 | [options] [<file/directory>] ... | ||
| 59 | yue -e <code_or_file> [args...] | ||
| 60 | yue -w [<directory>] [options] | ||
| 61 | yue - | ||
| 62 | |||
| 63 | Notes: | ||
| 64 | - '-' / '--' must be the first and only argument. | ||
| 65 | - '-o/--output' can not be used with multiple input files. | ||
| 66 | - '-w/--watch' can not be used with file input (directory only). | ||
| 67 | - with '-e/--execute', remaining tokens are treated as script args. | ||
| 68 | |||
| 69 | Options: | ||
| 70 | -h, --help Show this help message and exit. | ||
| 71 | -e <str>, --execute <str> Execute a file or raw codes | ||
| 72 | -m, --minify Generate minified codes | ||
| 73 | -r, --rewrite Rewrite output to match original line numbers | ||
| 74 | -t <output_to>, --output-to <output_to> | ||
| 75 | Specify where to place compiled files | ||
| 76 | -o <file>, --output <file> Write output to file | ||
| 77 | -p, --print Write output to standard out | ||
| 78 | -b, --benchmark Dump compile time (doesn't write output) | ||
| 79 | -g, --globals Dump global variables used in NAME LINE COLUMN | ||
| 80 | -s, --spaces Use spaces in generated codes instead of tabs | ||
| 81 | -l, --line-numbers Write line numbers from source codes | ||
| 82 | -j, --no-implicit-return Disable implicit return at end of file | ||
| 83 | -c, --reserve-comments Reserve comments before statement from source codes | ||
| 84 | -w [<dir>], --watch [<dir>] | ||
| 85 | Watch changes and compile every file under directory | ||
| 86 | -v, --version Print version | ||
| 87 | - Read from standard in, print to standard out | ||
| 88 | (Must be first and only argument) | ||
| 89 | -- Same as '-' (kept for backward compatibility) | ||
| 90 | |||
| 91 | --target <version> Specify the Lua version that codes will be generated to | ||
| 92 | (version can only be 5.1 to 5.5) | ||
| 93 | --path <path_str> Append an extra Lua search path string to package.path | ||
| 94 | --<key>=<value> Pass compiler option in key=value form (existing behavior) | ||
| 95 | |||
| 96 | Execute without options to enter REPL, type symbol '$' | ||
| 97 | in a single line to start/stop multi-line mode | ||
| 98 | ``` | ||
| 99 | Use cases: | ||
| 100 | |||
| 101 | Recursively compile every YueScript file with extension **.yue** under current path: **yue .** | ||
| 102 | |||
| 103 | Compile and save results to a target path: **yue -t /target/path/ .** | ||
| 104 | |||
| 105 | Compile and reserve debug info: **yue -l .** | ||
| 106 | |||
| 107 | Compile and generate minified codes: **yue -m .** | ||
| 108 | |||
| 109 | Execute raw codes: **yue -e 'print 123'** | ||
| 110 | |||
| 111 | Execute a YueScript file: **yue -e main.yue** | ||
diff --git a/doc/docs/de/doc/varargs-assignment.md b/doc/docs/de/doc/varargs-assignment.md new file mode 100644 index 0000000..1d66680 --- /dev/null +++ b/doc/docs/de/doc/varargs-assignment.md | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | # Varargs Assignment | ||
| 2 | |||
| 3 | You can assign the results returned from a function to a varargs symbol `...`. And then access its content using the Lua way. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | list = [1, 2, 3, 4, 5] | ||
| 7 | fn = (ok) -> ok, table.unpack list | ||
| 8 | ok, ... = fn true | ||
| 9 | count = select '#', ... | ||
| 10 | first = select 1, ... | ||
| 11 | print ok, count, first | ||
| 12 | ``` | ||
| 13 | <YueDisplay> | ||
| 14 | |||
| 15 | ```yue | ||
| 16 | list = [1, 2, 3, 4, 5] | ||
| 17 | fn = (ok) -> ok, table.unpack list | ||
| 18 | ok, ... = fn true | ||
| 19 | count = select '#', ... | ||
| 20 | first = select 1, ... | ||
| 21 | print ok, count, first | ||
| 22 | ``` | ||
| 23 | |||
| 24 | </YueDisplay> | ||
diff --git a/doc/docs/de/doc/while-loop.md b/doc/docs/de/doc/while-loop.md new file mode 100644 index 0000000..502935e --- /dev/null +++ b/doc/docs/de/doc/while-loop.md | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | # While Loop | ||
| 2 | |||
| 3 | The while loop also comes in four variations: | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | i = 10 | ||
| 7 | while i > 0 | ||
| 8 | print i | ||
| 9 | i -= 1 | ||
| 10 | |||
| 11 | while running == true do my_function! | ||
| 12 | ``` | ||
| 13 | <YueDisplay> | ||
| 14 | |||
| 15 | ```yue | ||
| 16 | i = 10 | ||
| 17 | while i > 0 | ||
| 18 | print i | ||
| 19 | i -= 1 | ||
| 20 | |||
| 21 | while running == true do my_function! | ||
| 22 | ``` | ||
| 23 | |||
| 24 | </YueDisplay> | ||
| 25 | |||
| 26 | ```yuescript | ||
| 27 | i = 10 | ||
| 28 | until i == 0 | ||
| 29 | print i | ||
| 30 | i -= 1 | ||
| 31 | |||
| 32 | until running == false do my_function! | ||
| 33 | ``` | ||
| 34 | <YueDisplay> | ||
| 35 | |||
| 36 | ```yue | ||
| 37 | i = 10 | ||
| 38 | until i == 0 | ||
| 39 | print i | ||
| 40 | i -= 1 | ||
| 41 | until running == false do my_function! | ||
| 42 | ``` | ||
| 43 | |||
| 44 | </YueDisplay> | ||
| 45 | |||
| 46 | 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. | ||
| 47 | |||
| 48 | ## Repeat Loop | ||
| 49 | |||
| 50 | The repeat loop comes from Lua: | ||
| 51 | |||
| 52 | ```yuescript | ||
| 53 | i = 10 | ||
| 54 | repeat | ||
| 55 | print i | ||
| 56 | i -= 1 | ||
| 57 | until i == 0 | ||
| 58 | ``` | ||
| 59 | <YueDisplay> | ||
| 60 | |||
| 61 | ```yue | ||
| 62 | i = 10 | ||
| 63 | repeat | ||
| 64 | print i | ||
| 65 | i -= 1 | ||
| 66 | until i == 0 | ||
| 67 | ``` | ||
| 68 | |||
| 69 | </YueDisplay> | ||
diff --git a/doc/docs/de/doc/whitespace.md b/doc/docs/de/doc/whitespace.md new file mode 100644 index 0000000..d742a2b --- /dev/null +++ b/doc/docs/de/doc/whitespace.md | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | # Whitespace | ||
| 2 | |||
| 3 | YueScript is a whitespace significant language. You have to write some code block in the same indent with space **' '** or tab **'\t'** like function body, value list and some control blocks. And expressions containing different whitespaces might mean different things. Tab is treated like 4 space, but it's better not mix the use of spaces and tabs. | ||
| 4 | |||
| 5 | ## Statement Separator | ||
| 6 | |||
| 7 | A statement normally ends at a line break. You can also use a semicolon `;` to explicitly terminate a statement, which allows writing multiple statements on the same line: | ||
| 8 | |||
| 9 | ```yuescript | ||
| 10 | a = 1; b = 2; print a + b | ||
| 11 | ``` | ||
| 12 | <YueDisplay> | ||
| 13 | |||
| 14 | ```yue | ||
| 15 | a = 1; b = 2; print a + b | ||
| 16 | ``` | ||
| 17 | |||
| 18 | </YueDisplay> | ||
| 19 | |||
| 20 | ## Multiline Chaining | ||
| 21 | |||
| 22 | You can write multi-line chaining function calls with a same indent. | ||
| 23 | |||
| 24 | ```yuescript | ||
| 25 | Rx.Observable | ||
| 26 | .fromRange 1, 8 | ||
| 27 | \filter (x) -> x % 2 == 0 | ||
| 28 | \concat Rx.Observable.of 'who do we appreciate' | ||
| 29 | \map (value) -> value .. '!' | ||
| 30 | \subscribe print | ||
| 31 | ``` | ||
| 32 | <YueDisplay> | ||
| 33 | |||
| 34 | ```yue | ||
| 35 | Rx.Observable | ||
| 36 | .fromRange 1, 8 | ||
| 37 | \filter (x) -> x % 2 == 0 | ||
| 38 | \concat Rx.Observable.of 'who do we appreciate' | ||
| 39 | \map (value) -> value .. '!' | ||
| 40 | \subscribe print | ||
| 41 | ``` | ||
| 42 | |||
| 43 | </YueDisplay> | ||
diff --git a/doc/docs/de/doc/with-statement.md b/doc/docs/de/doc/with-statement.md new file mode 100644 index 0000000..7786803 --- /dev/null +++ b/doc/docs/de/doc/with-statement.md | |||
| @@ -0,0 +1,126 @@ | |||
| 1 | # With Statement | ||
| 2 | |||
| 3 | |||
| 4 | A common pattern involving the creation of an object is calling a series of functions and setting a series of properties immediately after creating it. | ||
| 5 | |||
| 6 | This results in repeating the name of the object multiple times in code, adding unnecessary noise. A common solution to this is to pass a table in as an argument which contains a collection of keys and values to overwrite. The downside to this is that the constructor of this object must support this form. | ||
| 7 | |||
| 8 | The with block helps to alleviate this. Within a with block we can use a special statements that begin with either . or \ which represent those operations applied to the object we are using with on. | ||
| 9 | |||
| 10 | For example, we work with a newly created object: | ||
| 11 | |||
| 12 | ```yuescript | ||
| 13 | with Person! | ||
| 14 | .name = "Oswald" | ||
| 15 | \add_relative my_dad | ||
| 16 | \save! | ||
| 17 | print .name | ||
| 18 | ``` | ||
| 19 | <YueDisplay> | ||
| 20 | |||
| 21 | ```yue | ||
| 22 | with Person! | ||
| 23 | .name = "Oswald" | ||
| 24 | \add_relative my_dad | ||
| 25 | \save! | ||
| 26 | print .name | ||
| 27 | ``` | ||
| 28 | |||
| 29 | </YueDisplay> | ||
| 30 | |||
| 31 | The with statement can also be used as an expression which returns the value it has been giving access to. | ||
| 32 | |||
| 33 | ```yuescript | ||
| 34 | file = with File "favorite_foods.txt" | ||
| 35 | \set_encoding "utf8" | ||
| 36 | ``` | ||
| 37 | <YueDisplay> | ||
| 38 | |||
| 39 | ```yue | ||
| 40 | file = with File "favorite_foods.txt" | ||
| 41 | \set_encoding "utf8" | ||
| 42 | ``` | ||
| 43 | |||
| 44 | </YueDisplay> | ||
| 45 | |||
| 46 | Or… | ||
| 47 | |||
| 48 | ```yuescript | ||
| 49 | create_person = (name, relatives) -> | ||
| 50 | with Person! | ||
| 51 | .name = name | ||
| 52 | \add_relative relative for relative in *relatives | ||
| 53 | |||
| 54 | me = create_person "Leaf", [dad, mother, sister] | ||
| 55 | ``` | ||
| 56 | <YueDisplay> | ||
| 57 | |||
| 58 | ```yue | ||
| 59 | create_person = (name, relatives) -> | ||
| 60 | with Person! | ||
| 61 | .name = name | ||
| 62 | \add_relative relative for relative in *relatives | ||
| 63 | |||
| 64 | me = create_person "Leaf", [dad, mother, sister] | ||
| 65 | ``` | ||
| 66 | |||
| 67 | </YueDisplay> | ||
| 68 | |||
| 69 | In this usage, with can be seen as a special form of the K combinator. | ||
| 70 | |||
| 71 | The expression in the with statement can also be an assignment, if you want to give a name to the expression. | ||
| 72 | |||
| 73 | ```yuescript | ||
| 74 | with str := "Hello" | ||
| 75 | print "original:", str | ||
| 76 | print "upper:", \upper! | ||
| 77 | ``` | ||
| 78 | <YueDisplay> | ||
| 79 | |||
| 80 | ```yue | ||
| 81 | with str := "Hello" | ||
| 82 | print "original:", str | ||
| 83 | print "upper:", \upper! | ||
| 84 | ``` | ||
| 85 | |||
| 86 | </YueDisplay> | ||
| 87 | |||
| 88 | You can access special keys with `[]` in a `with` statement. | ||
| 89 | |||
| 90 | ```yuescript | ||
| 91 | with tb | ||
| 92 | [1] = 1 | ||
| 93 | print [2] | ||
| 94 | with [abc] | ||
| 95 | [3] = [2]\func! | ||
| 96 | ["key-name"] = value | ||
| 97 | [] = "abc" -- appending to "tb" | ||
| 98 | ``` | ||
| 99 | <YueDisplay> | ||
| 100 | |||
| 101 | ```yue | ||
| 102 | with tb | ||
| 103 | [1] = 1 | ||
| 104 | print [2] | ||
| 105 | with [abc] | ||
| 106 | [3] = [2]\func! | ||
| 107 | ["key-name"] = value | ||
| 108 | [] = "abc" -- appending to "tb" | ||
| 109 | ``` | ||
| 110 | |||
| 111 | </YueDisplay> | ||
| 112 | |||
| 113 | `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. | ||
| 114 | |||
| 115 | ```yuescript | ||
| 116 | with? obj | ||
| 117 | print obj.name | ||
| 118 | ``` | ||
| 119 | <YueDisplay> | ||
| 120 | |||
| 121 | ```yue | ||
| 122 | with? obj | ||
| 123 | print obj.name | ||
| 124 | ``` | ||
| 125 | |||
| 126 | </YueDisplay> | ||
diff --git a/doc/docs/de/index.md b/doc/docs/de/index.md new file mode 100644 index 0000000..1a923b2 --- /dev/null +++ b/doc/docs/de/index.md | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | --- | ||
| 2 | layout: home | ||
| 3 | hero: | ||
| 4 | name: YueScript | ||
| 5 | tagline: Eine Sprache, die zu Lua kompiliert | ||
| 6 | image: | ||
| 7 | src: /image/yuescript.svg | ||
| 8 | alt: YueScript | ||
| 9 | actions: | ||
| 10 | - theme: brand | ||
| 11 | text: Schnellstart → | ||
| 12 | link: /de/doc/ | ||
| 13 | features: | ||
| 14 | - title: Vertrauter Lua-Workflow | ||
| 15 | details: Schreibe kompakte Syntax, die in gut lesbares Lua mit vorhersehbarer Ausgabe kompiliert wird. | ||
| 16 | - title: Moderne Sprachfeatures | ||
| 17 | details: Pipe, Pattern Matching, Slicing und Destructuring – ohne auf Lua-Interop zu verzichten. | ||
| 18 | - title: Schnelle Iteration | ||
| 19 | details: Jedes Feedback ist willkommen, um die Entwicklung und Weiterentwicklung der Sprache zu beschleunigen! | ||
| 20 | footer: | ||
| 21 | message: MIT-Lizenz. | ||
| 22 | copyright: Copyright © 2017-2026 Li Jin. Alle Rechte vorbehalten. | ||
| 23 | --- | ||
| 24 | --- | ||
diff --git a/doc/docs/de/try/index.md b/doc/docs/de/try/index.md new file mode 100755 index 0000000..f8c3193 --- /dev/null +++ b/doc/docs/de/try/index.md | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | # YueScript Online-Compiler | ||
| 2 | --- | ||
| 3 | |||
| 4 | Probiere YueScript im Browser mit WASM aus. | ||
| 5 | |||
| 6 | <YueCompiler /> | ||
diff --git a/doc/docs/pt-br/doc/assignment.md b/doc/docs/pt-br/doc/assignment.md new file mode 100644 index 0000000..4dac6f4 --- /dev/null +++ b/doc/docs/pt-br/doc/assignment.md | |||
| @@ -0,0 +1,138 @@ | |||
| 1 | # Assignment | ||
| 2 | |||
| 3 | The variable is dynamic typed and is defined as local by default. But you can change the scope of declaration by **local** and **global** statement. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | hello = "world" | ||
| 7 | a, b, c = 1, 2, 3 | ||
| 8 | hello = 123 -- uses the existing variable | ||
| 9 | ``` | ||
| 10 | <YueDisplay> | ||
| 11 | |||
| 12 | ```yue | ||
| 13 | hello = "world" | ||
| 14 | a, b, c = 1, 2, 3 | ||
| 15 | hello = 123 -- uses the existing variable | ||
| 16 | ``` | ||
| 17 | |||
| 18 | </YueDisplay> | ||
| 19 | |||
| 20 | ## Perform Update | ||
| 21 | |||
| 22 | You can perform update assignment with many binary operators. | ||
| 23 | ```yuescript | ||
| 24 | x = 1 | ||
| 25 | x += 1 | ||
| 26 | x -= 1 | ||
| 27 | x *= 10 | ||
| 28 | x /= 10 | ||
| 29 | x %= 10 | ||
| 30 | s ..= "world" -- will add a new local if local variable is not exist | ||
| 31 | arg or= "default value" | ||
| 32 | ``` | ||
| 33 | <YueDisplay> | ||
| 34 | |||
| 35 | ```yue | ||
| 36 | x = 1 | ||
| 37 | x += 1 | ||
| 38 | x -= 1 | ||
| 39 | x *= 10 | ||
| 40 | x /= 10 | ||
| 41 | x %= 10 | ||
| 42 | s ..= "world" -- will add a new local if local variable is not exist | ||
| 43 | arg or= "default value" | ||
| 44 | ``` | ||
| 45 | |||
| 46 | </YueDisplay> | ||
| 47 | |||
| 48 | ## Chaining Assignment | ||
| 49 | |||
| 50 | You can do chaining assignment to assign multiple items to hold the same value. | ||
| 51 | ```yuescript | ||
| 52 | a = b = c = d = e = 0 | ||
| 53 | x = y = z = f! | ||
| 54 | ``` | ||
| 55 | <YueDisplay> | ||
| 56 | |||
| 57 | ```yue | ||
| 58 | a = b = c = d = e = 0 | ||
| 59 | x = y = z = f! | ||
| 60 | ``` | ||
| 61 | |||
| 62 | </YueDisplay> | ||
| 63 | |||
| 64 | ## Explicit Locals | ||
| 65 | ```yuescript | ||
| 66 | do | ||
| 67 | local a = 1 | ||
| 68 | local * | ||
| 69 | print "forward declare all variables as locals" | ||
| 70 | x = -> 1 + y + z | ||
| 71 | y, z = 2, 3 | ||
| 72 | global instance = Item\new! | ||
| 73 | |||
| 74 | do | ||
| 75 | local X = 1 | ||
| 76 | local ^ | ||
| 77 | print "only forward declare upper case variables" | ||
| 78 | a = 1 | ||
| 79 | B = 2 | ||
| 80 | ``` | ||
| 81 | <YueDisplay> | ||
| 82 | |||
| 83 | ```yue | ||
| 84 | do | ||
| 85 | local a = 1 | ||
| 86 | local * | ||
| 87 | print "forward declare all variables as locals" | ||
| 88 | x = -> 1 + y + z | ||
| 89 | y, z = 2, 3 | ||
| 90 | global instance = Item\new! | ||
| 91 | |||
| 92 | do | ||
| 93 | local X = 1 | ||
| 94 | local ^ | ||
| 95 | print "only forward declare upper case variables" | ||
| 96 | a = 1 | ||
| 97 | B = 2 | ||
| 98 | ``` | ||
| 99 | |||
| 100 | </YueDisplay> | ||
| 101 | |||
| 102 | ## Explicit Globals | ||
| 103 | ```yuescript | ||
| 104 | do | ||
| 105 | global a = 1 | ||
| 106 | global * | ||
| 107 | print "declare all variables as globals" | ||
| 108 | x = -> 1 + y + z | ||
| 109 | y, z = 2, 3 | ||
| 110 | |||
| 111 | do | ||
| 112 | global X = 1 | ||
| 113 | global ^ | ||
| 114 | print "only declare upper case variables as globals" | ||
| 115 | a = 1 | ||
| 116 | B = 2 | ||
| 117 | local Temp = "a local value" | ||
| 118 | ``` | ||
| 119 | <YueDisplay> | ||
| 120 | |||
| 121 | ```yue | ||
| 122 | do | ||
| 123 | global a = 1 | ||
| 124 | global * | ||
| 125 | print "declare all variables as globals" | ||
| 126 | x = -> 1 + y + z | ||
| 127 | y, z = 2, 3 | ||
| 128 | |||
| 129 | do | ||
| 130 | global X = 1 | ||
| 131 | global ^ | ||
| 132 | print "only declare upper case variables as globals" | ||
| 133 | a = 1 | ||
| 134 | B = 2 | ||
| 135 | local Temp = "a local value" | ||
| 136 | ``` | ||
| 137 | |||
| 138 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/doc/attributes.md b/doc/docs/pt-br/doc/attributes.md new file mode 100644 index 0000000..e6fd5a7 --- /dev/null +++ b/doc/docs/pt-br/doc/attributes.md | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | # Attributes | ||
| 2 | |||
| 3 | Syntax support for Lua 5.4 attributes. And you can still use both the `const` and `close` declaration and get constant check and scoped callback working when targeting Lua versions below 5.4. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | const a = 123 | ||
| 7 | close _ = <close>: -> print "Out of scope." | ||
| 8 | ``` | ||
| 9 | <YueDisplay> | ||
| 10 | |||
| 11 | ```yue | ||
| 12 | const a = 123 | ||
| 13 | close _ = <close>: -> print "Out of scope." | ||
| 14 | ``` | ||
| 15 | |||
| 16 | </YueDisplay> | ||
| 17 | |||
| 18 | You can do desctructuring with variables attributed as constant. | ||
| 19 | |||
| 20 | ```yuescript | ||
| 21 | const {:a, :b, c, d} = tb | ||
| 22 | -- a = 1 | ||
| 23 | ``` | ||
| 24 | <YueDisplay> | ||
| 25 | |||
| 26 | ```yue | ||
| 27 | const {:a, :b, c, d} = tb | ||
| 28 | -- a = 1 | ||
| 29 | ``` | ||
| 30 | |||
| 31 | </YueDisplay> | ||
| 32 | |||
| 33 | You can also declare a global variable to be `const`. | ||
| 34 | |||
| 35 | ```yuescript | ||
| 36 | global const Constant = 123 | ||
| 37 | -- Constant = 1 | ||
| 38 | ``` | ||
| 39 | <YueDisplay> | ||
| 40 | |||
| 41 | ```yue | ||
| 42 | global const Constant = 123 | ||
| 43 | -- Constant = 1 | ||
| 44 | ``` | ||
| 45 | |||
| 46 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/doc/backcalls.md b/doc/docs/pt-br/doc/backcalls.md new file mode 100644 index 0000000..e34331e --- /dev/null +++ b/doc/docs/pt-br/doc/backcalls.md | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | # Backcalls | ||
| 2 | |||
| 3 | Backcalls are used for unnesting callbacks. They are defined using arrows pointed to the left as the last parameter by default filling in a function call. All the syntax is mostly the same as regular arrow functions except that it is just pointing the other way and the function body does not require indent. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | x <- f | ||
| 7 | print "hello" .. x | ||
| 8 | ``` | ||
| 9 | <YueDisplay> | ||
| 10 | |||
| 11 | ```yue | ||
| 12 | x <- f | ||
| 13 | print "hello" .. x | ||
| 14 | ``` | ||
| 15 | |||
| 16 | </YueDisplay> | ||
| 17 | |||
| 18 | Fat arrow functions are also available. | ||
| 19 | |||
| 20 | ```yuescript | ||
| 21 | <= f | ||
| 22 | print @value | ||
| 23 | ``` | ||
| 24 | <YueDisplay> | ||
| 25 | |||
| 26 | ```yue | ||
| 27 | <= f | ||
| 28 | print @value | ||
| 29 | ``` | ||
| 30 | |||
| 31 | </YueDisplay> | ||
| 32 | |||
| 33 | You can specify a placeholder for where you want the backcall function to go as a parameter. | ||
| 34 | |||
| 35 | ```yuescript | ||
| 36 | (x) <- map _, [1, 2, 3] | ||
| 37 | x * 2 | ||
| 38 | ``` | ||
| 39 | <YueDisplay> | ||
| 40 | |||
| 41 | ```yue | ||
| 42 | (x) <- map _, [1, 2, 3] | ||
| 43 | x * 2 | ||
| 44 | ``` | ||
| 45 | |||
| 46 | </YueDisplay> | ||
| 47 | |||
| 48 | If you wish to have further code after your backcalls, you can set them aside with a do statement. And the parentheses can be omitted with non-fat arrow functions. | ||
| 49 | |||
| 50 | ```yuescript | ||
| 51 | result, msg = do | ||
| 52 | data <- readAsync "filename.txt" | ||
| 53 | print data | ||
| 54 | info <- processAsync data | ||
| 55 | check info | ||
| 56 | print result, msg | ||
| 57 | ``` | ||
| 58 | <YueDisplay> | ||
| 59 | |||
| 60 | ```yue | ||
| 61 | result, msg = do | ||
| 62 | data <- readAsync "filename.txt" | ||
| 63 | print data | ||
| 64 | info <- processAsync data | ||
| 65 | check info | ||
| 66 | print result, msg | ||
| 67 | ``` | ||
| 68 | |||
| 69 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/doc/comment.md b/doc/docs/pt-br/doc/comment.md new file mode 100644 index 0000000..b67c97d --- /dev/null +++ b/doc/docs/pt-br/doc/comment.md | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | # Comment | ||
| 2 | |||
| 3 | ```yuescript | ||
| 4 | -- I am a comment | ||
| 5 | |||
| 6 | str = --[[ | ||
| 7 | This is a multi-line comment. | ||
| 8 | It's OK. | ||
| 9 | ]] strA \ -- comment 1 | ||
| 10 | .. strB \ -- comment 2 | ||
| 11 | .. strC | ||
| 12 | |||
| 13 | func --[[port]] 3000, --[[ip]] "192.168.1.1" | ||
| 14 | ``` | ||
| 15 | <YueDisplay> | ||
| 16 | |||
| 17 | ```yue | ||
| 18 | -- I am a comment | ||
| 19 | |||
| 20 | str = --[[ | ||
| 21 | This is a multi-line comment. | ||
| 22 | It's OK. | ||
| 23 | ]] strA \ -- comment 1 | ||
| 24 | .. strB \ -- comment 2 | ||
| 25 | .. strC | ||
| 26 | |||
| 27 | func --[[port]] 3000, --[[ip]] "192.168.1.1" | ||
| 28 | ``` | ||
| 29 | |||
| 30 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/doc/comprehensions.md b/doc/docs/pt-br/doc/comprehensions.md new file mode 100644 index 0000000..3a92167 --- /dev/null +++ b/doc/docs/pt-br/doc/comprehensions.md | |||
| @@ -0,0 +1,271 @@ | |||
| 1 | # Comprehensions | ||
| 2 | |||
| 3 | Comprehensions provide a convenient syntax for constructing a new table by iterating over some existing object and applying an expression to its values. There are two kinds of comprehensions: list comprehensions and table comprehensions. They both produce Lua tables; list comprehensions accumulate values into an array-like table, and table comprehensions let you set both the key and the value on each iteration. | ||
| 4 | |||
| 5 | ## List Comprehensions | ||
| 6 | |||
| 7 | The following creates a copy of the items table but with all the values doubled. | ||
| 8 | |||
| 9 | ```yuescript | ||
| 10 | items = [ 1, 2, 3, 4 ] | ||
| 11 | doubled = [item * 2 for i, item in ipairs items] | ||
| 12 | ``` | ||
| 13 | <YueDisplay> | ||
| 14 | |||
| 15 | ```yue | ||
| 16 | items = [ 1, 2, 3, 4 ] | ||
| 17 | doubled = [item * 2 for i, item in ipairs items] | ||
| 18 | ``` | ||
| 19 | |||
| 20 | </YueDisplay> | ||
| 21 | |||
| 22 | The items included in the new table can be restricted with a when clause: | ||
| 23 | |||
| 24 | ```yuescript | ||
| 25 | slice = [item for i, item in ipairs items when i > 1 and i < 3] | ||
| 26 | ``` | ||
| 27 | <YueDisplay> | ||
| 28 | |||
| 29 | ```yue | ||
| 30 | slice = [item for i, item in ipairs items when i > 1 and i < 3] | ||
| 31 | ``` | ||
| 32 | |||
| 33 | </YueDisplay> | ||
| 34 | |||
| 35 | Because it is common to iterate over the values of a numerically indexed table, an **\*** operator is introduced. The doubled example can be rewritten as: | ||
| 36 | |||
| 37 | ```yuescript | ||
| 38 | doubled = [item * 2 for item in *items] | ||
| 39 | ``` | ||
| 40 | <YueDisplay> | ||
| 41 | |||
| 42 | ```yue | ||
| 43 | doubled = [item * 2 for item in *items] | ||
| 44 | ``` | ||
| 45 | |||
| 46 | </YueDisplay> | ||
| 47 | |||
| 48 | In list comprehensions, you can also use the spread operator `...` to flatten nested lists, achieving a flat map effect: | ||
| 49 | |||
| 50 | ```yuescript | ||
| 51 | data = | ||
| 52 | a: [1, 2, 3] | ||
| 53 | b: [4, 5, 6] | ||
| 54 | |||
| 55 | flat = [...v for k,v in pairs data] | ||
| 56 | -- flat is now [1, 2, 3, 4, 5, 6] | ||
| 57 | ``` | ||
| 58 | <YueDisplay> | ||
| 59 | |||
| 60 | ```yue | ||
| 61 | data = | ||
| 62 | a: [1, 2, 3] | ||
| 63 | b: [4, 5, 6] | ||
| 64 | |||
| 65 | flat = [...v for k,v in pairs data] | ||
| 66 | -- flat is now [1, 2, 3, 4, 5, 6] | ||
| 67 | ``` | ||
| 68 | |||
| 69 | </YueDisplay> | ||
| 70 | |||
| 71 | The for and when clauses can be chained as much as desired. The only requirement is that a comprehension has at least one for clause. | ||
| 72 | |||
| 73 | Using multiple for clauses is the same as using nested loops: | ||
| 74 | |||
| 75 | ```yuescript | ||
| 76 | x_coords = [4, 5, 6, 7] | ||
| 77 | y_coords = [9, 2, 3] | ||
| 78 | |||
| 79 | points = [ [x, y] for x in *x_coords \ | ||
| 80 | for y in *y_coords] | ||
| 81 | ``` | ||
| 82 | <YueDisplay> | ||
| 83 | |||
| 84 | ```yue | ||
| 85 | x_coords = [4, 5, 6, 7] | ||
| 86 | y_coords = [9, 2, 3] | ||
| 87 | |||
| 88 | points = [ [x, y] for x in *x_coords \ | ||
| 89 | for y in *y_coords] | ||
| 90 | ``` | ||
| 91 | |||
| 92 | </YueDisplay> | ||
| 93 | |||
| 94 | Numeric for loops can also be used in comprehensions: | ||
| 95 | |||
| 96 | ```yuescript | ||
| 97 | evens = [i for i = 1, 100 when i % 2 == 0] | ||
| 98 | ``` | ||
| 99 | <YueDisplay> | ||
| 100 | |||
| 101 | ```yue | ||
| 102 | evens = [i for i = 1, 100 when i % 2 == 0] | ||
| 103 | ``` | ||
| 104 | |||
| 105 | </YueDisplay> | ||
| 106 | |||
| 107 | ## Table Comprehensions | ||
| 108 | |||
| 109 | The syntax for table comprehensions is very similar, only differing by using **{** and **}** and taking two values from each iteration. | ||
| 110 | |||
| 111 | This example makes a copy of the tablething: | ||
| 112 | |||
| 113 | ```yuescript | ||
| 114 | thing = { | ||
| 115 | color: "red" | ||
| 116 | name: "fast" | ||
| 117 | width: 123 | ||
| 118 | } | ||
| 119 | |||
| 120 | thing_copy = {k, v for k, v in pairs thing} | ||
| 121 | ``` | ||
| 122 | <YueDisplay> | ||
| 123 | |||
| 124 | ```yue | ||
| 125 | thing = { | ||
| 126 | color: "red" | ||
| 127 | name: "fast" | ||
| 128 | width: 123 | ||
| 129 | } | ||
| 130 | |||
| 131 | thing_copy = {k, v for k, v in pairs thing} | ||
| 132 | ``` | ||
| 133 | |||
| 134 | </YueDisplay> | ||
| 135 | |||
| 136 | ```yuescript | ||
| 137 | no_color = {k, v for k, v in pairs thing when k != "color"} | ||
| 138 | ``` | ||
| 139 | <YueDisplay> | ||
| 140 | |||
| 141 | ```yue | ||
| 142 | no_color = {k, v for k, v in pairs thing when k != "color"} | ||
| 143 | ``` | ||
| 144 | |||
| 145 | </YueDisplay> | ||
| 146 | |||
| 147 | The **\*** operator is also supported. Here we create a square root look up table for a few numbers. | ||
| 148 | |||
| 149 | ```yuescript | ||
| 150 | numbers = [1, 2, 3, 4] | ||
| 151 | sqrts = {i, math.sqrt i for i in *numbers} | ||
| 152 | ``` | ||
| 153 | <YueDisplay> | ||
| 154 | |||
| 155 | ```yue | ||
| 156 | numbers = [1, 2, 3, 4] | ||
| 157 | sqrts = {i, math.sqrt i for i in *numbers} | ||
| 158 | ``` | ||
| 159 | |||
| 160 | </YueDisplay> | ||
| 161 | |||
| 162 | The key-value tuple in a table comprehension can also come from a single expression, in which case the expression should return two values. The first is used as the key and the second is used as the value: | ||
| 163 | |||
| 164 | In this example we convert an array of pairs to a table where the first item in the pair is the key and the second is the value. | ||
| 165 | |||
| 166 | ```yuescript | ||
| 167 | tuples = [ ["hello", "world"], ["foo", "bar"]] | ||
| 168 | tbl = {unpack tuple for tuple in *tuples} | ||
| 169 | ``` | ||
| 170 | <YueDisplay> | ||
| 171 | |||
| 172 | ```yue | ||
| 173 | tuples = [ ["hello", "world"], ["foo", "bar"]] | ||
| 174 | tbl = {unpack tuple for tuple in *tuples} | ||
| 175 | ``` | ||
| 176 | |||
| 177 | </YueDisplay> | ||
| 178 | |||
| 179 | ## Slicing | ||
| 180 | |||
| 181 | A special syntax is provided to restrict the items that are iterated over when using the **\*** operator. This is equivalent to setting the iteration bounds and a step size in a for loop. | ||
| 182 | |||
| 183 | Here we can set the minimum and maximum bounds, taking all items with indexes between 1 and 5 inclusive: | ||
| 184 | |||
| 185 | ```yuescript | ||
| 186 | slice = [item for item in *items[1, 5]] | ||
| 187 | ``` | ||
| 188 | <YueDisplay> | ||
| 189 | |||
| 190 | ```yue | ||
| 191 | slice = [item for item in *items[1, 5]] | ||
| 192 | ``` | ||
| 193 | |||
| 194 | </YueDisplay> | ||
| 195 | |||
| 196 | Any of the slice arguments can be left off to use a sensible default. In this example, if the max index is left off it defaults to the length of the table. This will take everything but the first element: | ||
| 197 | |||
| 198 | ```yuescript | ||
| 199 | slice = [item for item in *items[2,]] | ||
| 200 | ``` | ||
| 201 | <YueDisplay> | ||
| 202 | |||
| 203 | ```yue | ||
| 204 | slice = [item for item in *items[2,]] | ||
| 205 | ``` | ||
| 206 | |||
| 207 | </YueDisplay> | ||
| 208 | |||
| 209 | If the minimum bound is left out, it defaults to 1. Here we only provide a step size and leave the other bounds blank. This takes all odd indexed items: (1, 3, 5, …) | ||
| 210 | |||
| 211 | ```yuescript | ||
| 212 | slice = [item for item in *items[,,2]] | ||
| 213 | ``` | ||
| 214 | <YueDisplay> | ||
| 215 | |||
| 216 | ```yue | ||
| 217 | slice = [item for item in *items[,,2]] | ||
| 218 | ``` | ||
| 219 | |||
| 220 | </YueDisplay> | ||
| 221 | |||
| 222 | Both the minimum and maximum bounds can be negative, which means that the bounds are counted from the end of the table. | ||
| 223 | |||
| 224 | ```yuescript | ||
| 225 | -- take the last 4 items | ||
| 226 | slice = [item for item in *items[-4,-1]] | ||
| 227 | ``` | ||
| 228 | <YueDisplay> | ||
| 229 | |||
| 230 | ```yue | ||
| 231 | -- take the last 4 items | ||
| 232 | slice = [item for item in *items[-4,-1]] | ||
| 233 | ``` | ||
| 234 | |||
| 235 | </YueDisplay> | ||
| 236 | |||
| 237 | The step size can also be negative, which means that the items are taken in reverse order. | ||
| 238 | |||
| 239 | ```yuescript | ||
| 240 | reverse_slice = [item for item in *items[-1,1,-1]] | ||
| 241 | ``` | ||
| 242 | <YueDisplay> | ||
| 243 | |||
| 244 | ```yue | ||
| 245 | reverse_slice = [item for item in *items[-1,1,-1]] | ||
| 246 | ``` | ||
| 247 | |||
| 248 | </YueDisplay> | ||
| 249 | |||
| 250 | ### Slicing Expression | ||
| 251 | |||
| 252 | Slicing can also be used as an expression. This is useful for getting a sub-list of a table. | ||
| 253 | |||
| 254 | ```yuescript | ||
| 255 | -- take the 2nd and 4th items as a new list | ||
| 256 | sub_list = items[2, 4] | ||
| 257 | |||
| 258 | -- take the last 4 items | ||
| 259 | last_four_items = items[-4, -1] | ||
| 260 | ``` | ||
| 261 | <YueDisplay> | ||
| 262 | |||
| 263 | ```yue | ||
| 264 | -- take the 2nd and 4th items as a new list | ||
| 265 | sub_list = items[2, 4] | ||
| 266 | |||
| 267 | -- take the last 4 items | ||
| 268 | last_four_items = items[-4, -1] | ||
| 269 | ``` | ||
| 270 | |||
| 271 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/doc/conditionals.md b/doc/docs/pt-br/doc/conditionals.md new file mode 100644 index 0000000..5ba81cf --- /dev/null +++ b/doc/docs/pt-br/doc/conditionals.md | |||
| @@ -0,0 +1,149 @@ | |||
| 1 | # Conditionals | ||
| 2 | |||
| 3 | ```yuescript | ||
| 4 | have_coins = false | ||
| 5 | if have_coins | ||
| 6 | print "Got coins" | ||
| 7 | else | ||
| 8 | print "No coins" | ||
| 9 | ``` | ||
| 10 | <YueDisplay> | ||
| 11 | |||
| 12 | ```yue | ||
| 13 | have_coins = false | ||
| 14 | if have_coins | ||
| 15 | print "Got coins" | ||
| 16 | else | ||
| 17 | print "No coins" | ||
| 18 | ``` | ||
| 19 | |||
| 20 | </YueDisplay> | ||
| 21 | |||
| 22 | A short syntax for single statements can also be used: | ||
| 23 | |||
| 24 | ```yuescript | ||
| 25 | have_coins = false | ||
| 26 | if have_coins then print "Got coins" else print "No coins" | ||
| 27 | ``` | ||
| 28 | <YueDisplay> | ||
| 29 | |||
| 30 | ```yue | ||
| 31 | have_coins = false | ||
| 32 | if have_coins then print "Got coins" else print "No coins" | ||
| 33 | ``` | ||
| 34 | |||
| 35 | </YueDisplay> | ||
| 36 | |||
| 37 | Because if statements can be used as expressions, this can also be written as: | ||
| 38 | |||
| 39 | ```yuescript | ||
| 40 | have_coins = false | ||
| 41 | print if have_coins then "Got coins" else "No coins" | ||
| 42 | ``` | ||
| 43 | <YueDisplay> | ||
| 44 | |||
| 45 | ```yue | ||
| 46 | have_coins = false | ||
| 47 | print if have_coins then "Got coins" else "No coins" | ||
| 48 | ``` | ||
| 49 | |||
| 50 | </YueDisplay> | ||
| 51 | |||
| 52 | Conditionals can also be used in return statements and assignments: | ||
| 53 | |||
| 54 | ```yuescript | ||
| 55 | is_tall = (name) -> | ||
| 56 | if name == "Rob" | ||
| 57 | true | ||
| 58 | else | ||
| 59 | false | ||
| 60 | |||
| 61 | message = if is_tall "Rob" | ||
| 62 | "I am very tall" | ||
| 63 | else | ||
| 64 | "I am not so tall" | ||
| 65 | |||
| 66 | print message -- prints: I am very tall | ||
| 67 | ``` | ||
| 68 | <YueDisplay> | ||
| 69 | |||
| 70 | ```yue | ||
| 71 | is_tall = (name) -> | ||
| 72 | if name == "Rob" | ||
| 73 | true | ||
| 74 | else | ||
| 75 | false | ||
| 76 | |||
| 77 | message = if is_tall "Rob" | ||
| 78 | "I am very tall" | ||
| 79 | else | ||
| 80 | "I am not so tall" | ||
| 81 | |||
| 82 | print message -- prints: I am very tall | ||
| 83 | ``` | ||
| 84 | |||
| 85 | </YueDisplay> | ||
| 86 | |||
| 87 | The opposite of if is unless: | ||
| 88 | |||
| 89 | ```yuescript | ||
| 90 | unless os.date("%A") == "Monday" | ||
| 91 | print "it is not Monday!" | ||
| 92 | ``` | ||
| 93 | <YueDisplay> | ||
| 94 | |||
| 95 | ```yue | ||
| 96 | unless os.date("%A") == "Monday" | ||
| 97 | print "it is not Monday!" | ||
| 98 | ``` | ||
| 99 | |||
| 100 | </YueDisplay> | ||
| 101 | |||
| 102 | ```yuescript | ||
| 103 | print "You're lucky!" unless math.random! > 0.1 | ||
| 104 | ``` | ||
| 105 | <YueDisplay> | ||
| 106 | |||
| 107 | ```yue | ||
| 108 | print "You're lucky!" unless math.random! > 0.1 | ||
| 109 | ``` | ||
| 110 | |||
| 111 | </YueDisplay> | ||
| 112 | |||
| 113 | ## In Expression | ||
| 114 | |||
| 115 | You can write range checking code with an `in-expression`. | ||
| 116 | |||
| 117 | ```yuescript | ||
| 118 | a = 5 | ||
| 119 | |||
| 120 | if a in [1, 3, 5, 7] | ||
| 121 | print "checking equality with discrete values" | ||
| 122 | |||
| 123 | if a in list | ||
| 124 | print "checking if `a` is in a list" | ||
| 125 | ``` | ||
| 126 | <YueDisplay> | ||
| 127 | |||
| 128 | ```yue | ||
| 129 | a = 5 | ||
| 130 | |||
| 131 | if a in [1, 3, 5, 7] | ||
| 132 | print "checking equality with discrete values" | ||
| 133 | |||
| 134 | if a in list | ||
| 135 | print "checking if `a` is in a list" | ||
| 136 | ``` | ||
| 137 | |||
| 138 | </YueDisplay> | ||
| 139 | |||
| 140 | ```yuescript | ||
| 141 | print "You're lucky!" unless math.random! > 0.1 | ||
| 142 | ``` | ||
| 143 | <YueDisplay> | ||
| 144 | |||
| 145 | ```yue | ||
| 146 | print "You're lucky!" unless math.random! > 0.1 | ||
| 147 | ``` | ||
| 148 | |||
| 149 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/doc/continue.md b/doc/docs/pt-br/doc/continue.md new file mode 100644 index 0000000..b000765 --- /dev/null +++ b/doc/docs/pt-br/doc/continue.md | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | # Continue | ||
| 2 | |||
| 3 | A continue statement can be used to skip the current iteration in a loop. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | i = 0 | ||
| 7 | while i < 10 | ||
| 8 | i += 1 | ||
| 9 | continue if i % 2 == 0 | ||
| 10 | print i | ||
| 11 | ``` | ||
| 12 | <YueDisplay> | ||
| 13 | |||
| 14 | ```yue | ||
| 15 | i = 0 | ||
| 16 | while i < 10 | ||
| 17 | i += 1 | ||
| 18 | continue if i % 2 == 0 | ||
| 19 | print i | ||
| 20 | ``` | ||
| 21 | |||
| 22 | </YueDisplay> | ||
| 23 | |||
| 24 | continue can also be used with loop expressions to prevent that iteration from accumulating into the result. This examples filters the array table into just even numbers: | ||
| 25 | |||
| 26 | ```yuescript | ||
| 27 | my_numbers = [1, 2, 3, 4, 5, 6] | ||
| 28 | odds = for x in *my_numbers | ||
| 29 | continue if x % 2 == 1 | ||
| 30 | x | ||
| 31 | ``` | ||
| 32 | <YueDisplay> | ||
| 33 | |||
| 34 | ```yue | ||
| 35 | my_numbers = [1, 2, 3, 4, 5, 6] | ||
| 36 | odds = for x in *my_numbers | ||
| 37 | continue if x % 2 == 1 | ||
| 38 | x | ||
| 39 | ``` | ||
| 40 | |||
| 41 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/doc/destructuring-assignment.md b/doc/docs/pt-br/doc/destructuring-assignment.md new file mode 100644 index 0000000..e7b8046 --- /dev/null +++ b/doc/docs/pt-br/doc/destructuring-assignment.md | |||
| @@ -0,0 +1,240 @@ | |||
| 1 | # Destructuring Assignment | ||
| 2 | |||
| 3 | Destructuring assignment is a way to quickly extract values from a table by their name or position in array based tables. | ||
| 4 | |||
| 5 | Typically when you see a table literal, {1,2,3}, it is on the right hand side of an assignment because it is a value. Destructuring assignment swaps the role of the table literal, and puts it on the left hand side of an assign statement. | ||
| 6 | |||
| 7 | This is best explained with examples. Here is how you would unpack the first two values from a table: | ||
| 8 | |||
| 9 | ```yuescript | ||
| 10 | thing = [1, 2] | ||
| 11 | |||
| 12 | [a, b] = thing | ||
| 13 | print a, b | ||
| 14 | ``` | ||
| 15 | <YueDisplay> | ||
| 16 | |||
| 17 | ```yue | ||
| 18 | thing = [1, 2] | ||
| 19 | |||
| 20 | [a, b] = thing | ||
| 21 | print a, b | ||
| 22 | ``` | ||
| 23 | |||
| 24 | </YueDisplay> | ||
| 25 | |||
| 26 | In the destructuring table literal, the key represents the key to read from the right hand side, and the value represents the name the read value will be assigned to. | ||
| 27 | |||
| 28 | ```yuescript | ||
| 29 | obj = { | ||
| 30 | hello: "world" | ||
| 31 | day: "tuesday" | ||
| 32 | length: 20 | ||
| 33 | } | ||
| 34 | |||
| 35 | {hello: hello, day: the_day} = obj | ||
| 36 | print hello, the_day | ||
| 37 | |||
| 38 | :day = obj -- OK to do simple destructuring without braces | ||
| 39 | ``` | ||
| 40 | <YueDisplay> | ||
| 41 | |||
| 42 | ```yue | ||
| 43 | obj = { | ||
| 44 | hello: "world" | ||
| 45 | day: "tuesday" | ||
| 46 | length: 20 | ||
| 47 | } | ||
| 48 | |||
| 49 | {hello: hello, day: the_day} = obj | ||
| 50 | print hello, the_day | ||
| 51 | |||
| 52 | :day = obj -- OK to do simple destructuring without braces | ||
| 53 | ``` | ||
| 54 | |||
| 55 | </YueDisplay> | ||
| 56 | |||
| 57 | This also works with nested data structures as well: | ||
| 58 | |||
| 59 | ```yuescript | ||
| 60 | obj2 = { | ||
| 61 | numbers: [1, 2, 3, 4] | ||
| 62 | properties: { | ||
| 63 | color: "green" | ||
| 64 | height: 13.5 | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | {numbers: [first, second], properties: {color: color}} = obj2 | ||
| 69 | print first, second, color | ||
| 70 | ``` | ||
| 71 | <YueDisplay> | ||
| 72 | |||
| 73 | ```yue | ||
| 74 | obj2 = { | ||
| 75 | numbers: [1, 2, 3, 4] | ||
| 76 | properties: { | ||
| 77 | color: "green" | ||
| 78 | height: 13.5 | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | {numbers: [first, second], properties: {color: color}} = obj2 | ||
| 83 | print first, second, color | ||
| 84 | ``` | ||
| 85 | |||
| 86 | </YueDisplay> | ||
| 87 | |||
| 88 | If the destructuring statement is complicated, feel free to spread it out over a few lines. A slightly more complicated example: | ||
| 89 | |||
| 90 | ```yuescript | ||
| 91 | { | ||
| 92 | numbers: [first, second] | ||
| 93 | properties: { | ||
| 94 | color: color | ||
| 95 | } | ||
| 96 | } = obj2 | ||
| 97 | ``` | ||
| 98 | <YueDisplay> | ||
| 99 | |||
| 100 | ```yue | ||
| 101 | { | ||
| 102 | numbers: [first, second] | ||
| 103 | properties: { | ||
| 104 | color: color | ||
| 105 | } | ||
| 106 | } = obj2 | ||
| 107 | ``` | ||
| 108 | |||
| 109 | </YueDisplay> | ||
| 110 | |||
| 111 | It's common to extract values from at table and assign them the local variables that have the same name as the key. In order to avoid repetition we can use the **:** prefix operator: | ||
| 112 | |||
| 113 | ```yuescript | ||
| 114 | {:concat, :insert} = table | ||
| 115 | ``` | ||
| 116 | <YueDisplay> | ||
| 117 | |||
| 118 | ```yue | ||
| 119 | {:concat, :insert} = table | ||
| 120 | ``` | ||
| 121 | |||
| 122 | </YueDisplay> | ||
| 123 | |||
| 124 | This is effectively the same as import, but we can rename fields we want to extract by mixing the syntax: | ||
| 125 | |||
| 126 | ```yuescript | ||
| 127 | {:mix, :max, random: rand} = math | ||
| 128 | ``` | ||
| 129 | <YueDisplay> | ||
| 130 | |||
| 131 | ```yue | ||
| 132 | {:mix, :max, random: rand} = math | ||
| 133 | ``` | ||
| 134 | |||
| 135 | </YueDisplay> | ||
| 136 | |||
| 137 | You can write default values while doing destructuring like: | ||
| 138 | |||
| 139 | ```yuescript | ||
| 140 | {:name = "nameless", :job = "jobless"} = person | ||
| 141 | ``` | ||
| 142 | <YueDisplay> | ||
| 143 | |||
| 144 | ```yue | ||
| 145 | {:name = "nameless", :job = "jobless"} = person | ||
| 146 | ``` | ||
| 147 | |||
| 148 | </YueDisplay> | ||
| 149 | |||
| 150 | You can use `_` as placeholder when doing a list destructuring: | ||
| 151 | |||
| 152 | ```yuescript | ||
| 153 | [_, two, _, four] = items | ||
| 154 | ``` | ||
| 155 | <YueDisplay> | ||
| 156 | |||
| 157 | ```yue | ||
| 158 | [_, two, _, four] = items | ||
| 159 | ``` | ||
| 160 | |||
| 161 | </YueDisplay> | ||
| 162 | |||
| 163 | ## Range Destructuring | ||
| 164 | |||
| 165 | You can use the spread operator `...` in list destructuring to capture a range of values. This is useful when you want to extract specific elements from the beginning and end of a list while collecting the rest in between. | ||
| 166 | |||
| 167 | ```yuescript | ||
| 168 | orders = ["first", "second", "third", "fourth", "last"] | ||
| 169 | [first, ...bulk, last] = orders | ||
| 170 | print first -- prints: first | ||
| 171 | print bulk -- prints: {"second", "third", "fourth"} | ||
| 172 | print last -- prints: last | ||
| 173 | ``` | ||
| 174 | <YueDisplay> | ||
| 175 | |||
| 176 | ```yue | ||
| 177 | orders = ["first", "second", "third", "fourth", "last"] | ||
| 178 | [first, ...bulk, last] = orders | ||
| 179 | print first -- prints: first | ||
| 180 | print bulk -- prints: {"second", "third", "fourth"} | ||
| 181 | print last -- prints: last | ||
| 182 | ``` | ||
| 183 | |||
| 184 | </YueDisplay> | ||
| 185 | |||
| 186 | The spread operator can be used in different positions to capture different ranges, and you can use `_` as a placeholder for the values you don't want to capture: | ||
| 187 | |||
| 188 | ```yuescript | ||
| 189 | -- Capture everything after first element | ||
| 190 | [first, ...rest] = orders | ||
| 191 | |||
| 192 | -- Capture everything before last element | ||
| 193 | [...start, last] = orders | ||
| 194 | |||
| 195 | -- Capture things except the middle elements | ||
| 196 | [first, ..._, last] = orders | ||
| 197 | ``` | ||
| 198 | <YueDisplay> | ||
| 199 | |||
| 200 | ```yue | ||
| 201 | -- Capture everything after first element | ||
| 202 | [first, ...rest] = orders | ||
| 203 | |||
| 204 | -- Capture everything before last element | ||
| 205 | [...start, last] = orders | ||
| 206 | |||
| 207 | -- Capture things except the middle elements | ||
| 208 | [first, ..._, last] = orders | ||
| 209 | ``` | ||
| 210 | |||
| 211 | </YueDisplay> | ||
| 212 | |||
| 213 | ## Destructuring In Other Places | ||
| 214 | |||
| 215 | Destructuring can also show up in places where an assignment implicitly takes place. An example of this is a for loop: | ||
| 216 | |||
| 217 | ```yuescript | ||
| 218 | tuples = [ | ||
| 219 | ["hello", "world"] | ||
| 220 | ["egg", "head"] | ||
| 221 | ] | ||
| 222 | |||
| 223 | for [left, right] in *tuples | ||
| 224 | print left, right | ||
| 225 | ``` | ||
| 226 | <YueDisplay> | ||
| 227 | |||
| 228 | ```yue | ||
| 229 | tuples = [ | ||
| 230 | ["hello", "world"] | ||
| 231 | ["egg", "head"] | ||
| 232 | ] | ||
| 233 | |||
| 234 | for [left, right] in *tuples | ||
| 235 | print left, right | ||
| 236 | ``` | ||
| 237 | |||
| 238 | </YueDisplay> | ||
| 239 | |||
| 240 | We know each element in the array table is a two item tuple, so we can unpack it directly in the names clause of the for statement using a destructure. | ||
diff --git a/doc/docs/pt-br/doc/do.md b/doc/docs/pt-br/doc/do.md new file mode 100644 index 0000000..4990d6f --- /dev/null +++ b/doc/docs/pt-br/doc/do.md | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | # Do | ||
| 2 | |||
| 3 | When used as a statement, do works just like it does in Lua. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | do | ||
| 7 | var = "hello" | ||
| 8 | print var | ||
| 9 | print var -- nil here | ||
| 10 | ``` | ||
| 11 | <YueDisplay> | ||
| 12 | |||
| 13 | ```yue | ||
| 14 | do | ||
| 15 | var = "hello" | ||
| 16 | print var | ||
| 17 | print var -- nil here | ||
| 18 | ``` | ||
| 19 | |||
| 20 | </YueDisplay> | ||
| 21 | |||
| 22 | 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. | ||
| 23 | |||
| 24 | ```yuescript | ||
| 25 | counter = do | ||
| 26 | i = 0 | ||
| 27 | -> | ||
| 28 | i += 1 | ||
| 29 | i | ||
| 30 | |||
| 31 | print counter! | ||
| 32 | print counter! | ||
| 33 | ``` | ||
| 34 | <YueDisplay> | ||
| 35 | |||
| 36 | ```yue | ||
| 37 | counter = do | ||
| 38 | i = 0 | ||
| 39 | -> | ||
| 40 | i += 1 | ||
| 41 | i | ||
| 42 | |||
| 43 | print counter! | ||
| 44 | print counter! | ||
| 45 | ``` | ||
| 46 | |||
| 47 | </YueDisplay> | ||
| 48 | |||
| 49 | ```yuescript | ||
| 50 | tbl = { | ||
| 51 | key: do | ||
| 52 | print "assigning key!" | ||
| 53 | 1234 | ||
| 54 | } | ||
| 55 | ``` | ||
| 56 | <YueDisplay> | ||
| 57 | |||
| 58 | ```yue | ||
| 59 | tbl = { | ||
| 60 | key: do | ||
| 61 | print "assigning key!" | ||
| 62 | 1234 | ||
| 63 | } | ||
| 64 | ``` | ||
| 65 | |||
| 66 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/doc/for-loop.md b/doc/docs/pt-br/doc/for-loop.md new file mode 100644 index 0000000..cabcde5 --- /dev/null +++ b/doc/docs/pt-br/doc/for-loop.md | |||
| @@ -0,0 +1,127 @@ | |||
| 1 | # For Loop | ||
| 2 | |||
| 3 | There are two for loop forms, just like in Lua. A numeric one and a generic one: | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | for i = 10, 20 | ||
| 7 | print i | ||
| 8 | |||
| 9 | for k = 1, 15, 2 -- an optional step provided | ||
| 10 | print k | ||
| 11 | |||
| 12 | for key, value in pairs object | ||
| 13 | print key, value | ||
| 14 | ``` | ||
| 15 | <YueDisplay> | ||
| 16 | |||
| 17 | ```yue | ||
| 18 | for i = 10, 20 | ||
| 19 | print i | ||
| 20 | |||
| 21 | for k = 1, 15, 2 -- an optional step provided | ||
| 22 | print k | ||
| 23 | |||
| 24 | for key, value in pairs object | ||
| 25 | print key, value | ||
| 26 | ``` | ||
| 27 | |||
| 28 | </YueDisplay> | ||
| 29 | |||
| 30 | The slicing and **\*** operators can be used, just like with comprehensions: | ||
| 31 | |||
| 32 | ```yuescript | ||
| 33 | for item in *items[2, 4] | ||
| 34 | print item | ||
| 35 | ``` | ||
| 36 | <YueDisplay> | ||
| 37 | |||
| 38 | ```yue | ||
| 39 | for item in *items[2, 4] | ||
| 40 | print item | ||
| 41 | ``` | ||
| 42 | |||
| 43 | </YueDisplay> | ||
| 44 | |||
| 45 | A shorter syntax is also available for all variations when the body is only a single line: | ||
| 46 | |||
| 47 | ```yuescript | ||
| 48 | for item in *items do print item | ||
| 49 | |||
| 50 | for j = 1, 10, 3 do print j | ||
| 51 | ``` | ||
| 52 | <YueDisplay> | ||
| 53 | |||
| 54 | ```yue | ||
| 55 | for item in *items do print item | ||
| 56 | |||
| 57 | for j = 1, 10, 3 do print j | ||
| 58 | ``` | ||
| 59 | |||
| 60 | </YueDisplay> | ||
| 61 | |||
| 62 | A for loop can also be used as an expression. The last statement in the body of the for loop is coerced into an expression and appended to an accumulating array table. | ||
| 63 | |||
| 64 | Doubling every even number: | ||
| 65 | |||
| 66 | ```yuescript | ||
| 67 | doubled_evens = for i = 1, 20 | ||
| 68 | if i % 2 == 0 | ||
| 69 | i * 2 | ||
| 70 | else | ||
| 71 | i | ||
| 72 | ``` | ||
| 73 | <YueDisplay> | ||
| 74 | |||
| 75 | ```yue | ||
| 76 | doubled_evens = for i = 1, 20 | ||
| 77 | if i % 2 == 0 | ||
| 78 | i * 2 | ||
| 79 | else | ||
| 80 | i | ||
| 81 | ``` | ||
| 82 | |||
| 83 | </YueDisplay> | ||
| 84 | |||
| 85 | 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. | ||
| 86 | |||
| 87 | For example, to find the first number greater than 10: | ||
| 88 | |||
| 89 | ```yuescript | ||
| 90 | first_large = for n in *numbers | ||
| 91 | break n if n > 10 | ||
| 92 | ``` | ||
| 93 | <YueDisplay> | ||
| 94 | |||
| 95 | ```yue | ||
| 96 | first_large = for n in *numbers | ||
| 97 | break n if n > 10 | ||
| 98 | ``` | ||
| 99 | |||
| 100 | </YueDisplay> | ||
| 101 | |||
| 102 | This break-with-value syntax enables concise and expressive search or early-exit patterns directly within loop expressions. | ||
| 103 | |||
| 104 | You can also filter values by combining the for loop expression with the continue statement. | ||
| 105 | |||
| 106 | 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. | ||
| 107 | |||
| 108 | ```yuescript | ||
| 109 | func_a = -> for i = 1, 10 do print i | ||
| 110 | func_b = -> return for i = 1, 10 do i | ||
| 111 | |||
| 112 | print func_a! -- prints nil | ||
| 113 | print func_b! -- prints table object | ||
| 114 | ``` | ||
| 115 | <YueDisplay> | ||
| 116 | |||
| 117 | ```yue | ||
| 118 | func_a = -> for i = 1, 10 do print i | ||
| 119 | func_b = -> return for i = 1, 10 do i | ||
| 120 | |||
| 121 | print func_a! -- prints nil | ||
| 122 | print func_b! -- prints table object | ||
| 123 | ``` | ||
| 124 | |||
| 125 | </YueDisplay> | ||
| 126 | |||
| 127 | This is done to avoid the needless creation of tables for functions that don't need to return the results of the loop. | ||
diff --git a/doc/docs/pt-br/doc/function-literals.md b/doc/docs/pt-br/doc/function-literals.md new file mode 100644 index 0000000..316e07c --- /dev/null +++ b/doc/docs/pt-br/doc/function-literals.md | |||
| @@ -0,0 +1,494 @@ | |||
| 1 | # Function Literals | ||
| 2 | |||
| 3 | All functions are created using a function expression. A simple function is denoted using the arrow: **->**. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | my_function = -> | ||
| 7 | my_function() -- call the empty function | ||
| 8 | ``` | ||
| 9 | <YueDisplay> | ||
| 10 | |||
| 11 | ```yue | ||
| 12 | my_function = -> | ||
| 13 | my_function() -- call the empty function | ||
| 14 | ``` | ||
| 15 | |||
| 16 | </YueDisplay> | ||
| 17 | |||
| 18 | The body of the function can either be one statement placed directly after the arrow, or it can be a series of statements indented on the following lines: | ||
| 19 | |||
| 20 | ```yuescript | ||
| 21 | func_a = -> print "hello world" | ||
| 22 | |||
| 23 | func_b = -> | ||
| 24 | value = 100 | ||
| 25 | print "The value:", value | ||
| 26 | ``` | ||
| 27 | <YueDisplay> | ||
| 28 | |||
| 29 | ```yue | ||
| 30 | func_a = -> print "hello world" | ||
| 31 | |||
| 32 | func_b = -> | ||
| 33 | value = 100 | ||
| 34 | print "The value:", value | ||
| 35 | ``` | ||
| 36 | |||
| 37 | </YueDisplay> | ||
| 38 | |||
| 39 | If a function has no arguments, it can be called using the ! operator, instead of empty parentheses. The ! invocation is the preferred way to call functions with no arguments. | ||
| 40 | |||
| 41 | ```yuescript | ||
| 42 | func_a! | ||
| 43 | func_b() | ||
| 44 | ``` | ||
| 45 | <YueDisplay> | ||
| 46 | |||
| 47 | ```yue | ||
| 48 | func_a! | ||
| 49 | func_b() | ||
| 50 | ``` | ||
| 51 | |||
| 52 | </YueDisplay> | ||
| 53 | |||
| 54 | Functions with arguments can be created by preceding the arrow with a list of argument names in parentheses: | ||
| 55 | |||
| 56 | ```yuescript | ||
| 57 | sum = (x, y) -> print "sum", x + y | ||
| 58 | ``` | ||
| 59 | <YueDisplay> | ||
| 60 | |||
| 61 | ```yue | ||
| 62 | sum = (x, y) -> print "sum", x + y | ||
| 63 | ``` | ||
| 64 | |||
| 65 | </YueDisplay> | ||
| 66 | |||
| 67 | Functions can be called by listing the arguments after the name of an expression that evaluates to a function. When chaining together function calls, the arguments are applied to the closest function to the left. | ||
| 68 | |||
| 69 | ```yuescript | ||
| 70 | sum 10, 20 | ||
| 71 | print sum 10, 20 | ||
| 72 | |||
| 73 | a b c "a", "b", "c" | ||
| 74 | ``` | ||
| 75 | <YueDisplay> | ||
| 76 | |||
| 77 | ```yue | ||
| 78 | sum 10, 20 | ||
| 79 | print sum 10, 20 | ||
| 80 | |||
| 81 | a b c "a", "b", "c" | ||
| 82 | ``` | ||
| 83 | |||
| 84 | </YueDisplay> | ||
| 85 | |||
| 86 | In order to avoid ambiguity in when calling functions, parentheses can also be used to surround the arguments. This is required here in order to make sure the right arguments get sent to the right functions. | ||
| 87 | |||
| 88 | ```yuescript | ||
| 89 | print "x:", sum(10, 20), "y:", sum(30, 40) | ||
| 90 | ``` | ||
| 91 | <YueDisplay> | ||
| 92 | |||
| 93 | ```yue | ||
| 94 | print "x:", sum(10, 20), "y:", sum(30, 40) | ||
| 95 | ``` | ||
| 96 | |||
| 97 | </YueDisplay> | ||
| 98 | |||
| 99 | There must not be any space between the opening parenthesis and the function. | ||
| 100 | |||
| 101 | Functions will coerce the last statement in their body into a return statement, this is called implicit return: | ||
| 102 | |||
| 103 | ```yuescript | ||
| 104 | sum = (x, y) -> x + y | ||
| 105 | print "The sum is ", sum 10, 20 | ||
| 106 | ``` | ||
| 107 | <YueDisplay> | ||
| 108 | |||
| 109 | ```yue | ||
| 110 | sum = (x, y) -> x + y | ||
| 111 | print "The sum is ", sum 10, 20 | ||
| 112 | ``` | ||
| 113 | |||
| 114 | </YueDisplay> | ||
| 115 | |||
| 116 | And if you need to explicitly return, you can use the return keyword: | ||
| 117 | |||
| 118 | ```yuescript | ||
| 119 | sum = (x, y) -> return x + y | ||
| 120 | ``` | ||
| 121 | <YueDisplay> | ||
| 122 | |||
| 123 | ```yue | ||
| 124 | sum = (x, y) -> return x + y | ||
| 125 | ``` | ||
| 126 | |||
| 127 | </YueDisplay> | ||
| 128 | |||
| 129 | Just like in Lua, functions can return multiple values. The last statement must be a list of values separated by commas: | ||
| 130 | |||
| 131 | ```yuescript | ||
| 132 | mystery = (x, y) -> x + y, x - y | ||
| 133 | a, b = mystery 10, 20 | ||
| 134 | ``` | ||
| 135 | <YueDisplay> | ||
| 136 | |||
| 137 | ```yue | ||
| 138 | mystery = (x, y) -> x + y, x - y | ||
| 139 | a, b = mystery 10, 20 | ||
| 140 | ``` | ||
| 141 | |||
| 142 | </YueDisplay> | ||
| 143 | |||
| 144 | ## Fat Arrows | ||
| 145 | |||
| 146 | Because it is an idiom in Lua to send an object as the first argument when calling a method, a special syntax is provided for creating functions which automatically includes a self argument. | ||
| 147 | |||
| 148 | ```yuescript | ||
| 149 | func = (num) => @value + num | ||
| 150 | ``` | ||
| 151 | <YueDisplay> | ||
| 152 | |||
| 153 | ```yue | ||
| 154 | func = (num) => @value + num | ||
| 155 | ``` | ||
| 156 | |||
| 157 | </YueDisplay> | ||
| 158 | |||
| 159 | ## Argument Defaults | ||
| 160 | |||
| 161 | It is possible to provide default values for the arguments of a function. An argument is determined to be empty if its value is nil. Any nil arguments that have a default value will be replace before the body of the function is run. | ||
| 162 | |||
| 163 | ```yuescript | ||
| 164 | my_function = (name = "something", height = 100) -> | ||
| 165 | print "Hello I am", name | ||
| 166 | print "My height is", height | ||
| 167 | ``` | ||
| 168 | <YueDisplay> | ||
| 169 | |||
| 170 | ```yue | ||
| 171 | my_function = (name = "something", height = 100) -> | ||
| 172 | print "Hello I am", name | ||
| 173 | print "My height is", height | ||
| 174 | ``` | ||
| 175 | |||
| 176 | </YueDisplay> | ||
| 177 | |||
| 178 | An argument default value expression is evaluated in the body of the function in the order of the argument declarations. For this reason default values have access to previously declared arguments. | ||
| 179 | |||
| 180 | ```yuescript | ||
| 181 | some_args = (x = 100, y = x + 1000) -> | ||
| 182 | print x + y | ||
| 183 | ``` | ||
| 184 | <YueDisplay> | ||
| 185 | |||
| 186 | ```yue | ||
| 187 | some_args = (x = 100, y = x + 1000) -> | ||
| 188 | print x + y | ||
| 189 | ``` | ||
| 190 | |||
| 191 | </YueDisplay> | ||
| 192 | |||
| 193 | ## Considerations | ||
| 194 | |||
| 195 | Because of the expressive parentheses-less way of calling functions, some restrictions must be put in place to avoid parsing ambiguity involving whitespace. | ||
| 196 | |||
| 197 | The minus sign plays two roles, a unary negation operator and a binary subtraction operator. Consider how the following examples compile: | ||
| 198 | |||
| 199 | ```yuescript | ||
| 200 | a = x - 10 | ||
| 201 | b = x-10 | ||
| 202 | c = x -y | ||
| 203 | d = x- z | ||
| 204 | ``` | ||
| 205 | <YueDisplay> | ||
| 206 | |||
| 207 | ```yue | ||
| 208 | a = x - 10 | ||
| 209 | b = x-10 | ||
| 210 | c = x -y | ||
| 211 | d = x- z | ||
| 212 | ``` | ||
| 213 | |||
| 214 | </YueDisplay> | ||
| 215 | |||
| 216 | The precedence of the first argument of a function call can be controlled using whitespace if the argument is a literal string. In Lua, it is common to leave off parentheses when calling a function with a single string or table literal. | ||
| 217 | |||
| 218 | When there is no space between a variable and a string literal, the function call takes precedence over any following expressions. No other arguments can be passed to the function when it is called this way. | ||
| 219 | |||
| 220 | Where there is a space following a variable and a string literal, the function call acts as show above. The string literal belongs to any following expressions (if they exist), which serves as the argument list. | ||
| 221 | |||
| 222 | ```yuescript | ||
| 223 | x = func"hello" + 100 | ||
| 224 | y = func "hello" + 100 | ||
| 225 | ``` | ||
| 226 | <YueDisplay> | ||
| 227 | |||
| 228 | ```yue | ||
| 229 | x = func"hello" + 100 | ||
| 230 | y = func "hello" + 100 | ||
| 231 | ``` | ||
| 232 | |||
| 233 | </YueDisplay> | ||
| 234 | |||
| 235 | ## Multi-line arguments | ||
| 236 | |||
| 237 | When calling functions that take a large number of arguments, it is convenient to split the argument list over multiple lines. Because of the white-space sensitive nature of the language, care must be taken when splitting up the argument list. | ||
| 238 | |||
| 239 | If an argument list is to be continued onto the next line, the current line must end in a comma. And the following line must be indented more than the current indentation. Once indented, all other argument lines must be at the same level of indentation to be part of the argument list | ||
| 240 | |||
| 241 | ```yuescript | ||
| 242 | my_func 5, 4, 3, | ||
| 243 | 8, 9, 10 | ||
| 244 | |||
| 245 | cool_func 1, 2, | ||
| 246 | 3, 4, | ||
| 247 | 5, 6, | ||
| 248 | 7, 8 | ||
| 249 | ``` | ||
| 250 | <YueDisplay> | ||
| 251 | |||
| 252 | ```yue | ||
| 253 | my_func 5, 4, 3, | ||
| 254 | 8, 9, 10 | ||
| 255 | |||
| 256 | cool_func 1, 2, | ||
| 257 | 3, 4, | ||
| 258 | 5, 6, | ||
| 259 | 7, 8 | ||
| 260 | ``` | ||
| 261 | |||
| 262 | </YueDisplay> | ||
| 263 | |||
| 264 | This type of invocation can be nested. The level of indentation is used to determine to which function the arguments belong to. | ||
| 265 | |||
| 266 | ```yuescript | ||
| 267 | my_func 5, 6, 7, | ||
| 268 | 6, another_func 6, 7, 8, | ||
| 269 | 9, 1, 2, | ||
| 270 | 5, 4 | ||
| 271 | ``` | ||
| 272 | <YueDisplay> | ||
| 273 | |||
| 274 | ```yue | ||
| 275 | my_func 5, 6, 7, | ||
| 276 | 6, another_func 6, 7, 8, | ||
| 277 | 9, 1, 2, | ||
| 278 | 5, 4 | ||
| 279 | ``` | ||
| 280 | |||
| 281 | </YueDisplay> | ||
| 282 | |||
| 283 | Because tables also use the comma as a delimiter, this indentation syntax is helpful for letting values be part of the argument list instead of being part of the table. | ||
| 284 | |||
| 285 | ```yuescript | ||
| 286 | x = [ | ||
| 287 | 1, 2, 3, 4, a_func 4, 5, | ||
| 288 | 5, 6, | ||
| 289 | 8, 9, 10 | ||
| 290 | ] | ||
| 291 | ``` | ||
| 292 | <YueDisplay> | ||
| 293 | |||
| 294 | ```yue | ||
| 295 | x = [ | ||
| 296 | 1, 2, 3, 4, a_func 4, 5, | ||
| 297 | 5, 6, | ||
| 298 | 8, 9, 10 | ||
| 299 | ] | ||
| 300 | ``` | ||
| 301 | |||
| 302 | </YueDisplay> | ||
| 303 | |||
| 304 | Although uncommon, notice how we can give a deeper indentation for function arguments if we know we will be using a lower indentation further on. | ||
| 305 | |||
| 306 | ```yuescript | ||
| 307 | y = [ my_func 1, 2, 3, | ||
| 308 | 4, 5, | ||
| 309 | 5, 6, 7 | ||
| 310 | ] | ||
| 311 | ``` | ||
| 312 | <YueDisplay> | ||
| 313 | |||
| 314 | ```yue | ||
| 315 | y = [ my_func 1, 2, 3, | ||
| 316 | 4, 5, | ||
| 317 | 5, 6, 7 | ||
| 318 | ] | ||
| 319 | ``` | ||
| 320 | |||
| 321 | </YueDisplay> | ||
| 322 | |||
| 323 | The same thing can be done with other block level statements like conditionals. We can use indentation level to determine what statement a value belongs to: | ||
| 324 | |||
| 325 | ```yuescript | ||
| 326 | if func 1, 2, 3, | ||
| 327 | "hello", | ||
| 328 | "world" | ||
| 329 | print "hello" | ||
| 330 | print "I am inside if" | ||
| 331 | |||
| 332 | if func 1, 2, 3, | ||
| 333 | "hello", | ||
| 334 | "world" | ||
| 335 | print "hello" | ||
| 336 | print "I am inside if" | ||
| 337 | ``` | ||
| 338 | <YueDisplay> | ||
| 339 | |||
| 340 | ```yue | ||
| 341 | if func 1, 2, 3, | ||
| 342 | "hello", | ||
| 343 | "world" | ||
| 344 | print "hello" | ||
| 345 | print "I am inside if" | ||
| 346 | |||
| 347 | if func 1, 2, 3, | ||
| 348 | "hello", | ||
| 349 | "world" | ||
| 350 | print "hello" | ||
| 351 | print "I am inside if" | ||
| 352 | ``` | ||
| 353 | |||
| 354 | </YueDisplay> | ||
| 355 | |||
| 356 | ## Parameter Destructuring | ||
| 357 | |||
| 358 | YueScript now supports destructuring function parameters when the argument is an object. Two forms of destructuring table literals are available: | ||
| 359 | |||
| 360 | * **Curly-brace wrapped literals/object parameters**, allowing optional default values when fields are missing (e.g., `{:a, :b}`, `{a: a1 = 123}`). | ||
| 361 | |||
| 362 | * **Unwrapped simple table syntax**, starting with a sequence of key-value or shorthand bindings and continuing until another expression terminates it (e.g., `:a, b: b1, :c`). This form extracts multiple fields from the same object. | ||
| 363 | |||
| 364 | ```yuescript | ||
| 365 | f1 = (:a, :b, :c) -> | ||
| 366 | print a, b, c | ||
| 367 | |||
| 368 | f1 a: 1, b: "2", c: {} | ||
| 369 | |||
| 370 | f2 = ({a: a1 = 123, :b = 'abc'}, c = {}) -> | ||
| 371 | print a1, b, c | ||
| 372 | |||
| 373 | arg1 = {a: 0} | ||
| 374 | f2 arg1, arg2 | ||
| 375 | ``` | ||
| 376 | <YueDisplay> | ||
| 377 | |||
| 378 | ```yue | ||
| 379 | f1 = (:a, :b, :c) -> | ||
| 380 | print a, b, c | ||
| 381 | |||
| 382 | f1 a: 1, b: "2", c: {} | ||
| 383 | |||
| 384 | f2 = ({a: a1 = 123, :b = 'abc'}, c = {}) -> | ||
| 385 | print a1, b, c | ||
| 386 | |||
| 387 | arg1 = {a: 0} | ||
| 388 | f2 arg1, arg2 | ||
| 389 | ``` | ||
| 390 | |||
| 391 | </YueDisplay> | ||
| 392 | |||
| 393 | ## Prefixed Return Expression | ||
| 394 | |||
| 395 | When working with deeply nested function bodies, it can be tedious to maintain readability and consistency of the return value. To address this, YueScript introduces the **Prefixed Return Expression** syntax. Its form is as follows: | ||
| 396 | |||
| 397 | ```yuescript | ||
| 398 | findFirstEven = (list): nil -> | ||
| 399 | for item in *list | ||
| 400 | if type(item) == "table" | ||
| 401 | for sub in *item | ||
| 402 | if sub % 2 == 0 | ||
| 403 | return sub | ||
| 404 | ``` | ||
| 405 | <YueDisplay> | ||
| 406 | |||
| 407 | ```yue | ||
| 408 | findFirstEven = (list): nil -> | ||
| 409 | for item in *list | ||
| 410 | if type(item) == "table" | ||
| 411 | for sub in *item | ||
| 412 | if sub % 2 == 0 | ||
| 413 | return sub | ||
| 414 | ``` | ||
| 415 | |||
| 416 | </YueDisplay> | ||
| 417 | |||
| 418 | This is equivalent to: | ||
| 419 | |||
| 420 | ```yuescript | ||
| 421 | findFirstEven = (list) -> | ||
| 422 | for item in *list | ||
| 423 | if type(item) == "table" | ||
| 424 | for sub in *item | ||
| 425 | if sub % 2 == 0 | ||
| 426 | return sub | ||
| 427 | nil | ||
| 428 | ``` | ||
| 429 | <YueDisplay> | ||
| 430 | |||
| 431 | ```yue | ||
| 432 | findFirstEven = (list) -> | ||
| 433 | for item in *list | ||
| 434 | if type(item) == "table" | ||
| 435 | for sub in *item | ||
| 436 | if sub % 2 == 0 | ||
| 437 | return sub | ||
| 438 | nil | ||
| 439 | ``` | ||
| 440 | |||
| 441 | </YueDisplay> | ||
| 442 | |||
| 443 | The only difference is that you can move the final return expression before the `->` or `=>` token to indicate the function’s implicit return value as the last statement. This way, even in functions with multiple nested loops or conditional branches, you no longer need to write a trailing return expression at the end of the function body, making the logic structure more straightforward and easier to follow. | ||
| 444 | |||
| 445 | ## Named Varargs | ||
| 446 | |||
| 447 | You can use the `(...t) ->` syntax to automatically store varargs into a named table. This table will contain all passed arguments (including `nil` values), and the `n` field of the table will store the actual number of arguments passed (including `nil` values). | ||
| 448 | |||
| 449 | ```yuescript | ||
| 450 | f = (...t) -> | ||
| 451 | print "argument count:", t.n | ||
| 452 | print "table length:", #t | ||
| 453 | for i = 1, t.n | ||
| 454 | print t[i] | ||
| 455 | |||
| 456 | f 1, 2, 3 | ||
| 457 | f "a", "b", "c", "d" | ||
| 458 | f! | ||
| 459 | |||
| 460 | -- Handling cases with nil values | ||
| 461 | process = (...args) -> | ||
| 462 | sum = 0 | ||
| 463 | for i = 1, args.n | ||
| 464 | if args[i] != nil and type(args[i]) == "number" | ||
| 465 | sum += args[i] | ||
| 466 | sum | ||
| 467 | |||
| 468 | process 1, nil, 3, nil, 5 | ||
| 469 | ``` | ||
| 470 | <YueDisplay> | ||
| 471 | |||
| 472 | ```yue | ||
| 473 | f = (...t) -> | ||
| 474 | print "argument count:", t.n | ||
| 475 | print "table length:", #t | ||
| 476 | for i = 1, t.n | ||
| 477 | print t[i] | ||
| 478 | |||
| 479 | f 1, 2, 3 | ||
| 480 | f "a", "b", "c", "d" | ||
| 481 | f! | ||
| 482 | |||
| 483 | -- Handling cases with nil values | ||
| 484 | process = (...args) -> | ||
| 485 | sum = 0 | ||
| 486 | for i = 1, args.n | ||
| 487 | if args[i] != nil and type(args[i]) == "number" | ||
| 488 | sum += args[i] | ||
| 489 | sum | ||
| 490 | |||
| 491 | process 1, nil, 3, nil, 5 | ||
| 492 | ``` | ||
| 493 | |||
| 494 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/doc/function-stubs.md b/doc/docs/pt-br/doc/function-stubs.md new file mode 100644 index 0000000..57a8b0c --- /dev/null +++ b/doc/docs/pt-br/doc/function-stubs.md | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | # Function Stubs | ||
| 2 | |||
| 3 | It is common to pass a function from an object around as a value, for example, passing an instance method into a function as a callback. If the function expects the object it is operating on as the first argument then you must somehow bundle that object with the function so it can be called properly. | ||
| 4 | |||
| 5 | The function stub syntax is a shorthand for creating a new closure function that bundles both the object and function. This new function calls the wrapped function in the correct context of the object. | ||
| 6 | |||
| 7 | Its syntax is the same as calling an instance method with the \ operator but with no argument list provided. | ||
| 8 | |||
| 9 | ```yuescript | ||
| 10 | my_object = { | ||
| 11 | value: 1000 | ||
| 12 | write: => print "the value:", @value | ||
| 13 | } | ||
| 14 | |||
| 15 | run_callback = (func) -> | ||
| 16 | print "running callback..." | ||
| 17 | func! | ||
| 18 | |||
| 19 | -- this will not work: | ||
| 20 | -- the function has to no reference to my_object | ||
| 21 | run_callback my_object.write | ||
| 22 | |||
| 23 | -- function stub syntax | ||
| 24 | -- lets us bundle the object into a new function | ||
| 25 | run_callback my_object\write | ||
| 26 | ``` | ||
| 27 | <YueDisplay> | ||
| 28 | |||
| 29 | ```yue | ||
| 30 | my_object = { | ||
| 31 | value: 1000 | ||
| 32 | write: => print "the value:", @value | ||
| 33 | } | ||
| 34 | |||
| 35 | run_callback = (func) -> | ||
| 36 | print "running callback..." | ||
| 37 | func! | ||
| 38 | |||
| 39 | -- this will not work: | ||
| 40 | -- the function has to no reference to my_object | ||
| 41 | run_callback my_object.write | ||
| 42 | |||
| 43 | -- function stub syntax | ||
| 44 | -- lets us bundle the object into a new function | ||
| 45 | run_callback my_object\write | ||
| 46 | ``` | ||
| 47 | |||
| 48 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/doc/if-assignment.md b/doc/docs/pt-br/doc/if-assignment.md new file mode 100644 index 0000000..02984e8 --- /dev/null +++ b/doc/docs/pt-br/doc/if-assignment.md | |||
| @@ -0,0 +1,72 @@ | |||
| 1 | # If Assignment | ||
| 2 | |||
| 3 | `if` and `elseif` blocks can take an assignment in place of a conditional expression. Upon evaluating the conditional, the assignment will take place and the value that was assigned to will be used as the conditional expression. The assigned variable is only in scope for the body of the conditional, meaning it is never available if the value is not truthy. And you have to use "the walrus operator" `:=` instead of `=` to do assignment. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | if user := database.find_user "moon" | ||
| 7 | print user.name | ||
| 8 | ``` | ||
| 9 | <YueDisplay> | ||
| 10 | |||
| 11 | ```yue | ||
| 12 | if user := database.find_user "moon" | ||
| 13 | print user.name | ||
| 14 | ``` | ||
| 15 | |||
| 16 | </YueDisplay> | ||
| 17 | |||
| 18 | ```yuescript | ||
| 19 | if hello := os.getenv "hello" | ||
| 20 | print "You have hello", hello | ||
| 21 | elseif world := os.getenv "world" | ||
| 22 | print "you have world", world | ||
| 23 | else | ||
| 24 | print "nothing :(" | ||
| 25 | ``` | ||
| 26 | <YueDisplay> | ||
| 27 | |||
| 28 | ```yue | ||
| 29 | if hello := os.getenv "hello" | ||
| 30 | print "You have hello", hello | ||
| 31 | elseif world := os.getenv "world" | ||
| 32 | print "you have world", world | ||
| 33 | else | ||
| 34 | print "nothing :(" | ||
| 35 | ``` | ||
| 36 | |||
| 37 | </YueDisplay> | ||
| 38 | |||
| 39 | If assignment with multiple return values. Only the first value is getting checked, other values are scoped. | ||
| 40 | ```yuescript | ||
| 41 | if success, result := pcall -> "get result without problems" | ||
| 42 | print result -- variable result is scoped | ||
| 43 | print "OK" | ||
| 44 | ``` | ||
| 45 | <YueDisplay> | ||
| 46 | |||
| 47 | ```yue | ||
| 48 | if success, result := pcall -> "get result without problems" | ||
| 49 | print result -- variable result is scoped | ||
| 50 | print "OK" | ||
| 51 | ``` | ||
| 52 | |||
| 53 | </YueDisplay> | ||
| 54 | |||
| 55 | ## While Assignment | ||
| 56 | |||
| 57 | You can also use if assignment in a while loop to get the value as the loop condition. | ||
| 58 | |||
| 59 | ```yuescript | ||
| 60 | while byte := stream\read_one! | ||
| 61 | -- do something with the byte | ||
| 62 | print byte | ||
| 63 | ``` | ||
| 64 | <YueDisplay> | ||
| 65 | |||
| 66 | ```yue | ||
| 67 | while byte := stream\read_one! | ||
| 68 | -- do something with the byte | ||
| 69 | print byte | ||
| 70 | ``` | ||
| 71 | |||
| 72 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/doc/index.md b/doc/docs/pt-br/doc/index.md new file mode 100755 index 0000000..bf2d15a --- /dev/null +++ b/doc/docs/pt-br/doc/index.md | |||
| @@ -0,0 +1,11 @@ | |||
| 1 | --- | ||
| 2 | title: Referência | ||
| 3 | --- | ||
| 4 | |||
| 5 | # Documentação do YueScript | ||
| 6 | |||
| 7 | <img src="/image/yuescript.svg" width="250px" height="250px" alt="logo" style="padding-top: 3em; padding-bottom: 2em;"/> | ||
| 8 | |||
| 9 | Bem-vindo à documentação oficial do <b>YueScript</b>!<br/> | ||
| 10 | Aqui você encontra recursos da linguagem, uso, exemplos de referência e materiais úteis.<br/> | ||
| 11 | Selecione um capítulo na barra lateral para começar a aprender YueScript. | ||
diff --git a/doc/docs/pt-br/doc/installation.md b/doc/docs/pt-br/doc/installation.md new file mode 100644 index 0000000..a93ddfd --- /dev/null +++ b/doc/docs/pt-br/doc/installation.md | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | # Installation | ||
| 2 | |||
| 3 | ## Lua Module | ||
| 4 | |||
| 5 | Install [luarocks](https://luarocks.org), a package manager for Lua modules. Then install it as a Lua module and executable with: | ||
| 6 | |||
| 7 | ```shell | ||
| 8 | luarocks install yuescript | ||
| 9 | ``` | ||
| 10 | |||
| 11 | Or you can build `yue.so` file with: | ||
| 12 | |||
| 13 | ```shell | ||
| 14 | make shared LUAI=/usr/local/include/lua LUAL=/usr/local/lib/lua | ||
| 15 | ``` | ||
| 16 | |||
| 17 | Then get the binary file from path **bin/shared/yue.so**. | ||
| 18 | |||
| 19 | ## Build Binary Tool | ||
| 20 | |||
| 21 | Clone this repo, then build and install executable with: | ||
| 22 | |||
| 23 | ```shell | ||
| 24 | make install | ||
| 25 | ``` | ||
| 26 | |||
| 27 | Build YueScript tool without macro feature: | ||
| 28 | |||
| 29 | ```shell | ||
| 30 | make install NO_MACRO=true | ||
| 31 | ``` | ||
| 32 | |||
| 33 | Build YueScript tool without built-in Lua binary: | ||
| 34 | |||
| 35 | ```shell | ||
| 36 | make install NO_LUA=true | ||
| 37 | ``` | ||
| 38 | |||
| 39 | ## Download Precompiled Binary | ||
| 40 | |||
| 41 | You can download precompiled binary files, including binary executable files compatible with different Lua versions and library files. | ||
| 42 | |||
| 43 | Download precompiled binary files from [here](https://github.com/IppClub/YueScript/releases). | ||
diff --git a/doc/docs/pt-br/doc/introduction.md b/doc/docs/pt-br/doc/introduction.md new file mode 100644 index 0000000..a9a9389 --- /dev/null +++ b/doc/docs/pt-br/doc/introduction.md | |||
| @@ -0,0 +1,105 @@ | |||
| 1 | # Introduction | ||
| 2 | |||
| 3 | YueScript is a dynamic language that compiles to Lua. And it's a [MoonScript](https://github.com/leafo/moonscript) dialect. The codes written in YueScript are expressive and extremely concise. And it is suitable for writing some changing application logic with more maintainable codes and runs in a Lua embeded environment such as games or website servers. | ||
| 4 | |||
| 5 | Yue (月) is the name of moon in Chinese and it's pronounced as [jyɛ]. | ||
| 6 | |||
| 7 | ## An Overview of YueScript | ||
| 8 | |||
| 9 | ```yuescript | ||
| 10 | -- import syntax | ||
| 11 | import p, to_lua from "yue" | ||
| 12 | |||
| 13 | -- object literals | ||
| 14 | inventory = | ||
| 15 | equipment: | ||
| 16 | - "sword" | ||
| 17 | - "shield" | ||
| 18 | items: | ||
| 19 | - name: "potion" | ||
| 20 | count: 10 | ||
| 21 | - name: "bread" | ||
| 22 | count: 3 | ||
| 23 | |||
| 24 | -- list comprehension | ||
| 25 | map = (arr, action) -> | ||
| 26 | [action item for item in *arr] | ||
| 27 | |||
| 28 | filter = (arr, cond) -> | ||
| 29 | [item for item in *arr when cond item] | ||
| 30 | |||
| 31 | reduce = (arr, init, action): init -> | ||
| 32 | init = action init, item for item in *arr | ||
| 33 | |||
| 34 | -- pipe operator | ||
| 35 | [1, 2, 3] | ||
| 36 | |> map (x) -> x * 2 | ||
| 37 | |> filter (x) -> x > 4 | ||
| 38 | |> reduce 0, (a, b) -> a + b | ||
| 39 | |||
| 40 | |||
| 41 | -- metatable manipulation | ||
| 42 | apple = | ||
| 43 | size: 15 | ||
| 44 | <index>: | ||
| 45 | color: 0x00ffff | ||
| 46 | |||
| 47 | with apple | ||
| 48 | p .size, .color, .<index> if .<>? | ||
| 49 | |||
| 50 | -- js-like export syntax | ||
| 51 | export 🌛 = "月之脚本" | ||
| 52 | ``` | ||
| 53 | |||
| 54 | <YueDisplay> | ||
| 55 | |||
| 56 | ```yue | ||
| 57 | -- import syntax | ||
| 58 | import p, to_lua from "yue" | ||
| 59 | |||
| 60 | -- object literals | ||
| 61 | inventory = | ||
| 62 | equipment: | ||
| 63 | - "sword" | ||
| 64 | - "shield" | ||
| 65 | items: | ||
| 66 | - name: "potion" | ||
| 67 | count: 10 | ||
| 68 | - name: "bread" | ||
| 69 | count: 3 | ||
| 70 | |||
| 71 | -- list comprehension | ||
| 72 | map = (arr, action) -> | ||
| 73 | [action item for item in *arr] | ||
| 74 | |||
| 75 | filter = (arr, cond) -> | ||
| 76 | [item for item in *arr when cond item] | ||
| 77 | |||
| 78 | reduce = (arr, init, action): init -> | ||
| 79 | init = action init, item for item in *arr | ||
| 80 | |||
| 81 | -- pipe operator | ||
| 82 | [1, 2, 3] | ||
| 83 | |> map (x) -> x * 2 | ||
| 84 | |> filter (x) -> x > 4 | ||
| 85 | |> reduce 0, (a, b) -> a + b | ||
| 86 | |||
| 87 | |||
| 88 | -- metatable manipulation | ||
| 89 | apple = | ||
| 90 | size: 15 | ||
| 91 | <index>: | ||
| 92 | color: 0x00ffff | ||
| 93 | |||
| 94 | with apple | ||
| 95 | p .size, .color, .<index> if .<>? | ||
| 96 | |||
| 97 | -- js-like export syntax | ||
| 98 | export 🌛 = "月之脚本" | ||
| 99 | ``` | ||
| 100 | |||
| 101 | </YueDisplay> | ||
| 102 | |||
| 103 | ## About Dora SSR | ||
| 104 | |||
| 105 | YueScript is being developed and maintained alongside the open-source game engine [Dora SSR](https://github.com/Dora-SSR/Dora-SSR). It has been used to create engine tools, game demos and prototypes, validating its capabilities in real-world scenarios while enhancing the Dora SSR development experience. \ No newline at end of file | ||
diff --git a/doc/docs/pt-br/doc/licence-mit.md b/doc/docs/pt-br/doc/licence-mit.md new file mode 100644 index 0000000..f1d5d60 --- /dev/null +++ b/doc/docs/pt-br/doc/licence-mit.md | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | # License: MIT | ||
| 2 | |||
| 3 | Copyright (c) 2017-2026 Li Jin \<dragon-fly@qq.com\> | ||
| 4 | |||
| 5 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 6 | of this software and associated documentation files (the "Software"), to deal | ||
| 7 | in the Software without restriction, including without limitation the rights | ||
| 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 9 | copies of the Software, and to permit persons to whom the Software is | ||
| 10 | furnished to do so, subject to the following conditions: | ||
| 11 | |||
| 12 | The above copyright notice and this permission notice shall be included in all | ||
| 13 | copies or substantial portions of the Software. | ||
| 14 | |||
| 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 21 | SOFTWARE. | ||
| 22 | |||
| 23 | <CompilerModal /> | ||
diff --git a/doc/docs/pt-br/doc/line-decorators.md b/doc/docs/pt-br/doc/line-decorators.md new file mode 100644 index 0000000..292bc77 --- /dev/null +++ b/doc/docs/pt-br/doc/line-decorators.md | |||
| @@ -0,0 +1,44 @@ | |||
| 1 | # Line Decorators | ||
| 2 | |||
| 3 | For convenience, the for loop and if statement can be applied to single statements at the end of the line: | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | print "hello world" if name == "Rob" | ||
| 7 | ``` | ||
| 8 | <YueDisplay> | ||
| 9 | |||
| 10 | ```yue | ||
| 11 | print "hello world" if name == "Rob" | ||
| 12 | ``` | ||
| 13 | |||
| 14 | </YueDisplay> | ||
| 15 | |||
| 16 | And with basic loops: | ||
| 17 | |||
| 18 | ```yuescript | ||
| 19 | print "item: ", item for item in *items | ||
| 20 | ``` | ||
| 21 | <YueDisplay> | ||
| 22 | |||
| 23 | ```yue | ||
| 24 | print "item: ", item for item in *items | ||
| 25 | ``` | ||
| 26 | |||
| 27 | </YueDisplay> | ||
| 28 | |||
| 29 | And with while loops: | ||
| 30 | |||
| 31 | ```yuescript | ||
| 32 | game\update! while game\isRunning! | ||
| 33 | |||
| 34 | reader\parse_line! until reader\eof! | ||
| 35 | ``` | ||
| 36 | <YueDisplay> | ||
| 37 | |||
| 38 | ```yue | ||
| 39 | game\update! while game\isRunning! | ||
| 40 | |||
| 41 | reader\parse_line! until reader\eof! | ||
| 42 | ``` | ||
| 43 | |||
| 44 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/doc/literals.md b/doc/docs/pt-br/doc/literals.md new file mode 100644 index 0000000..e097c62 --- /dev/null +++ b/doc/docs/pt-br/doc/literals.md | |||
| @@ -0,0 +1,110 @@ | |||
| 1 | # Literals | ||
| 2 | |||
| 3 | All of the primitive literals in Lua can be used. This applies to numbers, strings, booleans, and **nil**. | ||
| 4 | |||
| 5 | Unlike Lua, Line breaks are allowed inside of single and double quote strings without an escape sequence: | ||
| 6 | |||
| 7 | ```yuescript | ||
| 8 | some_string = "Here is a string | ||
| 9 | that has a line break in it." | ||
| 10 | |||
| 11 | -- You can mix expressions into string literals using #{} syntax. | ||
| 12 | -- String interpolation is only available in double quoted strings. | ||
| 13 | print "I am #{math.random! * 100}% sure." | ||
| 14 | ``` | ||
| 15 | <YueDisplay> | ||
| 16 | |||
| 17 | ```yue | ||
| 18 | some_string = "Here is a string | ||
| 19 | that has a line break in it." | ||
| 20 | |||
| 21 | -- You can mix expressions into string literals using #{} syntax. | ||
| 22 | -- String interpolation is only available in double quoted strings. | ||
| 23 | print "I am #{math.random! * 100}% sure." | ||
| 24 | ``` | ||
| 25 | |||
| 26 | </YueDisplay> | ||
| 27 | |||
| 28 | ## Number Literals | ||
| 29 | |||
| 30 | You can use underscores in a number literal to increase readability. | ||
| 31 | |||
| 32 | ```yuescript | ||
| 33 | integer = 1_000_000 | ||
| 34 | hex = 0xEF_BB_BF | ||
| 35 | binary = 0B10011 | ||
| 36 | ``` | ||
| 37 | <YueDisplay> | ||
| 38 | |||
| 39 | ```yue | ||
| 40 | integer = 1_000_000 | ||
| 41 | hex = 0xEF_BB_BF | ||
| 42 | binary = 0B10011 | ||
| 43 | ``` | ||
| 44 | |||
| 45 | </YueDisplay> | ||
| 46 | |||
| 47 | ## YAML Multiline String | ||
| 48 | |||
| 49 | The `|` prefix introduces a YAML-style multiline string literal: | ||
| 50 | |||
| 51 | ```yuescript | ||
| 52 | str = | | ||
| 53 | key: value | ||
| 54 | list: | ||
| 55 | - item1 | ||
| 56 | - #{expr} | ||
| 57 | ``` | ||
| 58 | <YueDisplay> | ||
| 59 | |||
| 60 | ```yue | ||
| 61 | str = | | ||
| 62 | key: value | ||
| 63 | list: | ||
| 64 | - item1 | ||
| 65 | - #{expr} | ||
| 66 | ``` | ||
| 67 | |||
| 68 | </YueDisplay> | ||
| 69 | |||
| 70 | This allows writing structured multiline text conveniently. All line breaks and indentation are preserved relative to the first non-empty line, and expressions inside `#{...}` are interpolated automatically as `tostring(expr)`. | ||
| 71 | |||
| 72 | YAML Multiline String automatically detects the common leading whitespace prefix (minimum indentation across all non-empty lines) and removes it from all lines. This makes it easy to indent your code visually without affecting the resulting string content. | ||
| 73 | |||
| 74 | ```yuescript | ||
| 75 | fn = -> | ||
| 76 | str = | | ||
| 77 | foo: | ||
| 78 | bar: baz | ||
| 79 | return str | ||
| 80 | ``` | ||
| 81 | <YueDisplay> | ||
| 82 | |||
| 83 | ```yue | ||
| 84 | fn = -> | ||
| 85 | str = | | ||
| 86 | foo: | ||
| 87 | bar: baz | ||
| 88 | return str | ||
| 89 | ``` | ||
| 90 | |||
| 91 | </YueDisplay> | ||
| 92 | |||
| 93 | Internal indentation is preserved relative to the removed common prefix, allowing clean nested structures. | ||
| 94 | |||
| 95 | All special characters like quotes (`"`) and backslashes (`\`) in the YAMLMultiline block are automatically escaped so that the generated Lua string is syntactically valid and behaves as expected. | ||
| 96 | |||
| 97 | ```yuescript | ||
| 98 | str = | | ||
| 99 | path: "C:\Program Files\App" | ||
| 100 | note: 'He said: "#{Hello}!"' | ||
| 101 | ``` | ||
| 102 | <YueDisplay> | ||
| 103 | |||
| 104 | ```yue | ||
| 105 | str = | | ||
| 106 | path: "C:\Program Files\App" | ||
| 107 | note: 'He said: "#{Hello}!"' | ||
| 108 | ``` | ||
| 109 | |||
| 110 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/doc/macro.md b/doc/docs/pt-br/doc/macro.md new file mode 100644 index 0000000..6d194c3 --- /dev/null +++ b/doc/docs/pt-br/doc/macro.md | |||
| @@ -0,0 +1,275 @@ | |||
| 1 | # Macro | ||
| 2 | |||
| 3 | ## Common Usage | ||
| 4 | |||
| 5 | Macro function is used for evaluating a string in the compile time and insert the generated codes into final compilation. | ||
| 6 | |||
| 7 | ```yuescript | ||
| 8 | macro PI2 = -> math.pi * 2 | ||
| 9 | area = $PI2 * 5 | ||
| 10 | |||
| 11 | macro HELLO = -> "'hello world'" | ||
| 12 | print $HELLO | ||
| 13 | |||
| 14 | macro config = (debugging) -> | ||
| 15 | global debugMode = debugging == "true" | ||
| 16 | "" | ||
| 17 | |||
| 18 | macro asserts = (cond) -> | ||
| 19 | debugMode and "assert #{cond}" or "" | ||
| 20 | |||
| 21 | macro assert = (cond) -> | ||
| 22 | debugMode and "assert #{cond}" or "#{cond}" | ||
| 23 | |||
| 24 | $config true | ||
| 25 | $asserts item ~= nil | ||
| 26 | |||
| 27 | $config false | ||
| 28 | value = $assert item | ||
| 29 | |||
| 30 | -- the passed expressions are treated as strings | ||
| 31 | macro and = (...) -> "#{ table.concat {...}, ' and ' }" | ||
| 32 | if $and f1!, f2!, f3! | ||
| 33 | print "OK" | ||
| 34 | ``` | ||
| 35 | <YueDisplay> | ||
| 36 | |||
| 37 | ```yue | ||
| 38 | macro PI2 = -> math.pi * 2 | ||
| 39 | area = $PI2 * 5 | ||
| 40 | |||
| 41 | macro HELLO = -> "'hello world'" | ||
| 42 | print $HELLO | ||
| 43 | |||
| 44 | macro config = (debugging) -> | ||
| 45 | global debugMode = debugging == "true" | ||
| 46 | "" | ||
| 47 | |||
| 48 | macro asserts = (cond) -> | ||
| 49 | debugMode and "assert #{cond}" or "" | ||
| 50 | |||
| 51 | macro assert = (cond) -> | ||
| 52 | debugMode and "assert #{cond}" or "#{cond}" | ||
| 53 | |||
| 54 | $config true | ||
| 55 | $asserts item ~= nil | ||
| 56 | |||
| 57 | $config false | ||
| 58 | value = $assert item | ||
| 59 | |||
| 60 | -- the passed expressions are treated as strings | ||
| 61 | macro and = (...) -> "#{ table.concat {...}, ' and ' }" | ||
| 62 | if $and f1!, f2!, f3! | ||
| 63 | print "OK" | ||
| 64 | ``` | ||
| 65 | |||
| 66 | </YueDisplay> | ||
| 67 | |||
| 68 | ## Insert Raw Codes | ||
| 69 | |||
| 70 | A macro function can either return a YueScript string or a config table containing Lua codes. | ||
| 71 | ```yuescript | ||
| 72 | macro yueFunc = (var) -> "local #{var} = ->" | ||
| 73 | $yueFunc funcA | ||
| 74 | funcA = -> "fail to assign to the Yue macro defined variable" | ||
| 75 | |||
| 76 | macro luaFunc = (var) -> { | ||
| 77 | code: "local function #{var}() end" | ||
| 78 | type: "lua" | ||
| 79 | } | ||
| 80 | $luaFunc funcB | ||
| 81 | funcB = -> "fail to assign to the Lua macro defined variable" | ||
| 82 | |||
| 83 | macro lua = (code) -> { | ||
| 84 | :code | ||
| 85 | type: "lua" | ||
| 86 | } | ||
| 87 | |||
| 88 | -- the raw string leading and ending symbols are auto trimed | ||
| 89 | $lua[==[ | ||
| 90 | -- raw Lua codes insertion | ||
| 91 | if cond then | ||
| 92 | print("output") | ||
| 93 | end | ||
| 94 | ]==] | ||
| 95 | ``` | ||
| 96 | <YueDisplay> | ||
| 97 | |||
| 98 | ```yue | ||
| 99 | macro yueFunc = (var) -> "local #{var} = ->" | ||
| 100 | $yueFunc funcA | ||
| 101 | funcA = -> "fail to assign to the Yue macro defined variable" | ||
| 102 | |||
| 103 | macro luaFunc = (var) -> { | ||
| 104 | code: "local function #{var}() end" | ||
| 105 | type: "lua" | ||
| 106 | } | ||
| 107 | $luaFunc funcB | ||
| 108 | funcB = -> "fail to assign to the Lua macro defined variable" | ||
| 109 | |||
| 110 | macro lua = (code) -> { | ||
| 111 | :code | ||
| 112 | type: "lua" | ||
| 113 | } | ||
| 114 | |||
| 115 | -- the raw string leading and ending symbols are auto trimed | ||
| 116 | $lua[==[ | ||
| 117 | -- raw Lua codes insertion | ||
| 118 | if cond then | ||
| 119 | print("output") | ||
| 120 | end | ||
| 121 | ]==] | ||
| 122 | ``` | ||
| 123 | |||
| 124 | </YueDisplay> | ||
| 125 | |||
| 126 | ## Export Macro | ||
| 127 | |||
| 128 | Macro functions can be exported from a module and get imported in another module. You have to put export macro functions in a single file to be used, and only macro definition, macro importing and macro expansion in place can be put into the macro exporting module. | ||
| 129 | ```yuescript | ||
| 130 | -- file: utils.yue | ||
| 131 | export macro map = (items, action) -> "[#{action} for _ in *#{items}]" | ||
| 132 | export macro filter = (items, action) -> "[_ for _ in *#{items} when #{action}]" | ||
| 133 | export macro foreach = (items, action) -> "for _ in *#{items} | ||
| 134 | #{action}" | ||
| 135 | |||
| 136 | -- file main.yue | ||
| 137 | import "utils" as { | ||
| 138 | $, -- symbol to import all macros | ||
| 139 | $foreach: $each -- rename macro $foreach to $each | ||
| 140 | } | ||
| 141 | [1, 2, 3] |> $map(_ * 2) |> $filter(_ > 4) |> $each print _ | ||
| 142 | ``` | ||
| 143 | <YueDisplay> | ||
| 144 | |||
| 145 | ```yue | ||
| 146 | -- file: utils.yue | ||
| 147 | export macro map = (items, action) -> "[#{action} for _ in *#{items}]" | ||
| 148 | export macro filter = (items, action) -> "[_ for _ in *#{items} when #{action}]" | ||
| 149 | export macro foreach = (items, action) -> "for _ in *#{items} | ||
| 150 | #{action}" | ||
| 151 | |||
| 152 | -- file main.yue | ||
| 153 | -- import function is not available in browser, try it in a real environment | ||
| 154 | --[[ | ||
| 155 | import "utils" as { | ||
| 156 | $, -- symbol to import all macros | ||
| 157 | $foreach: $each -- rename macro $foreach to $each | ||
| 158 | } | ||
| 159 | [1, 2, 3] |> $map(_ * 2) |> $filter(_ > 4) |> $each print _ | ||
| 160 | ]] | ||
| 161 | ``` | ||
| 162 | |||
| 163 | </YueDisplay> | ||
| 164 | |||
| 165 | ## Builtin Macro | ||
| 166 | |||
| 167 | There are some builtin macros but you can override them by declaring macros with the same names. | ||
| 168 | ```yuescript | ||
| 169 | print $FILE -- get string of current module name | ||
| 170 | print $LINE -- get number 2 | ||
| 171 | ``` | ||
| 172 | <YueDisplay> | ||
| 173 | |||
| 174 | ```yue | ||
| 175 | print $FILE -- get string of current module name | ||
| 176 | print $LINE -- get number 2 | ||
| 177 | ``` | ||
| 178 | |||
| 179 | </YueDisplay> | ||
| 180 | |||
| 181 | ## Generating Macros with Macros | ||
| 182 | |||
| 183 | In YueScript, macro functions allow you to generate code at compile time. By nesting macro functions, you can create more complex generation patterns. This feature enables you to define a macro function that generates another macro function, allowing for more dynamic code generation. | ||
| 184 | |||
| 185 | ```yuescript | ||
| 186 | macro Enum = (...) -> | ||
| 187 | items = {...} | ||
| 188 | itemSet = {item, true for item in *items} | ||
| 189 | (item) -> | ||
| 190 | error "got \"#{item}\", expecting one of #{table.concat items, ', '}" unless itemSet[item] | ||
| 191 | "\"#{item}\"" | ||
| 192 | |||
| 193 | macro BodyType = $Enum( | ||
| 194 | Static | ||
| 195 | Dynamic | ||
| 196 | Kinematic | ||
| 197 | ) | ||
| 198 | |||
| 199 | print "Valid enum type:", $BodyType Static | ||
| 200 | -- print "Compilation error with enum type:", $BodyType Unknown | ||
| 201 | ``` | ||
| 202 | |||
| 203 | <YueDisplay> | ||
| 204 | |||
| 205 | ```yue | ||
| 206 | macro Enum = (...) -> | ||
| 207 | items = {...} | ||
| 208 | itemSet = {item, true for item in *items} | ||
| 209 | (item) -> | ||
| 210 | error "got \"#{item}\", expecting one of #{table.concat items, ', '}" unless itemSet[item] | ||
| 211 | "\"#{item}\"" | ||
| 212 | |||
| 213 | macro BodyType = $Enum( | ||
| 214 | Static | ||
| 215 | Dynamic | ||
| 216 | Kinematic | ||
| 217 | ) | ||
| 218 | |||
| 219 | print "Valid enum type:", $BodyType Static | ||
| 220 | -- print "Compilation error with enum type:", $BodyType Unknown | ||
| 221 | ``` | ||
| 222 | |||
| 223 | </YueDisplay> | ||
| 224 | |||
| 225 | ## Argument Validation | ||
| 226 | |||
| 227 | You can declare the expected AST node types in the argument list, and check whether the incoming macro arguments meet the expectations at compile time. | ||
| 228 | |||
| 229 | ```yuescript | ||
| 230 | macro printNumAndStr = (num `Num, str `String) -> | | ||
| 231 | print( | ||
| 232 | #{num} | ||
| 233 | #{str} | ||
| 234 | ) | ||
| 235 | |||
| 236 | $printNumAndStr 123, "hello" | ||
| 237 | ``` | ||
| 238 | <YueDisplay> | ||
| 239 | |||
| 240 | ```yue | ||
| 241 | macro printNumAndStr = (num `Num, str `String) -> | | ||
| 242 | print( | ||
| 243 | #{num} | ||
| 244 | #{str} | ||
| 245 | ) | ||
| 246 | |||
| 247 | $printNumAndStr 123, "hello" | ||
| 248 | ``` | ||
| 249 | |||
| 250 | </YueDisplay> | ||
| 251 | |||
| 252 | If you need more flexible argument checking, you can use the built-in `$is_ast` macro function to manually check at the appropriate place. | ||
| 253 | |||
| 254 | ```yuescript | ||
| 255 | macro printNumAndStr = (num, str) -> | ||
| 256 | error "expected Num as first argument" unless $is_ast Num, num | ||
| 257 | error "expected String as second argument" unless $is_ast String, str | ||
| 258 | "print(#{num}, #{str})" | ||
| 259 | |||
| 260 | $printNumAndStr 123, "hello" | ||
| 261 | ``` | ||
| 262 | <YueDisplay> | ||
| 263 | |||
| 264 | ```yue | ||
| 265 | macro printNumAndStr = (num, str) -> | ||
| 266 | error "expected Num as first argument" unless $is_ast Num, num | ||
| 267 | error "expected String as second argument" unless $is_ast String, str | ||
| 268 | "print(#{num}, #{str})" | ||
| 269 | |||
| 270 | $printNumAndStr 123, "hello" | ||
| 271 | ``` | ||
| 272 | |||
| 273 | </YueDisplay> | ||
| 274 | |||
| 275 | For more details about available AST nodes, please refer to the uppercased definitions in [yue_parser.cpp](https://github.com/IppClub/YueScript/blob/main/src/yuescript/yue_parser.cpp). | ||
diff --git a/doc/docs/pt-br/doc/module.md b/doc/docs/pt-br/doc/module.md new file mode 100644 index 0000000..c955092 --- /dev/null +++ b/doc/docs/pt-br/doc/module.md | |||
| @@ -0,0 +1,245 @@ | |||
| 1 | # Module | ||
| 2 | |||
| 3 | ## Import | ||
| 4 | |||
| 5 | 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. | ||
| 6 | |||
| 7 | ```yuescript | ||
| 8 | -- used as table destructuring | ||
| 9 | do | ||
| 10 | import insert, concat from table | ||
| 11 | -- report error when assigning to insert, concat | ||
| 12 | import C, Ct, Cmt from require "lpeg" | ||
| 13 | -- shortcut for implicit requiring | ||
| 14 | import x, y, z from 'mymodule' | ||
| 15 | -- import with Python style | ||
| 16 | from 'module' import a, b, c | ||
| 17 | |||
| 18 | -- shortcut for requring a module | ||
| 19 | do | ||
| 20 | import 'module' | ||
| 21 | import 'module_x' | ||
| 22 | import "d-a-s-h-e-s" | ||
| 23 | import "module.part" | ||
| 24 | |||
| 25 | -- requring module with aliasing or table destructuring | ||
| 26 | do | ||
| 27 | import "player" as PlayerModule | ||
| 28 | import "lpeg" as :C, :Ct, :Cmt | ||
| 29 | import "export" as {one, two, Something:{umm:{ch}}} | ||
| 30 | ``` | ||
| 31 | <YueDisplay> | ||
| 32 | |||
| 33 | ```yue | ||
| 34 | -- used as table destructuring | ||
| 35 | do | ||
| 36 | import insert, concat from table | ||
| 37 | -- report error when assigning to insert, concat | ||
| 38 | import C, Ct, Cmt from require "lpeg" | ||
| 39 | -- shortcut for implicit requiring | ||
| 40 | import x, y, z from 'mymodule' | ||
| 41 | -- import with Python style | ||
| 42 | from 'module' import a, b, c | ||
| 43 | |||
| 44 | -- shortcut for requring a module | ||
| 45 | do | ||
| 46 | import 'module' | ||
| 47 | import 'module_x' | ||
| 48 | import "d-a-s-h-e-s" | ||
| 49 | import "module.part" | ||
| 50 | |||
| 51 | -- requring module with aliasing or table destructuring | ||
| 52 | do | ||
| 53 | import "player" as PlayerModule | ||
| 54 | import "lpeg" as :C, :Ct, :Cmt | ||
| 55 | import "export" as {one, two, Something:{umm:{ch}}} | ||
| 56 | ``` | ||
| 57 | |||
| 58 | </YueDisplay> | ||
| 59 | |||
| 60 | ## Import Global | ||
| 61 | |||
| 62 | 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. | ||
| 63 | |||
| 64 | ```yuescript | ||
| 65 | do | ||
| 66 | import tostring | ||
| 67 | import table.concat | ||
| 68 | print concat ["a", tostring 1] | ||
| 69 | ``` | ||
| 70 | <YueDisplay> | ||
| 71 | |||
| 72 | ```yue | ||
| 73 | do | ||
| 74 | import tostring | ||
| 75 | import table.concat | ||
| 76 | print concat ["a", tostring 1] | ||
| 77 | ``` | ||
| 78 | |||
| 79 | </YueDisplay> | ||
| 80 | |||
| 81 | ### Automatic Global Variable Import | ||
| 82 | |||
| 83 | 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. | ||
| 84 | |||
| 85 | Names that are explicitly declared as globals in the same scope will not be imported, so you can still assign to them. | ||
| 86 | |||
| 87 | ```yuescript | ||
| 88 | do | ||
| 89 | import global | ||
| 90 | print "hello" | ||
| 91 | math.random 3 | ||
| 92 | -- print = nil -- error: imported globals are const | ||
| 93 | |||
| 94 | do | ||
| 95 | -- explicit global variable will not be imported | ||
| 96 | import global | ||
| 97 | global FLAG | ||
| 98 | print FLAG | ||
| 99 | FLAG = 123 | ||
| 100 | ``` | ||
| 101 | <YueDisplay> | ||
| 102 | |||
| 103 | ```yue | ||
| 104 | do | ||
| 105 | import global | ||
| 106 | print "hello" | ||
| 107 | math.random 3 | ||
| 108 | -- print = nil -- error: imported globals are const | ||
| 109 | |||
| 110 | do | ||
| 111 | -- explicit global variable will not be imported | ||
| 112 | import global | ||
| 113 | global FLAG | ||
| 114 | print FLAG | ||
| 115 | FLAG = 123 | ||
| 116 | ``` | ||
| 117 | |||
| 118 | </YueDisplay> | ||
| 119 | |||
| 120 | ## Export | ||
| 121 | |||
| 122 | The export statement offers a concise way to define modules. | ||
| 123 | |||
| 124 | ### Named Export | ||
| 125 | |||
| 126 | Named export will define a local variable as well as adding a field in the exported table. | ||
| 127 | |||
| 128 | ```yuescript | ||
| 129 | export a, b, c = 1, 2, 3 | ||
| 130 | export cool = "cat" | ||
| 131 | |||
| 132 | export What = if this | ||
| 133 | "abc" | ||
| 134 | else | ||
| 135 | "def" | ||
| 136 | |||
| 137 | export y = -> | ||
| 138 | hallo = 3434 | ||
| 139 | |||
| 140 | export class Something | ||
| 141 | umm: "cool" | ||
| 142 | ``` | ||
| 143 | <YueDisplay> | ||
| 144 | |||
| 145 | ```yue | ||
| 146 | export a, b, c = 1, 2, 3 | ||
| 147 | export cool = "cat" | ||
| 148 | |||
| 149 | export What = if this | ||
| 150 | "abc" | ||
| 151 | else | ||
| 152 | "def" | ||
| 153 | |||
| 154 | export y = -> | ||
| 155 | hallo = 3434 | ||
| 156 | |||
| 157 | export class Something | ||
| 158 | umm: "cool" | ||
| 159 | ``` | ||
| 160 | |||
| 161 | </YueDisplay> | ||
| 162 | |||
| 163 | Doing named export with destructuring. | ||
| 164 | |||
| 165 | ```yuescript | ||
| 166 | export :loadstring, to_lua: tolua = yue | ||
| 167 | export {itemA: {:fieldA = 'default'}} = tb | ||
| 168 | ``` | ||
| 169 | <YueDisplay> | ||
| 170 | |||
| 171 | ```yue | ||
| 172 | export :loadstring, to_lua: tolua = yue | ||
| 173 | export {itemA: {:fieldA = 'default'}} = tb | ||
| 174 | ``` | ||
| 175 | |||
| 176 | </YueDisplay> | ||
| 177 | |||
| 178 | Export named items from module without creating local variables. | ||
| 179 | |||
| 180 | ```yuescript | ||
| 181 | export.itemA = tb | ||
| 182 | export.<index> = items | ||
| 183 | export["a-b-c"] = 123 | ||
| 184 | ``` | ||
| 185 | <YueDisplay> | ||
| 186 | |||
| 187 | ```yue | ||
| 188 | export.itemA = tb | ||
| 189 | export.<index> = items | ||
| 190 | export["a-b-c"] = 123 | ||
| 191 | ``` | ||
| 192 | |||
| 193 | </YueDisplay> | ||
| 194 | |||
| 195 | ### Unnamed Export | ||
| 196 | |||
| 197 | Unnamed export will add the target item into the array part of the exported table. | ||
| 198 | |||
| 199 | ```yuescript | ||
| 200 | d, e, f = 3, 2, 1 | ||
| 201 | export d, e, f | ||
| 202 | |||
| 203 | export if this | ||
| 204 | 123 | ||
| 205 | else | ||
| 206 | 456 | ||
| 207 | |||
| 208 | export with tmp | ||
| 209 | j = 2000 | ||
| 210 | ``` | ||
| 211 | <YueDisplay> | ||
| 212 | |||
| 213 | ```yue | ||
| 214 | d, e, f = 3, 2, 1 | ||
| 215 | export d, e, f | ||
| 216 | |||
| 217 | export if this | ||
| 218 | 123 | ||
| 219 | else | ||
| 220 | 456 | ||
| 221 | |||
| 222 | export with tmp | ||
| 223 | j = 2000 | ||
| 224 | ``` | ||
| 225 | |||
| 226 | </YueDisplay> | ||
| 227 | |||
| 228 | ### Default Export | ||
| 229 | |||
| 230 | Using the **default** keyword in export statement to replace the exported table with any thing. | ||
| 231 | |||
| 232 | ```yuescript | ||
| 233 | export default -> | ||
| 234 | print "hello" | ||
| 235 | 123 | ||
| 236 | ``` | ||
| 237 | <YueDisplay> | ||
| 238 | |||
| 239 | ```yue | ||
| 240 | export default -> | ||
| 241 | print "hello" | ||
| 242 | 123 | ||
| 243 | ``` | ||
| 244 | |||
| 245 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/doc/object-oriented-programming.md b/doc/docs/pt-br/doc/object-oriented-programming.md new file mode 100644 index 0000000..6a8559e --- /dev/null +++ b/doc/docs/pt-br/doc/object-oriented-programming.md | |||
| @@ -0,0 +1,555 @@ | |||
| 1 | # Object Oriented Programming | ||
| 2 | |||
| 3 | In these examples, the generated Lua code may appear overwhelming. It is best to focus on the meaning of the YueScript code at first, then look into the Lua code if you wish to know the implementation details. | ||
| 4 | |||
| 5 | A simple class: | ||
| 6 | |||
| 7 | ```yuescript | ||
| 8 | class Inventory | ||
| 9 | new: => | ||
| 10 | @items = {} | ||
| 11 | |||
| 12 | add_item: (name) => | ||
| 13 | if @items[name] | ||
| 14 | @items[name] += 1 | ||
| 15 | else | ||
| 16 | @items[name] = 1 | ||
| 17 | ``` | ||
| 18 | <YueDisplay> | ||
| 19 | |||
| 20 | ```yue | ||
| 21 | class Inventory | ||
| 22 | new: => | ||
| 23 | @items = {} | ||
| 24 | |||
| 25 | add_item: (name) => | ||
| 26 | if @items[name] | ||
| 27 | @items[name] += 1 | ||
| 28 | else | ||
| 29 | @items[name] = 1 | ||
| 30 | ``` | ||
| 31 | |||
| 32 | </YueDisplay> | ||
| 33 | |||
| 34 | A class is declared with a class statement followed by a table-like declaration where all of the methods and properties are listed. | ||
| 35 | |||
| 36 | The new property is special in that it will become the constructor. | ||
| 37 | |||
| 38 | Notice how all the methods in the class use the fat arrow function syntax. When calling methods on a instance, the instance itself is sent in as the first argument. The fat arrow handles the creation of a self argument. | ||
| 39 | |||
| 40 | The @ prefix on a variable name is shorthand for self.. @items becomes self.items. | ||
| 41 | |||
| 42 | Creating an instance of the class is done by calling the name of the class as a function. | ||
| 43 | |||
| 44 | ```yuescript | ||
| 45 | inv = Inventory! | ||
| 46 | inv\add_item "t-shirt" | ||
| 47 | inv\add_item "pants" | ||
| 48 | ``` | ||
| 49 | <YueDisplay> | ||
| 50 | |||
| 51 | ```yue | ||
| 52 | inv = Inventory! | ||
| 53 | inv\add_item "t-shirt" | ||
| 54 | inv\add_item "pants" | ||
| 55 | ``` | ||
| 56 | |||
| 57 | </YueDisplay> | ||
| 58 | |||
| 59 | Because the instance of the class needs to be sent to the methods when they are called, the \ operator is used. | ||
| 60 | |||
| 61 | All properties of a class are shared among the instances. This is fine for functions, but for other types of objects, undesired results may occur. | ||
| 62 | |||
| 63 | Consider the example below, the clothes property is shared amongst all instances, so modifications to it in one instance will show up in another: | ||
| 64 | |||
| 65 | ```yuescript | ||
| 66 | class Person | ||
| 67 | clothes: [] | ||
| 68 | give_item: (name) => | ||
| 69 | table.insert @clothes, name | ||
| 70 | |||
| 71 | a = Person! | ||
| 72 | b = Person! | ||
| 73 | |||
| 74 | a\give_item "pants" | ||
| 75 | b\give_item "shirt" | ||
| 76 | |||
| 77 | -- will print both pants and shirt | ||
| 78 | print item for item in *a.clothes | ||
| 79 | ``` | ||
| 80 | <YueDisplay> | ||
| 81 | |||
| 82 | ```yue | ||
| 83 | class Person | ||
| 84 | clothes: [] | ||
| 85 | give_item: (name) => | ||
| 86 | table.insert @clothes, name | ||
| 87 | |||
| 88 | a = Person! | ||
| 89 | b = Person! | ||
| 90 | |||
| 91 | a\give_item "pants" | ||
| 92 | b\give_item "shirt" | ||
| 93 | |||
| 94 | -- will print both pants and shirt | ||
| 95 | print item for item in *a.clothes | ||
| 96 | ``` | ||
| 97 | |||
| 98 | </YueDisplay> | ||
| 99 | |||
| 100 | The proper way to avoid this problem is to create the mutable state of the object in the constructor: | ||
| 101 | |||
| 102 | ```yuescript | ||
| 103 | class Person | ||
| 104 | new: => | ||
| 105 | @clothes = [] | ||
| 106 | ``` | ||
| 107 | <YueDisplay> | ||
| 108 | |||
| 109 | ```yue | ||
| 110 | class Person | ||
| 111 | new: => | ||
| 112 | @clothes = [] | ||
| 113 | ``` | ||
| 114 | |||
| 115 | </YueDisplay> | ||
| 116 | |||
| 117 | ## Inheritance | ||
| 118 | |||
| 119 | The extends keyword can be used in a class declaration to inherit the properties and methods from another class. | ||
| 120 | |||
| 121 | ```yuescript | ||
| 122 | class BackPack extends Inventory | ||
| 123 | size: 10 | ||
| 124 | add_item: (name) => | ||
| 125 | if #@items > size then error "backpack is full" | ||
| 126 | super name | ||
| 127 | ``` | ||
| 128 | <YueDisplay> | ||
| 129 | |||
| 130 | ```yue | ||
| 131 | class BackPack extends Inventory | ||
| 132 | size: 10 | ||
| 133 | add_item: (name) => | ||
| 134 | if #@items > size then error "backpack is full" | ||
| 135 | super name | ||
| 136 | ``` | ||
| 137 | |||
| 138 | </YueDisplay> | ||
| 139 | |||
| 140 | Here we extend our Inventory class, and limit the amount of items it can carry. | ||
| 141 | |||
| 142 | In this example, we don't define a constructor on the subclass, so the parent class' constructor is called when we make a new instance. If we did define a constructor then we can use the super method to call the parent constructor. | ||
| 143 | |||
| 144 | Whenever a class inherits from another, it sends a message to the parent class by calling the method __inherited on the parent class if it exists. The function receives two arguments, the class that is being inherited and the child class. | ||
| 145 | |||
| 146 | ```yuescript | ||
| 147 | class Shelf | ||
| 148 | @__inherited: (child) => | ||
| 149 | print @__name, "was inherited by", child.__name | ||
| 150 | |||
| 151 | -- will print: Shelf was inherited by Cupboard | ||
| 152 | class Cupboard extends Shelf | ||
| 153 | ``` | ||
| 154 | <YueDisplay> | ||
| 155 | |||
| 156 | ```yue | ||
| 157 | class Shelf | ||
| 158 | @__inherited: (child) => | ||
| 159 | print @__name, "was inherited by", child.__name | ||
| 160 | |||
| 161 | -- will print: Shelf was inherited by Cupboard | ||
| 162 | class Cupboard extends Shelf | ||
| 163 | ``` | ||
| 164 | |||
| 165 | </YueDisplay> | ||
| 166 | |||
| 167 | ## Super | ||
| 168 | |||
| 169 | **super** is a special keyword that can be used in two different ways: It can be treated as an object, or it can be called like a function. It only has special functionality when inside a class. | ||
| 170 | |||
| 171 | When called as a function, it will call the function of the same name in the parent class. The current self will automatically be passed as the first argument. (As seen in the inheritance example above) | ||
| 172 | |||
| 173 | When super is used as a normal value, it is a reference to the parent class object. | ||
| 174 | |||
| 175 | It can be accessed like any of object in order to retrieve values in the parent class that might have been shadowed by the child class. | ||
| 176 | |||
| 177 | When the \ calling operator is used with super, self is inserted as the first argument instead of the value of super itself. When using . to retrieve a function, the raw function is returned. | ||
| 178 | |||
| 179 | A few examples of using super in different ways: | ||
| 180 | |||
| 181 | ```yuescript | ||
| 182 | class MyClass extends ParentClass | ||
| 183 | a_method: => | ||
| 184 | -- the following have the same effect: | ||
| 185 | super "hello", "world" | ||
| 186 | super\a_method "hello", "world" | ||
| 187 | super.a_method self, "hello", "world" | ||
| 188 | |||
| 189 | -- super as a value is equal to the parent class: | ||
| 190 | assert super == ParentClass | ||
| 191 | ``` | ||
| 192 | <YueDisplay> | ||
| 193 | |||
| 194 | ```yue | ||
| 195 | class MyClass extends ParentClass | ||
| 196 | a_method: => | ||
| 197 | -- the following have the same effect: | ||
| 198 | super "hello", "world" | ||
| 199 | super\a_method "hello", "world" | ||
| 200 | super.a_method self, "hello", "world" | ||
| 201 | |||
| 202 | -- super as a value is equal to the parent class: | ||
| 203 | assert super == ParentClass | ||
| 204 | ``` | ||
| 205 | |||
| 206 | </YueDisplay> | ||
| 207 | |||
| 208 | **super** can also be used on left side of a Function Stub. The only major difference is that instead of the resulting function being bound to the value of super, it is bound to self. | ||
| 209 | |||
| 210 | ## Types | ||
| 211 | |||
| 212 | Every instance of a class carries its type with it. This is stored in the special __class property. This property holds the class object. The class object is what we call to build a new instance. We can also index the class object to retrieve class methods and properties. | ||
| 213 | |||
| 214 | ```yuescript | ||
| 215 | b = BackPack! | ||
| 216 | assert b.__class == BackPack | ||
| 217 | |||
| 218 | print BackPack.size -- prints 10 | ||
| 219 | ``` | ||
| 220 | <YueDisplay> | ||
| 221 | |||
| 222 | ```yue | ||
| 223 | b = BackPack! | ||
| 224 | assert b.__class == BackPack | ||
| 225 | |||
| 226 | print BackPack.size -- prints 10 | ||
| 227 | ``` | ||
| 228 | |||
| 229 | </YueDisplay> | ||
| 230 | |||
| 231 | ## Class Objects | ||
| 232 | |||
| 233 | The class object is what we create when we use a class statement. The class object is stored in a variable of the same name of the class. | ||
| 234 | |||
| 235 | The class object can be called like a function in order to create new instances. That's how we created instances of classes in the examples above. | ||
| 236 | |||
| 237 | A class is made up of two tables. The class table itself, and the base table. The base is used as the metatable for all the instances. All properties listed in the class declaration are placed in the base. | ||
| 238 | |||
| 239 | The class object's metatable reads properties from the base if they don't exist in the class object. This means we can access functions and properties directly from the class. | ||
| 240 | |||
| 241 | It is important to note that assigning to the class object does not assign into the base, so it's not a valid way to add new methods to instances. Instead the base must explicitly be changed. See the __base field below. | ||
| 242 | |||
| 243 | The class object has a couple special properties: | ||
| 244 | |||
| 245 | The name of the class as when it was declared is stored as a string in the __name field of the class object. | ||
| 246 | |||
| 247 | ```yuescript | ||
| 248 | print BackPack.__name -- prints Backpack | ||
| 249 | ``` | ||
| 250 | <YueDisplay> | ||
| 251 | |||
| 252 | ```yue | ||
| 253 | print BackPack.__name -- prints Backpack | ||
| 254 | ``` | ||
| 255 | |||
| 256 | </YueDisplay> | ||
| 257 | |||
| 258 | The base object is stored in __base. We can modify this table to add functionality to instances that have already been created and ones that are yet to be created. | ||
| 259 | |||
| 260 | If the class extends from anything, the parent class object is stored in __parent. | ||
| 261 | |||
| 262 | ## Class Variables | ||
| 263 | |||
| 264 | We can create variables directly in the class object instead of in the base by using @ in the front of the property name in a class declaration. | ||
| 265 | |||
| 266 | ```yuescript | ||
| 267 | class Things | ||
| 268 | @some_func: => print "Hello from", @__name | ||
| 269 | |||
| 270 | Things\some_func! | ||
| 271 | |||
| 272 | -- class variables not visible in instances | ||
| 273 | assert Things().some_func == nil | ||
| 274 | ``` | ||
| 275 | <YueDisplay> | ||
| 276 | |||
| 277 | ```yue | ||
| 278 | class Things | ||
| 279 | @some_func: => print "Hello from", @__name | ||
| 280 | |||
| 281 | Things\some_func! | ||
| 282 | |||
| 283 | -- class variables not visible in instances | ||
| 284 | assert Things().some_func == nil | ||
| 285 | ``` | ||
| 286 | |||
| 287 | </YueDisplay> | ||
| 288 | |||
| 289 | In expressions, we can use @@ to access a value that is stored in the __class of self. Thus, @@hello is shorthand for self.__class.hello. | ||
| 290 | |||
| 291 | ```yuescript | ||
| 292 | class Counter | ||
| 293 | @count: 0 | ||
| 294 | |||
| 295 | new: => | ||
| 296 | @@count += 1 | ||
| 297 | |||
| 298 | Counter! | ||
| 299 | Counter! | ||
| 300 | |||
| 301 | print Counter.count -- prints 2 | ||
| 302 | ``` | ||
| 303 | <YueDisplay> | ||
| 304 | |||
| 305 | ```yue | ||
| 306 | class Counter | ||
| 307 | @count: 0 | ||
| 308 | |||
| 309 | new: => | ||
| 310 | @@count += 1 | ||
| 311 | |||
| 312 | Counter! | ||
| 313 | Counter! | ||
| 314 | |||
| 315 | print Counter.count -- prints 2 | ||
| 316 | ``` | ||
| 317 | |||
| 318 | </YueDisplay> | ||
| 319 | |||
| 320 | The calling semantics of @@ are similar to @. Calling a @@ name will pass the class in as the first argument using Lua's colon syntax. | ||
| 321 | |||
| 322 | ```yuescript | ||
| 323 | @@hello 1,2,3,4 | ||
| 324 | ``` | ||
| 325 | <YueDisplay> | ||
| 326 | |||
| 327 | ```yue | ||
| 328 | @@hello 1,2,3,4 | ||
| 329 | ``` | ||
| 330 | |||
| 331 | </YueDisplay> | ||
| 332 | |||
| 333 | ## Class Declaration Statements | ||
| 334 | |||
| 335 | In the body of a class declaration, we can have normal expressions in addition to key/value pairs. In this context, self is equal to the class object. | ||
| 336 | |||
| 337 | Here is an alternative way to create a class variable compared to what's described above: | ||
| 338 | |||
| 339 | ```yuescript | ||
| 340 | class Things | ||
| 341 | @class_var = "hello world" | ||
| 342 | ``` | ||
| 343 | <YueDisplay> | ||
| 344 | |||
| 345 | ```yue | ||
| 346 | class Things | ||
| 347 | @class_var = "hello world" | ||
| 348 | ``` | ||
| 349 | |||
| 350 | </YueDisplay> | ||
| 351 | |||
| 352 | These expressions are executed after all the properties have been added to the base. | ||
| 353 | |||
| 354 | All variables declared in the body of the class are local to the classes properties. This is convenient for placing private values or helper functions that only the class methods can access: | ||
| 355 | |||
| 356 | ```yuescript | ||
| 357 | class MoreThings | ||
| 358 | secret = 123 | ||
| 359 | log = (msg) -> print "LOG:", msg | ||
| 360 | |||
| 361 | some_method: => | ||
| 362 | log "hello world: " .. secret | ||
| 363 | ``` | ||
| 364 | <YueDisplay> | ||
| 365 | |||
| 366 | ```yue | ||
| 367 | class MoreThings | ||
| 368 | secret = 123 | ||
| 369 | log = (msg) -> print "LOG:", msg | ||
| 370 | |||
| 371 | some_method: => | ||
| 372 | log "hello world: " .. secret | ||
| 373 | ``` | ||
| 374 | |||
| 375 | </YueDisplay> | ||
| 376 | |||
| 377 | ## @ and @@ Values | ||
| 378 | |||
| 379 | When @ and @@ are prefixed in front of a name they represent, respectively, that name accessed in self and self.__class. | ||
| 380 | |||
| 381 | If they are used all by themselves, they are aliases for self and self.__class. | ||
| 382 | |||
| 383 | ```yuescript | ||
| 384 | assert @ == self | ||
| 385 | assert @@ == self.__class | ||
| 386 | ``` | ||
| 387 | <YueDisplay> | ||
| 388 | |||
| 389 | ```yue | ||
| 390 | assert @ == self | ||
| 391 | assert @@ == self.__class | ||
| 392 | ``` | ||
| 393 | |||
| 394 | </YueDisplay> | ||
| 395 | |||
| 396 | For example, a quick way to create a new instance of the same class from an instance method using @@: | ||
| 397 | |||
| 398 | ```yuescript | ||
| 399 | some_instance_method = (...) => @@ ... | ||
| 400 | ``` | ||
| 401 | <YueDisplay> | ||
| 402 | |||
| 403 | ```yue | ||
| 404 | some_instance_method = (...) => @@ ... | ||
| 405 | ``` | ||
| 406 | |||
| 407 | </YueDisplay> | ||
| 408 | |||
| 409 | ## Constructor Property Promotion | ||
| 410 | |||
| 411 | To reduce the boilerplate code for definition of simple value objects. You can write a simple class like: | ||
| 412 | |||
| 413 | ```yuescript | ||
| 414 | class Something | ||
| 415 | new: (@foo, @bar, @@biz, @@baz) => | ||
| 416 | |||
| 417 | -- Which is short for | ||
| 418 | |||
| 419 | class Something | ||
| 420 | new: (foo, bar, biz, baz) => | ||
| 421 | @foo = foo | ||
| 422 | @bar = bar | ||
| 423 | @@biz = biz | ||
| 424 | @@baz = baz | ||
| 425 | ``` | ||
| 426 | <YueDisplay> | ||
| 427 | |||
| 428 | ```yue | ||
| 429 | class Something | ||
| 430 | new: (@foo, @bar, @@biz, @@baz) => | ||
| 431 | |||
| 432 | -- Which is short for | ||
| 433 | |||
| 434 | class Something | ||
| 435 | new: (foo, bar, biz, baz) => | ||
| 436 | @foo = foo | ||
| 437 | @bar = bar | ||
| 438 | @@biz = biz | ||
| 439 | @@baz = baz | ||
| 440 | ``` | ||
| 441 | |||
| 442 | </YueDisplay> | ||
| 443 | |||
| 444 | You can also use this syntax for a common function to initialize a object's fields. | ||
| 445 | |||
| 446 | ```yuescript | ||
| 447 | new = (@fieldA, @fieldB) => @ | ||
| 448 | obj = new {}, 123, "abc" | ||
| 449 | print obj | ||
| 450 | ``` | ||
| 451 | <YueDisplay> | ||
| 452 | |||
| 453 | ```yue | ||
| 454 | new = (@fieldA, @fieldB) => @ | ||
| 455 | obj = new {}, 123, "abc" | ||
| 456 | print obj | ||
| 457 | ``` | ||
| 458 | |||
| 459 | </YueDisplay> | ||
| 460 | |||
| 461 | ## Class Expressions | ||
| 462 | |||
| 463 | The class syntax can also be used as an expression which can be assigned to a variable or explicitly returned. | ||
| 464 | |||
| 465 | ```yuescript | ||
| 466 | x = class Bucket | ||
| 467 | drops: 0 | ||
| 468 | add_drop: => @drops += 1 | ||
| 469 | ``` | ||
| 470 | <YueDisplay> | ||
| 471 | |||
| 472 | ```yue | ||
| 473 | x = class Bucket | ||
| 474 | drops: 0 | ||
| 475 | add_drop: => @drops += 1 | ||
| 476 | ``` | ||
| 477 | |||
| 478 | </YueDisplay> | ||
| 479 | |||
| 480 | ## Anonymous classes | ||
| 481 | |||
| 482 | The name can be left out when declaring a class. The __name attribute will be nil, unless the class expression is in an assignment. The name on the left hand side of the assignment is used instead of nil. | ||
| 483 | |||
| 484 | ```yuescript | ||
| 485 | BigBucket = class extends Bucket | ||
| 486 | add_drop: => @drops += 10 | ||
| 487 | |||
| 488 | assert Bucket.__name == "BigBucket" | ||
| 489 | ``` | ||
| 490 | <YueDisplay> | ||
| 491 | |||
| 492 | ```yue | ||
| 493 | BigBucket = class extends Bucket | ||
| 494 | add_drop: => @drops += 10 | ||
| 495 | |||
| 496 | assert Bucket.__name == "BigBucket" | ||
| 497 | ``` | ||
| 498 | |||
| 499 | </YueDisplay> | ||
| 500 | |||
| 501 | You can even leave off the body, meaning you can write a blank anonymous class like this: | ||
| 502 | |||
| 503 | ```yuescript | ||
| 504 | x = class | ||
| 505 | ``` | ||
| 506 | <YueDisplay> | ||
| 507 | |||
| 508 | ```yue | ||
| 509 | x = class | ||
| 510 | ``` | ||
| 511 | |||
| 512 | </YueDisplay> | ||
| 513 | |||
| 514 | ## Class Mixing | ||
| 515 | |||
| 516 | You can do mixing with keyword `using` to copy functions from either a plain table or a predefined class object into your new class. When doing mixing with a plain table, you can override the class indexing function (metamethod `__index`) to your customized implementation. When doing mixing with an existing class object, the class object's metamethods won't be copied. | ||
| 517 | |||
| 518 | ```yuescript | ||
| 519 | MyIndex = __index: var: 1 | ||
| 520 | |||
| 521 | class X using MyIndex | ||
| 522 | func: => | ||
| 523 | print 123 | ||
| 524 | |||
| 525 | x = X! | ||
| 526 | print x.var | ||
| 527 | |||
| 528 | class Y using X | ||
| 529 | |||
| 530 | y = Y! | ||
| 531 | y\func! | ||
| 532 | |||
| 533 | assert y.__class.__parent ~= X -- X is not parent of Y | ||
| 534 | ``` | ||
| 535 | <YueDisplay> | ||
| 536 | |||
| 537 | ```yue | ||
| 538 | MyIndex = __index: var: 1 | ||
| 539 | |||
| 540 | class X using MyIndex | ||
| 541 | func: => | ||
| 542 | print 123 | ||
| 543 | |||
| 544 | x = X! | ||
| 545 | print x.var | ||
| 546 | |||
| 547 | class Y using X | ||
| 548 | |||
| 549 | y = Y! | ||
| 550 | y\func! | ||
| 551 | |||
| 552 | assert y.__class.__parent ~= X -- X is not parent of Y | ||
| 553 | ``` | ||
| 554 | |||
| 555 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/doc/operator.md b/doc/docs/pt-br/doc/operator.md new file mode 100644 index 0000000..9a2e89b --- /dev/null +++ b/doc/docs/pt-br/doc/operator.md | |||
| @@ -0,0 +1,460 @@ | |||
| 1 | # Operator | ||
| 2 | |||
| 3 | All of Lua's binary and unary operators are available. Additionally **!=** is as an alias for **~=**, and either **\\** or **::** can be used to write a chaining function call like `tb\func!` or `tb::func!`. And Yuescipt offers some other special operators to write more expressive codes. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | tb\func! if tb ~= nil | ||
| 7 | tb::func! if tb != nil | ||
| 8 | ``` | ||
| 9 | <YueDisplay> | ||
| 10 | |||
| 11 | ```yue | ||
| 12 | tb\func! if tb ~= nil | ||
| 13 | tb::func! if tb != nil | ||
| 14 | ``` | ||
| 15 | |||
| 16 | </YueDisplay> | ||
| 17 | |||
| 18 | ## Chaining Comparisons | ||
| 19 | |||
| 20 | Comparisons can be arbitrarily chained: | ||
| 21 | |||
| 22 | ```yuescript | ||
| 23 | print 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5 | ||
| 24 | -- output: true | ||
| 25 | |||
| 26 | a = 5 | ||
| 27 | print 1 <= a <= 10 | ||
| 28 | -- output: true | ||
| 29 | ``` | ||
| 30 | <YueDisplay> | ||
| 31 | |||
| 32 | ```yue | ||
| 33 | print 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5 | ||
| 34 | -- output: true | ||
| 35 | |||
| 36 | a = 5 | ||
| 37 | print 1 <= a <= 10 | ||
| 38 | -- output: true | ||
| 39 | ``` | ||
| 40 | |||
| 41 | </YueDisplay> | ||
| 42 | |||
| 43 | Note the evaluation behavior of chained comparisons: | ||
| 44 | |||
| 45 | ```yuescript | ||
| 46 | v = (x) -> | ||
| 47 | print x | ||
| 48 | x | ||
| 49 | |||
| 50 | print v(1) < v(2) <= v(3) | ||
| 51 | --[[ | ||
| 52 | output: | ||
| 53 | 2 | ||
| 54 | 1 | ||
| 55 | 3 | ||
| 56 | true | ||
| 57 | ]] | ||
| 58 | |||
| 59 | print v(1) > v(2) <= v(3) | ||
| 60 | --[[ | ||
| 61 | output: | ||
| 62 | 2 | ||
| 63 | 1 | ||
| 64 | false | ||
| 65 | ]] | ||
| 66 | ``` | ||
| 67 | <YueDisplay> | ||
| 68 | |||
| 69 | ```yue | ||
| 70 | v = (x) -> | ||
| 71 | print x | ||
| 72 | x | ||
| 73 | |||
| 74 | print v(1) < v(2) <= v(3) | ||
| 75 | --[[ | ||
| 76 | output: | ||
| 77 | 2 | ||
| 78 | 1 | ||
| 79 | 3 | ||
| 80 | true | ||
| 81 | ]] | ||
| 82 | |||
| 83 | print v(1) > v(2) <= v(3) | ||
| 84 | --[[ | ||
| 85 | output: | ||
| 86 | 2 | ||
| 87 | 1 | ||
| 88 | false | ||
| 89 | ]] | ||
| 90 | ``` | ||
| 91 | |||
| 92 | </YueDisplay> | ||
| 93 | |||
| 94 | The middle expression is only evaluated once, rather than twice as it would be if the expression were written as `v(1) < v(2) and v(2) <= v(3)`. However, the order of evaluations in a chained comparison is undefined. It is strongly recommended not to use expressions with side effects (such as printing) in chained comparisons. If side effects are required, the short-circuit `and` operator should be used explicitly. | ||
| 95 | |||
| 96 | ## Table Appending | ||
| 97 | |||
| 98 | The **[] =** operator is used to append values to tables. | ||
| 99 | |||
| 100 | ```yuescript | ||
| 101 | tab = [] | ||
| 102 | tab[] = "Value" | ||
| 103 | ``` | ||
| 104 | <YueDisplay> | ||
| 105 | |||
| 106 | ```yue | ||
| 107 | tab = [] | ||
| 108 | tab[] = "Value" | ||
| 109 | ``` | ||
| 110 | |||
| 111 | </YueDisplay> | ||
| 112 | |||
| 113 | You can also use the spread operator `...` to append all elements from one list to another: | ||
| 114 | |||
| 115 | ```yuescript | ||
| 116 | tbA = [1, 2, 3] | ||
| 117 | tbB = [4, 5, 6] | ||
| 118 | tbA[] = ...tbB | ||
| 119 | -- tbA is now [1, 2, 3, 4, 5, 6] | ||
| 120 | ``` | ||
| 121 | <YueDisplay> | ||
| 122 | |||
| 123 | ```yue | ||
| 124 | tbA = [1, 2, 3] | ||
| 125 | tbB = [4, 5, 6] | ||
| 126 | tbA[] = ...tbB | ||
| 127 | -- tbA is now [1, 2, 3, 4, 5, 6] | ||
| 128 | ``` | ||
| 129 | |||
| 130 | </YueDisplay> | ||
| 131 | |||
| 132 | ## Table Spreading | ||
| 133 | |||
| 134 | You can concatenate array tables or hash tables using spread operator `...` before expressions in table literals. | ||
| 135 | |||
| 136 | ```yuescript | ||
| 137 | parts = | ||
| 138 | * "shoulders" | ||
| 139 | * "knees" | ||
| 140 | lyrics = | ||
| 141 | * "head" | ||
| 142 | * ...parts | ||
| 143 | * "and" | ||
| 144 | * "toes" | ||
| 145 | |||
| 146 | copy = {...other} | ||
| 147 | |||
| 148 | a = {1, 2, 3, x: 1} | ||
| 149 | b = {4, 5, y: 1} | ||
| 150 | merge = {...a, ...b} | ||
| 151 | ``` | ||
| 152 | <YueDisplay> | ||
| 153 | |||
| 154 | ```yue | ||
| 155 | parts = | ||
| 156 | * "shoulders" | ||
| 157 | * "knees" | ||
| 158 | lyrics = | ||
| 159 | * "head" | ||
| 160 | * ...parts | ||
| 161 | * "and" | ||
| 162 | * "toes" | ||
| 163 | |||
| 164 | copy = {...other} | ||
| 165 | |||
| 166 | a = {1, 2, 3, x: 1} | ||
| 167 | b = {4, 5, y: 1} | ||
| 168 | merge = {...a, ...b} | ||
| 169 | ``` | ||
| 170 | |||
| 171 | </YueDisplay> | ||
| 172 | |||
| 173 | ## Table Reversed Indexing | ||
| 174 | |||
| 175 | You can use the **#** operator to get the last elements of a table. | ||
| 176 | |||
| 177 | ```yuescript | ||
| 178 | last = data.items[#] | ||
| 179 | second_last = data.items[#-1] | ||
| 180 | data.items[#] = 1 | ||
| 181 | ``` | ||
| 182 | <YueDisplay> | ||
| 183 | |||
| 184 | ```yue | ||
| 185 | last = data.items[#] | ||
| 186 | second_last = data.items[#-1] | ||
| 187 | data.items[#] = 1 | ||
| 188 | ``` | ||
| 189 | |||
| 190 | </YueDisplay> | ||
| 191 | |||
| 192 | ## Metatable | ||
| 193 | |||
| 194 | The **<>** operator can be used as a shortcut for metatable manipulation. | ||
| 195 | |||
| 196 | ### Metatable Creation | ||
| 197 | |||
| 198 | Create normal table with empty bracekets **<>** or metamethod key which is surrounded by **<>**. | ||
| 199 | |||
| 200 | ```yuescript | ||
| 201 | mt = {} | ||
| 202 | add = (right) => <>: mt, value: @value + right.value | ||
| 203 | mt.__add = add | ||
| 204 | |||
| 205 | a = <>: mt, value: 1 | ||
| 206 | -- set field with variable of the same name | ||
| 207 | b = :<add>, value: 2 | ||
| 208 | c = <add>: mt.__add, value: 3 | ||
| 209 | |||
| 210 | d = a + b + c | ||
| 211 | print d.value | ||
| 212 | |||
| 213 | close _ = <close>: -> print "out of scope" | ||
| 214 | ``` | ||
| 215 | <YueDisplay> | ||
| 216 | |||
| 217 | ```yue | ||
| 218 | mt = {} | ||
| 219 | add = (right) => <>: mt, value: @value + right.value | ||
| 220 | mt.__add = add | ||
| 221 | |||
| 222 | a = <>: mt, value: 1 | ||
| 223 | -- set field with variable of the same name | ||
| 224 | b = :<add>, value: 2 | ||
| 225 | c = <add>: mt.__add, value: 3 | ||
| 226 | |||
| 227 | d = a + b + c | ||
| 228 | print d.value | ||
| 229 | |||
| 230 | close _ = <close>: -> print "out of scope" | ||
| 231 | ``` | ||
| 232 | |||
| 233 | </YueDisplay> | ||
| 234 | |||
| 235 | ### Metatable Accessing | ||
| 236 | |||
| 237 | Accessing metatable with **<>** or metamethod name surrounded by **<>** or writing some expression in **<>**. | ||
| 238 | |||
| 239 | ```yuescript | ||
| 240 | -- create with metatable containing field "value" | ||
| 241 | tb = <"value">: 123 | ||
| 242 | tb.<index> = tb.<> | ||
| 243 | print tb.value | ||
| 244 | |||
| 245 | tb.<> = __index: {item: "hello"} | ||
| 246 | print tb.item | ||
| 247 | ``` | ||
| 248 | <YueDisplay> | ||
| 249 | |||
| 250 | ```yue | ||
| 251 | -- create with metatable containing field "value" | ||
| 252 | tb = <"value">: 123 | ||
| 253 | tb.<index> = tb.<> | ||
| 254 | print tb.value | ||
| 255 | tb.<> = __index: {item: "hello"} | ||
| 256 | print tb.item | ||
| 257 | ``` | ||
| 258 | |||
| 259 | </YueDisplay> | ||
| 260 | |||
| 261 | ### Metatable Destructure | ||
| 262 | |||
| 263 | Destruct metatable with metamethod key surrounded by **<>**. | ||
| 264 | |||
| 265 | ```yuescript | ||
| 266 | {item, :new, :<close>, <index>: getter} = tb | ||
| 267 | print item, new, close, getter | ||
| 268 | ``` | ||
| 269 | <YueDisplay> | ||
| 270 | |||
| 271 | ```yue | ||
| 272 | {item, :new, :<close>, <index>: getter} = tb | ||
| 273 | print item, new, close, getter | ||
| 274 | ``` | ||
| 275 | |||
| 276 | </YueDisplay> | ||
| 277 | |||
| 278 | ## Existence | ||
| 279 | |||
| 280 | The **?** operator can be used in a variety of contexts to check for existence. | ||
| 281 | |||
| 282 | ```yuescript | ||
| 283 | func?! | ||
| 284 | print abc?["hello world"]?.xyz | ||
| 285 | |||
| 286 | x = tab?.value | ||
| 287 | len = utf8?.len or string?.len or (o) -> #o | ||
| 288 | |||
| 289 | if print and x? | ||
| 290 | print x | ||
| 291 | |||
| 292 | with? io.open "test.txt", "w" | ||
| 293 | \write "hello" | ||
| 294 | \close! | ||
| 295 | ``` | ||
| 296 | <YueDisplay> | ||
| 297 | |||
| 298 | ```yue | ||
| 299 | func?! | ||
| 300 | print abc?["hello world"]?.xyz | ||
| 301 | |||
| 302 | x = tab?.value | ||
| 303 | len = utf8?.len or string?.len or (o) -> #o | ||
| 304 | |||
| 305 | if print and x? | ||
| 306 | print x | ||
| 307 | |||
| 308 | with? io.open "test.txt", "w" | ||
| 309 | \write "hello" | ||
| 310 | \close! | ||
| 311 | ``` | ||
| 312 | |||
| 313 | </YueDisplay> | ||
| 314 | |||
| 315 | ## Piping | ||
| 316 | |||
| 317 | Instead of a series of nested function calls, you can pipe values with operator **|>**. | ||
| 318 | |||
| 319 | ```yuescript | ||
| 320 | "hello" |> print | ||
| 321 | 1 |> print 2 -- insert pipe item as the first argument | ||
| 322 | 2 |> print 1, _, 3 -- pipe with a placeholder | ||
| 323 | |||
| 324 | -- pipe expression in multiline | ||
| 325 | readFile "example.txt" | ||
| 326 | |> extract language, {} | ||
| 327 | |> parse language | ||
| 328 | |> emit | ||
| 329 | |> render | ||
| 330 | |||
| 331 | ``` | ||
| 332 | <YueDisplay> | ||
| 333 | |||
| 334 | ```yue | ||
| 335 | "hello" |> print | ||
| 336 | 1 |> print 2 -- insert pipe item as the first argument | ||
| 337 | 2 |> print 1, _, 3 -- pipe with a placeholder | ||
| 338 | -- pipe expression in multiline | ||
| 339 | readFile "example.txt" | ||
| 340 | |> extract language, {} | ||
| 341 | |> parse language | ||
| 342 | |> emit | ||
| 343 | |> render | ||
| 344 | |||
| 345 | ``` | ||
| 346 | |||
| 347 | </YueDisplay> | ||
| 348 | |||
| 349 | ## Nil Coalescing | ||
| 350 | |||
| 351 | The nil-coalescing operator **??** returns the value of its left-hand operand if it isn't **nil**; otherwise, it evaluates the right-hand operand and returns its result. The **??** operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-nil. | ||
| 352 | ```yuescript | ||
| 353 | local a, b, c, d | ||
| 354 | a = b ?? c ?? d | ||
| 355 | func a ?? {} | ||
| 356 | |||
| 357 | a ??= false | ||
| 358 | ``` | ||
| 359 | <YueDisplay> | ||
| 360 | |||
| 361 | ```yue | ||
| 362 | local a, b, c, d | ||
| 363 | a = b ?? c ?? d | ||
| 364 | func a ?? {} | ||
| 365 | a ??= false | ||
| 366 | ``` | ||
| 367 | |||
| 368 | </YueDisplay> | ||
| 369 | |||
| 370 | ## Implicit Object | ||
| 371 | |||
| 372 | You can write a list of implicit structures that starts with the symbol **\*** or **-** inside a table block. If you are creating implicit object, the fields of the object must be with the same indent. | ||
| 373 | |||
| 374 | ```yuescript | ||
| 375 | -- assignment with implicit object | ||
| 376 | list = | ||
| 377 | * 1 | ||
| 378 | * 2 | ||
| 379 | * 3 | ||
| 380 | |||
| 381 | -- function call with implicit object | ||
| 382 | func | ||
| 383 | * 1 | ||
| 384 | * 2 | ||
| 385 | * 3 | ||
| 386 | |||
| 387 | -- return with implicit object | ||
| 388 | f = -> | ||
| 389 | return | ||
| 390 | * 1 | ||
| 391 | * 2 | ||
| 392 | * 3 | ||
| 393 | |||
| 394 | -- table with implicit object | ||
| 395 | tb = | ||
| 396 | name: "abc" | ||
| 397 | |||
| 398 | values: | ||
| 399 | - "a" | ||
| 400 | - "b" | ||
| 401 | - "c" | ||
| 402 | |||
| 403 | objects: | ||
| 404 | - name: "a" | ||
| 405 | value: 1 | ||
| 406 | func: => @value + 1 | ||
| 407 | tb: | ||
| 408 | fieldA: 1 | ||
| 409 | |||
| 410 | - name: "b" | ||
| 411 | value: 2 | ||
| 412 | func: => @value + 2 | ||
| 413 | tb: { } | ||
| 414 | |||
| 415 | ``` | ||
| 416 | <YueDisplay> | ||
| 417 | |||
| 418 | ```yue | ||
| 419 | -- assignment with implicit object | ||
| 420 | list = | ||
| 421 | * 1 | ||
| 422 | * 2 | ||
| 423 | * 3 | ||
| 424 | |||
| 425 | -- function call with implicit object | ||
| 426 | func | ||
| 427 | * 1 | ||
| 428 | * 2 | ||
| 429 | * 3 | ||
| 430 | |||
| 431 | -- return with implicit object | ||
| 432 | f = -> | ||
| 433 | return | ||
| 434 | * 1 | ||
| 435 | * 2 | ||
| 436 | * 3 | ||
| 437 | |||
| 438 | -- table with implicit object | ||
| 439 | tb = | ||
| 440 | name: "abc" | ||
| 441 | |||
| 442 | values: | ||
| 443 | - "a" | ||
| 444 | - "b" | ||
| 445 | - "c" | ||
| 446 | |||
| 447 | objects: | ||
| 448 | - name: "a" | ||
| 449 | value: 1 | ||
| 450 | func: => @value + 1 | ||
| 451 | tb: | ||
| 452 | fieldA: 1 | ||
| 453 | |||
| 454 | - name: "b" | ||
| 455 | value: 2 | ||
| 456 | func: => @value + 2 | ||
| 457 | tb: { } | ||
| 458 | ``` | ||
| 459 | |||
| 460 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/doc/switch.md b/doc/docs/pt-br/doc/switch.md new file mode 100644 index 0000000..f503a80 --- /dev/null +++ b/doc/docs/pt-br/doc/switch.md | |||
| @@ -0,0 +1,296 @@ | |||
| 1 | # Switch | ||
| 2 | |||
| 3 | The switch statement is shorthand for writing a series of if statements that check against the same value. Note that the value is only evaluated once. Like if statements, switches can have an else block to handle no matches. Comparison is done with the == operator. In switch statement, you can also use assignment expression to store temporary variable value. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | switch name := "Dan" | ||
| 7 | when "Robert" | ||
| 8 | print "You are Robert" | ||
| 9 | when "Dan", "Daniel" | ||
| 10 | print "Your name, it's Dan" | ||
| 11 | else | ||
| 12 | print "I don't know about you with name #{name}" | ||
| 13 | ``` | ||
| 14 | <YueDisplay> | ||
| 15 | |||
| 16 | ```yue | ||
| 17 | switch name := "Dan" | ||
| 18 | when "Robert" | ||
| 19 | print "You are Robert" | ||
| 20 | when "Dan", "Daniel" | ||
| 21 | print "Your name, it's Dan" | ||
| 22 | else | ||
| 23 | print "I don't know about you with name #{name}" | ||
| 24 | ``` | ||
| 25 | |||
| 26 | </YueDisplay> | ||
| 27 | |||
| 28 | A switch when clause can match against multiple values by listing them out comma separated. | ||
| 29 | |||
| 30 | Switches can be used as expressions as well, here we can assign the result of the switch to a variable: | ||
| 31 | |||
| 32 | ```yuescript | ||
| 33 | b = 1 | ||
| 34 | next_number = switch b | ||
| 35 | when 1 | ||
| 36 | 2 | ||
| 37 | when 2 | ||
| 38 | 3 | ||
| 39 | else | ||
| 40 | error "can't count that high!" | ||
| 41 | ``` | ||
| 42 | <YueDisplay> | ||
| 43 | |||
| 44 | ```yue | ||
| 45 | b = 1 | ||
| 46 | next_number = switch b | ||
| 47 | when 1 | ||
| 48 | 2 | ||
| 49 | when 2 | ||
| 50 | 3 | ||
| 51 | else | ||
| 52 | error "can't count that high!" | ||
| 53 | ``` | ||
| 54 | |||
| 55 | </YueDisplay> | ||
| 56 | |||
| 57 | We can use the then keyword to write a switch's when block on a single line. No extra keyword is needed to write the else block on a single line. | ||
| 58 | |||
| 59 | ```yuescript | ||
| 60 | msg = switch math.random(1, 5) | ||
| 61 | when 1 then "you are lucky" | ||
| 62 | when 2 then "you are almost lucky" | ||
| 63 | else "not so lucky" | ||
| 64 | ``` | ||
| 65 | <YueDisplay> | ||
| 66 | |||
| 67 | ```yue | ||
| 68 | msg = switch math.random(1, 5) | ||
| 69 | when 1 then "you are lucky" | ||
| 70 | when 2 then "you are almost lucky" | ||
| 71 | else "not so lucky" | ||
| 72 | ``` | ||
| 73 | |||
| 74 | </YueDisplay> | ||
| 75 | |||
| 76 | If you want to write code with one less indent when writing a switch statement, you can put the first when clause on the statement start line, and then all other clauses can be written with one less indent. | ||
| 77 | |||
| 78 | ```yuescript | ||
| 79 | switch math.random(1, 5) | ||
| 80 | when 1 | ||
| 81 | print "you are lucky" -- two indents | ||
| 82 | else | ||
| 83 | print "not so lucky" | ||
| 84 | |||
| 85 | switch math.random(1, 5) when 1 | ||
| 86 | print "you are lucky" -- one indent | ||
| 87 | else | ||
| 88 | print "not so lucky" | ||
| 89 | ``` | ||
| 90 | <YueDisplay> | ||
| 91 | |||
| 92 | ```yue | ||
| 93 | switch math.random(1, 5) | ||
| 94 | when 1 | ||
| 95 | print "you are lucky" -- two indents | ||
| 96 | else | ||
| 97 | print "not so lucky" | ||
| 98 | |||
| 99 | switch math.random(1, 5) when 1 | ||
| 100 | print "you are lucky" -- one indent | ||
| 101 | else | ||
| 102 | print "not so lucky" | ||
| 103 | ``` | ||
| 104 | |||
| 105 | </YueDisplay> | ||
| 106 | |||
| 107 | It is worth noting the order of the case comparison expression. The case's expression is on the left hand side. This can be useful if the case's expression wants to overwrite how the comparison is done by defining an eq metamethod. | ||
| 108 | |||
| 109 | ## Table Matching | ||
| 110 | |||
| 111 | You can do table matching in a switch when clause, if the table can be destructured by a specific structure and get non-nil values. | ||
| 112 | |||
| 113 | ```yuescript | ||
| 114 | items = | ||
| 115 | * x: 100 | ||
| 116 | y: 200 | ||
| 117 | * width: 300 | ||
| 118 | height: 400 | ||
| 119 | |||
| 120 | for item in *items | ||
| 121 | switch item | ||
| 122 | when :x, :y | ||
| 123 | print "Vec2 #{x}, #{y}" | ||
| 124 | when :width, :height | ||
| 125 | print "size #{width}, #{height}" | ||
| 126 | ``` | ||
| 127 | <YueDisplay> | ||
| 128 | |||
| 129 | ```yue | ||
| 130 | items = | ||
| 131 | * x: 100 | ||
| 132 | y: 200 | ||
| 133 | * width: 300 | ||
| 134 | height: 400 | ||
| 135 | |||
| 136 | for item in *items | ||
| 137 | switch item | ||
| 138 | when :x, :y | ||
| 139 | print "Vec2 #{x}, #{y}" | ||
| 140 | when :width, :height | ||
| 141 | print "size #{width}, #{height}" | ||
| 142 | ``` | ||
| 143 | |||
| 144 | </YueDisplay> | ||
| 145 | |||
| 146 | You can use default values to optionally destructure the table for some fields. | ||
| 147 | |||
| 148 | ```yuescript | ||
| 149 | item = {} | ||
| 150 | |||
| 151 | {pos: {:x = 50, :y = 200}} = item -- get error: attempt to index a nil value (field 'pos') | ||
| 152 | |||
| 153 | switch item | ||
| 154 | when {pos: {:x = 50, :y = 200}} | ||
| 155 | print "Vec2 #{x}, #{y}" -- table destructuring will still pass | ||
| 156 | ``` | ||
| 157 | <YueDisplay> | ||
| 158 | |||
| 159 | ```yue | ||
| 160 | item = {} | ||
| 161 | |||
| 162 | {pos: {:x = 50, :y = 200}} = item -- get error: attempt to index a nil value (field 'pos') | ||
| 163 | |||
| 164 | switch item | ||
| 165 | when {pos: {:x = 50, :y = 200}} | ||
| 166 | print "Vec2 #{x}, #{y}" -- table destructuring will still pass | ||
| 167 | ``` | ||
| 168 | |||
| 169 | </YueDisplay> | ||
| 170 | |||
| 171 | You can also match against array elements, table fields, and even nested structures with array or table literals. | ||
| 172 | |||
| 173 | Match against array elements. | ||
| 174 | |||
| 175 | ```yuescript | ||
| 176 | switch tb | ||
| 177 | when [1, 2, 3] | ||
| 178 | print "1, 2, 3" | ||
| 179 | when [1, b, 3] | ||
| 180 | print "1, #{b}, 3" | ||
| 181 | when [1, 2, b = 3] -- b has a default value | ||
| 182 | print "1, 2, #{b}" | ||
| 183 | ``` | ||
| 184 | <YueDisplay> | ||
| 185 | |||
| 186 | ```yue | ||
| 187 | switch tb | ||
| 188 | when [1, 2, 3] | ||
| 189 | print "1, 2, 3" | ||
| 190 | when [1, b, 3] | ||
| 191 | print "1, #{b}, 3" | ||
| 192 | when [1, 2, b = 3] -- b has a default value | ||
| 193 | print "1, 2, #{b}" | ||
| 194 | ``` | ||
| 195 | |||
| 196 | </YueDisplay> | ||
| 197 | |||
| 198 | Match against table fields with destructuring. | ||
| 199 | |||
| 200 | ```yuescript | ||
| 201 | switch tb | ||
| 202 | when success: true, :result | ||
| 203 | print "success", result | ||
| 204 | when success: false | ||
| 205 | print "failed", result | ||
| 206 | else | ||
| 207 | print "invalid" | ||
| 208 | ``` | ||
| 209 | <YueDisplay> | ||
| 210 | |||
| 211 | ```yue | ||
| 212 | switch tb | ||
| 213 | when success: true, :result | ||
| 214 | print "success", result | ||
| 215 | when success: false | ||
| 216 | print "failed", result | ||
| 217 | else | ||
| 218 | print "invalid" | ||
| 219 | ``` | ||
| 220 | |||
| 221 | </YueDisplay> | ||
| 222 | |||
| 223 | Match against nested table structures. | ||
| 224 | |||
| 225 | ```yuescript | ||
| 226 | switch tb | ||
| 227 | when data: {type: "success", :content} | ||
| 228 | print "success", content | ||
| 229 | when data: {type: "error", :content} | ||
| 230 | print "failed", content | ||
| 231 | else | ||
| 232 | print "invalid" | ||
| 233 | ``` | ||
| 234 | <YueDisplay> | ||
| 235 | |||
| 236 | ```yue | ||
| 237 | switch tb | ||
| 238 | when data: {type: "success", :content} | ||
| 239 | print "success", content | ||
| 240 | when data: {type: "error", :content} | ||
| 241 | print "failed", content | ||
| 242 | else | ||
| 243 | print "invalid" | ||
| 244 | ``` | ||
| 245 | |||
| 246 | </YueDisplay> | ||
| 247 | |||
| 248 | Match against array of tables. | ||
| 249 | |||
| 250 | ```yuescript | ||
| 251 | switch tb | ||
| 252 | when [ | ||
| 253 | {a: 1, b: 2} | ||
| 254 | {a: 3, b: 4} | ||
| 255 | {a: 5, b: 6} | ||
| 256 | fourth | ||
| 257 | ] | ||
| 258 | print "matched", fourth | ||
| 259 | ``` | ||
| 260 | <YueDisplay> | ||
| 261 | |||
| 262 | ```yue | ||
| 263 | switch tb | ||
| 264 | when [ | ||
| 265 | {a: 1, b: 2} | ||
| 266 | {a: 3, b: 4} | ||
| 267 | {a: 5, b: 6} | ||
| 268 | fourth | ||
| 269 | ] | ||
| 270 | print "matched", fourth | ||
| 271 | ``` | ||
| 272 | |||
| 273 | </YueDisplay> | ||
| 274 | |||
| 275 | Match against a list and capture a range of elements. | ||
| 276 | |||
| 277 | ```yuescript | ||
| 278 | segments = ["admin", "users", "logs", "view"] | ||
| 279 | switch segments | ||
| 280 | when [...groups, resource, action] | ||
| 281 | print "Group:", groups -- prints: {"admin", "users"} | ||
| 282 | print "Resource:", resource -- prints: "logs" | ||
| 283 | print "Action:", action -- prints: "view" | ||
| 284 | ``` | ||
| 285 | <YueDisplay> | ||
| 286 | |||
| 287 | ```yue | ||
| 288 | segments = ["admin", "users", "logs", "view"] | ||
| 289 | switch segments | ||
| 290 | when [...groups, resource, action] | ||
| 291 | print "Group:", groups -- prints: {"admin", "users"} | ||
| 292 | print "Resource:", resource -- prints: "logs" | ||
| 293 | print "Action:", action -- prints: "view" | ||
| 294 | ``` | ||
| 295 | |||
| 296 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/doc/table-literals.md b/doc/docs/pt-br/doc/table-literals.md new file mode 100644 index 0000000..c1adcab --- /dev/null +++ b/doc/docs/pt-br/doc/table-literals.md | |||
| @@ -0,0 +1,168 @@ | |||
| 1 | # Table Literals | ||
| 2 | |||
| 3 | Like in Lua, tables are delimited in curly braces. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | some_values = [1, 2, 3, 4] | ||
| 7 | ``` | ||
| 8 | <YueDisplay> | ||
| 9 | |||
| 10 | ```yue | ||
| 11 | some_values = [1, 2, 3, 4] | ||
| 12 | ``` | ||
| 13 | |||
| 14 | </YueDisplay> | ||
| 15 | |||
| 16 | Unlike Lua, assigning a value to a key in a table is done with **:** (instead of **=**). | ||
| 17 | |||
| 18 | ```yuescript | ||
| 19 | some_values = { | ||
| 20 | name: "Bill", | ||
| 21 | age: 200, | ||
| 22 | ["favorite food"]: "rice" | ||
| 23 | } | ||
| 24 | ``` | ||
| 25 | <YueDisplay> | ||
| 26 | |||
| 27 | ```yue | ||
| 28 | some_values = { | ||
| 29 | name: "Bill", | ||
| 30 | age: 200, | ||
| 31 | ["favorite food"]: "rice" | ||
| 32 | } | ||
| 33 | ``` | ||
| 34 | |||
| 35 | </YueDisplay> | ||
| 36 | |||
| 37 | The curly braces can be left off if a single table of key value pairs is being assigned. | ||
| 38 | |||
| 39 | ```yuescript | ||
| 40 | profile = | ||
| 41 | height: "4 feet", | ||
| 42 | shoe_size: 13, | ||
| 43 | favorite_foods: ["ice cream", "donuts"] | ||
| 44 | ``` | ||
| 45 | <YueDisplay> | ||
| 46 | |||
| 47 | ```yue | ||
| 48 | profile = | ||
| 49 | height: "4 feet", | ||
| 50 | shoe_size: 13, | ||
| 51 | favorite_foods: ["ice cream", "donuts"] | ||
| 52 | ``` | ||
| 53 | |||
| 54 | </YueDisplay> | ||
| 55 | |||
| 56 | Newlines can be used to delimit values instead of a comma (or both): | ||
| 57 | |||
| 58 | ```yuescript | ||
| 59 | values = { | ||
| 60 | 1, 2, 3, 4 | ||
| 61 | 5, 6, 7, 8 | ||
| 62 | name: "superman" | ||
| 63 | occupation: "crime fighting" | ||
| 64 | } | ||
| 65 | ``` | ||
| 66 | <YueDisplay> | ||
| 67 | |||
| 68 | ```yue | ||
| 69 | values = { | ||
| 70 | 1, 2, 3, 4 | ||
| 71 | 5, 6, 7, 8 | ||
| 72 | name: "superman" | ||
| 73 | occupation: "crime fighting" | ||
| 74 | } | ||
| 75 | ``` | ||
| 76 | |||
| 77 | </YueDisplay> | ||
| 78 | |||
| 79 | When creating a single line table literal, the curly braces can also be left off: | ||
| 80 | |||
| 81 | ```yuescript | ||
| 82 | my_function dance: "Tango", partner: "none" | ||
| 83 | |||
| 84 | y = type: "dog", legs: 4, tails: 1 | ||
| 85 | ``` | ||
| 86 | <YueDisplay> | ||
| 87 | |||
| 88 | ```yue | ||
| 89 | my_function dance: "Tango", partner: "none" | ||
| 90 | |||
| 91 | y = type: "dog", legs: 4, tails: 1 | ||
| 92 | ``` | ||
| 93 | |||
| 94 | </YueDisplay> | ||
| 95 | |||
| 96 | The keys of a table literal can be language keywords without being escaped: | ||
| 97 | |||
| 98 | ```yuescript | ||
| 99 | tbl = { | ||
| 100 | do: "something" | ||
| 101 | end: "hunger" | ||
| 102 | } | ||
| 103 | ``` | ||
| 104 | <YueDisplay> | ||
| 105 | |||
| 106 | ```yue | ||
| 107 | tbl = { | ||
| 108 | do: "something" | ||
| 109 | end: "hunger" | ||
| 110 | } | ||
| 111 | ``` | ||
| 112 | |||
| 113 | </YueDisplay> | ||
| 114 | |||
| 115 | If you are constructing a table out of variables and wish the keys to be the same as the variable names, then the **:** prefix operator can be used: | ||
| 116 | |||
| 117 | ```yuescript | ||
| 118 | hair = "golden" | ||
| 119 | height = 200 | ||
| 120 | person = { :hair, :height, shoe_size: 40 } | ||
| 121 | |||
| 122 | print_table :hair, :height | ||
| 123 | ``` | ||
| 124 | <YueDisplay> | ||
| 125 | |||
| 126 | ```yue | ||
| 127 | hair = "golden" | ||
| 128 | height = 200 | ||
| 129 | person = { :hair, :height, shoe_size: 40 } | ||
| 130 | |||
| 131 | print_table :hair, :height | ||
| 132 | ``` | ||
| 133 | |||
| 134 | </YueDisplay> | ||
| 135 | |||
| 136 | If you want the key of a field in the table to to be result of an expression, then you can wrap it in **[ ]**, just like in Lua. You can also use a string literal directly as a key, leaving out the square brackets. This is useful if your key has any special characters. | ||
| 137 | |||
| 138 | ```yuescript | ||
| 139 | t = { | ||
| 140 | [1 + 2]: "hello" | ||
| 141 | "hello world": true | ||
| 142 | } | ||
| 143 | ``` | ||
| 144 | <YueDisplay> | ||
| 145 | |||
| 146 | ```yue | ||
| 147 | t = { | ||
| 148 | [1 + 2]: "hello" | ||
| 149 | "hello world": true | ||
| 150 | } | ||
| 151 | ``` | ||
| 152 | |||
| 153 | </YueDisplay> | ||
| 154 | |||
| 155 | Lua tables have both an array part and a hash part, but sometimes you want to make a semantic distinction between array and hash usage when writing Lua tables. Then you can write Lua table with **[ ]** instead of **{ }** to represent an array table and writing any key value pair in a list table won't be allowed. | ||
| 156 | |||
| 157 | ```yuescript | ||
| 158 | some_values = [1, 2, 3, 4] | ||
| 159 | list_with_one_element = [1, ] | ||
| 160 | ``` | ||
| 161 | <YueDisplay> | ||
| 162 | |||
| 163 | ```yue | ||
| 164 | some_values = [1, 2, 3, 4] | ||
| 165 | list_with_one_element = [1, ] | ||
| 166 | ``` | ||
| 167 | |||
| 168 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/doc/the-using-clause-controlling-destructive-assignment.md b/doc/docs/pt-br/doc/the-using-clause-controlling-destructive-assignment.md new file mode 100644 index 0000000..fb9b740 --- /dev/null +++ b/doc/docs/pt-br/doc/the-using-clause-controlling-destructive-assignment.md | |||
| @@ -0,0 +1,98 @@ | |||
| 1 | # The Using Clause; Controlling Destructive Assignment | ||
| 2 | |||
| 3 | While lexical scoping can be a great help in reducing the complexity of the code we write, things can get unwieldy as the code size increases. Consider the following snippet: | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | i = 100 | ||
| 7 | |||
| 8 | -- many lines of code... | ||
| 9 | |||
| 10 | my_func = -> | ||
| 11 | i = 10 | ||
| 12 | while i > 0 | ||
| 13 | print i | ||
| 14 | i -= 1 | ||
| 15 | |||
| 16 | my_func! | ||
| 17 | |||
| 18 | print i -- will print 0 | ||
| 19 | ``` | ||
| 20 | <YueDisplay> | ||
| 21 | |||
| 22 | ```yue | ||
| 23 | i = 100 | ||
| 24 | |||
| 25 | -- many lines of code... | ||
| 26 | |||
| 27 | my_func = -> | ||
| 28 | i = 10 | ||
| 29 | while i > 0 | ||
| 30 | print i | ||
| 31 | i -= 1 | ||
| 32 | |||
| 33 | my_func! | ||
| 34 | |||
| 35 | print i -- will print 0 | ||
| 36 | ``` | ||
| 37 | |||
| 38 | </YueDisplay> | ||
| 39 | |||
| 40 | In my_func, we've overwritten the value of i mistakenly. In this example it is quite obvious, but consider a large, or foreign code base where it isn't clear what names have already been declared. | ||
| 41 | |||
| 42 | It would be helpful to say which variables from the enclosing scope we intend on change, in order to prevent us from changing others by accident. | ||
| 43 | |||
| 44 | The using keyword lets us do that. using nil makes sure that no closed variables are overwritten in assignment. The using clause is placed after the argument list in a function, or in place of it if there are no arguments. | ||
| 45 | |||
| 46 | ```yuescript | ||
| 47 | i = 100 | ||
| 48 | |||
| 49 | my_func = (using nil) -> | ||
| 50 | i = "hello" -- a new local variable is created here | ||
| 51 | |||
| 52 | my_func! | ||
| 53 | print i -- prints 100, i is unaffected | ||
| 54 | ``` | ||
| 55 | <YueDisplay> | ||
| 56 | |||
| 57 | ```yue | ||
| 58 | i = 100 | ||
| 59 | |||
| 60 | my_func = (using nil) -> | ||
| 61 | i = "hello" -- a new local variable is created here | ||
| 62 | |||
| 63 | my_func! | ||
| 64 | print i -- prints 100, i is unaffected | ||
| 65 | ``` | ||
| 66 | |||
| 67 | </YueDisplay> | ||
| 68 | |||
| 69 | Multiple names can be separated by commas. Closure values can still be accessed, they just cant be modified: | ||
| 70 | |||
| 71 | ```yuescript | ||
| 72 | tmp = 1213 | ||
| 73 | i, k = 100, 50 | ||
| 74 | |||
| 75 | my_func = (add using k, i) -> | ||
| 76 | tmp = tmp + add -- a new local tmp is created | ||
| 77 | i += tmp | ||
| 78 | k += tmp | ||
| 79 | |||
| 80 | my_func(22) | ||
| 81 | print i, k -- these have been updated | ||
| 82 | ``` | ||
| 83 | <YueDisplay> | ||
| 84 | |||
| 85 | ```yue | ||
| 86 | tmp = 1213 | ||
| 87 | i, k = 100, 50 | ||
| 88 | |||
| 89 | my_func = (add using k, i) -> | ||
| 90 | tmp = tmp + add -- a new local tmp is created | ||
| 91 | i += tmp | ||
| 92 | k += tmp | ||
| 93 | |||
| 94 | my_func(22) | ||
| 95 | print i, k -- these have been updated | ||
| 96 | ``` | ||
| 97 | |||
| 98 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/doc/the-yuescript-library.md b/doc/docs/pt-br/doc/the-yuescript-library.md new file mode 100644 index 0000000..3761755 --- /dev/null +++ b/doc/docs/pt-br/doc/the-yuescript-library.md | |||
| @@ -0,0 +1,821 @@ | |||
| 1 | # The YueScript Library | ||
| 2 | |||
| 3 | Access it by `local yue = require("yue")` in Lua. | ||
| 4 | |||
| 5 | ## yue | ||
| 6 | |||
| 7 | **Description:** | ||
| 8 | |||
| 9 | The YueScript language library. | ||
| 10 | |||
| 11 | ### version | ||
| 12 | |||
| 13 | **Type:** Field. | ||
| 14 | |||
| 15 | **Description:** | ||
| 16 | |||
| 17 | The YueScript version. | ||
| 18 | |||
| 19 | **Signature:** | ||
| 20 | ```lua | ||
| 21 | version: string | ||
| 22 | ``` | ||
| 23 | |||
| 24 | ### dirsep | ||
| 25 | |||
| 26 | **Type:** Field. | ||
| 27 | |||
| 28 | **Description:** | ||
| 29 | |||
| 30 | The file separator for the current platform. | ||
| 31 | |||
| 32 | **Signature:** | ||
| 33 | ```lua | ||
| 34 | dirsep: string | ||
| 35 | ``` | ||
| 36 | |||
| 37 | ### yue_compiled | ||
| 38 | |||
| 39 | **Type:** Field. | ||
| 40 | |||
| 41 | **Description:** | ||
| 42 | |||
| 43 | The compiled module code cache. | ||
| 44 | |||
| 45 | **Signature:** | ||
| 46 | ```lua | ||
| 47 | yue_compiled: {string: string} | ||
| 48 | ``` | ||
| 49 | |||
| 50 | ### to_lua | ||
| 51 | |||
| 52 | **Type:** Function. | ||
| 53 | |||
| 54 | **Description:** | ||
| 55 | |||
| 56 | The YueScript compiling function. It compiles the YueScript code to Lua code. | ||
| 57 | |||
| 58 | **Signature:** | ||
| 59 | ```lua | ||
| 60 | to_lua: function(code: string, config?: Config): | ||
| 61 | --[[codes]] string | nil, | ||
| 62 | --[[error]] string | nil, | ||
| 63 | --[[globals]] {{string, integer, integer}} | nil | ||
| 64 | ``` | ||
| 65 | |||
| 66 | **Parameters:** | ||
| 67 | |||
| 68 | | Parameter | Type | Description | | ||
| 69 | | --- | --- | --- | | ||
| 70 | | code | string | The YueScript code. | | ||
| 71 | | config | Config | [Optional] The compiler options. | | ||
| 72 | |||
| 73 | **Returns:** | ||
| 74 | |||
| 75 | | Return Type | Description | | ||
| 76 | | --- | --- | | ||
| 77 | | string \| nil | The compiled Lua code, or nil if the compilation failed. | | ||
| 78 | | string \| nil | The error message, or nil if the compilation succeeded. | | ||
| 79 | | {{string, integer, integer}} \| nil | The global variables appearing in the code (with name, row and column), or nil if the compiler option `lint_global` is false. | | ||
| 80 | |||
| 81 | ### file_exist | ||
| 82 | |||
| 83 | **Type:** Function. | ||
| 84 | |||
| 85 | **Description:** | ||
| 86 | |||
| 87 | The source file existence checking function. Can be overridden to customize the behavior. | ||
| 88 | |||
| 89 | **Signature:** | ||
| 90 | ```lua | ||
| 91 | file_exist: function(filename: string): boolean | ||
| 92 | ``` | ||
| 93 | |||
| 94 | **Parameters:** | ||
| 95 | |||
| 96 | | Parameter | Type | Description | | ||
| 97 | | --- | --- | --- | | ||
| 98 | | filename | string | The file name. | | ||
| 99 | |||
| 100 | **Returns:** | ||
| 101 | |||
| 102 | | Return Type | Description | | ||
| 103 | | --- | --- | | ||
| 104 | | boolean | Whether the file exists. | | ||
| 105 | |||
| 106 | ### read_file | ||
| 107 | |||
| 108 | **Type:** Function. | ||
| 109 | |||
| 110 | **Description:** | ||
| 111 | |||
| 112 | The source file reading function. Can be overridden to customize the behavior. | ||
| 113 | |||
| 114 | **Signature:** | ||
| 115 | ```lua | ||
| 116 | read_file: function(filename: string): string | ||
| 117 | ``` | ||
| 118 | |||
| 119 | **Parameters:** | ||
| 120 | |||
| 121 | | Parameter | Type | Description | | ||
| 122 | | --- | --- | --- | | ||
| 123 | | filename | string | The file name. | | ||
| 124 | |||
| 125 | **Returns:** | ||
| 126 | |||
| 127 | | Return Type | Description | | ||
| 128 | | --- | --- | | ||
| 129 | | string | The file content. | | ||
| 130 | |||
| 131 | ### insert_loader | ||
| 132 | |||
| 133 | **Type:** Function. | ||
| 134 | |||
| 135 | **Description:** | ||
| 136 | |||
| 137 | Insert the YueScript loader to the package loaders (searchers). | ||
| 138 | |||
| 139 | **Signature:** | ||
| 140 | ```lua | ||
| 141 | insert_loader: function(pos?: integer): boolean | ||
| 142 | ``` | ||
| 143 | |||
| 144 | **Parameters:** | ||
| 145 | |||
| 146 | | Parameter | Type | Description | | ||
| 147 | | --- | --- | --- | | ||
| 148 | | pos | integer | [Optional] The position to insert the loader. Default is 3. | | ||
| 149 | |||
| 150 | **Returns:** | ||
| 151 | |||
| 152 | | Return Type | Description | | ||
| 153 | | --- | --- | | ||
| 154 | | boolean | Whether the loader is inserted successfully. It will fail if the loader is already inserted. | | ||
| 155 | |||
| 156 | ### remove_loader | ||
| 157 | |||
| 158 | **Type:** Function. | ||
| 159 | |||
| 160 | **Description:** | ||
| 161 | |||
| 162 | Remove the YueScript loader from the package loaders (searchers). | ||
| 163 | |||
| 164 | **Signature:** | ||
| 165 | ```lua | ||
| 166 | remove_loader: function(): boolean | ||
| 167 | ``` | ||
| 168 | |||
| 169 | **Returns:** | ||
| 170 | |||
| 171 | | Return Type | Description | | ||
| 172 | | --- | --- | | ||
| 173 | | boolean | Whether the loader is removed successfully. It will fail if the loader is not inserted. | | ||
| 174 | |||
| 175 | ### loadstring | ||
| 176 | |||
| 177 | **Type:** Function. | ||
| 178 | |||
| 179 | **Description:** | ||
| 180 | |||
| 181 | Loads YueScript code from a string into a function. | ||
| 182 | |||
| 183 | **Signature:** | ||
| 184 | ```lua | ||
| 185 | loadstring: function(input: string, chunkname: string, env: table, config?: Config): | ||
| 186 | --[[loaded function]] nil | function(...: any): (any...), | ||
| 187 | --[[error]] string | nil | ||
| 188 | ``` | ||
| 189 | |||
| 190 | **Parameters:** | ||
| 191 | |||
| 192 | | Parameter | Type | Description | | ||
| 193 | | --- | --- | --- | | ||
| 194 | | input | string | The YueScript code. | | ||
| 195 | | chunkname | string | The name of the code chunk. | | ||
| 196 | | env | table | The environment table. | | ||
| 197 | | config | Config | [Optional] The compiler options. | | ||
| 198 | |||
| 199 | **Returns:** | ||
| 200 | |||
| 201 | | Return Type | Description | | ||
| 202 | | --- | --- | | ||
| 203 | | function \| nil | The loaded function, or nil if the loading failed. | | ||
| 204 | | string \| nil | The error message, or nil if the loading succeeded. | | ||
| 205 | |||
| 206 | ### loadstring | ||
| 207 | |||
| 208 | **Type:** Function. | ||
| 209 | |||
| 210 | **Description:** | ||
| 211 | |||
| 212 | Loads YueScript code from a string into a function. | ||
| 213 | |||
| 214 | **Signature:** | ||
| 215 | ```lua | ||
| 216 | loadstring: function(input: string, chunkname: string, config?: Config): | ||
| 217 | --[[loaded function]] nil | function(...: any): (any...), | ||
| 218 | --[[error]] string | nil | ||
| 219 | ``` | ||
| 220 | |||
| 221 | **Parameters:** | ||
| 222 | |||
| 223 | | Parameter | Type | Description | | ||
| 224 | | --- | --- | --- | | ||
| 225 | | input | string | The YueScript code. | | ||
| 226 | | chunkname | string | The name of the code chunk. | | ||
| 227 | | config | Config | [Optional] The compiler options. | | ||
| 228 | |||
| 229 | **Returns:** | ||
| 230 | |||
| 231 | | Return Type | Description | | ||
| 232 | | --- | --- | | ||
| 233 | | function \| nil | The loaded function, or nil if the loading failed. | | ||
| 234 | | string \| nil | The error message, or nil if the loading succeeded. | | ||
| 235 | |||
| 236 | ### loadstring | ||
| 237 | |||
| 238 | **Type:** Function. | ||
| 239 | |||
| 240 | **Description:** | ||
| 241 | |||
| 242 | Loads YueScript code from a string into a function. | ||
| 243 | |||
| 244 | **Signature:** | ||
| 245 | ```lua | ||
| 246 | loadstring: function(input: string, config?: Config): | ||
| 247 | --[[loaded function]] nil | function(...: any): (any...), | ||
| 248 | --[[error]] string | nil | ||
| 249 | ``` | ||
| 250 | |||
| 251 | **Parameters:** | ||
| 252 | |||
| 253 | | Parameter | Type | Description | | ||
| 254 | | --- | --- | --- | | ||
| 255 | | input | string | The YueScript code. | | ||
| 256 | | config | Config | [Optional] The compiler options. | | ||
| 257 | |||
| 258 | **Returns:** | ||
| 259 | |||
| 260 | | Return Type | Description | | ||
| 261 | | --- | --- | | ||
| 262 | | function \| nil | The loaded function, or nil if the loading failed. | | ||
| 263 | | string \| nil | The error message, or nil if the loading succeeded. | | ||
| 264 | |||
| 265 | ### loadfile | ||
| 266 | |||
| 267 | **Type:** Function. | ||
| 268 | |||
| 269 | **Description:** | ||
| 270 | |||
| 271 | Loads YueScript code from a file into a function. | ||
| 272 | |||
| 273 | **Signature:** | ||
| 274 | ```lua | ||
| 275 | loadfile: function(filename: string, env: table, config?: Config): | ||
| 276 | nil | function(...: any): (any...), | ||
| 277 | string | nil | ||
| 278 | ``` | ||
| 279 | |||
| 280 | **Parameters:** | ||
| 281 | |||
| 282 | | Parameter | Type | Description | | ||
| 283 | | --- | --- | --- | | ||
| 284 | | filename | string | The file name. | | ||
| 285 | | env | table | The environment table. | | ||
| 286 | | config | Config | [Optional] The compiler options. | | ||
| 287 | |||
| 288 | **Returns:** | ||
| 289 | |||
| 290 | | Return Type | Description | | ||
| 291 | | --- | --- | | ||
| 292 | | function \| nil | The loaded function, or nil if the loading failed. | | ||
| 293 | | string \| nil | The error message, or nil if the loading succeeded. | | ||
| 294 | |||
| 295 | ### loadfile | ||
| 296 | |||
| 297 | **Type:** Function. | ||
| 298 | |||
| 299 | **Description:** | ||
| 300 | |||
| 301 | Loads YueScript code from a file into a function. | ||
| 302 | |||
| 303 | **Signature:** | ||
| 304 | ```lua | ||
| 305 | loadfile: function(filename: string, config?: Config): | ||
| 306 | nil | function(...: any): (any...), | ||
| 307 | string | nil | ||
| 308 | ``` | ||
| 309 | |||
| 310 | **Parameters:** | ||
| 311 | |||
| 312 | | Parameter | Type | Description | | ||
| 313 | | --- | --- | --- | | ||
| 314 | | filename | string | The file name. | | ||
| 315 | | config | Config | [Optional] The compiler options. | | ||
| 316 | |||
| 317 | **Returns:** | ||
| 318 | |||
| 319 | | Return Type | Description | | ||
| 320 | | --- | --- | | ||
| 321 | | function \| nil | The loaded function, or nil if the loading failed. | | ||
| 322 | | string \| nil | The error message, or nil if the loading succeeded. | | ||
| 323 | |||
| 324 | ### dofile | ||
| 325 | |||
| 326 | **Type:** Function. | ||
| 327 | |||
| 328 | **Description:** | ||
| 329 | |||
| 330 | Loads YueScript code from a file into a function and executes it. | ||
| 331 | |||
| 332 | **Signature:** | ||
| 333 | ```lua | ||
| 334 | dofile: function(filename: string, env: table, config?: Config): any... | ||
| 335 | ``` | ||
| 336 | |||
| 337 | **Parameters:** | ||
| 338 | |||
| 339 | | Parameter | Type | Description | | ||
| 340 | | --- | --- | --- | | ||
| 341 | | filename | string | The file name. | | ||
| 342 | | env | table | The environment table. | | ||
| 343 | | config | Config | [Optional] The compiler options. | | ||
| 344 | |||
| 345 | **Returns:** | ||
| 346 | |||
| 347 | | Return Type | Description | | ||
| 348 | | --- | --- | | ||
| 349 | | any... | The return values of the loaded function. | | ||
| 350 | |||
| 351 | ### dofile | ||
| 352 | |||
| 353 | **Type:** Function. | ||
| 354 | |||
| 355 | **Description:** | ||
| 356 | |||
| 357 | Loads YueScript code from a file into a function and executes it. | ||
| 358 | |||
| 359 | **Signature:** | ||
| 360 | ```lua | ||
| 361 | dofile: function(filename: string, config?: Config): any... | ||
| 362 | ``` | ||
| 363 | |||
| 364 | **Parameters:** | ||
| 365 | |||
| 366 | | Parameter | Type | Description | | ||
| 367 | | --- | --- | --- | | ||
| 368 | | filename | string | The file name. | | ||
| 369 | | config | Config | [Optional] The compiler options. | | ||
| 370 | |||
| 371 | **Returns:** | ||
| 372 | |||
| 373 | | Return Type | Description | | ||
| 374 | | --- | --- | | ||
| 375 | | any... | The return values of the loaded function. | | ||
| 376 | |||
| 377 | ### find_modulepath | ||
| 378 | |||
| 379 | **Type:** Function. | ||
| 380 | |||
| 381 | **Description:** | ||
| 382 | |||
| 383 | Resolves the YueScript module name to the file path. | ||
| 384 | |||
| 385 | **Signature:** | ||
| 386 | ```lua | ||
| 387 | find_modulepath: function(name: string): string | ||
| 388 | ``` | ||
| 389 | |||
| 390 | **Parameters:** | ||
| 391 | |||
| 392 | | Parameter | Type | Description | | ||
| 393 | | --- | --- | --- | | ||
| 394 | | name | string | The module name. | | ||
| 395 | |||
| 396 | **Returns:** | ||
| 397 | |||
| 398 | | Return Type | Description | | ||
| 399 | | --- | --- | | ||
| 400 | | string | The file path. | | ||
| 401 | |||
| 402 | ### pcall | ||
| 403 | |||
| 404 | **Type:** Function. | ||
| 405 | |||
| 406 | **Description:** | ||
| 407 | |||
| 408 | Calls a function in protected mode. | ||
| 409 | Catches any errors and returns a status code and results or error object. | ||
| 410 | Rewrites the error line number to the original line number in the YueScript code when errors occur. | ||
| 411 | |||
| 412 | **Signature:** | ||
| 413 | ```lua | ||
| 414 | pcall: function(f: function, ...: any): boolean, any... | ||
| 415 | ``` | ||
| 416 | |||
| 417 | **Parameters:** | ||
| 418 | |||
| 419 | | Parameter | Type | Description | | ||
| 420 | | --- | --- | --- | | ||
| 421 | | f | function | The function to call. | | ||
| 422 | | ... | any | Arguments to pass to the function. | | ||
| 423 | |||
| 424 | **Returns:** | ||
| 425 | |||
| 426 | | Return Type | Description | | ||
| 427 | | --- | --- | | ||
| 428 | | boolean, ... | Status code and function results or error object. | | ||
| 429 | |||
| 430 | ### require | ||
| 431 | |||
| 432 | **Type:** Function. | ||
| 433 | |||
| 434 | **Description:** | ||
| 435 | |||
| 436 | Loads a given module. Can be either a Lua module or a YueScript module. | ||
| 437 | Rewrites the error line number to the original line number in the YueScript code if the module is a YueScript module and loading fails. | ||
| 438 | |||
| 439 | **Signature:** | ||
| 440 | ```lua | ||
| 441 | require: function(name: string): any... | ||
| 442 | ``` | ||
| 443 | |||
| 444 | **Parameters:** | ||
| 445 | |||
| 446 | | Parameter | Type | Description | | ||
| 447 | | --- | --- | --- | | ||
| 448 | | modname | string | The name of the module to load. | | ||
| 449 | |||
| 450 | **Returns:** | ||
| 451 | |||
| 452 | | Return Type | Description | | ||
| 453 | | --- | --- | | ||
| 454 | | any | The value stored at package.loaded[modname] if the module is already loaded.Otherwise, tries to find a loader and returns the final value of package.loaded[modname] and a loader data as a second result. | | ||
| 455 | |||
| 456 | ### p | ||
| 457 | |||
| 458 | **Type:** Function. | ||
| 459 | |||
| 460 | **Description:** | ||
| 461 | |||
| 462 | Inspects the structures of the passed values and prints string representations. | ||
| 463 | |||
| 464 | **Signature:** | ||
| 465 | ```lua | ||
| 466 | p: function(...: any) | ||
| 467 | ``` | ||
| 468 | |||
| 469 | **Parameters:** | ||
| 470 | |||
| 471 | | Parameter | Type | Description | | ||
| 472 | | --- | --- | --- | | ||
| 473 | | ... | any | The values to inspect. | | ||
| 474 | |||
| 475 | ### options | ||
| 476 | |||
| 477 | **Type:** Field. | ||
| 478 | |||
| 479 | **Description:** | ||
| 480 | |||
| 481 | The current compiler options. | ||
| 482 | |||
| 483 | **Signature:** | ||
| 484 | ```lua | ||
| 485 | options: Config.Options | ||
| 486 | ``` | ||
| 487 | |||
| 488 | ### traceback | ||
| 489 | |||
| 490 | **Type:** Function. | ||
| 491 | |||
| 492 | **Description:** | ||
| 493 | |||
| 494 | The traceback function that rewrites the stack trace line numbers to the original line numbers in the YueScript code. | ||
| 495 | |||
| 496 | **Signature:** | ||
| 497 | ```lua | ||
| 498 | traceback: function(message: string): string | ||
| 499 | ``` | ||
| 500 | |||
| 501 | **Parameters:** | ||
| 502 | |||
| 503 | | Parameter | Type | Description | | ||
| 504 | | --- | --- | --- | | ||
| 505 | | message | string | The traceback message. | | ||
| 506 | |||
| 507 | **Returns:** | ||
| 508 | |||
| 509 | | Return Type | Description | | ||
| 510 | | --- | --- | | ||
| 511 | | string | The rewritten traceback message. | | ||
| 512 | |||
| 513 | ### is_ast | ||
| 514 | |||
| 515 | **Type:** Function. | ||
| 516 | |||
| 517 | **Description:** | ||
| 518 | |||
| 519 | Checks whether the code matches the specified AST. | ||
| 520 | |||
| 521 | **Signature:** | ||
| 522 | ```lua | ||
| 523 | is_ast: function(astName: string, code: string): boolean | ||
| 524 | ``` | ||
| 525 | |||
| 526 | **Parameters:** | ||
| 527 | |||
| 528 | | Parameter | Type | Description | | ||
| 529 | | --- | --- | --- | | ||
| 530 | | astName | string | The AST name. | | ||
| 531 | | code | string | The code. | | ||
| 532 | |||
| 533 | **Returns:** | ||
| 534 | |||
| 535 | | Return Type | Description | | ||
| 536 | | --- | --- | | ||
| 537 | | boolean | Whether the code matches the AST. | | ||
| 538 | |||
| 539 | ### AST | ||
| 540 | |||
| 541 | **Type:** Field. | ||
| 542 | |||
| 543 | **Description:** | ||
| 544 | |||
| 545 | The AST type definition with name, row, column and sub nodes. | ||
| 546 | |||
| 547 | **Signature:** | ||
| 548 | ```lua | ||
| 549 | type AST = {string, integer, integer, any} | ||
| 550 | ``` | ||
| 551 | |||
| 552 | ### to_ast | ||
| 553 | |||
| 554 | **Type:** Function. | ||
| 555 | |||
| 556 | **Description:** | ||
| 557 | |||
| 558 | Converts the code to the AST. | ||
| 559 | |||
| 560 | **Signature:** | ||
| 561 | ```lua | ||
| 562 | to_ast: function(code: string, flattenLevel?: number, astName?: string, reserveComment?: boolean): | ||
| 563 | --[[AST]] AST | nil, | ||
| 564 | --[[error]] nil | string | ||
| 565 | ``` | ||
| 566 | |||
| 567 | **Parameters:** | ||
| 568 | |||
| 569 | | Parameter | Type | Description | | ||
| 570 | | --- | --- | --- | | ||
| 571 | | code | string | The code. | | ||
| 572 | | flattenLevel | integer | [Optional] The flatten level. Higher level means more flattening. Default is 0. Maximum is 2. | | ||
| 573 | | astName | string | [Optional] The AST name. Default is "File". | | ||
| 574 | | reserveComment | boolean | [Optional] Whether to reserve the original comments. Default is false. | | ||
| 575 | |||
| 576 | **Returns:** | ||
| 577 | |||
| 578 | | Return Type | Description | | ||
| 579 | | --- | --- | | ||
| 580 | | AST \| nil | The AST, or nil if the conversion failed. | | ||
| 581 | | string \| nil | The error message, or nil if the conversion succeeded. | | ||
| 582 | |||
| 583 | ### format | ||
| 584 | |||
| 585 | **Type:** Function. | ||
| 586 | |||
| 587 | **Description:** | ||
| 588 | |||
| 589 | Formats the YueScript code. | ||
| 590 | |||
| 591 | **Signature:** | ||
| 592 | ```lua | ||
| 593 | format: function(code: string, tabSize?: number, reserveComment?: boolean): string | ||
| 594 | ``` | ||
| 595 | |||
| 596 | **Parameters:** | ||
| 597 | |||
| 598 | | Parameter | Type | Description | | ||
| 599 | | --- | --- | --- | | ||
| 600 | | code | string | The code. | | ||
| 601 | | tabSize | integer | [Optional] The tab size. Default is 4. | | ||
| 602 | | reserveComment | boolean | [Optional] Whether to reserve the original comments. Default is true. | | ||
| 603 | |||
| 604 | **Returns:** | ||
| 605 | |||
| 606 | | Return Type | Description | | ||
| 607 | | --- | --- | | ||
| 608 | | string | The formatted code. | | ||
| 609 | |||
| 610 | ### __call | ||
| 611 | |||
| 612 | **Type:** Metamethod. | ||
| 613 | |||
| 614 | **Description:** | ||
| 615 | |||
| 616 | Requires the YueScript module. | ||
| 617 | Rewrites the error line number to the original line number in the YueScript code when loading fails. | ||
| 618 | |||
| 619 | **Signature:** | ||
| 620 | ```lua | ||
| 621 | metamethod __call: function(self: yue, module: string): any... | ||
| 622 | ``` | ||
| 623 | |||
| 624 | **Parameters:** | ||
| 625 | |||
| 626 | | Parameter | Type | Description | | ||
| 627 | | --- | --- | --- | | ||
| 628 | | module | string | The module name. | | ||
| 629 | |||
| 630 | **Returns:** | ||
| 631 | |||
| 632 | | Return Type | Description | | ||
| 633 | | --- | --- | | ||
| 634 | | any | The module value. | | ||
| 635 | |||
| 636 | ## Config | ||
| 637 | |||
| 638 | **Description:** | ||
| 639 | |||
| 640 | The compiler compile options. | ||
| 641 | |||
| 642 | ### lint_global | ||
| 643 | |||
| 644 | **Type:** Field. | ||
| 645 | |||
| 646 | **Description:** | ||
| 647 | |||
| 648 | Whether the compiler should collect the global variables appearing in the code. | ||
| 649 | |||
| 650 | **Signature:** | ||
| 651 | ```lua | ||
| 652 | lint_global: boolean | ||
| 653 | ``` | ||
| 654 | |||
| 655 | ### implicit_return_root | ||
| 656 | |||
| 657 | **Type:** Field. | ||
| 658 | |||
| 659 | **Description:** | ||
| 660 | |||
| 661 | Whether the compiler should do an implicit return for the root code block. | ||
| 662 | |||
| 663 | **Signature:** | ||
| 664 | ```lua | ||
| 665 | implicit_return_root: boolean | ||
| 666 | ``` | ||
| 667 | |||
| 668 | ### reserve_line_number | ||
| 669 | |||
| 670 | **Type:** Field. | ||
| 671 | |||
| 672 | **Description:** | ||
| 673 | |||
| 674 | Whether the compiler should reserve the original line number in the compiled code. | ||
| 675 | |||
| 676 | **Signature:** | ||
| 677 | ```lua | ||
| 678 | reserve_line_number: boolean | ||
| 679 | ``` | ||
| 680 | |||
| 681 | ### reserve_comment | ||
| 682 | |||
| 683 | **Type:** Field. | ||
| 684 | |||
| 685 | **Description:** | ||
| 686 | |||
| 687 | Whether the compiler should reserve the original comments in the compiled code. | ||
| 688 | |||
| 689 | **Signature:** | ||
| 690 | ```lua | ||
| 691 | reserve_comment: boolean | ||
| 692 | ``` | ||
| 693 | |||
| 694 | ### space_over_tab | ||
| 695 | |||
| 696 | **Type:** Field. | ||
| 697 | |||
| 698 | **Description:** | ||
| 699 | |||
| 700 | Whether the compiler should use the space character instead of the tab character in the compiled code. | ||
| 701 | |||
| 702 | **Signature:** | ||
| 703 | ```lua | ||
| 704 | space_over_tab: boolean | ||
| 705 | ``` | ||
| 706 | |||
| 707 | ### same_module | ||
| 708 | |||
| 709 | **Type:** Field. | ||
| 710 | |||
| 711 | **Description:** | ||
| 712 | |||
| 713 | Whether the compiler should treat the code to be compiled as the same currently being compiled module. For internal use only. | ||
| 714 | |||
| 715 | **Signature:** | ||
| 716 | ```lua | ||
| 717 | same_module: boolean | ||
| 718 | ``` | ||
| 719 | |||
| 720 | ### line_offset | ||
| 721 | |||
| 722 | **Type:** Field. | ||
| 723 | |||
| 724 | **Description:** | ||
| 725 | |||
| 726 | Whether the compiler error message should include the line number offset. For internal use only. | ||
| 727 | |||
| 728 | **Signature:** | ||
| 729 | ```lua | ||
| 730 | line_offset: integer | ||
| 731 | ``` | ||
| 732 | |||
| 733 | ### yue.Config.LuaTarget | ||
| 734 | |||
| 735 | **Type:** Enumeration. | ||
| 736 | |||
| 737 | **Description:** | ||
| 738 | |||
| 739 | The target Lua version enumeration. | ||
| 740 | |||
| 741 | **Signature:** | ||
| 742 | ```lua | ||
| 743 | enum LuaTarget | ||
| 744 | "5.1" | ||
| 745 | "5.2" | ||
| 746 | "5.3" | ||
| 747 | "5.4" | ||
| 748 | "5.5" | ||
| 749 | end | ||
| 750 | ``` | ||
| 751 | |||
| 752 | ### options | ||
| 753 | |||
| 754 | **Type:** Field. | ||
| 755 | |||
| 756 | **Description:** | ||
| 757 | |||
| 758 | The extra options to be passed to the compilation function. | ||
| 759 | |||
| 760 | **Signature:** | ||
| 761 | ```lua | ||
| 762 | options: Options | ||
| 763 | ``` | ||
| 764 | |||
| 765 | ## Options | ||
| 766 | |||
| 767 | **Description:** | ||
| 768 | |||
| 769 | The extra compiler options definition. | ||
| 770 | |||
| 771 | ### target | ||
| 772 | |||
| 773 | **Type:** Field. | ||
| 774 | |||
| 775 | **Description:** | ||
| 776 | |||
| 777 | The target Lua version for the compilation. | ||
| 778 | |||
| 779 | **Signature:** | ||
| 780 | ```lua | ||
| 781 | target: LuaTarget | ||
| 782 | ``` | ||
| 783 | |||
| 784 | ### path | ||
| 785 | |||
| 786 | **Type:** Field. | ||
| 787 | |||
| 788 | **Description:** | ||
| 789 | |||
| 790 | The extra module search path. | ||
| 791 | |||
| 792 | **Signature:** | ||
| 793 | ```lua | ||
| 794 | path: string | ||
| 795 | ``` | ||
| 796 | |||
| 797 | ### dump_locals | ||
| 798 | |||
| 799 | **Type:** Field. | ||
| 800 | |||
| 801 | **Description:** | ||
| 802 | |||
| 803 | Whether to dump the local variables in the traceback error message. Default is false. | ||
| 804 | |||
| 805 | **Signature:** | ||
| 806 | ```lua | ||
| 807 | dump_locals: boolean | ||
| 808 | ``` | ||
| 809 | |||
| 810 | ### simplified | ||
| 811 | |||
| 812 | **Type:** Field. | ||
| 813 | |||
| 814 | **Description:** | ||
| 815 | |||
| 816 | Whether to simplify the error message. Default is true. | ||
| 817 | |||
| 818 | **Signature:** | ||
| 819 | ```lua | ||
| 820 | simplified: boolean | ||
| 821 | ``` | ||
diff --git a/doc/docs/pt-br/doc/try.md b/doc/docs/pt-br/doc/try.md new file mode 100644 index 0000000..23c7877 --- /dev/null +++ b/doc/docs/pt-br/doc/try.md | |||
| @@ -0,0 +1,105 @@ | |||
| 1 | # Try | ||
| 2 | |||
| 3 | The syntax for Lua error handling in a common form. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | try | ||
| 7 | func 1, 2, 3 | ||
| 8 | catch err | ||
| 9 | print yue.traceback err | ||
| 10 | |||
| 11 | success, result = try | ||
| 12 | func 1, 2, 3 | ||
| 13 | catch err | ||
| 14 | yue.traceback err | ||
| 15 | |||
| 16 | try func 1, 2, 3 | ||
| 17 | catch err | ||
| 18 | print yue.traceback err | ||
| 19 | |||
| 20 | success, result = try func 1, 2, 3 | ||
| 21 | |||
| 22 | try | ||
| 23 | print "trying" | ||
| 24 | func 1, 2, 3 | ||
| 25 | |||
| 26 | -- working with if assignment pattern | ||
| 27 | if success, result := try func 1, 2, 3 | ||
| 28 | catch err | ||
| 29 | print yue.traceback err | ||
| 30 | print result | ||
| 31 | ``` | ||
| 32 | <YueDisplay> | ||
| 33 | |||
| 34 | ```yue | ||
| 35 | try | ||
| 36 | func 1, 2, 3 | ||
| 37 | catch err | ||
| 38 | print yue.traceback err | ||
| 39 | |||
| 40 | success, result = try | ||
| 41 | func 1, 2, 3 | ||
| 42 | catch err | ||
| 43 | yue.traceback err | ||
| 44 | |||
| 45 | try func 1, 2, 3 | ||
| 46 | catch err | ||
| 47 | print yue.traceback err | ||
| 48 | |||
| 49 | success, result = try func 1, 2, 3 | ||
| 50 | |||
| 51 | try | ||
| 52 | print "trying" | ||
| 53 | func 1, 2, 3 | ||
| 54 | |||
| 55 | -- working with if assignment pattern | ||
| 56 | if success, result := try func 1, 2, 3 | ||
| 57 | catch err | ||
| 58 | print yue.traceback err | ||
| 59 | print result | ||
| 60 | ``` | ||
| 61 | |||
| 62 | </YueDisplay> | ||
| 63 | |||
| 64 | ## Try? | ||
| 65 | |||
| 66 | `try?` is a simplified use for error handling syntax that omit the boolean status from the `try` statement, and it will return the result from the try block when success, return nil instead of error object otherwise. | ||
| 67 | |||
| 68 | ```yuescript | ||
| 69 | a, b, c = try? func! | ||
| 70 | |||
| 71 | -- with nil coalescing operator | ||
| 72 | a = (try? func!) ?? "default" | ||
| 73 | |||
| 74 | -- as function argument | ||
| 75 | f try? func! | ||
| 76 | |||
| 77 | -- with catch block | ||
| 78 | f try? | ||
| 79 | print 123 | ||
| 80 | func! | ||
| 81 | catch e | ||
| 82 | print e | ||
| 83 | e | ||
| 84 | ``` | ||
| 85 | <YueDisplay> | ||
| 86 | |||
| 87 | ```yue | ||
| 88 | a, b, c = try? func! | ||
| 89 | |||
| 90 | -- with nil coalescing operator | ||
| 91 | a = (try? func!) ?? "default" | ||
| 92 | |||
| 93 | -- as function argument | ||
| 94 | f try? func! | ||
| 95 | |||
| 96 | -- with catch block | ||
| 97 | f try? | ||
| 98 | print 123 | ||
| 99 | func! | ||
| 100 | catch e | ||
| 101 | print e | ||
| 102 | e | ||
| 103 | ``` | ||
| 104 | |||
| 105 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/doc/usage.md b/doc/docs/pt-br/doc/usage.md new file mode 100644 index 0000000..45161c6 --- /dev/null +++ b/doc/docs/pt-br/doc/usage.md | |||
| @@ -0,0 +1,111 @@ | |||
| 1 | # Usage | ||
| 2 | |||
| 3 | ## Lua Module | ||
| 4 | |||
| 5 | Use YueScript module in Lua: | ||
| 6 | |||
| 7 | * **Case 1** | ||
| 8 | |||
| 9 | Require "your_yuescript_entry.yue" in Lua. | ||
| 10 | ```Lua | ||
| 11 | require("yue")("your_yuescript_entry") | ||
| 12 | ``` | ||
| 13 | And this code still works when you compile "your_yuescript_entry.yue" to "your_yuescript_entry.lua" in the same path. In the rest YueScript files just use the normal **require** or **import**. The code line numbers in error messages will also be handled correctly. | ||
| 14 | |||
| 15 | * **Case 2** | ||
| 16 | |||
| 17 | Require YueScript module and rewite message by hand. | ||
| 18 | |||
| 19 | ```lua | ||
| 20 | local yue = require("yue") | ||
| 21 | yue.insert_loader() | ||
| 22 | local success, result = xpcall(function() | ||
| 23 | return require("yuescript_module_name") | ||
| 24 | end, function(err) | ||
| 25 | return yue.traceback(err) | ||
| 26 | end) | ||
| 27 | ``` | ||
| 28 | |||
| 29 | * **Case 3** | ||
| 30 | |||
| 31 | Use the YueScript compiler function in Lua. | ||
| 32 | |||
| 33 | ```lua | ||
| 34 | local yue = require("yue") | ||
| 35 | local codes, err, globals = yue.to_lua([[ | ||
| 36 | f = -> | ||
| 37 | print "hello world" | ||
| 38 | f! | ||
| 39 | ]],{ | ||
| 40 | implicit_return_root = true, | ||
| 41 | reserve_line_number = true, | ||
| 42 | lint_global = true, | ||
| 43 | space_over_tab = false, | ||
| 44 | options = { | ||
| 45 | target = "5.4", | ||
| 46 | path = "/script" | ||
| 47 | } | ||
| 48 | }) | ||
| 49 | ``` | ||
| 50 | |||
| 51 | ## YueScript Tool | ||
| 52 | |||
| 53 | Use YueScript tool with: | ||
| 54 | |||
| 55 | ```shell | ||
| 56 | > yue -h | ||
| 57 | Usage: yue | ||
| 58 | [options] [<file/directory>] ... | ||
| 59 | yue -e <code_or_file> [args...] | ||
| 60 | yue -w [<directory>] [options] | ||
| 61 | yue - | ||
| 62 | |||
| 63 | Notes: | ||
| 64 | - '-' / '--' must be the first and only argument. | ||
| 65 | - '-o/--output' can not be used with multiple input files. | ||
| 66 | - '-w/--watch' can not be used with file input (directory only). | ||
| 67 | - with '-e/--execute', remaining tokens are treated as script args. | ||
| 68 | |||
| 69 | Options: | ||
| 70 | -h, --help Show this help message and exit. | ||
| 71 | -e <str>, --execute <str> Execute a file or raw codes | ||
| 72 | -m, --minify Generate minified codes | ||
| 73 | -r, --rewrite Rewrite output to match original line numbers | ||
| 74 | -t <output_to>, --output-to <output_to> | ||
| 75 | Specify where to place compiled files | ||
| 76 | -o <file>, --output <file> Write output to file | ||
| 77 | -p, --print Write output to standard out | ||
| 78 | -b, --benchmark Dump compile time (doesn't write output) | ||
| 79 | -g, --globals Dump global variables used in NAME LINE COLUMN | ||
| 80 | -s, --spaces Use spaces in generated codes instead of tabs | ||
| 81 | -l, --line-numbers Write line numbers from source codes | ||
| 82 | -j, --no-implicit-return Disable implicit return at end of file | ||
| 83 | -c, --reserve-comments Reserve comments before statement from source codes | ||
| 84 | -w [<dir>], --watch [<dir>] | ||
| 85 | Watch changes and compile every file under directory | ||
| 86 | -v, --version Print version | ||
| 87 | - Read from standard in, print to standard out | ||
| 88 | (Must be first and only argument) | ||
| 89 | -- Same as '-' (kept for backward compatibility) | ||
| 90 | |||
| 91 | --target <version> Specify the Lua version that codes will be generated to | ||
| 92 | (version can only be 5.1 to 5.5) | ||
| 93 | --path <path_str> Append an extra Lua search path string to package.path | ||
| 94 | --<key>=<value> Pass compiler option in key=value form (existing behavior) | ||
| 95 | |||
| 96 | Execute without options to enter REPL, type symbol '$' | ||
| 97 | in a single line to start/stop multi-line mode | ||
| 98 | ``` | ||
| 99 | Use cases: | ||
| 100 | |||
| 101 | Recursively compile every YueScript file with extension **.yue** under current path: **yue .** | ||
| 102 | |||
| 103 | Compile and save results to a target path: **yue -t /target/path/ .** | ||
| 104 | |||
| 105 | Compile and reserve debug info: **yue -l .** | ||
| 106 | |||
| 107 | Compile and generate minified codes: **yue -m .** | ||
| 108 | |||
| 109 | Execute raw codes: **yue -e 'print 123'** | ||
| 110 | |||
| 111 | Execute a YueScript file: **yue -e main.yue** | ||
diff --git a/doc/docs/pt-br/doc/varargs-assignment.md b/doc/docs/pt-br/doc/varargs-assignment.md new file mode 100644 index 0000000..1d66680 --- /dev/null +++ b/doc/docs/pt-br/doc/varargs-assignment.md | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | # Varargs Assignment | ||
| 2 | |||
| 3 | You can assign the results returned from a function to a varargs symbol `...`. And then access its content using the Lua way. | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | list = [1, 2, 3, 4, 5] | ||
| 7 | fn = (ok) -> ok, table.unpack list | ||
| 8 | ok, ... = fn true | ||
| 9 | count = select '#', ... | ||
| 10 | first = select 1, ... | ||
| 11 | print ok, count, first | ||
| 12 | ``` | ||
| 13 | <YueDisplay> | ||
| 14 | |||
| 15 | ```yue | ||
| 16 | list = [1, 2, 3, 4, 5] | ||
| 17 | fn = (ok) -> ok, table.unpack list | ||
| 18 | ok, ... = fn true | ||
| 19 | count = select '#', ... | ||
| 20 | first = select 1, ... | ||
| 21 | print ok, count, first | ||
| 22 | ``` | ||
| 23 | |||
| 24 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/doc/while-loop.md b/doc/docs/pt-br/doc/while-loop.md new file mode 100644 index 0000000..502935e --- /dev/null +++ b/doc/docs/pt-br/doc/while-loop.md | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | # While Loop | ||
| 2 | |||
| 3 | The while loop also comes in four variations: | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | i = 10 | ||
| 7 | while i > 0 | ||
| 8 | print i | ||
| 9 | i -= 1 | ||
| 10 | |||
| 11 | while running == true do my_function! | ||
| 12 | ``` | ||
| 13 | <YueDisplay> | ||
| 14 | |||
| 15 | ```yue | ||
| 16 | i = 10 | ||
| 17 | while i > 0 | ||
| 18 | print i | ||
| 19 | i -= 1 | ||
| 20 | |||
| 21 | while running == true do my_function! | ||
| 22 | ``` | ||
| 23 | |||
| 24 | </YueDisplay> | ||
| 25 | |||
| 26 | ```yuescript | ||
| 27 | i = 10 | ||
| 28 | until i == 0 | ||
| 29 | print i | ||
| 30 | i -= 1 | ||
| 31 | |||
| 32 | until running == false do my_function! | ||
| 33 | ``` | ||
| 34 | <YueDisplay> | ||
| 35 | |||
| 36 | ```yue | ||
| 37 | i = 10 | ||
| 38 | until i == 0 | ||
| 39 | print i | ||
| 40 | i -= 1 | ||
| 41 | until running == false do my_function! | ||
| 42 | ``` | ||
| 43 | |||
| 44 | </YueDisplay> | ||
| 45 | |||
| 46 | 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. | ||
| 47 | |||
| 48 | ## Repeat Loop | ||
| 49 | |||
| 50 | The repeat loop comes from Lua: | ||
| 51 | |||
| 52 | ```yuescript | ||
| 53 | i = 10 | ||
| 54 | repeat | ||
| 55 | print i | ||
| 56 | i -= 1 | ||
| 57 | until i == 0 | ||
| 58 | ``` | ||
| 59 | <YueDisplay> | ||
| 60 | |||
| 61 | ```yue | ||
| 62 | i = 10 | ||
| 63 | repeat | ||
| 64 | print i | ||
| 65 | i -= 1 | ||
| 66 | until i == 0 | ||
| 67 | ``` | ||
| 68 | |||
| 69 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/doc/whitespace.md b/doc/docs/pt-br/doc/whitespace.md new file mode 100644 index 0000000..d742a2b --- /dev/null +++ b/doc/docs/pt-br/doc/whitespace.md | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | # Whitespace | ||
| 2 | |||
| 3 | YueScript is a whitespace significant language. You have to write some code block in the same indent with space **' '** or tab **'\t'** like function body, value list and some control blocks. And expressions containing different whitespaces might mean different things. Tab is treated like 4 space, but it's better not mix the use of spaces and tabs. | ||
| 4 | |||
| 5 | ## Statement Separator | ||
| 6 | |||
| 7 | A statement normally ends at a line break. You can also use a semicolon `;` to explicitly terminate a statement, which allows writing multiple statements on the same line: | ||
| 8 | |||
| 9 | ```yuescript | ||
| 10 | a = 1; b = 2; print a + b | ||
| 11 | ``` | ||
| 12 | <YueDisplay> | ||
| 13 | |||
| 14 | ```yue | ||
| 15 | a = 1; b = 2; print a + b | ||
| 16 | ``` | ||
| 17 | |||
| 18 | </YueDisplay> | ||
| 19 | |||
| 20 | ## Multiline Chaining | ||
| 21 | |||
| 22 | You can write multi-line chaining function calls with a same indent. | ||
| 23 | |||
| 24 | ```yuescript | ||
| 25 | Rx.Observable | ||
| 26 | .fromRange 1, 8 | ||
| 27 | \filter (x) -> x % 2 == 0 | ||
| 28 | \concat Rx.Observable.of 'who do we appreciate' | ||
| 29 | \map (value) -> value .. '!' | ||
| 30 | \subscribe print | ||
| 31 | ``` | ||
| 32 | <YueDisplay> | ||
| 33 | |||
| 34 | ```yue | ||
| 35 | Rx.Observable | ||
| 36 | .fromRange 1, 8 | ||
| 37 | \filter (x) -> x % 2 == 0 | ||
| 38 | \concat Rx.Observable.of 'who do we appreciate' | ||
| 39 | \map (value) -> value .. '!' | ||
| 40 | \subscribe print | ||
| 41 | ``` | ||
| 42 | |||
| 43 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/doc/with-statement.md b/doc/docs/pt-br/doc/with-statement.md new file mode 100644 index 0000000..7786803 --- /dev/null +++ b/doc/docs/pt-br/doc/with-statement.md | |||
| @@ -0,0 +1,126 @@ | |||
| 1 | # With Statement | ||
| 2 | |||
| 3 | |||
| 4 | A common pattern involving the creation of an object is calling a series of functions and setting a series of properties immediately after creating it. | ||
| 5 | |||
| 6 | This results in repeating the name of the object multiple times in code, adding unnecessary noise. A common solution to this is to pass a table in as an argument which contains a collection of keys and values to overwrite. The downside to this is that the constructor of this object must support this form. | ||
| 7 | |||
| 8 | The with block helps to alleviate this. Within a with block we can use a special statements that begin with either . or \ which represent those operations applied to the object we are using with on. | ||
| 9 | |||
| 10 | For example, we work with a newly created object: | ||
| 11 | |||
| 12 | ```yuescript | ||
| 13 | with Person! | ||
| 14 | .name = "Oswald" | ||
| 15 | \add_relative my_dad | ||
| 16 | \save! | ||
| 17 | print .name | ||
| 18 | ``` | ||
| 19 | <YueDisplay> | ||
| 20 | |||
| 21 | ```yue | ||
| 22 | with Person! | ||
| 23 | .name = "Oswald" | ||
| 24 | \add_relative my_dad | ||
| 25 | \save! | ||
| 26 | print .name | ||
| 27 | ``` | ||
| 28 | |||
| 29 | </YueDisplay> | ||
| 30 | |||
| 31 | The with statement can also be used as an expression which returns the value it has been giving access to. | ||
| 32 | |||
| 33 | ```yuescript | ||
| 34 | file = with File "favorite_foods.txt" | ||
| 35 | \set_encoding "utf8" | ||
| 36 | ``` | ||
| 37 | <YueDisplay> | ||
| 38 | |||
| 39 | ```yue | ||
| 40 | file = with File "favorite_foods.txt" | ||
| 41 | \set_encoding "utf8" | ||
| 42 | ``` | ||
| 43 | |||
| 44 | </YueDisplay> | ||
| 45 | |||
| 46 | Or… | ||
| 47 | |||
| 48 | ```yuescript | ||
| 49 | create_person = (name, relatives) -> | ||
| 50 | with Person! | ||
| 51 | .name = name | ||
| 52 | \add_relative relative for relative in *relatives | ||
| 53 | |||
| 54 | me = create_person "Leaf", [dad, mother, sister] | ||
| 55 | ``` | ||
| 56 | <YueDisplay> | ||
| 57 | |||
| 58 | ```yue | ||
| 59 | create_person = (name, relatives) -> | ||
| 60 | with Person! | ||
| 61 | .name = name | ||
| 62 | \add_relative relative for relative in *relatives | ||
| 63 | |||
| 64 | me = create_person "Leaf", [dad, mother, sister] | ||
| 65 | ``` | ||
| 66 | |||
| 67 | </YueDisplay> | ||
| 68 | |||
| 69 | In this usage, with can be seen as a special form of the K combinator. | ||
| 70 | |||
| 71 | The expression in the with statement can also be an assignment, if you want to give a name to the expression. | ||
| 72 | |||
| 73 | ```yuescript | ||
| 74 | with str := "Hello" | ||
| 75 | print "original:", str | ||
| 76 | print "upper:", \upper! | ||
| 77 | ``` | ||
| 78 | <YueDisplay> | ||
| 79 | |||
| 80 | ```yue | ||
| 81 | with str := "Hello" | ||
| 82 | print "original:", str | ||
| 83 | print "upper:", \upper! | ||
| 84 | ``` | ||
| 85 | |||
| 86 | </YueDisplay> | ||
| 87 | |||
| 88 | You can access special keys with `[]` in a `with` statement. | ||
| 89 | |||
| 90 | ```yuescript | ||
| 91 | with tb | ||
| 92 | [1] = 1 | ||
| 93 | print [2] | ||
| 94 | with [abc] | ||
| 95 | [3] = [2]\func! | ||
| 96 | ["key-name"] = value | ||
| 97 | [] = "abc" -- appending to "tb" | ||
| 98 | ``` | ||
| 99 | <YueDisplay> | ||
| 100 | |||
| 101 | ```yue | ||
| 102 | with tb | ||
| 103 | [1] = 1 | ||
| 104 | print [2] | ||
| 105 | with [abc] | ||
| 106 | [3] = [2]\func! | ||
| 107 | ["key-name"] = value | ||
| 108 | [] = "abc" -- appending to "tb" | ||
| 109 | ``` | ||
| 110 | |||
| 111 | </YueDisplay> | ||
| 112 | |||
| 113 | `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. | ||
| 114 | |||
| 115 | ```yuescript | ||
| 116 | with? obj | ||
| 117 | print obj.name | ||
| 118 | ``` | ||
| 119 | <YueDisplay> | ||
| 120 | |||
| 121 | ```yue | ||
| 122 | with? obj | ||
| 123 | print obj.name | ||
| 124 | ``` | ||
| 125 | |||
| 126 | </YueDisplay> | ||
diff --git a/doc/docs/pt-br/index.md b/doc/docs/pt-br/index.md new file mode 100644 index 0000000..818bb58 --- /dev/null +++ b/doc/docs/pt-br/index.md | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | --- | ||
| 2 | layout: home | ||
| 3 | hero: | ||
| 4 | name: YueScript | ||
| 5 | tagline: Uma linguagem que compila para Lua | ||
| 6 | image: | ||
| 7 | src: /image/yuescript.svg | ||
| 8 | alt: YueScript | ||
| 9 | actions: | ||
| 10 | - theme: brand | ||
| 11 | text: Início rápido → | ||
| 12 | link: /pt-br/doc/ | ||
| 13 | features: | ||
| 14 | - title: Fluxo de trabalho familiar do Lua | ||
| 15 | details: Escreva uma sintaxe concisa que compila para Lua legível, com saída previsível. | ||
| 16 | - title: Recursos modernos de linguagem | ||
| 17 | details: Pipe, pattern matching, slicing e destructuring sem abrir mão da interoperabilidade com Lua. | ||
| 18 | - title: Iteração rápida | ||
| 19 | details: Todo feedback é bem-vindo para ajudar a acelerar o desenvolvimento e a evolução da linguagem! | ||
| 20 | footer: | ||
| 21 | message: Licença MIT. | ||
| 22 | copyright: Copyright © 2017-2026 Li Jin. Todos os direitos reservados. | ||
| 23 | --- | ||
| 24 | --- | ||
diff --git a/doc/docs/pt-br/try/index.md b/doc/docs/pt-br/try/index.md new file mode 100755 index 0000000..624b3bf --- /dev/null +++ b/doc/docs/pt-br/try/index.md | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | # Compilador online do YueScript | ||
| 2 | --- | ||
| 3 | |||
| 4 | Experimente YueScript no navegador com WASM. | ||
| 5 | |||
| 6 | <YueCompiler /> | ||
