8 Commits

29 changed files with 665 additions and 506 deletions

View File

@ -15,7 +15,9 @@ Builder builder_open( char const* path )
return result; return result;
} }
result.Buffer = strbuilder_make_reserve( _ctx->Allocator_Temp, _ctx->InitSize_BuilderBuffer ); Context* ctx = get_context();
GEN_ASSERT_NOT_NULL(ctx);
result.Buffer = strbuilder_make_reserve( ctx->Allocator_Temp, ctx->InitSize_BuilderBuffer );
// log_fmt("$Builder - Opened file: %s\n", result.File.filename ); // log_fmt("$Builder - Opened file: %s\n", result.File.filename );
return result; return result;

View File

@ -25,13 +25,14 @@ Builder builder_open ( char const* path );
void builder_pad_lines ( Builder* builder, s32 num ); void builder_pad_lines ( Builder* builder, s32 num );
void builder_print ( Builder* builder, Code code ); void builder_print ( Builder* builder, Code code );
void builder_print_fmt_va( Builder* builder, char const* fmt, va_list va ); void builder_print_fmt_va( Builder* builder, char const* fmt, va_list va );
void builder_print_fmt ( Builder* builder, char const* fmt, ... ) { void builder_write ( Builder* builder );
forceinline void builder_print_fmt ( Builder* builder, char const* fmt, ... ) {
va_list va; va_list va;
va_start( va, fmt ); va_start( va, fmt );
builder_print_fmt_va( builder, fmt, va ); builder_print_fmt_va( builder, fmt, va );
va_end( va ); va_end( va );
} }
void builder_write( Builder* builder );
struct Builder struct Builder
{ {
@ -56,10 +57,10 @@ struct Builder
}; };
#if GEN_COMPILER_CPP && ! GEN_C_LIKE_CPP #if GEN_COMPILER_CPP && ! GEN_C_LIKE_CPP
void builder_pad_lines( Builder& builder, s32 num ) { return builder_pad_lines(& builder, num); } forceinline void builder_pad_lines( Builder& builder, s32 num ) { return builder_pad_lines(& builder, num); }
void builder_print ( Builder& builder, Code code ) { return builder_print(& builder, code); } forceinline void builder_print ( Builder& builder, Code code ) { return builder_print(& builder, code); }
void builder_write ( Builder& builder ) { return builder_write(& builder ); } forceinline void builder_write ( Builder& builder ) { return builder_write(& builder ); }
void builder_print_fmt( Builder& builder, char const* fmt, ...) { forceinline void builder_print_fmt( Builder& builder, char const* fmt, ...) {
va_list va; va_list va;
va_start( va, fmt ); va_start( va, fmt );
builder_print_fmt_va( & builder, fmt, va ); builder_print_fmt_va( & builder, fmt, va );

View File

@ -20,7 +20,7 @@ Code scan_file( char const* path )
GEN_FATAL("scan_file: %s is empty", path ); GEN_FATAL("scan_file: %s is empty", path );
} }
StrBuilder str = strbuilder_make_reserve( _ctx->Allocator_Temp, fsize ); StrBuilder str = strbuilder_make_reserve( get_context()->Allocator_Temp, fsize );
file_read( & file, str, fsize ); file_read( & file, str, fsize );
strbuilder_get_header(str)->Length = fsize; strbuilder_get_header(str)->Length = fsize;
@ -117,7 +117,7 @@ Code scan_file( char const* path )
} }
CodeBody parse_file( const char* path ) { CodeBody parse_file( const char* path ) {
FileContents file = file_read_contents( _ctx->Allocator_Temp, true, path ); FileContents file = file_read_contents( get_context()->Allocator_Temp, true, path );
Str content = { (char const*)file.data, file.size }; Str content = { (char const*)file.data, file.size };
CodeBody code = parse_global_body( content ); CodeBody code = parse_global_body( content );
log_fmt("\nParsed: %s\n", path); log_fmt("\nParsed: %s\n", path);

View File

@ -42,6 +42,7 @@ struct AST_Body
}; };
static_assert( sizeof(AST_Body) == sizeof(AST), "ERROR: AST_Body is not the same size as AST"); static_assert( sizeof(AST_Body) == sizeof(AST), "ERROR: AST_Body is not the same size as AST");
// TODO(Ed): Support chaining attributes (Use parameter linkage pattern)
struct AST_Attributes struct AST_Attributes
{ {
union { union {

View File

@ -39,7 +39,7 @@ enum Specifier : u32
inline Str spec_to_str( Specifier type ) inline Str spec_to_str( Specifier type )
{ {
local_persist Str lookup[26] = { local_persist Str lookup[] = {
{ "INVALID", sizeof( "INVALID" ) - 1 }, { "INVALID", sizeof( "INVALID" ) - 1 },
{ "consteval", sizeof( "consteval" ) - 1 }, { "consteval", sizeof( "consteval" ) - 1 },
{ "constexpr", sizeof( "constexpr" ) - 1 }, { "constexpr", sizeof( "constexpr" ) - 1 },

View File

@ -6,7 +6,7 @@
#pragma region Constants #pragma region Constants
extern PreprocessorMacro enum_underlying_macro; extern Macro enum_underlying_macro;
extern Code access_public; extern Code access_public;
extern Code access_protected; extern Code access_protected;

View File

@ -262,9 +262,9 @@ forceinline void define_params_append (CodeDefineParams appendee
forceinline CodeDefineParams define_params_get (CodeDefineParams self, s32 idx ) { return (CodeDefineParams) (Code) params_get( cast(CodeParams, self), idx); } forceinline CodeDefineParams define_params_get (CodeDefineParams self, s32 idx ) { return (CodeDefineParams) (Code) params_get( cast(CodeParams, self), idx); }
forceinline bool define_params_has_entries(CodeDefineParams self) { return params_has_entries( cast(CodeParams, self)); } forceinline bool define_params_has_entries(CodeDefineParams self) { return params_has_entries( cast(CodeParams, self)); }
CodeDefineParams begin_CodeDefineParams(CodeDefineParams params) { return (CodeDefineParams) (Code) begin_CodeParams( cast(CodeParams, (Code)params)); } forceinline CodeDefineParams begin_CodeDefineParams(CodeDefineParams params) { return (CodeDefineParams) (Code) begin_CodeParams( cast(CodeParams, (Code)params)); }
CodeDefineParams end_CodeDefineParams (CodeDefineParams params) { return (CodeDefineParams) (Code) end_CodeParams ( cast(CodeParams, (Code)params)); } forceinline CodeDefineParams end_CodeDefineParams (CodeDefineParams params) { return (CodeDefineParams) (Code) end_CodeParams ( cast(CodeParams, (Code)params)); }
CodeDefineParams next_CodeDefineParams (CodeDefineParams params, CodeDefineParams entry_iter) { return (CodeDefineParams) (Code) next_CodeParams ( cast(CodeParams, (Code)params), cast(CodeParams, (Code)entry_iter)); } forceinline CodeDefineParams next_CodeDefineParams (CodeDefineParams params, CodeDefineParams entry_iter) { return (CodeDefineParams) (Code) next_CodeParams ( cast(CodeParams, (Code)params), cast(CodeParams, (Code)entry_iter)); }
#if GEN_COMPILER_CPP #if GEN_COMPILER_CPP
forceinline forceinline

View File

@ -179,28 +179,28 @@ void define_constants()
#endif #endif
spec_const = def_specifier( Spec_Const); code_set_global( cast(Code, spec_const )); spec_const = def_specifier( Spec_Const); code_set_global( cast(Code, spec_const ));
spec_consteval = def_specifier( Spec_Consteval); code_set_global( cast(Code, spec_consteval ));; spec_consteval = def_specifier( Spec_Consteval); code_set_global( cast(Code, spec_consteval ));
spec_constexpr = def_specifier( Spec_Constexpr); code_set_global( cast(Code, spec_constexpr ));; spec_constexpr = def_specifier( Spec_Constexpr); code_set_global( cast(Code, spec_constexpr ));
spec_constinit = def_specifier( Spec_Constinit); code_set_global( cast(Code, spec_constinit ));; spec_constinit = def_specifier( Spec_Constinit); code_set_global( cast(Code, spec_constinit ));
spec_extern_linkage = def_specifier( Spec_External_Linkage); code_set_global( cast(Code, spec_extern_linkage ));; spec_extern_linkage = def_specifier( Spec_External_Linkage); code_set_global( cast(Code, spec_extern_linkage ));
spec_final = def_specifier( Spec_Final); code_set_global( cast(Code, spec_final ));; spec_final = def_specifier( Spec_Final); code_set_global( cast(Code, spec_final ));
spec_forceinline = def_specifier( Spec_ForceInline); code_set_global( cast(Code, spec_forceinline ));; spec_forceinline = def_specifier( Spec_ForceInline); code_set_global( cast(Code, spec_forceinline ));
spec_global = def_specifier( Spec_Global); code_set_global( cast(Code, spec_global ));; spec_global = def_specifier( Spec_Global); code_set_global( cast(Code, spec_global ));
spec_inline = def_specifier( Spec_Inline); code_set_global( cast(Code, spec_inline ));; spec_inline = def_specifier( Spec_Inline); code_set_global( cast(Code, spec_inline ));
spec_internal_linkage = def_specifier( Spec_Internal_Linkage); code_set_global( cast(Code, spec_internal_linkage ));; spec_internal_linkage = def_specifier( Spec_Internal_Linkage); code_set_global( cast(Code, spec_internal_linkage ));
spec_local_persist = def_specifier( Spec_Local_Persist); code_set_global( cast(Code, spec_local_persist ));; spec_local_persist = def_specifier( Spec_Local_Persist); code_set_global( cast(Code, spec_local_persist ));
spec_mutable = def_specifier( Spec_Mutable); code_set_global( cast(Code, spec_mutable ));; spec_mutable = def_specifier( Spec_Mutable); code_set_global( cast(Code, spec_mutable ));
spec_neverinline = def_specifier( Spec_NeverInline); code_set_global( cast(Code, spec_neverinline ));; spec_neverinline = def_specifier( Spec_NeverInline); code_set_global( cast(Code, spec_neverinline ));
spec_noexcept = def_specifier( Spec_NoExceptions); code_set_global( cast(Code, spec_noexcept ));; spec_noexcept = def_specifier( Spec_NoExceptions); code_set_global( cast(Code, spec_noexcept ));
spec_override = def_specifier( Spec_Override); code_set_global( cast(Code, spec_override ));; spec_override = def_specifier( Spec_Override); code_set_global( cast(Code, spec_override ));
spec_ptr = def_specifier( Spec_Ptr); code_set_global( cast(Code, spec_ptr ));; spec_ptr = def_specifier( Spec_Ptr); code_set_global( cast(Code, spec_ptr ));
spec_pure = def_specifier( Spec_Pure); code_set_global( cast(Code, spec_pure )); spec_pure = def_specifier( Spec_Pure); code_set_global( cast(Code, spec_pure ));
spec_ref = def_specifier( Spec_Ref); code_set_global( cast(Code, spec_ref ));; spec_ref = def_specifier( Spec_Ref); code_set_global( cast(Code, spec_ref ));
spec_register = def_specifier( Spec_Register); code_set_global( cast(Code, spec_register ));; spec_register = def_specifier( Spec_Register); code_set_global( cast(Code, spec_register ));
spec_rvalue = def_specifier( Spec_RValue); code_set_global( cast(Code, spec_rvalue ));; spec_rvalue = def_specifier( Spec_RValue); code_set_global( cast(Code, spec_rvalue ));
spec_static_member = def_specifier( Spec_Static); code_set_global( cast(Code, spec_static_member ));; spec_static_member = def_specifier( Spec_Static); code_set_global( cast(Code, spec_static_member ));
spec_thread_local = def_specifier( Spec_Thread_Local); code_set_global( cast(Code, spec_thread_local ));; spec_thread_local = def_specifier( Spec_Thread_Local); code_set_global( cast(Code, spec_thread_local ));
spec_virtual = def_specifier( Spec_Virtual); code_set_global( cast(Code, spec_virtual ));; spec_virtual = def_specifier( Spec_Virtual); code_set_global( cast(Code, spec_virtual ));
spec_volatile = def_specifier( Spec_Volatile); code_set_global( cast(Code, spec_volatile )); spec_volatile = def_specifier( Spec_Volatile); code_set_global( cast(Code, spec_volatile ));
spec_local_persist = def_specifiers( 1, Spec_Local_Persist ); spec_local_persist = def_specifiers( 1, Spec_Local_Persist );
@ -211,7 +211,7 @@ void define_constants()
enum_underlying_macro.Type = MT_Expression; enum_underlying_macro.Type = MT_Expression;
enum_underlying_macro.Flags = MF_Functional; enum_underlying_macro.Flags = MF_Functional;
} }
register_preprocess_macro(enum_underlying_macro); register_macro(enum_underlying_macro);
} }
void init(Context* ctx) void init(Context* ctx)
@ -315,8 +315,8 @@ void init(Context* ctx)
if ( ctx->StrCache.Entries == nullptr ) if ( ctx->StrCache.Entries == nullptr )
GEN_FATAL( "gen::init: Failed to initialize the StringCache"); GEN_FATAL( "gen::init: Failed to initialize the StringCache");
ctx->PreprocessorMacros = hashtable_init(PreprocessorMacro, ctx->Allocator_DyanmicContainers); ctx->Macros = hashtable_init(Macro, ctx->Allocator_DyanmicContainers);
if (ctx->PreprocessorMacros.Hashes == nullptr || ctx->PreprocessorMacros.Entries == nullptr) { if (ctx->Macros.Hashes == nullptr || ctx->Macros.Entries == nullptr) {
GEN_FATAL( "gen::init: Failed to initialize the PreprocessMacros table" ); GEN_FATAL( "gen::init: Failed to initialize the PreprocessMacros table" );
} }
} }
@ -356,7 +356,7 @@ void deinit(Context* ctx)
array_free( ctx->CodePools); array_free( ctx->CodePools);
array_free( ctx->StringArenas); array_free( ctx->StringArenas);
hashtable_destroy(ctx->PreprocessorMacros); hashtable_destroy(ctx->Macros);
left = array_num( ctx->Fallback_AllocatorBuckets); left = array_num( ctx->Fallback_AllocatorBuckets);
if (left) if (left)
@ -376,6 +376,13 @@ void deinit(Context* ctx)
if (_ctx == ctx) if (_ctx == ctx)
_ctx = nullptr; _ctx = nullptr;
-- context_counter; -- context_counter;
Context wipe = {};
* ctx = wipe;
}
Context* get_context() {
return _ctx;
} }
void reset(Context* ctx) void reset(Context* ctx)
@ -401,7 +408,7 @@ void reset(Context* ctx)
while ( left--, left ); while ( left--, left );
hashtable_clear(ctx->StrCache); hashtable_clear(ctx->StrCache);
hashtable_clear(ctx->PreprocessorMacros); hashtable_clear(ctx->Macros);
define_constants(); define_constants();
} }
@ -464,47 +471,50 @@ Code make_code()
return result; return result;
} }
PreprocessorMacro* lookup_preprocess_macro( Str name ) { Macro* lookup_macro( Str name ) {
u32 key = crc32( name.Ptr, name.Len ); u32 key = crc32( name.Ptr, name.Len );
return hashtable_get( _ctx->PreprocessorMacros, key ); return hashtable_get( _ctx->Macros, key );
} }
void register_preprocess_macro( PreprocessorMacro macro ) { void register_macro( Macro macro ) {
GEN_ASSERT_NOT_NULL(macro.Name.Ptr); GEN_ASSERT_NOT_NULL(macro.Name.Ptr);
GEN_ASSERT(macro.Name.Len > 0); GEN_ASSERT(macro.Name.Len > 0);
u32 key = crc32( macro.Name.Ptr, macro.Name.Len ); u32 key = crc32( macro.Name.Ptr, macro.Name.Len );
hashtable_set( _ctx->PreprocessorMacros, key, macro ); macro.Name = cache_str(macro.Name);
hashtable_set( _ctx->Macros, key, macro );
} }
void register_preprocess_macros( s32 num, ... ) void register_macros( s32 num, ... )
{ {
GEN_ASSERT(num > 0); GEN_ASSERT(num > 0);
va_list va; va_list va;
va_start(va, num); va_start(va, num);
do do
{ {
PreprocessorMacro macro = va_arg(va, PreprocessorMacro); Macro macro = va_arg(va, Macro);
GEN_ASSERT_NOT_NULL(macro.Name.Ptr); GEN_ASSERT_NOT_NULL(macro.Name.Ptr);
GEN_ASSERT(macro.Name.Len > 0); GEN_ASSERT(macro.Name.Len > 0);
macro.Name = cache_str(macro.Name);
u32 key = crc32( macro.Name.Ptr, macro.Name.Len ); u32 key = crc32( macro.Name.Ptr, macro.Name.Len );
hashtable_set( _ctx->PreprocessorMacros, key, macro ); hashtable_set( _ctx->Macros, key, macro );
} }
while (num--, num > 0); while (num--, num > 0);
va_end(va); va_end(va);
} }
void register_preprocess_macros( s32 num, PreprocessorMacro* macros ) void register_macros( s32 num, Macro* macros )
{ {
GEN_ASSERT(num > 0); GEN_ASSERT(num > 0);
do do
{ {
PreprocessorMacro macro = * macros; Macro macro = * macros;
GEN_ASSERT_NOT_NULL(macro.Name.Ptr); GEN_ASSERT_NOT_NULL(macro.Name.Ptr);
GEN_ASSERT(macro.Name.Len > 0); GEN_ASSERT(macro.Name.Len > 0);
macro.Name = cache_str(macro.Name);
u32 key = crc32( macro.Name.Ptr, macro.Name.Len ); u32 key = crc32( macro.Name.Ptr, macro.Name.Len );
hashtable_set( _ctx->PreprocessorMacros, key, macro ); hashtable_set( _ctx->Macros, key, macro );
++ macros; ++ macros;
} }
while (num--, num > 0); while (num--, num > 0);

View File

@ -71,7 +71,7 @@ struct Context
// Used by the lexer to persistently treat all these identifiers as preprocessor defines. // Used by the lexer to persistently treat all these identifiers as preprocessor defines.
// Populate with strings via gen::cache_str. // Populate with strings via gen::cache_str.
// Functional defines must have format: id( ;at minimum to indicate that the define is only valid with arguments. // Functional defines must have format: id( ;at minimum to indicate that the define is only valid with arguments.
HashTable(PreprocessorMacro) PreprocessorMacros; MacroTable Macros;
// Backend // Backend
@ -79,6 +79,8 @@ struct Context
u32 InitSize_Fallback_Allocator_Bucket_Size; u32 InitSize_Fallback_Allocator_Bucket_Size;
Array(Arena) Fallback_AllocatorBuckets; Array(Arena) Fallback_AllocatorBuckets;
StringTable token_fmt_map;
// Array(Token) LexerTokens; // Array(Token) LexerTokens;
Array(Pool) CodePools; Array(Pool) CodePools;
@ -100,6 +102,9 @@ GEN_API void init(Context* ctx);
// However on Windows at least, it doesn't need to occur as the OS will clean up after the process. // However on Windows at least, it doesn't need to occur as the OS will clean up after the process.
GEN_API void deinit(Context* ctx); GEN_API void deinit(Context* ctx);
// Retrieves the active context (not usually needed, but here in case...)
GEN_API Context* get_context();
// Clears the allocations, but doesn't free the memoery, then calls init() again. // Clears the allocations, but doesn't free the memoery, then calls init() again.
// Ease of use. // Ease of use.
GEN_API void reset(Context* ctx); GEN_API void reset(Context* ctx);
@ -107,16 +112,16 @@ GEN_API void reset(Context* ctx);
GEN_API void set_context(Context* ctx); GEN_API void set_context(Context* ctx);
// Mostly intended for the parser // Mostly intended for the parser
GEN_API PreprocessorMacro* lookup_preprocess_macro( Str Name ); GEN_API Macro* lookup_macro( Str Name );
// Alternative way to add a preprocess define entry for the lexer & parser to utilize // Alternative way to add a preprocess define entry for the lexer & parser to utilize
// if the user doesn't want to use def_define // if the user doesn't want to use def_define
// Macros are tracked by name so if the name already exists the entry will be overwritten. // Macros are tracked by name so if the name already exists the entry will be overwritten.
GEN_API void register_preprocess_macro( PreprocessorMacro macro ); GEN_API void register_macro( Macro macro );
// Ease of use batch registration // Ease of use batch registration
GEN_API void register_preprocess_macros( s32 num, ... ); GEN_API void register_macros( s32 num, ... );
GEN_API void register_preprocess_macros( s32 num, PreprocessorMacro* macros ); GEN_API void register_macros( s32 num, Macro* macros );
// Used internally to retrive or make string allocations. // Used internally to retrive or make string allocations.
// Strings are stored in a series of string arenas of fixed size (SizePer_StringArena) // Strings are stored in a series of string arenas of fixed size (SizePer_StringArena)
@ -378,12 +383,20 @@ GEN_API Code untyped_token_fmt( s32 num_tokens, char const* fmt, ... );
#ifndef name #ifndef name
// Convienence for defining any name used with the gen api. // Convienence for defining any name used with the gen api.
// Lets you provide the length and string literal to the functions without the need for the DSL. // Lets you provide the length and string literal to the functions without the need for the DSL.
#define name( Id_ ) { stringize(Id_), sizeof(stringize( Id_ )) - 1 } # if GEN_COMPILER_C
# define name( Id_ ) (Str){ stringize(Id_), sizeof(stringize( Id_ )) - 1 }
# else
# define name( Id_ ) Str { stringize(Id_), sizeof(stringize( Id_ )) - 1 }
# endif
#endif #endif
#ifndef code #ifndef code
// Same as name just used to indicate intention of literal for code instead of names. // Same as name just used to indicate intention of literal for code instead of names.
#define code( ... ) { stringize( __VA_ARGS__ ), sizeof(stringize(__VA_ARGS__)) - 1 } # if GEN_COMPILER_C
# define code( ... ) (Str){ stringize( __VA_ARGS__ ), sizeof(stringize(__VA_ARGS__)) - 1 }
# else
# define code( ... ) Str { stringize( __VA_ARGS__ ), sizeof(stringize(__VA_ARGS__)) - 1 }
# endif
#endif #endif
#ifndef args #ifndef args

View File

@ -8,9 +8,8 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va )
char const* buf_begin = buf; char const* buf_begin = buf;
ssize remaining = buf_size; ssize remaining = buf_size;
local_persist StringTable tok_map; if (_ctx->token_fmt_map.Hashes == nullptr) {
do_once() { _ctx->token_fmt_map = hashtable_init(Str, _ctx->Allocator_DyanmicContainers );
tok_map = hashtable_init(Str, _ctx->Allocator_DyanmicContainers );
} }
// Populate token pairs // Populate token pairs
{ {
@ -22,7 +21,7 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va )
Str value = va_arg( va, Str ); Str value = va_arg( va, Str );
u32 key = crc32( token, c_str_len(token) ); u32 key = crc32( token, c_str_len(token) );
hashtable_set( tok_map, key, value ); hashtable_set( _ctx->token_fmt_map, key, value );
} }
} }
@ -58,7 +57,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 );
Str* value = hashtable_get(tok_map, key ); Str* value = hashtable_get(_ctx->token_fmt_map, key );
if ( value ) if ( value )
{ {
@ -87,7 +86,7 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va )
current = * fmt; current = * fmt;
} }
} }
hashtable_clear(tok_map); hashtable_clear(_ctx->token_fmt_map);
ssize result = buf_size - remaining; ssize result = buf_size - remaining;
return result; return result;
} }

