forked from alex-eg/sex
remove closures, keep lambdas
A closure was a code pointer beside an inline environment: capture records, per-signature structs, conversion thunks and call helpers. It is gone; a lambda still lifts to an ordinary function and a (fn ...) value is still a bare code pointer. The capture budget, the environment union and the closure glyph go with it. The implementation is kept at tag 0.0.1.
This commit is contained in:
@@ -474,52 +474,12 @@
|
||||
(not (emits? (in-fn "(static-assert (== (sizeof int) 4) \"x\")")
|
||||
"static_assert("))))
|
||||
|
||||
;; A closure is a code pointer beside its captures. The struct is
|
||||
;; named from the signature, so separate translation units agree on
|
||||
;; it, and calling one goes through `code' with `env' passed first.
|
||||
;;
|
||||
;; A closure struct, and the helper its calls go through, are each
|
||||
;; emitted once per signature; the registries deciding that are
|
||||
;; compile-time state like the type databases, and outlive a single
|
||||
;; `sex->c' here. So every case below that looks for a *definition*
|
||||
;; uses a signature of its own -- cases looking at a call site can
|
||||
;; share one.
|
||||
(test-group "closures"
|
||||
(test-assert "the type becomes a struct named for its signature"
|
||||
(emits? "(fn f ((c (closure ((int)) int))) int (return (c 1)))"
|
||||
"struct ƛint_int"))
|
||||
;; captures are a closure's other parameters, under C's rule for
|
||||
;; those: no two alike, and none redeclared at the body's top level.
|
||||
;; Both were C's redefinition errors
|
||||
;; ((+ a 1)) was a capture named `+', holding a
|
||||
(test-assert "a capture is a name, or a name and a value"
|
||||
(reports? (in-fn "(var c (closure () int) (closure () int ((+ a 1)) (return 1)))")
|
||||
"a capture is a name, or (name value)"))
|
||||
(test-assert "a capture named like a parameter is refused"
|
||||
(reports? (in-fn "(var c (closure ((int)) int) (closure ((a int)) int (a) (return a)))")
|
||||
"a capture and a parameter share a name"))
|
||||
(test-assert "so is one captured twice"
|
||||
(reports? (in-fn "(var c (closure () int) (closure () int (a a) (return a)))")
|
||||
"two captures share a name"))
|
||||
(test-assert "so is one redeclared at the top of the body"
|
||||
(reports? (in-fn "(var c (closure () int) (closure () int (a) (var a int 5) (return a)))")
|
||||
"the body redeclares a capture"))
|
||||
(test-assert "while a block inside may shadow it"
|
||||
(emits? (in-fn "(var c (closure () int) (closure () int (a) (do (var a int 5) (g a)) (return a)))")
|
||||
"int a = 5;"))
|
||||
;; the closure's type was built from each parameter's second element,
|
||||
;; which a type alone has none of
|
||||
(test-assert "a parameter may name nothing"
|
||||
(emits? (in-fn "(var c (closure ((int) (int)) int) (closure ((int) (b int)) int () (return b)))")
|
||||
"struct ƛint__int"))
|
||||
(test-assert "and its code names it, a definition needing one before C23"
|
||||
(emits? (in-fn "(var c (closure ((int) (int)) int) (closure ((int) (b int)) int () (return b)))")
|
||||
"int ƛu0, int b)"))
|
||||
;; a definition names every parameter before C23, a lambda's as a
|
||||
;; closure's code does
|
||||
(test-group "lambdas"
|
||||
;; a definition names every parameter before C23, so one written as a
|
||||
;; type alone gets a name nothing reads
|
||||
(test-assert "a lambda's unnamed parameter is named in its definition"
|
||||
(emits? "(fn f () void (var g (fn ((int) (int)) int) (lambda ((int) (b int)) int (return b))))"
|
||||
"λ0_f (int ƛu0, int b)"))
|
||||
"λ0_f (int λu0, int b)"))
|
||||
(test-assert "and a variadic one keeps its ..."
|
||||
(emits? "(fn f () void (var g (fn ((int) ...) int) (lambda ((n int) ...) int (return n))))"
|
||||
"λ0_f (int n, ...)"))
|
||||
@@ -527,120 +487,7 @@
|
||||
;; nothing defined it
|
||||
(test-assert "an empty lambda is an empty function"
|
||||
(emits? "(fn f () void (var g (fn () void) (lambda () void)))"
|
||||
"static void λ0_f (void) {}"))
|
||||
(test-assert "so is an empty closure's code"
|
||||
(emits? (in-fn "(var c (closure () void) (closure () void ()))") "(void * ƛe) {}"))
|
||||
;; the call helper would have to forward the rest, which C cannot
|
||||
(test-assert "a closure cannot be variadic"
|
||||
(reports? (in-fn "(var c _ (closure ((n int) ...) int () (return n)))")
|
||||
"a closure cannot be variadic; a lambda can"))
|
||||
;; The environment is one shared union, declared in the prelude --
|
||||
;; its layout is part of the ABI two units agree on, so it cannot
|
||||
;; depend on what either file contains. `sex->c' has no prelude, so
|
||||
;; what is visible here is the member
|
||||
(test-assert "whose environment is the shared union"
|
||||
(emits? "(fn f ((c (closure ((float)) int))) int (return (c 1.0)))"
|
||||
"union ƛenv env;"))
|
||||
;; The receiver goes through a helper rather than being written
|
||||
;; out twice, so that `[table (++ i)]' evaluates its index once,
|
||||
;; exactly as it would for an array of function pointers
|
||||
(test-assert "a call passes the receiver to a helper"
|
||||
(emits? "(fn f ((c (closure ((int)) int))) int (return (c 1)))"
|
||||
"ƛint_int_call(c, 1)"))
|
||||
(test-assert "and the helper is what dereferences it"
|
||||
(emits? "(fn f ((c (closure ((long)) int))) int (return (c 1)))"
|
||||
"ƛc.code(&ƛc.env, ƛa0)"))
|
||||
(test-assert "a subscript receiver is evaluated once"
|
||||
(emits? "(fn f ((t [(closure ((int)) int) 4]) (i int)) int (return ([t (++ i)] 1)))"
|
||||
"ƛint_int_call(t[++i], 1)"))
|
||||
(test-assert "so is a member receiver"
|
||||
(emits? "(struct h ((cb (closure ((int)) int))))
|
||||
(fn f ((s (struct h))) int (return ((. s cb) 1)))"
|
||||
"ƛint_int_call(s.cb, 1)"))
|
||||
(test-assert "a captured name is rebound in the lifted body"
|
||||
(emits? "(fn f ((n int)) (closure ((char)) int) (return (closure ((x char)) int (n) (return n))))"
|
||||
"int n = ƛcaptures->n;"))
|
||||
(test-assert "captures are checked against the environment"
|
||||
(emits? "(fn f ((n int)) (closure ((short)) int) (return (closure ((x short)) int (n) (return n))))"
|
||||
"_Static_assert(sizeof(struct"))
|
||||
;; A closure over nothing has no record to point at, and C has no
|
||||
;; empty struct to declare for it
|
||||
(test-assert "no captures means no capture record"
|
||||
(not (emits? "(fn f () (closure () int) (return (closure () int () (return 7))))"
|
||||
"_captures {")))
|
||||
;; `(name expr)' names a capture and gives what it holds, so the
|
||||
;; expression is evaluated once, where the closure is written
|
||||
(test-assert "a named capture takes its type from the expression"
|
||||
(emits? "(struct p ((x int) (y int)))
|
||||
(fn f ((s (struct p))) (closure () int)
|
||||
(return (closure () int ((sum (+ (. s x) (. s y)))) (return sum))))"
|
||||
"int sum;"))
|
||||
(test-assert "and the constructor is handed the expression"
|
||||
(emits? "(struct p ((x int) (y int)))
|
||||
(fn f ((s (struct p))) (closure () int)
|
||||
(return (closure () int ((sum (+ (. s x) (. s y)))) (return sum))))"
|
||||
"_make(s.x + s.y)"))
|
||||
(test-assert "capturing a pointer is how by-reference is spelled"
|
||||
(emits? "(struct p ((x int)))
|
||||
(fn f ((s (* (struct p)))) (closure () int)
|
||||
(return (closure () int ((q s)) (return (-> q x)))))"
|
||||
"struct p* q;"))
|
||||
;; A bare function is a closure that captures nothing, so it
|
||||
;; converts wherever one is expected -- the pointer goes in the
|
||||
;; environment and one thunk per signature reads it back out
|
||||
(test-assert "a named function in a var initializer"
|
||||
(emits? "(fn g ((n int)) int (return n))
|
||||
(fn f () void (var c (closure ((int)) int) g))"
|
||||
"ƛint_int_fromfn(g)"))
|
||||
(test-assert "a lambda, which is a bare function too"
|
||||
(emits? "(fn f () void (var c (closure ((int)) int)
|
||||
(lambda ((n int)) int (return n))))"
|
||||
"ƛint_int_fromfn(λ0_f)"))
|
||||
(test-assert "an argument, against the parameter that signature wrote"
|
||||
(emits? "(fn g ((n int)) int (return n))
|
||||
(fn h ((c (closure ((int)) int))) int (return (c 1)))
|
||||
(fn f () int (return (h g)))"
|
||||
"h(ƛint_int_fromfn(g))"))
|
||||
(test-assert "a return, against the declared return type"
|
||||
(emits? "(fn g ((n int)) int (return n))
|
||||
(fn f () (closure ((int)) int) (return g))"
|
||||
"return ƛint_int_fromfn(g)"))
|
||||
(test-assert "the thunk reads the pointer out of the environment"
|
||||
(emits? "(fn g ((n float)) int (return 1))
|
||||
(fn f () void (var c (closure ((float)) int) g))"
|
||||
"return ƛcaptures->f(ƛa0)"))
|
||||
;; a signature that does not match is left alone, and C rejects it
|
||||
(test-assert "a function of the wrong signature does not convert"
|
||||
(not (emits? "(fn g ((n float)) int (return 1))
|
||||
(fn f () void (var c (closure ((int)) int) g))"
|
||||
"_fromfn(g)")))
|
||||
;; a closure is lifted into the function it was written in, so
|
||||
;; there has to be one
|
||||
(test-assert "a closure at toplevel is refused"
|
||||
(reports? "(var c (closure () int) (closure () int () (return 1)))"
|
||||
"only be written inside a function"))
|
||||
(test-assert "and so is one in a struct field"
|
||||
(reports? "(struct s ((f (closure () int) (closure () int () (return 1)))))"
|
||||
"only be written inside a function"))
|
||||
;; A block opens a scope, so what it declares ends with it
|
||||
(test-assert "a name shadowed in a block does not escape it"
|
||||
(emits? "(fn mk () (closure () int) (return (closure () int () (return 1))))
|
||||
(fn f () int (var c (closure () int) (mk))
|
||||
(do (var c int 9) (g c))
|
||||
(return (c)))"
|
||||
"ƛvoid_int_call(c)"))
|
||||
(test-assert "and the shadowing declaration is what the block sees"
|
||||
(emits? "(fn mk () (closure () int) (return (closure () int () (return 1))))
|
||||
(fn f () int (var c (closure () int) (mk))
|
||||
(do (var c int 9) (g c))
|
||||
(return (c)))"
|
||||
"g(c)"))
|
||||
;; The receiver is written twice, so a name used as an argument
|
||||
;; must not be mistaken for a call of its own
|
||||
(test-assert "a closure passed as an argument stays a value"
|
||||
(emits? "(fn g ((c (closure ((int)) int))) int (return 0))
|
||||
(fn f ((c (closure ((int)) int))) int (return (g c)))"
|
||||
"g(c)")))
|
||||
"static void λ0_f (void) {}")))
|
||||
|
||||
;; An include target is a path, so none of the identifier mangling
|
||||
;; applies to it: GL/gl.h is a file and GLːgl.h is not.
|
||||
@@ -696,16 +543,16 @@
|
||||
(defmacro (t x) (type-match (type-of x) (float 1) (else 0)))
|
||||
(fn f () int (return (t (g))))"
|
||||
"return 1;"))
|
||||
;; a macro is shown the written spelling, not the generated struct
|
||||
(test-assert "a closure, spelled the way it was written"
|
||||
(emits? "(fn mk () (closure ((int)) int)
|
||||
(return (closure ((b int)) int () (return b))))
|
||||
(defmacro (t x) (type-match (type-of x) ((closure _ _) 1) (else 0)))
|
||||
;; a macro is shown the written spelling, not a generated name
|
||||
(test-assert "a function pointer, spelled the way it was written"
|
||||
(emits? "(fn mk () (fn ((int)) int)
|
||||
(return (lambda ((b int)) int (return b))))
|
||||
(defmacro (t x) (type-match (type-of x) ((fn _ _) 1) (else 0)))
|
||||
(fn f () int (var c _ (mk)) (return (t c)))"
|
||||
"return 1;"))
|
||||
(test-assert "and calling one has the closure's return type"
|
||||
(emits? "(fn mk () (closure ((int)) int)
|
||||
(return (closure ((b int)) int () (return b))))
|
||||
(test-assert "and calling one has the function's return type"
|
||||
(emits? "(fn mk () (fn ((int)) int)
|
||||
(return (lambda ((b int)) int (return b))))
|
||||
(defmacro (t x) (type-match (type-of x) (int 1) (else 0)))
|
||||
(fn f () int (var c _ (mk)) (return (t (c 1))))"
|
||||
"return 1;"))
|
||||
@@ -786,9 +633,6 @@
|
||||
(test-assert "a lambda's as well"
|
||||
(reports? (in-fn "(var l (fn ((int)) int) (lambda ((x _)) int (return x)))")
|
||||
"a function's types are written out"))
|
||||
(test-assert "and a closure's"
|
||||
(reports? (in-fn "(var c (closure ((int)) int) (closure ((x int)) _ () (return x)))")
|
||||
"a function's types are written out"))
|
||||
;; a call was typed by asking the global table what a function of
|
||||
;; that name returns, which a local pointer is not
|
||||
;; a value if had no rule either: it is the join of its values
|
||||
@@ -820,13 +664,6 @@
|
||||
(emits? (string-append "(struct ops ((op (fn ((int)) float))))"
|
||||
"(fn f ((o (struct ops))) void (var r _ ((. o op) 1)))")
|
||||
"float r = o.op(1)"))
|
||||
(test-assert "a closure's call, which goes through a helper nothing typed"
|
||||
(emits? "(fn f ((c (closure ((int)) int))) void (var r _ (c 4)))"
|
||||
"int r = ƛint_int_call(c, 4)"))
|
||||
(test-assert "and one returning a closure is called as one"
|
||||
(emits? (string-append "(fn mk ((k int)) (closure ((int)) int) (return (closure ((b int)) int (k) (return (+ k b)))))"
|
||||
"(fn f () void (var fp (fn ((int)) (closure ((int)) int)) mk) (var r _ ((fp 3) 4)))")
|
||||
"int r = ƛint_int_call(fp(3), 4)"))
|
||||
(test-assert "while a function is the pointer it already is"
|
||||
(emits? "(fn g ((n int)) int (return n)) (fn f () void (var p _ g))"
|
||||
"int (*p)(int) = g"))
|
||||
@@ -879,18 +716,17 @@
|
||||
(emits? (in-fn "(var t [_ 10] #(1 2))") "int t[10] = {1, 2}"))
|
||||
(test-assert "and they have to agree with each other"
|
||||
(reports? (in-fn "(var t [_ 2] #(1 \"s\"))") "type mismatch"))
|
||||
;; a closure type reaches the solver as the struct that stands for
|
||||
;; it, which is the spelling `parse-type' knows
|
||||
(test-assert "a closure, from the signature that produced it"
|
||||
(emits? "(fn mk () (closure ((int)) int)
|
||||
(return (closure ((b int)) int () (return b))))
|
||||
;; a function pointer type reaches the solver as itself
|
||||
(test-assert "a lambda, from the signature that produced it"
|
||||
(emits? "(fn mk () (fn ((int)) int)
|
||||
(return (lambda ((b int)) int (return b))))
|
||||
(fn f () void (var c _ (mk)))"
|
||||
"struct ƛint_int c = mk()"))
|
||||
"int (*c)(int) = mk()"))
|
||||
(test-assert "and it is callable once inferred"
|
||||
(emits? "(fn mk () (closure ((int)) int)
|
||||
(return (closure ((b int)) int () (return b))))
|
||||
(emits? "(fn mk () (fn ((int)) int)
|
||||
(return (lambda ((b int)) int (return b))))
|
||||
(fn f () int (var c _ (mk)) (return (c 1)))"
|
||||
"ƛint_int_call(c, 1)"))
|
||||
"return c(1)"))
|
||||
;; C's usual arithmetic conversions, far enough to answer `_'
|
||||
(test-assert "floating beats integral"
|
||||
(emits? (in-fn "(var d double 1.0) (var x _ (+ a d))") "double x = a + d"))
|
||||
@@ -964,16 +800,16 @@
|
||||
;; without `$' a list is one form, so a head that is itself a form
|
||||
;; stays a call rather than becoming two statements
|
||||
(test-assert "a computed callee stays one form"
|
||||
(emits? "(fn mk () (closure ((int)) int)
|
||||
(return (closure ((b int)) int () (return b))))
|
||||
(emits? "(fn mk () (fn ((int)) int)
|
||||
(return (lambda ((b int)) int (return b))))
|
||||
(defmacro (apply-it x) `((mk) ,x))
|
||||
(fn f () int (return (apply-it 5)))"
|
||||
"ƛint_int_call(mk(), 5)"))
|
||||
"mk()(5)"))
|
||||
;; spliced, it would have become two forms in the `return' -- the
|
||||
;; comma operator, and the wrong answer
|
||||
(test-assert "rather than two forms in its context"
|
||||
(not (emits? "(fn mk () (closure ((int)) int)
|
||||
(return (closure ((b int)) int () (return b))))
|
||||
(not (emits? "(fn mk () (fn ((int)) int)
|
||||
(return (lambda ((b int)) int (return b))))
|
||||
(defmacro (apply-it x) `((mk) ,x))
|
||||
(fn f () int (return (apply-it 5)))"
|
||||
"return mk(), 5"))))
|
||||
|
||||
@@ -269,12 +269,12 @@ ahead of its pub forms."
|
||||
(reports? "(sum box ((inner ((x [box 2])))))" "a sum holds itself through a pointer"))
|
||||
(test-assert "though it can point at itself"
|
||||
(emits? "(sum box ((inner ((x (* box))))))" "box * x;"))
|
||||
(test-assert "a function argument becomes the closure its field holds"
|
||||
(test-assert "a function fills a function-pointer field"
|
||||
(emits? (string-append
|
||||
"(fn g ((n int)) int (return n))"
|
||||
"(sum t ((a ((k (closure ((int)) int))))))"
|
||||
"(sum t ((a ((k (fn ((int)) int))))))"
|
||||
"(fn f () t (return (t/a g)))")
|
||||
"ƛint_int_fromfn(g)"))
|
||||
"(struct tːa){g}"))
|
||||
(test-assert "a tag-only sum has no union at all"
|
||||
(not (emits? "(sum light ((red ()) (green ())))" "union")))
|
||||
(test-assert "a sum is declared once"
|
||||
@@ -308,18 +308,18 @@ ahead of its pub forms."
|
||||
").h")))
|
||||
(test-assert "a scrutinee that is not a path is read once"
|
||||
(emits? (sum-src "(match (mk) ((circle (r)) (= out r)) (else (= out 0.0)))")
|
||||
"shape ƛm0 = mk()"))
|
||||
"shape λm0 = mk()"))
|
||||
(test-assert "a path scrutinee is not copied"
|
||||
(not (emits? (sum-src "(match s ((circle (r)) (= out r)) (else (= out 0.0)))")
|
||||
"ƛm0")))
|
||||
"λm0")))
|
||||
(test-assert "unless a binder is named like a part of it"
|
||||
(emits? (sum-src "(match s ((circle (s)) (= out s)) (else (= out 0.0)))")
|
||||
"shape ƛm0 = s"))
|
||||
"shape λm0 = s"))
|
||||
(test-assert "a macro scrutinee is typed by what it expands to"
|
||||
(emits? (string-append "(defmacro (the a) a)"
|
||||
(sum-src "(match (the s) ((circle (r)) (= out r))"
|
||||
" (else (= out 0.0)))"))
|
||||
"shape ƛm0 = s"))
|
||||
"shape λm0 = s"))
|
||||
(test-assert "a typedef of a sum is matched as the sum"
|
||||
(emits? (string-append
|
||||
"(sum shape ((circle ((r int))) (empty ())))"
|
||||
@@ -392,10 +392,10 @@ ahead of its pub forms."
|
||||
(emits? (sum-src "(switch 1 (case 1 (match s ((circle (r)) (= out r))"
|
||||
" (else (= out 0.0)))))")
|
||||
"s.tag == shapeǂcircle"))
|
||||
(test-assert "and so is a closure's, wherever the closure stands"
|
||||
(emits? (sum-src "(var c (closure () int) (closure () int ((t s))"
|
||||
" (match t ((circle (r)) (return 1)) (else (return 0)))))")
|
||||
"t.tag == shapeǂcircle"))
|
||||
(test-assert "and so is a lambda's, wherever the lambda stands"
|
||||
(emits? (sum-src "(var c (fn ((shape)) int) (lambda ((v shape)) int"
|
||||
" (match v ((circle (r)) (return 1)) (else (return 0)))))")
|
||||
"v.tag == shapeǂcircle"))
|
||||
;; a pointer to one was refused, where `.' reads through one
|
||||
;; a variant with fields is a member of the sum's union, which C
|
||||
;; would not take named `int'
|
||||
|
||||
@@ -10,11 +10,8 @@
|
||||
;;;
|
||||
;;; It also checks what only a second translation unit can check: that an
|
||||
;;; imported type reaches the type database, by expanding a macro that
|
||||
;;; reads the imported struct's fields; and that a closure type crossing
|
||||
;;; the boundary works both ways -- one built in the module and called
|
||||
;;; here, one built here and called there, through a code pointer that is
|
||||
;;; static in the other object. And that a record can cross as a handle
|
||||
;;; alone, its fields left behind and its methods still found.
|
||||
;;; reads the imported struct's fields; and that a record can cross as a
|
||||
;;; handle alone, its fields left behind and its methods still found.
|
||||
;;;
|
||||
;;; The public forms carry comments in their headers, which the reduction
|
||||
;;; to a prototype and to an extern both have to look past. And a type the
|
||||
@@ -152,7 +149,6 @@
|
||||
"text times "
|
||||
"text times "
|
||||
"mood 1"
|
||||
"closure 15 21 201"
|
||||
"vec2 7 7"
|
||||
"tally 2"
|
||||
"board 42"
|
||||
|
||||
@@ -27,13 +27,6 @@
|
||||
(var m (enum mood) grumpy)
|
||||
(printf "mood %d\n" m)
|
||||
|
||||
(var add-10 (closure ((int)) int) (make-adder 10))
|
||||
(printf "closure %d %d" (add-10 5) (apply-twice add-10 1))
|
||||
(var base int 100)
|
||||
(var here (closure ((int)) int)
|
||||
(closure ((x int)) int (base) (return (+ base x))))
|
||||
(printf " %d\n" (apply-twice here 1))
|
||||
|
||||
;; the bare record name, then the method both ways round
|
||||
(var v vec2 #(vec2 : 3 4))
|
||||
(printf "vec2 %d %d\n" (.manhattan v) (vec2/manhattan (& v)))
|
||||
|
||||
@@ -62,19 +62,6 @@
|
||||
(pub fn ledger-total ((l (* const ledger))) int
|
||||
(return (. l total)))
|
||||
|
||||
;;; A closure type crossing the boundary. Both units generate the
|
||||
;;; struct for this signature independently, so they have to agree on
|
||||
;;; its tag and its layout, or the value is passed wrong and nothing
|
||||
;;; says so.
|
||||
(pub fn make-adder ((n int)) (closure ((int)) int)
|
||||
(return (closure ((b int)) int (n)
|
||||
(return (+ n b)))))
|
||||
|
||||
;;; The other direction: a closure built by the importer, whose code
|
||||
;;; pointer is static in *its* object, called from here
|
||||
(pub fn apply-twice ((f (closure ((int)) int)) (x int)) int
|
||||
(return (f (f x))))
|
||||
|
||||
;;; A record and an ops block crossing the boundary. The record has to
|
||||
;;; arrive as the struct *and* its typedef, or the bare name does not
|
||||
;;; work there; the ops block has to arrive as a block rather than as
|
||||
|
||||
@@ -9,10 +9,10 @@
|
||||
;;; one that reaches past it -- a GNU statement expression, a C23
|
||||
;;; keyword -- fails here rather than on a user's compiler.
|
||||
;;;
|
||||
;;; A capturing closure is the one that leans on C11 itself, through
|
||||
;;; max_align_t in the environment and `_Static_assert' on its size;
|
||||
;;; the record, the method and the match are here for what they emit,
|
||||
;;; and the globals for initializers that have to be constants.
|
||||
;;; A lifted lambda is the one that leans on C11 itself, through
|
||||
;;; `_Static_assert'; the record, the method and the match are here for
|
||||
;;; what they emit, and the globals for initializers that have to be
|
||||
;;; constants.
|
||||
;;;
|
||||
;;; It is also the one mode where C still reads trigraphs, which Sex has
|
||||
;;; none of: a string says what was written, `??/' before its closing
|
||||
@@ -44,9 +44,8 @@
|
||||
|
||||
(pub fn main () int
|
||||
(var p point #(point : 6 7))
|
||||
(var k int 1)
|
||||
(var add-k (closure ((int)) int)
|
||||
(closure ((v int)) int (k) (return (+ v k))))
|
||||
(var add-k (fn ((int)) int)
|
||||
(lambda ((v int)) int (return (+ v 1))))
|
||||
(printf "c11: %d %d %d %d %d %d %d %d %lld\n"
|
||||
(.area p) (add-k (.area p)) (+ (sides (shape/square 1)) (sides (shape/dot)) 2)
|
||||
(sides unit) (.area corner) (.area [pair 1]) [primes 2] (. origin y) least)
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
(input)
|
||||
(output "one argument of two words: 7"
|
||||
"two arguments of one: 7"
|
||||
"unsigned, one argument: 9"
|
||||
"unsigned, two arguments: 3"
|
||||
"captured global: 12"
|
||||
"captured function: 8"
|
||||
"curried: 42 42"
|
||||
"taking one: 21")
|
||||
(return 0)
|
||||
|
||||
;;; A closure's struct is named after its signature, so that two
|
||||
;;; translation units agree on it without sharing a header. The name is
|
||||
;;; built by flattening the argument list, and flattening loses where one
|
||||
;;; argument ends and the next begins: `((long long))' and
|
||||
;;; `((long) (long))' are different signatures that used to mangle alike,
|
||||
;;; and the second quietly reused the first one's struct.
|
||||
;;;
|
||||
;;; 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)
|
||||
|
||||
(var scale int 3)
|
||||
|
||||
(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))))
|
||||
(printf "one argument of two words: %d\n" (one-wide 7))
|
||||
|
||||
(var two-longs (closure ((long) (long)) int)
|
||||
(closure ((a long) (b long)) int () (return (cast (+ a b) int))))
|
||||
(printf "two arguments of one: %d\n" (two-longs 3 4))
|
||||
|
||||
(var one-unsigned (closure ((unsigned int)) int)
|
||||
(closure ((a (unsigned int))) int () (return (cast a int))))
|
||||
(printf "unsigned, one argument: %d\n" (one-unsigned 9))
|
||||
|
||||
(var two-unsigned (closure ((unsigned) (int)) int)
|
||||
(closure ((a unsigned) (b int)) int () (return (+ (cast a int) b))))
|
||||
(printf "unsigned, two arguments: %d\n" (two-unsigned 1 2))
|
||||
|
||||
;; a capture names what it borrows, and the name need not be a local
|
||||
(var scaled (closure ((int)) int)
|
||||
(closure ((x int)) int (scale) (return (* x scale))))
|
||||
(printf "captured global: %d\n" (scaled 4))
|
||||
|
||||
(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))
|
||||
@@ -1,149 +0,0 @@
|
||||
(input)
|
||||
(output "Adders: 15 25"
|
||||
"Two captures: 47"
|
||||
"No captures: 7"
|
||||
"Through a parameter: 110"
|
||||
"From an array: 1 2 3"
|
||||
"Index evaluated once: 21 i 1"
|
||||
"Through a struct member: 8"
|
||||
"Shadowed in a block: 9 then 15"
|
||||
"Nested: 33"
|
||||
"Named capture: 7"
|
||||
"Captured pointer: 11 then 12"
|
||||
"From a bare fn: 20 42 7"
|
||||
"Captured array: 6 3, still 6"
|
||||
"Captured const: 6"
|
||||
"Unnamed parameter: 2"
|
||||
"Empty bodies: called")
|
||||
(return 0)
|
||||
|
||||
;;; A closure is a code pointer beside its captures, so what this
|
||||
;;; checks is that the captures survive the lifting -- that two
|
||||
;;; closures of one shape keep their own environments, that a closure
|
||||
;;; outlives the call that built it, and that calling one through a
|
||||
;;; parameter, an array element or a struct member resolves the same
|
||||
;;; way as through a local.
|
||||
;;;
|
||||
;;; A receiver is also an ordinary expression: `[table (++ i)]' has to
|
||||
;;; evaluate its index exactly once, the way it would for an array of
|
||||
;;; function pointers.
|
||||
|
||||
(include stdio.h)
|
||||
|
||||
(fn double-it ((n int)) int
|
||||
(return (* n 2)))
|
||||
|
||||
;;; a bare function is a closure that captures nothing, so it converts
|
||||
;;; wherever one is expected -- here a declared return type
|
||||
(fn as-closure () (closure ((int)) int)
|
||||
(return double-it))
|
||||
|
||||
(fn make-adder ((n int)) (closure ((int)) int)
|
||||
(return (closure ((b int)) int (n)
|
||||
(return (+ n b)))))
|
||||
|
||||
(fn make-affine ((k int) (b int)) (closure ((int)) int)
|
||||
(return (closure ((x int)) int (k b)
|
||||
(return (+ (* k x) b)))))
|
||||
|
||||
(fn make-const-7 () (closure () int)
|
||||
(return (closure () int ()
|
||||
(return 7))))
|
||||
|
||||
;;; A closure arriving as a parameter: its type is written, so the call
|
||||
;;; resolves without knowing where it came from
|
||||
(fn apply-twice ((f (closure ((int)) int)) (x int)) int
|
||||
(return (f (f x))))
|
||||
|
||||
(struct handlers ((on-tick (closure ((int)) int))))
|
||||
|
||||
(pub fn main () int
|
||||
(var add-10 (closure ((int)) int) (make-adder 10))
|
||||
(var add-20 (closure ((int)) int) (make-adder 20))
|
||||
(printf "Adders: %d %d\n" (add-10 5) (add-20 5))
|
||||
|
||||
(var affine (closure ((int)) int) (make-affine 5 2))
|
||||
(printf "Two captures: %d\n" (affine 9))
|
||||
|
||||
(var seven (closure () int) (make-const-7))
|
||||
(printf "No captures: %d\n" (seven))
|
||||
|
||||
(printf "Through a parameter: %d\n" (apply-twice (make-adder 50) 10))
|
||||
|
||||
(var table [(closure ((int)) int) 3])
|
||||
(var i int 0)
|
||||
(for (= i 0) (< i 3) (++ i)
|
||||
(= [table i] (make-adder i)))
|
||||
(printf "From an array: %d %d %d\n"
|
||||
([table 0] 1) ([table 1] 1) ([table 2] 1))
|
||||
|
||||
;; the index must be evaluated once, so `i' ends at 1 and not 2 --
|
||||
;; read in a separate statement, since reading and bumping it in one
|
||||
;; printf would be unsequenced whatever the closure did
|
||||
(= i 0)
|
||||
(var once int ([table (++ i)] 20))
|
||||
(printf "Index evaluated once: %d i %d\n" once i)
|
||||
|
||||
(var h (struct handlers) #((struct handlers) : .on-tick (make-adder 5)))
|
||||
(printf "Through a struct member: %d\n" ((. h on-tick) 3))
|
||||
|
||||
;; a block opens a scope: the inner `add-10' ends with it, and the
|
||||
;; call after it is the closure again
|
||||
(do (var add-10 int 9)
|
||||
(printf "Shadowed in a block: %d then " add-10))
|
||||
(printf "%d\n" (add-10 5))
|
||||
|
||||
;; a closure built inside a closure, capturing that one's capture
|
||||
(var outer (closure ((int)) int)
|
||||
(closure ((x int)) int ()
|
||||
(var inner (closure ((int)) int) (make-adder x))
|
||||
(return (inner 3))))
|
||||
(printf "Nested: %d\n" (outer 30))
|
||||
|
||||
;; a capture can name what it holds rather than borrow a variable's
|
||||
;; name, and the expression is evaluated where the closure is written
|
||||
(var pt (struct handlers))
|
||||
(var sum-once (closure () int)
|
||||
(closure () int ((sum (+ 3 4))) (return sum)))
|
||||
(printf "Named capture: %d\n" (sum-once))
|
||||
|
||||
;; capturing a pointer is how by-reference is spelled; the caller owns
|
||||
;; what it points at
|
||||
(var counter int 11)
|
||||
(var peek (closure () int)
|
||||
(closure () int ((at (& counter))) (return (* at))))
|
||||
(printf "Captured pointer: %d then " (peek))
|
||||
(++ counter)
|
||||
(printf "%d\n" (peek))
|
||||
|
||||
;; ...and in an initializer, as an argument, and as a return
|
||||
(var from-fn (closure ((int)) int) double-it)
|
||||
(printf "From a bare fn: %d %d %d\n"
|
||||
(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))
|
||||
|
||||
(var second (closure ((int) (int)) int) (closure ((int) (b int)) int () (return b)))
|
||||
(printf "Unnamed parameter: %d\n" (second 1 2))
|
||||
|
||||
(var nothing (closure () void) (closure () void ()))
|
||||
(var nothing-either (fn () void) (lambda () void))
|
||||
(nothing)
|
||||
(nothing-either)
|
||||
(printf "Empty bodies: called\n")
|
||||
(return 0))
|
||||
@@ -1,61 +0,0 @@
|
||||
(input)
|
||||
(output "direct: 120"
|
||||
"fac: 120 3628800"
|
||||
"fib: 55 6765"
|
||||
"applied on the spot: 120")
|
||||
(return 0)
|
||||
|
||||
;;; A fixed point built out of closures, which is the hardest thing to
|
||||
;;; ask of them: recursion with no recursive function anywhere, only
|
||||
;;; self-application.
|
||||
;;;
|
||||
;;; Self-application needs `x x' and so a recursive type, which is
|
||||
;;; spelled here by routing it through a named struct whose field is a
|
||||
;;; closure whose own signature mentions that struct. The generated
|
||||
;;; closure struct is written before `struct rec' is, so this only
|
||||
;;; compiles because a forward declaration is emitted ahead of both.
|
||||
;;;
|
||||
;;; Note what `fix' captures: a *pointer* to the knot, not the knot. A
|
||||
;;; closure is a code pointer beside N bytes of environment, so
|
||||
;;; capturing one by value would need N >= 8 + N. No budget makes that
|
||||
;;; true, and the static assertion says so rather than letting it
|
||||
;;; corrupt anything.
|
||||
|
||||
(include stdio.h)
|
||||
|
||||
(struct rec ((f (closure (((* (struct rec))) (int)) int))))
|
||||
|
||||
;;; Takes a step that expects itself, returns an ordinary closure with
|
||||
;;; the self-application hidden inside
|
||||
(fn fix ((step (* (struct rec)))) (closure ((int)) int)
|
||||
(return (closure ((n int)) int (step)
|
||||
(return ((. step f) step n)))))
|
||||
|
||||
(pub fn main () int
|
||||
(var fac-knot (struct rec))
|
||||
(= (. fac-knot f)
|
||||
(closure ((self (* (struct rec))) (n int)) int ()
|
||||
(if (<= n 1) (return 1))
|
||||
(return (* n ((. self f) self (- n 1))))))
|
||||
|
||||
;; the knot applied to itself directly, without fix
|
||||
(printf "direct: %d\n" ((. fac-knot f) (& fac-knot) 5))
|
||||
|
||||
(var fib-knot (struct rec))
|
||||
(= (. fib-knot f)
|
||||
(closure ((self (* (struct rec))) (n int)) int ()
|
||||
(if (< n 2) (return n))
|
||||
(return (+ ((. self f) self (- n 1))
|
||||
((. self f) self (- n 2))))))
|
||||
|
||||
;; one combinator, two different recursions
|
||||
(var fac (closure ((int)) int) (fix (& fac-knot)))
|
||||
(var fib (closure ((int)) int) (fix (& fib-knot)))
|
||||
(printf "fac: %d %d\n" (fac 5) (fac 10))
|
||||
(printf "fib: %d %d\n" (fib 10) (fib 20))
|
||||
|
||||
;; the combinator's result invoked where it is returned, with no
|
||||
;; intervening `var' -- the receiver's type is the return type of the
|
||||
;; signature it came from, which is what the name table records
|
||||
(printf "applied on the spot: %d\n" ((fix (& fac-knot)) 5))
|
||||
(return 0))
|
||||
@@ -8,8 +8,8 @@
|
||||
"0 1 4 9 "
|
||||
"4"
|
||||
"42"
|
||||
"<closure of 0: 10>"
|
||||
"42"
|
||||
"<fn of 0: 10>"
|
||||
"32"
|
||||
"2 1"
|
||||
"Hello from Sex!")
|
||||
(return 0)
|
||||
@@ -47,11 +47,11 @@
|
||||
(= (. m y) (/ (+ (. a y) (. b y)) 2))
|
||||
(return m))
|
||||
|
||||
;;; The closure's type is written once, in the signature; `_' reads it
|
||||
;;; from there at every use.
|
||||
(fn make-adder ((n int)) (closure ((int)) int)
|
||||
(return (closure ((b int)) int (n)
|
||||
(return (+ n b)))))
|
||||
;;; A lambda's type is written once, in the signature; `_' reads it
|
||||
;;; from there wherever the function is called.
|
||||
(fn make-adder () (fn ((int)) int)
|
||||
(return (lambda ((b int)) int
|
||||
(return (+ b 10)))))
|
||||
|
||||
;;; A macro that asks what it was handed.
|
||||
;;;
|
||||
@@ -68,9 +68,9 @@
|
||||
;; NOTE: ,x twice -- a macro that duplicates its argument still has
|
||||
;; to think about evaluating it twice. Inference does not fix that.
|
||||
((struct point) `(printf "(%d, %d)\n" (. ,x x) (. ,x y)))
|
||||
;; a closure is a type like any other, so it dispatches like one --
|
||||
;; and `_' saves a clause per signature
|
||||
((closure _ int) `(printf "<closure of 0: %d>\n" (,x 0)))
|
||||
;; a function type is a type like any other, so it dispatches like
|
||||
;; one -- and `_' saves a clause per signature
|
||||
((fn _ int) `(printf "<fn of 0: %d>\n" (,x 0)))
|
||||
(else (error "print: don't know how to print" (type-of x)))))
|
||||
|
||||
;;; The temporary's type is the thing the macro could not write down
|
||||
@@ -136,14 +136,14 @@
|
||||
(var squares [_ 4] #(0 1 4 9))
|
||||
(print [squares 2])
|
||||
|
||||
;; A closure's type comes from the signature that produced it, and
|
||||
;; calling one needs that type and nothing else.
|
||||
(var add-10 _ (make-adder 10))
|
||||
;; A function pointer's type comes from the signature that produced
|
||||
;; it, and calling one needs that type and nothing else.
|
||||
(var add-10 _ (make-adder))
|
||||
(print (add-10 32))
|
||||
(print add-10)
|
||||
|
||||
;; ...including where it is returned, with no name in between.
|
||||
(print ((make-adder 20) 22))
|
||||
(print ((make-adder) 22))
|
||||
|
||||
;; A macro writing a declaration it could not have written before.
|
||||
(var a _ 1)
|
||||
@@ -157,12 +157,12 @@
|
||||
;;; Open questions this draft raises, to settle before Layer 2 ships:
|
||||
;;;
|
||||
;;; 1. SETTLED. `type-match' took `_' on the pattern side, so
|
||||
;;; `(closure _ int)' above is one clause rather than one per
|
||||
;;; signature, and `(* _)' and `[_ _]' say "any pointer" and "any
|
||||
;;; array". A `_' written last takes the rest, since a type's words
|
||||
;;; are spread and not nested: `(* const char)' is three elements.
|
||||
;;; Nothing destructures -- a macro body is Scheme and a type is a
|
||||
;;; list, so `(caddr (type-of x))' reads an array's length.
|
||||
;;; `(fn _ int)' above is one clause rather than one per signature,
|
||||
;;; and `(* _)' and `[_ _]' say "any pointer" and "any array". A `_'
|
||||
;;; written last takes the rest, since a type's words are spread and
|
||||
;;; not nested: `(* const char)' is three elements. Nothing
|
||||
;;; destructures -- a macro body is Scheme and a type is a list, so
|
||||
;;; `(caddr (type-of x))' reads an array's length.
|
||||
;;;
|
||||
;;; 2. SETTLED, allowed. `[_ 4]' against `#(0 1 4 9)' unifies each
|
||||
;;; element with the hole, so the element type comes from the
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
"decayed: 2 2 there"
|
||||
"unsigned wins either way: 4294967295 4294967295"
|
||||
"toplevel: 1 2.5 hi 12"
|
||||
"closure in place: 5"
|
||||
"a type is not a call: 7"
|
||||
"radix literals: 224 5 128"
|
||||
"a double reaches C whole: 1"
|
||||
@@ -35,9 +34,6 @@
|
||||
(var s _ "hi")
|
||||
(var a _ (+ 3 (* 3 3)))
|
||||
|
||||
(fn make-adder ((k int)) (closure ((int)) int)
|
||||
(return (closure ((b int)) int (k) (return (+ k b)))))
|
||||
|
||||
(pub fn main () int
|
||||
(var x int 6)
|
||||
(var y int 3)
|
||||
@@ -80,14 +76,9 @@
|
||||
|
||||
(printf "toplevel: %d %g %s %d\n" n d s a)
|
||||
|
||||
;; a closure literal is its own type, so it can be called where it is
|
||||
;; written, the way a lambda already could
|
||||
(printf "closure in place: %d\n"
|
||||
((closure ((v int)) int () (return v)) 5))
|
||||
|
||||
;; a `fn' type's parameter list looks exactly like a call; with a
|
||||
;; closure named `f' in scope it used to be read as one
|
||||
(var f (closure ((int)) int) (make-adder 1))
|
||||
;; function named `f' in scope it used to be read as one
|
||||
(var f (fn ((int)) int) (lambda ((v int)) int (return (+ v 1))))
|
||||
(var fp (fn ((f int) (g int)) int) area)
|
||||
(printf "a type is not a call: %d\n" (f 6))
|
||||
|
||||
|
||||
@@ -189,8 +189,8 @@
|
||||
'yes (type-match '(struct point) ((struct _) 'yes) (else 'no)))
|
||||
(test "and the keyword still has to agree"
|
||||
'no (type-match '(union point) ((struct _) 'yes) (else 'no)))
|
||||
(test "a closure of any signature"
|
||||
'yes (type-match '(closure ((float)) int) ((closure _ _) 'yes) (else 'no)))
|
||||
(test "a function of any signature"
|
||||
'yes (type-match '(fn ((float)) int) ((fn _ _) 'yes) (else 'no)))
|
||||
(test "an exact pattern is still exact"
|
||||
'no (type-match '(* int) ((* const char) 'yes) (else 'no)))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user