mirror of
https://github.com/Ed94/gencpp.git
synced 2025-06-14 18:51:47 -07:00
Started sanity test suite (Non-parsed api)
Made some fixes based on errors found while iterating on first generation
This commit is contained in:
@ -84,12 +84,12 @@ using zpl::str_fmt_va;
|
||||
using zpl::str_fmt_out_va;
|
||||
using zpl::str_fmt_out_err_va;
|
||||
using zpl::str_compare;
|
||||
using zpl::string_appendc;
|
||||
using zpl::string_append_fmt;
|
||||
using zpl::string_append_length;
|
||||
using zpl::string_make_length;
|
||||
using zpl::string_length;
|
||||
using zpl::string_make;
|
||||
// using zpl::string_appendc;
|
||||
// using zpl::string_append_fmt;
|
||||
// using zpl::string_append_length;
|
||||
// using zpl::string_make_length;
|
||||
// using zpl::string_length;
|
||||
// using zpl::string_make;
|
||||
using zpl::str_len;
|
||||
|
||||
#if __clang__
|
||||
@ -198,7 +198,8 @@ char const* Msg_Invalid_Value = "INVALID VALUE PROVIDED";
|
||||
sw Len;
|
||||
char const* Ptr;
|
||||
|
||||
static StrC from( char const* str )
|
||||
static constexpr
|
||||
StrC from( char const* str )
|
||||
{
|
||||
return { str_len( str ), str };
|
||||
}
|
||||
@ -210,7 +211,7 @@ char const* Msg_Invalid_Value = "INVALID VALUE PROVIDED";
|
||||
};
|
||||
|
||||
// Dynamic String
|
||||
// This is directly based off the ZPL string api.
|
||||
// This is directly based off the ZPL string api.
|
||||
// They used a header pattern
|
||||
// I kept it for simplicty of porting but its not necessary to keep it that way.
|
||||
struct String
|
||||
@ -268,7 +269,7 @@ char const* Msg_Invalid_Value = "INVALID VALUE PROVIDED";
|
||||
if ( ! str )
|
||||
mem_set( allocation, 0, alloc_size );
|
||||
|
||||
Header&
|
||||
Header&
|
||||
header = * rcast(Header*, allocation);
|
||||
header = { allocator, length, length };
|
||||
|
||||
@ -339,7 +340,7 @@ char const* Msg_Invalid_Value = "INVALID VALUE PROVIDED";
|
||||
// NOTE: Return if there is enough space left
|
||||
if ( available >= add_len )
|
||||
{
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -371,7 +372,6 @@ char const* Msg_Invalid_Value = "INVALID VALUE PROVIDED";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool append( char const* str )
|
||||
{
|
||||
return append( str, str_len( str ) );
|
||||
@ -379,15 +379,15 @@ char const* Msg_Invalid_Value = "INVALID VALUE PROVIDED";
|
||||
|
||||
bool append( char const* str, sw length )
|
||||
{
|
||||
Header& header = get_header();
|
||||
|
||||
if ( sptr(str) > 0 )
|
||||
{
|
||||
sw curr_len = header.Length;
|
||||
sw curr_len = this->length();
|
||||
|
||||
if ( make_space_for( str, length ) )
|
||||
if ( ! make_space_for( str, length ) )
|
||||
return false;
|
||||
|
||||
Header& header = get_header();
|
||||
|
||||
mem_copy( Data + curr_len, str, length );
|
||||
|
||||
Data[ curr_len + length ] = '\0';
|
||||
|
149
project/gen.cpp
149
project/gen.cpp
@ -506,8 +506,8 @@ namespace gen
|
||||
}
|
||||
|
||||
String AST::to_string()
|
||||
{
|
||||
# define ProcessModuleFlags() \
|
||||
{
|
||||
# define ProcessModuleFlags() \
|
||||
if ( bitfield_is_equal( u32, ModuleFlags, ModuleFlag::Export )) \
|
||||
result.append( "export " ); \
|
||||
\
|
||||
@ -524,11 +524,33 @@ namespace gen
|
||||
s32 tab_count = 0;
|
||||
{
|
||||
AST* curr_parent = Parent;
|
||||
|
||||
while ( Parent )
|
||||
while ( curr_parent )
|
||||
{
|
||||
switch ( curr_parent->Type )
|
||||
{
|
||||
using namespace ECode;
|
||||
|
||||
case Class_Body:
|
||||
case Enum_Body:
|
||||
case Extern_Linkage_Body:
|
||||
case Function_Body:
|
||||
case Global_Body:
|
||||
case Namespace_Body:
|
||||
case Struct_Body:
|
||||
case Union_Body:
|
||||
break;
|
||||
|
||||
default:
|
||||
{
|
||||
curr_parent = curr_parent->Parent;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
indent_str[tab_count] = '\t';
|
||||
tab_count++;
|
||||
|
||||
curr_parent = curr_parent->Parent;
|
||||
}
|
||||
}
|
||||
StrC indent = { tab_count, indent_str };
|
||||
@ -558,14 +580,17 @@ namespace gen
|
||||
{
|
||||
s32 length = 0;
|
||||
while ( left && Content[index] != '\n' )
|
||||
{
|
||||
length++;
|
||||
left--;
|
||||
}
|
||||
|
||||
str_copy( line, Content, length );
|
||||
line[length] = '\0';
|
||||
|
||||
result.append_fmt( "// %s\n", line );
|
||||
result.append_fmt( "// %s", line );
|
||||
}
|
||||
while ( left--, left );
|
||||
while ( left--, left > 0 );
|
||||
}
|
||||
break;
|
||||
|
||||
@ -585,32 +610,43 @@ namespace gen
|
||||
|
||||
ProcessModuleFlags();
|
||||
|
||||
result.append( "class " );
|
||||
|
||||
s32 idx = 1;
|
||||
|
||||
if ( Entries[idx]->Type == Attributes )
|
||||
if ( num_entries() > 1 )
|
||||
{
|
||||
result.append_fmt( "%s ", Entries[idx]->to_string() );
|
||||
idx++;
|
||||
result.append( "class " );
|
||||
|
||||
s32 idx = 1;
|
||||
|
||||
if ( Entries[idx]->Type == Attributes )
|
||||
{
|
||||
result.append_fmt( "%s ", Entries[idx]->to_string() );
|
||||
idx++;
|
||||
}
|
||||
|
||||
result.append( Name );
|
||||
|
||||
AST* parent = Entries[idx];
|
||||
|
||||
if ( parent )
|
||||
{
|
||||
char const* access_level = to_str( ParentAccess );
|
||||
|
||||
result.append_fmt( ": %s %s\n%s{\n"
|
||||
, access_level
|
||||
, parent
|
||||
, indent_str
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.append( "\n{\n" );
|
||||
}
|
||||
|
||||
result.append_fmt( "%s\n%s};\n", body()->to_string(), indent_str );
|
||||
}
|
||||
|
||||
result.append( Name );
|
||||
|
||||
AST* parent = Entries[idx];
|
||||
|
||||
if ( parent )
|
||||
else
|
||||
{
|
||||
char const* access_level = to_str( ParentAccess );
|
||||
|
||||
result.append_fmt( ": %s %s\n%s{\n"
|
||||
, access_level
|
||||
, parent
|
||||
, indent_str
|
||||
);
|
||||
result.append_fmt( "class %s\n{\n%s\n%s};\n", Name, body()->to_string(), indent_str );
|
||||
}
|
||||
|
||||
result.append_fmt( "%s\n%s};\n", body()->to_string(), indent_str );
|
||||
}
|
||||
break;
|
||||
|
||||
@ -820,31 +856,42 @@ namespace gen
|
||||
ProcessModuleFlags();
|
||||
|
||||
u32 idx = 0;
|
||||
u32 left = array_count( Entries );
|
||||
u32 left = num_entries();
|
||||
|
||||
if ( Entries[idx]->Type == Attributes )
|
||||
AST* Entry = Entries[idx];
|
||||
|
||||
if ( Entry && Entry->Type == Attributes )
|
||||
{
|
||||
result.append_fmt( "%s ", Entries[idx]->to_string() );
|
||||
result.append_fmt( "%s ", Entry->to_string() );
|
||||
idx++;
|
||||
left--;
|
||||
Entry = Entries[idx];
|
||||
}
|
||||
|
||||
if ( Entries[idx]->Type == Specifiers )
|
||||
if ( Entry && Entry->Type == Specifiers )
|
||||
{
|
||||
result.append_fmt( "%s\n", Entries[idx]->to_string() );
|
||||
result.append_fmt( "%s\n", Entry->to_string() );
|
||||
idx++;
|
||||
left--;
|
||||
Entry = Entries[idx];
|
||||
}
|
||||
|
||||
result.append_fmt( "\n%s %s(", Entries[idx]->to_string(), Name );
|
||||
idx++;
|
||||
left--;
|
||||
|
||||
if ( left && Entries[idx]->Type == Parameters )
|
||||
if ( Entry && Entry->Type == Typename )
|
||||
{
|
||||
result.append_fmt( "%s", Entries[idx]->to_string() );
|
||||
result.append_fmt( "%s ", Entry->to_string() );
|
||||
idx++;
|
||||
left--;
|
||||
Entry = Entries[idx];
|
||||
}
|
||||
|
||||
result.append_fmt( "%s(", Name );
|
||||
|
||||
if ( left && Entry && Entry->Type == Parameters )
|
||||
{
|
||||
result.append_fmt("%s", Entry->to_string() );
|
||||
idx++;
|
||||
left--;
|
||||
Entry = Entries[idx];
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -1073,7 +1120,7 @@ namespace gen
|
||||
break;
|
||||
|
||||
case Typename:
|
||||
if ( Entries[0] )
|
||||
if ( num_entries() && Entries[0] )
|
||||
{
|
||||
result.append_fmt( "%s %s", Name, Entries[0]->to_string() );
|
||||
}
|
||||
@ -1486,6 +1533,7 @@ namespace gen
|
||||
result->StaticIndex = 0;
|
||||
result->Readonly = false;
|
||||
result->DynamicEntries = false;
|
||||
result->Entries = result->ArrStatic;
|
||||
|
||||
return result;
|
||||
}
|
||||
@ -2189,7 +2237,7 @@ namespace gen
|
||||
return Code::Invalid;
|
||||
}
|
||||
|
||||
if ( ret_type == nullptr || ret_type->Type != Typename )
|
||||
if ( ret_type && ret_type->Type != Typename )
|
||||
{
|
||||
log_failure( "gen::def_function: ret_type was not a Typename: %s", ret_type->debug_str() );
|
||||
return Code::Invalid;
|
||||
@ -5738,7 +5786,7 @@ namespace gen
|
||||
return result;
|
||||
}
|
||||
|
||||
Code untyped_fmt(char const* fmt, ...)
|
||||
Code untyped_fmt( char const* fmt, ...)
|
||||
{
|
||||
local_persist thread_local
|
||||
char buf[ZPL_PRINTF_MAXLEN] = { 0 };
|
||||
@ -5783,7 +5831,20 @@ namespace gen
|
||||
#pragma region Builder
|
||||
void Builder::print( Code code )
|
||||
{
|
||||
Buffer.append_fmt( "%s\n\n", code.to_string() );
|
||||
Buffer.append_fmt( "%s\n", code.to_string() );
|
||||
}
|
||||
|
||||
void Builder::print_fmt( char const* fmt, ... )
|
||||
{
|
||||
sw res;
|
||||
char buf[ ZPL_PRINTF_MAXLEN ] = { 0 };
|
||||
|
||||
va_list va;
|
||||
va_start( va, fmt );
|
||||
res = str_fmt_va( buf, count_of( buf ) - 1, fmt, va ) - 1;
|
||||
va_end( va );
|
||||
|
||||
Buffer.append( buf, res );
|
||||
}
|
||||
|
||||
bool Builder::open( char const* path )
|
||||
@ -5803,7 +5864,7 @@ namespace gen
|
||||
|
||||
void Builder::write()
|
||||
{
|
||||
bool result = file_write( & File, Buffer, string_length(Buffer) );
|
||||
bool result = file_write( & File, Buffer, Buffer.length() );
|
||||
|
||||
if ( result == false )
|
||||
log_failure("gen::File::write - Failed to write to file: %s", file_name( & File ) );
|
||||
|
@ -14,7 +14,7 @@
|
||||
// #define GEN_DEFINE_DSL
|
||||
#define GEN_DEFINE_LIBRARY_CODE_CONSTANTS
|
||||
// #define GEN_DONT_USE_FATAL
|
||||
#define GEN_ENFORCE_READONLY_AST
|
||||
// #define GEN_ENFORCE_READONLY_AST
|
||||
|
||||
#define GEN_FEATURE_INCREMENTAL
|
||||
// #define GEN_FEATURE_PARSING
|
||||
@ -939,6 +939,7 @@ namespace gen
|
||||
String Buffer;
|
||||
|
||||
void print( Code );
|
||||
void print_fmt( char const* fmt, ... );
|
||||
|
||||
bool open( char const* path );
|
||||
void write();
|
||||
|
Reference in New Issue
Block a user