From: Brendan Hansen Date: Wed, 23 Sep 2020 19:19:54 +0000 (-0500) Subject: added printing floats X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=4e490b87e2f749c849fd8d3ff74273562153a998;p=onyx.git added printing floats --- diff --git a/core/stdio.onyx b/core/stdio.onyx index 5c63d7a8..9b686ec4 100644 --- a/core/stdio.onyx +++ b/core/stdio.onyx @@ -18,6 +18,8 @@ print_string :: proc (s: string) { print_cstring :: proc (s: cstring) do string_builder_append(^print_buffer, s); print_i64 :: proc (n: i64, base := 10l) do string_builder_append(^print_buffer, n, base); print_i32 :: proc (n: i32, base := 10) do string_builder_append(^print_buffer, cast(i64) n, cast(u64) base); +print_f64 :: proc (n: f64) do string_builder_append(^print_buffer, n); +print_f32 :: proc (n: f32) do string_builder_append(^print_buffer, cast(f64) n); print_bool :: proc (b: bool) do string_builder_append(^print_buffer, b); print_ptr :: proc (p: ^void) do string_builder_append(^print_buffer, cast(i64) p, 16l); @@ -32,6 +34,7 @@ print_range :: proc (r: range, sep := " ") { print :: proc { print_string, print_cstring, print_i64, print_i32, + print_f64, print_f32, print_bool, print_ptr, print_range, } diff --git a/core/string.onyx b/core/string.onyx index 88766ae5..8fcd42c6 100644 --- a/core/string.onyx +++ b/core/string.onyx @@ -80,10 +80,10 @@ string_contains :: proc (str: string, c: u8) -> bool { // string_compare :: proc (str1: string, str2: string) -> i32 { // if str1.count != str2.count do return str1.count - str2.count; -// +// // i := 0; // while i < str1.count && str1[i] == str2[i] do i += 1; -// +// // if i == str1.count do return 0; // return ~~(str1[i] - str2[i]); // } @@ -183,10 +183,7 @@ i64_to_string :: proc (n_: i64, base: u64, buf: Buffer) -> string { n = cast(u64) -n_; } - str :: cast(^u8) buf.data; - for i: 0 .. buf.count do str[i] = #char "\0"; - - c := cast(^u8) ^str[buf.count - 1]; + c := ^(cast(^u8) buf.data)[buf.count - 1]; len := 0; s :: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"; @@ -233,12 +230,40 @@ i64_to_string :: proc (n_: i64, base: u64, buf: Buffer) -> string { return string.{ data = c + 1, count = len }; } +// NOTE: This is a big hack but it will work for now +f64_to_string :: proc (f: f64, buf: [] u8) -> string { + a := f; + a *= 10000.0; + v := cast(i64) a; + + b := Buffer.{ cast(^void) buf.data, buf.count }; + len := 0; + + s1 := i64_to_string(v / 10000l, 10l, b); + for i: 0 .. s1.count do buf.data[i] = s1.data[i]; + buf.data[s1.count] = #char "."; + len = s1.count + 1; + + if v < ~~0 do v = -v; + s2 := i64_to_string(v % 10000l, 10l, b); + for i: 0 .. s2.count do buf.data[s1.count + 1 + i] = s2.data[i]; + len += s2.count; + + return string.{ buf.data, len }; +} + string_builder_add_i64 :: proc (use sb: ^StringBuilder, n: i64, base := 10l) -> ^StringBuilder { buf : [256] u8; s := i64_to_string(n, base, Buffer.{ cast(^void) buf, 256 }); return string_builder_add_string(sb, s); } +string_builder_add_f64 :: proc (use sb: ^StringBuilder, f: f64) -> ^StringBuilder { + buf : [256] u8; + s := f64_to_string(f, buf[0 .. 256]); + return string_builder_add_string(sb, s); +} + string_builder_add_bool :: proc (use sb: ^StringBuilder, b: bool) -> ^StringBuilder { if b { return string_builder_add_string(sb, "true"); @@ -253,6 +278,7 @@ string_builder_append :: proc { string_builder_add_string, string_builder_add_cstring, string_builder_add_i64, + string_builder_add_f64, string_builder_add_bool, } diff --git a/onyx b/onyx index 6ec963ca..70b0a692 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/poly_struct.onyx b/progs/poly_struct.onyx index e8a1ff44..2c80fe29 100644 --- a/progs/poly_struct.onyx +++ b/progs/poly_struct.onyx @@ -7,7 +7,7 @@ use package core main :: proc (args: [] cstring) { x := 2.4f; - y := 1; + y := 1.2f; y += ~~x; println(y);