safer use of ... in syntax-rules

This commit is contained in:
ESL 2023-03-18 16:07:10 -04:00
parent cbb8eed040
commit cd1b192446
2 changed files with 1760 additions and 1681 deletions

3417
k.c

File diff suppressed because it is too large Load diff

View file

@ -557,19 +557,23 @@
(assq tmpl new-literals)))] (assq tmpl new-literals)))]
[(vector? tmpl) [(vector? tmpl)
(list->vector (expand-part (vector->list tmpl)))] (list->vector (expand-part (vector->list tmpl)))]
[(pair? tmpl) [(and (pair? tmpl) (ellipsis-pair? (cdr tmpl)))
(if (ellipsis-pair? (cdr tmpl)) (let ([vars-to-iterate (list-ellipsis-vars (car tmpl))])
(let ([vars-to-iterate (list-ellipsis-vars (car tmpl))]) (define (lookup var)
(define (lookup var) (cdr (assq var bindings)))
(cdr (assq var bindings))) (define (expand-using-vals . vals)
(define (expand-using-vals . vals) (expand (car tmpl)
(expand (car tmpl) (map cons vars-to-iterate vals)))
(map cons vars-to-iterate vals))) (if (null? vars-to-iterate)
; ellipsis following non-repeatable part is an error, but we don't care
(cons (expand-part (car tmpl)) (expand-part (cddr tmpl))) ; repeat once
; correct use of ellipsis
(let ([val-lists (map lookup vars-to-iterate)]) (let ([val-lists (map lookup vars-to-iterate)])
(append (append
(apply map (cons expand-using-vals val-lists)) (apply map (cons expand-using-vals val-lists))
(expand-part (cddr tmpl))))) (expand-part (cddr tmpl))))))]
(cons (expand-part (car tmpl)) (expand-part (cdr tmpl))))] [(pair? tmpl)
(cons (expand-part (car tmpl)) (expand-part (cdr tmpl)))]
[else tmpl])))) [else tmpl]))))
(lambda (use use-env) (lambda (use use-env)