handle runtime error

This commit is contained in:
Yuichi Nishiwaki 2013-10-20 23:30:01 +09:00
parent fe88ba7096
commit cefa9a9885
4 changed files with 40 additions and 8 deletions

View File

@ -441,10 +441,11 @@ pic_codegen(pic_state *pic, pic_value obj, struct pic_env *env)
if (! pic->jmp) { if (! pic->jmp) {
jmp_buf jmp; jmp_buf jmp;
if (setjmp(jmp) == 0) {
pic->jmp = &jmp; pic->jmp = &jmp;
if (setjmp(*pic->jmp) != 0) { }
else {
/* error occured */ /* error occured */
pic->jmp = NULL; pic->jmp = NULL;
return NULL; return NULL;
} }

View File

@ -18,6 +18,9 @@ pic_car(pic_state *pic, pic_value obj)
{ {
struct pic_pair *pair; struct pic_pair *pair;
if (! pic_pair_p(obj)) {
pic_error(pic, "pair required");
}
pair = (struct pic_pair *)obj.u.data; pair = (struct pic_pair *)obj.u.data;
return pair->car; return pair->car;
@ -28,6 +31,9 @@ pic_cdr(pic_state *pic, pic_value obj)
{ {
struct pic_pair *pair; struct pic_pair *pair;
if (! pic_pair_p(obj)) {
pic_error(pic, "pair required");
}
pair = (struct pic_pair *)obj.u.data; pair = (struct pic_pair *)obj.u.data;
return pair->cdr; return pair->cdr;

View File

@ -64,6 +64,7 @@ pic_run(pic_state *pic, struct pic_proc *proc, pic_value args)
{ {
struct pic_code *pc; struct pic_code *pc;
int ai = pic_gc_arena_preserve(pic); int ai = pic_gc_arena_preserve(pic);
jmp_buf jmp;
#if PIC_DIRECT_THREADED_VM #if PIC_DIRECT_THREADED_VM
static void *oplabels[] = { static void *oplabels[] = {
@ -76,6 +77,13 @@ pic_run(pic_state *pic, struct pic_proc *proc, pic_value args)
pc = proc->u.irep->code; pc = proc->u.irep->code;
if (setjmp(jmp) == 0) {
pic->jmp = &jmp;
}
else {
goto L_RAISE;
}
/* adjust call frame */ /* adjust call frame */
pic->sp[0] = pic_obj_value(proc); pic->sp[0] = pic_obj_value(proc);
pic->ci->argc = 1; pic->ci->argc = 1;
@ -158,11 +166,17 @@ pic_run(pic_state *pic, struct pic_proc *proc, pic_value args)
pic_value v; pic_value v;
pic_callinfo *ci; pic_callinfo *ci;
L_RAISE:
if (pic->errmsg) {
goto L_STOP;
}
else {
v = POP(); v = POP();
ci = POPCI(); ci = POPCI();
pc = ci->pc; pc = ci->pc;
pic->sp -= ci->argc; pic->sp -= ci->argc;
PUSH(v); PUSH(v);
}
NEXT; NEXT;
} }
CASE(OP_LAMBDA) { CASE(OP_LAMBDA) {
@ -232,8 +246,14 @@ pic_run(pic_state *pic, struct pic_proc *proc, pic_value args)
CASE(OP_STOP) { CASE(OP_STOP) {
pic_value val; pic_value val;
L_STOP:
val = POP(); val = POP();
pic->jmp = NULL;
if (pic->errmsg) {
return pic_undef_value();
}
#if VM_DEBUG #if VM_DEBUG
puts("**VM END STATE**"); puts("**VM END STATE**");
printf("stbase = %p\nsp = %p\n", pic->stbase, pic->sp); printf("stbase = %p\nsp = %p\n", pic->stbase, pic->sp);

View File

@ -87,6 +87,11 @@ main()
goto next; goto next;
} }
v = pic_run(pic, proc, pic_nil_value()); v = pic_run(pic, proc, pic_nil_value());
if (pic_undef_p(v)) {
printf("runtime error: %s\n", pic->errmsg);
pic->errmsg = NULL;
goto next;
}
/* print */ /* print */
printf("=> "); printf("=> ");