picrin/lib/error.c

309 lines
6.4 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
2016-02-20 10:58:58 -05:00
#include "picrin/extra.h"
2016-02-20 11:13:16 -05:00
#include "picrin/private/object.h"
#include "picrin/private/state.h"
2014-08-25 00:38:09 -04:00
void
2016-02-21 04:59:18 -05:00
pic_panic(pic_state *pic, const char *msg)
2014-08-25 00:38:09 -04:00
{
2016-02-21 04:59:18 -05:00
if (pic->panicf) {
pic->panicf(pic, msg);
}
2016-02-22 14:37:58 -05:00
#if PIC_USE_STDIO
2016-02-21 04:59:18 -05:00
fprintf(stderr, "picrin panic!: %s\n", msg);
2015-05-27 13:12:26 -04:00
#endif
2016-02-21 04:59:18 -05:00
2015-05-27 13:12:26 -04:00
PIC_ABORT(pic);
2016-02-21 07:12:24 -05:00
PIC_UNREACHABLE();
2014-08-25 00:38:09 -04:00
}
void
pic_warnf(pic_state *pic, const char *fmt, ...)
{
va_list ap;
2016-02-19 13:26:52 -05:00
pic_value err;
2014-08-25 00:38:09 -04:00
va_start(ap, fmt);
err = pic_vstrf_value(pic, fmt, ap);
2014-08-25 00:38:09 -04:00
va_end(ap);
2016-06-19 15:49:01 -04:00
pic_fprintf(pic, pic_stderr(pic), "warn: %s\n", pic_str(pic, err));
2014-08-25 00:38:09 -04:00
}
2016-02-23 08:42:03 -05:00
static pic_value
native_exception_handler(pic_state *pic)
2014-09-17 08:58:51 -04:00
{
2016-02-23 08:42:03 -05:00
pic_value err;
2014-09-17 08:58:51 -04:00
2016-02-23 08:42:03 -05:00
pic_get_args(pic, "o", &err);
2014-09-17 08:58:51 -04:00
2016-02-23 08:42:03 -05:00
pic->err = err;
pic_call(pic, pic_closure_ref(pic, 0), 1, pic_false_value(pic));
PIC_UNREACHABLE();
2014-09-17 08:58:51 -04:00
}
2016-02-23 08:42:03 -05:00
static pic_value
dynamic_set(pic_state *pic)
2014-08-25 00:38:09 -04:00
{
2016-02-23 08:42:03 -05:00
pic_value var, val;
pic_get_args(pic, "");
var = pic_closure_ref(pic, 0);
val = pic_closure_ref(pic, 1);
pic_proc_ptr(pic, var)->locals[0] = val;
2014-09-24 07:21:28 -04:00
2016-02-23 08:42:03 -05:00
return pic_undef_value(pic);
}
2014-08-25 00:38:09 -04:00
2016-02-19 10:03:16 -05:00
pic_value
2016-02-23 08:42:03 -05:00
pic_start_try(pic_state *pic, PIC_JMPBUF *jmp)
{
2016-02-23 08:48:06 -05:00
struct cont *cont;
2016-02-23 08:42:03 -05:00
pic_value handler;
pic_value var, old_val, new_val;
pic_value in, out;
struct checkpoint *here;
2015-06-01 18:57:52 -04:00
2016-02-23 08:42:03 -05:00
/* call/cc */
2014-08-25 00:38:09 -04:00
2016-02-23 08:42:03 -05:00
cont = pic_alloca_cont(pic);
pic_save_point(pic, cont, jmp);
handler = pic_lambda(pic, native_exception_handler, 1, pic_make_cont(pic, cont));
2016-02-21 10:02:44 -05:00
2016-02-23 08:42:03 -05:00
/* with-exception-handler */
2016-02-21 10:02:44 -05:00
2016-02-23 08:42:03 -05:00
var = pic_ref(pic, "picrin.base", "current-exception-handlers");
old_val = pic_call(pic, var, 0);
new_val = pic_cons(pic, handler, old_val);
2016-02-21 10:02:44 -05:00
2016-02-23 08:42:03 -05:00
in = pic_lambda(pic, dynamic_set, 2, var, new_val);
out = pic_lambda(pic, dynamic_set, 2, var, old_val);
2016-02-21 10:02:44 -05:00
2016-02-23 08:42:03 -05:00
/* dynamic-wind */
pic_call(pic, in, 0); /* enter */
here = pic->cp;
pic->cp = (struct checkpoint *)pic_obj_alloc(pic, sizeof(struct checkpoint), PIC_TYPE_CP);
pic->cp->prev = here;
pic->cp->depth = here->depth + 1;
pic->cp->in = pic_proc_ptr(pic, in);
pic->cp->out = pic_proc_ptr(pic, out);
return pic_cons(pic, pic_obj_value(here), out);
2016-02-21 10:02:44 -05:00
}
void
2016-02-23 08:42:03 -05:00
pic_end_try(pic_state *pic, pic_value cookie)
2016-02-21 10:02:44 -05:00
{
2016-02-23 08:42:03 -05:00
struct checkpoint *here = (struct checkpoint *)pic_obj_ptr(pic_car(pic, cookie));
pic_value out = pic_cdr(pic, cookie);
2016-02-21 10:02:44 -05:00
2016-02-23 08:42:03 -05:00
pic->cp = here;
2016-02-21 10:02:44 -05:00
2016-02-23 08:42:03 -05:00
pic_call(pic, out, 0); /* exit */
pic_exit_point(pic);
2016-02-21 10:02:44 -05:00
}
pic_value
pic_err(pic_state *pic)
{
return pic->err;
}
2016-02-20 02:59:05 -05:00
pic_value
2016-02-18 11:34:13 -05:00
pic_make_error(pic_state *pic, const char *type, const char *msg, pic_value irrs)
2014-08-25 00:38:09 -04:00
{
2016-02-21 06:32:00 -05:00
struct error *e;
2016-02-20 01:31:14 -05:00
pic_value stack, ty = pic_intern_cstr(pic, type);
2014-08-25 00:38:09 -04:00
stack = pic_get_backtrace(pic);
2016-02-21 06:32:00 -05:00
e = (struct error *)pic_obj_alloc(pic, sizeof(struct error), PIC_TYPE_ERROR);
2016-02-20 01:31:14 -05:00
e->type = pic_sym_ptr(pic, ty);
2016-02-19 13:26:52 -05:00
e->msg = pic_str_ptr(pic, pic_cstr_value(pic, msg));
2014-08-25 00:38:09 -04:00
e->irrs = irrs;
2016-02-19 13:26:52 -05:00
e->stack = pic_str_ptr(pic, stack);
2014-08-25 00:38:09 -04:00
2016-02-20 02:59:05 -05:00
return pic_obj_value(e);
2014-08-25 00:38:09 -04:00
}
2016-02-23 08:42:03 -05:00
pic_value pic_raise_continuable(pic_state *, pic_value err);
void
pic_error(pic_state *pic, const char *msg, int n, ...)
2014-09-17 08:58:51 -04:00
{
2016-02-23 08:42:03 -05:00
va_list ap;
pic_value irrs;
2014-09-17 08:58:51 -04:00
2016-02-23 08:42:03 -05:00
va_start(ap, n);
irrs = pic_vlist(pic, n, ap);
va_end(ap);
2016-02-23 08:42:03 -05:00
pic_raise(pic, pic_make_error(pic, "", msg, irrs));
}
2016-02-23 08:42:03 -05:00
static pic_value
raise_action(pic_state *pic)
2016-02-23 08:42:03 -05:00
{
pic_get_args(pic, "");
2016-02-23 08:42:03 -05:00
pic_call(pic, pic_closure_ref(pic, 0), 1, pic_closure_ref(pic, 1));
2016-02-23 08:42:03 -05:00
pic_error(pic, "handler returned", 2, pic_closure_ref(pic, 0), pic_closure_ref(pic, 1));
}
2015-01-07 16:11:48 -05:00
void
pic_raise(pic_state *pic, pic_value err)
{
2016-02-23 08:42:03 -05:00
pic_value stack, exc = pic_ref(pic, "picrin.base", "current-exception-handlers");
2014-08-25 00:38:09 -04:00
2016-02-23 08:42:03 -05:00
stack = pic_call(pic, exc, 0);
2014-08-25 00:38:09 -04:00
if (pic_nil_p(pic, stack)) {
pic_panic(pic, "no exception handler");
}
pic_dynamic_bind(pic, exc, pic_cdr(pic, stack), pic_lambda(pic, raise_action, 2, pic_car(pic, stack), err));
2014-08-25 00:38:09 -04:00
2016-02-23 08:42:03 -05:00
PIC_UNREACHABLE();
2014-08-25 00:38:09 -04:00
}
static pic_value
2016-02-23 08:42:03 -05:00
raise_continuable(pic_state *pic)
2014-08-25 00:38:09 -04:00
{
2016-02-23 08:42:03 -05:00
pic_get_args(pic, "");
2014-08-25 00:38:09 -04:00
2016-02-23 08:42:03 -05:00
return pic_call(pic, pic_closure_ref(pic, 0), 1, pic_closure_ref(pic, 1));
}
2014-08-25 00:38:09 -04:00
2016-02-23 08:42:03 -05:00
pic_value
pic_raise_continuable(pic_state *pic, pic_value err)
{
pic_value stack, exc = pic_ref(pic, "picrin.base", "current-exception-handlers");
2016-02-23 08:42:03 -05:00
stack = pic_call(pic, exc, 0);
if (pic_nil_p(pic, stack)) {
pic_panic(pic, "no exception handler");
}
2016-02-23 08:42:03 -05:00
return pic_dynamic_bind(pic, exc, pic_cdr(pic, stack), pic_lambda(pic, raise_continuable, 2, pic_car(pic, stack), err));
}
static pic_value
pic_error_with_exception_handler(pic_state *pic)
{
pic_value handler, thunk;
pic_value stack, exc = pic_ref(pic, "picrin.base", "current-exception-handlers");
pic_get_args(pic, "ll", &handler, &thunk);
2016-02-23 08:42:03 -05:00
stack = pic_call(pic, exc, 0);
2016-02-23 08:42:03 -05:00
return pic_dynamic_bind(pic, exc, pic_cons(pic, handler, stack), thunk);
2014-08-25 00:38:09 -04:00
}
2015-01-07 16:11:48 -05:00
static pic_value
2014-08-25 00:38:09 -04:00
pic_error_raise(pic_state *pic)
{
pic_value v;
pic_get_args(pic, "o", &v);
2014-09-16 11:28:55 -04:00
pic_raise(pic, v);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_error_raise_continuable(pic_state *pic)
{
pic_value v;
pic_get_args(pic, "o", &v);
2014-09-17 08:58:51 -04:00
return pic_raise_continuable(pic, v);
2014-08-25 00:38:09 -04:00
}
2015-01-07 16:11:48 -05:00
static pic_value
2014-08-25 00:38:09 -04:00
pic_error_error(pic_state *pic)
{
const char *str;
2015-08-26 06:04:27 -04:00
int argc;
2014-08-25 00:38:09 -04:00
pic_value *argv;
pic_get_args(pic, "z*", &str, &argc, &argv);
2016-02-22 12:23:22 -05:00
pic_raise(pic, pic_make_error(pic, "", str, pic_make_list(pic, argc, argv)));
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_error_error_object_p(pic_state *pic)
{
pic_value v;
pic_get_args(pic, "o", &v);
return pic_bool_value(pic, pic_error_p(pic, v));
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_error_error_object_message(pic_state *pic)
{
2016-02-20 02:59:05 -05:00
pic_value e;
2014-08-25 00:38:09 -04:00
2016-02-20 02:59:05 -05:00
pic_get_args(pic, "o", &e);
2014-08-25 00:38:09 -04:00
2016-02-23 08:53:20 -05:00
TYPE_CHECK(pic, e, error);
2016-02-20 02:59:05 -05:00
return pic_obj_value(pic_error_ptr(pic, e)->msg);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_error_error_object_irritants(pic_state *pic)
{
2016-02-20 02:59:05 -05:00
pic_value e;
pic_get_args(pic, "o", &e);
2014-08-25 00:38:09 -04:00
2016-02-23 08:53:20 -05:00
TYPE_CHECK(pic, e, error);
2014-08-25 00:38:09 -04:00
2016-02-20 02:59:05 -05:00
return pic_error_ptr(pic, e)->irrs;
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_error_error_object_type(pic_state *pic)
2014-08-25 00:38:09 -04:00
{
2016-02-20 02:59:05 -05:00
pic_value e;
pic_get_args(pic, "o", &e);
2014-08-25 00:38:09 -04:00
2016-02-23 08:53:20 -05:00
TYPE_CHECK(pic, e, error);
2014-09-16 11:28:55 -04:00
2016-02-20 02:59:05 -05:00
return pic_obj_value(pic_error_ptr(pic, e)->type);
2014-08-25 00:38:09 -04:00
}
void
pic_init_error(pic_state *pic)
{
2016-02-23 08:42:03 -05:00
pic_defvar(pic, "current-exception-handlers", pic_nil_value(pic), pic_false_value(pic));
2014-08-25 00:38:09 -04:00
pic_defun(pic, "with-exception-handler", pic_error_with_exception_handler);
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, "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, "error-object-type", pic_error_error_object_type);
2014-08-25 00:38:09 -04:00
}