Parsing constructors passed the sanity test!

This commit is contained in:
Edward R. Gonzalez 2023-07-10 22:14:41 -04:00
parent ed6a1d0f95
commit 14568d512e
19 changed files with 1296 additions and 491 deletions

29
.vscode/gencpp.natvis vendored
View File

@ -1,6 +1,10 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010"> <AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
<Type Name="StrC">
<DisplayString>Len:{Len} Ptr:{Ptr, [Len]s}</DisplayString>
</Type>
<Type Name="String"> <Type Name="String">
<DisplayString Condition="Data == nullptr">null</DisplayString> <DisplayString Condition="Data == nullptr">null</DisplayString>
<DisplayString>{Data,na}</DisplayString> <DisplayString>{Data,na}</DisplayString>
@ -30,7 +34,30 @@
<Expand> <Expand>
<Item Name="Type">Type</Item> <Item Name="Type">Type</Item>
<Item Name="Name">Name</Item> <Item Name="Name">Name</Item>
<Item Name="Parent">Parent</Item>
<Item Name="Op" Condition="Op != 43">Op</Item>
<Item Name="ModuleFlags">ModuleFlags</Item>
<Item Name="ParentAcess" Condition="ParentAccess != gen::AccessSpec::Invalid">ParentAccess</Item>
<Item Name="ArrStatic" Condition="DynamicEntries == false">ArrStatic</Item> <Item Name="ArrStatic" Condition="DynamicEntries == false">ArrStatic</Item>
<Item Name="Index" Condition="DynamicEntries == false">StaticIndex</Item>
<Item Name="ArrDyn" Condition="DynamicEntries == true">ArrDyn</Item>
<Item Name="Index" Condition="DynamicEntries == true">((ArrayHeader*)((char*)ArrDyn - sizeof(ArrayHeader)))->count</Item>
</Expand>
</Type>
<Type Name="gen::Code">
<DisplayString Condition="ast != nullptr">{ast->Name} {ast->Type}</DisplayString>
<Expand>
<Item Name="Type">ast->Type</Item>
<Item Name="Name">ast->Name</Item>
<Item Name="Parent">ast->Parent</Item>
<Item Name="Op" Condition="ast->Op != 43">ast->Op</Item>
<Item Name="ModuleFlags">ast->ModuleFlags</Item>
<Item Name="ParentAcess" Condition="ast->ParentAccess != gen::AccessSpec::Invalid">ast->ParentAccess</Item>
<Item Name="ArrStatic" Condition="ast->DynamicEntries == false">ast->ArrStatic</Item>
<Item Name="Index" Condition="ast->DynamicEntries == false">ast->StaticIndex</Item>
<Item Name="ArrDyn" Condition="ast->DynamicEntries == true">ast->ArrDyn</Item>
<Item Name="Index" Condition="ast->DynamicEntries == true">((ArrayHeader*)((char*)ast->ArrDyn - sizeof(ArrayHeader)))->count</Item>
</Expand> </Expand>
</Type> </Type>
@ -39,7 +66,7 @@
</Type> </Type>
<Type Name ="gen::Parser::Token"> <Type Name ="gen::Parser::Token">
<DisplayString>Type:{Type} Text:{Text, [Length]s} Length:{Length}</DisplayString> <DisplayString>Length:{Length} Text:{Text, [Length]s} Type:{Type}</DisplayString>
</Type> </Type>
<Type Name="gen::Parser::TokArray"> <Type Name="gen::Parser::TokArray">

View File

@ -262,7 +262,8 @@ Data Notes:
Two generic templated containers throughout the library: Two generic templated containers throughout the library:
`template< class Type> struct Array` and `template< class Type> struct HashTable >` * `template< class Type> struct Array`
* `template< class Type> struct HashTable`
Otherwise the library is free of any templates. Otherwise the library is free of any templates.

View File

@ -0,0 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=53AF600D_002DC09C_002D4F39_002D83E0_002DE022AA9479F2_002Fd_003Athirdparty_002Ff_003Azpl_002Eh/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/SweaWarningsMode/@EntryValue">ShowAndRun</s:String></wpf:ResourceDictionary>

View File

@ -103,27 +103,34 @@
<None Include="scripts\build.ps1" /> <None Include="scripts\build.ps1" />
<None Include="scripts\clean.ps1" /> <None Include="scripts\clean.ps1" />
<None Include="scripts\get_sources.ps1" /> <None Include="scripts\get_sources.ps1" />
<None Include="scripts\meson.build" />
<None Include="singleheader\genc.refactor" />
<None Include="test\gen\meson.build" /> <None Include="test\gen\meson.build" />
<None Include="test\meson.build" /> <None Include="test\meson.build" />
<None Include="test\Readme.md" /> <None Include="test\Readme.md" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="project\Banned.define.hpp" />
<ClInclude Include="project\Banned.undef.hpp" />
<ClInclude Include="project\Bloat.hpp" /> <ClInclude Include="project\Bloat.hpp" />
<ClInclude Include="project\Bloat.redef.hpp" />
<ClInclude Include="project\Bloat.undef.hpp" />
<ClInclude Include="project\gen.hpp" /> <ClInclude Include="project\gen.hpp" />
<ClInclude Include="test\NonParsed\Sanity.hpp" /> <ClInclude Include="test\DummyInclude.hpp" />
<ClInclude Include="test\NonParsed\Array.NonParsed.hpp" />
<ClInclude Include="test\NonParsed\Buffer.NonParsed.hpp" />
<ClInclude Include="test\NonParsed\HashTable.NonParsed.hpp" />
<ClInclude Include="test\NonParsed\Memory.NonParsed.hpp" />
<ClInclude Include="test\NonParsed\Ring.NonParsed.hpp" />
<ClInclude Include="test\NonParsed\Sanity.NonParsed.hpp" />
<ClInclude Include="test\Parsed\Array.Parsed.hpp" />
<ClInclude Include="test\Parsed\Sanity.Parsed.hpp" />
<ClInclude Include="thirdparty\zpl.h" /> <ClInclude Include="thirdparty\zpl.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="project\Bloat.cpp" /> <ClCompile Include="project\Bloat.cpp" />
<ClCompile Include="project\gen.cpp" /> <ClCompile Include="project\gen.cpp" />
<ClCompile Include="project\gen.singleheader.cpp" /> <ClCompile Include="singleheader\gen\gen.singleheader.cpp" />
<ClCompile Include="test\gen\build\meson-private\sanitycheckc.c" /> <ClCompile Include="test\gen\build\meson-private\sanitycheckc.c" />
<ClCompile Include="test\gen\build\meson-private\sanitycheckcpp.cc" /> <ClCompile Include="test\gen\build\meson-private\sanitycheckcpp.cc" />
<ClCompile Include="test\test.cpp" /> <ClCompile Include="test\test.NonParsed.cpp" />
<ClCompile Include="test\test.Parsed.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Natvis Include=".vscode\gencpp.natvis" /> <Natvis Include=".vscode\gencpp.natvis" />

View File

