Finished to_string initial impl

This commit is contained in:
Edward R. Gonzalez 2023-04-23 00:43:31 -04:00
parent 7ba474069c
commit 09491be375
5 changed files with 328 additions and 76 deletions

View File

@ -339,6 +339,8 @@ Interface :
* def_typedef * def_typedef
* def_using * def_using
Bodies:
* def_class_body * def_class_body
* def_enum_body * def_enum_body
* def_function_body NOTE: Use this for operator bodies as well. * def_function_body NOTE: Use this for operator bodies as well.
@ -411,6 +413,8 @@ Interface :
* parse_typedef * parse_typedef
* parse_using * parse_using
Multiple defs:
* parse_classes * parse_classes
* parse_enums * parse_enums
* parse_functions * parse_functions

View File

@ -426,7 +426,7 @@ namespace gen
} }
} }
String AST::to_string() const String AST::to_string()
{ {
String result = string_make( g_allocator, "" ); String result = string_make( g_allocator, "" );
@ -452,117 +452,220 @@ namespace gen
break; break;
case Class: case Class:
{
result = string_append_fmt( result, "class");
s32 idx = 1;
if ( Entries[idx]->Type == Specifiers )
{
result = string_append_fmt( result, " %s", Entries[idx]->to_string() );
idx++;
}
result = string_append_fmt( result, " %s", Name );
if ( parent() )
result = string_append_fmt( result, " : %s", parent()->to_string() );
result = string_append_fmt( result, "\n{\n%s\n};\n", body()->to_string() );
}
break; break;
case Class_Fwd: case Class_Fwd:
break; {
result = string_append_fmt( result, "class");
case Class_Body: s32 idx = 1;
if ( Entries[idx]->Type == Specifiers )
{
result = string_append_fmt( result, " %s", Entries[idx]->to_string() );
idx++;
}
result = string_append_fmt( result, " %s;", Name );
}
break; break;
case Enum: case Enum:
if ( underlying_type() )
{
result = string_append_fmt( result, "enum %s : %s\n{\n%s\n};\n"
, Name
, underlying_type()->to_string()
, body()->to_string()
);
}
else
{
result = string_append_fmt( result, "enum %s\n{\n%s\n};\n"
, Name
, body()->to_string()
);
}
break; break;
case Enum_Fwd: case Enum_Fwd:
break; result = string_append_fmt( result, "enum %s : %s;\n", Name, underlying_type()->to_string() );
case Enum_Body:
break; break;
case Enum_Class: case Enum_Class:
if ( underlying_type() )
{
result = string_append_fmt( result, "enum class %s : %s\n{\n%s\n};\n"
, Name
, underlying_type()->to_string()
, body()->to_string()
);
}
else
{
result = string_append_fmt( result, "enum class %s\n{\n%s\n};\n"
, Name
, body()->to_string()
);
}
break; break;
case Enum_Class_Fwd: case Enum_Class_Fwd:
result = string_append_fmt( result, "enum class %s : %s;\n", Name, underlying_type()->to_string() );
break; break;
case Friend: case Friend:
result = string_append_fmt( result, "friend %s;\n", Entries[0]->to_string() );
break; break;
case Function: case Function:
{ {
u32 index = 0; u32 idx = 1;
u32 left = array_count( Entries ); u32 left = num_entries();
if ( left <= 0 ) if ( left <= 0 )
log_failure( "Code::to_string - Name: %s Type: %s, expected definition", Name, Type ); log_failure( "Code::to_string - Name: %s Type: %s, expected definition", Name, Type );
if ( Entries[index]->Type == Specifiers ) if ( Entries[idx]->Type == Specifiers )
{ {
result = string_append_fmt( result, "%s", Entries[index]->to_string() ); result = string_append_fmt( result, "%s", Entries[idx]->to_string() );
index++; idx++;
left--; left--;
} }
if ( left <= 0 ) if ( left <= 0 )
log_failure( "Code::to_string - Name: %s Type: %s, expected return type", Name, Type ); log_failure( "Code::to_string - Name: %s Type: %s, expected return type", Name, Type );
result = string_append_fmt( result, "\n%s %s(", Entries[index]->to_string(), Name ); result = string_append_fmt( result, "\n%s %s(", Entries[idx]->to_string(), Name );
index++; idx++;
left--; left--;
if ( left && Entries[index]->Type == Parameters ) if ( left && Entries[idx]->Type == Parameters )
{ {
result = string_append_fmt( result, "%s", Entries[index]->to_string() ); result = string_append_fmt( result, "%s", Entries[idx]->to_string() );
index++; idx++;
left--; left--;
} }
else
{
result = string_append_fmt( result, "void" );
}
result = string_append_fmt( result, ")\n{\n%s\n}", Entries[index]->to_string() ); result = string_append_fmt( result, ")\n{\n%s\n}", body()->to_string() );
} }
break; break;
case Function_Fwd: case Function_Fwd:
{ {
u32 index = 0; u32 idx = 0;
u32 left = array_count( Entries ); u32 left = array_count( Entries );
if ( left <= 0 ) if ( left <= 0 )
log_failure( "Code::to_string - Name: %s Type: %s, expected definition", Name, Type ); log_failure( "Code::to_string - Name: %s Type: %s, expected definition", Name, Type );
if ( Entries[index]->Type == Specifiers ) if ( Entries[idx]->Type == Specifiers )
{ {
result = string_append_fmt( result, "%s\n", Entries[index]->to_string() ); result = string_append_fmt( result, "%s\n", Entries[idx]->to_string() );
index++; idx++;
left--; left--;
} }
if ( left <= 0 ) if ( left <= 0 )
log_failure( "Code::to_string - Name: %s Type: %s, expected return type", Name, Type ); log_failure( "Code::to_string - Name: %s Type: %s, expected return type", Name, Type );
result = string_append_fmt( result, "\n%s %s(", Entries[index]->to_string(), Name ); result = string_append_fmt( result, "\n%s %s(", Entries[idx]->to_string(), Name );
index++; idx++;
left--; left--;
if ( left && Entries[index]->Type == Parameters ) if ( left && Entries[idx]->Type == Parameters )
{ {
result = string_append_fmt( result, "%s", Entries[index]->to_string() ); result = string_append_fmt( result, "%s", Entries[idx]->to_string() );
index++; idx++;
left--; left--;
} }
else
{
result = string_append_fmt( result, "void" );
}
result = string_appendc( result, ");\n" ); result = string_appendc( result, ");\n" );
} }
break; break;
case Function_Body:
log_failure("NOT SUPPORTED YET");
break;
case Global_Body:
break;
case Namespace: case Namespace:
log_failure("NOT SUPPORTED YET"); result = string_append_fmt( result, "namespace %s\n{\n%s\n};\n", Name, body()->to_string() );
break;
case Namespace_Body:
log_failure("NOT SUPPORTED YET");
break; break;
case Operator: case Operator:
case Operator_Member:
{
s32 idx = 1;
if ( Entries[idx]->Type == Specifiers )
{
result = string_append_fmt( result, "%s", Entries[idx]->to_string() );
idx++;
}
result = string_append_fmt( result, "%s operator%s(", Entries[idx]->to_string(), Name );
idx++;
if ( Entries[idx]->Type == Parameters )
{
result = string_append_fmt( result, "%s", Entries[idx]->to_string() );
idx++;
}
else
{
result = string_append_fmt( result, "void" );
}
result = string_append_fmt( result, ")\n{\n%s\n}", body()->to_string() );
}
break; break;
case Operator_Fwd: case Operator_Fwd:
case Operator_Member_Fwd:
{
s32 idx;
if ( Entries[idx]->Type == Specifiers )
{
result = string_append_fmt( result, "%s", Entries[idx]->to_string() );
idx++;
}
result = string_append_fmt( result, "%s operator%s(", Entries[idx]->to_string(), Name );
idx++;
if ( Entries[idx]->Type == Parameters )
{
result = string_append_fmt( result, "%s);\n", Entries[idx]->to_string() );
idx++;
}
else
{
result = string_append_fmt( result, "void);\n" );
}
}
break; break;
case Parameters: case Parameters:
@ -581,26 +684,70 @@ namespace gen
break; break;
case Specifiers: case Specifiers:
result = string_append_fmt( result, "%s", Content ); {
s32 idx = 0;
s32 left = StaticIndex;
while ( left--, left > 0 )
result = string_append_fmt( result, "%s ", ESpecifier::to_str( ArrSpecs[idx]) );
}
break; break;
case Struct: case Struct:
log_failure("NOT SUPPORTED YET"); {
result = string_append_fmt( result, "struct");
s32 idx = 1;
if ( Entries[idx]->Type == Specifiers )
{
result = string_append_fmt( result, " %s", Entries[idx]->to_string() );
idx++;
}
result = string_append_fmt( result, " %s", Name );
if ( parent() )
result = string_append_fmt( result, " : %s", parent()->to_string() );
result = string_append_fmt( result, "\n{\n%s\n};\n", body()->to_string() );
}
break; break;
case Struct_Fwd: case Struct_Fwd:
break; {
result = string_append_fmt( result, "struct");
case Struct_Body: s32 idx = 1;
log_failure("NOT SUPPORTED YET");
if ( Entries[idx]->Type == Specifiers )
{
result = string_append_fmt( result, " %s", Entries[idx]->to_string() );
idx++;
}
result = string_append_fmt( result, " %s;", Name );
}
break; break;
case Variable: case Variable:
log_failure("NOT SUPPORTED YET"); {
s32 idx = 1;
if ( Entries[idx]->Type == Specifiers )
{
result = string_append_fmt( result, "%s", Entries[idx]->to_string() );
idx++;
}
result = string_append_fmt( result, "%s %s", Entries[0]->to_string(), Name );
if ( Entries[idx] )
result = string_append_fmt( result, " = %s", Entries[idx]->to_string() );
}
break; break;
case Typedef: case Typedef:
log_failure("NOT SUPPORTED YET"); result = string_append_fmt( result, "typedef %s %s", Entries[0]->to_string(), Name );
break; break;
case Typename: case Typename:
@ -608,7 +755,34 @@ namespace gen
break; break;
case Using: case Using:
log_failure("NOT SUPPORTED YET"); if ( Entries[0] )
result = string_append_fmt( result, "using %s = %s", Name, Entries[0] );
else
result = string_append_fmt( result, "using %s", Name );
break;
case Using_Namespace:
result = string_append_fmt( result, "using namespace %s", Name );
break;
case Class_Body:
case Enum_Body:
case Function_Body:
case Global_Body:
case Namespace_Body:
case Struct_Body:
{
s32 index = 0;
s32 left = num_entries();
while ( left -- )
{
result = string_append_fmt( result, "%s\n", Entries[index]->to_string() );
index++;
}
}
break; break;
} }
@ -1326,12 +1500,12 @@ namespace gen
result->Type = Class_Fwd; result->Type = Class_Fwd;
} }
if ( parent )
result->add_entry( parent );
if ( specifiers ) if ( specifiers )
result->add_entry( specifiers ); result->add_entry( specifiers );
if ( parent )
result->add_entry( parent );
result.lock(); result.lock();
return result; return result;
} }
@ -1352,11 +1526,6 @@ namespace gen
result = make_code(); result = make_code();
result->Name = get_cached_string( name, length ); result->Name = get_cached_string( name, length );
if ( type )
{
result->add_entry( type );
}
if ( body ) if ( body )
{ {
switch ( body->Type ) switch ( body->Type )
@ -1381,6 +1550,16 @@ namespace gen
Enum_Class_Fwd : Enum_Fwd; Enum_Class_Fwd : Enum_Fwd;
} }
if ( type )
{
result->add_entry( type );
}
else if ( result->Type == Enum_Class_Fwd || result->Type == Enum_Fwd )
{
log_failure( "gen::def_enum: enum forward declaration must have an underlying type" );
return Code::Invalid;
}
result.lock(); result.lock();
return result; return result;
} }
@ -1569,13 +1748,13 @@ namespace gen
Operator_Fwd : Operator_Member_Fwd; Operator_Fwd : Operator_Member_Fwd;
} }
if (params_code) if ( specifiers )
result->add_entry( params_code ); result->add_entry( specifiers );
result->add_entry( ret_type ); result->add_entry( ret_type );
if ( specifiers ) if (params_code)
result->add_entry( specifiers ); result->add_entry( params_code );
result.lock(); result.lock();
return result; return result;
@ -1681,11 +1860,11 @@ namespace gen
result->Name = get_cached_string( name, length ); result->Name = get_cached_string( name, length );
result->Type = ECode::Variable; result->Type = ECode::Variable;
result->add_entry( type );
if ( specifiers ) if ( specifiers )
result->add_entry( specifiers ); result->add_entry( specifiers );
result->add_entry( type );
if ( value ) if ( value )
result->add_entry( value ); result->add_entry( value );
@ -1770,6 +1949,7 @@ namespace gen
case UsingRegular: case UsingRegular:
result->Type = ECode::Using; result->Type = ECode::Using;
if ( type )
result->add_entry( type ); result->add_entry( type );
break; break;
@ -2596,7 +2776,10 @@ namespace gen
inline inline
bool tok_is_specifier( Token const& tok ) bool tok_is_specifier( Token const& tok )
{ {
return tok.Type >= TokType::Spec_API && tok.Type <= TokType::Spec_Volatile; return tok.Type >= TokType::Spec_API && tok.Type <= TokType::Star
|| tok.Type == TokType::Ampersand
|| tok.Type == TokType::Ampersand_DBL
;
} }
# undef Define_TokType # undef Define_TokType
@ -3025,7 +3208,6 @@ namespace gen
} }
return { 0, Tokens }; return { 0, Tokens };
# undef current # undef current
# undef move_forward # undef move_forward
# undef SkipWhitespace # undef SkipWhitespace
@ -3397,6 +3579,7 @@ namespace gen
SpecifierT spec = ESpecifier::to_type( currtok.Text, currtok.Length ); SpecifierT spec = ESpecifier::to_type( currtok.Text, currtok.Length );
if ( spec != ESpecifier::Const if ( spec != ESpecifier::Const
&& spec != ESpecifier::Ptr
&& spec != ESpecifier::Ref && spec != ESpecifier::Ref
&& spec != ESpecifier::RValue && spec != ESpecifier::RValue
&& spec < ESpecifier::Type_Signed ) && spec < ESpecifier::Type_Signed )

