From: Damien Mattei <damien.mattei@gmail.com>
To: guile-user <guile-user@gnu.org>
Subject: Fwd: reserved-keyword in macro
Date: Wed, 2 Feb 2022 14:02:52 +0100 [thread overview]
Message-ID: <CADEOadddn2qCu5XWsq2uAvNDfo0vAMT+eC_E7muMDLa8aOQUqg@mail.gmail.com> (raw)
In-Reply-To: <CADEOaddzjFbSYvVGEOkrSrjHSZvUkqC9beiRw29jbH3XHeRkZw@mail.gmail.com>
sorry i miss copy to mailing list...
---------- Forwarded message ---------
From: Damien Mattei <damien.mattei@gmail.com>
Date: Wed, Feb 2, 2022 at 12:09 PM
Subject: Re: reserved-keyword in macro
To: Maxime Devos <maximedevos@telenet.be>
thanks maxim ,it works so my problem come from elsewhere
scheme@(guile-user)> (define-syntax macro
(syntax-rules (reserved-keyword)
((_ reserved-keyword arg) 'first)
... ((_ arg1 arg2) 'second)))
scheme@(guile-user)> (macro 4 5)
second
scheme@(guile-user)> (macro reserved-keyword 5)
first
this was a simplified example my real case is this:
(define-syntax <-
(syntax-rules ($bracket-apply$)
;; special form like : (<- ($bracket-apply$ T 3) ($bracket-apply$ T 4))
;; one dimension array, example: {a[4] <- 7}
;; $bracket-apply$ of SRFI 105
((_ ($bracket-apply$ container index) expr)
(let ((value expr)) ;; to avoid compute it twice
;; (if (equal? (quote $bracket-apply$) (quote funct-or-macro)) ;;
test funct-or-macro equal $bracket-apply$
;; normal case
;; {T[2] <- 4}
;; {T[3] <- T[2]}
;;(begin
;;(display "<- : vector or array set! or hash-table set!") (newline)
(cond ((vector? container) (vector-set! container index value))
((hash-table? container) (hash-table-set! container index value))
(else (array-set! container index value)));)
;; rare case (to prevent any error)
;; (let ((var (funct-or-macro container index))) ;; MUST be in a
variable , otherwise:
;; While compiling expression:
;; Syntax error:
;; unknown location: quote: bad syntax in form quote
;; <- : variable set! after creation
;; (set! var value)))
value))
;; multi dimensions array : {a[2 4] <- 7}
;; $bracket-apply$ of SRFI 105
((_ ($bracket-apply$ array index1 index2 ...) expr)
(let ((value expr)) ;; to avoid compute it twice
;; (if (equal? (quote $bracket-apply$) (quote funct-or-macro)) ;;
test funct-or-macro equal $bracket-apply$
;; normal case
;;(begin
;;(display "<- : multidimensional vector or array set!") (newline)
(if (vector? array)
(array-n-dim-set! array value index1 index2 ...)
(array-set! array index1 index2 ... value));)
;; rare case (to prevent any error)
;; (let ((var (funct-or-macro array index ...))) ;; MUST be in a variable
;; (display "<- : variable set! after creation (multidimensional)")
(newline)
;; (set! var value)))
value))
;; not sure this case will be usefull
;; (define (foo) (values 1 2 3))
;; (call-with-values foo list)
;;'(1 2 3)
;; (define (foo) (display "inside foo") (newline) (values 1 2 3))
;; > (declare x y z)
;; > (<- (x y z) (foo))
;; ((_ (funct-or-macro arg ...) expr)
;; (let ((var (funct-or-macro arg ...))
;; (value expr)) ;; to avoid compute it twice
;; (set! var value)
;; var))
((_ (var ...) expr)
(begin
(display expr) (newline)
(let ((expr-list (call-with-values (lambda () expr) list)))
(assign-var (var ...) expr-list)
expr-list)))
;;(<- x 5)
((_ var expr)
(begin
;;(display "<- : variable set!") (newline)
(set! var expr)
var))
;; (declare x y z t)
;; {x <- y <- z <- t <- 7}
;; 7
;; (list x y z t)
;; (7 7 7 7)
;; (declare I)
;; {I <- (make-array 0 4 4)}
;; #2((0 0 0 0)
;; (0 0 0 0)
;; (0 0 0 0)
;; (0 0 0 0))
;;
;; {I[0 0] <- I[1 1] <- I[2 2] <- I[3 3] <- 1}
;; 1
;;
;; I
;; #2((1 0 0 0)
;; (0 1 0 0)
;; (0 0 1 0)
;; (0 0 0 1))
((_ var var1 var2 ...) ;; there is an expression in the last part of
ellipsis!
(<- var (<- var1 var2 ...)))
))
;; > (declare x y z)
;; > (assign-var (x y z) (1 2 3))
;; > x
;; 1
;; > y
;; 2
;; > z
;; 3
(define-syntax assign-var
(syntax-rules ()
((_ (var ...) (exp ...)) (begin (set! var exp) ...))))
i'm cross developping in Guile and Racket and i'm sure Guile will do as
Racket and here is the result:
Bienvenue dans DrRacket, version 7.7 [3m].
Langage: reader "../SRFI/SRFI-105.rkt", avec débogage; limite mémoire : 128
MB.
> (declare x y z)
> (define (foo) (display "inside foo") (newline) (values 1 2 3))
> (<- (x y z) (foo))
inside foo
. .
../../../../../../../../usr/share/racket/collects/racket/private/kw.rkt:1201:25:
result arity mismatch;
expected number of values not received
expected: 1
received: 3
values...:
> (define T (make-vector 5))
> {T[2] <- 1}
1
. ../required-files/assignment.rkt:140:7: assign-var: bad syntax in:
(assign-var ($bracket-apply$ T 2) expr-list)
> (<- T[2] 1)
. ../required-files/assignment.rkt:140:7: assign-var: bad syntax in:
(assign-var (2) expr-list)
> (<- ($bracket-apply$ T 2) 1)
1
. ../required-files/assignment.rkt:140:7: assign-var: bad syntax in:
(assign-var ($bracket-apply$ T 2) expr-list)
>
the problem is that anyway i do it when using reserved keyword in
define-syntax the macro use the third case:
(_ (var ...) expr)
even when i have :
(syntax-rules ($bracket-apply$)
;; special form like : (<- ($bracket-apply$ T 3) ($bracket-apply$ T 4))
;; one dimension array, example: {a[4] <- 7}
;; $bracket-apply$ of SRFI 105
((_ ($bracket-apply$ container index) expr)
with (<- ($bracket-apply$ T 2) 1) entered at toplevel i should not be in
the third case but the first of the macro <-
that is strange... i think i could not get help from mailing list on a so
complex example that rely on my Scheme+ development for multiple values
return (aka let-values replacment with <- ),
sorry for the inconvenience
Damien
On Wed, Feb 2, 2022 at 11:33 AM Maxime Devos <maximedevos@telenet.be> wrote:
> Damien Mattei schreef op wo 02-02-2022 om 11:13 [+0100]:
> > (define-syntax macro
> > (syntax-rules ()
> > ((_ arg1 arg2) code1)
> > ((_ reserved-keyword arg) code2)))
>
> The rules are matched in-order, so when the first rule matches, the
> second rule is ignored. I suggest:
>
> (define-syntax macro
> (syntax-rules (reserved-keyword)
> ((_ reserved-keyword arg) code2)
> ((_ arg1 arg2) code1)))
>
> Greetings,
> Maxime
>
>
next prev parent reply other threads:[~2022-02-02 13:02 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-02 10:13 reserved-keyword in macro Damien Mattei
2022-02-02 10:33 ` Maxime Devos
[not found] ` <CADEOaddzjFbSYvVGEOkrSrjHSZvUkqC9beiRw29jbH3XHeRkZw@mail.gmail.com>
2022-02-02 13:02 ` Damien Mattei [this message]
2022-02-02 21:23 ` Damien Mattei
2022-02-02 21:38 ` Damien Mattei
2022-02-03 0:52 ` Vijay Marupudi
2022-02-03 10:09 ` Damien Mattei
2022-02-04 8:21 ` Damien Mattei
2022-02-04 16:24 ` Damien Mattei
2022-02-04 16:44 ` Maxime Devos
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=CADEOadddn2qCu5XWsq2uAvNDfo0vAMT+eC_E7muMDLa8aOQUqg@mail.gmail.com \
--to=damien.mattei@gmail.com \
--cc=guile-user@gnu.org \
/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).