added printing floats
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 23 Sep 2020 19:19:54 +0000 (14:19 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 23 Sep 2020 19:19:54 +0000 (14:19 -0500)
core/stdio.onyx
core/string.onyx
onyx
progs/poly_struct.onyx

index 5c63d7a81a65e004f02e8b6298634a73a9b41dfe..9b686ec46c104f62a1459a09422f8a338e5b3cfc 100644 (file)
@@ -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,
 }
index 88766ae527c68a97c0f9bf7ee46818e81a3406ac..8fcd42c659e1e8cf7883663ef99f10b9735ef830 100644 (file)
@@ -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 6ec963ca66d9048d0c3c010b9128d1ca1ddac8b8..70b0a6920e7fb5882647712aa03d017298cdd3bb 100755 (executable)
Binary files a/onyx and b/onyx differ
index e8a1ff448829a0ab8155a62a46823f190a6676e6..2c80fe29906a6fa47513e958b64b64c4bf554e6b 100644 (file)
@@ -7,7 +7,7 @@ use package core
 main :: proc (args: [] cstring) {
 
     x := 2.4f;
-    y := 1;
+    y := 1.2f;
     y += ~~x;
     println(y);