From 022666a8ff1429cb676f6c8d11edf7938c0e79c6 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Sun, 10 Nov 2019 16:08:58 +0100 Subject: [PATCH] core: Use exceptions instead of asserts. WIP * src/lib.c (assert_num, assert_struct): New function. * src/struct.c (struct_ref_): Use them. --- src/lib.c | 43 +++++++++++++++++++++++++++++++++++++++++++ src/struct.c | 8 ++++++-- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/lib.c b/src/lib.c index f8925536..2be271f3 100644 --- a/src/lib.c +++ b/src/lib.c @@ -173,3 +173,46 @@ integer_to_char (struct scm *x) { return make_char (x->value); } + +/* +void +assert_type (long type, char const *name_name, struct scm *x) +{ + if (x->type != type) + { + eputs (name); + eputs (": "); + error (cell_wrong_type_arg, cons (x, cell_nil)); + } +} +*/ + +void +assert_num (long pos, struct scm *x) +{ + if (x->type != TNUMBER) + error (cell_symbol_wrong_type_arg, cons (cell_type_number, cons (make_number (pos), x))); +} + +void +assert_struct (long pos, struct scm *x) +{ + if (x->type != TSTRUCT) + error (cell_symbol_wrong_type_arg, cons (cell_type_struct, cons (make_number (pos), x))); +} + +void +assert_range (int assert, long i) +{ + if (assert == 0) + { + eputs ("value out of range: "); + eputs (ltoa (i)); + eputs (": "); + assert_msg (assert, "value out of range"); + } + /* + if (assert != 0) + error (cell_symbol_out_of_range, cons (cell_type_struct, cons (make_number (pos), cons (x, cell_nil)))); + */ +} diff --git a/src/struct.c b/src/struct.c index 19162c70..540bd67d 100644 --- a/src/struct.c +++ b/src/struct.c @@ -54,11 +54,15 @@ struct_length (struct scm *x) return make_number (x->length); } +void assert_num (long pos, struct scm *x); +void assert_struct (long pos, struct scm *x); +void assert_range (int assert, long x); + struct scm * struct_ref_ (struct scm *x, long i) { - assert_msg (x->type == TSTRUCT, "x->type == TSTRUCT"); - assert_msg (i < x->length, "i < x->length"); + assert_struct (1, x); + assert_range (i < x->length, i); struct scm *e = cell_ref (x->structure, i); if (e->type == TREF) e = e->ref;