summaryrefslogtreecommitdiffstats
path: root/hash.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2012-03-14 06:53:16 -0700
committerKaz Kylheku <kaz@kylheku.com>2012-03-14 06:53:16 -0700
commit96f072cfdb5d1eac3e32dbdb15704b0a32258a37 (patch)
treeb25990f165c0485db4f990a4e721191b4b8380d5 /hash.c
parent7f0f22c4e455f457d37ddf542b36c49db20d16af (diff)
downloadtxr-96f072cfdb5d1eac3e32dbdb15704b0a32258a37.tar.gz
txr-96f072cfdb5d1eac3e32dbdb15704b0a32258a37.tar.bz2
txr-96f072cfdb5d1eac3e32dbdb15704b0a32258a37.zip
Implementing #H((...) ...) read/print syntax for hash tables.
* hash.c (print_key_val, hash_print_op): New static functions. (hash_ops): hash_print_op wired in in place of cobj_print_op. * parser.l (HASH_H): New token recognized. * parser.y (HASH_H): New terminal symbol. (hash): New nonterminal symbol. (expr): Acquires hash as a constituent. (hash_from_notation): New static function. * txr.1: Hash syntax described. * txr.vim: Updated.
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c41
1 files changed, 40 insertions, 1 deletions
diff --git a/hash.c b/hash.c
index f8bba6bd..bc086ae9 100644
--- a/hash.c
+++ b/hash.c
@@ -36,6 +36,7 @@
#include "lib.h"
#include "gc.h"
#include "unwind.h"
+#include "stream.h"
#include "hash.h"
typedef enum hash_flags {
@@ -184,6 +185,44 @@ cnum cobj_hash_op(val obj)
abort();
}
+static val print_key_val(val out, val key, val value)
+{
+ format(out, lit(" (~s ~s)"), key, value, nao);
+ return nil;
+}
+
+static void hash_print_op(val hash, val out)
+{
+ struct hash *h = (struct hash *) hash->co.handle;
+ int need_space = 0;
+
+ put_string(lit("#H(("), out);
+ if (h->hash_fun == equal_hash) {
+ obj_print(equal_based_k, out);
+ need_space = 1;
+ }
+ if (h->flags != hash_weak_none) {
+ if (need_space)
+ put_string(lit(" "), out);
+ switch (h->flags) {
+ case hash_weak_both:
+ obj_print(weak_keys_k, out);
+ /* fallthrough */
+ case hash_weak_vals:
+ obj_print(weak_vals_k, out);
+ break;
+ case hash_weak_keys:
+ obj_print(weak_keys_k, out);
+ break;
+ default:
+ break;
+ }
+ }
+ put_string(lit(")"), out);
+ maphash(curry_123_23(func_n3(print_key_val), out), hash);
+ put_string(lit(")"), out);
+}
+
static void hash_mark(val hash)
{
struct hash *h = (struct hash *) hash->co.handle;
@@ -236,7 +275,7 @@ static void hash_mark(val hash)
static struct cobj_ops hash_ops = {
cobj_equal_op,
- cobj_print_op,
+ hash_print_op,
cobj_destroy_free_op,
hash_mark,
cobj_hash_op