For Development HEAD DRAFTSearch (procedure/syntax/module):

Next: , Previous: , Up: Macros   [Contents][Index]

5.4 Hybrid macros

A hybrid macro is both a macro and a procedure simultaneously. If a symbol bound to a hybrid macro appears in the first position of a form, it behaves like a macro and the form is expanded accodring to its macro expander. If a symbol appears other places, it is evaluated to a procedure at runtime.

It can realize so-called “compiler macros”—at a compile time, the macro part examines the arguments and can transform the form as desired. In all other circumstances, it behaves like a normal procedure binding, so you can pass the procedure to map, for example.

Macro: define-hybrid-syntax variable expr transformer-spec

Binds variable to both an ordinary Scheme value and a macro simultaneously. At the compile time, transformer-spec is evaluated; it must yield a macro in the the compile-time environment, and bound to variable to be used at macro expansion. At the execution time, expr is evaluated and bound to variable to be used as a run-time value.

The macro transformer can return the input form as is (that is, returns an object eq? to the input form), to indicate that it doesn’t need to expand it. In that case, Gauche compiles the form as an ordinary procedure call, to use the value of expr at run-time.

Note: If what you want to do with the hybrid macro is just to inline-expand the procedure body, use define-inline (see Definitions).

Note about the syntax: Traditionally in Lisp, compiler macros are defined by a separate form from the procedure binding.

However, having bindings to the same identifier twice makes the program semantics ambiguous. What if the two forms are separated into different modules? What if the identifier is redefined? It would be clearer that single form determines the binding.

Next: , Previous: , Up: Macros   [Contents][Index]

For Development HEAD DRAFTSearch (procedure/syntax/module):