diff --git a/.vscode/settings.json b/.vscode/settings.json index 40870c8..399b819 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,5 +9,6 @@ "xutility": "cpp", "initializer_list": "cpp", "table.h": "c" - } + }, + "C_Cpp.intelliSenseEngineFallback": "disabled" } \ No newline at end of file diff --git a/project/Bloat.cpp b/project/Bloat.cpp index 5bfaf2e..813cb0b 100644 --- a/project/Bloat.cpp +++ b/project/Bloat.cpp @@ -10,7 +10,7 @@ namespace Global namespace Memory { arena Global_Arena {}; - + void setup() { arena_init_from_allocator( & Global_Arena, heap(), Initial_Reserve ); @@ -20,16 +20,16 @@ namespace Memory assert_crash( "Failed to reserve memory for Tests:: Global_Arena" ); } } - + void resize( uw new_size ) { - void* new_memory = resize( heap(), Global_Arena.physical_start, Global_Arena.total_size, new_size ); - + void* new_memory = resize( heap(), Global_Arena.physical_start, Global_Arena.total_size, new_size ); + if ( new_memory == nullptr ) { fatal("Failed to resize global arena!"); } - + Global_Arena.physical_start = new_memory; Global_Arena.total_size = new_size; } @@ -37,14 +37,14 @@ namespace Memory void cleanup() { arena_free( & Global_Arena); - } + } } struct TokEntry { char const* Str; - s32 Length; + sw Length; }; ZPL_TABLE( static, TokMap, tokmap_, TokEntry ) @@ -65,10 +65,10 @@ sw token_fmt_va( char* buf, uw buf_size, char const* fmt, s32 num_tokens, va_lis char const* token = va_arg( va, char const* ); char const* value = va_arg( va, char const* ); - TokEntry entry - { + TokEntry entry + { value, - zpl_strnlen(value, 128) + zpl_strnlen(value, (sw)128) }; u32 key = crc32( token, zpl_strnlen(token, 32) ); @@ -84,7 +84,7 @@ sw token_fmt_va( char* buf, uw buf_size, char const* fmt, s32 num_tokens, va_lis { sw len = 0; - while ( current && current != '{' && remaining ) + while ( current && current != '{' && remaining ) { *buf = *fmt; buf++; @@ -119,7 +119,7 @@ sw token_fmt_va( char* buf, uw buf_size, char const* fmt, s32 num_tokens, va_lis } scanner++; - fmt = scanner; + fmt = scanner; current = *fmt; } } diff --git a/project/Bloat.hpp b/project/Bloat.hpp index 4d5d967..73540a3 100644 --- a/project/Bloat.hpp +++ b/project/Bloat.hpp @@ -3,7 +3,7 @@ This contians all definitions not directly related to the project. */ -#pragma once +#pragma once #ifdef BLOAT_IMPL # define ZPL_IMPLEMENTATION @@ -12,7 +12,7 @@ #pragma region ZPL INCLUDE #if __clang__ -# pragma clang diagnostic push +# pragma clang diagnostic push # pragma clang diagnostic ignored "-Wmissing-braces" # pragma clang diagnostic ignored "-Wbraced-scalar-init" #endif @@ -46,32 +46,48 @@ # pragma clang diagnostic ignored "-Wswitch" # pragma clang diagnostic ignored "-Wunused-variable" # pragma clang diagnostic ignored "-Wunknown-pragmas" +# pragma clang diagnostic ignored "-Wvarargs" #endif -#if defined(__GNUC__) || defined(__clang__) || 1 - // Supports 0-10 arguments - #define VA_NARGS_IMPL( _0, \ - _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \ - _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \ - _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \ - _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \ - _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \ - N, ...) N - // ## deletes preceding comma if _VA_ARGS__ is empty (GCC, Clang) - #define VA_NARGS(...) VA_NARGS_IMPL(_, ## __VA_ARGS__, \ - 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, \ - 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, \ - 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, \ - 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \ - 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, \ - 0) +#if defined(__GNUC__) || defined(__clang__) || true + // Supports 0-10 arguments + #define macro_num_args_impl( _0, \ + _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \ + _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \ + N, ... \ + ) N + // _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \ + // _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \ + // _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, + + // ## deletes preceding comma if _VA_ARGS__ is empty (GCC, Clang) + #define macro_num_args(...) \ + macro_num_args_impl(_, ## __VA_ARGS__, \ + 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \ + 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, \ + 0 \ + ) + // 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, \ + // 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, \ + // 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, #else - // Supports 1-10 arguments - #define VA_NARGS_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N - #define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1) + // Supports 1-10 arguments + #define macro_num_args_impl( \ + _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \ + _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \ + N, ... \ + ) N + + #define macro_num_args(...) \ + macro_num_args_impl( __VA_ARGS__, \ + 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \ + 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 \ + ) #endif +#define macro_expand( Expanded_ ) Expanded_ + #define bit( Value_ ) ( 1 << Value_ ) #define bitfield_is_equal( Field_, Mask_ ) ( ( (Mask_) & (Field_) ) == (Mask_) ) #define ct constexpr @@ -83,8 +99,7 @@ #define pcast( Type_, Value_ ) ( * (Type_*)( & (Value_) ) ) #define txt_impl( Value_ ) #Value_ #define txt( Value_ ) txt_impl( Value_ ) -#define txt_with_length( Value_ ) txt_impl( Value_ ), sizeof( txt_impl( Value_ ) ) - +#define txt_n_len( Value_ ) sizeof( txt_impl( Value_ ) ), txt_impl( Value_ ) #define do_once() \ do \ { \ @@ -136,7 +151,7 @@ sw token_fmt_va( char* buf, uw buf_size, char const* fmt, s32 num_tokens, va_lis inline char const* token_fmt( char const* fmt, sw num_tokens, ... ) { - local_persist thread_local + local_persist thread_local char buf[ZPL_PRINTF_MAXLEN] = { 0 }; va_list va; @@ -148,25 +163,25 @@ char const* token_fmt( char const* fmt, sw num_tokens, ... ) } inline -sw log_fmt(char const *fmt, ...) +sw log_fmt(char const *fmt, ...) { if ( Global::ShouldShowDebug == false ) return 0; sw res; va_list va; - + va_start(va, fmt); res = zpl_printf_va(fmt, va); va_end(va); - + return res; } inline -sw fatal(char const *fmt, ...) +sw fatal(char const *fmt, ...) { - local_persist thread_local + local_persist thread_local char buf[ZPL_PRINTF_MAXLEN] = { 0 }; va_list va; diff --git a/project/Bloat.redef.hpp b/project/Bloat.redef.hpp new file mode 100644 index 0000000..4017ade --- /dev/null +++ b/project/Bloat.redef.hpp @@ -0,0 +1,4 @@ +/* + Redefine any macros. + Used if you have some code thats getting parsed and don't want it ot get preprocessed. +*/ diff --git a/project/gen.cpp b/project/gen.cpp index 2b0dc7a..f3e33dd 100644 --- a/project/gen.cpp +++ b/project/gen.cpp @@ -9,22 +9,77 @@ namespace gen namespace StaticData { + #ifndef GEN_CODE_USE_SOA static array(CodePOD) CodePool = nullptr; + #else + using DataUnion = union + { + array(AST*) Entries; + string Content; + }; + + namespace CodePool + { + array(CodeT) Type; + array(bool) Readonly; + array(AST*) Parent; + array(string) Name; + array(string) Comment; + array(DataUnion) Data; + + forceinline + CodeT& type( sw index ) + { + return Type[index]; + } + + forceinline + bool& readonly( sw index ) + { + return Readonly[index]; + } + + forceinline + AST*& parent( sw index ) + { + return Parent[index]; + } + + forceinline + string& name( sw index ) + { + return Name[index]; + } + + forceinline + string& comment( sw index ) + { + return Comment[index]; + } + + forceinline + DataUnion& data( sw index ) + { + return Data[index]; + } + } + #endif + static array(arena) StringArenas = nullptr; - static StringTable StringMap; - static TypeTable TypeMap; + static StringTable StringMap; + static TypeTable TypeMap; static sw InitSize_CodePool = megabytes(64); static sw InitSize_StringArena = megabytes(32); static sw InitSize_StringTable = megabytes(4); static sw InitSize_TypeTable = megabytes(4); - static allocator Allocator_CodePool = zpl_heap(); - static allocator Allocator_StringArena = zpl_heap(); - static allocator Allocator_StringTable = zpl_heap(); - static allocator Allocator_TypeTable = zpl_heap(); + static allocator Allocator_CodePool = heap(); + static allocator Allocator_StringArena = heap(); + static allocator Allocator_StringTable = heap(); + static allocator Allocator_TypeTable = heap(); } #pragma region CONSTANTS @@ -63,29 +118,50 @@ namespace gen { using namespace StaticData; +# ifndef GEN_CODE_USE_SOA ct CodePOD Invalid = { ECode::Invalid, false, nullptr, nullptr, nullptr, { nullptr } }; array_append( CodePool, Invalid ); return pcast( Code, array_back( CodePool )); +# else + + array_append( CodePool::Type, ECode::Invalid ); + array_append( CodePool::Readonly, false ); + array_append( CodePool::Name, nullptr ); + array_append( CodePool::Comment, nullptr ); + array_append( CodePool::Data, { nullptr } ); + + Code code { array_count( CodePool::Type) - 1 }; + + return code; +# endif } void init() { - array_init( StaticData::CodePool, g_allocator ); + array_init( StaticData::CodePool, StaticData::Allocator_CodePool ); + array_init( StaticData::StringArenas, heap() ); - Code& - InvalidCode_write = ccast( Code, InvalidCode ); + arena string_arena; + arena_init_from_allocator( & string_arena, StaticData::Allocator_StringArena, StaticData::InitSize_StringArena ); + + str_tbl_init( & StaticData::StringMap, StaticData::Allocator_StringTable ); + type_tbl_init( & StaticData::TypeMap, StaticData::Allocator_TypeTable ); + + Code& + InvalidCode_write = ccast( Code, Code::Invalid ); InvalidCode_write = make_code(); #ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS - Code& + Code& t_bool_write = ccast( Code, t_void ); t_bool_write = def_type( txt(void) ); # define def_constant_code_type( Type_ ) \ Code& \ - t_##Type_ = def_type( txt(Type_) ) + t_##Type_ = def_type( txt(Type_) ); \ + t_##Type_.lock() def_constant_code_type( bool ); def_constant_code_type( char ); @@ -106,23 +182,21 @@ namespace gen def_constant_code_type( f32 ); def_constant_code_type( f64 ); - # undef def_constant_code_type - + #endif + Code& spec_constexpr_write = ccast( Code, spec_constexpr ); spec_constexpr_write = def_specifiers( 1, ESpecifier::Constexpr ); - # define def_constant_spec( Type_, ... ) \ - Code& \ - spec_##Type_##_write = ccast( Code, spec_##Type_ ); \ - spec_##Type_##_write = def_specifiers( VA_NARGS(__VA_ARGS__), __VA_ARGS__) + # define def_constant_spec( Type_, ... ) \ + Code& \ + spec_##Type_ = def_specifiers( macro_num_args(__VA_ARGS__), __VA_ARGS__); \ + spec_##Type_.lock(); + def_constant_spec( const, ESpecifier::Const ); def_constant_spec( inline, ESpecifier::Inline ); - # undef def_constant_spec - - #endif } void clear_code_pool() @@ -211,70 +285,11 @@ namespace gen } # pragma region Upfront Constructors - Code def_params( s32 num, ... ) - { - using namespace ECode; - - if (num <= 0) - { - log_failure( "TT::make_paramters: num cannot be zero or neg" ); - return InvalidCode; - } - - Code - result = make_code(); - result->Type = Parameters; - - va_list va; - va_start(va, num); - - result->Name = string_make( g_allocator, va_arg(va, char const*) ); - - array_init( result->Entries, g_allocator ); - - Code type = va_arg(va, Code); - - if ( type->Type != Typename ) - { - log_failure( "gen::def_parameters: type of param %d is not a Typename", num - num + 1 ); - return InvalidCode; - } - - result->add( type ); - - while( num -= 2, num && num % 2 == 0 ) - { - type = va_arg(va, Code); - - Code - param = make_code(); - param->Type = Parameters; - param->Name = string_make( g_allocator, va_arg(va, char const*) ); - - array_init( param->Entries, g_allocator ); - - if ( type->Type != Typename ) - { - log_failure( "gen::def_parameters: type of param %d is not a Typename", num - num + 1 ); - return InvalidCode; - } - - param->add( type ); - param.lock(); - - result->add(param); - } - va_end(va); - - result.lock(); - return result; - } - - Code def_proc( char const* name + Code def_function( char const* name , Code specifiers , Code params , Code ret_type - , Code body + , Code body ) { using namespace ECode; @@ -282,40 +297,42 @@ namespace gen if ( specifiers && specifiers->Type != Specifiers ) { log_failure( "gen::def_function: specifiers was not a `Specifiers` type" ); - return InvalidCode; + return Code::Invalid; } if ( params && params->Type != Parameters ) { log_failure( "gen::def_function: params was not a `Parameters` type" ); - return InvalidCode; + return Code::Invalid; } if ( ret_type == nullptr || ret_type->Type != Typename ) { log_failure( "gen::def_function: ret_type was not a Typename" ); - return InvalidCode; + return Code::Invalid; } switch ( body->Type ) { - case Proc_Body: + case Function_Body: case Untyped: break; default: { log_failure("gen::def_function: body must be either of Function_Body or Untyped type."); - return InvalidCode; + return Code::Invalid; } } - Code + s32 name_length = zpl_strnlen( name, MaxNameLength ); + + Code result = make_code(); - result->Name = string_make( g_allocator, name ); - result->Type = Proc; - - array_init( result->Entries, g_allocator ); + result->Name = code_string( name, name_length ); + result->Type = Function; + + array_init( result->Entries, StaticData::Allocator_CodePool ); result->add( body ); @@ -331,14 +348,14 @@ namespace gen return result; } - Code def_proc_body( s32 num, ... ) + Code def_function_body( s32 num, ... ) { using namespace ECode; if ( num <= 0 ) { log_failure("gen::def_function_body: num cannot zero or neg"); - return InvalidCode; + return Code::Invalid; } Code result = make_code(); @@ -354,12 +371,12 @@ namespace gen if ( ! entry ) { log_failure("gen::def_function_body: Provided an invalid entry!"); - return InvalidCode; + return Code::Invalid; } switch ( entry->Type ) { - case Proc_Forward: + case Function_Forward: case Namespace: case Namespace_Body: case Parameters: @@ -368,7 +385,7 @@ namespace gen case Typename: { log_failure("gen::def_function_body: Entry type is not allowed: %s", entry->type_str() ); - return InvalidCode; + return Code::Invalid; } default: @@ -376,27 +393,27 @@ namespace gen } result->add( entry ); - } + } while ( num--, num > 0 ); va_end(va); - + return result; } - Code def_proc_body( s32 num, Code* codes ) + Code def_function_body( s32 num, Code* codes ) { using namespace ECode; if ( num <= 0 ) { log_failure("gen::def_function_body: num cannot zero or neg"); - return InvalidCode; + return Code::Invalid; } if ( codes == nullptr ) { log_failure("gen::def_function_body: Provided a null array of codes!"); - return InvalidCode; + return Code::Invalid; } Code result = make_code(); @@ -409,12 +426,12 @@ namespace gen if ( ! entry ) { log_failure("gen::def_function_body: Provided an invalid entry!"); - return InvalidCode; + return Code::Invalid; } switch ( entry->Type ) { - case Proc_Forward: + case Function_Forward: case Namespace: case Namespace_Body: case Parameters: @@ -423,7 +440,7 @@ namespace gen case Typename: { log_failure("gen::def_function_body: Entry type is not allowed: %s", entry->type_str() ); - return InvalidCode; + return Code::Invalid; } default: @@ -431,17 +448,83 @@ namespace gen } result->add( entry ); - } + } while ( num--, num > 0 ); - + return result; } + Code def_params( s32 num, ... ) + { + using namespace ECode; + + if (num <= 0) + { + log_failure( "TT::make_paramters: num cannot be zero or neg" ); + return Code::Invalid; + } + + Code + result = make_code(); + result->Type = Parameters; + + va_list va; + va_start(va, num); + + Code type = va_arg(va, Code); + + char const* name = va_arg(va, char const*); + s32 name_length = zpl_strnlen(name, MaxNameLength); + + result->Name = code_string( name, name_length ); + + array_init( result->Entries, g_allocator ); + + if ( type->Type != Typename ) + { + log_failure( "gen::def_parameters: type of param %d is not a Typename", num - num + 1 ); + return Code::Invalid; + } + + result->add( type ); + + while( num -= 2, num && num % 2 == 0 ) + { + type = va_arg(va, Code); + + name = va_arg(va, char const*); + name_length = zpl_strnlen(name, MaxNameLength); + + Code + param = make_code(); + param->Type = Parameters; + param->Name = code_string(name, name_length); + + array_init( param->Entries, StaticData::Allocator_CodePool ); + + if ( type->Type != Typename ) + { + log_failure( "gen::def_parameters: type of param %d is not a Typename", num - num + 1 ); + return Code::Invalid; + } + + param->add( type ); + param.lock(); + + result->add(param); + } + va_end(va); + + result.lock(); + return result; + } + + Code def_namespace( char const* name, Code body ) { using namespace ECode; - Code + Code result = make_code(); result->Type = Namespace; @@ -450,7 +533,7 @@ namespace gen if ( body->Type != Namespace_Body || body->Type != Untyped ) { log_failure("gen::def_namespace: body is not of namespace or untyped type"); - return InvalidCode; + return Code::Invalid; } result->add( body ); @@ -465,7 +548,7 @@ namespace gen if ( num <= 0 ) { log_failure("gen::make_specifier: num cannot be zero or less"); - return InvalidCode; + return Code::Invalid; } Code @@ -477,7 +560,7 @@ namespace gen do { Code entry = va_arg(va, Code); - + switch ( entry->Type ) { case Namespace_Body: @@ -486,14 +569,14 @@ namespace gen case Struct_Body: case Typename: log_failure("gen::def_function_body: Entry type is not allowed: %s", ECode::str(entry->Type) ); - return InvalidCode; + return Code::Invalid; default: break; } result->add( entry ); - } + } while ( num--, num > 0 ); va_end(va); @@ -505,7 +588,7 @@ namespace gen if ( num <= 0 ) fatal("gen::make_specifier: num cannot be zero or less"); - Code + Code result = make_code(); result->Type = ECode::Specifiers; result->Content = string_make( g_allocator, "" ); @@ -542,19 +625,19 @@ namespace gen if ( specifiers && specifiers->Type != Specifiers ) { log_failure( "gen::def_struct: specifiers was not a `Specifiers` type" ); - return InvalidCode; + return Code::Invalid; } if ( parent && parent->Type != Struct ) { log_failure( "gen::def_struct: parent was not a `Struct` type" ); - return InvalidCode; + return Code::Invalid; } if ( body && body->Type != Struct_Body ) { log_failure( "gen::def_struct: body was not a Struct_Body type" ); - return InvalidCode; + return Code::Invalid; } Code @@ -583,7 +666,7 @@ namespace gen if ( num == 0 ) { log_failure("gen::def_struct_body: num cannot be zero"); - return InvalidCode; + return Code::Invalid; } Code result = make_code(); @@ -606,15 +689,15 @@ namespace gen case Typename: { log_failure("gen::def_struct_body: Entry type is not allowed: %s", ECode::str(entry->Type) ); - return InvalidCode; + return Code::Invalid; } } result->add( entry ); - } + } while ( num--, num > 0 ); va_end(va); - + return result; } @@ -623,26 +706,26 @@ namespace gen if ( specifiers && specifiers->Type != ECode::Specifiers ) { log_failure( "gen::def_variable: specifiers was not a `Specifiers` type" ); - return InvalidCode; + return Code::Invalid; } if ( type->Type != ECode::Typename ) { log_failure( "gen::def_variable: type was not a Typename" ); - return InvalidCode; + return Code::Invalid; } if ( value && value->Type != ECode::Untyped ) { log_failure( "gen::def_variable: value was not a `Untyped` type" ); - return InvalidCode; + return Code::Invalid; } - Code + Code result = make_code(); result->Name = string_make( g_allocator, name ); result->Type = ECode::Variable; - + array_init( result->Entries, g_allocator ); if ( specifiers ) @@ -658,7 +741,7 @@ namespace gen Code def_type( char const* name, Code specifiers ) { - Code + Code result = make_code(); result->Name = string_make( g_allocator, name ); result->Type = ECode::Typename; @@ -683,7 +766,7 @@ namespace gen # pragma endregion Upfront Constructors # pragma region Incremetnal Constructors - Code make_proc( char const* name + Code make_function( char const* name , Code specifiers , Code params , Code ret_type @@ -694,26 +777,26 @@ namespace gen if ( specifiers && specifiers->Type != Specifiers ) { log_failure( "gen::def_function: specifiers was not a `Specifiers` type" ); - return InvalidCode; + return Code::Invalid; } if ( params && params->Type != Parameters ) { log_failure( "gen::def_function: params was not a `Parameters` type" ); - return InvalidCode; + return Code::Invalid; } if ( ret_type == nullptr || ret_type->Type != Typename ) { log_failure( "gen::def_function: ret_type was not a Typename" ); - return InvalidCode; + return Code::Invalid; } - Code + Code result = make_code(); result->Name = string_make( g_allocator, name ); - result->Type = Proc; - + result->Type = Function; + array_init( result->Entries, g_allocator ); // Making body at entry 0. @@ -731,20 +814,20 @@ namespace gen return result; } - Code make_struct( char const* name, Code parent = UnusedCode, Code specifiers = UnusedCode ) + Code make_struct( char const* name, Code parent, Code specifiers ) { using namespace ECode; if ( specifiers && specifiers->Type != Specifiers ) { log_failure( "gen::def_struct: specifiers was not a `Specifiers` type" ); - return InvalidCode; + return Code::Invalid; } if ( parent && parent->Type != Struct ) { log_failure( "gen::def_struct: parent was not a `Struct` type" ); - return InvalidCode; + return Code::Invalid; } Code @@ -788,7 +871,7 @@ namespace gen if ( def == nullptr ) { log_failure("gen::parse_proc: def is null!"); - return InvalidCode; + return Code::Invalid; } arena mem; @@ -806,13 +889,13 @@ namespace gen // Making all significant tokens have a max length of 128 for this parser. ct sw LengthID = 128; - struct Param - { - char const Type[LengthID]; - char const Name[LengthID]; + struct Param + { + char const Type[LengthID]; + char const Name[LengthID]; }; - static + static Param Params[ 64 ] { 0 }; // Zero out params before a run of this func. @@ -848,7 +931,7 @@ namespace gen s32 length = 0; char const* spec_or_type = scanner; - + while ( char_is_alpha( * scanner) || * scanner == '_') { length++; @@ -870,7 +953,7 @@ namespace gen ret_length = length; scanner++; break; - } + } while( left ); SkipWhitespace(); @@ -887,13 +970,13 @@ namespace gen // Parameters SkipWhitespace(); - + if ( * scanner != '(' ) { log_failure("gen::parse_proc: Error, expected a ( in %s", string_make_length( g_allocator, scanner - 4, 100) ); - return InvalidCode; + return Code::Invalid; } - + scanner++; do @@ -909,7 +992,7 @@ namespace gen // get end specifiers - + if ( * scanner != '{') @@ -919,16 +1002,16 @@ namespace gen } Code specifiers = def_specifiers( num_specifiers, specs_found ); - + Code params = make_code(); Code ret_type = def_type( ret_type_str ); - Code body = untyped_str( body_str, body_length ); + Code body = untyped_str( body_length, body_str ); - Code + Code result = make_code(); result->Name = string_make( g_allocator, name ); - result->Type = ECode::Proc; - + result->Type = ECode::Function; + array_init( result->Entries, g_allocator ); result->add( body ); @@ -970,7 +1053,7 @@ namespace gen # pragma region Untyped Constructors Code untyped_str(char const* fmt) { - Code + Code result = make_code(); result->Name = string_make( g_allocator, fmt ); result->Type = ECode::Untyped; @@ -981,15 +1064,15 @@ namespace gen Code untyped_fmt(char const* fmt, ...) { - local_persist thread_local + local_persist thread_local char buf[ZPL_PRINTF_MAXLEN] = { 0 }; va_list va; va_start(va, fmt); zpl_snprintf_va(buf, ZPL_PRINTF_MAXLEN, fmt, va); va_end(va); - - Code + + Code result = make_code(); result->Name = string_make( g_allocator, fmt ); result->Type = ECode::Untyped; @@ -1000,7 +1083,7 @@ namespace gen Code untyped_token_fmt( char const* fmt, s32 num_tokens, ... ) { - local_persist thread_local + local_persist thread_local char buf[ZPL_PRINTF_MAXLEN] = { 0 }; va_list va; @@ -1033,13 +1116,13 @@ namespace gen case Global_Body: break; - case Proc: + case Function: break; - case Proc_Body: + case Function_Body: break; - case Proc_Forward: + case Function_Forward: break; case Namespace: @@ -1103,7 +1186,7 @@ namespace gen result = string_append_length( result, Content, string_length(Content) ); break; - case Proc_Forward: + case Function_Forward: { u32 index = 0; u32 left = array_count( Entries ); @@ -1136,7 +1219,7 @@ namespace gen } break; - case Proc: + case Function: { u32 index = 0; u32 left = array_count( Entries ); @@ -1169,7 +1252,7 @@ namespace gen } break; - case Proc_Body: + case Function_Body: fatal("NOT SUPPORTED YET"); break; @@ -1181,7 +1264,7 @@ namespace gen fatal("NOT SUPPORTED YET"); break; - case Parameters: + case Parameters: { result = string_append_fmt( result, "%s %s", Entries[0]->to_string(), Name ); @@ -1191,7 +1274,7 @@ namespace gen while ( left--, left > 0 ) result = string_append_fmt( result, ", %s %s" , Entries[index]->Entries[0]->to_string() - , Entries[index]->Name + , Entries[index]->Name ); } break; @@ -1227,9 +1310,12 @@ namespace gen return result; } + + const Code Code::Invalid; # pragma endregion AST + # pragma region Builder void Builder::print( Code code ) { @@ -1247,7 +1333,7 @@ namespace gen } Buffer = string_make( g_allocator, "" ); - + return true; } diff --git a/project/gen.hpp b/project/gen.hpp index 3df7c32..ad499ce 100644 --- a/project/gen.hpp +++ b/project/gen.hpp @@ -1,18 +1,21 @@ /* gencpp: A simple staged metaprogramming library for C++. - + + The library is mostly a compositon of code element constructors. + These build up a code AST to then serialize with a file builder. + This library is intended for small-to midsize projects. - AST type checking supports only a small subset of c++. + AST type checking supports only a small subset of c++. See the 'ECode' namespace and 'gen API' region to see what is supported. ### *WHAT IS NOT PROVIDED* - * Macro or template generation : This library is to avoid those, adding support for them adds unnecessary complexity. - If you desire define them outside the gen_time scopes. - * Expression validation : Execution expressions are defined using the untyped string API. + * Macro or template generation : This library is to avoid those, adding support for them adds unnecessary complexity. + If you desire define them outside the gen_time scopes. + * Expression validation : Execution expressions are defined using the untyped string API. There is no parse API for validating expression (possibly will add in the future) - * Complete file parser DSL : This isn't like the unreal header tool. + * Complete file parser DSL : This isn't like the unreal header tool. Code injection to file or based off a file contents is not supported by the api. However nothing is stopping you using the library for that purpose. * Modern c++ (STL library) features @@ -21,17 +24,17 @@ Notes: * The allocator definitions used are exposed to the user incase they want to dictate memory usage - * ASTs are wrapped for the user in a Code struct which essentially a warpper for a AST* type. + * ASTs are wrapped for the user in a Code struct which essentially a warpper for a AST* type. * Both AST and Code have member symbols but their data layout is enforced to be POD types. Data layout of AST struct: - CodeT Type; + CodeT Type; bool Readonly; - AST* Parent; - string Name; - string Comment; - union { + AST* Parent; + string Name; + string Comment; + union { array(AST*) Entries; string Content; }; @@ -39,7 +42,7 @@ *`CodeT` is a typedef for `ECode::Type` which is the type of the enum.* ASTs can be set to readonly by calling Code's lock() member function. - Adding comments is always available even if the AST is set to readonly. + Adding comments is always available even if the AST is set to readonly. ### There are four sets of interfaces for Code AST generation the library provides @@ -57,25 +60,21 @@ * def_class * def_class_body - * def_class_fwd * def_enum * def_enum_class * def_enum_body + * def_function + * def_function_body * def_global_body * def_namespace * def_namespace_body * def_operator - * def_operator_fwd * def_param * def_params - * def_proc - * def_proc_body - * def_proc_fwd * def_specifier * def_specifiers * def_struct * def_struct_body - * def_struct_fwd * def_variable * def_type * def_typedef @@ -97,17 +96,13 @@ * make_class * make_enum * make_enum_class + * make_function * make_global_body * make_namespace * make_operator * make_params - * make_proc * make_specifiers * make_struct - * make_variable - * make_type - * make_typedef - * make_using ### Parse construction @@ -119,13 +114,13 @@ * parse_classes * parse_enum * parse_enums + * parse_function + * parse_functions * parse_global_body * parse_namespace * parse_namespaces * parse_operator * parse_operators - * parse_proc - * parse_procs * parse_struct * parse_strucs * parse_variable @@ -139,7 +134,7 @@ The parse API treats any execution scope definitions with no validation and are turned into untyped Code ASTs. This includes the assignmetn of variables; due to the library not yet supporting c/c++ expression parsing. - The plural variants provide an array of codes, its up to the user to add them to a body AST + The plural variants provide an array of codes, its up to the user to add them to a body AST (they are not auto-added to a body) ### Untyped constructions @@ -152,7 +147,7 @@ * untyped_fmt * untyped_token_fmt - During serialization any untyped Code AST is has its string value directly injected inline of + During serialization any untyped Code AST is has its string value directly injected inline of whatever context the content existed as an entry within. Even though thse are not validated from somewhat correct c/c++ syntax or components, it doesn't mean that Untyped code can be added as any component of a Code AST: @@ -169,53 +164,56 @@ // Temporarily here for debugging purposes. #define gen_time + + +#define GEN_BAN_CPP_TEMPLATES #define GEN_ENFORCE_READONLY_AST -// #define GEN_DEFINE_DSL +#define GEN_DEFINE_DSL #define GEN_DEFINE_LIBRARY_CODE_CONSTANTS -// #define GEN_BAN_CPP_TEMPLATES #define GEN_USE_FATAL + #ifdef gen_time namespace gen { using LogFailType = sw(*)(char const*, ...); -#ifdef GEN_BAN_CPP_TEMPLATES +# ifdef GEN_BAN_CPP_TEMPLATES #define template static_assert("Templates are banned within gen_time scope blocks") -#endif +# endif -#ifdef GEN_USE_FATAL +# ifdef GEN_USE_FATAL ct LogFailType log_failure = fatal; -#else +# else ct LogFailType log_failure = log_fmt; -#endif +# endif namespace ECode { enum Type : u8 { - Invalid, - Untyped, + Invalid, + Untyped, Access_Public, Access_Private, Access_Protected, - Class, - Enum, - Enum_Body, - Global_Body, - Namespace, - Namespace_Body, - Parameters, - Proc, - Proc_Body, - Proc_Forward, - Specifiers, - Struct, - Struct_Body, - Variable, - Typedef, - Typename, - Using, + Class, + Enum, + Enum_Body, + Global_Body, + Namespace, + Namespace_Body, + Parameters, + Function, + Function_Body, + Function_Forward, + Specifiers, + Struct, + Struct_Body, + Variable, + Typedef, + Typename, + Using, Num_Types }; @@ -223,7 +221,7 @@ namespace gen local_persist char const* str( Type type ) { - static + static char const* lookup[Num_Types] = { "Invalid", "Untyped", @@ -267,7 +265,7 @@ namespace gen Num_Ops }; - inline + inline char const* str( Type op ) { local_persist @@ -287,23 +285,23 @@ namespace gen { enum Type : u8 { - Attribute, - Alignas, - Constexpr, - Const, - Inline, - Pointer, - Reference, - RValue, + Attribute, + Alignas, + Constexpr, + Const, + Inline, + Pointer, + Reference, + RValue, - C_Linkage, - API_Import, - API_Export, - External_Linkage, - Internal_Linkage, - Static_Member, - Local_Persist, - Thread_Local, + C_Linkage, + API_Import, + API_Export, + External_Linkage, + Internal_Linkage, + Static_Member, + Local_Persist, + Thread_Local, Invalid, Num_Specifiers, @@ -325,13 +323,13 @@ namespace gen "extern \"C\"", - #if defined(ZPL_SYSTEM_WINDOWS) +# if defined(ZPL_SYSTEM_WINDOWS) "__declspec(dllexport)", "__declspec(dllimport)", - #elif defined(ZPL_SYSTEM_MACOS) +# elif defined(ZPL_SYSTEM_MACOS) "__attribute__ ((visibility (\"default\")))", "__attribute__ ((visibility (\"default\")))", - #endif +# endif "extern", "static", @@ -342,7 +340,7 @@ namespace gen return lookup[ specifier ]; } - + Type to_type( char const* str, s32 length ) { local_persist @@ -371,7 +369,7 @@ namespace gen #pragma region Data Structures // TODO: If perf needs it, convert layout an SOA format. - /* + /* Simple AST POD with functionality to seralize into C++ syntax. ASTs are currently stored as an AOS. They are always reconstructed on demand. @@ -467,7 +465,7 @@ namespace gen /* AST* typedef as to not constantly have to add the '*' as this is written often.. - If GEN_ENFORCE_READONLY_AST is defined, readonly assertions will be done on any member dreference, + If GEN_ENFORCE_READONLY_AST is defined, readonly assertions will be done on any member dreference, and the 'gen API' related functions. will set their created ASTs to readonly before returning. Casting to AST* will bypass. @@ -481,22 +479,22 @@ namespace gen if ( ast->Type == ECode::Invalid ) { log_failure("Code::body: Type is invalid, cannot get"); - return InvalidCode; + return Invalid; } if ( ast->Entries == nullptr || array_count(ast->Entries) == 0 ) { log_failure("Code::body: Entries of ast not properly setup."); - return InvalidCode; + return Invalid; } - #ifdef GEN_ENFORCE_READONLY_AST +# ifdef GEN_ENFORCE_READONLY_AST if ( ast->Readonly ) { - log_failure("Attempted to a body AST from a readonly AST!"); - return InvalidCode; + log_failure("Attempted to a body AST from a readonly AST!"); + return Invalid; } - #endif +# endif return * (Code*)( ast->body() ); } @@ -522,7 +520,7 @@ namespace gen { return ast; } - + Code& operator =( Code other ) { if ( ast == nullptr ) @@ -531,10 +529,10 @@ namespace gen return *this; } - #ifdef GEN_ENFORCE_READONLY_AST + #ifdef GEN_ENFORCE_READONLY_AST if ( ast->Readonly ) { - log_failure("Attempted to set a readonly AST!"); + log_failure("Attempted to set a readonly AST!"); return *this; } #endif @@ -545,7 +543,7 @@ namespace gen } forceinline - AST* operator ->() + AST* operator ->() { if ( ast == nullptr ) { @@ -553,24 +551,25 @@ namespace gen return nullptr; } - #ifdef GEN_ENFORCE_READONLY_AST +# ifdef GEN_ENFORCE_READONLY_AST if ( ast->Readonly ) { - log_failure("Attempted to access a member from a readonly AST!"); + log_failure("Attempted to access a member from a readonly AST!"); return nullptr; } - #endif +# endif return ast; } + + // Used internally for the most part to identify invaidly generated code. + static const Code Invalid; }; static_assert( sizeof(Code) == sizeof(AST*), "ERROR: Code is not POD" ); // Used when the its desired when omission is allowed in a definition. - ct Code UnusedCode = { nullptr }; - - // Used internally for the most part to identify invaidly generated code. - extern const Code InvalidCode; + ct Code NoCode = { nullptr }; + // extern const Code InvalidCode; /* */ @@ -590,10 +589,10 @@ namespace gen /* Initialize the library. This currently just initializes the CodePool. - */ + */ void init(); - // Use this only if you know you generated the code you needed to a file + // Use this only if you know you generated the code you needed to a file. // And rather get rid of current code asts instead of growing the pool memory. void clear_code_pool(); @@ -610,186 +609,100 @@ namespace gen void set_allocator_type_table ( allocator type_reg_allocator ); # pragma region Upfront - Code def_class ( char const* name, Code body, Code parent = UnusedCode, Code specifiers = UnusedCode ); - Code def_class_body( s32 num, ... ); - Code def_class_fwd ( char const* name ); + Code def_class ( char const* name, Code parent = NoCode, Code specifiers = NoCode, Code body = NoCode ); + Code def_class ( s32 length, char const* name, Code parent = NoCode, Code specifiers = NoCode, Code body = NoCode ); + Code def_enum ( char const* name, Code type = NoCode, Code body = NoCode); + Code def_enum ( s32 length, char const* name, Code type = NoCode, Code body = NoCode ); + Code def_function ( char const* name, Code params = NoCode, Code ret_type = NoCode, Code Specifiers = NoCode, Code body = NoCode ); + Code def_function ( s32 length, char const* name, Code params = NoCode, Code ret_type = NoCode, Code Specifiers = NoCode, Code body = NoCode ); + Code def_namespace ( char const* name, Code body ); + Code def_namespace ( s32 length, char const* name, Code body ); + Code def_operator ( OperatorT op, Code params = NoCode, Code ret_type = NoCode, Code Specifiers = NoCode, Code body = NoCode ); - Code def_enum( char const* name, Code type, Code body ); + Code def_param ( Code type, char const* name ); + Code def_param ( Code type, s32 length, char const* name ); - Code def_global_body( s32 num, ... ); + Code def_specifier ( SpecifierT* specifier ); - Code def_namespace ( char const* name, Code body ); - Code def_namespace_body( s32 num, ... ); + Code def_struct ( char const* name, Code parent = NoCode, Code specifiers = NoCode, Code body = NoCode ); + Code def_struct ( s32 length, char const* name, Code parent = NoCode, Code specifiers = NoCode, Code body = NoCode ); + Code def_type ( char const* name, Code specifiers = NoCode ); + Code def_type ( s32 length, char const* name, Code specifiers = NoCode ); + Code def_using ( char const* name, Code type ); + Code def_using ( s32 length, char const* name, Code type ); + Code def_using_namespace( char const* name ); + Code def_using_namespace( s32 length, char const* name ); - Code def_operator( OperatorT op - , Code specifiers - , Code params - , Code ret_type - , Code body - ); - Code def_operator_fwd( OperatorT op, Code specifiers, Code params, Code ret_type ); + Code def_variable ( Code type, char const* name, Code value = NoCode, Code specifiers = NoCode ); + Code def_variable ( Code type, s32 length, char const* name, Code value = NoCode, Code specifiers = NoCode ); - Code def_param( Code type, char const* name ); - Code def_params( s32 num, ... ); - Code def_params( s32 num, Code* params ); - - Code def_proc( char const* name - , Code specifiers - , Code params - , Code ret_type - , Code body - ); - - Code def_proc_body( s32 num, ... ); - Code def_proc_body( s32 num, Code* codes ); - - Code def_proc_fwd( char const* name - , Code specifiers - , Code params - , Code ret_type - ); - - Code def_specifier( SpecifierT* specifier ); - Code def_specifiers( s32 num , ... ); - Code def_specifiers( s32 num, SpecifierT* specs ); - - Code def_struct( char const* name, Code body, Code parent = UnusedCode, Code specifiers = UnusedCode ); - - Code def_struct_body( s32 num, ... ); - Code def_struct_body( s32 num, Code* codes ); - - Code def_sturct_fwd(); - - Code def_variable( Code type, char const* name, Code value = UnusedCode, Code specifiers = UnusedCode ); - - Code def_type( char const* name, Code specifiers = UnusedCode ); - - Code def_using ( char const* name, Code type ); - Code def_using_namespace( char const* name ); + Code def_class_body ( s32 num, ... ); + Code def_enum_body ( u32 num, ... ); + Code def_enum_body ( u32 num, Code* Values ); + Code def_global_body ( s32 num, ... ); + Code def_function_body ( s32 num, ... ); + Code def_function_body ( s32 num, Code* codes ); + Code def_namespace_body ( s32 num, ... ); + Code def_operator_body ( s32 num, ... ); + Code def_params ( s32 num, ... ); + Code def_params ( s32 num, Code* params ); + Code def_specifiers ( s32 num , ... ); + Code def_specifiers ( s32 num, SpecifierT* specs ); + Code def_struct_body ( s32 num, ... ); + Code def_struct_body ( s32 num, Code* codes ); # pragma endregion Upfront # pragma region Incremental - Code make_class( char const* name, Code parent = UnusedCode, Code specifiers = UnusedCode ); + Code make_class ( char const* name, Code parent = NoCode, Code specifiers = NoCode ); + Code make_class ( s32 length, char const* name, Code parent = NoCode, Code specifiers = NoCode ); + Code make_enum ( s32 length, char const* name, Code type = NoCode, Code body = NoCode ); + Code make_enum ( char const* name, Code type = NoCode, Code body = NoCode ); + Code make_enum_class ( s32 length, char const* name, Code type = NoCode, Code body = NoCode ); + Code make_function ( s32 length, char const* name, Code params = NoCode, Code ret_type = NoCode, Code specifiers = NoCode ); + Code make_function ( char const* name, Code params = NoCode, Code ret_type = NoCode, Code specifiers = NoCode ); + Code make_global_body ( char const* name = "", s32 num = 0, ... ); + Code make_global_body ( s32 length, char const* name = "", s32 num = 0, ... ); + Code make_namespace ( char const* name ); + Code make_namespace ( s32 length, char const* name ); + Code make_operator ( OperatorT op, Code params = NoCode, Code ret_type = NoCode, Code specifiers = NoCode ); + Code make_struct ( char const* name, Code parent = NoCode, Code specifiers = NoCode ); + Code make_struct ( s32 length, char const* name, Code parent = NoCode, Code specifiers = NoCode ); - Code make_enum ( char const* name, Code type = UnusedCode, Code body = UnusedCode ); - Code make_enum_class( char const* name, Code type = UnusedCode, Code body = UnusedCode ); - - Code make_global_body( char const* name = "", s32 num = 0, ... ); - - Code make_namespace( char const* name ); - - Code make_operator( OperatorT op - , Code specifiers = UnusedCode - , Code params = UnusedCode - , Code ret_type = UnusedCode - , Code body = UnusedCode - ); - - Code make_params( s32 num, ... ); - - Code make_proc( char const* name - , Code specifiers = UnusedCode - , Code params = UnusedCode - , Code ret_type = UnusedCode - ); - - Code make_specifiers( s32 num , ... ); - Code make_specifiers( s32 num, SpecifierT* specs ); - - Code make_struct( char const* name, Code parent = UnusedCode, Code specifiers = UnusedCode ); - - Code make_variable( char const* name, Code type = UnusedCode, Code value = UnusedCode, Code specifiers = UnusedCode ); - - Code make_type( char const* name, Code specifiers = UnusedCode ); - - Code make_using( char const* name, Code type = UnusedCode, Code specifiers = UnusedCode ); + Code make_params ( s32 num, ... ); + Code make_params_macro( s32 num, ... ); + Code make_params ( s32 num, Code* params ); + Code make_specifiers ( s32 num, ... ); + Code make_specifiers ( s32 num, SpecifierT* specs ); # pragma endregion Incremental # pragma region Parsing - Code parse_class( char const* class_def, s32 length ); - s32 parse_classes( char const* class_defs, s32 length, Code* out_classes ); + Code parse_class ( s32 length, char const* class_def ); + Code parse_enum ( s32 length, char const* enum_def ); + Code parse_function ( s32 length, char const* fn_def ); + Code parse_global_body( s32 length, char const* body_def ); + Code parse_operator ( s32 length, char const* operator_def ); + Code parse_struct ( s32 length, char const* struct_def ); + Code parse_variable ( s32 length, char const* var_def ); + Code parse_type ( s32 length, char const* type_def ); + Code parse_typedef ( s32 length, char const* typedef_def ); + Code parse_using ( s32 length, char const* using_def ); - Code parse_enum( char const* enum_def, s32 length); - s32 parse_enums( char const* enum_defs, s32 length, Code* out_enums ); - - Code parse_global_body( char const* body_def, s32 length ); - - Code parse_operator( char const* operator_def, s32 length ); - - Code parse_proc( char const* proc_def, s32 length ); - s32 parse_procs( char const* proc_defs, s32 length, Code* out_procs ); - - Code parse_struct( char const* struct_def, s32 length ); - s32 parse_structs( char const* struct_defs, s32 length, Code* out_struct_codes ); - - Code parse_variable( char const* var_def, s32 length ); - s32 parse_variables( char const* vars_def, s32 length, Code* out_var_codes ); - - Code parse_type( char const* type_def, s32 length ); - - Code parse_typedef( char const* typedef_def, s32 length ); - s32 parse_typedef( char const* typedef_def, s32 length, Code* out_typedef_codes ); - - Code parse_using ( char const* using_def, s32 length ); - s32 parse_usings( char const* usings_def, s32 length, Code* out_using_codes ); + s32 parse_classes ( s32 length, char const* class_defs, Code* out_classes ); + s32 parse_enums ( s32 length, char const* enum_defs, Code* out_enums ); + s32 parse_functions( s32 length, char const* fn_defs, Code* out_fn_codes ); + s32 parse_structs ( s32 length, char const* struct_defs, Code* out_struct_codes ); + s32 parse_variables( s32 length, char const* vars_def, Code* out_var_codes ); + s32 parse_typedef ( s32 length, char const* typedef_def, Code* out_typedef_codes ); + s32 parse_usings ( s32 length, char const* usings_def, Code* out_using_codes ); # pragma endregion Parsing # pragma region Untyped text - /* - Define an untyped code string. - - Untyped code may be used in bodies of functions, namespaces, or structs - or the in places where expressions may be placed. - - Because the code within it is untyped, errors will naturally not be provided. - Consider this an a preprocessor define. - */ - Code untyped_str( char const* str ); - Code untyped_str( char const* str, s32 length); - - /* - Define an untyped code string using traditional 'printf'. - - Untyped code may be used in bodies of functions, namespaces, or structs - or the in places where expressions may be placed. - - Because the code within it is untyped, errors will naturally not be provided. - Consider this an a preprocessor define. - */ - Code untyped_fmt( char const* fmt, ... ); - - /* - Define an untyped code string using token formatting: - ... { } ... Will be repalced with value of token ID. - - Values are to provided as: , , ... - - num_tokens : The number of ID-Value pairs provided. - - Untyped code may be used in bodies of functions, namespaces, or structs - or the in places where expressions may be placed. - - Because the code within it is untyped, errors will naturally not be provided. - Consider this an a preprocessor define. - */ - Code untyped_token_fmt( char const* fmt, s32 num_tokens, ... ); + Code untyped_str ( char const* str ); + Code untyped_str ( s32 length, char const* str); + Code untyped_fmt ( char const* fmt, ... ); + Code untyped_token_fmt( char const* fmt, s32 num_tokens, ... ); # pragma endregion Untyped text - /* - Used to generate the files. - This is inspired by jai's usage of the string_builder with #insert. - - Its expected when using this library that Code ast will be serialized with the: - Builder::print() proc - - The seralized content of the Code ast will be appended to Buffer within an empty line - prepared for a another definition or to add an empty newline to the end of the file. - - Builder::write() should be called when all Code has been seralized for that file. - - The #insert directive is thus represented by an #include of the generated file at your desired line - of any file in the target project. - */ struct Builder { zpl_file File; @@ -805,7 +718,9 @@ namespace gen #pragma region MACROS # define gen_main main -# define __ UnusedCode + +# define __ NoCode +# define spec_alignas( Value_ ) ESpecifier::Alignas, Value /* gen's Domain Specific Langauge. @@ -827,45 +742,99 @@ namespace gen The code macros are used to embed c/c++ to insert into the desired lcoation. */ #ifdef GEN_DEFINE_DSL -# define untyped_code( Name_, Value_ ) Code Name_ = gen::untyped_str( txt(Value_) ) -# define typename( Name_, Value_ ) Code t_##Name_ = gen::def_type( txt(Value_) ) -# define typename_fmt( Name_, Fmt_, ... ) Code t_##Name_ = gen::def_type( bprintf( Fmt_, __VA_ARGS__ ) ) -# define using_type( Name_, Type_ ) Code Name_ = gen::def_using( #Name_, t_##Type_ ) -# define variable( Type_, Name_, ... ) Code Name_ = gen::def_variable( t_##Type_, #Name_, __VA_ARGS__ ) -# define untyped( Value_ ) gen::untyped_str( txt_with_length(Value_) ) -# define code_token( Fmt_, ... ) gen::untyped_token_fmt( Fmt_, VA_NARGS( __VA_ARGS__), __VA_ARGS__ ) -# define code_fmt( Fmt_, ... ) gen::untyped_fmt( Fmt_, __VA_ARGS__ ) -# define specifiers( ... ) gen::def_specifiers( VA_NARGS( __VA_ARGS__ ), __VA_ARGS__ ) -# define type( Value_ ) gen::def_type( txt(Value_) ) -# define type_fmt( Fmt_, ... ) gen::def_type( bprintf( Fmt_, __VA_ARGS__ ) ) -# define using( Name_, Type_ ) gen::def_using( #Name_, Type_ ) -# define var( Type_, Name_, ... ) gen::def_variable( Type_, #Name_, __VA_ARGS__ ) +// Boilerplate -# define make( Type_, Name_, ... ) Code Name_ = make_##Type_( #Name_, __VA_ARGS__ ); -# define enum( Name_, Type_, Body_ ) gen::def_enum( #Name_, t_##Type_, Body_ ) -# define proc( Name_, Specifiers_, RetType_, Parameters_, Body_ ) Name_ = gen::def_proc( #Name_, Specifiers_, Parameters_, RetType_, Body_ ) -# define proc_body( ... ) gen::def_proc_body( VA_NARGS( __VA_ARS__ ), __VA_ARGS__ ) -# define params( ... ) gen::def_params( VA_NARGS( __VA_ARGS__ ) / 2, __VA_ARGS__ ) -# define struct( Name_, Parent_, Specifiers_, Body_ ) Name_ = gen::def_struct( #Name_, Body_, Parent_, Specifiers_ ) -# define struct_body( ... ) gen::def_struct_body( VA_NARGS( __VA_ARGS__ ), __VA_ARGS__ ) +/* + In order to provide some convient syntax sugar this polymoprhic macro boilerplate is needed for: + * function( ... ) + * operator( ... ) + * params( ... ) -# define add_var( Value_ ) add( gen::parse_variable( txt_with_length(Value_)) ) -# define add_untyped( Value_ ) add( gen::untyped_str( txt_with_length( Value_ ) ) ) -# define add_type( Value_ ) add( gen::parse_type( txt_with_length(Value_)) ) -# define add_params( Value_ ) add( gen::parse_params( txt_with_length(Value_) )) + macrofn based off of: https://stackoverflow.com/questions/3046889/optional-parameters-with-c-macros + specifically: https://stackoverflow.com/a/56038661 +*/ -# define enum_code( Def_ ) gen::parse_enum( txt( Def_ ), sizeof( txt( Def_ )) ) -# define global_code( Def_ ) gen::parse_global_body( txt_with_length( Def_ )) -# define namespace_code( Def_ ) gen::parse_namespace( txt(Def_), sizeof( txt(Def_)) ) -# define proc_code( Def_ ) gen::parse_proc( txt( Def_ ), sizeof( txt( Def_ )) ) -# define struct_code( Def_ ) gen::parse_struct( txt( Def_ ), sizeof( txt( Def_ )) ) -# define variable_code( Def_ ) gen::parse_variable( txt_with_length( Def_ ) ) +#define macrofn_chooser(_f0, _f1, _f2, _f3, _f4, _f5, _f6, _f7, _f8, _f9, _f10, _f11, _f12, _f13, _f14, _f15, _f16, ...) _f16 +#define macrofn_recomposer(ArgsWithParentheses_) macrofn_chooser ArgsWithParentheses_ +#define macrofn_chose_from_arg_num(F, ...) macrofn_recomposer((__VA_ARGS__, F##_16, F##_15, F##_14, F##_13, F##_12, F##_11, F##_10, F##_9, F##_8, F##_7, F##_6, F##_5, F##_4, F##_3, F##_2, F##_1, )) +#define marcofn_no_arg_expander(Func) ,,,,,,,,,,,,,,,,Func_ ## _0 +#define macrofn_finder(Func_, ...) macrofn_chose_from_arg_num(Func_, NO_ARG_EXPANDER __VA_ARGS__ (Func_)) +#define macrofn_polymorphic(Func_, ...) macrofn_finder(Func_, __VA_ARGS__)(__VA_ARGS__) + +#define function_5( Name_, Params_, RetType_, Specifiers_, Body_ ) gen::def_function( txt_n_len( Name_ ), macro_expand( Params_ ), t_##RetType_, Specifiers_, Body_ ) +#define function_4( Name_, Params_, RetType_, Specifiers_ ) gen::def_function( txt_n_len( Name_ ), macro_expand( Params_ ), t_##RetType_, Specifiers_ ) +#define function_3( Name_, Params_, RetType_ ) gen::def_function( txt_n_len( Name_ ), macro_expand( Params_ ), t_##RetType_ ) +#define function_2( Name_, Params_ ) gen::def_function( txt_n_len( Name_ ), macro_expand( Params_ ) ) +#define function_1( Name_ ) gen::def_function( txt_n_len( Name_ ) ) + +#define params_12( T_1, V_1, T_2, V_2, T_3, V_3, T_4, V_4, T_5, V_5, T_6, V_6 ) gen::def_params( 6, t_##T_1, txt_n_len( V_1), t_##T_2, txt_n_len( V_2), t_##T_3, txt_n_len( V_3), t_##T_4, txt_n_len( V_4), t_##T_5, txt_n_len( V_5)) +#define params_10( T_1, V_1, T_2, V_2, T_3, V_3, T_4, V_4, T_5, V_5 ) gen::def_params( 5, t_##T_1, txt_n_len( V_1), t_##T_2, txt_n_len( V_2), t_##T_3, txt_n_len( V_3), t_##T_4, txt_n_len( V_4), t_##T_5, txt_n_len( V_5)) +#define params_8( T_1, V_1, T_2, V_2, T_3, V_3, T_4, V_4 ) gen::def_params( 4, t_##T_1, txt_n_len( V_1), t_##T_2, txt_n_len( V_2), t_##T_3, txt_n_len( V_3), t_##T_4, txt_n_len( V_4) ) +#define params_6( T_1, V_1, T_2, V_2, T_3, V_3 ) gen::def_params( 3, t_##T_1, txt_n_len( V_1), t_##T_2, txt_n_len( V_2), t_##T_3, txt_n_len( V_3)) +#define params_4( T_1, V_1, T_2, V_2 ) gen::def_params( 2, t_##T_1, txt_n_len( V_1), t_##T_2, txt_n_len( V_2)) +#define params_2( T_1, V_1 ) gen::def_params( 1, t_##T_1, txt_n_len( V_1)) +#define params_bad static_assert("params(...): Invalid number of parameters provided.") +#define params_11 params_bad +#define params_9 params_bad +#define params_7 params_bad +#define params_5 params_bad +#define params_3 params_bad +#define params_1 params_bad + +// Upfront + +# define class( Name_, ... ) gen::def_class( txt_n_len(Name_), __VA_ARGS__ ) +# define enum( Name_, Type_, Body_ ) gen::def_enum ( txt_n_len(Name_), t_##Type_, Body_ ) + +# define function( ... ) macrofn_polymorphic( function, __VA_ARGS__ ) +# define namespace( Name_, Body_ ) gen::def_namespace ( txt_n_len(Name_), Body_ ) +# define operator( Op_, ... ) macrofn_polymorphic( operator, __VA_ARGS__ ) +# define params( ... ) macrofn_polymorphic( params, __VA_ARGS__ ) +# define specifiers( ... ) gen::def_specifiers ( macro_num_args( __VA_ARGS__ ), __VA_ARGS__ ) +# define struct( Name_, ... ) gen::def_struct ( txt_n_len(Name_), ## __VA_ARGS__ ) +# define variable( Type_, Name_, ... ) gen::def_variable ( t_##Type_, txt_n_len(Name_), __VA_ARGS__ ) +# define type( Value_, ... ) gen::def_type ( txt_n_len(Value_), __VA_ARGS__ ) +# define type_fmt( Fmt_, ... ) gen::def_type ( bprintf( Fmt_, __VA_ARGS__ ) ) +# define using( Name_, Type_ ) gen::def_using ( txt_n_len(Name_), t_##Type_ ) +# define using_namespace( Name_ ) gen::def_using_namespace( txt_n_len(Name_) ) + +# define class_body( ... ) gen::def_class_body ( macro_num_args( __VA_ARGS__ ), __VA_ARGS__ ) +# define enum_body( ... ) gen::def_enum_body ( macro_num_args( __VA_ARGS__ ), __VA_ARGS__ ) +# define global_body( ... ) gen::def_global_body ( macro_num_args( __VA_ARGS__ ), __VA_ARGS__ ) +# define function_body( ... ) gen::def_function_body ( macro_num_args( __VA_ARGS__ ), __VA_ARGS__ ) +# define namespace_body( ... ) gen::def_namespace_body ( macro_num_args( __VA_ARGS__ ), __VA_ARGS__ ) +# define operator_body( ... ) gen::def_operator_body ( macro_num_args( __VA_ARGS__ ), __VA_ARGS__ ) +# define struct_body( ... ) gen::def_struct_body ( macro_num_args( __VA_ARGS__ ), __VA_ARGS__ ) + +// Incremental + +# define make( ConstructType_, Name_, ... ) Code Name_ = make_##ConstructType_( #Name_, ## __VA_ARGS__ ); + +// Parsing + +# define class_code( ... ) gen::parse_class ( txt_n_len( __VA_ARGS__ )) +# define enum_code( ... ) gen::parse_enum ( txt_n_len( __VA_ARGS__ )) +# define function_code( ... ) gen::parse_function ( txt_n_len( __VA_ARGS__ )) +# define global_body_code( ... ) gen::parse_global_body( txt_n_len( __VA_ARGS__ )) +# define operator_code( ... ) gen::parse_operator ( txt_n_len( __VA_ARGS__ )) +# define namespace_code( ... ) gen::parse_namespace ( txt_n_len( __VA_ARGS__ )) +# define struct_code( ... ) gen::parse_struct ( txt_n_len( __VA_ARGS__ )) +# define variable_code( ... ) gen::parse_variable ( txt_n_len( __VA_ARGS__ )) +# define type_code( ... ) gen::parse_type ( txt_n_len( __VA_ARGS__ )) +# define typedef_code( ... ) gen::parse_typedef ( txt_n_len( __VA_ARGS__ )) +# define using_code( ... ) gen::parse_code ( txt_n_len( __VA_ARGS__ )) + +// Untyped + +# define code_str( ... ) gen::untyped_str ( txt_n_len(__VA_ARGS__) ) +# define code_fmt( Fmt_, ... ) gen::untyped_fmt ( Fmt_, __VA_ARGS__ ) +# define code_token( Fmt_, ... ) gen::untyped_token_fmt( Fmt_, macro_num_args( __VA_ARGS__), __VA_ARGS__ ) #endif #pragma endregion MACROS #pragma region CONSTANTS -#ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS +#ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS namespace gen { // Predefined typename codes. @@ -895,12 +864,18 @@ namespace gen extern Code t_f64; extern Code spec_constexpr; + extern Code spec_const; extern Code spec_inline; + extern Code spec_ptr; + extern Code spec_ref; } #endif namespace gen { + ct s32 MaxNameLength = 128; + ct s32 StringTable_MaxHashLength = kilobytes(1); + extern Code access_public; extern Code access_protected; extern Code access_private; diff --git a/project/gen.singleheader.c99.cpp b/project/gen.singleheader.c99.cpp index 8a5a85e..5321c10 100644 --- a/project/gen.singleheader.c99.cpp +++ b/project/gen.singleheader.c99.cpp @@ -189,7 +189,7 @@ Code make_log_failure() R"(#ifdef GEN_USE_FATAL)" )); - result->add( proc_code( + result->add( function_code( inline sw genc_log_failure(char const *fmt, ...) { @@ -211,32 +211,31 @@ Code make_log_failure() R"(#else)" )); - result->add( parse_proc( -R"(inline -sw genc_log_failure(char const *fmt, ...) -{ - local_persist thread_local - char buf[ZPL_PRINTF_MAXLEN] = { 0 }; + result->add( proc_code( + inline + sw genc_log_failure(char const *fmt, ...) + { + local_persist thread_local + char buf[ZPL_PRINTF_MAXLEN] = { 0 }; - va_list va; + va_list va; -#if Build_Debug - va_start(va, fmt); - zpl_snprintf_va(buf, ZPL_PRINTF_MAXLEN, fmt, va); - va_end(va); + #if Build_Debug + va_start(va, fmt); + zpl_snprintf_va(buf, ZPL_PRINTF_MAXLEN, fmt, va); + va_end(va); - assert_crash(buf); - return -1; -#else - va_start(va, fmt); - zpl_printf_err_va( fmt, va); - va_end(va); + assert_crash(buf); + return -1; + #else + va_start(va, fmt); + zpl_printf_err_va( fmt, va); + va_end(va); - zpl_exit(1); - return -1; -#endif -})" - , 372 + zpl_exit(1); + return -1; + #endif + } )); result->add( untyped_str( @@ -253,32 +252,31 @@ Code make_ECode() { Code ECode = make_global_body(); { - ECode->add( parse_enum( -R"(enum genc_ECode -{ - Invalid, - Untyped, - Enum, - Enum_Body, - Global_Body, - Parameters, - Proc, - Proc_Body, - Proc_Forward, - Specifiers, - Struct, - Struct_Body, - Variable, - Typedef, - Typename, + ECode->add( enum_code( + enum genc_ECode + { + Invalid, + Untyped, + Enum, + Enum_Body, + Global_Body, + Parameters, + Proc, + Proc_Body, + Proc_Forward, + Specifiers, + Struct, + Struct_Body, + Variable, + Typedef, + Typename, - Num_Types -}; -typedef u32 genc_CodeT;)" + 1 - , 280 + Num_Types + }; + typedef u32 genc_CodeT; )); - ECode->add( proc_code( + ECode->add( function_code( inline char const* genc_ecode_to_str( Type type ) { @@ -353,11 +351,11 @@ Code make_Code() typedef genc_AST* Code; )); - code->add( proc_code( + code->add( function_code( bool genc_ast_add( genc_Code other ); )); - code->add( proc_code( + code->add( function_code( genc_forceinline void genc_ast_add_entry( genc_Code self, genc_Code other ) { @@ -371,7 +369,7 @@ Code make_Code() R"(#define genc_code_body( AST_ ) AST->Entries[0])", 47 )); - code->add( proc_code( + code->add( function_code( genc_forceinline bool genc_code_has_entries( Code self ) const { @@ -406,7 +404,7 @@ Code make_static_Data() Code data = make_global_body(); data->add( IfDef_GENC_IMPLEMENTATION ); - data->add( global_code( + data->add( global_body_code( static genc_array(genc_AST) genc_CodePool = nullptr; static genc_array(genc_arena) genc_StringArenas = nullptr; @@ -425,7 +423,7 @@ Code make_static_Data() static allocator genc_Allocator_TypeTable = zpl_heap(); )); data->add( untyped_str( R"(#ifdef GENC_DEFINE_LIBRARY_CODE_CONSTANTS)")); - data->add( global_code( + data->add( global_body_code( Code t_void; Code t_bool; @@ -449,7 +447,7 @@ Code make_static_Data() Code t_f64; )); data->add( untyped_str( R"(#endif)")); - data->add( global_code( + data->add( global_body_code( Code spec_inline; Code spec_const; )); @@ -509,11 +507,11 @@ int gen_main() Memory::setup(); gen::init(); - IfDef_GENC_IMPLEMENTATION = untyped( + IfDef_GENC_IMPLEMENTATION = untyped_str( R"(#ifdef GENC_IMPLEMENTATION)" ); - EndIf_GENC_IMPLEMENTATION = untyped( + EndIf_GENC_IMPLEMENTATION = untyped_str( R"(#endif // GENC_IMPLEMENTATION)" ); diff --git a/test/Array.hpp b/test/Array.hpp index 317bf41..0c0183f 100644 --- a/test/Array.hpp +++ b/test/Array.hpp @@ -13,7 +13,7 @@ #ifdef gen_time Code gen__array_base() { - #ifndef GEN_DEFINE_DSL +# ifndef GEN_DEFINE_DSL using namespace gen; Code t_allocator = def_type( txt(allocator) ); @@ -34,39 +34,40 @@ Code params = def_params(1, t_uw, "value" ); Code body = untyped_str( txt( return 2 * value * 8; ) ); - grow_formula = def_proc( "grow_formula", spec, params, t_sw, body ); + grow_formula = def_function( "grow_formula", spec, params, t_sw, body ); } Code body = def_struct_body(2, header, grow_formula); Code ArrayBase = def_struct( "ArrayBase", body ); - - #else - typename( allocator, allocator ); + +# else + Code t_allocator = type( allocator ); Code Header; { - variable( uw, Num, ); - variable( uw, Capacity ); - variable( allocator, Allocator ); + Code Num = variable( uw, Num, ); + Code Capacity = variable( uw, Capacity ); + Code Allocator = variable( allocator, Allocator ); + Code body = struct_body( Num, Capacity, Allocator ); - Code body = struct_body( Num, Capacity, Allocator ); - - struct( Header, __, __, body ); + Header = struct( Header, __, __, body ); } - Code proc( grow_formula, spec_inline, t_uw, params( t_uw, "value" ), - untyped( return 2 * value * 8 ) + Code test = params( uw, value ); + + Code grow_formula = function( grow_formula, params( uw, value ), uw, spec_inline, + code_str( return 2 * value * 8 ) ); - Code struct( ArrayBase, __, __, struct_body( Header, grow_formula ) ); - #endif + Code ArrayBase = struct( ArrayBase, __, __, struct_body( Header, grow_formula ) ); +# endif return ArrayBase; } Code gen__array( char const* type_str, s32 type_size, Code parent ) { - #ifndef GEN_DEFINE_DSL +# ifndef GEN_DEFINE_DSL // Make these global consts to be accessed anywhere... Code t_allocator = def_type( txt(allocator) ); @@ -96,7 +97,7 @@ Code params = def_params( 1, t_allocator, "mem_handler" ); Code body = untyped_str( txt( return init_reserve( mem_handler, grow_formula(0) ); ) ); - init = def_proc( "init", UnusedCode, params, t_bool, body ); + init = def_function( "init", UnusedCode, params, t_bool, body ); } Code init_reserve; @@ -104,8 +105,8 @@ Code params = def_params( 2, t_allocator, "mem_handler", t_sw, "capacity" ); Code body; { - Code header_value = untyped_str( txt( - rcast( Header*, alloc( mem_handler, sizeof( Header ) + sizeof(Type) + capacity )) + Code header_value = untyped_str( txt( + rcast( Header*, alloc( mem_handler, sizeof( Header ) + sizeof(Type) + capacity )) )); Code header = def_variable( ptr_header, "header", header_value ); @@ -125,11 +126,11 @@ "Data = rcast( %s, header + 1 );", ptr_type ); - Code ret_true = untyped_str( txt( - return true + Code ret_true = untyped_str( txt( + return true )); - body = def_proc_body( 5 + body = def_function_body( 5 , header , null_check , header_init @@ -138,7 +139,7 @@ ); } - init_reserve = def_proc( "init_reserve", UnusedCode, params, t_bool, body ); + init_reserve = def_function( "init_reserve", UnusedCode, params, t_bool, body ); } Code free; @@ -148,17 +149,17 @@ ::free( header.Allocator, & get_header() ); )); - free = def_proc( "free", UnusedCode, UnusedCode, t_void, body ); + free = def_function( "free", UnusedCode, UnusedCode, t_void, body ); } - Code append = make_proc( "append" ); + Code append = make_function( "append" ); { append->add( def_params( 1, type, "value") ); append->add( t_bool ); - Code + Code body = append.body(); - body->add( + body->add( untyped_str( txt( if ( header.Capacity < header.Num + 1 ) if ( ! grow(0) ) @@ -169,7 +170,7 @@ body->add( untyped_str( txt( Data[ header.Num ] = value; header.Num++; - + return true; ))); @@ -184,14 +185,14 @@ return data[ header.Num - 1 ]; )); - back = def_proc( "back", UnusedCode, UnusedCode, type, body ); + back = def_function( "back", UnusedCode, UnusedCode, type, body ); } Code clear; { Code body = untyped_str( txt( get_header().Num = 0; )); - clear = def_proc( "clear", UnusedCode, UnusedCode, t_void, body ); + clear = def_function( "clear", UnusedCode, UnusedCode, t_void, body ); } Code fill; @@ -209,17 +210,17 @@ Data[index] = vallue; )); - body = def_proc_body( 3, header, check, iter ); + body = def_function_body( 3, header, check, iter ); } - fill = def_proc( "fill", UnusedCode, params, t_void, body ); + fill = def_function( "fill", UnusedCode, params, t_void, body ); } Code get_header; { Code body = untyped_str( txt( return pcast( Header, Data - 1 ); )); - get_header = def_proc( "get_header", spec_inline, UnusedCode, ref_header, body ); + get_header = def_function( "get_header", spec_inline, UnusedCode, ref_header, body ); } Code grow; @@ -236,10 +237,10 @@ Code ret = untyped_str( "return set_capacity( new_capacity );" ); - body = def_proc_body( 4, header, new_capacity, check_n_set, ret ); + body = def_function_body( 4, header, new_capacity, check_n_set, ret ); } - grow = def_proc( "grow", UnusedCode, param, t_bool, body ); + grow = def_function( "grow", UnusedCode, param, t_bool, body ); } Code pop; @@ -249,10 +250,10 @@ Code assertion = untyped_str( "assert( header.Num > 0 );" ); Code decrement = untyped_str( "header.Num--; " ); - body = def_proc_body( 3, header, assertion, decrement ); + body = def_function_body( 3, header, assertion, decrement ); } - pop = def_proc( "pop", UnusedCode, UnusedCode, t_void, body ); + pop = def_function( "pop", UnusedCode, UnusedCode, t_void, body ); } Code reserve; @@ -267,10 +268,10 @@ Code ret = untyped_str( "\t" "return true" ); - body = def_proc_body( 3, header, check_n_set, ret ); + body = def_function_body( 3, header, check_n_set, ret ); } - reserve = def_proc( "reserve", UnusedCode, params, t_bool, body ); + reserve = def_function( "reserve", UnusedCode, params, t_bool, body ); } Code resize; @@ -290,10 +291,10 @@ "\n""return true;" ); - body = def_proc_body( 3, header, check_n_grow, set_n_ret ); + body = def_function_body( 3, header, check_n_grow, set_n_ret ); } - resize = def_proc( "resize", UnusedCode, param, t_bool, body ); + resize = def_function( "resize", UnusedCode, param, t_bool, body ); } Code set_capacity = parse_proc( txt_with_length( @@ -350,191 +351,192 @@ array_def = def_struct( name, body, parent ); } - #else - typename( allocator, allocator ); - - untyped( v_nullptr, nullptr ); +# else + type( allocator ); - typename( elem_type, type_str ); - typename_fmt( ptr_type, "%s*", type_str ); - typename_fmt( ref_type, "%&", type_str ); + Code v_nullptr = code_str( nullptr ); + + Code t_elem_type = def_type( type_str ); + Code t_ptr_type = def_type( type_str, spec_ptr ); + Code t_ref_type = type_fmt( type_str, spec_ref ); // From ArrayBase - typename( header, Header ); - typename( ptr_header, Header* ); - typename( ref_header, Header& ); + Code t_Header = type( Header ); + Code t_ref_Header = type( ref_Header, spec_ref ); + Code t_ptr_Header = type( ptr_Header, spec_ptr ); +# pragma push_macro("rcast") +# undef rcast Code array_def; { - using_type( Type, elem_type ); + Code Type = using( Type, elem_type ); - variable( ptr_type, Data ); - variable( ref_header, header, untyped_str("get_header()") ); + Code Data = variable( ptr_type, Data ); + Code header = variable( ref_Header, header, code_str(get_header()) ); Code init; { - Code body = proc_body( untyped( - return init_reserve( mem_handler, grow_formula(0) ); + Code body = function_body( code_str( + return init_reserve( mem_handler, grow_formula(0) ); )); - proc( init, __, t_bool, params( t_allocator, "mem_handler" ), body ); + init = function( init, params( allocator, mem_handler ), bool, __, body ); } - make( proc, init_reserve, __, params( t_ref_type, "mem_handler" ), t_bool); + make( function, init_reserve, params( ref_type, mem_handler ), bool, __); { - Code + Code body = init_reserve.body(); - body->add_var( ptr_header, header, untyped( - value_str( rcast( Header*, alloc( mem_handler, sizeof(Header) + sizeof(Type) + capacity)) ) + body->add( variable( ptr_Header, header, + code_str( rcast( Header*, alloc( mem_handler, sizeof(Header) + sizeof(Type) + capacity)) ) ) ); - body->add_untyped( + body->add( code_str( if (header == nullptr) return false; - ); + )); - body->add_untyped( + body->add( code_str( header->Num = 0; header->Capacity = capacity; header->Allocator = mem_handler; - ); + )); - body->add_untyped( + body->add( code_str( Data = rcast( Type*, header + 1 ); return true; - ); - } - - Code free; - { - proc( free, __, t_void, __, untyped( - Header& header = get_header(); - - free( header.Allocator, & get_header() ); )); } - make( proc, append ) - { - append->add_params( t_elem_value, "value" ); - append->add_ret_type( void ); + Code free = function( free, __, void, __, code_str( + Header& header = get_header(); - Code + free( header.Allocator, & get_header() ); + )); + + make( function, append ) + { + append->add( params( elem_type, value) ); + append->add( type( void ) ); + + Code body = append.body(); - body->add_untyped( + body->add( code_str( if ( header.Capacity < header.Num + 1 ) if ( ! grow(0) ) return false; - ); + )); - body->add_untyped( assign, + body->add( code_str( Data[ header.Num ] = value; header.Num++; - + return true; - ); + )); } Code back; { - Code body = untyped( + Code body = code_str( Header& header = get_header(); return data[ header.Num - 1 ]; ); - proc( back, __, t_elem_type, __, body ); + back = function( back, __, elem_type, __, body ); } Code clear; - proc( clear, __, t_void, __, untyped_str("get_header().Num = 0;") ); + function( clear, __, t_void, __, untyped_str("get_header().Num = 0;") ); Code fill; { - Code check = untyped( + Code check = code_str( if ( begin < 0 || end >= header.Num ) fatal( "Range out of bounds" ); ); - Code iter = untyped( + Code iter = code_str( for ( sw index = begin; index < end; index++ ) Data[index] = vallue; ); - Code body = proc_body( header, check, iter ); + Code body = function_body( header, check, iter ); - proc( fill, __, t_void, params( t_uw, "begin", t_uw, "end", t_elem_type, "value" ), body ); + fill = function( fill, params( uw, begin, uw, end, elem_type, value ), void, __, body ); } Code get_header; - proc( get_header, spec_inline, t_ref_header, __, untyped_str("return pcast( Header, Data - 1);") ); + function( get_header, __, ref_Header, spec_inline, code_str( return pcast( Header, Data - 1); ) ); + + Code test = function( test, __, void, __, __ ); Code grow; { Code body; { - variable( uw, new_capacity, untyped( grow_formula( header.Capacity) )); + variable( uw, new_capacity, code_str( grow_formula( header.Capacity) )); - Code check_n_set = untyped( + Code check_n_set = code_str( if ( new_capacity < min_capacity ) new_capacity = min_capacity; ); - Code ret = untyped( return set_capacity( new_capacity ); ); + Code ret = code_str( return set_capacity( new_capacity ); ); - body = proc_body( header, new_capacity, check_n_set, ret ); + body = function_body( header, new_capacity, check_n_set, ret ); } - proc( grow, __, t_bool, params( t_uw, "min_capacity" ), body ); + grow = function( grow, params( uw, min_capacity ), bool, body ); } Code pop; { - untyped_code( assertion, assert( header.Num > 0 ); ); - untyped_code( decrement, header.Num--; ); + Code assertion = code_str( assert( header.Num > 0 ); ); + Code decrement = code_str( header.Num--; ); - Code body = proc_body( header, assertion, decrement ); + Code body = function_body( header, assertion, decrement ); - proc( pop, __, t_void, __, body ); + pop = function( pop, __, void, __, body ); } Code reserve; { - untyped_code( check_n_set, + Code check_n_set = code_str( if ( header.Capacity < new_capacity ) return set_capacity( new_capacity ); ); - Code ret = untyped_str("return true"); + Code ret = code_str( return true ); - Code body = proc_body( header, check_n_set, ret ); + Code body = function_body( header, check_n_set, ret ); - proc( reserve, __, t_bool, params( t_uw, "new_capacity" ), body ); + reserve = function( reserve, params( uw, new_capacity ), bool, __, body ); } Code resize; { Code body; { - untyped_code( check_n_grow, + Code check_n_grow = code_str( if ( header.Capacity < new_num ) if ( ! grow( new_num) ) return false; ); - untyped_code( set_n_ret, + Code set_n_ret = code_str( header.Count = new_num; return true; ); - body = proc_body( header, check_n_grow, set_n_ret ); + body = function_body( header, check_n_grow, set_n_ret ); } - proc( resize, __, t_bool, params( t_uw, "new_num" ), body ); + resize = function( resize, params( uw, new_num ), bool, __, body ); } - Code set_capacity = proc_code( + Code set_capacity = function_code( bool set_capacity( new_capacity ) { Header& header = get_header(); @@ -546,7 +548,7 @@ header.Num = capacity; uw size = sizeof(Header) + sizeof(Type) * capacity; - Header* new_header = rcast( Header* alloc( header.Allocator, size )); + Header* new_header = rcast( Header*, alloc( header.Allocator, size )); if ( new_header == nullptr ) return false; @@ -568,7 +570,7 @@ char const* name = bprintf( "Array_%s", type_str ); Code body = struct_body( - Type + Type , Data , init @@ -586,9 +588,10 @@ , set_capacity ); - array_def = def_struct( name, body, parent ); + array_def = struct( name, parent, body ); } - #endif +# pragma pop_macro("rcast") +# endif return array_def; } diff --git a/test/gen/meson.build b/test/gen/meson.build index 4be9050..03522e2 100644 --- a/test/gen/meson.build +++ b/test/gen/meson.build @@ -2,10 +2,10 @@ project( 'test', 'c', 'cpp', default_options : ['buildtype=debug'] ) # add_global_arguments('-E', language : 'cpp') -includes = include_directories( -[ +includes = include_directories( +[ '../../project', - '../../thirdparty' + '../../thirdparty' ]) # get_sources = files('./get_sources.ps1') @@ -16,8 +16,11 @@ sources = [ '../test.cpp' ] if get_option('buildtype').startswith('debug') add_project_arguments('-DBuild_Debug', language : ['c', 'cpp']) + # add_project_arguments('-E', language : ['c', 'cpp']) endif + # add_project_arguments('-E', language : ['c', 'cpp']) + # add_global_arguments( '-E', language : ['cpp']) add_project_arguments('-Dgen_time', language : ['c', 'cpp']) diff --git a/test/math.hpp b/test/math.hpp index 18c70bf..a317673 100644 --- a/test/math.hpp +++ b/test/math.hpp @@ -10,7 +10,7 @@ /* What it should generate: - + inline type square( type value ) { @@ -20,19 +20,19 @@ #define gen_square( Type_ ) gen__square( #Type_ ) Code gen__square( char const* type ) { - Code integral_type = def_type( type ); + Code t_integral_type = def_type( type ); #ifndef GEN_DEFINE_DSL string name = string_sprintf( g_allocator, (char*)sprintf_buf, ZPL_PRINTF_MAXLEN, "square", type ); - #if 0 + #if 1 Code square; { Code params = def_params( 1, integral_type, "value" ); Code specifiers = def_specifiers( 1, SpecifierT::Inline ); Code ret_stmt = untyped_str( txt( return value * value; )); - square = def_proc( name, specifiers, params, integral_type, ret_stmt ); + square = def_function( name, specifiers, params, integral_type, ret_stmt ); } #else @@ -45,16 +45,15 @@ ); char const* gen_code = token_fmt( tmpl, 1, "type", type ); - Code square = parse_proc(gen_code, strlen(gen_code)); + Code square = parse_function(gen_code, strlen(gen_code)); #endif - #else - - Code proc( square, __, integral_type, params( integral_type, "value" ), - untyped(return value * value) + #else + Code square = function( square, params( integral_type, value), integral_type, __, + code_str(return value * value) ); #endif - + if ( ! square ) fatal( "Failed to generate square function for: %s", type ); @@ -68,7 +67,7 @@ Code fadd_u32 = gen_square( u32 ); Code fadd_u64 = gen_square( u64 ); - Builder + Builder mathgen; mathgen.open( "math.gen.hpp" ); mathgen.print( fadd_u8 );