forked from alex-eg/sex
remove spec and realize
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.
This commit is contained in:
@@ -3,8 +3,8 @@
|
||||
"-5")
|
||||
(return 0)
|
||||
|
||||
;;; A recursive sum, an inherent method on it, and a spec realized for
|
||||
;;; it: every layer of encapsulation in one program, built at once.
|
||||
;;; A recursive sum and an inherent method on it: every layer of
|
||||
;;; encapsulation in one program, built at once.
|
||||
|
||||
(include stdio.h)
|
||||
|
||||
@@ -19,20 +19,15 @@
|
||||
(match (* self)
|
||||
((lit (v)) (return v))
|
||||
((neg (e)) (return (- (.eval e))))
|
||||
((add (l r)) (return (+ (.eval l) (.eval r)))))))
|
||||
|
||||
(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")))
|
||||
|
||||
(realize (printable expr)
|
||||
(fn show ((self (* const expr))) void
|
||||
((add (l r)) (return (+ (.eval l) (.eval r))))))
|
||||
(pub fn show ((self (* const expr))) void
|
||||
(match (* self)
|
||||
((lit (v)) (printf "%g" v))
|
||||
((neg (e)) (printf "-") (.show e))
|
||||
((add (l r)) (printf "(") (.show l) (printf " + ") (.show r) (printf ")")))))
|
||||
((add (l r)) (printf "(") (.show l) (printf " + ") (.show r) (printf ")"))))
|
||||
(pub fn show-line ((self (* const expr))) void
|
||||
(.show self)
|
||||
(printf "\n")))
|
||||
|
||||
(pub fn main () int
|
||||
(var two expr (expr/lit 2.0))
|
||||
|
||||
@@ -1,83 +0,0 @@
|
||||
(input)
|
||||
(output "$12.34"
|
||||
"(3, 4)"
|
||||
"max 1234"
|
||||
"realizes ord: money 1, point 0"
|
||||
"calls its own: 9")
|
||||
(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))
|
||||
|
||||
;;; A realization's methods may call one another, whatever order they
|
||||
;;; come in: `total', written, calls the default `doubled', which calls
|
||||
;;; the later default `plus-one', which calls `base', written below them
|
||||
(spec (sized T)
|
||||
(fn total ((self (* const T))) int)
|
||||
(fn doubled ((self (* const T))) int (return (* 2 (.plus-one self))))
|
||||
(fn plus-one ((self (* const T))) int (return (+ 1 (.base self))))
|
||||
(fn base ((self (* const T))) int))
|
||||
|
||||
(realize (sized point)
|
||||
(fn total ((self (* const point))) int (return (+ 1 (.doubled self))))
|
||||
(fn base ((self (* const point))) int (return (. self x))))
|
||||
|
||||
(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))
|
||||
(printf "calls its own: %d\n" (.total p))
|
||||
(return 0))
|
||||
Reference in New Issue
Block a user