picrin/extlib/benz/var.c

106 lines
1.9 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
2015-06-08 09:28:17 -04:00
static pic_value
var_get(pic_state *pic, struct pic_proc *var)
{
pic_value elem, it;
2016-02-10 07:50:39 -05:00
struct pic_weak *weak;
2015-06-08 09:28:17 -04:00
pic_for_each (elem, pic->ptable, it) {
2016-02-10 07:50:39 -05:00
weak = pic_weak_ptr(elem);
if (pic_weak_has(pic, weak, var)) {
return pic_weak_ref(pic, weak, var);
}
2014-08-25 00:38:09 -04:00
}
2015-06-08 09:28:17 -04:00
pic_panic(pic, "logic flaw");
2014-08-25 00:38:09 -04:00
}
static pic_value
2015-06-08 09:28:17 -04:00
var_set(pic_state *pic, struct pic_proc *var, pic_value val)
2014-08-25 00:38:09 -04:00
{
2016-02-10 07:50:39 -05:00
struct pic_weak *weak;
2014-08-25 00:38:09 -04:00
2016-02-10 07:50:39 -05:00
weak = pic_weak_ptr(pic_car(pic, pic->ptable));
2014-08-25 00:38:09 -04:00
2016-02-10 07:50:39 -05:00
pic_weak_set(pic, weak, var, val);
2014-08-25 00:38:09 -04:00
return pic_undef_value(pic);
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)
{
struct pic_proc *self;
2015-06-08 09:28:17 -04:00
pic_value val;
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) {
return var_get(pic, self);
} else {
pic_value conv;
conv = pic_closure_ref(pic, 0);
if (! pic_false_p(pic, conv)) {
val = pic_call(pic, pic_proc_ptr(conv), 1, val);
}
return var_set(pic, self, val);
}
2014-08-25 00:38:09 -04:00
}
struct pic_proc *
pic_make_var(pic_state *pic, pic_value init, struct pic_proc *conv)
2014-08-25 00:38:09 -04:00
{
struct pic_proc *var;
pic_value c = pic_false_value(pic);
2015-06-08 09:28:17 -04:00
if (conv != NULL) {
c = pic_obj_value(conv);
2015-06-08 09:28:17 -04:00
}
var = pic_lambda(pic, var_call, 1, c);
2015-06-08 09:28:17 -04:00
2016-02-13 23:46:55 -05:00
pic_call(pic, var, 1, init);
2014-08-25 00:38:09 -04:00
return var;
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
{
struct pic_proc *conv = NULL;
pic_value init;
2014-08-25 00:38:09 -04:00
pic_get_args(pic, "o|l", &init, &conv);
2014-08-25 00:38:09 -04:00
return pic_obj_value(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
pic_var_with_parameter(pic_state *pic)
{
struct pic_proc *body;
pic_value val;
pic_get_args(pic, "l", &body);
2016-02-10 07:50:39 -05:00
pic->ptable = pic_cons(pic, pic_obj_value(pic_make_weak(pic)), pic->ptable);
2015-06-08 09:28:17 -04:00
2016-02-13 23:46:55 -05:00
val = pic_call(pic, body, 0);
2015-06-08 09:28:17 -04:00
pic->ptable = pic_cdr(pic, pic->ptable);
return val;
}
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);
2015-06-08 09:28:17 -04:00
pic_defun(pic, "with-parameter", pic_var_with_parameter);
2014-08-25 00:38:09 -04:00
}