From 8ef982003af5e50d20b5dbe7c0069cbe707feb4d Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sun, 1 Dec 2024 12:48:58 -0500 Subject: [PATCH] Added is_body to AST and Code types --- gen_c_library/c_library.cpp | 133 ++++++++++++------ gen_c_library/components/containers.array.hpp | 2 +- gen_c_library/components/misc.hpp | 6 +- project/components/parser.cpp | 1 + project/dependencies/containers.hpp | 7 +- project/dependencies/macros.hpp | 2 +- project/dependencies/memory.hpp | 24 +--- project/dependencies/platform.hpp | 2 +- project/dependencies/strings.hpp | 7 +- 9 files changed, 109 insertions(+), 75 deletions(-) diff --git a/gen_c_library/c_library.cpp b/gen_c_library/c_library.cpp index 7ee7d43..d22110c 100644 --- a/gen_c_library/c_library.cpp +++ b/gen_c_library/c_library.cpp @@ -90,6 +90,7 @@ CodeBody parse_file( const char* path ) { FileContents file = file_read_contents( GlobalAllocator, true, path ); CodeBody code = parse_global_body( { file.size, (char const*)file.data } ); + log_fmt("\nParsed: %s\n", path); return code; } @@ -126,7 +127,7 @@ int gen_main() header.print( debug ); CodeBody parsed_memory = parse_file( project_dir "dependencies/memory.hpp" ); - CodeBody memory = def_body(ECode::Struct_Body); + CodeBody memory = def_body(ECode::Global_Body); for ( Code entry = parsed_memory.begin(); entry != parsed_memory.end(); ++ entry ) { switch (entry->Type) @@ -145,12 +146,32 @@ int gen_main() CodeFn fn = entry.cast(); s32 constexpr_found = fn->Specs.remove( ESpecifier::Constexpr ); if (constexpr_found > -1) { - log_fmt("Found constexpr proc\n"); + log_fmt("Found constexpr: %S\n", entry->to_string()); fn->Specs.append(ESpecifier::Inline); } memory.append(entry); } break; + case ECode::Template: + { + CodeTemplate tmpl = entry.cast(); + if ( tmpl->Declaration->Name.contains(txt("swap"))) + { + CodeBody macro_swap = parse_global_body( txt(R"( +#define swap( a, b ) \ + do \ + { \ + typeof( a ) temp = ( a ); \ + ( a ) = ( b ); \ + ( b ) = temp; \ + } while ( 0 ) +)" + )); + memory.append(macro_swap); + log_fmt( "\nmacro swap: %S\n", macro_swap.to_string() ); + } + } + break; case ECode::Class: case ECode::Struct: { @@ -175,17 +196,26 @@ int gen_main() break; case ECode::Preprocess_If: { - ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), entry, parsed_memory ); + b32 found = ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), entry, parsed_memory ); + if (found) break; + + memory.append(entry); } break; case ECode::Preprocess_IfDef: { - ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_memory ); + b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_memory ); + if (found) break; + + memory.append(entry); } break; case ECode::Preprocess_Pragma: { - swap_pragma_region_implementation( txt("FixedArena"), gen_fixed_arenas, entry, memory); + b32 found = swap_pragma_region_implementation( txt("FixedArena"), gen_fixed_arenas, entry, memory); + if (found) break; + + memory.append(entry); } break; default: { @@ -194,69 +224,84 @@ int gen_main() break; } } - - header.print( memory ); + header.print( dump_to_scratch_and_retireve(memory) ); Code string_ops = scan_file( project_dir "dependencies/string_ops.hpp" ); header.print( string_ops ); CodeBody printing_parsed = parse_file( project_dir "dependencies/printing.hpp" ); - CodeBody printing = def_body(ECode::Struct_Body); + CodeBody printing = def_body(ECode::Global_Body); for ( Code entry = printing_parsed.begin(); entry != printing_parsed.end(); ++ entry ) { switch (entry->Type) { case ECode::Preprocess_IfDef: { - ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, printing_parsed ); + b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, printing_parsed ); + if (found) break; + + printing.append(entry); } - } - - if (entry->Type == ECode::Variable && - contains(entry->Name, txt("Msg_Invalid_Value"))) - { - CodeDefine define = def_define(entry->Name, entry->Value->Content); - printing.append(define); - continue; - } - printing.append(entry); - } - - header.print(printing); - - CodeBody parsed_containers = parse_file( project_dir "dependencies/containers.hpp" ); - CodeBody containers = def_body(ECode::Struct_Body); - for ( Code entry = parsed_containers.begin(); entry != parsed_containers.end(); ++ entry ) - { - switch ( entry->Type ) - { - case ECode::Preprocess_Pragma: + break; + case ECode::Variable: { - bool found = false; - - found = swap_pragma_region_implementation( txt("Array"), gen_array_base, entry, containers); - if (found) { - break; + if (contains(entry->Name, txt("Msg_Invalid_Value"))) + { + CodeDefine define = def_define(entry->Name, entry->Value->Content); + printing.append(define); + continue; } - - found = swap_pragma_region_implementation( txt("HashTable"), gen_hashtable_base, entry, containers); - if (found) { - break; - } - - containers.append(entry); + printing.append(entry); } break; + default: + printing.append(entry); + break; } } + header.print(dump_to_scratch_and_retireve(printing)); - header.print(containers); + CodeBody containers = def_body(ECode::Global_Body); + { + containers.append( def_pragma(code(region Containers))); + + containers.append( gen_array_base() ); + containers.append( gen_hashtable_base() ); + + containers.append( def_pragma(code(endregion Containers))); + } + header.print(fmt_newline); + header.print(dump_to_scratch_and_retireve(containers)); + + Code hashing = scan_file( project_dir "dependencies/hashing.hpp" ); + header.print( hashing ); + + CodeBody parsed_strings = parse_file( project_dir "dependencies/strings.hpp" ); + CodeBody strings = def_body(ECode::Global_Body); + for ( Code entry = parsed_strings.begin(); entry != parsed_strings.end(); ++ entry ) + { + switch (entry->Type) + { + case ECode::Preprocess_IfDef: + { + ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_strings ); + } + break; + + default: + strings.append(entry); + break; + } + } + header.print(dump_to_scratch_and_retireve(strings)); + + header.print_fmt( roll_own_dependencies_guard_end ); } header.print( pop_ignores ); header.write(); - format_file( "gen/gen.h" ); + // format_file( "gen/gen.h" ); gen::deinit(); return 0; diff --git a/gen_c_library/components/containers.array.hpp b/gen_c_library/components/containers.array.hpp index 45dc684..c374258 100644 --- a/gen_c_library/components/containers.array.hpp +++ b/gen_c_library/components/containers.array.hpp @@ -19,7 +19,7 @@ CodeBody gen_array_base() // Code grow_formula = untyped_str( txt( "#define gen_array_grow_formula( value ) ( 2 * value + 8 )\n" )); Code get_header = untyped_str( txt( "#define array_get_header( Type, self ) ( (ArrayHeader*)( self ) - 1)\n" )); - return def_global_body( args( td_header, header, get_header ) ); + return def_global_body( args( fmt_newline, td_header, header, get_header, fmt_newline ) ); }; CodeBody gen_array( StrC type, StrC array_name ) diff --git a/gen_c_library/components/misc.hpp b/gen_c_library/components/misc.hpp index 7280231..109f42b 100644 --- a/gen_c_library/components/misc.hpp +++ b/gen_c_library/components/misc.hpp @@ -7,9 +7,13 @@ using SwapContentProc = CodeBody(void); b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& body ) { + b32 found = false; CodePreprocessCond cond = entry_iter.cast(); if ( cond->Content.contains(cond_sig) ) { + log_fmt("Preprocess cond found: %S\n", cond->Content); + found = true; + s32 depth = 1; ++ entry_iter; for(b32 continue_for = true; continue_for && entry_iter != body.end(); ) switch (entry_iter->Type) { @@ -34,7 +38,7 @@ b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& bod } } - return entry_iter != body.end(); + return found; } bool swap_pragma_region_implementation( StrC region_name, SwapContentProc* swap_content, Code& entry_iter, CodeBody& body ) diff --git a/project/components/parser.cpp b/project/components/parser.cpp index 9b9e853..7801ab0 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -4406,6 +4406,7 @@ CodeTemplate parse_template() result->Params = params; result->Declaration = definition; result->ModuleFlags = mflags; + // result->Name = definition->Name; Context.pop(); return result; diff --git a/project/dependencies/containers.hpp b/project/dependencies/containers.hpp index 948c0d1..66c6565 100644 --- a/project/dependencies/containers.hpp +++ b/project/dependencies/containers.hpp @@ -14,20 +14,17 @@ template using TRemoveConst = typename RemoveConst::Type; #pragma region Array -#if ! GEN_COMPILER_C #define Array(Type) Array // #define array_init(Type, ...) array_init (__VA_ARGS__) // #define array_init_reserve(Type, ...) array_init_reserve(__VA_ARGS__) -#endif struct ArrayHeader; #if GEN_SUPPORT_CPP_MEMBER_FEATURES -template struct Array; + template struct Array; #else -template -using Array = Type*; + template using Array = Type*; #endif usize array_grow_formula(ssize value); diff --git a/project/dependencies/macros.hpp b/project/dependencies/macros.hpp index a42c141..791ad1e 100644 --- a/project/dependencies/macros.hpp +++ b/project/dependencies/macros.hpp @@ -205,7 +205,7 @@ # define foreach(Type, entry_id, iterable) for ( Type entry_id : iterable ) #endif -#if GENC_COMPILERC +#if GEN_COMPILER_C # if __STDC_VERSION__ >= 202311L # define enum_underlying(type) : type # else diff --git a/project/dependencies/memory.hpp b/project/dependencies/memory.hpp index 545e337..aeae598 100644 --- a/project/dependencies/memory.hpp +++ b/project/dependencies/memory.hpp @@ -14,23 +14,13 @@ #define GEN__HIGHS ( GEN__ONES * ( GEN_U8_MAX / 2 + 1 ) ) #define GEN__HAS_ZERO( x ) ( ( ( x ) - GEN__ONES ) & ~( x ) & GEN__HIGHS ) -#if ! GEN_COMPILER_C - template< class Type > - void swap( Type& a, Type& b ) - { - Type tmp = a; - a = b; - b = tmp; - } -#else - #define swap( a, b ) \ - do { \ - typeof(a) \ - temp = (a); \ - (a) = (b); \ - (b) = temp; \ - } while(0) -#endif +template< class Type > +void swap( Type& a, Type& b ) +{ + Type tmp = a; + a = b; + b = tmp; +} //! Checks if value is power of 2. b32 is_power_of_two( ssize x ); diff --git a/project/dependencies/platform.hpp b/project/dependencies/platform.hpp index 1e9a173..583b260 100644 --- a/project/dependencies/platform.hpp +++ b/project/dependencies/platform.hpp @@ -106,7 +106,7 @@ # define GEN_GCC_VERSION_CHECK(major,minor,patch) (0) #endif -#ifndef GEN_COMPIELR_C +#ifndef GEN_COMPILER_C # if defined(__STDC_VERSION__) # define GEN_COMPILER_C 1 # else diff --git a/project/dependencies/strings.hpp b/project/dependencies/strings.hpp index 77e9f8f..946a610 100644 --- a/project/dependencies/strings.hpp +++ b/project/dependencies/strings.hpp @@ -11,8 +11,10 @@ struct StrC ssize Len; char const* Ptr; +#if ! GEN_COMPILER_C operator char const* () const { return Ptr; } char const& operator[]( ssize index ) const { return Ptr[index]; } +#endif }; #define cast_to_strc( str ) * rcast( StrC*, (str) - sizeof(ssize) ) @@ -29,12 +31,7 @@ StrC to_str( char const* str ) { // I kept it for simplicty of porting but its not necessary to keep it that way. #pragma region String struct StringHeader; - -#if GEN_COMPILER_C -typedef char* String; -#else struct String; -#endif String string_make(AllocatorInfo allocator, char const* str); String string_make(AllocatorInfo allocator, StrC str);