small optimizations and working on json library
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 15 Jun 2021 04:37:53 +0000 (23:37 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 15 Jun 2021 04:37:53 +0000 (23:37 -0500)
bin/onyx
modules/json/decoder.onyx
modules/json/module.onyx
modules/json/types.onyx
src/onyxtypes.c

index 6a272f27c86fe72d5a9b90f1b0453b939db09380..9c856b9f9d32fa1743467ca1b1db90735047a061 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index 8d6443edc8a6f736564fc773202bb1a53d102d48..003f04c631b54fdfa20ce7963c789aaf93b3ebc3 100644 (file)
@@ -1,11 +1,11 @@
 package json
 use package core
 
-decode_string :: (data: str, allocator := context.allocator) -> Value {
+decode_string :: (data: str, allocator := context.allocator) -> ^Value {
     stream := io.string_stream_make(data);
     return decode(^stream, allocator);
 }
 
-decode :: (stream: ^io.Stream, allocator := context.allocator) -> Value {
-
+decode :: (stream: ^io.Stream, allocator := context.allocator) -> ^Value {
+    
 }
\ No newline at end of file
index 6d8d8ceb1fbe01c689d6de413bbafeb5e65e2819..d8ce7fb969a9353179c019cd4c26c4676a4f15a4 100644 (file)
@@ -4,4 +4,5 @@
 package json
 
 #load "./encoder"
-#load "./decoder"
\ No newline at end of file
+#load "./decoder"
+#load "./types"
\ No newline at end of file
index cec32eff612dd8eecd75e77f9541ce9e73cb31b9..b4c4c72ea32535db486d4deda82f769af4edce1d 100644 (file)
@@ -1,10 +1,11 @@
 package json
+use package core
 
-use package core.map { Map }
+null_value : Value
 
 Value :: struct {
     Type :: enum {
-        Null;
+        Null :: 0x00;
         Bool;
         Integer;
         Float;
@@ -13,13 +14,55 @@ Value :: struct {
         Object;
     }
 
-    type  : Type;
-    value : struct #union {
+    KeyValue :: struct {
+        key : str;
+        value : ^Value;
+    }
+
+    type : Type;
+    allocator : Allocator;
+    use value : struct #union {
         bool_   : bool;
         int_    : i64;
         float_  : f64;
         str_    : str;
-        array_  : [..] Value;
-        object_ : Map(str, Value);
+
+        @CompilerBug @Cleanup // I don't want these to be pointers in the long run. In theory, they should be
+        // able to be normal values, but there is a compiler bug that prevents the size and alignment of Value
+        // being known.
+        array_  : [..] ^Value;
+        object_ : [..] KeyValue;
+    };
+}
+
+is_null :: (v: ^Value) -> bool {
+    if v == null do return true;
+    return v == ^null_value || v.type == .Null;
+}
+
+to_str :: (v: ^Value) -> str do return null_str;
+
+get :: (v: ^Value, key: str) -> ^Value {
+    if v.type != .Object do return ^null_value;
+
+    for ^entry: v.object_ {
+        if entry.key == key do return entry.value;
+    }
+    return ^null_value;
+}
+
+free :: (v: ^Value) do switch v.type {
+    case .Array {
+        for elem: v.array_ {
+            free(elem);
+        }
+        array.free(^v.array_);
+    }
+
+    case .Object {
+        for ^entry: v.object_ {
+            free(entry.value);
+        }
+        array.free(^v.object_);
     }
-}
\ No newline at end of file
+}
index 3ad513ee891c6baf60734774e02b798c0b96f037..ae2b26dae0c9b3659b1e7b54d04699ac1d77da2b 100644 (file)
@@ -300,15 +300,18 @@ Type* type_build_from_ast(bh_allocator alloc, AstType* type_node) {
                 s_type->Struct.unique_id = next_unique_id++;
                 s_type->Struct.mem_count = bh_arr_length(s_node->members);
 
+                s_type->Struct.memarr = NULL;
+                bh_table_init(global_heap_allocator, s_type->Struct.members, s_type->Struct.mem_count + 1);
+                bh_arr_new(global_heap_allocator, s_type->Struct.memarr, s_type->Struct.mem_count);
+
             } else {
                 s_type = s_node->stcache;
             }
 
-            s_type->Struct.memarr = NULL;
             s_type->Struct.poly_sln = NULL;
 
-            bh_table_init(global_heap_allocator, s_type->Struct.members, s_type->Struct.mem_count + 1);
-            bh_arr_new(global_heap_allocator, s_type->Struct.memarr, s_type->Struct.mem_count);
+            bh_arr_clear(s_type->Struct.memarr);
+            bh_table_clear(s_type->Struct.members);
 
             s_node->stcache_is_valid = 1;