Skip to content

Commit 575e2f4

Browse files
committed
Improve trace and assert macros to display expressions, use prn, and support custom messages
1 parent d718653 commit 575e2f4

3 files changed

Lines changed: 37 additions & 36 deletions

File tree

core/Debug.carp

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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))))))

core/Dynamic.carp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,6 @@ integers [`imod`](#imod).")
7373
(defndynamic tail [s]
7474
(String.suffix s 1))
7575
)
76-
77-
(defmodule Debug
78-
(doc trace "prints the value of an expression to `stdout`, then returns its value.")
79-
(defmacro trace [x]
80-
(let [sym (gensym)]
81-
`(let-do [%sym %x]
82-
; we use eval here to ensure we resolve the symbol before putting it
83-
; into file, line, and column
84-
(macro-log
85-
%(eval `(file %x)) ":"
86-
%(eval `(line %x)) ":"
87-
%(eval `(column %x)) ": "
88-
%sym)
89-
%sym))
90-
)
91-
)
9276
)
9377

9478

test/trace_assert_check.carp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
;; This file verifies the improved 'trace' and 'assert' macros in core/Debug.carp
2+
3+
(defn main []
4+
(let [x (Debug.trace (+ 10 20))
5+
y (Debug.trace @"hello")]
6+
(do
7+
(assert (= x 30))
8+
(assert (= x 30) "x must be 30")
9+
;; Print success message
10+
(IO.println "All test assertions passed successfully!")
11+
())))

0 commit comments

Comments
 (0)