Macro Declaration and Parameters
Macro functions are declared as public macro Name(input: Tokens): Tokens. Macros must be declared as public because they are always used by other packages. Both parameter type and return type are Tokens, representing Token sequences.
Macros can be divided into non-attribute macros and attribute macros. Non-attribute macros accept only one parameter, the decorated code fragment, and are invoked using @MacroName(...). When applied to declarations, parentheses can be omitted. Attribute macros accept two parameters: the first is the attribute in [], and the second is the decorated code, invoked using @MacroName[attr](...).
When invoking a non-attribute macro, the content within parentheses (or the declaration itself) becomes the input parameter. When invoking an attribute macro, the content within [] becomes the first parameter attr, and the decorated code following it becomes the second parameter input. The Tokens returned by the macro replace the entire macro call in the source code.
In the example on the right, AddPrefix is an attribute macro that receives both an attribute and input, concatenates them, and returns the result. When calling @AddPrefix[public ]struct Data, attr is "public ", input is struct Data {...}, and the return value is public struct Data {...}.
@MacroName[](...).Observe the expansion results of both macros to understand how parameters are passed for non-attribute and attribute macros.
define.cj and the commented usage example as main.cj. Run cjc define.cj --compile-macro, then cjc main.cj --import-path . -o main && ./main. The online single-file runner does not support this multi-file build step.