successful compile of c_library for: platform, macros, basic_types, debug, and memory headers (and newly generated c-code)

This commit is contained in:
Edward R. Gonzalez 2024-12-05 00:40:51 -05:00
parent cae1555b11
commit a3e7ec4c72
20 changed files with 471 additions and 248 deletions

View File

@ -130,22 +130,6 @@ int gen_main()
header.print( basic_types ); header.print( basic_types );
header.print( debug ); header.print( debug );
// Array(StrC) to_rename = array_init_reserve(StrC, GlobalAllocator, 128);
// to_rename.append(txt("align_forward"));
// to_rename.append(txt("pointer_add"));
// to_rename.append(txt("allocator_info"));
// to_rename.append(txt("size_remaining"));
// to_rename.append(txt("free"));
// to_rename.append(txt("clear"));
// to_rename.append(txt("init_sub"));
// to_rename.append(txt("check"));
// NeedsSelectors needs_selectors;
// needs_selectors.table = hashtable_init_reserve(Array(CodeFn), GlobalAllocator, 1024);
// for ( StrC id : to_rename ) {
// needs_selectors.set(id, array_init_reserve(CodeFn, GlobalAllocator, 128));
// }
CodeBody parsed_memory = parse_file( project_dir "dependencies/memory.hpp" ); CodeBody parsed_memory = parse_file( project_dir "dependencies/memory.hpp" );
CodeBody memory = def_body(CT_Global_Body); CodeBody memory = def_body(CT_Global_Body);
@ -317,7 +301,7 @@ int gen_main()
containers.append( def_pragma(code(endregion Containers))); containers.append( def_pragma(code(endregion Containers)));
} }
header.print(fmt_newline); // header.print(fmt_newline);
// header.print(dump_to_scratch_and_retireve(containers)); // header.print(dump_to_scratch_and_retireve(containers));
Code hashing = scan_file( project_dir "dependencies/hashing.hpp" ); Code hashing = scan_file( project_dir "dependencies/hashing.hpp" );
@ -421,7 +405,7 @@ int gen_main()
CodeBody especifier = gen_especifier( project_dir "enums/ESpecifier.csv" ); CodeBody especifier = gen_especifier( project_dir "enums/ESpecifier.csv" );
CodeBody ast_inlines = gen_ast_inlines(); CodeBody ast_inlines = gen_ast_inlines();
#if 0 #if 0
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 );

View File

@ -9,38 +9,40 @@ CodeBody gen_fixed_arenas()
result.append(def_pragma(txt("region FixedArena"))); result.append(def_pragma(txt("region FixedArena")));
char const* template_struct = stringize( char const* template_struct = stringize(
struct FixedArena_<Name> struct FixedArena_<Name>_Def
{ {
char memory[<Size>]; char memory[<Size>];
Arena arena; Arena arena;
}; };
typedef struct FixedArena_<Name>_Def FixedArena_<Name>;
); );
char const* template_interface = stringize( char const* template_interface = stringize(
inline inline
void fixed_arena_init_<Name>(FixedArena_<Name>* result) { void fixed_arena_init_<Name>(FixedArena_<Name>* result)
zero_size(& result->memory[0], <Size>); {
result->arena = arena_init_from_memory(& result->memory[0], <Size>); result->arena = arena_init_from_memory(& result->memory[0], <Size>);
} }
inline inline
ssize fixed_arena_size_remaining_<Name>(FixedArena_<Name>* fixed_arena, ssize alignment) { ssize fixed_arena_size_remaining_<Name>(FixedArena_<Name>* fixed_arena, ssize alignment)
return size_remaining(fixed_arena->arena, alignment); {
return arena_size_remaining( & fixed_arena->arena, alignment);
} }
); );
CodeStruct arena_struct_1kb = parse_struct( token_fmt_impl( 3, "Name", txt("1KB"), "Size", txt("kilobytes(1)"), template_struct )); CodeBody arena_struct_1kb = parse_global_body( token_fmt_impl( 3, "Name", txt("1KB"), "Size", txt("kilobytes(1)"), template_struct ));
CodeStruct arena_struct_4kb = parse_struct( token_fmt_impl( 3, "Name", txt("4KB"), "Size", txt("kilobytes(4)"), template_struct )); CodeBody arena_struct_4kb = parse_global_body( token_fmt_impl( 3, "Name", txt("4KB"), "Size", txt("kilobytes(4)"), template_struct ));
CodeStruct arena_struct_8kb = parse_struct( token_fmt_impl( 3, "Name", txt("8KB"), "Size", txt("kilobytes(8)"), template_struct )); CodeBody arena_struct_8kb = parse_global_body( token_fmt_impl( 3, "Name", txt("8KB"), "Size", txt("kilobytes(8)"), template_struct ));
CodeStruct arena_struct_16kb = parse_struct( token_fmt_impl( 3, "Name", txt("16KB"), "Size", txt("kilobytes(16)"), template_struct )); CodeBody arena_struct_16kb = parse_global_body( token_fmt_impl( 3, "Name", txt("16KB"), "Size", txt("kilobytes(16)"), template_struct ));
CodeStruct arena_struct_32kb = parse_struct( token_fmt_impl( 3, "Name", txt("32KB"), "Size", txt("kilobytes(32)"), template_struct )); CodeBody arena_struct_32kb = parse_global_body( token_fmt_impl( 3, "Name", txt("32KB"), "Size", txt("kilobytes(32)"), template_struct ));
CodeStruct arena_struct_64kb = parse_struct( token_fmt_impl( 3, "Name", txt("64KB"), "Size", txt("kilobytes(64)"), template_struct )); CodeBody arena_struct_64kb = parse_global_body( token_fmt_impl( 3, "Name", txt("64KB"), "Size", txt("kilobytes(64)"), template_struct ));
CodeStruct arena_struct_128kb = parse_struct( token_fmt_impl( 3, "Name", txt("128KB"), "Size", txt("kilobytes(128)"), template_struct )); CodeBody arena_struct_128kb = parse_global_body( token_fmt_impl( 3, "Name", txt("128KB"), "Size", txt("kilobytes(128)"), template_struct ));
CodeStruct arena_struct_256kb = parse_struct( token_fmt_impl( 3, "Name", txt("256KB"), "Size", txt("kilobytes(256)"), template_struct )); CodeBody arena_struct_256kb = parse_global_body( token_fmt_impl( 3, "Name", txt("256KB"), "Size", txt("kilobytes(256)"), template_struct ));
CodeStruct arena_struct_512kb = parse_struct( token_fmt_impl( 3, "Name", txt("512KB"), "Size", txt("kilobytes(512)"), template_struct )); CodeBody arena_struct_512kb = parse_global_body( token_fmt_impl( 3, "Name", txt("512KB"), "Size", txt("kilobytes(512)"), template_struct ));
CodeStruct arena_struct_1mb = parse_struct( token_fmt_impl( 3, "Name", txt("1MB"), "Size", txt("megabytes(1)"), template_struct )); CodeBody arena_struct_1mb = parse_global_body( token_fmt_impl( 3, "Name", txt("1MB"), "Size", txt("megabytes(1)"), template_struct ));
CodeStruct arena_struct_2mb = parse_struct( token_fmt_impl( 3, "Name", txt("2MB"), "Size", txt("megabytes(2)"), template_struct )); CodeBody arena_struct_2mb = parse_global_body( token_fmt_impl( 3, "Name", txt("2MB"), "Size", txt("megabytes(2)"), template_struct ));
CodeStruct arena_struct_4mb = parse_struct( token_fmt_impl( 3, "Name", txt("4MB"), "Size", txt("megabytes(4)"), template_struct )); CodeBody arena_struct_4mb = parse_global_body( token_fmt_impl( 3, "Name", txt("4MB"), "Size", txt("megabytes(4)"), template_struct ));
CodeBody arena_interface_1kb = parse_global_body( token_fmt_impl( 3, "Name", txt("1KB"), "Size", txt("kilobytes(1)"), template_interface )); CodeBody arena_interface_1kb = parse_global_body( token_fmt_impl( 3, "Name", txt("1KB"), "Size", txt("kilobytes(1)"), template_interface ));
@ -82,6 +84,7 @@ CodeBody gen_fixed_arenas()
CodeDefine def = def_define(txt("fixed_arena_allocator_info(fixed_arena)"), code({ arena_allocator_proc, & fixed_arena.arena }) ); CodeDefine def = def_define(txt("fixed_arena_allocator_info(fixed_arena)"), code({ arena_allocator_proc, & fixed_arena.arena }) );
result.append(def); result.append(def);
result.append(fmt_newline);
result.append(parse_global_body(txt(R"( result.append(parse_global_body(txt(R"(
#define fixed_arena_init(expr) _Generic((expr), \ #define fixed_arena_init(expr) _Generic((expr), \
@ -116,6 +119,7 @@ CodeBody gen_fixed_arenas()
)" )"
))); )));
result.append(fmt_newline);
result.append(def_pragma(txt("endregion FixedArena"))); result.append(def_pragma(txt("endregion FixedArena")));
return result; return result;

