write arrays with brackets in tests

The tests spelled out (¤ ...), what [...] reads into, which nobody
writes. The reader's own tests keep it: there it is the point.
This commit is contained in:
2026-10-09 16:32:36 +03:00
parent f3d9c95daf
commit 7532c72dec
8 changed files with 55 additions and 55 deletions

View File

@@ -366,7 +366,7 @@ compiles."
(emits? "(fn f ((c (closure ((long)) int))) int (return (c 1)))"
"ƛc.code(&ƛc.env, ƛa0)"))
(test-assert "a subscript receiver is evaluated once"
(emits? "(fn f ((t (¤ (closure ((int)) int) 4)) (i int)) int (return ((¤ t (++ i)) 1)))"
(emits? "(fn f ((t [(closure ((int)) int) 4]) (i int)) int (return ([t (++ i)] 1)))"
"ƛint_int_call(t[++i], 1)"))
(test-assert "so is a member receiver"
(emits? "(struct h ((cb (closure ((int)) int))))
@@ -487,7 +487,7 @@ compiles."
(test-group "macro list accessors"
(test-assert "third reads an array's length"
(emits? "(defmacro (len t) (third t))
(fn f () int (return (len (¤ int 7))))"
(fn f () int (return (len [int 7])))"
"return 7;"))
(test-assert "second reads a tag"
(emits? "(defmacro (tag t) (symbol->string (second t)))
@@ -561,7 +561,7 @@ compiles."
(fn f ((s (struct p))) void (var x (* _) (& s)))"
"struct p* x = &s"))
(test-assert "a wildcard inside an array"
(emits? (in-fn "(var t (¤ _ 3) #((¤ int 3) : 1 2 3))") "int t[3]"))
(emits? (in-fn "(var t [_ 3] #([int 3] : 1 2 3))") "int t[3]"))
;; a compound literal carries its own type, where a brace
;; initializer has none and takes one from its context
(test-assert "a compound literal answers a bare wildcard"
@@ -572,11 +572,11 @@ compiles."
(reports? (in-fn "(var x _ #(1 2))") "cannot infer the type"))
;; ...but its elements still solve the hole in an array type
(test-assert "elements solve an array's element type"
(emits? (in-fn "(var t (¤ _ 4) #(0 1 4 9))") "int t[4] = {0, 1, 4, 9}"))
(emits? (in-fn "(var t [_ 4] #(0 1 4 9))") "int t[4] = {0, 1, 4, 9}"))
(test-assert "including when there are fewer than the length"
(emits? (in-fn "(var t (¤ _ 10) #(1 2))") "int t[10] = {1, 2}"))
(emits? (in-fn "(var t [_ 10] #(1 2))") "int t[10] = {1, 2}"))
(test-assert "and they have to agree with each other"
(reports? (in-fn "(var t (¤ _ 2) #(1 \"s\"))") "type mismatch"))
(reports? (in-fn "(var t [_ 2] #(1 \"s\"))") "type mismatch"))
;; a closure type reaches the solver as the struct that stands for
;; it, which is the spelling `parse-type' knows
(test-assert "a closure, from the signature that produced it"
@@ -680,28 +680,28 @@ compiles."
(emits? (in-fn "(var n int (+ (- a) b))") "-a + b")))
;; An array bound was taken only when it was an integer literal, so a
;; symbolic one fell into the type: `(¤ int N)' came out `int N a[]'.
;; symbolic one fell into the type: `[int N]' came out `int N a[]'.
(test-group "array bounds"
(test-assert "a symbolic bound"
(emits? "(define N 4) (struct s ((a (¤ int N))))" "int a[N]"))
(emits? "(define N 4) (struct s ((a [int N])))" "int a[N]"))
(test-assert "an expression bound"
(emits? "(define N 4) (struct s ((a (¤ char (* 2 N)))))" "char a[2 * N]"))
(emits? "(define N 4) (struct s ((a [char (* 2 N)])))" "char a[2 * N]"))
(test-assert "an integer bound still works"
(emits? "(struct s ((a (¤ int 4))))" "int a[4]"))
(emits? "(struct s ((a [int 4])))" "int a[4]"))
;; ...and it is spelled the way the same name is in an expression
(test-assert "a bound's name is converted"
(emits? (in-fn "(var xs (¤ u16 CHUNK-AREA))") "u16 xs[CHUNK_AREA]"))
(emits? (in-fn "(var xs [u16 CHUNK-AREA])") "u16 xs[CHUNK_AREA]"))
(test-assert "so is one inside an expression bound"
(emits? (in-fn "(var xs (¤ u16 (* 2 CHUNK-AREA)))") "u16 xs[2 * CHUNK_AREA]"))
(emits? (in-fn "(var xs [u16 (* 2 CHUNK-AREA)])") "u16 xs[2 * CHUNK_AREA]"))
;; a multi-word type is keywords all the way down, so a trailing
;; keyword belongs to the type and leaves the array unsized
(test-assert "a multi-word type is not a bound"
(emits? "(struct s ((a (¤ unsigned int))))" "unsigned int a[]"))
(emits? "(struct s ((a [unsigned int])))" "unsigned int a[]"))
;; ...and a tag always follows its keyword
(test-assert "nor is an aggregate tag"
(emits? "(struct t ((z int))) (struct s ((a (¤ struct t))))" "struct t a[]"))
(emits? "(struct t ((z int))) (struct s ((a [struct t])))" "struct t a[]"))
(test-assert "nor one behind a pointer"
(emits? "(struct t ((z int))) (struct s ((a (¤ * struct t))))" "struct t* a[]")))
(emits? "(struct t ((z int))) (struct s ((a [* struct t])))" "struct t* a[]")))
;; `sizeof' takes a type or an expression, and `(* x)' is either.
;; What is in scope says which: a name C alone knows is a type
@@ -715,7 +715,7 @@ compiles."
(test-assert "a multi-word type"
(emits? (in-fn "(var n size-t (sizeof (unsigned int)))") "sizeof(unsigned int)"))
(test-assert "an array type"
(emits? (in-fn "(var n size-t (sizeof (¤ int 4)))") "sizeof(int [4])"))
(emits? (in-fn "(var n size-t (sizeof [int 4]))") "sizeof(int [4])"))
(test-assert "a dereference of a local"
(emits? (in-fn "(var p (* int) 0) (var n size-t (sizeof (* p)))") "sizeof(*p)"))
(test-assert "of a parameter"
@@ -723,7 +723,7 @@ compiles."
(test-assert "of a call"
(emits? (in-fn "(var n size-t (sizeof (* (g))))") "sizeof(*g())"))
(test-assert "a subscript"
(emits? (in-fn "(var xs (¤ int 4)) (var n size-t (sizeof (¤ xs 0)))") "sizeof(xs[0])")))
(emits? (in-fn "(var xs [int 4]) (var n size-t (sizeof [xs 0]))") "sizeof(xs[0])")))
;; A define's body was pasted unwalked, and unparenthesised: `cast'
;; stayed a call, and (* x D) regrouped with D's own operator
@@ -750,7 +750,7 @@ compiles."
(test-assert "of a pointer"
(emits? "(typedef cstr (* const char))" "typedef const char * cstr;"))
(test-assert "of an array"
(emits? "(typedef vec4 (¤ float 4))" "typedef float vec4[4];"))
(emits? "(typedef vec4 [float 4])" "typedef float vec4[4];"))
(test-assert "of a function pointer"
(emits? "(typedef colour-fn (fn ((i32)) u32))"
"typedef u32 (*colour_fn)(i32);")))