The author's exception to the foreign-spelling list: int is i32 and float is f32, and nothing else on that list moves. Spelled in Types.ikind_of_name and Types.fkind_of_name rather than as two prelude defaliases, because Check.is_cast asks those two functions and never the alias table — a prelude alias would have left (int x) with no reading while (i32 x) had one. Both names join primitive_names for the same reason one layer down: that list is what decides (vec-new int) and the three-element (defvar x int). Nothing reverses: ikind_name still says i32, so every message, signature, inspector line and DWARF name shows the machine type whichever spelling was written. A defalias restating the builtin is the no-op it says it is; one pointing the name anywhere else is refused, since the alias table is never consulted and the declaration would otherwise mean i32 in silence.
64 lines
2.3 KiB
Plaintext
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 defvar whose third element is read
|
|
;;; as a type and not as a value.
|
|
(defvar 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)
|