chibi-scheme icon indicating copy to clipboard operation
chibi-scheme copied to clipboard

Expansion of templates with leading ellipsis - do not allow invalid templates

Open jpellegrini opened this issue 1 year ago • 5 comments

Hello!

From what I understand of R7RS, the templates beginning with ellipses, like (... a b c etc) are expressed as a list, but not expanded as one (that is, they should be spliced into the code generated from the template).

A template of the form (<ellipsis> <template>) is identical to <template>, except that ellipses within the template have no special meaning

That the text says "(<ellipsis> <template>) is identical to <template>" seems to indicate that the list should be spliced (no?) Also,

In particular, the template (<ellipsis> <ellipsis>) produces a single <ellipsis>

seems to be concordant with that.

For example:

(define-syntax f
    (syntax-rules ()
      ((f) '(a b (... c ... d) e))))

would splice the (c ... d) list into the expanded form:

(f) => (a b c ... d e)

But Chibi seems to not splice the list:

(define-syntax f
   (syntax-rules ()
     ((f) '(a b (... c ... d) e))))

(f) => (a b (c ... d) e)

unless it's a single ellipsis (?)

(define-syntax f
    (syntax-rules ()
      ((f) '(a (... ...) b))))

(f) => (a ... b)

Maybe I didn't get some detail (sorry for the noise if this is the case).

jpellegrini avatar Dec 27 '24 02:12 jpellegrini

The point is the template pattern is (<ellipsis> <template>), i.e. a single element after the ellipsis. So:

(... c ... d)

is simply invalid. I should probably update it to throw an exception in that case.

ashinn avatar Dec 27 '24 02:12 ashinn

Ah, I misread the standard. Only the improper tail is a "subtemplate". Sorry!

jpellegrini avatar Dec 27 '24 02:12 jpellegrini

Wait... If a single element should be after the ellipsis, then it only makes sense to use (... ...), since (... something-else) would be meaningless -- I could have written something-else instead.

Let me see if I understand this right: in page 24 of R7RS,

  • A rule must be (<pattern> <template>)
  • A <template> can be (<element> <element> ... . <template>) second case for templates described in R7RS, page 24)
  • "where an <element> is a <template> optionally followed by an <ellipsis>.

Then,

  • A template of the form (<ellipsis> <template>) is identical to <template>, except that ellipses within the template have no special meaning. That is, any ellipses contained within <template> are treated as ordinary identifiers. (page 25)

So there could be several templates, in sequence, and any of them could be of the form (<ellipsis> <template>), if I understand correctly. And there could be several elements after the ellipsis.

And since (<ellipsis> <template) is equivalent to <template>, the effect would be to both turn the ellipsis to an ordinary symbol inside the template, and splice the list.

Is my interpretation correct?

jpellegrini avatar Jan 29 '25 02:01 jpellegrini

It's an escaping mechanism. ... inside <template> are not handled specially.

So the most trivial case is indeed (... ...) to insert a literal ... which you otherwise can't do. However, you can also use this to insert larger templates with nested ....

ashinn avatar Jan 29 '25 04:01 ashinn

Sorry, I misunderstood what you had said. Right, I re-read R7RS, and see where my misinterpretation was.

jpellegrini avatar Jan 29 '25 18:01 jpellegrini