diff --git a/extlib/benz/dict.c b/extlib/benz/dict.c index 656547d1..88c67e4c 100644 --- a/extlib/benz/dict.c +++ b/extlib/benz/dict.c @@ -7,21 +7,20 @@ KHASH_DEFINE(dict, pic_sym *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal) -struct pic_dict * +pic_value pic_make_dict(pic_state *pic) { struct pic_dict *dict; dict = (struct pic_dict *)pic_obj_alloc(pic, sizeof(struct pic_dict), PIC_TYPE_DICT); kh_init(dict, &dict->hash); - - return dict; + return pic_obj_value(dict); } pic_value -pic_dict_ref(pic_state *pic, struct pic_dict *dict, pic_sym *key) +pic_dict_ref(pic_state *pic, pic_value dict, pic_sym *key) { - khash_t(dict) *h = &dict->hash; + khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash; khiter_t it; it = kh_get(dict, h, key); @@ -32,9 +31,9 @@ pic_dict_ref(pic_state *pic, struct pic_dict *dict, pic_sym *key) } void -pic_dict_set(pic_state PIC_UNUSED(*pic), struct pic_dict *dict, pic_sym *key, pic_value val) +pic_dict_set(pic_state *pic, pic_value dict, pic_sym *key, pic_value val) { - khash_t(dict) *h = &dict->hash; + khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash; int ret; khiter_t it; @@ -43,21 +42,23 @@ pic_dict_set(pic_state PIC_UNUSED(*pic), struct pic_dict *dict, pic_sym *key, pi } int -pic_dict_size(pic_state PIC_UNUSED(*pic), struct pic_dict *dict) +pic_dict_size(pic_state PIC_UNUSED(*pic), pic_value dict) { - return kh_size(&dict->hash); + return kh_size(&pic_dict_ptr(pic, dict)->hash); } bool -pic_dict_has(pic_state PIC_UNUSED(*pic), struct pic_dict *dict, pic_sym *key) +pic_dict_has(pic_state *pic, pic_value dict, pic_sym *key) { - return kh_get(dict, &dict->hash, key) != kh_end(&dict->hash); + khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash; + + return kh_get(dict, h, key) != kh_end(h); } void -pic_dict_del(pic_state *pic, struct pic_dict *dict, pic_sym *key) +pic_dict_del(pic_state *pic, pic_value dict, pic_sym *key) { - khash_t(dict) *h = &dict->hash; + khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash; khiter_t it; it = kh_get(dict, h, key); @@ -68,9 +69,9 @@ pic_dict_del(pic_state *pic, struct pic_dict *dict, pic_sym *key) } bool -pic_dict_next(pic_state PIC_UNUSED(*pic), struct pic_dict *dict, int *iter, pic_sym **key, pic_value *val) +pic_dict_next(pic_state PIC_UNUSED(*pic), pic_value dict, int *iter, pic_sym **key, pic_value *val) { - khash_t(dict) *h = &dict->hash; + khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash; int it = *iter; for (it = *iter; it != kh_end(h); ++it) { @@ -87,20 +88,15 @@ pic_dict_next(pic_state PIC_UNUSED(*pic), struct pic_dict *dict, int *iter, pic_ static pic_value pic_dict_make_dictionary(pic_state *pic) { - struct pic_dict *dict; - pic_get_args(pic, ""); - dict = pic_make_dict(pic); - - return pic_obj_value(dict); + return pic_make_dict(pic); } static pic_value pic_dict_dictionary(pic_state *pic) { - struct pic_dict *dict; - pic_value *argv; + pic_value dict, *argv; int argc, i; pic_get_args(pic, "*", &argc, &argv); @@ -112,7 +108,7 @@ pic_dict_dictionary(pic_state *pic) pic_dict_set(pic, dict, pic_sym_ptr(argv[i]), argv[i+1]); } - return pic_obj_value(dict); + return dict; } static pic_value @@ -128,7 +124,7 @@ pic_dict_dictionary_p(pic_state *pic) static pic_value pic_dict_dictionary_ref(pic_state *pic) { - struct pic_dict *dict; + pic_value dict; pic_sym *key; pic_get_args(pic, "dm", &dict, &key); @@ -142,7 +138,7 @@ pic_dict_dictionary_ref(pic_state *pic) static pic_value pic_dict_dictionary_set(pic_state *pic) { - struct pic_dict *dict; + pic_value dict; pic_sym *key; pic_value val; @@ -162,7 +158,7 @@ pic_dict_dictionary_set(pic_state *pic) static pic_value pic_dict_dictionary_size(pic_state *pic) { - struct pic_dict *dict; + pic_value dict; pic_get_args(pic, "d", &dict); @@ -173,21 +169,15 @@ static pic_value pic_dict_dictionary_map(pic_state *pic) { struct pic_proc *proc; - struct pic_dict *dict; - khiter_t it; - khash_t(dict) *kh; - pic_value ret = pic_nil_value(pic); + pic_value dict, ret = pic_nil_value(pic); + pic_sym *key; + int it = 0; pic_get_args(pic, "ld", &proc, &dict); - kh = &dict->hash; - - for (it = kh_begin(kh); it != kh_end(kh); ++it) { - if (kh_exist(kh, it)) { - pic_push(pic, pic_call(pic, proc, 1, pic_obj_value(kh_key(kh, it))), ret); - } + while (pic_dict_next(pic, dict, &it, &key, NULL)) { + pic_push(pic, pic_call(pic, proc, 1, pic_obj_value(key)), ret); } - return pic_reverse(pic, ret); } @@ -195,18 +185,14 @@ static pic_value pic_dict_dictionary_for_each(pic_state *pic) { struct pic_proc *proc; - struct pic_dict *dict; - khiter_t it; - khash_t(dict) *kh; + pic_value dict; + pic_sym *key; + int it; pic_get_args(pic, "ld", &proc, &dict); - kh = &dict->hash; - - for (it = kh_begin(kh); it != kh_end(kh); ++it) { - if (kh_exist(kh, it)) { - pic_call(pic, proc, 1, pic_obj_value(kh_key(kh, it))); - } + while (pic_dict_next(pic, dict, &it, &key, NULL)) { + pic_call(pic, proc, 1, pic_obj_value(key)); } return pic_undef_value(pic); @@ -215,8 +201,7 @@ pic_dict_dictionary_for_each(pic_state *pic) static pic_value pic_dict_dictionary_to_alist(pic_state *pic) { - struct pic_dict *dict; - pic_value val, alist = pic_nil_value(pic); + pic_value dict, val, alist = pic_nil_value(pic); pic_sym *sym; int it = 0; @@ -232,8 +217,7 @@ pic_dict_dictionary_to_alist(pic_state *pic) static pic_value pic_dict_alist_to_dictionary(pic_state *pic) { - struct pic_dict *dict; - pic_value alist, e, it; + pic_value dict, alist, e, it; pic_get_args(pic, "o", &alist); @@ -244,14 +228,13 @@ pic_dict_alist_to_dictionary(pic_state *pic) pic_dict_set(pic, dict, pic_sym_ptr(pic_car(pic, e)), pic_cdr(pic, e)); } - return pic_obj_value(dict); + return dict; } static pic_value pic_dict_dictionary_to_plist(pic_state *pic) { - struct pic_dict *dict; - pic_value val, plist = pic_nil_value(pic); + pic_value dict, val, plist = pic_nil_value(pic); pic_sym *sym; int it = 0; @@ -268,8 +251,7 @@ pic_dict_dictionary_to_plist(pic_state *pic) static pic_value pic_dict_plist_to_dictionary(pic_state *pic) { - struct pic_dict *dict; - pic_value plist, e; + pic_value dict, plist, e; pic_get_args(pic, "o", &plist); @@ -280,7 +262,7 @@ pic_dict_plist_to_dictionary(pic_state *pic) pic_dict_set(pic, dict, pic_sym_ptr(pic_cadr(pic, e)), pic_car(pic, e)); } - return pic_obj_value(dict); + return dict; } void diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index b6958e31..2a7a29a2 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -363,7 +363,7 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) pic_value val; int it = 0; - while (pic_dict_next(pic, &obj->u.dict, &it, &sym, &val)) { + while (pic_dict_next(pic, pic_obj_value(&obj->u.dict), &it, &sym, &val)) { gc_mark_object(pic, (struct pic_object *)sym); gc_mark(pic, val); } diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 94600879..b2421e3b 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -241,13 +241,13 @@ void pic_vec_set(pic_state *, pic_vec *, int, pic_value); int pic_vec_len(pic_state *, pic_vec *); /* dictionary */ -struct pic_dict *pic_make_dict(pic_state *); -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 *); -bool pic_dict_has(pic_state *, struct pic_dict *, pic_sym *); -int pic_dict_size(pic_state *, struct pic_dict *); -bool pic_dict_next(pic_state *, struct pic_dict *, int *iter, pic_sym **key, pic_value *val); +pic_value pic_make_dict(pic_state *); +pic_value pic_dict_ref(pic_state *, pic_value dict, pic_sym *); +void pic_dict_set(pic_state *, pic_value dict, pic_sym *, pic_value); +void pic_dict_del(pic_state *, pic_value dict, pic_sym *); +bool pic_dict_has(pic_state *, pic_value dict, pic_sym *); +int pic_dict_size(pic_state *, pic_value dict); +bool pic_dict_next(pic_state *, pic_value dict, int *iter, pic_sym **key, pic_value *val); /* ephemeron */ struct pic_weak *pic_make_weak(pic_state *); diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index 2335cd59..4f5ddfe9 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -9,11 +9,9 @@ extern "C" { #endif - -struct pic_object *pic_obj_alloc(pic_state *, size_t, int type); - - -/* symbol & identifier */ +KHASH_DECLARE(env, pic_id *, pic_sym *) +KHASH_DECLARE(dict, pic_sym *, pic_value) +KHASH_DECLARE(weak, void *, pic_value) struct pic_id { union { @@ -29,21 +27,12 @@ struct pic_id { } u; }; -#define pic_sym_ptr(v) ((pic_sym *)pic_obj_ptr(v)) - -#define pic_id_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ID || pic_type(pic, v) == PIC_TYPE_SYMBOL) -#define pic_id_ptr(v) ((pic_id *)pic_obj_ptr(v)) - -pic_id *pic_make_identifier(pic_state *, pic_id *, struct pic_env *); - -pic_sym *pic_add_identifier(pic_state *, pic_id *, struct pic_env *); -pic_sym *pic_put_identifier(pic_state *, pic_id *, pic_sym *, struct pic_env *); -pic_sym *pic_find_identifier(pic_state *, pic_id *, struct pic_env *); - -struct pic_string *pic_id_name(pic_state *, pic_id *); - - -/* pair */ +struct pic_env { + PIC_OBJECT_HEADER + khash_t(env) map; + struct pic_env *up; + struct pic_string *lib; +}; struct pic_pair { PIC_OBJECT_HEADER @@ -51,69 +40,33 @@ struct pic_pair { pic_value cdr; }; -#define pic_pair_ptr(o) ((struct pic_pair *)pic_obj_ptr(o)) - - -/* blob */ - struct pic_blob { PIC_OBJECT_HEADER unsigned char *data; int len; }; -#define pic_blob_ptr(v) ((struct pic_blob *)pic_obj_ptr(v)) - -/* string */ - struct pic_string { PIC_OBJECT_HEADER struct pic_rope *rope; }; -void pic_rope_incref(pic_state *, struct pic_rope *); -void pic_rope_decref(pic_state *, struct pic_rope *); - -#define pic_str_ptr(o) ((struct pic_string *)pic_obj_ptr(o)) - - -/* vector */ - -struct pic_vector { - PIC_OBJECT_HEADER - pic_value *data; - int len; -}; - -#define pic_vec_ptr(o) ((struct pic_vector *)pic_obj_ptr(o)) - - -/* dictionary */ - -KHASH_DECLARE(dict, pic_sym *, pic_value) - struct pic_dict { PIC_OBJECT_HEADER khash_t(dict) hash; }; -#define pic_dict_ptr(v) ((struct pic_dict *)pic_obj_ptr(v)) - - -/* weak */ - -KHASH_DECLARE(weak, void *, pic_value) - struct pic_weak { PIC_OBJECT_HEADER khash_t(weak) hash; struct pic_weak *prev; /* for GC */ }; -#define pic_weak_ptr(v) ((struct pic_weak *)pic_obj_ptr(v)) - - -/* data */ +struct pic_vector { + PIC_OBJECT_HEADER + pic_value *data; + int len; +}; struct pic_data { PIC_OBJECT_HEADER @@ -121,11 +74,6 @@ struct pic_data { void *data; }; -#define pic_data_ptr(o) ((struct pic_data *)pic_obj_ptr(o)) - - -/* context */ - struct pic_context { PIC_OBJECT_HEADER pic_value *regs; @@ -134,11 +82,6 @@ struct pic_context { pic_value storage[1]; }; -#define pic_context_ptr(o) ((struct pic_context *)pic_obj_ptr(o)) - - -/* procedure */ - struct pic_proc { PIC_OBJECT_HEADER enum { @@ -158,31 +101,12 @@ struct pic_proc { pic_value locals[1]; }; -#define pic_proc_ptr(o) ((struct pic_proc *)pic_obj_ptr(o)) - -#define pic_proc_func_p(proc) ((proc)->tag == PIC_PROC_TAG_FUNC) -#define pic_proc_irep_p(proc) ((proc)->tag == PIC_PROC_TAG_IREP) - -struct pic_proc *pic_make_proc(pic_state *, pic_func_t, int, pic_value *); -struct pic_proc *pic_make_proc_irep(pic_state *, struct pic_irep *, struct pic_context *); - - -/* record */ - struct pic_record { PIC_OBJECT_HEADER pic_value type; pic_value datum; }; -#define pic_rec_p(pic, v) (pic_type(pic, v) == PIC_TYPE_RECORD) -#define pic_rec_ptr(v) ((struct pic_record *)pic_obj_ptr(v)) - -struct pic_record *pic_make_rec(pic_state *, pic_value, pic_value); - - -/* error */ - struct pic_error { PIC_OBJECT_HEADER pic_sym *type; @@ -191,38 +115,52 @@ struct pic_error { struct pic_string *stack; }; -#define pic_error_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ERROR) -#define pic_error_ptr(v) ((struct pic_error *)pic_obj_ptr(v)) - -struct pic_error *pic_make_error(pic_state *, const char *, const char *, pic_value); - - -/* port */ - struct pic_port { PIC_OBJECT_HEADER xFILE *file; }; +#define pic_dict_ptr(pic, v) ((struct pic_dict *)pic_obj_ptr(v)) +#define pic_sym_ptr(v) ((pic_sym *)pic_obj_ptr(v)) +#define pic_id_ptr(v) ((pic_id *)pic_obj_ptr(v)) +#define pic_pair_ptr(o) ((struct pic_pair *)pic_obj_ptr(o)) +#define pic_blob_ptr(v) ((struct pic_blob *)pic_obj_ptr(v)) +#define pic_str_ptr(o) ((struct pic_string *)pic_obj_ptr(o)) +#define pic_vec_ptr(o) ((struct pic_vector *)pic_obj_ptr(o)) +#define pic_weak_ptr(v) ((struct pic_weak *)pic_obj_ptr(v)) +#define pic_data_ptr(o) ((struct pic_data *)pic_obj_ptr(o)) +#define pic_context_ptr(o) ((struct pic_context *)pic_obj_ptr(o)) +#define pic_proc_ptr(o) ((struct pic_proc *)pic_obj_ptr(o)) +#define pic_rec_ptr(v) ((struct pic_record *)pic_obj_ptr(v)) +#define pic_error_ptr(v) ((struct pic_error *)pic_obj_ptr(v)) #define pic_port_ptr(v) ((struct pic_port *)pic_obj_ptr(v)) - - -/* environment */ - -KHASH_DECLARE(env, pic_id *, pic_sym *) - -struct pic_env { - PIC_OBJECT_HEADER - khash_t(env) map; - struct pic_env *up; - struct pic_string *lib; -}; - -#define pic_env_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ENV) #define pic_env_ptr(v) ((struct pic_env *)pic_obj_ptr(v)) +#define pic_env_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ENV) +#define pic_error_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ERROR) +#define pic_rec_p(pic, v) (pic_type(pic, v) == PIC_TYPE_RECORD) +#define pic_id_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ID || pic_type(pic, v) == PIC_TYPE_SYMBOL) + +struct pic_object *pic_obj_alloc(pic_state *, size_t, int type); + +pic_id *pic_make_identifier(pic_state *, pic_id *, struct pic_env *); +struct pic_proc *pic_make_proc(pic_state *, pic_func_t, int, pic_value *); +struct pic_proc *pic_make_proc_irep(pic_state *, struct pic_irep *, struct pic_context *); +struct pic_record *pic_make_rec(pic_state *, pic_value, pic_value); +struct pic_error *pic_make_error(pic_state *, const char *, const char *, pic_value); struct pic_env *pic_make_env(pic_state *, struct pic_env *); +pic_sym *pic_add_identifier(pic_state *, pic_id *, struct pic_env *); +pic_sym *pic_put_identifier(pic_state *, pic_id *, pic_sym *, struct pic_env *); +pic_sym *pic_find_identifier(pic_state *, pic_id *, struct pic_env *); +struct pic_string *pic_id_name(pic_state *, pic_id *); + +void pic_rope_incref(pic_state *, struct pic_rope *); +void pic_rope_decref(pic_state *, struct pic_rope *); + +#define pic_proc_func_p(proc) ((proc)->tag == PIC_PROC_TAG_FUNC) +#define pic_proc_irep_p(proc) ((proc)->tag == PIC_PROC_TAG_IREP) + #if defined(__cplusplus) } diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index bf7a7246..b97c49f0 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -57,7 +57,7 @@ pic_make_library(pic_state *pic, const char *lib) const char *old_lib; struct pic_string *name; struct pic_env *env; - struct pic_dict *exports; + pic_value exports; khiter_t it; int ret; @@ -76,7 +76,7 @@ pic_make_library(pic_state *pic, const char *lib) kh_val(h, it).name = name; kh_val(h, it).env = env; - kh_val(h, it).exports = exports; + kh_val(h, it).exports = pic_dict_ptr(pic, exports); if (pic->lib) { pic->lib = get_library(pic, old_lib); /* ltable might be rehashed */ @@ -117,7 +117,7 @@ pic_import(pic_state *pic, const char *lib) libp = get_library(pic, lib); - while (pic_dict_next(pic, libp->exports, &it, &name, &val)) { + while (pic_dict_next(pic, pic_obj_value(libp->exports), &it, &name, &val)) { realname = pic_sym_ptr(val); if ((uid = pic_find_identifier(pic, (pic_id *)realname, libp->env)) == NULL) { @@ -130,7 +130,7 @@ pic_import(pic_state *pic, const char *lib) void pic_export(pic_state *pic, pic_sym *name) { - pic_dict_set(pic, pic->lib->exports, name, pic_obj_value(name)); + pic_dict_set(pic, pic_obj_value(pic->lib->exports), name, pic_obj_value(name)); } static pic_value @@ -188,10 +188,10 @@ pic_lib_library_import(pic_state *pic) libp = get_library(pic, lib); - if (! pic_dict_has(pic, libp->exports, name)) { + if (! pic_dict_has(pic, pic_obj_value(libp->exports), name)) { pic_errorf(pic, "attempted to import undefined variable '~s'", pic_obj_value(name)); } else { - realname = pic_sym_ptr(pic_dict_ref(pic, libp->exports, name)); + realname = pic_sym_ptr(pic_dict_ref(pic, pic_obj_value(libp->exports), name)); } if ((uid = pic_find_identifier(pic, (pic_id *)realname, libp->env)) == NULL) { @@ -214,7 +214,7 @@ pic_lib_library_export(pic_state *pic) alias = name; } - pic_dict_set(pic, pic->lib->exports, alias, pic_obj_value(name)); + pic_dict_set(pic, pic_obj_value(pic->lib->exports), alias, pic_obj_value(name)); return pic_undef_value(pic); } @@ -232,7 +232,7 @@ pic_lib_library_exports(pic_state *pic) libp = get_library(pic, lib); - while (pic_dict_next(pic, libp->exports, &it, &sym, NULL)) { + while (pic_dict_next(pic, pic_obj_value(libp->exports), &it, &sym, NULL)) { pic_push(pic, pic_obj_value(sym), exports); } diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 9ce9dc70..b3a8970e 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -26,7 +26,7 @@ * b struct pic_blob ** bytevector object * l struct pic_proc ** lambda object * p struct pic_port ** port object - * d struct pic_dict ** dictionary object + * d pic_value * dictionary object * e struct pic_error ** error object * r struct pic_record ** record object * @@ -153,10 +153,13 @@ pic_get_args(pic_state *pic, const char *format, ...) PTR_CASE('b', blob, struct pic_blob *) PTR_CASE('l', proc, struct pic_proc *) PTR_CASE('p', port, struct pic_port *) - PTR_CASE('d', dict, struct pic_dict *) PTR_CASE('e', error, struct pic_error *) PTR_CASE('r', rec, struct pic_record *) +#define OBJ_CASE(c, type) VAL_CASE(c, type, pic_value, v) + + OBJ_CASE('d', dict) + default: pic_errorf(pic, "pic_get_args: invalid argument specifier '%c' given", c); } diff --git a/extlib/benz/write.c b/extlib/benz/write.c index e23bcedc..c80b7fc3 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -238,7 +238,7 @@ write_vec(struct writer_control *p, pic_vec *vec) } static void -write_dict(struct writer_control *p, struct pic_dict *dict) +write_dict(struct writer_control *p, pic_value dict) { pic_state *pic = p->pic; xFILE *file = p->file; @@ -318,7 +318,7 @@ write_core(struct writer_control *p, pic_value obj) write_vec(p, pic_vec_ptr(obj)); break; case PIC_TYPE_DICT: - write_dict(p, pic_dict_ptr(obj)); + write_dict(p, obj); break; default: xfprintf(pic, file, "#<%s %p>", pic_typename(pic, pic_type(pic, obj)), pic_obj_ptr(obj)); @@ -369,7 +369,7 @@ traverse(struct writer_control *p, pic_value obj) /* dictionary */ int it = 0; pic_value val; - while (pic_dict_next(pic, pic_dict_ptr(obj), &it, NULL, &val)) { + while (pic_dict_next(pic, obj, &it, NULL, &val)) { traverse(p, val); } }