# bend-float64's format mini-language: format(x, spec) is Python's format(x, spec) for a float, every field of the # spec ([[fill]align][sign][z][#][0][width][grouping][.precision][type]) and every type but 'n' (which depends on # the locale). A spec Python refuses gives Fail{} with Python's message. Pure. import Base import ./wide.bend as W import ./big.bend as B import ./float64.bend as F import ./text.bend as T import ./format.bend as FM # ---- the spec ---- # fill and align (0: none given), sign ('-', '+' or ' '), z, '#', width, grouping (0, ',' or '_'), precision (has: # given or not), type (0: none) type Spec is Data: Spec{fill: U32, fill_set: Bool, align: U32, sign: U32, z: Bool, alt: Bool, width: U32, group: U32, has_prec: Bool, prec: U32, typ: U32} type PR is Data: POk{s: Spec} PErr{why: String} def is_align(+c: U32) -> Bool: Bool.or(Bool.or(U32.is_eq(c, 60), U32.is_eq(c, 62)), Bool.or(U32.is_eq(c, 61), U32.is_eq(c, 94))) def is_dig(+c: U32) -> Bool: Bool.and(U32.is_le(48, c), U32.is_le(c, 57)) def head(xs: List<&2, U32>) -> U32: match xs: case []: 0 case x <> t: x def tail(xs: List<&2, U32>) -> List<&2, U32>: match xs: case []: [] case x <> t: t def second(xs: List<&2, U32>) -> U32: head(tail(xs)) # a run of digits: its value (stuck past 2^31: too many digits), how many, and what follows type Num is Data: Num{v: U32, n: U32, rest: List<&2, U32>} def num_go(xs: List<&2, U32>, +v: U32, +n: U32) -> Num: match xs: case []: Num{v, n, []} case +c <> +t: Bool.pick(Num, is_dig(c), num_go(t, Bool.pick(U32, U32.is_lt(v, 214748364), (v * 10 + c - 48 : U32), 2147483648), (n + 1 : U32)), Num{v, n, c <> t}) def num(xs: List<&2, U32>) -> Num: num_go(xs, 0, 0) def too_many() -> PR: PErr{"Too many decimal digits in format string"} def chars(xs: List<&2, U32>) -> String: match xs: case []: SNil{} case x <> t: SCon{Chr{x}, chars(t)} def invalid(+spec: List<&2, U32>) -> PR: PErr{"Invalid format specifier '" ++ chars(spec) ++ "' for object of type 'float'"} def hex_digit(+d: U32) -> U32: (d + Bool.pick(U32, U32.is_lt(d, 10), 48, 87) : U32) def app(xs: List<&2, U32>, ys: List<&2, U32>) -> List<&2, U32>: List.append(&2, U32, xs, ys) # x's hex digits from bit 4 (n - 1) down, leading zeros dropped def hex_go(n: Nat, +x: U32, +started: Bool) -> List<&2, U32>: match n: case 0n: [] case 1n+ +k: +d = U32.and(U32.shrn(x, U32.to_nat(U32.mul(U32.from_nat(k), 4))), 15) +on = Bool.or(started, Bool.or(U32.is_ne(d, 0), Nat.is_eq(k, 0n))) app(Bool.pick(List<&2, U32>, on, [hex_digit(d)], []), hex_go(k, x, on)) def hex_lower(+x: U32) -> String: chars(hex_go(8n, x, False{})) # a printable ASCII code as itself, any other as \x and its hex digits # a printable ASCII code as itself, any other as a backslash, x and its hex digits (as CPython writes it) def code_text(+c: U32) -> String: Bool.pick(String, Bool.and(U32.is_lt(32, c), U32.is_lt(c, 128)), chars([c]), chars([92, 120]) ++ hex_lower(c)) def unknown(+c: U32) -> PR: PErr{"Unknown format code '" ++ code_text(c) ++ "' for object of type 'float'"} # 'e' 'E' 'f' 'F' 'g' 'G' '%' (and 'n', refused here) def float_type(+c: U32) -> Bool: Bool.or(Bool.or(Bool.or(U32.is_eq(c, 101), U32.is_eq(c, 69)), Bool.or(U32.is_eq(c, 102), U32.is_eq(c, 70))), Bool.or(Bool.or(U32.is_eq(c, 103), U32.is_eq(c, 71)), U32.is_eq(c, 37))) # the types CPython lets a grouping through with (the float's own type check comes after): d and the float types, # and with '_' also b, o, x, X def group_ok(+g: U32, +c: U32) -> Bool: Bool.or(Bool.or(float_type(c), U32.is_eq(c, 100)), Bool.and(U32.is_eq(g, 95), Bool.or(Bool.or(U32.is_eq(c, 98), U32.is_eq(c, 111)), Bool.or(U32.is_eq(c, 120), U32.is_eq(c, 88))))) def len_l(xs: List<&2, U32>) -> U32: match xs: case []: 0 case x <> t: (1 + len_l(t) : U32) def group_of(s: Spec) -> U32: match s: case Spec{f, fs, a, sg, z, alt, w, g, hp, p, ty}: g def with_type(s: Spec, +t: U32) -> Spec: match s: case Spec{f, fs, a, sg, z, alt, w, g, hp, p, ty}: Spec{f, fs, a, sg, z, alt, w, g, hp, p, t} def p_type(+rest: List<&2, U32>, +spec: List<&2, U32>, +s: Spec) -> PR: Bool.pick(PR, U32.is_eq(len_l(rest), 0), POk{s}, Bool.pick(PR, U32.is_lt(1, len_l(rest)), invalid(spec), Bool.pick(PR, Bool.and(U32.is_ne(group_of(s), 0), Bool.not(group_ok(group_of(s), head(rest)))), PErr{"Cannot specify '" ++ chars([group_of(s)]) ++ "' with '" ++ code_text(head(rest)) ++ "'."}, Bool.pick(PR, U32.is_eq(head(rest), 110), PErr{"format type 'n' is not supported (it depends on the locale)"}, Bool.pick(PR, float_type(head(rest)), POk{with_type(s, head(rest))}, unknown(head(rest))))))) def with_prec(s: Spec, +p: U32) -> Spec: match s: case Spec{f, fs, a, sg, z, alt, w, g, hp, op, ty}: Spec{f, fs, a, sg, z, alt, w, g, True{}, p, ty} def p_prec_num(n: Num, +spec: List<&2, U32>, +s: Spec) -> PR: match n: case Num{+v, +k, rest}: Bool.pick(PR, U32.is_eq(k, 0), PErr{"Format specifier missing precision"}, Bool.pick(PR, U32.is_le(2147483648, v), too_many(), p_type(rest, spec, with_prec(s, v)))) def p_prec(+xs: List<&2, U32>, +spec: List<&2, U32>, +s: Spec) -> PR: Bool.pick(PR, U32.is_eq(head(xs), 46), p_prec_num(num(tail(xs)), spec, s), p_type(xs, spec, s)) def with_group(s: Spec, +g: U32) -> Spec: match s: case Spec{f, fs, a, sg, z, alt, w, og, hp, p, ty}: Spec{f, fs, a, sg, z, alt, w, g, hp, p, ty} def is_group(+c: U32) -> Bool: Bool.or(U32.is_eq(c, 44), U32.is_eq(c, 95)) def p_group(+xs: List<&2, U32>, +spec: List<&2, U32>, +s: Spec) -> PR: Bool.pick(PR, is_group(head(xs)), Bool.pick(PR, Bool.and(is_group(second(xs)), U32.is_ne(second(xs), head(xs))), PErr{"Cannot specify both ',' and '_'."}, p_prec(tail(xs), spec, with_group(s, head(xs)))), p_prec(xs, spec, s)) def with_width(s: Spec, +w: U32) -> Spec: match s: case Spec{f, fs, a, sg, z, alt, ow, g, hp, p, ty}: Spec{f, fs, a, sg, z, alt, w, g, hp, p, ty} def p_width_num(n: Num, +spec: List<&2, U32>, +s: Spec) -> PR: match n: case Num{+v, k, rest}: Bool.pick(PR, U32.is_le(2147483648, v), too_many(), p_group(rest, spec, with_width(s, v))) # '0': the fill, unless given, becomes '0'; the alignment, unless given, '=' def p_zero(+xs: List<&2, U32>, +spec: List<&2, U32>, s: Spec) -> PR: match s: case Spec{+f, +fs, +a, +sg, +z, +alt, +w, +g, +hp, +p, +ty}: Bool.pick(PR, U32.is_eq(head(xs), 48), p_width_num(num(tail(xs)), spec, Spec{Bool.pick(U32, fs, f, 48), True{}, Bool.pick(U32, U32.is_eq(a, 0), 61, a), sg, z, alt, w, g, hp, p, ty}), p_width_num(num(xs), spec, Spec{f, fs, a, sg, z, alt, w, g, hp, p, ty})) def p_alt(+xs: List<&2, U32>, +spec: List<&2, U32>, s: Spec) -> PR: match s: case Spec{f, fs, a, sg, z, alt, w, g, hp, p, ty}: +y = U32.is_eq(head(xs), 35) p_zero(Bool.pick(List<&2, U32>, y, tail(xs), xs), spec, Spec{f, fs, a, sg, z, y, w, g, hp, p, ty}) def p_z(+xs: List<&2, U32>, +spec: List<&2, U32>, s: Spec) -> PR: match s: case Spec{f, fs, a, sg, z, alt, w, g, hp, p, ty}: +y = U32.is_eq(head(xs), 122) p_alt(Bool.pick(List<&2, U32>, y, tail(xs), xs), spec, Spec{f, fs, a, sg, y, alt, w, g, hp, p, ty}) def p_sign(+xs: List<&2, U32>, +spec: List<&2, U32>, s: Spec) -> PR: match s: case Spec{f, fs, a, sg, z, alt, w, g, hp, p, ty}: +c = head(xs) +y = Bool.or(Bool.or(U32.is_eq(c, 43), U32.is_eq(c, 45)), U32.is_eq(c, 32)) p_z(Bool.pick(List<&2, U32>, y, tail(xs), xs), spec, Spec{f, fs, a, Bool.pick(U32, y, c, 45), z, alt, w, g, hp, p, ty}) def empty_spec() -> Spec: Spec{32, False{}, 0, 45, False{}, False{}, 0, 0, False{}, 6, 0} # fill and align: a fill character is any character followed by an alignment character def p_fill(+xs: List<&2, U32>) -> PR: Bool.pick(PR, Bool.and(U32.is_lt(1, len_l(xs)), is_align(second(xs))), p_sign(tail(tail(xs)), xs, Spec{head(xs), True{}, second(xs), 45, False{}, False{}, 0, 0, False{}, 6, 0}), Bool.pick(PR, is_align(head(xs)), p_sign(tail(xs), xs, Spec{32, False{}, head(xs), 45, False{}, False{}, 0, 0, False{}, 6, 0}), p_sign(xs, xs, empty_spec()))) # ---- the number's text (no sign) ---- def upper(+c: U32) -> U32: Bool.pick(U32, Bool.and(U32.is_le(97, c), U32.is_le(c, 122)), (c - 32 : U32), c) def map_upper(xs: List<&2, U32>) -> List<&2, U32>: match xs: case []: [] case c <> t: upper(c) <> map_upper(t) def contains(xs: List<&2, U32>, +c: U32) -> Bool: match xs: case []: False{} case x <> t: Bool.or(U32.is_eq(x, c), contains(t, c)) def insert_point(xs: List<&2, U32>) -> List<&2, U32>: match xs: case []: [46] case +c <> +t: Bool.pick(List<&2, U32>, Bool.or(U32.is_eq(c, 101), U32.is_eq(c, 69)), 46 <> c <> t, c <> insert_point(t)) # a point inserted before the exponent (or at the end) when there is none: '#' for f, e and none def ensure_point(+xs: List<&2, U32>) -> List<&2, U32>: Bool.pick(List<&2, U32>, contains(xs, 46), xs, insert_point(xs)) def dig_list(ds: List<&2, U32>) -> List<&2, U32>: match ds: case []: [] case d <> t: (d + 48 : U32) <> dig_list(t) def exp_list(+x: U32) -> List<&2, U32>: 101 <> T.codes(T.exp_text(x)) # %g (add0: none-type with a precision, which keeps a ".0" and switches to an exponent one place sooner; alt: '#', # which keeps trailing zeros and the point) type GS is Data: GFix{} GLow{} GExp{} def g_frac(+alt: Bool, +add0: Bool, +sci: Bool, +fr: List<&2, U32>) -> List<&2, U32>: +f2 = Bool.pick(List<&2, U32>, alt, fr, FM.strip_frac(fr)) Bool.pick(List<&2, U32>, Bool.or(alt, U32.is_ne(len_l(f2), 0)), 46 <> dig_list(f2), Bool.pick(List<&2, U32>, Bool.and(add0, Bool.not(sci)), [46, 48], [])) def g_shape(g: GS, +ds: List<&2, U32>, +x: U32, +alt: Bool, +add0: Bool) -> List<&2, U32>: match g: case GFix{}: List.append(&2, U32, dig_list(T.take_d(U32.to_nat((x + 1 : U32)), ds)), g_frac(alt, add0, False{}, T.drop_d(U32.to_nat((x + 1 : U32)), ds))) case GLow{}: 48 <> g_frac(alt, add0, False{}, FM.pad(U32.to_nat((0 - x - 1 : U32)), ds)) case GExp{}: List.append(&2, U32, dig_list(T.take_d(1n, ds)), List.append(&2, U32, g_frac(alt, add0, True{}, T.drop_d(1n, ds)), exp_list(x))) def g_sig(s: FM.Sig, +p: U32, +alt: Bool, +add0: Bool) -> List<&2, U32>: match s: case FM.Sig{+x, r}: +lim = Bool.pick(U32, add0, (p - 1 : U32), p) g_shape(Bool.pick(GS, Bool.and(Bool.not(F.s_lt(x, (0 - 4 : U32))), F.s_lt(x, lim)), Bool.pick(GS, FM.is_neg(x), GLow{}, GFix{}), GExp{}), FM.digits(r), x, alt, add0) def g_body_p(c: F.Cls, +p: U32, +alt: Bool, +add0: Bool) -> List<&2, U32>: match c: case F.FFin{s, +e, +m}: g_sig(FM.sig(e, m, (p - 1 : U32)), p, alt, add0) case _: g_shape(Bool.pick(GS, U32.is_lt(0, Bool.pick(U32, add0, (p - 1 : U32), p)), GFix{}, GExp{}), FM.pad(U32.to_nat(p), []), 0, alt, add0) # p significant digits (1 for 0) def g_body(c: F.Cls, +p0: U32, +alt: Bool, +add0: Bool) -> List<&2, U32>: g_body_p(c, Bool.pick(U32, U32.is_eq(p0, 0), 1, p0), alt, add0) # the text of |x| for a type (x finite) type TK is Data: TF{} TE{} TG{} TPct{} TRepr{} TNone{} def tk_of(+t: U32, +has_prec: Bool) -> TK: Bool.pick(TK, Bool.or(U32.is_eq(t, 102), U32.is_eq(t, 70)), TF{}, Bool.pick(TK, Bool.or(U32.is_eq(t, 101), U32.is_eq(t, 69)), TE{}, Bool.pick(TK, Bool.or(U32.is_eq(t, 103), U32.is_eq(t, 71)), TG{}, Bool.pick(TK, U32.is_eq(t, 37), TPct{}, Bool.pick(TK, has_prec, TNone{}, TRepr{}))))) def body(k: TK, +ax: F.F64, +p: U32, +alt: Bool) -> List<&2, U32>: match k: case TF{}: +b = T.codes(FM.fixed(ax, p)) Bool.pick(List<&2, U32>, alt, ensure_point(b), b) case TE{}: +b = T.codes(FM.sci(ax, p)) Bool.pick(List<&2, U32>, alt, ensure_point(b), b) case TG{}: g_body(F.cls(ax), p, alt, False{}) case TPct{}: +y = F.mul(ax, F.F64{1079574528, 0}) +b = T.codes(FM.fixed(F.abs(y), p)) List.append(&2, U32, Bool.pick(List<&2, U32>, Bool.and(alt, F.is_finite(y)), ensure_point(b), b), [37]) case TRepr{}: +b = T.codes(T.show(ax)) Bool.pick(List<&2, U32>, alt, ensure_point(b), b) case TNone{}: g_body(F.cls(ax), p, alt, True{}) # no digit from 1 to 9 before the exponent: the number rounded to zero ('z' then drops its sign) def zero_text(xs: List<&2, U32>) -> Bool: match xs: case []: True{} case +c <> t: Bool.pick(Bool, Bool.or(U32.is_eq(c, 101), U32.is_eq(c, 69)), True{}, Bool.and(Bool.not(Bool.and(U32.is_le(49, c), U32.is_le(c, 57))), zero_text(t))) # ---- grouping and padding ---- def lead_digits(xs: List<&2, U32>) -> List<&2, U32>: match xs: case []: [] case +c <> t: Bool.pick(List<&2, U32>, is_dig(c), c <> lead_digits(t), []) def after_digits(xs: List<&2, U32>) -> List<&2, U32>: match xs: case []: [] case +c <> +t: Bool.pick(List<&2, U32>, is_dig(c), after_digits(t), c <> t) def group_go(xs: List<&2, U32>, +sep: U32, +i: U32) -> List<&2, U32>: match xs: case []: [] case +c <> t: List.append(&2, U32, Bool.pick(List<&2, U32>, Bool.and(U32.is_eq(U32.mod(i, 3), 0), U32.is_ne(i, 0)), [sep, c], [c]), group_go(t, sep, (i + 1 : U32))) def grouped(+ds: List<&2, U32>, +sep: U32) -> List<&2, U32>: List.reverse(&2, U32, group_go(List.reverse(&2, U32, ds), sep, 0)) # k digits grouped take k + (k - 1) / 3 characters def glen(+k: U32) -> U32: (k + U32.div((k - 1 : U32), 3) : U32) def zcount(n: Nat, +k: U32, +minw: U32) -> U32: match n: case 0n: k case 1n+j: Bool.pick(U32, U32.is_le(minw, glen(k)), k, zcount(j, (k + 1 : U32), minw)) def fills(n: Nat, +c: U32) -> List<&2, U32>: match n: case 0n: [] case 1n+k: c <> fills(k, c) # '0' fill with '=' alignment and grouping: zeros join the digits, grouped too, until the number fills minw (and it # never starts with a separator) def zero_grouped(+ds: List<&2, U32>, +sep: U32, +minw: U32) -> List<&2, U32>: +l = len_l(ds) +k = zcount(U32.to_nat((minw + 2 : U32)), l, minw) grouped(app(fills(U32.to_nat((k - l : U32)), 48), ds), sep) type AL is Data: ALeft{} ARight{} ACenter{} AEq{} def al_of(+a: U32) -> AL: Bool.pick(AL, U32.is_eq(a, 60), ALeft{}, Bool.pick(AL, U32.is_eq(a, 61), AEq{}, Bool.pick(AL, U32.is_eq(a, 94), ACenter{}, ARight{}))) def pad_al(k: AL, +fill: U32, +pad: U32, +sg: List<&2, U32>, +b: List<&2, U32>) -> List<&2, U32>: match k: case ALeft{}: app(app(sg, b), fills(U32.to_nat(pad), fill)) case AEq{}: app(app(sg, fills(U32.to_nat(pad), fill)), b) case ACenter{}: app(app(fills(U32.to_nat(U32.div(pad, 2)), fill), app(sg, b)), fills(U32.to_nat((pad - U32.div(pad, 2) : U32)), fill)) case ARight{}: app(fills(U32.to_nat(pad), fill), app(sg, b)) # sign, then the body, padded to width with fill as align says ('>' when none) def pad_out(+align: U32, +fill: U32, +width: U32, +sg: List<&2, U32>, +b: List<&2, U32>) -> List<&2, U32>: +len = (len_l(sg) + len_l(b) : U32) pad_al(al_of(align), fill, Bool.pick(U32, U32.is_lt(len, width), (width - len : U32), 0), sg, b) def sign_list(+neg: Bool, +sign: U32) -> List<&2, U32>: Bool.pick(List<&2, U32>, neg, [45], Bool.pick(List<&2, U32>, U32.is_eq(sign, 43), [43], Bool.pick(List<&2, U32>, U32.is_eq(sign, 32), [32], []))) def is_upper_type(+t: U32) -> Bool: Bool.or(Bool.or(U32.is_eq(t, 70), U32.is_eq(t, 69)), U32.is_eq(t, 71)) type GK is Data: GNone{} GPlain{} GZeros{} def group_k(k: GK, +ip: List<&2, U32>, +rest: List<&2, U32>, +g: U32, +minw: U32) -> List<&2, U32>: match k: case GNone{}: app(ip, rest) case GPlain{}: app(grouped(ip, g), rest) case GZeros{}: app(zero_grouped(ip, g, minw), rest) # the digits before the point grouped; under '0' fill with '=' alignment the padding zeros join them def group_num(+b: List<&2, U32>, +f: U32, +a: U32, +w: U32, +g: U32, +slen: U32) -> List<&2, U32>: +ip = lead_digits(b) +rest = after_digits(b) +used = (slen + len_l(rest) : U32) group_k(Bool.pick(GK, U32.is_eq(g, 0), GNone{}, Bool.pick(GK, Bool.and(U32.is_eq(f, 48), U32.is_eq(a, 61)), GZeros{}, GPlain{})), ip, rest, g, Bool.pick(U32, U32.is_lt(used, w), (w - used : U32), 0)) # ---- the whole ---- # '%' follows inf and nan too def pct(+ty: U32, +b: List<&2, U32>) -> List<&2, U32>: Bool.pick(List<&2, U32>, U32.is_eq(ty, 37), app(b, [37]), b) def render_go(c: F.Cls, +x: F.F64, +f: U32, +a: U32, +sg: U32, +z: Bool, +alt: Bool, +w: U32, +g: U32, +hp: Bool, +p: U32, +ty: U32, +up: Bool) -> List<&2, U32>: match c: case F.FNan{}: pad_out(a, f, w, sign_list(False{}, sg), pct(ty, Bool.pick(List<&2, U32>, up, [78, 65, 78], [110, 97, 110]))) case F.FInf{s}: pad_out(a, f, w, sign_list(U32.is_ne(s, 0), sg), pct(ty, Bool.pick(List<&2, U32>, up, [73, 78, 70], [105, 110, 102]))) case _: +b0 = body(tk_of(ty, hp), F.abs(x), p, alt) +b = Bool.pick(List<&2, U32>, up, map_upper(b0), b0) +sl = sign_list(Bool.and(F.signbit(x), Bool.not(Bool.and(z, zero_text(b)))), sg) pad_out(a, f, w, sl, group_num(b, f, a, w, g, len_l(sl))) def render_c(c: F.Cls, +x: F.F64, s: Spec) -> List<&2, U32>: match s: case Spec{+f, fs, +a, +sg, +z, +alt, +w, +g, +hp, +p, +ty}: render_go(c, x, f, a, sg, z, alt, w, g, hp, p, ty, is_upper_type(ty)) def render(+x: F.F64, s: Spec) -> List<&2, U32>: render_c(F.cls(x), x, s) def done(r: PR, +x: F.F64) -> Result<&2, &2, String, String>: match r: case POk{s}: Done{chars(render(x, s))} case PErr{w}: Fail{w} # Python's format(x, spec) for a float: Done{text}, or Fail{Python's message} for a spec Python refuses (or one with # type 'n', which depends on the locale and is not supported) def format(+x: F.F64, spec: String) -> Result<&2, &2, String, String>: done(p_fill(T.codes(spec)), x)