initial version of new and better str_format
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 1 Jul 2021 16:29:19 +0000 (11:29 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 1 Jul 2021 16:29:19 +0000 (11:29 -0500)
50 files changed:
bin/onyx
core/alloc/logging.onyx
core/conv.onyx
core/io/writer.onyx
core/std.onyx
core/stdio.onyx
core/type_info/helper.onyx
modules/immediate_mode/gl_utils.onyx
src/onyxwasm.c
tests/aoc-2020/day1.onyx
tests/aoc-2020/day10.onyx
tests/aoc-2020/day11.onyx
tests/aoc-2020/day12.onyx
tests/aoc-2020/day13.onyx
tests/aoc-2020/day14.onyx
tests/aoc-2020/day15.onyx
tests/aoc-2020/day16.onyx
tests/aoc-2020/day17.onyx
tests/aoc-2020/day18.onyx
tests/aoc-2020/day19.onyx
tests/aoc-2020/day2.onyx
tests/aoc-2020/day20.onyx
tests/aoc-2020/day21.onyx
tests/aoc-2020/day22.onyx
tests/aoc-2020/day23.onyx
tests/aoc-2020/day24.onyx
tests/aoc-2020/day25.onyx
tests/aoc-2020/day3.onyx
tests/aoc-2020/day4.onyx
tests/aoc-2020/day5.onyx
tests/aoc-2020/day6.onyx
tests/aoc-2020/day7.onyx
tests/aoc-2020/day8.onyx
tests/aoc-2020/day9.onyx
tests/array_struct_robustness.onyx
tests/baked_parameters.onyx
tests/better_field_accesses.onyx
tests/defer_with_continue.onyx
tests/float_parsing
tests/i32map.onyx
tests/lazy_iterators.onyx
tests/multiple_returns_robustness.onyx
tests/named_arguments_test.onyx
tests/new_printf [new file with mode: 0644]
tests/new_printf.onyx [new file with mode: 0644]
tests/new_struct_behaviour.onyx
tests/operator_overload.onyx
tests/polymorphic_array_lengths.onyx
tests/struct_robustness.onyx
tests/vararg_test.onyx

index 61def4067d2fce92f8f597256dd7c8137d717e57..018c527e86e83b881220b65689209e054c44cecf 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index 14f5144eb3d685a34561c6950f33bd81ed142188..33579f3c46662853f864aa6a799be682e9ab8806 100644 (file)
@@ -14,7 +14,7 @@ logging_allocator_proc :: (data: rawptr, aa: AllocationAction, size: u32, align:
     res := allocator.func(allocator.data, aa, size, align, oldptr);
 
     use package core { printf }
-    printf("%s with size %i, align %i, oldptr %p returns %p\n",
+    printf("{} with size {}, align {}, oldptr {} returns {}\n",
         Allocation_Action_Strings[cast(u32) aa], size, align, oldptr, res);
 
     return res;
index fe93b1f62e1597eab1e0c45f29be623c1b4bb4a1..cd020b69f2c577266972e855f54d6b3979428604 100644 (file)
@@ -157,6 +157,7 @@ i64_to_str :: (n: i64, base: u64, buf: [] u8, min_length := 0) -> str {
     return str.{ data = c + 1, count = len };
 }
 
+#if false {
 @Hack @Cleanup // This is a big hack but it will work for now
 f64_to_str :: (f: f64, buf: [] u8) -> str {
     f *= 10000.0;
@@ -181,7 +182,281 @@ f64_to_str :: (f: f64, buf: [] u8) -> str {
 
     return str.{ buf.data, len };
 }
+}
+
+// This is better than the above version, but still relies on converting the integer
+// part of the float to an integer, which could overflow.
+f64_to_str :: (f: f64, buf: [] u8) -> str {
+    math :: package core.math
+
+    len := 0;
+
+    if f < 0 {
+        f = -f;
+        buf[0] = #char "-";
+        len += 1;
+    }
+
+    dec_part := f - math.trunc(f);
+    int_part := f - dec_part;
+    dec_part  = math.abs(dec_part);
+
+    s1 := i64_to_str(~~int_part, 10, buf);
+    for i: 0 .. s1.count do buf.data[i + len] = s1.data[i];
+    buf.data[s1.count + len] = #char ".";
+    len += s1.count + 1;
+
+    digits := "0123456789";
+
+    decimals :: 4;
+    for i: decimals {
+        dec_part *= 10;
+        v := math.trunc(dec_part);
+        dec_part -= v;
+
+        buf.data[len + i] = digits[cast(i32) v];
+    }
+    len += decimals;
+
+    return str.{ buf.data, len };
+}
+
+str_format :: (format: str, buffer: [] u8, va: ..any) -> str {
+    return str_format_va(format, buffer, ~~va); 
+}
+
+str_format_va :: (format: str, buffer: [] u8, va: [] any) -> str {
+    @Cleanup
+    Output :: struct {
+        data: ^u8;
+        count: u32;
+        capacity: u32;
+
+        write :: #match {
+            (use output: ^Output, c: u8) {
+                if count >= capacity do return;
+
+                data[count] = c;
+                count += 1;
+            },
+
+            (use output: ^Output, s: str) {
+                for c: s do output->write(c);
+            }
+        }
+    }
+
+    output := Output.{ buffer.data, 0, buffer.count };
+
+    Format :: struct {
+        digits_after_decimal := cast(u32) 4;
+    }
+    formatting := Format.{};
+
+    vararg_index := 0;
+
+    while i := 0; i < format.count {
+        defer i += 1;
+
+        ch := format[i];
+
+        if ch == #char "{" {
+            if format[i + 1] == #char "{" {
+                output->write(#char "{");
+                i += 1;
+                continue;
+            }
+
+            while true {
+                i += 1;
+                ch = format[i];
+
+                switch ch {
+                    case #char "." {
+                        i += 1;
+
+                        digits := 0;
+                        while format[i] >= #char "0" && format[i] <= #char "9" {
+                            digits *= 10;
+                            digits += ~~(format[i] - #char "0");
+                            i += 1;
+                        }
+
+                        formatting.digits_after_decimal = digits;
+                    }
 
+                    case #default do break break;
+                }
+            }
+        }
+
+        if ch == #char "}" {
+            if format[i + 1] == #char "}" {
+                output->write(#char "}");
+                i += 1;
+                continue;
+            }
+
+            arg := va[vararg_index];
+            vararg_index += 1;
+            print_any(^output, arg);
+
+            continue;
+        }
+
+        output->write(ch);
+    }
+
+    return .{ output.data, output.count };
+
+    print_any :: (output: ^Output, v: any) {
+        use package builtin.type_info
+        array :: package core.array
+
+        switch v.type {
+            case bool {
+                value := *(cast(^bool) v.data);
+                if value do output->write("true");
+                else     do output->write("false");
+            }
+
+            case i32, u32 {
+                value := *(cast(^i32) v.data);
+
+                ibuf : [128] u8;
+                istr := i64_to_str(~~value, 10, ~~ibuf);
+                output->write(istr);
+            }
+
+            case i64, u64 {
+                value := *(cast(^i64) v.data);
+
+                ibuf : [128] u8;
+                istr := i64_to_str(~~value, 10, ~~ibuf);
+                output->write(istr);
+            }
+
+            case f32 {
+                value := *(cast(^f32) v.data);
+
+                fbuf : [128] u8;
+                fstr := f64_to_str(~~value, ~~fbuf);
+                output->write(fstr);
+            }
+
+            case f64 {
+                value := *(cast(^f64) v.data);
+
+                fbuf : [128] u8;
+                fstr := f64_to_str(~~value, ~~fbuf);
+                output->write(fstr);
+            }
+
+            case str do output->write(*(cast(^str) v.data));
+
+            case rawptr {
+                value := *(cast(^rawptr) v.data);
+
+                ibuf : [128] u8;
+                istr := i64_to_str(~~value, 16, ~~ibuf);
+                output->write(istr);
+            }
+
+            case #default {
+                info := get_type_info(v.type);
+
+                if info.kind == .Struct {
+                    s := cast(^Type_Info_Struct) info;
+
+                    if s.name.count > 0 do output->write(s.name);
+                    output->write(" { ");
+                    
+                    for ^member: s.members {
+                        if member != s.members.data do output->write(", ");
+
+                        output->write(member.name);
+                        output->write(" = ");
+
+                        print_any(output, .{ ~~(cast(^u8) v.data + member.offset), member.type });
+                    }
+
+                    output->write(" }");
+                }
+
+                if info.kind == .Function do output->write("<function>");
+
+                if info.kind == .Pointer {
+                    value := *(cast(^rawptr) v.data);
+
+                    ibuf : [128] u8;
+                    istr := i64_to_str(~~value, 16, ~~ibuf);
+                    output->write(istr);
+                }
+
+                // This assumes that the following type_info kinds are basically the same.
+                if info.kind == .Dynamic_Array || info.kind == .Slice || info.kind == .Variadic_Argument {
+                    output->write("[ ");
+
+                    a := cast(^Type_Info_Dynamic_Array) info;
+                    arr := cast(^array.Untyped_Array) v.data;
+                    data  := arr.data;
+                    count := arr.count;
+
+                    for i: count {
+                        if i != 0 do output->write(", ");
+
+                        print_any(output, .{ ~~(cast(^u8) data + get_type_info(a.of).size * i), a.of });
+                    }
+                    
+                    output->write(" ]");
+                }
+
+                if info.kind == .Array {
+                    output->write("[ ");
+
+                    a := cast(^Type_Info_Array) info;
+                    data := v.data;
+
+                    for i: a.count {
+                        if i != 0 do output->write(", ");
+
+                        print_any(output, .{ ~~(cast(^u8) data + get_type_info(a.of).size * i), a.of });
+                    }
+
+                    output->write(" ]");
+                }
+
+                if info.kind == .Enum {
+                    e := cast(^Type_Info_Enum) info;
+
+                    value: u64;
+                    switch e.backing_type {
+                        case i8,  u8  do value = cast(u64) *(cast(^u8) v.data);
+                        case i16, u16 do value = cast(u64) *(cast(^u16) v.data);
+                        case i32, u32 do value = cast(u64) *(cast(^u32) v.data);
+                        case i64, u64 do value = cast(u64) *(cast(^u64) v.data);
+                        case #default do assert(false, "Bad enum backing type");
+                    }
+
+                    {
+                        for ^member: e.members {
+                            if value == member.value {
+                                output->write(member.name);
+                                break break;
+                            }
+                        }
+
+                        output->write("UNKNOWN");
+                    }
+                }
+            }
+        }
+    }
+}
+
+
+// Old % style print formatting
+#if false {
 str_format :: (format: str, buffer: [] u8, va: ...) -> str {
        return str_format_va(format, buffer, va);
 }
