Factor handbook > The language

Prev:Parsing words

The macros vocabulary implements macros, which are code transformations that may run at compile-time under the right circumstances.

Macros can be used to implement combinators whose stack effects depend on an input parameter. Since macros are expanded at compile time, this permits the compiler to infer a static stack effect for the word calling the macro.

Macros can also be used to calculate lookup tables and generate code at compile time, which can improve performance, raise the level of abstraction, and simplify code.

Factor macros are similar to Lisp macros; they are not like C preprocessor macros.

Defining new macros:

A slightly lower-level facility, compiler transforms, allows an ordinary word definition to co-exist with a version that performs compile-time expansion. The ordinary definition is only used from code compiled with the non-optimizing compiler. Under normal circumstances, macros should be used instead of compiler transforms; compiler transforms are only used for words such as cond which are frequently invoked during the bootstrap process, and this having a performant non-optimized definition which does not generate code on the fly is important.
define-transform ( word quot n -- )

See also
Generalized shuffle words and combinators, Fried quotations