Changes to include usage, starting to attempt singleheader automated verification

This commit is contained in:
Edward R. Gonzalez 2023-08-23 13:17:22 -04:00
parent f9117a2353
commit 30eec99628
5 changed files with 64 additions and 16 deletions

View File

@ -609,7 +609,7 @@ String AST::to_string()
break; break;
case Preprocess_Include: case Preprocess_Include:
result.append_fmt( "#include \"%S\"\n", Content ); result.append_fmt( "#include %S\n", Content );
break; break;
case Preprocess_ElIf: case Preprocess_ElIf:

View File

@ -68,7 +68,7 @@ CodeFn def_function( StrC name
, CodeSpecifiers specifiers = NoCode, CodeAttributes attributes = NoCode , CodeSpecifiers specifiers = NoCode, CodeAttributes attributes = NoCode
, ModuleFlag mflags = ModuleFlag::None ); , ModuleFlag mflags = ModuleFlag::None );
CodeInclude def_include ( StrC content ); CodeInclude def_include ( StrC content, bool foreign = false );
CodeModule def_module ( StrC name, ModuleFlag mflags = ModuleFlag::None ); CodeModule def_module ( StrC name, ModuleFlag mflags = ModuleFlag::None );
CodeNS def_namespace( StrC name, Code body, ModuleFlag mflags = ModuleFlag::None ); CodeNS def_namespace( StrC name, Code body, ModuleFlag mflags = ModuleFlag::None );

View File

@ -56,6 +56,21 @@ namespace Parser
{ {
return scast(AccessSpec, Type); return scast(AccessSpec, Type);
} }
String to_string()
{
String result = String::make_reserve( GlobalAllocator, kilobytes(4) );
StrC type_str = ETokType::to_str( Type );
result.append_fmt( "Line: %d Column: %d, Type: %.*s Content: %.*s"
, Line, Column
, type_str.Len, type_str.Ptr
, Length, Text
);
return result;
}
}; };
constexpr Token NullToken { nullptr, 0, TokType::Invalid, false, 0, 0 }; constexpr Token NullToken { nullptr, 0, TokType::Invalid, false, 0, 0 };
@ -303,6 +318,13 @@ namespace Parser
while (left ) while (left )
{ {
#if 0
if (Tokens.num())
{
log_fmt("\nLastTok: %S", Tokens.back().to_string());
}
#endif
Token token = { scanner, 0, TokType::Invalid, line, column, false }; Token token = { scanner, 0, TokType::Invalid, line, column, false };
bool is_define = false; bool is_define = false;
@ -873,18 +895,15 @@ namespace Parser
token.Text = scanner; token.Text = scanner;
token.Length = 1; token.Length = 1;
token.Type = TokType::Operator; token.Type = TokType::Operator;
move_forward();
if ( left ) if ( left )
{ {
move_forward();
if ( current == '/' ) if ( current == '/' )
{ {
token.Type = TokType::Comment; token.Type = TokType::Comment;
token.Length = 2; token.Length = 2;
move_forward(); move_forward();
token.Length++;
while ( left && current != '\n' && current != '\r' ) while ( left && current != '\n' && current != '\r' )
{ {
@ -909,9 +928,7 @@ namespace Parser
{ {
token.Type = TokType::Comment; token.Type = TokType::Comment;
token.Length = 2; token.Length = 2;
move_forward(); move_forward();
token.Length++;
bool star = current == '*'; bool star = current == '*';
bool slash = scanner[1] == '/'; bool slash = scanner[1] == '/';
@ -925,11 +942,21 @@ namespace Parser
slash = scanner[1] == '/'; slash = scanner[1] == '/';
at_end = star && slash; at_end = star && slash;
} }
token.Length += 3; token.Length += 2;
Tokens.append( token );
move_forward(); move_forward();
move_forward(); move_forward();
if ( current == '\r' )
{
move_forward();
token.Length++;
}
if ( current == '\n' )
{
move_forward();
token.Length++;
}
Tokens.append( token );
end_line(); end_line();
continue; continue;
} }

View File

@ -416,7 +416,7 @@ CodeComment def_comment( StrC content )
} }
static char line[ MaxCommentLineLength ]; static char line[ MaxCommentLineLength ];
String cmt_formatted = String::make_reserve( GlobalAllocator, kilobytes(1) ); String cmt_formatted = String::make_reserve( GlobalAllocator, kilobytes(1) );
char const* end = content.Ptr + content.Len; char const* end = content.Ptr + content.Len;
char const* scanner = content.Ptr; char const* scanner = content.Ptr;
@ -442,13 +442,13 @@ CodeComment def_comment( StrC content )
if ( cmt_formatted.back() != '\n' ) if ( cmt_formatted.back() != '\n' )
cmt_formatted.append( "\n" ); cmt_formatted.append( "\n" );
Code Code
result = make_code(); result = make_code();
result->Type = ECode::Comment; result->Type = ECode::Comment;
result->Name = get_cached_string( cmt_formatted ); result->Name = get_cached_string( cmt_formatted );
result->Content = result->Name; result->Content = result->Name;
cmt_formatted.free(); cmt_formatted.free();
return (CodeComment) result; return (CodeComment) result;
@ -850,7 +850,7 @@ CodeFn def_function( StrC name
return result; return result;
} }
CodeInclude def_include ( StrC path ) CodeInclude def_include( StrC path, bool foreign )
{ {
if ( path.Len <= 0 || path.Ptr == nullptr ) if ( path.Len <= 0 || path.Ptr == nullptr )
{ {
@ -858,10 +858,14 @@ CodeInclude def_include ( StrC path )
return CodeInvalid; return CodeInvalid;
} }
StrC content = foreign ?
to_str( str_fmt_buf( "<%.*s>\n", path.Len, path.Ptr ))
: to_str( str_fmt_buf( "\"%.*s\"\n", path.Len, path.Ptr ));
Code Code
result = make_code(); result = make_code();
result->Type = ECode::Preprocess_Include; result->Type = ECode::Preprocess_Include;
result->Name = get_cached_string( path ); result->Name = get_cached_string( content );
result->Content = result->Name; result->Content = result->Name;
return (CodeInclude) result; return (CodeInclude) result;

View File

@ -27,7 +27,24 @@ void check_singleheader_ast()
builder.print( ast ); builder.print( ast );
builder.write(); builder.write();
log_fmt("passed!! Time taken: %llu ms\n", time_rel_ms() - time_start); log_fmt("Serialized. Time taken: %llu ms\n", time_rel_ms() - time_start);
FileContents file_gen = file_read_contents( GlobalAllocator, true, "gen/singleheader_copy.gen.hpp" );
log_fmt("Reconstructing from generated file:\n");
time_start = time_rel_ms();
CodeBody ast_gen = parse_global_body( { file_gen.size, (char const*)file_gen.data } );
log_fmt("\nAst generated. Time taken: %llu ms\n", time_rel_ms() - time_start);
time_start = time_rel_ms();
if ( ast.is_equal( ast_gen ) )
log_fmt( "Passed!: AST passed validation!\n" );
else
log_fmt( "Failed: AST did not pass validation\n" );
log_fmt( "Time taken: %llu ms\n", time_rel_ms() - time_start );
gen::deinit(); gen::deinit();
} }