From 33efb3e950b88cbefa9554ce1e4f2f194d388a76 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 16 Sep 2014 23:43:15 +0900 Subject: [PATCH 1/6] remove pic_error --- blob.c | 8 ++++---- char.c | 2 +- codegen.c | 24 ++++++++++++------------ include/picrin.h | 4 ---- macro.c | 14 +++++++------- number.c | 6 +++--- pair.c | 8 ++++---- port.c | 18 +++++++++--------- proc.c | 2 +- string.c | 2 +- symbol.c | 4 ++-- vector.c | 4 ++-- vm.c | 24 ++++++++++++------------ 13 files changed, 58 insertions(+), 62 deletions(-) diff --git a/blob.c b/blob.c index 337e3333..a8f28c9b 100644 --- a/blob.c +++ b/blob.c @@ -64,7 +64,7 @@ pic_blob_bytevector(pic_state *pic) pic_assert_type(pic, argv[i], int); if (pic_int(argv[i]) < 0 || pic_int(argv[i]) > 255) { - pic_error(pic, "byte out of range"); + pic_errorf(pic, "byte out of range"); } *data++ = pic_int(argv[i]); @@ -82,7 +82,7 @@ pic_blob_make_bytevector(pic_state *pic) pic_get_args(pic, "i|i", &k, &b); if (b < 0 || b > 255) - pic_error(pic, "byte out of range"); + pic_errorf(pic, "byte out of range"); blob = pic_make_blob(pic, k); for (i = 0; i < k; ++i) { @@ -122,7 +122,7 @@ pic_blob_bytevector_u8_set(pic_state *pic) pic_get_args(pic, "bii", &bv, &k, &v); if (v < 0 || v > 255) - pic_error(pic, "byte out of range"); + pic_errorf(pic, "byte out of range"); bv->data[k] = v; return pic_none_value(); @@ -227,7 +227,7 @@ pic_blob_list_to_bytevector(pic_state *pic) pic_assert_type(pic, e, int); if (pic_int(e) < 0 || pic_int(e) > 255) - pic_error(pic, "byte out of range"); + pic_errorf(pic, "byte out of range"); *data++ = pic_int(e); } diff --git a/char.c b/char.c index 0dca9f52..a460fb2b 100644 --- a/char.c +++ b/char.c @@ -53,7 +53,7 @@ pic_char_integer_to_char(pic_state *pic) if (pic_char_p(argv[i])) \ d = pic_char(argv[i]); \ else \ - pic_error(pic, #op ": char required"); \ + pic_errorf(pic, #op ": char required"); \ \ if (! (c op d)) \ return pic_false_value(); \ diff --git a/codegen.c b/codegen.c index 34505723..55bb8587 100644 --- a/codegen.c +++ b/codegen.c @@ -425,7 +425,7 @@ analyze_lambda(analyze_state *state, pic_value obj) pic_value formals, body_exprs; if (pic_length(pic, obj) < 2) { - pic_error(pic, "syntax error"); + pic_errorf(pic, "syntax error"); } formals = pic_list_ref(pic, obj, 1); @@ -450,12 +450,12 @@ analyze_define(analyze_state *state, pic_value obj) pic_sym sym; if (pic_length(pic, obj) != 3) { - pic_error(pic, "syntax error"); + pic_errorf(pic, "syntax error"); } var = pic_list_ref(pic, obj, 1); if (! pic_sym_p(var)) { - pic_error(pic, "syntax error"); + pic_errorf(pic, "syntax error"); } else { sym = pic_sym(var); } @@ -472,7 +472,7 @@ analyze_define(analyze_state *state, pic_value obj) val = analyze_defer(state, pic_sym_value(sym), formals, body_exprs); } else { if (pic_length(pic, obj) != 3) { - pic_error(pic, "syntax error"); + pic_errorf(pic, "syntax error"); } val = analyze(state, pic_list_ref(pic, obj, 2), false); } @@ -489,7 +489,7 @@ analyze_if(analyze_state *state, pic_value obj, bool tailpos) if_false = pic_none_value(); switch (pic_length(pic, obj)) { default: - pic_error(pic, "syntax error"); + pic_errorf(pic, "syntax error"); break; case 4: if_false = pic_list_ref(pic, obj, 3); @@ -539,12 +539,12 @@ analyze_set(analyze_state *state, pic_value obj) pic_value var, val; if (pic_length(pic, obj) != 3) { - pic_error(pic, "syntax error"); + pic_errorf(pic, "syntax error"); } var = pic_list_ref(pic, obj, 1); if (! pic_sym_p(var)) { - pic_error(pic, "syntax error"); + pic_errorf(pic, "syntax error"); } val = pic_list_ref(pic, obj, 2); @@ -561,14 +561,14 @@ analyze_quote(analyze_state *state, pic_value obj) pic_state *pic = state->pic; if (pic_length(pic, obj) != 2) { - pic_error(pic, "syntax error"); + pic_errorf(pic, "syntax error"); } return pic_list2(pic, pic_sym_value(pic->sQUOTE), pic_list_ref(pic, obj, 1)); } #define ARGC_ASSERT_GE(n) do { \ if (pic_length(pic, obj) < (n) + 1) { \ - pic_error(pic, "wrong number of arguments"); \ + pic_errorf(pic, "wrong number of arguments"); \ } \ } while (0) @@ -699,7 +699,7 @@ analyze_call_with_values(analyze_state *state, pic_value obj, bool tailpos) pic_sym call; if (pic_length(pic, obj) != 3) { - pic_error(pic, "wrong number of arguments"); + pic_errorf(pic, "wrong number of arguments"); } if (! tailpos) { @@ -714,7 +714,7 @@ analyze_call_with_values(analyze_state *state, pic_value obj, bool tailpos) #define ARGC_ASSERT(n) do { \ if (pic_length(pic, obj) != (n) + 1) { \ - pic_error(pic, "wrong number of arguments"); \ + pic_errorf(pic, "wrong number of arguments"); \ } \ } while (0) @@ -1413,7 +1413,7 @@ codegen(codegen_state *state, pic_value obj) cxt->clen++; return; } - pic_error(pic, "codegen: unknown AST type"); + pic_errorf(pic, "codegen: unknown AST type"); } static struct pic_irep * diff --git a/include/picrin.h b/include/picrin.h index e726ee4c..e911ff04 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -209,10 +209,6 @@ pic_str *pic_get_backtrace(pic_state *); void pic_print_backtrace(pic_state *, struct pic_error *); /* obsoleted */ -noreturn static inline void pic_error(pic_state *pic, const char *msg) -{ - pic_errorf(pic, msg); -} static inline void pic_warn(pic_state *pic, const char *msg) { pic_warnf(pic, msg); diff --git a/macro.c b/macro.c index 17f12cc6..c7cd243f 100644 --- a/macro.c +++ b/macro.c @@ -161,7 +161,7 @@ macroexpand_lambda(pic_state *pic, pic_value expr, struct pic_senv *senv) pic_value a; if (pic_length(pic, expr) < 2) { - pic_error(pic, "syntax error"); + pic_errorf(pic, "syntax error"); } in = pic_make_senv(pic, senv); @@ -170,7 +170,7 @@ macroexpand_lambda(pic_state *pic, pic_value expr, struct pic_senv *senv) pic_value v = pic_car(pic, a); if (! pic_sym_p(v)) { - pic_error(pic, "syntax error"); + pic_errorf(pic, "syntax error"); } pic_add_rename(pic, in, pic_sym(v)); } @@ -178,7 +178,7 @@ macroexpand_lambda(pic_state *pic, pic_value expr, struct pic_senv *senv) pic_add_rename(pic, in, pic_sym(a)); } else if (! pic_nil_p(a)) { - pic_error(pic, "syntax error"); + pic_errorf(pic, "syntax error"); } formal = macroexpand_list(pic, pic_cadr(pic, expr), in); @@ -203,12 +203,12 @@ macroexpand_define(pic_state *pic, pic_value expr, struct pic_senv *senv) } if (pic_length(pic, expr) != 3) { - pic_error(pic, "syntax error"); + pic_errorf(pic, "syntax error"); } var = pic_cadr(pic, expr); if (! pic_sym_p(var)) { - pic_error(pic, "binding to non-symbol object"); + pic_errorf(pic, "binding to non-symbol object"); } sym = pic_sym(var); if (! pic_find_rename(pic, senv, sym, &rename)) { @@ -226,12 +226,12 @@ macroexpand_defsyntax(pic_state *pic, pic_value expr, struct pic_senv *senv) pic_sym sym, rename; if (pic_length(pic, expr) != 3) { - pic_error(pic, "syntax error"); + pic_errorf(pic, "syntax error"); } var = pic_cadr(pic, expr); if (! pic_sym_p(var)) { - pic_error(pic, "binding to non-symbol object"); + pic_errorf(pic, "binding to non-symbol object"); } sym = pic_sym(var); if (! pic_find_rename(pic, senv, sym, &rename)) { diff --git a/number.c b/number.c index 10dc03aa..4c13df35 100644 --- a/number.c +++ b/number.c @@ -179,7 +179,7 @@ pic_number_nan_p(pic_state *pic) else if (pic_int_p(argv[i])) \ g = pic_int(argv[i]); \ else \ - pic_error(pic, #op ": number required"); \ + pic_errorf(pic, #op ": number required"); \ \ if (! (f op g)) \ return pic_false_value(); \ @@ -216,7 +216,7 @@ DEFINE_ARITH_CMP(>=, ge) f op##= pic_float(argv[i]); \ } \ else { \ - pic_error(pic, #op ": number required"); \ + pic_errorf(pic, #op ": number required"); \ } \ } \ \ @@ -252,7 +252,7 @@ DEFINE_ARITH_OP(*, mul, 1) f op##= pic_float(argv[i]); \ } \ else { \ - pic_error(pic, #op ": number required"); \ + pic_errorf(pic, #op ": number required"); \ } \ } \ \ diff --git a/pair.c b/pair.c index d9960347..0bb0d90e 100644 --- a/pair.c +++ b/pair.c @@ -51,7 +51,7 @@ pic_set_car(pic_state *pic, pic_value obj, pic_value val) struct pic_pair *pair; if (! pic_pair_p(obj)) { - pic_error(pic, "pair required"); + pic_errorf(pic, "pair required"); } pair = pic_pair_ptr(obj); @@ -64,7 +64,7 @@ pic_set_cdr(pic_state *pic, pic_value obj, pic_value val) struct pic_pair *pair; if (! pic_pair_p(obj)) { - pic_error(pic, "pair required"); + pic_errorf(pic, "pair required"); } pair = pic_pair_ptr(obj); @@ -520,7 +520,7 @@ pic_pair_set_car(pic_state *pic) pic_get_args(pic, "oo", &v, &w); if (! pic_pair_p(v)) - pic_error(pic, "pair expected"); + pic_errorf(pic, "pair expected"); pic_pair_ptr(v)->car = w; return pic_none_value(); @@ -534,7 +534,7 @@ pic_pair_set_cdr(pic_state *pic) pic_get_args(pic, "oo", &v, &w); if (! pic_pair_p(v)) - pic_error(pic, "pair expected"); + pic_errorf(pic, "pair expected"); pic_pair_ptr(v)->cdr = w; return pic_none_value(); diff --git a/port.c b/port.c index 859df8a5..558a81b6 100644 --- a/port.c +++ b/port.c @@ -107,7 +107,7 @@ void pic_close_port(pic_state *pic, struct pic_port *port) { if (xfclose(port->file) == EOF) { - pic_error(pic, "close-port: failure"); + pic_errorf(pic, "close-port: failure"); } port->status = PIC_PORT_CLOSE; } @@ -247,25 +247,25 @@ pic_port_close_port(pic_state *pic) if ((port->flags & (flgs)) != (flgs)) { \ switch (flgs) { \ case PIC_PORT_IN: \ - pic_error(pic, caller ": expected output port"); \ + pic_errorf(pic, caller ": expected output port"); \ case PIC_PORT_OUT: \ - pic_error(pic, caller ": expected input port"); \ + pic_errorf(pic, caller ": expected input port"); \ case PIC_PORT_IN | PIC_PORT_TEXT: \ - pic_error(pic, caller ": expected input/textual port"); \ + pic_errorf(pic, caller ": expected input/textual port"); \ case PIC_PORT_IN | PIC_PORT_BINARY: \ - pic_error(pic, caller ": expected input/binary port"); \ + pic_errorf(pic, caller ": expected input/binary port"); \ case PIC_PORT_OUT | PIC_PORT_TEXT: \ - pic_error(pic, caller ": expected output/textual port"); \ + pic_errorf(pic, caller ": expected output/textual port"); \ case PIC_PORT_OUT | PIC_PORT_BINARY: \ - pic_error(pic, caller ": expected output/binary port"); \ + pic_errorf(pic, caller ": expected output/binary port"); \ } \ } \ if (port->status != stat) { \ switch (stat) { \ case PIC_PORT_OPEN: \ - pic_error(pic, caller ": expected open port"); \ + pic_errorf(pic, caller ": expected open port"); \ case PIC_PORT_CLOSE: \ - pic_error(pic, caller ": expected close port"); \ + pic_errorf(pic, caller ": expected close port"); \ } \ } \ } while (0) diff --git a/proc.c b/proc.c index 1dee4813..9702819c 100644 --- a/proc.c +++ b/proc.c @@ -91,7 +91,7 @@ pic_proc_apply(pic_state *pic) pic_get_args(pic, "l*", &proc, &argc, &args); if (argc == 0) { - pic_error(pic, "apply: wrong number of arguments"); + pic_errorf(pic, "apply: wrong number of arguments"); } arg_list = args[--argc]; diff --git a/string.c b/string.c index fc2d2ab6..6876a969 100644 --- a/string.c +++ b/string.c @@ -404,7 +404,7 @@ pic_str_string_append(pic_state *pic) str = pic_make_str(pic, NULL, 0); for (i = 0; i < argc; ++i) { if (! pic_str_p(argv[i])) { - pic_error(pic, "type error"); + pic_errorf(pic, "type error"); } str = pic_strcat(pic, str, pic_str_ptr(argv[i])); } diff --git a/symbol.c b/symbol.c index 7c6479b4..6629abac 100644 --- a/symbol.c +++ b/symbol.c @@ -132,7 +132,7 @@ pic_symbol_symbol_to_string(pic_state *pic) pic_get_args(pic, "o", &v); if (! pic_sym_p(v)) { - pic_error(pic, "symbol->string: expected symbol"); + pic_errorf(pic, "symbol->string: expected symbol"); } return pic_obj_value(pic_make_str_cstr(pic, pic_symbol_name(pic, pic_sym(v)))); @@ -146,7 +146,7 @@ pic_symbol_string_to_symbol(pic_state *pic) pic_get_args(pic, "o", &v); if (! pic_str_p(v)) { - pic_error(pic, "string->symbol: expected string"); + pic_errorf(pic, "string->symbol: expected string"); } return pic_symbol_value(pic_intern_cstr(pic, pic_str_cstr(pic_str_ptr(v)))); diff --git a/vector.c b/vector.c index 4a3e8194..57790b36 100644 --- a/vector.c +++ b/vector.c @@ -105,7 +105,7 @@ pic_vec_vector_ref(pic_state *pic) pic_get_args(pic, "vi", &v, &k); if (k < 0 || v->len <= (size_t)k) { - pic_error(pic, "vector-ref: index out of range"); + pic_errorf(pic, "vector-ref: index out of range"); } return v->data[k]; } @@ -120,7 +120,7 @@ pic_vec_vector_set(pic_state *pic) pic_get_args(pic, "vio", &v, &k, &o); if (k < 0 || v->len <= (size_t)k) { - pic_error(pic, "vector-set!: index out of range"); + pic_errorf(pic, "vector-set!: index out of range"); } v->data[k] = o; return pic_none_value(); diff --git a/vm.c b/vm.c index b84bc96e..609c702f 100644 --- a/vm.c +++ b/vm.c @@ -29,7 +29,7 @@ pic_get_proc(pic_state *pic) pic_value v = GET_OPERAND(pic,0); if (! pic_proc_p(v)) { - pic_error(pic, "fatal error"); + pic_errorf(pic, "fatal error"); } return pic_proc_ptr(v); } @@ -70,7 +70,7 @@ pic_get_args(pic_state *pic, const char *format, ...) switch (c) { default: if (argc <= i && ! opt) { - pic_error(pic, "wrong number of arguments"); + pic_errorf(pic, "wrong number of arguments"); } break; case '|': @@ -375,7 +375,7 @@ pic_get_args(pic_state *pic, const char *format, ...) *e = pic_error_ptr(v); } else { - pic_error(pic, "pic_get_args, expected error"); + pic_errorf(pic, "pic_get_args, expected error"); } i++; } @@ -398,7 +398,7 @@ pic_get_args(pic_state *pic, const char *format, ...) } } else if (argc > i) { - pic_error(pic, "wrong number of arguments"); + pic_errorf(pic, "wrong number of arguments"); } va_end(ap); return i - 1; @@ -675,7 +675,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv) #endif if (! pic_list_p(argv)) { - pic_error(pic, "argv must be a proper list"); + pic_errorf(pic, "argv must be a proper list"); } argc = pic_length(pic, argv) + 1; @@ -728,11 +728,11 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv) self = pic->ci->fp[0]; if (! pic_proc_p(self)) { - pic_error(pic, "logic flaw"); + pic_errorf(pic, "logic flaw"); } irep = pic_proc_ptr(self)->u.irep; if (! pic_proc_irep_p(pic_proc_ptr(self))) { - pic_error(pic, "logic flaw"); + pic_errorf(pic, "logic flaw"); } PUSH(irep->pool[c.u.i]); NEXT; @@ -950,11 +950,11 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv) self = pic->ci->fp[0]; if (! pic_proc_p(self)) { - pic_error(pic, "logic flaw"); + pic_errorf(pic, "logic flaw"); } irep = pic_proc_ptr(self)->u.irep; if (! pic_proc_irep_p(pic_proc_ptr(self))) { - pic_error(pic, "logic flaw"); + pic_errorf(pic, "logic flaw"); } if (pic->ci->env == NULL) { @@ -1017,7 +1017,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv) PUSH(pic_float_value(pic_float(a) op pic_int(b))); \ } \ else { \ - pic_error(pic, #op " got non-number operands"); \ + pic_errorf(pic, #op " got non-number operands"); \ } \ NEXT; \ } @@ -1037,7 +1037,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv) PUSH(pic_float_value(-pic_float(n))); } else { - pic_error(pic, "unary - got a non-number operand"); + pic_errorf(pic, "unary - got a non-number operand"); } NEXT; } @@ -1060,7 +1060,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv) PUSH(pic_bool_value(pic_float(a) op pic_int(b))); \ } \ else { \ - pic_error(pic, #op " got non-number operands"); \ + pic_errorf(pic, #op " got non-number operands"); \ } \ NEXT; \ } From 78a982fb4148d1c5dcd8ec24a8d3d0a36f5d3ccb Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Wed, 17 Sep 2014 00:28:55 +0900 Subject: [PATCH 2/6] refactor error object --- debug.c | 41 +++++++++++++---------------- error.c | 60 +++++++++++++++++++++++++----------------- file.c | 2 +- gc.c | 4 +-- include/picrin.h | 5 ++-- include/picrin/error.h | 14 ++++------ include/picrin/value.h | 1 + lib.c | 2 +- read.c | 2 +- state.c | 6 +++-- 10 files changed, 71 insertions(+), 66 deletions(-) diff --git a/debug.c b/debug.c index 09c70553..3d7b92c6 100644 --- a/debug.c +++ b/debug.c @@ -36,37 +36,32 @@ pic_get_backtrace(pic_state *pic) } void -pic_print_backtrace(pic_state *pic, struct pic_error *e) +pic_print_backtrace(pic_state *pic) { size_t ai = pic_gc_arena_preserve(pic); pic_str *trace; - assert(pic->err != NULL); + assert(! pic_undef_p(pic->err)); - trace = pic_make_str(pic, NULL, 0); + if (! pic_error_p(pic->err)) { + trace = pic_format(pic, "raised: ~s", pic->err); + } else { + struct pic_error *e; - switch (e->type) { - case PIC_ERROR_OTHER: - trace = pic_strcat(pic, trace, pic_make_str_cstr(pic, "error: ")); - break; - case PIC_ERROR_FILE: - trace = pic_strcat(pic, trace, pic_make_str_cstr(pic, "file error: ")); - break; - case PIC_ERROR_READ: - trace = pic_strcat(pic, trace, pic_make_str_cstr(pic, "read error: ")); - break; - case PIC_ERROR_RAISED: - trace = pic_strcat(pic, trace, pic_make_str_cstr(pic, "raised: ")); - break; + e = pic_error_ptr(pic->err); + if (e->type != pic_intern_cstr(pic, "")) { + trace = pic_format(pic, "~s ", pic_sym_value(e->type)); + } else { + trace = pic_make_str(pic, NULL, 0); + } + trace = pic_strcat(pic, trace, pic_format(pic, "error: ~s", pic_obj_value(e->msg))); + + /* TODO: print error irritants */ + + trace = pic_strcat(pic, trace, pic_make_str(pic, "\n", 1)); + trace = pic_strcat(pic, trace, e->stack); } - trace = pic_strcat(pic, trace, e->msg); - - /* TODO: print error irritants */ - - trace = pic_strcat(pic, trace, pic_make_str(pic, "\n", 1)); - trace = pic_strcat(pic, trace, e->stack); - /* print! */ printf("%s", pic_str_cstr(trace)); diff --git a/error.c b/error.c index d5596a7b..dad4a604 100644 --- a/error.c +++ b/error.c @@ -89,37 +89,51 @@ make_error(pic_state *pic, short type, pic_str *msg, pic_value irrs) } noreturn void -pic_throw_error(pic_state *pic, struct pic_error *e) +pic_raise(pic_state *pic, pic_value err) { void pic_vm_tear_off(pic_state *); pic_vm_tear_off(pic); /* tear off */ - pic->err = e; + pic->err = err; if (! pic->jmp) { puts(pic_errmsg(pic)); - abort(); + pic_abort(pic, "no handler found on stack"); } longjmp(*pic->jmp, 1); } noreturn void -pic_throw(pic_state *pic, short type, const char *msg, pic_value irrs) +pic_throw(pic_state *pic, pic_sym type, const char *msg, pic_value irrs) { struct pic_error *e; e = make_error(pic, type, pic_make_str_cstr(pic, msg), irrs); - pic_throw_error(pic, e); + pic_raise(pic, pic_obj_value(e)); +} + +noreturn void +pic_error(pic_state *pic, const char *msg, pic_value irrs) +{ + pic_throw(pic, pic_intern_cstr(pic, ""), msg, irrs); } const char * pic_errmsg(pic_state *pic) { - assert(pic->err != NULL); + pic_str *str; - return pic_str_cstr(pic->err->msg); + assert(! pic_undef_p(pic->err)); + + if (! pic_error_p(pic->err)) { + str = pic_format(pic, "~s", pic->err); + } else { + str = pic_error_ptr(pic->err)->msg; + } + + return pic_str_cstr(str); } void @@ -136,34 +150,30 @@ pic_errorf(pic_state *pic, const char *fmt, ...) msg = pic_str_cstr(pic_str_ptr(pic_car(pic, err_line))); irrs = pic_cdr(pic, err_line); - pic_throw(pic, PIC_ERROR_OTHER, msg, irrs); + pic_error(pic, msg, irrs); } static pic_value pic_error_with_exception_handler(pic_state *pic) { struct pic_proc *handler, *thunk; - pic_value v; + pic_value val; pic_get_args(pic, "ll", &handler, &thunk); pic_try_with_handler(handler) { - v = pic_apply0(pic, thunk); + val = pic_apply0(pic, thunk); } pic_catch { - struct pic_error *e = pic->err; + pic_value e = pic->err; - pic->err = NULL; + pic->err = pic_undef_value(); - if (e->type == PIC_ERROR_RAISED) { - v = pic_list_ref(pic, e->irrs, 0); - } else { - v = pic_obj_value(e); - } - v = pic_apply1(pic, handler, v); - pic_errorf(pic, "error handler returned ~s, by error ~s", v, pic_obj_value(e)); + val = pic_apply1(pic, handler, e); + + pic_errorf(pic, "error handler returned with ~s on error ~s", val, e); } - return v; + return val; } noreturn static pic_value @@ -173,7 +183,7 @@ pic_error_raise(pic_state *pic) pic_get_args(pic, "o", &v); - pic_throw(pic, PIC_ERROR_RAISED, "object is raised", pic_list1(pic, v)); + pic_raise(pic, v); } static pic_value @@ -206,7 +216,7 @@ pic_error_error(pic_state *pic) pic_get_args(pic, "z*", &str, &argc, &argv); - pic_throw(pic, PIC_ERROR_OTHER, str, pic_list_by_array(pic, argc, argv)); + pic_error(pic, str, pic_list_by_array(pic, argc, argv)); } static pic_value @@ -252,7 +262,8 @@ pic_error_read_error_p(pic_state *pic) } e = pic_error_ptr(v); - return pic_bool_value(e->type == PIC_ERROR_READ); + + return pic_bool_value(e->type == pic->sREAD); } static pic_value @@ -268,7 +279,8 @@ pic_error_file_error_p(pic_state *pic) } e = pic_error_ptr(v); - return pic_bool_value(e->type == PIC_ERROR_FILE); + + return pic_bool_value(e->type == pic->sFILE); } void diff --git a/file.c b/file.c index cfc266b5..4a5f57d7 100644 --- a/file.c +++ b/file.c @@ -9,7 +9,7 @@ static noreturn void file_error(pic_state *pic, const char *msg) { - pic_throw(pic, PIC_ERROR_FILE, msg, pic_nil_value()); + pic_throw(pic, pic->sFILE, msg, pic_nil_value()); } static pic_value diff --git a/gc.c b/gc.c index ba0e23d7..bb241a43 100644 --- a/gc.c +++ b/gc.c @@ -580,9 +580,7 @@ gc_mark_phase(pic_state *pic) } /* error object */ - if (pic->err) { - gc_mark_object(pic, (struct pic_object *)pic->err); - } + gc_mark(pic, pic->err); /* arena */ for (j = 0; j < pic->arena_idx; ++j) { diff --git a/include/picrin.h b/include/picrin.h index e911ff04..fe564822 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -81,6 +81,7 @@ typedef struct { pic_sym sCONS, sCAR, sCDR, sNILP; pic_sym sADD, sSUB, sMUL, sDIV, sMINUS; pic_sym sEQ, sLT, sLE, sGT, sGE, sNOT; + pic_sym sREAD, sFILE; pic_sym rDEFINE, rLAMBDA, rIF, rBEGIN, rQUOTE, rSETBANG; pic_sym rDEFINE_SYNTAX, rIMPORT, rEXPORT; @@ -104,7 +105,7 @@ typedef struct { struct pic_reader *reader; jmp_buf *jmp; - struct pic_error *err; + pic_value err; struct pic_jmpbuf *try_jmps; size_t try_jmp_size, try_jmp_idx; @@ -206,7 +207,7 @@ noreturn void pic_abort(pic_state *, const char *); noreturn void pic_errorf(pic_state *, const char *, ...); void pic_warnf(pic_state *, const char *, ...); pic_str *pic_get_backtrace(pic_state *); -void pic_print_backtrace(pic_state *, struct pic_error *); +void pic_print_backtrace(pic_state *); /* obsoleted */ static inline void pic_warn(pic_state *pic, const char *msg) diff --git a/include/picrin/error.h b/include/picrin/error.h index 5005346a..f61f8082 100644 --- a/include/picrin/error.h +++ b/include/picrin/error.h @@ -34,18 +34,14 @@ struct pic_jmpbuf { void pic_push_try(pic_state *, struct pic_proc *); void pic_pop_try(pic_state *); -noreturn void pic_throw(pic_state *, short, const char *, pic_value); -noreturn void pic_throw_error(pic_state *, struct pic_error *); +noreturn void pic_raise(pic_state *, pic_value); +noreturn void pic_throw(pic_state *, pic_sym, const char *, pic_list); +noreturn void pic_error(pic_state *, const char *, pic_list); struct pic_error { PIC_OBJECT_HEADER - enum pic_error_kind { - PIC_ERROR_OTHER, - PIC_ERROR_FILE, - PIC_ERROR_READ, - PIC_ERROR_RAISED - } type; - struct pic_string *msg; + pic_sym type; + pic_str *msg; pic_value irrs; pic_str *stack; }; diff --git a/include/picrin/value.h b/include/picrin/value.h index 0523c688..6a211dc1 100644 --- a/include/picrin/value.h +++ b/include/picrin/value.h @@ -142,6 +142,7 @@ struct pic_blob; struct pic_proc; struct pic_port; +struct pic_error; /* set aliases to basic types */ typedef pic_value pic_list; diff --git a/lib.c b/lib.c index b716d41e..cb1843d8 100644 --- a/lib.c +++ b/lib.c @@ -318,7 +318,7 @@ pic_lib_define_library(pic_state *pic) } pic_catch { pic_in_library(pic, prev->name); /* restores pic->lib even if an error occurs */ - pic_throw_error(pic, pic->err); + pic_raise(pic, pic->err); } return pic_none_value(); diff --git a/read.c b/read.c index ad23436f..7deb5eb2 100644 --- a/read.c +++ b/read.c @@ -21,7 +21,7 @@ static pic_value read_nullable(pic_state *pic, struct pic_port *port, int c); static noreturn void read_error(pic_state *pic, const char *msg) { - pic_throw(pic, PIC_ERROR_READ, msg, pic_nil_value()); + pic_throw(pic, pic->sREAD, msg, pic_nil_value()); } static int diff --git a/state.c b/state.c index e7300e9c..b88f40a1 100644 --- a/state.c +++ b/state.c @@ -72,7 +72,7 @@ pic_open(int argc, char *argv[], char **envp) /* error handling */ pic->jmp = NULL; - pic->err = NULL; + pic->err = pic_undef_value(); pic->try_jmps = calloc(PIC_RESCUE_SIZE, sizeof(struct pic_jmpbuf)); pic->try_jmp_idx = 0; pic->try_jmp_size = PIC_RESCUE_SIZE; @@ -131,6 +131,8 @@ pic_open(int argc, char *argv[], char **envp) S(sGT, ">"); S(sGE, ">="); S(sNOT, "not"); + S(sREAD, "read"); + S(sFILE, "file"); pic_gc_arena_restore(pic, ai); #define R(slot,name) pic->slot = pic_gensym(pic, pic_intern_cstr(pic, name)); @@ -191,7 +193,7 @@ pic_close(pic_state *pic) pic->sp = pic->stbase; pic->ci = pic->cibase; pic->arena_idx = 0; - pic->err = NULL; + pic->err = pic_undef_value(); xh_clear(&pic->macros); pic->features = pic_nil_value(); pic->libs = pic_nil_value(); From 486c99bf6fdebf453c3aa2199c4c48715e50828e Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Wed, 17 Sep 2014 00:32:52 +0900 Subject: [PATCH 3/6] [bugfix] 'type' field is now of pic_sym type --- error.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/error.c b/error.c index dad4a604..82f7f968 100644 --- a/error.c +++ b/error.c @@ -72,7 +72,7 @@ pic_pop_try(pic_state *pic) } static struct pic_error * -make_error(pic_state *pic, short type, pic_str *msg, pic_value irrs) +make_error(pic_state *pic, pic_sym type, pic_str *msg, pic_value irrs) { struct pic_error *e; pic_str *stack; From 061ddb58eed60a6c02dc0ed6ebb9874367865602 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Wed, 17 Sep 2014 00:43:03 +0900 Subject: [PATCH 4/6] publish pic_make_error --- error.c | 8 ++++---- include/picrin/error.h | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/error.c b/error.c index 82f7f968..468935c3 100644 --- a/error.c +++ b/error.c @@ -71,8 +71,8 @@ pic_pop_try(pic_state *pic) pic->jmp = try_jmp->prev_jmp; } -static struct pic_error * -make_error(pic_state *pic, pic_sym type, pic_str *msg, pic_value irrs) +struct pic_error * +pic_make_error(pic_state *pic, pic_sym type, const char *msg, pic_value irrs) { struct pic_error *e; pic_str *stack; @@ -81,7 +81,7 @@ make_error(pic_state *pic, pic_sym type, pic_str *msg, pic_value irrs) e = (struct pic_error *)pic_obj_alloc(pic, sizeof(struct pic_error), PIC_TT_ERROR); e->type = type; - e->msg = msg; + e->msg = pic_make_str_cstr(pic, msg); e->irrs = irrs; e->stack = stack; @@ -109,7 +109,7 @@ pic_throw(pic_state *pic, pic_sym type, const char *msg, pic_value irrs) { struct pic_error *e; - e = make_error(pic, type, pic_make_str_cstr(pic, msg), irrs); + e = pic_make_error(pic, type, msg, irrs); pic_raise(pic, pic_obj_value(e)); } diff --git a/include/picrin/error.h b/include/picrin/error.h index f61f8082..1b96f3ee 100644 --- a/include/picrin/error.h +++ b/include/picrin/error.h @@ -49,6 +49,8 @@ struct pic_error { #define pic_error_p(v) (pic_type(v) == PIC_TT_ERROR) #define pic_error_ptr(v) ((struct pic_error *)pic_ptr(v)) +struct pic_error *pic_make_error(pic_state *, pic_sym, const char *, pic_list); + #if defined(__cplusplus) } #endif From 854f83a247b6f980c9db9462c9f8d088fa58f9ef Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Wed, 17 Sep 2014 00:43:12 +0900 Subject: [PATCH 5/6] add make-error-object procedure --- error.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/error.c b/error.c index 468935c3..276fc7d1 100644 --- a/error.c +++ b/error.c @@ -219,6 +219,22 @@ pic_error_error(pic_state *pic) pic_error(pic, str, pic_list_by_array(pic, argc, argv)); } +static pic_value +pic_error_make_error_object(pic_state *pic) +{ + struct pic_error *e; + pic_sym type; + pic_str *msg; + size_t argc; + pic_value *argv; + + pic_get_args(pic, "ms*", &type, &msg, &argc, &argv); + + e = pic_make_error(pic, type, pic_str_cstr(msg), pic_list_by_array(pic, argc, argv)); + + return pic_obj_value(e); +} + static pic_value pic_error_error_object_p(pic_state *pic) { @@ -290,6 +306,7 @@ pic_init_error(pic_state *pic) pic_defun(pic, "raise", pic_error_raise); pic_defun(pic, "raise-continuable", pic_error_raise_continuable); pic_defun(pic, "error", pic_error_error); + pic_defun(pic, "make-error-object", pic_error_make_error_object); pic_defun(pic, "error-object?", pic_error_error_object_p); pic_defun(pic, "error-object-message", pic_error_error_object_message); pic_defun(pic, "error-object-irritants", pic_error_error_object_irritants); From 05e76c0467af886f2cd88d2f6ad3d008dc64e73d Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Wed, 17 Sep 2014 00:49:37 +0900 Subject: [PATCH 6/6] remove read-error? and file-error? and add error-object-type instead --- error.c | 33 ++++----------------------------- 1 file changed, 4 insertions(+), 29 deletions(-) diff --git a/error.c b/error.c index 276fc7d1..054a780a 100644 --- a/error.c +++ b/error.c @@ -266,37 +266,13 @@ pic_error_error_object_irritants(pic_state *pic) } static pic_value -pic_error_read_error_p(pic_state *pic) +pic_error_error_object_type(pic_state *pic) { - pic_value v; struct pic_error *e; - pic_get_args(pic, "o", &v); + pic_get_args(pic, "e", &e); - if (! pic_error_p(v)) { - return pic_false_value(); - } - - e = pic_error_ptr(v); - - return pic_bool_value(e->type == pic->sREAD); -} - -static pic_value -pic_error_file_error_p(pic_state *pic) -{ - pic_value v; - struct pic_error *e; - - pic_get_args(pic, "o", &v); - - if (! pic_error_p(v)) { - return pic_false_value(); - } - - e = pic_error_ptr(v); - - return pic_bool_value(e->type == pic->sFILE); + return pic_sym_value(e->type); } void @@ -310,6 +286,5 @@ pic_init_error(pic_state *pic) pic_defun(pic, "error-object?", pic_error_error_object_p); pic_defun(pic, "error-object-message", pic_error_error_object_message); pic_defun(pic, "error-object-irritants", pic_error_error_object_irritants); - pic_defun(pic, "read-error?", pic_error_read_error_p); - pic_defun(pic, "file-error?", pic_error_file_error_p); + pic_defun(pic, "error-object-type", pic_error_error_object_type); }