From 9ae6f0cbe98434a2412729c7170ca2203dac9e09 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 01:34:13 +0900 Subject: [PATCH] remove error.h --- contrib/20.r7rs/src/file.c | 6 +-- extlib/benz/error.c | 13 +++--- extlib/benz/include/picrin.h | 32 ++++++++++++++- extlib/benz/include/picrin/error.h | 63 ----------------------------- extlib/benz/include/picrin/object.h | 16 ++++++++ extlib/benz/port.c | 4 +- extlib/benz/read.c | 8 +--- 7 files changed, 59 insertions(+), 83 deletions(-) delete mode 100644 extlib/benz/include/picrin/error.h diff --git a/contrib/20.r7rs/src/file.c b/contrib/20.r7rs/src/file.c index 270260db..f0da4f49 100644 --- a/contrib/20.r7rs/src/file.c +++ b/contrib/20.r7rs/src/file.c @@ -9,11 +9,7 @@ PIC_NORETURN static void file_error(pic_state *pic, const char *msg) { - struct pic_error *e; - - e = pic_make_error(pic, pic_intern_lit(pic, "file"), msg, pic_nil_value(pic)); - - pic_raise(pic, pic_obj_value(e)); + pic_error(pic, "file", msg, pic_nil_value(pic)); } pic_value diff --git a/extlib/benz/error.c b/extlib/benz/error.c index 658d85af..b86a42b5 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -44,7 +44,7 @@ pic_errorf(pic_state *pic, const char *fmt, ...) msg = pic_str(pic, err); - pic_error(pic, msg, pic_nil_value(pic)); + pic_error(pic, "", msg, pic_nil_value(pic)); } pic_value @@ -92,15 +92,16 @@ pic_pop_handler(pic_state *pic) } struct pic_error * -pic_make_error(pic_state *pic, pic_sym *type, const char *msg, pic_value irrs) +pic_make_error(pic_state *pic, const char *type, const char *msg, pic_value irrs) { struct pic_error *e; struct pic_string *stack; + pic_sym *ty = pic_intern_cstr(pic, type); stack = pic_get_backtrace(pic); e = (struct pic_error *)pic_obj_alloc(pic, sizeof(struct pic_error), PIC_TYPE_ERROR); - e->type = type; + e->type = ty; e->msg = pic_cstr_value(pic, msg); e->irrs = irrs; e->stack = stack; @@ -138,11 +139,11 @@ pic_raise(pic_state *pic, pic_value err) } void -pic_error(pic_state *pic, const char *msg, pic_value irrs) +pic_error(pic_state *pic, const char *type, const char *msg, pic_value irrs) { struct pic_error *e; - e = pic_make_error(pic, pic_intern_lit(pic, ""), msg, irrs); + e = pic_make_error(pic, type, msg, irrs); pic_raise(pic, pic_obj_value(e)); } @@ -193,7 +194,7 @@ pic_error_error(pic_state *pic) pic_get_args(pic, "z*", &str, &argc, &argv); - pic_error(pic, str, pic_list_by_array(pic, argc, argv)); + pic_error(pic, "", str, pic_list_by_array(pic, argc, argv)); } static pic_value diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 319f01fb..f19f419d 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -109,6 +109,8 @@ void pic_export(pic_state *, pic_sym *sym); PIC_NORETURN void pic_panic(pic_state *, const char *msg); PIC_NORETURN void pic_errorf(pic_state *, const char *fmt, ...); +PIC_NORETURN void pic_error(pic_state *, const char *type, const char *msg, pic_value irrs); +PIC_NORETURN void pic_raise(pic_state *, pic_value v); struct pic_proc *pic_lambda(pic_state *, pic_func_t f, int n, ...); struct pic_proc *pic_vlambda(pic_state *, pic_func_t f, int n, va_list); @@ -260,7 +262,6 @@ int pic_str_hash(pic_state *, struct pic_string *); #include "picrin/state.h" #include "picrin/cont.h" -#include "picrin/error.h" #include "picrin/macro.h" #include "picrin/pair.h" #include "picrin/port.h" @@ -301,6 +302,35 @@ bool pic_data_type_p(pic_state *, pic_value, const pic_data_type *); pic_in_library(pic, lib); \ } while (0) +/* do not return from try block! */ + +#define pic_try \ + pic_try_(PIC_GENSYM(cont), PIC_GENSYM(handler)) +#define pic_catch \ + pic_catch_(PIC_GENSYM(label)) +#define pic_try_(cont, handler) \ + do { \ + extern void pic_push_handler(pic_state *, struct pic_proc *); \ + extern struct pic_proc *pic_pop_handler(pic_state *); \ + extern pic_value pic_native_exception_handler(pic_state *); \ + struct pic_cont cont; \ + pic_save_point(pic, &cont); \ + if (PIC_SETJMP(pic, cont.jmp) == 0) { \ + struct pic_proc *handler; \ + handler = pic_lambda(pic, pic_native_exception_handler, 1, pic_obj_value(pic_make_cont(pic, &cont))); \ + do { \ + pic_push_handler(pic, handler); +#define pic_catch_(label) \ + pic_pop_handler(pic); \ + } while (0); \ + pic->cc = pic->cc->prev; \ + } else { \ + goto label; \ + } \ + } while (0); \ + if (0) \ + label: + void pic_warnf(pic_state *, const char *, ...); struct pic_string *pic_get_backtrace(pic_state *); void pic_print_backtrace(pic_state *, xFILE *); diff --git a/extlib/benz/include/picrin/error.h b/extlib/benz/include/picrin/error.h deleted file mode 100644 index d09056c7..00000000 --- a/extlib/benz/include/picrin/error.h +++ /dev/null @@ -1,63 +0,0 @@ -/** - * See Copyright Notice in picrin.h - */ - -#ifndef PICRIN_ERROR_H -#define PICRIN_ERROR_H - -#if defined(__cplusplus) -extern "C" { -#endif - -struct pic_error { - PIC_OBJECT_HEADER - pic_sym *type; - struct pic_string *msg; - pic_value irrs; - struct pic_string *stack; -}; - -#define pic_error_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ERROR) -#define pic_error_ptr(v) ((struct pic_error *)pic_obj_ptr(v)) - -struct pic_error *pic_make_error(pic_state *, pic_sym *, const char *, pic_value); - -/* do not return from try block! */ - -#define pic_try \ - pic_try_(PIC_GENSYM(cont), PIC_GENSYM(handler)) -#define pic_catch \ - pic_catch_(PIC_GENSYM(label)) -#define pic_try_(cont, handler) \ - do { \ - struct pic_cont cont; \ - pic_save_point(pic, &cont); \ - if (PIC_SETJMP(pic, cont.jmp) == 0) { \ - extern pic_value pic_native_exception_handler(pic_state *); \ - struct pic_proc *handler; \ - handler = pic_lambda(pic, pic_native_exception_handler, 1, pic_obj_value(pic_make_cont(pic, &cont))); \ - do { \ - pic_push_handler(pic, handler); -#define pic_catch_(label) \ - pic_pop_handler(pic); \ - } while (0); \ - pic->cc = pic->cc->prev; \ - } else { \ - goto label; \ - } \ - } while (0); \ - if (0) \ - label: - -void pic_push_handler(pic_state *, struct pic_proc *); -struct pic_proc *pic_pop_handler(pic_state *); - -pic_value pic_raise_continuable(pic_state *, pic_value); -PIC_NORETURN void pic_raise(pic_state *, pic_value); -PIC_NORETURN void pic_error(pic_state *, const char *, pic_value); - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index 1e70892a..1866820c 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -163,6 +163,22 @@ struct pic_record { struct pic_record *pic_make_rec(pic_state *, pic_value, pic_value); +/* error */ + +struct pic_error { + PIC_OBJECT_HEADER + pic_sym *type; + struct pic_string *msg; + pic_value irrs; + struct pic_string *stack; +}; + +#define pic_error_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ERROR) +#define pic_error_ptr(v) ((struct pic_error *)pic_obj_ptr(v)) + +struct pic_error *pic_make_error(pic_state *, const char *, const char *, pic_value); + + #if defined(__cplusplus) } #endif diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 5169978b..0c88e230 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -94,7 +94,7 @@ file_error(pic_state *pic, const char *msg) { struct pic_error *e; - e = pic_make_error(pic, pic_intern_lit(pic, "file"), msg, pic_nil_value(pic)); + e = pic_make_error(pic, "file", msg, pic_nil_value(pic)); pic_raise(pic, pic_obj_value(e)); } @@ -257,7 +257,7 @@ string_open(pic_state *pic, const char *data, size_t size) if (file == NULL) { string_close(pic, m); - pic_error(pic, "could not open new output string/bytevector port", pic_nil_value(pic)); + pic_error(pic, "", "could not open new output string/bytevector port", pic_nil_value(pic)); } return file; } diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 84738f2c..1ddfcb29 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -11,13 +11,9 @@ static pic_value read(pic_state *pic, struct pic_port *port, int c); static pic_value read_nullable(pic_state *pic, struct pic_port *port, int c); PIC_NORETURN static void -read_error(pic_state *pic, const char *msg, pic_value irritant) +read_error(pic_state *pic, const char *msg, pic_value irritants) { - struct pic_error *e; - - e = pic_make_error(pic, pic_intern_lit(pic, "read"), msg, irritant); - - pic_raise(pic, pic_obj_value(e)); + pic_error(pic, "read", msg, irritants); } static int