From: Brendan Hansen Date: Mon, 5 Jul 2021 15:19:26 +0000 (-0500) Subject: added printing of type names in printf X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=b961e2ab6934fe93ba1a6df10fa2d73ebc9c92cf;p=onyx.git added printing of type names in printf --- diff --git a/core/conv.onyx b/core/conv.onyx index dd35531f..0f16b1a3 100644 --- a/core/conv.onyx +++ b/core/conv.onyx @@ -387,6 +387,23 @@ str_format_va :: (format: str, buffer: [] u8, va: [] any) -> str { output->write(istr); } + case type_expr { + value := *(cast(^type_expr) v.data); + + io :: package core.io + + buf : [256] u8; + + // This is a little gross but the only way to output the type name for a type_expr + // is through a io.Writer. That should maybe be changed in the future? Also, I think + // 256 bytes is enough for the name of a type but I'm not entirely sure... + stream := io.string_stream_make(~~buf); + writer := io.writer_make(^stream); + type_info.write_type_name(^writer, value); + + output->write(io.string_stream_to_str(^stream)); + } + case #default { info := get_type_info(v.type); diff --git a/core/io/stream.onyx b/core/io/stream.onyx index d94abde3..9ab53f6d 100644 --- a/core/io/stream.onyx +++ b/core/io/stream.onyx @@ -153,6 +153,9 @@ string_stream_make :: (s: str) -> StringStream { }; } +string_stream_to_str :: (use ss: ^StringStream) -> str { + return .{ data.data, curr_pos }; +} #private string_stream_vtable := Stream_Vtable.{ diff --git a/tests/new_printf b/tests/new_printf index 08594c78..e9e3a922 100644 --- a/tests/new_printf +++ b/tests/new_printf @@ -1 +1 @@ -123 Test {} false 12.3 12.34567 +123 Test {} false 12.3 12.34567 [..] Map(i32, [] u8) diff --git a/tests/new_printf.onyx b/tests/new_printf.onyx index cf188d54..97193712 100644 --- a/tests/new_printf.onyx +++ b/tests/new_printf.onyx @@ -3,5 +3,5 @@ use package core main :: (args: [] cstr) { - printf("{} {} {{}} {} {.1} {.5}\n", 123, "Test", false, 12.34, 12.3456789); + printf("{} {} {{}} {} {.1} {.5} {}\n", 123, "Test", false, 12.34, 12.3456789, #type [..] map.Map(i32, str)); }