picrin/src/pair.c

65 lines
1.1 KiB
C
Raw Normal View History

2013-10-10 04:06:26 -04:00
#include "picrin.h"
2013-10-19 23:34:57 -04:00
#include "picrin/pair.h"
2013-10-10 04:06:26 -04:00
pic_value
pic_cons(pic_state *pic, pic_value car, pic_value cdr)
{
struct pic_pair *pair;
2013-10-13 03:55:07 -04:00
pair = (struct pic_pair *)pic_obj_alloc(pic, sizeof(struct pic_pair), PIC_TT_PAIR);
2013-10-10 04:06:26 -04:00
pair->car = car;
pair->cdr = cdr;
return pic_obj_value(pair);
}
pic_value
pic_car(pic_state *pic, pic_value obj)
{
struct pic_pair *pair;
2013-10-20 10:30:01 -04:00
if (! pic_pair_p(obj)) {
pic_error(pic, "pair required");
}
2013-10-10 04:54:35 -04:00
pair = (struct pic_pair *)obj.u.data;
2013-10-10 04:06:26 -04:00
return pair->car;
}
pic_value
pic_cdr(pic_state *pic, pic_value obj)
{
struct pic_pair *pair;
2013-10-20 10:30:01 -04:00
if (! pic_pair_p(obj)) {
pic_error(pic, "pair required");
}
2013-10-10 04:54:35 -04:00
pair = (struct pic_pair *)obj.u.data;
2013-10-10 04:06:26 -04:00
return pair->cdr;
}
2013-10-19 23:34:57 -04:00
pic_value
pic_assq(pic_state *pic, pic_value key, pic_value assoc)
{
pic_value cell;
enter:
if (pic_nil_p(assoc))
return assoc;
cell = pic_car(pic, assoc);
if (pic_eq_p(pic, key, pic_car(pic, cell)))
return cell;
assoc = pic_cdr(pic, assoc);
goto enter;
}
2013-10-20 01:04:39 -04:00
pic_value
pic_acons(pic_state *pic, pic_value key, pic_value val, pic_value assoc)
{
return pic_cons(pic, pic_cons(pic, key, val), assoc);
}