picrin/extlib/benz/var.c

102 lines
2.0 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
2015-06-08 09:28:17 -04:00
static pic_value
2016-02-23 06:50:26 -05:00
var_conv(pic_state *pic, pic_value val, pic_value conv)
2015-06-08 09:28:17 -04:00
{
2016-02-23 06:50:26 -05:00
if (! pic_false_p(pic, conv)) {
val = pic_call(pic, conv, 1, val);
2014-08-25 00:38:09 -04:00
}
2016-02-23 06:50:26 -05:00
return val;
2015-06-08 09:28:17 -04:00
}
2014-08-25 00:38:09 -04:00
2015-06-08 09:28:17 -04:00
static pic_value
var_call(pic_state *pic)
{
2016-02-19 10:03:16 -05:00
pic_value self, val;
2015-06-08 09:28:17 -04:00
int n;
2014-08-25 00:38:09 -04:00
n = pic_get_args(pic, "&|o", &self, &val);
2014-08-25 00:38:09 -04:00
2015-06-08 09:28:17 -04:00
if (n == 0) {
2016-02-23 06:50:26 -05:00
return pic_closure_ref(pic, 0);
2015-06-08 09:28:17 -04:00
} else {
2016-02-23 06:50:26 -05:00
pic_closure_set(pic, 0, var_conv(pic, val, pic_closure_ref(pic, 1)));
return pic_undef_value(pic);
}
2014-08-25 00:38:09 -04:00
}
2016-02-19 10:03:16 -05:00
pic_value
pic_make_var(pic_state *pic, pic_value init, pic_value conv)
2014-08-25 00:38:09 -04:00
{
2016-02-23 06:50:26 -05:00
return pic_lambda(pic, var_call, 2, var_conv(pic, init, conv), conv);
}
static pic_value
dynamic_set(pic_state *pic)
{
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;
return pic_undef_value(pic);
}
pic_value
pic_dynamic_bind(pic_state *pic, pic_value var, pic_value val, pic_value thunk)
{
pic_value in, out, new_val, old_val;
2015-06-08 09:28:17 -04:00
2016-02-23 06:50:26 -05:00
old_val = pic_call(pic, var, 0);
new_val = var_conv(pic, val, pic_proc_ptr(pic, var)->locals[1]);
2015-06-08 09:28:17 -04:00
2016-02-23 06:50:26 -05:00
in = pic_lambda(pic, dynamic_set, 2, var, new_val);
out = pic_lambda(pic, dynamic_set, 2, var, old_val);
2014-08-25 00:38:09 -04:00
2016-02-23 06:50:26 -05:00
return pic_dynamic_wind(pic, in, thunk, out);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_var_make_parameter(pic_state *pic)
2014-08-25 00:38:09 -04:00
{
2016-02-19 10:03:16 -05:00
pic_value init, conv = pic_false_value(pic);
2014-08-25 00:38:09 -04:00
pic_get_args(pic, "o|l", &init, &conv);
2014-08-25 00:38:09 -04:00
2016-02-19 10:03:16 -05:00
return pic_make_var(pic, init, conv);
2014-08-25 00:38:09 -04:00
}
2015-06-08 09:28:17 -04:00
static pic_value
2016-02-23 06:50:26 -05:00
pic_var_dynamic_bind(pic_state *pic)
2015-06-08 09:28:17 -04:00
{
2016-02-23 06:50:26 -05:00
pic_value var, val, thunk;
2015-06-08 09:28:17 -04:00
2016-02-23 06:50:26 -05:00
pic_get_args(pic, "lol", &var, &val, &thunk);
2015-06-08 09:28:17 -04:00
2016-02-23 06:50:26 -05:00
if (! (pic_proc_p(pic, var) && pic_proc_ptr(pic, var)->u.f.func == var_call)) {
pic_error(pic, "parameter required", 1, var);
}
2015-06-08 09:28:17 -04:00
2016-02-23 06:50:26 -05:00
return pic_dynamic_bind(pic, var, val, thunk);
2015-06-08 09:28:17 -04:00
}
2014-08-25 00:38:09 -04:00
void
pic_init_var(pic_state *pic)
{
2014-08-31 22:37:52 -04:00
pic_defun(pic, "make-parameter", pic_var_make_parameter);
2016-02-23 06:50:26 -05:00
pic_defun(pic, "dynamic-bind", pic_var_dynamic_bind);
2014-08-25 00:38:09 -04:00
}