picrin/extlib/benz/reg.c

133 lines
2.6 KiB
C
Raw Normal View History

2015-06-09 05:31:46 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
2015-06-24 18:14:29 -04:00
KHASH_DEFINE(reg, void *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal)
2015-06-09 05:31:46 -04:00
struct pic_reg *
pic_make_reg(pic_state *pic)
{
struct pic_reg *reg;
reg = (struct pic_reg *)pic_obj_alloc(pic, sizeof(struct pic_reg), PIC_TT_REG);
reg->prev = NULL;
2015-06-24 18:14:29 -04:00
kh_init(reg, &reg->hash);
2015-06-09 05:31:46 -04:00
return reg;
}
pic_value
pic_reg_ref(pic_state *pic, struct pic_reg *reg, void *key)
{
2015-06-24 18:14:29 -04:00
khash_t(reg) *h = &reg->hash;
khiter_t it;
2015-06-09 05:31:46 -04:00
2015-06-24 18:14:29 -04:00
it = kh_get(reg, h, key);
if (it == kh_end(h)) {
2015-06-09 05:31:46 -04:00
pic_errorf(pic, "element not found for a key: ~s", pic_obj_value(key));
}
2015-06-24 18:14:29 -04:00
return kh_val(h, it);
2015-06-09 05:31:46 -04:00
}
void
pic_reg_set(pic_state PIC_UNUSED(*pic), struct pic_reg *reg, void *key, pic_value val)
{
2015-06-24 18:14:29 -04:00
khash_t(reg) *h = &reg->hash;
int ret;
khiter_t it;
it = kh_put(reg, h, key, &ret);
kh_val(h, it) = val;
2015-06-09 05:31:46 -04:00
}
bool
pic_reg_has(pic_state PIC_UNUSED(*pic), struct pic_reg *reg, void *key)
{
2015-06-24 18:14:29 -04:00
return kh_get(reg, &reg->hash, key) != kh_end(&reg->hash);
2015-06-09 05:31:46 -04:00
}
void
pic_reg_del(pic_state *pic, struct pic_reg *reg, void *key)
{
2015-06-24 18:14:29 -04:00
khash_t(reg) *h = &reg->hash;
khiter_t it;
it = kh_get(reg, h, key);
if (it == kh_end(h)) {
2015-06-09 13:28:32 -04:00
pic_errorf(pic, "no slot named ~s found in register", pic_obj_value(key));
2015-06-09 05:31:46 -04:00
}
2015-06-24 18:14:29 -04:00
kh_del(reg, h, it);
2015-06-09 05:31:46 -04:00
}
2015-06-09 05:50:46 -04:00
static pic_value
reg_get(pic_state *pic, struct pic_reg *reg, void *key)
{
if (! pic_reg_has(pic, reg, key)) {
2015-07-18 02:28:53 -04:00
return pic_false_value();
2015-06-09 05:50:46 -04:00
}
2015-07-18 02:28:53 -04:00
return pic_cons(pic, pic_obj_value(key), pic_reg_ref(pic, reg, key));
2015-06-09 05:50:46 -04:00
}
static pic_value
reg_set(pic_state *pic, struct pic_reg *reg, void *key, pic_value val)
{
if (pic_undef_p(val)) {
if (pic_reg_has(pic, reg, key)) {
pic_reg_del(pic, reg, key);
}
} else {
pic_reg_set(pic, reg, key, val);
}
return pic_undef_value();
}
static pic_value
reg_call(pic_state *pic)
{
struct pic_proc *self = pic_get_proc(pic);
struct pic_reg *reg;
pic_value key, val;
int n;
n = pic_get_args(pic, "o|o", &key, &val);
if (! pic_obj_p(key)) {
2015-06-09 13:28:32 -04:00
pic_errorf(pic, "attempted to set a non-object key '~s' in a register", key);
2015-06-09 05:50:46 -04:00
}
reg = pic_reg_ptr(pic_proc_env_ref(pic, self, "reg"));
if (n == 1) {
return reg_get(pic, reg, pic_obj_ptr(key));
} else {
return reg_set(pic, reg, pic_obj_ptr(key), val);
}
}
static pic_value
2015-06-09 13:28:32 -04:00
pic_reg_make_register(pic_state *pic)
2015-06-09 05:50:46 -04:00
{
struct pic_reg *reg;
struct pic_proc *proc;
pic_get_args(pic, "");
reg = pic_make_reg(pic);
2015-06-27 06:02:18 -04:00
proc = pic_make_proc(pic, reg_call);
2015-06-09 05:50:46 -04:00
pic_proc_env_set(pic, proc, "reg", pic_obj_value(reg));
return pic_obj_value(proc);
}
void
pic_init_reg(pic_state *pic)
{
2015-06-09 13:28:32 -04:00
pic_defun(pic, "make-register", pic_reg_make_register);
2015-06-09 05:50:46 -04:00
}