forked from alex-eg/sex
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.
103 lines
3.3 KiB
Scheme
103 lines
3.3 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)
|
||
(include stdlib.h)
|
||
|
||
(import ledger)
|
||
|
||
(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))))))
|
||
|
||
(var greetings ledger #(ledger : 0))
|
||
|
||
(pub fn greet ;; ...and here the prototype would lose its return type
|
||
((name (* const char))) void
|
||
"Print a greeting for NAME."
|
||
(++ greet-count)
|
||
(ledger-add (& greetings) 1)
|
||
(printf "hello, %s\n" name))
|
||
|
||
;;; Types the importer cannot see, crossing behind a pointer. `cell' is
|
||
;;; private and `ledger' was imported without `pub', and the importer
|
||
;;; is handed a declaration of each and nothing else.
|
||
(record cell ((v int)))
|
||
|
||
(var the-cell cell #(cell : 40))
|
||
|
||
(pub record board ((first (* cell)) (size int)))
|
||
|
||
(pub fn make-board () board
|
||
(return #(board : (& the-cell) 2)))
|
||
|
||
(pub fn board-total ((b (* const board))) int
|
||
(return (+ (. b first v) (. b size))))
|
||
|
||
(pub fn greet-ledger () (* ledger)
|
||
(return (& greetings)))
|
||
|
||
(pub fn ledger-total ((l (* const ledger))) int
|
||
(return (. l total)))
|
||
|
||
;;; 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; a constructor is written in place where it is
|
||
;;; called, so there is no function for two units to both define.
|
||
(pub sum colour ((rgb ((r int) (g int) (b int))) (none ())))
|
||
|
||
;;; Not `pub': invisible to importers, and static in the generated C.
|
||
(fn unused-helper () void
|
||
(printf "private\n"))
|