Proper 'assert' macro that reports source location.

This commit is contained in:
Erik Svedäng 2018-02-06 10:12:43 +01:00
parent ed095b1baa
commit a25606725e
3 changed files with 16 additions and 8 deletions

View File

@ -29,11 +29,13 @@
(IO.println &(str &x))
x))
;; TODO: Should be a macro and provide the location of the assertion.
;; Also its output looks weird because of lack of 'prn'.
(defn assert-eq [expected got]
(if (= expected got)
()
(println* "ASSERTION FAILED! Expected " expected " but got " got)))
)
(defmacro assert [expr]
(list 'if (list '= true expr)
()
(list 'do
(list 'IO.println (list 'ref
(list 'String.append (list 'String.copy "Assertion failed ")
(list 'String.copy (source-location)))))
'(System.exit 1))))

View File

@ -6,3 +6,9 @@
;; (defn f []
;; (for [n 1.4f 123.2f] ;; Should report error here, not in Macros.carp
;; ()))
(defn f [x]
(assert x))
(defn main []
(f false))

View File

@ -8,4 +8,4 @@
;; The one allocation left after 'carp_init_globals' should be 'g' itself:
(defn main []
(Debug.assert-eq 1l (Debug.memory-balance)))
(assert (= 1l (Debug.memory-balance))))