@@ -306,3 +581,5 @@ str_format_va :: (format: str, buffer: [] u8, va: vararg) -> str {
 
     return str.{ ~~buffer.data, len };
 }
+}
+
index 0bb40c2b9742bbb56f6c010cb2d08eaae6223705..da730eae5bbabff93ad97e0814ccda6ff47b1530 100644 (file)
@@ -75,10 +75,10 @@ write_range :: (use writer: ^Writer, r: range, sep := " ") {
     }
 }
 
-write_format :: (use writer: ^Writer, format: str, va: ...) {
+write_format :: (use writer: ^Writer, format: str, va: ..any) {
     // POTENTIAL BUG: this buffer will need to be bigger (or dynamic).
     buffer: [2048] u8;
-    write_str(writer, conv.str_format_va(format, ~~buffer, va));
+    write_str(writer, conv.str_format_va(format, ~~buffer, ~~va));
 }
 
 write_escaped_str :: (use writer: ^Writer, s: str) {
index 0d12896cabd3b0ec0149e9600018b8f9067ed939..139e13c6f675b9237d3dc40949b63a7407f365b7 100644 (file)
@@ -33,6 +33,8 @@ package core
 #load "./runtime/build_opts"
 #load "./runtime/common"
 
+#load "./type_info/helper"
+
 #private_file runtime :: package runtime
 #if runtime.Runtime == runtime.Runtime_Wasi {
     #load "./runtime/wasi"
@@ -40,8 +42,6 @@ package core
     #load "./wasi/env"
     #load "./wasi/clock"
     #load "./io/file"
-
-    #load "./type_info/helper"
 }
 
 #if runtime.Runtime == runtime.Runtime_Js {
index 90d4680e78a87c31b0b60183992bbe62d8a076da..51c9adea096c7512889379b031e2defe2740b7d8 100644 (file)
@@ -29,9 +29,9 @@ println :: (x: $T) {
     print("\n");
 }
 
-printf :: (format: str, va: ...) {
+printf :: (format: str, va: ..any) {
     buffer: [2048] u8;
-    print(conv.str_format_va(format, buffer[0 .. 2048], va));
+    print(conv.str_format_va(format, buffer[0 .. 2048], ~~va));
 }
 
 // This works on both slices and arrays
index d85a476de5a910ab6f831ef239d6037570e27e44..faa0704b8e0616ce02fd4070b44a9b082e9614e3 100644 (file)
@@ -49,7 +49,7 @@ write_type_name :: (writer: ^io.Writer, t: type_expr) {
 
         case .Array {
             arr := cast(^Type_Info_Array) info;
-            io.write_format(writer, "[%i] ", arr.count);
+            io.write_format(writer, "[{}] ", arr.count);
             write_type_name(writer, arr.of);
         }
 
index fbf8c1e14d0f561abfd4166e537499878a49ba31..9abbcb499bad7eae511b81f018963583457c7b60 100644 (file)
@@ -52,7 +52,7 @@ Shader :: struct {
 
             success := true;
             if gl.getShaderParameter(shader, gl.COMPILE_STATUS) == 0 {
-                printf("Error compiling shader.");
+                println("Error compiling shader.");
                 gl.printShaderInfoLog(shader);
                 success = false;
             }
@@ -68,7 +68,7 @@ Shader :: struct {
 
             success := true;
             if gl.getProgramParameter(program, gl.LINK_STATUS) == 0 {
-                printf("Error linking program.");
+                println("Error linking program.");
                 gl.printProgramInfoLog(program);
                 success = false;
             }
index 478490493bcb48bfe567140f51cefd148fb53370..60d5a3c884033658f9a340cb15169e5b38567a39 100644 (file)
@@ -3089,6 +3089,13 @@ static void emit_string_literal(OnyxWasmModule* mod, AstStrLit* strlit) {
     i8* strdata = bh_alloc_array(global_heap_allocator, i8, strlit->token->length + 1);
     i32 length  = string_process_escape_seqs(strdata, strlit->token->text, strlit->token->length);
 
+    // Warning for having '%' in a string literal (because that probably is being used for a old print format)
+    /*
+    if (charset_contains((const char *) strdata, '%')) {
+        onyx_report_warning(strlit->token->pos, "Found string literal with '%%'");
+    }
+    */
+
     if (bh_table_has(StrLitInfo, mod->string_literals, (char *) strdata)) {
         StrLitInfo sti = bh_table_get(StrLitInfo, mod->string_literals, (char *) strdata);
         strlit->addr   = sti.addr;
index d4728cae5d81b57746e5ff740811a241bcf7d65d..eecdcddcfb5987d9984b4c0f020df3c60ac4a5ee 100644 (file)
@@ -17,7 +17,7 @@ main :: (args: [] cstr) {
 
     for i: nums do for j: nums do for k: nums {
         if j + i + k == 2020 {
-            printf("Answer: %i\n", i * j * k);
+            printf("Answer: {}\n", i * j * k);
 
             // Break out of all of the loops
             break break break;
index 61f942b6ac2cb329e97e5baee01b85ee585aa753..81fa274685ad12a07e88b4415c25f8910eb0a86a 100644 (file)
@@ -47,6 +47,6 @@ main :: (args: [] cstr) {
     for i: 1 .. nums.count do diffs[nums[i] - nums[i - 1] - 1] += 1;
     diffs[2] += 1;
 
-    printf("Diff prod: %i\n", diffs[0] * diffs[2]);
-    printf("Arrangements: %l\n", count_ending_paths(nums));
+    printf("Diff prod: {}\n", diffs[0] * diffs[2]);
+    printf("Arrangements: {}\n", count_ending_paths(nums));
 }
index af756dc5780788122fc71a94bad1b2fcaba9ed05..40a5df5a1a3a649e3a939f2a8b7cf807ab6f26a2 100644 (file)
@@ -96,6 +96,6 @@ main :: (args: [] cstr) {
     occupied := 0;
     for s: gos.seats do if s == SeatState.Occupied do occupied += 1;
     
-    printf("Occupied: %i\n", occupied);
+    printf("Occupied: {}\n", occupied);
 }
 
index 0dd6ff557824818608d07b6bc38367cf4ce6610d..6269127dbf99e2b9b40b021b4a527b82dba15e2a 100644 (file)
@@ -67,5 +67,5 @@ main :: (args: [] cstr) {
         }
     }
 
-    printf("Ship distance: %i\n", math.abs(ship.x) + math.abs(ship.y));
+    printf("Ship distance: {}\n", math.abs(ship.x) + math.abs(ship.y));
 }
index 1cfd7f59b08c55bbe93304c645b5f01b3eb15892..97fe1603c98928120f34b2159f4087f11dcceacd 100644 (file)
@@ -82,11 +82,11 @@ main :: (args: [] cstr) {
     // }
 
     // result := take_bus * (depart - est);
-    // printf("Bus: %i\n", take_bus);
-    // printf("Depart at: %i\n", depart);
-    // printf("Result: %i\n", result);
+    // printf("Bus: {}\n", take_bus);
+    // printf("Depart at: {}\n", depart);
+    // printf("Result: {}\n", result);
 
     // Part 2
     result := chinese_remainder_theorem(buses, rems); 
-    printf("Result: %l\n", result);
+    printf("Result: {}\n", result);
 }
index 4fff2311fff6a7969398c7b98e5728805c9906b5..78a16dc727377ff45f00178a5d6624b7554addaf 100644 (file)
@@ -125,5 +125,5 @@ main :: (args: [] cstr) {
        s: u64 = 0;
        for e: mem.entries do s += e.value;
 
-       printf("Sum: %l\n", s);
+       printf("Sum: {}\n", s);
 }
index 168ced9cba08a0714dd0d0e5e3b5f1903b3c4576..c5417d9bc9160270f3c78f58136c6bb214d61950 100644 (file)
@@ -40,5 +40,5 @@ main :: (args: [] cstr) {
         turn += 1;
     }
 
-    printf("2020th: %i\n", last_num);
+    printf("2020th: {}\n", last_num);
 }
index 5228289d7aebc2b41c32012607c71355b9f4bb1f..1742a204a749c178e88665ea43b9ce8a20dd2c59 100644 (file)
@@ -92,7 +92,7 @@ main :: (args: [] cstr) {
                }
        }
 
-       printf("Scanning error: %i\n", total_scanning_error);
+       printf("Scanning error: {}\n", total_scanning_error);
 
        cols_to_assign := array.make(u32, fields.count);
        defer array.free(^cols_to_assign);
@@ -134,5 +134,5 @@ main :: (args: [] cstr) {
                if string.starts_with(field.name, "departure") do prod *= ~~my_ticket[field.column];
        }
 
-       printf("Departure multiply: %l\n", prod);
+       printf("Departure multiply: {}\n", prod);
 }
index 9839aa0c6591d6ac3fe5bf92bd8d5ee8a24a1452..6c6825f2bca02f5b40c836e318b09bf35d233097 100644 (file)
@@ -100,5 +100,5 @@ main :: (args: [] cstr) {
     active_count := 0;
     for ^cube_entry: cubes.entries do if cube_entry.value.alive do active_count += 1;
 
-    printf("Active count: %i\n", active_count);
+    printf("Active count: {}\n", active_count);
 }
index 645121861c6fb0dd1ea134661f85148839cb6803..1d7981056ff9f432246052febb1b1eb879548a05 100644 (file)
@@ -73,5 +73,5 @@ main :: (args: [] cstr) {
                total += parse_expression_mul(^file);
        }
 
-       printf("Total: %l\n", total);
+       printf("Total: {}\n", total);
 }
index 928e1d72a4d6eb3368334ff4f7443801de83ef66..3327b85f436d74f47cdfc5ddb8967fe0bd51553a 100644 (file)
@@ -171,5 +171,5 @@ main :: (args: [] cstr) {
         if cyk_algorithm(^grammar, line) do valid_count += 1;
     }
 
-    printf("Valid count: %i\n", valid_count);
+    printf("Valid count: {}\n", valid_count);
 }
index 1731a9121d199f7ab9e9fc60baf1371b24fe67f6..fca7b94f481280e2437affaa062e1112c10f67fc 100644 (file)
@@ -35,5 +35,5 @@ main :: (args: [] cstr) {
         if count == 1 do valid += 1;
     }
 
-    printf("Number valid: %i\n", valid);
+    printf("Number valid: {}\n", valid);
 }
index dafa71cc65248a56200c7457f27c15555e8ea358..c616f27566c7f461acbbb736a96ce5ffb0b64f54 100644 (file)
@@ -313,7 +313,7 @@ main :: (args: [] cstr) {
         }
        }
 
-       printf("Corner product: %l\n", prod);
+       printf("Corner product: {}\n", prod);
 
     grid : [12 * 12] u32;
     memory.set(^grid, 0, sizeof [12 * 12] u32);
@@ -369,5 +369,5 @@ main :: (args: [] cstr) {
     safe_count := 0;
     for c: forest do if c == #char "#" do safe_count += 1;
 
-    printf("Safe count: %i\n", safe_count);
+    printf("Safe count: {}\n", safe_count);
 }
index 118eb6ce6775bafd81e1e045376dff8ad5a06a95..f10729d8df658962b2b2ce655365e8f273eeb32f 100644 (file)
@@ -129,7 +129,7 @@ main :: (args: [] cstr) {
         map.delete(^ingredient_map, safe);
     }
 
-    printf("Total safe: %i\n", total_safe);
+    printf("Total safe: {}\n", total_safe);
 
     matched_ingredients := array.make(Ingredient);
     defer array.free(^matched_ingredients);
@@ -166,8 +166,8 @@ main :: (args: [] cstr) {
         return string.compare(i1.allergen, i2.allergen);
     });
 