View File

@ -578,8 +578,8 @@ CodeDefine def_define( Str name, MacroType type, Opts_def_define p )
b32 register_define = ! p.dont_register_to_preprocess_macros; b32 register_define = ! p.dont_register_to_preprocess_macros;
if ( register_define ) { if ( register_define ) {
PreprocessorMacro macro_entry = { result->Name, type, p.flags }; Macro macro_entry = { result->Name, type, p.flags };
register_preprocess_macro(macro_entry); register_macro(macro_entry);
} }
return result; return result;
} }
@ -1274,7 +1274,7 @@ CodeUsing def_using_namespace( Str name )
CodeVar def_variable( CodeTypename type, Str name, Opts_def_variable p ) CodeVar def_variable( CodeTypename type, Str name, Opts_def_variable p )
{ {
if ( ! name_check( def_variable, name ) || null_check( def_variable, type ) ) { if ( ! name_check( def_variable, name ) || ! null_check( def_variable, type ) ) {
GEN_DEBUG_TRAP(); GEN_DEBUG_TRAP();
return InvalidCode; return InvalidCode;
} }

View File

@ -128,8 +128,8 @@ s32 lex_preprocessor_define( LexContext* ctx )
b32 not_specifier = spec == Spec_Invalid; b32 not_specifier = spec == Spec_Invalid;
b32 not_attribute = attrib <= Tok___Attributes_Start; b32 not_attribute = attrib <= Tok___Attributes_Start;
PreprocessorMacro macro = { name.Text, MT_Expression, (MacroFlags)0 }; Macro macro = { name.Text, MT_Expression, (MacroFlags)0 };
PreprocessorMacro* registered_macro = lookup_preprocess_macro(name.Text); Macro* registered_macro = lookup_macro(name.Text);
if ( registered_macro == nullptr && not_specifier && not_attribute ) { if ( registered_macro == nullptr && not_specifier && not_attribute ) {
log_fmt("Warning: '%S' was not registered before the lexer processed its #define directive, it will be registered as a expression macro\n" log_fmt("Warning: '%S' was not registered before the lexer processed its #define directive, it will be registered as a expression macro\n"
@ -155,7 +155,7 @@ s32 lex_preprocessor_define( LexContext* ctx )
array_append( _ctx->Lexer_Tokens, opening_paren ); array_append( _ctx->Lexer_Tokens, opening_paren );
move_forward(); move_forward();
Token last_parameter; Token last_parameter = {};
// We need to tokenize the define's arguments now: // We need to tokenize the define's arguments now:
while( ctx->left && * ctx->scanner != ')') while( ctx->left && * ctx->scanner != ')')
{ {
@ -256,7 +256,7 @@ s32 lex_preprocessor_define( LexContext* ctx )
} }
if ( registered_macro == nullptr ) { if ( registered_macro == nullptr ) {
register_preprocess_macro(macro); register_macro(macro);
} }
// Define's content handled by lex_preprocessor_directive (the original caller of this) // Define's content handled by lex_preprocessor_directive (the original caller of this)
@ -525,52 +525,31 @@ void lex_found_token( LexContext* ctx )
return; return;
} }
PreprocessorMacro* macro = lookup_preprocess_macro( ctx->token.Text ); Macro* macro = lookup_macro( ctx->token.Text );
b32 has_args = ctx->left && (* ctx->scanner) == '('; b32 has_args = ctx->left && (* ctx->scanner) == '(';
b32 resolved_to_macro = false; b32 resolved_to_macro = false;
if (macro) { if (macro) {
ctx->token.Type = macrotype_to_toktype(macro->Type); ctx->token.Type = macrotype_to_toktype(macro->Type);
b32 is_functional = macro_is_functional(* macro); b32 is_functional = macro_is_functional(* macro);
resolved_to_macro = has_args ? is_functional : ! is_functional; resolved_to_macro = has_args ? is_functional : ! is_functional;
if ( ! resolved_to_macro ) {
log_fmt("Info(%d, %d): %S identified as a macro but usage here does not resolve to one (interpreting as identifier)\n"
, ctx->token.Line
, ctx->token.Line
, macro->Name
);
}
} }
if ( resolved_to_macro ) if ( resolved_to_macro )
{ {
// TODO(Ed): When we introduce a macro AST (and expression support), we'll properly lex this section. // TODO(Ed): When we introduce a macro AST (and expression support), we'll properly lex this section.
// Want to ignore any arguments the define may have as they can be execution expressions. // Want to ignore any arguments the define may have as they can be execution expressions.
if ( has_args ) if ( has_args ) {
{
ctx->token.Flags |= TF_Macro_Functional; ctx->token.Flags |= TF_Macro_Functional;
// move_forward();
// ctx->token.Text.Len++;
// s32 level = 0;
// while ( ctx->left && ((* ctx->scanner) != ')' || level > 0) )
// {
// if ( (* ctx->scanner) == '(' )
// level++;
// else if ( (* ctx->scanner) == ')' && level > 0 )
// level--;
// move_forward();
// ctx->token.Text.Len++;
// }
// move_forward();
// ctx->token.Text.Len++;
} }
if ( bitfield_is_set(MacroFlags, macro->Flags, MF_Allow_As_Attribute) ) {
//if ( (* ctx->scanner) == '\r' && ctx->scanner[1] == '\n' ) ctx->token.Flags |= TF_Attribute;
//{ }
// move_forward();
// ctx->token..Text.Length++;
//}
//else if ( (* ctx->scanner) == '\n' )
//{
// move_forward();
// ctx->token..Text.Length++;
//}
} }
else else
{ {

View File

@ -97,7 +97,7 @@ bool lex__eat(TokArray* self, TokType type )
b32 is_identifier = at_idx.Type == Tok_Identifier; b32 is_identifier = at_idx.Type == Tok_Identifier;
if ( not_accepted ) if ( not_accepted )
{ {
PreprocessorMacro* macro = lookup_preprocess_macro(at_idx.Text); Macro* macro = lookup_macro(at_idx.Text);
b32 accept_as_identifier = macro && bitfield_is_set(MacroFlags, macro->Flags, MF_Allow_As_Identifier ); b32 accept_as_identifier = macro && bitfield_is_set(MacroFlags, macro->Flags, MF_Allow_As_Identifier );
not_accepted = type == Tok_Identifier && accept_as_identifier ? false : true; not_accepted = type == Tok_Identifier && accept_as_identifier ? false : true;
} }
@ -196,7 +196,6 @@ internal CodeComment parse_comment ();
internal Code parse_complicated_definition ( TokType which ); internal Code parse_complicated_definition ( TokType which );
internal CodeBody parse_class_struct_body ( TokType which, Token name ); internal CodeBody parse_class_struct_body ( TokType which, Token name );
internal Code parse_class_struct ( TokType which, bool inplace_def ); internal Code parse_class_struct ( TokType which, bool inplace_def );
internal CodeDefine parser_parse_define ();
internal Code parse_expression (); internal Code parse_expression ();
internal Code parse_forward_or_definition ( TokType which, bool is_inplace ); internal Code parse_forward_or_definition ( TokType which, bool is_inplace );
internal CodeFn parse_function_after_name ( ModuleFlag mflags, CodeAttributes attributes, CodeSpecifiers specifiers, CodeTypename ret_type, Token name ); internal CodeFn parse_function_after_name ( ModuleFlag mflags, CodeAttributes attributes, CodeSpecifiers specifiers, CodeTypename ret_type, Token name );
@ -205,6 +204,7 @@ internal CodeBody parse_global_nspace ( CodeType which
internal Code parse_global_nspace_constructor_destructor( CodeSpecifiers specifiers ); internal Code parse_global_nspace_constructor_destructor( CodeSpecifiers specifiers );
internal Token parse_identifier ( bool* possible_member_function ); internal Token parse_identifier ( bool* possible_member_function );
internal CodeInclude parse_include (); internal CodeInclude parse_include ();
internal Code parse_macro_as_definiton ( CodeAttributes attributes, CodeSpecifiers specifiers );
internal CodeOperator parse_operator_after_ret_type ( ModuleFlag mflags, CodeAttributes attributes, CodeSpecifiers specifiers, CodeTypename ret_type ); internal CodeOperator parse_operator_after_ret_type ( ModuleFlag mflags, CodeAttributes attributes, CodeSpecifiers specifiers, CodeTypename ret_type );
internal Code parse_operator_function_or_variable( bool expects_function, CodeAttributes attributes, CodeSpecifiers specifiers ); internal Code parse_operator_function_or_variable( bool expects_function, CodeAttributes attributes, CodeSpecifiers specifiers );
internal CodePragma parse_pragma (); internal CodePragma parse_pragma ();
@ -218,6 +218,7 @@ internal CodeVar parse_variable_declaration_list ();
internal CodeClass parser_parse_class ( bool inplace_def ); internal CodeClass parser_parse_class ( bool inplace_def );
internal CodeConstructor parser_parse_constructor ( CodeSpecifiers specifiers ); internal CodeConstructor parser_parse_constructor ( CodeSpecifiers specifiers );
internal CodeDefine parser_parse_define ();
internal CodeDestructor parser_parse_destructor ( CodeSpecifiers specifiers ); internal CodeDestructor parser_parse_destructor ( CodeSpecifiers specifiers );
internal CodeEnum parser_parse_enum ( bool inplace_def ); internal CodeEnum parser_parse_enum ( bool inplace_def );
internal CodeBody parser_parse_export_body (); internal CodeBody parser_parse_export_body ();
@ -578,7 +579,7 @@ CodeAttributes parse_attributes()
s32 len = 0; s32 len = 0;
// There can be more than one attribute. If there is flatten them to a single string. // There can be more than one attribute. If there is flatten them to a single string.
// TODO(Ed): Support keeping an linked list of attributes similar to parameters // TODO(Ed): Support chaining attributes (Use parameter linkage pattern)
while ( left && tok_is_attribute(currtok) ) while ( left && tok_is_attribute(currtok) )
{ {
if ( check( Tok_Attribute_Open ) ) if ( check( Tok_Attribute_Open ) )
@ -1293,89 +1294,6 @@ Code parse_complicated_definition( TokType which )
} }
} }
internal inline
CodeDefine parser_parse_define()
{
push_scope();
if ( check(Tok_Preprocess_Hash)) {
// If parse_define is called by the user the hash reach here.
eat(Tok_Preprocess_Hash);
}
eat( Tok_Preprocess_Define );
// #define
CodeDefine
define = (CodeDefine) make_code();
define->Type = CT_Preprocess_Define;
if ( ! check( Tok_Identifier ) ) {
log_failure( "Error, expected identifier after #define\n%s", parser_to_strbuilder(_ctx->parser) );
parser_pop(& _ctx->parser);
return InvalidCode;
}
_ctx->parser.Scope->Name = currtok.Text;
define->Name = cache_str( tok_to_str(currtok) );
eat( Tok_Identifier );
// #define <Name>
PreprocessorMacro* macro = lookup_preprocess_macro(define->Name);
if (macro_is_functional(* macro)) {
eat( Tok_Capture_Start );
// #define <Name> (
CodeDefineParams params;
if ( left && currtok.Type != Tok_Capture_End ) {
params = (CodeDefineParams) make_code();
params->Type = CT_Parameters_Define;
params->Name = currtok.Text;
params->NumEntries ++;
define->Params = params;
eat( Tok_Preprocess_Define_Param );
// #define <Name> ( <param>
}
while( left && currtok.Type != Tok_Capture_End ) {
eat( Tok_Comma );
// #define <Name> ( <param>,
CodeDefineParams next_param = (CodeDefineParams) make_code();
next_param->Type = CT_Parameters_Define;
next_param->Name = currtok.Text;
define_params_append(params, next_param);
// #define <Name> ( <param>, <next_param> ...
eat( Tok_Preprocess_Define_Param );
}
eat( Tok_Capture_End );
// #define <Name> ( <params> )
}
if ( ! check( Tok_Preprocess_Content ))
{
log_failure( "Error, expected content after #define %s\n%s", define->Name, parser_to_strbuilder(_ctx->parser) );
parser_pop(& _ctx->parser);
return InvalidCode;
}
if ( currtok.Text.Len == 0 )
{
define->Body = untyped_str( txt("\n") );
eat( Tok_Preprocess_Content );
// #define <Name> ( <params> ) <Content>
parser_pop(& _ctx->parser);
return define;
}
define->Body = untyped_str( strbuilder_to_str( parser_strip_formatting( tok_to_str(currtok), parser_strip_formatting_dont_preserve_newlines )) );
eat( Tok_Preprocess_Content );
// #define <Name> ( <params> ) <Content>
parser_pop(& _ctx->parser);
return define;
}
internal inline internal inline
Code parse_assignment_expression() Code parse_assignment_expression()
{ {
@ -2517,6 +2435,12 @@ Code parse_operator_function_or_variable( bool expects_function, CodeAttributes
Code result = InvalidCode; Code result = InvalidCode;
Code macro_stmt = parse_macro_as_definiton(attributes, specifiers);
if (macro_stmt) {
parser_pop(& _ctx->parser);
return macro_stmt;
}
CodeTypename type = parser_parse_type( parser_not_from_template, nullptr ); CodeTypename type = parser_parse_type( parser_not_from_template, nullptr );
// <Attributes> <Specifiers> <ReturnType/ValueType> // <Attributes> <Specifiers> <ReturnType/ValueType>
@ -2566,7 +2490,40 @@ Code parse_operator_function_or_variable( bool expects_function, CodeAttributes
// Example : <Capture_Start> <Value> <Comma> // Example : <Capture_Start> <Value> <Comma>
// idx +1 +2 // idx +1 +2
bool detected_comma = _ctx->parser.Tokens.Arr[ _ctx->parser.Tokens.Idx + 2 ].Type == Tok_Comma; bool detected_comma = _ctx->parser.Tokens.Arr[ _ctx->parser.Tokens.Idx + 2 ].Type == Tok_Comma;
if ( detected_capture && ! detected_comma )
b32 detected_non_varadic_unpaired_param = detected_comma && nexttok.Type != Tok_Varadic_Argument;
if (! detected_non_varadic_unpaired_param && nexttok.Type == Tok_Preprocess_Macro_Expr) for( s32 break_scope = 0; break_scope == 0; ++ break_scope)
{
Macro* macro = lookup_macro( nexttok.Text );
if (macro == nullptr || ! macro_is_functional(* macro))
break;
// ( <Macro_Expr> (
// Idx +1 +2
s32 idx = _ctx->parser.Tokens.Idx + 1;
s32 level = 0;
// Find end of the token expression
for ( ; idx < array_num(_ctx->parser.Tokens.Arr); idx++ )
{
Token tok = _ctx->parser.Tokens.Arr[ idx ];
if ( tok.Type == Tok_Capture_Start )
level++;
else if ( tok.Type == Tok_Capture_End && level > 0 )
level--;
if (level == 0 && tok.Type == Tok_Capture_End)
break;
}
++ idx; // Will incremnt to possible comma position
if ( _ctx->parser.Tokens.Arr[ idx ].Type != Tok_Comma )
break;
detected_non_varadic_unpaired_param = true;
}
if ( detected_capture && ! detected_non_varadic_unpaired_param )
{ {
// Dealing with a function // Dealing with a function
result = cast(Code, parse_function_after_name( ModuleFlag_None, attributes, specifiers, type, name )); result = cast(Code, parse_function_after_name( ModuleFlag_None, attributes, specifiers, type, name ));
@ -2589,6 +2546,36 @@ Code parse_operator_function_or_variable( bool expects_function, CodeAttributes
return result; return result;
} }
internal
Code parse_macro_as_definiton( CodeAttributes attributes, CodeSpecifiers specifiers )
{
push_scope();
if (currtok.Type != Tok_Preprocess_Macro_Stmt ) {
parser_pop(& _ctx->parser);
return NullCode;
}
Macro* macro = lookup_macro(currtok.Text);
b32 can_resolve_to_definition = macro && bitfield_is_set(MacroFlags, macro->Flags, MF_Allow_As_Definition);
if ( ! can_resolve_to_definition) {
parser_pop(& _ctx->parser);
return NullCode;
}
// TODO(Ed): When AST_Macro is made, have it support attributs and specifiers for when its behaving as a declaration/definition.
Code code = parse_simple_preprocess( Tok_Preprocess_Macro_Stmt );
// Attributes and sepcifiers will be collapsed into the macro's serialization.
StrBuilder resolved_definition = strbuilder_fmt_buf(_ctx->Allocator_Temp, "%S %S %S"
, attributes ? strbuilder_to_str( attributes_to_strbuilder(attributes)) : txt("")
, specifiers ? strbuilder_to_str( specifiers_to_strbuilder(specifiers)) : txt("")
, code->Content
);
Code result = untyped_str( strbuilder_to_str(resolved_definition) );
parser_pop(& _ctx->parser);
return result;
}
internal internal
CodePragma parse_pragma() CodePragma parse_pragma()
{ {
@ -2945,7 +2932,7 @@ Code parse_simple_preprocess( TokType which )
eat( which ); eat( which );
// <Macro> // <Macro>
PreprocessorMacro* macro = lookup_preprocess_macro( full_macro.Text ); Macro* macro = lookup_macro( full_macro.Text );
if ( which != Tok_Preprocess_Unsupported && macro == nullptr ) { if ( which != Tok_Preprocess_Unsupported && macro == nullptr ) {
log_failure("Expected the macro %S to be registered\n%S", full_macro, parser_to_strbuilder(_ctx->parser)); log_failure("Expected the macro %S to be registered\n%S", full_macro, parser_to_strbuilder(_ctx->parser));
return NullCode; return NullCode;
@ -3048,7 +3035,7 @@ Code parse_simple_preprocess( TokType which )
|| str_contains(calling_proc, txt("parse_class_struct_body")) || str_contains(calling_proc, txt("parse_class_struct_body"))
) )
{ {
if (peektok.Type == Tok_Statement_End) if (left && peektok.Type == Tok_Statement_End)
{ {
Token stmt_end = currtok; Token stmt_end = currtok;
eat( Tok_Statement_End ); eat( Tok_Statement_End );
@ -3484,6 +3471,93 @@ CodeConstructor parser_parse_constructor( CodeSpecifiers specifiers )
return result; return result;
} }
internal inline
CodeDefine parser_parse_define()
{
push_scope();
if ( check(Tok_Preprocess_Hash)) {
// If parse_define is called by the user the hash reach here.
eat(Tok_Preprocess_Hash);
}
eat( Tok_Preprocess_Define );
// #define
CodeDefine
define = (CodeDefine) make_code();
define->Type = CT_Preprocess_Define;
if ( ! check( Tok_Identifier ) ) {
log_failure( "Error, expected identifier after #define\n%s", parser_to_strbuilder(_ctx->parser) );
parser_pop(& _ctx->parser);
return InvalidCode;
}
_ctx->parser.Scope->Name = currtok.Text;
define->Name = cache_str( tok_to_str(currtok) );
eat( Tok_Identifier );
// #define <Name>
Macro* macro = lookup_macro(define->Name);
if (macro_is_functional(* macro)) {
eat( Tok_Capture_Start );
// #define <Name> (
// We provide the define params even if empty to make sure '()' are serialized.
CodeDefineParams
params = (CodeDefineParams) make_code();
params->Type = CT_Parameters_Define;
if ( left && currtok.Type != Tok_Capture_End ) {
params->Name = currtok.Text;
params->NumEntries ++;
eat( Tok_Preprocess_Define_Param );
// #define <Name> ( <param>
}
while( left && currtok.Type != Tok_Capture_End ) {
eat( Tok_Comma );
// #define <Name> ( <param>,
CodeDefineParams next_param = (CodeDefineParams) make_code();
next_param->Type = CT_Parameters_Define;
next_param->Name = currtok.Text;
define_params_append(params, next_param);
// #define <Name> ( <param>, <next_param> ...
eat( Tok_Preprocess_Define_Param );
}
eat( Tok_Capture_End );
// #define <Name> ( <params> )
define->Params = params;
}
if ( ! check( Tok_Preprocess_Content ))
{
log_failure( "Error, expected content after #define %s\n%s", define->Name, parser_to_strbuilder(_ctx->parser) );
parser_pop(& _ctx->parser);
return InvalidCode;
}
if ( currtok.Text.Len == 0 )
{
define->Body = untyped_str( txt("\n") );
eat( Tok_Preprocess_Content );
// #define <Name> ( <params> ) <Content>
parser_pop(& _ctx->parser);
return define;
}
define->Body = untyped_str( strbuilder_to_str( parser_strip_formatting( tok_to_str(currtok), parser_strip_formatting_dont_preserve_newlines )) );
eat( Tok_Preprocess_Content );
// #define <Name> ( <params> ) <Content>
parser_pop(& _ctx->parser);
return define;
}
internal internal
CodeDestructor parser_parse_destructor( CodeSpecifiers specifiers ) CodeDestructor parser_parse_destructor( CodeSpecifiers specifiers )
{ {
@ -3757,8 +3831,11 @@ CodeEnum parser_parse_enum( bool inplace_def )
// Unreal UMETA macro support // Unreal UMETA macro support
if ( currtok.Type == Tok_Preprocess_Macro_Expr ) { if ( currtok.Type == Tok_Preprocess_Macro_Expr ) {
eat( Tok_Preprocess_Macro_Expr ); Code macro = parse_simple_preprocess( Tok_Preprocess_Macro_Expr );
// <Name> = <Expression> <Macro> // <Name> = <Expression> <Macro>
// We're intentially ignoring this code as its going to be serialized as an untyped string with the rest of the enum "entry".
// TODO(Ed): We need a CodeEnumEntry, AST_EnumEntry types
} }
if ( currtok.Type == Tok_Comma ) if ( currtok.Type == Tok_Comma )
@ -4056,6 +4133,13 @@ CodeFn parser_parse_function()
} }
// <export> <Attributes> <Specifiers> // <export> <Attributes> <Specifiers>
// Note(Ed): We're enforcing that using this codepath requires non-macro jank.
// Code macro_stmt = parse_macro_as_definiton(attributes, specifiers);
// if (macro_stmt) {
// parser_pop(& _ctx->parser);
// return macro_stmt;
// }
CodeTypename ret_type = parser_parse_type(parser_not_from_template, nullptr); CodeTypename ret_type = parser_parse_type(parser_not_from_template, nullptr);
if ( cast(Code, ret_type) == Code_Invalid ) { if ( cast(Code, ret_type) == Code_Invalid ) {
parser_pop(& _ctx->parser); parser_pop(& _ctx->parser);
@ -4960,11 +5044,10 @@ CodeTypedef parser_parse_typedef()
valid_macro |= left && currtok.Type == Tok_Preprocess_Macro_Stmt; valid_macro |= left && currtok.Type == Tok_Preprocess_Macro_Stmt;
// if (currtok.Type == Tok_Preprocess_Macro_Stmt) // if (currtok.Type == Tok_Preprocess_Macro_Stmt)
// { // {
// PreprocessMacro* macro = lookup_preprocess_macro(currtok.Text); // PreprocessMacro* macro = lookup_macro(currtok.Text);
// valid_macro |= macro && macro_expects_body(* macro)); // valid_macro |= macro && macro_expects_body(* macro));
// } // }
Code macro;
if ( valid_macro ) if ( valid_macro )
#endif #endif
{ {
@ -5444,6 +5527,13 @@ CodeVar parser_parse_variable()
} }
// <ModuleFlags> <Attributes> <Specifiers> // <ModuleFlags> <Attributes> <Specifiers>
// Note(Ed): We're enforcing that using this codepath requires non-macro jank.
// Code macro_stmt = parse_macro_as_definiton(attributes, specifiers);
// if (macro_stmt) {
// parser_pop(& _ctx->parser);
// return macro_stmt;
// }
CodeTypename type = parser_parse_type(parser_not_from_template, nullptr); CodeTypename type = parser_parse_type(parser_not_from_template, nullptr);
// <ModuleFlags> <Attributes> <Specifiers> <ValueType> // <ModuleFlags> <Attributes> <Specifiers> <ValueType>

View File

@ -152,6 +152,7 @@ TokType macrotype_to_toktype( MacroType type ) {
return Tok_Invalid; return Tok_Invalid;
} }
inline
Str macrotype_to_str( MacroType type ) Str macrotype_to_str( MacroType type )
{ {
local_persist local_persist
@ -177,15 +178,29 @@ enum EMacroFlags : u16
{ {
MF_Functional = bit(0), // Macro has parameters (args expected to be passed) MF_Functional = bit(0), // Macro has parameters (args expected to be passed)
MF_Expects_Body = bit(1), // Expects to assign a braced scope to its body. MF_Expects_Body = bit(1), // Expects to assign a braced scope to its body.
MF_Allow_As_Identifier = bit(2), // lex__eat wil treat this macro as an identifier if the parser attempts to consume it as one.
// ^^^ This is a sort of kludge because we don't support push/pop macro programs rn. ^^^ // lex__eat wil treat this macro as an identifier if the parser attempts to consume it as one.
// ^^^ This is a kludge because we don't support push/pop macro pragmas rn.
MF_Allow_As_Identifier = bit(2),
// lex__eat wil treat this macro as an attribute if the parser attempts to consume it as one.
// ^^^ This a kludge because unreal has a macro that behaves as both a 'statement' and an attribute (UE_DEPRECATED, PRAGMA_ENABLE_DEPRECATION_WARNINGS, etc)
// TODO(Ed): We can keep the MF_Allow_As_Attribute flag for macros, however, we need to add the ability of AST_Attributes to chain themselves.
// Its thats already a thing in the standard language anyway
// & it would allow UE_DEPRECATED, (UE_PROPERTY / UE_FUNCTION) to chain themselves as attributes of a resolved member function/varaible definition
MF_Allow_As_Attribute = bit(3),
// When a macro is encountered after attributs and specifiers while parsing a function, or variable:
// It will consume the macro and treat it as resolving the definition. (Yes this is for Unreal Engine)
// (MUST BE OF MT_Statement TYPE)
MF_Allow_As_Definition = bit(4),
MF_Null = 0, MF_Null = 0,
MF_UnderlyingType = GEN_U16_MAX, MF_UnderlyingType = GEN_U16_MAX,
}; };
typedef u16 MacroFlags; typedef u16 MacroFlags;
struct PreprocessorMacro struct Macro
{ {
StrCached Name; StrCached Name;
MacroType Type; MacroType Type;
@ -193,11 +208,18 @@ struct PreprocessorMacro
}; };
forceinline forceinline
b32 macro_is_functional( PreprocessorMacro macro ) { b32 macro_is_functional( Macro macro ) {
return bitfield_is_set( b16, macro.Flags, MF_Functional ); return bitfield_is_set( b16, macro.Flags, MF_Functional );
} }
forceinline forceinline
b32 macro_expects_body( PreprocessorMacro macro ) { b32 macro_expects_body( Macro macro ) {
return bitfield_is_set( b16, macro.Flags, MF_Expects_Body ); return bitfield_is_set( b16, macro.Flags, MF_Expects_Body );
} }
#if GEN_COMPILER_CPP && ! GEN_C_LIKE_CPP
forceinline b32 is_functional( Macro macro ) { return bitfield_is_set( b16, macro.Flags, MF_Functional ); }
forceinline b32 expects_body ( Macro macro ) { return bitfield_is_set( b16, macro.Flags, MF_Expects_Body ); }
#endif
typedef HashTable(Macro) MacroTable;

View File

@ -9,7 +9,7 @@ global Context* _ctx;
#pragma region Constants #pragma region Constants
global u32 context_counter; global u32 context_counter;
global PreprocessorMacro enum_underlying_macro; global Macro enum_underlying_macro;
global Code Code_Global; global Code Code_Global;
global Code Code_Invalid; global Code Code_Invalid;

View File

@ -143,6 +143,7 @@ Array<Type> array_init_reserve(AllocatorInfo allocator, ssize capacity)
return {rcast(Type*, header + 1)}; return {rcast(Type*, header + 1)};
} }
forceinline
usize array_grow_formula(ssize value) { usize array_grow_formula(ssize value) {
return 2 * value + 8; return 2 * value + 8;
} }
@ -202,7 +203,7 @@ bool array_append_at(Array<Type>* array, Type item, usize idx)
ArrayHeader* header = array_get_header(* array); ArrayHeader* header = array_get_header(* array);
ssize slot = idx; ssize slot = idx;
if (slot >= header->Num) if (slot >= (ssize)(header->Num))
slot = header->Num - 1; slot = header->Num - 1;
if (slot < 0) if (slot < 0)
@ -354,7 +355,6 @@ bool array_reserve(Array<Type>* array, usize new_capacity)
{ {
GEN_ASSERT( array != nullptr); GEN_ASSERT( array != nullptr);
GEN_ASSERT(* array != nullptr); GEN_ASSERT(* array != nullptr);
GEN_ASSERT(num > 0)
ArrayHeader* header = array_get_header(array); ArrayHeader* header = array_get_header(array);
if (header->Capacity < new_capacity) if (header->Capacity < new_capacity)
@ -763,7 +763,7 @@ HashTableFindResult hashtable__find(HashTable<Type> table, u64 key)
} }
template<typename Type> forceinline template<typename Type> forceinline
bool hashtable_full(HashTable<Type> table) { b32 hashtable_full(HashTable<Type> table) {
GEN_ASSERT_NOT_NULL(table.Hashes); GEN_ASSERT_NOT_NULL(table.Hashes);
GEN_ASSERT_NOT_NULL(table.Entries); GEN_ASSERT_NOT_NULL(table.Entries);
usize critical_load = usize(HashTable_CriticalLoadScale * f32(array_num(table.Hashes))); usize critical_load = usize(HashTable_CriticalLoadScale * f32(array_num(table.Hashes)));

View File

@ -44,7 +44,7 @@
// NOTE: Things that shouldn't happen with a message! // NOTE: Things that shouldn't happen with a message!
#define GEN_PANIC( msg, ... ) GEN_ASSERT_MSG( 0, msg, ##__VA_ARGS__ ) #define GEN_PANIC( msg, ... ) GEN_ASSERT_MSG( 0, msg, ##__VA_ARGS__ )
#if GEN_BULD_DEBUG #if GEN_BUILD_DEBUG
#define GEN_FATAL( ... ) \ #define GEN_FATAL( ... ) \
do \ do \
{ \ { \

View File

@ -86,14 +86,16 @@
#define stringize( ... ) stringize_va( __VA_ARGS__ ) #define stringize( ... ) stringize_va( __VA_ARGS__ )
#endif #endif
#define src_line_str stringize(__LINE__)
#ifndef do_once #ifndef do_once
#define do_once() \ #define do_once() \
static int __do_once_counter_##__LINE__ = 0; \ static int __do_once_counter_##src_line_str = 0; \
for(; __do_once_counter_##__LINE__ != 1; __do_once_counter_##__LINE__ = 1 ) \ for(; __do_once_counter_##src_line_str != 1; __do_once_counter_##src_line_str = 1 ) \
#define do_once_defer( expression ) \ #define do_once_defer( expression ) \
static int __do_once_counter_##__LINE__ = 0; \ static int __do_once_counter_##src_line_str = 0; \
for(; __do_once_counter_##__LINE__ != 1; __do_once_counter_##__LINE__ = 1, (expression)) \ for(; __do_once_counter_##src_line_str != 1; __do_once_counter_##src_line_str = 1, (expression)) \
#define do_once_start \ #define do_once_start \
do \ do \

View File

@ -355,9 +355,9 @@ void* arena_allocator_proc( void* allocator_data, AllocType type, ssize size, ss
{ {
// zpl__printf_err("%s", "Arena out of memory\n"); // zpl__printf_err("%s", "Arena out of memory\n");
GEN_FATAL("Arena out of memory! (Possibly could not fit for the largest size Arena!!)"); GEN_FATAL("Arena out of memory! (Possibly could not fit for the largest size Arena!!)");
return nullptr;
} }
ptr = align_forward( end, alignment ); ptr = align_forward( end, alignment );
arena->TotalUsed += total_size; arena->TotalUsed += total_size;

View File

@ -229,7 +229,6 @@ forceinline void check(Arena& arena) { return arena_check(& arena); }
#pragma pop_macro("check") #pragma pop_macro("check")
#endif #endif
inline inline
AllocatorInfo arena_allocator_info( Arena* arena ) { AllocatorInfo arena_allocator_info( Arena* arena ) {
GEN_ASSERT(arena != nullptr); GEN_ASSERT(arena != nullptr);
@ -392,9 +391,9 @@ void pool_clear(Pool* pool);
void pool_free(Pool* pool); void pool_free(Pool* pool);
#if GEN_COMPILER_CPP && ! GEN_C_LIKE_CPP #if GEN_COMPILER_CPP && ! GEN_C_LIKE_CPP
AllocatorInfo allocator_info(Pool& pool) { return pool_allocator_info(& pool); } forceinline AllocatorInfo allocator_info(Pool& pool) { return pool_allocator_info(& pool); }
void clear(Pool& pool) { return pool_clear(& pool); } forceinline void clear(Pool& pool) { return pool_clear(& pool); }
void free(Pool& pool) { return pool_free(& pool); } forceinline void free(Pool& pool) { return pool_free(& pool); }
#endif #endif
struct Pool struct Pool

View File

@ -40,9 +40,9 @@ struct Str
#ifndef txt #ifndef txt
# if GEN_COMPILER_CPP # if GEN_COMPILER_CPP
# define txt( text ) Str { ( text ), sizeof( text ) - 1 } # define txt( text ) GEN_NS Str { ( text ), sizeof( text ) - 1 }
# else # else
# define txt( text ) (Str){ ( text ), sizeof( text ) - 1 } # define txt( text ) (GEN_NS Str){ ( text ), sizeof( text ) - 1 }
# endif # endif
#endif #endif
@ -392,6 +392,7 @@ bool strbuilder_append_string(StrBuilder* str, StrBuilder const other) {
return strbuilder_append_c_str_len(str, (char const*)other, strbuilder_length(other)); return strbuilder_append_c_str_len(str, (char const*)other, strbuilder_length(other));
} }
inline
bool strbuilder_append_fmt(StrBuilder* str, char const* fmt, ...) { bool strbuilder_append_fmt(StrBuilder* str, char const* fmt, ...) {
GEN_ASSERT(str != nullptr); GEN_ASSERT(str != nullptr);
ssize res; ssize res;

View File

@ -237,7 +237,7 @@ CodeBody gen_especifier( char const* path, bool use_c_definition = false )
Str spec_to_str( Specifier type ) Str spec_to_str( Specifier type )
{ {
local_persist local_persist
Str lookup[<num>] = { Str lookup[] = {
<entries> <entries>
}; };
@ -283,7 +283,7 @@ CodeBody gen_especifier( char const* path, bool use_c_definition = false )
body_append(result, enum_code); body_append(result, enum_code);
if (use_c_definition) if (use_c_definition)
{ {
CodeTypedef specifier_t = parse_typedef( code(typedef u32 Specifier; )); CodeTypedef specifier_t = parse_typedef( code(typedef enum Specifier Specifier; ));
body_append(result, specifier_t); body_append(result, specifier_t);
} }
@ -499,60 +499,60 @@ CodeBody gen_ast_inlines()
#pragma pop_macro("GEN_NS") #pragma pop_macro("GEN_NS")
#pragma pop_macro("CodeInvalid") #pragma pop_macro("CodeInvalid")
CodeBody impl_code = parse_global_body( token_fmt( "typename", Str name(Code), code_impl_tmpl )); CodeBody impl_code = parse_global_body( token_fmt( "typename", name(Code), code_impl_tmpl ));
CodeBody impl_code_body = parse_global_body( token_fmt( "typename", Str name(CodeBody), code_impl_tmpl )); CodeBody impl_code_body = parse_global_body( token_fmt( "typename", name(CodeBody), code_impl_tmpl ));
CodeBody impl_code_attr = parse_global_body( token_fmt( "typename", Str name(CodeAttributes), code_impl_tmpl )); CodeBody impl_code_attr = parse_global_body( token_fmt( "typename", name(CodeAttributes), code_impl_tmpl ));
CodeBody impl_code_cmt = parse_global_body( token_fmt( "typename", Str name(CodeComment), code_impl_tmpl )); CodeBody impl_code_cmt = parse_global_body( token_fmt( "typename", name(CodeComment), code_impl_tmpl ));
CodeBody impl_code_constr = parse_global_body( token_fmt( "typename", Str name(CodeConstructor), code_impl_tmpl )); CodeBody impl_code_constr = parse_global_body( token_fmt( "typename", name(CodeConstructor), code_impl_tmpl ));
CodeBody impl_code_class = parse_global_body( token_fmt( "typename", Str name(CodeClass), code_impl_tmpl )); CodeBody impl_code_class = parse_global_body( token_fmt( "typename", name(CodeClass), code_impl_tmpl ));
CodeBody impl_code_define = parse_global_body( token_fmt( "typename", Str name(CodeDefine), code_impl_tmpl )); CodeBody impl_code_define = parse_global_body( token_fmt( "typename", name(CodeDefine), code_impl_tmpl ));
CodeBody impl_code_define_params = parse_global_body( token_fmt( "typename", Str name(CodeDefineParams), code_impl_tmpl )); CodeBody impl_code_define_params = parse_global_body( token_fmt( "typename", name(CodeDefineParams), code_impl_tmpl ));
CodeBody impl_code_destruct = parse_global_body( token_fmt( "typename", Str name(CodeDestructor), code_impl_tmpl )); CodeBody impl_code_destruct = parse_global_body( token_fmt( "typename", name(CodeDestructor), code_impl_tmpl ));
CodeBody impl_code_enum = parse_global_body( token_fmt( "typename", Str name(CodeEnum), code_impl_tmpl )); CodeBody impl_code_enum = parse_global_body( token_fmt( "typename", name(CodeEnum), code_impl_tmpl ));
CodeBody impl_code_exec = parse_global_body( token_fmt( "typename", Str name(CodeExec), code_impl_tmpl )); CodeBody impl_code_exec = parse_global_body( token_fmt( "typename", name(CodeExec), code_impl_tmpl ));
CodeBody impl_code_extern = parse_global_body( token_fmt( "typename", Str name(CodeExtern), code_impl_tmpl )); CodeBody impl_code_extern = parse_global_body( token_fmt( "typename", name(CodeExtern), code_impl_tmpl ));
CodeBody impl_code_include = parse_global_body( token_fmt( "typename", Str name(CodeInclude), code_impl_tmpl )); CodeBody impl_code_include = parse_global_body( token_fmt( "typename", name(CodeInclude), code_impl_tmpl ));
CodeBody impl_code_friend = parse_global_body( token_fmt( "typename", Str name(CodeFriend), code_impl_tmpl )); CodeBody impl_code_friend = parse_global_body( token_fmt( "typename", name(CodeFriend), code_impl_tmpl ));
CodeBody impl_code_fn = parse_global_body( token_fmt( "typename", Str name(CodeFn), code_impl_tmpl )); CodeBody impl_code_fn = parse_global_body( token_fmt( "typename", name(CodeFn), code_impl_tmpl ));
CodeBody impl_code_module = parse_global_body( token_fmt( "typename", Str name(CodeModule), code_impl_tmpl )); CodeBody impl_code_module = parse_global_body( token_fmt( "typename", name(CodeModule), code_impl_tmpl ));
CodeBody impl_code_ns = parse_global_body( token_fmt( "typename", Str name(CodeNS), code_impl_tmpl )); CodeBody impl_code_ns = parse_global_body( token_fmt( "typename", name(CodeNS), code_impl_tmpl ));
CodeBody impl_code_op = parse_global_body( token_fmt( "typename", Str name(CodeOperator), code_impl_tmpl )); CodeBody impl_code_op = parse_global_body( token_fmt( "typename", name(CodeOperator), code_impl_tmpl ));
CodeBody impl_code_opcast = parse_global_body( token_fmt( "typename", Str name(CodeOpCast), code_impl_tmpl )); CodeBody impl_code_opcast = parse_global_body( token_fmt( "typename", name(CodeOpCast), code_impl_tmpl ));
CodeBody impl_code_params = parse_global_body( token_fmt( "typename", Str name(CodeParams), code_impl_tmpl )); CodeBody impl_code_params = parse_global_body( token_fmt( "typename", name(CodeParams), code_impl_tmpl ));
CodeBody impl_code_pragma = parse_global_body( token_fmt( "typename", Str name(CodePragma), code_impl_tmpl )); CodeBody impl_code_pragma = parse_global_body( token_fmt( "typename", name(CodePragma), code_impl_tmpl ));
CodeBody impl_code_precond = parse_global_body( token_fmt( "typename", Str name(CodePreprocessCond), code_impl_tmpl )); CodeBody impl_code_precond = parse_global_body( token_fmt( "typename", name(CodePreprocessCond), code_impl_tmpl ));
CodeBody impl_code_specs = parse_global_body( token_fmt( "typename", Str name(CodeSpecifiers), code_impl_tmpl )); CodeBody impl_code_specs = parse_global_body( token_fmt( "typename", name(CodeSpecifiers), code_impl_tmpl ));
CodeBody impl_code_struct = parse_global_body( token_fmt( "typename", Str name(CodeStruct), code_impl_tmpl )); CodeBody impl_code_struct = parse_global_body( token_fmt( "typename", name(CodeStruct), code_impl_tmpl ));
CodeBody impl_code_tmpl = parse_global_body( token_fmt( "typename", Str name(CodeTemplate), code_impl_tmpl )); CodeBody impl_code_tmpl = parse_global_body( token_fmt( "typename", name(CodeTemplate), code_impl_tmpl ));
CodeBody impl_code_type = parse_global_body( token_fmt( "typename", Str name(CodeTypename), code_impl_tmpl )); CodeBody impl_code_type = parse_global_body( token_fmt( "typename", name(CodeTypename), code_impl_tmpl ));
CodeBody impl_code_typedef = parse_global_body( token_fmt( "typename", Str name(CodeTypedef), code_impl_tmpl )); CodeBody impl_code_typedef = parse_global_body( token_fmt( "typename", name(CodeTypedef), code_impl_tmpl ));
CodeBody impl_code_union = parse_global_body( token_fmt( "typename", Str name(CodeUnion), code_impl_tmpl )); CodeBody impl_code_union = parse_global_body( token_fmt( "typename", name(CodeUnion), code_impl_tmpl ));
CodeBody impl_code_using = parse_global_body( token_fmt( "typename", Str name(CodeUsing), code_impl_tmpl )); CodeBody impl_code_using = parse_global_body( token_fmt( "typename", name(CodeUsing), code_impl_tmpl ));
CodeBody impl_code_var = parse_global_body( token_fmt( "typename", Str name(CodeVar), code_impl_tmpl )); CodeBody impl_code_var = parse_global_body( token_fmt( "typename", name(CodeVar), code_impl_tmpl ));
body_append(impl_code_attr, parse_global_body( token_fmt( "typename", Str name(Attributes), codetype_impl_tmpl ))); body_append(impl_code_attr, parse_global_body( token_fmt( "typename", name(Attributes), codetype_impl_tmpl )));
body_append(impl_code_cmt, parse_global_body( token_fmt( "typename", Str name(Comment), codetype_impl_tmpl ))); body_append(impl_code_cmt, parse_global_body( token_fmt( "typename", name(Comment), codetype_impl_tmpl )));
body_append(impl_code_constr, parse_global_body( token_fmt( "typename", Str name(Constructor), codetype_impl_tmpl ))); body_append(impl_code_constr, parse_global_body( token_fmt( "typename", name(Constructor), codetype_impl_tmpl )));
body_append(impl_code_define, parse_global_body( token_fmt( "typename", Str name(Define), codetype_impl_tmpl ))); body_append(impl_code_define, parse_global_body( token_fmt( "typename", name(Define), codetype_impl_tmpl )));
body_append(impl_code_destruct, parse_global_body( token_fmt( "typename", Str name(Destructor), codetype_impl_tmpl ))); body_append(impl_code_destruct, parse_global_body( token_fmt( "typename", name(Destructor), codetype_impl_tmpl )));
body_append(impl_code_enum, parse_global_body( token_fmt( "typename", Str name(Enum), codetype_impl_tmpl ))); body_append(impl_code_enum, parse_global_body( token_fmt( "typename", name(Enum), codetype_impl_tmpl )));
body_append(impl_code_exec, parse_global_body( token_fmt( "typename", Str name(Exec), codetype_impl_tmpl ))); body_append(impl_code_exec, parse_global_body( token_fmt( "typename", name(Exec), codetype_impl_tmpl )));
body_append(impl_code_extern, parse_global_body( token_fmt( "typename", Str name(Extern), codetype_impl_tmpl ))); body_append(impl_code_extern, parse_global_body( token_fmt( "typename", name(Extern), codetype_impl_tmpl )));
body_append(impl_code_include, parse_global_body( token_fmt( "typename", Str name(Include), codetype_impl_tmpl ))); body_append(impl_code_include, parse_global_body( token_fmt( "typename", name(Include), codetype_impl_tmpl )));
body_append(impl_code_friend, parse_global_body( token_fmt( "typename", Str name(Friend), codetype_impl_tmpl ))); body_append(impl_code_friend, parse_global_body( token_fmt( "typename", name(Friend), codetype_impl_tmpl )));
body_append(impl_code_fn, parse_global_body( token_fmt( "typename", Str name(Fn), codetype_impl_tmpl ))); body_append(impl_code_fn, parse_global_body( token_fmt( "typename", name(Fn), codetype_impl_tmpl )));
body_append(impl_code_module, parse_global_body( token_fmt( "typename", Str name(Module), codetype_impl_tmpl ))); body_append(impl_code_module, parse_global_body( token_fmt( "typename", name(Module), codetype_impl_tmpl )));
body_append(impl_code_ns, parse_global_body( token_fmt( "typename", Str name(NS), codetype_impl_tmpl ))); body_append(impl_code_ns, parse_global_body( token_fmt( "typename", name(NS), codetype_impl_tmpl )));
body_append(impl_code_op, parse_global_body( token_fmt( "typename", Str name(Operator), codetype_impl_tmpl ))); body_append(impl_code_op, parse_global_body( token_fmt( "typename", name(Operator), codetype_impl_tmpl )));
body_append(impl_code_opcast, parse_global_body( token_fmt( "typename", Str name(OpCast), codetype_impl_tmpl ))); body_append(impl_code_opcast, parse_global_body( token_fmt( "typename", name(OpCast), codetype_impl_tmpl )));
body_append(impl_code_pragma, parse_global_body( token_fmt( "typename", Str name(Pragma), codetype_impl_tmpl ))); body_append(impl_code_pragma, parse_global_body( token_fmt( "typename", name(Pragma), codetype_impl_tmpl )));
body_append(impl_code_precond, parse_global_body( token_fmt( "typename", Str name(PreprocessCond), codetype_impl_tmpl ))); body_append(impl_code_precond, parse_global_body( token_fmt( "typename", name(PreprocessCond), codetype_impl_tmpl )));
body_append(impl_code_tmpl, parse_global_body( token_fmt( "typename", Str name(Template), codetype_impl_tmpl ))); body_append(impl_code_tmpl, parse_global_body( token_fmt( "typename", name(Template), codetype_impl_tmpl )));
body_append(impl_code_type, parse_global_body( token_fmt( "typename", Str name(Typename), codetype_impl_tmpl ))); body_append(impl_code_type, parse_global_body( token_fmt( "typename", name(Typename), codetype_impl_tmpl )));
body_append(impl_code_typedef, parse_global_body( token_fmt( "typename", Str name(Typedef), codetype_impl_tmpl ))); body_append(impl_code_typedef, parse_global_body( token_fmt( "typename", name(Typedef), codetype_impl_tmpl )));
body_append(impl_code_union, parse_global_body( token_fmt( "typename", Str name(Union), codetype_impl_tmpl ))); body_append(impl_code_union, parse_global_body( token_fmt( "typename", name(Union), codetype_impl_tmpl )));
body_append(impl_code_using, parse_global_body( token_fmt( "typename", Str name(Using), codetype_impl_tmpl ))); body_append(impl_code_using, parse_global_body( token_fmt( "typename", name(Using), codetype_impl_tmpl )));
body_append(impl_code_var, parse_global_body( token_fmt( "typename", Str name(Var), codetype_impl_tmpl ))); body_append(impl_code_var, parse_global_body( token_fmt( "typename", name(Var), codetype_impl_tmpl )));
#pragma push_macro("forceinline") #pragma push_macro("forceinline")
#undef forceinline #undef forceinline
@ -564,35 +564,35 @@ CodeBody gen_ast_inlines()
); );
#pragma pop_macro("forceinline") #pragma pop_macro("forceinline")
CodeBody impl_cast_body = parse_global_body( token_fmt( "typename", Str name(Body), cast_tmpl )); CodeBody impl_cast_body = parse_global_body( token_fmt( "typename", name(Body), cast_tmpl ));
CodeBody impl_cast_attribute = parse_global_body( token_fmt( "typename", Str name(Attributes), cast_tmpl )); CodeBody impl_cast_attribute = parse_global_body( token_fmt( "typename", name(Attributes), cast_tmpl ));
CodeBody impl_cast_cmt = parse_global_body( token_fmt( "typename", Str name(Comment), cast_tmpl )); CodeBody impl_cast_cmt = parse_global_body( token_fmt( "typename", name(Comment), cast_tmpl ));
CodeBody impl_cast_constr = parse_global_body( token_fmt( "typename", Str name(Constructor), cast_tmpl )); CodeBody impl_cast_constr = parse_global_body( token_fmt( "typename", name(Constructor), cast_tmpl ));
CodeBody impl_cast_class = parse_global_body( token_fmt( "typename", Str name(Class), cast_tmpl )); CodeBody impl_cast_class = parse_global_body( token_fmt( "typename", name(Class), cast_tmpl ));
CodeBody impl_cast_define = parse_global_body( token_fmt( "typename", Str name(Define), cast_tmpl )); CodeBody impl_cast_define = parse_global_body( token_fmt( "typename", name(Define), cast_tmpl ));
CodeBody impl_cast_define_params = parse_global_body( token_fmt( "typename", Str name(DefineParams), cast_tmpl )); CodeBody impl_cast_define_params = parse_global_body( token_fmt( "typename", name(DefineParams), cast_tmpl ));
CodeBody impl_cast_destruct = parse_global_body( token_fmt( "typename", Str name(Destructor), cast_tmpl )); CodeBody impl_cast_destruct = parse_global_body( token_fmt( "typename", name(Destructor), cast_tmpl ));
CodeBody impl_cast_enum = parse_global_body( token_fmt( "typename", Str name(Enum), cast_tmpl )); CodeBody impl_cast_enum = parse_global_body( token_fmt( "typename", name(Enum), cast_tmpl ));
CodeBody impl_cast_exec = parse_global_body( token_fmt( "typename", Str name(Exec), cast_tmpl )); CodeBody impl_cast_exec = parse_global_body( token_fmt( "typename", name(Exec), cast_tmpl ));
CodeBody impl_cast_extern = parse_global_body( token_fmt( "typename", Str name(Extern), cast_tmpl )); CodeBody impl_cast_extern = parse_global_body( token_fmt( "typename", name(Extern), cast_tmpl ));
CodeBody impl_cast_friend = parse_global_body( token_fmt( "typename", Str name(Friend), cast_tmpl )); CodeBody impl_cast_friend = parse_global_body( token_fmt( "typename", name(Friend), cast_tmpl ));
CodeBody impl_cast_fn = parse_global_body( token_fmt( "typename", Str name(Fn), cast_tmpl )); CodeBody impl_cast_fn = parse_global_body( token_fmt( "typename", name(Fn), cast_tmpl ));
CodeBody impl_cast_include = parse_global_body( token_fmt( "typename", Str name(Include), cast_tmpl )); CodeBody impl_cast_include = parse_global_body( token_fmt( "typename", name(Include), cast_tmpl ));
CodeBody impl_cast_module = parse_global_body( token_fmt( "typename", Str name(Module), cast_tmpl )); CodeBody impl_cast_module = parse_global_body( token_fmt( "typename", name(Module), cast_tmpl ));
CodeBody impl_cast_ns = parse_global_body( token_fmt( "typename", Str name(NS), cast_tmpl )); CodeBody impl_cast_ns = parse_global_body( token_fmt( "typename", name(NS), cast_tmpl ));
CodeBody impl_cast_op = parse_global_body( token_fmt( "typename", Str name(Operator), cast_tmpl )); CodeBody impl_cast_op = parse_global_body( token_fmt( "typename", name(Operator), cast_tmpl ));
CodeBody impl_cast_opcast = parse_global_body( token_fmt( "typename", Str name(OpCast), cast_tmpl )); CodeBody impl_cast_opcast = parse_global_body( token_fmt( "typename", name(OpCast), cast_tmpl ));
CodeBody impl_cast_params = parse_global_body( token_fmt( "typename", Str name(Params), cast_tmpl )); CodeBody impl_cast_params = parse_global_body( token_fmt( "typename", name(Params), cast_tmpl ));
CodeBody impl_cast_pragma = parse_global_body( token_fmt( "typename", Str name(Pragma), cast_tmpl )); CodeBody impl_cast_pragma = parse_global_body( token_fmt( "typename", name(Pragma), cast_tmpl ));
CodeBody impl_cast_precond = parse_global_body( token_fmt( "typename", Str name(PreprocessCond), cast_tmpl )); CodeBody impl_cast_precond = parse_global_body( token_fmt( "typename", name(PreprocessCond), cast_tmpl ));
CodeBody impl_cast_specs = parse_global_body( token_fmt( "typename", Str name(Specifiers), cast_tmpl )); CodeBody impl_cast_specs = parse_global_body( token_fmt( "typename", name(Specifiers), cast_tmpl ));
CodeBody impl_cast_struct = parse_global_body( token_fmt( "typename", Str name(Struct), cast_tmpl )); CodeBody impl_cast_struct = parse_global_body( token_fmt( "typename", name(Struct), cast_tmpl ));
CodeBody impl_cast_tmpl = parse_global_body( token_fmt( "typename", Str name(Template), cast_tmpl )); CodeBody impl_cast_tmpl = parse_global_body( token_fmt( "typename", name(Template), cast_tmpl ));
CodeBody impl_cast_type = parse_global_body( token_fmt( "typename", Str name(Typename), cast_tmpl )); CodeBody impl_cast_type = parse_global_body( token_fmt( "typename", name(Typename), cast_tmpl ));
CodeBody impl_cast_typedef = parse_global_body( token_fmt( "typename", Str name(Typedef), cast_tmpl )); CodeBody impl_cast_typedef = parse_global_body( token_fmt( "typename", name(Typedef), cast_tmpl ));
CodeBody impl_cast_union = parse_global_body( token_fmt( "typename", Str name(Union), cast_tmpl )); CodeBody impl_cast_union = parse_global_body( token_fmt( "typename", name(Union), cast_tmpl ));
CodeBody impl_cast_using = parse_global_body( token_fmt( "typename", Str name(Using), cast_tmpl )); CodeBody impl_cast_using = parse_global_body( token_fmt( "typename", name(Using), cast_tmpl ));
CodeBody impl_cast_var = parse_global_body( token_fmt( "typename", Str name(Var), cast_tmpl )); CodeBody impl_cast_var = parse_global_body( token_fmt( "typename", name(Var), cast_tmpl ));
CodeBody result = def_global_body( args( CodeBody result = def_global_body( args(
def_pragma( txt("region generated code inline implementation")), def_pragma( txt("region generated code inline implementation")),

View File

@ -65,122 +65,122 @@ int gen_main()
Context ctx {}; Context ctx {};
gen::init(& ctx); gen::init(& ctx);
register_preprocess_macros( args( register_macros( args(
(PreprocessorMacro { txt("bit"), MT_Expression, MF_Functional }), (Macro { txt("bit"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("bitfield_is_set"), MT_Expression, MF_Functional }), (Macro { txt("bitfield_is_set"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("GEN_C_LIKE_CPP"), MT_Expression, MF_Null }), (Macro { txt("GEN_C_LIKE_CPP"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("cast"), MT_Expression, MF_Functional }), (Macro { txt("cast"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("ccast"), MT_Expression, MF_Functional }), (Macro { txt("ccast"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("rcast"), MT_Expression, MF_Functional }), (Macro { txt("rcast"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("pcast"), MT_Expression, MF_Functional }), (Macro { txt("pcast"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("scast"), MT_Expression, MF_Functional }), (Macro { txt("scast"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("stringize_va"), MT_Expression, MF_Functional }), (Macro { txt("stringize_va"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("stringize"), MT_Expression, MF_Functional }), (Macro { txt("stringize"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("do_once"), MT_Expression, MF_Functional }), (Macro { txt("do_once"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("do_once_defer"), MT_Expression, MF_Functional }), (Macro { txt("do_once_defer"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("do_once_start"), MT_Statement, MF_Null }), (Macro { txt("do_once_start"), MT_Statement, MF_Null }),
(PreprocessorMacro { txt("do_once_end"), MT_Statement, MF_Null }), (Macro { txt("do_once_end"), MT_Statement, MF_Null }),
(PreprocessorMacro { txt("labeled_scope_start"), MT_Statement, MF_Null }), (Macro { txt("labeled_scope_start"), MT_Statement, MF_Null }),
(PreprocessorMacro { txt("labeled_scope_end"), MT_Statement, MF_Null }), (Macro { txt("labeled_scope_end"), MT_Statement, MF_Null }),
(PreprocessorMacro { txt("compiler_decorated_func_name"), MT_Expression, MF_Null }), (Macro { txt("compiler_decorated_func_name"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("num_args_impl"), MT_Expression, MF_Functional }), (Macro { txt("num_args_impl"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("num_args"), MT_Expression, MF_Functional }), (Macro { txt("num_args"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("count_of"), MT_Expression, MF_Functional }), (Macro { txt("count_of"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("clamp"), MT_Expression, MF_Functional }), (Macro { txt("clamp"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("is_between"), MT_Expression, MF_Functional }), (Macro { txt("is_between"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("size_of"), MT_Expression, MF_Functional }), (Macro { txt("size_of"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("min"), MT_Expression, MF_Functional }), (Macro { txt("min"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("max"), MT_Expression, MF_Functional }), (Macro { txt("max"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("offset_of"), MT_Expression, MF_Functional }), (Macro { txt("offset_of"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("static_assert"), MT_Statement, MF_Functional }), (Macro { txt("static_assert"), MT_Statement, MF_Functional }),
(PreprocessorMacro { txt("typeof"), MT_Expression, MF_Null }), (Macro { txt("typeof"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("GEN_API_C_BEGIN"), MT_Statement, MF_Null }), (Macro { txt("GEN_API_C_BEGIN"), MT_Statement, MF_Null }),
(PreprocessorMacro { txt("GEN_API_C_END"), MT_Statement, MF_Null }), (Macro { txt("GEN_API_C_END"), MT_Statement, MF_Null }),
(PreprocessorMacro { txt("nullptr"), MT_Expression, MF_Null }), (Macro { txt("nullptr"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("GEN_REMOVE_PTR"), MT_Expression, MF_Functional }), (Macro { txt("GEN_REMOVE_PTR"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("GEN_PARAM_DEFAULT"), MT_Expression, MF_Null }), (Macro { txt("GEN_PARAM_DEFAULT"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("struct_init"), MT_Expression, MF_Functional }), (Macro { txt("struct_init"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("GEN_OPTIMIZE_MAPPINGS_BEGIN"), MT_Statement, MF_Null }), (Macro { txt("GEN_OPTIMIZE_MAPPINGS_BEGIN"), MT_Statement, MF_Null }),
(PreprocessorMacro { txt("GEN_OPITMIZE_MAPPINGS_END"), MT_Statement, MF_Null }), (Macro { txt("GEN_OPITMIZE_MAPPINGS_END"), MT_Statement, MF_Null }),
(PreprocessorMacro { txt("Array"), MT_Typename, MF_Functional }), (Macro { txt("Array"), MT_Typename, MF_Functional }),
(PreprocessorMacro { txt("HashTable"), MT_Typename, MF_Functional }), (Macro { txt("HashTable"), MT_Typename, MF_Functional }),
(PreprocessorMacro { txt("Using_Code"), MT_Statement, MF_Functional }), (Macro { txt("Using_Code"), MT_Statement, MF_Functional }),
(PreprocessorMacro { txt("Using_CodeOps"), MT_Statement, MF_Functional }), (Macro { txt("Using_CodeOps"), MT_Statement, MF_Functional }),
(PreprocessorMacro { txt("kilobytes"), MT_Expression, MF_Functional }), (Macro { txt("kilobytes"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("megabytes"), MT_Expression, MF_Functional }), (Macro { txt("megabytes"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("gigabytes"), MT_Expression, MF_Functional }), (Macro { txt("gigabytes"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("terabytes"), MT_Expression, MF_Functional }), (Macro { txt("terabytes"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("GEN__ONES"), MT_Expression, MF_Null }), (Macro { txt("GEN__ONES"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("GEN__HIGHS"), MT_Expression, MF_Null }), (Macro { txt("GEN__HIGHS"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("GEN__HAS_ZERO"), MT_Expression, MF_Functional }), (Macro { txt("GEN__HAS_ZERO"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("zero_item"), MT_Expression, MF_Functional }), (Macro { txt("zero_item"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("zero_array"), MT_Expression, MF_Functional }), (Macro { txt("zero_array"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("GEN_DEFAULT_MEMORY_ALIGNMENT"), MT_Expression, MF_Null }), (Macro { txt("GEN_DEFAULT_MEMORY_ALIGNMENT"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("GEN_DEFAULT_ALLOCATOR_FLAGS"), MT_Expression, MF_Null }), (Macro { txt("GEN_DEFAULT_ALLOCATOR_FLAGS"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("alloc_item"), MT_Expression, MF_Functional }), (Macro { txt("alloc_item"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("alloc_array"), MT_Expression, MF_Functional }), (Macro { txt("alloc_array"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("malloc"), MT_Expression, MF_Functional }), (Macro { txt("malloc"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("mfree"), MT_Expression, MF_Functional }), (Macro { txt("mfree"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("GEN_PRINTF_MAXLEN"), MT_Expression, MF_Null }), (Macro { txt("GEN_PRINTF_MAXLEN"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("cast_to_str"), MT_Expression, MF_Functional }), (Macro { txt("cast_to_str"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("current"), MT_Expression, MF_Null }), (Macro { txt("current"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("txt"), MT_Expression, MF_Functional }), (Macro { txt("txt"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("GEN_FILE_OPEN_PROC"), MT_Statement, MF_Functional | MF_Expects_Body }), (Macro { txt("GEN_FILE_OPEN_PROC"), MT_Statement, MF_Functional | MF_Expects_Body }),
(PreprocessorMacro { txt("GEN_FILE_READ_AT_PROC"), MT_Statement, MF_Functional | MF_Expects_Body }), (Macro { txt("GEN_FILE_READ_AT_PROC"), MT_Statement, MF_Functional | MF_Expects_Body }),
(PreprocessorMacro { txt("GEN_FILE_WRITE_AT_PROC"), MT_Statement, MF_Functional | MF_Expects_Body }), (Macro { txt("GEN_FILE_WRITE_AT_PROC"), MT_Statement, MF_Functional | MF_Expects_Body }),
(PreprocessorMacro { txt("GEN_FILE_SEEK_PROC"), MT_Statement, MF_Functional | MF_Expects_Body }), (Macro { txt("GEN_FILE_SEEK_PROC"), MT_Statement, MF_Functional | MF_Expects_Body }),
(PreprocessorMacro { txt("GEN_FILE_CLOSE_PROC"), MT_Statement, MF_Functional | MF_Expects_Body }), (Macro { txt("GEN_FILE_CLOSE_PROC"), MT_Statement, MF_Functional | MF_Expects_Body }),
(PreprocessorMacro { txt("log_failure"), MT_Expression, MF_Null }), (Macro { txt("log_failure"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("operator"), MT_Expression, MF_Null }), (Macro { txt("operator"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("InvalidCode"), MT_Expression, MF_Null }), (Macro { txt("InvalidCode"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("NullCode"), MT_Expression, MF_Null }), (Macro { txt("NullCode"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("Verify_POD"), MT_Expression, MF_Functional }), (Macro { txt("Verify_POD"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("gen_main"), MT_Statement, MF_Null }) (Macro { txt("gen_main"), MT_Statement, MF_Null })
)); ));
register_preprocess_macros( args( register_macros( args(
(PreprocessorMacro { txt("name"), MT_Expression, MF_Functional }), (Macro { txt("name"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("code"), MT_Expression, MF_Functional }), (Macro { txt("code"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("args"), MT_Expression, MF_Functional }), (Macro { txt("args"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("code_str"), MT_Expression, MF_Functional }), (Macro { txt("code_str"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("code_fmt"), MT_Expression, MF_Functional }), (Macro { txt("code_fmt"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("parse_fmt"), MT_Expression, MF_Functional }), (Macro { txt("parse_fmt"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("token_fmt"), MT_Expression, MF_Functional }), (Macro { txt("token_fmt"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("check_member_val"), MT_Expression, MF_Functional }), (Macro { txt("check_member_val"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("check_member_str"), MT_Expression, MF_Functional }), (Macro { txt("check_member_str"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("check_member_content"), MT_Expression, MF_Functional }), (Macro { txt("check_member_content"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("check_member_ast"), MT_Expression, MF_Functional }), (Macro { txt("check_member_ast"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("check_params"), MT_Expression, MF_Functional }), (Macro { txt("check_params"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("check_param_eq_ret"), MT_Expression, MF_Functional }), (Macro { txt("check_param_eq_ret"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("specs"), MT_Expression, MF_Functional | MF_Allow_As_Identifier }), (Macro { txt("specs"), MT_Expression, MF_Functional | MF_Allow_As_Identifier }),
(PreprocessorMacro { txt("name_check"), MT_Expression, MF_Functional }), (Macro { txt("name_check"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("null_check"), MT_Expression, MF_Functional }), (Macro { txt("null_check"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("def_body_start"), MT_Expression, MF_Functional }), (Macro { txt("def_body_start"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("def_body_code_array_start"), MT_Expression, MF_Functional }), (Macro { txt("def_body_code_array_start"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("move_forward"), MT_Expression, MF_Functional }), (Macro { txt("move_forward"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("skip_whitespace"), MT_Expression, MF_Functional }), (Macro { txt("skip_whitespace"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("end_line"), MT_Expression, MF_Functional }), (Macro { txt("end_line"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("check_parse_args"), MT_Expression, MF_Functional }), (Macro { txt("check_parse_args"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("currtok_noskip"), MT_Expression, MF_Null }), (Macro { txt("currtok_noskip"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("currtok"), MT_Expression, MF_Null }), (Macro { txt("currtok"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("peektok"), MT_Expression, MF_Null }), (Macro { txt("peektok"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("prevtok"), MT_Expression, MF_Null }), (Macro { txt("prevtok"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("nexttok"), MT_Expression, MF_Null }), (Macro { txt("nexttok"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("nexttok_noskip"), MT_Expression, MF_Null }), (Macro { txt("nexttok_noskip"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("eat"), MT_Expression, MF_Functional }), (Macro { txt("eat"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("left"), MT_Expression, MF_Null | MF_Allow_As_Identifier }), (Macro { txt("left"), MT_Expression, MF_Null | MF_Allow_As_Identifier }),
(PreprocessorMacro { txt("def_assign"), MT_Expression, MF_Functional }), (Macro { txt("def_assign"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("CHECK_WAS_DEFINED"), MT_Expression, MF_Null }), (Macro { txt("CHECK_WAS_DEFINED"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("check_noskip"), MT_Expression, MF_Functional }), (Macro { txt("check_noskip"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("check"), MT_Expression, MF_Functional | MF_Allow_As_Identifier }), (Macro { txt("check"), MT_Expression, MF_Functional | MF_Allow_As_Identifier }),
(PreprocessorMacro { txt("push_scope"), MT_Expression, MF_Functional }), (Macro { txt("push_scope"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("cut_length"), MT_Expression, MF_Null }), (Macro { txt("cut_length"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("cut_ptr"), MT_Expression, MF_Null }), (Macro { txt("cut_ptr"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("pos"), MT_Expression, MF_Null }), (Macro { txt("pos"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("move_fwd"), MT_Expression, MF_Functional }), (Macro { txt("move_fwd"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("Entry"), MT_Expression, MF_Functional }), (Macro { txt("Entry"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("CheckEndParams"), MT_Expression, MF_Functional }), (Macro { txt("CheckEndParams"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("UseTemplateCapture"), MT_Expression, MF_Null }), (Macro { txt("UseTemplateCapture"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("check_current"), MT_Expression, MF_Functional }) (Macro { txt("check_current"), MT_Expression, MF_Functional })
)); ));
Code push_ignores = scan_file( path_base "helpers/push_ignores.inline.hpp" ); Code push_ignores = scan_file( path_base "helpers/push_ignores.inline.hpp" );
@ -243,7 +243,7 @@ int gen_main()
CodeTemplate tmpl = cast(CodeTemplate, entry); CodeTemplate tmpl = cast(CodeTemplate, entry);
if ( tmpl->Declaration->Name.contains(txt("swap"))) if ( tmpl->Declaration->Name.contains(txt("swap")))
{ {
register_preprocess_macro({ txt("swap"), MT_Expression, MF_Functional }); register_macro({ txt("swap"), MT_Expression, MF_Functional });
CodeDefine macro_swap = parse_define( txt(R"( CodeDefine macro_swap = parse_define( txt(R"(
#define swap( a, b ) \ #define swap( a, b ) \
do \ do \
@ -1115,6 +1115,7 @@ R"(#define <interface_name>( code ) _Generic( (code), \
CodeBody array_arena = gen_array(txt("gen_Arena"), txt("Array_gen_Arena")); CodeBody array_arena = gen_array(txt("gen_Arena"), txt("Array_gen_Arena"));
CodeBody array_pool = gen_array(txt("gen_Pool"), txt("Array_gen_Pool")); CodeBody array_pool = gen_array(txt("gen_Pool"), txt("Array_gen_Pool"));
CodeBody ht_preprocessor_macro = gen_hashtable(txt("gen_Macro"), txt("MacroTable"));
CodeBody parsed_interface = parse_file( path_base "components/interface.hpp" ); CodeBody parsed_interface = parse_file( path_base "components/interface.hpp" );
CodeBody interface = def_body(CT_Global_Body); CodeBody interface = def_body(CT_Global_Body);
@ -1354,11 +1355,37 @@ R"(#define <interface_name>( code ) _Generic( (code), \
Code src_static_data = scan_file( path_base "components/static_data.cpp" ); Code src_static_data = scan_file( path_base "components/static_data.cpp" );
Code src_ast_case_macros = scan_file( path_base "components/ast_case_macros.cpp" ); Code src_ast_case_macros = scan_file( path_base "components/ast_case_macros.cpp" );
Code src_code_serialization = scan_file( path_base "components/code_serialization.cpp" ); Code src_code_serialization = scan_file( path_base "components/code_serialization.cpp" );
Code src_interface = scan_file( path_base "components/interface.cpp" );
Code src_parsing_interface = scan_file( path_base "components/interface.parsing.cpp" ); Code src_parsing_interface = scan_file( path_base "components/interface.parsing.cpp" );
Code src_untyped = scan_file( path_base "components/interface.untyped.cpp" ); Code src_untyped = scan_file( path_base "components/interface.untyped.cpp" );
Code src_parser_case_macros = scan_file( path_base "components/parser_case_macros.cpp" ); Code src_parser_case_macros = scan_file( path_base "components/parser_case_macros.cpp" );
CodeBody parsed_src_interface = parse_file( path_base "components/interface.cpp" );
CodeBody src_interface = def_body(CT_Global_Body);
for ( Code entry = parsed_src_interface.begin(); entry != parsed_src_interface.end(); ++ entry ) switch( entry ->Type )
{
case CT_Function:
{
CodeFn fn = cast(CodeFn, entry);
Code prev = entry->Prev;
for ( CodeParams arr_param : fn->Params )
{
b32 repeat_register_macros = fn->Name.is_equal(txt("register_macros")) && arr_param->Name.is_equal(txt("num")) && ! arr_param->Next->Name.is_equal(txt("..."));
if ( repeat_register_macros ) {
// rename second definition so there isn't a symbol conflict
StrBuilder postfix_arr = StrBuilder::fmt_buf(_ctx->Allocator_Temp, "%S_arr", fn->Name);
fn->Name = cache_str(postfix_arr.to_str());
}
}
src_interface.append(fn);
}
break;
default:
src_interface.append(entry);
break;
}
CodeBody parsed_src_ast = parse_file( path_base "components/ast.cpp" ); CodeBody parsed_src_ast = parse_file( path_base "components/ast.cpp" );
CodeBody src_ast = def_body(CT_Global_Body); CodeBody src_ast = def_body(CT_Global_Body);
for ( Code entry = parsed_src_ast.begin(); entry != parsed_src_ast.end(); ++ entry ) switch( entry ->Type ) for ( Code entry = parsed_src_ast.begin(); entry != parsed_src_ast.end(); ++ entry ) switch( entry ->Type )
@ -1418,26 +1445,22 @@ R"(#define <interface_name>( code ) _Generic( (code), \
Code prev = entry->Prev; Code prev = entry->Prev;
for ( CodeParams arr_param : fn->Params ) for ( CodeParams arr_param : fn->Params )
if ( fn->Name.starts_with(txt("def_"))
&& ( (arr_param->ValueType->Name.starts_with(txt("Specifier")) && fn->Params->NumEntries > 1)
|| arr_param->ValueType->Name.starts_with(txt("Code")) )
)
{ {
b32 repeat_def_array = fn->Name.starts_with(txt("def_")) && arr_param->Name.is_equal(txt("num")) && ! arr_param->Next->Name.is_equal(txt("..."));
if ( repeat_def_array ) {
// rename second definition so there isn't a symbol conflict // rename second definition so there isn't a symbol conflict
StrBuilder postfix_arr = StrBuilder::fmt_buf(_ctx->Allocator_Temp, "%S_arr", fn->Name); StrBuilder postfix_arr = StrBuilder::fmt_buf(_ctx->Allocator_Temp, "%S_arr", fn->Name);
fn->Name = cache_str(postfix_arr.to_str()); fn->Name = cache_str(postfix_arr.to_str());
postfix_arr.free();
} }
}
for ( CodeParams opt_param : fn->Params ) if (opt_param->ValueType->Name.starts_with(txt("Opts_"))) for ( CodeParams opt_param : fn->Params ) if (opt_param->ValueType->Name.starts_with(txt("Opts_")))
{ {
// The frontend names are warapped in macros so we need to give it the intenral symbol name
Str prefix = txt("def_"); Str prefix = txt("def_");
Str actual_name = { fn->Name.Ptr + prefix.Len, fn->Name.Len - prefix.Len }; Str actual_name = { fn->Name.Ptr + prefix.Len, fn->Name.Len - prefix.Len };
Str new_name = StrBuilder::fmt_buf(_ctx->Allocator_Temp, "def__%S", actual_name ).to_str(); Str new_name = StrBuilder::fmt_buf(_ctx->Allocator_Temp, "def__%S", actual_name ).to_str();
fn->Name = cache_str(new_name); fn->Name = cache_str(new_name);
} }
src_upfront.append(fn); src_upfront.append(fn);
} }
break; break;
@ -1588,6 +1611,7 @@ R"(#define <interface_name>( code ) _Generic( (code), \
Code rf_interface = refactor_and_format(interface); Code rf_interface = refactor_and_format(interface);
Code rf_inlines = refactor_and_format(inlines); Code rf_inlines = refactor_and_format(inlines);
Code rf_ht_preprocessor_macro = refactor_and_format(ht_preprocessor_macro);
Code rf_array_string_cached = refactor_and_format(array_string_cached); Code rf_array_string_cached = refactor_and_format(array_string_cached);
Code rf_header_end = refactor_and_format(header_end); Code rf_header_end = refactor_and_format(header_end);
Code rf_header_builder = refactor_and_format(header_builder); Code rf_header_builder = refactor_and_format(header_builder);
@ -1613,7 +1637,7 @@ R"(#define <interface_name>( code ) _Generic( (code), \
Code r_src_code_serialization = refactor(src_code_serialization); Code r_src_code_serialization = refactor(src_code_serialization);
Code r_src_parser_case_macros = refactor(src_parser_case_macros); Code r_src_parser_case_macros = refactor(src_parser_case_macros);
Code r_src_interface = refactor(src_interface); Code r_src_interface = refactor_and_format(src_interface);
Code r_src_upfront = refactor_and_format(src_upfront); Code r_src_upfront = refactor_and_format(src_upfront);
Code r_src_lexer = refactor_and_format(src_lexer); Code r_src_lexer = refactor_and_format(src_lexer);
Code rf_array_code_typename = refactor_and_format(array_code_typename); Code rf_array_code_typename = refactor_and_format(array_code_typename);
@ -1696,6 +1720,8 @@ R"(#define <interface_name>( code ) _Generic( (code), \
header.print( rf_array_pool); header.print( rf_array_pool);
header.print( fmt_newline); header.print( fmt_newline);
header.print( rf_array_string_cached ); header.print( rf_array_string_cached );
header.print( fmt_newline);
header.print( rf_ht_preprocessor_macro );
header.print( rf_interface ); header.print( rf_interface );
header.print(fmt_newline); header.print(fmt_newline);

View File

@ -310,6 +310,14 @@ word spec_to_str, gen_spec_to_str
word spec_is_trailing, gen_spec_is_trailing word spec_is_trailing, gen_spec_is_trailing
// word str_to_specifier, gen_str_to_specifier // word str_to_specifier, gen_str_to_specifier
word MacroType, gen_MacroType
word EMacroFlags, gen_EMacroFlags
word MacroFlags, gen_MacroFlags
word Macro, gen_Macro
namespace macro_, gen_macro_
namespace macrotype, gen_macrotype_
// AST // AST
word AST, gen_AST word AST, gen_AST

View File

@ -19,7 +19,7 @@ CodeBody gen_hashtable_base()
)); ));
Code define_type = untyped_str(txt( Code define_type = untyped_str(txt(
R"(#define HashTable(_type) struct _type R"(#define HashTable(_type) struct gen_HashTable_##_type
)" )"
)); ));

View File

@ -89,11 +89,11 @@ CodeBody gen_fixed_arenas()
result.append(arena_interface_2mb); result.append(arena_interface_2mb);
result.append(arena_interface_4mb); result.append(arena_interface_4mb);
register_preprocess_macros( args( register_macros( args(
( PreprocessorMacro { txt("fixed_arena_allocator_info"), MT_Expression, MF_Functional }), ( Macro { txt("fixed_arena_allocator_info"), MT_Expression, MF_Functional }),
( PreprocessorMacro { txt("fixed_arena_init"), MT_Expression, MF_Functional }), ( Macro { txt("fixed_arena_init"), MT_Expression, MF_Functional }),
( PreprocessorMacro { txt("fixed_arena_free"), MT_Expression, MF_Functional }), ( Macro { txt("fixed_arena_free"), MT_Expression, MF_Functional }),
( PreprocessorMacro { txt("fixed_arena_size_remaining"), MT_Expression, MF_Functional }) ( Macro { txt("fixed_arena_size_remaining"), MT_Expression, MF_Functional })
)); ));
CodeDefine def = parse_define(txt("#define fixed_arena_allocator_info(fixed_arena) ( (AllocatorInfo) { arena_allocator_proc, & (fixed_arena)->arena } )\n")); CodeDefine def = parse_define(txt("#define fixed_arena_allocator_info(fixed_arena) ( (AllocatorInfo) { arena_allocator_proc, & (fixed_arena)->arena } )\n"));

View File

@ -4,4 +4,3 @@ COREUOBJECT_API, COREUOBJECT_API
ENGINE_API, ENGINE_API ENGINE_API, ENGINE_API
GAMEPLAYABILITIES_API, GAMEPLAYABILITIES_API GAMEPLAYABILITIES_API, GAMEPLAYABILITIES_API
UMG_API, UMG_API UMG_API, UMG_API
UE_DEPRECATED, UE_DEPRECATED

1 API_Export GEN_API_Export_Code
4 ENGINE_API ENGINE_API
5 GAMEPLAYABILITIES_API GAMEPLAYABILITIES_API
6 UMG_API UMG_API
UE_DEPRECATED UE_DEPRECATED

View File

@ -42,6 +42,7 @@ Number, "__number__"
Operator, "__operator__" Operator, "__operator__"
Preprocess_Hash, "#" Preprocess_Hash, "#"
Preprocess_Define, "define" Preprocess_Define, "define"
Preprocess_Define_Param, "__define_param__"
Preprocess_If, "if" Preprocess_If, "if"
Preprocess_IfDef, "ifdef" Preprocess_IfDef, "ifdef"
Preprocess_IfNotDef, "ifndef" Preprocess_IfNotDef, "ifndef"

1 Invalid __invalid__
42 Operator __operator__
43 Preprocess_Hash #
44 Preprocess_Define define
45 Preprocess_Define_Param __define_param__
46 Preprocess_If if
47 Preprocess_IfDef ifdef
48 Preprocess_IfNotDef ifndef

View File

@ -64,43 +64,43 @@ int gen_main()
Code ue_forceinline = code_str(FORCEINLINE); Code ue_forceinline = code_str(FORCEINLINE);
// Code // Code
register_preprocess_macros( args( register_macros( args(
(PreprocessorMacro { txt("bit"), MT_Expression, MF_Functional }), (Macro { txt("bit"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("bitfield_is_set"), MT_Expression, MF_Functional }), (Macro { txt("bitfield_is_set"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("GEN_C_LIKE_CPP"), MT_Expression, MF_Null }), (Macro { txt("GEN_C_LIKE_CPP"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("cast"), MT_Expression, MF_Functional }), (Macro { txt("cast"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("ccast"), MT_Expression, MF_Functional }), (Macro { txt("ccast"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("rcast"), MT_Expression, MF_Functional }), (Macro { txt("rcast"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("pcast"), MT_Expression, MF_Functional }), (Macro { txt("pcast"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("scast"), MT_Expression, MF_Functional }), (Macro { txt("scast"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("stringize_va"), MT_Expression, MF_Functional }), (Macro { txt("stringize_va"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("stringize"), MT_Expression, MF_Functional }), (Macro { txt("stringize"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("do_once"), MT_Expression, MF_Functional }), (Macro { txt("do_once"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("do_once_defer"), MT_Expression, MF_Functional }), (Macro { txt("do_once_defer"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("do_once_start"), MT_Statement, MF_Null }), (Macro { txt("do_once_start"), MT_Statement, MF_Null }),
(PreprocessorMacro { txt("do_once_end"), MT_Statement, MF_Null }), (Macro { txt("do_once_end"), MT_Statement, MF_Null }),
(PreprocessorMacro { txt("labeled_scope_start"), MT_Statement, MF_Null }), (Macro { txt("labeled_scope_start"), MT_Statement, MF_Null }),
(PreprocessorMacro { txt("labeled_scope_end"), MT_Statement, MF_Null }), (Macro { txt("labeled_scope_end"), MT_Statement, MF_Null }),
(PreprocessorMacro { txt("compiler_decorated_func_name"), MT_Expression, MF_Null }), (Macro { txt("compiler_decorated_func_name"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("num_args_impl"), MT_Expression, MF_Functional }), (Macro { txt("num_args_impl"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("num_args"), MT_Expression, MF_Functional }), (Macro { txt("num_args"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("count_of"), MT_Expression, MF_Functional }), (Macro { txt("count_of"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("clamp"), MT_Expression, MF_Functional }), (Macro { txt("clamp"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("is_between"), MT_Expression, MF_Functional }), (Macro { txt("is_between"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("size_of"), MT_Expression, MF_Functional }), (Macro { txt("size_of"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("min"), MT_Expression, MF_Functional }), (Macro { txt("min"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("max"), MT_Expression, MF_Functional }), (Macro { txt("max"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("offset_of"), MT_Expression, MF_Functional }), (Macro { txt("offset_of"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("static_assert"), MT_Statement, MF_Functional }), (Macro { txt("static_assert"), MT_Statement, MF_Functional }),
(PreprocessorMacro { txt("typeof"), MT_Expression, MF_Null }), (Macro { txt("typeof"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("GEN_API_C_BEGIN"), MT_Statement, MF_Null }), (Macro { txt("GEN_API_C_BEGIN"), MT_Statement, MF_Null }),
(PreprocessorMacro { txt("GEN_API_C_END"), MT_Statement, MF_Null }), (Macro { txt("GEN_API_C_END"), MT_Statement, MF_Null }),
(PreprocessorMacro { txt("nullptr"), MT_Expression, MF_Null }), (Macro { txt("nullptr"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("GEN_REMOVE_PTR"), MT_Expression, MF_Functional }), (Macro { txt("GEN_REMOVE_PTR"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("GEN_PARAM_DEFAULT"), MT_Expression, MF_Null }), (Macro { txt("GEN_PARAM_DEFAULT"), MT_Expression, MF_Null }),
(PreprocessorMacro { txt("struct_init"), MT_Expression, MF_Functional }), (Macro { txt("struct_init"), MT_Expression, MF_Functional }),
(PreprocessorMacro { txt("GEN_OPTIMIZE_MAPPINGS_BEGIN"), MT_Statement, MF_Null }), (Macro { txt("GEN_OPTIMIZE_MAPPINGS_BEGIN"), MT_Statement, MF_Null }),
(PreprocessorMacro { txt("GEN_OPITMIZE_MAPPINGS_END"), MT_Statement, MF_Null }) (Macro { txt("GEN_OPITMIZE_MAPPINGS_END"), MT_Statement, MF_Null })
)); ));
// gen_dep.hpp // gen_dep.hpp
@ -147,6 +147,7 @@ int gen_main()
Code strings = scan_file( path_base "dependencies/strings.hpp" ); Code strings = scan_file( path_base "dependencies/strings.hpp" );
Code filesystem = scan_file( path_base "dependencies/filesystem.hpp" ); Code filesystem = scan_file( path_base "dependencies/filesystem.hpp" );
Code timing = scan_file( path_base "dependencies/timing.hpp" ); Code timing = scan_file( path_base "dependencies/timing.hpp" );
Code parsing = scan_file( path_base "dependencies/parsing.hpp" );
Builder Builder
header = Builder::open("gen/gen.dep.hpp"); header = Builder::open("gen/gen.dep.hpp");
@ -168,6 +169,7 @@ int gen_main()
header.print( strings ); header.print( strings );
header.print( filesystem ); header.print( filesystem );
header.print( timing ); header.print( timing );
header.print(parsing);
header.print_fmt( "\nGEN_NS_END\n" ); header.print_fmt( "\nGEN_NS_END\n" );
header.print( fmt_newline ); header.print( fmt_newline );
@ -186,6 +188,7 @@ int gen_main()
Code strings = scan_file( path_base "dependencies/strings.cpp" ); Code strings = scan_file( path_base "dependencies/strings.cpp" );
Code filesystem = scan_file( path_base "dependencies/filesystem.cpp" ); Code filesystem = scan_file( path_base "dependencies/filesystem.cpp" );
Code timing = scan_file( path_base "dependencies/timing.cpp" ); Code timing = scan_file( path_base "dependencies/timing.cpp" );
Code parsing = scan_file( path_base "dependencies/parsing.cpp" );
Builder Builder
src = Builder::open( "gen/gen.dep.cpp" ); src = Builder::open( "gen/gen.dep.cpp" );
@ -204,6 +207,7 @@ int gen_main()
src.print( strings ); src.print( strings );
src.print( filesystem ); src.print( filesystem );
src.print( timing ); src.print( timing );
src.print( parsing );
src.print_fmt( "\nGEN_NS_END\n" ); src.print_fmt( "\nGEN_NS_END\n" );
src.print( fmt_newline ); src.print( fmt_newline );
@ -310,14 +314,20 @@ int gen_main()
src.print_fmt( "\n#pragma region Interface\n" ); src.print_fmt( "\n#pragma region Interface\n" );
src.print( interface ); src.print( interface );
src.print( upfront ); src.print( upfront );
src.print_fmt( "\n#pragma region Parsing\n\n" ); src.print_fmt( "\n#pragma region Parsing\n\n" );
src.print( lexer ); src.print( lexer );
src.print( parser_case_macros ); src.print( parser_case_macros );
src.print( parser ); src.print( parser );
src.print( parsing_interface ); src.print( parsing_interface );
src.print( untyped );
src.print_fmt( "\n#pragma endregion Parsing\n\n" ); src.print_fmt( "\n#pragma endregion Parsing\n\n" );
src.print_fmt( "\n#pragma region Untyped\n\n" );
src.print( untyped );
src.print_fmt( "#pragma endregion \n\n" );
src.print_fmt( "#pragma endregion Interface\n\n" ); src.print_fmt( "#pragma endregion Interface\n\n" );
src.print_fmt( "GEN_NS_END\n\n"); src.print_fmt( "GEN_NS_END\n\n");
@ -364,7 +374,6 @@ int gen_main()
// gen_scanner.hpp // gen_scanner.hpp
{ {
Code parsing = scan_file( path_base "dependencies/parsing.hpp" );
Code scanner = scan_file( path_base "auxillary/scanner.hpp" ); Code scanner = scan_file( path_base "auxillary/scanner.hpp" );
Builder Builder
@ -375,7 +384,6 @@ int gen_main()
header.print( fmt_newline ); header.print( fmt_newline );
header.print( def_include( txt("gen.hpp") ) ); header.print( def_include( txt("gen.hpp") ) );
header.print_fmt( "\nGEN_NS_BEGIN\n" ); header.print_fmt( "\nGEN_NS_BEGIN\n" );
header.print( parsing );
header.print( scanner ); header.print( scanner );
header.print_fmt( "\nGEN_NS_END\n" ); header.print_fmt( "\nGEN_NS_END\n" );
header.print( fmt_newline ); header.print( fmt_newline );
@ -385,7 +393,6 @@ int gen_main()
// gen.scanner.cpp // gen.scanner.cpp
{ {
Code parsing = scan_file( path_base "dependencies/parsing.cpp" );
Code scanner = scan_file( path_base "auxillary/scanner.cpp" ); Code scanner = scan_file( path_base "auxillary/scanner.cpp" );
Builder Builder
@ -395,8 +402,7 @@ int gen_main()
src.print( fmt_newline ); src.print( fmt_newline );
src.print( def_include( txt("gen.scanner.hpp") ) ); src.print( def_include( txt("gen.scanner.hpp") ) );
src.print_fmt( "\nGEN_NS_BEGIN\n" ); src.print_fmt( "\nGEN_NS_BEGIN\n" );
src.print( parsing ); src.print( scanner );
// src.print( scanner );
src.print_fmt( "\nGEN_NS_END\n" ); src.print_fmt( "\nGEN_NS_END\n" );
src.print( fmt_newline ); src.print( fmt_newline );
src.print( pop_ignores ); src.print( pop_ignores );