;;; 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) (include stdlib.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))))) ;;; An opaque record: the handle is public and the fields are not, so ;;; the importer holds a (* tally) and reaches it only through methods. (pub record tally) (record tally ((n int))) (ops tally (pub fn make () (* tally) (var t (* tally) (malloc (sizeof tally))) (= (. t n) 0) (return t)) (pub fn bump ((self (* tally))) void (++ (. self n))) (pub fn count ((self (* const tally))) int (return (. self n))) (pub fn drop ((self (* tally))) void (free self))) ;;; 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"))