-    for ^mi: matched_ingredients do printf("%s -> %s\n", mi.name, mi.allergen);
-    for ^mi: matched_ingredients do printf("%s,", mi.name);
+    for ^mi: matched_ingredients do printf("{} -> {}\n", mi.name, mi.allergen);
+    for ^mi: matched_ingredients do printf("{},", mi.name);
     println("\n(Don't copy the last ','!)");
 }
 
index c1d99d90fad768ec2ab20393b2422ea750dcdbd8..616d62f4173ed1b3236dba98dc7cdf85d5bd9aa8 100644 (file)
@@ -134,7 +134,7 @@ main :: (args: [] cstr) {
         defer array.free(^player2_copy);
 
         combat_score := combat(^player1_copy, ^player2_copy);
-        printf("Answer: %i\n", combat_score);
+        printf("Answer: {}\n", combat_score);
     }
 
     {
@@ -152,6 +152,6 @@ main :: (args: [] cstr) {
         if winner == 1 do score = score_player(^player1_copy);
         if winner == 2 do score = score_player(^player2_copy);
 
-        printf("Recursive answer: %i\n", score);
+        printf("Recursive answer: {}\n", score);
     }
 }
index c40b3bb1822e1bd4874f29f4c335df6001f371b1..7f64f0cc2a1f7cd96755013232a2bf0af3651701 100644 (file)
@@ -76,12 +76,12 @@ main :: (args: [] cstr) {
     // Part 1
     // one_idx := get_idx(array.to_slice(^cups), 1);
     // for i: 1 .. 9 {
-    //     printf("%i", cast(i32) cups[w(one_idx + i)]);
+    //     printf("{}", cast(i32) cups[w(one_idx + i)]);
     // }
     // printf("\n");
 
     // Part 2
     one_idx := get_idx(array.to_slice(^cups), 1);
     prod: i64 = cast(i64) (cups[w(one_idx + 1)]) * cast(i64) (cups[w(one_idx + 2)]);
-    printf("Cup product: %l\n", prod);
+    printf("Cup product: {}\n", prod);
 }
