From f7709bb64e5afa8f6eb240ab146a772a0365fdfd Mon Sep 17 00:00:00 2001 From: Ed_ Date: Wed, 4 Dec 2024 11:30:54 -0500 Subject: [PATCH] more progress --- gen_c_library/c_library.cpp | 4 +- gen_c_library/components/containers.array.hpp | 4 +- .../components/containers.hashtable.hpp | 4 +- project/bootstrap.cpp | 2 +- project/components/ast.cpp | 4 +- project/components/interface.cpp | 2 +- project/components/interface.upfront.cpp | 10 +-- project/components/lexer.cpp | 2 +- project/components/parser.cpp | 12 +-- project/dependencies/memory.cpp | 14 +-- project/dependencies/memory.hpp | 12 ++- project/dependencies/strings.hpp | 90 +++++++++---------- project/helpers/helper.hpp | 22 ++--- 13 files changed, 95 insertions(+), 87 deletions(-) diff --git a/gen_c_library/c_library.cpp b/gen_c_library/c_library.cpp index 4ff9726..3389bc7 100644 --- a/gen_c_library/c_library.cpp +++ b/gen_c_library/c_library.cpp @@ -53,7 +53,7 @@ constexpr StrC implementation_guard_end = txt(R"( void format_file( char const* path ) { - String resolved_path = String::make(GlobalAllocator, to_str(path)); + String resolved_path = String::make(GlobalAllocator, to_strc_from_c_str(path)); String style_arg = String::make(GlobalAllocator, txt("-style=file:")); style_arg.append("../scripts/.clang-format "); @@ -289,7 +289,7 @@ int gen_main() break; case CT_Variable: { - if (contains(entry->Name, txt("Msg_Invalid_Value"))) + if ( strc_contains(entry->Name, txt("Msg_Invalid_Value"))) { CodeDefine define = def_define(entry->Name, entry->Value->Content); printing.append(define); diff --git a/gen_c_library/components/containers.array.hpp b/gen_c_library/components/containers.array.hpp index c374258..cfe2698 100644 --- a/gen_c_library/components/containers.array.hpp +++ b/gen_c_library/components/containers.array.hpp @@ -283,11 +283,11 @@ CodeBody gen_array( StrC type, StrC array_name ) #pragma pop_macro( "GEN_ASSERT" ) return def_global_body( args( - def_pragma( to_str( str_fmt_buf( "region %S", array_type ))), + def_pragma( string_to_strc( string_fmt_buf( GlobalAllocator, "region %S", array_type ))), fmt_newline, result, fmt_newline, - def_pragma( to_str( str_fmt_buf( "endregion %S", array_type ))), + def_pragma( string_to_strc(string_fmt_buf( GlobalAllocator, "endregion %S", array_type ))), fmt_newline )); }; diff --git a/gen_c_library/components/containers.hashtable.hpp b/gen_c_library/components/containers.hashtable.hpp index 6c460a3..c8ef7fc 100644 --- a/gen_c_library/components/containers.hashtable.hpp +++ b/gen_c_library/components/containers.hashtable.hpp @@ -341,14 +341,14 @@ CodeBody gen_hashtable( StrC type, StrC hashtable_name ) , type.Len, type.Ptr ); return def_global_body(args( - def_pragma( to_str( str_fmt_buf( "region %S", tbl_type ))), + def_pragma( string_to_strc( string_fmt_buf( GlobalAllocator, "region %S", tbl_type ))), fmt_newline, hashtable_types, fmt_newline, entry_array, hashtable_def, fmt_newline, - def_pragma( to_str( str_fmt_buf( "endregion %S", tbl_type ))), + def_pragma( string_to_strc( string_fmt_buf( GlobalAllocator, "endregion %S", tbl_type ))), fmt_newline )); } diff --git a/project/bootstrap.cpp b/project/bootstrap.cpp index 7c24fe7..c9b55ce 100644 --- a/project/bootstrap.cpp +++ b/project/bootstrap.cpp @@ -145,7 +145,7 @@ int gen_main() def_include(txt("components/types.hpp")), preprocess_endif, fmt_newline, - untyped_str( strc_to_str(generation_notice) ) + untyped_str( to_strc_from_c_str(generation_notice) ) )); // gen.hpp diff --git a/project/components/ast.cpp b/project/components/ast.cpp index ca3d40f..f101ec4 100644 --- a/project/components/ast.cpp +++ b/project/components/ast.cpp @@ -265,7 +265,7 @@ char const* debug_str(Code self) case CT_Specifiers: { string_append_fmt( result, "\n\tNumEntries: %d", self->NumEntries ); - string_append_c_str( result, "\n\tArrSpecs: " ); + string_append_strc( result, txt("\n\tArrSpecs: ") ); s32 idx = 0; s32 left = self->NumEntries; @@ -387,7 +387,7 @@ void to_string( Code self, String* result ) #ifdef GEN_DONT_ALLOW_INVALID_CODE log_failure("Attempted to serialize invalid code! - %S", Parent ? Parent->debug_str() : Name ); #else - append_fmt( result, "Invalid Code!" ); + string_append_fmt( result, "Invalid Code!" ); #endif break; diff --git a/project/components/interface.cpp b/project/components/interface.cpp index 79c329d..99d0344 100644 --- a/project/components/interface.cpp +++ b/project/components/interface.cpp @@ -351,7 +351,7 @@ void reset() do { Pool* code_pool = & CodePools[index]; - clear(* code_pool); + pool_clear(code_pool); index++; } while ( left--, left ); diff --git a/project/components/interface.upfront.cpp b/project/components/interface.upfront.cpp index fe510b6..49bfe81 100644 --- a/project/components/interface.upfront.cpp +++ b/project/components/interface.upfront.cpp @@ -907,14 +907,14 @@ CodeInclude def_include( StrC path, Opts_def_include p ) return InvalidCode; } - StrC content = p.foreign ? - string_to_strc( str_fmt_buf( "<%.*s>", path.Len, path.Ptr )) - : string_to_strc( str_fmt_buf( "\"%.*s\"", path.Len, path.Ptr )); + String content = p.foreign ? + string_fmt_buf( GlobalAllocator, "<%.*s>", path.Len, path.Ptr ) + : string_fmt_buf( GlobalAllocator, "\"%.*s\"", path.Len, path.Ptr ); Code result = make_code(); result->Type = CT_Preprocess_Include; - result->Name = get_cached_string( content ); + result->Name = get_cached_string( string_to_strc(content) ); result->Content = result->Name; return (CodeInclude) result; @@ -938,7 +938,7 @@ CodeNS def_namespace( StrC name, Code body, Opts_def_namespace p ) { name_check( def_namespace, name ); null_check( def_namespace, body); - + if ( body && body->Type != CT_Namespace_Body && body->Type != CT_Untyped ) { log_failure("gen::def_namespace: body is not of namespace or untyped type %s", debug_str(body)); diff --git a/project/components/lexer.cpp b/project/components/lexer.cpp index 04948ac..d4bb9b2 100644 --- a/project/components/lexer.cpp +++ b/project/components/lexer.cpp @@ -96,7 +96,7 @@ String to_string(Token tok) StrC type_str = to_str( tok.Type ); - append_fmt( & result, "Line: %d Column: %d, Type: %.*s Content: %.*s" + string_append_fmt( & result, "Line: %d Column: %d, Type: %.*s Content: %.*s" , tok.Line, tok.Column , type_str.Len, type_str.Ptr , tok.Length, tok.Text diff --git a/project/components/parser.cpp b/project/components/parser.cpp index ff38920..c06ccfa 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -365,7 +365,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) string_append_c_str_len( & content, cut_ptr, cut_length); if ( * string_back( content ) != ' ' ) - string_append_strc( & content, txt(' ')); + string_append_char( & content, ' ' ); move_fwd(); last_cut = sptr(scanner) - sptr(raw_text.Ptr); @@ -391,7 +391,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) // Replace with a space if ( * string_back( content ) != ' ' ) - string_append_strc( & content, txt(' ') ); + string_append_char( & content, ' ' ); scanner += 2; tokleft -= 2; @@ -417,8 +417,8 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) string_append_c_str_len( & content, cut_ptr, cut_length ); // Replace with a space - if ( * back( & content ) != ' ' ) - string_append_strc( & content, txt(' ') ); + if ( * string_back( content ) != ' ' ) + string_append_char( & content, ' ' ); move_fwd(); @@ -466,9 +466,9 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); // Preserve only 1 space of formattting - char* last = back(& content); + char* last = string_back(content); if ( last == nullptr || * last != ' ' ) - string_append_strc( & content, txt(' ') ); + string_append_char( & content, ' ' ); continue; } diff --git a/project/dependencies/memory.cpp b/project/dependencies/memory.cpp index 09a9023..33a7cf3 100644 --- a/project/dependencies/memory.cpp +++ b/project/dependencies/memory.cpp @@ -5,6 +5,8 @@ #pragma region Memory +GEN_API_C_BEGIN + void* mem_copy( void* dest, void const* source, ssize n ) { if ( dest == NULL ) @@ -495,16 +497,16 @@ Pool pool_init_align( AllocatorInfo backing, ssize num_blocks, ssize block_size, return pool; } -void clear(Pool& pool) +void pool_clear(Pool* pool) { ssize actual_block_size, block_index; void* curr; uptr* end; - actual_block_size = pool.BlockSize + pool.BlockAlign; + actual_block_size = pool->BlockSize + pool->BlockAlign; - curr = pool.PhysicalStart; - for ( block_index = 0; block_index < pool.NumBlocks - 1; block_index++ ) + curr = pool->PhysicalStart; + for ( block_index = 0; block_index < pool->NumBlocks - 1; block_index++ ) { uptr* next = ( uptr* ) curr; *next = ( uptr ) curr + actual_block_size; @@ -514,7 +516,9 @@ void clear(Pool& pool) end = ( uptr* ) curr; *end = ( uptr ) NULL; - pool.FreeList = pool.PhysicalStart; + pool->FreeList = pool->PhysicalStart; } +GEN_API_C_END + #pragma endregion Memory diff --git a/project/dependencies/memory.hpp b/project/dependencies/memory.hpp index a69b5bf..92bacce 100644 --- a/project/dependencies/memory.hpp +++ b/project/dependencies/memory.hpp @@ -222,6 +222,7 @@ struct Arena_Def }; #if GEN_SUPPORT_CPP_REFERENCES +GEN_API_C_END forceinline AllocatorInfo allocator_info(Arena& arena ) { return arena_allocator_info(& arena); } forceinline Arena init_sub (Arena& parent, ssize size) { return arena_init_sub( & parent, size); } forceinline ssize alignment_of (Arena& arena, ssize alignment) { return arena_alignment_of( & arena, alignment); } @@ -231,8 +232,9 @@ forceinline ssize size_remaining(Arena& arena, ssize alignment) { return // This id is defined by Unreal for asserts #pragma push_macro("check") #undef check -forceinline void check(Arena& arena) { return check(& arena); }; +forceinline void check(Arena& arena) { return arena_check(& arena); }; #pragma pop_macro("check") +GEN_API_C_BEGIN #endif @@ -397,9 +399,11 @@ void pool_clear(Pool* pool); void pool_free(Pool* pool); #if GEN_SUPPORT_CPP_REFERENCES -AllocatorInfo allocator_info(Pool& pool) { return GEN_NS allocator_info(& pool); } -void clear(Pool& pool) { return GEN_NS clear(& pool); } -void free(Pool& pool) { return GEN_NS free(& pool); } +GEN_API_C_END +AllocatorInfo allocator_info(Pool& pool) { return pool_allocator_info(& pool); } +void clear(Pool& pool) { return pool_clear(& pool); } +void free(Pool& pool) { return pool_free(& pool); } +GEN_API_C_BEGIN #endif struct Pool_Def diff --git a/project/dependencies/strings.hpp b/project/dependencies/strings.hpp index b02437c..57273a4 100644 --- a/project/dependencies/strings.hpp +++ b/project/dependencies/strings.hpp @@ -170,7 +170,7 @@ struct String forceinline operator char*() { return Data; } forceinline operator char const*() const { return Data; } - forceinline operator StrC() const { return { GEN_NS length(* this), Data }; } + forceinline operator StrC() const { return { string_length(* this), Data }; } String const& operator=(String const& other) const { if (this == &other) @@ -191,23 +191,23 @@ struct String friend forceinline bool operator!=(std::nullptr_t, const String str) { return str.Data != nullptr; } forceinline char* begin() const { return Data; } - forceinline char* end() const { return Data + GEN_NS length(* this); } + forceinline char* end() const { return Data + string_length(* this); } #pragma region Member Mapping - forceinline static String make(AllocatorInfo allocator, char const* str) { return GEN_NS string_make(allocator, str); } - forceinline static String make(AllocatorInfo allocator, StrC str) { return GEN_NS string_make(allocator, str); } - forceinline static String make_reserve(AllocatorInfo allocator, ssize cap) { return GEN_NS string_make_reserve(allocator, cap); } - forceinline static String make_length(AllocatorInfo a, char const* s, ssize l) { return GEN_NS string_make_length(a, s, l); } - forceinline static String join(AllocatorInfo a, char const** p, ssize n, char const* g) { return GEN_NS string_join(a, p, n, g); } - forceinline static usize grow_formula(usize value) { return GEN_NS string_grow_formula(value); } + forceinline static String make(AllocatorInfo allocator, char const* str) { return string_make_c_str(allocator, str); } + forceinline static String make(AllocatorInfo allocator, StrC str) { return string_make_strc(allocator, str); } + forceinline static String make_reserve(AllocatorInfo allocator, ssize cap) { return string_make_reserve(allocator, cap); } + forceinline static String make_length(AllocatorInfo a, char const* s, ssize l) { return string_make_length(a, s, l); } + forceinline static String join(AllocatorInfo a, char const** p, ssize n, char const* g) { return string_join(a, p, n, g); } + forceinline static usize grow_formula(usize value) { return string_grow_formula(value); } static String fmt(AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...) { va_list va; va_start(va, fmt); - str_fmt_va(buf, buf_size, fmt, va); + ssize res = str_fmt_va(buf, buf_size, fmt, va); va_end(va); - return GEN_NS string_make(allocator, buf); + return string_make_length(allocator, buf, res); } static @@ -216,37 +216,37 @@ struct String char buf[GEN_PRINTF_MAXLEN] = { 0 }; va_list va; va_start(va, fmt); - str_fmt_va(buf, GEN_PRINTF_MAXLEN, fmt, va); + ssize res = str_fmt_va(buf, GEN_PRINTF_MAXLEN, fmt, va); va_end(va); - return GEN_NS string_make(allocator, buf); + return string_make_length(allocator, buf, res); } - forceinline bool make_space_for(char const* str, ssize add_len) { return GEN_NS make_space_for(this, str, add_len); } - forceinline bool append(char c) { return GEN_NS string_append_char(this, c); } - forceinline bool append(char const* str) { return GEN_NS string_append_c_str(this, str); } - forceinline bool append(char const* str, ssize length) { return GEN_NS string_append_c_str_len(this, str, length); } - forceinline bool append(StrC str) { return GEN_NS string_append_strc(this, str); } - forceinline bool append(const String other) { return GEN_NS string_append_string(this, other); } - forceinline ssize avail_space() const { return GEN_NS string_avail_space(* this); } - forceinline char* back() { return GEN_NS string_back(* this); } - forceinline bool contains(StrC substring) const { return GEN_NS string_contains_strc(* this, substring); } - forceinline bool contains(String const& substring) const { return GEN_NS string_contains_string(* this, substring); } - forceinline ssize capacity() const { return GEN_NS capacity(* this); } - forceinline void clear() { GEN_NS clear(* this); } - forceinline String duplicate(AllocatorInfo allocator) const { return GEN_NS duplicate(* this, allocator); } - forceinline void free() { GEN_NS free(this); } - forceinline bool is_equal(String const& other) const { return GEN_NS are_equal(* this, other); } - forceinline bool is_equal(StrC other) const { return GEN_NS are_equal(* this, other); } - forceinline ssize length() const { return GEN_NS length(* this); } - forceinline b32 starts_with(StrC substring) const { return GEN_NS starts_with(* this, substring); } - forceinline b32 starts_with(String substring) const { return GEN_NS starts_with(* this, substring); } - forceinline void skip_line() { GEN_NS skip_line(* this); } - forceinline void strip_space() { GEN_NS strip_space(* this); } - forceinline StrC to_strc() { return { length(), Data}; } - forceinline void trim(char const* cut_set) { GEN_NS trim(* this, cut_set); } - forceinline void trim_space() { GEN_NS trim_space(* this); } - forceinline String visualize_whitespace() const { return GEN_NS visualize_whitespace(* this); } - forceinline StringHeader& get_header() { return * GEN_NS get_header(* this); } + forceinline bool make_space_for(char const* str, ssize add_len) { return string_make_space_for(this, str, add_len); } + forceinline bool append(char c) { return string_append_char(this, c); } + forceinline bool append(char const* str) { return string_append_c_str(this, str); } + forceinline bool append(char const* str, ssize length) { return string_append_c_str_len(this, str, length); } + forceinline bool append(StrC str) { return string_append_strc(this, str); } + forceinline bool append(const String other) { return string_append_string(this, other); } + forceinline ssize avail_space() const { return string_avail_space(* this); } + forceinline char* back() { return string_back(* this); } + forceinline bool contains(StrC substring) const { return string_contains_strc(* this, substring); } + forceinline bool contains(String const& substring) const { return string_contains_string(* this, substring); } + forceinline ssize capacity() const { return string_capacity(* this); } + forceinline void clear() { string_clear(* this); } + forceinline String duplicate(AllocatorInfo allocator) const { return string_duplicate(* this, allocator); } + forceinline void free() { string_free(this); } + forceinline bool is_equal(String const& other) const { return string_are_equal(* this, other); } + forceinline bool is_equal(StrC other) const { return string_are_equal_strc(* this, other); } + forceinline ssize length() const { return string_length(* this); } + forceinline b32 starts_with(StrC substring) const { return string_starts_with_strc(* this, substring); } + forceinline b32 starts_with(String substring) const { return string_starts_with_string(* this, substring); } + forceinline void skip_line() { string_skip_line(* this); } + forceinline void strip_space() { string_strip_space(* this); } + forceinline StrC to_strc() { return { string_length(*this), Data}; } + forceinline void trim(char const* cut_set) { string_trim(* this, cut_set); } + forceinline void trim_space() { string_trim_space(* this); } + forceinline String visualize_whitespace() const { return string_visualize_whitespace(* this); } + forceinline StringHeader& get_header() { return * string_get_header(* this); } bool append_fmt(char const* fmt, ...) { ssize res; @@ -257,7 +257,7 @@ struct String res = str_fmt_va(buf, count_of(buf) - 1, fmt, va) - 1; va_end(va); - return GEN_NS append(this, buf, res); + return string_append_c_str_len(this, buf, res); } #pragma endregion Member Mapping }; @@ -360,7 +360,7 @@ bool string_append_c_str(String* str, char const* str_to_append) { } inline -bool string_append_str_c_len(String* str, char const* str_to_append, ssize append_length) +bool string_append_c_str_len(String* str, char const* str_to_append, ssize append_length) { GEN_ASSERT(str != nullptr); if (sptr(str_to_append) > 0) @@ -394,7 +394,7 @@ bool string_append_string(String* str, String const other) { return string_append_c_str_len(str, (char const*)other, string_length(other)); } -bool append_fmt(String* str, char const* fmt, ...) { +bool string_append_fmt(String* str, char const* fmt, ...) { GEN_ASSERT(str != nullptr); ssize res; char buf[GEN_PRINTF_MAXLEN] = { 0 }; @@ -434,18 +434,18 @@ bool string_are_equal_strc(String const lhs, StrC rhs) } forceinline -ssize avail_space(String const str) { +ssize string_avail_space(String const str) { StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); return header->Capacity - header->Length; } forceinline -char* back(String* str) { - return & (*str)[string_length(* str) - 1]; +char* string_back(String str) { + return & (str)[string_length(str) - 1]; } inline -bool contains(String const str, StrC substring) +bool string_contains_StrC(String const str, StrC substring) { StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); diff --git a/project/helpers/helper.hpp b/project/helpers/helper.hpp index 314fcc8..c847fa8 100644 --- a/project/helpers/helper.hpp +++ b/project/helpers/helper.hpp @@ -27,8 +27,8 @@ CodeBody gen_ecode( char const* path ) { char const* code = node->string; - append_fmt( & enum_entries, "CT_%s,\n", code ); - append_fmt( & to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", code, code ); + string_append_fmt( & enum_entries, "CT_%s,\n", code ); + string_append_fmt( & to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", code, code ); } CodeEnum enum_code = parse_enum(gen::token_fmt_impl((3 + 1) / 2, "entries", string_to_strc(enum_entries), @@ -79,8 +79,8 @@ CodeBody gen_eoperator( char const* path ) char const* enum_str = enum_strs[idx].string; char const* entry_to_str = str_strs [idx].string; - append_fmt( & enum_entries, "Op_%s,\n", enum_str ); - append_fmt( & to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); + string_append_fmt( & enum_entries, "Op_%s,\n", enum_str ); + string_append_fmt( & to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); } CodeEnum enum_code = parse_enum(token_fmt("entries", string_to_strc(enum_entries), stringize( @@ -135,8 +135,8 @@ CodeBody gen_especifier( char const* path ) char const* enum_str = enum_strs[idx].string; char const* entry_to_str = str_strs [idx].string; - append_fmt( & enum_entries, "Spec_%s,\n", enum_str ); - append_fmt( & to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); + string_append_fmt( & enum_entries, "Spec_%s,\n", enum_str ); + string_append_fmt( & to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); } CodeEnum enum_code = parse_enum(token_fmt("entries", string_to_strc(enum_entries), stringize( @@ -252,8 +252,8 @@ CodeBody gen_etoktype( char const* etok_path, char const* attr_path ) char const* enum_str = enum_strs[idx].string; char const* entry_to_str = enum_str_strs [idx].string; - append_fmt( & enum_entries, "Tok_%s,\n", enum_str ); - append_fmt( & to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); + string_append_fmt( & enum_entries, "Tok_%s,\n", enum_str ); + string_append_fmt( & to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); } for ( usize idx = 0; idx < array_num(attribute_strs); idx++ ) @@ -261,9 +261,9 @@ CodeBody gen_etoktype( char const* etok_path, char const* attr_path ) char const* attribute_str = attribute_strs[idx].string; char const* entry_to_str = attribute_str_strs [idx].string; - append_fmt( & attribute_entries, "Tok_Attribute_%s,\n", attribute_str ); - append_fmt( & to_str_attributes, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); - append_fmt( & attribute_define_entries, "Entry( Tok_Attribute_%s, \"%s\" )", attribute_str, entry_to_str ); + string_append_fmt( & attribute_entries, "Tok_Attribute_%s,\n", attribute_str ); + string_append_fmt( & to_str_attributes, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); + string_append_fmt( & attribute_define_entries, "Entry( Tok_Attribute_%s, \"%s\" )", attribute_str, entry_to_str ); if ( idx < array_num(attribute_strs) - 1 ) string_append_strc( & attribute_define_entries, txt(" \\\n"));