picrin/extlib/benz/cont.c

317 lines
6.2 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
2014-09-18 09:33:20 -04:00
void
2015-06-08 08:04:04 -04:00
pic_wind(pic_state *pic, pic_checkpoint *here, pic_checkpoint *there)
2014-09-18 09:33:20 -04:00
{
if (here == there)
return;
if (here->depth < there->depth) {
pic_wind(pic, here, there->prev);
pic_apply0(pic, there->in);
}
else {
pic_apply0(pic, there->out);
pic_wind(pic, here->prev, there);
}
}
pic_value
pic_dynamic_wind(pic_state *pic, struct pic_proc *in, struct pic_proc *thunk, struct pic_proc *out)
{
2015-06-08 08:04:04 -04:00
pic_checkpoint *here;
2014-09-18 09:33:20 -04:00
pic_value val;
if (in != NULL) {
pic_apply0(pic, in); /* enter */
}
2015-06-08 08:04:04 -04:00
here = pic->cp;
2015-06-22 04:06:13 -04:00
pic->cp = (pic_checkpoint *)pic_obj_alloc(pic, sizeof(pic_checkpoint), PIC_TT_CP);
2015-06-08 08:04:04 -04:00
pic->cp->prev = here;
pic->cp->depth = here->depth + 1;
pic->cp->in = in;
pic->cp->out = out;
2014-09-18 09:33:20 -04:00
val = pic_apply0(pic, thunk);
2015-06-08 08:04:04 -04:00
pic->cp = here;
2014-09-18 09:33:20 -04:00
if (out != NULL) {
pic_apply0(pic, out); /* exit */
}
return val;
}
2014-09-24 07:06:14 -04:00
void
2015-06-01 17:32:43 -04:00
pic_save_point(pic_state *pic, struct pic_cont *cont)
2014-09-19 03:41:49 -04:00
{
/* save runtime context */
2015-06-08 08:04:04 -04:00
cont->cp = pic->cp;
2015-06-01 17:32:43 -04:00
cont->sp_offset = pic->sp - pic->stbase;
cont->ci_offset = pic->ci - pic->cibase;
cont->xp_offset = pic->xp - pic->xpbase;
cont->arena_idx = pic->arena_idx;
cont->ip = pic->ip;
2015-06-08 09:28:17 -04:00
cont->ptable = pic->ptable;
2015-06-22 14:02:12 -04:00
cont->prev = pic->cc;
2015-06-01 17:32:43 -04:00
cont->results = pic_undef_value();
cont->id = pic->ccnt++;
2015-06-22 14:02:12 -04:00
pic->cc = cont;
2014-09-19 03:41:49 -04:00
}
void
2015-06-01 17:32:43 -04:00
pic_load_point(pic_state *pic, struct pic_cont *cont)
2014-09-19 03:41:49 -04:00
{
2015-06-08 08:04:04 -04:00
pic_wind(pic, pic->cp, cont->cp);
2014-09-19 03:41:49 -04:00
/* load runtime context */
2015-06-08 08:04:04 -04:00
pic->cp = cont->cp;
2015-06-01 17:32:43 -04:00
pic->sp = pic->stbase + cont->sp_offset;
pic->ci = pic->cibase + cont->ci_offset;
pic->xp = pic->xpbase + cont->xp_offset;
pic->arena_idx = cont->arena_idx;
pic->ip = cont->ip;
2015-06-08 09:28:17 -04:00
pic->ptable = cont->ptable;
pic->cc = cont->prev;
2014-09-19 03:41:49 -04:00
}
2015-01-07 16:11:48 -05:00
static pic_value
2015-06-01 17:32:43 -04:00
cont_call(pic_state *pic)
2014-09-19 03:41:49 -04:00
{
struct pic_proc *self = pic_get_proc(pic);
2014-09-27 07:17:02 -04:00
size_t argc;
2014-09-19 03:41:49 -04:00
pic_value *argv;
int id;
struct pic_cont *cc, *cont;
2014-09-19 03:41:49 -04:00
pic_get_args(pic, "*", &argc, &argv);
id = pic_int(pic_proc_env_ref(pic, self, "id"));
/* check if continuation is alive */
for (cc = pic->cc; cc != NULL; cc = cc->prev) {
if (cc->id == id) {
break;
}
}
if (cc == NULL) {
pic_errorf(pic, "calling dead escape continuation");
}
cont = pic_data_ptr(pic_proc_env_ref(pic, self, "escape"))->data;
cont->results = pic_list_by_array(pic, argc, argv);
2014-09-19 03:41:49 -04:00
pic_load_point(pic, cont);
PIC_LONGJMP(pic, cont->jmp, 1);
2015-05-27 11:41:55 -04:00
PIC_UNREACHABLE();
2014-09-19 03:41:49 -04:00
}
2014-09-24 02:34:46 -04:00
struct pic_proc *
2015-06-01 17:32:43 -04:00
pic_make_cont(pic_state *pic, struct pic_cont *cont)
2014-09-24 01:57:49 -04:00
{
2015-06-22 14:02:12 -04:00
static const pic_data_type cont_type = { "cont", NULL, NULL };
2015-06-01 17:32:43 -04:00
struct pic_proc *c;
2014-09-24 01:57:49 -04:00
struct pic_data *e;
2015-06-27 06:02:18 -04:00
c = pic_make_proc(pic, cont_call);
2014-09-24 01:57:49 -04:00
2015-06-01 17:32:43 -04:00
e = pic_data_alloc(pic, &cont_type, cont);
2014-09-24 01:57:49 -04:00
/* save the escape continuation in proc */
2015-06-01 06:47:40 -04:00
pic_proc_env_set(pic, c, "escape", pic_obj_value(e));
pic_proc_env_set(pic, c, "id", pic_int_value(cont->id));
2014-09-24 01:57:49 -04:00
2015-06-01 17:32:43 -04:00
return c;
2014-09-24 01:57:49 -04:00
}
2014-09-19 03:41:49 -04:00
pic_value
pic_callcc(pic_state *pic, struct pic_proc *proc)
2014-09-19 03:41:49 -04:00
{
2015-06-22 14:02:12 -04:00
struct pic_cont cont;
2014-09-19 03:41:49 -04:00
2015-06-22 14:02:12 -04:00
pic_save_point(pic, &cont);
2014-09-19 03:41:49 -04:00
2015-06-22 14:02:12 -04:00
if (PIC_SETJMP(pic, cont.jmp)) {
return pic_values_by_list(pic, cont.results);
2014-09-19 03:41:49 -04:00
}
else {
pic_value val;
2015-06-22 14:02:12 -04:00
val = pic_apply1(pic, proc, pic_obj_value(pic_make_cont(pic, &cont)));
2014-09-19 03:41:49 -04:00
2015-06-22 14:02:12 -04:00
pic->cc = pic->cc->prev;
2014-09-19 03:41:49 -04:00
return val;
}
}
2015-06-28 15:25:47 -04:00
static pic_value
pic_va_values(pic_state *pic, size_t n, ...)
{
2015-07-12 20:27:12 -04:00
pic_vec *args = pic_make_vec(pic, n);
2015-06-28 15:25:47 -04:00
va_list ap;
size_t i = 0;
va_start(ap, n);
while (i < n) {
2015-07-12 20:27:12 -04:00
args->data[i++] = va_arg(ap, pic_value);
2015-06-28 15:25:47 -04:00
}
va_end(ap);
2015-07-12 20:27:12 -04:00
return pic_values(pic, n, args->data);
2015-06-28 15:25:47 -04:00
}
2014-08-25 00:38:09 -04:00
pic_value
pic_values0(pic_state *pic)
{
2015-06-28 15:25:47 -04:00
return pic_va_values(pic, 0);
2014-08-25 00:38:09 -04:00
}
pic_value
pic_values1(pic_state *pic, pic_value arg1)
{
2015-06-28 15:25:47 -04:00
return pic_va_values(pic, 1, arg1);
2014-08-25 00:38:09 -04:00
}
pic_value
pic_values2(pic_state *pic, pic_value arg1, pic_value arg2)
{
2015-06-28 15:25:47 -04:00
return pic_va_values(pic, 2, arg1, arg2);
2014-08-25 00:38:09 -04:00
}
pic_value
pic_values3(pic_state *pic, pic_value arg1, pic_value arg2, pic_value arg3)
{
2015-06-28 15:25:47 -04:00
return pic_va_values(pic, 3, arg1, arg2, arg3);
2014-08-25 00:38:09 -04:00
}
pic_value
pic_values4(pic_state *pic, pic_value arg1, pic_value arg2, pic_value arg3, pic_value arg4)
{
2015-06-28 15:25:47 -04:00
return pic_va_values(pic, 4, arg1, arg2, arg3, arg4);
2014-08-25 00:38:09 -04:00
}
pic_value
pic_values5(pic_state *pic, pic_value arg1, pic_value arg2, pic_value arg3, pic_value arg4, pic_value arg5)
{
2015-06-28 15:25:47 -04:00
return pic_va_values(pic, 5, arg1, arg2, arg3, arg4, arg5);
2014-08-25 00:38:09 -04:00
}
pic_value
2015-06-28 15:25:47 -04:00
pic_values(pic_state *pic, size_t argc, pic_value *argv)
2014-08-25 00:38:09 -04:00
{
2014-09-27 07:21:54 -04:00
size_t i;
2014-08-25 00:38:09 -04:00
for (i = 0; i < argc; ++i) {
pic->sp[i] = argv[i];
}
2014-09-27 07:21:54 -04:00
pic->ci->retc = (int)argc;
2014-08-25 00:38:09 -04:00
2015-06-09 03:34:45 -04:00
return argc == 0 ? pic_undef_value() : pic->sp[0];
2014-08-25 00:38:09 -04:00
}
pic_value
pic_values_by_list(pic_state *pic, pic_value list)
{
2015-01-22 05:28:31 -05:00
pic_value v, it;
int i;
2014-08-25 00:38:09 -04:00
i = 0;
2015-01-22 05:28:31 -05:00
pic_for_each (v, list, it) {
2014-08-25 00:38:09 -04:00
pic->sp[i++] = v;
}
pic->ci->retc = i;
2015-06-09 03:34:45 -04:00
return pic_nil_p(list) ? pic_undef_value() : pic->sp[0];
2014-08-25 00:38:09 -04:00
}
2014-09-27 07:21:54 -04:00
size_t
pic_receive(pic_state *pic, size_t n, pic_value *argv)
2014-08-25 00:38:09 -04:00
{
pic_callinfo *ci;
2014-09-27 07:21:54 -04:00
size_t i, retc;
2014-08-25 00:38:09 -04:00
/* take info from discarded frame */
ci = pic->ci + 1;
2014-09-27 07:21:54 -04:00
retc = (size_t)ci->retc;
2014-08-25 00:38:09 -04:00
for (i = 0; i < retc && i < n; ++i) {
argv[i] = ci->fp[i];
}
return retc;
}
static pic_value
pic_cont_callcc(pic_state *pic)
{
struct pic_proc *cb;
pic_get_args(pic, "l", &cb);
return pic_callcc(pic, cb);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_cont_dynamic_wind(pic_state *pic)
{
struct pic_proc *in, *thunk, *out;
pic_get_args(pic, "lll", &in, &thunk, &out);
return pic_dynamic_wind(pic, in, thunk, out);
}
static pic_value
pic_cont_values(pic_state *pic)
{
2014-09-27 07:17:02 -04:00
size_t argc;
2014-08-25 00:38:09 -04:00
pic_value *argv;
pic_get_args(pic, "*", &argc, &argv);
2015-06-28 15:25:47 -04:00
return pic_values(pic, argc, argv);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_cont_call_with_values(pic_state *pic)
{
struct pic_proc *producer, *consumer;
2015-07-12 20:27:12 -04:00
size_t argc;
pic_vec *args;
2014-08-25 00:38:09 -04:00
pic_get_args(pic, "ll", &producer, &consumer);
2015-08-10 09:13:16 -04:00
pic_apply0(pic, producer);
2014-08-25 00:38:09 -04:00
2015-07-12 20:27:12 -04:00
argc = pic_receive(pic, 0, NULL);
args = pic_make_vec(pic, argc);
2015-07-04 05:01:30 -04:00
2015-07-12 20:27:12 -04:00
pic_receive(pic, argc, args->data);
2014-08-25 00:38:09 -04:00
2015-07-12 20:27:12 -04:00
return pic_apply_trampoline(pic, consumer, argc, args->data);
2014-08-25 00:38:09 -04:00
}
void
pic_init_cont(pic_state *pic)
{
pic_defun(pic, "call-with-current-continuation", pic_cont_callcc);
2014-09-08 06:38:33 -04:00
pic_defun(pic, "call/cc", pic_cont_callcc);
2015-07-06 01:19:12 -04:00
pic_defun(pic, "escape", pic_cont_callcc);
2014-08-25 00:38:09 -04:00
pic_defun(pic, "dynamic-wind", pic_cont_dynamic_wind);
2015-06-04 00:53:41 -04:00
2015-07-01 17:17:27 -04:00
pic_defun(pic, "values", pic_cont_values);
pic_defun(pic, "call-with-values", pic_cont_call_with_values);
2014-08-25 00:38:09 -04:00
}