index 4979884aff6a6ebbf3301b539e43d6f4072f3152..0e52d1ddef8c89beed57e1a5caeeb04286bc5920 100644 (file)
@@ -74,7 +74,7 @@ main :: (args: [] cstr) {
        for ^cell: grid.entries {
                if cell.value.alive do black_count += 1;
        }       
-       printf("Black count: %i\n", black_count);
+       printf("Black count: {}\n", black_count);
 
        // Part 2
        cells_to_consider := array.make(Vec2);
@@ -118,7 +118,7 @@ main :: (args: [] cstr) {
        for ^cell: grid.entries {
                if cell.value.alive do black_count += 1;
        }       
-       printf("GOL black count: %i\n", black_count);
+       printf("GOL black count: {}\n", black_count);
 }
 
 get_neighbor_count :: (grid: ^map.Map(Vec2, Cell), pos: Vec2) -> u32 {
index db11f1ece328807820eea9c158158c5cd082e06d..7806bede2e09f96ecb3e70fa9739fb8405846ba5 100644 (file)
@@ -67,5 +67,5 @@ main :: (args: [] cstr) {
     encryption_key_0 := transform_subject(door_pub_key, card_loop_secret);
     encryption_key_1 := transform_subject(card_pub_key, door_loop_secret);
 
-    printf("%i == %i\n", encryption_key_0, encryption_key_1);
+    printf("{} == {}\n", encryption_key_0, encryption_key_1);
 }
index 63a7553d10ce9465bcf01208532251f8f962692c..f494fe659d0fb4598eb3da60722457d496462afd 100644 (file)
@@ -58,5 +58,5 @@ main :: (args: [] cstr) {
         tree_prod *= tree_count;
     }
 
-    printf("Tree product: %l\n", tree_prod);
+    printf("Tree product: {}\n", tree_prod);
 }
index 94f6c3edaf65e3623187070da48c1229c6c03e89..6b70058a0d3870f05396d641b8b2969ba6b64276 100644 (file)
@@ -40,5 +40,5 @@ main :: (args: [] cstr) {
         if field_count == 7 do valid_passports += 1;
     }
 
-    printf("Valid passports: %i\n", valid_passports);
+    printf("Valid passports: {}\n", valid_passports);
 }
