[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

splicing macros

From: Stefan Israelsson Tampe
Subject: splicing macros
Date: Sat, 26 Jan 2013 14:03:20 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)

Hi all,

This is something fun to implement using ck-macros and

The idea is to patch guile's quasisyntax.scm in ice-9 to implement
splicing of macros. 

If we assume a reader macro #.code  ==> (macro-splicing code), with the
proposed hack I can now write:

  (define-syntax-rule (2x x) (x x))


  (define-syntax 4x
     (lambda (x)
        (syntax-case x ()
          ((_ x) #`(#.(2x x) #.(2x x))))))


 (define-syntax g (lambda (x) #`(#.(4x 1) #.(4x 2))))

$4 = (1 1 1 1 2 2 2 2)

I will assume that you are familliar with th ck macro, included in
recent guile releases or else consider looking it up at

It is now quite natural to use his c-append macro to do do the
  (a b #.(f x) c d)
  (ck () (c-append '(a b) (c-append (ck-it '(f x)) '(c d))))

And assuming that ck-it can dispatch the macro (f x) the appending would 
be obvious. The second magic is ck-it, it is defined as

(use-modules (system syntax))

(define-syntax ck-it 
  (lambda (x)
    (syntax-case x (quote)
      ((_ s (quote f))
       (and (identifier? #'f) (eq? (syntax-local-binding #'f) 'macro))
       (call-with-values (lambda () (syntax-local-binding #'f))
         (lambda (m transformer)
           (list #'ck-it #'s (list #'quote (transformer #'f))))))

      ((_ s (quote (quote x))) 
       (list #'ck #'s #'(quote x)))

      ((_ s (quote (f . x)))
       (and (identifier? #'f) (eq? (syntax-local-binding #'f) 'macro))
       (call-with-values (lambda () (syntax-local-binding #'f))
         (lambda (m transformer)
           (list #'ck-it #'s (list #'quote (transformer #'(f . x)))))))
      ((_ s f)
       (list #'ck #'s #'f)))))

It will iterativelly apply macros until a non macro sexp is appearing,
To inhibit macro expeansion one need to quote it. This is a bit unclean
because if the macro returns (quote x), x any sexp, it will return x in 
stead. A better solution might be to introduce a special inhibit
macro. Also notice how splicing in already spliced syntaxes works as the
example above works. Another thing to note is how we use
syntax-local-binding to search find the macro transformer and use that
in order to make all this work.

Any thoughts? Should we add ck-it to guile's ck.scm? Should we add
splicing macros? 


reply via email to

[Prev in Thread] Current Thread [Next in Thread]