Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #313 from dcurrie/master
Browse files Browse the repository at this point in the history
Better error reporting for uninitialized global.
  • Loading branch information
nyuichi committed Dec 27, 2015
2 parents 5e6ffec + 17d7312 commit 7524633
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions extlib/benz/include/picrin/reg.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pic_value pic_reg_ref(pic_state *, struct pic_reg *, void *);
void pic_reg_set(pic_state *, struct pic_reg *, void *, pic_value);
void pic_reg_del(pic_state *, struct pic_reg *, void *);
bool pic_reg_has(pic_state *, struct pic_reg *, void *);
void *pic_reg_rev_ref(pic_state *, struct pic_reg *, pic_value);

#if defined(__cplusplus)
}
Expand Down
16 changes: 16 additions & 0 deletions extlib/benz/reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ pic_reg_ref(pic_state *pic, struct pic_reg *reg, void *key)
return kh_val(h, it);
}

void *
pic_reg_rev_ref(pic_state *pic, struct pic_reg *reg, pic_value val)
{
khash_t(reg) *h = &reg->hash;

if (h->n_buckets) {
khint_t i = 0;
while ((i < h->n_buckets) && (ac_iseither(h->flags, i) || !pic_eq_p(h->vals[i], val))) {
i += 1;
}
if (i < h->n_buckets) return kh_key(h, i);
}
pic_errorf(pic, "key not found for an element: ~s", val);
return NULL;
}

void
pic_reg_set(pic_state PIC_UNUSED(*pic), struct pic_reg *reg, void *key, pic_value val)
{
Expand Down
4 changes: 2 additions & 2 deletions extlib/benz/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,9 @@ vm_gref(pic_state *pic, struct pic_box *slot, pic_sym *uid)
{
if (pic_invalid_p(slot->value)) {
if (uid == NULL) {
uid = pic_intern(pic, "unknown"); /* FIXME */
uid = pic_reg_rev_ref(pic, pic->globals, pic_obj_value(slot));
}
pic_errorf(pic, "uninitialized global variable: ~a", pic_obj_value(uid));
pic_errorf(pic, "uninitialized global variable: %s", pic_symbol_name(pic, uid));
}
return slot->value;
}
Expand Down

0 comments on commit 7524633

Please sign in to comment.