Iterations on serialization improvements.

This commit is contained in:
Edward R. Gonzalez 2023-08-01 20:56:00 -04:00
parent 684569750d
commit 4c8a0f0005
4 changed files with 48 additions and 38 deletions

View File

@ -38,10 +38,13 @@ String AST::to_string()
case Comment: case Comment:
{ {
result.append("\n");
static char line[MaxCommentLineLength]; static char line[MaxCommentLineLength];
s32 left = Content.length(); s32 left = Content.length();
s32 index = 0; s32 index = 0;
s32 curr = 0;
do do
{ {
s32 length = 0; s32 length = 0;
@ -49,12 +52,17 @@ String AST::to_string()
{ {
length++; length++;
left--; left--;
index++;
} }
index++;
str_copy( line, Content, length ); str_copy( line, Content + curr, length );
line[length] = '\0'; result.append_fmt( "//%.*s", length, line );
mem_set( line, 0, MaxCommentLineLength);
result.append_fmt( "// %s", line ); length++;
left--;
curr = index;
} }
while ( left--, left > 0 ); while ( left--, left > 0 );
} }
@ -115,7 +123,7 @@ String AST::to_string()
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 && Parent->Type != ECode::Typedef ) if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) )
result.append(";"); result.append(";");
} }
break; break;
@ -130,7 +138,7 @@ String AST::to_string()
else result.append_fmt( "class %s", Name ); else result.append_fmt( "class %s", Name );
if ( Parent && Parent->Type != ECode::Typedef ) if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) )
result.append(";"); result.append(";");
} }
break; break;
@ -164,7 +172,7 @@ String AST::to_string()
, Body->to_string() , Body->to_string()
); );
if ( Parent && Parent->Type != ECode::Typedef ) if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) )
result.append(";"); result.append(";");
} }
break; break;
@ -179,7 +187,7 @@ String AST::to_string()
result.append_fmt( "enum %s : %s", Name, UnderlyingType->to_string() ); result.append_fmt( "enum %s : %s", Name, UnderlyingType->to_string() );
if ( Parent && Parent->Type != ECode::Typedef ) if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) )
result.append(";"); result.append(";");
} }
break; break;
@ -221,7 +229,7 @@ String AST::to_string()
); );
} }
if ( Parent && Parent->Type != ECode::Typedef ) if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) )
result.append(";"); result.append(";");
} }
break; break;
@ -238,7 +246,7 @@ String AST::to_string()
result.append_fmt( "%s : %s", Name, UnderlyingType->to_string() ); result.append_fmt( "%s : %s", Name, UnderlyingType->to_string() );
if ( Parent && Parent->Type != ECode::Typedef ) if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) )
result.append(";"); result.append(";");
} }
break; break;
@ -282,7 +290,7 @@ String AST::to_string()
result.append_fmt( "%s ", Attributes->to_string() ); result.append_fmt( "%s ", Attributes->to_string() );
if ( Specs ) if ( Specs )
result.append_fmt( "%s\n", 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 );
@ -322,7 +330,7 @@ String AST::to_string()
result.append_fmt( "%s ", Attributes->to_string() ); result.append_fmt( "%s ", Attributes->to_string() );
if ( Specs ) if ( Specs )
result.append_fmt( "%s\n", 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 );
@ -514,7 +522,7 @@ String AST::to_string()
break; break;
case Preprocess_Define: case Preprocess_Define:
result.append_fmt( "#define %s %s", Name, Content ); result.append_fmt( "#define %s%s", Name, Content );
break; break;
case Preprocess_If: case Preprocess_If:
@ -542,7 +550,7 @@ String AST::to_string()
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:
@ -619,7 +627,7 @@ String AST::to_string()
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 && Parent->Type != ECode::Typedef ) if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) )
result.append(";"); result.append(";");
} }
break; break;
@ -634,7 +642,7 @@ String AST::to_string()
else result.append_fmt( "struct %s", Name ); else result.append_fmt( "struct %s", Name );
if ( Parent && Parent->Type != ECode::Typedef ) if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) )
result.append(";"); result.append(";");
} }
break; break;
@ -659,11 +667,11 @@ String AST::to_string()
if ( UnderlyingType->Type == Typename && UnderlyingType->ArrExpr ) if ( UnderlyingType->Type == Typename && UnderlyingType->ArrExpr )
{ {
result.append_fmt( "[%s];\n", UnderlyingType->ArrExpr->to_string() ); result.append_fmt( "[%s];", UnderlyingType->ArrExpr->to_string() );
} }
else else
{ {
result.append( ";\n" ); result.append( ";" );
} }
} }
break; break;
@ -713,9 +721,7 @@ String AST::to_string()
); );
} }
bool add_semicolon = Parent && Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable; if ( Parent == nullptr || ( Parent->Type != ECode::Typedef && Parent->Type != ECode::Variable ) )
if ( add_semicolon )
result.append(";"); result.append(";");
} }
break; break;
@ -765,7 +771,7 @@ String AST::to_string()
result.append_fmt( "[%s]", ValueType->ArrExpr->to_string() ); result.append_fmt( "[%s]", ValueType->ArrExpr->to_string() );
if ( BitfieldSize ) if ( BitfieldSize )
result.append_fmt( " : %lu", BitfieldSize ); 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() );
@ -776,7 +782,7 @@ String AST::to_string()
} }
if ( BitfieldSize ) if ( BitfieldSize )
result.append_fmt( "%s : %lu", ValueType->to_string(), BitfieldSize ); result.append_fmt( "%s : %s", 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];", UnderlyingType->to_string(), Name, UnderlyingType->ArrExpr->to_string() );

