flan/web/examples/arrays.flan
Joseph Ferano dfd64d89ea The examples are files that run, not prose, so the page cannot drift from them
Copying a snippet into HTML is where a documented language stops being the
real one. Each block on the page is a program here with its recorded output
beside it, and check.sh is what says the page is still true after a change.
2026-09-12 03:40:03 +07:00

24 lines
814 B
Plaintext

(defconst rows 3)
(defconst cols 4)
;; A fixed array is a value: inline storage, copies on assignment.
(defconst palette [4 u32] [0xE6B800FF 0x3B6E8CFF 0xA83232FF 0xCC6B1FFF])
;; No initialiser means all-bytes-zero, so this is BSS and costs nothing.
(defvar grid [rows [cols i32]])
(defn main []
(set (at grid 1 2) 7)
(print-i64 (i64 (at grid 1 2))) (newline) ; 7
(print-i64 (i64 (len palette))) (newline) ; 4
;; A slice is ptr+len and non-owning: it views the array, it does not copy it.
(let [row (slice (at grid 1) 0 cols)]
(set (at row 0) 5)
(print-i64 (i64 (at grid 1 0))) (newline) ; 5 — the same storage
(print-i64 (sum-i32 row)) (newline)) ; 12
;; (zeroed) is a memset, not an allocation.
(set grid (zeroed))
(print-i64 (i64 (at grid 1 2))) (newline)) ; 0