mirror of
https://github.com/Ed94/gencpp.git
synced 2025-06-14 18:51:47 -07:00
Major changes to library design, change test to reflect it.
This commit is contained in:
507
test/Array.hpp
507
test/Array.hpp
@ -17,6 +17,7 @@
|
||||
Code t_uw = def_type( txt(uw) );
|
||||
Code t_allocator = def_type( txt(allocator) );
|
||||
|
||||
#ifndef GEN_DEFINE_DSL
|
||||
Code header;
|
||||
{
|
||||
Code num = def_variable( "Num", t_uw );
|
||||
@ -30,20 +31,44 @@
|
||||
Code grow_formula;
|
||||
{
|
||||
Code spec = def_specifiers(1, Specifier::Inline);
|
||||
Code params = def_parameters(1, "value", t_uw );
|
||||
Code body = untyped_fmt( "\t""return 2 * value * 8;" );
|
||||
Code params = def_parameters(1, t_uw, "value" );
|
||||
Code body = untyped_str( "return 2 * value * 8;" );
|
||||
|
||||
grow_formula = def_function( "grow_formula", spec, params, t_sw, body );
|
||||
}
|
||||
|
||||
Code base_body = def_struct_body(2, header, grow_formula);
|
||||
Code base = def_struct( "ArrayBase", base_body );
|
||||
return base;
|
||||
Code body = def_struct_body(2, header, grow_formula);
|
||||
Code ArrayBase = def_struct( "ArrayBase", body );
|
||||
|
||||
#else
|
||||
def( ArrayBase )
|
||||
def ( Header )
|
||||
{
|
||||
var( Num, t_uw, __, __ );
|
||||
var( Capacity, t_uw, __, __);
|
||||
var( Allocator, t_allocator, __, __);
|
||||
|
||||
Code body = struct_body( Num, Capacity, Allocator );
|
||||
|
||||
struct( Header, __, __, body );
|
||||
}
|
||||
|
||||
def( grow_formula )
|
||||
{
|
||||
function( grow_formula, spec_inline, t_uw, params( t_uw, "value" ), untyped_str("return 2 * value * 8") );
|
||||
}
|
||||
|
||||
Code body = struct_body( Header, grow_formula );
|
||||
struct( ArrayBase, __, __, body );
|
||||
#endif
|
||||
|
||||
return ArrayBase;
|
||||
}
|
||||
|
||||
#define gen_array( Type_ ) gen__array( #Type_, sizeof(Type_), a_base )
|
||||
Code gen__array( char const* type_str, s32 type_size, Code parent )
|
||||
{
|
||||
#ifndef GEN_DEFINE_DSL
|
||||
// Make these global consts to be accessed anywhere...
|
||||
Code t_uw = def_type( txt(uw) );
|
||||
Code t_sw = def_type( txt(sw) );
|
||||
@ -51,16 +76,14 @@
|
||||
Code t_allocator = def_type( txt(allocator) );
|
||||
Code t_void = def_type( txt(void) );
|
||||
|
||||
Code v_nullptr = untyped_fmt( "nullptr" );
|
||||
Code v_nullptr = untyped_str( "nullptr" );
|
||||
|
||||
Code spec_ct = def_specifiers(1, Specifier::Constexpr );
|
||||
Code spec_inline = def_specifiers(1, Specifier::Inline );
|
||||
|
||||
Code type = def_type( type_str );
|
||||
Code ptr_type = def_type( string_sprintf_buf( g_allocator, "%s*", type_str ) );
|
||||
Code ref_type = def_type( string_sprintf_buf( g_allocator, "%s&", type_str ) );
|
||||
|
||||
string name = string_sprintf_buf( g_allocator, "Array_%s", type_str );
|
||||
Code ptr_type = def_type( bprintf( "%s*", type_str ) );
|
||||
Code ref_type = def_type( bprintf( "%s&", type_str ) );
|
||||
|
||||
// From ArrayBase
|
||||
Code t_header = def_type( "Header" );
|
||||
@ -69,43 +92,43 @@
|
||||
|
||||
Code array_def;
|
||||
{
|
||||
Code using_type = def_using( "type", type );
|
||||
Code using_type = def_using( "Type", type );
|
||||
Code data = def_variable( "Data", ptr_type );
|
||||
|
||||
Code init;
|
||||
{
|
||||
Code params = def_parameters( 1, "mem_hanlder", t_allocator );
|
||||
Code body = untyped_fmt( "\t""return init_reserve( mem_handler, grow_formula(0) );" );
|
||||
Code params = def_parameters( 1, t_allocator, "mem_handler" );
|
||||
Code body = untyped_str( "return init_reserve( mem_handler, grow_formula(0) );" );
|
||||
|
||||
init = def_function( "init", UnusedCode, params, t_bool, body );
|
||||
}
|
||||
|
||||
Code init_reserve;
|
||||
{
|
||||
Code params = def_parameters( 2, "mem_handler", ref_type, "capacity", t_sw );
|
||||
Code params = def_parameters( 2, t_allocator, "mem_handler", t_sw, "capacity" );
|
||||
Code body;
|
||||
{
|
||||
Code header_value = untyped_fmt(
|
||||
"rcast( Header*, alloc( mem_handler, sizeof( Header ) + sizeof(type) + capacity ))"
|
||||
Code header_value = untyped_str(
|
||||
"rcast( Header*, alloc( mem_handler, sizeof( Header ) + sizeof(Type) + capacity ))"
|
||||
);
|
||||
Code header = def_variable( "", ptr_header, header_value );
|
||||
Code header = def_variable( "header", ptr_header, header_value );
|
||||
|
||||
Code null_check = untyped_fmt(
|
||||
"\t" "if (header == nullptr)"
|
||||
"\n\t\t" "return false;"
|
||||
Code null_check = untyped_str(
|
||||
"if (header == nullptr)"
|
||||
"\n" "return false;"
|
||||
);
|
||||
|
||||
Code header_init = untyped_fmt(
|
||||
"\n\t" "header->Num = 0;"
|
||||
"\n\t" "header->Capacity = capacity;"
|
||||
"\n\t" "header->Allocator = mem_handler;"
|
||||
Code header_init = untyped_str(
|
||||
"header->Num = 0;"
|
||||
"\n""header->Capacity = capacity;"
|
||||
"\n""header->Allocator = mem_handler;"
|
||||
);
|
||||
|
||||
Code assign_data = untyped_fmt(
|
||||
"\t" "Data = rcast( %s, header + 1 );", ptr_type
|
||||
Code assign_data = untyped_str(
|
||||
"Data = rcast( %s, header + 1 );", ptr_type
|
||||
);
|
||||
|
||||
Code ret_true = untyped_fmt( "\t""return true" );
|
||||
Code ret_true = untyped_str( "\t""return true" );
|
||||
|
||||
body = def_function_body( 5
|
||||
, header
|
||||
@ -121,9 +144,9 @@
|
||||
|
||||
Code free;
|
||||
{
|
||||
Code body = untyped_fmt(
|
||||
"\t" "Header& header = get_header();"
|
||||
"\n\t" "::free( header.Allocator, & get_header() );"
|
||||
Code body = untyped_str(
|
||||
"Header& header = get_header();"
|
||||
"\n""::free( header.Allocator, & get_header() );"
|
||||
);
|
||||
|
||||
free = def_function( "free", UnusedCode, UnusedCode, t_void, body );
|
||||
@ -131,22 +154,22 @@
|
||||
|
||||
Code append;
|
||||
{
|
||||
Code params = def_parameters( 1, "value", type );
|
||||
Code params = def_parameters( 1, type, "value" );
|
||||
Code body;
|
||||
{
|
||||
Code header = def_variable( "", ref_header, untyped_fmt( "get_header()") );
|
||||
Code header = def_variable( "header", ref_header, untyped_str( "get_header()") );
|
||||
|
||||
Code check_cap = untyped_fmt(
|
||||
"\t" "if ( header.Capacity < header.Num + 1 )"
|
||||
"\n\t\t" "if ( ! grow(0) )"
|
||||
"\n\t\t\t" "return false;"
|
||||
Code check_cap = untyped_str(
|
||||
"if ( header.Capacity < header.Num + 1 )"
|
||||
"\n" "if ( ! grow(0) )"
|
||||
"\n" "return false;"
|
||||
);
|
||||
|
||||
Code assign = untyped_fmt(
|
||||
"\t" "Data[ header.Num ] = value;"
|
||||
"\t\n" "header.Num++;"
|
||||
Code assign = untyped_str(
|
||||
"Data[ header.Num ] = value;"
|
||||
"\n" "header.Num++;"
|
||||
"\n"
|
||||
"\n\t" "return true;"
|
||||
"\n" "return true;"
|
||||
);
|
||||
|
||||
body = def_function_body( 3, header, check_cap, assign );
|
||||
@ -157,9 +180,9 @@
|
||||
|
||||
Code back;
|
||||
{
|
||||
Code body = untyped_fmt(
|
||||
"\t" "Header& header = get_header();"
|
||||
"\n\t" "return data[ header.Num - 1 ];"
|
||||
Code body = untyped_str(
|
||||
"Header& header = get_header();"
|
||||
"\n" "return data[ header.Num - 1 ];"
|
||||
);
|
||||
|
||||
back = def_function( "back", UnusedCode, UnusedCode, type, body );
|
||||
@ -167,26 +190,26 @@
|
||||
|
||||
Code clear;
|
||||
{
|
||||
Code body = untyped_fmt( "\t""get_header().Num = 0;" );
|
||||
Code body = untyped_str( "get_header().Num = 0;" );
|
||||
|
||||
clear = def_function( "clear", UnusedCode, UnusedCode, t_void, body );
|
||||
}
|
||||
|
||||
Code fill;
|
||||
{
|
||||
Code params = def_parameters( 3, "begin", t_uw, "end", t_uw, "value", type );
|
||||
Code params = def_parameters( 3, t_uw, "begin", t_uw, "end", type, "value" );
|
||||
Code body;
|
||||
{
|
||||
Code header = def_variable( "", ref_header, untyped_fmt( "get_header()") );
|
||||
Code header = def_variable( "header", ref_header, untyped_str( "get_header()") );
|
||||
|
||||
Code check = untyped_fmt(
|
||||
"\t" "if ( begin < 0 || end >= header.Num )"
|
||||
"\n\t\t" "fatal( \"Range out of bounds\" );"
|
||||
Code check = untyped_str(
|
||||
"if ( begin < 0 || end >= header.Num )"
|
||||
"\n" "fatal( \"Range out of bounds\" );"
|
||||
);
|
||||
|
||||
Code iter = untyped_fmt(
|
||||
"\t" "for ( sw index = begin; index < end; index++ )"
|
||||
"\n\t\t" "Data[index] = vallue;"
|
||||
Code iter = untyped_str(
|
||||
"for ( sw index = begin; index < end; index++ )"
|
||||
"\n" "Data[index] = vallue;"
|
||||
);
|
||||
|
||||
body = def_function_body( 3, header, check, iter );
|
||||
@ -197,25 +220,25 @@
|
||||
|
||||
Code get_header;
|
||||
{
|
||||
Code body = untyped_fmt( "\t""return pcast( Header, Data - 1 );" );
|
||||
Code body = untyped_str( "return pcast( Header, Data - 1 );" );
|
||||
|
||||
get_header = def_function( "get_header", spec_inline, UnusedCode, ref_header, body );
|
||||
}
|
||||
|
||||
Code grow;
|
||||
{
|
||||
Code param = def_parameters( 1, "min_capacity", t_uw );
|
||||
Code param = def_parameters( 1, t_uw, "min_capacity" );
|
||||
Code body;
|
||||
{
|
||||
Code header = def_variable( "header", ref_header, untyped_fmt("get_header") );
|
||||
Code new_capacity = def_variable( "new_capacity", t_uw, untyped_fmt("grow_formula( header.Capacity )") );
|
||||
Code header = def_variable( "header", ref_header, untyped_str("get_header()") );
|
||||
Code new_capacity = def_variable( "new_capacity", t_uw, untyped_str("grow_formula( header.Capacity )") );
|
||||
|
||||
Code check_n_set = untyped_fmt(
|
||||
"\t" "if ( new_capacity < min_capacity )"
|
||||
"\n\t\t" "new_capacity = min_capacity;"
|
||||
Code check_n_set = untyped_str(
|
||||
"if ( new_capacity < min_capacity )"
|
||||
"\n" "new_capacity = min_capacity;"
|
||||
);
|
||||
|
||||
Code ret = untyped_fmt( "\t" "return set_capacity( new_capacity );" );
|
||||
Code ret = untyped_str( "return set_capacity( new_capacity );" );
|
||||
|
||||
body = def_function_body( 4, header, new_capacity, check_n_set, ret );
|
||||
}
|
||||
@ -227,10 +250,10 @@
|
||||
{
|
||||
Code body;
|
||||
{
|
||||
Code header = def_variable( "header", ref_header, untyped_fmt("get_header()") );
|
||||
Code header = def_variable( "header", ref_header, untyped_str("get_header()") );
|
||||
|
||||
Code assertion = untyped_fmt( "\t" "assert( header.Num > 0 );" );
|
||||
Code decrement = untyped_fmt( "\t" "header.Num--; " );
|
||||
Code assertion = untyped_str( "assert( header.Num > 0 );" );
|
||||
Code decrement = untyped_str( "header.Num--; " );
|
||||
|
||||
body = def_function_body( 3, header, assertion, decrement );
|
||||
}
|
||||
@ -240,17 +263,17 @@
|
||||
|
||||
Code reserve;
|
||||
{
|
||||
Code params = def_parameters( 1, "new_capacity", t_uw );
|
||||
Code params = def_parameters( 1, t_uw, "new_capacity" );
|
||||
Code body;
|
||||
{
|
||||
Code header = def_variable( "header", ref_header, untyped_fmt("get_header()") );
|
||||
Code header = def_variable( "header", ref_header, untyped_str("get_header()") );
|
||||
|
||||
Code check_n_set = untyped_fmt(
|
||||
"\t" "if ( header.Capacity < new_capacity )"
|
||||
"\n\t\t" "return set_capacity( new_capacity );"
|
||||
Code check_n_set = untyped_str(
|
||||
"if ( header.Capacity < new_capacity )"
|
||||
"\n" "return set_capacity( new_capacity );"
|
||||
);
|
||||
|
||||
Code ret = untyped_fmt( "\t" "return true" );
|
||||
Code ret = untyped_str( "\t" "return true" );
|
||||
|
||||
body = def_function_body( 3, header, check_n_set, ret );
|
||||
}
|
||||
@ -260,21 +283,21 @@
|
||||
|
||||
Code resize;
|
||||
{
|
||||
Code param = def_parameters( 1, "new_num", t_uw );
|
||||
Code param = def_parameters( 1, t_uw, "new_num" );
|
||||
|
||||
Code body;
|
||||
{
|
||||
Code header = def_variable( "header", ref_header, untyped_fmt("get_header()") );
|
||||
Code header = def_variable( "header", ref_header, untyped_str("get_header()") );
|
||||
|
||||
Code check_n_grow = untyped_fmt(
|
||||
"\t" "if ( header.Capacity < new_num )"
|
||||
"\n\t\t" "if ( ! grow( new_num) )"
|
||||
"\n\t\t\t" "return false;"
|
||||
Code check_n_grow = untyped_str(
|
||||
"if ( header.Capacity < new_num )"
|
||||
"\n" "if ( ! grow( new_num) )"
|
||||
"\n" "return false;"
|
||||
);
|
||||
|
||||
Code set_n_ret = untyped_fmt(
|
||||
"\t" "header.Count = new_num;"
|
||||
"\n\t" "return true;"
|
||||
Code set_n_ret = untyped_str(
|
||||
"header.Count = new_num;"
|
||||
"\n""return true;"
|
||||
);
|
||||
|
||||
body = def_function_body( 3, header, check_n_grow, set_n_ret );
|
||||
@ -285,40 +308,40 @@
|
||||
|
||||
Code set_capacity;
|
||||
{
|
||||
Code param = def_parameters( 1, "capacity", t_uw );
|
||||
Code param = def_parameters( 1, t_uw, "capacity" );
|
||||
|
||||
Code body;
|
||||
{
|
||||
Code header = def_variable( "header", ref_header, untyped_fmt("get_header()") );
|
||||
Code header = def_variable( "header", ref_header, untyped_str("get_header()") );
|
||||
|
||||
Code checks = untyped_fmt(
|
||||
"\t" "if ( capacity == header.Capacity )"
|
||||
"\n\t\t" "return true;"
|
||||
"\n"
|
||||
"\n\t" "if ( capacity < header.Num )"
|
||||
"\n\t\t" "header.Num = capacity;"
|
||||
Code checks = untyped_str(
|
||||
"if ( capacity == header.Capacity )"
|
||||
"\n" "return true;"
|
||||
|
||||
"if ( capacity < header.Num )"
|
||||
"\n" "header.Num = capacity;"
|
||||
);
|
||||
|
||||
Code size = def_variable( "size", t_uw, untyped_fmt("sizeof(Header) + sizeof(type) * capacity"));
|
||||
Code new_header = def_variable( "new_header", ptr_header, untyped_fmt("rcast( Header*, alloc( header.Allocator, size ));"));
|
||||
Code size = def_variable( "size", t_uw, untyped_str("sizeof(Header) + sizeof(Type) * capacity"));
|
||||
Code new_header = def_variable( "new_header", ptr_header, untyped_str("rcast( Header*, alloc( header.Allocator, size ));"));
|
||||
|
||||
Code check_n_move = untyped_fmt(
|
||||
"\t""if ( new_header == nullptr )"
|
||||
"\n\t\t""return false;"
|
||||
"\n"
|
||||
"\n\t""memmove( new_header, & header, sizeof(Header) + sizeof(type) * header.Num );"
|
||||
Code check_n_move = untyped_str(
|
||||
"if ( new_header == nullptr )"
|
||||
"\n" "return false;"
|
||||
"\n"
|
||||
"\n" "memmove( new_header, & header, sizeof(Header) + sizeof(Type) * header.Num );"
|
||||
);
|
||||
|
||||
Code set_free_ret = untyped_fmt(
|
||||
"\t" "new_header->Allocator = header.Allocator;"
|
||||
"\n\t" "new_header->Num = header.Num;"
|
||||
"\n\t" "new_header->Capacity = header.Capacity;"
|
||||
"\n"
|
||||
"\n\t" "zpl_free( header );"
|
||||
"\n"
|
||||
"\n\t" "*Data = new_header + 1;"
|
||||
"\n"
|
||||
"\n\t" "return true;"
|
||||
Code set_free_ret = untyped_str(
|
||||
"new_header->Allocator = header.Allocator;"
|
||||
"\n" "new_header->Num = header.Num;"
|
||||
"\n" "new_header->Capacity = header.Capacity;"
|
||||
"\n"
|
||||
"\n" "zpl_free( header );"
|
||||
"\n"
|
||||
"\n" "*Data = new_header + 1;"
|
||||
"\n"
|
||||
"\n" "return true;"
|
||||
);
|
||||
|
||||
body = def_function_body( 6, header, checks, size, new_header, check_n_move, set_free_ret );
|
||||
@ -327,6 +350,8 @@
|
||||
set_capacity = def_function( "set_capacity", UnusedCode, param, t_bool, body );
|
||||
}
|
||||
|
||||
string name = bprintf( "Array_%s", type_str );
|
||||
|
||||
Code body = def_struct_body( 15
|
||||
, using_type
|
||||
, data
|
||||
@ -348,6 +373,282 @@
|
||||
|
||||
array_def = def_struct( name, body, parent );
|
||||
}
|
||||
#else
|
||||
type( t_uw, uw );
|
||||
type( t_sw, sw );
|
||||
type( t_bool, bool );
|
||||
type( t_allocator, allocator );
|
||||
type( t_void, void );
|
||||
|
||||
value( v_nullptr, nullptr );
|
||||
|
||||
specifiers( spec_ct, Specifier::Constexpr );
|
||||
specifiers( spec_inline, Specifier::Inline );
|
||||
|
||||
type_fmt( type, type_str, __);
|
||||
type_fmt( ptr_type, "%s*", type_str );
|
||||
type_fmt( ref_type, "%&", type_str );
|
||||
|
||||
|
||||
// From ArrayBase
|
||||
type( t_header, Header );
|
||||
type( ptr_header, Header* );
|
||||
type( ref_header, Header& );
|
||||
|
||||
def( array_def )
|
||||
{
|
||||
using( Type, type );
|
||||
var( Data, ptr_type, __, __ );
|
||||
|
||||
def( init )
|
||||
{
|
||||
Code body = function_body( untyped_str("return init_reserve( mem_handler, grow_formula(0) );" ));
|
||||
|
||||
function( init, __, t_bool, params( t_allocator, "mem_handler" ), body );
|
||||
}
|
||||
|
||||
def( init_reserve )
|
||||
{
|
||||
def( body )
|
||||
{
|
||||
var(header, ptr_header, untyped_str("rcast( Header*, alloc( mem_handler, sizeof(Header) + sizeof(Type) + capacity))" ), __);
|
||||
|
||||
Code null_check = untyped_str(
|
||||
"if (header == nullptr)"
|
||||
"\n" "return false;"
|
||||
);
|
||||
|
||||
Code header_init = untyped_str(
|
||||
"header->Num = 0;"
|
||||
"\n""header->Capacity = capacity;"
|
||||
"\n""header->Allocator = mem_handler;"
|
||||
);
|
||||
|
||||
Code assign_data = untyped_str( "Data = rcast( Type*, header + 1 );" );
|
||||
Code ret_true = untyped_str( "return true" );
|
||||
|
||||
Code body = function_body( header, null_check, header_init, assign_data, ret_true );
|
||||
}
|
||||
|
||||
function( init_reserve, __, t_bool, params( ref_type, "mem_handler" ) , body);
|
||||
}
|
||||
|
||||
def( free )
|
||||
{
|
||||
Code body = untyped_str(
|
||||
"Header& header = get_header();"
|
||||
"\n""free( header.Allocator, & get_header() );"
|
||||
);
|
||||
|
||||
function( free, __, t_void, __, body );
|
||||
}
|
||||
|
||||
def( append )
|
||||
{
|
||||
def( body )
|
||||
{
|
||||
var( header, ref_header, untyped_str("get_header()"), __ );
|
||||
|
||||
Code check_cap = untyped_str(
|
||||
"if ( header.Capacity < header.Num + 1 )"
|
||||
"\n" "if ( ! grow(0) )"
|
||||
"\n" "return false;"
|
||||
);
|
||||
|
||||
Code assign = untyped_str(
|
||||
"Data[ header.Num ] = value;"
|
||||
"\n" "header.Num++;"
|
||||
"\n"
|
||||
"\n" "return true;"
|
||||
);
|
||||
|
||||
body = function_body( header, check_cap, assign );
|
||||
}
|
||||
|
||||
function( append, __, t_void, params( type, "value" ), body );
|
||||
}
|
||||
|
||||
def( back );
|
||||
{
|
||||
Code body = untyped_str(
|
||||
"Header& header = get_header();"
|
||||
"\n" "return data[ header.Num - 1 ];"
|
||||
);
|
||||
|
||||
function( back, __, type, __, body );
|
||||
}
|
||||
|
||||
def( clear )
|
||||
function( clear, __, t_void, __, untyped_str("get_header().Num = 0;") );
|
||||
|
||||
def( fill );
|
||||
{
|
||||
def( body )
|
||||
{
|
||||
var( header, ref_header, untyped_str("get_header()"), __ );
|
||||
|
||||
Code check = untyped_str(
|
||||
"if ( begin < 0 || end >= header.Num )"
|
||||
"\n" "fatal( \"Range out of bounds\" );"
|
||||
);
|
||||
|
||||
Code iter = untyped_str(
|
||||
"for ( sw index = begin; index < end; index++ )"
|
||||
"\n" "Data[index] = vallue;"
|
||||
);
|
||||
|
||||
function_body( header, check, iter );
|
||||
}
|
||||
|
||||
function( fill, __, t_void, params( t_uw, "begin", t_uw, "end", type, "value" ), body );
|
||||
}
|
||||
|
||||
def( get_header )
|
||||
function( get_header, spec_inline, ref_header, __, untyped_str("return pcast( Header, Data - 1);") );
|
||||
|
||||
def( grow )
|
||||
{
|
||||
def( body )
|
||||
{
|
||||
var( header, ref_header, untyped_str("get_header()"), __ );
|
||||
var( new_capacity, t_uw, untyped_str("grow_formula( header.Capacity)"), __);
|
||||
|
||||
Code check_n_set = untyped_str(
|
||||
"if ( new_capacity < min_capacity )"
|
||||
"new_capacity = min_capacity;"
|
||||
);
|
||||
|
||||
Code ret = untyped_str( "return set_capacity( new_capacity );" );
|
||||
|
||||
body = function_body( header, new_capacity, check_n_set, ret );
|
||||
}
|
||||
|
||||
function( grow, __, t_bool, params( t_uw, "min_capacity" ), body );
|
||||
}
|
||||
|
||||
def( pop )
|
||||
{
|
||||
def( body )
|
||||
{
|
||||
var( header, ref_header, get_header(), UnusedCode );
|
||||
|
||||
Code assertion = untyped_str( "assert( header.Num > 0 );" );
|
||||
Code decrement = untyped_str( "header.Num--; " );
|
||||
|
||||
body = function_body( header, assertion, decrement );
|
||||
}
|
||||
|
||||
function( pop, __, t_void, __, body );
|
||||
}
|
||||
|
||||
def( reserve )
|
||||
{
|
||||
def( body )
|
||||
{
|
||||
var( header, ref_header, untyped_str("get_header()"), __ );
|
||||
|
||||
Code check_n_set = untyped_str(
|
||||
"if ( header.Capacity < new_capacity )"
|
||||
"return set_capacity( new_capacity );"
|
||||
);
|
||||
|
||||
Code ret = untyped_str("return true");
|
||||
|
||||
body = function_body( header, check_n_set, ret );
|
||||
}
|
||||
|
||||
function( reserve, __, t_bool, params( t_uw, "new_capacity" ), body );
|
||||
}
|
||||
|
||||
def( resize )
|
||||
{
|
||||
def( body )
|
||||
{
|
||||
var( header, ref_header, untyped_str("get_header()"), __ );
|
||||
|
||||
Code check_n_grow = untyped_str(
|
||||
"if ( header.Capacity < new_num )"
|
||||
"if ( ! grow( new_num) )"
|
||||
"return false;"
|
||||
);
|
||||
|
||||
Code set_n_ret = untyped_str(
|
||||
"header.Count = new_num;"
|
||||
"return true;"
|
||||
);
|
||||
|
||||
body = function_body( header, check_n_grow, set_n_ret );
|
||||
}
|
||||
|
||||
function( resize, __, t_bool, params( t_uw, "new_num" ), body );
|
||||
}
|
||||
|
||||
def( set_capacity )
|
||||
{
|
||||
def( body )
|
||||
{
|
||||
var( header, ref_header, untyped_str("get_header()"), __ );
|
||||
|
||||
Code checks = untyped_str(
|
||||
"if ( capacity == header.Capacity )"
|
||||
"return true;"
|
||||
"\n\n"
|
||||
"if ( capacity < header.Num )"
|
||||
"header.Num = capacity;"
|
||||
);
|
||||
|
||||
var( size, t_uw, untyped_str("sizeof(Header) + sizeof(Type) * capacity"), __ );
|
||||
var( new_header, ptr_header, untyped_str("rcast( Header*, alloc( header.Allocator, size ))"), __ );
|
||||
|
||||
Code check_n_move = untyped_str(
|
||||
"if ( new_header == nullptr )"
|
||||
"return false;"
|
||||
"\n\n"
|
||||
"memmove( new_header, & header, sizeof(Header) + sizeof(Type) * header.Num );"
|
||||
);
|
||||
|
||||
Code set_free_ret = untyped_str(
|
||||
"new_header->Allocator = header.Allocator;"
|
||||
"new_header->Num = header.Num;"
|
||||
"new_header->Capacity = header.Capacity;"
|
||||
"\n\n"
|
||||
"zpl_free( header );"
|
||||
"\n\n"
|
||||
"*Data = new_header + 1;"
|
||||
"\n\n"
|
||||
"return true;"
|
||||
);
|
||||
|
||||
body = function_body( header, checks, size, new_header, check_n_move, set_free_ret );
|
||||
}
|
||||
|
||||
function( set_capacity, __, t_bool, params( t_uw, "capacity" ), body );
|
||||
}
|
||||
|
||||
char const* name = bprintf( "Array_%s", type_str );
|
||||
|
||||
Code body = struct_body(
|
||||
Type
|
||||
, Data
|
||||
|
||||
, init
|
||||
, init_reserve
|
||||
, append
|
||||
, back
|
||||
, clear
|
||||
, fill
|
||||
, free
|
||||
, get_header
|
||||
, grow
|
||||
, pop
|
||||
, reserve
|
||||
, resize
|
||||
, set_capacity
|
||||
);
|
||||
|
||||
array_def = def_struct( name, body, parent );
|
||||
}
|
||||
#endif
|
||||
|
||||
return array_def;
|
||||
}
|
||||
|
@ -22,21 +22,27 @@
|
||||
{
|
||||
Code integral_type = def_type( type );
|
||||
|
||||
#ifndef GEN_DEFINE_DSL
|
||||
string name = string_sprintf( g_allocator, (char*)sprintf_buf, ZPL_PRINTF_MAXLEN, "square", type );
|
||||
|
||||
Code result;
|
||||
Code square;
|
||||
{
|
||||
Code params = def_parameters( 1, "value", integral_type );
|
||||
Code params = def_parameters( 1, integral_type, "value" );
|
||||
Code specifiers = def_specifiers( 1, Specifier::Inline );
|
||||
Code ret_stmt = untyped_fmt( "\treturn value * value;" );
|
||||
Code ret_stmt = untyped_fmt( "return value * value;" );
|
||||
|
||||
result = def_function( name, specifiers, params, integral_type, ret_stmt );
|
||||
square = def_function( name, specifiers, params, integral_type, ret_stmt );
|
||||
}
|
||||
|
||||
if ( ! result )
|
||||
#else
|
||||
def( square )
|
||||
function( square, __, integral_type, params( integral_type, "value" ), untyped_str("return value * value") );
|
||||
#endif
|
||||
|
||||
if ( ! square )
|
||||
fatal( "Failed to generate square function for: %s", type );
|
||||
|
||||
return result;
|
||||
return square;
|
||||
}
|
||||
|
||||
u32 gen_math()
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include "Bloat.cpp"
|
||||
#include "math.hpp"
|
||||
#include "Array.hpp"
|
||||
|
||||
|
||||
#ifdef gen_time
|
||||
@ -12,9 +13,11 @@ int gen_main()
|
||||
zpl_printf("\nPress any key after attaching to process\n");
|
||||
getchar();
|
||||
|
||||
gen::init()
|
||||
gen::init();
|
||||
|
||||
int result = gen_math();
|
||||
int
|
||||
result = gen_math();
|
||||
result = gen_array_file();
|
||||
|
||||
Memory::cleanup();
|
||||
return result;
|
||||
|
Reference in New Issue
Block a user