public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
* syntax? in kawa/scheme
@ 2024-05-13 12:20 Damien Mattei
  2024-05-13 14:59 ` Per Bothner
  0 siblings, 1 reply; 7+ messages in thread
From: Damien Mattei @ 2024-05-13 12:20 UTC (permalink / raw)
  To: kawa

[-- Attachment #1: Type: text/plain, Size: 117 bytes --]

hello,

any idea for a predicate for syntax object in Kawa/scheme like exist in 
Racket 'syntax?' ?

regards,
Damien

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: syntax? in kawa/scheme
  2024-05-13 12:20 syntax? in kawa/scheme Damien Mattei
@ 2024-05-13 14:59 ` Per Bothner
  2024-05-13 20:36   ` Damien Mattei
  0 siblings, 1 reply; 7+ messages in thread
From: Per Bothner @ 2024-05-13 14:59 UTC (permalink / raw)
  To: Damien.MATTEI, kawa



On 5/13/24 5:20 AM, Damien Mattei wrote:
> hello,
> 
> any idea for a predicate for syntax object in Kawa/scheme like exist in Racket 'syntax?' ?

One problem is that everything is a syntax object.

You can check if an object implements kawa.lang.SyntaxForm, but other objects are
also allowed in syntax values, if they don't need an explicit TemplateScope.
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: syntax? in kawa/scheme
  2024-05-13 14:59 ` Per Bothner
@ 2024-05-13 20:36   ` Damien Mattei
  2024-05-13 21:47     ` Per Bothner
  0 siblings, 1 reply; 7+ messages in thread
From: Damien Mattei @ 2024-05-13 20:36 UTC (permalink / raw)
  To: Per Bothner, kawa

yes in kawa there is no difference in this example:

(equal? #'(1 2 3) '(1 2 3))
#t

i had written for scheme (guile) a predicate based on the parsing of 
display of an object but it will not work with kawa:

(define (syntax? obj)

   (cond ; a syntax object is:
    ((pair? obj) ; a pair of syntax objects,
     (and (syntax? (car obj))
      (syntax? (cdr obj))))
    ((list? obj)
      (every syntax? obj))
    ((vector? obj) ; a vector of syntax objects
     (every syntax? (vector->list obj)))
    ((string? obj) ; as i will use the string representation of object
     #f)
    ;; parse the representation of object to search for #<syntax something>
    (else (let* ((str-obj (format #f "~s" obj))
         (lgt-str-obj (string-length str-obj))
         (str-syntax "#<syntax")
         (lgt-str-syntax (string-length str-syntax)))
        (and (> lgt-str-obj lgt-str-syntax) ; first, length greater
         (string=? (substring str-obj 0 lgt-str-syntax)
               str-syntax) ; begin by "#<syntax"
         (char=? #\> (string-ref str-obj (- lgt-str-obj 1)))))))) ; last 
char is >

but in fact my problem is to test equality for 2 syntax object and i can 
not use equal? :

#|kawa:34|# (equal? #'* #'*)
#f

but bound-identifier=?


#|kawa:35|# (bound-identifier=?  #'* #'*)
#t
#|kawa:36|# (bound-identifier=?  #'* #'-)
#f

my problem is to test equality for operators in a syntax parser

but this does not works for every syntax object:


#|kawa:38|# (bound-identifier=? #'(1 2 3) #'(1 2 3))
java.lang.RuntimeException: bound-identifier-? - argument is not an 
identifier
     at kawa.standard.syntax_error.error(syntax_error.java:55)
     at kawa.lib.prim_imports.reportSyntaxError(prim_imports.scm:249)
     at kawa.lib.std_syntax.isBoundIdentifier$Eq(std_syntax.scm:312)
     at atInteractiveLevel-37.run(tty:38)
     at gnu.expr.ModuleExp.evalModule2(ModuleExp.java:290)
     at gnu.expr.ModuleExp.evalModule(ModuleExp.java:211)
     at kawa.Shell.run(Shell.java:289)
     at kawa.Shell.run(Shell.java:196)
     at kawa.Shell.run(Shell.java:183)
     at kawa.repl.processArgs(repl.java:724)
     at kawa.repl.main(repl.java:830)


so i must check a difference between the syntax object that have in 
their string representation #<syntax something > and others



Le 13/05/2024 à 16:59, Per Bothner a écrit :
> Ce message provient d’un expéditeur externe à l’université (adresse : 
> per@bothner.com). Ne cliquez pas sur les liens et n’ouvrez pas les 
> pièces jointes si vous ne connaissez pas l’expéditeur ou que vous 
> n’êtes pas sûr du contenu. En cas de doute, merci de transférer le 
> mail à abuse@univ-cotedazur.fr
>
>
> On 5/13/24 5:20 AM, Damien Mattei wrote:
>> hello,
>>
>> any idea for a predicate for syntax object in Kawa/scheme like exist 
>> in Racket 'syntax?' ?
>
> One problem is that everything is a syntax object.
>
> You can check if an object implements kawa.lang.SyntaxForm, but other 
> objects are
> also allowed in syntax values, if they don't need an explicit 
> TemplateScope.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: syntax? in kawa/scheme
  2024-05-13 20:36   ` Damien Mattei
@ 2024-05-13 21:47     ` Per Bothner
  2024-05-13 22:20       ` Damien Mattei
  0 siblings, 1 reply; 7+ messages in thread
From: Per Bothner @ 2024-05-13 21:47 UTC (permalink / raw)
  To: Damien.MATTEI, kawa



On 5/13/24 1:36 PM, Damien Mattei wrote:
> but in fact my problem is to test equality for 2 syntax object and i can not use equal? :

Why? What are you actually trying to do?
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: syntax? in kawa/scheme
  2024-05-13 21:47     ` Per Bothner
@ 2024-05-13 22:20       ` Damien Mattei
  2024-05-14  7:48         ` Damien Mattei
  0 siblings, 1 reply; 7+ messages in thread
From: Damien Mattei @ 2024-05-13 22:20 UTC (permalink / raw)
  To: Per Bothner, kawa

i modify a SRFI 105  curly infix parser to move some function towards 
macro, then to pre-parse the code on the fly at the pre-compil stage of 
the macro,then use the pre-parsed code in the expansion phase. Finally 
the run-time execution of the generated code will be faster as no 
parsing will be done at this run-time phase. (exmple : a mathematic 
formula is known and will not change at run-time ,the infix to prefix 
operator precedence algorithm can be applied before runtime) . Prior i 
parsed s-expr now it will be syntax expression, the logic is the same 
but the code will change, instead of procedure or quoted procedures or 
form it will be syntax objects. For the operator precedence i need to be 
able to compare equality of some operator in a syntaxic form (like #'*, 
#'+ etc...) I admit as there is no native curly-infix srfi 105 in kawa 
it is already done by a parser in command line. (this more easy in Guile 
but very few scheme implement SRFI 105 anyway so i will reuse this code 
in the future)

Le 13/05/2024 à 23:47, Per Bothner a écrit :
> Ce message provient d’un expéditeur externe à l’université (adresse : 
> per@bothner.com). Ne cliquez pas sur les liens et n’ouvrez pas les 
> pièces jointes si vous ne connaissez pas l’expéditeur ou que vous 
> n’êtes pas sûr du contenu. En cas de doute, merci de transférer le 
> mail à abuse@univ-cotedazur.fr
>
>
> On 5/13/24 1:36 PM, Damien Mattei wrote:
>> but in fact my problem is to test equality for 2 syntax object and i 
>> can not use equal? :
>
> Why? What are you actually trying to do?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: syntax? in kawa/scheme
  2024-05-13 22:20       ` Damien Mattei
@ 2024-05-14  7:48         ` Damien Mattei
  2024-05-14  7:59           ` Damien Mattei
  0 siblings, 1 reply; 7+ messages in thread
From: Damien Mattei @ 2024-05-14  7:48 UTC (permalink / raw)
  To: kawa

but in fact testing the string representation is enought for me as i 
need only to test for operators such as #'* #'+....

indeed only Racket has a good syntax? procedure:

Welcome to DrRacket, version 8.12 [cs].
Language: racket, with debugging; memory limit: 8192 MB.

 > (syntax? #'(2 * 3))
#t

someone answered me about syntax? for Guile being in (system syntax) but 
it works well only on atomic expression:

scheme@(guile-user)> (use-modules (system syntax))
scheme@(guile-user)> syntax?
$1 = #<procedure syntax? (_)>
scheme@(guile-user)> (syntax? #'(2 * 3))
$2 = #f


this is not the good result (above)

as:

scheme@(guile-user)> #'(2 * 3)
$3 = (#<syntax:unknown file:6:3 2> #<syntax:unknown file:6:5 *> 
#<syntax:unknown file:6:7 3>)
scheme@(guile-user)> (syntax? #'*)
$4 = #t

scheme@(guile-user)> (syntax? *)
$6 = #f


but if someone explain me how to  'check if an object implements 
kawa.lang.SyntaxForm' i can do that?

perheaps with an exception....


Damien

Le 14/05/2024 à 00:20, Damien Mattei a écrit :
> Ce message provient d’un expéditeur externe à l’université (adresse : 
> kawa-bounces+damien.mattei=unice.fr@sourceware.org). Ne cliquez pas 
> sur les liens et n’ouvrez pas les pièces jointes si vous ne connaissez 
> pas l’expéditeur ou que vous n’êtes pas sûr du contenu. En cas de 
> doute, merci de transférer le mail à abuse@univ-cotedazur.fr
>
> i modify a SRFI 105  curly infix parser to move some function towards 
> macro, then to pre-parse the code on the fly at the pre-compil stage 
> of the macro,then use the pre-parsed code in the expansion phase. 
> Finally the run-time execution of the generated code will be faster as 
> no parsing will be done at this run-time phase. (exmple : a mathematic 
> formula is known and will not change at run-time ,the infix to prefix 
> operator precedence algorithm can be applied before runtime) . Prior i 
> parsed s-expr now it will be syntax expression, the logic is the same 
> but the code will change, instead of procedure or quoted procedures or 
> form it will be syntax objects. For the operator precedence i need to 
> be able to compare equality of some operator in a syntaxic form (like 
> #'*, #'+ etc...) I admit as there is no native curly-infix srfi 105 in 
> kawa it is already done by a parser in command line. (this more easy 
> in Guile but very few scheme implement SRFI 105 anyway so i will reuse 
> this code in the future)
>
> Le 13/05/2024 à 23:47, Per Bothner a écrit :
>> Ce message provient d’un expéditeur externe à l’université (adresse : 
>> per@bothner.com). Ne cliquez pas sur les liens et n’ouvrez pas les 
>> pièces jointes si vous ne connaissez pas l’expéditeur ou que vous 
>> n’êtes pas sûr du contenu. En cas de doute, merci de transférer le 
>> mail à abuse@univ-cotedazur.fr
>>
>>
>> On 5/13/24 1:36 PM, Damien Mattei wrote:
>>> but in fact my problem is to test equality for 2 syntax object and i 
>>> can not use equal? :
>>
>> Why? What are you actually trying to do?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: syntax? in kawa/scheme
  2024-05-14  7:48         ` Damien Mattei
@ 2024-05-14  7:59           ` Damien Mattei
  0 siblings, 0 replies; 7+ messages in thread
From: Damien Mattei @ 2024-05-14  7:59 UTC (permalink / raw)
  To: kawa

i can do a minimal syntax check like this for operator (works also for 
procedures):

;; parse the representation of object to search for #<syntax something>
(define (syntax-check? obj)

   (let* ((str-obj (format #f "~s" obj))
      (lgt-str-obj (string-length str-obj))
      (str-syntax "#<syntax")
      (lgt-str-syntax (string-length str-syntax)))
     (and (> lgt-str-obj lgt-str-syntax) ; first, length greater
      (string=? (substring str-obj 0 lgt-str-syntax)
            str-syntax) ; begin by "#<syntax"
      (char=? #\> (string-ref str-obj (- lgt-str-obj 1)))))) ; last char 
is >


#|kawa:57|# (syntax-check? #'cons)
#t
#|kawa:58|# (syntax-check? #'*)
#t
#|kawa:59|# (syntax-check? *)
#f


Le 14/05/2024 à 09:48, Damien Mattei a écrit :
> Ce message provient d’un expéditeur externe à l’université (adresse : 
> kawa-bounces+damien.mattei=unice.fr@sourceware.org). Ne cliquez pas 
> sur les liens et n’ouvrez pas les pièces jointes si vous ne connaissez 
> pas l’expéditeur ou que vous n’êtes pas sûr du contenu. En cas de 
> doute, merci de transférer le mail à abuse@univ-cotedazur.fr
>
> but in fact testing the string representation is enought for me as i 
> need only to test for operators such as #'* #'+....
>
> indeed only Racket has a good syntax? procedure:
>
> Welcome to DrRacket, version 8.12 [cs].
> Language: racket, with debugging; memory limit: 8192 MB.
>
> > (syntax? #'(2 * 3))
> #t
>
> someone answered me about syntax? for Guile being in (system syntax) 
> but it works well only on atomic expression:
>
> scheme@(guile-user)> (use-modules (system syntax))
> scheme@(guile-user)> syntax?
> $1 = #<procedure syntax? (_)>
> scheme@(guile-user)> (syntax? #'(2 * 3))
> $2 = #f
>
>
> this is not the good result (above)
>
> as:
>
> scheme@(guile-user)> #'(2 * 3)
> $3 = (#<syntax:unknown file:6:3 2> #<syntax:unknown file:6:5 *> 
> #<syntax:unknown file:6:7 3>)
> scheme@(guile-user)> (syntax? #'*)
> $4 = #t
>
> scheme@(guile-user)> (syntax? *)
> $6 = #f
>
>
> but if someone explain me how to  'check if an object implements 
> kawa.lang.SyntaxForm' i can do that?
>
> perheaps with an exception....
>
>
> Damien
>
> Le 14/05/2024 à 00:20, Damien Mattei a écrit :
>> Ce message provient d’un expéditeur externe à l’université (adresse : 
>> kawa-bounces+damien.mattei=unice.fr@sourceware.org). Ne cliquez pas 
>> sur les liens et n’ouvrez pas les pièces jointes si vous ne 
>> connaissez pas l’expéditeur ou que vous n’êtes pas sûr du contenu. En 
>> cas de doute, merci de transférer le mail à abuse@univ-cotedazur.fr
>>
>> i modify a SRFI 105  curly infix parser to move some function towards 
>> macro, then to pre-parse the code on the fly at the pre-compil stage 
>> of the macro,then use the pre-parsed code in the expansion phase. 
>> Finally the run-time execution of the generated code will be faster 
>> as no parsing will be done at this run-time phase. (exmple : a 
>> mathematic formula is known and will not change at run-time ,the 
>> infix to prefix operator precedence algorithm can be applied before 
>> runtime) . Prior i parsed s-expr now it will be syntax expression, 
>> the logic is the same but the code will change, instead of procedure 
>> or quoted procedures or form it will be syntax objects. For the 
>> operator precedence i need to be able to compare equality of some 
>> operator in a syntaxic form (like #'*, #'+ etc...) I admit as there 
>> is no native curly-infix srfi 105 in kawa it is already done by a 
>> parser in command line. (this more easy in Guile but very few scheme 
>> implement SRFI 105 anyway so i will reuse this code in the future)
>>
>> Le 13/05/2024 à 23:47, Per Bothner a écrit :
>>> Ce message provient d’un expéditeur externe à l’université (adresse 
>>> : per@bothner.com). Ne cliquez pas sur les liens et n’ouvrez pas les 
>>> pièces jointes si vous ne connaissez pas l’expéditeur ou que vous 
>>> n’êtes pas sûr du contenu. En cas de doute, merci de transférer le 
>>> mail à abuse@univ-cotedazur.fr
>>>
>>>
>>> On 5/13/24 1:36 PM, Damien Mattei wrote:
>>>> but in fact my problem is to test equality for 2 syntax object and 
>>>> i can not use equal? :
>>>
>>> Why? What are you actually trying to do?

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-05-14  7:59 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-13 12:20 syntax? in kawa/scheme Damien Mattei
2024-05-13 14:59 ` Per Bothner
2024-05-13 20:36   ` Damien Mattei
2024-05-13 21:47     ` Per Bothner
2024-05-13 22:20       ` Damien Mattei
2024-05-14  7:48         ` Damien Mattei
2024-05-14  7:59           ` Damien Mattei

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).