index 15be62981eb759a47af4cb7f6d730f0f217bd338..0fa916201c900e3f9b9323ad013eda08879ee45e 100644 (file)
@@ -31,8 +31,8 @@ main :: (args: [] cstr) {
         if vals[i + 1] - vals[i] != 1 do missing = vals[i] + 1;
     }
 
-    printf("Max val: %i\n", max_val);
-    printf("Your seat: %i\n", missing);
+    printf("Max val: {}\n", max_val);
+    printf("Your seat: {}\n", missing);
 }
 
 cmp_asc :: (a: $T, b: T) -> i32 do return ~~(a - b);
index 1d731702033c9b53d95ca54485af2ca41a44b609..a034bbca54d818a0cb717ecd841caf8ce0148ee0 100644 (file)
@@ -52,5 +52,5 @@ main :: (args: [] cstr) {
         unique_sum += unique;
     }
 
-    printf("Unique sum: %i\n", unique_sum);
+    printf("Unique sum: {}\n", unique_sum);
 }
index 8c06460b9c03f8d950eee72c6ed4d7538c161b60..2fb4ea3c9a81ca262c6e5eb86296f3b8955b5134 100644 (file)
@@ -106,13 +106,13 @@ main :: (args: [] cstr) {
     //     
     //     for container: bag.contained_in {
     //         if !array.contains(^processed_bags, container.bag) && !array.contains(^to_process_bags, container.bag) {
-    //             // printf("Adding %s to process.\n", container.bag.color);
+    //             // printf("Adding {} to process.\n", container.bag.color);
     //             array.push(^to_process_bags, container.bag);
     //         }
     //     }
     // }
 
-    // printf("Count: %i\n", processed_bags.count - 1);
+    // printf("Count: {}\n", processed_bags.count - 1);
     
     // Part 2
     to_process := array.make(#type ^BagNode);
@@ -132,5 +132,5 @@ main :: (args: [] cstr) {
         }
     }
 
-    printf("Count: %i\n", count - 1);
+    printf("Count: {}\n", count - 1);
 }
