unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Damien Mattei <damien.mattei@gmail.com>
To: guile-user <guile-user@gnu.org>
Cc: julien.provillard@unice.fr, Jean-Paul Roy <jean-paul.roy@unice.fr>
Subject: Re: syntax-case equivalent to syntax-rules
Date: Sun, 29 Aug 2021 19:17:02 +0200	[thread overview]
Message-ID: <CADEOaderPY1cL0scLHqa7ep8-ZN7vjcx8K8NFR6CkZOpOqOS4g@mail.gmail.com> (raw)
In-Reply-To: <CADEOadczv23PAKMk4fMzpGTayZC5u+dcNm2+wrn7AGHX5UKtxA@mail.gmail.com>

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
>


      reply	other threads:[~2021-08-29 17:17 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-29 11:29 Damien Mattei
2021-08-29 17:17 ` Damien Mattei [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CADEOaderPY1cL0scLHqa7ep8-ZN7vjcx8K8NFR6CkZOpOqOS4g@mail.gmail.com \
    --to=damien.mattei@gmail.com \
    --cc=guile-user@gnu.org \
    --cc=jean-paul.roy@unice.fr \
    --cc=julien.provillard@unice.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).