Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ellipsis escaping, as per R7RS #711

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jpellegrini
Copy link
Contributor

@jpellegrini jpellegrini commented Jan 29, 2025

In Section 4.3.2 of R7RS (page 23 of the PDF), it is said that a can be one of:

  1. (<element> ...)
  2. (<element> <element> ... . <template>)
  3. (<ellipsis> <template>)
  4. #(<element> ...)

Number (3) is an escaping pattern. Inside ir, ellipses are treated like a symbol, and not like the syntax-rules ellipsis. And that template must have only two elements.

(<ellipsis> <template>)
should be transformed into
<template>

but protecting the ellipses (they have no special meaning there).

So

(... a)          => a
(... (a b ... c) => (a b ... c)
(... ...)        => ...
(... a b)        => error (too many elements)

but the ellipses on the right side are ignored by syntax-rules.

The implementation is:

  1. In MBE, include a check for malformed escapes (as in the last case above, (... a b))
  2. In MBE: (mbe:subst-ellipsis expr ell new-ell) will go through expr, and substitute only the sublists that match the form of an escaping ellipsis, doing the appropriate transform. However, inside the subtemplate, the ellipses will be transformed into a symbol which is internal to MBE, produced by gensym (so no match will be possible). This is in find-clause
  3. in MBE, "replace" can be used to get the original ellipses back. This is also in find-clause
  4. In runtime-macros, syntax-rules calls the internal MBE procedure check-wrong-ellipsis-escape
  5. In MBE, let-syntax also does that.

Some tests, including the example in R7RS, were added.

Fix #704

@jpellegrini
Copy link
Contributor Author

There is a glitch in this, I'm fixing.

In Section 4.3.2 of R7RS (page 23 of the PDF), it is said that
a <template> can be one of:

1. (<element> ...)
2. (<element> <element> ... . <template>)
3. (<ellipsis> <template>)
4. #(<element> ...)

Number (3) is an escaping pattern. Inside ir, ellipses are treated
like a symbol, and not like the syntax-rules ellipsis. And that
template *must* have only two elements.

(<ellipsis> <template>)
should be transformed into
<template>

but protecting the ellipses (they have no special meaning there).

So

(... a)          => a
(... (a b ... c) => (a b ... c)
(... ...)        => ...
(... a b)        => error (too many elements)

but the ellipses on the right side are ignored by syntax-rules.

The implementation is:

1. In MBE, include a check for malformed escapes (as in the last
   case above, (... a b))
2. In MBE: (mbe:subst-ellipsis expr ell new-ell) will go through expr,
   and substitute *only* the sublists that match the form of an
   escaping ellipsis, doing the appropriate transform. However,
   inside the subtemplate, the ellipses will be transformed into
   a symbol which is internal to MBE, produced by gensym (so no
   match will be possible). This is in find-clause
3. in MBE, "replace" can be used to get the original ellipses back.
   This is also in find-clause
4. In runtime-macros, syntax-rules calls the internal MBE procedure
   check-wrong-ellipsis-escape
5. In MBE, let-syntax also does that.

Some tests, including the example in R7RS, were added.
@jpellegrini
Copy link
Contributor Author

Ok, I got it working. But one of the examples does not work with let-syntax. I think it's something in let-syntax; I'll take a look later. Its code seems different from that of syntax-rules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

syntax-rules does not support patterns beginning with ellipsis, like (... ...)
1 participant