2013-05-11 20:57:46 +02:00
|
|
|
(defmacro rev [&rest body]
|
|
|
|
"Execute the `body` statements in reverse"
|
|
|
|
(quasiquote (do (unquote-splice (list (reversed body))))))
|
|
|
|
|
|
|
|
|
2013-05-11 19:40:48 +02:00
|
|
|
(defn test-rev-macro []
|
|
|
|
"NATIVE: test stararged native macros"
|
|
|
|
(setv x [])
|
|
|
|
(rev (.append x 1) (.append x 2) (.append x 3))
|
|
|
|
(assert (= x [3 2 1])))
|
2013-06-05 12:19:06 +02:00
|
|
|
|
|
|
|
; Macros returning constants
|
|
|
|
|
|
|
|
(defmacro an-int [] 42)
|
|
|
|
(assert (= (an-int) 42))
|
|
|
|
|
2013-06-07 16:35:28 +02:00
|
|
|
(defmacro a-true [] True)
|
|
|
|
(assert (= (a-true) True))
|
|
|
|
(defmacro a-false [] False)
|
|
|
|
(assert (= (a-false) False))
|
|
|
|
|
2013-06-05 12:19:06 +02:00
|
|
|
(defmacro a-float [] 42.)
|
|
|
|
(assert (= (a-float) 42.))
|
|
|
|
|
|
|
|
(defmacro a-complex [] 42j)
|
|
|
|
(assert (= (a-complex) 42j))
|
|
|
|
|
|
|
|
(defmacro a-string [] "foo")
|
|
|
|
(assert (= (a-string) "foo"))
|
|
|
|
|
|
|
|
(defmacro a-list [] [1 2])
|
|
|
|
(assert (= (a-list) [1 2]))
|
|
|
|
|
|
|
|
(defmacro a-dict [] {1 2})
|
|
|
|
(assert (= (a-dict) {1 2}))
|
2013-06-02 17:21:03 +02:00
|
|
|
|
|
|
|
; A macro calling a previously defined function
|
|
|
|
(eval-when-compile
|
|
|
|
(defn foo [x y]
|
|
|
|
(quasiquote (+ (unquote x) (unquote y)))))
|
|
|
|
|
|
|
|
(defmacro bar [x y]
|
|
|
|
(foo x y))
|
|
|
|
|
|
|
|
(defn test-fn-calling-macro []
|
|
|
|
"NATIVE: test macro calling a plain function"
|
|
|
|
(assert (= 3 (bar 1 2))))
|
2013-06-05 11:41:58 +02:00
|
|
|
|
2013-07-14 19:03:08 +02:00
|
|
|
(defn test-midtree-yield []
|
|
|
|
"NATIVE: test yielding with a returnable"
|
|
|
|
(defn kruft [] (yield) (+ 1 1)))
|
|
|
|
|
|
|
|
(defn test-midtree-yield-in-for []
|
|
|
|
"NATIVE: test yielding in a for with a return"
|
|
|
|
(defn kruft-in-for []
|
|
|
|
(for [i (range 5)]
|
|
|
|
(yield i))
|
|
|
|
(+ 1 2)))
|
|
|
|
|
|
|
|
(defn test-midtree-yield-in-while []
|
|
|
|
"NATIVE: test yielding in a while with a return"
|
|
|
|
(defn kruft-in-while []
|
|
|
|
(setv i 0)
|
|
|
|
(while (< i 5)
|
|
|
|
(yield i)
|
|
|
|
(setv i (+ i 1)))
|
|
|
|
(+ 2 3)))
|
|
|
|
|
|
|
|
(defn test-multi-yield []
|
|
|
|
"NATIVE: testing multiple yields"
|
|
|
|
(defn multi-yield []
|
|
|
|
(for [i (range 3)]
|
|
|
|
(yield i))
|
|
|
|
(yield "a")
|
|
|
|
(yield "end"))
|
|
|
|
(assert (= (list (multi-yield)) [0 1 2 "a" "end"])))
|
|
|
|
|
|
|
|
|
2013-06-05 11:41:58 +02:00
|
|
|
; Macro that checks a variable defined at compile or load time
|
|
|
|
(setv phase "load")
|
|
|
|
(eval-when-compile
|
|
|
|
(setv phase "compile"))
|
|
|
|
(defmacro phase-when-compiling [] phase)
|
|
|
|
(assert (= phase "load"))
|
|
|
|
(assert (= (phase-when-compiling) "compile"))
|
|
|
|
|
2013-06-07 16:30:00 +02:00
|
|
|
(setv initialized False)
|
|
|
|
(eval-and-compile
|
|
|
|
(setv initialized True))
|
|
|
|
(defmacro test-initialized [] initialized)
|
|
|
|
(assert initialized)
|
|
|
|
(assert (test-initialized))
|
|
|
|
|
2013-08-17 17:37:48 +02:00
|
|
|
|
|
|
|
(defn test-yield-from []
|
|
|
|
"NATIVE: testing yield from"
|
|
|
|
(defn yield-from-test []
|
|
|
|
(for [i (range 3)]
|
|
|
|
(yield i))
|
|
|
|
(yield-from [1 2 3]))
|
|
|
|
(assert (= (list (yield-from-test)) [0 1 2 1 2 3])))
|