guile-user
[Top][All Lists]
Advanced

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

Re: reserved-keyword in macro


From: Damien Mattei
Subject: Re: reserved-keyword in macro
Date: Wed, 2 Feb 2022 22:38:31 +0100

i suppose it has something to see with this:
"A literal matches an input expression if the input expression is an
identifier with the same name as the literal, and both are unbound13
<https://www.gnu.org/software/guile/manual/html_node/Syntax-Rules.html#FOOT13>.
"
as $bracket-apply$ is already bind to a definition the pattern will not be
matched:
https://www.gnu.org/software/guile/manual/html_node/Syntax-Rules.html


On Wed, Feb 2, 2022 at 10:23 PM Damien Mattei <damien.mattei@gmail.com>
wrote:

> really,nothing have an idea? seems $bracket-apply$ is bind to the variable
> var of the third case in the macro instead of ignored in the first case...
> but why? (it the same thing if i change the patterns order)
>
> On Wed, Feb 2, 2022 at 2:02 PM Damien Mattei <damien.mattei@gmail.com>
> wrote:
>
>> 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
>>>
>>>


reply via email to

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