forked from alex-eg/sex
capture an array and a const local
C copies an array neither by = nor from another array's initializer, and a const field cannot be assigned: both failed in the C. An array is copied in element by element; a const one is held as its value.
This commit is contained in:
50
semen.scm
50
semen.scm
@@ -1837,17 +1837,14 @@
|
||||
(append
|
||||
(if (null? caps)
|
||||
(list)
|
||||
(list `(struct ,record ,caps)))
|
||||
(list `(struct ,record ,(map capture-field caps))))
|
||||
|
||||
(list
|
||||
`(fn ,code ((ƛe (* void)) ,@arglist) ,ret-type
|
||||
,@(if (null? caps)
|
||||
(list)
|
||||
`((var ƛcaptures (* (struct ,record)) ƛe)
|
||||
,@(map (lambda (cap)
|
||||
`(var ,(first cap) ,(second cap)
|
||||
(%-> ƛcaptures ,(first cap))))
|
||||
caps)))
|
||||
,@(append-map (cut capture-local <> form) caps)))
|
||||
,@body)
|
||||
|
||||
`(fn ,construct ,caps (struct ,closure)
|
||||
@@ -1863,12 +1860,49 @@
|
||||
(list)
|
||||
`((var ƛcaptures (* (struct ,record))
|
||||
(cast (& (dot-access ƛc env)) (* (struct ,record))))
|
||||
,@(map (lambda (cap)
|
||||
`(= (%-> ƛcaptures ,(first cap)) ,(first cap)))
|
||||
caps)))
|
||||
,@(map (cut capture-store <> form) caps)))
|
||||
(return ƛc))))))))
|
||||
(else (sex-error form "malformed closure" form))))
|
||||
|
||||
;;; A capture's field is assigned to, so it drops what qualifies the
|
||||
;;; capture itself: (const int) is held as an int, and read back into a
|
||||
;;; const one. (* const char) points at const, and is left alone.
|
||||
(define (capture-field cap)
|
||||
(let ((type (second cap)))
|
||||
(list (first cap)
|
||||
(if (and (pair? type) (memq (car type) '(const volatile)))
|
||||
(let ((rest (drop-while (cut memq <> '(const volatile)) type)))
|
||||
(if (null? (cdr rest)) (car rest) rest))
|
||||
type))))
|
||||
|
||||
;;; The code's local for a capture, read back out of the record
|
||||
(define (capture-local cap form)
|
||||
(let ((name (first cap)) (type (second cap)))
|
||||
(if (array-form? type)
|
||||
`((var ,name ,type) ,(array-copy type name `(%-> ƛcaptures ,name) form))
|
||||
`((var ,name ,type (%-> ƛcaptures ,name))))))
|
||||
|
||||
;;; The constructor's filling of a capture's field
|
||||
(define (capture-store cap form)
|
||||
(let ((name (first cap)) (type (second cap)))
|
||||
(if (array-form? type)
|
||||
(array-copy type `(%-> ƛcaptures ,name) name form)
|
||||
`(= (%-> ƛcaptures ,name) ,name))))
|
||||
|
||||
;;; DEST, an array of TYPE, given SRC's elements one at a time: C copies
|
||||
;;; an array neither by = nor through an initializer of another
|
||||
;;;
|
||||
;;; (for (var ƛi0 int 0) (< ƛi0 3) (++ ƛi0) (= [dest ƛi0] [src ƛi0]))
|
||||
(define (array-copy type dest src form #!optional (depth 0))
|
||||
(unless (array-bound? type)
|
||||
(sex-error form "a captured array needs its length written" dest))
|
||||
(let ((i (string->symbol (string-append "ƛi" (number->string depth))))
|
||||
(element (array-element-type type)))
|
||||
`(for (var ,i int 0) (< ,i ,(array-bound type)) (++ ,i)
|
||||
,(if (array-form? element)
|
||||
(array-copy element `(¤ ,dest ,i) `(¤ ,src ,i) form (+ depth 1))
|
||||
`(= (¤ ,dest ,i) (¤ ,src ,i))))))
|
||||
|
||||
;;; Structs
|
||||
|
||||
;;; Record the named structs, unions and enums in the type database
|
||||
|
||||
Reference in New Issue
Block a user