From 868b93cdd0fd27bf340ff89584efc59e5696ad42 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sun, 15 Dec 2024 17:52:31 -0500 Subject: [PATCH] bugfixes while testing with unreal (still more needs fixing --- base/auxillary/builder.cpp | 4 +++- base/auxillary/builder.hpp | 13 ++++++----- base/auxillary/scanner.cpp | 4 ++-- base/components/inlines.hpp | 6 ++--- base/components/interface.cpp | 4 ++++ base/components/interface.hpp | 3 +++ base/components/lexer.cpp | 2 +- base/components/parser.cpp | 40 ++++++++++++++++++++++++++++---- base/components/parser_types.hpp | 8 ++++++- base/dependencies/containers.hpp | 6 ++--- base/dependencies/debug.hpp | 2 +- base/dependencies/memory.cpp | 34 +++++++++++++-------------- base/dependencies/memory.hpp | 7 +++--- base/dependencies/strings.hpp | 1 + gen_unreal_engine/unreal.cpp | 18 +++++++++----- 15 files changed, 103 insertions(+), 49 deletions(-) diff --git a/base/auxillary/builder.cpp b/base/auxillary/builder.cpp index 606ab33..02adda8 100644 --- a/base/auxillary/builder.cpp +++ b/base/auxillary/builder.cpp @@ -15,7 +15,9 @@ Builder builder_open( char const* path ) return result; } - result.Buffer = strbuilder_make_reserve( _ctx->Allocator_Temp, _ctx->InitSize_BuilderBuffer ); + Context* ctx = get_context(); + GEN_ASSERT_NOT_NULL(ctx); + result.Buffer = strbuilder_make_reserve( ctx->Allocator_Temp, ctx->InitSize_BuilderBuffer ); // log_fmt("$Builder - Opened file: %s\n", result.File.filename ); return result; diff --git a/base/auxillary/builder.hpp b/base/auxillary/builder.hpp index 1de05c3..1729fe0 100644 --- a/base/auxillary/builder.hpp +++ b/base/auxillary/builder.hpp @@ -25,13 +25,14 @@ Builder builder_open ( char const* path ); void builder_pad_lines ( Builder* builder, s32 num ); void builder_print ( Builder* builder, Code code ); void builder_print_fmt_va( Builder* builder, char const* fmt, va_list va ); -void builder_print_fmt ( Builder* builder, char const* fmt, ... ) { +void builder_write ( Builder* builder ); + +forceinline void builder_print_fmt ( Builder* builder, char const* fmt, ... ) { va_list va; va_start( va, fmt ); builder_print_fmt_va( builder, fmt, va ); va_end( va ); } -void builder_write( Builder* builder ); struct Builder { @@ -56,10 +57,10 @@ struct Builder }; #if GEN_COMPILER_CPP && ! GEN_C_LIKE_CPP -void builder_pad_lines( Builder& builder, s32 num ) { return builder_pad_lines(& builder, num); } -void builder_print ( Builder& builder, Code code ) { return builder_print(& builder, code); } -void builder_write ( Builder& builder ) { return builder_write(& builder ); } -void builder_print_fmt( Builder& builder, char const* fmt, ...) { +forceinline void builder_pad_lines( Builder& builder, s32 num ) { return builder_pad_lines(& builder, num); } +forceinline void builder_print ( Builder& builder, Code code ) { return builder_print(& builder, code); } +forceinline void builder_write ( Builder& builder ) { return builder_write(& builder ); } +forceinline void builder_print_fmt( Builder& builder, char const* fmt, ...) { va_list va; va_start( va, fmt ); builder_print_fmt_va( & builder, fmt, va ); diff --git a/base/auxillary/scanner.cpp b/base/auxillary/scanner.cpp index 94f5e48..6cde239 100644 --- a/base/auxillary/scanner.cpp +++ b/base/auxillary/scanner.cpp @@ -20,7 +20,7 @@ Code scan_file( char const* path ) GEN_FATAL("scan_file: %s is empty", path ); } - StrBuilder str = strbuilder_make_reserve( _ctx->Allocator_Temp, fsize ); + StrBuilder str = strbuilder_make_reserve( get_context()->Allocator_Temp, fsize ); file_read( & file, str, fsize ); strbuilder_get_header(str)->Length = fsize; @@ -117,7 +117,7 @@ Code scan_file( char const* path ) } CodeBody parse_file( const char* path ) { - FileContents file = file_read_contents( _ctx->Allocator_Temp, true, path ); + FileContents file = file_read_contents( get_context()->Allocator_Temp, true, path ); Str content = { (char const*)file.data, file.size }; CodeBody code = parse_global_body( content ); log_fmt("\nParsed: %s\n", path); diff --git a/base/components/inlines.hpp b/base/components/inlines.hpp index b557a4f..87e2710 100644 --- a/base/components/inlines.hpp +++ b/base/components/inlines.hpp @@ -262,9 +262,9 @@ forceinline void define_params_append (CodeDefineParams appendee forceinline CodeDefineParams define_params_get (CodeDefineParams self, s32 idx ) { return (CodeDefineParams) (Code) params_get( cast(CodeParams, self), idx); } forceinline bool define_params_has_entries(CodeDefineParams self) { return params_has_entries( cast(CodeParams, self)); } -CodeDefineParams begin_CodeDefineParams(CodeDefineParams params) { return (CodeDefineParams) (Code) begin_CodeParams( cast(CodeParams, (Code)params)); } -CodeDefineParams end_CodeDefineParams (CodeDefineParams params) { return (CodeDefineParams) (Code) end_CodeParams ( cast(CodeParams, (Code)params)); } -CodeDefineParams next_CodeDefineParams (CodeDefineParams params, CodeDefineParams entry_iter) { return (CodeDefineParams) (Code) next_CodeParams ( cast(CodeParams, (Code)params), cast(CodeParams, (Code)entry_iter)); } +forceinline CodeDefineParams begin_CodeDefineParams(CodeDefineParams params) { return (CodeDefineParams) (Code) begin_CodeParams( cast(CodeParams, (Code)params)); } +forceinline CodeDefineParams end_CodeDefineParams (CodeDefineParams params) { return (CodeDefineParams) (Code) end_CodeParams ( cast(CodeParams, (Code)params)); } +forceinline CodeDefineParams next_CodeDefineParams (CodeDefineParams params, CodeDefineParams entry_iter) { return (CodeDefineParams) (Code) next_CodeParams ( cast(CodeParams, (Code)params), cast(CodeParams, (Code)entry_iter)); } #if GEN_COMPILER_CPP forceinline diff --git a/base/components/interface.cpp b/base/components/interface.cpp index 83e44e6..ab2f580 100644 --- a/base/components/interface.cpp +++ b/base/components/interface.cpp @@ -378,6 +378,10 @@ void deinit(Context* ctx) -- context_counter; } +Context* get_context() { + return _ctx; +} + void reset(Context* ctx) { s32 index = 0; diff --git a/base/components/interface.hpp b/base/components/interface.hpp index cb70376..0ee2422 100644 --- a/base/components/interface.hpp +++ b/base/components/interface.hpp @@ -100,6 +100,9 @@ GEN_API void init(Context* ctx); // However on Windows at least, it doesn't need to occur as the OS will clean up after the process. GEN_API void deinit(Context* ctx); +// Retrieves the active context (not usually needed, but here in case...) +GEN_API Context* get_context(); + // Clears the allocations, but doesn't free the memoery, then calls init() again. // Ease of use. GEN_API void reset(Context* ctx); diff --git a/base/components/lexer.cpp b/base/components/lexer.cpp index 1d7f4ee..07cf62f 100644 --- a/base/components/lexer.cpp +++ b/base/components/lexer.cpp @@ -155,7 +155,7 @@ s32 lex_preprocessor_define( LexContext* ctx ) array_append( _ctx->Lexer_Tokens, opening_paren ); move_forward(); - Token last_parameter; + Token last_parameter = {}; // We need to tokenize the define's arguments now: while( ctx->left && * ctx->scanner != ')') { diff --git a/base/components/parser.cpp b/base/components/parser.cpp index be520ba..47b5813 100644 --- a/base/components/parser.cpp +++ b/base/components/parser.cpp @@ -2490,7 +2490,40 @@ Code parse_operator_function_or_variable( bool expects_function, CodeAttributes // Example : // idx +1 +2 bool detected_comma = _ctx->parser.Tokens.Arr[ _ctx->parser.Tokens.Idx + 2 ].Type == Tok_Comma; - if ( detected_capture && ! detected_comma ) + + b32 detected_non_varadic_unpaired_param = detected_comma && nexttok.Type != Tok_Varadic_Argument; + if (! detected_non_varadic_unpaired_param && nexttok.Type == Tok_Preprocess_Macro_Expr) for( s32 break_scope = 0; break_scope == 0; ++ break_scope) + { + Macro* macro = lookup_macro( nexttok.Text ); + if (macro == nullptr || ! macro_is_functional(* macro)) + break; + + // ( ( + // Idx +1 +2 + s32 idx = _ctx->parser.Tokens.Idx + 1; + s32 level = 0; + + // Find end of the token expression + for ( ; idx < array_num(_ctx->parser.Tokens.Arr); idx++ ) + { + Token tok = _ctx->parser.Tokens.Arr[ idx ]; + + if ( tok.Type == Tok_Capture_Start ) + level++; + else if ( tok.Type == Tok_Capture_End && level > 0 ) + level--; + if (level == 0 && tok.Type == Tok_Capture_End) + break; + } + ++ idx; // Will incremnt to possible comma position + + if ( _ctx->parser.Tokens.Arr[ idx ].Type != Tok_Comma ) + break; + + detected_non_varadic_unpaired_param = true; + } + + if ( detected_capture && ! detected_non_varadic_unpaired_param ) { // Dealing with a function result = cast(Code, parse_function_after_name( ModuleFlag_None, attributes, specifiers, type, name )); @@ -2509,7 +2542,7 @@ Code parse_operator_function_or_variable( bool expects_function, CodeAttributes } } - parser_pop(& _ctx->parser); + parser_pop(& _ctx->parser); return result; } @@ -3002,7 +3035,7 @@ Code parse_simple_preprocess( TokType which ) || str_contains(calling_proc, txt("parse_class_struct_body")) ) { - if (peektok.Type == Tok_Statement_End) + if (left && peektok.Type == Tok_Statement_End) { Token stmt_end = currtok; eat( Tok_Statement_End ); @@ -5015,7 +5048,6 @@ CodeTypedef parser_parse_typedef() // valid_macro |= macro && macro_expects_body(* macro)); // } - Code macro; if ( valid_macro ) #endif { diff --git a/base/components/parser_types.hpp b/base/components/parser_types.hpp index 9275b53..f98291e 100644 --- a/base/components/parser_types.hpp +++ b/base/components/parser_types.hpp @@ -9,7 +9,7 @@ enum TokFlags : u32 { - TF_Operator = bit(0), + TF_Operator = bit(0), TF_Assign = bit(1), TF_Preprocess = bit(2), TF_Preprocess_Cond = bit(3), @@ -152,6 +152,7 @@ TokType macrotype_to_toktype( MacroType type ) { return Tok_Invalid; } +inline Str macrotype_to_str( MacroType type ) { local_persist @@ -216,4 +217,9 @@ b32 macro_expects_body( Macro macro ) { return bitfield_is_set( b16, macro.Flags, MF_Expects_Body ); } +#if GEN_COMPILER_CPP && ! GEN_C_LIKE_CPP +forceinline b32 is_functional( Macro macro ) { return bitfield_is_set( b16, macro.Flags, MF_Functional ); } +forceinline b32 expects_body ( Macro macro ) { return bitfield_is_set( b16, macro.Flags, MF_Expects_Body ); } +#endif + typedef HashTable(Macro) MacroTable; diff --git a/base/dependencies/containers.hpp b/base/dependencies/containers.hpp index 13386e6..a84d148 100644 --- a/base/dependencies/containers.hpp +++ b/base/dependencies/containers.hpp @@ -143,6 +143,7 @@ Array array_init_reserve(AllocatorInfo allocator, ssize capacity) return {rcast(Type*, header + 1)}; } +forceinline usize array_grow_formula(ssize value) { return 2 * value + 8; } @@ -202,7 +203,7 @@ bool array_append_at(Array* array, Type item, usize idx) ArrayHeader* header = array_get_header(* array); ssize slot = idx; - if (slot >= header->Num) + if (slot >= (ssize)(header->Num)) slot = header->Num - 1; if (slot < 0) @@ -354,7 +355,6 @@ bool array_reserve(Array* array, usize new_capacity) { GEN_ASSERT( array != nullptr); GEN_ASSERT(* array != nullptr); - GEN_ASSERT(num > 0) ArrayHeader* header = array_get_header(array); if (header->Capacity < new_capacity) @@ -763,7 +763,7 @@ HashTableFindResult hashtable__find(HashTable table, u64 key) } template forceinline -bool hashtable_full(HashTable table) { +b32 hashtable_full(HashTable table) { GEN_ASSERT_NOT_NULL(table.Hashes); GEN_ASSERT_NOT_NULL(table.Entries); usize critical_load = usize(HashTable_CriticalLoadScale * f32(array_num(table.Hashes))); diff --git a/base/dependencies/debug.hpp b/base/dependencies/debug.hpp index 6870261..858b277 100644 --- a/base/dependencies/debug.hpp +++ b/base/dependencies/debug.hpp @@ -44,7 +44,7 @@ // NOTE: Things that shouldn't happen with a message! #define GEN_PANIC( msg, ... ) GEN_ASSERT_MSG( 0, msg, ##__VA_ARGS__ ) -#if GEN_BULD_DEBUG +#if GEN_BUILD_DEBUG #define GEN_FATAL( ... ) \ do \ { \ diff --git a/base/dependencies/memory.cpp b/base/dependencies/memory.cpp index 3d8ad2d..b38097a 100644 --- a/base/dependencies/memory.cpp +++ b/base/dependencies/memory.cpp @@ -346,25 +346,25 @@ void* arena_allocator_proc( void* allocator_data, AllocType type, ssize size, ss switch ( type ) { case EAllocation_ALLOC : + { + void* end = pointer_add( arena->PhysicalStart, arena->TotalUsed ); + ssize total_size = align_forward_s64( size, alignment ); + + // NOTE: Out of memory + if ( arena->TotalUsed + total_size > (ssize) arena->TotalSize ) { - void* end = pointer_add( arena->PhysicalStart, arena->TotalUsed ); - ssize total_size = align_forward_s64( size, alignment ); - - // NOTE: Out of memory - if ( arena->TotalUsed + total_size > (ssize) arena->TotalSize ) - { - // zpl__printf_err("%s", "Arena out of memory\n"); - GEN_FATAL("Arena out of memory! (Possibly could not fit for the largest size Arena!!)"); - return nullptr; - } - - ptr = align_forward( end, alignment ); - arena->TotalUsed += total_size; - - if ( flags & ALLOCATOR_FLAG_CLEAR_TO_ZERO ) - zero_size( ptr, size ); + // zpl__printf_err("%s", "Arena out of memory\n"); + GEN_FATAL("Arena out of memory! (Possibly could not fit for the largest size Arena!!)"); } - break; + + + ptr = align_forward( end, alignment ); + arena->TotalUsed += total_size; + + if ( flags & ALLOCATOR_FLAG_CLEAR_TO_ZERO ) + zero_size( ptr, size ); + } + break; case EAllocation_FREE : // NOTE: Free all at once diff --git a/base/dependencies/memory.hpp b/base/dependencies/memory.hpp index c28cb09..ff04129 100644 --- a/base/dependencies/memory.hpp +++ b/base/dependencies/memory.hpp @@ -229,7 +229,6 @@ forceinline void check(Arena& arena) { return arena_check(& arena); } #pragma pop_macro("check") #endif - inline AllocatorInfo arena_allocator_info( Arena* arena ) { GEN_ASSERT(arena != nullptr); @@ -392,9 +391,9 @@ void pool_clear(Pool* pool); void pool_free(Pool* pool); #if GEN_COMPILER_CPP && ! GEN_C_LIKE_CPP -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); } +forceinline AllocatorInfo allocator_info(Pool& pool) { return pool_allocator_info(& pool); } +forceinline void clear(Pool& pool) { return pool_clear(& pool); } +forceinline void free(Pool& pool) { return pool_free(& pool); } #endif struct Pool diff --git a/base/dependencies/strings.hpp b/base/dependencies/strings.hpp index 3c60b68..76b7ce1 100644 --- a/base/dependencies/strings.hpp +++ b/base/dependencies/strings.hpp @@ -392,6 +392,7 @@ bool strbuilder_append_string(StrBuilder* str, StrBuilder const other) { return strbuilder_append_c_str_len(str, (char const*)other, strbuilder_length(other)); } +inline bool strbuilder_append_fmt(StrBuilder* str, char const* fmt, ...) { GEN_ASSERT(str != nullptr); ssize res; diff --git a/gen_unreal_engine/unreal.cpp b/gen_unreal_engine/unreal.cpp index 5719a7c..2afa5ae 100644 --- a/gen_unreal_engine/unreal.cpp +++ b/gen_unreal_engine/unreal.cpp @@ -147,6 +147,7 @@ int gen_main() Code strings = scan_file( path_base "dependencies/strings.hpp" ); Code filesystem = scan_file( path_base "dependencies/filesystem.hpp" ); Code timing = scan_file( path_base "dependencies/timing.hpp" ); + Code parsing = scan_file( path_base "dependencies/parsing.hpp" ); Builder header = Builder::open("gen/gen.dep.hpp"); @@ -168,6 +169,7 @@ int gen_main() header.print( strings ); header.print( filesystem ); header.print( timing ); + header.print(parsing); header.print_fmt( "\nGEN_NS_END\n" ); header.print( fmt_newline ); @@ -186,6 +188,7 @@ int gen_main() Code strings = scan_file( path_base "dependencies/strings.cpp" ); Code filesystem = scan_file( path_base "dependencies/filesystem.cpp" ); Code timing = scan_file( path_base "dependencies/timing.cpp" ); + Code parsing = scan_file( path_base "dependencies/parsing.cpp" ); Builder src = Builder::open( "gen/gen.dep.cpp" ); @@ -204,6 +207,7 @@ int gen_main() src.print( strings ); src.print( filesystem ); src.print( timing ); + src.print( parsing ); src.print_fmt( "\nGEN_NS_END\n" ); src.print( fmt_newline ); @@ -310,14 +314,20 @@ int gen_main() src.print_fmt( "\n#pragma region Interface\n" ); src.print( interface ); + src.print( upfront ); + src.print_fmt( "\n#pragma region Parsing\n\n" ); src.print( lexer ); src.print( parser_case_macros ); src.print( parser ); src.print( parsing_interface ); - src.print( untyped ); src.print_fmt( "\n#pragma endregion Parsing\n\n" ); + + src.print_fmt( "\n#pragma region Untyped\n\n" ); + src.print( untyped ); + src.print_fmt( "#pragma endregion \n\n" ); + src.print_fmt( "#pragma endregion Interface\n\n" ); src.print_fmt( "GEN_NS_END\n\n"); @@ -364,7 +374,6 @@ int gen_main() // gen_scanner.hpp { - Code parsing = scan_file( path_base "dependencies/parsing.hpp" ); Code scanner = scan_file( path_base "auxillary/scanner.hpp" ); Builder @@ -375,7 +384,6 @@ int gen_main() header.print( fmt_newline ); header.print( def_include( txt("gen.hpp") ) ); header.print_fmt( "\nGEN_NS_BEGIN\n" ); - header.print( parsing ); header.print( scanner ); header.print_fmt( "\nGEN_NS_END\n" ); header.print( fmt_newline ); @@ -385,7 +393,6 @@ int gen_main() // gen.scanner.cpp { - Code parsing = scan_file( path_base "dependencies/parsing.cpp" ); Code scanner = scan_file( path_base "auxillary/scanner.cpp" ); Builder @@ -395,8 +402,7 @@ int gen_main() src.print( fmt_newline ); src.print( def_include( txt("gen.scanner.hpp") ) ); src.print_fmt( "\nGEN_NS_BEGIN\n" ); - src.print( parsing ); - // src.print( scanner ); + src.print( scanner ); src.print_fmt( "\nGEN_NS_END\n" ); src.print( fmt_newline ); src.print( pop_ignores );