hand an array field its elements
An array field compiled to C that cc refused: an array is neither an initializer of another nor something a payload literal can be built from. Both the constructor and the binder now spell it out element by element, which needs a length sexc can count. A sum holding itself by value is refused at the declaration rather than as cc's incomplete type.
This commit is contained in:
86
semen.scm
86
semen.scm
@@ -697,10 +697,55 @@
|
||||
(unless (= (length fields) (length args))
|
||||
(sex-error form "variant takes this many fields" tag (length fields)))
|
||||
(sum-literal name tag
|
||||
(map (lambda (field arg)
|
||||
(convert-to-closure (second field) arg env form))
|
||||
(map (lambda (field arg) (field-argument field arg env form))
|
||||
fields args))))
|
||||
|
||||
;;; An array field cannot be handed an array, C copying neither, so it
|
||||
;;; is handed the array's elements:
|
||||
;;;
|
||||
;;; (shape/row #(1 2 3 4)) -> ...(struct shapeːrow){{1, 2, 3, 4}}
|
||||
;;; (shape/row a) -> ...(struct shapeːrow){{a[0], a[1], a[2], a[3]}}
|
||||
(define (field-argument field arg env form)
|
||||
(let ((type (second field)))
|
||||
(if (not (array-form? type))
|
||||
(convert-to-closure type arg env form)
|
||||
(let ((given (expression-type arg env)))
|
||||
(cond
|
||||
((brace-initializer? arg) arg)
|
||||
((not (array-form? given))
|
||||
(sex-error form "an array field takes an array" (first field)))
|
||||
((not (eqv? (array-length given) (array-length type)))
|
||||
(sex-error form "array is not as long as its field" (first field)))
|
||||
;; `#([int 4] : 1 2 3 4)' is its own elements
|
||||
((vector? arg) (list->vector (cdr (memq ': (vector->list arg)))))
|
||||
((not (simple-path? arg))
|
||||
(sex-error form "an array is read once per element; bind it to a var first"
|
||||
(first field)))
|
||||
(else (spell-out-array type arg)))))))
|
||||
|
||||
(define (array-form? type)
|
||||
(and (pair? type) (eq? '¤ (car type))))
|
||||
|
||||
;;; [int 4] -> 4, and [int N] -> 4 after (define N 4); #f for a length
|
||||
;;; nothing here can count
|
||||
(define (array-length type)
|
||||
(and (array-bound? type)
|
||||
(let ((bound (last type)))
|
||||
(cond ((and (integer? bound) (exact? bound)) bound)
|
||||
((symbol? bound)
|
||||
(let ((info (get-type-info bound)))
|
||||
(and info (eq? 'define (car info))
|
||||
(let ((value (caddr info)))
|
||||
(and (= 1 (length value))
|
||||
(integer? (car value))
|
||||
(exact? (car value))
|
||||
(car value))))))
|
||||
(else #f)))))
|
||||
|
||||
;;; xs : [int 2] -> #((¤ xs 0) (¤ xs 1))
|
||||
(define (spell-out-array type expr)
|
||||
(list->vector (map (lambda (i) `(¤ ,expr ,i)) (iota (array-length type)))))
|
||||
|
||||
;;; The forms a macro call stands for, with `(type-of x)' answered from
|
||||
;;; ENV
|
||||
(define (expand-call form env)
|
||||
@@ -867,6 +912,9 @@
|
||||
(else (case (car expr)
|
||||
((dot-access %->) (simple-path? (second expr)))
|
||||
((*) (and (= 2 (length expr)) (simple-path? (second expr))))
|
||||
((¤) (and (= 3 (length expr))
|
||||
(simple-path? (second expr))
|
||||
(or (symbol? (third expr)) (number? (third expr)))))
|
||||
(else #f)))))
|
||||
|
||||
(define (match-arm-variant arm) (and (pair? (car arm)) (car (car arm))))
|
||||
@@ -916,13 +964,18 @@
|
||||
,@(cdr arm))))))
|
||||
arms)))
|
||||
|
||||
;;; A binder is a copy of its field, an array one included:
|
||||
;;;
|
||||
;;; (var xs [int 2] #((¤ s.u.row.xs 0) (¤ s.u.row.xs 1)))
|
||||
(define (match-bindings scrutinee variant binders fields)
|
||||
(filter-map
|
||||
(lambda (binder field)
|
||||
(and (not (eq? binder '_))
|
||||
`(var ,binder ,(second field)
|
||||
(dot-access (dot-access (dot-access ,scrutinee u) ,variant)
|
||||
,(first field)))))
|
||||
(let ((type (second field))
|
||||
(path `(dot-access (dot-access (dot-access ,scrutinee u) ,variant)
|
||||
,(first field))))
|
||||
`(var ,binder ,type
|
||||
,(if (array-form? type) (spell-out-array type path) path)))))
|
||||
binders fields))
|
||||
|
||||
;;; (.length v) is a call to vec2ːlength; (. v length) is the field
|
||||
@@ -1648,6 +1701,10 @@
|
||||
(unless (and (symbol? name) (list? declared) (pair? declared))
|
||||
(sex-error form "sum needs a name and a list of variants" form))
|
||||
(let ((variants (map (lambda (v) (parse-variant form v)) declared)))
|
||||
(for-each (lambda (v)
|
||||
(for-each (lambda (field) (check-variant-field form name field))
|
||||
(cdr v)))
|
||||
variants)
|
||||
(let* ((names (map car variants))
|
||||
(twice (find (lambda (v) (> (count (cut eq? <> v) names) 1)) names)))
|
||||
(when twice
|
||||
@@ -1666,6 +1723,25 @@
|
||||
(sex-error form "a variant is (name (fields ...))" v))
|
||||
(cons (car v) (normalize-fields (cadr v))))
|
||||
|
||||
;;; (sum expr ((neg ((e expr))))) has no size; (neg ((e (* expr)))) does.
|
||||
;;; An array field is spelled out element by element wherever it is
|
||||
;;; copied, so its length has to be one sexc can count.
|
||||
(define (check-variant-field form name field)
|
||||
(let ((type (second field)))
|
||||
(when (holds-by-value? type name)
|
||||
(sex-error form "a sum holds itself through a pointer" (first field)))
|
||||
(when (and (array-form? type) (not (array-length type)))
|
||||
(sex-error form "array field needs a length written as a number"
|
||||
(first field)))))
|
||||
|
||||
(define (holds-by-value? type name)
|
||||
(cond ((eq? type name) #t)
|
||||
((not (pair? type)) #f)
|
||||
((memq (car type) '(struct const volatile))
|
||||
(and (pair? (cdr type)) (holds-by-value? (cadr type) name)))
|
||||
((array-form? type) (holds-by-value? (array-element-type type) name))
|
||||
(else #f)))
|
||||
|
||||
(define (sum-forms name variants)
|
||||
(let ((carried (filter (lambda (v) (pair? (cdr v))) variants)))
|
||||
`(;; forward, so a variant may hold a pointer back to the sum
|
||||
|
||||
Reference in New Issue
Block a user