diff options
author | Li Jin <dragon-fly@qq.com> | 2024-08-05 17:20:00 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2024-08-05 17:20:00 +0800 |
commit | 94edfbc8c7d62d700dfb59334a0ed3beedd49493 (patch) | |
tree | f695cec8b8873d5a6e763ac94bf3c81fb6ba0be9 /doc | |
parent | f8cd1220147d606b7e96f88c12fd0f163fb4e1c5 (diff) | |
download | yuescript-94edfbc8c7d62d700dfb59334a0ed3beedd49493.tar.gz yuescript-94edfbc8c7d62d700dfb59334a0ed3beedd49493.tar.bz2 yuescript-94edfbc8c7d62d700dfb59334a0ed3beedd49493.zip |
add macros generating macros feature.v0.23.9
Diffstat (limited to 'doc')
-rwxr-xr-x | doc/docs/doc/README.md | 42 | ||||
-rwxr-xr-x | doc/docs/zh/doc/README.md | 41 |
2 files changed, 83 insertions, 0 deletions
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md index 811497a..378e0f4 100755 --- a/doc/docs/doc/README.md +++ b/doc/docs/doc/README.md | |||
@@ -374,6 +374,48 @@ print $LINE -- get number 2 | |||
374 | </pre> | 374 | </pre> |
375 | </YueDisplay> | 375 | </YueDisplay> |
376 | 376 | ||
377 | ### Generating Macros with Macros | ||
378 | |||
379 | 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. | ||
380 | |||
381 | ```moonscript | ||
382 | macro Enum = (...) -> | ||
383 | items = {...} | ||
384 | itemSet = {item, true for item in *items} | ||
385 | (item) -> | ||
386 | error "got \"#{item}\", expecting one of #{table.concat items, ', '}" unless itemSet[item] | ||
387 | "\"#{item}\"" | ||
388 | |||
389 | macro BodyType = $Enum( | ||
390 | Static | ||
391 | Dynamic | ||
392 | Kinematic | ||
393 | ) | ||
394 | |||
395 | print "Valid enum type:", $BodyType Static | ||
396 | -- print "Compilation error with enum type:", $BodyType Unknown | ||
397 | ``` | ||
398 | |||
399 | <YueDisplay> | ||
400 | <pre> | ||
401 | macro Enum = (...) -> | ||
402 | items = {...} | ||
403 | itemSet = {item, true for item in *items} | ||
404 | (item) -> | ||
405 | error "got \"#{item}\", expecting one of #{table.concat items, ', '}" unless itemSet[item] | ||
406 | "\"#{item}\"" | ||
407 | |||
408 | macro BodyType = $Enum( | ||
409 | Static | ||
410 | Dynamic | ||
411 | Kinematic | ||
412 | ) | ||
413 | |||
414 | print "Valid enum type:", $BodyType Static | ||
415 | -- print "Compilation error with enum type:", $BodyType Unknown | ||
416 | </pre> | ||
417 | </YueDisplay> | ||
418 | |||
377 | ## Operator | 419 | ## Operator |
378 | 420 | ||
379 | 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. | 421 | 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. |
diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md index 0257dee..90d1820 100755 --- a/doc/docs/zh/doc/README.md +++ b/doc/docs/zh/doc/README.md | |||
@@ -371,6 +371,47 @@ print $LINE -- 获取当前代码行数:2 | |||
371 | </pre> | 371 | </pre> |
372 | </YueDisplay> | 372 | </YueDisplay> |
373 | 373 | ||
374 | ### 用宏生成宏 | ||
375 | |||
376 | 在月之脚本中,宏函数允许你在编译时生成代码。通过嵌套的宏函数,你可以创建更复杂的生成模式。这个特性允许你定义一个宏函数,用它来生成另一个宏函数,从而实现更加动态的代码生成。 | ||
377 | |||
378 | ```moonscript | ||
379 | macro Enum = (...) -> | ||
380 | items = {...} | ||
381 | itemSet = {item, true for item in *items} | ||
382 | (item) -> | ||
383 | error "got \"#{item}\", expecting one of #{table.concat items, ', '}" unless itemSet[item] | ||
384 | "\"#{item}\"" | ||
385 | |||
386 | macro BodyType = $Enum( | ||
387 | Static | ||
388 | Dynamic | ||
389 | Kinematic | ||
390 | ) | ||
391 | |||
392 | print "有效的枚举类型:", $BodyType Static | ||
393 | -- print "编译报错的枚举类型:", $BodyType Unknown | ||
394 | ``` | ||
395 | <YueDisplay> | ||
396 | <pre> | ||
397 | macro Enum = (...) -> | ||
398 | items = {...} | ||
399 | itemSet = {item, true for item in *items} | ||
400 | (item) -> | ||
401 | error "got \"#{item}\", expecting one of #{table.concat items, ', '}" unless itemSet[item] | ||
402 | "\"#{item}\"" | ||
403 | |||
404 | macro BodyType = $Enum( | ||
405 | Static | ||
406 | Dynamic | ||
407 | Kinematic | ||
408 | ) | ||
409 | |||
410 | print "有效的枚举类型:", $BodyType Static | ||
411 | -- print "编译报错的枚举类型:", $BodyType Unknown | ||
412 | </pre> | ||
413 | </YueDisplay> | ||
414 | |||
374 | ## 操作符 | 415 | ## 操作符 |
375 | 416 | ||
376 | Lua的所有二元和一元操作符在月之脚本中都是可用的。此外,**!=** 符号是 **~=** 的别名,而 **\\** 或 **::** 均可用于编写链式函数调用,如写作 `tb\func!` 或 `tb::func!`。此外月之脚本还提供了一些其他特殊的操作符,以编写更具表达力的代码。 | 417 | Lua的所有二元和一元操作符在月之脚本中都是可用的。此外,**!=** 符号是 **~=** 的别名,而 **\\** 或 **::** 均可用于编写链式函数调用,如写作 `tb\func!` 或 `tb::func!`。此外月之脚本还提供了一些其他特殊的操作符,以编写更具表达力的代码。 |