Iniital commit

This commit is contained in:
2023-04-01 22:21:46 -04:00
commit f09fe6aa15
19 changed files with 19851 additions and 0 deletions

View File

@ -0,0 +1,13 @@
// Handwritten generated code:
#pragma region gen_array
#pragma endregion gen_array
#define Array( Type_ ) Array_##Type_

164
test/array.hpp.txt Normal file
View File

@ -0,0 +1,164 @@
#include "gen.hpp"
#include "bloat.hpp"
#if tt
#define gen_array( Type_ ) gen__array( #Type_ )
Code* gen__array( char const* type )
{
Code codegen;
Code
data;
data.add( "%type* data" );
Code header;
header.define_struct( "Header",
"uw Num;"
"uw Capacity;"
"allocator Allocator;"
);
Code grow_formula;
grow_formula.define_function( "grow_formula", "static", "forceinline",
"return 2 * value * 8"
);
codegen.define_struct( "Array",
data
, header
, grow_formula
);
codegen.define
R"(
%type* data;
struct Header
{
uw Num;
uw Capacity;
allocator Allocator;
};
static forceinline
sw grow_formula ( sw value )
{
return 2 * value * 8;
}
static
bool init ( %Array& array, allocator mem_handler )
{
return init_reserve( array, mem_handler, grow_formula(0) );
}
static
bool init_reserve( %Array& array, allocator mem_handler, uw capacity )
{
Header*
header = nullptr;
header = rcast( Header*, alloc( mem_handler, size_of( Header ) + sizeof(type) * capacity ));
if (header == nullptr)
return false;
header->Num = 0;
header->Capacity = capacity;
header->Allocator = mem_handler;
array.data = rcast( %type*, header + 1 );
return true;
}
void append( %type value )
{
}
type back()
{
Header& header = get_header();
return data[ header.Num - 1 ];
}
void clear()
{
get_header().Num = 0;
}
void fill( uw begin, uw end, type value )
{
Header& header = get_header();
if ( begin < 0 || end >= header.Num )
{
fatal( "Range out of bounds" );
}
}
void free()
{
Header& header = get_header();
::free( header.Allocator, & get_header() );
}
bool grow( uw min_capacity )
{
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 );
}
forceinline
Header& get_header()
{
return vcast( Header, data - 1 );
}
void pop()
{
}
void reserve()
{
}
void resize()
{
}
bool set_capacity( uw capacity )
{
Header& header = get_header();
if ( capacity == header.Capacity )
{
}
}
)"
, type
);
};
#endif
void tt_run()
{
gen_array( u32 );
}
#endif tt
#if !tt
#include "array.gen.manual.hpp"
#endif

25
test/gen/meson.build Normal file
View File

@ -0,0 +1,25 @@
project( 'test', 'c', 'cpp', default_options : ['buildtype=debug'] )
# add_global_arguments('-E', language : 'cpp')
includes = include_directories(
[
'../test'
'../project'
, '../thirdparty'
])
# get_sources = files('./get_sources.ps1')
# sources = files(run_command('powershell', get_sources, check: true).stdout().strip().split('\n'))
sources = [ 'test.cpp' ]
if get_option('buildtype').startswith('debug')
add_project_arguments('-DBuild_Debug', language : ['c', 'cpp'])
endif
add_project_arguments('-Dgen_time', langauge : ['c', 'cpp'])
executable( '', sources, include_directories : includes )

71
test/math.hpp Normal file
View File

@ -0,0 +1,71 @@
#pragma once
#ifdef gen_time
#include "Bloat.hpp"
#include "gen.hpp"
using namespace gen;
/*
What it should generate:
inline
type square_#type( type value )
{
return value * value;
}
*/
#define gen_square( Type_ ) gen__squre( #Type_ )
Code gen__squre( char const* type )
{
Code integral_type = make_type( type );
string name = string_sprintf_buf( g_allocator, "square_%s", type );
Code specifiers = make_specifiers( 1, Specifier::Inline );
Code params = make_parameters( 1, "value", integral_type );
Code ret_stmt = make_fmt( "return value * value" );
Code result = make_function( name,
specifiers,
params,
integral_type,
ret_stmt
);
if ( ! result )
fatal( "Failed to generate square function for: %s", type );
return result;
}
u32 gen_math()
{
Code fadd_u8 = gen_square( u8 );
Code fadd_u16 = gen_square( u16 );
Code fadd_u32 = gen_square( u32 );
Code fadd_u64 = gen_square( u64 );
File
mathgen;
mathgen.open( "math.gen.hpp" );
mathgen.print( fadd_u8 );
mathgen.print( fadd_u16 );
mathgen.print( fadd_u32 );
mathgen.print( fadd_u64 );
mathgen.write();
return 0;
}
#endif
#ifndef gen_time
#include "math.gen.hpp"
#undef square
#define sym_square( Type_, Value_ ) square_#Type_( Value_ )
template<class type>
type square( type value )
[
sym_square( type, value );
]
#endif

22
test/meson.build Normal file
View File

@ -0,0 +1,22 @@
project( 'test', 'c', 'cpp', default_options : ['buildtype=debug'] )
# add_global_arguments('-E', language : 'cpp')
includes = include_directories(
[
'../project'
, '../thirdparty'
])
# get_sources = files('./get_sources.ps1')
# sources = files(run_command('powershell', get_sources, check: true).stdout().strip().split('\n'))
sources = [ 'math.hpp' ]
if get_option('buildtype').startswith('debug')
add_project_arguments('-DBuild_Debug', language : ['c', 'cpp'])
endif
executable( '', sources, include_directories : includes )

21
test/test.cpp Normal file
View File

@ -0,0 +1,21 @@
#include "math.hpp"
#ifdef gen_time
u32 gen_main()
{
return gen_math();
}
#include "gen.cpp"
#endif
#ifndef gen_time
int main()
{
u32 result = square(5);
zpl_printf("TEST RESULT: %d", result);
}
#endif