Defining defmacro using define-syntax

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Defining defmacro using define-syntax

Martin Ward

The FermaT program transformation system is implemented in WSL
and translated to Scheme for compiling or interpreting.

It was originally developed using SCM scheme which uses defmacro
to define macros, eg:

(defmacro floop (name . body)
   `(call-with-current-continuation
      (lambda (,name)
        (do () (#f #t)
           ,@body))))

(defmacro pop (v1 v2)
   `(begin
      (set! ,v1 (car ,v2))
      (set! ,v2 (cdr ,v2))))

(defmacro push (v e)
   `(set! ,v (cons ,e ,v)))

I am trying to port it to other Scheme versions. For bigloo
I can use define-macro to define defmacro as a macro:

(define-macro (defmacro name . forms)
   \`(define-macro (,name . ,(car forms)) ,\@(cdr forms)))

Chicken scheme does not appear to have defmacro or define-macro
but does have define-syntax.

Is there a way to define defmacro using define-syntax?

--
                        Martin

Dr Martin Ward | Email: [hidden email] | http://www.gkc.org.uk
G.K.Chesterton site: http://www.gkc.org.uk/gkc | Erdos number: 4

_______________________________________________
Chicken-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/chicken-users
Reply | Threaded
Open this post in threaded view
|

Re: Defining defmacro using define-syntax

Phil Bewig-2

(define-syntax (define-macro x)

  (syntax-case x ()
    ((_ (name . args) . body)
      (syntax (define-macro name (lambda args . body))))
    ((_ name transformer)
      (syntax
       (define-syntax (name y)
         (syntax-case y ()
           ((_ . args)
             (datum->syntax-object
               (syntax _)
               (apply transformer
                 (syntax-object->datum (syntax args)))))))))))  

On Mon, May 13, 2019 at 1:24 PM Martin Ward <[hidden email]> wrote:

The FermaT program transformation system is implemented in WSL
and translated to Scheme for compiling or interpreting.

It was originally developed using SCM scheme which uses defmacro
to define macros, eg:

(defmacro floop (name . body)
   `(call-with-current-continuation
      (lambda (,name)
        (do () (#f #t)
           ,@body))))

(defmacro pop (v1 v2)
   `(begin
      (set! ,v1 (car ,v2))
      (set! ,v2 (cdr ,v2))))

(defmacro push (v e)
   `(set! ,v (cons ,e ,v)))

I am trying to port it to other Scheme versions. For bigloo
I can use define-macro to define defmacro as a macro:

(define-macro (defmacro name . forms)
   \`(define-macro (,name . ,(car forms)) ,\@(cdr forms)))

Chicken scheme does not appear to have defmacro or define-macro
but does have define-syntax.

Is there a way to define defmacro using define-syntax?

--
                        Martin

Dr Martin Ward | Email: [hidden email] | http://www.gkc.org.uk
G.K.Chesterton site: http://www.gkc.org.uk/gkc | Erdos number: 4

_______________________________________________
Chicken-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/chicken-users

_______________________________________________
Chicken-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/chicken-users
Reply | Threaded
Open this post in threaded view
|

Re: Defining defmacro using define-syntax

Peter Bex
In reply to this post by Martin Ward
On Mon, May 13, 2019 at 07:11:40PM +0100, Martin Ward wrote:
> Chicken scheme does not appear to have defmacro or define-macro
> but does have define-syntax.
>
> Is there a way to define defmacro using define-syntax?

This is of course highly discouraged (because defmacro is
inherently unhygienic), but you can do:

(import (chicken syntax))

(define-syntax defmacro
  (syntax-rules ()
    ((_ ?name ?args ?body ...)
     (define-syntax ?name
       (er-macro-transformer
         (lambda (e r c)
           (apply (lambda ?args ?body ...) (cdr e))))))))

Cheers,
Peter

_______________________________________________
Chicken-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/chicken-users

signature.asc (499 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Defining defmacro using define-syntax

Martin Ward
In reply to this post by Phil Bewig-2
On 13/05/19 19:28, Phil Bewig wrote:

>  From the Standard Prelude <http://programmingpraxis.com/standard-prelude> at my
> blog <http://programmingpraxis.com>:
>
> (define-syntax (define-macro x)
>    (syntax-case x ()
>      ((_ (name . args) . body)
>        (syntax (define-macro name (lambda args . body))))
>      ((_ name transformer)
>        (syntax
>         (define-syntax (name y)
>           (syntax-case y ()
>             ((_ . args)
>               (datum->syntax-object
>                 (syntax _)
>                 (apply transformer
>                   (syntax-object->datum (syntax args)))))))))))
>

This gives the following error:

% csi
CHICKEN
(c) 2008-2018, The CHICKEN Team
(c) 2000-2007, Felix L. Winkelmann
Version 5.0.0 (rev 12f2f2cc)
linux-unix-gnu-x86-64 [ 64bit dload ptables ]

#;1> (define-syntax (define-macro x)
   (syntax-case x ()
     ((_ (name . args) . body)
       (syntax (define-macro name (lambda args . body))))
     ((_ name transformer)
       (syntax
        (define-syntax (name y)
          (syntax-case y ()
            ((_ . args)
              (datum->syntax-object
                (syntax _)
                (apply transformer
                  (syntax-object->datum (syntax args)))))))))))

Error: during expansion of (define-syntax ...) - in `define-syntax' -
symbol expected: (define-syntax (define-macro x) (syntax-case x () ((_
(name . args) . body) (syntax (define-macro name (lambda args . body))))
((_ name transformer) (syntax (define-syntax (name y) (syntax-case y ()
((_ . args) (datum->syntax-object (syntax _) (apply transformer
(syntax-object->datum (syntax args)))))))))))

         Call history:

         <syntax>          (define-syntax (define-macro x) (syntax-case
x () ((_ (name . args) . body) (syntax (define-macro na...     <--
#;1>


--
                        Martin

Dr Martin Ward | Email: [hidden email] | http://www.gkc.org.uk
G.K.Chesterton site: http://www.gkc.org.uk/gkc | Erdos number: 4

_______________________________________________
Chicken-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/chicken-users
Reply | Threaded
Open this post in threaded view
|

Re: Defining defmacro using define-syntax

Martin Ward
In reply to this post by Peter Bex
On 13/05/19 19:35, Peter Bex wrote:

> On Mon, May 13, 2019 at 07:11:40PM +0100, Martin Ward wrote:
>> Chicken scheme does not appear to have defmacro or define-macro
>> but does have define-syntax.
>>
>> Is there a way to define defmacro using define-syntax?
>
> This is of course highly discouraged (because defmacro is
> inherently unhygienic), but you can do:
>
> (import (chicken syntax))
>
> (define-syntax defmacro
>   (syntax-rules ()
>     ((_ ?name ?args ?body ...)
>      (define-syntax ?name
>        (er-macro-transformer
>          (lambda (e r c)
>            (apply (lambda ?args ?body ...) (cdr e))))))))

That works, thanks! :-)

--
                        Martin

Dr Martin Ward | Email: [hidden email] | http://www.gkc.org.uk
G.K.Chesterton site: http://www.gkc.org.uk/gkc | Erdos number: 4

_______________________________________________
Chicken-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/chicken-users
Reply | Threaded
Open this post in threaded view
|

Re: Defining defmacro using define-syntax

Juergen Lorenz
In reply to this post by Martin Ward
Hi all,

yes, there is a simple way: (import procedural-macros) and you'll have
not only a hygienic (if you want so) define-macro, but other procedural
macros and macro creators as well.

Cheers Juergen

>
> The FermaT program transformation system is implemented in WSL
> and translated to Scheme for compiling or interpreting.
>
> It was originally developed using SCM scheme which uses defmacro
> to define macros, eg:
>
> (defmacro floop (name . body)
>   `(call-with-current-continuation
>      (lambda (,name)
>        (do () (#f #t)
>           ,@body))))
>
> (defmacro pop (v1 v2)
>   `(begin
>      (set! ,v1 (car ,v2))
>      (set! ,v2 (cdr ,v2))))
>
> (defmacro push (v e)
>   `(set! ,v (cons ,e ,v)))
>
> I am trying to port it to other Scheme versions. For bigloo
> I can use define-macro to define defmacro as a macro:
>
> (define-macro (defmacro name . forms)
>   \`(define-macro (,name . ,(car forms)) ,\@(cdr forms)))
>
> Chicken scheme does not appear to have defmacro or define-macro
> but does have define-syntax.
>
> Is there a way to define defmacro using define-syntax?
>
> --
> Martin
>
> Dr Martin Ward | Email: [hidden email] | http://www.gkc.org.uk
> G.K.Chesterton site: http://www.gkc.org.uk/gkc | Erdos number: 4
>
> _______________________________________________
> Chicken-users mailing list
> [hidden email]
> https://lists.nongnu.org/mailman/listinfo/chicken-users
--

Dr. Juergen Lorenz
Gruener Weg 27
29471 Gartow


_______________________________________________
Chicken-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/chicken-users

signature.asc (499 bytes) Download Attachment