guile-user
[Top][All Lists]
Advanced

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

syntax-case equivalent to syntax-rules


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

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]