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.
51 lines
1.5 KiB
Scheme
51 lines
1.5 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)
|
|
|
|
;; 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)))
|
|
|
|
;; 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))
|