From: Brendan Hansen Date: Tue, 15 Jun 2021 04:37:53 +0000 (-0500) Subject: small optimizations and working on json library X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=b558462831a778464c33f4192def8ebcfe3309c5;p=onyx.git small optimizations and working on json library --- diff --git a/bin/onyx b/bin/onyx index 6a272f27..9c856b9f 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/modules/json/decoder.onyx b/modules/json/decoder.onyx index 8d6443ed..003f04c6 100644 --- a/modules/json/decoder.onyx +++ b/modules/json/decoder.onyx @@ -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 diff --git a/modules/json/module.onyx b/modules/json/module.onyx index 6d8d8ceb..d8ce7fb9 100644 --- a/modules/json/module.onyx +++ b/modules/json/module.onyx @@ -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 diff --git a/modules/json/types.onyx b/modules/json/types.onyx index cec32eff..b4c4c72e 100644 --- a/modules/json/types.onyx +++ b/modules/json/types.onyx @@ -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 +} diff --git a/src/onyxtypes.c b/src/onyxtypes.c index 3ad513ee..ae2b26da 100644 --- a/src/onyxtypes.c +++ b/src/onyxtypes.c @@ -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;