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

View File

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

View File

@ -1,6 +1,6 @@
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, ... )

View File

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