First compiling version of operator overloading for C! (on both msvc and clang using -std=c11 flag, using _Generic selection with some helper macros)

Extremely satsified with how unofuscated the generated code is for _Generic.
Still fixing up the templated container code though in the c-codegen
This commit is contained in:
Edward R. Gonzalez 2024-12-05 17:04:17 -05:00
parent 47b9c37e94
commit a3407c14d5
15 changed files with 424 additions and 263 deletions

View File

@ -118,22 +118,33 @@ int gen_main()
{ {
header.print( c_library_header_start ); header.print( c_library_header_start );
#pragma region Scan, Parse, and Generate Components
Code types = scan_file( project_dir "components/types.hpp" );
Code ast = scan_file( project_dir "components/ast.hpp" );
Code ast_types = scan_file( project_dir "components/ast_types.hpp" );
Code code_types = scan_file( project_dir "components/code_types.hpp" );
Code interface = scan_file( project_dir "components/interface.hpp" );
Code inlines = scan_file( project_dir "components/inlines.hpp" );
Code header_end = scan_file( project_dir "components/header_end.hpp" );
CodeBody ecode = gen_ecode ( project_dir "enums/ECode.csv" );
CodeBody eoperator = gen_eoperator ( project_dir "enums/EOperator.csv" );
CodeBody especifier = gen_especifier( project_dir "enums/ESpecifier.csv" );
CodeBody ast_inlines = gen_ast_inlines();
#pragma endregion Scan, Parse, and Generate Components
#pragma region Scan, Parse, and Generate Dependencies
Code platform = scan_file( project_dir "dependencies/platform.hpp" ); Code platform = scan_file( project_dir "dependencies/platform.hpp" );
Code macros = scan_file( project_dir "dependencies/macros.hpp" ); Code macros = scan_file( project_dir "dependencies/macros.hpp" );
Code basic_types = scan_file( project_dir "dependencies/basic_types.hpp" ); Code basic_types = scan_file( project_dir "dependencies/basic_types.hpp" );
Code debug = scan_file( project_dir "dependencies/debug.hpp" ); Code debug = scan_file( project_dir "dependencies/debug.hpp" );
Code string_ops = scan_file( project_dir "dependencies/string_ops.hpp" );
header.print_fmt( roll_own_dependencies_guard_start ); Code hashing = scan_file( project_dir "dependencies/hashing.hpp" );
header.print( platform ); Code filesystem = scan_file( project_dir "dependencies/filesystem.hpp" );
header.print_fmt( "\nGEN_NS_BEGIN\n" ); Code timing = scan_file( project_dir "dependencies/timing.hpp" );
header.print( macros );
header.print( basic_types );
header.print( debug );
CodeBody parsed_memory = parse_file( project_dir "dependencies/memory.hpp" ); CodeBody parsed_memory = parse_file( project_dir "dependencies/memory.hpp" );
CodeBody memory = def_body(CT_Global_Body); CodeBody memory = def_body(CT_Global_Body);
for ( Code entry = parsed_memory.begin(); entry != parsed_memory.end(); ++ entry ) for ( Code entry = parsed_memory.begin(); entry != parsed_memory.end(); ++ entry )
{ {
switch (entry->Type) switch (entry->Type)
@ -252,11 +263,6 @@ int gen_main()
} }
} }
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_parsed = parse_file( project_dir "dependencies/printing.hpp" );
CodeBody printing = def_body(CT_Global_Body); CodeBody printing = def_body(CT_Global_Body);
for ( Code entry = printing_parsed.begin(); entry != printing_parsed.end(); ++ entry ) for ( Code entry = printing_parsed.begin(); entry != printing_parsed.end(); ++ entry )
@ -287,27 +293,6 @@ int gen_main()
break; break;
} }
} }
header.print(dump_to_scratch_and_retireve(printing));
CodeBody containers = def_body(CT_Global_Body);
{
containers.append( def_pragma(code(region Containers)));
containers.append( gen_array_base() );
containers.append( gen_hashtable_base() );
containers.append(fmt_newline);
CodeBody array_ssize = gen_array(txt("ssize"), txt("Array_ssize"));
containers.append(array_ssize);
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 parsed_strings = parse_file( project_dir "dependencies/strings.hpp" );
CodeBody strings = def_body(CT_Global_Body); CodeBody strings = def_body(CT_Global_Body);
@ -386,7 +371,7 @@ int gen_main()
CodeTypedef td = cast(CodeTypedef, entry); CodeTypedef td = cast(CodeTypedef, entry);
if (td->Name.contains(name_string_table)) if (td->Name.contains(name_string_table))
{ {
CodeBody ht = gen_hashtable(name_string_table, name_string_table); CodeBody ht = gen_hashtable(txt("StrC"), name_string_table);
strings.append(ht); strings.append(ht);
strings.append(td); strings.append(td);
break; break;
@ -400,30 +385,49 @@ int gen_main()
break; break;
} }
} }
header.print(dump_to_scratch_and_retireve(strings));
Code filesystem = scan_file( project_dir "dependencies/filesystem.hpp" ); CodeBody containers = def_body(CT_Global_Body);
Code timing = scan_file( project_dir "dependencies/timing.hpp" ); {
CodeBody array_ssize = gen_array(txt("ssize"), txt("Array_ssize"));
containers.append( def_pragma(code(region Containers)));
// At this point all arrays required should have been defined so its safe to generate the generic selectors.
containers.append( gen_array_base() );
containers.append( gen_array_generic_selection_interface());
containers.append( gen_hashtable_base() );
containers.append(fmt_newline);
containers.append(array_ssize);
containers.append( def_pragma(code(endregion Containers)));
containers.append(fmt_newline);
}
#pragma endregion Scan, Parse, and Generate Dependencies
#pragma region Print Dependencies
header.print_fmt( roll_own_dependencies_guard_start );
header.print( platform );
header.print_fmt( "\nGEN_NS_BEGIN\n" );
header.print( macros );
header.print( basic_types );
header.print( debug );
header.print( dump_to_scratch_and_retireve(memory) );
header.print( dump_to_scratch_and_retireve(printing));
header.print( string_ops );
header.print( dump_to_scratch_and_retireve(containers));
header.print( hashing );
header.print( dump_to_scratch_and_retireve(strings));
// header.print( filesystem ); // header.print( filesystem );
// header.print( timing ); // header.print( timing );
header.print_fmt( "\nGEN_NS_END\n" ); header.print_fmt( "\nGEN_NS_END\n" );
header.print_fmt( roll_own_dependencies_guard_end ); header.print_fmt( roll_own_dependencies_guard_end );
#pragma endregion Print Dependencies
Code types = scan_file( project_dir "components/types.hpp" );
Code ast = scan_file( project_dir "components/ast.hpp" );
Code ast_types = scan_file( project_dir "components/ast_types.hpp" );
Code code_types = scan_file( project_dir "components/code_types.hpp" );
Code interface = scan_file( project_dir "components/interface.hpp" );
Code inlines = scan_file( project_dir "components/inlines.hpp" );
Code header_end = scan_file( project_dir "components/header_end.hpp" );
CodeBody ecode = gen_ecode ( project_dir "enums/ECode.csv" );
CodeBody eoperator = gen_eoperator ( project_dir "enums/EOperator.csv" );
CodeBody especifier = gen_especifier( project_dir "enums/ESpecifier.csv" );
CodeBody ast_inlines = gen_ast_inlines();
#if 0 #if 0
#region region Print Components
header.print_fmt("#pragma region Types\n"); header.print_fmt("#pragma region Types\n");
header.print( types ); header.print( types );
header.print( fmt_newline ); header.print( fmt_newline );
@ -434,6 +438,7 @@ int gen_main()
header.print( dump_to_scratch_and_retireve( especifier )); header.print( dump_to_scratch_and_retireve( especifier ));
header.print( fmt_newline ); header.print( fmt_newline );
header.print_fmt("#pragma endregion Types\n\n"); header.print_fmt("#pragma endregion Types\n\n");
#pragma endregion Print Compoennts
#endif #endif
} }

