Files
mercury/tests/invalid/string_format_bad.m
Zoltan Somogyi d4861d739d Allow formatting of sized integers.
library/string.m:
    Add {i,u}{8.16,32,64} as function symbols in the poly_type type,
    each with a single argument containing an integer with the named
    signedness and size.

    The idea is that each of these poly_type values works exactly
    the same way as the i(_) poly_type (if signed) or the u(_) poly_type
    (if unsigned), with the exception that the value specified by the call
    is cast to int or uint before being processed.

library/string.parse_runtime.m:
    Parse the new kinds of poly_types. Change the representation of the result
    of the parsing to allow recording of the sizes of ints and uints.

    Put the code that does the parsing into a predicate of its own.

library/string.format.m:
    Do a cast to int or uint if the size information recorded in the
    specification of a signed or unsigned integer value calls for it.

    Provide functions to do the casting that do not require the import
    of {int,uint}{8,16,32,64}.m. This is to allow the compiler to generate
    calls to do such casts without having to implicitly import those modules.

    Abort if a 64 bit number is being cast to a 32 bit word.

compiler/parse_string_format.m:
    Make the same changes as in string.parse_runtime.m, mutatis mutandis.

compiler/format_call.m:
    Handle the new kinds of poly_types by adding a cast to int or uint
    if necessary, using the predicates added to library/string.format.m.

    Use a convenience function to make code creating instmap deltas
    more readable.

library/io.m:
library/pprint.m:
library/string.parse_util.m:
tests/invalid/string_format_bad.m:
tests/invalid/string_format_unknown.m:
    Conform to the changes above.

tests/string_format/string_format_d.m:
tests/string_format/string_format_u.m:
    Test the printing of some of the new poly_types.

tests/string_format/string_format_d.exp2:
tests/string_format/string_format_u.exp2:
    Update the expected output of these tests on 64-bit platforms.

tests/string_format/string_format_lib.m:
    Update programming style.
2020-11-10 11:00:47 +11:00

73 lines
1.8 KiB
Mathematica

% vim: ts=4 sw=4 expandtab ft=mercury
:- module string_format_bad.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module bool.
:- import_module list.
:- import_module stream.
:- import_module stream.string_writer.
:- import_module string.
main(!IO) :-
S1 = string.format("", [s("x1")]),
io.write_string(S1, !IO),
S2 = string.format("%d", [s("x2")]),
io.write_string(S2, !IO),
io.stdout_stream(OutputStream, !IO),
io.format("%d", [s("x3")], !IO),
io.format(OutputStream, "%d", [s("x4")], !IO),
stream.string_writer.format(OutputStream, "%d", [s("x4")], !IO),
io.format("%w", [i(5)], !IO),
io.write_string(p(s("five")), !IO),
F6 = "%s %f",
make_bool(6, T6),
( if
T6 = yes,
V6A = i(6)
then
V6 = [s("six"), V6A],
io.format(OutputStream, F6, V6, !IO),
make_bool(7, T7),
F7 = "%d %s %d",
(
T7 = yes,
io.format(OutputStream, F7, [f(7.0) | V6], !IO)
;
T7 = no
)
else
true
).
:- pred make_bool(int::in, bool::out) is det.
make_bool(_, yes).
:- func t(string) = string.
t(S) = S.
:- func p(string.poly_type) = string.
p(s(S)) = t(string.format("%s", [s(S)])).
p(c(C)) = t(string.format("%c", [c(C)])).
p(i(I)) = t(string.format("%d", [i(I)])).
p(i8(I8)) = t(string.format("%d", [i8(I8)])).
p(i16(I16)) = t(string.format("%d", [i16(I16)])).
p(i32(I32)) = t(string.format("%d", [i32(I32)])).
p(i64(I64)) = t(string.format("%d", [i64(I64)])).
p(f(F)) = t(string.format("%f", [f(F)])).
p(u(U)) = t(string.format("%u", [u(U)])).
p(u8(U8)) = t(string.format("%u", [u8(U8)])).
p(u16(U16)) = t(string.format("%u", [u16(U16)])).
p(u32(U32)) = t(string.format("%u", [u32(U32)])).
p(u64(U64)) = t(string.format("%u", [u64(U64)])).