Fixes and improvements to serialization.

There were multiple issues with comment and newline lexing.

Extended printing functions to support Strings with %S flag (captial 'S').
Allows for length detection. Also made it so that precision for strings is the string length.
This commit is contained in:
Edward R. Gonzalez 2023-08-08 22:14:58 -04:00
parent bb35444be9
commit b5fa864318
9 changed files with 223 additions and 224 deletions

View File

@ -26,7 +26,7 @@ String AST::to_string()
using namespace ECode; using namespace ECode;
case Invalid: case Invalid:
log_failure("Attempted to serialize invalid code! - %s", Parent ? Parent->debug_str() : Name ); log_failure("Attempted to serialize invalid code! - %S", Parent ? Parent->debug_str() : Name );
break; break;
case NewLine: case NewLine:
@ -40,6 +40,7 @@ String AST::to_string()
case Comment: case Comment:
{ {
if ( Prev && Prev->Type != Comment && Prev->Type != NewLine )
result.append("\n"); result.append("\n");
static char line[MaxCommentLineLength]; static char line[MaxCommentLineLength];
@ -49,7 +50,7 @@ String AST::to_string()
s32 curr = 0; s32 curr = 0;
do do
{ {
s32 length = 0; s32 length = 1;
while ( left && Content[index] != '\n' ) while ( left && Content[index] != '\n' )
{ {
length++; length++;
@ -67,6 +68,9 @@ String AST::to_string()
curr = index; curr = index;
} }
while ( left--, left > 0 ); while ( left--, left > 0 );
if ( result.back() != '\n' )
result.append("\n");
} }
break; break;
@ -90,43 +94,39 @@ String AST::to_string()
if ( Attributes ) if ( Attributes )
{ {
result.append_fmt( "%s ", Attributes->to_string() ); result.append_fmt( "%S ", Attributes->to_string() );
} }
if ( ParentType ) if ( ParentType )
{ {
char const* access_level = to_str( ParentAccess ); char const* access_level = to_str( ParentAccess );
result.append_fmt( "%s : %s %s\n{\n%s\n};" result.append_fmt( "%S : %s %S", Name, access_level, ParentType );
, Name
, access_level
, ParentType->to_string()
, Body->to_string()
);
CodeType interface = Next->cast<CodeType>(); CodeType interface = Next->cast< CodeType >();
if ( interface ) if ( interface )
result.append("\n"); result.append( "\n" );
while ( interface ) while ( interface )
{ {
result.append_fmt( ", %s", interface.to_string() ); result.append_fmt( ", %S", interface.to_string() );
interface = interface->Next ? interface->Next->cast< CodeType >() : Code { nullptr };
interface = interface->Next->cast<CodeType>();
} }
result.append_fmt( "\n{\n%S\n}", Body->to_string() );
} }
else else
{ {
result.append_fmt( "%s \n{\n%s\n}", Name, Body->to_string() ); result.append_fmt( "%S \n{\n%S\n}", Name, Body->to_string() );
} }
} }
else else
{ {
result.append_fmt( "class %s\n{\n%s\n}", Name, Body->to_string() ); result.append_fmt( "class %S\n{\n%S\n}", Name, Body->to_string() );
} }
if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) ) if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) )
result.append(";"); result.append(";\n");
} }
break; break;
@ -136,12 +136,12 @@ String AST::to_string()
result.append( "export " ); result.append( "export " );
if ( Attributes ) if ( Attributes )
result.append_fmt( "class %s %s", Attributes->to_string(), Name ); result.append_fmt( "class %S %S", Attributes->to_string(), Name );
else result.append_fmt( "class %s", Name ); else result.append_fmt( "class %S", Name );
if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) ) if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) )
result.append(";"); result.append(";\n");
} }
break; break;
@ -150,14 +150,14 @@ String AST::to_string()
result.append( Parent->Name ); result.append( Parent->Name );
if ( Params ) if ( Params )
result.append_fmt( "( %s )", Params->to_string() ); result.append_fmt( "( %S )", Params->to_string() );
else else
result.append( "(void)" ); result.append( "(void)" );
if ( InitializerList ) if ( InitializerList )
result.append_fmt( " : %s", InitializerList->to_string() ); result.append_fmt( " : %S", InitializerList->to_string() );
result.append_fmt( "\n{\n%s\n}", Body->to_string() ); result.append_fmt( "\n{\n%S\n}\n", Body->to_string() );
} }
break; break;
@ -166,9 +166,9 @@ String AST::to_string()
result.append( Parent->Name ); result.append( Parent->Name );
if ( Params ) if ( Params )
result.append_fmt( "( %s )", Params->to_string() ); result.append_fmt( "( %S )", Params->to_string() );
else else
result.append( "(void);" ); result.append( "(void);\n" );
} }
break; break;
@ -179,14 +179,14 @@ String AST::to_string()
CodeSpecifiers specs = Specs->cast<CodeSpecifiers>(); CodeSpecifiers specs = Specs->cast<CodeSpecifiers>();
if ( specs.has( ESpecifier::Virtual ) ) if ( specs.has( ESpecifier::Virtual ) )
result.append_fmt( "virtual ~%s()", Parent->Name ); result.append_fmt( "virtual ~%S()", Parent->Name );
else else
result.append_fmt( "~%s()", Parent->Name ); result.append_fmt( "~%S()", Parent->Name );
} }
else else
result.append_fmt( "~%s()", Parent->Name ); result.append_fmt( "~%S()", Parent->Name );
result.append_fmt( "\n{\n%s\n}", Body->to_string() ); result.append_fmt( "\n{\n%S\n}\n", Body->to_string() );
} }
break; break;
@ -197,15 +197,15 @@ String AST::to_string()
CodeSpecifiers specs = Specs->cast<CodeSpecifiers>(); CodeSpecifiers specs = Specs->cast<CodeSpecifiers>();
if ( specs.has( ESpecifier::Virtual ) ) if ( specs.has( ESpecifier::Virtual ) )
result.append_fmt( "virtual ~%s();", Parent->Name ); result.append_fmt( "virtual ~%S();\n", Parent->Name );
else else
result.append_fmt( "~%s()", Parent->Name ); result.append_fmt( "~%S()", Parent->Name );
if ( specs.has( ESpecifier::Pure ) ) if ( specs.has( ESpecifier::Pure ) )
result.append( " = 0;" ); result.append( " = 0;\n" );
} }
else else
result.append_fmt( "~%s();", Parent->Name ); result.append_fmt( "~%S();\n", Parent->Name );
} }
break; break;
@ -219,27 +219,21 @@ String AST::to_string()
result.append( "enum " ); result.append( "enum " );
if ( Attributes ) if ( Attributes )
result.append_fmt( "%s ", Attributes->to_string() ); result.append_fmt( "%S ", Attributes->to_string() );
if ( UnderlyingType ) if ( UnderlyingType )
result.append_fmt( "%s : %s\n{\n%s\n}" result.append_fmt( "%S : %S\n{\n%S\n}"
, Name , Name
, UnderlyingType->to_string() , UnderlyingType->to_string()
, Body->to_string() , Body->to_string()
); );
else result.append_fmt( "%s\n{\n%s\n}" else result.append_fmt( "%S\n{\n%S\n}", Name, Body->to_string() );
, Name
, Body->to_string()
);
} }
else result.append_fmt( "enum %s\n{\n%s\n}" else result.append_fmt( "enum %S\n{\n%S\n}", Name, Body->to_string() );
, Name
, Body->to_string()
);
if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) ) if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) )
result.append(";"); result.append(";\n");
} }
break; break;
@ -249,12 +243,12 @@ String AST::to_string()
result.append( "export " ); result.append( "export " );
if ( Attributes ) if ( Attributes )
result.append_fmt( "%s ", Attributes->to_string() ); result.append_fmt( "%S ", Attributes->to_string() );
result.append_fmt( "enum %s : %s", Name, UnderlyingType->to_string() ); result.append_fmt( "enum %S : %S", Name, UnderlyingType->to_string() );
if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) ) if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) )
result.append(";"); result.append(";\n");
} }
break; break;
@ -269,34 +263,25 @@ String AST::to_string()
if ( Attributes ) if ( Attributes )
{ {
result.append_fmt( "%s ", Attributes->to_string() ); result.append_fmt( "%S ", Attributes->to_string() );
} }
if ( UnderlyingType ) if ( UnderlyingType )
{ {
result.append_fmt( "%s : %s\n{\n%s\n}" result.append_fmt( "%S : %S\n{\n%S\n}", Name, UnderlyingType->to_string(), Body->to_string() );
, Name
, UnderlyingType->to_string()
, Body->to_string()
);
} }
else else
{ {
result.append_fmt( "%s\n{\n%s\n}" result.append_fmt( "%S\n{\n%S\n}", Name, Body->to_string() );
, Name
, Body->to_string()
);
} }
} }
else else
{ {
result.append_fmt( "enum class %s\n{\n%s\n}" result.append_fmt( "enum class %S\n{\n%S\n}", Body->to_string() );
, Body->to_string()
);
} }
if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) ) if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) )
result.append(";"); result.append(";\n");
} }
break; break;
@ -308,12 +293,12 @@ String AST::to_string()
result.append( "enum class " ); result.append( "enum class " );
if ( Attributes ) if ( Attributes )
result.append_fmt( "%s ", Attributes->to_string() ); result.append_fmt( "%S ", Attributes->to_string() );
result.append_fmt( "%s : %s", Name, UnderlyingType->to_string() ); result.append_fmt( "%S : %S", Name, UnderlyingType->to_string() );
if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) ) if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) )
result.append(";"); result.append(";\n");
} }
break; break;
@ -325,26 +310,23 @@ String AST::to_string()
s32 left = NumEntries; s32 left = NumEntries;
while ( left-- ) while ( left-- )
{ {
result.append_fmt( "%s", curr.to_string() ); result.append_fmt( "%S", curr.to_string() );
++curr; ++curr;
} }
result.append_fmt( "};" ); result.append_fmt( "};\n" );
} }
break; break;
case Extern_Linkage: case Extern_Linkage:
result.append_fmt( "extern \"%s\"\n{\n%s\n}" result.append_fmt( "extern \"%S\"\n{\n%S\n}\n", Name, Body->to_string() );
, Name
, Body->to_string()
);
break; break;
case Friend: case Friend:
result.append_fmt( "friend %s", Declaration->to_string() ); result.append_fmt( "friend %S", Declaration->to_string() );
if ( result[ result.length() -1 ] != ';' ) if ( result[ result.length() -1 ] != ';' )
result.append( ";" ); result.append( ";\n" );
break; break;
case Function: case Function:
@ -353,19 +335,19 @@ String AST::to_string()
result.append( "export " ); result.append( "export " );
if ( Attributes ) if ( Attributes )
result.append_fmt( "%s ", Attributes->to_string() ); result.append_fmt( "%S ", Attributes->to_string() );
if ( Specs ) if ( Specs )
result.append_fmt( "%s", Specs->to_string() ); result.append_fmt( "%S", Specs->to_string() );
if ( ReturnType ) if ( ReturnType )
result.append_fmt( "%s %s(", ReturnType->to_string(), Name ); result.append_fmt( "%S %S(", ReturnType->to_string(), Name );
else else
result.append_fmt( "%s(", Name ); result.append_fmt( "%S(", Name );
if ( Params ) if ( Params )
result.append_fmt( "%s)", Params->to_string() ); result.append_fmt( "%S)", Params->to_string() );
else else
result.append( "void)" ); result.append( "void)" );
@ -375,13 +357,14 @@ String AST::to_string()
for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() ) for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() )
{ {
if ( ESpecifier::is_trailing( spec ) ) if ( ESpecifier::is_trailing( spec ) )
result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) ); {
StrC spec_str = ESpecifier::to_str( spec );
result.append_fmt( " %.*s", spec_str.Len, spec_str.Ptr );
}
} }
} }
result.append_fmt( "\n{\n%s\n}" result.append_fmt( "\n{\n%S\n}\n", Body->to_string() );
, Body->to_string()
);
} }
break; break;
@ -391,19 +374,19 @@ String AST::to_string()
result.append( "export " ); result.append( "export " );
if ( Attributes ) if ( Attributes )
result.append_fmt( "%s ", Attributes->to_string() ); result.append_fmt( "%S ", Attributes->to_string() );
if ( Specs ) if ( Specs )
result.append_fmt( "%s", Specs->to_string() ); result.append_fmt( "%S", Specs->to_string() );
if ( ReturnType ) if ( ReturnType )
result.append_fmt( "%s %s(", ReturnType->to_string(), Name ); result.append_fmt( "%S %S(", ReturnType->to_string(), Name );
else else
result.append_fmt( "%s(", Name ); result.append_fmt( "%S(", Name );
if ( Params ) if ( Params )
result.append_fmt( "%s)", Params->to_string() ); result.append_fmt( "%S)", Params->to_string() );
else else
result.append( "void)" ); result.append( "void)" );
@ -413,11 +396,14 @@ String AST::to_string()
for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() ) for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() )
{ {
if ( ESpecifier::is_trailing( spec ) ) if ( ESpecifier::is_trailing( spec ) )
result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) ); {
StrC spec_str = ESpecifier::to_str( spec );
result.append_fmt( " %.*s", spec_str.Len, spec_str.Ptr );
}
} }
} }
result.append( ";" ); result.append( ";\n" );
} }
break; break;
@ -428,17 +414,14 @@ String AST::to_string()
if (((u32(ModuleFlag::Import) & u32(ModuleFlags)) == u32(ModuleFlag::Import))) if (((u32(ModuleFlag::Import) & u32(ModuleFlags)) == u32(ModuleFlag::Import)))
result.append("import "); result.append("import ");
result.append_fmt( "%s;", Name ); result.append_fmt( "%S;\n", Name );
break; break;
case Namespace: case Namespace:
if ( bitfield_is_equal( u32, ModuleFlags, ModuleFlag::Export )) if ( bitfield_is_equal( u32, ModuleFlags, ModuleFlag::Export ))
result.append( "export " ); result.append( "export " );
result.append_fmt( "namespace %s\n{\n%s\n}" result.append_fmt( "namespace %S\n{\n%S\n}\n", Name , Body->to_string() );
, Name
, Body->to_string()
);
break; break;
case Operator: case Operator:
@ -448,16 +431,16 @@ String AST::to_string()
result.append( "export " ); result.append( "export " );
if ( Attributes ) if ( Attributes )
result.append_fmt( "%s ", Attributes->to_string() ); result.append_fmt( "%S ", Attributes->to_string() );
if ( Attributes ) if ( Attributes )
result.append_fmt( "%s\n", Attributes->to_string() ); result.append_fmt( "%S\n", Attributes->to_string() );
if ( ReturnType ) if ( ReturnType )
result.append_fmt( "%s %s (", ReturnType->to_string(), Name ); result.append_fmt( "%S %S (", ReturnType->to_string(), Name );
if ( Params ) if ( Params )
result.append_fmt( "%s)", Params->to_string() ); result.append_fmt( "%S)", Params->to_string() );
else else
result.append( "void)" ); result.append( "void)" );
@ -467,11 +450,14 @@ String AST::to_string()
for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() ) for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() )
{ {
if ( ESpecifier::is_trailing( spec ) ) if ( ESpecifier::is_trailing( spec ) )
result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) ); {
StrC spec_str = ESpecifier::to_str( spec );
result.append_fmt( " %.*s", spec_str.Len, spec_str.Ptr );
}
} }
} }
result.append_fmt( "\n{\n%s\n}" result.append_fmt( "\n{\n%S\n}\n"
, Body->to_string() , Body->to_string()
); );
} }
@ -484,15 +470,15 @@ String AST::to_string()
result.append( "export " ); result.append( "export " );
if ( Attributes ) if ( Attributes )
result.append_fmt( "%s ", Attributes->to_string() ); result.append_fmt( "%S\n", Attributes->to_string() );
if ( Specs ) if ( Specs )
result.append_fmt( "%s", Specs->to_string() ); result.append_fmt( "%S\n", Specs->to_string() );
result.append_fmt( "%s %s (", ReturnType->to_string(), Name ); result.append_fmt( "%S %S (", ReturnType->to_string(), Name );
if ( Params ) if ( Params )
result.append_fmt( "%s)", Params->to_string() ); result.append_fmt( "%S)", Params->to_string() );
else else
result.append_fmt( "void)" ); result.append_fmt( "void)" );
@ -502,11 +488,14 @@ String AST::to_string()
for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() ) for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() )
{ {
if ( ESpecifier::is_trailing( spec ) ) if ( ESpecifier::is_trailing( spec ) )
result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) ); {
StrC spec_str = ESpecifier::to_str( spec );
result.append_fmt( " %.*s", spec_str.Len, spec_str.Ptr );
}
} }
} }
result.append( ";" ); result.append( ";\n" );
} }
break; break;
@ -515,105 +504,111 @@ String AST::to_string()
if ( Specs ) if ( Specs )
{ {
if ( Name && Name.length() ) if ( Name && Name.length() )
result.append_fmt( "%.*soperator %s()", Name.length(), Name, ValueType->to_string() ); result.append_fmt( "%Soperator %S()", Name, ValueType->to_string() );
else else
result.append_fmt( "operator %s()", ValueType->to_string() ); result.append_fmt( "operator %S()", ValueType->to_string() );
for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() ) for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() )
{ {
if ( ESpecifier::is_trailing( spec ) ) if ( ESpecifier::is_trailing( spec ) )
result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) ); {
StrC spec_str = ESpecifier::to_str( spec );
result.append_fmt( " %.*s", spec_str.Len, spec_str.Ptr );
}
} }
result.append_fmt( "\n{\n%s\n}", Body->to_string() ); result.append_fmt( "\n{\n%S\n}\n", Body->to_string() );
break; break;
} }
if ( Name && Name.length() ) if ( Name && Name.length() )
result.append_fmt("%.*soperator %s()\n{\n%s\n}", Name.length(), Name, ValueType->to_string(), Body->to_string() ); result.append_fmt("%Soperator %S()\n{\n%S\n}\n", Name, ValueType->to_string(), Body->to_string() );
else else
result.append_fmt("operator %s()\n{\n%s\n}", ValueType->to_string(), Body->to_string() ); result.append_fmt("operator %S()\n{\n%S\n}\n", ValueType->to_string(), Body->to_string() );
} }
break; break;
case Operator_Cast_Fwd: case Operator_Cast_Fwd:
if ( Specs ) if ( Specs )
{ {
result.append_fmt( "operator %s()", ValueType->to_string() ); result.append_fmt( "operator %S()", ValueType->to_string() );
for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() ) for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() )
{ {
if ( ESpecifier::is_trailing( spec ) ) if ( ESpecifier::is_trailing( spec ) )
result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) ); {
StrC spec_str = ESpecifier::to_str( spec );
result.append_fmt( " %*s", spec_str.Len, spec_str.Ptr );
}
} }
result.append( ";" ); result.append( ";" );
break; break;
} }
result.append_fmt("operator %s();", ValueType->to_string() ); result.append_fmt("operator %S();\n", ValueType->to_string() );
break; break;
case Parameters: case Parameters:
{ {
if ( ValueType == nullptr ) if ( ValueType == nullptr )
{ {
result.append_fmt( "%s", Name ); result.append_fmt( "%S", Name );
break; break;
} }
if ( Name ) if ( Name )
result.append_fmt( "%s %s", ValueType->to_string(), Name ); result.append_fmt( "%S %S", ValueType->to_string(), Name );
else else
result.append_fmt( "%s", ValueType->to_string() ); result.append_fmt( "%S", ValueType->to_string() );
if ( Value ) if ( Value )
result.append_fmt( "= %s", Value->to_string() ); result.append_fmt( "= %S", Value->to_string() );
if ( NumEntries - 1 > 0) if ( NumEntries - 1 > 0)
{ {
for ( CodeParam param : CodeParam { (AST_Param*) Next } ) for ( CodeParam param : CodeParam { (AST_Param*) Next } )
{ {
result.append_fmt( ", %s", param.to_string() ); result.append_fmt( ", %S", param.to_string() );
} }
} }
} }
break; break;
case Preprocess_Define: case Preprocess_Define:
result.append_fmt( "#define %s %s", Name, Content ); result.append_fmt( "#define %S %S\n", Name, Content );
break; break;
case Preprocess_If: case Preprocess_If:
result.append_fmt( "#if %s", Content ); result.append_fmt( "#if %S\n", Content );
break; break;
case Preprocess_IfDef: case Preprocess_IfDef:
result.append_fmt( "#ifdef %s", Content ); result.append_fmt( "#ifdef %S\n", Content );
break; break;
case Preprocess_IfNotDef: case Preprocess_IfNotDef:
result.append_fmt( "#ifndef %s", Content ); result.append_fmt( "#ifndef %S\n", Content );
break; break;
case Preprocess_Include: case Preprocess_Include:
result.append_fmt( "#include \"%s\"\n\n", Content ); result.append_fmt( "#include \"%S\"\n", Content );
break; break;
case Preprocess_ElIf: case Preprocess_ElIf:
result.append_fmt( "#elif %s", Content ); result.append_fmt( "#elif %S\n", Content );
break; break;
case Preprocess_Else: case Preprocess_Else:
result.append_fmt( "\n#else" ); result.append_fmt( "#else\n" );
break; break;
case Preprocess_EndIf: case Preprocess_EndIf:
result.append_fmt( "#endif" ); result.append_fmt( "#endif\n" );
break; break;
case Preprocess_Pragma: case Preprocess_Pragma:
result.append_fmt( "#pragma %s", Content ); result.append_fmt( "#pragma %S\n", Content );
break; break;
case Specifiers: case Specifiers:
@ -628,7 +623,8 @@ String AST::to_string()
continue; continue;
} }
result.append_fmt( "%s ", (char const*)ESpecifier::to_str( ArrSpecs[idx]) ); StrC spec = ESpecifier::to_str( ArrSpecs[idx] );
result.append_fmt( "%.*s ", spec.Len, spec.Ptr );
idx++; idx++;
} }
} }
@ -641,7 +637,7 @@ String AST::to_string()
if ( Name == nullptr) if ( Name == nullptr)
{ {
result.append_fmt( "struct\n{\n%s\n};", Body->to_string() ); result.append_fmt( "struct\n{\n%S\n};\n", Body->to_string() );
break; break;
} }
@ -650,44 +646,40 @@ String AST::to_string()
result.append( "struct " ); result.append( "struct " );
if ( Attributes ) if ( Attributes )
result.append_fmt( "%s ", Attributes->to_string() ); result.append_fmt( "%S ", Attributes->to_string() );
if ( ParentType ) if ( ParentType )
{ {
char const* access_level = to_str( ParentAccess ); char const* access_level = to_str( ParentAccess );
result.append_fmt( "%s : %s %s\n{\n%s\n};" result.append_fmt( "%S : %s %S", Name, access_level, ParentType );
, Name
, access_level
, ParentType->to_string()
, Body->to_string()
);
CodeType interface = Next->cast<CodeType>(); CodeType interface = Next->cast< CodeType >();
if ( interface ) if ( interface )
result.append("\n"); result.append( "\n" );
while ( interface ) while ( interface )
{ {
result.append_fmt( ", public %s", interface.to_string() ); result.append_fmt( ", %S", interface.to_string() );
interface = interface->Next->cast<CodeType>(); interface = interface->Next ? interface->Next->cast< CodeType >() : Code { nullptr };
} }
result.append_fmt( "\n{\n%S\n}", Body->to_string() );
} }
else else
{ {
if ( Name ) if ( Name )
result.append_fmt( "%S \n{\n%S\n}", Name, Body->to_string() );
result.append_fmt( "%s \n{\n%s\n}", Name, Body->to_string() );
} }
} }
else else
{ {
result.append_fmt( "struct %s\n{\n%s\n}", Name, Body->to_string() ); result.append_fmt( "struct %S\n{\n%S\n}", Name, Body->to_string() );
} }
if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) ) if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) )
result.append(";"); result.append(";\n");
} }
break; break;
@ -697,12 +689,12 @@ String AST::to_string()
result.append( "export " ); result.append( "export " );
if ( Attributes ) if ( Attributes )
result.append_fmt( "struct %s %s", Attributes->to_string(), Name ); result.append_fmt( "struct %S %S", Attributes->to_string(), Name );
else result.append_fmt( "struct %s", Name ); else result.append_fmt( "struct %S", Name );
if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) ) if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) )
result.append(";"); result.append(";\n");
} }
break; break;
@ -711,7 +703,7 @@ String AST::to_string()
if ( bitfield_is_equal( u32, ModuleFlags, ModuleFlag::Export )) if ( bitfield_is_equal( u32, ModuleFlags, ModuleFlag::Export ))
result.append( "export " ); result.append( "export " );
result.append_fmt( "template< %s >\n%s", Params->to_string(), Declaration->to_string() ); result.append_fmt( "template< %S >\n%S", Params->to_string(), Declaration->to_string() );
} }
break; break;
@ -725,15 +717,15 @@ String AST::to_string()
if ( IsFunction ) if ( IsFunction )
result.append( UnderlyingType->to_string() ); result.append( UnderlyingType->to_string() );
else else
result.append_fmt( "%s %s", UnderlyingType->to_string(), Name ); result.append_fmt( "%S %S", UnderlyingType->to_string(), Name );
if ( UnderlyingType->Type == Typename && UnderlyingType->ArrExpr ) if ( UnderlyingType->Type == Typename && UnderlyingType->ArrExpr )
{ {
result.append_fmt( "[%s];", UnderlyingType->ArrExpr->to_string() ); result.append_fmt( "[%S];", UnderlyingType->ArrExpr->to_string() );
} }
else else
{ {
result.append( ";" ); result.append( ";\n" );
} }
} }
break; break;
@ -743,17 +735,17 @@ String AST::to_string()
if ( Attributes || Specs ) if ( Attributes || Specs )
{ {
if ( Attributes ) if ( Attributes )
result.append_fmt( "%s ", Attributes->to_string() ); result.append_fmt( "%S ", Attributes->to_string() );
if ( Specs ) if ( Specs )
result.append_fmt( "%s %s", Name, Specs->to_string() ); result.append_fmt( "%S %S", Name, Specs->to_string() );
else else
result.append_fmt( "%s", Name ); result.append_fmt( "%S", Name );
} }
else else
{ {
result.append_fmt( "%s", Name ); result.append_fmt( "%S", Name );
} }
} }
break; break;
@ -766,11 +758,11 @@ String AST::to_string()
result.append( "union " ); result.append( "union " );
if ( Attributes ) if ( Attributes )
result.append_fmt( "%s ", Attributes->to_string() ); result.append_fmt( "%S ", Attributes->to_string() );
if ( Name ) if ( Name )
{ {
result.append_fmt( "%s\n{\n%s\n}" result.append_fmt( "%S\n{\n%S\n}"
, Name , Name
, Body->to_string() , Body->to_string()
); );
@ -778,13 +770,13 @@ String AST::to_string()
else else
{ {
// Anonymous union // Anonymous union
result.append_fmt( "\n{\n%s\n}" result.append_fmt( "\n{\n%S\n}"
, Body->to_string() , Body->to_string()
); );
} }
if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) ) if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) )
result.append(";"); result.append(";\n");
} }
break; break;
@ -794,24 +786,24 @@ String AST::to_string()
result.append( "export " ); result.append( "export " );
if ( Attributes ) if ( Attributes )
result.append_fmt( "%s ", Attributes->to_string() ); result.append_fmt( "%S ", Attributes->to_string() );
if ( UnderlyingType ) if ( UnderlyingType )
{ {
result.append_fmt( "using %s = %s", Name, UnderlyingType->to_string() ); result.append_fmt( "using %S = %S", Name, UnderlyingType->to_string() );
if ( UnderlyingType->ArrExpr ) if ( UnderlyingType->ArrExpr )
result.append_fmt( "[%s]", UnderlyingType->ArrExpr->to_string() ); result.append_fmt( "[%S]", UnderlyingType->ArrExpr->to_string() );
result.append( ";" ); result.append( ";\n" );
} }
else else
result.append_fmt( "using %s;", Name ); result.append_fmt( "using %S;\n", Name );
} }
break; break;
case Using_Namespace: case Using_Namespace:
result.append_fmt( "using namespace %s;", Name ); result.append_fmt( "using namespace %s;\n", Name );
break; break;
case Variable: case Variable:
@ -822,51 +814,38 @@ String AST::to_string()
if ( Attributes || Specs ) if ( Attributes || Specs )
{ {
if ( Attributes ) if ( Attributes )
result.append_fmt( "%s ", Specs->to_string() ); result.append_fmt( "%S ", Specs->to_string() );
if ( Specs ) if ( Specs )
result.append_fmt( "%s\n", Specs->to_string() ); result.append_fmt( "%S\n", Specs->to_string() );
result.append_fmt( "%s %s", ValueType->to_string(), Name ); result.append_fmt( "%S %S", ValueType->to_string(), Name );
if ( ValueType->ArrExpr ) if ( ValueType->ArrExpr )
result.append_fmt( "[%s]", ValueType->ArrExpr->to_string() ); result.append_fmt( "[%S]", ValueType->ArrExpr->to_string() );
if ( BitfieldSize ) if ( BitfieldSize )
result.append_fmt( " : %s", BitfieldSize->to_string() ); result.append_fmt( " : %S", BitfieldSize->to_string() );
if ( Value ) if ( Value )
result.append_fmt( " = %s", Value->to_string() ); result.append_fmt( " = %S", Value->to_string() );
result.append( ";" ); result.append( ";\n" );
break; break;
} }
if ( BitfieldSize ) if ( BitfieldSize )
result.append_fmt( "%s : %s", ValueType->to_string(), BitfieldSize->to_string() ); result.append_fmt( "%S : %S;\n", ValueType->to_string(), BitfieldSize->to_string() );
else if ( UnderlyingType->ArrExpr ) else if ( UnderlyingType->ArrExpr )
result.append_fmt( "%s %s[%s];", UnderlyingType->to_string(), Name, UnderlyingType->ArrExpr->to_string() ); result.append_fmt( "%S %S[%S];\n", UnderlyingType->to_string(), Name, UnderlyingType->ArrExpr->to_string() );
else else
result.append_fmt( "%s %s;", UnderlyingType->to_string(), Name ); result.append_fmt( "%S %S;\n", UnderlyingType->to_string(), Name );
} }
break; break;
#if 0
{
Code curr = Front->cast<Code>();
s32 left = NumEntries;
while ( left -- )
{
result.append_fmt( "%s", curr.to_string() );
++curr;
}
}
break;
#endif
case Enum_Body: case Enum_Body:
case Class_Body: case Class_Body:
case Extern_Linkage_Body: case Extern_Linkage_Body:
@ -880,11 +859,7 @@ String AST::to_string()
s32 left = NumEntries; s32 left = NumEntries;
while ( left -- ) while ( left -- )
{ {
result.append_fmt( "%s", curr.to_string() ); result.append_fmt( "%S", curr.to_string() );
if ( curr->Type != ECode::NewLine )
result.append( "\n" );
++curr; ++curr;
} }
} }

