diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2016-06-01 06:55:21 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2016-06-01 06:55:21 -0700 |
commit | 8a443d67ef95021529db7eb451479e79fb39b272 (patch) | |
tree | 98703e3c35ef8d20e4420a8855fee670e1ac427c /struct.c | |
parent | d6ce2f2ab5dbf62e423f632aa9d727f55ac5911b (diff) | |
download | txr-8a443d67ef95021529db7eb451479e79fb39b272.tar.gz txr-8a443d67ef95021529db7eb451479e79fb39b272.tar.bz2 txr-8a443d67ef95021529db7eb451479e79fb39b272.zip |
Print method on objects.
* struct.c (print_s): New symbol variable.
(struct_init): Initialize print_s.
(struct_inst_print): If pretty-printing, try to look
up object's print method and use it.
* txr.1: Documented pretty-printing via print method.
Diffstat (limited to 'struct.c')
-rw-r--r-- | struct.c | 11 |
1 files changed, 10 insertions, 1 deletions
@@ -76,7 +76,7 @@ struct struct_inst { val slot[1]; }; -val struct_type_s, meth_s; +val struct_type_s, meth_s, print_s; static cnum struct_id_counter; static val struct_type_hash; @@ -98,6 +98,7 @@ void struct_init(void) convert(val *, 0)); struct_type_s = intern(lit("struct-type"), user_package); meth_s = intern(lit("meth"), user_package); + print_s = intern(lit("print"), user_package); struct_type_hash = make_hash(nil, nil, nil); slot_hash = make_hash(nil, nil, t); struct_type_finalize_f = func_n1(struct_type_finalize); @@ -985,6 +986,14 @@ static void struct_inst_print(val obj, val out, val pretty) num_fast(indent_data)); val save_indent, iter, once; + if (pretty) { + loc ptr = lookup_static_slot_load(st->self, st, print_s); + if (!nullocp(ptr)) { + funcall2(deref(ptr), obj, out); + return; + } + } + put_string(lit("#S("), out); obj_print_impl(st->name, out, pretty); save_indent = inc_indent(out, one); |