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:
@@ -362,9 +362,10 @@ forms, and what remains."
|
|||||||
;; (const * const * const char) -> (const char * const * const)
|
;; (const * const * const char) -> (const char * const * const)
|
||||||
;; (fn ((int) (float)) void) -> (%fun void ((int) (float)))
|
;; (fn ((int) (float)) void) -> (%fun void ((int) (float)))
|
||||||
(match form
|
(match form
|
||||||
(('¤ . array-type)
|
(('¤ . _)
|
||||||
(if (array-bound? form)
|
(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,
|
;; sugar for pointer... Do we really need it? Guess why not,
|
||||||
;; it's a strong semantic cue
|
;; it's a strong semantic cue
|
||||||
`(%array ,(walk-type (array-element-type form)))))
|
`(%array ,(walk-type (array-element-type form)))))
|
||||||
|
|||||||
@@ -282,7 +282,7 @@
|
|||||||
;;; way the C writer unwraps them, so `[int 16]' and `[(int) 16]' are
|
;;; way the C writer unwraps them, so `[int 16]' and `[(int) 16]' are
|
||||||
;;; one type.
|
;;; one type.
|
||||||
(define (parse-array surface)
|
(define (parse-array surface)
|
||||||
(let* ((rest (cdr surface))
|
(let* ((rest (cdr (nest-array-type surface)))
|
||||||
(sized? (and (pair? rest) (integer? (last rest))))
|
(sized? (and (pair? rest) (integer? (last rest))))
|
||||||
(size (and sized? (last rest)))
|
(size (and sized? (last rest)))
|
||||||
(words (if sized? (drop-right rest 1) rest)))
|
(words (if sized? (drop-right rest 1) rest)))
|
||||||
|
|||||||
19
semen.scm
19
semen.scm
@@ -673,6 +673,16 @@
|
|||||||
|
|
||||||
((dot-access) (walk-member-path form env))
|
((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)
|
((sizeof)
|
||||||
(if (and (= 2 (length form)) (written-type? (second form) env))
|
(if (and (= 2 (length form)) (written-type? (second form) env))
|
||||||
(copy-form-source!
|
(copy-form-source!
|
||||||
@@ -755,7 +765,7 @@
|
|||||||
;;; nothing here can count
|
;;; nothing here can count
|
||||||
(define (array-length type)
|
(define (array-length type)
|
||||||
(and (array-bound? type)
|
(and (array-bound? type)
|
||||||
(let ((bound (last type)))
|
(let ((bound (array-bound type)))
|
||||||
(cond ((and (integer? bound) (exact? bound)) bound)
|
(cond ((and (integer? bound) (exact? bound)) bound)
|
||||||
((symbol? bound)
|
((symbol? bound)
|
||||||
(let ((info (get-type-info bound)))
|
(let ((info (get-type-info bound)))
|
||||||
@@ -796,11 +806,14 @@
|
|||||||
;;; there that is a type:
|
;;; there that is a type:
|
||||||
;;;
|
;;;
|
||||||
;;; (* char) (* const char) (* size-t) (unsigned int) types
|
;;; (* 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)
|
(define (written-type? form env)
|
||||||
(and (pair? form)
|
(and (pair? form)
|
||||||
;; the length a dereference and a subscript have
|
;; the length a dereference and a subscript have
|
||||||
(if (= (length form) (case (car form) ((*) 2) ((¤) 3) (else -1)))
|
(if (case (car form)
|
||||||
|
((*) (= (length form) 2))
|
||||||
|
((¤) (>= (length form) 3))
|
||||||
|
(else #f))
|
||||||
(let ((operand (second form)))
|
(let ((operand (second form)))
|
||||||
(if (symbol? operand)
|
(if (symbol? operand)
|
||||||
(not (or (lookup-name env operand) (get-name-type operand)))
|
(not (or (lookup-name env operand) (get-name-type operand)))
|
||||||
|
|||||||
@@ -231,7 +231,7 @@
|
|||||||
((*) (append-map (lambda (t) (type-uses t #t)) (cdr type)))
|
((*) (append-map (lambda (t) (type-uses t #t)) (cdr type)))
|
||||||
((¤) (let ((element (array-element-type type)))
|
((¤) (let ((element (array-element-type type)))
|
||||||
(append (if element (type-uses element behind?) (list))
|
(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)
|
((fn closure)
|
||||||
(if (and (pair? (cdr type)) (list? (second type)))
|
(if (and (pair? (cdr type)) (list? (second type)))
|
||||||
(append-map (lambda (t) (type-uses t #f))
|
(append-map (lambda (t) (type-uses t #f))
|
||||||
|
|||||||
@@ -801,7 +801,32 @@ whitespace aside, THEN?"
|
|||||||
(test-assert "after a qualified name too"
|
(test-assert "after a qualified name too"
|
||||||
(emits? (in-fn "(var b [mytype const N])") "mytype const b[N]"))
|
(emits? (in-fn "(var b [mytype const N])") "mytype const b[N]"))
|
||||||
(test-assert "but a qualifier alone still waits for the type"
|
(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.
|
;; `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
|
;; What is in scope says which: a name C alone knows is a type
|
||||||
|
|||||||
@@ -8,7 +8,8 @@
|
|||||||
"named bound: 2 4 b"
|
"named bound: 2 4 b"
|
||||||
"unnamed parameters: 7 -1 2"
|
"unnamed parameters: 7 -1 2"
|
||||||
"typedefs: hi 4 2"
|
"typedefs: hi 4 2"
|
||||||
"sizeof a pointer type: 1 1")
|
"sizeof a pointer type: 1 1"
|
||||||
|
"dimensions: 6 12 5")
|
||||||
(return 0)
|
(return 0)
|
||||||
|
|
||||||
;;; Three questions about a written type that used to be answered in
|
;;; Three questions about a written type that used to be answered in
|
||||||
@@ -95,4 +96,9 @@
|
|||||||
(printf "sizeof a pointer type: %d %d\n"
|
(printf "sizeof a pointer type: %d %d\n"
|
||||||
(== (sizeof (* const char)) (sizeof greeting))
|
(== (sizeof (* const char)) (sizeof greeting))
|
||||||
(== (sizeof (* greeting)) 1))
|
(== (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))
|
(return 0))
|
||||||
|
|||||||
53
types.scm
53
types.scm
@@ -64,7 +64,9 @@
|
|||||||
arglist-types
|
arglist-types
|
||||||
strip-pub
|
strip-pub
|
||||||
array-bound?
|
array-bound?
|
||||||
array-element-type)
|
array-element-type
|
||||||
|
array-bound
|
||||||
|
nest-array-type)
|
||||||
|
|
||||||
;;; Two namespaces: `struct point' and a `point' typedef are separate
|
;;; Two namespaces: `struct point' and a `point' typedef are separate
|
||||||
;;; declarations. Tags -- struct, union and enum alike -- share the
|
;;; declarations. Tags -- struct, union and enum alike -- share the
|
||||||
@@ -486,6 +488,9 @@
|
|||||||
(let ((bound (last type))
|
(let ((bound (last type))
|
||||||
(preceding (last (drop-right type 1))))
|
(preceding (last (drop-right type 1))))
|
||||||
(cond
|
(cond
|
||||||
|
((and (pair? bound) (every pair? bound)
|
||||||
|
(aggregate-head? (drop-right (cdr type) 1)))
|
||||||
|
#f)
|
||||||
((not (symbol? bound)) #t)
|
((not (symbol? bound)) #t)
|
||||||
((or (memq bound +c-specifiers+) (memq bound +c-qualifiers+)) #f)
|
((or (memq bound +c-specifiers+) (memq bound +c-qualifiers+)) #f)
|
||||||
((memq preceding '(* struct union enum)) #f)
|
((memq preceding '(* struct union enum)) #f)
|
||||||
@@ -496,14 +501,52 @@
|
|||||||
(drop-right (cdr type) 2)))
|
(drop-right (cdr type) 2)))
|
||||||
(else #t)))))
|
(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:
|
;;; 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)
|
(define (array-element-type type)
|
||||||
(and (pair? type)
|
(and (pair? type)
|
||||||
(eq? '¤ (car type))
|
(eq? '¤ (car type))
|
||||||
(pair? (cdr type))
|
(pair? (cdr type))
|
||||||
(let ((words (if (array-bound? type)
|
(let* ((type (nest-array-type type))
|
||||||
(drop-right (cdr type) 1)
|
(words (if (array-bound? type)
|
||||||
(cdr type))))
|
(drop-right (cdr type) 1)
|
||||||
|
(cdr type))))
|
||||||
(and (pair? words)
|
(and (pair? words)
|
||||||
(if (null? (cdr words)) (car words) words)))))
|
(if (null? (cdr words)) (car words) words)))))
|
||||||
|
|||||||
Reference in New Issue
Block a user