forked from alex-eg/sex
take an unnamed closure parameter, and refuse a variadic one
The closure's type was built from each parameter's second element, so a type alone, or the ..., crashed sexc. A variadic closure cannot work: its call helper would have to forward what C cannot.
This commit is contained in:
19
semen.scm
19
semen.scm
@@ -1822,13 +1822,15 @@
|
||||
(define (lift-closure base form env)
|
||||
(match form
|
||||
(('closure arglist ret-type captures . body)
|
||||
;; its call helper would have to forward the rest, which C cannot
|
||||
(when (any (lambda (arg) (or (eq? arg '...) (equal? arg '(...)))) arglist)
|
||||
(sex-error form "a closure cannot be variadic; a lambda can"))
|
||||
(check-capture-names form arglist (map capture-name captures) body)
|
||||
(let* ((caps (map (lambda (c) (capture-binding c form env)) captures))
|
||||
(record (suffixed base "_captures"))
|
||||
(code (suffixed base "_code"))
|
||||
(construct (suffixed base "_make"))
|
||||
(type `(closure ,(map (lambda (p) (list (second p))) arglist)
|
||||
,ret-type))
|
||||
(type `(closure ,(arglist-types arglist) ,ret-type))
|
||||
(closure (register-closure-type! type form)))
|
||||
(values
|
||||
construct
|
||||
@@ -1841,7 +1843,7 @@
|
||||
(list `(struct ,record ,(map capture-field caps))))
|
||||
|
||||
(list
|
||||
`(fn ,code ((ƛe (* void)) ,@arglist) ,ret-type
|
||||
`(fn ,code ((ƛe (* void)) ,@(named-parameters arglist)) ,ret-type
|
||||
,@(if (null? caps)
|
||||
(list)
|
||||
`((var ƛcaptures (* (struct ,record)) ƛe)
|
||||
@@ -1865,6 +1867,17 @@
|
||||
(return ƛc))))))))
|
||||
(else (sex-error form "malformed closure" form))))
|
||||
|
||||
;;; A definition names every parameter before C23, so one written as a
|
||||
;;; type alone gets a name nothing reads: ((int) (b int)) -> ((ƛu0 int) (b int))
|
||||
(define (named-parameters arglist)
|
||||
(map (lambda (arg index)
|
||||
(if (or (named-arg? arg) (comment-form? arg))
|
||||
arg
|
||||
(list (string->symbol (string-append "ƛu" (number->string index)))
|
||||
(unwrap-type arg))))
|
||||
arglist
|
||||
(iota (length arglist))))
|
||||
|
||||
;;; Captures are a closure's other parameters, and C's rule for those
|
||||
;;; holds of them: no two alike, and none redeclared at the top of the
|
||||
;;; body. A block inside may shadow one, as it may a parameter.
|
||||
|
||||
Reference in New Issue
Block a user