@@ -0,0 +1,180 @@
;;; 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 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 ) ")" ) )
( 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 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 "an address cannot be taken of a call"
( reports? ( ops-src "(.length (mk))" ) "receiver is not an lvalue" ) ) )
;; 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" ) ) )
;; 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" ) ) ) )