index 61728050650b45514f97ca1fff577fe82afa4a97..d121d690a78950cf69fa4c5242fb459bc68185a7 100644 (file)
@@ -81,5 +81,5 @@ main :: (args: [] cstr) {
         elseif instr.opcode == OpCode.Jmp do instr.opcode = OpCode.Nop;
     }
 
-    printf("Accumulator value: %i\n", acc);
+    printf("Accumulator value: {}\n", acc);
 }
index 8df60e34453aec801b9abb908a4b4fab16cd64a0..9bcd661fe8cb4391bd0cf270f5c448476376731a 100644 (file)
@@ -60,12 +60,12 @@ main :: (args: [] cstr) {
         }
     }
 
-    printf("Invalid number: %l\n", invalid);
+    printf("Invalid number: {}\n", invalid);
 
     start, end := find_contiguous_subarray_with_sum(nums, invalid);
 
     max := array.fold(nums.data[start .. end + 1], cast(u64) 0, math.max_poly);
     min := array.fold(nums.data[start .. end + 1], cast(u64) max, math.min_poly);
 
-    printf("Extrema sum: %l\n", min + max);
+    printf("Extrema sum: {}\n", min + max);
 }
index 1668b9cf56980e9d1e3564b1dc00dbbd87ce52f5..39d93bc0477a2b6ddabc50836e10b63a754f1951 100644 (file)
@@ -6,7 +6,7 @@ Vec2 :: struct { x: i32; y: i32; }
 
 // Overload print() to print Vec2's.
 #add_match io.write, (use writer: ^io.Writer, use v: Vec2) {
