guile-user
[Top][All Lists]
Advanced

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

Re: syntax-case equivalent to syntax-rules


From: Damien Mattei
Subject: Re: syntax-case equivalent to syntax-rules
Date: Sun, 29 Aug 2021 19:17:02 +0200

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 via email to

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