@ -2,6 +2,165 @@
#include "Bloat.hpp" #include "Bloat.hpp"
void* gen_Arena::allocator_proc( void* allocator_data, AllocType type, sw size, sw alignment, void* old_memory, sw old_size, u64 flags )
{
gen_Arena* arena = rcast(gen_Arena*, allocator_data);
void* ptr = NULL;
// unused( old_size );
switch ( type )
{
case EAllocation_ALLOC :
{
void* end = pointer_add( arena->PhysicalStart, arena->TotalUsed );
sw total_size = align_forward_i64( size, alignment );
// NOTE: Out of memory
if ( arena->TotalUsed + total_size > (sw) arena->TotalSize )
{
// zpl__printf_err("%s", "Arena out of memory\n");
return nullptr;
}
ptr = align_forward( end, alignment );
arena->TotalUsed += total_size;
if ( flags & ZPL_ALLOCATOR_FLAG_CLEAR_TO_ZERO )
zero_size( ptr, size );
}
break;
case EAllocation_FREE :
// NOTE: Free all at once
// Use Temp_Arena_Memory if you want to free a block
break;
case EAllocation_FREE_ALL :
arena->TotalUsed = 0;
break;
case EAllocation_RESIZE :
{
// TODO : Check if ptr is on top of stack and just extend
AllocatorInfo a = arena->Backing;
ptr = default_resize_align( a, old_memory, old_size, size, alignment );
}
break;
}
return ptr;
}
void* gen_Pool::allocator_proc( void* allocator_data, AllocType type, sw size, sw alignment, void* old_memory, sw old_size, u64 flags )
{
gen_Pool* pool = zpl_cast( gen_Pool* ) allocator_data;
void* ptr = NULL;
// unused( old_size );
switch ( type )
{
case EAllocation_ALLOC :
{
uptr next_free;
ZPL_ASSERT( size == pool->BlockSize );
ZPL_ASSERT( alignment == pool->BlockAlign );
ZPL_ASSERT( pool->FreeList != NULL );
next_free = *zpl_cast( uptr* ) pool->FreeList;
ptr = pool->FreeList;
pool->FreeList = zpl_cast( void* ) next_free;
pool->TotalSize += pool->BlockSize;
if ( flags & ZPL_ALLOCATOR_FLAG_CLEAR_TO_ZERO )
zero_size( ptr, size );
}
break;
case EAllocation_FREE :
{
uptr* next;
if ( old_memory == NULL )
return NULL;
next = zpl_cast( uptr* ) old_memory;
*next = zpl_cast( uptr ) pool->FreeList;
pool->FreeList = old_memory;
pool->TotalSize -= pool->BlockSize;
}
break;
case EAllocation_FREE_ALL :
{
sw actual_block_size, block_index;
void* curr;
uptr* end;
actual_block_size = pool->BlockSize + pool->BlockAlign;
pool->TotalSize = 0;
// NOTE: Init intrusive freelist
curr = pool->PhysicalStart;
for ( block_index = 0; block_index < pool->NumBlocks - 1; block_index++ )
{
uptr* next = zpl_cast( uptr* ) curr;
*next = zpl_cast( uptr ) curr + actual_block_size;
curr = pointer_add( curr, actual_block_size );
}
end = zpl_cast( uptr* ) curr;
*end = zpl_cast( uptr ) NULL;
pool->FreeList = pool->PhysicalStart;
}
break;
case EAllocation_RESIZE :
// NOTE: Cannot resize
ZPL_PANIC( "You cannot resize something allocated by with a pool." );
break;
}
return ptr;
}
gen_Pool gen_Pool::init_align( AllocatorInfo backing, sw num_blocks, sw block_size, sw block_align )
{
gen_Pool pool = {};
sw actual_block_size, pool_size, block_index;
void *data, *curr;
uptr* end;
pool.Backing = backing;
pool.BlockSize = block_size;
pool.BlockAlign = block_align;
pool.NumBlocks = num_blocks;
actual_block_size = block_size + block_align;
pool_size = num_blocks * actual_block_size;
data = alloc_align( backing, pool_size, block_align );
// NOTE: Init intrusive freelist
curr = data;
for ( block_index = 0; block_index < num_blocks - 1; block_index++ )
{
uptr* next = ( uptr* ) curr;
*next = ( uptr ) curr + actual_block_size;
curr = pointer_add( curr, actual_block_size );
}
end = ( uptr* ) curr;
*end = ( uptr ) 0;
pool.PhysicalStart = data;
pool.FreeList = data;
return pool;
}
namespace Memory namespace Memory
{ {
using namespace zpl; using namespace zpl;
@ -16,7 +175,7 @@ namespace Memory
switch ( type ) switch ( type )
{ {
case EAllocationALLOC: case EAllocation_ALLOC:
{ {
if ( last->total_allocated + size > last->total_size ) if ( last->total_allocated + size > last->total_size )
{ {
@ -34,15 +193,15 @@ namespace Memory
return alloc_align( arena_allocator( last), size, alignment ); return alloc_align( arena_allocator( last), size, alignment );
} }
case EAllocationFREE: case EAllocation_FREE:
{ {
// Doesn't recycle. // Doesn't recycle.
} }
case EAllocationFREE_ALL: case EAllocation_FREE_ALL:
{ {
// Memory::cleanup instead. // Memory::cleanup instead.
} }
case EAllocationRESIZE: case EAllocation_RESIZE:
{ {
if ( last->total_allocated + size > last->total_size ) if ( last->total_allocated + size > last->total_size )
{ {

View File

@ -40,6 +40,7 @@ using zpl::sw;
using zpl::sptr; using zpl::sptr;
using zpl::uptr; using zpl::uptr;
using zpl::AllocType;
using zpl::Arena; using zpl::Arena;
using zpl::AllocatorInfo; using zpl::AllocatorInfo;
using zpl::ArrayHeader; using zpl::ArrayHeader;
@ -48,9 +49,17 @@ using zpl::FileError;
using zpl::Pool; using zpl::Pool;
// using zpl::String; // using zpl::String;
using zpl::EAllocation_ALLOC;
using zpl::EAllocation_FREE;
using zpl::EAllocation_FREE_ALL;
using zpl::EAllocation_RESIZE;
using zpl::EFileMode_WRITE; using zpl::EFileMode_WRITE;
using zpl::EFileError_NONE; using zpl::EFileError_NONE;
using zpl::ZPL_ALLOCATOR_FLAG_CLEAR_TO_ZERO;
using zpl::align_forward;
using zpl::align_forward_i64;
using zpl::alloc; using zpl::alloc;
using zpl::alloc_align; using zpl::alloc_align;
using zpl::arena_allocator; using zpl::arena_allocator;
@ -58,7 +67,6 @@ using zpl::arena_init_from_memory;
using zpl::arena_init_from_allocator; using zpl::arena_init_from_allocator;
using zpl::arena_free; using zpl::arena_free;
using zpl::assert_crash; using zpl::assert_crash;
using zpl::str_fmt_buf;
using zpl::char_first_occurence; using zpl::char_first_occurence;
using zpl::char_is_alpha; using zpl::char_is_alpha;
using zpl::char_is_alphanumeric; using zpl::char_is_alphanumeric;
@ -67,19 +75,23 @@ using zpl::char_is_hex_digit;
using zpl::char_is_space; using zpl::char_is_space;
using zpl::crc32; using zpl::crc32;
using zpl::free_all; using zpl::free_all;
using zpl::is_power_of_two;
using zpl::mem_copy; using zpl::mem_copy;
using zpl::mem_move; using zpl::mem_move;
using zpl::mem_set; using zpl::mem_set;
using zpl::pointer_add;
using zpl::pool_allocator; using zpl::pool_allocator;
using zpl::pool_init; using zpl::pool_init;
using zpl::pool_free; using zpl::pool_free;
using zpl::process_exit; using zpl::process_exit;
using zpl::str_compare;
using zpl::str_copy; using zpl::str_copy;
using zpl::str_fmt_buf;
using zpl::str_fmt_va; using zpl::str_fmt_va;
using zpl::str_fmt_out_va; using zpl::str_fmt_out_va;
using zpl::str_fmt_out_err_va; using zpl::str_fmt_out_err_va;
using zpl::str_compare;
using zpl::str_len; using zpl::str_len;
using zpl::zero_size;
#if __clang__ #if __clang__
# pragma clang diagnostic pop # pragma clang diagnostic pop
@ -141,14 +153,13 @@ using zpl::str_len;
#define bit( Value_ ) ( 1 << Value_ ) #define bit( Value_ ) ( 1 << Value_ )
#define bitfield_is_equal( Type_, Field_, Mask_ ) ( (Type_(Mask_) & Type_(Field_)) == Type_(Mask_) ) #define bitfield_is_equal( Type_, Field_, Mask_ ) ( (Type_(Mask_) & Type_(Field_)) == Type_(Mask_) )
#define forceinline ZPL_ALWAYS_INLINE #define forceinline ZPL_ALWAYS_INLINE
#define print_nl( _) zpl_printf("\n")
#define ccast( Type_, Value_ ) * const_cast< Type_* >( & (Value_) ) #define ccast( Type_, Value_ ) * const_cast< Type_* >( & (Value_) )
#define scast( Type_, Value_ ) static_cast< Type_ >( Value_ ) #define scast( Type_, Value_ ) static_cast< Type_ >( Value_ )
#define rcast( Type_, Value_ ) reinterpret_cast< Type_ >( Value_ ) #define rcast( Type_, Value_ ) reinterpret_cast< Type_ >( Value_ )
#define pcast( Type_, Value_ ) ( * (Type_*)( & (Value_) ) ) #define pcast( Type_, Value_ ) ( * (Type_*)( & (Value_) ) )
#define txt_impl( ... ) #__VA_ARGS__ #define GEN_STRINGIZE_VA( ... ) #__VA_ARGS__
#define txt( ... ) txt_impl( __VA_ARGS__ ) #define txt( ... ) GEN_STRINGIZE_VA( __VA_ARGS__ )
#define txt_n_len( ... ) sizeof( txt_impl( __VA_ARGS__ ) ), txt_impl( __VA_ARGS__ ) #define txt_to_StrC( ... ) sizeof( GEN_STRINGIZE_VA( __VA_ARGS__ ) ), GEN_STRINGIZE_VA( __VA_ARGS__ )
#define do_once() \ #define do_once() \
do \ do \
{ \ { \
@ -176,10 +187,127 @@ while(0);
constexpr constexpr
char const* Msg_Invalid_Value = "INVALID VALUE PROVIDED"; char const* Msg_Invalid_Value = "INVALID VALUE PROVIDED";
#pragma region Memory
// TODO : Use it.
struct gen_Arena
{
static
void* allocator_proc( void* allocator_data, AllocType type, sw size, sw alignment, void* old_memory, sw old_size, u64 flags );
static
gen_Arena init_from_memory( void* start, sw size )
{
return
{
{ nullptr, nullptr },
start,
size,
0,
0
};
}
static
gen_Arena init_from_allocator( AllocatorInfo backing, sw size )
{
gen_Arena result =
{
backing,
alloc( backing, size),
size,
0,
0
};
return result;
}
static
gen_Arena init_sub( gen_Arena& parent, sw size )
{
return init_from_allocator( parent.Backing, size );
}
sw alignment_of( sw alignment )
{
sw alignment_offset, result_pointer, mask;
ZPL_ASSERT( is_power_of_two( alignment ) );
alignment_offset = 0;
result_pointer = (sw) PhysicalStart + TotalUsed;
mask = alignment - 1;
if ( result_pointer & mask )
alignment_offset = alignment - ( result_pointer & mask );
return alignment_offset;
}
void check()
{
ZPL_ASSERT( TempCount == 0 );
}
void free()
{
if ( Backing.proc )
{
zpl::free( Backing, PhysicalStart );
PhysicalStart = nullptr;
}
}
sw size_remaining( sw alignment )
{
sw result = TotalSize - ( TotalUsed + alignment_of( alignment ) );
return result;
}
AllocatorInfo Backing;
void* PhysicalStart;
sw TotalSize;
sw TotalUsed;
sw TempCount;
};
struct gen_Pool
{
static
void* allocator_proc( void* allocator_data, AllocType type, sw size, sw alignment, void* old_memory, sw old_size, u64 flags );
static
gen_Pool init( AllocatorInfo backing, sw num_blocks, sw block_size )
{
return init_align( backing, num_blocks, block_size, ZPL_DEFAULT_MEMORY_ALIGNMENT );
}
static
gen_Pool init_align( AllocatorInfo backing, sw num_blocks, sw block_size, sw block_align );
void free()
{
if ( Backing.proc )
{
zpl::free( Backing, PhysicalStart );
}
}
AllocatorInfo Backing;
void* PhysicalStart;
void* FreeList;
sw BlockSize;
sw BlockAlign;
sw TotalSize;
sw NumBlocks;
};
#pragma endregion Memory
#pragma region Containers #pragma region Containers
#pragma push_macro("template") #pragma push_macro("template")
#undef template #undef template
// TODO : Use it.
template<class Type> template<class Type>
struct TArray struct TArray
{ {
@ -191,13 +319,13 @@ struct TArray
}; };
static static
TArray<Type> init( AllocatorInfo allocator ) TArray init( AllocatorInfo allocator )
{ {
return init_reserve( allocator, grow_formula(0) ); return init_reserve( allocator, grow_formula(0) );
} }
static static
TArray<Type> init_reserve( AllocatorInfo allocator, sw capacity ) TArray init_reserve( AllocatorInfo allocator, sw capacity )
{ {
Header* header = rcast( Header*, alloc( allocator, sizeof(Header) + sizeof(Type) )); Header* header = rcast( Header*, alloc( allocator, sizeof(Header) + sizeof(Type) ));
@ -369,6 +497,7 @@ struct TArray
} }
}; };
// TODO : Use it.
template<typename Type> template<typename Type>
struct THashTable struct THashTable
{ {
@ -387,7 +516,7 @@ struct THashTable
}; };
static static
THashTable<Type> init( AllocatorInfo allocator ) THashTable init( AllocatorInfo allocator )
{ {
THashTable<Type> result = {0}; THashTable<Type> result = {0};
@ -621,9 +750,6 @@ protected:
#pragma pop_macro("template") #pragma pop_macro("template")
#pragma endregion Containers #pragma endregion Containers
#pragma region Memory
#pragma endregion Memory
#pragma region String #pragma region String
// Constant string with length. // Constant string with length.
struct StrC struct StrC

File diff suppressed because it is too large Load Diff

View File

@ -65,6 +65,8 @@ namespace gen
Entry( Operator_Fwd ) \ Entry( Operator_Fwd ) \
Entry( Operator_Member ) \ Entry( Operator_Member ) \
Entry( Operator_Member_Fwd ) \ Entry( Operator_Member_Fwd ) \
Entry( Operator_Cast ) \
Entry( Operator_Cast_Fwd ) \
Entry( Parameters ) \ Entry( Parameters ) \
Entry( Preprocessor_Include ) \ Entry( Preprocessor_Include ) \
Entry( Specifiers ) \ Entry( Specifiers ) \
@ -95,7 +97,7 @@ namespace gen
{ {
static static
StrC lookup[Num_Types] = { StrC lookup[Num_Types] = {
# define Entry( Type ) { txt_n_len( Type ) }, # define Entry( Type ) { txt_to_StrC( Type ) },
Define_Types Define_Types
# undef Entry # undef Entry
}; };
@ -132,11 +134,11 @@ namespace gen
Entry( Assign, = ) \ Entry( Assign, = ) \
Entry( Assign_Add, += ) \ Entry( Assign_Add, += ) \
Entry( Assign_Subtract, -= ) \ Entry( Assign_Subtract, -= ) \
Entry( Assgin_Multiply, *= ) \ Entry( Assign_Multiply, *= ) \
Entry( Assgin_Divide, /= ) \ Entry( Assign_Divide, /= ) \
Entry( Assgin_Modulo, %= ) \ Entry( Assign_Modulo, %= ) \
Entry( Assgin_BAnd, &= ) \ Entry( Assign_BAnd, &= ) \
Entry( Assgin_BOr, |= ) \ Entry( Assign_BOr, |= ) \
Entry( Assign_BXOr, ^= ) \ Entry( Assign_BXOr, ^= ) \
Entry( Assign_LShift, <<= ) \ Entry( Assign_LShift, <<= ) \
Entry( Assign_RShift, >>= ) \ Entry( Assign_RShift, >>= ) \
@ -144,6 +146,7 @@ namespace gen
Entry( Decrement, -- ) \ Entry( Decrement, -- ) \
Entry( Unary_Plus, + ) \ Entry( Unary_Plus, + ) \
Entry( Unary_Minus, - ) \ Entry( Unary_Minus, - ) \
Entry( UnaryNot, ! ) \
Entry( Add, + ) \ Entry( Add, + ) \
Entry( Subtract, - ) \ Entry( Subtract, - ) \
Entry( Multiply, * ) \ Entry( Multiply, * ) \
@ -155,11 +158,10 @@ namespace gen
Entry( BXOr, ^ ) \ Entry( BXOr, ^ ) \
Entry( LShift, << ) \ Entry( LShift, << ) \
Entry( RShift, >> ) \ Entry( RShift, >> ) \
Entry( LNot, ! ) \
Entry( LAnd, && ) \ Entry( LAnd, && ) \
Entry( LOr, || ) \ Entry( LOr, || ) \
Entry( Equals, == ) \ Entry( LEqual, == ) \
Entry( NotEquals, != ) \ Entry( LNot, != ) \
Entry( Lesser, < ) \ Entry( Lesser, < ) \
Entry( Greater, > ) \ Entry( Greater, > ) \
Entry( LesserEqual, <= ) \ Entry( LesserEqual, <= ) \
@ -250,7 +252,7 @@ namespace gen
# define internal internal # define internal internal
# define local_persist local_persist # define local_persist local_persist
# define Entry( Spec_, Code_ ) { txt_n_len(Code_) }, # define Entry( Spec_, Code_ ) { txt_to_StrC(Code_) },
Define_Specifiers Define_Specifiers
# undef Entry # undef Entry
@ -272,7 +274,9 @@ namespace gen
{ {
StrC enum_str = to_str( (Type)index ); StrC enum_str = to_str( (Type)index );
keymap[index] = crc32( enum_str.Ptr, enum_str.Len); // We subtract 1 to remove the null terminator
// This is because the tokens lexed are not null terminated.
keymap[index] = crc32( enum_str.Ptr, enum_str.Len - 1);
} }
do_once_end do_once_end
@ -398,7 +402,6 @@ namespace gen
# pragma region Member Functions # pragma region Member Functions
void add_entry( AST* other ); void add_entry( AST* other );
inline
AST* body() AST* body()
{ {
return entry( 0 ); return entry( 0 );
@ -411,22 +414,18 @@ namespace gen
return DynamicEntries ? ArrDyn[ idx ] : ArrStatic[ idx ]; return DynamicEntries ? ArrDyn[ idx ] : ArrStatic[ idx ];
} }
inline
bool has_entries() bool has_entries()
{ {
return num_entries(); return num_entries();
} }
inline
bool is_invalid() bool is_invalid()
{ {
return Type != ECode::Invalid; return Type != ECode::Invalid;
} }
inline
bool is_equal( AST* other ); bool is_equal( AST* other );
inline
s32 num_entries() s32 num_entries()
{ {
return DynamicEntries ? array_count( ArrDyn ) : StaticIndex; return DynamicEntries ? array_count( ArrDyn ) : StaticIndex;
@ -434,7 +433,6 @@ namespace gen
// Parameter // Parameter
inline
AST* get_param( s32 index ) AST* get_param( s32 index )
{ {
if ( index <= 0 ) if ( index <= 0 )
@ -443,14 +441,12 @@ namespace gen
return entry( index + 1 ); return entry( index + 1 );
} }
inline
s32 param_count() s32 param_count()
{ {
// The first entry (which holds the type) represents the first parameter. // The first entry (which holds the type) represents the first parameter.
return num_entries(); return num_entries();
} }
inline
AST* param_type() AST* param_type()
{ {
return entry( 0 ); return entry( 0 );
@ -458,7 +454,6 @@ namespace gen
// Specifiers // Specifiers
inline
bool add_specifier( SpecifierT spec ) bool add_specifier( SpecifierT spec )
{ {
if ( StaticIndex == AST::ArrSpecs_Cap ) if ( StaticIndex == AST::ArrSpecs_Cap )
@ -472,7 +467,6 @@ namespace gen
return true; return true;
} }
inline
s32 has_specifier( SpecifierT spec ) s32 has_specifier( SpecifierT spec )
{ {
for ( s32 idx = 0; idx < StaticIndex; idx++ ) for ( s32 idx = 0; idx < StaticIndex; idx++ )
@ -486,21 +480,18 @@ namespace gen
// Typename // Typename
inline
bool typename_is_ptr() bool typename_is_ptr()
{ {
assert_crash("not implemented"); assert_crash("not implemented");
return false; return false;
} }
inline
bool typename_is_ref() bool typename_is_ref()
{ {
assert_crash("not implemented"); assert_crash("not implemented");
return false; return false;
} }
inline
AST* typename_specifiers() AST* typename_specifiers()
{ {
return entry( 0 ); return entry( 0 );
@ -508,7 +499,6 @@ namespace gen
// Serialization // Serialization
inline
char const* debug_str() char const* debug_str()
{ {
char const* fmt = txt( char const* fmt = txt(
@ -530,7 +520,6 @@ namespace gen
); );
} }
inline
char const* type_str() char const* type_str()
{ {
return ECode::to_str( Type ); return ECode::to_str( Type );
@ -582,9 +571,6 @@ namespace gen
# undef Using_CodePOD # undef Using_CodePOD
}; };
constexpr sw size_AST = sizeof(AST);
constexpr sw size_POD = sizeof(AST_POD);
// Its intended for the AST to have equivalent size to its POD. // Its intended for the AST to have equivalent size to its POD.
// All extra functionality within the AST namespace should just be syntatic sugar. // All extra functionality within the AST namespace should just be syntatic sugar.
static_assert( sizeof(AST) == sizeof(AST_POD), "ERROR: AST IS NOT POD" ); static_assert( sizeof(AST) == sizeof(AST_POD), "ERROR: AST IS NOT POD" );
@ -604,7 +590,6 @@ namespace gen
# pragma endregion Statics # pragma endregion Statics
# pragma region Member Functions # pragma region Member Functions
inline
Code body() Code body()
{ {
if ( ast == nullptr ) if ( ast == nullptr )
@ -622,13 +607,11 @@ namespace gen
return { ast->body() }; return { ast->body() };
} }
inline
String to_string() const String to_string() const
{ {
return ast->to_string(); return ast->to_string();
} }
inline
void set_global() void set_global()
{ {
if ( ast == nullptr ) if ( ast == nullptr )
@ -640,45 +623,34 @@ namespace gen
ast->Parent = Global.ast; ast->Parent = Global.ast;
} }
inline bool is_valid()
operator bool() const
{ {
return ast; // Originally intended to use operator bool(), however for some reason
// The C++ standard has operator Type*() with higher precedence than operator bool().
// Even when directly casting to bool. Amazing.
return ast != nullptr && ast->Type != ECode::Invalid;
}
operator bool() const
{
return ast != nullptr && ast->Type != ECode::Invalid;
} }
inline
bool operator ==( Code other ) bool operator ==( Code other )
{ {
return ast == other.ast; return ast == other.ast;
} }
inline
bool operator !=( Code other ) bool operator !=( Code other )
{ {
return ast != other.ast; return ast != other.ast;
} }
inline
operator AST*() operator AST*()
{ {
return ast; return ast;
} }
inline
Code& operator=( Code other )
{
if ( other.ast == nullptr )
{
log_failure("Attempted to assign a nullptr!");
return *this;
}
ast = other.ast;
return *this;
}
inline
AST* operator->() AST* operator->()
{ {
if ( ast == nullptr ) if ( ast == nullptr )
@ -732,12 +704,12 @@ namespace gen
// Set these before calling gen's init() procedure. // Set these before calling gen's init() procedure.
// Data // Data
void set_allocator_data_arrays ( AllocatorInfo data_array_allocator ); void set_allocator_data_arrays ( AllocatorInfo data_array_allocator );
void set_allocator_code_pool ( AllocatorInfo pool_allocator ); void set_allocator_code_pool ( AllocatorInfo pool_allocator );
void set_allocator_code_enries_arena( AllocatorInfo pool_allocator ); void set_allocator_code_enrties_arena( AllocatorInfo pool_allocator );
void set_allocator_string_arena ( AllocatorInfo string_allocator ); void set_allocator_string_arena ( AllocatorInfo string_allocator );
void set_allocator_string_table ( AllocatorInfo string_allocator ); void set_allocator_string_table ( AllocatorInfo string_allocator );
void set_allocator_type_table ( AllocatorInfo type_reg_allocator ); void set_allocator_type_table ( AllocatorInfo type_reg_allocator );
# pragma region Upfront # pragma region Upfront
Code def_attributes( StrC content ); Code def_attributes( StrC content );
@ -772,6 +744,8 @@ namespace gen
, Code specifiers = NoCode, Code attributes = NoCode , Code specifiers = NoCode, Code attributes = NoCode
, ModuleFlag mflags = ModuleFlag::None ); , ModuleFlag mflags = ModuleFlag::None );
Code def_operator_cast( Code type, Code body = NoCode );
Code def_param ( Code type, StrC name, Code value = NoCode ); Code def_param ( Code type, StrC name, Code value = NoCode );
Code def_specifier( SpecifierT specifier ); Code def_specifier( SpecifierT specifier );
@ -824,22 +798,23 @@ namespace gen
# pragma region Parsing # pragma region Parsing
# ifdef GEN_FEATURE_PARSING # ifdef GEN_FEATURE_PARSING
Code parse_class ( StrC class_def ); Code parse_class ( StrC class_def );
Code parse_enum ( StrC enum_def ); Code parse_enum ( StrC enum_def );
Code parse_export_body ( StrC export_def ); Code parse_export_body ( StrC export_def );
Code parse_extern_link ( StrC exten_link_def); Code parse_extern_link ( StrC exten_link_def);
Code parse_friend ( StrC friend_def ); Code parse_friend ( StrC friend_def );
Code parse_function ( StrC fn_def ); Code parse_function ( StrC fn_def );
Code parse_global_body ( StrC body_def ); Code parse_global_body ( StrC body_def );
Code parse_namespace ( StrC namespace_def ); Code parse_namespace ( StrC namespace_def );
Code parse_operator ( StrC operator_def ); Code parse_operator ( StrC operator_def );
Code parse_struct ( StrC struct_def ); Code parse_operator_cast( StrC operator_def );
Code parse_template ( StrC template_def ); Code parse_struct ( StrC struct_def );
Code parse_type ( StrC type_def ); Code parse_template ( StrC template_def );
Code parse_typedef ( StrC typedef_def ); Code parse_type ( StrC type_def );
Code parse_union ( StrC union_def ); Code parse_typedef ( StrC typedef_def );
Code parse_using ( StrC using_def ); Code parse_union ( StrC union_def );
Code parse_variable ( StrC var_def ); Code parse_using ( StrC using_def );
Code parse_variable ( StrC var_def );
# endif # endif
# pragma endregion Parsing # pragma endregion Parsing
@ -994,10 +969,10 @@ namespace gen
// Convienence for defining any name used with the gen api. // Convienence for defining any name used with the gen api.
// Lets you provide the length and string literal to the functions without the need for the DSL. // Lets you provide the length and string literal to the functions without the need for the DSL.
# define name( Id_ ) { txt_n_len( Id_ ) } # define name( Id_ ) { txt_to_StrC( Id_ ) }
// Same as name just used to indicate intention of literal for code instead of names. // Same as name just used to indicate intention of literal for code instead of names.
# define code( ... ) { txt_n_len( __VA_ARGS__ ) } # define code( ... ) { txt_to_StrC( __VA_ARGS__ ) }
#pragma endregion Macros #pragma endregion Macros
#pragma region Constants #pragma region Constants

View File

@ -0,0 +1,28 @@
#if gen_time
// This undefines the macros used by the gen library but are not necessary for the user.
// TODO : This is incomplete until all dependencies are brough in from ZPL into bloat.
#undef bit
#undef bitfield_is_equal
#undef ccast
#undef scast
#undef rcast
#undef pcast
#undef do_once
#undef do_once_start
#undef do_once_end
#undef kilobytes
#undef megabytes
#undef gigabytes
#undef terabytes
#undef global
#undef internal
#undef local_persist
#undef forceinline
#undef txt
// gen_time
#endif

230
scripts/gencpp.refactor Normal file
View File

@ -0,0 +1,230 @@
__VERSION 1
// This is a example template to be used with the refactor program
// Use it to refactor the naming convention of this library to your own.
// Can be used as an aid to help use use your project's implementation if it fullfills the dependencies of this project.
// Example: Most likely have a memory and string library already, just rename the functions and make sure the args are the same.
// Program: https://github.com/Ed94/refactor
// NOTE: Due to the current limitations of the program, not every symbol in the library can be renamed.
// This is due to the program not actually parsing C/C++.
// not : Ignore
// include : #includes
// word : Alphanumeric or underscore
// namespace : Prefix search and replace (c-namspaces).
// regex : Unavailable in __VERSION 1.
// Precedence (highest to lowest):
// word, namespace, regex
// Basic Types
// word b32 new_name
// word s8 new_name
// word s64 new_name
// word u8 new_name
// word u32 new_name
// word u64 new_name
// word uw new_name
// word sw new_name
// word sptr new_name
// word uptr new_name
// ZPL Derived
// namespace EAllocator_ new_namespace_
// namespace EFileMode_ new_namespace_
// namespace EFileError_ new_namespace_
// word Arena new_name
// word AllocatorInfo new_name
// word ArrayHeader new_name
// word FileInfo new_name
// word FileError new_name
// word Pool new_name
// word Array new_name
// word HashTable new_name
// word alloc new_name
// word alloc_align new_name
// word assert_crash new_name
// word char_first_occurence new_name
// word char_is_alpha new_name
// word char_is_alphanumeric new_name
// word char_is_digit new_name
// word char_is_hex_digit new_name
// word char_is_space new_name
// word crc32 new_name
// word free_all new_name
// word mem_copy new_name
// word mem_move new_name
// word mem_set new_name
// word process_exit new_name
// word str_compare new_name
// word str_copy new_name
// word str_fmt_buf new_name
// word str_fmt_va new_name
// word str_fmt_out_va new_name
// word str_fmt_out_err_va new_name
// word str_len new_name
// generic API
// word to_str new_name
// word to_type new_name
// gencpp Types & Constants
// word LogFailType new_name
// word log_failure new_name
// word ECode new_name
// word EnumT new_name
// word EnumClass new_name
// word EnumRegular new_name
// word UsingT new_name
// word UsingRegular new_name
// word UsingNamespace new_name
// word EOperator new_name
// word OperatorT new_name
// word ESpecifier new_name
// word SpecifierT new_name
// word AccessSpec new_name
// word ModuleFlag new_name
// word API_Export new_name
// word API_Import new_name
// word Keyword new_name
// gencpp Data
// word StringTable new_name
// word StringCached new_name
// word AST_POD_Size new_name
// word AST new_name
// word AST_POD new_name
// word Code new_name
// word Code_POD new_name
// word NoCode new_name
// gencpp API
// word init new_name
// word deinit new_name
// word get_cached_string new_name
// word make_code new_name
// word make_code_entries new_name
// word set_allocator_data_arrays new_name
// word set_allocator_code_pool new_name
// word set_allocator_code_entries_arena new_name
// word set_allocator_string_arena new_name
// word set_allocator_string_table new_name
// word set_allocator_type_table new_name
// upfront constructor namespace
// namespace def_ new_namespace_
// upfront constructor individual
// word def_attributes new_name
// word def_comment new_name
// word def_class new_name
// word def_enum new_name
// word def_execution new_name
// word def_extern_link new_name
// word def_friend new_name
// word def_function new_name
// word def_include new_name
// word def_module new_name
// word def_namespace new_name
// word def_operator new_name
// word def_param new_name
// word def_specifier new_name
// word def_struct new_name
// word def_template new_name
// word def_type new_name
// word def_typedef new_name
// word def_union new_name
// word def_using new_name
// word def_using_namespace new_name
// word def_variable new_name
// word def_class_body new_name
// word def_class_body new_name
// word def_enum_body new_name
// word def_enum_body new_name
// word def_export_body new_name
// word def_export_body new_name
// word def_extern_link_body new_name
// word def_extern_link_body new_name
// word def_function_body new_name
// word def_function_body new_name
// word def_global_body new_name
// word def_global_body new_name
// word def_namespace_body new_name
// word def_namespace_body new_name
// word def_params new_name
// word def_params new_name
// word def_specifiers new_name
// word def_specifiers new_name
// word def_struct_body new_name
// word def_struct_body new_name
// word def_union_body new_name
// word def_union_body new_name
// parse constructor namespace
// namespace parse_ new_namespace_
// parse constructor individual
// word parse_class new_name
// word parse_enum new_name
// word parse_export_body new_name
// word parse_extern_link new_name
// word parse_friend new_name
// word parse_function new_name
// word parse_global_body new_name
// word parse_namespace new_name
// word parse_operator new_name
// word parse_struct new_name
// word parse_template new_name
// word parse_type new_name
// word parse_typedef new_name
// word parse_union new_name
// word parse_using new_name
// word parse_variable new_name
// untyped constructor namespace
// namespace untyped_ new_namespace_
// untyped constructor individual
// word token_fmt_va new_name
// word token_fmt new_name
// word untyped_str new_name
// word untyped_fmt new_name
// word untyped_token_fmt new_name
// File Handling
// word Builder new_name
// word Editor new_name
// word Scanner new_name
// gencpp macros
// word __ new_name
// word code new_name
// word name new_name
// word txt new_name
// word txt_to_StrC new_name
// Type AST namespace
// namespace t_ new_namespace_
// Specifier AST namespace
// namespace spec_ new_namespace_

View File

@ -258,12 +258,9 @@ Code gen__array( StrC type, sw type_size )
set_capacity = def_function( name(set_capacity), def_param( t_uw, name(new_capacity)), t_bool, body ); set_capacity = def_function( name(set_capacity), def_param( t_uw, name(new_capacity)), t_bool, body );
} }
Code op_ptr = untyped_str( code( Code op_ptr = def_operator_cast( t_type_ptr, def_execution( code(
operator Type*() return Data;
{ )));
return Data;
}
));
Code body = def_struct_body( 20 Code body = def_struct_body( 20
, using_header , using_header
@ -327,7 +324,7 @@ void gen__array_request( StrC type, sw size, StrC dep = {} )
GenArrayRequest request = { dep, type, size }; GenArrayRequest request = { dep, type, size };
array_append( GenArrayRequests, request ); array_append( GenArrayRequests, request );
} }
#define gen_array( type ) gen__array_request( { txt_n_len(type) }, sizeof(type) ) #define gen_array( type ) gen__array_request( { txt_to_StrC(type) }, sizeof(type) )
u32 gen_array_file() u32 gen_array_file()
{ {

View File

@ -172,12 +172,9 @@ Code gen__buffer( StrC type, sw type_size )
)) ))
); );
Code op_type_ptr = untyped_str( code( Code op_type_ptr = def_operator_cast( t_type_ptr, def_execution( code(
operator Type*() return Data;
{ )));
return Data;
}
));
buffer = def_struct( name, def_struct_body( 14 buffer = def_struct( name, def_struct_body( 14
, using_header , using_header
@ -232,7 +229,7 @@ void gen__buffer_request( StrC type, sw size, StrC dep = {} )
GenBufferRequest request = { dep, type, size}; GenBufferRequest request = { dep, type, size};
array_append( GenBufferRequests, request ); array_append( GenBufferRequests, request );
} }
#define gen_buffer( type ) gen__buffer_request( { txt_n_len(type) }, sizeof( type )) #define gen_buffer( type ) gen__buffer_request( { txt_to_StrC(type) }, sizeof( type ))
u32 gen_buffer_file() u32 gen_buffer_file()
{ {

View File

@ -425,7 +425,7 @@ void gen__hashtable_request( StrC type, sw size, StrC dep = {} )
GenHashTableRequest request = { dep, type, size}; GenHashTableRequest request = { dep, type, size};
array_append( GenHashTableRequests, request ); array_append( GenHashTableRequests, request );
} }
#define gen_hashtable( type ) gen__hashtable_request( { txt_n_len(type) }, sizeof( type )) #define gen_hashtable( type ) gen__hashtable_request( { txt_to_StrC(type) }, sizeof( type ))
u32 gen_hashtable_file() u32 gen_hashtable_file()
{ {

View File

@ -188,7 +188,7 @@ void gen__ring_request( StrC type, sw size, StrC dep = {} )
GenRingRequest request = { dep, type, size}; GenRingRequest request = { dep, type, size};
array_append( GenRingRequests, request ); array_append( GenRingRequests, request );
} }
#define gen_ring( type ) gen__ring_request( { txt_n_len(type) }, sizeof( type )) #define gen_ring( type ) gen__ring_request( { txt_to_StrC(type) }, sizeof( type ))
u32 gen_ring_file() u32 gen_ring_file()
{ {

View File

@ -190,6 +190,19 @@ u32 gen_sanity()
gen_sanity_file.print_fmt("\n"); gen_sanity_file.print_fmt("\n");
// Operator cast
{
Code t_u8_ptr = def_type( name(u8), __, spec_ptr );
Code op_ptr = def_operator_cast( t_u8_ptr, __ );
Code op_class = def_class( name(TestOperatorCast), def_class_body( 1, op_ptr ) );
gen_sanity_file.print(op_class);
}
gen_sanity_file.print_fmt("\n");
// Parameters // Parameters
{ {
Code fwd; Code fwd;
@ -295,6 +308,21 @@ u32 gen_sanity()
gen_sanity_file.print_fmt("\n"); gen_sanity_file.print_fmt("\n");
// Template
{
Code t_Type = def_type( name(Type) );
Code tmpl = def_template( def_param( t_class, name(Type) )
, def_function( name(test_template), def_param( t_Type, name(a) ), __
, def_function_body(1, def_comment( StrC::from("Empty template function body")))
)
);
gen_sanity_file.print(tmpl);
}
gen_sanity_file.print_fmt("\n");
gen_sanity_file.print( def_comment( StrC::from( gen_sanity_file.print( def_comment( StrC::from(
"End of base case tests.\n" "End of base case tests.\n"
))); )));

View File

@ -251,7 +251,7 @@ void gen__array_request( StrC type, sw size, StrC dep = {} )
GenArrayRequest request = { dep, type, size }; GenArrayRequest request = { dep, type, size };
array_append( GenArrayRequests, request ); array_append( GenArrayRequests, request );
} }
#define gen_array( type ) gen__array_request( { txt_n_len(type) }, sizeof(type) ) #define gen_array( type ) gen__array_request( { txt_to_StrC(type) }, sizeof(type) )
u32 gen_array_file() u32 gen_array_file()
{ {

View File

@ -142,7 +142,6 @@ u32 gen_sanity()
gen_sanity_file.print_fmt("\n"); gen_sanity_file.print_fmt("\n");
// Operator // Operator
if (0)
{ {
Code bitflagtest = parse_enum( code( Code bitflagtest = parse_enum( code(
enum class EBitFlagTest : u8 enum class EBitFlagTest : u8
@ -171,6 +170,25 @@ u32 gen_sanity()
gen_sanity_file.print_fmt("\n"); gen_sanity_file.print_fmt("\n");
// Operator cast
{
Code op_ptr = parse_operator_cast( code(
operator u8* ();
));
Code class_def = parse_class( code(
class TestClass
{
};
));
class_def.body()->add_entry( op_ptr );
gen_sanity_file.print(class_def);
}
gen_sanity_file.print_fmt("\n");
// Parameters // Parameters
{ {
Code fwd = parse_function( code( Code fwd = parse_function( code(
@ -209,7 +227,6 @@ u32 gen_sanity()
gen_sanity_file.print_fmt("\n"); gen_sanity_file.print_fmt("\n");
// Struct // Struct
if (0)
{ {
Code fwd = parse_struct( code( Code fwd = parse_struct( code(
struct TestEmptyStruct; struct TestEmptyStruct;
@ -229,7 +246,6 @@ u32 gen_sanity()
gen_sanity_file.print_fmt("\n"); gen_sanity_file.print_fmt("\n");
// Union // Union
if (0)
{ {
Code empty = parse_union( code( Code empty = parse_union( code(
union TestEmptyUnion union TestEmptyUnion
@ -239,10 +255,13 @@ u32 gen_sanity()
empty.body()->add_entry( def_comment( StrC::from("Empty union body") ) ); empty.body()->add_entry( def_comment( StrC::from("Empty union body") ) );
gen_sanity_file.print( parse_typedef( code( typedef unsigned short u16; )) );
gen_sanity_file.print( parse_typedef( code( typedef unsigned long u32; )) );
Code def = parse_union( code( Code def = parse_union( code(
union TestUnion union TestUnion
{ {
u8 a; u8 a;
u16 b; u16 b;
u32 c; u32 c;
}; };
@ -255,28 +274,30 @@ u32 gen_sanity()
gen_sanity_file.print_fmt("\n"); gen_sanity_file.print_fmt("\n");
// Using // Using
if (0)
{ {
Code reg = parse_using( code( Code reg = parse_using( code(
using TestUsing = u8; using TestUsing = u8;
)); ));
Code nspace = parse_using( code( Code nspace = parse_namespace( code(
namespace TestNamespace namespace TestNamespace
{ {
}; };
using namespace TestUsing; ));
Code npspace_using = parse_using( code(
using namespace TestNamespace;
)); ));
gen_sanity_file.print(reg); gen_sanity_file.print(reg);
gen_sanity_file.print(nspace); gen_sanity_file.print(nspace);
gen_sanity_file.print(npspace_using);
} }
gen_sanity_file.print_fmt("\n"); gen_sanity_file.print_fmt("\n");
// Variable // Variable
if (0)
{ {
Code bss = parse_variable( code( Code bss = parse_variable( code(
u8 test_variable; u8 test_variable;
@ -285,6 +306,26 @@ u32 gen_sanity()
Code data = parse_variable( code( Code data = parse_variable( code(
u8 test_variable = 0x12; u8 test_variable = 0x12;
)); ));
gen_sanity_file.print(bss);
gen_sanity_file.print(data);
}
gen_sanity_file.print_fmt("\n");
// template
{
#pragma push_macro("template")
#undef template
Code tmpl = parse_template( code(
template< typename Type >
void test_template( Type a )
{
}
));
#pragma pop_macro("template")
gen_sanity_file.print(tmpl);
} }
gen_sanity_file.print_fmt("\n"); gen_sanity_file.print_fmt("\n");

View File

@ -17,7 +17,7 @@ int gen_main()
Memory::setup(); Memory::setup();
gen::init(); gen::init();
// gen_sanity(); gen_sanity();
gen_array( u8 ); gen_array( u8 );
// gen_array( sw ); // gen_array( sw );

102
thirdparty/zpl.h vendored
View File

@ -3141,10 +3141,10 @@ ZPL_BEGIN_C_DECLS
typedef enum AllocType typedef enum AllocType
{ {
EAllocationALLOC, EAllocation_ALLOC,
EAllocationFREE, EAllocation_FREE,
EAllocationFREE_ALL, EAllocation_FREE_ALL,
EAllocationRESIZE, EAllocation_RESIZE,
} AllocType; } AllocType;
// NOTE: This is useful so you can define an allocator of the same type and parameters // NOTE: This is useful so you can define an allocator of the same type and parameters
@ -3392,7 +3392,7 @@ ZPL_DEF ZPL_ALLOCATOR_PROC( stack_allocator_proc );
ZPL_IMPL_INLINE void* alloc_align( AllocatorInfo a, sw size, sw alignment ) ZPL_IMPL_INLINE void* alloc_align( AllocatorInfo a, sw size, sw alignment )
{ {
return a.proc( a.data, EAllocationALLOC, size, alignment, NULL, 0, ZPL_DEFAULT_ALLOCATOR_FLAGS ); return a.proc( a.data, EAllocation_ALLOC, size, alignment, NULL, 0, ZPL_DEFAULT_ALLOCATOR_FLAGS );
} }
ZPL_IMPL_INLINE void* alloc( AllocatorInfo a, sw size ) ZPL_IMPL_INLINE void* alloc( AllocatorInfo a, sw size )
@ -3403,12 +3403,12 @@ ZPL_IMPL_INLINE void* alloc( AllocatorInfo a, sw size )
ZPL_IMPL_INLINE void free( AllocatorInfo a, void* ptr ) ZPL_IMPL_INLINE void free( AllocatorInfo a, void* ptr )
{ {
if ( ptr != NULL ) if ( ptr != NULL )
a.proc( a.data, EAllocationFREE, 0, 0, ptr, 0, ZPL_DEFAULT_ALLOCATOR_FLAGS ); a.proc( a.data, EAllocation_FREE, 0, 0, ptr, 0, ZPL_DEFAULT_ALLOCATOR_FLAGS );
} }
ZPL_IMPL_INLINE void free_all( AllocatorInfo a ) ZPL_IMPL_INLINE void free_all( AllocatorInfo a )
{ {
a.proc( a.data, EAllocationFREE_ALL, 0, 0, NULL, 0, ZPL_DEFAULT_ALLOCATOR_FLAGS ); a.proc( a.data, EAllocation_FREE_ALL, 0, 0, NULL, 0, ZPL_DEFAULT_ALLOCATOR_FLAGS );
} }
ZPL_IMPL_INLINE void* resize( AllocatorInfo a, void* ptr, sw old_size, sw new_size ) ZPL_IMPL_INLINE void* resize( AllocatorInfo a, void* ptr, sw old_size, sw new_size )
@ -3418,7 +3418,7 @@ ZPL_IMPL_INLINE void* resize( AllocatorInfo a, void* ptr, sw old_size, sw new_si
ZPL_IMPL_INLINE void* resize_align( AllocatorInfo a, void* ptr, sw old_size, sw new_size, sw alignment ) ZPL_IMPL_INLINE void* resize_align( AllocatorInfo a, void* ptr, sw old_size, sw new_size, sw alignment )
{ {
return a.proc( a.data, EAllocationRESIZE, new_size, alignment, ptr, old_size, ZPL_DEFAULT_ALLOCATOR_FLAGS ); return a.proc( a.data, EAllocation_RESIZE, new_size, alignment, ptr, old_size, ZPL_DEFAULT_ALLOCATOR_FLAGS );
} }
ZPL_IMPL_INLINE void* alloc_copy( AllocatorInfo a, void const* src, sw size ) ZPL_IMPL_INLINE void* alloc_copy( AllocatorInfo a, void const* src, sw size )
@ -4025,12 +4025,12 @@ typedef struct BufferHeader
buffer_count( x ) += ( item_count ); \ buffer_count( x ) += ( item_count ); \
} while ( 0 ) } while ( 0 )
#define buffer_copy_init( y, x ) \ #define buffer_copy_init( y, x ) \
do \ do \
{ \ { \
buffer_init( y, buffer_allocator( x ), buffer_capacity( x ) ); \ ZPL_NS( buffer_init )( y, ZPL_NS( buffer_allocator )( x ), buffer_capacity( x ) ); \
ZPL_NS( mem_copy )( y, x, buffer_capacity( x ) * size_of( *x ) ); \ ZPL_NS( mem_copy )( y, x, buffer_capacity( x ) * size_of( *x ) ); \
buffer_count( y ) = buffer_count( x ); \ buffer_count( y ) = buffer_count( x ); \
} while ( 0 ) } while ( 0 )
#define buffer_pop( x ) \ #define buffer_pop( x ) \
@ -8324,10 +8324,10 @@ ZPL_BEGIN_C_DECLS
typedef enum CSV_Error typedef enum CSV_Error
{ {
ECSV_Error_NONE, ECSV_Error__NONE,
ECSV_Error_INTERNAL, ECSV_Error__INTERNAL,
ECSV_Error_UNEXPECTED_END_OF_INPUT, ECSV_Error__UNEXPECTED_END_OF_INPUT,
ECSV_Error_MISMATCHED_ROWS, ECSV_Error__MISMATCHED_ROWS,
} CSV_Error; } CSV_Error;
typedef ADT_Node CSV_Object; typedef ADT_Node CSV_Object;
@ -9302,7 +9302,7 @@ ZPL_ALLOCATOR_PROC( heap_allocator_proc )
sw track_size = max( alloc_info_size, alignment ) + alloc_info_remainder; sw track_size = max( alloc_info_size, alignment ) + alloc_info_remainder;
switch ( type ) switch ( type )
{ {
case EAllocationFREE : case EAllocation_FREE :
{ {
if ( ! old_memory ) if ( ! old_memory )
break; break;
@ -9312,7 +9312,7 @@ ZPL_ALLOCATOR_PROC( heap_allocator_proc )
old_memory = alloc_info->physical_start; old_memory = alloc_info->physical_start;
} }
break; break;
case EAllocationALLOC : case EAllocation_ALLOC :
{ {
size += track_size; size += track_size;
} }
@ -9325,15 +9325,15 @@ ZPL_ALLOCATOR_PROC( heap_allocator_proc )
switch ( type ) switch ( type )
{ {
#if defined( ZPL_COMPILER_MSVC ) || ( defined( ZPL_COMPILER_GCC ) && defined( ZPL_SYSTEM_WINDOWS ) ) || ( defined( ZPL_COMPILER_TINYC ) && defined( ZPL_SYSTEM_WINDOWS ) ) #if defined( ZPL_COMPILER_MSVC ) || ( defined( ZPL_COMPILER_GCC ) && defined( ZPL_SYSTEM_WINDOWS ) ) || ( defined( ZPL_COMPILER_TINYC ) && defined( ZPL_SYSTEM_WINDOWS ) )
case EAllocationALLOC : case EAllocation_ALLOC :
ptr = _aligned_malloc( size, alignment ); ptr = _aligned_malloc( size, alignment );
if ( flags & ZPL_ALLOCATOR_FLAG_CLEAR_TO_ZERO ) if ( flags & ZPL_ALLOCATOR_FLAG_CLEAR_TO_ZERO )
zero_size( ptr, size ); zero_size( ptr, size );
break; break;
case EAllocationFREE : case EAllocation_FREE :
_aligned_free( old_memory ); _aligned_free( old_memory );
break; break;
case EAllocationRESIZE : case EAllocation_RESIZE :
{ {
AllocatorInfo a = heap_allocator(); AllocatorInfo a = heap_allocator();
ptr = default_resize_align( a, old_memory, old_size, size, alignment ); ptr = default_resize_align( a, old_memory, old_size, size, alignment );
@ -9341,7 +9341,7 @@ ZPL_ALLOCATOR_PROC( heap_allocator_proc )
break; break;
#elif defined( ZPL_SYSTEM_LINUX ) && ! defined( ZPL_CPU_ARM ) && ! defined( ZPL_COMPILER_TINYC ) #elif defined( ZPL_SYSTEM_LINUX ) && ! defined( ZPL_CPU_ARM ) && ! defined( ZPL_COMPILER_TINYC )
case EAllocationALLOC : case EAllocation_ALLOC :
{ {
ptr = aligned_alloc( alignment, ( size + alignment - 1 ) & ~( alignment - 1 ) ); ptr = aligned_alloc( alignment, ( size + alignment - 1 ) & ~( alignment - 1 ) );
@ -9352,20 +9352,20 @@ ZPL_ALLOCATOR_PROC( heap_allocator_proc )
} }
break; break;
case EAllocationFREE : case EAllocation_FREE :
{ {
free( old_memory ); free( old_memory );
} }
break; break;
case EAllocationRESIZE : case EAllocation_RESIZE :
{ {
AllocatorInfo a = heap_allocator(); AllocatorInfo a = heap_allocator();
ptr = default_resize_align( a, old_memory, old_size, size, alignment ); ptr = default_resize_align( a, old_memory, old_size, size, alignment );
} }
break; break;
#else #else
case EAllocationALLOC : case EAllocation_ALLOC :
{ {
posix_memalign( &ptr, alignment, size ); posix_memalign( &ptr, alignment, size );
@ -9376,13 +9376,13 @@ ZPL_ALLOCATOR_PROC( heap_allocator_proc )
} }
break; break;
case EAllocationFREE : case EAllocation_FREE :
{ {
free( old_memory ); free( old_memory );
} }
break; break;
case EAllocationRESIZE : case EAllocation_RESIZE :
{ {
AllocatorInfo a = heap_allocator(); AllocatorInfo a = heap_allocator();
ptr = default_resize_align( a, old_memory, old_size, size, alignment ); ptr = default_resize_align( a, old_memory, old_size, size, alignment );
@ -9390,12 +9390,12 @@ ZPL_ALLOCATOR_PROC( heap_allocator_proc )
break; break;
#endif #endif
case EAllocationFREE_ALL : case EAllocation_FREE_ALL :
break; break;
} }
#ifdef ZPL_HEAP_ANALYSIS #ifdef ZPL_HEAP_ANALYSIS
if ( type == EAllocationALLOC ) if ( type == EAllocation_ALLOC )
{ {
_heap_alloc_info* alloc_info = zpl_cast( _heap_alloc_info* )( zpl_cast( char* ) ptr + alloc_info_remainder ); _heap_alloc_info* alloc_info = zpl_cast( _heap_alloc_info* )( zpl_cast( char* ) ptr + alloc_info_remainder );
zero_item( alloc_info ); zero_item( alloc_info );
@ -9423,7 +9423,7 @@ ZPL_ALLOCATOR_PROC( arena_allocator_proc )
switch ( type ) switch ( type )
{ {
case EAllocationALLOC : case EAllocation_ALLOC :
{ {
void* end = pointer_add( arena->physical_start, arena->total_allocated ); void* end = pointer_add( arena->physical_start, arena->total_allocated );
sw total_size = align_forward_i64( size, alignment ); sw total_size = align_forward_i64( size, alignment );
@ -9442,16 +9442,16 @@ ZPL_ALLOCATOR_PROC( arena_allocator_proc )
} }
break; break;
case EAllocationFREE : case EAllocation_FREE :
// NOTE: Free all at once // NOTE: Free all at once
// Use Temp_Arena_Memory if you want to free a block // Use Temp_Arena_Memory if you want to free a block
break; break;
case EAllocationFREE_ALL : case EAllocation_FREE_ALL :
arena->total_allocated = 0; arena->total_allocated = 0;
break; break;
case EAllocationRESIZE : case EAllocation_RESIZE :
{ {
// TODO: Check if ptr is on top of stack and just extend // TODO: Check if ptr is on top of stack and just extend
AllocatorInfo a = arena_allocator( arena ); AllocatorInfo a = arena_allocator( arena );
@ -9509,7 +9509,7 @@ ZPL_ALLOCATOR_PROC( pool_allocator_proc )
switch ( type ) switch ( type )
{ {
case EAllocationALLOC : case EAllocation_ALLOC :
{ {
uptr next_free; uptr next_free;
ZPL_ASSERT( size == pool->block_size ); ZPL_ASSERT( size == pool->block_size );
@ -9525,7 +9525,7 @@ ZPL_ALLOCATOR_PROC( pool_allocator_proc )
} }
break; break;
case EAllocationFREE : case EAllocation_FREE :
{ {
uptr* next; uptr* next;
if ( old_memory == NULL ) if ( old_memory == NULL )
@ -9538,7 +9538,7 @@ ZPL_ALLOCATOR_PROC( pool_allocator_proc )
} }
break; break;
case EAllocationFREE_ALL : case EAllocation_FREE_ALL :
{ {
sw actual_block_size, block_index; sw actual_block_size, block_index;
void* curr; void* curr;
@ -9562,7 +9562,7 @@ ZPL_ALLOCATOR_PROC( pool_allocator_proc )
} }
break; break;
case EAllocationRESIZE : case EAllocation_RESIZE :
// NOTE: Cannot resize // NOTE: Cannot resize
ZPL_PANIC( "You cannot resize something allocated by with a pool." ); ZPL_PANIC( "You cannot resize something allocated by with a pool." );
break; break;
@ -9608,7 +9608,7 @@ ZPL_ALLOCATOR_PROC( scratch_allocator_proc )
switch ( type ) switch ( type )
{ {
case EAllocationALLOC : case EAllocation_ALLOC :
{ {
void* pt = s->alloc_point; void* pt = s->alloc_point;
allocation_header_ev* header = zpl_cast( allocation_header_ev* ) pt; allocation_header_ev* header = zpl_cast( allocation_header_ev* ) pt;
@ -9641,7 +9641,7 @@ ZPL_ALLOCATOR_PROC( scratch_allocator_proc )
} }
break; break;
case EAllocationFREE : case EAllocation_FREE :
{ {
if ( old_memory ) if ( old_memory )
{ {
@ -9672,12 +9672,12 @@ ZPL_ALLOCATOR_PROC( scratch_allocator_proc )
} }
break; break;
case EAllocationFREE_ALL : case EAllocation_FREE_ALL :
s->alloc_point = s->physical_start; s->alloc_point = s->physical_start;
s->free_point = s->physical_start; s->free_point = s->physical_start;
break; break;
case EAllocationRESIZE : case EAllocation_RESIZE :
ptr = default_resize_align( scratch_allocator( s ), old_memory, old_size, size, alignment ); ptr = default_resize_align( scratch_allocator( s ), old_memory, old_size, size, alignment );
break; break;
} }
@ -9698,7 +9698,7 @@ ZPL_ALLOCATOR_PROC( stack_allocator_proc )
switch ( type ) switch ( type )
{ {
case EAllocationALLOC : case EAllocation_ALLOC :
{ {
size += ZPL_STACK_ALLOC_OFFSET; size += ZPL_STACK_ALLOC_OFFSET;
u64 alloc_offset = s->allocated; u64 alloc_offset = s->allocated;
@ -9729,7 +9729,7 @@ ZPL_ALLOCATOR_PROC( stack_allocator_proc )
} }
break; break;
case EAllocationFREE : case EAllocation_FREE :
{ {
if ( old_memory ) if ( old_memory )
{ {
@ -9742,13 +9742,13 @@ ZPL_ALLOCATOR_PROC( stack_allocator_proc )
} }
break; break;
case EAllocationFREE_ALL : case EAllocation_FREE_ALL :
{ {
s->allocated = 0; s->allocated = 0;
} }
break; break;
case EAllocationRESIZE : case EAllocation_RESIZE :
{ {
ZPL_PANIC( "You cannot resize something allocated by a stack." ); ZPL_PANIC( "You cannot resize something allocated by a stack." );
} }
@ -21338,7 +21338,7 @@ ZPL_BEGIN_C_DECLS
u8 csv_parse_delimiter( CSV_Object* root, char* text, AllocatorInfo allocator, b32 has_header, char delim ) u8 csv_parse_delimiter( CSV_Object* root, char* text, AllocatorInfo allocator, b32 has_header, char delim )
{ {
CSV_Error err = ECSV_Error_NONE; CSV_Error err = ECSV_Error__NONE;
ZPL_ASSERT_NOT_NULL( root ); ZPL_ASSERT_NOT_NULL( root );
ZPL_ASSERT_NOT_NULL( text ); ZPL_ASSERT_NOT_NULL( text );
zero_item( root ); zero_item( root );
@ -21379,7 +21379,7 @@ u8 csv_parse_delimiter( CSV_Object* root, char* text, AllocatorInfo allocator, b
if ( *e == 0 ) if ( *e == 0 )
{ {
ZPL_CSV_ASSERT( "unmatched quoted string" ); ZPL_CSV_ASSERT( "unmatched quoted string" );
err = ECSV_Error_UNEXPECTED_END_OF_INPUT; err = ECSV_Error__UNEXPECTED_END_OF_INPUT;
return err; return err;
} }
*e = 0; *e = 0;
@ -21467,7 +21467,7 @@ u8 csv_parse_delimiter( CSV_Object* root, char* text, AllocatorInfo allocator, b
else if ( total_colc != colc ) else if ( total_colc != colc )
{ {
ZPL_CSV_ASSERT( "mismatched rows" ); ZPL_CSV_ASSERT( "mismatched rows" );
err = ECSV_Error_MISMATCHED_ROWS; err = ECSV_Error__MISMATCHED_ROWS;
return err; return err;
} }
colc = 0; colc = 0;
@ -21479,7 +21479,7 @@ u8 csv_parse_delimiter( CSV_Object* root, char* text, AllocatorInfo allocator, b
if ( array_count( root->nodes ) == 0 ) if ( array_count( root->nodes ) == 0 )
{ {
ZPL_CSV_ASSERT( "unexpected end of input. stream is empty." ); ZPL_CSV_ASSERT( "unexpected end of input. stream is empty." );
err = ECSV_Error_UNEXPECTED_END_OF_INPUT; err = ECSV_Error__UNEXPECTED_END_OF_INPUT;
return err; return err;
} }