Skip to content
Tom Barham edited this page Sep 14, 2015 · 2 revisions

Macros are the glue in MCA - they allow you to easily repeat common code, in a similar way to traditional functions and variables.

There are two types of macros: simple find-replace macros, or macros with arguments. Both of these are defined in the same way:

MACRO_NAME macro value;

During the first pass, the compiler finds all references to macros and replaces them with the macro definition (note: for block macros, the macro is not 'expanded' to prevent recursion errors, but is instead hoisted and memorized).

Because of this relaxed method to macro replacing, it is possible to do many interesting things with macros. For example, the following is valid syntax, although will probably break most syntax highlighters:

STRING_BOUND ";

myvar = STRING_BOUND hello! STRING_BOUND;
/say {STRING_BOUND}{myvar}{STRING_BOUND};

// becomes:
/say " hello! ";

(For more information on the recommended coding style, see Style Guide).

To define a macro with arguments, we can specify an argument list in brackets after the macro name:

ADD_ONE(n) n + 1;

two = ADD_ONE(1);

// becomes:
two = 1 + 1;

Any macros with arguments are automatically converted to blocks if required. For more information on block scope and returning, see Blocks.

Clone this wiki locally