added printing of type names in printf
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 5 Jul 2021 15:19:26 +0000 (10:19 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 5 Jul 2021 15:19:26 +0000 (10:19 -0500)
core/conv.onyx
core/io/stream.onyx
tests/new_printf
tests/new_printf.onyx

index dd35531fe50aaa77c4976b91c6116cbc5b048075..0f16b1a34233f2047b7e917f5690a1b647577b05 100644 (file)
@@ -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);
 
index d94abde3062f748d0edab9432450e03cb48d58ec..9ab53f6d7a859d97c783936ca8ea18b62ff2df57 100644 (file)
@@ -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.{
index 08594c7822c7084fbc9eec43cdbe6b3c38c12755..e9e3a9229df925f5c179f539f1338d5db679057c 100644 (file)
@@ -1 +1 @@
-123 Test {} false 12.3 12.34567
+123 Test {} false 12.3 12.34567 [..] Map(i32, [] u8)
index cf188d5451fa1bb77795780625cb9b330467356c..9719371298696f15d3bba50e58463cba9d33442b 100644 (file)
@@ -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));
 }