flan/test/programs/macro-unless.flan

45 lines
1.5 KiB
Plaintext

;;;; unless, which used to be a special form in parse.ml and is a defmacro in
;;;; the prelude now. plan.org milestone 5 says the five conditional sugars are
;;;; special forms only until macros land; this is the first one to stop being
;;;; one, and running this file means the expander compiled a macro into a
;;;; shared object, dlopened it into the compiler, and called it -- before the
;;;; first line below was parsed.
;;;;
;;;; Nothing here is new syntax. Every line of it compiled the same way before
;;;; the move, which is the point: the test for the feature is the corpus that
;;;; was written against the special form.
(defn classify [n i32] str
(let [out "even"]
(unless (= 0 (% n 2))
(set out "odd"))
out))
(defn main [] i32
;; One body form, the common case.
(unless false (println "the test was false"))
(unless true (println "NOT PRINTED"))
;; Several, which is what the do in the expansion is for.
(unless false
(print "a")
(print "b")
(println "c"))
;; A computed test, so the argument is a form the macro had to put back
;; rather than a literal it could have ignored.
(let [n 7]
(unless (< n 3) (println "7 is not less than 3")))
;; Inside a function that returns a value, and inside a loop: the expansion
;; is an if with no else, so it is () and it does not decide the body's
;; value.
(println (classify 4))
(println (classify 5))
(let [seen 0]
(dotimes [i 5]
(unless (= i 2) (set seen (+ seen 1))))
(print seen) (println ""))
0)