diff options
| -rw-r--r-- | doc/docs/de/doc/advanced/macro.md | 142 | ||||
| -rw-r--r-- | doc/docs/doc/advanced/macro.md | 153 | ||||
| -rw-r--r-- | doc/docs/id-id/doc/advanced/macro.md | 142 | ||||
| -rw-r--r-- | doc/docs/pt-br/doc/advanced/macro.md | 142 | ||||
| -rw-r--r-- | doc/docs/zh/doc/advanced/macro.md | 142 | ||||
| -rw-r--r-- | doc/yue-de.md | 70 | ||||
| -rw-r--r-- | doc/yue-en.md | 81 | ||||
| -rw-r--r-- | doc/yue-id-id.md | 70 | ||||
| -rw-r--r-- | doc/yue-pt-br.md | 70 | ||||
| -rw-r--r-- | doc/yue-zh.md | 70 | ||||
| -rw-r--r-- | spec/outputs/codes_from_doc_de.lua | 110 | ||||
| -rw-r--r-- | spec/outputs/codes_from_doc_en.lua | 110 | ||||
| -rw-r--r-- | spec/outputs/codes_from_doc_id-id.lua | 110 | ||||
| -rw-r--r-- | spec/outputs/codes_from_doc_pt-br.lua | 110 | ||||
| -rw-r--r-- | spec/outputs/codes_from_doc_zh.lua | 110 |
15 files changed, 1630 insertions, 2 deletions
diff --git a/doc/docs/de/doc/advanced/macro.md b/doc/docs/de/doc/advanced/macro.md index 8b78306..4287b2f 100644 --- a/doc/docs/de/doc/advanced/macro.md +++ b/doc/docs/de/doc/advanced/macro.md | |||
| @@ -338,3 +338,145 @@ $printNumAndStr 123, "hallo" | |||
| 338 | </YueDisplay> | 338 | </YueDisplay> |
| 339 | 339 | ||
| 340 | Weitere Details zu verfügbaren AST-Knoten findest du in den großgeschriebenen Definitionen in `yue_parser.cpp`. | 340 | Weitere Details zu verfügbaren AST-Knoten findest du in den großgeschriebenen Definitionen in `yue_parser.cpp`. |
| 341 | |||
| 342 | ## Annotation-Anweisungen | ||
| 343 | |||
| 344 | Annotation-Anweisungen wenden ein Makro auf die direkt folgende Anweisung an. | ||
| 345 | |||
| 346 | Das entspricht einem Makroaufruf, bei dem der Quelltext der folgenden Anweisung als letztes Argument zusätzlich übergeben wird. | ||
| 347 | |||
| 348 | ```yuescript | ||
| 349 | macro ShowName = (code) -> | | ||
| 350 | print "#{code\match '^[%w_]*'}" | ||
| 351 | |||
| 352 | $[ShowName] | ||
| 353 | myFunc = -> | ||
| 354 | |||
| 355 | return | ||
| 356 | ``` | ||
| 357 | |||
| 358 | <YueDisplay> | ||
| 359 | |||
| 360 | ```yue | ||
| 361 | macro ShowName = (code) -> | | ||
| 362 | print "#{code\match '^[%w_]*'}" | ||
| 363 | |||
| 364 | $[ShowName] | ||
| 365 | myFunc = -> | ||
| 366 | |||
| 367 | return | ||
| 368 | ``` | ||
| 369 | |||
| 370 | </YueDisplay> | ||
| 371 | |||
| 372 | Wenn das Annotationsmakro eine Konfigurationstabelle zurückgibt, steuert das optionale Feld `before`, ob das Ergebnis vor oder nach der annotierten Anweisung eingefügt wird. | ||
| 373 | |||
| 374 | ```yuescript | ||
| 375 | macro Tag = (tag, code) -> | ||
| 376 | tableName = code\match "^[%w_]+" | ||
| 377 | return | ||
| 378 | type: "text" | ||
| 379 | before: tag == "before" | ||
| 380 | code: "-- #{tag}:#{tableName}" | ||
| 381 | |||
| 382 | $[Tag before] | ||
| 383 | tableA = {} | ||
| 384 | |||
| 385 | $[Tag after] | ||
| 386 | tableB = {} | ||
| 387 | |||
| 388 | return | ||
| 389 | ``` | ||
| 390 | |||
| 391 | <YueDisplay> | ||
| 392 | |||
| 393 | ```yue | ||
| 394 | macro Tag = (tag, code) -> | ||
| 395 | tableName = code\match "^[%w_]+" | ||
| 396 | return | ||
| 397 | type: "text" | ||
| 398 | before: tag == "before" | ||
| 399 | code: "-- #{tag}:#{tableName}" | ||
| 400 | |||
| 401 | $[Tag before] | ||
| 402 | tableA = {} | ||
| 403 | |||
| 404 | $[Tag after] | ||
| 405 | tableB = {} | ||
| 406 | |||
| 407 | return | ||
| 408 | ``` | ||
| 409 | |||
| 410 | </YueDisplay> | ||
| 411 | |||
| 412 | Da die folgende Anweisung als zusätzliches Makroargument übergeben wird, lassen sich mit Annotationen auch direkt Registrierungszeilen aus Klassendeklarationen erzeugen. Du kannst dabei dieselben AST-Argumentprüfungen wie bei normalen Makros verwenden. | ||
| 413 | |||
| 414 | ```yuescript | ||
| 415 | macro Register = (registry, code`ClassDecl) -> | ||
| 416 | className = code\match "^class%s+(%w+)" | ||
| 417 | return | | ||
| 418 | #{registry}["#{className}"] = #{className} | ||
| 419 | |||
| 420 | registry = {} | ||
| 421 | |||
| 422 | $[Register(registry)] | ||
| 423 | class Worker | ||
| 424 | run: => "ok" | ||
| 425 | |||
| 426 | return | ||
| 427 | ``` | ||
| 428 | |||
| 429 | <YueDisplay> | ||
| 430 | |||
| 431 | ```yue | ||
| 432 | macro Register = (registry, code`ClassDecl) -> | ||
| 433 | className = code\match "^class%s+(%w+)" | ||
| 434 | return | | ||
| 435 | #{registry}["#{className}"] = #{className} | ||
| 436 | |||
| 437 | registry = {} | ||
| 438 | |||
| 439 | $[Register(registry)] | ||
| 440 | class Worker | ||
| 441 | run: => "ok" | ||
| 442 | |||
| 443 | return | ||
| 444 | ``` | ||
| 445 | |||
| 446 | </YueDisplay> | ||
| 447 | |||
| 448 | Annotationen können auch Wrapper-Code um Funktionen herum einfügen: | ||
| 449 | |||
| 450 | ```yuescript | ||
| 451 | macro ValidateNumberArgs = (code) -> | ||
| 452 | funcName = code\match "^(%w+)%s*=" | ||
| 453 | return | | ||
| 454 | local __orig_#{funcName} = #{funcName} | ||
| 455 | #{funcName} = (...) -> | ||
| 456 | for i = 1, select "#", ... | ||
| 457 | assert type(select i, ...) == "number", "expected number for arg \#{i}" | ||
| 458 | __orig_#{funcName} ... | ||
| 459 | |||
| 460 | $[ValidateNumberArgs] | ||
| 461 | add = (a, b) -> a + b | ||
| 462 | ``` | ||
| 463 | |||
| 464 | <YueDisplay> | ||
| 465 | |||
| 466 | ```yue | ||
| 467 | macro ValidateNumberArgs = (code) -> | ||
| 468 | funcName = code\match "^(%w+)%s*=" | ||
| 469 | return | | ||
| 470 | local __orig_#{funcName} = #{funcName} | ||
| 471 | #{funcName} = (...) -> | ||
| 472 | for i = 1, select "#", ... | ||
| 473 | assert type(select i, ...) == "number", "expected number for arg \#{i}" | ||
| 474 | __orig_#{funcName} ... | ||
| 475 | |||
| 476 | $[ValidateNumberArgs] | ||
| 477 | add = (a, b) -> a + b | ||
| 478 | ``` | ||
| 479 | |||
| 480 | </YueDisplay> | ||
| 481 | |||
| 482 | Auf eine Annotation muss immer eine Anweisung folgen, und sie kann nicht auf eine `return`-Anweisung angewendet werden. Wenn die annotierte Anweisung am Ende eines Blocks steht und du die rohe AST-Form der Anweisung brauchst, füge ein explizites `return` danach ein, damit sie nicht in einen implizit zurückgegebenen Ausdruck eingebettet wird. | ||
diff --git a/doc/docs/doc/advanced/macro.md b/doc/docs/doc/advanced/macro.md index af7c773..bc7b56c 100644 --- a/doc/docs/doc/advanced/macro.md +++ b/doc/docs/doc/advanced/macro.md | |||
| @@ -68,7 +68,7 @@ if $and f1!, f2!, f3! | |||
| 68 | 68 | ||
| 69 | ## Insert Raw Codes | 69 | ## Insert Raw Codes |
| 70 | 70 | ||
| 71 | A macro function can either return a YueScript string or a config table containing Lua codes. | 71 | A macro function can either return a YueScript string or a config table containing generated code. |
| 72 | 72 | ||
| 73 | ```yuescript | 73 | ```yuescript |
| 74 | macro yueFunc = (var) -> "local #{var} = ->" | 74 | macro yueFunc = (var) -> "local #{var} = ->" |
| @@ -126,6 +126,15 @@ end | |||
| 126 | 126 | ||
| 127 | </YueDisplay> | 127 | </YueDisplay> |
| 128 | 128 | ||
| 129 | The returned table can be used to control how the generated code gets inserted. | ||
| 130 | |||
| 131 | - `code` is the generated text. | ||
| 132 | - `type` chooses how the text is handled. It can be `"yue"` (the default), `"lua"`, or `"text"`. | ||
| 133 | - `locals` declares local names introduced by inserted text. | ||
| 134 | - `before` puts the generated result before the annotated statement instead of after it. | ||
| 135 | |||
| 136 | In most cases you only need to choose a `type`. Use `"yue"` for generated YueScript, `"lua"` for raw Lua, and `"text"` for text that should be copied straight into the final output. | ||
| 137 | |||
| 129 | ## Export Macro | 138 | ## Export Macro |
| 130 | 139 | ||
| 131 | 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. | 140 | 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. |
| @@ -338,3 +347,145 @@ $printNumAndStr 123, "hello" | |||
| 338 | </YueDisplay> | 347 | </YueDisplay> |
| 339 | 348 | ||
| 340 | 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). | 349 | 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). |
| 350 | |||
| 351 | ## Annotation Statements | ||
| 352 | |||
| 353 | Annotation statements apply a macro to the statement immediately following them. | ||
| 354 | |||
| 355 | This is equivalent to calling the macro with the following statement's source text appended as the last argument. | ||
| 356 | |||
| 357 | ```yuescript | ||
| 358 | macro ShowName = (code) -> | | ||
| 359 | print "#{code\match '^[%w_]*'}" | ||
| 360 | |||
| 361 | $[ShowName] | ||
| 362 | myFunc = -> | ||
| 363 | |||
| 364 | return | ||
| 365 | ``` | ||
| 366 | |||
| 367 | <YueDisplay> | ||
| 368 | |||
| 369 | ```yue | ||
| 370 | macro ShowName = (code) -> | | ||
| 371 | print "#{code\match '^[%w_]*'}" | ||
| 372 | |||
| 373 | $[ShowName] | ||
| 374 | myFunc = -> | ||
| 375 | |||
| 376 | return | ||
| 377 | ``` | ||
| 378 | |||
| 379 | </YueDisplay> | ||
| 380 | |||
| 381 | When the annotation macro returns a config table, the optional `before` field controls whether the generated result is emitted before or after the annotated statement. | ||
| 382 | |||
| 383 | ```yuescript | ||
| 384 | macro Tag = (tag, code) -> | ||
| 385 | tableName = code\match "^[%w_]+" | ||
| 386 | return | ||
| 387 | type: "text" | ||
| 388 | before: tag == "before" | ||
| 389 | code: "-- #{tag}:#{tableName}" | ||
| 390 | |||
| 391 | $[Tag before] | ||
| 392 | tableA = {} | ||
| 393 | |||
| 394 | $[Tag after] | ||
| 395 | tableB = {} | ||
| 396 | |||
| 397 | return | ||
| 398 | ``` | ||
| 399 | |||
| 400 | <YueDisplay> | ||
| 401 | |||
| 402 | ```yue | ||
| 403 | macro Tag = (tag, code) -> | ||
| 404 | tableName = code\match "^[%w_]+" | ||
| 405 | return | ||
| 406 | type: "text" | ||
| 407 | before: tag == "before" | ||
| 408 | code: "-- #{tag}:#{tableName}" | ||
| 409 | |||
| 410 | $[Tag before] | ||
| 411 | tableA = {} | ||
| 412 | |||
| 413 | $[Tag after] | ||
| 414 | tableB = {} | ||
| 415 | |||
| 416 | return | ||
| 417 | ``` | ||
| 418 | |||
| 419 | </YueDisplay> | ||
| 420 | |||
| 421 | Because the followed statement is passed in as an extra macro argument, annotations can also be used to generate registration code from class declarations. Because the followed statement is passed in as an extra macro argument, you can use the same AST argument checks as normal macros: | ||
| 422 | |||
| 423 | ```yuescript | ||
| 424 | macro Register = (registry, code`ClassDecl) -> | ||
| 425 | className = code\match "^class%s+(%w+)" | ||
| 426 | return | | ||
| 427 | #{registry}["#{className}"] = #{className} | ||
| 428 | |||
| 429 | registry = {} | ||
| 430 | |||
| 431 | $[Register(registry)] | ||
| 432 | class Worker | ||
| 433 | run: => "ok" | ||
| 434 | |||
| 435 | return | ||
| 436 | ``` | ||
| 437 | |||
| 438 | <YueDisplay> | ||
| 439 | |||
| 440 | ```yue | ||
| 441 | macro Register = (registry, code`ClassDecl) -> | ||
| 442 | className = code\match "^class%s+(%w+)" | ||
| 443 | return | | ||
| 444 | #{registry}["#{className}"] = #{className} | ||
| 445 | |||
| 446 | registry = {} | ||
| 447 | |||
| 448 | $[Register(registry)] | ||
| 449 | class Worker | ||
| 450 | run: => "ok" | ||
| 451 | |||
| 452 | return | ||
| 453 | ``` | ||
| 454 | |||
| 455 | </YueDisplay> | ||
| 456 | |||
| 457 | Annotations can also inject wrapper code around functions: | ||
| 458 | |||
| 459 | ```yuescript | ||
| 460 | macro ValidateNumberArgs = (code) -> | ||
| 461 | funcName = code\match "^(%w+)%s*=" | ||
| 462 | return | | ||
| 463 | local __orig_#{funcName} = #{funcName} | ||
| 464 | #{funcName} = (...) -> | ||
| 465 | for i = 1, select "#", ... | ||
| 466 | assert type(select i, ...) == "number", "expected number for arg \#{i}" | ||
| 467 | __orig_#{funcName} ... | ||
| 468 | |||
| 469 | $[ValidateNumberArgs] | ||
| 470 | add = (a, b) -> a + b | ||
| 471 | ``` | ||
| 472 | |||
| 473 | <YueDisplay> | ||
| 474 | |||
| 475 | ```yue | ||
| 476 | macro ValidateNumberArgs = (code) -> | ||
| 477 | funcName = code\match "^(%w+)%s*=" | ||
| 478 | return | | ||
| 479 | local __orig_#{funcName} = #{funcName} | ||
| 480 | #{funcName} = (...) -> | ||
| 481 | for i = 1, select "#", ... | ||
| 482 | assert type(select i, ...) == "number", "expected number for arg \#{i}" | ||
| 483 | __orig_#{funcName} ... | ||
| 484 | |||
| 485 | $[ValidateNumberArgs] | ||
| 486 | add = (a, b) -> a + b | ||
| 487 | ``` | ||
| 488 | |||
| 489 | </YueDisplay> | ||
| 490 | |||
| 491 | An annotation must always be followed by a statement, and it can not be applied to a `return` statement. If the annotated statement appears at the end of a block, add an explicit trailing `return` when you need the raw statement AST shape instead of an implicitly returned expression. | ||
diff --git a/doc/docs/id-id/doc/advanced/macro.md b/doc/docs/id-id/doc/advanced/macro.md index 9a1494f..96e1785 100644 --- a/doc/docs/id-id/doc/advanced/macro.md +++ b/doc/docs/id-id/doc/advanced/macro.md | |||
| @@ -338,3 +338,145 @@ $printNumAndStr 123, "hello" | |||
| 338 | </YueDisplay> | 338 | </YueDisplay> |
| 339 | 339 | ||
| 340 | Untuk detail lebih lanjut tentang node AST yang tersedia, silakan lihat definisi huruf besar di [yue_parser.cpp](https://github.com/IppClub/YueScript/blob/main/src/yuescript/yue_parser.cpp). | 340 | Untuk detail lebih lanjut tentang node AST yang tersedia, silakan lihat definisi huruf besar di [yue_parser.cpp](https://github.com/IppClub/YueScript/blob/main/src/yuescript/yue_parser.cpp). |
| 341 | |||
| 342 | ## Pernyataan anotasi | ||
| 343 | |||
| 344 | Pernyataan anotasi menerapkan sebuah macro ke pernyataan yang tepat mengikutinya. | ||
| 345 | |||
| 346 | Ini setara dengan memanggil macro sambil menambahkan kode sumber dari pernyataan berikutnya sebagai argumen terakhir. | ||
| 347 | |||
| 348 | ```yuescript | ||
| 349 | macro ShowName = (code) -> | | ||
| 350 | print "#{code\match '^[%w_]*'}" | ||
| 351 | |||
| 352 | $[ShowName] | ||
| 353 | myFunc = -> | ||
| 354 | |||
| 355 | return | ||
| 356 | ``` | ||
| 357 | |||
| 358 | <YueDisplay> | ||
| 359 | |||
| 360 | ```yue | ||
| 361 | macro ShowName = (code) -> | | ||
| 362 | print "#{code\match '^[%w_]*'}" | ||
| 363 | |||
| 364 | $[ShowName] | ||
| 365 | myFunc = -> | ||
| 366 | |||
| 367 | return | ||
| 368 | ``` | ||
| 369 | |||
| 370 | </YueDisplay> | ||
| 371 | |||
| 372 | Saat macro anotasi mengembalikan tabel konfigurasi, field opsional `before` mengatur apakah hasil yang dihasilkan akan diletakkan sebelum atau sesudah pernyataan yang dianotasi. | ||
| 373 | |||
| 374 | ```yuescript | ||
| 375 | macro Tag = (tag, code) -> | ||
| 376 | tableName = code\match "^[%w_]+" | ||
| 377 | return | ||
| 378 | type: "text" | ||
| 379 | before: tag == "before" | ||
| 380 | code: "-- #{tag}:#{tableName}" | ||
| 381 | |||
| 382 | $[Tag before] | ||
| 383 | tableA = {} | ||
| 384 | |||
| 385 | $[Tag after] | ||
| 386 | tableB = {} | ||
| 387 | |||
| 388 | return | ||
| 389 | ``` | ||
| 390 | |||
| 391 | <YueDisplay> | ||
| 392 | |||
| 393 | ```yue | ||
| 394 | macro Tag = (tag, code) -> | ||
| 395 | tableName = code\match "^[%w_]+" | ||
| 396 | return | ||
| 397 | type: "text" | ||
| 398 | before: tag == "before" | ||
| 399 | code: "-- #{tag}:#{tableName}" | ||
| 400 | |||
| 401 | $[Tag before] | ||
| 402 | tableA = {} | ||
| 403 | |||
| 404 | $[Tag after] | ||
| 405 | tableB = {} | ||
| 406 | |||
| 407 | return | ||
| 408 | ``` | ||
| 409 | |||
| 410 | </YueDisplay> | ||
| 411 | |||
| 412 | Karena pernyataan berikutnya diteruskan sebagai argumen macro tambahan, anotasi juga bisa digunakan untuk menghasilkan kode registrasi langsung dari deklarasi class. Anda juga dapat menggunakan pemeriksaan argumen AST yang sama seperti pada macro biasa. | ||
| 413 | |||
| 414 | ```yuescript | ||
| 415 | macro Register = (registry, code`ClassDecl) -> | ||
| 416 | className = code\match "^class%s+(%w+)" | ||
| 417 | return | | ||
| 418 | #{registry}["#{className}"] = #{className} | ||
| 419 | |||
| 420 | registry = {} | ||
| 421 | |||
| 422 | $[Register(registry)] | ||
| 423 | class Worker | ||
| 424 | run: => "ok" | ||
| 425 | |||
| 426 | return | ||
| 427 | ``` | ||
| 428 | |||
| 429 | <YueDisplay> | ||
| 430 | |||
| 431 | ```yue | ||
| 432 | macro Register = (registry, code`ClassDecl) -> | ||
| 433 | className = code\match "^class%s+(%w+)" | ||
| 434 | return | | ||
| 435 | #{registry}["#{className}"] = #{className} | ||
| 436 | |||
| 437 | registry = {} | ||
| 438 | |||
| 439 | $[Register(registry)] | ||
| 440 | class Worker | ||
| 441 | run: => "ok" | ||
| 442 | |||
| 443 | return | ||
| 444 | ``` | ||
| 445 | |||
| 446 | </YueDisplay> | ||
| 447 | |||
| 448 | Anotasi juga bisa menyisipkan kode pembungkus di sekitar fungsi: | ||
| 449 | |||
| 450 | ```yuescript | ||
| 451 | macro ValidateNumberArgs = (code) -> | ||
| 452 | funcName = code\match "^(%w+)%s*=" | ||
| 453 | return | | ||
| 454 | local __orig_#{funcName} = #{funcName} | ||
| 455 | #{funcName} = (...) -> | ||
| 456 | for i = 1, select "#", ... | ||
| 457 | assert type(select i, ...) == "number", "expected number for arg \#{i}" | ||
| 458 | __orig_#{funcName} ... | ||
| 459 | |||
| 460 | $[ValidateNumberArgs] | ||
| 461 | add = (a, b) -> a + b | ||
| 462 | ``` | ||
| 463 | |||
| 464 | <YueDisplay> | ||
| 465 | |||
| 466 | ```yue | ||
| 467 | macro ValidateNumberArgs = (code) -> | ||
| 468 | funcName = code\match "^(%w+)%s*=" | ||
| 469 | return | | ||
| 470 | local __orig_#{funcName} = #{funcName} | ||
| 471 | #{funcName} = (...) -> | ||
| 472 | for i = 1, select "#", ... | ||
| 473 | assert type(select i, ...) == "number", "expected number for arg \#{i}" | ||
| 474 | __orig_#{funcName} ... | ||
| 475 | |||
| 476 | $[ValidateNumberArgs] | ||
| 477 | add = (a, b) -> a + b | ||
| 478 | ``` | ||
| 479 | |||
| 480 | </YueDisplay> | ||
| 481 | |||
| 482 | Sebuah anotasi harus selalu diikuti oleh sebuah pernyataan, dan tidak bisa diterapkan ke pernyataan `return`. Jika pernyataan yang dianotasi berada di akhir sebuah blok dan Anda membutuhkan bentuk AST mentah dari pernyataan itu, tambahkan `return` eksplisit setelahnya agar ia tidak dibungkus menjadi ekspresi yang dikembalikan secara implisit. | ||
diff --git a/doc/docs/pt-br/doc/advanced/macro.md b/doc/docs/pt-br/doc/advanced/macro.md index 248011e..6fa04be 100644 --- a/doc/docs/pt-br/doc/advanced/macro.md +++ b/doc/docs/pt-br/doc/advanced/macro.md | |||
| @@ -337,3 +337,145 @@ $printNumAndStr 123, "hello" | |||
| 337 | </YueDisplay> | 337 | </YueDisplay> |
| 338 | 338 | ||
| 339 | Para mais detalhes sobre os nós AST disponíveis, consulte as definições em maiúsculas em [yue_parser.cpp](https://github.com/IppClub/YueScript/blob/main/src/yuescript/yue_parser.cpp). | 339 | Para mais detalhes sobre os nós AST disponíveis, consulte as definições em maiúsculas em [yue_parser.cpp](https://github.com/IppClub/YueScript/blob/main/src/yuescript/yue_parser.cpp). |
| 340 | |||
| 341 | ## Instruções de anotação | ||
| 342 | |||
| 343 | As instruções de anotação aplicam uma macro à instrução logo em seguida. | ||
| 344 | |||
| 345 | Isso equivale a chamar a macro com o código-fonte da instrução seguinte anexado como último argumento. | ||
| 346 | |||
| 347 | ```yuescript | ||
| 348 | macro ShowName = (code) -> | | ||
| 349 | print "#{code\match '^[%w_]*'}" | ||
| 350 | |||
| 351 | $[ShowName] | ||
| 352 | myFunc = -> | ||
| 353 | |||
| 354 | return | ||
| 355 | ``` | ||
| 356 | |||
| 357 | <YueDisplay> | ||
| 358 | |||
| 359 | ```yue | ||
| 360 | macro ShowName = (code) -> | | ||
| 361 | print "#{code\match '^[%w_]*'}" | ||
| 362 | |||
| 363 | $[ShowName] | ||
| 364 | myFunc = -> | ||
| 365 | |||
| 366 | return | ||
| 367 | ``` | ||
| 368 | |||
| 369 | </YueDisplay> | ||
| 370 | |||
| 371 | Quando a macro de anotação retorna uma tabela de configuração, o campo opcional `before` controla se o resultado gerado será emitido antes ou depois da instrução anotada. | ||
| 372 | |||
| 373 | ```yuescript | ||
| 374 | macro Tag = (tag, code) -> | ||
| 375 | tableName = code\match "^[%w_]+" | ||
| 376 | return | ||
| 377 | type: "text" | ||
| 378 | before: tag == "before" | ||
| 379 | code: "-- #{tag}:#{tableName}" | ||
| 380 | |||
| 381 | $[Tag before] | ||
| 382 | tableA = {} | ||
| 383 | |||
| 384 | $[Tag after] | ||
| 385 | tableB = {} | ||
| 386 | |||
| 387 | return | ||
| 388 | ``` | ||
| 389 | |||
| 390 | <YueDisplay> | ||
| 391 | |||
| 392 | ```yue | ||
| 393 | macro Tag = (tag, code) -> | ||
| 394 | tableName = code\match "^[%w_]+" | ||
| 395 | return | ||
| 396 | type: "text" | ||
| 397 | before: tag == "before" | ||
| 398 | code: "-- #{tag}:#{tableName}" | ||
| 399 | |||
| 400 | $[Tag before] | ||
| 401 | tableA = {} | ||
| 402 | |||
| 403 | $[Tag after] | ||
| 404 | tableB = {} | ||
| 405 | |||
| 406 | return | ||
| 407 | ``` | ||
| 408 | |||
| 409 | </YueDisplay> | ||
| 410 | |||
| 411 | Como a instrução seguinte é passada como um argumento extra para a macro, anotações também podem ser usadas para gerar código de registro a partir de declarações de classe. Você também pode usar as mesmas checagens de AST nos argumentos que macros normais oferecem. | ||
| 412 | |||
| 413 | ```yuescript | ||
| 414 | macro Register = (registry, code`ClassDecl) -> | ||
| 415 | className = code\match "^class%s+(%w+)" | ||
| 416 | return | | ||
| 417 | #{registry}["#{className}"] = #{className} | ||
| 418 | |||
| 419 | registry = {} | ||
| 420 | |||
| 421 | $[Register(registry)] | ||
| 422 | class Worker | ||
| 423 | run: => "ok" | ||
| 424 | |||
| 425 | return | ||
| 426 | ``` | ||
| 427 | |||
| 428 | <YueDisplay> | ||
| 429 | |||
| 430 | ```yue | ||
| 431 | macro Register = (registry, code`ClassDecl) -> | ||
| 432 | className = code\match "^class%s+(%w+)" | ||
| 433 | return | | ||
| 434 | #{registry}["#{className}"] = #{className} | ||
| 435 | |||
| 436 | registry = {} | ||
| 437 | |||
| 438 | $[Register(registry)] | ||
| 439 | class Worker | ||
| 440 | run: => "ok" | ||
| 441 | |||
| 442 | return | ||
| 443 | ``` | ||
| 444 | |||
| 445 | </YueDisplay> | ||
| 446 | |||
| 447 | Anotações também podem injetar código de empacotamento em volta de funções: | ||
| 448 | |||
| 449 | ```yuescript | ||
| 450 | macro ValidateNumberArgs = (code) -> | ||
| 451 | funcName = code\match "^(%w+)%s*=" | ||
| 452 | return | | ||
| 453 | local __orig_#{funcName} = #{funcName} | ||
| 454 | #{funcName} = (...) -> | ||
| 455 | for i = 1, select "#", ... | ||
| 456 | assert type(select i, ...) == "number", "expected number for arg \#{i}" | ||
| 457 | __orig_#{funcName} ... | ||
| 458 | |||
| 459 | $[ValidateNumberArgs] | ||
| 460 | add = (a, b) -> a + b | ||
| 461 | ``` | ||
| 462 | |||
| 463 | <YueDisplay> | ||
| 464 | |||
| 465 | ```yue | ||
| 466 | macro ValidateNumberArgs = (code) -> | ||
| 467 | funcName = code\match "^(%w+)%s*=" | ||
| 468 | return | | ||
| 469 | local __orig_#{funcName} = #{funcName} | ||
| 470 | #{funcName} = (...) -> | ||
| 471 | for i = 1, select "#", ... | ||
| 472 | assert type(select i, ...) == "number", "expected number for arg \#{i}" | ||
| 473 | __orig_#{funcName} ... | ||
| 474 | |||
| 475 | $[ValidateNumberArgs] | ||
| 476 | add = (a, b) -> a + b | ||
| 477 | ``` | ||
| 478 | |||
| 479 | </YueDisplay> | ||
| 480 | |||
| 481 | Uma anotação sempre precisa ser seguida por uma instrução, e ela não pode ser aplicada a uma instrução `return`. Se a instrução anotada aparecer no fim de um bloco e você precisar da forma AST bruta dessa instrução, adicione um `return` explícito em seguida para evitar que ela seja envolvida por uma expressão com retorno implícito. | ||
diff --git a/doc/docs/zh/doc/advanced/macro.md b/doc/docs/zh/doc/advanced/macro.md index 5110571..0d7ee6d 100644 --- a/doc/docs/zh/doc/advanced/macro.md +++ b/doc/docs/zh/doc/advanced/macro.md | |||
| @@ -338,3 +338,145 @@ $printNumAndStr 123, "hello" | |||
| 338 | </YueDisplay> | 338 | </YueDisplay> |
| 339 | 339 | ||
| 340 |   更多关于可用 AST 节点的详细信息,请参考 [yue_parser.cpp](https://github.com/IppClub/YueScript/blob/main/src/yuescript/yue_parser.cpp) 中大写的规则定义。 | 340 |   更多关于可用 AST 节点的详细信息,请参考 [yue_parser.cpp](https://github.com/IppClub/YueScript/blob/main/src/yuescript/yue_parser.cpp) 中大写的规则定义。 |
| 341 | |||
| 342 | ## 注解语句 | ||
| 343 | |||
| 344 |   注解语句会把一个宏应用到它后面的那条语句上。 | ||
| 345 | |||
| 346 |   这等价于调用该宏,并把后面那条语句的源码作为最后一个参数附加进去。 | ||
| 347 | |||
| 348 | ```yuescript | ||
| 349 | macro ShowName = (code) -> | | ||
| 350 | print "#{code\match '^[%w_]*'}" | ||
| 351 | |||
| 352 | $[ShowName] | ||
| 353 | myFunc = -> | ||
| 354 | |||
| 355 | return | ||
| 356 | ``` | ||
| 357 | |||
| 358 | <YueDisplay> | ||
| 359 | |||
| 360 | ```yue | ||
| 361 | macro ShowName = (code) -> | | ||
| 362 | print "#{code\match '^[%w_]*'}" | ||
| 363 | |||
| 364 | $[ShowName] | ||
| 365 | myFunc = -> | ||
| 366 | |||
| 367 | return | ||
| 368 | ``` | ||
| 369 | |||
| 370 | </YueDisplay> | ||
| 371 | |||
| 372 |   如果注解宏返回的是配置表,可选字段 `before` 可以控制生成结果插入到被注解语句之前还是之后。 | ||
| 373 | |||
| 374 | ```yuescript | ||
| 375 | macro Tag = (tag, code) -> | ||
| 376 | tableName = code\match "^[%w_]+" | ||
| 377 | return | ||
| 378 | type: "text" | ||
| 379 | before: tag == "before" | ||
| 380 | code: "-- #{tag}:#{tableName}" | ||
| 381 | |||
| 382 | $[Tag before] | ||
| 383 | tableA = {} | ||
| 384 | |||
| 385 | $[Tag after] | ||
| 386 | tableB = {} | ||
| 387 | |||
| 388 | return | ||
| 389 | ``` | ||
| 390 | |||
| 391 | <YueDisplay> | ||
| 392 | |||
| 393 | ```yue | ||
| 394 | macro Tag = (tag, code) -> | ||
| 395 | tableName = code\match "^[%w_]+" | ||
| 396 | return | ||
| 397 | type: "text" | ||
| 398 | before: tag == "before" | ||
| 399 | code: "-- #{tag}:#{tableName}" | ||
| 400 | |||
| 401 | $[Tag before] | ||
| 402 | tableA = {} | ||
| 403 | |||
| 404 | $[Tag after] | ||
| 405 | tableB = {} | ||
| 406 | |||
| 407 | return | ||
| 408 | ``` | ||
| 409 | |||
| 410 | </YueDisplay> | ||
| 411 | |||
| 412 |   由于后面的语句会作为额外的宏参数传入,注解也可以从类声明生成注册代码。它同样可以使用普通宏支持的 AST 参数检查。 | ||
| 413 | |||
| 414 | ```yuescript | ||
| 415 | macro Register = (registry, code`ClassDecl) -> | ||
| 416 | className = code\match "^class%s+(%w+)" | ||
| 417 | return | | ||
| 418 | #{registry}["#{className}"] = #{className} | ||
| 419 | |||
| 420 | registry = {} | ||
| 421 | |||
| 422 | $[Register(registry)] | ||
| 423 | class Worker | ||
| 424 | run: => "ok" | ||
| 425 | |||
| 426 | return | ||
| 427 | ``` | ||
| 428 | |||
| 429 | <YueDisplay> | ||
| 430 | |||
| 431 | ```yue | ||
| 432 | macro Register = (registry, code`ClassDecl) -> | ||
| 433 | className = code\match "^class%s+(%w+)" | ||
| 434 | return | | ||
| 435 | #{registry}["#{className}"] = #{className} | ||
| 436 | |||
| 437 | registry = {} | ||
| 438 | |||
| 439 | $[Register(registry)] | ||
| 440 | class Worker | ||
| 441 | run: => "ok" | ||
| 442 | |||
| 443 | return | ||
| 444 | ``` | ||
| 445 | |||
| 446 | </YueDisplay> | ||
| 447 | |||
| 448 |   注解也可以用来给函数注入包装代码。 | ||
| 449 | |||
| 450 | ```yuescript | ||
| 451 | macro ValidateNumberArgs = (code) -> | ||
| 452 | funcName = code\match "^(%w+)%s*=" | ||
| 453 | return | | ||
| 454 | local __orig_#{funcName} = #{funcName} | ||
| 455 | #{funcName} = (...) -> | ||
| 456 | for i = 1, select "#", ... | ||
| 457 | assert type(select i, ...) == "number", "expected number for arg \#{i}" | ||
| 458 | __orig_#{funcName} ... | ||
| 459 | |||
| 460 | $[ValidateNumberArgs] | ||
| 461 | add = (a, b) -> a + b | ||
| 462 | ``` | ||
| 463 | |||
| 464 | <YueDisplay> | ||
| 465 | |||
| 466 | ```yue | ||
| 467 | macro ValidateNumberArgs = (code) -> | ||
| 468 | funcName = code\match "^(%w+)%s*=" | ||
| 469 | return | | ||
| 470 | local __orig_#{funcName} = #{funcName} | ||
| 471 | #{funcName} = (...) -> | ||
| 472 | for i = 1, select "#", ... | ||
| 473 | assert type(select i, ...) == "number", "expected number for arg \#{i}" | ||
| 474 | __orig_#{funcName} ... | ||
| 475 | |||
| 476 | $[ValidateNumberArgs] | ||
| 477 | add = (a, b) -> a + b | ||
| 478 | ``` | ||
| 479 | |||
| 480 | </YueDisplay> | ||
| 481 | |||
| 482 |   注解后面必须紧跟一条语句,而且不能作用在 `return` 语句上。如果被注解的语句正好位于代码块末尾,而你又需要拿到原始的语句 AST 形态,就需要额外补一个显式的 `return`,避免它被隐式返回表达式包起来。 | ||
diff --git a/doc/yue-de.md b/doc/yue-de.md index 4c7ede9..8ad750d 100644 --- a/doc/yue-de.md +++ b/doc/yue-de.md | |||
| @@ -237,6 +237,76 @@ $printNumAndStr 123, "hallo" | |||
| 237 | 237 | ||
| 238 | Weitere Details zu verfügbaren AST-Knoten findest du in den großgeschriebenen Definitionen in `yue_parser.cpp`. | 238 | Weitere Details zu verfügbaren AST-Knoten findest du in den großgeschriebenen Definitionen in `yue_parser.cpp`. |
| 239 | 239 | ||
| 240 | ## Annotation-Anweisungen | ||
| 241 | |||
| 242 | Annotation-Anweisungen wenden ein Makro auf die direkt folgende Anweisung an. | ||
| 243 | |||
| 244 | Das entspricht einem Makroaufruf, bei dem der Quelltext der folgenden Anweisung als letztes Argument zusätzlich übergeben wird. | ||
| 245 | |||
| 246 | ```yuescript | ||
| 247 | macro ShowName = (code) -> | | ||
| 248 | print "#{code\match '^[%w_]*'}" | ||
| 249 | |||
| 250 | $[ShowName] | ||
| 251 | myFunc = -> | ||
| 252 | |||
| 253 | return | ||
| 254 | ``` | ||
| 255 | |||
| 256 | Wenn das Annotationsmakro eine Konfigurationstabelle zurückgibt, steuert das optionale Feld `before`, ob das Ergebnis vor oder nach der annotierten Anweisung eingefügt wird. | ||
| 257 | |||
| 258 | ```yuescript | ||
| 259 | macro Tag = (tag, code) -> | ||
| 260 | tableName = code\match "^[%w_]+" | ||
| 261 | return | ||
| 262 | type: "text" | ||
| 263 | before: tag == "before" | ||
| 264 | code: "-- #{tag}:#{tableName}" | ||
| 265 | |||
| 266 | $[Tag before] | ||
| 267 | tableA = {} | ||
| 268 | |||
| 269 | $[Tag after] | ||
| 270 | tableB = {} | ||
| 271 | |||
| 272 | return | ||
| 273 | ``` | ||
| 274 | |||
| 275 | Da die folgende Anweisung als zusätzliches Makroargument übergeben wird, lassen sich mit Annotationen auch direkt Registrierungszeilen aus Klassendeklarationen erzeugen. Du kannst dabei dieselben AST-Argumentprüfungen wie bei normalen Makros verwenden. | ||
| 276 | |||
| 277 | ```yuescript | ||
| 278 | macro Register = (registry, code`ClassDecl) -> | ||
| 279 | className = code\match "^class%s+(%w+)" | ||
| 280 | return | | ||
| 281 | #{registry}["#{className}"] = #{className} | ||
| 282 | |||
| 283 | registry = {} | ||
| 284 | |||
| 285 | $[Register(registry)] | ||
| 286 | class Worker | ||
| 287 | run: => "ok" | ||
| 288 | |||
| 289 | return | ||
| 290 | ``` | ||
| 291 | |||
| 292 | Annotationen können auch Wrapper-Code um Funktionen herum einfügen: | ||
| 293 | |||
| 294 | ```yuescript | ||
| 295 | macro ValidateNumberArgs = (code) -> | ||
| 296 | funcName = code\match "^(%w+)%s*=" | ||
| 297 | return | | ||
| 298 | local __orig_#{funcName} = #{funcName} | ||
| 299 | #{funcName} = (...) -> | ||
| 300 | for i = 1, select "#", ... | ||
| 301 | assert type(select i, ...) == "number", "expected number for arg \#{i}" | ||
| 302 | __orig_#{funcName} ... | ||
| 303 | |||
| 304 | $[ValidateNumberArgs] | ||
| 305 | add = (a, b) -> a + b | ||
| 306 | ``` | ||
| 307 | |||
| 308 | Auf eine Annotation muss immer eine Anweisung folgen, und sie kann nicht auf eine `return`-Anweisung angewendet werden. Wenn die annotierte Anweisung am Ende eines Blocks steht und du die rohe AST-Form der Anweisung brauchst, füge ein explizites `return` danach ein, damit sie nicht in einen implizit zurückgegebenen Ausdruck eingebettet wird. | ||
| 309 | |||
| 240 | # Try | 310 | # Try |
| 241 | 311 | ||
| 242 | Die Syntax für Fehlerbehandlung in Lua in einer gängigen Form. | 312 | Die Syntax für Fehlerbehandlung in Lua in einer gängigen Form. |
diff --git a/doc/yue-en.md b/doc/yue-en.md index dc61570..ab43a7c 100644 --- a/doc/yue-en.md +++ b/doc/yue-en.md | |||
| @@ -105,7 +105,7 @@ if $and f1!, f2!, f3! | |||
| 105 | 105 | ||
| 106 | ## Insert Raw Codes | 106 | ## Insert Raw Codes |
| 107 | 107 | ||
| 108 | A macro function can either return a YueScript string or a config table containing Lua codes. | 108 | A macro function can either return a YueScript string or a config table containing generated code. |
| 109 | 109 | ||
| 110 | ```yuescript | 110 | ```yuescript |
| 111 | macro yueFunc = (var) -> "local #{var} = ->" | 111 | macro yueFunc = (var) -> "local #{var} = ->" |
| @@ -133,6 +133,15 @@ end | |||
| 133 | ]==] | 133 | ]==] |
| 134 | ``` | 134 | ``` |
| 135 | 135 | ||
| 136 | The returned table can be used to control how the generated code gets inserted. | ||
| 137 | |||
| 138 | - `code` is the generated text. | ||
| 139 | - `type` chooses how the text is handled. It can be `"yue"` (the default), `"lua"`, or `"text"`. | ||
| 140 | - `locals` declares local names introduced by inserted text. | ||
| 141 | - `before` puts the generated result before the annotated statement instead of after it. | ||
| 142 | |||
| 143 | In most cases you only need to choose a `type`. Use `"yue"` for generated YueScript, `"lua"` for raw Lua, and `"text"` for text that should be copied straight into the final output. | ||
| 144 | |||
| 136 | ## Export Macro | 145 | ## Export Macro |
| 137 | 146 | ||
| 138 | 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. | 147 | 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. |
| @@ -237,6 +246,76 @@ $printNumAndStr 123, "hello" | |||
| 237 | 246 | ||
| 238 | 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). | 247 | 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). |
| 239 | 248 | ||
| 249 | ## Annotation Statements | ||
| 250 | |||
| 251 | Annotation statements apply a macro to the statement immediately following them. | ||
| 252 | |||
| 253 | This is equivalent to calling the macro with the following statement's source text appended as the last argument. | ||
| 254 | |||
| 255 | ```yuescript | ||
| 256 | macro ShowName = (code) -> | | ||
| 257 | print "#{code\match '^[%w_]*'}" | ||
| 258 | |||
| 259 | $[ShowName] | ||
| 260 | myFunc = -> | ||
| 261 | |||
| 262 | return | ||
| 263 | ``` | ||
| 264 | |||
| 265 | When the annotation macro returns a config table, the optional `before` field controls whether the generated result is emitted before or after the annotated statement. | ||
| 266 | |||
| 267 | ```yuescript | ||
| 268 | macro Tag = (tag, code) -> | ||
| 269 | tableName = code\match "^[%w_]+" | ||
| 270 | return | ||
| 271 | type: "text" | ||
| 272 | before: tag == "before" | ||
| 273 | code: "-- #{tag}:#{tableName}" | ||
| 274 | |||
| 275 | $[Tag before] | ||
| 276 | tableA = {} | ||
| 277 | |||
| 278 | $[Tag after] | ||
| 279 | tableB = {} | ||
| 280 | |||
| 281 | return | ||
| 282 | ``` | ||
| 283 | |||
| 284 | Because the followed statement is passed in as an extra macro argument, annotations can also be used to generate registration code from class declarations. Because the followed statement is passed in as an extra macro argument, you can use the same AST argument checks as normal macros: | ||
| 285 | |||
| 286 | ```yuescript | ||
| 287 | macro Register = (registry, code`ClassDecl) -> | ||
| 288 | className = code\match "^class%s+(%w+)" | ||
| 289 | return | | ||
| 290 | #{registry}["#{className}"] = #{className} | ||
| 291 | |||
| 292 | registry = {} | ||
| 293 | |||
| 294 | $[Register(registry)] | ||
| 295 | class Worker | ||
| 296 | run: => "ok" | ||
| 297 | |||
| 298 | return | ||
| 299 | ``` | ||
| 300 | |||
| 301 | Annotations can also inject wrapper code around functions: | ||
| 302 | |||
| 303 | ```yuescript | ||
| 304 | macro ValidateNumberArgs = (code) -> | ||
| 305 | funcName = code\match "^(%w+)%s*=" | ||
| 306 | return | | ||
| 307 | local __orig_#{funcName} = #{funcName} | ||
| 308 | #{funcName} = (...) -> | ||
| 309 | for i = 1, select "#", ... | ||
| 310 | assert type(select i, ...) == "number", "expected number for arg \#{i}" | ||
| 311 | __orig_#{funcName} ... | ||
| 312 | |||
| 313 | $[ValidateNumberArgs] | ||
| 314 | add = (a, b) -> a + b | ||
| 315 | ``` | ||
| 316 | |||
| 317 | An annotation must always be followed by a statement, and it can not be applied to a `return` statement. If the annotated statement appears at the end of a block, add an explicit trailing `return` when you need the raw statement AST shape instead of an implicitly returned expression. | ||
| 318 | |||
| 240 | # Try | 319 | # Try |
| 241 | 320 | ||
| 242 | The syntax for Lua error handling in a common form. | 321 | The syntax for Lua error handling in a common form. |
diff --git a/doc/yue-id-id.md b/doc/yue-id-id.md index a2a70d4..d891c57 100644 --- a/doc/yue-id-id.md +++ b/doc/yue-id-id.md | |||
| @@ -237,6 +237,76 @@ $printNumAndStr 123, "hello" | |||
| 237 | 237 | ||
| 238 | Untuk detail lebih lanjut tentang node AST yang tersedia, silakan lihat definisi huruf besar di [yue_parser.cpp](https://github.com/IppClub/YueScript/blob/main/src/yuescript/yue_parser.cpp). | 238 | Untuk detail lebih lanjut tentang node AST yang tersedia, silakan lihat definisi huruf besar di [yue_parser.cpp](https://github.com/IppClub/YueScript/blob/main/src/yuescript/yue_parser.cpp). |
| 239 | 239 | ||
| 240 | ## Pernyataan anotasi | ||
| 241 | |||
| 242 | Pernyataan anotasi menerapkan sebuah macro ke pernyataan yang tepat mengikutinya. | ||
| 243 | |||
| 244 | Ini setara dengan memanggil macro sambil menambahkan kode sumber dari pernyataan berikutnya sebagai argumen terakhir. | ||
| 245 | |||
| 246 | ```yuescript | ||
| 247 | macro ShowName = (code) -> | | ||
| 248 | print "#{code\match '^[%w_]*'}" | ||
| 249 | |||
| 250 | $[ShowName] | ||
| 251 | myFunc = -> | ||
| 252 | |||
| 253 | return | ||
| 254 | ``` | ||
| 255 | |||
| 256 | Saat macro anotasi mengembalikan tabel konfigurasi, field opsional `before` mengatur apakah hasil yang dihasilkan akan diletakkan sebelum atau sesudah pernyataan yang dianotasi. | ||
| 257 | |||
| 258 | ```yuescript | ||
| 259 | macro Tag = (tag, code) -> | ||
| 260 | tableName = code\match "^[%w_]+" | ||
| 261 | return | ||
| 262 | type: "text" | ||
| 263 | before: tag == "before" | ||
| 264 | code: "-- #{tag}:#{tableName}" | ||
| 265 | |||
| 266 | $[Tag before] | ||
| 267 | tableA = {} | ||
| 268 | |||
| 269 | $[Tag after] | ||
| 270 | tableB = {} | ||
| 271 | |||
| 272 | return | ||
| 273 | ``` | ||
| 274 | |||
| 275 | Karena pernyataan berikutnya diteruskan sebagai argumen macro tambahan, anotasi juga bisa digunakan untuk menghasilkan kode registrasi langsung dari deklarasi class. Anda juga dapat menggunakan pemeriksaan argumen AST yang sama seperti pada macro biasa. | ||
| 276 | |||
| 277 | ```yuescript | ||
| 278 | macro Register = (registry, code`ClassDecl) -> | ||
| 279 | className = code\match "^class%s+(%w+)" | ||
| 280 | return | | ||
| 281 | #{registry}["#{className}"] = #{className} | ||
| 282 | |||
| 283 | registry = {} | ||
| 284 | |||
| 285 | $[Register(registry)] | ||
| 286 | class Worker | ||
| 287 | run: => "ok" | ||
| 288 | |||
| 289 | return | ||
| 290 | ``` | ||
| 291 | |||
| 292 | Anotasi juga bisa menyisipkan kode pembungkus di sekitar fungsi: | ||
| 293 | |||
| 294 | ```yuescript | ||
| 295 | macro ValidateNumberArgs = (code) -> | ||
| 296 | funcName = code\match "^(%w+)%s*=" | ||
| 297 | return | | ||
| 298 | local __orig_#{funcName} = #{funcName} | ||
| 299 | #{funcName} = (...) -> | ||
| 300 | for i = 1, select "#", ... | ||
| 301 | assert type(select i, ...) == "number", "expected number for arg \#{i}" | ||
| 302 | __orig_#{funcName} ... | ||
| 303 | |||
| 304 | $[ValidateNumberArgs] | ||
| 305 | add = (a, b) -> a + b | ||
| 306 | ``` | ||
| 307 | |||
| 308 | Sebuah anotasi harus selalu diikuti oleh sebuah pernyataan, dan tidak bisa diterapkan ke pernyataan `return`. Jika pernyataan yang dianotasi berada di akhir sebuah blok dan Anda membutuhkan bentuk AST mentah dari pernyataan itu, tambahkan `return` eksplisit setelahnya agar ia tidak dibungkus menjadi ekspresi yang dikembalikan secara implisit. | ||
| 309 | |||
| 240 | # Try | 310 | # Try |
| 241 | 311 | ||
| 242 | Sintaks untuk penanganan error Lua dalam bentuk umum. | 312 | Sintaks untuk penanganan error Lua dalam bentuk umum. |
diff --git a/doc/yue-pt-br.md b/doc/yue-pt-br.md index 2e9ab40..a858c50 100644 --- a/doc/yue-pt-br.md +++ b/doc/yue-pt-br.md | |||
| @@ -237,6 +237,76 @@ $printNumAndStr 123, "hello" | |||
| 237 | 237 | ||
| 238 | Para mais detalhes sobre os nós AST disponíveis, consulte as definições em maiúsculas em [yue_parser.cpp](https://github.com/IppClub/YueScript/blob/main/src/yuescript/yue_parser.cpp). | 238 | Para mais detalhes sobre os nós AST disponíveis, consulte as definições em maiúsculas em [yue_parser.cpp](https://github.com/IppClub/YueScript/blob/main/src/yuescript/yue_parser.cpp). |
| 239 | 239 | ||
| 240 | ## Instruções de anotação | ||
| 241 | |||
| 242 | As instruções de anotação aplicam uma macro à instrução logo em seguida. | ||
| 243 | |||
| 244 | Isso equivale a chamar a macro com o código-fonte da instrução seguinte anexado como último argumento. | ||
| 245 | |||
| 246 | ```yuescript | ||
| 247 | macro ShowName = (code) -> | | ||
| 248 | print "#{code\match '^[%w_]*'}" | ||
| 249 | |||
| 250 | $[ShowName] | ||
| 251 | myFunc = -> | ||
| 252 | |||
| 253 | return | ||
| 254 | ``` | ||
| 255 | |||
| 256 | Quando a macro de anotação retorna uma tabela de configuração, o campo opcional `before` controla se o resultado gerado será emitido antes ou depois da instrução anotada. | ||
| 257 | |||
| 258 | ```yuescript | ||
| 259 | macro Tag = (tag, code) -> | ||
| 260 | tableName = code\match "^[%w_]+" | ||
| 261 | return | ||
| 262 | type: "text" | ||
| 263 | before: tag == "before" | ||
| 264 | code: "-- #{tag}:#{tableName}" | ||
| 265 | |||
| 266 | $[Tag before] | ||
| 267 | tableA = {} | ||
| 268 | |||
| 269 | $[Tag after] | ||
| 270 | tableB = {} | ||
| 271 | |||
| 272 | return | ||
| 273 | ``` | ||
| 274 | |||
| 275 | Como a instrução seguinte é passada como um argumento extra para a macro, anotações também podem ser usadas para gerar código de registro a partir de declarações de classe. Você também pode usar as mesmas checagens de AST nos argumentos que macros normais oferecem. | ||
| 276 | |||
| 277 | ```yuescript | ||
| 278 | macro Register = (registry, code`ClassDecl) -> | ||
| 279 | className = code\match "^class%s+(%w+)" | ||
| 280 | return | | ||
| 281 | #{registry}["#{className}"] = #{className} | ||
| 282 | |||
| 283 | registry = {} | ||
| 284 | |||
| 285 | $[Register(registry)] | ||
| 286 | class Worker | ||
| 287 | run: => "ok" | ||
| 288 | |||
| 289 | return | ||
| 290 | ``` | ||
| 291 | |||
| 292 | Anotações também podem injetar código de empacotamento em volta de funções: | ||
| 293 | |||
| 294 | ```yuescript | ||
| 295 | macro ValidateNumberArgs = (code) -> | ||
| 296 | funcName = code\match "^(%w+)%s*=" | ||
| 297 | return | | ||
| 298 | local __orig_#{funcName} = #{funcName} | ||
| 299 | #{funcName} = (...) -> | ||
| 300 | for i = 1, select "#", ... | ||
| 301 | assert type(select i, ...) == "number", "expected number for arg \#{i}" | ||
| 302 | __orig_#{funcName} ... | ||
| 303 | |||
| 304 | $[ValidateNumberArgs] | ||
| 305 | add = (a, b) -> a + b | ||
| 306 | ``` | ||
| 307 | |||
| 308 | Uma anotação sempre precisa ser seguida por uma instrução, e ela não pode ser aplicada a uma instrução `return`. Se a instrução anotada aparecer no fim de um bloco e você precisar da forma AST bruta dessa instrução, adicione um `return` explícito em seguida para evitar que ela seja envolvida por uma expressão com retorno implícito. | ||
| 309 | |||
| 240 | # Try | 310 | # Try |
| 241 | 311 | ||
| 242 | A sintaxe para tratamento de erros do Lua em uma forma comum. | 312 | A sintaxe para tratamento de erros do Lua em uma forma comum. |
diff --git a/doc/yue-zh.md b/doc/yue-zh.md index e0f8b36..6494bf8 100644 --- a/doc/yue-zh.md +++ b/doc/yue-zh.md | |||
| @@ -237,6 +237,76 @@ $printNumAndStr 123, "hello" | |||
| 237 | 237 | ||
| 238 |   更多关于可用 AST 节点的详细信息,请参考 [yue_parser.cpp](https://github.com/IppClub/YueScript/blob/main/src/yuescript/yue_parser.cpp) 中大写的规则定义。 | 238 |   更多关于可用 AST 节点的详细信息,请参考 [yue_parser.cpp](https://github.com/IppClub/YueScript/blob/main/src/yuescript/yue_parser.cpp) 中大写的规则定义。 |
| 239 | 239 | ||
| 240 | ## 注解语句 | ||
| 241 | |||
| 242 |   注解语句会把一个宏应用到它后面的那条语句上。 | ||
| 243 | |||
| 244 |   这等价于调用该宏,并把后面那条语句的源码作为最后一个参数附加进去。 | ||
| 245 | |||
| 246 | ```yuescript | ||
| 247 | macro ShowName = (code) -> | | ||
| 248 | print "#{code\match '^[%w_]*'}" | ||
| 249 | |||
| 250 | $[ShowName] | ||
| 251 | myFunc = -> | ||
| 252 | |||
| 253 | return | ||
| 254 | ``` | ||
| 255 | |||
| 256 |   如果注解宏返回的是配置表,可选字段 `before` 可以控制生成结果插入到被注解语句之前还是之后。 | ||
| 257 | |||
| 258 | ```yuescript | ||
| 259 | macro Tag = (tag, code) -> | ||
| 260 | tableName = code\match "^[%w_]+" | ||
| 261 | return | ||
| 262 | type: "text" | ||
| 263 | before: tag == "before" | ||
| 264 | code: "-- #{tag}:#{tableName}" | ||
| 265 | |||
| 266 | $[Tag before] | ||
| 267 | tableA = {} | ||
| 268 | |||
| 269 | $[Tag after] | ||
| 270 | tableB = {} | ||
| 271 | |||
| 272 | return | ||
| 273 | ``` | ||
| 274 | |||
| 275 |   由于后面的语句会作为额外的宏参数传入,注解也可以从类声明生成注册代码。它同样可以使用普通宏支持的 AST 参数检查。 | ||
| 276 | |||
| 277 | ```yuescript | ||
| 278 | macro Register = (registry, code`ClassDecl) -> | ||
| 279 | className = code\match "^class%s+(%w+)" | ||
| 280 | return | | ||
| 281 | #{registry}["#{className}"] = #{className} | ||
| 282 | |||
| 283 | registry = {} | ||
| 284 | |||
| 285 | $[Register(registry)] | ||
| 286 | class Worker | ||
| 287 | run: => "ok" | ||
| 288 | |||
| 289 | return | ||
| 290 | ``` | ||
| 291 | |||
| 292 |   注解也可以用来给函数注入包装代码。 | ||
| 293 | |||
| 294 | ```yuescript | ||
| 295 | macro ValidateNumberArgs = (code) -> | ||
| 296 | funcName = code\match "^(%w+)%s*=" | ||
| 297 | return | | ||
| 298 | local __orig_#{funcName} = #{funcName} | ||
| 299 | #{funcName} = (...) -> | ||
| 300 | for i = 1, select "#", ... | ||
| 301 | assert type(select i, ...) == "number", "expected number for arg \#{i}" | ||
| 302 | __orig_#{funcName} ... | ||
| 303 | |||
| 304 | $[ValidateNumberArgs] | ||
| 305 | add = (a, b) -> a + b | ||
| 306 | ``` | ||
| 307 | |||
| 308 |   注解后面必须紧跟一条语句,而且不能作用在 `return` 语句上。如果被注解的语句正好位于代码块末尾,而你又需要拿到原始的语句 AST 形态,就需要额外补一个显式的 `return`,避免它被隐式返回表达式包起来。 | ||
| 309 | |||
| 240 | # 错误处理 | 310 | # 错误处理 |
| 241 | 311 | ||
| 242 |   用于统一进行 Lua 错误处理的便捷语法。 | 312 |   用于统一进行 Lua 错误处理的便捷语法。 |
diff --git a/spec/outputs/codes_from_doc_de.lua b/spec/outputs/codes_from_doc_de.lua index 2530977..e1346db 100644 --- a/spec/outputs/codes_from_doc_de.lua +++ b/spec/outputs/codes_from_doc_de.lua | |||
| @@ -143,6 +143,61 @@ end | |||
| 143 | do | 143 | do |
| 144 | print(123, "hallo") | 144 | print(123, "hallo") |
| 145 | end | 145 | end |
| 146 | local myFunc | ||
| 147 | myFunc = function() end | ||
| 148 | do | ||
| 149 | print("myFunc") | ||
| 150 | end | ||
| 151 | return | ||
| 152 | -- before:tableA | ||
| 153 | local tableA = { } | ||
| 154 | local tableB = { } | ||
| 155 | -- after:tableB | ||
| 156 | return | ||
| 157 | local registry = { } | ||
| 158 | local Worker | ||
| 159 | do | ||
| 160 | local _class_0 | ||
| 161 | local _base_0 = { | ||
| 162 | run = function(self) | ||
| 163 | return "ok" | ||
| 164 | end | ||
| 165 | } | ||
| 166 | if _base_0.__index == nil then | ||
| 167 | _base_0.__index = _base_0 | ||
| 168 | end | ||
| 169 | _class_0 = setmetatable({ | ||
| 170 | __init = function() end, | ||
| 171 | __base = _base_0, | ||
| 172 | __name = "Worker" | ||
| 173 | }, { | ||
| 174 | __index = _base_0, | ||
| 175 | __call = function(cls, ...) | ||
| 176 | local _self_0 = setmetatable({ }, _base_0) | ||
| 177 | cls.__init(_self_0, ...) | ||
| 178 | return _self_0 | ||
| 179 | end | ||
| 180 | }) | ||
| 181 | _base_0.__class = _class_0 | ||
| 182 | Worker = _class_0 | ||
| 183 | end | ||
| 184 | do | ||
| 185 | registry["Worker"] = Worker | ||
| 186 | end | ||
| 187 | return | ||
| 188 | local add | ||
| 189 | add = function(a, b) | ||
| 190 | return a + b | ||
| 191 | end | ||
| 192 | do | ||
| 193 | local __orig_add = add | ||
| 194 | add = function(...) | ||
| 195 | for i = 1, select("#", ...) do | ||
| 196 | assert(type(select(i, ...)) == "number", "expected number for arg " .. tostring(i)) | ||
| 197 | end | ||
| 198 | return __orig_add(...) | ||
| 199 | end | ||
| 200 | end | ||
| 146 | local area = 6.2831853071796 * 5 | 201 | local area = 6.2831853071796 * 5 |
| 147 | print('Hallo Welt') | 202 | print('Hallo Welt') |
| 148 | do | 203 | do |
| @@ -194,6 +249,61 @@ end | |||
| 194 | do | 249 | do |
| 195 | print(123, "hallo") | 250 | print(123, "hallo") |
| 196 | end | 251 | end |
| 252 | local myFunc | ||
| 253 | myFunc = function() end | ||
| 254 | do | ||
| 255 | print("myFunc") | ||
| 256 | end | ||
| 257 | return | ||
| 258 | -- before:tableA | ||
| 259 | local tableA = { } | ||
| 260 | local tableB = { } | ||
| 261 | -- after:tableB | ||
| 262 | return | ||
| 263 | local registry = { } | ||
| 264 | local Worker | ||
| 265 | do | ||
| 266 | local _class_0 | ||
| 267 | local _base_0 = { | ||
| 268 | run = function(self) | ||
| 269 | return "ok" | ||
| 270 | end | ||
| 271 | } | ||
| 272 | if _base_0.__index == nil then | ||
| 273 | _base_0.__index = _base_0 | ||
| 274 | end | ||
| 275 | _class_0 = setmetatable({ | ||
| 276 | __init = function() end, | ||
| 277 | __base = _base_0, | ||
| 278 | __name = "Worker" | ||
| 279 | }, { | ||
| 280 | __index = _base_0, | ||
| 281 | __call = function(cls, ...) | ||
| 282 | local _self_0 = setmetatable({ }, _base_0) | ||
| 283 | cls.__init(_self_0, ...) | ||
| 284 | return _self_0 | ||
| 285 | end | ||
| 286 | }) | ||
| 287 | _base_0.__class = _class_0 | ||
| 288 | Worker = _class_0 | ||
| 289 | end | ||
| 290 | do | ||
| 291 | registry["Worker"] = Worker | ||
| 292 | end | ||
| 293 | return | ||
| 294 | local add | ||
| 295 | add = function(a, b) | ||
| 296 | return a + b | ||
| 297 | end | ||
| 298 | do | ||
| 299 | local __orig_add = add | ||
| 300 | add = function(...) | ||
| 301 | for i = 1, select("#", ...) do | ||
| 302 | assert(type(select(i, ...)) == "number", "expected number for arg " .. tostring(i)) | ||
| 303 | end | ||
| 304 | return __orig_add(...) | ||
| 305 | end | ||
| 306 | end | ||
| 197 | xpcall(function() | 307 | xpcall(function() |
| 198 | return func(1, 2, 3) | 308 | return func(1, 2, 3) |
| 199 | end, function(err) | 309 | end, function(err) |
diff --git a/spec/outputs/codes_from_doc_en.lua b/spec/outputs/codes_from_doc_en.lua index 18f0227..1a6469d 100644 --- a/spec/outputs/codes_from_doc_en.lua +++ b/spec/outputs/codes_from_doc_en.lua | |||
| @@ -143,6 +143,61 @@ end | |||
| 143 | do | 143 | do |
| 144 | print(123, "hello") | 144 | print(123, "hello") |
| 145 | end | 145 | end |
| 146 | local myFunc | ||
| 147 | myFunc = function() end | ||
| 148 | do | ||
| 149 | print("myFunc") | ||
| 150 | end | ||
| 151 | return | ||
| 152 | -- before:tableA | ||
| 153 | local tableA = { } | ||
| 154 | local tableB = { } | ||
| 155 | -- after:tableB | ||
| 156 | return | ||
| 157 | local registry = { } | ||
| 158 | local Worker | ||
| 159 | do | ||
| 160 | local _class_0 | ||
| 161 | local _base_0 = { | ||
| 162 | run = function(self) | ||
| 163 | return "ok" | ||
| 164 | end | ||
| 165 | } | ||
| 166 | if _base_0.__index == nil then | ||
| 167 | _base_0.__index = _base_0 | ||
| 168 | end | ||
| 169 | _class_0 = setmetatable({ | ||
| 170 | __init = function() end, | ||
| 171 | __base = _base_0, | ||
| 172 | __name = "Worker" | ||
| 173 | }, { | ||
| 174 | __index = _base_0, | ||
| 175 | __call = function(cls, ...) | ||
| 176 | local _self_0 = setmetatable({ }, _base_0) | ||
| 177 | cls.__init(_self_0, ...) | ||
| 178 | return _self_0 | ||
| 179 | end | ||
| 180 | }) | ||
| 181 | _base_0.__class = _class_0 | ||
| 182 | Worker = _class_0 | ||
| 183 | end | ||
| 184 | do | ||
| 185 | registry["Worker"] = Worker | ||
| 186 | end | ||
| 187 | return | ||
| 188 | local add | ||
| 189 | add = function(a, b) | ||
| 190 | return a + b | ||
| 191 | end | ||
| 192 | do | ||
| 193 | local __orig_add = add | ||
| 194 | add = function(...) | ||
| 195 | for i = 1, select("#", ...) do | ||
| 196 | assert(type(select(i, ...)) == "number", "expected number for arg " .. tostring(i)) | ||
| 197 | end | ||
| 198 | return __orig_add(...) | ||
| 199 | end | ||
| 200 | end | ||
| 146 | local area = 6.2831853071796 * 5 | 201 | local area = 6.2831853071796 * 5 |
| 147 | print('hello world') | 202 | print('hello world') |
| 148 | do | 203 | do |
| @@ -194,6 +249,61 @@ end | |||
| 194 | do | 249 | do |
| 195 | print(123, "hello") | 250 | print(123, "hello") |
| 196 | end | 251 | end |
| 252 | local myFunc | ||
| 253 | myFunc = function() end | ||
| 254 | do | ||
| 255 | print("myFunc") | ||
| 256 | end | ||
| 257 | return | ||
| 258 | -- before:tableA | ||
| 259 | local tableA = { } | ||
| 260 | local tableB = { } | ||
| 261 | -- after:tableB | ||
| 262 | return | ||
| 263 | local registry = { } | ||
| 264 | local Worker | ||
| 265 | do | ||
| 266 | local _class_0 | ||
| 267 | local _base_0 = { | ||
| 268 | run = function(self) | ||
| 269 | return "ok" | ||
| 270 | end | ||
| 271 | } | ||
| 272 | if _base_0.__index == nil then | ||
| 273 | _base_0.__index = _base_0 | ||
| 274 | end | ||
| 275 | _class_0 = setmetatable({ | ||
| 276 | __init = function() end, | ||
| 277 | __base = _base_0, | ||
| 278 | __name = "Worker" | ||
| 279 | }, { | ||
| 280 | __index = _base_0, | ||
| 281 | __call = function(cls, ...) | ||
| 282 | local _self_0 = setmetatable({ }, _base_0) | ||
| 283 | cls.__init(_self_0, ...) | ||
| 284 | return _self_0 | ||
| 285 | end | ||
| 286 | }) | ||
| 287 | _base_0.__class = _class_0 | ||
| 288 | Worker = _class_0 | ||
| 289 | end | ||
| 290 | do | ||
| 291 | registry["Worker"] = Worker | ||
| 292 | end | ||
| 293 | return | ||
| 294 | local add | ||
| 295 | add = function(a, b) | ||
| 296 | return a + b | ||
| 297 | end | ||
| 298 | do | ||
| 299 | local __orig_add = add | ||
| 300 | add = function(...) | ||
| 301 | for i = 1, select("#", ...) do | ||
| 302 | assert(type(select(i, ...)) == "number", "expected number for arg " .. tostring(i)) | ||
| 303 | end | ||
| 304 | return __orig_add(...) | ||
| 305 | end | ||
| 306 | end | ||
| 197 | xpcall(function() | 307 | xpcall(function() |
| 198 | return func(1, 2, 3) | 308 | return func(1, 2, 3) |
| 199 | end, function(err) | 309 | end, function(err) |
diff --git a/spec/outputs/codes_from_doc_id-id.lua b/spec/outputs/codes_from_doc_id-id.lua index 452d88d..868c6ec 100644 --- a/spec/outputs/codes_from_doc_id-id.lua +++ b/spec/outputs/codes_from_doc_id-id.lua | |||
| @@ -143,6 +143,61 @@ end | |||
| 143 | do | 143 | do |
| 144 | print(123, "hello") | 144 | print(123, "hello") |
| 145 | end | 145 | end |
| 146 | local myFunc | ||
| 147 | myFunc = function() end | ||
| 148 | do | ||
| 149 | print("myFunc") | ||
| 150 | end | ||
| 151 | return | ||
| 152 | -- before:tableA | ||
| 153 | local tableA = { } | ||
| 154 | local tableB = { } | ||
| 155 | -- after:tableB | ||
| 156 | return | ||
| 157 | local registry = { } | ||
| 158 | local Worker | ||
| 159 | do | ||
| 160 | local _class_0 | ||
| 161 | local _base_0 = { | ||
| 162 | run = function(self) | ||
| 163 | return "ok" | ||
| 164 | end | ||
| 165 | } | ||
| 166 | if _base_0.__index == nil then | ||
| 167 | _base_0.__index = _base_0 | ||
| 168 | end | ||
| 169 | _class_0 = setmetatable({ | ||
| 170 | __init = function() end, | ||
| 171 | __base = _base_0, | ||
| 172 | __name = "Worker" | ||
| 173 | }, { | ||
| 174 | __index = _base_0, | ||
| 175 | __call = function(cls, ...) | ||
| 176 | local _self_0 = setmetatable({ }, _base_0) | ||
| 177 | cls.__init(_self_0, ...) | ||
| 178 | return _self_0 | ||
| 179 | end | ||
| 180 | }) | ||
| 181 | _base_0.__class = _class_0 | ||
| 182 | Worker = _class_0 | ||
| 183 | end | ||
| 184 | do | ||
| 185 | registry["Worker"] = Worker | ||
| 186 | end | ||
| 187 | return | ||
| 188 | local add | ||
| 189 | add = function(a, b) | ||
| 190 | return a + b | ||
| 191 | end | ||
| 192 | do | ||
| 193 | local __orig_add = add | ||
| 194 | add = function(...) | ||
| 195 | for i = 1, select("#", ...) do | ||
| 196 | assert(type(select(i, ...)) == "number", "expected number for arg " .. tostring(i)) | ||
| 197 | end | ||
| 198 | return __orig_add(...) | ||
| 199 | end | ||
| 200 | end | ||
| 146 | local area = 6.2831853071796 * 5 | 201 | local area = 6.2831853071796 * 5 |
| 147 | print('hello world') | 202 | print('hello world') |
| 148 | do | 203 | do |
| @@ -194,6 +249,61 @@ end | |||
| 194 | do | 249 | do |
| 195 | print(123, "hello") | 250 | print(123, "hello") |
| 196 | end | 251 | end |
| 252 | local myFunc | ||
| 253 | myFunc = function() end | ||
| 254 | do | ||
| 255 | print("myFunc") | ||
| 256 | end | ||
| 257 | return | ||
| 258 | -- before:tableA | ||
| 259 | local tableA = { } | ||
| 260 | local tableB = { } | ||
| 261 | -- after:tableB | ||
| 262 | return | ||
| 263 | local registry = { } | ||
| 264 | local Worker | ||
| 265 | do | ||
| 266 | local _class_0 | ||
| 267 | local _base_0 = { | ||
| 268 | run = function(self) | ||
| 269 | return "ok" | ||
| 270 | end | ||
| 271 | } | ||
| 272 | if _base_0.__index == nil then | ||
| 273 | _base_0.__index = _base_0 | ||
| 274 | end | ||
| 275 | _class_0 = setmetatable({ | ||
| 276 | __init = function() end, | ||
| 277 | __base = _base_0, | ||
| 278 | __name = "Worker" | ||
| 279 | }, { | ||
| 280 | __index = _base_0, | ||
| 281 | __call = function(cls, ...) | ||
| 282 | local _self_0 = setmetatable({ }, _base_0) | ||
| 283 | cls.__init(_self_0, ...) | ||
| 284 | return _self_0 | ||
| 285 | end | ||
| 286 | }) | ||
| 287 | _base_0.__class = _class_0 | ||
| 288 | Worker = _class_0 | ||
| 289 | end | ||
| 290 | do | ||
| 291 | registry["Worker"] = Worker | ||
| 292 | end | ||
| 293 | return | ||
| 294 | local add | ||
| 295 | add = function(a, b) | ||
| 296 | return a + b | ||
| 297 | end | ||
| 298 | do | ||
| 299 | local __orig_add = add | ||
| 300 | add = function(...) | ||
| 301 | for i = 1, select("#", ...) do | ||
| 302 | assert(type(select(i, ...)) == "number", "expected number for arg " .. tostring(i)) | ||
| 303 | end | ||
| 304 | return __orig_add(...) | ||
| 305 | end | ||
| 306 | end | ||
| 197 | xpcall(function() | 307 | xpcall(function() |
| 198 | return func(1, 2, 3) | 308 | return func(1, 2, 3) |
| 199 | end, function(err) | 309 | end, function(err) |
diff --git a/spec/outputs/codes_from_doc_pt-br.lua b/spec/outputs/codes_from_doc_pt-br.lua index f9784d7..ce8c016 100644 --- a/spec/outputs/codes_from_doc_pt-br.lua +++ b/spec/outputs/codes_from_doc_pt-br.lua | |||
| @@ -143,6 +143,61 @@ end | |||
| 143 | do | 143 | do |
| 144 | print(123, "hello") | 144 | print(123, "hello") |
| 145 | end | 145 | end |
| 146 | local myFunc | ||
| 147 | myFunc = function() end | ||
| 148 | do | ||
| 149 | print("myFunc") | ||
| 150 | end | ||
| 151 | return | ||
| 152 | -- before:tableA | ||
| 153 | local tableA = { } | ||
| 154 | local tableB = { } | ||
| 155 | -- after:tableB | ||
| 156 | return | ||
| 157 | local registry = { } | ||
| 158 | local Worker | ||
| 159 | do | ||
| 160 | local _class_0 | ||
| 161 | local _base_0 = { | ||
| 162 | run = function(self) | ||
| 163 | return "ok" | ||
| 164 | end | ||
| 165 | } | ||
| 166 | if _base_0.__index == nil then | ||
| 167 | _base_0.__index = _base_0 | ||
| 168 | end | ||
| 169 | _class_0 = setmetatable({ | ||
| 170 | __init = function() end, | ||
| 171 | __base = _base_0, | ||
| 172 | __name = "Worker" | ||
| 173 | }, { | ||
| 174 | __index = _base_0, | ||
| 175 | __call = function(cls, ...) | ||
| 176 | local _self_0 = setmetatable({ }, _base_0) | ||
| 177 | cls.__init(_self_0, ...) | ||
| 178 | return _self_0 | ||
| 179 | end | ||
| 180 | }) | ||
| 181 | _base_0.__class = _class_0 | ||
| 182 | Worker = _class_0 | ||
| 183 | end | ||
| 184 | do | ||
| 185 | registry["Worker"] = Worker | ||
| 186 | end | ||
| 187 | return | ||
| 188 | local add | ||
| 189 | add = function(a, b) | ||
| 190 | return a + b | ||
| 191 | end | ||
| 192 | do | ||
| 193 | local __orig_add = add | ||
| 194 | add = function(...) | ||
| 195 | for i = 1, select("#", ...) do | ||
| 196 | assert(type(select(i, ...)) == "number", "expected number for arg " .. tostring(i)) | ||
| 197 | end | ||
| 198 | return __orig_add(...) | ||
| 199 | end | ||
| 200 | end | ||
| 146 | local area = 6.2831853071796 * 5 | 201 | local area = 6.2831853071796 * 5 |
| 147 | print('hello world') | 202 | print('hello world') |
| 148 | do | 203 | do |
| @@ -194,6 +249,61 @@ end | |||
| 194 | do | 249 | do |
| 195 | print(123, "hello") | 250 | print(123, "hello") |
| 196 | end | 251 | end |
| 252 | local myFunc | ||
| 253 | myFunc = function() end | ||
| 254 | do | ||
| 255 | print("myFunc") | ||
| 256 | end | ||
| 257 | return | ||
| 258 | -- before:tableA | ||
| 259 | local tableA = { } | ||
| 260 | local tableB = { } | ||
| 261 | -- after:tableB | ||
| 262 | return | ||
| 263 | local registry = { } | ||
| 264 | local Worker | ||
| 265 | do | ||
| 266 | local _class_0 | ||
| 267 | local _base_0 = { | ||
| 268 | run = function(self) | ||
| 269 | return "ok" | ||
| 270 | end | ||
| 271 | } | ||
| 272 | if _base_0.__index == nil then | ||
| 273 | _base_0.__index = _base_0 | ||
| 274 | end | ||
| 275 | _class_0 = setmetatable({ | ||
| 276 | __init = function() end, | ||
| 277 | __base = _base_0, | ||
| 278 | __name = "Worker" | ||
| 279 | }, { | ||
| 280 | __index = _base_0, | ||
| 281 | __call = function(cls, ...) | ||
| 282 | local _self_0 = setmetatable({ }, _base_0) | ||
| 283 | cls.__init(_self_0, ...) | ||
| 284 | return _self_0 | ||
| 285 | end | ||
| 286 | }) | ||
| 287 | _base_0.__class = _class_0 | ||
| 288 | Worker = _class_0 | ||
| 289 | end | ||
| 290 | do | ||
| 291 | registry["Worker"] = Worker | ||
| 292 | end | ||
| 293 | return | ||
| 294 | local add | ||
| 295 | add = function(a, b) | ||
| 296 | return a + b | ||
| 297 | end | ||
| 298 | do | ||
| 299 | local __orig_add = add | ||
| 300 | add = function(...) | ||
| 301 | for i = 1, select("#", ...) do | ||
| 302 | assert(type(select(i, ...)) == "number", "expected number for arg " .. tostring(i)) | ||
| 303 | end | ||
| 304 | return __orig_add(...) | ||
| 305 | end | ||
| 306 | end | ||
| 197 | xpcall(function() | 307 | xpcall(function() |
| 198 | return func(1, 2, 3) | 308 | return func(1, 2, 3) |
| 199 | end, function(err) | 309 | end, function(err) |
diff --git a/spec/outputs/codes_from_doc_zh.lua b/spec/outputs/codes_from_doc_zh.lua index 4c254a2..2f849b8 100644 --- a/spec/outputs/codes_from_doc_zh.lua +++ b/spec/outputs/codes_from_doc_zh.lua | |||
| @@ -143,6 +143,61 @@ end | |||
| 143 | do | 143 | do |
| 144 | print(123, "hello") | 144 | print(123, "hello") |
| 145 | end | 145 | end |
| 146 | local myFunc | ||
| 147 | myFunc = function() end | ||
| 148 | do | ||
| 149 | print("myFunc") | ||
| 150 | end | ||
| 151 | return | ||
| 152 | -- before:tableA | ||
| 153 | local tableA = { } | ||
| 154 | local tableB = { } | ||
| 155 | -- after:tableB | ||
| 156 | return | ||
| 157 | local registry = { } | ||
| 158 | local Worker | ||
| 159 | do | ||
| 160 | local _class_0 | ||
| 161 | local _base_0 = { | ||
| 162 | run = function(self) | ||
| 163 | return "ok" | ||
| 164 | end | ||
| 165 | } | ||
| 166 | if _base_0.__index == nil then | ||
| 167 | _base_0.__index = _base_0 | ||
| 168 | end | ||
| 169 | _class_0 = setmetatable({ | ||
| 170 | __init = function() end, | ||
| 171 | __base = _base_0, | ||
| 172 | __name = "Worker" | ||
| 173 | }, { | ||
| 174 | __index = _base_0, | ||
| 175 | __call = function(cls, ...) | ||
| 176 | local _self_0 = setmetatable({ }, _base_0) | ||
| 177 | cls.__init(_self_0, ...) | ||
| 178 | return _self_0 | ||
| 179 | end | ||
| 180 | }) | ||
| 181 | _base_0.__class = _class_0 | ||
| 182 | Worker = _class_0 | ||
| 183 | end | ||
| 184 | do | ||
| 185 | registry["Worker"] = Worker | ||
| 186 | end | ||
| 187 | return | ||
| 188 | local add | ||
| 189 | add = function(a, b) | ||
| 190 | return a + b | ||
| 191 | end | ||
| 192 | do | ||
| 193 | local __orig_add = add | ||
| 194 | add = function(...) | ||
| 195 | for i = 1, select("#", ...) do | ||
| 196 | assert(type(select(i, ...)) == "number", "expected number for arg " .. tostring(i)) | ||
| 197 | end | ||
| 198 | return __orig_add(...) | ||
| 199 | end | ||
| 200 | end | ||
| 146 | local area = 6.2831853071796 * 5 | 201 | local area = 6.2831853071796 * 5 |
| 147 | print('你好 世界') | 202 | print('你好 世界') |
| 148 | do | 203 | do |
| @@ -194,6 +249,61 @@ end | |||
| 194 | do | 249 | do |
| 195 | print(123, "hello") | 250 | print(123, "hello") |
| 196 | end | 251 | end |
| 252 | local myFunc | ||
| 253 | myFunc = function() end | ||
| 254 | do | ||
| 255 | print("myFunc") | ||
| 256 | end | ||
| 257 | return | ||
| 258 | -- before:tableA | ||
| 259 | local tableA = { } | ||
| 260 | local tableB = { } | ||
| 261 | -- after:tableB | ||
| 262 | return | ||
| 263 | local registry = { } | ||
| 264 | local Worker | ||
| 265 | do | ||
| 266 | local _class_0 | ||
| 267 | local _base_0 = { | ||
| 268 | run = function(self) | ||
| 269 | return "ok" | ||
| 270 | end | ||
| 271 | } | ||
| 272 | if _base_0.__index == nil then | ||
| 273 | _base_0.__index = _base_0 | ||
| 274 | end | ||
| 275 | _class_0 = setmetatable({ | ||
| 276 | __init = function() end, | ||
| 277 | __base = _base_0, | ||
| 278 | __name = "Worker" | ||
| 279 | }, { | ||
| 280 | __index = _base_0, | ||
| 281 | __call = function(cls, ...) | ||
| 282 | local _self_0 = setmetatable({ }, _base_0) | ||
| 283 | cls.__init(_self_0, ...) | ||
| 284 | return _self_0 | ||
| 285 | end | ||
| 286 | }) | ||
| 287 | _base_0.__class = _class_0 | ||
| 288 | Worker = _class_0 | ||
| 289 | end | ||
| 290 | do | ||
| 291 | registry["Worker"] = Worker | ||
| 292 | end | ||
| 293 | return | ||
| 294 | local add | ||
| 295 | add = function(a, b) | ||
| 296 | return a + b | ||
| 297 | end | ||
| 298 | do | ||
| 299 | local __orig_add = add | ||
| 300 | add = function(...) | ||
| 301 | for i = 1, select("#", ...) do | ||
| 302 | assert(type(select(i, ...)) == "number", "expected number for arg " .. tostring(i)) | ||
| 303 | end | ||
| 304 | return __orig_add(...) | ||
| 305 | end | ||
| 306 | end | ||
| 197 | xpcall(function() | 307 | xpcall(function() |
| 198 | return func(1, 2, 3) | 308 | return func(1, 2, 3) |
| 199 | end, function(err) | 309 | end, function(err) |
