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, string -- 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 str 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)
|