diff --git a/.editorconfig b/.editorconfig index 48d0d41..de2510a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -11,3 +11,10 @@ indent_size = 4 [*.cpp] indent_style = tab indent_size = 4 + +[*.h] +indent_style = tab +indent_size = 4 + +[*.hpp] +indent_style = tab diff --git a/.vscode/settings.json b/.vscode/settings.json index bf1b0ae..7d111f3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -6,6 +6,7 @@ "type_traits": "cpp", "utility": "cpp", "xtr1common": "cpp", - "xutility": "cpp" + "xutility": "cpp", + "initializer_list": "cpp" } } \ No newline at end of file diff --git a/Readme.md b/Readme.md index a83b6a3..95b9dbd 100644 --- a/Readme.md +++ b/Readme.md @@ -2,18 +2,22 @@ An attempt at simple staged metaprogramming for c/c++. -This library is intended for small-to midsize projects that want rapid complation times. -for fast debugging. +This library is intended for small-to midsize projects. + +### TOC + +* [Notes](#Notes) +* [Building](#Notes) ## Notes 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. -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. @@ -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. +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 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. 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 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. * Generate a 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. diff --git a/project/Bloat.cpp b/project/Bloat.cpp index b59d5af..6edb9e3 100644 --- a/project/Bloat.cpp +++ b/project/Bloat.cpp @@ -1,6 +1,7 @@ #define BLOAT_IMPL #include "Bloat.hpp" + namespace Global { 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) - { - return false; - } + char const* Str; + s32 Length; +}; - t->text = string_append_length(t->text, " ", 1); - t->text = string_appendc( t->text, b ); +ZPL_TABLE( static, TokMap, tokmap_, TokEntry ) - return true; -} - -b32 opts_custom_compile(opts *options, int argc, char **argv) +sw token_fmt_va( char* buf, uw buf_size, char const* fmt, s32 num_tokens, va_list va ) { - 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]; - - if (*arg) + tokmap_init( & tok_map, g_allocator ); + + 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 == '-') - { - opts_entry* entry = 0; - b32 checkln = false; - if ( *(arg + 1) == '-') - { - checkln = true; - ++arg; - } + TokEntry entry + { + value, + zpl_strnlen(value, 128) + }; - char *b = arg + 1, *e = b; + u32 key = crc32( token, zpl_strnlen(token, 32) ); - while (char_is_alphanumeric(*e) || *e == '-' || *e == '_') { - ++e; - } - - entry = zpl__opts_find(options, b, (e - b), checkln); - - if (entry) - { - char *ob = b; - b = e; - - /**/ - 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; - } - else if (*e == '\0') - { - char *sp = argv[i+1]; - - if (sp && *sp != '-' && (array_count(options->positioned) < 1 || entry->type != ZPL_OPTS_FLAG)) - { - if (entry->type == ZPL_OPTS_FLAG) - { - zpl__opts_push_error(options, b, ZPL_OPTS_ERR_EXTRA_VALUE); - had_errors = true; - - continue; - } - - arg = sp; - b = e = sp; - ++i; - } - else - { - if (entry->type != ZPL_OPTS_FLAG) - { - zpl__opts_push_error(options, ob, ZPL_OPTS_ERR_MISSING_VALUE); - had_errors = true; - continue; - } - - entry->met = true; - - continue; - } - } - - e = (char *)str_control_skip(e, '\0'); - 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; - } + tokmap_set( & tok_map, key, entry ); } } - return !had_errors; + sw result = 0; + char current = *fmt; + + while ( current ) + { + sw len = 0; + + while ( current && current != '{' && remaining ) + { + *buf = *fmt; + buf++; + fmt++; + + current = *fmt; + } + + if ( current == '{' ) + { + char const* scanner = fmt; + + s32 tok_len = 0; + + while ( *scanner != '}' ) + { + tok_len++; + scanner++; + } + + char const* token = fmt; + + s32 key = crc32( token, tok_len ); + TokEntry value = * tokmap_get( & tok_map, key ); + s32 left = value.Length; + + while ( left-- ) + { + *buf = *value.Str; + buf++; + value.Str++; + } + + scanner++; + fmt = scanner; + current = *fmt; + } + } + + return result; } diff --git a/project/Bloat.hpp b/project/Bloat.hpp index 3cb9e4a..bc0ba7c 100644 --- a/project/Bloat.hpp +++ b/project/Bloat.hpp @@ -1,35 +1,15 @@ /* BLOAT. + + This contians all definitions not directly related to the project. */ - #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 # define ZPL_IMPLEMENTATION #endif + #pragma region ZPL INCLUDE #if __clang__ # pragma clang diagnostic push @@ -44,15 +24,15 @@ # define ZPL_MODULE_CORE # define ZPL_MODULE_TIMER # define ZPL_MODULE_HASHING -// # define ZPL_MODULE_REGEX -// # define ZPL_MODULE_EVENT -// # define ZPL_MODULE_DLL -# define ZPL_MODULE_OPTS -// # define ZPL_MODULE_PROCESS -// # define ZPL_MODULE_MAT -// # define ZPL_MODULE_THREADING -// # define ZPL_MODULE_JOBS -// # define ZPL_MODULE_PARSER +// # define ZPL_MODULE_REGEX +// # define ZPL_MODULE_EVENT +// # define ZPL_MODULE_DLL +// # define ZPL_MODULE_OPTS +// # define ZPL_MODULE_PROCESS +// # define ZPL_MODULE_MAT +// # define ZPL_MODULE_THREADING +// # define ZPL_MODULE_JOBS +// # define ZPL_MODULE_PARSER #include "zpl.h" #if __clang__ @@ -61,7 +41,6 @@ #pragma endregion ZPL INCLUDE - #if __clang__ # pragma clang diagnostic ignored "-Wunused-const-variable" # pragma clang diagnostic ignored "-Wswitch" @@ -70,16 +49,41 @@ #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 bitfield_is_equal( Field_, Mask_ ) ( ( Mask_ & Field_ ) == Mask_ ) #define ct constexpr #define forceinline ZPL_ALWAYS_INLINE #define print_nl( _) zpl_printf("\n") +#define ccast( Type_, Value_ ) * const_cast< Type_* >( & Value_ ) #define scast( Type_, Value_ ) static_cast< Type_ >( Value_ ) #define rcast( Type_, Value_ ) reinterpret_cast< 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() \ do \ @@ -90,7 +94,7 @@ do \ return; \ Done = true; \ } \ -while(0) \ +while(0) #define do_once_start \ do \ @@ -105,14 +109,8 @@ do \ } \ while(0); - -using Line = char*; -using Array_Line = array( Line ); - - ct char const* Msg_Invalid_Value = "INVALID VALUE PROVIDED"; - namespace Global { extern bool ShouldShowDebug; @@ -124,6 +122,8 @@ namespace Memory extern arena 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() void setup(); @@ -131,9 +131,19 @@ namespace Memory void cleanup(); } -// Had to be made to support multiple sub-arguments per "opt" argument. -b32 opts_custom_compile(opts *options, int argc, char **argv); +inline +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 sw log_fmt(char const *fmt, ...) @@ -152,7 +162,7 @@ sw log_fmt(char const *fmt, ...) } inline -void fatal(char const *fmt, ...) +sw fatal(char const *fmt, ...) { local_persist thread_local char buf[ZPL_PRINTF_MAXLEN] = { 0 }; @@ -165,11 +175,13 @@ void fatal(char const *fmt, ...) va_end(va); assert_crash(buf); + return -1; #else va_start(va, fmt); zpl_printf_err_va( fmt, va); va_end(va); zpl_exit(1); + return -1; #endif } diff --git a/project/Bloat.undef.hpp b/project/Bloat.undef.hpp new file mode 100644 index 0000000..28ccbc3 --- /dev/null +++ b/project/Bloat.undef.hpp @@ -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 + diff --git a/project/gen.cpp b/project/gen.cpp index 1119461..65d151f 100644 --- a/project/gen.cpp +++ b/project/gen.cpp @@ -1,6 +1,5 @@ #include "Bloat.hpp" #include "gen.hpp" -#define gen_time #ifdef gen_time namespace gen @@ -10,48 +9,136 @@ namespace gen static array(CodePOD) CodePool = nullptr; } +#pragma region CONSTANTS +# ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS + const Code t_void; + + const Code t_bool; + const Code t_char; + const Code t_char_wide; + + const Code t_s8; + const Code t_s16; + const Code t_s32; + const Code t_s64; + + const Code t_u8; + const Code t_u16; + const Code t_u32; + const Code t_u64; + + const Code t_sw; + const Code t_uw; + + const Code t_f32; + const Code t_f64; + + const Code spec_constexpr; + const Code spec_inline; +# endif +#pragma endregion CONSTANTS + + /* Used internally to retireve a Code object form the CodePool. */ - Code make() + static Code make_code() { using namespace StaticData; - array_append( CodePool, InvalidCode ); + ct CodePOD Invalid = { ECode::Invalid, false, nullptr, nullptr, nullptr, { nullptr } }; - return * (Code*) & array_back( CodePool ); + array_append( CodePool, Invalid ); + + return pcast( Code, array_back( CodePool )); } - - void init() { array_init( StaticData::CodePool, g_allocator ); + + Code& + InvalidCode_write = ccast( Code, InvalidCode ); + InvalidCode_write = make_code(); + + #ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS + Code& + t_bool_write = ccast( Code, t_void ); + t_bool_write = def_type( txt(void) ); + + # define def_constant_code_type( Type_ ) \ + Code& \ + t_##Type_##_write = ccast( Code, t_##Type_ ); \ + t_##Type_##_write = def_type( txt(Type_) ) \ + + def_constant_code_type( bool ); + def_constant_code_type( char ); + def_constant_code_type( wchar_t ); + + def_constant_code_type( s8 ); + def_constant_code_type( s16 ); + def_constant_code_type( s32 ); + def_constant_code_type( s64 ); + + def_constant_code_type( u8 ); + def_constant_code_type( u16 ); + def_constant_code_type( u32 ); + def_constant_code_type( u64 ); + + def_constant_code_type( sw ); + def_constant_code_type( uw ); + + def_constant_code_type( f32 ); + def_constant_code_type( f64 ); + + # undef def_constant_code_type + + Code& + spec_constexpr_write = ccast( Code, spec_constexpr ); + spec_constexpr_write = def_specifiers( 1, ESpecifier::Constexpr ); + + # define def_constant_spec( Type_, ... ) \ + Code& \ + spec_##Type_##_write = ccast( Code, spec_##Type_ ); \ + spec_##Type_##_write = def_specifiers( VA_NARGS(__VA_ARGS__), __VA_ARGS__) + + def_constant_spec( inline, ESpecifier::Inline ); + + # undef def_constant_spec + + #endif } - Code decl_type( char const* name, Code type, Code specifiers ) + Code decl_type( Code type, char const* name, Code specifiers ) { using namespace ECode; if ( type->Type != Specifiers ) - fatal( "gen::decl_type: type is not a Typename"); + { + log_failure( "gen::decl_type: type is not a Typename"); + return InvalidCode; + } if ( type->Type != Typename ) - fatal( "gen::decl_type: specifiers is not a 'Specfiers' type"); + { + log_failure( "gen::decl_type: specifiers is not a 'Specfiers' type"); + return InvalidCode; + } Code - result = make(); + result = make_code(); result->Type = Decl_Type; result->Name = string_make( g_allocator, name ); array_init( result->Entries, g_allocator ); result->add( specifiers ); result->add( type ); + result.lock(); return result; } - Code decl_fn( char const* name + Code decl_proc( char const* name , Code specifiers , Code params , Code ret_type @@ -60,16 +147,25 @@ namespace gen using namespace ECode; if ( specifiers->Type != Specifiers ) - fatal( "gen::decl_fn: specifiers was not a `Specifiers` type" ); + { + log_failure( "gen::decl_fn: specifiers was not a `Specifiers` type" ); + return InvalidCode; + } if ( params->Type != Parameters ) - fatal( "gen::decl_fn: params was not a `Parameters` type" ); + { + log_failure( "gen::decl_fn: params was not a `Parameters` type" ); + return InvalidCode; + } if ( ret_type->Type != Typename ) - fatal( "gen::decl_fn: ret_type was not a Typename" ); + { + log_failure( "gen::decl_fn: ret_type was not a Typename" ); + return InvalidCode; + } Code - result = make(); + result = make_code(); result->Type = Decl_Function; result->Name = string_make( g_allocator, name ); @@ -83,18 +179,22 @@ namespace gen if ( params ) result->add( params ); + result.lock(); return result; } - Code def_parameters( s32 num, ... ) + Code def_params( s32 num, ... ) { using namespace ECode; if (num <= 0) - fatal( "TT::make_paramters: num cannot be zero or neg" ); + { + log_failure( "TT::make_paramters: num cannot be zero or neg" ); + return InvalidCode; + } Code - result = make(); + result = make_code(); result->Type = Parameters; va_list va; @@ -107,7 +207,10 @@ namespace gen Code type = va_arg(va, Code); if ( type->Type != Typename ) - fatal( "gen::def_parameters: type of param %d is not a Typename", num - num + 1 ); + { + log_failure( "gen::def_parameters: type of param %d is not a Typename", num - num + 1 ); + return InvalidCode; + } result->add( type ); @@ -116,24 +219,30 @@ namespace gen type = va_arg(va, Code); Code - param = make(); + param = make_code(); param->Type = Parameters; param->Name = string_make( g_allocator, va_arg(va, char const*) ); array_init( param->Entries, g_allocator ); if ( type->Type != Typename ) - fatal( "gen::def_parameters: type of param %d is not a Typename", num - num + 1 ); + { + log_failure( "gen::def_parameters: type of param %d is not a Typename", num - num + 1 ); + return InvalidCode; + } param->add( type ); + param.lock(); + result->add(param); } va_end(va); + result.lock(); return result; } - Code def_function( char const* name + Code def_proc( char const* name , Code specifiers , Code params , Code ret_type @@ -143,13 +252,22 @@ namespace gen using namespace ECode; if ( specifiers && specifiers->Type != Specifiers ) - fatal( "gen::def_function: specifiers was not a `Specifiers` type" ); + { + log_failure( "gen::def_function: specifiers was not a `Specifiers` type" ); + return InvalidCode; + } if ( params && params->Type != Parameters ) - fatal( "gen::def_function: params was not a `Parameters` type" ); + { + log_failure( "gen::def_function: params was not a `Parameters` type" ); + return InvalidCode; + } if ( ret_type == nullptr || ret_type->Type != Typename ) - fatal( "gen::def_function: ret_type was not a Typename" ); + { + log_failure( "gen::def_function: ret_type was not a Typename" ); + return InvalidCode; + } switch ( body->Type ) { @@ -158,16 +276,21 @@ namespace gen break; default: - fatal("gen::def_function: body must be either of Function_Body or Untyped type."); + { + log_failure("gen::def_function: body must be either of Function_Body or Untyped type."); + return InvalidCode; + } } Code - result = make(); + result = make_code(); result->Name = string_make( g_allocator, name ); result->Type = Function; array_init( result->Entries, g_allocator ); + result->add( body ); + if ( specifiers ) result->add( specifiers ); @@ -176,21 +299,21 @@ namespace gen if ( params ) result->add( params ); - result->add( body ); - - body->Parent = result; - + result.lock(); return result; } - Code def_function_body( s32 num, ... ) + Code def_proc_body( s32 num, ... ) { using namespace ECode; if ( num <= 0 ) - fatal("gen::def_function_body: num cannot zero or neg"); + { + log_failure("gen::def_function_body: num cannot zero or neg"); + return InvalidCode; + } - Code result = make(); + Code result = make_code(); array_init( result->Entries, g_allocator ); @@ -200,6 +323,12 @@ namespace gen { Code entry = va_arg(va, Code); + if ( ! entry ) + { + log_failure("gen::def_function_body: Provided an invalid entry!"); + return InvalidCode; + } + switch ( entry->Type ) { case Decl_Function: @@ -210,7 +339,10 @@ namespace gen case Specifiers: case Struct_Body: case Typename: - fatal("gen::def_function_body: Entry type is not allowed: %s", entry->type_str() ); + { + log_failure("gen::def_function_body: Entry type is not allowed: %s", entry->type_str() ); + return InvalidCode; + } default: break; @@ -224,18 +356,76 @@ namespace gen return result; } + Code def_proc_body( s32 num, Code* codes ) + { + using namespace ECode; + + if ( num <= 0 ) + { + log_failure("gen::def_function_body: num cannot zero or neg"); + return InvalidCode; + } + + if ( codes == nullptr ) + { + log_failure("gen::def_function_body: Provided a null array of codes!"); + return InvalidCode; + } + + Code result = make_code(); + + array_init( result->Entries, g_allocator ); + do + { + Code entry = *codes; + + if ( ! entry ) + { + log_failure("gen::def_function_body: Provided an invalid entry!"); + return InvalidCode; + } + + switch ( entry->Type ) + { + case Decl_Function: + case Decl_Type: + case Namespace: + case Namespace_Body: + case Parameters: + case Specifiers: + case Struct_Body: + case Typename: + { + log_failure("gen::def_function_body: Entry type is not allowed: %s", entry->type_str() ); + return InvalidCode; + } + + default: + break; + } + + result->add( entry ); + } + while ( num--, num > 0 ); + + return result; + } + Code def_namespace( char const* name, Code body ) { using namespace ECode; Code - result = make(); + result = make_code(); result->Type = Namespace; array_init( result->Entries, g_allocator ); if ( body->Type != Namespace_Body || body->Type != Untyped ) - fatal("gen::def_namespace: body is not of namespace or untyped type"); + { + log_failure("gen::def_namespace: body is not of namespace or untyped type"); + return InvalidCode; + } result->add( body ); @@ -247,10 +437,13 @@ namespace gen using namespace ECode; if ( num <= 0 ) - fatal("gen::make_specifier: num cannot be zero or less"); + { + log_failure("gen::make_specifier: num cannot be zero or less"); + return InvalidCode; + } Code - result = make(); + result = make_code(); result->Type = Namespace_Body; va_list va; @@ -266,7 +459,8 @@ namespace gen case Specifiers: case Struct_Body: case Typename: - fatal("gen::def_function_body: Entry type is not allowed: %s", ECode::str(entry->Type) ); + log_failure("gen::def_function_body: Entry type is not allowed: %s", ECode::str(entry->Type) ); + return InvalidCode; default: break; @@ -286,7 +480,7 @@ namespace gen fatal("gen::make_specifier: num cannot be zero or less"); Code - result = make(); + result = make_code(); result->Type = ECode::Specifiers; result->Content = string_make( g_allocator, "" ); @@ -294,16 +488,16 @@ namespace gen va_start(va, num); do { - Specifier type = (Specifier)va_arg(va, int); + SpecifierT type = (SpecifierT)va_arg(va, int); switch ( type ) { - case Alignas: - result->Content = string_append_fmt( result->Content, "%s(%d)", specifier_str(type), va_arg(va, u32) ); + case ESpecifier::Alignas: + result->Content = string_append_fmt( result->Content, "%s(%d)", ESpecifier::to_str(type), va_arg(va, u32) ); break; default: - const char* str = specifier_str(type); + const char* str = ESpecifier::to_str(type); result->Content = string_append_fmt( result->Content, "%s", str ); break; @@ -320,16 +514,25 @@ namespace gen using namespace ECode; if ( specifiers && specifiers->Type != Specifiers ) - fatal( "gen::def_struct: specifiers was not a `Specifiers` type" ); + { + log_failure( "gen::def_struct: specifiers was not a `Specifiers` type" ); + return InvalidCode; + } if ( parent && parent->Type != Struct ) - fatal( "gen::def_struct: parent was not a `Struct` type" ); + { + log_failure( "gen::def_struct: parent was not a `Struct` type" ); + return InvalidCode; + } if ( body && body->Type != Struct_Body ) - fatal( "gen::def_struct: body was not a Struct_Body type" ); + { + log_failure( "gen::def_struct: body was not a Struct_Body type" ); + return InvalidCode; + } Code - result = make(); + result = make_code(); result->Type = Struct; result->Name = string_make( g_allocator, name ); @@ -352,9 +555,12 @@ namespace gen using namespace ECode; if ( num == 0 ) - fatal("gen::def_struct_body: num cannot be zero"); + { + log_failure("gen::def_struct_body: num cannot be zero"); + return InvalidCode; + } - Code result = make(); + Code result = make_code(); array_init( result->Entries, g_allocator ); @@ -372,7 +578,10 @@ namespace gen case Specifiers: case Struct_Body: case Typename: - fatal("gen::def_struct_body: Entry type is not allowed: %s", ECode::str(entry->Type) ); + { + log_failure("gen::def_struct_body: Entry type is not allowed: %s", ECode::str(entry->Type) ); + return InvalidCode; + } } result->add( entry ); @@ -383,19 +592,28 @@ namespace gen return result; } - Code def_variable( char const* name, Code type, Code value, Code specifiers ) + Code def_variable( Code type, char const* name, Code value, Code specifiers ) { if ( specifiers && specifiers->Type != ECode::Specifiers ) - fatal( "gen::def_variable: specifiers was not a `Specifiers` type" ); + { + log_failure( "gen::def_variable: specifiers was not a `Specifiers` type" ); + return InvalidCode; + } if ( type->Type != ECode::Typename ) - fatal( "gen::def_variable: type was not a Typename" ); + { + log_failure( "gen::def_variable: type was not a Typename" ); + return InvalidCode; + } if ( value && value->Type != ECode::Untyped ) - fatal( "gen::def_variable: value was not a `Untyped` type" ); + { + log_failure( "gen::def_variable: value was not a `Untyped` type" ); + return InvalidCode; + } Code - result = make(); + result = make_code(); result->Name = string_make( g_allocator, name ); result->Type = ECode::Variable; @@ -415,7 +633,7 @@ namespace gen Code def_type( char const* name ) { Code - result = make(); + result = make_code(); result->Name = string_make( g_allocator, name ); result->Type = ECode::Typename; @@ -425,7 +643,7 @@ namespace gen Code def_using( char const* name, Code type ) { Code - result = make(); + result = make_code(); result->Name = string_make( g_allocator, name ); result->Type = ECode::Using; @@ -437,6 +655,16 @@ namespace gen return result; } + Code untyped_str(char const* fmt) + { + Code + result = make_code(); + result->Name = string_make( g_allocator, fmt ); + result->Type = ECode::Untyped; + + return result; + } + Code untyped_fmt(char const* fmt, ...) { local_persist thread_local @@ -448,7 +676,7 @@ namespace gen va_end(va); Code - result = make(); + result = make_code(); result->Name = string_make( g_allocator, fmt ); result->Type = ECode::Untyped; result->Content = string_make( g_allocator, buf ); @@ -456,94 +684,7 @@ namespace gen return result; } - - struct TokEntry - { - char const* Str; - s32 Length; - }; - - ZPL_TABLE( static, TokMap, tokmap_, TokEntry ) - - sw token_fmt_va( char* buf, uw buf_size, char const* fmt, s32 num_tokens, va_list va ) - { - char const* buf_begin = buf; - sw remaining = buf_size; - - TokMap tok_map; - { - tokmap_init( & tok_map, g_allocator ); - - s32 left = num_tokens; - - while ( left-- ) - { - char const* token = va_arg( va, char const* ); - char const* value = va_arg( va, char const* ); - - TokEntry entry - { - value, - zpl_strnlen(value, 128) - }; - - u32 key = crc32( token, zpl_strnlen(token, 32) ); - - tokmap_set( & tok_map, key, entry ); - } - } - - sw result = 0; - char current = *fmt; - - while ( current ) - { - sw len = 0; - - while ( current && current != '{' && remaining ) - { - *buf = *fmt; - buf++; - fmt++; - - current = *fmt; - } - - if ( current == '{' ) - { - char const* scanner = fmt; - - s32 tok_len = 0; - - while ( *scanner != '}' ) - { - tok_len++; - scanner++; - } - - char const* token = fmt; - - s32 key = crc32( token, tok_len ); - TokEntry value = *tokmap_get( & tok_map, key ); - s32 left = value.Length; - - while ( left-- ) - { - *buf = *value.Str; - buf++; - value.Str++; - } - - scanner++; - fmt = scanner; - current = *fmt; - } - } - - return result; - } - - Code token_fmt( char const* fmt, s32 num_tokens, ... ) + Code untyped_token_fmt( char const* fmt, s32 num_tokens, ... ) { local_persist thread_local char buf[ZPL_PRINTF_MAXLEN] = { 0 }; @@ -554,17 +695,300 @@ namespace gen va_end(va); Code - result = make(); - result->Name = string_make( g_allocator, fmt ); - result->Type = ECode::Untyped; - result->Content = string_make( g_allocator, buf ); + result = make_code(); + result->Name = string_make( g_allocator, fmt ); + result->Type = ECode::Untyped; + result->Content = string_make( g_allocator, buf ); + + result.lock(); return result; } + Code make_proc( char const* name + , Code specifiers + , Code params + , Code ret_type + ) + { + using namespace ECode; + + if ( specifiers && specifiers->Type != Specifiers ) + { + log_failure( "gen::def_function: specifiers was not a `Specifiers` type" ); + return InvalidCode; + } + + if ( params && params->Type != Parameters ) + { + log_failure( "gen::def_function: params was not a `Parameters` type" ); + return InvalidCode; + } + + if ( ret_type == nullptr || ret_type->Type != Typename ) + { + log_failure( "gen::def_function: ret_type was not a Typename" ); + return InvalidCode; + } + + Code + result = make_code(); + result->Name = string_make( g_allocator, name ); + result->Type = Function; + + array_init( result->Entries, g_allocator ); + + // Making body at entry 0. + result->add( make_code() ); + + if ( specifiers ) + result->add( specifiers ); + + if ( ret_type ) + result->add( ret_type ); + + if ( params ) + result->add( params ); + + return result; + } + + Code make_struct( char const* name, Code parent = UnusedCode, Code specifiers = UnusedCode ) + { + using namespace ECode; + + if ( specifiers && specifiers->Type != Specifiers ) + { + log_failure( "gen::def_struct: specifiers was not a `Specifiers` type" ); + return InvalidCode; + } + + if ( parent && parent->Type != Struct ) + { + log_failure( "gen::def_struct: parent was not a `Struct` type" ); + return InvalidCode; + } + + Code + result = make_code(); + result->Type = Struct; + result->Name = string_make( g_allocator, name ); + + array_init( result->Entries, g_allocator ); + + // Making body at entry 0. + result->add( make_code() ); + + if ( parent ) + result->add( parent ); + + if ( specifiers ) + result->add( specifiers ); + + return result; + } + + Code make_unit( char const* name ) + { + Code + result = make_code(); + result->Type = ECode::Unit; + result->Name = string_make( g_allocator, name ); + + array_init( result->Entries, g_allocator ); + + // Making body at entry 0; + result->add( make_code() ); + + return result; + } + + Code parse_proc( char const* def, s32 length ) + { + if ( def == nullptr ) + { + log_failure("gen::parse_proc: def is null!"); + return InvalidCode; + } + + arena mem; + do_once_start + { + arena_init_from_allocator( & mem, heap(), kilobytes( 10 ) ); + } + do_once_end + + // Pretty sure its impossible to have more than this. + SpecifierT specs_found[16] { ESpecifier::Num_Specifiers }; + + u8 num_specifiers; + + // Making all significant tokens have a max length of 128 for this parser. + ct sw LengthID = 128; + + struct Param + { + char const Type[LengthID]; + char const Name[LengthID]; + }; + + static + Param Params[ 64 ] { 0 }; + + // Zero out params before a run of this func. + zpl_memset( Params, 0, sizeof( Params )); + + char const* name; + s32 name_length = 0; + + char const* body_str = nullptr; + s32 body_length = 0; + + char const* ret_type_str = nullptr; + s32 ret_length = 0; + + s32 left = length; + char const* scanner = def; + + while ( left -- ) + { + #define SkipWhitespace() \ + while ( left && char_is_space( * scanner ) ) \ + { \ + left--; \ + scanner++ ; + } + + #define Get + + // Find all specifiers (if any) and the return type + do + { + SkipWhitespace(); + + s32 length = 0; + char const* spec_or_type = scanner; + + while ( char_is_alpha( * scanner) || * scanner == '_') + { + length++; + left--; + scanner++; + } + + SpecifierT type = ESpecifier::to_type( spec_or_type, length ); + + if ( type != ESpecifier::Invalid ) + { + specs_found[num_specifiers] = type; + num_specifiers++; + continue; + } + + // Should be return type + ret_type_str = scanner; + ret_length = length; + scanner++; + break; + } + while( left ); + + SkipWhitespace(); + + // Next up should be the name if before there were specifiers + name = scanner; + + while ( char_is_alpha( * scanner) || * scanner == '_') + { + ret_length++; + left--; + scanner++; + } + + // Parameters + SkipWhitespace(); + + if ( * scanner != '(' ) + { + log_failure("gen::parse_proc: Error, expected a ( in %s", string_make_length( g_allocator, scanner - 4, 100) ); + return InvalidCode; + } + + scanner++; + + do + { + + } + while (0); + + // Get past ')' + scanner++; + + SkipWhitespace() + + // get end specifiers + + - string AST::to_string() + if ( * scanner != '{') + { + + } + } + + Code specifiers = def_specifiers( num_specifiers, specs_found ); + + Code params = make_code(); + Code ret_type = def_type( ret_type_str ); + Code body = untyped_str( body_str, body_length ); + + Code + result = make_code(); + result->Name = string_make( g_allocator, name ); + result->Type = ECode::Function; + + array_init( result->Entries, g_allocator ); + + result->add( body ); + + if ( specifiers ) + result->add( specifiers ); + + result->add( ret_type ); + + if ( params ) + result->add( params ); + + result.lock(); + return result; + } + + Code parse_struct( char const* def, s32 length ) + { + arena mem; + do_once_start + arena_init_from_allocator( & mem, heap(), kilobytes( 10 ) ); + do_once_end + + // Pretty sure its impossible to have more than this. + SpecifierT specs_found[16] { ESpecifier::Num_Specifiers }; + + u8 num_specifiers; + + // Making all significant tokens have a max length of 128 for this parser. + ct sw LengthID = 128; + + char const name[LengthID] { 0 }; + char const parent[LengthID] { 0 }; + + + } + + + + string AST::to_string() const { string result = string_make( g_allocator, "" ); diff --git a/project/gen.hpp b/project/gen.hpp index aa8f21d..6cc89b3 100644 --- a/project/gen.hpp +++ b/project/gen.hpp @@ -1,105 +1,163 @@ /* gencpp: A simple staged metaprogramming library for C++. - - This library is intended for small-to midsize projects that want rapid complation times - for fast debugging. + + This library is intended for small-to midsize projects. AST type checking supports only a small subset of c++. 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 #include "Bloat.hpp" -// Defined by default. - -#define GEN_ENABLE_READONLY_AST -// #define GEN_DEFINE_DSL - +// Temporarily here for debugging purposes. #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 namespace gen { - #if 0 - ct sw ColumnLimit = 256; - ct sw MaxLines = kilobytes(256); + using LogFailType = sw(*)(char const*, ...); - using LineStr = char[ColumnLimit]; - #endif +#ifdef GEN_BAN_CPP_TEMPLATES + #define template static_assert("Templates are banned within gen_time scope blocks") +#endif - // Specifier Type - enum Specifier : u8 - { - Alignas, // alignas(#) - Constexpr, // constexpr - Inline, // inline +#ifdef GEN_USE_FATAL + ct LogFailType log_failure = fatal; +#else + ct LogFailType log_failure = log_fmt; +#endif - 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 { enum Type : u8 { - Invalid, - - Untyped, // User provided raw string. - - Decl_Function, // Forward a function - Decl_Type, // Forward a type. + Invalid, // Used only with improperly created Code nodes + Untyped, // User provided raw string + Decl_Function, // ( ) + Decl_Type, // ; Function, // ( ) Function_Body, // { } - Namespace, - Namespace_Body, - Parameters, // Used with functions. - Specifiers, - Struct, - Struct_Body, - Variable, - Typedef, - Typename, - Using, + Namespace, // Define a namespace + Namespace_Body, // { } + Parameters, // ... + Specifiers, // Used with functions, structs, variables + Struct, // struct + Struct_Body, // { } + Variable, // + Typedef, // typedef + Typename, // Typename, used with other types + Using, // using = + Unit, // Represents a file. Num_Types }; @@ -110,11 +168,9 @@ namespace gen static char const* lookup[Num_Types] = { "Invalid", - "Untyped", - "Decl_Function", - "Decl_type", + "Decl_Type", "Function", "Function_Body", "Namespace", @@ -126,7 +182,8 @@ namespace gen "Variable", "Typedef", "Typename", - "using" + "Using", + "Unit", }; return lookup[ type ]; @@ -134,6 +191,116 @@ namespace gen } 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, // [ 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; } @@ -247,28 +435,27 @@ namespace gen return *this; } - #ifdef GEN_ENABLE_READONLY_AST forceinline AST* operator ->() { + #ifdef GEN_ENFORCE_READONLY_AST if ( ast == nullptr ) fatal("Attempt to dereference a nullptr!"); if ( ast->Readonly ) fatal("Attempted to access a member from a readonly ast!"); + #endif 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. ct Code UnusedCode = { nullptr }; // 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. @@ -277,7 +464,7 @@ namespace gen Strings made with the Typename ASTs are stored in thier own arena allocator. 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 /* @@ -286,17 +473,18 @@ namespace gen */ void init(); + #pragma region Upfront /* Foward Declare a type: ; */ - 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: ( ); */ - Code decl_fn( char const* name + Code def_fwd_proc( char const* name , Code specifiers , Code params , Code ret_type @@ -305,8 +493,10 @@ namespace gen /* Define an 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: @@ -315,7 +505,7 @@ namespace gen } */ - Code def_function( char const* name + Code def_proc( char const* name , Code specifiers , Code params , 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; @@ -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, ... ); @@ -359,12 +550,23 @@ namespace gen Define a set of parameters for a function: , ... */ - 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 + + 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, SpecifierT* specs ); /* 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* codes ); /* Define a variable: = ; */ - 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. - Useless by itself, its intended to be used in conjunction with + Define a typename AST value. + 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 ); @@ -412,7 +621,88 @@ namespace gen Can only be used in either a */ 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. @@ -423,6 +713,7 @@ namespace gen Consider this an a preprocessor define. */ Code untyped_str( char const* str ); + Code untyped_str( char const* str, s32 length); /* 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. Consider this an a preprocessor define. */ - Code token_fmt( char const* fmt, s32 num_tokens, ... ); - - - /* - 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 ); + Code untyped_token_fmt( char const* fmt, s32 num_tokens, ... ); + #pragma endregion Untyped text /* Used to generate the files. @@ -492,62 +773,93 @@ namespace gen #pragma region MACROS # define gen_main main - -# define __ UnusedCode +# define __ UnusedCode /* 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( , ) + { + ... + } + + Where ... are whatever is deemed necessary to produce the definition for the def( ). + + The code macros are used to embed c/c++ to insert into the desired lcoation. */ #ifdef GEN_DEFINE_DSL -# define type( Name_, Value_ ) Code Name_ = gen::def_type( txt(Value_) ) -# define type_fmt( Name_, Fmt_, ... ) Code Name_ = gen::def_type( bprintf( Fmt_, __VA_ARGS__ ) ) -# define value( Name_, Value_ ) Code Name_ = gen::untyped_str( Value_ ) -# define specifiers( Name_, ... ) Code Name_ = gen::def_specifiers( VA_NARGS( __VA_ARGS__ ), __VA_ARGS__ ) -# define using( Name_, Type_ ) Code Name_ = gen::def_using( #Name_, Type_ ) +# define untyped_code( Name_, Value_ ) Code Name_ = gen::untyped_str( txt(Value_) ) +# define typename( Name_, Value_ ) Code t_##Name_ = gen::def_type( txt(Value_) ) +# define typename_fmt( Name_, Fmt_, ... ) Code t_##Name_ = gen::def_type( bprintf( Fmt_, __VA_ARGS__ ) ) +# define using_type( Name_, Type_ ) Code Name_ = gen::def_using( #Name_, t_##Type_ ) +# define variable( Type_, Name_, ... ) Code Name_ = gen::def_variable( t_##Type_, #Name_, __VA_ARGS__ ) -# define var( Name_, Type_, Value_, Specifiers_ ) \ - Code Name_ = gen::def_variable( #Name_, Type_, untyped_str( #Value_ ), Specifiers_ ) +# define untyped( Value_ ) gen::untyped_str( txt(Value_) ) +# 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( ... ) -/* - Defines scoped symbol. - - 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__ ) +# define proc_code( Def_ ) gen::parse_proc( txt( Def_ ), sizeof( txt( Def_ )) ) +# define struct_code( Def_ ) gen::parse_struct( txt( Def_ ), sizeof( txt( Def_ )) ) #endif #pragma endregion MACROS #pragma region CONSTANTS +#ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS namespace gen { // 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_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_uw; + extern const Code t_f32; + extern const Code t_f64; + + extern const Code spec_constexpr; extern const Code spec_inline; } +#endif #pragma endregion CONSTANTS #endif diff --git a/test/Array.hpp b/test/Array.hpp index 956d9d8..7a285e6 100644 --- a/test/Array.hpp +++ b/test/Array.hpp @@ -1,5 +1,8 @@ /* 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 @@ -8,21 +11,18 @@ #include "gen.hpp" #ifdef gen_time - using namespace gen; - Code gen__array_base() { - // Make these global consts to be accessed anywhere... - Code t_sw = def_type( txt(sw) ); - Code t_uw = def_type( txt(uw) ); - Code t_allocator = def_type( txt(allocator) ); - #ifndef GEN_DEFINE_DSL + using namespace gen; + + Code t_allocator = def_type( txt(allocator) ); + Code header; { - Code num = def_variable( "Num", t_uw ); - Code capacity = def_variable( "Capacity", t_uw ); - Code allocator_var = def_variable( "Allocator", t_allocator ); + Code num = def_variable( t_uw, "Num" ); + Code capacity = def_variable( t_uw, "Capacity" ); + Code allocator_var = def_variable( t_allocator, "Allocator" ); Code header_body = def_struct_body( 3, num, capacity, allocator_var ); header = def_struct( "ArrayHeader", header_body ); @@ -30,36 +30,35 @@ Code grow_formula; { - Code spec = def_specifiers(1, Specifier::Inline); - Code params = def_parameters(1, t_uw, "value" ); - Code body = untyped_str( "return 2 * value * 8;" ); + Code spec = def_specifiers(1, ESpecifier::Inline); + Code params = def_params(1, t_uw, "value" ); + 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 ); #else - def( ArrayBase ) - def ( Header ) - { - var( Num, t_uw, __, __ ); - var( Capacity, t_uw, __, __); - var( Allocator, t_allocator, __, __); + typename( allocator, allocator ); - Code body = struct_body( Num, Capacity, Allocator ); + Code Header; + { + variable( uw, Num, ); + variable( uw, Capacity ); + variable( allocator, Allocator ); - struct( Header, __, __, body ); - } + Code body = struct_body( Num, Capacity, Allocator ); - def( grow_formula ) - { - function( grow_formula, spec_inline, t_uw, params( t_uw, "value" ), untyped_str("return 2 * value * 8") ); - } + struct( Header, __, __, body ); + } - Code body = struct_body( Header, grow_formula ); - struct( ArrayBase, __, __, body ); + Code proc( grow_formula, spec_inline, t_uw, params( t_uw, "value" ), + untyped( return 2 * value * 8 ) + ); + + Code struct( ArrayBase, __, __, struct_body( Header, grow_formula ) ); #endif return ArrayBase; @@ -70,16 +69,11 @@ { #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) ); - Code t_bool = def_type( txt(bool) ); Code t_allocator = def_type( txt(allocator) ); - Code t_void = def_type( txt(void) ); Code v_nullptr = untyped_str( "nullptr" ); - Code spec_ct = def_specifiers(1, Specifier::Constexpr ); - Code spec_inline = def_specifiers(1, Specifier::Inline ); + Code spec_ct = def_specifiers(1, ESpecifier::Constexpr ); Code type = def_type( type_str ); Code ptr_type = def_type( bprintf( "%s*", type_str ) ); @@ -93,44 +87,50 @@ Code array_def; { 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 params = def_parameters( 1, t_allocator, "mem_handler" ); - Code body = untyped_str( "return init_reserve( mem_handler, grow_formula(0) );" ); + Code params = def_params( 1, t_allocator, "mem_handler" ); + 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 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 header_value = untyped_str( - "rcast( Header*, alloc( mem_handler, sizeof( Header ) + sizeof(Type) + capacity ))" - ); - Code header = def_variable( "header", ptr_header, header_value ); + Code header_value = untyped_str( txt( + rcast( Header*, alloc( mem_handler, sizeof( Header ) + sizeof(Type) + capacity )) + )); - Code null_check = untyped_str( - "if (header == nullptr)" - "\n" "return false;" - ); + Code header = def_variable( ptr_header, "header", header_value ); - Code header_init = untyped_str( - "header->Num = 0;" - "\n""header->Capacity = capacity;" - "\n""header->Allocator = mem_handler;" - ); + Code null_check = untyped_str( txt( + if (header == nullptr) + return false; + )); - 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 ); - 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 , null_check , 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 body = untyped_str( - "Header& header = get_header();" - "\n""::free( header.Allocator, & get_header() );" - ); + Code body = untyped_str( txt( + Header& header = 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" ); - Code body; - { - Code header = def_variable( "header", ref_header, untyped_str( "get_header()") ); + append->add( def_params( 1, type, "value") ); - Code check_cap = untyped_str( - "if ( header.Capacity < header.Num + 1 )" - "\n" "if ( ! grow(0) )" - "\n" "return false;" - ); + Code + body = append.body(); + body->add( + untyped_str( txt( + if ( header.Capacity < header.Num + 1 ) + if ( ! grow(0) ) + return false; + )) + ); - Code assign = untyped_str( - "Data[ header.Num ] = value;" - "\n" "header.Num++;" - "\n" - "\n" "return true;" - ); - - body = def_function_body( 3, header, check_cap, assign ); - } - - append = def_function( "append", UnusedCode, params, t_void, body ); + body->add( untyped_str( txt( + Data[ header.Num ] = value; + header.Num++; + + return true; + ))); } Code back; { - Code body = untyped_str( - "Header& header = get_header();" - "\n" "return data[ header.Num - 1 ];" - ); + Code body = untyped_str( txt( + Header& header = get_header(); - back = def_function( "back", UnusedCode, UnusedCode, type, body ); + return data[ header.Num - 1 ]; + )); + + back = def_proc( "back", UnusedCode, UnusedCode, type, body ); } 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 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 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( - "if ( begin < 0 || end >= header.Num )" - "\n" "fatal( \"Range out of bounds\" );" - ); + Code iter = untyped_str( txt( + for ( sw index = begin; index < end; index++ ) + 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 ); + body = def_proc_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 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 param = def_parameters( 1, t_uw, "min_capacity" ); + Code param = def_params( 1, t_uw, "min_capacity" ); Code body; { - 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 new_capacity = def_variable( t_uw, "new_capacity", untyped_str("grow_formula( header.Capacity )") ); - Code check_n_set = untyped_str( - "if ( new_capacity < min_capacity )" - "\n" "new_capacity = min_capacity;" - ); + Code check_n_set = untyped_str( txt( + if ( new_capacity < min_capacity ) + new_capacity = min_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 body; { - Code header = def_variable( "header", ref_header, untyped_str("get_header()") ); - Code assertion = untyped_str( "assert( header.Num > 0 );" ); 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 params = def_parameters( 1, t_uw, "new_capacity" ); + Code params = def_params( 1, t_uw, "new_capacity" ); Code body; { - Code header = def_variable( "header", ref_header, untyped_str("get_header()") ); - Code check_n_set = untyped_str( "if ( header.Capacity < new_capacity )" "\n" "return set_capacity( new_capacity );" @@ -275,80 +265,65 @@ 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 param = def_parameters( 1, t_uw, "new_num" ); + Code param = def_params( 1, t_uw, "new_num" ); Code body; { - Code header = def_variable( "header", ref_header, untyped_str("get_header()") ); - - Code check_n_grow = untyped_str( - "if ( header.Capacity < new_num )" - "\n" "if ( ! grow( new_num) )" - "\n" "return false;" - ); + Code check_n_grow = untyped_str( txt( + if ( header.Capacity < new_num ) + if ( ! grow( new_num) ) + return false; + )); 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 ); + 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 param = def_parameters( 1, t_uw, "capacity" ); - - Code body; + Code set_capacity = parse_proc( txt_with_length( + bool set_capacity( new_capacity ) { - Code header = def_variable( "header", ref_header, untyped_str("get_header()") ); + Header& header = get_header(); - Code checks = untyped_str( - "if ( capacity == header.Capacity )" - "\n" "return true;" - - "if ( capacity < header.Num )" - "\n" "header.Num = capacity;" - ); + if ( capacity == header.Capacity ) + return true; - 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 ));")); + if ( capacity < header.Num ) + header.Num = capacity; - 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 );" - ); + uw size = sizeof(Header) + sizeof(Type) * capacity; + Header* new_header = rcast( Header* alloc( header.Allocator, size )); - 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;" - ); + if ( new_header == nullptr ) + return false; - body = def_function_body( 6, 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; + new_header->Num = header.Num; + new_header->Capacity = header.Capacity; + + free( header ); + + *Data = new_header + 1; + + return true; } - - set_capacity = def_function( "set_capacity", UnusedCode, param, t_bool, body ); - } + )); string name = bprintf( "Array_%s", type_str ); @@ -374,256 +349,219 @@ 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 ); + typename( allocator, allocator ); - value( v_nullptr, nullptr ); + untyped( 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 ); + typename( elem_type, type_str ); + typename_fmt( ptr_type, "%s*", type_str ); + typename_fmt( ref_type, "%&", type_str ); // From ArrayBase - type( t_header, Header ); - type( ptr_header, Header* ); - type( ref_header, Header& ); + typename( header, Header ); + typename( ptr_header, Header* ); + typename( ref_header, Header& ); - def( array_def ) + Code array_def; { - using( Type, type ); - var( Data, ptr_type, __, __ ); + using_type( Type, elem_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 ) - { - var(header, ptr_header, untyped_str("rcast( Header*, alloc( mem_handler, sizeof(Header) + sizeof(Type) + capacity))" ), __); + Code + body = init_reserve.body(); + body->add_var( ptr_header, header, untyped( + value_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() );" + body->add_untyped( + if (header == nullptr) + return false; ); - 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 ];" + body->add_untyped( + header->Num = 0; + header->Capacity = capacity; + header->Allocator = mem_handler; ); - function( back, __, type, __, body ); + body->add_untyped( + Data = rcast( Type*, header + 1 ); + return true; + ); } - def( clear ) - function( clear, __, t_void, __, untyped_str("get_header().Num = 0;") ); - - def( fill ); + Code free; { - def( body ) + proc( free, __, t_void, __, untyped( + Header& header = 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; + ); + + body->add_untyped( assign, + Data[ header.Num ] = value; + header.Num++; + + return true; + ); + } + + Code back; + { + Code body = untyped( + Header& header = get_header(); + + return data[ header.Num - 1 ]; + ); + + proc( back, __, t_elem_type, __, body ); + } + + Code clear; + 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" ); + ); + + Code iter = untyped( + for ( sw index = begin; index < end; index++ ) + Data[index] = vallue; + ); + + Code body = proc_body( header, check, iter ); + + proc( fill, __, t_void, params( t_uw, "begin", t_uw, "end", t_elem_type, "value" ), body ); + } + + Code get_header; + proc( get_header, spec_inline, t_ref_header, __, untyped_str("return pcast( Header, Data - 1);") ); + + Code grow; + { + Code body; { - var( header, ref_header, untyped_str("get_header()"), __ ); + variable( uw, new_capacity, untyped( grow_formula( header.Capacity) )); - Code check = untyped_str( - "if ( begin < 0 || end >= header.Num )" - "\n" "fatal( \"Range out of bounds\" );" + Code check_n_set = untyped( + if ( new_capacity < min_capacity ) + new_capacity = min_capacity; ); - Code iter = untyped_str( - "for ( sw index = begin; index < end; index++ )" - "\n" "Data[index] = vallue;" - ); + Code ret = untyped( return set_capacity( new_capacity ); ); - 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 ) - function( get_header, spec_inline, ref_header, __, untyped_str("return pcast( Header, Data - 1);") ); - - def( grow ) + Code pop; { - def( body ) - { - var( header, ref_header, untyped_str("get_header()"), __ ); - var( new_capacity, t_uw, untyped_str("grow_formula( header.Capacity)"), __); + untyped_code( assertion, assert( header.Num > 0 ); ); + untyped_code( decrement, header.Num--; ); - Code check_n_set = untyped_str( - "if ( new_capacity < min_capacity )" - "new_capacity = min_capacity;" + Code body = proc_body( header, assertion, decrement ); + + proc( pop, __, t_void, __, body ); + } + + Code reserve; + { + untyped_code( check_n_set, + if ( header.Capacity < new_capacity ) + return set_capacity( new_capacity ); + ); + + Code ret = untyped_str("return true"); + + Code body = proc_body( header, check_n_set, ret ); + + proc( reserve, __, t_bool, params( t_uw, "new_capacity" ), body ); + } + + Code resize; + { + Code body; + { + untyped_code( check_n_grow, + if ( header.Capacity < new_num ) + if ( ! grow( new_num) ) + return false; ); - Code ret = untyped_str( "return set_capacity( new_capacity );" ); + untyped_code( set_n_ret, + header.Count = new_num; + return true; + ); - body = function_body( header, new_capacity, check_n_set, ret ); + body = proc_body( header, check_n_grow, set_n_ret ); } - function( grow, __, t_bool, params( t_uw, "min_capacity" ), body ); + proc( resize, __, t_bool, params( t_uw, "new_num" ), body ); } - def( pop ) - { - def( body ) + Code set_capacity = proc_code( + bool set_capacity( new_capacity ) { - var( header, ref_header, get_header(), UnusedCode ); + Header& header = get_header(); - Code assertion = untyped_str( "assert( header.Num > 0 );" ); - Code decrement = untyped_str( "header.Num--; " ); + if ( capacity == header.Capacity ) + return true; - body = function_body( header, assertion, decrement ); + if ( capacity < header.Num ) + header.Num = capacity; + + uw size = sizeof(Header) + sizeof(Type) * capacity; + Header* new_header = rcast( Header* alloc( header.Allocator, size )); + + if ( new_header == nullptr ) + return false; + + memmove( new_header, & header, sizeof( Header ) + sizeof(Type) * header.Num ); + + new_header->Allocator = header.Allocator; + new_header->Num = header.Num; + new_header->Capacity = header.Capacity; + + free( header ); + + *Data = new_header + 1; + + return true; } - - 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 ); diff --git a/test/Readme.md b/test/Readme.md new file mode 100644 index 0000000..31ca264 --- /dev/null +++ b/test/Readme.md @@ -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. \ No newline at end of file diff --git a/test/math.hpp b/test/math.hpp index be5eb85..5d34c30 100644 --- a/test/math.hpp +++ b/test/math.hpp @@ -25,18 +25,34 @@ #ifndef GEN_DEFINE_DSL string name = string_sprintf( g_allocator, (char*)sprintf_buf, ZPL_PRINTF_MAXLEN, "square", type ); + #if 1 Code square; { - Code params = def_parameters( 1, integral_type, "value" ); - Code specifiers = def_specifiers( 1, Specifier::Inline ); - Code ret_stmt = untyped_fmt( "return value * value;" ); + 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_function( name, specifiers, params, integral_type, ret_stmt ); + square = def_proc( name, specifiers, params, integral_type, ret_stmt ); } + #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 - def( square ) - function( square, __, integral_type, params( integral_type, "value" ), untyped_str("return value * value") ); + + Code proc( square, __, integral_type, params( integral_type, "value" ), + untyped(return value * value) + ); #endif if ( ! square ) diff --git a/test/test.cpp b/test/test.cpp index 618733a..610155b 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -17,7 +17,7 @@ int gen_main() int result = gen_math(); - result = gen_array_file(); + // result = gen_array_file(); Memory::cleanup(); return result;