forked from alex-eg/sex
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:
@@ -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