An importer is given the pub forms and nothing else, so a pub form naming a private type, or one from an import without pub, compiled in its module and failed in every importer with `unknown type name'. The pub forms are checked as the module itself is compiled. A record, struct, union or sum behind a pointer needs only declaring, so its bodyless declaration goes out ahead of them -- the opaque handle that (pub record T) already gave by hand. Held by value it needs its body, and a typedef or an enum has no declaration short of its definition: those are errors at the pub form, saying what would do instead. A name Sex has no declaration for is left to C. An importer may now be handed a declaration after the definition it declares, so a bodyless aggregate no longer replaces known fields. Fixes #57, #58.
60 lines
1.8 KiB
Scheme
60 lines
1.8 KiB
Scheme
;;; Uses the greet module. Build both, then link them:
|
|
;;;
|
|
;;; ./sexc example/greet.sex -c -o greet.o
|
|
;;; ./sexc example/greet-app.sex -o greet-app -- greet.o
|
|
;;;
|
|
;;; `(import greet)' pastes greet's public declarations here: `greet'
|
|
;;; as a prototype and `greet-count' as an extern. Both keep external
|
|
;;; linkage, so they refer to the one definition in greet.o rather than
|
|
;;; to private copies.
|
|
;;;
|
|
;;; Also check that import populates type-database, by means of
|
|
;;; describe-fields macro, which should work on imported type.
|
|
|
|
(include stdio.h)
|
|
|
|
(import greet)
|
|
|
|
(pub fn main () int
|
|
(greet "world")
|
|
(greet "sex")
|
|
(printf "%d greetings\n" greet-count)
|
|
(describe-fields greeting)
|
|
(printf "\n")
|
|
;; ...and through the imported typedef for it
|
|
(describe-fields greeting-t)
|
|
(printf "\n")
|
|
(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)))
|
|
|
|
(.show-line v)
|
|
|
|
;; only the handle crossed: (. t n) here is an incomplete type
|
|
(var t (* tally) (tally/make))
|
|
(.bump t)
|
|
(.bump t)
|
|
(printf "tally %d\n" (.count t))
|
|
(.drop t)
|
|
|
|
;; neither cell nor ledger is visible here, only declared
|
|
(var b board (make-board))
|
|
(printf "board %d\n" (board-total (& b)))
|
|
(printf "ledger %d\n" (ledger-total (greet-ledger)))
|
|
|
|
(var c colour (colour/rgb 1 2 3))
|
|
(match c
|
|
((rgb (r g b)) (printf "colour %d%d%d\n" r g b))
|
|
((none ()) (printf "colour none\n")))
|
|
(return 0))
|