chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] define-macro in chicken 4?


From: Marco Maggi
Subject: Re: [Chicken-users] define-macro in chicken 4?
Date: Wed, 26 May 2010 11:55:48 +0200

"Alejandro Forero Cuervo" wrote:
>>> I have some  extensions that I would like  to migrate to
>>> Chicken 4.  Many of  these depend on define-macro.  Does
>>> define-macro work  in Chicken 4?  What's  my best option
>>> for migrating out of Chicken 3?
>>
>> The  best option  will be  to rewrite  the macros  to use
>> explicit renaming or syntax-rules (it's not that hard).
>
> OK, I will try my  hand at rewriting stream-parser and all
> my other  eggs to work on  Chicken 4.  We'll  see how that
> goes.  I  agree that, at  worst, I'll just have  to switch
> evaluations that,  before, took place at  compile time, to
> now take place at run time, but that's not so bad, not the
> end of the world.

Sorry  to come in  this late  and possibly  to underestimate
your  knowledge  of macro  systems;  I  see  on the  Chicken
site[1]  that a  SYNTAX-CASE  library is  available, and  it
seems to support features similar  to the ones in R6RS which
I know a bit of.

  If  you can use  this library,  IMHO the  easiest starting
point to  migrate from DEFINE-MACRO is to  convert the input
form into an S-expression; for example to migrate from:

(define-macro (the-macro . args)
  ---)

you can start with:

(define-syntax the-macro
  (lambda (use)
    (let ((sexp (unwrap-syntax-object use)))
      ---)))

where:

(define (unwrap-syntax-object use)
  (syntax-case use ()
    (()
     '())
    ((?car . ?cdr)
     (cons (unwrap-syntax-object (syntax ?car))
           (unwrap-syntax-object (syntax ?cdr))))
    (#(?item ...)
     (list->vector (unwrap-syntax-object (syntax (?item ...)))))
    (?atom
     (identifier? (syntax ?atom))
     (syntax ?atom))
    (?atom
     (syntax-object->datum (syntax ?atom)))))

now the macro use:

(the-macro 1 hello #(ciao 2) 3 salut)

is  processed  and  the  result  is bound  to  SEXP  as  the
S-expression:

(#<syntax the-macro> 1
 #<syntax hello> #(#<syntax ciao> 2) 3 #<syntax salut>)

that  is: where  symbols appear  in  the macro  use you  get
"identifiers",  that  is  syntax  objects holding  a  symbol
annotated  with its  originating lexical  context.   You can
process SEXP with the ordinary Scheme functions, then return
as result a  SEXP holding datums and syntax  objects that do
what you want.

  One   thing    you   need   to   care    about   is   that
UNWRAP-SYNTAX-OBJECT  must be  available at  expand  time to
THE-MACRO; I dunno  how to do it in  Chicken using libraries
(I need some help from the  list on this), for a single shot
macro this is an example (not tried on Chicken):

  (define-syntax receive
    (lambda (use)
      (define (unwrap-syntax-object use)
        (syntax-case use ()
          (()
           '())
          ((?car . ?cdr)
           (cons (unwrap-syntax-object (syntax ?car))
                 (unwrap-syntax-object (syntax ?cdr))))
          (#(?item ...)
           (list->vector (unwrap-syntax-object (syntax (?item ...)))))
          (?atom
           (identifier? (syntax ?atom))
           (syntax ?atom))
          (?atom
           (syntax-object->datum (syntax ?atom)))))
      (let ((sexp (unwrap-syntax-object use)))
        (let ((formals          (cadr  sexp))
              (expression       (caddr sexp))
              (body             (cdddr sexp)))
          `(,(syntax call-with-values)
                (,(syntax lambda) () ,expression)
                (,(syntax lambda) ,formals . ,body))))))

  (receive (a b)
      (values 1 2)
    (write 'ciao)
    (list a b))

HTH

[1] <http://chicken.wiki.br/syntax-case>
--
Marco Maggi



reply via email to

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