View File

@ -4,6 +4,9 @@
using namespace gen; using namespace gen;
// Used to know what slot the array will be for generic selection
global s32 ArrayDefinitionCounter = 0;
CodeBody gen_array_base() CodeBody gen_array_base()
{ {
CodeTypedef td_header = parse_typedef( code( typedef struct ArrayHeader ArrayHeader; )); CodeTypedef td_header = parse_typedef( code( typedef struct ArrayHeader ArrayHeader; ));
@ -16,26 +19,31 @@ CodeBody gen_array_base()
}; };
)); ));
// Code grow_formula = untyped_str( txt( "#define gen_array_grow_formula( value ) ( 2 * value + 8 )\n" )); Code grow_formula = untyped_str( txt( "#define array_grow_formula( value ) ( 2 * value + 8 )\n" ));
Code get_header = untyped_str( txt( "#define array_get_header( Type, self ) ( (ArrayHeader*)( self ) - 1)\n" )); Code get_header = untyped_str( txt( "#define array_get_header( self ) ( (ArrayHeader*)( self ) - 1)\n" ));
return def_global_body( args( fmt_newline, td_header, header, get_header, fmt_newline ) ); return def_global_body( args( fmt_newline, td_header, header, grow_formula, get_header, fmt_newline ) );
}; };
CodeBody gen_array( StrC type, StrC array_name ) CodeBody gen_array( StrC type, StrC array_name )
{ {
String array_type = String::fmt_buf( GlobalAllocator, "%.*s", array_name.Len, array_name.Ptr ); String array_type = String::fmt_buf( GlobalAllocator, "%.*s", array_name.Len, array_name.Ptr );
String fn = String::fmt_buf( GlobalAllocator, "%.*s", array_name.Len, array_name.Ptr ); String fn = String::fmt_buf( GlobalAllocator, "%.*s", array_name.Len, array_name.Ptr );
str_to_lower(fn.Data); // str_to_lower(fn.Data);
#pragma push_macro( "GEN_ASSERT" ) #pragma push_macro( "GEN_ASSERT" )
#pragma push_macro( "rcast" )
#pragma push_macro( "cast" )
#undef GEN_ASSERT #undef GEN_ASSERT
#undef rcast
#undef cast
CodeBody result = parse_global_body( token_fmt( "array_type", (StrC)array_type, "fn", (StrC)fn, "type", (StrC)type CodeBody result = parse_global_body( token_fmt( "array_type", (StrC)array_type, "fn", (StrC)fn, "type", (StrC)type
, stringize( , stringize(
typedef <type>* <array_type>; typedef <type>* <array_type>;
<array_type> <fn>_init ( AllocatorInfo allocator ); void <fn>_init ( <array_type>* self, AllocatorInfo allocator );
<array_type> <fn>_init_reserve ( AllocatorInfo allocator, usize capacity ); void <fn>_init_reserve ( <array_type>* self, AllocatorInfo allocator, usize capacity );
bool <fn>_append_array ( <array_type>* self, <array_type> other );
bool <fn>_append ( <array_type>* self, <type> value ); bool <fn>_append ( <array_type>* self, <type> value );
bool <fn>_append_items ( <array_type>* self, <type>* items, usize item_num ); bool <fn>_append_items ( <array_type>* self, <type>* items, usize item_num );
bool <fn>_append_at ( <array_type>* self, <type> item, usize idx ); bool <fn>_append_at ( <array_type>* self, <type> item, usize idx );
@ -51,35 +59,41 @@ CodeBody gen_array( StrC type, StrC array_name )
bool <fn>_resize ( <array_type>* self, usize num ); bool <fn>_resize ( <array_type>* self, usize num );
bool <fn>_set_capacity ( <array_type>* self, usize new_capacity ); bool <fn>_set_capacity ( <array_type>* self, usize new_capacity );
<array_type> <fn>_init( AllocatorInfo allocator ) void <fn>_init( <array_type>* self, AllocatorInfo allocator )
{ {
return <fn>_init_reserve( allocator, array_grow_formula( 0 ) ); size_t initial_size = array_grow_formula(0);
array_init_reserve( * self, allocator, initial_size );
} }
<array_type> <fn>_init_reserve( AllocatorInfo allocator, usize capacity ) void <fn>_init_reserve( <array_type>* self, AllocatorInfo allocator, usize capacity )
{ {
ArrayHeader* header = cast(ArrayHeader*, alloc( allocator, sizeof(ArrayHeader) + sizeof(<type>) * capacity ) ); ArrayHeader* header = rcast(ArrayHeader*, alloc(allocator, sizeof(ArrayHeader) + sizeof(<type>) * capacity));
if ( header == NULL ) if (header == nullptr)
return NULL; self = nullptr;
header->Allocator = allocator; header->Allocator = allocator;
header->Capacity = capacity; header->Capacity = capacity;
header->Num = 0; header->Num = 0;
return cast( <type>*, header + 1 ); self = rcast(<array_type>*, header + 1);
}
bool <fn>_append_array( <array_type>* self, <array_type> other )
{
return array_append_items( * self, (<array_type>)other, <fn>_num(other));
} }
bool <fn>_append( <array_type>* self, <type> value ) bool <fn>_append( <array_type>* self, <type> value )
{ {
ArrayHeader* header = get_header( * self ); ArrayHeader* header = array_get_header( * self );
if ( header->Num == header->Capacity ) if ( header->Num == header->Capacity )
{ {
if ( ! <fn>_grow( self, header->Capacity)) if ( ! array_grow( self, header->Capacity))
return false; return false;
header = get_header( * self ); header = array_get_header( * self );
} }
(* self)[ header->Num ] = value; (* self)[ header->Num ] = value;
@ -90,14 +104,14 @@ CodeBody gen_array( StrC type, StrC array_name )
bool <fn>_append_items( <array_type>* self, <type>* items, usize item_num ) bool <fn>_append_items( <array_type>* self, <type>* items, usize item_num )
{ {
ArrayHeader* header = get_header( * self ); ArrayHeader* header = array_get_header( * self );
if ( header->Num + item_num > header->Capacity ) if ( header->Num + item_num > header->Capacity )
{ {
if ( ! <fn>_grow( self, header->Capacity + item_num )) if ( ! array_grow( self, header->Capacity + item_num ))
return false; return false;
header = get_header( * self ); header = array_get_header( * self );
} }
mem_copy( (* self) + header->Num, items, sizeof(<type>) * item_num ); mem_copy( (* self) + header->Num, items, sizeof(<type>) * item_num );
@ -108,7 +122,7 @@ CodeBody gen_array( StrC type, StrC array_name )
bool <fn>_append_at( <array_type>* self, <type> item, usize idx ) bool <fn>_append_at( <array_type>* self, <type> item, usize idx )
{ {
ArrayHeader* header = get_header( * self ); ArrayHeader* header = array_get_header( * self );
if ( idx >= header->Num ) if ( idx >= header->Num )
idx = header->Num - 1; idx = header->Num - 1;
@ -118,10 +132,10 @@ CodeBody gen_array( StrC type, StrC array_name )
if ( header->Capacity < header->Num + 1 ) if ( header->Capacity < header->Num + 1 )
{ {
if ( ! <fn>_grow( self, header->Capacity + 1 ) ) if ( ! array_grow( self, header->Capacity + 1 ) )
return false; return false;
header = get_header( * self ); header = array_get_header( * self );
} }
<array_type> target = (* self) + idx; <array_type> target = (* self) + idx;
@ -134,19 +148,19 @@ CodeBody gen_array( StrC type, StrC array_name )
bool <fn>_append_items_at( <array_type>* self, <type>* items, usize item_num, usize idx ) bool <fn>_append_items_at( <array_type>* self, <type>* items, usize item_num, usize idx )
{ {
ArrayHeader* header = get_header( * self ); ArrayHeader* header = array_get_header( * self );
if ( idx >= header->Num ) if ( idx >= header->Num )
{ {
return <fn>_append_items( self, items, item_num ); return array_append_items( * self, items, item_num );
} }
if ( item_num > header->Capacity ) if ( item_num > header->Capacity )
{ {
if ( ! <fn>_grow( self, item_num + header->Capacity ) ) if ( ! array_grow( self, item_num + header->Capacity ) )
return false; return false;
header = get_header( * self ); header = array_get_header( * self );
} }
<type>* target = (* self) + idx + item_num; <type>* target = (* self) + idx + item_num;
@ -161,7 +175,7 @@ CodeBody gen_array( StrC type, StrC array_name )
<type>* <fn>_back( <array_type> self ) <type>* <fn>_back( <array_type> self )
{ {
ArrayHeader* header = get_header( self ); ArrayHeader* header = array_get_header( self );
if ( header->Num == 0 ) if ( header->Num == 0 )
return NULL; return NULL;
@ -171,13 +185,13 @@ CodeBody gen_array( StrC type, StrC array_name )
void <fn>_clear( <array_type> self ) void <fn>_clear( <array_type> self )
{ {
ArrayHeader* header = get_header( self ); ArrayHeader* header = array_get_header( self );
header->Num = 0; header->Num = 0;
} }
bool <fn>_fill( <array_type> self, usize begin, usize end, <type> value ) bool <fn>_fill( <array_type> self, usize begin, usize end, <type> value )
{ {
ArrayHeader* header = get_header( self ); ArrayHeader* header = array_get_header( self );
if ( begin < 0 || end >= header->Num ) if ( begin < 0 || end >= header->Num )
return false; return false;
@ -190,30 +204,30 @@ CodeBody gen_array( StrC type, StrC array_name )
void <fn>_free( <array_type> self ) void <fn>_free( <array_type> self )
{ {
ArrayHeader* header = get_header( self ); ArrayHeader* header = array_get_header( self );
free( header->Allocator, header ); allocator_free( header->Allocator, header );
self = NULL; self = NULL;
} }
bool <fn>_grow( <array_type>* self, usize min_capacity ) bool <fn>_grow( <array_type>* self, usize min_capacity )
{ {
ArrayHeader* header = get_header( *self ); ArrayHeader* header = array_get_header( *self );
usize new_capacity = array_grow_formula( header->Capacity ); usize new_capacity = array_grow_formula( header->Capacity );
if ( new_capacity < min_capacity ) if ( new_capacity < min_capacity )
new_capacity = min_capacity; new_capacity = min_capacity;
return <fn>_set_capacity( self, new_capacity ); return array_set_capacity( * self, new_capacity );
} }
usize <fn>_num( <array_type> self ) usize <fn>_num( <array_type> self )
{ {
return get_header(self)->Num; return array_get_header(self)->Num;
} }
<type> <fn>_pop( <array_type> self ) <type> <fn>_pop( <array_type> self )
{ {
ArrayHeader* header = get_header( self ); ArrayHeader* header = array_get_header( self );
GEN_ASSERT( header->Num > 0 ); GEN_ASSERT( header->Num > 0 );
<type> result = self[ header->Num - 1 ]; <type> result = self[ header->Num - 1 ];
@ -223,7 +237,7 @@ CodeBody gen_array( StrC type, StrC array_name )
void <fn>_remove_at( <array_type> self, usize idx ) void <fn>_remove_at( <array_type> self, usize idx )
{ {
ArrayHeader* header = get_header( self ); ArrayHeader* header = array_get_header( self );
GEN_ASSERT( idx < header->Num ); GEN_ASSERT( idx < header->Num );
mem_move( self + idx, self + idx + 1, sizeof( <type> ) * ( header->Num - idx - 1 ) ); mem_move( self + idx, self + idx + 1, sizeof( <type> ) * ( header->Num - idx - 1 ) );
@ -232,24 +246,24 @@ CodeBody gen_array( StrC type, StrC array_name )
bool <fn>_reserve( <array_type>* self, usize new_capacity ) bool <fn>_reserve( <array_type>* self, usize new_capacity )
{ {
ArrayHeader* header = get_header( * self ); ArrayHeader* header = array_get_header( * self );
if ( header->Capacity < new_capacity ) if ( header->Capacity < new_capacity )
return <fn>_set_capacity( self, new_capacity ); return array_set_capacity( * self, new_capacity );
return true; return true;
} }
bool <fn>_resize( <array_type>* self, usize num ) bool <fn>_resize( <array_type>* self, usize num )
{ {
ArrayHeader* header = get_header( * self ); ArrayHeader* header = array_get_header( * self );
if ( header->Capacity < num ) if ( header->Capacity < num )
{ {
if ( ! <fn>_grow( self, num ) ) if ( ! array_grow( self, num ) )
return false; return false;
header = get_header( * self ); header = array_get_header( * self );
} }
header->Num = num; header->Num = num;
@ -258,7 +272,7 @@ CodeBody gen_array( StrC type, StrC array_name )
bool <fn>_set_capacity( <array_type>* self, usize new_capacity ) bool <fn>_set_capacity( <array_type>* self, usize new_capacity )
{ {
ArrayHeader* header = get_header( * self ); ArrayHeader* header = array_get_header( * self );
if ( new_capacity == header->Capacity ) if ( new_capacity == header->Capacity )
return true; return true;
@ -273,7 +287,7 @@ CodeBody gen_array( StrC type, StrC array_name )
return false; return false;
mem_move( new_header, header, sizeof( ArrayHeader ) + sizeof( <type> ) * header->Num ); mem_move( new_header, header, sizeof( ArrayHeader ) + sizeof( <type> ) * header->Num );
free( header->Allocator, & header ); allocator_free( header->Allocator, & header );
new_header->Capacity = new_capacity; new_header->Capacity = new_capacity;
* self = cast( <type>*, new_header + 1 ); * self = cast( <type>*, new_header + 1 );
@ -281,10 +295,36 @@ CodeBody gen_array( StrC type, StrC array_name )
} }
))); )));
#pragma pop_macro( "GEN_ASSERT" ) #pragma pop_macro( "GEN_ASSERT" )
#pragma pop_macro( "rcast" )
#pragma pop_macro( "cast" )
++ ArrayDefinitionCounter;
StrC slot_str = String::fmt_buf(GlobalAllocator, "%d", ArrayDefinitionCounter).to_strc();
Code generic_interface_slot = untyped_str(token_fmt( "type_delimiter", (StrC)array_type, "slot", (StrC)slot_str,
R"(#define GENERIC_SLOT_<slot>__array_init <type_delimiter>, <type_delimiter>_init
#define GENERIC_SLOT_<slot>__array_init_reserve <type_delimiter>, <type_delimiter>_init_reserve
#define GENERIC_SLOT_<slot>__array_append <type_delimiter>, <type_delimiter>_append
#define GENERIC_SLOT_<slot>__array_append_items <type_delimiter>, <type_delimiter>_append_items
#define GENERIC_SLOT_<slot>__array_append_at <type_delimiter>, <type_delimiter>_append_at
#define GENERIC_SLOT_<slot>__array_append_items_at <type_delimiter>, <type_delimiter>_append_items_at
#define GENERIC_SLOT_<slot>__array_back <type_delimiter>, <type_delimiter>_back
#define GENERIC_SLOT_<slot>__array_clear <type_delimiter>, <type_delimiter>_clear
#define GENERIC_SLOT_<slot>__array_fill <type_delimiter>, <type_delimiter>_fill
#define GENERIC_SLOT_<slot>__array_grow <type_delimiter>*, <type_delimiter>_grow
#define GENERIC_SLOT_<slot>__array_num <type_delimiter>, <type_delimiter>_num
#define GENERIC_SLOT_<slot>__array_pop <type_delimiter>, <type_delimiter>_pop
#define GENERIC_SLOT_<slot>__array_reserve <type_delimiter>, <type_delimiter>_reserve
#define GENERIC_SLOT_<slot>__array_resize <type_delimiter>, <type_delimiter>_resize
#define GENERIC_SLOT_<slot>__array_set_capacity <type_delimiter>, <type_delimiter>_set_capacity
)"
));
return def_global_body( args( return def_global_body( args(
def_pragma( string_to_strc( string_fmt_buf( GlobalAllocator, "region %S", array_type ))), def_pragma( string_to_strc( string_fmt_buf( GlobalAllocator, "region %S", array_type ))),
fmt_newline, fmt_newline,
generic_interface_slot,
fmt_newline,
result, result,
fmt_newline, fmt_newline,
def_pragma( string_to_strc(string_fmt_buf( GlobalAllocator, "endregion %S", array_type ))), def_pragma( string_to_strc(string_fmt_buf( GlobalAllocator, "endregion %S", array_type ))),
@ -292,4 +332,67 @@ CodeBody gen_array( StrC type, StrC array_name )
)); ));
}; };
// CodeBody gen_ constexpr bool array_by_ref = true;
Code gen_array_generic_selection_function_macro( StrC macro_name, bool by_ref = false )
{
local_persist
String define_builder = String::make_reserve(GlobalAllocator, kilobytes(64));
define_builder.clear();
StrC macro_begin = token_fmt( "macro_name", (StrC)macro_name,
R"(#define <macro_name>(selector_arg, ...) _Generic( \
(selector_arg), /* Select Via Expression*/ \
/* Extendibility slots: */ \
)"
);
define_builder.append(macro_begin);
for ( s32 slot = 1; slot <= ArrayDefinitionCounter; ++ slot )
{
StrC slot_str = String::fmt_buf(GlobalAllocator, "%d", slot).to_strc();
if (slot == ArrayDefinitionCounter)
{
define_builder.append( token_fmt( "macro_name", macro_name, "slot", slot_str,
R"( GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT_LAST( GENERIC_SLOT_<slot>__<macro_name> ) \
)"
));
continue;
}
define_builder.append( token_fmt( "macro_name", macro_name, "slot", slot_str,
R"( GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_<slot>__<macro_name> ) \
)"
));
}
if (by_ref)
define_builder.append(txt(")\tGEN_RESOLVED_FUNCTION_CALL( & selector_arg, __VA_ARGS__ )"));
else
define_builder.append(txt(")\tGEN_RESOLVED_FUNCTION_CALL( selector_arg, __VA_ARGS__ )"));
// Add gap for next definition
define_builder.append(txt("\n\n"));
Code macro = untyped_str(define_builder.to_strc());
return macro;
}
CodeBody gen_array_generic_selection_interface()
{
CodeBody interface_defines = def_body(CT_Global_Body);
interface_defines.append( gen_array_generic_selection_function_macro(txt("array_init"), array_by_ref) );
interface_defines.append( gen_array_generic_selection_function_macro(txt("array_init_reserve"), array_by_ref) );
interface_defines.append( gen_array_generic_selection_function_macro(txt("array_append"), array_by_ref) );
interface_defines.append( gen_array_generic_selection_function_macro(txt("array_append_items"), array_by_ref) );
interface_defines.append( gen_array_generic_selection_function_macro(txt("array_back")) );
interface_defines.append( gen_array_generic_selection_function_macro(txt("array_clear")) );
interface_defines.append( gen_array_generic_selection_function_macro(txt("array_fill")) );
interface_defines.append( gen_array_generic_selection_function_macro(txt("array_free")) );
interface_defines.append( gen_array_generic_selection_function_macro(txt("array_grow")) );
interface_defines.append( gen_array_generic_selection_function_macro(txt("array_num")) );
interface_defines.append( gen_array_generic_selection_function_macro(txt("arary_pop")) );
interface_defines.append( gen_array_generic_selection_function_macro(txt("arary_remove_at")) );
interface_defines.append( gen_array_generic_selection_function_macro(txt("arary_reserve"), array_by_ref) );
interface_defines.append( gen_array_generic_selection_function_macro(txt("array_set_capacity"), array_by_ref) );
return interface_defines;
}

View File

@ -30,10 +30,10 @@ CodeBody gen_hashtable( StrC type, StrC hashtable_name )
String tbl_type = {(char*) hashtable_name.duplicate(GlobalAllocator).Ptr}; String tbl_type = {(char*) hashtable_name.duplicate(GlobalAllocator).Ptr};
String fn = tbl_type.duplicate(GlobalAllocator); String fn = tbl_type.duplicate(GlobalAllocator);
str_to_lower(fn.Data); // str_to_lower(fn.Data);
String name_lower = String::make( GlobalAllocator, hashtable_name ); String name_lower = String::make( GlobalAllocator, hashtable_name );
str_to_lower( name_lower.Data ); // str_to_lower( name_lower.Data );
String hashtable_entry = String::fmt_buf( GlobalAllocator, "HTE_%.*s", hashtable_name.Len, hashtable_name.Ptr ); String hashtable_entry = String::fmt_buf( GlobalAllocator, "HTE_%.*s", hashtable_name.Len, hashtable_name.Ptr );
String entry_array_name = String::fmt_buf( GlobalAllocator, "Arr_HTE_%.*s", hashtable_name.Len, hashtable_name.Ptr ); String entry_array_name = String::fmt_buf( GlobalAllocator, "Arr_HTE_%.*s", hashtable_name.Len, hashtable_name.Ptr );
@ -44,6 +44,7 @@ CodeBody gen_hashtable( StrC type, StrC hashtable_name )
"tbl_name", (StrC) hashtable_name, "tbl_name", (StrC) hashtable_name,
"tbl_type", (StrC) tbl_type, "tbl_type", (StrC) tbl_type,
stringize( stringize(
typedef struct <tbl_type> <tbl_type>;
typedef struct HTE_<tbl_name> HTE_<tbl_name>; typedef struct HTE_<tbl_name> HTE_<tbl_name>;
struct HTE_<tbl_name> struct HTE_<tbl_name>
{ {
@ -101,8 +102,8 @@ CodeBody gen_hashtable( StrC type, StrC hashtable_name )
{ {
<tbl_type> <tbl_type>
result = { NULL, NULL }; result = { NULL, NULL };
result.Hashes = array_ssize_make( allocator ); array_init(result.Hashes, allocator );
result.Entries = <fn_array>_make( allocator ); array_init(result.Entries, allocator );
return result; return result;
} }
@ -111,8 +112,8 @@ CodeBody gen_hashtable( StrC type, StrC hashtable_name )
{ {
<tbl_type> <tbl_type>
result = { NULL, NULL }; result = { NULL, NULL };
result.Hashes = array_ssize_make_reserve( allocator, num ); array_init_reserve(result.Hashes, allocator, num );
result.Entries = <fn_array>_make_reserve( allocator, num ); array_init_reserve(result.Entries, allocator, num );
return result; return result;
} }

View File

@ -63,6 +63,7 @@ CodeBody gen_fixed_arenas()
result.append(arena_struct_8kb); result.append(arena_struct_8kb);
result.append(arena_struct_16kb); result.append(arena_struct_16kb);
result.append(arena_struct_32kb); result.append(arena_struct_32kb);
result.append(arena_struct_64kb);
result.append(arena_struct_128kb); result.append(arena_struct_128kb);
result.append(arena_struct_256kb); result.append(arena_struct_256kb);
result.append(arena_struct_512kb); result.append(arena_struct_512kb);
@ -75,6 +76,7 @@ CodeBody gen_fixed_arenas()
result.append(arena_interface_8kb); result.append(arena_interface_8kb);
result.append(arena_interface_16kb); result.append(arena_interface_16kb);
result.append(arena_interface_32kb); result.append(arena_interface_32kb);
result.append(arena_interface_64kb);
result.append(arena_interface_128kb); result.append(arena_interface_128kb);
result.append(arena_interface_256kb); result.append(arena_interface_256kb);
result.append(arena_interface_512kb); result.append(arena_interface_512kb);
@ -100,7 +102,7 @@ CodeBody gen_fixed_arenas()
FixedArena_1MB* : fixed_arena_init_1MB, \ FixedArena_1MB* : fixed_arena_init_1MB, \
FixedArena_2MB* : fixed_arena_init_2MB, \ FixedArena_2MB* : fixed_arena_init_2MB, \
FixedArena_4MB* : fixed_arena_init_4MB \ FixedArena_4MB* : fixed_arena_init_4MB \
)(expr) ) GEN_RESOLVED_FUNCTION_CALL(& expr)
#define fixed_arena_size_remaining(expr, alignment) _Generic((expr), \ #define fixed_arena_size_remaining(expr, alignment) _Generic((expr), \
FixedArena_1KB* : fixed_arena_size_remaining_1KB, \ FixedArena_1KB* : fixed_arena_size_remaining_1KB, \
@ -115,7 +117,7 @@ CodeBody gen_fixed_arenas()
FixedArena_1MB* : fixed_arena_size_remaining_1MB, \ FixedArena_1MB* : fixed_arena_size_remaining_1MB, \
FixedArena_2MB* : fixed_arena_size_remaining_2MB, \ FixedArena_2MB* : fixed_arena_size_remaining_2MB, \
FixedArena_4MB* : fixed_arena_size_remaining_4MB \ FixedArena_4MB* : fixed_arena_size_remaining_4MB \
)(expr, alignment) ) GEN_RESOLVED_FUNCTION_CALL(& expr, alignment)
)" )"
))); )));

View File

@ -24,7 +24,7 @@ void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, s
if ( bucket.PhysicalStart == nullptr ) if ( bucket.PhysicalStart == nullptr )
GEN_FATAL( "Failed to create bucket for Global_AllocatorBuckets"); GEN_FATAL( "Failed to create bucket for Global_AllocatorBuckets");
if ( ! array_append( & Global_AllocatorBuckets, bucket ) ) if ( ! array_append( Global_AllocatorBuckets, bucket ) )
GEN_FATAL( "Failed to append bucket to Global_AllocatorBuckets"); GEN_FATAL( "Failed to append bucket to Global_AllocatorBuckets");
last = array_back(Global_AllocatorBuckets); last = array_back(Global_AllocatorBuckets);
@ -51,7 +51,7 @@ void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, s
if ( bucket.PhysicalStart == nullptr ) if ( bucket.PhysicalStart == nullptr )
GEN_FATAL( "Failed to create bucket for Global_AllocatorBuckets"); GEN_FATAL( "Failed to create bucket for Global_AllocatorBuckets");
if ( ! array_append( & Global_AllocatorBuckets, bucket ) ) if ( ! array_append( Global_AllocatorBuckets, bucket ) )
GEN_FATAL( "Failed to append bucket to Global_AllocatorBuckets"); GEN_FATAL( "Failed to append bucket to Global_AllocatorBuckets");
last = array_back(Global_AllocatorBuckets); last = array_back(Global_AllocatorBuckets);
@ -249,7 +249,7 @@ void init()
if ( bucket.PhysicalStart == nullptr ) if ( bucket.PhysicalStart == nullptr )
GEN_FATAL( "Failed to create first bucket for Global_AllocatorBuckets"); GEN_FATAL( "Failed to create first bucket for Global_AllocatorBuckets");
array_append( & Global_AllocatorBuckets, bucket ); array_append( Global_AllocatorBuckets, bucket );
} }
// Setup the arrays // Setup the arrays
@ -272,7 +272,7 @@ void init()
if ( code_pool.PhysicalStart == nullptr ) if ( code_pool.PhysicalStart == nullptr )
GEN_FATAL( "gen::init: Failed to initialize the code pool" ); GEN_FATAL( "gen::init: Failed to initialize the code pool" );
array_append( & CodePools, code_pool ); array_append( CodePools, code_pool );
LexArena = arena_init_from_allocator( Allocator_Lexer, LexAllocator_Size ); LexArena = arena_init_from_allocator( Allocator_Lexer, LexAllocator_Size );
@ -281,7 +281,7 @@ void init()
if ( string_arena.PhysicalStart == nullptr ) if ( string_arena.PhysicalStart == nullptr )
GEN_FATAL( "gen::init: Failed to initialize the string arena" ); GEN_FATAL( "gen::init: Failed to initialize the string arena" );
array_append( & StringArenas, string_arena ); array_append( StringArenas, string_arena );
} }
// Setup the hash tables // Setup the hash tables
@ -381,7 +381,7 @@ AllocatorInfo get_string_allocator( s32 str_length )
{ {
Arena new_arena = arena_init_from_allocator( Allocator_StringArena, SizePer_StringArena ); Arena new_arena = arena_init_from_allocator( Allocator_StringArena, SizePer_StringArena );
if ( ! array_append( & StringArenas, new_arena ) ) if ( ! array_append( StringArenas, new_arena ) )
GEN_FATAL( "gen::get_string_allocator: Failed to allocate a new string arena" ); GEN_FATAL( "gen::get_string_allocator: Failed to allocate a new string arena" );
last = array_back(StringArenas); last = array_back(StringArenas);
@ -419,7 +419,7 @@ Code make_code()
if ( code_pool.PhysicalStart == nullptr ) if ( code_pool.PhysicalStart == nullptr )
GEN_FATAL( "gen::make_code: Failed to allocate a new code pool - CodePool allcoator returned nullptr." ); GEN_FATAL( "gen::make_code: Failed to allocate a new code pool - CodePool allcoator returned nullptr." );
if ( ! array_append( & CodePools, code_pool ) ) if ( ! array_append( CodePools, code_pool ) )
GEN_FATAL( "gen::make_code: Failed to allocate a new code pool - CodePools failed to append new 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);

View File

@ -236,7 +236,8 @@ forceinline
s32 lex_preprocessor_directive( LexContext* ctx ) s32 lex_preprocessor_directive( LexContext* ctx )
{ {
char const* hash = ctx->scanner; char const* hash = ctx->scanner;
array_append( & Tokens, { hash, 1, Tok_Preprocess_Hash, ctx->line, ctx->column, TF_Preprocess } ); Token hash_tok = { hash, 1, Tok_Preprocess_Hash, ctx->line, ctx->column, TF_Preprocess };
array_append( Tokens, hash_tok );
move_forward(); move_forward();
SkipWhitespace(); SkipWhitespace();
@ -312,14 +313,14 @@ s32 lex_preprocessor_directive( LexContext* ctx )
ctx->token.Length = ctx->token.Length + ctx->token.Text - hash; ctx->token.Length = ctx->token.Length + ctx->token.Text - hash;
ctx->token.Text = hash; ctx->token.Text = hash;
array_append( & Tokens, ctx->token ); array_append( Tokens, ctx->token );
return Lex_Continue; // Skip found token, its all handled here. return Lex_Continue; // Skip found token, its all handled here.
} }
if ( ctx->token.Type == Tok_Preprocess_Else || ctx->token.Type == Tok_Preprocess_EndIf ) if ( ctx->token.Type == Tok_Preprocess_Else || ctx->token.Type == Tok_Preprocess_EndIf )
{ {
ctx->token.Flags |= TF_Preprocess_Cond; ctx->token.Flags |= TF_Preprocess_Cond;
array_append( & Tokens, ctx->token ); array_append( Tokens, ctx->token );
end_line(); end_line();
return Lex_Continue; return Lex_Continue;
} }
@ -328,7 +329,7 @@ s32 lex_preprocessor_directive( LexContext* ctx )
ctx->token.Flags |= TF_Preprocess_Cond; ctx->token.Flags |= TF_Preprocess_Cond;
} }
array_append( & Tokens, ctx->token ); array_append( Tokens, ctx->token );
SkipWhitespace(); SkipWhitespace();
@ -352,7 +353,7 @@ s32 lex_preprocessor_directive( LexContext* ctx )
name.Length++; name.Length++;
} }
array_append( & Tokens, name ); array_append( Tokens, name );
u64 key = crc32( name.Text, name.Length ); u64 key = crc32( name.Text, name.Length );
hashtable_set(ctx->defines, key, to_str(name) ); hashtable_set(ctx->defines, key, to_str(name) );
@ -398,7 +399,7 @@ s32 lex_preprocessor_directive( LexContext* ctx )
move_forward(); move_forward();
} }
array_append( & Tokens, preprocess_content ); array_append( Tokens, preprocess_content );
return Lex_Continue; // Skip found token, its all handled here. return Lex_Continue; // Skip found token, its all handled here.
} }
@ -461,7 +462,7 @@ s32 lex_preprocessor_directive( LexContext* ctx )
preprocess_content.Length++; preprocess_content.Length++;
} }
array_append( & Tokens, preprocess_content ); array_append( Tokens, preprocess_content );
return Lex_Continue; // Skip found token, its all handled here. return Lex_Continue; // Skip found token, its all handled here.
} }
@ -470,7 +471,7 @@ void lex_found_token( LexContext* ctx )
{ {
if ( ctx->token.Type != Tok_Invalid ) if ( ctx->token.Type != Tok_Invalid )
{ {
array_append( & Tokens, ctx->token ); array_append( Tokens, ctx->token );
return; return;
} }
@ -497,7 +498,7 @@ void lex_found_token( LexContext* ctx )
} }
ctx->token.Type = type; ctx->token.Type = type;
array_append( & Tokens, ctx->token ); array_append( Tokens, ctx->token );
return; return;
} }
@ -507,7 +508,7 @@ void lex_found_token( LexContext* ctx )
{ {
ctx->token.Type = type; ctx->token.Type = type;
ctx->token.Flags |= TF_Specifier; ctx->token.Flags |= TF_Specifier;
array_append( & Tokens, ctx->token ); array_append( Tokens, ctx->token );
return; return;
} }
@ -515,7 +516,7 @@ void lex_found_token( LexContext* ctx )
if ( type != Tok_Invalid ) if ( type != Tok_Invalid )
{ {
ctx->token.Type = type; ctx->token.Type = type;
array_append( & Tokens, ctx->token ); array_append( Tokens, ctx->token );
return; return;
} }
@ -569,7 +570,7 @@ void lex_found_token( LexContext* ctx )
ctx->token.Type = Tok_Identifier; ctx->token.Type = Tok_Identifier;
} }
array_append( & Tokens, ctx->token ); array_append( Tokens, ctx->token );
} }
neverinline neverinline
@ -643,7 +644,7 @@ TokArray lex( StrC content )
c.token.Type = Tok_NewLine; c.token.Type = Tok_NewLine;
c.token.Length++; c.token.Length++;
array_append( & Tokens, c.token ); array_append( Tokens, c.token );
continue; continue;
} }
} }
@ -679,7 +680,7 @@ TokArray lex( StrC content )
c.token.Length++; c.token.Length++;
move_forward(); move_forward();
array_append( & Tokens, c.token ); array_append( Tokens, c.token );
} }
} }
@ -1134,7 +1135,7 @@ TokArray lex( StrC content )
move_forward(); move_forward();
c.token.Length++; c.token.Length++;
} }
array_append( & Tokens, c.token ); array_append( Tokens, c.token );
continue; continue;
} }
else if ( current == '*' ) else if ( current == '*' )
@ -1170,7 +1171,7 @@ TokArray lex( StrC content )
move_forward(); move_forward();
c.token.Length++; c.token.Length++;
} }
array_append( & Tokens, c.token ); array_append( Tokens, c.token );
// end_line(); // end_line();
continue; continue;
} }
@ -1303,7 +1304,7 @@ TokArray lex( StrC content )
c.token.Length++; c.token.Length++;
move_forward(); move_forward();
array_append( & Tokens, c.token ); array_append( Tokens, c.token );
continue; continue;
} }
} }

