WIP : Design for V1 finalization

There shouldn't be much more changes design wise.

Anything else is based on what needs to be done for it to work.
This commit is contained in:
Edward R. Gonzalez 2023-04-06 02:21:23 -04:00
parent a4cb0c12ef
commit 47c15e7af2
10 changed files with 768 additions and 684 deletions

View File

@ -9,5 +9,6 @@
"xutility": "cpp",
"initializer_list": "cpp",
"table.h": "c"
}
},
"C_Cpp.intelliSenseEngineFallback": "disabled"
}

View File

@ -44,7 +44,7 @@ namespace Memory
struct TokEntry
{
char const* Str;
s32 Length;
sw Length;
};
ZPL_TABLE( static, TokMap, tokmap_, TokEntry )
@ -68,7 +68,7 @@ sw token_fmt_va( char* buf, uw buf_size, char const* fmt, s32 num_tokens, va_lis
TokEntry entry
{
value,
zpl_strnlen(value, 128)
zpl_strnlen(value, (sw)128)
};
u32 key = crc32( token, zpl_strnlen(token, 32) );

View File

@ -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
#if defined(__GNUC__) || defined(__clang__) || true
// Supports 0-10 arguments
#define VA_NARGS_IMPL( _0, \
#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, \
_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
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 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, \
#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)
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)
#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 \
{ \

4
project/Bloat.redef.hpp Normal file
View File

@ -0,0 +1,4 @@
/*
Redefine any macros.
Used if you have some code thats getting parsed and don't want it ot get preprocessed.
*/

View File

@ -9,8 +9,63 @@ 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;
@ -21,10 +76,10 @@ namespace gen
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,19 +118,39 @@ 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() );
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, InvalidCode );
InvalidCode_write = ccast( Code, Code::Invalid );
InvalidCode_write = make_code();
#ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS
@ -85,7 +160,8 @@ namespace gen
# 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,8 +182,8 @@ 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 );
@ -115,14 +191,12 @@ namespace gen
# define def_constant_spec( Type_, ... ) \
Code& \
spec_##Type_##_write = ccast( Code, spec_##Type_ ); \
spec_##Type_##_write = def_specifiers( VA_NARGS(__VA_ARGS__), __VA_ARGS__)
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,66 +285,7 @@ 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
@ -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;
}
}
s32 name_length = zpl_strnlen( name, MaxNameLength );
Code
result = make_code();
result->Name = string_make( g_allocator, name );
result->Type = Proc;
result->Name = code_string( name, name_length );
result->Type = Function;
array_init( result->Entries, g_allocator );
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:
@ -383,20 +400,20 @@ namespace gen
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:
@ -437,6 +454,72 @@ namespace gen
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;
@ -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
@ -486,7 +569,7 @@ 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;
@ -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,7 +689,7 @@ 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;
}
}
@ -623,19 +706,19 @@ 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
@ -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,25 +777,25 @@ 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
result = make_code();
result->Name = string_make( g_allocator, name );
result->Type = Proc;
result->Type = Function;
array_init( result->Entries, g_allocator );
@ -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;
@ -891,7 +974,7 @@ namespace gen
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++;
@ -922,12 +1005,12 @@ namespace gen
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
result = make_code();
result->Name = string_make( g_allocator, name );
result->Type = ECode::Proc;
result->Type = ECode::Function;
array_init( result->Entries, g_allocator );
@ -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;
@ -1227,9 +1310,12 @@ namespace gen
return result;
}
const Code Code::Invalid;
# pragma endregion AST
# pragma region Builder
void Builder::print( Code code )
{

View File

@ -1,6 +1,9 @@
/*
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++.
@ -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
@ -169,26 +164,29 @@
// 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
{
@ -206,9 +204,9 @@ namespace gen
Namespace,
Namespace_Body,
Parameters,
Proc,
Proc_Body,
Proc_Forward,
Function,
Function_Body,
Function_Forward,
Specifiers,
Struct,
Struct_Body,
@ -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",
@ -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;
return Invalid;
}
#endif
# endif
return * (Code*)( ast->body() );
}
@ -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!");
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;
/*
*/
@ -593,7 +592,7 @@ namespace gen
*/
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_enum( char const* name, Code type, Code body );
Code def_global_body( s32 num, ... );
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_body( s32 num, ... );
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_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_param ( Code type, char const* name );
Code def_param ( Code type, s32 length, char const* name );
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_specifier ( SpecifierT* specifier );
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_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_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:
... { <ID> } ... Will be repalced with value of token ID.
Values are to provided as: <char const* ID>, <char const* Value>, ...
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_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,40 +742,94 @@ 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
@ -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;

View File

@ -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, ...)
{
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;
#if Build_Debug
#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
#else
va_start(va, fmt);
zpl_printf_err_va( fmt, va);
va_end(va);
zpl_exit(1);
return -1;
#endif
})"
, 372
#endif
}
));
result->add( untyped_str(
@ -253,9 +252,9 @@ Code make_ECode()
{
Code ECode = make_global_body();
{
ECode->add( parse_enum(
R"(enum genc_ECode
{
ECode->add( enum_code(
enum genc_ECode
{
Invalid,
Untyped,
Enum,
@ -273,12 +272,11 @@ R"(enum genc_ECode
Typename,
Num_Types
};
typedef u32 genc_CodeT;)" + 1
, 280
};
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)"
);

View File

@ -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 );
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;
@ -129,7 +130,7 @@
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,10 +149,10 @@
::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 );
@ -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 );
# else
type( allocator );
untyped( v_nullptr, nullptr );
Code v_nullptr = code_str( nullptr );
typename( elem_type, type_str );
typename_fmt( ptr_type, "%s*", type_str );
typename_fmt( ref_type, "%&", type_str );
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(
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
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(
Code free = function( free, __, void, __, code_str(
Header& header = get_header();
free( header.Allocator, & get_header() );
));
}
make( proc, append )
make( function, append )
{
append->add_params( t_elem_value, "value" );
append->add_ret_type( void );
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;
@ -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;
}

View File

@ -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'])

View File

@ -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,13 +45,12 @@
);
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)
Code square = function( square, params( integral_type, value), integral_type, __,
code_str(return value * value)
);
#endif