flan/test/programs/dyn-char.flan

52 lines
1.6 KiB
Plaintext

;;;; Dyn chars: a char literal that ends up dyn is a char and prints as its
;;;; literal; a dyn text counts characters, not bytes; chars and text convert
;;;; between a text and a vec of chars. With an argument, a dyn text handed to
;;;; an i32 traps at the call, and with "big" an int past an i32's range does.
(defn show [x] () (println x))
(defn code-point [c i32] i32 c)
(defn main [args [str]] i32
(let [t (the dyn "é日😀 ok")
cs (chars t)]
;; literals, printed
(show \I)
(show \é)
(show \日)
(show \😀)
(show [\a \space \( \newline])
(show (type-of \é))
;; length and indexing count characters
(show (length t))
(show (at t 0))
(show (at t 1))
(show (at t 2))
(show (at t 4))
(show (slice t 1 3))
;; chars and text
(show cs)
(show (length cs))
(show (text cs))
(show (= (text cs) t))
(show (text \日))
(show (text [\o \k]))
;; equality, ordering, and a char as a map key
(show (= (at t 1) \日))
(show (= (at t 0) \e))
(show (= (at t 4) (the dyn "o")))
(show (= (at t 4) (the dyn 111)))
(show (< (at t 0) (at t 1)))
(show (> (at t 1) (at t 2)))
(show (< (at t 4) \é))
(show (get {\é 1 \日 2} (at t 1)))
;; into typed code: the code point
(show (code-point (at t 2)))
;; an int that fits goes in too, and a cast agrees with the crossing
(show (code-point (the dyn 5)))
(show (i32 (the dyn \a)))
(when (> (length args) 1)
(if (= (at args 1) "big")
(show (code-point (the dyn 5000000000)))
(show (code-point (the dyn "x"))))))
0)