flan/test/programs/int-float.flan
Joseph Ferano a4c6b996ff def re-runs its initialiser, and defvar is renamed defonce
The trio the author decided on 2026-09-20 is now all built: def is CL's
defparameter — its initialiser runs on every daemon re-run, unguarded, so
an edited initialiser repaints the same storage on C-c C-c plus re-run —
defonce (Clojure's name for CL's defvar, per the author) initialises once
behind the .init~once. flag, and defconst stays the image.

One parse arm reads both forms; the difference is Ast.reinit, carried to
Tast.global's grerun. Emit.startup_plan gives a def no guard flag, and
Check.check_global lifts every def initialiser — zero and literal
included — into global/<n>, so the host's startup reaches it through the
function cell and a re-evaluated def swaps it (Session's def_inits;
Emit.redefinition declares the cell for a non-sibling target). The old
defvar spelling is refused with the rename and both compiling spellings,
and every program, test, doc and editor list is swept — except sand.flan,
the author's live WIP, whose seven defvar lines are flagged in FIX.org
and keep its three dependent tests red on this branch.
2026-09-21 07:12:04 +07:00

64 lines
2.3 KiB
Plaintext

;;;; The two builtin aliases: int is i32 and float is f32.
;;;;
;;;; Not a prelude defalias -- an entry in Types.ikind_of_name and
;;;; Types.fkind_of_name, so the name IS the machine type rather than a second
;;;; name for it. What that buys is what this program exercises: every
;;;; position that takes i32 takes int, the cast head included, and nothing
;;;; downstream of the checker ever hears the word.
;;;;
;;;; The rest of the foreign spellings -- long, double, integer, str -- are
;;;; still refusals that teach the Flan name, which a program cannot show
;;;; because it would not compile; that half is in test_flan.ml.
(defstruct Point [x int y float])
;;; A type alias over one: the alias machinery sees a resolved i32, exactly as
;;; if (Vec i32) had been written.
(defalias Row (Vec int))
;;; A zeroed static, from the three-element defonce whose third element is read
;;; as a type and not as a value.
(defonce total int)
;;; Both spellings in one signature, to make the point that they are the same
;;; two types and not a parallel pair.
(defn mix [a int b i32 c float d f32] int
(+ a b (int c) (int d)))
(defn main [] i32
;; The cast head, which is the position a prelude alias could not have
;; reached: Check.is_cast asks Types.ikind_of_name and Types.fkind_of_name
;; whether the head names a primitive, and never looks in the alias table.
(let [n (int 7)
f (float 2.5)
back (int f) ; f32 -> i32, truncating
wide (i64 n)]
(println n) ; 7
(println f) ; 2.5
(println back) ; 2
(println wide)) ; 7
;; Generic type arguments: the element type of a Vec and both halves of a
;; Map, named with the alias.
(let [v (vec-new int)
m (map-new string int)]
(push v 10)
(push v 20)
(put m "k" 30)
(println (at v 0)) ; 10
(println (at v 1)) ; 20
(println (match (get m "k") (Some n) n None -1))) ; 30
;; Struct fields, written and read.
(let [p (Point {.x 3 .y 1.5})]
(println (.x p)) ; 3
(println (.y p))) ; 1.5
;; The zeroed global, before and after.
(println total) ; 0
(set total 41)
(println (+ total 1)) ; 42
(println (mix 1 2 3.5 4.5)) ; 10
0)