picrin/extlib/benz/var.c

119 lines
2.4 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
static pic_value
2015-06-08 09:28:17 -04:00
var_conv(pic_state *pic, struct pic_proc *var, pic_value val)
2014-08-25 00:38:09 -04:00
{
2015-06-08 09:28:17 -04:00
if (pic_proc_env_has(pic, var, "conv") != 0) {
return pic_apply1(pic, pic_proc_ptr(pic_proc_env_ref(pic, var, "conv")), val);
2014-08-25 00:38:09 -04:00
}
2015-06-08 09:28:17 -04:00
return val;
}
static pic_value
var_get(pic_state *pic, struct pic_proc *var)
{
pic_value elem, it;
pic_sym *id;
struct pic_dict *dict;
2014-08-25 00:38:09 -04:00
2015-06-08 09:28:17 -04:00
id = pic_sym_ptr(pic_proc_env_ref(pic, var, "id"));
2015-06-08 09:28:17 -04:00
pic_for_each (elem, pic->ptable, it) {
dict = pic_dict_ptr(elem);
if (pic_dict_has(pic, dict, id)) {
return pic_dict_ref(pic, dict, id);
}
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
{
2015-06-08 09:28:17 -04:00
pic_sym *id;
struct pic_dict *dict;
2014-08-25 00:38:09 -04:00
2015-06-08 09:28:17 -04:00
id = pic_sym_ptr(pic_proc_env_ref(pic, var, "id"));
2014-08-25 00:38:09 -04:00
2015-06-08 09:28:17 -04:00
dict = pic_dict_ptr(pic_car(pic, pic->ptable));
2014-08-25 00:38:09 -04:00
2015-06-08 09:41:43 -04:00
pic_dict_set(pic, dict, id, val);
2014-08-25 00:38:09 -04:00
2015-06-08 09:28:17 -04:00
return pic_none_value();
}
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 = pic_get_proc(pic);
pic_value val;
int n;
2014-08-25 00:38:09 -04:00
2015-06-08 09:28:17 -04:00
n = pic_get_args(pic, "|o", &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 {
2015-06-08 09:41:43 -04:00
return var_set(pic, self, var_conv(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;
2015-06-08 09:28:17 -04:00
pic_value converter = conv ? pic_obj_value(conv) : pic_false_value();
pic_sym *id;
2014-08-25 00:38:09 -04:00
var = pic_make_proc(pic, var_call, "<var-call>");
2015-06-08 09:28:17 -04:00
if (conv != NULL) {
pic_proc_env_set(pic, var, "conv", converter);
}
id = pic_intern(pic, pic_format(pic, "%d", pic->pnum++));
pic_proc_env_set(pic, var, "id", pic_obj_value(id));
pic_apply1(pic, var, 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);
pic->ptable = pic_cons(pic, pic_obj_value(pic_make_dict(pic)), pic->ptable);
val = pic_apply0(pic, body);
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
}