From 94edfbc8c7d62d700dfb59334a0ed3beedd49493 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Mon, 5 Aug 2024 17:20:00 +0800 Subject: add macros generating macros feature. --- doc/docs/doc/README.md | 42 ++++++++++++++++++++++++++++++++++++++++++ doc/docs/zh/doc/README.md | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) (limited to 'doc') 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 +### Generating Macros with Macros + +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. + +```moonscript +macro Enum = (...) -> + items = {...} + itemSet = {item, true for item in *items} + (item) -> + error "got \"#{item}\", expecting one of #{table.concat items, ', '}" unless itemSet[item] + "\"#{item}\"" + +macro BodyType = $Enum( + Static + Dynamic + Kinematic +) + +print "Valid enum type:", $BodyType Static +-- print "Compilation error with enum type:", $BodyType Unknown +``` + + +
+macro Enum = (...) ->
+	items = {...}
+	itemSet = {item, true for item in *items}
+	(item) ->
+		error "got \"#{item}\", expecting one of #{table.concat items, ', '}" unless itemSet[item]
+		"\"#{item}\""
+
+macro BodyType = $Enum(
+	Static
+	Dynamic
+	Kinematic
+)
+
+print "Valid enum type:", $BodyType Static
+-- print "Compilation error with enum type:", $BodyType Unknown
+
+
+ ## Operator 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 +### 用宏生成宏 + +在月之脚本中,宏函数允许你在编译时生成代码。通过嵌套的宏函数,你可以创建更复杂的生成模式。这个特性允许你定义一个宏函数,用它来生成另一个宏函数,从而实现更加动态的代码生成。 + +```moonscript +macro Enum = (...) -> + items = {...} + itemSet = {item, true for item in *items} + (item) -> + error "got \"#{item}\", expecting one of #{table.concat items, ', '}" unless itemSet[item] + "\"#{item}\"" + +macro BodyType = $Enum( + Static + Dynamic + Kinematic +) + +print "有效的枚举类型:", $BodyType Static +-- print "编译报错的枚举类型:", $BodyType Unknown +``` + +
+macro Enum = (...) ->
+	items = {...}
+	itemSet = {item, true for item in *items}
+	(item) ->
+		error "got \"#{item}\", expecting one of #{table.concat items, ', '}" unless itemSet[item]
+		"\"#{item}\""
+
+macro BodyType = $Enum(
+	Static
+	Dynamic
+	Kinematic
+)
+
+print "有效的枚举类型:", $BodyType Static
+-- print "编译报错的枚举类型:", $BodyType Unknown
+
+
+ ## 操作符 Lua的所有二元和一元操作符在月之脚本中都是可用的。此外,**!=** 符号是 **~=** 的别名,而 **\\** 或 **::** 均可用于编写链式函数调用,如写作 `tb\func!` 或 `tb::func!`。此外月之脚本还提供了一些其他特殊的操作符,以编写更具表达力的代码。 -- cgit v1.2.3-55-g6feb