[Next] [Previous] [Up] [Top] [Contents] [Index]

10 Macros

Extensible Grammar

There are three kinds of macros: definition macros, which extend the available set of definitions; statement macros, which extend the available set of statements; and function macros, which syntactically resemble function calls but are more flexible. Named value references and local declarations cannot be macro calls. Only statements, function calls, and definitions are extensible.

Definition Macros

A definition macro extends the definition-macro-call production of the Dylan phrase grammar to recognize additional constructs as valid definitions, by creating a new define-body-word that is recognized by the following grammar line:

definition-macro-call:
define modifiersopt define-body-word body-fragmentopt definition-tail

or by creating a new define-list-word that is recognized by the following grammar line:

definition-macro-call:
define modifiersopt define-list-word list-fragmentopt

This allows programmers to extend Dylan by defining new kinds of definitions. The syntax of the definition must be parseable by one of these two predefined grammar rules. The first handles body-style definitions like define class, define method, and define module, while the second handles list-style definitions like define constant. See Appendix A, "BNF," for the details.

The new define-body-word or define-list-word becomes a partially reserved word in each module where the macro definition is visible. In particular a define-body-word or define-list-word cannot be used as a modifier in a definition. It can still be used as a variable-name.

Statement Macros

A statement macro extends the statement production of the Dylan phrase grammar to recognize additional constructs as valid statements, by creating a new begin-word that is recognized by the following grammar line:

statement:
begin-word body-fragmentopt end-clause

The new begin-word becomes a reserved word in each module where the macro definition is visible. It can only be used at the beginning and end of this new statement.

Function Macros

A function macro extends the function-macro-call production of the Dylan phrase grammar to recognize additional constructs, by creating a new function-word that is recognized by the following two grammar lines:

function-macro-call:
function-word ( body-fragmentopt )
function-word ( body-fragmentopt ) := expression

The new function-word becomes a reserved word in each module where the macro definition is visible. It can only be used at the beginning of a macro call.

A function-macro-call containing an assignment operator,

function-word  (  body-fragmentopt  ) := expression
becomes

begin let temp = expression ;
  function-word-setter ( temp,  body-fragmentopt  );
  temp
end
where function-word-setter is function-word with a "-setter" suffix and temp is a unique name. If the body-fragment is missing then the comma preceding it is omitted. Assignment does not expand a macro call on the left-hand side.

To simplify its presentation, the grammar in Appendix A, "BNF," is ambiguous. A function-macro-call containing an assignment operator could also be parsed as an expression consisting of the first form of function-macro-call, followed by := and further binary-operands and binary-operators. This alternative parse is disallowed.


Dylan Reference Manual - 17 OCT 1995
[Next] [Previous] [Up] [Top] [Contents] [Index]

Generated with Harlequin WebMaker