Skip to content

Commit

Permalink
hashmap printing, commas as whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
thezerobit committed Apr 20, 2012
1 parent 131e271 commit 5f1a656
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
40 changes: 38 additions & 2 deletions hashmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ pointer new_hashmap_from_list(pointer list) {
return new_hm;
}

typedef struct {
pointer list;
} list_builder;

void add_to_list(pointer key, pointer val, pointer data) {
list_builder * plb = (list_builder *)data;
plb->list = new_pair(val, new_pair(key, plb->list));
}

pointer list_from_hashmap(pointer h) {
list_builder lb;
lb.list = NIL;
HashMap hm = get_hashmap(h);
PersistentHashMap_foreach(hm->phm, add_to_list, &lb);
pointer new_list = reverse(lb.list);
return new_list;
}

pointer hashmap_get(pointer m, pointer key, pointer notFound) {
HashMap hm = get_hashmap(m);
return PersistentHashMap_valAtDef(hm->phm, key, notFound);
Expand All @@ -57,8 +75,21 @@ pointer ff_hashmap_assoc(pointer l) {
}

void print_hashmap(pointer p) {
HashMap hm = get_hashmap(p);
printf("<hashmap (%d elems)>", hm->phm->count);
void * l = list_from_hashmap(p);
void * n;
printf("{");
n = l;
do {
print_thing(car(n));
printf(" ");
n = cdr(n);
print_thing(car(n));
n = cdr(n);
if(is_pair(n)) {
printf(", ");
}
} while (is_pair(n));
printf("}");
}

pointer ff_hashmap_get(pointer l) {
Expand All @@ -76,4 +107,9 @@ pointer ff_list_to_hashmap(pointer l) {
return new_hashmap_from_list(car(l));
}

pointer ff_hashmap_to_list(pointer l) {
assert(count(l) == 1);
return list_from_hashmap(car(l));
}


2 changes: 2 additions & 0 deletions hashmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ int is_hashmap(pointer p);
HashMap get_hashmap(pointer p);
pointer new_hashmap();
pointer new_hashmap_from_list(pointer list);
pointer list_from_hashmap(pointer hashmap);
pointer hashmap_get(pointer m, pointer key, pointer notFound);
pointer ff_hashmap_assoc(pointer l);
void print_hashmap(pointer p);
pointer ff_hashmap_get(pointer l);
pointer ff_list_to_hashmap(pointer l);
pointer ff_hashmap_to_list(pointer l);
/* pointer ff_hashmap_to_list(pointer l); */
/* void test_hashmap(); */

Expand Down
11 changes: 10 additions & 1 deletion lisp.c
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,15 @@ int is_symbol_char(char c) {
}

int is_whitespace(char c) {
return (c == ' ') || (c == '\t') || (c == '\n');
switch(c) {
case ' ':
case '\t':
case '\n':
case ',':
return 1;
default:
return 0;
}
}

void skip_whitespace(read_pointer * rp) {
Expand Down Expand Up @@ -1142,6 +1150,7 @@ pointer build_core_env() {
def_env(env, new_symbol("assoc"), new_func(ff_hashmap_assoc));
def_env(env, new_symbol("hash-map"), new_func(new_hashmap_from_list));
def_env(env, new_symbol("list->hash-map"), new_func(ff_list_to_hashmap));
def_env(env, new_symbol("hash-map->list"), new_func(ff_hashmap_to_list));
/* predicates */
def_env(env, new_symbol("keyword?"), new_func(ff_is_keyword));

Expand Down

0 comments on commit 5f1a656

Please sign in to comment.