read every trailing array bound as a dimension

[m 1 2] lost its second index, and [int 2 3] came out int 2 x[3]. Each
index now subscripts what the one before it gave, and [int 2 3] declares
what it indexes, int[2][3]. An inline struct's fields, once more than
one bound was looked for, were taken for one; their shape tells them apart.
This commit is contained in:
2026-10-16 15:26:05 +03:00
parent f9350c0474
commit 2c92a2fd7d
7 changed files with 99 additions and 14 deletions

View File

@@ -362,9 +362,10 @@ forms, and what remains."
;; (const * const * const char) -> (const char * const * const)
;; (fn ((int) (float)) void) -> (%fun void ((int) (float)))
(match form
(('¤ . array-type)
(('¤ . _)
(if (array-bound? form)
`(%array ,(walk-type (array-element-type form)) ,(walk-expr (last array-type)))
`(%array ,(walk-type (array-element-type form))
,(walk-expr (array-bound form)))
;; sugar for pointer... Do we really need it? Guess why not,
;; it's a strong semantic cue
`(%array ,(walk-type (array-element-type form)))))

View File

@@ -282,7 +282,7 @@
;;; way the C writer unwraps them, so `[int 16]' and `[(int) 16]' are
;;; one type.
(define (parse-array surface)
(let* ((rest (cdr surface))
(let* ((rest (cdr (nest-array-type surface)))
(sized? (and (pair? rest) (integer? (last rest))))
(size (and sized? (last rest)))
(words (if sized? (drop-right rest 1) rest)))

View File

@@ -675,6 +675,16 @@
((dot-access) (walk-member-path form env))
;; [m 1 2] is [[m 1] 2], m[1][2]
((¤)
(if (> (length form) 3)
(walk-statement
(copy-form-source!
form
(list '¤ (copy-form-source! form (drop-right form 1)) (last form)))
env)
(walk-parts form env)))
((sizeof)
(if (and (= 2 (length form)) (written-type? (second form) env))
(copy-form-source!
@@ -757,7 +767,7 @@
;;; nothing here can count
(define (array-length type)
(and (array-bound? type)
(let ((bound (last type)))
(let ((bound (array-bound type)))
(cond ((and (integer? bound) (exact? bound)) bound)
((symbol? bound)
(let ((info (get-type-info bound)))
@@ -798,10 +808,10 @@
;;; there that is a type:
;;;
;;; (* char) (* const char) (* size-t) (unsigned int) types
;;; (* p) (* (g)) (¤ xs 0) expressions
;;; (* p) (* (g)) (¤ xs 0) (¤ m 1 2) expressions
(define (written-type? form env)
(match form
((or ('* operand) ('¤ operand _))
((or ('* operand) ('¤ operand _ . _))
(if (symbol? operand)
(not (or (lookup-name env operand) (get-name-type operand)))
(written-type? operand env)))

View File

@@ -230,7 +230,7 @@
((*) (append-map (lambda (t) (type-uses t #t)) (cdr type)))
((¤) (let ((element (array-element-type type)))
(append (if element (type-uses element behind?) (list))
(if (array-bound? type) (bound-uses (last type)) (list)))))
(if (array-bound? type) (bound-uses (array-bound type)) (list)))))
((fn closure)
(if (and (pair? (cdr type)) (list? (second type)))
(append-map (lambda (t) (type-uses t #f))

View File

@@ -800,7 +800,32 @@ whitespace aside, THEN?"
(test-assert "after a qualified name too"
(emits? (in-fn "(var b [mytype const N])") "mytype const b[N]"))
(test-assert "but a qualifier alone still waits for the type"
(emits? "(typedef mytype int) (struct s ((a [const mytype])))" "const mytype a[]")))
(emits? "(typedef mytype int) (struct s ((a [const mytype])))" "const mytype a[]"))
;; [m 1 2] is m[1][2], so [int 2 3] declares what it indexes. A
;; second bound was taken for the last word of the element type
(test-assert "every trailing bound is a dimension"
(emits? (in-fn "(var m [int 2 3])") "int m[2][3];"))
(test-assert "however many there are"
(emits? (in-fn "(var c [u8 2 3 4])") "u8 c[2][3][4];"))
(test-assert "named ones too"
(emits? "(define N 2) (struct s ((a [* char N 3])))" "char * a[N][3]"))
;; a list after a tag's keyword is the aggregate's body
(test-assert "an inline aggregate's fields are not one"
(emits? "(struct s ((a [struct ((x int)) 2 3])))" "a[2][3]"))
(test-assert "but an expression after a tag is"
(emits? "(define N 2) (struct t ((z int))) (struct s ((a [struct t (* 2 N)])))"
"struct t a[2 * N]")))
;; ...and an index after the first was dropped
(test-group "subscripts"
(test-assert "each index subscripts what the one before it gave"
(emits? (in-fn "(var m [int 2 3]) (= [m 1 2] a)") "m[1][2] = a;"))
(test-assert "which is an element to inference"
(emits? (in-fn "(var m [int 2 3]) (var e _ [m 1 2])") "int e = m[1][2];"))
(test-assert "and an expression to sizeof"
(emits? (in-fn "(var m [int 2 3]) (var n size-t (sizeof [m 1 1]))") "sizeof(m[1][1])"))
(test-assert "nested by hand it is the same"
(emits? (in-fn "(var m [int 2 3]) (= [[m 1] 2] a)") "m[1][2] = 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

View File

@@ -8,7 +8,8 @@
"named bound: 2 4 b"
"unnamed parameters: 7 -1 2"
"typedefs: hi 4 2"
"sizeof a pointer type: 1 1")
"sizeof a pointer type: 1 1"
"dimensions: 6 12 5")
(return 0)
;;; Three questions about a written type that used to be answered in
@@ -95,4 +96,9 @@
(printf "sizeof a pointer type: %d %d\n"
(== (sizeof (* const char)) (sizeof greeting))
(== (sizeof (* greeting)) 1))
;; [int 2 3] is two rows of three, indexed [g row column]
(var g [int 2 3] #(#(0 1 2) #(3 4 5)))
(printf "dimensions: %zu %zu %d\n"
(/ (sizeof g) (sizeof int)) (sizeof [g 0]) [g 1 2])
(return 0))

View File

@@ -64,7 +64,9 @@
arglist-types
strip-pub
array-bound?
array-element-type)
array-element-type
array-bound
nest-array-type)
;;; Two namespaces: `struct point' and a `point' typedef are separate
;;; declarations. Tags -- struct, union and enum alike -- share the
@@ -486,6 +488,9 @@
(let ((bound (last type))
(preceding (last (drop-right type 1))))
(cond
((and (pair? bound) (every pair? bound)
(aggregate-head? (drop-right (cdr type) 1)))
#f)
((not (symbol? bound)) #t)
((or (memq bound +c-specifiers+) (memq bound +c-qualifiers+)) #f)
((memq preceding '(* struct union enum)) #f)
@@ -496,14 +501,52 @@
(drop-right (cdr type) 2)))
(else #t)))))
;;; Do WORDS end the way a struct's head does, so that a list of fields
;;; after them is its body? A list of anything else is a bound, as in
;;; (¤ struct point (* 2 N)):
;;;
;;; (struct) (struct point) heads (* struct point) (int) not
(define (aggregate-head? words)
(let ((tag-words (if (and (pair? words) (symbol? (last words))
(not (memq (last words) '(struct union enum))))
(drop-right words 1)
words)))
(and (pair? tag-words)
(memq (last tag-words) '(struct union enum))
#t)))
;;; [int 2 3] is two of [int 3], so it declares what [m 1 2] indexes,
;;; m[1][2]. Every trailing bound is a dimension, the first outermost:
;;;
;;; (¤ int 2 3) -> (¤ (¤ int 3) 2)
;;; (¤ char N 3) -> (¤ (¤ char 3) N)
;;; (¤ int 4) -> (¤ int 4)
(define (nest-array-type type)
(let peel ((base type) (bounds (list)))
(if (array-bound? base)
(peel (drop-right base 1) (cons (last base) bounds))
(if (< (length bounds) 2)
type
(let ((inner-first (reverse bounds)))
(fold (lambda (bound inner) (list '¤ inner bound))
(append base (list (car inner-first)))
(cdr inner-first)))))))
;;; How many elements a written array type has, when array-bound? says
;;; it says: (¤ int 4) -> 4, (¤ int 2 3) -> 2
(define (array-bound type)
(last (nest-array-type type)))
;;; What one element of a written array type is:
;;; (¤ struct point 2) -> (struct point), (¤ * const char 2) -> (* const char)
;;; (¤ struct point 2) -> (struct point), (¤ * const char 2) -> (* const char),
;;; (¤ int 2 3) -> (¤ int 3)
(define (array-element-type type)
(and (pair? type)
(eq? '¤ (car type))
(pair? (cdr type))
(let ((words (if (array-bound? type)
(drop-right (cdr type) 1)
(cdr type))))
(let* ((type (nest-array-type type))
(words (if (array-bound? type)
(drop-right (cdr type) 1)
(cdr type))))
(and (pair? words)
(if (null? (cdr words)) (car words) words)))))