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
|
||||
|
||||
@@ -216,6 +216,35 @@
|
||||
(test-assert "and is not a function value"
|
||||
(reports? "(sum t ((a ((x int)))))(fn f () void (var g (fn ((x int)) t) t/a))"
|
||||
"a constructor is not a function"))
|
||||
(test-assert "an array field takes a brace literal as it is"
|
||||
(emits? "(sum t ((a ((xs [int 2])))))(fn f () t (return (t/a #(1 2))))"
|
||||
"(struct tːa){{1, 2}}"))
|
||||
(test-assert "and an array by its elements"
|
||||
(emits? "(sum t ((a ((xs [int 2])))))(fn f () t (var v [int 2] #(1 2)) (return (t/a v)))"
|
||||
"(struct tːa){{v[0], v[1]}}"))
|
||||
(test-assert "which have to be countable"
|
||||
(reports? "(fn n () int (return 2))(sum t ((a ((xs [int (n)])))))"
|
||||
"array field needs a length"))
|
||||
(test-assert "and as many as the field holds"
|
||||
(reports? "(sum t ((a ((xs [int 2])))))(fn f () t (var v [int 3] #(1 2 3)) (return (t/a v)))"
|
||||
"array is not as long as its field"))
|
||||
(test-assert "an array field takes an array"
|
||||
(reports? "(sum t ((a ((xs [int 2])))))(fn f () t (var p (* int) 0) (return (t/a p)))"
|
||||
"an array field takes an array"))
|
||||
(test-assert "read from a path, not from a call"
|
||||
(reports? (string-append "(sum t ((a ((xs [int 2])))))(fn g () int (return 0))"
|
||||
"(fn f () t (var m [t 2]) (return (t/a (. [m (g)] u a xs))))")
|
||||
"bind it to a var first"))
|
||||
(test-assert "an array binder is a copy of each element"
|
||||
(emits? (string-append "(sum t ((a ((xs [int 2])))))"
|
||||
"(fn f ((s t)) int (match s ((a (xs)) (return [xs 0]))))")
|
||||
"int xs[2] = {s.u.a.xs[0], s.u.a.xs[1]}"))
|
||||
(test-assert "a sum cannot hold itself"
|
||||
(reports? "(sum box ((inner ((x box)))))" "a sum holds itself through a pointer"))
|
||||
(test-assert "nor an array of itself"
|
||||
(reports? "(sum box ((inner ((x [box 2])))))" "a sum holds itself through a pointer"))
|
||||
(test-assert "though it can point at itself"
|
||||
(emits? "(sum box ((inner ((x (* box))))))" "box * x;"))
|
||||
(test-assert "a function argument becomes the closure its field holds"
|
||||
(emits? (string-append
|
||||
"(fn g ((n int)) int (return n))"
|
||||
|
||||
@@ -2,14 +2,15 @@
|
||||
(output "3.14 12.00 0.00"
|
||||
"held once: a 1"
|
||||
"nested: -(2 + 3) = -5"
|
||||
"tagged only: 1")
|
||||
"tagged only: 1"
|
||||
"rows: 18 5")
|
||||
(return 0)
|
||||
|
||||
;;; A sum is a tag enum, a payload struct per variant that carries
|
||||
;;; fields, the record holding both, and a constructor each. match is
|
||||
;;; a switch over the tag whose arms bind the payload as locals, and
|
||||
;;; the compiler refuses one that leaves a variant out -- which is the
|
||||
;;; thing the form exists for.
|
||||
;;; fields, and the record holding both; a constructor is the value
|
||||
;;; itself, written in place. match is a switch over the tag whose arms
|
||||
;;; bind the payload as locals, and the compiler refuses one that leaves
|
||||
;;; a variant out -- which is the thing the form exists for.
|
||||
|
||||
(include stdio.h)
|
||||
|
||||
@@ -52,6 +53,12 @@
|
||||
(sum held ((a ((x int) (y int))) (b ())))
|
||||
(fn mk-held () held (return (held/a 1 2)))
|
||||
|
||||
;;; An array field is handed elements, never an array: C copies neither
|
||||
;;; into a parameter nor into another array, so both the constructor
|
||||
;;; and the binder spell it out
|
||||
(define N 4)
|
||||
(sum row ((cells ((xs [int N]))) (none ())))
|
||||
|
||||
(pub fn main () int
|
||||
(printf "%.2f %.2f %.2f\n"
|
||||
(area (mk-shape))
|
||||
@@ -75,4 +82,16 @@
|
||||
(match l
|
||||
((red ()) (printf "tagged only: 0\n"))
|
||||
((green ()) (printf "tagged only: 1\n")))
|
||||
|
||||
(var a [int N] #(5 6 7 8))
|
||||
(var rows [row 2] #((row/cells a) (row/cells #(1 2 3 4))))
|
||||
(= [a 0] 50) ; the row holds a copy
|
||||
(var sum int 0)
|
||||
(for (var i int 0) (< i 2) (++ i)
|
||||
(match [rows i]
|
||||
((cells (xs))
|
||||
(= [xs 0] 100) ; and so does the binder
|
||||
(+= sum (+ [xs 1] [xs 2])))
|
||||
((none ()) (+= sum 0))))
|
||||
(printf "rows: %d %d\n" sum (. [rows 0] u cells [xs 0]))
|
||||
(return 0))
|
||||
|
||||
Reference in New Issue
Block a user