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 36ff456d24
commit 12d0d13a7c
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);")))

View File

@@ -66,12 +66,12 @@
(printf "Through a parameter: %d\n" (apply-twice (make-adder 50) 10))
(var table (¤ (closure ((int)) int) 3))
(var table [(closure ((int)) int) 3])
(var i int 0)
(for (= i 0) (< i 3) (++ i)
(= (¤ table i) (make-adder i)))
(= [table i] (make-adder i)))
(printf "From an array: %d %d %d\n"
((¤ table 0) 1) ((¤ table 1) 1) ((¤ table 2) 1))
([table 0] 1) ([table 1] 1) ([table 2] 1))
;; the index must be evaluated once, so `i' ends at 1 and not 2 --
;; read in a separate statement, since reading and bumping it in one

View File

@@ -38,7 +38,7 @@
;; an array literal decays the way an array does
(var a (* int) #([int 3] : 10 20 30))
(printf "array %d %d %d\n" (¤ a 0) (¤ a 1) (¤ a 2))
(printf "array %d %d %d\n" [a 0] [a 1] [a 2])
(printf "argument %d\n" (sum #(struct point : 2 4)))

View File

@@ -158,7 +158,7 @@
;;;
;;; 1. SETTLED. `type-match' took `_' on the pattern side, so
;;; `(closure _ int)' above is one clause rather than one per
;;; signature, and `(* _)' and `(¤ _ _)' say "any pointer" and "any
;;; signature, and `(* _)' and `[_ _]' say "any pointer" and "any
;;; array". A `_' written last takes the rest, since a type's words
;;; are spread and not nested: `(* const char)' is three elements.
;;; Nothing destructures -- a macro body is Scheme and a type is a

View File

@@ -62,10 +62,10 @@
(printf "increment: %d\n" inc)
;; an array operand decays, so this is a pointer and not an array
(var xs (¤ int 4) #(1 2 3 4))
(var xs [int 4] #(1 2 3 4))
(var p _ (+ xs 1))
(var q _ (+ 1 xs))
(var names (¤ (* const char) 2) #("hi" "there"))
(var names [(* const char) 2] #("hi" "there"))
(var np _ (+ names 1))
(printf "decayed: %d %d %s\n" (* p) (* q) (* np))

View File

@@ -13,7 +13,7 @@
;;; Three questions about a written type that used to be answered in
;;; three places and disagreed: is `(a b)' a named parameter or a bare
;;; type, is the last element of a `¤' its bound or the last word of
;;; type, is the last element of a `[...]' its bound or the last word of
;;; its element type, and what is one element of an array.
;;;
;;; They are one question -- where does the type end -- so the answer
@@ -25,7 +25,7 @@
(typedef small int)
(typedef cstr (* const char))
(typedef vec4 (¤ float 4))
(typedef vec4 [float 4])
(typedef unary (fn ((small)) small))
(define CAP 2)
@@ -45,40 +45,40 @@
(pub fn main () int
;; an element keeps every word of its type, tag and all
(var pts (¤ (struct point) 2) #(#((struct point) : 1 2)
(var pts [(struct point) 2] #(#((struct point) : 1 2)
#((struct point) : 3 4)))
(var p _ (¤ pts 1))
(var p _ [pts 1])
(printf "aggregate element: %d %d\n" (. p x) (. p y))
(var names (¤ (* const char) 2) #("hi" "there"))
(var s _ (¤ names 1))
(var names [(* const char) 2] #("hi" "there"))
(var s _ [names 1])
(printf "pointer element: %s\n" s)
(var nums (¤ unsigned int 3) #(7 8 9))
(var u _ (¤ nums 2))
(var nums [unsigned int 3] #(7 8 9))
(var u _ [nums 2])
(printf "multi-word element: %u\n" u)
;; subscripting a pointer answers the same as subscripting an array
(var q (* (struct point)) (& (¤ pts 0)))
(var r _ (¤ q 1))
(var q (* (struct point)) (& [pts 0]))
(var r _ [q 1])
(printf "through a pointer: %d\n" (+ (. r x) (* 13 (. r y))))
;; the last word of an unsized array's type is not its bound: neither
;; a typedef name nor the target of a `*' can be one
(var tail (¤ const small) #(1 2))
(printf "unsized of a typedef: %d %d\n" (¤ tail 0) (¤ tail 1))
(var tail [const small] #(1 2))
(printf "unsized of a typedef: %d %d\n" [tail 0] [tail 1])
(var one size-t 5)
(var sizes (¤ * size-t) #((& one)))
(var w _ (¤ sizes 0))
(var sizes [* size-t] #((& one)))
(var w _ [sizes 0])
(printf "unsized of a pointer: %d\n" (cast (* w) int))
;; ...but after a whole type, a name is: `size-t' is a typedef only C
;; knows of, and `(* const char)' a type grouped into one element
(var ws (¤ size-t CAP) #(3 4))
(var gs (¤ (* const char) CAP) #("a" "b"))
(var ws [size-t CAP] #(3 4))
(var gs [(* const char) CAP] #("a" "b"))
(printf "named bound: %zu %zu %s\n"
(/ (sizeof ws) (sizeof (¤ ws 0))) (¤ ws 1) (¤ gs 1))
(/ (sizeof ws) (sizeof [ws 0])) [ws 1] [gs 1])
;; the same question in type position: `(fn ((unsigned int)) int)'
;; takes one parameter, not two
@@ -89,7 +89,7 @@
(var greeting cstr "hi")
(var v vec4 #(1.0 2.0 3.0 4.0))
(var op unary twice)
(printf "typedefs: %s %zu %d\n" greeting (/ (sizeof v) (sizeof (¤ v 0))) (op 1))
(printf "typedefs: %s %zu %d\n" greeting (/ (sizeof v) (sizeof [v 0])) (op 1))
;; `(* T)' is a type or a dereference, and only what is in scope says
(printf "sizeof a pointer type: %d %d\n"

View File

@@ -25,8 +25,8 @@
(fn origin-x ((p (* (struct point)))) int
(return (. (* p) x)))
(fn second-of ((xs (¤ float 4))) float
(return (¤ xs 1)))
(fn second-of ((xs [float 4])) float
(return [xs 1]))
(fn say ((fmt (* const char)) ...) void
(var ap va-list)
@@ -43,8 +43,8 @@
(var f (fn ((* (struct point))) int) origin-x)
(printf "aggregate: %d\n" (f (& p)))
(var xs (¤ float 4) #(1.5 2.5 3.5 4.5))
(var g (fn ((¤ float 4)) float) second-of)
(var xs [float 4] #(1.5 2.5 3.5 4.5))
(var g (fn ([float 4]) float) second-of)
(printf "array: %g\n" (g xs))
(say "variadic: %d %s\n" 1 "two")

View File

@@ -44,11 +44,11 @@
(var pp _ (& p))
(printf "pointers: %d %g\n" (. pp x) (. pp y))
(var table (¤ int 3))
(= (¤ table 0) 10)
(= (¤ table 1) 20)
(var first _ (¤ table 0))
(var second _ (¤ table 1))
(var table [int 3])
(= [table 0] 10)
(= [table 1] 20)
(var first _ [table 0])
(var second _ [table 1])
(printf "arrays: %d\n" (+ first second))
;; a for opens a scope, and its initializer is declared inside it
@@ -103,7 +103,7 @@
;; a brace initializer has no type of its own, but its elements solve
;; the hole in the array type around it -- and the length stays as
;; written, whether or not every slot is initialized
(var squares (¤ _ 4) #(0 1 4 9))
(var sparse (¤ _ 8) #(0 1))
(printf "from elements: %d %d %d\n" (¤ squares 2) (¤ squares 3) (¤ sparse 7))
(var squares [_ 4] #(0 1 4 9))
(var sparse [_ 8] #(0 1))
(printf "from elements: %d %d %d\n" [squares 2] [squares 3] [sparse 7])
(return 0))