From a3e7ec4c72027e28d7ebe6c70d1ff6fdf006f227 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Thu, 5 Dec 2024 00:40:51 -0500 Subject: [PATCH] successful compile of c_library for: platform, macros, basic_types, debug, and memory headers (and newly generated c-code) --- gen_c_library/c_library.cpp | 20 +- .../components/memory.fixed_arena.hpp | 50 +++-- gen_c_library/components/misc.hpp | 31 +-- project/auxillary/builder.cpp | 2 +- project/components/code_serialization.cpp | 6 +- project/components/interface.cpp | 36 ++-- project/components/interface.untyped.cpp | 18 +- project/components/interface.upfront.cpp | 6 +- project/components/lexer.cpp | 95 ++++++-- project/components/parser.cpp | 46 ++-- project/dependencies/basic_types.hpp | 4 +- project/dependencies/containers.hpp | 204 ++++++++++-------- project/dependencies/filesystem.cpp | 2 +- project/dependencies/macros.hpp | 22 +- project/dependencies/memory.hpp | 20 +- project/dependencies/platform.hpp | 4 + project/dependencies/strings.hpp | 8 +- project/gen.cpp | 81 +++++++ project/gen.hpp | 38 ++++ .../helpers/push_container_defines.inline.hpp | 26 +++ 20 files changed, 471 insertions(+), 248 deletions(-) create mode 100644 project/helpers/push_container_defines.inline.hpp diff --git a/gen_c_library/c_library.cpp b/gen_c_library/c_library.cpp index f07659a..698b298 100644 --- a/gen_c_library/c_library.cpp +++ b/gen_c_library/c_library.cpp @@ -130,22 +130,6 @@ int gen_main() header.print( basic_types ); header.print( debug ); - // Array(StrC) to_rename = array_init_reserve(StrC, GlobalAllocator, 128); - // to_rename.append(txt("align_forward")); - // to_rename.append(txt("pointer_add")); - // to_rename.append(txt("allocator_info")); - // to_rename.append(txt("size_remaining")); - // to_rename.append(txt("free")); - // to_rename.append(txt("clear")); - // to_rename.append(txt("init_sub")); - // to_rename.append(txt("check")); - - // NeedsSelectors needs_selectors; - // needs_selectors.table = hashtable_init_reserve(Array(CodeFn), GlobalAllocator, 1024); - // for ( StrC id : to_rename ) { - // needs_selectors.set(id, array_init_reserve(CodeFn, GlobalAllocator, 128)); - // } - CodeBody parsed_memory = parse_file( project_dir "dependencies/memory.hpp" ); CodeBody memory = def_body(CT_Global_Body); @@ -317,7 +301,7 @@ int gen_main() containers.append( def_pragma(code(endregion Containers))); } - header.print(fmt_newline); + // header.print(fmt_newline); // header.print(dump_to_scratch_and_retireve(containers)); Code hashing = scan_file( project_dir "dependencies/hashing.hpp" ); @@ -421,7 +405,7 @@ int gen_main() CodeBody especifier = gen_especifier( project_dir "enums/ESpecifier.csv" ); CodeBody ast_inlines = gen_ast_inlines(); -#if 0 + #if 0 header.print_fmt("#pragma region Types\n"); header.print( types ); header.print( fmt_newline ); diff --git a/gen_c_library/components/memory.fixed_arena.hpp b/gen_c_library/components/memory.fixed_arena.hpp index 3a73a0e..204adc8 100644 --- a/gen_c_library/components/memory.fixed_arena.hpp +++ b/gen_c_library/components/memory.fixed_arena.hpp @@ -9,46 +9,48 @@ CodeBody gen_fixed_arenas() result.append(def_pragma(txt("region FixedArena"))); char const* template_struct = stringize( - struct FixedArena_ + struct FixedArena__Def { char memory[]; Arena arena; }; + typedef struct FixedArena__Def FixedArena_; ); char const* template_interface = stringize( inline - void fixed_arena_init_(FixedArena_* result) { - zero_size(& result->memory[0], ); + void fixed_arena_init_(FixedArena_* result) + { result->arena = arena_init_from_memory(& result->memory[0], ); } inline - ssize fixed_arena_size_remaining_(FixedArena_* fixed_arena, ssize alignment) { - return size_remaining(fixed_arena->arena, alignment); + ssize fixed_arena_size_remaining_(FixedArena_* fixed_arena, ssize alignment) + { + return arena_size_remaining( & fixed_arena->arena, alignment); } ); - CodeStruct arena_struct_1kb = parse_struct( token_fmt_impl( 3, "Name", txt("1KB"), "Size", txt("kilobytes(1)"), template_struct )); - CodeStruct arena_struct_4kb = parse_struct( token_fmt_impl( 3, "Name", txt("4KB"), "Size", txt("kilobytes(4)"), template_struct )); - CodeStruct arena_struct_8kb = parse_struct( token_fmt_impl( 3, "Name", txt("8KB"), "Size", txt("kilobytes(8)"), template_struct )); - CodeStruct arena_struct_16kb = parse_struct( token_fmt_impl( 3, "Name", txt("16KB"), "Size", txt("kilobytes(16)"), template_struct )); - CodeStruct arena_struct_32kb = parse_struct( token_fmt_impl( 3, "Name", txt("32KB"), "Size", txt("kilobytes(32)"), template_struct )); - CodeStruct arena_struct_64kb = parse_struct( token_fmt_impl( 3, "Name", txt("64KB"), "Size", txt("kilobytes(64)"), template_struct )); - CodeStruct arena_struct_128kb = parse_struct( token_fmt_impl( 3, "Name", txt("128KB"), "Size", txt("kilobytes(128)"), template_struct )); - CodeStruct arena_struct_256kb = parse_struct( token_fmt_impl( 3, "Name", txt("256KB"), "Size", txt("kilobytes(256)"), template_struct )); - CodeStruct arena_struct_512kb = parse_struct( token_fmt_impl( 3, "Name", txt("512KB"), "Size", txt("kilobytes(512)"), template_struct )); - CodeStruct arena_struct_1mb = parse_struct( token_fmt_impl( 3, "Name", txt("1MB"), "Size", txt("megabytes(1)"), template_struct )); - CodeStruct arena_struct_2mb = parse_struct( token_fmt_impl( 3, "Name", txt("2MB"), "Size", txt("megabytes(2)"), template_struct )); - CodeStruct arena_struct_4mb = parse_struct( token_fmt_impl( 3, "Name", txt("4MB"), "Size", txt("megabytes(4)"), template_struct )); + CodeBody arena_struct_1kb = parse_global_body( token_fmt_impl( 3, "Name", txt("1KB"), "Size", txt("kilobytes(1)"), template_struct )); + CodeBody arena_struct_4kb = parse_global_body( token_fmt_impl( 3, "Name", txt("4KB"), "Size", txt("kilobytes(4)"), template_struct )); + CodeBody arena_struct_8kb = parse_global_body( token_fmt_impl( 3, "Name", txt("8KB"), "Size", txt("kilobytes(8)"), template_struct )); + CodeBody arena_struct_16kb = parse_global_body( token_fmt_impl( 3, "Name", txt("16KB"), "Size", txt("kilobytes(16)"), template_struct )); + CodeBody arena_struct_32kb = parse_global_body( token_fmt_impl( 3, "Name", txt("32KB"), "Size", txt("kilobytes(32)"), template_struct )); + CodeBody arena_struct_64kb = parse_global_body( token_fmt_impl( 3, "Name", txt("64KB"), "Size", txt("kilobytes(64)"), template_struct )); + CodeBody arena_struct_128kb = parse_global_body( token_fmt_impl( 3, "Name", txt("128KB"), "Size", txt("kilobytes(128)"), template_struct )); + CodeBody arena_struct_256kb = parse_global_body( token_fmt_impl( 3, "Name", txt("256KB"), "Size", txt("kilobytes(256)"), template_struct )); + CodeBody arena_struct_512kb = parse_global_body( token_fmt_impl( 3, "Name", txt("512KB"), "Size", txt("kilobytes(512)"), template_struct )); + CodeBody arena_struct_1mb = parse_global_body( token_fmt_impl( 3, "Name", txt("1MB"), "Size", txt("megabytes(1)"), template_struct )); + CodeBody arena_struct_2mb = parse_global_body( token_fmt_impl( 3, "Name", txt("2MB"), "Size", txt("megabytes(2)"), template_struct )); + CodeBody arena_struct_4mb = parse_global_body( token_fmt_impl( 3, "Name", txt("4MB"), "Size", txt("megabytes(4)"), template_struct )); - CodeBody arena_interface_1kb = parse_global_body( token_fmt_impl( 3, "Name", txt("1KB"), "Size", txt("kilobytes(1)"), template_interface )); - CodeBody arena_interface_4kb = parse_global_body( token_fmt_impl( 3, "Name", txt("4KB"), "Size", txt("kilobytes(4)"), template_interface )); - CodeBody arena_interface_8kb = parse_global_body( token_fmt_impl( 3, "Name", txt("8KB"), "Size", txt("kilobytes(8)"), template_interface )); - CodeBody arena_interface_16kb = parse_global_body( token_fmt_impl( 3, "Name", txt("16KB"), "Size", txt("kilobytes(16)"), template_interface )); - CodeBody arena_interface_32kb = parse_global_body( token_fmt_impl( 3, "Name", txt("32KB"), "Size", txt("kilobytes(32)"), template_interface )); - CodeBody arena_interface_64kb = parse_global_body( token_fmt_impl( 3, "Name", txt("64KB"), "Size", txt("kilobytes(64)"), template_interface )); + CodeBody arena_interface_1kb = parse_global_body( token_fmt_impl( 3, "Name", txt("1KB"), "Size", txt("kilobytes(1)"), template_interface )); + CodeBody arena_interface_4kb = parse_global_body( token_fmt_impl( 3, "Name", txt("4KB"), "Size", txt("kilobytes(4)"), template_interface )); + CodeBody arena_interface_8kb = parse_global_body( token_fmt_impl( 3, "Name", txt("8KB"), "Size", txt("kilobytes(8)"), template_interface )); + CodeBody arena_interface_16kb = parse_global_body( token_fmt_impl( 3, "Name", txt("16KB"), "Size", txt("kilobytes(16)"), template_interface )); + CodeBody arena_interface_32kb = parse_global_body( token_fmt_impl( 3, "Name", txt("32KB"), "Size", txt("kilobytes(32)"), template_interface )); + CodeBody arena_interface_64kb = parse_global_body( token_fmt_impl( 3, "Name", txt("64KB"), "Size", txt("kilobytes(64)"), template_interface )); CodeBody arena_interface_128kb = parse_global_body( token_fmt_impl( 3, "Name", txt("128KB"), "Size", txt("kilobytes(128)"), template_interface )); CodeBody arena_interface_256kb = parse_global_body( token_fmt_impl( 3, "Name", txt("256KB"), "Size", txt("kilobytes(256)"), template_interface )); CodeBody arena_interface_512kb = parse_global_body( token_fmt_impl( 3, "Name", txt("512KB"), "Size", txt("kilobytes(512)"), template_interface )); @@ -82,6 +84,7 @@ CodeBody gen_fixed_arenas() CodeDefine def = def_define(txt("fixed_arena_allocator_info(fixed_arena)"), code({ arena_allocator_proc, & fixed_arena.arena }) ); result.append(def); + result.append(fmt_newline); result.append(parse_global_body(txt(R"( #define fixed_arena_init(expr) _Generic((expr), \ @@ -116,6 +119,7 @@ CodeBody gen_fixed_arenas() )" ))); + result.append(fmt_newline); result.append(def_pragma(txt("endregion FixedArena"))); return result; diff --git a/gen_c_library/components/misc.hpp b/gen_c_library/components/misc.hpp index 2a707f0..3916b23 100644 --- a/gen_c_library/components/misc.hpp +++ b/gen_c_library/components/misc.hpp @@ -3,38 +3,8 @@ // using namespace gen; -using SwapContentProc = CodeBody(void); -struct NeedsSelectors { - HashTable(Array(CodeFn)) table; - - void set(StrC sig, Array(CodeFn) list) { - u32 key = crc32(sig.Ptr, sig.Len); - table.set(key, list); - } - Array(CodeFn)* get(StrC sig) { - u32 key = crc32(sig.Ptr, sig.Len); - return table.get(key); - } -}; -CodeBody generate_generic_selectors(Array(Array(CodeFn)) listing) -{ - constexpr char const* tmpl_selector = -R"( #define () \ -_Generic(( -)) -); -)"; - - CodeBody result = def_body(CT_Global_Body); - for (Array(CodeFn) functions : listing) - { - - } - return result; -} - b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& body ) { b32 found = false; @@ -153,6 +123,7 @@ CodeFn rename_function_to_unique_symbol(CodeFn fn, StrC optional_prefix = txt("" return fn; } +using SwapContentProc = CodeBody(void); bool swap_pragma_region_implementation( StrC region_name, SwapContentProc* swap_content, Code& entry_iter, CodeBody& body ) { bool found = false; diff --git a/project/auxillary/builder.cpp b/project/auxillary/builder.cpp index 2af0c5c..8bb207b 100644 --- a/project/auxillary/builder.cpp +++ b/project/auxillary/builder.cpp @@ -43,7 +43,7 @@ void Builder::print_fmt( char const* fmt, ... ) va_end( va ); // log_fmt( "$%s - print_fmt: %.*s\n", File.filename, res > 80 ? 80 : res, buf ); - string_append_c_str_len( (String*) & Buffer, (char const*)buf, res ); + string_append_c_str_len( (String*) & Buffer, (char const*)buf, res); } void Builder::write() diff --git a/project/components/code_serialization.cpp b/project/components/code_serialization.cpp index 576df34..6521a0c 100644 --- a/project/components/code_serialization.cpp +++ b/project/components/code_serialization.cpp @@ -221,12 +221,12 @@ void to_string_fwd( CodeClass self, String* result ) String to_string(CodeDefine define) { - return string_fmt_buf( GlobalAllocator, "#define %SC %SC\n", define->Name, define->Content ); + return string_fmt_buf( GlobalAllocator, "#define %SC %SC", define->Name, define->Content ); } void to_string(CodeDefine define, String* result ) { - string_append_fmt( result, "#define %SC %SC\n", define->Name, define->Content ); + string_append_fmt( result, "#define %SC %SC", define->Name, define->Content ); } String to_string(CodeDestructor self) @@ -919,7 +919,7 @@ void to_string_ifdef(CodePreprocessCond cond, String* result ) void to_string_ifndef(CodePreprocessCond cond, String* result ) { - string_append_fmt( result, "#ifndef %SC\n", cond->Content ); + string_append_fmt( result, "#ifndef %SC", cond->Content ); } void to_string_elif(CodePreprocessCond cond, String* result ) diff --git a/project/components/interface.cpp b/project/components/interface.cpp index 99d0344..ce3dfe6 100644 --- a/project/components/interface.cpp +++ b/project/components/interface.cpp @@ -11,7 +11,7 @@ internal void deinit(); internal void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ) { - Arena* last = array_back(& Global_AllocatorBuckets); + Arena* last = array_back(Global_AllocatorBuckets); switch ( type ) { @@ -27,7 +27,7 @@ void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, s if ( ! array_append( & Global_AllocatorBuckets, bucket ) ) GEN_FATAL( "Failed to append bucket to Global_AllocatorBuckets"); - last = array_back(& Global_AllocatorBuckets); + last = array_back(Global_AllocatorBuckets); } return alloc_align( arena_allocator_info(last), size, alignment ); @@ -54,7 +54,7 @@ void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, s if ( ! array_append( & Global_AllocatorBuckets, bucket ) ) GEN_FATAL( "Failed to append bucket to Global_AllocatorBuckets"); - last = array_back(& Global_AllocatorBuckets); + last = array_back(Global_AllocatorBuckets); } void* result = alloc_align( last->Backing, size, alignment ); @@ -296,7 +296,7 @@ void init() PreprocessorDefines = array_init_reserve( GlobalAllocator, kilobytes(1) ); define_constants(); - parser::init(); + GEN_NS_PARSER init(); } void deinit() @@ -321,14 +321,14 @@ void deinit() } while ( left--, left ); - destroy(& StringCache); + hashtable_destroy(StringCache); - array_free( & CodePools); - array_free( & StringArenas); + array_free( CodePools); + array_free( StringArenas); arena_free(& LexArena); - array_free(& PreprocessorDefines); + array_free(PreprocessorDefines); index = 0; left = array_num(Global_AllocatorBuckets); @@ -340,8 +340,8 @@ void deinit() } while ( left--, left ); - array_free(& Global_AllocatorBuckets); - parser::deinit(); + array_free(Global_AllocatorBuckets); + GEN_NS_PARSER deinit(); } void reset() @@ -366,14 +366,14 @@ void reset() } while ( left--, left ); - clear(StringCache); + hashtable_clear(StringCache); define_constants(); } AllocatorInfo get_string_allocator( s32 str_length ) { - Arena* last = array_back(& StringArenas); + Arena* last = array_back(StringArenas); usize size_req = str_length + sizeof(StringHeader) + sizeof(char*); @@ -384,7 +384,7 @@ AllocatorInfo get_string_allocator( s32 str_length ) if ( ! array_append( & StringArenas, new_arena ) ) GEN_FATAL( "gen::get_string_allocator: Failed to allocate a new string arena" ); - last = array_back(& StringArenas); + last = array_back(StringArenas); } return arena_allocator_info(last); @@ -396,14 +396,14 @@ StringCached get_cached_string( StrC str ) s32 hash_length = str.Len > kilobytes(1) ? kilobytes(1) : str.Len; u64 key = crc32( str.Ptr, hash_length ); { - StringCached* result = get(StringCache, key ); + StringCached* result = hashtable_get(StringCache, key ); if ( result ) return * result; } - String result = string_make_strc( get_string_allocator( str.Len ), str ); - set(& StringCache, key, { str.Len, result } ); + StrC result = string_to_strc( string_make_strc( get_string_allocator( str.Len ), str )); + hashtable_set(StringCache, key, result ); return { str.Len, result }; } @@ -411,7 +411,7 @@ StringCached get_cached_string( StrC str ) // Used internally to retireve a Code object form the CodePool. Code make_code() { - Pool* allocator = array_back( & CodePools); + Pool* allocator = array_back( CodePools); if ( allocator->FreeList == nullptr ) { Pool code_pool = pool_init( Allocator_CodePool, CodePool_NumBlocks, sizeof(AST) ); @@ -422,7 +422,7 @@ Code make_code() if ( ! array_append( & CodePools, code_pool ) ) GEN_FATAL( "gen::make_code: Failed to allocate a new code pool - CodePools failed to append new pool." ); - allocator = array_back( & CodePools); + allocator = array_back( CodePools); } Code result { rcast( AST*, alloc( pool_allocator_info(allocator), sizeof(AST) )) }; diff --git a/project/components/interface.untyped.cpp b/project/components/interface.untyped.cpp index afece45..45ecdc1 100644 --- a/project/components/interface.untyped.cpp +++ b/project/components/interface.untyped.cpp @@ -9,15 +9,13 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va ) ssize remaining = buf_size; local_persist - Arena tok_map_arena; + FixedArena tok_map_arena; + fixed_arena_init( & tok_map_arena); + local_persist HashTable(StrC) tok_map; { - local_persist - char tok_map_mem[ TokenFmt_TokenMap_MemSize ]; - - tok_map_arena = arena_init_from_memory( tok_map_mem, sizeof(tok_map_mem) ); - tok_map = hashtable_init(StrC, arena_allocator_info(& tok_map_arena) ); + tok_map = hashtable_init(StrC, fixed_arena_allocator_info(& tok_map_arena) ); s32 left = num_tokens - 1; @@ -27,7 +25,7 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va ) StrC value = va_arg( va, StrC ); u32 key = crc32( token, str_len(token) ); - set(& tok_map, key, value ); + hashtable_set( tok_map, key, value ); } } @@ -63,7 +61,7 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va ) char const* token = fmt + 1; u32 key = crc32( token, tok_len ); - StrC* value = get(tok_map, key ); + StrC* value = hashtable_get(tok_map, key ); if ( value ) { @@ -93,8 +91,8 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va ) } } - clear(tok_map); - arena_free(& tok_map_arena); + hashtable_clear(tok_map); + fixed_arena_free(& tok_map_arena); ssize result = buf_size - remaining; diff --git a/project/components/interface.upfront.cpp b/project/components/interface.upfront.cpp index 49bfe81..ff0a0ae 100644 --- a/project/components/interface.upfront.cpp +++ b/project/components/interface.upfront.cpp @@ -635,7 +635,7 @@ CodeDefine def_define( StrC name, StrC content ) result->Content = get_cached_string( txt("") ); } else - result->Content = get_cached_string( content ); + result->Content = get_cached_string( string_to_strc(string_fmt_buf(GlobalAllocator, "%SC\n", content)) ); return result; } @@ -644,13 +644,13 @@ CodeDestructor def_destructor( Opts_def_destructor p ) { Code body = p.body; CodeSpecifiers specifiers = p.specifiers; - + if ( specifiers && specifiers->Type != CT_Specifiers ) { log_failure( "gen::def_destructor: specifiers was not a 'Specifiers' type: %s", debug_str(specifiers) ); return InvalidCode; } - + CodeDestructor result = (CodeDestructor) make_code(); if ( specifiers ) diff --git a/project/components/lexer.cpp b/project/components/lexer.cpp index e74b5d0..1879315 100644 --- a/project/components/lexer.cpp +++ b/project/components/lexer.cpp @@ -124,6 +124,21 @@ Token* current(TokArray* self, bool skip_formatting ) return & self->Arr[self->Idx]; } +Token* peek(TokArray self, bool skip_formatting) +{ + s32 idx = self.Idx; + + if ( skip_formatting ) + { + while ( self.Arr[idx].Type == Tok_NewLine ) + idx++; + + return & self.Arr[idx]; + } + + return & self.Arr[idx]; +} + Token* previous(TokArray self, bool skip_formatting) { s32 idx = self.Idx; @@ -340,7 +355,7 @@ s32 lex_preprocessor_directive( LexContext* ctx ) array_append( & Tokens, name ); u64 key = crc32( name.Text, name.Length ); - set(& ctx->defines, key, to_str(name) ); + hashtable_set(ctx->defines, key, to_str(name) ); } Token preprocess_content = { ctx->scanner, 0, Tok_Preprocess_Content, ctx->line, ctx->column, TF_Preprocess }; @@ -432,12 +447,13 @@ s32 lex_preprocessor_directive( LexContext* ctx ) if ( current == '\r' ) { - move_forward(); + break; + //move_forward(); } if ( current == '\n' ) { - move_forward(); + //move_forward(); break; } @@ -509,7 +525,7 @@ void lex_found_token( LexContext* ctx ) else key = crc32( ctx->token.Text, ctx->token.Length ); - StrC* define = get(ctx->defines, key ); + StrC* define = hashtable_get(ctx->defines, key ); if ( define ) { ctx->token.Type = Tok_Preprocess_Macro; @@ -537,14 +553,16 @@ void lex_found_token( LexContext* ctx ) ctx->token.Length++; } - if ( current == '\r' && ctx->scanner[1] == '\n' ) - { - move_forward(); - } - else if ( current == '\n' ) - { - move_forward(); - } + //if ( current == '\r' && ctx->scanner[1] == '\n' ) + //{ + // move_forward(); + // ctx->token.Length++; + //} + //else if ( current == '\n' ) + //{ + // move_forward(); + // ctx->token.Length++; + //} } else { @@ -554,7 +572,6 @@ void lex_found_token( LexContext* ctx ) array_append( & Tokens, ctx->token ); } - neverinline // TokArray lex( Array tokens, StrC content ) TokArray lex( StrC content ) @@ -593,7 +610,7 @@ TokArray lex( StrC content ) } u64 key = crc32( * entry, length ); - set(& c.defines, key, (StrC) * entry ); + hashtable_set(c.defines, key, (StrC) * entry ); } array_clear(Tokens); @@ -645,7 +662,29 @@ TokArray lex( StrC content ) switch ( result ) { case Lex_Continue: + { + //TokType last_type = Tokens[array_get_header(Tokens)->Num - 2].Type; + //if ( last_type == Tok_Preprocess_Pragma ) + { + c.token = { c.scanner, 0, Tok_Invalid, c.line, c.column, TF_Null }; + if ( current == '\r') + { + move_forward(); + c.token.Length = 1; + } + + if ( current == '\n' ) + { + c.token.Type = Tok_NewLine; + c.token.Length++; + move_forward(); + + array_append( & Tokens, c.token ); + } + } + continue; + } case Lex_ReturnNull: return { {}, 0 }; @@ -1245,8 +1284,30 @@ TokArray lex( StrC content ) } } - FoundToken: - lex_found_token( ctx ); + FoundToken: + { + lex_found_token( ctx ); + TokType last_type = array_back(Tokens)->Type; + if ( last_type == Tok_Preprocess_Macro ) + { + c.token = { c.scanner, 0, Tok_Invalid, c.line, c.column, TF_Null }; + if ( current == '\r') + { + move_forward(); + c.token.Length = 1; + } + + if ( current == '\n' ) + { + c.token.Type = Tok_NewLine; + c.token.Length++; + move_forward(); + + array_append( & Tokens, c.token ); + continue; + } + } + } } if ( array_num(Tokens) == 0 ) @@ -1255,7 +1316,7 @@ TokArray lex( StrC content ) return { {}, 0 }; } - clear(defines); + hashtable_clear(defines); // defines_map_arena.free(); return { Tokens, 0 }; } diff --git a/project/components/parser.cpp b/project/components/parser.cpp index 74c9ba7..614fb7c 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -54,7 +54,7 @@ String to_string(ParseContext ctx) sptr length = scope_start.Length; char const* current = scope_start.Text + length; - while ( current <= array_back( & ctx.Tokens.Arr)->Text && *current != '\n' && length < 74 ) + while ( current <= array_back( ctx.Tokens.Arr)->Text && *current != '\n' && length < 74 ) { current++; length++; @@ -141,7 +141,7 @@ void init() ); fixed_arena_init(& defines_map_arena); - defines = hashtable_init_reserve(StrC, allocator_info( & defines_map_arena), 256 ); + defines = hashtable_init_reserve(StrC, fixed_arena_allocator_info( & defines_map_arena), 256 ); } internal @@ -170,8 +170,9 @@ bool _check_parse_args( StrC def, char const* func_name ) return true; } -# define currtok_noskip (* current( & Context.Tokens, dont_skip_formatting )) -# define currtok (* current( & Context.Tokens, skip_formatting )) +# define currtok_noskip (* current( & Context.Tokens, dont_skip_formatting )) +# define currtok (* current( & Context.Tokens, skip_formatting )) +# define peektok (* peek(Context.Tokens, skip_formatting)) # define prevtok (* previous( Context.Tokens, dont_skip_formatting)) # define nexttok (* next( Context.Tokens, skip_formatting )) # define eat( Type_ ) __eat( & Context.Tokens, Type_ ) @@ -720,7 +721,7 @@ Code parse_class_struct( TokType which, bool inplace_def = false ) // local_persist - char interface_arr_mem[ kilobytes(4) ] {0}; + char interface_arr_mem[ kilobytes(4) ] {0}; Array interfaces; { Arena arena = arena_init_from_memory( interface_arr_mem, kilobytes(4) ); interfaces = array_init_reserve(CodeTypename, arena_allocator_info(& arena), 4 ); @@ -786,7 +787,7 @@ Code parse_class_struct( TokType which, bool inplace_def = false ) if ( inline_cmt ) result->InlineCmt = inline_cmt; - array_free(& interfaces); + array_free(interfaces); return result; } @@ -1708,8 +1709,17 @@ CodeBody parse_global_nspace( CodeType which ) break; case Tok_Preprocess_Macro: + { member = parse_simple_preprocess( Tok_Preprocess_Macro ); // + + if ( member == Code_Invalid ) + { + log_failure( "Failed to parse member\n%s", to_string(Context) ); + pop(& Context); + return InvalidCode; + } + } break; case Tok_Preprocess_Pragma: @@ -2935,7 +2945,7 @@ Code parse_simple_preprocess( TokType which ) eat( which ); // - if ( currtok.Type == Tok_BraceCurly_Open ) + if ( peektok.Type == Tok_BraceCurly_Open ) { // Eat the block scope right after the macro. Were assuming the macro defines a function definition's signature eat( Tok_BraceCurly_Open ); @@ -2977,7 +2987,7 @@ Code parse_simple_preprocess( TokType which ) { if ( str_compare_len( Context.Scope->Prev->ProcName.Ptr, "parse_typedef", Context.Scope->Prev->ProcName.Len ) != 0 ) { - if ( check( Tok_Statement_End )) + if ( peektok.Type == Tok_Statement_End ) { Token stmt_end = currtok; eat( Tok_Statement_End ); @@ -2989,7 +2999,7 @@ Code parse_simple_preprocess( TokType which ) } } - tok.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)tok.Text; + tok.Length = ( (sptr)currtok_noskip.Text + currtok_noskip.Length ) - (sptr)tok.Text; } char const* content = str_fmt_buf( "%.*s ", tok.Length, tok.Text ); @@ -3554,7 +3564,7 @@ CodeEnum parse_enum( bool inplace_def ) push_scope(); Specifier specs_found[16] { Spec_NumSpecifiers }; - s32 NumSpecifiers = 0; + s32 NumSpecifiers = 0; CodeAttributes attributes = { nullptr }; @@ -3589,7 +3599,7 @@ CodeEnum parse_enum( bool inplace_def ) // enum b32 use_macro_underlying = false; - Code underlying_macro = { nullptr }; + Code underlying_macro = { nullptr }; if ( currtok.Type == Tok_Assign_Classifer ) { eat( Tok_Assign_Classifer ); @@ -3701,9 +3711,9 @@ CodeEnum parse_enum( bool inplace_def ) eat( Tok_Operator ); // = - while ( currtok_noskip.Type != Tok_Comma && currtok_noskip.Type != Tok_BraceCurly_Close ) + while ( currtok.Type != Tok_Comma && currtok.Type != Tok_BraceCurly_Close ) { - eat( currtok_noskip.Type ); + eat( currtok.Type ); } } // = @@ -3717,6 +3727,9 @@ CodeEnum parse_enum( bool inplace_def ) if ( currtok.Type == Tok_Comma ) { + //Token prev = * previous(Context.Tokens, dont_skip_formatting); + //entry.Length = ( (sptr)prev.Text + prev.Length ) - (sptr)entry.Text; + eat( Tok_Comma ); // = , } @@ -3727,10 +3740,9 @@ CodeEnum parse_enum( bool inplace_def ) // eat( Tok_Comment ); // = , // // } - - - //Token prev = * previous(Context.Tokens, dont_skip_formatting); - entry.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)entry.Text; + + Token prev = * previous(Context.Tokens, dont_skip_formatting); + entry.Length = ( (sptr)prev.Text + prev.Length ) - (sptr)entry.Text; member = untyped_str( to_str(entry) ); break; diff --git a/project/dependencies/basic_types.hpp b/project/dependencies/basic_types.hpp index 37da1de..bdf8570 100644 --- a/project/dependencies/basic_types.hpp +++ b/project/dependencies/basic_types.hpp @@ -133,8 +133,8 @@ template sptr to_sptr( Type* ptr ) { return (sptr)ptr; } template mem_ptr to_mem_ptr ( Type ptr ) { return (mem_ptr) ptr; } template mem_ptr_const to_mem_ptr_const( Type ptr ) { return (mem_ptr_const)ptr; } #else -#define to_utpr( ptr ) ((uptr)(ptr)) -#define to_stpr( ptr ) ((sptr)(ptr)) +#define to_uptr( ptr ) ((uptr)(ptr)) +#define to_sptr( ptr ) ((sptr)(ptr)) #define to_mem_ptr( ptr) ((mem_ptr)ptr) #define to_mem_ptr_const( ptr) ((mem_ptr)ptr) diff --git a/project/dependencies/containers.hpp b/project/dependencies/containers.hpp index 01f650a..cbf6232 100644 --- a/project/dependencies/containers.hpp +++ b/project/dependencies/containers.hpp @@ -10,8 +10,13 @@ template struct RemoveConst { typede template struct RemoveConst { typedef TType Type[]; }; template struct RemoveConst { typedef TType Type[Size]; }; -template -using TRemoveConst = typename RemoveConst::Type; +template using TRemoveConst = typename RemoveConst::Type; + +template struct RemovePtr { typedef TType Type; }; +template struct RemovePtr { typedef TType Type; }; + +template using TRemovePtr = typename RemovePtr::Type; + #pragma region Array #define Array(Type) Array @@ -23,31 +28,33 @@ struct ArrayHeader; #if GEN_SUPPORT_CPP_MEMBER_FEATURES template struct Array; +# define get_array_underlying_type(array) typename TRemovePtr:: DataType #else template using Array = Type*; +# define get_array_underlying_type(array) TRemovePtr #endif usize array_grow_formula(ssize value); -template Array(Type) array_init (AllocatorInfo allocator); -template Array(Type) array_init_reserve(AllocatorInfo allocator, ssize capacity); -template bool array_append (Array(Type)* array, Array(Type) other); -template bool array_append (Array(Type)* array, Type value); -template bool array_append (Array(Type)* array, Type* items, usize item_num); -template bool array_append_at (Array(Type)* array, Type item, usize idx); -template bool array_append_at (Array(Type)* array, Type* items, usize item_num, usize idx); -template Type* array_back (Array(Type) array); -template void array_clear (Array(Type) array); -template bool array_fill (Array(Type) array, usize begin, usize end, Type value); -template void array_free (Array(Type)* array); -template bool arary_grow (Array(Type)* array, usize min_capacity); -template usize array_num (Array(Type) array); -template void arary_pop (Array(Type) array); -template void arary_remove_at (Array(Type) array, usize idx); -template bool arary_reserve (Array(Type)* array, usize new_capacity); -template bool arary_resize (Array(Type)* array, usize num); -template bool arary_set_capacity(Array(Type)* array, usize new_capacity); -template ArrayHeader* arary_get_header (Array(Type) array); +template Array array_init (AllocatorInfo allocator); +template Array array_init_reserve (AllocatorInfo allocator, ssize capacity); +template bool array_append_array (Array* array, Array other); +template bool array_append_value (Array* array, Type value); +template bool array_append_items (Array* array, Type* items, usize item_num); +template bool array_append_at (Array* array, Type item, usize idx); +template bool array_append_at_items(Array* array, Type* items, usize item_num, usize idx); +template Type* array_back (Array array); +template void array_clear (Array array); +template bool array_fill (Array array, usize begin, usize end, Type value); +template void array_free (Array* array); +template bool arary_grow (Array* array, usize min_capacity); +template usize array_num (Array array); +template void arary_pop (Array array); +template void arary_remove_at (Array array, usize idx); +template bool arary_reserve (Array* array, usize new_capacity); +template bool arary_resize (Array* array, usize num); +template bool arary_set_capacity (Array* array, usize new_capacity); +template ArrayHeader* arary_get_header (Array array); struct ArrayHeader { AllocatorInfo Allocator; @@ -92,6 +99,8 @@ struct Array forceinline Type& operator[](ssize index) { return Data[index]; } forceinline Type const& operator[](ssize index) const { return Data[index]; } + + using DataType = Type; }; #endif @@ -240,15 +249,15 @@ bool array_append_at(Array* array, Type* items, usize item_num, usize idx) } template inline -Type* array_back(Array* array) +Type* array_back(Array array) { GEN_ASSERT(array != nullptr); - ArrayHeader* header = array_get_header(* array); + ArrayHeader* header = array_get_header(array); if (header->Num <= 0) return nullptr; - return & (*array)[header->Num - 1]; + return & (array)[header->Num - 1]; } template inline @@ -380,7 +389,30 @@ bool array_set_capacity(Array* array, usize new_capacity) return true; } -#define array_init_reserve(type, allocator, cap) array_init_reserve(allocator, cap) +// These are intended for use in the base library of gencpp and the C-variant of the library +// It provides a interoperability between the C++ and C implementation of arrays. (not letting these do any crazy substiution though) +// They are undefined in gen.hpp and gen.cpp at the end of the files. +// We cpp library expects the user to use the regular calls as they can resolve the type fine. + +#define array_init(type, allocator) array_init (allocator ) +#define array_init_reserve(type, allocator, cap) array_init_reserve (allocator, cap) +#define array_append_array(array, other) array_append < get_array_underlying_type(array) > (& array, other ) +#define array_append_value(array, value) array_append < get_array_underlying_type(array) > (& array, value ) +#define array_append_items(array, items, item_num) array_append < get_array_underlying_type(array) > (& array, items, item_num ) +#define array_append_at(array, item, idx ) array_append_at < get_array_underlying_type(array) > (& array, item, idx ) +#define array_append_at_items(array, items, item_num, idx) array_append_at_items< get_array_underlying_type(array) > (& items, item_num, idx ) +#define array_back(array) array_back < get_array_underlying_type(array) > (array ) +#define array_clear(array) array_clear < get_array_underlying_type(array) > (array ) +#define array_fill(array, begin, end, value) array_fill < get_array_underlying_type(array) > (array, begin, end, value ) +#define array_free(array) array_free < get_array_underlying_type(array) > (& array ) +#define arary_grow(array, min_capacity) arary_grow < get_array_underlying_type(array) > (& array, min_capacity) +#define array_num(array) array_num < get_array_underlying_type(array) > (array ) +#define arary_pop(array) arary_pop < get_array_underlying_type(array) > (array ) +#define arary_remove_at(array, idx) arary_remove_at < get_array_underlying_type(array) > (idx) +#define arary_reserve(array, new_capacity) arary_reserve < get_array_underlying_type(array) > (& array, new_capacity ) +#define arary_resize(array, num) arary_resize < get_array_underlying_type(array) > (& array, num) +#define arary_set_capacity(new_capacity) arary_set_capacity < get_array_underlying_type(array) > (& array, new_capacity ) +#define arary_get_header(array) arary_get_header < get_array_underlying_type(array) > (array ) #pragma endregion Array @@ -406,23 +438,23 @@ struct HashTableEntry { #define HashTableEntry(Type) HashTableEntry -template HashTable hashtable_init(AllocatorInfo allocator); +template HashTable hashtable_init (AllocatorInfo allocator); template HashTable hashtable_init_reserve(AllocatorInfo allocator, usize num); -template void clear (HashTable table); -template void destroy (HashTable* table); -template Type* get (HashTable table, u64 key); -template void grow (HashTable* table); -template void rehash (HashTable* table, ssize new_num); -template void rehash_fast (HashTable table); -template void remove (HashTable table, u64 key); -template void remove_entry (HashTable table, ssize idx); -template void set (HashTable* table, u64 key, Type value); -template ssize slot (HashTable table, u64 key); -template ssize add_entry (HashTable* table, u64 key); -template HashTableFindResult find (HashTable table, u64 key); -template bool full (HashTable table); -template void map (HashTable table, void (*map_proc)(u64 key, Type value)); -template void map_mut (HashTable table, void (*map_proc)(u64 key, Type* value)); +template void hashtable_clear (HashTable table); +template void hashtable_destroy (HashTable* table); +template Type* hashtable_get (HashTable table, u64 key); +template void hashtable_grow (HashTable* table); +template void hashtable_rehash (HashTable* table, ssize new_num); +template void hashtable_rehash_fast (HashTable table); +template void hashtable_remove (HashTable table, u64 key); +template void hashtable_remove_entry(HashTable table, ssize idx); +template void hashtable_set (HashTable* table, u64 key, Type value); +template ssize hashtable_slot (HashTable table, u64 key); +template ssize hashtable_add_entry (HashTable* table, u64 key); +template HashTableFindResult hashtable_find (HashTable table, u64 key); +template bool hashtable_full (HashTable table); +template void hashtable_map (HashTable table, void (*map_proc)(u64 key, Type value)); +template void hashtable_map_mut (HashTable table, void (*map_proc)(u64 key, Type* value)); static constexpr f32 HashTable_CriticalLoadScale = 0.7f; @@ -482,22 +514,22 @@ HashTable hashtable_init_reserve(AllocatorInfo allocator, usize num) } template inline -void clear(HashTable table) { +void hashtable_clear(HashTable table) { array_clear(table.Entries); array_fill(table.Hashes, 0, array_num(table.Hashes), (ssize)-1); } template inline -void destroy(HashTable* table) { +void hashtable_destroy(HashTable* table) { if (table->Hashes && array_get_header(table->Hashes)->Capacity) { - array_free(& table->Hashes); - array_free(& table->Entries); + array_free(table->Hashes); + array_free(table->Entries); } } template inline -Type* get(HashTable table, u64 key) { - ssize idx = find(table, key).EntryIndex; +Type* hashtable_get(HashTable table, u64 key) { + ssize idx = hashtable_find(table, key).EntryIndex; if (idx >= 0) return & table.Entries[idx].Value; @@ -505,7 +537,7 @@ Type* get(HashTable table, u64 key) { } template inline -void map(HashTable table, void (*map_proc)(u64 key, Type value)) { +void hashtable_map(HashTable table, void (*map_proc)(u64 key, Type value)) { GEN_ASSERT_NOT_NULL(map_proc); for (ssize idx = 0; idx < ssize(num(table.Entries)); ++idx) { @@ -514,7 +546,7 @@ void map(HashTable table, void (*map_proc)(u64 key, Type value)) { } template inline -void map_mut(HashTable table, void (*map_proc)(u64 key, Type* value)) { +void hashtable_map_mut(HashTable table, void (*map_proc)(u64 key, Type* value)) { GEN_ASSERT_NOT_NULL(map_proc); for (ssize idx = 0; idx < ssize(num(table.Entries)); ++idx) { @@ -523,13 +555,13 @@ void map_mut(HashTable table, void (*map_proc)(u64 key, Type* value)) { } template inline -void grow(HashTable* table) { +void hashtable_grow(HashTable* table) { ssize new_num = array_grow_formula( array_num(table->Entries)); - rehash(table, new_num); + hashtable_rehash(table, new_num); } template inline -void rehash(HashTable* table, ssize new_num) +void hashtable_rehash(HashTable* table, ssize new_num) { ssize last_added_index; HashTable new_ht = hashtable_init_reserve( array_get_header(table->Hashes)->Allocator, new_num); @@ -539,8 +571,8 @@ void rehash(HashTable* table, ssize new_num) HashTableFindResult find_result; HashTableEntry& entry = table->Entries[idx]; - find_result = find(new_ht, entry.Key); - last_added_index = add_entry(& new_ht, entry.Key); + find_result = hashtable_find(new_ht, entry.Key); + last_added_index = hashtable_add_entry(& new_ht, entry.Key); if (find_result.PrevIndex < 0) new_ht.Hashes[find_result.HashIndex] = last_added_index; @@ -551,12 +583,12 @@ void rehash(HashTable* table, ssize new_num) new_ht.Entries[last_added_index].Value = entry.Value; } - destroy(table); + hashtable_destroy(table); * table = new_ht; } template inline -void rehash_fast(HashTable table) +void hashtable_rehash_fast(HashTable table) { ssize idx; @@ -582,7 +614,7 @@ void rehash_fast(HashTable table) } template inline -void remove(HashTable table, u64 key) { +void hashtable_remove(HashTable table, u64 key) { HashTableFindResult find_result = find(table, key); if (find_result.EntryIndex >= 0) { @@ -592,26 +624,26 @@ void remove(HashTable table, u64 key) { } template inline -void remove_entry(HashTable table, ssize idx) { +void hashtable_remove_entry(HashTable table, ssize idx) { remove_at(table.Entries, idx); } template inline -void set(HashTable* table, u64 key, Type value) +void hashtable_set(HashTable* table, u64 key, Type value) { ssize idx; HashTableFindResult find_result; - if (full(* table)) - grow(table); + if (hashtable_full(* table)) + hashtable_grow(table); - find_result = find(* table, key); + find_result = hashtable_find(* table, key); if (find_result.EntryIndex >= 0) { idx = find_result.EntryIndex; } else { - idx = add_entry(table, key); + idx = hashtable_add_entry(table, key); if (find_result.PrevIndex >= 0) { table->Entries[find_result.PrevIndex].Next = idx; @@ -623,12 +655,12 @@ void set(HashTable* table, u64 key, Type value) table->Entries[idx].Value = value; - if (full(* table)) - grow(table); + if (hashtable_full(* table)) + hashtable_grow(table); } template inline -ssize slot(HashTable table, u64 key) { +ssize hashtable_slot(HashTable table, u64 key) { for (ssize idx = 0; idx < ssize(num(table.Hashes)); ++idx) if (table.Hashes[idx] == key) return idx; @@ -637,7 +669,7 @@ ssize slot(HashTable table, u64 key) { } template inline -ssize add_entry(HashTable* table, u64 key) { +ssize hashtable_add_entry(HashTable* table, u64 key) { ssize idx; HashTableEntry entry = { key, -1 }; @@ -647,7 +679,7 @@ ssize add_entry(HashTable* table, u64 key) { } template inline -HashTableFindResult find(HashTable table, u64 key) +HashTableFindResult hashtable_find(HashTable table, u64 key) { HashTableFindResult result = { -1, -1, -1 }; @@ -670,29 +702,29 @@ HashTableFindResult find(HashTable table, u64 key) } template inline -bool full(HashTable table) { +bool hashtable_full(HashTable table) { usize critical_load = usize(HashTable_CriticalLoadScale * f32(array_num(table.Hashes))); b32 result = array_num(table.Entries) > critical_load; return result; } -#define hashtable_init(Type, allocator) hashtable_init(allocator) -#define hashtable_init_reserve(Type, allocator, num) hashtable_init_reserve(allocator, num) -#define hashtable_clear(Type, table) clear(table) -#define hashtable_destroy(Type, table) destroy(table) -#define hashtable_get(Type, table, key) get(table, key) -#define hashtable_grow(Type, table) grow(table) -#define hashtable_rehash(Type, table, new_num) rehash(table, new_num) -#define hashtable_rehash_fast(Type, table) rehash_fast(table) -#define hashtable_remove(Type, table, key) remove(table, key) -#define hashtable_remove_entry(Type, table, idx) remove_entry(table, idx) -#define hashtable_set(Type, table, key, value) set(table, key, value) -#define hashtable_slot(Type, table, key) slot(table, key) -#define hashtable_add_entry(Type, table, key) add_entry(table, key) -#define hashtable_find(Type, table, key) find(table, key) -#define hashtable_full(Type, table) full(table) -#define hashtable_map(Type, table, map_proc) map(table, map_proc) -#define hashtable_map_mut(Type, table, map_proc) map_mut(table, map_proc) +#define hashtable_init(type, allocator) hashtable_init (allocator) +#define hashtable_init_reserve(type, allocator, num) hashtable_init_reserve(allocator, num) +#define hashtable_clear(table) hashtable_clear < TStripPtr >(table) +#define hashtable_destroy(table) hashtable_destroy < TStripPtr
>(& table) +#define hashtable_get(table, key) hashtable_get < TStripPtr
>(table, key) +#define hashtable_grow(table) hashtable_grow < TStripPtr
>(& table) +#define hashtable_rehash(table, new_num) hashtable_rehash < TStripPtr
>(& table, new_num) +#define hashtable_rehash_fast(table) hashtable_rehash_fast < TStripPtr
>(table) +#define hashtable_remove(table, key) hashtable_remove < TStripPtr
>(table, key) +#define hashtable_remove_entry(table, idx) hashtable_remove_entry< TStripPtr
>(table, idx) +#define hashtable_set(table, key, value) hashtable_set < TStripPtr
>(& table, key, value) +#define hashtable_slot(table, key) hashtable_slot < TStripPtr
>(table, key) +#define hashtable_add_entry(table, key) hashtable_add_entry < TStripPtr
>(& table, key) +#define hashtable_find(table, key) hashtable_find < TStripPtr
>(table, key) +#define hashtable_full(table) hashtable_full < TStripPtr
>(table) +#define hashtable_map(table, map_proc) hashtable_map < TStripPtr
>(table, map_proc) +#define hashtable_map_mut(table, map_proc) hashtable_map_mut < TStripPtr
>(table, map_proc) #pragma endregion HashTable diff --git a/project/dependencies/filesystem.cpp b/project/dependencies/filesystem.cpp index 832288d..aa1d41d 100644 --- a/project/dependencies/filesystem.cpp +++ b/project/dependencies/filesystem.cpp @@ -647,7 +647,7 @@ GEN_FILE_CLOSE_PROC( _memory_file_close ) if ( d->flags & EFileStream_CLONE_WRITABLE ) { Array arr = { d->buf }; - array_free(& arr); + array_free(arr); } allocator_free( allocator, d ); diff --git a/project/dependencies/macros.hpp b/project/dependencies/macros.hpp index bee0015..df7132a 100644 --- a/project/dependencies/macros.hpp +++ b/project/dependencies/macros.hpp @@ -183,14 +183,20 @@ # endif #endif -#if !defined(GEN_SUPPORT_CPP_REFERENCES) && (GEN_COMPILER_C || __STDC_VERSION__ < 202311L) -# undef GEN_SUPPORT_CPP_REFERENCES -# define GEN_SUPPORT_CPP_REFERENCES 0 +#if !defined(GEN_SUPPORT_CPP_REFERENCES) +# define GEN_SUPPORT_CPP_REFERENCES 1 +#endif +#if GEN_COMPILER_C && defined(GEN_SUPPORT_CPP_REFERENCES) +# undef GEN_SUPPORT_CPP_REFERENCES +# define GEN_SUPPORT_CPP_REFERENCES 0 #endif -#if !defined(GEN_SUPPORT_CPP_MEMBER_FEATURES) && (GEN_COMPILER_C || __STDC_VERSION__ < 202311L) -# undef GEN_SUPPORT_CPP_MEMBER_FEATURES -# define GEN_SUPPORT_CPP_MEMBER_FEATURES 0 +#if !defined(GEN_SUPPORT_CPP_MEMBER_FEATURES) +# define GEN_SUPPORT_CPP_MEMBER_FEATURES 1 +#endif +#if GEN_COMPILER_C && defined(GEN_SUPPORT_CPP_MEMBER_FEATURES) +# undef GEN_SUPPORT_CPP_MEMBER_FEATURES +# define GEN_SUPPORT_CPP_MEMBER_FEATURES 0 #endif #if ! defined(typeof) && (!GEN_COMPILER_C || __STDC_VERSION__ < 202311L) @@ -229,6 +235,10 @@ # ifndef nullptr # define nullptr NULL # endif + +# ifndef GEN_REMOVE_PTR +# define GEN_REMOVE_PTR(type) typeof(* ( (type) NULL) ) +# endif #endif #if ! defined(GEN_PARAM_DEFAULT) && GEN_COMPILER_CPP diff --git a/project/dependencies/memory.hpp b/project/dependencies/memory.hpp index 76d2af1..3f60bec 100644 --- a/project/dependencies/memory.hpp +++ b/project/dependencies/memory.hpp @@ -320,17 +320,15 @@ ssize arena_size_remaining(Arena* arena, ssize alignment) } #pragma endregion Arena - GEN_API_C_END - #pragma region FixedArena template struct FixedArena; template FixedArena fixed_arena_init(); -template AllocatorInfo allocator_info(FixedArena* fixed_arena ); -template ssize size_remaining(FixedArena* fixed_arena, ssize alignment); +template AllocatorInfo fixed_arena_allocator_info(FixedArena* fixed_arena ); +template ssize fixed_arena_size_remaining(FixedArena* fixed_arena, ssize alignment); #if GEN_SUPPORT_CPP_REFERENCES template AllocatorInfo allocator_info( FixedArena& fixed_arena ) { return allocator_info(& fixed_arena); } @@ -356,7 +354,7 @@ struct FixedArena }; template inline -AllocatorInfo allocator_info( FixedArena* fixed_arena ) { +AllocatorInfo fixed_arena_allocator_info( FixedArena* fixed_arena ) { GEN_ASSERT(fixed_arena); return { arena_allocator_proc, & fixed_arena->arena }; } @@ -368,7 +366,12 @@ void fixed_arena_init(FixedArena* result) { } template inline -ssize size_remaining(FixedArena* fixed_arena, ssize alignment) { +void fixed_arena_free(FixedArena* fixed_arena) { + arena_free( & fixed_arena->arena); +} + +template inline +ssize fixed_arena_size_remaining(FixedArena* fixed_arena, ssize alignment) { return size_remaining(fixed_arena->arena, alignment); } @@ -386,10 +389,8 @@ using Arena_2MB = FixedArena< megabytes( 2 ) >; using Arena_4MB = FixedArena< megabytes( 4 ) >; #pragma endregion FixedArena - GEN_API_C_BEGIN - #pragma region Pool struct Pool_Def; typedef struct Pool_Def Pool; @@ -435,7 +436,8 @@ struct Pool_Def inline AllocatorInfo pool_allocator_info(Pool* pool) { - return { pool_allocator_proc, pool }; + AllocatorInfo info = { pool_allocator_proc, pool }; + return info; } inline diff --git a/project/dependencies/platform.hpp b/project/dependencies/platform.hpp index 3aad481..1c3c5df 100644 --- a/project/dependencies/platform.hpp +++ b/project/dependencies/platform.hpp @@ -139,6 +139,7 @@ #if GEN_COMPILER_C #include +#include #endif #pragma endregion Mandatory Includes @@ -148,6 +149,7 @@ # define GEN_NS_PARSER_BEGIN # define GEN_NS_PARSER_END # define GEN_USING_NS_PARSER +# define GEN_NS_PARSER # define GEN_NS # define GEN_NS_BEGIN # define GEN_NS_END @@ -155,6 +157,7 @@ # define GEN_NS_PARSER_BEGIN namespace parser { # define GEN_NS_PARSER_END } # define GEN_USING_NS_PARSER using namespace parser +# define GEN_NS_PARSER parser:: # define GEN_NS :: # define GEN_NS_BEGIN # define GEN_NS_END @@ -162,6 +165,7 @@ #else # define GEN_NS_PARSER_BEGIN namespace parser { # define GEN_NS_PARSER_END } +# define GEN_NS_PARSER parser:: # define GEN_USING_NS_PARSER using namespace parser # define GEN_NS gen:: # define GEN_NS_BEGIN namespace gen { diff --git a/project/dependencies/strings.hpp b/project/dependencies/strings.hpp index 117fd84..e8d7332 100644 --- a/project/dependencies/strings.hpp +++ b/project/dependencies/strings.hpp @@ -208,7 +208,7 @@ struct String String fmt(AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...) { va_list va; va_start(va, fmt); - ssize res = str_fmt_va(buf, buf_size, fmt, va); + ssize res = str_fmt_va(buf, buf_size, fmt, va) - 1; va_end(va); return string_make_length(allocator, buf, res); } @@ -219,7 +219,7 @@ struct String char buf[GEN_PRINTF_MAXLEN] = { 0 }; va_list va; va_start(va, fmt); - ssize res = str_fmt_va(buf, GEN_PRINTF_MAXLEN, fmt, va); + ssize res = str_fmt_va(buf, GEN_PRINTF_MAXLEN, fmt, va) - 1; va_end(va); return string_make_length(allocator, buf, res); } @@ -314,7 +314,7 @@ inline String string_fmt(AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...) { va_list va; va_start(va, fmt); - ssize res = str_fmt_va(buf, buf_size, fmt, va); + ssize res = str_fmt_va(buf, buf_size, fmt, va) - 1; va_end(va); return string_make_length(allocator, buf, res); @@ -328,7 +328,7 @@ String string_fmt_buf(AllocatorInfo allocator, char const* fmt, ...) va_list va; va_start(va, fmt); - ssize res = str_fmt_va(buf, GEN_PRINTF_MAXLEN, fmt, va); + ssize res = str_fmt_va(buf, GEN_PRINTF_MAXLEN, fmt, va) -1; va_end(va); return string_make_length(allocator, buf, res); diff --git a/project/gen.cpp b/project/gen.cpp index 3014698..4e60a3b 100644 --- a/project/gen.cpp +++ b/project/gen.cpp @@ -8,6 +8,49 @@ #include "gen.hpp" +// These are intended for use in the base library of gencpp and the C-variant of the library +// It provides a interoperability between the C++ and C interfacing for containers. (not letting these do any crazy substiution though) +// They are undefined in gen.hpp and gen.cpp at the end of the files. +// We cpp library expects the user to use the regular calls as they can resolve the type fine. + +#define array_init(type, allocator) array_init (allocator ) +#define array_init_reserve(type, allocator, cap) array_init_reserve (allocator, cap) +#define array_append_array(array, other) array_append < get_array_underlying_type(array) > (& array, other ) +#define array_append_value(array, value) array_append < get_array_underlying_type(array) > (& array, value ) +#define array_append_items(array, items, item_num) array_append < get_array_underlying_type(array) > (& array, items, item_num ) +#define array_append_at(array, item, idx ) array_append_at < get_array_underlying_type(array) > (& array, item, idx ) +#define array_append_at_items(array, items, item_num, idx) array_append_at_items< get_array_underlying_type(array) > (& items, item_num, idx ) +#define array_back(array) array_back < get_array_underlying_type(array) > (array ) +#define array_clear(array) array_clear < get_array_underlying_type(array) > (array ) +#define array_fill(array, begin, end, value) array_fill < get_array_underlying_type(array) > (array, begin, end, value ) +#define array_free(array) array_free < get_array_underlying_type(array) > (& array ) +#define arary_grow(array, min_capacity) arary_grow < get_array_underlying_type(array) > (& array, min_capacity) +#define array_num(array) array_num < get_array_underlying_type(array) > (array ) +#define arary_pop(array) arary_pop < get_array_underlying_type(array) > (array ) +#define arary_remove_at(array, idx) arary_remove_at < get_array_underlying_type(array) > (idx) +#define arary_reserve(array, new_capacity) arary_reserve < get_array_underlying_type(array) > (& array, new_capacity ) +#define arary_resize(array, num) arary_resize < get_array_underlying_type(array) > (& array, num) +#define arary_set_capacity(new_capacity) arary_set_capacity < get_array_underlying_type(array) > (& array, new_capacity ) +#define arary_get_header(array) arary_get_header < get_array_underlying_type(array) > (array ) + +#define hashtable_init(type, allocator) hashtable_init (allocator) +#define hashtable_init_reserve(type, allocator, num) hashtable_init_reserve(allocator, num) +#define hashtable_clear(table) hashtable_clear (table) +#define hashtable_destroy(table) hashtable_destroy (& table) +#define hashtable_get(table, key) hashtable_get (table, key) +#define hashtable_grow(table) hashtable_grow (& table) +#define hashtable_rehash(table, new_num) hashtable_rehash (& table, new_num) +#define hashtable_rehash_fast(table) hashtable_rehash_fast (table) +#define hashtable_remove(table, key) hashtable_remove (table, key) +#define hashtable_remove_entry(table, idx) hashtable_remove_entry(table, idx) +#define hashtable_set(table, key, value) hashtable_set (& table, key, value) +#define hashtable_slot(table, key) hashtable_slot (table, key) +#define hashtable_add_entry(table, key) hashtable_add_entry (& table, key) +#define hashtable_find(table, key) hashtable_find (table, key) +#define hashtable_full(table) hashtable_full (table) +#define hashtable_map(table, map_proc) hashtable_map (table, map_proc) +#define hashtable_map_mut(table, map_proc) hashtable_map_mut (table, map_proc) + //! If its desired to roll your own dependencies, define GEN_ROLL_OWN_DEPENDENCIES before including this file. //! Dependencies are derived from the c-zpl library: https://github.com/zpl-c/zpl #ifndef GEN_ROLL_OWN_DEPENDENCIES @@ -33,3 +76,41 @@ GEN_NS_BEGIN GEN_NS_END #include "helpers/pop_ignores.inline.hpp" + +#undef array_init +#undef array_init_reserve +#undef array_append_array +#undef array_append_value +#undef array_append_items +#undef array_append_at +#undef array_append_at +#undef array_back +#undef array_clear +#undef array_fill +#undef array_free +#undef arary_grow +#undef array_num +#undef arary_pop +#undef arary_remove_at +#undef arary_reserve +#undef arary_resize +#undef arary_set_capacity +#undef arary_get_header + +#undef hashtable_init +#undef hashtable_init_reserve +#undef hashtable_clear +#undef hashtable_destroy +#undef hashtable_get +#undef hashtable_grow +#undef hashtable_rehash +#undef hashtable_rehash_fast +#undef hashtable_remove +#undef hashtable_remove_entry +#undef hashtable_set +#undef hashtable_slot +#undef hashtable_add_entry +#undef hashtable_find +#undef hashtable_full +#undef hashtable_map +#undef hashtable_map_mut diff --git a/project/gen.hpp b/project/gen.hpp index 4a4a1b2..eafb8cd 100644 --- a/project/gen.hpp +++ b/project/gen.hpp @@ -31,3 +31,41 @@ GEN_NS_BEGIN GEN_NS_END #include "helpers/pop_ignores.inline.hpp" + +#undef array_init +#undef array_init_reserve +#undef array_append_array +#undef array_append_value +#undef array_append_items +#undef array_append_at +#undef array_append_at +#undef array_back +#undef array_clear +#undef array_fill +#undef array_free +#undef arary_grow +#undef array_num +#undef arary_pop +#undef arary_remove_at +#undef arary_reserve +#undef arary_resize +#undef arary_set_capacity +#undef arary_get_header + +#undef hashtable_init +#undef hashtable_init_reserve +#undef hashtable_clear +#undef hashtable_destroy +#undef hashtable_get +#undef hashtable_grow +#undef hashtable_rehash +#undef hashtable_rehash_fast +#undef hashtable_remove +#undef hashtable_remove_entry +#undef hashtable_set +#undef hashtable_slot +#undef hashtable_add_entry +#undef hashtable_find +#undef hashtable_full +#undef hashtable_map +#undef hashtable_map_mut diff --git a/project/helpers/push_container_defines.inline.hpp b/project/helpers/push_container_defines.inline.hpp new file mode 100644 index 0000000..3ca8d4c --- /dev/null +++ b/project/helpers/push_container_defines.inline.hpp @@ -0,0 +1,26 @@ +#if GEN_SUPPORT_CPP_MEMBER_FEATURES +// These are intended for use in the base library of gencpp and the C-variant of the library +// It provides a interoperability between the C++ and C implementation of arrays. (not letting these do any crazy substiution though) +// They are undefined in gen.hpp and gen.cpp at the end of the files. +// We cpp library expects the user to use the regular calls as they can resolve the type fine. + +#define array_init(type, allocator) array_init (type, allocator ) +#define array_init_reserve(type, allocator, cap) array_init_reserve(type, allocator, cap) +#define array_append_array(type, array, other) array_append (type, & array, other ) +#define array_append_value(type, array, value) array_append (type, & array, value ) +#define array_append_items(type, array, items, item_num) array_append (type, & array, items, item_num ) +#define array_append_at(type, array, item, idx ) array_append_at (type, & array, item, idx ) +#define array_append_at(type, array, items, item_num, idx) array_append_at (type, & items, item_num, idx ) +#define array_back(type, array ) array_back (type, array ) +#define array_clear(type, array ) array_clear (type, array ) +#define array_fill(type, array, begin, end, value) array_fill (type, array, begin, end, value ) +#define array_free(type, array) array_free (type, & array ) +#define arary_grow(type, array, min_capacity) arary_grow (type, & array, min_capacity) +#define array_num(type, array) array_num (type, array ) +#define arary_pop(type, array) arary_pop (type, array ) +#define arary_remove_at(type, array, idx) arary_remove_at (type, idx) +#define arary_reserve(type, array, new_capacity) arary_reserve (type, & array, new_capacity ) +#define arary_resize(type, array, num) arary_resize (type, & array, num) +#define arary_set_capacity(array, new_capacity) arary_set_capacity(type, & array, new_capacity ) +#define arary_get_header(array) arary_get_header (type, array ) +#endif