www.delorie.com/gnu/docs/guile/guile_268.html   search  
 
Buy GNU books!


Guile Reference Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

23.8 Internal Representation of Macros and Syntax

Internally, Guile uses three different flavors of macros. The three flavors are called acro (or syntax), macro and mmacro.

Given the expression

 
(foo ...)

with foo being some flavor of macro, one of the following things will happen when the expression is evaluated.

The key difference between a macro and a mmacro is that the expression returned by a mmacro procedure is remembered (or memoized) so that the expansion does not need to be done again next time the containing code is evaluated.

The primitives procedure->syntax, procedure->macro and procedure->memoizing-macro are used to construct acros, macros and mmacros respectively. However, if you do not have a very special reason to use one of these primitives, you should avoid them: they are very specific to Guile's current implementation and therefore likely to change. Use defmacro, define-macro (see section 23.5 Lisp Style Macro Definitions) or define-syntax (see section 23.6 The R5RS syntax-rules System) instead. (In low level terms, defmacro, define-macro and define-syntax are all implemented as mmacros.)

Scheme Procedure: procedure->syntax code
C Function: scm_makacro (code)
Return a macro which, when a symbol defined to this value appears as the first symbol in an expression, returns the result of applying code to the expression and the environment.

Scheme Procedure: procedure->macro code
C Function: scm_makmacro (code)
Return a macro which, when a symbol defined to this value appears as the first symbol in an expression, evaluates the result of applying code to the expression and the environment. For example:

 
(define trace
  (procedure->macro
    (lambda (x env)
      `(set! ,(cadr x) (tracef ,(cadr x) ',(cadr x))))))

(trace foo)
==
(set! foo (tracef foo 'foo)).

Scheme Procedure: procedure->memoizing-macro code
C Function: scm_makmmacro (code)
Return a macro which, when a symbol defined to this value appears as the first symbol in an expression, evaluates the result of applying code to the expression and the environment. procedure->memoizing-macro is the same as procedure->macro, except that the expression returned by code replaces the original macro expression in the memoized form of the containing code.

In the following primitives, acro flavor macros are referred to as syntax transformers.

Scheme Procedure: macro? obj
C Function: scm_macro_p (obj)
Return #t if obj is a regular macro, a memoizing macro or a syntax transformer.

Scheme Procedure: macro-type m
C Function: scm_macro_type (m)
Return one of the symbols syntax, macro or macro!, depending on whether m is a syntax transformer, a regular macro, or a memoizing macro, respectively. If m is not a macro, #f is returned.

Scheme Procedure: macro-name m
C Function: scm_macro_name (m)
Return the name of the macro m.

Scheme Procedure: macro-transformer m
C Function: scm_macro_transformer (m)
Return the transformer of the macro m.

Scheme Procedure: cons-source xorig x y
C Function: scm_cons_source (xorig, x, y)
Create and return a new pair whose car and cdr are x and y. Any source properties associated with xorig are also associated with the new pair.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003