diff --git a/fmt-c-writer.scm b/fmt-c-writer.scm index 92ce013..6511f6e 100644 --- a/fmt-c-writer.scm +++ b/fmt-c-writer.scm @@ -353,9 +353,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))))) diff --git a/infer.scm b/infer.scm index bfa5db9..78f5c1a 100644 --- a/infer.scm +++ b/infer.scm @@ -215,7 +215,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))) diff --git a/semen.scm b/semen.scm index 05f28a9..7ad2472 100644 --- a/semen.scm +++ b/semen.scm @@ -671,6 +671,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! @@ -753,7 +763,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))) @@ -794,10 +804,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))) diff --git a/sex-modules.scm b/sex-modules.scm index 5ab1f79..a91da8f 100644 --- a/sex-modules.scm +++ b/sex-modules.scm @@ -227,7 +227,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)) diff --git a/tests/codegen.scm b/tests/codegen.scm index a5b5e17..5e9b4c1 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -735,7 +735,32 @@ (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 diff --git a/tests/sex-programs/type-shapes.sex b/tests/sex-programs/type-shapes.sex index 58f6c25..ebcc036 100644 --- a/tests/sex-programs/type-shapes.sex +++ b/tests/sex-programs/type-shapes.sex @@ -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)) diff --git a/types.scm b/types.scm index 9d99c58..8a73ccc 100644 --- a/types.scm +++ b/types.scm @@ -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)))))