View File

@ -755,7 +755,7 @@ Code parse_class_struct( TokType which, bool inplace_def = false )
} }
Token interface_tok = parse_identifier(); Token interface_tok = parse_identifier();
array_append( & interfaces, def_type( to_str(interface_tok) ) ); array_append( interfaces, def_type( to_str(interface_tok) ) );
// <ModuleFlags> <class/struct> <Attributes> <Name> : <Access Specifier> <Name>, ... // <ModuleFlags> <class/struct> <Attributes> <Name> : <Access Specifier> <Name>, ...
} }
} }

View File

@ -39,10 +39,10 @@ usize array_grow_formula(ssize value);
template<class Type> Array<Type> array_init (AllocatorInfo allocator); template<class Type> Array<Type> array_init (AllocatorInfo allocator);
template<class Type> Array<Type> array_init_reserve (AllocatorInfo allocator, ssize capacity); template<class Type> Array<Type> array_init_reserve (AllocatorInfo allocator, ssize capacity);
template<class Type> bool array_append_array (Array<Type>* array, Array<Type> other); template<class Type> bool array_append_array (Array<Type>* array, Array<Type> other);
template<class Type> bool array_append_value (Array<Type>* array, Type value); template<class Type> bool array_append (Array<Type>* array, Type value);
template<class Type> bool array_append_items (Array<Type>* array, Type* items, usize item_num); template<class Type> bool array_append_items (Array<Type>* array, Type* items, usize item_num);
template<class Type> bool array_append_at (Array<Type>* array, Type item, usize idx); template<class Type> bool array_append_at (Array<Type>* array, Type item, usize idx);
template<class Type> bool array_append_at_items(Array<Type>* array, Type* items, usize item_num, usize idx); template<class Type> bool array_append_items_at(Array<Type>* array, Type* items, usize item_num, usize idx);
template<class Type> Type* array_back (Array<Type> array); template<class Type> Type* array_back (Array<Type> array);
template<class Type> void array_clear (Array<Type> array); template<class Type> void array_clear (Array<Type> array);
template<class Type> bool array_fill (Array<Type> array, usize begin, usize end, Type value); template<class Type> bool array_fill (Array<Type> array, usize begin, usize end, Type value);
@ -150,8 +150,8 @@ usize array_grow_formula(ssize value) {
} }
template<class Type> inline template<class Type> inline
bool array_append(Array<Type>* array, Array<Type> other) { bool array_append_array(Array<Type>* array, Array<Type> other) {
return append(array, other, num(other)); return array_append_items(array, (Type*)other, num(other));
} }
template<class Type> inline template<class Type> inline
@ -173,7 +173,7 @@ bool array_append(Array<Type>* array, Type value)
} }
template<class Type> inline template<class Type> inline
bool array_append(Array<Type>* array, Type* items, usize item_num) bool array_append_items(Array<Type>* array, Type* items, usize item_num)
{ {
ArrayHeader* header = array_get_header(array); ArrayHeader* header = array_get_header(array);
@ -221,7 +221,7 @@ bool array_append_at(Array<Type>* array, Type item, usize idx)
} }
template<class Type> inline template<class Type> inline
bool array_append_at(Array<Type>* array, Type* items, usize item_num, usize idx) bool array_append_items_at(Array<Type>* array, Type* items, usize item_num, usize idx)
{ {
ArrayHeader* header = get_header(array); ArrayHeader* header = get_header(array);
@ -396,9 +396,9 @@ bool array_set_capacity(Array<Type>* array, usize new_capacity)
#define array_init(type, allocator) array_init <type> (allocator ) #define array_init(type, allocator) array_init <type> (allocator )
#define array_init_reserve(type, allocator, cap) array_init_reserve <type> (allocator, cap) #define array_init_reserve(type, allocator, cap) array_init_reserve <type> (allocator, cap)
#define array_append_array(array, other) array_append < get_array_underlying_type(array) > (& array, other ) #define array_append_array(array, other) array_append_array < 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(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_items(array, items, item_num) array_append_items < 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(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_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_back(array) array_back < get_array_underlying_type(array) > (array )
@ -680,7 +680,7 @@ ssize hashtable_add_entry(HashTable<Type>* table, u64 key) {
HashTableEntry<Type> entry = { key, -1 }; HashTableEntry<Type> entry = { key, -1 };
idx = array_num(table->Entries); idx = array_num(table->Entries);
array_append( & table->Entries, entry); array_append( table->Entries, entry);
return idx; return idx;
} }

View File

@ -89,6 +89,10 @@
#endif #endif
#ifndef num_args_impl #ifndef num_args_impl
// This is essentially an arg couneter version of GEN_SELECT_ARG macros
// See section : _Generic function overloading for that usage (explains this heavier case)
#define num_args_impl( _0, \ #define num_args_impl( _0, \
_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \ _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
_11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \ _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
@ -265,4 +269,98 @@
#define struct_init(type, value) {value} #define struct_init(type, value) {value}
#endif #endif
// ------------------------ _Generic function overloading -----------------------------------------
#if GEN_COMPILER_C
// This implemnents macros for utilizing "The Naive Extendible _Generic Macro" explained in:
// https://github.com/JacksonAllan/CC/blob/main/articles/Better_C_Generics_Part_1_The_Extendible_Generic.md
// Since gencpp is used to generate the c-library, it was choosen over the more novel implementations to keep the macros as easy to understand and unopaque as possible.
// Extensive effort was put in below to make this as easy as possible to understand what is going on with this mess of a preoprocessor.
#define GEN_COMMA_OPERATOR , // The comma operator is used by preprocessor macros to delimit arguments, so we have to represent it via a macro to prevent parsing incorrectly.
// Helper macros for argument selection
#define GEN_SELECT_ARG_1( _1, ... ) _1 // <-- Of all th args passed pick _1.
#define GEN_SELECT_ARG_2( _1, _2, ... ) _2 // <-- Of all the args passed pick _2.
#define GEN_SELECT_ARG_3( _1, _2, _3, ... ) _3 // etc.. (by induction until _8, which we don't support any more beyond)
// #define GEN_SELECT_ARG_4( _1, _2, _3, _4, ... ) _4
// #define GEN_SELECT_ARG_5( _1, _2, _3, _4, _5, ... ) _5
// #define GEN_SELECT_ARG_6( _1, _2, _3, _4, _5, _6, ... ) _6
// #define GEN_SELECT_ARG_7( _1, _2, _3, _4, _5, _6, _7, ... ) _7
// #define GEN_SELECT_ARG_8( _1, _2, _3, _4, _5, _6, _7, _8, ... ) _8
#define GEN_GENERIC_SEL_ENTRY_TYPE GEN_SELECT_ARG_1 // Use the arg expansion macro to select arg 1 which should have the type.
#define GEN_GENERIC_SEL_ENTRY_FUNCTION GEN_SELECT_ARG_2 // Use the arg expansion macro to select arg 2 which should have the function.
#define GEN_GENERIC_SEL_ENTRY_COMMA_DELIMITER GEN_SELECT_ARG_3 // Use the arg expansion macro to select arg 3 which should have the comma delimiter ','.
#define GEN_RESOLVED_FUNCTION_CALL // Just used to indicate where the call "occurs"
// ----------------------------------------------------------------------------------------------------------------------------------
// GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( macro ) includes a _Generic slot only if the specified macro is defined (as type, function_name).
// It takes advantage of the fact that if the macro is defined, then the expanded text will contain a comma.
// Expands to ',' if it can find (type): (function) <comma_operator: ',' >
// Where GEN_GENERIC_SEL_ENTRY_COMMA_DELIMITER is specifically looking for that <comma> ,
#define GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( slot_exp ) GEN_SELECT_ARG_3( slot_exp, GEN_SELECT_ARG_1( slot_exp, ): GEN_SELECT_ARG_2( slot_exp, ) GEN_COMMA_OPERATOR, , )
// ^Selects the comma ^ is the type ^ is the function ^ comma delimiter to select ^ Insert a comma
// The slot won't exist if that comma is not found.
// |
// This is the same as above but it does not insert a comma V no comma here.
#define GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT_LAST( slot_exp ) GEN_GENERIC_SEL_ENTRY_COMMA_DELIMITER( slot_exp, GEN_GENERIC_SEL_ENTRY_TYPE( slot_exp, ): GEN_GENERIC_SEL_ENTRY_FUNCTION( slot_exp, ), , )
// Needed for the last slot as they don't allow trailing commas.
// ----------------------------------------------------------------------------------------------------------------------------------
// Below are generated on demand for a generated function
// ----------------------------------------------------------------------------------------------------------------------------------
#define GEN_FUNCTION_GENERIC_EXAMPLE( function_arguments ) _Generic( \
(function_arguments), /* Select Via Expression*/ \
/* Extendibility slots: */ \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT_LAST FunctionID__ARGS_SIG_1 ) \
) GEN_RESOLVED_FUNCTION_CALL( function_arguments )
// ----------------------------------------------------------------------------------------------------------------------------------
// Then each definiton of a function has an associated define:
// #define <function_id_macro> GEN_GENERIC_FUNCTION_ARG_SIGNATURE( <function_id>, <arguments> )
#define GEN_GENERIC_FUNCTION_ARG_SIGNATURE( name_of_function, type_delimiter ) type_delimiter name_of_function
// Then somehwere later on
// <etc> <return_type> <function_id> ( <arguments> ) { <implementation> }
// Concrete example:
#define ENABLE_GENERIC_EXAMPLE 0
#if ENABLE_GENERIC_EXAMPLE
// To add support for long:
#define HASH__ARGS_SIG_1 GEN_GENERIC_FUNCTION_ARG_SIGNATURE( hash__P_long, long long )
size_t hash__P_long ( long val ) { return val * 2654435761ull; }
// To add support for long long:
#define HASH__ARGS_SIG_2 GEN_GENERIC_FUNCTION_ARG_SIGNATURE( hash__P_long_long, long long )
size_t hash__P_long_long( long long val ) { return val * 2654435761ull; }
// If using an Editor with support for syntax hightlighting macros: HASH__ARGS_SIG_1 and HASH_ARGS_SIG_2 should show color highlighting indicating the slot is enabled,
// or, "defined" for usage during the compilation pass that handles the _Generic instrinsic.
#define hash( function_arguments ) _Generic( \
(function_arguments), /* Select Via Expression*/ \
/* Extendibility slots: */ \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_2 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_3 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_4 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_5 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_6 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_7 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT_LAST( HASH__ARGS_SIG_8 ) \
) GEN_RESOLVED_FUNCTION_CALL( function_arguments )
#endif // ENABLE_GENERIC_EXAMPLE
// END OF ------------------------ _Generic function overloading ----------------------------------------- END OF
#endif
#pragma endregion Macros #pragma endregion Macros

