mighty-gerbils / gerbil

Gerbil Scheme
https://cons.io
GNU Lesser General Public License v2.1
1.15k stars 110 forks source link

Cannot specify an ellipsis for syntax-rules #706

Open Zambito1 opened 1 year ago

Zambito1 commented 1 year ago

In R7RS, syntax-rules has an optional extra argument to specify the term to use for the ellipsis. https://index.scheme.org/filterset/r7rs_small/%28scheme%2520base%29/syntax-rules

This does not seem to be handled correctly by Gerbil.

$ gxi --lang r7rs
R7RS Scheme in Gerbil v0.17 on Gambit v4.9.3
> (import (scheme base))
> (define-syntax foo (syntax-rules () ((_ x) (+ x 1))))
> (foo 5)
6
> (define-syntax foo (syntax-rules ::: () ((_ x :::) (+ x ::: 1))))
*** ERROR IN ##main -- Syntax Error
*** ERROR IN (stdin)@4.20
--- Syntax Error: Bad syntax
... form:   (syntax-rules ::: () ((_ x :::) (+ x ::: 1)))
> 
*** EOF again to exit
$ chibi-scheme -R
> (define-syntax foo (syntax-rules () ((_ x) (+ x 1))))
> (foo 5)
6
> (define-syntax foo (syntax-rules ::: () ((_ x :::) (+ x ::: 1))))
> (foo 1 2 3 4)
11
vyzo commented 1 year ago

Yeah, iirc it was in my todo list but never got around implementing it.

Care for a pr? It shouldnt be all that hard.

We can also just document it as a limitation, waiting for the next hacker to be annoyed enough to fix it ;)

vyzo commented 1 year ago

Ah btw, ::: is a keyword!

So you should |:::| or use an identifier.

iacore commented 6 months ago

I think this is not a bug?

vyzo commented 6 months ago

Using a keyword is wrong, but still, custom ellipsis is not implemented atm. It is not a bug per se, more of an omission, that is readily fixable.

I will try to fix it for v0.18.2.

vyzo commented 6 months ago

You can also give it a go if you want!

iacore commented 6 months ago

I found a way to workaround this

❯ rlwrap gerbil
Gerbil v0.18.1 on Gambit v4.9.5-78-g8b18ab69
> (define-syntax foo (syntax-rules () ((_ x . xs) (+ x 1 . xs))))
> (foo 1)
2
> (foo 2)
3
> 
iacore commented 6 months ago

You can also give it a go if you want!

I looked at the code in core.scm and I don't know what I was looking at.

it seems to be something like this:

(define-syntax ... (syntax-rules () (_ (unquote-splicing xs))))
vyzo commented 6 months ago

It needs to be supported by syntax-case, it is in src/gerbil/expander/stxcase.ss

iacore commented 6 months ago

It seems like i just need to replace ellipsis? with a custom one. Is the symbol lexical in Gambit? Can I just replace that somehow?

vyzo commented 6 months ago

yes, it is simple.

vyzo commented 6 months ago

Add an optional argument for the ellipsis? procedure, defaulting to the real ellipsis?.

You will the have to change the syntax-rules macro a bit to recongize and pass the parameter as free-identifier=? the user's identifier.

iacore commented 6 months ago

is it possible to nest ellipsis somehow?

for example:

(syntax-rules ...a () ((_ ...a)
(syntax-rules ...b () ((_ ...b)
(test ...a)
))
))
vyzo commented 6 months ago

yes, (... ...)

iacore commented 6 months ago

I don't know enough about Gerbil to patch this. Is it possible to rewrite the body before it is sent to src/gerbil/expander/stxcase.ss?

vyzo commented 6 months ago

nah, no need. I will add it to my todo list.