WIP: Still reworking based on design changes.

This commit is contained in:
Edward R. Gonzalez 2023-04-04 02:04:19 -04:00
parent 9957ef0e7d
commit 2e8d4a3d93
12 changed files with 1730 additions and 859 deletions

View File

@ -11,3 +11,10 @@ indent_size = 4
[*.cpp] [*.cpp]
indent_style = tab indent_style = tab
indent_size = 4 indent_size = 4
[*.h]
indent_style = tab
indent_size = 4
[*.hpp]
indent_style = tab

View File

@ -6,6 +6,7 @@
"type_traits": "cpp", "type_traits": "cpp",
"utility": "cpp", "utility": "cpp",
"xtr1common": "cpp", "xtr1common": "cpp",
"xutility": "cpp" "xutility": "cpp",
"initializer_list": "cpp"
} }
} }

183
Readme.md
View File

@ -2,18 +2,22 @@
An attempt at simple staged metaprogramming for c/c++. An attempt at simple staged metaprogramming for c/c++.
This library is intended for small-to midsize projects that want rapid complation times. This library is intended for small-to midsize projects.
for fast debugging.
### TOC
* [Notes](#Notes)
* [Building](#Notes)
## Notes ## Notes
This project is not minimum feature complete yet. This project is not minimum feature complete yet.
Version 1 will have c and a subset of c++ features available to it. Version 1 will have C and a subset of C++ features available to it.
I will generate with this library a C99 or 11 variant when Version 1 is complete. I will generate with this library a C99 or 11 variant when Version 1 is complete.
A single-header version will also be genrated. A single-header version will also be generated.
## How it works ## Usage
A metaprogram is built to generate files before the main program is built. We'll term runtime for this program as `gen_time`. The metaprogram's core implementation are within `gen.hpp` and `gen.cpp` in the project directory. A metaprogram is built to generate files before the main program is built. We'll term runtime for this program as `gen_time`. The metaprogram's core implementation are within `gen.hpp` and `gen.cpp` in the project directory.
@ -44,6 +48,66 @@ u32 gen_main()
``` ```
This is ofc entirely optional and the metaprogram can be quite separated from the runtime in file organization. This is ofc entirely optional and the metaprogram can be quite separated from the runtime in file organization.
The design a constructive builder of a sort of AST for the code to generate.
In the current version of the library, constructions are defined with thier paramters in one go.
Example:
```cpp
// Get a Code AST from the CodePool.
Code header = make_struct( "Header" );
{
// Make a struct body.
Code body = header.body();
// Members
body->add( def_varaible( uw, "Num") );
body->add( def_varaible( uw, "Capacity") );
body->add( def_varaible( allocator, "Allocator") );
}
```
We first define a Code variable called header. We then open a stack frame for header's components.
Header will be a struct so we need to define its body, in this case struct acts a pure POD with only variable member symbols.
The components are defined first, then the struct body is constructed. Finally the body is provided the def_struct function.
This will generate the following C code:
```cpp
struct ArrayHeader
{
uw Num;
uw Capacity;
allocator Allocator;
};
```
**Note: The formatting shown here is not how it will look. For your desired formatting its recommended to run a pass through the files with an auto-formatter.**
## Gen's DSL
If you don't mind a low amount of macros (29 lines), a DSL may be optionally defined with:
```cpp
GEN_DEFINE_DSL
```
Using the previous example to show usage:
```cpp
make( struct, ArrayHeader )
{
Code
body = ArrayHeader.body();
body->add_var( uw, Num );
body->add_var( uw, Capacity );
body->add_var( allocaotr, Allocator );
}
```
The `__` represents the `UnusedCode` value constant, of unneeded varaibles.
The DSL purposefully has no nested macros, with the follwing exceptions:
* `__VA_ARGS__` for parameter expnasion
* `VA_NARGS( __VA_ARGS__ )` for determing number of paramters
* `txt(Value_)` and `txt_with_length(Value_)` to serialize a value type identifier.
## Building ## Building
An example of building is provided in the test directory. An example of building is provided in the test directory.
@ -56,6 +120,113 @@ Both of them build the same source file: `test.cpp`. The only differences betwee
This method is setup where all the metaprogram's code are the within the same files as the regular program's code. This method is setup where all the metaprogram's code are the within the same files as the regular program's code.
If in your use case, decide to have exclusive separation or partial separation of the metaprogam's code from the program's code files then your build configuration would need to change to reflect that (specifically the sources). If in your use case, decide to have exclusive separation or partial separation of the metaprogam's code from the program's code files then your build configuration would need to change to reflect that (specifically the sources).
## Outline
### *WHAT IS NOT PROVIDED* :
* Macro or template generation : This library is to avoid those,
adding support for them adds unnecessary complexity. If you desire define them outside the gen_time scopes.
* Expression validation : Execution expressions are defined using the untyped string API. There is no parse API for validating expression (possibly will add in the future)
* Complete file parser DSL : This isn't like the unreal header tool. Code injection to file or based off a file contents is not supported by the api. However nothing is stopping you using the library for that purpose.
* Modern c++ (STL library) features
### There are four different of construction of Code ast's the library provides:
* Upfront construction
* Incremental construction
* Parse construction
* Untyped
### Upfront Construction:
All component ASTs must be previously constructed, and provided on creation of the code AST.
The construction will fail and return InvalidCode otherwise.
API:
* def_forward_decl
* def_class
* def_global_body
* def_proc
* def_proc_body
* def_namespace
* def_namespace_body
* def_param
* def_params
* def_operator
* def_specifier
* def_specifiers
* def_struct
* def_struct_body
* def_variable
* def_type
* def_using
* def_using_namespace
### Incremental construction:
A Code ast is provided but only completed upfront if all components are provided.
Components are then added using the AST API for adding ASTs:
* code.add( AST* ) // Adds AST with validation.
* code.add_entry( AST* ) // Adds AST entry without validation.
* code.add_content( AST* ) // Adds AST string content without validation.
API:
* make_forward_decl
* make_class
* make_global_body
* make_proc
* make_namespace
* make_params
* make_operator
* make_specifiers
* make_struct
* make_variable
* make_type
* make_using
* make_using_namespace
### Parse construction:
A string provided to the API is parsed for the intended language construct.
API:
* parse_forward_decl
* parse_class
* parse_glboal_body
* parse_proc
* parse_namespace
* parse_params
* parse_operator
* parse_specifiers
* parse_struct
* parse_variable
* parse_type
* parse_using
* parse_using
The parse API treats any execution scope definitions with no validation and are turned into untyped Code ASTs.
This includes the assignmetn of variables; due to the library not yet supporting c/c++ expression parsing.
Untyped constructions:
Code ASTs are constructed using unvalidated strings.
API:
* untyped_str
* untyped_fmt
* untyped_token_fmt
During serialization any untyped Code AST is has its string value directly injected inline of
whatever context the content existed as an entry within.
Even though thse are not validated from somewhat correct c/c++ syntax or components, it doesn't mean that
Untyped code can be added as any component of a Code AST:
* Untyped code cannot have children, thus there cannot be recursive injection this way.
* Untyped code can only be a child of a parent of body AST, or for values of an assignment.
These restrictions help prevent abuse of untyped code to some extent.
## Why ## Why
Macros in c/c++ are usually painful to debug, and templates can be unless your on a monsterous IDE (and even then fail often). Macros in c/c++ are usually painful to debug, and templates can be unless your on a monsterous IDE (and even then fail often).
@ -81,3 +252,5 @@ However, if the code being generated becomes complex, or from a datatable or dat
* Make a test suite made up of collections based of the ZPL library templated colllection macros and the memory module. * Make a test suite made up of collections based of the ZPL library templated colllection macros and the memory module.
* Generate a single-header library. * Generate a single-header library.
* Generate a C-supported single-header library. * Generate a C-supported single-header library.
* Remove full ZPL dependency, move into Bloat header/source only what is used.
* This library has heavy string allocations, most likely will make a string flyweight for it.

View File

@ -1,6 +1,7 @@
#define BLOAT_IMPL #define BLOAT_IMPL
#include "Bloat.hpp" #include "Bloat.hpp"
namespace Global namespace Global
{ {
bool ShouldShowDebug = false; bool ShouldShowDebug = false;
@ -40,131 +41,88 @@ namespace Memory
} }
bool opts_custom_add(opts* options, opts_entry *t, char* b) struct TokEntry
{ {
if (t->type != ZPL_OPTS_STRING) char const* Str;
{ s32 Length;
return false; };
}
t->text = string_append_length(t->text, " ", 1); ZPL_TABLE( static, TokMap, tokmap_, TokEntry )
t->text = string_appendc( t->text, b );
return true; sw token_fmt_va( char* buf, uw buf_size, char const* fmt, s32 num_tokens, va_list va )
}
b32 opts_custom_compile(opts *options, int argc, char **argv)
{ {
b32 had_errors = false; char const* buf_begin = buf;
sw remaining = buf_size;
for (int i = 1; i < argc; ++i) TokMap tok_map;
{ {
char* arg = argv[i]; tokmap_init( & tok_map, g_allocator );
if (*arg) s32 left = num_tokens;
while ( left-- )
{ {
arg = (char*)str_trim(arg, false); char const* token = va_arg( va, char const* );
char const* value = va_arg( va, char const* );
if (*arg == '-') TokEntry entry
{ {
opts_entry* entry = 0; value,
b32 checkln = false; zpl_strnlen(value, 128)
if ( *(arg + 1) == '-') };
u32 key = crc32( token, zpl_strnlen(token, 32) );
tokmap_set( & tok_map, key, entry );
}
}
sw result = 0;
char current = *fmt;
while ( current )
{ {
checkln = true; sw len = 0;
++arg;
}
char *b = arg + 1, *e = b; while ( current && current != '{' && remaining )
while (char_is_alphanumeric(*e) || *e == '-' || *e == '_') {
++e;
}
entry = zpl__opts_find(options, b, (e - b), checkln);
if (entry)
{ {
char *ob = b; *buf = *fmt;
b = e; buf++;
fmt++;
/**/ current = *fmt;
if (*e == '=')
{
if (entry->type == ZPL_OPTS_FLAG)
{
*e = '\0';
zpl__opts_push_error(options, ob, ZPL_OPTS_ERR_EXTRA_VALUE);
had_errors = true;
continue;
} }
b = e = e + 1; if ( current == '{' )
}
else if (*e == '\0')
{ {
char *sp = argv[i+1]; char const* scanner = fmt;
if (sp && *sp != '-' && (array_count(options->positioned) < 1 || entry->type != ZPL_OPTS_FLAG)) s32 tok_len = 0;
{
if (entry->type == ZPL_OPTS_FLAG)
{
zpl__opts_push_error(options, b, ZPL_OPTS_ERR_EXTRA_VALUE);
had_errors = true;
continue; while ( *scanner != '}' )
{
tok_len++;
scanner++;
} }
arg = sp; char const* token = fmt;
b = e = sp;
++i; s32 key = crc32( token, tok_len );
} TokEntry value = * tokmap_get( & tok_map, key );
else s32 left = value.Length;
while ( left-- )
{ {
if (entry->type != ZPL_OPTS_FLAG) *buf = *value.Str;
{ buf++;
zpl__opts_push_error(options, ob, ZPL_OPTS_ERR_MISSING_VALUE); value.Str++;
had_errors = true;
continue;
} }
entry->met = true; scanner++;
fmt = scanner;
continue; current = *fmt;
} }
} }
e = (char *)str_control_skip(e, '\0'); return result;
zpl__opts_set_value(options, entry, b);
if ( (i + 1) < argc )
{
for ( b = argv[i + 1]; i < argc && b[0] != '-'; i++, b = argv[i + 1] )
{
opts_custom_add(options, entry, b );
}
}
}
else
{
zpl__opts_push_error(options, b, ZPL_OPTS_ERR_OPTION);
had_errors = true;
}
}
else if (array_count(options->positioned))
{
opts_entry *l = array_back(options->positioned);
array_pop(options->positioned);
zpl__opts_set_value(options, l, arg);
}
else
{
zpl__opts_push_error(options, arg, ZPL_OPTS_ERR_VALUE);
had_errors = true;
}
}
}
return !had_errors;
} }

View File

@ -1,35 +1,15 @@
/* /*
BLOAT. BLOAT.
This contians all definitions not directly related to the project.
*/ */
#pragma once #pragma once
#if defined(__GNUC__) || defined(__clang__) || 1
// Supports 0-10 arguments
#define VA_NARGS_IMPL( _0, \
_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
_11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
_21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \
N, ...) N
// ## deletes preceding comma if _VA_ARGS__ is empty (GCC, Clang)
#define VA_NARGS(...) VA_NARGS_IMPL(_, ## __VA_ARGS__, \
30, 29, 28, 27, 26, 25, 24, 23, 22, 21, \
20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, \
0)
#else
// Supports 1-10 arguments
#define VA_NARGS_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
#define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
#endif
#define VA_NARGS2(...) ((int)(sizeof((int[]){ __VA_ARGS__ })/sizeof(int)))
#ifdef BLOAT_IMPL #ifdef BLOAT_IMPL
# define ZPL_IMPLEMENTATION # define ZPL_IMPLEMENTATION
#endif #endif
#pragma region ZPL INCLUDE #pragma region ZPL INCLUDE
#if __clang__ #if __clang__
# pragma clang diagnostic push # pragma clang diagnostic push
@ -47,7 +27,7 @@
// # define ZPL_MODULE_REGEX // # define ZPL_MODULE_REGEX
// # define ZPL_MODULE_EVENT // # define ZPL_MODULE_EVENT
// # define ZPL_MODULE_DLL // # define ZPL_MODULE_DLL
# define ZPL_MODULE_OPTS // # define ZPL_MODULE_OPTS
// # define ZPL_MODULE_PROCESS // # define ZPL_MODULE_PROCESS
// # define ZPL_MODULE_MAT // # define ZPL_MODULE_MAT
// # define ZPL_MODULE_THREADING // # define ZPL_MODULE_THREADING
@ -61,7 +41,6 @@
#pragma endregion ZPL INCLUDE #pragma endregion ZPL INCLUDE
#if __clang__ #if __clang__
# pragma clang diagnostic ignored "-Wunused-const-variable" # pragma clang diagnostic ignored "-Wunused-const-variable"
# pragma clang diagnostic ignored "-Wswitch" # pragma clang diagnostic ignored "-Wswitch"
@ -70,16 +49,41 @@
#endif #endif
#if defined(__GNUC__) || defined(__clang__) || 1
// Supports 0-10 arguments
#define VA_NARGS_IMPL( _0, \
_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
_11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
_21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \
_31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \
_41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \
N, ...) N
// ## deletes preceding comma if _VA_ARGS__ is empty (GCC, Clang)
#define VA_NARGS(...) VA_NARGS_IMPL(_, ## __VA_ARGS__, \
50, 49, 48, 47, 46, 45, 44, 43, 42, 41, \
40, 39, 38, 37, 36, 35, 34, 33, 32, 31, \
30, 29, 28, 27, 26, 25, 24, 23, 22, 21, \
20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, \
0)
#else
// Supports 1-10 arguments
#define VA_NARGS_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
#define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
#endif
#define bit( Value_ ) ( 1 << Value_ ) #define bit( Value_ ) ( 1 << Value_ )
#define bitfield_is_equal( Field_, Mask_ ) ( ( Mask_ & Field_ ) == Mask_ ) #define bitfield_is_equal( Field_, Mask_ ) ( ( Mask_ & Field_ ) == Mask_ )
#define ct constexpr #define ct constexpr
#define forceinline ZPL_ALWAYS_INLINE #define forceinline ZPL_ALWAYS_INLINE
#define print_nl( _) zpl_printf("\n") #define print_nl( _) zpl_printf("\n")
#define ccast( Type_, Value_ ) * const_cast< Type_* >( & Value_ )
#define scast( Type_, Value_ ) static_cast< Type_ >( Value_ ) #define scast( Type_, Value_ ) static_cast< Type_ >( Value_ )
#define rcast( Type_, Value_ ) reinterpret_cast< Type_ >( Value_ ) #define rcast( Type_, Value_ ) reinterpret_cast< Type_ >( Value_ )
#define pcast( Type_, Value_ ) ( * (Type_*)( & Value_ ) ) #define pcast( Type_, Value_ ) ( * (Type_*)( & Value_ ) )
#define txt( Value_ ) ZPL_STRINGIFY_EX( Value_ ) #define txt_impl( Value_ ) #Value_
#define txt( Value_ ) txt_impl( Value_ )
#define txt_with_length( Value_ ) txt_impl( Value_ ), sizeof( txt_impl( Value_) )
#define do_once() \ #define do_once() \
do \ do \
@ -90,7 +94,7 @@ do \
return; \ return; \
Done = true; \ Done = true; \
} \ } \
while(0) \ while(0)
#define do_once_start \ #define do_once_start \
do \ do \
@ -105,14 +109,8 @@ do \
} \ } \
while(0); while(0);
using Line = char*;
using Array_Line = array( Line );
ct char const* Msg_Invalid_Value = "INVALID VALUE PROVIDED"; ct char const* Msg_Invalid_Value = "INVALID VALUE PROVIDED";
namespace Global namespace Global
{ {
extern bool ShouldShowDebug; extern bool ShouldShowDebug;
@ -124,6 +122,8 @@ namespace Memory
extern arena Global_Arena; extern arena Global_Arena;
// #define g_allocator arena_allocator( & Memory::Global_Arena) // #define g_allocator arena_allocator( & Memory::Global_Arena)
// Heap allocator is being used for now to isolate errors from being memory related (tech debt till ready to address)
#define g_allocator heap() #define g_allocator heap()
void setup(); void setup();
@ -131,9 +131,19 @@ namespace Memory
void cleanup(); void cleanup();
} }
// Had to be made to support multiple sub-arguments per "opt" argument. inline
b32 opts_custom_compile(opts *options, int argc, char **argv); char const* token_fmt( char const* fmt, sw num_tokens, ... )
{
local_persist thread_local
char buf[ZPL_PRINTF_MAXLEN] = { 0 };
va_list va;
va_start(va, fmt);
token_fmt_va(buf, ZPL_PRINTF_MAXLEN, fmt, num_tokens, va);
va_end(va);
return buf;
}
inline inline
sw log_fmt(char const *fmt, ...) sw log_fmt(char const *fmt, ...)
@ -152,7 +162,7 @@ sw log_fmt(char const *fmt, ...)
} }
inline inline
void fatal(char const *fmt, ...) sw fatal(char const *fmt, ...)
{ {
local_persist thread_local local_persist thread_local
char buf[ZPL_PRINTF_MAXLEN] = { 0 }; char buf[ZPL_PRINTF_MAXLEN] = { 0 };
@ -165,11 +175,13 @@ void fatal(char const *fmt, ...)
va_end(va); va_end(va);
assert_crash(buf); assert_crash(buf);
return -1;
#else #else
va_start(va, fmt); va_start(va, fmt);
zpl_printf_err_va( fmt, va); zpl_printf_err_va( fmt, va);
va_end(va); va_end(va);
zpl_exit(1); zpl_exit(1);
return -1;
#endif #endif
} }

