Make (defmain ...) a clean macro
This commit is contained in:
parent
2ad8d444a8
commit
509b72a799
@ -200,16 +200,14 @@
|
|||||||
|
|
||||||
(defmacro defmain [args &rest body]
|
(defmacro defmain [args &rest body]
|
||||||
"Write a function named \"main\" and do the if __main__ dance"
|
"Write a function named \"main\" and do the if __main__ dance"
|
||||||
(let [[retval (gensym)]]
|
(let [[retval (gensym)]
|
||||||
`(do
|
[mainfn `(fn [~@args]
|
||||||
(defn main [~@args]
|
~@body)]]
|
||||||
~@body)
|
`(when (= --name-- "__main__")
|
||||||
|
(import sys)
|
||||||
(when (= --name-- "__main__")
|
(setv ~retval (apply ~mainfn sys.argv))
|
||||||
(import sys)
|
(if (integer? ~retval)
|
||||||
(setv ~retval (apply main sys.argv))
|
(sys.exit ~retval)))))
|
||||||
(if (integer? ~retval)
|
|
||||||
(sys.exit ~retval))))))
|
|
||||||
|
|
||||||
|
|
||||||
(defmacro-alias [defn-alias defun-alias] [names lambda-list &rest body]
|
(defmacro-alias [defn-alias defun-alias] [names lambda-list &rest body]
|
||||||
|
@ -259,3 +259,19 @@
|
|||||||
|
|
||||||
(defn test-botsbuildbots []
|
(defn test-botsbuildbots []
|
||||||
(assert (> (len (first (Botsbuildbots))) 50)))
|
(assert (> (len (first (Botsbuildbots))) 50)))
|
||||||
|
|
||||||
|
|
||||||
|
(defn test-defmain []
|
||||||
|
"NATIVE: make sure defmain is clean"
|
||||||
|
(global --name--)
|
||||||
|
(setv oldname --name--)
|
||||||
|
(setv --name-- "__main__")
|
||||||
|
(defn main []
|
||||||
|
(print 'Hy)
|
||||||
|
42)
|
||||||
|
(try
|
||||||
|
(defmain [&rest args]
|
||||||
|
(main))
|
||||||
|
(except [e SystemExit]
|
||||||
|
(assert (= (str e) "42"))))
|
||||||
|
(setv --name-- oldname))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user