revert 48f0ec90
. dicitonary is now symbol-to-object structure
This commit is contained in:
parent
3e9da6d3e3
commit
6968c85282
|
@ -24,13 +24,13 @@ pic_attr(pic_state *pic, pic_value obj)
|
|||
pic_value
|
||||
pic_attr_ref(pic_state *pic, pic_value obj, const char *key)
|
||||
{
|
||||
return pic_dict_ref(pic, pic_attr(pic, obj), pic_sym_value(pic_intern_cstr(pic, key)));
|
||||
return pic_dict_ref(pic, pic_attr(pic, obj), pic_intern_cstr(pic, key));
|
||||
}
|
||||
|
||||
void
|
||||
pic_attr_set(pic_state *pic, pic_value obj, const char *key, pic_value v)
|
||||
{
|
||||
pic_dict_set(pic, pic_attr(pic, obj), pic_sym_value(pic_intern_cstr(pic, key)), v);
|
||||
pic_dict_set(pic, pic_attr(pic, obj), pic_intern_cstr(pic, key), v);
|
||||
}
|
||||
|
||||
static pic_value
|
||||
|
|
|
@ -7,98 +7,35 @@
|
|||
#include "picrin/cont.h"
|
||||
#include "picrin/pair.h"
|
||||
|
||||
static int
|
||||
xh_value_hash(const void *key, void *data)
|
||||
{
|
||||
union { double f; int i; } u;
|
||||
pic_value val = *(pic_value *)key;
|
||||
int hash, vtype;
|
||||
|
||||
PIC_UNUSED(data);
|
||||
|
||||
vtype = pic_vtype(val);
|
||||
|
||||
switch (vtype) {
|
||||
default:
|
||||
hash = 0;
|
||||
break;
|
||||
case PIC_VTYPE_SYMBOL:
|
||||
hash = pic_sym(val);
|
||||
break;
|
||||
case PIC_VTYPE_FLOAT:
|
||||
u.f = pic_float(val);
|
||||
hash = u.i;
|
||||
break;
|
||||
case PIC_VTYPE_INT:
|
||||
hash = pic_int(val);
|
||||
break;
|
||||
case PIC_VTYPE_HEAP:
|
||||
hash = (int)(intptr_t)pic_ptr(val);
|
||||
break;
|
||||
}
|
||||
|
||||
return hash + vtype;
|
||||
}
|
||||
|
||||
static int
|
||||
xh_value_equal(const void *key1, const void *key2, void *pic)
|
||||
{
|
||||
return pic_equal_p(pic, *(pic_value *)key1, *(pic_value *)key2);
|
||||
}
|
||||
|
||||
static void
|
||||
xh_init_value(pic_state *pic, xhash *x)
|
||||
{
|
||||
xh_init_(x, sizeof(pic_value), sizeof(pic_value), xh_value_hash, xh_value_equal, pic);
|
||||
}
|
||||
|
||||
static inline xh_entry *
|
||||
xh_get_value(xhash *x, pic_value key)
|
||||
{
|
||||
return xh_get_(x, &key);
|
||||
}
|
||||
|
||||
static inline xh_entry *
|
||||
xh_put_value(xhash *x, pic_value key, void *val)
|
||||
{
|
||||
return xh_put_(x, &key, val);
|
||||
}
|
||||
|
||||
static inline void
|
||||
xh_del_value(xhash *x, pic_value key)
|
||||
{
|
||||
xh_del_(x, &key);
|
||||
}
|
||||
|
||||
struct pic_dict *
|
||||
pic_make_dict(pic_state *pic)
|
||||
{
|
||||
struct pic_dict *dict;
|
||||
|
||||
dict = (struct pic_dict *)pic_obj_alloc(pic, sizeof(struct pic_dict), PIC_TT_DICT);
|
||||
xh_init_value(pic, &dict->hash);
|
||||
xh_init_int(&dict->hash, sizeof(pic_value));
|
||||
|
||||
return dict;
|
||||
}
|
||||
|
||||
pic_value
|
||||
pic_dict_ref(pic_state *pic, struct pic_dict *dict, pic_value key)
|
||||
pic_dict_ref(pic_state *pic, struct pic_dict *dict, pic_sym key)
|
||||
{
|
||||
xh_entry *e;
|
||||
|
||||
e = xh_get_value(&dict->hash, key);
|
||||
e = xh_get_int(&dict->hash, key);
|
||||
if (! e) {
|
||||
pic_errorf(pic, "element not found for a key: ~s", key);
|
||||
pic_errorf(pic, "element not found for a key: ~s", pic_sym_value(key));
|
||||
}
|
||||
return xh_val(e, pic_value);
|
||||
}
|
||||
|
||||
void
|
||||
pic_dict_set(pic_state *pic, struct pic_dict *dict, pic_value key, pic_value val)
|
||||
pic_dict_set(pic_state *pic, struct pic_dict *dict, pic_sym key, pic_value val)
|
||||
{
|
||||
PIC_UNUSED(pic);
|
||||
|
||||
xh_put_value(&dict->hash, key, &val);
|
||||
xh_put_int(&dict->hash, key, &val);
|
||||
}
|
||||
|
||||
size_t
|
||||
|
@ -110,21 +47,21 @@ pic_dict_size(pic_state *pic, struct pic_dict *dict)
|
|||
}
|
||||
|
||||
bool
|
||||
pic_dict_has(pic_state *pic, struct pic_dict *dict, pic_value key)
|
||||
pic_dict_has(pic_state *pic, struct pic_dict *dict, pic_sym key)
|
||||
{
|
||||
PIC_UNUSED(pic);
|
||||
|
||||
return xh_get_value(&dict->hash, key) != NULL;
|
||||
return xh_get_int(&dict->hash, key) != NULL;
|
||||
}
|
||||
|
||||
void
|
||||
pic_dict_del(pic_state *pic, struct pic_dict *dict, pic_value key)
|
||||
pic_dict_del(pic_state *pic, struct pic_dict *dict, pic_sym key)
|
||||
{
|
||||
if (xh_get_value(&dict->hash, key) == NULL) {
|
||||
pic_errorf(pic, "no slot named ~s found in dictionary", key);
|
||||
if (xh_get_int(&dict->hash, key) == NULL) {
|
||||
pic_errorf(pic, "no slot named ~s found in dictionary", pic_sym_value(key));
|
||||
}
|
||||
|
||||
xh_del_value(&dict->hash, key);
|
||||
xh_del_int(&dict->hash, key);
|
||||
}
|
||||
|
||||
static pic_value
|
||||
|
@ -151,7 +88,8 @@ pic_dict_dictionary(pic_state *pic)
|
|||
dict = pic_make_dict(pic);
|
||||
|
||||
for (i = 0; i < argc; i += 2) {
|
||||
pic_dict_set(pic, dict, argv[i], argv[i+1]);
|
||||
pic_assert_type(pic, argv[i], sym);
|
||||
pic_dict_set(pic, dict, pic_sym(argv[i]), argv[i+1]);
|
||||
}
|
||||
|
||||
return pic_obj_value(dict);
|
||||
|
@ -171,9 +109,9 @@ static pic_value
|
|||
pic_dict_dictionary_ref(pic_state *pic)
|
||||
{
|
||||
struct pic_dict *dict;
|
||||
pic_value key;
|
||||
pic_sym key;
|
||||
|
||||
pic_get_args(pic, "do", &dict, &key);
|
||||
pic_get_args(pic, "dm", &dict, &key);
|
||||
|
||||
if (pic_dict_has(pic, dict, key)) {
|
||||
return pic_values2(pic, pic_dict_ref(pic, dict, key), pic_true_value());
|
||||
|
@ -186,9 +124,10 @@ static pic_value
|
|||
pic_dict_dictionary_set(pic_state *pic)
|
||||
{
|
||||
struct pic_dict *dict;
|
||||
pic_value key, val;
|
||||
pic_sym key;
|
||||
pic_value val;
|
||||
|
||||
pic_get_args(pic, "doo", &dict, &key, &val);
|
||||
pic_get_args(pic, "dmo", &dict, &key, &val);
|
||||
|
||||
pic_dict_set(pic, dict, key, val);
|
||||
|
||||
|
@ -199,9 +138,9 @@ static pic_value
|
|||
pic_dict_dictionary_del(pic_state *pic)
|
||||
{
|
||||
struct pic_dict *dict;
|
||||
pic_value key;
|
||||
pic_sym key;
|
||||
|
||||
pic_get_args(pic, "do", &dict, &key);
|
||||
pic_get_args(pic, "dm", &dict, &key);
|
||||
|
||||
pic_dict_del(pic, dict, key);
|
||||
|
||||
|
@ -215,7 +154,7 @@ pic_dict_dictionary_size(pic_state *pic)
|
|||
|
||||
pic_get_args(pic, "d", &dict);
|
||||
|
||||
return pic_size_value(pic_dict_size(pic, dict));
|
||||
return pic_int_value(pic_dict_size(pic, dict));
|
||||
}
|
||||
|
||||
static pic_value
|
||||
|
@ -228,7 +167,7 @@ pic_dict_dictionary_to_alist(pic_state *pic)
|
|||
pic_get_args(pic, "d", &dict);
|
||||
|
||||
for (it = xh_begin(&dict->hash); it != NULL; it = xh_next(it)) {
|
||||
item = pic_cons(pic, xh_key(it, pic_value), xh_val(it, pic_value));
|
||||
item = pic_cons(pic, pic_sym_value(xh_key(it, pic_sym)), xh_val(it, pic_value));
|
||||
pic_push(pic, item, alist);
|
||||
}
|
||||
|
||||
|
@ -246,7 +185,8 @@ pic_dict_alist_to_dictionary(pic_state *pic)
|
|||
dict = pic_make_dict(pic);
|
||||
|
||||
pic_for_each (e, pic_reverse(pic, alist)) {
|
||||
pic_dict_set(pic, dict, pic_car(pic, e), pic_cdr(pic, e));
|
||||
pic_assert_type(pic, pic_car(pic, e), sym);
|
||||
pic_dict_set(pic, dict, pic_sym(pic_car(pic, e)), pic_cdr(pic, e));
|
||||
}
|
||||
|
||||
return pic_obj_value(dict);
|
||||
|
@ -262,7 +202,7 @@ pic_dict_dictionary_to_plist(pic_state *pic)
|
|||
pic_get_args(pic, "d", &dict);
|
||||
|
||||
for (it = xh_begin(&dict->hash); it != NULL; it = xh_next(it)) {
|
||||
pic_push(pic, xh_key(it, pic_value), plist);
|
||||
pic_push(pic, pic_sym_value(xh_key(it, pic_sym)), plist);
|
||||
pic_push(pic, xh_val(it, pic_value), plist);
|
||||
}
|
||||
|
||||
|
@ -280,7 +220,8 @@ pic_dict_plist_to_dictionary(pic_state *pic)
|
|||
dict = pic_make_dict(pic);
|
||||
|
||||
for (e = pic_reverse(pic, plist); ! pic_nil_p(e); e = pic_cddr(pic, e)) {
|
||||
pic_dict_set(pic, dict, pic_cadr(pic, e), pic_car(pic, e));
|
||||
pic_assert_type(pic, pic_cadr(pic, e), sym);
|
||||
pic_dict_set(pic, dict, pic_sym(pic_cadr(pic, e)), pic_car(pic, e));
|
||||
}
|
||||
|
||||
return pic_obj_value(dict);
|
||||
|
|
|
@ -474,7 +474,6 @@ gc_mark_object(pic_state *pic, struct pic_object *obj)
|
|||
xh_entry *it;
|
||||
|
||||
for (it = xh_begin(&dict->hash); it != NULL; it = xh_next(it)) {
|
||||
gc_mark(pic, xh_key(it, pic_value));
|
||||
gc_mark(pic, xh_val(it, pic_value));
|
||||
}
|
||||
break;
|
||||
|
|
|
@ -19,11 +19,11 @@ struct pic_dict {
|
|||
|
||||
struct pic_dict *pic_make_dict(pic_state *);
|
||||
|
||||
pic_value pic_dict_ref(pic_state *, struct pic_dict *, pic_value);
|
||||
void pic_dict_set(pic_state *, struct pic_dict *, pic_value, pic_value);
|
||||
void pic_dict_del(pic_state *, struct pic_dict *, pic_value);
|
||||
pic_value pic_dict_ref(pic_state *, struct pic_dict *, pic_sym);
|
||||
void pic_dict_set(pic_state *, struct pic_dict *, pic_sym, pic_value);
|
||||
void pic_dict_del(pic_state *, struct pic_dict *, pic_sym);
|
||||
size_t pic_dict_size(pic_state *, struct pic_dict *);
|
||||
bool pic_dict_has(pic_state *, struct pic_dict *, pic_value);
|
||||
bool pic_dict_has(pic_state *, struct pic_dict *, pic_sym);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
|
|
|
@ -334,9 +334,7 @@ write_core(struct writer_control *p, pic_value obj)
|
|||
case PIC_TT_DICT:
|
||||
xfprintf(file, "#.(dictionary");
|
||||
for (it = xh_begin(&pic_dict_ptr(obj)->hash); it != NULL; it = xh_next(it)) {
|
||||
xfprintf(file, " '");
|
||||
write_core(p, xh_key(it, pic_value));
|
||||
xfprintf(file, " '");
|
||||
xfprintf(file, " '%s ", pic_symbol_name(pic, xh_key(it, pic_sym)));
|
||||
write_core(p, xh_val(it, pic_value));
|
||||
}
|
||||
xfprintf(file, ")");
|
||||
|
|
Loading…
Reference in New Issue