2023-04-01 19:21:46 -07:00
|
|
|
#pragma once
|
|
|
|
|
2023-04-01 22:07:44 -07:00
|
|
|
#include "Bloat.hpp"
|
|
|
|
#include "gen.hpp"
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-01 22:07:44 -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
|
2023-04-02 09:35:14 -07:00
|
|
|
type square( type value )
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
|
|
|
return value * value;
|
|
|
|
}
|
|
|
|
*/
|
2023-04-01 22:07:44 -07:00
|
|
|
#define gen_square( Type_ ) gen__square( #Type_ )
|
|
|
|
Code gen__square( char const* type )
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
2023-04-02 09:35:14 -07:00
|
|
|
Code integral_type = def_type( type );
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-03 00:55:28 -07:00
|
|
|
#ifndef GEN_DEFINE_DSL
|
2023-04-02 09:35:14 -07:00
|
|
|
string name = string_sprintf( g_allocator, (char*)sprintf_buf, ZPL_PRINTF_MAXLEN, "square", type );
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-05 00:03:56 -07:00
|
|
|
#if 0
|
2023-04-03 00:55:28 -07:00
|
|
|
Code square;
|
2023-04-02 09:35:14 -07:00
|
|
|
{
|
2023-04-03 23:04:19 -07:00
|
|
|
Code params = def_params( 1, integral_type, "value" );
|
|
|
|
Code specifiers = def_specifiers( 1, SpecifierT::Inline );
|
|
|
|
Code ret_stmt = untyped_str( txt( return value * value; ));
|
2023-04-02 09:35:14 -07:00
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
square = def_proc( name, specifiers, params, integral_type, ret_stmt );
|
2023-04-02 09:35:14 -07:00
|
|
|
}
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
#else
|
|
|
|
// Test of token template str.
|
|
|
|
char const* tmpl = txt(
|
|
|
|
{type} square( {type} value )
|
|
|
|
{
|
|
|
|
return value * value;
|
|
|
|
}
|
|
|
|
);
|
2023-04-05 00:03:56 -07:00
|
|
|
char const* gen_code = token_fmt( tmpl, 1, "type", type );
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-05 00:03:56 -07:00
|
|
|
Code square = parse_proc(gen_code, strlen(gen_code));
|
2023-04-03 23:04:19 -07:00
|
|
|
#endif
|
|
|
|
|
2023-04-03 00:55:28 -07:00
|
|
|
#else
|
2023-04-03 23:04:19 -07:00
|
|
|
|
|
|
|
Code proc( square, __, integral_type, params( integral_type, "value" ),
|
|
|
|
untyped(return value * value)
|
|
|
|
);
|
2023-04-03 00:55:28 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if ( ! square )
|
2023-04-01 19:21:46 -07:00
|
|
|
fatal( "Failed to generate square function for: %s", type );
|
|
|
|
|
2023-04-03 00:55:28 -07:00
|
|
|
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
|
|
|
|
2023-04-01 22:07:44 -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
|
2023-04-02 09:35:14 -07:00
|
|
|
# include "math.gen.hpp"
|
|
|
|
# undef square
|
2023-04-01 19:21:46 -07:00
|
|
|
|
|
|
|
#endif
|