diff options
-rw-r--r-- | eval.c | 8 | ||||
-rw-r--r-- | hash.c | 2 | ||||
-rw-r--r-- | lib.c | 51 | ||||
-rw-r--r-- | lib.h | 5 | ||||
-rw-r--r-- | match.c | 2 | ||||
-rw-r--r-- | stream.c | 5 | ||||
-rw-r--r-- | struct.c | 8 | ||||
-rw-r--r-- | tests/012/oop.tl | 2 | ||||
-rw-r--r-- | txr.1 | 86 |
9 files changed, 96 insertions, 73 deletions
@@ -4615,14 +4615,14 @@ static val mapf(val fun, struct args *funlist) val prinl(val obj, val stream) { - val ret = obj_print(obj, stream); + val ret = obj_print(obj, default_arg(stream, std_output), nil); put_char(chr('\n'), stream); return ret; } val pprinl(val obj, val stream) { - val ret = obj_pprint(obj, stream); + val ret = obj_print(obj, default_arg(stream, std_output), t); put_char(chr('\n'), stream); return ret; } @@ -5158,8 +5158,8 @@ void eval_init(void) reg_fun(intern(lit("tf"), user_package), func_n0v(tf)); reg_fun(intern(lit("nilf"), user_package), func_n0v(nilf)); - reg_fun(intern(lit("print"), user_package), func_n2o(obj_print, 1)); - reg_fun(intern(lit("pprint"), user_package), func_n2o(obj_pprint, 1)); + reg_fun(intern(lit("print"), user_package), func_n3o(print, 1)); + reg_fun(intern(lit("pprint"), user_package), func_n2o(pprint, 1)); reg_fun(intern(lit("tostring"), user_package), func_n1(tostring)); reg_fun(intern(lit("tostringp"), user_package), func_n1(tostringp)); reg_fun(intern(lit("prinl"), user_package), func_n2o(prinl, 1)); @@ -389,7 +389,7 @@ static void hash_print_op(val hash, val out, val pretty, struct strm_ctx *ctx) put_char(chr('('), out); if (h->hash_fun == equal_hash) { - obj_print(equal_based_k, out); + obj_print_impl(equal_based_k, out, pretty, ctx); need_space = 1; } if (h->flags != hash_weak_none) { @@ -9425,10 +9425,9 @@ tail: } } -val obj_print(val obj, val stream) +val obj_print(val obj, val out, val pretty) { val ret = nil; - val out = default_arg(stream, std_output); val save_mode = get_indent_mode(out); val save_indent = get_indent(out); struct strm_ctx *ctx_orig = get_ctx(out); @@ -9448,7 +9447,7 @@ val obj_print(val obj, val stream) } } - ret = obj_print_impl(obj, out, nil, ctx); + ret = obj_print_impl(obj, out, pretty, ctx); uw_unwind { set_indent_mode(out, save_mode); @@ -9462,54 +9461,28 @@ val obj_print(val obj, val stream) return ret; } -val obj_pprint(val obj, val stream) +val print(val obj, val stream, val pretty) { - volatile val ret = nil; - val out = default_arg(stream, std_output); - val save_mode = get_indent_mode(out); - val save_indent = get_indent(out); - struct strm_ctx *ctx_orig = get_ctx(out); - struct strm_ctx *volatile ctx = ctx_orig, ctx_struct; - - uw_simple_catch_begin; - - if (ctx) { - populate_obj_hash(obj, ctx); - } else { - if (cdr(lookup_var(nil, print_circle_s))) { - ctx = &ctx_struct; - ctx->obj_hash = make_hash(nil, nil, nil); - ctx->counter = zero; - get_set_ctx(out, ctx); - populate_obj_hash(obj, ctx); - } - } - - ret = obj_print_impl(obj, out, t, ctx); - - uw_unwind { - set_indent_mode(out, save_mode); - set_indent(out, save_indent); - if (ctx != ctx_orig) - get_set_ctx(out, ctx_orig); - } - - uw_catch_end; + return obj_print(obj, default_arg(stream, std_output), + default_bool_arg(pretty)); +} - return ret; +val pprint(val obj, val stream) +{ + return obj_print(obj, default_arg(stream, std_output), t); } val tostring(val obj) { val ss = make_string_output_stream(); - obj_print(obj, ss); + obj_print(obj, ss, nil); return get_string_from_stream(ss); } val tostringp(val obj) { val ss = make_string_output_stream(); - obj_pprint(obj, ss); + pprint(obj, ss); return get_string_from_stream(ss); } @@ -10001,7 +9974,7 @@ int compat_fixup(int compat_ver) void dump(val obj, val out) { - obj_print(obj, out); + obj_print(obj, out, nil); put_char(chr('\n'), out); } @@ -978,8 +978,9 @@ val set_to(val range, val to); val env(void); void out_str_char(wchar_t ch, val out, int *semi_flag); val obj_print_impl(val obj, val out, val pretty, struct strm_ctx *); -val obj_print(val obj, val stream); -val obj_pprint(val obj, val stream); +val obj_print(val obj, val stream, val pretty); +val print(val obj, val stream, val pretty); +val pprint(val obj, val stream); val tostring(val obj); val tostringp(val obj); val display_width(val obj); @@ -166,7 +166,7 @@ static void dump_var(val var, char *pfx1, size_t len1, val ss = make_string_output_stream(); val str; - obj_pprint(value, ss); + pprint(value, ss); str = get_string_from_stream(ss); put_string(var, std_output); @@ -3218,10 +3218,7 @@ val formatv(val stream_in, val fmtstr, struct args *al) continue; } } - if (ch == 'a') - obj_pprint(obj, stream); - else - obj_print(obj, stream); + obj_print(obj, stream, if2(ch == 'a', t)); continue; case 'p': { @@ -1251,11 +1251,15 @@ static void struct_inst_print(val obj, val out, val pretty, val save_mode = test_set_indent_mode(out, num_fast(indent_off), num_fast(indent_data)); val save_indent, iter, once; + int compat = opt_compat && opt_compat <= 154; - if (pretty) { + if (!compat || pretty) { loc ptr = lookup_static_slot_load(st->self, st, print_s); if (!nullocp(ptr)) { - funcall2(deref(ptr), obj, out); + if (compat) + funcall2(deref(ptr), obj, out); + else + funcall3(deref(ptr), obj, out, pretty); return; } } diff --git a/tests/012/oop.tl b/tests/012/oop.tl index 34808af2..51dadbf3 100644 --- a/tests/012/oop.tl +++ b/tests/012/oop.tl @@ -2,7 +2,7 @@ (defstruct animal nil (:function whoami () "n/a") - (:method print (self stream) (put-string self.[whoami] stream))) + (:method print (self stream : pretty-p) (put-string self.[whoami] stream))) (defstruct dog animal (:function whoami () "dog")) @@ -22104,7 +22104,7 @@ hash table, searches for those keys will not work reliably. .coNP Method @ print .synb -.mets << object .(print << stream ) +.mets << object .(print < stream << pretty-p ) .syne .desc If a method named by the symbol @@ -22113,10 +22113,23 @@ is defined for a structure type, then it is used for pretty-printing instances of that type. The -.code stream +.meta stream argument specifies the output stream to which the printed representation is to be written. +The +.meta pretty-p +argument is a Boolean flag indicating whether pretty-printing +is requested. Its value may simply be passed to recursive calls to +.codn print , +or used to select between +.code ~s +or +.code ~a +formatting if +.code format +is used. + The value returned by the .code print method is ignored. @@ -35970,7 +35983,7 @@ and .coNP Functions @, print @, pprint @, prinl @, pprinl @ tostring and @ tostringp .synb -.mets (print < obj <> [ stream ]) +.mets (print < obj >> [ stream <> [ pretty-p ]]) .mets (pprint < obj <> [ stream ]) .mets (prinl < obj <> [ stream ]) .mets (pprinl < obj <> [ stream ]) @@ -35986,33 +35999,59 @@ functions render a printed character representation of the .meta obj argument into .metn stream . -If a stream argument is not supplied, then + +If the +.meta stream +argument is not supplied, then the destination is the stream currently stored in the .code *stdout* -variable. The +variable. + +If Boolean argument +.meta pretty-p +is not supplied or is explicitly specified as +.codn nil , +then the .code print function renders in a way which strives for read-print consistency: an object is printed in a notation which is recognized as a similar object of the same kind when it appears in \*(TX source code. +If +.meta pretty-p +is true, then +.code print +performs does not strive for read-print consistency. For instance, it prints a +string object simply by dumping its characters, rather than by adding the +surrounding quotes and rendering escape syntax for special characters. + The -.code pprint -function ("pretty print") does not strive for read-print consistency. -For instance it prints a string object simply by dumping its characters, rather -than by adding the surrounding quotes and rendering escape syntax for -special characters. Both functions return +.code print +function returns .metn obj . The +.code pprint +("pretty print") function is equivalent to +.codn print , +with the +.meta pretty-p +argument hard-coded true. + +The .code prinl -and -.code pprinl -functions are like +function ("print and new line") behaves like a call to .code print -and -.codn pprint , -except that they issue a newline character after printing the object. -These functions also return -.metn obj . +with +.meta pretty-p +defaulting to +.codn nil , +followed by issuing a newline characters to the stream. + +The +.code pprinl +function ("pretty print and new line") behaves like +.code pprint +followed by issuing a newline to the stream. The .code tostring @@ -47348,7 +47387,16 @@ and then the slot values are assigned. This means that the values specified in the literal override any manipulations of those slots by the type's user-defined .code :postinit -handlers. +handlers. Also, after 154, +.code print +methods are expected to take three arguments and are invoked for both +pretty printing and regular machine-readable printing. Until 154, a struct's +.code print +methods was called only when that struct was being pretty-printed, and +only with two arguments; ordinary printing side-stepped the method and rendered +the standard +.code #S +syntax featuring all instance slots. .IP 151 After version 151, changes were implemented to the way static slots work in \*(TL structs. Selecting compatibility with 151 restores most of the behaviors. |