diff --git a/semen.scm b/semen.scm index 5a3505f..f0733df 100644 --- a/semen.scm +++ b/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 diff --git a/tests/sex-programs/closures.sex b/tests/sex-programs/closures.sex index fa20184..adca632 100644 --- a/tests/sex-programs/closures.sex +++ b/tests/sex-programs/closures.sex @@ -10,7 +10,9 @@ "Nested: 33" "Named capture: 7" "Captured pointer: 11 then 12" - "From a bare fn: 20 42 7") + "From a bare fn: 20 42 7" + "Captured array: 6 3, still 6" + "Captured const: 6") (return 0) ;;; A closure is a code pointer beside its captures, so what this @@ -118,4 +120,19 @@ (apply-twice from-fn 5) ((as-closure) 21) (apply-twice (lambda ((n int)) int (return (+ n 1))) 5)) + + ;; an array is copied in, element by element, as C copies one neither + ;; by = nor into a parameter; so are the rows of one of arrays + (var xs [int 3] #(1 2 3)) + (var m [int 2 2] #(#(1 2) #(3 4))) + (var sum3 (closure () int) (closure () int (xs) (return (+ [xs 0] [xs 1] [xs 2])))) + (var corner (closure () int) (closure () int (m) (return [m 1 0]))) + (printf "Captured array: %d %d" (sum3) (corner)) + (= [xs 0] 100) + (printf ", still %d\n" (sum3)) + + ;; a const local is captured as its value, and stays const inside + (var k (const int) 5) + (var plus-k (closure ((int)) int) (closure ((v int)) int (k) (return (+ v k)))) + (printf "Captured const: %d\n" (plus-k 1)) (return 0))