View File

@ -330,8 +330,6 @@ namespace gen
return Entries[0]; return Entries[0];
} }
bool check();
AST* duplicate(); AST* duplicate();
forceinline forceinline
@ -352,6 +350,22 @@ namespace gen
return DynamicEntries ? array_count(Entries) : StaticIndex; return DynamicEntries ? array_count(Entries) : StaticIndex;
} }
// Class/Struct
forceinline
AST* parent()
{
return Entries[1];
}
// Enum
forceinline
AST* underlying_type()
{
return Entries[1];
}
// Parameter // Parameter
bool add_param( AST* type, s32 length, char const* name ); bool add_param( AST* type, s32 length, char const* name );
@ -459,7 +473,7 @@ namespace gen
return ECode::to_str( Type ); return ECode::to_str( Type );
} }
String to_string() const; String to_string();
#pragma endregion Member Functions #pragma endregion Member Functions
constexpr static constexpr static
@ -769,9 +783,9 @@ namespace gen
s32 parse_namespaces( s32 length, char const* namespace_defs, Code* out_namespaces_codes ); s32 parse_namespaces( s32 length, char const* namespace_defs, Code* out_namespaces_codes );
s32 parse_operators ( s32 length, char const* operator_defs, Code* out_operator_codes ); s32 parse_operators ( s32 length, char const* operator_defs, Code* out_operator_codes );
s32 parse_structs ( s32 length, char const* struct_defs, Code* out_struct_codes ); s32 parse_structs ( s32 length, char const* struct_defs, Code* out_struct_codes );
s32 parse_variables ( s32 length, char const* vars_def, Code* out_var_codes ); s32 parse_variables ( s32 length, char const* var_defs, Code* out_var_codes );
s32 parse_typedefs ( s32 length, char const* typedef_def, Code* out_typedef_codes ); s32 parse_typedefs ( s32 length, char const* typedef_defs, Code* out_typedef_codes );
s32 parse_usings ( s32 length, char const* usings_def, Code* out_using_codes ); s32 parse_usings ( s32 length, char const* using_defs, Code* out_using_codes );
#endif #endif
#pragma endregion Parsing #pragma endregion Parsing

51
test/sanity_suite.cpp Normal file
View File

@ -0,0 +1,51 @@
#include "Bloat.cpp"
#ifdef gen_time
#include "gen.cpp"
void case_untyped()
{
}
int gen_main()
{
Memory::setup();
log_fmt("\nPress any key after attaching to process\n");
getchar();
gen::init();
case_untyped();
Memory::cleanup();
return 0;
}
#endif
#ifdef runtime
int main()
{
return 0;
}
#endif

View File

@ -8,13 +8,13 @@ int gen_main()
{ {
Memory::setup(); Memory::setup();
zpl_printf("\nPress any key after attaching to process\n"); log_fmt("\nPress any key after attaching to process\n");
getchar(); getchar();
gen::init(); gen::init();
Memory::cleanup(); Memory::cleanup();
return result; return 0;
} }
#endif #endif