forked from alex-eg/sex
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:
53
types.scm
53
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)))))
|
||||
|
||||
Reference in New Issue
Block a user