Allow an empty (cond)
For easier macro writing purposes, allow an empty (cond), that simply returns nil. Closes #904. Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
This commit is contained in:
parent
649dd38040
commit
ab7d1be001
@ -67,26 +67,29 @@
|
|||||||
bar
|
bar
|
||||||
(if baz
|
(if baz
|
||||||
quux))"
|
quux))"
|
||||||
(setv branches (iter branches))
|
(if (empty? branches)
|
||||||
(setv branch (next branches))
|
nil
|
||||||
(defn check-branch [branch]
|
(do
|
||||||
"check `cond` branch for validity, return the corresponding `if` expr"
|
(setv branches (iter branches))
|
||||||
(if (not (= (type branch) HyList))
|
(setv branch (next branches))
|
||||||
(macro-error branch "cond branches need to be a list"))
|
(defn check-branch [branch]
|
||||||
(if (< (len branch) 2)
|
"check `cond` branch for validity, return the corresponding `if` expr"
|
||||||
(macro-error branch "cond branches need at least two items: a test and one or more code branches"))
|
(if (not (= (type branch) HyList))
|
||||||
(setv test (car branch))
|
(macro-error branch "cond branches need to be a list"))
|
||||||
(setv thebranch (cdr branch))
|
(if (< (len branch) 2)
|
||||||
`(if ~test (do ~@thebranch)))
|
(macro-error branch "cond branches need at least two items: a test and one or more code branches"))
|
||||||
|
(setv test (car branch))
|
||||||
|
(setv thebranch (cdr branch))
|
||||||
|
`(if ~test (do ~@thebranch)))
|
||||||
|
|
||||||
(setv root (check-branch branch))
|
(setv root (check-branch branch))
|
||||||
(setv latest-branch root)
|
(setv latest-branch root)
|
||||||
|
|
||||||
(for* [branch branches]
|
(for* [branch branches]
|
||||||
(setv cur-branch (check-branch branch))
|
(setv cur-branch (check-branch branch))
|
||||||
(.append latest-branch cur-branch)
|
(.append latest-branch cur-branch)
|
||||||
(setv latest-branch cur-branch))
|
(setv latest-branch cur-branch))
|
||||||
root)
|
root)))
|
||||||
|
|
||||||
|
|
||||||
(defmacro for [args &rest body]
|
(defmacro for [args &rest body]
|
||||||
|
@ -231,7 +231,8 @@
|
|||||||
"NATIVE: test if cond sorta works."
|
"NATIVE: test if cond sorta works."
|
||||||
(cond
|
(cond
|
||||||
[(= 1 2) (assert (is true false))]
|
[(= 1 2) (assert (is true false))]
|
||||||
[(is None None) (setv x true) (assert x)]))
|
[(is None None) (setv x true) (assert x)])
|
||||||
|
(assert (= (cond) nil)))
|
||||||
|
|
||||||
|
|
||||||
(defn test-index []
|
(defn test-index []
|
||||||
|
Loading…
x
Reference in New Issue
Block a user