forked from alex-eg/sex
`.' now writes whichever of C's member operators the type calls for, so nothing needs the arrow spelled out and the name is free for the macro Clojure spells the same way: (-> 5 double-it (add 3)) reads in the order it happens rather than inside out. It has no business in the prelude, which every unit pays for whether or not it threads anything, so it opens the standard library instead and is imported like anything else. The compiler finds lib/ by asking the binary where it is -- beside sexc in a work tree, under ../share/sex once installed -- and looks there last, so a module of one's own by that name still wins. The C arrow becomes `%->', and that rename is what makes the rest possible. The closure lifter writes arrows of its own for the capture record, those forms go back through walk-statement, and a macro named `->' threads them: (-> ƛcaptures n) turns into a call to n, with nothing saying so until the C compiler. fmt-c rendered a bare `->' as the arrow, which left the one spelling that cannot be right -- threading with no import -- looking like the operator it stopped being. It goes through c-apply now, the way any other head nothing knows does.
85 lines
3.2 KiB
Scheme
85 lines
3.2 KiB
Scheme
;;; A module. Everything marked `pub' forms its public interface;
|
||
;;; everything else is private to this file.
|
||
;;;
|
||
;;; Importing a module does not link it: it pastes the declarations, so
|
||
;;; the compiled object still has to be handed to the C compiler. See
|
||
;;; greet-app.sex.
|
||
|
||
(pub include stdio.h)
|
||
|
||
(pub var greet-count ;; a comment in the header of a public form is
|
||
;; not part of it: what the importer is given has
|
||
;; to be `extern int greet-count', not a form
|
||
;; counted off by one
|
||
int 0)
|
||
|
||
(pub struct greeting
|
||
"A greeting to print."
|
||
((text (* const char)) (times int)))
|
||
|
||
(pub enum mood (cheerful grumpy))
|
||
|
||
(pub typedef greeting-t (struct greeting))
|
||
|
||
;;; Compile-time reflection across the module boundary: both the macro
|
||
;;; and the struct it asks about are exported, and the importing unit
|
||
;;; has to know the struct's fields to expand this.
|
||
(pub defmacro (describe-fields type)
|
||
`(do ,@(map-fields type
|
||
(lambda (name field-type)
|
||
`(printf "%s " ,(symbol->string name))))))
|
||
|
||
(pub fn greet ;; ...and here the prototype would lose its return type
|
||
((name (* const char))) void
|
||
"Print a greeting for NAME."
|
||
(++ greet-count)
|
||
(printf "hello, %s\n" name))
|
||
|
||
;;; 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
|
||
;;; loose prototypes, or the importer learns that vec2ːmanhattan exists
|
||
;;; without learning that vec2 has a `manhattan' -- and (. v manhattan)
|
||
;;; needs the second thing.
|
||
(pub record vec2 ((x int) (y int)))
|
||
|
||
(ops vec2
|
||
(pub fn manhattan ((self (* const vec2))) int
|
||
(return (+ (. self x) (. self y)))))
|
||
|
||
;;; A pub sum is pasted whole, so the importer builds its own copy of
|
||
;;; the declarations -- and of the constructors, which is why those are
|
||
;;; static: two units defining colourːrgb would not link.
|
||
(pub sum colour ((rgb ((r int) (g int) (b int))) (none ())))
|
||
|
||
;;; A spec and a realization over the boundary. The written method
|
||
;;; travels as a prototype and keeps external linkage; the default
|
||
;;; does not travel at all -- the importer synthesizes its own static
|
||
;;; copy from the spec, which is why a default is static even here.
|
||
(pub spec (printable T)
|
||
(fn show ((self (* const T))) void)
|
||
(fn show-line ((self (* const T))) void
|
||
(.show self)
|
||
(printf "\n")))
|
||
|
||
(pub realize (printable vec2)
|
||
(fn show ((self (* const vec2))) void
|
||
(printf "vec2(%d %d)" (. self x) (. self y))))
|
||
|
||
;;; Not `pub': invisible to importers, and static in the generated C.
|
||
(fn unused-helper () void
|
||
(printf "private\n"))
|