1
0
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:
2026-10-14 17:14:36 +03:00
parent 427177d50f
commit 9c5f678830
3 changed files with 134 additions and 10 deletions

View File

@@ -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))