aboutsummaryrefslogtreecommitdiff
path: root/doc/docs
diff options
context:
space:
mode:
Diffstat (limited to 'doc/docs')
-rw-r--r--doc/docs/de/doc/advanced/macro.md142
-rw-r--r--doc/docs/doc/advanced/macro.md153
-rw-r--r--doc/docs/id-id/doc/advanced/macro.md142
-rw-r--r--doc/docs/pt-br/doc/advanced/macro.md142
-rw-r--r--doc/docs/zh/doc/advanced/macro.md142
5 files changed, 720 insertions, 1 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
340Weitere Details zu verfügbaren AST-Knoten findest du in den großgeschriebenen Definitionen in `yue_parser.cpp`. 340Weitere Details zu verfügbaren AST-Knoten findest du in den großgeschriebenen Definitionen in `yue_parser.cpp`.
341
342## Annotation-Anweisungen
343
344Annotation-Anweisungen wenden ein Makro auf die direkt folgende Anweisung an.
345
346Das entspricht einem Makroaufruf, bei dem der Quelltext der folgenden Anweisung als letztes Argument zusätzlich übergeben wird.
347
348```yuescript
349macro ShowName = (code) -> |
350 print "#{code\match '^[%w_]*'}"
351
352$[ShowName]
353myFunc = ->
354
355return
356```
357
358<YueDisplay>
359
360```yue
361macro ShowName = (code) -> |
362 print "#{code\match '^[%w_]*'}"
363
364$[ShowName]
365myFunc = ->
366
367return
368```
369
370</YueDisplay>
371
372Wenn 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
375macro 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]
383tableA = {}
384
385$[Tag after]
386tableB = {}
387
388return
389```
390
391<YueDisplay>
392
393```yue
394macro 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]
402tableA = {}
403
404$[Tag after]
405tableB = {}
406
407return
408```
409
410</YueDisplay>
411
412Da 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
415macro Register = (registry, code`ClassDecl) ->
416 className = code\match "^class%s+(%w+)"
417 return |
418 #{registry}["#{className}"] = #{className}
419
420registry = {}
421
422$[Register(registry)]
423class Worker
424 run: => "ok"
425
426return
427```
428
429<YueDisplay>
430
431```yue
432macro Register = (registry, code`ClassDecl) ->
433 className = code\match "^class%s+(%w+)"
434 return |
435 #{registry}["#{className}"] = #{className}
436
437registry = {}
438
439$[Register(registry)]
440class Worker
441 run: => "ok"
442
443return
444```
445
446</YueDisplay>
447
448Annotationen können auch Wrapper-Code um Funktionen herum einfügen:
449
450```yuescript
451macro 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]
461add = (a, b) -> a + b
462```
463
464<YueDisplay>
465
466```yue
467macro 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]
477add = (a, b) -> a + b
478```
479
480</YueDisplay>
481
482Auf 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
71A macro function can either return a YueScript string or a config table containing Lua codes. 71A macro function can either return a YueScript string or a config table containing generated code.
72 72
73```yuescript 73```yuescript
74macro yueFunc = (var) -> "local #{var} = ->" 74macro yueFunc = (var) -> "local #{var} = ->"
@@ -126,6 +126,15 @@ end
126 126
127</YueDisplay> 127</YueDisplay>
128 128
129The 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
136In 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
131Macro 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. 140Macro 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
340For 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). 349For 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
353Annotation statements apply a macro to the statement immediately following them.
354
355This is equivalent to calling the macro with the following statement's source text appended as the last argument.
356
357```yuescript
358macro ShowName = (code) -> |
359 print "#{code\match '^[%w_]*'}"
360
361$[ShowName]
362myFunc = ->
363
364return
365```
366
367<YueDisplay>
368
369```yue
370macro ShowName = (code) -> |
371 print "#{code\match '^[%w_]*'}"
372
373$[ShowName]
374myFunc = ->
375
376return
377```
378
379</YueDisplay>
380
381When 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
384macro 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]
392tableA = {}
393
394$[Tag after]
395tableB = {}
396
397return
398```
399
400<YueDisplay>
401
402```yue
403macro 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]
411tableA = {}
412
413$[Tag after]
414tableB = {}
415
416return
417```
418
419</YueDisplay>
420
421Because 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
424macro Register = (registry, code`ClassDecl) ->
425 className = code\match "^class%s+(%w+)"
426 return |
427 #{registry}["#{className}"] = #{className}
428
429registry = {}
430
431$[Register(registry)]
432class Worker
433 run: => "ok"
434
435return
436```
437
438<YueDisplay>
439
440```yue
441macro Register = (registry, code`ClassDecl) ->
442 className = code\match "^class%s+(%w+)"
443 return |
444 #{registry}["#{className}"] = #{className}
445
446registry = {}
447
448$[Register(registry)]
449class Worker
450 run: => "ok"
451
452return
453```
454
455</YueDisplay>
456
457Annotations can also inject wrapper code around functions:
458
459```yuescript
460macro 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]
470add = (a, b) -> a + b
471```
472
473<YueDisplay>
474
475```yue
476macro 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]
486add = (a, b) -> a + b
487```
488
489</YueDisplay>
490
491An 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
340Untuk 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). 340Untuk 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
344Pernyataan anotasi menerapkan sebuah macro ke pernyataan yang tepat mengikutinya.
345
346Ini setara dengan memanggil macro sambil menambahkan kode sumber dari pernyataan berikutnya sebagai argumen terakhir.
347
348```yuescript
349macro ShowName = (code) -> |
350 print "#{code\match '^[%w_]*'}"
351
352$[ShowName]
353myFunc = ->
354
355return
356```
357
358<YueDisplay>
359
360```yue
361macro ShowName = (code) -> |
362 print "#{code\match '^[%w_]*'}"
363
364$[ShowName]
365myFunc = ->
366
367return
368```
369
370</YueDisplay>
371
372Saat macro anotasi mengembalikan tabel konfigurasi, field opsional `before` mengatur apakah hasil yang dihasilkan akan diletakkan sebelum atau sesudah pernyataan yang dianotasi.
373
374```yuescript
375macro 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]
383tableA = {}
384
385$[Tag after]
386tableB = {}
387
388return
389```
390
391<YueDisplay>
392
393```yue
394macro 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]
402tableA = {}
403
404$[Tag after]
405tableB = {}
406
407return
408```
409
410</YueDisplay>
411
412Karena 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
415macro Register = (registry, code`ClassDecl) ->
416 className = code\match "^class%s+(%w+)"
417 return |
418 #{registry}["#{className}"] = #{className}
419
420registry = {}
421
422$[Register(registry)]
423class Worker
424 run: => "ok"
425
426return
427```
428
429<YueDisplay>
430
431```yue
432macro Register = (registry, code`ClassDecl) ->
433 className = code\match "^class%s+(%w+)"
434 return |
435 #{registry}["#{className}"] = #{className}
436
437registry = {}
438
439$[Register(registry)]
440class Worker
441 run: => "ok"
442
443return
444```
445
446</YueDisplay>
447
448Anotasi juga bisa menyisipkan kode pembungkus di sekitar fungsi:
449
450```yuescript
451macro 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]
461add = (a, b) -> a + b
462```
463
464<YueDisplay>
465
466```yue
467macro 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]
477add = (a, b) -> a + b
478```
479
480</YueDisplay>
481
482Sebuah 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
339Para 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). 339Para 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
343As instruções de anotação aplicam uma macro à instrução logo em seguida.
344
345Isso equivale a chamar a macro com o código-fonte da instrução seguinte anexado como último argumento.
346
347```yuescript
348macro ShowName = (code) -> |
349 print "#{code\match '^[%w_]*'}"
350
351$[ShowName]
352myFunc = ->
353
354return
355```
356
357<YueDisplay>
358
359```yue
360macro ShowName = (code) -> |
361 print "#{code\match '^[%w_]*'}"
362
363$[ShowName]
364myFunc = ->
365
366return
367```
368
369</YueDisplay>
370
371Quando 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
374macro 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]
382tableA = {}
383
384$[Tag after]
385tableB = {}
386
387return
388```
389
390<YueDisplay>
391
392```yue
393macro 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]
401tableA = {}
402
403$[Tag after]
404tableB = {}
405
406return
407```
408
409</YueDisplay>
410
411Como 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
414macro Register = (registry, code`ClassDecl) ->
415 className = code\match "^class%s+(%w+)"
416 return |
417 #{registry}["#{className}"] = #{className}
418
419registry = {}
420
421$[Register(registry)]
422class Worker
423 run: => "ok"
424
425return
426```
427
428<YueDisplay>
429
430```yue
431macro Register = (registry, code`ClassDecl) ->
432 className = code\match "^class%s+(%w+)"
433 return |
434 #{registry}["#{className}"] = #{className}
435
436registry = {}
437
438$[Register(registry)]
439class Worker
440 run: => "ok"
441
442return
443```
444
445</YueDisplay>
446
447Anotações também podem injetar código de empacotamento em volta de funções:
448
449```yuescript
450macro 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]
460add = (a, b) -> a + b
461```
462
463<YueDisplay>
464
465```yue
466macro 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]
476add = (a, b) -> a + b
477```
478
479</YueDisplay>
480
481Uma 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&emsp;&emsp;更多关于可用 AST 节点的详细信息,请参考 [yue_parser.cpp](https://github.com/IppClub/YueScript/blob/main/src/yuescript/yue_parser.cpp) 中大写的规则定义。 340&emsp;&emsp;更多关于可用 AST 节点的详细信息,请参考 [yue_parser.cpp](https://github.com/IppClub/YueScript/blob/main/src/yuescript/yue_parser.cpp) 中大写的规则定义。
341
342## 注解语句
343
344&emsp;&emsp;注解语句会把一个宏应用到它后面的那条语句上。
345
346&emsp;&emsp;这等价于调用该宏,并把后面那条语句的源码作为最后一个参数附加进去。
347
348```yuescript
349macro ShowName = (code) -> |
350 print "#{code\match '^[%w_]*'}"
351
352$[ShowName]
353myFunc = ->
354
355return
356```
357
358<YueDisplay>
359
360```yue
361macro ShowName = (code) -> |
362 print "#{code\match '^[%w_]*'}"
363
364$[ShowName]
365myFunc = ->
366
367return
368```
369
370</YueDisplay>
371
372&emsp;&emsp;如果注解宏返回的是配置表,可选字段 `before` 可以控制生成结果插入到被注解语句之前还是之后。
373
374```yuescript
375macro 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]
383tableA = {}
384
385$[Tag after]
386tableB = {}
387
388return
389```
390
391<YueDisplay>
392
393```yue
394macro 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]
402tableA = {}
403
404$[Tag after]
405tableB = {}
406
407return
408```
409
410</YueDisplay>
411
412&emsp;&emsp;由于后面的语句会作为额外的宏参数传入,注解也可以从类声明生成注册代码。它同样可以使用普通宏支持的 AST 参数检查。
413
414```yuescript
415macro Register = (registry, code`ClassDecl) ->
416 className = code\match "^class%s+(%w+)"
417 return |
418 #{registry}["#{className}"] = #{className}
419
420registry = {}
421
422$[Register(registry)]
423class Worker
424 run: => "ok"
425
426return
427```
428
429<YueDisplay>
430
431```yue
432macro Register = (registry, code`ClassDecl) ->
433 className = code\match "^class%s+(%w+)"
434 return |
435 #{registry}["#{className}"] = #{className}
436
437registry = {}
438
439$[Register(registry)]
440class Worker
441 run: => "ok"
442
443return
444```
445
446</YueDisplay>
447
448&emsp;&emsp;注解也可以用来给函数注入包装代码。
449
450```yuescript
451macro 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]
461add = (a, b) -> a + b
462```
463
464<YueDisplay>
465
466```yue
467macro 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]
477add = (a, b) -> a + b
478```
479
480</YueDisplay>
481
482&emsp;&emsp;注解后面必须紧跟一条语句,而且不能作用在 `return` 语句上。如果被注解的语句正好位于代码块末尾,而你又需要拿到原始的语句 AST 形态,就需要额外补一个显式的 `return`,避免它被隐式返回表达式包起来。