1.3 Backquote Macro

Writing macros using only the Syntax Tree Format can be quite difficult. The backquote macro makes some of it a little bit easier. The Mozart backquote macro was inspired by the classical Lisp backquote macro. However, it is not quite as nice and easy as in Lisp: in Lisp all source code is represented by lists, which makes syntactic manipulation quite uniform and simple. Oz, on the other hand, is a language that makes heavy use of keywords and special notations. Its natural representation is the Syntax Tree Format described in Appendix C of ``The Mozart Compiler'' which is more structured and does not lend itself quite as easily to uniform processing. Nonetheless, if you ever write macros, you will find the backquote facility of great convenience, even if it only gets you part of the way.

The idea of the backquote facility is that it gives you convenient first class access to syntactic representation of code. For example:

declare U = <<'`' if {F X} then X+end>>

Causes U to be bound to the Syntax Tree Format representation of the code if {F X} then X+end.

Furthermore, within the scope of a backquote macro, you can invoke the ``comma'' macro to insert a piece of syntactic representation.

declare V = <<'`' local X = 7 in <<',' U>> end>>

V is now bound to the syntactic representation of

local X = 7 in if {F X} then X+end end

The limits of this technique are simply the limits of the Oz syntax: each macro invocation must represent one ``phrase'' in the language.


Denys Duchier
Version 1.4.0 (20100209)