View File

@ -955,7 +955,7 @@ u8 csv_parse_delimiter( CSV_Object* root, char* text, AllocatorInfo allocator, b
adt_append_arr( root, NULL ); adt_append_arr( root, NULL );
} }
array_append( & root->nodes[ columnIndex ].nodes, rowItem ); array_append( root->nodes[ columnIndex ].nodes, rowItem );
if ( delimiter == delim ) if ( delimiter == delim )
{ {

View File

@ -13,43 +13,7 @@
// They are undefined in gen.hpp and gen.cpp at the end of the files. // 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. // 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 ) #include "helpers/push_container_defines.inline.hpp"
#define array_init_reserve(type, allocator, cap) array_init_reserve <type> (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 <type >(allocator)
#define hashtable_init_reserve(type, allocator, num) hashtable_init_reserve<type >(allocator, num)
#define hashtable_clear(table) hashtable_clear < get_hashtable_underlying_type(table) >(table)
#define hashtable_destroy(table) hashtable_destroy < get_hashtable_underlying_type(table) >(& table)
#define hashtable_get(table, key) hashtable_get < get_hashtable_underlying_type(table) >(table, key)
#define hashtable_grow(table) hashtable_grow < get_hashtable_underlying_type(table) >(& table)
#define hashtable_rehash(table, new_num) hashtable_rehash < get_hashtable_underlying_type(table) >(& table, new_num)
#define hashtable_rehash_fast(table) hashtable_rehash_fast < get_hashtable_underlying_type(table) >(table)
#define hashtable_remove(table, key) hashtable_remove < get_hashtable_underlying_type(table) >(table, key)
#define hashtable_remove_entry(table, idx) hashtable_remove_entry< get_hashtable_underlying_type(table) >(table, idx)
#define hashtable_set(table, key, value) hashtable_set < get_hashtable_underlying_type(table) >(& table, key, value)
#define hashtable_slot(table, key) hashtable_slot < get_hashtable_underlying_type(table) >(table, key)
#define hashtable_add_entry(table, key) hashtable_add_entry < get_hashtable_underlying_type(table) >(& table, key)
#define hashtable_find(table, key) hashtable_find < get_hashtable_underlying_type(table) >(table, key)
#define hashtable_full(table) hashtable_full < get_hashtable_underlying_type(table) >(table)
#define hashtable_map(table, map_proc) hashtable_map < get_hashtable_underlying_type(table) >(table, map_proc)
#define hashtable_map_mut(table, map_proc) hashtable_map_mut < get_hashtable_underlying_type(table) >(table, map_proc)
//! If its desired to roll your own dependencies, define GEN_ROLL_OWN_DEPENDENCIES before including this file. //! 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 //! Dependencies are derived from the c-zpl library: https://github.com/zpl-c/zpl
@ -75,42 +39,5 @@ GEN_NS_BEGIN
GEN_NS_END GEN_NS_END
#include "helpers/pop_container_defines.inline.hpp"
#include "helpers/pop_ignores.inline.hpp" #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

View File

@ -11,6 +11,9 @@
#include "helpers/push_ignores.inline.hpp" #include "helpers/push_ignores.inline.hpp"
#include "components/header_start.hpp" #include "components/header_start.hpp"
// Has container defines pushed
#include "gen.dep.hpp"
GEN_NS_BEGIN GEN_NS_BEGIN
#include "components/types.hpp" #include "components/types.hpp"
@ -30,42 +33,5 @@ GEN_NS_BEGIN
GEN_NS_END GEN_NS_END
#include "helpers/pop_container_defines.inline.hpp"
#include "helpers/pop_ignores.inline.hpp" #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

View File

@ -0,0 +1,38 @@
#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

View File

@ -1,8 +1,9 @@
#define array_init(type, allocator) array_init <type> (allocator ) #define array_init(type, allocator) array_init <type> (allocator )
#define array_init_reserve(type, allocator, cap) array_init_reserve <type> (allocator, cap) #define array_init_reserve(type, allocator, cap) array_init_reserve <type> (allocator, cap)
#define array_append_array(array, other) array_append < get_array_underlying_type(array) > (& array, other ) #define array_append_array(array, other) array_append_array < 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(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_items(array, items, item_num) array_append_items < 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(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_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_back(array) array_back < get_array_underlying_type(array) > (array )
@ -17,3 +18,21 @@
#define arary_resize(array, num) arary_resize < get_array_underlying_type(array) > (& array, num) #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_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 arary_get_header(array) arary_get_header < get_array_underlying_type(array) > (array )
#define hashtable_init(type, allocator) hashtable_init <type >(allocator)
#define hashtable_init_reserve(type, allocator, num) hashtable_init_reserve<type >(allocator, num)
#define hashtable_clear(table) hashtable_clear < get_hashtable_underlying_type(table) >(table)
#define hashtable_destroy(table) hashtable_destroy < get_hashtable_underlying_type(table) >(& table)
#define hashtable_get(table, key) hashtable_get < get_hashtable_underlying_type(table) >(table, key)
#define hashtable_grow(table) hashtable_grow < get_hashtable_underlying_type(table) >(& table)
#define hashtable_rehash(table, new_num) hashtable_rehash < get_hashtable_underlying_type(table) >(& table, new_num)
#define hashtable_rehash_fast(table) hashtable_rehash_fast < get_hashtable_underlying_type(table) >(table)
#define hashtable_remove(table, key) hashtable_remove < get_hashtable_underlying_type(table) >(table, key)
#define hashtable_remove_entry(table, idx) hashtable_remove_entry< get_hashtable_underlying_type(table) >(table, idx)
#define hashtable_set(table, key, value) hashtable_set < get_hashtable_underlying_type(table) >(& table, key, value)
#define hashtable_slot(table, key) hashtable_slot < get_hashtable_underlying_type(table) >(table, key)
#define hashtable_add_entry(table, key) hashtable_add_entry < get_hashtable_underlying_type(table) >(& table, key)
#define hashtable_find(table, key) hashtable_find < get_hashtable_underlying_type(table) >(table, key)
#define hashtable_full(table) hashtable_full < get_hashtable_underlying_type(table) >(table)
#define hashtable_map(table, map_proc) hashtable_map < get_hashtable_underlying_type(table) >(table, map_proc)
#define hashtable_map_mut(table, map_proc) hashtable_map_mut < get_hashtable_underlying_type(table) >(table, map_proc)

View File

@ -237,6 +237,7 @@ if ( $c_library )
} elseif ($vendor -eq "msvc") { } elseif ($vendor -eq "msvc") {
$compiler_args += "/TC" # Compile as C $compiler_args += "/TC" # Compile as C
$compiler_args += "/Zc:__cplusplus" # Fix __cplusplus macro $compiler_args += "/Zc:__cplusplus" # Fix __cplusplus macro
$compiler_args += "/std:c11"
} }
build-simple $path_build $includes $compiler_args $linker_args $unit $executable build-simple $path_build $includes $compiler_args $linker_args $unit $executable