From 30eec99628ad3d93ef75e7b9d3116d5ac49a9309 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Wed, 23 Aug 2023 13:17:22 -0400 Subject: [PATCH] Changes to include usage, starting to attempt singleheader automated verification --- project/components/ast.cpp | 2 +- project/components/interface.hpp | 2 +- project/components/interface.parsing.cpp | 43 +++++++++++++++++++----- project/components/interface.upfront.cpp | 14 +++++--- test/validate.singleheader.cpp | 19 ++++++++++- 5 files changed, 64 insertions(+), 16 deletions(-) diff --git a/project/components/ast.cpp b/project/components/ast.cpp index 2fb0ff3..b0a3cc4 100644 --- a/project/components/ast.cpp +++ b/project/components/ast.cpp @@ -609,7 +609,7 @@ String AST::to_string() break; case Preprocess_Include: - result.append_fmt( "#include \"%S\"\n", Content ); + result.append_fmt( "#include %S\n", Content ); break; case Preprocess_ElIf: diff --git a/project/components/interface.hpp b/project/components/interface.hpp index 4b90046..91ecab9 100644 --- a/project/components/interface.hpp +++ b/project/components/interface.hpp @@ -68,7 +68,7 @@ CodeFn def_function( StrC name , CodeSpecifiers specifiers = NoCode, CodeAttributes attributes = NoCode , 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 ); CodeNS def_namespace( StrC name, Code body, ModuleFlag mflags = ModuleFlag::None ); diff --git a/project/components/interface.parsing.cpp b/project/components/interface.parsing.cpp index c65ef79..b20e8e6 100644 --- a/project/components/interface.parsing.cpp +++ b/project/components/interface.parsing.cpp @@ -56,6 +56,21 @@ namespace Parser { 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 }; @@ -303,6 +318,13 @@ namespace Parser 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 }; bool is_define = false; @@ -873,18 +895,15 @@ namespace Parser token.Text = scanner; token.Length = 1; token.Type = TokType::Operator; + move_forward(); if ( left ) { - move_forward(); - if ( current == '/' ) { token.Type = TokType::Comment; token.Length = 2; - move_forward(); - token.Length++; while ( left && current != '\n' && current != '\r' ) { @@ -909,9 +928,7 @@ namespace Parser { token.Type = TokType::Comment; token.Length = 2; - move_forward(); - token.Length++; bool star = current == '*'; bool slash = scanner[1] == '/'; @@ -925,11 +942,21 @@ namespace Parser slash = scanner[1] == '/'; at_end = star && slash; } - token.Length += 3; - Tokens.append( token ); + token.Length += 2; move_forward(); move_forward(); + if ( current == '\r' ) + { + move_forward(); + token.Length++; + } + if ( current == '\n' ) + { + move_forward(); + token.Length++; + } + Tokens.append( token ); end_line(); continue; } diff --git a/project/components/interface.upfront.cpp b/project/components/interface.upfront.cpp index 764c92a..af42b74 100644 --- a/project/components/interface.upfront.cpp +++ b/project/components/interface.upfront.cpp @@ -416,7 +416,7 @@ CodeComment def_comment( StrC content ) } static char line[ MaxCommentLineLength ]; - + String cmt_formatted = String::make_reserve( GlobalAllocator, kilobytes(1) ); char const* end = content.Ptr + content.Len; char const* scanner = content.Ptr; @@ -442,13 +442,13 @@ CodeComment def_comment( StrC content ) if ( cmt_formatted.back() != '\n' ) cmt_formatted.append( "\n" ); - + Code result = make_code(); result->Type = ECode::Comment; result->Name = get_cached_string( cmt_formatted ); result->Content = result->Name; - + cmt_formatted.free(); return (CodeComment) result; @@ -850,7 +850,7 @@ CodeFn def_function( StrC name return result; } -CodeInclude def_include ( StrC path ) +CodeInclude def_include( StrC path, bool foreign ) { if ( path.Len <= 0 || path.Ptr == nullptr ) { @@ -858,10 +858,14 @@ CodeInclude def_include ( StrC path ) 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 result = make_code(); result->Type = ECode::Preprocess_Include; - result->Name = get_cached_string( path ); + result->Name = get_cached_string( content ); result->Content = result->Name; return (CodeInclude) result; diff --git a/test/validate.singleheader.cpp b/test/validate.singleheader.cpp index e9d9388..6104cc4 100644 --- a/test/validate.singleheader.cpp +++ b/test/validate.singleheader.cpp @@ -27,7 +27,24 @@ void check_singleheader_ast() builder.print( ast ); 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(); }