Turn pr.opts.readably into pr.opts.display

This commit is contained in:
Lassi Kortela 2019-08-25 17:27:16 +03:00
parent 726b7cad29
commit fe4b63b9a3
1 changed files with 19 additions and 17 deletions

View File

@ -1,8 +1,8 @@
extern void *memrchr(const void *s, int c, size_t n); extern void *memrchr(const void *s, int c, size_t n);
struct printer_options { struct printer_options {
// *print-readably* -- use `write` repr instead of `display` repr
int readably; int display; // Use `display` repr instead of `write` repr
// *print-pretty* -- indent instead of printing everything on one // *print-pretty* -- indent instead of printing everything on one
// *long line // *long line
@ -414,7 +414,7 @@ void fl_print_child(struct ios *f, value_t v)
break; break;
case TAG_SYM: case TAG_SYM:
name = symbol_name(v); name = symbol_name(v);
if (!pr.opts.readably) if (pr.opts.display)
outs(name, f); outs(name, f);
else if (ismanaged(v)) { else if (ismanaged(v)) {
outsn("#:", f, 2); outsn("#:", f, 2);
@ -432,12 +432,12 @@ void fl_print_child(struct ios *f, value_t v)
} else if (v == FL_EOF) { } else if (v == FL_EOF) {
outsn("#<eof>", f, 6); outsn("#<eof>", f, 6);
} else if (isbuiltin(v)) { } else if (isbuiltin(v)) {
if (pr.opts.readably) if (!pr.opts.display)
outsn("#.", f, 2); outsn("#.", f, 2);
outs(builtin_names[uintval(v)], f); outs(builtin_names[uintval(v)], f);
} else { } else {
assert(isclosure(v)); assert(isclosure(v));
if (pr.opts.readably) { if (!pr.opts.display) {
struct function *fn; struct function *fn;
char *data; char *data;
size_t i, sz; size_t i, sz;
@ -478,7 +478,7 @@ void fl_print_child(struct ios *f, value_t v)
case TAG_CVALUE: case TAG_CVALUE:
case TAG_VECTOR: case TAG_VECTOR:
case TAG_CONS: case TAG_CONS:
if (pr.opts.readably && write_cycle_prefix(f, v)) if (!pr.opts.display && write_cycle_prefix(f, v))
break; break;
if (isvector(v)) { if (isvector(v)) {
int newindent, est, sz, i; int newindent, est, sz, i;
@ -651,7 +651,7 @@ static void cvalue_printdata(struct ios *f, void *data, size_t len,
{ {
if (type == bytesym) { if (type == bytesym) {
unsigned char ch = *(unsigned char *)data; unsigned char ch = *(unsigned char *)data;
if (!pr.opts.readably) if (pr.opts.display)
outc(ch, f); outc(ch, f);
else if (weak) else if (weak)
pr.column += ios_printf(f, "#x%hhx", ch); pr.column += ios_printf(f, "#x%hhx", ch);
@ -663,7 +663,7 @@ static void cvalue_printdata(struct ios *f, void *data, size_t len,
size_t nb = u8_toutf8(seq, sizeof(seq), &wc, 1); size_t nb = u8_toutf8(seq, sizeof(seq), &wc, 1);
seq[nb] = '\0'; seq[nb] = '\0';
if (!pr.opts.readably) { if (pr.opts.display) {
// TODO: better multibyte handling // TODO: better multibyte handling
if (wc == 0) if (wc == 0)
ios_putc(0, f); ios_putc(0, f);
@ -717,7 +717,7 @@ static void cvalue_printdata(struct ios *f, void *data, size_t len,
rep = sign_bit(d) ? "-nan.0" : "+nan.0"; rep = sign_bit(d) ? "-nan.0" : "+nan.0";
else else
rep = sign_bit(d) ? "-inf.0" : "+inf.0"; rep = sign_bit(d) ? "-inf.0" : "+inf.0";
if (type == floatsym && pr.opts.readably && !weak) if (type == floatsym && !pr.opts.display && !weak)
pr.column += ios_printf(f, "#%s(%s)", symbol_name(type), rep); pr.column += ios_printf(f, "#%s(%s)", symbol_name(type), rep);
else else
outs(rep, f); outs(rep, f);
@ -726,7 +726,7 @@ static void cvalue_printdata(struct ios *f, void *data, size_t len,
outsn("-0.0", f, 4); outsn("-0.0", f, 4);
else else
outsn("0.0", f, 3); outsn("0.0", f, 3);
if (type == floatsym && pr.opts.readably && !weak) if (type == floatsym && !pr.opts.display && !weak)
outc('f', f); outc('f', f);
} else { } else {
int hasdec; int hasdec;
@ -736,7 +736,7 @@ static void cvalue_printdata(struct ios *f, void *data, size_t len,
outs(buf, f); outs(buf, f);
if (!hasdec) if (!hasdec)
outsn(".0", f, 2); outsn(".0", f, 2);
if (type == floatsym && pr.opts.readably && !weak) if (type == floatsym && !pr.opts.display && !weak)
outc('f', f); outc('f', f);
} }
} else if (type == uint64sym } else if (type == uint64sym
@ -745,7 +745,7 @@ static void cvalue_printdata(struct ios *f, void *data, size_t len,
#endif #endif
) { ) {
uint64_t ui64 = *(uint64_t *)data; uint64_t ui64 = *(uint64_t *)data;
if (weak || !pr.opts.readably) if (weak || pr.opts.display)
pr.column += ios_printf(f, "%llu", ui64); pr.column += ios_printf(f, "%llu", ui64);
else else
pr.column += ios_printf(f, "#%s(%llu)", symbol_name(type), ui64); pr.column += ios_printf(f, "#%s(%llu)", symbol_name(type), ui64);
@ -757,7 +757,7 @@ static void cvalue_printdata(struct ios *f, void *data, size_t len,
pr.column += ios_printf(f, "#<%s>", symbol_name(type)); pr.column += ios_printf(f, "#<%s>", symbol_name(type));
} else { } else {
int64_t i64 = conv_to_int64(data, nt); int64_t i64 = conv_to_int64(data, nt);
if (weak || !pr.opts.readably) if (weak || pr.opts.display)
pr.column += ios_printf(f, "%lld", i64); pr.column += ios_printf(f, "%lld", i64);
else else
pr.column += pr.column +=
@ -778,7 +778,7 @@ static void cvalue_printdata(struct ios *f, void *data, size_t len,
cnt = elsize ? len / elsize : 0; cnt = elsize ? len / elsize : 0;
} }
if (eltype == bytesym) { if (eltype == bytesym) {
if (!pr.opts.readably) { if (pr.opts.display) {
ios_write(f, data, len); ios_write(f, data, len);
/* /*
char *nl = memrchr(data, '\n', len); char *nl = memrchr(data, '\n', len);
@ -850,7 +850,7 @@ static void cvalue_print(struct ios *f, value_t v)
pr.column += pr.column +=
ios_printf(f, "#<builtin @#x%08zx>", (size_t)(builtin_t)fptr); ios_printf(f, "#<builtin @#x%08zx>", (size_t)(builtin_t)fptr);
} else { } else {
if (!pr.opts.readably) { if (pr.opts.display) {
outs(symbol_name(label), f); outs(symbol_name(label), f);
} else { } else {
outsn("#fn(", f, 4); outsn("#fn(", f, 4);
@ -874,7 +874,7 @@ void print_with_options(struct ios *f, value_t v,
memcpy(&pr.opts, opts, sizeof(pr.opts)); memcpy(&pr.opts, opts, sizeof(pr.opts));
pr.level = 0; pr.level = 0;
pr.cycle_labels = 0; pr.cycle_labels = 0;
if (pr.opts.readably) if (!pr.opts.display)
print_traverse(v); print_traverse(v);
pr.line = pr.column = 0; pr.line = pr.column = 0;
@ -896,8 +896,10 @@ void fl_print(struct ios *f, value_t v)
struct printer_options opts; struct printer_options opts;
value_t pl; value_t pl;
// *print-readably*
opts.display = (symbol_value(printreadablysym) == FL_F);
opts.pretty = (symbol_value(printprettysym) != FL_F); opts.pretty = (symbol_value(printprettysym) != FL_F);
opts.readably = (symbol_value(printreadablysym) != FL_F);
pl = symbol_value(printlengthsym); pl = symbol_value(printlengthsym);
if (isfixnum(pl)) if (isfixnum(pl))