18
project/Bloat.undef.hpp Normal file
View File

@ -0,0 +1,18 @@
/*
Remvoe any macro definitions related to the Bloat header.
*/
#undef bit
#undef bitfield_is_equal
#undef ct
#undef forceinline
#undef print_nl
#undef scast
#undef rcast
#undef pcast
#undef txt
#undef do_once
#undef do_once_start
#undef do_once_end

File diff suppressed because it is too large Load Diff

View File

@ -1,105 +1,163 @@
/* /*
gencpp: A simple staged metaprogramming library for C++. gencpp: A simple staged metaprogramming library for C++.
This library is intended for small-to midsize projects that want rapid complation times This library is intended for small-to midsize projects.
for fast debugging.
AST type checking supports only a small subset of c++. AST type checking supports only a small subset of c++.
See the 'ECode' namespace and 'gen API' region to see what is supported. See the 'ECode' namespace and 'gen API' region to see what is supported.
There is no support for accessability fields in structs. WHAT IS NOT PROVIDED:
* Macro or template generation : This library is to avoid those, adding support for them
adds unnecessary complexity.
If you desire define them outside the gen_time scopes.
* Modern c++ (STL library) features :
* Expression validation : Execution expressions are defined using the untyped string API.
: There is no parse API for validating expression (possibly will add in the future)
* Complete file parser DSL : This isn't like the unreal header tool.
Code injection to file or based off a file contents is not supported by the api.
However nothing is stopping you using the library for that purpose.
There are four different of construction of Code ast's the library provides:
* Upfront construction
* Incremental construction
* Parse construction
* Untyped
Upfront Construction:
All component ASTs must be previously constructed, and provided on creation of the code AST.
The construction will fail and return InvalidCode otherwise.
API:
* def_forward_decl
* def_class
* def_global_body
* def_proc
* def_proc_body
* def_namespace
* def_namespace_body
* def_param
* def_params
* def_operator
* def_specifier
* def_specifiers
* def_struct
* def_struct_body
* def_variable
* def_type
* def_using
* def_using_namespace
Incremental construction:
A Code ast is provided but only completed upfront if all components are provided.
Components are then added using the AST API for adding ASTs:
* code.add( AST* ) // Adds AST with validation.
* code.add_entry( AST* ) // Adds AST entry without validation.
* code.add_content( AST* ) // Adds AST string content without validation.
API:
* make_forward_decl
* make_class
* make_global_body
* make_proc
* make_namespace
* make_params
* make_operator
* make_specifiers
* make_struct
* make_variable
* make_type
* make_using
* make_using_namespace
Parse construction:
A string provided to the API is parsed for the intended language construct.
API:
* parse_forward_decl
* parse_class
* parse_glboal_body
* parse_proc
* parse_namespace
* parse_params
* parse_operator
* parse_specifiers
* parse_struct
* parse_variable
* parse_type
* parse_using
* parse_using
The parse API treats any execution scope definitions with no validation and are turned into untyped Code ASTs.
This includes the assignmetn of variables; due to the library not yet supporting c/c++ expression parsing.
Untyped constructions:
Code ASTs are constructed using unvalidated strings.
API:
* untyped_str
* untyped_fmt
* untyped_token_fmt
During serialization any untyped Code AST is has its string value directly injected inline of
whatever context the content existed as an entry within.
Even though thse are not validated from somewhat correct c/c++ syntax or components, it doesn't mean that
Untyped code can be added as any component of a Code AST:
* Untyped code cannot have children, thus there cannot be recursive injection this way.
* Untyped code can only be a child of a parent of body AST, or for values of an assignment.
These restrictions help prevent abuse of untyped code to some extent.
*/ */
#pragma once #pragma once
#include "Bloat.hpp" #include "Bloat.hpp"
// Defined by default. // Temporarily here for debugging purposes.
#define GEN_ENABLE_READONLY_AST
// #define GEN_DEFINE_DSL
#define gen_time #define gen_time
#define GEN_ENFORCE_READONLY_AST
// #define GEN_DEFINE_DSL
#define GEN_DEFINE_LIBRARY_CODE_CONSTANTS
// #define GEN_BAN_CPP_TEMPLATES
// #define GEN_USE_FATAL
#ifdef gen_time #ifdef gen_time
namespace gen namespace gen
{ {
#if 0 using LogFailType = sw(*)(char const*, ...);
ct sw ColumnLimit = 256;
ct sw MaxLines = kilobytes(256);
using LineStr = char[ColumnLimit]; #ifdef GEN_BAN_CPP_TEMPLATES
#endif #define template static_assert("Templates are banned within gen_time scope blocks")
#endif
// Specifier Type #ifdef GEN_USE_FATAL
enum Specifier : u8 ct LogFailType log_failure = fatal;
{ #else
Alignas, // alignas(#) ct LogFailType log_failure = log_fmt;
Constexpr, // constexpr #endif
Inline, // inline
C_Linkage, // extern "C"
API_Import, // Vendor specific way dynamic import symbol
API_Export, // Vendor specific way to dynamic export
External_Linkage, // extern
Internal_Linkage, // static (within unit file)
Static_Member, // static (within sturct/class)
Local_Persist, // static (within function)
Thread_Local, // thread_local
Num_Specifiers
};
// Specifier to string
inline
char const* specifier_str( Specifier specifier )
{
static
char const* lookup[ Num_Specifiers ] = {
"alignas",
"constexpr",
"inline",
"extern \"C\"",
#if defined(ZPL_SYSTEM_WINDOWS)
"__declspec(dllexport)",
"__declspec(dllimport)",
#elif defined(ZPL_SYSTEM_MACOS)
"__attribute__ ((visibility ("default")))",
"__attribute__ ((visibility ("default")))",
#endif
"extern",
"static",
"static",
"static",
"thread_local"
};
return lookup[ specifier ];
}
// Code Type
namespace ECode namespace ECode
{ {
enum Type : u8 enum Type : u8
{ {
Invalid, Invalid, // Used only with improperly created Code nodes
Untyped, // User provided raw string
Untyped, // User provided raw string. Decl_Function, // <specifier> <type> <name> ( <params> )
Decl_Type, // <type> <name>;
Decl_Function, // Forward a function
Decl_Type, // Forward a type.
Function, // <type> <name>( <parameters> ) Function, // <type> <name>( <parameters> )
Function_Body, // { <body> } Function_Body, // { <body> }
Namespace, Namespace, // Define a namespace
Namespace_Body, Namespace_Body, // { <body> }
Parameters, // Used with functions. Parameters, // <type> <param> ...
Specifiers, Specifiers, // Used with functions, structs, variables
Struct, Struct, // struct <specifier> <name> <parent>
Struct_Body, Struct_Body, // {<body> }
Variable, Variable, // <type> <name>
Typedef, Typedef, // typedef <type> <alias>
Typename, Typename, // Typename, used with other types
Using, Using, // using <name> = <type>
Unit, // Represents a file.
Num_Types Num_Types
}; };
@ -110,11 +168,9 @@ namespace gen
static static
char const* lookup[Num_Types] = { char const* lookup[Num_Types] = {
"Invalid", "Invalid",
"Untyped", "Untyped",
"Decl_Function", "Decl_Function",
"Decl_type", "Decl_Type",
"Function", "Function",
"Function_Body", "Function_Body",
"Namespace", "Namespace",
@ -126,7 +182,8 @@ namespace gen
"Variable", "Variable",
"Typedef", "Typedef",
"Typename", "Typename",
"using" "Using",
"Unit",
}; };
return lookup[ type ]; return lookup[ type ];
@ -134,6 +191,116 @@ namespace gen
} }
using CodeT = ECode::Type; using CodeT = ECode::Type;
namespace EOperator
{
enum Type : u8
{
Add,
Subtract,
Multiply,
Divide,
Modulo,
Num_Ops
};
inline
char const* str( Type op )
{
static
char const* lookup[ Num_Ops ] = {
"+",
"-",
"*",
"/",
};
return lookup[ op ];
}
}
using OperatorT = EOperator::Type;
namespace ESpecifier
{
enum Type : u8
{
Attribute, // [ <attributes ]
Alignas, // alignas(#)
Constexpr, // constexpr
Const, // const
Inline, // inline
RValue, //
C_Linkage, // extern "C"
API_Import, // Vendor specific way dynamic import symbol
API_Export, // Vendor specific way to dynamic export
External_Linkage, // extern
Internal_Linkage, // static (within unit file)
Static_Member, // static (within sturct/class)
Local_Persist, // static (within function)
Thread_Local, // thread_local
Num_Specifiers,
Invalid
};
// Specifier to string
inline
char const* to_str( Type specifier )
{
static
char const* lookup[ Num_Specifiers ] = {
"alignas",
"constexpr",
"const",
"inline",
"extern \"C\"",
#if defined(ZPL_SYSTEM_WINDOWS) && 0// API_Import and API_Export strings
"__declspec(dllexport)",
"__declspec(dllimport)",
#elif defined(ZPL_SYSTEM_MACOS) || 1
"__attribute__ ((visibility (\"default\")))",
"__attribute__ ((visibility (\"default\")))",
#endif
"extern",
"static",
"static",
"static",
"thread_local"
};
return lookup[ specifier ];
}
Type to_type( char const* str, s32 length )
{
static
u32 keymap[ Num_Specifiers ];
do_once_start
for ( u32 index = 0; index < Num_Specifiers; index++ )
{
char const* enum_str = to_str( (Type)index );
keymap[index] = crc32( enum_str, strnlen(enum_str, 42) );
}
do_once_end
u32 hash = crc32(str, length );
for ( u32 index = 0; index < Num_Specifiers; index++ )
{
if ( keymap[index] == hash )
return (Type)index;
}
return Invalid;
}
}
using SpecifierT = ESpecifier::Type;
// TODO: If perf needs it, convert layout an SOA format. // TODO: If perf needs it, convert layout an SOA format.
/* /*
Simple AST POD with functionality to seralize into C++ syntax. Simple AST POD with functionality to seralize into C++ syntax.
@ -154,38 +321,40 @@ namespace gen
} }
forceinline forceinline
bool has_entries() bool has_entries() const
{ {
static bool lookup[ ECode::Num_Types] = { static bool lookup[ ECode::Num_Types] = {
false, // Invalid false, // Invalid
false, // Unused
false, // Untyped false, // Untyped
true, // Decl_Type
true, // Decl_Function true, // Decl_Function
true, // Parameter true, // Decl_Type
true, // Struct
true, // Function true, // Function
false, // Specifier true, // Parameters
false, // Specifies
true, // Struct
true, // Struct_Body
true, // Variable true, // Variable
true, // Typedef
true, // Typename true, // Typename
true, // Using
}; };
return lookup[Type]; return lookup[Type];
} }
forceinline forceinline
bool is_invalid() bool is_invalid() const
{ {
return Type != ECode::Invalid; return Type != ECode::Invalid;
} }
forceinline forceinline
char const* type_str() char const* type_str() const
{ {
return ECode::str( Type ); return ECode::str( Type );
} }
string to_string(); string to_string() const;
#pragma endregion Member API #pragma endregion Member API
@ -215,7 +384,7 @@ namespace gen
/* /*
AST* typedef as to not constantly have to add the '*' as this is written often.. AST* typedef as to not constantly have to add the '*' as this is written often..
If GEN_ENABLE_READONLY_AST is defined, readonly assertions will be done on any member dreference, If GEN_ENFORCE_READONLY_AST is defined, readonly assertions will be done on any member dreference,
and the 'gen API' related functions. will set their created ASTs to readonly before returning. and the 'gen API' related functions. will set their created ASTs to readonly before returning.
Casting to AST* will bypass. Casting to AST* will bypass.
@ -224,13 +393,32 @@ namespace gen
{ {
AST* ast; AST* ast;
forceinline Code body()
operator bool()
{ {
return ast->is_invalid(); if ( ast->Type == ECode::Invalid )
fatal("Code::body: Type is invalid, cannot get");
if ( ast->Entries == nullptr || array_count(ast->Entries) == 0 )
fatal("Code::body: Entries of ast not properly setup.");
return pcast( Code, ast->Entries[0]);
} }
bool operator ==( Code other ) forceinline
void lock()
{
#ifdef GEN_ENFORCE_READONLY_AST
ast->Readonly = true;
#endif
}
forceinline
operator bool() const
{
return ast && ast->is_invalid();
}
bool operator ==( Code other ) const
{ {
return ast == other.ast; return ast == other.ast;
} }
@ -247,28 +435,27 @@ namespace gen
return *this; return *this;
} }
#ifdef GEN_ENABLE_READONLY_AST
forceinline forceinline
AST* operator ->() AST* operator ->()
{ {
#ifdef GEN_ENFORCE_READONLY_AST
if ( ast == nullptr ) if ( ast == nullptr )
fatal("Attempt to dereference a nullptr!"); fatal("Attempt to dereference a nullptr!");
if ( ast->Readonly ) if ( ast->Readonly )
fatal("Attempted to access a member from a readonly ast!"); fatal("Attempted to access a member from a readonly ast!");
#endif
return ast; return ast;
} }
Code& operator *() = delete;
#endif
}; };
static_assert( sizeof(Code) == sizeof(AST*), "ERROR: Code is not POD" );
// Used when the its desired when omission is allowed in a definition. // Used when the its desired when omission is allowed in a definition.
ct Code UnusedCode = { nullptr }; ct Code UnusedCode = { nullptr };
// Used internally for the most part to identify invaidly generated code. // Used internally for the most part to identify invaidly generated code.
ct CodePOD InvalidCode = { ECode::Invalid, false, nullptr, nullptr, nullptr, { nullptr } }; extern const Code InvalidCode;
/* /*
Type registy: Used to store Typename ASTs. Types are registered by their string literal value. Type registy: Used to store Typename ASTs. Types are registered by their string literal value.
@ -277,7 +464,7 @@ namespace gen
Strings made with the Typename ASTs are stored in thier own arena allocator. Strings made with the Typename ASTs are stored in thier own arena allocator.
TODO: Implement and replace usage of def_type. TODO: Implement and replace usage of def_type.
*/ */
// ZPL_TABLE_DECLARE( ZPL_EXTERN, TypeRegistry, type_reg_, Code ); ZPL_TABLE_DECLARE( ZPL_EXTERN, TypeRegistry, type_reg_, Code );
#pragma region gen API #pragma region gen API
/* /*
@ -286,17 +473,18 @@ namespace gen
*/ */
void init(); void init();
#pragma region Upfront
/* /*
Foward Declare a type: Foward Declare a type:
<specifiers> <type> <name>; <specifiers> <type> <name>;
*/ */
Code decl_type( char const* name, Code type, Code specifiers = UnusedCode ); Code def_fwd_type( Code type, char const* name, Code specifiers = UnusedCode );
/* /*
Foward Declare a function: Foward Declare a function:
<specifiers> <name> ( <params> ); <specifiers> <name> ( <params> );
*/ */
Code decl_fn( char const* name Code def_fwd_proc( char const* name
, Code specifiers , Code specifiers
, Code params , Code params
, Code ret_type , Code ret_type
@ -305,8 +493,10 @@ namespace gen
/* /*
Define an expression: Define an expression:
< c/c++ expression > < c/c++ expression >
TODO: Evalute if you want to validiate at the execution layer during gen_time (dosen't seem necessary)
*/ */
Code def_expression( Code value ); // Code def_expression( Code value );
/* /*
Define a function: Define a function:
@ -315,7 +505,7 @@ namespace gen
<body> <body>
} }
*/ */
Code def_function( char const* name Code def_proc( char const* name
, Code specifiers , Code specifiers
, Code params , Code params
, Code ret_type , Code ret_type
@ -330,9 +520,10 @@ namespace gen
... ...
} }
Each entry is provided an empty line separation. There will be an empty line separation between entires
*/ */
Code def_function_body( s32 num, ... ); Code def_proc_body( s32 num, ... );
Code def_proc_body( s32 num, Code* codes );
/* /*
Define a namespace; Define a namespace;
@ -351,7 +542,7 @@ namespace gen
... ...
} }
Each entry is provided an empty line separation. There will be an empty line separation between entires
*/ */
Code def_namespace_body( s32 num, ... ); Code def_namespace_body( s32 num, ... );
@ -359,12 +550,23 @@ namespace gen
Define a set of parameters for a function: Define a set of parameters for a function:
<name> <type>, ... <name> <type>, ...
*/ */
Code def_parameters( s32 num, ... ); Code def_params( s32 num, ... );
Code def_params( s32 num, char const** params );
/*
Define an operator definition.
*/
Code def_operator( OperatorT op, Code specifiers, Code params, Code ret_type, Code body );
/* /*
Define a set of specifiers for a function, struct, type, or varaible Define a set of specifiers for a function, struct, type, or varaible
Note:
If alignas is specified the procedure expects the next argument to be the alignment value.
If attribute is specified the procedure expects the next argument to be its content as a string.
*/ */
Code def_specifiers( s32 num , ... ); Code def_specifiers( s32 num , ... );
Code def_specifiers( s32 num, SpecifierT* specs );
/* /*
Define a struct: Define a struct:
@ -383,19 +585,26 @@ namespace gen
... ...
} }
Each entry is provided an empty line separation. There will be an empty line separation between entires
*/ */
Code def_struct_body( s32 num, ... ); Code def_struct_body( s32 num, ... );
Code def_struct_body( s32 num, Code* codes );
/* /*
Define a variable: Define a variable:
<specifiers> <type> <name> = <value>; <specifiers> <type> <name> = <value>;
*/ */
Code def_variable( char const* name, Code type, Code value = UnusedCode, Code specifiers = UnusedCode ); Code def_variable( Code type, char const* name, Code value = UnusedCode, Code specifiers = UnusedCode );
/* /*
Define a type AST value. Define a typename AST value.
Useless by itself, its intended to be used in conjunction with Useless by itself, its intended to be used in conjunction with other Code.
Planned - Not yet Implemented:
Typename Codes are not held in the CodePool, instead they are stored in a
type registry (hastable where the key is a crc hash of the name string).
If a key exists the existing code value will be provided.
*/ */
Code def_type( char const* name ); Code def_type( char const* name );
@ -412,7 +621,88 @@ namespace gen
Can only be used in either a Can only be used in either a
*/ */
Code def_using_namespace( char const* name ); Code def_using_namespace( char const* name );
#pragma endregion Upfront
#pragma region Incremental
/*
Provides an incomplete procedure AST but sets the intended type.
Any adds will be type checked.
Body is automatically made. Use body() to retrieve.
*/
Code make_proc( char const* name
, Code specifiers = UnusedCode
, Code params = UnusedCode
, Code ret_type = UnusedCode
);
/*
Provides an incomplete struct AST but sets the intended type.
Any adds will be type checked.
Body is automatically made. Use body() to retrieve.
*/
Code make_struct( char const* name, Code parent = UnusedCode, Code specifiers = UnusedCode );
/*
Creates a unit file.
These represent an encapsulation of a generated file
Used this if you need to pass around a group of Code entires at file scope level.
The name provided is the name of the file.
*/
Code make_file_body( char const* name );
#pragma endregion Incremental
/*
*/
Code parse_variable( char const* var_def, s32 length );
/*
*/
Code parse_using( char const* using_def, s32 length );
/*
*/
Code parse_operator( char const* operator_def, s32 length );
/*
Define a procedure by parsing a string.
Note: This parser only supports the language features the library supports
Any other features used and the lex or parse operation will fail.
This is not a full-on c/c++ parser, it literally only grabs
what it needs to reconstruct the Code AST for seralization in the
builder, nothing else.
*/
Code parse_proc( char const* proc_def, s32 length );
/*
Define a struct by parsing a string.
Note: This parser only supports the language features the library supports
Any other features used and the lex or parse operation will fail.
This is not a full-on c/c++ parser, it literally only grabs
what it needs to reconstruct the Code AST for seralization in the
builder, nothing else.
*/
Code parse_struct( char const* struct_def, s32 length );
/*
*/
s32 parse_vars( char const* vars_def, s32 length, Code* out_vars_codes );
/*
*/
s32 parse_usings( char const* usings_def, s32 length, Code* out_usings_codes );
#pragma region Untyped text
/* /*
Define an untyped code string. Define an untyped code string.
@ -423,6 +713,7 @@ namespace gen
Consider this an a preprocessor define. Consider this an a preprocessor define.
*/ */
Code untyped_str( char const* str ); Code untyped_str( char const* str );
Code untyped_str( char const* str, s32 length);
/* /*
Define an untyped code string using traditional 'printf'. Define an untyped code string using traditional 'printf'.
@ -449,18 +740,8 @@ namespace gen
Because the code within it is untyped, errors will naturally not be provided. Because the code within it is untyped, errors will naturally not be provided.
Consider this an a preprocessor define. Consider this an a preprocessor define.
*/ */
Code token_fmt( char const* fmt, s32 num_tokens, ... ); Code untyped_token_fmt( char const* fmt, s32 num_tokens, ... );
#pragma endregion Untyped text
/*
Creates a unit file.
These represent an encapsulation of a generated file
Used this if you need to pass around a group of Code entires at file scope level.
The name provided is the name of the file.
*/
Code create_Unit( char const* name );
/* /*
Used to generate the files. Used to generate the files.
@ -492,62 +773,93 @@ namespace gen
#pragma region MACROS #pragma region MACROS
# define gen_main main # define gen_main main
# define __ UnusedCode # define __ UnusedCode
/* /*
gen's Domain Specific Langauge. gen's Domain Specific Langauge.
Completely optional, makes the code gen syntax less verbose.. Completely optional, makes the code gen syntax less verbose and cumbersome...
Since its C macros ends up looking like a lisp dialect...
Longforms auto-define the variable.
Shorthands are just the function call.
Anything below the make() macro is intended to be syntacticall used int he follwing format:
make( <type>, <name> )
{
...
}
Where ... are whatever is deemed necessary to produce the definition for the def( <name> ).
The code macros are used to embed c/c++ to insert into the desired lcoation.
*/ */
#ifdef GEN_DEFINE_DSL #ifdef GEN_DEFINE_DSL
# define type( Name_, Value_ ) Code Name_ = gen::def_type( txt(Value_) ) # define untyped_code( Name_, Value_ ) Code Name_ = gen::untyped_str( txt(Value_) )
# define type_fmt( Name_, Fmt_, ... ) Code Name_ = gen::def_type( bprintf( Fmt_, __VA_ARGS__ ) ) # define typename( Name_, Value_ ) Code t_##Name_ = gen::def_type( txt(Value_) )
# define value( Name_, Value_ ) Code Name_ = gen::untyped_str( Value_ ) # define typename_fmt( Name_, Fmt_, ... ) Code t_##Name_ = gen::def_type( bprintf( Fmt_, __VA_ARGS__ ) )
# define specifiers( Name_, ... ) Code Name_ = gen::def_specifiers( VA_NARGS( __VA_ARGS__ ), __VA_ARGS__ ) # define using_type( Name_, Type_ ) Code Name_ = gen::def_using( #Name_, t_##Type_ )
# define using( Name_, Type_ ) Code Name_ = gen::def_using( #Name_, Type_ ) # define variable( Type_, Name_, ... ) Code Name_ = gen::def_variable( t_##Type_, #Name_, __VA_ARGS__ )
# define var( Name_, Type_, Value_, Specifiers_ ) \ # define untyped( Value_ ) gen::untyped_str( txt(Value_) )
Code Name_ = gen::def_variable( #Name_, Type_, untyped_str( #Value_ ), Specifiers_ ) # define code_token( Fmt_, ... ) gen::untyped_token_fmt( Fmt_, VA_NARGS( __VA_ARGS__), __VA_ARGS__ )
# define code_fmt( Fmt_, ... ) gen::untyped_fmt( Fmt_, __VA_ARGS__ )
# define specifiers( ... ) gen::def_specifiers( VA_NARGS( __VA_ARGS__ ), __VA_ARGS__ )
# define type( Value_ ) gen::def_type( txt(Value_) )
# define type_fmt( Fmt_, ... ) gen::def_type( bprintf( Fmt_, __VA_ARGS__ ) )
# define using( Name_, Type_ ) gen::def_using( #Name_, Type_ )
# define var( Type_, Name_, ... ) gen::def_variable( Type_, #Name_, __VA_ARGS__ )
// # define def ( Name _ ) Code Name_; # define make( Type_, Name_, ... ) Code Name_ = make_##Type_( #Name_, __VA_ARGS__ );
# define proc( Name_, Specifiers_, RetType_, Parameters_, Body_ ) Name_ = gen::def_proc( #Name_, Specifiers_, Parameters_, RetType_, Body_ )
# define proc_body( ... ) gen::def_proc_body( VA_NARGS( __VA_ARS__ ), __VA_ARGS__ )
# define params( ... ) gen::def_params( VA_NARGS( __VA_ARGS__ ) / 2, __VA_ARGS__ )
# define struct( Name_, Parent_, Specifiers_, Body_ ) Name_ = gen::def_struct( #Name_, Body_, Parent_, Specifiers_ )
# define struct_body( ... ) gen::def_struct_body( VA_NARGS( __VA_ARGS__ ), __VA_ARGS__ )
# define params( ... ) gen::def_parameters( VA_NARGS( __VA_ARGS__ ) / 2, __VA_ARGS__ ) # define add_var( Type_, Name_, ... ) add( gen::def_variable( t_##Type_, #Name_, __VA_ARGS__ ) )
# define add_untyped( Value_ ) add( gen::untyped_str( txt( Value ) ) )
# define add_ret_type( ... )
# define add_params( ... )
/* # define proc_code( Def_ ) gen::parse_proc( txt( Def_ ), sizeof( txt( Def_ )) )
Defines scoped symbol. # define struct_code( Def_ ) gen::parse_struct( txt( Def_ ), sizeof( txt( Def_ )) )
Used with:
- function
- namespace
- struct
*/
# define def( Name_ ) Code Name_;
# define function( Name_, Specifiers_, ReturnType_, Parameters_, Body_ ) \
Name_ = gen::def_function( #Name_, Specifiers_, Parameters_, ReturnType_, Body_ )
# define function_body( ... ) \
gen::def_function_body( VA_NARGS( __VA_ARS__ ), __VA_ARGS__ )
# define struct( Name_, Parent_, Specifiers_, Body_ ) \
Name_ = gen::def_struct( #Name_, Body_, Parent_, Specifiers_ )
# define struct_body( ... ) \
gen::def_struct_body( VA_NARGS( __VA_ARGS__ ), __VA_ARGS__ )
#endif #endif
#pragma endregion MACROS #pragma endregion MACROS
#pragma region CONSTANTS #pragma region CONSTANTS
#ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS
namespace gen namespace gen
{ {
// Predefined typename codes. // Predefined typename codes.
// These are not set until gen::init is called.
// This just preloads a bunch of Code types into the code pool.
extern const Code t_void;
extern const Code t_bool; extern const Code t_bool;
extern const Code t_char;
extern const Code t_wchar_t;
extern const Code t_s8;
extern const Code t_s16;
extern const Code t_s32;
extern const Code t_s64;
extern const Code t_u8;
extern const Code t_u16;
extern const Code t_u32;
extern const Code t_u64;
extern const Code t_sw; extern const Code t_sw;
extern const Code t_uw; extern const Code t_uw;
extern const Code t_f32;
extern const Code t_f64;
extern const Code spec_constexpr;
extern const Code spec_inline; extern const Code spec_inline;
} }
#endif
#pragma endregion CONSTANTS #pragma endregion CONSTANTS
#endif #endif

View File

@ -1,5 +1,8 @@
/* /*
This is based of the array container implementation in the zpl.h library. This is based of the array container implementation in the zpl.h library.
This specific header has two implementations of the array generator;
One showing use of the gen api directly, the other using it's DSL.
*/ */
#pragma once #pragma once
@ -8,21 +11,18 @@
#include "gen.hpp" #include "gen.hpp"
#ifdef gen_time #ifdef gen_time
using namespace gen;
Code gen__array_base() Code gen__array_base()
{ {
// Make these global consts to be accessed anywhere... #ifndef GEN_DEFINE_DSL
Code t_sw = def_type( txt(sw) ); using namespace gen;
Code t_uw = def_type( txt(uw) );
Code t_allocator = def_type( txt(allocator) ); Code t_allocator = def_type( txt(allocator) );
#ifndef GEN_DEFINE_DSL
Code header; Code header;
{ {
Code num = def_variable( "Num", t_uw ); Code num = def_variable( t_uw, "Num" );
Code capacity = def_variable( "Capacity", t_uw ); Code capacity = def_variable( t_uw, "Capacity" );
Code allocator_var = def_variable( "Allocator", t_allocator ); Code allocator_var = def_variable( t_allocator, "Allocator" );
Code header_body = def_struct_body( 3, num, capacity, allocator_var ); Code header_body = def_struct_body( 3, num, capacity, allocator_var );
header = def_struct( "ArrayHeader", header_body ); header = def_struct( "ArrayHeader", header_body );
@ -30,36 +30,35 @@
Code grow_formula; Code grow_formula;
{ {
Code spec = def_specifiers(1, Specifier::Inline); Code spec = def_specifiers(1, ESpecifier::Inline);
Code params = def_parameters(1, t_uw, "value" ); Code params = def_params(1, t_uw, "value" );
Code body = untyped_str( "return 2 * value * 8;" ); Code body = untyped_str( txt( return 2 * value * 8; ) );
grow_formula = def_function( "grow_formula", spec, params, t_sw, body ); grow_formula = def_proc( "grow_formula", spec, params, t_sw, body );
} }
Code body = def_struct_body(2, header, grow_formula); Code body = def_struct_body(2, header, grow_formula);
Code ArrayBase = def_struct( "ArrayBase", body ); Code ArrayBase = def_struct( "ArrayBase", body );
#else #else
def( ArrayBase ) typename( allocator, allocator );
def ( Header )
Code Header;
{ {
var( Num, t_uw, __, __ ); variable( uw, Num, );
var( Capacity, t_uw, __, __); variable( uw, Capacity );
var( Allocator, t_allocator, __, __); variable( allocator, Allocator );
Code body = struct_body( Num, Capacity, Allocator ); Code body = struct_body( Num, Capacity, Allocator );
struct( Header, __, __, body ); struct( Header, __, __, body );
} }
def( grow_formula ) Code proc( grow_formula, spec_inline, t_uw, params( t_uw, "value" ),
{ untyped( return 2 * value * 8 )
function( grow_formula, spec_inline, t_uw, params( t_uw, "value" ), untyped_str("return 2 * value * 8") ); );
}
Code body = struct_body( Header, grow_formula ); Code struct( ArrayBase, __, __, struct_body( Header, grow_formula ) );
struct( ArrayBase, __, __, body );
#endif #endif
return ArrayBase; return ArrayBase;
@ -70,16 +69,11 @@
{ {
#ifndef GEN_DEFINE_DSL #ifndef GEN_DEFINE_DSL
// Make these global consts to be accessed anywhere... // Make these global consts to be accessed anywhere...
Code t_uw = def_type( txt(uw) );
Code t_sw = def_type( txt(sw) );
Code t_bool = def_type( txt(bool) );
Code t_allocator = def_type( txt(allocator) ); Code t_allocator = def_type( txt(allocator) );
Code t_void = def_type( txt(void) );
Code v_nullptr = untyped_str( "nullptr" ); Code v_nullptr = untyped_str( "nullptr" );
Code spec_ct = def_specifiers(1, Specifier::Constexpr ); Code spec_ct = def_specifiers(1, ESpecifier::Constexpr );
Code spec_inline = def_specifiers(1, Specifier::Inline );
Code type = def_type( type_str ); Code type = def_type( type_str );
Code ptr_type = def_type( bprintf( "%s*", type_str ) ); Code ptr_type = def_type( bprintf( "%s*", type_str ) );
@ -93,44 +87,50 @@
Code array_def; 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 data = def_variable( ptr_type, "Data" );
// This getter is used often in all the member procedures
Code header = def_variable( ref_header, "header", untyped_str( txt( get_header() )));
Code init; Code init;
{ {
Code params = def_parameters( 1, t_allocator, "mem_handler" ); Code params = def_params( 1, t_allocator, "mem_handler" );
Code body = untyped_str( "return init_reserve( mem_handler, grow_formula(0) );" ); Code body = untyped_str( txt( return init_reserve( mem_handler, grow_formula(0) ); ) );
init = def_function( "init", UnusedCode, params, t_bool, body ); init = def_proc( "init", UnusedCode, params, t_bool, body );
} }
Code init_reserve; Code init_reserve;
{ {
Code params = def_parameters( 2, t_allocator, "mem_handler", t_sw, "capacity" ); Code params = def_params( 2, t_allocator, "mem_handler", t_sw, "capacity" );
Code body; Code body;
{ {
Code header_value = untyped_str( Code header_value = untyped_str( txt(
"rcast( Header*, alloc( mem_handler, sizeof( Header ) + sizeof(Type) + capacity ))" rcast( Header*, alloc( mem_handler, sizeof( Header ) + sizeof(Type) + capacity ))
); ));
Code header = def_variable( "header", ptr_header, header_value );
Code null_check = untyped_str( Code header = def_variable( ptr_header, "header", header_value );
"if (header == nullptr)"
"\n" "return false;"
);
Code header_init = untyped_str( Code null_check = untyped_str( txt(
"header->Num = 0;" if (header == nullptr)
"\n""header->Capacity = capacity;" return false;
"\n""header->Allocator = mem_handler;" ));
);
Code assign_data = untyped_str( Code header_init = untyped_str( txt(
header->Num = 0;
header->Capacity = capacity;
header->Allocator = mem_handler;
));
Code assign_data = untyped_fmt(
"Data = rcast( %s, header + 1 );", ptr_type "Data = rcast( %s, header + 1 );", ptr_type
); );
Code ret_true = untyped_str( "\t""return true" ); Code ret_true = untyped_str( txt(
return true
));
body = def_function_body( 5 body = def_proc_body( 5
, header , header
, null_check , null_check
, header_init , header_init
@ -139,135 +139,125 @@
); );
} }
init_reserve = def_function( "init_reserve", UnusedCode, params, t_bool, body ); init_reserve = def_proc( "init_reserve", UnusedCode, params, t_bool, body );
} }
Code free; Code free;
{ {
Code body = untyped_str( Code body = untyped_str( txt(
"Header& header = get_header();" Header& header = get_header();
"\n""::free( header.Allocator, & get_header() );" ::free( header.Allocator, & get_header() );
); ));
free = def_function( "free", UnusedCode, UnusedCode, t_void, body ); free = def_proc( "free", UnusedCode, UnusedCode, t_void, body );
} }
Code append; Code append = make_proc( "append" );
{ {
Code params = def_parameters( 1, type, "value" ); append->add( def_params( 1, type, "value") );
Code body;
{
Code header = def_variable( "header", ref_header, untyped_str( "get_header()") );
Code check_cap = untyped_str( Code
"if ( header.Capacity < header.Num + 1 )" body = append.body();
"\n" "if ( ! grow(0) )" body->add(
"\n" "return false;" untyped_str( txt(
if ( header.Capacity < header.Num + 1 )
if ( ! grow(0) )
return false;
))
); );
Code assign = untyped_str( body->add( untyped_str( txt(
"Data[ header.Num ] = value;" Data[ header.Num ] = value;
"\n" "header.Num++;" header.Num++;
"\n"
"\n" "return true;"
);
body = def_function_body( 3, header, check_cap, assign ); return true;
} )));
append = def_function( "append", UnusedCode, params, t_void, body );
} }
Code back; Code back;
{ {
Code body = untyped_str( Code body = untyped_str( txt(
"Header& header = get_header();" Header& header = get_header();
"\n" "return data[ header.Num - 1 ];"
);
back = def_function( "back", UnusedCode, UnusedCode, type, body ); return data[ header.Num - 1 ];
));
back = def_proc( "back", UnusedCode, UnusedCode, type, body );
} }
Code clear; Code clear;
{ {
Code body = untyped_str( "get_header().Num = 0;" ); Code body = untyped_str( txt( get_header().Num = 0; ));
clear = def_function( "clear", UnusedCode, UnusedCode, t_void, body ); clear = def_proc( "clear", UnusedCode, UnusedCode, t_void, body );
} }
Code fill; Code fill;
{ {
Code params = def_parameters( 3, t_uw, "begin", t_uw, "end", type, "value" ); Code params = def_params( 3, t_uw, "begin", t_uw, "end", type, "value" );
Code body; Code body;
{ {
Code header = def_variable( "header", ref_header, untyped_str( "get_header()") ); Code check = untyped_str( txt(
if ( begin < 0 || end >= header.Num )
fatal( "Range out of bounds" );
));
Code check = untyped_str( Code iter = untyped_str( txt(
"if ( begin < 0 || end >= header.Num )" for ( sw index = begin; index < end; index++ )
"\n" "fatal( \"Range out of bounds\" );" Data[index] = vallue;
); ));
Code iter = untyped_str( body = def_proc_body( 3, header, check, iter );
"for ( sw index = begin; index < end; index++ )"
"\n" "Data[index] = vallue;"
);
body = def_function_body( 3, header, check, iter );
} }
fill = def_function( "fill", UnusedCode, params, t_void, body ); fill = def_proc( "fill", UnusedCode, params, t_void, body );
} }
Code get_header; Code get_header;
{ {
Code body = untyped_str( "return pcast( Header, Data - 1 );" ); Code body = untyped_str( txt( return pcast( Header, Data - 1 ); ));
get_header = def_function( "get_header", spec_inline, UnusedCode, ref_header, body ); get_header = def_proc( "get_header", spec_inline, UnusedCode, ref_header, body );
} }
Code grow; Code grow;
{ {
Code param = def_parameters( 1, t_uw, "min_capacity" ); Code param = def_params( 1, t_uw, "min_capacity" );
Code body; Code body;
{ {
Code header = def_variable( "header", ref_header, untyped_str("get_header()") ); Code new_capacity = def_variable( t_uw, "new_capacity", untyped_str("grow_formula( header.Capacity )") );
Code new_capacity = def_variable( "new_capacity", t_uw, untyped_str("grow_formula( header.Capacity )") );
Code check_n_set = untyped_str( Code check_n_set = untyped_str( txt(
"if ( new_capacity < min_capacity )" if ( new_capacity < min_capacity )
"\n" "new_capacity = min_capacity;" new_capacity = min_capacity;
); ));
Code ret = untyped_str( "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 ); body = def_proc_body( 4, header, new_capacity, check_n_set, ret );
} }
grow = def_function( "grow", UnusedCode, param, t_bool, body ); grow = def_proc( "grow", UnusedCode, param, t_bool, body );
} }
Code pop; Code pop;
{ {
Code body; Code body;
{ {
Code header = def_variable( "header", ref_header, untyped_str("get_header()") );
Code assertion = untyped_str( "assert( header.Num > 0 );" ); Code assertion = untyped_str( "assert( header.Num > 0 );" );
Code decrement = untyped_str( "header.Num--; " ); Code decrement = untyped_str( "header.Num--; " );
body = def_function_body( 3, header, assertion, decrement ); body = def_proc_body( 3, header, assertion, decrement );
} }
pop = def_function( "pop", UnusedCode, UnusedCode, t_void, body ); pop = def_proc( "pop", UnusedCode, UnusedCode, t_void, body );
} }
Code reserve; Code reserve;
{ {
Code params = def_parameters( 1, t_uw, "new_capacity" ); Code params = def_params( 1, t_uw, "new_capacity" );
Code body; Code body;
{ {
Code header = def_variable( "header", ref_header, untyped_str("get_header()") );
Code check_n_set = untyped_str( Code check_n_set = untyped_str(
"if ( header.Capacity < new_capacity )" "if ( header.Capacity < new_capacity )"
"\n" "return set_capacity( new_capacity );" "\n" "return set_capacity( new_capacity );"
@ -275,80 +265,65 @@
Code ret = untyped_str( "\t" "return true" ); Code ret = untyped_str( "\t" "return true" );
body = def_function_body( 3, header, check_n_set, ret ); body = def_proc_body( 3, header, check_n_set, ret );
} }
reserve = def_function( "reserve", UnusedCode, params, t_bool, body ); reserve = def_proc( "reserve", UnusedCode, params, t_bool, body );
} }
Code resize; Code resize;
{ {
Code param = def_parameters( 1, t_uw, "new_num" ); Code param = def_params( 1, t_uw, "new_num" );
Code body; Code body;
{ {
Code header = def_variable( "header", ref_header, untyped_str("get_header()") ); Code check_n_grow = untyped_str( txt(
if ( header.Capacity < new_num )
Code check_n_grow = untyped_str( if ( ! grow( new_num) )
"if ( header.Capacity < new_num )" return false;
"\n" "if ( ! grow( new_num) )" ));
"\n" "return false;"
);
Code set_n_ret = untyped_str( Code set_n_ret = untyped_str(
"header.Count = new_num;" "header.Count = new_num;"
"\n""return true;" "\n""return true;"
); );
body = def_function_body( 3, header, check_n_grow, set_n_ret ); body = def_proc_body( 3, header, check_n_grow, set_n_ret );
} }
resize = def_function( "resize", UnusedCode, param, t_bool, body ); resize = def_proc( "resize", UnusedCode, param, t_bool, body );
} }
Code set_capacity; Code set_capacity = parse_proc( txt_with_length(
bool set_capacity( new_capacity )
{ {
Code param = def_parameters( 1, t_uw, "capacity" ); Header& header = get_header();
Code body; if ( capacity == header.Capacity )
{ return true;
Code header = def_variable( "header", ref_header, untyped_str("get_header()") );
Code checks = untyped_str( if ( capacity < header.Num )
"if ( capacity == header.Capacity )" header.Num = capacity;
"\n" "return true;"
"if ( capacity < header.Num )" uw size = sizeof(Header) + sizeof(Type) * capacity;
"\n" "header.Num = capacity;" Header* new_header = rcast( Header* alloc( header.Allocator, size ));
);
Code size = def_variable( "size", t_uw, untyped_str("sizeof(Header) + sizeof(Type) * capacity")); if ( new_header == nullptr )
Code new_header = def_variable( "new_header", ptr_header, untyped_str("rcast( Header*, alloc( header.Allocator, size ));")); return false;
Code check_n_move = untyped_str( memmove( new_header, & header, sizeof( Header ) + sizeof(Type) * header.Num );
"if ( new_header == nullptr )"
"\n" "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->Allocator = header.Allocator;" new_header->Num = header.Num;
"\n" "new_header->Num = header.Num;" new_header->Capacity = header.Capacity;
"\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 ); free( header );
}
*Data = new_header + 1;
set_capacity = def_function( "set_capacity", UnusedCode, param, t_bool, body );
return true;
} }
));
string name = bprintf( "Array_%s", type_str ); string name = bprintf( "Array_%s", type_str );
@ -374,256 +349,219 @@
array_def = def_struct( name, body, parent ); array_def = def_struct( name, body, parent );
} }
#else #else
type( t_uw, uw ); typename( allocator, allocator );
type( t_sw, sw );
type( t_bool, bool );
type( t_allocator, allocator );
type( t_void, void );
value( v_nullptr, nullptr ); untyped( v_nullptr, nullptr );
specifiers( spec_ct, Specifier::Constexpr ); typename( elem_type, type_str );
specifiers( spec_inline, Specifier::Inline ); typename_fmt( ptr_type, "%s*", type_str );
typename_fmt( ref_type, "%&", type_str );
type_fmt( type, type_str, __);
type_fmt( ptr_type, "%s*", type_str );
type_fmt( ref_type, "%&", type_str );
// From ArrayBase // From ArrayBase
type( t_header, Header ); typename( header, Header );
type( ptr_header, Header* ); typename( ptr_header, Header* );
type( ref_header, Header& ); typename( ref_header, Header& );
def( array_def ) Code array_def;
{ {
using( Type, type ); using_type( Type, elem_type );
var( Data, ptr_type, __, __ );
def( init ) variable( ptr_type, Data );
variable( ref_header, header, untyped_str("get_header()") );
Code init;
{ {
Code body = function_body( untyped_str("return init_reserve( mem_handler, grow_formula(0) );" )); Code body = proc_body( untyped(
return init_reserve( mem_handler, grow_formula(0) );
));
function( init, __, t_bool, params( t_allocator, "mem_handler" ), body ); proc( init, __, t_bool, params( t_allocator, "mem_handler" ), body );
} }
def( init_reserve ) make( proc, init_reserve, __, params( t_ref_type, "mem_handler" ), t_bool);
{ {
def( body ) Code
{ body = init_reserve.body();
var(header, ptr_header, untyped_str("rcast( Header*, alloc( mem_handler, sizeof(Header) + sizeof(Type) + capacity))" ), __); body->add_var( ptr_header, header, untyped(
value_str( rcast( Header*, alloc( mem_handler, sizeof(Header) + sizeof(Type) + capacity)) )
) );
Code null_check = untyped_str( body->add_untyped(
"if (header == nullptr)" if (header == nullptr)
"\n" "return false;" return false;
); );
Code header_init = untyped_str( body->add_untyped(
"header->Num = 0;" header->Num = 0;
"\n""header->Capacity = capacity;" header->Capacity = capacity;
"\n""header->Allocator = mem_handler;" header->Allocator = mem_handler;
); );
Code assign_data = untyped_str( "Data = rcast( Type*, header + 1 );" ); body->add_untyped(
Code ret_true = untyped_str( "return true" ); Data = rcast( Type*, header + 1 );
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); Code free;
}
def( free )
{ {
Code body = untyped_str( proc( free, __, t_void, __, untyped(
"Header& header = get_header();" Header& header = get_header();
"\n""free( header.Allocator, & get_header() );"
free( header.Allocator, & get_header() );
));
}
make( proc, append )
{
append->add_params( t_elem_value, "value" );
append->add_ret_type( void );
Code
body = append.body();
body->add_untyped(
if ( header.Capacity < header.Num + 1 )
if ( ! grow(0) )
return false;
); );
function( free, __, t_void, __, body ); body->add_untyped( assign,
Data[ header.Num ] = value;
header.Num++;
return true;
);
} }
def( append ) Code back;
{ {
def( body ) Code body = untyped(
{ Header& header = get_header();
var( header, ref_header, untyped_str("get_header()"), __ );
Code check_cap = untyped_str( return data[ header.Num - 1 ];
"if ( header.Capacity < header.Num + 1 )"
"\n" "if ( ! grow(0) )"
"\n" "return false;"
); );
Code assign = untyped_str( proc( back, __, t_elem_type, __, body );
"Data[ header.Num ] = value;" }
"\n" "header.Num++;"
"\n" Code clear;
"\n" "return true;" proc( clear, __, t_void, __, untyped_str("get_header().Num = 0;") );
Code fill;
{
Code check = untyped(
if ( begin < 0 || end >= header.Num )
fatal( "Range out of bounds" );
); );
body = function_body( header, check_cap, assign ); Code iter = untyped(
} for ( sw index = begin; index < end; index++ )
Data[index] = vallue;
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 ); Code body = proc_body( header, check, iter );
proc( fill, __, t_void, params( t_uw, "begin", t_uw, "end", t_elem_type, "value" ), body );
} }
def( clear ) Code get_header;
function( clear, __, t_void, __, untyped_str("get_header().Num = 0;") ); proc( get_header, spec_inline, t_ref_header, __, untyped_str("return pcast( Header, Data - 1);") );
def( fill ); Code grow;
{ {
def( body ) Code body;
{ {
var( header, ref_header, untyped_str("get_header()"), __ ); variable( uw, new_capacity, untyped( grow_formula( header.Capacity) ));
Code check = untyped_str( Code check_n_set = untyped(
"if ( begin < 0 || end >= header.Num )" if ( new_capacity < min_capacity )
"\n" "fatal( \"Range out of bounds\" );" new_capacity = min_capacity;
); );
Code iter = untyped_str( Code ret = untyped( return set_capacity( new_capacity ); );
"for ( sw index = begin; index < end; index++ )"
"\n" "Data[index] = vallue;"
);
function_body( header, check, iter ); body = proc_body( header, new_capacity, check_n_set, ret );
} }
function( fill, __, t_void, params( t_uw, "begin", t_uw, "end", type, "value" ), body ); proc( grow, __, t_bool, params( t_uw, "min_capacity" ), body );
} }
def( get_header ) Code pop;
function( get_header, spec_inline, ref_header, __, untyped_str("return pcast( Header, Data - 1);") );
def( grow )
{ {
def( body ) untyped_code( assertion, assert( header.Num > 0 ); );
{ untyped_code( decrement, header.Num--; );
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( Code body = proc_body( header, assertion, decrement );
"if ( new_capacity < min_capacity )"
"new_capacity = min_capacity;"
);
Code ret = untyped_str( "return set_capacity( new_capacity );" ); proc( pop, __, t_void, __, body );
body = function_body( header, new_capacity, check_n_set, ret );
} }
function( grow, __, t_bool, params( t_uw, "min_capacity" ), body ); Code reserve;
}
def( pop )
{ {
def( body ) untyped_code( check_n_set,
{ if ( header.Capacity < new_capacity )
var( header, ref_header, get_header(), UnusedCode ); return set_capacity( new_capacity );
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"); Code ret = untyped_str("return true");
body = function_body( header, check_n_set, ret ); Code body = proc_body( header, check_n_set, ret );
proc( reserve, __, t_bool, params( t_uw, "new_capacity" ), body );
} }
function( reserve, __, t_bool, params( t_uw, "new_capacity" ), body ); Code resize;
}
def( resize )
{ {
def( body ) Code body;
{ {
var( header, ref_header, untyped_str("get_header()"), __ ); untyped_code( check_n_grow,
if ( header.Capacity < new_num )
Code check_n_grow = untyped_str( if ( ! grow( new_num) )
"if ( header.Capacity < new_num )" return false;
"if ( ! grow( new_num) )"
"return false;"
); );
Code set_n_ret = untyped_str( untyped_code( set_n_ret,
"header.Count = new_num;" header.Count = new_num;
"return true;" return true;
); );
body = function_body( header, check_n_grow, set_n_ret ); body = proc_body( header, check_n_grow, set_n_ret );
} }
function( resize, __, t_bool, params( t_uw, "new_num" ), body ); proc( resize, __, t_bool, params( t_uw, "new_num" ), body );
} }
def( set_capacity ) Code set_capacity = proc_code(
bool set_capacity( new_capacity )
{ {
def( body ) Header& header = get_header();
{
var( header, ref_header, untyped_str("get_header()"), __ );
Code checks = untyped_str( if ( capacity == header.Capacity )
"if ( capacity == header.Capacity )" return true;
"return true;"
"\n\n"
"if ( capacity < header.Num )"
"header.Num = capacity;"
);
var( size, t_uw, untyped_str("sizeof(Header) + sizeof(Type) * capacity"), __ ); if ( capacity < header.Num )
var( new_header, ptr_header, untyped_str("rcast( Header*, alloc( header.Allocator, size ))"), __ ); header.Num = capacity;
Code check_n_move = untyped_str( uw size = sizeof(Header) + sizeof(Type) * capacity;
"if ( new_header == nullptr )" Header* new_header = rcast( Header* alloc( header.Allocator, size ));
"return false;"
"\n\n"
"memmove( new_header, & header, sizeof(Header) + sizeof(Type) * header.Num );"
);
Code set_free_ret = untyped_str( if ( new_header == nullptr )
"new_header->Allocator = header.Allocator;" return false;
"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 ); memmove( new_header, & header, sizeof( Header ) + sizeof(Type) * header.Num );
}
new_header->Allocator = header.Allocator;
function( set_capacity, __, t_bool, params( t_uw, "capacity" ), body ); new_header->Num = header.Num;
new_header->Capacity = header.Capacity;
free( header );
*Data = new_header + 1;
return true;
} }
);
char const* name = bprintf( "Array_%s", type_str ); char const* name = bprintf( "Array_%s", type_str );

12
test/Readme.md Normal file
View File

@ -0,0 +1,12 @@
# Test
The following tests focus on attempting to generate some math, containers, and the memory module of zpl.
Not all the files are written how I would practically use the librarry,
there is a bunch of conditionally compiled code for testing different aspects of the library.
Specifically, Array.hpp is far more complicated that it would need to be;
with at least double the code that it would actually have to define itself.
There will be down the line a proper container, and memory libraries made with this gen library
once the stress test files are complete.

View File

@ -25,18 +25,34 @@
#ifndef GEN_DEFINE_DSL #ifndef GEN_DEFINE_DSL
string name = string_sprintf( g_allocator, (char*)sprintf_buf, ZPL_PRINTF_MAXLEN, "square", type ); string name = string_sprintf( g_allocator, (char*)sprintf_buf, ZPL_PRINTF_MAXLEN, "square", type );
#if 1
Code square; Code square;
{ {
Code params = def_parameters( 1, integral_type, "value" ); Code params = def_params( 1, integral_type, "value" );
Code specifiers = def_specifiers( 1, Specifier::Inline ); Code specifiers = def_specifiers( 1, SpecifierT::Inline );
Code ret_stmt = untyped_fmt( "return value * value;" ); Code ret_stmt = untyped_str( txt( return value * value; ));
square = def_function( name, specifiers, params, integral_type, ret_stmt ); square = def_proc( name, specifiers, params, integral_type, ret_stmt );
} }
#else #else
def( square ) // Test of token template str.
function( square, __, integral_type, params( integral_type, "value" ), untyped_str("return value * value") ); 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 #endif
if ( ! square ) if ( ! square )

View File

@ -17,7 +17,7 @@ int gen_main()
int int
result = gen_math(); result = gen_math();
result = gen_array_file(); // result = gen_array_file();
Memory::cleanup(); Memory::cleanup();
return result; return result;