Rewrite _with

This commit is contained in:
Kodi Arfer 2019-08-02 14:53:57 -04:00
parent 6cced31738
commit 9ddb3b1031
2 changed files with 14 additions and 16 deletions

View File

@ -48,16 +48,14 @@ be associated in pairs."
(defn _with [node args body] (defn _with [node args body]
(if (not (empty? args)) (if
(do (not args)
(if (>= (len args) 2) `(do ~@body)
(do (<= (len args) 2)
(setv p1 (.pop args 0) `(~node [~@args] ~@body)
p2 (.pop args 0) True (do
primary [p1 p2]) (setv [p1 p2 #* args] args)
`(~node [~@primary] ~(_with node args body))) `(~node [~p1 ~p2] ~(_with node args body)))))
`(~node [~@args] ~@body)))
`(do ~@body)))
(defmacro with [args &rest body] (defmacro with [args &rest body]

View File

@ -43,19 +43,19 @@
(assert (= (macroexpand-all '(foo-walk)) (assert (= (macroexpand-all '(foo-walk))
42)) 42))
(assert (= (macroexpand-all '(with [a 1])) (assert (= (macroexpand-all '(with [a 1]))
'(with* [a 1] (do)))) '(with* [a 1])))
(assert (= (macroexpand-all '(with [a 1 b 2 c 3] (for [d c] foo))) (assert (= (macroexpand-all '(with [a 1 b 2 c 3] (for [d c] foo)))
'(with* [a 1] (with* [b 2] (with* [c 3] (do (for [d c] foo))))))) '(with* [a 1] (with* [b 2] (with* [c 3] (for [d c] foo))))))
(assert (= (macroexpand-all '(with [a 1] (assert (= (macroexpand-all '(with [a 1]
'(with [b 2]) '(with [b 2])
`(with [c 3] `(with [c 3]
~(with [d 4]) ~(with [d 4])
~@[(with [e 5])]))) ~@[(with [e 5])])))
'(with* [a 1] '(with* [a 1]
(do '(with [b 2]) '(with [b 2])
`(with [c 3] `(with [c 3]
~(with* [d 4] (do)) ~(with* [d 4])
~@[(with* [e 5] (do))]))))) ~@[(with* [e 5])]))))
(defmacro require-macro [] (defmacro require-macro []
`(do `(do