add pic_raise_continuable

This commit is contained in:
Yuichi Nishiwaki 2014-09-17 21:58:51 +09:00
parent 836c607979
commit a17e79a160
2 changed files with 54 additions and 45 deletions

98
error.c
View File

@ -33,6 +33,39 @@ pic_warnf(pic_state *pic, const char *fmt, ...)
fprintf(stderr, "warn: %s\n", pic_str_cstr(pic_str_ptr(pic_car(pic, err_line)))); fprintf(stderr, "warn: %s\n", pic_str_cstr(pic_str_ptr(pic_car(pic, err_line))));
} }
void
pic_errorf(pic_state *pic, const char *fmt, ...)
{
va_list ap;
pic_value err_line, irrs;
const char *msg;
va_start(ap, fmt);
err_line = pic_xvformat(pic, fmt, ap);
va_end(ap);
msg = pic_str_cstr(pic_str_ptr(pic_car(pic, err_line)));
irrs = pic_cdr(pic, err_line);
pic_error(pic, msg, irrs);
}
const char *
pic_errmsg(pic_state *pic)
{
pic_str *str;
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 void
pic_push_try(pic_state *pic, struct pic_proc *handler) pic_push_try(pic_state *pic, struct pic_proc *handler)
{ {
@ -88,6 +121,25 @@ pic_make_error(pic_state *pic, pic_sym type, const char *msg, pic_value irrs)
return e; return e;
} }
pic_value
pic_raise_continuable(pic_state *pic, pic_value err)
{
pic_value v;
if (pic->try_jmp_idx == 0) {
pic_errorf(pic, "no exception handler registered");
}
if (pic->try_jmps[pic->try_jmp_idx - 1].handler == NULL) {
pic_errorf(pic, "uncontinuable exception handler is on top");
}
else {
pic->try_jmp_idx--;
v = pic_apply1(pic, pic->try_jmps[pic->try_jmp_idx].handler, err);
++pic->try_jmp_idx;
}
return v;
}
noreturn void noreturn void
pic_raise(pic_state *pic, pic_value err) pic_raise(pic_state *pic, pic_value err)
{ {
@ -120,39 +172,6 @@ pic_error(pic_state *pic, const char *msg, pic_value irrs)
pic_throw(pic, pic_intern_cstr(pic, ""), msg, irrs); pic_throw(pic, pic_intern_cstr(pic, ""), msg, irrs);
} }
const char *
pic_errmsg(pic_state *pic)
{
pic_str *str;
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
pic_errorf(pic_state *pic, const char *fmt, ...)
{
va_list ap;
pic_value err_line, irrs;
const char *msg;
va_start(ap, fmt);
err_line = pic_xvformat(pic, fmt, ap);
va_end(ap);
msg = pic_str_cstr(pic_str_ptr(pic_car(pic, err_line)));
irrs = pic_cdr(pic, err_line);
pic_error(pic, msg, irrs);
}
static pic_value static pic_value
pic_error_with_exception_handler(pic_state *pic) pic_error_with_exception_handler(pic_state *pic)
{ {
@ -193,18 +212,7 @@ pic_error_raise_continuable(pic_state *pic)
pic_get_args(pic, "o", &v); pic_get_args(pic, "o", &v);
if (pic->try_jmp_idx == 0) { return pic_raise_continuable(pic, v);
pic_errorf(pic, "no exception handler registered");
}
if (pic->try_jmps[pic->try_jmp_idx - 1].handler == NULL) {
pic_errorf(pic, "uncontinuable exception handler is on top");
}
else {
pic->try_jmp_idx--;
v = pic_apply1(pic, pic->try_jmps[pic->try_jmp_idx].handler, v);
++pic->try_jmp_idx;
}
return v;
} }
noreturn static pic_value noreturn static pic_value

View File

@ -34,6 +34,7 @@ struct pic_jmpbuf {
void pic_push_try(pic_state *, struct pic_proc *); void pic_push_try(pic_state *, struct pic_proc *);
void pic_pop_try(pic_state *); void pic_pop_try(pic_state *);
pic_value pic_raise_continuable(pic_state *, pic_value);
noreturn void pic_raise(pic_state *, pic_value); noreturn void pic_raise(pic_state *, pic_value);
noreturn void pic_throw(pic_state *, pic_sym, const char *, pic_list); noreturn void pic_throw(pic_state *, pic_sym, const char *, pic_list);
noreturn void pic_error(pic_state *, const char *, pic_list); noreturn void pic_error(pic_state *, const char *, pic_list);