View File

@ -3,38 +3,8 @@
// using namespace gen; // using namespace gen;
using SwapContentProc = CodeBody(void);
struct NeedsSelectors {
HashTable(Array(CodeFn)) table;
void set(StrC sig, Array(CodeFn) list) {
u32 key = crc32(sig.Ptr, sig.Len);
table.set(key, list);
}
Array(CodeFn)* get(StrC sig) {
u32 key = crc32(sig.Ptr, sig.Len);
return table.get(key);
}
};
CodeBody generate_generic_selectors(Array(Array(CodeFn)) listing)
{
constexpr char const* tmpl_selector =
R"( #define <base_name>(<params>) \
_Generic((
))
);
)";
CodeBody result = def_body(CT_Global_Body);
for (Array(CodeFn) functions : listing)
{
}
return result;
}
b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& body ) b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& body )
{ {
b32 found = false; b32 found = false;
@ -153,6 +123,7 @@ CodeFn rename_function_to_unique_symbol(CodeFn fn, StrC optional_prefix = txt(""
return fn; return fn;
} }
using SwapContentProc = CodeBody(void);
bool swap_pragma_region_implementation( StrC region_name, SwapContentProc* swap_content, Code& entry_iter, CodeBody& body ) bool swap_pragma_region_implementation( StrC region_name, SwapContentProc* swap_content, Code& entry_iter, CodeBody& body )
{ {
bool found = false; bool found = false;

View File

@ -43,7 +43,7 @@ void Builder::print_fmt( char const* fmt, ... )
va_end( va ); va_end( va );
// log_fmt( "$%s - print_fmt: %.*s\n", File.filename, res > 80 ? 80 : res, buf ); // log_fmt( "$%s - print_fmt: %.*s\n", File.filename, res > 80 ? 80 : res, buf );
string_append_c_str_len( (String*) & Buffer, (char const*)buf, res ); string_append_c_str_len( (String*) & Buffer, (char const*)buf, res);
} }
void Builder::write() void Builder::write()

View File

@ -221,12 +221,12 @@ void to_string_fwd( CodeClass self, String* result )
String to_string(CodeDefine define) String to_string(CodeDefine define)
{ {
return string_fmt_buf( GlobalAllocator, "#define %SC %SC\n", define->Name, define->Content ); return string_fmt_buf( GlobalAllocator, "#define %SC %SC", define->Name, define->Content );
} }
void to_string(CodeDefine define, String* result ) void to_string(CodeDefine define, String* result )
{ {
string_append_fmt( result, "#define %SC %SC\n", define->Name, define->Content ); string_append_fmt( result, "#define %SC %SC", define->Name, define->Content );
} }
String to_string(CodeDestructor self) String to_string(CodeDestructor self)
@ -919,7 +919,7 @@ void to_string_ifdef(CodePreprocessCond cond, String* result )
void to_string_ifndef(CodePreprocessCond cond, String* result ) void to_string_ifndef(CodePreprocessCond cond, String* result )
{ {
string_append_fmt( result, "#ifndef %SC\n", cond->Content ); string_append_fmt( result, "#ifndef %SC", cond->Content );
} }
void to_string_elif(CodePreprocessCond cond, String* result ) void to_string_elif(CodePreprocessCond cond, String* result )

View File

@ -11,7 +11,7 @@ internal void deinit();
internal internal
void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ) void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags )
{ {
Arena* last = array_back(& Global_AllocatorBuckets); Arena* last = array_back(Global_AllocatorBuckets);
switch ( type ) switch ( type )
{ {
@ -27,7 +27,7 @@ void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, s
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);
} }
return alloc_align( arena_allocator_info(last), size, alignment ); return alloc_align( arena_allocator_info(last), size, alignment );
@ -54,7 +54,7 @@ void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, s
if ( ! array_append( & Global_AllocatorBuckets, bucket ) ) 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);
} }
void* result = alloc_align( last->Backing, size, alignment ); void* result = alloc_align( last->Backing, size, alignment );
@ -296,7 +296,7 @@ void init()
PreprocessorDefines = array_init_reserve<StringCached>( GlobalAllocator, kilobytes(1) ); PreprocessorDefines = array_init_reserve<StringCached>( GlobalAllocator, kilobytes(1) );
define_constants(); define_constants();
parser::init(); GEN_NS_PARSER init();
} }
void deinit() void deinit()
@ -321,14 +321,14 @@ void deinit()
} }
while ( left--, left ); while ( left--, left );
destroy(& StringCache); hashtable_destroy(StringCache);
array_free( & CodePools); array_free( CodePools);
array_free( & StringArenas); array_free( StringArenas);
arena_free(& LexArena); arena_free(& LexArena);
array_free(& PreprocessorDefines); array_free(PreprocessorDefines);
index = 0; index = 0;
left = array_num(Global_AllocatorBuckets); left = array_num(Global_AllocatorBuckets);
@ -340,8 +340,8 @@ void deinit()
} }
while ( left--, left ); while ( left--, left );
array_free(& Global_AllocatorBuckets); array_free(Global_AllocatorBuckets);
parser::deinit(); GEN_NS_PARSER deinit();
} }
void reset() void reset()
@ -366,14 +366,14 @@ void reset()
} }
while ( left--, left ); while ( left--, left );
clear(StringCache); hashtable_clear(StringCache);
define_constants(); define_constants();
} }
AllocatorInfo get_string_allocator( s32 str_length ) AllocatorInfo get_string_allocator( s32 str_length )
{ {
Arena* last = array_back(& StringArenas); Arena* last = array_back(StringArenas);
usize size_req = str_length + sizeof(StringHeader) + sizeof(char*); usize size_req = str_length + sizeof(StringHeader) + sizeof(char*);
@ -384,7 +384,7 @@ AllocatorInfo get_string_allocator( s32 str_length )
if ( ! array_append( & StringArenas, new_arena ) ) 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);
} }
return arena_allocator_info(last); return arena_allocator_info(last);
@ -396,14 +396,14 @@ StringCached get_cached_string( StrC str )
s32 hash_length = str.Len > kilobytes(1) ? kilobytes(1) : str.Len; s32 hash_length = str.Len > kilobytes(1) ? kilobytes(1) : str.Len;
u64 key = crc32( str.Ptr, hash_length ); u64 key = crc32( str.Ptr, hash_length );
{ {
StringCached* result = get(StringCache, key ); StringCached* result = hashtable_get(StringCache, key );
if ( result ) if ( result )
return * result; return * result;
} }
String result = string_make_strc( get_string_allocator( str.Len ), str ); StrC result = string_to_strc( string_make_strc( get_string_allocator( str.Len ), str ));
set(& StringCache, key, { str.Len, result } ); hashtable_set(StringCache, key, result );
return { str.Len, result }; return { str.Len, result };
} }
@ -411,7 +411,7 @@ StringCached get_cached_string( StrC str )
// Used internally to retireve a Code object form the CodePool. // Used internally to retireve a Code object form the CodePool.
Code make_code() Code make_code()
{ {
Pool* allocator = array_back( & CodePools); Pool* allocator = array_back( CodePools);
if ( allocator->FreeList == nullptr ) if ( allocator->FreeList == nullptr )
{ {
Pool code_pool = pool_init( Allocator_CodePool, CodePool_NumBlocks, sizeof(AST) ); Pool code_pool = pool_init( Allocator_CodePool, CodePool_NumBlocks, sizeof(AST) );
@ -422,7 +422,7 @@ Code make_code()
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);
} }
Code result { rcast( AST*, alloc( pool_allocator_info(allocator), sizeof(AST) )) }; Code result { rcast( AST*, alloc( pool_allocator_info(allocator), sizeof(AST) )) };

View File

@ -9,15 +9,13 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va )
ssize remaining = buf_size; ssize remaining = buf_size;
local_persist local_persist
Arena tok_map_arena; FixedArena<TokenFmt_TokenMap_MemSize> tok_map_arena;
fixed_arena_init( & tok_map_arena);
local_persist
HashTable(StrC) tok_map; HashTable(StrC) tok_map;
{ {
local_persist tok_map = hashtable_init(StrC, fixed_arena_allocator_info(& tok_map_arena) );
char tok_map_mem[ TokenFmt_TokenMap_MemSize ];
tok_map_arena = arena_init_from_memory( tok_map_mem, sizeof(tok_map_mem) );
tok_map = hashtable_init(StrC, arena_allocator_info(& tok_map_arena) );
s32 left = num_tokens - 1; s32 left = num_tokens - 1;
@ -27,7 +25,7 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va )
StrC value = va_arg( va, StrC ); StrC value = va_arg( va, StrC );
u32 key = crc32( token, str_len(token) ); u32 key = crc32( token, str_len(token) );
set(& tok_map, key, value ); hashtable_set( tok_map, key, value );
} }
} }
@ -63,7 +61,7 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va )
char const* token = fmt + 1; char const* token = fmt + 1;
u32 key = crc32( token, tok_len ); u32 key = crc32( token, tok_len );
StrC* value = get(tok_map, key ); StrC* value = hashtable_get(tok_map, key );
if ( value ) if ( value )
{ {
@ -93,8 +91,8 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va )
} }
} }
clear(tok_map); hashtable_clear(tok_map);
arena_free(& tok_map_arena); fixed_arena_free(& tok_map_arena);
ssize result = buf_size - remaining; ssize result = buf_size - remaining;

