aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2025-06-22 17:32:33 +0800
committerMistivia <i@mistivia.com>2025-06-22 17:32:33 +0800
commit665fb6be7eef3703c8e543645d09f329b9a466c5 (patch)
treeb88691d2d2185a78a66f6725e2d5a8a96fc5d085
parent47c8353366c5cd7544d182a897bacaa303c08d8e (diff)
better assert
-rw-r--r--src/primitives.c8
-rw-r--r--tests/math.lisp9
2 files changed, 11 insertions, 6 deletions
diff --git a/src/primitives.c b/src/primitives.c
index a0a90a0..1c0d1e3 100644
--- a/src/primitives.c
+++ b/src/primitives.c
@@ -6,7 +6,11 @@
SExpRef primitive_assert_error(Interp *interp, SExpRef args, bool istail) {
SExpRef eargs = lisp_eval_args(interp, args);
if (VALTYPE(eargs) == kErrSignal) return interp->t;
- return new_error(interp, "assert-error failed: no error.\n");
+
+ const char *expstr = lisp_to_string(interp, CAR(args));
+ SExpRef ret = new_error(interp, "assert-error failed, no error: %s.\n", expstr);
+ free((void*)expstr);
+ return ret;
}
SExpRef primitive_load(Interp *interp, SExpRef args, bool istail) {
@@ -63,7 +67,7 @@ SExpRef primitive_assert(Interp *interp, SExpRef args, bool istail) {
if (TRUEP(CAR(eargs))) {
return interp->t;
} else {
- const char *expstr = lisp_to_string(interp, args);
+ const char *expstr = lisp_to_string(interp, CAR(args));
SExpRef ret = new_error(interp, "Assertion failed: %s.\n", expstr);
free((void*)expstr);
return ret;
diff --git a/tests/math.lisp b/tests/math.lisp
index 53e4668..cbd5298 100644
--- a/tests/math.lisp
+++ b/tests/math.lisp
@@ -1,7 +1,8 @@
-(defun ~~ (a b)
- (if (< (abs (- a b)) 0.01)
- nil
- (error "failed")))
+(defun ~~f (a b)
+ (< (abs (- a b)) 0.01))
+
+(defmacro ~~ (a b)
+ `(assert (~~f ,a ,b)))
(assert (= 1 (abs -1)))
(assert (= 1.1 (abs -1.1)))