picrin/lib/ext/cont.c

124 lines
2.6 KiB
C
Raw Normal View History

2017-04-25 01:08:37 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
2017-05-05 23:53:20 -04:00
#include "../value.h"
2017-04-25 01:08:37 -04:00
#include "../object.h"
#include "../state.h"
#if PIC_USE_CALLCC
2017-04-25 13:10:40 -04:00
/*
* [(reset e)]k = k ([e] halt ())
* [(shift e)]k = [e] halt (\c x, c (k x))
*/
static pic_value
pic_cont_reset(pic_state *pic)
{
pic_value thunk;
2017-04-29 10:48:32 -04:00
struct context cxt;
2017-04-25 13:10:40 -04:00
pic_get_args(pic, "l", &thunk);
2017-04-29 10:48:32 -04:00
CONTEXT_INITK(pic, &cxt, thunk, pic->halt, 0, (pic_value *) NULL);
cxt.reset = 1;
pic_vm(pic, &cxt);
return pic_protect(pic, cxt.fp->regs[1]);
2017-04-25 13:10:40 -04:00
}
static pic_value
shift_call(pic_state *pic)
{
pic_value x;
struct context cxt;
pic_get_args(pic, "o", &x);
CONTEXT_INIT(pic, &cxt, pic_closure_ref(pic, 0), 1, &x);
2017-04-29 10:48:32 -04:00
cxt.reset = 1;
pic->dyn_env = pic_closure_ref(pic, 1);
2017-04-25 13:10:40 -04:00
pic_vm(pic, &cxt);
return pic_protect(pic, cxt.fp->regs[1]);
}
static pic_value
pic_cont_shift(pic_state *pic)
{
pic_value f, k;
pic_get_args(pic, "l", &f);
2017-04-29 10:48:32 -04:00
if (! pic->cxt->reset) {
pic_error(pic, "c function call interleaved in delimited continuation", 0);
}
k = pic_lambda(pic, shift_call, 2, pic->cxt->fp->regs[1], pic->dyn_env);
2017-04-25 13:10:40 -04:00
CONTEXT_INITK(pic, pic->cxt, f, pic->halt, 1, &k);
return pic_invalid_value(pic);
}
2017-04-25 01:08:37 -04:00
static pic_value
cont_call(pic_state *pic)
{
int argc;
pic_value *argv, k, dyn_env;
2017-04-25 13:30:17 -04:00
struct context *cxt;
2017-04-25 01:08:37 -04:00
pic_get_args(pic, "*", &argc, &argv);
2017-04-25 13:30:17 -04:00
if (! pic_bool(pic, pic_closure_ref(pic, 0))) {
2017-04-25 01:08:37 -04:00
pic_error(pic, "calling dead escape continuation", 0);
}
2017-04-25 13:30:17 -04:00
cxt = pic_data(pic, pic_closure_ref(pic, 1));
k = pic_closure_ref(pic, 2);
dyn_env = pic_closure_ref(pic, 3);
2017-04-25 13:10:40 -04:00
CONTEXT_INIT(pic, cxt, k, argc, argv);
2017-04-25 01:08:37 -04:00
2017-04-25 13:30:17 -04:00
while (pic->cxt != cxt) {
pic_value c, it;
pic_for_each (c, pic->cxt->conts, it) {
proc_ptr(pic, c)->env->regs[0] = pic_false_value(pic);
}
pic->cxt = pic->cxt->prev;
}
2017-04-25 01:08:37 -04:00
pic->dyn_env = dyn_env;
longjmp(cxt->jmp, 1);
PIC_UNREACHABLE();
}
pic_value
pic_make_cont(pic_state *pic, pic_value k)
{
static const pic_data_type cxt_type = { "cxt", NULL };
2017-04-25 13:30:17 -04:00
pic_value c;
c = pic_lambda(pic, cont_call, 4, pic_true_value(pic), pic_data_value(pic, pic->cxt, &cxt_type), k, pic->dyn_env);
pic->cxt->conts = pic_cons(pic, c, pic->cxt->conts);
return c;
2017-04-25 01:08:37 -04:00
}
static pic_value
pic_cont_callcc(pic_state *pic)
{
2017-04-25 13:10:40 -04:00
pic_value f;
2017-04-25 01:08:37 -04:00
pic_get_args(pic, "l", &f);
2017-04-25 13:10:40 -04:00
return pic_callk(pic, f, 1, pic_make_cont(pic, pic->cxt->fp->regs[1]));
2017-04-25 01:08:37 -04:00
}
void
pic_init_cont(pic_state *pic)
{
pic_defun(pic, "call-with-current-continuation", pic_cont_callcc);
pic_defun(pic, "call/cc", pic_cont_callcc);
2017-04-25 13:10:40 -04:00
pic_defun(pic, "shift", pic_cont_shift);
pic_defun(pic, "reset", pic_cont_reset);
2017-04-25 01:08:37 -04:00
}
#endif /* PIC_USE_CALCC */