unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* [PATCH] Replace define-macro with syntax-rules
@ 2013-09-28  5:25 KAction
  2013-09-28 10:27 ` Taylan Ulrich B.
  0 siblings, 1 reply; 4+ messages in thread
From: KAction @ 2013-09-28  5:25 UTC (permalink / raw)
  To: guile-devel; +Cc: wingo, ludo, Dmitry Bogatov

From: Dmitry Bogatov <KAction@gnu.org>

As I can see, definition with define-macro is hygienic and 
move to syntax-rules adds two lines of code, but define-syntax 
is considered preferable. 

WDYT?
Signed-off-by: Dmitry Bogatov <KAction@gnu.org>
---
 module/rnrs/bytevectors.scm | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/module/rnrs/bytevectors.scm b/module/rnrs/bytevectors.scm
index 9744359..856ad4a 100644
--- a/module/rnrs/bytevectors.scm
+++ b/module/rnrs/bytevectors.scm
@@ -75,9 +75,12 @@
 (load-extension (string-append "libguile-" (effective-version))
                 "scm_init_bytevectors")
 
-(define-macro (endianness sym)
-  (if (memq sym '(big little))
-      `(quote ,sym)
-      (error "unsupported endianness" sym)))
+(define-syntax endianness
+    (syntax-rules ()
+        ((_ sym)
+         (let ((qsym (quote sym)))
+             (if (memq qsym '(big little))
+                 qsym
+                 (error "unsupported endianness" qsym))))))
 
 ;;; bytevector.scm ends here
-- 
Recipients list generated via git-blame. Tell me, if you object.




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

* Re: [PATCH] Replace define-macro with syntax-rules
  2013-09-28  5:25 [PATCH] Replace define-macro with syntax-rules KAction
@ 2013-09-28 10:27 ` Taylan Ulrich B.
  2013-09-29 20:38   ` Ian Price
  0 siblings, 1 reply; 4+ messages in thread
From: Taylan Ulrich B. @ 2013-09-28 10:27 UTC (permalink / raw)
  To: KAction; +Cc: wingo, ludo, guile-devel

KAction@gnu.org writes:

> From: Dmitry Bogatov <KAction@gnu.org>
>
> As I can see, definition with define-macro is hygienic and 
> move to syntax-rules adds two lines of code, but define-syntax 
> is considered preferable. 
>
> WDYT?
> Signed-off-by: Dmitry Bogatov <KAction@gnu.org>
> ---
>  module/rnrs/bytevectors.scm | 11 +++++++----
>  1 file changed, 7 insertions(+), 4 deletions(-)
>
> diff --git a/module/rnrs/bytevectors.scm b/module/rnrs/bytevectors.scm
> index 9744359..856ad4a 100644
> --- a/module/rnrs/bytevectors.scm
> +++ b/module/rnrs/bytevectors.scm
> @@ -75,9 +75,12 @@
>  (load-extension (string-append "libguile-" (effective-version))
>                  "scm_init_bytevectors")
>  
> -(define-macro (endianness sym)
> -  (if (memq sym '(big little))
> -      `(quote ,sym)
> -      (error "unsupported endianness" sym)))
> +(define-syntax endianness
> +    (syntax-rules ()
> +        ((_ sym)
> +         (let ((qsym (quote sym)))
> +             (if (memq qsym '(big little))
> +                 qsym
> +                 (error "unsupported endianness" qsym))))))
>  
>  ;;; bytevector.scm ends here

(FYI, your mail ended up in my Gmail spam folder.  Don't know why.)

That definition is not equivalent, it moves the error to run-time.
Something like the following should be better:

(define-syntax endianness
  (syntax-rules ()
    ((_ big) 'big)
    ((_ little) 'little)))

This removes the "unsupported endianness" error and will instead raise a
generic syntax error ("source expression failed to match any pattern in
form ...").  This might just be enough, but if we want it a specific
error message could be achieved with `syntax-case', or a `syntax-error'
macro (used in an "else" clause of `syntax-rules').

Taylan



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

* Re: [PATCH] Replace define-macro with syntax-rules
  2013-09-28 10:27 ` Taylan Ulrich B.
@ 2013-09-29 20:38   ` Ian Price
  2013-09-30 19:24     ` Taylan Ulrich B.
  0 siblings, 1 reply; 4+ messages in thread
From: Ian Price @ 2013-09-29 20:38 UTC (permalink / raw)
  To: Taylan Ulrich B.; +Cc: guile-devel

taylanbayirli@gmail.com (Taylan Ulrich B.) writes:

> (define-syntax endianness
>   (syntax-rules ()
>     ((_ big) 'big)
>     ((_ little) 'little)))
Except now whenever you use it, you only ever get big endianness. You
forgot to add big and little to the literals list.

Also, IMO, if you are using literals they should be to bound identifiers.

> This removes the "unsupported endianness" error and will instead raise a
> generic syntax error ("source expression failed to match any pattern in
> form ...").  This might just be enough, but if we want it a specific
> error message could be achieved with `syntax-case', or a `syntax-error'
> macro (used in an "else" clause of `syntax-rules').

Pattern match errors are completely useless, really.

-- 
Ian Price -- shift-reset.com

"Programming is like pinball. The reward for doing it well is
the opportunity to do it again" - from "The Wizardy Compiled"



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

* Re: [PATCH] Replace define-macro with syntax-rules
  2013-09-29 20:38   ` Ian Price
@ 2013-09-30 19:24     ` Taylan Ulrich B.
  0 siblings, 0 replies; 4+ messages in thread
From: Taylan Ulrich B. @ 2013-09-30 19:24 UTC (permalink / raw)
  To: guile-devel

Ian Price <ianprice90@googlemail.com> writes:

> taylanbayirli@gmail.com (Taylan Ulrich B.) writes:
>
>> (define-syntax endianness
>>   (syntax-rules ()
>>     ((_ big) 'big)
>>     ((_ little) 'little)))
> Except now whenever you use it, you only ever get big endianness. You
> forgot to add big and little to the literals list.

Doh!  Can't believe I made such a mistake, must write more Scheme. :)

Well, actually, it will still coincidentally work because the `big' in
`'big' (`(quote big)') will refer to the pattern variable.  (Essentially
we made an alias to `quote'.)



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

end of thread, other threads:[~2013-09-30 19:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-09-28  5:25 [PATCH] Replace define-macro with syntax-rules KAction
2013-09-28 10:27 ` Taylan Ulrich B.
2013-09-29 20:38   ` Ian Price
2013-09-30 19:24     ` Taylan Ulrich B.

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