View File

@ -292,11 +292,13 @@ namespace Parser
if ( current == '\n' ) if ( current == '\n' )
{ {
token.Type = TokType::NewLine;
token.Length ++;
move_forward(); move_forward();
token.Type = TokType::NewLine;
token.Length++;
Tokens.append( token ); Tokens.append( token );
// log_fmt( "NewLine: %d\n", token.Line );
continue; continue;
} }
} }
@ -370,14 +372,14 @@ namespace Parser
if ( current == '\r' ) if ( current == '\r' )
{ {
// move_forward(); move_forward();
// token.Length++; token.Length++;
} }
if ( current == '\n' ) if ( current == '\n' )
{ {
// move_forward(); move_forward();
// token.Length++; token.Length++;
break; break;
} }
@ -502,13 +504,13 @@ namespace Parser
if ( current == '\r' ) if ( current == '\r' )
{ {
// move_forward(); move_forward();
// content.Length++; // content.Length++;
} }
if ( current == '\n' ) if ( current == '\n' )
{ {
// move_forward(); move_forward();
// content.Length++; // content.Length++;
break; break;
} }
@ -880,7 +882,8 @@ namespace Parser
Tokens.append( token ); Tokens.append( token );
move_forward(); move_forward();
Token content = { scanner, 1, TokType::Comment, line, column, false }; // move_forward();
Token content = { scanner, 0, TokType::Comment, line, column, false };
bool star = current == '*'; bool star = current == '*';
bool slash = scanner[1] == '/'; bool slash = scanner[1] == '/';
@ -1044,15 +1047,13 @@ namespace Parser
token.Length++; token.Length++;
} }
if ( current == '\r' ) if ( current == '\r' && scanner[1] == '\n' )
{ {
move_forward(); move_forward();
// token.Length++;
} }
if ( current == '\n' ) else if ( current == '\n' )
{ {
move_forward(); move_forward();
// token.Length++;
} }
} }
else else
@ -1326,10 +1327,9 @@ Code parse_static_assert()
content.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)content.Text; content.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)content.Text;
// content.Text = str_fmt_buf( "%.*s\n", content.Length, content.Text ); char const* result = str_fmt_buf( "%.*s\n", content.Length, content.Text );
// content.Length++;
assert->Content = get_cached_string( content ); assert->Content = get_cached_string( to_StrC( result ) );
assert->Name = assert->Content; assert->Name = assert->Content;
Context.pop(); Context.pop();
@ -2197,7 +2197,7 @@ CodeVar parse_variable_after_name(
eat( currtok.Type ); eat( currtok.Type );
} }
expr_tok.Length = ( (sptr)currtok.Text + currtok.Length ) - (sptr)expr_tok.Text; expr_tok.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)expr_tok.Text;
bitfield_expr = untyped_str( expr_tok ); bitfield_expr = untyped_str( expr_tok );
} }
@ -2262,7 +2262,9 @@ Code parse_simple_preprocess( Parser::TokType which )
tok.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)tok.Text; tok.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)tok.Text;
} }
Code result = untyped_str( tok ); char const* content = str_fmt_buf( "%.*s\n", tok.Length, tok.Text );
Code result = untyped_str( to_StrC( content ) );
Context.Scope->Name = tok; Context.Scope->Name = tok;
if ( str_compare( Context.Scope->Prev->ProcName.Ptr, "parse_typedef", Context.Scope->Prev->ProcName.Len ) != 0 ) if ( str_compare( Context.Scope->Prev->ProcName.Ptr, "parse_typedef", Context.Scope->Prev->ProcName.Len ) != 0 )
@ -2520,7 +2522,7 @@ CodeBody parse_class_struct_body( Parser::TokType which, Parser::Token name = Pa
Context.Scope->Start = currtok_noskip; Context.Scope->Start = currtok_noskip;
if ( currtok.Type == TokType::Preprocess_Hash ) if ( currtok_noskip.Type == TokType::Preprocess_Hash )
eat( TokType::Preprocess_Hash ); eat( TokType::Preprocess_Hash );
switch ( currtok_noskip.Type ) switch ( currtok_noskip.Type )
@ -2906,7 +2908,7 @@ CodeBody parse_global_nspace( CodeT which )
Context.Scope->Start = currtok_noskip; Context.Scope->Start = currtok_noskip;
if ( currtok.Type == TokType::Preprocess_Hash ) if ( currtok_noskip.Type == TokType::Preprocess_Hash )
eat( TokType::Preprocess_Hash ); eat( TokType::Preprocess_Hash );
switch ( currtok_noskip.Type ) switch ( currtok_noskip.Type )

View File

@ -1059,13 +1059,17 @@ CodePreprocessCond def_preprocess_cond( EPreprocessCond type, StrC expr )
switch (type) switch (type)
{ {
case EPreprocessCond::If: case EPreprocessCond::If:
result->Type = ECode::Preprocess_If; result->Type = Preprocess_If;
break;
case EPreprocessCond::IfDef: case EPreprocessCond::IfDef:
result->Type = Preprocess_IfDef; result->Type = Preprocess_IfDef;
break;
case EPreprocessCond::IfNotDef: case EPreprocessCond::IfNotDef:
result->Type = Preprocess_IfNotDef; result->Type = Preprocess_IfNotDef;
break;
case EPreprocessCond::ElIf: case EPreprocessCond::ElIf:
result->Type = Preprocess_ElIf; result->Type = Preprocess_ElIf;
break;
} }
return result; return result;

View File

@ -47,7 +47,8 @@ internal sw _print_string( char* text, sw max_len, _format_info* info, char cons
} }
if ( info && info->precision >= 0 ) if ( info && info->precision >= 0 )
len = str_len( str, info->precision ); // Made the design decision for this library that precision is the length of the string.
len = info->precision;
else else
len = str_len( str ); len = str_len( str );
@ -410,6 +411,15 @@ neverinline sw str_fmt_va( char* text, sw max_len, char const* fmt, va_list va )
len = _print_string( text, remaining, &info, va_arg( va, char* ) ); len = _print_string( text, remaining, &info, va_arg( va, char* ) );
break; break;
case 'S':
{
String gen_str = String { va_arg( va, char*) };
info.precision = gen_str.length();
len = _print_string( text, remaining, &info, gen_str );
}
break;
case 'r' : case 'r' :
len = _print_repeated_char( text, remaining, &info, va_arg( va, int ) ); len = _print_repeated_char( text, remaining, &info, va_arg( va, int ) );
break; break;

