;;; record, ops, sum and match, through the whole pipeline. ;;; ;;; The diagnostics are the point of these forms -- a match that ;;; leaves a variant out is the thing C cannot say -- so most of what ;;; is checked here is what the compiler refuses, and that it refuses ;;; it with the name of whatever is wrong. ;;; ;;; `sex->c', `emits?', `reports?' and `in-fn' come from codegen.scm, ;;; which run.scm includes first. (define (ops-src . body) (string-append "(record vec2 ((x float) (y float)))" "(ops vec2" " (pub fn length ((self (* const vec2))) float (return 1.0))" " (fn sum-of ((self vec2)) float (return (. self x)))" " (fn scale ((self (* vec2))) void (= (. self x) 2.0)))" "(fn mk () vec2 (return #(vec2 : 1.0 2.0)))" "(fn f () void (var v vec2 #(vec2 : 1.0 2.0)) (var p (* vec2) (& v)) " (apply string-append body) ")")) (define (sum-src . body) (string-append "(sum shape ((circle ((r float))) (rect ((w float) (h float))) (empty ())))" "(fn mk () shape (return (shape/circle 1.0)))" "(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 ;; name stands where (struct name) had to. (test-group "record" (test-assert "emits the struct" (emits? "(record point ((x int) (y int)))" "struct point {")) (test-assert "and a typedef of the same name" (emits? "(record point ((x int) (y int)))" "typedef struct point point;")) (test-assert "so the bare name is a type" (emits? "(record point ((x int)))(fn f () void (var p point))" "point p;")) (test-assert "and so is a pointer to it" (emits? "(record point ((x int)))(fn f () void (var p (* const point)))" "const point * p")) (test-assert "struct keeps C's meaning" (not (emits? "(struct raw ((x int)))" "typedef"))) (test-assert "a record needs a name" (reports? "(record ((x int)))" "record needs a name")) ;; An opaque handle: the declaration is public, the fields are not (test-assert "declared, then defined, is still defined" (emits? "(pub record h)(record h ((x int)))" "struct h {"))) ;; An ops block is mangled functions plus a registry entry. `pub' ;; inside it means what it means outside. (test-group "ops" (test-assert "a method is the type and the name, mangled" (emits? (ops-src "") "float vec2ːlength")) (test-assert "a pub method has external linkage" (not (emits? (ops-src "") "static float vec2ːlength"))) (test-assert "a private one does not" (emits? (ops-src "") "static float vec2ːsum_of")) (test-assert "the slash is not a division" (emits? "(fn f () int (return (/ 10 2)))" "10 / 2")) (test-assert "ops needs a type name" (reports? "(ops (fn g ((s int)) void))" "ops needs a type name")) (test-assert "and takes only fn forms" (reports? "(record v ((x int)))(ops v (var n int 1))" "ops takes fn forms")) (test-assert "a method is defined once" (reports? (string-append "(record v ((x int)))" "(ops v (fn g ((s v)) int (return 1))" " (fn g ((s v)) int (return 2)))") "method is already defined on")) (test-assert "pub goes on the methods, not the block" (reports? "(record v ((x int)))(pub ops v)" "pub belongs on the methods"))) ;; The method sits in the head, so the shape of a form can be read ;; without knowing the receiver's type. (test-group "method calls" (test-assert "a value meeting a pointer self is addressed" (emits? (ops-src "(.length v)") "vec2ːlength(&v)")) (test-assert "a pointer meeting a pointer self is passed along" (emits? (ops-src "(.length p)") "vec2ːlength(p)")) (test-assert "a pointer meeting a value self is dereferenced" (emits? (ops-src "(.sum-of p)") "vec2ːsum_of(*p)")) (test-assert "a value meeting a value self is passed along" (emits? (ops-src "(.sum-of v)") "vec2ːsum_of(v)")) (test-assert "a field is still a field" (emits? (ops-src "(var q float (. v x))") "v.x")) ;; the whole reason the method moved into the head (test-assert "a field and a method of one name coexist" (emits? (string-append "(record w ((x int)))" "(ops w (fn x ((self (* const w))) int (return 2)))" "(fn f () void (var a w #(w : 1)) (var b int (. a x))" " (var c int (.x a)))") "wːx(&a)")) (test-assert "and the field reading is untouched" (emits? (string-append "(record w ((x int)))" "(ops w (fn x ((self (* const w))) int (return 2)))" "(fn f () void (var a w #(w : 1)) (var b int (. a x)))") "int b = a.x")) (test-assert "a method that is not there is named" (reports? (ops-src "(.nope v)") "no such method on")) (test-assert "and so is a receiver with no type to dispatch on" (reports? "(fn f () void (var n int 1) (.get n))" "no such method on")) (test-assert "a self spread over the parameter is a pointer self too" (emits? (string-append "(record w ((x int)))" "(ops w (fn get ((self * const w)) int (return (. self x))))" "(fn f () int (var a w #(w : 1)) (var p (* const w) (& a))" " (return (+ (.get a) (.get p))))") "wːget(&a) + wːget(p)")) (test-assert "a const receiver is not handed to a method that may change it" (reports? (ops-src "(var c (* const vec2) p) (.scale c)") "this one is const")) (test-assert "one that cannot change it takes it" (emits? (ops-src "(var c (* const vec2) p) (var n float (.length c))") "vec2ːlength(c)")) (test-assert "an address cannot be taken of a call" (reports? (ops-src "(.length (mk))") "receiver is not an lvalue")) ;; the inner call has to be typed to find the outer one's receiver (test-assert "a method call can be another's receiver" (emits? (string-append "(record w ((x int)))" "(ops w (fn twice ((a w)) w (return a))" " (fn get ((a w)) int (return (. a x))))" "(fn f () int (var a w #(w : 1)) (return (.get (.twice a))))") "wːget(wːtwice(a))"))) ;; A sum is the tag enum, a payload struct per variant that carries ;; fields, the record holding both, and a constructor each. (test-group "sum" (test-assert "the tag enum is named after the sum" (emits? (sum-src "") "enum shapeːtag")) (test-assert "a variant with fields gets a payload struct" (emits? (sum-src "") "struct shapeːcircle {")) (test-assert "the sum is typedef'd" (emits? (sum-src "") "typedef struct shape shape;")) ;; a typedef here would land in the namespace the constructor holds (test-assert "a payload struct is not" (not (emits? (sum-src "") "typedef struct shapeːcircle"))) (test-assert "the typedef comes before the payloads, for a recursive sum" (emits? "(sum expr ((lit ((v float))) (neg ((e (* const expr))))))" "typedef struct expr expr;")) (test-assert "constructors are static even when the sum is pub" (emits? "(pub sum t ((a ((x int)))))" "static t tːa")) (test-assert "a tag-only sum has no union at all" (not (emits? "(sum light ((red ()) (green ())))" "union"))) (test-assert "a sum is declared once" (reports? "(sum t ((a ())))(sum t ((b ())))" "sum is already defined")) (test-assert "and needs variants" (reports? "(sum t)" "sum needs a name and a list of variants")) (test-assert "a variant is a name and a field list" (reports? "(sum t (a))" "a variant is"))) ;; 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 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 a switch over the tag whose arms bind the payload. What ;; it refuses is why it exists. (test-group "match" (test-assert "it switches on the tag" (emits? (sum-src "(match s ((circle (r)) (= out r)) ((rect (w h)) (= out w))" " ((empty ()) (= out 0.0)))") "switch (s.tag)")) (test-assert "an arm binds its payload" (emits? (sum-src "(match s ((circle (r)) (= out r)) ((rect (w h)) (= out w))" " ((empty ()) (= out 0.0)))") "float r = s.u.circle.r")) (test-assert "_ binds nothing" (not (emits? (sum-src "(match s ((circle (r)) (= out r))" " ((rect (w _)) (= out w))" " ((empty ()) (= out 0.0)))") ").h"))) (test-assert "a scrutinee that is not a path is read once" (emits? (sum-src "(match (mk) ((circle (r)) (= out r)) (else (= out 0.0)))") "shape ƛm0 = mk()")) (test-assert "a path scrutinee is not copied" (not (emits? (sum-src "(match s ((circle (r)) (= out r)) (else (= out 0.0)))") "ƛm0"))) (test-assert "else becomes default" (emits? (sum-src "(match s ((circle (r)) (= out r)) (else (= out 0.0)))") "default:")) (test-assert "a comment between arms is dropped" (emits? (sum-src "(match s ((circle (r)) (= out r)) ;; gone\n (else (= out 0.0)))") "default:")) (test-assert "a variant left out is an error" (reports? (sum-src "(match s ((circle (r)) (= out r)))") "match leaves a variant out")) (test-assert "and the missing ones are named" (reports? (sum-src "(match s ((circle (r)) (= out r)))") "rect")) (test-assert "else excuses the rest" (emits? (sum-src "(match s ((circle (r)) (= out r)) (else (= out 0.0)))") "switch (s.tag)")) (test-assert "a variant the sum does not have is an error" (reports? (sum-src "(match s ((circle (r)) (= out r)) ((square (w)) (= out w))" " ((rect (w h)) (= out w)) ((empty ()) (= out 0.0)))") "not a variant of")) (test-assert "so is the wrong number of binders" (reports? (sum-src "(match s ((circle (r q)) (= out r)) (else (= out 0.0)))") "variant takes this many fields")) (test-assert "so is the same arm twice" (reports? (sum-src "(match s ((circle (r)) (= out r)) ((circle (q)) (= out q))" " (else (= out 0.0)))") "arm is written twice")) (test-assert "and a scrutinee that is not a sum" (reports? "(fn f () void (var n int 1) (match n ((a ()) (= n 2))))" "match wants a sum"))))