unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* splicing macros
@ 2013-01-26 13:03 Stefan Israelsson Tampe
  2013-01-27 10:17 ` Andy Wingo
  0 siblings, 1 reply; 6+ messages in thread
From: Stefan Israelsson Tampe @ 2013-01-26 13:03 UTC (permalink / raw)
  To: guile-devel


Hi all,

This is something fun to implement using ck-macros and
local-syntax-value.

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))

and

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

and

 (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

  http://okmij.org/ftp/Scheme/macros.html

It is now quite natural to use his c-append macro to do do the
transformation:
  (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? 

/Stefan



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-03-20 20:32 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-26 13:03 splicing macros Stefan Israelsson Tampe
2013-01-27 10:17 ` Andy Wingo
2013-01-28 11:27   ` Stefan Israelsson Tampe
2013-02-08 20:53   ` Stefan Israelsson Tampe
2013-04-29 20:46     ` Fwd: " Stefan Israelsson Tampe
2014-03-20 20:32     ` Andy Wingo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).