Advanced Computing Environment
Hosted by SourceForge
brix-os project page

Previous: Extensions ----- Up: Contents ----- Next: Types

Hygienic Macros

Extensions allow code to be executed at compile-time which modifies the compiler state or injects code into the program. It is possible to use extensions to create simple code substitution but requires extra boilerplate code. The `macro and `open-macro flags automate this process by wrapping a pattern with scope and injection code. Parameters and any variables exist only within the pattern but the pattern may access variables from the parent scope by prefixing them with the $ operator. A descriptive compile-time exception is thrown if the variables can't be found in the parent scope.

DEFX type (parameters) -> pattern
  • parameters -- parameter list, parentheses required even when empty
  • -> -- required syntax
  • pattern -- single or block expression
type: (one is required)
  • `macro -- function style macro
  • `open-macro -- open parameter style macro
	empty? = defx`open-macro (c:collection_interface) -> (typeOf c).isEmpty(c)

Patterns must be complete expressions and can not depend on substituion to acquire addition parameters.

	foo = defx`macro (a:Number) -> a +; // incomplete pattern
	x := foo(1) 2; // invalid

	message := "a message"
	if not empty? message, echo message; // valid

Previous: Extensions ----- Up: Contents ----- Next: Types