View File

@ -257,6 +257,8 @@ namespace Parser
{ {
case '#': case '#':
{ {
char const* hash = scanner;
move_forward(); move_forward();
SkipWhitespace(); SkipWhitespace();
@ -292,13 +294,13 @@ 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++;
continue; continue;
} }
else else
@ -316,7 +318,7 @@ namespace Parser
if ( current == '\n' ) if ( current == '\n' )
{ {
move_forward(); move_forward();
token.Length++; // token.Length++;
break; break;
} }
@ -324,6 +326,8 @@ namespace Parser
token.Length++; token.Length++;
} }
token.Text = hash;
token.Length = (sptr)token.Text + token.Length - (sptr)hash;
Tokens.append( token ); Tokens.append( token );
continue; // Skip found token, its all handled here. continue; // Skip found token, its all handled here.
} }
@ -434,7 +438,7 @@ namespace Parser
if ( current == '\n' ) if ( current == '\n' )
{ {
move_forward(); move_forward();
content.Length++; // content.Length++;
break; break;
} }
@ -1183,8 +1187,8 @@ 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 ); // content.Text = str_fmt_buf( "%.*s\n", content.Length, content.Text );
content.Length++; // content.Length++;
assert->Content = get_cached_string( content ); assert->Content = get_cached_string( content );
assert->Name = assert->Content; assert->Name = assert->Content;
@ -1979,7 +1983,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)currtok.Text + currtok.Length ) - (sptr)expr_tok.Text - 1;
expr = untyped_str( expr_tok ); expr = untyped_str( expr_tok );
} }
@ -2043,7 +2047,7 @@ Code parse_simple_preprocess( Parser::TokType which )
push_scope(); push_scope();
Token tok = currtok; Token tok = currtok;
tok.Text = str_fmt_buf( "%.*s\n", tok.Length, tok.Text ); tok.Text = str_fmt_buf( "%.*s", tok.Length, tok.Text );
tok.Length++; tok.Length++;
Code result = untyped_str( tok ); Code result = untyped_str( tok );
eat( which ); eat( which );

View File

@ -1,6 +1,6 @@
void Builder::print( Code code ) void Builder::print( Code code )
{ {
Buffer.append_fmt( "%s\n", code->to_string() ); Buffer.append_fmt( "%s", code->to_string() );
} }
void Builder::print_fmt( char const* fmt, ... ) void Builder::print_fmt( char const* fmt, ... )

View File

@ -20,16 +20,16 @@ void check_singleheader_ast()
log_fmt("generated AST!!!\n"); log_fmt("generated AST!!!\n");
s32 idx = 0; // s32 idx = 0;
for ( Code entry : ast ) // for ( Code entry : ast )
{ // {
log_fmt("Entry %d: %s", idx, entry.to_string() ); // log_fmt("Entry %d: %s", idx, entry.to_string() );
idx++; // idx++;
} // }
Builder builder; Builder builder;
builder.open( "singleheader_copy.gen.hpp" ); builder.open( "singleheader_copy.gen.hpp" );
log_fmt("serializng ast\n"); log_fmt("\n\nserializng ast\n");
builder.print( ast ); builder.print( ast );
builder.write(); builder.write();