picrin/extlib/benz/record.c

115 lines
2.3 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
struct pic_record *
2015-08-10 12:34:45 -04:00
pic_make_record(pic_state *pic, struct pic_record *type)
2014-08-25 00:38:09 -04:00
{
struct pic_record *rec;
2015-08-10 12:34:45 -04:00
struct pic_dict *data = pic_make_dict(pic);
2014-08-25 00:38:09 -04:00
rec = (struct pic_record *)pic_obj_alloc(pic, sizeof(struct pic_record), PIC_TT_RECORD);
2015-01-18 07:40:14 -05:00
rec->data = data;
2015-08-10 12:34:45 -04:00
rec->type = type;
2014-08-25 00:38:09 -04:00
2015-08-10 12:34:45 -04:00
if (rec->type == NULL) {
rec->type = rec;
}
2014-08-25 00:38:09 -04:00
return rec;
}
2015-08-10 12:34:45 -04:00
struct pic_record *
pic_record_type(pic_state PIC_UNUSED(*pic), struct pic_record *rec)
2014-08-25 00:38:09 -04:00
{
2015-08-10 12:34:45 -04:00
return rec->type;
2014-08-25 00:38:09 -04:00
}
pic_value
2015-01-20 02:02:28 -05:00
pic_record_ref(pic_state *pic, struct pic_record *rec, pic_sym *slot)
2014-08-25 00:38:09 -04:00
{
2015-01-18 07:40:14 -05:00
if (! pic_dict_has(pic, rec->data, slot)) {
2015-06-19 10:34:12 -04:00
pic_errorf(pic, "slot named ~s is not found for record: ~s", pic_obj_value(slot), pic_obj_value(rec));
2014-08-25 00:38:09 -04:00
}
2015-01-18 07:40:14 -05:00
return pic_dict_ref(pic, rec->data, slot);
2014-08-25 00:38:09 -04:00
}
void
2015-01-20 02:02:28 -05:00
pic_record_set(pic_state *pic, struct pic_record *rec, pic_sym *slot, pic_value val)
2014-08-25 00:38:09 -04:00
{
2015-01-18 07:40:14 -05:00
pic_dict_set(pic, rec->data, slot, val);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_record_make_record(pic_state *pic)
{
struct pic_record * rec;
pic_value rectype;
pic_get_args(pic, "o", &rectype);
2015-08-10 12:34:45 -04:00
pic_assert_type(pic, rectype, record);
rec = pic_make_record(pic, pic_record_ptr(rectype));
2014-08-25 00:38:09 -04:00
return pic_obj_value(rec);
}
static pic_value
pic_record_record_p(pic_state *pic)
{
pic_value rec;
pic_get_args(pic, "o", &rec);
return pic_bool_value(pic_record_p(rec));
}
static pic_value
pic_record_record_type(pic_state *pic)
{
struct pic_record *rec;
pic_get_args(pic, "r", &rec);
2015-08-10 12:34:45 -04:00
return pic_obj_value(pic_record_type(pic, rec));
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_record_record_ref(pic_state *pic)
{
struct pic_record *rec;
2015-01-20 02:02:28 -05:00
pic_sym *slot;
2014-08-25 00:38:09 -04:00
pic_get_args(pic, "rm", &rec, &slot);
return pic_record_ref(pic, rec, slot);
}
static pic_value
pic_record_record_set(pic_state *pic)
{
struct pic_record *rec;
2015-01-20 02:02:28 -05:00
pic_sym *slot;
2014-08-25 00:38:09 -04:00
pic_value val;
pic_get_args(pic, "rmo", &rec, &slot, &val);
pic_record_set(pic, rec, slot, val);
2015-06-09 03:34:45 -04:00
return pic_undef_value();
2014-08-25 00:38:09 -04:00
}
void
pic_init_record(pic_state *pic)
{
2014-08-31 22:37:52 -04:00
pic_defun(pic, "make-record", pic_record_make_record);
pic_defun(pic, "record?", pic_record_record_p);
pic_defun(pic, "record-type", pic_record_record_type);
pic_defun(pic, "record-ref", pic_record_record_ref);
pic_defun(pic, "record-set!", pic_record_record_set);
2015-08-10 12:34:45 -04:00
pic_define(pic, "<record-type>", pic_obj_value(pic_make_record(pic, NULL)));
2014-08-25 00:38:09 -04:00
}