Setup testing and library for getting the parse api done.

This commit is contained in:
2023-07-08 14:11:41 -04:00
parent a0250d1109
commit b360cf3024
8 changed files with 358 additions and 198 deletions

View File

@ -7,206 +7,251 @@ using namespace gen;
Code gen__array_base()
{
Code array_base = parse_struct( code(
struct ArrayBase
return parse_global_body( code(
struct ArrayHeader
{
struct Header
{
AllocatorInfo Allocator;
uw Capacity;
uw Num;
};
static inline
sw grow_formula( sw value )
{
return 2 * value * 8;
}
AllocatorInfo Allocator;
uw Capacity;
uw Num;
};
));
return array_base;
static inline uw array_grow_formula( uw value )
{
return 2 * value * 8;
}
));
}
Code gen__array( s32 length, char const* type_str, sw type_size )
Code gen__array( StrC type, sw type_size )
{
StrC tmpl = code(
struct Array_{type} : ArrayBase
{
using Type = {type};
StrC name;
{
char const* name_str = str_fmt_buf( "Array_%s\0", type.Ptr );
s32 name_len = str_len( name_str );
Type* Data;
name = { name_len, name_str };
};
static Array_{type} init( AllocatorInfo allocator )
Code array = parse_struct( token_fmt(
txt(
struct <ArrayType>
{
return init_reserve( allocator, grow_formula(0) );
}
using Header = ArrayHeader;
using Type = <type>;
static Array_{type} init_reserve( AllocatorInfo allocator, uw capacity )
{
Header* header = rcast( Header*, alloc( allocator, sizeof(Header) + sizeof(Type) ));
constexpr auto grow_formula = &array_glow_formula;
if ( header == nullptr )
return false;
header->Allocator = allocator;
header->Capacity = capacity;
header->Num = 0;
Array_{type} array;
array.Data = rcast( Type*, header + 1 );
return array;
}
bool append( Type const& value )
{
Header& header = get_header();
if ( header.Num == header.Capacity )
static
<ArrayType> init( AllocatorInfo allocator )
{
if ( ! grow( header.Allocator ))
return false;
return init_reserve( allocator, grow_formula(0) );
}
data[ header.Num ] = value;
header.Num++;
static
<ArrayType> init_reserve( AllocatorInfo allocator, sw capacity )
{
Header* header = rcast( Header*, alloc( allocator, sizeof(Header) + sizeof(Type) ));
return true;
}
if ( header == nullptr )
return { nullptr };
Type& back()
{
Header& header = get_header();
return data[ header.Num - 1 ];
}
header->Allocator = allocator;
header->Capacity = capacity;
header->Num = 0;
void clear()
{
Header& header = get_header();
header.Num = 0;
}
return { rcast( Type*, header + 1) };
}
bool fill( uw begin, uw end, Type const& value )
{
Header& header = get_header();
bool append( Type value )
{
Header& header = get_header();
if ( begin < 0 || end >= header.Num )
return false;
if ( header.Num == header.Capacity )
{
if ( ! grow( header.Capacity ))
return false;
}
for ( uw idx = begin; idx < end; idx++ )
data[ idx ] = value;
Data[ header.Num ] = value;
header.Num++;
return true;
}
return true;
}
void free()
{
Header& header = get_header();
::free( header.Allocator, & header );
}
inline Type& back( void )
{
Header& header = get_header();
return Data[ header.Num - 1 ];
}
Header& get_header()
{
return pcast( Header, Data - 1 );
}
inline void clear( void )
{
Header& header = get_header();
header.Num = 0;
}
bool grow( uw min_capacity )
{
Header& header = get_header();
bool fill( uw begin, uw end, Type value )
{
Header& header = get_header();
uw new_capacity = grow_formula( header.Capacity );
if ( new_capacity < min_capacity )
new_capacity = min_capacity;
return set_capacity( new_capacity );
}
void pop()
{
Header& header = get_header();
assert_crash( header.Num > 0 );
header.Num--;
}
bool reserve( uw new_capacity )
{
Header& header = get_header();
if ( header.Capacity < new_capacity )
return set_capacity( new_capacity );
return true;
}
bool resize( uw num )
{
Header& header = get_header();
if ( header.Capacity < num )
if ( ! grow( num ))
if ( begin < 0 || end >= header.Num )
return false;
header.Num = num;
return true;
}
for ( sw idx = begin; idx < end; idx++ )
{
Data[ idx ] = value;
}
bool set_capacity( uw new_capacity )
{
Header& header = get_header();
if ( new_capacity == header.Capacity )
return true;
}
if ( new_capacity < header.Num )
header.Num = new_capacity;
inline void free( void )
{
Header& header = get_header();
zpl::free( header.Allocator, &header );
}
sw size = sizeof(Header) + sizeof(Type) * new_capacity;
Header* new_header = rcast( Header*, alloc( header.Allocator, size ));
inline Header& get_header( void )
{
return *( reinterpret_cast< Header* >( Data ) - 1 );
}
if ( new_header == nullptr )
return false;
bool grow( uw min_capacity )
{
Header& header = get_header();
uw new_capacity = grow_formula( header.Capacity );
mem_move( new_header, & header, sizeof( Header ) + sizeof(Type) * header.Num );
if ( new_capacity < min_capacity )
new_capacity = 8;
new_header->Allocator = header.Allocator;
new_header->Num = header.Num;
new_header->Capacity = new_capacity;
return set_capacity( new_capacity );
}
::free( header );
inline uw num( void )
{
return get_header().Num;
}
*Data = new_header + 1;
inline bool pop( void )
{
Header& header = get_header();
return true;
}
ZPL_ASSERT( header.Num > 0 );
header.Num--;
}
inline void remove_at( uw idx )
{
Header* header = &get_header();
ZPL_ASSERT( idx < header->Num );
mem_move( header + idx, header + idx + 1, sizeof( Type ) * ( header->Num - idx - 1 ) );
header->Num--;
}
bool reserve( uw new_capacity )
{
Header& header = get_header();
if ( header.Capacity < new_capacity )
return set_capacity( new_capacity );
return true;
}
bool resize( uw num )
{
Header& header = get_header();
if ( num > header.Capacity )
{
if ( ! grow( header.Capacity ) )
return false;
}
header.Num = num;
return true;
}
bool set_capacity( uw new_capacity )
{
Header& header = get_header();
if ( new_capacity == header.Capacity )
return true;
if ( new_capacity < header.Num )
header.Num = new_capacity;
sw size = sizeof( Header ) + sizeof( Type ) * new_capacity;
Header* new_header = reinterpret_cast< Header* >( alloc( header.Allocator, size ) );
if ( new_header == nullptr )
return false;
mem_move( new_header, &header, sizeof( Header ) + sizeof( Type ) * header.Num );
new_header->Allocator = header.Allocator;
new_header->Num = header.Num;
new_header->Capacity = new_capacity;
zpl::free( header.Allocator, &header );
Data = ( Type* )new_header + 1;
return true;
}
Type* Data;
};
),
// Tokens
2
, "ArrayType", name
, "type", type
));
Code op_ptr = untyped_str( code(
operator Type*()
{
return Data;
}
);
));
array.body()->add_entry( op_ptr );
char const* gen_from_tmpl = token_fmt( tmpl.Ptr, 1, "type", type_str );
s32 gen_from_tmpl_len = str_len( gen_from_tmpl );
Code array = parse_struct( { gen_from_tmpl_len, gen_from_tmpl } );
return array;
}
struct GenArrayRequest
{
s32 TypeLength;
char const* Type;
sw Size;
s32 DependencyLength;
char const* Dependency;
StrC Dependency;
StrC Type;
sw Size;
};
Array(GenArrayRequest) GenArrayRequests;
void gen__array_request( s32 type_len, char const* type_str, sw type_size, s32 dep_len, char const* dep )
void gen__array_request( StrC type, sw size, StrC dep = {} )
{
GenArrayRequest request = { type_len, type_str, type_size, dep_len, dep };
do_once_start
array_init( GenArrayRequests, g_allocator );
do_once_end
// Make sure we don't already have a request for the type.
for ( sw idx = 0; idx < array_count( GenArrayRequests ); ++idx )
{
StrC const reqest_type = GenArrayRequests[ idx ].Type;
if ( reqest_type.Len != type.Len )
continue;
if ( str_compare( reqest_type.Ptr, type.Ptr, reqest_type.Len ) == 0 )
return;
}
GenArrayRequest request = { dep, type, size };
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_n_len(type) }, sizeof(type) )
u32 gen_array_file()
{
@ -214,6 +259,11 @@ u32 gen_array_file()
gen_array_file;
gen_array_file.open( "array.gen.hpp" );
Code include_zpl = def_include( StrC::from("Bloat.hpp") );
gen_array_file.print( include_zpl );
Code array_base = gen__array_base();
gen_array_file.print( array_base );
GenArrayRequest* current = GenArrayRequests;
s32 left = array_count( GenArrayRequests );
@ -221,7 +271,7 @@ u32 gen_array_file()
{
GenArrayRequest const& request = * current;
Code generated_array = gen__array( request.TypeLength, request.Type, request.Size );
Code generated_array = gen__array( request.Type, request.Size );
if ( request.Dependency )
{
@ -229,7 +279,7 @@ u32 gen_array_file()
s32 cmt_len = str_len( cmt_str );
Code cmt = def_comment( { cmt_len, cmt_str } );
Code include = def_include( { request.DependencyLength, request.Dependency } );
Code include = def_include( request.Dependency );
gen_array_file.print( cmt );
gen_array_file.print( include );

29
test/Parsed/Sanity.hpp Normal file
View File

@ -0,0 +1,29 @@
#pragma once
#ifdef gen_time
#include "gen.hpp"
using namespace gen;
void gen_sanity()
{
Builder
gen_sanity_file;
gen_sanity_file.open("./sanity.gen.hpp");
gen_sanity_file.print( def_comment( StrC::from(
"The following will show a series of base cases for the gen parsed api.\n"
)));
// Typedef
{
Code u8_typedef = parse_typedef( code(
typedef unsigned char u8;
));
gen_sanity_file.print(u8_typedef);
}
gen_sanity_file.write();
}
#endif