gencpp/test/math.hpp

88 lines
1.7 KiB
C++
Raw Normal View History

2023-04-01 19:21:46 -07:00
#pragma once
#include "Bloat.hpp"
#include "gen.hpp"
2023-04-01 19:21:46 -07:00
#ifdef gen_time
2023-04-01 19:21:46 -07:00
using namespace gen;
2023-04-02 08:53:15 -07:00
char* sprintf_buf[ZPL_PRINTF_MAXLEN];
2023-04-01 19:21:46 -07:00
/*
What it should generate:
inline
type square( type value )
2023-04-01 19:21:46 -07:00
{
return value * value;
}
*/
#define gen_square( Type_ ) gen__square( #Type_ )
Code gen__square( char const* type )
2023-04-01 19:21:46 -07:00
{
Code integral_type = def_type( type );
2023-04-01 19:21:46 -07:00
#ifndef GEN_DEFINE_DSL
string name = string_sprintf( g_allocator, (char*)sprintf_buf, ZPL_PRINTF_MAXLEN, "square", type );
2023-04-01 19:21:46 -07:00
#if 1
Code square;
{
Code params = def_params( 1, integral_type, "value" );
Code specifiers = def_specifiers( 1, SpecifierT::Inline );
Code ret_stmt = untyped_str( txt( return value * value; ));
square = def_proc( name, specifiers, params, integral_type, ret_stmt );
}
2023-04-01 19:21:46 -07:00
#else
// Test of token template str.
char const* tmpl = txt(
{type} square( {type} value )
{
return value * value;
}
);
char const* gen_code = token_fmt( tmpl, 1, type );
Code square = parse_proc(gen_code);
#endif
#else
Code proc( square, __, integral_type, params( integral_type, "value" ),
untyped(return value * value)
);
#endif
if ( ! square )
2023-04-01 19:21:46 -07:00
fatal( "Failed to generate square function for: %s", type );
return square;
2023-04-01 19:21:46 -07:00
}
u32 gen_math()
{
Code fadd_u8 = gen_square( u8 );
2023-04-02 08:53:15 -07:00
Code fadd_u16 = gen_square( u16 );
Code fadd_u32 = gen_square( u32 );
Code fadd_u64 = gen_square( u64 );
2023-04-01 19:21:46 -07:00
Builder
2023-04-01 19:21:46 -07:00
mathgen;
mathgen.open( "math.gen.hpp" );
mathgen.print( fadd_u8 );
2023-04-02 08:53:15 -07:00
mathgen.print( fadd_u16 );
mathgen.print( fadd_u32 );
mathgen.print( fadd_u64 );
2023-04-01 19:21:46 -07:00
mathgen.write();
return 0;
}
#endif
#ifndef gen_time
# include "math.gen.hpp"
# undef square
2023-04-01 19:21:46 -07:00
#endif