View File

@ -635,7 +635,7 @@ CodeDefine def_define( StrC name, StrC content )
result->Content = get_cached_string( txt("") ); result->Content = get_cached_string( txt("") );
} }
else else
result->Content = get_cached_string( content ); result->Content = get_cached_string( string_to_strc(string_fmt_buf(GlobalAllocator, "%SC\n", content)) );
return result; return result;
} }

View File

@ -124,6 +124,21 @@ Token* current(TokArray* self, bool skip_formatting )
return & self->Arr[self->Idx]; return & self->Arr[self->Idx];
} }
Token* peek(TokArray self, bool skip_formatting)
{
s32 idx = self.Idx;
if ( skip_formatting )
{
while ( self.Arr[idx].Type == Tok_NewLine )
idx++;
return & self.Arr[idx];
}
return & self.Arr[idx];
}
Token* previous(TokArray self, bool skip_formatting) Token* previous(TokArray self, bool skip_formatting)
{ {
s32 idx = self.Idx; s32 idx = self.Idx;
@ -340,7 +355,7 @@ s32 lex_preprocessor_directive( LexContext* ctx )
array_append( & Tokens, name ); array_append( & Tokens, name );
u64 key = crc32( name.Text, name.Length ); u64 key = crc32( name.Text, name.Length );
set(& ctx->defines, key, to_str(name) ); hashtable_set(ctx->defines, key, to_str(name) );
} }
Token preprocess_content = { ctx->scanner, 0, Tok_Preprocess_Content, ctx->line, ctx->column, TF_Preprocess }; Token preprocess_content = { ctx->scanner, 0, Tok_Preprocess_Content, ctx->line, ctx->column, TF_Preprocess };
@ -432,12 +447,13 @@ s32 lex_preprocessor_directive( LexContext* ctx )
if ( current == '\r' ) if ( current == '\r' )
{ {
move_forward(); break;
//move_forward();
} }
if ( current == '\n' ) if ( current == '\n' )
{ {
move_forward(); //move_forward();
break; break;
} }
@ -509,7 +525,7 @@ void lex_found_token( LexContext* ctx )
else else
key = crc32( ctx->token.Text, ctx->token.Length ); key = crc32( ctx->token.Text, ctx->token.Length );
StrC* define = get(ctx->defines, key ); StrC* define = hashtable_get(ctx->defines, key );
if ( define ) if ( define )
{ {
ctx->token.Type = Tok_Preprocess_Macro; ctx->token.Type = Tok_Preprocess_Macro;
@ -537,14 +553,16 @@ void lex_found_token( LexContext* ctx )
ctx->token.Length++; ctx->token.Length++;
} }
if ( current == '\r' && ctx->scanner[1] == '\n' ) //if ( current == '\r' && ctx->scanner[1] == '\n' )
{ //{
move_forward(); // move_forward();
} // ctx->token.Length++;
else if ( current == '\n' ) //}
{ //else if ( current == '\n' )
move_forward(); //{
} // move_forward();
// ctx->token.Length++;
//}
} }
else else
{ {
@ -554,7 +572,6 @@ void lex_found_token( LexContext* ctx )
array_append( & Tokens, ctx->token ); array_append( & Tokens, ctx->token );
} }
neverinline neverinline
// TokArray lex( Array<Token> tokens, StrC content ) // TokArray lex( Array<Token> tokens, StrC content )
TokArray lex( StrC content ) TokArray lex( StrC content )
@ -593,7 +610,7 @@ TokArray lex( StrC content )
} }
u64 key = crc32( * entry, length ); u64 key = crc32( * entry, length );
set(& c.defines, key, (StrC) * entry ); hashtable_set(c.defines, key, (StrC) * entry );
} }
array_clear(Tokens); array_clear(Tokens);
@ -645,7 +662,29 @@ TokArray lex( StrC content )
switch ( result ) switch ( result )
{ {
case Lex_Continue: case Lex_Continue:
{
//TokType last_type = Tokens[array_get_header(Tokens)->Num - 2].Type;
//if ( last_type == Tok_Preprocess_Pragma )
{
c.token = { c.scanner, 0, Tok_Invalid, c.line, c.column, TF_Null };
if ( current == '\r')
{
move_forward();
c.token.Length = 1;
}
if ( current == '\n' )
{
c.token.Type = Tok_NewLine;
c.token.Length++;
move_forward();
array_append( & Tokens, c.token );
}
}
continue; continue;
}
case Lex_ReturnNull: case Lex_ReturnNull:
return { {}, 0 }; return { {}, 0 };
@ -1246,7 +1285,29 @@ TokArray lex( StrC content )
} }
FoundToken: FoundToken:
{
lex_found_token( ctx ); lex_found_token( ctx );
TokType last_type = array_back(Tokens)->Type;
if ( last_type == Tok_Preprocess_Macro )
{
c.token = { c.scanner, 0, Tok_Invalid, c.line, c.column, TF_Null };
if ( current == '\r')
{
move_forward();
c.token.Length = 1;
}
if ( current == '\n' )
{
c.token.Type = Tok_NewLine;
c.token.Length++;
move_forward();
array_append( & Tokens, c.token );
continue;
}
}
}
} }
if ( array_num(Tokens) == 0 ) if ( array_num(Tokens) == 0 )
@ -1255,7 +1316,7 @@ TokArray lex( StrC content )
return { {}, 0 }; return { {}, 0 };
} }
clear(defines); hashtable_clear(defines);
// defines_map_arena.free(); // defines_map_arena.free();
return { Tokens, 0 }; return { Tokens, 0 };
} }

View File