-    io.write_format(writer, "Vec2(%i, %i)", x, y);
+    io.write_format(writer, "Vec2({}, {})", x, y);
 }
 
 EntityStore :: struct {
index 461466a6ec17c4f550cc714a5df6444178bb343a..237afbc3890ec14495b4a103f95f6a541541f98b 100644 (file)
@@ -3,7 +3,7 @@
 use package core
 
 count_to :: ($N: i32) {
-    for i: 0 .. N do printf("%i ", i);
+    for i: 0 .. N do printf("{} ", i);
     print("\n");
 }
 
@@ -40,7 +40,7 @@ main :: (args: [] cstr) {
 
     print_age :: (ages: ^map.Map(str, u32), name: str) {
         age := map.get(ages, name);
-        printf("%s's age is %i.\n", name, age);
+        printf("{}'s age is {}.\n", name, age);
     }
 
     print_age(^ages, "Dwight");
index 3485f315fbb40b11f9a865fd8d7bf834d6f6392c..df0fe17f98bc6a936e452b1442b33a4badc5d1ff 100644 (file)
@@ -17,7 +17,7 @@ get_foo :: () -> Foo {
 }
 
 main :: (args: [] cstr) {
-    printf("%i\n", "Hello World!".count);
+    printf("{}\n", "Hello World!".count);
 
     println(get_foo().name);
     println(get_foo().x);
index 1c076ae77eeba53059c1dde34a9f0957f2f558e0..6594a51a61e3f997ae6a4ee9259a078b0ff8fb5b 100644 (file)
@@ -7,31 +7,31 @@ main :: (args: [] cstr) {
     i := 0;
     while i < 10 {
         defer i += 1;
-        defer printf("Index is %i\n", i);
+        defer printf("Index is {}\n", i);
 
         if i == 3 {
-            printf("Skipping %i!!\n", i);
+            printf("Skipping {}!!\n", i);
             continue;
         }
 
-        printf("Doing something with ");
+        print("Doing something with ");
     }
-    printf("i is %i\n", i);
+    printf("i is {}\n", i);
 
     println("\n\n===================================");
     i = 0;
     while i < 10 {
         defer i += 1;
-        defer printf("Index is %i\n", i);
+        defer printf("Index is {}\n", i);
 
         if i == 3 {
-            printf("Skipping %i!!\n", i);
+            printf("Skipping {}!!\n", i);
             break;
         }
 
-        printf("Doing something with ");
+        print("Doing something with ");
     }
-    printf("i is %i\n", i);
+    printf("i is {}\n", i);
 
     println("\n\n===================================");
     switch i {
index 4d98370229a6bfbb5eb53df17d64be34a3a2a5e8..f84a3c91ea635c6bcbb88c1e3ba9511db7779b77 100644 (file)
@@ -1,7 +1,7 @@
 12.0000
 12.0000
 8.0000
-12.3400
+12.3399
 0.3399
 2.0000
 1.0000
index 2a55f7767a661424ce9446d140ee91fd78c58396..05ab5d3a5a6ad654099431a3e168e0a6ec41e38c 100644 (file)
@@ -17,7 +17,7 @@ main :: (args: [] cstr) {
     println(map.has(^imap, 50));
     println(map.has(^imap, 51));
 
-    printf("%s%s\n", map.get(^imap, 50), map.get(^imap, 1234));
+    printf("{}{}\n", map.get(^imap, 50), map.get(^imap, 1234));
 
     map.delete(^imap, 50);
     println(map.has(^imap, 50));
index c069b3aba3cbb6b6bce93f3234ccefa9a9a2b8b9..b0f1abe46c00328c31e88c3db3f2b617705e6b8a 100644 (file)
@@ -67,6 +67,6 @@ main :: (args: [] cstr) {
                 |> iter.zip(iter.const(42.0f));
 
     for value: zipped_iterator {
-        printf("%i   %f\n", value.first, value.second);
+        printf("{}   {}\n", value.first, value.second);
     }
 }
index 0021d167666511be17b3609447127907592e8700..79c3a10d97803bd4eceb93574562d3f15ae98079 100644 (file)
@@ -41,7 +41,7 @@ main :: (args: [] cstr) {
 
 
     least, greatest := get_extrema(i32.[ -5, 20, 5, -10, 200, 8 ]);
-    printf("%i to %i\n", least, greatest);
+    printf("{} to {}\n", least, greatest);
 
     iarr, farr := return_multiple_arrays();
     array_print(iarr);
index 4f993236012ca57f460bdec7eff901d405150cbb..7b1bcc26b33c7420a5f13a163071b6add2d7b96e 100644 (file)
@@ -4,8 +4,8 @@ use package core
 
 main :: (args: [] cstr) {
     foo :: (x: i32, y: f32) {
-        printf("x is %i\n", x);
-        printf("y is %f\n", y);
+        printf("x is {}\n", x);
+        printf("y is {}\n", y);
     }
 
     foo(10, 20);
@@ -42,8 +42,8 @@ main :: (args: [] cstr) {
         println(x);
         println(y);
 
-        for elem: z do printf("%f ", elem);
-        printf("\n");
+        for elem: z do printf("{} ", elem);
+        print("\n");
     }
 
     overload_with_varargs(10, 20, 30, 40, 50);
@@ -61,7 +61,7 @@ main :: (args: [] cstr) {
         options_f := false,
         options_g := true,
         ) {
-        printf("%b %b %b %b %b %b %b\n",
+        printf("{} {} {} {} {} {} {}\n",
             options_a, options_b,
             options_c, options_d,
             options_e, options_f,
diff --git a/tests/new_printf b/tests/new_printf
new file mode 100644 (file)
index 0000000..3442358
--- /dev/null
@@ -0,0 +1 @@
+123 Test { } false 0x4E8
diff --git a/tests/new_printf.onyx b/tests/new_printf.onyx
new file mode 100644 (file)
index 0000000..c53077a
--- /dev/null
@@ -0,0 +1,7 @@
+#load "core/std"
+
+use package core
+
+main :: (args: [] cstr) {
+    printf("{} {} {{ }} {} {}\n", 123, "Test", false, ^stdio.print_writer);
+}
index 4f114fb150d48e4a5d403aaac9a519ade27842ca..c3094a2b7f2771686d68fdbbd82b585f4108346d 100644 (file)
@@ -66,7 +66,7 @@ main :: (args: [] cstr) {
     println(foo.some_constant);
     
     person := Foo2.SubStruct.{ name = "Joe", age = 30 };
-    printf("%s is %i.\n", person.name, person.age);
+    printf("{} is {}.\n", person.name, person.age);
 
     {
         BasicallyANamespace.function_3();
@@ -80,7 +80,7 @@ main :: (args: [] cstr) {
 
 
     v := Vec3.{ 4, 9, 16 };
-    printf("V3(%f, %f, %f)\n", v.x, v.y, v.z);
+    printf("V3({}, {}, {})\n", v.x, v.y, v.z);
 }
 
 BasicallyANamespace :: struct {
index d3c0eefb18109fff4fc72aac47c3dd247366a040..0fa1aaf0360f5f6a9dcdaabb1d1f4dd577754311 100644 (file)
@@ -68,24 +68,24 @@ main :: (args: [] cstr) {
         b := C(0, 1);
 
         c := a + b;
-        printf("(%f, %f)\n", c.re, c.im);
+        printf("({}, {})\n", c.re, c.im);
 
         c = a - b;
-        printf("(%f, %f)\n", c.re, c.im);
+        printf("({}, {})\n", c.re, c.im);
 
         c = a * b;
-        printf("(%f, %f)\n", c.re, c.im);
+        printf("({}, {})\n", c.re, c.im);
     }
 
     {
         a := make_vec(f32.[10, 20]);
         b := make_vec(f32.[3,  4]);
         c := a + b;
-        printf("(%f, %f)\n", c.data[0], c.data[1]);
+        printf("({}, {})\n", c.data[0], c.data[1]);
 
         d := make_vec(f32.[2, 3, 5, 7]);
         e := join(d, c);
-        for v: e.data do printf("%f ", v);
+        for v: e.data do printf("{} ", v);
         print("\n");
     }
 
@@ -93,7 +93,7 @@ main :: (args: [] cstr) {
         a := make_vec(Complex.[C(2, 3), C(0, 1)]);
         b := make_vec(Complex.[C(1, 0), C(3, 7)]);
         c := a * b;
-        printf("(%f, %f)\n", c.re, c.im);
+        printf("({}, {})\n", c.re, c.im);
     }
 
     println(test_overload("World!", "Hello!"));
index 7f88e2b306a7d6eb49be511689a947da910cf5ad..8ec8c4a33a2c3c3e895dcc08c2c2cb420308eaad 100644 (file)
@@ -4,7 +4,7 @@ use package core
 
 main :: (args: [] cstr) {
     arr := u32.[ 1, 2, 3, 4, 5 ];
-    for elem: array_to_slice(arr) do printf("%i ", elem);
+    for elem: array_to_slice(arr) do printf("{} ", elem);
 
     roots : [20] f32;
     compute_roots(roots);
index f84f7c8871f574a18d6b6b9a8abb01f71bf6fee6..55d5f974ec99ec087692fd3af684610266640104 100644 (file)
@@ -27,7 +27,7 @@ main :: (args: [] cstr) {
         ss: SimpleStruct = SimpleStruct.{ 41, 67, "Steve" };
 
         use ss;
-        printf("SimpleStruct<%i, %i>(%i, %i, %s)\n",
+        printf("SimpleStruct<{}, {}>({}, {}, {})\n",
             sizeof SimpleStruct,
             alignof SimpleStruct,
             cast(u32) age, height, name);
@@ -44,7 +44,7 @@ main :: (args: [] cstr) {
         u : SimpleUnion;
         u.float_val = 0.5;
 
-        printf("%p == 0x3F000000\n", u.int_val);
+        printf("{} == 0x3F000000\n", cast(rawptr) u.int_val);
     }
 
     test_default_values :: () {
@@ -64,7 +64,7 @@ main :: (args: [] cstr) {
         print_defaulted(ds2);
 
         print_defaulted :: (use ds: DefaultedStruct) {
-            printf("DefaultedStruct(%i, %f, %l, %d)\n", i, f, l, d);
+            printf("DefaultedStruct({}, {}, {}, {})\n", i, f, l, d);
         }
     }
 
@@ -96,7 +96,7 @@ main :: (args: [] cstr) {
         // swu3 := StructWithUse.{ 1234, 1, 2, 5678 };
 
         print_swu :: (use swu: StructWithUse) {
-            printf("StructWithUse(%i, (%f, %f), %i)\n",
+            printf("StructWithUse({}, ({}, {}), {})\n",
                 first_member, x, y, last_member);
         }
 
@@ -116,11 +116,11 @@ main :: (args: [] cstr) {
         ps1.t_data = 1234;
         ps1.r_data = 5678;
 
-        printf("PolyStruct<i32, f32>(%i, %f)\n", ps1.t_data, ps1.r_data);
+        printf("PolyStruct<i32, f32>({}, {})\n", ps1.t_data, ps1.r_data);
 
         // Currently, this is how you have to do this.
         ps2 := <PolyStruct(f32, i32)>.{ 1234, 5678 };
-        printf("PolyStruct<f32, i32>(%f, %i)\n", ps2.t_data, ps2.r_data);
+        printf("PolyStruct<f32, i32>({}, {})\n", ps2.t_data, ps2.r_data);
     }
 
     test_polymorphic_with_defaults :: () {
@@ -134,7 +134,7 @@ main :: (args: [] cstr) {
         PolyStructTyped :: <PolyStruct(i32, f32)>;
 
         ps := PolyStructTyped.{};
-        printf("PolyStruct<i32, f32>(%i, %f)\n", ps.t_data, ps.r_data);
+        printf("PolyStruct<i32, f32>({}, {})\n", ps.t_data, ps.r_data);
     }
 
     test_polymorphic_union_with_use :: () {
@@ -147,12 +147,12 @@ main :: (args: [] cstr) {
             };
         }
 
-        printf("%i == 16\n", sizeof PolyStruct(i32, [] u32));
+        printf("{} == 16\n", sizeof PolyStruct(i32, [] u32));
 
         ps : PolyStruct(i32, f64);
         ps.t_data = 1234;
         ps.r_data = 5678;
-        printf("%i, %d\n", ps.t_data, ps.r_data);
+        printf("{}, {}\n", ps.t_data, ps.r_data);
     }
 
     test_union_with_use :: () {
@@ -171,10 +171,10 @@ main :: (args: [] cstr) {
         u : Union;
         u.x = 10;
         u.y = 20;
-        printf("Union(%i, %i)\n", u.x, u.y);
+        printf("Union({}, {})\n", u.x, u.y);
 
         u.name = "Joe";
-        printf("Union(%s)\n", u.name);
+        printf("Union({})\n", u.name);
     }
 
     test_polymorphic_union :: () {
@@ -187,9 +187,9 @@ main :: (args: [] cstr) {
 
         pu : PolyUnion(str, i32);
         pu.t_data = "Hello World.";
-        printf("PolyUnion(%s)\n", pu.t_data);
+        printf("PolyUnion({})\n", pu.t_data);
 
         pu.r_data = 0x4D2;
-        printf("PolyUnion(%i)\n", pu.r_data);
+        printf("PolyUnion({})\n", pu.r_data);
     }
 }
index 7751a254eca874384eb22e8c646c8485a94c1f67..899930b582698dc702146f654442a9dd81ec2dfc 100644 (file)
@@ -23,5 +23,5 @@ main :: (args: [] cstr) {
     new_va_test("foo", 1, 2, 3.0f, 5.0f);
     old_va_test("bar", 1);
 
-    printf("Hello, %i, %s!\n", 1234, "World");
+    printf("Hello, {}, {}!\n", 1234, "World");
 }