They were too much surface for a language this small: a nominal contract layer, per-type default synthesis and a pub interface of their own. Type introspection and macros are the intended replacement, so the storage, the forms and the tests that pin them go; the ops method registry they leaned on stays. The implementation is kept at tag 0.0.1.
116 lines
3.8 KiB
Scheme
116 lines
3.8 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 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; 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"))
|