@ -54,7 +54,7 @@ String to_string(ParseContext ctx)
sptr length = scope_start.Length; sptr length = scope_start.Length;
char const* current = scope_start.Text + length; char const* current = scope_start.Text + length;
while ( current <= array_back( & ctx.Tokens.Arr)->Text && *current != '\n' && length < 74 ) while ( current <= array_back( ctx.Tokens.Arr)->Text && *current != '\n' && length < 74 )
{ {
current++; current++;
length++; length++;
@ -141,7 +141,7 @@ void init()
); );
fixed_arena_init(& defines_map_arena); fixed_arena_init(& defines_map_arena);
defines = hashtable_init_reserve(StrC, allocator_info( & defines_map_arena), 256 ); defines = hashtable_init_reserve(StrC, fixed_arena_allocator_info( & defines_map_arena), 256 );
} }
internal internal
@ -172,6 +172,7 @@ bool _check_parse_args( StrC def, char const* func_name )
# define currtok_noskip (* current( & Context.Tokens, dont_skip_formatting )) # define currtok_noskip (* current( & Context.Tokens, dont_skip_formatting ))
# define currtok (* current( & Context.Tokens, skip_formatting )) # define currtok (* current( & Context.Tokens, skip_formatting ))
# define peektok (* peek(Context.Tokens, skip_formatting))
# define prevtok (* previous( Context.Tokens, dont_skip_formatting)) # define prevtok (* previous( Context.Tokens, dont_skip_formatting))
# define nexttok (* next( Context.Tokens, skip_formatting )) # define nexttok (* next( Context.Tokens, skip_formatting ))
# define eat( Type_ ) __eat( & Context.Tokens, Type_ ) # define eat( Type_ ) __eat( & Context.Tokens, Type_ )
@ -786,7 +787,7 @@ Code parse_class_struct( TokType which, bool inplace_def = false )
if ( inline_cmt ) if ( inline_cmt )
result->InlineCmt = inline_cmt; result->InlineCmt = inline_cmt;
array_free(& interfaces); array_free(interfaces);
return result; return result;
} }
@ -1708,8 +1709,17 @@ CodeBody parse_global_nspace( CodeType which )
break; break;
case Tok_Preprocess_Macro: case Tok_Preprocess_Macro:
{
member = parse_simple_preprocess( Tok_Preprocess_Macro ); member = parse_simple_preprocess( Tok_Preprocess_Macro );
// <Macro> // <Macro>
if ( member == Code_Invalid )
{
log_failure( "Failed to parse member\n%s", to_string(Context) );
pop(& Context);
return InvalidCode;
}
}
break; break;
case Tok_Preprocess_Pragma: case Tok_Preprocess_Pragma:
@ -2935,7 +2945,7 @@ Code parse_simple_preprocess( TokType which )
eat( which ); eat( which );
// <Macro> // <Macro>
if ( currtok.Type == Tok_BraceCurly_Open ) if ( peektok.Type == Tok_BraceCurly_Open )
{ {
// Eat the block scope right after the macro. Were assuming the macro defines a function definition's signature // Eat the block scope right after the macro. Were assuming the macro defines a function definition's signature
eat( Tok_BraceCurly_Open ); eat( Tok_BraceCurly_Open );
@ -2977,7 +2987,7 @@ Code parse_simple_preprocess( TokType which )
{ {
if ( str_compare_len( Context.Scope->Prev->ProcName.Ptr, "parse_typedef", Context.Scope->Prev->ProcName.Len ) != 0 ) if ( str_compare_len( Context.Scope->Prev->ProcName.Ptr, "parse_typedef", Context.Scope->Prev->ProcName.Len ) != 0 )
{ {
if ( check( Tok_Statement_End )) if ( peektok.Type == Tok_Statement_End )
{ {
Token stmt_end = currtok; Token stmt_end = currtok;
eat( Tok_Statement_End ); eat( Tok_Statement_End );
@ -2989,7 +2999,7 @@ Code parse_simple_preprocess( TokType which )
} }
} }
tok.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)tok.Text; tok.Length = ( (sptr)currtok_noskip.Text + currtok_noskip.Length ) - (sptr)tok.Text;
} }
char const* content = str_fmt_buf( "%.*s ", tok.Length, tok.Text ); char const* content = str_fmt_buf( "%.*s ", tok.Length, tok.Text );
@ -3701,9 +3711,9 @@ CodeEnum parse_enum( bool inplace_def )
eat( Tok_Operator ); eat( Tok_Operator );
// <Name> = // <Name> =
while ( currtok_noskip.Type != Tok_Comma && currtok_noskip.Type != Tok_BraceCurly_Close ) while ( currtok.Type != Tok_Comma && currtok.Type != Tok_BraceCurly_Close )
{ {
eat( currtok_noskip.Type ); eat( currtok.Type );
} }
} }
// <Name> = <Expression> // <Name> = <Expression>
@ -3717,6 +3727,9 @@ CodeEnum parse_enum( bool inplace_def )
if ( currtok.Type == Tok_Comma ) if ( currtok.Type == Tok_Comma )
{ {
//Token prev = * previous(Context.Tokens, dont_skip_formatting);
//entry.Length = ( (sptr)prev.Text + prev.Length ) - (sptr)entry.Text;
eat( Tok_Comma ); eat( Tok_Comma );
// <Name> = <Expression> <Macro>, // <Name> = <Expression> <Macro>,
} }
@ -3728,9 +3741,8 @@ CodeEnum parse_enum( bool inplace_def )
// <Name> = <Expression> <Macro>, // <Inline Comment> // <Name> = <Expression> <Macro>, // <Inline Comment>
// } // }
Token prev = * previous(Context.Tokens, dont_skip_formatting);
//Token prev = * previous(Context.Tokens, dont_skip_formatting); entry.Length = ( (sptr)prev.Text + prev.Length ) - (sptr)entry.Text;
entry.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)entry.Text;
member = untyped_str( to_str(entry) ); member = untyped_str( to_str(entry) );
break; break;

View File

@ -133,8 +133,8 @@ template<typename Type> sptr to_sptr( Type* ptr ) { return (sptr)ptr; }
template<typename Type> mem_ptr to_mem_ptr ( Type ptr ) { return (mem_ptr) ptr; } template<typename Type> mem_ptr to_mem_ptr ( Type ptr ) { return (mem_ptr) ptr; }
template<typename Type> mem_ptr_const to_mem_ptr_const( Type ptr ) { return (mem_ptr_const)ptr; } template<typename Type> mem_ptr_const to_mem_ptr_const( Type ptr ) { return (mem_ptr_const)ptr; }
#else #else
#define to_utpr( ptr ) ((uptr)(ptr)) #define to_uptr( ptr ) ((uptr)(ptr))
#define to_stpr( ptr ) ((sptr)(ptr)) #define to_sptr( ptr ) ((sptr)(ptr))
#define to_mem_ptr( ptr) ((mem_ptr)ptr) #define to_mem_ptr( ptr) ((mem_ptr)ptr)
#define to_mem_ptr_const( ptr) ((mem_ptr)ptr) #define to_mem_ptr_const( ptr) ((mem_ptr)ptr)

View File

