For Gauche 0.9.5


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

5.4 Macro expansion

Function: macroexpand form
Function: macroexpand-1 form

If form is a list and its first element is a variable globally bound to a macro, macroexpand-1 invokes its macro transformer and returns the expanded form. Otherwise, returns form as is.

macroexpand repeats macroexpand-1 until the form can’t be expanded.

These procedures can be used to expand globally defined macros.

Function: macroexpand-all form

Fully expand macros inside form. The result only contains function calls and Gauche’s built-in syntax. The form is assumed to be a toplevel form within the current module. (See Module semantics, for the concept of the current module. It can be tricky, for sometimes the current module differ between compile-time and run-time.)

Any local variables introduced in form is renamed to avoid collision. Since each local variable has unique name, all let forms become letrec forms (we can safely replace let with letrec if no bindings introduced by let shadows outer bindings.)

NB: If a macro in form inserts a reference to a global variable which belongs to other module, the information is lost in the current implementation. There are a few ways to address this issue; we may leave such reference as an identifier object, convert it to with-module form, or introduce a special syntax to represent such case. It’s undecided currently, so do not rely too much on the current behavior. For the time being, it’s best to use this feature only for interactive macro testing.

(macroexpand-all
 '(letrec-syntax
      [(when-not (syntax-rules ()
                   [(_ test . body) (if test #f (begin . body))]))]
    (let ([if list])
      (define x (expt foo))
      (let1 x 3
        (when-not (bar) (if x))))))
 ⇒ (letrec ((if.0 list)) 
     (letrec ((x.1 (expt foo)))
       (letrec ((x.2 '3)) 
        (if (bar) '#f (if.0 x.2)))))
Special Form: %macroexpand form
Special Form: %macroexpand-1 form

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