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:
@@ -42,16 +42,6 @@ ahead of its pub forms."
|
||||
"(fn f () void (var s shape (shape/circle 1.0)) (var out float 0.0) "
|
||||
(apply string-append body) ")"))
|
||||
|
||||
(define (spec-src . body)
|
||||
(string-append
|
||||
"(record v ((x int)))"
|
||||
"(spec (p T)"
|
||||
" (fn s ((self (* const T))) void)"
|
||||
" (fn both ((self (* const T))) void (.s self)))"
|
||||
"(realize (p v)"
|
||||
" (fn s ((self (* const v))) void (return)))"
|
||||
(apply string-append body)))
|
||||
|
||||
(test-group "encapsulation"
|
||||
|
||||
;; A record is a struct plus a typedef of its own name, so the bare
|
||||
@@ -296,104 +286,6 @@ ahead of its pub forms."
|
||||
(test-assert "and is declared once"
|
||||
(reports? "(sum t ((a ((x int))) (a ())))" "variant is declared twice")))
|
||||
|
||||
;; A spec is a contract that emits nothing; a realization is its
|
||||
;; methods for one type. The refusals are what make it a contract.
|
||||
(test-group "spec and realize"
|
||||
(test-assert "a spec on its own emits nothing"
|
||||
(not (emits? "(spec (p T) (fn s ((a (* const T))) void))" "p")))
|
||||
(test-assert "a realization mangles type, spec and method"
|
||||
(emits? (spec-src "") "void vːpːs"))
|
||||
(test-assert "a default is copied per realizing type"
|
||||
(emits? (spec-src "") "vːpːboth"))
|
||||
(test-assert "and its parameter is substituted"
|
||||
(emits? (spec-src "") "void vːpːboth (const v * self)"))
|
||||
(test-assert "a default calls that type's own required method"
|
||||
(emits? (spec-src "") "vːpːs(self)"))
|
||||
(test-assert "a default is static even when the realization is pub"
|
||||
(emits? (string-append "(record v ((x int)))"
|
||||
"(spec (p T) (fn s ((a (* const T))) void)"
|
||||
" (fn d ((a (* const T))) void (.s a)))"
|
||||
"(pub realize (p v)"
|
||||
" (fn s ((a (* const v))) void (return)))")
|
||||
"static void vːpːd"))
|
||||
(test-assert "but a written method is not"
|
||||
(not (emits? (string-append "(record v ((x int)))"
|
||||
"(spec (p T) (fn s ((a (* const T))) void))"
|
||||
"(pub realize (p v)"
|
||||
" (fn s ((a (* const v))) void (return)))")
|
||||
"static void vːpːs")))
|
||||
(test-assert "a spec method answers to .method"
|
||||
(emits? (spec-src "(fn f () void (var a v #(v : 1)) (.s (& a)))") "vːpːs(&a)"))
|
||||
|
||||
(test-assert "a spec is declared once"
|
||||
(reports? "(spec (p T) (fn s ((a T)) void))(spec (p T) (fn q ((a T)) void))"
|
||||
"spec is already declared"))
|
||||
(test-assert "a typedef realizes what its type does"
|
||||
(reports? (string-append (spec-src "") "(typedef alias v)(realize (p alias))")
|
||||
"spec is already realized for"))
|
||||
(test-assert "a type realizes a spec once"
|
||||
(reports? (string-append (spec-src "") "(realize (p v)"
|
||||
" (fn s ((a (* const v))) void (return)))")
|
||||
"spec is already realized for"))
|
||||
(test-assert "a realization names a spec that exists"
|
||||
(reports? "(record v ((x int)))(realize (nope v))" "no such spec"))
|
||||
(test-assert "every required method is provided"
|
||||
(reports? (string-append "(record v ((x int)))"
|
||||
"(spec (p T) (fn s ((a (* const T))) void)"
|
||||
" (fn q ((a (* const T))) void))"
|
||||
"(realize (p v) (fn s ((a (* const v))) void (return)))")
|
||||
"realization is missing a method of"))
|
||||
(test-assert "and the missing one is named"
|
||||
(reports? (string-append "(record v ((x int)))"
|
||||
"(spec (p T) (fn s ((a (* const T))) void)"
|
||||
" (fn q ((a (* const T))) void))"
|
||||
"(realize (p v) (fn s ((a (* const v))) void (return)))")
|
||||
"q"))
|
||||
(test-assert "a docstring is not a body"
|
||||
(reports? (string-append "(record v ((x int)))"
|
||||
"(spec (p T) (fn s ((a (* const T))) void \"doc\"))"
|
||||
"(realize (p v))")
|
||||
"realization is missing a method of"))
|
||||
(test-assert "nor is a comment"
|
||||
(reports? (string-append "(record v ((x int)))"
|
||||
"(spec (p T) (fn s ((a (* const T))) int\n"
|
||||
" ;; must be implemented\n"
|
||||
" ))"
|
||||
"(realize (p v))")
|
||||
"realization is missing a method of"))
|
||||
(test-assert "a documented default is still a default"
|
||||
(emits? (string-append "(record v ((x int)))"
|
||||
"(spec (p T) (fn s ((a (* const T))) int \"doc\" (return 1)))"
|
||||
"(realize (p v))")
|
||||
"/* doc */"))
|
||||
(test-assert "a method the spec has not got is refused"
|
||||
(reports? (string-append "(record v ((x int)))"
|
||||
"(spec (p T) (fn s ((a (* const T))) void))"
|
||||
"(realize (p v) (fn s ((a (* const v))) void (return))"
|
||||
" (fn z ((a (* const v))) void (return)))")
|
||||
"spec has no such method"))
|
||||
(test-assert "a return type has to match the spec"
|
||||
(reports? (string-append "(record v ((x int)))"
|
||||
"(spec (p T) (fn s ((a (* const T))) void))"
|
||||
"(realize (p v) (fn s ((a (* const v))) int (return 1)))")
|
||||
"return type does not match"))
|
||||
(test-assert "so do the arguments"
|
||||
(reports? (string-append "(record v ((x int)))"
|
||||
"(spec (p T) (fn s ((a (* const T))) void))"
|
||||
"(realize (p v) (fn s ((a v)) void (return)))")
|
||||
"arguments do not match"))
|
||||
(test-assert "but not on how a pointer is spelled"
|
||||
(emits? (string-append "(record v ((x int)))"
|
||||
"(spec (p T) (fn s ((a * const T)) void))"
|
||||
"(realize (p v) (fn s ((a (* const v))) void (return)))")
|
||||
"void vːpːs (const v * a)"))
|
||||
(test-assert "a method name already taken on the type is refused"
|
||||
(reports? (string-append "(record v ((x int)))"
|
||||
"(ops v (fn s ((a (* const v))) void (return)))"
|
||||
"(spec (p T) (fn s ((a (* const T))) void))"
|
||||
"(realize (p v) (fn s ((a (* const v))) void (return)))")
|
||||
"method is already defined on")))
|
||||
|
||||
;; match is an if over the tag whose arms bind the payload. What
|
||||
;; it refuses is why it exists.
|
||||
(test-group "match"
|
||||
@@ -557,17 +449,6 @@ ahead of its pub forms."
|
||||
(interface-reports? "(pub var n _ 42)" "write it out"))
|
||||
(test-assert "all of it"
|
||||
(interface-reports? "(pub var p (* _) 0)" "write it out"))
|
||||
;; a spec is seen by importers as a type is: pub, or not at all
|
||||
(test-assert "a pub realization of a private spec is refused"
|
||||
(interface-reports? (string-append "(spec (shown T) (fn show ((self (* const T))) int))"
|
||||
"(pub record m ((c int)))"
|
||||
"(pub realize (shown m) (fn show ((self (* const m))) int (return 1)))")
|
||||
"importers cannot see this spec; make it pub"))
|
||||
(test "and of a pub one is not"
|
||||
'()
|
||||
(interface-of (string-append "(pub spec (shown T) (fn show ((self (* const T))) int))"
|
||||
"(pub record m ((c int)))"
|
||||
"(pub realize (shown m) (fn show ((self (* const m))) int (return 1)))")))
|
||||
(test "a private one is solved where it is"
|
||||
'()
|
||||
(interface-of "(var n _ 42)"))
|
||||
|
||||
@@ -145,12 +145,6 @@
|
||||
(let ((err (error-of sexc "--public-interface" "gen/broken.sex")))
|
||||
(and err (irregex-search "broken\\.sex:8: expanding boom: nope" err))))
|
||||
|
||||
;;; ...while a realization of a spec importers cannot see is refused by
|
||||
;;; the module that writes it, not by an importer naming it first
|
||||
(test-assert "a pub realization of a spec imported without pub is refused"
|
||||
(let ((err (error-of sexc "specs/money.sex" "-C" "-o" "-")))
|
||||
(and err (irregex-search "money.sex:8: importers cannot see this spec" err) #t)))
|
||||
|
||||
(test "the app runs through both"
|
||||
'("hello, world"
|
||||
"hello, sex"
|
||||
@@ -160,7 +154,6 @@
|
||||
"mood 1"
|
||||
"closure 15 21 201"
|
||||
"vec2 7 7"
|
||||
"vec2(3 4)"
|
||||
"tally 2"
|
||||
"board 42"
|
||||
"ledger 2"
|
||||
|
||||
@@ -38,8 +38,6 @@
|
||||
(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)
|
||||
|
||||
@@ -110,20 +110,6 @@
|
||||
;;; called, so there is no function for two units to both define.
|
||||
(pub sum colour ((rgb ((r int) (g int) (b int))) (none ())))
|
||||
|
||||
;;; A spec and a realization over the boundary. The written method
|
||||
;;; travels as a prototype and keeps external linkage; the default
|
||||
;;; does not travel at all -- the importer synthesizes its own static
|
||||
;;; copy from the spec, which is why a default is static even here.
|
||||
(pub spec (printable T)
|
||||
(fn show ((self (* const T))) void)
|
||||
(fn show-line ((self (* const T))) void
|
||||
(.show self)
|
||||
(printf "\n")))
|
||||
|
||||
(pub realize (printable vec2)
|
||||
(fn show ((self (* const vec2))) void
|
||||
(printf "vec2(%d %d)" (. self x) (. self y))))
|
||||
|
||||
;;; Not `pub': invisible to importers, and static in the generated C.
|
||||
(fn unused-helper () void
|
||||
(printf "private\n"))
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
;;; Imports shown without pub, so its importers cannot see the spec
|
||||
;;; this realizes: refused here, rather than in whichever importer
|
||||
;;; names money before shown
|
||||
(import shown)
|
||||
|
||||
(pub record money ((cents int)))
|
||||
|
||||
(pub realize (shown money)
|
||||
(fn show ((self (* const money))) int (return (. self cents))))
|
||||
@@ -1,2 +0,0 @@
|
||||
(pub spec (shown T)
|
||||
(fn show ((self (* const T))) int))
|
||||
@@ -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