unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* syntax-case equivalent to syntax-rules
@ 2021-08-29 11:29 Damien Mattei
  2021-08-29 17:17 ` Damien Mattei
  0 siblings, 1 reply; 2+ messages in thread
From: Damien Mattei @ 2021-08-29 11:29 UTC (permalink / raw)
  To: guile-user; +Cc: julien.provillard, Jean-Paul Roy

hello,

i write a macro that needs both the features of syntax-rules and
syntax-case based on documentation:
https://www.gnu.org/software/guile/manual/html_node/Syntax-Case.html

i have :

;; scheme@(guile-user)> (def (foo) (when #t (return "hello") "bye"))
;; scheme@(guile-user)> (foo)
;;  "hello"

;; (def x)

;; TODO: study def of a recursive function
(define-syntax def

    (lambda (stx)
      (syntax-case stx (declare)

;; definition without a value assigned
;; (def x)
((_ var) #`(define var '()))

;; (def x 7)
((_ var expr) #`(define var expr))

;; added body body*
;;((_ (declare var1) <body> <body>* ...) #`(letrec ((var1 '())) <body>
<body>* ...))

;; 'compiles' but do not have the expected behavior:
((_ (declare var1 ...) <body> ...) #`(letrec ((var1 '()) ...)
      <body> ...))

 ;;  (def (foo) (when #t (return "hello") "bye"))
        ((_ (<name> <arg> ...) <body> <body>* ...)
         (let ((ret-id (datum->syntax stx 'return)))
           #`(define (<name> <arg> ...)
               (call/cc (lambda (#,ret-id) <body> <body>* ...))))))))

and def2 that have the good behavior:

scheme@(guile-user)> (def2 (declare x y z) (list x y z))
$1 = (() () ())

(define-syntax def2

  (syntax-rules (declare)

    ((_ (declare var1 ...) <body> ...) (letrec ((var1 '()) ...)
<body> ...))))

i want to merge def and def2 in a single macro

any solution?

Damien


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

* Re: syntax-case equivalent to syntax-rules
  2021-08-29 11:29 syntax-case equivalent to syntax-rules Damien Mattei
@ 2021-08-29 17:17 ` Damien Mattei
  0 siblings, 0 replies; 2+ messages in thread
From: Damien Mattei @ 2021-08-29 17:17 UTC (permalink / raw)
  To: guile-user; +Cc: julien.provillard, Jean-Paul Roy

i find myself that the order of cases in syntax-case and macros is
important,
complex case must be put before simplest ones in order to avoid them to be
parsed before,
unless that they are shadowed by simplest ones...

the solution was just to reorder them from complex -> simple ones:

(define-syntax def

    (lambda (stx)
      (syntax-case stx (declare)

;; (def (declare x y z) 1 2 (list x y z))
;; (() () ())

((_ (declare var1 ...) <body> ...) #`(letrec ((var1 '()) ...)
      <body> ...))

;;  (def (foo) (when #t (return "hello") "bye"))
        ((_ (<name> <arg> ...) <body>  ...)
         (let ((ret-id (datum->syntax stx 'return)))
           #`(define (<name> <arg> ...)
               (call/cc (lambda (#,ret-id) <body>  ...)))))

;; definition without a value assigned
;; (def x)
((_ var) #`(define var '()))

;; (def x 7)
((_ var expr) #`(define var expr))


)))



On Sun, Aug 29, 2021 at 1:29 PM Damien Mattei <damien.mattei@gmail.com>
wrote:

> hello,
>
> i write a macro that needs both the features of syntax-rules and
> syntax-case based on documentation:
> https://www.gnu.org/software/guile/manual/html_node/Syntax-Case.html
>
> i have :
>
> ;; scheme@(guile-user)> (def (foo) (when #t (return "hello") "bye"))
> ;; scheme@(guile-user)> (foo)
> ;;  "hello"
>
> ;; (def x)
>
> ;; TODO: study def of a recursive function
> (define-syntax def
>
>     (lambda (stx)
>       (syntax-case stx (declare)
>
> ;; definition without a value assigned
> ;; (def x)
> ((_ var) #`(define var '()))
>
> ;; (def x 7)
> ((_ var expr) #`(define var expr))
>
> ;; added body body*
> ;;((_ (declare var1) <body> <body>* ...) #`(letrec ((var1 '())) <body>
> <body>* ...))
>
> ;; 'compiles' but do not have the expected behavior:
> ((_ (declare var1 ...) <body> ...) #`(letrec ((var1 '()) ...)
>       <body> ...))
>
>  ;;  (def (foo) (when #t (return "hello") "bye"))
>         ((_ (<name> <arg> ...) <body> <body>* ...)
>          (let ((ret-id (datum->syntax stx 'return)))
>            #`(define (<name> <arg> ...)
>                (call/cc (lambda (#,ret-id) <body> <body>* ...))))))))
>
> and def2 that have the good behavior:
>
> scheme@(guile-user)> (def2 (declare x y z) (list x y z))
> $1 = (() () ())
>
> (define-syntax def2
>
>   (syntax-rules (declare)
>
>     ((_ (declare var1 ...) <body> ...) (letrec ((var1 '()) ...)
> <body> ...))))
>
> i want to merge def and def2 in a single macro
>
> any solution?
>
> Damien
>


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

end of thread, other threads:[~2021-08-29 17:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-29 11:29 syntax-case equivalent to syntax-rules Damien Mattei
2021-08-29 17:17 ` Damien Mattei

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 NNTP newsgroup(s).