forked from alex-eg/sex
`.' now writes whichever of C's member operators the type calls for, so nothing needs the arrow spelled out and the name is free for the macro Clojure spells the same way: (-> 5 double-it (add 3)) reads in the order it happens rather than inside out. It has no business in the prelude, which every unit pays for whether or not it threads anything, so it opens the standard library instead and is imported like anything else. The compiler finds lib/ by asking the binary where it is -- beside sexc in a work tree, under ../share/sex once installed -- and looks there last, so a module of one's own by that name still wins. The C arrow becomes `%->', and that rename is what makes the rest possible. The closure lifter writes arrows of its own for the capture record, those forms go back through walk-statement, and a macro named `->' threads them: (-> ƛcaptures n) turns into a call to n, with nothing saying so until the C compiler. fmt-c rendered a bare `->' as the arrow, which left the one spelling that cannot be right -- threading with no import -- looking like the operator it stopped being. It goes through c-apply now, the way any other head nothing knows does.
69 lines
2.0 KiB
Scheme
69 lines
2.0 KiB
Scheme
(input)
|
||
(output "$12.34"
|
||
"(3, 4)"
|
||
"max 1234"
|
||
"realizes ord: money 1, point 0")
|
||
(return 0)
|
||
|
||
;;; A spec is a contract and emits nothing; a realization is the
|
||
;;; spec's methods for one type, mangled typeːspecːmethod. A default
|
||
;;; is copied into every realizing type with the parameter
|
||
;;; substituted, so the `show' it calls is that type's own.
|
||
;;;
|
||
;;; What the layer is for is the last line: a generic asks `realizes?'
|
||
;;; before it expands, and says so itself rather than leaving a wall
|
||
;;; of C errors two stages downstream.
|
||
|
||
(include stdio.h)
|
||
|
||
(record money ((cents int)))
|
||
(record point ((x int) (y int)))
|
||
|
||
(spec (printable T)
|
||
(fn show ((self (* const T))) void)
|
||
(fn show-line ((self (* const T))) void ; default, calls the required one
|
||
(.show self)
|
||
(printf "\n")))
|
||
|
||
(spec (ord T)
|
||
(fn less ((a (* const T)) (b (* const T))) bool))
|
||
|
||
(realize (printable money)
|
||
(fn show ((self (* const money))) void
|
||
(printf "$%d.%02d" (/ (. self cents) 100) (% (. self cents) 100))))
|
||
|
||
(realize (printable point)
|
||
(fn show ((self (* const point))) void
|
||
(printf "(%d, %d)" (. self x) (. self y))))
|
||
|
||
(realize (ord money)
|
||
(fn less ((a (* const money)) (b (* const money))) bool
|
||
(return (< (. a cents) (. b cents)))))
|
||
|
||
;;; A bounded generic: the macro refuses before it expands
|
||
(defmacro (max-of type)
|
||
(unless (realizes? 'ord type)
|
||
(error "does not realize ord" type))
|
||
`(fn ,(cat 'max- type) ((a (* const ,type)) (b (* const ,type))) ,type
|
||
(if (,(cat type '/ord/less) a b)
|
||
(return (* b))
|
||
(return (* a)))))
|
||
|
||
(max-of money)
|
||
|
||
(defmacro (ord? type) (if (realizes? 'ord type) 1 0))
|
||
|
||
(pub fn main () int
|
||
(var m money #(money : 1234))
|
||
(var p point #(point : 3 4))
|
||
|
||
(.show-line m) ; the default, through the registry
|
||
(point/printable/show-line (& p)) ; and spelled out
|
||
|
||
(var cheap money #(money : 150))
|
||
(var big money (max-money (& m) (& cheap)))
|
||
(printf "max %d\n" (. big cents))
|
||
|
||
(printf "realizes ord: money %d, point %d\n" (ord? money) (ord? point))
|
||
(return 0))
|