diff --git a/semen.scm b/semen.scm index 93b8c3c..a791da5 100644 --- a/semen.scm +++ b/semen.scm @@ -1641,7 +1641,17 @@ ;;; The code pointer takes the environment first; everything else is ;;; the closure's own signature. (define (closure-code-type type) - `(fn (((* void)) ,@(second type)) ,(third type))) + (let ((type (signature-resolved type))) + `(fn (((* void)) ,@(second type)) ,(third type)))) + +;;; TYPE with the closure types of its signature made their structs: the +;;; code generated for it goes out as it is, and C has no `closure'. +;;; +;;; (closure ((int)) (closure ((int)) int)) +;;; -> (closure ((int)) (struct ƛint_int)) +(define (signature-resolved type) + `(closure ,(map resolve-closure-types (second type)) + ,(resolve-closure-types (third type)))) ;;; Emitted once per signature, before the toplevel form that first ;;; needed it. @@ -1697,6 +1707,7 @@ ;;; } (define (register-fn-conversion! type src-form) (let* ((closure (closure-struct-name type)) + (type (signature-resolved type)) (convert (suffixed closure "_fromfn")) (record (suffixed closure "_fnptr")) (thunk (suffixed closure "_fnthunk")) @@ -1756,6 +1767,7 @@ ;;; `table[++i].code(&table[++i].env, 10)' and bump `i' twice. (define (register-closure-call! type src-form) (let* ((closure (closure-struct-name type)) + (type (signature-resolved type)) (helper (suffixed closure "_call")) (returns (third type)) (params (map (lambda (arg index) diff --git a/tests/sex-programs/closure-signatures.sex b/tests/sex-programs/closure-signatures.sex index 494bcde..e483e3c 100644 --- a/tests/sex-programs/closure-signatures.sex +++ b/tests/sex-programs/closure-signatures.sex @@ -4,7 +4,9 @@ "unsigned, one argument: 9" "unsigned, two arguments: 3" "captured global: 12" - "captured function: 8") + "captured function: 8" + "curried: 42 42" + "taking one: 21") (return 0) ;;; A closure's struct is named after its signature, so that two @@ -16,6 +18,9 @@ ;;; ;;; A capture that borrows a name reads it from wherever the name is ;;; declared, a global or a function included. +;;; +;;; A closure's signature may name closures in turn: one returned is +;;; currying, one taken is a callback. (include stdio.h) @@ -24,6 +29,10 @@ (fn double-it ((n int)) int (return (* n 2))) +(fn curried-add () (closure ((int)) (closure ((int)) int)) + (return (closure ((a int)) (closure ((int)) int) () + (return (closure ((b int)) int (a) (return (+ a b))))))) + (pub fn main () int (var one-wide (closure ((long long)) int) (closure ((a (long long))) int () (return (cast a int)))) @@ -49,4 +58,12 @@ (var doubled (closure ((int)) int) (closure ((x int)) int (double-it) (return (double-it x)))) (printf "captured function: %d\n" (doubled 4)) + + (var add (closure ((int)) (closure ((int)) int)) (curried-add)) + (var add2 (closure ((int)) int) (add 2)) + (printf "curried: %d %d\n" (add2 40) ((add 1) 41)) + + (var twice (closure (((closure ((int)) int)) (int)) int) + (closure ((f (closure ((int)) int)) (x int)) int () (return (f (f x))))) + (printf "taking one: %d\n" (twice add2 17)) (return 0))