View File

@ -226,6 +226,11 @@ struct String
return header.Capacity - header.Length; return header.Capacity - header.Length;
} }
char& back()
{
return Data[ length() - 1 ];
}
sw capacity() const sw capacity() const
{ {
Header const& Header const&

View File

@ -53,8 +53,8 @@ CodeBody gen_ecode( char const* path )
))); )));
#pragma pop_macro( "local_persist" ) #pragma pop_macro( "local_persist" )
CodeNS nspace = def_namespace( name(ECode), def_namespace_body( args( enum_code, to_str ) ) ); CodeNS nspace = def_namespace( name(ECode), def_namespace_body( args( enum_code, to_str ) ) );
CodeUsing code_t = def_using( name(CodeT), def_type( name(ECode::Type) ) ); CodeUsing code_t = def_using( name(CodeT), def_type( name(ECode::Type) ) );
return def_global_body( args( nspace, code_t ) ); return def_global_body( args( nspace, code_t ) );
} }
@ -324,8 +324,8 @@ CodeBody gen_etoktype( char const* etok_path, char const* attr_path )
#pragma pop_macro( "do_once_start" ) #pragma pop_macro( "do_once_start" )
#pragma pop_macro( "do_once_end" ) #pragma pop_macro( "do_once_end" )
CodeNS nspace = def_namespace( name(ETokType), def_namespace_body( args( attribute_entires_def, enum_code, to_str, to_type ) ) ); CodeNS nspace = def_namespace( name(ETokType), def_namespace_body( args( attribute_entires_def, enum_code, to_str, to_type ) ) );
CodeUsing td_toktype = def_using( name(TokType), def_type( name(ETokType::Type) ) ); CodeUsing td_toktype = def_using( name(TokType), def_type( name(ETokType::Type) ) );
return def_global_body( args( nspace, td_toktype ) ); return def_global_body( args( nspace, td_toktype ) );
} }

