@@ -39,31 +39,37 @@ immediately, raising a `SIGABRT` if it fails.")
3939 (IO.println &(fmt "Invalid memory balance: %d" (Debug.memory-balance)))
4040 (ignore (System.exit 1)))))))
4141
42- (doc trace "prints the value of an expression to `stdout`, then returns its value.")
42+ (doc trace "prints the expression and its value to `stdout`, then returns its value.")
4343 (defmacro trace [x]
44- (let [sym (gensym)]
44+ (let [sym (gensym)
45+ x-str (str x)]
4546 `(let-do [%sym %x]
46- ; we use eval here to ensure we resolve the symbol before putting it
47- ; into file, line, and column
48- (IO.println
49- (ref
50- (fmt "%s:%d:%d: %s"
51- %(eval `(file %x))
52- %(eval `(line %x))
53- %(eval `(column %x))
54- &(str %sym))))
55- %sym)
56- )
57- )
47+ (IO.println
48+ (ref
49+ (fmt "%s:%d:%d: %s = %s"
50+ %(eval `(file %x))
51+ %(eval `(line %x))
52+ %(eval `(column %x))
53+ %x-str
54+ &(prn &%sym))))
55+ %sym)))
5856
5957 (doc leak-array "leaks some memory. This function is useful for testing tools that detect leaks.")
6058 (register leak-array (Fn [a] ()) "Debug_leak_MINUS_array")
6159
6260)
6361
64- ;; Crash the program with an error message unless the expression evaluates to 'true'.
65- (defmacro assert [expr]
66- `(unless (= true %expr)
67- (do
68- (println* (fmt "Assertion '%s' failed at line %d, column %d in file %s" %(str expr) %(line) %(column) %(file)))
69- (System.abort))))
62+ (doc assert "Crash the program with an error message unless the expression evaluates to 'true'. Supports an optional custom message.")
63+ (defmacro assert [expr :rest msg]
64+ (let [has-msg (> (length msg) 0)]
65+ (if has-msg
66+ (let [custom-msg (car msg)]
67+ `(unless (= true %expr)
68+ (let [msg-val (str %custom-msg)]
69+ (do
70+ (println* (fmt "Assertion '%s' failed: %s at line %d, column %d in file %s" %(str expr) &msg-val %(eval `(line %expr)) %(eval `(column %expr)) %(eval `(file %expr))))
71+ (System.abort)))))
72+ `(unless (= true %expr)
73+ (do
74+ (println* (fmt "Assertion '%s' failed at line %d, column %d in file %s" %(str expr) %(eval `(line %expr)) %(eval `(column %expr)) %(eval `(file %expr))))
75+ (System.abort))))))
0 commit comments