From f9c21ebc049f53dd6d11a8e2680def5a5e9a0011 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sun, 1 Dec 2024 23:35:58 -0500 Subject: [PATCH] progress --- project/auxillary/builder.cpp | 8 +- project/auxillary/scanner.hpp | 6 +- project/bootstrap.cpp | 10 +- project/components/ast.cpp | 13 +- project/components/code_serialization.cpp | 486 +++++++++++----------- project/components/interface.cpp | 4 +- project/components/interface.untyped.cpp | 3 +- project/components/interface.upfront.cpp | 8 +- project/components/lexer.cpp | 6 +- project/components/parser.cpp | 55 +-- project/dependencies/containers.hpp | 130 +++--- project/dependencies/strings.hpp | 293 +++++++------ project/helpers/helper.hpp | 26 +- 13 files changed, 540 insertions(+), 508 deletions(-) diff --git a/project/auxillary/builder.cpp b/project/auxillary/builder.cpp index 4632772..b283e96 100644 --- a/project/auxillary/builder.cpp +++ b/project/auxillary/builder.cpp @@ -21,7 +21,7 @@ Builder Builder::open( char const* path ) void Builder::pad_lines( s32 num ) { - append( Buffer, "\n" ); + append( & Buffer, "\n" ); } void Builder::print( Code code ) @@ -29,7 +29,7 @@ void Builder::print( Code code ) String str = code->to_string(); // const ssize len = str.length(); // log_fmt( "%s - print: %.*s\n", File.filename, len > 80 ? 80 : len, str.Data ); - append( Buffer, str ); + append( & Buffer, str ); } void Builder::print_fmt( char const* fmt, ... ) @@ -43,7 +43,7 @@ void Builder::print_fmt( char const* fmt, ... ) va_end( va ); // log_fmt( "$%s - print_fmt: %.*s\n", File.filename, res > 80 ? 80 : res, buf ); - append( Buffer, buf, res ); + append( & Buffer, buf, res ); } void Builder::write() @@ -55,5 +55,5 @@ void Builder::write() log_fmt( "Generated: %s\n", File.filename ); file_close( & File ); - free(Buffer); + free(& Buffer); } diff --git a/project/auxillary/scanner.hpp b/project/auxillary/scanner.hpp index 6540a4d..0cac91c 100644 --- a/project/auxillary/scanner.hpp +++ b/project/auxillary/scanner.hpp @@ -25,7 +25,7 @@ Code scan_file( char const* path ) String str = string_make_reserve( GlobalAllocator, fsize ); file_read( & file, str, fsize ); - get_header(str).Length = fsize; + get_header(str)->Length = fsize; // Skip GEN_INTELLISENSE_DIRECTIVES preprocessor blocks // Its designed so that the directive should be the first thing in the file. @@ -97,12 +97,12 @@ Code scan_file( char const* path ) if ( (scanner + 2) >= ( str.Data + fsize ) ) { mem_move( str, scanner, left ); - get_header(str).Length = left; + get_header(str)->Length = left; break; } mem_move( str, scanner, left ); - get_header(str).Length = left; + get_header(str)->Length = left; break; } diff --git a/project/bootstrap.cpp b/project/bootstrap.cpp index 24dd2bb..2a7332c 100644 --- a/project/bootstrap.cpp +++ b/project/bootstrap.cpp @@ -29,17 +29,17 @@ void format_file( char const* path ) String resolved_path = string_make(GlobalAllocator, to_str(path)); String style_arg = string_make(GlobalAllocator, txt("-style=file:")); - append( style_arg, "../scripts/.clang-format "); + append( & style_arg, "../scripts/.clang-format "); // Need to execute clang format on the generated file to get it to match the original. #define clang_format "clang-format " #define cf_format_inplace "-i " #define cf_verbose "-verbose " String command = string_make( GlobalAllocator, clang_format ); - append( command, cf_format_inplace ); - append( command, cf_verbose ); - append( command, style_arg ); - append( command, resolved_path ); + append( & command, cf_format_inplace ); + append( & command, cf_verbose ); + append( & command, style_arg ); + append( & command, resolved_path ); log_fmt("\tRunning clang-format on file:\n"); system( command ); log_fmt("\tclang-format finished reformatting.\n"); diff --git a/project/components/ast.cpp b/project/components/ast.cpp index 5f8fa43..792333f 100644 --- a/project/components/ast.cpp +++ b/project/components/ast.cpp @@ -10,7 +10,8 @@ Code Code::Invalid; char const* debug_str(AST* self) { GEN_ASSERT(self != nullptr); - String result = string_make_reserve( GlobalAllocator, kilobytes(1) ); + String result_stack = string_make_reserve( GlobalAllocator, kilobytes(1) ); + String* result = & result_stack; if ( self->Parent ) append_fmt( result, "\n\tParent : %S %S", self->Parent->type_str(), self->Name ? self->Name : "" ); @@ -356,7 +357,7 @@ char const* debug_str(AST* self) break; } - return result; + return * result; } AST* duplicate(AST* self) @@ -391,25 +392,25 @@ void AST::to_string( String& result ) #ifdef GEN_DONT_ALLOW_INVALID_CODE log_failure("Attempted to serialize invalid code! - %S", Parent ? Parent->debug_str() : Name ); #else - append_fmt( result, "Invalid Code!" ); + GEN_NS append_fmt( & result, "Invalid Code!" ); #endif break; case NewLine: - GEN_NS append( result,"\n"); + GEN_NS append( & result,"\n"); break; case Untyped: case Execution: case Comment: case PlatformAttributes: - GEN_NS append( result, Content ); + GEN_NS append( & result, Content ); break; case Access_Private: case Access_Protected: case Access_Public: - GEN_NS append( result, Name ); + GEN_NS append( & result, Name ); break; case Class: diff --git a/project/components/code_serialization.cpp b/project/components/code_serialization.cpp index 170feab..d8c90c5 100644 --- a/project/components/code_serialization.cpp +++ b/project/components/code_serialization.cpp @@ -26,7 +26,7 @@ String CodeBody::to_string() using namespace ECode; case Untyped: case Execution: - GEN_NS append( result, raw()->Content ); + GEN_NS append( & result, raw()->Content ); break; case Enum_Body: @@ -53,24 +53,24 @@ void CodeBody::to_string( String& result ) s32 left = ast->NumEntries; while ( left -- ) { - append_fmt( result, "%S", curr.to_string() ); + append_fmt( & result, "%S", curr.to_string() ); ++curr; } } void CodeBody::to_string_export( String& result ) { - append_fmt( result, "export\n{\n" ); + append_fmt( & result, "export\n{\n" ); Code curr = *this; s32 left = ast->NumEntries; while ( left-- ) { - append_fmt( result, "%S", curr.to_string() ); + append_fmt( & result, "%S", curr.to_string() ); ++curr; } - append_fmt( result, "};\n" ); + append_fmt( & result, "};\n" ); } String CodeComment::to_string() @@ -98,48 +98,48 @@ void CodeConstructor::to_string_def( String& result ) { AST* ClassStructParent = ast->Parent->Parent; if (ClassStructParent) { - append( result, ClassStructParent->Name ); + append( & result, ClassStructParent->Name ); } else { - append( result, ast->Name ); + append( & result, ast->Name ); } if ( ast->Params ) - append_fmt( result, "( %S )", ast->Params.to_string() ); + append_fmt( & result, "( %S )", ast->Params.to_string() ); else - append( result, "()" ); + append( & result, "()" ); if ( ast->InitializerList ) - append_fmt( result, " : %S", ast->InitializerList.to_string() ); + append_fmt( & result, " : %S", ast->InitializerList.to_string() ); if ( ast->InlineCmt ) - append_fmt( result, " // %S", ast->InlineCmt->Content ); + append_fmt( & result, " // %S", ast->InlineCmt->Content ); - append_fmt( result, "\n{\n%S\n}\n", ast->Body.to_string() ); + append_fmt( & result, "\n{\n%S\n}\n", ast->Body.to_string() ); } void CodeConstructor::to_string_fwd( String& result ) { AST* ClassStructParent = ast->Parent->Parent; if (ClassStructParent) { - append( result, ClassStructParent->Name ); + append( & result, ClassStructParent->Name ); } else { - append( result, ast->Name ); + append( & result, ast->Name ); } if ( ast->Params ) - append_fmt( result, "( %S )", ast->Params.to_string() ); + append_fmt( & result, "( %S )", ast->Params.to_string() ); else - append_fmt( result, "()"); + append_fmt( & result, "()"); if (ast->Body) - append_fmt( result, " = %S", ast->Body.to_string() ); + append_fmt( & result, " = %S", ast->Body.to_string() ); if ( ast->InlineCmt ) - append_fmt( result, "; // %S\n", ast->InlineCmt->Content ); + append_fmt( & result, "; // %S\n", ast->InlineCmt->Content ); else - append( result, ";\n" ); + append( & result, ";\n" ); } String CodeClass::to_string() @@ -161,64 +161,64 @@ String CodeClass::to_string() void CodeClass::to_string_def( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); - append( result, "class " ); + append( & result, "class " ); if ( ast->Attributes ) { - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); } if ( ast->ParentType ) { char const* access_level = to_str( ast->ParentAccess ); - append_fmt( result, "%S : %s %S", ast->Name, access_level, ast->ParentType.to_string() ); + append_fmt( & result, "%S : %s %S", ast->Name, access_level, ast->ParentType.to_string() ); CodeType interface = ast->ParentType->Next->code_cast< CodeType >(); if ( interface ) - append( result, "\n" ); + append( & result, "\n" ); while ( interface ) { - append_fmt( result, ", %S", interface.to_string() ); + append_fmt( & result, ", %S", interface.to_string() ); interface = interface->Next ? interface->Next->code_cast< CodeType >() : CodeType { nullptr }; } } else if ( ast->Name ) { - append( result, ast->Name ); + append( & result, ast->Name ); } if ( ast->InlineCmt ) { - append_fmt( result, " // %S", ast->InlineCmt->Content ); + append_fmt( & result, " // %S", ast->InlineCmt->Content ); } - append_fmt( result, "\n{\n%S\n}", ast->Body.to_string() ); + append_fmt( & result, "\n{\n%S\n}", ast->Body.to_string() ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) - append( result, ";\n"); + append( & result, ";\n"); } void CodeClass::to_string_fwd( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); if ( ast->Attributes ) - append_fmt( result, "class %S %S", ast->Attributes.to_string(), ast->Name ); + append_fmt( & result, "class %S %S", ast->Attributes.to_string(), ast->Name ); - else append_fmt( result, "class %S", ast->Name ); + else append_fmt( & result, "class %S", ast->Name ); // Check if it can have an end-statement if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) { if ( ast->InlineCmt ) - append_fmt( result, "; // %S\n", ast->InlineCmt->Content ); + append_fmt( & result, "; // %S\n", ast->InlineCmt->Content ); else - append( result,";\n"); + append( & result,";\n"); } } @@ -229,7 +229,7 @@ String CodeDefine::to_string() void CodeDefine::to_string( String& result ) { - append_fmt( result, "#define %S %S\n", ast->Name, ast->Content ); + append_fmt( & result, "#define %S %S\n", ast->Name, ast->Content ); } String CodeDestructor::to_string() @@ -252,19 +252,19 @@ void CodeDestructor::to_string_def( String& result ) { if ( ast->Name ) { - append_fmt( result, "%S()", ast->Name ); + append_fmt( & result, "%S()", ast->Name ); } else if ( ast->Specs ) { if ( ast->Specs.has( ESpecifier::Virtual ) ) - append_fmt( result, "virtual ~%S()", ast->Parent->Name ); + append_fmt( & result, "virtual ~%S()", ast->Parent->Name ); else - append_fmt( result, "~%S()", ast->Parent->Name ); + append_fmt( & result, "~%S()", ast->Parent->Name ); } else - append_fmt( result, "~%S()", ast->Parent->Name ); + append_fmt( & result, "~%S()", ast->Parent->Name ); - append_fmt( result, "\n{\n%S\n}\n", ast->Body.to_string() ); + append_fmt( & result, "\n{\n%S\n}\n", ast->Body.to_string() ); } void CodeDestructor::to_string_fwd( String& result ) @@ -272,22 +272,22 @@ void CodeDestructor::to_string_fwd( String& result ) if ( ast->Specs ) { if ( ast->Specs.has( ESpecifier::Virtual ) ) - append_fmt( result, "virtual ~%S();\n", ast->Parent->Name ); + append_fmt( & result, "virtual ~%S();\n", ast->Parent->Name ); else - append_fmt( result, "~%S()", ast->Parent->Name ); + append_fmt( & result, "~%S()", ast->Parent->Name ); if ( ast->Specs.has( ESpecifier::Pure ) ) - append( result, " = 0;" ); + append( & result, " = 0;" ); else if (ast->Body) - append_fmt( result, " = %S;", ast->Body.to_string() ); + append_fmt( & result, " = %S;", ast->Body.to_string() ); } else - append_fmt( result, "~%S();", ast->Parent->Name ); + append_fmt( & result, "~%S();", ast->Parent->Name ); if ( ast->InlineCmt ) - append_fmt( result, " %S", ast->InlineCmt->Content ); + append_fmt( & result, " %S", ast->InlineCmt->Content ); else - append( result, "\n"); + append( & result, "\n"); } String CodeEnum::to_string() @@ -315,108 +315,108 @@ String CodeEnum::to_string() void CodeEnum::to_string_def( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); if ( ast->Attributes || ast->UnderlyingType ) { - append( result, "enum " ); + append( & result, "enum " ); if ( ast->Attributes ) - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); if ( ast->UnderlyingType ) - append_fmt( result, "%S : %S\n{\n%S\n}" + append_fmt( & result, "%S : %S\n{\n%S\n}" , ast->Name , ast->UnderlyingType.to_string() , ast->Body.to_string() ); else if ( ast->UnderlyingTypeMacro ) - append_fmt( result, "%S : %S\n{\n%S\n}" + append_fmt( & result, "%S : %S\n{\n%S\n}" , ast->Name , ast->UnderlyingTypeMacro.to_string() , ast->Body.to_string() ); - else append_fmt( result, "%S\n{\n%S\n}", ast->Name, ast->Body.to_string() ); + else append_fmt( & result, "%S\n{\n%S\n}", ast->Name, ast->Body.to_string() ); } - else append_fmt( result, "enum %S\n{\n%S\n}", ast->Name, ast->Body.to_string() ); + else append_fmt( & result, "enum %S\n{\n%S\n}", ast->Name, ast->Body.to_string() ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) - append( result, ";\n"); + append( & result, ";\n"); } void CodeEnum::to_string_fwd( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); if ( ast->Attributes ) - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); if ( ast->UnderlyingType ) - append_fmt( result, "enum %S : %S", ast->Name, ast->UnderlyingType.to_string() ); + append_fmt( & result, "enum %S : %S", ast->Name, ast->UnderlyingType.to_string() ); else - append_fmt( result, "enum %S", ast->Name ); + append_fmt( & result, "enum %S", ast->Name ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) { if ( ast->InlineCmt ) - append_fmt( result, "; %S", ast->InlineCmt->Content ); + append_fmt( & result, "; %S", ast->InlineCmt->Content ); else - append( result, ";\n"); + append( & result, ";\n"); } } void CodeEnum::to_string_class_def( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); if ( ast->Attributes || ast->UnderlyingType ) { - append( result, "enum class " ); + append( & result, "enum class " ); if ( ast->Attributes ) { - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); } if ( ast->UnderlyingType ) { - append_fmt( result, "%S : %S\n{\n%S\n}", ast->Name, ast->UnderlyingType.to_string(), ast->Body.to_string() ); + append_fmt( & result, "%S : %S\n{\n%S\n}", ast->Name, ast->UnderlyingType.to_string(), ast->Body.to_string() ); } else { - append_fmt( result, "%S\n{\n%S\n}", ast->Name, ast->Body.to_string() ); + append_fmt( & result, "%S\n{\n%S\n}", ast->Name, ast->Body.to_string() ); } } else { - append_fmt( result, "enum class %S\n{\n%S\n}", ast->Body.to_string() ); + append_fmt( & result, "enum class %S\n{\n%S\n}", ast->Body.to_string() ); } if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) - append( result, ";\n"); + append( & result, ";\n"); } void CodeEnum::to_string_class_fwd( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); - append( result, "enum class " ); + append( & result, "enum class " ); if ( ast->Attributes ) - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); - append_fmt( result, "%S : %S", ast->Name, ast->UnderlyingType.to_string() ); + append_fmt( & result, "%S : %S", ast->Name, ast->UnderlyingType.to_string() ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) { if ( ast->InlineCmt ) - append_fmt( result, "; %S", ast->InlineCmt->Content ); + append_fmt( & result, "; %S", ast->InlineCmt->Content ); else - append( result, ";\n"); + append( & result, ";\n"); } } @@ -428,9 +428,9 @@ String CodeExec::to_string() void CodeExtern::to_string( String& result ) { if ( ast->Body ) - append_fmt( result, "extern \"%S\"\n{\n%S\n}\n", ast->Name, ast->Body.to_string() ); + append_fmt( & result, "extern \"%S\"\n{\n%S\n}\n", ast->Name, ast->Body.to_string() ); else - append_fmt( result, "extern \"%S\"\n{}\n", ast->Name ); + append_fmt( & result, "extern \"%S\"\n{}\n", ast->Name ); } String CodeInclude::to_string() @@ -440,7 +440,7 @@ String CodeInclude::to_string() void CodeInclude::to_string( String& result ) { - append_fmt( result, "#include %S\n", ast->Content ); + append_fmt( & result, "#include %S\n", ast->Content ); } String CodeFriend::to_string() @@ -452,17 +452,17 @@ String CodeFriend::to_string() void CodeFriend::to_string( String& result ) { - append_fmt( result, "friend %S", ast->Declaration->to_string() ); + append_fmt( & result, "friend %S", ast->Declaration->to_string() ); if ( ast->Declaration->Type != ECode::Function && result[ length(result) - 1 ] != ';' ) { - append( result, ";" ); + append( & result, ";" ); } if ( ast->InlineCmt ) - append_fmt( result, " %S", ast->InlineCmt->Content ); + append_fmt( & result, " %S", ast->InlineCmt->Content ); else - append( result, "\n"); + append( & result, "\n"); } String CodeFn::to_string() @@ -484,10 +484,10 @@ String CodeFn::to_string() void CodeFn::to_string_def( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export" ); + append( & result, "export" ); if ( ast->Attributes ) - append_fmt( result, " %S ", ast->Attributes.to_string() ); + append_fmt( & result, " %S ", ast->Attributes.to_string() ); bool prefix_specs = false; if ( ast->Specs ) @@ -497,7 +497,7 @@ void CodeFn::to_string_def( String& result ) if ( ! ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); prefix_specs = true; } @@ -505,19 +505,19 @@ void CodeFn::to_string_def( String& result ) } if ( ast->Attributes || prefix_specs ) - append( result, "\n" ); + append( & result, "\n" ); if ( ast->ReturnType ) - append_fmt( result, "%S %S(", ast->ReturnType.to_string(), ast->Name ); + append_fmt( & result, "%S %S(", ast->ReturnType.to_string(), ast->Name ); else - append_fmt( result, "%S(", ast->Name ); + append_fmt( & result, "%S(", ast->Name ); if ( ast->Params ) - append_fmt( result, "%S)", ast->Params.to_string() ); + append_fmt( & result, "%S)", ast->Params.to_string() ); else - append( result, ")" ); + append( & result, ")" ); if ( ast->Specs ) { @@ -526,21 +526,21 @@ void CodeFn::to_string_def( String& result ) if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } - append_fmt( result, "\n{\n%S\n}\n", ast->Body.to_string() ); + append_fmt( & result, "\n{\n%S\n}\n", ast->Body.to_string() ); } void CodeFn::to_string_fwd( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); if ( ast->Attributes ) - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); b32 prefix_specs = false; if ( ast->Specs ) @@ -550,7 +550,7 @@ void CodeFn::to_string_fwd( String& result ) if ( ! ESpecifier::is_trailing( spec ) || ! (spec != ESpecifier::Pure) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); prefix_specs = true; } @@ -559,20 +559,20 @@ void CodeFn::to_string_fwd( String& result ) if ( ast->Attributes || prefix_specs ) { - append( result, "\n" ); + append( & result, "\n" ); } if ( ast->ReturnType ) - append_fmt( result, "%S %S(", ast->ReturnType.to_string(), ast->Name ); + append_fmt( & result, "%S %S(", ast->ReturnType.to_string(), ast->Name ); else - append_fmt( result, "%S(", ast->Name ); + append_fmt( & result, "%S(", ast->Name ); if ( ast->Params ) - append_fmt( result, "%S)", ast->Params.to_string() ); + append_fmt( & result, "%S)", ast->Params.to_string() ); else - append( result, ")" ); + append( & result, ")" ); if ( ast->Specs ) { @@ -581,20 +581,20 @@ void CodeFn::to_string_fwd( String& result ) if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } if ( ast->Specs && ast->Specs.has( ESpecifier::Pure ) >= 0 ) - append( result, " = 0;" ); + append( & result, " = 0;" ); else if (ast->Body) - append_fmt( result, " = %S;", ast->Body.to_string() ); + append_fmt( & result, " = %S;", ast->Body.to_string() ); if ( ast->InlineCmt ) - append_fmt( result, "; %S", ast->InlineCmt->Content ); + append_fmt( & result, "; %S", ast->InlineCmt->Content ); else - append( result, ";\n" ); + append( & result, ";\n" ); } String CodeModule::to_string() @@ -607,12 +607,12 @@ String CodeModule::to_string() void CodeModule::to_string( String& result ) { if (((u32(ModuleFlag_Export) & u32(ast->ModuleFlags)) == u32(ModuleFlag_Export))) - append( result, "export "); + append( & result, "export "); if (((u32(ModuleFlag_Import) & u32(ast->ModuleFlags)) == u32(ModuleFlag_Import))) - append( result, "import "); + append( & result, "import "); - append_fmt( result, "%S;\n", ast->Name ); + append_fmt( & result, "%S;\n", ast->Name ); } String CodeNS::to_string() @@ -625,9 +625,9 @@ String CodeNS::to_string() void CodeNS::to_string( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); - append_fmt( result, "namespace %S\n{\n%S\n}\n", ast->Name , ast->Body.to_string() ); + append_fmt( & result, "namespace %S\n{\n%S\n}\n", ast->Name , ast->Body.to_string() ); } String CodeOperator::to_string() @@ -651,13 +651,13 @@ String CodeOperator::to_string() void CodeOperator::to_string_def( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); if ( ast->Attributes ) - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); if ( ast->Attributes ) - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); if ( ast->Specs ) { @@ -666,24 +666,24 @@ void CodeOperator::to_string_def( String& result ) if ( ! ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } if ( ast->Attributes || ast->Specs ) { - append( result, "\n" ); + append( & result, "\n" ); } if ( ast->ReturnType ) - append_fmt( result, "%S %S (", ast->ReturnType.to_string(), ast->Name ); + append_fmt( & result, "%S %S (", ast->ReturnType.to_string(), ast->Name ); if ( ast->Params ) - append_fmt( result, "%S)", ast->Params.to_string() ); + append_fmt( & result, "%S)", ast->Params.to_string() ); else - append( result, ")" ); + append( & result, ")" ); if ( ast->Specs ) { @@ -692,12 +692,12 @@ void CodeOperator::to_string_def( String& result ) if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } - append_fmt( result, "\n{\n%S\n}\n" + append_fmt( & result, "\n{\n%S\n}\n" , ast->Body.to_string() ); } @@ -705,10 +705,10 @@ void CodeOperator::to_string_def( String& result ) void CodeOperator::to_string_fwd( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); if ( ast->Attributes ) - append_fmt( result, "%S\n", ast->Attributes.to_string() ); + append_fmt( & result, "%S\n", ast->Attributes.to_string() ); if ( ast->Specs ) { @@ -717,23 +717,23 @@ void CodeOperator::to_string_fwd( String& result ) if ( ! ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } if ( ast->Attributes || ast->Specs ) { - append( result, "\n" ); + append( & result, "\n" ); } - append_fmt( result, "%S %S (", ast->ReturnType.to_string(), ast->Name ); + append_fmt( & result, "%S %S (", ast->ReturnType.to_string(), ast->Name ); if ( ast->Params ) - append_fmt( result, "%S)", ast->Params.to_string() ); + append_fmt( & result, "%S)", ast->Params.to_string() ); else - append_fmt( result, ")" ); + append_fmt( & result, ")" ); if ( ast->Specs ) { @@ -742,15 +742,15 @@ void CodeOperator::to_string_fwd( String& result ) if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } if ( ast->InlineCmt ) - append_fmt( result, "; %S", ast->InlineCmt->Content ); + append_fmt( & result, "; %S", ast->InlineCmt->Content ); else - append( result, ";\n" ); + append( & result, ";\n" ); } String CodeOpCast::to_string() @@ -778,32 +778,32 @@ void CodeOpCast::to_string_def( String& result ) if ( ! ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, "%*s ", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, "%*s ", spec_str.Len, spec_str.Ptr ); } } if ( ast->Name && length(ast->Name) ) - append_fmt( result, "%Soperator %S()", ast->Name, ast->ValueType.to_string() ); + append_fmt( & result, "%Soperator %S()", ast->Name, ast->ValueType.to_string() ); else - append_fmt( result, "operator %S()", ast->ValueType.to_string() ); + append_fmt( & result, "operator %S()", ast->ValueType.to_string() ); for ( SpecifierT spec : ast->Specs ) { if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); } } - append_fmt( result, "\n{\n%S\n}\n", ast->Body.to_string() ); + append_fmt( & result, "\n{\n%S\n}\n", ast->Body.to_string() ); return; } if ( ast->Name && length(ast->Name) ) - append_fmt( result, "%Soperator %S()\n{\n%S\n}\n", ast->Name, ast->ValueType.to_string(), ast->Body.to_string() ); + append_fmt( & result, "%Soperator %S()\n{\n%S\n}\n", ast->Name, ast->ValueType.to_string(), ast->Body.to_string() ); else - append_fmt( result, "operator %S()\n{\n%S\n}\n", ast->ValueType.to_string(), ast->Body.to_string() ); + append_fmt( & result, "operator %S()\n{\n%S\n}\n", ast->ValueType.to_string(), ast->Body.to_string() ); } void CodeOpCast::to_string_fwd( String& result ) @@ -815,32 +815,32 @@ void CodeOpCast::to_string_fwd( String& result ) if ( ! ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, "%*s ", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, "%*s ", spec_str.Len, spec_str.Ptr ); } } - append_fmt( result, "operator %S()", ast->ValueType.to_string() ); + append_fmt( & result, "operator %S()", ast->ValueType.to_string() ); for ( SpecifierT spec : ast->Specs ) { if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, " %*s", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, " %*s", spec_str.Len, spec_str.Ptr ); } } if ( ast->InlineCmt ) - append_fmt( result, "; %S", ast->InlineCmt->Content ); + append_fmt( & result, "; %S", ast->InlineCmt->Content ); else - append( result, ";\n" ); + append( & result, ";\n" ); return; } if ( ast->InlineCmt ) - append_fmt( result, "operator %S(); %S", ast->ValueType.to_string() ); + append_fmt( & result, "operator %S(); %S", ast->ValueType.to_string() ); else - append_fmt( result, "operator %S();\n", ast->ValueType.to_string() ); + append_fmt( & result, "operator %S();\n", ast->ValueType.to_string() ); } String CodeParam::to_string() @@ -855,34 +855,34 @@ void CodeParam::to_string( String& result ) if ( ast->Macro ) { // Related to parsing: ( , ... ) - GEN_NS append( result, ast->Macro.ast->Content ); + GEN_NS append( & result, ast->Macro.ast->Content ); // Could also be: ( , ... ) } if ( ast->Name ) { if ( ast->ValueType.ast == nullptr ) - append_fmt( result, " %S", ast->Name ); + append_fmt( & result, " %S", ast->Name ); else - append_fmt( result, " %S %S", ast->ValueType.to_string(), ast->Name ); + append_fmt( & result, " %S %S", ast->ValueType.to_string(), ast->Name ); } else if ( ast->ValueType ) - append_fmt( result, " %S", ast->ValueType.to_string() ); + append_fmt( & result, " %S", ast->ValueType.to_string() ); if ( ast->PostNameMacro ) { - append_fmt( result, " %S", ast->PostNameMacro.to_string() ); + append_fmt( & result, " %S", ast->PostNameMacro.to_string() ); } if ( ast->Value ) - append_fmt( result, " = %S", ast->Value.to_string() ); + append_fmt( & result, " = %S", ast->Value.to_string() ); if ( ast->NumEntries - 1 > 0 ) { for ( CodeParam param : ast->Next ) { - append_fmt( result, ", %S", param.to_string() ); + append_fmt( & result, ", %S", param.to_string() ); } } } @@ -917,32 +917,32 @@ String CodePreprocessCond::to_string() void CodePreprocessCond::to_string_if( String& result ) { - append_fmt( result, "#if %S\n", ast->Content ); + append_fmt( & result, "#if %S\n", ast->Content ); } void CodePreprocessCond::to_string_ifdef( String& result ) { - append_fmt( result, "#ifdef %S\n", ast->Content ); + append_fmt( & result, "#ifdef %S\n", ast->Content ); } void CodePreprocessCond::to_string_ifndef( String& result ) { - append_fmt( result, "#ifndef %S\n", ast->Content ); + append_fmt( & result, "#ifndef %S\n", ast->Content ); } void CodePreprocessCond::to_string_elif( String& result ) { - append_fmt( result, "#elif %S\n", ast->Content ); + append_fmt( & result, "#elif %S\n", ast->Content ); } void CodePreprocessCond::to_string_else( String& result ) { - append_fmt( result, "#else\n" ); + append_fmt( & result, "#else\n" ); } void CodePreprocessCond::to_string_endif( String& result ) { - append_fmt( result, "#endif\n" ); + append_fmt( & result, "#endif\n" ); } String CodePragma::to_string() @@ -954,7 +954,7 @@ String CodePragma::to_string() void CodePragma::to_string( String& result ) { - append_fmt( result, "#pragma %S\n", ast->Content ); + append_fmt( & result, "#pragma %S\n", ast->Content ); } String CodeSpecifiers::to_string() @@ -971,7 +971,7 @@ void CodeSpecifiers::to_string( String& result ) while ( left-- ) { StrC spec = ESpecifier::to_str( ast->ArrSpecs[idx] ); - append_fmt( result, "%.*s ", spec.Len, spec.Ptr ); + append_fmt( & result, "%.*s ", spec.Len, spec.Ptr ); idx++; } } @@ -995,63 +995,63 @@ String CodeStruct::to_string() void CodeStruct::to_string_def( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); - append( result, "struct " ); + append( & result, "struct " ); if ( ast->Attributes ) { - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); } if ( ast->ParentType ) { char const* access_level = to_str( ast->ParentAccess ); - append_fmt( result, "%S : %s %S", ast->Name, access_level, ast->ParentType.to_string() ); + append_fmt( & result, "%S : %s %S", ast->Name, access_level, ast->ParentType.to_string() ); CodeType interface = ast->ParentType->Next->code_cast< CodeType >(); if ( interface ) - append( result, "\n" ); + append( & result, "\n" ); while ( interface ) { - append_fmt( result, ", %S", interface.to_string() ); + append_fmt( & result, ", %S", interface.to_string() ); interface = interface->Next ? interface->Next->code_cast< CodeType >() : CodeType { nullptr }; } } else if ( ast->Name ) { - append( result, ast->Name ); + append( & result, ast->Name ); } if ( ast->InlineCmt ) { - append_fmt( result, " // %S", ast->InlineCmt->Content ); + append_fmt( & result, " // %S", ast->InlineCmt->Content ); } - append_fmt( result, "\n{\n%S\n}", ast->Body.to_string() ); + append_fmt( & result, "\n{\n%S\n}", ast->Body.to_string() ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) - append( result, ";\n"); + append( & result, ";\n"); } void CodeStruct::to_string_fwd( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); if ( ast->Attributes ) - append_fmt( result, "struct %S %S", ast->Attributes.to_string(), ast->Name ); + append_fmt( & result, "struct %S %S", ast->Attributes.to_string(), ast->Name ); - else append_fmt( result, "struct %S", ast->Name ); + else append_fmt( & result, "struct %S", ast->Name ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) { if ( ast->InlineCmt ) - append_fmt( result, "; %S", ast->InlineCmt->Content ); + append_fmt( & result, "; %S", ast->InlineCmt->Content ); else - append( result, ";\n"); + append( & result, ";\n"); } } @@ -1065,12 +1065,12 @@ String CodeTemplate::to_string() void CodeTemplate::to_string( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); if ( ast->Params ) - append_fmt( result, "template< %S >\n%S", ast->Params.to_string(), ast->Declaration.to_string() ); + append_fmt( & result, "template< %S >\n%S", ast->Params.to_string(), ast->Declaration.to_string() ); else - append_fmt( result, "template<>\n%S", ast->Declaration.to_string() ); + append_fmt( & result, "template<>\n%S", ast->Declaration.to_string() ); } String CodeTypedef::to_string() @@ -1083,36 +1083,36 @@ String CodeTypedef::to_string() void CodeTypedef::to_string( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); - append( result, "typedef "); + append( & result, "typedef "); // Determines if the typedef is a function typename if ( ast->UnderlyingType->ReturnType ) - append( result, ast->UnderlyingType.to_string() ); + append( & result, ast->UnderlyingType.to_string() ); else - append_fmt( result, "%S %S", ast->UnderlyingType.to_string(), ast->Name ); + append_fmt( & result, "%S %S", ast->UnderlyingType.to_string(), ast->Name ); if ( ast->UnderlyingType->Type == ECode::Typename && ast->UnderlyingType->ArrExpr ) { - append_fmt( result, "[ %S ];", ast->UnderlyingType->ArrExpr->to_string() ); + append_fmt( & result, "[ %S ];", ast->UnderlyingType->ArrExpr->to_string() ); AST* next_arr_expr = ast->UnderlyingType->ArrExpr->Next; while ( next_arr_expr ) { - append_fmt( result, "[ %S ];", next_arr_expr->to_string() ); + append_fmt( & result, "[ %S ];", next_arr_expr->to_string() ); next_arr_expr = next_arr_expr->Next; } } else { - append( result, ";" ); + append( & result, ";" ); } if ( ast->InlineCmt ) - append_fmt( result, " %S", ast->InlineCmt->Content); + append_fmt( & result, " %S", ast->InlineCmt->Content); else - append( result, "\n"); + append( & result, "\n"); } String CodeType::to_string() @@ -1132,9 +1132,9 @@ void CodeType::to_string( String& result ) else { if ( ast->Specs ) - append_fmt( result, "%S ( %S ) ( %S ) %S", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string(), ast->Specs.to_string() ); + append_fmt( & result, "%S ( %S ) ( %S ) %S", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string(), ast->Specs.to_string() ); else - append_fmt( result, "%S ( %S ) ( %S )", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string() ); + append_fmt( & result, "%S ( %S ) ( %S )", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string() ); } break; @@ -1143,13 +1143,13 @@ void CodeType::to_string( String& result ) if ( ast->ReturnType && ast->Params ) { if ( ast->Attributes ) - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); else { if ( ast->Specs ) - append_fmt( result, "%S %S ( %S ) %S", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string(), ast->Specs.to_string() ); + append_fmt( & result, "%S %S ( %S ) %S", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string(), ast->Specs.to_string() ); else - append_fmt( result, "%S %S ( %S )", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string() ); + append_fmt( & result, "%S %S ( %S )", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string() ); } return; @@ -1157,15 +1157,15 @@ void CodeType::to_string( String& result ) #endif if ( ast->Attributes ) - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); if ( ast->Specs ) - append_fmt( result, "%S %S", ast->Name, ast->Specs.to_string() ); + append_fmt( & result, "%S %S", ast->Name, ast->Specs.to_string() ); else - append_fmt( result, "%S", ast->Name ); + append_fmt( & result, "%S", ast->Name ); if ( ast->IsParamPack ) - append( result, "..."); + append( & result, "..."); } String CodeUnion::to_string() @@ -1178,16 +1178,16 @@ String CodeUnion::to_string() void CodeUnion::to_string( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); - append( result, "union " ); + append( & result, "union " ); if ( ast->Attributes ) - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); if ( ast->Name ) { - append_fmt( result, "%S\n{\n%S\n}" + append_fmt( & result, "%S\n{\n%S\n}" , ast->Name , ast->Body.to_string() ); @@ -1195,13 +1195,13 @@ void CodeUnion::to_string( String& result ) else { // Anonymous union - append_fmt( result, "\n{\n%S\n}" + append_fmt( & result, "\n{\n%S\n}" , ast->Body.to_string() ); } if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) - append( result, ";\n"); + append( & result, ";\n"); } String CodeUsing::to_string() @@ -1223,44 +1223,44 @@ String CodeUsing::to_string() void CodeUsing::to_string( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); if ( ast->Attributes ) - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); if ( ast->UnderlyingType ) { - append_fmt( result, "using %S = %S", ast->Name, ast->UnderlyingType.to_string() ); + append_fmt( & result, "using %S = %S", ast->Name, ast->UnderlyingType.to_string() ); if ( ast->UnderlyingType->ArrExpr ) { - append_fmt( result, "[ %S ]", ast->UnderlyingType->ArrExpr.to_string() ); + append_fmt( & result, "[ %S ]", ast->UnderlyingType->ArrExpr.to_string() ); AST* next_arr_expr = ast->UnderlyingType->ArrExpr->Next; while ( next_arr_expr ) { - append_fmt( result, "[ %S ]", next_arr_expr->to_string() ); + append_fmt( & result, "[ %S ]", next_arr_expr->to_string() ); next_arr_expr = next_arr_expr->Next; } } - append( result, ";" ); + append( & result, ";" ); } else - append_fmt( result, "using %S;", ast->Name ); + append_fmt( & result, "using %S;", ast->Name ); if ( ast->InlineCmt ) - append_fmt( result, " %S\n", ast->InlineCmt->Content ); + append_fmt( & result, " %S\n", ast->InlineCmt->Content ); else - append( result, "\n"); + append( & result, "\n"); } void CodeUsing::to_string_ns( String& result ) { if ( ast->InlineCmt ) - append_fmt( result, "using namespace $S; %S", ast->Name, ast->InlineCmt->Content ); + append_fmt( & result, "using namespace $S; %S", ast->Name, ast->InlineCmt->Content ); else - append_fmt( result, "using namespace %s;\n", ast->Name ); + append_fmt( & result, "using namespace %s;\n", ast->Name ); } String CodeVar::to_string() @@ -1277,18 +1277,18 @@ void CodeVar::to_string( String& result ) // Its a comma-separated variable ( a NextVar ) if ( ast->Specs ) - append_fmt( result, "%S ", ast->Specs.to_string() ); + append_fmt( & result, "%S ", ast->Specs.to_string() ); - append( result, ast->Name ); + append( & result, ast->Name ); if ( ast->ValueType->ArrExpr ) { - append_fmt( result, "[ %S ]", ast->ValueType->ArrExpr.to_string() ); + append_fmt( & result, "[ %S ]", ast->ValueType->ArrExpr.to_string() ); AST* next_arr_expr = ast->ValueType->ArrExpr->Next; while ( next_arr_expr ) { - append_fmt( result, "[ %S ]", next_arr_expr->to_string() ); + append_fmt( & result, "[ %S ]", next_arr_expr->to_string() ); next_arr_expr = next_arr_expr->Next; } } @@ -1296,107 +1296,107 @@ void CodeVar::to_string( String& result ) if ( ast->Value ) { if ( ast->VarConstructorInit ) - append_fmt( result, "( %S ", ast->Value.to_string() ); + append_fmt( & result, "( %S ", ast->Value.to_string() ); else - append_fmt( result, " = %S", ast->Value.to_string() ); + append_fmt( & result, " = %S", ast->Value.to_string() ); } // Keep the chain going... if ( ast->NextVar ) - append_fmt( result, ", %S", ast->NextVar.to_string() ); + append_fmt( & result, ", %S", ast->NextVar.to_string() ); if ( ast->VarConstructorInit ) - append( result, " )"); + append( & result, " )"); return; } if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); if ( ast->Attributes || ast->Specs ) { if ( ast->Attributes ) - append_fmt( result, "%S ", ast->Specs.to_string() ); + append_fmt( & result, "%S ", ast->Specs.to_string() ); if ( ast->Specs ) - append_fmt( result, "%S\n", ast->Specs.to_string() ); + append_fmt( & result, "%S\n", ast->Specs.to_string() ); - append_fmt( result, "%S %S", ast->ValueType.to_string(), ast->Name ); + append_fmt( & result, "%S %S", ast->ValueType.to_string(), ast->Name ); if ( ast->ValueType->ArrExpr ) { - append_fmt( result, "[ %S ]", ast->ValueType->ArrExpr.to_string() ); + append_fmt( & result, "[ %S ]", ast->ValueType->ArrExpr.to_string() ); AST* next_arr_expr = ast->ValueType->ArrExpr->Next; while ( next_arr_expr ) { - append_fmt( result, "[ %S ]", next_arr_expr->to_string() ); + append_fmt( & result, "[ %S ]", next_arr_expr->to_string() ); next_arr_expr = next_arr_expr->Next; } } if ( ast->BitfieldSize ) - append_fmt( result, " : %S", ast->BitfieldSize.to_string() ); + append_fmt( & result, " : %S", ast->BitfieldSize.to_string() ); if ( ast->Value ) { if ( ast->VarConstructorInit ) - append_fmt( result, "( %S ", ast->Value.to_string() ); + append_fmt( & result, "( %S ", ast->Value.to_string() ); else - append_fmt( result, " = %S", ast->Value.to_string() ); + append_fmt( & result, " = %S", ast->Value.to_string() ); } if ( ast->NextVar ) - append_fmt( result, ", %S", ast->NextVar.to_string() ); + append_fmt( & result, ", %S", ast->NextVar.to_string() ); if ( ast->VarConstructorInit ) - append( result, " )"); + append( & result, " )"); if ( ast->InlineCmt ) - append_fmt( result, "; %S", ast->InlineCmt->Content); + append_fmt( & result, "; %S", ast->InlineCmt->Content); else - append( result, ";\n" ); + append( & result, ";\n" ); return; } if ( ast->BitfieldSize ) - append_fmt( result, "%S %S : %S", ast->ValueType.to_string(), ast->Name, ast->BitfieldSize.to_string() ); + append_fmt( & result, "%S %S : %S", ast->ValueType.to_string(), ast->Name, ast->BitfieldSize.to_string() ); else if ( ast->ValueType->ArrExpr ) { - append_fmt( result, "%S %S[ %S ]", ast->ValueType.to_string(), ast->Name, ast->ValueType->ArrExpr.to_string() ); + append_fmt( & result, "%S %S[ %S ]", ast->ValueType.to_string(), ast->Name, ast->ValueType->ArrExpr.to_string() ); AST* next_arr_expr = ast->ValueType->ArrExpr->Next; while ( next_arr_expr ) { - append_fmt( result, "[ %S ]", next_arr_expr->to_string() ); + append_fmt( & result, "[ %S ]", next_arr_expr->to_string() ); next_arr_expr = next_arr_expr->Next; } } else - append_fmt( result, "%S %S", ast->ValueType.to_string(), ast->Name ); + append_fmt( & result, "%S %S", ast->ValueType.to_string(), ast->Name ); if ( ast->Value ) { if ( ast->VarConstructorInit ) - append_fmt( result, "( %S ", ast->Value.to_string() ); + append_fmt( & result, "( %S ", ast->Value.to_string() ); else - append_fmt( result, " = %S", ast->Value.to_string() ); + append_fmt( & result, " = %S", ast->Value.to_string() ); } if ( ast->NextVar ) - append_fmt( result, ", %S", ast->NextVar.to_string() ); + append_fmt( & result, ", %S", ast->NextVar.to_string() ); if ( ast->VarConstructorInit ) - append( result, " )"); + append( & result, " )"); - append( result, ";" ); + append( & result, ";" ); if ( ast->InlineCmt ) - append_fmt( result, " %S", ast->InlineCmt->Content); + append_fmt( & result, " %S", ast->InlineCmt->Content); else - append( result, "\n"); + append( & result, "\n"); } diff --git a/project/components/interface.cpp b/project/components/interface.cpp index 9b3cbfb..b455259 100644 --- a/project/components/interface.cpp +++ b/project/components/interface.cpp @@ -321,7 +321,7 @@ void deinit() } while ( left--, left ); - destroy(StringCache); + destroy(& StringCache); free( & CodePools); free( & StringArenas); @@ -403,7 +403,7 @@ StringCached get_cached_string( StrC str ) } String result = string_make( get_string_allocator( str.Len ), str ); - set(StringCache, key, result ); + set(& StringCache, key, result ); return result; } diff --git a/project/components/interface.untyped.cpp b/project/components/interface.untyped.cpp index bafc313..4f49d5f 100644 --- a/project/components/interface.untyped.cpp +++ b/project/components/interface.untyped.cpp @@ -27,8 +27,7 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va ) StrC value = va_arg( va, StrC ); u32 key = crc32( token, str_len(token) ); - - set(tok_map, key, value ); + set(& tok_map, key, value ); } } diff --git a/project/components/interface.upfront.cpp b/project/components/interface.upfront.cpp index c0e9c9b..fab42a1 100644 --- a/project/components/interface.upfront.cpp +++ b/project/components/interface.upfront.cpp @@ -474,15 +474,15 @@ CodeComment def_comment( StrC content ) length++; str_copy( line, scanner, length ); - append_fmt(cmt_formatted, "//%.*s", length, line ); + append_fmt(& cmt_formatted, "//%.*s", length, line ); mem_set( line, 0, MaxCommentLineLength ); scanner += length; } while ( scanner <= end ); - if ( back(cmt_formatted) != '\n' ) - append( cmt_formatted, "\n" ); + if ( * back(& cmt_formatted) != '\n' ) + append( & cmt_formatted, "\n" ); Code result = make_code(); @@ -490,7 +490,7 @@ CodeComment def_comment( StrC content ) result->Name = get_cached_string( cmt_formatted ); result->Content = result->Name; - free(cmt_formatted); + free(& cmt_formatted); return (CodeComment) result; } diff --git a/project/components/lexer.cpp b/project/components/lexer.cpp index 8e4164f..33f957e 100644 --- a/project/components/lexer.cpp +++ b/project/components/lexer.cpp @@ -93,7 +93,7 @@ struct Token StrC type_str = ETokType::to_str( Type ); - append_fmt( result, "Line: %d Column: %d, Type: %.*s Content: %.*s" + append_fmt( & result, "Line: %d Column: %d, Type: %.*s Content: %.*s" , Line, Column , type_str.Len, type_str.Ptr , Length, Text @@ -341,7 +341,7 @@ s32 lex_preprocessor_directive( append( & Tokens, name ); u64 key = crc32( name.Text, name.Length ); - set(defines, key, name ); + set(& defines, key, name ); } Token preprocess_content = { scanner, 0, TokType::Preprocess_Content, line, column, TF_Preprocess }; @@ -597,7 +597,7 @@ TokArray lex( StrC content ) } u64 key = crc32( entry.Data, length ); - set(defines, key, entry ); + set(& defines, key, entry ); } clear(Tokens); diff --git a/project/components/parser.cpp b/project/components/parser.cpp index f184d70..7055811 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -59,17 +59,17 @@ struct ParseContext } String line = string_make( GlobalAllocator, { length, scope_start.Text } ); - append_fmt( result, "\tScope : %s\n", line ); - free(line); + append_fmt( & result, "\tScope : %s\n", line ); + free(& line); sptr dist = (sptr)last_valid.Text - (sptr)scope_start.Text + 2; sptr length_from_err = dist; String line_from_err = string_make( GlobalAllocator, { length_from_err, last_valid.Text } ); if ( length_from_err < 100 ) - append_fmt(result, "\t(%d, %d):%*c\n", last_valid.Line, last_valid.Column, length_from_err, '^' ); + append_fmt(& result, "\t(%d, %d):%*c\n", last_valid.Line, last_valid.Column, length_from_err, '^' ); else - append_fmt(result, "\t(%d, %d)\n", last_valid.Line, last_valid.Column ); + append_fmt(& result, "\t(%d, %d)\n", last_valid.Line, last_valid.Column ); StackNode* curr_scope = Scope; s32 level = 0; @@ -77,11 +77,11 @@ struct ParseContext { if ( curr_scope->Name ) { - append_fmt(result, "\t%d: %s, AST Name: %.*s\n", level, curr_scope->ProcName.Ptr, curr_scope->Name.Length, curr_scope->Name.Text ); + append_fmt(& result, "\t%d: %s, AST Name: %.*s\n", level, curr_scope->ProcName.Ptr, curr_scope->Name.Length, curr_scope->Name.Text ); } else { - append_fmt(result, "\t%d: %s\n", level, curr_scope->ProcName.Ptr ); + append_fmt(& result, "\t%d: %s\n", level, curr_scope->ProcName.Ptr ); } curr_scope = curr_scope->Prev; @@ -290,7 +290,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) if ( tokleft ) move_fwd(); - append( content, cut_ptr, cut_length ); + append( & content, cut_ptr, cut_length ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); continue; } @@ -312,7 +312,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) if ( tokleft ) move_fwd(); - append( content, cut_ptr, cut_length ); + append( & content, cut_ptr, cut_length ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); continue; } @@ -326,7 +326,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) scanner += 2; tokleft -= 2; - append( content, cut_ptr, cut_length ); + append( & content, cut_ptr, cut_length ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); continue; } @@ -345,7 +345,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) if (tokleft) move_fwd(); - append( content, cut_ptr, cut_length ); + append( & content, cut_ptr, cut_length ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); continue; } @@ -354,10 +354,10 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) if (scanner[0] == '\t') { if (pos > last_cut) - append( content, cut_ptr, cut_length); + append( & content, cut_ptr, cut_length); - if ( back( content ) != ' ' ) - append( content, ' '); + if ( * back( & content ) != ' ' ) + append( & content, ' '); move_fwd(); last_cut = sptr(scanner) - sptr(raw_text.Ptr); @@ -373,17 +373,17 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) scanner += 2; tokleft -= 2; - append( content, cut_ptr, cut_length ); + append( & content, cut_ptr, cut_length ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); continue; } if ( pos > last_cut ) - append( content, cut_ptr, cut_length ); + append( & content, cut_ptr, cut_length ); // Replace with a space - if ( back( content ) != ' ' ) - append( content, ' ' ); + if ( * back( & content ) != ' ' ) + append( & content, ' ' ); scanner += 2; tokleft -= 2; @@ -400,17 +400,17 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) move_fwd(); - append( content, cut_ptr, cut_length ); + append( & content, cut_ptr, cut_length ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); continue; } if ( pos > last_cut ) - append( content, cut_ptr, cut_length ); + append( & content, cut_ptr, cut_length ); // Replace with a space - if ( back( content ) != ' ' ) - append( content, ' ' ); + if ( * back( & content ) != ' ' ) + append( & content, ' ' ); move_fwd(); @@ -421,7 +421,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) // Escaped newlines if ( scanner[0] == '\\' ) { - append( content, cut_ptr, cut_length ); + append( & content, cut_ptr, cut_length ); s32 amount_to_skip = 1; if ( tokleft > 1 && scanner[1] == '\n' ) @@ -448,7 +448,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) // Consectuive spaces if ( tokleft > 1 && char_is_space( scanner[0] ) && char_is_space( scanner[ 1 ] ) ) { - append( content, cut_ptr, cut_length ); + append( & content, cut_ptr, cut_length ); do { move_fwd(); @@ -458,8 +458,9 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); // Preserve only 1 space of formattting - if ( back( content ) != ' ' ) - append( content, ' ' ); + char* last = back(& content); + if ( last == nullptr || * last != ' ' ) + append( & content, ' ' ); continue; } @@ -469,7 +470,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) if ( last_cut < raw_text.Len ) { - append( content, cut_ptr, raw_text.Len - last_cut ); + append( & content, cut_ptr, raw_text.Len - last_cut ); } #undef cut_ptr @@ -1040,7 +1041,7 @@ CodeBody parse_class_struct_body( TokType which, Token name ) if ( attributes ) { String fused = string_make_reserve( GlobalAllocator, length(attributes->Content) + length(more_attributes->Content) ); - append_fmt( fused, "%S %S", attributes->Content, more_attributes->Content ); + append_fmt( & fused, "%S %S", attributes->Content, more_attributes->Content ); attributes->Name = get_cached_string(fused); attributes->Content = attributes->Name; diff --git a/project/dependencies/containers.hpp b/project/dependencies/containers.hpp index f59fb2f..a6d6248 100644 --- a/project/dependencies/containers.hpp +++ b/project/dependencies/containers.hpp @@ -49,10 +49,6 @@ template bool resize (Array(Type)* array, usize n template bool set_capacity (Array(Type)* array, usize new_capacity); template ArrayHeader* get_header (Array(Type) array); -// template forceinline Type* begin(Array array) { return array; } -// template forceinline Type* end(Array array) { return array + get_header(array)->Num; } -// template forceinline Type* next(Array array, Type* entry) { return entry + 1; } - struct ArrayHeader { AllocatorInfo Allocator; usize Capacity; @@ -114,6 +110,10 @@ template bool set_capacity(Array& array, usize new_cap template forceinline Type* begin(Array& array) { return array; } template forceinline Type* end(Array& array) { return array + get_header(array)->Num; } template forceinline Type* next(Array& array, Type* entry) { return entry + 1; } +#else +template forceinline Type* begin(Array array) { return array; } +template forceinline Type* end(Array array) { return array + get_header(array)->Num; } +template forceinline Type* next(Array array, Type* entry) { return entry + 1; } #endif template inline @@ -402,21 +402,21 @@ struct HashTableEntry { template HashTable hashtable_init(AllocatorInfo allocator); template HashTable hashtable_init_reserve(AllocatorInfo allocator, usize num); -template void clear(HashTable& table); -template void destroy(HashTable& table); -template Type* get(HashTable& table, u64 key); -template void grow(HashTable& table); -template void rehash(HashTable& table, ssize new_num); -template void rehash_fast(HashTable& table); -template void remove(HashTable& table, u64 key); -template void remove_entry(HashTable& table, ssize idx); -template void set(HashTable& table, u64 key, Type value); -template ssize slot(HashTable& table, u64 key); -template ssize add_entry(HashTable& table, u64 key); -template HashTableFindResult find(HashTable& table, u64 key); -template bool full(HashTable& table); -template void map(HashTable& table, void (*map_proc)(u64 key, Type value)); -template void map_mut(HashTable& table, void (*map_proc)(u64 key, Type* value)); +template void clear (HashTable table); +template void destroy (HashTable* table); +template Type* get (HashTable table, u64 key); +template void grow (HashTable* table); +template void rehash (HashTable* table, ssize new_num); +template void rehash_fast (HashTable table); +template void remove (HashTable table, u64 key); +template void remove_entry (HashTable table, ssize idx); +template void set (HashTable* table, u64 key, Type value); +template ssize slot (HashTable table, u64 key); +template ssize add_entry (HashTable* table, u64 key); +template HashTableFindResult find (HashTable table, u64 key); +template bool full (HashTable table); +template void map (HashTable table, void (*map_proc)(u64 key, Type value)); +template void map_mut (HashTable table, void (*map_proc)(u64 key, Type* value)); static constexpr f32 HashTable_CriticalLoadScale = 0.7f; @@ -447,6 +447,14 @@ struct HashTable #endif }; +#if GEN_SUPPORT_CPP_REFERENCES +template void destroy (HashTable& table) { destroy(& table); } +template void grow (HashTable& table) { grow(& table); } +template void rehash (HashTable& table, ssize new_num) { rehash(& table, new_num); } +template void set (HashTable& table, u64 key, Type value) { set(& table, key, value); } +template ssize add_entry(HashTable& table, u64 key) { add_entry(& table, key); } +#endif + template inline HashTable hashtable_init(AllocatorInfo allocator) { HashTable result = hashtable_init_reserve(allocator, 8); @@ -468,65 +476,65 @@ HashTable hashtable_init_reserve(AllocatorInfo allocator, usize num) } template inline -void clear(HashTable& table) { +void clear(HashTable table) { clear(table.Entries); fill(table.Hashes, 0, num(table.Hashes), -1); } template inline -void destroy(HashTable& table) { - if (table.Hashes && get_header(table.Hashes)->Capacity) { - free(& table.Hashes); - free(& table.Entries); +void destroy(HashTable* table) { + if (table->Hashes && get_header(table->Hashes)->Capacity) { + free(& table->Hashes); + free(& table->Entries); } } template inline -Type* get(HashTable& table, u64 key) { +Type* get(HashTable table, u64 key) { ssize idx = find(table, key).EntryIndex; if (idx >= 0) - return &table.Entries[idx].Value; + return & table.Entries[idx].Value; return nullptr; } template inline -void map(HashTable& table, void (*map_proc)(u64 key, Type value)) { +void map(HashTable table, void (*map_proc)(u64 key, Type value)) { GEN_ASSERT_NOT_NULL(map_proc); - for (ssize idx = 0; idx < ssize(table.Entries.num()); ++idx) { + for (ssize idx = 0; idx < ssize(num(table.Entries)); ++idx) { map_proc(table.Entries[idx].Key, table.Entries[idx].Value); } } template inline -void map_mut(HashTable& table, void (*map_proc)(u64 key, Type* value)) { +void map_mut(HashTable table, void (*map_proc)(u64 key, Type* value)) { GEN_ASSERT_NOT_NULL(map_proc); - for (ssize idx = 0; idx < ssize(table.Entries.num()); ++idx) { - map_proc(table.Entries[idx].Key, &table.Entries[idx].Value); + for (ssize idx = 0; idx < ssize(num(table.Entries)); ++idx) { + map_proc(table.Entries[idx].Key, & table.Entries[idx].Value); } } template inline -void grow(HashTable& table) { - ssize new_num = array_grow_formula(num(table.Entries)); +void grow(HashTable* table) { + ssize new_num = array_grow_formula(num(table->Entries)); rehash(table, new_num); } template inline -void rehash(HashTable& table, ssize new_num) +void rehash(HashTable* table, ssize new_num) { ssize last_added_index; - HashTable new_ht = hashtable_init_reserve(get_header(table.Hashes)->Allocator, new_num); + HashTable new_ht = hashtable_init_reserve(get_header(table->Hashes)->Allocator, new_num); - for (ssize idx = 0; idx < ssize(num(table.Entries)); ++idx) + for (ssize idx = 0; idx < ssize(num(table->Entries)); ++idx) { HashTableFindResult find_result; - HashTableEntry& entry = table.Entries[idx]; + HashTableEntry& entry = table->Entries[idx]; find_result = find(new_ht, entry.Key); - last_added_index = add_entry(new_ht, entry.Key); + last_added_index = add_entry(& new_ht, entry.Key); if (find_result.PrevIndex < 0) new_ht.Hashes[find_result.HashIndex] = last_added_index; @@ -538,21 +546,21 @@ void rehash(HashTable& table, ssize new_num) } destroy(table); - table = new_ht; + * table = new_ht; } template inline -void rehash_fast(HashTable& table) +void rehash_fast(HashTable table) { ssize idx; - for (idx = 0; idx < ssize(table.Entries.num()); idx++) + for (idx = 0; idx < ssize(num(table.Entries)); idx++) table.Entries[idx].Next = -1; - for (idx = 0; idx < ssize(table.Hashes.num()); idx++) + for (idx = 0; idx < ssize(num(table.Hashes)); idx++) table.Hashes[idx] = -1; - for (idx = 0; idx < ssize(table.Entries.num()); idx++) + for (idx = 0; idx < ssize(num(table.Entries)); idx++) { HashTableEntry* entry; HashTableFindResult find_result; @@ -568,30 +576,30 @@ void rehash_fast(HashTable& table) } template inline -void remove(HashTable& table, u64 key) { +void remove(HashTable table, u64 key) { HashTableFindResult find_result = find(table, key); if (find_result.EntryIndex >= 0) { - table.Entries.remove_at(find_result.EntryIndex); + remove_at(table.Entries, find_result.EntryIndex); rehash_fast(table); } } template inline -void remove_entry(HashTable& table, ssize idx) { - table.Entries.remove_at(idx); +void remove_entry(HashTable table, ssize idx) { + remove_at(table.Entries, idx); } template inline -void set(HashTable& table, u64 key, Type value) +void set(HashTable* table, u64 key, Type value) { ssize idx; HashTableFindResult find_result; - if (full(table)) + if (full(* table)) grow(table); - find_result = find(table, key); + find_result = find(* table, key); if (find_result.EntryIndex >= 0) { idx = find_result.EntryIndex; } @@ -600,22 +608,22 @@ void set(HashTable& table, u64 key, Type value) idx = add_entry(table, key); if (find_result.PrevIndex >= 0) { - table.Entries[find_result.PrevIndex].Next = idx; + table->Entries[find_result.PrevIndex].Next = idx; } else { - table.Hashes[find_result.HashIndex] = idx; + table->Hashes[find_result.HashIndex] = idx; } } - table.Entries[idx].Value = value; + table->Entries[idx].Value = value; - if (full(table)) + if (full(* table)) grow(table); } template inline -ssize slot(HashTable& table, u64 key) { - for (ssize idx = 0; idx < ssize(table.Hashes.num()); ++idx) +ssize slot(HashTable table, u64 key) { + for (ssize idx = 0; idx < ssize(num(table.Hashes)); ++idx) if (table.Hashes[idx] == key) return idx; @@ -623,17 +631,17 @@ ssize slot(HashTable& table, u64 key) { } template inline -ssize add_entry(HashTable& table, u64 key) { +ssize add_entry(HashTable* table, u64 key) { ssize idx; HashTableEntry entry = { key, -1 }; - idx = num(table.Entries); - append( & table.Entries, entry); + idx = num(table->Entries); + append( & table->Entries, entry); return idx; } template inline -HashTableFindResult find(HashTable& table, u64 key) +HashTableFindResult find(HashTable table, u64 key) { HashTableFindResult result = { -1, -1, -1 }; @@ -656,7 +664,7 @@ HashTableFindResult find(HashTable& table, u64 key) } template inline -bool full(HashTable& table) { +bool full(HashTable table) { usize critical_load = usize(HashTable_CriticalLoadScale * f32(num(table.Hashes))); b32 result = num(table.Entries) > critical_load; return result; diff --git a/project/dependencies/strings.hpp b/project/dependencies/strings.hpp index 3058f87..3c539ca 100644 --- a/project/dependencies/strings.hpp +++ b/project/dependencies/strings.hpp @@ -33,40 +33,41 @@ StrC to_str( char const* str ) { struct StringHeader; struct String; -String string_make(AllocatorInfo allocator, char const* str); -String string_make(AllocatorInfo allocator, StrC str); -String string_make_reserve(AllocatorInfo allocator, ssize capacity); -String string_make_length(AllocatorInfo allocator, char const* str, ssize length); -String string_fmt(AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...); -String string_fmt_buf(AllocatorInfo allocator, char const* fmt, ...); -String string_join(AllocatorInfo allocator, char const** parts, ssize num_parts, char const* glue); -usize string_grow_formula(usize value); -bool are_equal(String const& lhs, String const& rhs); -bool are_equal(String const& lhs, StrC rhs); -bool make_space_for(String& str, char const* to_append, ssize add_len); -bool append(String& str, char c); -bool append(String& str, char const* str_to_append); -bool append(String& str, char const* str_to_append, ssize length); -bool append(String& str, StrC str_to_append); -bool append(String& str, const String other); -bool append_fmt(String& str, char const* fmt, ...); -ssize avail_space(String const& str); -char& back(String& str); -bool contains(String const& str, StrC substring); -bool contains(String const& str, String const& substring); -ssize capacity(String const& str); -void clear(String& str); -String duplicate(String const& str, AllocatorInfo allocator); -void free(String& str); -StringHeader& get_header(String& str); -ssize length(String const& str); -b32 starts_with(String const& str, StrC substring); -b32 starts_with(String const& str, String substring); -void skip_line(String& str); -void strip_space(String& str); -void trim(String& str, char const* cut_set); -void trim_space(String& str); -String visualize_whitespace(String const& str); +usize string_grow_formula(usize value); + +String string_make (AllocatorInfo allocator, char const* str); +String string_make (AllocatorInfo allocator, StrC str); +String string_make_reserve (AllocatorInfo allocator, ssize capacity); +String string_make_length (AllocatorInfo allocator, char const* str, ssize length); +String string_fmt (AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...); +String string_fmt_buf (AllocatorInfo allocator, char const* fmt, ...); +String string_join (AllocatorInfo allocator, char const** parts, ssize num_parts, char const* glue); +bool are_equal (String const lhs, String const rhs); +bool are_equal (String const lhs, StrC rhs); +bool make_space_for (String* str, char const* to_append, ssize add_len); +bool append (String* str, char c); +bool append (String* str, char const* str_to_append); +bool append (String* str, char const* str_to_append, ssize length); +bool append (String* str, StrC str_to_append); +bool append (String* str, String const other); +bool append_fmt (String* str, char const* fmt, ...); +ssize avail_space (String const str); +char* back (String str); +bool contains (String const str, StrC substring); +bool contains (String const str, String const substring); +ssize capacity (String const str); +void clear (String str); +String duplicate (String const str, AllocatorInfo allocator); +void free (String* str); +StringHeader* get_header (String str); +ssize length (String const str); +b32 starts_with (String const str, StrC substring); +b32 starts_with (String const str, String substring); +void skip_line (String str); +void strip_space (String str); +void trim (String str, char const* cut_set); +void trim_space (String str); +String visualize_whitespace(String const str); struct StringHeader { AllocatorInfo Allocator; @@ -129,31 +130,31 @@ struct String return GEN_NS string_make(allocator, buf); } - forceinline bool make_space_for(char const* str, ssize add_len) { return GEN_NS make_space_for(*this, str, add_len); } - forceinline bool append(char c) { return GEN_NS append(*this, c); } - forceinline bool append(char const* str) { return GEN_NS append(*this, str); } - forceinline bool append(char const* str, ssize length) { return GEN_NS append(*this, str, length); } - forceinline bool append(StrC str) { return GEN_NS append(*this, str); } - forceinline bool append(const String other) { return GEN_NS append(*this, other); } - forceinline ssize avail_space() const { return GEN_NS avail_space(*this); } - forceinline char& back() { return GEN_NS back(*this); } - forceinline bool contains(StrC substring) const { return GEN_NS contains(*this, substring); } - forceinline bool contains(String const& substring) const { return GEN_NS contains(*this, substring); } - forceinline ssize capacity() const { return GEN_NS capacity(*this); } - forceinline void clear() { GEN_NS clear(*this); } - forceinline String duplicate(AllocatorInfo allocator) const { return GEN_NS duplicate(*this, allocator); } - forceinline void free() { GEN_NS free(*this); } + forceinline bool make_space_for(char const* str, ssize add_len) { return GEN_NS make_space_for(this, str, add_len); } + forceinline bool append(char c) { return GEN_NS append(this, c); } + forceinline bool append(char const* str) { return GEN_NS append(this, str); } + forceinline bool append(char const* str, ssize length) { return GEN_NS append(this, str, length); } + forceinline bool append(StrC str) { return GEN_NS append(this, str); } + forceinline bool append(const String other) { return GEN_NS append(this, other); } + forceinline ssize avail_space() const { return GEN_NS avail_space(* this); } + forceinline char* back() { return GEN_NS back(* this); } + forceinline bool contains(StrC substring) const { return GEN_NS contains(* this, substring); } + forceinline bool contains(String const& substring) const { return GEN_NS contains(* this, substring); } + forceinline ssize capacity() const { return GEN_NS capacity(* this); } + forceinline void clear() { GEN_NS clear(* this); } + forceinline String duplicate(AllocatorInfo allocator) const { return GEN_NS duplicate(* this, allocator); } + forceinline void free() { GEN_NS free(this); } forceinline bool is_equal(String const& other) const { return GEN_NS are_equal(* this, other); } forceinline bool is_equal(StrC other) const { return GEN_NS are_equal(* this, other); } - forceinline ssize length() const { return GEN_NS length(*this); } - forceinline b32 starts_with(StrC substring) const { return GEN_NS starts_with(*this, substring); } - forceinline b32 starts_with(String substring) const { return GEN_NS starts_with(*this, substring); } - forceinline void skip_line() { GEN_NS skip_line(*this); } - forceinline void strip_space() { GEN_NS strip_space(*this); } - forceinline void trim(char const* cut_set) { GEN_NS trim(*this, cut_set); } - forceinline void trim_space() { GEN_NS trim_space(*this); } - forceinline String visualize_whitespace() const { return GEN_NS visualize_whitespace(*this); } - forceinline StringHeader& get_header() { return GEN_NS get_header(*this); } + forceinline ssize length() const { return GEN_NS length(* this); } + forceinline b32 starts_with(StrC substring) const { return GEN_NS starts_with(* this, substring); } + forceinline b32 starts_with(String substring) const { return GEN_NS starts_with(* this, substring); } + forceinline void skip_line() { GEN_NS skip_line(* this); } + forceinline void strip_space() { GEN_NS strip_space(* this); } + forceinline void trim(char const* cut_set) { GEN_NS trim(* this, cut_set); } + forceinline void trim_space() { GEN_NS trim_space(* this); } + forceinline String visualize_whitespace() const { return GEN_NS visualize_whitespace(* this); } + forceinline StringHeader& get_header() { return * GEN_NS get_header(* this); } bool append_fmt(char const* fmt, ...) { ssize res; @@ -164,13 +165,26 @@ struct String res = str_fmt_va(buf, count_of(buf) - 1, fmt, va) - 1; va_end(va); - return GEN_NS append(*this, buf, res); + return GEN_NS append(this, buf, res); } #pragma endregion Member Mapping #endif }; #endif +#if GEN_SUPPORT_CPP_REFERENCES +bool make_space_for(String& str, char const* to_append, ssize add_len); +bool append(String& str, char c); +bool append(String& str, char const* str_to_append); +bool append(String& str, char const* str_to_append, ssize length); +bool append(String& str, StrC str_to_append); +bool append(String& str, const String other); +bool append_fmt(String& str, char const* fmt, ...); +char& back(String& str); +void clear(String& str); +void free(String& str); +#endif + inline char* begin(String& str) { return str; } inline char* end(String& str) { return scast(char*, str) + length(str); } inline char* next(String& str) { return scast(char*, str) + 1; } @@ -223,57 +237,64 @@ String string_join(AllocatorInfo allocator, char const** parts, ssize num_parts, for (ssize idx = 0; idx < num_parts; ++idx) { - append(result, parts[idx]); + append(& result, parts[idx]); if (idx < num_parts - 1) - append(result, glue); + append(& result, glue); } return result; } inline -bool append(String& str, char c) { +bool append(String* str, char c) { + GEN_ASSERT(str != nullptr); return append(str, &c, 1); } inline -bool append(String& str, char const* str_to_append) { +bool append(String* str, char const* str_to_append) { + GEN_ASSERT(str != nullptr); return append(str, str_to_append, str_len(str_to_append)); } inline -bool append(String& str, char const* str_to_append, ssize append_length) +bool append(String* str, char const* str_to_append, ssize append_length) { + GEN_ASSERT(str != nullptr); if (sptr(str_to_append) > 0) { - ssize curr_len = length(str); + ssize curr_len = length(* str); - if (!make_space_for(str, str_to_append, append_length)) + if ( ! make_space_for(str, str_to_append, append_length)) return false; - StringHeader& header = get_header(str); + StringHeader* header = get_header(* str); - mem_copy( scast(char*, str) + curr_len, str_to_append, append_length); + char* Data = * str; + mem_copy( Data + curr_len, str_to_append, append_length); - str[curr_len + append_length] = '\0'; + Data[curr_len + append_length] = '\0'; - header.Length = curr_len + append_length; + header->Length = curr_len + append_length; } return str_to_append != nullptr; } inline -bool append(String& str, StrC str_to_append) { +bool append(String* str, StrC str_to_append) { + GEN_ASSERT(str != nullptr); return append(str, str_to_append.Ptr, str_to_append.Len); } inline -bool append(String& str, const String other) { +bool append(String* str, const String other) { + GEN_ASSERT(str != nullptr); return append(str, other, length(other)); } -bool append_fmt(String& str, char const* fmt, ...) { +bool append_fmt(String* str, char const* fmt, ...) { + GEN_ASSERT(str != nullptr); ssize res; char buf[GEN_PRINTF_MAXLEN] = { 0 }; @@ -286,7 +307,7 @@ bool append_fmt(String& str, char const* fmt, ...) { } inline -bool are_equal(String const& lhs, String const& rhs) +bool are_equal(String const lhs, String const rhs) { if (length(lhs) != length(rhs)) return false; @@ -299,7 +320,7 @@ bool are_equal(String const& lhs, String const& rhs) } inline -bool are_equal(String const& lhs, StrC rhs) +bool are_equal(String const lhs, StrC rhs) { if (length(lhs) != (rhs.Len)) return false; @@ -312,26 +333,26 @@ bool are_equal(String const& lhs, StrC rhs) } inline -ssize avail_space(String const& str) { - StringHeader const& header = *rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); - return header.Capacity - header.Length; +ssize avail_space(String const str) { + StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); + return header->Capacity - header->Length; } inline -char& back(String& str) { - return str[length(str) - 1]; +char* back(String* str) { + return & (*str)[length(* str) - 1]; } inline -bool contains(String const& str, StrC substring) +bool contains(String const str, StrC substring) { - StringHeader const& header = *rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); + StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); - if (substring.Len > header.Length) + if (substring.Len > header->Length) return false; - ssize main_len = header.Length; - ssize sub_len = substring.Len; + ssize main_len = header->Length; + ssize sub_len = substring.Len; for (ssize idx = 0; idx <= main_len - sub_len; ++idx) { @@ -343,15 +364,15 @@ bool contains(String const& str, StrC substring) } inline -bool contains(String const& str, String const& substring) +bool contains(String const str, String const substring) { - StringHeader const& header = *rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); + StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); - if (length(substring) > header.Length) + if (length(substring) > header->Length) return false; - ssize main_len = header.Length; - ssize sub_len = length(substring); + ssize main_len = header->Length; + ssize sub_len = length(substring); for (ssize idx = 0; idx <= main_len - sub_len; ++idx) { @@ -363,46 +384,47 @@ bool contains(String const& str, String const& substring) } inline -ssize capacity(String const& str) { - StringHeader const& header = *rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); - return header.Capacity; +ssize capacity(String const str) { + StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); + return header->Capacity; } inline -void clear(String& str) { - get_header(str).Length = 0; +void clear(String str) { + get_header(str)->Length = 0; } inline -String duplicate(String const& str, AllocatorInfo allocator) { +String duplicate(String const str, AllocatorInfo allocator) { return string_make_length(allocator, str, length(str)); } inline -void free(String& str) { - if (! str) - return; +void free(String* str) { + GEN_ASSERT(str != nullptr); + if (! (* str)) + return; - StringHeader& header = get_header(str); - GEN_NS free(header.Allocator, &header); + StringHeader* header = get_header(* str); + GEN_NS free(header->Allocator, header); } inline -StringHeader& get_header(String& str) { - return *(StringHeader*)(scast(char*, str) - sizeof(StringHeader)); +StringHeader* get_header(String str) { + return (StringHeader*)(scast(char*, str) - sizeof(StringHeader)); } inline -ssize length(String const& str) +ssize length(String const str) { StringHeader const& header = *rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); return header.Length; } inline -bool make_space_for(String& str, char const* to_append, ssize add_len) +bool make_space_for(String* str, char const* to_append, ssize add_len) { - ssize available = avail_space(str); + ssize available = avail_space(* str); if (available >= add_len) { return true; @@ -413,12 +435,12 @@ bool make_space_for(String& str, char const* to_append, ssize add_len) void* ptr; void* new_ptr; - AllocatorInfo allocator = get_header(str).Allocator; - StringHeader* header = nullptr; + AllocatorInfo allocator = get_header(* str)->Allocator; + StringHeader* header = nullptr; - new_len = string_grow_formula(length(str) + add_len); - ptr = &get_header(str); - old_size = size_of(StringHeader) + length(str) + 1; + new_len = string_grow_formula(length(* str) + add_len); + ptr = get_header(* str); + old_size = size_of(StringHeader) + length(* str) + 1; new_size = size_of(StringHeader) + new_len + 1; new_ptr = resize(allocator, ptr, old_size, new_size); @@ -428,16 +450,17 @@ bool make_space_for(String& str, char const* to_append, ssize add_len) header = rcast(StringHeader*, new_ptr); header->Allocator = allocator; - header->Capacity = new_len; + header->Capacity = new_len; - str.Data = rcast(char*, header + 1); + char** Data = rcast(char**, str); + * Data = rcast(char*, header + 1); return true; } } inline -b32 starts_with(String const& str, StrC substring) { +b32 starts_with(String const str, StrC substring) { if (substring.Len > length(str)) return false; @@ -446,7 +469,7 @@ b32 starts_with(String const& str, StrC substring) { } inline -b32 starts_with(String const& str, String substring) { +b32 starts_with(String const str, String substring) { if (length(substring) > length(str)) return false; @@ -455,7 +478,7 @@ b32 starts_with(String const& str, String substring) { } inline -void skip_line(String& str) +void skip_line(String str) { #define current (*scanner) char* scanner = str.Data; @@ -471,23 +494,23 @@ void skip_line(String& str) mem_move(str.Data, scanner, new_length); - StringHeader* header = &get_header(str); + StringHeader* header = get_header(str); header->Length = new_length; #undef current } inline -void strip_space(String& str) +void strip_space(String str) { - char* write_pos = str.Data; - char* read_pos = str.Data; + char* write_pos = str; + char* read_pos = str; - while (*read_pos) + while (* read_pos) { - if (!char_is_space(*read_pos)) + if (! char_is_space(* read_pos)) { - *write_pos = *read_pos; - write_pos++; + * write_pos = * read_pos; + write_pos++; } read_pos++; } @@ -495,11 +518,11 @@ void strip_space(String& str) write_pos[0] = '\0'; // Null-terminate the modified string // Update the length if needed - get_header(str).Length = write_pos - str.Data; + get_header(str)->Length = write_pos - str.Data; } inline -void trim(String& str, char const* cut_set) +void trim(String str, char const* cut_set) { ssize len = 0; @@ -519,16 +542,16 @@ void trim(String& str, char const* cut_set) str.Data[len] = '\0'; - get_header(str).Length = len; + get_header(str)->Length = len; } inline -void trim_space(String& str) { +void trim_space(String str) { trim(str, " \t\r\n\v\f"); } inline -String visualize_whitespace(String const& str) +String visualize_whitespace(String const str) { StringHeader* header = (StringHeader*)(scast(char const*, str) - sizeof(StringHeader)); String result = string_make_reserve(header->Allocator, length(str) * 2); // Assume worst case for space requirements. @@ -536,25 +559,25 @@ String visualize_whitespace(String const& str) for (auto c : str) switch (c) { case ' ': - append(result, txt("·")); + append(& result, txt("·")); break; case '\t': - append(result, txt("→")); + append(& result, txt("→")); break; case '\n': - append(result, txt("↵")); + append(& result, txt("↵")); break; case '\r': - append(result, txt("⏎")); + append(& result, txt("⏎")); break; case '\v': - append(result, txt("⇕")); + append(& result, txt("⇕")); break; case '\f': - append(result, txt("⌂")); + append(& result, txt("⌂")); break; default: - append(result, c); + append(& result, c); break; } diff --git a/project/helpers/helper.hpp b/project/helpers/helper.hpp index 5fa885c..388c0e9 100644 --- a/project/helpers/helper.hpp +++ b/project/helpers/helper.hpp @@ -27,8 +27,8 @@ CodeBody gen_ecode( char const* path ) { char const* code = node.string; - append_fmt( enum_entries, "%s,\n", code ); - append_fmt( to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", code, code ); + append_fmt( & enum_entries, "%s,\n", code ); + append_fmt( & to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", code, code ); } CodeEnum enum_code = parse_enum(gen::token_fmt_impl((3 + 1) / 2, "entries", (StrC)enum_entries, "enum Type : u32 { NumTypes };")); @@ -76,8 +76,8 @@ CodeBody gen_eoperator( char const* path ) char const* enum_str = enum_strs[idx].string; char const* entry_to_str = str_strs [idx].string; - append_fmt( enum_entries, "%s,\n", enum_str ); - append_fmt( to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); + append_fmt( & enum_entries, "%s,\n", enum_str ); + append_fmt( & to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); } CodeEnum enum_code = parse_enum(token_fmt("entries", (StrC)enum_entries, stringize( @@ -132,8 +132,8 @@ CodeBody gen_especifier( char const* path ) char const* enum_str = enum_strs[idx].string; char const* entry_to_str = str_strs [idx].string; - append_fmt( enum_entries, "%s,\n", enum_str ); - append_fmt( to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); + append_fmt( & enum_entries, "%s,\n", enum_str ); + append_fmt( & to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); } CodeEnum enum_code = parse_enum(token_fmt("entries", (StrC)enum_entries, stringize( @@ -249,8 +249,8 @@ CodeBody gen_etoktype( char const* etok_path, char const* attr_path ) char const* enum_str = enum_strs[idx].string; char const* entry_to_str = enum_str_strs [idx].string; - append_fmt( enum_entries, "%s,\n", enum_str ); - append_fmt( to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); + append_fmt( & enum_entries, "%s,\n", enum_str ); + append_fmt( & to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); } for ( usize idx = 0; idx < num(attribute_strs); idx++ ) @@ -258,14 +258,14 @@ CodeBody gen_etoktype( char const* etok_path, char const* attr_path ) char const* attribute_str = attribute_strs[idx].string; char const* entry_to_str = attribute_str_strs [idx].string; - append_fmt( attribute_entries, "Attribute_%s,\n", attribute_str ); - append_fmt( to_str_attributes, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); - append_fmt( attribute_define_entries, "Entry( Attribute_%s, \"%s\" )", attribute_str, entry_to_str ); + append_fmt( & attribute_entries, "Attribute_%s,\n", attribute_str ); + append_fmt( & to_str_attributes, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); + append_fmt( & attribute_define_entries, "Entry( Attribute_%s, \"%s\" )", attribute_str, entry_to_str ); if ( idx < num(attribute_strs) - 1 ) - append( attribute_define_entries, " \\\n"); + append( & attribute_define_entries, " \\\n"); else - append( attribute_define_entries, "\n"); + append( & attribute_define_entries, "\n"); } #pragma push_macro("GEN_DEFINE_ATTRIBUTE_TOKENS")