View File

@ -23,6 +23,9 @@ $path_gen = Join-Path $path_test gen
$path_test_build = Join-Path $path_test build $path_test_build = Join-Path $path_test build
$path_gen_build = Join-Path $path_gen build $path_gen_build = Join-Path $path_gen build
# Invoke-Expression "& $(Join-Path $PSScriptRoot 'bootstrap.ci.ps1')"
# Invoke-Expression "& $(Join-Path $PSScriptRoot 'singleheader.ci.ps1')"
write-host "`n`nBuilding Test`n" write-host "`n`nBuilding Test`n"
if ( -not( Test-Path $path_gen_build ) ) if ( -not( Test-Path $path_gen_build ) )

View File

@ -118,9 +118,9 @@ void check_SOA()
{ {
log_fmt("\ncheck_SOA:"); log_fmt("\ncheck_SOA:");
gen::init(); gen::init();
Builder soa_test = Builder::open( "SOA.gen.hpp" ); Builder soa_test = Builder::open( "SOA.gen.hpp" );
soa_test.print( parse_using( code( soa_test.print( parse_using( code(
using u16 = unsigned short; using u16 = unsigned short;
))); )));

View File

@ -16,9 +16,9 @@ int gen_main()
// check_sanity(); // check_sanity();
check_SOA(); // check_SOA();
// check_singleheader_ast(); check_singleheader_ast();
return 0; return 0;
} }