@ -10,8 +10,13 @@ template<class TType> struct RemoveConst<const TType> { typede
template<class TType> struct RemoveConst<const TType[]> { typedef TType Type[]; }; template<class TType> struct RemoveConst<const TType[]> { typedef TType Type[]; };
template<class TType, usize Size> struct RemoveConst<const TType[Size]> { typedef TType Type[Size]; }; template<class TType, usize Size> struct RemoveConst<const TType[Size]> { typedef TType Type[Size]; };
template<class TType> template<class TType> using TRemoveConst = typename RemoveConst<TType>::Type;
using TRemoveConst = typename RemoveConst<TType>::Type;
template <class TType> struct RemovePtr { typedef TType Type; };
template <class TType> struct RemovePtr<TType*> { typedef TType Type; };
template <class TType> using TRemovePtr = typename RemovePtr<TType>::Type;
#pragma region Array #pragma region Array
#define Array(Type) Array<Type> #define Array(Type) Array<Type>
@ -23,31 +28,33 @@ struct ArrayHeader;
#if GEN_SUPPORT_CPP_MEMBER_FEATURES #if GEN_SUPPORT_CPP_MEMBER_FEATURES
template<class Type> struct Array; template<class Type> struct Array;
# define get_array_underlying_type(array) typename TRemovePtr<typeof(array)>:: DataType
#else #else
template<class Type> using Array = Type*; template<class Type> using Array = Type*;
# define get_array_underlying_type(array) TRemovePtr<typeof(array)>
#endif #endif
usize array_grow_formula(ssize value); 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(Type)* array, Array(Type) other); template<class Type> bool array_append_array (Array<Type>* array, Array<Type> other);
template<class Type> bool array_append (Array(Type)* array, Type value); template<class Type> bool array_append_value (Array<Type>* array, Type value);
template<class Type> bool array_append (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 (Array(Type)* array, Type* items, usize item_num, usize idx); template<class Type> bool array_append_at_items(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);
template<class Type> void array_free (Array(Type)* array); template<class Type> void array_free (Array<Type>* array);
template<class Type> bool arary_grow (Array(Type)* array, usize min_capacity); template<class Type> bool arary_grow (Array<Type>* array, usize min_capacity);
template<class Type> usize array_num (Array(Type) array); template<class Type> usize array_num (Array<Type> array);
template<class Type> void arary_pop (Array(Type) array); template<class Type> void arary_pop (Array<Type> array);
template<class Type> void arary_remove_at (Array(Type) array, usize idx); template<class Type> void arary_remove_at (Array<Type> array, usize idx);
template<class Type> bool arary_reserve (Array(Type)* array, usize new_capacity); template<class Type> bool arary_reserve (Array<Type>* array, usize new_capacity);
template<class Type> bool arary_resize (Array(Type)* array, usize num); template<class Type> bool arary_resize (Array<Type>* array, usize num);
template<class Type> bool arary_set_capacity(Array(Type)* array, usize new_capacity); template<class Type> bool arary_set_capacity (Array<Type>* array, usize new_capacity);
template<class Type> ArrayHeader* arary_get_header (Array(Type) array); template<class Type> ArrayHeader* arary_get_header (Array<Type> array);
struct ArrayHeader { struct ArrayHeader {
AllocatorInfo Allocator; AllocatorInfo Allocator;
@ -92,6 +99,8 @@ struct Array
forceinline Type& operator[](ssize index) { return Data[index]; } forceinline Type& operator[](ssize index) { return Data[index]; }
forceinline Type const& operator[](ssize index) const { return Data[index]; } forceinline Type const& operator[](ssize index) const { return Data[index]; }
using DataType = Type;
}; };
#endif #endif
@ -240,15 +249,15 @@ bool array_append_at(Array<Type>* array, Type* items, usize item_num, usize idx)
} }
template<class Type> inline template<class Type> inline
Type* array_back(Array<Type>* array) Type* array_back(Array<Type> array)
{ {
GEN_ASSERT(array != nullptr); GEN_ASSERT(array != nullptr);
ArrayHeader* header = array_get_header(* array); ArrayHeader* header = array_get_header(array);
if (header->Num <= 0) if (header->Num <= 0)
return nullptr; return nullptr;
return & (*array)[header->Num - 1]; return & (array)[header->Num - 1];
} }
template<class Type> inline template<class Type> inline
@ -380,7 +389,30 @@ bool array_set_capacity(Array<Type>* array, usize new_capacity)
return true; return true;
} }
#define array_init_reserve(type, allocator, cap) array_init_reserve<type>(allocator, cap) // These are intended for use in the base library of gencpp and the C-variant of the library
// It provides a interoperability between the C++ and C implementation of arrays. (not letting these do any crazy substiution though)
// They are undefined in gen.hpp and gen.cpp at the end of the files.
// We cpp library expects the user to use the regular calls as they can resolve the type fine.
#define array_init(type, allocator) array_init <type> (allocator )
#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 )
#pragma endregion Array #pragma endregion Array
@ -406,23 +438,23 @@ struct HashTableEntry {
#define HashTableEntry(Type) HashTableEntry<Type> #define HashTableEntry(Type) HashTableEntry<Type>
template<class Type> HashTable<Type> hashtable_init(AllocatorInfo allocator); template<class Type> HashTable<Type> hashtable_init (AllocatorInfo allocator);
template<class Type> HashTable<Type> hashtable_init_reserve(AllocatorInfo allocator, usize num); template<class Type> HashTable<Type> hashtable_init_reserve(AllocatorInfo allocator, usize num);
template<class Type> void clear (HashTable<Type> table); template<class Type> void hashtable_clear (HashTable<Type> table);
template<class Type> void destroy (HashTable<Type>* table); template<class Type> void hashtable_destroy (HashTable<Type>* table);
template<class Type> Type* get (HashTable<Type> table, u64 key); template<class Type> Type* hashtable_get (HashTable<Type> table, u64 key);
template<class Type> void grow (HashTable<Type>* table); template<class Type> void hashtable_grow (HashTable<Type>* table);
template<class Type> void rehash (HashTable<Type>* table, ssize new_num); template<class Type> void hashtable_rehash (HashTable<Type>* table, ssize new_num);
template<class Type> void rehash_fast (HashTable<Type> table); template<class Type> void hashtable_rehash_fast (HashTable<Type> table);
template<class Type> void remove (HashTable<Type> table, u64 key); template<class Type> void hashtable_remove (HashTable<Type> table, u64 key);
template<class Type> void remove_entry (HashTable<Type> table, ssize idx); template<class Type> void hashtable_remove_entry(HashTable<Type> table, ssize idx);
template<class Type> void set (HashTable<Type>* table, u64 key, Type value); template<class Type> void hashtable_set (HashTable<Type>* table, u64 key, Type value);
template<class Type> ssize slot (HashTable<Type> table, u64 key); template<class Type> ssize hashtable_slot (HashTable<Type> table, u64 key);
template<class Type> ssize add_entry (HashTable<Type>* table, u64 key); template<class Type> ssize hashtable_add_entry (HashTable<Type>* table, u64 key);
template<class Type> HashTableFindResult find (HashTable<Type> table, u64 key); template<class Type> HashTableFindResult hashtable_find (HashTable<Type> table, u64 key);
template<class Type> bool full (HashTable<Type> table); template<class Type> bool hashtable_full (HashTable<Type> table);
template<class Type> void map (HashTable<Type> table, void (*map_proc)(u64 key, Type value)); template<class Type> void hashtable_map (HashTable<Type> table, void (*map_proc)(u64 key, Type value));
template<class Type> void map_mut (HashTable<Type> table, void (*map_proc)(u64 key, Type* value)); template<class Type> void hashtable_map_mut (HashTable<Type> table, void (*map_proc)(u64 key, Type* value));
static constexpr f32 HashTable_CriticalLoadScale = 0.7f; static constexpr f32 HashTable_CriticalLoadScale = 0.7f;
@ -482,22 +514,22 @@ HashTable<Type> hashtable_init_reserve(AllocatorInfo allocator, usize num)
} }
template<typename Type> inline template<typename Type> inline
void clear(HashTable<Type> table) { void hashtable_clear(HashTable<Type> table) {
array_clear(table.Entries); array_clear(table.Entries);
array_fill(table.Hashes, 0, array_num(table.Hashes), (ssize)-1); array_fill(table.Hashes, 0, array_num(table.Hashes), (ssize)-1);
} }
template<typename Type> inline template<typename Type> inline
void destroy(HashTable<Type>* table) { void hashtable_destroy(HashTable<Type>* table) {
if (table->Hashes && array_get_header(table->Hashes)->Capacity) { if (table->Hashes && array_get_header(table->Hashes)->Capacity) {
array_free(& table->Hashes); array_free(table->Hashes);
array_free(& table->Entries); array_free(table->Entries);
} }
} }
template<typename Type> inline template<typename Type> inline
Type* get(HashTable<Type> table, u64 key) { Type* hashtable_get(HashTable<Type> table, u64 key) {
ssize idx = find(table, key).EntryIndex; ssize idx = hashtable_find(table, key).EntryIndex;
if (idx >= 0) if (idx >= 0)
return & table.Entries[idx].Value; return & table.Entries[idx].Value;
@ -505,7 +537,7 @@ Type* get(HashTable<Type> table, u64 key) {
} }
template<typename Type> inline template<typename Type> inline
void map(HashTable<Type> table, void (*map_proc)(u64 key, Type value)) { void hashtable_map(HashTable<Type> table, void (*map_proc)(u64 key, Type value)) {
GEN_ASSERT_NOT_NULL(map_proc); GEN_ASSERT_NOT_NULL(map_proc);
for (ssize idx = 0; idx < ssize(num(table.Entries)); ++idx) { for (ssize idx = 0; idx < ssize(num(table.Entries)); ++idx) {
@ -514,7 +546,7 @@ void map(HashTable<Type> table, void (*map_proc)(u64 key, Type value)) {
} }
template<typename Type> inline template<typename Type> inline
void map_mut(HashTable<Type> table, void (*map_proc)(u64 key, Type* value)) { void hashtable_map_mut(HashTable<Type> table, void (*map_proc)(u64 key, Type* value)) {
GEN_ASSERT_NOT_NULL(map_proc); GEN_ASSERT_NOT_NULL(map_proc);
for (ssize idx = 0; idx < ssize(num(table.Entries)); ++idx) { for (ssize idx = 0; idx < ssize(num(table.Entries)); ++idx) {
@ -523,13 +555,13 @@ void map_mut(HashTable<Type> table, void (*map_proc)(u64 key, Type* value)) {
} }
template<typename Type> inline template<typename Type> inline
void grow(HashTable<Type>* table) { void hashtable_grow(HashTable<Type>* table) {
ssize new_num = array_grow_formula( array_num(table->Entries)); ssize new_num = array_grow_formula( array_num(table->Entries));
rehash(table, new_num); hashtable_rehash(table, new_num);
} }
template<typename Type> inline template<typename Type> inline
void rehash(HashTable<Type>* table, ssize new_num) void hashtable_rehash(HashTable<Type>* table, ssize new_num)
{ {
ssize last_added_index; ssize last_added_index;
HashTable<Type> new_ht = hashtable_init_reserve<Type>( array_get_header(table->Hashes)->Allocator, new_num); HashTable<Type> new_ht = hashtable_init_reserve<Type>( array_get_header(table->Hashes)->Allocator, new_num);
@ -539,8 +571,8 @@ void rehash(HashTable<Type>* table, ssize new_num)
HashTableFindResult find_result; HashTableFindResult find_result;
HashTableEntry<Type>& entry = table->Entries[idx]; HashTableEntry<Type>& entry = table->Entries[idx];
find_result = find(new_ht, entry.Key); find_result = hashtable_find(new_ht, entry.Key);
last_added_index = add_entry(& new_ht, entry.Key); last_added_index = hashtable_add_entry(& new_ht, entry.Key);
if (find_result.PrevIndex < 0) if (find_result.PrevIndex < 0)
new_ht.Hashes[find_result.HashIndex] = last_added_index; new_ht.Hashes[find_result.HashIndex] = last_added_index;
@ -551,12 +583,12 @@ void rehash(HashTable<Type>* table, ssize new_num)
new_ht.Entries[last_added_index].Value = entry.Value; new_ht.Entries[last_added_index].Value = entry.Value;
} }
destroy(table); hashtable_destroy(table);
* table = new_ht; * table = new_ht;
} }
template<typename Type> inline template<typename Type> inline
void rehash_fast(HashTable<Type> table) void hashtable_rehash_fast(HashTable<Type> table)
{ {
ssize idx; ssize idx;
@ -582,7 +614,7 @@ void rehash_fast(HashTable<Type> table)
} }
template<typename Type> inline template<typename Type> inline
void remove(HashTable<Type> table, u64 key) { void hashtable_remove(HashTable<Type> table, u64 key) {
HashTableFindResult find_result = find(table, key); HashTableFindResult find_result = find(table, key);
if (find_result.EntryIndex >= 0) { if (find_result.EntryIndex >= 0) {
@ -592,26 +624,26 @@ void remove(HashTable<Type> table, u64 key) {
} }
template<typename Type> inline template<typename Type> inline
void remove_entry(HashTable<Type> table, ssize idx) { void hashtable_remove_entry(HashTable<Type> table, ssize idx) {
remove_at(table.Entries, idx); remove_at(table.Entries, idx);
} }
template<typename Type> inline template<typename Type> inline
void set(HashTable<Type>* table, u64 key, Type value) void hashtable_set(HashTable<Type>* table, u64 key, Type value)
{ {
ssize idx; ssize idx;
HashTableFindResult find_result; HashTableFindResult find_result;
if (full(* table)) if (hashtable_full(* table))
grow(table); hashtable_grow(table);
find_result = find(* table, key); find_result = hashtable_find(* table, key);
if (find_result.EntryIndex >= 0) { if (find_result.EntryIndex >= 0) {
idx = find_result.EntryIndex; idx = find_result.EntryIndex;
} }
else else
{ {
idx = add_entry(table, key); idx = hashtable_add_entry(table, key);
if (find_result.PrevIndex >= 0) { if (find_result.PrevIndex >= 0) {
table->Entries[find_result.PrevIndex].Next = idx; table->Entries[find_result.PrevIndex].Next = idx;
@ -623,12 +655,12 @@ void set(HashTable<Type>* table, u64 key, Type value)
table->Entries[idx].Value = value; table->Entries[idx].Value = value;
if (full(* table)) if (hashtable_full(* table))
grow(table); hashtable_grow(table);
} }
template<typename Type> inline template<typename Type> inline
ssize slot(HashTable<Type> table, u64 key) { ssize hashtable_slot(HashTable<Type> table, u64 key) {
for (ssize idx = 0; idx < ssize(num(table.Hashes)); ++idx) for (ssize idx = 0; idx < ssize(num(table.Hashes)); ++idx)
if (table.Hashes[idx] == key) if (table.Hashes[idx] == key)
return idx; return idx;
@ -637,7 +669,7 @@ ssize slot(HashTable<Type> table, u64 key) {
} }
template<typename Type> inline template<typename Type> inline
ssize add_entry(HashTable<Type>* table, u64 key) { ssize hashtable_add_entry(HashTable<Type>* table, u64 key) {
ssize idx; ssize idx;
HashTableEntry<Type> entry = { key, -1 }; HashTableEntry<Type> entry = { key, -1 };
@ -647,7 +679,7 @@ ssize add_entry(HashTable<Type>* table, u64 key) {
} }
template<typename Type> inline template<typename Type> inline
HashTableFindResult find(HashTable<Type> table, u64 key) HashTableFindResult hashtable_find(HashTable<Type> table, u64 key)
{ {
HashTableFindResult result = { -1, -1, -1 }; HashTableFindResult result = { -1, -1, -1 };
@ -670,29 +702,29 @@ HashTableFindResult find(HashTable<Type> table, u64 key)
} }
template<typename Type> inline template<typename Type> inline
bool full(HashTable<Type> table) { bool hashtable_full(HashTable<Type> table) {
usize critical_load = usize(HashTable_CriticalLoadScale * f32(array_num(table.Hashes))); usize critical_load = usize(HashTable_CriticalLoadScale * f32(array_num(table.Hashes)));
b32 result = array_num(table.Entries) > critical_load; b32 result = array_num(table.Entries) > critical_load;
return result; return result;
} }
#define hashtable_init(Type, allocator) hashtable_init<Type>(allocator) #define hashtable_init(type, allocator) hashtable_init <type>(allocator)
#define hashtable_init_reserve(Type, allocator, num) hashtable_init_reserve<Type>(allocator, num) #define hashtable_init_reserve(type, allocator, num) hashtable_init_reserve<type>(allocator, num)
#define hashtable_clear(Type, table) clear<Type>(table) #define hashtable_clear(table) hashtable_clear < TStripPtr<table> >(table)
#define hashtable_destroy(Type, table) destroy<Type>(table) #define hashtable_destroy(table) hashtable_destroy < TStripPtr<table> >(& table)
#define hashtable_get(Type, table, key) get<Type>(table, key) #define hashtable_get(table, key) hashtable_get < TStripPtr<table> >(table, key)
#define hashtable_grow(Type, table) grow<Type>(table) #define hashtable_grow(table) hashtable_grow < TStripPtr<table> >(& table)
#define hashtable_rehash(Type, table, new_num) rehash<Type>(table, new_num) #define hashtable_rehash(table, new_num) hashtable_rehash < TStripPtr<table> >(& table, new_num)
#define hashtable_rehash_fast(Type, table) rehash_fast<Type>(table) #define hashtable_rehash_fast(table) hashtable_rehash_fast < TStripPtr<table> >(table)
#define hashtable_remove(Type, table, key) remove<Type>(table, key) #define hashtable_remove(table, key) hashtable_remove < TStripPtr<table> >(table, key)
#define hashtable_remove_entry(Type, table, idx) remove_entry<Type>(table, idx) #define hashtable_remove_entry(table, idx) hashtable_remove_entry< TStripPtr<table> >(table, idx)
#define hashtable_set(Type, table, key, value) set<Type>(table, key, value) #define hashtable_set(table, key, value) hashtable_set < TStripPtr<table> >(& table, key, value)
#define hashtable_slot(Type, table, key) slot<Type>(table, key) #define hashtable_slot(table, key) hashtable_slot < TStripPtr<table> >(table, key)
#define hashtable_add_entry(Type, table, key) add_entry<Type>(table, key) #define hashtable_add_entry(table, key) hashtable_add_entry < TStripPtr<table> >(& table, key)
#define hashtable_find(Type, table, key) find<Type>(table, key) #define hashtable_find(table, key) hashtable_find < TStripPtr<table> >(table, key)
#define hashtable_full(Type, table) full<Type>(table) #define hashtable_full(table) hashtable_full < TStripPtr<table> >(table)
#define hashtable_map(Type, table, map_proc) map<Type>(table, map_proc) #define hashtable_map(table, map_proc) hashtable_map < TStripPtr<table> >(table, map_proc)
#define hashtable_map_mut(Type, table, map_proc) map_mut<Type>(table, map_proc) #define hashtable_map_mut(table, map_proc) hashtable_map_mut < TStripPtr<table> >(table, map_proc)
#pragma endregion HashTable #pragma endregion HashTable

View File

@ -647,7 +647,7 @@ GEN_FILE_CLOSE_PROC( _memory_file_close )
if ( d->flags & EFileStream_CLONE_WRITABLE ) if ( d->flags & EFileStream_CLONE_WRITABLE )
{ {
Array<u8> arr = { d->buf }; Array<u8> arr = { d->buf };
array_free(& arr); array_free(arr);
} }
allocator_free( allocator, d ); allocator_free( allocator, d );

View File

@ -183,12 +183,18 @@
# endif # endif
#endif #endif
#if !defined(GEN_SUPPORT_CPP_REFERENCES) && (GEN_COMPILER_C || __STDC_VERSION__ < 202311L) #if !defined(GEN_SUPPORT_CPP_REFERENCES)
# define GEN_SUPPORT_CPP_REFERENCES 1
#endif
#if GEN_COMPILER_C && defined(GEN_SUPPORT_CPP_REFERENCES)
# undef GEN_SUPPORT_CPP_REFERENCES # undef GEN_SUPPORT_CPP_REFERENCES
# define GEN_SUPPORT_CPP_REFERENCES 0 # define GEN_SUPPORT_CPP_REFERENCES 0
#endif #endif
#if !defined(GEN_SUPPORT_CPP_MEMBER_FEATURES) && (GEN_COMPILER_C || __STDC_VERSION__ < 202311L) #if !defined(GEN_SUPPORT_CPP_MEMBER_FEATURES)
# define GEN_SUPPORT_CPP_MEMBER_FEATURES 1
#endif
#if GEN_COMPILER_C && defined(GEN_SUPPORT_CPP_MEMBER_FEATURES)
# undef GEN_SUPPORT_CPP_MEMBER_FEATURES # undef GEN_SUPPORT_CPP_MEMBER_FEATURES
# define GEN_SUPPORT_CPP_MEMBER_FEATURES 0 # define GEN_SUPPORT_CPP_MEMBER_FEATURES 0
#endif #endif
@ -229,6 +235,10 @@
# ifndef nullptr # ifndef nullptr
# define nullptr NULL # define nullptr NULL
# endif # endif
# ifndef GEN_REMOVE_PTR
# define GEN_REMOVE_PTR(type) typeof(* ( (type) NULL) )
# endif
#endif #endif
#if ! defined(GEN_PARAM_DEFAULT) && GEN_COMPILER_CPP #if ! defined(GEN_PARAM_DEFAULT) && GEN_COMPILER_CPP

View File

@ -320,17 +320,15 @@ ssize arena_size_remaining(Arena* arena, ssize alignment)
} }
#pragma endregion Arena #pragma endregion Arena
GEN_API_C_END GEN_API_C_END
#pragma region FixedArena #pragma region FixedArena
template<s32 Size> template<s32 Size>
struct FixedArena; struct FixedArena;
template<s32 Size> FixedArena<Size> fixed_arena_init(); template<s32 Size> FixedArena<Size> fixed_arena_init();
template<s32 Size> AllocatorInfo allocator_info(FixedArena<Size>* fixed_arena ); template<s32 Size> AllocatorInfo fixed_arena_allocator_info(FixedArena<Size>* fixed_arena );
template<s32 Size> ssize size_remaining(FixedArena<Size>* fixed_arena, ssize alignment); template<s32 Size> ssize fixed_arena_size_remaining(FixedArena<Size>* fixed_arena, ssize alignment);
#if GEN_SUPPORT_CPP_REFERENCES #if GEN_SUPPORT_CPP_REFERENCES
template<s32 Size> AllocatorInfo allocator_info( FixedArena<Size>& fixed_arena ) { return allocator_info(& fixed_arena); } template<s32 Size> AllocatorInfo allocator_info( FixedArena<Size>& fixed_arena ) { return allocator_info(& fixed_arena); }
@ -356,7 +354,7 @@ struct FixedArena
}; };
template<s32 Size> inline template<s32 Size> inline
AllocatorInfo allocator_info( FixedArena<Size>* fixed_arena ) { AllocatorInfo fixed_arena_allocator_info( FixedArena<Size>* fixed_arena ) {
GEN_ASSERT(fixed_arena); GEN_ASSERT(fixed_arena);
return { arena_allocator_proc, & fixed_arena->arena }; return { arena_allocator_proc, & fixed_arena->arena };
} }
@ -368,7 +366,12 @@ void fixed_arena_init(FixedArena<Size>* result) {
} }
template<s32 Size> inline template<s32 Size> inline
ssize size_remaining(FixedArena<Size>* fixed_arena, ssize alignment) { void fixed_arena_free(FixedArena<Size>* fixed_arena) {
arena_free( & fixed_arena->arena);
}
template<s32 Size> inline
ssize fixed_arena_size_remaining(FixedArena<Size>* fixed_arena, ssize alignment) {
return size_remaining(fixed_arena->arena, alignment); return size_remaining(fixed_arena->arena, alignment);
} }
@ -386,10 +389,8 @@ using Arena_2MB = FixedArena< megabytes( 2 ) >;
using Arena_4MB = FixedArena< megabytes( 4 ) >; using Arena_4MB = FixedArena< megabytes( 4 ) >;
#pragma endregion FixedArena #pragma endregion FixedArena
GEN_API_C_BEGIN GEN_API_C_BEGIN
#pragma region Pool #pragma region Pool
struct Pool_Def; struct Pool_Def;
typedef struct Pool_Def Pool; typedef struct Pool_Def Pool;
@ -435,7 +436,8 @@ struct Pool_Def
inline inline
AllocatorInfo pool_allocator_info(Pool* pool) { AllocatorInfo pool_allocator_info(Pool* pool) {
return { pool_allocator_proc, pool }; AllocatorInfo info = { pool_allocator_proc, pool };
return info;
} }
inline inline

View File

@ -139,6 +139,7 @@
#if GEN_COMPILER_C #if GEN_COMPILER_C
#include <assert.h> #include <assert.h>
#include <stdbool.h>
#endif #endif
#pragma endregion Mandatory Includes #pragma endregion Mandatory Includes
@ -148,6 +149,7 @@
# define GEN_NS_PARSER_BEGIN # define GEN_NS_PARSER_BEGIN
# define GEN_NS_PARSER_END # define GEN_NS_PARSER_END
# define GEN_USING_NS_PARSER # define GEN_USING_NS_PARSER
# define GEN_NS_PARSER
# define GEN_NS # define GEN_NS
# define GEN_NS_BEGIN # define GEN_NS_BEGIN
# define GEN_NS_END # define GEN_NS_END
@ -155,6 +157,7 @@
# define GEN_NS_PARSER_BEGIN namespace parser { # define GEN_NS_PARSER_BEGIN namespace parser {
# define GEN_NS_PARSER_END } # define GEN_NS_PARSER_END }
# define GEN_USING_NS_PARSER using namespace parser # define GEN_USING_NS_PARSER using namespace parser
# define GEN_NS_PARSER parser::
# define GEN_NS :: # define GEN_NS ::
# define GEN_NS_BEGIN # define GEN_NS_BEGIN
# define GEN_NS_END # define GEN_NS_END
@ -162,6 +165,7 @@
#else #else
# define GEN_NS_PARSER_BEGIN namespace parser { # define GEN_NS_PARSER_BEGIN namespace parser {
# define GEN_NS_PARSER_END } # define GEN_NS_PARSER_END }
# define GEN_NS_PARSER parser::
# define GEN_USING_NS_PARSER using namespace parser # define GEN_USING_NS_PARSER using namespace parser
# define GEN_NS gen:: # define GEN_NS gen::
# define GEN_NS_BEGIN namespace gen { # define GEN_NS_BEGIN namespace gen {

View File

@ -208,7 +208,7 @@ struct String
String fmt(AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...) { String fmt(AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...) {
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
ssize res = str_fmt_va(buf, buf_size, fmt, va); ssize res = str_fmt_va(buf, buf_size, fmt, va) - 1;
va_end(va); va_end(va);
return string_make_length(allocator, buf, res); return string_make_length(allocator, buf, res);
} }
@ -219,7 +219,7 @@ struct String
char buf[GEN_PRINTF_MAXLEN] = { 0 }; char buf[GEN_PRINTF_MAXLEN] = { 0 };
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
ssize res = str_fmt_va(buf, GEN_PRINTF_MAXLEN, fmt, va); ssize res = str_fmt_va(buf, GEN_PRINTF_MAXLEN, fmt, va) - 1;
va_end(va); va_end(va);
return string_make_length(allocator, buf, res); return string_make_length(allocator, buf, res);
} }
@ -314,7 +314,7 @@ inline
String string_fmt(AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...) { String string_fmt(AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...) {
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
ssize res = str_fmt_va(buf, buf_size, fmt, va); ssize res = str_fmt_va(buf, buf_size, fmt, va) - 1;
va_end(va); va_end(va);
return string_make_length(allocator, buf, res); return string_make_length(allocator, buf, res);
@ -328,7 +328,7 @@ String string_fmt_buf(AllocatorInfo allocator, char const* fmt, ...)
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
ssize res = str_fmt_va(buf, GEN_PRINTF_MAXLEN, fmt, va); ssize res = str_fmt_va(buf, GEN_PRINTF_MAXLEN, fmt, va) -1;
va_end(va); va_end(va);
return string_make_length(allocator, buf, res); return string_make_length(allocator, buf, res);

View File

@ -8,6 +8,49 @@
#include "gen.hpp" #include "gen.hpp"
// These are intended for use in the base library of gencpp and the C-variant of the library
// It provides a interoperability between the C++ and C interfacing for containers. (not letting these do any crazy substiution though)
// They are undefined in gen.hpp and gen.cpp at the end of the files.
// We cpp library expects the user to use the regular calls as they can resolve the type fine.
#define array_init(type, allocator) array_init <type> (allocator )
#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 (table)
#define hashtable_destroy(table) hashtable_destroy (& table)
#define hashtable_get(table, key) hashtable_get (table, key)
#define hashtable_grow(table) hashtable_grow (& table)
#define hashtable_rehash(table, new_num) hashtable_rehash (& table, new_num)
#define hashtable_rehash_fast(table) hashtable_rehash_fast (table)
#define hashtable_remove(table, key) hashtable_remove (table, key)
#define hashtable_remove_entry(table, idx) hashtable_remove_entry(table, idx)
#define hashtable_set(table, key, value) hashtable_set (& table, key, value)
#define hashtable_slot(table, key) hashtable_slot (table, key)
#define hashtable_add_entry(table, key) hashtable_add_entry (& table, key)
#define hashtable_find(table, key) hashtable_find (table, key)
#define hashtable_full(table) hashtable_full (table)
#define hashtable_map(table, map_proc) hashtable_map (table, map_proc)
#define hashtable_map_mut(table, map_proc) hashtable_map_mut (table, map_proc)
//! If its desired to roll your own dependencies, define GEN_ROLL_OWN_DEPENDENCIES before including this file. //! 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
#ifndef GEN_ROLL_OWN_DEPENDENCIES #ifndef GEN_ROLL_OWN_DEPENDENCIES
@ -33,3 +76,41 @@ GEN_NS_BEGIN
GEN_NS_END GEN_NS_END
#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

@ -31,3 +31,41 @@ GEN_NS_BEGIN
GEN_NS_END GEN_NS_END
#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,26 @@
#if GEN_SUPPORT_CPP_MEMBER_FEATURES
// These are intended for use in the base library of gencpp and the C-variant of the library
// It provides a interoperability between the C++ and C implementation of arrays. (not letting these do any crazy substiution though)
// They are undefined in gen.hpp and gen.cpp at the end of the files.
// We cpp library expects the user to use the regular calls as they can resolve the type fine.
#define array_init(type, allocator) array_init <typeof(array)>(type, allocator )
#define array_init_reserve(type, allocator, cap) array_init_reserve<typeof(array)>(type, allocator, cap)
#define array_append_array(type, array, other) array_append <typeof(array)>(type, & array, other )
#define array_append_value(type, array, value) array_append <typeof(array)>(type, & array, value )
#define array_append_items(type, array, items, item_num) array_append <typeof(array)>(type, & array, items, item_num )
#define array_append_at(type, array, item, idx ) array_append_at <typeof(array)>(type, & array, item, idx )
#define array_append_at(type, array, items, item_num, idx) array_append_at <typeof(array)>(type, & items, item_num, idx )
#define array_back(type, array ) array_back <typeof(array)>(type, array )
#define array_clear(type, array ) array_clear <typeof(array)>(type, array )
#define array_fill(type, array, begin, end, value) array_fill <typeof(array)>(type, array, begin, end, value )
#define array_free(type, array) array_free <typeof(array)>(type, & array )
#define arary_grow(type, array, min_capacity) arary_grow <typeof(array)>(type, & array, min_capacity)
#define array_num(type, array) array_num <typeof(array)>(type, array )
#define arary_pop(type, array) arary_pop <typeof(array)>(type, array )
#define arary_remove_at(type, array, idx) arary_remove_at <typeof(array)>(type, idx)
#define arary_reserve(type, array, new_capacity) arary_reserve <typeof(array)>(type, & array, new_capacity )
#define arary_resize(type, array, num) arary_resize <typeof(array)>(type, & array, num)
#define arary_set_capacity(array, new_capacity) arary_set_capacity<typeof(array)>(type, & array, new_capacity )
#define arary_get_header(array) arary_get_header <typeof(array)>(type, array )
#endif